ttm_bo.c 45 KB

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