xref: /linux/drivers/gpu/drm/amd/amdgpu/soc15.c (revision 5b723b12301272ed3c6c99c4ad8b43a520f880ea)
1 /*
2  * Copyright 2016 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 <drm/amdgpu_drm.h>
29 
30 #include "amdgpu.h"
31 #include "amdgpu_atombios.h"
32 #include "amdgpu_ih.h"
33 #include "amdgpu_uvd.h"
34 #include "amdgpu_vce.h"
35 #include "amdgpu_ucode.h"
36 #include "amdgpu_psp.h"
37 #include "atom.h"
38 #include "amd_pcie.h"
39 
40 #include "uvd/uvd_7_0_offset.h"
41 #include "gc/gc_9_0_offset.h"
42 #include "gc/gc_9_0_sh_mask.h"
43 #include "sdma0/sdma0_4_0_offset.h"
44 #include "sdma1/sdma1_4_0_offset.h"
45 #include "nbio/nbio_7_0_default.h"
46 #include "nbio/nbio_7_0_offset.h"
47 #include "nbio/nbio_7_0_sh_mask.h"
48 #include "nbio/nbio_7_0_smn.h"
49 #include "mp/mp_9_0_offset.h"
50 
51 #include "soc15.h"
52 #include "soc15_common.h"
53 #include "gfx_v9_0.h"
54 #include "gmc_v9_0.h"
55 #include "gfxhub_v1_0.h"
56 #include "mmhub_v1_0.h"
57 #include "df_v1_7.h"
58 #include "df_v3_6.h"
59 #include "nbio_v6_1.h"
60 #include "nbio_v7_0.h"
61 #include "nbio_v7_4.h"
62 #include "hdp_v4_0.h"
63 #include "vega10_ih.h"
64 #include "vega20_ih.h"
65 #include "navi10_ih.h"
66 #include "sdma_v4_0.h"
67 #include "uvd_v7_0.h"
68 #include "vce_v4_0.h"
69 #include "vcn_v1_0.h"
70 #include "vcn_v2_0.h"
71 #include "jpeg_v2_0.h"
72 #include "vcn_v2_5.h"
73 #include "jpeg_v2_5.h"
74 #include "smuio_v9_0.h"
75 #include "smuio_v11_0.h"
76 #include "smuio_v13_0.h"
77 #include "amdgpu_vkms.h"
78 #include "mxgpu_ai.h"
79 #include "amdgpu_ras.h"
80 #include "amdgpu_xgmi.h"
81 #include <uapi/linux/kfd_ioctl.h>
82 
83 #define mmMP0_MISC_CGTT_CTRL0                                                                   0x01b9
84 #define mmMP0_MISC_CGTT_CTRL0_BASE_IDX                                                          0
85 #define mmMP0_MISC_LIGHT_SLEEP_CTRL                                                             0x01ba
86 #define mmMP0_MISC_LIGHT_SLEEP_CTRL_BASE_IDX                                                    0
87 
88 static const struct amd_ip_funcs soc15_common_ip_funcs;
89 
90 /* Vega, Raven, Arcturus */
91 static const struct amdgpu_video_codec_info vega_video_codecs_encode_array[] =
92 {
93 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 2304, 0)},
94 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 2304, 0)},
95 };
96 
97 static const struct amdgpu_video_codecs vega_video_codecs_encode =
98 {
99 	.codec_count = ARRAY_SIZE(vega_video_codecs_encode_array),
100 	.codec_array = vega_video_codecs_encode_array,
101 };
102 
103 /* Vega */
104 static const struct amdgpu_video_codec_info vega_video_codecs_decode_array[] =
105 {
106 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4906, 3)},
107 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4906, 5)},
108 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4906, 52)},
109 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4906, 4)},
110 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 4096, 186)},
111 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
112 };
113 
114 static const struct amdgpu_video_codecs vega_video_codecs_decode =
115 {
116 	.codec_count = ARRAY_SIZE(vega_video_codecs_decode_array),
117 	.codec_array = vega_video_codecs_decode_array,
118 };
119 
120 /* Raven */
121 static const struct amdgpu_video_codec_info rv_video_codecs_decode_array[] =
122 {
123 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4906, 3)},
124 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4906, 5)},
125 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4906, 52)},
126 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4906, 4)},
127 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 4096, 4096, 186)},
128 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
129 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 4096, 4096, 0)},
130 };
131 
132 static const struct amdgpu_video_codecs rv_video_codecs_decode =
133 {
134 	.codec_count = ARRAY_SIZE(rv_video_codecs_decode_array),
135 	.codec_array = rv_video_codecs_decode_array,
136 };
137 
138 /* Renoir, Arcturus */
139 static const struct amdgpu_video_codec_info rn_video_codecs_decode_array[] =
140 {
141 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2, 4096, 4906, 3)},
142 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4, 4096, 4906, 5)},
143 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC, 4096, 4906, 52)},
144 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1, 4096, 4906, 4)},
145 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC, 8192, 4352, 186)},
146 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG, 4096, 4096, 0)},
147 	{codec_info_build(AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9, 8192, 4352, 0)},
148 };
149 
150 static const struct amdgpu_video_codecs rn_video_codecs_decode =
151 {
152 	.codec_count = ARRAY_SIZE(rn_video_codecs_decode_array),
153 	.codec_array = rn_video_codecs_decode_array,
154 };
155 
156 static int soc15_query_video_codecs(struct amdgpu_device *adev, bool encode,
157 				    const struct amdgpu_video_codecs **codecs)
158 {
159 	if (adev->ip_versions[VCE_HWIP][0]) {
160 		switch (adev->ip_versions[VCE_HWIP][0]) {
161 		case IP_VERSION(4, 0, 0):
162 		case IP_VERSION(4, 1, 0):
163 			if (encode)
164 				*codecs = &vega_video_codecs_encode;
165 			else
166 				*codecs = &vega_video_codecs_decode;
167 			return 0;
168 		default:
169 			return -EINVAL;
170 		}
171 	} else {
172 		switch (adev->ip_versions[UVD_HWIP][0]) {
173 		case IP_VERSION(1, 0, 0):
174 		case IP_VERSION(1, 0, 1):
175 			if (encode)
176 				*codecs = &vega_video_codecs_encode;
177 			else
178 				*codecs = &rv_video_codecs_decode;
179 			return 0;
180 		case IP_VERSION(2, 5, 0):
181 		case IP_VERSION(2, 6, 0):
182 		case IP_VERSION(2, 2, 0):
183 			if (encode)
184 				*codecs = &vega_video_codecs_encode;
185 			else
186 				*codecs = &rn_video_codecs_decode;
187 			return 0;
188 		default:
189 			return -EINVAL;
190 		}
191 	}
192 }
193 
194 /*
195  * Indirect registers accessor
196  */
197 static u32 soc15_pcie_rreg(struct amdgpu_device *adev, u32 reg)
198 {
199 	unsigned long address, data;
200 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
201 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
202 
203 	return amdgpu_device_indirect_rreg(adev, address, data, reg);
204 }
205 
206 static void soc15_pcie_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
207 {
208 	unsigned long address, data;
209 
210 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
211 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
212 
213 	amdgpu_device_indirect_wreg(adev, address, data, reg, v);
214 }
215 
216 static u64 soc15_pcie_rreg64(struct amdgpu_device *adev, u32 reg)
217 {
218 	unsigned long address, data;
219 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
220 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
221 
222 	return amdgpu_device_indirect_rreg64(adev, address, data, reg);
223 }
224 
225 static void soc15_pcie_wreg64(struct amdgpu_device *adev, u32 reg, u64 v)
226 {
227 	unsigned long address, data;
228 
229 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
230 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
231 
232 	amdgpu_device_indirect_wreg64(adev, address, data, reg, v);
233 }
234 
235 static u32 soc15_uvd_ctx_rreg(struct amdgpu_device *adev, u32 reg)
236 {
237 	unsigned long flags, address, data;
238 	u32 r;
239 
240 	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
241 	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
242 
243 	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
244 	WREG32(address, ((reg) & 0x1ff));
245 	r = RREG32(data);
246 	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
247 	return r;
248 }
249 
250 static void soc15_uvd_ctx_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
251 {
252 	unsigned long flags, address, data;
253 
254 	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
255 	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
256 
257 	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
258 	WREG32(address, ((reg) & 0x1ff));
259 	WREG32(data, (v));
260 	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
261 }
262 
263 static u32 soc15_didt_rreg(struct amdgpu_device *adev, u32 reg)
264 {
265 	unsigned long flags, address, data;
266 	u32 r;
267 
268 	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
269 	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
270 
271 	spin_lock_irqsave(&adev->didt_idx_lock, flags);
272 	WREG32(address, (reg));
273 	r = RREG32(data);
274 	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
275 	return r;
276 }
277 
278 static void soc15_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
279 {
280 	unsigned long flags, address, data;
281 
282 	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
283 	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
284 
285 	spin_lock_irqsave(&adev->didt_idx_lock, flags);
286 	WREG32(address, (reg));
287 	WREG32(data, (v));
288 	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
289 }
290 
291 static u32 soc15_gc_cac_rreg(struct amdgpu_device *adev, u32 reg)
292 {
293 	unsigned long flags;
294 	u32 r;
295 
296 	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
297 	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
298 	r = RREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA);
299 	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
300 	return r;
301 }
302 
303 static void soc15_gc_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
304 {
305 	unsigned long flags;
306 
307 	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
308 	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
309 	WREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA, (v));
310 	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
311 }
312 
313 static u32 soc15_se_cac_rreg(struct amdgpu_device *adev, u32 reg)
314 {
315 	unsigned long flags;
316 	u32 r;
317 
318 	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
319 	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
320 	r = RREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA);
321 	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
322 	return r;
323 }
324 
325 static void soc15_se_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
326 {
327 	unsigned long flags;
328 
329 	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
330 	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
331 	WREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA, (v));
332 	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
333 }
334 
335 static u32 soc15_get_config_memsize(struct amdgpu_device *adev)
336 {
337 	return adev->nbio.funcs->get_memsize(adev);
338 }
339 
340 static u32 soc15_get_xclk(struct amdgpu_device *adev)
341 {
342 	u32 reference_clock = adev->clock.spll.reference_freq;
343 
344 	if (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(12, 0, 0) ||
345 	    adev->ip_versions[MP1_HWIP][0] == IP_VERSION(12, 0, 1))
346 		return 10000;
347 	if (adev->ip_versions[MP1_HWIP][0] == IP_VERSION(10, 0, 0) ||
348 	    adev->ip_versions[MP1_HWIP][0] == IP_VERSION(10, 0, 1))
349 		return reference_clock / 4;
350 
351 	return reference_clock;
352 }
353 
354 
355 void soc15_grbm_select(struct amdgpu_device *adev,
356 		     u32 me, u32 pipe, u32 queue, u32 vmid)
357 {
358 	u32 grbm_gfx_cntl = 0;
359 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
360 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
361 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
362 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
363 
364 	WREG32_SOC15_RLC_SHADOW(GC, 0, mmGRBM_GFX_CNTL, grbm_gfx_cntl);
365 }
366 
367 static void soc15_vga_set_state(struct amdgpu_device *adev, bool state)
368 {
369 	/* todo */
370 }
371 
372 static bool soc15_read_disabled_bios(struct amdgpu_device *adev)
373 {
374 	/* todo */
375 	return false;
376 }
377 
378 static struct soc15_allowed_register_entry soc15_allowed_read_registers[] = {
379 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)},
380 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)},
381 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)},
382 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)},
383 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)},
384 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)},
385 	{ SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)},
386 	{ SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)},
387 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STAT)},
388 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)},
389 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)},
390 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)},
391 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)},
392 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)},
393 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)},
394 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_BUSY_STAT)},
395 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)},
396 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)},
397 	{ SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)},
398 	{ SOC15_REG_ENTRY(GC, 0, mmDB_DEBUG2)},
399 };
400 
401 static uint32_t soc15_read_indexed_register(struct amdgpu_device *adev, u32 se_num,
402 					 u32 sh_num, u32 reg_offset)
403 {
404 	uint32_t val;
405 
406 	mutex_lock(&adev->grbm_idx_mutex);
407 	if (se_num != 0xffffffff || sh_num != 0xffffffff)
408 		amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff);
409 
410 	val = RREG32(reg_offset);
411 
412 	if (se_num != 0xffffffff || sh_num != 0xffffffff)
413 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
414 	mutex_unlock(&adev->grbm_idx_mutex);
415 	return val;
416 }
417 
418 static uint32_t soc15_get_register_value(struct amdgpu_device *adev,
419 					 bool indexed, u32 se_num,
420 					 u32 sh_num, u32 reg_offset)
421 {
422 	if (indexed) {
423 		return soc15_read_indexed_register(adev, se_num, sh_num, reg_offset);
424 	} else {
425 		if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG))
426 			return adev->gfx.config.gb_addr_config;
427 		else if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmDB_DEBUG2))
428 			return adev->gfx.config.db_debug2;
429 		return RREG32(reg_offset);
430 	}
431 }
432 
433 static int soc15_read_register(struct amdgpu_device *adev, u32 se_num,
434 			    u32 sh_num, u32 reg_offset, u32 *value)
435 {
436 	uint32_t i;
437 	struct soc15_allowed_register_entry  *en;
438 
439 	*value = 0;
440 	for (i = 0; i < ARRAY_SIZE(soc15_allowed_read_registers); i++) {
441 		en = &soc15_allowed_read_registers[i];
442 		if (adev->reg_offset[en->hwip][en->inst] &&
443 			reg_offset != (adev->reg_offset[en->hwip][en->inst][en->seg]
444 					+ en->reg_offset))
445 			continue;
446 
447 		*value = soc15_get_register_value(adev,
448 						  soc15_allowed_read_registers[i].grbm_indexed,
449 						  se_num, sh_num, reg_offset);
450 		return 0;
451 	}
452 	return -EINVAL;
453 }
454 
455 
456 /**
457  * soc15_program_register_sequence - program an array of registers.
458  *
459  * @adev: amdgpu_device pointer
460  * @regs: pointer to the register array
461  * @array_size: size of the register array
462  *
463  * Programs an array or registers with and and or masks.
464  * This is a helper for setting golden registers.
465  */
466 
467 void soc15_program_register_sequence(struct amdgpu_device *adev,
468 					     const struct soc15_reg_golden *regs,
469 					     const u32 array_size)
470 {
471 	const struct soc15_reg_golden *entry;
472 	u32 tmp, reg;
473 	int i;
474 
475 	for (i = 0; i < array_size; ++i) {
476 		entry = &regs[i];
477 		reg =  adev->reg_offset[entry->hwip][entry->instance][entry->segment] + entry->reg;
478 
479 		if (entry->and_mask == 0xffffffff) {
480 			tmp = entry->or_mask;
481 		} else {
482 			tmp = (entry->hwip == GC_HWIP) ?
483 				RREG32_SOC15_IP(GC, reg) : RREG32(reg);
484 
485 			tmp &= ~(entry->and_mask);
486 			tmp |= (entry->or_mask & entry->and_mask);
487 		}
488 
489 		if (reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_3) ||
490 			reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_ENHANCE) ||
491 			reg == SOC15_REG_OFFSET(GC, 0, mmPA_SC_ENHANCE_1) ||
492 			reg == SOC15_REG_OFFSET(GC, 0, mmSH_MEM_CONFIG))
493 			WREG32_RLC(reg, tmp);
494 		else
495 			(entry->hwip == GC_HWIP) ?
496 				WREG32_SOC15_IP(GC, reg, tmp) : WREG32(reg, tmp);
497 
498 	}
499 
500 }
501 
502 static int soc15_asic_baco_reset(struct amdgpu_device *adev)
503 {
504 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
505 	int ret = 0;
506 
507 	/* avoid NBIF got stuck when do RAS recovery in BACO reset */
508 	if (ras && adev->ras_enabled)
509 		adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
510 
511 	ret = amdgpu_dpm_baco_reset(adev);
512 	if (ret)
513 		return ret;
514 
515 	/* re-enable doorbell interrupt after BACO exit */
516 	if (ras && adev->ras_enabled)
517 		adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
518 
519 	return 0;
520 }
521 
522 static enum amd_reset_method
523 soc15_asic_reset_method(struct amdgpu_device *adev)
524 {
525 	bool baco_reset = false;
526 	bool connected_to_cpu = false;
527 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
528 
529         if (adev->gmc.xgmi.supported && adev->gmc.xgmi.connected_to_cpu)
530                 connected_to_cpu = true;
531 
532 	if (amdgpu_reset_method == AMD_RESET_METHOD_MODE1 ||
533 	    amdgpu_reset_method == AMD_RESET_METHOD_MODE2 ||
534 	    amdgpu_reset_method == AMD_RESET_METHOD_BACO ||
535 	    amdgpu_reset_method == AMD_RESET_METHOD_PCI) {
536 		/* If connected to cpu, driver only support mode2 */
537                 if (connected_to_cpu)
538                         return AMD_RESET_METHOD_MODE2;
539                 return amdgpu_reset_method;
540         }
541 
542 	if (amdgpu_reset_method != -1)
543 		dev_warn(adev->dev, "Specified reset method:%d isn't supported, using AUTO instead.\n",
544 				  amdgpu_reset_method);
545 
546 	switch (adev->ip_versions[MP1_HWIP][0]) {
547 	case IP_VERSION(10, 0, 0):
548 	case IP_VERSION(10, 0, 1):
549 	case IP_VERSION(12, 0, 0):
550 	case IP_VERSION(12, 0, 1):
551 		return AMD_RESET_METHOD_MODE2;
552 	case IP_VERSION(9, 0, 0):
553 	case IP_VERSION(11, 0, 2):
554 		if (adev->asic_type == CHIP_VEGA20) {
555 			if (adev->psp.sos.fw_version >= 0x80067)
556 				baco_reset = amdgpu_dpm_is_baco_supported(adev);
557 			/*
558 			 * 1. PMFW version > 0x284300: all cases use baco
559 			 * 2. PMFW version <= 0x284300: only sGPU w/o RAS use baco
560 			 */
561 			if (ras && adev->ras_enabled &&
562 			    adev->pm.fw_version <= 0x283400)
563 				baco_reset = false;
564 		} else {
565 			baco_reset = amdgpu_dpm_is_baco_supported(adev);
566 		}
567 		break;
568 	case IP_VERSION(13, 0, 2):
569 		 /*
570 		 * 1.connected to cpu: driver issue mode2 reset
571 		 * 2.discret gpu: driver issue mode1 reset
572 		 */
573 		if (connected_to_cpu)
574 			return AMD_RESET_METHOD_MODE2;
575 		break;
576 	default:
577 		break;
578 	}
579 
580 	if (baco_reset)
581 		return AMD_RESET_METHOD_BACO;
582 	else
583 		return AMD_RESET_METHOD_MODE1;
584 }
585 
586 static int soc15_asic_reset(struct amdgpu_device *adev)
587 {
588 	/* original raven doesn't have full asic reset */
589 	if ((adev->apu_flags & AMD_APU_IS_RAVEN) ||
590 	    (adev->apu_flags & AMD_APU_IS_RAVEN2))
591 		return 0;
592 
593 	switch (soc15_asic_reset_method(adev)) {
594 	case AMD_RESET_METHOD_PCI:
595 		dev_info(adev->dev, "PCI reset\n");
596 		return amdgpu_device_pci_reset(adev);
597 	case AMD_RESET_METHOD_BACO:
598 		dev_info(adev->dev, "BACO reset\n");
599 		return soc15_asic_baco_reset(adev);
600 	case AMD_RESET_METHOD_MODE2:
601 		dev_info(adev->dev, "MODE2 reset\n");
602 		return amdgpu_dpm_mode2_reset(adev);
603 	default:
604 		dev_info(adev->dev, "MODE1 reset\n");
605 		return amdgpu_device_mode1_reset(adev);
606 	}
607 }
608 
609 static bool soc15_supports_baco(struct amdgpu_device *adev)
610 {
611 	switch (adev->ip_versions[MP1_HWIP][0]) {
612 	case IP_VERSION(9, 0, 0):
613 	case IP_VERSION(11, 0, 2):
614 		if (adev->asic_type == CHIP_VEGA20) {
615 			if (adev->psp.sos.fw_version >= 0x80067)
616 				return amdgpu_dpm_is_baco_supported(adev);
617 			return false;
618 		} else {
619 			return amdgpu_dpm_is_baco_supported(adev);
620 		}
621 		break;
622 	default:
623 		return false;
624 	}
625 }
626 
627 /*static int soc15_set_uvd_clock(struct amdgpu_device *adev, u32 clock,
628 			u32 cntl_reg, u32 status_reg)
629 {
630 	return 0;
631 }*/
632 
633 static int soc15_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
634 {
635 	/*int r;
636 
637 	r = soc15_set_uvd_clock(adev, vclk, ixCG_VCLK_CNTL, ixCG_VCLK_STATUS);
638 	if (r)
639 		return r;
640 
641 	r = soc15_set_uvd_clock(adev, dclk, ixCG_DCLK_CNTL, ixCG_DCLK_STATUS);
642 	*/
643 	return 0;
644 }
645 
646 static int soc15_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
647 {
648 	/* todo */
649 
650 	return 0;
651 }
652 
653 static void soc15_pcie_gen3_enable(struct amdgpu_device *adev)
654 {
655 	if (pci_is_root_bus(adev->pdev->bus))
656 		return;
657 
658 	if (amdgpu_pcie_gen2 == 0)
659 		return;
660 
661 	if (adev->flags & AMD_IS_APU)
662 		return;
663 
664 	if (!(adev->pm.pcie_gen_mask & (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
665 					CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)))
666 		return;
667 
668 	/* todo */
669 }
670 
671 static void soc15_program_aspm(struct amdgpu_device *adev)
672 {
673 	if (!amdgpu_device_should_use_aspm(adev))
674 		return;
675 
676 	if (!(adev->flags & AMD_IS_APU) &&
677 	    (adev->nbio.funcs->program_aspm))
678 		adev->nbio.funcs->program_aspm(adev);
679 }
680 
681 static void soc15_enable_doorbell_aperture(struct amdgpu_device *adev,
682 					   bool enable)
683 {
684 	adev->nbio.funcs->enable_doorbell_aperture(adev, enable);
685 	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, enable);
686 }
687 
688 const struct amdgpu_ip_block_version vega10_common_ip_block =
689 {
690 	.type = AMD_IP_BLOCK_TYPE_COMMON,
691 	.major = 2,
692 	.minor = 0,
693 	.rev = 0,
694 	.funcs = &soc15_common_ip_funcs,
695 };
696 
697 static uint32_t soc15_get_rev_id(struct amdgpu_device *adev)
698 {
699 	return adev->nbio.funcs->get_rev_id(adev);
700 }
701 
702 static void soc15_reg_base_init(struct amdgpu_device *adev)
703 {
704 	int r;
705 
706 	/* Set IP register base before any HW register access */
707 	switch (adev->asic_type) {
708 	case CHIP_VEGA10:
709 	case CHIP_VEGA12:
710 	case CHIP_RAVEN:
711 		vega10_reg_base_init(adev);
712 		break;
713 	case CHIP_RENOIR:
714 		/* It's safe to do ip discovery here for Renoir,
715 		 * it doesn't support SRIOV. */
716 		if (amdgpu_discovery) {
717 			r = amdgpu_discovery_reg_base_init(adev);
718 			if (r == 0)
719 				break;
720 			DRM_WARN("failed to init reg base from ip discovery table, "
721 				 "fallback to legacy init method\n");
722 		}
723 		vega10_reg_base_init(adev);
724 		break;
725 	case CHIP_VEGA20:
726 		vega20_reg_base_init(adev);
727 		break;
728 	case CHIP_ARCTURUS:
729 		arct_reg_base_init(adev);
730 		break;
731 	case CHIP_ALDEBARAN:
732 		aldebaran_reg_base_init(adev);
733 		break;
734 	default:
735 		DRM_ERROR("Unsupported asic type: %d!\n", adev->asic_type);
736 		break;
737 	}
738 }
739 
740 void soc15_set_virt_ops(struct amdgpu_device *adev)
741 {
742 	adev->virt.ops = &xgpu_ai_virt_ops;
743 
744 	/* init soc15 reg base early enough so we can
745 	 * request request full access for sriov before
746 	 * set_ip_blocks. */
747 	soc15_reg_base_init(adev);
748 }
749 
750 static bool soc15_need_full_reset(struct amdgpu_device *adev)
751 {
752 	/* change this when we implement soft reset */
753 	return true;
754 }
755 
756 static void soc15_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
757 				 uint64_t *count1)
758 {
759 	uint32_t perfctr = 0;
760 	uint64_t cnt0_of, cnt1_of;
761 	int tmp;
762 
763 	/* This reports 0 on APUs, so return to avoid writing/reading registers
764 	 * that may or may not be different from their GPU counterparts
765 	 */
766 	if (adev->flags & AMD_IS_APU)
767 		return;
768 
769 	/* Set the 2 events that we wish to watch, defined above */
770 	/* Reg 40 is # received msgs */
771 	/* Reg 104 is # of posted requests sent */
772 	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT0_SEL, 40);
773 	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT1_SEL, 104);
774 
775 	/* Write to enable desired perf counters */
776 	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK, perfctr);
777 	/* Zero out and enable the perf counters
778 	 * Write 0x5:
779 	 * Bit 0 = Start all counters(1)
780 	 * Bit 2 = Global counter reset enable(1)
781 	 */
782 	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
783 
784 	msleep(1000);
785 
786 	/* Load the shadow and disable the perf counters
787 	 * Write 0x2:
788 	 * Bit 0 = Stop counters(0)
789 	 * Bit 1 = Load the shadow counters(1)
790 	 */
791 	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
792 
793 	/* Read register values to get any >32bit overflow */
794 	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK);
795 	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER0_UPPER);
796 	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER1_UPPER);
797 
798 	/* Get the values and add the overflow */
799 	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK) | (cnt0_of << 32);
800 	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK) | (cnt1_of << 32);
801 }
802 
803 static void vega20_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
804 				 uint64_t *count1)
805 {
806 	uint32_t perfctr = 0;
807 	uint64_t cnt0_of, cnt1_of;
808 	int tmp;
809 
810 	/* This reports 0 on APUs, so return to avoid writing/reading registers
811 	 * that may or may not be different from their GPU counterparts
812 	 */
813 	if (adev->flags & AMD_IS_APU)
814 		return;
815 
816 	/* Set the 2 events that we wish to watch, defined above */
817 	/* Reg 40 is # received msgs */
818 	/* Reg 108 is # of posted requests sent on VG20 */
819 	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK3,
820 				EVENT0_SEL, 40);
821 	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK3,
822 				EVENT1_SEL, 108);
823 
824 	/* Write to enable desired perf counters */
825 	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK3, perfctr);
826 	/* Zero out and enable the perf counters
827 	 * Write 0x5:
828 	 * Bit 0 = Start all counters(1)
829 	 * Bit 2 = Global counter reset enable(1)
830 	 */
831 	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
832 
833 	msleep(1000);
834 
835 	/* Load the shadow and disable the perf counters
836 	 * Write 0x2:
837 	 * Bit 0 = Stop counters(0)
838 	 * Bit 1 = Load the shadow counters(1)
839 	 */
840 	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
841 
842 	/* Read register values to get any >32bit overflow */
843 	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK3);
844 	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK3, COUNTER0_UPPER);
845 	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK3, COUNTER1_UPPER);
846 
847 	/* Get the values and add the overflow */
848 	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK3) | (cnt0_of << 32);
849 	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK3) | (cnt1_of << 32);
850 }
851 
852 static bool soc15_need_reset_on_init(struct amdgpu_device *adev)
853 {
854 	u32 sol_reg;
855 
856 	/* Just return false for soc15 GPUs.  Reset does not seem to
857 	 * be necessary.
858 	 */
859 	if (!amdgpu_passthrough(adev))
860 		return false;
861 
862 	if (adev->flags & AMD_IS_APU)
863 		return false;
864 
865 	/* Check sOS sign of life register to confirm sys driver and sOS
866 	 * are already been loaded.
867 	 */
868 	sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
869 	if (sol_reg)
870 		return true;
871 
872 	return false;
873 }
874 
875 static uint64_t soc15_get_pcie_replay_count(struct amdgpu_device *adev)
876 {
877 	uint64_t nak_r, nak_g;
878 
879 	/* Get the number of NAKs received and generated */
880 	nak_r = RREG32_PCIE(smnPCIE_RX_NUM_NAK);
881 	nak_g = RREG32_PCIE(smnPCIE_RX_NUM_NAK_GENERATED);
882 
883 	/* Add the total number of NAKs, i.e the number of replays */
884 	return (nak_r + nak_g);
885 }
886 
887 static void soc15_pre_asic_init(struct amdgpu_device *adev)
888 {
889 	gmc_v9_0_restore_registers(adev);
890 }
891 
892 static const struct amdgpu_asic_funcs soc15_asic_funcs =
893 {
894 	.read_disabled_bios = &soc15_read_disabled_bios,
895 	.read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom,
896 	.read_register = &soc15_read_register,
897 	.reset = &soc15_asic_reset,
898 	.reset_method = &soc15_asic_reset_method,
899 	.set_vga_state = &soc15_vga_set_state,
900 	.get_xclk = &soc15_get_xclk,
901 	.set_uvd_clocks = &soc15_set_uvd_clocks,
902 	.set_vce_clocks = &soc15_set_vce_clocks,
903 	.get_config_memsize = &soc15_get_config_memsize,
904 	.need_full_reset = &soc15_need_full_reset,
905 	.init_doorbell_index = &vega10_doorbell_index_init,
906 	.get_pcie_usage = &soc15_get_pcie_usage,
907 	.need_reset_on_init = &soc15_need_reset_on_init,
908 	.get_pcie_replay_count = &soc15_get_pcie_replay_count,
909 	.supports_baco = &soc15_supports_baco,
910 	.pre_asic_init = &soc15_pre_asic_init,
911 	.query_video_codecs = &soc15_query_video_codecs,
912 };
913 
914 static const struct amdgpu_asic_funcs vega20_asic_funcs =
915 {
916 	.read_disabled_bios = &soc15_read_disabled_bios,
917 	.read_bios_from_rom = &amdgpu_soc15_read_bios_from_rom,
918 	.read_register = &soc15_read_register,
919 	.reset = &soc15_asic_reset,
920 	.reset_method = &soc15_asic_reset_method,
921 	.set_vga_state = &soc15_vga_set_state,
922 	.get_xclk = &soc15_get_xclk,
923 	.set_uvd_clocks = &soc15_set_uvd_clocks,
924 	.set_vce_clocks = &soc15_set_vce_clocks,
925 	.get_config_memsize = &soc15_get_config_memsize,
926 	.need_full_reset = &soc15_need_full_reset,
927 	.init_doorbell_index = &vega20_doorbell_index_init,
928 	.get_pcie_usage = &vega20_get_pcie_usage,
929 	.need_reset_on_init = &soc15_need_reset_on_init,
930 	.get_pcie_replay_count = &soc15_get_pcie_replay_count,
931 	.supports_baco = &soc15_supports_baco,
932 	.pre_asic_init = &soc15_pre_asic_init,
933 	.query_video_codecs = &soc15_query_video_codecs,
934 };
935 
936 static int soc15_common_early_init(void *handle)
937 {
938 #define MMIO_REG_HOLE_OFFSET (0x80000 - PAGE_SIZE)
939 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
940 
941 	if (!amdgpu_sriov_vf(adev)) {
942 		adev->rmmio_remap.reg_offset = MMIO_REG_HOLE_OFFSET;
943 		adev->rmmio_remap.bus_addr = adev->rmmio_base + MMIO_REG_HOLE_OFFSET;
944 	}
945 	adev->smc_rreg = NULL;
946 	adev->smc_wreg = NULL;
947 	adev->pcie_rreg = &soc15_pcie_rreg;
948 	adev->pcie_wreg = &soc15_pcie_wreg;
949 	adev->pcie_rreg64 = &soc15_pcie_rreg64;
950 	adev->pcie_wreg64 = &soc15_pcie_wreg64;
951 	adev->uvd_ctx_rreg = &soc15_uvd_ctx_rreg;
952 	adev->uvd_ctx_wreg = &soc15_uvd_ctx_wreg;
953 	adev->didt_rreg = &soc15_didt_rreg;
954 	adev->didt_wreg = &soc15_didt_wreg;
955 	adev->gc_cac_rreg = &soc15_gc_cac_rreg;
956 	adev->gc_cac_wreg = &soc15_gc_cac_wreg;
957 	adev->se_cac_rreg = &soc15_se_cac_rreg;
958 	adev->se_cac_wreg = &soc15_se_cac_wreg;
959 
960 	adev->rev_id = soc15_get_rev_id(adev);
961 	adev->external_rev_id = 0xFF;
962 	/* TODO: split the GC and PG flags based on the relevant IP version for which
963 	 * they are relevant.
964 	 */
965 	switch (adev->ip_versions[GC_HWIP][0]) {
966 	case IP_VERSION(9, 0, 1):
967 		adev->asic_funcs = &soc15_asic_funcs;
968 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
969 			AMD_CG_SUPPORT_GFX_MGLS |
970 			AMD_CG_SUPPORT_GFX_RLC_LS |
971 			AMD_CG_SUPPORT_GFX_CP_LS |
972 			AMD_CG_SUPPORT_GFX_3D_CGCG |
973 			AMD_CG_SUPPORT_GFX_3D_CGLS |
974 			AMD_CG_SUPPORT_GFX_CGCG |
975 			AMD_CG_SUPPORT_GFX_CGLS |
976 			AMD_CG_SUPPORT_BIF_MGCG |
977 			AMD_CG_SUPPORT_BIF_LS |
978 			AMD_CG_SUPPORT_HDP_LS |
979 			AMD_CG_SUPPORT_DRM_MGCG |
980 			AMD_CG_SUPPORT_DRM_LS |
981 			AMD_CG_SUPPORT_ROM_MGCG |
982 			AMD_CG_SUPPORT_DF_MGCG |
983 			AMD_CG_SUPPORT_SDMA_MGCG |
984 			AMD_CG_SUPPORT_SDMA_LS |
985 			AMD_CG_SUPPORT_MC_MGCG |
986 			AMD_CG_SUPPORT_MC_LS;
987 		adev->pg_flags = 0;
988 		adev->external_rev_id = 0x1;
989 		break;
990 	case IP_VERSION(9, 2, 1):
991 		adev->asic_funcs = &soc15_asic_funcs;
992 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
993 			AMD_CG_SUPPORT_GFX_MGLS |
994 			AMD_CG_SUPPORT_GFX_CGCG |
995 			AMD_CG_SUPPORT_GFX_CGLS |
996 			AMD_CG_SUPPORT_GFX_3D_CGCG |
997 			AMD_CG_SUPPORT_GFX_3D_CGLS |
998 			AMD_CG_SUPPORT_GFX_CP_LS |
999 			AMD_CG_SUPPORT_MC_LS |
1000 			AMD_CG_SUPPORT_MC_MGCG |
1001 			AMD_CG_SUPPORT_SDMA_MGCG |
1002 			AMD_CG_SUPPORT_SDMA_LS |
1003 			AMD_CG_SUPPORT_BIF_MGCG |
1004 			AMD_CG_SUPPORT_BIF_LS |
1005 			AMD_CG_SUPPORT_HDP_MGCG |
1006 			AMD_CG_SUPPORT_HDP_LS |
1007 			AMD_CG_SUPPORT_ROM_MGCG |
1008 			AMD_CG_SUPPORT_VCE_MGCG |
1009 			AMD_CG_SUPPORT_UVD_MGCG;
1010 		adev->pg_flags = 0;
1011 		adev->external_rev_id = adev->rev_id + 0x14;
1012 		break;
1013 	case IP_VERSION(9, 4, 0):
1014 		adev->asic_funcs = &vega20_asic_funcs;
1015 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1016 			AMD_CG_SUPPORT_GFX_MGLS |
1017 			AMD_CG_SUPPORT_GFX_CGCG |
1018 			AMD_CG_SUPPORT_GFX_CGLS |
1019 			AMD_CG_SUPPORT_GFX_3D_CGCG |
1020 			AMD_CG_SUPPORT_GFX_3D_CGLS |
1021 			AMD_CG_SUPPORT_GFX_CP_LS |
1022 			AMD_CG_SUPPORT_MC_LS |
1023 			AMD_CG_SUPPORT_MC_MGCG |
1024 			AMD_CG_SUPPORT_SDMA_MGCG |
1025 			AMD_CG_SUPPORT_SDMA_LS |
1026 			AMD_CG_SUPPORT_BIF_MGCG |
1027 			AMD_CG_SUPPORT_BIF_LS |
1028 			AMD_CG_SUPPORT_HDP_MGCG |
1029 			AMD_CG_SUPPORT_HDP_LS |
1030 			AMD_CG_SUPPORT_ROM_MGCG |
1031 			AMD_CG_SUPPORT_VCE_MGCG |
1032 			AMD_CG_SUPPORT_UVD_MGCG;
1033 		adev->pg_flags = 0;
1034 		adev->external_rev_id = adev->rev_id + 0x28;
1035 		break;
1036 	case IP_VERSION(9, 1, 0):
1037 	case IP_VERSION(9, 2, 2):
1038 		adev->asic_funcs = &soc15_asic_funcs;
1039 
1040 		if (adev->rev_id >= 0x8)
1041 			adev->apu_flags |= AMD_APU_IS_RAVEN2;
1042 
1043 		if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1044 			adev->external_rev_id = adev->rev_id + 0x79;
1045 		else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1046 			adev->external_rev_id = adev->rev_id + 0x41;
1047 		else if (adev->rev_id == 1)
1048 			adev->external_rev_id = adev->rev_id + 0x20;
1049 		else
1050 			adev->external_rev_id = adev->rev_id + 0x01;
1051 
1052 		if (adev->apu_flags & AMD_APU_IS_RAVEN2) {
1053 			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1054 				AMD_CG_SUPPORT_GFX_MGLS |
1055 				AMD_CG_SUPPORT_GFX_CP_LS |
1056 				AMD_CG_SUPPORT_GFX_3D_CGCG |
1057 				AMD_CG_SUPPORT_GFX_3D_CGLS |
1058 				AMD_CG_SUPPORT_GFX_CGCG |
1059 				AMD_CG_SUPPORT_GFX_CGLS |
1060 				AMD_CG_SUPPORT_BIF_LS |
1061 				AMD_CG_SUPPORT_HDP_LS |
1062 				AMD_CG_SUPPORT_MC_MGCG |
1063 				AMD_CG_SUPPORT_MC_LS |
1064 				AMD_CG_SUPPORT_SDMA_MGCG |
1065 				AMD_CG_SUPPORT_SDMA_LS |
1066 				AMD_CG_SUPPORT_VCN_MGCG;
1067 
1068 			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
1069 		} else if (adev->apu_flags & AMD_APU_IS_PICASSO) {
1070 			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1071 				AMD_CG_SUPPORT_GFX_MGLS |
1072 				AMD_CG_SUPPORT_GFX_CP_LS |
1073 				AMD_CG_SUPPORT_GFX_3D_CGLS |
1074 				AMD_CG_SUPPORT_GFX_CGCG |
1075 				AMD_CG_SUPPORT_GFX_CGLS |
1076 				AMD_CG_SUPPORT_BIF_LS |
1077 				AMD_CG_SUPPORT_HDP_LS |
1078 				AMD_CG_SUPPORT_MC_MGCG |
1079 				AMD_CG_SUPPORT_MC_LS |
1080 				AMD_CG_SUPPORT_SDMA_MGCG |
1081 				AMD_CG_SUPPORT_SDMA_LS |
1082 				AMD_CG_SUPPORT_VCN_MGCG;
1083 
1084 			/*
1085 			 * MMHUB PG needs to be disabled for Picasso for
1086 			 * stability reasons.
1087 			 */
1088 			adev->pg_flags = AMD_PG_SUPPORT_SDMA |
1089 				AMD_PG_SUPPORT_VCN;
1090 		} else {
1091 			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1092 				AMD_CG_SUPPORT_GFX_MGLS |
1093 				AMD_CG_SUPPORT_GFX_RLC_LS |
1094 				AMD_CG_SUPPORT_GFX_CP_LS |
1095 				AMD_CG_SUPPORT_GFX_3D_CGLS |
1096 				AMD_CG_SUPPORT_GFX_CGCG |
1097 				AMD_CG_SUPPORT_GFX_CGLS |
1098 				AMD_CG_SUPPORT_BIF_MGCG |
1099 				AMD_CG_SUPPORT_BIF_LS |
1100 				AMD_CG_SUPPORT_HDP_MGCG |
1101 				AMD_CG_SUPPORT_HDP_LS |
1102 				AMD_CG_SUPPORT_DRM_MGCG |
1103 				AMD_CG_SUPPORT_DRM_LS |
1104 				AMD_CG_SUPPORT_MC_MGCG |
1105 				AMD_CG_SUPPORT_MC_LS |
1106 				AMD_CG_SUPPORT_SDMA_MGCG |
1107 				AMD_CG_SUPPORT_SDMA_LS |
1108 				AMD_CG_SUPPORT_VCN_MGCG;
1109 
1110 			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
1111 		}
1112 		break;
1113 	case IP_VERSION(9, 4, 1):
1114 		adev->asic_funcs = &vega20_asic_funcs;
1115 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1116 			AMD_CG_SUPPORT_GFX_MGLS |
1117 			AMD_CG_SUPPORT_GFX_CGCG |
1118 			AMD_CG_SUPPORT_GFX_CGLS |
1119 			AMD_CG_SUPPORT_GFX_CP_LS |
1120 			AMD_CG_SUPPORT_HDP_MGCG |
1121 			AMD_CG_SUPPORT_HDP_LS |
1122 			AMD_CG_SUPPORT_SDMA_MGCG |
1123 			AMD_CG_SUPPORT_SDMA_LS |
1124 			AMD_CG_SUPPORT_MC_MGCG |
1125 			AMD_CG_SUPPORT_MC_LS |
1126 			AMD_CG_SUPPORT_IH_CG |
1127 			AMD_CG_SUPPORT_VCN_MGCG |
1128 			AMD_CG_SUPPORT_JPEG_MGCG;
1129 		adev->pg_flags = AMD_PG_SUPPORT_VCN | AMD_PG_SUPPORT_VCN_DPG;
1130 		adev->external_rev_id = adev->rev_id + 0x32;
1131 		break;
1132 	case IP_VERSION(9, 3, 0):
1133 		adev->asic_funcs = &soc15_asic_funcs;
1134 
1135 		if (adev->apu_flags & AMD_APU_IS_RENOIR)
1136 			adev->external_rev_id = adev->rev_id + 0x91;
1137 		else
1138 			adev->external_rev_id = adev->rev_id + 0xa1;
1139 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1140 				 AMD_CG_SUPPORT_GFX_MGLS |
1141 				 AMD_CG_SUPPORT_GFX_3D_CGCG |
1142 				 AMD_CG_SUPPORT_GFX_3D_CGLS |
1143 				 AMD_CG_SUPPORT_GFX_CGCG |
1144 				 AMD_CG_SUPPORT_GFX_CGLS |
1145 				 AMD_CG_SUPPORT_GFX_CP_LS |
1146 				 AMD_CG_SUPPORT_MC_MGCG |
1147 				 AMD_CG_SUPPORT_MC_LS |
1148 				 AMD_CG_SUPPORT_SDMA_MGCG |
1149 				 AMD_CG_SUPPORT_SDMA_LS |
1150 				 AMD_CG_SUPPORT_BIF_LS |
1151 				 AMD_CG_SUPPORT_HDP_LS |
1152 				 AMD_CG_SUPPORT_VCN_MGCG |
1153 				 AMD_CG_SUPPORT_JPEG_MGCG |
1154 				 AMD_CG_SUPPORT_IH_CG |
1155 				 AMD_CG_SUPPORT_ATHUB_LS |
1156 				 AMD_CG_SUPPORT_ATHUB_MGCG |
1157 				 AMD_CG_SUPPORT_DF_MGCG;
1158 		adev->pg_flags = AMD_PG_SUPPORT_SDMA |
1159 				 AMD_PG_SUPPORT_VCN |
1160 				 AMD_PG_SUPPORT_JPEG |
1161 				 AMD_PG_SUPPORT_VCN_DPG;
1162 		break;
1163 	case IP_VERSION(9, 4, 2):
1164 		adev->asic_funcs = &vega20_asic_funcs;
1165 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
1166 			AMD_CG_SUPPORT_GFX_MGLS |
1167 			AMD_CG_SUPPORT_GFX_CP_LS |
1168 			AMD_CG_SUPPORT_HDP_LS |
1169 			AMD_CG_SUPPORT_SDMA_MGCG |
1170 			AMD_CG_SUPPORT_SDMA_LS |
1171 			AMD_CG_SUPPORT_IH_CG |
1172 			AMD_CG_SUPPORT_VCN_MGCG | AMD_CG_SUPPORT_JPEG_MGCG;
1173 		adev->pg_flags = AMD_PG_SUPPORT_VCN_DPG;
1174 		adev->external_rev_id = adev->rev_id + 0x3c;
1175 		break;
1176 	default:
1177 		/* FIXME: not supported yet */
1178 		return -EINVAL;
1179 	}
1180 
1181 	if (amdgpu_sriov_vf(adev)) {
1182 		amdgpu_virt_init_setting(adev);
1183 		xgpu_ai_mailbox_set_irq_funcs(adev);
1184 	}
1185 
1186 	return 0;
1187 }
1188 
1189 static int soc15_common_late_init(void *handle)
1190 {
1191 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1192 
1193 	if (amdgpu_sriov_vf(adev))
1194 		xgpu_ai_mailbox_get_irq(adev);
1195 
1196 	return 0;
1197 }
1198 
1199 static int soc15_common_sw_init(void *handle)
1200 {
1201 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1202 
1203 	if (amdgpu_sriov_vf(adev))
1204 		xgpu_ai_mailbox_add_irq_id(adev);
1205 
1206 	if (adev->df.funcs &&
1207 	    adev->df.funcs->sw_init)
1208 		adev->df.funcs->sw_init(adev);
1209 
1210 	return 0;
1211 }
1212 
1213 static int soc15_common_sw_fini(void *handle)
1214 {
1215 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1216 
1217 	if (adev->nbio.ras && adev->nbio.ras->ras_block.ras_fini)
1218 		adev->nbio.ras->ras_block.ras_fini(adev);
1219 
1220 	if (adev->df.funcs &&
1221 	    adev->df.funcs->sw_fini)
1222 		adev->df.funcs->sw_fini(adev);
1223 	return 0;
1224 }
1225 
1226 static void soc15_doorbell_range_init(struct amdgpu_device *adev)
1227 {
1228 	int i;
1229 	struct amdgpu_ring *ring;
1230 
1231 	/* sdma/ih doorbell range are programed by hypervisor */
1232 	if (!amdgpu_sriov_vf(adev)) {
1233 		for (i = 0; i < adev->sdma.num_instances; i++) {
1234 			ring = &adev->sdma.instance[i].ring;
1235 			adev->nbio.funcs->sdma_doorbell_range(adev, i,
1236 				ring->use_doorbell, ring->doorbell_index,
1237 				adev->doorbell_index.sdma_doorbell_range);
1238 		}
1239 
1240 		adev->nbio.funcs->ih_doorbell_range(adev, adev->irq.ih.use_doorbell,
1241 						adev->irq.ih.doorbell_index);
1242 	}
1243 }
1244 
1245 static int soc15_common_hw_init(void *handle)
1246 {
1247 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1248 
1249 	/* enable pcie gen2/3 link */
1250 	soc15_pcie_gen3_enable(adev);
1251 	/* enable aspm */
1252 	soc15_program_aspm(adev);
1253 	/* setup nbio registers */
1254 	adev->nbio.funcs->init_registers(adev);
1255 	/* remap HDP registers to a hole in mmio space,
1256 	 * for the purpose of expose those registers
1257 	 * to process space
1258 	 */
1259 	if (adev->nbio.funcs->remap_hdp_registers && !amdgpu_sriov_vf(adev))
1260 		adev->nbio.funcs->remap_hdp_registers(adev);
1261 
1262 	/* enable the doorbell aperture */
1263 	soc15_enable_doorbell_aperture(adev, true);
1264 	/* HW doorbell routing policy: doorbell writing not
1265 	 * in SDMA/IH/MM/ACV range will be routed to CP. So
1266 	 * we need to init SDMA/IH/MM/ACV doorbell range prior
1267 	 * to CP ip block init and ring test.
1268 	 */
1269 	soc15_doorbell_range_init(adev);
1270 
1271 	return 0;
1272 }
1273 
1274 static int soc15_common_hw_fini(void *handle)
1275 {
1276 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1277 
1278 	/* disable the doorbell aperture */
1279 	soc15_enable_doorbell_aperture(adev, false);
1280 	if (amdgpu_sriov_vf(adev))
1281 		xgpu_ai_mailbox_put_irq(adev);
1282 
1283 	if (adev->nbio.ras_if &&
1284 	    amdgpu_ras_is_supported(adev, adev->nbio.ras_if->block)) {
1285 		if (adev->nbio.ras &&
1286 		    adev->nbio.ras->init_ras_controller_interrupt)
1287 			amdgpu_irq_put(adev, &adev->nbio.ras_controller_irq, 0);
1288 		if (adev->nbio.ras &&
1289 		    adev->nbio.ras->init_ras_err_event_athub_interrupt)
1290 			amdgpu_irq_put(adev, &adev->nbio.ras_err_event_athub_irq, 0);
1291 	}
1292 
1293 	return 0;
1294 }
1295 
1296 static int soc15_common_suspend(void *handle)
1297 {
1298 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1299 
1300 	return soc15_common_hw_fini(adev);
1301 }
1302 
1303 static int soc15_common_resume(void *handle)
1304 {
1305 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1306 
1307 	return soc15_common_hw_init(adev);
1308 }
1309 
1310 static bool soc15_common_is_idle(void *handle)
1311 {
1312 	return true;
1313 }
1314 
1315 static int soc15_common_wait_for_idle(void *handle)
1316 {
1317 	return 0;
1318 }
1319 
1320 static int soc15_common_soft_reset(void *handle)
1321 {
1322 	return 0;
1323 }
1324 
1325 static void soc15_update_drm_clock_gating(struct amdgpu_device *adev, bool enable)
1326 {
1327 	uint32_t def, data;
1328 
1329 	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1330 
1331 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_MGCG))
1332 		data &= ~(0x01000000 |
1333 			  0x02000000 |
1334 			  0x04000000 |
1335 			  0x08000000 |
1336 			  0x10000000 |
1337 			  0x20000000 |
1338 			  0x40000000 |
1339 			  0x80000000);
1340 	else
1341 		data |= (0x01000000 |
1342 			 0x02000000 |
1343 			 0x04000000 |
1344 			 0x08000000 |
1345 			 0x10000000 |
1346 			 0x20000000 |
1347 			 0x40000000 |
1348 			 0x80000000);
1349 
1350 	if (def != data)
1351 		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0), data);
1352 }
1353 
1354 static void soc15_update_drm_light_sleep(struct amdgpu_device *adev, bool enable)
1355 {
1356 	uint32_t def, data;
1357 
1358 	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1359 
1360 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_LS))
1361 		data |= 1;
1362 	else
1363 		data &= ~1;
1364 
1365 	if (def != data)
1366 		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL), data);
1367 }
1368 
1369 static int soc15_common_set_clockgating_state(void *handle,
1370 					    enum amd_clockgating_state state)
1371 {
1372 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1373 
1374 	if (amdgpu_sriov_vf(adev))
1375 		return 0;
1376 
1377 	switch (adev->ip_versions[NBIO_HWIP][0]) {
1378 	case IP_VERSION(6, 1, 0):
1379 	case IP_VERSION(6, 2, 0):
1380 	case IP_VERSION(7, 4, 0):
1381 		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1382 				state == AMD_CG_STATE_GATE);
1383 		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1384 				state == AMD_CG_STATE_GATE);
1385 		adev->hdp.funcs->update_clock_gating(adev,
1386 				state == AMD_CG_STATE_GATE);
1387 		soc15_update_drm_clock_gating(adev,
1388 				state == AMD_CG_STATE_GATE);
1389 		soc15_update_drm_light_sleep(adev,
1390 				state == AMD_CG_STATE_GATE);
1391 		adev->smuio.funcs->update_rom_clock_gating(adev,
1392 				state == AMD_CG_STATE_GATE);
1393 		adev->df.funcs->update_medium_grain_clock_gating(adev,
1394 				state == AMD_CG_STATE_GATE);
1395 		break;
1396 	case IP_VERSION(7, 0, 0):
1397 	case IP_VERSION(7, 0, 1):
1398 	case IP_VERSION(2, 5, 0):
1399 		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1400 				state == AMD_CG_STATE_GATE);
1401 		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1402 				state == AMD_CG_STATE_GATE);
1403 		adev->hdp.funcs->update_clock_gating(adev,
1404 				state == AMD_CG_STATE_GATE);
1405 		soc15_update_drm_clock_gating(adev,
1406 				state == AMD_CG_STATE_GATE);
1407 		soc15_update_drm_light_sleep(adev,
1408 				state == AMD_CG_STATE_GATE);
1409 		break;
1410 	case IP_VERSION(7, 4, 1):
1411 	case IP_VERSION(7, 4, 4):
1412 		adev->hdp.funcs->update_clock_gating(adev,
1413 				state == AMD_CG_STATE_GATE);
1414 		break;
1415 	default:
1416 		break;
1417 	}
1418 	return 0;
1419 }
1420 
1421 static void soc15_common_get_clockgating_state(void *handle, u32 *flags)
1422 {
1423 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1424 	int data;
1425 
1426 	if (amdgpu_sriov_vf(adev))
1427 		*flags = 0;
1428 
1429 	adev->nbio.funcs->get_clockgating_state(adev, flags);
1430 
1431 	adev->hdp.funcs->get_clock_gating_state(adev, flags);
1432 
1433 	if (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2)) {
1434 
1435 		/* AMD_CG_SUPPORT_DRM_MGCG */
1436 		data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1437 		if (!(data & 0x01000000))
1438 			*flags |= AMD_CG_SUPPORT_DRM_MGCG;
1439 
1440 		/* AMD_CG_SUPPORT_DRM_LS */
1441 		data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1442 		if (data & 0x1)
1443 			*flags |= AMD_CG_SUPPORT_DRM_LS;
1444 	}
1445 
1446 	/* AMD_CG_SUPPORT_ROM_MGCG */
1447 	adev->smuio.funcs->get_clock_gating_state(adev, flags);
1448 
1449 	adev->df.funcs->get_clockgating_state(adev, flags);
1450 }
1451 
1452 static int soc15_common_set_powergating_state(void *handle,
1453 					    enum amd_powergating_state state)
1454 {
1455 	/* todo */
1456 	return 0;
1457 }
1458 
1459 static const struct amd_ip_funcs soc15_common_ip_funcs = {
1460 	.name = "soc15_common",
1461 	.early_init = soc15_common_early_init,
1462 	.late_init = soc15_common_late_init,
1463 	.sw_init = soc15_common_sw_init,
1464 	.sw_fini = soc15_common_sw_fini,
1465 	.hw_init = soc15_common_hw_init,
1466 	.hw_fini = soc15_common_hw_fini,
1467 	.suspend = soc15_common_suspend,
1468 	.resume = soc15_common_resume,
1469 	.is_idle = soc15_common_is_idle,
1470 	.wait_for_idle = soc15_common_wait_for_idle,
1471 	.soft_reset = soc15_common_soft_reset,
1472 	.set_clockgating_state = soc15_common_set_clockgating_state,
1473 	.set_powergating_state = soc15_common_set_powergating_state,
1474 	.get_clockgating_state= soc15_common_get_clockgating_state,
1475 };
1476