nouveau_bo.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /*
  2. * Copyright 2007 Dave Airlied
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Authors: Dave Airlied <airlied@linux.ie>
  26. * Ben Skeggs <darktama@iinet.net.au>
  27. * Jeremy Kolb <jkolb@brandeis.edu>
  28. */
  29. #include <linux/dma-mapping.h>
  30. #include <linux/swiotlb.h>
  31. #include "nouveau_drm.h"
  32. #include "nouveau_dma.h"
  33. #include "nouveau_fence.h"
  34. #include "nouveau_bo.h"
  35. #include "nouveau_ttm.h"
  36. #include "nouveau_gem.h"
  37. /*
  38. * NV10-NV40 tiling helpers
  39. */
  40. static void
  41. nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
  42. u32 addr, u32 size, u32 pitch, u32 flags)
  43. {
  44. struct nouveau_drm *drm = nouveau_drm(dev);
  45. int i = reg - drm->tile.reg;
  46. struct nvkm_fb *pfb = nvxx_fb(&drm->device);
  47. struct nvkm_fb_tile *tile = &pfb->tile.region[i];
  48. struct nvkm_engine *engine;
  49. nouveau_fence_unref(&reg->fence);
  50. if (tile->pitch)
  51. pfb->tile.fini(pfb, i, tile);
  52. if (pitch)
  53. pfb->tile.init(pfb, i, addr, size, pitch, flags, tile);
  54. pfb->tile.prog(pfb, i, tile);
  55. if ((engine = nvkm_engine(pfb, NVDEV_ENGINE_GR)))
  56. engine->tile_prog(engine, i);
  57. if ((engine = nvkm_engine(pfb, NVDEV_ENGINE_MPEG)))
  58. engine->tile_prog(engine, i);
  59. }
  60. static struct nouveau_drm_tile *
  61. nv10_bo_get_tile_region(struct drm_device *dev, int i)
  62. {
  63. struct nouveau_drm *drm = nouveau_drm(dev);
  64. struct nouveau_drm_tile *tile = &drm->tile.reg[i];
  65. spin_lock(&drm->tile.lock);
  66. if (!tile->used &&
  67. (!tile->fence || nouveau_fence_done(tile->fence)))
  68. tile->used = true;
  69. else
  70. tile = NULL;
  71. spin_unlock(&drm->tile.lock);
  72. return tile;
  73. }
  74. static void
  75. nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
  76. struct fence *fence)
  77. {
  78. struct nouveau_drm *drm = nouveau_drm(dev);
  79. if (tile) {
  80. spin_lock(&drm->tile.lock);
  81. tile->fence = (struct nouveau_fence *)fence_get(fence);
  82. tile->used = false;
  83. spin_unlock(&drm->tile.lock);
  84. }
  85. }
  86. static struct nouveau_drm_tile *
  87. nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
  88. u32 size, u32 pitch, u32 flags)
  89. {
  90. struct nouveau_drm *drm = nouveau_drm(dev);
  91. struct nvkm_fb *pfb = nvxx_fb(&drm->device);
  92. struct nouveau_drm_tile *tile, *found = NULL;
  93. int i;
  94. for (i = 0; i < pfb->tile.regions; i++) {
  95. tile = nv10_bo_get_tile_region(dev, i);
  96. if (pitch && !found) {
  97. found = tile;
  98. continue;
  99. } else if (tile && pfb->tile.region[i].pitch) {
  100. /* Kill an unused tile region. */
  101. nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
  102. }
  103. nv10_bo_put_tile_region(dev, tile, NULL);
  104. }
  105. if (found)
  106. nv10_bo_update_tile_region(dev, found, addr, size,
  107. pitch, flags);
  108. return found;
  109. }
  110. static void
  111. nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
  112. {
  113. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  114. struct drm_device *dev = drm->dev;
  115. struct nouveau_bo *nvbo = nouveau_bo(bo);
  116. if (unlikely(nvbo->gem.filp))
  117. DRM_ERROR("bo %p still attached to GEM object\n", bo);
  118. WARN_ON(nvbo->pin_refcnt > 0);
  119. nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
  120. kfree(nvbo);
  121. }
  122. static void
  123. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
  124. int *align, int *size)
  125. {
  126. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  127. struct nvif_device *device = &drm->device;
  128. if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
  129. if (nvbo->tile_mode) {
  130. if (device->info.chipset >= 0x40) {
  131. *align = 65536;
  132. *size = roundup(*size, 64 * nvbo->tile_mode);
  133. } else if (device->info.chipset >= 0x30) {
  134. *align = 32768;
  135. *size = roundup(*size, 64 * nvbo->tile_mode);
  136. } else if (device->info.chipset >= 0x20) {
  137. *align = 16384;
  138. *size = roundup(*size, 64 * nvbo->tile_mode);
  139. } else if (device->info.chipset >= 0x10) {
  140. *align = 16384;
  141. *size = roundup(*size, 32 * nvbo->tile_mode);
  142. }
  143. }
  144. } else {
  145. *size = roundup(*size, (1 << nvbo->page_shift));
  146. *align = max((1 << nvbo->page_shift), *align);
  147. }
  148. *size = roundup(*size, PAGE_SIZE);
  149. }
  150. int
  151. nouveau_bo_new(struct drm_device *dev, int size, int align,
  152. uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
  153. struct sg_table *sg, struct reservation_object *robj,
  154. struct nouveau_bo **pnvbo)
  155. {
  156. struct nouveau_drm *drm = nouveau_drm(dev);
  157. struct nouveau_bo *nvbo;
  158. size_t acc_size;
  159. int ret;
  160. int type = ttm_bo_type_device;
  161. int lpg_shift = 12;
  162. int max_size;
  163. if (drm->client.vm)
  164. lpg_shift = drm->client.vm->mmu->lpg_shift;
  165. max_size = INT_MAX & ~((1 << lpg_shift) - 1);
  166. if (size <= 0 || size > max_size) {
  167. NV_WARN(drm, "skipped size %x\n", (u32)size);
  168. return -EINVAL;
  169. }
  170. if (sg)
  171. type = ttm_bo_type_sg;
  172. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  173. if (!nvbo)
  174. return -ENOMEM;
  175. INIT_LIST_HEAD(&nvbo->head);
  176. INIT_LIST_HEAD(&nvbo->entry);
  177. INIT_LIST_HEAD(&nvbo->vma_list);
  178. nvbo->tile_mode = tile_mode;
  179. nvbo->tile_flags = tile_flags;
  180. nvbo->bo.bdev = &drm->ttm.bdev;
  181. if (!nv_device_is_cpu_coherent(nvxx_device(&drm->device)))
  182. nvbo->force_coherent = flags & TTM_PL_FLAG_UNCACHED;
  183. nvbo->page_shift = 12;
  184. if (drm->client.vm) {
  185. if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
  186. nvbo->page_shift = drm->client.vm->mmu->lpg_shift;
  187. }
  188. nouveau_bo_fixup_align(nvbo, flags, &align, &size);
  189. nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
  190. nouveau_bo_placement_set(nvbo, flags, 0);
  191. acc_size = ttm_bo_dma_acc_size(&drm->ttm.bdev, size,
  192. sizeof(struct nouveau_bo));
  193. ret = ttm_bo_init(&drm->ttm.bdev, &nvbo->bo, size,
  194. type, &nvbo->placement,
  195. align >> PAGE_SHIFT, false, NULL, acc_size, sg,
  196. robj, nouveau_bo_del_ttm);
  197. if (ret) {
  198. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  199. return ret;
  200. }
  201. *pnvbo = nvbo;
  202. return 0;
  203. }
  204. static void
  205. set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t type, uint32_t flags)
  206. {
  207. *n = 0;
  208. if (type & TTM_PL_FLAG_VRAM)
  209. pl[(*n)++].flags = TTM_PL_FLAG_VRAM | flags;
  210. if (type & TTM_PL_FLAG_TT)
  211. pl[(*n)++].flags = TTM_PL_FLAG_TT | flags;
  212. if (type & TTM_PL_FLAG_SYSTEM)
  213. pl[(*n)++].flags = TTM_PL_FLAG_SYSTEM | flags;
  214. }
  215. static void
  216. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  217. {
  218. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  219. u32 vram_pages = drm->device.info.ram_size >> PAGE_SHIFT;
  220. unsigned i, fpfn, lpfn;
  221. if (drm->device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
  222. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  223. nvbo->bo.mem.num_pages < vram_pages / 4) {
  224. /*
  225. * Make sure that the color and depth buffers are handled
  226. * by independent memory controller units. Up to a 9x
  227. * speed up when alpha-blending and depth-test are enabled
  228. * at the same time.
  229. */
  230. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  231. fpfn = vram_pages / 2;
  232. lpfn = ~0;
  233. } else {
  234. fpfn = 0;
  235. lpfn = vram_pages / 2;
  236. }
  237. for (i = 0; i < nvbo->placement.num_placement; ++i) {
  238. nvbo->placements[i].fpfn = fpfn;
  239. nvbo->placements[i].lpfn = lpfn;
  240. }
  241. for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
  242. nvbo->busy_placements[i].fpfn = fpfn;
  243. nvbo->busy_placements[i].lpfn = lpfn;
  244. }
  245. }
  246. }
  247. void
  248. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  249. {
  250. struct ttm_placement *pl = &nvbo->placement;
  251. uint32_t flags = (nvbo->force_coherent ? TTM_PL_FLAG_UNCACHED :
  252. TTM_PL_MASK_CACHING) |
  253. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  254. pl->placement = nvbo->placements;
  255. set_placement_list(nvbo->placements, &pl->num_placement,
  256. type, flags);
  257. pl->busy_placement = nvbo->busy_placements;
  258. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  259. type | busy, flags);
  260. set_placement_range(nvbo, type);
  261. }
  262. int
  263. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype, bool contig)
  264. {
  265. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  266. struct ttm_buffer_object *bo = &nvbo->bo;
  267. bool force = false, evict = false;
  268. int ret;
  269. ret = ttm_bo_reserve(bo, false, false, false, NULL);
  270. if (ret)
  271. return ret;
  272. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
  273. memtype == TTM_PL_FLAG_VRAM && contig) {
  274. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
  275. if (bo->mem.mem_type == TTM_PL_VRAM) {
  276. struct nvkm_mem *mem = bo->mem.mm_node;
  277. if (!list_is_singular(&mem->regions))
  278. evict = true;
  279. }
  280. nvbo->tile_flags &= ~NOUVEAU_GEM_TILE_NONCONTIG;
  281. force = true;
  282. }
  283. }
  284. if (nvbo->pin_refcnt) {
  285. if (!(memtype & (1 << bo->mem.mem_type)) || evict) {
  286. NV_ERROR(drm, "bo %p pinned elsewhere: "
  287. "0x%08x vs 0x%08x\n", bo,
  288. 1 << bo->mem.mem_type, memtype);
  289. ret = -EBUSY;
  290. }
  291. nvbo->pin_refcnt++;
  292. goto out;
  293. }
  294. if (evict) {
  295. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT, 0);
  296. ret = nouveau_bo_validate(nvbo, false, false);
  297. if (ret)
  298. goto out;
  299. }
  300. nvbo->pin_refcnt++;
  301. nouveau_bo_placement_set(nvbo, memtype, 0);
  302. /* drop pin_refcnt temporarily, so we don't trip the assertion
  303. * in nouveau_bo_move() that makes sure we're not trying to
  304. * move a pinned buffer
  305. */
  306. nvbo->pin_refcnt--;
  307. ret = nouveau_bo_validate(nvbo, false, false);
  308. if (ret)
  309. goto out;
  310. nvbo->pin_refcnt++;
  311. switch (bo->mem.mem_type) {
  312. case TTM_PL_VRAM:
  313. drm->gem.vram_available -= bo->mem.size;
  314. break;
  315. case TTM_PL_TT:
  316. drm->gem.gart_available -= bo->mem.size;
  317. break;
  318. default:
  319. break;
  320. }
  321. out:
  322. if (force && ret)
  323. nvbo->tile_flags |= NOUVEAU_GEM_TILE_NONCONTIG;
  324. ttm_bo_unreserve(bo);
  325. return ret;
  326. }
  327. int
  328. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  329. {
  330. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  331. struct ttm_buffer_object *bo = &nvbo->bo;
  332. int ret, ref;
  333. ret = ttm_bo_reserve(bo, false, false, false, NULL);
  334. if (ret)
  335. return ret;
  336. ref = --nvbo->pin_refcnt;
  337. WARN_ON_ONCE(ref < 0);
  338. if (ref)
  339. goto out;
  340. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  341. ret = nouveau_bo_validate(nvbo, false, false);
  342. if (ret == 0) {
  343. switch (bo->mem.mem_type) {
  344. case TTM_PL_VRAM:
  345. drm->gem.vram_available += bo->mem.size;
  346. break;
  347. case TTM_PL_TT:
  348. drm->gem.gart_available += bo->mem.size;
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. out:
  355. ttm_bo_unreserve(bo);
  356. return ret;
  357. }
  358. int
  359. nouveau_bo_map(struct nouveau_bo *nvbo)
  360. {
  361. int ret;
  362. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
  363. if (ret)
  364. return ret;
  365. /*
  366. * TTM buffers allocated using the DMA API already have a mapping, let's
  367. * use it instead.
  368. */
  369. if (!nvbo->force_coherent)
  370. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
  371. &nvbo->kmap);
  372. ttm_bo_unreserve(&nvbo->bo);
  373. return ret;
  374. }
  375. void
  376. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  377. {
  378. if (!nvbo)
  379. return;
  380. /*
  381. * TTM buffers allocated using the DMA API already had a coherent
  382. * mapping which we used, no need to unmap.
  383. */
  384. if (!nvbo->force_coherent)
  385. ttm_bo_kunmap(&nvbo->kmap);
  386. }
  387. void
  388. nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
  389. {
  390. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  391. struct nvkm_device *device = nvxx_device(&drm->device);
  392. struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
  393. int i;
  394. if (!ttm_dma)
  395. return;
  396. /* Don't waste time looping if the object is coherent */
  397. if (nvbo->force_coherent)
  398. return;
  399. for (i = 0; i < ttm_dma->ttm.num_pages; i++)
  400. dma_sync_single_for_device(nv_device_base(device),
  401. ttm_dma->dma_address[i], PAGE_SIZE, DMA_TO_DEVICE);
  402. }
  403. void
  404. nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
  405. {
  406. struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
  407. struct nvkm_device *device = nvxx_device(&drm->device);
  408. struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
  409. int i;
  410. if (!ttm_dma)
  411. return;
  412. /* Don't waste time looping if the object is coherent */
  413. if (nvbo->force_coherent)
  414. return;
  415. for (i = 0; i < ttm_dma->ttm.num_pages; i++)
  416. dma_sync_single_for_cpu(nv_device_base(device),
  417. ttm_dma->dma_address[i], PAGE_SIZE, DMA_FROM_DEVICE);
  418. }
  419. int
  420. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  421. bool no_wait_gpu)
  422. {
  423. int ret;
  424. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
  425. interruptible, no_wait_gpu);
  426. if (ret)
  427. return ret;
  428. nouveau_bo_sync_for_device(nvbo);
  429. return 0;
  430. }
  431. static inline void *
  432. _nouveau_bo_mem_index(struct nouveau_bo *nvbo, unsigned index, void *mem, u8 sz)
  433. {
  434. struct ttm_dma_tt *dma_tt;
  435. u8 *m = mem;
  436. index *= sz;
  437. if (m) {
  438. /* kmap'd address, return the corresponding offset */
  439. m += index;
  440. } else {
  441. /* DMA-API mapping, lookup the right address */
  442. dma_tt = (struct ttm_dma_tt *)nvbo->bo.ttm;
  443. m = dma_tt->cpu_address[index / PAGE_SIZE];
  444. m += index % PAGE_SIZE;
  445. }
  446. return m;
  447. }
  448. #define nouveau_bo_mem_index(o, i, m) _nouveau_bo_mem_index(o, i, m, sizeof(*m))
  449. void
  450. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  451. {
  452. bool is_iomem;
  453. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  454. mem = nouveau_bo_mem_index(nvbo, index, mem);
  455. if (is_iomem)
  456. iowrite16_native(val, (void __force __iomem *)mem);
  457. else
  458. *mem = val;
  459. }
  460. u32
  461. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  462. {
  463. bool is_iomem;
  464. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  465. mem = nouveau_bo_mem_index(nvbo, index, mem);
  466. if (is_iomem)
  467. return ioread32_native((void __force __iomem *)mem);
  468. else
  469. return *mem;
  470. }
  471. void
  472. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  473. {
  474. bool is_iomem;
  475. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  476. mem = nouveau_bo_mem_index(nvbo, index, mem);
  477. if (is_iomem)
  478. iowrite32_native(val, (void __force __iomem *)mem);
  479. else
  480. *mem = val;
  481. }
  482. static struct ttm_tt *
  483. nouveau_ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
  484. uint32_t page_flags, struct page *dummy_read)
  485. {
  486. #if __OS_HAS_AGP
  487. struct nouveau_drm *drm = nouveau_bdev(bdev);
  488. struct drm_device *dev = drm->dev;
  489. if (drm->agp.stat == ENABLED) {
  490. return ttm_agp_tt_create(bdev, dev->agp->bridge, size,
  491. page_flags, dummy_read);
  492. }
  493. #endif
  494. return nouveau_sgdma_create_ttm(bdev, size, page_flags, dummy_read);
  495. }
  496. static int
  497. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  498. {
  499. /* We'll do this from user space. */
  500. return 0;
  501. }
  502. static int
  503. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  504. struct ttm_mem_type_manager *man)
  505. {
  506. struct nouveau_drm *drm = nouveau_bdev(bdev);
  507. switch (type) {
  508. case TTM_PL_SYSTEM:
  509. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  510. man->available_caching = TTM_PL_MASK_CACHING;
  511. man->default_caching = TTM_PL_FLAG_CACHED;
  512. break;
  513. case TTM_PL_VRAM:
  514. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  515. TTM_MEMTYPE_FLAG_MAPPABLE;
  516. man->available_caching = TTM_PL_FLAG_UNCACHED |
  517. TTM_PL_FLAG_WC;
  518. man->default_caching = TTM_PL_FLAG_WC;
  519. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  520. /* Some BARs do not support being ioremapped WC */
  521. if (nvxx_bar(&drm->device)->iomap_uncached) {
  522. man->available_caching = TTM_PL_FLAG_UNCACHED;
  523. man->default_caching = TTM_PL_FLAG_UNCACHED;
  524. }
  525. man->func = &nouveau_vram_manager;
  526. man->io_reserve_fastpath = false;
  527. man->use_io_reserve_lru = true;
  528. } else {
  529. man->func = &ttm_bo_manager_func;
  530. }
  531. break;
  532. case TTM_PL_TT:
  533. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
  534. man->func = &nouveau_gart_manager;
  535. else
  536. if (drm->agp.stat != ENABLED)
  537. man->func = &nv04_gart_manager;
  538. else
  539. man->func = &ttm_bo_manager_func;
  540. if (drm->agp.stat == ENABLED) {
  541. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  542. man->available_caching = TTM_PL_FLAG_UNCACHED |
  543. TTM_PL_FLAG_WC;
  544. man->default_caching = TTM_PL_FLAG_WC;
  545. } else {
  546. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  547. TTM_MEMTYPE_FLAG_CMA;
  548. man->available_caching = TTM_PL_MASK_CACHING;
  549. man->default_caching = TTM_PL_FLAG_CACHED;
  550. }
  551. break;
  552. default:
  553. return -EINVAL;
  554. }
  555. return 0;
  556. }
  557. static void
  558. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  559. {
  560. struct nouveau_bo *nvbo = nouveau_bo(bo);
  561. switch (bo->mem.mem_type) {
  562. case TTM_PL_VRAM:
  563. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  564. TTM_PL_FLAG_SYSTEM);
  565. break;
  566. default:
  567. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  568. break;
  569. }
  570. *pl = nvbo->placement;
  571. }
  572. static int
  573. nve0_bo_move_init(struct nouveau_channel *chan, u32 handle)
  574. {
  575. int ret = RING_SPACE(chan, 2);
  576. if (ret == 0) {
  577. BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
  578. OUT_RING (chan, handle & 0x0000ffff);
  579. FIRE_RING (chan);
  580. }
  581. return ret;
  582. }
  583. static int
  584. nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  585. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  586. {
  587. struct nvkm_mem *node = old_mem->mm_node;
  588. int ret = RING_SPACE(chan, 10);
  589. if (ret == 0) {
  590. BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
  591. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  592. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  593. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  594. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  595. OUT_RING (chan, PAGE_SIZE);
  596. OUT_RING (chan, PAGE_SIZE);
  597. OUT_RING (chan, PAGE_SIZE);
  598. OUT_RING (chan, new_mem->num_pages);
  599. BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
  600. }
  601. return ret;
  602. }
  603. static int
  604. nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle)
  605. {
  606. int ret = RING_SPACE(chan, 2);
  607. if (ret == 0) {
  608. BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
  609. OUT_RING (chan, handle);
  610. }
  611. return ret;
  612. }
  613. static int
  614. nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  615. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  616. {
  617. struct nvkm_mem *node = old_mem->mm_node;
  618. u64 src_offset = node->vma[0].offset;
  619. u64 dst_offset = node->vma[1].offset;
  620. u32 page_count = new_mem->num_pages;
  621. int ret;
  622. page_count = new_mem->num_pages;
  623. while (page_count) {
  624. int line_count = (page_count > 8191) ? 8191 : page_count;
  625. ret = RING_SPACE(chan, 11);
  626. if (ret)
  627. return ret;
  628. BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8);
  629. OUT_RING (chan, upper_32_bits(src_offset));
  630. OUT_RING (chan, lower_32_bits(src_offset));
  631. OUT_RING (chan, upper_32_bits(dst_offset));
  632. OUT_RING (chan, lower_32_bits(dst_offset));
  633. OUT_RING (chan, PAGE_SIZE);
  634. OUT_RING (chan, PAGE_SIZE);
  635. OUT_RING (chan, PAGE_SIZE);
  636. OUT_RING (chan, line_count);
  637. BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
  638. OUT_RING (chan, 0x00000110);
  639. page_count -= line_count;
  640. src_offset += (PAGE_SIZE * line_count);
  641. dst_offset += (PAGE_SIZE * line_count);
  642. }
  643. return 0;
  644. }
  645. static int
  646. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  647. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  648. {
  649. struct nvkm_mem *node = old_mem->mm_node;
  650. u64 src_offset = node->vma[0].offset;
  651. u64 dst_offset = node->vma[1].offset;
  652. u32 page_count = new_mem->num_pages;
  653. int ret;
  654. page_count = new_mem->num_pages;
  655. while (page_count) {
  656. int line_count = (page_count > 2047) ? 2047 : page_count;
  657. ret = RING_SPACE(chan, 12);
  658. if (ret)
  659. return ret;
  660. BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2);
  661. OUT_RING (chan, upper_32_bits(dst_offset));
  662. OUT_RING (chan, lower_32_bits(dst_offset));
  663. BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6);
  664. OUT_RING (chan, upper_32_bits(src_offset));
  665. OUT_RING (chan, lower_32_bits(src_offset));
  666. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  667. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  668. OUT_RING (chan, PAGE_SIZE); /* line_length */
  669. OUT_RING (chan, line_count);
  670. BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
  671. OUT_RING (chan, 0x00100110);
  672. page_count -= line_count;
  673. src_offset += (PAGE_SIZE * line_count);
  674. dst_offset += (PAGE_SIZE * line_count);
  675. }
  676. return 0;
  677. }
  678. static int
  679. nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  680. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  681. {
  682. struct nvkm_mem *node = old_mem->mm_node;
  683. u64 src_offset = node->vma[0].offset;
  684. u64 dst_offset = node->vma[1].offset;
  685. u32 page_count = new_mem->num_pages;
  686. int ret;
  687. page_count = new_mem->num_pages;
  688. while (page_count) {
  689. int line_count = (page_count > 8191) ? 8191 : page_count;
  690. ret = RING_SPACE(chan, 11);
  691. if (ret)
  692. return ret;
  693. BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
  694. OUT_RING (chan, upper_32_bits(src_offset));
  695. OUT_RING (chan, lower_32_bits(src_offset));
  696. OUT_RING (chan, upper_32_bits(dst_offset));
  697. OUT_RING (chan, lower_32_bits(dst_offset));
  698. OUT_RING (chan, PAGE_SIZE);
  699. OUT_RING (chan, PAGE_SIZE);
  700. OUT_RING (chan, PAGE_SIZE);
  701. OUT_RING (chan, line_count);
  702. BEGIN_NV04(chan, NvSubCopy, 0x0300, 1);
  703. OUT_RING (chan, 0x00000110);
  704. page_count -= line_count;
  705. src_offset += (PAGE_SIZE * line_count);
  706. dst_offset += (PAGE_SIZE * line_count);
  707. }
  708. return 0;
  709. }
  710. static int
  711. nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  712. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  713. {
  714. struct nvkm_mem *node = old_mem->mm_node;
  715. int ret = RING_SPACE(chan, 7);
  716. if (ret == 0) {
  717. BEGIN_NV04(chan, NvSubCopy, 0x0320, 6);
  718. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  719. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  720. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  721. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  722. OUT_RING (chan, 0x00000000 /* COPY */);
  723. OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
  724. }
  725. return ret;
  726. }
  727. static int
  728. nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  729. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  730. {
  731. struct nvkm_mem *node = old_mem->mm_node;
  732. int ret = RING_SPACE(chan, 7);
  733. if (ret == 0) {
  734. BEGIN_NV04(chan, NvSubCopy, 0x0304, 6);
  735. OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
  736. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  737. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  738. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  739. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  740. OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */);
  741. }
  742. return ret;
  743. }
  744. static int
  745. nv50_bo_move_init(struct nouveau_channel *chan, u32 handle)
  746. {
  747. int ret = RING_SPACE(chan, 6);
  748. if (ret == 0) {
  749. BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
  750. OUT_RING (chan, handle);
  751. BEGIN_NV04(chan, NvSubCopy, 0x0180, 3);
  752. OUT_RING (chan, chan->drm->ntfy.handle);
  753. OUT_RING (chan, chan->vram.handle);
  754. OUT_RING (chan, chan->vram.handle);
  755. }
  756. return ret;
  757. }
  758. static int
  759. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  760. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  761. {
  762. struct nvkm_mem *node = old_mem->mm_node;
  763. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  764. u64 src_offset = node->vma[0].offset;
  765. u64 dst_offset = node->vma[1].offset;
  766. int src_tiled = !!node->memtype;
  767. int dst_tiled = !!((struct nvkm_mem *)new_mem->mm_node)->memtype;
  768. int ret;
  769. while (length) {
  770. u32 amount, stride, height;
  771. ret = RING_SPACE(chan, 18 + 6 * (src_tiled + dst_tiled));
  772. if (ret)
  773. return ret;
  774. amount = min(length, (u64)(4 * 1024 * 1024));
  775. stride = 16 * 4;
  776. height = amount / stride;
  777. if (src_tiled) {
  778. BEGIN_NV04(chan, NvSubCopy, 0x0200, 7);
  779. OUT_RING (chan, 0);
  780. OUT_RING (chan, 0);
  781. OUT_RING (chan, stride);
  782. OUT_RING (chan, height);
  783. OUT_RING (chan, 1);
  784. OUT_RING (chan, 0);
  785. OUT_RING (chan, 0);
  786. } else {
  787. BEGIN_NV04(chan, NvSubCopy, 0x0200, 1);
  788. OUT_RING (chan, 1);
  789. }
  790. if (dst_tiled) {
  791. BEGIN_NV04(chan, NvSubCopy, 0x021c, 7);
  792. OUT_RING (chan, 0);
  793. OUT_RING (chan, 0);
  794. OUT_RING (chan, stride);
  795. OUT_RING (chan, height);
  796. OUT_RING (chan, 1);
  797. OUT_RING (chan, 0);
  798. OUT_RING (chan, 0);
  799. } else {
  800. BEGIN_NV04(chan, NvSubCopy, 0x021c, 1);
  801. OUT_RING (chan, 1);
  802. }
  803. BEGIN_NV04(chan, NvSubCopy, 0x0238, 2);
  804. OUT_RING (chan, upper_32_bits(src_offset));
  805. OUT_RING (chan, upper_32_bits(dst_offset));
  806. BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
  807. OUT_RING (chan, lower_32_bits(src_offset));
  808. OUT_RING (chan, lower_32_bits(dst_offset));
  809. OUT_RING (chan, stride);
  810. OUT_RING (chan, stride);
  811. OUT_RING (chan, stride);
  812. OUT_RING (chan, height);
  813. OUT_RING (chan, 0x00000101);
  814. OUT_RING (chan, 0x00000000);
  815. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  816. OUT_RING (chan, 0);
  817. length -= amount;
  818. src_offset += amount;
  819. dst_offset += amount;
  820. }
  821. return 0;
  822. }
  823. static int
  824. nv04_bo_move_init(struct nouveau_channel *chan, u32 handle)
  825. {
  826. int ret = RING_SPACE(chan, 4);
  827. if (ret == 0) {
  828. BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
  829. OUT_RING (chan, handle);
  830. BEGIN_NV04(chan, NvSubCopy, 0x0180, 1);
  831. OUT_RING (chan, chan->drm->ntfy.handle);
  832. }
  833. return ret;
  834. }
  835. static inline uint32_t
  836. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  837. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  838. {
  839. if (mem->mem_type == TTM_PL_TT)
  840. return NvDmaTT;
  841. return chan->vram.handle;
  842. }
  843. static int
  844. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  845. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  846. {
  847. u32 src_offset = old_mem->start << PAGE_SHIFT;
  848. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  849. u32 page_count = new_mem->num_pages;
  850. int ret;
  851. ret = RING_SPACE(chan, 3);
  852. if (ret)
  853. return ret;
  854. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  855. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  856. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  857. page_count = new_mem->num_pages;
  858. while (page_count) {
  859. int line_count = (page_count > 2047) ? 2047 : page_count;
  860. ret = RING_SPACE(chan, 11);
  861. if (ret)
  862. return ret;
  863. BEGIN_NV04(chan, NvSubCopy,
  864. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  865. OUT_RING (chan, src_offset);
  866. OUT_RING (chan, dst_offset);
  867. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  868. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  869. OUT_RING (chan, PAGE_SIZE); /* line_length */
  870. OUT_RING (chan, line_count);
  871. OUT_RING (chan, 0x00000101);
  872. OUT_RING (chan, 0x00000000);
  873. BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  874. OUT_RING (chan, 0);
  875. page_count -= line_count;
  876. src_offset += (PAGE_SIZE * line_count);
  877. dst_offset += (PAGE_SIZE * line_count);
  878. }
  879. return 0;
  880. }
  881. static int
  882. nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
  883. struct ttm_mem_reg *mem)
  884. {
  885. struct nvkm_mem *old_node = bo->mem.mm_node;
  886. struct nvkm_mem *new_node = mem->mm_node;
  887. u64 size = (u64)mem->num_pages << PAGE_SHIFT;
  888. int ret;
  889. ret = nvkm_vm_get(drm->client.vm, size, old_node->page_shift,
  890. NV_MEM_ACCESS_RW, &old_node->vma[0]);
  891. if (ret)
  892. return ret;
  893. ret = nvkm_vm_get(drm->client.vm, size, new_node->page_shift,
  894. NV_MEM_ACCESS_RW, &old_node->vma[1]);
  895. if (ret) {
  896. nvkm_vm_put(&old_node->vma[0]);
  897. return ret;
  898. }
  899. nvkm_vm_map(&old_node->vma[0], old_node);
  900. nvkm_vm_map(&old_node->vma[1], new_node);
  901. return 0;
  902. }
  903. static int
  904. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  905. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  906. {
  907. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  908. struct nouveau_channel *chan = drm->ttm.chan;
  909. struct nouveau_cli *cli = (void *)nvif_client(&chan->device->base);
  910. struct nouveau_fence *fence;
  911. int ret;
  912. /* create temporary vmas for the transfer and attach them to the
  913. * old nvkm_mem node, these will get cleaned up after ttm has
  914. * destroyed the ttm_mem_reg
  915. */
  916. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  917. ret = nouveau_bo_move_prep(drm, bo, new_mem);
  918. if (ret)
  919. return ret;
  920. }
  921. mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
  922. ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, intr);
  923. if (ret == 0) {
  924. ret = drm->ttm.move(chan, bo, &bo->mem, new_mem);
  925. if (ret == 0) {
  926. ret = nouveau_fence_new(chan, false, &fence);
  927. if (ret == 0) {
  928. ret = ttm_bo_move_accel_cleanup(bo,
  929. &fence->base,
  930. evict,
  931. no_wait_gpu,
  932. new_mem);
  933. nouveau_fence_unref(&fence);
  934. }
  935. }
  936. }
  937. mutex_unlock(&cli->mutex);
  938. return ret;
  939. }
  940. void
  941. nouveau_bo_move_init(struct nouveau_drm *drm)
  942. {
  943. static const struct {
  944. const char *name;
  945. int engine;
  946. u32 oclass;
  947. int (*exec)(struct nouveau_channel *,
  948. struct ttm_buffer_object *,
  949. struct ttm_mem_reg *, struct ttm_mem_reg *);
  950. int (*init)(struct nouveau_channel *, u32 handle);
  951. } _methods[] = {
  952. { "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
  953. { "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  954. { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
  955. { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
  956. { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
  957. { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
  958. { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
  959. { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
  960. { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
  961. { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
  962. { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
  963. {},
  964. { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init },
  965. }, *mthd = _methods;
  966. const char *name = "CPU";
  967. int ret;
  968. do {
  969. struct nouveau_channel *chan;
  970. if (mthd->engine)
  971. chan = drm->cechan;
  972. else
  973. chan = drm->channel;
  974. if (chan == NULL)
  975. continue;
  976. ret = nvif_object_init(chan->object, NULL,
  977. mthd->oclass | (mthd->engine << 16),
  978. mthd->oclass, NULL, 0,
  979. &drm->ttm.copy);
  980. if (ret == 0) {
  981. ret = mthd->init(chan, drm->ttm.copy.handle);
  982. if (ret) {
  983. nvif_object_fini(&drm->ttm.copy);
  984. continue;
  985. }
  986. drm->ttm.move = mthd->exec;
  987. drm->ttm.chan = chan;
  988. name = mthd->name;
  989. break;
  990. }
  991. } while ((++mthd)->exec);
  992. NV_INFO(drm, "MM: using %s for buffer copies\n", name);
  993. }
  994. static int
  995. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  996. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  997. {
  998. struct ttm_place placement_memtype = {
  999. .fpfn = 0,
  1000. .lpfn = 0,
  1001. .flags = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING
  1002. };
  1003. struct ttm_placement placement;
  1004. struct ttm_mem_reg tmp_mem;
  1005. int ret;
  1006. placement.num_placement = placement.num_busy_placement = 1;
  1007. placement.placement = placement.busy_placement = &placement_memtype;
  1008. tmp_mem = *new_mem;
  1009. tmp_mem.mm_node = NULL;
  1010. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
  1011. if (ret)
  1012. return ret;
  1013. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  1014. if (ret)
  1015. goto out;
  1016. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_mem);
  1017. if (ret)
  1018. goto out;
  1019. ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
  1020. out:
  1021. ttm_bo_mem_put(bo, &tmp_mem);
  1022. return ret;
  1023. }
  1024. static int
  1025. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  1026. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  1027. {
  1028. struct ttm_place placement_memtype = {
  1029. .fpfn = 0,
  1030. .lpfn = 0,
  1031. .flags = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING
  1032. };
  1033. struct ttm_placement placement;
  1034. struct ttm_mem_reg tmp_mem;
  1035. int ret;
  1036. placement.num_placement = placement.num_busy_placement = 1;
  1037. placement.placement = placement.busy_placement = &placement_memtype;
  1038. tmp_mem = *new_mem;
  1039. tmp_mem.mm_node = NULL;
  1040. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
  1041. if (ret)
  1042. return ret;
  1043. ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
  1044. if (ret)
  1045. goto out;
  1046. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_mem);
  1047. if (ret)
  1048. goto out;
  1049. out:
  1050. ttm_bo_mem_put(bo, &tmp_mem);
  1051. return ret;
  1052. }
  1053. static void
  1054. nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
  1055. {
  1056. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1057. struct nvkm_vma *vma;
  1058. /* ttm can now (stupidly) pass the driver bos it didn't create... */
  1059. if (bo->destroy != nouveau_bo_del_ttm)
  1060. return;
  1061. list_for_each_entry(vma, &nvbo->vma_list, head) {
  1062. if (new_mem && new_mem->mem_type != TTM_PL_SYSTEM &&
  1063. (new_mem->mem_type == TTM_PL_VRAM ||
  1064. nvbo->page_shift != vma->vm->mmu->lpg_shift)) {
  1065. nvkm_vm_map(vma, new_mem->mm_node);
  1066. } else {
  1067. nvkm_vm_unmap(vma);
  1068. }
  1069. }
  1070. }
  1071. static int
  1072. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  1073. struct nouveau_drm_tile **new_tile)
  1074. {
  1075. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1076. struct drm_device *dev = drm->dev;
  1077. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1078. u64 offset = new_mem->start << PAGE_SHIFT;
  1079. *new_tile = NULL;
  1080. if (new_mem->mem_type != TTM_PL_VRAM)
  1081. return 0;
  1082. if (drm->device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
  1083. *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
  1084. nvbo->tile_mode,
  1085. nvbo->tile_flags);
  1086. }
  1087. return 0;
  1088. }
  1089. static void
  1090. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  1091. struct nouveau_drm_tile *new_tile,
  1092. struct nouveau_drm_tile **old_tile)
  1093. {
  1094. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1095. struct drm_device *dev = drm->dev;
  1096. struct fence *fence = reservation_object_get_excl(bo->resv);
  1097. nv10_bo_put_tile_region(dev, *old_tile, fence);
  1098. *old_tile = new_tile;
  1099. }
  1100. static int
  1101. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  1102. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  1103. {
  1104. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1105. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1106. struct ttm_mem_reg *old_mem = &bo->mem;
  1107. struct nouveau_drm_tile *new_tile = NULL;
  1108. int ret = 0;
  1109. if (nvbo->pin_refcnt)
  1110. NV_WARN(drm, "Moving pinned object %p!\n", nvbo);
  1111. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  1112. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  1113. if (ret)
  1114. return ret;
  1115. }
  1116. /* Fake bo copy. */
  1117. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  1118. BUG_ON(bo->mem.mm_node != NULL);
  1119. bo->mem = *new_mem;
  1120. new_mem->mm_node = NULL;
  1121. goto out;
  1122. }
  1123. /* Hardware assisted copy. */
  1124. if (drm->ttm.move) {
  1125. if (new_mem->mem_type == TTM_PL_SYSTEM)
  1126. ret = nouveau_bo_move_flipd(bo, evict, intr,
  1127. no_wait_gpu, new_mem);
  1128. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  1129. ret = nouveau_bo_move_flips(bo, evict, intr,
  1130. no_wait_gpu, new_mem);
  1131. else
  1132. ret = nouveau_bo_move_m2mf(bo, evict, intr,
  1133. no_wait_gpu, new_mem);
  1134. if (!ret)
  1135. goto out;
  1136. }
  1137. /* Fallback to software copy. */
  1138. ret = ttm_bo_wait(bo, true, intr, no_wait_gpu);
  1139. if (ret == 0)
  1140. ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
  1141. out:
  1142. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  1143. if (ret)
  1144. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  1145. else
  1146. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  1147. }
  1148. return ret;
  1149. }
  1150. static int
  1151. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  1152. {
  1153. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1154. return drm_vma_node_verify_access(&nvbo->gem.vma_node, filp);
  1155. }
  1156. static int
  1157. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1158. {
  1159. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1160. struct nouveau_drm *drm = nouveau_bdev(bdev);
  1161. struct nvkm_mem *node = mem->mm_node;
  1162. int ret;
  1163. mem->bus.addr = NULL;
  1164. mem->bus.offset = 0;
  1165. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  1166. mem->bus.base = 0;
  1167. mem->bus.is_iomem = false;
  1168. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  1169. return -EINVAL;
  1170. switch (mem->mem_type) {
  1171. case TTM_PL_SYSTEM:
  1172. /* System memory */
  1173. return 0;
  1174. case TTM_PL_TT:
  1175. #if __OS_HAS_AGP
  1176. if (drm->agp.stat == ENABLED) {
  1177. mem->bus.offset = mem->start << PAGE_SHIFT;
  1178. mem->bus.base = drm->agp.base;
  1179. mem->bus.is_iomem = !drm->dev->agp->cant_use_aperture;
  1180. }
  1181. #endif
  1182. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA || !node->memtype)
  1183. /* untiled */
  1184. break;
  1185. /* fallthrough, tiled memory */
  1186. case TTM_PL_VRAM:
  1187. mem->bus.offset = mem->start << PAGE_SHIFT;
  1188. mem->bus.base = nv_device_resource_start(nvxx_device(&drm->device), 1);
  1189. mem->bus.is_iomem = true;
  1190. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  1191. struct nvkm_bar *bar = nvxx_bar(&drm->device);
  1192. ret = bar->umap(bar, node, NV_MEM_ACCESS_RW,
  1193. &node->bar_vma);
  1194. if (ret)
  1195. return ret;
  1196. mem->bus.offset = node->bar_vma.offset;
  1197. }
  1198. break;
  1199. default:
  1200. return -EINVAL;
  1201. }
  1202. return 0;
  1203. }
  1204. static void
  1205. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1206. {
  1207. struct nouveau_drm *drm = nouveau_bdev(bdev);
  1208. struct nvkm_bar *bar = nvxx_bar(&drm->device);
  1209. struct nvkm_mem *node = mem->mm_node;
  1210. if (!node->bar_vma.node)
  1211. return;
  1212. bar->unmap(bar, &node->bar_vma);
  1213. }
  1214. static int
  1215. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  1216. {
  1217. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  1218. struct nouveau_bo *nvbo = nouveau_bo(bo);
  1219. struct nvif_device *device = &drm->device;
  1220. u32 mappable = nv_device_resource_len(nvxx_device(device), 1) >> PAGE_SHIFT;
  1221. int i, ret;
  1222. /* as long as the bo isn't in vram, and isn't tiled, we've got
  1223. * nothing to do here.
  1224. */
  1225. if (bo->mem.mem_type != TTM_PL_VRAM) {
  1226. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA ||
  1227. !nouveau_bo_tile_layout(nvbo))
  1228. return 0;
  1229. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  1230. nouveau_bo_placement_set(nvbo, TTM_PL_TT, 0);
  1231. ret = nouveau_bo_validate(nvbo, false, false);
  1232. if (ret)
  1233. return ret;
  1234. }
  1235. return 0;
  1236. }
  1237. /* make sure bo is in mappable vram */
  1238. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
  1239. bo->mem.start + bo->mem.num_pages < mappable)
  1240. return 0;
  1241. for (i = 0; i < nvbo->placement.num_placement; ++i) {
  1242. nvbo->placements[i].fpfn = 0;
  1243. nvbo->placements[i].lpfn = mappable;
  1244. }
  1245. for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
  1246. nvbo->busy_placements[i].fpfn = 0;
  1247. nvbo->busy_placements[i].lpfn = mappable;
  1248. }
  1249. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
  1250. return nouveau_bo_validate(nvbo, false, false);
  1251. }
  1252. static int
  1253. nouveau_ttm_tt_populate(struct ttm_tt *ttm)
  1254. {
  1255. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  1256. struct nouveau_drm *drm;
  1257. struct nvkm_device *device;
  1258. struct drm_device *dev;
  1259. struct device *pdev;
  1260. unsigned i;
  1261. int r;
  1262. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  1263. if (ttm->state != tt_unpopulated)
  1264. return 0;
  1265. if (slave && ttm->sg) {
  1266. /* make userspace faulting work */
  1267. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  1268. ttm_dma->dma_address, ttm->num_pages);
  1269. ttm->state = tt_unbound;
  1270. return 0;
  1271. }
  1272. drm = nouveau_bdev(ttm->bdev);
  1273. device = nvxx_device(&drm->device);
  1274. dev = drm->dev;
  1275. pdev = nv_device_base(device);
  1276. /*
  1277. * Objects matching this condition have been marked as force_coherent,
  1278. * so use the DMA API for them.
  1279. */
  1280. if (!nv_device_is_cpu_coherent(device) &&
  1281. ttm->caching_state == tt_uncached)
  1282. return ttm_dma_populate(ttm_dma, dev->dev);
  1283. #if __OS_HAS_AGP
  1284. if (drm->agp.stat == ENABLED) {
  1285. return ttm_agp_tt_populate(ttm);
  1286. }
  1287. #endif
  1288. #ifdef CONFIG_SWIOTLB
  1289. if (swiotlb_nr_tbl()) {
  1290. return ttm_dma_populate((void *)ttm, dev->dev);
  1291. }
  1292. #endif
  1293. r = ttm_pool_populate(ttm);
  1294. if (r) {
  1295. return r;
  1296. }
  1297. for (i = 0; i < ttm->num_pages; i++) {
  1298. dma_addr_t addr;
  1299. addr = dma_map_page(pdev, ttm->pages[i], 0, PAGE_SIZE,
  1300. DMA_BIDIRECTIONAL);
  1301. if (dma_mapping_error(pdev, addr)) {
  1302. while (--i) {
  1303. dma_unmap_page(pdev, ttm_dma->dma_address[i],
  1304. PAGE_SIZE, DMA_BIDIRECTIONAL);
  1305. ttm_dma->dma_address[i] = 0;
  1306. }
  1307. ttm_pool_unpopulate(ttm);
  1308. return -EFAULT;
  1309. }
  1310. ttm_dma->dma_address[i] = addr;
  1311. }
  1312. return 0;
  1313. }
  1314. static void
  1315. nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
  1316. {
  1317. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  1318. struct nouveau_drm *drm;
  1319. struct nvkm_device *device;
  1320. struct drm_device *dev;
  1321. struct device *pdev;
  1322. unsigned i;
  1323. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  1324. if (slave)
  1325. return;
  1326. drm = nouveau_bdev(ttm->bdev);
  1327. device = nvxx_device(&drm->device);
  1328. dev = drm->dev;
  1329. pdev = nv_device_base(device);
  1330. /*
  1331. * Objects matching this condition have been marked as force_coherent,
  1332. * so use the DMA API for them.
  1333. */
  1334. if (!nv_device_is_cpu_coherent(device) &&
  1335. ttm->caching_state == tt_uncached) {
  1336. ttm_dma_unpopulate(ttm_dma, dev->dev);
  1337. return;
  1338. }
  1339. #if __OS_HAS_AGP
  1340. if (drm->agp.stat == ENABLED) {
  1341. ttm_agp_tt_unpopulate(ttm);
  1342. return;
  1343. }
  1344. #endif
  1345. #ifdef CONFIG_SWIOTLB
  1346. if (swiotlb_nr_tbl()) {
  1347. ttm_dma_unpopulate((void *)ttm, dev->dev);
  1348. return;
  1349. }
  1350. #endif
  1351. for (i = 0; i < ttm->num_pages; i++) {
  1352. if (ttm_dma->dma_address[i]) {
  1353. dma_unmap_page(pdev, ttm_dma->dma_address[i], PAGE_SIZE,
  1354. DMA_BIDIRECTIONAL);
  1355. }
  1356. }
  1357. ttm_pool_unpopulate(ttm);
  1358. }
  1359. void
  1360. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
  1361. {
  1362. struct reservation_object *resv = nvbo->bo.resv;
  1363. if (exclusive)
  1364. reservation_object_add_excl_fence(resv, &fence->base);
  1365. else if (fence)
  1366. reservation_object_add_shared_fence(resv, &fence->base);
  1367. }
  1368. struct ttm_bo_driver nouveau_bo_driver = {
  1369. .ttm_tt_create = &nouveau_ttm_tt_create,
  1370. .ttm_tt_populate = &nouveau_ttm_tt_populate,
  1371. .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
  1372. .invalidate_caches = nouveau_bo_invalidate_caches,
  1373. .init_mem_type = nouveau_bo_init_mem_type,
  1374. .evict_flags = nouveau_bo_evict_flags,
  1375. .move_notify = nouveau_bo_move_ntfy,
  1376. .move = nouveau_bo_move,
  1377. .verify_access = nouveau_bo_verify_access,
  1378. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  1379. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  1380. .io_mem_free = &nouveau_ttm_io_mem_free,
  1381. };
  1382. struct nvkm_vma *
  1383. nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nvkm_vm *vm)
  1384. {
  1385. struct nvkm_vma *vma;
  1386. list_for_each_entry(vma, &nvbo->vma_list, head) {
  1387. if (vma->vm == vm)
  1388. return vma;
  1389. }
  1390. return NULL;
  1391. }
  1392. int
  1393. nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nvkm_vm *vm,
  1394. struct nvkm_vma *vma)
  1395. {
  1396. const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  1397. int ret;
  1398. ret = nvkm_vm_get(vm, size, nvbo->page_shift,
  1399. NV_MEM_ACCESS_RW, vma);
  1400. if (ret)
  1401. return ret;
  1402. if ( nvbo->bo.mem.mem_type != TTM_PL_SYSTEM &&
  1403. (nvbo->bo.mem.mem_type == TTM_PL_VRAM ||
  1404. nvbo->page_shift != vma->vm->mmu->lpg_shift))
  1405. nvkm_vm_map(vma, nvbo->bo.mem.mm_node);
  1406. list_add_tail(&vma->head, &nvbo->vma_list);
  1407. vma->refcount = 1;
  1408. return 0;
  1409. }
  1410. void
  1411. nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nvkm_vma *vma)
  1412. {
  1413. if (vma->node) {
  1414. if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM)
  1415. nvkm_vm_unmap(vma);
  1416. nvkm_vm_put(vma);
  1417. list_del(&vma->head);
  1418. }
  1419. }