ttm_bo.c 45 KB

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