amdgpu_ttm.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Jerome Glisse <glisse@freedesktop.org>
  29. * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
  30. * Dave Airlie
  31. */
  32. #include <drm/ttm/ttm_bo_api.h>
  33. #include <drm/ttm/ttm_bo_driver.h>
  34. #include <drm/ttm/ttm_placement.h>
  35. #include <drm/ttm/ttm_module.h>
  36. #include <drm/ttm/ttm_page_alloc.h>
  37. #include <drm/drmP.h>
  38. #include <drm/amdgpu_drm.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/slab.h>
  41. #include <linux/swiotlb.h>
  42. #include <linux/swap.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/debugfs.h>
  45. #include "amdgpu.h"
  46. #include "amdgpu_trace.h"
  47. #include "bif/bif_4_1_d.h"
  48. #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
  49. static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
  50. struct ttm_mem_reg *mem, unsigned num_pages,
  51. uint64_t offset, unsigned window,
  52. struct amdgpu_ring *ring,
  53. uint64_t *addr);
  54. static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
  55. static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
  56. /*
  57. * Global memory.
  58. */
  59. static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
  60. {
  61. return ttm_mem_global_init(ref->object);
  62. }
  63. static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
  64. {
  65. ttm_mem_global_release(ref->object);
  66. }
  67. static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
  68. {
  69. struct drm_global_reference *global_ref;
  70. struct amdgpu_ring *ring;
  71. struct amd_sched_rq *rq;
  72. int r;
  73. adev->mman.mem_global_referenced = false;
  74. global_ref = &adev->mman.mem_global_ref;
  75. global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  76. global_ref->size = sizeof(struct ttm_mem_global);
  77. global_ref->init = &amdgpu_ttm_mem_global_init;
  78. global_ref->release = &amdgpu_ttm_mem_global_release;
  79. r = drm_global_item_ref(global_ref);
  80. if (r) {
  81. DRM_ERROR("Failed setting up TTM memory accounting "
  82. "subsystem.\n");
  83. goto error_mem;
  84. }
  85. adev->mman.bo_global_ref.mem_glob =
  86. adev->mman.mem_global_ref.object;
  87. global_ref = &adev->mman.bo_global_ref.ref;
  88. global_ref->global_type = DRM_GLOBAL_TTM_BO;
  89. global_ref->size = sizeof(struct ttm_bo_global);
  90. global_ref->init = &ttm_bo_global_init;
  91. global_ref->release = &ttm_bo_global_release;
  92. r = drm_global_item_ref(global_ref);
  93. if (r) {
  94. DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  95. goto error_bo;
  96. }
  97. mutex_init(&adev->mman.gtt_window_lock);
  98. ring = adev->mman.buffer_funcs_ring;
  99. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  100. r = amd_sched_entity_init(&ring->sched, &adev->mman.entity,
  101. rq, amdgpu_sched_jobs);
  102. if (r) {
  103. DRM_ERROR("Failed setting up TTM BO move run queue.\n");
  104. goto error_entity;
  105. }
  106. adev->mman.mem_global_referenced = true;
  107. return 0;
  108. error_entity:
  109. drm_global_item_unref(&adev->mman.bo_global_ref.ref);
  110. error_bo:
  111. drm_global_item_unref(&adev->mman.mem_global_ref);
  112. error_mem:
  113. return r;
  114. }
  115. static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
  116. {
  117. if (adev->mman.mem_global_referenced) {
  118. amd_sched_entity_fini(adev->mman.entity.sched,
  119. &adev->mman.entity);
  120. mutex_destroy(&adev->mman.gtt_window_lock);
  121. drm_global_item_unref(&adev->mman.bo_global_ref.ref);
  122. drm_global_item_unref(&adev->mman.mem_global_ref);
  123. adev->mman.mem_global_referenced = false;
  124. }
  125. }
  126. static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  127. {
  128. return 0;
  129. }
  130. static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  131. struct ttm_mem_type_manager *man)
  132. {
  133. struct amdgpu_device *adev;
  134. adev = amdgpu_ttm_adev(bdev);
  135. switch (type) {
  136. case TTM_PL_SYSTEM:
  137. /* System memory */
  138. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  139. man->available_caching = TTM_PL_MASK_CACHING;
  140. man->default_caching = TTM_PL_FLAG_CACHED;
  141. break;
  142. case TTM_PL_TT:
  143. man->func = &amdgpu_gtt_mgr_func;
  144. man->gpu_offset = adev->mc.gart_start;
  145. man->available_caching = TTM_PL_MASK_CACHING;
  146. man->default_caching = TTM_PL_FLAG_CACHED;
  147. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
  148. break;
  149. case TTM_PL_VRAM:
  150. /* "On-card" video ram */
  151. man->func = &amdgpu_vram_mgr_func;
  152. man->gpu_offset = adev->mc.vram_start;
  153. man->flags = TTM_MEMTYPE_FLAG_FIXED |
  154. TTM_MEMTYPE_FLAG_MAPPABLE;
  155. man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
  156. man->default_caching = TTM_PL_FLAG_WC;
  157. break;
  158. case AMDGPU_PL_GDS:
  159. case AMDGPU_PL_GWS:
  160. case AMDGPU_PL_OA:
  161. /* On-chip GDS memory*/
  162. man->func = &ttm_bo_manager_func;
  163. man->gpu_offset = 0;
  164. man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
  165. man->available_caching = TTM_PL_FLAG_UNCACHED;
  166. man->default_caching = TTM_PL_FLAG_UNCACHED;
  167. break;
  168. default:
  169. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  170. return -EINVAL;
  171. }
  172. return 0;
  173. }
  174. static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
  175. struct ttm_placement *placement)
  176. {
  177. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  178. struct amdgpu_bo *abo;
  179. static const struct ttm_place placements = {
  180. .fpfn = 0,
  181. .lpfn = 0,
  182. .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
  183. };
  184. if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
  185. placement->placement = &placements;
  186. placement->busy_placement = &placements;
  187. placement->num_placement = 1;
  188. placement->num_busy_placement = 1;
  189. return;
  190. }
  191. abo = container_of(bo, struct amdgpu_bo, tbo);
  192. switch (bo->mem.mem_type) {
  193. case TTM_PL_VRAM:
  194. if (adev->mman.buffer_funcs &&
  195. adev->mman.buffer_funcs_ring &&
  196. adev->mman.buffer_funcs_ring->ready == false) {
  197. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
  198. } else if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
  199. !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
  200. unsigned fpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
  201. struct drm_mm_node *node = bo->mem.mm_node;
  202. unsigned long pages_left;
  203. for (pages_left = bo->mem.num_pages;
  204. pages_left;
  205. pages_left -= node->size, node++) {
  206. if (node->start < fpfn)
  207. break;
  208. }
  209. if (!pages_left)
  210. goto gtt;
  211. /* Try evicting to the CPU inaccessible part of VRAM
  212. * first, but only set GTT as busy placement, so this
  213. * BO will be evicted to GTT rather than causing other
  214. * BOs to be evicted from VRAM
  215. */
  216. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
  217. AMDGPU_GEM_DOMAIN_GTT);
  218. abo->placements[0].fpfn = fpfn;
  219. abo->placements[0].lpfn = 0;
  220. abo->placement.busy_placement = &abo->placements[1];
  221. abo->placement.num_busy_placement = 1;
  222. } else {
  223. gtt:
  224. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
  225. }
  226. break;
  227. case TTM_PL_TT:
  228. default:
  229. amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
  230. }
  231. *placement = abo->placement;
  232. }
  233. static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  234. {
  235. struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
  236. if (amdgpu_ttm_tt_get_usermm(bo->ttm))
  237. return -EPERM;
  238. return drm_vma_node_verify_access(&abo->gem_base.vma_node,
  239. filp->private_data);
  240. }
  241. static void amdgpu_move_null(struct ttm_buffer_object *bo,
  242. struct ttm_mem_reg *new_mem)
  243. {
  244. struct ttm_mem_reg *old_mem = &bo->mem;
  245. BUG_ON(old_mem->mm_node != NULL);
  246. *old_mem = *new_mem;
  247. new_mem->mm_node = NULL;
  248. }
  249. static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
  250. struct drm_mm_node *mm_node,
  251. struct ttm_mem_reg *mem)
  252. {
  253. uint64_t addr = 0;
  254. if (mem->mem_type != TTM_PL_TT ||
  255. amdgpu_gtt_mgr_is_allocated(mem)) {
  256. addr = mm_node->start << PAGE_SHIFT;
  257. addr += bo->bdev->man[mem->mem_type].gpu_offset;
  258. }
  259. return addr;
  260. }
  261. static int amdgpu_move_blit(struct ttm_buffer_object *bo,
  262. bool evict, bool no_wait_gpu,
  263. struct ttm_mem_reg *new_mem,
  264. struct ttm_mem_reg *old_mem)
  265. {
  266. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  267. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  268. struct drm_mm_node *old_mm, *new_mm;
  269. uint64_t old_start, old_size, new_start, new_size;
  270. unsigned long num_pages;
  271. struct dma_fence *fence = NULL;
  272. int r;
  273. BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
  274. if (!ring->ready) {
  275. DRM_ERROR("Trying to move memory with ring turned off.\n");
  276. return -EINVAL;
  277. }
  278. old_mm = old_mem->mm_node;
  279. old_size = old_mm->size;
  280. old_start = amdgpu_mm_node_addr(bo, old_mm, old_mem);
  281. new_mm = new_mem->mm_node;
  282. new_size = new_mm->size;
  283. new_start = amdgpu_mm_node_addr(bo, new_mm, new_mem);
  284. num_pages = new_mem->num_pages;
  285. mutex_lock(&adev->mman.gtt_window_lock);
  286. while (num_pages) {
  287. unsigned long cur_pages = min(min(old_size, new_size),
  288. (u64)AMDGPU_GTT_MAX_TRANSFER_SIZE);
  289. uint64_t from = old_start, to = new_start;
  290. struct dma_fence *next;
  291. if (old_mem->mem_type == TTM_PL_TT &&
  292. !amdgpu_gtt_mgr_is_allocated(old_mem)) {
  293. r = amdgpu_map_buffer(bo, old_mem, cur_pages,
  294. old_start, 0, ring, &from);
  295. if (r)
  296. goto error;
  297. }
  298. if (new_mem->mem_type == TTM_PL_TT &&
  299. !amdgpu_gtt_mgr_is_allocated(new_mem)) {
  300. r = amdgpu_map_buffer(bo, new_mem, cur_pages,
  301. new_start, 1, ring, &to);
  302. if (r)
  303. goto error;
  304. }
  305. r = amdgpu_copy_buffer(ring, from, to,
  306. cur_pages * PAGE_SIZE,
  307. bo->resv, &next, false, true);
  308. if (r)
  309. goto error;
  310. dma_fence_put(fence);
  311. fence = next;
  312. num_pages -= cur_pages;
  313. if (!num_pages)
  314. break;
  315. old_size -= cur_pages;
  316. if (!old_size) {
  317. old_start = amdgpu_mm_node_addr(bo, ++old_mm, old_mem);
  318. old_size = old_mm->size;
  319. } else {
  320. old_start += cur_pages * PAGE_SIZE;
  321. }
  322. new_size -= cur_pages;
  323. if (!new_size) {
  324. new_start = amdgpu_mm_node_addr(bo, ++new_mm, new_mem);
  325. new_size = new_mm->size;
  326. } else {
  327. new_start += cur_pages * PAGE_SIZE;
  328. }
  329. }
  330. mutex_unlock(&adev->mman.gtt_window_lock);
  331. r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
  332. dma_fence_put(fence);
  333. return r;
  334. error:
  335. mutex_unlock(&adev->mman.gtt_window_lock);
  336. if (fence)
  337. dma_fence_wait(fence, false);
  338. dma_fence_put(fence);
  339. return r;
  340. }
  341. static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
  342. bool evict, bool interruptible,
  343. bool no_wait_gpu,
  344. struct ttm_mem_reg *new_mem)
  345. {
  346. struct amdgpu_device *adev;
  347. struct ttm_mem_reg *old_mem = &bo->mem;
  348. struct ttm_mem_reg tmp_mem;
  349. struct ttm_place placements;
  350. struct ttm_placement placement;
  351. int r;
  352. adev = amdgpu_ttm_adev(bo->bdev);
  353. tmp_mem = *new_mem;
  354. tmp_mem.mm_node = NULL;
  355. placement.num_placement = 1;
  356. placement.placement = &placements;
  357. placement.num_busy_placement = 1;
  358. placement.busy_placement = &placements;
  359. placements.fpfn = 0;
  360. placements.lpfn = 0;
  361. placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  362. r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
  363. interruptible, no_wait_gpu);
  364. if (unlikely(r)) {
  365. return r;
  366. }
  367. r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
  368. if (unlikely(r)) {
  369. goto out_cleanup;
  370. }
  371. r = ttm_tt_bind(bo->ttm, &tmp_mem);
  372. if (unlikely(r)) {
  373. goto out_cleanup;
  374. }
  375. r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
  376. if (unlikely(r)) {
  377. goto out_cleanup;
  378. }
  379. r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem);
  380. out_cleanup:
  381. ttm_bo_mem_put(bo, &tmp_mem);
  382. return r;
  383. }
  384. static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
  385. bool evict, bool interruptible,
  386. bool no_wait_gpu,
  387. struct ttm_mem_reg *new_mem)
  388. {
  389. struct amdgpu_device *adev;
  390. struct ttm_mem_reg *old_mem = &bo->mem;
  391. struct ttm_mem_reg tmp_mem;
  392. struct ttm_placement placement;
  393. struct ttm_place placements;
  394. int r;
  395. adev = amdgpu_ttm_adev(bo->bdev);
  396. tmp_mem = *new_mem;
  397. tmp_mem.mm_node = NULL;
  398. placement.num_placement = 1;
  399. placement.placement = &placements;
  400. placement.num_busy_placement = 1;
  401. placement.busy_placement = &placements;
  402. placements.fpfn = 0;
  403. placements.lpfn = 0;
  404. placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
  405. r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
  406. interruptible, no_wait_gpu);
  407. if (unlikely(r)) {
  408. return r;
  409. }
  410. r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem);
  411. if (unlikely(r)) {
  412. goto out_cleanup;
  413. }
  414. r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
  415. if (unlikely(r)) {
  416. goto out_cleanup;
  417. }
  418. out_cleanup:
  419. ttm_bo_mem_put(bo, &tmp_mem);
  420. return r;
  421. }
  422. static int amdgpu_bo_move(struct ttm_buffer_object *bo,
  423. bool evict, bool interruptible,
  424. bool no_wait_gpu,
  425. struct ttm_mem_reg *new_mem)
  426. {
  427. struct amdgpu_device *adev;
  428. struct amdgpu_bo *abo;
  429. struct ttm_mem_reg *old_mem = &bo->mem;
  430. int r;
  431. /* Can't move a pinned BO */
  432. abo = container_of(bo, struct amdgpu_bo, tbo);
  433. if (WARN_ON_ONCE(abo->pin_count > 0))
  434. return -EINVAL;
  435. adev = amdgpu_ttm_adev(bo->bdev);
  436. if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
  437. amdgpu_move_null(bo, new_mem);
  438. return 0;
  439. }
  440. if ((old_mem->mem_type == TTM_PL_TT &&
  441. new_mem->mem_type == TTM_PL_SYSTEM) ||
  442. (old_mem->mem_type == TTM_PL_SYSTEM &&
  443. new_mem->mem_type == TTM_PL_TT)) {
  444. /* bind is enough */
  445. amdgpu_move_null(bo, new_mem);
  446. return 0;
  447. }
  448. if (adev->mman.buffer_funcs == NULL ||
  449. adev->mman.buffer_funcs_ring == NULL ||
  450. !adev->mman.buffer_funcs_ring->ready) {
  451. /* use memcpy */
  452. goto memcpy;
  453. }
  454. if (old_mem->mem_type == TTM_PL_VRAM &&
  455. new_mem->mem_type == TTM_PL_SYSTEM) {
  456. r = amdgpu_move_vram_ram(bo, evict, interruptible,
  457. no_wait_gpu, new_mem);
  458. } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
  459. new_mem->mem_type == TTM_PL_VRAM) {
  460. r = amdgpu_move_ram_vram(bo, evict, interruptible,
  461. no_wait_gpu, new_mem);
  462. } else {
  463. r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
  464. }
  465. if (r) {
  466. memcpy:
  467. r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem);
  468. if (r) {
  469. return r;
  470. }
  471. }
  472. if (bo->type == ttm_bo_type_device &&
  473. new_mem->mem_type == TTM_PL_VRAM &&
  474. old_mem->mem_type != TTM_PL_VRAM) {
  475. /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
  476. * accesses the BO after it's moved.
  477. */
  478. abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  479. }
  480. /* update statistics */
  481. atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
  482. return 0;
  483. }
  484. static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  485. {
  486. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  487. struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
  488. mem->bus.addr = NULL;
  489. mem->bus.offset = 0;
  490. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  491. mem->bus.base = 0;
  492. mem->bus.is_iomem = false;
  493. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  494. return -EINVAL;
  495. switch (mem->mem_type) {
  496. case TTM_PL_SYSTEM:
  497. /* system memory */
  498. return 0;
  499. case TTM_PL_TT:
  500. break;
  501. case TTM_PL_VRAM:
  502. mem->bus.offset = mem->start << PAGE_SHIFT;
  503. /* check if it's visible */
  504. if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
  505. return -EINVAL;
  506. mem->bus.base = adev->mc.aper_base;
  507. mem->bus.is_iomem = true;
  508. break;
  509. default:
  510. return -EINVAL;
  511. }
  512. return 0;
  513. }
  514. static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  515. {
  516. }
  517. static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
  518. unsigned long page_offset)
  519. {
  520. struct drm_mm_node *mm = bo->mem.mm_node;
  521. uint64_t size = mm->size;
  522. uint64_t offset = page_offset;
  523. page_offset = do_div(offset, size);
  524. mm += offset;
  525. return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset;
  526. }
  527. /*
  528. * TTM backend functions.
  529. */
  530. struct amdgpu_ttm_gup_task_list {
  531. struct list_head list;
  532. struct task_struct *task;
  533. };
  534. struct amdgpu_ttm_tt {
  535. struct ttm_dma_tt ttm;
  536. struct amdgpu_device *adev;
  537. u64 offset;
  538. uint64_t userptr;
  539. struct mm_struct *usermm;
  540. uint32_t userflags;
  541. spinlock_t guptasklock;
  542. struct list_head guptasks;
  543. atomic_t mmu_invalidations;
  544. struct list_head list;
  545. };
  546. int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
  547. {
  548. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  549. unsigned int flags = 0;
  550. unsigned pinned = 0;
  551. int r;
  552. if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
  553. flags |= FOLL_WRITE;
  554. if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
  555. /* check that we only use anonymous memory
  556. to prevent problems with writeback */
  557. unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
  558. struct vm_area_struct *vma;
  559. vma = find_vma(gtt->usermm, gtt->userptr);
  560. if (!vma || vma->vm_file || vma->vm_end < end)
  561. return -EPERM;
  562. }
  563. do {
  564. unsigned num_pages = ttm->num_pages - pinned;
  565. uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
  566. struct page **p = pages + pinned;
  567. struct amdgpu_ttm_gup_task_list guptask;
  568. guptask.task = current;
  569. spin_lock(&gtt->guptasklock);
  570. list_add(&guptask.list, &gtt->guptasks);
  571. spin_unlock(&gtt->guptasklock);
  572. r = get_user_pages(userptr, num_pages, flags, p, NULL);
  573. spin_lock(&gtt->guptasklock);
  574. list_del(&guptask.list);
  575. spin_unlock(&gtt->guptasklock);
  576. if (r < 0)
  577. goto release_pages;
  578. pinned += r;
  579. } while (pinned < ttm->num_pages);
  580. return 0;
  581. release_pages:
  582. release_pages(pages, pinned, 0);
  583. return r;
  584. }
  585. static void amdgpu_trace_dma_map(struct ttm_tt *ttm)
  586. {
  587. struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
  588. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  589. unsigned i;
  590. if (unlikely(trace_amdgpu_ttm_tt_populate_enabled())) {
  591. for (i = 0; i < ttm->num_pages; i++) {
  592. trace_amdgpu_ttm_tt_populate(
  593. adev,
  594. gtt->ttm.dma_address[i],
  595. page_to_phys(ttm->pages[i]));
  596. }
  597. }
  598. }
  599. static void amdgpu_trace_dma_unmap(struct ttm_tt *ttm)
  600. {
  601. struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
  602. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  603. unsigned i;
  604. if (unlikely(trace_amdgpu_ttm_tt_unpopulate_enabled())) {
  605. for (i = 0; i < ttm->num_pages; i++) {
  606. trace_amdgpu_ttm_tt_unpopulate(
  607. adev,
  608. gtt->ttm.dma_address[i],
  609. page_to_phys(ttm->pages[i]));
  610. }
  611. }
  612. }
  613. /* prepare the sg table with the user pages */
  614. static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  615. {
  616. struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
  617. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  618. unsigned nents;
  619. int r;
  620. int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
  621. enum dma_data_direction direction = write ?
  622. DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
  623. r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
  624. ttm->num_pages << PAGE_SHIFT,
  625. GFP_KERNEL);
  626. if (r)
  627. goto release_sg;
  628. r = -ENOMEM;
  629. nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
  630. if (nents != ttm->sg->nents)
  631. goto release_sg;
  632. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  633. gtt->ttm.dma_address, ttm->num_pages);
  634. amdgpu_trace_dma_map(ttm);
  635. return 0;
  636. release_sg:
  637. kfree(ttm->sg);
  638. ttm->sg = NULL;
  639. return r;
  640. }
  641. static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
  642. {
  643. struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
  644. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  645. struct sg_page_iter sg_iter;
  646. int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
  647. enum dma_data_direction direction = write ?
  648. DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
  649. /* double check that we don't free the table twice */
  650. if (!ttm->sg || !ttm->sg->sgl)
  651. return;
  652. /* free the sg table and pages again */
  653. dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
  654. for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
  655. struct page *page = sg_page_iter_page(&sg_iter);
  656. if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
  657. set_page_dirty(page);
  658. mark_page_accessed(page);
  659. put_page(page);
  660. }
  661. amdgpu_trace_dma_unmap(ttm);
  662. sg_free_table(ttm->sg);
  663. }
  664. static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
  665. struct ttm_mem_reg *bo_mem)
  666. {
  667. struct amdgpu_ttm_tt *gtt = (void*)ttm;
  668. uint64_t flags;
  669. int r = 0;
  670. if (gtt->userptr) {
  671. r = amdgpu_ttm_tt_pin_userptr(ttm);
  672. if (r) {
  673. DRM_ERROR("failed to pin userptr\n");
  674. return r;
  675. }
  676. }
  677. if (!ttm->num_pages) {
  678. WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
  679. ttm->num_pages, bo_mem, ttm);
  680. }
  681. if (bo_mem->mem_type == AMDGPU_PL_GDS ||
  682. bo_mem->mem_type == AMDGPU_PL_GWS ||
  683. bo_mem->mem_type == AMDGPU_PL_OA)
  684. return -EINVAL;
  685. if (!amdgpu_gtt_mgr_is_allocated(bo_mem))
  686. return 0;
  687. spin_lock(&gtt->adev->gtt_list_lock);
  688. flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
  689. gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
  690. r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
  691. ttm->pages, gtt->ttm.dma_address, flags);
  692. if (r) {
  693. DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
  694. ttm->num_pages, gtt->offset);
  695. goto error_gart_bind;
  696. }
  697. list_add_tail(&gtt->list, &gtt->adev->gtt_list);
  698. error_gart_bind:
  699. spin_unlock(&gtt->adev->gtt_list_lock);
  700. return r;
  701. }
  702. bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
  703. {
  704. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  705. return gtt && !list_empty(&gtt->list);
  706. }
  707. int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
  708. {
  709. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
  710. struct ttm_tt *ttm = bo->ttm;
  711. struct ttm_mem_reg tmp;
  712. struct ttm_placement placement;
  713. struct ttm_place placements;
  714. int r;
  715. if (!ttm || amdgpu_ttm_is_bound(ttm))
  716. return 0;
  717. tmp = bo->mem;
  718. tmp.mm_node = NULL;
  719. placement.num_placement = 1;
  720. placement.placement = &placements;
  721. placement.num_busy_placement = 1;
  722. placement.busy_placement = &placements;
  723. placements.fpfn = 0;
  724. placements.lpfn = adev->mc.gart_size >> PAGE_SHIFT;
  725. placements.flags = bo->mem.placement | TTM_PL_FLAG_TT;
  726. r = ttm_bo_mem_space(bo, &placement, &tmp, true, false);
  727. if (unlikely(r))
  728. return r;
  729. r = ttm_bo_move_ttm(bo, true, false, &tmp);
  730. if (unlikely(r))
  731. ttm_bo_mem_put(bo, &tmp);
  732. else
  733. bo->offset = (bo->mem.start << PAGE_SHIFT) +
  734. bo->bdev->man[bo->mem.mem_type].gpu_offset;
  735. return r;
  736. }
  737. int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
  738. {
  739. struct amdgpu_ttm_tt *gtt, *tmp;
  740. struct ttm_mem_reg bo_mem;
  741. uint64_t flags;
  742. int r;
  743. bo_mem.mem_type = TTM_PL_TT;
  744. spin_lock(&adev->gtt_list_lock);
  745. list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
  746. flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
  747. r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
  748. gtt->ttm.ttm.pages, gtt->ttm.dma_address,
  749. flags);
  750. if (r) {
  751. spin_unlock(&adev->gtt_list_lock);
  752. DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
  753. gtt->ttm.ttm.num_pages, gtt->offset);
  754. return r;
  755. }
  756. }
  757. spin_unlock(&adev->gtt_list_lock);
  758. return 0;
  759. }
  760. static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
  761. {
  762. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  763. int r;
  764. if (gtt->userptr)
  765. amdgpu_ttm_tt_unpin_userptr(ttm);
  766. if (!amdgpu_ttm_is_bound(ttm))
  767. return 0;
  768. /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
  769. spin_lock(&gtt->adev->gtt_list_lock);
  770. r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
  771. if (r) {
  772. DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
  773. gtt->ttm.ttm.num_pages, gtt->offset);
  774. goto error_unbind;
  775. }
  776. list_del_init(&gtt->list);
  777. error_unbind:
  778. spin_unlock(&gtt->adev->gtt_list_lock);
  779. return r;
  780. }
  781. static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
  782. {
  783. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  784. ttm_dma_tt_fini(&gtt->ttm);
  785. kfree(gtt);
  786. }
  787. static struct ttm_backend_func amdgpu_backend_func = {
  788. .bind = &amdgpu_ttm_backend_bind,
  789. .unbind = &amdgpu_ttm_backend_unbind,
  790. .destroy = &amdgpu_ttm_backend_destroy,
  791. };
  792. static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
  793. unsigned long size, uint32_t page_flags,
  794. struct page *dummy_read_page)
  795. {
  796. struct amdgpu_device *adev;
  797. struct amdgpu_ttm_tt *gtt;
  798. adev = amdgpu_ttm_adev(bdev);
  799. gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
  800. if (gtt == NULL) {
  801. return NULL;
  802. }
  803. gtt->ttm.ttm.func = &amdgpu_backend_func;
  804. gtt->adev = adev;
  805. if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
  806. kfree(gtt);
  807. return NULL;
  808. }
  809. INIT_LIST_HEAD(&gtt->list);
  810. return &gtt->ttm.ttm;
  811. }
  812. static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
  813. {
  814. struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
  815. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  816. unsigned i;
  817. int r;
  818. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  819. if (ttm->state != tt_unpopulated)
  820. return 0;
  821. if (gtt && gtt->userptr) {
  822. ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
  823. if (!ttm->sg)
  824. return -ENOMEM;
  825. ttm->page_flags |= TTM_PAGE_FLAG_SG;
  826. ttm->state = tt_unbound;
  827. return 0;
  828. }
  829. if (slave && ttm->sg) {
  830. drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
  831. gtt->ttm.dma_address, ttm->num_pages);
  832. ttm->state = tt_unbound;
  833. r = 0;
  834. goto trace_mappings;
  835. }
  836. #ifdef CONFIG_SWIOTLB
  837. if (swiotlb_nr_tbl()) {
  838. r = ttm_dma_populate(&gtt->ttm, adev->dev);
  839. goto trace_mappings;
  840. }
  841. #endif
  842. r = ttm_pool_populate(ttm);
  843. if (r) {
  844. return r;
  845. }
  846. for (i = 0; i < ttm->num_pages; i++) {
  847. gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
  848. 0, PAGE_SIZE,
  849. PCI_DMA_BIDIRECTIONAL);
  850. if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
  851. while (i--) {
  852. pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
  853. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  854. gtt->ttm.dma_address[i] = 0;
  855. }
  856. ttm_pool_unpopulate(ttm);
  857. return -EFAULT;
  858. }
  859. }
  860. r = 0;
  861. trace_mappings:
  862. if (likely(!r))
  863. amdgpu_trace_dma_map(ttm);
  864. return r;
  865. }
  866. static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
  867. {
  868. struct amdgpu_device *adev;
  869. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  870. unsigned i;
  871. bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  872. if (gtt && gtt->userptr) {
  873. kfree(ttm->sg);
  874. ttm->sg = NULL;
  875. ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
  876. return;
  877. }
  878. if (slave)
  879. return;
  880. adev = amdgpu_ttm_adev(ttm->bdev);
  881. amdgpu_trace_dma_unmap(ttm);
  882. #ifdef CONFIG_SWIOTLB
  883. if (swiotlb_nr_tbl()) {
  884. ttm_dma_unpopulate(&gtt->ttm, adev->dev);
  885. return;
  886. }
  887. #endif
  888. for (i = 0; i < ttm->num_pages; i++) {
  889. if (gtt->ttm.dma_address[i]) {
  890. pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
  891. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  892. }
  893. }
  894. ttm_pool_unpopulate(ttm);
  895. }
  896. int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
  897. uint32_t flags)
  898. {
  899. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  900. if (gtt == NULL)
  901. return -EINVAL;
  902. gtt->userptr = addr;
  903. gtt->usermm = current->mm;
  904. gtt->userflags = flags;
  905. spin_lock_init(&gtt->guptasklock);
  906. INIT_LIST_HEAD(&gtt->guptasks);
  907. atomic_set(&gtt->mmu_invalidations, 0);
  908. return 0;
  909. }
  910. struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
  911. {
  912. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  913. if (gtt == NULL)
  914. return NULL;
  915. return gtt->usermm;
  916. }
  917. bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
  918. unsigned long end)
  919. {
  920. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  921. struct amdgpu_ttm_gup_task_list *entry;
  922. unsigned long size;
  923. if (gtt == NULL || !gtt->userptr)
  924. return false;
  925. size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
  926. if (gtt->userptr > end || gtt->userptr + size <= start)
  927. return false;
  928. spin_lock(&gtt->guptasklock);
  929. list_for_each_entry(entry, &gtt->guptasks, list) {
  930. if (entry->task == current) {
  931. spin_unlock(&gtt->guptasklock);
  932. return false;
  933. }
  934. }
  935. spin_unlock(&gtt->guptasklock);
  936. atomic_inc(&gtt->mmu_invalidations);
  937. return true;
  938. }
  939. bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
  940. int *last_invalidated)
  941. {
  942. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  943. int prev_invalidated = *last_invalidated;
  944. *last_invalidated = atomic_read(&gtt->mmu_invalidations);
  945. return prev_invalidated != *last_invalidated;
  946. }
  947. bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
  948. {
  949. struct amdgpu_ttm_tt *gtt = (void *)ttm;
  950. if (gtt == NULL)
  951. return false;
  952. return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
  953. }
  954. uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
  955. struct ttm_mem_reg *mem)
  956. {
  957. uint64_t flags = 0;
  958. if (mem && mem->mem_type != TTM_PL_SYSTEM)
  959. flags |= AMDGPU_PTE_VALID;
  960. if (mem && mem->mem_type == TTM_PL_TT) {
  961. flags |= AMDGPU_PTE_SYSTEM;
  962. if (ttm->caching_state == tt_cached)
  963. flags |= AMDGPU_PTE_SNOOPED;
  964. }
  965. flags |= adev->gart.gart_pte_flags;
  966. flags |= AMDGPU_PTE_READABLE;
  967. if (!amdgpu_ttm_tt_is_readonly(ttm))
  968. flags |= AMDGPU_PTE_WRITEABLE;
  969. return flags;
  970. }
  971. static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  972. const struct ttm_place *place)
  973. {
  974. unsigned long num_pages = bo->mem.num_pages;
  975. struct drm_mm_node *node = bo->mem.mm_node;
  976. if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
  977. return ttm_bo_eviction_valuable(bo, place);
  978. switch (bo->mem.mem_type) {
  979. case TTM_PL_TT:
  980. return true;
  981. case TTM_PL_VRAM:
  982. /* Check each drm MM node individually */
  983. while (num_pages) {
  984. if (place->fpfn < (node->start + node->size) &&
  985. !(place->lpfn && place->lpfn <= node->start))
  986. return true;
  987. num_pages -= node->size;
  988. ++node;
  989. }
  990. break;
  991. default:
  992. break;
  993. }
  994. return ttm_bo_eviction_valuable(bo, place);
  995. }
  996. static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
  997. unsigned long offset,
  998. void *buf, int len, int write)
  999. {
  1000. struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
  1001. struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
  1002. struct drm_mm_node *nodes = abo->tbo.mem.mm_node;
  1003. uint32_t value = 0;
  1004. int ret = 0;
  1005. uint64_t pos;
  1006. unsigned long flags;
  1007. if (bo->mem.mem_type != TTM_PL_VRAM)
  1008. return -EIO;
  1009. while (offset >= (nodes->size << PAGE_SHIFT)) {
  1010. offset -= nodes->size << PAGE_SHIFT;
  1011. ++nodes;
  1012. }
  1013. pos = (nodes->start << PAGE_SHIFT) + offset;
  1014. while (len && pos < adev->mc.mc_vram_size) {
  1015. uint64_t aligned_pos = pos & ~(uint64_t)3;
  1016. uint32_t bytes = 4 - (pos & 3);
  1017. uint32_t shift = (pos & 3) * 8;
  1018. uint32_t mask = 0xffffffff << shift;
  1019. if (len < bytes) {
  1020. mask &= 0xffffffff >> (bytes - len) * 8;
  1021. bytes = len;
  1022. }
  1023. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  1024. WREG32(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
  1025. WREG32(mmMM_INDEX_HI, aligned_pos >> 31);
  1026. if (!write || mask != 0xffffffff)
  1027. value = RREG32(mmMM_DATA);
  1028. if (write) {
  1029. value &= ~mask;
  1030. value |= (*(uint32_t *)buf << shift) & mask;
  1031. WREG32(mmMM_DATA, value);
  1032. }
  1033. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  1034. if (!write) {
  1035. value = (value & mask) >> shift;
  1036. memcpy(buf, &value, bytes);
  1037. }
  1038. ret += bytes;
  1039. buf = (uint8_t *)buf + bytes;
  1040. pos += bytes;
  1041. len -= bytes;
  1042. if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
  1043. ++nodes;
  1044. pos = (nodes->start << PAGE_SHIFT);
  1045. }
  1046. }
  1047. return ret;
  1048. }
  1049. static struct ttm_bo_driver amdgpu_bo_driver = {
  1050. .ttm_tt_create = &amdgpu_ttm_tt_create,
  1051. .ttm_tt_populate = &amdgpu_ttm_tt_populate,
  1052. .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
  1053. .invalidate_caches = &amdgpu_invalidate_caches,
  1054. .init_mem_type = &amdgpu_init_mem_type,
  1055. .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
  1056. .evict_flags = &amdgpu_evict_flags,
  1057. .move = &amdgpu_bo_move,
  1058. .verify_access = &amdgpu_verify_access,
  1059. .move_notify = &amdgpu_bo_move_notify,
  1060. .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
  1061. .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
  1062. .io_mem_free = &amdgpu_ttm_io_mem_free,
  1063. .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
  1064. .access_memory = &amdgpu_ttm_access_memory
  1065. };
  1066. int amdgpu_ttm_init(struct amdgpu_device *adev)
  1067. {
  1068. uint64_t gtt_size;
  1069. int r;
  1070. u64 vis_vram_limit;
  1071. r = amdgpu_ttm_global_init(adev);
  1072. if (r) {
  1073. return r;
  1074. }
  1075. /* No others user of address space so set it to 0 */
  1076. r = ttm_bo_device_init(&adev->mman.bdev,
  1077. adev->mman.bo_global_ref.ref.object,
  1078. &amdgpu_bo_driver,
  1079. adev->ddev->anon_inode->i_mapping,
  1080. DRM_FILE_PAGE_OFFSET,
  1081. adev->need_dma32);
  1082. if (r) {
  1083. DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
  1084. return r;
  1085. }
  1086. adev->mman.initialized = true;
  1087. r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
  1088. adev->mc.real_vram_size >> PAGE_SHIFT);
  1089. if (r) {
  1090. DRM_ERROR("Failed initializing VRAM heap.\n");
  1091. return r;
  1092. }
  1093. /* Reduce size of CPU-visible VRAM if requested */
  1094. vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
  1095. if (amdgpu_vis_vram_limit > 0 &&
  1096. vis_vram_limit <= adev->mc.visible_vram_size)
  1097. adev->mc.visible_vram_size = vis_vram_limit;
  1098. /* Change the size here instead of the init above so only lpfn is affected */
  1099. amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
  1100. r = amdgpu_bo_create_kernel(adev, adev->mc.stolen_size, PAGE_SIZE,
  1101. AMDGPU_GEM_DOMAIN_VRAM,
  1102. &adev->stolen_vga_memory,
  1103. NULL, NULL);
  1104. if (r)
  1105. return r;
  1106. DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
  1107. (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
  1108. if (amdgpu_gtt_size == -1)
  1109. gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
  1110. adev->mc.mc_vram_size);
  1111. else
  1112. gtt_size = (uint64_t)amdgpu_gtt_size << 20;
  1113. r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
  1114. if (r) {
  1115. DRM_ERROR("Failed initializing GTT heap.\n");
  1116. return r;
  1117. }
  1118. DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
  1119. (unsigned)(gtt_size / (1024 * 1024)));
  1120. adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
  1121. adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
  1122. adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
  1123. adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
  1124. adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
  1125. adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
  1126. adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
  1127. adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
  1128. adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
  1129. /* GDS Memory */
  1130. if (adev->gds.mem.total_size) {
  1131. r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
  1132. adev->gds.mem.total_size >> PAGE_SHIFT);
  1133. if (r) {
  1134. DRM_ERROR("Failed initializing GDS heap.\n");
  1135. return r;
  1136. }
  1137. }
  1138. /* GWS */
  1139. if (adev->gds.gws.total_size) {
  1140. r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
  1141. adev->gds.gws.total_size >> PAGE_SHIFT);
  1142. if (r) {
  1143. DRM_ERROR("Failed initializing gws heap.\n");
  1144. return r;
  1145. }
  1146. }
  1147. /* OA */
  1148. if (adev->gds.oa.total_size) {
  1149. r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
  1150. adev->gds.oa.total_size >> PAGE_SHIFT);
  1151. if (r) {
  1152. DRM_ERROR("Failed initializing oa heap.\n");
  1153. return r;
  1154. }
  1155. }
  1156. r = amdgpu_ttm_debugfs_init(adev);
  1157. if (r) {
  1158. DRM_ERROR("Failed to init debugfs\n");
  1159. return r;
  1160. }
  1161. return 0;
  1162. }
  1163. void amdgpu_ttm_fini(struct amdgpu_device *adev)
  1164. {
  1165. int r;
  1166. if (!adev->mman.initialized)
  1167. return;
  1168. amdgpu_ttm_debugfs_fini(adev);
  1169. if (adev->stolen_vga_memory) {
  1170. r = amdgpu_bo_reserve(adev->stolen_vga_memory, true);
  1171. if (r == 0) {
  1172. amdgpu_bo_unpin(adev->stolen_vga_memory);
  1173. amdgpu_bo_unreserve(adev->stolen_vga_memory);
  1174. }
  1175. amdgpu_bo_unref(&adev->stolen_vga_memory);
  1176. }
  1177. ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
  1178. ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
  1179. if (adev->gds.mem.total_size)
  1180. ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
  1181. if (adev->gds.gws.total_size)
  1182. ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
  1183. if (adev->gds.oa.total_size)
  1184. ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
  1185. ttm_bo_device_release(&adev->mman.bdev);
  1186. amdgpu_gart_fini(adev);
  1187. amdgpu_ttm_global_fini(adev);
  1188. adev->mman.initialized = false;
  1189. DRM_INFO("amdgpu: ttm finalized\n");
  1190. }
  1191. /* this should only be called at bootup or when userspace
  1192. * isn't running */
  1193. void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
  1194. {
  1195. struct ttm_mem_type_manager *man;
  1196. if (!adev->mman.initialized)
  1197. return;
  1198. man = &adev->mman.bdev.man[TTM_PL_VRAM];
  1199. /* this just adjusts TTM size idea, which sets lpfn to the correct value */
  1200. man->size = size >> PAGE_SHIFT;
  1201. }
  1202. int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
  1203. {
  1204. struct drm_file *file_priv;
  1205. struct amdgpu_device *adev;
  1206. if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
  1207. return -EINVAL;
  1208. file_priv = filp->private_data;
  1209. adev = file_priv->minor->dev->dev_private;
  1210. if (adev == NULL)
  1211. return -EINVAL;
  1212. return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
  1213. }
  1214. static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
  1215. struct ttm_mem_reg *mem, unsigned num_pages,
  1216. uint64_t offset, unsigned window,
  1217. struct amdgpu_ring *ring,
  1218. uint64_t *addr)
  1219. {
  1220. struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
  1221. struct amdgpu_device *adev = ring->adev;
  1222. struct ttm_tt *ttm = bo->ttm;
  1223. struct amdgpu_job *job;
  1224. unsigned num_dw, num_bytes;
  1225. dma_addr_t *dma_address;
  1226. struct dma_fence *fence;
  1227. uint64_t src_addr, dst_addr;
  1228. uint64_t flags;
  1229. int r;
  1230. BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
  1231. AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
  1232. *addr = adev->mc.gart_start;
  1233. *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
  1234. AMDGPU_GPU_PAGE_SIZE;
  1235. num_dw = adev->mman.buffer_funcs->copy_num_dw;
  1236. while (num_dw & 0x7)
  1237. num_dw++;
  1238. num_bytes = num_pages * 8;
  1239. r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
  1240. if (r)
  1241. return r;
  1242. src_addr = num_dw * 4;
  1243. src_addr += job->ibs[0].gpu_addr;
  1244. dst_addr = adev->gart.table_addr;
  1245. dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
  1246. amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
  1247. dst_addr, num_bytes);
  1248. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  1249. WARN_ON(job->ibs[0].length_dw > num_dw);
  1250. dma_address = &gtt->ttm.dma_address[offset >> PAGE_SHIFT];
  1251. flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
  1252. r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
  1253. &job->ibs[0].ptr[num_dw]);
  1254. if (r)
  1255. goto error_free;
  1256. r = amdgpu_job_submit(job, ring, &adev->mman.entity,
  1257. AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
  1258. if (r)
  1259. goto error_free;
  1260. dma_fence_put(fence);
  1261. return r;
  1262. error_free:
  1263. amdgpu_job_free(job);
  1264. return r;
  1265. }
  1266. int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
  1267. uint64_t dst_offset, uint32_t byte_count,
  1268. struct reservation_object *resv,
  1269. struct dma_fence **fence, bool direct_submit,
  1270. bool vm_needs_flush)
  1271. {
  1272. struct amdgpu_device *adev = ring->adev;
  1273. struct amdgpu_job *job;
  1274. uint32_t max_bytes;
  1275. unsigned num_loops, num_dw;
  1276. unsigned i;
  1277. int r;
  1278. max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
  1279. num_loops = DIV_ROUND_UP(byte_count, max_bytes);
  1280. num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
  1281. /* for IB padding */
  1282. while (num_dw & 0x7)
  1283. num_dw++;
  1284. r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
  1285. if (r)
  1286. return r;
  1287. job->vm_needs_flush = vm_needs_flush;
  1288. if (resv) {
  1289. r = amdgpu_sync_resv(adev, &job->sync, resv,
  1290. AMDGPU_FENCE_OWNER_UNDEFINED);
  1291. if (r) {
  1292. DRM_ERROR("sync failed (%d).\n", r);
  1293. goto error_free;
  1294. }
  1295. }
  1296. for (i = 0; i < num_loops; i++) {
  1297. uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
  1298. amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
  1299. dst_offset, cur_size_in_bytes);
  1300. src_offset += cur_size_in_bytes;
  1301. dst_offset += cur_size_in_bytes;
  1302. byte_count -= cur_size_in_bytes;
  1303. }
  1304. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  1305. WARN_ON(job->ibs[0].length_dw > num_dw);
  1306. if (direct_submit) {
  1307. r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
  1308. NULL, fence);
  1309. job->fence = dma_fence_get(*fence);
  1310. if (r)
  1311. DRM_ERROR("Error scheduling IBs (%d)\n", r);
  1312. amdgpu_job_free(job);
  1313. } else {
  1314. r = amdgpu_job_submit(job, ring, &adev->mman.entity,
  1315. AMDGPU_FENCE_OWNER_UNDEFINED, fence);
  1316. if (r)
  1317. goto error_free;
  1318. }
  1319. return r;
  1320. error_free:
  1321. amdgpu_job_free(job);
  1322. return r;
  1323. }
  1324. int amdgpu_fill_buffer(struct amdgpu_bo *bo,
  1325. uint64_t src_data,
  1326. struct reservation_object *resv,
  1327. struct dma_fence **fence)
  1328. {
  1329. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  1330. /* max_bytes applies to SDMA_OP_PTEPDE as well as SDMA_OP_CONST_FILL*/
  1331. uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
  1332. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  1333. struct drm_mm_node *mm_node;
  1334. unsigned long num_pages;
  1335. unsigned int num_loops, num_dw;
  1336. struct amdgpu_job *job;
  1337. int r;
  1338. if (!ring->ready) {
  1339. DRM_ERROR("Trying to clear memory with ring turned off.\n");
  1340. return -EINVAL;
  1341. }
  1342. if (bo->tbo.mem.mem_type == TTM_PL_TT) {
  1343. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  1344. if (r)
  1345. return r;
  1346. }
  1347. num_pages = bo->tbo.num_pages;
  1348. mm_node = bo->tbo.mem.mm_node;
  1349. num_loops = 0;
  1350. while (num_pages) {
  1351. uint32_t byte_count = mm_node->size << PAGE_SHIFT;
  1352. num_loops += DIV_ROUND_UP(byte_count, max_bytes);
  1353. num_pages -= mm_node->size;
  1354. ++mm_node;
  1355. }
  1356. /* 10 double words for each SDMA_OP_PTEPDE cmd */
  1357. num_dw = num_loops * 10;
  1358. /* for IB padding */
  1359. num_dw += 64;
  1360. r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
  1361. if (r)
  1362. return r;
  1363. if (resv) {
  1364. r = amdgpu_sync_resv(adev, &job->sync, resv,
  1365. AMDGPU_FENCE_OWNER_UNDEFINED);
  1366. if (r) {
  1367. DRM_ERROR("sync failed (%d).\n", r);
  1368. goto error_free;
  1369. }
  1370. }
  1371. num_pages = bo->tbo.num_pages;
  1372. mm_node = bo->tbo.mem.mm_node;
  1373. while (num_pages) {
  1374. uint32_t byte_count = mm_node->size << PAGE_SHIFT;
  1375. uint64_t dst_addr;
  1376. WARN_ONCE(byte_count & 0x7, "size should be a multiple of 8");
  1377. dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
  1378. while (byte_count) {
  1379. uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
  1380. amdgpu_vm_set_pte_pde(adev, &job->ibs[0],
  1381. dst_addr, 0,
  1382. cur_size_in_bytes >> 3, 0,
  1383. src_data);
  1384. dst_addr += cur_size_in_bytes;
  1385. byte_count -= cur_size_in_bytes;
  1386. }
  1387. num_pages -= mm_node->size;
  1388. ++mm_node;
  1389. }
  1390. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  1391. WARN_ON(job->ibs[0].length_dw > num_dw);
  1392. r = amdgpu_job_submit(job, ring, &adev->mman.entity,
  1393. AMDGPU_FENCE_OWNER_UNDEFINED, fence);
  1394. if (r)
  1395. goto error_free;
  1396. return 0;
  1397. error_free:
  1398. amdgpu_job_free(job);
  1399. return r;
  1400. }
  1401. #if defined(CONFIG_DEBUG_FS)
  1402. static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
  1403. {
  1404. struct drm_info_node *node = (struct drm_info_node *)m->private;
  1405. unsigned ttm_pl = *(int *)node->info_ent->data;
  1406. struct drm_device *dev = node->minor->dev;
  1407. struct amdgpu_device *adev = dev->dev_private;
  1408. struct ttm_mem_type_manager *man = &adev->mman.bdev.man[ttm_pl];
  1409. struct drm_printer p = drm_seq_file_printer(m);
  1410. man->func->debug(man, &p);
  1411. return 0;
  1412. }
  1413. static int ttm_pl_vram = TTM_PL_VRAM;
  1414. static int ttm_pl_tt = TTM_PL_TT;
  1415. static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
  1416. {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
  1417. {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
  1418. {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
  1419. #ifdef CONFIG_SWIOTLB
  1420. {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
  1421. #endif
  1422. };
  1423. static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
  1424. size_t size, loff_t *pos)
  1425. {
  1426. struct amdgpu_device *adev = file_inode(f)->i_private;
  1427. ssize_t result = 0;
  1428. int r;
  1429. if (size & 0x3 || *pos & 0x3)
  1430. return -EINVAL;
  1431. if (*pos >= adev->mc.mc_vram_size)
  1432. return -ENXIO;
  1433. while (size) {
  1434. unsigned long flags;
  1435. uint32_t value;
  1436. if (*pos >= adev->mc.mc_vram_size)
  1437. return result;
  1438. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  1439. WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
  1440. WREG32(mmMM_INDEX_HI, *pos >> 31);
  1441. value = RREG32(mmMM_DATA);
  1442. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  1443. r = put_user(value, (uint32_t *)buf);
  1444. if (r)
  1445. return r;
  1446. result += 4;
  1447. buf += 4;
  1448. *pos += 4;
  1449. size -= 4;
  1450. }
  1451. return result;
  1452. }
  1453. static const struct file_operations amdgpu_ttm_vram_fops = {
  1454. .owner = THIS_MODULE,
  1455. .read = amdgpu_ttm_vram_read,
  1456. .llseek = default_llseek
  1457. };
  1458. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  1459. static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
  1460. size_t size, loff_t *pos)
  1461. {
  1462. struct amdgpu_device *adev = file_inode(f)->i_private;
  1463. ssize_t result = 0;
  1464. int r;
  1465. while (size) {
  1466. loff_t p = *pos / PAGE_SIZE;
  1467. unsigned off = *pos & ~PAGE_MASK;
  1468. size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
  1469. struct page *page;
  1470. void *ptr;
  1471. if (p >= adev->gart.num_cpu_pages)
  1472. return result;
  1473. page = adev->gart.pages[p];
  1474. if (page) {
  1475. ptr = kmap(page);
  1476. ptr += off;
  1477. r = copy_to_user(buf, ptr, cur_size);
  1478. kunmap(adev->gart.pages[p]);
  1479. } else
  1480. r = clear_user(buf, cur_size);
  1481. if (r)
  1482. return -EFAULT;
  1483. result += cur_size;
  1484. buf += cur_size;
  1485. *pos += cur_size;
  1486. size -= cur_size;
  1487. }
  1488. return result;
  1489. }
  1490. static const struct file_operations amdgpu_ttm_gtt_fops = {
  1491. .owner = THIS_MODULE,
  1492. .read = amdgpu_ttm_gtt_read,
  1493. .llseek = default_llseek
  1494. };
  1495. #endif
  1496. #endif
  1497. static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
  1498. {
  1499. #if defined(CONFIG_DEBUG_FS)
  1500. unsigned count;
  1501. struct drm_minor *minor = adev->ddev->primary;
  1502. struct dentry *ent, *root = minor->debugfs_root;
  1503. ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
  1504. adev, &amdgpu_ttm_vram_fops);
  1505. if (IS_ERR(ent))
  1506. return PTR_ERR(ent);
  1507. i_size_write(ent->d_inode, adev->mc.mc_vram_size);
  1508. adev->mman.vram = ent;
  1509. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  1510. ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
  1511. adev, &amdgpu_ttm_gtt_fops);
  1512. if (IS_ERR(ent))
  1513. return PTR_ERR(ent);
  1514. i_size_write(ent->d_inode, adev->mc.gart_size);
  1515. adev->mman.gtt = ent;
  1516. #endif
  1517. count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
  1518. #ifdef CONFIG_SWIOTLB
  1519. if (!swiotlb_nr_tbl())
  1520. --count;
  1521. #endif
  1522. return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
  1523. #else
  1524. return 0;
  1525. #endif
  1526. }
  1527. static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
  1528. {
  1529. #if defined(CONFIG_DEBUG_FS)
  1530. debugfs_remove(adev->mman.vram);
  1531. adev->mman.vram = NULL;
  1532. #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  1533. debugfs_remove(adev->mman.gtt);
  1534. adev->mman.gtt = NULL;
  1535. #endif
  1536. #endif
  1537. }