amdgpu_vm.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/dma-fence-array.h>
  29. #include <linux/interval_tree_generic.h>
  30. #include <linux/idr.h>
  31. #include <drm/drmP.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "amdgpu_trace.h"
  35. #include "amdgpu_amdkfd.h"
  36. /*
  37. * GPUVM
  38. * GPUVM is similar to the legacy gart on older asics, however
  39. * rather than there being a single global gart table
  40. * for the entire GPU, there are multiple VM page tables active
  41. * at any given time. The VM page tables can contain a mix
  42. * vram pages and system memory pages and system memory pages
  43. * can be mapped as snooped (cached system pages) or unsnooped
  44. * (uncached system pages).
  45. * Each VM has an ID associated with it and there is a page table
  46. * associated with each VMID. When execting a command buffer,
  47. * the kernel tells the the ring what VMID to use for that command
  48. * buffer. VMIDs are allocated dynamically as commands are submitted.
  49. * The userspace drivers maintain their own address space and the kernel
  50. * sets up their pages tables accordingly when they submit their
  51. * command buffers and a VMID is assigned.
  52. * Cayman/Trinity support up to 8 active VMs at any given time;
  53. * SI supports 16.
  54. */
  55. #define START(node) ((node)->start)
  56. #define LAST(node) ((node)->last)
  57. INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
  58. START, LAST, static, amdgpu_vm_it)
  59. #undef START
  60. #undef LAST
  61. /* Local structure. Encapsulate some VM table update parameters to reduce
  62. * the number of function parameters
  63. */
  64. struct amdgpu_pte_update_params {
  65. /* amdgpu device we do this update for */
  66. struct amdgpu_device *adev;
  67. /* optional amdgpu_vm we do this update for */
  68. struct amdgpu_vm *vm;
  69. /* address where to copy page table entries from */
  70. uint64_t src;
  71. /* indirect buffer to fill with commands */
  72. struct amdgpu_ib *ib;
  73. /* Function which actually does the update */
  74. void (*func)(struct amdgpu_pte_update_params *params,
  75. struct amdgpu_bo *bo, uint64_t pe,
  76. uint64_t addr, unsigned count, uint32_t incr,
  77. uint64_t flags);
  78. /* The next two are used during VM update by CPU
  79. * DMA addresses to use for mapping
  80. * Kernel pointer of PD/PT BO that needs to be updated
  81. */
  82. dma_addr_t *pages_addr;
  83. void *kptr;
  84. };
  85. /* Helper to disable partial resident texture feature from a fence callback */
  86. struct amdgpu_prt_cb {
  87. struct amdgpu_device *adev;
  88. struct dma_fence_cb cb;
  89. };
  90. static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
  91. struct amdgpu_vm *vm,
  92. struct amdgpu_bo *bo)
  93. {
  94. base->vm = vm;
  95. base->bo = bo;
  96. INIT_LIST_HEAD(&base->bo_list);
  97. INIT_LIST_HEAD(&base->vm_status);
  98. if (!bo)
  99. return;
  100. list_add_tail(&base->bo_list, &bo->va);
  101. if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
  102. return;
  103. if (bo->preferred_domains &
  104. amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
  105. return;
  106. /*
  107. * we checked all the prerequisites, but it looks like this per vm bo
  108. * is currently evicted. add the bo to the evicted list to make sure it
  109. * is validated on next vm use to avoid fault.
  110. * */
  111. list_move_tail(&base->vm_status, &vm->evicted);
  112. }
  113. /**
  114. * amdgpu_vm_level_shift - return the addr shift for each level
  115. *
  116. * @adev: amdgpu_device pointer
  117. *
  118. * Returns the number of bits the pfn needs to be right shifted for a level.
  119. */
  120. static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
  121. unsigned level)
  122. {
  123. unsigned shift = 0xff;
  124. switch (level) {
  125. case AMDGPU_VM_PDB2:
  126. case AMDGPU_VM_PDB1:
  127. case AMDGPU_VM_PDB0:
  128. shift = 9 * (AMDGPU_VM_PDB0 - level) +
  129. adev->vm_manager.block_size;
  130. break;
  131. case AMDGPU_VM_PTB:
  132. shift = 0;
  133. break;
  134. default:
  135. dev_err(adev->dev, "the level%d isn't supported.\n", level);
  136. }
  137. return shift;
  138. }
  139. /**
  140. * amdgpu_vm_num_entries - return the number of entries in a PD/PT
  141. *
  142. * @adev: amdgpu_device pointer
  143. *
  144. * Calculate the number of entries in a page directory or page table.
  145. */
  146. static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
  147. unsigned level)
  148. {
  149. unsigned shift = amdgpu_vm_level_shift(adev,
  150. adev->vm_manager.root_level);
  151. if (level == adev->vm_manager.root_level)
  152. /* For the root directory */
  153. return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
  154. else if (level != AMDGPU_VM_PTB)
  155. /* Everything in between */
  156. return 512;
  157. else
  158. /* For the page tables on the leaves */
  159. return AMDGPU_VM_PTE_COUNT(adev);
  160. }
  161. /**
  162. * amdgpu_vm_bo_size - returns the size of the BOs in bytes
  163. *
  164. * @adev: amdgpu_device pointer
  165. *
  166. * Calculate the size of the BO for a page directory or page table in bytes.
  167. */
  168. static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
  169. {
  170. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
  171. }
  172. /**
  173. * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
  174. *
  175. * @vm: vm providing the BOs
  176. * @validated: head of validation list
  177. * @entry: entry to add
  178. *
  179. * Add the page directory to the list of BOs to
  180. * validate for command submission.
  181. */
  182. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  183. struct list_head *validated,
  184. struct amdgpu_bo_list_entry *entry)
  185. {
  186. entry->robj = vm->root.base.bo;
  187. entry->priority = 0;
  188. entry->tv.bo = &entry->robj->tbo;
  189. entry->tv.shared = true;
  190. entry->user_pages = NULL;
  191. list_add(&entry->tv.head, validated);
  192. }
  193. /**
  194. * amdgpu_vm_validate_pt_bos - validate the page table BOs
  195. *
  196. * @adev: amdgpu device pointer
  197. * @vm: vm providing the BOs
  198. * @validate: callback to do the validation
  199. * @param: parameter for the validation callback
  200. *
  201. * Validate the page table BOs on command submission if neccessary.
  202. */
  203. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  204. int (*validate)(void *p, struct amdgpu_bo *bo),
  205. void *param)
  206. {
  207. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  208. struct amdgpu_vm_bo_base *bo_base, *tmp;
  209. int r = 0;
  210. list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
  211. struct amdgpu_bo *bo = bo_base->bo;
  212. if (bo->parent) {
  213. r = validate(param, bo);
  214. if (r)
  215. break;
  216. spin_lock(&glob->lru_lock);
  217. ttm_bo_move_to_lru_tail(&bo->tbo);
  218. if (bo->shadow)
  219. ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
  220. spin_unlock(&glob->lru_lock);
  221. }
  222. if (bo->tbo.type != ttm_bo_type_kernel) {
  223. spin_lock(&vm->moved_lock);
  224. list_move(&bo_base->vm_status, &vm->moved);
  225. spin_unlock(&vm->moved_lock);
  226. } else {
  227. list_move(&bo_base->vm_status, &vm->relocated);
  228. }
  229. }
  230. spin_lock(&glob->lru_lock);
  231. list_for_each_entry(bo_base, &vm->idle, vm_status) {
  232. struct amdgpu_bo *bo = bo_base->bo;
  233. if (!bo->parent)
  234. continue;
  235. ttm_bo_move_to_lru_tail(&bo->tbo);
  236. if (bo->shadow)
  237. ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
  238. }
  239. spin_unlock(&glob->lru_lock);
  240. return r;
  241. }
  242. /**
  243. * amdgpu_vm_ready - check VM is ready for updates
  244. *
  245. * @vm: VM to check
  246. *
  247. * Check if all VM PDs/PTs are ready for updates
  248. */
  249. bool amdgpu_vm_ready(struct amdgpu_vm *vm)
  250. {
  251. return list_empty(&vm->evicted);
  252. }
  253. /**
  254. * amdgpu_vm_clear_bo - initially clear the PDs/PTs
  255. *
  256. * @adev: amdgpu_device pointer
  257. * @bo: BO to clear
  258. * @level: level this BO is at
  259. *
  260. * Root PD needs to be reserved when calling this.
  261. */
  262. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  263. struct amdgpu_vm *vm, struct amdgpu_bo *bo,
  264. unsigned level, bool pte_support_ats)
  265. {
  266. struct ttm_operation_ctx ctx = { true, false };
  267. struct dma_fence *fence = NULL;
  268. unsigned entries, ats_entries;
  269. struct amdgpu_ring *ring;
  270. struct amdgpu_job *job;
  271. uint64_t addr;
  272. int r;
  273. addr = amdgpu_bo_gpu_offset(bo);
  274. entries = amdgpu_bo_size(bo) / 8;
  275. if (pte_support_ats) {
  276. if (level == adev->vm_manager.root_level) {
  277. ats_entries = amdgpu_vm_level_shift(adev, level);
  278. ats_entries += AMDGPU_GPU_PAGE_SHIFT;
  279. ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
  280. ats_entries = min(ats_entries, entries);
  281. entries -= ats_entries;
  282. } else {
  283. ats_entries = entries;
  284. entries = 0;
  285. }
  286. } else {
  287. ats_entries = 0;
  288. }
  289. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  290. r = reservation_object_reserve_shared(bo->tbo.resv);
  291. if (r)
  292. return r;
  293. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  294. if (r)
  295. goto error;
  296. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  297. if (r)
  298. goto error;
  299. if (ats_entries) {
  300. uint64_t ats_value;
  301. ats_value = AMDGPU_PTE_DEFAULT_ATC;
  302. if (level != AMDGPU_VM_PTB)
  303. ats_value |= AMDGPU_PDE_PTE;
  304. amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
  305. ats_entries, 0, ats_value);
  306. addr += ats_entries * 8;
  307. }
  308. if (entries)
  309. amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
  310. entries, 0, 0);
  311. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  312. WARN_ON(job->ibs[0].length_dw > 64);
  313. r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
  314. AMDGPU_FENCE_OWNER_UNDEFINED, false);
  315. if (r)
  316. goto error_free;
  317. r = amdgpu_job_submit(job, ring, &vm->entity,
  318. AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
  319. if (r)
  320. goto error_free;
  321. amdgpu_bo_fence(bo, fence, true);
  322. dma_fence_put(fence);
  323. if (bo->shadow)
  324. return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
  325. level, pte_support_ats);
  326. return 0;
  327. error_free:
  328. amdgpu_job_free(job);
  329. error:
  330. return r;
  331. }
  332. /**
  333. * amdgpu_vm_alloc_levels - allocate the PD/PT levels
  334. *
  335. * @adev: amdgpu_device pointer
  336. * @vm: requested vm
  337. * @saddr: start of the address range
  338. * @eaddr: end of the address range
  339. *
  340. * Make sure the page directories and page tables are allocated
  341. */
  342. static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  343. struct amdgpu_vm *vm,
  344. struct amdgpu_vm_pt *parent,
  345. uint64_t saddr, uint64_t eaddr,
  346. unsigned level, bool ats)
  347. {
  348. unsigned shift = amdgpu_vm_level_shift(adev, level);
  349. unsigned pt_idx, from, to;
  350. u64 flags;
  351. int r;
  352. if (!parent->entries) {
  353. unsigned num_entries = amdgpu_vm_num_entries(adev, level);
  354. parent->entries = kvmalloc_array(num_entries,
  355. sizeof(struct amdgpu_vm_pt),
  356. GFP_KERNEL | __GFP_ZERO);
  357. if (!parent->entries)
  358. return -ENOMEM;
  359. memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
  360. }
  361. from = saddr >> shift;
  362. to = eaddr >> shift;
  363. if (from >= amdgpu_vm_num_entries(adev, level) ||
  364. to >= amdgpu_vm_num_entries(adev, level))
  365. return -EINVAL;
  366. ++level;
  367. saddr = saddr & ((1 << shift) - 1);
  368. eaddr = eaddr & ((1 << shift) - 1);
  369. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  370. if (vm->use_cpu_for_update)
  371. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  372. else
  373. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  374. AMDGPU_GEM_CREATE_SHADOW);
  375. /* walk over the address space and allocate the page tables */
  376. for (pt_idx = from; pt_idx <= to; ++pt_idx) {
  377. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  378. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  379. struct amdgpu_bo *pt;
  380. if (!entry->base.bo) {
  381. struct amdgpu_bo_param bp;
  382. memset(&bp, 0, sizeof(bp));
  383. bp.size = amdgpu_vm_bo_size(adev, level);
  384. bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
  385. bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
  386. bp.flags = flags;
  387. bp.type = ttm_bo_type_kernel;
  388. bp.resv = resv;
  389. r = amdgpu_bo_create(adev, &bp, &pt);
  390. if (r)
  391. return r;
  392. r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
  393. if (r) {
  394. amdgpu_bo_unref(&pt->shadow);
  395. amdgpu_bo_unref(&pt);
  396. return r;
  397. }
  398. if (vm->use_cpu_for_update) {
  399. r = amdgpu_bo_kmap(pt, NULL);
  400. if (r) {
  401. amdgpu_bo_unref(&pt->shadow);
  402. amdgpu_bo_unref(&pt);
  403. return r;
  404. }
  405. }
  406. /* Keep a reference to the root directory to avoid
  407. * freeing them up in the wrong order.
  408. */
  409. pt->parent = amdgpu_bo_ref(parent->base.bo);
  410. amdgpu_vm_bo_base_init(&entry->base, vm, pt);
  411. list_move(&entry->base.vm_status, &vm->relocated);
  412. }
  413. if (level < AMDGPU_VM_PTB) {
  414. uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
  415. uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
  416. ((1 << shift) - 1);
  417. r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
  418. sub_eaddr, level, ats);
  419. if (r)
  420. return r;
  421. }
  422. }
  423. return 0;
  424. }
  425. /**
  426. * amdgpu_vm_alloc_pts - Allocate page tables.
  427. *
  428. * @adev: amdgpu_device pointer
  429. * @vm: VM to allocate page tables for
  430. * @saddr: Start address which needs to be allocated
  431. * @size: Size from start address we need.
  432. *
  433. * Make sure the page tables are allocated.
  434. */
  435. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  436. struct amdgpu_vm *vm,
  437. uint64_t saddr, uint64_t size)
  438. {
  439. uint64_t eaddr;
  440. bool ats = false;
  441. /* validate the parameters */
  442. if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
  443. return -EINVAL;
  444. eaddr = saddr + size - 1;
  445. if (vm->pte_support_ats)
  446. ats = saddr < AMDGPU_VA_HOLE_START;
  447. saddr /= AMDGPU_GPU_PAGE_SIZE;
  448. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  449. if (eaddr >= adev->vm_manager.max_pfn) {
  450. dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
  451. eaddr, adev->vm_manager.max_pfn);
  452. return -EINVAL;
  453. }
  454. return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
  455. adev->vm_manager.root_level, ats);
  456. }
  457. /**
  458. * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
  459. *
  460. * @adev: amdgpu_device pointer
  461. */
  462. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
  463. {
  464. const struct amdgpu_ip_block *ip_block;
  465. bool has_compute_vm_bug;
  466. struct amdgpu_ring *ring;
  467. int i;
  468. has_compute_vm_bug = false;
  469. ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
  470. if (ip_block) {
  471. /* Compute has a VM bug for GFX version < 7.
  472. Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
  473. if (ip_block->version->major <= 7)
  474. has_compute_vm_bug = true;
  475. else if (ip_block->version->major == 8)
  476. if (adev->gfx.mec_fw_version < 673)
  477. has_compute_vm_bug = true;
  478. }
  479. for (i = 0; i < adev->num_rings; i++) {
  480. ring = adev->rings[i];
  481. if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
  482. /* only compute rings */
  483. ring->has_compute_vm_bug = has_compute_vm_bug;
  484. else
  485. ring->has_compute_vm_bug = false;
  486. }
  487. }
  488. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  489. struct amdgpu_job *job)
  490. {
  491. struct amdgpu_device *adev = ring->adev;
  492. unsigned vmhub = ring->funcs->vmhub;
  493. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  494. struct amdgpu_vmid *id;
  495. bool gds_switch_needed;
  496. bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
  497. if (job->vmid == 0)
  498. return false;
  499. id = &id_mgr->ids[job->vmid];
  500. gds_switch_needed = ring->funcs->emit_gds_switch && (
  501. id->gds_base != job->gds_base ||
  502. id->gds_size != job->gds_size ||
  503. id->gws_base != job->gws_base ||
  504. id->gws_size != job->gws_size ||
  505. id->oa_base != job->oa_base ||
  506. id->oa_size != job->oa_size);
  507. if (amdgpu_vmid_had_gpu_reset(adev, id))
  508. return true;
  509. return vm_flush_needed || gds_switch_needed;
  510. }
  511. static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
  512. {
  513. return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
  514. }
  515. /**
  516. * amdgpu_vm_flush - hardware flush the vm
  517. *
  518. * @ring: ring to use for flush
  519. * @vmid: vmid number to use
  520. * @pd_addr: address of the page directory
  521. *
  522. * Emit a VM flush when it is necessary.
  523. */
  524. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
  525. {
  526. struct amdgpu_device *adev = ring->adev;
  527. unsigned vmhub = ring->funcs->vmhub;
  528. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  529. struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
  530. bool gds_switch_needed = ring->funcs->emit_gds_switch && (
  531. id->gds_base != job->gds_base ||
  532. id->gds_size != job->gds_size ||
  533. id->gws_base != job->gws_base ||
  534. id->gws_size != job->gws_size ||
  535. id->oa_base != job->oa_base ||
  536. id->oa_size != job->oa_size);
  537. bool vm_flush_needed = job->vm_needs_flush;
  538. bool pasid_mapping_needed = id->pasid != job->pasid ||
  539. !id->pasid_mapping ||
  540. !dma_fence_is_signaled(id->pasid_mapping);
  541. struct dma_fence *fence = NULL;
  542. unsigned patch_offset = 0;
  543. int r;
  544. if (amdgpu_vmid_had_gpu_reset(adev, id)) {
  545. gds_switch_needed = true;
  546. vm_flush_needed = true;
  547. pasid_mapping_needed = true;
  548. }
  549. gds_switch_needed &= !!ring->funcs->emit_gds_switch;
  550. vm_flush_needed &= !!ring->funcs->emit_vm_flush;
  551. pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
  552. ring->funcs->emit_wreg;
  553. if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
  554. return 0;
  555. if (ring->funcs->init_cond_exec)
  556. patch_offset = amdgpu_ring_init_cond_exec(ring);
  557. if (need_pipe_sync)
  558. amdgpu_ring_emit_pipeline_sync(ring);
  559. if (vm_flush_needed) {
  560. trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
  561. amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
  562. }
  563. if (pasid_mapping_needed)
  564. amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
  565. if (vm_flush_needed || pasid_mapping_needed) {
  566. r = amdgpu_fence_emit(ring, &fence, 0);
  567. if (r)
  568. return r;
  569. }
  570. if (vm_flush_needed) {
  571. mutex_lock(&id_mgr->lock);
  572. dma_fence_put(id->last_flush);
  573. id->last_flush = dma_fence_get(fence);
  574. id->current_gpu_reset_count =
  575. atomic_read(&adev->gpu_reset_counter);
  576. mutex_unlock(&id_mgr->lock);
  577. }
  578. if (pasid_mapping_needed) {
  579. id->pasid = job->pasid;
  580. dma_fence_put(id->pasid_mapping);
  581. id->pasid_mapping = dma_fence_get(fence);
  582. }
  583. dma_fence_put(fence);
  584. if (ring->funcs->emit_gds_switch && gds_switch_needed) {
  585. id->gds_base = job->gds_base;
  586. id->gds_size = job->gds_size;
  587. id->gws_base = job->gws_base;
  588. id->gws_size = job->gws_size;
  589. id->oa_base = job->oa_base;
  590. id->oa_size = job->oa_size;
  591. amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
  592. job->gds_size, job->gws_base,
  593. job->gws_size, job->oa_base,
  594. job->oa_size);
  595. }
  596. if (ring->funcs->patch_cond_exec)
  597. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  598. /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
  599. if (ring->funcs->emit_switch_buffer) {
  600. amdgpu_ring_emit_switch_buffer(ring);
  601. amdgpu_ring_emit_switch_buffer(ring);
  602. }
  603. return 0;
  604. }
  605. /**
  606. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  607. *
  608. * @vm: requested vm
  609. * @bo: requested buffer object
  610. *
  611. * Find @bo inside the requested vm.
  612. * Search inside the @bos vm list for the requested vm
  613. * Returns the found bo_va or NULL if none is found
  614. *
  615. * Object has to be reserved!
  616. */
  617. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  618. struct amdgpu_bo *bo)
  619. {
  620. struct amdgpu_bo_va *bo_va;
  621. list_for_each_entry(bo_va, &bo->va, base.bo_list) {
  622. if (bo_va->base.vm == vm) {
  623. return bo_va;
  624. }
  625. }
  626. return NULL;
  627. }
  628. /**
  629. * amdgpu_vm_do_set_ptes - helper to call the right asic function
  630. *
  631. * @params: see amdgpu_pte_update_params definition
  632. * @bo: PD/PT to update
  633. * @pe: addr of the page entry
  634. * @addr: dst addr to write into pe
  635. * @count: number of page entries to update
  636. * @incr: increase next addr by incr bytes
  637. * @flags: hw access flags
  638. *
  639. * Traces the parameters and calls the right asic functions
  640. * to setup the page table using the DMA.
  641. */
  642. static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
  643. struct amdgpu_bo *bo,
  644. uint64_t pe, uint64_t addr,
  645. unsigned count, uint32_t incr,
  646. uint64_t flags)
  647. {
  648. pe += amdgpu_bo_gpu_offset(bo);
  649. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  650. if (count < 3) {
  651. amdgpu_vm_write_pte(params->adev, params->ib, pe,
  652. addr | flags, count, incr);
  653. } else {
  654. amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
  655. count, incr, flags);
  656. }
  657. }
  658. /**
  659. * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
  660. *
  661. * @params: see amdgpu_pte_update_params definition
  662. * @bo: PD/PT to update
  663. * @pe: addr of the page entry
  664. * @addr: dst addr to write into pe
  665. * @count: number of page entries to update
  666. * @incr: increase next addr by incr bytes
  667. * @flags: hw access flags
  668. *
  669. * Traces the parameters and calls the DMA function to copy the PTEs.
  670. */
  671. static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
  672. struct amdgpu_bo *bo,
  673. uint64_t pe, uint64_t addr,
  674. unsigned count, uint32_t incr,
  675. uint64_t flags)
  676. {
  677. uint64_t src = (params->src + (addr >> 12) * 8);
  678. pe += amdgpu_bo_gpu_offset(bo);
  679. trace_amdgpu_vm_copy_ptes(pe, src, count);
  680. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
  681. }
  682. /**
  683. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  684. *
  685. * @pages_addr: optional DMA address to use for lookup
  686. * @addr: the unmapped addr
  687. *
  688. * Look up the physical address of the page that the pte resolves
  689. * to and return the pointer for the page table entry.
  690. */
  691. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  692. {
  693. uint64_t result;
  694. /* page table offset */
  695. result = pages_addr[addr >> PAGE_SHIFT];
  696. /* in case cpu page size != gpu page size*/
  697. result |= addr & (~PAGE_MASK);
  698. result &= 0xFFFFFFFFFFFFF000ULL;
  699. return result;
  700. }
  701. /**
  702. * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
  703. *
  704. * @params: see amdgpu_pte_update_params definition
  705. * @bo: PD/PT to update
  706. * @pe: kmap addr of the page entry
  707. * @addr: dst addr to write into pe
  708. * @count: number of page entries to update
  709. * @incr: increase next addr by incr bytes
  710. * @flags: hw access flags
  711. *
  712. * Write count number of PT/PD entries directly.
  713. */
  714. static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
  715. struct amdgpu_bo *bo,
  716. uint64_t pe, uint64_t addr,
  717. unsigned count, uint32_t incr,
  718. uint64_t flags)
  719. {
  720. unsigned int i;
  721. uint64_t value;
  722. pe += (unsigned long)amdgpu_bo_kptr(bo);
  723. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  724. for (i = 0; i < count; i++) {
  725. value = params->pages_addr ?
  726. amdgpu_vm_map_gart(params->pages_addr, addr) :
  727. addr;
  728. amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
  729. i, value, flags);
  730. addr += incr;
  731. }
  732. }
  733. static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  734. void *owner)
  735. {
  736. struct amdgpu_sync sync;
  737. int r;
  738. amdgpu_sync_create(&sync);
  739. amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
  740. r = amdgpu_sync_wait(&sync, true);
  741. amdgpu_sync_free(&sync);
  742. return r;
  743. }
  744. /*
  745. * amdgpu_vm_update_pde - update a single level in the hierarchy
  746. *
  747. * @param: parameters for the update
  748. * @vm: requested vm
  749. * @parent: parent directory
  750. * @entry: entry to update
  751. *
  752. * Makes sure the requested entry in parent is up to date.
  753. */
  754. static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
  755. struct amdgpu_vm *vm,
  756. struct amdgpu_vm_pt *parent,
  757. struct amdgpu_vm_pt *entry)
  758. {
  759. struct amdgpu_bo *bo = parent->base.bo, *pbo;
  760. uint64_t pde, pt, flags;
  761. unsigned level;
  762. /* Don't update huge pages here */
  763. if (entry->huge)
  764. return;
  765. for (level = 0, pbo = bo->parent; pbo; ++level)
  766. pbo = pbo->parent;
  767. level += params->adev->vm_manager.root_level;
  768. pt = amdgpu_bo_gpu_offset(entry->base.bo);
  769. flags = AMDGPU_PTE_VALID;
  770. amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
  771. pde = (entry - parent->entries) * 8;
  772. if (bo->shadow)
  773. params->func(params, bo->shadow, pde, pt, 1, 0, flags);
  774. params->func(params, bo, pde, pt, 1, 0, flags);
  775. }
  776. /*
  777. * amdgpu_vm_invalidate_level - mark all PD levels as invalid
  778. *
  779. * @parent: parent PD
  780. *
  781. * Mark all PD level as invalid after an error.
  782. */
  783. static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
  784. struct amdgpu_vm *vm,
  785. struct amdgpu_vm_pt *parent,
  786. unsigned level)
  787. {
  788. unsigned pt_idx, num_entries;
  789. /*
  790. * Recurse into the subdirectories. This recursion is harmless because
  791. * we only have a maximum of 5 layers.
  792. */
  793. num_entries = amdgpu_vm_num_entries(adev, level);
  794. for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
  795. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  796. if (!entry->base.bo)
  797. continue;
  798. if (!entry->base.moved)
  799. list_move(&entry->base.vm_status, &vm->relocated);
  800. amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
  801. }
  802. }
  803. /*
  804. * amdgpu_vm_update_directories - make sure that all directories are valid
  805. *
  806. * @adev: amdgpu_device pointer
  807. * @vm: requested vm
  808. *
  809. * Makes sure all directories are up to date.
  810. * Returns 0 for success, error for failure.
  811. */
  812. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  813. struct amdgpu_vm *vm)
  814. {
  815. struct amdgpu_pte_update_params params;
  816. struct amdgpu_job *job;
  817. unsigned ndw = 0;
  818. int r = 0;
  819. if (list_empty(&vm->relocated))
  820. return 0;
  821. restart:
  822. memset(&params, 0, sizeof(params));
  823. params.adev = adev;
  824. if (vm->use_cpu_for_update) {
  825. struct amdgpu_vm_bo_base *bo_base;
  826. list_for_each_entry(bo_base, &vm->relocated, vm_status) {
  827. r = amdgpu_bo_kmap(bo_base->bo, NULL);
  828. if (unlikely(r))
  829. return r;
  830. }
  831. r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
  832. if (unlikely(r))
  833. return r;
  834. params.func = amdgpu_vm_cpu_set_ptes;
  835. } else {
  836. ndw = 512 * 8;
  837. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  838. if (r)
  839. return r;
  840. params.ib = &job->ibs[0];
  841. params.func = amdgpu_vm_do_set_ptes;
  842. }
  843. while (!list_empty(&vm->relocated)) {
  844. struct amdgpu_vm_bo_base *bo_base, *parent;
  845. struct amdgpu_vm_pt *pt, *entry;
  846. struct amdgpu_bo *bo;
  847. bo_base = list_first_entry(&vm->relocated,
  848. struct amdgpu_vm_bo_base,
  849. vm_status);
  850. bo_base->moved = false;
  851. list_move(&bo_base->vm_status, &vm->idle);
  852. bo = bo_base->bo->parent;
  853. if (!bo)
  854. continue;
  855. parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
  856. bo_list);
  857. pt = container_of(parent, struct amdgpu_vm_pt, base);
  858. entry = container_of(bo_base, struct amdgpu_vm_pt, base);
  859. amdgpu_vm_update_pde(&params, vm, pt, entry);
  860. if (!vm->use_cpu_for_update &&
  861. (ndw - params.ib->length_dw) < 32)
  862. break;
  863. }
  864. if (vm->use_cpu_for_update) {
  865. /* Flush HDP */
  866. mb();
  867. amdgpu_asic_flush_hdp(adev, NULL);
  868. } else if (params.ib->length_dw == 0) {
  869. amdgpu_job_free(job);
  870. } else {
  871. struct amdgpu_bo *root = vm->root.base.bo;
  872. struct amdgpu_ring *ring;
  873. struct dma_fence *fence;
  874. ring = container_of(vm->entity.sched, struct amdgpu_ring,
  875. sched);
  876. amdgpu_ring_pad_ib(ring, params.ib);
  877. amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
  878. AMDGPU_FENCE_OWNER_VM, false);
  879. WARN_ON(params.ib->length_dw > ndw);
  880. r = amdgpu_job_submit(job, ring, &vm->entity,
  881. AMDGPU_FENCE_OWNER_VM, &fence);
  882. if (r)
  883. goto error;
  884. amdgpu_bo_fence(root, fence, true);
  885. dma_fence_put(vm->last_update);
  886. vm->last_update = fence;
  887. }
  888. if (!list_empty(&vm->relocated))
  889. goto restart;
  890. return 0;
  891. error:
  892. amdgpu_vm_invalidate_level(adev, vm, &vm->root,
  893. adev->vm_manager.root_level);
  894. amdgpu_job_free(job);
  895. return r;
  896. }
  897. /**
  898. * amdgpu_vm_find_entry - find the entry for an address
  899. *
  900. * @p: see amdgpu_pte_update_params definition
  901. * @addr: virtual address in question
  902. * @entry: resulting entry or NULL
  903. * @parent: parent entry
  904. *
  905. * Find the vm_pt entry and it's parent for the given address.
  906. */
  907. void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
  908. struct amdgpu_vm_pt **entry,
  909. struct amdgpu_vm_pt **parent)
  910. {
  911. unsigned level = p->adev->vm_manager.root_level;
  912. *parent = NULL;
  913. *entry = &p->vm->root;
  914. while ((*entry)->entries) {
  915. unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
  916. *parent = *entry;
  917. *entry = &(*entry)->entries[addr >> shift];
  918. addr &= (1ULL << shift) - 1;
  919. }
  920. if (level != AMDGPU_VM_PTB)
  921. *entry = NULL;
  922. }
  923. /**
  924. * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
  925. *
  926. * @p: see amdgpu_pte_update_params definition
  927. * @entry: vm_pt entry to check
  928. * @parent: parent entry
  929. * @nptes: number of PTEs updated with this operation
  930. * @dst: destination address where the PTEs should point to
  931. * @flags: access flags fro the PTEs
  932. *
  933. * Check if we can update the PD with a huge page.
  934. */
  935. static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
  936. struct amdgpu_vm_pt *entry,
  937. struct amdgpu_vm_pt *parent,
  938. unsigned nptes, uint64_t dst,
  939. uint64_t flags)
  940. {
  941. uint64_t pde;
  942. /* In the case of a mixed PT the PDE must point to it*/
  943. if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
  944. nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
  945. /* Set the huge page flag to stop scanning at this PDE */
  946. flags |= AMDGPU_PDE_PTE;
  947. }
  948. if (!(flags & AMDGPU_PDE_PTE)) {
  949. if (entry->huge) {
  950. /* Add the entry to the relocated list to update it. */
  951. entry->huge = false;
  952. list_move(&entry->base.vm_status, &p->vm->relocated);
  953. }
  954. return;
  955. }
  956. entry->huge = true;
  957. amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
  958. pde = (entry - parent->entries) * 8;
  959. if (parent->base.bo->shadow)
  960. p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
  961. p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
  962. }
  963. /**
  964. * amdgpu_vm_update_ptes - make sure that page tables are valid
  965. *
  966. * @params: see amdgpu_pte_update_params definition
  967. * @vm: requested vm
  968. * @start: start of GPU address range
  969. * @end: end of GPU address range
  970. * @dst: destination address to map to, the next dst inside the function
  971. * @flags: mapping flags
  972. *
  973. * Update the page tables in the range @start - @end.
  974. * Returns 0 for success, -EINVAL for failure.
  975. */
  976. static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  977. uint64_t start, uint64_t end,
  978. uint64_t dst, uint64_t flags)
  979. {
  980. struct amdgpu_device *adev = params->adev;
  981. const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
  982. uint64_t addr, pe_start;
  983. struct amdgpu_bo *pt;
  984. unsigned nptes;
  985. /* walk over the address space and update the page tables */
  986. for (addr = start; addr < end; addr += nptes,
  987. dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
  988. struct amdgpu_vm_pt *entry, *parent;
  989. amdgpu_vm_get_entry(params, addr, &entry, &parent);
  990. if (!entry)
  991. return -ENOENT;
  992. if ((addr & ~mask) == (end & ~mask))
  993. nptes = end - addr;
  994. else
  995. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  996. amdgpu_vm_handle_huge_pages(params, entry, parent,
  997. nptes, dst, flags);
  998. /* We don't need to update PTEs for huge pages */
  999. if (entry->huge)
  1000. continue;
  1001. pt = entry->base.bo;
  1002. pe_start = (addr & mask) * 8;
  1003. if (pt->shadow)
  1004. params->func(params, pt->shadow, pe_start, dst, nptes,
  1005. AMDGPU_GPU_PAGE_SIZE, flags);
  1006. params->func(params, pt, pe_start, dst, nptes,
  1007. AMDGPU_GPU_PAGE_SIZE, flags);
  1008. }
  1009. return 0;
  1010. }
  1011. /*
  1012. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  1013. *
  1014. * @params: see amdgpu_pte_update_params definition
  1015. * @vm: requested vm
  1016. * @start: first PTE to handle
  1017. * @end: last PTE to handle
  1018. * @dst: addr those PTEs should point to
  1019. * @flags: hw mapping flags
  1020. * Returns 0 for success, -EINVAL for failure.
  1021. */
  1022. static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  1023. uint64_t start, uint64_t end,
  1024. uint64_t dst, uint64_t flags)
  1025. {
  1026. /**
  1027. * The MC L1 TLB supports variable sized pages, based on a fragment
  1028. * field in the PTE. When this field is set to a non-zero value, page
  1029. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  1030. * flags are considered valid for all PTEs within the fragment range
  1031. * and corresponding mappings are assumed to be physically contiguous.
  1032. *
  1033. * The L1 TLB can store a single PTE for the whole fragment,
  1034. * significantly increasing the space available for translation
  1035. * caching. This leads to large improvements in throughput when the
  1036. * TLB is under pressure.
  1037. *
  1038. * The L2 TLB distributes small and large fragments into two
  1039. * asymmetric partitions. The large fragment cache is significantly
  1040. * larger. Thus, we try to use large fragments wherever possible.
  1041. * Userspace can support this by aligning virtual base address and
  1042. * allocation size to the fragment size.
  1043. */
  1044. unsigned max_frag = params->adev->vm_manager.fragment_size;
  1045. int r;
  1046. /* system pages are non continuously */
  1047. if (params->src || !(flags & AMDGPU_PTE_VALID))
  1048. return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  1049. while (start != end) {
  1050. uint64_t frag_flags, frag_end;
  1051. unsigned frag;
  1052. /* This intentionally wraps around if no bit is set */
  1053. frag = min((unsigned)ffs(start) - 1,
  1054. (unsigned)fls64(end - start) - 1);
  1055. if (frag >= max_frag) {
  1056. frag_flags = AMDGPU_PTE_FRAG(max_frag);
  1057. frag_end = end & ~((1ULL << max_frag) - 1);
  1058. } else {
  1059. frag_flags = AMDGPU_PTE_FRAG(frag);
  1060. frag_end = start + (1 << frag);
  1061. }
  1062. r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
  1063. flags | frag_flags);
  1064. if (r)
  1065. return r;
  1066. dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
  1067. start = frag_end;
  1068. }
  1069. return 0;
  1070. }
  1071. /**
  1072. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  1073. *
  1074. * @adev: amdgpu_device pointer
  1075. * @exclusive: fence we need to sync to
  1076. * @pages_addr: DMA addresses to use for mapping
  1077. * @vm: requested vm
  1078. * @start: start of mapped range
  1079. * @last: last mapped entry
  1080. * @flags: flags for the entries
  1081. * @addr: addr to set the area to
  1082. * @fence: optional resulting fence
  1083. *
  1084. * Fill in the page table entries between @start and @last.
  1085. * Returns 0 for success, -EINVAL for failure.
  1086. */
  1087. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  1088. struct dma_fence *exclusive,
  1089. dma_addr_t *pages_addr,
  1090. struct amdgpu_vm *vm,
  1091. uint64_t start, uint64_t last,
  1092. uint64_t flags, uint64_t addr,
  1093. struct dma_fence **fence)
  1094. {
  1095. struct amdgpu_ring *ring;
  1096. void *owner = AMDGPU_FENCE_OWNER_VM;
  1097. unsigned nptes, ncmds, ndw;
  1098. struct amdgpu_job *job;
  1099. struct amdgpu_pte_update_params params;
  1100. struct dma_fence *f = NULL;
  1101. int r;
  1102. memset(&params, 0, sizeof(params));
  1103. params.adev = adev;
  1104. params.vm = vm;
  1105. /* sync to everything on unmapping */
  1106. if (!(flags & AMDGPU_PTE_VALID))
  1107. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  1108. if (vm->use_cpu_for_update) {
  1109. /* params.src is used as flag to indicate system Memory */
  1110. if (pages_addr)
  1111. params.src = ~0;
  1112. /* Wait for PT BOs to be free. PTs share the same resv. object
  1113. * as the root PD BO
  1114. */
  1115. r = amdgpu_vm_wait_pd(adev, vm, owner);
  1116. if (unlikely(r))
  1117. return r;
  1118. params.func = amdgpu_vm_cpu_set_ptes;
  1119. params.pages_addr = pages_addr;
  1120. return amdgpu_vm_frag_ptes(&params, start, last + 1,
  1121. addr, flags);
  1122. }
  1123. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  1124. nptes = last - start + 1;
  1125. /*
  1126. * reserve space for two commands every (1 << BLOCK_SIZE)
  1127. * entries or 2k dwords (whatever is smaller)
  1128. *
  1129. * The second command is for the shadow pagetables.
  1130. */
  1131. if (vm->root.base.bo->shadow)
  1132. ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
  1133. else
  1134. ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
  1135. /* padding, etc. */
  1136. ndw = 64;
  1137. if (pages_addr) {
  1138. /* copy commands needed */
  1139. ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
  1140. /* and also PTEs */
  1141. ndw += nptes * 2;
  1142. params.func = amdgpu_vm_do_copy_ptes;
  1143. } else {
  1144. /* set page commands needed */
  1145. ndw += ncmds * 10;
  1146. /* extra commands for begin/end fragments */
  1147. if (vm->root.base.bo->shadow)
  1148. ndw += 2 * 10 * adev->vm_manager.fragment_size * 2;
  1149. else
  1150. ndw += 2 * 10 * adev->vm_manager.fragment_size;
  1151. params.func = amdgpu_vm_do_set_ptes;
  1152. }
  1153. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  1154. if (r)
  1155. return r;
  1156. params.ib = &job->ibs[0];
  1157. if (pages_addr) {
  1158. uint64_t *pte;
  1159. unsigned i;
  1160. /* Put the PTEs at the end of the IB. */
  1161. i = ndw - nptes * 2;
  1162. pte= (uint64_t *)&(job->ibs->ptr[i]);
  1163. params.src = job->ibs->gpu_addr + i * 4;
  1164. for (i = 0; i < nptes; ++i) {
  1165. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  1166. AMDGPU_GPU_PAGE_SIZE);
  1167. pte[i] |= flags;
  1168. }
  1169. addr = 0;
  1170. }
  1171. r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
  1172. if (r)
  1173. goto error_free;
  1174. r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
  1175. owner, false);
  1176. if (r)
  1177. goto error_free;
  1178. r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
  1179. if (r)
  1180. goto error_free;
  1181. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1182. if (r)
  1183. goto error_free;
  1184. amdgpu_ring_pad_ib(ring, params.ib);
  1185. WARN_ON(params.ib->length_dw > ndw);
  1186. r = amdgpu_job_submit(job, ring, &vm->entity,
  1187. AMDGPU_FENCE_OWNER_VM, &f);
  1188. if (r)
  1189. goto error_free;
  1190. amdgpu_bo_fence(vm->root.base.bo, f, true);
  1191. dma_fence_put(*fence);
  1192. *fence = f;
  1193. return 0;
  1194. error_free:
  1195. amdgpu_job_free(job);
  1196. return r;
  1197. }
  1198. /**
  1199. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1200. *
  1201. * @adev: amdgpu_device pointer
  1202. * @exclusive: fence we need to sync to
  1203. * @pages_addr: DMA addresses to use for mapping
  1204. * @vm: requested vm
  1205. * @mapping: mapped range and flags to use for the update
  1206. * @flags: HW flags for the mapping
  1207. * @nodes: array of drm_mm_nodes with the MC addresses
  1208. * @fence: optional resulting fence
  1209. *
  1210. * Split the mapping into smaller chunks so that each update fits
  1211. * into a SDMA IB.
  1212. * Returns 0 for success, -EINVAL for failure.
  1213. */
  1214. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1215. struct dma_fence *exclusive,
  1216. dma_addr_t *pages_addr,
  1217. struct amdgpu_vm *vm,
  1218. struct amdgpu_bo_va_mapping *mapping,
  1219. uint64_t flags,
  1220. struct drm_mm_node *nodes,
  1221. struct dma_fence **fence)
  1222. {
  1223. unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
  1224. uint64_t pfn, start = mapping->start;
  1225. int r;
  1226. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1227. * but in case of something, we filter the flags in first place
  1228. */
  1229. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1230. flags &= ~AMDGPU_PTE_READABLE;
  1231. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1232. flags &= ~AMDGPU_PTE_WRITEABLE;
  1233. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1234. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1235. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1236. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1237. if ((mapping->flags & AMDGPU_PTE_PRT) &&
  1238. (adev->asic_type >= CHIP_VEGA10)) {
  1239. flags |= AMDGPU_PTE_PRT;
  1240. flags &= ~AMDGPU_PTE_VALID;
  1241. }
  1242. trace_amdgpu_vm_bo_update(mapping);
  1243. pfn = mapping->offset >> PAGE_SHIFT;
  1244. if (nodes) {
  1245. while (pfn >= nodes->size) {
  1246. pfn -= nodes->size;
  1247. ++nodes;
  1248. }
  1249. }
  1250. do {
  1251. dma_addr_t *dma_addr = NULL;
  1252. uint64_t max_entries;
  1253. uint64_t addr, last;
  1254. if (nodes) {
  1255. addr = nodes->start << PAGE_SHIFT;
  1256. max_entries = (nodes->size - pfn) *
  1257. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1258. } else {
  1259. addr = 0;
  1260. max_entries = S64_MAX;
  1261. }
  1262. if (pages_addr) {
  1263. uint64_t count;
  1264. max_entries = min(max_entries, 16ull * 1024ull);
  1265. for (count = 1; count < max_entries; ++count) {
  1266. uint64_t idx = pfn + count;
  1267. if (pages_addr[idx] !=
  1268. (pages_addr[idx - 1] + PAGE_SIZE))
  1269. break;
  1270. }
  1271. if (count < min_linear_pages) {
  1272. addr = pfn << PAGE_SHIFT;
  1273. dma_addr = pages_addr;
  1274. } else {
  1275. addr = pages_addr[pfn];
  1276. max_entries = count;
  1277. }
  1278. } else if (flags & AMDGPU_PTE_VALID) {
  1279. addr += adev->vm_manager.vram_base_offset;
  1280. addr += pfn << PAGE_SHIFT;
  1281. }
  1282. last = min((uint64_t)mapping->last, start + max_entries - 1);
  1283. r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
  1284. start, last, flags, addr,
  1285. fence);
  1286. if (r)
  1287. return r;
  1288. pfn += last - start + 1;
  1289. if (nodes && nodes->size == pfn) {
  1290. pfn = 0;
  1291. ++nodes;
  1292. }
  1293. start = last + 1;
  1294. } while (unlikely(start != mapping->last + 1));
  1295. return 0;
  1296. }
  1297. /**
  1298. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1299. *
  1300. * @adev: amdgpu_device pointer
  1301. * @bo_va: requested BO and VM object
  1302. * @clear: if true clear the entries
  1303. *
  1304. * Fill in the page table entries for @bo_va.
  1305. * Returns 0 for success, -EINVAL for failure.
  1306. */
  1307. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1308. struct amdgpu_bo_va *bo_va,
  1309. bool clear)
  1310. {
  1311. struct amdgpu_bo *bo = bo_va->base.bo;
  1312. struct amdgpu_vm *vm = bo_va->base.vm;
  1313. struct amdgpu_bo_va_mapping *mapping;
  1314. dma_addr_t *pages_addr = NULL;
  1315. struct ttm_mem_reg *mem;
  1316. struct drm_mm_node *nodes;
  1317. struct dma_fence *exclusive, **last_update;
  1318. uint64_t flags;
  1319. int r;
  1320. if (clear || !bo_va->base.bo) {
  1321. mem = NULL;
  1322. nodes = NULL;
  1323. exclusive = NULL;
  1324. } else {
  1325. struct ttm_dma_tt *ttm;
  1326. mem = &bo_va->base.bo->tbo.mem;
  1327. nodes = mem->mm_node;
  1328. if (mem->mem_type == TTM_PL_TT) {
  1329. ttm = container_of(bo_va->base.bo->tbo.ttm,
  1330. struct ttm_dma_tt, ttm);
  1331. pages_addr = ttm->dma_address;
  1332. }
  1333. exclusive = reservation_object_get_excl(bo->tbo.resv);
  1334. }
  1335. if (bo)
  1336. flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
  1337. else
  1338. flags = 0x0;
  1339. if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
  1340. last_update = &vm->last_update;
  1341. else
  1342. last_update = &bo_va->last_pt_update;
  1343. if (!clear && bo_va->base.moved) {
  1344. bo_va->base.moved = false;
  1345. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1346. } else if (bo_va->cleared != clear) {
  1347. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1348. }
  1349. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1350. r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
  1351. mapping, flags, nodes,
  1352. last_update);
  1353. if (r)
  1354. return r;
  1355. }
  1356. if (vm->use_cpu_for_update) {
  1357. /* Flush HDP */
  1358. mb();
  1359. amdgpu_asic_flush_hdp(adev, NULL);
  1360. }
  1361. spin_lock(&vm->moved_lock);
  1362. list_del_init(&bo_va->base.vm_status);
  1363. spin_unlock(&vm->moved_lock);
  1364. /* If the BO is not in its preferred location add it back to
  1365. * the evicted list so that it gets validated again on the
  1366. * next command submission.
  1367. */
  1368. if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1369. uint32_t mem_type = bo->tbo.mem.mem_type;
  1370. if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type)))
  1371. list_add_tail(&bo_va->base.vm_status, &vm->evicted);
  1372. else
  1373. list_add(&bo_va->base.vm_status, &vm->idle);
  1374. }
  1375. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1376. bo_va->cleared = clear;
  1377. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1378. list_for_each_entry(mapping, &bo_va->valids, list)
  1379. trace_amdgpu_vm_bo_mapping(mapping);
  1380. }
  1381. return 0;
  1382. }
  1383. /**
  1384. * amdgpu_vm_update_prt_state - update the global PRT state
  1385. */
  1386. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1387. {
  1388. unsigned long flags;
  1389. bool enable;
  1390. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1391. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1392. adev->gmc.gmc_funcs->set_prt(adev, enable);
  1393. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1394. }
  1395. /**
  1396. * amdgpu_vm_prt_get - add a PRT user
  1397. */
  1398. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1399. {
  1400. if (!adev->gmc.gmc_funcs->set_prt)
  1401. return;
  1402. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1403. amdgpu_vm_update_prt_state(adev);
  1404. }
  1405. /**
  1406. * amdgpu_vm_prt_put - drop a PRT user
  1407. */
  1408. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1409. {
  1410. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1411. amdgpu_vm_update_prt_state(adev);
  1412. }
  1413. /**
  1414. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1415. */
  1416. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1417. {
  1418. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1419. amdgpu_vm_prt_put(cb->adev);
  1420. kfree(cb);
  1421. }
  1422. /**
  1423. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1424. */
  1425. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1426. struct dma_fence *fence)
  1427. {
  1428. struct amdgpu_prt_cb *cb;
  1429. if (!adev->gmc.gmc_funcs->set_prt)
  1430. return;
  1431. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1432. if (!cb) {
  1433. /* Last resort when we are OOM */
  1434. if (fence)
  1435. dma_fence_wait(fence, false);
  1436. amdgpu_vm_prt_put(adev);
  1437. } else {
  1438. cb->adev = adev;
  1439. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1440. amdgpu_vm_prt_cb))
  1441. amdgpu_vm_prt_cb(fence, &cb->cb);
  1442. }
  1443. }
  1444. /**
  1445. * amdgpu_vm_free_mapping - free a mapping
  1446. *
  1447. * @adev: amdgpu_device pointer
  1448. * @vm: requested vm
  1449. * @mapping: mapping to be freed
  1450. * @fence: fence of the unmap operation
  1451. *
  1452. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1453. */
  1454. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1455. struct amdgpu_vm *vm,
  1456. struct amdgpu_bo_va_mapping *mapping,
  1457. struct dma_fence *fence)
  1458. {
  1459. if (mapping->flags & AMDGPU_PTE_PRT)
  1460. amdgpu_vm_add_prt_cb(adev, fence);
  1461. kfree(mapping);
  1462. }
  1463. /**
  1464. * amdgpu_vm_prt_fini - finish all prt mappings
  1465. *
  1466. * @adev: amdgpu_device pointer
  1467. * @vm: requested vm
  1468. *
  1469. * Register a cleanup callback to disable PRT support after VM dies.
  1470. */
  1471. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1472. {
  1473. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  1474. struct dma_fence *excl, **shared;
  1475. unsigned i, shared_count;
  1476. int r;
  1477. r = reservation_object_get_fences_rcu(resv, &excl,
  1478. &shared_count, &shared);
  1479. if (r) {
  1480. /* Not enough memory to grab the fence list, as last resort
  1481. * block for all the fences to complete.
  1482. */
  1483. reservation_object_wait_timeout_rcu(resv, true, false,
  1484. MAX_SCHEDULE_TIMEOUT);
  1485. return;
  1486. }
  1487. /* Add a callback for each fence in the reservation object */
  1488. amdgpu_vm_prt_get(adev);
  1489. amdgpu_vm_add_prt_cb(adev, excl);
  1490. for (i = 0; i < shared_count; ++i) {
  1491. amdgpu_vm_prt_get(adev);
  1492. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1493. }
  1494. kfree(shared);
  1495. }
  1496. /**
  1497. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1498. *
  1499. * @adev: amdgpu_device pointer
  1500. * @vm: requested vm
  1501. * @fence: optional resulting fence (unchanged if no work needed to be done
  1502. * or if an error occurred)
  1503. *
  1504. * Make sure all freed BOs are cleared in the PT.
  1505. * Returns 0 for success.
  1506. *
  1507. * PTs have to be reserved and mutex must be locked!
  1508. */
  1509. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1510. struct amdgpu_vm *vm,
  1511. struct dma_fence **fence)
  1512. {
  1513. struct amdgpu_bo_va_mapping *mapping;
  1514. uint64_t init_pte_value = 0;
  1515. struct dma_fence *f = NULL;
  1516. int r;
  1517. while (!list_empty(&vm->freed)) {
  1518. mapping = list_first_entry(&vm->freed,
  1519. struct amdgpu_bo_va_mapping, list);
  1520. list_del(&mapping->list);
  1521. if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
  1522. init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
  1523. r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
  1524. mapping->start, mapping->last,
  1525. init_pte_value, 0, &f);
  1526. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1527. if (r) {
  1528. dma_fence_put(f);
  1529. return r;
  1530. }
  1531. }
  1532. if (fence && f) {
  1533. dma_fence_put(*fence);
  1534. *fence = f;
  1535. } else {
  1536. dma_fence_put(f);
  1537. }
  1538. return 0;
  1539. }
  1540. /**
  1541. * amdgpu_vm_handle_moved - handle moved BOs in the PT
  1542. *
  1543. * @adev: amdgpu_device pointer
  1544. * @vm: requested vm
  1545. * @sync: sync object to add fences to
  1546. *
  1547. * Make sure all BOs which are moved are updated in the PTs.
  1548. * Returns 0 for success.
  1549. *
  1550. * PTs have to be reserved!
  1551. */
  1552. int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  1553. struct amdgpu_vm *vm)
  1554. {
  1555. struct amdgpu_bo_va *bo_va, *tmp;
  1556. struct list_head moved;
  1557. bool clear;
  1558. int r;
  1559. INIT_LIST_HEAD(&moved);
  1560. spin_lock(&vm->moved_lock);
  1561. list_splice_init(&vm->moved, &moved);
  1562. spin_unlock(&vm->moved_lock);
  1563. list_for_each_entry_safe(bo_va, tmp, &moved, base.vm_status) {
  1564. struct reservation_object *resv = bo_va->base.bo->tbo.resv;
  1565. /* Per VM BOs never need to bo cleared in the page tables */
  1566. if (resv == vm->root.base.bo->tbo.resv)
  1567. clear = false;
  1568. /* Try to reserve the BO to avoid clearing its ptes */
  1569. else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
  1570. clear = false;
  1571. /* Somebody else is using the BO right now */
  1572. else
  1573. clear = true;
  1574. r = amdgpu_vm_bo_update(adev, bo_va, clear);
  1575. if (r) {
  1576. spin_lock(&vm->moved_lock);
  1577. list_splice(&moved, &vm->moved);
  1578. spin_unlock(&vm->moved_lock);
  1579. return r;
  1580. }
  1581. if (!clear && resv != vm->root.base.bo->tbo.resv)
  1582. reservation_object_unlock(resv);
  1583. }
  1584. return 0;
  1585. }
  1586. /**
  1587. * amdgpu_vm_bo_add - add a bo to a specific vm
  1588. *
  1589. * @adev: amdgpu_device pointer
  1590. * @vm: requested vm
  1591. * @bo: amdgpu buffer object
  1592. *
  1593. * Add @bo into the requested vm.
  1594. * Add @bo to the list of bos associated with the vm
  1595. * Returns newly added bo_va or NULL for failure
  1596. *
  1597. * Object has to be reserved!
  1598. */
  1599. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1600. struct amdgpu_vm *vm,
  1601. struct amdgpu_bo *bo)
  1602. {
  1603. struct amdgpu_bo_va *bo_va;
  1604. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1605. if (bo_va == NULL) {
  1606. return NULL;
  1607. }
  1608. amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
  1609. bo_va->ref_count = 1;
  1610. INIT_LIST_HEAD(&bo_va->valids);
  1611. INIT_LIST_HEAD(&bo_va->invalids);
  1612. return bo_va;
  1613. }
  1614. /**
  1615. * amdgpu_vm_bo_insert_mapping - insert a new mapping
  1616. *
  1617. * @adev: amdgpu_device pointer
  1618. * @bo_va: bo_va to store the address
  1619. * @mapping: the mapping to insert
  1620. *
  1621. * Insert a new mapping into all structures.
  1622. */
  1623. static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
  1624. struct amdgpu_bo_va *bo_va,
  1625. struct amdgpu_bo_va_mapping *mapping)
  1626. {
  1627. struct amdgpu_vm *vm = bo_va->base.vm;
  1628. struct amdgpu_bo *bo = bo_va->base.bo;
  1629. mapping->bo_va = bo_va;
  1630. list_add(&mapping->list, &bo_va->invalids);
  1631. amdgpu_vm_it_insert(mapping, &vm->va);
  1632. if (mapping->flags & AMDGPU_PTE_PRT)
  1633. amdgpu_vm_prt_get(adev);
  1634. if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
  1635. !bo_va->base.moved) {
  1636. spin_lock(&vm->moved_lock);
  1637. list_move(&bo_va->base.vm_status, &vm->moved);
  1638. spin_unlock(&vm->moved_lock);
  1639. }
  1640. trace_amdgpu_vm_bo_map(bo_va, mapping);
  1641. }
  1642. /**
  1643. * amdgpu_vm_bo_map - map bo inside a vm
  1644. *
  1645. * @adev: amdgpu_device pointer
  1646. * @bo_va: bo_va to store the address
  1647. * @saddr: where to map the BO
  1648. * @offset: requested offset in the BO
  1649. * @flags: attributes of pages (read/write/valid/etc.)
  1650. *
  1651. * Add a mapping of the BO at the specefied addr into the VM.
  1652. * Returns 0 for success, error for failure.
  1653. *
  1654. * Object has to be reserved and unreserved outside!
  1655. */
  1656. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1657. struct amdgpu_bo_va *bo_va,
  1658. uint64_t saddr, uint64_t offset,
  1659. uint64_t size, uint64_t flags)
  1660. {
  1661. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1662. struct amdgpu_bo *bo = bo_va->base.bo;
  1663. struct amdgpu_vm *vm = bo_va->base.vm;
  1664. uint64_t eaddr;
  1665. /* validate the parameters */
  1666. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1667. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1668. return -EINVAL;
  1669. /* make sure object fit at this offset */
  1670. eaddr = saddr + size - 1;
  1671. if (saddr >= eaddr ||
  1672. (bo && offset + size > amdgpu_bo_size(bo)))
  1673. return -EINVAL;
  1674. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1675. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1676. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1677. if (tmp) {
  1678. /* bo and tmp overlap, invalid addr */
  1679. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1680. "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
  1681. tmp->start, tmp->last + 1);
  1682. return -EINVAL;
  1683. }
  1684. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1685. if (!mapping)
  1686. return -ENOMEM;
  1687. mapping->start = saddr;
  1688. mapping->last = eaddr;
  1689. mapping->offset = offset;
  1690. mapping->flags = flags;
  1691. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1692. return 0;
  1693. }
  1694. /**
  1695. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1696. *
  1697. * @adev: amdgpu_device pointer
  1698. * @bo_va: bo_va to store the address
  1699. * @saddr: where to map the BO
  1700. * @offset: requested offset in the BO
  1701. * @flags: attributes of pages (read/write/valid/etc.)
  1702. *
  1703. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1704. * mappings as we do so.
  1705. * Returns 0 for success, error for failure.
  1706. *
  1707. * Object has to be reserved and unreserved outside!
  1708. */
  1709. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1710. struct amdgpu_bo_va *bo_va,
  1711. uint64_t saddr, uint64_t offset,
  1712. uint64_t size, uint64_t flags)
  1713. {
  1714. struct amdgpu_bo_va_mapping *mapping;
  1715. struct amdgpu_bo *bo = bo_va->base.bo;
  1716. uint64_t eaddr;
  1717. int r;
  1718. /* validate the parameters */
  1719. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1720. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1721. return -EINVAL;
  1722. /* make sure object fit at this offset */
  1723. eaddr = saddr + size - 1;
  1724. if (saddr >= eaddr ||
  1725. (bo && offset + size > amdgpu_bo_size(bo)))
  1726. return -EINVAL;
  1727. /* Allocate all the needed memory */
  1728. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1729. if (!mapping)
  1730. return -ENOMEM;
  1731. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
  1732. if (r) {
  1733. kfree(mapping);
  1734. return r;
  1735. }
  1736. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1737. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1738. mapping->start = saddr;
  1739. mapping->last = eaddr;
  1740. mapping->offset = offset;
  1741. mapping->flags = flags;
  1742. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1743. return 0;
  1744. }
  1745. /**
  1746. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1747. *
  1748. * @adev: amdgpu_device pointer
  1749. * @bo_va: bo_va to remove the address from
  1750. * @saddr: where to the BO is mapped
  1751. *
  1752. * Remove a mapping of the BO at the specefied addr from the VM.
  1753. * Returns 0 for success, error for failure.
  1754. *
  1755. * Object has to be reserved and unreserved outside!
  1756. */
  1757. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1758. struct amdgpu_bo_va *bo_va,
  1759. uint64_t saddr)
  1760. {
  1761. struct amdgpu_bo_va_mapping *mapping;
  1762. struct amdgpu_vm *vm = bo_va->base.vm;
  1763. bool valid = true;
  1764. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1765. list_for_each_entry(mapping, &bo_va->valids, list) {
  1766. if (mapping->start == saddr)
  1767. break;
  1768. }
  1769. if (&mapping->list == &bo_va->valids) {
  1770. valid = false;
  1771. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1772. if (mapping->start == saddr)
  1773. break;
  1774. }
  1775. if (&mapping->list == &bo_va->invalids)
  1776. return -ENOENT;
  1777. }
  1778. list_del(&mapping->list);
  1779. amdgpu_vm_it_remove(mapping, &vm->va);
  1780. mapping->bo_va = NULL;
  1781. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1782. if (valid)
  1783. list_add(&mapping->list, &vm->freed);
  1784. else
  1785. amdgpu_vm_free_mapping(adev, vm, mapping,
  1786. bo_va->last_pt_update);
  1787. return 0;
  1788. }
  1789. /**
  1790. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  1791. *
  1792. * @adev: amdgpu_device pointer
  1793. * @vm: VM structure to use
  1794. * @saddr: start of the range
  1795. * @size: size of the range
  1796. *
  1797. * Remove all mappings in a range, split them as appropriate.
  1798. * Returns 0 for success, error for failure.
  1799. */
  1800. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  1801. struct amdgpu_vm *vm,
  1802. uint64_t saddr, uint64_t size)
  1803. {
  1804. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  1805. LIST_HEAD(removed);
  1806. uint64_t eaddr;
  1807. eaddr = saddr + size - 1;
  1808. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1809. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1810. /* Allocate all the needed memory */
  1811. before = kzalloc(sizeof(*before), GFP_KERNEL);
  1812. if (!before)
  1813. return -ENOMEM;
  1814. INIT_LIST_HEAD(&before->list);
  1815. after = kzalloc(sizeof(*after), GFP_KERNEL);
  1816. if (!after) {
  1817. kfree(before);
  1818. return -ENOMEM;
  1819. }
  1820. INIT_LIST_HEAD(&after->list);
  1821. /* Now gather all removed mappings */
  1822. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1823. while (tmp) {
  1824. /* Remember mapping split at the start */
  1825. if (tmp->start < saddr) {
  1826. before->start = tmp->start;
  1827. before->last = saddr - 1;
  1828. before->offset = tmp->offset;
  1829. before->flags = tmp->flags;
  1830. before->bo_va = tmp->bo_va;
  1831. list_add(&before->list, &tmp->bo_va->invalids);
  1832. }
  1833. /* Remember mapping split at the end */
  1834. if (tmp->last > eaddr) {
  1835. after->start = eaddr + 1;
  1836. after->last = tmp->last;
  1837. after->offset = tmp->offset;
  1838. after->offset += after->start - tmp->start;
  1839. after->flags = tmp->flags;
  1840. after->bo_va = tmp->bo_va;
  1841. list_add(&after->list, &tmp->bo_va->invalids);
  1842. }
  1843. list_del(&tmp->list);
  1844. list_add(&tmp->list, &removed);
  1845. tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
  1846. }
  1847. /* And free them up */
  1848. list_for_each_entry_safe(tmp, next, &removed, list) {
  1849. amdgpu_vm_it_remove(tmp, &vm->va);
  1850. list_del(&tmp->list);
  1851. if (tmp->start < saddr)
  1852. tmp->start = saddr;
  1853. if (tmp->last > eaddr)
  1854. tmp->last = eaddr;
  1855. tmp->bo_va = NULL;
  1856. list_add(&tmp->list, &vm->freed);
  1857. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  1858. }
  1859. /* Insert partial mapping before the range */
  1860. if (!list_empty(&before->list)) {
  1861. amdgpu_vm_it_insert(before, &vm->va);
  1862. if (before->flags & AMDGPU_PTE_PRT)
  1863. amdgpu_vm_prt_get(adev);
  1864. } else {
  1865. kfree(before);
  1866. }
  1867. /* Insert partial mapping after the range */
  1868. if (!list_empty(&after->list)) {
  1869. amdgpu_vm_it_insert(after, &vm->va);
  1870. if (after->flags & AMDGPU_PTE_PRT)
  1871. amdgpu_vm_prt_get(adev);
  1872. } else {
  1873. kfree(after);
  1874. }
  1875. return 0;
  1876. }
  1877. /**
  1878. * amdgpu_vm_bo_lookup_mapping - find mapping by address
  1879. *
  1880. * @vm: the requested VM
  1881. *
  1882. * Find a mapping by it's address.
  1883. */
  1884. struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
  1885. uint64_t addr)
  1886. {
  1887. return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
  1888. }
  1889. /**
  1890. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1891. *
  1892. * @adev: amdgpu_device pointer
  1893. * @bo_va: requested bo_va
  1894. *
  1895. * Remove @bo_va->bo from the requested vm.
  1896. *
  1897. * Object have to be reserved!
  1898. */
  1899. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1900. struct amdgpu_bo_va *bo_va)
  1901. {
  1902. struct amdgpu_bo_va_mapping *mapping, *next;
  1903. struct amdgpu_vm *vm = bo_va->base.vm;
  1904. list_del(&bo_va->base.bo_list);
  1905. spin_lock(&vm->moved_lock);
  1906. list_del(&bo_va->base.vm_status);
  1907. spin_unlock(&vm->moved_lock);
  1908. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1909. list_del(&mapping->list);
  1910. amdgpu_vm_it_remove(mapping, &vm->va);
  1911. mapping->bo_va = NULL;
  1912. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1913. list_add(&mapping->list, &vm->freed);
  1914. }
  1915. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1916. list_del(&mapping->list);
  1917. amdgpu_vm_it_remove(mapping, &vm->va);
  1918. amdgpu_vm_free_mapping(adev, vm, mapping,
  1919. bo_va->last_pt_update);
  1920. }
  1921. dma_fence_put(bo_va->last_pt_update);
  1922. kfree(bo_va);
  1923. }
  1924. /**
  1925. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1926. *
  1927. * @adev: amdgpu_device pointer
  1928. * @vm: requested vm
  1929. * @bo: amdgpu buffer object
  1930. *
  1931. * Mark @bo as invalid.
  1932. */
  1933. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1934. struct amdgpu_bo *bo, bool evicted)
  1935. {
  1936. struct amdgpu_vm_bo_base *bo_base;
  1937. /* shadow bo doesn't have bo base, its validation needs its parent */
  1938. if (bo->parent && bo->parent->shadow == bo)
  1939. bo = bo->parent;
  1940. list_for_each_entry(bo_base, &bo->va, bo_list) {
  1941. struct amdgpu_vm *vm = bo_base->vm;
  1942. bool was_moved = bo_base->moved;
  1943. bo_base->moved = true;
  1944. if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1945. if (bo->tbo.type == ttm_bo_type_kernel)
  1946. list_move(&bo_base->vm_status, &vm->evicted);
  1947. else
  1948. list_move_tail(&bo_base->vm_status,
  1949. &vm->evicted);
  1950. continue;
  1951. }
  1952. if (was_moved)
  1953. continue;
  1954. if (bo->tbo.type == ttm_bo_type_kernel) {
  1955. list_move(&bo_base->vm_status, &vm->relocated);
  1956. } else {
  1957. spin_lock(&bo_base->vm->moved_lock);
  1958. list_move(&bo_base->vm_status, &vm->moved);
  1959. spin_unlock(&bo_base->vm->moved_lock);
  1960. }
  1961. }
  1962. }
  1963. static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
  1964. {
  1965. /* Total bits covered by PD + PTs */
  1966. unsigned bits = ilog2(vm_size) + 18;
  1967. /* Make sure the PD is 4K in size up to 8GB address space.
  1968. Above that split equal between PD and PTs */
  1969. if (vm_size <= 8)
  1970. return (bits - 9);
  1971. else
  1972. return ((bits + 3) / 2);
  1973. }
  1974. /**
  1975. * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
  1976. *
  1977. * @adev: amdgpu_device pointer
  1978. * @vm_size: the default vm size if it's set auto
  1979. */
  1980. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
  1981. uint32_t fragment_size_default, unsigned max_level,
  1982. unsigned max_bits)
  1983. {
  1984. uint64_t tmp;
  1985. /* adjust vm size first */
  1986. if (amdgpu_vm_size != -1) {
  1987. unsigned max_size = 1 << (max_bits - 30);
  1988. vm_size = amdgpu_vm_size;
  1989. if (vm_size > max_size) {
  1990. dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
  1991. amdgpu_vm_size, max_size);
  1992. vm_size = max_size;
  1993. }
  1994. }
  1995. adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
  1996. tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
  1997. if (amdgpu_vm_block_size != -1)
  1998. tmp >>= amdgpu_vm_block_size - 9;
  1999. tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
  2000. adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
  2001. switch (adev->vm_manager.num_level) {
  2002. case 3:
  2003. adev->vm_manager.root_level = AMDGPU_VM_PDB2;
  2004. break;
  2005. case 2:
  2006. adev->vm_manager.root_level = AMDGPU_VM_PDB1;
  2007. break;
  2008. case 1:
  2009. adev->vm_manager.root_level = AMDGPU_VM_PDB0;
  2010. break;
  2011. default:
  2012. dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
  2013. }
  2014. /* block size depends on vm size and hw setup*/
  2015. if (amdgpu_vm_block_size != -1)
  2016. adev->vm_manager.block_size =
  2017. min((unsigned)amdgpu_vm_block_size, max_bits
  2018. - AMDGPU_GPU_PAGE_SHIFT
  2019. - 9 * adev->vm_manager.num_level);
  2020. else if (adev->vm_manager.num_level > 1)
  2021. adev->vm_manager.block_size = 9;
  2022. else
  2023. adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
  2024. if (amdgpu_vm_fragment_size == -1)
  2025. adev->vm_manager.fragment_size = fragment_size_default;
  2026. else
  2027. adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
  2028. DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
  2029. vm_size, adev->vm_manager.num_level + 1,
  2030. adev->vm_manager.block_size,
  2031. adev->vm_manager.fragment_size);
  2032. }
  2033. /**
  2034. * amdgpu_vm_init - initialize a vm instance
  2035. *
  2036. * @adev: amdgpu_device pointer
  2037. * @vm: requested vm
  2038. * @vm_context: Indicates if it GFX or Compute context
  2039. *
  2040. * Init @vm fields.
  2041. */
  2042. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  2043. int vm_context, unsigned int pasid)
  2044. {
  2045. struct amdgpu_bo_param bp;
  2046. struct amdgpu_bo *root;
  2047. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  2048. AMDGPU_VM_PTE_COUNT(adev) * 8);
  2049. unsigned ring_instance;
  2050. struct amdgpu_ring *ring;
  2051. struct drm_sched_rq *rq;
  2052. unsigned long size;
  2053. uint64_t flags;
  2054. int r, i;
  2055. vm->va = RB_ROOT_CACHED;
  2056. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2057. vm->reserved_vmid[i] = NULL;
  2058. INIT_LIST_HEAD(&vm->evicted);
  2059. INIT_LIST_HEAD(&vm->relocated);
  2060. spin_lock_init(&vm->moved_lock);
  2061. INIT_LIST_HEAD(&vm->moved);
  2062. INIT_LIST_HEAD(&vm->idle);
  2063. INIT_LIST_HEAD(&vm->freed);
  2064. /* create scheduler entity for page table updates */
  2065. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  2066. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  2067. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  2068. rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
  2069. r = drm_sched_entity_init(&ring->sched, &vm->entity,
  2070. rq, NULL);
  2071. if (r)
  2072. return r;
  2073. vm->pte_support_ats = false;
  2074. if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
  2075. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2076. AMDGPU_VM_USE_CPU_FOR_COMPUTE);
  2077. if (adev->asic_type == CHIP_RAVEN)
  2078. vm->pte_support_ats = true;
  2079. } else {
  2080. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2081. AMDGPU_VM_USE_CPU_FOR_GFX);
  2082. }
  2083. DRM_DEBUG_DRIVER("VM update mode is %s\n",
  2084. vm->use_cpu_for_update ? "CPU" : "SDMA");
  2085. WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
  2086. "CPU update of VM recommended only for large BAR system\n");
  2087. vm->last_update = NULL;
  2088. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
  2089. if (vm->use_cpu_for_update)
  2090. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  2091. else
  2092. flags |= AMDGPU_GEM_CREATE_SHADOW;
  2093. size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
  2094. memset(&bp, 0, sizeof(bp));
  2095. bp.size = size;
  2096. bp.byte_align = align;
  2097. bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
  2098. bp.flags = flags;
  2099. bp.type = ttm_bo_type_kernel;
  2100. bp.resv = NULL;
  2101. r = amdgpu_bo_create(adev, &bp, &root);
  2102. if (r)
  2103. goto error_free_sched_entity;
  2104. r = amdgpu_bo_reserve(root, true);
  2105. if (r)
  2106. goto error_free_root;
  2107. r = amdgpu_vm_clear_bo(adev, vm, root,
  2108. adev->vm_manager.root_level,
  2109. vm->pte_support_ats);
  2110. if (r)
  2111. goto error_unreserve;
  2112. amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
  2113. amdgpu_bo_unreserve(vm->root.base.bo);
  2114. if (pasid) {
  2115. unsigned long flags;
  2116. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2117. r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
  2118. GFP_ATOMIC);
  2119. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2120. if (r < 0)
  2121. goto error_free_root;
  2122. vm->pasid = pasid;
  2123. }
  2124. INIT_KFIFO(vm->faults);
  2125. vm->fault_credit = 16;
  2126. return 0;
  2127. error_unreserve:
  2128. amdgpu_bo_unreserve(vm->root.base.bo);
  2129. error_free_root:
  2130. amdgpu_bo_unref(&vm->root.base.bo->shadow);
  2131. amdgpu_bo_unref(&vm->root.base.bo);
  2132. vm->root.base.bo = NULL;
  2133. error_free_sched_entity:
  2134. drm_sched_entity_fini(&ring->sched, &vm->entity);
  2135. return r;
  2136. }
  2137. /**
  2138. * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
  2139. *
  2140. * This only works on GFX VMs that don't have any BOs added and no
  2141. * page tables allocated yet.
  2142. *
  2143. * Changes the following VM parameters:
  2144. * - use_cpu_for_update
  2145. * - pte_supports_ats
  2146. * - pasid (old PASID is released, because compute manages its own PASIDs)
  2147. *
  2148. * Reinitializes the page directory to reflect the changed ATS
  2149. * setting. May leave behind an unused shadow BO for the page
  2150. * directory when switching from SDMA updates to CPU updates.
  2151. *
  2152. * Returns 0 for success, -errno for errors.
  2153. */
  2154. int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2155. {
  2156. bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
  2157. int r;
  2158. r = amdgpu_bo_reserve(vm->root.base.bo, true);
  2159. if (r)
  2160. return r;
  2161. /* Sanity checks */
  2162. if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) {
  2163. r = -EINVAL;
  2164. goto error;
  2165. }
  2166. /* Check if PD needs to be reinitialized and do it before
  2167. * changing any other state, in case it fails.
  2168. */
  2169. if (pte_support_ats != vm->pte_support_ats) {
  2170. r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
  2171. adev->vm_manager.root_level,
  2172. pte_support_ats);
  2173. if (r)
  2174. goto error;
  2175. }
  2176. /* Update VM state */
  2177. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2178. AMDGPU_VM_USE_CPU_FOR_COMPUTE);
  2179. vm->pte_support_ats = pte_support_ats;
  2180. DRM_DEBUG_DRIVER("VM update mode is %s\n",
  2181. vm->use_cpu_for_update ? "CPU" : "SDMA");
  2182. WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
  2183. "CPU update of VM recommended only for large BAR system\n");
  2184. if (vm->pasid) {
  2185. unsigned long flags;
  2186. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2187. idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
  2188. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2189. vm->pasid = 0;
  2190. }
  2191. error:
  2192. amdgpu_bo_unreserve(vm->root.base.bo);
  2193. return r;
  2194. }
  2195. /**
  2196. * amdgpu_vm_free_levels - free PD/PT levels
  2197. *
  2198. * @adev: amdgpu device structure
  2199. * @parent: PD/PT starting level to free
  2200. * @level: level of parent structure
  2201. *
  2202. * Free the page directory or page table level and all sub levels.
  2203. */
  2204. static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
  2205. struct amdgpu_vm_pt *parent,
  2206. unsigned level)
  2207. {
  2208. unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
  2209. if (parent->base.bo) {
  2210. list_del(&parent->base.bo_list);
  2211. list_del(&parent->base.vm_status);
  2212. amdgpu_bo_unref(&parent->base.bo->shadow);
  2213. amdgpu_bo_unref(&parent->base.bo);
  2214. }
  2215. if (parent->entries)
  2216. for (i = 0; i < num_entries; i++)
  2217. amdgpu_vm_free_levels(adev, &parent->entries[i],
  2218. level + 1);
  2219. kvfree(parent->entries);
  2220. }
  2221. /**
  2222. * amdgpu_vm_fini - tear down a vm instance
  2223. *
  2224. * @adev: amdgpu_device pointer
  2225. * @vm: requested vm
  2226. *
  2227. * Tear down @vm.
  2228. * Unbind the VM and remove all bos from the vm bo list
  2229. */
  2230. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2231. {
  2232. struct amdgpu_bo_va_mapping *mapping, *tmp;
  2233. bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
  2234. struct amdgpu_bo *root;
  2235. u64 fault;
  2236. int i, r;
  2237. amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
  2238. /* Clear pending page faults from IH when the VM is destroyed */
  2239. while (kfifo_get(&vm->faults, &fault))
  2240. amdgpu_ih_clear_fault(adev, fault);
  2241. if (vm->pasid) {
  2242. unsigned long flags;
  2243. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2244. idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
  2245. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2246. }
  2247. drm_sched_entity_fini(vm->entity.sched, &vm->entity);
  2248. if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
  2249. dev_err(adev->dev, "still active bo inside vm\n");
  2250. }
  2251. rbtree_postorder_for_each_entry_safe(mapping, tmp,
  2252. &vm->va.rb_root, rb) {
  2253. list_del(&mapping->list);
  2254. amdgpu_vm_it_remove(mapping, &vm->va);
  2255. kfree(mapping);
  2256. }
  2257. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  2258. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  2259. amdgpu_vm_prt_fini(adev, vm);
  2260. prt_fini_needed = false;
  2261. }
  2262. list_del(&mapping->list);
  2263. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  2264. }
  2265. root = amdgpu_bo_ref(vm->root.base.bo);
  2266. r = amdgpu_bo_reserve(root, true);
  2267. if (r) {
  2268. dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
  2269. } else {
  2270. amdgpu_vm_free_levels(adev, &vm->root,
  2271. adev->vm_manager.root_level);
  2272. amdgpu_bo_unreserve(root);
  2273. }
  2274. amdgpu_bo_unref(&root);
  2275. dma_fence_put(vm->last_update);
  2276. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2277. amdgpu_vmid_free_reserved(adev, vm, i);
  2278. }
  2279. /**
  2280. * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
  2281. *
  2282. * @adev: amdgpu_device pointer
  2283. * @pasid: PASID do identify the VM
  2284. *
  2285. * This function is expected to be called in interrupt context. Returns
  2286. * true if there was fault credit, false otherwise
  2287. */
  2288. bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
  2289. unsigned int pasid)
  2290. {
  2291. struct amdgpu_vm *vm;
  2292. spin_lock(&adev->vm_manager.pasid_lock);
  2293. vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
  2294. if (!vm) {
  2295. /* VM not found, can't track fault credit */
  2296. spin_unlock(&adev->vm_manager.pasid_lock);
  2297. return true;
  2298. }
  2299. /* No lock needed. only accessed by IRQ handler */
  2300. if (!vm->fault_credit) {
  2301. /* Too many faults in this VM */
  2302. spin_unlock(&adev->vm_manager.pasid_lock);
  2303. return false;
  2304. }
  2305. vm->fault_credit--;
  2306. spin_unlock(&adev->vm_manager.pasid_lock);
  2307. return true;
  2308. }
  2309. /**
  2310. * amdgpu_vm_manager_init - init the VM manager
  2311. *
  2312. * @adev: amdgpu_device pointer
  2313. *
  2314. * Initialize the VM manager structures
  2315. */
  2316. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  2317. {
  2318. unsigned i;
  2319. amdgpu_vmid_mgr_init(adev);
  2320. adev->vm_manager.fence_context =
  2321. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2322. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  2323. adev->vm_manager.seqno[i] = 0;
  2324. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  2325. spin_lock_init(&adev->vm_manager.prt_lock);
  2326. atomic_set(&adev->vm_manager.num_prt_users, 0);
  2327. /* If not overridden by the user, by default, only in large BAR systems
  2328. * Compute VM tables will be updated by CPU
  2329. */
  2330. #ifdef CONFIG_X86_64
  2331. if (amdgpu_vm_update_mode == -1) {
  2332. if (amdgpu_vm_is_large_bar(adev))
  2333. adev->vm_manager.vm_update_mode =
  2334. AMDGPU_VM_USE_CPU_FOR_COMPUTE;
  2335. else
  2336. adev->vm_manager.vm_update_mode = 0;
  2337. } else
  2338. adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
  2339. #else
  2340. adev->vm_manager.vm_update_mode = 0;
  2341. #endif
  2342. idr_init(&adev->vm_manager.pasid_idr);
  2343. spin_lock_init(&adev->vm_manager.pasid_lock);
  2344. }
  2345. /**
  2346. * amdgpu_vm_manager_fini - cleanup VM manager
  2347. *
  2348. * @adev: amdgpu_device pointer
  2349. *
  2350. * Cleanup the VM manager and free resources.
  2351. */
  2352. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  2353. {
  2354. WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
  2355. idr_destroy(&adev->vm_manager.pasid_idr);
  2356. amdgpu_vmid_mgr_fini(adev);
  2357. }
  2358. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  2359. {
  2360. union drm_amdgpu_vm *args = data;
  2361. struct amdgpu_device *adev = dev->dev_private;
  2362. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  2363. int r;
  2364. switch (args->in.op) {
  2365. case AMDGPU_VM_OP_RESERVE_VMID:
  2366. /* current, we only have requirement to reserve vmid from gfxhub */
  2367. r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2368. if (r)
  2369. return r;
  2370. break;
  2371. case AMDGPU_VM_OP_UNRESERVE_VMID:
  2372. amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2373. break;
  2374. default:
  2375. return -EINVAL;
  2376. }
  2377. return 0;
  2378. }