ttm_bo.c 43 KB

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