ttm_bo.c 45 KB

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