ttm_bo.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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. #define TTM_ASSERT_LOCKED(param)
  42. #define TTM_DEBUG(fmt, arg...)
  43. #define TTM_BO_HASH_ORDER 13
  44. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink);
  45. static void ttm_bo_global_kobj_release(struct kobject *kobj);
  46. static struct attribute ttm_bo_count = {
  47. .name = "bo_count",
  48. .mode = S_IRUGO
  49. };
  50. static inline int ttm_mem_type_from_flags(uint32_t flags, uint32_t *mem_type)
  51. {
  52. int i;
  53. for (i = 0; i <= TTM_PL_PRIV5; i++)
  54. if (flags & (1 << i)) {
  55. *mem_type = i;
  56. return 0;
  57. }
  58. return -EINVAL;
  59. }
  60. static void ttm_mem_type_debug(struct ttm_bo_device *bdev, int mem_type)
  61. {
  62. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  63. pr_err(" has_type: %d\n", man->has_type);
  64. pr_err(" use_type: %d\n", man->use_type);
  65. pr_err(" flags: 0x%08X\n", man->flags);
  66. pr_err(" gpu_offset: 0x%08lX\n", man->gpu_offset);
  67. pr_err(" size: %llu\n", man->size);
  68. pr_err(" available_caching: 0x%08X\n", man->available_caching);
  69. pr_err(" default_caching: 0x%08X\n", man->default_caching);
  70. if (mem_type != TTM_PL_SYSTEM)
  71. (*man->func->debug)(man, TTM_PFX);
  72. }
  73. static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo,
  74. struct ttm_placement *placement)
  75. {
  76. int i, ret, mem_type;
  77. pr_err("No space for %p (%lu pages, %luK, %luM)\n",
  78. bo, bo->mem.num_pages, bo->mem.size >> 10,
  79. bo->mem.size >> 20);
  80. for (i = 0; i < placement->num_placement; i++) {
  81. ret = ttm_mem_type_from_flags(placement->placement[i],
  82. &mem_type);
  83. if (ret)
  84. return;
  85. pr_err(" placement[%d]=0x%08X (%d)\n",
  86. i, placement->placement[i], mem_type);
  87. ttm_mem_type_debug(bo->bdev, mem_type);
  88. }
  89. }
  90. static ssize_t ttm_bo_global_show(struct kobject *kobj,
  91. struct attribute *attr,
  92. char *buffer)
  93. {
  94. struct ttm_bo_global *glob =
  95. container_of(kobj, struct ttm_bo_global, kobj);
  96. return snprintf(buffer, PAGE_SIZE, "%lu\n",
  97. (unsigned long) atomic_read(&glob->bo_count));
  98. }
  99. static struct attribute *ttm_bo_global_attrs[] = {
  100. &ttm_bo_count,
  101. NULL
  102. };
  103. static const struct sysfs_ops ttm_bo_global_ops = {
  104. .show = &ttm_bo_global_show
  105. };
  106. static struct kobj_type ttm_bo_glob_kobj_type = {
  107. .release = &ttm_bo_global_kobj_release,
  108. .sysfs_ops = &ttm_bo_global_ops,
  109. .default_attrs = ttm_bo_global_attrs
  110. };
  111. static inline uint32_t ttm_bo_type_flags(unsigned type)
  112. {
  113. return 1 << (type);
  114. }
  115. static void ttm_bo_release_list(struct kref *list_kref)
  116. {
  117. struct ttm_buffer_object *bo =
  118. container_of(list_kref, struct ttm_buffer_object, list_kref);
  119. struct ttm_bo_device *bdev = bo->bdev;
  120. size_t acc_size = bo->acc_size;
  121. BUG_ON(atomic_read(&bo->list_kref.refcount));
  122. BUG_ON(atomic_read(&bo->kref.refcount));
  123. BUG_ON(atomic_read(&bo->cpu_writers));
  124. BUG_ON(bo->sync_obj != NULL);
  125. BUG_ON(bo->mem.mm_node != NULL);
  126. BUG_ON(!list_empty(&bo->lru));
  127. BUG_ON(!list_empty(&bo->ddestroy));
  128. if (bo->ttm)
  129. ttm_tt_destroy(bo->ttm);
  130. atomic_dec(&bo->glob->bo_count);
  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);
  150. kref_get(&bo->list_kref);
  151. if (bo->ttm != NULL) {
  152. list_add_tail(&bo->swap, &bo->glob->swap_lru);
  153. kref_get(&bo->list_kref);
  154. }
  155. }
  156. }
  157. EXPORT_SYMBOL(ttm_bo_add_to_lru);
  158. int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
  159. {
  160. int put_count = 0;
  161. if (!list_empty(&bo->swap)) {
  162. list_del_init(&bo->swap);
  163. ++put_count;
  164. }
  165. if (!list_empty(&bo->lru)) {
  166. list_del_init(&bo->lru);
  167. ++put_count;
  168. }
  169. /*
  170. * TODO: Add a driver hook to delete from
  171. * driver-specific LRU's here.
  172. */
  173. return put_count;
  174. }
  175. static void ttm_bo_ref_bug(struct kref *list_kref)
  176. {
  177. BUG();
  178. }
  179. void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
  180. bool never_free)
  181. {
  182. kref_sub(&bo->list_kref, count,
  183. (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
  184. }
  185. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
  186. {
  187. int put_count;
  188. spin_lock(&bo->glob->lru_lock);
  189. put_count = ttm_bo_del_from_lru(bo);
  190. spin_unlock(&bo->glob->lru_lock);
  191. ttm_bo_list_ref_sub(bo, put_count, true);
  192. }
  193. EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
  194. /*
  195. * Call bo->mutex locked.
  196. */
  197. static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
  198. {
  199. struct ttm_bo_device *bdev = bo->bdev;
  200. struct ttm_bo_global *glob = bo->glob;
  201. int ret = 0;
  202. uint32_t page_flags = 0;
  203. TTM_ASSERT_LOCKED(&bo->mutex);
  204. bo->ttm = NULL;
  205. if (bdev->need_dma32)
  206. page_flags |= TTM_PAGE_FLAG_DMA32;
  207. switch (bo->type) {
  208. case ttm_bo_type_device:
  209. if (zero_alloc)
  210. page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
  211. case ttm_bo_type_kernel:
  212. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  213. page_flags, glob->dummy_read_page);
  214. if (unlikely(bo->ttm == NULL))
  215. ret = -ENOMEM;
  216. break;
  217. case ttm_bo_type_sg:
  218. bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
  219. page_flags | TTM_PAGE_FLAG_SG,
  220. glob->dummy_read_page);
  221. if (unlikely(bo->ttm == NULL)) {
  222. ret = -ENOMEM;
  223. break;
  224. }
  225. bo->ttm->sg = bo->sg;
  226. break;
  227. default:
  228. pr_err("Illegal buffer object type\n");
  229. ret = -EINVAL;
  230. break;
  231. }
  232. return ret;
  233. }
  234. static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo,
  235. struct ttm_mem_reg *mem,
  236. bool evict, bool interruptible,
  237. bool no_wait_gpu)
  238. {
  239. struct ttm_bo_device *bdev = bo->bdev;
  240. bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem);
  241. bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem);
  242. struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type];
  243. struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type];
  244. int ret = 0;
  245. if (old_is_pci || new_is_pci ||
  246. ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) {
  247. ret = ttm_mem_io_lock(old_man, true);
  248. if (unlikely(ret != 0))
  249. goto out_err;
  250. ttm_bo_unmap_virtual_locked(bo);
  251. ttm_mem_io_unlock(old_man);
  252. }
  253. /*
  254. * Create and bind a ttm if required.
  255. */
  256. if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  257. if (bo->ttm == NULL) {
  258. bool zero = !(old_man->flags & TTM_MEMTYPE_FLAG_FIXED);
  259. ret = ttm_bo_add_ttm(bo, zero);
  260. if (ret)
  261. goto out_err;
  262. }
  263. ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement);
  264. if (ret)
  265. goto out_err;
  266. if (mem->mem_type != TTM_PL_SYSTEM) {
  267. ret = ttm_tt_bind(bo->ttm, mem);
  268. if (ret)
  269. goto out_err;
  270. }
  271. if (bo->mem.mem_type == TTM_PL_SYSTEM) {
  272. if (bdev->driver->move_notify)
  273. bdev->driver->move_notify(bo, mem);
  274. bo->mem = *mem;
  275. mem->mm_node = NULL;
  276. goto moved;
  277. }
  278. }
  279. if (bdev->driver->move_notify)
  280. bdev->driver->move_notify(bo, mem);
  281. if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) &&
  282. !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED))
  283. ret = ttm_bo_move_ttm(bo, evict, no_wait_gpu, mem);
  284. else if (bdev->driver->move)
  285. ret = bdev->driver->move(bo, evict, interruptible,
  286. no_wait_gpu, mem);
  287. else
  288. ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, mem);
  289. if (ret) {
  290. if (bdev->driver->move_notify) {
  291. struct ttm_mem_reg tmp_mem = *mem;
  292. *mem = bo->mem;
  293. bo->mem = tmp_mem;
  294. bdev->driver->move_notify(bo, mem);
  295. bo->mem = *mem;
  296. *mem = tmp_mem;
  297. }
  298. goto out_err;
  299. }
  300. moved:
  301. if (bo->evicted) {
  302. if (bdev->driver->invalidate_caches) {
  303. ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement);
  304. if (ret)
  305. pr_err("Can not flush read caches\n");
  306. }
  307. bo->evicted = false;
  308. }
  309. if (bo->mem.mm_node) {
  310. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  311. bdev->man[bo->mem.mem_type].gpu_offset;
  312. bo->cur_placement = bo->mem.placement;
  313. } else
  314. bo->offset = 0;
  315. return 0;
  316. out_err:
  317. new_man = &bdev->man[bo->mem.mem_type];
  318. if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) {
  319. ttm_tt_unbind(bo->ttm);
  320. ttm_tt_destroy(bo->ttm);
  321. bo->ttm = NULL;
  322. }
  323. return ret;
  324. }
  325. /**
  326. * Call bo::reserved.
  327. * Will release GPU memory type usage on destruction.
  328. * This is the place to put in driver specific hooks to release
  329. * driver private resources.
  330. * Will release the bo::reserved lock.
  331. */
  332. static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
  333. {
  334. if (bo->bdev->driver->move_notify)
  335. bo->bdev->driver->move_notify(bo, NULL);
  336. if (bo->ttm) {
  337. ttm_tt_unbind(bo->ttm);
  338. ttm_tt_destroy(bo->ttm);
  339. bo->ttm = NULL;
  340. }
  341. ttm_bo_mem_put(bo, &bo->mem);
  342. ww_mutex_unlock (&bo->resv->lock);
  343. }
  344. static void ttm_bo_cleanup_refs_or_queue(struct ttm_buffer_object *bo)
  345. {
  346. struct ttm_bo_device *bdev = bo->bdev;
  347. struct ttm_bo_global *glob = bo->glob;
  348. struct ttm_bo_driver *driver = bdev->driver;
  349. void *sync_obj = NULL;
  350. int put_count;
  351. int ret;
  352. spin_lock(&glob->lru_lock);
  353. ret = __ttm_bo_reserve(bo, false, true, false, 0);
  354. spin_lock(&bdev->fence_lock);
  355. (void) ttm_bo_wait(bo, false, false, true);
  356. if (!ret && !bo->sync_obj) {
  357. spin_unlock(&bdev->fence_lock);
  358. put_count = ttm_bo_del_from_lru(bo);
  359. spin_unlock(&glob->lru_lock);
  360. ttm_bo_cleanup_memtype_use(bo);
  361. ttm_bo_list_ref_sub(bo, put_count, true);
  362. return;
  363. }
  364. if (bo->sync_obj)
  365. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  366. spin_unlock(&bdev->fence_lock);
  367. if (!ret) {
  368. /*
  369. * Make NO_EVICT bos immediately available to
  370. * shrinkers, now that they are queued for
  371. * destruction.
  372. */
  373. if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) {
  374. bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT;
  375. ttm_bo_add_to_lru(bo);
  376. }
  377. __ttm_bo_unreserve(bo);
  378. }
  379. kref_get(&bo->list_kref);
  380. list_add_tail(&bo->ddestroy, &bdev->ddestroy);
  381. spin_unlock(&glob->lru_lock);
  382. if (sync_obj) {
  383. driver->sync_obj_flush(sync_obj);
  384. driver->sync_obj_unref(&sync_obj);
  385. }
  386. schedule_delayed_work(&bdev->wq,
  387. ((HZ / 100) < 1) ? 1 : HZ / 100);
  388. }
  389. /**
  390. * function ttm_bo_cleanup_refs_and_unlock
  391. * If bo idle, remove from delayed- and lru lists, and unref.
  392. * If not idle, do nothing.
  393. *
  394. * Must be called with lru_lock and reservation held, this function
  395. * will drop both before returning.
  396. *
  397. * @interruptible Any sleeps should occur interruptibly.
  398. * @no_wait_gpu Never wait for gpu. Return -EBUSY instead.
  399. */
  400. static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo,
  401. bool interruptible,
  402. bool no_wait_gpu)
  403. {
  404. struct ttm_bo_device *bdev = bo->bdev;
  405. struct ttm_bo_driver *driver = bdev->driver;
  406. struct ttm_bo_global *glob = bo->glob;
  407. int put_count;
  408. int ret;
  409. spin_lock(&bdev->fence_lock);
  410. ret = ttm_bo_wait(bo, false, false, true);
  411. if (ret && !no_wait_gpu) {
  412. void *sync_obj;
  413. /*
  414. * Take a reference to the fence and unreserve,
  415. * at this point the buffer should be dead, so
  416. * no new sync objects can be attached.
  417. */
  418. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  419. spin_unlock(&bdev->fence_lock);
  420. __ttm_bo_unreserve(bo);
  421. spin_unlock(&glob->lru_lock);
  422. ret = driver->sync_obj_wait(sync_obj, false, interruptible);
  423. driver->sync_obj_unref(&sync_obj);
  424. if (ret)
  425. return ret;
  426. /*
  427. * remove sync_obj with ttm_bo_wait, the wait should be
  428. * finished, and no new wait object should have been added.
  429. */
  430. spin_lock(&bdev->fence_lock);
  431. ret = ttm_bo_wait(bo, false, false, true);
  432. WARN_ON(ret);
  433. spin_unlock(&bdev->fence_lock);
  434. if (ret)
  435. return ret;
  436. spin_lock(&glob->lru_lock);
  437. ret = __ttm_bo_reserve(bo, false, true, false, 0);
  438. /*
  439. * We raced, and lost, someone else holds the reservation now,
  440. * and is probably busy in ttm_bo_cleanup_memtype_use.
  441. *
  442. * Even if it's not the case, because we finished waiting any
  443. * delayed destruction would succeed, so just return success
  444. * here.
  445. */
  446. if (ret) {
  447. spin_unlock(&glob->lru_lock);
  448. return 0;
  449. }
  450. } else
  451. spin_unlock(&bdev->fence_lock);
  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, false, 0);
  488. if (remove_all && ret) {
  489. spin_unlock(&glob->lru_lock);
  490. ret = __ttm_bo_reserve(entry, false, false,
  491. false, 0);
  492. spin_lock(&glob->lru_lock);
  493. }
  494. if (!ret)
  495. ret = ttm_bo_cleanup_refs_and_unlock(entry, false,
  496. !remove_all);
  497. else
  498. spin_unlock(&glob->lru_lock);
  499. kref_put(&entry->list_kref, ttm_bo_release_list);
  500. entry = nentry;
  501. if (ret || !entry)
  502. goto out;
  503. spin_lock(&glob->lru_lock);
  504. if (list_empty(&entry->ddestroy))
  505. break;
  506. }
  507. out_unlock:
  508. spin_unlock(&glob->lru_lock);
  509. out:
  510. if (entry)
  511. kref_put(&entry->list_kref, ttm_bo_release_list);
  512. return ret;
  513. }
  514. static void ttm_bo_delayed_workqueue(struct work_struct *work)
  515. {
  516. struct ttm_bo_device *bdev =
  517. container_of(work, struct ttm_bo_device, wq.work);
  518. if (ttm_bo_delayed_delete(bdev, false)) {
  519. schedule_delayed_work(&bdev->wq,
  520. ((HZ / 100) < 1) ? 1 : HZ / 100);
  521. }
  522. }
  523. static void ttm_bo_release(struct kref *kref)
  524. {
  525. struct ttm_buffer_object *bo =
  526. container_of(kref, struct ttm_buffer_object, kref);
  527. struct ttm_bo_device *bdev = bo->bdev;
  528. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  529. drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node);
  530. ttm_mem_io_lock(man, false);
  531. ttm_mem_io_free_vm(bo);
  532. ttm_mem_io_unlock(man);
  533. ttm_bo_cleanup_refs_or_queue(bo);
  534. kref_put(&bo->list_kref, ttm_bo_release_list);
  535. }
  536. void ttm_bo_unref(struct ttm_buffer_object **p_bo)
  537. {
  538. struct ttm_buffer_object *bo = *p_bo;
  539. *p_bo = NULL;
  540. kref_put(&bo->kref, ttm_bo_release);
  541. }
  542. EXPORT_SYMBOL(ttm_bo_unref);
  543. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev)
  544. {
  545. return cancel_delayed_work_sync(&bdev->wq);
  546. }
  547. EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue);
  548. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched)
  549. {
  550. if (resched)
  551. schedule_delayed_work(&bdev->wq,
  552. ((HZ / 100) < 1) ? 1 : HZ / 100);
  553. }
  554. EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue);
  555. static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible,
  556. bool no_wait_gpu)
  557. {
  558. struct ttm_bo_device *bdev = bo->bdev;
  559. struct ttm_mem_reg evict_mem;
  560. struct ttm_placement placement;
  561. int ret = 0;
  562. spin_lock(&bdev->fence_lock);
  563. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  564. spin_unlock(&bdev->fence_lock);
  565. if (unlikely(ret != 0)) {
  566. if (ret != -ERESTARTSYS) {
  567. pr_err("Failed to expire sync object before buffer eviction\n");
  568. }
  569. goto out;
  570. }
  571. lockdep_assert_held(&bo->resv->lock.base);
  572. evict_mem = bo->mem;
  573. evict_mem.mm_node = NULL;
  574. evict_mem.bus.io_reserved_vm = false;
  575. evict_mem.bus.io_reserved_count = 0;
  576. placement.fpfn = 0;
  577. placement.lpfn = 0;
  578. placement.num_placement = 0;
  579. placement.num_busy_placement = 0;
  580. bdev->driver->evict_flags(bo, &placement);
  581. ret = ttm_bo_mem_space(bo, &placement, &evict_mem, interruptible,
  582. no_wait_gpu);
  583. if (ret) {
  584. if (ret != -ERESTARTSYS) {
  585. pr_err("Failed to find memory space for buffer 0x%p eviction\n",
  586. bo);
  587. ttm_bo_mem_space_debug(bo, &placement);
  588. }
  589. goto out;
  590. }
  591. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible,
  592. no_wait_gpu);
  593. if (ret) {
  594. if (ret != -ERESTARTSYS)
  595. pr_err("Buffer eviction failed\n");
  596. ttm_bo_mem_put(bo, &evict_mem);
  597. goto out;
  598. }
  599. bo->evicted = true;
  600. out:
  601. return ret;
  602. }
  603. static int ttm_mem_evict_first(struct ttm_bo_device *bdev,
  604. uint32_t mem_type,
  605. bool interruptible,
  606. bool no_wait_gpu)
  607. {
  608. struct ttm_bo_global *glob = bdev->glob;
  609. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  610. struct ttm_buffer_object *bo;
  611. int ret = -EBUSY, put_count;
  612. spin_lock(&glob->lru_lock);
  613. list_for_each_entry(bo, &man->lru, lru) {
  614. ret = __ttm_bo_reserve(bo, false, true, false, 0);
  615. if (!ret)
  616. break;
  617. }
  618. if (ret) {
  619. spin_unlock(&glob->lru_lock);
  620. return ret;
  621. }
  622. kref_get(&bo->list_kref);
  623. if (!list_empty(&bo->ddestroy)) {
  624. ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible,
  625. no_wait_gpu);
  626. kref_put(&bo->list_kref, ttm_bo_release_list);
  627. return ret;
  628. }
  629. put_count = ttm_bo_del_from_lru(bo);
  630. spin_unlock(&glob->lru_lock);
  631. BUG_ON(ret != 0);
  632. ttm_bo_list_ref_sub(bo, put_count, true);
  633. ret = ttm_bo_evict(bo, interruptible, no_wait_gpu);
  634. ttm_bo_unreserve(bo);
  635. kref_put(&bo->list_kref, ttm_bo_release_list);
  636. return ret;
  637. }
  638. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem)
  639. {
  640. struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type];
  641. if (mem->mm_node)
  642. (*man->func->put_node)(man, mem);
  643. }
  644. EXPORT_SYMBOL(ttm_bo_mem_put);
  645. /**
  646. * Repeatedly evict memory from the LRU for @mem_type until we create enough
  647. * space, or we've evicted everything and there isn't enough space.
  648. */
  649. static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
  650. uint32_t mem_type,
  651. struct ttm_placement *placement,
  652. struct ttm_mem_reg *mem,
  653. bool interruptible,
  654. bool no_wait_gpu)
  655. {
  656. struct ttm_bo_device *bdev = bo->bdev;
  657. struct ttm_mem_type_manager *man = &bdev->man[mem_type];
  658. int ret;
  659. do {
  660. ret = (*man->func->get_node)(man, bo, placement, mem);
  661. if (unlikely(ret != 0))
  662. return ret;
  663. if (mem->mm_node)
  664. break;
  665. ret = ttm_mem_evict_first(bdev, mem_type,
  666. interruptible, no_wait_gpu);
  667. if (unlikely(ret != 0))
  668. return ret;
  669. } while (1);
  670. if (mem->mm_node == NULL)
  671. return -ENOMEM;
  672. mem->mem_type = mem_type;
  673. return 0;
  674. }
  675. static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
  676. uint32_t cur_placement,
  677. uint32_t proposed_placement)
  678. {
  679. uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING;
  680. uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING;
  681. /**
  682. * Keep current caching if possible.
  683. */
  684. if ((cur_placement & caching) != 0)
  685. result |= (cur_placement & caching);
  686. else if ((man->default_caching & caching) != 0)
  687. result |= man->default_caching;
  688. else if ((TTM_PL_FLAG_CACHED & caching) != 0)
  689. result |= TTM_PL_FLAG_CACHED;
  690. else if ((TTM_PL_FLAG_WC & caching) != 0)
  691. result |= TTM_PL_FLAG_WC;
  692. else if ((TTM_PL_FLAG_UNCACHED & caching) != 0)
  693. result |= TTM_PL_FLAG_UNCACHED;
  694. return result;
  695. }
  696. static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
  697. uint32_t mem_type,
  698. uint32_t proposed_placement,
  699. uint32_t *masked_placement)
  700. {
  701. uint32_t cur_flags = ttm_bo_type_flags(mem_type);
  702. if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
  703. return false;
  704. if ((proposed_placement & man->available_caching) == 0)
  705. return false;
  706. cur_flags |= (proposed_placement & man->available_caching);
  707. *masked_placement = cur_flags;
  708. return true;
  709. }
  710. /**
  711. * Creates space for memory region @mem according to its type.
  712. *
  713. * This function first searches for free space in compatible memory types in
  714. * the priority order defined by the driver. If free space isn't found, then
  715. * ttm_bo_mem_force_space is attempted in priority order to evict and find
  716. * space.
  717. */
  718. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  719. struct ttm_placement *placement,
  720. struct ttm_mem_reg *mem,
  721. bool interruptible,
  722. bool no_wait_gpu)
  723. {
  724. struct ttm_bo_device *bdev = bo->bdev;
  725. struct ttm_mem_type_manager *man;
  726. uint32_t mem_type = TTM_PL_SYSTEM;
  727. uint32_t cur_flags = 0;
  728. bool type_found = false;
  729. bool type_ok = false;
  730. bool has_erestartsys = false;
  731. int i, ret;
  732. mem->mm_node = NULL;
  733. for (i = 0; i < placement->num_placement; ++i) {
  734. ret = ttm_mem_type_from_flags(placement->placement[i],
  735. &mem_type);
  736. if (ret)
  737. return ret;
  738. man = &bdev->man[mem_type];
  739. type_ok = ttm_bo_mt_compatible(man,
  740. mem_type,
  741. placement->placement[i],
  742. &cur_flags);
  743. if (!type_ok)
  744. continue;
  745. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  746. cur_flags);
  747. /*
  748. * Use the access and other non-mapping-related flag bits from
  749. * the memory placement flags to the current flags
  750. */
  751. ttm_flag_masked(&cur_flags, placement->placement[i],
  752. ~TTM_PL_MASK_MEMTYPE);
  753. if (mem_type == TTM_PL_SYSTEM)
  754. break;
  755. if (man->has_type && man->use_type) {
  756. type_found = true;
  757. ret = (*man->func->get_node)(man, bo, placement, mem);
  758. if (unlikely(ret))
  759. return ret;
  760. }
  761. if (mem->mm_node)
  762. break;
  763. }
  764. if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) {
  765. mem->mem_type = mem_type;
  766. mem->placement = cur_flags;
  767. return 0;
  768. }
  769. if (!type_found)
  770. return -EINVAL;
  771. for (i = 0; i < placement->num_busy_placement; ++i) {
  772. ret = ttm_mem_type_from_flags(placement->busy_placement[i],
  773. &mem_type);
  774. if (ret)
  775. return ret;
  776. man = &bdev->man[mem_type];
  777. if (!man->has_type)
  778. continue;
  779. if (!ttm_bo_mt_compatible(man,
  780. mem_type,
  781. placement->busy_placement[i],
  782. &cur_flags))
  783. continue;
  784. cur_flags = ttm_bo_select_caching(man, bo->mem.placement,
  785. cur_flags);
  786. /*
  787. * Use the access and other non-mapping-related flag bits from
  788. * the memory placement flags to the current flags
  789. */
  790. ttm_flag_masked(&cur_flags, placement->busy_placement[i],
  791. ~TTM_PL_MASK_MEMTYPE);
  792. if (mem_type == TTM_PL_SYSTEM) {
  793. mem->mem_type = mem_type;
  794. mem->placement = cur_flags;
  795. mem->mm_node = NULL;
  796. return 0;
  797. }
  798. ret = ttm_bo_mem_force_space(bo, mem_type, placement, mem,
  799. interruptible, no_wait_gpu);
  800. if (ret == 0 && mem->mm_node) {
  801. mem->placement = cur_flags;
  802. return 0;
  803. }
  804. if (ret == -ERESTARTSYS)
  805. has_erestartsys = true;
  806. }
  807. ret = (has_erestartsys) ? -ERESTARTSYS : -ENOMEM;
  808. return ret;
  809. }
  810. EXPORT_SYMBOL(ttm_bo_mem_space);
  811. static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
  812. struct ttm_placement *placement,
  813. bool interruptible,
  814. bool no_wait_gpu)
  815. {
  816. int ret = 0;
  817. struct ttm_mem_reg mem;
  818. struct ttm_bo_device *bdev = bo->bdev;
  819. lockdep_assert_held(&bo->resv->lock.base);
  820. /*
  821. * FIXME: It's possible to pipeline buffer moves.
  822. * Have the driver move function wait for idle when necessary,
  823. * instead of doing it here.
  824. */
  825. spin_lock(&bdev->fence_lock);
  826. ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu);
  827. spin_unlock(&bdev->fence_lock);
  828. if (ret)
  829. return ret;
  830. mem.num_pages = bo->num_pages;
  831. mem.size = mem.num_pages << PAGE_SHIFT;
  832. mem.page_alignment = bo->mem.page_alignment;
  833. mem.bus.io_reserved_vm = false;
  834. mem.bus.io_reserved_count = 0;
  835. /*
  836. * Determine where to move the buffer.
  837. */
  838. ret = ttm_bo_mem_space(bo, placement, &mem,
  839. interruptible, no_wait_gpu);
  840. if (ret)
  841. goto out_unlock;
  842. ret = ttm_bo_handle_move_mem(bo, &mem, false,
  843. interruptible, no_wait_gpu);
  844. out_unlock:
  845. if (ret && mem.mm_node)
  846. ttm_bo_mem_put(bo, &mem);
  847. return ret;
  848. }
  849. static bool ttm_bo_mem_compat(struct ttm_placement *placement,
  850. struct ttm_mem_reg *mem,
  851. uint32_t *new_flags)
  852. {
  853. int i;
  854. if (mem->mm_node && placement->lpfn != 0 &&
  855. (mem->start < placement->fpfn ||
  856. mem->start + mem->num_pages > placement->lpfn))
  857. return false;
  858. for (i = 0; i < placement->num_placement; i++) {
  859. *new_flags = placement->placement[i];
  860. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  861. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  862. return true;
  863. }
  864. for (i = 0; i < placement->num_busy_placement; i++) {
  865. *new_flags = placement->busy_placement[i];
  866. if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) &&
  867. (*new_flags & mem->placement & TTM_PL_MASK_MEM))
  868. return true;
  869. }
  870. return false;
  871. }
  872. int ttm_bo_validate(struct ttm_buffer_object *bo,
  873. struct ttm_placement *placement,
  874. bool interruptible,
  875. bool no_wait_gpu)
  876. {
  877. int ret;
  878. uint32_t new_flags;
  879. lockdep_assert_held(&bo->resv->lock.base);
  880. /* Check that range is valid */
  881. if (placement->lpfn || placement->fpfn)
  882. if (placement->fpfn > placement->lpfn ||
  883. (placement->lpfn - placement->fpfn) < bo->num_pages)
  884. return -EINVAL;
  885. /*
  886. * Check whether we need to move buffer.
  887. */
  888. if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) {
  889. ret = ttm_bo_move_buffer(bo, placement, interruptible,
  890. no_wait_gpu);
  891. if (ret)
  892. return ret;
  893. } else {
  894. /*
  895. * Use the access and other non-mapping-related flag bits from
  896. * the compatible memory placement flags to the active flags
  897. */
  898. ttm_flag_masked(&bo->mem.placement, new_flags,
  899. ~TTM_PL_MASK_MEMTYPE);
  900. }
  901. /*
  902. * We might need to add a TTM.
  903. */
  904. if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  905. ret = ttm_bo_add_ttm(bo, true);
  906. if (ret)
  907. return ret;
  908. }
  909. return 0;
  910. }
  911. EXPORT_SYMBOL(ttm_bo_validate);
  912. int ttm_bo_check_placement(struct ttm_buffer_object *bo,
  913. struct ttm_placement *placement)
  914. {
  915. BUG_ON((placement->fpfn || placement->lpfn) &&
  916. (bo->mem.num_pages > (placement->lpfn - placement->fpfn)));
  917. return 0;
  918. }
  919. int ttm_bo_init(struct ttm_bo_device *bdev,
  920. struct ttm_buffer_object *bo,
  921. unsigned long size,
  922. enum ttm_bo_type type,
  923. struct ttm_placement *placement,
  924. uint32_t page_alignment,
  925. bool interruptible,
  926. struct file *persistent_swap_storage,
  927. size_t acc_size,
  928. struct sg_table *sg,
  929. void (*destroy) (struct ttm_buffer_object *))
  930. {
  931. int ret = 0;
  932. unsigned long num_pages;
  933. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  934. bool locked;
  935. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  936. if (ret) {
  937. pr_err("Out of kernel memory\n");
  938. if (destroy)
  939. (*destroy)(bo);
  940. else
  941. kfree(bo);
  942. return -ENOMEM;
  943. }
  944. num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  945. if (num_pages == 0) {
  946. pr_err("Illegal buffer object size\n");
  947. if (destroy)
  948. (*destroy)(bo);
  949. else
  950. kfree(bo);
  951. ttm_mem_global_free(mem_glob, acc_size);
  952. return -EINVAL;
  953. }
  954. bo->destroy = destroy;
  955. kref_init(&bo->kref);
  956. kref_init(&bo->list_kref);
  957. atomic_set(&bo->cpu_writers, 0);
  958. INIT_LIST_HEAD(&bo->lru);
  959. INIT_LIST_HEAD(&bo->ddestroy);
  960. INIT_LIST_HEAD(&bo->swap);
  961. INIT_LIST_HEAD(&bo->io_reserve_lru);
  962. mutex_init(&bo->wu_mutex);
  963. bo->bdev = bdev;
  964. bo->glob = bdev->glob;
  965. bo->type = type;
  966. bo->num_pages = num_pages;
  967. bo->mem.size = num_pages << PAGE_SHIFT;
  968. bo->mem.mem_type = TTM_PL_SYSTEM;
  969. bo->mem.num_pages = bo->num_pages;
  970. bo->mem.mm_node = NULL;
  971. bo->mem.page_alignment = page_alignment;
  972. bo->mem.bus.io_reserved_vm = false;
  973. bo->mem.bus.io_reserved_count = 0;
  974. bo->priv_flags = 0;
  975. bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED);
  976. bo->persistent_swap_storage = persistent_swap_storage;
  977. bo->acc_size = acc_size;
  978. bo->sg = sg;
  979. bo->resv = &bo->ttm_resv;
  980. reservation_object_init(bo->resv);
  981. atomic_inc(&bo->glob->bo_count);
  982. drm_vma_node_reset(&bo->vma_node);
  983. ret = ttm_bo_check_placement(bo, placement);
  984. /*
  985. * For ttm_bo_type_device buffers, allocate
  986. * address space from the device.
  987. */
  988. if (likely(!ret) &&
  989. (bo->type == ttm_bo_type_device ||
  990. bo->type == ttm_bo_type_sg))
  991. ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node,
  992. bo->mem.num_pages);
  993. locked = ww_mutex_trylock(&bo->resv->lock);
  994. WARN_ON(!locked);
  995. if (likely(!ret))
  996. ret = ttm_bo_validate(bo, placement, interruptible, false);
  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);
  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, 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. spin_lock_init(&bdev->fence_lock);
  1254. mutex_lock(&glob->device_list_mutex);
  1255. list_add_tail(&bdev->device_list, &glob->device_list);
  1256. mutex_unlock(&glob->device_list_mutex);
  1257. return 0;
  1258. out_no_sys:
  1259. return ret;
  1260. }
  1261. EXPORT_SYMBOL(ttm_bo_device_init);
  1262. /*
  1263. * buffer object vm functions.
  1264. */
  1265. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  1266. {
  1267. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  1268. if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) {
  1269. if (mem->mem_type == TTM_PL_SYSTEM)
  1270. return false;
  1271. if (man->flags & TTM_MEMTYPE_FLAG_CMA)
  1272. return false;
  1273. if (mem->placement & TTM_PL_FLAG_CACHED)
  1274. return false;
  1275. }
  1276. return true;
  1277. }
  1278. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
  1279. {
  1280. struct ttm_bo_device *bdev = bo->bdev;
  1281. drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping);
  1282. ttm_mem_io_free_vm(bo);
  1283. }
  1284. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo)
  1285. {
  1286. struct ttm_bo_device *bdev = bo->bdev;
  1287. struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type];
  1288. ttm_mem_io_lock(man, false);
  1289. ttm_bo_unmap_virtual_locked(bo);
  1290. ttm_mem_io_unlock(man);
  1291. }
  1292. EXPORT_SYMBOL(ttm_bo_unmap_virtual);
  1293. int ttm_bo_wait(struct ttm_buffer_object *bo,
  1294. bool lazy, bool interruptible, bool no_wait)
  1295. {
  1296. struct ttm_bo_driver *driver = bo->bdev->driver;
  1297. struct ttm_bo_device *bdev = bo->bdev;
  1298. void *sync_obj;
  1299. int ret = 0;
  1300. if (likely(bo->sync_obj == NULL))
  1301. return 0;
  1302. while (bo->sync_obj) {
  1303. if (driver->sync_obj_signaled(bo->sync_obj)) {
  1304. void *tmp_obj = bo->sync_obj;
  1305. bo->sync_obj = NULL;
  1306. clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags);
  1307. spin_unlock(&bdev->fence_lock);
  1308. driver->sync_obj_unref(&tmp_obj);
  1309. spin_lock(&bdev->fence_lock);
  1310. continue;
  1311. }
  1312. if (no_wait)
  1313. return -EBUSY;
  1314. sync_obj = driver->sync_obj_ref(bo->sync_obj);
  1315. spin_unlock(&bdev->fence_lock);
  1316. ret = driver->sync_obj_wait(sync_obj,
  1317. lazy, interruptible);
  1318. if (unlikely(ret != 0)) {
  1319. driver->sync_obj_unref(&sync_obj);
  1320. spin_lock(&bdev->fence_lock);
  1321. return ret;
  1322. }
  1323. spin_lock(&bdev->fence_lock);
  1324. if (likely(bo->sync_obj == sync_obj)) {
  1325. void *tmp_obj = bo->sync_obj;
  1326. bo->sync_obj = NULL;
  1327. clear_bit(TTM_BO_PRIV_FLAG_MOVING,
  1328. &bo->priv_flags);
  1329. spin_unlock(&bdev->fence_lock);
  1330. driver->sync_obj_unref(&sync_obj);
  1331. driver->sync_obj_unref(&tmp_obj);
  1332. spin_lock(&bdev->fence_lock);
  1333. } else {
  1334. spin_unlock(&bdev->fence_lock);
  1335. driver->sync_obj_unref(&sync_obj);
  1336. spin_lock(&bdev->fence_lock);
  1337. }
  1338. }
  1339. return 0;
  1340. }
  1341. EXPORT_SYMBOL(ttm_bo_wait);
  1342. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait)
  1343. {
  1344. struct ttm_bo_device *bdev = bo->bdev;
  1345. int ret = 0;
  1346. /*
  1347. * Using ttm_bo_reserve makes sure the lru lists are updated.
  1348. */
  1349. ret = ttm_bo_reserve(bo, true, no_wait, false, 0);
  1350. if (unlikely(ret != 0))
  1351. return ret;
  1352. spin_lock(&bdev->fence_lock);
  1353. ret = ttm_bo_wait(bo, false, true, no_wait);
  1354. spin_unlock(&bdev->fence_lock);
  1355. if (likely(ret == 0))
  1356. atomic_inc(&bo->cpu_writers);
  1357. ttm_bo_unreserve(bo);
  1358. return ret;
  1359. }
  1360. EXPORT_SYMBOL(ttm_bo_synccpu_write_grab);
  1361. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo)
  1362. {
  1363. atomic_dec(&bo->cpu_writers);
  1364. }
  1365. EXPORT_SYMBOL(ttm_bo_synccpu_write_release);
  1366. /**
  1367. * A buffer object shrink method that tries to swap out the first
  1368. * buffer object on the bo_global::swap_lru list.
  1369. */
  1370. static int ttm_bo_swapout(struct ttm_mem_shrink *shrink)
  1371. {
  1372. struct ttm_bo_global *glob =
  1373. container_of(shrink, struct ttm_bo_global, shrink);
  1374. struct ttm_buffer_object *bo;
  1375. int ret = -EBUSY;
  1376. int put_count;
  1377. uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM);
  1378. spin_lock(&glob->lru_lock);
  1379. list_for_each_entry(bo, &glob->swap_lru, swap) {
  1380. ret = __ttm_bo_reserve(bo, false, true, false, 0);
  1381. if (!ret)
  1382. break;
  1383. }
  1384. if (ret) {
  1385. spin_unlock(&glob->lru_lock);
  1386. return ret;
  1387. }
  1388. kref_get(&bo->list_kref);
  1389. if (!list_empty(&bo->ddestroy)) {
  1390. ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false);
  1391. kref_put(&bo->list_kref, ttm_bo_release_list);
  1392. return ret;
  1393. }
  1394. put_count = ttm_bo_del_from_lru(bo);
  1395. spin_unlock(&glob->lru_lock);
  1396. ttm_bo_list_ref_sub(bo, put_count, true);
  1397. /**
  1398. * Wait for GPU, then move to system cached.
  1399. */
  1400. spin_lock(&bo->bdev->fence_lock);
  1401. ret = ttm_bo_wait(bo, false, false, false);
  1402. spin_unlock(&bo->bdev->fence_lock);
  1403. if (unlikely(ret != 0))
  1404. goto out;
  1405. if ((bo->mem.placement & swap_placement) != swap_placement) {
  1406. struct ttm_mem_reg evict_mem;
  1407. evict_mem = bo->mem;
  1408. evict_mem.mm_node = NULL;
  1409. evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED;
  1410. evict_mem.mem_type = TTM_PL_SYSTEM;
  1411. ret = ttm_bo_handle_move_mem(bo, &evict_mem, true,
  1412. false, false);
  1413. if (unlikely(ret != 0))
  1414. goto out;
  1415. }
  1416. ttm_bo_unmap_virtual(bo);
  1417. /**
  1418. * Swap out. Buffer will be swapped in again as soon as
  1419. * anyone tries to access a ttm page.
  1420. */
  1421. if (bo->bdev->driver->swap_notify)
  1422. bo->bdev->driver->swap_notify(bo);
  1423. ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage);
  1424. out:
  1425. /**
  1426. *
  1427. * Unreserve without putting on LRU to avoid swapping out an
  1428. * already swapped buffer.
  1429. */
  1430. __ttm_bo_unreserve(bo);
  1431. kref_put(&bo->list_kref, ttm_bo_release_list);
  1432. return ret;
  1433. }
  1434. void ttm_bo_swapout_all(struct ttm_bo_device *bdev)
  1435. {
  1436. while (ttm_bo_swapout(&bdev->glob->shrink) == 0)
  1437. ;
  1438. }
  1439. EXPORT_SYMBOL(ttm_bo_swapout_all);
  1440. /**
  1441. * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become
  1442. * unreserved
  1443. *
  1444. * @bo: Pointer to buffer
  1445. */
  1446. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo)
  1447. {
  1448. int ret;
  1449. /*
  1450. * In the absense of a wait_unlocked API,
  1451. * Use the bo::wu_mutex to avoid triggering livelocks due to
  1452. * concurrent use of this function. Note that this use of
  1453. * bo::wu_mutex can go away if we change locking order to
  1454. * mmap_sem -> bo::reserve.
  1455. */
  1456. ret = mutex_lock_interruptible(&bo->wu_mutex);
  1457. if (unlikely(ret != 0))
  1458. return -ERESTARTSYS;
  1459. if (!ww_mutex_is_locked(&bo->resv->lock))
  1460. goto out_unlock;
  1461. ret = __ttm_bo_reserve(bo, true, false, false, NULL);
  1462. if (unlikely(ret != 0))
  1463. goto out_unlock;
  1464. __ttm_bo_unreserve(bo);
  1465. out_unlock:
  1466. mutex_unlock(&bo->wu_mutex);
  1467. return ret;
  1468. }