amdgpu_vm.c 86 KB

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