ttm_bo.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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. size_t acc_size = bo->acc_size;
  123. BUG_ON(atomic_read(&bo->list_kref.refcount));
  124. BUG_ON(atomic_read(&bo->kref.refcount));
  125. BUG_ON(atomic_read(&bo->cpu_writers));
  126. BUG_ON(bo->sync_obj != NULL);
  127. BUG_ON(bo->mem.mm_node != NULL);
  128. BUG_ON(!list_empty(&bo->lru));
  129. BUG_ON(!list_empty(&bo->ddestroy));
  130. if (bo->ttm)
  131. ttm_tt_destroy(bo->ttm);
  132. atomic_dec(&bo->glob->bo_count);
  133. if (bo->destroy)
  134. bo->destroy(bo);
  135. else {
  136. kfree(bo);
  137. }
  138. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  139. }
  140. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible)
  141. {
  142. if (interruptible) {
  143. return wait_event_interruptible(bo->event_queue,
  144. atomic_read(&bo->reserved) == 0);
  145. } else {
  146. wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0);
  147. return 0;
  148. }
  149. }
  150. EXPORT_SYMBOL(ttm_bo_wait_unreserved);
  151. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  152. {
  153. struct ttm_bo_device *bdev = bo->bdev;
  154. struct ttm_mem_type_manager *man;
  155. BUG_ON(!atomic_read(&bo->reserved));
  156. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  157. BUG_ON(!list_empty(&bo->lru));
  158. man = &bdev->man[bo->mem.mem_type];
  159. list_add_tail(&bo->lru, &man->lru);
  160. kref_get(&bo->list_kref);
  161. if (bo->ttm != NULL) {
  162. list_add_tail(&bo->swap, &bo->glob->swap_lru);
  163. kref_get(&bo->list_kref);
  164. }
  165. }
  166. }
  167. int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  168. {
  169. int put_count = 0;
  170. if (!list_empty(&bo->swap)) {
  171. list_del_init(&bo->swap);
  172. ++put_count;
  173. }
  174. if (!list_empty(&bo->lru)) {
  175. list_del_init(&bo->lru);
  176. ++put_count;
  177. }
  178. /*
  179. * TODO: Add a driver hook to delete from
  180. * driver-specific LRU's here.
  181. */
  182. return put_count;
  183. }
  184. int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
  185. bool interruptible,
  186. bool no_wait, bool use_sequence, uint32_t sequence)
  187. {
  188. struct ttm_bo_global *glob = bo->glob;
  189. int ret;
  190. while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) {
  191. /**
  192. * Deadlock avoidance for multi-bo reserving.
  193. */
  194. if (use_sequence && bo->seq_valid) {
  195. /**
  196. * We've already reserved this one.
  197. */
  198. if (unlikely(sequence == bo->val_seq))
  199. return -EDEADLK;
  200. /**
  201. * Already reserved by a thread that will not back
  202. * off for us. We need to back off.
  203. */
  204. if (unlikely(sequence - bo->val_seq < (1 << 31)))
  205. return -EAGAIN;
  206. }
  207. if (no_wait)
  208. return -EBUSY;
  209. spin_unlock(&glob->lru_lock);
  210. ret = ttm_bo_wait_unreserved(bo, interruptible);
  211. spin_lock(&glob->lru_lock);
  212. if (unlikely(ret))
  213. return ret;
  214. }
  215. if (use_sequence) {
  216. /**
  217. * Wake up waiters that may need to recheck for deadlock,
  218. * if we decreased the sequence number.
  219. */
  220. if (unlikely((bo->val_seq - sequence < (1 << 31))
  221. || !bo->seq_valid))
  222. wake_up_all(&bo->event_queue);
  223. bo->val_seq = sequence;
  224. bo->seq_valid = true;
  225. } else {
  226. bo->seq_valid = false;
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(ttm_bo_reserve);
  231. static void ttm_bo_ref_bug(struct kref *list_kref)
  232. {
  233. BUG();
  234. }
  235. void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
  236. bool never_free)
  237. {
  238. kref_sub(&bo->list_kref, count,
  239. (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
  240. }
  241. int ttm_bo_reserve(struct ttm_buffer_object *bo,
  242. bool interruptible,
  243. bool no_wait, bool use_sequence, uint32_t sequence)
  244. {
  245. struct ttm_bo_global *glob = bo->glob;
  246. int put_count = 0;
  247. int ret;
  248. spin_lock(&glob->lru_lock);
  249. ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence,
  250. sequence);
  251. if (likely(ret == 0))
  252. put_count = ttm_bo_del_from_lru(bo);
  253. spin_unlock(&glob->lru_lock);
  254. ttm_bo_list_ref_sub(bo, put_count, true);
  255. return ret;
  256. }
  257. void ttm_bo_unreserve_locked(struct ttm_buffer_object *bo)
  258. {
  259. ttm_bo_add_to_lru(bo);
  260. atomic_set(&bo->reserved, 0);
  261. wake_up_all(&bo->event_queue);
  262. }
  263. void ttm_bo_unreserve(struct ttm_buffer_object *bo)
  264. {
  265. struct ttm_bo_global *glob = bo->glob;
  266. spin_lock(&glob->lru_lock);
  267. ttm_bo_unreserve_locked(bo);
  268. spin_unlock(&glob->lru_lock);
  269. }
  270. EXPORT_SYMBOL(ttm_bo_unreserve);
  271. /*
  272. * Call bo->mutex locked.
  273. */
  274. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  275. {
  276. struct ttm_bo_device *bdev = bo->bdev;
  277. struct ttm_bo_global *glob = bo->glob;
  278. int ret = 0;
  279. uint32_t page_flags = 0;
  280. TTM_ASSERT_LOCKED(&bo->mutex);
  281. bo->ttm = NULL;
  282. if (bdev->need_dma32)
  283. page_flags |= TTM_PAGE_FLAG_DMA32;
  284. switch (bo->type) {
  285. case ttm_bo_type_device:
  286. if (zero_alloc)
  287. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  288. case ttm_bo_type_kernel:
  289. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  290. page_flags, glob->dummy_read_page);
  291. if (unlikely(bo->ttm == NULL))
  292. ret = -ENOMEM;
  293. break;
  294. default:
  295. printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
  296. ret = -EINVAL;
  297. break;
  298. }
  299. return ret;
  300. }
  301. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  302. struct ttm_mem_reg *mem,
  303. bool evict, bool interruptible,
  304. bool no_wait_reserve, bool no_wait_gpu)
  305. {
  306. struct ttm_bo_device *bdev = bo->bdev;
  307. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  308. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  309. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  310. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  311. int ret = 0;
  312. if (old_is_pci || new_is_pci ||
  313. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  314. ret = ttm_mem_io_lock(old_man, true);
  315. if (unlikely(ret != 0))
  316. goto out_err;
  317. ttm_bo_unmap_virtual_locked(bo);
  318. ttm_mem_io_unlock(old_man);
  319. }
  320. /*
  321. * Create and bind a ttm if required.
  322. */
  323. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  324. if (bo->ttm == NULL) {
  325. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  326. ret = ttm_bo_add_ttm(bo, zero);
  327. if (ret)
  328. goto out_err;
  329. }
  330. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  331. if (ret)
  332. goto out_err;
  333. if (mem->mem_type != TTM_PL_SYSTEM) {
  334. ret = ttm_tt_bind(bo->ttm, mem);
  335. if (ret)
  336. goto out_err;
  337. }
  338. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  339. if (bdev->driver->move_notify)
  340. bdev->driver->move_notify(bo, mem);
  341. bo->mem = *mem;
  342. mem->mm_node = NULL;
  343. goto moved;
  344. }
  345. }
  346. if (bdev->driver->move_notify)
  347. bdev->driver->move_notify(bo, mem);
  348. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  349. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  350. ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, mem);
  351. else if (bdev->driver->move)
  352. ret = bdev->driver->move(bo, evict, interruptible,
  353. no_wait_reserve, no_wait_gpu, mem);
  354. else
  355. ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, mem);
  356. if (ret) {
  357. if (bdev->driver->move_notify) {
  358. struct ttm_mem_reg tmp_mem = *mem;
  359. *mem = bo->mem;
  360. bo->mem = tmp_mem;
  361. bdev->driver->move_notify(bo, mem);
  362. bo->mem = *mem;
  363. }
  364. goto out_err;
  365. }
  366. moved:
  367. if (bo->evicted) {
  368. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  369. if (ret)
  370. printk(KERN_ERR TTM_PFX "Can not flush read caches\n");
  371. bo->evicted = false;
  372. }
  373. if (bo->mem.mm_node) {
  374. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  375. bdev->man[bo->mem.mem_type].gpu_offset;
  376. bo->cur_placement = bo->mem.placement;
  377. } else
  378. bo->offset = 0;
  379. return 0;
  380. out_err:
  381. new_man = &bdev->man[bo->mem.mem_type];
  382. if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
  383. ttm_tt_unbind(bo->ttm);
  384. ttm_tt_destroy(bo->ttm);
  385. bo->ttm = NULL;
  386. }
  387. return ret;
  388. }
  389. /**
  390. * Call bo::reserved.
  391. * Will release GPU memory type usage on destruction.
  392. * This is the place to put in driver specific hooks to release
  393. * driver private resources.
  394. * Will release the bo::reserved lock.
  395. */
  396. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  397. {
  398. if (bo->bdev->driver->move_notify)
  399. bo->bdev->driver->move_notify(bo, NULL);
  400. if (bo->ttm) {
  401. ttm_tt_unbind(bo->ttm);
  402. ttm_tt_destroy(bo->ttm);
  403. bo->ttm = NULL;
  404. }
  405. ttm_bo_mem_put(bo, &bo->mem);
  406. atomic_set(&bo->reserved, 0);
  407. /*
  408. * Make processes trying to reserve really pick it up.
  409. */
  410. smp_mb__after_atomic_dec();
  411. wake_up_all(&bo->event_queue);
  412. }
  413. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  414. {
  415. struct ttm_bo_device *bdev = bo->bdev;
  416. struct ttm_bo_global *glob = bo->glob;
  417. struct ttm_bo_driver *driver;
  418. void *sync_obj = NULL;
  419. void *sync_obj_arg;
  420. int put_count;
  421. int ret;
  422. spin_lock(&bdev->fence_lock);
  423. (void) ttm_bo_wait(bo, false, false, true);
  424. if (!bo->sync_obj) {
  425. spin_lock(&glob->lru_lock);
  426. /**
  427. * Lock inversion between bo:reserve and bdev::fence_lock here,
  428. * but that's OK, since we're only trylocking.
  429. */
  430. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  431. if (unlikely(ret == -EBUSY))
  432. goto queue;
  433. spin_unlock(&bdev->fence_lock);
  434. put_count = ttm_bo_del_from_lru(bo);
  435. spin_unlock(&glob->lru_lock);
  436. ttm_bo_cleanup_memtype_use(bo);
  437. ttm_bo_list_ref_sub(bo, put_count, true);
  438. return;
  439. } else {
  440. spin_lock(&glob->lru_lock);
  441. }
  442. queue:
  443. driver = bdev->driver;
  444. if (bo->sync_obj)
  445. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  446. sync_obj_arg = bo->sync_obj_arg;
  447. kref_get(&bo->list_kref);
  448. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  449. spin_unlock(&glob->lru_lock);
  450. spin_unlock(&bdev->fence_lock);
  451. if (sync_obj) {
  452. driver->sync_obj_flush(sync_obj, sync_obj_arg);
  453. driver->sync_obj_unref(&sync_obj);
  454. }
  455. schedule_delayed_work(&bdev->wq,
  456. ((HZ / 100) < 1) ? 1 : HZ / 100);
  457. }
  458. /**
  459. * function ttm_bo_cleanup_refs
  460. * If bo idle, remove from delayed- and lru lists, and unref.
  461. * If not idle, do nothing.
  462. *
  463. * @interruptible Any sleeps should occur interruptibly.
  464. * @no_wait_reserve Never wait for reserve. Return -EBUSY instead.
  465. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  466. */
  467. static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo,
  468. bool interruptible,
  469. bool no_wait_reserve,
  470. bool no_wait_gpu)
  471. {
  472. struct ttm_bo_device *bdev = bo->bdev;
  473. struct ttm_bo_global *glob = bo->glob;
  474. int put_count;
  475. int ret = 0;
  476. retry:
  477. spin_lock(&bdev->fence_lock);
  478. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  479. spin_unlock(&bdev->fence_lock);
  480. if (unlikely(ret != 0))
  481. return ret;
  482. spin_lock(&glob->lru_lock);
  483. if (unlikely(list_empty(&bo->ddestroy))) {
  484. spin_unlock(&glob->lru_lock);
  485. return 0;
  486. }
  487. ret = ttm_bo_reserve_locked(bo, interruptible,
  488. no_wait_reserve, false, 0);
  489. if (unlikely(ret != 0)) {
  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. uint32_t mem_type,
  764. uint32_t proposed_placement,
  765. uint32_t *masked_placement)
  766. {
  767. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  768. if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
  769. return false;
  770. if ((proposed_placement & man->available_caching) == 0)
  771. return false;
  772. cur_flags |= (proposed_placement & man->available_caching);
  773. *masked_placement = cur_flags;
  774. return true;
  775. }
  776. /**
  777. * Creates space for memory region @mem according to its type.
  778. *
  779. * This function first searches for free space in compatible memory types in
  780. * the priority order defined by the driver. If free space isn't found, then
  781. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  782. * space.
  783. */
  784. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  785. struct ttm_placement *placement,
  786. struct ttm_mem_reg *mem,
  787. bool interruptible, bool no_wait_reserve,
  788. bool no_wait_gpu)
  789. {
  790. struct ttm_bo_device *bdev = bo->bdev;
  791. struct ttm_mem_type_manager *man;
  792. uint32_t mem_type = TTM_PL_SYSTEM;
  793. uint32_t cur_flags = 0;
  794. bool type_found = false;
  795. bool type_ok = false;
  796. bool has_erestartsys = false;
  797. int i, ret;
  798. mem->mm_node = NULL;
  799. for (i = 0; i < placement->num_placement; ++i) {
  800. ret = ttm_mem_type_from_flags(placement->placement[i],
  801. &mem_type);
  802. if (ret)
  803. return ret;
  804. man = &bdev->man[mem_type];
  805. type_ok = ttm_bo_mt_compatible(man,
  806. mem_type,
  807. placement->placement[i],
  808. &cur_flags);
  809. if (!type_ok)
  810. continue;
  811. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  812. cur_flags);
  813. /*
  814. * Use the access and other non-mapping-related flag bits from
  815. * the memory placement flags to the current flags
  816. */
  817. ttm_flag_masked(&cur_flags, placement->placement[i],
  818. ~TTM_PL_MASK_MEMTYPE);
  819. if (mem_type == TTM_PL_SYSTEM)
  820. break;
  821. if (man->has_type && man->use_type) {
  822. type_found = true;
  823. ret = (*man->func->get_node)(man, bo, placement, mem);
  824. if (unlikely(ret))
  825. return ret;
  826. }
  827. if (mem->mm_node)
  828. break;
  829. }
  830. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  831. mem->mem_type = mem_type;
  832. mem->placement = cur_flags;
  833. return 0;
  834. }
  835. if (!type_found)
  836. return -EINVAL;
  837. for (i = 0; i < placement->num_busy_placement; ++i) {
  838. ret = ttm_mem_type_from_flags(placement->busy_placement[i],
  839. &mem_type);
  840. if (ret)
  841. return ret;
  842. man = &bdev->man[mem_type];
  843. if (!man->has_type)
  844. continue;
  845. if (!ttm_bo_mt_compatible(man,
  846. mem_type,
  847. placement->busy_placement[i],
  848. &cur_flags))
  849. continue;
  850. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  851. cur_flags);
  852. /*
  853. * Use the access and other non-mapping-related flag bits from
  854. * the memory placement flags to the current flags
  855. */
  856. ttm_flag_masked(&cur_flags, placement->busy_placement[i],
  857. ~TTM_PL_MASK_MEMTYPE);
  858. if (mem_type == TTM_PL_SYSTEM) {
  859. mem->mem_type = mem_type;
  860. mem->placement = cur_flags;
  861. mem->mm_node = NULL;
  862. return 0;
  863. }
  864. ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
  865. interruptible, no_wait_reserve, no_wait_gpu);
  866. if (ret == 0 && mem->mm_node) {
  867. mem->placement = cur_flags;
  868. return 0;
  869. }
  870. if (ret == -ERESTARTSYS)
  871. has_erestartsys = true;
  872. }
  873. ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  874. return ret;
  875. }
  876. EXPORT_SYMBOL(ttm_bo_mem_space);
  877. int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait)
  878. {
  879. if ((atomic_read(&bo->cpu_writers) > 0) && no_wait)
  880. return -EBUSY;
  881. return wait_event_interruptible(bo->event_queue,
  882. atomic_read(&bo->cpu_writers) == 0);
  883. }
  884. EXPORT_SYMBOL(ttm_bo_wait_cpu);
  885. int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  886. struct ttm_placement *placement,
  887. bool interruptible, bool no_wait_reserve,
  888. bool no_wait_gpu)
  889. {
  890. int ret = 0;
  891. struct ttm_mem_reg mem;
  892. struct ttm_bo_device *bdev = bo->bdev;
  893. BUG_ON(!atomic_read(&bo->reserved));
  894. /*
  895. * FIXME: It's possible to pipeline buffer moves.
  896. * Have the driver move function wait for idle when necessary,
  897. * instead of doing it here.
  898. */
  899. spin_lock(&bdev->fence_lock);
  900. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  901. spin_unlock(&bdev->fence_lock);
  902. if (ret)
  903. return ret;
  904. mem.num_pages = bo->num_pages;
  905. mem.size = mem.num_pages << PAGE_SHIFT;
  906. mem.page_alignment = bo->mem.page_alignment;
  907. mem.bus.io_reserved_vm = false;
  908. mem.bus.io_reserved_count = 0;
  909. /*
  910. * Determine where to move the buffer.
  911. */
  912. ret = ttm_bo_mem_space(bo, placement, &mem, interruptible, no_wait_reserve, no_wait_gpu);
  913. if (ret)
  914. goto out_unlock;
  915. ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait_reserve, no_wait_gpu);
  916. out_unlock:
  917. if (ret && mem.mm_node)
  918. ttm_bo_mem_put(bo, &mem);
  919. return ret;
  920. }
  921. static int ttm_bo_mem_compat(struct ttm_placement *placement,
  922. struct ttm_mem_reg *mem)
  923. {
  924. int i;
  925. if (mem->mm_node && placement->lpfn != 0 &&
  926. (mem->start < placement->fpfn ||
  927. mem->start + mem->num_pages > placement->lpfn))
  928. return -1;
  929. for (i = 0; i < placement->num_placement; i++) {
  930. if ((placement->placement[i] & mem->placement &
  931. TTM_PL_MASK_CACHING) &&
  932. (placement->placement[i] & mem->placement &
  933. TTM_PL_MASK_MEM))
  934. return i;
  935. }
  936. return -1;
  937. }
  938. int ttm_bo_validate(struct ttm_buffer_object *bo,
  939. struct ttm_placement *placement,
  940. bool interruptible, bool no_wait_reserve,
  941. bool no_wait_gpu)
  942. {
  943. int ret;
  944. BUG_ON(!atomic_read(&bo->reserved));
  945. /* Check that range is valid */
  946. if (placement->lpfn || placement->fpfn)
  947. if (placement->fpfn > placement->lpfn ||
  948. (placement->lpfn - placement->fpfn) < bo->num_pages)
  949. return -EINVAL;
  950. /*
  951. * Check whether we need to move buffer.
  952. */
  953. ret = ttm_bo_mem_compat(placement, &bo->mem);
  954. if (ret < 0) {
  955. ret = ttm_bo_move_buffer(bo, placement, interruptible, no_wait_reserve, no_wait_gpu);
  956. if (ret)
  957. return ret;
  958. } else {
  959. /*
  960. * Use the access and other non-mapping-related flag bits from
  961. * the compatible memory placement flags to the active flags
  962. */
  963. ttm_flag_masked(&bo->mem.placement, placement->placement[ret],
  964. ~TTM_PL_MASK_MEMTYPE);
  965. }
  966. /*
  967. * We might need to add a TTM.
  968. */
  969. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  970. ret = ttm_bo_add_ttm(bo, true);
  971. if (ret)
  972. return ret;
  973. }
  974. return 0;
  975. }
  976. EXPORT_SYMBOL(ttm_bo_validate);
  977. int ttm_bo_check_placement(struct ttm_buffer_object *bo,
  978. struct ttm_placement *placement)
  979. {
  980. BUG_ON((placement->fpfn || placement->lpfn) &&
  981. (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
  982. return 0;
  983. }
  984. int ttm_bo_init(struct ttm_bo_device *bdev,
  985. struct ttm_buffer_object *bo,
  986. unsigned long size,
  987. enum ttm_bo_type type,
  988. struct ttm_placement *placement,
  989. uint32_t page_alignment,
  990. unsigned long buffer_start,
  991. bool interruptible,
  992. struct file *persistent_swap_storage,
  993. size_t acc_size,
  994. void (*destroy) (struct ttm_buffer_object *))
  995. {
  996. int ret = 0;
  997. unsigned long num_pages;
  998. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  999. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  1000. if (ret) {
  1001. printk(KERN_ERR TTM_PFX "Out of kernel memory.\n");
  1002. if (destroy)
  1003. (*destroy)(bo);
  1004. else
  1005. kfree(bo);
  1006. return -ENOMEM;
  1007. }
  1008. size += buffer_start & ~PAGE_MASK;
  1009. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1010. if (num_pages == 0) {
  1011. printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n");
  1012. if (destroy)
  1013. (*destroy)(bo);
  1014. else
  1015. kfree(bo);
  1016. return -EINVAL;
  1017. }
  1018. bo->destroy = destroy;
  1019. kref_init(&bo->kref);
  1020. kref_init(&bo->list_kref);
  1021. atomic_set(&bo->cpu_writers, 0);
  1022. atomic_set(&bo->reserved, 1);
  1023. init_waitqueue_head(&bo->event_queue);
  1024. INIT_LIST_HEAD(&bo->lru);
  1025. INIT_LIST_HEAD(&bo->ddestroy);
  1026. INIT_LIST_HEAD(&bo->swap);
  1027. INIT_LIST_HEAD(&bo->io_reserve_lru);
  1028. bo->bdev = bdev;
  1029. bo->glob = bdev->glob;
  1030. bo->type = type;
  1031. bo->num_pages = num_pages;
  1032. bo->mem.size = num_pages << PAGE_SHIFT;
  1033. bo->mem.mem_type = TTM_PL_SYSTEM;
  1034. bo->mem.num_pages = bo->num_pages;
  1035. bo->mem.mm_node = NULL;
  1036. bo->mem.page_alignment = page_alignment;
  1037. bo->mem.bus.io_reserved_vm = false;
  1038. bo->mem.bus.io_reserved_count = 0;
  1039. bo->buffer_start = buffer_start & PAGE_MASK;
  1040. bo->priv_flags = 0;
  1041. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1042. bo->seq_valid = false;
  1043. bo->persistent_swap_storage = persistent_swap_storage;
  1044. bo->acc_size = acc_size;
  1045. atomic_inc(&bo->glob->bo_count);
  1046. ret = ttm_bo_check_placement(bo, placement);
  1047. if (unlikely(ret != 0))
  1048. goto out_err;
  1049. /*
  1050. * For ttm_bo_type_device buffers, allocate
  1051. * address space from the device.
  1052. */
  1053. if (bo->type == ttm_bo_type_device) {
  1054. ret = ttm_bo_setup_vm(bo);
  1055. if (ret)
  1056. goto out_err;
  1057. }
  1058. ret = ttm_bo_validate(bo, placement, interruptible, false, false);
  1059. if (ret)
  1060. goto out_err;
  1061. ttm_bo_unreserve(bo);
  1062. return 0;
  1063. out_err:
  1064. ttm_bo_unreserve(bo);
  1065. ttm_bo_unref(&bo);
  1066. return ret;
  1067. }
  1068. EXPORT_SYMBOL(ttm_bo_init);
  1069. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1070. unsigned long bo_size,
  1071. unsigned struct_size)
  1072. {
  1073. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1074. size_t size = 0;
  1075. size += ttm_round_pot(struct_size);
  1076. size += PAGE_ALIGN(npages * sizeof(void *));
  1077. size += ttm_round_pot(sizeof(struct ttm_tt));
  1078. return size;
  1079. }
  1080. EXPORT_SYMBOL(ttm_bo_acc_size);
  1081. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1082. unsigned long bo_size,
  1083. unsigned struct_size)
  1084. {
  1085. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1086. size_t size = 0;
  1087. size += ttm_round_pot(struct_size);
  1088. size += PAGE_ALIGN(npages * sizeof(void *));
  1089. size += PAGE_ALIGN(npages * sizeof(dma_addr_t));
  1090. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1091. return size;
  1092. }
  1093. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1094. int ttm_bo_create(struct ttm_bo_device *bdev,
  1095. unsigned long size,
  1096. enum ttm_bo_type type,
  1097. struct ttm_placement *placement,
  1098. uint32_t page_alignment,
  1099. unsigned long buffer_start,
  1100. bool interruptible,
  1101. struct file *persistent_swap_storage,
  1102. struct ttm_buffer_object **p_bo)
  1103. {
  1104. struct ttm_buffer_object *bo;
  1105. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  1106. size_t acc_size;
  1107. int ret;
  1108. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1109. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  1110. if (unlikely(ret != 0))
  1111. return ret;
  1112. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1113. if (unlikely(bo == NULL)) {
  1114. ttm_mem_global_free(mem_glob, acc_size);
  1115. return -ENOMEM;
  1116. }
  1117. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1118. buffer_start, interruptible,
  1119. persistent_swap_storage, acc_size, NULL);
  1120. if (likely(ret == 0))
  1121. *p_bo = bo;
  1122. return ret;
  1123. }
  1124. EXPORT_SYMBOL(ttm_bo_create);
  1125. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1126. unsigned mem_type, bool allow_errors)
  1127. {
  1128. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1129. struct ttm_bo_global *glob = bdev->glob;
  1130. int ret;
  1131. /*
  1132. * Can't use standard list traversal since we're unlocking.
  1133. */
  1134. spin_lock(&glob->lru_lock);
  1135. while (!list_empty(&man->lru)) {
  1136. spin_unlock(&glob->lru_lock);
  1137. ret = ttm_mem_evict_first(bdev, mem_type, false, false, false);
  1138. if (ret) {
  1139. if (allow_errors) {
  1140. return ret;
  1141. } else {
  1142. printk(KERN_ERR TTM_PFX
  1143. "Cleanup eviction failed\n");
  1144. }
  1145. }
  1146. spin_lock(&glob->lru_lock);
  1147. }
  1148. spin_unlock(&glob->lru_lock);
  1149. return 0;
  1150. }
  1151. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1152. {
  1153. struct ttm_mem_type_manager *man;
  1154. int ret = -EINVAL;
  1155. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1156. printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type);
  1157. return ret;
  1158. }
  1159. man = &bdev->man[mem_type];
  1160. if (!man->has_type) {
  1161. printk(KERN_ERR TTM_PFX "Trying to take down uninitialized "
  1162. "memory manager type %u\n", mem_type);
  1163. return ret;
  1164. }
  1165. man->use_type = false;
  1166. man->has_type = false;
  1167. ret = 0;
  1168. if (mem_type > 0) {
  1169. ttm_bo_force_list_clean(bdev, mem_type, false);
  1170. ret = (*man->func->takedown)(man);
  1171. }
  1172. return ret;
  1173. }
  1174. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1175. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1176. {
  1177. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1178. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1179. printk(KERN_ERR TTM_PFX
  1180. "Illegal memory manager memory type %u.\n",
  1181. mem_type);
  1182. return -EINVAL;
  1183. }
  1184. if (!man->has_type) {
  1185. printk(KERN_ERR TTM_PFX
  1186. "Memory type %u has not been initialized.\n",
  1187. mem_type);
  1188. return 0;
  1189. }
  1190. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1191. }
  1192. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1193. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1194. unsigned long p_size)
  1195. {
  1196. int ret = -EINVAL;
  1197. struct ttm_mem_type_manager *man;
  1198. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1199. man = &bdev->man[type];
  1200. BUG_ON(man->has_type);
  1201. man->io_reserve_fastpath = true;
  1202. man->use_io_reserve_lru = false;
  1203. mutex_init(&man->io_reserve_mutex);
  1204. INIT_LIST_HEAD(&man->io_reserve_lru);
  1205. ret = bdev->driver->init_mem_type(bdev, type, man);
  1206. if (ret)
  1207. return ret;
  1208. man->bdev = bdev;
  1209. ret = 0;
  1210. if (type != TTM_PL_SYSTEM) {
  1211. ret = (*man->func->init)(man, p_size);
  1212. if (ret)
  1213. return ret;
  1214. }
  1215. man->has_type = true;
  1216. man->use_type = true;
  1217. man->size = p_size;
  1218. INIT_LIST_HEAD(&man->lru);
  1219. return 0;
  1220. }
  1221. EXPORT_SYMBOL(ttm_bo_init_mm);
  1222. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1223. {
  1224. struct ttm_bo_global *glob =
  1225. container_of(kobj, struct ttm_bo_global, kobj);
  1226. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1227. __free_page(glob->dummy_read_page);
  1228. kfree(glob);
  1229. }
  1230. void ttm_bo_global_release(struct drm_global_reference *ref)
  1231. {
  1232. struct ttm_bo_global *glob = ref->object;
  1233. kobject_del(&glob->kobj);
  1234. kobject_put(&glob->kobj);
  1235. }
  1236. EXPORT_SYMBOL(ttm_bo_global_release);
  1237. int ttm_bo_global_init(struct drm_global_reference *ref)
  1238. {
  1239. struct ttm_bo_global_ref *bo_ref =
  1240. container_of(ref, struct ttm_bo_global_ref, ref);
  1241. struct ttm_bo_global *glob = ref->object;
  1242. int ret;
  1243. mutex_init(&glob->device_list_mutex);
  1244. spin_lock_init(&glob->lru_lock);
  1245. glob->mem_glob = bo_ref->mem_glob;
  1246. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1247. if (unlikely(glob->dummy_read_page == NULL)) {
  1248. ret = -ENOMEM;
  1249. goto out_no_drp;
  1250. }
  1251. INIT_LIST_HEAD(&glob->swap_lru);
  1252. INIT_LIST_HEAD(&glob->device_list);
  1253. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1254. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1255. if (unlikely(ret != 0)) {
  1256. printk(KERN_ERR TTM_PFX
  1257. "Could not register buffer object swapout.\n");
  1258. goto out_no_shrink;
  1259. }
  1260. atomic_set(&glob->bo_count, 0);
  1261. ret = kobject_init_and_add(
  1262. &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects");
  1263. if (unlikely(ret != 0))
  1264. kobject_put(&glob->kobj);
  1265. return ret;
  1266. out_no_shrink:
  1267. __free_page(glob->dummy_read_page);
  1268. out_no_drp:
  1269. kfree(glob);
  1270. return ret;
  1271. }
  1272. EXPORT_SYMBOL(ttm_bo_global_init);
  1273. int ttm_bo_device_release(struct ttm_bo_device *bdev)
  1274. {
  1275. int ret = 0;
  1276. unsigned i = TTM_NUM_MEM_TYPES;
  1277. struct ttm_mem_type_manager *man;
  1278. struct ttm_bo_global *glob = bdev->glob;
  1279. while (i--) {
  1280. man = &bdev->man[i];
  1281. if (man->has_type) {
  1282. man->use_type = false;
  1283. if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
  1284. ret = -EBUSY;
  1285. printk(KERN_ERR TTM_PFX
  1286. "DRM memory manager type %d "
  1287. "is not clean.\n", i);
  1288. }
  1289. man->has_type = false;
  1290. }
  1291. }
  1292. mutex_lock(&glob->device_list_mutex);
  1293. list_del(&bdev->device_list);
  1294. mutex_unlock(&glob->device_list_mutex);
  1295. cancel_delayed_work_sync(&bdev->wq);
  1296. while (ttm_bo_delayed_delete(bdev, true))
  1297. ;
  1298. spin_lock(&glob->lru_lock);
  1299. if (list_empty(&bdev->ddestroy))
  1300. TTM_DEBUG("Delayed destroy list was clean\n");
  1301. if (list_empty(&bdev->man[0].lru))
  1302. TTM_DEBUG("Swap list was clean\n");
  1303. spin_unlock(&glob->lru_lock);
  1304. BUG_ON(!drm_mm_clean(&bdev->addr_space_mm));
  1305. write_lock(&bdev->vm_lock);
  1306. drm_mm_takedown(&bdev->addr_space_mm);
  1307. write_unlock(&bdev->vm_lock);
  1308. return ret;
  1309. }
  1310. EXPORT_SYMBOL(ttm_bo_device_release);
  1311. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1312. struct ttm_bo_global *glob,
  1313. struct ttm_bo_driver *driver,
  1314. uint64_t file_page_offset,
  1315. bool need_dma32)
  1316. {
  1317. int ret = -EINVAL;
  1318. rwlock_init(&bdev->vm_lock);
  1319. bdev->driver = driver;
  1320. memset(bdev->man, 0, sizeof(bdev->man));
  1321. /*
  1322. * Initialize the system memory buffer type.
  1323. * Other types need to be driver / IOCTL initialized.
  1324. */
  1325. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1326. if (unlikely(ret != 0))
  1327. goto out_no_sys;
  1328. bdev->addr_space_rb = RB_ROOT;
  1329. ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000);
  1330. if (unlikely(ret != 0))
  1331. goto out_no_addr_mm;
  1332. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1333. bdev->nice_mode = true;
  1334. INIT_LIST_HEAD(&bdev->ddestroy);
  1335. bdev->dev_mapping = NULL;
  1336. bdev->glob = glob;
  1337. bdev->need_dma32 = need_dma32;
  1338. bdev->val_seq = 0;
  1339. spin_lock_init(&bdev->fence_lock);
  1340. mutex_lock(&glob->device_list_mutex);
  1341. list_add_tail(&bdev->device_list, &glob->device_list);
  1342. mutex_unlock(&glob->device_list_mutex);
  1343. return 0;
  1344. out_no_addr_mm:
  1345. ttm_bo_clean_mm(bdev, 0);
  1346. out_no_sys:
  1347. return ret;
  1348. }
  1349. EXPORT_SYMBOL(ttm_bo_device_init);
  1350. /*
  1351. * buffer object vm functions.
  1352. */
  1353. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1354. {
  1355. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1356. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1357. if (mem->mem_type == TTM_PL_SYSTEM)
  1358. return false;
  1359. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1360. return false;
  1361. if (mem->placement & TTM_PL_FLAG_CACHED)
  1362. return false;
  1363. }
  1364. return true;
  1365. }
  1366. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1367. {
  1368. struct ttm_bo_device *bdev = bo->bdev;
  1369. loff_t offset = (loff_t) bo->addr_space_offset;
  1370. loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
  1371. if (!bdev->dev_mapping)
  1372. return;
  1373. unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
  1374. ttm_mem_io_free_vm(bo);
  1375. }
  1376. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1377. {
  1378. struct ttm_bo_device *bdev = bo->bdev;
  1379. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1380. ttm_mem_io_lock(man, false);
  1381. ttm_bo_unmap_virtual_locked(bo);
  1382. ttm_mem_io_unlock(man);
  1383. }
  1384. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1385. static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo)
  1386. {
  1387. struct ttm_bo_device *bdev = bo->bdev;
  1388. struct rb_node **cur = &bdev->addr_space_rb.rb_node;
  1389. struct rb_node *parent = NULL;
  1390. struct ttm_buffer_object *cur_bo;
  1391. unsigned long offset = bo->vm_node->start;
  1392. unsigned long cur_offset;
  1393. while (*cur) {
  1394. parent = *cur;
  1395. cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb);
  1396. cur_offset = cur_bo->vm_node->start;
  1397. if (offset < cur_offset)
  1398. cur = &parent->rb_left;
  1399. else if (offset > cur_offset)
  1400. cur = &parent->rb_right;
  1401. else
  1402. BUG();
  1403. }
  1404. rb_link_node(&bo->vm_rb, parent, cur);
  1405. rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb);
  1406. }
  1407. /**
  1408. * ttm_bo_setup_vm:
  1409. *
  1410. * @bo: the buffer to allocate address space for
  1411. *
  1412. * Allocate address space in the drm device so that applications
  1413. * can mmap the buffer and access the contents. This only
  1414. * applies to ttm_bo_type_device objects as others are not
  1415. * placed in the drm device address space.
  1416. */
  1417. static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
  1418. {
  1419. struct ttm_bo_device *bdev = bo->bdev;
  1420. int ret;
  1421. retry_pre_get:
  1422. ret = drm_mm_pre_get(&bdev->addr_space_mm);
  1423. if (unlikely(ret != 0))
  1424. return ret;
  1425. write_lock(&bdev->vm_lock);
  1426. bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm,
  1427. bo->mem.num_pages, 0, 0);
  1428. if (unlikely(bo->vm_node == NULL)) {
  1429. ret = -ENOMEM;
  1430. goto out_unlock;
  1431. }
  1432. bo->vm_node = drm_mm_get_block_atomic(bo->vm_node,
  1433. bo->mem.num_pages, 0);
  1434. if (unlikely(bo->vm_node == NULL)) {
  1435. write_unlock(&bdev->vm_lock);
  1436. goto retry_pre_get;
  1437. }
  1438. ttm_bo_vm_insert_rb(bo);
  1439. write_unlock(&bdev->vm_lock);
  1440. bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT;
  1441. return 0;
  1442. out_unlock:
  1443. write_unlock(&bdev->vm_lock);
  1444. return ret;
  1445. }
  1446. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1447. bool lazy, bool interruptible, bool no_wait)
  1448. {
  1449. struct ttm_bo_driver *driver = bo->bdev->driver;
  1450. struct ttm_bo_device *bdev = bo->bdev;
  1451. void *sync_obj;
  1452. void *sync_obj_arg;
  1453. int ret = 0;
  1454. if (likely(bo->sync_obj == NULL))
  1455. return 0;
  1456. while (bo->sync_obj) {
  1457. if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) {
  1458. void *tmp_obj = bo->sync_obj;
  1459. bo->sync_obj = NULL;
  1460. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1461. spin_unlock(&bdev->fence_lock);
  1462. driver->sync_obj_unref(&tmp_obj);
  1463. spin_lock(&bdev->fence_lock);
  1464. continue;
  1465. }
  1466. if (no_wait)
  1467. return -EBUSY;
  1468. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  1469. sync_obj_arg = bo->sync_obj_arg;
  1470. spin_unlock(&bdev->fence_lock);
  1471. ret = driver->sync_obj_wait(sync_obj, sync_obj_arg,
  1472. lazy, interruptible);
  1473. if (unlikely(ret != 0)) {
  1474. driver->sync_obj_unref(&sync_obj);
  1475. spin_lock(&bdev->fence_lock);
  1476. return ret;
  1477. }
  1478. spin_lock(&bdev->fence_lock);
  1479. if (likely(bo->sync_obj == sync_obj &&
  1480. bo->sync_obj_arg == sync_obj_arg)) {
  1481. void *tmp_obj = bo->sync_obj;
  1482. bo->sync_obj = NULL;
  1483. clear_bit(TTM_BO_PRIV_FLAG_MOVING,
  1484. &bo->priv_flags);
  1485. spin_unlock(&bdev->fence_lock);
  1486. driver->sync_obj_unref(&sync_obj);
  1487. driver->sync_obj_unref(&tmp_obj);
  1488. spin_lock(&bdev->fence_lock);
  1489. } else {
  1490. spin_unlock(&bdev->fence_lock);
  1491. driver->sync_obj_unref(&sync_obj);
  1492. spin_lock(&bdev->fence_lock);
  1493. }
  1494. }
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL(ttm_bo_wait);
  1498. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1499. {
  1500. struct ttm_bo_device *bdev = bo->bdev;
  1501. int ret = 0;
  1502. /*
  1503. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1504. */
  1505. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  1506. if (unlikely(ret != 0))
  1507. return ret;
  1508. spin_lock(&bdev->fence_lock);
  1509. ret = ttm_bo_wait(bo, false, true, no_wait);
  1510. spin_unlock(&bdev->fence_lock);
  1511. if (likely(ret == 0))
  1512. atomic_inc(&bo->cpu_writers);
  1513. ttm_bo_unreserve(bo);
  1514. return ret;
  1515. }
  1516. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1517. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1518. {
  1519. if (atomic_dec_and_test(&bo->cpu_writers))
  1520. wake_up_all(&bo->event_queue);
  1521. }
  1522. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1523. /**
  1524. * A buffer object shrink method that tries to swap out the first
  1525. * buffer object on the bo_global::swap_lru list.
  1526. */
  1527. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1528. {
  1529. struct ttm_bo_global *glob =
  1530. container_of(shrink, struct ttm_bo_global, shrink);
  1531. struct ttm_buffer_object *bo;
  1532. int ret = -EBUSY;
  1533. int put_count;
  1534. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1535. spin_lock(&glob->lru_lock);
  1536. while (ret == -EBUSY) {
  1537. if (unlikely(list_empty(&glob->swap_lru))) {
  1538. spin_unlock(&glob->lru_lock);
  1539. return -EBUSY;
  1540. }
  1541. bo = list_first_entry(&glob->swap_lru,
  1542. struct ttm_buffer_object, swap);
  1543. kref_get(&bo->list_kref);
  1544. if (!list_empty(&bo->ddestroy)) {
  1545. spin_unlock(&glob->lru_lock);
  1546. (void) ttm_bo_cleanup_refs(bo, false, false, false);
  1547. kref_put(&bo->list_kref, ttm_bo_release_list);
  1548. continue;
  1549. }
  1550. /**
  1551. * Reserve buffer. Since we unlock while sleeping, we need
  1552. * to re-check that nobody removed us from the swap-list while
  1553. * we slept.
  1554. */
  1555. ret = ttm_bo_reserve_locked(bo, false, true, false, 0);
  1556. if (unlikely(ret == -EBUSY)) {
  1557. spin_unlock(&glob->lru_lock);
  1558. ttm_bo_wait_unreserved(bo, false);
  1559. kref_put(&bo->list_kref, ttm_bo_release_list);
  1560. spin_lock(&glob->lru_lock);
  1561. }
  1562. }
  1563. BUG_ON(ret != 0);
  1564. put_count = ttm_bo_del_from_lru(bo);
  1565. spin_unlock(&glob->lru_lock);
  1566. ttm_bo_list_ref_sub(bo, put_count, true);
  1567. /**
  1568. * Wait for GPU, then move to system cached.
  1569. */
  1570. spin_lock(&bo->bdev->fence_lock);
  1571. ret = ttm_bo_wait(bo, false, false, false);
  1572. spin_unlock(&bo->bdev->fence_lock);
  1573. if (unlikely(ret != 0))
  1574. goto out;
  1575. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1576. struct ttm_mem_reg evict_mem;
  1577. evict_mem = bo->mem;
  1578. evict_mem.mm_node = NULL;
  1579. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1580. evict_mem.mem_type = TTM_PL_SYSTEM;
  1581. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1582. false, false, false);
  1583. if (unlikely(ret != 0))
  1584. goto out;
  1585. }
  1586. ttm_bo_unmap_virtual(bo);
  1587. /**
  1588. * Swap out. Buffer will be swapped in again as soon as
  1589. * anyone tries to access a ttm page.
  1590. */
  1591. if (bo->bdev->driver->swap_notify)
  1592. bo->bdev->driver->swap_notify(bo);
  1593. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1594. out:
  1595. /**
  1596. *
  1597. * Unreserve without putting on LRU to avoid swapping out an
  1598. * already swapped buffer.
  1599. */
  1600. atomic_set(&bo->reserved, 0);
  1601. wake_up_all(&bo->event_queue);
  1602. kref_put(&bo->list_kref, ttm_bo_release_list);
  1603. return ret;
  1604. }
  1605. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1606. {
  1607. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1608. ;
  1609. }
  1610. EXPORT_SYMBOL(ttm_bo_swapout_all);