ttm_bo.c 46 KB

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