ttm_bo.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-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. #define pr_fmt(fmt) "[TTM] " fmt
  32. #include <drm/ttm/ttm_bo.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <drm/ttm/ttm_tt.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/slab.h>
  37. #include <linux/sched.h>
  38. #include <linux/mm.h>
  39. #include <linux/file.h>
  40. #include <linux/module.h>
  41. #include <linux/atomic.h>
  42. #include <linux/dma-resv.h>
  43. #include "ttm_module.h"
  44. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  45. struct ttm_placement *placement)
  46. {
  47. struct drm_printer p = drm_dbg_printer(NULL, DRM_UT_CORE, TTM_PFX);
  48. struct ttm_resource_manager *man;
  49. int i, mem_type;
  50. for (i = 0; i < placement->num_placement; i++) {
  51. mem_type = placement->placement[i].mem_type;
  52. drm_printf(&p, " placement[%d]=0x%08X (%d)\n",
  53. i, placement->placement[i].flags, mem_type);
  54. man = ttm_manager_type(bo->bdev, mem_type);
  55. ttm_resource_manager_debug(man, &p);
  56. }
  57. }
  58. /**
  59. * ttm_bo_move_to_lru_tail
  60. *
  61. * @bo: The buffer object.
  62. *
  63. * Move this BO to the tail of all lru lists used to lookup and reserve an
  64. * object. This function must be called with struct ttm_global::lru_lock
  65. * held, and is used to make a BO less likely to be considered for eviction.
  66. */
  67. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
  68. {
  69. dma_resv_assert_held(bo->base.resv);
  70. if (bo->resource)
  71. ttm_resource_move_to_lru_tail(bo->resource);
  72. }
  73. EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
  74. /**
  75. * ttm_bo_set_bulk_move - update BOs bulk move object
  76. *
  77. * @bo: The buffer object.
  78. * @bulk: bulk move structure
  79. *
  80. * Update the BOs bulk move object, making sure that resources are added/removed
  81. * as well. A bulk move allows to move many resource on the LRU at once,
  82. * resulting in much less overhead of maintaining the LRU.
  83. * The only requirement is that the resources stay together on the LRU and are
  84. * never separated. This is enforces by setting the bulk_move structure on a BO.
  85. * ttm_lru_bulk_move_tail() should be used to move all resources to the tail of
  86. * their LRU list.
  87. */
  88. void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
  89. struct ttm_lru_bulk_move *bulk)
  90. {
  91. dma_resv_assert_held(bo->base.resv);
  92. if (bo->bulk_move == bulk)
  93. return;
  94. spin_lock(&bo->bdev->lru_lock);
  95. if (bo->resource)
  96. ttm_resource_del_bulk_move(bo->resource, bo);
  97. bo->bulk_move = bulk;
  98. if (bo->resource)
  99. ttm_resource_add_bulk_move(bo->resource, bo);
  100. spin_unlock(&bo->bdev->lru_lock);
  101. }
  102. EXPORT_SYMBOL(ttm_bo_set_bulk_move);
  103. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  104. struct ttm_resource *mem, bool evict,
  105. struct ttm_operation_ctx *ctx,
  106. struct ttm_place *hop)
  107. {
  108. struct ttm_device *bdev = bo->bdev;
  109. bool old_use_tt, new_use_tt;
  110. int ret;
  111. old_use_tt = !bo->resource || ttm_manager_type(bdev, bo->resource->mem_type)->use_tt;
  112. new_use_tt = ttm_manager_type(bdev, mem->mem_type)->use_tt;
  113. ttm_bo_unmap_virtual(bo);
  114. /*
  115. * Create and bind a ttm if required.
  116. */
  117. if (new_use_tt) {
  118. /* Zero init the new TTM structure if the old location should
  119. * have used one as well.
  120. */
  121. ret = ttm_tt_create(bo, old_use_tt);
  122. if (ret)
  123. goto out_err;
  124. if (mem->mem_type != TTM_PL_SYSTEM) {
  125. ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
  126. if (ret)
  127. goto out_err;
  128. }
  129. }
  130. ret = dma_resv_reserve_fences(bo->base.resv, 1);
  131. if (ret)
  132. goto out_err;
  133. ret = bdev->funcs->move(bo, evict, ctx, mem, hop);
  134. if (ret) {
  135. if (ret == -EMULTIHOP)
  136. return ret;
  137. goto out_err;
  138. }
  139. ctx->bytes_moved += bo->base.size;
  140. return 0;
  141. out_err:
  142. if (!old_use_tt)
  143. ttm_bo_tt_destroy(bo);
  144. return ret;
  145. }
  146. /*
  147. * Call bo::reserved.
  148. * Will release GPU memory type usage on destruction.
  149. * This is the place to put in driver specific hooks to release
  150. * driver private resources.
  151. * Will release the bo::reserved lock.
  152. */
  153. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  154. {
  155. if (bo->bdev->funcs->delete_mem_notify)
  156. bo->bdev->funcs->delete_mem_notify(bo);
  157. ttm_bo_tt_destroy(bo);
  158. ttm_resource_free(bo, &bo->resource);
  159. }
  160. static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
  161. {
  162. int r;
  163. if (bo->base.resv == &bo->base._resv)
  164. return 0;
  165. BUG_ON(!dma_resv_trylock(&bo->base._resv));
  166. r = dma_resv_copy_fences(&bo->base._resv, bo->base.resv);
  167. dma_resv_unlock(&bo->base._resv);
  168. if (r)
  169. return r;
  170. if (bo->type != ttm_bo_type_sg) {
  171. /* This works because the BO is about to be destroyed and nobody
  172. * reference it any more. The only tricky case is the trylock on
  173. * the resv object while holding the lru_lock.
  174. */
  175. spin_lock(&bo->bdev->lru_lock);
  176. bo->base.resv = &bo->base._resv;
  177. spin_unlock(&bo->bdev->lru_lock);
  178. }
  179. return r;
  180. }
  181. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  182. {
  183. struct dma_resv *resv = &bo->base._resv;
  184. struct dma_resv_iter cursor;
  185. struct dma_fence *fence;
  186. dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
  187. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  188. if (!fence->ops->signaled)
  189. dma_fence_enable_sw_signaling(fence);
  190. }
  191. dma_resv_iter_end(&cursor);
  192. }
  193. /*
  194. * Block for the dma_resv object to become idle, lock the buffer and clean up
  195. * the resource and tt object.
  196. */
  197. static void ttm_bo_delayed_delete(struct work_struct *work)
  198. {
  199. struct ttm_buffer_object *bo;
  200. bo = container_of(work, typeof(*bo), delayed_delete);
  201. dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, false,
  202. MAX_SCHEDULE_TIMEOUT);
  203. dma_resv_lock(bo->base.resv, NULL);
  204. ttm_bo_cleanup_memtype_use(bo);
  205. dma_resv_unlock(bo->base.resv);
  206. ttm_bo_put(bo);
  207. }
  208. static void ttm_bo_release(struct kref *kref)
  209. {
  210. struct ttm_buffer_object *bo =
  211. container_of(kref, struct ttm_buffer_object, kref);
  212. struct ttm_device *bdev = bo->bdev;
  213. int ret;
  214. WARN_ON_ONCE(bo->pin_count);
  215. WARN_ON_ONCE(bo->bulk_move);
  216. if (!bo->deleted) {
  217. ret = ttm_bo_individualize_resv(bo);
  218. if (ret) {
  219. /* Last resort, if we fail to allocate memory for the
  220. * fences block for the BO to become idle
  221. */
  222. dma_resv_wait_timeout(bo->base.resv,
  223. DMA_RESV_USAGE_BOOKKEEP, false,
  224. 30 * HZ);
  225. }
  226. if (bo->bdev->funcs->release_notify)
  227. bo->bdev->funcs->release_notify(bo);
  228. drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node);
  229. ttm_mem_io_free(bdev, bo->resource);
  230. if (!dma_resv_test_signaled(bo->base.resv,
  231. DMA_RESV_USAGE_BOOKKEEP) ||
  232. (want_init_on_free() && (bo->ttm != NULL)) ||
  233. bo->type == ttm_bo_type_sg ||
  234. !dma_resv_trylock(bo->base.resv)) {
  235. /* The BO is not idle, resurrect it for delayed destroy */
  236. ttm_bo_flush_all_fences(bo);
  237. bo->deleted = true;
  238. spin_lock(&bo->bdev->lru_lock);
  239. /*
  240. * Make pinned bos immediately available to
  241. * shrinkers, now that they are queued for
  242. * destruction.
  243. *
  244. * FIXME: QXL is triggering this. Can be removed when the
  245. * driver is fixed.
  246. */
  247. if (bo->pin_count) {
  248. bo->pin_count = 0;
  249. ttm_resource_move_to_lru_tail(bo->resource);
  250. }
  251. kref_init(&bo->kref);
  252. spin_unlock(&bo->bdev->lru_lock);
  253. INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete);
  254. /* Schedule the worker on the closest NUMA node. This
  255. * improves performance since system memory might be
  256. * cleared on free and that is best done on a CPU core
  257. * close to it.
  258. */
  259. queue_work_node(bdev->pool.nid, bdev->wq, &bo->delayed_delete);
  260. return;
  261. }
  262. ttm_bo_cleanup_memtype_use(bo);
  263. dma_resv_unlock(bo->base.resv);
  264. }
  265. atomic_dec(&ttm_glob.bo_count);
  266. bo->destroy(bo);
  267. }
  268. /**
  269. * ttm_bo_put
  270. *
  271. * @bo: The buffer object.
  272. *
  273. * Unreference a buffer object.
  274. */
  275. void ttm_bo_put(struct ttm_buffer_object *bo)
  276. {
  277. kref_put(&bo->kref, ttm_bo_release);
  278. }
  279. EXPORT_SYMBOL(ttm_bo_put);
  280. static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
  281. struct ttm_operation_ctx *ctx,
  282. struct ttm_place *hop)
  283. {
  284. struct ttm_placement hop_placement;
  285. struct ttm_resource *hop_mem;
  286. int ret;
  287. hop_placement.num_placement = 1;
  288. hop_placement.placement = hop;
  289. /* find space in the bounce domain */
  290. ret = ttm_bo_mem_space(bo, &hop_placement, &hop_mem, ctx);
  291. if (ret)
  292. return ret;
  293. /* move to the bounce domain */
  294. ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
  295. if (ret) {
  296. ttm_resource_free(bo, &hop_mem);
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. static int ttm_bo_evict(struct ttm_buffer_object *bo,
  302. struct ttm_operation_ctx *ctx)
  303. {
  304. struct ttm_device *bdev = bo->bdev;
  305. struct ttm_resource *evict_mem;
  306. struct ttm_placement placement;
  307. struct ttm_place hop;
  308. int ret = 0;
  309. memset(&hop, 0, sizeof(hop));
  310. dma_resv_assert_held(bo->base.resv);
  311. placement.num_placement = 0;
  312. bdev->funcs->evict_flags(bo, &placement);
  313. if (!placement.num_placement) {
  314. ret = ttm_bo_wait_ctx(bo, ctx);
  315. if (ret)
  316. return ret;
  317. /*
  318. * Since we've already synced, this frees backing store
  319. * immediately.
  320. */
  321. return ttm_bo_pipeline_gutting(bo);
  322. }
  323. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
  324. if (ret) {
  325. if (ret != -ERESTARTSYS) {
  326. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  327. bo);
  328. ttm_bo_mem_space_debug(bo, &placement);
  329. }
  330. goto out;
  331. }
  332. do {
  333. ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
  334. if (ret != -EMULTIHOP)
  335. break;
  336. ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
  337. } while (!ret);
  338. if (ret) {
  339. ttm_resource_free(bo, &evict_mem);
  340. if (ret != -ERESTARTSYS && ret != -EINTR)
  341. pr_err("Buffer eviction failed\n");
  342. }
  343. out:
  344. return ret;
  345. }
  346. /**
  347. * ttm_bo_eviction_valuable
  348. *
  349. * @bo: The buffer object to evict
  350. * @place: the placement we need to make room for
  351. *
  352. * Check if it is valuable to evict the BO to make room for the given placement.
  353. */
  354. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  355. const struct ttm_place *place)
  356. {
  357. struct ttm_resource *res = bo->resource;
  358. struct ttm_device *bdev = bo->bdev;
  359. dma_resv_assert_held(bo->base.resv);
  360. if (bo->resource->mem_type == TTM_PL_SYSTEM)
  361. return true;
  362. /* Don't evict this BO if it's outside of the
  363. * requested placement range
  364. */
  365. return ttm_resource_intersects(bdev, res, place, bo->base.size);
  366. }
  367. EXPORT_SYMBOL(ttm_bo_eviction_valuable);
  368. /**
  369. * ttm_bo_evict_first() - Evict the first bo on the manager's LRU list.
  370. * @bdev: The ttm device.
  371. * @man: The manager whose bo to evict.
  372. * @ctx: The TTM operation ctx governing the eviction.
  373. *
  374. * Return: 0 if successful or the resource disappeared. Negative error code on error.
  375. */
  376. int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man,
  377. struct ttm_operation_ctx *ctx)
  378. {
  379. struct ttm_resource_cursor cursor;
  380. struct ttm_buffer_object *bo;
  381. struct ttm_resource *res;
  382. unsigned int mem_type;
  383. int ret = 0;
  384. spin_lock(&bdev->lru_lock);
  385. res = ttm_resource_manager_first(man, &cursor);
  386. ttm_resource_cursor_fini(&cursor);
  387. if (!res) {
  388. ret = -ENOENT;
  389. goto out_no_ref;
  390. }
  391. bo = res->bo;
  392. if (!ttm_bo_get_unless_zero(bo))
  393. goto out_no_ref;
  394. mem_type = res->mem_type;
  395. spin_unlock(&bdev->lru_lock);
  396. ret = ttm_bo_reserve(bo, ctx->interruptible, ctx->no_wait_gpu, NULL);
  397. if (ret)
  398. goto out_no_lock;
  399. if (!bo->resource || bo->resource->mem_type != mem_type)
  400. goto out_bo_moved;
  401. if (bo->deleted) {
  402. ret = ttm_bo_wait_ctx(bo, ctx);
  403. if (!ret)
  404. ttm_bo_cleanup_memtype_use(bo);
  405. } else {
  406. ret = ttm_bo_evict(bo, ctx);
  407. }
  408. out_bo_moved:
  409. dma_resv_unlock(bo->base.resv);
  410. out_no_lock:
  411. ttm_bo_put(bo);
  412. return ret;
  413. out_no_ref:
  414. spin_unlock(&bdev->lru_lock);
  415. return ret;
  416. }
  417. /**
  418. * struct ttm_bo_evict_walk - Parameters for the evict walk.
  419. */
  420. struct ttm_bo_evict_walk {
  421. /** @walk: The walk base parameters. */
  422. struct ttm_lru_walk walk;
  423. /** @place: The place passed to the resource allocation. */
  424. const struct ttm_place *place;
  425. /** @evictor: The buffer object we're trying to make room for. */
  426. struct ttm_buffer_object *evictor;
  427. /** @res: The allocated resource if any. */
  428. struct ttm_resource **res;
  429. /** @evicted: Number of successful evictions. */
  430. unsigned long evicted;
  431. };
  432. static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
  433. {
  434. struct ttm_bo_evict_walk *evict_walk =
  435. container_of(walk, typeof(*evict_walk), walk);
  436. s64 lret;
  437. if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->place))
  438. return 0;
  439. if (bo->deleted) {
  440. lret = ttm_bo_wait_ctx(bo, walk->ctx);
  441. if (!lret)
  442. ttm_bo_cleanup_memtype_use(bo);
  443. } else {
  444. lret = ttm_bo_evict(bo, walk->ctx);
  445. }
  446. if (lret)
  447. goto out;
  448. evict_walk->evicted++;
  449. if (evict_walk->res)
  450. lret = ttm_resource_alloc(evict_walk->evictor, evict_walk->place,
  451. evict_walk->res);
  452. if (lret == 0)
  453. return 1;
  454. out:
  455. /* Errors that should terminate the walk. */
  456. if (lret == -ENOSPC)
  457. return -EBUSY;
  458. return lret;
  459. }
  460. static const struct ttm_lru_walk_ops ttm_evict_walk_ops = {
  461. .process_bo = ttm_bo_evict_cb,
  462. };
  463. static int ttm_bo_evict_alloc(struct ttm_device *bdev,
  464. struct ttm_resource_manager *man,
  465. const struct ttm_place *place,
  466. struct ttm_buffer_object *evictor,
  467. struct ttm_operation_ctx *ctx,
  468. struct ww_acquire_ctx *ticket,
  469. struct ttm_resource **res)
  470. {
  471. struct ttm_bo_evict_walk evict_walk = {
  472. .walk = {
  473. .ops = &ttm_evict_walk_ops,
  474. .ctx = ctx,
  475. .ticket = ticket,
  476. },
  477. .place = place,
  478. .evictor = evictor,
  479. .res = res,
  480. };
  481. s64 lret;
  482. evict_walk.walk.trylock_only = true;
  483. lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
  484. if (lret || !ticket)
  485. goto out;
  486. /* If ticket-locking, repeat while making progress. */
  487. evict_walk.walk.trylock_only = false;
  488. do {
  489. /* The walk may clear the evict_walk.walk.ticket field */
  490. evict_walk.walk.ticket = ticket;
  491. evict_walk.evicted = 0;
  492. lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1);
  493. } while (!lret && evict_walk.evicted);
  494. out:
  495. if (lret < 0)
  496. return lret;
  497. if (lret == 0)
  498. return -EBUSY;
  499. return 0;
  500. }
  501. /**
  502. * ttm_bo_pin - Pin the buffer object.
  503. * @bo: The buffer object to pin
  504. *
  505. * Make sure the buffer is not evicted any more during memory pressure.
  506. * @bo must be unpinned again by calling ttm_bo_unpin().
  507. */
  508. void ttm_bo_pin(struct ttm_buffer_object *bo)
  509. {
  510. dma_resv_assert_held(bo->base.resv);
  511. WARN_ON_ONCE(!kref_read(&bo->kref));
  512. spin_lock(&bo->bdev->lru_lock);
  513. if (bo->resource)
  514. ttm_resource_del_bulk_move(bo->resource, bo);
  515. ++bo->pin_count;
  516. spin_unlock(&bo->bdev->lru_lock);
  517. }
  518. EXPORT_SYMBOL(ttm_bo_pin);
  519. /**
  520. * ttm_bo_unpin - Unpin the buffer object.
  521. * @bo: The buffer object to unpin
  522. *
  523. * Allows the buffer object to be evicted again during memory pressure.
  524. */
  525. void ttm_bo_unpin(struct ttm_buffer_object *bo)
  526. {
  527. dma_resv_assert_held(bo->base.resv);
  528. WARN_ON_ONCE(!kref_read(&bo->kref));
  529. if (WARN_ON_ONCE(!bo->pin_count))
  530. return;
  531. spin_lock(&bo->bdev->lru_lock);
  532. --bo->pin_count;
  533. if (bo->resource)
  534. ttm_resource_add_bulk_move(bo->resource, bo);
  535. spin_unlock(&bo->bdev->lru_lock);
  536. }
  537. EXPORT_SYMBOL(ttm_bo_unpin);
  538. /*
  539. * Add the last move fence to the BO as kernel dependency and reserve a new
  540. * fence slot.
  541. */
  542. static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
  543. struct ttm_resource_manager *man,
  544. bool no_wait_gpu)
  545. {
  546. struct dma_fence *fence;
  547. int ret;
  548. spin_lock(&man->move_lock);
  549. fence = dma_fence_get(man->move);
  550. spin_unlock(&man->move_lock);
  551. if (!fence)
  552. return 0;
  553. if (no_wait_gpu) {
  554. ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY;
  555. dma_fence_put(fence);
  556. return ret;
  557. }
  558. dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
  559. ret = dma_resv_reserve_fences(bo->base.resv, 1);
  560. dma_fence_put(fence);
  561. return ret;
  562. }
  563. /**
  564. * ttm_bo_alloc_resource - Allocate backing store for a BO
  565. *
  566. * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
  567. * @placement: Proposed new placement for the buffer object
  568. * @ctx: if and how to sleep, lock buffers and alloc memory
  569. * @force_space: If we should evict buffers to force space
  570. * @res: The resulting struct ttm_resource.
  571. *
  572. * Allocates a resource for the buffer object pointed to by @bo, using the
  573. * placement flags in @placement, potentially evicting other buffer objects when
  574. * @force_space is true.
  575. * This function may sleep while waiting for resources to become available.
  576. * Returns:
  577. * -EBUSY: No space available (only if no_wait == true).
  578. * -ENOSPC: Could not allocate space for the buffer object, either due to
  579. * fragmentation or concurrent allocators.
  580. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  581. */
  582. static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo,
  583. struct ttm_placement *placement,
  584. struct ttm_operation_ctx *ctx,
  585. bool force_space,
  586. struct ttm_resource **res)
  587. {
  588. struct ttm_device *bdev = bo->bdev;
  589. struct ww_acquire_ctx *ticket;
  590. int i, ret;
  591. ticket = dma_resv_locking_ctx(bo->base.resv);
  592. ret = dma_resv_reserve_fences(bo->base.resv, 1);
  593. if (unlikely(ret))
  594. return ret;
  595. for (i = 0; i < placement->num_placement; ++i) {
  596. const struct ttm_place *place = &placement->placement[i];
  597. struct ttm_resource_manager *man;
  598. bool may_evict;
  599. man = ttm_manager_type(bdev, place->mem_type);
  600. if (!man || !ttm_resource_manager_used(man))
  601. continue;
  602. if (place->flags & (force_space ? TTM_PL_FLAG_DESIRED :
  603. TTM_PL_FLAG_FALLBACK))
  604. continue;
  605. may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM);
  606. ret = ttm_resource_alloc(bo, place, res);
  607. if (ret) {
  608. if (ret != -ENOSPC)
  609. return ret;
  610. if (!may_evict)
  611. continue;
  612. ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx,
  613. ticket, res);
  614. if (ret == -EBUSY)
  615. continue;
  616. if (ret)
  617. return ret;
  618. }
  619. ret = ttm_bo_add_move_fence(bo, man, ctx->no_wait_gpu);
  620. if (unlikely(ret)) {
  621. ttm_resource_free(bo, res);
  622. if (ret == -EBUSY)
  623. continue;
  624. return ret;
  625. }
  626. return 0;
  627. }
  628. return -ENOSPC;
  629. }
  630. /*
  631. * ttm_bo_mem_space - Wrapper around ttm_bo_alloc_resource
  632. *
  633. * @bo: Pointer to a struct ttm_buffer_object of which we want a resource for
  634. * @placement: Proposed new placement for the buffer object
  635. * @res: The resulting struct ttm_resource.
  636. * @ctx: if and how to sleep, lock buffers and alloc memory
  637. *
  638. * Tries both idle allocation and forcefully eviction of buffers. See
  639. * ttm_bo_alloc_resource for details.
  640. */
  641. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  642. struct ttm_placement *placement,
  643. struct ttm_resource **res,
  644. struct ttm_operation_ctx *ctx)
  645. {
  646. bool force_space = false;
  647. int ret;
  648. do {
  649. ret = ttm_bo_alloc_resource(bo, placement, ctx,
  650. force_space, res);
  651. force_space = !force_space;
  652. } while (ret == -ENOSPC && force_space);
  653. return ret;
  654. }
  655. EXPORT_SYMBOL(ttm_bo_mem_space);
  656. /**
  657. * ttm_bo_validate
  658. *
  659. * @bo: The buffer object.
  660. * @placement: Proposed placement for the buffer object.
  661. * @ctx: validation parameters.
  662. *
  663. * Changes placement and caching policy of the buffer object
  664. * according proposed placement.
  665. * Returns
  666. * -EINVAL on invalid proposed placement.
  667. * -ENOMEM on out-of-memory condition.
  668. * -EBUSY if no_wait is true and buffer busy.
  669. * -ERESTARTSYS if interrupted by a signal.
  670. */
  671. int ttm_bo_validate(struct ttm_buffer_object *bo,
  672. struct ttm_placement *placement,
  673. struct ttm_operation_ctx *ctx)
  674. {
  675. struct ttm_resource *res;
  676. struct ttm_place hop;
  677. bool force_space;
  678. int ret;
  679. dma_resv_assert_held(bo->base.resv);
  680. /*
  681. * Remove the backing store if no placement is given.
  682. */
  683. if (!placement->num_placement)
  684. return ttm_bo_pipeline_gutting(bo);
  685. force_space = false;
  686. do {
  687. /* Check whether we need to move buffer. */
  688. if (bo->resource &&
  689. ttm_resource_compatible(bo->resource, placement,
  690. force_space))
  691. return 0;
  692. /* Moving of pinned BOs is forbidden */
  693. if (bo->pin_count)
  694. return -EINVAL;
  695. /*
  696. * Determine where to move the buffer.
  697. *
  698. * If driver determines move is going to need
  699. * an extra step then it will return -EMULTIHOP
  700. * and the buffer will be moved to the temporary
  701. * stop and the driver will be called to make
  702. * the second hop.
  703. */
  704. ret = ttm_bo_alloc_resource(bo, placement, ctx, force_space,
  705. &res);
  706. force_space = !force_space;
  707. if (ret == -ENOSPC)
  708. continue;
  709. if (ret)
  710. return ret;
  711. bounce:
  712. ret = ttm_bo_handle_move_mem(bo, res, false, ctx, &hop);
  713. if (ret == -EMULTIHOP) {
  714. ret = ttm_bo_bounce_temp_buffer(bo, ctx, &hop);
  715. /* try and move to final place now. */
  716. if (!ret)
  717. goto bounce;
  718. }
  719. if (ret) {
  720. ttm_resource_free(bo, &res);
  721. return ret;
  722. }
  723. } while (ret && force_space);
  724. /* For backward compatibility with userspace */
  725. if (ret == -ENOSPC)
  726. return -ENOMEM;
  727. /*
  728. * We might need to add a TTM.
  729. */
  730. if (!bo->resource || bo->resource->mem_type == TTM_PL_SYSTEM) {
  731. ret = ttm_tt_create(bo, true);
  732. if (ret)
  733. return ret;
  734. }
  735. return 0;
  736. }
  737. EXPORT_SYMBOL(ttm_bo_validate);
  738. /**
  739. * ttm_bo_init_reserved
  740. *
  741. * @bdev: Pointer to a ttm_device struct.
  742. * @bo: Pointer to a ttm_buffer_object to be initialized.
  743. * @type: Requested type of buffer object.
  744. * @placement: Initial placement for buffer object.
  745. * @alignment: Data alignment in pages.
  746. * @ctx: TTM operation context for memory allocation.
  747. * @sg: Scatter-gather table.
  748. * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
  749. * @destroy: Destroy function. Use NULL for kfree().
  750. *
  751. * This function initializes a pre-allocated struct ttm_buffer_object.
  752. * As this object may be part of a larger structure, this function,
  753. * together with the @destroy function, enables driver-specific objects
  754. * derived from a ttm_buffer_object.
  755. *
  756. * On successful return, the caller owns an object kref to @bo. The kref and
  757. * list_kref are usually set to 1, but note that in some situations, other
  758. * tasks may already be holding references to @bo as well.
  759. * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
  760. * and it is the caller's responsibility to call ttm_bo_unreserve.
  761. *
  762. * If a failure occurs, the function will call the @destroy function. Thus,
  763. * after a failure, dereferencing @bo is illegal and will likely cause memory
  764. * corruption.
  765. *
  766. * Returns
  767. * -ENOMEM: Out of memory.
  768. * -EINVAL: Invalid placement flags.
  769. * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
  770. */
  771. int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
  772. enum ttm_bo_type type, struct ttm_placement *placement,
  773. uint32_t alignment, struct ttm_operation_ctx *ctx,
  774. struct sg_table *sg, struct dma_resv *resv,
  775. void (*destroy) (struct ttm_buffer_object *))
  776. {
  777. int ret;
  778. kref_init(&bo->kref);
  779. bo->bdev = bdev;
  780. bo->type = type;
  781. bo->page_alignment = alignment;
  782. bo->destroy = destroy;
  783. bo->pin_count = 0;
  784. bo->sg = sg;
  785. bo->bulk_move = NULL;
  786. if (resv)
  787. bo->base.resv = resv;
  788. else
  789. bo->base.resv = &bo->base._resv;
  790. atomic_inc(&ttm_glob.bo_count);
  791. /*
  792. * For ttm_bo_type_device buffers, allocate
  793. * address space from the device.
  794. */
  795. if (bo->type == ttm_bo_type_device || bo->type == ttm_bo_type_sg) {
  796. ret = drm_vma_offset_add(bdev->vma_manager, &bo->base.vma_node,
  797. PFN_UP(bo->base.size));
  798. if (ret)
  799. goto err_put;
  800. }
  801. /* passed reservation objects should already be locked,
  802. * since otherwise lockdep will be angered in radeon.
  803. */
  804. if (!resv)
  805. WARN_ON(!dma_resv_trylock(bo->base.resv));
  806. else
  807. dma_resv_assert_held(resv);
  808. ret = ttm_bo_validate(bo, placement, ctx);
  809. if (unlikely(ret))
  810. goto err_unlock;
  811. return 0;
  812. err_unlock:
  813. if (!resv)
  814. dma_resv_unlock(bo->base.resv);
  815. err_put:
  816. ttm_bo_put(bo);
  817. return ret;
  818. }
  819. EXPORT_SYMBOL(ttm_bo_init_reserved);
  820. /**
  821. * ttm_bo_init_validate
  822. *
  823. * @bdev: Pointer to a ttm_device struct.
  824. * @bo: Pointer to a ttm_buffer_object to be initialized.
  825. * @type: Requested type of buffer object.
  826. * @placement: Initial placement for buffer object.
  827. * @alignment: Data alignment in pages.
  828. * @interruptible: If needing to sleep to wait for GPU resources,
  829. * sleep interruptible.
  830. * pinned in physical memory. If this behaviour is not desired, this member
  831. * holds a pointer to a persistent shmem object. Typically, this would
  832. * point to the shmem object backing a GEM object if TTM is used to back a
  833. * GEM user interface.
  834. * @sg: Scatter-gather table.
  835. * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
  836. * @destroy: Destroy function. Use NULL for kfree().
  837. *
  838. * This function initializes a pre-allocated struct ttm_buffer_object.
  839. * As this object may be part of a larger structure, this function,
  840. * together with the @destroy function,
  841. * enables driver-specific objects derived from a ttm_buffer_object.
  842. *
  843. * On successful return, the caller owns an object kref to @bo. The kref and
  844. * list_kref are usually set to 1, but note that in some situations, other
  845. * tasks may already be holding references to @bo as well.
  846. *
  847. * If a failure occurs, the function will call the @destroy function, Thus,
  848. * after a failure, dereferencing @bo is illegal and will likely cause memory
  849. * corruption.
  850. *
  851. * Returns
  852. * -ENOMEM: Out of memory.
  853. * -EINVAL: Invalid placement flags.
  854. * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
  855. */
  856. int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
  857. enum ttm_bo_type type, struct ttm_placement *placement,
  858. uint32_t alignment, bool interruptible,
  859. struct sg_table *sg, struct dma_resv *resv,
  860. void (*destroy) (struct ttm_buffer_object *))
  861. {
  862. struct ttm_operation_ctx ctx = { interruptible, false };
  863. int ret;
  864. ret = ttm_bo_init_reserved(bdev, bo, type, placement, alignment, &ctx,
  865. sg, resv, destroy);
  866. if (ret)
  867. return ret;
  868. if (!resv)
  869. ttm_bo_unreserve(bo);
  870. return 0;
  871. }
  872. EXPORT_SYMBOL(ttm_bo_init_validate);
  873. /*
  874. * buffer object vm functions.
  875. */
  876. /**
  877. * ttm_bo_unmap_virtual
  878. *
  879. * @bo: tear down the virtual mappings for this BO
  880. */
  881. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  882. {
  883. struct ttm_device *bdev = bo->bdev;
  884. drm_vma_node_unmap(&bo->base.vma_node, bdev->dev_mapping);
  885. ttm_mem_io_free(bdev, bo->resource);
  886. }
  887. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  888. /**
  889. * ttm_bo_wait_ctx - wait for buffer idle.
  890. *
  891. * @bo: The buffer object.
  892. * @ctx: defines how to wait
  893. *
  894. * Waits for the buffer to be idle. Used timeout depends on the context.
  895. * Returns -EBUSY if wait timed outt, -ERESTARTSYS if interrupted by a signal or
  896. * zero on success.
  897. */
  898. int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
  899. {
  900. long ret;
  901. if (ctx->no_wait_gpu) {
  902. if (dma_resv_test_signaled(bo->base.resv,
  903. DMA_RESV_USAGE_BOOKKEEP))
  904. return 0;
  905. else
  906. return -EBUSY;
  907. }
  908. ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
  909. ctx->interruptible, 15 * HZ);
  910. if (unlikely(ret < 0))
  911. return ret;
  912. if (unlikely(ret == 0))
  913. return -EBUSY;
  914. return 0;
  915. }
  916. EXPORT_SYMBOL(ttm_bo_wait_ctx);
  917. /**
  918. * struct ttm_bo_swapout_walk - Parameters for the swapout walk
  919. */
  920. struct ttm_bo_swapout_walk {
  921. /** @walk: The walk base parameters. */
  922. struct ttm_lru_walk walk;
  923. /** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */
  924. gfp_t gfp_flags;
  925. };
  926. static s64
  927. ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo)
  928. {
  929. struct ttm_place place = {.mem_type = bo->resource->mem_type};
  930. struct ttm_bo_swapout_walk *swapout_walk =
  931. container_of(walk, typeof(*swapout_walk), walk);
  932. struct ttm_operation_ctx *ctx = walk->ctx;
  933. s64 ret;
  934. /*
  935. * While the bo may already reside in SYSTEM placement, set
  936. * SYSTEM as new placement to cover also the move further below.
  937. * The driver may use the fact that we're moving from SYSTEM
  938. * as an indication that we're about to swap out.
  939. */
  940. if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, &place)) {
  941. ret = -EBUSY;
  942. goto out;
  943. }
  944. if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) ||
  945. bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL ||
  946. bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED) {
  947. ret = -EBUSY;
  948. goto out;
  949. }
  950. if (bo->deleted) {
  951. pgoff_t num_pages = bo->ttm->num_pages;
  952. ret = ttm_bo_wait_ctx(bo, ctx);
  953. if (ret)
  954. goto out;
  955. ttm_bo_cleanup_memtype_use(bo);
  956. ret = num_pages;
  957. goto out;
  958. }
  959. /*
  960. * Move to system cached
  961. */
  962. if (bo->resource->mem_type != TTM_PL_SYSTEM) {
  963. struct ttm_resource *evict_mem;
  964. struct ttm_place hop;
  965. memset(&hop, 0, sizeof(hop));
  966. place.mem_type = TTM_PL_SYSTEM;
  967. ret = ttm_resource_alloc(bo, &place, &evict_mem);
  968. if (ret)
  969. goto out;
  970. ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
  971. if (ret) {
  972. WARN(ret == -EMULTIHOP,
  973. "Unexpected multihop in swapout - likely driver bug.\n");
  974. ttm_resource_free(bo, &evict_mem);
  975. goto out;
  976. }
  977. }
  978. /*
  979. * Make sure BO is idle.
  980. */
  981. ret = ttm_bo_wait_ctx(bo, ctx);
  982. if (ret)
  983. goto out;
  984. ttm_bo_unmap_virtual(bo);
  985. if (bo->bdev->funcs->swap_notify)
  986. bo->bdev->funcs->swap_notify(bo);
  987. if (ttm_tt_is_populated(bo->ttm))
  988. ret = ttm_tt_swapout(bo->bdev, bo->ttm, swapout_walk->gfp_flags);
  989. out:
  990. /* Consider -ENOMEM and -ENOSPC non-fatal. */
  991. if (ret == -ENOMEM || ret == -ENOSPC)
  992. ret = -EBUSY;
  993. return ret;
  994. }
  995. const struct ttm_lru_walk_ops ttm_swap_ops = {
  996. .process_bo = ttm_bo_swapout_cb,
  997. };
  998. /**
  999. * ttm_bo_swapout() - Swap out buffer objects on the LRU list to shmem.
  1000. * @bdev: The ttm device.
  1001. * @ctx: The ttm_operation_ctx governing the swapout operation.
  1002. * @man: The resource manager whose resources / buffer objects are
  1003. * goint to be swapped out.
  1004. * @gfp_flags: The gfp flags used for shmem page allocations.
  1005. * @target: The desired number of bytes to swap out.
  1006. *
  1007. * Return: The number of bytes actually swapped out, or negative error code
  1008. * on error.
  1009. */
  1010. s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  1011. struct ttm_resource_manager *man, gfp_t gfp_flags,
  1012. s64 target)
  1013. {
  1014. struct ttm_bo_swapout_walk swapout_walk = {
  1015. .walk = {
  1016. .ops = &ttm_swap_ops,
  1017. .ctx = ctx,
  1018. .trylock_only = true,
  1019. },
  1020. .gfp_flags = gfp_flags,
  1021. };
  1022. return ttm_lru_walk_for_evict(&swapout_walk.walk, bdev, man, target);
  1023. }
  1024. void ttm_bo_tt_destroy(struct ttm_buffer_object *bo)
  1025. {
  1026. if (bo->ttm == NULL)
  1027. return;
  1028. ttm_tt_unpopulate(bo->bdev, bo->ttm);
  1029. ttm_tt_destroy(bo->bdev, bo->ttm);
  1030. bo->ttm = NULL;
  1031. }