amdgpu_vm.c 80 KB

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