nouveau_bo.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. nve0_bo_move_copy(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. int ret = RING_SPACE(chan, 10);
  431. if (ret == 0) {
  432. BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
  433. OUT_RING (chan, upper_32_bits(node->vma[0].offset));
  434. OUT_RING (chan, lower_32_bits(node->vma[0].offset));
  435. OUT_RING (chan, upper_32_bits(node->vma[1].offset));
  436. OUT_RING (chan, lower_32_bits(node->vma[1].offset));
  437. OUT_RING (chan, PAGE_SIZE);
  438. OUT_RING (chan, PAGE_SIZE);
  439. OUT_RING (chan, PAGE_SIZE);
  440. OUT_RING (chan, new_mem->num_pages);
  441. BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
  442. }
  443. return ret;
  444. }
  445. static int
  446. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  447. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  448. {
  449. struct nouveau_mem *node = old_mem->mm_node;
  450. u64 src_offset = node->vma[0].offset;
  451. u64 dst_offset = node->vma[1].offset;
  452. u32 page_count = new_mem->num_pages;
  453. int ret;
  454. page_count = new_mem->num_pages;
  455. while (page_count) {
  456. int line_count = (page_count > 2047) ? 2047 : page_count;
  457. ret = RING_SPACE(chan, 12);
  458. if (ret)
  459. return ret;
  460. BEGIN_NVC0(chan, NvSubM2MF, 0x0238, 2);
  461. OUT_RING (chan, upper_32_bits(dst_offset));
  462. OUT_RING (chan, lower_32_bits(dst_offset));
  463. BEGIN_NVC0(chan, NvSubM2MF, 0x030c, 6);
  464. OUT_RING (chan, upper_32_bits(src_offset));
  465. OUT_RING (chan, lower_32_bits(src_offset));
  466. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  467. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  468. OUT_RING (chan, PAGE_SIZE); /* line_length */
  469. OUT_RING (chan, line_count);
  470. BEGIN_NVC0(chan, NvSubM2MF, 0x0300, 1);
  471. OUT_RING (chan, 0x00100110);
  472. page_count -= line_count;
  473. src_offset += (PAGE_SIZE * line_count);
  474. dst_offset += (PAGE_SIZE * line_count);
  475. }
  476. return 0;
  477. }
  478. static int
  479. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  480. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  481. {
  482. struct nouveau_mem *node = old_mem->mm_node;
  483. struct nouveau_bo *nvbo = nouveau_bo(bo);
  484. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  485. u64 src_offset = node->vma[0].offset;
  486. u64 dst_offset = node->vma[1].offset;
  487. int ret;
  488. while (length) {
  489. u32 amount, stride, height;
  490. amount = min(length, (u64)(4 * 1024 * 1024));
  491. stride = 16 * 4;
  492. height = amount / stride;
  493. if (new_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_NV04(chan, NvSubM2MF, 0x0200, 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_NV04(chan, NvSubM2MF, 0x0200, 1);
  511. OUT_RING (chan, 1);
  512. }
  513. if (old_mem->mem_type == TTM_PL_VRAM &&
  514. nouveau_bo_tile_layout(nvbo)) {
  515. ret = RING_SPACE(chan, 8);
  516. if (ret)
  517. return ret;
  518. BEGIN_NV04(chan, NvSubM2MF, 0x021c, 7);
  519. OUT_RING (chan, 0);
  520. OUT_RING (chan, 0);
  521. OUT_RING (chan, stride);
  522. OUT_RING (chan, height);
  523. OUT_RING (chan, 1);
  524. OUT_RING (chan, 0);
  525. OUT_RING (chan, 0);
  526. } else {
  527. ret = RING_SPACE(chan, 2);
  528. if (ret)
  529. return ret;
  530. BEGIN_NV04(chan, NvSubM2MF, 0x021c, 1);
  531. OUT_RING (chan, 1);
  532. }
  533. ret = RING_SPACE(chan, 14);
  534. if (ret)
  535. return ret;
  536. BEGIN_NV04(chan, NvSubM2MF, 0x0238, 2);
  537. OUT_RING (chan, upper_32_bits(src_offset));
  538. OUT_RING (chan, upper_32_bits(dst_offset));
  539. BEGIN_NV04(chan, NvSubM2MF, 0x030c, 8);
  540. OUT_RING (chan, lower_32_bits(src_offset));
  541. OUT_RING (chan, lower_32_bits(dst_offset));
  542. OUT_RING (chan, stride);
  543. OUT_RING (chan, stride);
  544. OUT_RING (chan, stride);
  545. OUT_RING (chan, height);
  546. OUT_RING (chan, 0x00000101);
  547. OUT_RING (chan, 0x00000000);
  548. BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  549. OUT_RING (chan, 0);
  550. length -= amount;
  551. src_offset += amount;
  552. dst_offset += amount;
  553. }
  554. return 0;
  555. }
  556. static inline uint32_t
  557. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  558. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  559. {
  560. if (mem->mem_type == TTM_PL_TT)
  561. return chan->gart_handle;
  562. return chan->vram_handle;
  563. }
  564. static int
  565. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  566. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  567. {
  568. u32 src_offset = old_mem->start << PAGE_SHIFT;
  569. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  570. u32 page_count = new_mem->num_pages;
  571. int ret;
  572. ret = RING_SPACE(chan, 3);
  573. if (ret)
  574. return ret;
  575. BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  576. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  577. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  578. page_count = new_mem->num_pages;
  579. while (page_count) {
  580. int line_count = (page_count > 2047) ? 2047 : page_count;
  581. ret = RING_SPACE(chan, 11);
  582. if (ret)
  583. return ret;
  584. BEGIN_NV04(chan, NvSubM2MF,
  585. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  586. OUT_RING (chan, src_offset);
  587. OUT_RING (chan, dst_offset);
  588. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  589. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  590. OUT_RING (chan, PAGE_SIZE); /* line_length */
  591. OUT_RING (chan, line_count);
  592. OUT_RING (chan, 0x00000101);
  593. OUT_RING (chan, 0x00000000);
  594. BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  595. OUT_RING (chan, 0);
  596. page_count -= line_count;
  597. src_offset += (PAGE_SIZE * line_count);
  598. dst_offset += (PAGE_SIZE * line_count);
  599. }
  600. return 0;
  601. }
  602. static int
  603. nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
  604. struct ttm_mem_reg *mem, struct nouveau_vma *vma)
  605. {
  606. struct nouveau_mem *node = mem->mm_node;
  607. int ret;
  608. ret = nouveau_vm_get(chan->vm, mem->num_pages << PAGE_SHIFT,
  609. node->page_shift, NV_MEM_ACCESS_RO, vma);
  610. if (ret)
  611. return ret;
  612. if (mem->mem_type == TTM_PL_VRAM)
  613. nouveau_vm_map(vma, node);
  614. else
  615. nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT, node);
  616. return 0;
  617. }
  618. static int
  619. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  620. bool no_wait_reserve, bool no_wait_gpu,
  621. struct ttm_mem_reg *new_mem)
  622. {
  623. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  624. struct nouveau_channel *chan = chan = dev_priv->channel;
  625. struct nouveau_bo *nvbo = nouveau_bo(bo);
  626. struct ttm_mem_reg *old_mem = &bo->mem;
  627. int ret;
  628. mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
  629. /* create temporary vmas for the transfer and attach them to the
  630. * old nouveau_mem node, these will get cleaned up after ttm has
  631. * destroyed the ttm_mem_reg
  632. */
  633. if (dev_priv->card_type >= NV_50) {
  634. struct nouveau_mem *node = old_mem->mm_node;
  635. ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
  636. if (ret)
  637. goto out;
  638. ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
  639. if (ret)
  640. goto out;
  641. }
  642. if (dev_priv->card_type < NV_50)
  643. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  644. else
  645. if (dev_priv->card_type < NV_C0)
  646. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  647. else
  648. if (dev_priv->card_type < NV_E0)
  649. ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  650. else
  651. ret = nve0_bo_move_copy(chan, bo, &bo->mem, new_mem);
  652. if (ret == 0) {
  653. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  654. no_wait_reserve,
  655. no_wait_gpu, new_mem);
  656. }
  657. out:
  658. mutex_unlock(&chan->mutex);
  659. return ret;
  660. }
  661. static int
  662. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  663. bool no_wait_reserve, bool no_wait_gpu,
  664. struct ttm_mem_reg *new_mem)
  665. {
  666. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  667. struct ttm_placement placement;
  668. struct ttm_mem_reg tmp_mem;
  669. int ret;
  670. placement.fpfn = placement.lpfn = 0;
  671. placement.num_placement = placement.num_busy_placement = 1;
  672. placement.placement = placement.busy_placement = &placement_memtype;
  673. tmp_mem = *new_mem;
  674. tmp_mem.mm_node = NULL;
  675. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  676. if (ret)
  677. return ret;
  678. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  679. if (ret)
  680. goto out;
  681. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  682. if (ret)
  683. goto out;
  684. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
  685. out:
  686. ttm_bo_mem_put(bo, &tmp_mem);
  687. return ret;
  688. }
  689. static int
  690. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  691. bool no_wait_reserve, bool no_wait_gpu,
  692. struct ttm_mem_reg *new_mem)
  693. {
  694. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  695. struct ttm_placement placement;
  696. struct ttm_mem_reg tmp_mem;
  697. int ret;
  698. placement.fpfn = placement.lpfn = 0;
  699. placement.num_placement = placement.num_busy_placement = 1;
  700. placement.placement = placement.busy_placement = &placement_memtype;
  701. tmp_mem = *new_mem;
  702. tmp_mem.mm_node = NULL;
  703. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  704. if (ret)
  705. return ret;
  706. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
  707. if (ret)
  708. goto out;
  709. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
  710. if (ret)
  711. goto out;
  712. out:
  713. ttm_bo_mem_put(bo, &tmp_mem);
  714. return ret;
  715. }
  716. static void
  717. nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
  718. {
  719. struct nouveau_bo *nvbo = nouveau_bo(bo);
  720. struct nouveau_vma *vma;
  721. /* ttm can now (stupidly) pass the driver bos it didn't create... */
  722. if (bo->destroy != nouveau_bo_del_ttm)
  723. return;
  724. list_for_each_entry(vma, &nvbo->vma_list, head) {
  725. if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
  726. nouveau_vm_map(vma, new_mem->mm_node);
  727. } else
  728. if (new_mem && new_mem->mem_type == TTM_PL_TT &&
  729. nvbo->page_shift == vma->vm->spg_shift) {
  730. if (((struct nouveau_mem *)new_mem->mm_node)->sg)
  731. nouveau_vm_map_sg_table(vma, 0, new_mem->
  732. num_pages << PAGE_SHIFT,
  733. new_mem->mm_node);
  734. else
  735. nouveau_vm_map_sg(vma, 0, new_mem->
  736. num_pages << PAGE_SHIFT,
  737. new_mem->mm_node);
  738. } else {
  739. nouveau_vm_unmap(vma);
  740. }
  741. }
  742. }
  743. static int
  744. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  745. struct nouveau_tile_reg **new_tile)
  746. {
  747. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  748. struct drm_device *dev = dev_priv->dev;
  749. struct nouveau_bo *nvbo = nouveau_bo(bo);
  750. u64 offset = new_mem->start << PAGE_SHIFT;
  751. *new_tile = NULL;
  752. if (new_mem->mem_type != TTM_PL_VRAM)
  753. return 0;
  754. if (dev_priv->card_type >= NV_10) {
  755. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  756. nvbo->tile_mode,
  757. nvbo->tile_flags);
  758. }
  759. return 0;
  760. }
  761. static void
  762. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  763. struct nouveau_tile_reg *new_tile,
  764. struct nouveau_tile_reg **old_tile)
  765. {
  766. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  767. struct drm_device *dev = dev_priv->dev;
  768. nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
  769. *old_tile = new_tile;
  770. }
  771. static int
  772. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  773. bool no_wait_reserve, bool no_wait_gpu,
  774. struct ttm_mem_reg *new_mem)
  775. {
  776. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  777. struct nouveau_bo *nvbo = nouveau_bo(bo);
  778. struct ttm_mem_reg *old_mem = &bo->mem;
  779. struct nouveau_tile_reg *new_tile = NULL;
  780. int ret = 0;
  781. if (dev_priv->card_type < NV_50) {
  782. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  783. if (ret)
  784. return ret;
  785. }
  786. /* Fake bo copy. */
  787. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  788. BUG_ON(bo->mem.mm_node != NULL);
  789. bo->mem = *new_mem;
  790. new_mem->mm_node = NULL;
  791. goto out;
  792. }
  793. /* Software copy if the card isn't up and running yet. */
  794. if (!dev_priv->channel) {
  795. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  796. goto out;
  797. }
  798. /* Hardware assisted copy. */
  799. if (new_mem->mem_type == TTM_PL_SYSTEM)
  800. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  801. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  802. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  803. else
  804. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  805. if (!ret)
  806. goto out;
  807. /* Fallback to software copy. */
  808. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  809. out:
  810. if (dev_priv->card_type < NV_50) {
  811. if (ret)
  812. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  813. else
  814. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  815. }
  816. return ret;
  817. }
  818. static int
  819. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  820. {
  821. return 0;
  822. }
  823. static int
  824. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  825. {
  826. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  827. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  828. struct drm_device *dev = dev_priv->dev;
  829. int ret;
  830. mem->bus.addr = NULL;
  831. mem->bus.offset = 0;
  832. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  833. mem->bus.base = 0;
  834. mem->bus.is_iomem = false;
  835. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  836. return -EINVAL;
  837. switch (mem->mem_type) {
  838. case TTM_PL_SYSTEM:
  839. /* System memory */
  840. return 0;
  841. case TTM_PL_TT:
  842. #if __OS_HAS_AGP
  843. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  844. mem->bus.offset = mem->start << PAGE_SHIFT;
  845. mem->bus.base = dev_priv->gart_info.aper_base;
  846. mem->bus.is_iomem = true;
  847. }
  848. #endif
  849. break;
  850. case TTM_PL_VRAM:
  851. {
  852. struct nouveau_mem *node = mem->mm_node;
  853. u8 page_shift;
  854. if (!dev_priv->bar1_vm) {
  855. mem->bus.offset = mem->start << PAGE_SHIFT;
  856. mem->bus.base = pci_resource_start(dev->pdev, 1);
  857. mem->bus.is_iomem = true;
  858. break;
  859. }
  860. if (dev_priv->card_type >= NV_C0)
  861. page_shift = node->page_shift;
  862. else
  863. page_shift = 12;
  864. ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,
  865. page_shift, NV_MEM_ACCESS_RW,
  866. &node->bar_vma);
  867. if (ret)
  868. return ret;
  869. nouveau_vm_map(&node->bar_vma, node);
  870. if (ret) {
  871. nouveau_vm_put(&node->bar_vma);
  872. return ret;
  873. }
  874. mem->bus.offset = node->bar_vma.offset;
  875. if (dev_priv->card_type == NV_50) /*XXX*/
  876. mem->bus.offset -= 0x0020000000ULL;
  877. mem->bus.base = pci_resource_start(dev->pdev, 1);
  878. mem->bus.is_iomem = true;
  879. }
  880. break;
  881. default:
  882. return -EINVAL;
  883. }
  884. return 0;
  885. }
  886. static void
  887. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  888. {
  889. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  890. struct nouveau_mem *node = mem->mm_node;
  891. if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
  892. return;
  893. if (!node->bar_vma.node)
  894. return;
  895. nouveau_vm_unmap(&node->bar_vma);
  896. nouveau_vm_put(&node->bar_vma);
  897. }
  898. static int
  899. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  900. {
  901. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  902. struct nouveau_bo *nvbo = nouveau_bo(bo);
  903. /* as long as the bo isn't in vram, and isn't tiled, we've got
  904. * nothing to do here.
  905. */
  906. if (bo->mem.mem_type != TTM_PL_VRAM) {
  907. if (dev_priv->card_type < NV_50 ||
  908. !nouveau_bo_tile_layout(nvbo))
  909. return 0;
  910. }
  911. /* make sure bo is in mappable vram */
  912. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  913. return 0;
  914. nvbo->placement.fpfn = 0;
  915. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  916. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
  917. return nouveau_bo_validate(nvbo, false, true, false);
  918. }
  919. static int
  920. nouveau_ttm_tt_populate(struct ttm_tt *ttm)
  921. {
  922. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  923. struct drm_nouveau_private *dev_priv;
  924. struct drm_device *dev;
  925. unsigned i;
  926. int r;
  927. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  928. if (ttm->state != tt_unpopulated)
  929. return 0;
  930. if (slave && ttm->sg) {
  931. /* make userspace faulting work */
  932. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  933. ttm_dma->dma_address, ttm->num_pages);
  934. ttm->state = tt_unbound;
  935. return 0;
  936. }
  937. dev_priv = nouveau_bdev(ttm->bdev);
  938. dev = dev_priv->dev;
  939. #if __OS_HAS_AGP
  940. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  941. return ttm_agp_tt_populate(ttm);
  942. }
  943. #endif
  944. #ifdef CONFIG_SWIOTLB
  945. if (swiotlb_nr_tbl()) {
  946. return ttm_dma_populate((void *)ttm, dev->dev);
  947. }
  948. #endif
  949. r = ttm_pool_populate(ttm);
  950. if (r) {
  951. return r;
  952. }
  953. for (i = 0; i < ttm->num_pages; i++) {
  954. ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
  955. 0, PAGE_SIZE,
  956. PCI_DMA_BIDIRECTIONAL);
  957. if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
  958. while (--i) {
  959. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  960. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  961. ttm_dma->dma_address[i] = 0;
  962. }
  963. ttm_pool_unpopulate(ttm);
  964. return -EFAULT;
  965. }
  966. }
  967. return 0;
  968. }
  969. static void
  970. nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
  971. {
  972. struct ttm_dma_tt *ttm_dma = (void *)ttm;
  973. struct drm_nouveau_private *dev_priv;
  974. struct drm_device *dev;
  975. unsigned i;
  976. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  977. if (slave)
  978. return;
  979. dev_priv = nouveau_bdev(ttm->bdev);
  980. dev = dev_priv->dev;
  981. #if __OS_HAS_AGP
  982. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  983. ttm_agp_tt_unpopulate(ttm);
  984. return;
  985. }
  986. #endif
  987. #ifdef CONFIG_SWIOTLB
  988. if (swiotlb_nr_tbl()) {
  989. ttm_dma_unpopulate((void *)ttm, dev->dev);
  990. return;
  991. }
  992. #endif
  993. for (i = 0; i < ttm->num_pages; i++) {
  994. if (ttm_dma->dma_address[i]) {
  995. pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
  996. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  997. }
  998. }
  999. ttm_pool_unpopulate(ttm);
  1000. }
  1001. void
  1002. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  1003. {
  1004. struct nouveau_fence *old_fence = NULL;
  1005. if (likely(fence))
  1006. nouveau_fence_ref(fence);
  1007. spin_lock(&nvbo->bo.bdev->fence_lock);
  1008. old_fence = nvbo->bo.sync_obj;
  1009. nvbo->bo.sync_obj = fence;
  1010. spin_unlock(&nvbo->bo.bdev->fence_lock);
  1011. nouveau_fence_unref(&old_fence);
  1012. }
  1013. static void
  1014. nouveau_bo_fence_unref(void **sync_obj)
  1015. {
  1016. nouveau_fence_unref((struct nouveau_fence **)sync_obj);
  1017. }
  1018. static void *
  1019. nouveau_bo_fence_ref(void *sync_obj)
  1020. {
  1021. return nouveau_fence_ref(sync_obj);
  1022. }
  1023. static bool
  1024. nouveau_bo_fence_signalled(void *sync_obj, void *sync_arg)
  1025. {
  1026. return nouveau_fence_signalled(sync_obj);
  1027. }
  1028. static int
  1029. nouveau_bo_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  1030. {
  1031. return nouveau_fence_wait(sync_obj, lazy, intr);
  1032. }
  1033. static int
  1034. nouveau_bo_fence_flush(void *sync_obj, void *sync_arg)
  1035. {
  1036. return 0;
  1037. }
  1038. struct ttm_bo_driver nouveau_bo_driver = {
  1039. .ttm_tt_create = &nouveau_ttm_tt_create,
  1040. .ttm_tt_populate = &nouveau_ttm_tt_populate,
  1041. .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
  1042. .invalidate_caches = nouveau_bo_invalidate_caches,
  1043. .init_mem_type = nouveau_bo_init_mem_type,
  1044. .evict_flags = nouveau_bo_evict_flags,
  1045. .move_notify = nouveau_bo_move_ntfy,
  1046. .move = nouveau_bo_move,
  1047. .verify_access = nouveau_bo_verify_access,
  1048. .sync_obj_signaled = nouveau_bo_fence_signalled,
  1049. .sync_obj_wait = nouveau_bo_fence_wait,
  1050. .sync_obj_flush = nouveau_bo_fence_flush,
  1051. .sync_obj_unref = nouveau_bo_fence_unref,
  1052. .sync_obj_ref = nouveau_bo_fence_ref,
  1053. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  1054. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  1055. .io_mem_free = &nouveau_ttm_io_mem_free,
  1056. };
  1057. struct nouveau_vma *
  1058. nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
  1059. {
  1060. struct nouveau_vma *vma;
  1061. list_for_each_entry(vma, &nvbo->vma_list, head) {
  1062. if (vma->vm == vm)
  1063. return vma;
  1064. }
  1065. return NULL;
  1066. }
  1067. int
  1068. nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
  1069. struct nouveau_vma *vma)
  1070. {
  1071. const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
  1072. struct nouveau_mem *node = nvbo->bo.mem.mm_node;
  1073. int ret;
  1074. ret = nouveau_vm_get(vm, size, nvbo->page_shift,
  1075. NV_MEM_ACCESS_RW, vma);
  1076. if (ret)
  1077. return ret;
  1078. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  1079. nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
  1080. else if (nvbo->bo.mem.mem_type == TTM_PL_TT) {
  1081. if (node->sg)
  1082. nouveau_vm_map_sg_table(vma, 0, size, node);
  1083. else
  1084. nouveau_vm_map_sg(vma, 0, size, node);
  1085. }
  1086. list_add_tail(&vma->head, &nvbo->vma_list);
  1087. vma->refcount = 1;
  1088. return 0;
  1089. }
  1090. void
  1091. nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
  1092. {
  1093. if (vma->node) {
  1094. if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
  1095. spin_lock(&nvbo->bo.bdev->fence_lock);
  1096. ttm_bo_wait(&nvbo->bo, false, false, false);
  1097. spin_unlock(&nvbo->bo.bdev->fence_lock);
  1098. nouveau_vm_unmap(vma);
  1099. }
  1100. nouveau_vm_put(vma);
  1101. list_del(&vma->head);
  1102. }
  1103. }