ttm_bo_util.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. #include <linux/reservation.h>
  40. void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
  41. {
  42. ttm_bo_mem_put(bo, &bo->mem);
  43. }
  44. int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  45. bool interruptible, bool no_wait_gpu,
  46. struct ttm_mem_reg *new_mem)
  47. {
  48. struct ttm_tt *ttm = bo->ttm;
  49. struct ttm_mem_reg *old_mem = &bo->mem;
  50. int ret;
  51. if (old_mem->mem_type != TTM_PL_SYSTEM) {
  52. ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
  53. if (unlikely(ret != 0)) {
  54. if (ret != -ERESTARTSYS)
  55. pr_err("Failed to expire sync object before unbinding TTM\n");
  56. return ret;
  57. }
  58. ttm_tt_unbind(ttm);
  59. ttm_bo_free_old_node(bo);
  60. ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
  61. TTM_PL_MASK_MEM);
  62. old_mem->mem_type = TTM_PL_SYSTEM;
  63. }
  64. ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
  65. if (unlikely(ret != 0))
  66. return ret;
  67. if (new_mem->mem_type != TTM_PL_SYSTEM) {
  68. ret = ttm_tt_bind(ttm, new_mem);
  69. if (unlikely(ret != 0))
  70. return ret;
  71. }
  72. *old_mem = *new_mem;
  73. new_mem->mm_node = NULL;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL(ttm_bo_move_ttm);
  77. int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
  78. {
  79. if (likely(man->io_reserve_fastpath))
  80. return 0;
  81. if (interruptible)
  82. return mutex_lock_interruptible(&man->io_reserve_mutex);
  83. mutex_lock(&man->io_reserve_mutex);
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(ttm_mem_io_lock);
  87. void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
  88. {
  89. if (likely(man->io_reserve_fastpath))
  90. return;
  91. mutex_unlock(&man->io_reserve_mutex);
  92. }
  93. EXPORT_SYMBOL(ttm_mem_io_unlock);
  94. static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
  95. {
  96. struct ttm_buffer_object *bo;
  97. if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
  98. return -EAGAIN;
  99. bo = list_first_entry(&man->io_reserve_lru,
  100. struct ttm_buffer_object,
  101. io_reserve_lru);
  102. list_del_init(&bo->io_reserve_lru);
  103. ttm_bo_unmap_virtual_locked(bo);
  104. return 0;
  105. }
  106. int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  107. struct ttm_mem_reg *mem)
  108. {
  109. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  110. int ret = 0;
  111. if (!bdev->driver->io_mem_reserve)
  112. return 0;
  113. if (likely(man->io_reserve_fastpath))
  114. return bdev->driver->io_mem_reserve(bdev, mem);
  115. if (bdev->driver->io_mem_reserve &&
  116. mem->bus.io_reserved_count++ == 0) {
  117. retry:
  118. ret = bdev->driver->io_mem_reserve(bdev, mem);
  119. if (ret == -EAGAIN) {
  120. ret = ttm_mem_io_evict(man);
  121. if (ret == 0)
  122. goto retry;
  123. }
  124. }
  125. return ret;
  126. }
  127. EXPORT_SYMBOL(ttm_mem_io_reserve);
  128. void ttm_mem_io_free(struct ttm_bo_device *bdev,
  129. struct ttm_mem_reg *mem)
  130. {
  131. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  132. if (likely(man->io_reserve_fastpath))
  133. return;
  134. if (bdev->driver->io_mem_reserve &&
  135. --mem->bus.io_reserved_count == 0 &&
  136. bdev->driver->io_mem_free)
  137. bdev->driver->io_mem_free(bdev, mem);
  138. }
  139. EXPORT_SYMBOL(ttm_mem_io_free);
  140. int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
  141. {
  142. struct ttm_mem_reg *mem = &bo->mem;
  143. int ret;
  144. if (!mem->bus.io_reserved_vm) {
  145. struct ttm_mem_type_manager *man =
  146. &bo->bdev->man[mem->mem_type];
  147. ret = ttm_mem_io_reserve(bo->bdev, mem);
  148. if (unlikely(ret != 0))
  149. return ret;
  150. mem->bus.io_reserved_vm = true;
  151. if (man->use_io_reserve_lru)
  152. list_add_tail(&bo->io_reserve_lru,
  153. &man->io_reserve_lru);
  154. }
  155. return 0;
  156. }
  157. void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
  158. {
  159. struct ttm_mem_reg *mem = &bo->mem;
  160. if (mem->bus.io_reserved_vm) {
  161. mem->bus.io_reserved_vm = false;
  162. list_del_init(&bo->io_reserve_lru);
  163. ttm_mem_io_free(bo->bdev, mem);
  164. }
  165. }
  166. static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
  167. void **virtual)
  168. {
  169. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  170. int ret;
  171. void *addr;
  172. *virtual = NULL;
  173. (void) ttm_mem_io_lock(man, false);
  174. ret = ttm_mem_io_reserve(bdev, mem);
  175. ttm_mem_io_unlock(man);
  176. if (ret || !mem->bus.is_iomem)
  177. return ret;
  178. if (mem->bus.addr) {
  179. addr = mem->bus.addr;
  180. } else {
  181. if (mem->placement & TTM_PL_FLAG_WC)
  182. addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
  183. else
  184. addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
  185. if (!addr) {
  186. (void) ttm_mem_io_lock(man, false);
  187. ttm_mem_io_free(bdev, mem);
  188. ttm_mem_io_unlock(man);
  189. return -ENOMEM;
  190. }
  191. }
  192. *virtual = addr;
  193. return 0;
  194. }
  195. static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
  196. void *virtual)
  197. {
  198. struct ttm_mem_type_manager *man;
  199. man = &bdev->man[mem->mem_type];
  200. if (virtual && mem->bus.addr == NULL)
  201. iounmap(virtual);
  202. (void) ttm_mem_io_lock(man, false);
  203. ttm_mem_io_free(bdev, mem);
  204. ttm_mem_io_unlock(man);
  205. }
  206. static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
  207. {
  208. uint32_t *dstP =
  209. (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
  210. uint32_t *srcP =
  211. (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
  212. int i;
  213. for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
  214. iowrite32(ioread32(srcP++), dstP++);
  215. return 0;
  216. }
  217. static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
  218. unsigned long page,
  219. pgprot_t prot)
  220. {
  221. struct page *d = ttm->pages[page];
  222. void *dst;
  223. if (!d)
  224. return -ENOMEM;
  225. src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
  226. #ifdef CONFIG_X86
  227. dst = kmap_atomic_prot(d, prot);
  228. #else
  229. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  230. dst = vmap(&d, 1, 0, prot);
  231. else
  232. dst = kmap(d);
  233. #endif
  234. if (!dst)
  235. return -ENOMEM;
  236. memcpy_fromio(dst, src, PAGE_SIZE);
  237. #ifdef CONFIG_X86
  238. kunmap_atomic(dst);
  239. #else
  240. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  241. vunmap(dst);
  242. else
  243. kunmap(d);
  244. #endif
  245. return 0;
  246. }
  247. static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
  248. unsigned long page,
  249. pgprot_t prot)
  250. {
  251. struct page *s = ttm->pages[page];
  252. void *src;
  253. if (!s)
  254. return -ENOMEM;
  255. dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
  256. #ifdef CONFIG_X86
  257. src = kmap_atomic_prot(s, prot);
  258. #else
  259. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  260. src = vmap(&s, 1, 0, prot);
  261. else
  262. src = kmap(s);
  263. #endif
  264. if (!src)
  265. return -ENOMEM;
  266. memcpy_toio(dst, src, PAGE_SIZE);
  267. #ifdef CONFIG_X86
  268. kunmap_atomic(src);
  269. #else
  270. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL))
  271. vunmap(src);
  272. else
  273. kunmap(s);
  274. #endif
  275. return 0;
  276. }
  277. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  278. bool interruptible, bool no_wait_gpu,
  279. struct ttm_mem_reg *new_mem)
  280. {
  281. struct ttm_bo_device *bdev = bo->bdev;
  282. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  283. struct ttm_tt *ttm = bo->ttm;
  284. struct ttm_mem_reg *old_mem = &bo->mem;
  285. struct ttm_mem_reg old_copy = *old_mem;
  286. void *old_iomap;
  287. void *new_iomap;
  288. int ret;
  289. unsigned long i;
  290. unsigned long page;
  291. unsigned long add = 0;
  292. int dir;
  293. ret = ttm_bo_wait(bo, interruptible, no_wait_gpu);
  294. if (ret)
  295. return ret;
  296. ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
  297. if (ret)
  298. return ret;
  299. ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
  300. if (ret)
  301. goto out;
  302. /*
  303. * Single TTM move. NOP.
  304. */
  305. if (old_iomap == NULL && new_iomap == NULL)
  306. goto out2;
  307. /*
  308. * Don't move nonexistent data. Clear destination instead.
  309. */
  310. if (old_iomap == NULL &&
  311. (ttm == NULL || (ttm->state == tt_unpopulated &&
  312. !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
  313. memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
  314. goto out2;
  315. }
  316. /*
  317. * TTM might be null for moves within the same region.
  318. */
  319. if (ttm && ttm->state == tt_unpopulated) {
  320. ret = ttm->bdev->driver->ttm_tt_populate(ttm);
  321. if (ret)
  322. goto out1;
  323. }
  324. add = 0;
  325. dir = 1;
  326. if ((old_mem->mem_type == new_mem->mem_type) &&
  327. (new_mem->start < old_mem->start + old_mem->size)) {
  328. dir = -1;
  329. add = new_mem->num_pages - 1;
  330. }
  331. for (i = 0; i < new_mem->num_pages; ++i) {
  332. page = i * dir + add;
  333. if (old_iomap == NULL) {
  334. pgprot_t prot = ttm_io_prot(old_mem->placement,
  335. PAGE_KERNEL);
  336. ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
  337. prot);
  338. } else if (new_iomap == NULL) {
  339. pgprot_t prot = ttm_io_prot(new_mem->placement,
  340. PAGE_KERNEL);
  341. ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
  342. prot);
  343. } else
  344. ret = ttm_copy_io_page(new_iomap, old_iomap, page);
  345. if (ret)
  346. goto out1;
  347. }
  348. mb();
  349. out2:
  350. old_copy = *old_mem;
  351. *old_mem = *new_mem;
  352. new_mem->mm_node = NULL;
  353. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  354. ttm_tt_destroy(ttm);
  355. bo->ttm = NULL;
  356. }
  357. out1:
  358. ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
  359. out:
  360. ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
  361. /*
  362. * On error, keep the mm node!
  363. */
  364. if (!ret)
  365. ttm_bo_mem_put(bo, &old_copy);
  366. return ret;
  367. }
  368. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  369. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  370. {
  371. kfree(bo);
  372. }
  373. /**
  374. * ttm_buffer_object_transfer
  375. *
  376. * @bo: A pointer to a struct ttm_buffer_object.
  377. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  378. * holding the data of @bo with the old placement.
  379. *
  380. * This is a utility function that may be called after an accelerated move
  381. * has been scheduled. A new buffer object is created as a placeholder for
  382. * the old data while it's being copied. When that buffer object is idle,
  383. * it can be destroyed, releasing the space of the old placement.
  384. * Returns:
  385. * !0: Failure.
  386. */
  387. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  388. struct ttm_buffer_object **new_obj)
  389. {
  390. struct ttm_buffer_object *fbo;
  391. int ret;
  392. fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
  393. if (!fbo)
  394. return -ENOMEM;
  395. *fbo = *bo;
  396. /**
  397. * Fix up members that we shouldn't copy directly:
  398. * TODO: Explicit member copy would probably be better here.
  399. */
  400. INIT_LIST_HEAD(&fbo->ddestroy);
  401. INIT_LIST_HEAD(&fbo->lru);
  402. INIT_LIST_HEAD(&fbo->swap);
  403. INIT_LIST_HEAD(&fbo->io_reserve_lru);
  404. mutex_init(&fbo->wu_mutex);
  405. fbo->moving = NULL;
  406. drm_vma_node_reset(&fbo->vma_node);
  407. atomic_set(&fbo->cpu_writers, 0);
  408. kref_init(&fbo->list_kref);
  409. kref_init(&fbo->kref);
  410. fbo->destroy = &ttm_transfered_destroy;
  411. fbo->acc_size = 0;
  412. fbo->resv = &fbo->ttm_resv;
  413. reservation_object_init(fbo->resv);
  414. ret = ww_mutex_trylock(&fbo->resv->lock);
  415. WARN_ON(!ret);
  416. *new_obj = fbo;
  417. return 0;
  418. }
  419. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  420. {
  421. /* Cached mappings need no adjustment */
  422. if (caching_flags & TTM_PL_FLAG_CACHED)
  423. return tmp;
  424. #if defined(__i386__) || defined(__x86_64__)
  425. if (caching_flags & TTM_PL_FLAG_WC)
  426. tmp = pgprot_writecombine(tmp);
  427. else if (boot_cpu_data.x86 > 3)
  428. tmp = pgprot_noncached(tmp);
  429. #endif
  430. #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
  431. defined(__powerpc__)
  432. if (caching_flags & TTM_PL_FLAG_WC)
  433. tmp = pgprot_writecombine(tmp);
  434. else
  435. tmp = pgprot_noncached(tmp);
  436. #endif
  437. #if defined(__sparc__) || defined(__mips__)
  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 = ttm_io_prot(mem->placement, PAGE_KERNEL);
  491. map->bo_kmap_type = ttm_bo_map_vmap;
  492. map->virtual = vmap(ttm->pages + start_page, num_pages,
  493. 0, prot);
  494. }
  495. return (!map->virtual) ? -ENOMEM : 0;
  496. }
  497. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  498. unsigned long start_page, unsigned long num_pages,
  499. struct ttm_bo_kmap_obj *map)
  500. {
  501. struct ttm_mem_type_manager *man =
  502. &bo->bdev->man[bo->mem.mem_type];
  503. unsigned long offset, size;
  504. int ret;
  505. BUG_ON(!list_empty(&bo->swap));
  506. map->virtual = NULL;
  507. map->bo = bo;
  508. if (num_pages > bo->num_pages)
  509. return -EINVAL;
  510. if (start_page > bo->num_pages)
  511. return -EINVAL;
  512. #if 0
  513. if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
  514. return -EPERM;
  515. #endif
  516. (void) ttm_mem_io_lock(man, false);
  517. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  518. ttm_mem_io_unlock(man);
  519. if (ret)
  520. return ret;
  521. if (!bo->mem.bus.is_iomem) {
  522. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  523. } else {
  524. offset = start_page << PAGE_SHIFT;
  525. size = num_pages << PAGE_SHIFT;
  526. return ttm_bo_ioremap(bo, offset, size, map);
  527. }
  528. }
  529. EXPORT_SYMBOL(ttm_bo_kmap);
  530. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  531. {
  532. struct ttm_buffer_object *bo = map->bo;
  533. struct ttm_mem_type_manager *man =
  534. &bo->bdev->man[bo->mem.mem_type];
  535. if (!map->virtual)
  536. return;
  537. switch (map->bo_kmap_type) {
  538. case ttm_bo_map_iomap:
  539. iounmap(map->virtual);
  540. break;
  541. case ttm_bo_map_vmap:
  542. vunmap(map->virtual);
  543. break;
  544. case ttm_bo_map_kmap:
  545. kunmap(map->page);
  546. break;
  547. case ttm_bo_map_premapped:
  548. break;
  549. default:
  550. BUG();
  551. }
  552. (void) ttm_mem_io_lock(man, false);
  553. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  554. ttm_mem_io_unlock(man);
  555. map->virtual = NULL;
  556. map->page = NULL;
  557. }
  558. EXPORT_SYMBOL(ttm_bo_kunmap);
  559. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  560. struct fence *fence,
  561. bool evict,
  562. struct ttm_mem_reg *new_mem)
  563. {
  564. struct ttm_bo_device *bdev = bo->bdev;
  565. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  566. struct ttm_mem_reg *old_mem = &bo->mem;
  567. int ret;
  568. struct ttm_buffer_object *ghost_obj;
  569. reservation_object_add_excl_fence(bo->resv, fence);
  570. if (evict) {
  571. ret = ttm_bo_wait(bo, false, false);
  572. if (ret)
  573. return ret;
  574. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  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. fence_put(bo->moving);
  588. bo->moving = fence_get(fence);
  589. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  590. if (ret)
  591. return ret;
  592. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  593. /**
  594. * If we're not moving to fixed memory, the TTM object
  595. * needs to stay alive. Otherwhise hang it on the ghost
  596. * bo to be unbound and destroyed.
  597. */
  598. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  599. ghost_obj->ttm = NULL;
  600. else
  601. bo->ttm = NULL;
  602. ttm_bo_unreserve(ghost_obj);
  603. ttm_bo_unref(&ghost_obj);
  604. }
  605. *old_mem = *new_mem;
  606. new_mem->mm_node = NULL;
  607. return 0;
  608. }
  609. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
  610. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  611. struct fence *fence, bool evict,
  612. struct ttm_mem_reg *new_mem)
  613. {
  614. struct ttm_bo_device *bdev = bo->bdev;
  615. struct ttm_mem_reg *old_mem = &bo->mem;
  616. struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
  617. struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
  618. int ret;
  619. reservation_object_add_excl_fence(bo->resv, fence);
  620. if (!evict) {
  621. struct ttm_buffer_object *ghost_obj;
  622. /**
  623. * This should help pipeline ordinary buffer moves.
  624. *
  625. * Hang old buffer memory on a new buffer object,
  626. * and leave it to be released when the GPU
  627. * operation has completed.
  628. */
  629. fence_put(bo->moving);
  630. bo->moving = fence_get(fence);
  631. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  632. if (ret)
  633. return ret;
  634. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  635. /**
  636. * If we're not moving to fixed memory, the TTM object
  637. * needs to stay alive. Otherwhise hang it on the ghost
  638. * bo to be unbound and destroyed.
  639. */
  640. if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
  641. ghost_obj->ttm = NULL;
  642. else
  643. bo->ttm = NULL;
  644. ttm_bo_unreserve(ghost_obj);
  645. ttm_bo_unref(&ghost_obj);
  646. } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
  647. /**
  648. * BO doesn't have a TTM we need to bind/unbind. Just remember
  649. * this eviction and free up the allocation
  650. */
  651. spin_lock(&from->move_lock);
  652. if (!from->move || fence_is_later(fence, from->move)) {
  653. fence_put(from->move);
  654. from->move = fence_get(fence);
  655. }
  656. spin_unlock(&from->move_lock);
  657. ttm_bo_free_old_node(bo);
  658. fence_put(bo->moving);
  659. bo->moving = fence_get(fence);
  660. } else {
  661. /**
  662. * Last resort, wait for the move to be completed.
  663. *
  664. * Should never happen in pratice.
  665. */
  666. ret = ttm_bo_wait(bo, false, false);
  667. if (ret)
  668. return ret;
  669. if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
  670. ttm_tt_destroy(bo->ttm);
  671. bo->ttm = NULL;
  672. }
  673. ttm_bo_free_old_node(bo);
  674. }
  675. *old_mem = *new_mem;
  676. new_mem->mm_node = NULL;
  677. return 0;
  678. }
  679. EXPORT_SYMBOL(ttm_bo_pipeline_move);