ttm_bo_util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #include "ttm/ttm_bo_driver.h"
  31. #include "ttm/ttm_placement.h"
  32. #include <linux/io.h>
  33. #include <linux/highmem.h>
  34. #include <linux/wait.h>
  35. #include <linux/slab.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/module.h>
  38. void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
  39. {
  40. ttm_bo_mem_put(bo, &bo->mem);
  41. }
  42. int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  43. bool evict, bool no_wait_reserve,
  44. bool no_wait_gpu, struct ttm_mem_reg *new_mem)
  45. {
  46. struct ttm_tt *ttm = bo->ttm;
  47. struct ttm_mem_reg *old_mem = &bo->mem;
  48. int ret;
  49. if (old_mem->mem_type != TTM_PL_SYSTEM) {
  50. ttm_tt_unbind(ttm);
  51. ttm_bo_free_old_node(bo);
  52. ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
  53. TTM_PL_MASK_MEM);
  54. old_mem->mem_type = TTM_PL_SYSTEM;
  55. }
  56. ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
  57. if (unlikely(ret != 0))
  58. return ret;
  59. if (new_mem->mem_type != TTM_PL_SYSTEM) {
  60. ret = ttm_tt_bind(ttm, new_mem);
  61. if (unlikely(ret != 0))
  62. return ret;
  63. }
  64. *old_mem = *new_mem;
  65. new_mem->mm_node = NULL;
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(ttm_bo_move_ttm);
  69. int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
  70. {
  71. if (likely(man->io_reserve_fastpath))
  72. return 0;
  73. if (interruptible)
  74. return mutex_lock_interruptible(&man->io_reserve_mutex);
  75. mutex_lock(&man->io_reserve_mutex);
  76. return 0;
  77. }
  78. void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
  79. {
  80. if (likely(man->io_reserve_fastpath))
  81. return;
  82. mutex_unlock(&man->io_reserve_mutex);
  83. }
  84. static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
  85. {
  86. struct ttm_buffer_object *bo;
  87. if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
  88. return -EAGAIN;
  89. bo = list_first_entry(&man->io_reserve_lru,
  90. struct ttm_buffer_object,
  91. io_reserve_lru);
  92. list_del_init(&bo->io_reserve_lru);
  93. ttm_bo_unmap_virtual_locked(bo);
  94. return 0;
  95. }
  96. static int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  97. struct ttm_mem_reg *mem)
  98. {
  99. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  100. int ret = 0;
  101. if (!bdev->driver->io_mem_reserve)
  102. return 0;
  103. if (likely(man->io_reserve_fastpath))
  104. return bdev->driver->io_mem_reserve(bdev, mem);
  105. if (bdev->driver->io_mem_reserve &&
  106. mem->bus.io_reserved_count++ == 0) {
  107. retry:
  108. ret = bdev->driver->io_mem_reserve(bdev, mem);
  109. if (ret == -EAGAIN) {
  110. ret = ttm_mem_io_evict(man);
  111. if (ret == 0)
  112. goto retry;
  113. }
  114. }
  115. return ret;
  116. }
  117. static void ttm_mem_io_free(struct ttm_bo_device *bdev,
  118. struct ttm_mem_reg *mem)
  119. {
  120. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  121. if (likely(man->io_reserve_fastpath))
  122. return;
  123. if (bdev->driver->io_mem_reserve &&
  124. --mem->bus.io_reserved_count == 0 &&
  125. bdev->driver->io_mem_free)
  126. bdev->driver->io_mem_free(bdev, mem);
  127. }
  128. int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
  129. {
  130. struct ttm_mem_reg *mem = &bo->mem;
  131. int ret;
  132. if (!mem->bus.io_reserved_vm) {
  133. struct ttm_mem_type_manager *man =
  134. &bo->bdev->man[mem->mem_type];
  135. ret = ttm_mem_io_reserve(bo->bdev, mem);
  136. if (unlikely(ret != 0))
  137. return ret;
  138. mem->bus.io_reserved_vm = true;
  139. if (man->use_io_reserve_lru)
  140. list_add_tail(&bo->io_reserve_lru,
  141. &man->io_reserve_lru);
  142. }
  143. return 0;
  144. }
  145. void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
  146. {
  147. struct ttm_mem_reg *mem = &bo->mem;
  148. if (mem->bus.io_reserved_vm) {
  149. mem->bus.io_reserved_vm = false;
  150. list_del_init(&bo->io_reserve_lru);
  151. ttm_mem_io_free(bo->bdev, mem);
  152. }
  153. }
  154. int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
  155. void **virtual)
  156. {
  157. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  158. int ret;
  159. void *addr;
  160. *virtual = NULL;
  161. (void) ttm_mem_io_lock(man, false);
  162. ret = ttm_mem_io_reserve(bdev, mem);
  163. ttm_mem_io_unlock(man);
  164. if (ret || !mem->bus.is_iomem)
  165. return ret;
  166. if (mem->bus.addr) {
  167. addr = mem->bus.addr;
  168. } else {
  169. if (mem->placement & TTM_PL_FLAG_WC)
  170. addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
  171. else
  172. addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
  173. if (!addr) {
  174. (void) ttm_mem_io_lock(man, false);
  175. ttm_mem_io_free(bdev, mem);
  176. ttm_mem_io_unlock(man);
  177. return -ENOMEM;
  178. }
  179. }
  180. *virtual = addr;
  181. return 0;
  182. }
  183. void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
  184. void *virtual)
  185. {
  186. struct ttm_mem_type_manager *man;
  187. man = &bdev->man[mem->mem_type];
  188. if (virtual && mem->bus.addr == NULL)
  189. iounmap(virtual);
  190. (void) ttm_mem_io_lock(man, false);
  191. ttm_mem_io_free(bdev, mem);
  192. ttm_mem_io_unlock(man);
  193. }
  194. static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
  195. {
  196. uint32_t *dstP =
  197. (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
  198. uint32_t *srcP =
  199. (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
  200. int i;
  201. for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
  202. iowrite32(ioread32(srcP++), dstP++);
  203. return 0;
  204. }
  205. static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
  206. unsigned long page,
  207. pgprot_t prot)
  208. {
  209. struct page *d = ttm->pages[page];
  210. void *dst;
  211. if (!d)
  212. return -ENOMEM;
  213. src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
  214. #ifdef CONFIG_X86
  215. dst = kmap_atomic_prot(d, prot);
  216. #else
  217. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  218. dst = vmap(&d, 1, 0, prot);
  219. else
  220. dst = kmap(d);
  221. #endif
  222. if (!dst)
  223. return -ENOMEM;
  224. memcpy_fromio(dst, src, PAGE_SIZE);
  225. #ifdef CONFIG_X86
  226. kunmap_atomic(dst);
  227. #else
  228. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  229. vunmap(dst);
  230. else
  231. kunmap(d);
  232. #endif
  233. return 0;
  234. }
  235. static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
  236. unsigned long page,
  237. pgprot_t prot)
  238. {
  239. struct page *s = ttm->pages[page];
  240. void *src;
  241. if (!s)
  242. return -ENOMEM;
  243. dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
  244. #ifdef CONFIG_X86
  245. src = kmap_atomic_prot(s, prot);
  246. #else
  247. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  248. src = vmap(&s, 1, 0, prot);
  249. else
  250. src = kmap(s);
  251. #endif
  252. if (!src)
  253. return -ENOMEM;
  254. memcpy_toio(dst, src, PAGE_SIZE);
  255. #ifdef CONFIG_X86
  256. kunmap_atomic(src);
  257. #else
  258. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  259. vunmap(src);
  260. else
  261. kunmap(s);
  262. #endif
  263. return 0;
  264. }
  265. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  266. bool evict, bool no_wait_reserve, bool no_wait_gpu,
  267. struct ttm_mem_reg *new_mem)
  268. {
  269. struct ttm_bo_device *bdev = bo->bdev;
  270. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  271. struct ttm_tt *ttm = bo->ttm;
  272. struct ttm_mem_reg *old_mem = &bo->mem;
  273. struct ttm_mem_reg old_copy = *old_mem;
  274. void *old_iomap;
  275. void *new_iomap;
  276. int ret;
  277. unsigned long i;
  278. unsigned long page;
  279. unsigned long add = 0;
  280. int dir;
  281. ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
  282. if (ret)
  283. return ret;
  284. ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
  285. if (ret)
  286. goto out;
  287. if (old_iomap == NULL && new_iomap == NULL)
  288. goto out2;
  289. if (old_iomap == NULL && ttm == NULL)
  290. goto out2;
  291. if (ttm->state == tt_unpopulated) {
  292. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  293. if (ret)
  294. goto out1;
  295. }
  296. add = 0;
  297. dir = 1;
  298. if ((old_mem->mem_type == new_mem->mem_type) &&
  299. (new_mem->start < old_mem->start + old_mem->size)) {
  300. dir = -1;
  301. add = new_mem->num_pages - 1;
  302. }
  303. for (i = 0; i < new_mem->num_pages; ++i) {
  304. page = i * dir + add;
  305. if (old_iomap == NULL) {
  306. pgprot_t prot = ttm_io_prot(old_mem->placement,
  307. PAGE_KERNEL);
  308. ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
  309. prot);
  310. } else if (new_iomap == NULL) {
  311. pgprot_t prot = ttm_io_prot(new_mem->placement,
  312. PAGE_KERNEL);
  313. ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
  314. prot);
  315. } else
  316. ret = ttm_copy_io_page(new_iomap, old_iomap, page);
  317. if (ret)
  318. goto out1;
  319. }
  320. mb();
  321. out2:
  322. old_copy = *old_mem;
  323. *old_mem = *new_mem;
  324. new_mem->mm_node = NULL;
  325. if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
  326. ttm_tt_unbind(ttm);
  327. ttm_tt_destroy(ttm);
  328. bo->ttm = NULL;
  329. }
  330. out1:
  331. ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
  332. out:
  333. ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
  334. ttm_bo_mem_put(bo, &old_copy);
  335. return ret;
  336. }
  337. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  338. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  339. {
  340. kfree(bo);
  341. }
  342. /**
  343. * ttm_buffer_object_transfer
  344. *
  345. * @bo: A pointer to a struct ttm_buffer_object.
  346. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  347. * holding the data of @bo with the old placement.
  348. *
  349. * This is a utility function that may be called after an accelerated move
  350. * has been scheduled. A new buffer object is created as a placeholder for
  351. * the old data while it's being copied. When that buffer object is idle,
  352. * it can be destroyed, releasing the space of the old placement.
  353. * Returns:
  354. * !0: Failure.
  355. */
  356. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  357. struct ttm_buffer_object **new_obj)
  358. {
  359. struct ttm_buffer_object *fbo;
  360. struct ttm_bo_device *bdev = bo->bdev;
  361. struct ttm_bo_driver *driver = bdev->driver;
  362. fbo = kzalloc(sizeof(*fbo), GFP_KERNEL);
  363. if (!fbo)
  364. return -ENOMEM;
  365. *fbo = *bo;
  366. /**
  367. * Fix up members that we shouldn't copy directly:
  368. * TODO: Explicit member copy would probably be better here.
  369. */
  370. init_waitqueue_head(&fbo->event_queue);
  371. INIT_LIST_HEAD(&fbo->ddestroy);
  372. INIT_LIST_HEAD(&fbo->lru);
  373. INIT_LIST_HEAD(&fbo->swap);
  374. INIT_LIST_HEAD(&fbo->io_reserve_lru);
  375. fbo->vm_node = NULL;
  376. atomic_set(&fbo->cpu_writers, 0);
  377. fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
  378. kref_init(&fbo->list_kref);
  379. kref_init(&fbo->kref);
  380. fbo->destroy = &ttm_transfered_destroy;
  381. fbo->acc_size = 0;
  382. *new_obj = fbo;
  383. return 0;
  384. }
  385. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  386. {
  387. #if defined(__i386__) || defined(__x86_64__)
  388. if (caching_flags & TTM_PL_FLAG_WC)
  389. tmp = pgprot_writecombine(tmp);
  390. else if (boot_cpu_data.x86 > 3)
  391. tmp = pgprot_noncached(tmp);
  392. #elif defined(__powerpc__)
  393. if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
  394. pgprot_val(tmp) |= _PAGE_NO_CACHE;
  395. if (caching_flags & TTM_PL_FLAG_UNCACHED)
  396. pgprot_val(tmp) |= _PAGE_GUARDED;
  397. }
  398. #endif
  399. #if defined(__ia64__)
  400. if (caching_flags & TTM_PL_FLAG_WC)
  401. tmp = pgprot_writecombine(tmp);
  402. else
  403. tmp = pgprot_noncached(tmp);
  404. #endif
  405. #if defined(__sparc__)
  406. if (!(caching_flags & TTM_PL_FLAG_CACHED))
  407. tmp = pgprot_noncached(tmp);
  408. #endif
  409. return tmp;
  410. }
  411. EXPORT_SYMBOL(ttm_io_prot);
  412. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  413. unsigned long offset,
  414. unsigned long size,
  415. struct ttm_bo_kmap_obj *map)
  416. {
  417. struct ttm_mem_reg *mem = &bo->mem;
  418. if (bo->mem.bus.addr) {
  419. map->bo_kmap_type = ttm_bo_map_premapped;
  420. map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
  421. } else {
  422. map->bo_kmap_type = ttm_bo_map_iomap;
  423. if (mem->placement & TTM_PL_FLAG_WC)
  424. map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
  425. size);
  426. else
  427. map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
  428. size);
  429. }
  430. return (!map->virtual) ? -ENOMEM : 0;
  431. }
  432. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  433. unsigned long start_page,
  434. unsigned long num_pages,
  435. struct ttm_bo_kmap_obj *map)
  436. {
  437. struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
  438. struct ttm_tt *ttm = bo->ttm;
  439. int ret;
  440. BUG_ON(!ttm);
  441. if (ttm->state == tt_unpopulated) {
  442. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  443. if (ret)
  444. return ret;
  445. }
  446. if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
  447. /*
  448. * We're mapping a single page, and the desired
  449. * page protection is consistent with the bo.
  450. */
  451. map->bo_kmap_type = ttm_bo_map_kmap;
  452. map->page = ttm->pages[start_page];
  453. map->virtual = kmap(map->page);
  454. } else {
  455. /*
  456. * We need to use vmap to get the desired page protection
  457. * or to make the buffer object look contiguous.
  458. */
  459. prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
  460. PAGE_KERNEL :
  461. ttm_io_prot(mem->placement, PAGE_KERNEL);
  462. map->bo_kmap_type = ttm_bo_map_vmap;
  463. map->virtual = vmap(ttm->pages + start_page, num_pages,
  464. 0, prot);
  465. }
  466. return (!map->virtual) ? -ENOMEM : 0;
  467. }
  468. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  469. unsigned long start_page, unsigned long num_pages,
  470. struct ttm_bo_kmap_obj *map)
  471. {
  472. struct ttm_mem_type_manager *man =
  473. &bo->bdev->man[bo->mem.mem_type];
  474. unsigned long offset, size;
  475. int ret;
  476. BUG_ON(!list_empty(&bo->swap));
  477. map->virtual = NULL;
  478. map->bo = bo;
  479. if (num_pages > bo->num_pages)
  480. return -EINVAL;
  481. if (start_page > bo->num_pages)
  482. return -EINVAL;
  483. #if 0
  484. if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
  485. return -EPERM;
  486. #endif
  487. (void) ttm_mem_io_lock(man, false);
  488. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  489. ttm_mem_io_unlock(man);
  490. if (ret)
  491. return ret;
  492. if (!bo->mem.bus.is_iomem) {
  493. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  494. } else {
  495. offset = start_page << PAGE_SHIFT;
  496. size = num_pages << PAGE_SHIFT;
  497. return ttm_bo_ioremap(bo, offset, size, map);
  498. }
  499. }
  500. EXPORT_SYMBOL(ttm_bo_kmap);
  501. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  502. {
  503. struct ttm_buffer_object *bo = map->bo;
  504. struct ttm_mem_type_manager *man =
  505. &bo->bdev->man[bo->mem.mem_type];
  506. if (!map->virtual)
  507. return;
  508. switch (map->bo_kmap_type) {
  509. case ttm_bo_map_iomap:
  510. iounmap(map->virtual);
  511. break;
  512. case ttm_bo_map_vmap:
  513. vunmap(map->virtual);
  514. break;
  515. case ttm_bo_map_kmap:
  516. kunmap(map->page);
  517. break;
  518. case ttm_bo_map_premapped:
  519. break;
  520. default:
  521. BUG();
  522. }
  523. (void) ttm_mem_io_lock(man, false);
  524. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  525. ttm_mem_io_unlock(man);
  526. map->virtual = NULL;
  527. map->page = NULL;
  528. }
  529. EXPORT_SYMBOL(ttm_bo_kunmap);
  530. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  531. void *sync_obj,
  532. void *sync_obj_arg,
  533. bool evict, bool no_wait_reserve,
  534. bool no_wait_gpu,
  535. struct ttm_mem_reg *new_mem)
  536. {
  537. struct ttm_bo_device *bdev = bo->bdev;
  538. struct ttm_bo_driver *driver = bdev->driver;
  539. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  540. struct ttm_mem_reg *old_mem = &bo->mem;
  541. int ret;
  542. struct ttm_buffer_object *ghost_obj;
  543. void *tmp_obj = NULL;
  544. spin_lock(&bdev->fence_lock);
  545. if (bo->sync_obj) {
  546. tmp_obj = bo->sync_obj;
  547. bo->sync_obj = NULL;
  548. }
  549. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  550. bo->sync_obj_arg = sync_obj_arg;
  551. if (evict) {
  552. ret = ttm_bo_wait(bo, false, false, false);
  553. spin_unlock(&bdev->fence_lock);
  554. if (tmp_obj)
  555. driver->sync_obj_unref(&tmp_obj);
  556. if (ret)
  557. return ret;
  558. if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  559. (bo->ttm != NULL)) {
  560. ttm_tt_unbind(bo->ttm);
  561. ttm_tt_destroy(bo->ttm);
  562. bo->ttm = NULL;
  563. }
  564. ttm_bo_free_old_node(bo);
  565. } else {
  566. /**
  567. * This should help pipeline ordinary buffer moves.
  568. *
  569. * Hang old buffer memory on a new buffer object,
  570. * and leave it to be released when the GPU
  571. * operation has completed.
  572. */
  573. set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  574. spin_unlock(&bdev->fence_lock);
  575. if (tmp_obj)
  576. driver->sync_obj_unref(&tmp_obj);
  577. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  578. if (ret)
  579. return ret;
  580. /**
  581. * If we're not moving to fixed memory, the TTM object
  582. * needs to stay alive. Otherwhise hang it on the ghost
  583. * bo to be unbound and destroyed.
  584. */
  585. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  586. ghost_obj->ttm = NULL;
  587. else
  588. bo->ttm = NULL;
  589. ttm_bo_unreserve(ghost_obj);
  590. ttm_bo_unref(&ghost_obj);
  591. }
  592. *old_mem = *new_mem;
  593. new_mem->mm_node = NULL;
  594. return 0;
  595. }
  596. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);