nouveau_bo.c 20 KB

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