amdgpu_device.c 102 KB

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