ttm_bo_util.c 17 KB

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