ttm_bo.c 44 KB

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