amdgpu_device.c 89 KB

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