xref: /linux/drivers/gpu/drm/amd/amdgpu/soc21.c (revision 298bb2b8903323f6ef2eab4819a2e477765f0ff1)
1 /*
2  * Copyright 2021 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 #include <linux/firmware.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 
28 #include "amdgpu.h"
29 #include "amdgpu_atombios.h"
30 #include "amdgpu_ih.h"
31 #include "amdgpu_uvd.h"
32 #include "amdgpu_vce.h"
33 #include "amdgpu_ucode.h"
34 #include "amdgpu_psp.h"
35 #include "amdgpu_smu.h"
36 #include "atom.h"
37 #include "amd_pcie.h"
38 
39 #include "gc/gc_11_0_0_offset.h"
40 #include "gc/gc_11_0_0_sh_mask.h"
41 #include "mp/mp_13_0_0_offset.h"
42 
43 #include "soc15.h"
44 #include "soc15_common.h"
45 #include "soc21.h"
46 #include "mxgpu_nv.h"
47 
48 static const struct amd_ip_funcs soc21_common_ip_funcs;
49 
50 /* SOC21 */
51 static const struct amdgpu_video_codec_info vcn_4_0_0_video_codecs_encode_array_vcn0[] = {
52 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 0)},
53 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 0)},
54 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
55 };
56 
57 static const struct amdgpu_video_codec_info vcn_4_0_0_video_codecs_encode_array_vcn1[] = {
58 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 0)},
59 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 0)},
60 };
61 
62 static const struct amdgpu_video_codecs vcn_4_0_0_video_codecs_encode_vcn0 = {
63 	.codec_count = ARRAY_SIZE(vcn_4_0_0_video_codecs_encode_array_vcn0),
64 	.codec_array = vcn_4_0_0_video_codecs_encode_array_vcn0,
65 };
66 
67 static const struct amdgpu_video_codecs vcn_4_0_0_video_codecs_encode_vcn1 = {
68 	.codec_count = ARRAY_SIZE(vcn_4_0_0_video_codecs_encode_array_vcn1),
69 	.codec_array = vcn_4_0_0_video_codecs_encode_array_vcn1,
70 };
71 
72 static const struct amdgpu_video_codec_info vcn_4_0_0_video_codecs_decode_array_vcn0[] = {
73 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
74 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
75 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 16384, 16384, 0)},
76 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
77 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
78 };
79 
80 static const struct amdgpu_video_codec_info vcn_4_0_0_video_codecs_decode_array_vcn1[] = {
81 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
82 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
83 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 16384, 16384, 0)},
84 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
85 };
86 
87 static const struct amdgpu_video_codecs vcn_4_0_0_video_codecs_decode_vcn0 = {
88 	.codec_count = ARRAY_SIZE(vcn_4_0_0_video_codecs_decode_array_vcn0),
89 	.codec_array = vcn_4_0_0_video_codecs_decode_array_vcn0,
90 };
91 
92 static const struct amdgpu_video_codecs vcn_4_0_0_video_codecs_decode_vcn1 = {
93 	.codec_count = ARRAY_SIZE(vcn_4_0_0_video_codecs_decode_array_vcn1),
94 	.codec_array = vcn_4_0_0_video_codecs_decode_array_vcn1,
95 };
96 
97 /* SRIOV SOC21, not const since data is controlled by host */
98 static struct amdgpu_video_codec_info sriov_vcn_4_0_0_video_codecs_encode_array_vcn0[] = {
99 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 0)},
100 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 0)},
101 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
102 };
103 
104 static struct amdgpu_video_codec_info sriov_vcn_4_0_0_video_codecs_encode_array_vcn1[] = {
105 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 0)},
106 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 0)},
107 };
108 
109 static struct amdgpu_video_codecs sriov_vcn_4_0_0_video_codecs_encode_vcn0 = {
110 	.codec_count = ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_encode_array_vcn0),
111 	.codec_array = sriov_vcn_4_0_0_video_codecs_encode_array_vcn0,
112 };
113 
114 static struct amdgpu_video_codecs sriov_vcn_4_0_0_video_codecs_encode_vcn1 = {
115 	.codec_count = ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_encode_array_vcn1),
116 	.codec_array = sriov_vcn_4_0_0_video_codecs_encode_array_vcn1,
117 };
118 
119 static struct amdgpu_video_codec_info sriov_vcn_4_0_0_video_codecs_decode_array_vcn0[] = {
120 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
121 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
122 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 16384, 16384, 0)},
123 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
124 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
125 };
126 
127 static struct amdgpu_video_codec_info sriov_vcn_4_0_0_video_codecs_decode_array_vcn1[] = {
128 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
129 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
130 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 16384, 16384, 0)},
131 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
132 };
133 
134 static struct amdgpu_video_codecs sriov_vcn_4_0_0_video_codecs_decode_vcn0 = {
135 	.codec_count = ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_decode_array_vcn0),
136 	.codec_array = sriov_vcn_4_0_0_video_codecs_decode_array_vcn0,
137 };
138 
139 static struct amdgpu_video_codecs sriov_vcn_4_0_0_video_codecs_decode_vcn1 = {
140 	.codec_count = ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_decode_array_vcn1),
141 	.codec_array = sriov_vcn_4_0_0_video_codecs_decode_array_vcn1,
142 };
143 
144 static const struct amdgpu_video_codec_info vcn_5_3_0_video_codecs_encode_array_vcn0[] = {
145         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 0)},
146         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 0)},
147         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
148 };
149 
150 static const struct amdgpu_video_codecs vcn_5_3_0_video_codecs_encode_vcn0 = {
151         .codec_count = ARRAY_SIZE(vcn_5_3_0_video_codecs_encode_array_vcn0),
152         .codec_array = vcn_5_3_0_video_codecs_encode_array_vcn0,
153 };
154 
155 static const struct amdgpu_video_codec_info vcn_5_3_0_video_codecs_decode_array_vcn0[] = {
156         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4096, 52)},
157         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
158         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 16384, 16384, 0)},
159         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
160         {codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1, 8192, 4352, 0)},
161 };
162 
163 static const struct amdgpu_video_codecs vcn_5_3_0_video_codecs_decode_vcn0 = {
164         .codec_count = ARRAY_SIZE(vcn_5_3_0_video_codecs_decode_array_vcn0),
165         .codec_array = vcn_5_3_0_video_codecs_decode_array_vcn0,
166 };
167 
168 
169 static int soc21_query_video_codecs(struct amdgpu_device *adev, bool encode,
170 				 const struct amdgpu_video_codecs **codecs)
171 {
172 	if (adev->vcn.num_vcn_inst == hweight8(adev->vcn.harvest_config))
173 		return -EINVAL;
174 
175 	switch (amdgpu_ip_version(adev, UVD_HWIP, 0)) {
176 	case IP_VERSION(4, 0, 0):
177 	case IP_VERSION(4, 0, 2):
178 	case IP_VERSION(4, 0, 4):
179 	case IP_VERSION(4, 0, 5):
180 		if (amdgpu_sriov_vf(adev)) {
181 			if ((adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0) ||
182 			!amdgpu_sriov_is_av1_support(adev)) {
183 				if (encode)
184 					*codecs = &sriov_vcn_4_0_0_video_codecs_encode_vcn1;
185 				else
186 					*codecs = &sriov_vcn_4_0_0_video_codecs_decode_vcn1;
187 			} else {
188 				if (encode)
189 					*codecs = &sriov_vcn_4_0_0_video_codecs_encode_vcn0;
190 				else
191 					*codecs = &sriov_vcn_4_0_0_video_codecs_decode_vcn0;
192 			}
193 		} else {
194 			if ((adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)) {
195 				if (encode)
196 					*codecs = &vcn_4_0_0_video_codecs_encode_vcn1;
197 				else
198 					*codecs = &vcn_4_0_0_video_codecs_decode_vcn1;
199 			} else {
200 				if (encode)
201 					*codecs = &vcn_4_0_0_video_codecs_encode_vcn0;
202 				else
203 					*codecs = &vcn_4_0_0_video_codecs_decode_vcn0;
204 			}
205 		}
206 		return 0;
207 	case IP_VERSION(4, 0, 6):
208 		if (encode)
209 			*codecs = &vcn_4_0_0_video_codecs_encode_vcn0;
210 		else
211 			*codecs = &vcn_4_0_0_video_codecs_decode_vcn0;
212 		return 0;
213 	case IP_VERSION(5, 3, 0):
214 		if (encode)
215 			*codecs = &vcn_5_3_0_video_codecs_encode_vcn0;
216 		else
217 			*codecs = &vcn_5_3_0_video_codecs_decode_vcn0;
218 		return 0;
219 	default:
220 		return -EINVAL;
221 	}
222 }
223 
224 static u32 soc21_didt_rreg(struct amdgpu_device *adev, u32 reg)
225 {
226 	unsigned long flags, address, data;
227 	u32 r;
228 
229 	address = SOC15_REG_OFFSET(GC, 0, regDIDT_IND_INDEX);
230 	data = SOC15_REG_OFFSET(GC, 0, regDIDT_IND_DATA);
231 
232 	spin_lock_irqsave(&adev->reg.didt.lock, flags);
233 	WREG32(address, (reg));
234 	r = RREG32(data);
235 	spin_unlock_irqrestore(&adev->reg.didt.lock, flags);
236 	return r;
237 }
238 
239 static void soc21_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
240 {
241 	unsigned long flags, address, data;
242 
243 	address = SOC15_REG_OFFSET(GC, 0, regDIDT_IND_INDEX);
244 	data = SOC15_REG_OFFSET(GC, 0, regDIDT_IND_DATA);
245 
246 	spin_lock_irqsave(&adev->reg.didt.lock, flags);
247 	WREG32(address, (reg));
248 	WREG32(data, (v));
249 	spin_unlock_irqrestore(&adev->reg.didt.lock, flags);
250 }
251 
252 static u32 soc21_get_config_memsize(struct amdgpu_device *adev)
253 {
254 	return adev->nbio.funcs->get_memsize(adev);
255 }
256 
257 static u32 soc21_get_xclk(struct amdgpu_device *adev)
258 {
259 	u32 reference_clock = adev->clock.spll.reference_freq;
260 
261 	/* reference clock is actually 99.81 Mhz rather than 100 Mhz */
262 	if ((adev->flags & AMD_IS_APU) && reference_clock == 10000)
263 		return 9981;
264 
265 	return reference_clock;
266 }
267 
268 
269 void soc21_grbm_select(struct amdgpu_device *adev,
270 		     u32 me, u32 pipe, u32 queue, u32 vmid)
271 {
272 	u32 grbm_gfx_cntl = 0;
273 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
274 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
275 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
276 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
277 
278 	WREG32_SOC15(GC, 0, regGRBM_GFX_CNTL, grbm_gfx_cntl);
279 }
280 
281 static bool soc21_read_disabled_bios(struct amdgpu_device *adev)
282 {
283 	/* todo */
284 	return false;
285 }
286 
287 static struct soc15_allowed_register_entry soc21_allowed_read_registers[] = {
288 	{ SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS)},
289 	{ SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS2)},
290 	{ SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS_SE0)},
291 	{ SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS_SE1)},
292 	{ SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS_SE2)},
293 	{ SOC15_REG_ENTRY(GC, 0, regGRBM_STATUS_SE3)},
294 	{ SOC15_REG_ENTRY(SDMA0, 0, regSDMA0_STATUS_REG)},
295 	{ SOC15_REG_ENTRY(SDMA1, 0, regSDMA1_STATUS_REG)},
296 	{ SOC15_REG_ENTRY(GC, 0, regCP_STAT)},
297 	{ SOC15_REG_ENTRY(GC, 0, regCP_STALLED_STAT1)},
298 	{ SOC15_REG_ENTRY(GC, 0, regCP_STALLED_STAT2)},
299 	{ SOC15_REG_ENTRY(GC, 0, regCP_STALLED_STAT3)},
300 	{ SOC15_REG_ENTRY(GC, 0, regCP_CPF_BUSY_STAT)},
301 	{ SOC15_REG_ENTRY(GC, 0, regCP_CPF_STALLED_STAT1)},
302 	{ SOC15_REG_ENTRY(GC, 0, regCP_CPF_STATUS)},
303 	{ SOC15_REG_ENTRY(GC, 0, regCP_CPC_BUSY_STAT)},
304 	{ SOC15_REG_ENTRY(GC, 0, regCP_CPC_STALLED_STAT1)},
305 	{ SOC15_REG_ENTRY(GC, 0, regCP_CPC_STATUS)},
306 	{ SOC15_REG_ENTRY(GC, 0, regGB_ADDR_CONFIG)},
307 };
308 
309 static uint32_t soc21_get_register_value(struct amdgpu_device *adev,
310 				      bool indexed, u32 se_num,
311 				      u32 sh_num, u32 reg_offset)
312 {
313 	if (indexed) {
314 		return amdgpu_read_indexed_register(adev, se_num, sh_num, reg_offset);
315 	} else {
316 		if (reg_offset == SOC15_REG_OFFSET(GC, 0, regGB_ADDR_CONFIG) && adev->gfx.config.gb_addr_config)
317 			return adev->gfx.config.gb_addr_config;
318 		return RREG32(reg_offset);
319 	}
320 }
321 
322 static int soc21_read_register(struct amdgpu_device *adev, u32 se_num,
323 			    u32 sh_num, u32 reg_offset, u32 *value)
324 {
325 	uint32_t i;
326 	struct soc15_allowed_register_entry  *en;
327 
328 	*value = 0;
329 	for (i = 0; i < ARRAY_SIZE(soc21_allowed_read_registers); i++) {
330 		en = &soc21_allowed_read_registers[i];
331 		if (!adev->reg_offset[en->hwip][en->inst])
332 			continue;
333 		else if (reg_offset != (adev->reg_offset[en->hwip][en->inst][en->seg]
334 					+ en->reg_offset))
335 			continue;
336 
337 		*value = soc21_get_register_value(adev,
338 					       soc21_allowed_read_registers[i].grbm_indexed,
339 					       se_num, sh_num, reg_offset);
340 		return 0;
341 	}
342 	return -EINVAL;
343 }
344 
345 #if 0
346 static int soc21_asic_mode1_reset(struct amdgpu_device *adev)
347 {
348 	u32 i;
349 	int ret = 0;
350 
351 	amdgpu_atombios_scratch_regs_engine_hung(adev, true);
352 
353 	/* disable BM */
354 	pci_clear_master(adev->pdev);
355 
356 	amdgpu_device_cache_pci_state(adev->pdev);
357 
358 	if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
359 		dev_info(adev->dev, "GPU smu mode1 reset\n");
360 		ret = amdgpu_dpm_mode1_reset(adev);
361 	} else {
362 		dev_info(adev->dev, "GPU psp mode1 reset\n");
363 		ret = psp_gpu_reset(adev);
364 	}
365 
366 	if (ret)
367 		dev_err(adev->dev, "GPU mode1 reset failed\n");
368 	amdgpu_device_load_pci_state(adev->pdev);
369 
370 	/* wait for asic to come out of reset */
371 	for (i = 0; i < adev->usec_timeout; i++) {
372 		u32 memsize = adev->nbio.funcs->get_memsize(adev);
373 
374 		if (memsize != 0xffffffff)
375 			break;
376 		udelay(1);
377 	}
378 
379 	amdgpu_atombios_scratch_regs_engine_hung(adev, false);
380 
381 	return ret;
382 }
383 #endif
384 
385 static enum amd_reset_method
386 soc21_asic_reset_method(struct amdgpu_device *adev)
387 {
388 	if (amdgpu_reset_method == AMD_RESET_METHOD_MODE1 ||
389 	    amdgpu_reset_method == AMD_RESET_METHOD_MODE2 ||
390 	    amdgpu_reset_method == AMD_RESET_METHOD_BACO)
391 		return amdgpu_reset_method;
392 
393 	if (amdgpu_reset_method != -1)
394 		dev_warn(adev->dev, "Specified reset method:%d isn't supported, using AUTO instead.\n",
395 				  amdgpu_reset_method);
396 
397 	switch (amdgpu_ip_version(adev, MP1_HWIP, 0)) {
398 	case IP_VERSION(13, 0, 0):
399 	case IP_VERSION(13, 0, 7):
400 	case IP_VERSION(13, 0, 10):
401 		return AMD_RESET_METHOD_MODE1;
402 	case IP_VERSION(13, 0, 4):
403 	case IP_VERSION(13, 0, 11):
404 	case IP_VERSION(14, 0, 0):
405 	case IP_VERSION(14, 0, 1):
406 	case IP_VERSION(14, 0, 4):
407 	case IP_VERSION(14, 0, 5):
408 	case IP_VERSION(15, 0, 0):
409 	case IP_VERSION(15, 0, 9):
410 		return AMD_RESET_METHOD_MODE2;
411 	default:
412 		if (amdgpu_dpm_is_baco_supported(adev))
413 			return AMD_RESET_METHOD_BACO;
414 		else
415 			return AMD_RESET_METHOD_MODE1;
416 	}
417 }
418 
419 static int soc21_asic_reset(struct amdgpu_device *adev)
420 {
421 	int ret = 0;
422 
423 	switch (soc21_asic_reset_method(adev)) {
424 	case AMD_RESET_METHOD_PCI:
425 		dev_info(adev->dev, "PCI reset\n");
426 		ret = amdgpu_device_pci_reset(adev);
427 		break;
428 	case AMD_RESET_METHOD_BACO:
429 		dev_info(adev->dev, "BACO reset\n");
430 		ret = amdgpu_dpm_baco_reset(adev);
431 		break;
432 	case AMD_RESET_METHOD_MODE2:
433 		dev_info(adev->dev, "MODE2 reset\n");
434 		ret = amdgpu_dpm_mode2_reset(adev);
435 		break;
436 	default:
437 		dev_info(adev->dev, "MODE1 reset\n");
438 		ret = amdgpu_device_mode1_reset(adev);
439 		break;
440 	}
441 
442 	return ret;
443 }
444 
445 static int soc21_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
446 {
447 	/* todo */
448 	return 0;
449 }
450 
451 static int soc21_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
452 {
453 	/* todo */
454 	return 0;
455 }
456 
457 const struct amdgpu_ip_block_version soc21_common_ip_block = {
458 	.type = AMD_IP_BLOCK_TYPE_COMMON,
459 	.major = 1,
460 	.minor = 0,
461 	.rev = 0,
462 	.funcs = &soc21_common_ip_funcs,
463 };
464 
465 static bool soc21_need_full_reset(struct amdgpu_device *adev)
466 {
467 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
468 	case IP_VERSION(11, 0, 0):
469 	case IP_VERSION(11, 0, 2):
470 	case IP_VERSION(11, 0, 3):
471 	default:
472 		return true;
473 	}
474 }
475 
476 static bool soc21_need_reset_on_init(struct amdgpu_device *adev)
477 {
478 	u32 sol_reg;
479 
480 	if (adev->flags & AMD_IS_APU)
481 		return false;
482 
483 	/* Check sOS sign of life register to confirm sys driver and sOS
484 	 * are already been loaded.
485 	 */
486 	sol_reg = RREG32_SOC15(MP0, 0, regMP0_SMN_C2PMSG_81);
487 	if (sol_reg)
488 		return true;
489 
490 	return false;
491 }
492 
493 static void soc21_init_doorbell_index(struct amdgpu_device *adev)
494 {
495 	adev->doorbell_index.kiq = AMDGPU_NAVI10_DOORBELL_KIQ;
496 	adev->doorbell_index.mec_ring0 = AMDGPU_NAVI10_DOORBELL_MEC_RING0;
497 	adev->doorbell_index.mec_ring1 = AMDGPU_NAVI10_DOORBELL_MEC_RING1;
498 	adev->doorbell_index.mec_ring2 = AMDGPU_NAVI10_DOORBELL_MEC_RING2;
499 	adev->doorbell_index.mec_ring3 = AMDGPU_NAVI10_DOORBELL_MEC_RING3;
500 	adev->doorbell_index.mec_ring4 = AMDGPU_NAVI10_DOORBELL_MEC_RING4;
501 	adev->doorbell_index.mec_ring5 = AMDGPU_NAVI10_DOORBELL_MEC_RING5;
502 	adev->doorbell_index.mec_ring6 = AMDGPU_NAVI10_DOORBELL_MEC_RING6;
503 	adev->doorbell_index.mec_ring7 = AMDGPU_NAVI10_DOORBELL_MEC_RING7;
504 	adev->doorbell_index.userqueue_start = AMDGPU_NAVI10_DOORBELL_USERQUEUE_START;
505 	adev->doorbell_index.userqueue_end = AMDGPU_NAVI10_DOORBELL_USERQUEUE_END;
506 	adev->doorbell_index.gfx_ring0 = AMDGPU_NAVI10_DOORBELL_GFX_RING0;
507 	adev->doorbell_index.gfx_ring1 = AMDGPU_NAVI10_DOORBELL_GFX_RING1;
508 	adev->doorbell_index.gfx_userqueue_start =
509 		AMDGPU_NAVI10_DOORBELL_GFX_USERQUEUE_START;
510 	adev->doorbell_index.gfx_userqueue_end =
511 		AMDGPU_NAVI10_DOORBELL_GFX_USERQUEUE_END;
512 	adev->doorbell_index.mes_ring0 = AMDGPU_NAVI10_DOORBELL_MES_RING0;
513 	adev->doorbell_index.mes_ring1 = AMDGPU_NAVI10_DOORBELL_MES_RING1;
514 	adev->doorbell_index.sdma_engine[0] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE0;
515 	adev->doorbell_index.sdma_engine[1] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE1;
516 	adev->doorbell_index.ih = AMDGPU_NAVI10_DOORBELL_IH;
517 	adev->doorbell_index.vcn.vcn_ring0_1 = AMDGPU_NAVI10_DOORBELL64_VCN0_1;
518 	adev->doorbell_index.vcn.vcn_ring2_3 = AMDGPU_NAVI10_DOORBELL64_VCN2_3;
519 	adev->doorbell_index.vcn.vcn_ring4_5 = AMDGPU_NAVI10_DOORBELL64_VCN4_5;
520 	adev->doorbell_index.vcn.vcn_ring6_7 = AMDGPU_NAVI10_DOORBELL64_VCN6_7;
521 	adev->doorbell_index.vpe_ring = AMDGPU_NAVI10_DOORBELL64_VPE;
522 	adev->doorbell_index.first_non_cp = AMDGPU_NAVI10_DOORBELL64_FIRST_NON_CP;
523 	adev->doorbell_index.last_non_cp = AMDGPU_NAVI10_DOORBELL64_LAST_NON_CP;
524 
525 	adev->doorbell_index.max_assignment = AMDGPU_NAVI10_DOORBELL_MAX_ASSIGNMENT << 1;
526 	adev->doorbell_index.sdma_doorbell_range = 20;
527 }
528 
529 static int soc21_update_umd_stable_pstate(struct amdgpu_device *adev,
530 					  bool enter)
531 {
532 	if (enter)
533 		amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
534 	else
535 		amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
536 
537 	if (adev->gfx.funcs->update_perfmon_mgcg)
538 		adev->gfx.funcs->update_perfmon_mgcg(adev, !enter);
539 
540 	return 0;
541 }
542 
543 static const struct amdgpu_asic_funcs soc21_asic_funcs = {
544 	.read_disabled_bios = &soc21_read_disabled_bios,
545 	.read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom,
546 	.read_register = &soc21_read_register,
547 	.reset = &soc21_asic_reset,
548 	.reset_method = &soc21_asic_reset_method,
549 	.get_xclk = &soc21_get_xclk,
550 	.set_uvd_clocks = &soc21_set_uvd_clocks,
551 	.set_vce_clocks = &soc21_set_vce_clocks,
552 	.get_config_memsize = &soc21_get_config_memsize,
553 	.init_doorbell_index = &soc21_init_doorbell_index,
554 	.need_full_reset = &soc21_need_full_reset,
555 	.need_reset_on_init = &soc21_need_reset_on_init,
556 	.get_pcie_replay_count = &amdgpu_nbio_get_pcie_replay_count,
557 	.supports_baco = &amdgpu_dpm_is_baco_supported,
558 	.query_video_codecs = &soc21_query_video_codecs,
559 	.update_umd_stable_pstate = &soc21_update_umd_stable_pstate,
560 };
561 
562 static int soc21_common_early_init(struct amdgpu_ip_block *ip_block)
563 {
564 	struct amdgpu_device *adev = ip_block->adev;
565 
566 	adev->nbio.funcs->set_reg_remap(adev);
567 	adev->reg.pcie.rreg = &amdgpu_device_indirect_rreg;
568 	adev->reg.pcie.wreg = &amdgpu_device_indirect_wreg;
569 	adev->reg.pcie.rreg64 = &amdgpu_device_indirect_rreg64;
570 	adev->reg.pcie.wreg64 = &amdgpu_device_indirect_wreg64;
571 	adev->reg.pcie.port_rreg = &amdgpu_device_pcie_port_rreg;
572 	adev->reg.pcie.port_wreg = &amdgpu_device_pcie_port_wreg;
573 
574 	adev->reg.didt.rreg = &soc21_didt_rreg;
575 	adev->reg.didt.wreg = &soc21_didt_wreg;
576 
577 	adev->asic_funcs = &soc21_asic_funcs;
578 
579 	adev->rev_id = amdgpu_device_get_rev_id(adev);
580 	adev->external_rev_id = 0xff;
581 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
582 	case IP_VERSION(11, 0, 0):
583 		adev->cg_flags = AMD_CG_SUPPORT_GFX_CGCG |
584 			AMD_CG_SUPPORT_GFX_CGLS |
585 #if 0
586 			AMD_CG_SUPPORT_GFX_3D_CGCG |
587 			AMD_CG_SUPPORT_GFX_3D_CGLS |
588 #endif
589 			AMD_CG_SUPPORT_GFX_MGCG |
590 			AMD_CG_SUPPORT_REPEATER_FGCG |
591 			AMD_CG_SUPPORT_GFX_FGCG |
592 			AMD_CG_SUPPORT_GFX_PERF_CLK |
593 			AMD_CG_SUPPORT_VCN_MGCG |
594 			AMD_CG_SUPPORT_JPEG_MGCG |
595 			AMD_CG_SUPPORT_ATHUB_MGCG |
596 			AMD_CG_SUPPORT_ATHUB_LS |
597 			AMD_CG_SUPPORT_MC_MGCG |
598 			AMD_CG_SUPPORT_MC_LS |
599 			AMD_CG_SUPPORT_IH_CG |
600 			AMD_CG_SUPPORT_HDP_SD;
601 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
602 			AMD_PG_SUPPORT_VCN_DPG |
603 			AMD_PG_SUPPORT_JPEG |
604 			AMD_PG_SUPPORT_ATHUB |
605 			AMD_PG_SUPPORT_MMHUB;
606 		adev->external_rev_id = adev->rev_id + 0x1; // TODO: need update
607 		break;
608 	case IP_VERSION(11, 0, 2):
609 		adev->cg_flags =
610 			AMD_CG_SUPPORT_GFX_CGCG |
611 			AMD_CG_SUPPORT_GFX_CGLS |
612 			AMD_CG_SUPPORT_REPEATER_FGCG |
613 			AMD_CG_SUPPORT_VCN_MGCG |
614 			AMD_CG_SUPPORT_JPEG_MGCG |
615 			AMD_CG_SUPPORT_ATHUB_MGCG |
616 			AMD_CG_SUPPORT_ATHUB_LS |
617 			AMD_CG_SUPPORT_IH_CG |
618 			AMD_CG_SUPPORT_HDP_SD;
619 		adev->pg_flags =
620 			AMD_PG_SUPPORT_VCN |
621 			AMD_PG_SUPPORT_VCN_DPG |
622 			AMD_PG_SUPPORT_JPEG |
623 			AMD_PG_SUPPORT_ATHUB |
624 			AMD_PG_SUPPORT_MMHUB;
625 		adev->external_rev_id = adev->rev_id + 0x10;
626 		break;
627 	case IP_VERSION(11, 0, 1):
628 		adev->cg_flags =
629 			AMD_CG_SUPPORT_GFX_CGCG |
630 			AMD_CG_SUPPORT_GFX_CGLS |
631 			AMD_CG_SUPPORT_GFX_MGCG |
632 			AMD_CG_SUPPORT_GFX_FGCG |
633 			AMD_CG_SUPPORT_REPEATER_FGCG |
634 			AMD_CG_SUPPORT_GFX_PERF_CLK |
635 			AMD_CG_SUPPORT_MC_MGCG |
636 			AMD_CG_SUPPORT_MC_LS |
637 			AMD_CG_SUPPORT_HDP_MGCG |
638 			AMD_CG_SUPPORT_HDP_LS |
639 			AMD_CG_SUPPORT_ATHUB_MGCG |
640 			AMD_CG_SUPPORT_ATHUB_LS |
641 			AMD_CG_SUPPORT_IH_CG |
642 			AMD_CG_SUPPORT_BIF_MGCG |
643 			AMD_CG_SUPPORT_BIF_LS |
644 			AMD_CG_SUPPORT_VCN_MGCG |
645 			AMD_CG_SUPPORT_JPEG_MGCG;
646 		adev->pg_flags =
647 			AMD_PG_SUPPORT_GFX_PG |
648 			AMD_PG_SUPPORT_VCN |
649 			AMD_PG_SUPPORT_VCN_DPG |
650 			AMD_PG_SUPPORT_JPEG;
651 		adev->external_rev_id = adev->rev_id + 0x1;
652 		break;
653 	case IP_VERSION(11, 0, 3):
654 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
655 			AMD_CG_SUPPORT_JPEG_MGCG |
656 			AMD_CG_SUPPORT_GFX_CGCG |
657 			AMD_CG_SUPPORT_GFX_CGLS |
658 			AMD_CG_SUPPORT_REPEATER_FGCG |
659 			AMD_CG_SUPPORT_GFX_MGCG |
660 			AMD_CG_SUPPORT_HDP_SD |
661 			AMD_CG_SUPPORT_ATHUB_MGCG |
662 			AMD_CG_SUPPORT_ATHUB_LS;
663 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
664 			AMD_PG_SUPPORT_VCN_DPG |
665 			AMD_PG_SUPPORT_JPEG;
666 		adev->external_rev_id = adev->rev_id + 0x20;
667 		break;
668 	case IP_VERSION(11, 0, 4):
669 		adev->cg_flags =
670 			AMD_CG_SUPPORT_GFX_CGCG |
671 			AMD_CG_SUPPORT_GFX_CGLS |
672 			AMD_CG_SUPPORT_GFX_MGCG |
673 			AMD_CG_SUPPORT_GFX_FGCG |
674 			AMD_CG_SUPPORT_REPEATER_FGCG |
675 			AMD_CG_SUPPORT_GFX_PERF_CLK |
676 			AMD_CG_SUPPORT_MC_MGCG |
677 			AMD_CG_SUPPORT_MC_LS |
678 			AMD_CG_SUPPORT_HDP_MGCG |
679 			AMD_CG_SUPPORT_HDP_LS |
680 			AMD_CG_SUPPORT_ATHUB_MGCG |
681 			AMD_CG_SUPPORT_ATHUB_LS |
682 			AMD_CG_SUPPORT_IH_CG |
683 			AMD_CG_SUPPORT_BIF_MGCG |
684 			AMD_CG_SUPPORT_BIF_LS |
685 			AMD_CG_SUPPORT_VCN_MGCG |
686 			AMD_CG_SUPPORT_JPEG_MGCG;
687 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
688 			AMD_PG_SUPPORT_VCN_DPG |
689 			AMD_PG_SUPPORT_GFX_PG |
690 			AMD_PG_SUPPORT_JPEG;
691 		adev->external_rev_id = adev->rev_id + 0x80;
692 		break;
693 	case IP_VERSION(11, 5, 0):
694 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
695 			AMD_CG_SUPPORT_JPEG_MGCG |
696 			AMD_CG_SUPPORT_GFX_CGCG |
697 			AMD_CG_SUPPORT_GFX_CGLS |
698 			AMD_CG_SUPPORT_GFX_MGCG |
699 			AMD_CG_SUPPORT_GFX_FGCG |
700 			AMD_CG_SUPPORT_REPEATER_FGCG |
701 			AMD_CG_SUPPORT_GFX_PERF_CLK	|
702 			AMD_CG_SUPPORT_GFX_3D_CGCG |
703 			AMD_CG_SUPPORT_GFX_3D_CGLS	|
704 			AMD_CG_SUPPORT_MC_MGCG |
705 			AMD_CG_SUPPORT_MC_LS |
706 			AMD_CG_SUPPORT_HDP_LS |
707 			AMD_CG_SUPPORT_HDP_DS |
708 			AMD_CG_SUPPORT_HDP_SD |
709 			AMD_CG_SUPPORT_ATHUB_MGCG |
710 			AMD_CG_SUPPORT_ATHUB_LS |
711 			AMD_CG_SUPPORT_IH_CG |
712 			AMD_CG_SUPPORT_BIF_MGCG |
713 			AMD_CG_SUPPORT_BIF_LS;
714 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG |
715 			AMD_PG_SUPPORT_JPEG_DPG |
716 			AMD_PG_SUPPORT_VCN |
717 			AMD_PG_SUPPORT_JPEG |
718 			AMD_PG_SUPPORT_GFX_PG;
719 		if (adev->rev_id == 0)
720 			adev->external_rev_id = 0x1;
721 		else
722 			adev->external_rev_id = adev->rev_id + 0x10;
723 		break;
724 	case IP_VERSION(11, 5, 1):
725 		adev->cg_flags =
726 			AMD_CG_SUPPORT_GFX_CGCG |
727 			AMD_CG_SUPPORT_GFX_CGLS |
728 			AMD_CG_SUPPORT_GFX_MGCG |
729 			AMD_CG_SUPPORT_GFX_FGCG |
730 			AMD_CG_SUPPORT_REPEATER_FGCG |
731 			AMD_CG_SUPPORT_GFX_PERF_CLK	|
732 			AMD_CG_SUPPORT_GFX_3D_CGCG |
733 			AMD_CG_SUPPORT_GFX_3D_CGLS	|
734 			AMD_CG_SUPPORT_MC_MGCG |
735 			AMD_CG_SUPPORT_MC_LS |
736 			AMD_CG_SUPPORT_HDP_LS |
737 			AMD_CG_SUPPORT_HDP_DS |
738 			AMD_CG_SUPPORT_HDP_SD |
739 			AMD_CG_SUPPORT_ATHUB_MGCG |
740 			AMD_CG_SUPPORT_ATHUB_LS |
741 			AMD_CG_SUPPORT_IH_CG |
742 			AMD_CG_SUPPORT_BIF_MGCG |
743 			AMD_CG_SUPPORT_BIF_LS |
744 			AMD_CG_SUPPORT_VCN_MGCG |
745 			AMD_CG_SUPPORT_JPEG_MGCG;
746 		adev->pg_flags =
747 			AMD_PG_SUPPORT_GFX_PG |
748 			AMD_PG_SUPPORT_VCN |
749 			AMD_PG_SUPPORT_VCN_DPG |
750 			AMD_PG_SUPPORT_JPEG;
751 		adev->external_rev_id = adev->rev_id + 0xc1;
752 		break;
753 	case IP_VERSION(11, 5, 2):
754 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
755 			AMD_CG_SUPPORT_JPEG_MGCG |
756 			AMD_CG_SUPPORT_GFX_CGCG |
757 			AMD_CG_SUPPORT_GFX_CGLS |
758 			AMD_CG_SUPPORT_GFX_MGCG |
759 			AMD_CG_SUPPORT_GFX_FGCG |
760 			AMD_CG_SUPPORT_REPEATER_FGCG |
761 			AMD_CG_SUPPORT_GFX_PERF_CLK	|
762 			AMD_CG_SUPPORT_GFX_3D_CGCG |
763 			AMD_CG_SUPPORT_GFX_3D_CGLS	|
764 			AMD_CG_SUPPORT_MC_MGCG |
765 			AMD_CG_SUPPORT_MC_LS |
766 			AMD_CG_SUPPORT_HDP_LS |
767 			AMD_CG_SUPPORT_HDP_DS |
768 			AMD_CG_SUPPORT_HDP_SD |
769 			AMD_CG_SUPPORT_ATHUB_MGCG |
770 			AMD_CG_SUPPORT_ATHUB_LS |
771 			AMD_CG_SUPPORT_IH_CG |
772 			AMD_CG_SUPPORT_BIF_MGCG |
773 			AMD_CG_SUPPORT_BIF_LS;
774 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG |
775 			AMD_PG_SUPPORT_VCN |
776 			AMD_PG_SUPPORT_JPEG_DPG |
777 			AMD_PG_SUPPORT_JPEG |
778 			AMD_PG_SUPPORT_GFX_PG;
779 		adev->external_rev_id = adev->rev_id + 0x40;
780 		break;
781 	case IP_VERSION(11, 5, 3):
782 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
783 			AMD_CG_SUPPORT_JPEG_MGCG |
784 			AMD_CG_SUPPORT_GFX_CGCG |
785 			AMD_CG_SUPPORT_GFX_CGLS |
786 			AMD_CG_SUPPORT_GFX_MGCG |
787 			AMD_CG_SUPPORT_GFX_FGCG |
788 			AMD_CG_SUPPORT_REPEATER_FGCG |
789 			AMD_CG_SUPPORT_GFX_PERF_CLK |
790 			AMD_CG_SUPPORT_GFX_3D_CGCG |
791 			AMD_CG_SUPPORT_GFX_3D_CGLS |
792 			AMD_CG_SUPPORT_MC_MGCG |
793 			AMD_CG_SUPPORT_MC_LS |
794 			AMD_CG_SUPPORT_HDP_LS |
795 			AMD_CG_SUPPORT_HDP_DS |
796 			AMD_CG_SUPPORT_HDP_SD |
797 			AMD_CG_SUPPORT_ATHUB_MGCG |
798 			AMD_CG_SUPPORT_ATHUB_LS |
799 			AMD_CG_SUPPORT_IH_CG |
800 			AMD_CG_SUPPORT_BIF_MGCG |
801 			AMD_CG_SUPPORT_BIF_LS;
802 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG |
803 			AMD_PG_SUPPORT_VCN |
804 			AMD_PG_SUPPORT_JPEG_DPG |
805 			AMD_PG_SUPPORT_JPEG |
806 			AMD_PG_SUPPORT_GFX_PG;
807 		adev->external_rev_id = adev->rev_id + 0x50;
808 		break;
809 	case IP_VERSION(11, 5, 4):
810 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
811 			AMD_CG_SUPPORT_JPEG_MGCG |
812 			AMD_CG_SUPPORT_GFX_CGCG |
813 			AMD_CG_SUPPORT_GFX_CGLS |
814 			AMD_CG_SUPPORT_GFX_MGCG |
815 			AMD_CG_SUPPORT_GFX_FGCG |
816 			AMD_CG_SUPPORT_REPEATER_FGCG |
817 			AMD_CG_SUPPORT_GFX_PERF_CLK |
818 			AMD_CG_SUPPORT_GFX_3D_CGCG |
819 			AMD_CG_SUPPORT_GFX_3D_CGLS |
820 			AMD_CG_SUPPORT_MC_MGCG |
821 			AMD_CG_SUPPORT_MC_LS |
822 			AMD_CG_SUPPORT_HDP_LS |
823 			AMD_CG_SUPPORT_HDP_DS |
824 			AMD_CG_SUPPORT_HDP_SD |
825 			AMD_CG_SUPPORT_ATHUB_MGCG |
826 			AMD_CG_SUPPORT_ATHUB_LS |
827 			AMD_CG_SUPPORT_IH_CG |
828 			AMD_CG_SUPPORT_BIF_MGCG |
829 			AMD_CG_SUPPORT_BIF_LS;
830 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG |
831 			AMD_PG_SUPPORT_VCN |
832 			AMD_PG_SUPPORT_JPEG_DPG |
833 			AMD_PG_SUPPORT_JPEG |
834 			AMD_PG_SUPPORT_GFX_PG;
835 		adev->external_rev_id = adev->rev_id + 0x1;
836                break;
837 	case IP_VERSION(11, 5, 6):
838 		adev->cg_flags = 0;
839 		adev->pg_flags = 0;
840 		adev->external_rev_id = adev->rev_id + 0xd0;
841 		break;
842 	case IP_VERSION(11, 7, 0):
843 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
844 			AMD_CG_SUPPORT_JPEG_MGCG |
845 			AMD_CG_SUPPORT_GFX_CGCG |
846 			AMD_CG_SUPPORT_GFX_CGLS |
847 			AMD_CG_SUPPORT_GFX_MGCG |
848 			AMD_CG_SUPPORT_GFX_FGCG |
849 			AMD_CG_SUPPORT_REPEATER_FGCG |
850 			AMD_CG_SUPPORT_GFX_PERF_CLK |
851 			AMD_CG_SUPPORT_GFX_3D_CGCG |
852 			AMD_CG_SUPPORT_GFX_3D_CGLS |
853 			AMD_CG_SUPPORT_MC_MGCG |
854 			AMD_CG_SUPPORT_MC_LS |
855 			AMD_CG_SUPPORT_HDP_LS |
856 			AMD_CG_SUPPORT_HDP_DS |
857 			AMD_CG_SUPPORT_HDP_SD |
858 			AMD_CG_SUPPORT_ATHUB_MGCG |
859 			AMD_CG_SUPPORT_ATHUB_LS |
860 			AMD_CG_SUPPORT_IH_CG |
861 			AMD_CG_SUPPORT_BIF_MGCG |
862 			AMD_CG_SUPPORT_BIF_LS;
863 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG |
864 			AMD_PG_SUPPORT_VCN |
865 			AMD_PG_SUPPORT_JPEG |
866 			AMD_PG_SUPPORT_GFX_PG;
867 		adev->external_rev_id = adev->rev_id + 0xF;
868 		break;
869 	case IP_VERSION(11, 7, 1):
870 		adev->cg_flags = AMD_CG_SUPPORT_VCN_MGCG |
871 			AMD_CG_SUPPORT_JPEG_MGCG |
872 			AMD_CG_SUPPORT_GFX_CGCG |
873 			AMD_CG_SUPPORT_GFX_CGLS |
874 			AMD_CG_SUPPORT_GFX_MGCG |
875 			AMD_CG_SUPPORT_GFX_FGCG |
876 			AMD_CG_SUPPORT_REPEATER_FGCG |
877 			AMD_CG_SUPPORT_GFX_PERF_CLK |
878 			AMD_CG_SUPPORT_GFX_3D_CGCG |
879 			AMD_CG_SUPPORT_GFX_3D_CGLS |
880 			AMD_CG_SUPPORT_MC_MGCG |
881 			AMD_CG_SUPPORT_MC_LS |
882 			AMD_CG_SUPPORT_HDP_LS |
883 			AMD_CG_SUPPORT_HDP_DS |
884 			AMD_CG_SUPPORT_HDP_SD |
885 			AMD_CG_SUPPORT_ATHUB_MGCG |
886 			AMD_CG_SUPPORT_ATHUB_LS |
887 			AMD_CG_SUPPORT_IH_CG |
888 			AMD_CG_SUPPORT_BIF_MGCG |
889 			AMD_CG_SUPPORT_BIF_LS;
890 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG |
891 			AMD_PG_SUPPORT_VCN |
892 			AMD_PG_SUPPORT_JPEG |
893 			AMD_PG_SUPPORT_GFX_PG;
894 		adev->external_rev_id = adev->rev_id + 0x40;
895 		break;
896 	default:
897 		/* FIXME: not supported yet */
898 		return -EINVAL;
899 	}
900 
901 	if (amdgpu_sriov_vf(adev)) {
902 		amdgpu_virt_init_setting(adev);
903 		xgpu_nv_mailbox_set_irq_funcs(adev);
904 	}
905 
906 	return 0;
907 }
908 
909 static int soc21_common_late_init(struct amdgpu_ip_block *ip_block)
910 {
911 	struct amdgpu_device *adev = ip_block->adev;
912 
913 	if (amdgpu_sriov_vf(adev)) {
914 		xgpu_nv_mailbox_get_irq(adev);
915 		if ((adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0) ||
916 		!amdgpu_sriov_is_av1_support(adev)) {
917 			amdgpu_virt_update_sriov_video_codec(adev,
918 							     sriov_vcn_4_0_0_video_codecs_encode_array_vcn1,
919 							     ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_encode_array_vcn1),
920 							     sriov_vcn_4_0_0_video_codecs_decode_array_vcn1,
921 							     ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_decode_array_vcn1));
922 		} else {
923 			amdgpu_virt_update_sriov_video_codec(adev,
924 							     sriov_vcn_4_0_0_video_codecs_encode_array_vcn0,
925 							     ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_encode_array_vcn0),
926 							     sriov_vcn_4_0_0_video_codecs_decode_array_vcn0,
927 							     ARRAY_SIZE(sriov_vcn_4_0_0_video_codecs_decode_array_vcn0));
928 		}
929 	} else {
930 		if (adev->nbio.ras &&
931 		    adev->nbio.ras_err_event_athub_irq.funcs)
932 			/* don't need to fail gpu late init
933 			 * if enabling athub_err_event interrupt failed
934 			 * nbio v4_3 only support fatal error hanlding
935 			 * just enable the interrupt directly */
936 			amdgpu_irq_get(adev, &adev->nbio.ras_err_event_athub_irq, 0);
937 	}
938 
939 	/* Enable selfring doorbell aperture late because doorbell BAR
940 	 * aperture will change if resize BAR successfully in gmc sw_init.
941 	 */
942 	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, true);
943 
944 	return 0;
945 }
946 
947 static int soc21_common_sw_init(struct amdgpu_ip_block *ip_block)
948 {
949 	struct amdgpu_device *adev = ip_block->adev;
950 
951 	if (amdgpu_sriov_vf(adev))
952 		xgpu_nv_mailbox_add_irq_id(adev);
953 
954 	return 0;
955 }
956 
957 static int soc21_common_hw_init(struct amdgpu_ip_block *ip_block)
958 {
959 	struct amdgpu_device *adev = ip_block->adev;
960 
961 	/* enable aspm */
962 	amdgpu_nbio_program_aspm(adev);
963 	/* setup nbio registers */
964 	adev->nbio.funcs->init_registers(adev);
965 	/* remap HDP registers to a hole in mmio space,
966 	 * for the purpose of expose those registers
967 	 * to process space
968 	 */
969 	if (adev->nbio.funcs->remap_hdp_registers && !amdgpu_sriov_vf(adev))
970 		adev->nbio.funcs->remap_hdp_registers(adev);
971 	/* enable the doorbell aperture */
972 	adev->nbio.funcs->enable_doorbell_aperture(adev, true);
973 
974 	return 0;
975 }
976 
977 static int soc21_common_hw_fini(struct amdgpu_ip_block *ip_block)
978 {
979 	struct amdgpu_device *adev = ip_block->adev;
980 
981 	/* Disable the doorbell aperture and selfring doorbell aperture
982 	 * separately in hw_fini because soc21_enable_doorbell_aperture
983 	 * has been removed and there is no need to delay disabling
984 	 * selfring doorbell.
985 	 */
986 	adev->nbio.funcs->enable_doorbell_aperture(adev, false);
987 	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, false);
988 
989 	if (amdgpu_sriov_vf(adev)) {
990 		xgpu_nv_mailbox_put_irq(adev);
991 	} else {
992 		if (adev->nbio.ras &&
993 		    adev->nbio.ras_err_event_athub_irq.funcs)
994 			amdgpu_irq_put(adev, &adev->nbio.ras_err_event_athub_irq, 0);
995 	}
996 
997 	return 0;
998 }
999 
1000 static int soc21_common_suspend(struct amdgpu_ip_block *ip_block)
1001 {
1002 	return soc21_common_hw_fini(ip_block);
1003 }
1004 
1005 static bool soc21_need_reset_on_resume(struct amdgpu_device *adev)
1006 {
1007 	u32 sol_reg1, sol_reg2;
1008 
1009 	/* Will reset for the following suspend abort cases.
1010 	 * 1) Only reset dGPU side.
1011 	 * 2) S3 suspend got aborted and TOS is active.
1012 	 *    As for dGPU suspend abort cases the SOL value
1013 	 *    will be kept as zero at this resume point.
1014 	 */
1015 	if (!(adev->flags & AMD_IS_APU) && adev->in_s3) {
1016 		sol_reg1 = RREG32_SOC15(MP0, 0, regMP0_SMN_C2PMSG_81);
1017 		msleep(100);
1018 		sol_reg2 = RREG32_SOC15(MP0, 0, regMP0_SMN_C2PMSG_81);
1019 
1020 		return (sol_reg1 != sol_reg2);
1021 	}
1022 
1023 	return false;
1024 }
1025 
1026 static int soc21_common_resume(struct amdgpu_ip_block *ip_block)
1027 {
1028 	struct amdgpu_device *adev = ip_block->adev;
1029 
1030 	if (soc21_need_reset_on_resume(adev)) {
1031 		dev_info(adev->dev, "S3 suspend aborted, resetting...");
1032 		soc21_asic_reset(adev);
1033 	}
1034 
1035 	return soc21_common_hw_init(ip_block);
1036 }
1037 
1038 static bool soc21_common_is_idle(struct amdgpu_ip_block *ip_block)
1039 {
1040 	return true;
1041 }
1042 
1043 static int soc21_common_set_clockgating_state(struct amdgpu_ip_block *ip_block,
1044 					   enum amd_clockgating_state state)
1045 {
1046 	struct amdgpu_device *adev = ip_block->adev;
1047 
1048 	switch (amdgpu_ip_version(adev, NBIO_HWIP, 0)) {
1049 	case IP_VERSION(4, 3, 0):
1050 	case IP_VERSION(4, 3, 1):
1051 	case IP_VERSION(7, 7, 0):
1052 	case IP_VERSION(7, 7, 1):
1053 	case IP_VERSION(7, 11, 0):
1054 	case IP_VERSION(7, 11, 1):
1055 	case IP_VERSION(7, 11, 2):
1056 	case IP_VERSION(7, 11, 3):
1057 	case IP_VERSION(7, 11, 4):
1058 		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1059 				state == AMD_CG_STATE_GATE);
1060 		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1061 				state == AMD_CG_STATE_GATE);
1062 		adev->hdp.funcs->update_clock_gating(adev,
1063 				state == AMD_CG_STATE_GATE);
1064 		break;
1065 	default:
1066 		break;
1067 	}
1068 	return 0;
1069 }
1070 
1071 static int soc21_common_set_powergating_state(struct amdgpu_ip_block *ip_block,
1072 					   enum amd_powergating_state state)
1073 {
1074 	struct amdgpu_device *adev = ip_block->adev;
1075 
1076 	switch (amdgpu_ip_version(adev, LSDMA_HWIP, 0)) {
1077 	case IP_VERSION(6, 0, 0):
1078 	case IP_VERSION(6, 0, 2):
1079 		adev->lsdma.funcs->update_memory_power_gating(adev,
1080 				state == AMD_PG_STATE_GATE);
1081 		break;
1082 	default:
1083 		break;
1084 	}
1085 
1086 	return 0;
1087 }
1088 
1089 static void soc21_common_get_clockgating_state(struct amdgpu_ip_block *ip_block, u64 *flags)
1090 {
1091 	struct amdgpu_device *adev = ip_block->adev;
1092 
1093 	adev->nbio.funcs->get_clockgating_state(adev, flags);
1094 
1095 	adev->hdp.funcs->get_clock_gating_state(adev, flags);
1096 }
1097 
1098 static const struct amd_ip_funcs soc21_common_ip_funcs = {
1099 	.name = "soc21_common",
1100 	.early_init = soc21_common_early_init,
1101 	.late_init = soc21_common_late_init,
1102 	.sw_init = soc21_common_sw_init,
1103 	.hw_init = soc21_common_hw_init,
1104 	.hw_fini = soc21_common_hw_fini,
1105 	.suspend = soc21_common_suspend,
1106 	.resume = soc21_common_resume,
1107 	.is_idle = soc21_common_is_idle,
1108 	.set_clockgating_state = soc21_common_set_clockgating_state,
1109 	.set_powergating_state = soc21_common_set_powergating_state,
1110 	.get_clockgating_state = soc21_common_get_clockgating_state,
1111 };
1112