nouveau_bo.c 42 KB

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