ttm_bo.c 42 KB

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