ttm_bo.c 41 KB

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