ttm_bo.c 45 KB

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