ttm_bo.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/slab.h>
  36. #include <linux/sched.h>
  37. #include <linux/mm.h>
  38. #include <linux/file.h>
  39. #include <linux/module.h>
  40. #include <linux/atomic.h>
  41. #include <linux/reservation.h>
  42. #define TTM_ASSERT_LOCKED(param)
  43. #define TTM_DEBUG(fmt, arg...)
  44. #define TTM_BO_HASH_ORDER 13
  45. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
  46. static void ttm_bo_global_kobj_release(struct kobject *kobj);
  47. static struct attribute ttm_bo_count = {
  48. .name = "bo_count",
  49. .mode = S_IRUGO
  50. };
  51. static inline int ttm_mem_type_from_place(const struct ttm_place *place,
  52. uint32_t *mem_type)
  53. {
  54. int pos;
  55. pos = ffs(place->flags & TTM_PL_MASK_MEM);
  56. if (unlikely(!pos))
  57. return -EINVAL;
  58. *mem_type = pos - 1;
  59. return 0;
  60. }
  61. static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
  62. {
  63. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  64. pr_err(" has_type: %d\n", man->has_type);
  65. pr_err(" use_type: %d\n", man->use_type);
  66. pr_err(" flags: 0x%08X\n", man->flags);
  67. pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset);
  68. pr_err(" size: %llu\n", man->size);
  69. pr_err(" available_caching: 0x%08X\n", man->available_caching);
  70. pr_err(" default_caching: 0x%08X\n", man->default_caching);
  71. if (mem_type != TTM_PL_SYSTEM)
  72. (*man->func->debug)(man, TTM_PFX);
  73. }
  74. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  75. struct ttm_placement *placement)
  76. {
  77. int i, ret, mem_type;
  78. pr_err("No space for %p (%lu pages, %luK, %luM)\n",
  79. bo, bo->mem.num_pages, bo->mem.size >> 10,
  80. bo->mem.size >> 20);
  81. for (i = 0; i < placement->num_placement; i++) {
  82. ret = ttm_mem_type_from_place(&placement->placement[i],
  83. &mem_type);
  84. if (ret)
  85. return;
  86. pr_err(" placement[%d]=0x%08X (%d)\n",
  87. i, placement->placement[i].flags, mem_type);
  88. ttm_mem_type_debug(bo->bdev, mem_type);
  89. }
  90. }
  91. static ssize_t ttm_bo_global_show(struct kobject *kobj,
  92. struct attribute *attr,
  93. char *buffer)
  94. {
  95. struct ttm_bo_global *glob =
  96. container_of(kobj, struct ttm_bo_global, kobj);
  97. return snprintf(buffer, PAGE_SIZE, "%lu\n",
  98. (unsigned long) atomic_read(&glob->bo_count));
  99. }
  100. static struct attribute *ttm_bo_global_attrs[] = {
  101. &ttm_bo_count,
  102. NULL
  103. };
  104. static const struct sysfs_ops ttm_bo_global_ops = {
  105. .show = &ttm_bo_global_show
  106. };
  107. static struct kobj_type ttm_bo_glob_kobj_type = {
  108. .release = &ttm_bo_global_kobj_release,
  109. .sysfs_ops = &ttm_bo_global_ops,
  110. .default_attrs = ttm_bo_global_attrs
  111. };
  112. static inline uint32_t ttm_bo_type_flags(unsigned type)
  113. {
  114. return 1 << (type);
  115. }
  116. static void ttm_bo_release_list(struct kref *list_kref)
  117. {
  118. struct ttm_buffer_object *bo =
  119. container_of(list_kref, struct ttm_buffer_object, list_kref);
  120. struct ttm_bo_device *bdev = bo->bdev;
  121. size_t acc_size = bo->acc_size;
  122. BUG_ON(atomic_read(&bo->list_kref.refcount));
  123. BUG_ON(atomic_read(&bo->kref.refcount));
  124. BUG_ON(atomic_read(&bo->cpu_writers));
  125. BUG_ON(bo->mem.mm_node != NULL);
  126. BUG_ON(!list_empty(&bo->lru));
  127. BUG_ON(!list_empty(&bo->ddestroy));
  128. ttm_tt_destroy(bo->ttm);
  129. atomic_dec(&bo->glob->bo_count);
  130. dma_fence_put(bo->moving);
  131. if (bo->resv == &bo->ttm_resv)
  132. reservation_object_fini(&bo->ttm_resv);
  133. mutex_destroy(&bo->wu_mutex);
  134. if (bo->destroy)
  135. bo->destroy(bo);
  136. else {
  137. kfree(bo);
  138. }
  139. ttm_mem_global_free(bdev->glob->mem_glob, acc_size);
  140. }
  141. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
  142. {
  143. struct ttm_bo_device *bdev = bo->bdev;
  144. lockdep_assert_held(&bo->resv->lock.base);
  145. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  146. BUG_ON(!list_empty(&bo->lru));
  147. list_add(&bo->lru, bdev->driver->lru_tail(bo));
  148. kref_get(&bo->list_kref);
  149. if (bo->ttm && !(bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
  150. list_add(&bo->swap, bdev->driver->swap_lru_tail(bo));
  151. kref_get(&bo->list_kref);
  152. }
  153. }
  154. }
  155. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  156. int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  157. {
  158. struct ttm_bo_device *bdev = bo->bdev;
  159. int put_count = 0;
  160. if (bdev->driver->lru_removal)
  161. bdev->driver->lru_removal(bo);
  162. if (!list_empty(&bo->swap)) {
  163. list_del_init(&bo->swap);
  164. ++put_count;
  165. }
  166. if (!list_empty(&bo->lru)) {
  167. list_del_init(&bo->lru);
  168. ++put_count;
  169. }
  170. return put_count;
  171. }
  172. static void ttm_bo_ref_bug(struct kref *list_kref)
  173. {
  174. BUG();
  175. }
  176. void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
  177. bool never_free)
  178. {
  179. kref_sub(&bo->list_kref, count,
  180. (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
  181. }
  182. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  183. {
  184. int put_count;
  185. spin_lock(&bo->glob->lru_lock);
  186. put_count = ttm_bo_del_from_lru(bo);
  187. spin_unlock(&bo->glob->lru_lock);
  188. ttm_bo_list_ref_sub(bo, put_count, true);
  189. }
  190. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  191. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo)
  192. {
  193. struct ttm_bo_device *bdev = bo->bdev;
  194. int put_count = 0;
  195. lockdep_assert_held(&bo->resv->lock.base);
  196. if (bdev->driver->lru_removal)
  197. bdev->driver->lru_removal(bo);
  198. put_count = ttm_bo_del_from_lru(bo);
  199. ttm_bo_list_ref_sub(bo, put_count, true);
  200. ttm_bo_add_to_lru(bo);
  201. }
  202. EXPORT_SYMBOL(ttm_bo_move_to_lru_tail);
  203. struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo)
  204. {
  205. return bo->bdev->man[bo->mem.mem_type].lru.prev;
  206. }
  207. EXPORT_SYMBOL(ttm_bo_default_lru_tail);
  208. struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo)
  209. {
  210. return bo->glob->swap_lru.prev;
  211. }
  212. EXPORT_SYMBOL(ttm_bo_default_swap_lru_tail);
  213. /*
  214. * Call bo->mutex locked.
  215. */
  216. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  217. {
  218. struct ttm_bo_device *bdev = bo->bdev;
  219. struct ttm_bo_global *glob = bo->glob;
  220. int ret = 0;
  221. uint32_t page_flags = 0;
  222. TTM_ASSERT_LOCKED(&bo->mutex);
  223. bo->ttm = NULL;
  224. if (bdev->need_dma32)
  225. page_flags |= TTM_PAGE_FLAG_DMA32;
  226. switch (bo->type) {
  227. case ttm_bo_type_device:
  228. if (zero_alloc)
  229. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  230. case ttm_bo_type_kernel:
  231. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  232. page_flags, glob->dummy_read_page);
  233. if (unlikely(bo->ttm == NULL))
  234. ret = -ENOMEM;
  235. break;
  236. case ttm_bo_type_sg:
  237. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  238. page_flags | TTM_PAGE_FLAG_SG,
  239. glob->dummy_read_page);
  240. if (unlikely(bo->ttm == NULL)) {
  241. ret = -ENOMEM;
  242. break;
  243. }
  244. bo->ttm->sg = bo->sg;
  245. break;
  246. default:
  247. pr_err("Illegal buffer object type\n");
  248. ret = -EINVAL;
  249. break;
  250. }
  251. return ret;
  252. }
  253. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  254. struct ttm_mem_reg *mem,
  255. bool evict, bool interruptible,
  256. bool no_wait_gpu)
  257. {
  258. struct ttm_bo_device *bdev = bo->bdev;
  259. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  260. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  261. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  262. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  263. int ret = 0;
  264. if (old_is_pci || new_is_pci ||
  265. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  266. ret = ttm_mem_io_lock(old_man, true);
  267. if (unlikely(ret != 0))
  268. goto out_err;
  269. ttm_bo_unmap_virtual_locked(bo);
  270. ttm_mem_io_unlock(old_man);
  271. }
  272. /*
  273. * Create and bind a ttm if required.
  274. */
  275. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  276. if (bo->ttm == NULL) {
  277. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  278. ret = ttm_bo_add_ttm(bo, zero);
  279. if (ret)
  280. goto out_err;
  281. }
  282. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  283. if (ret)
  284. goto out_err;
  285. if (mem->mem_type != TTM_PL_SYSTEM) {
  286. ret = ttm_tt_bind(bo->ttm, mem);
  287. if (ret)
  288. goto out_err;
  289. }
  290. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  291. if (bdev->driver->move_notify)
  292. bdev->driver->move_notify(bo, evict, mem);
  293. bo->mem = *mem;
  294. mem->mm_node = NULL;
  295. goto moved;
  296. }
  297. }
  298. if (bdev->driver->move_notify)
  299. bdev->driver->move_notify(bo, evict, mem);
  300. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  301. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  302. ret = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, mem);
  303. else if (bdev->driver->move)
  304. ret = bdev->driver->move(bo, evict, interruptible,
  305. no_wait_gpu, mem);
  306. else
  307. ret = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, mem);
  308. if (ret) {
  309. if (bdev->driver->move_notify) {
  310. struct ttm_mem_reg tmp_mem = *mem;
  311. *mem = bo->mem;
  312. bo->mem = tmp_mem;
  313. bdev->driver->move_notify(bo, false, mem);
  314. bo->mem = *mem;
  315. *mem = tmp_mem;
  316. }
  317. goto out_err;
  318. }
  319. moved:
  320. if (bo->evicted) {
  321. if (bdev->driver->invalidate_caches) {
  322. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  323. if (ret)
  324. pr_err("Can not flush read caches\n");
  325. }
  326. bo->evicted = false;
  327. }
  328. if (bo->mem.mm_node) {
  329. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  330. bdev->man[bo->mem.mem_type].gpu_offset;
  331. bo->cur_placement = bo->mem.placement;
  332. } else
  333. bo->offset = 0;
  334. return 0;
  335. out_err:
  336. new_man = &bdev->man[bo->mem.mem_type];
  337. if (new_man->flags & TTM_MEMTYPE_FLAG_FIXED) {
  338. ttm_tt_destroy(bo->ttm);
  339. bo->ttm = NULL;
  340. }
  341. return ret;
  342. }
  343. /**
  344. * Call bo::reserved.
  345. * Will release GPU memory type usage on destruction.
  346. * This is the place to put in driver specific hooks to release
  347. * driver private resources.
  348. * Will release the bo::reserved lock.
  349. */
  350. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  351. {
  352. if (bo->bdev->driver->move_notify)
  353. bo->bdev->driver->move_notify(bo, false, NULL);
  354. ttm_tt_destroy(bo->ttm);
  355. bo->ttm = NULL;
  356. ttm_bo_mem_put(bo, &bo->mem);
  357. ww_mutex_unlock (&bo->resv->lock);
  358. }
  359. static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  360. {
  361. struct reservation_object_list *fobj;
  362. struct dma_fence *fence;
  363. int i;
  364. fobj = reservation_object_get_list(bo->resv);
  365. fence = reservation_object_get_excl(bo->resv);
  366. if (fence && !fence->ops->signaled)
  367. dma_fence_enable_sw_signaling(fence);
  368. for (i = 0; fobj && i < fobj->shared_count; ++i) {
  369. fence = rcu_dereference_protected(fobj->shared[i],
  370. reservation_object_held(bo->resv));
  371. if (!fence->ops->signaled)
  372. dma_fence_enable_sw_signaling(fence);
  373. }
  374. }
  375. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  376. {
  377. struct ttm_bo_device *bdev = bo->bdev;
  378. struct ttm_bo_global *glob = bo->glob;
  379. int put_count;
  380. int ret;
  381. spin_lock(&glob->lru_lock);
  382. ret = __ttm_bo_reserve(bo, false, true, NULL);
  383. if (!ret) {
  384. if (!ttm_bo_wait(bo, false, true)) {
  385. put_count = ttm_bo_del_from_lru(bo);
  386. spin_unlock(&glob->lru_lock);
  387. ttm_bo_cleanup_memtype_use(bo);
  388. ttm_bo_list_ref_sub(bo, put_count, true);
  389. return;
  390. } else
  391. ttm_bo_flush_all_fences(bo);
  392. /*
  393. * Make NO_EVICT bos immediately available to
  394. * shrinkers, now that they are queued for
  395. * destruction.
  396. */
  397. if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
  398. bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
  399. ttm_bo_add_to_lru(bo);
  400. }
  401. __ttm_bo_unreserve(bo);
  402. }
  403. kref_get(&bo->list_kref);
  404. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  405. spin_unlock(&glob->lru_lock);
  406. schedule_delayed_work(&bdev->wq,
  407. ((HZ / 100) < 1) ? 1 : HZ / 100);
  408. }
  409. /**
  410. * function ttm_bo_cleanup_refs_and_unlock
  411. * If bo idle, remove from delayed- and lru lists, and unref.
  412. * If not idle, do nothing.
  413. *
  414. * Must be called with lru_lock and reservation held, this function
  415. * will drop both before returning.
  416. *
  417. * @interruptible Any sleeps should occur interruptibly.
  418. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  419. */
  420. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  421. bool interruptible,
  422. bool no_wait_gpu)
  423. {
  424. struct ttm_bo_global *glob = bo->glob;
  425. int put_count;
  426. int ret;
  427. ret = ttm_bo_wait(bo, false, true);
  428. if (ret && !no_wait_gpu) {
  429. long lret;
  430. ww_mutex_unlock(&bo->resv->lock);
  431. spin_unlock(&glob->lru_lock);
  432. lret = reservation_object_wait_timeout_rcu(bo->resv,
  433. true,
  434. interruptible,
  435. 30 * HZ);
  436. if (lret < 0)
  437. return lret;
  438. else if (lret == 0)
  439. return -EBUSY;
  440. spin_lock(&glob->lru_lock);
  441. ret = __ttm_bo_reserve(bo, false, true, NULL);
  442. /*
  443. * We raced, and lost, someone else holds the reservation now,
  444. * and is probably busy in ttm_bo_cleanup_memtype_use.
  445. *
  446. * Even if it's not the case, because we finished waiting any
  447. * delayed destruction would succeed, so just return success
  448. * here.
  449. */
  450. if (ret) {
  451. spin_unlock(&glob->lru_lock);
  452. return 0;
  453. }
  454. /*
  455. * remove sync_obj with ttm_bo_wait, the wait should be
  456. * finished, and no new wait object should have been added.
  457. */
  458. ret = ttm_bo_wait(bo, false, true);
  459. WARN_ON(ret);
  460. }
  461. if (ret || unlikely(list_empty(&bo->ddestroy))) {
  462. __ttm_bo_unreserve(bo);
  463. spin_unlock(&glob->lru_lock);
  464. return ret;
  465. }
  466. put_count = ttm_bo_del_from_lru(bo);
  467. list_del_init(&bo->ddestroy);
  468. ++put_count;
  469. spin_unlock(&glob->lru_lock);
  470. ttm_bo_cleanup_memtype_use(bo);
  471. ttm_bo_list_ref_sub(bo, put_count, true);
  472. return 0;
  473. }
  474. /**
  475. * Traverse the delayed list, and call ttm_bo_cleanup_refs on all
  476. * encountered buffers.
  477. */
  478. static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all)
  479. {
  480. struct ttm_bo_global *glob = bdev->glob;
  481. struct ttm_buffer_object *entry = NULL;
  482. int ret = 0;
  483. spin_lock(&glob->lru_lock);
  484. if (list_empty(&bdev->ddestroy))
  485. goto out_unlock;
  486. entry = list_first_entry(&bdev->ddestroy,
  487. struct ttm_buffer_object, ddestroy);
  488. kref_get(&entry->list_kref);
  489. for (;;) {
  490. struct ttm_buffer_object *nentry = NULL;
  491. if (entry->ddestroy.next != &bdev->ddestroy) {
  492. nentry = list_first_entry(&entry->ddestroy,
  493. struct ttm_buffer_object, ddestroy);
  494. kref_get(&nentry->list_kref);
  495. }
  496. ret = __ttm_bo_reserve(entry, false, true, NULL);
  497. if (remove_all && ret) {
  498. spin_unlock(&glob->lru_lock);
  499. ret = __ttm_bo_reserve(entry, false, false, NULL);
  500. spin_lock(&glob->lru_lock);
  501. }
  502. if (!ret)
  503. ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  504. !remove_all);
  505. else
  506. spin_unlock(&glob->lru_lock);
  507. kref_put(&entry->list_kref, ttm_bo_release_list);
  508. entry = nentry;
  509. if (ret || !entry)
  510. goto out;
  511. spin_lock(&glob->lru_lock);
  512. if (list_empty(&entry->ddestroy))
  513. break;
  514. }
  515. out_unlock:
  516. spin_unlock(&glob->lru_lock);
  517. out:
  518. if (entry)
  519. kref_put(&entry->list_kref, ttm_bo_release_list);
  520. return ret;
  521. }
  522. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  523. {
  524. struct ttm_bo_device *bdev =
  525. container_of(work, struct ttm_bo_device, wq.work);
  526. if (ttm_bo_delayed_delete(bdev, false)) {
  527. schedule_delayed_work(&bdev->wq,
  528. ((HZ / 100) < 1) ? 1 : HZ / 100);
  529. }
  530. }
  531. static void ttm_bo_release(struct kref *kref)
  532. {
  533. struct ttm_buffer_object *bo =
  534. container_of(kref, struct ttm_buffer_object, kref);
  535. struct ttm_bo_device *bdev = bo->bdev;
  536. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  537. drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  538. ttm_mem_io_lock(man, false);
  539. ttm_mem_io_free_vm(bo);
  540. ttm_mem_io_unlock(man);
  541. ttm_bo_cleanup_refs_or_queue(bo);
  542. kref_put(&bo->list_kref, ttm_bo_release_list);
  543. }
  544. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  545. {
  546. struct ttm_buffer_object *bo = *p_bo;
  547. *p_bo = NULL;
  548. kref_put(&bo->kref, ttm_bo_release);
  549. }
  550. EXPORT_SYMBOL(ttm_bo_unref);
  551. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  552. {
  553. return cancel_delayed_work_sync(&bdev->wq);
  554. }
  555. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  556. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  557. {
  558. if (resched)
  559. schedule_delayed_work(&bdev->wq,
  560. ((HZ / 100) < 1) ? 1 : HZ / 100);
  561. }
  562. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  563. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  564. bool no_wait_gpu)
  565. {
  566. struct ttm_bo_device *bdev = bo->bdev;
  567. struct ttm_mem_reg evict_mem;
  568. struct ttm_placement placement;
  569. int ret = 0;
  570. lockdep_assert_held(&bo->resv->lock.base);
  571. evict_mem = bo->mem;
  572. evict_mem.mm_node = NULL;
  573. evict_mem.bus.io_reserved_vm = false;
  574. evict_mem.bus.io_reserved_count = 0;
  575. placement.num_placement = 0;
  576. placement.num_busy_placement = 0;
  577. bdev->driver->evict_flags(bo, &placement);
  578. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  579. no_wait_gpu);
  580. if (ret) {
  581. if (ret != -ERESTARTSYS) {
  582. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  583. bo);
  584. ttm_bo_mem_space_debug(bo, &placement);
  585. }
  586. goto out;
  587. }
  588. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  589. no_wait_gpu);
  590. if (unlikely(ret)) {
  591. if (ret != -ERESTARTSYS)
  592. pr_err("Buffer eviction failed\n");
  593. ttm_bo_mem_put(bo, &evict_mem);
  594. goto out;
  595. }
  596. bo->evicted = true;
  597. out:
  598. return ret;
  599. }
  600. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  601. const struct ttm_place *place)
  602. {
  603. /* Don't evict this BO if it's outside of the
  604. * requested placement range
  605. */
  606. if (place->fpfn >= (bo->mem.start + bo->mem.size) ||
  607. (place->lpfn && place->lpfn <= bo->mem.start))
  608. return false;
  609. return true;
  610. }
  611. EXPORT_SYMBOL(ttm_bo_eviction_valuable);
  612. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  613. uint32_t mem_type,
  614. const struct ttm_place *place,
  615. bool interruptible,
  616. bool no_wait_gpu)
  617. {
  618. struct ttm_bo_global *glob = bdev->glob;
  619. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  620. struct ttm_buffer_object *bo;
  621. int ret = -EBUSY, put_count;
  622. spin_lock(&glob->lru_lock);
  623. list_for_each_entry(bo, &man->lru, lru) {
  624. ret = __ttm_bo_reserve(bo, false, true, NULL);
  625. if (ret)
  626. continue;
  627. if (place && !bdev->driver->eviction_valuable(bo, place)) {
  628. __ttm_bo_unreserve(bo);
  629. ret = -EBUSY;
  630. continue;
  631. }
  632. break;
  633. }
  634. if (ret) {
  635. spin_unlock(&glob->lru_lock);
  636. return ret;
  637. }
  638. kref_get(&bo->list_kref);
  639. if (!list_empty(&bo->ddestroy)) {
  640. ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
  641. no_wait_gpu);
  642. kref_put(&bo->list_kref, ttm_bo_release_list);
  643. return ret;
  644. }
  645. put_count = ttm_bo_del_from_lru(bo);
  646. spin_unlock(&glob->lru_lock);
  647. BUG_ON(ret != 0);
  648. ttm_bo_list_ref_sub(bo, put_count, true);
  649. ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
  650. ttm_bo_unreserve(bo);
  651. kref_put(&bo->list_kref, ttm_bo_release_list);
  652. return ret;
  653. }
  654. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  655. {
  656. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  657. if (mem->mm_node)
  658. (*man->func->put_node)(man, mem);
  659. }
  660. EXPORT_SYMBOL(ttm_bo_mem_put);
  661. /**
  662. * Add the last move fence to the BO and reserve a new shared slot.
  663. */
  664. static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
  665. struct ttm_mem_type_manager *man,
  666. struct ttm_mem_reg *mem)
  667. {
  668. struct dma_fence *fence;
  669. int ret;
  670. spin_lock(&man->move_lock);
  671. fence = dma_fence_get(man->move);
  672. spin_unlock(&man->move_lock);
  673. if (fence) {
  674. reservation_object_add_shared_fence(bo->resv, fence);
  675. ret = reservation_object_reserve_shared(bo->resv);
  676. if (unlikely(ret))
  677. return ret;
  678. dma_fence_put(bo->moving);
  679. bo->moving = fence;
  680. }
  681. return 0;
  682. }
  683. /**
  684. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  685. * space, or we've evicted everything and there isn't enough space.
  686. */
  687. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  688. uint32_t mem_type,
  689. const struct ttm_place *place,
  690. struct ttm_mem_reg *mem,
  691. bool interruptible,
  692. bool no_wait_gpu)
  693. {
  694. struct ttm_bo_device *bdev = bo->bdev;
  695. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  696. int ret;
  697. do {
  698. ret = (*man->func->get_node)(man, bo, place, mem);
  699. if (unlikely(ret != 0))
  700. return ret;
  701. if (mem->mm_node)
  702. break;
  703. ret = ttm_mem_evict_first(bdev, mem_type, place,
  704. interruptible, no_wait_gpu);
  705. if (unlikely(ret != 0))
  706. return ret;
  707. } while (1);
  708. mem->mem_type = mem_type;
  709. return ttm_bo_add_move_fence(bo, man, mem);
  710. }
  711. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  712. uint32_t cur_placement,
  713. uint32_t proposed_placement)
  714. {
  715. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  716. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  717. /**
  718. * Keep current caching if possible.
  719. */
  720. if ((cur_placement & caching) != 0)
  721. result |= (cur_placement & caching);
  722. else if ((man->default_caching & caching) != 0)
  723. result |= man->default_caching;
  724. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  725. result |= TTM_PL_FLAG_CACHED;
  726. else if ((TTM_PL_FLAG_WC & caching) != 0)
  727. result |= TTM_PL_FLAG_WC;
  728. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  729. result |= TTM_PL_FLAG_UNCACHED;
  730. return result;
  731. }
  732. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  733. uint32_t mem_type,
  734. const struct ttm_place *place,
  735. uint32_t *masked_placement)
  736. {
  737. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  738. if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0)
  739. return false;
  740. if ((place->flags & man->available_caching) == 0)
  741. return false;
  742. cur_flags |= (place->flags & man->available_caching);
  743. *masked_placement = cur_flags;
  744. return true;
  745. }
  746. /**
  747. * Creates space for memory region @mem according to its type.
  748. *
  749. * This function first searches for free space in compatible memory types in
  750. * the priority order defined by the driver. If free space isn't found, then
  751. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  752. * space.
  753. */
  754. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  755. struct ttm_placement *placement,
  756. struct ttm_mem_reg *mem,
  757. bool interruptible,
  758. bool no_wait_gpu)
  759. {
  760. struct ttm_bo_device *bdev = bo->bdev;
  761. struct ttm_mem_type_manager *man;
  762. uint32_t mem_type = TTM_PL_SYSTEM;
  763. uint32_t cur_flags = 0;
  764. bool type_found = false;
  765. bool type_ok = false;
  766. bool has_erestartsys = false;
  767. int i, ret;
  768. ret = reservation_object_reserve_shared(bo->resv);
  769. if (unlikely(ret))
  770. return ret;
  771. mem->mm_node = NULL;
  772. for (i = 0; i < placement->num_placement; ++i) {
  773. const struct ttm_place *place = &placement->placement[i];
  774. ret = ttm_mem_type_from_place(place, &mem_type);
  775. if (ret)
  776. return ret;
  777. man = &bdev->man[mem_type];
  778. if (!man->has_type || !man->use_type)
  779. continue;
  780. type_ok = ttm_bo_mt_compatible(man, mem_type, place,
  781. &cur_flags);
  782. if (!type_ok)
  783. continue;
  784. type_found = true;
  785. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  786. cur_flags);
  787. /*
  788. * Use the access and other non-mapping-related flag bits from
  789. * the memory placement flags to the current flags
  790. */
  791. ttm_flag_masked(&cur_flags, place->flags,
  792. ~TTM_PL_MASK_MEMTYPE);
  793. if (mem_type == TTM_PL_SYSTEM)
  794. break;
  795. ret = (*man->func->get_node)(man, bo, place, mem);
  796. if (unlikely(ret))
  797. return ret;
  798. if (mem->mm_node) {
  799. ret = ttm_bo_add_move_fence(bo, man, mem);
  800. if (unlikely(ret)) {
  801. (*man->func->put_node)(man, mem);
  802. return ret;
  803. }
  804. break;
  805. }
  806. }
  807. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  808. mem->mem_type = mem_type;
  809. mem->placement = cur_flags;
  810. return 0;
  811. }
  812. for (i = 0; i < placement->num_busy_placement; ++i) {
  813. const struct ttm_place *place = &placement->busy_placement[i];
  814. ret = ttm_mem_type_from_place(place, &mem_type);
  815. if (ret)
  816. return ret;
  817. man = &bdev->man[mem_type];
  818. if (!man->has_type || !man->use_type)
  819. continue;
  820. if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags))
  821. continue;
  822. type_found = true;
  823. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  824. cur_flags);
  825. /*
  826. * Use the access and other non-mapping-related flag bits from
  827. * the memory placement flags to the current flags
  828. */
  829. ttm_flag_masked(&cur_flags, place->flags,
  830. ~TTM_PL_MASK_MEMTYPE);
  831. if (mem_type == TTM_PL_SYSTEM) {
  832. mem->mem_type = mem_type;
  833. mem->placement = cur_flags;
  834. mem->mm_node = NULL;
  835. return 0;
  836. }
  837. ret = ttm_bo_mem_force_space(bo, mem_type, place, mem,
  838. interruptible, no_wait_gpu);
  839. if (ret == 0 && mem->mm_node) {
  840. mem->placement = cur_flags;
  841. return 0;
  842. }
  843. if (ret == -ERESTARTSYS)
  844. has_erestartsys = true;
  845. }
  846. if (!type_found) {
  847. printk(KERN_ERR TTM_PFX "No compatible memory type found.\n");
  848. return -EINVAL;
  849. }
  850. return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  851. }
  852. EXPORT_SYMBOL(ttm_bo_mem_space);
  853. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  854. struct ttm_placement *placement,
  855. bool interruptible,
  856. bool no_wait_gpu)
  857. {
  858. int ret = 0;
  859. struct ttm_mem_reg mem;
  860. lockdep_assert_held(&bo->resv->lock.base);
  861. mem.num_pages = bo->num_pages;
  862. mem.size = mem.num_pages << PAGE_SHIFT;
  863. mem.page_alignment = bo->mem.page_alignment;
  864. mem.bus.io_reserved_vm = false;
  865. mem.bus.io_reserved_count = 0;
  866. /*
  867. * Determine where to move the buffer.
  868. */
  869. ret = ttm_bo_mem_space(bo, placement, &mem,
  870. interruptible, no_wait_gpu);
  871. if (ret)
  872. goto out_unlock;
  873. ret = ttm_bo_handle_move_mem(bo, &mem, false,
  874. interruptible, no_wait_gpu);
  875. out_unlock:
  876. if (ret && mem.mm_node)
  877. ttm_bo_mem_put(bo, &mem);
  878. return ret;
  879. }
  880. bool ttm_bo_mem_compat(struct ttm_placement *placement,
  881. struct ttm_mem_reg *mem,
  882. uint32_t *new_flags)
  883. {
  884. int i;
  885. for (i = 0; i < placement->num_placement; i++) {
  886. const struct ttm_place *heap = &placement->placement[i];
  887. if (mem->mm_node &&
  888. (mem->start < heap->fpfn ||
  889. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  890. continue;
  891. *new_flags = heap->flags;
  892. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  893. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  894. return true;
  895. }
  896. for (i = 0; i < placement->num_busy_placement; i++) {
  897. const struct ttm_place *heap = &placement->busy_placement[i];
  898. if (mem->mm_node &&
  899. (mem->start < heap->fpfn ||
  900. (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn)))
  901. continue;
  902. *new_flags = heap->flags;
  903. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  904. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  905. return true;
  906. }
  907. return false;
  908. }
  909. EXPORT_SYMBOL(ttm_bo_mem_compat);
  910. int ttm_bo_validate(struct ttm_buffer_object *bo,
  911. struct ttm_placement *placement,
  912. bool interruptible,
  913. bool no_wait_gpu)
  914. {
  915. int ret;
  916. uint32_t new_flags;
  917. lockdep_assert_held(&bo->resv->lock.base);
  918. /*
  919. * Check whether we need to move buffer.
  920. */
  921. if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  922. ret = ttm_bo_move_buffer(bo, placement, interruptible,
  923. no_wait_gpu);
  924. if (ret)
  925. return ret;
  926. } else {
  927. /*
  928. * Use the access and other non-mapping-related flag bits from
  929. * the compatible memory placement flags to the active flags
  930. */
  931. ttm_flag_masked(&bo->mem.placement, new_flags,
  932. ~TTM_PL_MASK_MEMTYPE);
  933. }
  934. /*
  935. * We might need to add a TTM.
  936. */
  937. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  938. ret = ttm_bo_add_ttm(bo, true);
  939. if (ret)
  940. return ret;
  941. }
  942. return 0;
  943. }
  944. EXPORT_SYMBOL(ttm_bo_validate);
  945. int ttm_bo_init(struct ttm_bo_device *bdev,
  946. struct ttm_buffer_object *bo,
  947. unsigned long size,
  948. enum ttm_bo_type type,
  949. struct ttm_placement *placement,
  950. uint32_t page_alignment,
  951. bool interruptible,
  952. struct file *persistent_swap_storage,
  953. size_t acc_size,
  954. struct sg_table *sg,
  955. struct reservation_object *resv,
  956. void (*destroy) (struct ttm_buffer_object *))
  957. {
  958. int ret = 0;
  959. unsigned long num_pages;
  960. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  961. bool locked;
  962. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  963. if (ret) {
  964. pr_err("Out of kernel memory\n");
  965. if (destroy)
  966. (*destroy)(bo);
  967. else
  968. kfree(bo);
  969. return -ENOMEM;
  970. }
  971. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  972. if (num_pages == 0) {
  973. pr_err("Illegal buffer object size\n");
  974. if (destroy)
  975. (*destroy)(bo);
  976. else
  977. kfree(bo);
  978. ttm_mem_global_free(mem_glob, acc_size);
  979. return -EINVAL;
  980. }
  981. bo->destroy = destroy;
  982. kref_init(&bo->kref);
  983. kref_init(&bo->list_kref);
  984. atomic_set(&bo->cpu_writers, 0);
  985. INIT_LIST_HEAD(&bo->lru);
  986. INIT_LIST_HEAD(&bo->ddestroy);
  987. INIT_LIST_HEAD(&bo->swap);
  988. INIT_LIST_HEAD(&bo->io_reserve_lru);
  989. mutex_init(&bo->wu_mutex);
  990. bo->bdev = bdev;
  991. bo->glob = bdev->glob;
  992. bo->type = type;
  993. bo->num_pages = num_pages;
  994. bo->mem.size = num_pages << PAGE_SHIFT;
  995. bo->mem.mem_type = TTM_PL_SYSTEM;
  996. bo->mem.num_pages = bo->num_pages;
  997. bo->mem.mm_node = NULL;
  998. bo->mem.page_alignment = page_alignment;
  999. bo->mem.bus.io_reserved_vm = false;
  1000. bo->mem.bus.io_reserved_count = 0;
  1001. bo->moving = NULL;
  1002. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  1003. bo->persistent_swap_storage = persistent_swap_storage;
  1004. bo->acc_size = acc_size;
  1005. bo->sg = sg;
  1006. if (resv) {
  1007. bo->resv = resv;
  1008. lockdep_assert_held(&bo->resv->lock.base);
  1009. } else {
  1010. bo->resv = &bo->ttm_resv;
  1011. reservation_object_init(&bo->ttm_resv);
  1012. }
  1013. atomic_inc(&bo->glob->bo_count);
  1014. drm_vma_node_reset(&bo->vma_node);
  1015. /*
  1016. * For ttm_bo_type_device buffers, allocate
  1017. * address space from the device.
  1018. */
  1019. if (bo->type == ttm_bo_type_device ||
  1020. bo->type == ttm_bo_type_sg)
  1021. ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  1022. bo->mem.num_pages);
  1023. /* passed reservation objects should already be locked,
  1024. * since otherwise lockdep will be angered in radeon.
  1025. */
  1026. if (!resv) {
  1027. locked = ww_mutex_trylock(&bo->resv->lock);
  1028. WARN_ON(!locked);
  1029. }
  1030. if (likely(!ret))
  1031. ret = ttm_bo_validate(bo, placement, interruptible, false);
  1032. if (!resv) {
  1033. ttm_bo_unreserve(bo);
  1034. } else if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  1035. spin_lock(&bo->glob->lru_lock);
  1036. ttm_bo_add_to_lru(bo);
  1037. spin_unlock(&bo->glob->lru_lock);
  1038. }
  1039. if (unlikely(ret))
  1040. ttm_bo_unref(&bo);
  1041. return ret;
  1042. }
  1043. EXPORT_SYMBOL(ttm_bo_init);
  1044. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  1045. unsigned long bo_size,
  1046. unsigned struct_size)
  1047. {
  1048. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1049. size_t size = 0;
  1050. size += ttm_round_pot(struct_size);
  1051. size += ttm_round_pot(npages * sizeof(void *));
  1052. size += ttm_round_pot(sizeof(struct ttm_tt));
  1053. return size;
  1054. }
  1055. EXPORT_SYMBOL(ttm_bo_acc_size);
  1056. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  1057. unsigned long bo_size,
  1058. unsigned struct_size)
  1059. {
  1060. unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
  1061. size_t size = 0;
  1062. size += ttm_round_pot(struct_size);
  1063. size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
  1064. size += ttm_round_pot(sizeof(struct ttm_dma_tt));
  1065. return size;
  1066. }
  1067. EXPORT_SYMBOL(ttm_bo_dma_acc_size);
  1068. int ttm_bo_create(struct ttm_bo_device *bdev,
  1069. unsigned long size,
  1070. enum ttm_bo_type type,
  1071. struct ttm_placement *placement,
  1072. uint32_t page_alignment,
  1073. bool interruptible,
  1074. struct file *persistent_swap_storage,
  1075. struct ttm_buffer_object **p_bo)
  1076. {
  1077. struct ttm_buffer_object *bo;
  1078. size_t acc_size;
  1079. int ret;
  1080. bo = kzalloc(sizeof(*bo), GFP_KERNEL);
  1081. if (unlikely(bo == NULL))
  1082. return -ENOMEM;
  1083. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct ttm_buffer_object));
  1084. ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment,
  1085. interruptible, persistent_swap_storage, acc_size,
  1086. NULL, NULL, NULL);
  1087. if (likely(ret == 0))
  1088. *p_bo = bo;
  1089. return ret;
  1090. }
  1091. EXPORT_SYMBOL(ttm_bo_create);
  1092. static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev,
  1093. unsigned mem_type, bool allow_errors)
  1094. {
  1095. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1096. struct ttm_bo_global *glob = bdev->glob;
  1097. struct dma_fence *fence;
  1098. int ret;
  1099. /*
  1100. * Can't use standard list traversal since we're unlocking.
  1101. */
  1102. spin_lock(&glob->lru_lock);
  1103. while (!list_empty(&man->lru)) {
  1104. spin_unlock(&glob->lru_lock);
  1105. ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false);
  1106. if (ret) {
  1107. if (allow_errors) {
  1108. return ret;
  1109. } else {
  1110. pr_err("Cleanup eviction failed\n");
  1111. }
  1112. }
  1113. spin_lock(&glob->lru_lock);
  1114. }
  1115. spin_unlock(&glob->lru_lock);
  1116. spin_lock(&man->move_lock);
  1117. fence = dma_fence_get(man->move);
  1118. spin_unlock(&man->move_lock);
  1119. if (fence) {
  1120. ret = dma_fence_wait(fence, false);
  1121. dma_fence_put(fence);
  1122. if (ret) {
  1123. if (allow_errors) {
  1124. return ret;
  1125. } else {
  1126. pr_err("Cleanup eviction failed\n");
  1127. }
  1128. }
  1129. }
  1130. return 0;
  1131. }
  1132. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1133. {
  1134. struct ttm_mem_type_manager *man;
  1135. int ret = -EINVAL;
  1136. if (mem_type >= TTM_NUM_MEM_TYPES) {
  1137. pr_err("Illegal memory type %d\n", mem_type);
  1138. return ret;
  1139. }
  1140. man = &bdev->man[mem_type];
  1141. if (!man->has_type) {
  1142. pr_err("Trying to take down uninitialized memory manager type %u\n",
  1143. mem_type);
  1144. return ret;
  1145. }
  1146. dma_fence_put(man->move);
  1147. man->use_type = false;
  1148. man->has_type = false;
  1149. ret = 0;
  1150. if (mem_type > 0) {
  1151. ttm_bo_force_list_clean(bdev, mem_type, false);
  1152. ret = (*man->func->takedown)(man);
  1153. }
  1154. return ret;
  1155. }
  1156. EXPORT_SYMBOL(ttm_bo_clean_mm);
  1157. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
  1158. {
  1159. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  1160. if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) {
  1161. pr_err("Illegal memory manager memory type %u\n", mem_type);
  1162. return -EINVAL;
  1163. }
  1164. if (!man->has_type) {
  1165. pr_err("Memory type %u has not been initialized\n", mem_type);
  1166. return 0;
  1167. }
  1168. return ttm_bo_force_list_clean(bdev, mem_type, true);
  1169. }
  1170. EXPORT_SYMBOL(ttm_bo_evict_mm);
  1171. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  1172. unsigned long p_size)
  1173. {
  1174. int ret = -EINVAL;
  1175. struct ttm_mem_type_manager *man;
  1176. BUG_ON(type >= TTM_NUM_MEM_TYPES);
  1177. man = &bdev->man[type];
  1178. BUG_ON(man->has_type);
  1179. man->io_reserve_fastpath = true;
  1180. man->use_io_reserve_lru = false;
  1181. mutex_init(&man->io_reserve_mutex);
  1182. spin_lock_init(&man->move_lock);
  1183. INIT_LIST_HEAD(&man->io_reserve_lru);
  1184. ret = bdev->driver->init_mem_type(bdev, type, man);
  1185. if (ret)
  1186. return ret;
  1187. man->bdev = bdev;
  1188. ret = 0;
  1189. if (type != TTM_PL_SYSTEM) {
  1190. ret = (*man->func->init)(man, p_size);
  1191. if (ret)
  1192. return ret;
  1193. }
  1194. man->has_type = true;
  1195. man->use_type = true;
  1196. man->size = p_size;
  1197. INIT_LIST_HEAD(&man->lru);
  1198. man->move = NULL;
  1199. return 0;
  1200. }
  1201. EXPORT_SYMBOL(ttm_bo_init_mm);
  1202. static void ttm_bo_global_kobj_release(struct kobject *kobj)
  1203. {
  1204. struct ttm_bo_global *glob =
  1205. container_of(kobj, struct ttm_bo_global, kobj);
  1206. ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink);
  1207. __free_page(glob->dummy_read_page);
  1208. kfree(glob);
  1209. }
  1210. void ttm_bo_global_release(struct drm_global_reference *ref)
  1211. {
  1212. struct ttm_bo_global *glob = ref->object;
  1213. kobject_del(&glob->kobj);
  1214. kobject_put(&glob->kobj);
  1215. }
  1216. EXPORT_SYMBOL(ttm_bo_global_release);
  1217. int ttm_bo_global_init(struct drm_global_reference *ref)
  1218. {
  1219. struct ttm_bo_global_ref *bo_ref =
  1220. container_of(ref, struct ttm_bo_global_ref, ref);
  1221. struct ttm_bo_global *glob = ref->object;
  1222. int ret;
  1223. mutex_init(&glob->device_list_mutex);
  1224. spin_lock_init(&glob->lru_lock);
  1225. glob->mem_glob = bo_ref->mem_glob;
  1226. glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
  1227. if (unlikely(glob->dummy_read_page == NULL)) {
  1228. ret = -ENOMEM;
  1229. goto out_no_drp;
  1230. }
  1231. INIT_LIST_HEAD(&glob->swap_lru);
  1232. INIT_LIST_HEAD(&glob->device_list);
  1233. ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout);
  1234. ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink);
  1235. if (unlikely(ret != 0)) {
  1236. pr_err("Could not register buffer object swapout\n");
  1237. goto out_no_shrink;
  1238. }
  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. pr_err("DRM memory manager type %d is not clean\n",
  1265. i);
  1266. }
  1267. man->has_type = false;
  1268. }
  1269. }
  1270. mutex_lock(&glob->device_list_mutex);
  1271. list_del(&bdev->device_list);
  1272. mutex_unlock(&glob->device_list_mutex);
  1273. cancel_delayed_work_sync(&bdev->wq);
  1274. while (ttm_bo_delayed_delete(bdev, true))
  1275. ;
  1276. spin_lock(&glob->lru_lock);
  1277. if (list_empty(&bdev->ddestroy))
  1278. TTM_DEBUG("Delayed destroy list was clean\n");
  1279. if (list_empty(&bdev->man[0].lru))
  1280. TTM_DEBUG("Swap list was clean\n");
  1281. spin_unlock(&glob->lru_lock);
  1282. drm_vma_offset_manager_destroy(&bdev->vma_manager);
  1283. return ret;
  1284. }
  1285. EXPORT_SYMBOL(ttm_bo_device_release);
  1286. int ttm_bo_device_init(struct ttm_bo_device *bdev,
  1287. struct ttm_bo_global *glob,
  1288. struct ttm_bo_driver *driver,
  1289. struct address_space *mapping,
  1290. uint64_t file_page_offset,
  1291. bool need_dma32)
  1292. {
  1293. int ret = -EINVAL;
  1294. bdev->driver = driver;
  1295. memset(bdev->man, 0, sizeof(bdev->man));
  1296. /*
  1297. * Initialize the system memory buffer type.
  1298. * Other types need to be driver / IOCTL initialized.
  1299. */
  1300. ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0);
  1301. if (unlikely(ret != 0))
  1302. goto out_no_sys;
  1303. drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset,
  1304. 0x10000000);
  1305. INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue);
  1306. INIT_LIST_HEAD(&bdev->ddestroy);
  1307. bdev->dev_mapping = mapping;
  1308. bdev->glob = glob;
  1309. bdev->need_dma32 = need_dma32;
  1310. mutex_lock(&glob->device_list_mutex);
  1311. list_add_tail(&bdev->device_list, &glob->device_list);
  1312. mutex_unlock(&glob->device_list_mutex);
  1313. return 0;
  1314. out_no_sys:
  1315. return ret;
  1316. }
  1317. EXPORT_SYMBOL(ttm_bo_device_init);
  1318. /*
  1319. * buffer object vm functions.
  1320. */
  1321. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1322. {
  1323. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1324. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1325. if (mem->mem_type == TTM_PL_SYSTEM)
  1326. return false;
  1327. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1328. return false;
  1329. if (mem->placement & TTM_PL_FLAG_CACHED)
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1335. {
  1336. struct ttm_bo_device *bdev = bo->bdev;
  1337. drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1338. ttm_mem_io_free_vm(bo);
  1339. }
  1340. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1341. {
  1342. struct ttm_bo_device *bdev = bo->bdev;
  1343. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1344. ttm_mem_io_lock(man, false);
  1345. ttm_bo_unmap_virtual_locked(bo);
  1346. ttm_mem_io_unlock(man);
  1347. }
  1348. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1349. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1350. bool interruptible, bool no_wait)
  1351. {
  1352. long timeout = 15 * HZ;
  1353. if (no_wait) {
  1354. if (reservation_object_test_signaled_rcu(bo->resv, true))
  1355. return 0;
  1356. else
  1357. return -EBUSY;
  1358. }
  1359. timeout = reservation_object_wait_timeout_rcu(bo->resv, true,
  1360. interruptible, timeout);
  1361. if (timeout < 0)
  1362. return timeout;
  1363. if (timeout == 0)
  1364. return -EBUSY;
  1365. reservation_object_add_excl_fence(bo->resv, NULL);
  1366. return 0;
  1367. }
  1368. EXPORT_SYMBOL(ttm_bo_wait);
  1369. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1370. {
  1371. int ret = 0;
  1372. /*
  1373. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1374. */
  1375. ret = ttm_bo_reserve(bo, true, no_wait, NULL);
  1376. if (unlikely(ret != 0))
  1377. return ret;
  1378. ret = ttm_bo_wait(bo, true, no_wait);
  1379. if (likely(ret == 0))
  1380. atomic_inc(&bo->cpu_writers);
  1381. ttm_bo_unreserve(bo);
  1382. return ret;
  1383. }
  1384. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1385. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1386. {
  1387. atomic_dec(&bo->cpu_writers);
  1388. }
  1389. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1390. /**
  1391. * A buffer object shrink method that tries to swap out the first
  1392. * buffer object on the bo_global::swap_lru list.
  1393. */
  1394. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1395. {
  1396. struct ttm_bo_global *glob =
  1397. container_of(shrink, struct ttm_bo_global, shrink);
  1398. struct ttm_buffer_object *bo;
  1399. int ret = -EBUSY;
  1400. int put_count;
  1401. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1402. spin_lock(&glob->lru_lock);
  1403. list_for_each_entry(bo, &glob->swap_lru, swap) {
  1404. ret = __ttm_bo_reserve(bo, false, true, NULL);
  1405. if (!ret)
  1406. break;
  1407. }
  1408. if (ret) {
  1409. spin_unlock(&glob->lru_lock);
  1410. return ret;
  1411. }
  1412. kref_get(&bo->list_kref);
  1413. if (!list_empty(&bo->ddestroy)) {
  1414. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1415. kref_put(&bo->list_kref, ttm_bo_release_list);
  1416. return ret;
  1417. }
  1418. put_count = ttm_bo_del_from_lru(bo);
  1419. spin_unlock(&glob->lru_lock);
  1420. ttm_bo_list_ref_sub(bo, put_count, true);
  1421. /**
  1422. * Move to system cached
  1423. */
  1424. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1425. struct ttm_mem_reg evict_mem;
  1426. evict_mem = bo->mem;
  1427. evict_mem.mm_node = NULL;
  1428. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1429. evict_mem.mem_type = TTM_PL_SYSTEM;
  1430. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1431. false, false);
  1432. if (unlikely(ret != 0))
  1433. goto out;
  1434. }
  1435. /**
  1436. * Make sure BO is idle.
  1437. */
  1438. ret = ttm_bo_wait(bo, false, false);
  1439. if (unlikely(ret != 0))
  1440. goto out;
  1441. ttm_bo_unmap_virtual(bo);
  1442. /**
  1443. * Swap out. Buffer will be swapped in again as soon as
  1444. * anyone tries to access a ttm page.
  1445. */
  1446. if (bo->bdev->driver->swap_notify)
  1447. bo->bdev->driver->swap_notify(bo);
  1448. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1449. out:
  1450. /**
  1451. *
  1452. * Unreserve without putting on LRU to avoid swapping out an
  1453. * already swapped buffer.
  1454. */
  1455. __ttm_bo_unreserve(bo);
  1456. kref_put(&bo->list_kref, ttm_bo_release_list);
  1457. return ret;
  1458. }
  1459. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1460. {
  1461. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1462. ;
  1463. }
  1464. EXPORT_SYMBOL(ttm_bo_swapout_all);
  1465. /**
  1466. * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
  1467. * unreserved
  1468. *
  1469. * @bo: Pointer to buffer
  1470. */
  1471. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1472. {
  1473. int ret;
  1474. /*
  1475. * In the absense of a wait_unlocked API,
  1476. * Use the bo::wu_mutex to avoid triggering livelocks due to
  1477. * concurrent use of this function. Note that this use of
  1478. * bo::wu_mutex can go away if we change locking order to
  1479. * mmap_sem -> bo::reserve.
  1480. */
  1481. ret = mutex_lock_interruptible(&bo->wu_mutex);
  1482. if (unlikely(ret != 0))
  1483. return -ERESTARTSYS;
  1484. if (!ww_mutex_is_locked(&bo->resv->lock))
  1485. goto out_unlock;
  1486. ret = __ttm_bo_reserve(bo, true, false, NULL);
  1487. if (unlikely(ret != 0))
  1488. goto out_unlock;
  1489. __ttm_bo_unreserve(bo);
  1490. out_unlock:
  1491. mutex_unlock(&bo->wu_mutex);
  1492. return ret;
  1493. }