ttm_bo.c 45 KB

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