nouveau_bo.c 28 KB

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