ttm_bo.c 46 KB

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