amdgpu_vm.c 75 KB

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