ttm_bo.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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_reserve, 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_reserve, no_wait_gpu, mem);
  361. else if (bdev->driver->move)
  362. ret = bdev->driver->move(bo, evict, interruptible,
  363. no_wait_reserve, no_wait_gpu, mem);
  364. else
  365. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, 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. }
  374. goto out_err;
  375. }
  376. moved:
  377. if (bo->evicted) {
  378. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  379. if (ret)
  380. pr_err("Can not flush read caches\n");
  381. bo->evicted = false;
  382. }
  383. if (bo->mem.mm_node) {
  384. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  385. bdev->man[bo->mem.mem_type].gpu_offset;
  386. bo->cur_placement = bo->mem.placement;
  387. } else
  388. bo->offset = 0;
  389. return 0;
  390. out_err:
  391. new_man = &bdev->man[bo->mem.mem_type];
  392. if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
  393. ttm_tt_unbind(bo->ttm);
  394. ttm_tt_destroy(bo->ttm);
  395. bo->ttm = NULL;
  396. }
  397. return ret;
  398. }
  399. /**
  400. * Call bo::reserved.
  401. * Will release GPU memory type usage on destruction.
  402. * This is the place to put in driver specific hooks to release
  403. * driver private resources.
  404. * Will release the bo::reserved lock.
  405. */
  406. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  407. {
  408. if (bo->bdev->driver->move_notify)
  409. bo->bdev->driver->move_notify(bo, NULL);
  410. if (bo->ttm) {
  411. ttm_tt_unbind(bo->ttm);
  412. ttm_tt_destroy(bo->ttm);
  413. bo->ttm = NULL;
  414. }
  415. ttm_bo_mem_put(bo, &bo->mem);
  416. atomic_set(&bo->reserved, 0);
  417. wake_up_all(&bo->event_queue);
  418. /*
  419. * Since the final reference to this bo may not be dropped by
  420. * the current task we have to put a memory barrier here to make
  421. * sure the changes done in this function are always visible.
  422. *
  423. * This function only needs protection against the final kref_put.
  424. */
  425. smp_mb__before_atomic_dec();
  426. }
  427. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  428. {
  429. struct ttm_bo_device *bdev = bo->bdev;
  430. struct ttm_bo_global *glob = bo->glob;
  431. struct ttm_bo_driver *driver = bdev->driver;
  432. void *sync_obj = NULL;
  433. int put_count;
  434. int ret;
  435. spin_lock(&glob->lru_lock);
  436. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  437. spin_lock(&bdev->fence_lock);
  438. (void) ttm_bo_wait(bo, false, false, true);
  439. if (!ret && !bo->sync_obj) {
  440. spin_unlock(&bdev->fence_lock);
  441. put_count = ttm_bo_del_from_lru(bo);
  442. spin_unlock(&glob->lru_lock);
  443. ttm_bo_cleanup_memtype_use(bo);
  444. ttm_bo_list_ref_sub(bo, put_count, true);
  445. return;
  446. }
  447. if (bo->sync_obj)
  448. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  449. spin_unlock(&bdev->fence_lock);
  450. if (!ret) {
  451. atomic_set(&bo->reserved, 0);
  452. wake_up_all(&bo->event_queue);
  453. }
  454. kref_get(&bo->list_kref);
  455. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  456. spin_unlock(&glob->lru_lock);
  457. if (sync_obj) {
  458. driver->sync_obj_flush(sync_obj);
  459. driver->sync_obj_unref(&sync_obj);
  460. }
  461. schedule_delayed_work(&bdev->wq,
  462. ((HZ / 100) < 1) ? 1 : HZ / 100);
  463. }
  464. /**
  465. * function ttm_bo_cleanup_refs_and_unlock
  466. * If bo idle, remove from delayed- and lru lists, and unref.
  467. * If not idle, do nothing.
  468. *
  469. * Must be called with lru_lock and reservation held, this function
  470. * will drop both before returning.
  471. *
  472. * @interruptible Any sleeps should occur interruptibly.
  473. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  474. */
  475. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  476. bool interruptible,
  477. bool no_wait_gpu)
  478. {
  479. struct ttm_bo_device *bdev = bo->bdev;
  480. struct ttm_bo_driver *driver = bdev->driver;
  481. struct ttm_bo_global *glob = bo->glob;
  482. int put_count;
  483. int ret;
  484. spin_lock(&bdev->fence_lock);
  485. ret = ttm_bo_wait(bo, false, false, true);
  486. if (ret && !no_wait_gpu) {
  487. void *sync_obj;
  488. /*
  489. * Take a reference to the fence and unreserve,
  490. * at this point the buffer should be dead, so
  491. * no new sync objects can be attached.
  492. */
  493. sync_obj = driver->sync_obj_ref(&bo->sync_obj);
  494. spin_unlock(&bdev->fence_lock);
  495. atomic_set(&bo->reserved, 0);
  496. wake_up_all(&bo->event_queue);
  497. spin_unlock(&glob->lru_lock);
  498. ret = driver->sync_obj_wait(sync_obj, false, interruptible);
  499. driver->sync_obj_unref(&sync_obj);
  500. if (ret)
  501. return ret;
  502. /*
  503. * remove sync_obj with ttm_bo_wait, the wait should be
  504. * finished, and no new wait object should have been added.
  505. */
  506. spin_lock(&bdev->fence_lock);
  507. ret = ttm_bo_wait(bo, false, false, true);
  508. WARN_ON(ret);
  509. spin_unlock(&bdev->fence_lock);
  510. if (ret)
  511. return ret;
  512. spin_lock(&glob->lru_lock);
  513. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  514. /*
  515. * We raced, and lost, someone else holds the reservation now,
  516. * and is probably busy in ttm_bo_cleanup_memtype_use.
  517. *
  518. * Even if it's not the case, because we finished waiting any
  519. * delayed destruction would succeed, so just return success
  520. * here.
  521. */
  522. if (ret) {
  523. spin_unlock(&glob->lru_lock);
  524. return 0;
  525. }
  526. } else
  527. spin_unlock(&bdev->fence_lock);
  528. if (ret || unlikely(list_empty(&bo->ddestroy))) {
  529. atomic_set(&bo->reserved, 0);
  530. wake_up_all(&bo->event_queue);
  531. spin_unlock(&glob->lru_lock);
  532. return ret;
  533. }
  534. put_count = ttm_bo_del_from_lru(bo);
  535. list_del_init(&bo->ddestroy);
  536. ++put_count;
  537. spin_unlock(&glob->lru_lock);
  538. ttm_bo_cleanup_memtype_use(bo);
  539. ttm_bo_list_ref_sub(bo, put_count, true);
  540. return 0;
  541. }
  542. /**
  543. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  544. * encountered buffers.
  545. */
  546. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  547. {
  548. struct ttm_bo_global *glob = bdev->glob;
  549. struct ttm_buffer_object *entry = NULL;
  550. int ret = 0;
  551. spin_lock(&glob->lru_lock);
  552. if (list_empty(&bdev->ddestroy))
  553. goto out_unlock;
  554. entry = list_first_entry(&bdev->ddestroy,
  555. struct ttm_buffer_object, ddestroy);
  556. kref_get(&entry->list_kref);
  557. for (;;) {
  558. struct ttm_buffer_object *nentry = NULL;
  559. if (entry->ddestroy.next != &bdev->ddestroy) {
  560. nentry = list_first_entry(&entry->ddestroy,
  561. struct ttm_buffer_object, ddestroy);
  562. kref_get(&nentry->list_kref);
  563. }
  564. ret = ttm_bo_reserve_locked(entry, false, !remove_all, false, 0);
  565. if (!ret)
  566. ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  567. !remove_all);
  568. else
  569. spin_unlock(&glob->lru_lock);
  570. kref_put(&entry->list_kref, ttm_bo_release_list);
  571. entry = nentry;
  572. if (ret || !entry)
  573. goto out;
  574. spin_lock(&glob->lru_lock);
  575. if (list_empty(&entry->ddestroy))
  576. break;
  577. }
  578. out_unlock:
  579. spin_unlock(&glob->lru_lock);
  580. out:
  581. if (entry)
  582. kref_put(&entry->list_kref, ttm_bo_release_list);
  583. return ret;
  584. }
  585. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  586. {
  587. struct ttm_bo_device *bdev =
  588. container_of(work, struct ttm_bo_device, wq.work);
  589. if (ttm_bo_delayed_delete(bdev, false)) {
  590. schedule_delayed_work(&bdev->wq,
  591. ((HZ / 100) < 1) ? 1 : HZ / 100);
  592. }
  593. }
  594. static void ttm_bo_release(struct kref *kref)
  595. {
  596. struct ttm_buffer_object *bo =
  597. container_of(kref, struct ttm_buffer_object, kref);
  598. struct ttm_bo_device *bdev = bo->bdev;
  599. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  600. write_lock(&bdev->vm_lock);
  601. if (likely(bo->vm_node != NULL)) {
  602. rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
  603. drm_mm_put_block(bo->vm_node);
  604. bo->vm_node = NULL;
  605. }
  606. write_unlock(&bdev->vm_lock);
  607. ttm_mem_io_lock(man, false);
  608. ttm_mem_io_free_vm(bo);
  609. ttm_mem_io_unlock(man);
  610. ttm_bo_cleanup_refs_or_queue(bo);
  611. kref_put(&bo->list_kref, ttm_bo_release_list);
  612. }
  613. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  614. {
  615. struct ttm_buffer_object *bo = *p_bo;
  616. *p_bo = NULL;
  617. kref_put(&bo->kref, ttm_bo_release);
  618. }
  619. EXPORT_SYMBOL(ttm_bo_unref);
  620. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  621. {
  622. return cancel_delayed_work_sync(&bdev->wq);
  623. }
  624. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  625. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  626. {
  627. if (resched)
  628. schedule_delayed_work(&bdev->wq,
  629. ((HZ / 100) < 1) ? 1 : HZ / 100);
  630. }
  631. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  632. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  633. bool no_wait_reserve, bool no_wait_gpu)
  634. {
  635. struct ttm_bo_device *bdev = bo->bdev;
  636. struct ttm_mem_reg evict_mem;
  637. struct ttm_placement placement;
  638. int ret = 0;
  639. spin_lock(&bdev->fence_lock);
  640. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  641. spin_unlock(&bdev->fence_lock);
  642. if (unlikely(ret != 0)) {
  643. if (ret != -ERESTARTSYS) {
  644. pr_err("Failed to expire sync object before buffer eviction\n");
  645. }
  646. goto out;
  647. }
  648. BUG_ON(!ttm_bo_is_reserved(bo));
  649. evict_mem = bo->mem;
  650. evict_mem.mm_node = NULL;
  651. evict_mem.bus.io_reserved_vm = false;
  652. evict_mem.bus.io_reserved_count = 0;
  653. placement.fpfn = 0;
  654. placement.lpfn = 0;
  655. placement.num_placement = 0;
  656. placement.num_busy_placement = 0;
  657. bdev->driver->evict_flags(bo, &placement);
  658. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  659. no_wait_reserve, no_wait_gpu);
  660. if (ret) {
  661. if (ret != -ERESTARTSYS) {
  662. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  663. bo);
  664. ttm_bo_mem_space_debug(bo, &placement);
  665. }
  666. goto out;
  667. }
  668. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  669. no_wait_reserve, no_wait_gpu);
  670. if (ret) {
  671. if (ret != -ERESTARTSYS)
  672. pr_err("Buffer eviction failed\n");
  673. ttm_bo_mem_put(bo, &evict_mem);
  674. goto out;
  675. }
  676. bo->evicted = true;
  677. out:
  678. return ret;
  679. }
  680. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  681. uint32_t mem_type,
  682. bool interruptible, bool no_wait_reserve,
  683. bool no_wait_gpu)
  684. {
  685. struct ttm_bo_global *glob = bdev->glob;
  686. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  687. struct ttm_buffer_object *bo;
  688. int ret = -EBUSY, put_count;
  689. spin_lock(&glob->lru_lock);
  690. list_for_each_entry(bo, &man->lru, lru) {
  691. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  692. if (!ret)
  693. break;
  694. }
  695. if (ret) {
  696. spin_unlock(&glob->lru_lock);
  697. return ret;
  698. }
  699. kref_get(&bo->list_kref);
  700. if (!list_empty(&bo->ddestroy)) {
  701. ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
  702. no_wait_gpu);
  703. kref_put(&bo->list_kref, ttm_bo_release_list);
  704. return ret;
  705. }
  706. put_count = ttm_bo_del_from_lru(bo);
  707. spin_unlock(&glob->lru_lock);
  708. BUG_ON(ret != 0);
  709. ttm_bo_list_ref_sub(bo, put_count, true);
  710. ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
  711. ttm_bo_unreserve(bo);
  712. kref_put(&bo->list_kref, ttm_bo_release_list);
  713. return ret;
  714. }
  715. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  716. {
  717. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  718. if (mem->mm_node)
  719. (*man->func->put_node)(man, mem);
  720. }
  721. EXPORT_SYMBOL(ttm_bo_mem_put);
  722. /**
  723. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  724. * space, or we've evicted everything and there isn't enough space.
  725. */
  726. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  727. uint32_t mem_type,
  728. struct ttm_placement *placement,
  729. struct ttm_mem_reg *mem,
  730. bool interruptible,
  731. bool no_wait_reserve,
  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, interruptible,
  744. no_wait_reserve, 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, bool no_wait_reserve,
  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_reserve, 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, 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(!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, 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(!ttm_bo_is_reserved(bo));
  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. bool interruptible,
  995. struct file *persistent_swap_storage,
  996. size_t acc_size,
  997. struct sg_table *sg,
  998. void (*destroy) (struct ttm_buffer_object *))
  999. {
  1000. int ret = 0;
  1001. unsigned long num_pages;
  1002. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  1003. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  1004. if (ret) {
  1005. pr_err("Out of kernel memory\n");
  1006. if (destroy)
  1007. (*destroy)(bo);
  1008. else
  1009. kfree(bo);
  1010. return -ENOMEM;
  1011. }
  1012. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1013. if (num_pages == 0) {
  1014. pr_err("Illegal buffer object size\n");
  1015. if (destroy)
  1016. (*destroy)(bo);
  1017. else
  1018. kfree(bo);
  1019. ttm_mem_global_free(mem_glob, acc_size);
  1020. return -EINVAL;
  1021. }
  1022. bo->destroy = destroy;
  1023. kref_init(&bo->kref);
  1024. kref_init(&bo->list_kref);
  1025. atomic_set(&bo->cpu_writers, 0);
  1026. atomic_set(&bo->reserved, 1);
  1027. init_waitqueue_head(&bo->event_queue);
  1028. INIT_LIST_HEAD(&bo->lru);
  1029. INIT_LIST_HEAD(&bo->ddestroy);
  1030. INIT_LIST_HEAD(&bo->swap);
  1031. INIT_LIST_HEAD(&bo->io_reserve_lru);
  1032. bo->bdev = bdev;
  1033. bo->glob = bdev->glob;
  1034. bo->type = type;
  1035. bo->num_pages = num_pages;
  1036. bo->mem.size = num_pages << PAGE_SHIFT;
  1037. bo->mem.mem_type = TTM_PL_SYSTEM;
  1038. bo->mem.num_pages = bo->num_pages;
  1039. bo->mem.mm_node = NULL;
  1040. bo->mem.page_alignment = page_alignment;
  1041. bo->mem.bus.io_reserved_vm = false;
  1042. bo->mem.bus.io_reserved_count = 0;
  1043. bo->priv_flags = 0;
  1044. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1045. bo->seq_valid = false;
  1046. bo->persistent_swap_storage = persistent_swap_storage;
  1047. bo->acc_size = acc_size;
  1048. bo->sg = sg;
  1049. atomic_inc(&bo->glob->bo_count);
  1050. ret = ttm_bo_check_placement(bo, placement);
  1051. if (unlikely(ret != 0))
  1052. goto out_err;
  1053. /*
  1054. * For ttm_bo_type_device buffers, allocate
  1055. * address space from the device.
  1056. */
  1057. if (bo->type == ttm_bo_type_device ||
  1058. bo->type == ttm_bo_type_sg) {
  1059. ret = ttm_bo_setup_vm(bo);
  1060. if (ret)
  1061. goto out_err;
  1062. }
  1063. ret = ttm_bo_validate(bo, placement, interruptible, false, false);
  1064. if (ret)
  1065. goto out_err;
  1066. ttm_bo_unreserve(bo);
  1067. return 0;
  1068. out_err:
  1069. ttm_bo_unreserve(bo);
  1070. ttm_bo_unref(&bo);
  1071. return ret;
  1072. }
  1073. EXPORT_SYMBOL(ttm_bo_init);
  1074. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1075. unsigned long bo_size,
  1076. unsigned struct_size)
  1077. {
  1078. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1079. size_t size = 0;
  1080. size += ttm_round_pot(struct_size);
  1081. size += PAGE_ALIGN(npages * sizeof(void *));
  1082. size += ttm_round_pot(sizeof(struct ttm_tt));
  1083. return size;
  1084. }
  1085. EXPORT_SYMBOL(ttm_bo_acc_size);
  1086. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1087. unsigned long bo_size,
  1088. unsigned struct_size)
  1089. {
  1090. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1091. size_t size = 0;
  1092. size += ttm_round_pot(struct_size);
  1093. size += PAGE_ALIGN(npages * sizeof(void *));
  1094. size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
  1095. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1096. return size;
  1097. }
  1098. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1099. int ttm_bo_create(struct ttm_bo_device *bdev,
  1100. unsigned long size,
  1101. enum ttm_bo_type type,
  1102. struct ttm_placement *placement,
  1103. uint32_t page_alignment,
  1104. bool interruptible,
  1105. struct file *persistent_swap_storage,
  1106. struct ttm_buffer_object **p_bo)
  1107. {
  1108. struct ttm_buffer_object *bo;
  1109. size_t acc_size;
  1110. int ret;
  1111. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1112. if (unlikely(bo == NULL))
  1113. return -ENOMEM;
  1114. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1115. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1116. interruptible, persistent_swap_storage, acc_size,
  1117. NULL, NULL);
  1118. if (likely(ret == 0))
  1119. *p_bo = bo;
  1120. return ret;
  1121. }
  1122. EXPORT_SYMBOL(ttm_bo_create);
  1123. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1124. unsigned mem_type, bool allow_errors)
  1125. {
  1126. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1127. struct ttm_bo_global *glob = bdev->glob;
  1128. int ret;
  1129. /*
  1130. * Can't use standard list traversal since we're unlocking.
  1131. */
  1132. spin_lock(&glob->lru_lock);
  1133. while (!list_empty(&man->lru)) {
  1134. spin_unlock(&glob->lru_lock);
  1135. ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
  1136. if (ret) {
  1137. if (allow_errors) {
  1138. return ret;
  1139. } else {
  1140. pr_err("Cleanup eviction failed\n");
  1141. }
  1142. }
  1143. spin_lock(&glob->lru_lock);
  1144. }
  1145. spin_unlock(&glob->lru_lock);
  1146. return 0;
  1147. }
  1148. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1149. {
  1150. struct ttm_mem_type_manager *man;
  1151. int ret = -EINVAL;
  1152. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1153. pr_err("Illegal memory type %d\n", mem_type);
  1154. return ret;
  1155. }
  1156. man = &bdev->man[mem_type];
  1157. if (!man->has_type) {
  1158. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1159. mem_type);
  1160. return ret;
  1161. }
  1162. man->use_type = false;
  1163. man->has_type = false;
  1164. ret = 0;
  1165. if (mem_type > 0) {
  1166. ttm_bo_force_list_clean(bdev, mem_type, false);
  1167. ret = (*man->func->takedown)(man);
  1168. }
  1169. return ret;
  1170. }
  1171. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1172. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1173. {
  1174. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1175. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1176. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1177. return -EINVAL;
  1178. }
  1179. if (!man->has_type) {
  1180. pr_err("Memory type %u has not been initialized\n", mem_type);
  1181. return 0;
  1182. }
  1183. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1184. }
  1185. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1186. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1187. unsigned long p_size)
  1188. {
  1189. int ret = -EINVAL;
  1190. struct ttm_mem_type_manager *man;
  1191. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1192. man = &bdev->man[type];
  1193. BUG_ON(man->has_type);
  1194. man->io_reserve_fastpath = true;
  1195. man->use_io_reserve_lru = false;
  1196. mutex_init(&man->io_reserve_mutex);
  1197. INIT_LIST_HEAD(&man->io_reserve_lru);
  1198. ret = bdev->driver->init_mem_type(bdev, type, man);
  1199. if (ret)
  1200. return ret;
  1201. man->bdev = bdev;
  1202. ret = 0;
  1203. if (type != TTM_PL_SYSTEM) {
  1204. ret = (*man->func->init)(man, p_size);
  1205. if (ret)
  1206. return ret;
  1207. }
  1208. man->has_type = true;
  1209. man->use_type = true;
  1210. man->size = p_size;
  1211. INIT_LIST_HEAD(&man->lru);
  1212. return 0;
  1213. }
  1214. EXPORT_SYMBOL(ttm_bo_init_mm);
  1215. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1216. {
  1217. struct ttm_bo_global *glob =
  1218. container_of(kobj, struct ttm_bo_global, kobj);
  1219. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1220. __free_page(glob->dummy_read_page);
  1221. kfree(glob);
  1222. }
  1223. void ttm_bo_global_release(struct drm_global_reference *ref)
  1224. {
  1225. struct ttm_bo_global *glob = ref->object;
  1226. kobject_del(&glob->kobj);
  1227. kobject_put(&glob->kobj);
  1228. }
  1229. EXPORT_SYMBOL(ttm_bo_global_release);
  1230. int ttm_bo_global_init(struct drm_global_reference *ref)
  1231. {
  1232. struct ttm_bo_global_ref *bo_ref =
  1233. container_of(ref, struct ttm_bo_global_ref, ref);
  1234. struct ttm_bo_global *glob = ref->object;
  1235. int ret;
  1236. mutex_init(&glob->device_list_mutex);
  1237. spin_lock_init(&glob->lru_lock);
  1238. glob->mem_glob = bo_ref->mem_glob;
  1239. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1240. if (unlikely(glob->dummy_read_page == NULL)) {
  1241. ret = -ENOMEM;
  1242. goto out_no_drp;
  1243. }
  1244. INIT_LIST_HEAD(&glob->swap_lru);
  1245. INIT_LIST_HEAD(&glob->device_list);
  1246. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1247. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1248. if (unlikely(ret != 0)) {
  1249. pr_err("Could not register buffer object swapout\n");
  1250. goto out_no_shrink;
  1251. }
  1252. atomic_set(&glob->bo_count, 0);
  1253. ret = kobject_init_and_add(
  1254. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1255. if (unlikely(ret != 0))
  1256. kobject_put(&glob->kobj);
  1257. return ret;
  1258. out_no_shrink:
  1259. __free_page(glob->dummy_read_page);
  1260. out_no_drp:
  1261. kfree(glob);
  1262. return ret;
  1263. }
  1264. EXPORT_SYMBOL(ttm_bo_global_init);
  1265. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1266. {
  1267. int ret = 0;
  1268. unsigned i = TTM_NUM_MEM_TYPES;
  1269. struct ttm_mem_type_manager *man;
  1270. struct ttm_bo_global *glob = bdev->glob;
  1271. while (i--) {
  1272. man = &bdev->man[i];
  1273. if (man->has_type) {
  1274. man->use_type = false;
  1275. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1276. ret = -EBUSY;
  1277. pr_err("DRM memory manager type %d is not clean\n",
  1278. i);
  1279. }
  1280. man->has_type = false;
  1281. }
  1282. }
  1283. mutex_lock(&glob->device_list_mutex);
  1284. list_del(&bdev->device_list);
  1285. mutex_unlock(&glob->device_list_mutex);
  1286. cancel_delayed_work_sync(&bdev->wq);
  1287. while (ttm_bo_delayed_delete(bdev, true))
  1288. ;
  1289. spin_lock(&glob->lru_lock);
  1290. if (list_empty(&bdev->ddestroy))
  1291. TTM_DEBUG("Delayed destroy list was clean\n");
  1292. if (list_empty(&bdev->man[0].lru))
  1293. TTM_DEBUG("Swap list was clean\n");
  1294. spin_unlock(&glob->lru_lock);
  1295. BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
  1296. write_lock(&bdev->vm_lock);
  1297. drm_mm_takedown(&bdev->addr_space_mm);
  1298. write_unlock(&bdev->vm_lock);
  1299. return ret;
  1300. }
  1301. EXPORT_SYMBOL(ttm_bo_device_release);
  1302. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1303. struct ttm_bo_global *glob,
  1304. struct ttm_bo_driver *driver,
  1305. uint64_t file_page_offset,
  1306. bool need_dma32)
  1307. {
  1308. int ret = -EINVAL;
  1309. rwlock_init(&bdev->vm_lock);
  1310. bdev->driver = driver;
  1311. memset(bdev->man, 0, sizeof(bdev->man));
  1312. /*
  1313. * Initialize the system memory buffer type.
  1314. * Other types need to be driver / IOCTL initialized.
  1315. */
  1316. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1317. if (unlikely(ret != 0))
  1318. goto out_no_sys;
  1319. bdev->addr_space_rb = RB_ROOT;
  1320. ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
  1321. if (unlikely(ret != 0))
  1322. goto out_no_addr_mm;
  1323. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1324. INIT_LIST_HEAD(&bdev->ddestroy);
  1325. bdev->dev_mapping = NULL;
  1326. bdev->glob = glob;
  1327. bdev->need_dma32 = need_dma32;
  1328. bdev->val_seq = 0;
  1329. spin_lock_init(&bdev->fence_lock);
  1330. mutex_lock(&glob->device_list_mutex);
  1331. list_add_tail(&bdev->device_list, &glob->device_list);
  1332. mutex_unlock(&glob->device_list_mutex);
  1333. return 0;
  1334. out_no_addr_mm:
  1335. ttm_bo_clean_mm(bdev, 0);
  1336. out_no_sys:
  1337. return ret;
  1338. }
  1339. EXPORT_SYMBOL(ttm_bo_device_init);
  1340. /*
  1341. * buffer object vm functions.
  1342. */
  1343. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1344. {
  1345. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1346. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1347. if (mem->mem_type == TTM_PL_SYSTEM)
  1348. return false;
  1349. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1350. return false;
  1351. if (mem->placement & TTM_PL_FLAG_CACHED)
  1352. return false;
  1353. }
  1354. return true;
  1355. }
  1356. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1357. {
  1358. struct ttm_bo_device *bdev = bo->bdev;
  1359. loff_t offset = (loff_t) bo->addr_space_offset;
  1360. loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
  1361. if (!bdev->dev_mapping)
  1362. return;
  1363. unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
  1364. ttm_mem_io_free_vm(bo);
  1365. }
  1366. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1367. {
  1368. struct ttm_bo_device *bdev = bo->bdev;
  1369. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1370. ttm_mem_io_lock(man, false);
  1371. ttm_bo_unmap_virtual_locked(bo);
  1372. ttm_mem_io_unlock(man);
  1373. }
  1374. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1375. static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
  1376. {
  1377. struct ttm_bo_device *bdev = bo->bdev;
  1378. struct rb_node **cur = &bdev->addr_space_rb.rb_node;
  1379. struct rb_node *parent = NULL;
  1380. struct ttm_buffer_object *cur_bo;
  1381. unsigned long offset = bo->vm_node->start;
  1382. unsigned long cur_offset;
  1383. while (*cur) {
  1384. parent = *cur;
  1385. cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
  1386. cur_offset = cur_bo->vm_node->start;
  1387. if (offset < cur_offset)
  1388. cur = &parent->rb_left;
  1389. else if (offset > cur_offset)
  1390. cur = &parent->rb_right;
  1391. else
  1392. BUG();
  1393. }
  1394. rb_link_node(&bo->vm_rb, parent, cur);
  1395. rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
  1396. }
  1397. /**
  1398. * ttm_bo_setup_vm:
  1399. *
  1400. * @bo: the buffer to allocate address space for
  1401. *
  1402. * Allocate address space in the drm device so that applications
  1403. * can mmap the buffer and access the contents. This only
  1404. * applies to ttm_bo_type_device objects as others are not
  1405. * placed in the drm device address space.
  1406. */
  1407. static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
  1408. {
  1409. struct ttm_bo_device *bdev = bo->bdev;
  1410. int ret;
  1411. retry_pre_get:
  1412. ret = drm_mm_pre_get(&bdev->addr_space_mm);
  1413. if (unlikely(ret != 0))
  1414. return ret;
  1415. write_lock(&bdev->vm_lock);
  1416. bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
  1417. bo->mem.num_pages, 0, 0);
  1418. if (unlikely(bo->vm_node == NULL)) {
  1419. ret = -ENOMEM;
  1420. goto out_unlock;
  1421. }
  1422. bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
  1423. bo->mem.num_pages, 0);
  1424. if (unlikely(bo->vm_node == NULL)) {
  1425. write_unlock(&bdev->vm_lock);
  1426. goto retry_pre_get;
  1427. }
  1428. ttm_bo_vm_insert_rb(bo);
  1429. write_unlock(&bdev->vm_lock);
  1430. bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
  1431. return 0;
  1432. out_unlock:
  1433. write_unlock(&bdev->vm_lock);
  1434. return ret;
  1435. }
  1436. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1437. bool lazy, bool interruptible, bool no_wait)
  1438. {
  1439. struct ttm_bo_driver *driver = bo->bdev->driver;
  1440. struct ttm_bo_device *bdev = bo->bdev;
  1441. void *sync_obj;
  1442. int ret = 0;
  1443. if (likely(bo->sync_obj == NULL))
  1444. return 0;
  1445. while (bo->sync_obj) {
  1446. if (driver->sync_obj_signaled(bo->sync_obj)) {
  1447. void *tmp_obj = bo->sync_obj;
  1448. bo->sync_obj = NULL;
  1449. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1450. spin_unlock(&bdev->fence_lock);
  1451. driver->sync_obj_unref(&tmp_obj);
  1452. spin_lock(&bdev->fence_lock);
  1453. continue;
  1454. }
  1455. if (no_wait)
  1456. return -EBUSY;
  1457. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  1458. spin_unlock(&bdev->fence_lock);
  1459. ret = driver->sync_obj_wait(sync_obj,
  1460. lazy, interruptible);
  1461. if (unlikely(ret != 0)) {
  1462. driver->sync_obj_unref(&sync_obj);
  1463. spin_lock(&bdev->fence_lock);
  1464. return ret;
  1465. }
  1466. spin_lock(&bdev->fence_lock);
  1467. if (likely(bo->sync_obj == sync_obj)) {
  1468. void *tmp_obj = bo->sync_obj;
  1469. bo->sync_obj = NULL;
  1470. clear_bit(TTM_BO_PRIV_FLAG_MOVING,
  1471. &bo->priv_flags);
  1472. spin_unlock(&bdev->fence_lock);
  1473. driver->sync_obj_unref(&sync_obj);
  1474. driver->sync_obj_unref(&tmp_obj);
  1475. spin_lock(&bdev->fence_lock);
  1476. } else {
  1477. spin_unlock(&bdev->fence_lock);
  1478. driver->sync_obj_unref(&sync_obj);
  1479. spin_lock(&bdev->fence_lock);
  1480. }
  1481. }
  1482. return 0;
  1483. }
  1484. EXPORT_SYMBOL(ttm_bo_wait);
  1485. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1486. {
  1487. struct ttm_bo_device *bdev = bo->bdev;
  1488. int ret = 0;
  1489. /*
  1490. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1491. */
  1492. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  1493. if (unlikely(ret != 0))
  1494. return ret;
  1495. spin_lock(&bdev->fence_lock);
  1496. ret = ttm_bo_wait(bo, false, true, no_wait);
  1497. spin_unlock(&bdev->fence_lock);
  1498. if (likely(ret == 0))
  1499. atomic_inc(&bo->cpu_writers);
  1500. ttm_bo_unreserve(bo);
  1501. return ret;
  1502. }
  1503. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1504. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1505. {
  1506. atomic_dec(&bo->cpu_writers);
  1507. }
  1508. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1509. /**
  1510. * A buffer object shrink method that tries to swap out the first
  1511. * buffer object on the bo_global::swap_lru list.
  1512. */
  1513. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1514. {
  1515. struct ttm_bo_global *glob =
  1516. container_of(shrink, struct ttm_bo_global, shrink);
  1517. struct ttm_buffer_object *bo;
  1518. int ret = -EBUSY;
  1519. int put_count;
  1520. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1521. spin_lock(&glob->lru_lock);
  1522. list_for_each_entry(bo, &glob->swap_lru, swap) {
  1523. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  1524. if (!ret)
  1525. break;
  1526. }
  1527. if (ret) {
  1528. spin_unlock(&glob->lru_lock);
  1529. return ret;
  1530. }
  1531. kref_get(&bo->list_kref);
  1532. if (!list_empty(&bo->ddestroy)) {
  1533. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1534. kref_put(&bo->list_kref, ttm_bo_release_list);
  1535. return ret;
  1536. }
  1537. put_count = ttm_bo_del_from_lru(bo);
  1538. spin_unlock(&glob->lru_lock);
  1539. ttm_bo_list_ref_sub(bo, put_count, true);
  1540. /**
  1541. * Wait for GPU, then move to system cached.
  1542. */
  1543. spin_lock(&bo->bdev->fence_lock);
  1544. ret = ttm_bo_wait(bo, false, false, false);
  1545. spin_unlock(&bo->bdev->fence_lock);
  1546. if (unlikely(ret != 0))
  1547. goto out;
  1548. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1549. struct ttm_mem_reg evict_mem;
  1550. evict_mem = bo->mem;
  1551. evict_mem.mm_node = NULL;
  1552. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1553. evict_mem.mem_type = TTM_PL_SYSTEM;
  1554. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1555. false, false, false);
  1556. if (unlikely(ret != 0))
  1557. goto out;
  1558. }
  1559. ttm_bo_unmap_virtual(bo);
  1560. /**
  1561. * Swap out. Buffer will be swapped in again as soon as
  1562. * anyone tries to access a ttm page.
  1563. */
  1564. if (bo->bdev->driver->swap_notify)
  1565. bo->bdev->driver->swap_notify(bo);
  1566. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1567. out:
  1568. /**
  1569. *
  1570. * Unreserve without putting on LRU to avoid swapping out an
  1571. * already swapped buffer.
  1572. */
  1573. atomic_set(&bo->reserved, 0);
  1574. wake_up_all(&bo->event_queue);
  1575. kref_put(&bo->list_kref, ttm_bo_release_list);
  1576. return ret;
  1577. }
  1578. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1579. {
  1580. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1581. ;
  1582. }
  1583. EXPORT_SYMBOL(ttm_bo_swapout_all);