nouveau_bo.c 25 KB

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