nouveau_bo.c 25 KB

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