amdgpu_vm.c 87 KB

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