ttm_bo.c 45 KB

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