amdgpu_device.c 101 KB

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