ttm_bo_util.c 17 KB

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