amdgpu_vm.c 74 KB

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