ttm_bo.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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_module.h>
  33. #include <drm/ttm/ttm_bo_driver.h>
  34. #include <drm/ttm/ttm_placement.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/reservation.h>
  43. static void ttm_bo_global_kobj_release(struct kobject *kobj);
  44. static struct attribute ttm_bo_count = {
  45. .name = "bo_count",
  46. .mode = S_IRUGO
  47. };
  48. /* default destructor */
  49. static void ttm_bo_default_destroy(struct ttm_buffer_object *bo)
  50. {
  51. kfree(bo);
  52. }
  53. static inline int ttm_mem_type_from_place(const struct ttm_place *place,
  54. uint32_t *mem_type)
  55. {
  56. int pos;
  57. pos = ffs(place->flags & TTM_PL_MASK_MEM);
  58. if (unlikely(!pos))
  59. return -EINVAL;
  60. *mem_type = pos - 1;
  61. return 0;
  62. }
  63. static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
  64. {
  65. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  66. struct drm_printer p = drm_debug_printer(TTM_PFX);
  67. pr_err(" has_type: %d\n", man->has_type);
  68. pr_err(" use_type: %d\n", man->use_type);
  69. pr_err(" flags: 0x%08X\n", man->flags);
  70. pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
  71. pr_err(" size: %llu\n", man->size);
  72. pr_err(" available_caching: 0x%08X\n", man->available_caching);
  73. pr_err(" default_caching: 0x%08X\n", man->default_caching);
  74. if (mem_type != TTM_PL_SYSTEM)
  75. (*man->func->debug)(man, &p);
  76. }
  77. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  78. struct ttm_placement *placement)
  79. {
  80. int i, ret, mem_type;
  81. pr_err("No space for %p (%lu pages, %luK, %luM)\n",
  82. bo, bo->mem.num_pages, bo->mem.size >> 10,
  83. bo->mem.size >> 20);
  84. for (i = 0; i < placement->num_placement; i++) {
  85. ret = ttm_mem_type_from_place(&placement->placement[i],
  86. &mem_type);
  87. if (ret)
  88. return;
  89. pr_err(" placement[%d]=0x%08X (%d)\n",
  90. i, placement->placement[i].flags, mem_type);
  91. ttm_mem_type_debug(bo->bdev, mem_type);
  92. }
  93. }
  94. static ssize_t ttm_bo_global_show(struct kobject *kobj,
  95. struct attribute *attr,
  96. char *buffer)
  97. {
  98. struct ttm_bo_global *glob =
  99. container_of(kobj, struct ttm_bo_global, kobj);
  100. return snprintf(buffer, PAGE_SIZE, "%d\n",
  101. atomic_read(&glob->bo_count));
  102. }
  103. static struct attribute *ttm_bo_global_attrs[] = {
  104. &ttm_bo_count,
  105. NULL
  106. };
  107. static const struct sysfs_ops ttm_bo_global_ops = {
  108. .show = &ttm_bo_global_show
  109. };
  110. static struct kobj_type ttm_bo_glob_kobj_type = {
  111. .release = &ttm_bo_global_kobj_release,
  112. .sysfs_ops = &ttm_bo_global_ops,
  113. .default_attrs = ttm_bo_global_attrs
  114. };
  115. static inline uint32_t ttm_bo_type_flags(unsigned type)
  116. {
  117. return 1 << (type);
  118. }
  119. static void ttm_bo_release_list(struct kref *list_kref)
  120. {
  121. struct ttm_buffer_object *bo =
  122. container_of(list_kref, struct ttm_buffer_object, list_kref);
  123. struct ttm_bo_device *bdev = bo->bdev;
  124. size_t acc_size = bo->acc_size;
  125. BUG_ON(kref_read(&bo->list_kref));
  126. BUG_ON(kref_read(&bo->kref));
  127. BUG_ON(atomic_read(&bo->cpu_writers));
  128. BUG_ON(bo->mem.mm_node != NULL);
  129. BUG_ON(!list_empty(&bo->lru));
  130. BUG_ON(!list_empty(&bo->ddestroy));
  131. ttm_tt_destroy(bo->ttm);
  132. atomic_dec(&bo->bdev->glob->bo_count);
  133. dma_fence_put(bo->moving);
  134. reservation_object_fini(&bo->ttm_resv);
  135. mutex_destroy(&bo->wu_mutex);
  136. bo->destroy(bo);
  137. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  138. }
  139. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  140. {
  141. struct ttm_bo_device *bdev = bo->bdev;
  142. struct ttm_mem_type_manager *man;
  143. reservation_object_assert_held(bo->resv);
  144. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  145. BUG_ON(!list_empty(&bo->lru));
  146. man = &bdev->man[bo->mem.mem_type];
  147. list_add_tail(&bo->lru, &man->lru[bo->priority]);
  148. kref_get(&bo->list_kref);
  149. if (bo->ttm && !(bo->ttm->page_flags &
  150. (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED))) {
  151. list_add_tail(&bo->swap,
  152. &bdev->glob->swap_lru[bo->priority]);
  153. kref_get(&bo->list_kref);
  154. }
  155. }
  156. }
  157. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  158. static void ttm_bo_ref_bug(struct kref *list_kref)
  159. {
  160. BUG();
  161. }
  162. void ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  163. {
  164. if (!list_empty(&bo->swap)) {
  165. list_del_init(&bo->swap);
  166. kref_put(&bo->list_kref, ttm_bo_ref_bug);
  167. }
  168. if (!list_empty(&bo->lru)) {
  169. list_del_init(&bo->lru);
  170. kref_put(&bo->list_kref, ttm_bo_ref_bug);
  171. }
  172. /*
  173. * TODO: Add a driver hook to delete from
  174. * driver-specific LRU's here.
  175. */
  176. }
  177. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  178. {
  179. struct ttm_bo_global *glob = bo->bdev->glob;
  180. spin_lock(&glob->lru_lock);
  181. ttm_bo_del_from_lru(bo);
  182. spin_unlock(&glob->lru_lock);
  183. }
  184. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  185. static void ttm_bo_bulk_move_set_pos(struct ttm_lru_bulk_move_pos *pos,
  186. struct ttm_buffer_object *bo)
  187. {
  188. if (!pos->first)
  189. pos->first = bo;
  190. pos->last = bo;
  191. }
  192. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
  193. struct ttm_lru_bulk_move *bulk)
  194. {
  195. reservation_object_assert_held(bo->resv);
  196. ttm_bo_del_from_lru(bo);
  197. ttm_bo_add_to_lru(bo);
  198. if (bulk && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  199. switch (bo->mem.mem_type) {
  200. case TTM_PL_TT:
  201. ttm_bo_bulk_move_set_pos(&bulk->tt[bo->priority], bo);
  202. break;
  203. case TTM_PL_VRAM:
  204. ttm_bo_bulk_move_set_pos(&bulk->vram[bo->priority], bo);
  205. break;
  206. }
  207. if (bo->ttm && !(bo->ttm->page_flags &
  208. (TTM_PAGE_FLAG_SG | TTM_PAGE_FLAG_SWAPPED)))
  209. ttm_bo_bulk_move_set_pos(&bulk->swap[bo->priority], bo);
  210. }
  211. }
  212. EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
  213. void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk)
  214. {
  215. unsigned i;
  216. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  217. struct ttm_lru_bulk_move_pos *pos = &bulk->tt[i];
  218. struct ttm_mem_type_manager *man;
  219. if (!pos->first)
  220. continue;
  221. reservation_object_assert_held(pos->first->resv);
  222. reservation_object_assert_held(pos->last->resv);
  223. man = &pos->first->bdev->man[TTM_PL_TT];
  224. list_bulk_move_tail(&man->lru[i], &pos->first->lru,
  225. &pos->last->lru);
  226. }
  227. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  228. struct ttm_lru_bulk_move_pos *pos = &bulk->vram[i];
  229. struct ttm_mem_type_manager *man;
  230. if (!pos->first)
  231. continue;
  232. reservation_object_assert_held(pos->first->resv);
  233. reservation_object_assert_held(pos->last->resv);
  234. man = &pos->first->bdev->man[TTM_PL_VRAM];
  235. list_bulk_move_tail(&man->lru[i], &pos->first->lru,
  236. &pos->last->lru);
  237. }
  238. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  239. struct ttm_lru_bulk_move_pos *pos = &bulk->swap[i];
  240. struct list_head *lru;
  241. if (!pos->first)
  242. continue;
  243. reservation_object_assert_held(pos->first->resv);
  244. reservation_object_assert_held(pos->last->resv);
  245. lru = &pos->first->bdev->glob->swap_lru[i];
  246. list_bulk_move_tail(lru, &pos->first->swap, &pos->last->swap);
  247. }
  248. }
  249. EXPORT_SYMBOL(ttm_bo_bulk_move_lru_tail);
  250. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  251. struct ttm_mem_reg *mem, bool evict,
  252. struct ttm_operation_ctx *ctx)
  253. {
  254. struct ttm_bo_device *bdev = bo->bdev;
  255. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  256. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  257. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  258. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  259. int ret = 0;
  260. if (old_is_pci || new_is_pci ||
  261. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  262. ret = ttm_mem_io_lock(old_man, true);
  263. if (unlikely(ret != 0))
  264. goto out_err;
  265. ttm_bo_unmap_virtual_locked(bo);
  266. ttm_mem_io_unlock(old_man);
  267. }
  268. /*
  269. * Create and bind a ttm if required.
  270. */
  271. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  272. if (bo->ttm == NULL) {
  273. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  274. ret = ttm_tt_create(bo, zero);
  275. if (ret)
  276. goto out_err;
  277. }
  278. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  279. if (ret)
  280. goto out_err;
  281. if (mem->mem_type != TTM_PL_SYSTEM) {
  282. ret = ttm_tt_bind(bo->ttm, mem, ctx);
  283. if (ret)
  284. goto out_err;
  285. }
  286. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  287. if (bdev->driver->move_notify)
  288. bdev->driver->move_notify(bo, evict, mem);
  289. bo->mem = *mem;
  290. mem->mm_node = NULL;
  291. goto moved;
  292. }
  293. }
  294. if (bdev->driver->move_notify)
  295. bdev->driver->move_notify(bo, evict, mem);
  296. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  297. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  298. ret = ttm_bo_move_ttm(bo, ctx, mem);
  299. else if (bdev->driver->move)
  300. ret = bdev->driver->move(bo, evict, ctx, mem);
  301. else
  302. ret = ttm_bo_move_memcpy(bo, ctx, mem);
  303. if (ret) {
  304. if (bdev->driver->move_notify) {
  305. swap(*mem, bo->mem);
  306. bdev->driver->move_notify(bo, false, mem);
  307. swap(*mem, bo->mem);
  308. }
  309. goto out_err;
  310. }
  311. moved:
  312. if (bo->evicted) {
  313. if (bdev->driver->invalidate_caches) {
  314. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  315. if (ret)
  316. pr_err("Can not flush read caches\n");
  317. }
  318. bo->evicted = false;
  319. }
  320. if (bo->mem.mm_node)
  321. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  322. bdev->man[bo->mem.mem_type].gpu_offset;
  323. else
  324. bo->offset = 0;
  325. ctx->bytes_moved += bo->num_pages << PAGE_SHIFT;
  326. return 0;
  327. out_err:
  328. new_man = &bdev->man[bo->mem.mem_type];
  329. if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  330. ttm_tt_destroy(bo->ttm);
  331. bo->ttm = NULL;
  332. }
  333. return ret;
  334. }
  335. /**
  336. * Call bo::reserved.
  337. * Will release GPU memory type usage on destruction.
  338. * This is the place to put in driver specific hooks to release
  339. * driver private resources.
  340. * Will release the bo::reserved lock.
  341. */
  342. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  343. {
  344. if (bo->bdev->driver->move_notify)
  345. bo->bdev->driver->move_notify(bo, false, NULL);
  346. ttm_tt_destroy(bo->ttm);
  347. bo->ttm = NULL;
  348. ttm_bo_mem_put(bo, &bo->mem);
  349. }
  350. static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
  351. {
  352. int r;
  353. if (bo->resv == &bo->ttm_resv)
  354. return 0;
  355. BUG_ON(!reservation_object_trylock(&bo->ttm_resv));
  356. r = reservation_object_copy_fences(&bo->ttm_resv, bo->resv);
  357. if (r)
  358. reservation_object_unlock(&bo->ttm_resv);
  359. return r;
  360. }
  361. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  362. {
  363. struct reservation_object_list *fobj;
  364. struct dma_fence *fence;
  365. int i;
  366. fobj = reservation_object_get_list(&bo->ttm_resv);
  367. fence = reservation_object_get_excl(&bo->ttm_resv);
  368. if (fence && !fence->ops->signaled)
  369. dma_fence_enable_sw_signaling(fence);
  370. for (i = 0; fobj && i < fobj->shared_count; ++i) {
  371. fence = rcu_dereference_protected(fobj->shared[i],
  372. reservation_object_held(bo->resv));
  373. if (!fence->ops->signaled)
  374. dma_fence_enable_sw_signaling(fence);
  375. }
  376. }
  377. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  378. {
  379. struct ttm_bo_device *bdev = bo->bdev;
  380. struct ttm_bo_global *glob = bdev->glob;
  381. int ret;
  382. ret = ttm_bo_individualize_resv(bo);
  383. if (ret) {
  384. /* Last resort, if we fail to allocate memory for the
  385. * fences block for the BO to become idle
  386. */
  387. reservation_object_wait_timeout_rcu(bo->resv, true, false,
  388. 30 * HZ);
  389. spin_lock(&glob->lru_lock);
  390. goto error;
  391. }
  392. spin_lock(&glob->lru_lock);
  393. ret = reservation_object_trylock(bo->resv) ? 0 : -EBUSY;
  394. if (!ret) {
  395. if (reservation_object_test_signaled_rcu(&bo->ttm_resv, true)) {
  396. ttm_bo_del_from_lru(bo);
  397. spin_unlock(&glob->lru_lock);
  398. if (bo->resv != &bo->ttm_resv)
  399. reservation_object_unlock(&bo->ttm_resv);
  400. ttm_bo_cleanup_memtype_use(bo);
  401. reservation_object_unlock(bo->resv);
  402. return;
  403. }
  404. ttm_bo_flush_all_fences(bo);
  405. /*
  406. * Make NO_EVICT bos immediately available to
  407. * shrinkers, now that they are queued for
  408. * destruction.
  409. */
  410. if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
  411. bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
  412. ttm_bo_add_to_lru(bo);
  413. }
  414. reservation_object_unlock(bo->resv);
  415. }
  416. if (bo->resv != &bo->ttm_resv)
  417. reservation_object_unlock(&bo->ttm_resv);
  418. error:
  419. kref_get(&bo->list_kref);
  420. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  421. spin_unlock(&glob->lru_lock);
  422. schedule_delayed_work(&bdev->wq,
  423. ((HZ / 100) < 1) ? 1 : HZ / 100);
  424. }
  425. /**
  426. * function ttm_bo_cleanup_refs
  427. * If bo idle, remove from delayed- and lru lists, and unref.
  428. * If not idle, do nothing.
  429. *
  430. * Must be called with lru_lock and reservation held, this function
  431. * will drop the lru lock and optionally the reservation lock before returning.
  432. *
  433. * @interruptible Any sleeps should occur interruptibly.
  434. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  435. * @unlock_resv Unlock the reservation lock as well.
  436. */
  437. static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
  438. bool interruptible, bool no_wait_gpu,
  439. bool unlock_resv)
  440. {
  441. struct ttm_bo_global *glob = bo->bdev->glob;
  442. struct reservation_object *resv;
  443. int ret;
  444. if (unlikely(list_empty(&bo->ddestroy)))
  445. resv = bo->resv;
  446. else
  447. resv = &bo->ttm_resv;
  448. if (reservation_object_test_signaled_rcu(resv, true))
  449. ret = 0;
  450. else
  451. ret = -EBUSY;
  452. if (ret && !no_wait_gpu) {
  453. long lret;
  454. if (unlock_resv)
  455. reservation_object_unlock(bo->resv);
  456. spin_unlock(&glob->lru_lock);
  457. lret = reservation_object_wait_timeout_rcu(resv, true,
  458. interruptible,
  459. 30 * HZ);
  460. if (lret < 0)
  461. return lret;
  462. else if (lret == 0)
  463. return -EBUSY;
  464. spin_lock(&glob->lru_lock);
  465. if (unlock_resv && !reservation_object_trylock(bo->resv)) {
  466. /*
  467. * We raced, and lost, someone else holds the reservation now,
  468. * and is probably busy in ttm_bo_cleanup_memtype_use.
  469. *
  470. * Even if it's not the case, because we finished waiting any
  471. * delayed destruction would succeed, so just return success
  472. * here.
  473. */
  474. spin_unlock(&glob->lru_lock);
  475. return 0;
  476. }
  477. ret = 0;
  478. }
  479. if (ret || unlikely(list_empty(&bo->ddestroy))) {
  480. if (unlock_resv)
  481. reservation_object_unlock(bo->resv);
  482. spin_unlock(&glob->lru_lock);
  483. return ret;
  484. }
  485. ttm_bo_del_from_lru(bo);
  486. list_del_init(&bo->ddestroy);
  487. kref_put(&bo->list_kref, ttm_bo_ref_bug);
  488. spin_unlock(&glob->lru_lock);
  489. ttm_bo_cleanup_memtype_use(bo);
  490. if (unlock_resv)
  491. reservation_object_unlock(bo->resv);
  492. return 0;
  493. }
  494. /**
  495. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  496. * encountered buffers.
  497. */
  498. static bool ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  499. {
  500. struct ttm_bo_global *glob = bdev->glob;
  501. struct list_head removed;
  502. bool empty;
  503. INIT_LIST_HEAD(&removed);
  504. spin_lock(&glob->lru_lock);
  505. while (!list_empty(&bdev->ddestroy)) {
  506. struct ttm_buffer_object *bo;
  507. bo = list_first_entry(&bdev->ddestroy, struct ttm_buffer_object,
  508. ddestroy);
  509. kref_get(&bo->list_kref);
  510. list_move_tail(&bo->ddestroy, &removed);
  511. if (remove_all || bo->resv != &bo->ttm_resv) {
  512. spin_unlock(&glob->lru_lock);
  513. reservation_object_lock(bo->resv, NULL);
  514. spin_lock(&glob->lru_lock);
  515. ttm_bo_cleanup_refs(bo, false, !remove_all, true);
  516. } else if (reservation_object_trylock(bo->resv)) {
  517. ttm_bo_cleanup_refs(bo, false, !remove_all, true);
  518. } else {
  519. spin_unlock(&glob->lru_lock);
  520. }
  521. kref_put(&bo->list_kref, ttm_bo_release_list);
  522. spin_lock(&glob->lru_lock);
  523. }
  524. list_splice_tail(&removed, &bdev->ddestroy);
  525. empty = list_empty(&bdev->ddestroy);
  526. spin_unlock(&glob->lru_lock);
  527. return empty;
  528. }
  529. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  530. {
  531. struct ttm_bo_device *bdev =
  532. container_of(work, struct ttm_bo_device, wq.work);
  533. if (!ttm_bo_delayed_delete(bdev, false))
  534. schedule_delayed_work(&bdev->wq,
  535. ((HZ / 100) < 1) ? 1 : HZ / 100);
  536. }
  537. static void ttm_bo_release(struct kref *kref)
  538. {
  539. struct ttm_buffer_object *bo =
  540. container_of(kref, struct ttm_buffer_object, kref);
  541. struct ttm_bo_device *bdev = bo->bdev;
  542. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  543. drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  544. ttm_mem_io_lock(man, false);
  545. ttm_mem_io_free_vm(bo);
  546. ttm_mem_io_unlock(man);
  547. ttm_bo_cleanup_refs_or_queue(bo);
  548. kref_put(&bo->list_kref, ttm_bo_release_list);
  549. }
  550. void ttm_bo_put(struct ttm_buffer_object *bo)
  551. {
  552. kref_put(&bo->kref, ttm_bo_release);
  553. }
  554. EXPORT_SYMBOL(ttm_bo_put);
  555. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  556. {
  557. struct ttm_buffer_object *bo = *p_bo;
  558. *p_bo = NULL;
  559. ttm_bo_put(bo);
  560. }
  561. EXPORT_SYMBOL(ttm_bo_unref);
  562. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  563. {
  564. return cancel_delayed_work_sync(&bdev->wq);
  565. }
  566. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  567. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  568. {
  569. if (resched)
  570. schedule_delayed_work(&bdev->wq,
  571. ((HZ / 100) < 1) ? 1 : HZ / 100);
  572. }
  573. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  574. static int ttm_bo_evict(struct ttm_buffer_object *bo,
  575. struct ttm_operation_ctx *ctx)
  576. {
  577. struct ttm_bo_device *bdev = bo->bdev;
  578. struct ttm_mem_reg evict_mem;
  579. struct ttm_placement placement;
  580. int ret = 0;
  581. reservation_object_assert_held(bo->resv);
  582. placement.num_placement = 0;
  583. placement.num_busy_placement = 0;
  584. bdev->driver->evict_flags(bo, &placement);
  585. if (!placement.num_placement && !placement.num_busy_placement) {
  586. ret = ttm_bo_pipeline_gutting(bo);
  587. if (ret)
  588. return ret;
  589. return ttm_tt_create(bo, false);
  590. }
  591. evict_mem = bo->mem;
  592. evict_mem.mm_node = NULL;
  593. evict_mem.bus.io_reserved_vm = false;
  594. evict_mem.bus.io_reserved_count = 0;
  595. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, ctx);
  596. if (ret) {
  597. if (ret != -ERESTARTSYS) {
  598. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  599. bo);
  600. ttm_bo_mem_space_debug(bo, &placement);
  601. }
  602. goto out;
  603. }
  604. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx);
  605. if (unlikely(ret)) {
  606. if (ret != -ERESTARTSYS)
  607. pr_err("Buffer eviction failed\n");
  608. ttm_bo_mem_put(bo, &evict_mem);
  609. goto out;
  610. }
  611. bo->evicted = true;
  612. out:
  613. return ret;
  614. }
  615. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  616. const struct ttm_place *place)
  617. {
  618. /* Don't evict this BO if it's outside of the
  619. * requested placement range
  620. */
  621. if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
  622. (place->lpfn && place->lpfn <= bo->mem.start))
  623. return false;
  624. return true;
  625. }
  626. EXPORT_SYMBOL(ttm_bo_eviction_valuable);
  627. /**
  628. * Check the target bo is allowable to be evicted or swapout, including cases:
  629. *
  630. * a. if share same reservation object with ctx->resv, have assumption
  631. * reservation objects should already be locked, so not lock again and
  632. * return true directly when either the opreation allow_reserved_eviction
  633. * or the target bo already is in delayed free list;
  634. *
  635. * b. Otherwise, trylock it.
  636. */
  637. static bool ttm_bo_evict_swapout_allowable(struct ttm_buffer_object *bo,
  638. struct ttm_operation_ctx *ctx, bool *locked)
  639. {
  640. bool ret = false;
  641. *locked = false;
  642. if (bo->resv == ctx->resv) {
  643. reservation_object_assert_held(bo->resv);
  644. if (ctx->flags & TTM_OPT_FLAG_ALLOW_RES_EVICT
  645. || !list_empty(&bo->ddestroy))
  646. ret = true;
  647. } else {
  648. *locked = reservation_object_trylock(bo->resv);
  649. ret = *locked;
  650. }
  651. return ret;
  652. }
  653. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  654. uint32_t mem_type,
  655. const struct ttm_place *place,
  656. struct ttm_operation_ctx *ctx)
  657. {
  658. struct ttm_bo_global *glob = bdev->glob;
  659. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  660. struct ttm_buffer_object *bo = NULL;
  661. bool locked = false;
  662. unsigned i;
  663. int ret;
  664. spin_lock(&glob->lru_lock);
  665. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  666. list_for_each_entry(bo, &man->lru[i], lru) {
  667. if (!ttm_bo_evict_swapout_allowable(bo, ctx, &locked))
  668. continue;
  669. if (place && !bdev->driver->eviction_valuable(bo,
  670. place)) {
  671. if (locked)
  672. reservation_object_unlock(bo->resv);
  673. continue;
  674. }
  675. break;
  676. }
  677. /* If the inner loop terminated early, we have our candidate */
  678. if (&bo->lru != &man->lru[i])
  679. break;
  680. bo = NULL;
  681. }
  682. if (!bo) {
  683. spin_unlock(&glob->lru_lock);
  684. return -EBUSY;
  685. }
  686. kref_get(&bo->list_kref);
  687. if (!list_empty(&bo->ddestroy)) {
  688. ret = ttm_bo_cleanup_refs(bo, ctx->interruptible,
  689. ctx->no_wait_gpu, locked);
  690. kref_put(&bo->list_kref, ttm_bo_release_list);
  691. return ret;
  692. }
  693. ttm_bo_del_from_lru(bo);
  694. spin_unlock(&glob->lru_lock);
  695. ret = ttm_bo_evict(bo, ctx);
  696. if (locked) {
  697. ttm_bo_unreserve(bo);
  698. } else {
  699. spin_lock(&glob->lru_lock);
  700. ttm_bo_add_to_lru(bo);
  701. spin_unlock(&glob->lru_lock);
  702. }
  703. kref_put(&bo->list_kref, ttm_bo_release_list);
  704. return ret;
  705. }
  706. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  707. {
  708. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  709. if (mem->mm_node)
  710. (*man->func->put_node)(man, mem);
  711. }
  712. EXPORT_SYMBOL(ttm_bo_mem_put);
  713. /**
  714. * Add the last move fence to the BO and reserve a new shared slot.
  715. */
  716. static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
  717. struct ttm_mem_type_manager *man,
  718. struct ttm_mem_reg *mem)
  719. {
  720. struct dma_fence *fence;
  721. int ret;
  722. spin_lock(&man->move_lock);
  723. fence = dma_fence_get(man->move);
  724. spin_unlock(&man->move_lock);
  725. if (fence) {
  726. reservation_object_add_shared_fence(bo->resv, fence);
  727. ret = reservation_object_reserve_shared(bo->resv);
  728. if (unlikely(ret))
  729. return ret;
  730. dma_fence_put(bo->moving);
  731. bo->moving = fence;
  732. }
  733. return 0;
  734. }
  735. /**
  736. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  737. * space, or we've evicted everything and there isn't enough space.
  738. */
  739. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  740. uint32_t mem_type,
  741. const struct ttm_place *place,
  742. struct ttm_mem_reg *mem,
  743. struct ttm_operation_ctx *ctx)
  744. {
  745. struct ttm_bo_device *bdev = bo->bdev;
  746. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  747. int ret;
  748. do {
  749. ret = (*man->func->get_node)(man, bo, place, mem);
  750. if (unlikely(ret != 0))
  751. return ret;
  752. if (mem->mm_node)
  753. break;
  754. ret = ttm_mem_evict_first(bdev, mem_type, place, ctx);
  755. if (unlikely(ret != 0))
  756. return ret;
  757. } while (1);
  758. mem->mem_type = mem_type;
  759. return ttm_bo_add_move_fence(bo, man, mem);
  760. }
  761. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  762. uint32_t cur_placement,
  763. uint32_t proposed_placement)
  764. {
  765. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  766. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  767. /**
  768. * Keep current caching if possible.
  769. */
  770. if ((cur_placement & caching) != 0)
  771. result |= (cur_placement & caching);
  772. else if ((man->default_caching & caching) != 0)
  773. result |= man->default_caching;
  774. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  775. result |= TTM_PL_FLAG_CACHED;
  776. else if ((TTM_PL_FLAG_WC & caching) != 0)
  777. result |= TTM_PL_FLAG_WC;
  778. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  779. result |= TTM_PL_FLAG_UNCACHED;
  780. return result;
  781. }
  782. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  783. uint32_t mem_type,
  784. const struct ttm_place *place,
  785. uint32_t *masked_placement)
  786. {
  787. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  788. if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
  789. return false;
  790. if ((place->flags & man->available_caching) == 0)
  791. return false;
  792. cur_flags |= (place->flags & man->available_caching);
  793. *masked_placement = cur_flags;
  794. return true;
  795. }
  796. /**
  797. * Creates space for memory region @mem according to its type.
  798. *
  799. * This function first searches for free space in compatible memory types in
  800. * the priority order defined by the driver. If free space isn't found, then
  801. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  802. * space.
  803. */
  804. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  805. struct ttm_placement *placement,
  806. struct ttm_mem_reg *mem,
  807. struct ttm_operation_ctx *ctx)
  808. {
  809. struct ttm_bo_device *bdev = bo->bdev;
  810. struct ttm_mem_type_manager *man;
  811. uint32_t mem_type = TTM_PL_SYSTEM;
  812. uint32_t cur_flags = 0;
  813. bool type_found = false;
  814. bool type_ok = false;
  815. bool has_erestartsys = false;
  816. int i, ret;
  817. ret = reservation_object_reserve_shared(bo->resv);
  818. if (unlikely(ret))
  819. return ret;
  820. mem->mm_node = NULL;
  821. for (i = 0; i < placement->num_placement; ++i) {
  822. const struct ttm_place *place = &placement->placement[i];
  823. ret = ttm_mem_type_from_place(place, &mem_type);
  824. if (ret)
  825. return ret;
  826. man = &bdev->man[mem_type];
  827. if (!man->has_type || !man->use_type)
  828. continue;
  829. type_ok = ttm_bo_mt_compatible(man, mem_type, place,
  830. &cur_flags);
  831. if (!type_ok)
  832. continue;
  833. type_found = true;
  834. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  835. cur_flags);
  836. /*
  837. * Use the access and other non-mapping-related flag bits from
  838. * the memory placement flags to the current flags
  839. */
  840. ttm_flag_masked(&cur_flags, place->flags,
  841. ~TTM_PL_MASK_MEMTYPE);
  842. if (mem_type == TTM_PL_SYSTEM)
  843. break;
  844. ret = (*man->func->get_node)(man, bo, place, mem);
  845. if (unlikely(ret))
  846. return ret;
  847. if (mem->mm_node) {
  848. ret = ttm_bo_add_move_fence(bo, man, mem);
  849. if (unlikely(ret)) {
  850. (*man->func->put_node)(man, mem);
  851. return ret;
  852. }
  853. break;
  854. }
  855. }
  856. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  857. mem->mem_type = mem_type;
  858. mem->placement = cur_flags;
  859. return 0;
  860. }
  861. for (i = 0; i < placement->num_busy_placement; ++i) {
  862. const struct ttm_place *place = &placement->busy_placement[i];
  863. ret = ttm_mem_type_from_place(place, &mem_type);
  864. if (ret)
  865. return ret;
  866. man = &bdev->man[mem_type];
  867. if (!man->has_type || !man->use_type)
  868. continue;
  869. if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
  870. continue;
  871. type_found = true;
  872. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  873. cur_flags);
  874. /*
  875. * Use the access and other non-mapping-related flag bits from
  876. * the memory placement flags to the current flags
  877. */
  878. ttm_flag_masked(&cur_flags, place->flags,
  879. ~TTM_PL_MASK_MEMTYPE);
  880. if (mem_type == TTM_PL_SYSTEM) {
  881. mem->mem_type = mem_type;
  882. mem->placement = cur_flags;
  883. mem->mm_node = NULL;
  884. return 0;
  885. }
  886. ret = ttm_bo_mem_force_space(bo, mem_type, place, mem, ctx);
  887. if (ret == 0 && mem->mm_node) {
  888. mem->placement = cur_flags;
  889. return 0;
  890. }
  891. if (ret == -ERESTARTSYS)
  892. has_erestartsys = true;
  893. }
  894. if (!type_found) {
  895. pr_err(TTM_PFX "No compatible memory type found\n");
  896. return -EINVAL;
  897. }
  898. return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  899. }
  900. EXPORT_SYMBOL(ttm_bo_mem_space);
  901. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  902. struct ttm_placement *placement,
  903. struct ttm_operation_ctx *ctx)
  904. {
  905. int ret = 0;
  906. struct ttm_mem_reg mem;
  907. reservation_object_assert_held(bo->resv);
  908. mem.num_pages = bo->num_pages;
  909. mem.size = mem.num_pages << PAGE_SHIFT;
  910. mem.page_alignment = bo->mem.page_alignment;
  911. mem.bus.io_reserved_vm = false;
  912. mem.bus.io_reserved_count = 0;
  913. /*
  914. * Determine where to move the buffer.
  915. */
  916. ret = ttm_bo_mem_space(bo, placement, &mem, ctx);
  917. if (ret)
  918. goto out_unlock;
  919. ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx);
  920. out_unlock:
  921. if (ret && mem.mm_node)
  922. ttm_bo_mem_put(bo, &mem);
  923. return ret;
  924. }
  925. static bool ttm_bo_places_compat(const struct ttm_place *places,
  926. unsigned num_placement,
  927. struct ttm_mem_reg *mem,
  928. uint32_t *new_flags)
  929. {
  930. unsigned i;
  931. for (i = 0; i < num_placement; i++) {
  932. const struct ttm_place *heap = &places[i];
  933. if (mem->mm_node && (mem->start < heap->fpfn ||
  934. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  935. continue;
  936. *new_flags = heap->flags;
  937. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  938. (*new_flags & mem->placement & TTM_PL_MASK_MEM) &&
  939. (!(*new_flags & TTM_PL_FLAG_CONTIGUOUS) ||
  940. (mem->placement & TTM_PL_FLAG_CONTIGUOUS)))
  941. return true;
  942. }
  943. return false;
  944. }
  945. bool ttm_bo_mem_compat(struct ttm_placement *placement,
  946. struct ttm_mem_reg *mem,
  947. uint32_t *new_flags)
  948. {
  949. if (ttm_bo_places_compat(placement->placement, placement->num_placement,
  950. mem, new_flags))
  951. return true;
  952. if ((placement->busy_placement != placement->placement ||
  953. placement->num_busy_placement > placement->num_placement) &&
  954. ttm_bo_places_compat(placement->busy_placement,
  955. placement->num_busy_placement,
  956. mem, new_flags))
  957. return true;
  958. return false;
  959. }
  960. EXPORT_SYMBOL(ttm_bo_mem_compat);
  961. int ttm_bo_validate(struct ttm_buffer_object *bo,
  962. struct ttm_placement *placement,
  963. struct ttm_operation_ctx *ctx)
  964. {
  965. int ret;
  966. uint32_t new_flags;
  967. reservation_object_assert_held(bo->resv);
  968. /*
  969. * Check whether we need to move buffer.
  970. */
  971. if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  972. ret = ttm_bo_move_buffer(bo, placement, ctx);
  973. if (ret)
  974. return ret;
  975. } else {
  976. /*
  977. * Use the access and other non-mapping-related flag bits from
  978. * the compatible memory placement flags to the active flags
  979. */
  980. ttm_flag_masked(&bo->mem.placement, new_flags,
  981. ~TTM_PL_MASK_MEMTYPE);
  982. }
  983. /*
  984. * We might need to add a TTM.
  985. */
  986. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  987. ret = ttm_tt_create(bo, true);
  988. if (ret)
  989. return ret;
  990. }
  991. return 0;
  992. }
  993. EXPORT_SYMBOL(ttm_bo_validate);
  994. int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
  995. struct ttm_buffer_object *bo,
  996. unsigned long size,
  997. enum ttm_bo_type type,
  998. struct ttm_placement *placement,
  999. uint32_t page_alignment,
  1000. struct ttm_operation_ctx *ctx,
  1001. size_t acc_size,
  1002. struct sg_table *sg,
  1003. struct reservation_object *resv,
  1004. void (*destroy) (struct ttm_buffer_object *))
  1005. {
  1006. int ret = 0;
  1007. unsigned long num_pages;
  1008. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  1009. bool locked;
  1010. ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
  1011. if (ret) {
  1012. pr_err("Out of kernel memory\n");
  1013. if (destroy)
  1014. (*destroy)(bo);
  1015. else
  1016. kfree(bo);
  1017. return -ENOMEM;
  1018. }
  1019. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1020. if (num_pages == 0) {
  1021. pr_err("Illegal buffer object size\n");
  1022. if (destroy)
  1023. (*destroy)(bo);
  1024. else
  1025. kfree(bo);
  1026. ttm_mem_global_free(mem_glob, acc_size);
  1027. return -EINVAL;
  1028. }
  1029. bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
  1030. kref_init(&bo->kref);
  1031. kref_init(&bo->list_kref);
  1032. atomic_set(&bo->cpu_writers, 0);
  1033. INIT_LIST_HEAD(&bo->lru);
  1034. INIT_LIST_HEAD(&bo->ddestroy);
  1035. INIT_LIST_HEAD(&bo->swap);
  1036. INIT_LIST_HEAD(&bo->io_reserve_lru);
  1037. mutex_init(&bo->wu_mutex);
  1038. bo->bdev = bdev;
  1039. bo->type = type;
  1040. bo->num_pages = num_pages;
  1041. bo->mem.size = num_pages << PAGE_SHIFT;
  1042. bo->mem.mem_type = TTM_PL_SYSTEM;
  1043. bo->mem.num_pages = bo->num_pages;
  1044. bo->mem.mm_node = NULL;
  1045. bo->mem.page_alignment = page_alignment;
  1046. bo->mem.bus.io_reserved_vm = false;
  1047. bo->mem.bus.io_reserved_count = 0;
  1048. bo->moving = NULL;
  1049. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1050. bo->acc_size = acc_size;
  1051. bo->sg = sg;
  1052. if (resv) {
  1053. bo->resv = resv;
  1054. reservation_object_assert_held(bo->resv);
  1055. } else {
  1056. bo->resv = &bo->ttm_resv;
  1057. }
  1058. reservation_object_init(&bo->ttm_resv);
  1059. atomic_inc(&bo->bdev->glob->bo_count);
  1060. drm_vma_node_reset(&bo->vma_node);
  1061. /*
  1062. * For ttm_bo_type_device buffers, allocate
  1063. * address space from the device.
  1064. */
  1065. if (bo->type == ttm_bo_type_device ||
  1066. bo->type == ttm_bo_type_sg)
  1067. ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  1068. bo->mem.num_pages);
  1069. /* passed reservation objects should already be locked,
  1070. * since otherwise lockdep will be angered in radeon.
  1071. */
  1072. if (!resv) {
  1073. locked = reservation_object_trylock(bo->resv);
  1074. WARN_ON(!locked);
  1075. }
  1076. if (likely(!ret))
  1077. ret = ttm_bo_validate(bo, placement, ctx);
  1078. if (unlikely(ret)) {
  1079. if (!resv)
  1080. ttm_bo_unreserve(bo);
  1081. ttm_bo_put(bo);
  1082. return ret;
  1083. }
  1084. if (resv && !(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  1085. spin_lock(&bdev->glob->lru_lock);
  1086. ttm_bo_add_to_lru(bo);
  1087. spin_unlock(&bdev->glob->lru_lock);
  1088. }
  1089. return ret;
  1090. }
  1091. EXPORT_SYMBOL(ttm_bo_init_reserved);
  1092. int ttm_bo_init(struct ttm_bo_device *bdev,
  1093. struct ttm_buffer_object *bo,
  1094. unsigned long size,
  1095. enum ttm_bo_type type,
  1096. struct ttm_placement *placement,
  1097. uint32_t page_alignment,
  1098. bool interruptible,
  1099. size_t acc_size,
  1100. struct sg_table *sg,
  1101. struct reservation_object *resv,
  1102. void (*destroy) (struct ttm_buffer_object *))
  1103. {
  1104. struct ttm_operation_ctx ctx = { interruptible, false };
  1105. int ret;
  1106. ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
  1107. page_alignment, &ctx, acc_size,
  1108. sg, resv, destroy);
  1109. if (ret)
  1110. return ret;
  1111. if (!resv)
  1112. ttm_bo_unreserve(bo);
  1113. return 0;
  1114. }
  1115. EXPORT_SYMBOL(ttm_bo_init);
  1116. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1117. unsigned long bo_size,
  1118. unsigned struct_size)
  1119. {
  1120. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1121. size_t size = 0;
  1122. size += ttm_round_pot(struct_size);
  1123. size += ttm_round_pot(npages * sizeof(void *));
  1124. size += ttm_round_pot(sizeof(struct ttm_tt));
  1125. return size;
  1126. }
  1127. EXPORT_SYMBOL(ttm_bo_acc_size);
  1128. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1129. unsigned long bo_size,
  1130. unsigned struct_size)
  1131. {
  1132. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1133. size_t size = 0;
  1134. size += ttm_round_pot(struct_size);
  1135. size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
  1136. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1137. return size;
  1138. }
  1139. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1140. int ttm_bo_create(struct ttm_bo_device *bdev,
  1141. unsigned long size,
  1142. enum ttm_bo_type type,
  1143. struct ttm_placement *placement,
  1144. uint32_t page_alignment,
  1145. bool interruptible,
  1146. struct ttm_buffer_object **p_bo)
  1147. {
  1148. struct ttm_buffer_object *bo;
  1149. size_t acc_size;
  1150. int ret;
  1151. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1152. if (unlikely(bo == NULL))
  1153. return -ENOMEM;
  1154. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1155. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1156. interruptible, acc_size,
  1157. NULL, NULL, NULL);
  1158. if (likely(ret == 0))
  1159. *p_bo = bo;
  1160. return ret;
  1161. }
  1162. EXPORT_SYMBOL(ttm_bo_create);
  1163. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1164. unsigned mem_type)
  1165. {
  1166. struct ttm_operation_ctx ctx = {
  1167. .interruptible = false,
  1168. .no_wait_gpu = false,
  1169. .flags = TTM_OPT_FLAG_FORCE_ALLOC
  1170. };
  1171. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1172. struct ttm_bo_global *glob = bdev->glob;
  1173. struct dma_fence *fence;
  1174. int ret;
  1175. unsigned i;
  1176. /*
  1177. * Can't use standard list traversal since we're unlocking.
  1178. */
  1179. spin_lock(&glob->lru_lock);
  1180. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  1181. while (!list_empty(&man->lru[i])) {
  1182. spin_unlock(&glob->lru_lock);
  1183. ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx);
  1184. if (ret)
  1185. return ret;
  1186. spin_lock(&glob->lru_lock);
  1187. }
  1188. }
  1189. spin_unlock(&glob->lru_lock);
  1190. spin_lock(&man->move_lock);
  1191. fence = dma_fence_get(man->move);
  1192. spin_unlock(&man->move_lock);
  1193. if (fence) {
  1194. ret = dma_fence_wait(fence, false);
  1195. dma_fence_put(fence);
  1196. if (ret)
  1197. return ret;
  1198. }
  1199. return 0;
  1200. }
  1201. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1202. {
  1203. struct ttm_mem_type_manager *man;
  1204. int ret = -EINVAL;
  1205. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1206. pr_err("Illegal memory type %d\n", mem_type);
  1207. return ret;
  1208. }
  1209. man = &bdev->man[mem_type];
  1210. if (!man->has_type) {
  1211. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1212. mem_type);
  1213. return ret;
  1214. }
  1215. man->use_type = false;
  1216. man->has_type = false;
  1217. ret = 0;
  1218. if (mem_type > 0) {
  1219. ret = ttm_bo_force_list_clean(bdev, mem_type);
  1220. if (ret) {
  1221. pr_err("Cleanup eviction failed\n");
  1222. return ret;
  1223. }
  1224. ret = (*man->func->takedown)(man);
  1225. }
  1226. dma_fence_put(man->move);
  1227. man->move = NULL;
  1228. return ret;
  1229. }
  1230. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1231. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1232. {
  1233. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1234. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1235. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1236. return -EINVAL;
  1237. }
  1238. if (!man->has_type) {
  1239. pr_err("Memory type %u has not been initialized\n", mem_type);
  1240. return 0;
  1241. }
  1242. return ttm_bo_force_list_clean(bdev, mem_type);
  1243. }
  1244. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1245. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1246. unsigned long p_size)
  1247. {
  1248. int ret;
  1249. struct ttm_mem_type_manager *man;
  1250. unsigned i;
  1251. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1252. man = &bdev->man[type];
  1253. BUG_ON(man->has_type);
  1254. man->io_reserve_fastpath = true;
  1255. man->use_io_reserve_lru = false;
  1256. mutex_init(&man->io_reserve_mutex);
  1257. spin_lock_init(&man->move_lock);
  1258. INIT_LIST_HEAD(&man->io_reserve_lru);
  1259. ret = bdev->driver->init_mem_type(bdev, type, man);
  1260. if (ret)
  1261. return ret;
  1262. man->bdev = bdev;
  1263. if (type != TTM_PL_SYSTEM) {
  1264. ret = (*man->func->init)(man, p_size);
  1265. if (ret)
  1266. return ret;
  1267. }
  1268. man->has_type = true;
  1269. man->use_type = true;
  1270. man->size = p_size;
  1271. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  1272. INIT_LIST_HEAD(&man->lru[i]);
  1273. man->move = NULL;
  1274. return 0;
  1275. }
  1276. EXPORT_SYMBOL(ttm_bo_init_mm);
  1277. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1278. {
  1279. struct ttm_bo_global *glob =
  1280. container_of(kobj, struct ttm_bo_global, kobj);
  1281. __free_page(glob->dummy_read_page);
  1282. kfree(glob);
  1283. }
  1284. void ttm_bo_global_release(struct drm_global_reference *ref)
  1285. {
  1286. struct ttm_bo_global *glob = ref->object;
  1287. kobject_del(&glob->kobj);
  1288. kobject_put(&glob->kobj);
  1289. }
  1290. EXPORT_SYMBOL(ttm_bo_global_release);
  1291. int ttm_bo_global_init(struct drm_global_reference *ref)
  1292. {
  1293. struct ttm_bo_global_ref *bo_ref =
  1294. container_of(ref, struct ttm_bo_global_ref, ref);
  1295. struct ttm_bo_global *glob = ref->object;
  1296. int ret;
  1297. unsigned i;
  1298. mutex_init(&glob->device_list_mutex);
  1299. spin_lock_init(&glob->lru_lock);
  1300. glob->mem_glob = bo_ref->mem_glob;
  1301. glob->mem_glob->bo_glob = glob;
  1302. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1303. if (unlikely(glob->dummy_read_page == NULL)) {
  1304. ret = -ENOMEM;
  1305. goto out_no_drp;
  1306. }
  1307. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  1308. INIT_LIST_HEAD(&glob->swap_lru[i]);
  1309. INIT_LIST_HEAD(&glob->device_list);
  1310. atomic_set(&glob->bo_count, 0);
  1311. ret = kobject_init_and_add(
  1312. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1313. if (unlikely(ret != 0))
  1314. kobject_put(&glob->kobj);
  1315. return ret;
  1316. out_no_drp:
  1317. kfree(glob);
  1318. return ret;
  1319. }
  1320. EXPORT_SYMBOL(ttm_bo_global_init);
  1321. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1322. {
  1323. int ret = 0;
  1324. unsigned i = TTM_NUM_MEM_TYPES;
  1325. struct ttm_mem_type_manager *man;
  1326. struct ttm_bo_global *glob = bdev->glob;
  1327. while (i--) {
  1328. man = &bdev->man[i];
  1329. if (man->has_type) {
  1330. man->use_type = false;
  1331. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1332. ret = -EBUSY;
  1333. pr_err("DRM memory manager type %d is not clean\n",
  1334. i);
  1335. }
  1336. man->has_type = false;
  1337. }
  1338. }
  1339. mutex_lock(&glob->device_list_mutex);
  1340. list_del(&bdev->device_list);
  1341. mutex_unlock(&glob->device_list_mutex);
  1342. cancel_delayed_work_sync(&bdev->wq);
  1343. if (ttm_bo_delayed_delete(bdev, true))
  1344. pr_debug("Delayed destroy list was clean\n");
  1345. spin_lock(&glob->lru_lock);
  1346. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
  1347. if (list_empty(&bdev->man[0].lru[0]))
  1348. pr_debug("Swap list %d was clean\n", i);
  1349. spin_unlock(&glob->lru_lock);
  1350. drm_vma_offset_manager_destroy(&bdev->vma_manager);
  1351. return ret;
  1352. }
  1353. EXPORT_SYMBOL(ttm_bo_device_release);
  1354. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1355. struct ttm_bo_global *glob,
  1356. struct ttm_bo_driver *driver,
  1357. struct address_space *mapping,
  1358. uint64_t file_page_offset,
  1359. bool need_dma32)
  1360. {
  1361. int ret = -EINVAL;
  1362. bdev->driver = driver;
  1363. memset(bdev->man, 0, sizeof(bdev->man));
  1364. /*
  1365. * Initialize the system memory buffer type.
  1366. * Other types need to be driver / IOCTL initialized.
  1367. */
  1368. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1369. if (unlikely(ret != 0))
  1370. goto out_no_sys;
  1371. drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
  1372. 0x10000000);
  1373. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1374. INIT_LIST_HEAD(&bdev->ddestroy);
  1375. bdev->dev_mapping = mapping;
  1376. bdev->glob = glob;
  1377. bdev->need_dma32 = need_dma32;
  1378. mutex_lock(&glob->device_list_mutex);
  1379. list_add_tail(&bdev->device_list, &glob->device_list);
  1380. mutex_unlock(&glob->device_list_mutex);
  1381. return 0;
  1382. out_no_sys:
  1383. return ret;
  1384. }
  1385. EXPORT_SYMBOL(ttm_bo_device_init);
  1386. /*
  1387. * buffer object vm functions.
  1388. */
  1389. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1390. {
  1391. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1392. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1393. if (mem->mem_type == TTM_PL_SYSTEM)
  1394. return false;
  1395. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1396. return false;
  1397. if (mem->placement & TTM_PL_FLAG_CACHED)
  1398. return false;
  1399. }
  1400. return true;
  1401. }
  1402. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1403. {
  1404. struct ttm_bo_device *bdev = bo->bdev;
  1405. drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1406. ttm_mem_io_free_vm(bo);
  1407. }
  1408. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1409. {
  1410. struct ttm_bo_device *bdev = bo->bdev;
  1411. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1412. ttm_mem_io_lock(man, false);
  1413. ttm_bo_unmap_virtual_locked(bo);
  1414. ttm_mem_io_unlock(man);
  1415. }
  1416. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1417. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1418. bool interruptible, bool no_wait)
  1419. {
  1420. long timeout = 15 * HZ;
  1421. if (no_wait) {
  1422. if (reservation_object_test_signaled_rcu(bo->resv, true))
  1423. return 0;
  1424. else
  1425. return -EBUSY;
  1426. }
  1427. timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
  1428. interruptible, timeout);
  1429. if (timeout < 0)
  1430. return timeout;
  1431. if (timeout == 0)
  1432. return -EBUSY;
  1433. reservation_object_add_excl_fence(bo->resv, NULL);
  1434. return 0;
  1435. }
  1436. EXPORT_SYMBOL(ttm_bo_wait);
  1437. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1438. {
  1439. int ret = 0;
  1440. /*
  1441. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1442. */
  1443. ret = ttm_bo_reserve(bo, true, no_wait, NULL);
  1444. if (unlikely(ret != 0))
  1445. return ret;
  1446. ret = ttm_bo_wait(bo, true, no_wait);
  1447. if (likely(ret == 0))
  1448. atomic_inc(&bo->cpu_writers);
  1449. ttm_bo_unreserve(bo);
  1450. return ret;
  1451. }
  1452. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1453. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1454. {
  1455. atomic_dec(&bo->cpu_writers);
  1456. }
  1457. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1458. /**
  1459. * A buffer object shrink method that tries to swap out the first
  1460. * buffer object on the bo_global::swap_lru list.
  1461. */
  1462. int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx)
  1463. {
  1464. struct ttm_buffer_object *bo;
  1465. int ret = -EBUSY;
  1466. bool locked;
  1467. unsigned i;
  1468. spin_lock(&glob->lru_lock);
  1469. for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
  1470. list_for_each_entry(bo, &glob->swap_lru[i], swap) {
  1471. if (ttm_bo_evict_swapout_allowable(bo, ctx, &locked)) {
  1472. ret = 0;
  1473. break;
  1474. }
  1475. }
  1476. if (!ret)
  1477. break;
  1478. }
  1479. if (ret) {
  1480. spin_unlock(&glob->lru_lock);
  1481. return ret;
  1482. }
  1483. kref_get(&bo->list_kref);
  1484. if (!list_empty(&bo->ddestroy)) {
  1485. ret = ttm_bo_cleanup_refs(bo, false, false, locked);
  1486. kref_put(&bo->list_kref, ttm_bo_release_list);
  1487. return ret;
  1488. }
  1489. ttm_bo_del_from_lru(bo);
  1490. spin_unlock(&glob->lru_lock);
  1491. /**
  1492. * Move to system cached
  1493. */
  1494. if (bo->mem.mem_type != TTM_PL_SYSTEM ||
  1495. bo->ttm->caching_state != tt_cached) {
  1496. struct ttm_operation_ctx ctx = { false, false };
  1497. struct ttm_mem_reg evict_mem;
  1498. evict_mem = bo->mem;
  1499. evict_mem.mm_node = NULL;
  1500. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1501. evict_mem.mem_type = TTM_PL_SYSTEM;
  1502. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx);
  1503. if (unlikely(ret != 0))
  1504. goto out;
  1505. }
  1506. /**
  1507. * Make sure BO is idle.
  1508. */
  1509. ret = ttm_bo_wait(bo, false, false);
  1510. if (unlikely(ret != 0))
  1511. goto out;
  1512. ttm_bo_unmap_virtual(bo);
  1513. /**
  1514. * Swap out. Buffer will be swapped in again as soon as
  1515. * anyone tries to access a ttm page.
  1516. */
  1517. if (bo->bdev->driver->swap_notify)
  1518. bo->bdev->driver->swap_notify(bo);
  1519. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1520. out:
  1521. /**
  1522. *
  1523. * Unreserve without putting on LRU to avoid swapping out an
  1524. * already swapped buffer.
  1525. */
  1526. if (locked)
  1527. reservation_object_unlock(bo->resv);
  1528. kref_put(&bo->list_kref, ttm_bo_release_list);
  1529. return ret;
  1530. }
  1531. EXPORT_SYMBOL(ttm_bo_swapout);
  1532. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1533. {
  1534. struct ttm_operation_ctx ctx = {
  1535. .interruptible = false,
  1536. .no_wait_gpu = false
  1537. };
  1538. while (ttm_bo_swapout(bdev->glob, &ctx) == 0)
  1539. ;
  1540. }
  1541. EXPORT_SYMBOL(ttm_bo_swapout_all);
  1542. /**
  1543. * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
  1544. * unreserved
  1545. *
  1546. * @bo: Pointer to buffer
  1547. */
  1548. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1549. {
  1550. int ret;
  1551. /*
  1552. * In the absense of a wait_unlocked API,
  1553. * Use the bo::wu_mutex to avoid triggering livelocks due to
  1554. * concurrent use of this function. Note that this use of
  1555. * bo::wu_mutex can go away if we change locking order to
  1556. * mmap_sem -> bo::reserve.
  1557. */
  1558. ret = mutex_lock_interruptible(&bo->wu_mutex);
  1559. if (unlikely(ret != 0))
  1560. return -ERESTARTSYS;
  1561. if (!ww_mutex_is_locked(&bo->resv->lock))
  1562. goto out_unlock;
  1563. ret = reservation_object_lock_interruptible(bo->resv, NULL);
  1564. if (ret == -EINTR)
  1565. ret = -ERESTARTSYS;
  1566. if (unlikely(ret != 0))
  1567. goto out_unlock;
  1568. reservation_object_unlock(bo->resv);
  1569. out_unlock:
  1570. mutex_unlock(&bo->wu_mutex);
  1571. return ret;
  1572. }