amdgpu_device.c 75 KB

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