amdgpu_vm.c 87 KB

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