ttm_bo.c 46 KB

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