nouveau_bo.c 28 KB

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