ttm_bo.c 44 KB

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