ttm_bo.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/mm.h>
  38. #include <linux/file.h>
  39. #include <linux/module.h>
  40. #include <linux/atomic.h>
  41. #define TTM_ASSERT_LOCKED(param)
  42. #define TTM_DEBUG(fmt, arg...)
  43. #define TTM_BO_HASH_ORDER 13
  44. static int ttm_bo_setup_vm(struct ttm_buffer_object *bo);
  45. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
  46. static void ttm_bo_global_kobj_release(struct kobject *kobj);
  47. static struct attribute ttm_bo_count = {
  48. .name = "bo_count",
  49. .mode = S_IRUGO
  50. };
  51. static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
  52. {
  53. int i;
  54. for (i = 0; i <= TTM_PL_PRIV5; i++)
  55. if (flags & (1 << i)) {
  56. *mem_type = i;
  57. return 0;
  58. }
  59. return -EINVAL;
  60. }
  61. static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
  62. {
  63. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  64. pr_err(" has_type: %d\n", man->has_type);
  65. pr_err(" use_type: %d\n", man->use_type);
  66. pr_err(" flags: 0x%08X\n", man->flags);
  67. pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
  68. pr_err(" size: %llu\n", man->size);
  69. pr_err(" available_caching: 0x%08X\n", man->available_caching);
  70. pr_err(" default_caching: 0x%08X\n", man->default_caching);
  71. if (mem_type != TTM_PL_SYSTEM)
  72. (*man->func->debug)(man, TTM_PFX);
  73. }
  74. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  75. struct ttm_placement *placement)
  76. {
  77. int i, ret, mem_type;
  78. pr_err("No space for %p (%lu pages, %luK, %luM)\n",
  79. bo, bo->mem.num_pages, bo->mem.size >> 10,
  80. bo->mem.size >> 20);
  81. for (i = 0; i < placement->num_placement; i++) {
  82. ret = ttm_mem_type_from_flags(placement->placement[i],
  83. &mem_type);
  84. if (ret)
  85. return;
  86. pr_err(" placement[%d]=0x%08X (%d)\n",
  87. i, placement->placement[i], mem_type);
  88. ttm_mem_type_debug(bo->bdev, mem_type);
  89. }
  90. }
  91. static ssize_t ttm_bo_global_show(struct kobject *kobj,
  92. struct attribute *attr,
  93. char *buffer)
  94. {
  95. struct ttm_bo_global *glob =
  96. container_of(kobj, struct ttm_bo_global, kobj);
  97. return snprintf(buffer, PAGE_SIZE, "%lu\n",
  98. (unsigned long) atomic_read(&glob->bo_count));
  99. }
  100. static struct attribute *ttm_bo_global_attrs[] = {
  101. &ttm_bo_count,
  102. NULL
  103. };
  104. static const struct sysfs_ops ttm_bo_global_ops = {
  105. .show = &ttm_bo_global_show
  106. };
  107. static struct kobj_type ttm_bo_glob_kobj_type = {
  108. .release = &ttm_bo_global_kobj_release,
  109. .sysfs_ops = &ttm_bo_global_ops,
  110. .default_attrs = ttm_bo_global_attrs
  111. };
  112. static inline uint32_t ttm_bo_type_flags(unsigned type)
  113. {
  114. return 1 << (type);
  115. }
  116. static void ttm_bo_release_list(struct kref *list_kref)
  117. {
  118. struct ttm_buffer_object *bo =
  119. container_of(list_kref, struct ttm_buffer_object, list_kref);
  120. struct ttm_bo_device *bdev = bo->bdev;
  121. size_t acc_size = bo->acc_size;
  122. BUG_ON(atomic_read(&bo->list_kref.refcount));
  123. BUG_ON(atomic_read(&bo->kref.refcount));
  124. BUG_ON(atomic_read(&bo->cpu_writers));
  125. BUG_ON(bo->sync_obj != NULL);
  126. BUG_ON(bo->mem.mm_node != NULL);
  127. BUG_ON(!list_empty(&bo->lru));
  128. BUG_ON(!list_empty(&bo->ddestroy));
  129. if (bo->ttm)
  130. ttm_tt_destroy(bo->ttm);
  131. atomic_dec(&bo->glob->bo_count);
  132. if (bo->destroy)
  133. bo->destroy(bo);
  134. else {
  135. kfree(bo);
  136. }
  137. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  138. }
  139. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
  140. {
  141. if (interruptible) {
  142. return wait_event_interruptible(bo->event_queue,
  143. 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. retry_reserve:
  492. spin_lock(&glob->lru_lock);
  493. if (unlikely(list_empty(&bo->ddestroy))) {
  494. spin_unlock(&glob->lru_lock);
  495. return 0;
  496. }
  497. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  498. if (unlikely(ret == -EBUSY)) {
  499. spin_unlock(&glob->lru_lock);
  500. if (likely(!no_wait_reserve))
  501. ret = ttm_bo_wait_unreserved(bo, interruptible);
  502. if (unlikely(ret != 0))
  503. return ret;
  504. goto retry_reserve;
  505. }
  506. BUG_ON(ret != 0);
  507. /**
  508. * We can re-check for sync object without taking
  509. * the bo::lock since setting the sync object requires
  510. * also bo::reserved. A busy object at this point may
  511. * be caused by another thread recently starting an accelerated
  512. * eviction.
  513. */
  514. if (unlikely(bo->sync_obj)) {
  515. atomic_set(&bo->reserved, 0);
  516. wake_up_all(&bo->event_queue);
  517. spin_unlock(&glob->lru_lock);
  518. goto retry;
  519. }
  520. put_count = ttm_bo_del_from_lru(bo);
  521. list_del_init(&bo->ddestroy);
  522. ++put_count;
  523. spin_unlock(&glob->lru_lock);
  524. ttm_bo_cleanup_memtype_use(bo);
  525. ttm_bo_list_ref_sub(bo, put_count, true);
  526. return 0;
  527. }
  528. /**
  529. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  530. * encountered buffers.
  531. */
  532. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  533. {
  534. struct ttm_bo_global *glob = bdev->glob;
  535. struct ttm_buffer_object *entry = NULL;
  536. int ret = 0;
  537. spin_lock(&glob->lru_lock);
  538. if (list_empty(&bdev->ddestroy))
  539. goto out_unlock;
  540. entry = list_first_entry(&bdev->ddestroy,
  541. struct ttm_buffer_object, ddestroy);
  542. kref_get(&entry->list_kref);
  543. for (;;) {
  544. struct ttm_buffer_object *nentry = NULL;
  545. if (entry->ddestroy.next != &bdev->ddestroy) {
  546. nentry = list_first_entry(&entry->ddestroy,
  547. struct ttm_buffer_object, ddestroy);
  548. kref_get(&nentry->list_kref);
  549. }
  550. spin_unlock(&glob->lru_lock);
  551. ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
  552. !remove_all);
  553. kref_put(&entry->list_kref, ttm_bo_release_list);
  554. entry = nentry;
  555. if (ret || !entry)
  556. goto out;
  557. spin_lock(&glob->lru_lock);
  558. if (list_empty(&entry->ddestroy))
  559. break;
  560. }
  561. out_unlock:
  562. spin_unlock(&glob->lru_lock);
  563. out:
  564. if (entry)
  565. kref_put(&entry->list_kref, ttm_bo_release_list);
  566. return ret;
  567. }
  568. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  569. {
  570. struct ttm_bo_device *bdev =
  571. container_of(work, struct ttm_bo_device, wq.work);
  572. if (ttm_bo_delayed_delete(bdev, false)) {
  573. schedule_delayed_work(&bdev->wq,
  574. ((HZ / 100) < 1) ? 1 : HZ / 100);
  575. }
  576. }
  577. static void ttm_bo_release(struct kref *kref)
  578. {
  579. struct ttm_buffer_object *bo =
  580. container_of(kref, struct ttm_buffer_object, kref);
  581. struct ttm_bo_device *bdev = bo->bdev;
  582. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  583. if (likely(bo->vm_node != NULL)) {
  584. rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
  585. drm_mm_put_block(bo->vm_node);
  586. bo->vm_node = NULL;
  587. }
  588. write_unlock(&bdev->vm_lock);
  589. ttm_mem_io_lock(man, false);
  590. ttm_mem_io_free_vm(bo);
  591. ttm_mem_io_unlock(man);
  592. ttm_bo_cleanup_refs_or_queue(bo);
  593. kref_put(&bo->list_kref, ttm_bo_release_list);
  594. write_lock(&bdev->vm_lock);
  595. }
  596. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  597. {
  598. struct ttm_buffer_object *bo = *p_bo;
  599. struct ttm_bo_device *bdev = bo->bdev;
  600. *p_bo = NULL;
  601. write_lock(&bdev->vm_lock);
  602. kref_put(&bo->kref, ttm_bo_release);
  603. write_unlock(&bdev->vm_lock);
  604. }
  605. EXPORT_SYMBOL(ttm_bo_unref);
  606. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  607. {
  608. return cancel_delayed_work_sync(&bdev->wq);
  609. }
  610. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  611. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  612. {
  613. if (resched)
  614. schedule_delayed_work(&bdev->wq,
  615. ((HZ / 100) < 1) ? 1 : HZ / 100);
  616. }
  617. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  618. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  619. bool no_wait_reserve, bool no_wait_gpu)
  620. {
  621. struct ttm_bo_device *bdev = bo->bdev;
  622. struct ttm_mem_reg evict_mem;
  623. struct ttm_placement placement;
  624. int ret = 0;
  625. spin_lock(&bdev->fence_lock);
  626. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  627. spin_unlock(&bdev->fence_lock);
  628. if (unlikely(ret != 0)) {
  629. if (ret != -ERESTARTSYS) {
  630. pr_err("Failed to expire sync object before buffer eviction\n");
  631. }
  632. goto out;
  633. }
  634. BUG_ON(!atomic_read(&bo->reserved));
  635. evict_mem = bo->mem;
  636. evict_mem.mm_node = NULL;
  637. evict_mem.bus.io_reserved_vm = false;
  638. evict_mem.bus.io_reserved_count = 0;
  639. placement.fpfn = 0;
  640. placement.lpfn = 0;
  641. placement.num_placement = 0;
  642. placement.num_busy_placement = 0;
  643. bdev->driver->evict_flags(bo, &placement);
  644. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  645. no_wait_reserve, no_wait_gpu);
  646. if (ret) {
  647. if (ret != -ERESTARTSYS) {
  648. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  649. bo);
  650. ttm_bo_mem_space_debug(bo, &placement);
  651. }
  652. goto out;
  653. }
  654. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  655. no_wait_reserve, no_wait_gpu);
  656. if (ret) {
  657. if (ret != -ERESTARTSYS)
  658. pr_err("Buffer eviction failed\n");
  659. ttm_bo_mem_put(bo, &evict_mem);
  660. goto out;
  661. }
  662. bo->evicted = true;
  663. out:
  664. return ret;
  665. }
  666. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  667. uint32_t mem_type,
  668. bool interruptible, bool no_wait_reserve,
  669. bool no_wait_gpu)
  670. {
  671. struct ttm_bo_global *glob = bdev->glob;
  672. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  673. struct ttm_buffer_object *bo;
  674. int ret, put_count = 0;
  675. retry:
  676. spin_lock(&glob->lru_lock);
  677. if (list_empty(&man->lru)) {
  678. spin_unlock(&glob->lru_lock);
  679. return -EBUSY;
  680. }
  681. bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
  682. kref_get(&bo->list_kref);
  683. if (!list_empty(&bo->ddestroy)) {
  684. spin_unlock(&glob->lru_lock);
  685. ret = ttm_bo_cleanup_refs(bo, interruptible,
  686. no_wait_reserve, no_wait_gpu);
  687. kref_put(&bo->list_kref, ttm_bo_release_list);
  688. return ret;
  689. }
  690. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  691. if (unlikely(ret == -EBUSY)) {
  692. spin_unlock(&glob->lru_lock);
  693. if (likely(!no_wait_reserve))
  694. ret = ttm_bo_wait_unreserved(bo, interruptible);
  695. kref_put(&bo->list_kref, ttm_bo_release_list);
  696. /**
  697. * We *need* to retry after releasing the lru lock.
  698. */
  699. if (unlikely(ret != 0))
  700. return ret;
  701. goto retry;
  702. }
  703. put_count = ttm_bo_del_from_lru(bo);
  704. spin_unlock(&glob->lru_lock);
  705. BUG_ON(ret != 0);
  706. ttm_bo_list_ref_sub(bo, put_count, true);
  707. ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
  708. ttm_bo_unreserve(bo);
  709. kref_put(&bo->list_kref, ttm_bo_release_list);
  710. return ret;
  711. }
  712. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  713. {
  714. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  715. if (mem->mm_node)
  716. (*man->func->put_node)(man, mem);
  717. }
  718. EXPORT_SYMBOL(ttm_bo_mem_put);
  719. /**
  720. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  721. * space, or we've evicted everything and there isn't enough space.
  722. */
  723. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  724. uint32_t mem_type,
  725. struct ttm_placement *placement,
  726. struct ttm_mem_reg *mem,
  727. bool interruptible,
  728. bool no_wait_reserve,
  729. bool no_wait_gpu)
  730. {
  731. struct ttm_bo_device *bdev = bo->bdev;
  732. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  733. int ret;
  734. do {
  735. ret = (*man->func->get_node)(man, bo, placement, mem);
  736. if (unlikely(ret != 0))
  737. return ret;
  738. if (mem->mm_node)
  739. break;
  740. ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
  741. no_wait_reserve, no_wait_gpu);
  742. if (unlikely(ret != 0))
  743. return ret;
  744. } while (1);
  745. if (mem->mm_node == NULL)
  746. return -ENOMEM;
  747. mem->mem_type = mem_type;
  748. return 0;
  749. }
  750. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  751. uint32_t cur_placement,
  752. uint32_t proposed_placement)
  753. {
  754. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  755. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  756. /**
  757. * Keep current caching if possible.
  758. */
  759. if ((cur_placement & caching) != 0)
  760. result |= (cur_placement & caching);
  761. else if ((man->default_caching & caching) != 0)
  762. result |= man->default_caching;
  763. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  764. result |= TTM_PL_FLAG_CACHED;
  765. else if ((TTM_PL_FLAG_WC & caching) != 0)
  766. result |= TTM_PL_FLAG_WC;
  767. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  768. result |= TTM_PL_FLAG_UNCACHED;
  769. return result;
  770. }
  771. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  772. uint32_t mem_type,
  773. uint32_t proposed_placement,
  774. uint32_t *masked_placement)
  775. {
  776. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  777. if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
  778. return false;
  779. if ((proposed_placement & man->available_caching) == 0)
  780. return false;
  781. cur_flags |= (proposed_placement & man->available_caching);
  782. *masked_placement = cur_flags;
  783. return true;
  784. }
  785. /**
  786. * Creates space for memory region @mem according to its type.
  787. *
  788. * This function first searches for free space in compatible memory types in
  789. * the priority order defined by the driver. If free space isn't found, then
  790. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  791. * space.
  792. */
  793. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  794. struct ttm_placement *placement,
  795. struct ttm_mem_reg *mem,
  796. bool interruptible, bool no_wait_reserve,
  797. bool no_wait_gpu)
  798. {
  799. struct ttm_bo_device *bdev = bo->bdev;
  800. struct ttm_mem_type_manager *man;
  801. uint32_t mem_type = TTM_PL_SYSTEM;
  802. uint32_t cur_flags = 0;
  803. bool type_found = false;
  804. bool type_ok = false;
  805. bool has_erestartsys = false;
  806. int i, ret;
  807. mem->mm_node = NULL;
  808. for (i = 0; i < placement->num_placement; ++i) {
  809. ret = ttm_mem_type_from_flags(placement->placement[i],
  810. &mem_type);
  811. if (ret)
  812. return ret;
  813. man = &bdev->man[mem_type];
  814. type_ok = ttm_bo_mt_compatible(man,
  815. mem_type,
  816. placement->placement[i],
  817. &cur_flags);
  818. if (!type_ok)
  819. continue;
  820. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  821. cur_flags);
  822. /*
  823. * Use the access and other non-mapping-related flag bits from
  824. * the memory placement flags to the current flags
  825. */
  826. ttm_flag_masked(&cur_flags, placement->placement[i],
  827. ~TTM_PL_MASK_MEMTYPE);
  828. if (mem_type == TTM_PL_SYSTEM)
  829. break;
  830. if (man->has_type && man->use_type) {
  831. type_found = true;
  832. ret = (*man->func->get_node)(man, bo, placement, mem);
  833. if (unlikely(ret))
  834. return ret;
  835. }
  836. if (mem->mm_node)
  837. break;
  838. }
  839. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  840. mem->mem_type = mem_type;
  841. mem->placement = cur_flags;
  842. return 0;
  843. }
  844. if (!type_found)
  845. return -EINVAL;
  846. for (i = 0; i < placement->num_busy_placement; ++i) {
  847. ret = ttm_mem_type_from_flags(placement->busy_placement[i],
  848. &mem_type);
  849. if (ret)
  850. return ret;
  851. man = &bdev->man[mem_type];
  852. if (!man->has_type)
  853. continue;
  854. if (!ttm_bo_mt_compatible(man,
  855. mem_type,
  856. placement->busy_placement[i],
  857. &cur_flags))
  858. continue;
  859. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  860. cur_flags);
  861. /*
  862. * Use the access and other non-mapping-related flag bits from
  863. * the memory placement flags to the current flags
  864. */
  865. ttm_flag_masked(&cur_flags, placement->busy_placement[i],
  866. ~TTM_PL_MASK_MEMTYPE);
  867. if (mem_type == TTM_PL_SYSTEM) {
  868. mem->mem_type = mem_type;
  869. mem->placement = cur_flags;
  870. mem->mm_node = NULL;
  871. return 0;
  872. }
  873. ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
  874. interruptible, no_wait_reserve, no_wait_gpu);
  875. if (ret == 0 && mem->mm_node) {
  876. mem->placement = cur_flags;
  877. return 0;
  878. }
  879. if (ret == -ERESTARTSYS)
  880. has_erestartsys = true;
  881. }
  882. ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  883. return ret;
  884. }
  885. EXPORT_SYMBOL(ttm_bo_mem_space);
  886. int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
  887. {
  888. if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
  889. return -EBUSY;
  890. return wait_event_interruptible(bo->event_queue,
  891. atomic_read(&bo->cpu_writers) == 0);
  892. }
  893. EXPORT_SYMBOL(ttm_bo_wait_cpu);
  894. int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  895. struct ttm_placement *placement,
  896. bool interruptible, bool no_wait_reserve,
  897. bool no_wait_gpu)
  898. {
  899. int ret = 0;
  900. struct ttm_mem_reg mem;
  901. struct ttm_bo_device *bdev = bo->bdev;
  902. BUG_ON(!atomic_read(&bo->reserved));
  903. /*
  904. * FIXME: It's possible to pipeline buffer moves.
  905. * Have the driver move function wait for idle when necessary,
  906. * instead of doing it here.
  907. */
  908. spin_lock(&bdev->fence_lock);
  909. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  910. spin_unlock(&bdev->fence_lock);
  911. if (ret)
  912. return ret;
  913. mem.num_pages = bo->num_pages;
  914. mem.size = mem.num_pages << PAGE_SHIFT;
  915. mem.page_alignment = bo->mem.page_alignment;
  916. mem.bus.io_reserved_vm = false;
  917. mem.bus.io_reserved_count = 0;
  918. /*
  919. * Determine where to move the buffer.
  920. */
  921. ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
  922. if (ret)
  923. goto out_unlock;
  924. ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
  925. out_unlock:
  926. if (ret && mem.mm_node)
  927. ttm_bo_mem_put(bo, &mem);
  928. return ret;
  929. }
  930. static int ttm_bo_mem_compat(struct ttm_placement *placement,
  931. struct ttm_mem_reg *mem)
  932. {
  933. int i;
  934. if (mem->mm_node && placement->lpfn != 0 &&
  935. (mem->start < placement->fpfn ||
  936. mem->start + mem->num_pages > placement->lpfn))
  937. return -1;
  938. for (i = 0; i < placement->num_placement; i++) {
  939. if ((placement->placement[i] & mem->placement &
  940. TTM_PL_MASK_CACHING) &&
  941. (placement->placement[i] & mem->placement &
  942. TTM_PL_MASK_MEM))
  943. return i;
  944. }
  945. return -1;
  946. }
  947. int ttm_bo_validate(struct ttm_buffer_object *bo,
  948. struct ttm_placement *placement,
  949. bool interruptible, bool no_wait_reserve,
  950. bool no_wait_gpu)
  951. {
  952. int ret;
  953. BUG_ON(!atomic_read(&bo->reserved));
  954. /* Check that range is valid */
  955. if (placement->lpfn || placement->fpfn)
  956. if (placement->fpfn > placement->lpfn ||
  957. (placement->lpfn - placement->fpfn) < bo->num_pages)
  958. return -EINVAL;
  959. /*
  960. * Check whether we need to move buffer.
  961. */
  962. ret = ttm_bo_mem_compat(placement, &bo->mem);
  963. if (ret < 0) {
  964. ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
  965. if (ret)
  966. return ret;
  967. } else {
  968. /*
  969. * Use the access and other non-mapping-related flag bits from
  970. * the compatible memory placement flags to the active flags
  971. */
  972. ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
  973. ~TTM_PL_MASK_MEMTYPE);
  974. }
  975. /*
  976. * We might need to add a TTM.
  977. */
  978. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  979. ret = ttm_bo_add_ttm(bo, true);
  980. if (ret)
  981. return ret;
  982. }
  983. return 0;
  984. }
  985. EXPORT_SYMBOL(ttm_bo_validate);
  986. int ttm_bo_check_placement(struct ttm_buffer_object *bo,
  987. struct ttm_placement *placement)
  988. {
  989. BUG_ON((placement->fpfn || placement->lpfn) &&
  990. (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
  991. return 0;
  992. }
  993. int ttm_bo_init(struct ttm_bo_device *bdev,
  994. struct ttm_buffer_object *bo,
  995. unsigned long size,
  996. enum ttm_bo_type type,
  997. struct ttm_placement *placement,
  998. uint32_t page_alignment,
  999. unsigned long buffer_start,
  1000. bool interruptible,
  1001. struct file *persistent_swap_storage,
  1002. size_t acc_size,
  1003. struct sg_table *sg,
  1004. void (*destroy) (struct ttm_buffer_object *))
  1005. {
  1006. int ret = 0;
  1007. unsigned long num_pages;
  1008. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  1009. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  1010. if (ret) {
  1011. pr_err("Out of kernel memory\n");
  1012. if (destroy)
  1013. (*destroy)(bo);
  1014. else
  1015. kfree(bo);
  1016. return -ENOMEM;
  1017. }
  1018. size += buffer_start & ~PAGE_MASK;
  1019. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1020. if (num_pages == 0) {
  1021. pr_err("Illegal buffer object size\n");
  1022. if (destroy)
  1023. (*destroy)(bo);
  1024. else
  1025. kfree(bo);
  1026. ttm_mem_global_free(mem_glob, acc_size);
  1027. return -EINVAL;
  1028. }
  1029. bo->destroy = destroy;
  1030. kref_init(&bo->kref);
  1031. kref_init(&bo->list_kref);
  1032. atomic_set(&bo->cpu_writers, 0);
  1033. atomic_set(&bo->reserved, 1);
  1034. init_waitqueue_head(&bo->event_queue);
  1035. INIT_LIST_HEAD(&bo->lru);
  1036. INIT_LIST_HEAD(&bo->ddestroy);
  1037. INIT_LIST_HEAD(&bo->swap);
  1038. INIT_LIST_HEAD(&bo->io_reserve_lru);
  1039. bo->bdev = bdev;
  1040. bo->glob = bdev->glob;
  1041. bo->type = type;
  1042. bo->num_pages = num_pages;
  1043. bo->mem.size = num_pages << PAGE_SHIFT;
  1044. bo->mem.mem_type = TTM_PL_SYSTEM;
  1045. bo->mem.num_pages = bo->num_pages;
  1046. bo->mem.mm_node = NULL;
  1047. bo->mem.page_alignment = page_alignment;
  1048. bo->mem.bus.io_reserved_vm = false;
  1049. bo->mem.bus.io_reserved_count = 0;
  1050. bo->buffer_start = buffer_start & PAGE_MASK;
  1051. bo->priv_flags = 0;
  1052. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1053. bo->seq_valid = false;
  1054. bo->persistent_swap_storage = persistent_swap_storage;
  1055. bo->acc_size = acc_size;
  1056. bo->sg = sg;
  1057. atomic_inc(&bo->glob->bo_count);
  1058. ret = ttm_bo_check_placement(bo, placement);
  1059. if (unlikely(ret != 0))
  1060. goto out_err;
  1061. /*
  1062. * For ttm_bo_type_device buffers, allocate
  1063. * address space from the device.
  1064. */
  1065. if (bo->type == ttm_bo_type_device ||
  1066. bo->type == ttm_bo_type_sg) {
  1067. ret = ttm_bo_setup_vm(bo);
  1068. if (ret)
  1069. goto out_err;
  1070. }
  1071. ret = ttm_bo_validate(bo, placement, interruptible, false, false);
  1072. if (ret)
  1073. goto out_err;
  1074. ttm_bo_unreserve(bo);
  1075. return 0;
  1076. out_err:
  1077. ttm_bo_unreserve(bo);
  1078. ttm_bo_unref(&bo);
  1079. return ret;
  1080. }
  1081. EXPORT_SYMBOL(ttm_bo_init);
  1082. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1083. unsigned long bo_size,
  1084. unsigned struct_size)
  1085. {
  1086. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1087. size_t size = 0;
  1088. size += ttm_round_pot(struct_size);
  1089. size += PAGE_ALIGN(npages * sizeof(void *));
  1090. size += ttm_round_pot(sizeof(struct ttm_tt));
  1091. return size;
  1092. }
  1093. EXPORT_SYMBOL(ttm_bo_acc_size);
  1094. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1095. unsigned long bo_size,
  1096. unsigned struct_size)
  1097. {
  1098. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1099. size_t size = 0;
  1100. size += ttm_round_pot(struct_size);
  1101. size += PAGE_ALIGN(npages * sizeof(void *));
  1102. size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
  1103. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1104. return size;
  1105. }
  1106. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1107. int ttm_bo_create(struct ttm_bo_device *bdev,
  1108. unsigned long size,
  1109. enum ttm_bo_type type,
  1110. struct ttm_placement *placement,
  1111. uint32_t page_alignment,
  1112. unsigned long buffer_start,
  1113. bool interruptible,
  1114. struct file *persistent_swap_storage,
  1115. struct ttm_buffer_object **p_bo)
  1116. {
  1117. struct ttm_buffer_object *bo;
  1118. size_t acc_size;
  1119. int ret;
  1120. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1121. if (unlikely(bo == NULL))
  1122. return -ENOMEM;
  1123. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1124. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1125. buffer_start, interruptible,
  1126. persistent_swap_storage, acc_size, NULL, NULL);
  1127. if (likely(ret == 0))
  1128. *p_bo = bo;
  1129. return ret;
  1130. }
  1131. EXPORT_SYMBOL(ttm_bo_create);
  1132. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1133. unsigned mem_type, bool allow_errors)
  1134. {
  1135. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1136. struct ttm_bo_global *glob = bdev->glob;
  1137. int ret;
  1138. /*
  1139. * Can't use standard list traversal since we're unlocking.
  1140. */
  1141. spin_lock(&glob->lru_lock);
  1142. while (!list_empty(&man->lru)) {
  1143. spin_unlock(&glob->lru_lock);
  1144. ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
  1145. if (ret) {
  1146. if (allow_errors) {
  1147. return ret;
  1148. } else {
  1149. pr_err("Cleanup eviction failed\n");
  1150. }
  1151. }
  1152. spin_lock(&glob->lru_lock);
  1153. }
  1154. spin_unlock(&glob->lru_lock);
  1155. return 0;
  1156. }
  1157. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1158. {
  1159. struct ttm_mem_type_manager *man;
  1160. int ret = -EINVAL;
  1161. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1162. pr_err("Illegal memory type %d\n", mem_type);
  1163. return ret;
  1164. }
  1165. man = &bdev->man[mem_type];
  1166. if (!man->has_type) {
  1167. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1168. mem_type);
  1169. return ret;
  1170. }
  1171. man->use_type = false;
  1172. man->has_type = false;
  1173. ret = 0;
  1174. if (mem_type > 0) {
  1175. ttm_bo_force_list_clean(bdev, mem_type, false);
  1176. ret = (*man->func->takedown)(man);
  1177. }
  1178. return ret;
  1179. }
  1180. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1181. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1182. {
  1183. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1184. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1185. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1186. return -EINVAL;
  1187. }
  1188. if (!man->has_type) {
  1189. pr_err("Memory type %u has not been initialized\n", mem_type);
  1190. return 0;
  1191. }
  1192. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1193. }
  1194. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1195. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1196. unsigned long p_size)
  1197. {
  1198. int ret = -EINVAL;
  1199. struct ttm_mem_type_manager *man;
  1200. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1201. man = &bdev->man[type];
  1202. BUG_ON(man->has_type);
  1203. man->io_reserve_fastpath = true;
  1204. man->use_io_reserve_lru = false;
  1205. mutex_init(&man->io_reserve_mutex);
  1206. INIT_LIST_HEAD(&man->io_reserve_lru);
  1207. ret = bdev->driver->init_mem_type(bdev, type, man);
  1208. if (ret)
  1209. return ret;
  1210. man->bdev = bdev;
  1211. ret = 0;
  1212. if (type != TTM_PL_SYSTEM) {
  1213. ret = (*man->func->init)(man, p_size);
  1214. if (ret)
  1215. return ret;
  1216. }
  1217. man->has_type = true;
  1218. man->use_type = true;
  1219. man->size = p_size;
  1220. INIT_LIST_HEAD(&man->lru);
  1221. return 0;
  1222. }
  1223. EXPORT_SYMBOL(ttm_bo_init_mm);
  1224. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1225. {
  1226. struct ttm_bo_global *glob =
  1227. container_of(kobj, struct ttm_bo_global, kobj);
  1228. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1229. __free_page(glob->dummy_read_page);
  1230. kfree(glob);
  1231. }
  1232. void ttm_bo_global_release(struct drm_global_reference *ref)
  1233. {
  1234. struct ttm_bo_global *glob = ref->object;
  1235. kobject_del(&glob->kobj);
  1236. kobject_put(&glob->kobj);
  1237. }
  1238. EXPORT_SYMBOL(ttm_bo_global_release);
  1239. int ttm_bo_global_init(struct drm_global_reference *ref)
  1240. {
  1241. struct ttm_bo_global_ref *bo_ref =
  1242. container_of(ref, struct ttm_bo_global_ref, ref);
  1243. struct ttm_bo_global *glob = ref->object;
  1244. int ret;
  1245. mutex_init(&glob->device_list_mutex);
  1246. spin_lock_init(&glob->lru_lock);
  1247. glob->mem_glob = bo_ref->mem_glob;
  1248. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1249. if (unlikely(glob->dummy_read_page == NULL)) {
  1250. ret = -ENOMEM;
  1251. goto out_no_drp;
  1252. }
  1253. INIT_LIST_HEAD(&glob->swap_lru);
  1254. INIT_LIST_HEAD(&glob->device_list);
  1255. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1256. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1257. if (unlikely(ret != 0)) {
  1258. pr_err("Could not register buffer object swapout\n");
  1259. goto out_no_shrink;
  1260. }
  1261. atomic_set(&glob->bo_count, 0);
  1262. ret = kobject_init_and_add(
  1263. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1264. if (unlikely(ret != 0))
  1265. kobject_put(&glob->kobj);
  1266. return ret;
  1267. out_no_shrink:
  1268. __free_page(glob->dummy_read_page);
  1269. out_no_drp:
  1270. kfree(glob);
  1271. return ret;
  1272. }
  1273. EXPORT_SYMBOL(ttm_bo_global_init);
  1274. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1275. {
  1276. int ret = 0;
  1277. unsigned i = TTM_NUM_MEM_TYPES;
  1278. struct ttm_mem_type_manager *man;
  1279. struct ttm_bo_global *glob = bdev->glob;
  1280. while (i--) {
  1281. man = &bdev->man[i];
  1282. if (man->has_type) {
  1283. man->use_type = false;
  1284. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1285. ret = -EBUSY;
  1286. pr_err("DRM memory manager type %d is not clean\n",
  1287. i);
  1288. }
  1289. man->has_type = false;
  1290. }
  1291. }
  1292. mutex_lock(&glob->device_list_mutex);
  1293. list_del(&bdev->device_list);
  1294. mutex_unlock(&glob->device_list_mutex);
  1295. cancel_delayed_work_sync(&bdev->wq);
  1296. while (ttm_bo_delayed_delete(bdev, true))
  1297. ;
  1298. spin_lock(&glob->lru_lock);
  1299. if (list_empty(&bdev->ddestroy))
  1300. TTM_DEBUG("Delayed destroy list was clean\n");
  1301. if (list_empty(&bdev->man[0].lru))
  1302. TTM_DEBUG("Swap list was clean\n");
  1303. spin_unlock(&glob->lru_lock);
  1304. BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
  1305. write_lock(&bdev->vm_lock);
  1306. drm_mm_takedown(&bdev->addr_space_mm);
  1307. write_unlock(&bdev->vm_lock);
  1308. return ret;
  1309. }
  1310. EXPORT_SYMBOL(ttm_bo_device_release);
  1311. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1312. struct ttm_bo_global *glob,
  1313. struct ttm_bo_driver *driver,
  1314. uint64_t file_page_offset,
  1315. bool need_dma32)
  1316. {
  1317. int ret = -EINVAL;
  1318. rwlock_init(&bdev->vm_lock);
  1319. bdev->driver = driver;
  1320. memset(bdev->man, 0, sizeof(bdev->man));
  1321. /*
  1322. * Initialize the system memory buffer type.
  1323. * Other types need to be driver / IOCTL initialized.
  1324. */
  1325. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1326. if (unlikely(ret != 0))
  1327. goto out_no_sys;
  1328. bdev->addr_space_rb = RB_ROOT;
  1329. ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
  1330. if (unlikely(ret != 0))
  1331. goto out_no_addr_mm;
  1332. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1333. bdev->nice_mode = true;
  1334. INIT_LIST_HEAD(&bdev->ddestroy);
  1335. bdev->dev_mapping = NULL;
  1336. bdev->glob = glob;
  1337. bdev->need_dma32 = need_dma32;
  1338. bdev->val_seq = 0;
  1339. spin_lock_init(&bdev->fence_lock);
  1340. mutex_lock(&glob->device_list_mutex);
  1341. list_add_tail(&bdev->device_list, &glob->device_list);
  1342. mutex_unlock(&glob->device_list_mutex);
  1343. return 0;
  1344. out_no_addr_mm:
  1345. ttm_bo_clean_mm(bdev, 0);
  1346. out_no_sys:
  1347. return ret;
  1348. }
  1349. EXPORT_SYMBOL(ttm_bo_device_init);
  1350. /*
  1351. * buffer object vm functions.
  1352. */
  1353. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1354. {
  1355. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1356. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1357. if (mem->mem_type == TTM_PL_SYSTEM)
  1358. return false;
  1359. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1360. return false;
  1361. if (mem->placement & TTM_PL_FLAG_CACHED)
  1362. return false;
  1363. }
  1364. return true;
  1365. }
  1366. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1367. {
  1368. struct ttm_bo_device *bdev = bo->bdev;
  1369. loff_t offset = (loff_t) bo->addr_space_offset;
  1370. loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
  1371. if (!bdev->dev_mapping)
  1372. return;
  1373. unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
  1374. ttm_mem_io_free_vm(bo);
  1375. }
  1376. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1377. {
  1378. struct ttm_bo_device *bdev = bo->bdev;
  1379. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1380. ttm_mem_io_lock(man, false);
  1381. ttm_bo_unmap_virtual_locked(bo);
  1382. ttm_mem_io_unlock(man);
  1383. }
  1384. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1385. static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
  1386. {
  1387. struct ttm_bo_device *bdev = bo->bdev;
  1388. struct rb_node **cur = &bdev->addr_space_rb.rb_node;
  1389. struct rb_node *parent = NULL;
  1390. struct ttm_buffer_object *cur_bo;
  1391. unsigned long offset = bo->vm_node->start;
  1392. unsigned long cur_offset;
  1393. while (*cur) {
  1394. parent = *cur;
  1395. cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
  1396. cur_offset = cur_bo->vm_node->start;
  1397. if (offset < cur_offset)
  1398. cur = &parent->rb_left;
  1399. else if (offset > cur_offset)
  1400. cur = &parent->rb_right;
  1401. else
  1402. BUG();
  1403. }
  1404. rb_link_node(&bo->vm_rb, parent, cur);
  1405. rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
  1406. }
  1407. /**
  1408. * ttm_bo_setup_vm:
  1409. *
  1410. * @bo: the buffer to allocate address space for
  1411. *
  1412. * Allocate address space in the drm device so that applications
  1413. * can mmap the buffer and access the contents. This only
  1414. * applies to ttm_bo_type_device objects as others are not
  1415. * placed in the drm device address space.
  1416. */
  1417. static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
  1418. {
  1419. struct ttm_bo_device *bdev = bo->bdev;
  1420. int ret;
  1421. retry_pre_get:
  1422. ret = drm_mm_pre_get(&bdev->addr_space_mm);
  1423. if (unlikely(ret != 0))
  1424. return ret;
  1425. write_lock(&bdev->vm_lock);
  1426. bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
  1427. bo->mem.num_pages, 0, 0);
  1428. if (unlikely(bo->vm_node == NULL)) {
  1429. ret = -ENOMEM;
  1430. goto out_unlock;
  1431. }
  1432. bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
  1433. bo->mem.num_pages, 0);
  1434. if (unlikely(bo->vm_node == NULL)) {
  1435. write_unlock(&bdev->vm_lock);
  1436. goto retry_pre_get;
  1437. }
  1438. ttm_bo_vm_insert_rb(bo);
  1439. write_unlock(&bdev->vm_lock);
  1440. bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
  1441. return 0;
  1442. out_unlock:
  1443. write_unlock(&bdev->vm_lock);
  1444. return ret;
  1445. }
  1446. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1447. bool lazy, bool interruptible, bool no_wait)
  1448. {
  1449. struct ttm_bo_driver *driver = bo->bdev->driver;
  1450. struct ttm_bo_device *bdev = bo->bdev;
  1451. void *sync_obj;
  1452. void *sync_obj_arg;
  1453. int ret = 0;
  1454. if (likely(bo->sync_obj == NULL))
  1455. return 0;
  1456. while (bo->sync_obj) {
  1457. if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
  1458. void *tmp_obj = bo->sync_obj;
  1459. bo->sync_obj = NULL;
  1460. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1461. spin_unlock(&bdev->fence_lock);
  1462. driver->sync_obj_unref(&tmp_obj);
  1463. spin_lock(&bdev->fence_lock);
  1464. continue;
  1465. }
  1466. if (no_wait)
  1467. return -EBUSY;
  1468. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  1469. sync_obj_arg = bo->sync_obj_arg;
  1470. spin_unlock(&bdev->fence_lock);
  1471. ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
  1472. lazy, interruptible);
  1473. if (unlikely(ret != 0)) {
  1474. driver->sync_obj_unref(&sync_obj);
  1475. spin_lock(&bdev->fence_lock);
  1476. return ret;
  1477. }
  1478. spin_lock(&bdev->fence_lock);
  1479. if (likely(bo->sync_obj == sync_obj &&
  1480. bo->sync_obj_arg == sync_obj_arg)) {
  1481. void *tmp_obj = bo->sync_obj;
  1482. bo->sync_obj = NULL;
  1483. clear_bit(TTM_BO_PRIV_FLAG_MOVING,
  1484. &bo->priv_flags);
  1485. spin_unlock(&bdev->fence_lock);
  1486. driver->sync_obj_unref(&sync_obj);
  1487. driver->sync_obj_unref(&tmp_obj);
  1488. spin_lock(&bdev->fence_lock);
  1489. } else {
  1490. spin_unlock(&bdev->fence_lock);
  1491. driver->sync_obj_unref(&sync_obj);
  1492. spin_lock(&bdev->fence_lock);
  1493. }
  1494. }
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL(ttm_bo_wait);
  1498. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1499. {
  1500. struct ttm_bo_device *bdev = bo->bdev;
  1501. int ret = 0;
  1502. /*
  1503. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1504. */
  1505. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  1506. if (unlikely(ret != 0))
  1507. return ret;
  1508. spin_lock(&bdev->fence_lock);
  1509. ret = ttm_bo_wait(bo, false, true, no_wait);
  1510. spin_unlock(&bdev->fence_lock);
  1511. if (likely(ret == 0))
  1512. atomic_inc(&bo->cpu_writers);
  1513. ttm_bo_unreserve(bo);
  1514. return ret;
  1515. }
  1516. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1517. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1518. {
  1519. if (atomic_dec_and_test(&bo->cpu_writers))
  1520. wake_up_all(&bo->event_queue);
  1521. }
  1522. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1523. /**
  1524. * A buffer object shrink method that tries to swap out the first
  1525. * buffer object on the bo_global::swap_lru list.
  1526. */
  1527. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1528. {
  1529. struct ttm_bo_global *glob =
  1530. container_of(shrink, struct ttm_bo_global, shrink);
  1531. struct ttm_buffer_object *bo;
  1532. int ret = -EBUSY;
  1533. int put_count;
  1534. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1535. spin_lock(&glob->lru_lock);
  1536. while (ret == -EBUSY) {
  1537. if (unlikely(list_empty(&glob->swap_lru))) {
  1538. spin_unlock(&glob->lru_lock);
  1539. return -EBUSY;
  1540. }
  1541. bo = list_first_entry(&glob->swap_lru,
  1542. struct ttm_buffer_object, swap);
  1543. kref_get(&bo->list_kref);
  1544. if (!list_empty(&bo->ddestroy)) {
  1545. spin_unlock(&glob->lru_lock);
  1546. (void) ttm_bo_cleanup_refs(bo, false, false, false);
  1547. kref_put(&bo->list_kref, ttm_bo_release_list);
  1548. spin_lock(&glob->lru_lock);
  1549. continue;
  1550. }
  1551. /**
  1552. * Reserve buffer. Since we unlock while sleeping, we need
  1553. * to re-check that nobody removed us from the swap-list while
  1554. * we slept.
  1555. */
  1556. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  1557. if (unlikely(ret == -EBUSY)) {
  1558. spin_unlock(&glob->lru_lock);
  1559. ttm_bo_wait_unreserved(bo, false);
  1560. kref_put(&bo->list_kref, ttm_bo_release_list);
  1561. spin_lock(&glob->lru_lock);
  1562. }
  1563. }
  1564. BUG_ON(ret != 0);
  1565. put_count = ttm_bo_del_from_lru(bo);
  1566. spin_unlock(&glob->lru_lock);
  1567. ttm_bo_list_ref_sub(bo, put_count, true);
  1568. /**
  1569. * Wait for GPU, then move to system cached.
  1570. */
  1571. spin_lock(&bo->bdev->fence_lock);
  1572. ret = ttm_bo_wait(bo, false, false, false);
  1573. spin_unlock(&bo->bdev->fence_lock);
  1574. if (unlikely(ret != 0))
  1575. goto out;
  1576. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1577. struct ttm_mem_reg evict_mem;
  1578. evict_mem = bo->mem;
  1579. evict_mem.mm_node = NULL;
  1580. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1581. evict_mem.mem_type = TTM_PL_SYSTEM;
  1582. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1583. false, false, false);
  1584. if (unlikely(ret != 0))
  1585. goto out;
  1586. }
  1587. ttm_bo_unmap_virtual(bo);
  1588. /**
  1589. * Swap out. Buffer will be swapped in again as soon as
  1590. * anyone tries to access a ttm page.
  1591. */
  1592. if (bo->bdev->driver->swap_notify)
  1593. bo->bdev->driver->swap_notify(bo);
  1594. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1595. out:
  1596. /**
  1597. *
  1598. * Unreserve without putting on LRU to avoid swapping out an
  1599. * already swapped buffer.
  1600. */
  1601. atomic_set(&bo->reserved, 0);
  1602. wake_up_all(&bo->event_queue);
  1603. kref_put(&bo->list_kref, ttm_bo_release_list);
  1604. return ret;
  1605. }
  1606. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1607. {
  1608. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1609. ;
  1610. }
  1611. EXPORT_SYMBOL(ttm_bo_swapout_all);