amdgpu_device.c 92 KB

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