amdgpu_device.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435
  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. #define AMDGPU_RESUME_MS 2000
  65. static const char *amdgpu_asic_name[] = {
  66. "TAHITI",
  67. "PITCAIRN",
  68. "VERDE",
  69. "OLAND",
  70. "HAINAN",
  71. "BONAIRE",
  72. "KAVERI",
  73. "KABINI",
  74. "HAWAII",
  75. "MULLINS",
  76. "TOPAZ",
  77. "TONGA",
  78. "FIJI",
  79. "CARRIZO",
  80. "STONEY",
  81. "POLARIS10",
  82. "POLARIS11",
  83. "POLARIS12",
  84. "VEGAM",
  85. "VEGA10",
  86. "VEGA12",
  87. "VEGA20",
  88. "RAVEN",
  89. "PICASSO",
  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. chip_name = "raven";
  1196. break;
  1197. case CHIP_PICASSO:
  1198. chip_name = "picasso";
  1199. break;
  1200. }
  1201. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
  1202. err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
  1203. if (err) {
  1204. dev_err(adev->dev,
  1205. "Failed to load gpu_info firmware \"%s\"\n",
  1206. fw_name);
  1207. goto out;
  1208. }
  1209. err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
  1210. if (err) {
  1211. dev_err(adev->dev,
  1212. "Failed to validate gpu_info firmware \"%s\"\n",
  1213. fw_name);
  1214. goto out;
  1215. }
  1216. hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
  1217. amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
  1218. switch (hdr->version_major) {
  1219. case 1:
  1220. {
  1221. const struct gpu_info_firmware_v1_0 *gpu_info_fw =
  1222. (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
  1223. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  1224. adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
  1225. adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
  1226. adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
  1227. adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
  1228. adev->gfx.config.max_texture_channel_caches =
  1229. le32_to_cpu(gpu_info_fw->gc_num_tccs);
  1230. adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
  1231. adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
  1232. adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
  1233. adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
  1234. adev->gfx.config.double_offchip_lds_buf =
  1235. le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
  1236. adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
  1237. adev->gfx.cu_info.max_waves_per_simd =
  1238. le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
  1239. adev->gfx.cu_info.max_scratch_slots_per_cu =
  1240. le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
  1241. adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
  1242. break;
  1243. }
  1244. default:
  1245. dev_err(adev->dev,
  1246. "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
  1247. err = -EINVAL;
  1248. goto out;
  1249. }
  1250. out:
  1251. return err;
  1252. }
  1253. /**
  1254. * amdgpu_device_ip_early_init - run early init for hardware IPs
  1255. *
  1256. * @adev: amdgpu_device pointer
  1257. *
  1258. * Early initialization pass for hardware IPs. The hardware IPs that make
  1259. * up each asic are discovered each IP's early_init callback is run. This
  1260. * is the first stage in initializing the asic.
  1261. * Returns 0 on success, negative error code on failure.
  1262. */
  1263. static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
  1264. {
  1265. int i, r;
  1266. amdgpu_device_enable_virtual_display(adev);
  1267. switch (adev->asic_type) {
  1268. case CHIP_TOPAZ:
  1269. case CHIP_TONGA:
  1270. case CHIP_FIJI:
  1271. case CHIP_POLARIS10:
  1272. case CHIP_POLARIS11:
  1273. case CHIP_POLARIS12:
  1274. case CHIP_VEGAM:
  1275. case CHIP_CARRIZO:
  1276. case CHIP_STONEY:
  1277. if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
  1278. adev->family = AMDGPU_FAMILY_CZ;
  1279. else
  1280. adev->family = AMDGPU_FAMILY_VI;
  1281. r = vi_set_ip_blocks(adev);
  1282. if (r)
  1283. return r;
  1284. break;
  1285. #ifdef CONFIG_DRM_AMDGPU_SI
  1286. case CHIP_VERDE:
  1287. case CHIP_TAHITI:
  1288. case CHIP_PITCAIRN:
  1289. case CHIP_OLAND:
  1290. case CHIP_HAINAN:
  1291. adev->family = AMDGPU_FAMILY_SI;
  1292. r = si_set_ip_blocks(adev);
  1293. if (r)
  1294. return r;
  1295. break;
  1296. #endif
  1297. #ifdef CONFIG_DRM_AMDGPU_CIK
  1298. case CHIP_BONAIRE:
  1299. case CHIP_HAWAII:
  1300. case CHIP_KAVERI:
  1301. case CHIP_KABINI:
  1302. case CHIP_MULLINS:
  1303. if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
  1304. adev->family = AMDGPU_FAMILY_CI;
  1305. else
  1306. adev->family = AMDGPU_FAMILY_KV;
  1307. r = cik_set_ip_blocks(adev);
  1308. if (r)
  1309. return r;
  1310. break;
  1311. #endif
  1312. case CHIP_VEGA10:
  1313. case CHIP_VEGA12:
  1314. case CHIP_VEGA20:
  1315. case CHIP_RAVEN:
  1316. case CHIP_PICASSO:
  1317. if ((adev->asic_type == CHIP_RAVEN) || (adev->asic_type == CHIP_PICASSO))
  1318. adev->family = AMDGPU_FAMILY_RV;
  1319. else
  1320. adev->family = AMDGPU_FAMILY_AI;
  1321. r = soc15_set_ip_blocks(adev);
  1322. if (r)
  1323. return r;
  1324. break;
  1325. default:
  1326. /* FIXME: not supported yet */
  1327. return -EINVAL;
  1328. }
  1329. r = amdgpu_device_parse_gpu_info_fw(adev);
  1330. if (r)
  1331. return r;
  1332. amdgpu_amdkfd_device_probe(adev);
  1333. if (amdgpu_sriov_vf(adev)) {
  1334. r = amdgpu_virt_request_full_gpu(adev, true);
  1335. if (r)
  1336. return -EAGAIN;
  1337. }
  1338. adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
  1339. if (amdgpu_sriov_vf(adev))
  1340. adev->powerplay.pp_feature &= ~PP_GFXOFF_MASK;
  1341. for (i = 0; i < adev->num_ip_blocks; i++) {
  1342. if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
  1343. DRM_ERROR("disabled ip block: %d <%s>\n",
  1344. i, adev->ip_blocks[i].version->funcs->name);
  1345. adev->ip_blocks[i].status.valid = false;
  1346. } else {
  1347. if (adev->ip_blocks[i].version->funcs->early_init) {
  1348. r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
  1349. if (r == -ENOENT) {
  1350. adev->ip_blocks[i].status.valid = false;
  1351. } else if (r) {
  1352. DRM_ERROR("early_init of IP block <%s> failed %d\n",
  1353. adev->ip_blocks[i].version->funcs->name, r);
  1354. return r;
  1355. } else {
  1356. adev->ip_blocks[i].status.valid = true;
  1357. }
  1358. } else {
  1359. adev->ip_blocks[i].status.valid = true;
  1360. }
  1361. }
  1362. }
  1363. adev->cg_flags &= amdgpu_cg_mask;
  1364. adev->pg_flags &= amdgpu_pg_mask;
  1365. return 0;
  1366. }
  1367. /**
  1368. * amdgpu_device_ip_init - run init for hardware IPs
  1369. *
  1370. * @adev: amdgpu_device pointer
  1371. *
  1372. * Main initialization pass for hardware IPs. The list of all the hardware
  1373. * IPs that make up the asic is walked and the sw_init and hw_init callbacks
  1374. * are run. sw_init initializes the software state associated with each IP
  1375. * and hw_init initializes the hardware associated with each IP.
  1376. * Returns 0 on success, negative error code on failure.
  1377. */
  1378. static int amdgpu_device_ip_init(struct amdgpu_device *adev)
  1379. {
  1380. int i, r;
  1381. for (i = 0; i < adev->num_ip_blocks; i++) {
  1382. if (!adev->ip_blocks[i].status.valid)
  1383. continue;
  1384. r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
  1385. if (r) {
  1386. DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  1387. adev->ip_blocks[i].version->funcs->name, r);
  1388. return r;
  1389. }
  1390. adev->ip_blocks[i].status.sw = true;
  1391. /* need to do gmc hw init early so we can allocate gpu mem */
  1392. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1393. r = amdgpu_device_vram_scratch_init(adev);
  1394. if (r) {
  1395. DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
  1396. return r;
  1397. }
  1398. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1399. if (r) {
  1400. DRM_ERROR("hw_init %d failed %d\n", i, r);
  1401. return r;
  1402. }
  1403. r = amdgpu_device_wb_init(adev);
  1404. if (r) {
  1405. DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
  1406. return r;
  1407. }
  1408. adev->ip_blocks[i].status.hw = true;
  1409. /* right after GMC hw init, we create CSA */
  1410. if (amdgpu_sriov_vf(adev)) {
  1411. r = amdgpu_allocate_static_csa(adev);
  1412. if (r) {
  1413. DRM_ERROR("allocate CSA failed %d\n", r);
  1414. return r;
  1415. }
  1416. }
  1417. }
  1418. }
  1419. for (i = 0; i < adev->num_ip_blocks; i++) {
  1420. if (!adev->ip_blocks[i].status.sw)
  1421. continue;
  1422. if (adev->ip_blocks[i].status.hw)
  1423. continue;
  1424. r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
  1425. if (r) {
  1426. DRM_ERROR("hw_init of IP block <%s> failed %d\n",
  1427. adev->ip_blocks[i].version->funcs->name, r);
  1428. return r;
  1429. }
  1430. adev->ip_blocks[i].status.hw = true;
  1431. }
  1432. amdgpu_xgmi_add_device(adev);
  1433. amdgpu_amdkfd_device_init(adev);
  1434. if (amdgpu_sriov_vf(adev))
  1435. amdgpu_virt_release_full_gpu(adev, true);
  1436. return 0;
  1437. }
  1438. /**
  1439. * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
  1440. *
  1441. * @adev: amdgpu_device pointer
  1442. *
  1443. * Writes a reset magic value to the gart pointer in VRAM. The driver calls
  1444. * this function before a GPU reset. If the value is retained after a
  1445. * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
  1446. */
  1447. static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
  1448. {
  1449. memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
  1450. }
  1451. /**
  1452. * amdgpu_device_check_vram_lost - check if vram is valid
  1453. *
  1454. * @adev: amdgpu_device pointer
  1455. *
  1456. * Checks the reset magic value written to the gart pointer in VRAM.
  1457. * The driver calls this after a GPU reset to see if the contents of
  1458. * VRAM is lost or now.
  1459. * returns true if vram is lost, false if not.
  1460. */
  1461. static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
  1462. {
  1463. return !!memcmp(adev->gart.ptr, adev->reset_magic,
  1464. AMDGPU_RESET_MAGIC_NUM);
  1465. }
  1466. /**
  1467. * amdgpu_device_set_cg_state - set clockgating for amdgpu device
  1468. *
  1469. * @adev: amdgpu_device pointer
  1470. *
  1471. * The list of all the hardware IPs that make up the asic is walked and the
  1472. * set_clockgating_state callbacks are run.
  1473. * Late initialization pass enabling clockgating for hardware IPs.
  1474. * Fini or suspend, pass disabling clockgating for hardware IPs.
  1475. * Returns 0 on success, negative error code on failure.
  1476. */
  1477. static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
  1478. enum amd_clockgating_state state)
  1479. {
  1480. int i, j, r;
  1481. if (amdgpu_emu_mode == 1)
  1482. return 0;
  1483. for (j = 0; j < adev->num_ip_blocks; j++) {
  1484. i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
  1485. if (!adev->ip_blocks[i].status.valid)
  1486. continue;
  1487. /* skip CG for VCE/UVD, it's handled specially */
  1488. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1489. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
  1490. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
  1491. adev->ip_blocks[i].version->funcs->set_clockgating_state) {
  1492. /* enable clockgating to save power */
  1493. r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
  1494. state);
  1495. if (r) {
  1496. DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
  1497. adev->ip_blocks[i].version->funcs->name, r);
  1498. return r;
  1499. }
  1500. }
  1501. }
  1502. return 0;
  1503. }
  1504. static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
  1505. {
  1506. int i, j, r;
  1507. if (amdgpu_emu_mode == 1)
  1508. return 0;
  1509. for (j = 0; j < adev->num_ip_blocks; j++) {
  1510. i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
  1511. if (!adev->ip_blocks[i].status.valid)
  1512. continue;
  1513. /* skip CG for VCE/UVD, it's handled specially */
  1514. if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
  1515. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
  1516. adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
  1517. adev->ip_blocks[i].version->funcs->set_powergating_state) {
  1518. /* enable powergating to save power */
  1519. r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
  1520. state);
  1521. if (r) {
  1522. DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
  1523. adev->ip_blocks[i].version->funcs->name, r);
  1524. return r;
  1525. }
  1526. }
  1527. }
  1528. return 0;
  1529. }
  1530. /**
  1531. * amdgpu_device_ip_late_init - run late init for hardware IPs
  1532. *
  1533. * @adev: amdgpu_device pointer
  1534. *
  1535. * Late initialization pass for hardware IPs. The list of all the hardware
  1536. * IPs that make up the asic is walked and the late_init callbacks are run.
  1537. * late_init covers any special initialization that an IP requires
  1538. * after all of the have been initialized or something that needs to happen
  1539. * late in the init process.
  1540. * Returns 0 on success, negative error code on failure.
  1541. */
  1542. static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
  1543. {
  1544. int i = 0, r;
  1545. for (i = 0; i < adev->num_ip_blocks; i++) {
  1546. if (!adev->ip_blocks[i].status.valid)
  1547. continue;
  1548. if (adev->ip_blocks[i].version->funcs->late_init) {
  1549. r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
  1550. if (r) {
  1551. DRM_ERROR("late_init of IP block <%s> failed %d\n",
  1552. adev->ip_blocks[i].version->funcs->name, r);
  1553. return r;
  1554. }
  1555. adev->ip_blocks[i].status.late_initialized = true;
  1556. }
  1557. }
  1558. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
  1559. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
  1560. queue_delayed_work(system_wq, &adev->late_init_work,
  1561. msecs_to_jiffies(AMDGPU_RESUME_MS));
  1562. amdgpu_device_fill_reset_magic(adev);
  1563. return 0;
  1564. }
  1565. /**
  1566. * amdgpu_device_ip_fini - run fini for hardware IPs
  1567. *
  1568. * @adev: amdgpu_device pointer
  1569. *
  1570. * Main teardown pass for hardware IPs. The list of all the hardware
  1571. * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
  1572. * are run. hw_fini tears down the hardware associated with each IP
  1573. * and sw_fini tears down any software state associated with each IP.
  1574. * Returns 0 on success, negative error code on failure.
  1575. */
  1576. static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
  1577. {
  1578. int i, r;
  1579. amdgpu_amdkfd_device_fini(adev);
  1580. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
  1581. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
  1582. /* need to disable SMC first */
  1583. for (i = 0; i < adev->num_ip_blocks; i++) {
  1584. if (!adev->ip_blocks[i].status.hw)
  1585. continue;
  1586. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
  1587. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1588. /* XXX handle errors */
  1589. if (r) {
  1590. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1591. adev->ip_blocks[i].version->funcs->name, r);
  1592. }
  1593. adev->ip_blocks[i].status.hw = false;
  1594. break;
  1595. }
  1596. }
  1597. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1598. if (!adev->ip_blocks[i].status.hw)
  1599. continue;
  1600. r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
  1601. /* XXX handle errors */
  1602. if (r) {
  1603. DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
  1604. adev->ip_blocks[i].version->funcs->name, r);
  1605. }
  1606. adev->ip_blocks[i].status.hw = false;
  1607. }
  1608. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1609. if (!adev->ip_blocks[i].status.sw)
  1610. continue;
  1611. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
  1612. amdgpu_free_static_csa(adev);
  1613. amdgpu_device_wb_fini(adev);
  1614. amdgpu_device_vram_scratch_fini(adev);
  1615. }
  1616. r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
  1617. /* XXX handle errors */
  1618. if (r) {
  1619. DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
  1620. adev->ip_blocks[i].version->funcs->name, r);
  1621. }
  1622. adev->ip_blocks[i].status.sw = false;
  1623. adev->ip_blocks[i].status.valid = false;
  1624. }
  1625. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1626. if (!adev->ip_blocks[i].status.late_initialized)
  1627. continue;
  1628. if (adev->ip_blocks[i].version->funcs->late_fini)
  1629. adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
  1630. adev->ip_blocks[i].status.late_initialized = false;
  1631. }
  1632. if (amdgpu_sriov_vf(adev))
  1633. if (amdgpu_virt_release_full_gpu(adev, false))
  1634. DRM_ERROR("failed to release exclusive mode on fini\n");
  1635. return 0;
  1636. }
  1637. /**
  1638. * amdgpu_device_ip_late_init_func_handler - work handler for ib test
  1639. *
  1640. * @work: work_struct.
  1641. */
  1642. static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
  1643. {
  1644. struct amdgpu_device *adev =
  1645. container_of(work, struct amdgpu_device, late_init_work.work);
  1646. int r;
  1647. r = amdgpu_ib_ring_tests(adev);
  1648. if (r)
  1649. DRM_ERROR("ib ring test failed (%d).\n", r);
  1650. }
  1651. static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
  1652. {
  1653. struct amdgpu_device *adev =
  1654. container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
  1655. mutex_lock(&adev->gfx.gfx_off_mutex);
  1656. if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
  1657. if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
  1658. adev->gfx.gfx_off_state = true;
  1659. }
  1660. mutex_unlock(&adev->gfx.gfx_off_mutex);
  1661. }
  1662. /**
  1663. * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
  1664. *
  1665. * @adev: amdgpu_device pointer
  1666. *
  1667. * Main suspend function for hardware IPs. The list of all the hardware
  1668. * IPs that make up the asic is walked, clockgating is disabled and the
  1669. * suspend callbacks are run. suspend puts the hardware and software state
  1670. * in each IP into a state suitable for suspend.
  1671. * Returns 0 on success, negative error code on failure.
  1672. */
  1673. static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
  1674. {
  1675. int i, r;
  1676. amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
  1677. amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
  1678. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1679. if (!adev->ip_blocks[i].status.valid)
  1680. continue;
  1681. /* displays are handled separately */
  1682. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
  1683. /* XXX handle errors */
  1684. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1685. /* XXX handle errors */
  1686. if (r) {
  1687. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1688. adev->ip_blocks[i].version->funcs->name, r);
  1689. }
  1690. }
  1691. }
  1692. return 0;
  1693. }
  1694. /**
  1695. * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
  1696. *
  1697. * @adev: amdgpu_device pointer
  1698. *
  1699. * Main suspend function for hardware IPs. The list of all the hardware
  1700. * IPs that make up the asic is walked, clockgating is disabled and the
  1701. * suspend callbacks are run. suspend puts the hardware and software state
  1702. * in each IP into a state suitable for suspend.
  1703. * Returns 0 on success, negative error code on failure.
  1704. */
  1705. static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
  1706. {
  1707. int i, r;
  1708. for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
  1709. if (!adev->ip_blocks[i].status.valid)
  1710. continue;
  1711. /* displays are handled in phase1 */
  1712. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
  1713. continue;
  1714. /* XXX handle errors */
  1715. r = adev->ip_blocks[i].version->funcs->suspend(adev);
  1716. /* XXX handle errors */
  1717. if (r) {
  1718. DRM_ERROR("suspend of IP block <%s> failed %d\n",
  1719. adev->ip_blocks[i].version->funcs->name, r);
  1720. }
  1721. }
  1722. return 0;
  1723. }
  1724. /**
  1725. * amdgpu_device_ip_suspend - run suspend for hardware IPs
  1726. *
  1727. * @adev: amdgpu_device pointer
  1728. *
  1729. * Main suspend function for hardware IPs. The list of all the hardware
  1730. * IPs that make up the asic is walked, clockgating is disabled and the
  1731. * suspend callbacks are run. suspend puts the hardware and software state
  1732. * in each IP into a state suitable for suspend.
  1733. * Returns 0 on success, negative error code on failure.
  1734. */
  1735. int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
  1736. {
  1737. int r;
  1738. if (amdgpu_sriov_vf(adev))
  1739. amdgpu_virt_request_full_gpu(adev, false);
  1740. r = amdgpu_device_ip_suspend_phase1(adev);
  1741. if (r)
  1742. return r;
  1743. r = amdgpu_device_ip_suspend_phase2(adev);
  1744. if (amdgpu_sriov_vf(adev))
  1745. amdgpu_virt_release_full_gpu(adev, false);
  1746. return r;
  1747. }
  1748. static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
  1749. {
  1750. int i, r;
  1751. static enum amd_ip_block_type ip_order[] = {
  1752. AMD_IP_BLOCK_TYPE_GMC,
  1753. AMD_IP_BLOCK_TYPE_COMMON,
  1754. AMD_IP_BLOCK_TYPE_PSP,
  1755. AMD_IP_BLOCK_TYPE_IH,
  1756. };
  1757. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1758. int j;
  1759. struct amdgpu_ip_block *block;
  1760. for (j = 0; j < adev->num_ip_blocks; j++) {
  1761. block = &adev->ip_blocks[j];
  1762. if (block->version->type != ip_order[i] ||
  1763. !block->status.valid)
  1764. continue;
  1765. r = block->version->funcs->hw_init(adev);
  1766. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
  1767. if (r)
  1768. return r;
  1769. }
  1770. }
  1771. return 0;
  1772. }
  1773. static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
  1774. {
  1775. int i, r;
  1776. static enum amd_ip_block_type ip_order[] = {
  1777. AMD_IP_BLOCK_TYPE_SMC,
  1778. AMD_IP_BLOCK_TYPE_DCE,
  1779. AMD_IP_BLOCK_TYPE_GFX,
  1780. AMD_IP_BLOCK_TYPE_SDMA,
  1781. AMD_IP_BLOCK_TYPE_UVD,
  1782. AMD_IP_BLOCK_TYPE_VCE
  1783. };
  1784. for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
  1785. int j;
  1786. struct amdgpu_ip_block *block;
  1787. for (j = 0; j < adev->num_ip_blocks; j++) {
  1788. block = &adev->ip_blocks[j];
  1789. if (block->version->type != ip_order[i] ||
  1790. !block->status.valid)
  1791. continue;
  1792. r = block->version->funcs->hw_init(adev);
  1793. DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
  1794. if (r)
  1795. return r;
  1796. }
  1797. }
  1798. return 0;
  1799. }
  1800. /**
  1801. * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
  1802. *
  1803. * @adev: amdgpu_device pointer
  1804. *
  1805. * First resume function for hardware IPs. The list of all the hardware
  1806. * IPs that make up the asic is walked and the resume callbacks are run for
  1807. * COMMON, GMC, and IH. resume puts the hardware into a functional state
  1808. * after a suspend and updates the software state as necessary. This
  1809. * function is also used for restoring the GPU after a GPU reset.
  1810. * Returns 0 on success, negative error code on failure.
  1811. */
  1812. static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
  1813. {
  1814. int i, r;
  1815. for (i = 0; i < adev->num_ip_blocks; i++) {
  1816. if (!adev->ip_blocks[i].status.valid)
  1817. continue;
  1818. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1819. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1820. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
  1821. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1822. if (r) {
  1823. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1824. adev->ip_blocks[i].version->funcs->name, r);
  1825. return r;
  1826. }
  1827. }
  1828. }
  1829. return 0;
  1830. }
  1831. /**
  1832. * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
  1833. *
  1834. * @adev: amdgpu_device pointer
  1835. *
  1836. * First resume function for hardware IPs. The list of all the hardware
  1837. * IPs that make up the asic is walked and the resume callbacks are run for
  1838. * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
  1839. * functional state after a suspend and updates the software state as
  1840. * necessary. This function is also used for restoring the GPU after a GPU
  1841. * reset.
  1842. * Returns 0 on success, negative error code on failure.
  1843. */
  1844. static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
  1845. {
  1846. int i, r;
  1847. for (i = 0; i < adev->num_ip_blocks; i++) {
  1848. if (!adev->ip_blocks[i].status.valid)
  1849. continue;
  1850. if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
  1851. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
  1852. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
  1853. continue;
  1854. r = adev->ip_blocks[i].version->funcs->resume(adev);
  1855. if (r) {
  1856. DRM_ERROR("resume of IP block <%s> failed %d\n",
  1857. adev->ip_blocks[i].version->funcs->name, r);
  1858. return r;
  1859. }
  1860. }
  1861. return 0;
  1862. }
  1863. /**
  1864. * amdgpu_device_ip_resume - run resume for hardware IPs
  1865. *
  1866. * @adev: amdgpu_device pointer
  1867. *
  1868. * Main resume function for hardware IPs. The hardware IPs
  1869. * are split into two resume functions because they are
  1870. * are also used in in recovering from a GPU reset and some additional
  1871. * steps need to be take between them. In this case (S3/S4) they are
  1872. * run sequentially.
  1873. * Returns 0 on success, negative error code on failure.
  1874. */
  1875. static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
  1876. {
  1877. int r;
  1878. r = amdgpu_device_ip_resume_phase1(adev);
  1879. if (r)
  1880. return r;
  1881. r = amdgpu_device_ip_resume_phase2(adev);
  1882. return r;
  1883. }
  1884. /**
  1885. * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
  1886. *
  1887. * @adev: amdgpu_device pointer
  1888. *
  1889. * Query the VBIOS data tables to determine if the board supports SR-IOV.
  1890. */
  1891. static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
  1892. {
  1893. if (amdgpu_sriov_vf(adev)) {
  1894. if (adev->is_atom_fw) {
  1895. if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
  1896. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1897. } else {
  1898. if (amdgpu_atombios_has_gpu_virtualization_table(adev))
  1899. adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
  1900. }
  1901. if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
  1902. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
  1903. }
  1904. }
  1905. /**
  1906. * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
  1907. *
  1908. * @asic_type: AMD asic type
  1909. *
  1910. * Check if there is DC (new modesetting infrastructre) support for an asic.
  1911. * returns true if DC has support, false if not.
  1912. */
  1913. bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
  1914. {
  1915. switch (asic_type) {
  1916. #if defined(CONFIG_DRM_AMD_DC)
  1917. case CHIP_BONAIRE:
  1918. case CHIP_KAVERI:
  1919. case CHIP_KABINI:
  1920. case CHIP_MULLINS:
  1921. /*
  1922. * We have systems in the wild with these ASICs that require
  1923. * LVDS and VGA support which is not supported with DC.
  1924. *
  1925. * Fallback to the non-DC driver here by default so as not to
  1926. * cause regressions.
  1927. */
  1928. return amdgpu_dc > 0;
  1929. case CHIP_HAWAII:
  1930. case CHIP_CARRIZO:
  1931. case CHIP_STONEY:
  1932. case CHIP_POLARIS10:
  1933. case CHIP_POLARIS11:
  1934. case CHIP_POLARIS12:
  1935. case CHIP_VEGAM:
  1936. case CHIP_TONGA:
  1937. case CHIP_FIJI:
  1938. case CHIP_VEGA10:
  1939. case CHIP_VEGA12:
  1940. case CHIP_VEGA20:
  1941. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  1942. case CHIP_RAVEN:
  1943. case CHIP_PICASSO:
  1944. #endif
  1945. return amdgpu_dc != 0;
  1946. #endif
  1947. default:
  1948. return false;
  1949. }
  1950. }
  1951. /**
  1952. * amdgpu_device_has_dc_support - check if dc is supported
  1953. *
  1954. * @adev: amdgpu_device_pointer
  1955. *
  1956. * Returns true for supported, false for not supported
  1957. */
  1958. bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
  1959. {
  1960. if (amdgpu_sriov_vf(adev))
  1961. return false;
  1962. return amdgpu_device_asic_has_dc_support(adev->asic_type);
  1963. }
  1964. /**
  1965. * amdgpu_device_init - initialize the driver
  1966. *
  1967. * @adev: amdgpu_device pointer
  1968. * @ddev: drm dev pointer
  1969. * @pdev: pci dev pointer
  1970. * @flags: driver flags
  1971. *
  1972. * Initializes the driver info and hw (all asics).
  1973. * Returns 0 for success or an error on failure.
  1974. * Called at driver startup.
  1975. */
  1976. int amdgpu_device_init(struct amdgpu_device *adev,
  1977. struct drm_device *ddev,
  1978. struct pci_dev *pdev,
  1979. uint32_t flags)
  1980. {
  1981. int r, i;
  1982. bool runtime = false;
  1983. u32 max_MBps;
  1984. adev->shutdown = false;
  1985. adev->dev = &pdev->dev;
  1986. adev->ddev = ddev;
  1987. adev->pdev = pdev;
  1988. adev->flags = flags;
  1989. adev->asic_type = flags & AMD_ASIC_MASK;
  1990. adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
  1991. if (amdgpu_emu_mode == 1)
  1992. adev->usec_timeout *= 2;
  1993. adev->gmc.gart_size = 512 * 1024 * 1024;
  1994. adev->accel_working = false;
  1995. adev->num_rings = 0;
  1996. adev->mman.buffer_funcs = NULL;
  1997. adev->mman.buffer_funcs_ring = NULL;
  1998. adev->vm_manager.vm_pte_funcs = NULL;
  1999. adev->vm_manager.vm_pte_num_rqs = 0;
  2000. adev->gmc.gmc_funcs = NULL;
  2001. adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2002. bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
  2003. adev->smc_rreg = &amdgpu_invalid_rreg;
  2004. adev->smc_wreg = &amdgpu_invalid_wreg;
  2005. adev->pcie_rreg = &amdgpu_invalid_rreg;
  2006. adev->pcie_wreg = &amdgpu_invalid_wreg;
  2007. adev->pciep_rreg = &amdgpu_invalid_rreg;
  2008. adev->pciep_wreg = &amdgpu_invalid_wreg;
  2009. adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
  2010. adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
  2011. adev->didt_rreg = &amdgpu_invalid_rreg;
  2012. adev->didt_wreg = &amdgpu_invalid_wreg;
  2013. adev->gc_cac_rreg = &amdgpu_invalid_rreg;
  2014. adev->gc_cac_wreg = &amdgpu_invalid_wreg;
  2015. adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
  2016. adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
  2017. DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
  2018. amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
  2019. pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
  2020. /* mutex initialization are all done here so we
  2021. * can recall function without having locking issues */
  2022. atomic_set(&adev->irq.ih.lock, 0);
  2023. mutex_init(&adev->firmware.mutex);
  2024. mutex_init(&adev->pm.mutex);
  2025. mutex_init(&adev->gfx.gpu_clock_mutex);
  2026. mutex_init(&adev->srbm_mutex);
  2027. mutex_init(&adev->gfx.pipe_reserve_mutex);
  2028. mutex_init(&adev->gfx.gfx_off_mutex);
  2029. mutex_init(&adev->grbm_idx_mutex);
  2030. mutex_init(&adev->mn_lock);
  2031. mutex_init(&adev->virt.vf_errors.lock);
  2032. hash_init(adev->mn_hash);
  2033. mutex_init(&adev->lock_reset);
  2034. amdgpu_device_check_arguments(adev);
  2035. spin_lock_init(&adev->mmio_idx_lock);
  2036. spin_lock_init(&adev->smc_idx_lock);
  2037. spin_lock_init(&adev->pcie_idx_lock);
  2038. spin_lock_init(&adev->uvd_ctx_idx_lock);
  2039. spin_lock_init(&adev->didt_idx_lock);
  2040. spin_lock_init(&adev->gc_cac_idx_lock);
  2041. spin_lock_init(&adev->se_cac_idx_lock);
  2042. spin_lock_init(&adev->audio_endpt_idx_lock);
  2043. spin_lock_init(&adev->mm_stats.lock);
  2044. INIT_LIST_HEAD(&adev->shadow_list);
  2045. mutex_init(&adev->shadow_list_lock);
  2046. INIT_LIST_HEAD(&adev->ring_lru_list);
  2047. spin_lock_init(&adev->ring_lru_list_lock);
  2048. INIT_DELAYED_WORK(&adev->late_init_work,
  2049. amdgpu_device_ip_late_init_func_handler);
  2050. INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
  2051. amdgpu_device_delay_enable_gfx_off);
  2052. adev->gfx.gfx_off_req_count = 1;
  2053. adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
  2054. /* Registers mapping */
  2055. /* TODO: block userspace mapping of io register */
  2056. if (adev->asic_type >= CHIP_BONAIRE) {
  2057. adev->rmmio_base = pci_resource_start(adev->pdev, 5);
  2058. adev->rmmio_size = pci_resource_len(adev->pdev, 5);
  2059. } else {
  2060. adev->rmmio_base = pci_resource_start(adev->pdev, 2);
  2061. adev->rmmio_size = pci_resource_len(adev->pdev, 2);
  2062. }
  2063. adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
  2064. if (adev->rmmio == NULL) {
  2065. return -ENOMEM;
  2066. }
  2067. DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
  2068. DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
  2069. /* doorbell bar mapping */
  2070. amdgpu_device_doorbell_init(adev);
  2071. /* io port mapping */
  2072. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2073. if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
  2074. adev->rio_mem_size = pci_resource_len(adev->pdev, i);
  2075. adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
  2076. break;
  2077. }
  2078. }
  2079. if (adev->rio_mem == NULL)
  2080. DRM_INFO("PCI I/O BAR is not found.\n");
  2081. amdgpu_device_get_pcie_info(adev);
  2082. /* early init functions */
  2083. r = amdgpu_device_ip_early_init(adev);
  2084. if (r)
  2085. return r;
  2086. /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
  2087. /* this will fail for cards that aren't VGA class devices, just
  2088. * ignore it */
  2089. vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
  2090. if (amdgpu_device_is_px(ddev))
  2091. runtime = true;
  2092. if (!pci_is_thunderbolt_attached(adev->pdev))
  2093. vga_switcheroo_register_client(adev->pdev,
  2094. &amdgpu_switcheroo_ops, runtime);
  2095. if (runtime)
  2096. vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
  2097. if (amdgpu_emu_mode == 1) {
  2098. /* post the asic on emulation mode */
  2099. emu_soc_asic_init(adev);
  2100. goto fence_driver_init;
  2101. }
  2102. /* Read BIOS */
  2103. if (!amdgpu_get_bios(adev)) {
  2104. r = -EINVAL;
  2105. goto failed;
  2106. }
  2107. r = amdgpu_atombios_init(adev);
  2108. if (r) {
  2109. dev_err(adev->dev, "amdgpu_atombios_init failed\n");
  2110. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
  2111. goto failed;
  2112. }
  2113. /* detect if we are with an SRIOV vbios */
  2114. amdgpu_device_detect_sriov_bios(adev);
  2115. /* Post card if necessary */
  2116. if (amdgpu_device_need_post(adev)) {
  2117. if (!adev->bios) {
  2118. dev_err(adev->dev, "no vBIOS found\n");
  2119. r = -EINVAL;
  2120. goto failed;
  2121. }
  2122. DRM_INFO("GPU posting now...\n");
  2123. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2124. if (r) {
  2125. dev_err(adev->dev, "gpu post error!\n");
  2126. goto failed;
  2127. }
  2128. }
  2129. if (adev->is_atom_fw) {
  2130. /* Initialize clocks */
  2131. r = amdgpu_atomfirmware_get_clock_info(adev);
  2132. if (r) {
  2133. dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
  2134. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2135. goto failed;
  2136. }
  2137. } else {
  2138. /* Initialize clocks */
  2139. r = amdgpu_atombios_get_clock_info(adev);
  2140. if (r) {
  2141. dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
  2142. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
  2143. goto failed;
  2144. }
  2145. /* init i2c buses */
  2146. if (!amdgpu_device_has_dc_support(adev))
  2147. amdgpu_atombios_i2c_init(adev);
  2148. }
  2149. fence_driver_init:
  2150. /* Fence driver */
  2151. r = amdgpu_fence_driver_init(adev);
  2152. if (r) {
  2153. dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
  2154. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
  2155. goto failed;
  2156. }
  2157. /* init the mode config */
  2158. drm_mode_config_init(adev->ddev);
  2159. r = amdgpu_device_ip_init(adev);
  2160. if (r) {
  2161. /* failed in exclusive mode due to timeout */
  2162. if (amdgpu_sriov_vf(adev) &&
  2163. !amdgpu_sriov_runtime(adev) &&
  2164. amdgpu_virt_mmio_blocked(adev) &&
  2165. !amdgpu_virt_wait_reset(adev)) {
  2166. dev_err(adev->dev, "VF exclusive mode timeout\n");
  2167. /* Don't send request since VF is inactive. */
  2168. adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
  2169. adev->virt.ops = NULL;
  2170. r = -EAGAIN;
  2171. goto failed;
  2172. }
  2173. dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
  2174. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
  2175. goto failed;
  2176. }
  2177. adev->accel_working = true;
  2178. amdgpu_vm_check_compute_bug(adev);
  2179. /* Initialize the buffer migration limit. */
  2180. if (amdgpu_moverate >= 0)
  2181. max_MBps = amdgpu_moverate;
  2182. else
  2183. max_MBps = 8; /* Allow 8 MB/s. */
  2184. /* Get a log2 for easy divisions. */
  2185. adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
  2186. r = amdgpu_ib_pool_init(adev);
  2187. if (r) {
  2188. dev_err(adev->dev, "IB initialization failed (%d).\n", r);
  2189. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
  2190. goto failed;
  2191. }
  2192. if (amdgpu_sriov_vf(adev))
  2193. amdgpu_virt_init_data_exchange(adev);
  2194. amdgpu_fbdev_init(adev);
  2195. r = amdgpu_pm_sysfs_init(adev);
  2196. if (r)
  2197. DRM_ERROR("registering pm debugfs failed (%d).\n", r);
  2198. r = amdgpu_debugfs_gem_init(adev);
  2199. if (r)
  2200. DRM_ERROR("registering gem debugfs failed (%d).\n", r);
  2201. r = amdgpu_debugfs_regs_init(adev);
  2202. if (r)
  2203. DRM_ERROR("registering register debugfs failed (%d).\n", r);
  2204. r = amdgpu_debugfs_firmware_init(adev);
  2205. if (r)
  2206. DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
  2207. r = amdgpu_debugfs_init(adev);
  2208. if (r)
  2209. DRM_ERROR("Creating debugfs files failed (%d).\n", r);
  2210. if ((amdgpu_testing & 1)) {
  2211. if (adev->accel_working)
  2212. amdgpu_test_moves(adev);
  2213. else
  2214. DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
  2215. }
  2216. if (amdgpu_benchmarking) {
  2217. if (adev->accel_working)
  2218. amdgpu_benchmark(adev, amdgpu_benchmarking);
  2219. else
  2220. DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
  2221. }
  2222. /* enable clockgating, etc. after ib tests, etc. since some blocks require
  2223. * explicit gating rather than handling it automatically.
  2224. */
  2225. r = amdgpu_device_ip_late_init(adev);
  2226. if (r) {
  2227. dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
  2228. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
  2229. goto failed;
  2230. }
  2231. return 0;
  2232. failed:
  2233. amdgpu_vf_error_trans_all(adev);
  2234. if (runtime)
  2235. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2236. return r;
  2237. }
  2238. /**
  2239. * amdgpu_device_fini - tear down the driver
  2240. *
  2241. * @adev: amdgpu_device pointer
  2242. *
  2243. * Tear down the driver info (all asics).
  2244. * Called at driver shutdown.
  2245. */
  2246. void amdgpu_device_fini(struct amdgpu_device *adev)
  2247. {
  2248. int r;
  2249. DRM_INFO("amdgpu: finishing device.\n");
  2250. adev->shutdown = true;
  2251. /* disable all interrupts */
  2252. amdgpu_irq_disable_all(adev);
  2253. if (adev->mode_info.mode_config_initialized){
  2254. if (!amdgpu_device_has_dc_support(adev))
  2255. drm_crtc_force_disable_all(adev->ddev);
  2256. else
  2257. drm_atomic_helper_shutdown(adev->ddev);
  2258. }
  2259. amdgpu_ib_pool_fini(adev);
  2260. amdgpu_fence_driver_fini(adev);
  2261. amdgpu_pm_sysfs_fini(adev);
  2262. amdgpu_fbdev_fini(adev);
  2263. r = amdgpu_device_ip_fini(adev);
  2264. if (adev->firmware.gpu_info_fw) {
  2265. release_firmware(adev->firmware.gpu_info_fw);
  2266. adev->firmware.gpu_info_fw = NULL;
  2267. }
  2268. adev->accel_working = false;
  2269. cancel_delayed_work_sync(&adev->late_init_work);
  2270. /* free i2c buses */
  2271. if (!amdgpu_device_has_dc_support(adev))
  2272. amdgpu_i2c_fini(adev);
  2273. if (amdgpu_emu_mode != 1)
  2274. amdgpu_atombios_fini(adev);
  2275. kfree(adev->bios);
  2276. adev->bios = NULL;
  2277. if (!pci_is_thunderbolt_attached(adev->pdev))
  2278. vga_switcheroo_unregister_client(adev->pdev);
  2279. if (adev->flags & AMD_IS_PX)
  2280. vga_switcheroo_fini_domain_pm_ops(adev->dev);
  2281. vga_client_register(adev->pdev, NULL, NULL, NULL);
  2282. if (adev->rio_mem)
  2283. pci_iounmap(adev->pdev, adev->rio_mem);
  2284. adev->rio_mem = NULL;
  2285. iounmap(adev->rmmio);
  2286. adev->rmmio = NULL;
  2287. amdgpu_device_doorbell_fini(adev);
  2288. amdgpu_debugfs_regs_cleanup(adev);
  2289. }
  2290. /*
  2291. * Suspend & resume.
  2292. */
  2293. /**
  2294. * amdgpu_device_suspend - initiate device suspend
  2295. *
  2296. * @dev: drm dev pointer
  2297. * @suspend: suspend state
  2298. * @fbcon : notify the fbdev of suspend
  2299. *
  2300. * Puts the hw in the suspend state (all asics).
  2301. * Returns 0 for success or an error on failure.
  2302. * Called at driver suspend.
  2303. */
  2304. int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
  2305. {
  2306. struct amdgpu_device *adev;
  2307. struct drm_crtc *crtc;
  2308. struct drm_connector *connector;
  2309. int r;
  2310. if (dev == NULL || dev->dev_private == NULL) {
  2311. return -ENODEV;
  2312. }
  2313. adev = dev->dev_private;
  2314. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2315. return 0;
  2316. drm_kms_helper_poll_disable(dev);
  2317. if (fbcon)
  2318. amdgpu_fbdev_set_suspend(adev, 1);
  2319. cancel_delayed_work_sync(&adev->late_init_work);
  2320. if (!amdgpu_device_has_dc_support(adev)) {
  2321. /* turn off display hw */
  2322. drm_modeset_lock_all(dev);
  2323. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2324. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  2325. }
  2326. drm_modeset_unlock_all(dev);
  2327. /* unpin the front buffers and cursors */
  2328. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2329. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2330. struct drm_framebuffer *fb = crtc->primary->fb;
  2331. struct amdgpu_bo *robj;
  2332. if (amdgpu_crtc->cursor_bo) {
  2333. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2334. r = amdgpu_bo_reserve(aobj, true);
  2335. if (r == 0) {
  2336. amdgpu_bo_unpin(aobj);
  2337. amdgpu_bo_unreserve(aobj);
  2338. }
  2339. }
  2340. if (fb == NULL || fb->obj[0] == NULL) {
  2341. continue;
  2342. }
  2343. robj = gem_to_amdgpu_bo(fb->obj[0]);
  2344. /* don't unpin kernel fb objects */
  2345. if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
  2346. r = amdgpu_bo_reserve(robj, true);
  2347. if (r == 0) {
  2348. amdgpu_bo_unpin(robj);
  2349. amdgpu_bo_unreserve(robj);
  2350. }
  2351. }
  2352. }
  2353. }
  2354. amdgpu_amdkfd_suspend(adev);
  2355. r = amdgpu_device_ip_suspend_phase1(adev);
  2356. /* evict vram memory */
  2357. amdgpu_bo_evict_vram(adev);
  2358. amdgpu_fence_driver_suspend(adev);
  2359. r = amdgpu_device_ip_suspend_phase2(adev);
  2360. /* evict remaining vram memory
  2361. * This second call to evict vram is to evict the gart page table
  2362. * using the CPU.
  2363. */
  2364. amdgpu_bo_evict_vram(adev);
  2365. pci_save_state(dev->pdev);
  2366. if (suspend) {
  2367. /* Shut down the device */
  2368. pci_disable_device(dev->pdev);
  2369. pci_set_power_state(dev->pdev, PCI_D3hot);
  2370. } else {
  2371. r = amdgpu_asic_reset(adev);
  2372. if (r)
  2373. DRM_ERROR("amdgpu asic reset failed\n");
  2374. }
  2375. return 0;
  2376. }
  2377. /**
  2378. * amdgpu_device_resume - initiate device resume
  2379. *
  2380. * @dev: drm dev pointer
  2381. * @resume: resume state
  2382. * @fbcon : notify the fbdev of resume
  2383. *
  2384. * Bring the hw back to operating state (all asics).
  2385. * Returns 0 for success or an error on failure.
  2386. * Called at driver resume.
  2387. */
  2388. int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
  2389. {
  2390. struct drm_connector *connector;
  2391. struct amdgpu_device *adev = dev->dev_private;
  2392. struct drm_crtc *crtc;
  2393. int r = 0;
  2394. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  2395. return 0;
  2396. if (resume) {
  2397. pci_set_power_state(dev->pdev, PCI_D0);
  2398. pci_restore_state(dev->pdev);
  2399. r = pci_enable_device(dev->pdev);
  2400. if (r)
  2401. return r;
  2402. }
  2403. /* post card */
  2404. if (amdgpu_device_need_post(adev)) {
  2405. r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2406. if (r)
  2407. DRM_ERROR("amdgpu asic init failed\n");
  2408. }
  2409. r = amdgpu_device_ip_resume(adev);
  2410. if (r) {
  2411. DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
  2412. return r;
  2413. }
  2414. amdgpu_fence_driver_resume(adev);
  2415. r = amdgpu_device_ip_late_init(adev);
  2416. if (r)
  2417. return r;
  2418. if (!amdgpu_device_has_dc_support(adev)) {
  2419. /* pin cursors */
  2420. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2421. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  2422. if (amdgpu_crtc->cursor_bo) {
  2423. struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
  2424. r = amdgpu_bo_reserve(aobj, true);
  2425. if (r == 0) {
  2426. r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
  2427. if (r != 0)
  2428. DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
  2429. amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
  2430. amdgpu_bo_unreserve(aobj);
  2431. }
  2432. }
  2433. }
  2434. }
  2435. r = amdgpu_amdkfd_resume(adev);
  2436. if (r)
  2437. return r;
  2438. /* Make sure IB tests flushed */
  2439. flush_delayed_work(&adev->late_init_work);
  2440. /* blat the mode back in */
  2441. if (fbcon) {
  2442. if (!amdgpu_device_has_dc_support(adev)) {
  2443. /* pre DCE11 */
  2444. drm_helper_resume_force_mode(dev);
  2445. /* turn on display hw */
  2446. drm_modeset_lock_all(dev);
  2447. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2448. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
  2449. }
  2450. drm_modeset_unlock_all(dev);
  2451. }
  2452. amdgpu_fbdev_set_suspend(adev, 0);
  2453. }
  2454. drm_kms_helper_poll_enable(dev);
  2455. /*
  2456. * Most of the connector probing functions try to acquire runtime pm
  2457. * refs to ensure that the GPU is powered on when connector polling is
  2458. * performed. Since we're calling this from a runtime PM callback,
  2459. * trying to acquire rpm refs will cause us to deadlock.
  2460. *
  2461. * Since we're guaranteed to be holding the rpm lock, it's safe to
  2462. * temporarily disable the rpm helpers so this doesn't deadlock us.
  2463. */
  2464. #ifdef CONFIG_PM
  2465. dev->dev->power.disable_depth++;
  2466. #endif
  2467. if (!amdgpu_device_has_dc_support(adev))
  2468. drm_helper_hpd_irq_event(dev);
  2469. else
  2470. drm_kms_helper_hotplug_event(dev);
  2471. #ifdef CONFIG_PM
  2472. dev->dev->power.disable_depth--;
  2473. #endif
  2474. return 0;
  2475. }
  2476. /**
  2477. * amdgpu_device_ip_check_soft_reset - did soft reset succeed
  2478. *
  2479. * @adev: amdgpu_device pointer
  2480. *
  2481. * The list of all the hardware IPs that make up the asic is walked and
  2482. * the check_soft_reset callbacks are run. check_soft_reset determines
  2483. * if the asic is still hung or not.
  2484. * Returns true if any of the IPs are still in a hung state, false if not.
  2485. */
  2486. static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
  2487. {
  2488. int i;
  2489. bool asic_hang = false;
  2490. if (amdgpu_sriov_vf(adev))
  2491. return true;
  2492. if (amdgpu_asic_need_full_reset(adev))
  2493. return true;
  2494. for (i = 0; i < adev->num_ip_blocks; i++) {
  2495. if (!adev->ip_blocks[i].status.valid)
  2496. continue;
  2497. if (adev->ip_blocks[i].version->funcs->check_soft_reset)
  2498. adev->ip_blocks[i].status.hang =
  2499. adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
  2500. if (adev->ip_blocks[i].status.hang) {
  2501. DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
  2502. asic_hang = true;
  2503. }
  2504. }
  2505. return asic_hang;
  2506. }
  2507. /**
  2508. * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
  2509. *
  2510. * @adev: amdgpu_device pointer
  2511. *
  2512. * The list of all the hardware IPs that make up the asic is walked and the
  2513. * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
  2514. * handles any IP specific hardware or software state changes that are
  2515. * necessary for a soft reset to succeed.
  2516. * Returns 0 on success, negative error code on failure.
  2517. */
  2518. static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
  2519. {
  2520. int i, r = 0;
  2521. for (i = 0; i < adev->num_ip_blocks; i++) {
  2522. if (!adev->ip_blocks[i].status.valid)
  2523. continue;
  2524. if (adev->ip_blocks[i].status.hang &&
  2525. adev->ip_blocks[i].version->funcs->pre_soft_reset) {
  2526. r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
  2527. if (r)
  2528. return r;
  2529. }
  2530. }
  2531. return 0;
  2532. }
  2533. /**
  2534. * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
  2535. *
  2536. * @adev: amdgpu_device pointer
  2537. *
  2538. * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
  2539. * reset is necessary to recover.
  2540. * Returns true if a full asic reset is required, false if not.
  2541. */
  2542. static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
  2543. {
  2544. int i;
  2545. if (amdgpu_asic_need_full_reset(adev))
  2546. return true;
  2547. for (i = 0; i < adev->num_ip_blocks; i++) {
  2548. if (!adev->ip_blocks[i].status.valid)
  2549. continue;
  2550. if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
  2551. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
  2552. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
  2553. (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
  2554. adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
  2555. if (adev->ip_blocks[i].status.hang) {
  2556. DRM_INFO("Some block need full reset!\n");
  2557. return true;
  2558. }
  2559. }
  2560. }
  2561. return false;
  2562. }
  2563. /**
  2564. * amdgpu_device_ip_soft_reset - do a soft reset
  2565. *
  2566. * @adev: amdgpu_device pointer
  2567. *
  2568. * The list of all the hardware IPs that make up the asic is walked and the
  2569. * soft_reset callbacks are run if the block is hung. soft_reset handles any
  2570. * IP specific hardware or software state changes that are necessary to soft
  2571. * reset the IP.
  2572. * Returns 0 on success, negative error code on failure.
  2573. */
  2574. static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
  2575. {
  2576. int i, r = 0;
  2577. for (i = 0; i < adev->num_ip_blocks; i++) {
  2578. if (!adev->ip_blocks[i].status.valid)
  2579. continue;
  2580. if (adev->ip_blocks[i].status.hang &&
  2581. adev->ip_blocks[i].version->funcs->soft_reset) {
  2582. r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
  2583. if (r)
  2584. return r;
  2585. }
  2586. }
  2587. return 0;
  2588. }
  2589. /**
  2590. * amdgpu_device_ip_post_soft_reset - clean up from soft reset
  2591. *
  2592. * @adev: amdgpu_device pointer
  2593. *
  2594. * The list of all the hardware IPs that make up the asic is walked and the
  2595. * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
  2596. * handles any IP specific hardware or software state changes that are
  2597. * necessary after the IP has been soft reset.
  2598. * Returns 0 on success, negative error code on failure.
  2599. */
  2600. static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
  2601. {
  2602. int i, r = 0;
  2603. for (i = 0; i < adev->num_ip_blocks; i++) {
  2604. if (!adev->ip_blocks[i].status.valid)
  2605. continue;
  2606. if (adev->ip_blocks[i].status.hang &&
  2607. adev->ip_blocks[i].version->funcs->post_soft_reset)
  2608. r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
  2609. if (r)
  2610. return r;
  2611. }
  2612. return 0;
  2613. }
  2614. /**
  2615. * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
  2616. *
  2617. * @adev: amdgpu_device pointer
  2618. * @ring: amdgpu_ring for the engine handling the buffer operations
  2619. * @bo: amdgpu_bo buffer whose shadow is being restored
  2620. * @fence: dma_fence associated with the operation
  2621. *
  2622. * Restores the VRAM buffer contents from the shadow in GTT. Used to
  2623. * restore things like GPUVM page tables after a GPU reset where
  2624. * the contents of VRAM might be lost.
  2625. * Returns 0 on success, negative error code on failure.
  2626. */
  2627. static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
  2628. struct amdgpu_ring *ring,
  2629. struct amdgpu_bo *bo,
  2630. struct dma_fence **fence)
  2631. {
  2632. uint32_t domain;
  2633. int r;
  2634. if (!bo->shadow)
  2635. return 0;
  2636. r = amdgpu_bo_reserve(bo, true);
  2637. if (r)
  2638. return r;
  2639. domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
  2640. /* if bo has been evicted, then no need to recover */
  2641. if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
  2642. r = amdgpu_bo_validate(bo->shadow);
  2643. if (r) {
  2644. DRM_ERROR("bo validate failed!\n");
  2645. goto err;
  2646. }
  2647. r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  2648. NULL, fence, true);
  2649. if (r) {
  2650. DRM_ERROR("recover page table failed!\n");
  2651. goto err;
  2652. }
  2653. }
  2654. err:
  2655. amdgpu_bo_unreserve(bo);
  2656. return r;
  2657. }
  2658. /**
  2659. * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
  2660. *
  2661. * @adev: amdgpu_device pointer
  2662. *
  2663. * Restores the contents of VRAM buffers from the shadows in GTT. Used to
  2664. * restore things like GPUVM page tables after a GPU reset where
  2665. * the contents of VRAM might be lost.
  2666. * Returns 0 on success, 1 on failure.
  2667. */
  2668. static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
  2669. {
  2670. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  2671. struct amdgpu_bo *bo, *tmp;
  2672. struct dma_fence *fence = NULL, *next = NULL;
  2673. long r = 1;
  2674. int i = 0;
  2675. long tmo;
  2676. if (amdgpu_sriov_runtime(adev))
  2677. tmo = msecs_to_jiffies(8000);
  2678. else
  2679. tmo = msecs_to_jiffies(100);
  2680. DRM_INFO("recover vram bo from shadow start\n");
  2681. mutex_lock(&adev->shadow_list_lock);
  2682. list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
  2683. next = NULL;
  2684. amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
  2685. if (fence) {
  2686. r = dma_fence_wait_timeout(fence, false, tmo);
  2687. if (r == 0)
  2688. pr_err("wait fence %p[%d] timeout\n", fence, i);
  2689. else if (r < 0)
  2690. pr_err("wait fence %p[%d] interrupted\n", fence, i);
  2691. if (r < 1) {
  2692. dma_fence_put(fence);
  2693. fence = next;
  2694. break;
  2695. }
  2696. i++;
  2697. }
  2698. dma_fence_put(fence);
  2699. fence = next;
  2700. }
  2701. mutex_unlock(&adev->shadow_list_lock);
  2702. if (fence) {
  2703. r = dma_fence_wait_timeout(fence, false, tmo);
  2704. if (r == 0)
  2705. pr_err("wait fence %p[%d] timeout\n", fence, i);
  2706. else if (r < 0)
  2707. pr_err("wait fence %p[%d] interrupted\n", fence, i);
  2708. }
  2709. dma_fence_put(fence);
  2710. if (r > 0)
  2711. DRM_INFO("recover vram bo from shadow done\n");
  2712. else
  2713. DRM_ERROR("recover vram bo from shadow failed\n");
  2714. return (r > 0) ? 0 : 1;
  2715. }
  2716. /**
  2717. * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
  2718. *
  2719. * @adev: amdgpu device pointer
  2720. *
  2721. * attempt to do soft-reset or full-reset and reinitialize Asic
  2722. * return 0 means succeeded otherwise failed
  2723. */
  2724. static int amdgpu_device_reset(struct amdgpu_device *adev)
  2725. {
  2726. bool need_full_reset, vram_lost = 0;
  2727. int r;
  2728. need_full_reset = amdgpu_device_ip_need_full_reset(adev);
  2729. if (!need_full_reset) {
  2730. amdgpu_device_ip_pre_soft_reset(adev);
  2731. r = amdgpu_device_ip_soft_reset(adev);
  2732. amdgpu_device_ip_post_soft_reset(adev);
  2733. if (r || amdgpu_device_ip_check_soft_reset(adev)) {
  2734. DRM_INFO("soft reset failed, will fallback to full reset!\n");
  2735. need_full_reset = true;
  2736. }
  2737. }
  2738. if (need_full_reset) {
  2739. r = amdgpu_device_ip_suspend(adev);
  2740. retry:
  2741. r = amdgpu_asic_reset(adev);
  2742. /* post card */
  2743. amdgpu_atom_asic_init(adev->mode_info.atom_context);
  2744. if (!r) {
  2745. dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
  2746. r = amdgpu_device_ip_resume_phase1(adev);
  2747. if (r)
  2748. goto out;
  2749. vram_lost = amdgpu_device_check_vram_lost(adev);
  2750. if (vram_lost) {
  2751. DRM_ERROR("VRAM is lost!\n");
  2752. atomic_inc(&adev->vram_lost_counter);
  2753. }
  2754. r = amdgpu_gtt_mgr_recover(
  2755. &adev->mman.bdev.man[TTM_PL_TT]);
  2756. if (r)
  2757. goto out;
  2758. r = amdgpu_device_ip_resume_phase2(adev);
  2759. if (r)
  2760. goto out;
  2761. if (vram_lost)
  2762. amdgpu_device_fill_reset_magic(adev);
  2763. }
  2764. }
  2765. out:
  2766. if (!r) {
  2767. amdgpu_irq_gpu_reset_resume_helper(adev);
  2768. r = amdgpu_ib_ring_tests(adev);
  2769. if (r) {
  2770. dev_err(adev->dev, "ib ring test failed (%d).\n", r);
  2771. r = amdgpu_device_ip_suspend(adev);
  2772. need_full_reset = true;
  2773. goto retry;
  2774. }
  2775. }
  2776. if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
  2777. r = amdgpu_device_handle_vram_lost(adev);
  2778. return r;
  2779. }
  2780. /**
  2781. * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
  2782. *
  2783. * @adev: amdgpu device pointer
  2784. * @from_hypervisor: request from hypervisor
  2785. *
  2786. * do VF FLR and reinitialize Asic
  2787. * return 0 means succeeded otherwise failed
  2788. */
  2789. static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
  2790. bool from_hypervisor)
  2791. {
  2792. int r;
  2793. if (from_hypervisor)
  2794. r = amdgpu_virt_request_full_gpu(adev, true);
  2795. else
  2796. r = amdgpu_virt_reset_gpu(adev);
  2797. if (r)
  2798. return r;
  2799. /* Resume IP prior to SMC */
  2800. r = amdgpu_device_ip_reinit_early_sriov(adev);
  2801. if (r)
  2802. goto error;
  2803. /* we need recover gart prior to run SMC/CP/SDMA resume */
  2804. amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
  2805. /* now we are okay to resume SMC/CP/SDMA */
  2806. r = amdgpu_device_ip_reinit_late_sriov(adev);
  2807. if (r)
  2808. goto error;
  2809. amdgpu_irq_gpu_reset_resume_helper(adev);
  2810. r = amdgpu_ib_ring_tests(adev);
  2811. error:
  2812. amdgpu_virt_release_full_gpu(adev, true);
  2813. if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
  2814. atomic_inc(&adev->vram_lost_counter);
  2815. r = amdgpu_device_handle_vram_lost(adev);
  2816. }
  2817. return r;
  2818. }
  2819. /**
  2820. * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
  2821. *
  2822. * @adev: amdgpu device pointer
  2823. *
  2824. * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
  2825. * a hung GPU.
  2826. */
  2827. bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
  2828. {
  2829. if (!amdgpu_device_ip_check_soft_reset(adev)) {
  2830. DRM_INFO("Timeout, but no hardware hang detected.\n");
  2831. return false;
  2832. }
  2833. if (amdgpu_gpu_recovery == 0 || (amdgpu_gpu_recovery == -1 &&
  2834. !amdgpu_sriov_vf(adev))) {
  2835. DRM_INFO("GPU recovery disabled.\n");
  2836. return false;
  2837. }
  2838. return true;
  2839. }
  2840. /**
  2841. * amdgpu_device_gpu_recover - reset the asic and recover scheduler
  2842. *
  2843. * @adev: amdgpu device pointer
  2844. * @job: which job trigger hang
  2845. *
  2846. * Attempt to reset the GPU if it has hung (all asics).
  2847. * Returns 0 for success or an error on failure.
  2848. */
  2849. int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
  2850. struct amdgpu_job *job)
  2851. {
  2852. int i, r, resched;
  2853. dev_info(adev->dev, "GPU reset begin!\n");
  2854. mutex_lock(&adev->lock_reset);
  2855. atomic_inc(&adev->gpu_reset_counter);
  2856. adev->in_gpu_reset = 1;
  2857. /* Block kfd */
  2858. amdgpu_amdkfd_pre_reset(adev);
  2859. /* block TTM */
  2860. resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
  2861. /* block all schedulers and reset given job's ring */
  2862. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2863. struct amdgpu_ring *ring = adev->rings[i];
  2864. if (!ring || !ring->sched.thread)
  2865. continue;
  2866. kthread_park(ring->sched.thread);
  2867. if (job && job->base.sched == &ring->sched)
  2868. continue;
  2869. drm_sched_hw_job_reset(&ring->sched, job ? &job->base : NULL);
  2870. /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
  2871. amdgpu_fence_driver_force_completion(ring);
  2872. }
  2873. if (amdgpu_sriov_vf(adev))
  2874. r = amdgpu_device_reset_sriov(adev, job ? false : true);
  2875. else
  2876. r = amdgpu_device_reset(adev);
  2877. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  2878. struct amdgpu_ring *ring = adev->rings[i];
  2879. if (!ring || !ring->sched.thread)
  2880. continue;
  2881. /* only need recovery sched of the given job's ring
  2882. * or all rings (in the case @job is NULL)
  2883. * after above amdgpu_reset accomplished
  2884. */
  2885. if ((!job || job->base.sched == &ring->sched) && !r)
  2886. drm_sched_job_recovery(&ring->sched);
  2887. kthread_unpark(ring->sched.thread);
  2888. }
  2889. if (!amdgpu_device_has_dc_support(adev)) {
  2890. drm_helper_resume_force_mode(adev->ddev);
  2891. }
  2892. ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
  2893. if (r) {
  2894. /* bad news, how to tell it to userspace ? */
  2895. dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
  2896. amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
  2897. } else {
  2898. dev_info(adev->dev, "GPU reset(%d) succeeded!\n",atomic_read(&adev->gpu_reset_counter));
  2899. }
  2900. /*unlock kfd */
  2901. amdgpu_amdkfd_post_reset(adev);
  2902. amdgpu_vf_error_trans_all(adev);
  2903. adev->in_gpu_reset = 0;
  2904. mutex_unlock(&adev->lock_reset);
  2905. return r;
  2906. }
  2907. /**
  2908. * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
  2909. *
  2910. * @adev: amdgpu_device pointer
  2911. *
  2912. * Fetchs and stores in the driver the PCIE capabilities (gen speed
  2913. * and lanes) of the slot the device is in. Handles APUs and
  2914. * virtualized environments where PCIE config space may not be available.
  2915. */
  2916. static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
  2917. {
  2918. struct pci_dev *pdev;
  2919. enum pci_bus_speed speed_cap;
  2920. enum pcie_link_width link_width;
  2921. if (amdgpu_pcie_gen_cap)
  2922. adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
  2923. if (amdgpu_pcie_lane_cap)
  2924. adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
  2925. /* covers APUs as well */
  2926. if (pci_is_root_bus(adev->pdev->bus)) {
  2927. if (adev->pm.pcie_gen_mask == 0)
  2928. adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
  2929. if (adev->pm.pcie_mlw_mask == 0)
  2930. adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2931. return;
  2932. }
  2933. if (adev->pm.pcie_gen_mask == 0) {
  2934. /* asic caps */
  2935. pdev = adev->pdev;
  2936. speed_cap = pcie_get_speed_cap(pdev);
  2937. if (speed_cap == PCI_SPEED_UNKNOWN) {
  2938. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2939. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2940. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2941. } else {
  2942. if (speed_cap == PCIE_SPEED_16_0GT)
  2943. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2944. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2945. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
  2946. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
  2947. else if (speed_cap == PCIE_SPEED_8_0GT)
  2948. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2949. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2950. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2951. else if (speed_cap == PCIE_SPEED_5_0GT)
  2952. adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2953. CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
  2954. else
  2955. adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2956. }
  2957. /* platform caps */
  2958. pdev = adev->ddev->pdev->bus->self;
  2959. speed_cap = pcie_get_speed_cap(pdev);
  2960. if (speed_cap == PCI_SPEED_UNKNOWN) {
  2961. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2962. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
  2963. } else {
  2964. if (speed_cap == PCIE_SPEED_16_0GT)
  2965. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2966. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2967. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
  2968. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
  2969. else if (speed_cap == PCIE_SPEED_8_0GT)
  2970. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2971. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
  2972. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
  2973. else if (speed_cap == PCIE_SPEED_5_0GT)
  2974. adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
  2975. CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
  2976. else
  2977. adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
  2978. }
  2979. }
  2980. if (adev->pm.pcie_mlw_mask == 0) {
  2981. pdev = adev->ddev->pdev->bus->self;
  2982. link_width = pcie_get_width_cap(pdev);
  2983. if (link_width == PCIE_LNK_WIDTH_UNKNOWN) {
  2984. adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
  2985. } else {
  2986. switch (link_width) {
  2987. case PCIE_LNK_X32:
  2988. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
  2989. CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2990. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2991. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  2992. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  2993. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  2994. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  2995. break;
  2996. case PCIE_LNK_X16:
  2997. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
  2998. CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  2999. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3000. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3001. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3002. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3003. break;
  3004. case PCIE_LNK_X12:
  3005. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
  3006. CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3007. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3008. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3009. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3010. break;
  3011. case PCIE_LNK_X8:
  3012. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
  3013. CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3014. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3015. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3016. break;
  3017. case PCIE_LNK_X4:
  3018. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
  3019. CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3020. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3021. break;
  3022. case PCIE_LNK_X2:
  3023. adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
  3024. CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
  3025. break;
  3026. case PCIE_LNK_X1:
  3027. adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
  3028. break;
  3029. default:
  3030. break;
  3031. }
  3032. }
  3033. }
  3034. }