ttm_bo.c 45 KB

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