ttm_bo.c 45 KB

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