ttm_bo.c 45 KB

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