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