nouveau_bo.c 19 KB

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