ttm_bo_util.c 17 KB

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