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