ttm_bo.c 44 KB

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