amdgpu_device.c 74 KB

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