ttm_bo_util.c 17 KB

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