amdgpu_vm.c 88 KB

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