nouveau_bo.c 30 KB

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