amdgpu_device.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780
  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/kthread.h>
  29. #include <linux/console.h>
  30. #include <linux/slab.h>
  31. #include <linux/debugfs.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm_crtc_helper.h>
  34. #include <drm/drm_atomic_helper.h>
  35. #include <drm/amdgpu_drm.h>
  36. #include <linux/vgaarb.h>
  37. #include <linux/vga_switcheroo.h>
  38. #include <linux/efi.h>
  39. #include "amdgpu.h"
  40. #include "amdgpu_trace.h"
  41. #include "amdgpu_i2c.h"
  42. #include "atom.h"
  43. #include "amdgpu_atombios.h"
  44. #include "amdgpu_atomfirmware.h"
  45. #include "amd_pcie.h"
  46. #ifdef CONFIG_DRM_AMDGPU_SI
  47. #include "si.h"
  48. #endif
  49. #ifdef CONFIG_DRM_AMDGPU_CIK
  50. #include "cik.h"
  51. #endif
  52. #include "vi.h"
  53. #include "soc15.h"
  54. #include "bif/bif_4_1_d.h"
  55. #include <linux/pci.h>
  56. #include <linux/firmware.h>
  57. #include "amdgpu_vf_error.h"
  58. #include "amdgpu_amdkfd.h"
  59. #include "amdgpu_pm.h"
  60. MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  61. MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
  62. #define AMDGPU_RESUME_MS 2000
  63. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
  64. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
  65. static int amdgpu_debugfs_init(struct amdgpu_device *adev);
  66. static const char *amdgpu_asic_name[] = {
  67. "TAHITI",
  68. "PITCAIRN",
  69. "VERDE",
  70. "OLAND",
  71. "HAINAN",
  72. "BONAIRE",
  73. "KAVERI",
  74. "KABINI",
  75. "HAWAII",
  76. "MULLINS",
  77. "TOPAZ",
  78. "TONGA",
  79. "FIJI",
  80. "CARRIZO",
  81. "STONEY",
  82. "POLARIS10",
  83. "POLARIS11",
  84. "POLARIS12",
  85. "VEGA10",
  86. "RAVEN",
  87. "LAST",
  88. };
  89. bool amdgpu_device_is_px(struct drm_device *dev)
  90. {
  91. struct amdgpu_device *adev = dev->dev_private;
  92. if (adev->flags & AMD_IS_PX)
  93. return true;
  94. return false;
  95. }
  96. /*
  97. * MMIO register access helper functions.
  98. */
  99. uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
  100. uint32_t acc_flags)
  101. {
  102. uint32_t ret;
  103. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
  104. return amdgpu_virt_kiq_rreg(adev, reg);
  105. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  106. ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
  107. else {
  108. unsigned long flags;
  109. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  110. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  111. ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  112. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  113. }
  114. trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
  115. return ret;
  116. }
  117. void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
  118. uint32_t acc_flags)
  119. {
  120. trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
  121. if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
  122. adev->last_mm_index = v;
  123. }
  124. if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
  125. return amdgpu_virt_kiq_wreg(adev, reg, v);
  126. if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
  127. writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  128. else {
  129. unsigned long flags;
  130. spin_lock_irqsave(&adev->mmio_idx_lock, flags);
  131. writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
  132. writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
  133. spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  134. }
  135. if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
  136. udelay(500);
  137. }
  138. }
  139. u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  140. {
  141. if ((reg * 4) < adev->rio_mem_size)
  142. return ioread32(adev->rio_mem + (reg * 4));
  143. else {
  144. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  145. return ioread32(adev->rio_mem + (mmMM_DATA * 4));
  146. }
  147. }
  148. void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  149. {
  150. if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
  151. adev->last_mm_index = v;
  152. }
  153. if ((reg * 4) < adev->rio_mem_size)
  154. iowrite32(v, adev->rio_mem + (reg * 4));
  155. else {
  156. iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  157. iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  158. }
  159. if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
  160. udelay(500);
  161. }
  162. }
  163. /**
  164. * amdgpu_mm_rdoorbell - read a doorbell dword
  165. *
  166. * @adev: amdgpu_device pointer
  167. * @index: doorbell index
  168. *
  169. * Returns the value in the doorbell aperture at the
  170. * requested doorbell index (CIK).
  171. */
  172. u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  173. {
  174. if (index < adev->doorbell.num_doorbells) {
  175. return readl(adev->doorbell.ptr + index);
  176. } else {
  177. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  178. return 0;
  179. }
  180. }
  181. /**
  182. * amdgpu_mm_wdoorbell - write a doorbell dword
  183. *
  184. * @adev: amdgpu_device pointer
  185. * @index: doorbell index
  186. * @v: value to write
  187. *
  188. * Writes @v to the doorbell aperture at the
  189. * requested doorbell index (CIK).
  190. */
  191. void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  192. {
  193. if (index < adev->doorbell.num_doorbells) {
  194. writel(v, adev->doorbell.ptr + index);
  195. } else {
  196. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  197. }
  198. }
  199. /**
  200. * amdgpu_mm_rdoorbell64 - read a doorbell Qword
  201. *
  202. * @adev: amdgpu_device pointer
  203. * @index: doorbell index
  204. *
  205. * Returns the value in the doorbell aperture at the
  206. * requested doorbell index (VEGA10+).
  207. */
  208. u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
  209. {
  210. if (index < adev->doorbell.num_doorbells) {
  211. return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
  212. } else {
  213. DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
  214. return 0;
  215. }
  216. }
  217. /**
  218. * amdgpu_mm_wdoorbell64 - write a doorbell Qword
  219. *
  220. * @adev: amdgpu_device pointer
  221. * @index: doorbell index
  222. * @v: value to write
  223. *
  224. * Writes @v to the doorbell aperture at the
  225. * requested doorbell index (VEGA10+).
  226. */
  227. void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
  228. {
  229. if (index < adev->doorbell.num_doorbells) {
  230. atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
  231. } else {
  232. DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  233. }
  234. }
  235. /**
  236. * amdgpu_invalid_rreg - dummy reg read function
  237. *
  238. * @adev: amdgpu device pointer
  239. * @reg: offset of register
  240. *
  241. * Dummy register read function. Used for register blocks
  242. * that certain asics don't have (all asics).
  243. * Returns the value in the register.
  244. */
  245. static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
  246. {
  247. DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
  248. BUG();
  249. return 0;
  250. }
  251. /**
  252. * amdgpu_invalid_wreg - dummy reg write function
  253. *
  254. * @adev: amdgpu device pointer
  255. * @reg: offset of register
  256. * @v: value to write to the register
  257. *
  258. * Dummy register read function. Used for register blocks
  259. * that certain asics don't have (all asics).
  260. */
  261. static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
  262. {
  263. DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
  264. reg, v);
  265. BUG();
  266. }
  267. /**
  268. * amdgpu_block_invalid_rreg - dummy reg read function
  269. *
  270. * @adev: amdgpu device pointer
  271. * @block: offset of instance
  272. * @reg: offset of register
  273. *
  274. * Dummy register read function. Used for register blocks
  275. * that certain asics don't have (all asics).
  276. * Returns the value in the register.
  277. */
  278. static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
  279. uint32_t block, uint32_t reg)
  280. {
  281. DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
  282. reg, block);
  283. BUG();
  284. return 0;
  285. }
  286. /**
  287. * amdgpu_block_invalid_wreg - dummy reg write function
  288. *
  289. * @adev: amdgpu device pointer
  290. * @block: offset of instance
  291. * @reg: offset of register
  292. * @v: value to write to the register
  293. *
  294. * Dummy register read function. Used for register blocks
  295. * that certain asics don't have (all asics).
  296. */
  297. static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
  298. uint32_t block,
  299. uint32_t reg, uint32_t v)
  300. {
  301. DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
  302. reg, block, v);
  303. BUG();
  304. }
  305. static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
  306. {
  307. return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
  308. PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
  309. &adev->vram_scratch.robj,
  310. &adev->vram_scratch.gpu_addr,
  311. (void **)&adev->vram_scratch.ptr);
  312. }
  313. static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
  314. {
  315. amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
  316. }
  317. /**
  318. * amdgpu_program_register_sequence - program an array of registers.
  319. *
  320. * @adev: amdgpu_device pointer
  321. * @registers: pointer to the register array
  322. * @array_size: size of the register array
  323. *
  324. * Programs an array or registers with and and or masks.
  325. * This is a helper for setting golden registers.
  326. */
  327. void amdgpu_program_register_sequence(struct amdgpu_device *adev,
  328. const u32 *registers,
  329. const u32 array_size)
  330. {
  331. u32 tmp, reg, and_mask, or_mask;
  332. int i;
  333. if (array_size % 3)
  334. return;
  335. for (i = 0; i < array_size; i +=3) {
  336. reg = registers[i + 0];
  337. and_mask = registers[i + 1];
  338. or_mask = registers[i + 2];
  339. if (and_mask == 0xffffffff) {
  340. tmp = or_mask;
  341. } else {
  342. tmp = RREG32(reg);
  343. tmp &= ~and_mask;
  344. tmp |= or_mask;
  345. }
  346. WREG32(reg, tmp);
  347. }
  348. }
  349. void amdgpu_pci_config_reset(struct amdgpu_device *adev)
  350. {
  351. pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
  352. }
  353. /*
  354. * GPU doorbell aperture helpers function.
  355. */
  356. /**
  357. * amdgpu_doorbell_init - Init doorbell driver information.
  358. *
  359. * @adev: amdgpu_device pointer
  360. *
  361. * Init doorbell driver information (CIK)
  362. * Returns 0 on success, error on failure.
  363. */
  364. static int amdgpu_doorbell_init(struct amdgpu_device *adev)
  365. {
  366. /* No doorbell on SI hardware generation */
  367. if (adev->asic_type < CHIP_BONAIRE) {
  368. adev->doorbell.base = 0;
  369. adev->doorbell.size = 0;
  370. adev->doorbell.num_doorbells = 0;
  371. adev->doorbell.ptr = NULL;
  372. return 0;
  373. }
  374. if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
  375. return -EINVAL;
  376. /* doorbell bar mapping */
  377. adev->doorbell.base = pci_resource_start(adev->pdev, 2);
  378. adev->doorbell.size = pci_resource_len(adev->pdev, 2);
  379. adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
  380. AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
  381. if (adev->doorbell.num_doorbells == 0)
  382. return -EINVAL;
  383. adev->doorbell.ptr = ioremap(adev->doorbell.base,
  384. adev->doorbell.num_doorbells *
  385. sizeof(u32));
  386. if (adev->doorbell.ptr == NULL)
  387. return -ENOMEM;
  388. return 0;
  389. }
  390. /**
  391. * amdgpu_doorbell_fini - Tear down doorbell driver information.
  392. *
  393. * @adev: amdgpu_device pointer
  394. *
  395. * Tear down doorbell driver information (CIK)
  396. */
  397. static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
  398. {
  399. iounmap(adev->doorbell.ptr);
  400. adev->doorbell.ptr = NULL;
  401. }
  402. /**
  403. * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
  404. * setup amdkfd
  405. *
  406. * @adev: amdgpu_device pointer
  407. * @aperture_base: output returning doorbell aperture base physical address
  408. * @aperture_size: output returning doorbell aperture size in bytes
  409. * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
  410. *
  411. * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
  412. * takes doorbells required for its own rings and reports the setup to amdkfd.
  413. * amdgpu reserved doorbells are at the start of the doorbell aperture.
  414. */
  415. void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
  416. phys_addr_t *aperture_base,
  417. size_t *aperture_size,
  418. size_t *start_offset)
  419. {
  420. /*
  421. * The first num_doorbells are used by amdgpu.
  422. * amdkfd takes whatever's left in the aperture.
  423. */
  424. if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
  425. *aperture_base = adev->doorbell.base;
  426. *aperture_size = adev->doorbell.size;
  427. *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
  428. } else {
  429. *aperture_base = 0;
  430. *aperture_size = 0;
  431. *start_offset = 0;
  432. }
  433. }
  434. /*
  435. * amdgpu_wb_*()
  436. * Writeback is the method by which the GPU updates special pages in memory
  437. * with the status of certain GPU events (fences, ring pointers,etc.).
  438. */
  439. /**
  440. * amdgpu_wb_fini - Disable Writeback and free memory
  441. *
  442. * @adev: amdgpu_device pointer
  443. *
  444. * Disables Writeback and frees the Writeback memory (all asics).
  445. * Used at driver shutdown.
  446. */
  447. static void amdgpu_wb_fini(struct amdgpu_device *adev)
  448. {
  449. if (adev->wb.wb_obj) {
  450. amdgpu_bo_free_kernel(&adev->wb.wb_obj,
  451. &adev->wb.gpu_addr,
  452. (void **)&adev->wb.wb);
  453. adev->wb.wb_obj = NULL;
  454. }
  455. }
  456. /**
  457. * amdgpu_wb_init- Init Writeback driver info and allocate memory
  458. *
  459. * @adev: amdgpu_device pointer
  460. *
  461. * Initializes writeback and allocates writeback memory (all asics).
  462. * Used at driver startup.
  463. * Returns 0 on success or an -error on failure.
  464. */
  465. static int amdgpu_wb_init(struct amdgpu_device *adev)
  466. {
  467. int r;
  468. if (adev->wb.wb_obj == NULL) {
  469. /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
  470. r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
  471. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  472. &adev->wb.wb_obj, &adev->wb.gpu_addr,
  473. (void **)&adev->wb.wb);
  474. if (r) {
  475. dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
  476. return r;
  477. }
  478. adev->wb.num_wb = AMDGPU_MAX_WB;
  479. memset(&adev->wb.used, 0, sizeof(adev->wb.used));
  480. /* clear wb memory */
  481. memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
  482. }
  483. return 0;
  484. }
  485. /**
  486. * amdgpu_wb_get - Allocate a wb entry
  487. *
  488. * @adev: amdgpu_device pointer
  489. * @wb: wb index
  490. *
  491. * Allocate a wb slot for use by the driver (all asics).
  492. * Returns 0 on success or -EINVAL on failure.
  493. */
  494. int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
  495. {
  496. unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
  497. if (offset < adev->wb.num_wb) {
  498. __set_bit(offset, adev->wb.used);
  499. *wb = offset << 3; /* convert to dw offset */
  500. return 0;
  501. } else {
  502. return -EINVAL;
  503. }
  504. }
  505. /**
  506. * amdgpu_wb_free - Free a wb entry
  507. *
  508. * @adev: amdgpu_device pointer
  509. * @wb: wb index
  510. *
  511. * Free a wb slot allocated for use by the driver (all asics)
  512. */
  513. void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
  514. {
  515. if (wb < adev->wb.num_wb)
  516. __clear_bit(wb >> 3, adev->wb.used);
  517. }
  518. /**
  519. * amdgpu_vram_location - try to find VRAM location
  520. * @adev: amdgpu device structure holding all necessary informations
  521. * @mc: memory controller structure holding memory informations
  522. * @base: base address at which to put VRAM
  523. *
  524. * Function will try to place VRAM at base address provided
  525. * as parameter.
  526. */
  527. void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
  528. {
  529. uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
  530. mc->vram_start = base;
  531. mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
  532. if (limit && limit < mc->real_vram_size)
  533. mc->real_vram_size = limit;
  534. dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
  535. mc->mc_vram_size >> 20, mc->vram_start,
  536. mc->vram_end, mc->real_vram_size >> 20);
  537. }
  538. /**
  539. * amdgpu_gart_location - try to find GTT location
  540. * @adev: amdgpu device structure holding all necessary informations
  541. * @mc: memory controller structure holding memory informations
  542. *
  543. * Function will place try to place GTT before or after VRAM.
  544. *
  545. * If GTT size is bigger than space left then we ajust GTT size.
  546. * Thus function will never fails.
  547. *
  548. * FIXME: when reducing GTT size align new size on power of 2.
  549. */
  550. void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
  551. {
  552. u64 size_af, size_bf;
  553. size_af = adev->mc.mc_mask - mc->vram_end;
  554. size_bf = mc->vram_start;
  555. if (size_bf > size_af) {
  556. if (mc->gart_size > size_bf) {
  557. dev_warn(adev->dev, "limiting GTT\n");
  558. mc->gart_size = size_bf;
  559. }
  560. mc->gart_start = 0;
  561. } else {
  562. if (mc->gart_size > size_af) {
  563. dev_warn(adev->dev, "limiting GTT\n");
  564. mc->gart_size = size_af;
  565. }
  566. /* VCE doesn't like it when BOs cross a 4GB segment, so align
  567. * the GART base on a 4GB boundary as well.
  568. */
  569. mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
  570. }
  571. mc->gart_end = mc->gart_start + mc->gart_size - 1;
  572. dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
  573. mc->gart_size >> 20, mc->gart_start, mc->gart_end);
  574. }
  575. /*
  576. * Firmware Reservation functions
  577. */
  578. /**
  579. * amdgpu_fw_reserve_vram_fini - free fw reserved vram
  580. *
  581. * @adev: amdgpu_device pointer
  582. *
  583. * free fw reserved vram if it has been reserved.
  584. */
  585. void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
  586. {
  587. amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
  588. NULL, &adev->fw_vram_usage.va);
  589. }
  590. /**
  591. * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
  592. *
  593. * @adev: amdgpu_device pointer
  594. *
  595. * create bo vram reservation from fw.
  596. */
  597. int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
  598. {
  599. struct ttm_operation_ctx ctx = { false, false };
  600. int r = 0;
  601. int i;
  602. u64 vram_size = adev->mc.visible_vram_size;
  603. u64 offset = adev->fw_vram_usage.start_offset;
  604. u64 size = adev->fw_vram_usage.size;
  605. struct amdgpu_bo *bo;
  606. adev->fw_vram_usage.va = NULL;
  607. adev->fw_vram_usage.reserved_bo = NULL;
  608. if (adev->fw_vram_usage.size > 0 &&
  609. adev->fw_vram_usage.size <= vram_size) {
  610. r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
  611. PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
  612. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  613. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
  614. &adev->fw_vram_usage.reserved_bo);
  615. if (r)
  616. goto error_create;
  617. r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
  618. if (r)
  619. goto error_reserve;
  620. /* remove the original mem node and create a new one at the
  621. * request position
  622. */
  623. bo = adev->fw_vram_usage.reserved_bo;
  624. offset = ALIGN(offset, PAGE_SIZE);
  625. for (i = 0; i < bo->placement.num_placement; ++i) {
  626. bo->placements[i].fpfn = offset >> PAGE_SHIFT;
  627. bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
  628. }
  629. ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
  630. r = ttm_bo_mem_space(&bo->tbo, &bo->placement,
  631. &bo->tbo.mem, &ctx);
  632. if (r)
  633. goto error_pin;
  634. r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
  635. AMDGPU_GEM_DOMAIN_VRAM,
  636. adev->fw_vram_usage.start_offset,
  637. (adev->fw_vram_usage.start_offset +
  638. adev->fw_vram_usage.size), NULL);
  639. if (r)
  640. goto error_pin;
  641. r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
  642. &adev->fw_vram_usage.va);
  643. if (r)
  644. goto error_kmap;
  645. amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
  646. }
  647. return r;
  648. error_kmap:
  649. amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
  650. error_pin:
  651. amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
  652. error_reserve:
  653. amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
  654. error_create:
  655. adev->fw_vram_usage.va = NULL;
  656. adev->fw_vram_usage.reserved_bo = NULL;
  657. return r;
  658. }
  659. /**
  660. * amdgpu_device_resize_fb_bar - try to resize FB BAR
  661. *
  662. * @adev: amdgpu_device pointer
  663. *
  664. * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
  665. * to fail, but if any of the BARs is not accessible after the size we abort
  666. * driver loading by returning -ENODEV.
  667. */
  668. int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
  669. {
  670. u64 space_needed = roundup_pow_of_two(adev->mc.real_vram_size);
  671. u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
  672. struct pci_bus *root;
  673. struct resource *res;
  674. unsigned i;
  675. u16 cmd;
  676. int r;
  677. /* Bypass for VF */
  678. if (amdgpu_sriov_vf(adev))
  679. return 0;
  680. /* Check if the root BUS has 64bit memory resources */
  681. root = adev->pdev->bus;
  682. while (root->parent)
  683. root = root->parent;
  684. pci_bus_for_each_resource(root, res, i) {
  685. if (res && res->flags & IORESOURCE_MEM_64 &&
  686. res->start > 0x100000000ull)
  687. break;
  688. }
  689. /* Trying to resize is pointless without a root hub window above 4GB */
  690. if (!res)
  691. return 0;
  692. /* Disable memory decoding while we change the BAR addresses and size */
  693. pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
  694. pci_write_config_word(adev->pdev, PCI_COMMAND,
  695. cmd & ~PCI_COMMAND_MEMORY);
  696. /* Free the VRAM and doorbell BAR, we most likely need to move both. */
  697. amdgpu_doorbell_fini(adev);
  698. if (adev->asic_type >= CHIP_BONAIRE)
  699. pci_release_resource(adev->pdev, 2);
  700. pci_release_resource(adev->pdev, 0);
  701. r = pci_resize_resource(adev->pdev, 0, rbar_size);
  702. if (r == -ENOSPC)
  703. DRM_INFO("Not enough PCI address space for a large BAR.");
  704. else if (r && r != -ENOTSUPP)
  705. DRM_ERROR("Problem resizing BAR0 (%d).", r);
  706. pci_assign_unassigned_bus_resources(adev->pdev->bus);
  707. /* When the doorbell or fb BAR isn't available we have no chance of
  708. * using the device.
  709. */
  710. r = amdgpu_doorbell_init(adev);
  711. if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
  712. return -ENODEV;
  713. pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
  714. return 0;
  715. }
  716. /*
  717. * GPU helpers function.
  718. */
  719. /**
  720. * amdgpu_need_post - check if the hw need post or not
  721. *
  722. * @adev: amdgpu_device pointer
  723. *
  724. * Check if the asic has been initialized (all asics) at driver startup
  725. * or post is needed if hw reset is performed.
  726. * Returns true if need or false if not.
  727. */
  728. bool amdgpu_need_post(struct amdgpu_device *adev)
  729. {
  730. uint32_t reg;
  731. if (amdgpu_sriov_vf(adev))
  732. return false;
  733. if (amdgpu_passthrough(adev)) {
  734. /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
  735. * some old smc fw still need driver do vPost otherwise gpu hang, while
  736. * those smc fw version above 22.15 doesn't have this flaw, so we force
  737. * vpost executed for smc version below 22.15
  738. */
  739. if (adev->asic_type == CHIP_FIJI) {
  740. int err;
  741. uint32_t fw_ver;
  742. err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
  743. /* force vPost if error occured */
  744. if (err)
  745. return true;
  746. fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
  747. if (fw_ver < 0x00160e00)
  748. return true;
  749. }
  750. }
  751. if (adev->has_hw_reset) {
  752. adev->has_hw_reset = false;
  753. return true;
  754. }
  755. /* bios scratch used on CIK+ */
  756. if (adev->asic_type >= CHIP_BONAIRE)
  757. return amdgpu_atombios_scratch_need_asic_init(adev);
  758. /* check MEM_SIZE for older asics */
  759. reg = amdgpu_asic_get_config_memsize(adev);
  760. if ((reg != 0) && (reg != 0xffffffff))
  761. return false;
  762. return true;
  763. }
  764. /**
  765. * amdgpu_dummy_page_init - init dummy page used by the driver
  766. *
  767. * @adev: amdgpu_device pointer
  768. *
  769. * Allocate the dummy page used by the driver (all asics).
  770. * This dummy page is used by the driver as a filler for gart entries
  771. * when pages are taken out of the GART
  772. * Returns 0 on sucess, -ENOMEM on failure.
  773. */
  774. int amdgpu_dummy_page_init(struct amdgpu_device *adev)
  775. {
  776. if (adev->dummy_page.page)
  777. return 0;
  778. adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
  779. if (adev->dummy_page.page == NULL)
  780. return -ENOMEM;
  781. adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
  782. 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  783. if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
  784. dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
  785. __free_page(adev->dummy_page.page);
  786. adev->dummy_page.page = NULL;
  787. return -ENOMEM;
  788. }
  789. return 0;
  790. }
  791. /**
  792. * amdgpu_dummy_page_fini - free dummy page used by the driver
  793. *
  794. * @adev: amdgpu_device pointer
  795. *
  796. * Frees the dummy page used by the driver (all asics).
  797. */
  798. void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
  799. {
  800. if (adev->dummy_page.page == NULL)
  801. return;
  802. pci_unmap_page(adev->pdev, adev->dummy_page.addr,
  803. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  804. __free_page(adev->dummy_page.page);
  805. adev->dummy_page.page = NULL;
  806. }
  807. /* if we get transitioned to only one device, take VGA back */
  808. /**
  809. * amdgpu_vga_set_decode - enable/disable vga decode
  810. *
  811. * @cookie: amdgpu_device pointer
  812. * @state: enable/disable vga decode
  813. *
  814. * Enable/disable vga decode (all asics).
  815. * Returns VGA resource flags.
  816. */
  817. static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
  818. {
  819. struct amdgpu_device *adev = cookie;
  820. amdgpu_asic_set_vga_state(adev, state);
  821. if (state)
  822. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  823. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  824. else
  825. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  826. }
  827. static void amdgpu_check_block_size(struct amdgpu_device *adev)
  828. {
  829. /* defines number of bits in page table versus page directory,
  830. * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
  831. * page table and the remaining bits are in the page directory */
  832. if (amdgpu_vm_block_size == -1)
  833. return;
  834. if (amdgpu_vm_block_size < 9) {
  835. dev_warn(adev->dev, "VM page table size (%d) too small\n",
  836. amdgpu_vm_block_size);
  837. amdgpu_vm_block_size = -1;
  838. }
  839. }
  840. static void amdgpu_check_vm_size(struct amdgpu_device *adev)
  841. {
  842. /* no need to check the default value */
  843. if (amdgpu_vm_size == -1)
  844. return;
  845. if (amdgpu_vm_size < 1) {
  846. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  847. amdgpu_vm_size);
  848. amdgpu_vm_size = -1;
  849. }
  850. }
  851. /**
  852. * amdgpu_check_arguments - validate module params
  853. *
  854. * @adev: amdgpu_device pointer
  855. *
  856. * Validates certain module parameters and updates
  857. * the associated values used by the driver (all asics).
  858. */
  859. static void amdgpu_check_arguments(struct amdgpu_device *adev)
  860. {
  861. if (amdgpu_sched_jobs < 4) {
  862. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  863. amdgpu_sched_jobs);
  864. amdgpu_sched_jobs = 4;
  865. } else if (!is_power_of_2(amdgpu_sched_jobs)){
  866. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  867. amdgpu_sched_jobs);
  868. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  869. }
  870. if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
  871. /* gart size must be greater or equal to 32M */
  872. dev_warn(adev->dev, "gart size (%d) too small\n",
  873. amdgpu_gart_size);
  874. amdgpu_gart_size = -1;
  875. }
  876. if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
  877. /* gtt size must be greater or equal to 32M */
  878. dev_warn(adev->dev, "gtt size (%d) too small\n",
  879. amdgpu_gtt_size);
  880. amdgpu_gtt_size = -1;
  881. }
  882. /* valid range is between 4 and 9 inclusive */
  883. if (amdgpu_vm_fragment_size != -1 &&
  884. (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
  885. dev_warn(adev->dev, "valid range is between 4 and 9\n");
  886. amdgpu_vm_fragment_size = -1;
  887. }
  888. amdgpu_check_vm_size(adev);
  889. amdgpu_check_block_size(adev);
  890. if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
  891. !is_power_of_2(amdgpu_vram_page_split))) {
  892. dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
  893. amdgpu_vram_page_split);
  894. amdgpu_vram_page_split = 1024;
  895. }
  896. if (amdgpu_lockup_timeout == 0) {
  897. dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
  898. amdgpu_lockup_timeout = 10000;
  899. }
  900. }
  901. /**
  902. * amdgpu_switcheroo_set_state - set switcheroo state
  903. *
  904. * @pdev: pci dev pointer
  905. * @state: vga_switcheroo state
  906. *
  907. * Callback for the switcheroo driver. Suspends or resumes the
  908. * the asics before or after it is powered up using ACPI methods.
  909. */
  910. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  911. {
  912. struct drm_device *dev = pci_get_drvdata(pdev);
  913. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  914. return;
  915. if (state == VGA_SWITCHEROO_ON) {
  916. pr_info("amdgpu: switched on\n");
  917. /* don't suspend or resume card normally */
  918. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  919. amdgpu_device_resume(dev, true, true);
  920. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  921. drm_kms_helper_poll_enable(dev);
  922. } else {
  923. pr_info("amdgpu: switched off\n");
  924. drm_kms_helper_poll_disable(dev);
  925. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  926. amdgpu_device_suspend(dev, true, true);
  927. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  928. }
  929. }
  930. /**
  931. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  932. *
  933. * @pdev: pci dev pointer
  934. *
  935. * Callback for the switcheroo driver. Check of the switcheroo
  936. * state can be changed.
  937. * Returns true if the state can be changed, false if not.
  938. */
  939. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  940. {
  941. struct drm_device *dev = pci_get_drvdata(pdev);
  942. /*
  943. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  944. * locking inversion with the driver load path. And the access here is
  945. * completely racy anyway. So don't bother with locking for now.
  946. */
  947. return dev->open_count == 0;
  948. }
  949. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  950. .set_gpu_state = amdgpu_switcheroo_set_state,
  951. .reprobe = NULL,
  952. .can_switch = amdgpu_switcheroo_can_switch,
  953. };
  954. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  955. enum amd_ip_block_type block_type,
  956. enum amd_clockgating_state state)
  957. {
  958. int i, r = 0;
  959. for (i = 0; i < adev->num_ip_blocks; i++) {
  960. if (!adev->ip_blocks[i].status.valid)
  961. continue;
  962. if (adev->ip_blocks[i].version->type != block_type)
  963. continue;
  964. if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
  965. continue;
  966. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  967. (void *)adev, state);
  968. if (r)
  969. DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
  970. adev->ip_blocks[i].version->funcs->name, r);
  971. }
  972. return r;
  973. }
  974. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  975. enum amd_ip_block_type block_type,
  976. enum amd_powergating_state state)
  977. {
  978. int i, r = 0;
  979. for (i = 0; i < adev->num_ip_blocks; i++) {
  980. if (!adev->ip_blocks[i].status.valid)
  981. continue;
  982. if (adev->ip_blocks[i].version->type != block_type)
  983. continue;
  984. if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
  985. continue;
  986. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  987. (void *)adev, state);
  988. if (r)
  989. DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
  990. adev->ip_blocks[i].version->funcs->name, r);
  991. }
  992. return r;
  993. }
  994. void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
  995. {
  996. int i;
  997. for (i = 0; i < adev->num_ip_blocks; i++) {
  998. if (!adev->ip_blocks[i].status.valid)
  999. continue;
  1000. if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
  1001. adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
  1002. }
  1003. }
  1004. int amdgpu_wait_for_idle(struct amdgpu_device *adev,
  1005. enum amd_ip_block_type block_type)
  1006. {
  1007. int i, r;
  1008. for (i = 0; i < adev->num_ip_blocks; i++) {
  1009. if (!adev->ip_blocks[i].status.valid)
  1010. continue;
  1011. if (adev->ip_blocks[i].version->type == block_type) {
  1012. r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
  1013. if (r)
  1014. return r;
  1015. break;
  1016. }
  1017. }
  1018. return 0;
  1019. }
  1020. bool amdgpu_is_idle(struct amdgpu_device *adev,
  1021. enum amd_ip_block_type block_type)
  1022. {
  1023. int i;
  1024. for (i = 0; i < adev->num_ip_blocks; i++) {
  1025. if (!adev->ip_blocks[i].status.valid)
  1026. continue;
  1027. if (adev->ip_blocks[i].version->type == block_type)
  1028. return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
  1029. }
  1030. return true;
  1031. }
  1032. struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
  1033. enum amd_ip_block_type type)
  1034. {
  1035. int i;
  1036. for (i = 0; i < adev->num_ip_blocks; i++)
  1037. if (adev->ip_blocks[i].version->type == type)
  1038. return &adev->ip_blocks[i];
  1039. return NULL;
  1040. }
  1041. /**
  1042. * amdgpu_ip_block_version_cmp
  1043. *
  1044. * @adev: amdgpu_device pointer
  1045. * @type: enum amd_ip_block_type
  1046. * @major: major version
  1047. * @minor: minor version
  1048. *
  1049. * return 0 if equal or greater
  1050. * return 1 if smaller or the ip_block doesn't exist
  1051. */
  1052. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  1053. enum amd_ip_block_type type,
  1054. u32 major, u32 minor)
  1055. {
  1056. struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
  1057. if (ip_block && ((ip_block->version->major > major) ||
  1058. ((ip_block->version->major == major) &&
  1059. (ip_block->version->minor >= minor))))
  1060. return 0;
  1061. return 1;
  1062. }
  1063. /**
  1064. * amdgpu_ip_block_add
  1065. *
  1066. * @adev: amdgpu_device pointer
  1067. * @ip_block_version: pointer to the IP to add
  1068. *
  1069. * Adds the IP block driver information to the collection of IPs
  1070. * on the asic.
  1071. */
  1072. int amdgpu_ip_block_add(struct amdgpu_device *adev,
  1073. const struct amdgpu_ip_block_version *ip_block_version)
  1074. {
  1075. if (!ip_block_version)
  1076. return -EINVAL;
  1077. DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
  1078. ip_block_version->funcs->name);
  1079. adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
  1080. return 0;
  1081. }
  1082. static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
  1083. {
  1084. adev->enable_virtual_display = false;
  1085. if (amdgpu_virtual_display) {
  1086. struct drm_device *ddev = adev->ddev;
  1087. const char *pci_address_name = pci_name(ddev->pdev);
  1088. char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
  1089. pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
  1090. pciaddstr_tmp = pciaddstr;
  1091. while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
  1092. pciaddname = strsep(&pciaddname_tmp, ",");
  1093. if (!strcmp("all", pciaddname)
  1094. || !strcmp(pci_address_name, pciaddname)) {
  1095. long num_crtc;
  1096. int res = -1;
  1097. adev->enable_virtual_display = true;
  1098. if (pciaddname_tmp)
  1099. res = kstrtol(pciaddname_tmp, 10,
  1100. &num_crtc);
  1101. if (!res) {
  1102. if (num_crtc < 1)
  1103. num_crtc = 1;
  1104. if (num_crtc > 6)
  1105. num_crtc = 6;
  1106. adev->mode_info.num_crtc = num_crtc;
  1107. } else {
  1108. adev->mode_info.num_crtc = 1;
  1109. }
  1110. break;
  1111. }
  1112. }
  1113. DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
  1114. amdgpu_virtual_display, pci_address_name,
  1115. adev->enable_virtual_display, adev->mode_info.num_crtc);
  1116. kfree(pciaddstr);
  1117. }
  1118. }
  1119. static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
  1120. {
  1121. const char *chip_name;
  1122. char fw_name[30];
  1123. int err;
  1124. const struct gpu_info_firmware_header_v1_0 *hdr;
  1125. adev->firmware.gpu_info_fw = NULL;
  1126. switch (adev->asic_type) {
  1127. case CHIP_TOPAZ:
  1128. case CHIP_TONGA:
  1129. case CHIP_FIJI:
  1130. case CHIP_POLARIS11:
  1131. case CHIP_POLARIS10:
  1132. case CHIP_POLARIS12:
  1133. case CHIP_CARRIZO:
  1134. case CHIP_STONEY:
  1135. #ifdef CONFIG_DRM_AMDGPU_SI
  1136. case CHIP_VERDE:
  1137. case CHIP_TAHITI:
  1138. case CHIP_PITCAIRN:
  1139. case CHIP_OLAND:
  1140. case CHIP_HAINAN:
  1141. #endif
  1142. #ifdef CONFIG_DRM_AMDGPU_CIK
  1143. case CHIP_BONAIRE:
  1144. case CHIP_HAWAII:
  1145. case CHIP_KAVERI:
  1146. case CHIP_KABINI:
  1147. case CHIP_MULLINS:
  1148. #endif
  1149. default:
  1150. return 0;
  1151. case CHIP_VEGA10:
  1152. chip_name = "vega10";
  1153. break;
  1154. case CHIP_RAVEN:
  1155. chip_name = "raven";
  1156. break;
  1157. }
  1158. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
  1159. err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
  1160. if (err) {
  1161. dev_err(adev->dev,
  1162. "Failed to load gpu_info firmware \"%s\"\n",
  1163. fw_name);
  1164. goto out;
  1165. }
  1166. err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
  1167. if (err) {
  1168. dev_err(adev->dev,
  1169. "Failed to validate gpu_info firmware \"%s\"\n",
  1170. fw_name);
  1171. goto out;
  1172. }
  1173. hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
  1174. amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
  1175. switch (hdr->version_major) {
  1176. case 1:
  1177. {
  1178. const struct gpu_info_firmware_v1_0 *gpu_info_fw =
  1179. (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
  1180. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  1181. adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
  1182. adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
  1183. adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
  1184. adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
  1185. adev->gfx.config.max_texture_channel_caches =
  1186. le32_to_cpu(gpu_info_fw->gc_num_tccs);
  1187. adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
  1188. adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
  1189. adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
  1190. adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
  1191. adev->gfx.config.double_offchip_lds_buf =
  1192. le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
  1193. adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
  1194. adev->gfx.cu_info.max_waves_per_simd =
  1195. le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
  1196. adev->gfx.cu_info.max_scratch_slots_per_cu =
  1197. le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
  1198. adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
  1199. break;
  1200. }
  1201. default:
  1202. dev_err(adev->dev,
  1203. "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
  1204. err = -EINVAL;
  1205. goto out;
  1206. }
  1207. out:
  1208. return err;
  1209. }
  1210. static int amdgpu_early_init(struct amdgpu_device *adev)
  1211. {
  1212. int i, r;
  1213. amdgpu_device_enable_virtual_display(adev);
  1214. switch (adev->asic_type) {
  1215. case CHIP_TOPAZ:
  1216. case CHIP_TONGA:
  1217. case CHIP_FIJI:
  1218. case CHIP_POLARIS11:
  1219. case CHIP_POLARIS10:
  1220. case CHIP_POLARIS12:
  1221. case CHIP_CARRIZO:
  1222. case CHIP_STONEY:
  1223. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1224. adev->family = AMDGPU_FAMILY_CZ;
  1225. else
  1226. adev->family = AMDGPU_FAMILY_VI;
  1227. r = vi_set_ip_blocks(adev);
  1228. if (r)
  1229. return r;
  1230. break;
  1231. #ifdef CONFIG_DRM_AMDGPU_SI
  1232. case CHIP_VERDE:
  1233. case CHIP_TAHITI:
  1234. case CHIP_PITCAIRN:
  1235. case CHIP_OLAND:
  1236. case CHIP_HAINAN:
  1237. adev->family = AMDGPU_FAMILY_SI;
  1238. r = si_set_ip_blocks(adev);
  1239. if (r)
  1240. return r;
  1241. break;
  1242. #endif
  1243. #ifdef CONFIG_DRM_AMDGPU_CIK
  1244. case CHIP_BONAIRE:
  1245. case CHIP_HAWAII:
  1246. case CHIP_KAVERI:
  1247. case CHIP_KABINI:
  1248. case CHIP_MULLINS:
  1249. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1250. adev->family = AMDGPU_FAMILY_CI;
  1251. else
  1252. adev->family = AMDGPU_FAMILY_KV;
  1253. r = cik_set_ip_blocks(adev);
  1254. if (r)
  1255. return r;
  1256. break;
  1257. #endif
  1258. case CHIP_VEGA10:
  1259. case CHIP_RAVEN:
  1260. if (adev->asic_type == CHIP_RAVEN)
  1261. adev->family = AMDGPU_FAMILY_RV;
  1262. else
  1263. adev->family = AMDGPU_FAMILY_AI;
  1264. r = soc15_set_ip_blocks(adev);
  1265. if (r)
  1266. return r;
  1267. break;
  1268. default:
  1269. /* FIXME: not supported yet */
  1270. return -EINVAL;
  1271. }
  1272. r = amdgpu_device_parse_gpu_info_fw(adev);
  1273. if (r)
  1274. return r;
  1275. amdgpu_amdkfd_device_probe(adev);
  1276. if (amdgpu_sriov_vf(adev)) {
  1277. r = amdgpu_virt_request_full_gpu(adev, true);
  1278. if (r)
  1279. return -EAGAIN;
  1280. }
  1281. for (i = 0; i < adev->num_ip_blocks; i++) {
  1282. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1283. DRM_ERROR("disabled ip block: %d <%s>\n",
  1284. i, adev->ip_blocks[i].version->funcs->name);
  1285. adev->ip_blocks[i].status.valid = false;
  1286. } else {
  1287. if (adev->ip_blocks[i].version->funcs->early_init) {
  1288. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1289. if (r == -ENOENT) {
  1290. adev->ip_blocks[i].status.valid = false;
  1291. } else if (r) {
  1292. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1293. adev->ip_blocks[i].version->funcs->name, r);
  1294. return r;
  1295. } else {
  1296. adev->ip_blocks[i].status.valid = true;
  1297. }
  1298. } else {
  1299. adev->ip_blocks[i].status.valid = true;
  1300. }
  1301. }
  1302. }
  1303. adev->cg_flags &= amdgpu_cg_mask;
  1304. adev->pg_flags &= amdgpu_pg_mask;
  1305. return 0;
  1306. }
  1307. static int amdgpu_init(struct amdgpu_device *adev)
  1308. {
  1309. int i, r;
  1310. for (i = 0; i < adev->num_ip_blocks; i++) {
  1311. if (!adev->ip_blocks[i].status.valid)
  1312. continue;
  1313. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1314. if (r) {
  1315. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1316. adev->ip_blocks[i].version->funcs->name, r);
  1317. return r;
  1318. }
  1319. adev->ip_blocks[i].status.sw = true;
  1320. /* need to do gmc hw init early so we can allocate gpu mem */
  1321. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1322. r = amdgpu_vram_scratch_init(adev);
  1323. if (r) {
  1324. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1325. return r;
  1326. }
  1327. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1328. if (r) {
  1329. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1330. return r;
  1331. }
  1332. r = amdgpu_wb_init(adev);
  1333. if (r) {
  1334. DRM_ERROR("amdgpu_wb_init failed %d\n", r);
  1335. return r;
  1336. }
  1337. adev->ip_blocks[i].status.hw = true;
  1338. /* right after GMC hw init, we create CSA */
  1339. if (amdgpu_sriov_vf(adev)) {
  1340. r = amdgpu_allocate_static_csa(adev);
  1341. if (r) {
  1342. DRM_ERROR("allocate CSA failed %d\n", r);
  1343. return r;
  1344. }
  1345. }
  1346. }
  1347. }
  1348. for (i = 0; i < adev->num_ip_blocks; i++) {
  1349. if (!adev->ip_blocks[i].status.sw)
  1350. continue;
  1351. /* gmc hw init is done early */
  1352. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
  1353. continue;
  1354. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1355. if (r) {
  1356. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1357. adev->ip_blocks[i].version->funcs->name, r);
  1358. return r;
  1359. }
  1360. adev->ip_blocks[i].status.hw = true;
  1361. }
  1362. amdgpu_amdkfd_device_init(adev);
  1363. if (amdgpu_sriov_vf(adev))
  1364. amdgpu_virt_release_full_gpu(adev, true);
  1365. return 0;
  1366. }
  1367. static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
  1368. {
  1369. memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
  1370. }
  1371. static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
  1372. {
  1373. return !!memcmp(adev->gart.ptr, adev->reset_magic,
  1374. AMDGPU_RESET_MAGIC_NUM);
  1375. }
  1376. static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
  1377. {
  1378. int i = 0, r;
  1379. for (i = 0; i < adev->num_ip_blocks; i++) {
  1380. if (!adev->ip_blocks[i].status.valid)
  1381. continue;
  1382. /* skip CG for VCE/UVD, it's handled specially */
  1383. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1384. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1385. /* enable clockgating to save power */
  1386. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1387. AMD_CG_STATE_GATE);
  1388. if (r) {
  1389. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1390. adev->ip_blocks[i].version->funcs->name, r);
  1391. return r;
  1392. }
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. static int amdgpu_late_init(struct amdgpu_device *adev)
  1398. {
  1399. int i = 0, r;
  1400. for (i = 0; i < adev->num_ip_blocks; i++) {
  1401. if (!adev->ip_blocks[i].status.valid)
  1402. continue;
  1403. if (adev->ip_blocks[i].version->funcs->late_init) {
  1404. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1405. if (r) {
  1406. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1407. adev->ip_blocks[i].version->funcs->name, r);
  1408. return r;
  1409. }
  1410. adev->ip_blocks[i].status.late_initialized = true;
  1411. }
  1412. }
  1413. mod_delayed_work(system_wq, &adev->late_init_work,
  1414. msecs_to_jiffies(AMDGPU_RESUME_MS));
  1415. amdgpu_fill_reset_magic(adev);
  1416. return 0;
  1417. }
  1418. static int amdgpu_fini(struct amdgpu_device *adev)
  1419. {
  1420. int i, r;
  1421. amdgpu_amdkfd_device_fini(adev);
  1422. /* need to disable SMC first */
  1423. for (i = 0; i < adev->num_ip_blocks; i++) {
  1424. if (!adev->ip_blocks[i].status.hw)
  1425. continue;
  1426. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1427. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1428. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1429. AMD_CG_STATE_UNGATE);
  1430. if (r) {
  1431. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1432. adev->ip_blocks[i].version->funcs->name, r);
  1433. return r;
  1434. }
  1435. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1436. /* XXX handle errors */
  1437. if (r) {
  1438. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1439. adev->ip_blocks[i].version->funcs->name, r);
  1440. }
  1441. adev->ip_blocks[i].status.hw = false;
  1442. break;
  1443. }
  1444. }
  1445. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1446. if (!adev->ip_blocks[i].status.hw)
  1447. continue;
  1448. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1449. amdgpu_free_static_csa(adev);
  1450. amdgpu_wb_fini(adev);
  1451. amdgpu_vram_scratch_fini(adev);
  1452. }
  1453. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1454. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1455. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1456. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1457. AMD_CG_STATE_UNGATE);
  1458. if (r) {
  1459. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1460. adev->ip_blocks[i].version->funcs->name, r);
  1461. return r;
  1462. }
  1463. }
  1464. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1465. /* XXX handle errors */
  1466. if (r) {
  1467. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1468. adev->ip_blocks[i].version->funcs->name, r);
  1469. }
  1470. adev->ip_blocks[i].status.hw = false;
  1471. }
  1472. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1473. if (!adev->ip_blocks[i].status.sw)
  1474. continue;
  1475. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1476. /* XXX handle errors */
  1477. if (r) {
  1478. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1479. adev->ip_blocks[i].version->funcs->name, r);
  1480. }
  1481. adev->ip_blocks[i].status.sw = false;
  1482. adev->ip_blocks[i].status.valid = false;
  1483. }
  1484. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1485. if (!adev->ip_blocks[i].status.late_initialized)
  1486. continue;
  1487. if (adev->ip_blocks[i].version->funcs->late_fini)
  1488. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1489. adev->ip_blocks[i].status.late_initialized = false;
  1490. }
  1491. if (amdgpu_sriov_vf(adev))
  1492. if (amdgpu_virt_release_full_gpu(adev, false))
  1493. DRM_ERROR("failed to release exclusive mode on fini\n");
  1494. return 0;
  1495. }
  1496. static void amdgpu_late_init_func_handler(struct work_struct *work)
  1497. {
  1498. struct amdgpu_device *adev =
  1499. container_of(work, struct amdgpu_device, late_init_work.work);
  1500. amdgpu_late_set_cg_state(adev);
  1501. }
  1502. int amdgpu_suspend(struct amdgpu_device *adev)
  1503. {
  1504. int i, r;
  1505. if (amdgpu_sriov_vf(adev))
  1506. amdgpu_virt_request_full_gpu(adev, false);
  1507. /* ungate SMC block first */
  1508. r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1509. AMD_CG_STATE_UNGATE);
  1510. if (r) {
  1511. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
  1512. }
  1513. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1514. if (!adev->ip_blocks[i].status.valid)
  1515. continue;
  1516. /* ungate blocks so that suspend can properly shut them down */
  1517. if (i != AMD_IP_BLOCK_TYPE_SMC) {
  1518. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1519. AMD_CG_STATE_UNGATE);
  1520. if (r) {
  1521. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1522. adev->ip_blocks[i].version->funcs->name, r);
  1523. }
  1524. }
  1525. /* XXX handle errors */
  1526. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1527. /* XXX handle errors */
  1528. if (r) {
  1529. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1530. adev->ip_blocks[i].version->funcs->name, r);
  1531. }
  1532. }
  1533. if (amdgpu_sriov_vf(adev))
  1534. amdgpu_virt_release_full_gpu(adev, false);
  1535. return 0;
  1536. }
  1537. static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
  1538. {
  1539. int i, r;
  1540. static enum amd_ip_block_type ip_order[] = {
  1541. AMD_IP_BLOCK_TYPE_GMC,
  1542. AMD_IP_BLOCK_TYPE_COMMON,
  1543. AMD_IP_BLOCK_TYPE_IH,
  1544. };
  1545. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1546. int j;
  1547. struct amdgpu_ip_block *block;
  1548. for (j = 0; j < adev->num_ip_blocks; j++) {
  1549. block = &adev->ip_blocks[j];
  1550. if (block->version->type != ip_order[i] ||
  1551. !block->status.valid)
  1552. continue;
  1553. r = block->version->funcs->hw_init(adev);
  1554. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1555. }
  1556. }
  1557. return 0;
  1558. }
  1559. static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
  1560. {
  1561. int i, r;
  1562. static enum amd_ip_block_type ip_order[] = {
  1563. AMD_IP_BLOCK_TYPE_SMC,
  1564. AMD_IP_BLOCK_TYPE_PSP,
  1565. AMD_IP_BLOCK_TYPE_DCE,
  1566. AMD_IP_BLOCK_TYPE_GFX,
  1567. AMD_IP_BLOCK_TYPE_SDMA,
  1568. AMD_IP_BLOCK_TYPE_UVD,
  1569. AMD_IP_BLOCK_TYPE_VCE
  1570. };
  1571. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1572. int j;
  1573. struct amdgpu_ip_block *block;
  1574. for (j = 0; j < adev->num_ip_blocks; j++) {
  1575. block = &adev->ip_blocks[j];
  1576. if (block->version->type != ip_order[i] ||
  1577. !block->status.valid)
  1578. continue;
  1579. r = block->version->funcs->hw_init(adev);
  1580. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1581. }
  1582. }
  1583. return 0;
  1584. }
  1585. static int amdgpu_resume_phase1(struct amdgpu_device *adev)
  1586. {
  1587. int i, r;
  1588. for (i = 0; i < adev->num_ip_blocks; i++) {
  1589. if (!adev->ip_blocks[i].status.valid)
  1590. continue;
  1591. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1592. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1593. adev->ip_blocks[i].version->type ==
  1594. AMD_IP_BLOCK_TYPE_IH) {
  1595. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1596. if (r) {
  1597. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1598. adev->ip_blocks[i].version->funcs->name, r);
  1599. return r;
  1600. }
  1601. }
  1602. }
  1603. return 0;
  1604. }
  1605. static int amdgpu_resume_phase2(struct amdgpu_device *adev)
  1606. {
  1607. int i, r;
  1608. for (i = 0; i < adev->num_ip_blocks; i++) {
  1609. if (!adev->ip_blocks[i].status.valid)
  1610. continue;
  1611. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1612. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1613. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
  1614. continue;
  1615. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1616. if (r) {
  1617. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1618. adev->ip_blocks[i].version->funcs->name, r);
  1619. return r;
  1620. }
  1621. }
  1622. return 0;
  1623. }
  1624. static int amdgpu_resume(struct amdgpu_device *adev)
  1625. {
  1626. int r;
  1627. r = amdgpu_resume_phase1(adev);
  1628. if (r)
  1629. return r;
  1630. r = amdgpu_resume_phase2(adev);
  1631. return r;
  1632. }
  1633. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1634. {
  1635. if (amdgpu_sriov_vf(adev)) {
  1636. if (adev->is_atom_fw) {
  1637. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1638. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1639. } else {
  1640. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1641. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1642. }
  1643. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  1644. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  1645. }
  1646. }
  1647. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  1648. {
  1649. switch (asic_type) {
  1650. #if defined(CONFIG_DRM_AMD_DC)
  1651. case CHIP_BONAIRE:
  1652. case CHIP_HAWAII:
  1653. case CHIP_KAVERI:
  1654. case CHIP_CARRIZO:
  1655. case CHIP_STONEY:
  1656. case CHIP_POLARIS11:
  1657. case CHIP_POLARIS10:
  1658. case CHIP_POLARIS12:
  1659. case CHIP_TONGA:
  1660. case CHIP_FIJI:
  1661. #if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
  1662. return amdgpu_dc != 0;
  1663. #endif
  1664. case CHIP_KABINI:
  1665. case CHIP_MULLINS:
  1666. return amdgpu_dc > 0;
  1667. case CHIP_VEGA10:
  1668. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1669. case CHIP_RAVEN:
  1670. #endif
  1671. return amdgpu_dc != 0;
  1672. #endif
  1673. default:
  1674. return false;
  1675. }
  1676. }
  1677. /**
  1678. * amdgpu_device_has_dc_support - check if dc is supported
  1679. *
  1680. * @adev: amdgpu_device_pointer
  1681. *
  1682. * Returns true for supported, false for not supported
  1683. */
  1684. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  1685. {
  1686. if (amdgpu_sriov_vf(adev))
  1687. return false;
  1688. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  1689. }
  1690. /**
  1691. * amdgpu_device_init - initialize the driver
  1692. *
  1693. * @adev: amdgpu_device pointer
  1694. * @pdev: drm dev pointer
  1695. * @pdev: pci dev pointer
  1696. * @flags: driver flags
  1697. *
  1698. * Initializes the driver info and hw (all asics).
  1699. * Returns 0 for success or an error on failure.
  1700. * Called at driver startup.
  1701. */
  1702. int amdgpu_device_init(struct amdgpu_device *adev,
  1703. struct drm_device *ddev,
  1704. struct pci_dev *pdev,
  1705. uint32_t flags)
  1706. {
  1707. int r, i;
  1708. bool runtime = false;
  1709. u32 max_MBps;
  1710. adev->shutdown = false;
  1711. adev->dev = &pdev->dev;
  1712. adev->ddev = ddev;
  1713. adev->pdev = pdev;
  1714. adev->flags = flags;
  1715. adev->asic_type = flags & AMD_ASIC_MASK;
  1716. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1717. adev->mc.gart_size = 512 * 1024 * 1024;
  1718. adev->accel_working = false;
  1719. adev->num_rings = 0;
  1720. adev->mman.buffer_funcs = NULL;
  1721. adev->mman.buffer_funcs_ring = NULL;
  1722. adev->vm_manager.vm_pte_funcs = NULL;
  1723. adev->vm_manager.vm_pte_num_rings = 0;
  1724. adev->gart.gart_funcs = NULL;
  1725. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1726. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  1727. adev->smc_rreg = &amdgpu_invalid_rreg;
  1728. adev->smc_wreg = &amdgpu_invalid_wreg;
  1729. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1730. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1731. adev->pciep_rreg = &amdgpu_invalid_rreg;
  1732. adev->pciep_wreg = &amdgpu_invalid_wreg;
  1733. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1734. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1735. adev->didt_rreg = &amdgpu_invalid_rreg;
  1736. adev->didt_wreg = &amdgpu_invalid_wreg;
  1737. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  1738. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  1739. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1740. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1741. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1742. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1743. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1744. /* mutex initialization are all done here so we
  1745. * can recall function without having locking issues */
  1746. atomic_set(&adev->irq.ih.lock, 0);
  1747. mutex_init(&adev->firmware.mutex);
  1748. mutex_init(&adev->pm.mutex);
  1749. mutex_init(&adev->gfx.gpu_clock_mutex);
  1750. mutex_init(&adev->srbm_mutex);
  1751. mutex_init(&adev->gfx.pipe_reserve_mutex);
  1752. mutex_init(&adev->grbm_idx_mutex);
  1753. mutex_init(&adev->mn_lock);
  1754. mutex_init(&adev->virt.vf_errors.lock);
  1755. hash_init(adev->mn_hash);
  1756. mutex_init(&adev->lock_reset);
  1757. amdgpu_check_arguments(adev);
  1758. spin_lock_init(&adev->mmio_idx_lock);
  1759. spin_lock_init(&adev->smc_idx_lock);
  1760. spin_lock_init(&adev->pcie_idx_lock);
  1761. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1762. spin_lock_init(&adev->didt_idx_lock);
  1763. spin_lock_init(&adev->gc_cac_idx_lock);
  1764. spin_lock_init(&adev->se_cac_idx_lock);
  1765. spin_lock_init(&adev->audio_endpt_idx_lock);
  1766. spin_lock_init(&adev->mm_stats.lock);
  1767. INIT_LIST_HEAD(&adev->shadow_list);
  1768. mutex_init(&adev->shadow_list_lock);
  1769. INIT_LIST_HEAD(&adev->ring_lru_list);
  1770. spin_lock_init(&adev->ring_lru_list_lock);
  1771. INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
  1772. /* Registers mapping */
  1773. /* TODO: block userspace mapping of io register */
  1774. if (adev->asic_type >= CHIP_BONAIRE) {
  1775. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1776. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1777. } else {
  1778. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  1779. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  1780. }
  1781. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1782. if (adev->rmmio == NULL) {
  1783. return -ENOMEM;
  1784. }
  1785. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1786. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1787. /* doorbell bar mapping */
  1788. amdgpu_doorbell_init(adev);
  1789. /* io port mapping */
  1790. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1791. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1792. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1793. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1794. break;
  1795. }
  1796. }
  1797. if (adev->rio_mem == NULL)
  1798. DRM_INFO("PCI I/O BAR is not found.\n");
  1799. /* early init functions */
  1800. r = amdgpu_early_init(adev);
  1801. if (r)
  1802. return r;
  1803. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  1804. /* this will fail for cards that aren't VGA class devices, just
  1805. * ignore it */
  1806. vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
  1807. if (amdgpu_runtime_pm == 1)
  1808. runtime = true;
  1809. if (amdgpu_device_is_px(ddev))
  1810. runtime = true;
  1811. if (!pci_is_thunderbolt_attached(adev->pdev))
  1812. vga_switcheroo_register_client(adev->pdev,
  1813. &amdgpu_switcheroo_ops, runtime);
  1814. if (runtime)
  1815. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  1816. /* Read BIOS */
  1817. if (!amdgpu_get_bios(adev)) {
  1818. r = -EINVAL;
  1819. goto failed;
  1820. }
  1821. r = amdgpu_atombios_init(adev);
  1822. if (r) {
  1823. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  1824. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  1825. goto failed;
  1826. }
  1827. /* detect if we are with an SRIOV vbios */
  1828. amdgpu_device_detect_sriov_bios(adev);
  1829. /* Post card if necessary */
  1830. if (amdgpu_need_post(adev)) {
  1831. if (!adev->bios) {
  1832. dev_err(adev->dev, "no vBIOS found\n");
  1833. r = -EINVAL;
  1834. goto failed;
  1835. }
  1836. DRM_INFO("GPU posting now...\n");
  1837. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  1838. if (r) {
  1839. dev_err(adev->dev, "gpu post error!\n");
  1840. goto failed;
  1841. }
  1842. }
  1843. if (adev->is_atom_fw) {
  1844. /* Initialize clocks */
  1845. r = amdgpu_atomfirmware_get_clock_info(adev);
  1846. if (r) {
  1847. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  1848. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  1849. goto failed;
  1850. }
  1851. } else {
  1852. /* Initialize clocks */
  1853. r = amdgpu_atombios_get_clock_info(adev);
  1854. if (r) {
  1855. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  1856. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  1857. goto failed;
  1858. }
  1859. /* init i2c buses */
  1860. if (!amdgpu_device_has_dc_support(adev))
  1861. amdgpu_atombios_i2c_init(adev);
  1862. }
  1863. /* Fence driver */
  1864. r = amdgpu_fence_driver_init(adev);
  1865. if (r) {
  1866. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  1867. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  1868. goto failed;
  1869. }
  1870. /* init the mode config */
  1871. drm_mode_config_init(adev->ddev);
  1872. r = amdgpu_init(adev);
  1873. if (r) {
  1874. /* failed in exclusive mode due to timeout */
  1875. if (amdgpu_sriov_vf(adev) &&
  1876. !amdgpu_sriov_runtime(adev) &&
  1877. amdgpu_virt_mmio_blocked(adev) &&
  1878. !amdgpu_virt_wait_reset(adev)) {
  1879. dev_err(adev->dev, "VF exclusive mode timeout\n");
  1880. /* Don't send request since VF is inactive. */
  1881. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  1882. adev->virt.ops = NULL;
  1883. r = -EAGAIN;
  1884. goto failed;
  1885. }
  1886. dev_err(adev->dev, "amdgpu_init failed\n");
  1887. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  1888. amdgpu_fini(adev);
  1889. goto failed;
  1890. }
  1891. adev->accel_working = true;
  1892. amdgpu_vm_check_compute_bug(adev);
  1893. /* Initialize the buffer migration limit. */
  1894. if (amdgpu_moverate >= 0)
  1895. max_MBps = amdgpu_moverate;
  1896. else
  1897. max_MBps = 8; /* Allow 8 MB/s. */
  1898. /* Get a log2 for easy divisions. */
  1899. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  1900. r = amdgpu_ib_pool_init(adev);
  1901. if (r) {
  1902. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  1903. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  1904. goto failed;
  1905. }
  1906. r = amdgpu_ib_ring_tests(adev);
  1907. if (r)
  1908. DRM_ERROR("ib ring test failed (%d).\n", r);
  1909. if (amdgpu_sriov_vf(adev))
  1910. amdgpu_virt_init_data_exchange(adev);
  1911. amdgpu_fbdev_init(adev);
  1912. r = amdgpu_pm_sysfs_init(adev);
  1913. if (r)
  1914. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  1915. r = amdgpu_gem_debugfs_init(adev);
  1916. if (r)
  1917. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  1918. r = amdgpu_debugfs_regs_init(adev);
  1919. if (r)
  1920. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  1921. r = amdgpu_debugfs_firmware_init(adev);
  1922. if (r)
  1923. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  1924. r = amdgpu_debugfs_init(adev);
  1925. if (r)
  1926. DRM_ERROR("Creating debugfs files failed (%d).\n", r);
  1927. if ((amdgpu_testing & 1)) {
  1928. if (adev->accel_working)
  1929. amdgpu_test_moves(adev);
  1930. else
  1931. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  1932. }
  1933. if (amdgpu_benchmarking) {
  1934. if (adev->accel_working)
  1935. amdgpu_benchmark(adev, amdgpu_benchmarking);
  1936. else
  1937. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  1938. }
  1939. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  1940. * explicit gating rather than handling it automatically.
  1941. */
  1942. r = amdgpu_late_init(adev);
  1943. if (r) {
  1944. dev_err(adev->dev, "amdgpu_late_init failed\n");
  1945. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  1946. goto failed;
  1947. }
  1948. return 0;
  1949. failed:
  1950. amdgpu_vf_error_trans_all(adev);
  1951. if (runtime)
  1952. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1953. return r;
  1954. }
  1955. /**
  1956. * amdgpu_device_fini - tear down the driver
  1957. *
  1958. * @adev: amdgpu_device pointer
  1959. *
  1960. * Tear down the driver info (all asics).
  1961. * Called at driver shutdown.
  1962. */
  1963. void amdgpu_device_fini(struct amdgpu_device *adev)
  1964. {
  1965. int r;
  1966. DRM_INFO("amdgpu: finishing device.\n");
  1967. adev->shutdown = true;
  1968. if (adev->mode_info.mode_config_initialized)
  1969. drm_crtc_force_disable_all(adev->ddev);
  1970. amdgpu_ib_pool_fini(adev);
  1971. amdgpu_fence_driver_fini(adev);
  1972. amdgpu_fbdev_fini(adev);
  1973. r = amdgpu_fini(adev);
  1974. if (adev->firmware.gpu_info_fw) {
  1975. release_firmware(adev->firmware.gpu_info_fw);
  1976. adev->firmware.gpu_info_fw = NULL;
  1977. }
  1978. adev->accel_working = false;
  1979. cancel_delayed_work_sync(&adev->late_init_work);
  1980. /* free i2c buses */
  1981. if (!amdgpu_device_has_dc_support(adev))
  1982. amdgpu_i2c_fini(adev);
  1983. amdgpu_atombios_fini(adev);
  1984. kfree(adev->bios);
  1985. adev->bios = NULL;
  1986. if (!pci_is_thunderbolt_attached(adev->pdev))
  1987. vga_switcheroo_unregister_client(adev->pdev);
  1988. if (adev->flags & AMD_IS_PX)
  1989. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  1990. vga_client_register(adev->pdev, NULL, NULL, NULL);
  1991. if (adev->rio_mem)
  1992. pci_iounmap(adev->pdev, adev->rio_mem);
  1993. adev->rio_mem = NULL;
  1994. iounmap(adev->rmmio);
  1995. adev->rmmio = NULL;
  1996. amdgpu_doorbell_fini(adev);
  1997. amdgpu_pm_sysfs_fini(adev);
  1998. amdgpu_debugfs_regs_cleanup(adev);
  1999. }
  2000. /*
  2001. * Suspend & resume.
  2002. */
  2003. /**
  2004. * amdgpu_device_suspend - initiate device suspend
  2005. *
  2006. * @pdev: drm dev pointer
  2007. * @state: suspend state
  2008. *
  2009. * Puts the hw in the suspend state (all asics).
  2010. * Returns 0 for success or an error on failure.
  2011. * Called at driver suspend.
  2012. */
  2013. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  2014. {
  2015. struct amdgpu_device *adev;
  2016. struct drm_crtc *crtc;
  2017. struct drm_connector *connector;
  2018. int r;
  2019. if (dev == NULL || dev->dev_private == NULL) {
  2020. return -ENODEV;
  2021. }
  2022. adev = dev->dev_private;
  2023. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2024. return 0;
  2025. drm_kms_helper_poll_disable(dev);
  2026. if (!amdgpu_device_has_dc_support(adev)) {
  2027. /* turn off display hw */
  2028. drm_modeset_lock_all(dev);
  2029. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2030. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  2031. }
  2032. drm_modeset_unlock_all(dev);
  2033. }
  2034. amdgpu_amdkfd_suspend(adev);
  2035. /* unpin the front buffers and cursors */
  2036. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2037. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2038. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  2039. struct amdgpu_bo *robj;
  2040. if (amdgpu_crtc->cursor_bo) {
  2041. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2042. r = amdgpu_bo_reserve(aobj, true);
  2043. if (r == 0) {
  2044. amdgpu_bo_unpin(aobj);
  2045. amdgpu_bo_unreserve(aobj);
  2046. }
  2047. }
  2048. if (rfb == NULL || rfb->obj == NULL) {
  2049. continue;
  2050. }
  2051. robj = gem_to_amdgpu_bo(rfb->obj);
  2052. /* don't unpin kernel fb objects */
  2053. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2054. r = amdgpu_bo_reserve(robj, true);
  2055. if (r == 0) {
  2056. amdgpu_bo_unpin(robj);
  2057. amdgpu_bo_unreserve(robj);
  2058. }
  2059. }
  2060. }
  2061. /* evict vram memory */
  2062. amdgpu_bo_evict_vram(adev);
  2063. amdgpu_fence_driver_suspend(adev);
  2064. r = amdgpu_suspend(adev);
  2065. /* evict remaining vram memory
  2066. * This second call to evict vram is to evict the gart page table
  2067. * using the CPU.
  2068. */
  2069. amdgpu_bo_evict_vram(adev);
  2070. pci_save_state(dev->pdev);
  2071. if (suspend) {
  2072. /* Shut down the device */
  2073. pci_disable_device(dev->pdev);
  2074. pci_set_power_state(dev->pdev, PCI_D3hot);
  2075. } else {
  2076. r = amdgpu_asic_reset(adev);
  2077. if (r)
  2078. DRM_ERROR("amdgpu asic reset failed\n");
  2079. }
  2080. if (fbcon) {
  2081. console_lock();
  2082. amdgpu_fbdev_set_suspend(adev, 1);
  2083. console_unlock();
  2084. }
  2085. return 0;
  2086. }
  2087. /**
  2088. * amdgpu_device_resume - initiate device resume
  2089. *
  2090. * @pdev: drm dev pointer
  2091. *
  2092. * Bring the hw back to operating state (all asics).
  2093. * Returns 0 for success or an error on failure.
  2094. * Called at driver resume.
  2095. */
  2096. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2097. {
  2098. struct drm_connector *connector;
  2099. struct amdgpu_device *adev = dev->dev_private;
  2100. struct drm_crtc *crtc;
  2101. int r = 0;
  2102. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2103. return 0;
  2104. if (fbcon)
  2105. console_lock();
  2106. if (resume) {
  2107. pci_set_power_state(dev->pdev, PCI_D0);
  2108. pci_restore_state(dev->pdev);
  2109. r = pci_enable_device(dev->pdev);
  2110. if (r)
  2111. goto unlock;
  2112. }
  2113. /* post card */
  2114. if (amdgpu_need_post(adev)) {
  2115. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2116. if (r)
  2117. DRM_ERROR("amdgpu asic init failed\n");
  2118. }
  2119. r = amdgpu_resume(adev);
  2120. if (r) {
  2121. DRM_ERROR("amdgpu_resume failed (%d).\n", r);
  2122. goto unlock;
  2123. }
  2124. amdgpu_fence_driver_resume(adev);
  2125. if (resume) {
  2126. r = amdgpu_ib_ring_tests(adev);
  2127. if (r)
  2128. DRM_ERROR("ib ring test failed (%d).\n", r);
  2129. }
  2130. r = amdgpu_late_init(adev);
  2131. if (r)
  2132. goto unlock;
  2133. /* pin cursors */
  2134. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2135. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2136. if (amdgpu_crtc->cursor_bo) {
  2137. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2138. r = amdgpu_bo_reserve(aobj, true);
  2139. if (r == 0) {
  2140. r = amdgpu_bo_pin(aobj,
  2141. AMDGPU_GEM_DOMAIN_VRAM,
  2142. &amdgpu_crtc->cursor_addr);
  2143. if (r != 0)
  2144. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2145. amdgpu_bo_unreserve(aobj);
  2146. }
  2147. }
  2148. }
  2149. r = amdgpu_amdkfd_resume(adev);
  2150. if (r)
  2151. return r;
  2152. /* blat the mode back in */
  2153. if (fbcon) {
  2154. if (!amdgpu_device_has_dc_support(adev)) {
  2155. /* pre DCE11 */
  2156. drm_helper_resume_force_mode(dev);
  2157. /* turn on display hw */
  2158. drm_modeset_lock_all(dev);
  2159. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2160. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2161. }
  2162. drm_modeset_unlock_all(dev);
  2163. } else {
  2164. /*
  2165. * There is no equivalent atomic helper to turn on
  2166. * display, so we defined our own function for this,
  2167. * once suspend resume is supported by the atomic
  2168. * framework this will be reworked
  2169. */
  2170. amdgpu_dm_display_resume(adev);
  2171. }
  2172. }
  2173. drm_kms_helper_poll_enable(dev);
  2174. /*
  2175. * Most of the connector probing functions try to acquire runtime pm
  2176. * refs to ensure that the GPU is powered on when connector polling is
  2177. * performed. Since we're calling this from a runtime PM callback,
  2178. * trying to acquire rpm refs will cause us to deadlock.
  2179. *
  2180. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2181. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2182. */
  2183. #ifdef CONFIG_PM
  2184. dev->dev->power.disable_depth++;
  2185. #endif
  2186. if (!amdgpu_device_has_dc_support(adev))
  2187. drm_helper_hpd_irq_event(dev);
  2188. else
  2189. drm_kms_helper_hotplug_event(dev);
  2190. #ifdef CONFIG_PM
  2191. dev->dev->power.disable_depth--;
  2192. #endif
  2193. if (fbcon)
  2194. amdgpu_fbdev_set_suspend(adev, 0);
  2195. unlock:
  2196. if (fbcon)
  2197. console_unlock();
  2198. return r;
  2199. }
  2200. static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
  2201. {
  2202. int i;
  2203. bool asic_hang = false;
  2204. if (amdgpu_sriov_vf(adev))
  2205. return true;
  2206. for (i = 0; i < adev->num_ip_blocks; i++) {
  2207. if (!adev->ip_blocks[i].status.valid)
  2208. continue;
  2209. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2210. adev->ip_blocks[i].status.hang =
  2211. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2212. if (adev->ip_blocks[i].status.hang) {
  2213. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2214. asic_hang = true;
  2215. }
  2216. }
  2217. return asic_hang;
  2218. }
  2219. static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
  2220. {
  2221. int i, r = 0;
  2222. for (i = 0; i < adev->num_ip_blocks; i++) {
  2223. if (!adev->ip_blocks[i].status.valid)
  2224. continue;
  2225. if (adev->ip_blocks[i].status.hang &&
  2226. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2227. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2228. if (r)
  2229. return r;
  2230. }
  2231. }
  2232. return 0;
  2233. }
  2234. static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
  2235. {
  2236. int i;
  2237. for (i = 0; i < adev->num_ip_blocks; i++) {
  2238. if (!adev->ip_blocks[i].status.valid)
  2239. continue;
  2240. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2241. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2242. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2243. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2244. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2245. if (adev->ip_blocks[i].status.hang) {
  2246. DRM_INFO("Some block need full reset!\n");
  2247. return true;
  2248. }
  2249. }
  2250. }
  2251. return false;
  2252. }
  2253. static int amdgpu_soft_reset(struct amdgpu_device *adev)
  2254. {
  2255. int i, r = 0;
  2256. for (i = 0; i < adev->num_ip_blocks; i++) {
  2257. if (!adev->ip_blocks[i].status.valid)
  2258. continue;
  2259. if (adev->ip_blocks[i].status.hang &&
  2260. adev->ip_blocks[i].version->funcs->soft_reset) {
  2261. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2262. if (r)
  2263. return r;
  2264. }
  2265. }
  2266. return 0;
  2267. }
  2268. static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
  2269. {
  2270. int i, r = 0;
  2271. for (i = 0; i < adev->num_ip_blocks; i++) {
  2272. if (!adev->ip_blocks[i].status.valid)
  2273. continue;
  2274. if (adev->ip_blocks[i].status.hang &&
  2275. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2276. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2277. if (r)
  2278. return r;
  2279. }
  2280. return 0;
  2281. }
  2282. bool amdgpu_need_backup(struct amdgpu_device *adev)
  2283. {
  2284. if (adev->flags & AMD_IS_APU)
  2285. return false;
  2286. return amdgpu_gpu_recovery;
  2287. }
  2288. static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
  2289. struct amdgpu_ring *ring,
  2290. struct amdgpu_bo *bo,
  2291. struct dma_fence **fence)
  2292. {
  2293. uint32_t domain;
  2294. int r;
  2295. if (!bo->shadow)
  2296. return 0;
  2297. r = amdgpu_bo_reserve(bo, true);
  2298. if (r)
  2299. return r;
  2300. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2301. /* if bo has been evicted, then no need to recover */
  2302. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2303. r = amdgpu_bo_validate(bo->shadow);
  2304. if (r) {
  2305. DRM_ERROR("bo validate failed!\n");
  2306. goto err;
  2307. }
  2308. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2309. NULL, fence, true);
  2310. if (r) {
  2311. DRM_ERROR("recover page table failed!\n");
  2312. goto err;
  2313. }
  2314. }
  2315. err:
  2316. amdgpu_bo_unreserve(bo);
  2317. return r;
  2318. }
  2319. /*
  2320. * amdgpu_reset - reset ASIC/GPU for bare-metal or passthrough
  2321. *
  2322. * @adev: amdgpu device pointer
  2323. * @reset_flags: output param tells caller the reset result
  2324. *
  2325. * attempt to do soft-reset or full-reset and reinitialize Asic
  2326. * return 0 means successed otherwise failed
  2327. */
  2328. static int amdgpu_reset(struct amdgpu_device *adev, uint64_t* reset_flags)
  2329. {
  2330. bool need_full_reset, vram_lost = 0;
  2331. int r;
  2332. need_full_reset = amdgpu_need_full_reset(adev);
  2333. if (!need_full_reset) {
  2334. amdgpu_pre_soft_reset(adev);
  2335. r = amdgpu_soft_reset(adev);
  2336. amdgpu_post_soft_reset(adev);
  2337. if (r || amdgpu_check_soft_reset(adev)) {
  2338. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2339. need_full_reset = true;
  2340. }
  2341. }
  2342. if (need_full_reset) {
  2343. r = amdgpu_suspend(adev);
  2344. retry:
  2345. r = amdgpu_asic_reset(adev);
  2346. /* post card */
  2347. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2348. if (!r) {
  2349. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2350. r = amdgpu_resume_phase1(adev);
  2351. if (r)
  2352. goto out;
  2353. vram_lost = amdgpu_check_vram_lost(adev);
  2354. if (vram_lost) {
  2355. DRM_ERROR("VRAM is lost!\n");
  2356. atomic_inc(&adev->vram_lost_counter);
  2357. }
  2358. r = amdgpu_gtt_mgr_recover(
  2359. &adev->mman.bdev.man[TTM_PL_TT]);
  2360. if (r)
  2361. goto out;
  2362. r = amdgpu_resume_phase2(adev);
  2363. if (r)
  2364. goto out;
  2365. if (vram_lost)
  2366. amdgpu_fill_reset_magic(adev);
  2367. }
  2368. }
  2369. out:
  2370. if (!r) {
  2371. amdgpu_irq_gpu_reset_resume_helper(adev);
  2372. r = amdgpu_ib_ring_tests(adev);
  2373. if (r) {
  2374. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2375. r = amdgpu_suspend(adev);
  2376. need_full_reset = true;
  2377. goto retry;
  2378. }
  2379. }
  2380. if (reset_flags) {
  2381. if (vram_lost)
  2382. (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
  2383. if (need_full_reset)
  2384. (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
  2385. }
  2386. return r;
  2387. }
  2388. /*
  2389. * amdgpu_reset_sriov - reset ASIC for SR-IOV vf
  2390. *
  2391. * @adev: amdgpu device pointer
  2392. * @reset_flags: output param tells caller the reset result
  2393. *
  2394. * do VF FLR and reinitialize Asic
  2395. * return 0 means successed otherwise failed
  2396. */
  2397. static int amdgpu_reset_sriov(struct amdgpu_device *adev, uint64_t *reset_flags, bool from_hypervisor)
  2398. {
  2399. int r;
  2400. if (from_hypervisor)
  2401. r = amdgpu_virt_request_full_gpu(adev, true);
  2402. else
  2403. r = amdgpu_virt_reset_gpu(adev);
  2404. if (r)
  2405. return r;
  2406. /* Resume IP prior to SMC */
  2407. r = amdgpu_sriov_reinit_early(adev);
  2408. if (r)
  2409. goto error;
  2410. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2411. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2412. /* now we are okay to resume SMC/CP/SDMA */
  2413. r = amdgpu_sriov_reinit_late(adev);
  2414. if (r)
  2415. goto error;
  2416. amdgpu_irq_gpu_reset_resume_helper(adev);
  2417. r = amdgpu_ib_ring_tests(adev);
  2418. if (r)
  2419. dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
  2420. error:
  2421. /* release full control of GPU after ib test */
  2422. amdgpu_virt_release_full_gpu(adev, true);
  2423. if (reset_flags) {
  2424. if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2425. (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
  2426. atomic_inc(&adev->vram_lost_counter);
  2427. }
  2428. /* VF FLR or hotlink reset is always full-reset */
  2429. (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
  2430. }
  2431. return r;
  2432. }
  2433. /**
  2434. * amdgpu_gpu_recover - reset the asic and recover scheduler
  2435. *
  2436. * @adev: amdgpu device pointer
  2437. * @job: which job trigger hang
  2438. * @force forces reset regardless of amdgpu_gpu_recovery
  2439. *
  2440. * Attempt to reset the GPU if it has hung (all asics).
  2441. * Returns 0 for success or an error on failure.
  2442. */
  2443. int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job, bool force)
  2444. {
  2445. struct drm_atomic_state *state = NULL;
  2446. uint64_t reset_flags = 0;
  2447. int i, r, resched;
  2448. if (!amdgpu_check_soft_reset(adev)) {
  2449. DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
  2450. return 0;
  2451. }
  2452. if (!force && (amdgpu_gpu_recovery == 0 ||
  2453. (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
  2454. DRM_INFO("GPU recovery disabled.\n");
  2455. return 0;
  2456. }
  2457. dev_info(adev->dev, "GPU reset begin!\n");
  2458. mutex_lock(&adev->lock_reset);
  2459. atomic_inc(&adev->gpu_reset_counter);
  2460. adev->in_gpu_reset = 1;
  2461. /* block TTM */
  2462. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2463. /* store modesetting */
  2464. if (amdgpu_device_has_dc_support(adev))
  2465. state = drm_atomic_helper_suspend(adev->ddev);
  2466. /* block scheduler */
  2467. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2468. struct amdgpu_ring *ring = adev->rings[i];
  2469. if (!ring || !ring->sched.thread)
  2470. continue;
  2471. /* only focus on the ring hit timeout if &job not NULL */
  2472. if (job && job->ring->idx != i)
  2473. continue;
  2474. kthread_park(ring->sched.thread);
  2475. drm_sched_hw_job_reset(&ring->sched, &job->base);
  2476. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2477. amdgpu_fence_driver_force_completion(ring);
  2478. }
  2479. if (amdgpu_sriov_vf(adev))
  2480. r = amdgpu_reset_sriov(adev, &reset_flags, job ? false : true);
  2481. else
  2482. r = amdgpu_reset(adev, &reset_flags);
  2483. if (!r) {
  2484. if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
  2485. (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
  2486. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2487. struct amdgpu_bo *bo, *tmp;
  2488. struct dma_fence *fence = NULL, *next = NULL;
  2489. DRM_INFO("recover vram bo from shadow\n");
  2490. mutex_lock(&adev->shadow_list_lock);
  2491. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2492. next = NULL;
  2493. amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
  2494. if (fence) {
  2495. r = dma_fence_wait(fence, false);
  2496. if (r) {
  2497. WARN(r, "recovery from shadow isn't completed\n");
  2498. break;
  2499. }
  2500. }
  2501. dma_fence_put(fence);
  2502. fence = next;
  2503. }
  2504. mutex_unlock(&adev->shadow_list_lock);
  2505. if (fence) {
  2506. r = dma_fence_wait(fence, false);
  2507. if (r)
  2508. WARN(r, "recovery from shadow isn't completed\n");
  2509. }
  2510. dma_fence_put(fence);
  2511. }
  2512. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2513. struct amdgpu_ring *ring = adev->rings[i];
  2514. if (!ring || !ring->sched.thread)
  2515. continue;
  2516. /* only focus on the ring hit timeout if &job not NULL */
  2517. if (job && job->ring->idx != i)
  2518. continue;
  2519. drm_sched_job_recovery(&ring->sched);
  2520. kthread_unpark(ring->sched.thread);
  2521. }
  2522. } else {
  2523. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2524. struct amdgpu_ring *ring = adev->rings[i];
  2525. if (!ring || !ring->sched.thread)
  2526. continue;
  2527. /* only focus on the ring hit timeout if &job not NULL */
  2528. if (job && job->ring->idx != i)
  2529. continue;
  2530. kthread_unpark(adev->rings[i]->sched.thread);
  2531. }
  2532. }
  2533. if (amdgpu_device_has_dc_support(adev)) {
  2534. if (drm_atomic_helper_resume(adev->ddev, state))
  2535. dev_info(adev->dev, "drm resume failed:%d\n", r);
  2536. amdgpu_dm_display_resume(adev);
  2537. } else {
  2538. drm_helper_resume_force_mode(adev->ddev);
  2539. }
  2540. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2541. if (r) {
  2542. /* bad news, how to tell it to userspace ? */
  2543. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2544. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2545. } else {
  2546. dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
  2547. }
  2548. amdgpu_vf_error_trans_all(adev);
  2549. adev->in_gpu_reset = 0;
  2550. mutex_unlock(&adev->lock_reset);
  2551. return r;
  2552. }
  2553. void amdgpu_get_pcie_info(struct amdgpu_device *adev)
  2554. {
  2555. u32 mask;
  2556. int ret;
  2557. if (amdgpu_pcie_gen_cap)
  2558. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2559. if (amdgpu_pcie_lane_cap)
  2560. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2561. /* covers APUs as well */
  2562. if (pci_is_root_bus(adev->pdev->bus)) {
  2563. if (adev->pm.pcie_gen_mask == 0)
  2564. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2565. if (adev->pm.pcie_mlw_mask == 0)
  2566. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2567. return;
  2568. }
  2569. if (adev->pm.pcie_gen_mask == 0) {
  2570. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  2571. if (!ret) {
  2572. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2573. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2574. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2575. if (mask & DRM_PCIE_SPEED_25)
  2576. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2577. if (mask & DRM_PCIE_SPEED_50)
  2578. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  2579. if (mask & DRM_PCIE_SPEED_80)
  2580. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  2581. } else {
  2582. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2583. }
  2584. }
  2585. if (adev->pm.pcie_mlw_mask == 0) {
  2586. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  2587. if (!ret) {
  2588. switch (mask) {
  2589. case 32:
  2590. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2591. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2592. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2593. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2594. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2595. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2596. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2597. break;
  2598. case 16:
  2599. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2600. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2601. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2602. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2603. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2604. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2605. break;
  2606. case 12:
  2607. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2608. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2609. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2610. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2611. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2612. break;
  2613. case 8:
  2614. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2615. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2616. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2617. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2618. break;
  2619. case 4:
  2620. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2621. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2622. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2623. break;
  2624. case 2:
  2625. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2626. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2627. break;
  2628. case 1:
  2629. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  2630. break;
  2631. default:
  2632. break;
  2633. }
  2634. } else {
  2635. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2636. }
  2637. }
  2638. }
  2639. /*
  2640. * Debugfs
  2641. */
  2642. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  2643. const struct drm_info_list *files,
  2644. unsigned nfiles)
  2645. {
  2646. unsigned i;
  2647. for (i = 0; i < adev->debugfs_count; i++) {
  2648. if (adev->debugfs[i].files == files) {
  2649. /* Already registered */
  2650. return 0;
  2651. }
  2652. }
  2653. i = adev->debugfs_count + 1;
  2654. if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
  2655. DRM_ERROR("Reached maximum number of debugfs components.\n");
  2656. DRM_ERROR("Report so we increase "
  2657. "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
  2658. return -EINVAL;
  2659. }
  2660. adev->debugfs[adev->debugfs_count].files = files;
  2661. adev->debugfs[adev->debugfs_count].num_files = nfiles;
  2662. adev->debugfs_count = i;
  2663. #if defined(CONFIG_DEBUG_FS)
  2664. drm_debugfs_create_files(files, nfiles,
  2665. adev->ddev->primary->debugfs_root,
  2666. adev->ddev->primary);
  2667. #endif
  2668. return 0;
  2669. }
  2670. #if defined(CONFIG_DEBUG_FS)
  2671. static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
  2672. size_t size, loff_t *pos)
  2673. {
  2674. struct amdgpu_device *adev = file_inode(f)->i_private;
  2675. ssize_t result = 0;
  2676. int r;
  2677. bool pm_pg_lock, use_bank;
  2678. unsigned instance_bank, sh_bank, se_bank;
  2679. if (size & 0x3 || *pos & 0x3)
  2680. return -EINVAL;
  2681. /* are we reading registers for which a PG lock is necessary? */
  2682. pm_pg_lock = (*pos >> 23) & 1;
  2683. if (*pos & (1ULL << 62)) {
  2684. se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
  2685. sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
  2686. instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
  2687. if (se_bank == 0x3FF)
  2688. se_bank = 0xFFFFFFFF;
  2689. if (sh_bank == 0x3FF)
  2690. sh_bank = 0xFFFFFFFF;
  2691. if (instance_bank == 0x3FF)
  2692. instance_bank = 0xFFFFFFFF;
  2693. use_bank = 1;
  2694. } else {
  2695. use_bank = 0;
  2696. }
  2697. *pos &= (1UL << 22) - 1;
  2698. if (use_bank) {
  2699. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2700. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2701. return -EINVAL;
  2702. mutex_lock(&adev->grbm_idx_mutex);
  2703. amdgpu_gfx_select_se_sh(adev, se_bank,
  2704. sh_bank, instance_bank);
  2705. }
  2706. if (pm_pg_lock)
  2707. mutex_lock(&adev->pm.mutex);
  2708. while (size) {
  2709. uint32_t value;
  2710. if (*pos > adev->rmmio_size)
  2711. goto end;
  2712. value = RREG32(*pos >> 2);
  2713. r = put_user(value, (uint32_t *)buf);
  2714. if (r) {
  2715. result = r;
  2716. goto end;
  2717. }
  2718. result += 4;
  2719. buf += 4;
  2720. *pos += 4;
  2721. size -= 4;
  2722. }
  2723. end:
  2724. if (use_bank) {
  2725. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2726. mutex_unlock(&adev->grbm_idx_mutex);
  2727. }
  2728. if (pm_pg_lock)
  2729. mutex_unlock(&adev->pm.mutex);
  2730. return result;
  2731. }
  2732. static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
  2733. size_t size, loff_t *pos)
  2734. {
  2735. struct amdgpu_device *adev = file_inode(f)->i_private;
  2736. ssize_t result = 0;
  2737. int r;
  2738. bool pm_pg_lock, use_bank;
  2739. unsigned instance_bank, sh_bank, se_bank;
  2740. if (size & 0x3 || *pos & 0x3)
  2741. return -EINVAL;
  2742. /* are we reading registers for which a PG lock is necessary? */
  2743. pm_pg_lock = (*pos >> 23) & 1;
  2744. if (*pos & (1ULL << 62)) {
  2745. se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
  2746. sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
  2747. instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
  2748. if (se_bank == 0x3FF)
  2749. se_bank = 0xFFFFFFFF;
  2750. if (sh_bank == 0x3FF)
  2751. sh_bank = 0xFFFFFFFF;
  2752. if (instance_bank == 0x3FF)
  2753. instance_bank = 0xFFFFFFFF;
  2754. use_bank = 1;
  2755. } else {
  2756. use_bank = 0;
  2757. }
  2758. *pos &= (1UL << 22) - 1;
  2759. if (use_bank) {
  2760. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2761. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2762. return -EINVAL;
  2763. mutex_lock(&adev->grbm_idx_mutex);
  2764. amdgpu_gfx_select_se_sh(adev, se_bank,
  2765. sh_bank, instance_bank);
  2766. }
  2767. if (pm_pg_lock)
  2768. mutex_lock(&adev->pm.mutex);
  2769. while (size) {
  2770. uint32_t value;
  2771. if (*pos > adev->rmmio_size)
  2772. return result;
  2773. r = get_user(value, (uint32_t *)buf);
  2774. if (r)
  2775. return r;
  2776. WREG32(*pos >> 2, value);
  2777. result += 4;
  2778. buf += 4;
  2779. *pos += 4;
  2780. size -= 4;
  2781. }
  2782. if (use_bank) {
  2783. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2784. mutex_unlock(&adev->grbm_idx_mutex);
  2785. }
  2786. if (pm_pg_lock)
  2787. mutex_unlock(&adev->pm.mutex);
  2788. return result;
  2789. }
  2790. static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
  2791. size_t size, loff_t *pos)
  2792. {
  2793. struct amdgpu_device *adev = file_inode(f)->i_private;
  2794. ssize_t result = 0;
  2795. int r;
  2796. if (size & 0x3 || *pos & 0x3)
  2797. return -EINVAL;
  2798. while (size) {
  2799. uint32_t value;
  2800. value = RREG32_PCIE(*pos >> 2);
  2801. r = put_user(value, (uint32_t *)buf);
  2802. if (r)
  2803. return r;
  2804. result += 4;
  2805. buf += 4;
  2806. *pos += 4;
  2807. size -= 4;
  2808. }
  2809. return result;
  2810. }
  2811. static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
  2812. size_t size, loff_t *pos)
  2813. {
  2814. struct amdgpu_device *adev = file_inode(f)->i_private;
  2815. ssize_t result = 0;
  2816. int r;
  2817. if (size & 0x3 || *pos & 0x3)
  2818. return -EINVAL;
  2819. while (size) {
  2820. uint32_t value;
  2821. r = get_user(value, (uint32_t *)buf);
  2822. if (r)
  2823. return r;
  2824. WREG32_PCIE(*pos >> 2, value);
  2825. result += 4;
  2826. buf += 4;
  2827. *pos += 4;
  2828. size -= 4;
  2829. }
  2830. return result;
  2831. }
  2832. static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
  2833. size_t size, loff_t *pos)
  2834. {
  2835. struct amdgpu_device *adev = file_inode(f)->i_private;
  2836. ssize_t result = 0;
  2837. int r;
  2838. if (size & 0x3 || *pos & 0x3)
  2839. return -EINVAL;
  2840. while (size) {
  2841. uint32_t value;
  2842. value = RREG32_DIDT(*pos >> 2);
  2843. r = put_user(value, (uint32_t *)buf);
  2844. if (r)
  2845. return r;
  2846. result += 4;
  2847. buf += 4;
  2848. *pos += 4;
  2849. size -= 4;
  2850. }
  2851. return result;
  2852. }
  2853. static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
  2854. size_t size, loff_t *pos)
  2855. {
  2856. struct amdgpu_device *adev = file_inode(f)->i_private;
  2857. ssize_t result = 0;
  2858. int r;
  2859. if (size & 0x3 || *pos & 0x3)
  2860. return -EINVAL;
  2861. while (size) {
  2862. uint32_t value;
  2863. r = get_user(value, (uint32_t *)buf);
  2864. if (r)
  2865. return r;
  2866. WREG32_DIDT(*pos >> 2, value);
  2867. result += 4;
  2868. buf += 4;
  2869. *pos += 4;
  2870. size -= 4;
  2871. }
  2872. return result;
  2873. }
  2874. static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
  2875. size_t size, loff_t *pos)
  2876. {
  2877. struct amdgpu_device *adev = file_inode(f)->i_private;
  2878. ssize_t result = 0;
  2879. int r;
  2880. if (size & 0x3 || *pos & 0x3)
  2881. return -EINVAL;
  2882. while (size) {
  2883. uint32_t value;
  2884. value = RREG32_SMC(*pos);
  2885. r = put_user(value, (uint32_t *)buf);
  2886. if (r)
  2887. return r;
  2888. result += 4;
  2889. buf += 4;
  2890. *pos += 4;
  2891. size -= 4;
  2892. }
  2893. return result;
  2894. }
  2895. static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
  2896. size_t size, loff_t *pos)
  2897. {
  2898. struct amdgpu_device *adev = file_inode(f)->i_private;
  2899. ssize_t result = 0;
  2900. int r;
  2901. if (size & 0x3 || *pos & 0x3)
  2902. return -EINVAL;
  2903. while (size) {
  2904. uint32_t value;
  2905. r = get_user(value, (uint32_t *)buf);
  2906. if (r)
  2907. return r;
  2908. WREG32_SMC(*pos, value);
  2909. result += 4;
  2910. buf += 4;
  2911. *pos += 4;
  2912. size -= 4;
  2913. }
  2914. return result;
  2915. }
  2916. static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
  2917. size_t size, loff_t *pos)
  2918. {
  2919. struct amdgpu_device *adev = file_inode(f)->i_private;
  2920. ssize_t result = 0;
  2921. int r;
  2922. uint32_t *config, no_regs = 0;
  2923. if (size & 0x3 || *pos & 0x3)
  2924. return -EINVAL;
  2925. config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
  2926. if (!config)
  2927. return -ENOMEM;
  2928. /* version, increment each time something is added */
  2929. config[no_regs++] = 3;
  2930. config[no_regs++] = adev->gfx.config.max_shader_engines;
  2931. config[no_regs++] = adev->gfx.config.max_tile_pipes;
  2932. config[no_regs++] = adev->gfx.config.max_cu_per_sh;
  2933. config[no_regs++] = adev->gfx.config.max_sh_per_se;
  2934. config[no_regs++] = adev->gfx.config.max_backends_per_se;
  2935. config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
  2936. config[no_regs++] = adev->gfx.config.max_gprs;
  2937. config[no_regs++] = adev->gfx.config.max_gs_threads;
  2938. config[no_regs++] = adev->gfx.config.max_hw_contexts;
  2939. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
  2940. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
  2941. config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
  2942. config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
  2943. config[no_regs++] = adev->gfx.config.num_tile_pipes;
  2944. config[no_regs++] = adev->gfx.config.backend_enable_mask;
  2945. config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
  2946. config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
  2947. config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
  2948. config[no_regs++] = adev->gfx.config.num_gpus;
  2949. config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
  2950. config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
  2951. config[no_regs++] = adev->gfx.config.gb_addr_config;
  2952. config[no_regs++] = adev->gfx.config.num_rbs;
  2953. /* rev==1 */
  2954. config[no_regs++] = adev->rev_id;
  2955. config[no_regs++] = adev->pg_flags;
  2956. config[no_regs++] = adev->cg_flags;
  2957. /* rev==2 */
  2958. config[no_regs++] = adev->family;
  2959. config[no_regs++] = adev->external_rev_id;
  2960. /* rev==3 */
  2961. config[no_regs++] = adev->pdev->device;
  2962. config[no_regs++] = adev->pdev->revision;
  2963. config[no_regs++] = adev->pdev->subsystem_device;
  2964. config[no_regs++] = adev->pdev->subsystem_vendor;
  2965. while (size && (*pos < no_regs * 4)) {
  2966. uint32_t value;
  2967. value = config[*pos >> 2];
  2968. r = put_user(value, (uint32_t *)buf);
  2969. if (r) {
  2970. kfree(config);
  2971. return r;
  2972. }
  2973. result += 4;
  2974. buf += 4;
  2975. *pos += 4;
  2976. size -= 4;
  2977. }
  2978. kfree(config);
  2979. return result;
  2980. }
  2981. static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
  2982. size_t size, loff_t *pos)
  2983. {
  2984. struct amdgpu_device *adev = file_inode(f)->i_private;
  2985. int idx, x, outsize, r, valuesize;
  2986. uint32_t values[16];
  2987. if (size & 3 || *pos & 0x3)
  2988. return -EINVAL;
  2989. if (amdgpu_dpm == 0)
  2990. return -EINVAL;
  2991. /* convert offset to sensor number */
  2992. idx = *pos >> 2;
  2993. valuesize = sizeof(values);
  2994. if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
  2995. r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
  2996. else
  2997. return -EINVAL;
  2998. if (size > valuesize)
  2999. return -EINVAL;
  3000. outsize = 0;
  3001. x = 0;
  3002. if (!r) {
  3003. while (size) {
  3004. r = put_user(values[x++], (int32_t *)buf);
  3005. buf += 4;
  3006. size -= 4;
  3007. outsize += 4;
  3008. }
  3009. }
  3010. return !r ? outsize : r;
  3011. }
  3012. static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
  3013. size_t size, loff_t *pos)
  3014. {
  3015. struct amdgpu_device *adev = f->f_inode->i_private;
  3016. int r, x;
  3017. ssize_t result=0;
  3018. uint32_t offset, se, sh, cu, wave, simd, data[32];
  3019. if (size & 3 || *pos & 3)
  3020. return -EINVAL;
  3021. /* decode offset */
  3022. offset = (*pos & GENMASK_ULL(6, 0));
  3023. se = (*pos & GENMASK_ULL(14, 7)) >> 7;
  3024. sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
  3025. cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
  3026. wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
  3027. simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
  3028. /* switch to the specific se/sh/cu */
  3029. mutex_lock(&adev->grbm_idx_mutex);
  3030. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  3031. x = 0;
  3032. if (adev->gfx.funcs->read_wave_data)
  3033. adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
  3034. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  3035. mutex_unlock(&adev->grbm_idx_mutex);
  3036. if (!x)
  3037. return -EINVAL;
  3038. while (size && (offset < x * 4)) {
  3039. uint32_t value;
  3040. value = data[offset >> 2];
  3041. r = put_user(value, (uint32_t *)buf);
  3042. if (r)
  3043. return r;
  3044. result += 4;
  3045. buf += 4;
  3046. offset += 4;
  3047. size -= 4;
  3048. }
  3049. return result;
  3050. }
  3051. static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
  3052. size_t size, loff_t *pos)
  3053. {
  3054. struct amdgpu_device *adev = f->f_inode->i_private;
  3055. int r;
  3056. ssize_t result = 0;
  3057. uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
  3058. if (size & 3 || *pos & 3)
  3059. return -EINVAL;
  3060. /* decode offset */
  3061. offset = *pos & GENMASK_ULL(11, 0);
  3062. se = (*pos & GENMASK_ULL(19, 12)) >> 12;
  3063. sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
  3064. cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
  3065. wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
  3066. simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
  3067. thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
  3068. bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
  3069. data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
  3070. if (!data)
  3071. return -ENOMEM;
  3072. /* switch to the specific se/sh/cu */
  3073. mutex_lock(&adev->grbm_idx_mutex);
  3074. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  3075. if (bank == 0) {
  3076. if (adev->gfx.funcs->read_wave_vgprs)
  3077. adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
  3078. } else {
  3079. if (adev->gfx.funcs->read_wave_sgprs)
  3080. adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
  3081. }
  3082. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  3083. mutex_unlock(&adev->grbm_idx_mutex);
  3084. while (size) {
  3085. uint32_t value;
  3086. value = data[offset++];
  3087. r = put_user(value, (uint32_t *)buf);
  3088. if (r) {
  3089. result = r;
  3090. goto err;
  3091. }
  3092. result += 4;
  3093. buf += 4;
  3094. size -= 4;
  3095. }
  3096. err:
  3097. kfree(data);
  3098. return result;
  3099. }
  3100. static const struct file_operations amdgpu_debugfs_regs_fops = {
  3101. .owner = THIS_MODULE,
  3102. .read = amdgpu_debugfs_regs_read,
  3103. .write = amdgpu_debugfs_regs_write,
  3104. .llseek = default_llseek
  3105. };
  3106. static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
  3107. .owner = THIS_MODULE,
  3108. .read = amdgpu_debugfs_regs_didt_read,
  3109. .write = amdgpu_debugfs_regs_didt_write,
  3110. .llseek = default_llseek
  3111. };
  3112. static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
  3113. .owner = THIS_MODULE,
  3114. .read = amdgpu_debugfs_regs_pcie_read,
  3115. .write = amdgpu_debugfs_regs_pcie_write,
  3116. .llseek = default_llseek
  3117. };
  3118. static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
  3119. .owner = THIS_MODULE,
  3120. .read = amdgpu_debugfs_regs_smc_read,
  3121. .write = amdgpu_debugfs_regs_smc_write,
  3122. .llseek = default_llseek
  3123. };
  3124. static const struct file_operations amdgpu_debugfs_gca_config_fops = {
  3125. .owner = THIS_MODULE,
  3126. .read = amdgpu_debugfs_gca_config_read,
  3127. .llseek = default_llseek
  3128. };
  3129. static const struct file_operations amdgpu_debugfs_sensors_fops = {
  3130. .owner = THIS_MODULE,
  3131. .read = amdgpu_debugfs_sensor_read,
  3132. .llseek = default_llseek
  3133. };
  3134. static const struct file_operations amdgpu_debugfs_wave_fops = {
  3135. .owner = THIS_MODULE,
  3136. .read = amdgpu_debugfs_wave_read,
  3137. .llseek = default_llseek
  3138. };
  3139. static const struct file_operations amdgpu_debugfs_gpr_fops = {
  3140. .owner = THIS_MODULE,
  3141. .read = amdgpu_debugfs_gpr_read,
  3142. .llseek = default_llseek
  3143. };
  3144. static const struct file_operations *debugfs_regs[] = {
  3145. &amdgpu_debugfs_regs_fops,
  3146. &amdgpu_debugfs_regs_didt_fops,
  3147. &amdgpu_debugfs_regs_pcie_fops,
  3148. &amdgpu_debugfs_regs_smc_fops,
  3149. &amdgpu_debugfs_gca_config_fops,
  3150. &amdgpu_debugfs_sensors_fops,
  3151. &amdgpu_debugfs_wave_fops,
  3152. &amdgpu_debugfs_gpr_fops,
  3153. };
  3154. static const char *debugfs_regs_names[] = {
  3155. "amdgpu_regs",
  3156. "amdgpu_regs_didt",
  3157. "amdgpu_regs_pcie",
  3158. "amdgpu_regs_smc",
  3159. "amdgpu_gca_config",
  3160. "amdgpu_sensors",
  3161. "amdgpu_wave",
  3162. "amdgpu_gpr",
  3163. };
  3164. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3165. {
  3166. struct drm_minor *minor = adev->ddev->primary;
  3167. struct dentry *ent, *root = minor->debugfs_root;
  3168. unsigned i, j;
  3169. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3170. ent = debugfs_create_file(debugfs_regs_names[i],
  3171. S_IFREG | S_IRUGO, root,
  3172. adev, debugfs_regs[i]);
  3173. if (IS_ERR(ent)) {
  3174. for (j = 0; j < i; j++) {
  3175. debugfs_remove(adev->debugfs_regs[i]);
  3176. adev->debugfs_regs[i] = NULL;
  3177. }
  3178. return PTR_ERR(ent);
  3179. }
  3180. if (!i)
  3181. i_size_write(ent->d_inode, adev->rmmio_size);
  3182. adev->debugfs_regs[i] = ent;
  3183. }
  3184. return 0;
  3185. }
  3186. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
  3187. {
  3188. unsigned i;
  3189. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3190. if (adev->debugfs_regs[i]) {
  3191. debugfs_remove(adev->debugfs_regs[i]);
  3192. adev->debugfs_regs[i] = NULL;
  3193. }
  3194. }
  3195. }
  3196. static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
  3197. {
  3198. struct drm_info_node *node = (struct drm_info_node *) m->private;
  3199. struct drm_device *dev = node->minor->dev;
  3200. struct amdgpu_device *adev = dev->dev_private;
  3201. int r = 0, i;
  3202. /* hold on the scheduler */
  3203. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  3204. struct amdgpu_ring *ring = adev->rings[i];
  3205. if (!ring || !ring->sched.thread)
  3206. continue;
  3207. kthread_park(ring->sched.thread);
  3208. }
  3209. seq_printf(m, "run ib test:\n");
  3210. r = amdgpu_ib_ring_tests(adev);
  3211. if (r)
  3212. seq_printf(m, "ib ring tests failed (%d).\n", r);
  3213. else
  3214. seq_printf(m, "ib ring tests passed.\n");
  3215. /* go on the scheduler */
  3216. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  3217. struct amdgpu_ring *ring = adev->rings[i];
  3218. if (!ring || !ring->sched.thread)
  3219. continue;
  3220. kthread_unpark(ring->sched.thread);
  3221. }
  3222. return 0;
  3223. }
  3224. static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
  3225. {
  3226. struct drm_info_node *node = (struct drm_info_node *) m->private;
  3227. struct drm_device *dev = node->minor->dev;
  3228. struct amdgpu_device *adev = dev->dev_private;
  3229. seq_write(m, adev->bios, adev->bios_size);
  3230. return 0;
  3231. }
  3232. static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
  3233. {
  3234. struct drm_info_node *node = (struct drm_info_node *)m->private;
  3235. struct drm_device *dev = node->minor->dev;
  3236. struct amdgpu_device *adev = dev->dev_private;
  3237. seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
  3238. return 0;
  3239. }
  3240. static const struct drm_info_list amdgpu_debugfs_list[] = {
  3241. {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
  3242. {"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
  3243. {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram}
  3244. };
  3245. static int amdgpu_debugfs_init(struct amdgpu_device *adev)
  3246. {
  3247. return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
  3248. ARRAY_SIZE(amdgpu_debugfs_list));
  3249. }
  3250. #else
  3251. static int amdgpu_debugfs_init(struct amdgpu_device *adev)
  3252. {
  3253. return 0;
  3254. }
  3255. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3256. {
  3257. return 0;
  3258. }
  3259. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
  3260. #endif