ttm_bo.c 45 KB

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