nouveau_bo.c 31 KB

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