ttm_bo_util.c 17 KB

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