amdgpu_device.c 94 KB

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