nouveau_bo.c 39 KB

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