ttm_bo.c 43 KB

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