ttm_bo.c 46 KB

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