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