nouveau_bo.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. if (nvbo->vma.node) {
  47. nouveau_vm_unmap(&nvbo->vma);
  48. nouveau_vm_put(&nvbo->vma);
  49. }
  50. kfree(nvbo);
  51. }
  52. static void
  53. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
  54. int *align, int *size)
  55. {
  56. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  57. if (dev_priv->card_type < NV_50) {
  58. if (nvbo->tile_mode) {
  59. if (dev_priv->chipset >= 0x40) {
  60. *align = 65536;
  61. *size = roundup(*size, 64 * nvbo->tile_mode);
  62. } else if (dev_priv->chipset >= 0x30) {
  63. *align = 32768;
  64. *size = roundup(*size, 64 * nvbo->tile_mode);
  65. } else if (dev_priv->chipset >= 0x20) {
  66. *align = 16384;
  67. *size = roundup(*size, 64 * nvbo->tile_mode);
  68. } else if (dev_priv->chipset >= 0x10) {
  69. *align = 16384;
  70. *size = roundup(*size, 32 * nvbo->tile_mode);
  71. }
  72. }
  73. } else {
  74. *size = roundup(*size, (1 << nvbo->page_shift));
  75. *align = max((1 << nvbo->page_shift), *align);
  76. }
  77. *size = roundup(*size, PAGE_SIZE);
  78. }
  79. int
  80. nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
  81. int size, int align, uint32_t flags, uint32_t tile_mode,
  82. uint32_t tile_flags, struct nouveau_bo **pnvbo)
  83. {
  84. struct drm_nouveau_private *dev_priv = dev->dev_private;
  85. struct nouveau_bo *nvbo;
  86. int ret;
  87. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  88. if (!nvbo)
  89. return -ENOMEM;
  90. INIT_LIST_HEAD(&nvbo->head);
  91. INIT_LIST_HEAD(&nvbo->entry);
  92. nvbo->tile_mode = tile_mode;
  93. nvbo->tile_flags = tile_flags;
  94. nvbo->bo.bdev = &dev_priv->ttm.bdev;
  95. nvbo->page_shift = 12;
  96. if (dev_priv->bar1_vm) {
  97. if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
  98. nvbo->page_shift = dev_priv->bar1_vm->lpg_shift;
  99. }
  100. nouveau_bo_fixup_align(nvbo, flags, &align, &size);
  101. align >>= PAGE_SHIFT;
  102. if (dev_priv->chan_vm) {
  103. ret = nouveau_vm_get(dev_priv->chan_vm, size, nvbo->page_shift,
  104. NV_MEM_ACCESS_RW, &nvbo->vma);
  105. if (ret) {
  106. kfree(nvbo);
  107. return ret;
  108. }
  109. }
  110. nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
  111. nouveau_bo_placement_set(nvbo, flags, 0);
  112. nvbo->channel = chan;
  113. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  114. ttm_bo_type_device, &nvbo->placement, align, 0,
  115. false, NULL, size, nouveau_bo_del_ttm);
  116. if (ret) {
  117. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  118. return ret;
  119. }
  120. nvbo->channel = NULL;
  121. *pnvbo = nvbo;
  122. return 0;
  123. }
  124. static void
  125. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  126. {
  127. *n = 0;
  128. if (type & TTM_PL_FLAG_VRAM)
  129. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  130. if (type & TTM_PL_FLAG_TT)
  131. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  132. if (type & TTM_PL_FLAG_SYSTEM)
  133. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  134. }
  135. static void
  136. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  137. {
  138. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  139. int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
  140. if (dev_priv->card_type == NV_10 &&
  141. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  142. nvbo->bo.mem.num_pages < vram_pages / 2) {
  143. /*
  144. * Make sure that the color and depth buffers are handled
  145. * by independent memory controller units. Up to a 9x
  146. * speed up when alpha-blending and depth-test are enabled
  147. * at the same time.
  148. */
  149. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  150. nvbo->placement.fpfn = vram_pages / 2;
  151. nvbo->placement.lpfn = ~0;
  152. } else {
  153. nvbo->placement.fpfn = 0;
  154. nvbo->placement.lpfn = vram_pages / 2;
  155. }
  156. }
  157. }
  158. void
  159. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  160. {
  161. struct ttm_placement *pl = &nvbo->placement;
  162. uint32_t flags = TTM_PL_MASK_CACHING |
  163. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  164. pl->placement = nvbo->placements;
  165. set_placement_list(nvbo->placements, &pl->num_placement,
  166. type, flags);
  167. pl->busy_placement = nvbo->busy_placements;
  168. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  169. type | busy, flags);
  170. set_placement_range(nvbo, type);
  171. }
  172. int
  173. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  174. {
  175. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  176. struct ttm_buffer_object *bo = &nvbo->bo;
  177. int ret;
  178. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  179. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  180. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  181. 1 << bo->mem.mem_type, memtype);
  182. return -EINVAL;
  183. }
  184. if (nvbo->pin_refcnt++)
  185. return 0;
  186. ret = ttm_bo_reserve(bo, false, false, false, 0);
  187. if (ret)
  188. goto out;
  189. nouveau_bo_placement_set(nvbo, memtype, 0);
  190. ret = nouveau_bo_validate(nvbo, false, false, false);
  191. if (ret == 0) {
  192. switch (bo->mem.mem_type) {
  193. case TTM_PL_VRAM:
  194. dev_priv->fb_aper_free -= bo->mem.size;
  195. break;
  196. case TTM_PL_TT:
  197. dev_priv->gart_info.aper_free -= bo->mem.size;
  198. break;
  199. default:
  200. break;
  201. }
  202. }
  203. ttm_bo_unreserve(bo);
  204. out:
  205. if (unlikely(ret))
  206. nvbo->pin_refcnt--;
  207. return ret;
  208. }
  209. int
  210. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  211. {
  212. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  213. struct ttm_buffer_object *bo = &nvbo->bo;
  214. int ret;
  215. if (--nvbo->pin_refcnt)
  216. return 0;
  217. ret = ttm_bo_reserve(bo, false, false, false, 0);
  218. if (ret)
  219. return ret;
  220. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  221. ret = nouveau_bo_validate(nvbo, false, false, false);
  222. if (ret == 0) {
  223. switch (bo->mem.mem_type) {
  224. case TTM_PL_VRAM:
  225. dev_priv->fb_aper_free += bo->mem.size;
  226. break;
  227. case TTM_PL_TT:
  228. dev_priv->gart_info.aper_free += bo->mem.size;
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. ttm_bo_unreserve(bo);
  235. return ret;
  236. }
  237. int
  238. nouveau_bo_map(struct nouveau_bo *nvbo)
  239. {
  240. int ret;
  241. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  242. if (ret)
  243. return ret;
  244. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  245. ttm_bo_unreserve(&nvbo->bo);
  246. return ret;
  247. }
  248. void
  249. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  250. {
  251. if (nvbo)
  252. ttm_bo_kunmap(&nvbo->kmap);
  253. }
  254. int
  255. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  256. bool no_wait_reserve, bool no_wait_gpu)
  257. {
  258. int ret;
  259. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
  260. no_wait_reserve, no_wait_gpu);
  261. if (ret)
  262. return ret;
  263. return 0;
  264. }
  265. u16
  266. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  267. {
  268. bool is_iomem;
  269. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  270. mem = &mem[index];
  271. if (is_iomem)
  272. return ioread16_native((void __force __iomem *)mem);
  273. else
  274. return *mem;
  275. }
  276. void
  277. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  278. {
  279. bool is_iomem;
  280. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  281. mem = &mem[index];
  282. if (is_iomem)
  283. iowrite16_native(val, (void __force __iomem *)mem);
  284. else
  285. *mem = val;
  286. }
  287. u32
  288. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  289. {
  290. bool is_iomem;
  291. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  292. mem = &mem[index];
  293. if (is_iomem)
  294. return ioread32_native((void __force __iomem *)mem);
  295. else
  296. return *mem;
  297. }
  298. void
  299. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  300. {
  301. bool is_iomem;
  302. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  303. mem = &mem[index];
  304. if (is_iomem)
  305. iowrite32_native(val, (void __force __iomem *)mem);
  306. else
  307. *mem = val;
  308. }
  309. static struct ttm_backend *
  310. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  311. {
  312. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  313. struct drm_device *dev = dev_priv->dev;
  314. switch (dev_priv->gart_info.type) {
  315. #if __OS_HAS_AGP
  316. case NOUVEAU_GART_AGP:
  317. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  318. #endif
  319. case NOUVEAU_GART_PDMA:
  320. case NOUVEAU_GART_HW:
  321. return nouveau_sgdma_init_ttm(dev);
  322. default:
  323. NV_ERROR(dev, "Unknown GART type %d\n",
  324. dev_priv->gart_info.type);
  325. break;
  326. }
  327. return NULL;
  328. }
  329. static int
  330. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  331. {
  332. /* We'll do this from user space. */
  333. return 0;
  334. }
  335. static int
  336. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  337. struct ttm_mem_type_manager *man)
  338. {
  339. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  340. struct drm_device *dev = dev_priv->dev;
  341. switch (type) {
  342. case TTM_PL_SYSTEM:
  343. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  344. man->available_caching = TTM_PL_MASK_CACHING;
  345. man->default_caching = TTM_PL_FLAG_CACHED;
  346. break;
  347. case TTM_PL_VRAM:
  348. if (dev_priv->card_type >= NV_50) {
  349. man->func = &nouveau_vram_manager;
  350. man->io_reserve_fastpath = false;
  351. man->use_io_reserve_lru = true;
  352. } else {
  353. man->func = &ttm_bo_manager_func;
  354. }
  355. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  356. TTM_MEMTYPE_FLAG_MAPPABLE;
  357. man->available_caching = TTM_PL_FLAG_UNCACHED |
  358. TTM_PL_FLAG_WC;
  359. man->default_caching = TTM_PL_FLAG_WC;
  360. break;
  361. case TTM_PL_TT:
  362. if (dev_priv->card_type >= NV_50)
  363. man->func = &nouveau_gart_manager;
  364. else
  365. man->func = &ttm_bo_manager_func;
  366. switch (dev_priv->gart_info.type) {
  367. case NOUVEAU_GART_AGP:
  368. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  369. man->available_caching = TTM_PL_FLAG_UNCACHED |
  370. TTM_PL_FLAG_WC;
  371. man->default_caching = TTM_PL_FLAG_WC;
  372. break;
  373. case NOUVEAU_GART_PDMA:
  374. case NOUVEAU_GART_HW:
  375. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  376. TTM_MEMTYPE_FLAG_CMA;
  377. man->available_caching = TTM_PL_MASK_CACHING;
  378. man->default_caching = TTM_PL_FLAG_CACHED;
  379. man->gpu_offset = dev_priv->gart_info.aper_base;
  380. break;
  381. default:
  382. NV_ERROR(dev, "Unknown GART type: %d\n",
  383. dev_priv->gart_info.type);
  384. return -EINVAL;
  385. }
  386. break;
  387. default:
  388. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. static void
  394. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  395. {
  396. struct nouveau_bo *nvbo = nouveau_bo(bo);
  397. switch (bo->mem.mem_type) {
  398. case TTM_PL_VRAM:
  399. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  400. TTM_PL_FLAG_SYSTEM);
  401. break;
  402. default:
  403. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  404. break;
  405. }
  406. *pl = nvbo->placement;
  407. }
  408. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  409. * TTM_PL_{VRAM,TT} directly.
  410. */
  411. static int
  412. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  413. struct nouveau_bo *nvbo, bool evict,
  414. bool no_wait_reserve, bool no_wait_gpu,
  415. struct ttm_mem_reg *new_mem)
  416. {
  417. struct nouveau_fence *fence = NULL;
  418. int ret;
  419. ret = nouveau_fence_new(chan, &fence, true);
  420. if (ret)
  421. return ret;
  422. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  423. no_wait_reserve, no_wait_gpu, new_mem);
  424. nouveau_fence_unref(&fence);
  425. return ret;
  426. }
  427. static int
  428. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  429. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  430. {
  431. struct nouveau_mem *node = old_mem->mm_node;
  432. u64 src_offset = node->vma[0].offset;
  433. u64 dst_offset = node->vma[1].offset;
  434. u32 page_count = new_mem->num_pages;
  435. int ret;
  436. page_count = new_mem->num_pages;
  437. while (page_count) {
  438. int line_count = (page_count > 2047) ? 2047 : page_count;
  439. ret = RING_SPACE(chan, 12);
  440. if (ret)
  441. return ret;
  442. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0238, 2);
  443. OUT_RING (chan, upper_32_bits(dst_offset));
  444. OUT_RING (chan, lower_32_bits(dst_offset));
  445. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x030c, 6);
  446. OUT_RING (chan, upper_32_bits(src_offset));
  447. OUT_RING (chan, lower_32_bits(src_offset));
  448. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  449. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  450. OUT_RING (chan, PAGE_SIZE); /* line_length */
  451. OUT_RING (chan, line_count);
  452. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0300, 1);
  453. OUT_RING (chan, 0x00100110);
  454. page_count -= line_count;
  455. src_offset += (PAGE_SIZE * line_count);
  456. dst_offset += (PAGE_SIZE * line_count);
  457. }
  458. return 0;
  459. }
  460. static int
  461. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  462. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  463. {
  464. struct nouveau_mem *node = old_mem->mm_node;
  465. struct nouveau_bo *nvbo = nouveau_bo(bo);
  466. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  467. u64 src_offset = node->vma[0].offset;
  468. u64 dst_offset = node->vma[1].offset;
  469. int ret;
  470. while (length) {
  471. u32 amount, stride, height;
  472. amount = min(length, (u64)(4 * 1024 * 1024));
  473. stride = 16 * 4;
  474. height = amount / stride;
  475. if (new_mem->mem_type == TTM_PL_VRAM &&
  476. nouveau_bo_tile_layout(nvbo)) {
  477. ret = RING_SPACE(chan, 8);
  478. if (ret)
  479. return ret;
  480. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  481. OUT_RING (chan, 0);
  482. OUT_RING (chan, 0);
  483. OUT_RING (chan, stride);
  484. OUT_RING (chan, height);
  485. OUT_RING (chan, 1);
  486. OUT_RING (chan, 0);
  487. OUT_RING (chan, 0);
  488. } else {
  489. ret = RING_SPACE(chan, 2);
  490. if (ret)
  491. return ret;
  492. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  493. OUT_RING (chan, 1);
  494. }
  495. if (old_mem->mem_type == TTM_PL_VRAM &&
  496. nouveau_bo_tile_layout(nvbo)) {
  497. ret = RING_SPACE(chan, 8);
  498. if (ret)
  499. return ret;
  500. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  501. OUT_RING (chan, 0);
  502. OUT_RING (chan, 0);
  503. OUT_RING (chan, stride);
  504. OUT_RING (chan, height);
  505. OUT_RING (chan, 1);
  506. OUT_RING (chan, 0);
  507. OUT_RING (chan, 0);
  508. } else {
  509. ret = RING_SPACE(chan, 2);
  510. if (ret)
  511. return ret;
  512. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  513. OUT_RING (chan, 1);
  514. }
  515. ret = RING_SPACE(chan, 14);
  516. if (ret)
  517. return ret;
  518. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  519. OUT_RING (chan, upper_32_bits(src_offset));
  520. OUT_RING (chan, upper_32_bits(dst_offset));
  521. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  522. OUT_RING (chan, lower_32_bits(src_offset));
  523. OUT_RING (chan, lower_32_bits(dst_offset));
  524. OUT_RING (chan, stride);
  525. OUT_RING (chan, stride);
  526. OUT_RING (chan, stride);
  527. OUT_RING (chan, height);
  528. OUT_RING (chan, 0x00000101);
  529. OUT_RING (chan, 0x00000000);
  530. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  531. OUT_RING (chan, 0);
  532. length -= amount;
  533. src_offset += amount;
  534. dst_offset += amount;
  535. }
  536. return 0;
  537. }
  538. static inline uint32_t
  539. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  540. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  541. {
  542. if (mem->mem_type == TTM_PL_TT)
  543. return chan->gart_handle;
  544. return chan->vram_handle;
  545. }
  546. static int
  547. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  548. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  549. {
  550. u32 src_offset = old_mem->start << PAGE_SHIFT;
  551. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  552. u32 page_count = new_mem->num_pages;
  553. int ret;
  554. ret = RING_SPACE(chan, 3);
  555. if (ret)
  556. return ret;
  557. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  558. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  559. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  560. page_count = new_mem->num_pages;
  561. while (page_count) {
  562. int line_count = (page_count > 2047) ? 2047 : page_count;
  563. ret = RING_SPACE(chan, 11);
  564. if (ret)
  565. return ret;
  566. BEGIN_RING(chan, NvSubM2MF,
  567. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  568. OUT_RING (chan, src_offset);
  569. OUT_RING (chan, dst_offset);
  570. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  571. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  572. OUT_RING (chan, PAGE_SIZE); /* line_length */
  573. OUT_RING (chan, line_count);
  574. OUT_RING (chan, 0x00000101);
  575. OUT_RING (chan, 0x00000000);
  576. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  577. OUT_RING (chan, 0);
  578. page_count -= line_count;
  579. src_offset += (PAGE_SIZE * line_count);
  580. dst_offset += (PAGE_SIZE * line_count);
  581. }
  582. return 0;
  583. }
  584. static int
  585. nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
  586. struct ttm_mem_reg *mem, struct nouveau_vma *vma)
  587. {
  588. struct nouveau_mem *node = mem->mm_node;
  589. int ret;
  590. ret = nouveau_vm_get(chan->vm, mem->num_pages << PAGE_SHIFT,
  591. node->page_shift, NV_MEM_ACCESS_RO, vma);
  592. if (ret)
  593. return ret;
  594. if (mem->mem_type == TTM_PL_VRAM)
  595. nouveau_vm_map(vma, node);
  596. else
  597. nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT,
  598. node, node->pages);
  599. return 0;
  600. }
  601. static int
  602. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  603. bool no_wait_reserve, bool no_wait_gpu,
  604. struct ttm_mem_reg *new_mem)
  605. {
  606. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  607. struct nouveau_bo *nvbo = nouveau_bo(bo);
  608. struct ttm_mem_reg *old_mem = &bo->mem;
  609. struct nouveau_channel *chan;
  610. int ret;
  611. chan = nvbo->channel;
  612. if (!chan) {
  613. chan = dev_priv->channel;
  614. mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
  615. }
  616. /* create temporary vmas for the transfer and attach them to the
  617. * old nouveau_mem node, these will get cleaned up after ttm has
  618. * destroyed the ttm_mem_reg
  619. */
  620. if (dev_priv->card_type >= NV_50) {
  621. struct nouveau_mem *node = old_mem->mm_node;
  622. ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
  623. if (ret)
  624. goto out;
  625. ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
  626. if (ret)
  627. goto out;
  628. }
  629. if (dev_priv->card_type < NV_50)
  630. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  631. else
  632. if (dev_priv->card_type < NV_C0)
  633. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  634. else
  635. ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  636. if (ret == 0) {
  637. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  638. no_wait_reserve,
  639. no_wait_gpu, new_mem);
  640. }
  641. out:
  642. if (chan == dev_priv->channel)
  643. mutex_unlock(&chan->mutex);
  644. return ret;
  645. }
  646. static int
  647. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  648. bool no_wait_reserve, bool no_wait_gpu,
  649. struct ttm_mem_reg *new_mem)
  650. {
  651. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  652. struct ttm_placement placement;
  653. struct ttm_mem_reg tmp_mem;
  654. int ret;
  655. placement.fpfn = placement.lpfn = 0;
  656. placement.num_placement = placement.num_busy_placement = 1;
  657. placement.placement = placement.busy_placement = &placement_memtype;
  658. tmp_mem = *new_mem;
  659. tmp_mem.mm_node = NULL;
  660. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  661. if (ret)
  662. return ret;
  663. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  664. if (ret)
  665. goto out;
  666. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  667. if (ret)
  668. goto out;
  669. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
  670. out:
  671. ttm_bo_mem_put(bo, &tmp_mem);
  672. return ret;
  673. }
  674. static int
  675. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  676. bool no_wait_reserve, bool no_wait_gpu,
  677. struct ttm_mem_reg *new_mem)
  678. {
  679. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  680. struct ttm_placement placement;
  681. struct ttm_mem_reg tmp_mem;
  682. int ret;
  683. placement.fpfn = placement.lpfn = 0;
  684. placement.num_placement = placement.num_busy_placement = 1;
  685. placement.placement = placement.busy_placement = &placement_memtype;
  686. tmp_mem = *new_mem;
  687. tmp_mem.mm_node = NULL;
  688. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  689. if (ret)
  690. return ret;
  691. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
  692. if (ret)
  693. goto out;
  694. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
  695. if (ret)
  696. goto out;
  697. out:
  698. ttm_bo_mem_put(bo, &tmp_mem);
  699. return ret;
  700. }
  701. static void
  702. nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
  703. {
  704. struct nouveau_mem *node = new_mem->mm_node;
  705. struct nouveau_bo *nvbo = nouveau_bo(bo);
  706. struct nouveau_vma *vma = &nvbo->vma;
  707. if (!vma->vm)
  708. return;
  709. if (new_mem->mem_type == TTM_PL_VRAM) {
  710. nouveau_vm_map(&nvbo->vma, new_mem->mm_node);
  711. } else
  712. if (new_mem->mem_type == TTM_PL_TT &&
  713. nvbo->page_shift == nvbo->vma.vm->spg_shift) {
  714. nouveau_vm_map_sg(&nvbo->vma, 0, new_mem->
  715. num_pages << PAGE_SHIFT, node, node->pages);
  716. } else {
  717. nouveau_vm_unmap(&nvbo->vma);
  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_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. struct ttm_bo_driver nouveau_bo_driver = {
  909. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  910. .invalidate_caches = nouveau_bo_invalidate_caches,
  911. .init_mem_type = nouveau_bo_init_mem_type,
  912. .evict_flags = nouveau_bo_evict_flags,
  913. .move_notify = nouveau_bo_move_ntfy,
  914. .move = nouveau_bo_move,
  915. .verify_access = nouveau_bo_verify_access,
  916. .sync_obj_signaled = __nouveau_fence_signalled,
  917. .sync_obj_wait = __nouveau_fence_wait,
  918. .sync_obj_flush = __nouveau_fence_flush,
  919. .sync_obj_unref = __nouveau_fence_unref,
  920. .sync_obj_ref = __nouveau_fence_ref,
  921. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  922. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  923. .io_mem_free = &nouveau_ttm_io_mem_free,
  924. };