nouveau_bo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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, n = 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. if (flags & TTM_PL_FLAG_VRAM)
  130. nvbo->placements[n++] = TTM_PL_FLAG_VRAM | TTM_PL_MASK_CACHING;
  131. if (flags & TTM_PL_FLAG_TT)
  132. nvbo->placements[n++] = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  133. nvbo->placement.fpfn = 0;
  134. nvbo->placement.lpfn = mappable ? dev_priv->fb_mappable_pages : 0;
  135. nvbo->placement.placement = nvbo->placements;
  136. nvbo->placement.busy_placement = nvbo->placements;
  137. nvbo->placement.num_placement = n;
  138. nvbo->placement.num_busy_placement = n;
  139. nvbo->channel = chan;
  140. nouveau_bo_placement_set(nvbo, flags);
  141. ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
  142. ttm_bo_type_device, &nvbo->placement, align, 0,
  143. false, NULL, size, nouveau_bo_del_ttm);
  144. nvbo->channel = NULL;
  145. if (ret) {
  146. /* ttm will call nouveau_bo_del_ttm if it fails.. */
  147. return ret;
  148. }
  149. spin_lock(&dev_priv->ttm.bo_list_lock);
  150. list_add_tail(&nvbo->head, &dev_priv->ttm.bo_list);
  151. spin_unlock(&dev_priv->ttm.bo_list_lock);
  152. *pnvbo = nvbo;
  153. return 0;
  154. }
  155. void
  156. nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t memtype)
  157. {
  158. int n = 0;
  159. if (memtype & TTM_PL_FLAG_VRAM)
  160. nvbo->placements[n++] = TTM_PL_FLAG_VRAM | TTM_PL_MASK_CACHING;
  161. if (memtype & TTM_PL_FLAG_TT)
  162. nvbo->placements[n++] = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  163. if (memtype & TTM_PL_FLAG_SYSTEM)
  164. nvbo->placements[n++] = TTM_PL_FLAG_SYSTEM | TTM_PL_MASK_CACHING;
  165. nvbo->placement.placement = nvbo->placements;
  166. nvbo->placement.busy_placement = nvbo->placements;
  167. nvbo->placement.num_placement = n;
  168. nvbo->placement.num_busy_placement = n;
  169. if (nvbo->pin_refcnt) {
  170. while (n--)
  171. nvbo->placements[n] |= TTM_PL_FLAG_NO_EVICT;
  172. }
  173. }
  174. int
  175. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  176. {
  177. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  178. struct ttm_buffer_object *bo = &nvbo->bo;
  179. int ret, i;
  180. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  181. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  182. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  183. 1 << bo->mem.mem_type, memtype);
  184. return -EINVAL;
  185. }
  186. if (nvbo->pin_refcnt++)
  187. return 0;
  188. ret = ttm_bo_reserve(bo, false, false, false, 0);
  189. if (ret)
  190. goto out;
  191. nouveau_bo_placement_set(nvbo, memtype);
  192. for (i = 0; i < nvbo->placement.num_placement; i++)
  193. nvbo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  194. ret = ttm_bo_validate(bo, &nvbo->placement, false, false);
  195. if (ret == 0) {
  196. switch (bo->mem.mem_type) {
  197. case TTM_PL_VRAM:
  198. dev_priv->fb_aper_free -= bo->mem.size;
  199. break;
  200. case TTM_PL_TT:
  201. dev_priv->gart_info.aper_free -= bo->mem.size;
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. ttm_bo_unreserve(bo);
  208. out:
  209. if (unlikely(ret))
  210. nvbo->pin_refcnt--;
  211. return ret;
  212. }
  213. int
  214. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  215. {
  216. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  217. struct ttm_buffer_object *bo = &nvbo->bo;
  218. int ret, i;
  219. if (--nvbo->pin_refcnt)
  220. return 0;
  221. ret = ttm_bo_reserve(bo, false, false, false, 0);
  222. if (ret)
  223. return ret;
  224. for (i = 0; i < nvbo->placement.num_placement; i++)
  225. nvbo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT;
  226. ret = ttm_bo_validate(bo, &nvbo->placement, false, false);
  227. if (ret == 0) {
  228. switch (bo->mem.mem_type) {
  229. case TTM_PL_VRAM:
  230. dev_priv->fb_aper_free += bo->mem.size;
  231. break;
  232. case TTM_PL_TT:
  233. dev_priv->gart_info.aper_free += bo->mem.size;
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. ttm_bo_unreserve(bo);
  240. return ret;
  241. }
  242. int
  243. nouveau_bo_map(struct nouveau_bo *nvbo)
  244. {
  245. int ret;
  246. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  247. if (ret)
  248. return ret;
  249. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  250. ttm_bo_unreserve(&nvbo->bo);
  251. return ret;
  252. }
  253. void
  254. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  255. {
  256. ttm_bo_kunmap(&nvbo->kmap);
  257. }
  258. u16
  259. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  260. {
  261. bool is_iomem;
  262. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  263. mem = &mem[index];
  264. if (is_iomem)
  265. return ioread16_native((void __force __iomem *)mem);
  266. else
  267. return *mem;
  268. }
  269. void
  270. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  271. {
  272. bool is_iomem;
  273. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  274. mem = &mem[index];
  275. if (is_iomem)
  276. iowrite16_native(val, (void __force __iomem *)mem);
  277. else
  278. *mem = val;
  279. }
  280. u32
  281. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  282. {
  283. bool is_iomem;
  284. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  285. mem = &mem[index];
  286. if (is_iomem)
  287. return ioread32_native((void __force __iomem *)mem);
  288. else
  289. return *mem;
  290. }
  291. void
  292. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  293. {
  294. bool is_iomem;
  295. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  296. mem = &mem[index];
  297. if (is_iomem)
  298. iowrite32_native(val, (void __force __iomem *)mem);
  299. else
  300. *mem = val;
  301. }
  302. static struct ttm_backend *
  303. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  304. {
  305. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  306. struct drm_device *dev = dev_priv->dev;
  307. switch (dev_priv->gart_info.type) {
  308. #if __OS_HAS_AGP
  309. case NOUVEAU_GART_AGP:
  310. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  311. #endif
  312. case NOUVEAU_GART_SGDMA:
  313. return nouveau_sgdma_init_ttm(dev);
  314. default:
  315. NV_ERROR(dev, "Unknown GART type %d\n",
  316. dev_priv->gart_info.type);
  317. break;
  318. }
  319. return NULL;
  320. }
  321. static int
  322. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  323. {
  324. /* We'll do this from user space. */
  325. return 0;
  326. }
  327. static int
  328. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  329. struct ttm_mem_type_manager *man)
  330. {
  331. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  332. struct drm_device *dev = dev_priv->dev;
  333. switch (type) {
  334. case TTM_PL_SYSTEM:
  335. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  336. man->available_caching = TTM_PL_MASK_CACHING;
  337. man->default_caching = TTM_PL_FLAG_CACHED;
  338. break;
  339. case TTM_PL_VRAM:
  340. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  341. TTM_MEMTYPE_FLAG_MAPPABLE |
  342. TTM_MEMTYPE_FLAG_NEEDS_IOREMAP;
  343. man->available_caching = TTM_PL_FLAG_UNCACHED |
  344. TTM_PL_FLAG_WC;
  345. man->default_caching = TTM_PL_FLAG_WC;
  346. man->io_addr = NULL;
  347. man->io_offset = drm_get_resource_start(dev, 1);
  348. man->io_size = drm_get_resource_len(dev, 1);
  349. if (man->io_size > nouveau_mem_fb_amount(dev))
  350. man->io_size = nouveau_mem_fb_amount(dev);
  351. man->gpu_offset = dev_priv->vm_vram_base;
  352. break;
  353. case TTM_PL_TT:
  354. switch (dev_priv->gart_info.type) {
  355. case NOUVEAU_GART_AGP:
  356. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  357. TTM_MEMTYPE_FLAG_NEEDS_IOREMAP;
  358. man->available_caching = TTM_PL_FLAG_UNCACHED;
  359. man->default_caching = TTM_PL_FLAG_UNCACHED;
  360. break;
  361. case NOUVEAU_GART_SGDMA:
  362. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  363. TTM_MEMTYPE_FLAG_CMA;
  364. man->available_caching = TTM_PL_MASK_CACHING;
  365. man->default_caching = TTM_PL_FLAG_CACHED;
  366. break;
  367. default:
  368. NV_ERROR(dev, "Unknown GART type: %d\n",
  369. dev_priv->gart_info.type);
  370. return -EINVAL;
  371. }
  372. man->io_offset = dev_priv->gart_info.aper_base;
  373. man->io_size = dev_priv->gart_info.aper_size;
  374. man->io_addr = NULL;
  375. man->gpu_offset = dev_priv->vm_gart_base;
  376. break;
  377. default:
  378. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  379. return -EINVAL;
  380. }
  381. return 0;
  382. }
  383. static void
  384. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  385. {
  386. struct nouveau_bo *nvbo = nouveau_bo(bo);
  387. switch (bo->mem.mem_type) {
  388. case TTM_PL_VRAM:
  389. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT |
  390. TTM_PL_FLAG_SYSTEM);
  391. break;
  392. default:
  393. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM);
  394. break;
  395. }
  396. *pl = nvbo->placement;
  397. }
  398. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  399. * TTM_PL_{VRAM,TT} directly.
  400. */
  401. static int
  402. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  403. struct nouveau_bo *nvbo, bool evict, bool no_wait,
  404. struct ttm_mem_reg *new_mem)
  405. {
  406. struct nouveau_fence *fence = NULL;
  407. int ret;
  408. ret = nouveau_fence_new(chan, &fence, true);
  409. if (ret)
  410. return ret;
  411. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL,
  412. evict, no_wait, new_mem);
  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, true, 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. };