nouveau_bo.c 32 KB

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