ttm_bo.c 44 KB

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