ttm_bo_util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. */
  31. #include <drm/ttm/ttm_bo_driver.h>
  32. #include <drm/ttm/ttm_placement.h>
  33. #include <drm/drm_vma_manager.h>
  34. #include <linux/io.h>
  35. #include <linux/highmem.h>
  36. #include <linux/wait.h>
  37. #include <linux/slab.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/module.h>
  40. #include <linux/reservation.h>
  41. struct ttm_transfer_obj {
  42. struct ttm_buffer_object base;
  43. struct ttm_buffer_object *bo;
  44. };
  45. void ttm_bo_free_old_node(struct ttm_buffer_object *bo)
  46. {
  47. ttm_bo_mem_put(bo, &bo->mem);
  48. }
  49. int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  50. struct ttm_operation_ctx *ctx,
  51. struct ttm_mem_reg *new_mem)
  52. {
  53. struct ttm_tt *ttm = bo->ttm;
  54. struct ttm_mem_reg *old_mem = &bo->mem;
  55. int ret;
  56. if (old_mem->mem_type != TTM_PL_SYSTEM) {
  57. ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
  58. if (unlikely(ret != 0)) {
  59. if (ret != -ERESTARTSYS)
  60. pr_err("Failed to expire sync object before unbinding TTM\n");
  61. return ret;
  62. }
  63. ttm_tt_unbind(ttm);
  64. ttm_bo_free_old_node(bo);
  65. ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM,
  66. TTM_PL_MASK_MEM);
  67. old_mem->mem_type = TTM_PL_SYSTEM;
  68. }
  69. ret = ttm_tt_set_placement_caching(ttm, new_mem->placement);
  70. if (unlikely(ret != 0))
  71. return ret;
  72. if (new_mem->mem_type != TTM_PL_SYSTEM) {
  73. ret = ttm_tt_bind(ttm, new_mem, ctx);
  74. if (unlikely(ret != 0))
  75. return ret;
  76. }
  77. *old_mem = *new_mem;
  78. new_mem->mm_node = NULL;
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(ttm_bo_move_ttm);
  82. int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible)
  83. {
  84. if (likely(man->io_reserve_fastpath))
  85. return 0;
  86. if (interruptible)
  87. return mutex_lock_interruptible(&man->io_reserve_mutex);
  88. mutex_lock(&man->io_reserve_mutex);
  89. return 0;
  90. }
  91. EXPORT_SYMBOL(ttm_mem_io_lock);
  92. void ttm_mem_io_unlock(struct ttm_mem_type_manager *man)
  93. {
  94. if (likely(man->io_reserve_fastpath))
  95. return;
  96. mutex_unlock(&man->io_reserve_mutex);
  97. }
  98. EXPORT_SYMBOL(ttm_mem_io_unlock);
  99. static int ttm_mem_io_evict(struct ttm_mem_type_manager *man)
  100. {
  101. struct ttm_buffer_object *bo;
  102. if (!man->use_io_reserve_lru || list_empty(&man->io_reserve_lru))
  103. return -EAGAIN;
  104. bo = list_first_entry(&man->io_reserve_lru,
  105. struct ttm_buffer_object,
  106. io_reserve_lru);
  107. list_del_init(&bo->io_reserve_lru);
  108. ttm_bo_unmap_virtual_locked(bo);
  109. return 0;
  110. }
  111. int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  112. struct ttm_mem_reg *mem)
  113. {
  114. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  115. int ret = 0;
  116. if (!bdev->driver->io_mem_reserve)
  117. return 0;
  118. if (likely(man->io_reserve_fastpath))
  119. return bdev->driver->io_mem_reserve(bdev, mem);
  120. if (bdev->driver->io_mem_reserve &&
  121. mem->bus.io_reserved_count++ == 0) {
  122. retry:
  123. ret = bdev->driver->io_mem_reserve(bdev, mem);
  124. if (ret == -EAGAIN) {
  125. ret = ttm_mem_io_evict(man);
  126. if (ret == 0)
  127. goto retry;
  128. }
  129. }
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(ttm_mem_io_reserve);
  133. void ttm_mem_io_free(struct ttm_bo_device *bdev,
  134. struct ttm_mem_reg *mem)
  135. {
  136. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  137. if (likely(man->io_reserve_fastpath))
  138. return;
  139. if (bdev->driver->io_mem_reserve &&
  140. --mem->bus.io_reserved_count == 0 &&
  141. bdev->driver->io_mem_free)
  142. bdev->driver->io_mem_free(bdev, mem);
  143. }
  144. EXPORT_SYMBOL(ttm_mem_io_free);
  145. int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo)
  146. {
  147. struct ttm_mem_reg *mem = &bo->mem;
  148. int ret;
  149. if (!mem->bus.io_reserved_vm) {
  150. struct ttm_mem_type_manager *man =
  151. &bo->bdev->man[mem->mem_type];
  152. ret = ttm_mem_io_reserve(bo->bdev, mem);
  153. if (unlikely(ret != 0))
  154. return ret;
  155. mem->bus.io_reserved_vm = true;
  156. if (man->use_io_reserve_lru)
  157. list_add_tail(&bo->io_reserve_lru,
  158. &man->io_reserve_lru);
  159. }
  160. return 0;
  161. }
  162. void ttm_mem_io_free_vm(struct ttm_buffer_object *bo)
  163. {
  164. struct ttm_mem_reg *mem = &bo->mem;
  165. if (mem->bus.io_reserved_vm) {
  166. mem->bus.io_reserved_vm = false;
  167. list_del_init(&bo->io_reserve_lru);
  168. ttm_mem_io_free(bo->bdev, mem);
  169. }
  170. }
  171. static int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
  172. void **virtual)
  173. {
  174. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  175. int ret;
  176. void *addr;
  177. *virtual = NULL;
  178. (void) ttm_mem_io_lock(man, false);
  179. ret = ttm_mem_io_reserve(bdev, mem);
  180. ttm_mem_io_unlock(man);
  181. if (ret || !mem->bus.is_iomem)
  182. return ret;
  183. if (mem->bus.addr) {
  184. addr = mem->bus.addr;
  185. } else {
  186. if (mem->placement & TTM_PL_FLAG_WC)
  187. addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size);
  188. else
  189. addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size);
  190. if (!addr) {
  191. (void) ttm_mem_io_lock(man, false);
  192. ttm_mem_io_free(bdev, mem);
  193. ttm_mem_io_unlock(man);
  194. return -ENOMEM;
  195. }
  196. }
  197. *virtual = addr;
  198. return 0;
  199. }
  200. static void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem,
  201. void *virtual)
  202. {
  203. struct ttm_mem_type_manager *man;
  204. man = &bdev->man[mem->mem_type];
  205. if (virtual && mem->bus.addr == NULL)
  206. iounmap(virtual);
  207. (void) ttm_mem_io_lock(man, false);
  208. ttm_mem_io_free(bdev, mem);
  209. ttm_mem_io_unlock(man);
  210. }
  211. static int ttm_copy_io_page(void *dst, void *src, unsigned long page)
  212. {
  213. uint32_t *dstP =
  214. (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
  215. uint32_t *srcP =
  216. (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
  217. int i;
  218. for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
  219. iowrite32(ioread32(srcP++), dstP++);
  220. return 0;
  221. }
  222. #ifdef CONFIG_X86
  223. #define __ttm_kmap_atomic_prot(__page, __prot) kmap_atomic_prot(__page, __prot)
  224. #define __ttm_kunmap_atomic(__addr) kunmap_atomic(__addr)
  225. #else
  226. #define __ttm_kmap_atomic_prot(__page, __prot) vmap(&__page, 1, 0, __prot)
  227. #define __ttm_kunmap_atomic(__addr) vunmap(__addr)
  228. #endif
  229. /**
  230. * ttm_kmap_atomic_prot - Efficient kernel map of a single page with
  231. * specified page protection.
  232. *
  233. * @page: The page to map.
  234. * @prot: The page protection.
  235. *
  236. * This function maps a TTM page using the kmap_atomic api if available,
  237. * otherwise falls back to vmap. The user must make sure that the
  238. * specified page does not have an aliased mapping with a different caching
  239. * policy unless the architecture explicitly allows it. Also mapping and
  240. * unmapping using this api must be correctly nested. Unmapping should
  241. * occur in the reverse order of mapping.
  242. */
  243. void *ttm_kmap_atomic_prot(struct page *page, pgprot_t prot)
  244. {
  245. if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL))
  246. return kmap_atomic(page);
  247. else
  248. return __ttm_kmap_atomic_prot(page, prot);
  249. }
  250. EXPORT_SYMBOL(ttm_kmap_atomic_prot);
  251. /**
  252. * ttm_kunmap_atomic_prot - Unmap a page that was mapped using
  253. * ttm_kmap_atomic_prot.
  254. *
  255. * @addr: The virtual address from the map.
  256. * @prot: The page protection.
  257. */
  258. void ttm_kunmap_atomic_prot(void *addr, pgprot_t prot)
  259. {
  260. if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL))
  261. kunmap_atomic(addr);
  262. else
  263. __ttm_kunmap_atomic(addr);
  264. }
  265. EXPORT_SYMBOL(ttm_kunmap_atomic_prot);
  266. static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
  267. unsigned long page,
  268. pgprot_t prot)
  269. {
  270. struct page *d = ttm->pages[page];
  271. void *dst;
  272. if (!d)
  273. return -ENOMEM;
  274. src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
  275. dst = ttm_kmap_atomic_prot(d, prot);
  276. if (!dst)
  277. return -ENOMEM;
  278. memcpy_fromio(dst, src, PAGE_SIZE);
  279. ttm_kunmap_atomic_prot(dst, prot);
  280. return 0;
  281. }
  282. static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
  283. unsigned long page,
  284. pgprot_t prot)
  285. {
  286. struct page *s = ttm->pages[page];
  287. void *src;
  288. if (!s)
  289. return -ENOMEM;
  290. dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
  291. src = ttm_kmap_atomic_prot(s, prot);
  292. if (!src)
  293. return -ENOMEM;
  294. memcpy_toio(dst, src, PAGE_SIZE);
  295. ttm_kunmap_atomic_prot(src, prot);
  296. return 0;
  297. }
  298. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  299. struct ttm_operation_ctx *ctx,
  300. struct ttm_mem_reg *new_mem)
  301. {
  302. struct ttm_bo_device *bdev = bo->bdev;
  303. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  304. struct ttm_tt *ttm = bo->ttm;
  305. struct ttm_mem_reg *old_mem = &bo->mem;
  306. struct ttm_mem_reg old_copy = *old_mem;
  307. void *old_iomap;
  308. void *new_iomap;
  309. int ret;
  310. unsigned long i;
  311. unsigned long page;
  312. unsigned long add = 0;
  313. int dir;
  314. ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
  315. if (ret)
  316. return ret;
  317. ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
  318. if (ret)
  319. return ret;
  320. ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
  321. if (ret)
  322. goto out;
  323. /*
  324. * Single TTM move. NOP.
  325. */
  326. if (old_iomap == NULL && new_iomap == NULL)
  327. goto out2;
  328. /*
  329. * Don't move nonexistent data. Clear destination instead.
  330. */
  331. if (old_iomap == NULL &&
  332. (ttm == NULL || (ttm->state == tt_unpopulated &&
  333. !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
  334. memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
  335. goto out2;
  336. }
  337. /*
  338. * TTM might be null for moves within the same region.
  339. */
  340. if (ttm) {
  341. ret = ttm_tt_populate(ttm, ctx);
  342. if (ret)
  343. goto out1;
  344. }
  345. add = 0;
  346. dir = 1;
  347. if ((old_mem->mem_type == new_mem->mem_type) &&
  348. (new_mem->start < old_mem->start + old_mem->size)) {
  349. dir = -1;
  350. add = new_mem->num_pages - 1;
  351. }
  352. for (i = 0; i < new_mem->num_pages; ++i) {
  353. page = i * dir + add;
  354. if (old_iomap == NULL) {
  355. pgprot_t prot = ttm_io_prot(old_mem->placement,
  356. PAGE_KERNEL);
  357. ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
  358. prot);
  359. } else if (new_iomap == NULL) {
  360. pgprot_t prot = ttm_io_prot(new_mem->placement,
  361. PAGE_KERNEL);
  362. ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
  363. prot);
  364. } else {
  365. ret = ttm_copy_io_page(new_iomap, old_iomap, page);
  366. }
  367. if (ret)
  368. goto out1;
  369. }
  370. mb();
  371. out2:
  372. old_copy = *old_mem;
  373. *old_mem = *new_mem;
  374. new_mem->mm_node = NULL;
  375. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  376. ttm_tt_destroy(ttm);
  377. bo->ttm = NULL;
  378. }
  379. out1:
  380. ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
  381. out:
  382. ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
  383. /*
  384. * On error, keep the mm node!
  385. */
  386. if (!ret)
  387. ttm_bo_mem_put(bo, &old_copy);
  388. return ret;
  389. }
  390. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  391. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  392. {
  393. struct ttm_transfer_obj *fbo;
  394. fbo = container_of(bo, struct ttm_transfer_obj, base);
  395. ttm_bo_unref(&fbo->bo);
  396. kfree(fbo);
  397. }
  398. /**
  399. * ttm_buffer_object_transfer
  400. *
  401. * @bo: A pointer to a struct ttm_buffer_object.
  402. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  403. * holding the data of @bo with the old placement.
  404. *
  405. * This is a utility function that may be called after an accelerated move
  406. * has been scheduled. A new buffer object is created as a placeholder for
  407. * the old data while it's being copied. When that buffer object is idle,
  408. * it can be destroyed, releasing the space of the old placement.
  409. * Returns:
  410. * !0: Failure.
  411. */
  412. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  413. struct ttm_buffer_object **new_obj)
  414. {
  415. struct ttm_transfer_obj *fbo;
  416. int ret;
  417. fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
  418. if (!fbo)
  419. return -ENOMEM;
  420. fbo->base = *bo;
  421. fbo->bo = ttm_bo_reference(bo);
  422. /**
  423. * Fix up members that we shouldn't copy directly:
  424. * TODO: Explicit member copy would probably be better here.
  425. */
  426. atomic_inc(&bo->bdev->glob->bo_count);
  427. INIT_LIST_HEAD(&fbo->base.ddestroy);
  428. INIT_LIST_HEAD(&fbo->base.lru);
  429. INIT_LIST_HEAD(&fbo->base.swap);
  430. INIT_LIST_HEAD(&fbo->base.io_reserve_lru);
  431. mutex_init(&fbo->base.wu_mutex);
  432. fbo->base.moving = NULL;
  433. drm_vma_node_reset(&fbo->base.vma_node);
  434. atomic_set(&fbo->base.cpu_writers, 0);
  435. kref_init(&fbo->base.list_kref);
  436. kref_init(&fbo->base.kref);
  437. fbo->base.destroy = &ttm_transfered_destroy;
  438. fbo->base.acc_size = 0;
  439. fbo->base.resv = &fbo->base.ttm_resv;
  440. reservation_object_init(fbo->base.resv);
  441. ret = reservation_object_trylock(fbo->base.resv);
  442. WARN_ON(!ret);
  443. *new_obj = &fbo->base;
  444. return 0;
  445. }
  446. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  447. {
  448. /* Cached mappings need no adjustment */
  449. if (caching_flags & TTM_PL_FLAG_CACHED)
  450. return tmp;
  451. #if defined(__i386__) || defined(__x86_64__)
  452. if (caching_flags & TTM_PL_FLAG_WC)
  453. tmp = pgprot_writecombine(tmp);
  454. else if (boot_cpu_data.x86 > 3)
  455. tmp = pgprot_noncached(tmp);
  456. #endif
  457. #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
  458. defined(__powerpc__)
  459. if (caching_flags & TTM_PL_FLAG_WC)
  460. tmp = pgprot_writecombine(tmp);
  461. else
  462. tmp = pgprot_noncached(tmp);
  463. #endif
  464. #if defined(__sparc__) || defined(__mips__)
  465. tmp = pgprot_noncached(tmp);
  466. #endif
  467. return tmp;
  468. }
  469. EXPORT_SYMBOL(ttm_io_prot);
  470. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  471. unsigned long offset,
  472. unsigned long size,
  473. struct ttm_bo_kmap_obj *map)
  474. {
  475. struct ttm_mem_reg *mem = &bo->mem;
  476. if (bo->mem.bus.addr) {
  477. map->bo_kmap_type = ttm_bo_map_premapped;
  478. map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
  479. } else {
  480. map->bo_kmap_type = ttm_bo_map_iomap;
  481. if (mem->placement & TTM_PL_FLAG_WC)
  482. map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
  483. size);
  484. else
  485. map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
  486. size);
  487. }
  488. return (!map->virtual) ? -ENOMEM : 0;
  489. }
  490. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  491. unsigned long start_page,
  492. unsigned long num_pages,
  493. struct ttm_bo_kmap_obj *map)
  494. {
  495. struct ttm_mem_reg *mem = &bo->mem;
  496. struct ttm_operation_ctx ctx = {
  497. .interruptible = false,
  498. .no_wait_gpu = false
  499. };
  500. struct ttm_tt *ttm = bo->ttm;
  501. pgprot_t prot;
  502. int ret;
  503. BUG_ON(!ttm);
  504. ret = ttm_tt_populate(ttm, &ctx);
  505. if (ret)
  506. return ret;
  507. if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
  508. /*
  509. * We're mapping a single page, and the desired
  510. * page protection is consistent with the bo.
  511. */
  512. map->bo_kmap_type = ttm_bo_map_kmap;
  513. map->page = ttm->pages[start_page];
  514. map->virtual = kmap(map->page);
  515. } else {
  516. /*
  517. * We need to use vmap to get the desired page protection
  518. * or to make the buffer object look contiguous.
  519. */
  520. prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
  521. map->bo_kmap_type = ttm_bo_map_vmap;
  522. map->virtual = vmap(ttm->pages + start_page, num_pages,
  523. 0, prot);
  524. }
  525. return (!map->virtual) ? -ENOMEM : 0;
  526. }
  527. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  528. unsigned long start_page, unsigned long num_pages,
  529. struct ttm_bo_kmap_obj *map)
  530. {
  531. struct ttm_mem_type_manager *man =
  532. &bo->bdev->man[bo->mem.mem_type];
  533. unsigned long offset, size;
  534. int ret;
  535. map->virtual = NULL;
  536. map->bo = bo;
  537. if (num_pages > bo->num_pages)
  538. return -EINVAL;
  539. if (start_page > bo->num_pages)
  540. return -EINVAL;
  541. #if 0
  542. if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
  543. return -EPERM;
  544. #endif
  545. (void) ttm_mem_io_lock(man, false);
  546. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  547. ttm_mem_io_unlock(man);
  548. if (ret)
  549. return ret;
  550. if (!bo->mem.bus.is_iomem) {
  551. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  552. } else {
  553. offset = start_page << PAGE_SHIFT;
  554. size = num_pages << PAGE_SHIFT;
  555. return ttm_bo_ioremap(bo, offset, size, map);
  556. }
  557. }
  558. EXPORT_SYMBOL(ttm_bo_kmap);
  559. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  560. {
  561. struct ttm_buffer_object *bo = map->bo;
  562. struct ttm_mem_type_manager *man =
  563. &bo->bdev->man[bo->mem.mem_type];
  564. if (!map->virtual)
  565. return;
  566. switch (map->bo_kmap_type) {
  567. case ttm_bo_map_iomap:
  568. iounmap(map->virtual);
  569. break;
  570. case ttm_bo_map_vmap:
  571. vunmap(map->virtual);
  572. break;
  573. case ttm_bo_map_kmap:
  574. kunmap(map->page);
  575. break;
  576. case ttm_bo_map_premapped:
  577. break;
  578. default:
  579. BUG();
  580. }
  581. (void) ttm_mem_io_lock(man, false);
  582. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  583. ttm_mem_io_unlock(man);
  584. map->virtual = NULL;
  585. map->page = NULL;
  586. }
  587. EXPORT_SYMBOL(ttm_bo_kunmap);
  588. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  589. struct dma_fence *fence,
  590. bool evict,
  591. struct ttm_mem_reg *new_mem)
  592. {
  593. struct ttm_bo_device *bdev = bo->bdev;
  594. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  595. struct ttm_mem_reg *old_mem = &bo->mem;
  596. int ret;
  597. struct ttm_buffer_object *ghost_obj;
  598. reservation_object_add_excl_fence(bo->resv, fence);
  599. if (evict) {
  600. ret = ttm_bo_wait(bo, false, false);
  601. if (ret)
  602. return ret;
  603. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  604. ttm_tt_destroy(bo->ttm);
  605. bo->ttm = NULL;
  606. }
  607. ttm_bo_free_old_node(bo);
  608. } else {
  609. /**
  610. * This should help pipeline ordinary buffer moves.
  611. *
  612. * Hang old buffer memory on a new buffer object,
  613. * and leave it to be released when the GPU
  614. * operation has completed.
  615. */
  616. dma_fence_put(bo->moving);
  617. bo->moving = dma_fence_get(fence);
  618. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  619. if (ret)
  620. return ret;
  621. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  622. /**
  623. * If we're not moving to fixed memory, the TTM object
  624. * needs to stay alive. Otherwhise hang it on the ghost
  625. * bo to be unbound and destroyed.
  626. */
  627. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  628. ghost_obj->ttm = NULL;
  629. else
  630. bo->ttm = NULL;
  631. ttm_bo_unreserve(ghost_obj);
  632. ttm_bo_unref(&ghost_obj);
  633. }
  634. *old_mem = *new_mem;
  635. new_mem->mm_node = NULL;
  636. return 0;
  637. }
  638. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
  639. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  640. struct dma_fence *fence, bool evict,
  641. struct ttm_mem_reg *new_mem)
  642. {
  643. struct ttm_bo_device *bdev = bo->bdev;
  644. struct ttm_mem_reg *old_mem = &bo->mem;
  645. struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
  646. struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
  647. int ret;
  648. reservation_object_add_excl_fence(bo->resv, fence);
  649. if (!evict) {
  650. struct ttm_buffer_object *ghost_obj;
  651. /**
  652. * This should help pipeline ordinary buffer moves.
  653. *
  654. * Hang old buffer memory on a new buffer object,
  655. * and leave it to be released when the GPU
  656. * operation has completed.
  657. */
  658. dma_fence_put(bo->moving);
  659. bo->moving = dma_fence_get(fence);
  660. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  661. if (ret)
  662. return ret;
  663. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  664. /**
  665. * If we're not moving to fixed memory, the TTM object
  666. * needs to stay alive. Otherwhise hang it on the ghost
  667. * bo to be unbound and destroyed.
  668. */
  669. if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
  670. ghost_obj->ttm = NULL;
  671. else
  672. bo->ttm = NULL;
  673. ttm_bo_unreserve(ghost_obj);
  674. ttm_bo_unref(&ghost_obj);
  675. } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
  676. /**
  677. * BO doesn't have a TTM we need to bind/unbind. Just remember
  678. * this eviction and free up the allocation
  679. */
  680. spin_lock(&from->move_lock);
  681. if (!from->move || dma_fence_is_later(fence, from->move)) {
  682. dma_fence_put(from->move);
  683. from->move = dma_fence_get(fence);
  684. }
  685. spin_unlock(&from->move_lock);
  686. ttm_bo_free_old_node(bo);
  687. dma_fence_put(bo->moving);
  688. bo->moving = dma_fence_get(fence);
  689. } else {
  690. /**
  691. * Last resort, wait for the move to be completed.
  692. *
  693. * Should never happen in pratice.
  694. */
  695. ret = ttm_bo_wait(bo, false, false);
  696. if (ret)
  697. return ret;
  698. if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
  699. ttm_tt_destroy(bo->ttm);
  700. bo->ttm = NULL;
  701. }
  702. ttm_bo_free_old_node(bo);
  703. }
  704. *old_mem = *new_mem;
  705. new_mem->mm_node = NULL;
  706. return 0;
  707. }
  708. EXPORT_SYMBOL(ttm_bo_pipeline_move);
  709. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  710. {
  711. struct ttm_buffer_object *ghost;
  712. int ret;
  713. ret = ttm_buffer_object_transfer(bo, &ghost);
  714. if (ret)
  715. return ret;
  716. ret = reservation_object_copy_fences(ghost->resv, bo->resv);
  717. /* Last resort, wait for the BO to be idle when we are OOM */
  718. if (ret)
  719. ttm_bo_wait(bo, false, false);
  720. memset(&bo->mem, 0, sizeof(bo->mem));
  721. bo->mem.mem_type = TTM_PL_SYSTEM;
  722. bo->ttm = NULL;
  723. ttm_bo_unreserve(ghost);
  724. ttm_bo_unref(&ghost);
  725. return 0;
  726. }