ttm_bo.c 46 KB

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