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