amdgpu_device.c 75 KB

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