ttm_bo_util.c 17 KB

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