ttm_bo.c 46 KB

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