ttm_bo.c 45 KB

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