nouveau_bo.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. }
  142. int
  143. nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
  144. {
  145. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  146. struct ttm_buffer_object *bo = &nvbo->bo;
  147. int ret, i;
  148. if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
  149. NV_ERROR(nouveau_bdev(bo->bdev)->dev,
  150. "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
  151. 1 << bo->mem.mem_type, memtype);
  152. return -EINVAL;
  153. }
  154. if (nvbo->pin_refcnt++)
  155. return 0;
  156. ret = ttm_bo_reserve(bo, false, false, false, 0);
  157. if (ret)
  158. goto out;
  159. nouveau_bo_placement_set(nvbo, memtype);
  160. for (i = 0; i < nvbo->placement.num_placement; i++)
  161. nvbo->placements[i] |= TTM_PL_FLAG_NO_EVICT;
  162. ret = ttm_bo_validate(bo, &nvbo->placement, false, false);
  163. if (ret == 0) {
  164. switch (bo->mem.mem_type) {
  165. case TTM_PL_VRAM:
  166. dev_priv->fb_aper_free -= bo->mem.size;
  167. break;
  168. case TTM_PL_TT:
  169. dev_priv->gart_info.aper_free -= bo->mem.size;
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. ttm_bo_unreserve(bo);
  176. out:
  177. if (unlikely(ret))
  178. nvbo->pin_refcnt--;
  179. return ret;
  180. }
  181. int
  182. nouveau_bo_unpin(struct nouveau_bo *nvbo)
  183. {
  184. struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
  185. struct ttm_buffer_object *bo = &nvbo->bo;
  186. int ret, i;
  187. if (--nvbo->pin_refcnt)
  188. return 0;
  189. ret = ttm_bo_reserve(bo, false, false, false, 0);
  190. if (ret)
  191. return ret;
  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. return ret;
  209. }
  210. int
  211. nouveau_bo_map(struct nouveau_bo *nvbo)
  212. {
  213. int ret;
  214. ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
  215. if (ret)
  216. return ret;
  217. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
  218. ttm_bo_unreserve(&nvbo->bo);
  219. return ret;
  220. }
  221. void
  222. nouveau_bo_unmap(struct nouveau_bo *nvbo)
  223. {
  224. ttm_bo_kunmap(&nvbo->kmap);
  225. }
  226. u16
  227. nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
  228. {
  229. bool is_iomem;
  230. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  231. mem = &mem[index];
  232. if (is_iomem)
  233. return ioread16_native((void __force __iomem *)mem);
  234. else
  235. return *mem;
  236. }
  237. void
  238. nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
  239. {
  240. bool is_iomem;
  241. u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  242. mem = &mem[index];
  243. if (is_iomem)
  244. iowrite16_native(val, (void __force __iomem *)mem);
  245. else
  246. *mem = val;
  247. }
  248. u32
  249. nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
  250. {
  251. bool is_iomem;
  252. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  253. mem = &mem[index];
  254. if (is_iomem)
  255. return ioread32_native((void __force __iomem *)mem);
  256. else
  257. return *mem;
  258. }
  259. void
  260. nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
  261. {
  262. bool is_iomem;
  263. u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
  264. mem = &mem[index];
  265. if (is_iomem)
  266. iowrite32_native(val, (void __force __iomem *)mem);
  267. else
  268. *mem = val;
  269. }
  270. static struct ttm_backend *
  271. nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
  272. {
  273. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  274. struct drm_device *dev = dev_priv->dev;
  275. switch (dev_priv->gart_info.type) {
  276. case NOUVEAU_GART_AGP:
  277. return ttm_agp_backend_init(bdev, dev->agp->bridge);
  278. case NOUVEAU_GART_SGDMA:
  279. return nouveau_sgdma_init_ttm(dev);
  280. default:
  281. NV_ERROR(dev, "Unknown GART type %d\n",
  282. dev_priv->gart_info.type);
  283. break;
  284. }
  285. return NULL;
  286. }
  287. static int
  288. nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  289. {
  290. /* We'll do this from user space. */
  291. return 0;
  292. }
  293. static int
  294. nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  295. struct ttm_mem_type_manager *man)
  296. {
  297. struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
  298. struct drm_device *dev = dev_priv->dev;
  299. switch (type) {
  300. case TTM_PL_SYSTEM:
  301. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  302. man->available_caching = TTM_PL_MASK_CACHING;
  303. man->default_caching = TTM_PL_FLAG_CACHED;
  304. break;
  305. case TTM_PL_VRAM:
  306. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  307. TTM_MEMTYPE_FLAG_MAPPABLE |
  308. TTM_MEMTYPE_FLAG_NEEDS_IOREMAP;
  309. man->available_caching = TTM_PL_FLAG_UNCACHED |
  310. TTM_PL_FLAG_WC;
  311. man->default_caching = TTM_PL_FLAG_WC;
  312. man->io_addr = NULL;
  313. man->io_offset = drm_get_resource_start(dev, 1);
  314. man->io_size = drm_get_resource_len(dev, 1);
  315. if (man->io_size > nouveau_mem_fb_amount(dev))
  316. man->io_size = nouveau_mem_fb_amount(dev);
  317. man->gpu_offset = dev_priv->vm_vram_base;
  318. break;
  319. case TTM_PL_TT:
  320. switch (dev_priv->gart_info.type) {
  321. case NOUVEAU_GART_AGP:
  322. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  323. TTM_MEMTYPE_FLAG_NEEDS_IOREMAP;
  324. man->available_caching = TTM_PL_FLAG_UNCACHED;
  325. man->default_caching = TTM_PL_FLAG_UNCACHED;
  326. break;
  327. case NOUVEAU_GART_SGDMA:
  328. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
  329. TTM_MEMTYPE_FLAG_CMA;
  330. man->available_caching = TTM_PL_MASK_CACHING;
  331. man->default_caching = TTM_PL_FLAG_CACHED;
  332. break;
  333. default:
  334. NV_ERROR(dev, "Unknown GART type: %d\n",
  335. dev_priv->gart_info.type);
  336. return -EINVAL;
  337. }
  338. man->io_offset = dev_priv->gart_info.aper_base;
  339. man->io_size = dev_priv->gart_info.aper_size;
  340. man->io_addr = NULL;
  341. man->gpu_offset = dev_priv->vm_gart_base;
  342. break;
  343. default:
  344. NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
  345. return -EINVAL;
  346. }
  347. return 0;
  348. }
  349. static void
  350. nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
  351. {
  352. struct nouveau_bo *nvbo = nouveau_bo(bo);
  353. switch (bo->mem.mem_type) {
  354. default:
  355. nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM);
  356. break;
  357. }
  358. }
  359. /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
  360. * TTM_PL_{VRAM,TT} directly.
  361. */
  362. static int
  363. nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
  364. struct nouveau_bo *nvbo, bool evict, bool no_wait,
  365. struct ttm_mem_reg *new_mem)
  366. {
  367. struct nouveau_fence *fence = NULL;
  368. int ret;
  369. ret = nouveau_fence_new(chan, &fence, true);
  370. if (ret)
  371. return ret;
  372. ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL,
  373. evict, no_wait, new_mem);
  374. nouveau_fence_unref((void *)&fence);
  375. return ret;
  376. }
  377. static inline uint32_t
  378. nouveau_bo_mem_ctxdma(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
  379. struct ttm_mem_reg *mem)
  380. {
  381. if (chan == nouveau_bdev(nvbo->bo.bdev)->channel) {
  382. if (mem->mem_type == TTM_PL_TT)
  383. return NvDmaGART;
  384. return NvDmaVRAM;
  385. }
  386. if (mem->mem_type == TTM_PL_TT)
  387. return chan->gart_handle;
  388. return chan->vram_handle;
  389. }
  390. static int
  391. nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, int no_wait,
  392. struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
  393. {
  394. struct nouveau_bo *nvbo = nouveau_bo(bo);
  395. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  396. struct nouveau_channel *chan;
  397. uint64_t src_offset, dst_offset;
  398. uint32_t page_count;
  399. int ret;
  400. chan = nvbo->channel;
  401. if (!chan || nvbo->tile_flags || nvbo->no_vm) {
  402. chan = dev_priv->channel;
  403. if (!chan)
  404. return -EINVAL;
  405. }
  406. src_offset = old_mem->mm_node->start << PAGE_SHIFT;
  407. dst_offset = new_mem->mm_node->start << PAGE_SHIFT;
  408. if (chan != dev_priv->channel) {
  409. if (old_mem->mem_type == TTM_PL_TT)
  410. src_offset += dev_priv->vm_gart_base;
  411. else
  412. src_offset += dev_priv->vm_vram_base;
  413. if (new_mem->mem_type == TTM_PL_TT)
  414. dst_offset += dev_priv->vm_gart_base;
  415. else
  416. dst_offset += dev_priv->vm_vram_base;
  417. }
  418. ret = RING_SPACE(chan, 3);
  419. if (ret)
  420. return ret;
  421. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
  422. OUT_RING(chan, nouveau_bo_mem_ctxdma(nvbo, chan, old_mem));
  423. OUT_RING(chan, nouveau_bo_mem_ctxdma(nvbo, chan, new_mem));
  424. if (dev_priv->card_type >= NV_50) {
  425. ret = RING_SPACE(chan, 4);
  426. if (ret)
  427. return ret;
  428. BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
  429. OUT_RING(chan, 1);
  430. BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
  431. OUT_RING(chan, 1);
  432. }
  433. page_count = new_mem->num_pages;
  434. while (page_count) {
  435. int line_count = (page_count > 2047) ? 2047 : page_count;
  436. if (dev_priv->card_type >= NV_50) {
  437. ret = RING_SPACE(chan, 3);
  438. if (ret)
  439. return ret;
  440. BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
  441. OUT_RING(chan, upper_32_bits(src_offset));
  442. OUT_RING(chan, upper_32_bits(dst_offset));
  443. }
  444. ret = RING_SPACE(chan, 11);
  445. if (ret)
  446. return ret;
  447. BEGIN_RING(chan, NvSubM2MF,
  448. NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
  449. OUT_RING(chan, lower_32_bits(src_offset));
  450. OUT_RING(chan, lower_32_bits(dst_offset));
  451. OUT_RING(chan, PAGE_SIZE); /* src_pitch */
  452. OUT_RING(chan, PAGE_SIZE); /* dst_pitch */
  453. OUT_RING(chan, PAGE_SIZE); /* line_length */
  454. OUT_RING(chan, line_count);
  455. OUT_RING(chan, (1<<8)|(1<<0));
  456. OUT_RING(chan, 0);
  457. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
  458. OUT_RING(chan, 0);
  459. page_count -= line_count;
  460. src_offset += (PAGE_SIZE * line_count);
  461. dst_offset += (PAGE_SIZE * line_count);
  462. }
  463. return nouveau_bo_move_accel_cleanup(chan, nvbo, evict, no_wait, new_mem);
  464. }
  465. static int
  466. nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
  467. bool no_wait, struct ttm_mem_reg *new_mem)
  468. {
  469. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  470. struct ttm_placement placement;
  471. struct ttm_mem_reg tmp_mem;
  472. int ret;
  473. placement.fpfn = placement.lpfn = 0;
  474. placement.num_placement = placement.num_busy_placement = 1;
  475. placement.placement = &placement_memtype;
  476. tmp_mem = *new_mem;
  477. tmp_mem.mm_node = NULL;
  478. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait);
  479. if (ret)
  480. return ret;
  481. ret = ttm_tt_bind(bo->ttm, &tmp_mem);
  482. if (ret)
  483. goto out;
  484. ret = nouveau_bo_move_m2mf(bo, true, no_wait, &bo->mem, &tmp_mem);
  485. if (ret)
  486. goto out;
  487. ret = ttm_bo_move_ttm(bo, evict, no_wait, new_mem);
  488. out:
  489. if (tmp_mem.mm_node) {
  490. spin_lock(&bo->bdev->glob->lru_lock);
  491. drm_mm_put_block(tmp_mem.mm_node);
  492. spin_unlock(&bo->bdev->glob->lru_lock);
  493. }
  494. return ret;
  495. }
  496. static int
  497. nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
  498. bool no_wait, struct ttm_mem_reg *new_mem)
  499. {
  500. u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
  501. struct ttm_placement placement;
  502. struct ttm_mem_reg tmp_mem;
  503. int ret;
  504. placement.fpfn = placement.lpfn = 0;
  505. placement.num_placement = placement.num_busy_placement = 1;
  506. placement.placement = &placement_memtype;
  507. tmp_mem = *new_mem;
  508. tmp_mem.mm_node = NULL;
  509. ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait);
  510. if (ret)
  511. return ret;
  512. ret = ttm_bo_move_ttm(bo, evict, no_wait, &tmp_mem);
  513. if (ret)
  514. goto out;
  515. ret = nouveau_bo_move_m2mf(bo, true, no_wait, &bo->mem, new_mem);
  516. if (ret)
  517. goto out;
  518. out:
  519. if (tmp_mem.mm_node) {
  520. spin_lock(&bo->bdev->glob->lru_lock);
  521. drm_mm_put_block(tmp_mem.mm_node);
  522. spin_unlock(&bo->bdev->glob->lru_lock);
  523. }
  524. return ret;
  525. }
  526. static int
  527. nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
  528. bool no_wait, struct ttm_mem_reg *new_mem)
  529. {
  530. struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
  531. struct nouveau_bo *nvbo = nouveau_bo(bo);
  532. struct drm_device *dev = dev_priv->dev;
  533. struct ttm_mem_reg *old_mem = &bo->mem;
  534. int ret;
  535. if (dev_priv->card_type == NV_50 && new_mem->mem_type == TTM_PL_VRAM &&
  536. !nvbo->no_vm) {
  537. uint64_t offset = new_mem->mm_node->start << PAGE_SHIFT;
  538. ret = nv50_mem_vm_bind_linear(dev,
  539. offset + dev_priv->vm_vram_base,
  540. new_mem->size, nvbo->tile_flags,
  541. offset);
  542. if (ret)
  543. return ret;
  544. }
  545. if (dev_priv->init_state != NOUVEAU_CARD_INIT_DONE)
  546. return ttm_bo_move_memcpy(bo, evict, no_wait, new_mem);
  547. if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
  548. BUG_ON(bo->mem.mm_node != NULL);
  549. bo->mem = *new_mem;
  550. new_mem->mm_node = NULL;
  551. return 0;
  552. }
  553. if (new_mem->mem_type == TTM_PL_SYSTEM) {
  554. if (old_mem->mem_type == TTM_PL_SYSTEM)
  555. return ttm_bo_move_memcpy(bo, evict, no_wait, new_mem);
  556. if (nouveau_bo_move_flipd(bo, evict, intr, no_wait, new_mem))
  557. return ttm_bo_move_memcpy(bo, evict, no_wait, new_mem);
  558. } else if (old_mem->mem_type == TTM_PL_SYSTEM) {
  559. if (nouveau_bo_move_flips(bo, evict, intr, no_wait, new_mem))
  560. return ttm_bo_move_memcpy(bo, evict, no_wait, new_mem);
  561. } else {
  562. if (nouveau_bo_move_m2mf(bo, evict, no_wait, old_mem, new_mem))
  563. return ttm_bo_move_memcpy(bo, evict, no_wait, new_mem);
  564. }
  565. return 0;
  566. }
  567. static int
  568. nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  569. {
  570. return 0;
  571. }
  572. struct ttm_bo_driver nouveau_bo_driver = {
  573. .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
  574. .invalidate_caches = nouveau_bo_invalidate_caches,
  575. .init_mem_type = nouveau_bo_init_mem_type,
  576. .evict_flags = nouveau_bo_evict_flags,
  577. .move = nouveau_bo_move,
  578. .verify_access = nouveau_bo_verify_access,
  579. .sync_obj_signaled = nouveau_fence_signalled,
  580. .sync_obj_wait = nouveau_fence_wait,
  581. .sync_obj_flush = nouveau_fence_flush,
  582. .sync_obj_unref = nouveau_fence_unref,
  583. .sync_obj_ref = nouveau_fence_ref,
  584. };