nouveau_bo.c 23 KB

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