ttm_bo_util.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
  374. if (!fbo)
  375. return -ENOMEM;
  376. *fbo = *bo;
  377. /**
  378. * Fix up members that we shouldn't copy directly:
  379. * TODO: Explicit member copy would probably be better here.
  380. */
  381. init_waitqueue_head(&fbo->event_queue);
  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. *new_obj = fbo;
  399. return 0;
  400. }
  401. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  402. {
  403. #if defined(__i386__) || defined(__x86_64__)
  404. if (caching_flags & TTM_PL_FLAG_WC)
  405. tmp = pgprot_writecombine(tmp);
  406. else if (boot_cpu_data.x86 > 3)
  407. tmp = pgprot_noncached(tmp);
  408. #elif defined(__powerpc__)
  409. if (!(caching_flags & TTM_PL_FLAG_CACHED)) {
  410. pgprot_val(tmp) |= _PAGE_NO_CACHE;
  411. if (caching_flags & TTM_PL_FLAG_UNCACHED)
  412. pgprot_val(tmp) |= _PAGE_GUARDED;
  413. }
  414. #endif
  415. #if defined(__ia64__)
  416. if (caching_flags & TTM_PL_FLAG_WC)
  417. tmp = pgprot_writecombine(tmp);
  418. else
  419. tmp = pgprot_noncached(tmp);
  420. #endif
  421. #if defined(__sparc__) || defined(__mips__)
  422. if (!(caching_flags & TTM_PL_FLAG_CACHED))
  423. tmp = pgprot_noncached(tmp);
  424. #endif
  425. return tmp;
  426. }
  427. EXPORT_SYMBOL(ttm_io_prot);
  428. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  429. unsigned long offset,
  430. unsigned long size,
  431. struct ttm_bo_kmap_obj *map)
  432. {
  433. struct ttm_mem_reg *mem = &bo->mem;
  434. if (bo->mem.bus.addr) {
  435. map->bo_kmap_type = ttm_bo_map_premapped;
  436. map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
  437. } else {
  438. map->bo_kmap_type = ttm_bo_map_iomap;
  439. if (mem->placement & TTM_PL_FLAG_WC)
  440. map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
  441. size);
  442. else
  443. map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
  444. size);
  445. }
  446. return (!map->virtual) ? -ENOMEM : 0;
  447. }
  448. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  449. unsigned long start_page,
  450. unsigned long num_pages,
  451. struct ttm_bo_kmap_obj *map)
  452. {
  453. struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
  454. struct ttm_tt *ttm = bo->ttm;
  455. int ret;
  456. BUG_ON(!ttm);
  457. if (ttm->state == tt_unpopulated) {
  458. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  459. if (ret)
  460. return ret;
  461. }
  462. if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
  463. /*
  464. * We're mapping a single page, and the desired
  465. * page protection is consistent with the bo.
  466. */
  467. map->bo_kmap_type = ttm_bo_map_kmap;
  468. map->page = ttm->pages[start_page];
  469. map->virtual = kmap(map->page);
  470. } else {
  471. /*
  472. * We need to use vmap to get the desired page protection
  473. * or to make the buffer object look contiguous.
  474. */
  475. prot = (mem->placement & TTM_PL_FLAG_CACHED) ?
  476. PAGE_KERNEL :
  477. ttm_io_prot(mem->placement, PAGE_KERNEL);
  478. map->bo_kmap_type = ttm_bo_map_vmap;
  479. map->virtual = vmap(ttm->pages + start_page, num_pages,
  480. 0, prot);
  481. }
  482. return (!map->virtual) ? -ENOMEM : 0;
  483. }
  484. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  485. unsigned long start_page, unsigned long num_pages,
  486. struct ttm_bo_kmap_obj *map)
  487. {
  488. struct ttm_mem_type_manager *man =
  489. &bo->bdev->man[bo->mem.mem_type];
  490. unsigned long offset, size;
  491. int ret;
  492. BUG_ON(!list_empty(&bo->swap));
  493. map->virtual = NULL;
  494. map->bo = bo;
  495. if (num_pages > bo->num_pages)
  496. return -EINVAL;
  497. if (start_page > bo->num_pages)
  498. return -EINVAL;
  499. #if 0
  500. if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
  501. return -EPERM;
  502. #endif
  503. (void) ttm_mem_io_lock(man, false);
  504. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  505. ttm_mem_io_unlock(man);
  506. if (ret)
  507. return ret;
  508. if (!bo->mem.bus.is_iomem) {
  509. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  510. } else {
  511. offset = start_page << PAGE_SHIFT;
  512. size = num_pages << PAGE_SHIFT;
  513. return ttm_bo_ioremap(bo, offset, size, map);
  514. }
  515. }
  516. EXPORT_SYMBOL(ttm_bo_kmap);
  517. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  518. {
  519. struct ttm_buffer_object *bo = map->bo;
  520. struct ttm_mem_type_manager *man =
  521. &bo->bdev->man[bo->mem.mem_type];
  522. if (!map->virtual)
  523. return;
  524. switch (map->bo_kmap_type) {
  525. case ttm_bo_map_iomap:
  526. iounmap(map->virtual);
  527. break;
  528. case ttm_bo_map_vmap:
  529. vunmap(map->virtual);
  530. break;
  531. case ttm_bo_map_kmap:
  532. kunmap(map->page);
  533. break;
  534. case ttm_bo_map_premapped:
  535. break;
  536. default:
  537. BUG();
  538. }
  539. (void) ttm_mem_io_lock(man, false);
  540. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  541. ttm_mem_io_unlock(man);
  542. map->virtual = NULL;
  543. map->page = NULL;
  544. }
  545. EXPORT_SYMBOL(ttm_bo_kunmap);
  546. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  547. void *sync_obj,
  548. bool evict,
  549. bool no_wait_gpu,
  550. struct ttm_mem_reg *new_mem)
  551. {
  552. struct ttm_bo_device *bdev = bo->bdev;
  553. struct ttm_bo_driver *driver = bdev->driver;
  554. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  555. struct ttm_mem_reg *old_mem = &bo->mem;
  556. int ret;
  557. struct ttm_buffer_object *ghost_obj;
  558. void *tmp_obj = NULL;
  559. spin_lock(&bdev->fence_lock);
  560. if (bo->sync_obj) {
  561. tmp_obj = bo->sync_obj;
  562. bo->sync_obj = NULL;
  563. }
  564. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  565. if (evict) {
  566. ret = ttm_bo_wait(bo, false, false, false);
  567. spin_unlock(&bdev->fence_lock);
  568. if (tmp_obj)
  569. driver->sync_obj_unref(&tmp_obj);
  570. if (ret)
  571. return ret;
  572. if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  573. (bo->ttm != NULL)) {
  574. ttm_tt_unbind(bo->ttm);
  575. ttm_tt_destroy(bo->ttm);
  576. bo->ttm = NULL;
  577. }
  578. ttm_bo_free_old_node(bo);
  579. } else {
  580. /**
  581. * This should help pipeline ordinary buffer moves.
  582. *
  583. * Hang old buffer memory on a new buffer object,
  584. * and leave it to be released when the GPU
  585. * operation has completed.
  586. */
  587. set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  588. spin_unlock(&bdev->fence_lock);
  589. if (tmp_obj)
  590. driver->sync_obj_unref(&tmp_obj);
  591. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  592. if (ret)
  593. return ret;
  594. /**
  595. * If we're not moving to fixed memory, the TTM object
  596. * needs to stay alive. Otherwhise hang it on the ghost
  597. * bo to be unbound and destroyed.
  598. */
  599. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  600. ghost_obj->ttm = NULL;
  601. else
  602. bo->ttm = NULL;
  603. ttm_bo_unreserve(ghost_obj);
  604. ttm_bo_unref(&ghost_obj);
  605. }
  606. *old_mem = *new_mem;
  607. new_mem->mm_node = NULL;
  608. return 0;
  609. }
  610. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);