nouveau_bo.c 25 KB

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