nouveau_bo.c 28 KB

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