ttm_bo_util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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_put(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->base.mem.placement |= TTM_PL_FLAG_NO_EVICT;
  422. ttm_bo_get(bo);
  423. fbo->bo = bo;
  424. /**
  425. * Fix up members that we shouldn't copy directly:
  426. * TODO: Explicit member copy would probably be better here.
  427. */
  428. atomic_inc(&bo->bdev->glob->bo_count);
  429. INIT_LIST_HEAD(&fbo->base.ddestroy);
  430. INIT_LIST_HEAD(&fbo->base.lru);
  431. INIT_LIST_HEAD(&fbo->base.swap);
  432. INIT_LIST_HEAD(&fbo->base.io_reserve_lru);
  433. mutex_init(&fbo->base.wu_mutex);
  434. fbo->base.moving = NULL;
  435. drm_vma_node_reset(&fbo->base.vma_node);
  436. atomic_set(&fbo->base.cpu_writers, 0);
  437. kref_init(&fbo->base.list_kref);
  438. kref_init(&fbo->base.kref);
  439. fbo->base.destroy = &ttm_transfered_destroy;
  440. fbo->base.acc_size = 0;
  441. fbo->base.resv = &fbo->base.ttm_resv;
  442. reservation_object_init(fbo->base.resv);
  443. ret = reservation_object_trylock(fbo->base.resv);
  444. WARN_ON(!ret);
  445. *new_obj = &fbo->base;
  446. return 0;
  447. }
  448. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  449. {
  450. /* Cached mappings need no adjustment */
  451. if (caching_flags & TTM_PL_FLAG_CACHED)
  452. return tmp;
  453. #if defined(__i386__) || defined(__x86_64__)
  454. if (caching_flags & TTM_PL_FLAG_WC)
  455. tmp = pgprot_writecombine(tmp);
  456. else if (boot_cpu_data.x86 > 3)
  457. tmp = pgprot_noncached(tmp);
  458. #endif
  459. #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
  460. defined(__powerpc__)
  461. if (caching_flags & TTM_PL_FLAG_WC)
  462. tmp = pgprot_writecombine(tmp);
  463. else
  464. tmp = pgprot_noncached(tmp);
  465. #endif
  466. #if defined(__sparc__) || defined(__mips__)
  467. tmp = pgprot_noncached(tmp);
  468. #endif
  469. return tmp;
  470. }
  471. EXPORT_SYMBOL(ttm_io_prot);
  472. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  473. unsigned long offset,
  474. unsigned long size,
  475. struct ttm_bo_kmap_obj *map)
  476. {
  477. struct ttm_mem_reg *mem = &bo->mem;
  478. if (bo->mem.bus.addr) {
  479. map->bo_kmap_type = ttm_bo_map_premapped;
  480. map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
  481. } else {
  482. map->bo_kmap_type = ttm_bo_map_iomap;
  483. if (mem->placement & TTM_PL_FLAG_WC)
  484. map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
  485. size);
  486. else
  487. map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
  488. size);
  489. }
  490. return (!map->virtual) ? -ENOMEM : 0;
  491. }
  492. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  493. unsigned long start_page,
  494. unsigned long num_pages,
  495. struct ttm_bo_kmap_obj *map)
  496. {
  497. struct ttm_mem_reg *mem = &bo->mem;
  498. struct ttm_operation_ctx ctx = {
  499. .interruptible = false,
  500. .no_wait_gpu = false
  501. };
  502. struct ttm_tt *ttm = bo->ttm;
  503. pgprot_t prot;
  504. int ret;
  505. BUG_ON(!ttm);
  506. ret = ttm_tt_populate(ttm, &ctx);
  507. if (ret)
  508. return ret;
  509. if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
  510. /*
  511. * We're mapping a single page, and the desired
  512. * page protection is consistent with the bo.
  513. */
  514. map->bo_kmap_type = ttm_bo_map_kmap;
  515. map->page = ttm->pages[start_page];
  516. map->virtual = kmap(map->page);
  517. } else {
  518. /*
  519. * We need to use vmap to get the desired page protection
  520. * or to make the buffer object look contiguous.
  521. */
  522. prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
  523. map->bo_kmap_type = ttm_bo_map_vmap;
  524. map->virtual = vmap(ttm->pages + start_page, num_pages,
  525. 0, prot);
  526. }
  527. return (!map->virtual) ? -ENOMEM : 0;
  528. }
  529. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  530. unsigned long start_page, unsigned long num_pages,
  531. struct ttm_bo_kmap_obj *map)
  532. {
  533. struct ttm_mem_type_manager *man =
  534. &bo->bdev->man[bo->mem.mem_type];
  535. unsigned long offset, size;
  536. int ret;
  537. map->virtual = NULL;
  538. map->bo = bo;
  539. if (num_pages > bo->num_pages)
  540. return -EINVAL;
  541. if (start_page > bo->num_pages)
  542. return -EINVAL;
  543. #if 0
  544. if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
  545. return -EPERM;
  546. #endif
  547. (void) ttm_mem_io_lock(man, false);
  548. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  549. ttm_mem_io_unlock(man);
  550. if (ret)
  551. return ret;
  552. if (!bo->mem.bus.is_iomem) {
  553. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  554. } else {
  555. offset = start_page << PAGE_SHIFT;
  556. size = num_pages << PAGE_SHIFT;
  557. return ttm_bo_ioremap(bo, offset, size, map);
  558. }
  559. }
  560. EXPORT_SYMBOL(ttm_bo_kmap);
  561. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  562. {
  563. struct ttm_buffer_object *bo = map->bo;
  564. struct ttm_mem_type_manager *man =
  565. &bo->bdev->man[bo->mem.mem_type];
  566. if (!map->virtual)
  567. return;
  568. switch (map->bo_kmap_type) {
  569. case ttm_bo_map_iomap:
  570. iounmap(map->virtual);
  571. break;
  572. case ttm_bo_map_vmap:
  573. vunmap(map->virtual);
  574. break;
  575. case ttm_bo_map_kmap:
  576. kunmap(map->page);
  577. break;
  578. case ttm_bo_map_premapped:
  579. break;
  580. default:
  581. BUG();
  582. }
  583. (void) ttm_mem_io_lock(man, false);
  584. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  585. ttm_mem_io_unlock(man);
  586. map->virtual = NULL;
  587. map->page = NULL;
  588. }
  589. EXPORT_SYMBOL(ttm_bo_kunmap);
  590. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  591. struct dma_fence *fence,
  592. bool evict,
  593. struct ttm_mem_reg *new_mem)
  594. {
  595. struct ttm_bo_device *bdev = bo->bdev;
  596. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  597. struct ttm_mem_reg *old_mem = &bo->mem;
  598. int ret;
  599. struct ttm_buffer_object *ghost_obj;
  600. reservation_object_add_excl_fence(bo->resv, fence);
  601. if (evict) {
  602. ret = ttm_bo_wait(bo, false, false);
  603. if (ret)
  604. return ret;
  605. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  606. ttm_tt_destroy(bo->ttm);
  607. bo->ttm = NULL;
  608. }
  609. ttm_bo_free_old_node(bo);
  610. } else {
  611. /**
  612. * This should help pipeline ordinary buffer moves.
  613. *
  614. * Hang old buffer memory on a new buffer object,
  615. * and leave it to be released when the GPU
  616. * operation has completed.
  617. */
  618. dma_fence_put(bo->moving);
  619. bo->moving = dma_fence_get(fence);
  620. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  621. if (ret)
  622. return ret;
  623. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  624. /**
  625. * If we're not moving to fixed memory, the TTM object
  626. * needs to stay alive. Otherwhise hang it on the ghost
  627. * bo to be unbound and destroyed.
  628. */
  629. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  630. ghost_obj->ttm = NULL;
  631. else
  632. bo->ttm = NULL;
  633. ttm_bo_unreserve(ghost_obj);
  634. ttm_bo_put(ghost_obj);
  635. }
  636. *old_mem = *new_mem;
  637. new_mem->mm_node = NULL;
  638. return 0;
  639. }
  640. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
  641. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  642. struct dma_fence *fence, bool evict,
  643. struct ttm_mem_reg *new_mem)
  644. {
  645. struct ttm_bo_device *bdev = bo->bdev;
  646. struct ttm_mem_reg *old_mem = &bo->mem;
  647. struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
  648. struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
  649. int ret;
  650. reservation_object_add_excl_fence(bo->resv, fence);
  651. if (!evict) {
  652. struct ttm_buffer_object *ghost_obj;
  653. /**
  654. * This should help pipeline ordinary buffer moves.
  655. *
  656. * Hang old buffer memory on a new buffer object,
  657. * and leave it to be released when the GPU
  658. * operation has completed.
  659. */
  660. dma_fence_put(bo->moving);
  661. bo->moving = dma_fence_get(fence);
  662. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  663. if (ret)
  664. return ret;
  665. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  666. /**
  667. * If we're not moving to fixed memory, the TTM object
  668. * needs to stay alive. Otherwhise hang it on the ghost
  669. * bo to be unbound and destroyed.
  670. */
  671. if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
  672. ghost_obj->ttm = NULL;
  673. else
  674. bo->ttm = NULL;
  675. ttm_bo_unreserve(ghost_obj);
  676. ttm_bo_put(ghost_obj);
  677. } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
  678. /**
  679. * BO doesn't have a TTM we need to bind/unbind. Just remember
  680. * this eviction and free up the allocation
  681. */
  682. spin_lock(&from->move_lock);
  683. if (!from->move || dma_fence_is_later(fence, from->move)) {
  684. dma_fence_put(from->move);
  685. from->move = dma_fence_get(fence);
  686. }
  687. spin_unlock(&from->move_lock);
  688. ttm_bo_free_old_node(bo);
  689. dma_fence_put(bo->moving);
  690. bo->moving = dma_fence_get(fence);
  691. } else {
  692. /**
  693. * Last resort, wait for the move to be completed.
  694. *
  695. * Should never happen in pratice.
  696. */
  697. ret = ttm_bo_wait(bo, false, false);
  698. if (ret)
  699. return ret;
  700. if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
  701. ttm_tt_destroy(bo->ttm);
  702. bo->ttm = NULL;
  703. }
  704. ttm_bo_free_old_node(bo);
  705. }
  706. *old_mem = *new_mem;
  707. new_mem->mm_node = NULL;
  708. return 0;
  709. }
  710. EXPORT_SYMBOL(ttm_bo_pipeline_move);
  711. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  712. {
  713. struct ttm_buffer_object *ghost;
  714. int ret;
  715. ret = ttm_buffer_object_transfer(bo, &ghost);
  716. if (ret)
  717. return ret;
  718. ret = reservation_object_copy_fences(ghost->resv, bo->resv);
  719. /* Last resort, wait for the BO to be idle when we are OOM */
  720. if (ret)
  721. ttm_bo_wait(bo, false, false);
  722. memset(&bo->mem, 0, sizeof(bo->mem));
  723. bo->mem.mem_type = TTM_PL_SYSTEM;
  724. bo->ttm = NULL;
  725. ttm_bo_unreserve(ghost);
  726. ttm_bo_put(ghost);
  727. return 0;
  728. }