ttm_bo.c 46 KB

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