amdgpu_device.c 95 KB

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