ttm_bo.c 46 KB

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