ttm_bo.c 45 KB

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