nouveau_bo.c 27 KB

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