ttm_bo.c 46 KB

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