nouveau_bo.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. list_for_each_entry(vma, &nvbo->vma_list, head) {
  700. if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
  701. nouveau_vm_map(vma, new_mem->mm_node);
  702. } else
  703. if (new_mem && new_mem->mem_type == TTM_PL_TT &&
  704. nvbo->page_shift == vma->vm->spg_shift) {
  705. nouveau_vm_map_sg(vma, 0, new_mem->
  706. num_pages << PAGE_SHIFT,
  707. new_mem->mm_node);
  708. } else {
  709. nouveau_vm_unmap(vma);
  710. }
  711. }
  712. }
  713. static int
  714. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  715. struct nouveau_tile_reg **new_tile)
  716. {
  717. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  718. struct drm_device *dev = dev_priv->dev;
  719. struct nouveau_bo *nvbo = nouveau_bo(bo);
  720. u64 offset = new_mem->start << PAGE_SHIFT;
  721. *new_tile = NULL;
  722. if (new_mem->mem_type != TTM_PL_VRAM)
  723. return 0;
  724. if (dev_priv->card_type >= NV_10) {
  725. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  726. nvbo->tile_mode,
  727. nvbo->tile_flags);
  728. }
  729. return 0;
  730. }
  731. static void
  732. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  733. struct nouveau_tile_reg *new_tile,
  734. struct nouveau_tile_reg **old_tile)
  735. {
  736. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  737. struct drm_device *dev = dev_priv->dev;
  738. nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
  739. *old_tile = new_tile;
  740. }
  741. static int
  742. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  743. bool no_wait_reserve, bool no_wait_gpu,
  744. struct ttm_mem_reg *new_mem)
  745. {
  746. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  747. struct nouveau_bo *nvbo = nouveau_bo(bo);
  748. struct ttm_mem_reg *old_mem = &bo->mem;
  749. struct nouveau_tile_reg *new_tile = NULL;
  750. int ret = 0;
  751. if (dev_priv->card_type < NV_50) {
  752. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  753. if (ret)
  754. return ret;
  755. }
  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 (dev_priv->card_type < NV_50) {
  781. if (ret)
  782. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  783. else
  784. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  785. }
  786. return ret;
  787. }
  788. static int
  789. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  790. {
  791. return 0;
  792. }
  793. static int
  794. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  795. {
  796. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  797. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  798. struct drm_device *dev = dev_priv->dev;
  799. int ret;
  800. mem->bus.addr = NULL;
  801. mem->bus.offset = 0;
  802. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  803. mem->bus.base = 0;
  804. mem->bus.is_iomem = false;
  805. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  806. return -EINVAL;
  807. switch (mem->mem_type) {
  808. case TTM_PL_SYSTEM:
  809. /* System memory */
  810. return 0;
  811. case TTM_PL_TT:
  812. #if __OS_HAS_AGP
  813. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  814. mem->bus.offset = mem->start << PAGE_SHIFT;
  815. mem->bus.base = dev_priv->gart_info.aper_base;
  816. mem->bus.is_iomem = true;
  817. }
  818. #endif
  819. break;
  820. case TTM_PL_VRAM:
  821. {
  822. struct nouveau_mem *node = mem->mm_node;
  823. u8 page_shift;
  824. if (!dev_priv->bar1_vm) {
  825. mem->bus.offset = mem->start << PAGE_SHIFT;
  826. mem->bus.base = pci_resource_start(dev->pdev, 1);
  827. mem->bus.is_iomem = true;
  828. break;
  829. }
  830. if (dev_priv->card_type >= NV_C0)
  831. page_shift = node->page_shift;
  832. else
  833. page_shift = 12;
  834. ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,
  835. page_shift, NV_MEM_ACCESS_RW,
  836. &node->bar_vma);
  837. if (ret)
  838. return ret;
  839. nouveau_vm_map(&node->bar_vma, node);
  840. if (ret) {
  841. nouveau_vm_put(&node->bar_vma);
  842. return ret;
  843. }
  844. mem->bus.offset = node->bar_vma.offset;
  845. if (dev_priv->card_type == NV_50) /*XXX*/
  846. mem->bus.offset -= 0x0020000000ULL;
  847. mem->bus.base = pci_resource_start(dev->pdev, 1);
  848. mem->bus.is_iomem = true;
  849. }
  850. break;
  851. default:
  852. return -EINVAL;
  853. }
  854. return 0;
  855. }
  856. static void
  857. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  858. {
  859. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  860. struct nouveau_mem *node = mem->mm_node;
  861. if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
  862. return;
  863. if (!node->bar_vma.node)
  864. return;
  865. nouveau_vm_unmap(&node->bar_vma);
  866. nouveau_vm_put(&node->bar_vma);
  867. }
  868. static int
  869. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  870. {
  871. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  872. struct nouveau_bo *nvbo = nouveau_bo(bo);
  873. /* as long as the bo isn't in vram, and isn't tiled, we've got
  874. * nothing to do here.
  875. */
  876. if (bo->mem.mem_type != TTM_PL_VRAM) {
  877. if (dev_priv->card_type < NV_50 ||
  878. !nouveau_bo_tile_layout(nvbo))
  879. return 0;
  880. }
  881. /* make sure bo is in mappable vram */
  882. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  883. return 0;
  884. nvbo->placement.fpfn = 0;
  885. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  886. nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
  887. return nouveau_bo_validate(nvbo, false, true, false);
  888. }
  889. void
  890. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  891. {
  892. struct nouveau_fence *old_fence;
  893. if (likely(fence))
  894. nouveau_fence_ref(fence);
  895. spin_lock(&nvbo->bo.bdev->fence_lock);
  896. old_fence = nvbo->bo.sync_obj;
  897. nvbo->bo.sync_obj = fence;
  898. spin_unlock(&nvbo->bo.bdev->fence_lock);
  899. nouveau_fence_unref(&old_fence);
  900. }
  901. static int
  902. nouveau_ttm_tt_populate(struct ttm_tt *ttm)
  903. {
  904. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  905. struct drm_nouveau_private *dev_priv;
  906. struct drm_device *dev;
  907. unsigned i;
  908. int r;
  909. if (ttm->state != tt_unpopulated)
  910. return 0;
  911. dev_priv = nouveau_bdev(ttm->bdev);
  912. dev = dev_priv->dev;
  913. #if __OS_HAS_AGP
  914. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  915. return ttm_agp_tt_populate(ttm);
  916. }
  917. #endif
  918. #ifdef CONFIG_SWIOTLB
  919. if (swiotlb_nr_tbl()) {
  920. return ttm_dma_populate((void *)ttm, dev->dev);
  921. }
  922. #endif
  923. r = ttm_pool_populate(ttm);
  924. if (r) {
  925. return r;
  926. }
  927. for (i = 0; i < ttm->num_pages; i++) {
  928. ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
  929. 0, PAGE_SIZE,
  930. PCI_DMA_BIDIRECTIONAL);
  931. if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
  932. while (--i) {
  933. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  934. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  935. ttm_dma->dma_address[i] = 0;
  936. }
  937. ttm_pool_unpopulate(ttm);
  938. return -EFAULT;
  939. }
  940. }
  941. return 0;
  942. }
  943. static void
  944. nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
  945. {
  946. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  947. struct drm_nouveau_private *dev_priv;
  948. struct drm_device *dev;
  949. unsigned i;
  950. dev_priv = nouveau_bdev(ttm->bdev);
  951. dev = dev_priv->dev;
  952. #if __OS_HAS_AGP
  953. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  954. ttm_agp_tt_unpopulate(ttm);
  955. return;
  956. }
  957. #endif
  958. #ifdef CONFIG_SWIOTLB
  959. if (swiotlb_nr_tbl()) {
  960. ttm_dma_unpopulate((void *)ttm, dev->dev);
  961. return;
  962. }
  963. #endif
  964. for (i = 0; i < ttm->num_pages; i++) {
  965. if (ttm_dma->dma_address[i]) {
  966. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  967. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  968. }
  969. }
  970. ttm_pool_unpopulate(ttm);
  971. }
  972. struct ttm_bo_driver nouveau_bo_driver = {
  973. .ttm_tt_create = &nouveau_ttm_tt_create,
  974. .ttm_tt_populate = &nouveau_ttm_tt_populate,
  975. .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
  976. .invalidate_caches = nouveau_bo_invalidate_caches,
  977. .init_mem_type = nouveau_bo_init_mem_type,
  978. .evict_flags = nouveau_bo_evict_flags,
  979. .move_notify = nouveau_bo_move_ntfy,
  980. .move = nouveau_bo_move,
  981. .verify_access = nouveau_bo_verify_access,
  982. .sync_obj_signaled = __nouveau_fence_signalled,
  983. .sync_obj_wait = __nouveau_fence_wait,
  984. .sync_obj_flush = __nouveau_fence_flush,
  985. .sync_obj_unref = __nouveau_fence_unref,
  986. .sync_obj_ref = __nouveau_fence_ref,
  987. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  988. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  989. .io_mem_free = &nouveau_ttm_io_mem_free,
  990. };
  991. struct nouveau_vma *
  992. nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
  993. {
  994. struct nouveau_vma *vma;
  995. list_for_each_entry(vma, &nvbo->vma_list, head) {
  996. if (vma->vm == vm)
  997. return vma;
  998. }
  999. return NULL;
  1000. }
  1001. int
  1002. nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
  1003. struct nouveau_vma *vma)
  1004. {
  1005. const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  1006. struct nouveau_mem *node = nvbo->bo.mem.mm_node;
  1007. int ret;
  1008. ret = nouveau_vm_get(vm, size, nvbo->page_shift,
  1009. NV_MEM_ACCESS_RW, vma);
  1010. if (ret)
  1011. return ret;
  1012. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  1013. nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
  1014. else
  1015. if (nvbo->bo.mem.mem_type == TTM_PL_TT)
  1016. nouveau_vm_map_sg(vma, 0, size, node);
  1017. list_add_tail(&vma->head, &nvbo->vma_list);
  1018. vma->refcount = 1;
  1019. return 0;
  1020. }
  1021. void
  1022. nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
  1023. {
  1024. if (vma->node) {
  1025. if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
  1026. spin_lock(&nvbo->bo.bdev->fence_lock);
  1027. ttm_bo_wait(&nvbo->bo, false, false, false);
  1028. spin_unlock(&nvbo->bo.bdev->fence_lock);
  1029. nouveau_vm_unmap(vma);
  1030. }
  1031. nouveau_vm_put(vma);
  1032. list_del(&vma->head);
  1033. }
  1034. }