nouveau_bo.c 18 KB

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