ttm_bo.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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. if (unlikely(list_empty(&bo->ddestroy))) {
  488. spin_unlock(&glob->lru_lock);
  489. return 0;
  490. }
  491. ret = ttm_bo_reserve_locked(bo, interruptible,
  492. no_wait_reserve, false, 0);
  493. if (unlikely(ret != 0)) {
  494. spin_unlock(&glob->lru_lock);
  495. return ret;
  496. }
  497. /**
  498. * We can re-check for sync object without taking
  499. * the bo::lock since setting the sync object requires
  500. * also bo::reserved. A busy object at this point may
  501. * be caused by another thread recently starting an accelerated
  502. * eviction.
  503. */
  504. if (unlikely(bo->sync_obj)) {
  505. atomic_set(&bo->reserved, 0);
  506. wake_up_all(&bo->event_queue);
  507. spin_unlock(&glob->lru_lock);
  508. goto retry;
  509. }
  510. put_count = ttm_bo_del_from_lru(bo);
  511. list_del_init(&bo->ddestroy);
  512. ++put_count;
  513. spin_unlock(&glob->lru_lock);
  514. ttm_bo_cleanup_memtype_use(bo);
  515. ttm_bo_list_ref_sub(bo, put_count, true);
  516. return 0;
  517. }
  518. /**
  519. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  520. * encountered buffers.
  521. */
  522. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  523. {
  524. struct ttm_bo_global *glob = bdev->glob;
  525. struct ttm_buffer_object *entry = NULL;
  526. int ret = 0;
  527. spin_lock(&glob->lru_lock);
  528. if (list_empty(&bdev->ddestroy))
  529. goto out_unlock;
  530. entry = list_first_entry(&bdev->ddestroy,
  531. struct ttm_buffer_object, ddestroy);
  532. kref_get(&entry->list_kref);
  533. for (;;) {
  534. struct ttm_buffer_object *nentry = NULL;
  535. if (entry->ddestroy.next != &bdev->ddestroy) {
  536. nentry = list_first_entry(&entry->ddestroy,
  537. struct ttm_buffer_object, ddestroy);
  538. kref_get(&nentry->list_kref);
  539. }
  540. spin_unlock(&glob->lru_lock);
  541. ret = ttm_bo_cleanup_refs(entry, false, !remove_all,
  542. !remove_all);
  543. kref_put(&entry->list_kref, ttm_bo_release_list);
  544. entry = nentry;
  545. if (ret || !entry)
  546. goto out;
  547. spin_lock(&glob->lru_lock);
  548. if (list_empty(&entry->ddestroy))
  549. break;
  550. }
  551. out_unlock:
  552. spin_unlock(&glob->lru_lock);
  553. out:
  554. if (entry)
  555. kref_put(&entry->list_kref, ttm_bo_release_list);
  556. return ret;
  557. }
  558. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  559. {
  560. struct ttm_bo_device *bdev =
  561. container_of(work, struct ttm_bo_device, wq.work);
  562. if (ttm_bo_delayed_delete(bdev, false)) {
  563. schedule_delayed_work(&bdev->wq,
  564. ((HZ / 100) < 1) ? 1 : HZ / 100);
  565. }
  566. }
  567. static void ttm_bo_release(struct kref *kref)
  568. {
  569. struct ttm_buffer_object *bo =
  570. container_of(kref, struct ttm_buffer_object, kref);
  571. struct ttm_bo_device *bdev = bo->bdev;
  572. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  573. if (likely(bo->vm_node != NULL)) {
  574. rb_erase(&bo->vm_rb, &bdev->addr_space_rb);
  575. drm_mm_put_block(bo->vm_node);
  576. bo->vm_node = NULL;
  577. }
  578. write_unlock(&bdev->vm_lock);
  579. ttm_mem_io_lock(man, false);
  580. ttm_mem_io_free_vm(bo);
  581. ttm_mem_io_unlock(man);
  582. ttm_bo_cleanup_refs_or_queue(bo);
  583. kref_put(&bo->list_kref, ttm_bo_release_list);
  584. write_lock(&bdev->vm_lock);
  585. }
  586. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  587. {
  588. struct ttm_buffer_object *bo = *p_bo;
  589. struct ttm_bo_device *bdev = bo->bdev;
  590. *p_bo = NULL;
  591. write_lock(&bdev->vm_lock);
  592. kref_put(&bo->kref, ttm_bo_release);
  593. write_unlock(&bdev->vm_lock);
  594. }
  595. EXPORT_SYMBOL(ttm_bo_unref);
  596. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  597. {
  598. return cancel_delayed_work_sync(&bdev->wq);
  599. }
  600. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  601. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  602. {
  603. if (resched)
  604. schedule_delayed_work(&bdev->wq,
  605. ((HZ / 100) < 1) ? 1 : HZ / 100);
  606. }
  607. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  608. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  609. bool no_wait_reserve, bool no_wait_gpu)
  610. {
  611. struct ttm_bo_device *bdev = bo->bdev;
  612. struct ttm_mem_reg evict_mem;
  613. struct ttm_placement placement;
  614. int ret = 0;
  615. spin_lock(&bdev->fence_lock);
  616. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  617. spin_unlock(&bdev->fence_lock);
  618. if (unlikely(ret != 0)) {
  619. if (ret != -ERESTARTSYS) {
  620. printk(KERN_ERR TTM_PFX
  621. "Failed to expire sync object before "
  622. "buffer eviction.\n");
  623. }
  624. goto out;
  625. }
  626. BUG_ON(!atomic_read(&bo->reserved));
  627. evict_mem = bo->mem;
  628. evict_mem.mm_node = NULL;
  629. evict_mem.bus.io_reserved_vm = false;
  630. evict_mem.bus.io_reserved_count = 0;
  631. placement.fpfn = 0;
  632. placement.lpfn = 0;
  633. placement.num_placement = 0;
  634. placement.num_busy_placement = 0;
  635. bdev->driver->evict_flags(bo, &placement);
  636. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  637. no_wait_reserve, no_wait_gpu);
  638. if (ret) {
  639. if (ret != -ERESTARTSYS) {
  640. printk(KERN_ERR TTM_PFX
  641. "Failed to find memory space for "
  642. "buffer 0x%p eviction.\n", bo);
  643. ttm_bo_mem_space_debug(bo, &placement);
  644. }
  645. goto out;
  646. }
  647. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  648. no_wait_reserve, no_wait_gpu);
  649. if (ret) {
  650. if (ret != -ERESTARTSYS)
  651. printk(KERN_ERR TTM_PFX "Buffer eviction failed\n");
  652. ttm_bo_mem_put(bo, &evict_mem);
  653. goto out;
  654. }
  655. bo->evicted = true;
  656. out:
  657. return ret;
  658. }
  659. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  660. uint32_t mem_type,
  661. bool interruptible, bool no_wait_reserve,
  662. bool no_wait_gpu)
  663. {
  664. struct ttm_bo_global *glob = bdev->glob;
  665. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  666. struct ttm_buffer_object *bo;
  667. int ret, put_count = 0;
  668. retry:
  669. spin_lock(&glob->lru_lock);
  670. if (list_empty(&man->lru)) {
  671. spin_unlock(&glob->lru_lock);
  672. return -EBUSY;
  673. }
  674. bo = list_first_entry(&man->lru, struct ttm_buffer_object, lru);
  675. kref_get(&bo->list_kref);
  676. if (!list_empty(&bo->ddestroy)) {
  677. spin_unlock(&glob->lru_lock);
  678. ret = ttm_bo_cleanup_refs(bo, interruptible,
  679. no_wait_reserve, no_wait_gpu);
  680. kref_put(&bo->list_kref, ttm_bo_release_list);
  681. if (likely(ret == 0 || ret == -ERESTARTSYS))
  682. return ret;
  683. goto retry;
  684. }
  685. ret = ttm_bo_reserve_locked(bo, false, no_wait_reserve, false, 0);
  686. if (unlikely(ret == -EBUSY)) {
  687. spin_unlock(&glob->lru_lock);
  688. if (likely(!no_wait_gpu))
  689. ret = ttm_bo_wait_unreserved(bo, interruptible);
  690. kref_put(&bo->list_kref, ttm_bo_release_list);
  691. /**
  692. * We *need* to retry after releasing the lru lock.
  693. */
  694. if (unlikely(ret != 0))
  695. return ret;
  696. goto retry;
  697. }
  698. put_count = ttm_bo_del_from_lru(bo);
  699. spin_unlock(&glob->lru_lock);
  700. BUG_ON(ret != 0);
  701. ttm_bo_list_ref_sub(bo, put_count, true);
  702. ret = ttm_bo_evict(bo, interruptible, no_wait_reserve, no_wait_gpu);
  703. ttm_bo_unreserve(bo);
  704. kref_put(&bo->list_kref, ttm_bo_release_list);
  705. return ret;
  706. }
  707. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  708. {
  709. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  710. if (mem->mm_node)
  711. (*man->func->put_node)(man, mem);
  712. }
  713. EXPORT_SYMBOL(ttm_bo_mem_put);
  714. /**
  715. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  716. * space, or we've evicted everything and there isn't enough space.
  717. */
  718. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  719. uint32_t mem_type,
  720. struct ttm_placement *placement,
  721. struct ttm_mem_reg *mem,
  722. bool interruptible,
  723. bool no_wait_reserve,
  724. bool no_wait_gpu)
  725. {
  726. struct ttm_bo_device *bdev = bo->bdev;
  727. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  728. int ret;
  729. do {
  730. ret = (*man->func->get_node)(man, bo, placement, mem);
  731. if (unlikely(ret != 0))
  732. return ret;
  733. if (mem->mm_node)
  734. break;
  735. ret = ttm_mem_evict_first(bdev, mem_type, interruptible,
  736. no_wait_reserve, no_wait_gpu);
  737. if (unlikely(ret != 0))
  738. return ret;
  739. } while (1);
  740. if (mem->mm_node == NULL)
  741. return -ENOMEM;
  742. mem->mem_type = mem_type;
  743. return 0;
  744. }
  745. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  746. uint32_t cur_placement,
  747. uint32_t proposed_placement)
  748. {
  749. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  750. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  751. /**
  752. * Keep current caching if possible.
  753. */
  754. if ((cur_placement & caching) != 0)
  755. result |= (cur_placement & caching);
  756. else if ((man->default_caching & caching) != 0)
  757. result |= man->default_caching;
  758. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  759. result |= TTM_PL_FLAG_CACHED;
  760. else if ((TTM_PL_FLAG_WC & caching) != 0)
  761. result |= TTM_PL_FLAG_WC;
  762. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  763. result |= TTM_PL_FLAG_UNCACHED;
  764. return result;
  765. }
  766. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  767. bool disallow_fixed,
  768. uint32_t mem_type,
  769. uint32_t proposed_placement,
  770. uint32_t *masked_placement)
  771. {
  772. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  773. if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
  774. return false;
  775. if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
  776. return false;
  777. if ((proposed_placement & man->available_caching) == 0)
  778. return false;
  779. cur_flags |= (proposed_placement & man->available_caching);
  780. *masked_placement = cur_flags;
  781. return true;
  782. }
  783. /**
  784. * Creates space for memory region @mem according to its type.
  785. *
  786. * This function first searches for free space in compatible memory types in
  787. * the priority order defined by the driver. If free space isn't found, then
  788. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  789. * space.
  790. */
  791. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  792. struct ttm_placement *placement,
  793. struct ttm_mem_reg *mem,
  794. bool interruptible, bool no_wait_reserve,
  795. bool no_wait_gpu)
  796. {
  797. struct ttm_bo_device *bdev = bo->bdev;
  798. struct ttm_mem_type_manager *man;
  799. uint32_t mem_type = TTM_PL_SYSTEM;
  800. uint32_t cur_flags = 0;
  801. bool type_found = false;
  802. bool type_ok = false;
  803. bool has_erestartsys = false;
  804. int i, ret;
  805. mem->mm_node = NULL;
  806. for (i = 0; i < placement->num_placement; ++i) {
  807. ret = ttm_mem_type_from_flags(placement->placement[i],
  808. &mem_type);
  809. if (ret)
  810. return ret;
  811. man = &bdev->man[mem_type];
  812. type_ok = ttm_bo_mt_compatible(man,
  813. bo->type == ttm_bo_type_user,
  814. mem_type,
  815. placement->placement[i],
  816. &cur_flags);
  817. if (!type_ok)
  818. continue;
  819. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  820. cur_flags);
  821. /*
  822. * Use the access and other non-mapping-related flag bits from
  823. * the memory placement flags to the current flags
  824. */
  825. ttm_flag_masked(&cur_flags, placement->placement[i],
  826. ~TTM_PL_MASK_MEMTYPE);
  827. if (mem_type == TTM_PL_SYSTEM)
  828. break;
  829. if (man->has_type && man->use_type) {
  830. type_found = true;
  831. ret = (*man->func->get_node)(man, bo, placement, mem);
  832. if (unlikely(ret))
  833. return ret;
  834. }
  835. if (mem->mm_node)
  836. break;
  837. }
  838. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  839. mem->mem_type = mem_type;
  840. mem->placement = cur_flags;
  841. return 0;
  842. }
  843. if (!type_found)
  844. return -EINVAL;
  845. for (i = 0; i < placement->num_busy_placement; ++i) {
  846. ret = ttm_mem_type_from_flags(placement->busy_placement[i],
  847. &mem_type);
  848. if (ret)
  849. return ret;
  850. man = &bdev->man[mem_type];
  851. if (!man->has_type)
  852. continue;
  853. if (!ttm_bo_mt_compatible(man,
  854. bo->type == ttm_bo_type_user,
  855. mem_type,
  856. placement->busy_placement[i],
  857. &cur_flags))
  858. continue;
  859. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  860. cur_flags);
  861. /*
  862. * Use the access and other non-mapping-related flag bits from
  863. * the memory placement flags to the current flags
  864. */
  865. ttm_flag_masked(&cur_flags, placement->busy_placement[i],
  866. ~TTM_PL_MASK_MEMTYPE);
  867. if (mem_type == TTM_PL_SYSTEM) {
  868. mem->mem_type = mem_type;
  869. mem->placement = cur_flags;
  870. mem->mm_node = NULL;
  871. return 0;
  872. }
  873. ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
  874. interruptible, no_wait_reserve, no_wait_gpu);
  875. if (ret == 0 && mem->mm_node) {
  876. mem->placement = cur_flags;
  877. return 0;
  878. }
  879. if (ret == -ERESTARTSYS)
  880. has_erestartsys = true;
  881. }
  882. ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  883. return ret;
  884. }
  885. EXPORT_SYMBOL(ttm_bo_mem_space);
  886. int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
  887. {
  888. if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
  889. return -EBUSY;
  890. return wait_event_interruptible(bo->event_queue,
  891. atomic_read(&bo->cpu_writers) == 0);
  892. }
  893. EXPORT_SYMBOL(ttm_bo_wait_cpu);
  894. int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  895. struct ttm_placement *placement,
  896. bool interruptible, bool no_wait_reserve,
  897. bool no_wait_gpu)
  898. {
  899. int ret = 0;
  900. struct ttm_mem_reg mem;
  901. struct ttm_bo_device *bdev = bo->bdev;
  902. BUG_ON(!atomic_read(&bo->reserved));
  903. /*
  904. * FIXME: It's possible to pipeline buffer moves.
  905. * Have the driver move function wait for idle when necessary,
  906. * instead of doing it here.
  907. */
  908. spin_lock(&bdev->fence_lock);
  909. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  910. spin_unlock(&bdev->fence_lock);
  911. if (ret)
  912. return ret;
  913. mem.num_pages = bo->num_pages;
  914. mem.size = mem.num_pages << PAGE_SHIFT;
  915. mem.page_alignment = bo->mem.page_alignment;
  916. mem.bus.io_reserved_vm = false;
  917. mem.bus.io_reserved_count = 0;
  918. /*
  919. * Determine where to move the buffer.
  920. */
  921. ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
  922. if (ret)
  923. goto out_unlock;
  924. ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
  925. out_unlock:
  926. if (ret && mem.mm_node)
  927. ttm_bo_mem_put(bo, &mem);
  928. return ret;
  929. }
  930. static int ttm_bo_mem_compat(struct ttm_placement *placement,
  931. struct ttm_mem_reg *mem)
  932. {
  933. int i;
  934. if (mem->mm_node && placement->lpfn != 0 &&
  935. (mem->start < placement->fpfn ||
  936. mem->start + mem->num_pages > placement->lpfn))
  937. return -1;
  938. for (i = 0; i < placement->num_placement; i++) {
  939. if ((placement->placement[i] & mem->placement &
  940. TTM_PL_MASK_CACHING) &&
  941. (placement->placement[i] & mem->placement &
  942. TTM_PL_MASK_MEM))
  943. return i;
  944. }
  945. return -1;
  946. }
  947. int ttm_bo_validate(struct ttm_buffer_object *bo,
  948. struct ttm_placement *placement,
  949. bool interruptible, bool no_wait_reserve,
  950. bool no_wait_gpu)
  951. {
  952. int ret;
  953. BUG_ON(!atomic_read(&bo->reserved));
  954. /* Check that range is valid */
  955. if (placement->lpfn || placement->fpfn)
  956. if (placement->fpfn > placement->lpfn ||
  957. (placement->lpfn - placement->fpfn) < bo->num_pages)
  958. return -EINVAL;
  959. /*
  960. * Check whether we need to move buffer.
  961. */
  962. ret = ttm_bo_mem_compat(placement, &bo->mem);
  963. if (ret < 0) {
  964. ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
  965. if (ret)
  966. return ret;
  967. } else {
  968. /*
  969. * Use the access and other non-mapping-related flag bits from
  970. * the compatible memory placement flags to the active flags
  971. */
  972. ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
  973. ~TTM_PL_MASK_MEMTYPE);
  974. }
  975. /*
  976. * We might need to add a TTM.
  977. */
  978. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  979. ret = ttm_bo_add_ttm(bo, true);
  980. if (ret)
  981. return ret;
  982. }
  983. return 0;
  984. }
  985. EXPORT_SYMBOL(ttm_bo_validate);
  986. int ttm_bo_check_placement(struct ttm_buffer_object *bo,
  987. struct ttm_placement *placement)
  988. {
  989. BUG_ON((placement->fpfn || placement->lpfn) &&
  990. (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
  991. return 0;
  992. }
  993. int ttm_bo_init(struct ttm_bo_device *bdev,
  994. struct ttm_buffer_object *bo,
  995. unsigned long size,
  996. enum ttm_bo_type type,
  997. struct ttm_placement *placement,
  998. uint32_t page_alignment,
  999. unsigned long buffer_start,
  1000. bool interruptible,
  1001. struct file *persistent_swap_storage,
  1002. size_t acc_size,
  1003. void (*destroy) (struct ttm_buffer_object *))
  1004. {
  1005. int ret = 0;
  1006. unsigned long num_pages;
  1007. size += buffer_start & ~PAGE_MASK;
  1008. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1009. if (num_pages == 0) {
  1010. printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
  1011. if (destroy)
  1012. (*destroy)(bo);
  1013. else
  1014. kfree(bo);
  1015. return -EINVAL;
  1016. }
  1017. bo->destroy = destroy;
  1018. kref_init(&bo->kref);
  1019. kref_init(&bo->list_kref);
  1020. atomic_set(&bo->cpu_writers, 0);
  1021. atomic_set(&bo->reserved, 1);
  1022. init_waitqueue_head(&bo->event_queue);
  1023. INIT_LIST_HEAD(&bo->lru);
  1024. INIT_LIST_HEAD(&bo->ddestroy);
  1025. INIT_LIST_HEAD(&bo->swap);
  1026. INIT_LIST_HEAD(&bo->io_reserve_lru);
  1027. bo->bdev = bdev;
  1028. bo->glob = bdev->glob;
  1029. bo->type = type;
  1030. bo->num_pages = num_pages;
  1031. bo->mem.size = num_pages << PAGE_SHIFT;
  1032. bo->mem.mem_type = TTM_PL_SYSTEM;
  1033. bo->mem.num_pages = bo->num_pages;
  1034. bo->mem.mm_node = NULL;
  1035. bo->mem.page_alignment = page_alignment;
  1036. bo->mem.bus.io_reserved_vm = false;
  1037. bo->mem.bus.io_reserved_count = 0;
  1038. bo->buffer_start = buffer_start & PAGE_MASK;
  1039. bo->priv_flags = 0;
  1040. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1041. bo->seq_valid = false;
  1042. bo->persistent_swap_storage = persistent_swap_storage;
  1043. bo->acc_size = acc_size;
  1044. atomic_inc(&bo->glob->bo_count);
  1045. ret = ttm_bo_check_placement(bo, placement);
  1046. if (unlikely(ret != 0))
  1047. goto out_err;
  1048. /*
  1049. * For ttm_bo_type_device buffers, allocate
  1050. * address space from the device.
  1051. */
  1052. if (bo->type == ttm_bo_type_device) {
  1053. ret = ttm_bo_setup_vm(bo);
  1054. if (ret)
  1055. goto out_err;
  1056. }
  1057. ret = ttm_bo_validate(bo, placement, interruptible, false, false);
  1058. if (ret)
  1059. goto out_err;
  1060. ttm_bo_unreserve(bo);
  1061. return 0;
  1062. out_err:
  1063. ttm_bo_unreserve(bo);
  1064. ttm_bo_unref(&bo);
  1065. return ret;
  1066. }
  1067. EXPORT_SYMBOL(ttm_bo_init);
  1068. static inline size_t ttm_bo_size(struct ttm_bo_global *glob,
  1069. unsigned long num_pages)
  1070. {
  1071. size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) &
  1072. PAGE_MASK;
  1073. return glob->ttm_bo_size + 2 * page_array_size;
  1074. }
  1075. int ttm_bo_create(struct ttm_bo_device *bdev,
  1076. unsigned long size,
  1077. enum ttm_bo_type type,
  1078. struct ttm_placement *placement,
  1079. uint32_t page_alignment,
  1080. unsigned long buffer_start,
  1081. bool interruptible,
  1082. struct file *persistent_swap_storage,
  1083. struct ttm_buffer_object **p_bo)
  1084. {
  1085. struct ttm_buffer_object *bo;
  1086. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  1087. int ret;
  1088. size_t acc_size =
  1089. ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
  1090. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  1091. if (unlikely(ret != 0))
  1092. return ret;
  1093. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1094. if (unlikely(bo == NULL)) {
  1095. ttm_mem_global_free(mem_glob, acc_size);
  1096. return -ENOMEM;
  1097. }
  1098. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1099. buffer_start, interruptible,
  1100. persistent_swap_storage, acc_size, NULL);
  1101. if (likely(ret == 0))
  1102. *p_bo = bo;
  1103. return ret;
  1104. }
  1105. EXPORT_SYMBOL(ttm_bo_create);
  1106. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1107. unsigned mem_type, bool allow_errors)
  1108. {
  1109. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1110. struct ttm_bo_global *glob = bdev->glob;
  1111. int ret;
  1112. /*
  1113. * Can't use standard list traversal since we're unlocking.
  1114. */
  1115. spin_lock(&glob->lru_lock);
  1116. while (!list_empty(&man->lru)) {
  1117. spin_unlock(&glob->lru_lock);
  1118. ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
  1119. if (ret) {
  1120. if (allow_errors) {
  1121. return ret;
  1122. } else {
  1123. printk(KERN_ERR TTM_PFX
  1124. "Cleanup eviction failed\n");
  1125. }
  1126. }
  1127. spin_lock(&glob->lru_lock);
  1128. }
  1129. spin_unlock(&glob->lru_lock);
  1130. return 0;
  1131. }
  1132. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1133. {
  1134. struct ttm_mem_type_manager *man;
  1135. int ret = -EINVAL;
  1136. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1137. printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
  1138. return ret;
  1139. }
  1140. man = &bdev->man[mem_type];
  1141. if (!man->has_type) {
  1142. printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
  1143. "memory manager type %u\n", mem_type);
  1144. return ret;
  1145. }
  1146. man->use_type = false;
  1147. man->has_type = false;
  1148. ret = 0;
  1149. if (mem_type > 0) {
  1150. ttm_bo_force_list_clean(bdev, mem_type, false);
  1151. ret = (*man->func->takedown)(man);
  1152. }
  1153. return ret;
  1154. }
  1155. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1156. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1157. {
  1158. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1159. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1160. printk(KERN_ERR TTM_PFX
  1161. "Illegal memory manager memory type %u.\n",
  1162. mem_type);
  1163. return -EINVAL;
  1164. }
  1165. if (!man->has_type) {
  1166. printk(KERN_ERR TTM_PFX
  1167. "Memory type %u has not been initialized.\n",
  1168. mem_type);
  1169. return 0;
  1170. }
  1171. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1172. }
  1173. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1174. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1175. unsigned long p_size)
  1176. {
  1177. int ret = -EINVAL;
  1178. struct ttm_mem_type_manager *man;
  1179. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1180. man = &bdev->man[type];
  1181. BUG_ON(man->has_type);
  1182. man->io_reserve_fastpath = true;
  1183. man->use_io_reserve_lru = false;
  1184. mutex_init(&man->io_reserve_mutex);
  1185. INIT_LIST_HEAD(&man->io_reserve_lru);
  1186. ret = bdev->driver->init_mem_type(bdev, type, man);
  1187. if (ret)
  1188. return ret;
  1189. man->bdev = bdev;
  1190. ret = 0;
  1191. if (type != TTM_PL_SYSTEM) {
  1192. ret = (*man->func->init)(man, p_size);
  1193. if (ret)
  1194. return ret;
  1195. }
  1196. man->has_type = true;
  1197. man->use_type = true;
  1198. man->size = p_size;
  1199. INIT_LIST_HEAD(&man->lru);
  1200. return 0;
  1201. }
  1202. EXPORT_SYMBOL(ttm_bo_init_mm);
  1203. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1204. {
  1205. struct ttm_bo_global *glob =
  1206. container_of(kobj, struct ttm_bo_global, kobj);
  1207. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1208. __free_page(glob->dummy_read_page);
  1209. kfree(glob);
  1210. }
  1211. void ttm_bo_global_release(struct drm_global_reference *ref)
  1212. {
  1213. struct ttm_bo_global *glob = ref->object;
  1214. kobject_del(&glob->kobj);
  1215. kobject_put(&glob->kobj);
  1216. }
  1217. EXPORT_SYMBOL(ttm_bo_global_release);
  1218. int ttm_bo_global_init(struct drm_global_reference *ref)
  1219. {
  1220. struct ttm_bo_global_ref *bo_ref =
  1221. container_of(ref, struct ttm_bo_global_ref, ref);
  1222. struct ttm_bo_global *glob = ref->object;
  1223. int ret;
  1224. mutex_init(&glob->device_list_mutex);
  1225. spin_lock_init(&glob->lru_lock);
  1226. glob->mem_glob = bo_ref->mem_glob;
  1227. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1228. if (unlikely(glob->dummy_read_page == NULL)) {
  1229. ret = -ENOMEM;
  1230. goto out_no_drp;
  1231. }
  1232. INIT_LIST_HEAD(&glob->swap_lru);
  1233. INIT_LIST_HEAD(&glob->device_list);
  1234. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1235. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1236. if (unlikely(ret != 0)) {
  1237. printk(KERN_ERR TTM_PFX
  1238. "Could not register buffer object swapout.\n");
  1239. goto out_no_shrink;
  1240. }
  1241. glob->ttm_bo_extra_size =
  1242. ttm_round_pot(sizeof(struct ttm_tt)) +
  1243. ttm_round_pot(sizeof(struct ttm_backend));
  1244. glob->ttm_bo_size = glob->ttm_bo_extra_size +
  1245. ttm_round_pot(sizeof(struct ttm_buffer_object));
  1246. atomic_set(&glob->bo_count, 0);
  1247. ret = kobject_init_and_add(
  1248. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1249. if (unlikely(ret != 0))
  1250. kobject_put(&glob->kobj);
  1251. return ret;
  1252. out_no_shrink:
  1253. __free_page(glob->dummy_read_page);
  1254. out_no_drp:
  1255. kfree(glob);
  1256. return ret;
  1257. }
  1258. EXPORT_SYMBOL(ttm_bo_global_init);
  1259. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1260. {
  1261. int ret = 0;
  1262. unsigned i = TTM_NUM_MEM_TYPES;
  1263. struct ttm_mem_type_manager *man;
  1264. struct ttm_bo_global *glob = bdev->glob;
  1265. while (i--) {
  1266. man = &bdev->man[i];
  1267. if (man->has_type) {
  1268. man->use_type = false;
  1269. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1270. ret = -EBUSY;
  1271. printk(KERN_ERR TTM_PFX
  1272. "DRM memory manager type %d "
  1273. "is not clean.\n", i);
  1274. }
  1275. man->has_type = false;
  1276. }
  1277. }
  1278. mutex_lock(&glob->device_list_mutex);
  1279. list_del(&bdev->device_list);
  1280. mutex_unlock(&glob->device_list_mutex);
  1281. cancel_delayed_work_sync(&bdev->wq);
  1282. while (ttm_bo_delayed_delete(bdev, true))
  1283. ;
  1284. spin_lock(&glob->lru_lock);
  1285. if (list_empty(&bdev->ddestroy))
  1286. TTM_DEBUG("Delayed destroy list was clean\n");
  1287. if (list_empty(&bdev->man[0].lru))
  1288. TTM_DEBUG("Swap list was clean\n");
  1289. spin_unlock(&glob->lru_lock);
  1290. BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
  1291. write_lock(&bdev->vm_lock);
  1292. drm_mm_takedown(&bdev->addr_space_mm);
  1293. write_unlock(&bdev->vm_lock);
  1294. return ret;
  1295. }
  1296. EXPORT_SYMBOL(ttm_bo_device_release);
  1297. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1298. struct ttm_bo_global *glob,
  1299. struct ttm_bo_driver *driver,
  1300. uint64_t file_page_offset,
  1301. bool need_dma32)
  1302. {
  1303. int ret = -EINVAL;
  1304. rwlock_init(&bdev->vm_lock);
  1305. bdev->driver = driver;
  1306. memset(bdev->man, 0, sizeof(bdev->man));
  1307. /*
  1308. * Initialize the system memory buffer type.
  1309. * Other types need to be driver / IOCTL initialized.
  1310. */
  1311. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1312. if (unlikely(ret != 0))
  1313. goto out_no_sys;
  1314. bdev->addr_space_rb = RB_ROOT;
  1315. ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
  1316. if (unlikely(ret != 0))
  1317. goto out_no_addr_mm;
  1318. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1319. bdev->nice_mode = true;
  1320. INIT_LIST_HEAD(&bdev->ddestroy);
  1321. bdev->dev_mapping = NULL;
  1322. bdev->glob = glob;
  1323. bdev->need_dma32 = need_dma32;
  1324. bdev->val_seq = 0;
  1325. spin_lock_init(&bdev->fence_lock);
  1326. mutex_lock(&glob->device_list_mutex);
  1327. list_add_tail(&bdev->device_list, &glob->device_list);
  1328. mutex_unlock(&glob->device_list_mutex);
  1329. return 0;
  1330. out_no_addr_mm:
  1331. ttm_bo_clean_mm(bdev, 0);
  1332. out_no_sys:
  1333. return ret;
  1334. }
  1335. EXPORT_SYMBOL(ttm_bo_device_init);
  1336. /*
  1337. * buffer object vm functions.
  1338. */
  1339. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1340. {
  1341. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1342. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1343. if (mem->mem_type == TTM_PL_SYSTEM)
  1344. return false;
  1345. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1346. return false;
  1347. if (mem->placement & TTM_PL_FLAG_CACHED)
  1348. return false;
  1349. }
  1350. return true;
  1351. }
  1352. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1353. {
  1354. struct ttm_bo_device *bdev = bo->bdev;
  1355. loff_t offset = (loff_t) bo->addr_space_offset;
  1356. loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
  1357. if (!bdev->dev_mapping)
  1358. return;
  1359. unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
  1360. ttm_mem_io_free_vm(bo);
  1361. }
  1362. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1363. {
  1364. struct ttm_bo_device *bdev = bo->bdev;
  1365. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1366. ttm_mem_io_lock(man, false);
  1367. ttm_bo_unmap_virtual_locked(bo);
  1368. ttm_mem_io_unlock(man);
  1369. }
  1370. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1371. static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
  1372. {
  1373. struct ttm_bo_device *bdev = bo->bdev;
  1374. struct rb_node **cur = &bdev->addr_space_rb.rb_node;
  1375. struct rb_node *parent = NULL;
  1376. struct ttm_buffer_object *cur_bo;
  1377. unsigned long offset = bo->vm_node->start;
  1378. unsigned long cur_offset;
  1379. while (*cur) {
  1380. parent = *cur;
  1381. cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
  1382. cur_offset = cur_bo->vm_node->start;
  1383. if (offset < cur_offset)
  1384. cur = &parent->rb_left;
  1385. else if (offset > cur_offset)
  1386. cur = &parent->rb_right;
  1387. else
  1388. BUG();
  1389. }
  1390. rb_link_node(&bo->vm_rb, parent, cur);
  1391. rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
  1392. }
  1393. /**
  1394. * ttm_bo_setup_vm:
  1395. *
  1396. * @bo: the buffer to allocate address space for
  1397. *
  1398. * Allocate address space in the drm device so that applications
  1399. * can mmap the buffer and access the contents. This only
  1400. * applies to ttm_bo_type_device objects as others are not
  1401. * placed in the drm device address space.
  1402. */
  1403. static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
  1404. {
  1405. struct ttm_bo_device *bdev = bo->bdev;
  1406. int ret;
  1407. retry_pre_get:
  1408. ret = drm_mm_pre_get(&bdev->addr_space_mm);
  1409. if (unlikely(ret != 0))
  1410. return ret;
  1411. write_lock(&bdev->vm_lock);
  1412. bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
  1413. bo->mem.num_pages, 0, 0);
  1414. if (unlikely(bo->vm_node == NULL)) {
  1415. ret = -ENOMEM;
  1416. goto out_unlock;
  1417. }
  1418. bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
  1419. bo->mem.num_pages, 0);
  1420. if (unlikely(bo->vm_node == NULL)) {
  1421. write_unlock(&bdev->vm_lock);
  1422. goto retry_pre_get;
  1423. }
  1424. ttm_bo_vm_insert_rb(bo);
  1425. write_unlock(&bdev->vm_lock);
  1426. bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
  1427. return 0;
  1428. out_unlock:
  1429. write_unlock(&bdev->vm_lock);
  1430. return ret;
  1431. }
  1432. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1433. bool lazy, bool interruptible, bool no_wait)
  1434. {
  1435. struct ttm_bo_driver *driver = bo->bdev->driver;
  1436. struct ttm_bo_device *bdev = bo->bdev;
  1437. void *sync_obj;
  1438. void *sync_obj_arg;
  1439. int ret = 0;
  1440. if (likely(bo->sync_obj == NULL))
  1441. return 0;
  1442. while (bo->sync_obj) {
  1443. if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
  1444. void *tmp_obj = bo->sync_obj;
  1445. bo->sync_obj = NULL;
  1446. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1447. spin_unlock(&bdev->fence_lock);
  1448. driver->sync_obj_unref(&tmp_obj);
  1449. spin_lock(&bdev->fence_lock);
  1450. continue;
  1451. }
  1452. if (no_wait)
  1453. return -EBUSY;
  1454. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  1455. sync_obj_arg = bo->sync_obj_arg;
  1456. spin_unlock(&bdev->fence_lock);
  1457. ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
  1458. lazy, interruptible);
  1459. if (unlikely(ret != 0)) {
  1460. driver->sync_obj_unref(&sync_obj);
  1461. spin_lock(&bdev->fence_lock);
  1462. return ret;
  1463. }
  1464. spin_lock(&bdev->fence_lock);
  1465. if (likely(bo->sync_obj == sync_obj &&
  1466. bo->sync_obj_arg == sync_obj_arg)) {
  1467. void *tmp_obj = bo->sync_obj;
  1468. bo->sync_obj = NULL;
  1469. clear_bit(TTM_BO_PRIV_FLAG_MOVING,
  1470. &bo->priv_flags);
  1471. spin_unlock(&bdev->fence_lock);
  1472. driver->sync_obj_unref(&sync_obj);
  1473. driver->sync_obj_unref(&tmp_obj);
  1474. spin_lock(&bdev->fence_lock);
  1475. } else {
  1476. spin_unlock(&bdev->fence_lock);
  1477. driver->sync_obj_unref(&sync_obj);
  1478. spin_lock(&bdev->fence_lock);
  1479. }
  1480. }
  1481. return 0;
  1482. }
  1483. EXPORT_SYMBOL(ttm_bo_wait);
  1484. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1485. {
  1486. struct ttm_bo_device *bdev = bo->bdev;
  1487. int ret = 0;
  1488. /*
  1489. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1490. */
  1491. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  1492. if (unlikely(ret != 0))
  1493. return ret;
  1494. spin_lock(&bdev->fence_lock);
  1495. ret = ttm_bo_wait(bo, false, true, no_wait);
  1496. spin_unlock(&bdev->fence_lock);
  1497. if (likely(ret == 0))
  1498. atomic_inc(&bo->cpu_writers);
  1499. ttm_bo_unreserve(bo);
  1500. return ret;
  1501. }
  1502. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1503. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1504. {
  1505. if (atomic_dec_and_test(&bo->cpu_writers))
  1506. wake_up_all(&bo->event_queue);
  1507. }
  1508. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1509. /**
  1510. * A buffer object shrink method that tries to swap out the first
  1511. * buffer object on the bo_global::swap_lru list.
  1512. */
  1513. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1514. {
  1515. struct ttm_bo_global *glob =
  1516. container_of(shrink, struct ttm_bo_global, shrink);
  1517. struct ttm_buffer_object *bo;
  1518. int ret = -EBUSY;
  1519. int put_count;
  1520. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1521. spin_lock(&glob->lru_lock);
  1522. while (ret == -EBUSY) {
  1523. if (unlikely(list_empty(&glob->swap_lru))) {
  1524. spin_unlock(&glob->lru_lock);
  1525. return -EBUSY;
  1526. }
  1527. bo = list_first_entry(&glob->swap_lru,
  1528. struct ttm_buffer_object, swap);
  1529. kref_get(&bo->list_kref);
  1530. if (!list_empty(&bo->ddestroy)) {
  1531. spin_unlock(&glob->lru_lock);
  1532. (void) ttm_bo_cleanup_refs(bo, false, false, false);
  1533. kref_put(&bo->list_kref, ttm_bo_release_list);
  1534. continue;
  1535. }
  1536. /**
  1537. * Reserve buffer. Since we unlock while sleeping, we need
  1538. * to re-check that nobody removed us from the swap-list while
  1539. * we slept.
  1540. */
  1541. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  1542. if (unlikely(ret == -EBUSY)) {
  1543. spin_unlock(&glob->lru_lock);
  1544. ttm_bo_wait_unreserved(bo, false);
  1545. kref_put(&bo->list_kref, ttm_bo_release_list);
  1546. spin_lock(&glob->lru_lock);
  1547. }
  1548. }
  1549. BUG_ON(ret != 0);
  1550. put_count = ttm_bo_del_from_lru(bo);
  1551. spin_unlock(&glob->lru_lock);
  1552. ttm_bo_list_ref_sub(bo, put_count, true);
  1553. /**
  1554. * Wait for GPU, then move to system cached.
  1555. */
  1556. spin_lock(&bo->bdev->fence_lock);
  1557. ret = ttm_bo_wait(bo, false, false, false);
  1558. spin_unlock(&bo->bdev->fence_lock);
  1559. if (unlikely(ret != 0))
  1560. goto out;
  1561. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1562. struct ttm_mem_reg evict_mem;
  1563. evict_mem = bo->mem;
  1564. evict_mem.mm_node = NULL;
  1565. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1566. evict_mem.mem_type = TTM_PL_SYSTEM;
  1567. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1568. false, false, false);
  1569. if (unlikely(ret != 0))
  1570. goto out;
  1571. }
  1572. ttm_bo_unmap_virtual(bo);
  1573. /**
  1574. * Swap out. Buffer will be swapped in again as soon as
  1575. * anyone tries to access a ttm page.
  1576. */
  1577. if (bo->bdev->driver->swap_notify)
  1578. bo->bdev->driver->swap_notify(bo);
  1579. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1580. out:
  1581. /**
  1582. *
  1583. * Unreserve without putting on LRU to avoid swapping out an
  1584. * already swapped buffer.
  1585. */
  1586. atomic_set(&bo->reserved, 0);
  1587. wake_up_all(&bo->event_queue);
  1588. kref_put(&bo->list_kref, ttm_bo_release_list);
  1589. return ret;
  1590. }
  1591. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1592. {
  1593. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1594. ;
  1595. }
  1596. EXPORT_SYMBOL(ttm_bo_swapout_all);