amdgpu_device.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017
  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. amdgpu_vm_block_size = -1;
  1043. }
  1044. }
  1045. static void amdgpu_check_vm_size(struct amdgpu_device *adev)
  1046. {
  1047. /* no need to check the default value */
  1048. if (amdgpu_vm_size == -1)
  1049. return;
  1050. if (amdgpu_vm_size < 1) {
  1051. dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
  1052. amdgpu_vm_size);
  1053. amdgpu_vm_size = -1;
  1054. }
  1055. }
  1056. /**
  1057. * amdgpu_check_arguments - validate module params
  1058. *
  1059. * @adev: amdgpu_device pointer
  1060. *
  1061. * Validates certain module parameters and updates
  1062. * the associated values used by the driver (all asics).
  1063. */
  1064. static void amdgpu_check_arguments(struct amdgpu_device *adev)
  1065. {
  1066. if (amdgpu_sched_jobs < 4) {
  1067. dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
  1068. amdgpu_sched_jobs);
  1069. amdgpu_sched_jobs = 4;
  1070. } else if (!is_power_of_2(amdgpu_sched_jobs)){
  1071. dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
  1072. amdgpu_sched_jobs);
  1073. amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
  1074. }
  1075. if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
  1076. /* gart size must be greater or equal to 32M */
  1077. dev_warn(adev->dev, "gart size (%d) too small\n",
  1078. amdgpu_gart_size);
  1079. amdgpu_gart_size = -1;
  1080. }
  1081. if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
  1082. /* gtt size must be greater or equal to 32M */
  1083. dev_warn(adev->dev, "gtt size (%d) too small\n",
  1084. amdgpu_gtt_size);
  1085. amdgpu_gtt_size = -1;
  1086. }
  1087. /* valid range is between 4 and 9 inclusive */
  1088. if (amdgpu_vm_fragment_size != -1 &&
  1089. (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
  1090. dev_warn(adev->dev, "valid range is between 4 and 9\n");
  1091. amdgpu_vm_fragment_size = -1;
  1092. }
  1093. amdgpu_check_vm_size(adev);
  1094. amdgpu_check_block_size(adev);
  1095. if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
  1096. !is_power_of_2(amdgpu_vram_page_split))) {
  1097. dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
  1098. amdgpu_vram_page_split);
  1099. amdgpu_vram_page_split = 1024;
  1100. }
  1101. }
  1102. /**
  1103. * amdgpu_switcheroo_set_state - set switcheroo state
  1104. *
  1105. * @pdev: pci dev pointer
  1106. * @state: vga_switcheroo state
  1107. *
  1108. * Callback for the switcheroo driver. Suspends or resumes the
  1109. * the asics before or after it is powered up using ACPI methods.
  1110. */
  1111. static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  1112. {
  1113. struct drm_device *dev = pci_get_drvdata(pdev);
  1114. if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
  1115. return;
  1116. if (state == VGA_SWITCHEROO_ON) {
  1117. pr_info("amdgpu: switched on\n");
  1118. /* don't suspend or resume card normally */
  1119. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1120. amdgpu_device_resume(dev, true, true);
  1121. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  1122. drm_kms_helper_poll_enable(dev);
  1123. } else {
  1124. pr_info("amdgpu: switched off\n");
  1125. drm_kms_helper_poll_disable(dev);
  1126. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  1127. amdgpu_device_suspend(dev, true, true);
  1128. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  1129. }
  1130. }
  1131. /**
  1132. * amdgpu_switcheroo_can_switch - see if switcheroo state can change
  1133. *
  1134. * @pdev: pci dev pointer
  1135. *
  1136. * Callback for the switcheroo driver. Check of the switcheroo
  1137. * state can be changed.
  1138. * Returns true if the state can be changed, false if not.
  1139. */
  1140. static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
  1141. {
  1142. struct drm_device *dev = pci_get_drvdata(pdev);
  1143. /*
  1144. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  1145. * locking inversion with the driver load path. And the access here is
  1146. * completely racy anyway. So don't bother with locking for now.
  1147. */
  1148. return dev->open_count == 0;
  1149. }
  1150. static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
  1151. .set_gpu_state = amdgpu_switcheroo_set_state,
  1152. .reprobe = NULL,
  1153. .can_switch = amdgpu_switcheroo_can_switch,
  1154. };
  1155. int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
  1156. enum amd_ip_block_type block_type,
  1157. enum amd_clockgating_state state)
  1158. {
  1159. int i, r = 0;
  1160. for (i = 0; i < adev->num_ip_blocks; i++) {
  1161. if (!adev->ip_blocks[i].status.valid)
  1162. continue;
  1163. if (adev->ip_blocks[i].version->type != block_type)
  1164. continue;
  1165. if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
  1166. continue;
  1167. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  1168. (void *)adev, state);
  1169. if (r)
  1170. DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
  1171. adev->ip_blocks[i].version->funcs->name, r);
  1172. }
  1173. return r;
  1174. }
  1175. int amdgpu_set_powergating_state(struct amdgpu_device *adev,
  1176. enum amd_ip_block_type block_type,
  1177. enum amd_powergating_state state)
  1178. {
  1179. int i, r = 0;
  1180. for (i = 0; i < adev->num_ip_blocks; i++) {
  1181. if (!adev->ip_blocks[i].status.valid)
  1182. continue;
  1183. if (adev->ip_blocks[i].version->type != block_type)
  1184. continue;
  1185. if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
  1186. continue;
  1187. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  1188. (void *)adev, state);
  1189. if (r)
  1190. DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
  1191. adev->ip_blocks[i].version->funcs->name, r);
  1192. }
  1193. return r;
  1194. }
  1195. void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
  1196. {
  1197. int i;
  1198. for (i = 0; i < adev->num_ip_blocks; i++) {
  1199. if (!adev->ip_blocks[i].status.valid)
  1200. continue;
  1201. if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
  1202. adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
  1203. }
  1204. }
  1205. int amdgpu_wait_for_idle(struct amdgpu_device *adev,
  1206. enum amd_ip_block_type block_type)
  1207. {
  1208. int i, r;
  1209. for (i = 0; i < adev->num_ip_blocks; i++) {
  1210. if (!adev->ip_blocks[i].status.valid)
  1211. continue;
  1212. if (adev->ip_blocks[i].version->type == block_type) {
  1213. r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
  1214. if (r)
  1215. return r;
  1216. break;
  1217. }
  1218. }
  1219. return 0;
  1220. }
  1221. bool amdgpu_is_idle(struct amdgpu_device *adev,
  1222. enum amd_ip_block_type block_type)
  1223. {
  1224. int i;
  1225. for (i = 0; i < adev->num_ip_blocks; i++) {
  1226. if (!adev->ip_blocks[i].status.valid)
  1227. continue;
  1228. if (adev->ip_blocks[i].version->type == block_type)
  1229. return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
  1230. }
  1231. return true;
  1232. }
  1233. struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
  1234. enum amd_ip_block_type type)
  1235. {
  1236. int i;
  1237. for (i = 0; i < adev->num_ip_blocks; i++)
  1238. if (adev->ip_blocks[i].version->type == type)
  1239. return &adev->ip_blocks[i];
  1240. return NULL;
  1241. }
  1242. /**
  1243. * amdgpu_ip_block_version_cmp
  1244. *
  1245. * @adev: amdgpu_device pointer
  1246. * @type: enum amd_ip_block_type
  1247. * @major: major version
  1248. * @minor: minor version
  1249. *
  1250. * return 0 if equal or greater
  1251. * return 1 if smaller or the ip_block doesn't exist
  1252. */
  1253. int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
  1254. enum amd_ip_block_type type,
  1255. u32 major, u32 minor)
  1256. {
  1257. struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
  1258. if (ip_block && ((ip_block->version->major > major) ||
  1259. ((ip_block->version->major == major) &&
  1260. (ip_block->version->minor >= minor))))
  1261. return 0;
  1262. return 1;
  1263. }
  1264. /**
  1265. * amdgpu_ip_block_add
  1266. *
  1267. * @adev: amdgpu_device pointer
  1268. * @ip_block_version: pointer to the IP to add
  1269. *
  1270. * Adds the IP block driver information to the collection of IPs
  1271. * on the asic.
  1272. */
  1273. int amdgpu_ip_block_add(struct amdgpu_device *adev,
  1274. const struct amdgpu_ip_block_version *ip_block_version)
  1275. {
  1276. if (!ip_block_version)
  1277. return -EINVAL;
  1278. DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
  1279. ip_block_version->funcs->name);
  1280. adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
  1281. return 0;
  1282. }
  1283. static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
  1284. {
  1285. adev->enable_virtual_display = false;
  1286. if (amdgpu_virtual_display) {
  1287. struct drm_device *ddev = adev->ddev;
  1288. const char *pci_address_name = pci_name(ddev->pdev);
  1289. char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
  1290. pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
  1291. pciaddstr_tmp = pciaddstr;
  1292. while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
  1293. pciaddname = strsep(&pciaddname_tmp, ",");
  1294. if (!strcmp("all", pciaddname)
  1295. || !strcmp(pci_address_name, pciaddname)) {
  1296. long num_crtc;
  1297. int res = -1;
  1298. adev->enable_virtual_display = true;
  1299. if (pciaddname_tmp)
  1300. res = kstrtol(pciaddname_tmp, 10,
  1301. &num_crtc);
  1302. if (!res) {
  1303. if (num_crtc < 1)
  1304. num_crtc = 1;
  1305. if (num_crtc > 6)
  1306. num_crtc = 6;
  1307. adev->mode_info.num_crtc = num_crtc;
  1308. } else {
  1309. adev->mode_info.num_crtc = 1;
  1310. }
  1311. break;
  1312. }
  1313. }
  1314. DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
  1315. amdgpu_virtual_display, pci_address_name,
  1316. adev->enable_virtual_display, adev->mode_info.num_crtc);
  1317. kfree(pciaddstr);
  1318. }
  1319. }
  1320. static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
  1321. {
  1322. const char *chip_name;
  1323. char fw_name[30];
  1324. int err;
  1325. const struct gpu_info_firmware_header_v1_0 *hdr;
  1326. adev->firmware.gpu_info_fw = NULL;
  1327. switch (adev->asic_type) {
  1328. case CHIP_TOPAZ:
  1329. case CHIP_TONGA:
  1330. case CHIP_FIJI:
  1331. case CHIP_POLARIS11:
  1332. case CHIP_POLARIS10:
  1333. case CHIP_POLARIS12:
  1334. case CHIP_CARRIZO:
  1335. case CHIP_STONEY:
  1336. #ifdef CONFIG_DRM_AMDGPU_SI
  1337. case CHIP_VERDE:
  1338. case CHIP_TAHITI:
  1339. case CHIP_PITCAIRN:
  1340. case CHIP_OLAND:
  1341. case CHIP_HAINAN:
  1342. #endif
  1343. #ifdef CONFIG_DRM_AMDGPU_CIK
  1344. case CHIP_BONAIRE:
  1345. case CHIP_HAWAII:
  1346. case CHIP_KAVERI:
  1347. case CHIP_KABINI:
  1348. case CHIP_MULLINS:
  1349. #endif
  1350. default:
  1351. return 0;
  1352. case CHIP_VEGA10:
  1353. chip_name = "vega10";
  1354. break;
  1355. case CHIP_RAVEN:
  1356. chip_name = "raven";
  1357. break;
  1358. }
  1359. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
  1360. err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
  1361. if (err) {
  1362. dev_err(adev->dev,
  1363. "Failed to load gpu_info firmware \"%s\"\n",
  1364. fw_name);
  1365. goto out;
  1366. }
  1367. err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
  1368. if (err) {
  1369. dev_err(adev->dev,
  1370. "Failed to validate gpu_info firmware \"%s\"\n",
  1371. fw_name);
  1372. goto out;
  1373. }
  1374. hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
  1375. amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
  1376. switch (hdr->version_major) {
  1377. case 1:
  1378. {
  1379. const struct gpu_info_firmware_v1_0 *gpu_info_fw =
  1380. (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
  1381. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  1382. adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
  1383. adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
  1384. adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
  1385. adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
  1386. adev->gfx.config.max_texture_channel_caches =
  1387. le32_to_cpu(gpu_info_fw->gc_num_tccs);
  1388. adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
  1389. adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
  1390. adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
  1391. adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
  1392. adev->gfx.config.double_offchip_lds_buf =
  1393. le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
  1394. adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
  1395. adev->gfx.cu_info.max_waves_per_simd =
  1396. le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
  1397. adev->gfx.cu_info.max_scratch_slots_per_cu =
  1398. le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
  1399. adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
  1400. break;
  1401. }
  1402. default:
  1403. dev_err(adev->dev,
  1404. "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
  1405. err = -EINVAL;
  1406. goto out;
  1407. }
  1408. out:
  1409. return err;
  1410. }
  1411. static int amdgpu_early_init(struct amdgpu_device *adev)
  1412. {
  1413. int i, r;
  1414. amdgpu_device_enable_virtual_display(adev);
  1415. switch (adev->asic_type) {
  1416. case CHIP_TOPAZ:
  1417. case CHIP_TONGA:
  1418. case CHIP_FIJI:
  1419. case CHIP_POLARIS11:
  1420. case CHIP_POLARIS10:
  1421. case CHIP_POLARIS12:
  1422. case CHIP_CARRIZO:
  1423. case CHIP_STONEY:
  1424. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1425. adev->family = AMDGPU_FAMILY_CZ;
  1426. else
  1427. adev->family = AMDGPU_FAMILY_VI;
  1428. r = vi_set_ip_blocks(adev);
  1429. if (r)
  1430. return r;
  1431. break;
  1432. #ifdef CONFIG_DRM_AMDGPU_SI
  1433. case CHIP_VERDE:
  1434. case CHIP_TAHITI:
  1435. case CHIP_PITCAIRN:
  1436. case CHIP_OLAND:
  1437. case CHIP_HAINAN:
  1438. adev->family = AMDGPU_FAMILY_SI;
  1439. r = si_set_ip_blocks(adev);
  1440. if (r)
  1441. return r;
  1442. break;
  1443. #endif
  1444. #ifdef CONFIG_DRM_AMDGPU_CIK
  1445. case CHIP_BONAIRE:
  1446. case CHIP_HAWAII:
  1447. case CHIP_KAVERI:
  1448. case CHIP_KABINI:
  1449. case CHIP_MULLINS:
  1450. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1451. adev->family = AMDGPU_FAMILY_CI;
  1452. else
  1453. adev->family = AMDGPU_FAMILY_KV;
  1454. r = cik_set_ip_blocks(adev);
  1455. if (r)
  1456. return r;
  1457. break;
  1458. #endif
  1459. case CHIP_VEGA10:
  1460. case CHIP_RAVEN:
  1461. if (adev->asic_type == CHIP_RAVEN)
  1462. adev->family = AMDGPU_FAMILY_RV;
  1463. else
  1464. adev->family = AMDGPU_FAMILY_AI;
  1465. r = soc15_set_ip_blocks(adev);
  1466. if (r)
  1467. return r;
  1468. break;
  1469. default:
  1470. /* FIXME: not supported yet */
  1471. return -EINVAL;
  1472. }
  1473. r = amdgpu_device_parse_gpu_info_fw(adev);
  1474. if (r)
  1475. return r;
  1476. amdgpu_amdkfd_device_probe(adev);
  1477. if (amdgpu_sriov_vf(adev)) {
  1478. r = amdgpu_virt_request_full_gpu(adev, true);
  1479. if (r)
  1480. return -EAGAIN;
  1481. }
  1482. for (i = 0; i < adev->num_ip_blocks; i++) {
  1483. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1484. DRM_ERROR("disabled ip block: %d <%s>\n",
  1485. i, adev->ip_blocks[i].version->funcs->name);
  1486. adev->ip_blocks[i].status.valid = false;
  1487. } else {
  1488. if (adev->ip_blocks[i].version->funcs->early_init) {
  1489. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1490. if (r == -ENOENT) {
  1491. adev->ip_blocks[i].status.valid = false;
  1492. } else if (r) {
  1493. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1494. adev->ip_blocks[i].version->funcs->name, r);
  1495. return r;
  1496. } else {
  1497. adev->ip_blocks[i].status.valid = true;
  1498. }
  1499. } else {
  1500. adev->ip_blocks[i].status.valid = true;
  1501. }
  1502. }
  1503. }
  1504. adev->cg_flags &= amdgpu_cg_mask;
  1505. adev->pg_flags &= amdgpu_pg_mask;
  1506. return 0;
  1507. }
  1508. static int amdgpu_init(struct amdgpu_device *adev)
  1509. {
  1510. int i, r;
  1511. for (i = 0; i < adev->num_ip_blocks; i++) {
  1512. if (!adev->ip_blocks[i].status.valid)
  1513. continue;
  1514. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1515. if (r) {
  1516. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1517. adev->ip_blocks[i].version->funcs->name, r);
  1518. return r;
  1519. }
  1520. adev->ip_blocks[i].status.sw = true;
  1521. /* need to do gmc hw init early so we can allocate gpu mem */
  1522. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1523. r = amdgpu_vram_scratch_init(adev);
  1524. if (r) {
  1525. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1526. return r;
  1527. }
  1528. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1529. if (r) {
  1530. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1531. return r;
  1532. }
  1533. r = amdgpu_wb_init(adev);
  1534. if (r) {
  1535. DRM_ERROR("amdgpu_wb_init failed %d\n", r);
  1536. return r;
  1537. }
  1538. adev->ip_blocks[i].status.hw = true;
  1539. /* right after GMC hw init, we create CSA */
  1540. if (amdgpu_sriov_vf(adev)) {
  1541. r = amdgpu_allocate_static_csa(adev);
  1542. if (r) {
  1543. DRM_ERROR("allocate CSA failed %d\n", r);
  1544. return r;
  1545. }
  1546. }
  1547. }
  1548. }
  1549. for (i = 0; i < adev->num_ip_blocks; i++) {
  1550. if (!adev->ip_blocks[i].status.sw)
  1551. continue;
  1552. /* gmc hw init is done early */
  1553. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
  1554. continue;
  1555. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1556. if (r) {
  1557. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1558. adev->ip_blocks[i].version->funcs->name, r);
  1559. return r;
  1560. }
  1561. adev->ip_blocks[i].status.hw = true;
  1562. }
  1563. amdgpu_amdkfd_device_init(adev);
  1564. if (amdgpu_sriov_vf(adev))
  1565. amdgpu_virt_release_full_gpu(adev, true);
  1566. return 0;
  1567. }
  1568. static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
  1569. {
  1570. memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
  1571. }
  1572. static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
  1573. {
  1574. return !!memcmp(adev->gart.ptr, adev->reset_magic,
  1575. AMDGPU_RESET_MAGIC_NUM);
  1576. }
  1577. static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
  1578. {
  1579. int i = 0, r;
  1580. for (i = 0; i < adev->num_ip_blocks; i++) {
  1581. if (!adev->ip_blocks[i].status.valid)
  1582. continue;
  1583. /* skip CG for VCE/UVD, it's handled specially */
  1584. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1585. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1586. /* enable clockgating to save power */
  1587. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1588. AMD_CG_STATE_GATE);
  1589. if (r) {
  1590. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1591. adev->ip_blocks[i].version->funcs->name, r);
  1592. return r;
  1593. }
  1594. }
  1595. }
  1596. return 0;
  1597. }
  1598. static int amdgpu_late_init(struct amdgpu_device *adev)
  1599. {
  1600. int i = 0, r;
  1601. for (i = 0; i < adev->num_ip_blocks; i++) {
  1602. if (!adev->ip_blocks[i].status.valid)
  1603. continue;
  1604. if (adev->ip_blocks[i].version->funcs->late_init) {
  1605. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1606. if (r) {
  1607. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1608. adev->ip_blocks[i].version->funcs->name, r);
  1609. return r;
  1610. }
  1611. adev->ip_blocks[i].status.late_initialized = true;
  1612. }
  1613. }
  1614. mod_delayed_work(system_wq, &adev->late_init_work,
  1615. msecs_to_jiffies(AMDGPU_RESUME_MS));
  1616. amdgpu_fill_reset_magic(adev);
  1617. return 0;
  1618. }
  1619. static int amdgpu_fini(struct amdgpu_device *adev)
  1620. {
  1621. int i, r;
  1622. amdgpu_amdkfd_device_fini(adev);
  1623. /* need to disable SMC first */
  1624. for (i = 0; i < adev->num_ip_blocks; i++) {
  1625. if (!adev->ip_blocks[i].status.hw)
  1626. continue;
  1627. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1628. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1629. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1630. AMD_CG_STATE_UNGATE);
  1631. if (r) {
  1632. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1633. adev->ip_blocks[i].version->funcs->name, r);
  1634. return r;
  1635. }
  1636. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1637. /* XXX handle errors */
  1638. if (r) {
  1639. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1640. adev->ip_blocks[i].version->funcs->name, r);
  1641. }
  1642. adev->ip_blocks[i].status.hw = false;
  1643. break;
  1644. }
  1645. }
  1646. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1647. if (!adev->ip_blocks[i].status.hw)
  1648. continue;
  1649. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1650. amdgpu_free_static_csa(adev);
  1651. amdgpu_wb_fini(adev);
  1652. amdgpu_vram_scratch_fini(adev);
  1653. }
  1654. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1655. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
  1656. /* ungate blocks before hw fini so that we can shutdown the blocks safely */
  1657. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1658. AMD_CG_STATE_UNGATE);
  1659. if (r) {
  1660. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1661. adev->ip_blocks[i].version->funcs->name, r);
  1662. return r;
  1663. }
  1664. }
  1665. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1666. /* XXX handle errors */
  1667. if (r) {
  1668. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1669. adev->ip_blocks[i].version->funcs->name, r);
  1670. }
  1671. adev->ip_blocks[i].status.hw = false;
  1672. }
  1673. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1674. if (!adev->ip_blocks[i].status.sw)
  1675. continue;
  1676. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1677. /* XXX handle errors */
  1678. if (r) {
  1679. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1680. adev->ip_blocks[i].version->funcs->name, r);
  1681. }
  1682. adev->ip_blocks[i].status.sw = false;
  1683. adev->ip_blocks[i].status.valid = false;
  1684. }
  1685. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1686. if (!adev->ip_blocks[i].status.late_initialized)
  1687. continue;
  1688. if (adev->ip_blocks[i].version->funcs->late_fini)
  1689. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1690. adev->ip_blocks[i].status.late_initialized = false;
  1691. }
  1692. if (amdgpu_sriov_vf(adev))
  1693. if (amdgpu_virt_release_full_gpu(adev, false))
  1694. DRM_ERROR("failed to release exclusive mode on fini\n");
  1695. return 0;
  1696. }
  1697. static void amdgpu_late_init_func_handler(struct work_struct *work)
  1698. {
  1699. struct amdgpu_device *adev =
  1700. container_of(work, struct amdgpu_device, late_init_work.work);
  1701. amdgpu_late_set_cg_state(adev);
  1702. }
  1703. int amdgpu_suspend(struct amdgpu_device *adev)
  1704. {
  1705. int i, r;
  1706. if (amdgpu_sriov_vf(adev))
  1707. amdgpu_virt_request_full_gpu(adev, false);
  1708. /* ungate SMC block first */
  1709. r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
  1710. AMD_CG_STATE_UNGATE);
  1711. if (r) {
  1712. DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
  1713. }
  1714. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1715. if (!adev->ip_blocks[i].status.valid)
  1716. continue;
  1717. /* ungate blocks so that suspend can properly shut them down */
  1718. if (i != AMD_IP_BLOCK_TYPE_SMC) {
  1719. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1720. AMD_CG_STATE_UNGATE);
  1721. if (r) {
  1722. DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
  1723. adev->ip_blocks[i].version->funcs->name, r);
  1724. }
  1725. }
  1726. /* XXX handle errors */
  1727. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1728. /* XXX handle errors */
  1729. if (r) {
  1730. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1731. adev->ip_blocks[i].version->funcs->name, r);
  1732. }
  1733. }
  1734. if (amdgpu_sriov_vf(adev))
  1735. amdgpu_virt_release_full_gpu(adev, false);
  1736. return 0;
  1737. }
  1738. static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
  1739. {
  1740. int i, r;
  1741. static enum amd_ip_block_type ip_order[] = {
  1742. AMD_IP_BLOCK_TYPE_GMC,
  1743. AMD_IP_BLOCK_TYPE_COMMON,
  1744. AMD_IP_BLOCK_TYPE_IH,
  1745. };
  1746. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1747. int j;
  1748. struct amdgpu_ip_block *block;
  1749. for (j = 0; j < adev->num_ip_blocks; j++) {
  1750. block = &adev->ip_blocks[j];
  1751. if (block->version->type != ip_order[i] ||
  1752. !block->status.valid)
  1753. continue;
  1754. r = block->version->funcs->hw_init(adev);
  1755. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1756. }
  1757. }
  1758. return 0;
  1759. }
  1760. static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
  1761. {
  1762. int i, r;
  1763. static enum amd_ip_block_type ip_order[] = {
  1764. AMD_IP_BLOCK_TYPE_SMC,
  1765. AMD_IP_BLOCK_TYPE_PSP,
  1766. AMD_IP_BLOCK_TYPE_DCE,
  1767. AMD_IP_BLOCK_TYPE_GFX,
  1768. AMD_IP_BLOCK_TYPE_SDMA,
  1769. AMD_IP_BLOCK_TYPE_UVD,
  1770. AMD_IP_BLOCK_TYPE_VCE
  1771. };
  1772. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1773. int j;
  1774. struct amdgpu_ip_block *block;
  1775. for (j = 0; j < adev->num_ip_blocks; j++) {
  1776. block = &adev->ip_blocks[j];
  1777. if (block->version->type != ip_order[i] ||
  1778. !block->status.valid)
  1779. continue;
  1780. r = block->version->funcs->hw_init(adev);
  1781. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
  1782. }
  1783. }
  1784. return 0;
  1785. }
  1786. static int amdgpu_resume_phase1(struct amdgpu_device *adev)
  1787. {
  1788. int i, r;
  1789. for (i = 0; i < adev->num_ip_blocks; i++) {
  1790. if (!adev->ip_blocks[i].status.valid)
  1791. continue;
  1792. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1793. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1794. adev->ip_blocks[i].version->type ==
  1795. AMD_IP_BLOCK_TYPE_IH) {
  1796. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1797. if (r) {
  1798. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1799. adev->ip_blocks[i].version->funcs->name, r);
  1800. return r;
  1801. }
  1802. }
  1803. }
  1804. return 0;
  1805. }
  1806. static int amdgpu_resume_phase2(struct amdgpu_device *adev)
  1807. {
  1808. int i, r;
  1809. for (i = 0; i < adev->num_ip_blocks; i++) {
  1810. if (!adev->ip_blocks[i].status.valid)
  1811. continue;
  1812. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1813. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1814. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
  1815. continue;
  1816. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1817. if (r) {
  1818. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1819. adev->ip_blocks[i].version->funcs->name, r);
  1820. return r;
  1821. }
  1822. }
  1823. return 0;
  1824. }
  1825. static int amdgpu_resume(struct amdgpu_device *adev)
  1826. {
  1827. int r;
  1828. r = amdgpu_resume_phase1(adev);
  1829. if (r)
  1830. return r;
  1831. r = amdgpu_resume_phase2(adev);
  1832. return r;
  1833. }
  1834. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1835. {
  1836. if (amdgpu_sriov_vf(adev)) {
  1837. if (adev->is_atom_fw) {
  1838. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1839. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1840. } else {
  1841. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1842. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1843. }
  1844. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  1845. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  1846. }
  1847. }
  1848. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  1849. {
  1850. switch (asic_type) {
  1851. #if defined(CONFIG_DRM_AMD_DC)
  1852. case CHIP_BONAIRE:
  1853. case CHIP_HAWAII:
  1854. case CHIP_KAVERI:
  1855. case CHIP_CARRIZO:
  1856. case CHIP_STONEY:
  1857. case CHIP_POLARIS11:
  1858. case CHIP_POLARIS10:
  1859. case CHIP_POLARIS12:
  1860. case CHIP_TONGA:
  1861. case CHIP_FIJI:
  1862. #if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
  1863. return amdgpu_dc != 0;
  1864. #endif
  1865. case CHIP_KABINI:
  1866. case CHIP_MULLINS:
  1867. return amdgpu_dc > 0;
  1868. case CHIP_VEGA10:
  1869. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1870. case CHIP_RAVEN:
  1871. #endif
  1872. return amdgpu_dc != 0;
  1873. #endif
  1874. default:
  1875. return false;
  1876. }
  1877. }
  1878. /**
  1879. * amdgpu_device_has_dc_support - check if dc is supported
  1880. *
  1881. * @adev: amdgpu_device_pointer
  1882. *
  1883. * Returns true for supported, false for not supported
  1884. */
  1885. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  1886. {
  1887. if (amdgpu_sriov_vf(adev))
  1888. return false;
  1889. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  1890. }
  1891. /**
  1892. * amdgpu_device_init - initialize the driver
  1893. *
  1894. * @adev: amdgpu_device pointer
  1895. * @pdev: drm dev pointer
  1896. * @pdev: pci dev pointer
  1897. * @flags: driver flags
  1898. *
  1899. * Initializes the driver info and hw (all asics).
  1900. * Returns 0 for success or an error on failure.
  1901. * Called at driver startup.
  1902. */
  1903. int amdgpu_device_init(struct amdgpu_device *adev,
  1904. struct drm_device *ddev,
  1905. struct pci_dev *pdev,
  1906. uint32_t flags)
  1907. {
  1908. int r, i;
  1909. bool runtime = false;
  1910. u32 max_MBps;
  1911. adev->shutdown = false;
  1912. adev->dev = &pdev->dev;
  1913. adev->ddev = ddev;
  1914. adev->pdev = pdev;
  1915. adev->flags = flags;
  1916. adev->asic_type = flags & AMD_ASIC_MASK;
  1917. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1918. adev->mc.gart_size = 512 * 1024 * 1024;
  1919. adev->accel_working = false;
  1920. adev->num_rings = 0;
  1921. adev->mman.buffer_funcs = NULL;
  1922. adev->mman.buffer_funcs_ring = NULL;
  1923. adev->vm_manager.vm_pte_funcs = NULL;
  1924. adev->vm_manager.vm_pte_num_rings = 0;
  1925. adev->gart.gart_funcs = NULL;
  1926. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  1927. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  1928. adev->smc_rreg = &amdgpu_invalid_rreg;
  1929. adev->smc_wreg = &amdgpu_invalid_wreg;
  1930. adev->pcie_rreg = &amdgpu_invalid_rreg;
  1931. adev->pcie_wreg = &amdgpu_invalid_wreg;
  1932. adev->pciep_rreg = &amdgpu_invalid_rreg;
  1933. adev->pciep_wreg = &amdgpu_invalid_wreg;
  1934. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  1935. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  1936. adev->didt_rreg = &amdgpu_invalid_rreg;
  1937. adev->didt_wreg = &amdgpu_invalid_wreg;
  1938. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  1939. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  1940. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  1941. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  1942. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  1943. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  1944. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  1945. /* mutex initialization are all done here so we
  1946. * can recall function without having locking issues */
  1947. atomic_set(&adev->irq.ih.lock, 0);
  1948. mutex_init(&adev->firmware.mutex);
  1949. mutex_init(&adev->pm.mutex);
  1950. mutex_init(&adev->gfx.gpu_clock_mutex);
  1951. mutex_init(&adev->srbm_mutex);
  1952. mutex_init(&adev->gfx.pipe_reserve_mutex);
  1953. mutex_init(&adev->grbm_idx_mutex);
  1954. mutex_init(&adev->mn_lock);
  1955. mutex_init(&adev->virt.vf_errors.lock);
  1956. hash_init(adev->mn_hash);
  1957. mutex_init(&adev->lock_reset);
  1958. amdgpu_check_arguments(adev);
  1959. spin_lock_init(&adev->mmio_idx_lock);
  1960. spin_lock_init(&adev->smc_idx_lock);
  1961. spin_lock_init(&adev->pcie_idx_lock);
  1962. spin_lock_init(&adev->uvd_ctx_idx_lock);
  1963. spin_lock_init(&adev->didt_idx_lock);
  1964. spin_lock_init(&adev->gc_cac_idx_lock);
  1965. spin_lock_init(&adev->se_cac_idx_lock);
  1966. spin_lock_init(&adev->audio_endpt_idx_lock);
  1967. spin_lock_init(&adev->mm_stats.lock);
  1968. INIT_LIST_HEAD(&adev->shadow_list);
  1969. mutex_init(&adev->shadow_list_lock);
  1970. INIT_LIST_HEAD(&adev->ring_lru_list);
  1971. spin_lock_init(&adev->ring_lru_list_lock);
  1972. INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
  1973. /* Registers mapping */
  1974. /* TODO: block userspace mapping of io register */
  1975. if (adev->asic_type >= CHIP_BONAIRE) {
  1976. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  1977. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  1978. } else {
  1979. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  1980. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  1981. }
  1982. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  1983. if (adev->rmmio == NULL) {
  1984. return -ENOMEM;
  1985. }
  1986. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  1987. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  1988. /* doorbell bar mapping */
  1989. amdgpu_doorbell_init(adev);
  1990. /* io port mapping */
  1991. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  1992. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  1993. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  1994. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  1995. break;
  1996. }
  1997. }
  1998. if (adev->rio_mem == NULL)
  1999. DRM_INFO("PCI I/O BAR is not found.\n");
  2000. /* early init functions */
  2001. r = amdgpu_early_init(adev);
  2002. if (r)
  2003. return r;
  2004. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  2005. /* this will fail for cards that aren't VGA class devices, just
  2006. * ignore it */
  2007. vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
  2008. if (amdgpu_runtime_pm == 1)
  2009. runtime = true;
  2010. if (amdgpu_device_is_px(ddev))
  2011. runtime = true;
  2012. if (!pci_is_thunderbolt_attached(adev->pdev))
  2013. vga_switcheroo_register_client(adev->pdev,
  2014. &amdgpu_switcheroo_ops, runtime);
  2015. if (runtime)
  2016. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  2017. /* Read BIOS */
  2018. if (!amdgpu_get_bios(adev)) {
  2019. r = -EINVAL;
  2020. goto failed;
  2021. }
  2022. r = amdgpu_atombios_init(adev);
  2023. if (r) {
  2024. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  2025. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  2026. goto failed;
  2027. }
  2028. /* detect if we are with an SRIOV vbios */
  2029. amdgpu_device_detect_sriov_bios(adev);
  2030. /* Post card if necessary */
  2031. if (amdgpu_need_post(adev)) {
  2032. if (!adev->bios) {
  2033. dev_err(adev->dev, "no vBIOS found\n");
  2034. r = -EINVAL;
  2035. goto failed;
  2036. }
  2037. DRM_INFO("GPU posting now...\n");
  2038. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2039. if (r) {
  2040. dev_err(adev->dev, "gpu post error!\n");
  2041. goto failed;
  2042. }
  2043. }
  2044. if (adev->is_atom_fw) {
  2045. /* Initialize clocks */
  2046. r = amdgpu_atomfirmware_get_clock_info(adev);
  2047. if (r) {
  2048. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  2049. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2050. goto failed;
  2051. }
  2052. } else {
  2053. /* Initialize clocks */
  2054. r = amdgpu_atombios_get_clock_info(adev);
  2055. if (r) {
  2056. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  2057. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2058. goto failed;
  2059. }
  2060. /* init i2c buses */
  2061. if (!amdgpu_device_has_dc_support(adev))
  2062. amdgpu_atombios_i2c_init(adev);
  2063. }
  2064. /* Fence driver */
  2065. r = amdgpu_fence_driver_init(adev);
  2066. if (r) {
  2067. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  2068. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  2069. goto failed;
  2070. }
  2071. /* init the mode config */
  2072. drm_mode_config_init(adev->ddev);
  2073. r = amdgpu_init(adev);
  2074. if (r) {
  2075. /* failed in exclusive mode due to timeout */
  2076. if (amdgpu_sriov_vf(adev) &&
  2077. !amdgpu_sriov_runtime(adev) &&
  2078. amdgpu_virt_mmio_blocked(adev) &&
  2079. !amdgpu_virt_wait_reset(adev)) {
  2080. dev_err(adev->dev, "VF exclusive mode timeout\n");
  2081. /* Don't send request since VF is inactive. */
  2082. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  2083. adev->virt.ops = NULL;
  2084. r = -EAGAIN;
  2085. goto failed;
  2086. }
  2087. dev_err(adev->dev, "amdgpu_init failed\n");
  2088. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  2089. amdgpu_fini(adev);
  2090. goto failed;
  2091. }
  2092. adev->accel_working = true;
  2093. amdgpu_vm_check_compute_bug(adev);
  2094. /* Initialize the buffer migration limit. */
  2095. if (amdgpu_moverate >= 0)
  2096. max_MBps = amdgpu_moverate;
  2097. else
  2098. max_MBps = 8; /* Allow 8 MB/s. */
  2099. /* Get a log2 for easy divisions. */
  2100. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  2101. r = amdgpu_ib_pool_init(adev);
  2102. if (r) {
  2103. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  2104. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  2105. goto failed;
  2106. }
  2107. r = amdgpu_ib_ring_tests(adev);
  2108. if (r)
  2109. DRM_ERROR("ib ring test failed (%d).\n", r);
  2110. if (amdgpu_sriov_vf(adev))
  2111. amdgpu_virt_init_data_exchange(adev);
  2112. amdgpu_fbdev_init(adev);
  2113. r = amdgpu_pm_sysfs_init(adev);
  2114. if (r)
  2115. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  2116. r = amdgpu_gem_debugfs_init(adev);
  2117. if (r)
  2118. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  2119. r = amdgpu_debugfs_regs_init(adev);
  2120. if (r)
  2121. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  2122. r = amdgpu_debugfs_test_ib_ring_init(adev);
  2123. if (r)
  2124. DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
  2125. r = amdgpu_debugfs_firmware_init(adev);
  2126. if (r)
  2127. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  2128. r = amdgpu_debugfs_vbios_dump_init(adev);
  2129. if (r)
  2130. DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
  2131. if ((amdgpu_testing & 1)) {
  2132. if (adev->accel_working)
  2133. amdgpu_test_moves(adev);
  2134. else
  2135. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  2136. }
  2137. if (amdgpu_benchmarking) {
  2138. if (adev->accel_working)
  2139. amdgpu_benchmark(adev, amdgpu_benchmarking);
  2140. else
  2141. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  2142. }
  2143. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  2144. * explicit gating rather than handling it automatically.
  2145. */
  2146. r = amdgpu_late_init(adev);
  2147. if (r) {
  2148. dev_err(adev->dev, "amdgpu_late_init failed\n");
  2149. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  2150. goto failed;
  2151. }
  2152. return 0;
  2153. failed:
  2154. amdgpu_vf_error_trans_all(adev);
  2155. if (runtime)
  2156. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2157. return r;
  2158. }
  2159. /**
  2160. * amdgpu_device_fini - tear down the driver
  2161. *
  2162. * @adev: amdgpu_device pointer
  2163. *
  2164. * Tear down the driver info (all asics).
  2165. * Called at driver shutdown.
  2166. */
  2167. void amdgpu_device_fini(struct amdgpu_device *adev)
  2168. {
  2169. int r;
  2170. DRM_INFO("amdgpu: finishing device.\n");
  2171. adev->shutdown = true;
  2172. if (adev->mode_info.mode_config_initialized)
  2173. drm_crtc_force_disable_all(adev->ddev);
  2174. /* evict vram memory */
  2175. amdgpu_bo_evict_vram(adev);
  2176. amdgpu_ib_pool_fini(adev);
  2177. amdgpu_fence_driver_fini(adev);
  2178. amdgpu_fbdev_fini(adev);
  2179. r = amdgpu_fini(adev);
  2180. if (adev->firmware.gpu_info_fw) {
  2181. release_firmware(adev->firmware.gpu_info_fw);
  2182. adev->firmware.gpu_info_fw = NULL;
  2183. }
  2184. adev->accel_working = false;
  2185. cancel_delayed_work_sync(&adev->late_init_work);
  2186. /* free i2c buses */
  2187. if (!amdgpu_device_has_dc_support(adev))
  2188. amdgpu_i2c_fini(adev);
  2189. amdgpu_atombios_fini(adev);
  2190. kfree(adev->bios);
  2191. adev->bios = NULL;
  2192. if (!pci_is_thunderbolt_attached(adev->pdev))
  2193. vga_switcheroo_unregister_client(adev->pdev);
  2194. if (adev->flags & AMD_IS_PX)
  2195. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2196. vga_client_register(adev->pdev, NULL, NULL, NULL);
  2197. if (adev->rio_mem)
  2198. pci_iounmap(adev->pdev, adev->rio_mem);
  2199. adev->rio_mem = NULL;
  2200. iounmap(adev->rmmio);
  2201. adev->rmmio = NULL;
  2202. amdgpu_doorbell_fini(adev);
  2203. amdgpu_pm_sysfs_fini(adev);
  2204. amdgpu_debugfs_regs_cleanup(adev);
  2205. }
  2206. /*
  2207. * Suspend & resume.
  2208. */
  2209. /**
  2210. * amdgpu_device_suspend - initiate device suspend
  2211. *
  2212. * @pdev: drm dev pointer
  2213. * @state: suspend state
  2214. *
  2215. * Puts the hw in the suspend state (all asics).
  2216. * Returns 0 for success or an error on failure.
  2217. * Called at driver suspend.
  2218. */
  2219. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  2220. {
  2221. struct amdgpu_device *adev;
  2222. struct drm_crtc *crtc;
  2223. struct drm_connector *connector;
  2224. int r;
  2225. if (dev == NULL || dev->dev_private == NULL) {
  2226. return -ENODEV;
  2227. }
  2228. adev = dev->dev_private;
  2229. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2230. return 0;
  2231. drm_kms_helper_poll_disable(dev);
  2232. if (!amdgpu_device_has_dc_support(adev)) {
  2233. /* turn off display hw */
  2234. drm_modeset_lock_all(dev);
  2235. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2236. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  2237. }
  2238. drm_modeset_unlock_all(dev);
  2239. }
  2240. amdgpu_amdkfd_suspend(adev);
  2241. /* unpin the front buffers and cursors */
  2242. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2243. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2244. struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
  2245. struct amdgpu_bo *robj;
  2246. if (amdgpu_crtc->cursor_bo) {
  2247. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2248. r = amdgpu_bo_reserve(aobj, true);
  2249. if (r == 0) {
  2250. amdgpu_bo_unpin(aobj);
  2251. amdgpu_bo_unreserve(aobj);
  2252. }
  2253. }
  2254. if (rfb == NULL || rfb->obj == NULL) {
  2255. continue;
  2256. }
  2257. robj = gem_to_amdgpu_bo(rfb->obj);
  2258. /* don't unpin kernel fb objects */
  2259. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2260. r = amdgpu_bo_reserve(robj, true);
  2261. if (r == 0) {
  2262. amdgpu_bo_unpin(robj);
  2263. amdgpu_bo_unreserve(robj);
  2264. }
  2265. }
  2266. }
  2267. /* evict vram memory */
  2268. amdgpu_bo_evict_vram(adev);
  2269. amdgpu_fence_driver_suspend(adev);
  2270. r = amdgpu_suspend(adev);
  2271. /* evict remaining vram memory
  2272. * This second call to evict vram is to evict the gart page table
  2273. * using the CPU.
  2274. */
  2275. amdgpu_bo_evict_vram(adev);
  2276. amdgpu_atombios_scratch_regs_save(adev);
  2277. pci_save_state(dev->pdev);
  2278. if (suspend) {
  2279. /* Shut down the device */
  2280. pci_disable_device(dev->pdev);
  2281. pci_set_power_state(dev->pdev, PCI_D3hot);
  2282. } else {
  2283. r = amdgpu_asic_reset(adev);
  2284. if (r)
  2285. DRM_ERROR("amdgpu asic reset failed\n");
  2286. }
  2287. if (fbcon) {
  2288. console_lock();
  2289. amdgpu_fbdev_set_suspend(adev, 1);
  2290. console_unlock();
  2291. }
  2292. return 0;
  2293. }
  2294. /**
  2295. * amdgpu_device_resume - initiate device resume
  2296. *
  2297. * @pdev: drm dev pointer
  2298. *
  2299. * Bring the hw back to operating state (all asics).
  2300. * Returns 0 for success or an error on failure.
  2301. * Called at driver resume.
  2302. */
  2303. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2304. {
  2305. struct drm_connector *connector;
  2306. struct amdgpu_device *adev = dev->dev_private;
  2307. struct drm_crtc *crtc;
  2308. int r = 0;
  2309. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2310. return 0;
  2311. if (fbcon)
  2312. console_lock();
  2313. if (resume) {
  2314. pci_set_power_state(dev->pdev, PCI_D0);
  2315. pci_restore_state(dev->pdev);
  2316. r = pci_enable_device(dev->pdev);
  2317. if (r)
  2318. goto unlock;
  2319. }
  2320. amdgpu_atombios_scratch_regs_restore(adev);
  2321. /* post card */
  2322. if (amdgpu_need_post(adev)) {
  2323. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2324. if (r)
  2325. DRM_ERROR("amdgpu asic init failed\n");
  2326. }
  2327. r = amdgpu_resume(adev);
  2328. if (r) {
  2329. DRM_ERROR("amdgpu_resume failed (%d).\n", r);
  2330. goto unlock;
  2331. }
  2332. amdgpu_fence_driver_resume(adev);
  2333. if (resume) {
  2334. r = amdgpu_ib_ring_tests(adev);
  2335. if (r)
  2336. DRM_ERROR("ib ring test failed (%d).\n", r);
  2337. }
  2338. r = amdgpu_late_init(adev);
  2339. if (r)
  2340. goto unlock;
  2341. /* pin cursors */
  2342. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2343. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2344. if (amdgpu_crtc->cursor_bo) {
  2345. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2346. r = amdgpu_bo_reserve(aobj, true);
  2347. if (r == 0) {
  2348. r = amdgpu_bo_pin(aobj,
  2349. AMDGPU_GEM_DOMAIN_VRAM,
  2350. &amdgpu_crtc->cursor_addr);
  2351. if (r != 0)
  2352. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2353. amdgpu_bo_unreserve(aobj);
  2354. }
  2355. }
  2356. }
  2357. r = amdgpu_amdkfd_resume(adev);
  2358. if (r)
  2359. return r;
  2360. /* blat the mode back in */
  2361. if (fbcon) {
  2362. if (!amdgpu_device_has_dc_support(adev)) {
  2363. /* pre DCE11 */
  2364. drm_helper_resume_force_mode(dev);
  2365. /* turn on display hw */
  2366. drm_modeset_lock_all(dev);
  2367. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2368. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2369. }
  2370. drm_modeset_unlock_all(dev);
  2371. } else {
  2372. /*
  2373. * There is no equivalent atomic helper to turn on
  2374. * display, so we defined our own function for this,
  2375. * once suspend resume is supported by the atomic
  2376. * framework this will be reworked
  2377. */
  2378. amdgpu_dm_display_resume(adev);
  2379. }
  2380. }
  2381. drm_kms_helper_poll_enable(dev);
  2382. /*
  2383. * Most of the connector probing functions try to acquire runtime pm
  2384. * refs to ensure that the GPU is powered on when connector polling is
  2385. * performed. Since we're calling this from a runtime PM callback,
  2386. * trying to acquire rpm refs will cause us to deadlock.
  2387. *
  2388. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2389. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2390. */
  2391. #ifdef CONFIG_PM
  2392. dev->dev->power.disable_depth++;
  2393. #endif
  2394. if (!amdgpu_device_has_dc_support(adev))
  2395. drm_helper_hpd_irq_event(dev);
  2396. else
  2397. drm_kms_helper_hotplug_event(dev);
  2398. #ifdef CONFIG_PM
  2399. dev->dev->power.disable_depth--;
  2400. #endif
  2401. if (fbcon)
  2402. amdgpu_fbdev_set_suspend(adev, 0);
  2403. unlock:
  2404. if (fbcon)
  2405. console_unlock();
  2406. return r;
  2407. }
  2408. static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
  2409. {
  2410. int i;
  2411. bool asic_hang = false;
  2412. if (amdgpu_sriov_vf(adev))
  2413. return true;
  2414. for (i = 0; i < adev->num_ip_blocks; i++) {
  2415. if (!adev->ip_blocks[i].status.valid)
  2416. continue;
  2417. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2418. adev->ip_blocks[i].status.hang =
  2419. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2420. if (adev->ip_blocks[i].status.hang) {
  2421. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2422. asic_hang = true;
  2423. }
  2424. }
  2425. return asic_hang;
  2426. }
  2427. static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
  2428. {
  2429. int i, r = 0;
  2430. for (i = 0; i < adev->num_ip_blocks; i++) {
  2431. if (!adev->ip_blocks[i].status.valid)
  2432. continue;
  2433. if (adev->ip_blocks[i].status.hang &&
  2434. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2435. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2436. if (r)
  2437. return r;
  2438. }
  2439. }
  2440. return 0;
  2441. }
  2442. static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
  2443. {
  2444. int i;
  2445. for (i = 0; i < adev->num_ip_blocks; i++) {
  2446. if (!adev->ip_blocks[i].status.valid)
  2447. continue;
  2448. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2449. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2450. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2451. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2452. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2453. if (adev->ip_blocks[i].status.hang) {
  2454. DRM_INFO("Some block need full reset!\n");
  2455. return true;
  2456. }
  2457. }
  2458. }
  2459. return false;
  2460. }
  2461. static int amdgpu_soft_reset(struct amdgpu_device *adev)
  2462. {
  2463. int i, r = 0;
  2464. for (i = 0; i < adev->num_ip_blocks; i++) {
  2465. if (!adev->ip_blocks[i].status.valid)
  2466. continue;
  2467. if (adev->ip_blocks[i].status.hang &&
  2468. adev->ip_blocks[i].version->funcs->soft_reset) {
  2469. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2470. if (r)
  2471. return r;
  2472. }
  2473. }
  2474. return 0;
  2475. }
  2476. static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
  2477. {
  2478. int i, r = 0;
  2479. for (i = 0; i < adev->num_ip_blocks; i++) {
  2480. if (!adev->ip_blocks[i].status.valid)
  2481. continue;
  2482. if (adev->ip_blocks[i].status.hang &&
  2483. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2484. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2485. if (r)
  2486. return r;
  2487. }
  2488. return 0;
  2489. }
  2490. bool amdgpu_need_backup(struct amdgpu_device *adev)
  2491. {
  2492. if (adev->flags & AMD_IS_APU)
  2493. return false;
  2494. return amdgpu_lockup_timeout > 0 ? true : false;
  2495. }
  2496. static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
  2497. struct amdgpu_ring *ring,
  2498. struct amdgpu_bo *bo,
  2499. struct dma_fence **fence)
  2500. {
  2501. uint32_t domain;
  2502. int r;
  2503. if (!bo->shadow)
  2504. return 0;
  2505. r = amdgpu_bo_reserve(bo, true);
  2506. if (r)
  2507. return r;
  2508. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2509. /* if bo has been evicted, then no need to recover */
  2510. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2511. r = amdgpu_bo_validate(bo->shadow);
  2512. if (r) {
  2513. DRM_ERROR("bo validate failed!\n");
  2514. goto err;
  2515. }
  2516. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2517. NULL, fence, true);
  2518. if (r) {
  2519. DRM_ERROR("recover page table failed!\n");
  2520. goto err;
  2521. }
  2522. }
  2523. err:
  2524. amdgpu_bo_unreserve(bo);
  2525. return r;
  2526. }
  2527. /*
  2528. * amdgpu_reset - reset ASIC/GPU for bare-metal or passthrough
  2529. *
  2530. * @adev: amdgpu device pointer
  2531. * @reset_flags: output param tells caller the reset result
  2532. *
  2533. * attempt to do soft-reset or full-reset and reinitialize Asic
  2534. * return 0 means successed otherwise failed
  2535. */
  2536. static int amdgpu_reset(struct amdgpu_device *adev, uint64_t* reset_flags)
  2537. {
  2538. bool need_full_reset, vram_lost = 0;
  2539. int r;
  2540. need_full_reset = amdgpu_need_full_reset(adev);
  2541. if (!need_full_reset) {
  2542. amdgpu_pre_soft_reset(adev);
  2543. r = amdgpu_soft_reset(adev);
  2544. amdgpu_post_soft_reset(adev);
  2545. if (r || amdgpu_check_soft_reset(adev)) {
  2546. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2547. need_full_reset = true;
  2548. }
  2549. }
  2550. if (need_full_reset) {
  2551. r = amdgpu_suspend(adev);
  2552. retry:
  2553. amdgpu_atombios_scratch_regs_save(adev);
  2554. r = amdgpu_asic_reset(adev);
  2555. amdgpu_atombios_scratch_regs_restore(adev);
  2556. /* post card */
  2557. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2558. if (!r) {
  2559. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2560. r = amdgpu_resume_phase1(adev);
  2561. if (r)
  2562. goto out;
  2563. vram_lost = amdgpu_check_vram_lost(adev);
  2564. if (vram_lost) {
  2565. DRM_ERROR("VRAM is lost!\n");
  2566. atomic_inc(&adev->vram_lost_counter);
  2567. }
  2568. r = amdgpu_gtt_mgr_recover(
  2569. &adev->mman.bdev.man[TTM_PL_TT]);
  2570. if (r)
  2571. goto out;
  2572. r = amdgpu_resume_phase2(adev);
  2573. if (r)
  2574. goto out;
  2575. if (vram_lost)
  2576. amdgpu_fill_reset_magic(adev);
  2577. }
  2578. }
  2579. out:
  2580. if (!r) {
  2581. amdgpu_irq_gpu_reset_resume_helper(adev);
  2582. r = amdgpu_ib_ring_tests(adev);
  2583. if (r) {
  2584. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2585. r = amdgpu_suspend(adev);
  2586. need_full_reset = true;
  2587. goto retry;
  2588. }
  2589. }
  2590. if (reset_flags) {
  2591. if (vram_lost)
  2592. (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
  2593. if (need_full_reset)
  2594. (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
  2595. }
  2596. return r;
  2597. }
  2598. /*
  2599. * amdgpu_reset_sriov - reset ASIC for SR-IOV vf
  2600. *
  2601. * @adev: amdgpu device pointer
  2602. * @reset_flags: output param tells caller the reset result
  2603. *
  2604. * do VF FLR and reinitialize Asic
  2605. * return 0 means successed otherwise failed
  2606. */
  2607. static int amdgpu_reset_sriov(struct amdgpu_device *adev, uint64_t *reset_flags, bool from_hypervisor)
  2608. {
  2609. int r;
  2610. if (from_hypervisor)
  2611. r = amdgpu_virt_request_full_gpu(adev, true);
  2612. else
  2613. r = amdgpu_virt_reset_gpu(adev);
  2614. if (r)
  2615. return r;
  2616. /* Resume IP prior to SMC */
  2617. r = amdgpu_sriov_reinit_early(adev);
  2618. if (r)
  2619. goto error;
  2620. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2621. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2622. /* now we are okay to resume SMC/CP/SDMA */
  2623. r = amdgpu_sriov_reinit_late(adev);
  2624. if (r)
  2625. goto error;
  2626. amdgpu_irq_gpu_reset_resume_helper(adev);
  2627. r = amdgpu_ib_ring_tests(adev);
  2628. if (r)
  2629. dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
  2630. error:
  2631. /* release full control of GPU after ib test */
  2632. amdgpu_virt_release_full_gpu(adev, true);
  2633. if (reset_flags) {
  2634. if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2635. (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
  2636. atomic_inc(&adev->vram_lost_counter);
  2637. }
  2638. /* VF FLR or hotlink reset is always full-reset */
  2639. (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
  2640. }
  2641. return r;
  2642. }
  2643. /**
  2644. * amdgpu_gpu_recover - reset the asic and recover scheduler
  2645. *
  2646. * @adev: amdgpu device pointer
  2647. * @job: which job trigger hang
  2648. *
  2649. * Attempt to reset the GPU if it has hung (all asics).
  2650. * Returns 0 for success or an error on failure.
  2651. */
  2652. int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job)
  2653. {
  2654. struct drm_atomic_state *state = NULL;
  2655. uint64_t reset_flags = 0;
  2656. int i, r, resched;
  2657. if (!amdgpu_check_soft_reset(adev)) {
  2658. DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
  2659. return 0;
  2660. }
  2661. dev_info(adev->dev, "GPU reset begin!\n");
  2662. mutex_lock(&adev->lock_reset);
  2663. atomic_inc(&adev->gpu_reset_counter);
  2664. adev->in_gpu_reset = 1;
  2665. /* block TTM */
  2666. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2667. /* store modesetting */
  2668. if (amdgpu_device_has_dc_support(adev))
  2669. state = drm_atomic_helper_suspend(adev->ddev);
  2670. /* block scheduler */
  2671. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2672. struct amdgpu_ring *ring = adev->rings[i];
  2673. if (!ring || !ring->sched.thread)
  2674. continue;
  2675. /* only focus on the ring hit timeout if &job not NULL */
  2676. if (job && job->ring->idx != i)
  2677. continue;
  2678. kthread_park(ring->sched.thread);
  2679. drm_sched_hw_job_reset(&ring->sched, &job->base);
  2680. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2681. amdgpu_fence_driver_force_completion(ring);
  2682. }
  2683. if (amdgpu_sriov_vf(adev))
  2684. r = amdgpu_reset_sriov(adev, &reset_flags, job ? false : true);
  2685. else
  2686. r = amdgpu_reset(adev, &reset_flags);
  2687. if (!r) {
  2688. if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
  2689. (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
  2690. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2691. struct amdgpu_bo *bo, *tmp;
  2692. struct dma_fence *fence = NULL, *next = NULL;
  2693. DRM_INFO("recover vram bo from shadow\n");
  2694. mutex_lock(&adev->shadow_list_lock);
  2695. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2696. next = NULL;
  2697. amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
  2698. if (fence) {
  2699. r = dma_fence_wait(fence, false);
  2700. if (r) {
  2701. WARN(r, "recovery from shadow isn't completed\n");
  2702. break;
  2703. }
  2704. }
  2705. dma_fence_put(fence);
  2706. fence = next;
  2707. }
  2708. mutex_unlock(&adev->shadow_list_lock);
  2709. if (fence) {
  2710. r = dma_fence_wait(fence, false);
  2711. if (r)
  2712. WARN(r, "recovery from shadow isn't completed\n");
  2713. }
  2714. dma_fence_put(fence);
  2715. }
  2716. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2717. struct amdgpu_ring *ring = adev->rings[i];
  2718. if (!ring || !ring->sched.thread)
  2719. continue;
  2720. /* only focus on the ring hit timeout if &job not NULL */
  2721. if (job && job->ring->idx != i)
  2722. continue;
  2723. drm_sched_job_recovery(&ring->sched);
  2724. kthread_unpark(ring->sched.thread);
  2725. }
  2726. } else {
  2727. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2728. struct amdgpu_ring *ring = adev->rings[i];
  2729. if (!ring || !ring->sched.thread)
  2730. continue;
  2731. /* only focus on the ring hit timeout if &job not NULL */
  2732. if (job && job->ring->idx != i)
  2733. continue;
  2734. kthread_unpark(adev->rings[i]->sched.thread);
  2735. }
  2736. }
  2737. if (amdgpu_device_has_dc_support(adev)) {
  2738. if (drm_atomic_helper_resume(adev->ddev, state))
  2739. dev_info(adev->dev, "drm resume failed:%d\n", r);
  2740. amdgpu_dm_display_resume(adev);
  2741. } else {
  2742. drm_helper_resume_force_mode(adev->ddev);
  2743. }
  2744. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2745. if (r) {
  2746. /* bad news, how to tell it to userspace ? */
  2747. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2748. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2749. } else {
  2750. dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
  2751. }
  2752. amdgpu_vf_error_trans_all(adev);
  2753. adev->in_gpu_reset = 0;
  2754. mutex_unlock(&adev->lock_reset);
  2755. return r;
  2756. }
  2757. void amdgpu_get_pcie_info(struct amdgpu_device *adev)
  2758. {
  2759. u32 mask;
  2760. int ret;
  2761. if (amdgpu_pcie_gen_cap)
  2762. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2763. if (amdgpu_pcie_lane_cap)
  2764. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2765. /* covers APUs as well */
  2766. if (pci_is_root_bus(adev->pdev->bus)) {
  2767. if (adev->pm.pcie_gen_mask == 0)
  2768. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2769. if (adev->pm.pcie_mlw_mask == 0)
  2770. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2771. return;
  2772. }
  2773. if (adev->pm.pcie_gen_mask == 0) {
  2774. ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
  2775. if (!ret) {
  2776. adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2777. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2778. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2779. if (mask & DRM_PCIE_SPEED_25)
  2780. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2781. if (mask & DRM_PCIE_SPEED_50)
  2782. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
  2783. if (mask & DRM_PCIE_SPEED_80)
  2784. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
  2785. } else {
  2786. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2787. }
  2788. }
  2789. if (adev->pm.pcie_mlw_mask == 0) {
  2790. ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
  2791. if (!ret) {
  2792. switch (mask) {
  2793. case 32:
  2794. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2795. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2796. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2797. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2798. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2799. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2800. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2801. break;
  2802. case 16:
  2803. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2804. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2805. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2806. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2807. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2808. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2809. break;
  2810. case 12:
  2811. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2812. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2813. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2814. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2815. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2816. break;
  2817. case 8:
  2818. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2819. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2820. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2821. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2822. break;
  2823. case 4:
  2824. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2825. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2826. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2827. break;
  2828. case 2:
  2829. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2830. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2831. break;
  2832. case 1:
  2833. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  2834. break;
  2835. default:
  2836. break;
  2837. }
  2838. } else {
  2839. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2840. }
  2841. }
  2842. }
  2843. /*
  2844. * Debugfs
  2845. */
  2846. int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
  2847. const struct drm_info_list *files,
  2848. unsigned nfiles)
  2849. {
  2850. unsigned i;
  2851. for (i = 0; i < adev->debugfs_count; i++) {
  2852. if (adev->debugfs[i].files == files) {
  2853. /* Already registered */
  2854. return 0;
  2855. }
  2856. }
  2857. i = adev->debugfs_count + 1;
  2858. if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
  2859. DRM_ERROR("Reached maximum number of debugfs components.\n");
  2860. DRM_ERROR("Report so we increase "
  2861. "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
  2862. return -EINVAL;
  2863. }
  2864. adev->debugfs[adev->debugfs_count].files = files;
  2865. adev->debugfs[adev->debugfs_count].num_files = nfiles;
  2866. adev->debugfs_count = i;
  2867. #if defined(CONFIG_DEBUG_FS)
  2868. drm_debugfs_create_files(files, nfiles,
  2869. adev->ddev->primary->debugfs_root,
  2870. adev->ddev->primary);
  2871. #endif
  2872. return 0;
  2873. }
  2874. #if defined(CONFIG_DEBUG_FS)
  2875. static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
  2876. size_t size, loff_t *pos)
  2877. {
  2878. struct amdgpu_device *adev = file_inode(f)->i_private;
  2879. ssize_t result = 0;
  2880. int r;
  2881. bool pm_pg_lock, use_bank;
  2882. unsigned instance_bank, sh_bank, se_bank;
  2883. if (size & 0x3 || *pos & 0x3)
  2884. return -EINVAL;
  2885. /* are we reading registers for which a PG lock is necessary? */
  2886. pm_pg_lock = (*pos >> 23) & 1;
  2887. if (*pos & (1ULL << 62)) {
  2888. se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
  2889. sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
  2890. instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
  2891. if (se_bank == 0x3FF)
  2892. se_bank = 0xFFFFFFFF;
  2893. if (sh_bank == 0x3FF)
  2894. sh_bank = 0xFFFFFFFF;
  2895. if (instance_bank == 0x3FF)
  2896. instance_bank = 0xFFFFFFFF;
  2897. use_bank = 1;
  2898. } else {
  2899. use_bank = 0;
  2900. }
  2901. *pos &= (1UL << 22) - 1;
  2902. if (use_bank) {
  2903. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2904. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2905. return -EINVAL;
  2906. mutex_lock(&adev->grbm_idx_mutex);
  2907. amdgpu_gfx_select_se_sh(adev, se_bank,
  2908. sh_bank, instance_bank);
  2909. }
  2910. if (pm_pg_lock)
  2911. mutex_lock(&adev->pm.mutex);
  2912. while (size) {
  2913. uint32_t value;
  2914. if (*pos > adev->rmmio_size)
  2915. goto end;
  2916. value = RREG32(*pos >> 2);
  2917. r = put_user(value, (uint32_t *)buf);
  2918. if (r) {
  2919. result = r;
  2920. goto end;
  2921. }
  2922. result += 4;
  2923. buf += 4;
  2924. *pos += 4;
  2925. size -= 4;
  2926. }
  2927. end:
  2928. if (use_bank) {
  2929. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2930. mutex_unlock(&adev->grbm_idx_mutex);
  2931. }
  2932. if (pm_pg_lock)
  2933. mutex_unlock(&adev->pm.mutex);
  2934. return result;
  2935. }
  2936. static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
  2937. size_t size, loff_t *pos)
  2938. {
  2939. struct amdgpu_device *adev = file_inode(f)->i_private;
  2940. ssize_t result = 0;
  2941. int r;
  2942. bool pm_pg_lock, use_bank;
  2943. unsigned instance_bank, sh_bank, se_bank;
  2944. if (size & 0x3 || *pos & 0x3)
  2945. return -EINVAL;
  2946. /* are we reading registers for which a PG lock is necessary? */
  2947. pm_pg_lock = (*pos >> 23) & 1;
  2948. if (*pos & (1ULL << 62)) {
  2949. se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
  2950. sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
  2951. instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
  2952. if (se_bank == 0x3FF)
  2953. se_bank = 0xFFFFFFFF;
  2954. if (sh_bank == 0x3FF)
  2955. sh_bank = 0xFFFFFFFF;
  2956. if (instance_bank == 0x3FF)
  2957. instance_bank = 0xFFFFFFFF;
  2958. use_bank = 1;
  2959. } else {
  2960. use_bank = 0;
  2961. }
  2962. *pos &= (1UL << 22) - 1;
  2963. if (use_bank) {
  2964. if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
  2965. (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
  2966. return -EINVAL;
  2967. mutex_lock(&adev->grbm_idx_mutex);
  2968. amdgpu_gfx_select_se_sh(adev, se_bank,
  2969. sh_bank, instance_bank);
  2970. }
  2971. if (pm_pg_lock)
  2972. mutex_lock(&adev->pm.mutex);
  2973. while (size) {
  2974. uint32_t value;
  2975. if (*pos > adev->rmmio_size)
  2976. return result;
  2977. r = get_user(value, (uint32_t *)buf);
  2978. if (r)
  2979. return r;
  2980. WREG32(*pos >> 2, value);
  2981. result += 4;
  2982. buf += 4;
  2983. *pos += 4;
  2984. size -= 4;
  2985. }
  2986. if (use_bank) {
  2987. amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
  2988. mutex_unlock(&adev->grbm_idx_mutex);
  2989. }
  2990. if (pm_pg_lock)
  2991. mutex_unlock(&adev->pm.mutex);
  2992. return result;
  2993. }
  2994. static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
  2995. size_t size, loff_t *pos)
  2996. {
  2997. struct amdgpu_device *adev = file_inode(f)->i_private;
  2998. ssize_t result = 0;
  2999. int r;
  3000. if (size & 0x3 || *pos & 0x3)
  3001. return -EINVAL;
  3002. while (size) {
  3003. uint32_t value;
  3004. value = RREG32_PCIE(*pos >> 2);
  3005. r = put_user(value, (uint32_t *)buf);
  3006. if (r)
  3007. return r;
  3008. result += 4;
  3009. buf += 4;
  3010. *pos += 4;
  3011. size -= 4;
  3012. }
  3013. return result;
  3014. }
  3015. static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
  3016. size_t size, loff_t *pos)
  3017. {
  3018. struct amdgpu_device *adev = file_inode(f)->i_private;
  3019. ssize_t result = 0;
  3020. int r;
  3021. if (size & 0x3 || *pos & 0x3)
  3022. return -EINVAL;
  3023. while (size) {
  3024. uint32_t value;
  3025. r = get_user(value, (uint32_t *)buf);
  3026. if (r)
  3027. return r;
  3028. WREG32_PCIE(*pos >> 2, value);
  3029. result += 4;
  3030. buf += 4;
  3031. *pos += 4;
  3032. size -= 4;
  3033. }
  3034. return result;
  3035. }
  3036. static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
  3037. size_t size, loff_t *pos)
  3038. {
  3039. struct amdgpu_device *adev = file_inode(f)->i_private;
  3040. ssize_t result = 0;
  3041. int r;
  3042. if (size & 0x3 || *pos & 0x3)
  3043. return -EINVAL;
  3044. while (size) {
  3045. uint32_t value;
  3046. value = RREG32_DIDT(*pos >> 2);
  3047. r = put_user(value, (uint32_t *)buf);
  3048. if (r)
  3049. return r;
  3050. result += 4;
  3051. buf += 4;
  3052. *pos += 4;
  3053. size -= 4;
  3054. }
  3055. return result;
  3056. }
  3057. static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
  3058. size_t size, loff_t *pos)
  3059. {
  3060. struct amdgpu_device *adev = file_inode(f)->i_private;
  3061. ssize_t result = 0;
  3062. int r;
  3063. if (size & 0x3 || *pos & 0x3)
  3064. return -EINVAL;
  3065. while (size) {
  3066. uint32_t value;
  3067. r = get_user(value, (uint32_t *)buf);
  3068. if (r)
  3069. return r;
  3070. WREG32_DIDT(*pos >> 2, value);
  3071. result += 4;
  3072. buf += 4;
  3073. *pos += 4;
  3074. size -= 4;
  3075. }
  3076. return result;
  3077. }
  3078. static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
  3079. size_t size, loff_t *pos)
  3080. {
  3081. struct amdgpu_device *adev = file_inode(f)->i_private;
  3082. ssize_t result = 0;
  3083. int r;
  3084. if (size & 0x3 || *pos & 0x3)
  3085. return -EINVAL;
  3086. while (size) {
  3087. uint32_t value;
  3088. value = RREG32_SMC(*pos);
  3089. r = put_user(value, (uint32_t *)buf);
  3090. if (r)
  3091. return r;
  3092. result += 4;
  3093. buf += 4;
  3094. *pos += 4;
  3095. size -= 4;
  3096. }
  3097. return result;
  3098. }
  3099. static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
  3100. size_t size, loff_t *pos)
  3101. {
  3102. struct amdgpu_device *adev = file_inode(f)->i_private;
  3103. ssize_t result = 0;
  3104. int r;
  3105. if (size & 0x3 || *pos & 0x3)
  3106. return -EINVAL;
  3107. while (size) {
  3108. uint32_t value;
  3109. r = get_user(value, (uint32_t *)buf);
  3110. if (r)
  3111. return r;
  3112. WREG32_SMC(*pos, value);
  3113. result += 4;
  3114. buf += 4;
  3115. *pos += 4;
  3116. size -= 4;
  3117. }
  3118. return result;
  3119. }
  3120. static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
  3121. size_t size, loff_t *pos)
  3122. {
  3123. struct amdgpu_device *adev = file_inode(f)->i_private;
  3124. ssize_t result = 0;
  3125. int r;
  3126. uint32_t *config, no_regs = 0;
  3127. if (size & 0x3 || *pos & 0x3)
  3128. return -EINVAL;
  3129. config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
  3130. if (!config)
  3131. return -ENOMEM;
  3132. /* version, increment each time something is added */
  3133. config[no_regs++] = 3;
  3134. config[no_regs++] = adev->gfx.config.max_shader_engines;
  3135. config[no_regs++] = adev->gfx.config.max_tile_pipes;
  3136. config[no_regs++] = adev->gfx.config.max_cu_per_sh;
  3137. config[no_regs++] = adev->gfx.config.max_sh_per_se;
  3138. config[no_regs++] = adev->gfx.config.max_backends_per_se;
  3139. config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
  3140. config[no_regs++] = adev->gfx.config.max_gprs;
  3141. config[no_regs++] = adev->gfx.config.max_gs_threads;
  3142. config[no_regs++] = adev->gfx.config.max_hw_contexts;
  3143. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
  3144. config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
  3145. config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
  3146. config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
  3147. config[no_regs++] = adev->gfx.config.num_tile_pipes;
  3148. config[no_regs++] = adev->gfx.config.backend_enable_mask;
  3149. config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
  3150. config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
  3151. config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
  3152. config[no_regs++] = adev->gfx.config.num_gpus;
  3153. config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
  3154. config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
  3155. config[no_regs++] = adev->gfx.config.gb_addr_config;
  3156. config[no_regs++] = adev->gfx.config.num_rbs;
  3157. /* rev==1 */
  3158. config[no_regs++] = adev->rev_id;
  3159. config[no_regs++] = adev->pg_flags;
  3160. config[no_regs++] = adev->cg_flags;
  3161. /* rev==2 */
  3162. config[no_regs++] = adev->family;
  3163. config[no_regs++] = adev->external_rev_id;
  3164. /* rev==3 */
  3165. config[no_regs++] = adev->pdev->device;
  3166. config[no_regs++] = adev->pdev->revision;
  3167. config[no_regs++] = adev->pdev->subsystem_device;
  3168. config[no_regs++] = adev->pdev->subsystem_vendor;
  3169. while (size && (*pos < no_regs * 4)) {
  3170. uint32_t value;
  3171. value = config[*pos >> 2];
  3172. r = put_user(value, (uint32_t *)buf);
  3173. if (r) {
  3174. kfree(config);
  3175. return r;
  3176. }
  3177. result += 4;
  3178. buf += 4;
  3179. *pos += 4;
  3180. size -= 4;
  3181. }
  3182. kfree(config);
  3183. return result;
  3184. }
  3185. static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
  3186. size_t size, loff_t *pos)
  3187. {
  3188. struct amdgpu_device *adev = file_inode(f)->i_private;
  3189. int idx, x, outsize, r, valuesize;
  3190. uint32_t values[16];
  3191. if (size & 3 || *pos & 0x3)
  3192. return -EINVAL;
  3193. if (amdgpu_dpm == 0)
  3194. return -EINVAL;
  3195. /* convert offset to sensor number */
  3196. idx = *pos >> 2;
  3197. valuesize = sizeof(values);
  3198. if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
  3199. r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
  3200. else
  3201. return -EINVAL;
  3202. if (size > valuesize)
  3203. return -EINVAL;
  3204. outsize = 0;
  3205. x = 0;
  3206. if (!r) {
  3207. while (size) {
  3208. r = put_user(values[x++], (int32_t *)buf);
  3209. buf += 4;
  3210. size -= 4;
  3211. outsize += 4;
  3212. }
  3213. }
  3214. return !r ? outsize : r;
  3215. }
  3216. static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
  3217. size_t size, loff_t *pos)
  3218. {
  3219. struct amdgpu_device *adev = f->f_inode->i_private;
  3220. int r, x;
  3221. ssize_t result=0;
  3222. uint32_t offset, se, sh, cu, wave, simd, data[32];
  3223. if (size & 3 || *pos & 3)
  3224. return -EINVAL;
  3225. /* decode offset */
  3226. offset = (*pos & GENMASK_ULL(6, 0));
  3227. se = (*pos & GENMASK_ULL(14, 7)) >> 7;
  3228. sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
  3229. cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
  3230. wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
  3231. simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
  3232. /* switch to the specific se/sh/cu */
  3233. mutex_lock(&adev->grbm_idx_mutex);
  3234. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  3235. x = 0;
  3236. if (adev->gfx.funcs->read_wave_data)
  3237. adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
  3238. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  3239. mutex_unlock(&adev->grbm_idx_mutex);
  3240. if (!x)
  3241. return -EINVAL;
  3242. while (size && (offset < x * 4)) {
  3243. uint32_t value;
  3244. value = data[offset >> 2];
  3245. r = put_user(value, (uint32_t *)buf);
  3246. if (r)
  3247. return r;
  3248. result += 4;
  3249. buf += 4;
  3250. offset += 4;
  3251. size -= 4;
  3252. }
  3253. return result;
  3254. }
  3255. static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
  3256. size_t size, loff_t *pos)
  3257. {
  3258. struct amdgpu_device *adev = f->f_inode->i_private;
  3259. int r;
  3260. ssize_t result = 0;
  3261. uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
  3262. if (size & 3 || *pos & 3)
  3263. return -EINVAL;
  3264. /* decode offset */
  3265. offset = *pos & GENMASK_ULL(11, 0);
  3266. se = (*pos & GENMASK_ULL(19, 12)) >> 12;
  3267. sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
  3268. cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
  3269. wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
  3270. simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
  3271. thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
  3272. bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
  3273. data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
  3274. if (!data)
  3275. return -ENOMEM;
  3276. /* switch to the specific se/sh/cu */
  3277. mutex_lock(&adev->grbm_idx_mutex);
  3278. amdgpu_gfx_select_se_sh(adev, se, sh, cu);
  3279. if (bank == 0) {
  3280. if (adev->gfx.funcs->read_wave_vgprs)
  3281. adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
  3282. } else {
  3283. if (adev->gfx.funcs->read_wave_sgprs)
  3284. adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
  3285. }
  3286. amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  3287. mutex_unlock(&adev->grbm_idx_mutex);
  3288. while (size) {
  3289. uint32_t value;
  3290. value = data[offset++];
  3291. r = put_user(value, (uint32_t *)buf);
  3292. if (r) {
  3293. result = r;
  3294. goto err;
  3295. }
  3296. result += 4;
  3297. buf += 4;
  3298. size -= 4;
  3299. }
  3300. err:
  3301. kfree(data);
  3302. return result;
  3303. }
  3304. static const struct file_operations amdgpu_debugfs_regs_fops = {
  3305. .owner = THIS_MODULE,
  3306. .read = amdgpu_debugfs_regs_read,
  3307. .write = amdgpu_debugfs_regs_write,
  3308. .llseek = default_llseek
  3309. };
  3310. static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
  3311. .owner = THIS_MODULE,
  3312. .read = amdgpu_debugfs_regs_didt_read,
  3313. .write = amdgpu_debugfs_regs_didt_write,
  3314. .llseek = default_llseek
  3315. };
  3316. static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
  3317. .owner = THIS_MODULE,
  3318. .read = amdgpu_debugfs_regs_pcie_read,
  3319. .write = amdgpu_debugfs_regs_pcie_write,
  3320. .llseek = default_llseek
  3321. };
  3322. static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
  3323. .owner = THIS_MODULE,
  3324. .read = amdgpu_debugfs_regs_smc_read,
  3325. .write = amdgpu_debugfs_regs_smc_write,
  3326. .llseek = default_llseek
  3327. };
  3328. static const struct file_operations amdgpu_debugfs_gca_config_fops = {
  3329. .owner = THIS_MODULE,
  3330. .read = amdgpu_debugfs_gca_config_read,
  3331. .llseek = default_llseek
  3332. };
  3333. static const struct file_operations amdgpu_debugfs_sensors_fops = {
  3334. .owner = THIS_MODULE,
  3335. .read = amdgpu_debugfs_sensor_read,
  3336. .llseek = default_llseek
  3337. };
  3338. static const struct file_operations amdgpu_debugfs_wave_fops = {
  3339. .owner = THIS_MODULE,
  3340. .read = amdgpu_debugfs_wave_read,
  3341. .llseek = default_llseek
  3342. };
  3343. static const struct file_operations amdgpu_debugfs_gpr_fops = {
  3344. .owner = THIS_MODULE,
  3345. .read = amdgpu_debugfs_gpr_read,
  3346. .llseek = default_llseek
  3347. };
  3348. static const struct file_operations *debugfs_regs[] = {
  3349. &amdgpu_debugfs_regs_fops,
  3350. &amdgpu_debugfs_regs_didt_fops,
  3351. &amdgpu_debugfs_regs_pcie_fops,
  3352. &amdgpu_debugfs_regs_smc_fops,
  3353. &amdgpu_debugfs_gca_config_fops,
  3354. &amdgpu_debugfs_sensors_fops,
  3355. &amdgpu_debugfs_wave_fops,
  3356. &amdgpu_debugfs_gpr_fops,
  3357. };
  3358. static const char *debugfs_regs_names[] = {
  3359. "amdgpu_regs",
  3360. "amdgpu_regs_didt",
  3361. "amdgpu_regs_pcie",
  3362. "amdgpu_regs_smc",
  3363. "amdgpu_gca_config",
  3364. "amdgpu_sensors",
  3365. "amdgpu_wave",
  3366. "amdgpu_gpr",
  3367. };
  3368. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3369. {
  3370. struct drm_minor *minor = adev->ddev->primary;
  3371. struct dentry *ent, *root = minor->debugfs_root;
  3372. unsigned i, j;
  3373. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3374. ent = debugfs_create_file(debugfs_regs_names[i],
  3375. S_IFREG | S_IRUGO, root,
  3376. adev, debugfs_regs[i]);
  3377. if (IS_ERR(ent)) {
  3378. for (j = 0; j < i; j++) {
  3379. debugfs_remove(adev->debugfs_regs[i]);
  3380. adev->debugfs_regs[i] = NULL;
  3381. }
  3382. return PTR_ERR(ent);
  3383. }
  3384. if (!i)
  3385. i_size_write(ent->d_inode, adev->rmmio_size);
  3386. adev->debugfs_regs[i] = ent;
  3387. }
  3388. return 0;
  3389. }
  3390. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
  3391. {
  3392. unsigned i;
  3393. for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
  3394. if (adev->debugfs_regs[i]) {
  3395. debugfs_remove(adev->debugfs_regs[i]);
  3396. adev->debugfs_regs[i] = NULL;
  3397. }
  3398. }
  3399. }
  3400. static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
  3401. {
  3402. struct drm_info_node *node = (struct drm_info_node *) m->private;
  3403. struct drm_device *dev = node->minor->dev;
  3404. struct amdgpu_device *adev = dev->dev_private;
  3405. int r = 0, i;
  3406. /* hold on the scheduler */
  3407. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  3408. struct amdgpu_ring *ring = adev->rings[i];
  3409. if (!ring || !ring->sched.thread)
  3410. continue;
  3411. kthread_park(ring->sched.thread);
  3412. }
  3413. seq_printf(m, "run ib test:\n");
  3414. r = amdgpu_ib_ring_tests(adev);
  3415. if (r)
  3416. seq_printf(m, "ib ring tests failed (%d).\n", r);
  3417. else
  3418. seq_printf(m, "ib ring tests passed.\n");
  3419. /* go on the scheduler */
  3420. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  3421. struct amdgpu_ring *ring = adev->rings[i];
  3422. if (!ring || !ring->sched.thread)
  3423. continue;
  3424. kthread_unpark(ring->sched.thread);
  3425. }
  3426. return 0;
  3427. }
  3428. static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
  3429. {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
  3430. };
  3431. static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
  3432. {
  3433. return amdgpu_debugfs_add_files(adev,
  3434. amdgpu_debugfs_test_ib_ring_list, 1);
  3435. }
  3436. int amdgpu_debugfs_init(struct drm_minor *minor)
  3437. {
  3438. return 0;
  3439. }
  3440. static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
  3441. {
  3442. struct drm_info_node *node = (struct drm_info_node *) m->private;
  3443. struct drm_device *dev = node->minor->dev;
  3444. struct amdgpu_device *adev = dev->dev_private;
  3445. seq_write(m, adev->bios, adev->bios_size);
  3446. return 0;
  3447. }
  3448. static const struct drm_info_list amdgpu_vbios_dump_list[] = {
  3449. {"amdgpu_vbios",
  3450. amdgpu_debugfs_get_vbios_dump,
  3451. 0, NULL},
  3452. };
  3453. static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
  3454. {
  3455. return amdgpu_debugfs_add_files(adev,
  3456. amdgpu_vbios_dump_list, 1);
  3457. }
  3458. #else
  3459. static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
  3460. {
  3461. return 0;
  3462. }
  3463. static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
  3464. {
  3465. return 0;
  3466. }
  3467. static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
  3468. {
  3469. return 0;
  3470. }
  3471. static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
  3472. #endif