nouveau_bo.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. nouveau_vm_put(&nvbo->vma);
  47. kfree(nvbo);
  48. }
  49. static void
  50. nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
  51. int *align, int *size, int *page_shift)
  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. if (likely(dev_priv->chan_vm)) {
  72. if (!(flags & TTM_PL_FLAG_TT) && *size > 256 * 1024)
  73. *page_shift = dev_priv->chan_vm->lpg_shift;
  74. else
  75. *page_shift = dev_priv->chan_vm->spg_shift;
  76. } else {
  77. *page_shift = 12;
  78. }
  79. *size = roundup(*size, (1 << *page_shift));
  80. *align = max((1 << *page_shift), *align);
  81. }
  82. *size = roundup(*size, PAGE_SIZE);
  83. }
  84. int
  85. nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
  86. int size, int align, uint32_t flags, uint32_t tile_mode,
  87. uint32_t tile_flags, struct nouveau_bo **pnvbo)
  88. {
  89. struct drm_nouveau_private *dev_priv = dev->dev_private;
  90. struct nouveau_bo *nvbo;
  91. int ret = 0, page_shift = 0;
  92. nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
  93. if (!nvbo)
  94. return -ENOMEM;
  95. INIT_LIST_HEAD(&nvbo->head);
  96. INIT_LIST_HEAD(&nvbo->entry);
  97. nvbo->tile_mode = tile_mode;
  98. nvbo->tile_flags = tile_flags;
  99. nvbo->bo.bdev = &dev_priv->ttm.bdev;
  100. nouveau_bo_fixup_align(nvbo, flags, &align, &size, &page_shift);
  101. align >>= PAGE_SHIFT;
  102. if (dev_priv->chan_vm) {
  103. ret = nouveau_vm_get(dev_priv->chan_vm, size, 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. if (nvbo->vma.node) {
  122. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  123. nvbo->bo.offset = nvbo->vma.offset;
  124. }
  125. *pnvbo = nvbo;
  126. return 0;
  127. }
  128. static void
  129. set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
  130. {
  131. *n = 0;
  132. if (type & TTM_PL_FLAG_VRAM)
  133. pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
  134. if (type & TTM_PL_FLAG_TT)
  135. pl[(*n)++] = TTM_PL_FLAG_TT | flags;
  136. if (type & TTM_PL_FLAG_SYSTEM)
  137. pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
  138. }
  139. static void
  140. set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
  141. {
  142. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  143. int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
  144. if (dev_priv->card_type == NV_10 &&
  145. nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
  146. nvbo->bo.mem.num_pages < vram_pages / 2) {
  147. /*
  148. * Make sure that the color and depth buffers are handled
  149. * by independent memory controller units. Up to a 9x
  150. * speed up when alpha-blending and depth-test are enabled
  151. * at the same time.
  152. */
  153. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
  154. nvbo->placement.fpfn = vram_pages / 2;
  155. nvbo->placement.lpfn = ~0;
  156. } else {
  157. nvbo->placement.fpfn = 0;
  158. nvbo->placement.lpfn = vram_pages / 2;
  159. }
  160. }
  161. }
  162. void
  163. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
  164. {
  165. struct ttm_placement *pl = &nvbo->placement;
  166. uint32_t flags = TTM_PL_MASK_CACHING |
  167. (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
  168. pl->placement = nvbo->placements;
  169. set_placement_list(nvbo->placements, &pl->num_placement,
  170. type, flags);
  171. pl->busy_placement = nvbo->busy_placements;
  172. set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
  173. type | busy, flags);
  174. set_placement_range(nvbo, type);
  175. }
  176. int
  177. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  178. {
  179. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  180. struct ttm_buffer_object *bo = &nvbo->bo;
  181. int ret;
  182. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  183. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  184. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  185. 1 << bo->mem.mem_type, memtype);
  186. return -EINVAL;
  187. }
  188. if (nvbo->pin_refcnt++)
  189. return 0;
  190. ret = ttm_bo_reserve(bo, false, false, false, 0);
  191. if (ret)
  192. goto out;
  193. nouveau_bo_placement_set(nvbo, memtype, 0);
  194. ret = nouveau_bo_validate(nvbo, false, false, false);
  195. if (ret == 0) {
  196. switch (bo->mem.mem_type) {
  197. case TTM_PL_VRAM:
  198. dev_priv->fb_aper_free -= bo->mem.size;
  199. break;
  200. case TTM_PL_TT:
  201. dev_priv->gart_info.aper_free -= bo->mem.size;
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. ttm_bo_unreserve(bo);
  208. out:
  209. if (unlikely(ret))
  210. nvbo->pin_refcnt--;
  211. return ret;
  212. }
  213. int
  214. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  215. {
  216. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  217. struct ttm_buffer_object *bo = &nvbo->bo;
  218. int ret;
  219. if (--nvbo->pin_refcnt)
  220. return 0;
  221. ret = ttm_bo_reserve(bo, false, false, false, 0);
  222. if (ret)
  223. return ret;
  224. nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
  225. ret = nouveau_bo_validate(nvbo, false, false, false);
  226. if (ret == 0) {
  227. switch (bo->mem.mem_type) {
  228. case TTM_PL_VRAM:
  229. dev_priv->fb_aper_free += bo->mem.size;
  230. break;
  231. case TTM_PL_TT:
  232. dev_priv->gart_info.aper_free += bo->mem.size;
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. ttm_bo_unreserve(bo);
  239. return ret;
  240. }
  241. int
  242. nouveau_bo_map(struct nouveau_bo *nvbo)
  243. {
  244. int ret;
  245. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  246. if (ret)
  247. return ret;
  248. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  249. ttm_bo_unreserve(&nvbo->bo);
  250. return ret;
  251. }
  252. void
  253. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  254. {
  255. if (nvbo)
  256. ttm_bo_kunmap(&nvbo->kmap);
  257. }
  258. int
  259. nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
  260. bool no_wait_reserve, bool no_wait_gpu)
  261. {
  262. int ret;
  263. ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
  264. no_wait_reserve, no_wait_gpu);
  265. if (ret)
  266. return ret;
  267. if (nvbo->vma.node) {
  268. if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
  269. nvbo->bo.offset = nvbo->vma.offset;
  270. }
  271. return 0;
  272. }
  273. u16
  274. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  275. {
  276. bool is_iomem;
  277. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  278. mem = &mem[index];
  279. if (is_iomem)
  280. return ioread16_native((void __force __iomem *)mem);
  281. else
  282. return *mem;
  283. }
  284. void
  285. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  286. {
  287. bool is_iomem;
  288. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  289. mem = &mem[index];
  290. if (is_iomem)
  291. iowrite16_native(val, (void __force __iomem *)mem);
  292. else
  293. *mem = val;
  294. }
  295. u32
  296. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  297. {
  298. bool is_iomem;
  299. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  300. mem = &mem[index];
  301. if (is_iomem)
  302. return ioread32_native((void __force __iomem *)mem);
  303. else
  304. return *mem;
  305. }
  306. void
  307. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  308. {
  309. bool is_iomem;
  310. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  311. mem = &mem[index];
  312. if (is_iomem)
  313. iowrite32_native(val, (void __force __iomem *)mem);
  314. else
  315. *mem = val;
  316. }
  317. static struct ttm_backend *
  318. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  319. {
  320. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  321. struct drm_device *dev = dev_priv->dev;
  322. switch (dev_priv->gart_info.type) {
  323. #if __OS_HAS_AGP
  324. case NOUVEAU_GART_AGP:
  325. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  326. #endif
  327. case NOUVEAU_GART_PDMA:
  328. case NOUVEAU_GART_HW:
  329. return nouveau_sgdma_init_ttm(dev);
  330. default:
  331. NV_ERROR(dev, "Unknown GART type %d\n",
  332. dev_priv->gart_info.type);
  333. break;
  334. }
  335. return NULL;
  336. }
  337. static int
  338. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  339. {
  340. /* We'll do this from user space. */
  341. return 0;
  342. }
  343. static int
  344. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  345. struct ttm_mem_type_manager *man)
  346. {
  347. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  348. struct drm_device *dev = dev_priv->dev;
  349. switch (type) {
  350. case TTM_PL_SYSTEM:
  351. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  352. man->available_caching = TTM_PL_MASK_CACHING;
  353. man->default_caching = TTM_PL_FLAG_CACHED;
  354. break;
  355. case TTM_PL_VRAM:
  356. if (dev_priv->card_type >= NV_50) {
  357. man->func = &nouveau_vram_manager;
  358. man->io_reserve_fastpath = false;
  359. man->use_io_reserve_lru = true;
  360. } else {
  361. man->func = &ttm_bo_manager_func;
  362. }
  363. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  364. TTM_MEMTYPE_FLAG_MAPPABLE;
  365. man->available_caching = TTM_PL_FLAG_UNCACHED |
  366. TTM_PL_FLAG_WC;
  367. man->default_caching = TTM_PL_FLAG_WC;
  368. break;
  369. case TTM_PL_TT:
  370. man->func = &ttm_bo_manager_func;
  371. switch (dev_priv->gart_info.type) {
  372. case NOUVEAU_GART_AGP:
  373. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  374. man->available_caching = TTM_PL_FLAG_UNCACHED |
  375. TTM_PL_FLAG_WC;
  376. man->default_caching = TTM_PL_FLAG_WC;
  377. break;
  378. case NOUVEAU_GART_PDMA:
  379. case NOUVEAU_GART_HW:
  380. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  381. TTM_MEMTYPE_FLAG_CMA;
  382. man->available_caching = TTM_PL_MASK_CACHING;
  383. man->default_caching = TTM_PL_FLAG_CACHED;
  384. man->gpu_offset = dev_priv->gart_info.aper_base;
  385. break;
  386. default:
  387. NV_ERROR(dev, "Unknown GART type: %d\n",
  388. dev_priv->gart_info.type);
  389. return -EINVAL;
  390. }
  391. break;
  392. default:
  393. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  394. return -EINVAL;
  395. }
  396. return 0;
  397. }
  398. static void
  399. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  400. {
  401. struct nouveau_bo *nvbo = nouveau_bo(bo);
  402. switch (bo->mem.mem_type) {
  403. case TTM_PL_VRAM:
  404. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  405. TTM_PL_FLAG_SYSTEM);
  406. break;
  407. default:
  408. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  409. break;
  410. }
  411. *pl = nvbo->placement;
  412. }
  413. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  414. * TTM_PL_{VRAM,TT} directly.
  415. */
  416. static int
  417. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  418. struct nouveau_bo *nvbo, bool evict,
  419. bool no_wait_reserve, bool no_wait_gpu,
  420. struct ttm_mem_reg *new_mem)
  421. {
  422. struct nouveau_fence *fence = NULL;
  423. int ret;
  424. ret = nouveau_fence_new(chan, &fence, true);
  425. if (ret)
  426. return ret;
  427. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
  428. no_wait_reserve, no_wait_gpu, new_mem);
  429. nouveau_fence_unref(&fence);
  430. return ret;
  431. }
  432. static int
  433. nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  434. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  435. {
  436. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  437. struct nouveau_bo *nvbo = nouveau_bo(bo);
  438. u32 page_count = new_mem->num_pages;
  439. u64 src_offset, dst_offset;
  440. int ret;
  441. src_offset = old_mem->start << PAGE_SHIFT;
  442. if (old_mem->mem_type == TTM_PL_VRAM)
  443. src_offset = nvbo->vma.offset;
  444. else
  445. src_offset += dev_priv->gart_info.aper_base;
  446. dst_offset = new_mem->start << PAGE_SHIFT;
  447. if (new_mem->mem_type == TTM_PL_VRAM)
  448. dst_offset = nvbo->vma.offset;
  449. else
  450. dst_offset += dev_priv->gart_info.aper_base;
  451. page_count = new_mem->num_pages;
  452. while (page_count) {
  453. int line_count = (page_count > 2047) ? 2047 : page_count;
  454. ret = RING_SPACE(chan, 12);
  455. if (ret)
  456. return ret;
  457. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0238, 2);
  458. OUT_RING (chan, upper_32_bits(dst_offset));
  459. OUT_RING (chan, lower_32_bits(dst_offset));
  460. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x030c, 6);
  461. OUT_RING (chan, upper_32_bits(src_offset));
  462. OUT_RING (chan, lower_32_bits(src_offset));
  463. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  464. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  465. OUT_RING (chan, PAGE_SIZE); /* line_length */
  466. OUT_RING (chan, line_count);
  467. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0300, 1);
  468. OUT_RING (chan, 0x00100110);
  469. page_count -= line_count;
  470. src_offset += (PAGE_SIZE * line_count);
  471. dst_offset += (PAGE_SIZE * line_count);
  472. }
  473. return 0;
  474. }
  475. static int
  476. nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  477. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  478. {
  479. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  480. struct nouveau_bo *nvbo = nouveau_bo(bo);
  481. u64 length = (new_mem->num_pages << PAGE_SHIFT);
  482. u64 src_offset, dst_offset;
  483. int ret;
  484. src_offset = old_mem->start << PAGE_SHIFT;
  485. if (old_mem->mem_type == TTM_PL_VRAM)
  486. src_offset = nvbo->vma.offset;
  487. else
  488. src_offset += dev_priv->gart_info.aper_base;
  489. dst_offset = new_mem->start << PAGE_SHIFT;
  490. if (new_mem->mem_type == TTM_PL_VRAM)
  491. dst_offset = nvbo->vma.offset;
  492. else
  493. dst_offset += dev_priv->gart_info.aper_base;
  494. while (length) {
  495. u32 amount, stride, height;
  496. amount = min(length, (u64)(4 * 1024 * 1024));
  497. stride = 16 * 4;
  498. height = amount / stride;
  499. if (new_mem->mem_type == TTM_PL_VRAM &&
  500. nouveau_bo_tile_layout(nvbo)) {
  501. ret = RING_SPACE(chan, 8);
  502. if (ret)
  503. return ret;
  504. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  505. OUT_RING (chan, 0);
  506. OUT_RING (chan, 0);
  507. OUT_RING (chan, stride);
  508. OUT_RING (chan, height);
  509. OUT_RING (chan, 1);
  510. OUT_RING (chan, 0);
  511. OUT_RING (chan, 0);
  512. } else {
  513. ret = RING_SPACE(chan, 2);
  514. if (ret)
  515. return ret;
  516. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  517. OUT_RING (chan, 1);
  518. }
  519. if (old_mem->mem_type == TTM_PL_VRAM &&
  520. nouveau_bo_tile_layout(nvbo)) {
  521. ret = RING_SPACE(chan, 8);
  522. if (ret)
  523. return ret;
  524. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  525. OUT_RING (chan, 0);
  526. OUT_RING (chan, 0);
  527. OUT_RING (chan, stride);
  528. OUT_RING (chan, height);
  529. OUT_RING (chan, 1);
  530. OUT_RING (chan, 0);
  531. OUT_RING (chan, 0);
  532. } else {
  533. ret = RING_SPACE(chan, 2);
  534. if (ret)
  535. return ret;
  536. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  537. OUT_RING (chan, 1);
  538. }
  539. ret = RING_SPACE(chan, 14);
  540. if (ret)
  541. return ret;
  542. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  543. OUT_RING (chan, upper_32_bits(src_offset));
  544. OUT_RING (chan, upper_32_bits(dst_offset));
  545. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  546. OUT_RING (chan, lower_32_bits(src_offset));
  547. OUT_RING (chan, lower_32_bits(dst_offset));
  548. OUT_RING (chan, stride);
  549. OUT_RING (chan, stride);
  550. OUT_RING (chan, stride);
  551. OUT_RING (chan, height);
  552. OUT_RING (chan, 0x00000101);
  553. OUT_RING (chan, 0x00000000);
  554. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  555. OUT_RING (chan, 0);
  556. length -= amount;
  557. src_offset += amount;
  558. dst_offset += amount;
  559. }
  560. return 0;
  561. }
  562. static inline uint32_t
  563. nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
  564. struct nouveau_channel *chan, struct ttm_mem_reg *mem)
  565. {
  566. if (mem->mem_type == TTM_PL_TT)
  567. return chan->gart_handle;
  568. return chan->vram_handle;
  569. }
  570. static int
  571. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  572. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  573. {
  574. u32 src_offset = old_mem->start << PAGE_SHIFT;
  575. u32 dst_offset = new_mem->start << PAGE_SHIFT;
  576. u32 page_count = new_mem->num_pages;
  577. int ret;
  578. ret = RING_SPACE(chan, 3);
  579. if (ret)
  580. return ret;
  581. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  582. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  583. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  584. page_count = new_mem->num_pages;
  585. while (page_count) {
  586. int line_count = (page_count > 2047) ? 2047 : page_count;
  587. ret = RING_SPACE(chan, 11);
  588. if (ret)
  589. return ret;
  590. BEGIN_RING(chan, NvSubM2MF,
  591. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  592. OUT_RING (chan, src_offset);
  593. OUT_RING (chan, dst_offset);
  594. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  595. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  596. OUT_RING (chan, PAGE_SIZE); /* line_length */
  597. OUT_RING (chan, line_count);
  598. OUT_RING (chan, 0x00000101);
  599. OUT_RING (chan, 0x00000000);
  600. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  601. OUT_RING (chan, 0);
  602. page_count -= line_count;
  603. src_offset += (PAGE_SIZE * line_count);
  604. dst_offset += (PAGE_SIZE * line_count);
  605. }
  606. return 0;
  607. }
  608. static int
  609. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  610. bool no_wait_reserve, bool no_wait_gpu,
  611. struct ttm_mem_reg *new_mem)
  612. {
  613. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  614. struct nouveau_bo *nvbo = nouveau_bo(bo);
  615. struct nouveau_channel *chan;
  616. int ret;
  617. chan = nvbo->channel;
  618. if (!chan) {
  619. chan = dev_priv->channel;
  620. mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
  621. }
  622. if (dev_priv->card_type < NV_50)
  623. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  624. else
  625. if (dev_priv->card_type < NV_C0)
  626. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  627. else
  628. ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  629. if (ret == 0) {
  630. ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
  631. no_wait_reserve,
  632. no_wait_gpu, new_mem);
  633. }
  634. if (chan == dev_priv->channel)
  635. mutex_unlock(&chan->mutex);
  636. return ret;
  637. }
  638. static int
  639. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  640. bool no_wait_reserve, bool no_wait_gpu,
  641. struct ttm_mem_reg *new_mem)
  642. {
  643. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  644. struct ttm_placement placement;
  645. struct ttm_mem_reg tmp_mem;
  646. int ret;
  647. placement.fpfn = placement.lpfn = 0;
  648. placement.num_placement = placement.num_busy_placement = 1;
  649. placement.placement = placement.busy_placement = &placement_memtype;
  650. tmp_mem = *new_mem;
  651. tmp_mem.mm_node = NULL;
  652. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  653. if (ret)
  654. return ret;
  655. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  656. if (ret)
  657. goto out;
  658. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  659. if (ret)
  660. goto out;
  661. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
  662. out:
  663. ttm_bo_mem_put(bo, &tmp_mem);
  664. return ret;
  665. }
  666. static int
  667. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  668. bool no_wait_reserve, bool no_wait_gpu,
  669. struct ttm_mem_reg *new_mem)
  670. {
  671. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  672. struct ttm_placement placement;
  673. struct ttm_mem_reg tmp_mem;
  674. int ret;
  675. placement.fpfn = placement.lpfn = 0;
  676. placement.num_placement = placement.num_busy_placement = 1;
  677. placement.placement = placement.busy_placement = &placement_memtype;
  678. tmp_mem = *new_mem;
  679. tmp_mem.mm_node = NULL;
  680. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  681. if (ret)
  682. return ret;
  683. ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
  684. if (ret)
  685. goto out;
  686. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
  687. if (ret)
  688. goto out;
  689. out:
  690. ttm_bo_mem_put(bo, &tmp_mem);
  691. return ret;
  692. }
  693. static int
  694. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  695. struct nouveau_tile_reg **new_tile)
  696. {
  697. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  698. struct drm_device *dev = dev_priv->dev;
  699. struct nouveau_bo *nvbo = nouveau_bo(bo);
  700. uint64_t offset;
  701. if (new_mem->mem_type != TTM_PL_VRAM) {
  702. /* Nothing to do. */
  703. *new_tile = NULL;
  704. return 0;
  705. }
  706. offset = new_mem->start << PAGE_SHIFT;
  707. if (dev_priv->chan_vm) {
  708. nouveau_vm_map(&nvbo->vma, new_mem->mm_node);
  709. } else if (dev_priv->card_type >= NV_10) {
  710. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  711. nvbo->tile_mode,
  712. nvbo->tile_flags);
  713. }
  714. return 0;
  715. }
  716. static void
  717. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  718. struct nouveau_tile_reg *new_tile,
  719. struct nouveau_tile_reg **old_tile)
  720. {
  721. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  722. struct drm_device *dev = dev_priv->dev;
  723. if (dev_priv->card_type >= NV_10 &&
  724. dev_priv->card_type < NV_50) {
  725. nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
  726. *old_tile = new_tile;
  727. }
  728. }
  729. static int
  730. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  731. bool no_wait_reserve, bool no_wait_gpu,
  732. struct ttm_mem_reg *new_mem)
  733. {
  734. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  735. struct nouveau_bo *nvbo = nouveau_bo(bo);
  736. struct ttm_mem_reg *old_mem = &bo->mem;
  737. struct nouveau_tile_reg *new_tile = NULL;
  738. int ret = 0;
  739. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  740. if (ret)
  741. return ret;
  742. /* Fake bo copy. */
  743. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  744. BUG_ON(bo->mem.mm_node != NULL);
  745. bo->mem = *new_mem;
  746. new_mem->mm_node = NULL;
  747. goto out;
  748. }
  749. /* Software copy if the card isn't up and running yet. */
  750. if (!dev_priv->channel) {
  751. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  752. goto out;
  753. }
  754. /* Hardware assisted copy. */
  755. if (new_mem->mem_type == TTM_PL_SYSTEM)
  756. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  757. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  758. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  759. else
  760. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  761. if (!ret)
  762. goto out;
  763. /* Fallback to software copy. */
  764. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  765. out:
  766. if (ret)
  767. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  768. else
  769. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  770. return ret;
  771. }
  772. static int
  773. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  774. {
  775. return 0;
  776. }
  777. static int
  778. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  779. {
  780. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  781. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  782. struct drm_device *dev = dev_priv->dev;
  783. int ret;
  784. mem->bus.addr = NULL;
  785. mem->bus.offset = 0;
  786. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  787. mem->bus.base = 0;
  788. mem->bus.is_iomem = false;
  789. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  790. return -EINVAL;
  791. switch (mem->mem_type) {
  792. case TTM_PL_SYSTEM:
  793. /* System memory */
  794. return 0;
  795. case TTM_PL_TT:
  796. #if __OS_HAS_AGP
  797. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  798. mem->bus.offset = mem->start << PAGE_SHIFT;
  799. mem->bus.base = dev_priv->gart_info.aper_base;
  800. mem->bus.is_iomem = true;
  801. }
  802. #endif
  803. break;
  804. case TTM_PL_VRAM:
  805. {
  806. struct nouveau_vram *vram = mem->mm_node;
  807. u8 page_shift;
  808. if (!dev_priv->bar1_vm) {
  809. mem->bus.offset = mem->start << PAGE_SHIFT;
  810. mem->bus.base = pci_resource_start(dev->pdev, 1);
  811. mem->bus.is_iomem = true;
  812. break;
  813. }
  814. if (dev_priv->card_type == NV_C0)
  815. page_shift = vram->page_shift;
  816. else
  817. page_shift = 12;
  818. ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,
  819. page_shift, NV_MEM_ACCESS_RW,
  820. &vram->bar_vma);
  821. if (ret)
  822. return ret;
  823. nouveau_vm_map(&vram->bar_vma, vram);
  824. if (ret) {
  825. nouveau_vm_put(&vram->bar_vma);
  826. return ret;
  827. }
  828. mem->bus.offset = vram->bar_vma.offset;
  829. if (dev_priv->card_type == NV_50) /*XXX*/
  830. mem->bus.offset -= 0x0020000000ULL;
  831. mem->bus.base = pci_resource_start(dev->pdev, 1);
  832. mem->bus.is_iomem = true;
  833. }
  834. break;
  835. default:
  836. return -EINVAL;
  837. }
  838. return 0;
  839. }
  840. static void
  841. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  842. {
  843. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  844. struct nouveau_vram *vram = mem->mm_node;
  845. if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
  846. return;
  847. if (!vram->bar_vma.node)
  848. return;
  849. nouveau_vm_unmap(&vram->bar_vma);
  850. nouveau_vm_put(&vram->bar_vma);
  851. }
  852. static int
  853. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  854. {
  855. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  856. struct nouveau_bo *nvbo = nouveau_bo(bo);
  857. /* as long as the bo isn't in vram, and isn't tiled, we've got
  858. * nothing to do here.
  859. */
  860. if (bo->mem.mem_type != TTM_PL_VRAM) {
  861. if (dev_priv->card_type < NV_50 ||
  862. !nouveau_bo_tile_layout(nvbo))
  863. return 0;
  864. }
  865. /* make sure bo is in mappable vram */
  866. if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
  867. return 0;
  868. nvbo->placement.fpfn = 0;
  869. nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
  870. nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
  871. return nouveau_bo_validate(nvbo, false, true, false);
  872. }
  873. void
  874. nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
  875. {
  876. struct nouveau_fence *old_fence;
  877. if (likely(fence))
  878. nouveau_fence_ref(fence);
  879. spin_lock(&nvbo->bo.bdev->fence_lock);
  880. old_fence = nvbo->bo.sync_obj;
  881. nvbo->bo.sync_obj = fence;
  882. spin_unlock(&nvbo->bo.bdev->fence_lock);
  883. nouveau_fence_unref(&old_fence);
  884. }
  885. struct ttm_bo_driver nouveau_bo_driver = {
  886. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  887. .invalidate_caches = nouveau_bo_invalidate_caches,
  888. .init_mem_type = nouveau_bo_init_mem_type,
  889. .evict_flags = nouveau_bo_evict_flags,
  890. .move = nouveau_bo_move,
  891. .verify_access = nouveau_bo_verify_access,
  892. .sync_obj_signaled = __nouveau_fence_signalled,
  893. .sync_obj_wait = __nouveau_fence_wait,
  894. .sync_obj_flush = __nouveau_fence_flush,
  895. .sync_obj_unref = __nouveau_fence_unref,
  896. .sync_obj_ref = __nouveau_fence_ref,
  897. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  898. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  899. .io_mem_free = &nouveau_ttm_io_mem_free,
  900. };