ttm_bo_util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. struct ttm_operation_ctx *ctx,
  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, ctx->interruptible, ctx->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, ctx);
  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. #ifdef CONFIG_X86
  218. #define __ttm_kmap_atomic_prot(__page, __prot) kmap_atomic_prot(__page, __prot)
  219. #define __ttm_kunmap_atomic(__addr) kunmap_atomic(__addr)
  220. #else
  221. #define __ttm_kmap_atomic_prot(__page, __prot) vmap(&__page, 1, 0, __prot)
  222. #define __ttm_kunmap_atomic(__addr) vunmap(__addr)
  223. #endif
  224. /**
  225. * ttm_kmap_atomic_prot - Efficient kernel map of a single page with
  226. * specified page protection.
  227. *
  228. * @page: The page to map.
  229. * @prot: The page protection.
  230. *
  231. * This function maps a TTM page using the kmap_atomic api if available,
  232. * otherwise falls back to vmap. The user must make sure that the
  233. * specified page does not have an aliased mapping with a different caching
  234. * policy unless the architecture explicitly allows it. Also mapping and
  235. * unmapping using this api must be correctly nested. Unmapping should
  236. * occur in the reverse order of mapping.
  237. */
  238. void *ttm_kmap_atomic_prot(struct page *page, pgprot_t prot)
  239. {
  240. if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL))
  241. return kmap_atomic(page);
  242. else
  243. return __ttm_kmap_atomic_prot(page, prot);
  244. }
  245. EXPORT_SYMBOL(ttm_kmap_atomic_prot);
  246. /**
  247. * ttm_kunmap_atomic_prot - Unmap a page that was mapped using
  248. * ttm_kmap_atomic_prot.
  249. *
  250. * @addr: The virtual address from the map.
  251. * @prot: The page protection.
  252. */
  253. void ttm_kunmap_atomic_prot(void *addr, pgprot_t prot)
  254. {
  255. if (pgprot_val(prot) == pgprot_val(PAGE_KERNEL))
  256. kunmap_atomic(addr);
  257. else
  258. __ttm_kunmap_atomic(addr);
  259. }
  260. EXPORT_SYMBOL(ttm_kunmap_atomic_prot);
  261. static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
  262. unsigned long page,
  263. pgprot_t prot)
  264. {
  265. struct page *d = ttm->pages[page];
  266. void *dst;
  267. if (!d)
  268. return -ENOMEM;
  269. src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
  270. dst = ttm_kmap_atomic_prot(d, prot);
  271. if (!dst)
  272. return -ENOMEM;
  273. memcpy_fromio(dst, src, PAGE_SIZE);
  274. ttm_kunmap_atomic_prot(dst, prot);
  275. return 0;
  276. }
  277. static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
  278. unsigned long page,
  279. pgprot_t prot)
  280. {
  281. struct page *s = ttm->pages[page];
  282. void *src;
  283. if (!s)
  284. return -ENOMEM;
  285. dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
  286. src = ttm_kmap_atomic_prot(s, prot);
  287. if (!src)
  288. return -ENOMEM;
  289. memcpy_toio(dst, src, PAGE_SIZE);
  290. ttm_kunmap_atomic_prot(src, prot);
  291. return 0;
  292. }
  293. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  294. struct ttm_operation_ctx *ctx,
  295. struct ttm_mem_reg *new_mem)
  296. {
  297. struct ttm_bo_device *bdev = bo->bdev;
  298. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  299. struct ttm_tt *ttm = bo->ttm;
  300. struct ttm_mem_reg *old_mem = &bo->mem;
  301. struct ttm_mem_reg old_copy = *old_mem;
  302. void *old_iomap;
  303. void *new_iomap;
  304. int ret;
  305. unsigned long i;
  306. unsigned long page;
  307. unsigned long add = 0;
  308. int dir;
  309. ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
  310. if (ret)
  311. return ret;
  312. ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap);
  313. if (ret)
  314. return ret;
  315. ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap);
  316. if (ret)
  317. goto out;
  318. /*
  319. * Single TTM move. NOP.
  320. */
  321. if (old_iomap == NULL && new_iomap == NULL)
  322. goto out2;
  323. /*
  324. * Don't move nonexistent data. Clear destination instead.
  325. */
  326. if (old_iomap == NULL &&
  327. (ttm == NULL || (ttm->state == tt_unpopulated &&
  328. !(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)))) {
  329. memset_io(new_iomap, 0, new_mem->num_pages*PAGE_SIZE);
  330. goto out2;
  331. }
  332. /*
  333. * TTM might be null for moves within the same region.
  334. */
  335. if (ttm) {
  336. ret = ttm_tt_populate(ttm, ctx);
  337. if (ret)
  338. goto out1;
  339. }
  340. add = 0;
  341. dir = 1;
  342. if ((old_mem->mem_type == new_mem->mem_type) &&
  343. (new_mem->start < old_mem->start + old_mem->size)) {
  344. dir = -1;
  345. add = new_mem->num_pages - 1;
  346. }
  347. for (i = 0; i < new_mem->num_pages; ++i) {
  348. page = i * dir + add;
  349. if (old_iomap == NULL) {
  350. pgprot_t prot = ttm_io_prot(old_mem->placement,
  351. PAGE_KERNEL);
  352. ret = ttm_copy_ttm_io_page(ttm, new_iomap, page,
  353. prot);
  354. } else if (new_iomap == NULL) {
  355. pgprot_t prot = ttm_io_prot(new_mem->placement,
  356. PAGE_KERNEL);
  357. ret = ttm_copy_io_ttm_page(ttm, old_iomap, page,
  358. prot);
  359. } else {
  360. ret = ttm_copy_io_page(new_iomap, old_iomap, page);
  361. }
  362. if (ret)
  363. goto out1;
  364. }
  365. mb();
  366. out2:
  367. old_copy = *old_mem;
  368. *old_mem = *new_mem;
  369. new_mem->mm_node = NULL;
  370. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  371. ttm_tt_destroy(ttm);
  372. bo->ttm = NULL;
  373. }
  374. out1:
  375. ttm_mem_reg_iounmap(bdev, old_mem, new_iomap);
  376. out:
  377. ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap);
  378. /*
  379. * On error, keep the mm node!
  380. */
  381. if (!ret)
  382. ttm_bo_mem_put(bo, &old_copy);
  383. return ret;
  384. }
  385. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  386. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  387. {
  388. kfree(bo);
  389. }
  390. /**
  391. * ttm_buffer_object_transfer
  392. *
  393. * @bo: A pointer to a struct ttm_buffer_object.
  394. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  395. * holding the data of @bo with the old placement.
  396. *
  397. * This is a utility function that may be called after an accelerated move
  398. * has been scheduled. A new buffer object is created as a placeholder for
  399. * the old data while it's being copied. When that buffer object is idle,
  400. * it can be destroyed, releasing the space of the old placement.
  401. * Returns:
  402. * !0: Failure.
  403. */
  404. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  405. struct ttm_buffer_object **new_obj)
  406. {
  407. struct ttm_buffer_object *fbo;
  408. int ret;
  409. fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
  410. if (!fbo)
  411. return -ENOMEM;
  412. *fbo = *bo;
  413. /**
  414. * Fix up members that we shouldn't copy directly:
  415. * TODO: Explicit member copy would probably be better here.
  416. */
  417. atomic_inc(&bo->bdev->glob->bo_count);
  418. INIT_LIST_HEAD(&fbo->ddestroy);
  419. INIT_LIST_HEAD(&fbo->lru);
  420. INIT_LIST_HEAD(&fbo->swap);
  421. INIT_LIST_HEAD(&fbo->io_reserve_lru);
  422. mutex_init(&fbo->wu_mutex);
  423. fbo->moving = NULL;
  424. drm_vma_node_reset(&fbo->vma_node);
  425. atomic_set(&fbo->cpu_writers, 0);
  426. kref_init(&fbo->list_kref);
  427. kref_init(&fbo->kref);
  428. fbo->destroy = &ttm_transfered_destroy;
  429. fbo->acc_size = 0;
  430. fbo->resv = &fbo->ttm_resv;
  431. reservation_object_init(fbo->resv);
  432. ret = reservation_object_trylock(fbo->resv);
  433. WARN_ON(!ret);
  434. *new_obj = fbo;
  435. return 0;
  436. }
  437. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp)
  438. {
  439. /* Cached mappings need no adjustment */
  440. if (caching_flags & TTM_PL_FLAG_CACHED)
  441. return tmp;
  442. #if defined(__i386__) || defined(__x86_64__)
  443. if (caching_flags & TTM_PL_FLAG_WC)
  444. tmp = pgprot_writecombine(tmp);
  445. else if (boot_cpu_data.x86 > 3)
  446. tmp = pgprot_noncached(tmp);
  447. #endif
  448. #if defined(__ia64__) || defined(__arm__) || defined(__aarch64__) || \
  449. defined(__powerpc__)
  450. if (caching_flags & TTM_PL_FLAG_WC)
  451. tmp = pgprot_writecombine(tmp);
  452. else
  453. tmp = pgprot_noncached(tmp);
  454. #endif
  455. #if defined(__sparc__) || defined(__mips__)
  456. tmp = pgprot_noncached(tmp);
  457. #endif
  458. return tmp;
  459. }
  460. EXPORT_SYMBOL(ttm_io_prot);
  461. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  462. unsigned long offset,
  463. unsigned long size,
  464. struct ttm_bo_kmap_obj *map)
  465. {
  466. struct ttm_mem_reg *mem = &bo->mem;
  467. if (bo->mem.bus.addr) {
  468. map->bo_kmap_type = ttm_bo_map_premapped;
  469. map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset);
  470. } else {
  471. map->bo_kmap_type = ttm_bo_map_iomap;
  472. if (mem->placement & TTM_PL_FLAG_WC)
  473. map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset,
  474. size);
  475. else
  476. map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset,
  477. size);
  478. }
  479. return (!map->virtual) ? -ENOMEM : 0;
  480. }
  481. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  482. unsigned long start_page,
  483. unsigned long num_pages,
  484. struct ttm_bo_kmap_obj *map)
  485. {
  486. struct ttm_mem_reg *mem = &bo->mem;
  487. struct ttm_operation_ctx ctx = {
  488. .interruptible = false,
  489. .no_wait_gpu = false
  490. };
  491. struct ttm_tt *ttm = bo->ttm;
  492. pgprot_t prot;
  493. int ret;
  494. BUG_ON(!ttm);
  495. ret = ttm_tt_populate(ttm, &ctx);
  496. if (ret)
  497. return ret;
  498. if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
  499. /*
  500. * We're mapping a single page, and the desired
  501. * page protection is consistent with the bo.
  502. */
  503. map->bo_kmap_type = ttm_bo_map_kmap;
  504. map->page = ttm->pages[start_page];
  505. map->virtual = kmap(map->page);
  506. } else {
  507. /*
  508. * We need to use vmap to get the desired page protection
  509. * or to make the buffer object look contiguous.
  510. */
  511. prot = ttm_io_prot(mem->placement, PAGE_KERNEL);
  512. map->bo_kmap_type = ttm_bo_map_vmap;
  513. map->virtual = vmap(ttm->pages + start_page, num_pages,
  514. 0, prot);
  515. }
  516. return (!map->virtual) ? -ENOMEM : 0;
  517. }
  518. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  519. unsigned long start_page, unsigned long num_pages,
  520. struct ttm_bo_kmap_obj *map)
  521. {
  522. struct ttm_mem_type_manager *man =
  523. &bo->bdev->man[bo->mem.mem_type];
  524. unsigned long offset, size;
  525. int ret;
  526. map->virtual = NULL;
  527. map->bo = bo;
  528. if (num_pages > bo->num_pages)
  529. return -EINVAL;
  530. if (start_page > bo->num_pages)
  531. return -EINVAL;
  532. #if 0
  533. if (num_pages > 1 && !capable(CAP_SYS_ADMIN))
  534. return -EPERM;
  535. #endif
  536. (void) ttm_mem_io_lock(man, false);
  537. ret = ttm_mem_io_reserve(bo->bdev, &bo->mem);
  538. ttm_mem_io_unlock(man);
  539. if (ret)
  540. return ret;
  541. if (!bo->mem.bus.is_iomem) {
  542. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  543. } else {
  544. offset = start_page << PAGE_SHIFT;
  545. size = num_pages << PAGE_SHIFT;
  546. return ttm_bo_ioremap(bo, offset, size, map);
  547. }
  548. }
  549. EXPORT_SYMBOL(ttm_bo_kmap);
  550. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  551. {
  552. struct ttm_buffer_object *bo = map->bo;
  553. struct ttm_mem_type_manager *man =
  554. &bo->bdev->man[bo->mem.mem_type];
  555. if (!map->virtual)
  556. return;
  557. switch (map->bo_kmap_type) {
  558. case ttm_bo_map_iomap:
  559. iounmap(map->virtual);
  560. break;
  561. case ttm_bo_map_vmap:
  562. vunmap(map->virtual);
  563. break;
  564. case ttm_bo_map_kmap:
  565. kunmap(map->page);
  566. break;
  567. case ttm_bo_map_premapped:
  568. break;
  569. default:
  570. BUG();
  571. }
  572. (void) ttm_mem_io_lock(man, false);
  573. ttm_mem_io_free(map->bo->bdev, &map->bo->mem);
  574. ttm_mem_io_unlock(man);
  575. map->virtual = NULL;
  576. map->page = NULL;
  577. }
  578. EXPORT_SYMBOL(ttm_bo_kunmap);
  579. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  580. struct dma_fence *fence,
  581. bool evict,
  582. struct ttm_mem_reg *new_mem)
  583. {
  584. struct ttm_bo_device *bdev = bo->bdev;
  585. struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type];
  586. struct ttm_mem_reg *old_mem = &bo->mem;
  587. int ret;
  588. struct ttm_buffer_object *ghost_obj;
  589. reservation_object_add_excl_fence(bo->resv, fence);
  590. if (evict) {
  591. ret = ttm_bo_wait(bo, false, false);
  592. if (ret)
  593. return ret;
  594. if (man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  595. ttm_tt_destroy(bo->ttm);
  596. bo->ttm = NULL;
  597. }
  598. ttm_bo_free_old_node(bo);
  599. } else {
  600. /**
  601. * This should help pipeline ordinary buffer moves.
  602. *
  603. * Hang old buffer memory on a new buffer object,
  604. * and leave it to be released when the GPU
  605. * operation has completed.
  606. */
  607. dma_fence_put(bo->moving);
  608. bo->moving = dma_fence_get(fence);
  609. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  610. if (ret)
  611. return ret;
  612. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  613. /**
  614. * If we're not moving to fixed memory, the TTM object
  615. * needs to stay alive. Otherwhise hang it on the ghost
  616. * bo to be unbound and destroyed.
  617. */
  618. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED))
  619. ghost_obj->ttm = NULL;
  620. else
  621. bo->ttm = NULL;
  622. ttm_bo_unreserve(ghost_obj);
  623. ttm_bo_unref(&ghost_obj);
  624. }
  625. *old_mem = *new_mem;
  626. new_mem->mm_node = NULL;
  627. return 0;
  628. }
  629. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
  630. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  631. struct dma_fence *fence, bool evict,
  632. struct ttm_mem_reg *new_mem)
  633. {
  634. struct ttm_bo_device *bdev = bo->bdev;
  635. struct ttm_mem_reg *old_mem = &bo->mem;
  636. struct ttm_mem_type_manager *from = &bdev->man[old_mem->mem_type];
  637. struct ttm_mem_type_manager *to = &bdev->man[new_mem->mem_type];
  638. int ret;
  639. reservation_object_add_excl_fence(bo->resv, fence);
  640. if (!evict) {
  641. struct ttm_buffer_object *ghost_obj;
  642. /**
  643. * This should help pipeline ordinary buffer moves.
  644. *
  645. * Hang old buffer memory on a new buffer object,
  646. * and leave it to be released when the GPU
  647. * operation has completed.
  648. */
  649. dma_fence_put(bo->moving);
  650. bo->moving = dma_fence_get(fence);
  651. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  652. if (ret)
  653. return ret;
  654. reservation_object_add_excl_fence(ghost_obj->resv, fence);
  655. /**
  656. * If we're not moving to fixed memory, the TTM object
  657. * needs to stay alive. Otherwhise hang it on the ghost
  658. * bo to be unbound and destroyed.
  659. */
  660. if (!(to->flags & TTM_MEMTYPE_FLAG_FIXED))
  661. ghost_obj->ttm = NULL;
  662. else
  663. bo->ttm = NULL;
  664. ttm_bo_unreserve(ghost_obj);
  665. ttm_bo_unref(&ghost_obj);
  666. } else if (from->flags & TTM_MEMTYPE_FLAG_FIXED) {
  667. /**
  668. * BO doesn't have a TTM we need to bind/unbind. Just remember
  669. * this eviction and free up the allocation
  670. */
  671. spin_lock(&from->move_lock);
  672. if (!from->move || dma_fence_is_later(fence, from->move)) {
  673. dma_fence_put(from->move);
  674. from->move = dma_fence_get(fence);
  675. }
  676. spin_unlock(&from->move_lock);
  677. ttm_bo_free_old_node(bo);
  678. dma_fence_put(bo->moving);
  679. bo->moving = dma_fence_get(fence);
  680. } else {
  681. /**
  682. * Last resort, wait for the move to be completed.
  683. *
  684. * Should never happen in pratice.
  685. */
  686. ret = ttm_bo_wait(bo, false, false);
  687. if (ret)
  688. return ret;
  689. if (to->flags & TTM_MEMTYPE_FLAG_FIXED) {
  690. ttm_tt_destroy(bo->ttm);
  691. bo->ttm = NULL;
  692. }
  693. ttm_bo_free_old_node(bo);
  694. }
  695. *old_mem = *new_mem;
  696. new_mem->mm_node = NULL;
  697. return 0;
  698. }
  699. EXPORT_SYMBOL(ttm_bo_pipeline_move);
  700. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  701. {
  702. struct ttm_buffer_object *ghost;
  703. int ret;
  704. ret = ttm_buffer_object_transfer(bo, &ghost);
  705. if (ret)
  706. return ret;
  707. ret = reservation_object_copy_fences(ghost->resv, bo->resv);
  708. /* Last resort, wait for the BO to be idle when we are OOM */
  709. if (ret)
  710. ttm_bo_wait(bo, false, false);
  711. memset(&bo->mem, 0, sizeof(bo->mem));
  712. bo->mem.mem_type = TTM_PL_SYSTEM;
  713. bo->ttm = NULL;
  714. ttm_bo_unreserve(ghost);
  715. ttm_bo_unref(&ghost);
  716. return 0;
  717. }