amdgpu_vm.c 88 KB

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