nouveau_bo.c 27 KB

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