ttm_bo_util.c 17 KB

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