ttm_bo.c 45 KB

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