ttm_bo.c 46 KB

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