ttm_bo.c 45 KB

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