ttm_bo.c 42 KB

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