ttm_bo.c 45 KB

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