nouveau_bo.c 38 KB

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