ttm_bo.c 43 KB

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