ttm_bo.c 43 KB

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