nouveau_bo.c 28 KB

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