ttm_bo.c 46 KB

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