ttm_bo.c 46 KB

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