nouveau_bo.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. man->gpu_offset = dev_priv->vm_vram_base;
  351. break;
  352. case TTM_PL_TT:
  353. switch (dev_priv->gart_info.type) {
  354. case NOUVEAU_GART_AGP:
  355. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  356. man->available_caching = TTM_PL_FLAG_UNCACHED;
  357. man->default_caching = TTM_PL_FLAG_UNCACHED;
  358. break;
  359. case NOUVEAU_GART_SGDMA:
  360. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  361. TTM_MEMTYPE_FLAG_CMA;
  362. man->available_caching = TTM_PL_MASK_CACHING;
  363. man->default_caching = TTM_PL_FLAG_CACHED;
  364. break;
  365. default:
  366. NV_ERROR(dev, "Unknown GART type: %d\n",
  367. dev_priv->gart_info.type);
  368. return -EINVAL;
  369. }
  370. man->gpu_offset = dev_priv->vm_gart_base;
  371. break;
  372. default:
  373. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  374. return -EINVAL;
  375. }
  376. return 0;
  377. }
  378. static void
  379. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  380. {
  381. struct nouveau_bo *nvbo = nouveau_bo(bo);
  382. switch (bo->mem.mem_type) {
  383. case TTM_PL_VRAM:
  384. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
  385. TTM_PL_FLAG_SYSTEM);
  386. break;
  387. default:
  388. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
  389. break;
  390. }
  391. *pl = nvbo->placement;
  392. }
  393. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  394. * TTM_PL_{VRAM,TT} directly.
  395. */
  396. static int
  397. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  398. struct nouveau_bo *nvbo, bool evict,
  399. bool no_wait_reserve, bool no_wait_gpu,
  400. struct ttm_mem_reg *new_mem)
  401. {
  402. struct nouveau_fence *fence = NULL;
  403. int ret;
  404. ret = nouveau_fence_new(chan, &fence, true);
  405. if (ret)
  406. return ret;
  407. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL,
  408. evict || (nvbo->channel &&
  409. nvbo->channel != chan),
  410. no_wait_reserve, no_wait_gpu, new_mem);
  411. nouveau_fence_unref((void *)&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->mm_node->start << PAGE_SHIFT;
  438. dst_offset = new_mem->mm_node->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)(16 * 1024 * 1024));
  458. stride = 64 * 4;
  459. height = amount / stride;
  460. if (new_mem->mem_type == TTM_PL_VRAM && nvbo->tile_flags) {
  461. ret = RING_SPACE(chan, 8);
  462. if (ret)
  463. return ret;
  464. BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
  465. OUT_RING (chan, 0);
  466. OUT_RING (chan, 0x20);
  467. OUT_RING (chan, stride);
  468. OUT_RING (chan, height);
  469. OUT_RING (chan, 1);
  470. OUT_RING (chan, 0);
  471. OUT_RING (chan, 0);
  472. } else {
  473. ret = RING_SPACE(chan, 2);
  474. if (ret)
  475. return ret;
  476. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  477. OUT_RING (chan, 1);
  478. }
  479. if (old_mem->mem_type == TTM_PL_VRAM && nvbo->tile_flags) {
  480. ret = RING_SPACE(chan, 8);
  481. if (ret)
  482. return ret;
  483. BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
  484. OUT_RING (chan, 0);
  485. OUT_RING (chan, 0x20);
  486. OUT_RING (chan, stride);
  487. OUT_RING (chan, height);
  488. OUT_RING (chan, 1);
  489. OUT_RING (chan, 0);
  490. OUT_RING (chan, 0);
  491. } else {
  492. ret = RING_SPACE(chan, 2);
  493. if (ret)
  494. return ret;
  495. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  496. OUT_RING (chan, 1);
  497. }
  498. ret = RING_SPACE(chan, 14);
  499. if (ret)
  500. return ret;
  501. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  502. OUT_RING (chan, upper_32_bits(src_offset));
  503. OUT_RING (chan, upper_32_bits(dst_offset));
  504. BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
  505. OUT_RING (chan, lower_32_bits(src_offset));
  506. OUT_RING (chan, lower_32_bits(dst_offset));
  507. OUT_RING (chan, stride);
  508. OUT_RING (chan, stride);
  509. OUT_RING (chan, stride);
  510. OUT_RING (chan, height);
  511. OUT_RING (chan, 0x00000101);
  512. OUT_RING (chan, 0x00000000);
  513. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  514. OUT_RING (chan, 0);
  515. length -= amount;
  516. src_offset += amount;
  517. dst_offset += amount;
  518. }
  519. return 0;
  520. }
  521. static int
  522. nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
  523. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  524. {
  525. u32 src_offset = old_mem->mm_node->start << PAGE_SHIFT;
  526. u32 dst_offset = new_mem->mm_node->start << PAGE_SHIFT;
  527. u32 page_count = new_mem->num_pages;
  528. int ret;
  529. ret = RING_SPACE(chan, 3);
  530. if (ret)
  531. return ret;
  532. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  533. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
  534. OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
  535. page_count = new_mem->num_pages;
  536. while (page_count) {
  537. int line_count = (page_count > 2047) ? 2047 : page_count;
  538. ret = RING_SPACE(chan, 11);
  539. if (ret)
  540. return ret;
  541. BEGIN_RING(chan, NvSubM2MF,
  542. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  543. OUT_RING (chan, src_offset);
  544. OUT_RING (chan, dst_offset);
  545. OUT_RING (chan, PAGE_SIZE); /* src_pitch */
  546. OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
  547. OUT_RING (chan, PAGE_SIZE); /* line_length */
  548. OUT_RING (chan, line_count);
  549. OUT_RING (chan, 0x00000101);
  550. OUT_RING (chan, 0x00000000);
  551. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  552. OUT_RING (chan, 0);
  553. page_count -= line_count;
  554. src_offset += (PAGE_SIZE * line_count);
  555. dst_offset += (PAGE_SIZE * line_count);
  556. }
  557. return 0;
  558. }
  559. static int
  560. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
  561. bool no_wait_reserve, bool no_wait_gpu,
  562. struct ttm_mem_reg *new_mem)
  563. {
  564. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  565. struct nouveau_bo *nvbo = nouveau_bo(bo);
  566. struct nouveau_channel *chan;
  567. int ret;
  568. chan = nvbo->channel;
  569. if (!chan || nvbo->no_vm)
  570. chan = dev_priv->channel;
  571. if (dev_priv->card_type < NV_50)
  572. ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  573. else
  574. ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
  575. if (ret)
  576. return ret;
  577. return nouveau_bo_move_accel_cleanup(chan, nvbo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  578. }
  579. static int
  580. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  581. bool no_wait_reserve, bool no_wait_gpu,
  582. struct ttm_mem_reg *new_mem)
  583. {
  584. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  585. struct ttm_placement placement;
  586. struct ttm_mem_reg tmp_mem;
  587. int ret;
  588. placement.fpfn = placement.lpfn = 0;
  589. placement.num_placement = placement.num_busy_placement = 1;
  590. placement.placement = placement.busy_placement = &placement_memtype;
  591. tmp_mem = *new_mem;
  592. tmp_mem.mm_node = NULL;
  593. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  594. if (ret)
  595. return ret;
  596. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  597. if (ret)
  598. goto out;
  599. ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
  600. if (ret)
  601. goto out;
  602. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  603. out:
  604. if (tmp_mem.mm_node) {
  605. spin_lock(&bo->bdev->glob->lru_lock);
  606. drm_mm_put_block(tmp_mem.mm_node);
  607. spin_unlock(&bo->bdev->glob->lru_lock);
  608. }
  609. return ret;
  610. }
  611. static int
  612. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  613. bool no_wait_reserve, bool no_wait_gpu,
  614. struct ttm_mem_reg *new_mem)
  615. {
  616. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  617. struct ttm_placement placement;
  618. struct ttm_mem_reg tmp_mem;
  619. int ret;
  620. placement.fpfn = placement.lpfn = 0;
  621. placement.num_placement = placement.num_busy_placement = 1;
  622. placement.placement = placement.busy_placement = &placement_memtype;
  623. tmp_mem = *new_mem;
  624. tmp_mem.mm_node = NULL;
  625. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
  626. if (ret)
  627. return ret;
  628. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
  629. if (ret)
  630. goto out;
  631. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  632. if (ret)
  633. goto out;
  634. out:
  635. if (tmp_mem.mm_node) {
  636. spin_lock(&bo->bdev->glob->lru_lock);
  637. drm_mm_put_block(tmp_mem.mm_node);
  638. spin_unlock(&bo->bdev->glob->lru_lock);
  639. }
  640. return ret;
  641. }
  642. static int
  643. nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
  644. struct nouveau_tile_reg **new_tile)
  645. {
  646. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  647. struct drm_device *dev = dev_priv->dev;
  648. struct nouveau_bo *nvbo = nouveau_bo(bo);
  649. uint64_t offset;
  650. int ret;
  651. if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
  652. /* Nothing to do. */
  653. *new_tile = NULL;
  654. return 0;
  655. }
  656. offset = new_mem->mm_node->start << PAGE_SHIFT;
  657. if (dev_priv->card_type == NV_50) {
  658. ret = nv50_mem_vm_bind_linear(dev,
  659. offset + dev_priv->vm_vram_base,
  660. new_mem->size, nvbo->tile_flags,
  661. offset);
  662. if (ret)
  663. return ret;
  664. } else if (dev_priv->card_type >= NV_10) {
  665. *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
  666. nvbo->tile_mode);
  667. }
  668. return 0;
  669. }
  670. static void
  671. nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
  672. struct nouveau_tile_reg *new_tile,
  673. struct nouveau_tile_reg **old_tile)
  674. {
  675. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  676. struct drm_device *dev = dev_priv->dev;
  677. if (dev_priv->card_type >= NV_10 &&
  678. dev_priv->card_type < NV_50) {
  679. if (*old_tile)
  680. nv10_mem_expire_tiling(dev, *old_tile, bo->sync_obj);
  681. *old_tile = new_tile;
  682. }
  683. }
  684. static int
  685. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  686. bool no_wait_reserve, bool no_wait_gpu,
  687. struct ttm_mem_reg *new_mem)
  688. {
  689. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  690. struct nouveau_bo *nvbo = nouveau_bo(bo);
  691. struct ttm_mem_reg *old_mem = &bo->mem;
  692. struct nouveau_tile_reg *new_tile = NULL;
  693. int ret = 0;
  694. ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
  695. if (ret)
  696. return ret;
  697. /* Fake bo copy. */
  698. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  699. BUG_ON(bo->mem.mm_node != NULL);
  700. bo->mem = *new_mem;
  701. new_mem->mm_node = NULL;
  702. goto out;
  703. }
  704. /* Software copy if the card isn't up and running yet. */
  705. if (!dev_priv->channel) {
  706. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  707. goto out;
  708. }
  709. /* Hardware assisted copy. */
  710. if (new_mem->mem_type == TTM_PL_SYSTEM)
  711. ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  712. else if (old_mem->mem_type == TTM_PL_SYSTEM)
  713. ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  714. else
  715. ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
  716. if (!ret)
  717. goto out;
  718. /* Fallback to software copy. */
  719. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
  720. out:
  721. if (ret)
  722. nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
  723. else
  724. nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
  725. return ret;
  726. }
  727. static int
  728. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  729. {
  730. return 0;
  731. }
  732. static int
  733. nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  734. {
  735. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  736. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  737. struct drm_device *dev = dev_priv->dev;
  738. mem->bus.addr = NULL;
  739. mem->bus.offset = 0;
  740. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  741. mem->bus.base = 0;
  742. mem->bus.is_iomem = false;
  743. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  744. return -EINVAL;
  745. switch (mem->mem_type) {
  746. case TTM_PL_SYSTEM:
  747. /* System memory */
  748. return 0;
  749. case TTM_PL_TT:
  750. #if __OS_HAS_AGP
  751. if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
  752. mem->bus.offset = mem->mm_node->start << PAGE_SHIFT;
  753. mem->bus.base = dev_priv->gart_info.aper_base;
  754. mem->bus.is_iomem = true;
  755. }
  756. #endif
  757. break;
  758. case TTM_PL_VRAM:
  759. mem->bus.offset = mem->mm_node->start << PAGE_SHIFT;
  760. mem->bus.base = pci_resource_start(dev->pdev, 1);
  761. mem->bus.is_iomem = true;
  762. break;
  763. default:
  764. return -EINVAL;
  765. }
  766. return 0;
  767. }
  768. static void
  769. nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  770. {
  771. }
  772. static int
  773. nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  774. {
  775. return 0;
  776. }
  777. struct ttm_bo_driver nouveau_bo_driver = {
  778. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  779. .invalidate_caches = nouveau_bo_invalidate_caches,
  780. .init_mem_type = nouveau_bo_init_mem_type,
  781. .evict_flags = nouveau_bo_evict_flags,
  782. .move = nouveau_bo_move,
  783. .verify_access = nouveau_bo_verify_access,
  784. .sync_obj_signaled = nouveau_fence_signalled,
  785. .sync_obj_wait = nouveau_fence_wait,
  786. .sync_obj_flush = nouveau_fence_flush,
  787. .sync_obj_unref = nouveau_fence_unref,
  788. .sync_obj_ref = nouveau_fence_ref,
  789. .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
  790. .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
  791. .io_mem_free = &nouveau_ttm_io_mem_free,
  792. };