amdgpu_device.c 91 KB

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