ttm_bo.c 46 KB

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