ttm_bo_util.c 17 KB

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