ttm_bo_util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. /* TTM might be null for moves within the same region.
  292. */
  293. if (ttm && ttm->state == tt_unpopulated) {
  294. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  295. if (ret)
  296. goto out1;
  297. }
  298. add = 0;
  299. dir = 1;
  300. if ((old_mem->mem_type == new_mem->mem_type) &&
  301. (new_mem->start < old_mem->start + old_mem->size)) {
  302. dir = -1;
  303. add = new_mem->num_pages - 1;
  304. }
  305. for (i = 0; i < new_mem->num_pages; ++i) {
  306. page = i * dir + add;
  307. if (old_iomap == NULL) {
  308. pgprot_t prot = ttm_io_prot(old_mem->placement,
  309. PAGE_KERNEL);
  310. ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
  311. prot);
  312. } else if (new_iomap == NULL) {
  313. pgprot_t prot = ttm_io_prot(new_mem->placement,
  314. PAGE_KERNEL);
  315. ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
  316. prot);
  317. } else
  318. ret = ttm_copy_io_page(new_iomap, old_iomap, page);
  319. if (ret)
  320. goto out1;
  321. }
  322. mb();
  323. out2:
  324. old_copy = *old_mem;
  325. *old_mem = *new_mem;
  326. new_mem->mm_node = NULL;
  327. if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) {
  328. ttm_tt_unbind(ttm);
  329. ttm_tt_destroy(ttm);
  330. bo->ttm = NULL;
  331. }
  332. out1:
  333. ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
  334. out:
  335. ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
  336. ttm_bo_mem_put(bo, &old_copy);
  337. return ret;
  338. }
  339. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  340. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  341. {
  342. kfree(bo);
  343. }
  344. /**
  345. * ttm_buffer_object_transfer
  346. *
  347. * @bo: A pointer to a struct ttm_buffer_object.
  348. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  349. * holding the data of @bo with the old placement.
  350. *
  351. * This is a utility function that may be called after an accelerated move
  352. * has been scheduled. A new buffer object is created as a placeholder for
  353. * the old data while it's being copied. When that buffer object is idle,
  354. * it can be destroyed, releasing the space of the old placement.
  355. * Returns:
  356. * !0: Failure.
  357. */
  358. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  359. struct ttm_buffer_object **new_obj)
  360. {
  361. struct ttm_buffer_object *fbo;
  362. struct ttm_bo_device *bdev = bo->bdev;
  363. struct ttm_bo_driver *driver = bdev->driver;
  364. fbo = kzalloc(sizeof(*fbo), GFP_KERNEL);
  365. if (!fbo)
  366. return -ENOMEM;
  367. *fbo = *bo;
  368. /**
  369. * Fix up members that we shouldn't copy directly:
  370. * TODO: Explicit member copy would probably be better here.
  371. */
  372. init_waitqueue_head(&fbo->event_queue);
  373. INIT_LIST_HEAD(&fbo->ddestroy);
  374. INIT_LIST_HEAD(&fbo->lru);
  375. INIT_LIST_HEAD(&fbo->swap);
  376. INIT_LIST_HEAD(&fbo->io_reserve_lru);
  377. fbo->vm_node = NULL;
  378. atomic_set(&fbo->cpu_writers, 0);
  379. fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
  380. kref_init(&fbo->list_kref);
  381. kref_init(&fbo->kref);
  382. fbo->destroy = &ttm_transfered_destroy;
  383. fbo->acc_size = 0;
  384. *new_obj = fbo;
  385. return 0;
  386. }
  387. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  388. {
  389. #if defined(__i386__) || defined(__x86_64__)
  390. if (caching_flags & TTM_PL_FLAG_WC)
  391. tmp = pgprot_writecombine(tmp);
  392. else if (boot_cpu_data.x86 > 3)
  393. tmp = pgprot_noncached(tmp);
  394. #elif defined(__powerpc__)
  395. if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
  396. pgprot_val(tmp) |= _PAGE_NO_CACHE;
  397. if (caching_flags & TTM_PL_FLAG_UNCACHED)
  398. pgprot_val(tmp) |= _PAGE_GUARDED;
  399. }
  400. #endif
  401. #if defined(__ia64__)
  402. if (caching_flags & TTM_PL_FLAG_WC)
  403. tmp = pgprot_writecombine(tmp);
  404. else
  405. tmp = pgprot_noncached(tmp);
  406. #endif
  407. #if defined(__sparc__)
  408. if (!(caching_flags & TTM_PL_FLAG_CACHED))
  409. tmp = pgprot_noncached(tmp);
  410. #endif
  411. return tmp;
  412. }
  413. EXPORT_SYMBOL(ttm_io_prot);
  414. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  415. unsigned long offset,
  416. unsigned long size,
  417. struct ttm_bo_kmap_obj *map)
  418. {
  419. struct ttm_mem_reg *mem = &bo->mem;
  420. if (bo->mem.bus.addr) {
  421. map->bo_kmap_type = ttm_bo_map_premapped;
  422. map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
  423. } else {
  424. map->bo_kmap_type = ttm_bo_map_iomap;
  425. if (mem->placement & TTM_PL_FLAG_WC)
  426. map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
  427. size);
  428. else
  429. map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
  430. size);
  431. }
  432. return (!map->virtual) ? -ENOMEM : 0;
  433. }
  434. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  435. unsigned long start_page,
  436. unsigned long num_pages,
  437. struct ttm_bo_kmap_obj *map)
  438. {
  439. struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
  440. struct ttm_tt *ttm = bo->ttm;
  441. int ret;
  442. BUG_ON(!ttm);
  443. if (ttm->state == tt_unpopulated) {
  444. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  445. if (ret)
  446. return ret;
  447. }
  448. if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
  449. /*
  450. * We're mapping a single page, and the desired
  451. * page protection is consistent with the bo.
  452. */
  453. map->bo_kmap_type = ttm_bo_map_kmap;
  454. map->page = ttm->pages[start_page];
  455. map->virtual = kmap(map->page);
  456. } else {
  457. /*
  458. * We need to use vmap to get the desired page protection
  459. * or to make the buffer object look contiguous.
  460. */
  461. prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
  462. PAGE_KERNEL :
  463. ttm_io_prot(mem->placement, PAGE_KERNEL);
  464. map->bo_kmap_type = ttm_bo_map_vmap;
  465. map->virtual = vmap(ttm->pages + start_page, num_pages,
  466. 0, prot);
  467. }
  468. return (!map->virtual) ? -ENOMEM : 0;
  469. }
  470. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  471. unsigned long start_page, unsigned long num_pages,
  472. struct ttm_bo_kmap_obj *map)
  473. {
  474. struct ttm_mem_type_manager *man =
  475. &bo->bdev->man[bo->mem.mem_type];
  476. unsigned long offset, size;
  477. int ret;
  478. BUG_ON(!list_empty(&bo->swap));
  479. map->virtual = NULL;
  480. map->bo = bo;
  481. if (num_pages > bo->num_pages)
  482. return -EINVAL;
  483. if (start_page > bo->num_pages)
  484. return -EINVAL;
  485. #if 0
  486. if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
  487. return -EPERM;
  488. #endif
  489. (void) ttm_mem_io_lock(man, false);
  490. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  491. ttm_mem_io_unlock(man);
  492. if (ret)
  493. return ret;
  494. if (!bo->mem.bus.is_iomem) {
  495. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  496. } else {
  497. offset = start_page << PAGE_SHIFT;
  498. size = num_pages << PAGE_SHIFT;
  499. return ttm_bo_ioremap(bo, offset, size, map);
  500. }
  501. }
  502. EXPORT_SYMBOL(ttm_bo_kmap);
  503. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  504. {
  505. struct ttm_buffer_object *bo = map->bo;
  506. struct ttm_mem_type_manager *man =
  507. &bo->bdev->man[bo->mem.mem_type];
  508. if (!map->virtual)
  509. return;
  510. switch (map->bo_kmap_type) {
  511. case ttm_bo_map_iomap:
  512. iounmap(map->virtual);
  513. break;
  514. case ttm_bo_map_vmap:
  515. vunmap(map->virtual);
  516. break;
  517. case ttm_bo_map_kmap:
  518. kunmap(map->page);
  519. break;
  520. case ttm_bo_map_premapped:
  521. break;
  522. default:
  523. BUG();
  524. }
  525. (void) ttm_mem_io_lock(man, false);
  526. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  527. ttm_mem_io_unlock(man);
  528. map->virtual = NULL;
  529. map->page = NULL;
  530. }
  531. EXPORT_SYMBOL(ttm_bo_kunmap);
  532. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  533. void *sync_obj,
  534. void *sync_obj_arg,
  535. bool evict, bool no_wait_reserve,
  536. bool no_wait_gpu,
  537. struct ttm_mem_reg *new_mem)
  538. {
  539. struct ttm_bo_device *bdev = bo->bdev;
  540. struct ttm_bo_driver *driver = bdev->driver;
  541. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  542. struct ttm_mem_reg *old_mem = &bo->mem;
  543. int ret;
  544. struct ttm_buffer_object *ghost_obj;
  545. void *tmp_obj = NULL;
  546. spin_lock(&bdev->fence_lock);
  547. if (bo->sync_obj) {
  548. tmp_obj = bo->sync_obj;
  549. bo->sync_obj = NULL;
  550. }
  551. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  552. bo->sync_obj_arg = sync_obj_arg;
  553. if (evict) {
  554. ret = ttm_bo_wait(bo, false, false, false);
  555. spin_unlock(&bdev->fence_lock);
  556. if (tmp_obj)
  557. driver->sync_obj_unref(&tmp_obj);
  558. if (ret)
  559. return ret;
  560. if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  561. (bo->ttm != NULL)) {
  562. ttm_tt_unbind(bo->ttm);
  563. ttm_tt_destroy(bo->ttm);
  564. bo->ttm = NULL;
  565. }
  566. ttm_bo_free_old_node(bo);
  567. } else {
  568. /**
  569. * This should help pipeline ordinary buffer moves.
  570. *
  571. * Hang old buffer memory on a new buffer object,
  572. * and leave it to be released when the GPU
  573. * operation has completed.
  574. */
  575. set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  576. spin_unlock(&bdev->fence_lock);
  577. if (tmp_obj)
  578. driver->sync_obj_unref(&tmp_obj);
  579. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  580. if (ret)
  581. return ret;
  582. /**
  583. * If we're not moving to fixed memory, the TTM object
  584. * needs to stay alive. Otherwhise hang it on the ghost
  585. * bo to be unbound and destroyed.
  586. */
  587. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  588. ghost_obj->ttm = NULL;
  589. else
  590. bo->ttm = NULL;
  591. ttm_bo_unreserve(ghost_obj);
  592. ttm_bo_unref(&ghost_obj);
  593. }
  594. *old_mem = *new_mem;
  595. new_mem->mm_node = NULL;
  596. return 0;
  597. }
  598. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);