xref: /linux/drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 /*
2  * Copyright 2022 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 
25 #include "amdgpu.h"
26 #include "amdgpu_gfx.h"
27 #include "soc15.h"
28 #include "soc15d.h"
29 #include "soc15_common.h"
30 #include "vega10_enum.h"
31 
32 #include "v9_structs.h"
33 
34 #include "ivsrcid/gfx/irqsrcs_gfx_9_0.h"
35 
36 #include "gc/gc_9_4_3_offset.h"
37 #include "gc/gc_9_4_3_sh_mask.h"
38 
39 #include "gfx_v9_4_3.h"
40 #include "gfx_v9_4_3_cleaner_shader.h"
41 #include "amdgpu_xcp.h"
42 #include "amdgpu_aca.h"
43 
44 MODULE_FIRMWARE("amdgpu/gc_9_4_3_mec.bin");
45 MODULE_FIRMWARE("amdgpu/gc_9_4_4_mec.bin");
46 MODULE_FIRMWARE("amdgpu/gc_9_4_3_rlc.bin");
47 MODULE_FIRMWARE("amdgpu/gc_9_4_4_rlc.bin");
48 
49 #define GFX9_MEC_HPD_SIZE 4096
50 #define RLCG_UCODE_LOADING_START_ADDRESS 0x00002000L
51 
52 #define GOLDEN_GB_ADDR_CONFIG 0x2a114042
53 #define CP_HQD_PERSISTENT_STATE_DEFAULT 0xbe05301
54 
55 #define mmSMNAID_XCD0_MCA_SMU 0x36430400	/* SMN AID XCD0 */
56 #define mmSMNAID_XCD1_MCA_SMU 0x38430400	/* SMN AID XCD1 */
57 #define mmSMNXCD_XCD0_MCA_SMU 0x40430400	/* SMN XCD XCD0 */
58 
59 #define XCC_REG_RANGE_0_LOW  0x2000     /* XCC gfxdec0 lower Bound */
60 #define XCC_REG_RANGE_0_HIGH 0x3400     /* XCC gfxdec0 upper Bound */
61 #define XCC_REG_RANGE_1_LOW  0xA000     /* XCC gfxdec1 lower Bound */
62 #define XCC_REG_RANGE_1_HIGH 0x10000    /* XCC gfxdec1 upper Bound */
63 
64 #define NORMALIZE_XCC_REG_OFFSET(offset) \
65 	(offset & 0xFFFF)
66 
67 static const struct amdgpu_hwip_reg_entry gc_reg_list_9_4_3[] = {
68 	SOC15_REG_ENTRY_STR(GC, 0, regGRBM_STATUS),
69 	SOC15_REG_ENTRY_STR(GC, 0, regGRBM_STATUS2),
70 	SOC15_REG_ENTRY_STR(GC, 0, regCP_STALLED_STAT1),
71 	SOC15_REG_ENTRY_STR(GC, 0, regCP_STALLED_STAT2),
72 	SOC15_REG_ENTRY_STR(GC, 0, regCP_CPC_STALLED_STAT1),
73 	SOC15_REG_ENTRY_STR(GC, 0, regCP_CPF_STALLED_STAT1),
74 	SOC15_REG_ENTRY_STR(GC, 0, regCP_BUSY_STAT),
75 	SOC15_REG_ENTRY_STR(GC, 0, regCP_CPC_BUSY_STAT),
76 	SOC15_REG_ENTRY_STR(GC, 0, regCP_CPF_BUSY_STAT),
77 	SOC15_REG_ENTRY_STR(GC, 0, regCP_CPF_STATUS),
78 	SOC15_REG_ENTRY_STR(GC, 0, regCP_GFX_ERROR),
79 	SOC15_REG_ENTRY_STR(GC, 0, regCPF_UTCL1_STATUS),
80 	SOC15_REG_ENTRY_STR(GC, 0, regCPC_UTCL1_STATUS),
81 	SOC15_REG_ENTRY_STR(GC, 0, regCPG_UTCL1_STATUS),
82 	SOC15_REG_ENTRY_STR(GC, 0, regGDS_PROTECTION_FAULT),
83 	SOC15_REG_ENTRY_STR(GC, 0, regGDS_VM_PROTECTION_FAULT),
84 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_UTCL1_STATUS),
85 	SOC15_REG_ENTRY_STR(GC, 0, regRMI_UTCL1_STATUS),
86 	SOC15_REG_ENTRY_STR(GC, 0, regSQC_DCACHE_UTCL1_STATUS),
87 	SOC15_REG_ENTRY_STR(GC, 0, regSQC_ICACHE_UTCL1_STATUS),
88 	SOC15_REG_ENTRY_STR(GC, 0, regSQ_UTCL1_STATUS),
89 	SOC15_REG_ENTRY_STR(GC, 0, regTCP_UTCL1_STATUS),
90 	SOC15_REG_ENTRY_STR(GC, 0, regWD_UTCL1_STATUS),
91 	SOC15_REG_ENTRY_STR(GC, 0, regVM_L2_PROTECTION_FAULT_CNTL),
92 	SOC15_REG_ENTRY_STR(GC, 0, regVM_L2_PROTECTION_FAULT_STATUS),
93 	SOC15_REG_ENTRY_STR(GC, 0, regCP_DEBUG),
94 	SOC15_REG_ENTRY_STR(GC, 0, regCP_MEC_CNTL),
95 	SOC15_REG_ENTRY_STR(GC, 0, regCP_MEC1_INSTR_PNTR),
96 	SOC15_REG_ENTRY_STR(GC, 0, regCP_MEC2_INSTR_PNTR),
97 	SOC15_REG_ENTRY_STR(GC, 0, regCP_CPC_STATUS),
98 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_STAT),
99 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_SMU_COMMAND),
100 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_SMU_MESSAGE),
101 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_SMU_ARGUMENT_1),
102 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_SMU_ARGUMENT_2),
103 	SOC15_REG_ENTRY_STR(GC, 0, regSMU_RLC_RESPONSE),
104 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_SAFE_MODE),
105 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_SMU_SAFE_MODE),
106 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_INT_STAT),
107 	SOC15_REG_ENTRY_STR(GC, 0, regRLC_GPM_GENERAL_6),
108 	/* cp header registers */
109 	SOC15_REG_ENTRY_STR(GC, 0, regCP_MEC_ME1_HEADER_DUMP),
110 	SOC15_REG_ENTRY_STR(GC, 0, regCP_MEC_ME2_HEADER_DUMP),
111 	/* SE status registers */
112 	SOC15_REG_ENTRY_STR(GC, 0, regGRBM_STATUS_SE0),
113 	SOC15_REG_ENTRY_STR(GC, 0, regGRBM_STATUS_SE1),
114 	SOC15_REG_ENTRY_STR(GC, 0, regGRBM_STATUS_SE2),
115 	SOC15_REG_ENTRY_STR(GC, 0, regGRBM_STATUS_SE3)
116 };
117 
118 static const struct amdgpu_hwip_reg_entry gc_cp_reg_list_9_4_3[] = {
119 	/* compute queue registers */
120 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_VMID),
121 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_ACTIVE),
122 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PERSISTENT_STATE),
123 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PIPE_PRIORITY),
124 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_QUEUE_PRIORITY),
125 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_QUANTUM),
126 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_BASE),
127 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_BASE_HI),
128 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_RPTR),
129 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_WPTR_POLL_ADDR),
130 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_WPTR_POLL_ADDR_HI),
131 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL),
132 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_CONTROL),
133 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_IB_BASE_ADDR),
134 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_IB_BASE_ADDR_HI),
135 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_IB_RPTR),
136 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_IB_CONTROL),
137 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_DEQUEUE_REQUEST),
138 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_BASE_ADDR),
139 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_BASE_ADDR_HI),
140 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_CONTROL),
141 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_RPTR),
142 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_WPTR),
143 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_EVENTS),
144 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_CTX_SAVE_BASE_ADDR_LO),
145 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_CTX_SAVE_BASE_ADDR_HI),
146 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_CTX_SAVE_CONTROL),
147 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_CNTL_STACK_OFFSET),
148 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_CNTL_STACK_SIZE),
149 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_WG_STATE_OFFSET),
150 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_CTX_SAVE_SIZE),
151 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_GDS_RESOURCE_STATE),
152 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_ERROR),
153 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_EOP_WPTR_MEM),
154 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_WPTR_LO),
155 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_PQ_WPTR_HI),
156 	SOC15_REG_ENTRY_STR(GC, 0, regCP_HQD_GFX_STATUS),
157 };
158 
159 struct amdgpu_gfx_ras gfx_v9_4_3_ras;
160 
161 static void gfx_v9_4_3_set_ring_funcs(struct amdgpu_device *adev);
162 static void gfx_v9_4_3_set_irq_funcs(struct amdgpu_device *adev);
163 static void gfx_v9_4_3_set_gds_init(struct amdgpu_device *adev);
164 static void gfx_v9_4_3_set_rlc_funcs(struct amdgpu_device *adev);
165 static int gfx_v9_4_3_get_cu_info(struct amdgpu_device *adev,
166 				struct amdgpu_cu_info *cu_info);
167 static void gfx_v9_4_3_xcc_set_safe_mode(struct amdgpu_device *adev, int xcc_id);
168 static void gfx_v9_4_3_xcc_unset_safe_mode(struct amdgpu_device *adev, int xcc_id);
169 
170 static void gfx_v9_4_3_kiq_set_resources(struct amdgpu_ring *kiq_ring,
171 				uint64_t queue_mask)
172 {
173 	struct amdgpu_device *adev = kiq_ring->adev;
174 	u64 shader_mc_addr;
175 
176 	/* Cleaner shader MC address */
177 	shader_mc_addr = adev->gfx.cleaner_shader_gpu_addr >> 8;
178 
179 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_RESOURCES, 6));
180 	amdgpu_ring_write(kiq_ring,
181 		PACKET3_SET_RESOURCES_VMID_MASK(0) |
182 		/* vmid_mask:0* queue_type:0 (KIQ) */
183 		PACKET3_SET_RESOURCES_QUEUE_TYPE(0));
184 	amdgpu_ring_write(kiq_ring,
185 			lower_32_bits(queue_mask));	/* queue mask lo */
186 	amdgpu_ring_write(kiq_ring,
187 			upper_32_bits(queue_mask));	/* queue mask hi */
188 	amdgpu_ring_write(kiq_ring, lower_32_bits(shader_mc_addr)); /* cleaner shader addr lo */
189 	amdgpu_ring_write(kiq_ring, upper_32_bits(shader_mc_addr)); /* cleaner shader addr hi */
190 	amdgpu_ring_write(kiq_ring, 0);	/* oac mask */
191 	amdgpu_ring_write(kiq_ring, 0);	/* gds heap base:0, gds heap size:0 */
192 }
193 
194 static void gfx_v9_4_3_kiq_map_queues(struct amdgpu_ring *kiq_ring,
195 				 struct amdgpu_ring *ring)
196 {
197 	struct amdgpu_device *adev = kiq_ring->adev;
198 	uint64_t mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
199 	uint64_t wptr_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
200 	uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
201 
202 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_MAP_QUEUES, 5));
203 	/* Q_sel:0, vmid:0, vidmem: 1, engine:0, num_Q:1*/
204 	amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
205 			 PACKET3_MAP_QUEUES_QUEUE_SEL(0) | /* Queue_Sel */
206 			 PACKET3_MAP_QUEUES_VMID(0) | /* VMID */
207 			 PACKET3_MAP_QUEUES_QUEUE(ring->queue) |
208 			 PACKET3_MAP_QUEUES_PIPE(ring->pipe) |
209 			 PACKET3_MAP_QUEUES_ME((ring->me == 1 ? 0 : 1)) |
210 			 /*queue_type: normal compute queue */
211 			 PACKET3_MAP_QUEUES_QUEUE_TYPE(0) |
212 			 /* alloc format: all_on_one_pipe */
213 			 PACKET3_MAP_QUEUES_ALLOC_FORMAT(0) |
214 			 PACKET3_MAP_QUEUES_ENGINE_SEL(eng_sel) |
215 			 /* num_queues: must be 1 */
216 			 PACKET3_MAP_QUEUES_NUM_QUEUES(1));
217 	amdgpu_ring_write(kiq_ring,
218 			PACKET3_MAP_QUEUES_DOORBELL_OFFSET(ring->doorbell_index));
219 	amdgpu_ring_write(kiq_ring, lower_32_bits(mqd_addr));
220 	amdgpu_ring_write(kiq_ring, upper_32_bits(mqd_addr));
221 	amdgpu_ring_write(kiq_ring, lower_32_bits(wptr_addr));
222 	amdgpu_ring_write(kiq_ring, upper_32_bits(wptr_addr));
223 }
224 
225 static void gfx_v9_4_3_kiq_unmap_queues(struct amdgpu_ring *kiq_ring,
226 				   struct amdgpu_ring *ring,
227 				   enum amdgpu_unmap_queues_action action,
228 				   u64 gpu_addr, u64 seq)
229 {
230 	uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
231 
232 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_UNMAP_QUEUES, 4));
233 	amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
234 			  PACKET3_UNMAP_QUEUES_ACTION(action) |
235 			  PACKET3_UNMAP_QUEUES_QUEUE_SEL(0) |
236 			  PACKET3_UNMAP_QUEUES_ENGINE_SEL(eng_sel) |
237 			  PACKET3_UNMAP_QUEUES_NUM_QUEUES(1));
238 	amdgpu_ring_write(kiq_ring,
239 			PACKET3_UNMAP_QUEUES_DOORBELL_OFFSET0(ring->doorbell_index));
240 
241 	if (action == PREEMPT_QUEUES_NO_UNMAP) {
242 		amdgpu_ring_write(kiq_ring, lower_32_bits(gpu_addr));
243 		amdgpu_ring_write(kiq_ring, upper_32_bits(gpu_addr));
244 		amdgpu_ring_write(kiq_ring, seq);
245 	} else {
246 		amdgpu_ring_write(kiq_ring, 0);
247 		amdgpu_ring_write(kiq_ring, 0);
248 		amdgpu_ring_write(kiq_ring, 0);
249 	}
250 }
251 
252 static void gfx_v9_4_3_kiq_query_status(struct amdgpu_ring *kiq_ring,
253 				   struct amdgpu_ring *ring,
254 				   u64 addr,
255 				   u64 seq)
256 {
257 	uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
258 
259 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_QUERY_STATUS, 5));
260 	amdgpu_ring_write(kiq_ring,
261 			  PACKET3_QUERY_STATUS_CONTEXT_ID(0) |
262 			  PACKET3_QUERY_STATUS_INTERRUPT_SEL(0) |
263 			  PACKET3_QUERY_STATUS_COMMAND(2));
264 	/* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
265 	amdgpu_ring_write(kiq_ring,
266 			PACKET3_QUERY_STATUS_DOORBELL_OFFSET(ring->doorbell_index) |
267 			PACKET3_QUERY_STATUS_ENG_SEL(eng_sel));
268 	amdgpu_ring_write(kiq_ring, lower_32_bits(addr));
269 	amdgpu_ring_write(kiq_ring, upper_32_bits(addr));
270 	amdgpu_ring_write(kiq_ring, lower_32_bits(seq));
271 	amdgpu_ring_write(kiq_ring, upper_32_bits(seq));
272 }
273 
274 static void gfx_v9_4_3_kiq_invalidate_tlbs(struct amdgpu_ring *kiq_ring,
275 				uint16_t pasid, uint32_t flush_type,
276 				bool all_hub)
277 {
278 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_INVALIDATE_TLBS, 0));
279 	amdgpu_ring_write(kiq_ring,
280 			PACKET3_INVALIDATE_TLBS_DST_SEL(1) |
281 			PACKET3_INVALIDATE_TLBS_ALL_HUB(all_hub) |
282 			PACKET3_INVALIDATE_TLBS_PASID(pasid) |
283 			PACKET3_INVALIDATE_TLBS_FLUSH_TYPE(flush_type));
284 }
285 
286 static void gfx_v9_4_3_kiq_reset_hw_queue(struct amdgpu_ring *kiq_ring, uint32_t queue_type,
287 					  uint32_t me_id, uint32_t pipe_id, uint32_t queue_id,
288 					  uint32_t xcc_id, uint32_t vmid)
289 {
290 	struct amdgpu_device *adev = kiq_ring->adev;
291 	unsigned i;
292 
293 	/* enter save mode */
294 	amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id);
295 	mutex_lock(&adev->srbm_mutex);
296 	soc15_grbm_select(adev, me_id, pipe_id, queue_id, 0, xcc_id);
297 
298 	if (queue_type == AMDGPU_RING_TYPE_COMPUTE) {
299 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST, 0x2);
300 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regSPI_COMPUTE_QUEUE_RESET, 0x1);
301 		/* wait till dequeue take effects */
302 		for (i = 0; i < adev->usec_timeout; i++) {
303 			if (!(RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1))
304 				break;
305 			udelay(1);
306 		}
307 		if (i >= adev->usec_timeout)
308 			dev_err(adev->dev, "fail to wait on hqd deactive\n");
309 	} else {
310 		dev_err(adev->dev, "reset queue_type(%d) not supported\n\n", queue_type);
311 	}
312 
313 	soc15_grbm_select(adev, 0, 0, 0, 0, 0);
314 	mutex_unlock(&adev->srbm_mutex);
315 	/* exit safe mode */
316 	amdgpu_gfx_rlc_exit_safe_mode(adev, xcc_id);
317 }
318 
319 static const struct kiq_pm4_funcs gfx_v9_4_3_kiq_pm4_funcs = {
320 	.kiq_set_resources = gfx_v9_4_3_kiq_set_resources,
321 	.kiq_map_queues = gfx_v9_4_3_kiq_map_queues,
322 	.kiq_unmap_queues = gfx_v9_4_3_kiq_unmap_queues,
323 	.kiq_query_status = gfx_v9_4_3_kiq_query_status,
324 	.kiq_invalidate_tlbs = gfx_v9_4_3_kiq_invalidate_tlbs,
325 	.kiq_reset_hw_queue = gfx_v9_4_3_kiq_reset_hw_queue,
326 	.set_resources_size = 8,
327 	.map_queues_size = 7,
328 	.unmap_queues_size = 6,
329 	.query_status_size = 7,
330 	.invalidate_tlbs_size = 2,
331 };
332 
333 static void gfx_v9_4_3_set_kiq_pm4_funcs(struct amdgpu_device *adev)
334 {
335 	int i, num_xcc;
336 
337 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
338 	for (i = 0; i < num_xcc; i++)
339 		adev->gfx.kiq[i].pmf = &gfx_v9_4_3_kiq_pm4_funcs;
340 }
341 
342 static void gfx_v9_4_3_init_golden_registers(struct amdgpu_device *adev)
343 {
344 	int i, num_xcc, dev_inst;
345 
346 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
347 	for (i = 0; i < num_xcc; i++) {
348 		dev_inst = GET_INST(GC, i);
349 
350 		WREG32_SOC15(GC, dev_inst, regGB_ADDR_CONFIG,
351 			     GOLDEN_GB_ADDR_CONFIG);
352 		/* Golden settings applied by driver for ASIC with rev_id 0 */
353 		if (adev->rev_id == 0) {
354 			WREG32_FIELD15_PREREG(GC, dev_inst, TCP_UTCL1_CNTL1,
355 					      REDUCE_FIFO_DEPTH_BY_2, 2);
356 		} else {
357 			WREG32_FIELD15_PREREG(GC, dev_inst, TCP_UTCL1_CNTL2,
358 						SPARE, 0x1);
359 		}
360 	}
361 }
362 
363 static uint32_t gfx_v9_4_3_normalize_xcc_reg_offset(uint32_t reg)
364 {
365 	uint32_t normalized_reg = NORMALIZE_XCC_REG_OFFSET(reg);
366 
367 	/* If it is an XCC reg, normalize the reg to keep
368 	   lower 16 bits in local xcc */
369 
370 	if (((normalized_reg >= XCC_REG_RANGE_0_LOW) && (normalized_reg < XCC_REG_RANGE_0_HIGH)) ||
371 		((normalized_reg >= XCC_REG_RANGE_1_LOW) && (normalized_reg < XCC_REG_RANGE_1_HIGH)))
372 		return normalized_reg;
373 	else
374 		return reg;
375 }
376 
377 static void gfx_v9_4_3_write_data_to_reg(struct amdgpu_ring *ring, int eng_sel,
378 				       bool wc, uint32_t reg, uint32_t val)
379 {
380 	reg = gfx_v9_4_3_normalize_xcc_reg_offset(reg);
381 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
382 	amdgpu_ring_write(ring, WRITE_DATA_ENGINE_SEL(eng_sel) |
383 				WRITE_DATA_DST_SEL(0) |
384 				(wc ? WR_CONFIRM : 0));
385 	amdgpu_ring_write(ring, reg);
386 	amdgpu_ring_write(ring, 0);
387 	amdgpu_ring_write(ring, val);
388 }
389 
390 static void gfx_v9_4_3_wait_reg_mem(struct amdgpu_ring *ring, int eng_sel,
391 				  int mem_space, int opt, uint32_t addr0,
392 				  uint32_t addr1, uint32_t ref, uint32_t mask,
393 				  uint32_t inv)
394 {
395 	/* Only do the normalization on regspace */
396 	if (mem_space == 0) {
397 		addr0 = gfx_v9_4_3_normalize_xcc_reg_offset(addr0);
398 		addr1 = gfx_v9_4_3_normalize_xcc_reg_offset(addr1);
399 	}
400 
401 	amdgpu_ring_write(ring, PACKET3(PACKET3_WAIT_REG_MEM, 5));
402 	amdgpu_ring_write(ring,
403 				 /* memory (1) or register (0) */
404 				 (WAIT_REG_MEM_MEM_SPACE(mem_space) |
405 				 WAIT_REG_MEM_OPERATION(opt) | /* wait */
406 				 WAIT_REG_MEM_FUNCTION(3) |  /* equal */
407 				 WAIT_REG_MEM_ENGINE(eng_sel)));
408 
409 	if (mem_space)
410 		BUG_ON(addr0 & 0x3); /* Dword align */
411 	amdgpu_ring_write(ring, addr0);
412 	amdgpu_ring_write(ring, addr1);
413 	amdgpu_ring_write(ring, ref);
414 	amdgpu_ring_write(ring, mask);
415 	amdgpu_ring_write(ring, inv); /* poll interval */
416 }
417 
418 static int gfx_v9_4_3_ring_test_ring(struct amdgpu_ring *ring)
419 {
420 	uint32_t scratch_reg0_offset, xcc_offset;
421 	struct amdgpu_device *adev = ring->adev;
422 	uint32_t tmp = 0;
423 	unsigned i;
424 	int r;
425 
426 	/* Use register offset which is local to XCC in the packet */
427 	xcc_offset = SOC15_REG_OFFSET(GC, 0, regSCRATCH_REG0);
428 	scratch_reg0_offset = SOC15_REG_OFFSET(GC, GET_INST(GC, ring->xcc_id), regSCRATCH_REG0);
429 	WREG32(scratch_reg0_offset, 0xCAFEDEAD);
430 	tmp = RREG32(scratch_reg0_offset);
431 
432 	r = amdgpu_ring_alloc(ring, 3);
433 	if (r)
434 		return r;
435 
436 	amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
437 	amdgpu_ring_write(ring, xcc_offset - PACKET3_SET_UCONFIG_REG_START);
438 	amdgpu_ring_write(ring, 0xDEADBEEF);
439 	amdgpu_ring_commit(ring);
440 
441 	for (i = 0; i < adev->usec_timeout; i++) {
442 		tmp = RREG32(scratch_reg0_offset);
443 		if (tmp == 0xDEADBEEF)
444 			break;
445 		udelay(1);
446 	}
447 
448 	if (i >= adev->usec_timeout)
449 		r = -ETIMEDOUT;
450 	return r;
451 }
452 
453 static int gfx_v9_4_3_ring_test_ib(struct amdgpu_ring *ring, long timeout)
454 {
455 	struct amdgpu_device *adev = ring->adev;
456 	struct amdgpu_ib ib;
457 	struct dma_fence *f = NULL;
458 
459 	unsigned index;
460 	uint64_t gpu_addr;
461 	uint32_t tmp;
462 	long r;
463 
464 	r = amdgpu_device_wb_get(adev, &index);
465 	if (r)
466 		return r;
467 
468 	gpu_addr = adev->wb.gpu_addr + (index * 4);
469 	adev->wb.wb[index] = cpu_to_le32(0xCAFEDEAD);
470 	memset(&ib, 0, sizeof(ib));
471 
472 	r = amdgpu_ib_get(adev, NULL, 20, AMDGPU_IB_POOL_DIRECT, &ib);
473 	if (r)
474 		goto err1;
475 
476 	ib.ptr[0] = PACKET3(PACKET3_WRITE_DATA, 3);
477 	ib.ptr[1] = WRITE_DATA_DST_SEL(5) | WR_CONFIRM;
478 	ib.ptr[2] = lower_32_bits(gpu_addr);
479 	ib.ptr[3] = upper_32_bits(gpu_addr);
480 	ib.ptr[4] = 0xDEADBEEF;
481 	ib.length_dw = 5;
482 
483 	r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
484 	if (r)
485 		goto err2;
486 
487 	r = dma_fence_wait_timeout(f, false, timeout);
488 	if (r == 0) {
489 		r = -ETIMEDOUT;
490 		goto err2;
491 	} else if (r < 0) {
492 		goto err2;
493 	}
494 
495 	tmp = adev->wb.wb[index];
496 	if (tmp == 0xDEADBEEF)
497 		r = 0;
498 	else
499 		r = -EINVAL;
500 
501 err2:
502 	amdgpu_ib_free(adev, &ib, NULL);
503 	dma_fence_put(f);
504 err1:
505 	amdgpu_device_wb_free(adev, index);
506 	return r;
507 }
508 
509 
510 /* This value might differs per partition */
511 static uint64_t gfx_v9_4_3_get_gpu_clock_counter(struct amdgpu_device *adev)
512 {
513 	uint64_t clock;
514 
515 	mutex_lock(&adev->gfx.gpu_clock_mutex);
516 	WREG32_SOC15(GC, GET_INST(GC, 0), regRLC_CAPTURE_GPU_CLOCK_COUNT, 1);
517 	clock = (uint64_t)RREG32_SOC15(GC, GET_INST(GC, 0), regRLC_GPU_CLOCK_COUNT_LSB) |
518 		((uint64_t)RREG32_SOC15(GC, GET_INST(GC, 0), regRLC_GPU_CLOCK_COUNT_MSB) << 32ULL);
519 	mutex_unlock(&adev->gfx.gpu_clock_mutex);
520 
521 	return clock;
522 }
523 
524 static void gfx_v9_4_3_free_microcode(struct amdgpu_device *adev)
525 {
526 	amdgpu_ucode_release(&adev->gfx.pfp_fw);
527 	amdgpu_ucode_release(&adev->gfx.me_fw);
528 	amdgpu_ucode_release(&adev->gfx.ce_fw);
529 	amdgpu_ucode_release(&adev->gfx.rlc_fw);
530 	amdgpu_ucode_release(&adev->gfx.mec_fw);
531 	amdgpu_ucode_release(&adev->gfx.mec2_fw);
532 
533 	kfree(adev->gfx.rlc.register_list_format);
534 }
535 
536 static int gfx_v9_4_3_init_rlc_microcode(struct amdgpu_device *adev,
537 					  const char *chip_name)
538 {
539 	int err;
540 	const struct rlc_firmware_header_v2_0 *rlc_hdr;
541 	uint16_t version_major;
542 	uint16_t version_minor;
543 
544 
545 	err = amdgpu_ucode_request(adev, &adev->gfx.rlc_fw,
546 				   "amdgpu/%s_rlc.bin", chip_name);
547 	if (err)
548 		goto out;
549 	rlc_hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
550 
551 	version_major = le16_to_cpu(rlc_hdr->header.header_version_major);
552 	version_minor = le16_to_cpu(rlc_hdr->header.header_version_minor);
553 	err = amdgpu_gfx_rlc_init_microcode(adev, version_major, version_minor);
554 out:
555 	if (err)
556 		amdgpu_ucode_release(&adev->gfx.rlc_fw);
557 
558 	return err;
559 }
560 
561 static bool gfx_v9_4_3_should_disable_gfxoff(struct pci_dev *pdev)
562 {
563 	return true;
564 }
565 
566 static void gfx_v9_4_3_check_if_need_gfxoff(struct amdgpu_device *adev)
567 {
568 	if (gfx_v9_4_3_should_disable_gfxoff(adev->pdev))
569 		adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
570 }
571 
572 static int gfx_v9_4_3_init_cp_compute_microcode(struct amdgpu_device *adev,
573 					  const char *chip_name)
574 {
575 	int err;
576 
577 	err = amdgpu_ucode_request(adev, &adev->gfx.mec_fw,
578 				   "amdgpu/%s_mec.bin", chip_name);
579 	if (err)
580 		goto out;
581 	amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_MEC1);
582 	amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_MEC1_JT);
583 
584 	adev->gfx.mec2_fw_version = adev->gfx.mec_fw_version;
585 	adev->gfx.mec2_feature_version = adev->gfx.mec_feature_version;
586 
587 	gfx_v9_4_3_check_if_need_gfxoff(adev);
588 
589 out:
590 	if (err)
591 		amdgpu_ucode_release(&adev->gfx.mec_fw);
592 	return err;
593 }
594 
595 static int gfx_v9_4_3_init_microcode(struct amdgpu_device *adev)
596 {
597 	char ucode_prefix[15];
598 	int r;
599 
600 	amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix, sizeof(ucode_prefix));
601 
602 	r = gfx_v9_4_3_init_rlc_microcode(adev, ucode_prefix);
603 	if (r)
604 		return r;
605 
606 	r = gfx_v9_4_3_init_cp_compute_microcode(adev, ucode_prefix);
607 	if (r)
608 		return r;
609 
610 	return r;
611 }
612 
613 static void gfx_v9_4_3_mec_fini(struct amdgpu_device *adev)
614 {
615 	amdgpu_bo_free_kernel(&adev->gfx.mec.hpd_eop_obj, NULL, NULL);
616 	amdgpu_bo_free_kernel(&adev->gfx.mec.mec_fw_obj, NULL, NULL);
617 }
618 
619 static int gfx_v9_4_3_mec_init(struct amdgpu_device *adev)
620 {
621 	int r, i, num_xcc;
622 	u32 *hpd;
623 	const __le32 *fw_data;
624 	unsigned fw_size;
625 	u32 *fw;
626 	size_t mec_hpd_size;
627 
628 	const struct gfx_firmware_header_v1_0 *mec_hdr;
629 
630 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
631 	for (i = 0; i < num_xcc; i++)
632 		bitmap_zero(adev->gfx.mec_bitmap[i].queue_bitmap,
633 			AMDGPU_MAX_COMPUTE_QUEUES);
634 
635 	/* take ownership of the relevant compute queues */
636 	amdgpu_gfx_compute_queue_acquire(adev);
637 	mec_hpd_size =
638 		adev->gfx.num_compute_rings * num_xcc * GFX9_MEC_HPD_SIZE;
639 	if (mec_hpd_size) {
640 		r = amdgpu_bo_create_reserved(adev, mec_hpd_size, PAGE_SIZE,
641 					      AMDGPU_GEM_DOMAIN_VRAM |
642 					      AMDGPU_GEM_DOMAIN_GTT,
643 					      &adev->gfx.mec.hpd_eop_obj,
644 					      &adev->gfx.mec.hpd_eop_gpu_addr,
645 					      (void **)&hpd);
646 		if (r) {
647 			dev_warn(adev->dev, "(%d) create HDP EOP bo failed\n", r);
648 			gfx_v9_4_3_mec_fini(adev);
649 			return r;
650 		}
651 
652 		if (amdgpu_emu_mode == 1) {
653 			for (i = 0; i < mec_hpd_size / 4; i++) {
654 				memset((void *)(hpd + i), 0, 4);
655 				if (i % 50 == 0)
656 					msleep(1);
657 			}
658 		} else {
659 			memset(hpd, 0, mec_hpd_size);
660 		}
661 
662 		amdgpu_bo_kunmap(adev->gfx.mec.hpd_eop_obj);
663 		amdgpu_bo_unreserve(adev->gfx.mec.hpd_eop_obj);
664 	}
665 
666 	mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
667 
668 	fw_data = (const __le32 *)
669 		(adev->gfx.mec_fw->data +
670 		 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
671 	fw_size = le32_to_cpu(mec_hdr->header.ucode_size_bytes);
672 
673 	r = amdgpu_bo_create_reserved(adev, mec_hdr->header.ucode_size_bytes,
674 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
675 				      &adev->gfx.mec.mec_fw_obj,
676 				      &adev->gfx.mec.mec_fw_gpu_addr,
677 				      (void **)&fw);
678 	if (r) {
679 		dev_warn(adev->dev, "(%d) create mec firmware bo failed\n", r);
680 		gfx_v9_4_3_mec_fini(adev);
681 		return r;
682 	}
683 
684 	memcpy(fw, fw_data, fw_size);
685 
686 	amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_obj);
687 	amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_obj);
688 
689 	return 0;
690 }
691 
692 static void gfx_v9_4_3_xcc_select_se_sh(struct amdgpu_device *adev, u32 se_num,
693 					u32 sh_num, u32 instance, int xcc_id)
694 {
695 	u32 data;
696 
697 	if (instance == 0xffffffff)
698 		data = REG_SET_FIELD(0, GRBM_GFX_INDEX,
699 				     INSTANCE_BROADCAST_WRITES, 1);
700 	else
701 		data = REG_SET_FIELD(0, GRBM_GFX_INDEX,
702 				     INSTANCE_INDEX, instance);
703 
704 	if (se_num == 0xffffffff)
705 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
706 				     SE_BROADCAST_WRITES, 1);
707 	else
708 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_INDEX, se_num);
709 
710 	if (sh_num == 0xffffffff)
711 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
712 				     SH_BROADCAST_WRITES, 1);
713 	else
714 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SH_INDEX, sh_num);
715 
716 	WREG32_SOC15_RLC_SHADOW_EX(reg, GC, GET_INST(GC, xcc_id), regGRBM_GFX_INDEX, data);
717 }
718 
719 static uint32_t wave_read_ind(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd, uint32_t wave, uint32_t address)
720 {
721 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regSQ_IND_INDEX,
722 		(wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
723 		(simd << SQ_IND_INDEX__SIMD_ID__SHIFT) |
724 		(address << SQ_IND_INDEX__INDEX__SHIFT) |
725 		(SQ_IND_INDEX__FORCE_READ_MASK));
726 	return RREG32_SOC15(GC, GET_INST(GC, xcc_id), regSQ_IND_DATA);
727 }
728 
729 static void wave_read_regs(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd,
730 			   uint32_t wave, uint32_t thread,
731 			   uint32_t regno, uint32_t num, uint32_t *out)
732 {
733 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regSQ_IND_INDEX,
734 		(wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
735 		(simd << SQ_IND_INDEX__SIMD_ID__SHIFT) |
736 		(regno << SQ_IND_INDEX__INDEX__SHIFT) |
737 		(thread << SQ_IND_INDEX__THREAD_ID__SHIFT) |
738 		(SQ_IND_INDEX__FORCE_READ_MASK) |
739 		(SQ_IND_INDEX__AUTO_INCR_MASK));
740 	while (num--)
741 		*(out++) = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regSQ_IND_DATA);
742 }
743 
744 static void gfx_v9_4_3_read_wave_data(struct amdgpu_device *adev,
745 				      uint32_t xcc_id, uint32_t simd, uint32_t wave,
746 				      uint32_t *dst, int *no_fields)
747 {
748 	/* type 1 wave data */
749 	dst[(*no_fields)++] = 1;
750 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_STATUS);
751 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_PC_LO);
752 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_PC_HI);
753 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_EXEC_LO);
754 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_EXEC_HI);
755 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_HW_ID);
756 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_INST_DW0);
757 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_INST_DW1);
758 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_GPR_ALLOC);
759 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_LDS_ALLOC);
760 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_TRAPSTS);
761 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_IB_STS);
762 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_IB_DBG0);
763 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_M0);
764 	dst[(*no_fields)++] = wave_read_ind(adev, xcc_id, simd, wave, ixSQ_WAVE_MODE);
765 }
766 
767 static void gfx_v9_4_3_read_wave_sgprs(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd,
768 				       uint32_t wave, uint32_t start,
769 				       uint32_t size, uint32_t *dst)
770 {
771 	wave_read_regs(adev, xcc_id, simd, wave, 0,
772 		       start + SQIND_WAVE_SGPRS_OFFSET, size, dst);
773 }
774 
775 static void gfx_v9_4_3_read_wave_vgprs(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd,
776 				       uint32_t wave, uint32_t thread,
777 				       uint32_t start, uint32_t size,
778 				       uint32_t *dst)
779 {
780 	wave_read_regs(adev, xcc_id, simd, wave, thread,
781 		       start + SQIND_WAVE_VGPRS_OFFSET, size, dst);
782 }
783 
784 static void gfx_v9_4_3_select_me_pipe_q(struct amdgpu_device *adev,
785 					u32 me, u32 pipe, u32 q, u32 vm, u32 xcc_id)
786 {
787 	soc15_grbm_select(adev, me, pipe, q, vm, GET_INST(GC, xcc_id));
788 }
789 
790 static int gfx_v9_4_3_get_xccs_per_xcp(struct amdgpu_device *adev)
791 {
792 	u32 xcp_ctl;
793 
794 	/* Value is expected to be the same on all, fetch from first instance */
795 	xcp_ctl = RREG32_SOC15(GC, GET_INST(GC, 0), regCP_HYP_XCP_CTL);
796 
797 	return REG_GET_FIELD(xcp_ctl, CP_HYP_XCP_CTL, NUM_XCC_IN_XCP);
798 }
799 
800 static int gfx_v9_4_3_switch_compute_partition(struct amdgpu_device *adev,
801 						int num_xccs_per_xcp)
802 {
803 	int ret, i, num_xcc;
804 	u32 tmp = 0;
805 
806 	if (adev->psp.funcs) {
807 		ret = psp_spatial_partition(&adev->psp,
808 					    NUM_XCC(adev->gfx.xcc_mask) /
809 						    num_xccs_per_xcp);
810 		if (ret)
811 			return ret;
812 	} else {
813 		num_xcc = NUM_XCC(adev->gfx.xcc_mask);
814 
815 		for (i = 0; i < num_xcc; i++) {
816 			tmp = REG_SET_FIELD(tmp, CP_HYP_XCP_CTL, NUM_XCC_IN_XCP,
817 					    num_xccs_per_xcp);
818 			tmp = REG_SET_FIELD(tmp, CP_HYP_XCP_CTL, VIRTUAL_XCC_ID,
819 					    i % num_xccs_per_xcp);
820 			WREG32_SOC15(GC, GET_INST(GC, i), regCP_HYP_XCP_CTL,
821 				     tmp);
822 		}
823 		ret = 0;
824 	}
825 
826 	adev->gfx.num_xcc_per_xcp = num_xccs_per_xcp;
827 
828 	return ret;
829 }
830 
831 static int gfx_v9_4_3_ih_to_xcc_inst(struct amdgpu_device *adev, int ih_node)
832 {
833 	int xcc;
834 
835 	xcc = hweight8(adev->gfx.xcc_mask & GENMASK(ih_node / 2, 0));
836 	if (!xcc) {
837 		dev_err(adev->dev, "Couldn't find xcc mapping from IH node");
838 		return -EINVAL;
839 	}
840 
841 	return xcc - 1;
842 }
843 
844 static const struct amdgpu_gfx_funcs gfx_v9_4_3_gfx_funcs = {
845 	.get_gpu_clock_counter = &gfx_v9_4_3_get_gpu_clock_counter,
846 	.select_se_sh = &gfx_v9_4_3_xcc_select_se_sh,
847 	.read_wave_data = &gfx_v9_4_3_read_wave_data,
848 	.read_wave_sgprs = &gfx_v9_4_3_read_wave_sgprs,
849 	.read_wave_vgprs = &gfx_v9_4_3_read_wave_vgprs,
850 	.select_me_pipe_q = &gfx_v9_4_3_select_me_pipe_q,
851 	.switch_partition_mode = &gfx_v9_4_3_switch_compute_partition,
852 	.ih_node_to_logical_xcc = &gfx_v9_4_3_ih_to_xcc_inst,
853 	.get_xccs_per_xcp = &gfx_v9_4_3_get_xccs_per_xcp,
854 };
855 
856 static int gfx_v9_4_3_aca_bank_parser(struct aca_handle *handle,
857 				      struct aca_bank *bank, enum aca_smu_type type,
858 				      void *data)
859 {
860 	struct aca_bank_info info;
861 	u64 misc0;
862 	u32 instlo;
863 	int ret;
864 
865 	ret = aca_bank_info_decode(bank, &info);
866 	if (ret)
867 		return ret;
868 
869 	/* NOTE: overwrite info.die_id with xcd id for gfx */
870 	instlo = ACA_REG__IPID__INSTANCEIDLO(bank->regs[ACA_REG_IDX_IPID]);
871 	instlo &= GENMASK(31, 1);
872 	info.die_id = instlo == mmSMNAID_XCD0_MCA_SMU ? 0 : 1;
873 
874 	misc0 = bank->regs[ACA_REG_IDX_MISC0];
875 
876 	switch (type) {
877 	case ACA_SMU_TYPE_UE:
878 		ret = aca_error_cache_log_bank_error(handle, &info,
879 						     ACA_ERROR_TYPE_UE, 1ULL);
880 		break;
881 	case ACA_SMU_TYPE_CE:
882 		ret = aca_error_cache_log_bank_error(handle, &info,
883 						     ACA_ERROR_TYPE_CE, ACA_REG__MISC0__ERRCNT(misc0));
884 		break;
885 	default:
886 		return -EINVAL;
887 	}
888 
889 	return ret;
890 }
891 
892 static bool gfx_v9_4_3_aca_bank_is_valid(struct aca_handle *handle, struct aca_bank *bank,
893 					 enum aca_smu_type type, void *data)
894 {
895 	u32 instlo;
896 
897 	instlo = ACA_REG__IPID__INSTANCEIDLO(bank->regs[ACA_REG_IDX_IPID]);
898 	instlo &= GENMASK(31, 1);
899 	switch (instlo) {
900 	case mmSMNAID_XCD0_MCA_SMU:
901 	case mmSMNAID_XCD1_MCA_SMU:
902 	case mmSMNXCD_XCD0_MCA_SMU:
903 		return true;
904 	default:
905 		break;
906 	}
907 
908 	return false;
909 }
910 
911 static const struct aca_bank_ops gfx_v9_4_3_aca_bank_ops = {
912 	.aca_bank_parser = gfx_v9_4_3_aca_bank_parser,
913 	.aca_bank_is_valid = gfx_v9_4_3_aca_bank_is_valid,
914 };
915 
916 static const struct aca_info gfx_v9_4_3_aca_info = {
917 	.hwip = ACA_HWIP_TYPE_SMU,
918 	.mask = ACA_ERROR_UE_MASK | ACA_ERROR_CE_MASK,
919 	.bank_ops = &gfx_v9_4_3_aca_bank_ops,
920 };
921 
922 static int gfx_v9_4_3_gpu_early_init(struct amdgpu_device *adev)
923 {
924 	u32 gb_addr_config;
925 
926 	adev->gfx.funcs = &gfx_v9_4_3_gfx_funcs;
927 	adev->gfx.ras = &gfx_v9_4_3_ras;
928 
929 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
930 	case IP_VERSION(9, 4, 3):
931 	case IP_VERSION(9, 4, 4):
932 		adev->gfx.config.max_hw_contexts = 8;
933 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
934 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
935 		adev->gfx.config.sc_hiz_tile_fifo_size = 0x30;
936 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
937 		gb_addr_config = RREG32_SOC15(GC, GET_INST(GC, 0), regGB_ADDR_CONFIG);
938 		break;
939 	default:
940 		BUG();
941 		break;
942 	}
943 
944 	adev->gfx.config.gb_addr_config = gb_addr_config;
945 
946 	adev->gfx.config.gb_addr_config_fields.num_pipes = 1 <<
947 			REG_GET_FIELD(
948 					adev->gfx.config.gb_addr_config,
949 					GB_ADDR_CONFIG,
950 					NUM_PIPES);
951 
952 	adev->gfx.config.max_tile_pipes =
953 		adev->gfx.config.gb_addr_config_fields.num_pipes;
954 
955 	adev->gfx.config.gb_addr_config_fields.num_banks = 1 <<
956 			REG_GET_FIELD(
957 					adev->gfx.config.gb_addr_config,
958 					GB_ADDR_CONFIG,
959 					NUM_BANKS);
960 	adev->gfx.config.gb_addr_config_fields.max_compress_frags = 1 <<
961 			REG_GET_FIELD(
962 					adev->gfx.config.gb_addr_config,
963 					GB_ADDR_CONFIG,
964 					MAX_COMPRESSED_FRAGS);
965 	adev->gfx.config.gb_addr_config_fields.num_rb_per_se = 1 <<
966 			REG_GET_FIELD(
967 					adev->gfx.config.gb_addr_config,
968 					GB_ADDR_CONFIG,
969 					NUM_RB_PER_SE);
970 	adev->gfx.config.gb_addr_config_fields.num_se = 1 <<
971 			REG_GET_FIELD(
972 					adev->gfx.config.gb_addr_config,
973 					GB_ADDR_CONFIG,
974 					NUM_SHADER_ENGINES);
975 	adev->gfx.config.gb_addr_config_fields.pipe_interleave_size = 1 << (8 +
976 			REG_GET_FIELD(
977 					adev->gfx.config.gb_addr_config,
978 					GB_ADDR_CONFIG,
979 					PIPE_INTERLEAVE_SIZE));
980 
981 	return 0;
982 }
983 
984 static int gfx_v9_4_3_compute_ring_init(struct amdgpu_device *adev, int ring_id,
985 				        int xcc_id, int mec, int pipe, int queue)
986 {
987 	unsigned irq_type;
988 	struct amdgpu_ring *ring = &adev->gfx.compute_ring[ring_id];
989 	unsigned int hw_prio;
990 	uint32_t xcc_doorbell_start;
991 
992 	ring = &adev->gfx.compute_ring[xcc_id * adev->gfx.num_compute_rings +
993 				       ring_id];
994 
995 	/* mec0 is me1 */
996 	ring->xcc_id = xcc_id;
997 	ring->me = mec + 1;
998 	ring->pipe = pipe;
999 	ring->queue = queue;
1000 
1001 	ring->ring_obj = NULL;
1002 	ring->use_doorbell = true;
1003 	xcc_doorbell_start = adev->doorbell_index.mec_ring0 +
1004 			     xcc_id * adev->doorbell_index.xcc_doorbell_range;
1005 	ring->doorbell_index = (xcc_doorbell_start + ring_id) << 1;
1006 	ring->eop_gpu_addr = adev->gfx.mec.hpd_eop_gpu_addr +
1007 			     (ring_id + xcc_id * adev->gfx.num_compute_rings) *
1008 				     GFX9_MEC_HPD_SIZE;
1009 	ring->vm_hub = AMDGPU_GFXHUB(xcc_id);
1010 	sprintf(ring->name, "comp_%d.%d.%d.%d",
1011 			ring->xcc_id, ring->me, ring->pipe, ring->queue);
1012 
1013 	irq_type = AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP
1014 		+ ((ring->me - 1) * adev->gfx.mec.num_pipe_per_mec)
1015 		+ ring->pipe;
1016 	hw_prio = amdgpu_gfx_is_high_priority_compute_queue(adev, ring) ?
1017 			AMDGPU_GFX_PIPE_PRIO_HIGH : AMDGPU_GFX_PIPE_PRIO_NORMAL;
1018 	/* type-2 packets are deprecated on MEC, use type-3 instead */
1019 	return amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq, irq_type,
1020 				hw_prio, NULL);
1021 }
1022 
1023 static void gfx_v9_4_3_alloc_ip_dump(struct amdgpu_device *adev)
1024 {
1025 	uint32_t reg_count = ARRAY_SIZE(gc_reg_list_9_4_3);
1026 	uint32_t *ptr, num_xcc, inst;
1027 
1028 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1029 
1030 	ptr = kcalloc(reg_count * num_xcc, sizeof(uint32_t), GFP_KERNEL);
1031 	if (!ptr) {
1032 		DRM_ERROR("Failed to allocate memory for GFX IP Dump\n");
1033 		adev->gfx.ip_dump_core = NULL;
1034 	} else {
1035 		adev->gfx.ip_dump_core = ptr;
1036 	}
1037 
1038 	/* Allocate memory for compute queue registers for all the instances */
1039 	reg_count = ARRAY_SIZE(gc_cp_reg_list_9_4_3);
1040 	inst = adev->gfx.mec.num_mec * adev->gfx.mec.num_pipe_per_mec *
1041 		adev->gfx.mec.num_queue_per_pipe;
1042 
1043 	ptr = kcalloc(reg_count * inst * num_xcc, sizeof(uint32_t), GFP_KERNEL);
1044 	if (!ptr) {
1045 		DRM_ERROR("Failed to allocate memory for Compute Queues IP Dump\n");
1046 		adev->gfx.ip_dump_compute_queues = NULL;
1047 	} else {
1048 		adev->gfx.ip_dump_compute_queues = ptr;
1049 	}
1050 }
1051 
1052 static int gfx_v9_4_3_sw_init(struct amdgpu_ip_block *ip_block)
1053 {
1054 	int i, j, k, r, ring_id, xcc_id, num_xcc;
1055 	struct amdgpu_device *adev = ip_block->adev;
1056 
1057 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1058 	case IP_VERSION(9, 4, 3):
1059 	case IP_VERSION(9, 4, 4):
1060 		adev->gfx.cleaner_shader_ptr = gfx_9_4_3_cleaner_shader_hex;
1061 		adev->gfx.cleaner_shader_size = sizeof(gfx_9_4_3_cleaner_shader_hex);
1062 		if (adev->gfx.mec_fw_version >= 153) {
1063 			adev->gfx.enable_cleaner_shader = true;
1064 			r = amdgpu_gfx_cleaner_shader_sw_init(adev, adev->gfx.cleaner_shader_size);
1065 			if (r) {
1066 				adev->gfx.enable_cleaner_shader = false;
1067 				dev_err(adev->dev, "Failed to initialize cleaner shader\n");
1068 			}
1069 		}
1070 		break;
1071 	default:
1072 		adev->gfx.enable_cleaner_shader = false;
1073 		break;
1074 	}
1075 
1076 	adev->gfx.mec.num_mec = 2;
1077 	adev->gfx.mec.num_pipe_per_mec = 4;
1078 	adev->gfx.mec.num_queue_per_pipe = 8;
1079 
1080 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1081 
1082 	/* EOP Event */
1083 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_EOP_INTERRUPT, &adev->gfx.eop_irq);
1084 	if (r)
1085 		return r;
1086 
1087 	/* Bad opcode Event */
1088 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP,
1089 			      GFX_9_0__SRCID__CP_BAD_OPCODE_ERROR,
1090 			      &adev->gfx.bad_op_irq);
1091 	if (r)
1092 		return r;
1093 
1094 	/* Privileged reg */
1095 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_PRIV_REG_FAULT,
1096 			      &adev->gfx.priv_reg_irq);
1097 	if (r)
1098 		return r;
1099 
1100 	/* Privileged inst */
1101 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_PRIV_INSTR_FAULT,
1102 			      &adev->gfx.priv_inst_irq);
1103 	if (r)
1104 		return r;
1105 
1106 	adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
1107 
1108 	r = adev->gfx.rlc.funcs->init(adev);
1109 	if (r) {
1110 		DRM_ERROR("Failed to init rlc BOs!\n");
1111 		return r;
1112 	}
1113 
1114 	r = gfx_v9_4_3_mec_init(adev);
1115 	if (r) {
1116 		DRM_ERROR("Failed to init MEC BOs!\n");
1117 		return r;
1118 	}
1119 
1120 	/* set up the compute queues - allocate horizontally across pipes */
1121 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1122 		ring_id = 0;
1123 		for (i = 0; i < adev->gfx.mec.num_mec; ++i) {
1124 			for (j = 0; j < adev->gfx.mec.num_queue_per_pipe; j++) {
1125 				for (k = 0; k < adev->gfx.mec.num_pipe_per_mec;
1126 				     k++) {
1127 					if (!amdgpu_gfx_is_mec_queue_enabled(
1128 							adev, xcc_id, i, k, j))
1129 						continue;
1130 
1131 					r = gfx_v9_4_3_compute_ring_init(adev,
1132 								       ring_id,
1133 								       xcc_id,
1134 								       i, k, j);
1135 					if (r)
1136 						return r;
1137 
1138 					ring_id++;
1139 				}
1140 			}
1141 		}
1142 
1143 		r = amdgpu_gfx_kiq_init(adev, GFX9_MEC_HPD_SIZE, xcc_id);
1144 		if (r) {
1145 			DRM_ERROR("Failed to init KIQ BOs!\n");
1146 			return r;
1147 		}
1148 
1149 		r = amdgpu_gfx_kiq_init_ring(adev, xcc_id);
1150 		if (r)
1151 			return r;
1152 
1153 		/* create MQD for all compute queues as wel as KIQ for SRIOV case */
1154 		r = amdgpu_gfx_mqd_sw_init(adev,
1155 				sizeof(struct v9_mqd_allocation), xcc_id);
1156 		if (r)
1157 			return r;
1158 	}
1159 
1160 	adev->gfx.compute_supported_reset =
1161 		amdgpu_get_soft_full_reset_mask(&adev->gfx.compute_ring[0]);
1162 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1163 	case IP_VERSION(9, 4, 3):
1164 	case IP_VERSION(9, 4, 4):
1165 		if (adev->gfx.mec_fw_version >= 155) {
1166 			adev->gfx.compute_supported_reset |= AMDGPU_RESET_TYPE_PER_QUEUE;
1167 			adev->gfx.compute_supported_reset |= AMDGPU_RESET_TYPE_PER_PIPE;
1168 		}
1169 		break;
1170 	default:
1171 		break;
1172 	}
1173 	r = gfx_v9_4_3_gpu_early_init(adev);
1174 	if (r)
1175 		return r;
1176 
1177 	r = amdgpu_gfx_ras_sw_init(adev);
1178 	if (r)
1179 		return r;
1180 
1181 	r = amdgpu_gfx_sysfs_init(adev);
1182 	if (r)
1183 		return r;
1184 
1185 	gfx_v9_4_3_alloc_ip_dump(adev);
1186 
1187 	return 0;
1188 }
1189 
1190 static int gfx_v9_4_3_sw_fini(struct amdgpu_ip_block *ip_block)
1191 {
1192 	int i, num_xcc;
1193 	struct amdgpu_device *adev = ip_block->adev;
1194 
1195 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1196 	for (i = 0; i < adev->gfx.num_compute_rings * num_xcc; i++)
1197 		amdgpu_ring_fini(&adev->gfx.compute_ring[i]);
1198 
1199 	for (i = 0; i < num_xcc; i++) {
1200 		amdgpu_gfx_mqd_sw_fini(adev, i);
1201 		amdgpu_gfx_kiq_free_ring(&adev->gfx.kiq[i].ring);
1202 		amdgpu_gfx_kiq_fini(adev, i);
1203 	}
1204 
1205 	amdgpu_gfx_cleaner_shader_sw_fini(adev);
1206 
1207 	gfx_v9_4_3_mec_fini(adev);
1208 	amdgpu_bo_unref(&adev->gfx.rlc.clear_state_obj);
1209 	gfx_v9_4_3_free_microcode(adev);
1210 	amdgpu_gfx_sysfs_fini(adev);
1211 
1212 	kfree(adev->gfx.ip_dump_core);
1213 	kfree(adev->gfx.ip_dump_compute_queues);
1214 
1215 	return 0;
1216 }
1217 
1218 #define DEFAULT_SH_MEM_BASES	(0x6000)
1219 static void gfx_v9_4_3_xcc_init_compute_vmid(struct amdgpu_device *adev,
1220 					     int xcc_id)
1221 {
1222 	int i;
1223 	uint32_t sh_mem_config;
1224 	uint32_t sh_mem_bases;
1225 	uint32_t data;
1226 
1227 	/*
1228 	 * Configure apertures:
1229 	 * LDS:         0x60000000'00000000 - 0x60000001'00000000 (4GB)
1230 	 * Scratch:     0x60000001'00000000 - 0x60000002'00000000 (4GB)
1231 	 * GPUVM:       0x60010000'00000000 - 0x60020000'00000000 (1TB)
1232 	 */
1233 	sh_mem_bases = DEFAULT_SH_MEM_BASES | (DEFAULT_SH_MEM_BASES << 16);
1234 
1235 	sh_mem_config = SH_MEM_ADDRESS_MODE_64 |
1236 			SH_MEM_ALIGNMENT_MODE_UNALIGNED <<
1237 			SH_MEM_CONFIG__ALIGNMENT_MODE__SHIFT;
1238 
1239 	mutex_lock(&adev->srbm_mutex);
1240 	for (i = adev->vm_manager.first_kfd_vmid; i < AMDGPU_NUM_VMID; i++) {
1241 		soc15_grbm_select(adev, 0, 0, 0, i, GET_INST(GC, xcc_id));
1242 		/* CP and shaders */
1243 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regSH_MEM_CONFIG, sh_mem_config);
1244 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regSH_MEM_BASES, sh_mem_bases);
1245 
1246 		/* Enable trap for each kfd vmid. */
1247 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regSPI_GDBG_PER_VMID_CNTL);
1248 		data = REG_SET_FIELD(data, SPI_GDBG_PER_VMID_CNTL, TRAP_EN, 1);
1249 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regSPI_GDBG_PER_VMID_CNTL, data);
1250 	}
1251 	soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
1252 	mutex_unlock(&adev->srbm_mutex);
1253 
1254 	/*
1255 	 * Initialize all compute VMIDs to have no GDS, GWS, or OA
1256 	 * access. These should be enabled by FW for target VMIDs.
1257 	 */
1258 	for (i = adev->vm_manager.first_kfd_vmid; i < AMDGPU_NUM_VMID; i++) {
1259 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_VMID0_BASE, 2 * i, 0);
1260 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_VMID0_SIZE, 2 * i, 0);
1261 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_GWS_VMID0, i, 0);
1262 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_OA_VMID0, i, 0);
1263 	}
1264 }
1265 
1266 static void gfx_v9_4_3_xcc_init_gds_vmid(struct amdgpu_device *adev, int xcc_id)
1267 {
1268 	int vmid;
1269 
1270 	/*
1271 	 * Initialize all compute and user-gfx VMIDs to have no GDS, GWS, or OA
1272 	 * access. Compute VMIDs should be enabled by FW for target VMIDs,
1273 	 * the driver can enable them for graphics. VMID0 should maintain
1274 	 * access so that HWS firmware can save/restore entries.
1275 	 */
1276 	for (vmid = 1; vmid < AMDGPU_NUM_VMID; vmid++) {
1277 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_VMID0_BASE, 2 * vmid, 0);
1278 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_VMID0_SIZE, 2 * vmid, 0);
1279 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_GWS_VMID0, vmid, 0);
1280 		WREG32_SOC15_OFFSET(GC, GET_INST(GC, xcc_id), regGDS_OA_VMID0, vmid, 0);
1281 	}
1282 }
1283 
1284 static void gfx_v9_4_3_xcc_constants_init(struct amdgpu_device *adev,
1285 					  int xcc_id)
1286 {
1287 	u32 tmp;
1288 	int i;
1289 
1290 	/* XXX SH_MEM regs */
1291 	/* where to put LDS, scratch, GPUVM in FSA64 space */
1292 	mutex_lock(&adev->srbm_mutex);
1293 	for (i = 0; i < adev->vm_manager.id_mgr[AMDGPU_GFXHUB(0)].num_ids; i++) {
1294 		soc15_grbm_select(adev, 0, 0, 0, i, GET_INST(GC, xcc_id));
1295 		/* CP and shaders */
1296 		if (i == 0) {
1297 			tmp = REG_SET_FIELD(0, SH_MEM_CONFIG, ALIGNMENT_MODE,
1298 					    SH_MEM_ALIGNMENT_MODE_UNALIGNED);
1299 			tmp = REG_SET_FIELD(tmp, SH_MEM_CONFIG, RETRY_DISABLE,
1300 					    !!adev->gmc.noretry);
1301 			WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id),
1302 					 regSH_MEM_CONFIG, tmp);
1303 			WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id),
1304 					 regSH_MEM_BASES, 0);
1305 		} else {
1306 			tmp = REG_SET_FIELD(0, SH_MEM_CONFIG, ALIGNMENT_MODE,
1307 					    SH_MEM_ALIGNMENT_MODE_UNALIGNED);
1308 			tmp = REG_SET_FIELD(tmp, SH_MEM_CONFIG, RETRY_DISABLE,
1309 					    !!adev->gmc.noretry);
1310 			WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id),
1311 					 regSH_MEM_CONFIG, tmp);
1312 			tmp = REG_SET_FIELD(0, SH_MEM_BASES, PRIVATE_BASE,
1313 					    (adev->gmc.private_aperture_start >>
1314 					     48));
1315 			tmp = REG_SET_FIELD(tmp, SH_MEM_BASES, SHARED_BASE,
1316 					    (adev->gmc.shared_aperture_start >>
1317 					     48));
1318 			WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id),
1319 					 regSH_MEM_BASES, tmp);
1320 		}
1321 	}
1322 	soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, 0));
1323 
1324 	mutex_unlock(&adev->srbm_mutex);
1325 
1326 	gfx_v9_4_3_xcc_init_compute_vmid(adev, xcc_id);
1327 	gfx_v9_4_3_xcc_init_gds_vmid(adev, xcc_id);
1328 }
1329 
1330 static void gfx_v9_4_3_constants_init(struct amdgpu_device *adev)
1331 {
1332 	int i, num_xcc;
1333 
1334 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1335 
1336 	gfx_v9_4_3_get_cu_info(adev, &adev->gfx.cu_info);
1337 	adev->gfx.config.db_debug2 =
1338 		RREG32_SOC15(GC, GET_INST(GC, 0), regDB_DEBUG2);
1339 
1340 	for (i = 0; i < num_xcc; i++)
1341 		gfx_v9_4_3_xcc_constants_init(adev, i);
1342 }
1343 
1344 static void
1345 gfx_v9_4_3_xcc_enable_save_restore_machine(struct amdgpu_device *adev,
1346 					   int xcc_id)
1347 {
1348 	WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), RLC_SRM_CNTL, SRM_ENABLE, 1);
1349 }
1350 
1351 static void gfx_v9_4_3_xcc_init_pg(struct amdgpu_device *adev, int xcc_id)
1352 {
1353 	/*
1354 	 * Rlc save restore list is workable since v2_1.
1355 	 * And it's needed by gfxoff feature.
1356 	 */
1357 	if (adev->gfx.rlc.is_rlc_v2_1)
1358 		gfx_v9_4_3_xcc_enable_save_restore_machine(adev, xcc_id);
1359 }
1360 
1361 static void gfx_v9_4_3_xcc_disable_gpa_mode(struct amdgpu_device *adev, int xcc_id)
1362 {
1363 	uint32_t data;
1364 
1365 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCPC_PSP_DEBUG);
1366 	data |= CPC_PSP_DEBUG__UTCL2IUGPAOVERRIDE_MASK;
1367 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCPC_PSP_DEBUG, data);
1368 }
1369 
1370 static bool gfx_v9_4_3_is_rlc_enabled(struct amdgpu_device *adev)
1371 {
1372 	uint32_t rlc_setting;
1373 
1374 	/* if RLC is not enabled, do nothing */
1375 	rlc_setting = RREG32_SOC15(GC, GET_INST(GC, 0), regRLC_CNTL);
1376 	if (!(rlc_setting & RLC_CNTL__RLC_ENABLE_F32_MASK))
1377 		return false;
1378 
1379 	return true;
1380 }
1381 
1382 static void gfx_v9_4_3_xcc_set_safe_mode(struct amdgpu_device *adev, int xcc_id)
1383 {
1384 	uint32_t data;
1385 	unsigned i;
1386 
1387 	data = RLC_SAFE_MODE__CMD_MASK;
1388 	data |= (1 << RLC_SAFE_MODE__MESSAGE__SHIFT);
1389 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SAFE_MODE, data);
1390 
1391 	/* wait for RLC_SAFE_MODE */
1392 	for (i = 0; i < adev->usec_timeout; i++) {
1393 		if (!REG_GET_FIELD(RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SAFE_MODE), RLC_SAFE_MODE, CMD))
1394 			break;
1395 		udelay(1);
1396 	}
1397 }
1398 
1399 static void gfx_v9_4_3_xcc_unset_safe_mode(struct amdgpu_device *adev,
1400 					   int xcc_id)
1401 {
1402 	uint32_t data;
1403 
1404 	data = RLC_SAFE_MODE__CMD_MASK;
1405 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SAFE_MODE, data);
1406 }
1407 
1408 static void gfx_v9_4_3_init_rlcg_reg_access_ctrl(struct amdgpu_device *adev)
1409 {
1410 	int xcc_id, num_xcc;
1411 	struct amdgpu_rlcg_reg_access_ctrl *reg_access_ctrl;
1412 
1413 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1414 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1415 		reg_access_ctrl = &adev->gfx.rlc.reg_access_ctrl[GET_INST(GC, xcc_id)];
1416 		reg_access_ctrl->scratch_reg0 = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regSCRATCH_REG0);
1417 		reg_access_ctrl->scratch_reg1 = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regSCRATCH_REG1);
1418 		reg_access_ctrl->scratch_reg2 = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regSCRATCH_REG2);
1419 		reg_access_ctrl->scratch_reg3 = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regSCRATCH_REG3);
1420 		reg_access_ctrl->grbm_cntl = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regGRBM_GFX_CNTL);
1421 		reg_access_ctrl->grbm_idx = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regGRBM_GFX_INDEX);
1422 		reg_access_ctrl->spare_int = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPARE_INT);
1423 	}
1424 	adev->gfx.rlc.rlcg_reg_access_supported = true;
1425 }
1426 
1427 static int gfx_v9_4_3_rlc_init(struct amdgpu_device *adev)
1428 {
1429 	/* init spm vmid with 0xf */
1430 	if (adev->gfx.rlc.funcs->update_spm_vmid)
1431 		adev->gfx.rlc.funcs->update_spm_vmid(adev, NULL, 0xf);
1432 
1433 	return 0;
1434 }
1435 
1436 static void gfx_v9_4_3_xcc_wait_for_rlc_serdes(struct amdgpu_device *adev,
1437 					       int xcc_id)
1438 {
1439 	u32 i, j, k;
1440 	u32 mask;
1441 
1442 	mutex_lock(&adev->grbm_idx_mutex);
1443 	for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
1444 		for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
1445 			gfx_v9_4_3_xcc_select_se_sh(adev, i, j, 0xffffffff,
1446 						    xcc_id);
1447 			for (k = 0; k < adev->usec_timeout; k++) {
1448 				if (RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SERDES_CU_MASTER_BUSY) == 0)
1449 					break;
1450 				udelay(1);
1451 			}
1452 			if (k == adev->usec_timeout) {
1453 				gfx_v9_4_3_xcc_select_se_sh(adev, 0xffffffff,
1454 							    0xffffffff,
1455 							    0xffffffff, xcc_id);
1456 				mutex_unlock(&adev->grbm_idx_mutex);
1457 				DRM_INFO("Timeout wait for RLC serdes %u,%u\n",
1458 					 i, j);
1459 				return;
1460 			}
1461 		}
1462 	}
1463 	gfx_v9_4_3_xcc_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff,
1464 				    xcc_id);
1465 	mutex_unlock(&adev->grbm_idx_mutex);
1466 
1467 	mask = RLC_SERDES_NONCU_MASTER_BUSY__SE_MASTER_BUSY_MASK |
1468 		RLC_SERDES_NONCU_MASTER_BUSY__GC_MASTER_BUSY_MASK |
1469 		RLC_SERDES_NONCU_MASTER_BUSY__TC0_MASTER_BUSY_MASK |
1470 		RLC_SERDES_NONCU_MASTER_BUSY__TC1_MASTER_BUSY_MASK;
1471 	for (k = 0; k < adev->usec_timeout; k++) {
1472 		if ((RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SERDES_NONCU_MASTER_BUSY) & mask) == 0)
1473 			break;
1474 		udelay(1);
1475 	}
1476 }
1477 
1478 static void gfx_v9_4_3_xcc_enable_gui_idle_interrupt(struct amdgpu_device *adev,
1479 						     bool enable, int xcc_id)
1480 {
1481 	u32 tmp;
1482 
1483 	/* These interrupts should be enabled to drive DS clock */
1484 
1485 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_INT_CNTL_RING0);
1486 
1487 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_BUSY_INT_ENABLE, enable ? 1 : 0);
1488 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_EMPTY_INT_ENABLE, enable ? 1 : 0);
1489 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CMP_BUSY_INT_ENABLE, enable ? 1 : 0);
1490 
1491 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_INT_CNTL_RING0, tmp);
1492 }
1493 
1494 static void gfx_v9_4_3_xcc_rlc_stop(struct amdgpu_device *adev, int xcc_id)
1495 {
1496 	WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), RLC_CNTL,
1497 			      RLC_ENABLE_F32, 0);
1498 	gfx_v9_4_3_xcc_enable_gui_idle_interrupt(adev, false, xcc_id);
1499 	gfx_v9_4_3_xcc_wait_for_rlc_serdes(adev, xcc_id);
1500 }
1501 
1502 static void gfx_v9_4_3_rlc_stop(struct amdgpu_device *adev)
1503 {
1504 	int i, num_xcc;
1505 
1506 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1507 	for (i = 0; i < num_xcc; i++)
1508 		gfx_v9_4_3_xcc_rlc_stop(adev, i);
1509 }
1510 
1511 static void gfx_v9_4_3_xcc_rlc_reset(struct amdgpu_device *adev, int xcc_id)
1512 {
1513 	WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), GRBM_SOFT_RESET,
1514 			      SOFT_RESET_RLC, 1);
1515 	udelay(50);
1516 	WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), GRBM_SOFT_RESET,
1517 			      SOFT_RESET_RLC, 0);
1518 	udelay(50);
1519 }
1520 
1521 static void gfx_v9_4_3_rlc_reset(struct amdgpu_device *adev)
1522 {
1523 	int i, num_xcc;
1524 
1525 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1526 	for (i = 0; i < num_xcc; i++)
1527 		gfx_v9_4_3_xcc_rlc_reset(adev, i);
1528 }
1529 
1530 static void gfx_v9_4_3_xcc_rlc_start(struct amdgpu_device *adev, int xcc_id)
1531 {
1532 	WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), RLC_CNTL,
1533 			      RLC_ENABLE_F32, 1);
1534 	udelay(50);
1535 
1536 	/* carrizo do enable cp interrupt after cp inited */
1537 	if (!(adev->flags & AMD_IS_APU)) {
1538 		gfx_v9_4_3_xcc_enable_gui_idle_interrupt(adev, true, xcc_id);
1539 		udelay(50);
1540 	}
1541 }
1542 
1543 static void gfx_v9_4_3_rlc_start(struct amdgpu_device *adev)
1544 {
1545 #ifdef AMDGPU_RLC_DEBUG_RETRY
1546 	u32 rlc_ucode_ver;
1547 #endif
1548 	int i, num_xcc;
1549 
1550 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1551 	for (i = 0; i < num_xcc; i++) {
1552 		gfx_v9_4_3_xcc_rlc_start(adev, i);
1553 #ifdef AMDGPU_RLC_DEBUG_RETRY
1554 		/* RLC_GPM_GENERAL_6 : RLC Ucode version */
1555 		rlc_ucode_ver = RREG32_SOC15(GC, GET_INST(GC, i), regRLC_GPM_GENERAL_6);
1556 		if (rlc_ucode_ver == 0x108) {
1557 			dev_info(adev->dev,
1558 				 "Using rlc debug ucode. regRLC_GPM_GENERAL_6 ==0x08%x / fw_ver == %i \n",
1559 				 rlc_ucode_ver, adev->gfx.rlc_fw_version);
1560 			/* RLC_GPM_TIMER_INT_3 : Timer interval in RefCLK cycles,
1561 			 * default is 0x9C4 to create a 100us interval */
1562 			WREG32_SOC15(GC, GET_INST(GC, i), regRLC_GPM_TIMER_INT_3, 0x9C4);
1563 			/* RLC_GPM_GENERAL_12 : Minimum gap between wptr and rptr
1564 			 * to disable the page fault retry interrupts, default is
1565 			 * 0x100 (256) */
1566 			WREG32_SOC15(GC, GET_INST(GC, i), regRLC_GPM_GENERAL_12, 0x100);
1567 		}
1568 #endif
1569 	}
1570 }
1571 
1572 static int gfx_v9_4_3_xcc_rlc_load_microcode(struct amdgpu_device *adev,
1573 					     int xcc_id)
1574 {
1575 	const struct rlc_firmware_header_v2_0 *hdr;
1576 	const __le32 *fw_data;
1577 	unsigned i, fw_size;
1578 
1579 	if (!adev->gfx.rlc_fw)
1580 		return -EINVAL;
1581 
1582 	hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
1583 	amdgpu_ucode_print_rlc_hdr(&hdr->header);
1584 
1585 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1586 			   le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1587 	fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
1588 
1589 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_GPM_UCODE_ADDR,
1590 			RLCG_UCODE_LOADING_START_ADDRESS);
1591 	for (i = 0; i < fw_size; i++) {
1592 		if (amdgpu_emu_mode == 1 && i % 100 == 0) {
1593 			dev_info(adev->dev, "Write RLC ucode data %u DWs\n", i);
1594 			msleep(1);
1595 		}
1596 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_GPM_UCODE_DATA, le32_to_cpup(fw_data++));
1597 	}
1598 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_GPM_UCODE_ADDR, adev->gfx.rlc_fw_version);
1599 
1600 	return 0;
1601 }
1602 
1603 static int gfx_v9_4_3_xcc_rlc_resume(struct amdgpu_device *adev, int xcc_id)
1604 {
1605 	int r;
1606 
1607 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
1608 		gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
1609 		/* legacy rlc firmware loading */
1610 		r = gfx_v9_4_3_xcc_rlc_load_microcode(adev, xcc_id);
1611 		if (r)
1612 			return r;
1613 		gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
1614 	}
1615 
1616 	amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id);
1617 	/* disable CG */
1618 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGCG_CGLS_CTRL, 0);
1619 	gfx_v9_4_3_xcc_init_pg(adev, xcc_id);
1620 	amdgpu_gfx_rlc_exit_safe_mode(adev, xcc_id);
1621 
1622 	return 0;
1623 }
1624 
1625 static int gfx_v9_4_3_rlc_resume(struct amdgpu_device *adev)
1626 {
1627 	int r, i, num_xcc;
1628 
1629 	if (amdgpu_sriov_vf(adev))
1630 		return 0;
1631 
1632 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1633 	for (i = 0; i < num_xcc; i++) {
1634 		r = gfx_v9_4_3_xcc_rlc_resume(adev, i);
1635 		if (r)
1636 			return r;
1637 	}
1638 
1639 	return 0;
1640 }
1641 
1642 static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev, struct amdgpu_ring *ring,
1643 				       unsigned vmid)
1644 {
1645 	u32 reg, pre_data, data;
1646 
1647 	reg = SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regRLC_SPM_MC_CNTL);
1648 	if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev))
1649 		pre_data = RREG32_NO_KIQ(reg);
1650 	else
1651 		pre_data = RREG32(reg);
1652 
1653 	data =	pre_data & (~RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK);
1654 	data |= (vmid & RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK) << RLC_SPM_MC_CNTL__RLC_SPM_VMID__SHIFT;
1655 
1656 	if (pre_data != data) {
1657 		if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev)) {
1658 			WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, 0), regRLC_SPM_MC_CNTL, data);
1659 		} else
1660 			WREG32_SOC15(GC, GET_INST(GC, 0), regRLC_SPM_MC_CNTL, data);
1661 	}
1662 }
1663 
1664 static const struct soc15_reg_rlcg rlcg_access_gc_9_4_3[] = {
1665 	{SOC15_REG_ENTRY(GC, 0, regGRBM_GFX_INDEX)},
1666 	{SOC15_REG_ENTRY(GC, 0, regSQ_IND_INDEX)},
1667 };
1668 
1669 static bool gfx_v9_4_3_check_rlcg_range(struct amdgpu_device *adev,
1670 					uint32_t offset,
1671 					struct soc15_reg_rlcg *entries, int arr_size)
1672 {
1673 	int i, inst;
1674 	uint32_t reg;
1675 
1676 	if (!entries)
1677 		return false;
1678 
1679 	for (i = 0; i < arr_size; i++) {
1680 		const struct soc15_reg_rlcg *entry;
1681 
1682 		entry = &entries[i];
1683 		inst = adev->ip_map.logical_to_dev_inst ?
1684 			       adev->ip_map.logical_to_dev_inst(
1685 				       adev, entry->hwip, entry->instance) :
1686 			       entry->instance;
1687 		reg = adev->reg_offset[entry->hwip][inst][entry->segment] +
1688 		      entry->reg;
1689 		if (offset == reg)
1690 			return true;
1691 	}
1692 
1693 	return false;
1694 }
1695 
1696 static bool gfx_v9_4_3_is_rlcg_access_range(struct amdgpu_device *adev, u32 offset)
1697 {
1698 	return gfx_v9_4_3_check_rlcg_range(adev, offset,
1699 					(void *)rlcg_access_gc_9_4_3,
1700 					ARRAY_SIZE(rlcg_access_gc_9_4_3));
1701 }
1702 
1703 static void gfx_v9_4_3_xcc_cp_compute_enable(struct amdgpu_device *adev,
1704 					     bool enable, int xcc_id)
1705 {
1706 	if (enable) {
1707 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_MEC_CNTL, 0);
1708 	} else {
1709 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_MEC_CNTL,
1710 			(CP_MEC_CNTL__MEC_INVALIDATE_ICACHE_MASK |
1711 			 CP_MEC_CNTL__MEC_ME1_PIPE0_RESET_MASK |
1712 			 CP_MEC_CNTL__MEC_ME1_PIPE1_RESET_MASK |
1713 			 CP_MEC_CNTL__MEC_ME1_PIPE2_RESET_MASK |
1714 			 CP_MEC_CNTL__MEC_ME1_PIPE3_RESET_MASK |
1715 			 CP_MEC_CNTL__MEC_ME2_PIPE0_RESET_MASK |
1716 			 CP_MEC_CNTL__MEC_ME2_PIPE1_RESET_MASK |
1717 			 CP_MEC_CNTL__MEC_ME1_HALT_MASK |
1718 			 CP_MEC_CNTL__MEC_ME2_HALT_MASK));
1719 		adev->gfx.kiq[xcc_id].ring.sched.ready = false;
1720 	}
1721 	udelay(50);
1722 }
1723 
1724 static int gfx_v9_4_3_xcc_cp_compute_load_microcode(struct amdgpu_device *adev,
1725 						    int xcc_id)
1726 {
1727 	const struct gfx_firmware_header_v1_0 *mec_hdr;
1728 	const __le32 *fw_data;
1729 	unsigned i;
1730 	u32 tmp;
1731 	u32 mec_ucode_addr_offset;
1732 	u32 mec_ucode_data_offset;
1733 
1734 	if (!adev->gfx.mec_fw)
1735 		return -EINVAL;
1736 
1737 	gfx_v9_4_3_xcc_cp_compute_enable(adev, false, xcc_id);
1738 
1739 	mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
1740 	amdgpu_ucode_print_gfx_hdr(&mec_hdr->header);
1741 
1742 	fw_data = (const __le32 *)
1743 		(adev->gfx.mec_fw->data +
1744 		 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
1745 	tmp = 0;
1746 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, VMID, 0);
1747 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
1748 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_CPC_IC_BASE_CNTL, tmp);
1749 
1750 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_CPC_IC_BASE_LO,
1751 		adev->gfx.mec.mec_fw_gpu_addr & 0xFFFFF000);
1752 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_CPC_IC_BASE_HI,
1753 		upper_32_bits(adev->gfx.mec.mec_fw_gpu_addr));
1754 
1755 	mec_ucode_addr_offset =
1756 		SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_MEC_ME1_UCODE_ADDR);
1757 	mec_ucode_data_offset =
1758 		SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_MEC_ME1_UCODE_DATA);
1759 
1760 	/* MEC1 */
1761 	WREG32(mec_ucode_addr_offset, mec_hdr->jt_offset);
1762 	for (i = 0; i < mec_hdr->jt_size; i++)
1763 		WREG32(mec_ucode_data_offset,
1764 		       le32_to_cpup(fw_data + mec_hdr->jt_offset + i));
1765 
1766 	WREG32(mec_ucode_addr_offset, adev->gfx.mec_fw_version);
1767 	/* Todo : Loading MEC2 firmware is only necessary if MEC2 should run different microcode than MEC1. */
1768 
1769 	return 0;
1770 }
1771 
1772 /* KIQ functions */
1773 static void gfx_v9_4_3_xcc_kiq_setting(struct amdgpu_ring *ring, int xcc_id)
1774 {
1775 	uint32_t tmp;
1776 	struct amdgpu_device *adev = ring->adev;
1777 
1778 	/* tell RLC which is KIQ queue */
1779 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CP_SCHEDULERS);
1780 	tmp &= 0xffffff00;
1781 	tmp |= (ring->me << 5) | (ring->pipe << 3) | (ring->queue);
1782 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regRLC_CP_SCHEDULERS, tmp);
1783 	tmp |= 0x80;
1784 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regRLC_CP_SCHEDULERS, tmp);
1785 }
1786 
1787 static void gfx_v9_4_3_mqd_set_priority(struct amdgpu_ring *ring, struct v9_mqd *mqd)
1788 {
1789 	struct amdgpu_device *adev = ring->adev;
1790 
1791 	if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) {
1792 		if (amdgpu_gfx_is_high_priority_compute_queue(adev, ring)) {
1793 			mqd->cp_hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_HIGH;
1794 			mqd->cp_hqd_queue_priority =
1795 				AMDGPU_GFX_QUEUE_PRIORITY_MAXIMUM;
1796 		}
1797 	}
1798 }
1799 
1800 static int gfx_v9_4_3_xcc_mqd_init(struct amdgpu_ring *ring, int xcc_id)
1801 {
1802 	struct amdgpu_device *adev = ring->adev;
1803 	struct v9_mqd *mqd = ring->mqd_ptr;
1804 	uint64_t hqd_gpu_addr, wb_gpu_addr, eop_base_addr;
1805 	uint32_t tmp;
1806 
1807 	mqd->header = 0xC0310800;
1808 	mqd->compute_pipelinestat_enable = 0x00000001;
1809 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
1810 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
1811 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
1812 	mqd->compute_static_thread_mgmt_se3 = 0xffffffff;
1813 	mqd->compute_misc_reserved = 0x00000003;
1814 
1815 	mqd->dynamic_cu_mask_addr_lo =
1816 		lower_32_bits(ring->mqd_gpu_addr
1817 			      + offsetof(struct v9_mqd_allocation, dynamic_cu_mask));
1818 	mqd->dynamic_cu_mask_addr_hi =
1819 		upper_32_bits(ring->mqd_gpu_addr
1820 			      + offsetof(struct v9_mqd_allocation, dynamic_cu_mask));
1821 
1822 	eop_base_addr = ring->eop_gpu_addr >> 8;
1823 	mqd->cp_hqd_eop_base_addr_lo = eop_base_addr;
1824 	mqd->cp_hqd_eop_base_addr_hi = upper_32_bits(eop_base_addr);
1825 
1826 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
1827 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_EOP_CONTROL);
1828 	tmp = REG_SET_FIELD(tmp, CP_HQD_EOP_CONTROL, EOP_SIZE,
1829 			(order_base_2(GFX9_MEC_HPD_SIZE / 4) - 1));
1830 
1831 	mqd->cp_hqd_eop_control = tmp;
1832 
1833 	/* enable doorbell? */
1834 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL);
1835 
1836 	if (ring->use_doorbell) {
1837 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1838 				    DOORBELL_OFFSET, ring->doorbell_index);
1839 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1840 				    DOORBELL_EN, 1);
1841 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1842 				    DOORBELL_SOURCE, 0);
1843 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1844 				    DOORBELL_HIT, 0);
1845 		if (amdgpu_sriov_vf(adev))
1846 			tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1847 					    DOORBELL_MODE, 1);
1848 	} else {
1849 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
1850 					 DOORBELL_EN, 0);
1851 	}
1852 
1853 	mqd->cp_hqd_pq_doorbell_control = tmp;
1854 
1855 	/* disable the queue if it's active */
1856 	ring->wptr = 0;
1857 	mqd->cp_hqd_dequeue_request = 0;
1858 	mqd->cp_hqd_pq_rptr = 0;
1859 	mqd->cp_hqd_pq_wptr_lo = 0;
1860 	mqd->cp_hqd_pq_wptr_hi = 0;
1861 
1862 	/* set the pointer to the MQD */
1863 	mqd->cp_mqd_base_addr_lo = ring->mqd_gpu_addr & 0xfffffffc;
1864 	mqd->cp_mqd_base_addr_hi = upper_32_bits(ring->mqd_gpu_addr);
1865 
1866 	/* set MQD vmid to 0 */
1867 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MQD_CONTROL);
1868 	tmp = REG_SET_FIELD(tmp, CP_MQD_CONTROL, VMID, 0);
1869 	mqd->cp_mqd_control = tmp;
1870 
1871 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
1872 	hqd_gpu_addr = ring->gpu_addr >> 8;
1873 	mqd->cp_hqd_pq_base_lo = hqd_gpu_addr;
1874 	mqd->cp_hqd_pq_base_hi = upper_32_bits(hqd_gpu_addr);
1875 
1876 	/* set up the HQD, this is similar to CP_RB0_CNTL */
1877 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_CONTROL);
1878 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, QUEUE_SIZE,
1879 			    (order_base_2(ring->ring_size / 4) - 1));
1880 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, RPTR_BLOCK_SIZE,
1881 			((order_base_2(AMDGPU_GPU_PAGE_SIZE / 4) - 1) << 8));
1882 #ifdef __BIG_ENDIAN
1883 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, ENDIAN_SWAP, 1);
1884 #endif
1885 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, UNORD_DISPATCH, 0);
1886 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, ROQ_PQ_IB_FLIP, 0);
1887 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, PRIV_STATE, 1);
1888 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, KMD_QUEUE, 1);
1889 	mqd->cp_hqd_pq_control = tmp;
1890 
1891 	/* set the wb address whether it's enabled or not */
1892 	wb_gpu_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
1893 	mqd->cp_hqd_pq_rptr_report_addr_lo = wb_gpu_addr & 0xfffffffc;
1894 	mqd->cp_hqd_pq_rptr_report_addr_hi =
1895 		upper_32_bits(wb_gpu_addr) & 0xffff;
1896 
1897 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
1898 	wb_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
1899 	mqd->cp_hqd_pq_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffffc;
1900 	mqd->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
1901 
1902 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
1903 	ring->wptr = 0;
1904 	mqd->cp_hqd_pq_rptr = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR);
1905 
1906 	/* set the vmid for the queue */
1907 	mqd->cp_hqd_vmid = 0;
1908 
1909 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_PERSISTENT_STATE);
1910 	tmp = REG_SET_FIELD(tmp, CP_HQD_PERSISTENT_STATE, PRELOAD_SIZE, 0x53);
1911 	mqd->cp_hqd_persistent_state = tmp;
1912 
1913 	/* set MIN_IB_AVAIL_SIZE */
1914 	tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_IB_CONTROL);
1915 	tmp = REG_SET_FIELD(tmp, CP_HQD_IB_CONTROL, MIN_IB_AVAIL_SIZE, 3);
1916 	mqd->cp_hqd_ib_control = tmp;
1917 
1918 	/* set static priority for a queue/ring */
1919 	gfx_v9_4_3_mqd_set_priority(ring, mqd);
1920 	mqd->cp_hqd_quantum = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_QUANTUM);
1921 
1922 	/* map_queues packet doesn't need activate the queue,
1923 	 * so only kiq need set this field.
1924 	 */
1925 	if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
1926 		mqd->cp_hqd_active = 1;
1927 
1928 	return 0;
1929 }
1930 
1931 static int gfx_v9_4_3_xcc_kiq_init_register(struct amdgpu_ring *ring,
1932 					    int xcc_id)
1933 {
1934 	struct amdgpu_device *adev = ring->adev;
1935 	struct v9_mqd *mqd = ring->mqd_ptr;
1936 	int j;
1937 
1938 	/* disable wptr polling */
1939 	WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), CP_PQ_WPTR_POLL_CNTL, EN, 0);
1940 
1941 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_EOP_BASE_ADDR,
1942 	       mqd->cp_hqd_eop_base_addr_lo);
1943 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_EOP_BASE_ADDR_HI,
1944 	       mqd->cp_hqd_eop_base_addr_hi);
1945 
1946 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
1947 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_EOP_CONTROL,
1948 	       mqd->cp_hqd_eop_control);
1949 
1950 	/* enable doorbell? */
1951 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL,
1952 	       mqd->cp_hqd_pq_doorbell_control);
1953 
1954 	/* disable the queue if it's active */
1955 	if (RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1) {
1956 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST, 1);
1957 		for (j = 0; j < adev->usec_timeout; j++) {
1958 			if (!(RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1))
1959 				break;
1960 			udelay(1);
1961 		}
1962 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST,
1963 		       mqd->cp_hqd_dequeue_request);
1964 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR,
1965 		       mqd->cp_hqd_pq_rptr);
1966 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_LO,
1967 		       mqd->cp_hqd_pq_wptr_lo);
1968 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_HI,
1969 		       mqd->cp_hqd_pq_wptr_hi);
1970 	}
1971 
1972 	/* set the pointer to the MQD */
1973 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_MQD_BASE_ADDR,
1974 	       mqd->cp_mqd_base_addr_lo);
1975 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_MQD_BASE_ADDR_HI,
1976 	       mqd->cp_mqd_base_addr_hi);
1977 
1978 	/* set MQD vmid to 0 */
1979 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_MQD_CONTROL,
1980 	       mqd->cp_mqd_control);
1981 
1982 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
1983 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_BASE,
1984 	       mqd->cp_hqd_pq_base_lo);
1985 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_BASE_HI,
1986 	       mqd->cp_hqd_pq_base_hi);
1987 
1988 	/* set up the HQD, this is similar to CP_RB0_CNTL */
1989 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_CONTROL,
1990 	       mqd->cp_hqd_pq_control);
1991 
1992 	/* set the wb address whether it's enabled or not */
1993 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR_REPORT_ADDR,
1994 				mqd->cp_hqd_pq_rptr_report_addr_lo);
1995 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR_REPORT_ADDR_HI,
1996 				mqd->cp_hqd_pq_rptr_report_addr_hi);
1997 
1998 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
1999 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_POLL_ADDR,
2000 	       mqd->cp_hqd_pq_wptr_poll_addr_lo);
2001 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_POLL_ADDR_HI,
2002 	       mqd->cp_hqd_pq_wptr_poll_addr_hi);
2003 
2004 	/* enable the doorbell if requested */
2005 	if (ring->use_doorbell) {
2006 		WREG32_SOC15(
2007 			GC, GET_INST(GC, xcc_id),
2008 			regCP_MEC_DOORBELL_RANGE_LOWER,
2009 			((adev->doorbell_index.kiq +
2010 			  xcc_id * adev->doorbell_index.xcc_doorbell_range) *
2011 			 2) << 2);
2012 		WREG32_SOC15(
2013 			GC, GET_INST(GC, xcc_id),
2014 			regCP_MEC_DOORBELL_RANGE_UPPER,
2015 			((adev->doorbell_index.userqueue_end +
2016 			  xcc_id * adev->doorbell_index.xcc_doorbell_range) *
2017 			 2) << 2);
2018 	}
2019 
2020 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL,
2021 	       mqd->cp_hqd_pq_doorbell_control);
2022 
2023 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
2024 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_LO,
2025 	       mqd->cp_hqd_pq_wptr_lo);
2026 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_HI,
2027 	       mqd->cp_hqd_pq_wptr_hi);
2028 
2029 	/* set the vmid for the queue */
2030 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_VMID, mqd->cp_hqd_vmid);
2031 
2032 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PERSISTENT_STATE,
2033 	       mqd->cp_hqd_persistent_state);
2034 
2035 	/* activate the queue */
2036 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE,
2037 	       mqd->cp_hqd_active);
2038 
2039 	if (ring->use_doorbell)
2040 		WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), CP_PQ_STATUS, DOORBELL_ENABLE, 1);
2041 
2042 	return 0;
2043 }
2044 
2045 static int gfx_v9_4_3_xcc_q_fini_register(struct amdgpu_ring *ring,
2046 					    int xcc_id)
2047 {
2048 	struct amdgpu_device *adev = ring->adev;
2049 	int j;
2050 
2051 	/* disable the queue if it's active */
2052 	if (RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1) {
2053 
2054 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST, 1);
2055 
2056 		for (j = 0; j < adev->usec_timeout; j++) {
2057 			if (!(RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1))
2058 				break;
2059 			udelay(1);
2060 		}
2061 
2062 		if (j == AMDGPU_MAX_USEC_TIMEOUT) {
2063 			DRM_DEBUG("%s dequeue request failed.\n", ring->name);
2064 
2065 			/* Manual disable if dequeue request times out */
2066 			WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE, 0);
2067 		}
2068 
2069 		WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_DEQUEUE_REQUEST,
2070 		      0);
2071 	}
2072 
2073 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_IQ_TIMER, 0);
2074 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_IB_CONTROL, 0);
2075 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PERSISTENT_STATE, CP_HQD_PERSISTENT_STATE_DEFAULT);
2076 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL, 0x40000000);
2077 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_DOORBELL_CONTROL, 0);
2078 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_RPTR, 0);
2079 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_HI, 0);
2080 	WREG32_SOC15_RLC(GC, GET_INST(GC, xcc_id), regCP_HQD_PQ_WPTR_LO, 0);
2081 
2082 	return 0;
2083 }
2084 
2085 static int gfx_v9_4_3_xcc_kiq_init_queue(struct amdgpu_ring *ring, int xcc_id)
2086 {
2087 	struct amdgpu_device *adev = ring->adev;
2088 	struct v9_mqd *mqd = ring->mqd_ptr;
2089 	struct v9_mqd *tmp_mqd;
2090 
2091 	gfx_v9_4_3_xcc_kiq_setting(ring, xcc_id);
2092 
2093 	/* GPU could be in bad state during probe, driver trigger the reset
2094 	 * after load the SMU, in this case , the mqd is not be initialized.
2095 	 * driver need to re-init the mqd.
2096 	 * check mqd->cp_hqd_pq_control since this value should not be 0
2097 	 */
2098 	tmp_mqd = (struct v9_mqd *)adev->gfx.kiq[xcc_id].mqd_backup;
2099 	if (amdgpu_in_reset(adev) && tmp_mqd->cp_hqd_pq_control) {
2100 		/* for GPU_RESET case , reset MQD to a clean status */
2101 		if (adev->gfx.kiq[xcc_id].mqd_backup)
2102 			memcpy(mqd, adev->gfx.kiq[xcc_id].mqd_backup, sizeof(struct v9_mqd_allocation));
2103 
2104 		/* reset ring buffer */
2105 		ring->wptr = 0;
2106 		amdgpu_ring_clear_ring(ring);
2107 		mutex_lock(&adev->srbm_mutex);
2108 		soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0, GET_INST(GC, xcc_id));
2109 		gfx_v9_4_3_xcc_kiq_init_register(ring, xcc_id);
2110 		soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
2111 		mutex_unlock(&adev->srbm_mutex);
2112 	} else {
2113 		memset((void *)mqd, 0, sizeof(struct v9_mqd_allocation));
2114 		((struct v9_mqd_allocation *)mqd)->dynamic_cu_mask = 0xFFFFFFFF;
2115 		((struct v9_mqd_allocation *)mqd)->dynamic_rb_mask = 0xFFFFFFFF;
2116 		mutex_lock(&adev->srbm_mutex);
2117 		if (amdgpu_sriov_vf(adev) && adev->in_suspend)
2118 			amdgpu_ring_clear_ring(ring);
2119 		soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0, GET_INST(GC, xcc_id));
2120 		gfx_v9_4_3_xcc_mqd_init(ring, xcc_id);
2121 		gfx_v9_4_3_xcc_kiq_init_register(ring, xcc_id);
2122 		soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
2123 		mutex_unlock(&adev->srbm_mutex);
2124 
2125 		if (adev->gfx.kiq[xcc_id].mqd_backup)
2126 			memcpy(adev->gfx.kiq[xcc_id].mqd_backup, mqd, sizeof(struct v9_mqd_allocation));
2127 	}
2128 
2129 	return 0;
2130 }
2131 
2132 static int gfx_v9_4_3_xcc_kcq_init_queue(struct amdgpu_ring *ring, int xcc_id, bool restore)
2133 {
2134 	struct amdgpu_device *adev = ring->adev;
2135 	struct v9_mqd *mqd = ring->mqd_ptr;
2136 	int mqd_idx = ring - &adev->gfx.compute_ring[0];
2137 	struct v9_mqd *tmp_mqd;
2138 
2139 	/* Same as above kiq init, driver need to re-init the mqd if mqd->cp_hqd_pq_control
2140 	 * is not be initialized before
2141 	 */
2142 	tmp_mqd = (struct v9_mqd *)adev->gfx.mec.mqd_backup[mqd_idx];
2143 
2144 	if (!restore && (!tmp_mqd->cp_hqd_pq_control ||
2145 	    (!amdgpu_in_reset(adev) && !adev->in_suspend))) {
2146 		memset((void *)mqd, 0, sizeof(struct v9_mqd_allocation));
2147 		((struct v9_mqd_allocation *)mqd)->dynamic_cu_mask = 0xFFFFFFFF;
2148 		((struct v9_mqd_allocation *)mqd)->dynamic_rb_mask = 0xFFFFFFFF;
2149 		mutex_lock(&adev->srbm_mutex);
2150 		soc15_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0, GET_INST(GC, xcc_id));
2151 		gfx_v9_4_3_xcc_mqd_init(ring, xcc_id);
2152 		soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
2153 		mutex_unlock(&adev->srbm_mutex);
2154 
2155 		if (adev->gfx.mec.mqd_backup[mqd_idx])
2156 			memcpy(adev->gfx.mec.mqd_backup[mqd_idx], mqd, sizeof(struct v9_mqd_allocation));
2157 	} else {
2158 		/* restore MQD to a clean status */
2159 		if (adev->gfx.mec.mqd_backup[mqd_idx])
2160 			memcpy(mqd, adev->gfx.mec.mqd_backup[mqd_idx], sizeof(struct v9_mqd_allocation));
2161 		/* reset ring buffer */
2162 		ring->wptr = 0;
2163 		atomic64_set((atomic64_t *)&adev->wb.wb[ring->wptr_offs], 0);
2164 		amdgpu_ring_clear_ring(ring);
2165 	}
2166 
2167 	return 0;
2168 }
2169 
2170 static int gfx_v9_4_3_xcc_kcq_fini_register(struct amdgpu_device *adev, int xcc_id)
2171 {
2172 	struct amdgpu_ring *ring;
2173 	int j;
2174 
2175 	for (j = 0; j < adev->gfx.num_compute_rings; j++) {
2176 		ring = &adev->gfx.compute_ring[j +  xcc_id * adev->gfx.num_compute_rings];
2177 		if (!amdgpu_in_reset(adev) && !adev->in_suspend) {
2178 			mutex_lock(&adev->srbm_mutex);
2179 			soc15_grbm_select(adev, ring->me,
2180 					ring->pipe,
2181 					ring->queue, 0, GET_INST(GC, xcc_id));
2182 			gfx_v9_4_3_xcc_q_fini_register(ring, xcc_id);
2183 			soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
2184 			mutex_unlock(&adev->srbm_mutex);
2185 		}
2186 	}
2187 
2188 	return 0;
2189 }
2190 
2191 static int gfx_v9_4_3_xcc_kiq_resume(struct amdgpu_device *adev, int xcc_id)
2192 {
2193 	struct amdgpu_ring *ring;
2194 	int r;
2195 
2196 	ring = &adev->gfx.kiq[xcc_id].ring;
2197 
2198 	r = amdgpu_bo_reserve(ring->mqd_obj, false);
2199 	if (unlikely(r != 0))
2200 		return r;
2201 
2202 	r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
2203 	if (unlikely(r != 0)) {
2204 		amdgpu_bo_unreserve(ring->mqd_obj);
2205 		return r;
2206 	}
2207 
2208 	gfx_v9_4_3_xcc_kiq_init_queue(ring, xcc_id);
2209 	amdgpu_bo_kunmap(ring->mqd_obj);
2210 	ring->mqd_ptr = NULL;
2211 	amdgpu_bo_unreserve(ring->mqd_obj);
2212 	return 0;
2213 }
2214 
2215 static int gfx_v9_4_3_xcc_kcq_resume(struct amdgpu_device *adev, int xcc_id)
2216 {
2217 	struct amdgpu_ring *ring = NULL;
2218 	int r = 0, i;
2219 
2220 	gfx_v9_4_3_xcc_cp_compute_enable(adev, true, xcc_id);
2221 
2222 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
2223 		ring = &adev->gfx.compute_ring[i + xcc_id * adev->gfx.num_compute_rings];
2224 
2225 		r = amdgpu_bo_reserve(ring->mqd_obj, false);
2226 		if (unlikely(r != 0))
2227 			goto done;
2228 		r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
2229 		if (!r) {
2230 			r = gfx_v9_4_3_xcc_kcq_init_queue(ring, xcc_id, false);
2231 			amdgpu_bo_kunmap(ring->mqd_obj);
2232 			ring->mqd_ptr = NULL;
2233 		}
2234 		amdgpu_bo_unreserve(ring->mqd_obj);
2235 		if (r)
2236 			goto done;
2237 	}
2238 
2239 	r = amdgpu_gfx_enable_kcq(adev, xcc_id);
2240 done:
2241 	return r;
2242 }
2243 
2244 static int gfx_v9_4_3_xcc_cp_resume(struct amdgpu_device *adev, int xcc_id)
2245 {
2246 	struct amdgpu_ring *ring;
2247 	int r, j;
2248 
2249 	gfx_v9_4_3_xcc_enable_gui_idle_interrupt(adev, false, xcc_id);
2250 
2251 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
2252 		gfx_v9_4_3_xcc_disable_gpa_mode(adev, xcc_id);
2253 
2254 		r = gfx_v9_4_3_xcc_cp_compute_load_microcode(adev, xcc_id);
2255 		if (r)
2256 			return r;
2257 	} else {
2258 		gfx_v9_4_3_xcc_cp_compute_enable(adev, false, xcc_id);
2259 	}
2260 
2261 	r = gfx_v9_4_3_xcc_kiq_resume(adev, xcc_id);
2262 	if (r)
2263 		return r;
2264 
2265 	r = gfx_v9_4_3_xcc_kcq_resume(adev, xcc_id);
2266 	if (r)
2267 		return r;
2268 
2269 	for (j = 0; j < adev->gfx.num_compute_rings; j++) {
2270 		ring = &adev->gfx.compute_ring
2271 				[j + xcc_id * adev->gfx.num_compute_rings];
2272 		r = amdgpu_ring_test_helper(ring);
2273 		if (r)
2274 			return r;
2275 	}
2276 
2277 	gfx_v9_4_3_xcc_enable_gui_idle_interrupt(adev, true, xcc_id);
2278 
2279 	return 0;
2280 }
2281 
2282 static int gfx_v9_4_3_cp_resume(struct amdgpu_device *adev)
2283 {
2284 	int r = 0, i, num_xcc, num_xcp, num_xcc_per_xcp;
2285 
2286 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
2287 	if (amdgpu_sriov_vf(adev)) {
2288 		enum amdgpu_gfx_partition mode;
2289 
2290 		mode = amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
2291 						       AMDGPU_XCP_FL_NONE);
2292 		if (mode == AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE)
2293 			return -EINVAL;
2294 		num_xcc_per_xcp = gfx_v9_4_3_get_xccs_per_xcp(adev);
2295 		adev->gfx.num_xcc_per_xcp = num_xcc_per_xcp;
2296 		num_xcp = num_xcc / num_xcc_per_xcp;
2297 		r = amdgpu_xcp_init(adev->xcp_mgr, num_xcp, mode);
2298 
2299 	} else {
2300 		if (amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
2301 						    AMDGPU_XCP_FL_NONE) ==
2302 		    AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE)
2303 			r = amdgpu_xcp_switch_partition_mode(
2304 				adev->xcp_mgr, amdgpu_user_partt_mode);
2305 	}
2306 	if (r)
2307 		return r;
2308 
2309 	for (i = 0; i < num_xcc; i++) {
2310 		r = gfx_v9_4_3_xcc_cp_resume(adev, i);
2311 		if (r)
2312 			return r;
2313 	}
2314 
2315 	return 0;
2316 }
2317 
2318 static void gfx_v9_4_3_xcc_fini(struct amdgpu_device *adev, int xcc_id)
2319 {
2320 	if (amdgpu_gfx_disable_kcq(adev, xcc_id))
2321 		DRM_ERROR("XCD %d KCQ disable failed\n", xcc_id);
2322 
2323 	if (amdgpu_sriov_vf(adev)) {
2324 		/* must disable polling for SRIOV when hw finished, otherwise
2325 		 * CPC engine may still keep fetching WB address which is already
2326 		 * invalid after sw finished and trigger DMAR reading error in
2327 		 * hypervisor side.
2328 		 */
2329 		WREG32_FIELD15_PREREG(GC, GET_INST(GC, xcc_id), CP_PQ_WPTR_POLL_CNTL, EN, 0);
2330 		return;
2331 	}
2332 
2333 	/* Use deinitialize sequence from CAIL when unbinding device
2334 	 * from driver, otherwise KIQ is hanging when binding back
2335 	 */
2336 	if (!amdgpu_in_reset(adev) && !adev->in_suspend) {
2337 		mutex_lock(&adev->srbm_mutex);
2338 		soc15_grbm_select(adev, adev->gfx.kiq[xcc_id].ring.me,
2339 				  adev->gfx.kiq[xcc_id].ring.pipe,
2340 				  adev->gfx.kiq[xcc_id].ring.queue, 0,
2341 				  GET_INST(GC, xcc_id));
2342 		gfx_v9_4_3_xcc_q_fini_register(&adev->gfx.kiq[xcc_id].ring,
2343 						 xcc_id);
2344 		soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
2345 		mutex_unlock(&adev->srbm_mutex);
2346 	}
2347 
2348 	gfx_v9_4_3_xcc_kcq_fini_register(adev, xcc_id);
2349 	gfx_v9_4_3_xcc_cp_compute_enable(adev, false, xcc_id);
2350 }
2351 
2352 static int gfx_v9_4_3_hw_init(struct amdgpu_ip_block *ip_block)
2353 {
2354 	int r;
2355 	struct amdgpu_device *adev = ip_block->adev;
2356 
2357 	amdgpu_gfx_cleaner_shader_init(adev, adev->gfx.cleaner_shader_size,
2358 				       adev->gfx.cleaner_shader_ptr);
2359 
2360 	if (!amdgpu_sriov_vf(adev))
2361 		gfx_v9_4_3_init_golden_registers(adev);
2362 
2363 	gfx_v9_4_3_constants_init(adev);
2364 
2365 	r = adev->gfx.rlc.funcs->resume(adev);
2366 	if (r)
2367 		return r;
2368 
2369 	r = gfx_v9_4_3_cp_resume(adev);
2370 	if (r)
2371 		return r;
2372 
2373 	return r;
2374 }
2375 
2376 static int gfx_v9_4_3_hw_fini(struct amdgpu_ip_block *ip_block)
2377 {
2378 	struct amdgpu_device *adev = ip_block->adev;
2379 	int i, num_xcc;
2380 
2381 	amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
2382 	amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
2383 	amdgpu_irq_put(adev, &adev->gfx.bad_op_irq, 0);
2384 
2385 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
2386 	for (i = 0; i < num_xcc; i++) {
2387 		gfx_v9_4_3_xcc_fini(adev, i);
2388 	}
2389 
2390 	return 0;
2391 }
2392 
2393 static int gfx_v9_4_3_suspend(struct amdgpu_ip_block *ip_block)
2394 {
2395 	return gfx_v9_4_3_hw_fini(ip_block);
2396 }
2397 
2398 static int gfx_v9_4_3_resume(struct amdgpu_ip_block *ip_block)
2399 {
2400 	return gfx_v9_4_3_hw_init(ip_block);
2401 }
2402 
2403 static bool gfx_v9_4_3_is_idle(void *handle)
2404 {
2405 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2406 	int i, num_xcc;
2407 
2408 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
2409 	for (i = 0; i < num_xcc; i++) {
2410 		if (REG_GET_FIELD(RREG32_SOC15(GC, GET_INST(GC, i), regGRBM_STATUS),
2411 					GRBM_STATUS, GUI_ACTIVE))
2412 			return false;
2413 	}
2414 	return true;
2415 }
2416 
2417 static int gfx_v9_4_3_wait_for_idle(struct amdgpu_ip_block *ip_block)
2418 {
2419 	unsigned i;
2420 	struct amdgpu_device *adev = ip_block->adev;
2421 
2422 	for (i = 0; i < adev->usec_timeout; i++) {
2423 		if (gfx_v9_4_3_is_idle(adev))
2424 			return 0;
2425 		udelay(1);
2426 	}
2427 	return -ETIMEDOUT;
2428 }
2429 
2430 static int gfx_v9_4_3_soft_reset(struct amdgpu_ip_block *ip_block)
2431 {
2432 	u32 grbm_soft_reset = 0;
2433 	u32 tmp;
2434 	struct amdgpu_device *adev = ip_block->adev;
2435 
2436 	/* GRBM_STATUS */
2437 	tmp = RREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_STATUS);
2438 	if (tmp & (GRBM_STATUS__PA_BUSY_MASK | GRBM_STATUS__SC_BUSY_MASK |
2439 		   GRBM_STATUS__BCI_BUSY_MASK | GRBM_STATUS__SX_BUSY_MASK |
2440 		   GRBM_STATUS__TA_BUSY_MASK | GRBM_STATUS__VGT_BUSY_MASK |
2441 		   GRBM_STATUS__DB_BUSY_MASK | GRBM_STATUS__CB_BUSY_MASK |
2442 		   GRBM_STATUS__GDS_BUSY_MASK | GRBM_STATUS__SPI_BUSY_MASK |
2443 		   GRBM_STATUS__IA_BUSY_MASK | GRBM_STATUS__IA_BUSY_NO_DMA_MASK)) {
2444 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
2445 						GRBM_SOFT_RESET, SOFT_RESET_CP, 1);
2446 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
2447 						GRBM_SOFT_RESET, SOFT_RESET_GFX, 1);
2448 	}
2449 
2450 	if (tmp & (GRBM_STATUS__CP_BUSY_MASK | GRBM_STATUS__CP_COHERENCY_BUSY_MASK)) {
2451 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
2452 						GRBM_SOFT_RESET, SOFT_RESET_CP, 1);
2453 	}
2454 
2455 	/* GRBM_STATUS2 */
2456 	tmp = RREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_STATUS2);
2457 	if (REG_GET_FIELD(tmp, GRBM_STATUS2, RLC_BUSY))
2458 		grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset,
2459 						GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
2460 
2461 
2462 	if (grbm_soft_reset) {
2463 		/* stop the rlc */
2464 		adev->gfx.rlc.funcs->stop(adev);
2465 
2466 		/* Disable MEC parsing/prefetching */
2467 		gfx_v9_4_3_xcc_cp_compute_enable(adev, false, 0);
2468 
2469 		if (grbm_soft_reset) {
2470 			tmp = RREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_SOFT_RESET);
2471 			tmp |= grbm_soft_reset;
2472 			dev_info(adev->dev, "GRBM_SOFT_RESET=0x%08X\n", tmp);
2473 			WREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_SOFT_RESET, tmp);
2474 			tmp = RREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_SOFT_RESET);
2475 
2476 			udelay(50);
2477 
2478 			tmp &= ~grbm_soft_reset;
2479 			WREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_SOFT_RESET, tmp);
2480 			tmp = RREG32_SOC15(GC, GET_INST(GC, 0), regGRBM_SOFT_RESET);
2481 		}
2482 
2483 		/* Wait a little for things to settle down */
2484 		udelay(50);
2485 	}
2486 	return 0;
2487 }
2488 
2489 static void gfx_v9_4_3_ring_emit_gds_switch(struct amdgpu_ring *ring,
2490 					  uint32_t vmid,
2491 					  uint32_t gds_base, uint32_t gds_size,
2492 					  uint32_t gws_base, uint32_t gws_size,
2493 					  uint32_t oa_base, uint32_t oa_size)
2494 {
2495 	struct amdgpu_device *adev = ring->adev;
2496 
2497 	/* GDS Base */
2498 	gfx_v9_4_3_write_data_to_reg(ring, 0, false,
2499 				   SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regGDS_VMID0_BASE) + 2 * vmid,
2500 				   gds_base);
2501 
2502 	/* GDS Size */
2503 	gfx_v9_4_3_write_data_to_reg(ring, 0, false,
2504 				   SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regGDS_VMID0_SIZE) + 2 * vmid,
2505 				   gds_size);
2506 
2507 	/* GWS */
2508 	gfx_v9_4_3_write_data_to_reg(ring, 0, false,
2509 				   SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regGDS_GWS_VMID0) + vmid,
2510 				   gws_size << GDS_GWS_VMID0__SIZE__SHIFT | gws_base);
2511 
2512 	/* OA */
2513 	gfx_v9_4_3_write_data_to_reg(ring, 0, false,
2514 				   SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regGDS_OA_VMID0) + vmid,
2515 				   (1 << (oa_size + oa_base)) - (1 << oa_base));
2516 }
2517 
2518 static int gfx_v9_4_3_early_init(struct amdgpu_ip_block *ip_block)
2519 {
2520 	struct amdgpu_device *adev = ip_block->adev;
2521 
2522 	adev->gfx.num_compute_rings = min(amdgpu_gfx_get_num_kcq(adev),
2523 					  AMDGPU_MAX_COMPUTE_RINGS);
2524 	gfx_v9_4_3_set_kiq_pm4_funcs(adev);
2525 	gfx_v9_4_3_set_ring_funcs(adev);
2526 	gfx_v9_4_3_set_irq_funcs(adev);
2527 	gfx_v9_4_3_set_gds_init(adev);
2528 	gfx_v9_4_3_set_rlc_funcs(adev);
2529 
2530 	/* init rlcg reg access ctrl */
2531 	gfx_v9_4_3_init_rlcg_reg_access_ctrl(adev);
2532 
2533 	return gfx_v9_4_3_init_microcode(adev);
2534 }
2535 
2536 static int gfx_v9_4_3_late_init(struct amdgpu_ip_block *ip_block)
2537 {
2538 	struct amdgpu_device *adev = ip_block->adev;
2539 	int r;
2540 
2541 	r = amdgpu_irq_get(adev, &adev->gfx.priv_reg_irq, 0);
2542 	if (r)
2543 		return r;
2544 
2545 	r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
2546 	if (r)
2547 		return r;
2548 
2549 	r = amdgpu_irq_get(adev, &adev->gfx.bad_op_irq, 0);
2550 	if (r)
2551 		return r;
2552 
2553 	if (adev->gfx.ras &&
2554 	    adev->gfx.ras->enable_watchdog_timer)
2555 		adev->gfx.ras->enable_watchdog_timer(adev);
2556 
2557 	return 0;
2558 }
2559 
2560 static void gfx_v9_4_3_xcc_update_sram_fgcg(struct amdgpu_device *adev,
2561 					    bool enable, int xcc_id)
2562 {
2563 	uint32_t def, data;
2564 
2565 	if (!(adev->cg_flags & AMD_CG_SUPPORT_GFX_FGCG))
2566 		return;
2567 
2568 	def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id),
2569 				  regRLC_CGTT_MGCG_OVERRIDE);
2570 
2571 	if (enable)
2572 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_FGCG_OVERRIDE_MASK;
2573 	else
2574 		data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_FGCG_OVERRIDE_MASK;
2575 
2576 	if (def != data)
2577 		WREG32_SOC15(GC, GET_INST(GC, xcc_id),
2578 			     regRLC_CGTT_MGCG_OVERRIDE, data);
2579 
2580 }
2581 
2582 static void gfx_v9_4_3_xcc_update_repeater_fgcg(struct amdgpu_device *adev,
2583 						bool enable, int xcc_id)
2584 {
2585 	uint32_t def, data;
2586 
2587 	if (!(adev->cg_flags & AMD_CG_SUPPORT_REPEATER_FGCG))
2588 		return;
2589 
2590 	def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id),
2591 				  regRLC_CGTT_MGCG_OVERRIDE);
2592 
2593 	if (enable)
2594 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_REP_FGCG_OVERRIDE_MASK;
2595 	else
2596 		data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_REP_FGCG_OVERRIDE_MASK;
2597 
2598 	if (def != data)
2599 		WREG32_SOC15(GC, GET_INST(GC, xcc_id),
2600 			     regRLC_CGTT_MGCG_OVERRIDE, data);
2601 }
2602 
2603 static void
2604 gfx_v9_4_3_xcc_update_medium_grain_clock_gating(struct amdgpu_device *adev,
2605 						bool enable, int xcc_id)
2606 {
2607 	uint32_t data, def;
2608 
2609 	/* It is disabled by HW by default */
2610 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGCG)) {
2611 		/* 1 - RLC_CGTT_MGCG_OVERRIDE */
2612 		def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGTT_MGCG_OVERRIDE);
2613 
2614 		data &= ~(RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
2615 			  RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK |
2616 			  RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK |
2617 			  RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGLS_OVERRIDE_MASK);
2618 
2619 		if (def != data)
2620 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGTT_MGCG_OVERRIDE, data);
2621 
2622 		/* MGLS is a global flag to control all MGLS in GFX */
2623 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGLS) {
2624 			/* 2 - RLC memory Light sleep */
2625 			if (adev->cg_flags & AMD_CG_SUPPORT_GFX_RLC_LS) {
2626 				def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_MEM_SLP_CNTL);
2627 				data |= RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK;
2628 				if (def != data)
2629 					WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_MEM_SLP_CNTL, data);
2630 			}
2631 			/* 3 - CP memory Light sleep */
2632 			if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CP_LS) {
2633 				def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MEM_SLP_CNTL);
2634 				data |= CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK;
2635 				if (def != data)
2636 					WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MEM_SLP_CNTL, data);
2637 			}
2638 		}
2639 	} else {
2640 		/* 1 - MGCG_OVERRIDE */
2641 		def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGTT_MGCG_OVERRIDE);
2642 
2643 		data |= (RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK |
2644 			 RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
2645 			 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK |
2646 			 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGLS_OVERRIDE_MASK);
2647 
2648 		if (def != data)
2649 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGTT_MGCG_OVERRIDE, data);
2650 
2651 		/* 2 - disable MGLS in RLC */
2652 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_MEM_SLP_CNTL);
2653 		if (data & RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK) {
2654 			data &= ~RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK;
2655 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_MEM_SLP_CNTL, data);
2656 		}
2657 
2658 		/* 3 - disable MGLS in CP */
2659 		data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MEM_SLP_CNTL);
2660 		if (data & CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK) {
2661 			data &= ~CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK;
2662 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_MEM_SLP_CNTL, data);
2663 		}
2664 	}
2665 
2666 }
2667 
2668 static void
2669 gfx_v9_4_3_xcc_update_coarse_grain_clock_gating(struct amdgpu_device *adev,
2670 						bool enable, int xcc_id)
2671 {
2672 	uint32_t def, data;
2673 
2674 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGCG)) {
2675 
2676 		def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGTT_MGCG_OVERRIDE);
2677 		/* unset CGCG override */
2678 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGCG_OVERRIDE_MASK;
2679 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
2680 			data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
2681 		else
2682 			data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
2683 		/* update CGCG and CGLS override bits */
2684 		if (def != data)
2685 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGTT_MGCG_OVERRIDE, data);
2686 
2687 		/* CGCG Hysteresis: 400us */
2688 		def = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGCG_CGLS_CTRL);
2689 
2690 		data = (0x2710
2691 			<< RLC_CGCG_CGLS_CTRL__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
2692 		       RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK;
2693 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
2694 			data |= (0x000F << RLC_CGCG_CGLS_CTRL__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
2695 				RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK;
2696 		if (def != data)
2697 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGCG_CGLS_CTRL, data);
2698 
2699 		/* set IDLE_POLL_COUNT(0x33450100)*/
2700 		def = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_RB_WPTR_POLL_CNTL);
2701 		data = (0x0100 << CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY__SHIFT) |
2702 			(0x3345 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
2703 		if (def != data)
2704 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_RB_WPTR_POLL_CNTL, data);
2705 	} else {
2706 		def = data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGCG_CGLS_CTRL);
2707 		/* reset CGCG/CGLS bits */
2708 		data &= ~(RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK | RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK);
2709 		/* disable cgcg and cgls in FSM */
2710 		if (def != data)
2711 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CGCG_CGLS_CTRL, data);
2712 	}
2713 
2714 }
2715 
2716 static int gfx_v9_4_3_xcc_update_gfx_clock_gating(struct amdgpu_device *adev,
2717 						  bool enable, int xcc_id)
2718 {
2719 	amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id);
2720 
2721 	if (enable) {
2722 		/* FGCG */
2723 		gfx_v9_4_3_xcc_update_sram_fgcg(adev, enable, xcc_id);
2724 		gfx_v9_4_3_xcc_update_repeater_fgcg(adev, enable, xcc_id);
2725 
2726 		/* CGCG/CGLS should be enabled after MGCG/MGLS
2727 		 * ===  MGCG + MGLS ===
2728 		 */
2729 		gfx_v9_4_3_xcc_update_medium_grain_clock_gating(adev, enable,
2730 								xcc_id);
2731 		/* ===  CGCG + CGLS === */
2732 		gfx_v9_4_3_xcc_update_coarse_grain_clock_gating(adev, enable,
2733 								xcc_id);
2734 	} else {
2735 		/* CGCG/CGLS should be disabled before MGCG/MGLS
2736 		 * ===  CGCG + CGLS ===
2737 		 */
2738 		gfx_v9_4_3_xcc_update_coarse_grain_clock_gating(adev, enable,
2739 								xcc_id);
2740 		/* ===  MGCG + MGLS === */
2741 		gfx_v9_4_3_xcc_update_medium_grain_clock_gating(adev, enable,
2742 								xcc_id);
2743 
2744 		/* FGCG */
2745 		gfx_v9_4_3_xcc_update_sram_fgcg(adev, enable, xcc_id);
2746 		gfx_v9_4_3_xcc_update_repeater_fgcg(adev, enable, xcc_id);
2747 	}
2748 
2749 	amdgpu_gfx_rlc_exit_safe_mode(adev, xcc_id);
2750 
2751 	return 0;
2752 }
2753 
2754 static const struct amdgpu_rlc_funcs gfx_v9_4_3_rlc_funcs = {
2755 	.is_rlc_enabled = gfx_v9_4_3_is_rlc_enabled,
2756 	.set_safe_mode = gfx_v9_4_3_xcc_set_safe_mode,
2757 	.unset_safe_mode = gfx_v9_4_3_xcc_unset_safe_mode,
2758 	.init = gfx_v9_4_3_rlc_init,
2759 	.resume = gfx_v9_4_3_rlc_resume,
2760 	.stop = gfx_v9_4_3_rlc_stop,
2761 	.reset = gfx_v9_4_3_rlc_reset,
2762 	.start = gfx_v9_4_3_rlc_start,
2763 	.update_spm_vmid = gfx_v9_4_3_update_spm_vmid,
2764 	.is_rlcg_access_range = gfx_v9_4_3_is_rlcg_access_range,
2765 };
2766 
2767 static int gfx_v9_4_3_set_powergating_state(void *handle,
2768 					  enum amd_powergating_state state)
2769 {
2770 	return 0;
2771 }
2772 
2773 static int gfx_v9_4_3_set_clockgating_state(void *handle,
2774 					  enum amd_clockgating_state state)
2775 {
2776 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2777 	int i, num_xcc;
2778 
2779 	if (amdgpu_sriov_vf(adev))
2780 		return 0;
2781 
2782 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
2783 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
2784 	case IP_VERSION(9, 4, 3):
2785 	case IP_VERSION(9, 4, 4):
2786 		for (i = 0; i < num_xcc; i++)
2787 			gfx_v9_4_3_xcc_update_gfx_clock_gating(
2788 				adev, state == AMD_CG_STATE_GATE, i);
2789 		break;
2790 	default:
2791 		break;
2792 	}
2793 	return 0;
2794 }
2795 
2796 static void gfx_v9_4_3_get_clockgating_state(void *handle, u64 *flags)
2797 {
2798 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2799 	int data;
2800 
2801 	if (amdgpu_sriov_vf(adev))
2802 		*flags = 0;
2803 
2804 	/* AMD_CG_SUPPORT_GFX_MGCG */
2805 	data = RREG32_KIQ(SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regRLC_CGTT_MGCG_OVERRIDE));
2806 	if (!(data & RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK))
2807 		*flags |= AMD_CG_SUPPORT_GFX_MGCG;
2808 
2809 	/* AMD_CG_SUPPORT_GFX_CGCG */
2810 	data = RREG32_KIQ(SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regRLC_CGCG_CGLS_CTRL));
2811 	if (data & RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK)
2812 		*flags |= AMD_CG_SUPPORT_GFX_CGCG;
2813 
2814 	/* AMD_CG_SUPPORT_GFX_CGLS */
2815 	if (data & RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK)
2816 		*flags |= AMD_CG_SUPPORT_GFX_CGLS;
2817 
2818 	/* AMD_CG_SUPPORT_GFX_RLC_LS */
2819 	data = RREG32_KIQ(SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regRLC_MEM_SLP_CNTL));
2820 	if (data & RLC_MEM_SLP_CNTL__RLC_MEM_LS_EN_MASK)
2821 		*flags |= AMD_CG_SUPPORT_GFX_RLC_LS | AMD_CG_SUPPORT_GFX_MGLS;
2822 
2823 	/* AMD_CG_SUPPORT_GFX_CP_LS */
2824 	data = RREG32_KIQ(SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regCP_MEM_SLP_CNTL));
2825 	if (data & CP_MEM_SLP_CNTL__CP_MEM_LS_EN_MASK)
2826 		*flags |= AMD_CG_SUPPORT_GFX_CP_LS | AMD_CG_SUPPORT_GFX_MGLS;
2827 }
2828 
2829 static void gfx_v9_4_3_ring_emit_hdp_flush(struct amdgpu_ring *ring)
2830 {
2831 	struct amdgpu_device *adev = ring->adev;
2832 	u32 ref_and_mask, reg_mem_engine;
2833 	const struct nbio_hdp_flush_reg *nbio_hf_reg = adev->nbio.hdp_flush_reg;
2834 
2835 	if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) {
2836 		switch (ring->me) {
2837 		case 1:
2838 			ref_and_mask = nbio_hf_reg->ref_and_mask_cp2 << ring->pipe;
2839 			break;
2840 		case 2:
2841 			ref_and_mask = nbio_hf_reg->ref_and_mask_cp6 << ring->pipe;
2842 			break;
2843 		default:
2844 			return;
2845 		}
2846 		reg_mem_engine = 0;
2847 	} else {
2848 		ref_and_mask = nbio_hf_reg->ref_and_mask_cp0;
2849 		reg_mem_engine = 1; /* pfp */
2850 	}
2851 
2852 	gfx_v9_4_3_wait_reg_mem(ring, reg_mem_engine, 0, 1,
2853 			      adev->nbio.funcs->get_hdp_flush_req_offset(adev),
2854 			      adev->nbio.funcs->get_hdp_flush_done_offset(adev),
2855 			      ref_and_mask, ref_and_mask, 0x20);
2856 }
2857 
2858 static void gfx_v9_4_3_ring_emit_ib_compute(struct amdgpu_ring *ring,
2859 					  struct amdgpu_job *job,
2860 					  struct amdgpu_ib *ib,
2861 					  uint32_t flags)
2862 {
2863 	unsigned vmid = AMDGPU_JOB_GET_VMID(job);
2864 	u32 control = INDIRECT_BUFFER_VALID | ib->length_dw | (vmid << 24);
2865 
2866 	/* Currently, there is a high possibility to get wave ID mismatch
2867 	 * between ME and GDS, leading to a hw deadlock, because ME generates
2868 	 * different wave IDs than the GDS expects. This situation happens
2869 	 * randomly when at least 5 compute pipes use GDS ordered append.
2870 	 * The wave IDs generated by ME are also wrong after suspend/resume.
2871 	 * Those are probably bugs somewhere else in the kernel driver.
2872 	 *
2873 	 * Writing GDS_COMPUTE_MAX_WAVE_ID resets wave ID counters in ME and
2874 	 * GDS to 0 for this ring (me/pipe).
2875 	 */
2876 	if (ib->flags & AMDGPU_IB_FLAG_RESET_GDS_MAX_WAVE_ID) {
2877 		amdgpu_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
2878 		amdgpu_ring_write(ring, regGDS_COMPUTE_MAX_WAVE_ID);
2879 		amdgpu_ring_write(ring, ring->adev->gds.gds_compute_max_wave_id);
2880 	}
2881 
2882 	amdgpu_ring_write(ring, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
2883 	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
2884 	amdgpu_ring_write(ring,
2885 #ifdef __BIG_ENDIAN
2886 				(2 << 0) |
2887 #endif
2888 				lower_32_bits(ib->gpu_addr));
2889 	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
2890 	amdgpu_ring_write(ring, control);
2891 }
2892 
2893 static void gfx_v9_4_3_ring_emit_fence(struct amdgpu_ring *ring, u64 addr,
2894 				     u64 seq, unsigned flags)
2895 {
2896 	bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
2897 	bool int_sel = flags & AMDGPU_FENCE_FLAG_INT;
2898 	bool writeback = flags & AMDGPU_FENCE_FLAG_TC_WB_ONLY;
2899 
2900 	/* RELEASE_MEM - flush caches, send int */
2901 	amdgpu_ring_write(ring, PACKET3(PACKET3_RELEASE_MEM, 6));
2902 	amdgpu_ring_write(ring, ((writeback ? (EOP_TC_WB_ACTION_EN |
2903 					       EOP_TC_NC_ACTION_EN) :
2904 					      (EOP_TCL1_ACTION_EN |
2905 					       EOP_TC_ACTION_EN |
2906 					       EOP_TC_WB_ACTION_EN |
2907 					       EOP_TC_MD_ACTION_EN)) |
2908 				 EVENT_TYPE(CACHE_FLUSH_AND_INV_TS_EVENT) |
2909 				 EVENT_INDEX(5)));
2910 	amdgpu_ring_write(ring, DATA_SEL(write64bit ? 2 : 1) | INT_SEL(int_sel ? 2 : 0));
2911 
2912 	/*
2913 	 * the address should be Qword aligned if 64bit write, Dword
2914 	 * aligned if only send 32bit data low (discard data high)
2915 	 */
2916 	if (write64bit)
2917 		BUG_ON(addr & 0x7);
2918 	else
2919 		BUG_ON(addr & 0x3);
2920 	amdgpu_ring_write(ring, lower_32_bits(addr));
2921 	amdgpu_ring_write(ring, upper_32_bits(addr));
2922 	amdgpu_ring_write(ring, lower_32_bits(seq));
2923 	amdgpu_ring_write(ring, upper_32_bits(seq));
2924 	amdgpu_ring_write(ring, 0);
2925 }
2926 
2927 static void gfx_v9_4_3_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
2928 {
2929 	int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
2930 	uint32_t seq = ring->fence_drv.sync_seq;
2931 	uint64_t addr = ring->fence_drv.gpu_addr;
2932 
2933 	gfx_v9_4_3_wait_reg_mem(ring, usepfp, 1, 0,
2934 			      lower_32_bits(addr), upper_32_bits(addr),
2935 			      seq, 0xffffffff, 4);
2936 }
2937 
2938 static void gfx_v9_4_3_ring_emit_vm_flush(struct amdgpu_ring *ring,
2939 					unsigned vmid, uint64_t pd_addr)
2940 {
2941 	amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
2942 }
2943 
2944 static u64 gfx_v9_4_3_ring_get_rptr_compute(struct amdgpu_ring *ring)
2945 {
2946 	return ring->adev->wb.wb[ring->rptr_offs]; /* gfx9 hardware is 32bit rptr */
2947 }
2948 
2949 static u64 gfx_v9_4_3_ring_get_wptr_compute(struct amdgpu_ring *ring)
2950 {
2951 	u64 wptr;
2952 
2953 	/* XXX check if swapping is necessary on BE */
2954 	if (ring->use_doorbell)
2955 		wptr = atomic64_read((atomic64_t *)&ring->adev->wb.wb[ring->wptr_offs]);
2956 	else
2957 		BUG();
2958 	return wptr;
2959 }
2960 
2961 static void gfx_v9_4_3_ring_set_wptr_compute(struct amdgpu_ring *ring)
2962 {
2963 	struct amdgpu_device *adev = ring->adev;
2964 
2965 	/* XXX check if swapping is necessary on BE */
2966 	if (ring->use_doorbell) {
2967 		atomic64_set((atomic64_t *)&adev->wb.wb[ring->wptr_offs], ring->wptr);
2968 		WDOORBELL64(ring->doorbell_index, ring->wptr);
2969 	} else {
2970 		BUG(); /* only DOORBELL method supported on gfx9 now */
2971 	}
2972 }
2973 
2974 static void gfx_v9_4_3_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
2975 					 u64 seq, unsigned int flags)
2976 {
2977 	struct amdgpu_device *adev = ring->adev;
2978 
2979 	/* we only allocate 32bit for each seq wb address */
2980 	BUG_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
2981 
2982 	/* write fence seq to the "addr" */
2983 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
2984 	amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
2985 				 WRITE_DATA_DST_SEL(5) | WR_CONFIRM));
2986 	amdgpu_ring_write(ring, lower_32_bits(addr));
2987 	amdgpu_ring_write(ring, upper_32_bits(addr));
2988 	amdgpu_ring_write(ring, lower_32_bits(seq));
2989 
2990 	if (flags & AMDGPU_FENCE_FLAG_INT) {
2991 		/* set register to trigger INT */
2992 		amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
2993 		amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
2994 					 WRITE_DATA_DST_SEL(0) | WR_CONFIRM));
2995 		amdgpu_ring_write(ring, SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regCPC_INT_STATUS));
2996 		amdgpu_ring_write(ring, 0);
2997 		amdgpu_ring_write(ring, 0x20000000); /* src_id is 178 */
2998 	}
2999 }
3000 
3001 static void gfx_v9_4_3_ring_emit_rreg(struct amdgpu_ring *ring, uint32_t reg,
3002 				    uint32_t reg_val_offs)
3003 {
3004 	struct amdgpu_device *adev = ring->adev;
3005 
3006 	reg = gfx_v9_4_3_normalize_xcc_reg_offset(reg);
3007 
3008 	amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
3009 	amdgpu_ring_write(ring, 0 |	/* src: register*/
3010 				(5 << 8) |	/* dst: memory */
3011 				(1 << 20));	/* write confirm */
3012 	amdgpu_ring_write(ring, reg);
3013 	amdgpu_ring_write(ring, 0);
3014 	amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
3015 				reg_val_offs * 4));
3016 	amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
3017 				reg_val_offs * 4));
3018 }
3019 
3020 static void gfx_v9_4_3_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg,
3021 				    uint32_t val)
3022 {
3023 	uint32_t cmd = 0;
3024 
3025 	reg = gfx_v9_4_3_normalize_xcc_reg_offset(reg);
3026 
3027 	switch (ring->funcs->type) {
3028 	case AMDGPU_RING_TYPE_GFX:
3029 		cmd = WRITE_DATA_ENGINE_SEL(1) | WR_CONFIRM;
3030 		break;
3031 	case AMDGPU_RING_TYPE_KIQ:
3032 		cmd = (1 << 16); /* no inc addr */
3033 		break;
3034 	default:
3035 		cmd = WR_CONFIRM;
3036 		break;
3037 	}
3038 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
3039 	amdgpu_ring_write(ring, cmd);
3040 	amdgpu_ring_write(ring, reg);
3041 	amdgpu_ring_write(ring, 0);
3042 	amdgpu_ring_write(ring, val);
3043 }
3044 
3045 static void gfx_v9_4_3_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
3046 					uint32_t val, uint32_t mask)
3047 {
3048 	gfx_v9_4_3_wait_reg_mem(ring, 0, 0, 0, reg, 0, val, mask, 0x20);
3049 }
3050 
3051 static void gfx_v9_4_3_ring_emit_reg_write_reg_wait(struct amdgpu_ring *ring,
3052 						  uint32_t reg0, uint32_t reg1,
3053 						  uint32_t ref, uint32_t mask)
3054 {
3055 	amdgpu_ring_emit_reg_write_reg_wait_helper(ring, reg0, reg1,
3056 						   ref, mask);
3057 }
3058 
3059 static void gfx_v9_4_3_ring_soft_recovery(struct amdgpu_ring *ring,
3060 					  unsigned vmid)
3061 {
3062 	struct amdgpu_device *adev = ring->adev;
3063 	uint32_t value = 0;
3064 
3065 	value = REG_SET_FIELD(value, SQ_CMD, CMD, 0x03);
3066 	value = REG_SET_FIELD(value, SQ_CMD, MODE, 0x01);
3067 	value = REG_SET_FIELD(value, SQ_CMD, CHECK_VMID, 1);
3068 	value = REG_SET_FIELD(value, SQ_CMD, VM_ID, vmid);
3069 	amdgpu_gfx_rlc_enter_safe_mode(adev, ring->xcc_id);
3070 	WREG32_SOC15(GC, GET_INST(GC, ring->xcc_id), regSQ_CMD, value);
3071 	amdgpu_gfx_rlc_exit_safe_mode(adev, ring->xcc_id);
3072 }
3073 
3074 static void gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3075 	struct amdgpu_device *adev, int me, int pipe,
3076 	enum amdgpu_interrupt_state state, int xcc_id)
3077 {
3078 	u32 mec_int_cntl, mec_int_cntl_reg;
3079 
3080 	/*
3081 	 * amdgpu controls only the first MEC. That's why this function only
3082 	 * handles the setting of interrupts for this specific MEC. All other
3083 	 * pipes' interrupts are set by amdkfd.
3084 	 */
3085 
3086 	if (me == 1) {
3087 		switch (pipe) {
3088 		case 0:
3089 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE0_INT_CNTL);
3090 			break;
3091 		case 1:
3092 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE1_INT_CNTL);
3093 			break;
3094 		case 2:
3095 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE2_INT_CNTL);
3096 			break;
3097 		case 3:
3098 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE3_INT_CNTL);
3099 			break;
3100 		default:
3101 			DRM_DEBUG("invalid pipe %d\n", pipe);
3102 			return;
3103 		}
3104 	} else {
3105 		DRM_DEBUG("invalid me %d\n", me);
3106 		return;
3107 	}
3108 
3109 	switch (state) {
3110 	case AMDGPU_IRQ_STATE_DISABLE:
3111 		mec_int_cntl = RREG32_XCC(mec_int_cntl_reg, xcc_id);
3112 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
3113 					     TIME_STAMP_INT_ENABLE, 0);
3114 		WREG32_XCC(mec_int_cntl_reg, mec_int_cntl, xcc_id);
3115 		break;
3116 	case AMDGPU_IRQ_STATE_ENABLE:
3117 		mec_int_cntl = RREG32_XCC(mec_int_cntl_reg, xcc_id);
3118 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
3119 					     TIME_STAMP_INT_ENABLE, 1);
3120 		WREG32_XCC(mec_int_cntl_reg, mec_int_cntl, xcc_id);
3121 		break;
3122 	default:
3123 		break;
3124 	}
3125 }
3126 
3127 static u32 gfx_v9_4_3_get_cpc_int_cntl(struct amdgpu_device *adev,
3128 				     int xcc_id, int me, int pipe)
3129 {
3130 	/*
3131 	 * amdgpu controls only the first MEC. That's why this function only
3132 	 * handles the setting of interrupts for this specific MEC. All other
3133 	 * pipes' interrupts are set by amdkfd.
3134 	 */
3135 	if (me != 1)
3136 		return 0;
3137 
3138 	switch (pipe) {
3139 	case 0:
3140 		return SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE0_INT_CNTL);
3141 	case 1:
3142 		return SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE1_INT_CNTL);
3143 	case 2:
3144 		return SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE2_INT_CNTL);
3145 	case 3:
3146 		return SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_ME1_PIPE3_INT_CNTL);
3147 	default:
3148 		return 0;
3149 	}
3150 }
3151 
3152 static int gfx_v9_4_3_set_priv_reg_fault_state(struct amdgpu_device *adev,
3153 					     struct amdgpu_irq_src *source,
3154 					     unsigned type,
3155 					     enum amdgpu_interrupt_state state)
3156 {
3157 	u32 mec_int_cntl_reg, mec_int_cntl;
3158 	int i, j, k, num_xcc;
3159 
3160 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
3161 	switch (state) {
3162 	case AMDGPU_IRQ_STATE_DISABLE:
3163 	case AMDGPU_IRQ_STATE_ENABLE:
3164 		for (i = 0; i < num_xcc; i++) {
3165 			WREG32_FIELD15_PREREG(GC, GET_INST(GC, i), CP_INT_CNTL_RING0,
3166 					      PRIV_REG_INT_ENABLE,
3167 					      state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
3168 			for (j = 0; j < adev->gfx.mec.num_mec; j++) {
3169 				for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
3170 					/* MECs start at 1 */
3171 					mec_int_cntl_reg = gfx_v9_4_3_get_cpc_int_cntl(adev, i, j + 1, k);
3172 
3173 					if (mec_int_cntl_reg) {
3174 						mec_int_cntl = RREG32_XCC(mec_int_cntl_reg, i);
3175 						mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
3176 									     PRIV_REG_INT_ENABLE,
3177 									     state == AMDGPU_IRQ_STATE_ENABLE ?
3178 									     1 : 0);
3179 						WREG32_XCC(mec_int_cntl_reg, mec_int_cntl, i);
3180 					}
3181 				}
3182 			}
3183 		}
3184 		break;
3185 	default:
3186 		break;
3187 	}
3188 
3189 	return 0;
3190 }
3191 
3192 static int gfx_v9_4_3_set_bad_op_fault_state(struct amdgpu_device *adev,
3193 					     struct amdgpu_irq_src *source,
3194 					     unsigned type,
3195 					     enum amdgpu_interrupt_state state)
3196 {
3197 	u32 mec_int_cntl_reg, mec_int_cntl;
3198 	int i, j, k, num_xcc;
3199 
3200 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
3201 	switch (state) {
3202 	case AMDGPU_IRQ_STATE_DISABLE:
3203 	case AMDGPU_IRQ_STATE_ENABLE:
3204 		for (i = 0; i < num_xcc; i++) {
3205 			WREG32_FIELD15_PREREG(GC, GET_INST(GC, i), CP_INT_CNTL_RING0,
3206 					      OPCODE_ERROR_INT_ENABLE,
3207 					      state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
3208 			for (j = 0; j < adev->gfx.mec.num_mec; j++) {
3209 				for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
3210 					/* MECs start at 1 */
3211 					mec_int_cntl_reg = gfx_v9_4_3_get_cpc_int_cntl(adev, i, j + 1, k);
3212 
3213 					if (mec_int_cntl_reg) {
3214 						mec_int_cntl = RREG32_XCC(mec_int_cntl_reg, i);
3215 						mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
3216 									     OPCODE_ERROR_INT_ENABLE,
3217 									     state == AMDGPU_IRQ_STATE_ENABLE ?
3218 									     1 : 0);
3219 						WREG32_XCC(mec_int_cntl_reg, mec_int_cntl, i);
3220 					}
3221 				}
3222 			}
3223 		}
3224 		break;
3225 	default:
3226 		break;
3227 	}
3228 
3229 	return 0;
3230 }
3231 
3232 static int gfx_v9_4_3_set_priv_inst_fault_state(struct amdgpu_device *adev,
3233 					      struct amdgpu_irq_src *source,
3234 					      unsigned type,
3235 					      enum amdgpu_interrupt_state state)
3236 {
3237 	int i, num_xcc;
3238 
3239 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
3240 	switch (state) {
3241 	case AMDGPU_IRQ_STATE_DISABLE:
3242 	case AMDGPU_IRQ_STATE_ENABLE:
3243 		for (i = 0; i < num_xcc; i++)
3244 			WREG32_FIELD15_PREREG(GC, GET_INST(GC, i), CP_INT_CNTL_RING0,
3245 				PRIV_INSTR_INT_ENABLE,
3246 				state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
3247 		break;
3248 	default:
3249 		break;
3250 	}
3251 
3252 	return 0;
3253 }
3254 
3255 static int gfx_v9_4_3_set_eop_interrupt_state(struct amdgpu_device *adev,
3256 					    struct amdgpu_irq_src *src,
3257 					    unsigned type,
3258 					    enum amdgpu_interrupt_state state)
3259 {
3260 	int i, num_xcc;
3261 
3262 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
3263 	for (i = 0; i < num_xcc; i++) {
3264 		switch (type) {
3265 		case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP:
3266 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3267 				adev, 1, 0, state, i);
3268 			break;
3269 		case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE1_EOP:
3270 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3271 				adev, 1, 1, state, i);
3272 			break;
3273 		case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE2_EOP:
3274 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3275 				adev, 1, 2, state, i);
3276 			break;
3277 		case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE3_EOP:
3278 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3279 				adev, 1, 3, state, i);
3280 			break;
3281 		case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE0_EOP:
3282 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3283 				adev, 2, 0, state, i);
3284 			break;
3285 		case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE1_EOP:
3286 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3287 				adev, 2, 1, state, i);
3288 			break;
3289 		case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE2_EOP:
3290 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3291 				adev, 2, 2, state, i);
3292 			break;
3293 		case AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE3_EOP:
3294 			gfx_v9_4_3_xcc_set_compute_eop_interrupt_state(
3295 				adev, 2, 3, state, i);
3296 			break;
3297 		default:
3298 			break;
3299 		}
3300 	}
3301 
3302 	return 0;
3303 }
3304 
3305 static int gfx_v9_4_3_eop_irq(struct amdgpu_device *adev,
3306 			    struct amdgpu_irq_src *source,
3307 			    struct amdgpu_iv_entry *entry)
3308 {
3309 	int i, xcc_id;
3310 	u8 me_id, pipe_id, queue_id;
3311 	struct amdgpu_ring *ring;
3312 
3313 	DRM_DEBUG("IH: CP EOP\n");
3314 	me_id = (entry->ring_id & 0x0c) >> 2;
3315 	pipe_id = (entry->ring_id & 0x03) >> 0;
3316 	queue_id = (entry->ring_id & 0x70) >> 4;
3317 
3318 	xcc_id = gfx_v9_4_3_ih_to_xcc_inst(adev, entry->node_id);
3319 
3320 	if (xcc_id == -EINVAL)
3321 		return -EINVAL;
3322 
3323 	switch (me_id) {
3324 	case 0:
3325 	case 1:
3326 	case 2:
3327 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3328 			ring = &adev->gfx.compute_ring
3329 					[i +
3330 					 xcc_id * adev->gfx.num_compute_rings];
3331 			/* Per-queue interrupt is supported for MEC starting from VI.
3332 			  * The interrupt can only be enabled/disabled per pipe instead of per queue.
3333 			  */
3334 
3335 			if ((ring->me == me_id) && (ring->pipe == pipe_id) && (ring->queue == queue_id))
3336 				amdgpu_fence_process(ring);
3337 		}
3338 		break;
3339 	}
3340 	return 0;
3341 }
3342 
3343 static void gfx_v9_4_3_fault(struct amdgpu_device *adev,
3344 			   struct amdgpu_iv_entry *entry)
3345 {
3346 	u8 me_id, pipe_id, queue_id;
3347 	struct amdgpu_ring *ring;
3348 	int i, xcc_id;
3349 
3350 	me_id = (entry->ring_id & 0x0c) >> 2;
3351 	pipe_id = (entry->ring_id & 0x03) >> 0;
3352 	queue_id = (entry->ring_id & 0x70) >> 4;
3353 
3354 	xcc_id = gfx_v9_4_3_ih_to_xcc_inst(adev, entry->node_id);
3355 
3356 	if (xcc_id == -EINVAL)
3357 		return;
3358 
3359 	switch (me_id) {
3360 	case 0:
3361 	case 1:
3362 	case 2:
3363 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
3364 			ring = &adev->gfx.compute_ring
3365 					[i +
3366 					 xcc_id * adev->gfx.num_compute_rings];
3367 			if (ring->me == me_id && ring->pipe == pipe_id &&
3368 			    ring->queue == queue_id)
3369 				drm_sched_fault(&ring->sched);
3370 		}
3371 		break;
3372 	}
3373 }
3374 
3375 static int gfx_v9_4_3_priv_reg_irq(struct amdgpu_device *adev,
3376 				 struct amdgpu_irq_src *source,
3377 				 struct amdgpu_iv_entry *entry)
3378 {
3379 	DRM_ERROR("Illegal register access in command stream\n");
3380 	gfx_v9_4_3_fault(adev, entry);
3381 	return 0;
3382 }
3383 
3384 static int gfx_v9_4_3_bad_op_irq(struct amdgpu_device *adev,
3385 				 struct amdgpu_irq_src *source,
3386 				 struct amdgpu_iv_entry *entry)
3387 {
3388 	DRM_ERROR("Illegal opcode in command stream\n");
3389 	gfx_v9_4_3_fault(adev, entry);
3390 	return 0;
3391 }
3392 
3393 static int gfx_v9_4_3_priv_inst_irq(struct amdgpu_device *adev,
3394 				  struct amdgpu_irq_src *source,
3395 				  struct amdgpu_iv_entry *entry)
3396 {
3397 	DRM_ERROR("Illegal instruction in command stream\n");
3398 	gfx_v9_4_3_fault(adev, entry);
3399 	return 0;
3400 }
3401 
3402 static void gfx_v9_4_3_emit_mem_sync(struct amdgpu_ring *ring)
3403 {
3404 	const unsigned int cp_coher_cntl =
3405 			PACKET3_ACQUIRE_MEM_CP_COHER_CNTL_SH_ICACHE_ACTION_ENA(1) |
3406 			PACKET3_ACQUIRE_MEM_CP_COHER_CNTL_SH_KCACHE_ACTION_ENA(1) |
3407 			PACKET3_ACQUIRE_MEM_CP_COHER_CNTL_TC_ACTION_ENA(1) |
3408 			PACKET3_ACQUIRE_MEM_CP_COHER_CNTL_TCL1_ACTION_ENA(1) |
3409 			PACKET3_ACQUIRE_MEM_CP_COHER_CNTL_TC_WB_ACTION_ENA(1);
3410 
3411 	/* ACQUIRE_MEM -make one or more surfaces valid for use by the subsequent operations */
3412 	amdgpu_ring_write(ring, PACKET3(PACKET3_ACQUIRE_MEM, 5));
3413 	amdgpu_ring_write(ring, cp_coher_cntl); /* CP_COHER_CNTL */
3414 	amdgpu_ring_write(ring, 0xffffffff);  /* CP_COHER_SIZE */
3415 	amdgpu_ring_write(ring, 0xffffff);  /* CP_COHER_SIZE_HI */
3416 	amdgpu_ring_write(ring, 0); /* CP_COHER_BASE */
3417 	amdgpu_ring_write(ring, 0);  /* CP_COHER_BASE_HI */
3418 	amdgpu_ring_write(ring, 0x0000000A); /* POLL_INTERVAL */
3419 }
3420 
3421 static void gfx_v9_4_3_emit_wave_limit_cs(struct amdgpu_ring *ring,
3422 					uint32_t pipe, bool enable)
3423 {
3424 	struct amdgpu_device *adev = ring->adev;
3425 	uint32_t val;
3426 	uint32_t wcl_cs_reg;
3427 
3428 	/* regSPI_WCL_PIPE_PERCENT_CS[0-7]_DEFAULT values are same */
3429 	val = enable ? 0x1 : 0x7f;
3430 
3431 	switch (pipe) {
3432 	case 0:
3433 		wcl_cs_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regSPI_WCL_PIPE_PERCENT_CS0);
3434 		break;
3435 	case 1:
3436 		wcl_cs_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regSPI_WCL_PIPE_PERCENT_CS1);
3437 		break;
3438 	case 2:
3439 		wcl_cs_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regSPI_WCL_PIPE_PERCENT_CS2);
3440 		break;
3441 	case 3:
3442 		wcl_cs_reg = SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regSPI_WCL_PIPE_PERCENT_CS3);
3443 		break;
3444 	default:
3445 		DRM_DEBUG("invalid pipe %d\n", pipe);
3446 		return;
3447 	}
3448 
3449 	amdgpu_ring_emit_wreg(ring, wcl_cs_reg, val);
3450 
3451 }
3452 static void gfx_v9_4_3_emit_wave_limit(struct amdgpu_ring *ring, bool enable)
3453 {
3454 	struct amdgpu_device *adev = ring->adev;
3455 	uint32_t val;
3456 	int i;
3457 
3458 	/* regSPI_WCL_PIPE_PERCENT_GFX is 7 bit multiplier register to limit
3459 	 * number of gfx waves. Setting 5 bit will make sure gfx only gets
3460 	 * around 25% of gpu resources.
3461 	 */
3462 	val = enable ? 0x1f : 0x07ffffff;
3463 	amdgpu_ring_emit_wreg(ring,
3464 			      SOC15_REG_OFFSET(GC, GET_INST(GC, 0), regSPI_WCL_PIPE_PERCENT_GFX),
3465 			      val);
3466 
3467 	/* Restrict waves for normal/low priority compute queues as well
3468 	 * to get best QoS for high priority compute jobs.
3469 	 *
3470 	 * amdgpu controls only 1st ME(0-3 CS pipes).
3471 	 */
3472 	for (i = 0; i < adev->gfx.mec.num_pipe_per_mec; i++) {
3473 		if (i != ring->pipe)
3474 			gfx_v9_4_3_emit_wave_limit_cs(ring, i, enable);
3475 
3476 	}
3477 }
3478 
3479 static int gfx_v9_4_3_unmap_done(struct amdgpu_device *adev, uint32_t me,
3480 				uint32_t pipe, uint32_t queue,
3481 				uint32_t xcc_id)
3482 {
3483 	int i, r;
3484 	/* make sure dequeue is complete*/
3485 	gfx_v9_4_3_xcc_set_safe_mode(adev, xcc_id);
3486 	mutex_lock(&adev->srbm_mutex);
3487 	soc15_grbm_select(adev, me, pipe, queue, 0, GET_INST(GC, xcc_id));
3488 	for (i = 0; i < adev->usec_timeout; i++) {
3489 		if (!(RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_HQD_ACTIVE) & 1))
3490 			break;
3491 		udelay(1);
3492 	}
3493 	if (i >= adev->usec_timeout)
3494 		r = -ETIMEDOUT;
3495 	else
3496 		r = 0;
3497 	soc15_grbm_select(adev, 0, 0, 0, 0, GET_INST(GC, xcc_id));
3498 	mutex_unlock(&adev->srbm_mutex);
3499 	gfx_v9_4_3_xcc_unset_safe_mode(adev, xcc_id);
3500 
3501 	return r;
3502 
3503 }
3504 
3505 static bool gfx_v9_4_3_pipe_reset_support(struct amdgpu_device *adev)
3506 {
3507 	/*TODO: Need check gfx9.4.4 mec fw whether supports pipe reset as well.*/
3508 	if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) &&
3509 			adev->gfx.mec_fw_version >= 0x0000009b)
3510 		return true;
3511 	else
3512 		dev_warn_once(adev->dev, "Please use the latest MEC version to see whether support pipe reset\n");
3513 
3514 	return false;
3515 }
3516 
3517 static int gfx_v9_4_3_reset_hw_pipe(struct amdgpu_ring *ring)
3518 {
3519 	struct amdgpu_device *adev = ring->adev;
3520 	uint32_t reset_pipe, clean_pipe;
3521 	int r;
3522 
3523 	if (!gfx_v9_4_3_pipe_reset_support(adev))
3524 		return -EINVAL;
3525 
3526 	gfx_v9_4_3_xcc_set_safe_mode(adev, ring->xcc_id);
3527 	mutex_lock(&adev->srbm_mutex);
3528 
3529 	reset_pipe = RREG32_SOC15(GC, GET_INST(GC, ring->xcc_id), regCP_MEC_CNTL);
3530 	clean_pipe = reset_pipe;
3531 
3532 	if (ring->me == 1) {
3533 		switch (ring->pipe) {
3534 		case 0:
3535 			reset_pipe = REG_SET_FIELD(reset_pipe, CP_MEC_CNTL,
3536 						   MEC_ME1_PIPE0_RESET, 1);
3537 			break;
3538 		case 1:
3539 			reset_pipe = REG_SET_FIELD(reset_pipe, CP_MEC_CNTL,
3540 						   MEC_ME1_PIPE1_RESET, 1);
3541 			break;
3542 		case 2:
3543 			reset_pipe = REG_SET_FIELD(reset_pipe, CP_MEC_CNTL,
3544 						   MEC_ME1_PIPE2_RESET, 1);
3545 			break;
3546 		case 3:
3547 			reset_pipe = REG_SET_FIELD(reset_pipe, CP_MEC_CNTL,
3548 						   MEC_ME1_PIPE3_RESET, 1);
3549 			break;
3550 		default:
3551 			break;
3552 		}
3553 	} else {
3554 		if (ring->pipe)
3555 			reset_pipe = REG_SET_FIELD(reset_pipe, CP_MEC_CNTL,
3556 						   MEC_ME2_PIPE1_RESET, 1);
3557 		else
3558 			reset_pipe = REG_SET_FIELD(reset_pipe, CP_MEC_CNTL,
3559 						   MEC_ME2_PIPE0_RESET, 1);
3560 	}
3561 
3562 	WREG32_SOC15(GC, GET_INST(GC, ring->xcc_id), regCP_MEC_CNTL, reset_pipe);
3563 	WREG32_SOC15(GC, GET_INST(GC, ring->xcc_id), regCP_MEC_CNTL, clean_pipe);
3564 	mutex_unlock(&adev->srbm_mutex);
3565 	gfx_v9_4_3_xcc_unset_safe_mode(adev, ring->xcc_id);
3566 
3567 	r = gfx_v9_4_3_unmap_done(adev, ring->me, ring->pipe, ring->queue, ring->xcc_id);
3568 	return r;
3569 }
3570 
3571 static int gfx_v9_4_3_reset_kcq(struct amdgpu_ring *ring,
3572 				unsigned int vmid)
3573 {
3574 	struct amdgpu_device *adev = ring->adev;
3575 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[ring->xcc_id];
3576 	struct amdgpu_ring *kiq_ring = &kiq->ring;
3577 	unsigned long flags;
3578 	int r;
3579 
3580 	if (amdgpu_sriov_vf(adev))
3581 		return -EINVAL;
3582 
3583 	if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
3584 		return -EINVAL;
3585 
3586 	spin_lock_irqsave(&kiq->ring_lock, flags);
3587 
3588 	if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size)) {
3589 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
3590 		return -ENOMEM;
3591 	}
3592 
3593 	kiq->pmf->kiq_unmap_queues(kiq_ring, ring, RESET_QUEUES,
3594 				   0, 0);
3595 	amdgpu_ring_commit(kiq_ring);
3596 
3597 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
3598 
3599 	r = amdgpu_ring_test_ring(kiq_ring);
3600 	if (r) {
3601 		dev_err(adev->dev, "kiq ring test failed after ring: %s queue reset\n",
3602 				ring->name);
3603 		goto pipe_reset;
3604 	}
3605 
3606 	r = gfx_v9_4_3_unmap_done(adev, ring->me, ring->pipe, ring->queue, ring->xcc_id);
3607 	if (r)
3608 		dev_err(adev->dev, "fail to wait on hqd deactive and will try pipe reset\n");
3609 
3610 pipe_reset:
3611 	if(r) {
3612 		r = gfx_v9_4_3_reset_hw_pipe(ring);
3613 		dev_info(adev->dev, "ring: %s pipe reset :%s\n", ring->name,
3614 				r ? "failed" : "successfully");
3615 		if (r)
3616 			return r;
3617 	}
3618 
3619 	r = amdgpu_bo_reserve(ring->mqd_obj, false);
3620 	if (unlikely(r != 0)){
3621 		dev_err(adev->dev, "fail to resv mqd_obj\n");
3622 		return r;
3623 	}
3624 	r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
3625 	if (!r) {
3626 		r = gfx_v9_4_3_xcc_kcq_init_queue(ring, ring->xcc_id, true);
3627 		amdgpu_bo_kunmap(ring->mqd_obj);
3628 		ring->mqd_ptr = NULL;
3629 	}
3630 	amdgpu_bo_unreserve(ring->mqd_obj);
3631 	if (r) {
3632 		dev_err(adev->dev, "fail to unresv mqd_obj\n");
3633 		return r;
3634 	}
3635 	spin_lock_irqsave(&kiq->ring_lock, flags);
3636 	r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size);
3637 	if (r) {
3638 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
3639 		return -ENOMEM;
3640 	}
3641 	kiq->pmf->kiq_map_queues(kiq_ring, ring);
3642 	amdgpu_ring_commit(kiq_ring);
3643 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
3644 
3645 	r = amdgpu_ring_test_ring(kiq_ring);
3646 	if (r) {
3647 		dev_err(adev->dev, "fail to remap queue\n");
3648 		return r;
3649 	}
3650 	return amdgpu_ring_test_ring(ring);
3651 }
3652 
3653 enum amdgpu_gfx_cp_ras_mem_id {
3654 	AMDGPU_GFX_CP_MEM1 = 1,
3655 	AMDGPU_GFX_CP_MEM2,
3656 	AMDGPU_GFX_CP_MEM3,
3657 	AMDGPU_GFX_CP_MEM4,
3658 	AMDGPU_GFX_CP_MEM5,
3659 };
3660 
3661 enum amdgpu_gfx_gcea_ras_mem_id {
3662 	AMDGPU_GFX_GCEA_IOWR_CMDMEM = 4,
3663 	AMDGPU_GFX_GCEA_IORD_CMDMEM,
3664 	AMDGPU_GFX_GCEA_GMIWR_CMDMEM,
3665 	AMDGPU_GFX_GCEA_GMIRD_CMDMEM,
3666 	AMDGPU_GFX_GCEA_DRAMWR_CMDMEM,
3667 	AMDGPU_GFX_GCEA_DRAMRD_CMDMEM,
3668 	AMDGPU_GFX_GCEA_MAM_DMEM0,
3669 	AMDGPU_GFX_GCEA_MAM_DMEM1,
3670 	AMDGPU_GFX_GCEA_MAM_DMEM2,
3671 	AMDGPU_GFX_GCEA_MAM_DMEM3,
3672 	AMDGPU_GFX_GCEA_MAM_AMEM0,
3673 	AMDGPU_GFX_GCEA_MAM_AMEM1,
3674 	AMDGPU_GFX_GCEA_MAM_AMEM2,
3675 	AMDGPU_GFX_GCEA_MAM_AMEM3,
3676 	AMDGPU_GFX_GCEA_MAM_AFLUSH_BUFFER,
3677 	AMDGPU_GFX_GCEA_WRET_TAGMEM,
3678 	AMDGPU_GFX_GCEA_RRET_TAGMEM,
3679 	AMDGPU_GFX_GCEA_IOWR_DATAMEM,
3680 	AMDGPU_GFX_GCEA_GMIWR_DATAMEM,
3681 	AMDGPU_GFX_GCEA_DRAM_DATAMEM,
3682 };
3683 
3684 enum amdgpu_gfx_gc_cane_ras_mem_id {
3685 	AMDGPU_GFX_GC_CANE_MEM0 = 0,
3686 };
3687 
3688 enum amdgpu_gfx_gcutcl2_ras_mem_id {
3689 	AMDGPU_GFX_GCUTCL2_MEM2P512X95 = 160,
3690 };
3691 
3692 enum amdgpu_gfx_gds_ras_mem_id {
3693 	AMDGPU_GFX_GDS_MEM0 = 0,
3694 };
3695 
3696 enum amdgpu_gfx_lds_ras_mem_id {
3697 	AMDGPU_GFX_LDS_BANK0 = 0,
3698 	AMDGPU_GFX_LDS_BANK1,
3699 	AMDGPU_GFX_LDS_BANK2,
3700 	AMDGPU_GFX_LDS_BANK3,
3701 	AMDGPU_GFX_LDS_BANK4,
3702 	AMDGPU_GFX_LDS_BANK5,
3703 	AMDGPU_GFX_LDS_BANK6,
3704 	AMDGPU_GFX_LDS_BANK7,
3705 	AMDGPU_GFX_LDS_BANK8,
3706 	AMDGPU_GFX_LDS_BANK9,
3707 	AMDGPU_GFX_LDS_BANK10,
3708 	AMDGPU_GFX_LDS_BANK11,
3709 	AMDGPU_GFX_LDS_BANK12,
3710 	AMDGPU_GFX_LDS_BANK13,
3711 	AMDGPU_GFX_LDS_BANK14,
3712 	AMDGPU_GFX_LDS_BANK15,
3713 	AMDGPU_GFX_LDS_BANK16,
3714 	AMDGPU_GFX_LDS_BANK17,
3715 	AMDGPU_GFX_LDS_BANK18,
3716 	AMDGPU_GFX_LDS_BANK19,
3717 	AMDGPU_GFX_LDS_BANK20,
3718 	AMDGPU_GFX_LDS_BANK21,
3719 	AMDGPU_GFX_LDS_BANK22,
3720 	AMDGPU_GFX_LDS_BANK23,
3721 	AMDGPU_GFX_LDS_BANK24,
3722 	AMDGPU_GFX_LDS_BANK25,
3723 	AMDGPU_GFX_LDS_BANK26,
3724 	AMDGPU_GFX_LDS_BANK27,
3725 	AMDGPU_GFX_LDS_BANK28,
3726 	AMDGPU_GFX_LDS_BANK29,
3727 	AMDGPU_GFX_LDS_BANK30,
3728 	AMDGPU_GFX_LDS_BANK31,
3729 	AMDGPU_GFX_LDS_SP_BUFFER_A,
3730 	AMDGPU_GFX_LDS_SP_BUFFER_B,
3731 };
3732 
3733 enum amdgpu_gfx_rlc_ras_mem_id {
3734 	AMDGPU_GFX_RLC_GPMF32 = 1,
3735 	AMDGPU_GFX_RLC_RLCVF32,
3736 	AMDGPU_GFX_RLC_SCRATCH,
3737 	AMDGPU_GFX_RLC_SRM_ARAM,
3738 	AMDGPU_GFX_RLC_SRM_DRAM,
3739 	AMDGPU_GFX_RLC_TCTAG,
3740 	AMDGPU_GFX_RLC_SPM_SE,
3741 	AMDGPU_GFX_RLC_SPM_GRBMT,
3742 };
3743 
3744 enum amdgpu_gfx_sp_ras_mem_id {
3745 	AMDGPU_GFX_SP_SIMDID0 = 0,
3746 };
3747 
3748 enum amdgpu_gfx_spi_ras_mem_id {
3749 	AMDGPU_GFX_SPI_MEM0 = 0,
3750 	AMDGPU_GFX_SPI_MEM1,
3751 	AMDGPU_GFX_SPI_MEM2,
3752 	AMDGPU_GFX_SPI_MEM3,
3753 };
3754 
3755 enum amdgpu_gfx_sqc_ras_mem_id {
3756 	AMDGPU_GFX_SQC_INST_CACHE_A = 100,
3757 	AMDGPU_GFX_SQC_INST_CACHE_B = 101,
3758 	AMDGPU_GFX_SQC_INST_CACHE_TAG_A = 102,
3759 	AMDGPU_GFX_SQC_INST_CACHE_TAG_B = 103,
3760 	AMDGPU_GFX_SQC_INST_CACHE_MISS_FIFO_A = 104,
3761 	AMDGPU_GFX_SQC_INST_CACHE_MISS_FIFO_B = 105,
3762 	AMDGPU_GFX_SQC_INST_CACHE_GATCL1_MISS_FIFO_A = 106,
3763 	AMDGPU_GFX_SQC_INST_CACHE_GATCL1_MISS_FIFO_B = 107,
3764 	AMDGPU_GFX_SQC_DATA_CACHE_A = 200,
3765 	AMDGPU_GFX_SQC_DATA_CACHE_B = 201,
3766 	AMDGPU_GFX_SQC_DATA_CACHE_TAG_A = 202,
3767 	AMDGPU_GFX_SQC_DATA_CACHE_TAG_B = 203,
3768 	AMDGPU_GFX_SQC_DATA_CACHE_MISS_FIFO_A = 204,
3769 	AMDGPU_GFX_SQC_DATA_CACHE_MISS_FIFO_B = 205,
3770 	AMDGPU_GFX_SQC_DATA_CACHE_HIT_FIFO_A = 206,
3771 	AMDGPU_GFX_SQC_DATA_CACHE_HIT_FIFO_B = 207,
3772 	AMDGPU_GFX_SQC_DIRTY_BIT_A = 208,
3773 	AMDGPU_GFX_SQC_DIRTY_BIT_B = 209,
3774 	AMDGPU_GFX_SQC_WRITE_DATA_BUFFER_CU0 = 210,
3775 	AMDGPU_GFX_SQC_WRITE_DATA_BUFFER_CU1 = 211,
3776 	AMDGPU_GFX_SQC_UTCL1_MISS_LFIFO_DATA_CACHE_A = 212,
3777 	AMDGPU_GFX_SQC_UTCL1_MISS_LFIFO_DATA_CACHE_B = 213,
3778 	AMDGPU_GFX_SQC_UTCL1_MISS_LFIFO_INST_CACHE = 108,
3779 };
3780 
3781 enum amdgpu_gfx_sq_ras_mem_id {
3782 	AMDGPU_GFX_SQ_SGPR_MEM0 = 0,
3783 	AMDGPU_GFX_SQ_SGPR_MEM1,
3784 	AMDGPU_GFX_SQ_SGPR_MEM2,
3785 	AMDGPU_GFX_SQ_SGPR_MEM3,
3786 };
3787 
3788 enum amdgpu_gfx_ta_ras_mem_id {
3789 	AMDGPU_GFX_TA_FS_AFIFO_RAM_LO = 1,
3790 	AMDGPU_GFX_TA_FS_AFIFO_RAM_HI,
3791 	AMDGPU_GFX_TA_FS_CFIFO_RAM,
3792 	AMDGPU_GFX_TA_FSX_LFIFO,
3793 	AMDGPU_GFX_TA_FS_DFIFO_RAM,
3794 };
3795 
3796 enum amdgpu_gfx_tcc_ras_mem_id {
3797 	AMDGPU_GFX_TCC_MEM1 = 1,
3798 };
3799 
3800 enum amdgpu_gfx_tca_ras_mem_id {
3801 	AMDGPU_GFX_TCA_MEM1 = 1,
3802 };
3803 
3804 enum amdgpu_gfx_tci_ras_mem_id {
3805 	AMDGPU_GFX_TCIW_MEM = 1,
3806 };
3807 
3808 enum amdgpu_gfx_tcp_ras_mem_id {
3809 	AMDGPU_GFX_TCP_LFIFO0 = 1,
3810 	AMDGPU_GFX_TCP_SET0BANK0_RAM,
3811 	AMDGPU_GFX_TCP_SET0BANK1_RAM,
3812 	AMDGPU_GFX_TCP_SET0BANK2_RAM,
3813 	AMDGPU_GFX_TCP_SET0BANK3_RAM,
3814 	AMDGPU_GFX_TCP_SET1BANK0_RAM,
3815 	AMDGPU_GFX_TCP_SET1BANK1_RAM,
3816 	AMDGPU_GFX_TCP_SET1BANK2_RAM,
3817 	AMDGPU_GFX_TCP_SET1BANK3_RAM,
3818 	AMDGPU_GFX_TCP_SET2BANK0_RAM,
3819 	AMDGPU_GFX_TCP_SET2BANK1_RAM,
3820 	AMDGPU_GFX_TCP_SET2BANK2_RAM,
3821 	AMDGPU_GFX_TCP_SET2BANK3_RAM,
3822 	AMDGPU_GFX_TCP_SET3BANK0_RAM,
3823 	AMDGPU_GFX_TCP_SET3BANK1_RAM,
3824 	AMDGPU_GFX_TCP_SET3BANK2_RAM,
3825 	AMDGPU_GFX_TCP_SET3BANK3_RAM,
3826 	AMDGPU_GFX_TCP_VM_FIFO,
3827 	AMDGPU_GFX_TCP_DB_TAGRAM0,
3828 	AMDGPU_GFX_TCP_DB_TAGRAM1,
3829 	AMDGPU_GFX_TCP_DB_TAGRAM2,
3830 	AMDGPU_GFX_TCP_DB_TAGRAM3,
3831 	AMDGPU_GFX_TCP_UTCL1_LFIFO_PROBE0,
3832 	AMDGPU_GFX_TCP_UTCL1_LFIFO_PROBE1,
3833 	AMDGPU_GFX_TCP_CMD_FIFO,
3834 };
3835 
3836 enum amdgpu_gfx_td_ras_mem_id {
3837 	AMDGPU_GFX_TD_UTD_CS_FIFO_MEM = 1,
3838 	AMDGPU_GFX_TD_UTD_SS_FIFO_LO_MEM,
3839 	AMDGPU_GFX_TD_UTD_SS_FIFO_HI_MEM,
3840 };
3841 
3842 enum amdgpu_gfx_tcx_ras_mem_id {
3843 	AMDGPU_GFX_TCX_FIFOD0 = 0,
3844 	AMDGPU_GFX_TCX_FIFOD1,
3845 	AMDGPU_GFX_TCX_FIFOD2,
3846 	AMDGPU_GFX_TCX_FIFOD3,
3847 	AMDGPU_GFX_TCX_FIFOD4,
3848 	AMDGPU_GFX_TCX_FIFOD5,
3849 	AMDGPU_GFX_TCX_FIFOD6,
3850 	AMDGPU_GFX_TCX_FIFOD7,
3851 	AMDGPU_GFX_TCX_FIFOB0,
3852 	AMDGPU_GFX_TCX_FIFOB1,
3853 	AMDGPU_GFX_TCX_FIFOB2,
3854 	AMDGPU_GFX_TCX_FIFOB3,
3855 	AMDGPU_GFX_TCX_FIFOB4,
3856 	AMDGPU_GFX_TCX_FIFOB5,
3857 	AMDGPU_GFX_TCX_FIFOB6,
3858 	AMDGPU_GFX_TCX_FIFOB7,
3859 	AMDGPU_GFX_TCX_FIFOA0,
3860 	AMDGPU_GFX_TCX_FIFOA1,
3861 	AMDGPU_GFX_TCX_FIFOA2,
3862 	AMDGPU_GFX_TCX_FIFOA3,
3863 	AMDGPU_GFX_TCX_FIFOA4,
3864 	AMDGPU_GFX_TCX_FIFOA5,
3865 	AMDGPU_GFX_TCX_FIFOA6,
3866 	AMDGPU_GFX_TCX_FIFOA7,
3867 	AMDGPU_GFX_TCX_CFIFO0,
3868 	AMDGPU_GFX_TCX_CFIFO1,
3869 	AMDGPU_GFX_TCX_CFIFO2,
3870 	AMDGPU_GFX_TCX_CFIFO3,
3871 	AMDGPU_GFX_TCX_CFIFO4,
3872 	AMDGPU_GFX_TCX_CFIFO5,
3873 	AMDGPU_GFX_TCX_CFIFO6,
3874 	AMDGPU_GFX_TCX_CFIFO7,
3875 	AMDGPU_GFX_TCX_FIFO_ACKB0,
3876 	AMDGPU_GFX_TCX_FIFO_ACKB1,
3877 	AMDGPU_GFX_TCX_FIFO_ACKB2,
3878 	AMDGPU_GFX_TCX_FIFO_ACKB3,
3879 	AMDGPU_GFX_TCX_FIFO_ACKB4,
3880 	AMDGPU_GFX_TCX_FIFO_ACKB5,
3881 	AMDGPU_GFX_TCX_FIFO_ACKB6,
3882 	AMDGPU_GFX_TCX_FIFO_ACKB7,
3883 	AMDGPU_GFX_TCX_FIFO_ACKD0,
3884 	AMDGPU_GFX_TCX_FIFO_ACKD1,
3885 	AMDGPU_GFX_TCX_FIFO_ACKD2,
3886 	AMDGPU_GFX_TCX_FIFO_ACKD3,
3887 	AMDGPU_GFX_TCX_FIFO_ACKD4,
3888 	AMDGPU_GFX_TCX_FIFO_ACKD5,
3889 	AMDGPU_GFX_TCX_FIFO_ACKD6,
3890 	AMDGPU_GFX_TCX_FIFO_ACKD7,
3891 	AMDGPU_GFX_TCX_DST_FIFOA0,
3892 	AMDGPU_GFX_TCX_DST_FIFOA1,
3893 	AMDGPU_GFX_TCX_DST_FIFOA2,
3894 	AMDGPU_GFX_TCX_DST_FIFOA3,
3895 	AMDGPU_GFX_TCX_DST_FIFOA4,
3896 	AMDGPU_GFX_TCX_DST_FIFOA5,
3897 	AMDGPU_GFX_TCX_DST_FIFOA6,
3898 	AMDGPU_GFX_TCX_DST_FIFOA7,
3899 	AMDGPU_GFX_TCX_DST_FIFOB0,
3900 	AMDGPU_GFX_TCX_DST_FIFOB1,
3901 	AMDGPU_GFX_TCX_DST_FIFOB2,
3902 	AMDGPU_GFX_TCX_DST_FIFOB3,
3903 	AMDGPU_GFX_TCX_DST_FIFOB4,
3904 	AMDGPU_GFX_TCX_DST_FIFOB5,
3905 	AMDGPU_GFX_TCX_DST_FIFOB6,
3906 	AMDGPU_GFX_TCX_DST_FIFOB7,
3907 	AMDGPU_GFX_TCX_DST_FIFOD0,
3908 	AMDGPU_GFX_TCX_DST_FIFOD1,
3909 	AMDGPU_GFX_TCX_DST_FIFOD2,
3910 	AMDGPU_GFX_TCX_DST_FIFOD3,
3911 	AMDGPU_GFX_TCX_DST_FIFOD4,
3912 	AMDGPU_GFX_TCX_DST_FIFOD5,
3913 	AMDGPU_GFX_TCX_DST_FIFOD6,
3914 	AMDGPU_GFX_TCX_DST_FIFOD7,
3915 	AMDGPU_GFX_TCX_DST_FIFO_ACKB0,
3916 	AMDGPU_GFX_TCX_DST_FIFO_ACKB1,
3917 	AMDGPU_GFX_TCX_DST_FIFO_ACKB2,
3918 	AMDGPU_GFX_TCX_DST_FIFO_ACKB3,
3919 	AMDGPU_GFX_TCX_DST_FIFO_ACKB4,
3920 	AMDGPU_GFX_TCX_DST_FIFO_ACKB5,
3921 	AMDGPU_GFX_TCX_DST_FIFO_ACKB6,
3922 	AMDGPU_GFX_TCX_DST_FIFO_ACKB7,
3923 	AMDGPU_GFX_TCX_DST_FIFO_ACKD0,
3924 	AMDGPU_GFX_TCX_DST_FIFO_ACKD1,
3925 	AMDGPU_GFX_TCX_DST_FIFO_ACKD2,
3926 	AMDGPU_GFX_TCX_DST_FIFO_ACKD3,
3927 	AMDGPU_GFX_TCX_DST_FIFO_ACKD4,
3928 	AMDGPU_GFX_TCX_DST_FIFO_ACKD5,
3929 	AMDGPU_GFX_TCX_DST_FIFO_ACKD6,
3930 	AMDGPU_GFX_TCX_DST_FIFO_ACKD7,
3931 };
3932 
3933 enum amdgpu_gfx_atc_l2_ras_mem_id {
3934 	AMDGPU_GFX_ATC_L2_MEM0 = 0,
3935 };
3936 
3937 enum amdgpu_gfx_utcl2_ras_mem_id {
3938 	AMDGPU_GFX_UTCL2_MEM0 = 0,
3939 };
3940 
3941 enum amdgpu_gfx_vml2_ras_mem_id {
3942 	AMDGPU_GFX_VML2_MEM0 = 0,
3943 };
3944 
3945 enum amdgpu_gfx_vml2_walker_ras_mem_id {
3946 	AMDGPU_GFX_VML2_WALKER_MEM0 = 0,
3947 };
3948 
3949 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_cp_mem_list[] = {
3950 	{AMDGPU_GFX_CP_MEM1, "CP_MEM1"},
3951 	{AMDGPU_GFX_CP_MEM2, "CP_MEM2"},
3952 	{AMDGPU_GFX_CP_MEM3, "CP_MEM3"},
3953 	{AMDGPU_GFX_CP_MEM4, "CP_MEM4"},
3954 	{AMDGPU_GFX_CP_MEM5, "CP_MEM5"},
3955 };
3956 
3957 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_gcea_mem_list[] = {
3958 	{AMDGPU_GFX_GCEA_IOWR_CMDMEM, "GCEA_IOWR_CMDMEM"},
3959 	{AMDGPU_GFX_GCEA_IORD_CMDMEM, "GCEA_IORD_CMDMEM"},
3960 	{AMDGPU_GFX_GCEA_GMIWR_CMDMEM, "GCEA_GMIWR_CMDMEM"},
3961 	{AMDGPU_GFX_GCEA_GMIRD_CMDMEM, "GCEA_GMIRD_CMDMEM"},
3962 	{AMDGPU_GFX_GCEA_DRAMWR_CMDMEM, "GCEA_DRAMWR_CMDMEM"},
3963 	{AMDGPU_GFX_GCEA_DRAMRD_CMDMEM, "GCEA_DRAMRD_CMDMEM"},
3964 	{AMDGPU_GFX_GCEA_MAM_DMEM0, "GCEA_MAM_DMEM0"},
3965 	{AMDGPU_GFX_GCEA_MAM_DMEM1, "GCEA_MAM_DMEM1"},
3966 	{AMDGPU_GFX_GCEA_MAM_DMEM2, "GCEA_MAM_DMEM2"},
3967 	{AMDGPU_GFX_GCEA_MAM_DMEM3, "GCEA_MAM_DMEM3"},
3968 	{AMDGPU_GFX_GCEA_MAM_AMEM0, "GCEA_MAM_AMEM0"},
3969 	{AMDGPU_GFX_GCEA_MAM_AMEM1, "GCEA_MAM_AMEM1"},
3970 	{AMDGPU_GFX_GCEA_MAM_AMEM2, "GCEA_MAM_AMEM2"},
3971 	{AMDGPU_GFX_GCEA_MAM_AMEM3, "GCEA_MAM_AMEM3"},
3972 	{AMDGPU_GFX_GCEA_MAM_AFLUSH_BUFFER, "GCEA_MAM_AFLUSH_BUFFER"},
3973 	{AMDGPU_GFX_GCEA_WRET_TAGMEM, "GCEA_WRET_TAGMEM"},
3974 	{AMDGPU_GFX_GCEA_RRET_TAGMEM, "GCEA_RRET_TAGMEM"},
3975 	{AMDGPU_GFX_GCEA_IOWR_DATAMEM, "GCEA_IOWR_DATAMEM"},
3976 	{AMDGPU_GFX_GCEA_GMIWR_DATAMEM, "GCEA_GMIWR_DATAMEM"},
3977 	{AMDGPU_GFX_GCEA_DRAM_DATAMEM, "GCEA_DRAM_DATAMEM"},
3978 };
3979 
3980 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_gc_cane_mem_list[] = {
3981 	{AMDGPU_GFX_GC_CANE_MEM0, "GC_CANE_MEM0"},
3982 };
3983 
3984 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_gcutcl2_mem_list[] = {
3985 	{AMDGPU_GFX_GCUTCL2_MEM2P512X95, "GCUTCL2_MEM2P512X95"},
3986 };
3987 
3988 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_gds_mem_list[] = {
3989 	{AMDGPU_GFX_GDS_MEM0, "GDS_MEM"},
3990 };
3991 
3992 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_lds_mem_list[] = {
3993 	{AMDGPU_GFX_LDS_BANK0, "LDS_BANK0"},
3994 	{AMDGPU_GFX_LDS_BANK1, "LDS_BANK1"},
3995 	{AMDGPU_GFX_LDS_BANK2, "LDS_BANK2"},
3996 	{AMDGPU_GFX_LDS_BANK3, "LDS_BANK3"},
3997 	{AMDGPU_GFX_LDS_BANK4, "LDS_BANK4"},
3998 	{AMDGPU_GFX_LDS_BANK5, "LDS_BANK5"},
3999 	{AMDGPU_GFX_LDS_BANK6, "LDS_BANK6"},
4000 	{AMDGPU_GFX_LDS_BANK7, "LDS_BANK7"},
4001 	{AMDGPU_GFX_LDS_BANK8, "LDS_BANK8"},
4002 	{AMDGPU_GFX_LDS_BANK9, "LDS_BANK9"},
4003 	{AMDGPU_GFX_LDS_BANK10, "LDS_BANK10"},
4004 	{AMDGPU_GFX_LDS_BANK11, "LDS_BANK11"},
4005 	{AMDGPU_GFX_LDS_BANK12, "LDS_BANK12"},
4006 	{AMDGPU_GFX_LDS_BANK13, "LDS_BANK13"},
4007 	{AMDGPU_GFX_LDS_BANK14, "LDS_BANK14"},
4008 	{AMDGPU_GFX_LDS_BANK15, "LDS_BANK15"},
4009 	{AMDGPU_GFX_LDS_BANK16, "LDS_BANK16"},
4010 	{AMDGPU_GFX_LDS_BANK17, "LDS_BANK17"},
4011 	{AMDGPU_GFX_LDS_BANK18, "LDS_BANK18"},
4012 	{AMDGPU_GFX_LDS_BANK19, "LDS_BANK19"},
4013 	{AMDGPU_GFX_LDS_BANK20, "LDS_BANK20"},
4014 	{AMDGPU_GFX_LDS_BANK21, "LDS_BANK21"},
4015 	{AMDGPU_GFX_LDS_BANK22, "LDS_BANK22"},
4016 	{AMDGPU_GFX_LDS_BANK23, "LDS_BANK23"},
4017 	{AMDGPU_GFX_LDS_BANK24, "LDS_BANK24"},
4018 	{AMDGPU_GFX_LDS_BANK25, "LDS_BANK25"},
4019 	{AMDGPU_GFX_LDS_BANK26, "LDS_BANK26"},
4020 	{AMDGPU_GFX_LDS_BANK27, "LDS_BANK27"},
4021 	{AMDGPU_GFX_LDS_BANK28, "LDS_BANK28"},
4022 	{AMDGPU_GFX_LDS_BANK29, "LDS_BANK29"},
4023 	{AMDGPU_GFX_LDS_BANK30, "LDS_BANK30"},
4024 	{AMDGPU_GFX_LDS_BANK31, "LDS_BANK31"},
4025 	{AMDGPU_GFX_LDS_SP_BUFFER_A, "LDS_SP_BUFFER_A"},
4026 	{AMDGPU_GFX_LDS_SP_BUFFER_B, "LDS_SP_BUFFER_B"},
4027 };
4028 
4029 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_rlc_mem_list[] = {
4030 	{AMDGPU_GFX_RLC_GPMF32, "RLC_GPMF32"},
4031 	{AMDGPU_GFX_RLC_RLCVF32, "RLC_RLCVF32"},
4032 	{AMDGPU_GFX_RLC_SCRATCH, "RLC_SCRATCH"},
4033 	{AMDGPU_GFX_RLC_SRM_ARAM, "RLC_SRM_ARAM"},
4034 	{AMDGPU_GFX_RLC_SRM_DRAM, "RLC_SRM_DRAM"},
4035 	{AMDGPU_GFX_RLC_TCTAG, "RLC_TCTAG"},
4036 	{AMDGPU_GFX_RLC_SPM_SE, "RLC_SPM_SE"},
4037 	{AMDGPU_GFX_RLC_SPM_GRBMT, "RLC_SPM_GRBMT"},
4038 };
4039 
4040 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_sp_mem_list[] = {
4041 	{AMDGPU_GFX_SP_SIMDID0, "SP_SIMDID0"},
4042 };
4043 
4044 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_spi_mem_list[] = {
4045 	{AMDGPU_GFX_SPI_MEM0, "SPI_MEM0"},
4046 	{AMDGPU_GFX_SPI_MEM1, "SPI_MEM1"},
4047 	{AMDGPU_GFX_SPI_MEM2, "SPI_MEM2"},
4048 	{AMDGPU_GFX_SPI_MEM3, "SPI_MEM3"},
4049 };
4050 
4051 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_sqc_mem_list[] = {
4052 	{AMDGPU_GFX_SQC_INST_CACHE_A, "SQC_INST_CACHE_A"},
4053 	{AMDGPU_GFX_SQC_INST_CACHE_B, "SQC_INST_CACHE_B"},
4054 	{AMDGPU_GFX_SQC_INST_CACHE_TAG_A, "SQC_INST_CACHE_TAG_A"},
4055 	{AMDGPU_GFX_SQC_INST_CACHE_TAG_B, "SQC_INST_CACHE_TAG_B"},
4056 	{AMDGPU_GFX_SQC_INST_CACHE_MISS_FIFO_A, "SQC_INST_CACHE_MISS_FIFO_A"},
4057 	{AMDGPU_GFX_SQC_INST_CACHE_MISS_FIFO_B, "SQC_INST_CACHE_MISS_FIFO_B"},
4058 	{AMDGPU_GFX_SQC_INST_CACHE_GATCL1_MISS_FIFO_A, "SQC_INST_CACHE_GATCL1_MISS_FIFO_A"},
4059 	{AMDGPU_GFX_SQC_INST_CACHE_GATCL1_MISS_FIFO_B, "SQC_INST_CACHE_GATCL1_MISS_FIFO_B"},
4060 	{AMDGPU_GFX_SQC_DATA_CACHE_A, "SQC_DATA_CACHE_A"},
4061 	{AMDGPU_GFX_SQC_DATA_CACHE_B, "SQC_DATA_CACHE_B"},
4062 	{AMDGPU_GFX_SQC_DATA_CACHE_TAG_A, "SQC_DATA_CACHE_TAG_A"},
4063 	{AMDGPU_GFX_SQC_DATA_CACHE_TAG_B, "SQC_DATA_CACHE_TAG_B"},
4064 	{AMDGPU_GFX_SQC_DATA_CACHE_MISS_FIFO_A, "SQC_DATA_CACHE_MISS_FIFO_A"},
4065 	{AMDGPU_GFX_SQC_DATA_CACHE_MISS_FIFO_B, "SQC_DATA_CACHE_MISS_FIFO_B"},
4066 	{AMDGPU_GFX_SQC_DATA_CACHE_HIT_FIFO_A, "SQC_DATA_CACHE_HIT_FIFO_A"},
4067 	{AMDGPU_GFX_SQC_DATA_CACHE_HIT_FIFO_B, "SQC_DATA_CACHE_HIT_FIFO_B"},
4068 	{AMDGPU_GFX_SQC_DIRTY_BIT_A, "SQC_DIRTY_BIT_A"},
4069 	{AMDGPU_GFX_SQC_DIRTY_BIT_B, "SQC_DIRTY_BIT_B"},
4070 	{AMDGPU_GFX_SQC_WRITE_DATA_BUFFER_CU0, "SQC_WRITE_DATA_BUFFER_CU0"},
4071 	{AMDGPU_GFX_SQC_WRITE_DATA_BUFFER_CU1, "SQC_WRITE_DATA_BUFFER_CU1"},
4072 	{AMDGPU_GFX_SQC_UTCL1_MISS_LFIFO_DATA_CACHE_A, "SQC_UTCL1_MISS_LFIFO_DATA_CACHE_A"},
4073 	{AMDGPU_GFX_SQC_UTCL1_MISS_LFIFO_DATA_CACHE_B, "SQC_UTCL1_MISS_LFIFO_DATA_CACHE_B"},
4074 	{AMDGPU_GFX_SQC_UTCL1_MISS_LFIFO_INST_CACHE, "SQC_UTCL1_MISS_LFIFO_INST_CACHE"},
4075 };
4076 
4077 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_sq_mem_list[] = {
4078 	{AMDGPU_GFX_SQ_SGPR_MEM0, "SQ_SGPR_MEM0"},
4079 	{AMDGPU_GFX_SQ_SGPR_MEM1, "SQ_SGPR_MEM1"},
4080 	{AMDGPU_GFX_SQ_SGPR_MEM2, "SQ_SGPR_MEM2"},
4081 	{AMDGPU_GFX_SQ_SGPR_MEM3, "SQ_SGPR_MEM3"},
4082 };
4083 
4084 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_ta_mem_list[] = {
4085 	{AMDGPU_GFX_TA_FS_AFIFO_RAM_LO, "TA_FS_AFIFO_RAM_LO"},
4086 	{AMDGPU_GFX_TA_FS_AFIFO_RAM_HI, "TA_FS_AFIFO_RAM_HI"},
4087 	{AMDGPU_GFX_TA_FS_CFIFO_RAM, "TA_FS_CFIFO_RAM"},
4088 	{AMDGPU_GFX_TA_FSX_LFIFO, "TA_FSX_LFIFO"},
4089 	{AMDGPU_GFX_TA_FS_DFIFO_RAM, "TA_FS_DFIFO_RAM"},
4090 };
4091 
4092 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_tcc_mem_list[] = {
4093 	{AMDGPU_GFX_TCC_MEM1, "TCC_MEM1"},
4094 };
4095 
4096 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_tca_mem_list[] = {
4097 	{AMDGPU_GFX_TCA_MEM1, "TCA_MEM1"},
4098 };
4099 
4100 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_tci_mem_list[] = {
4101 	{AMDGPU_GFX_TCIW_MEM, "TCIW_MEM"},
4102 };
4103 
4104 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_tcp_mem_list[] = {
4105 	{AMDGPU_GFX_TCP_LFIFO0, "TCP_LFIFO0"},
4106 	{AMDGPU_GFX_TCP_SET0BANK0_RAM, "TCP_SET0BANK0_RAM"},
4107 	{AMDGPU_GFX_TCP_SET0BANK1_RAM, "TCP_SET0BANK1_RAM"},
4108 	{AMDGPU_GFX_TCP_SET0BANK2_RAM, "TCP_SET0BANK2_RAM"},
4109 	{AMDGPU_GFX_TCP_SET0BANK3_RAM, "TCP_SET0BANK3_RAM"},
4110 	{AMDGPU_GFX_TCP_SET1BANK0_RAM, "TCP_SET1BANK0_RAM"},
4111 	{AMDGPU_GFX_TCP_SET1BANK1_RAM, "TCP_SET1BANK1_RAM"},
4112 	{AMDGPU_GFX_TCP_SET1BANK2_RAM, "TCP_SET1BANK2_RAM"},
4113 	{AMDGPU_GFX_TCP_SET1BANK3_RAM, "TCP_SET1BANK3_RAM"},
4114 	{AMDGPU_GFX_TCP_SET2BANK0_RAM, "TCP_SET2BANK0_RAM"},
4115 	{AMDGPU_GFX_TCP_SET2BANK1_RAM, "TCP_SET2BANK1_RAM"},
4116 	{AMDGPU_GFX_TCP_SET2BANK2_RAM, "TCP_SET2BANK2_RAM"},
4117 	{AMDGPU_GFX_TCP_SET2BANK3_RAM, "TCP_SET2BANK3_RAM"},
4118 	{AMDGPU_GFX_TCP_SET3BANK0_RAM, "TCP_SET3BANK0_RAM"},
4119 	{AMDGPU_GFX_TCP_SET3BANK1_RAM, "TCP_SET3BANK1_RAM"},
4120 	{AMDGPU_GFX_TCP_SET3BANK2_RAM, "TCP_SET3BANK2_RAM"},
4121 	{AMDGPU_GFX_TCP_SET3BANK3_RAM, "TCP_SET3BANK3_RAM"},
4122 	{AMDGPU_GFX_TCP_VM_FIFO, "TCP_VM_FIFO"},
4123 	{AMDGPU_GFX_TCP_DB_TAGRAM0, "TCP_DB_TAGRAM0"},
4124 	{AMDGPU_GFX_TCP_DB_TAGRAM1, "TCP_DB_TAGRAM1"},
4125 	{AMDGPU_GFX_TCP_DB_TAGRAM2, "TCP_DB_TAGRAM2"},
4126 	{AMDGPU_GFX_TCP_DB_TAGRAM3, "TCP_DB_TAGRAM3"},
4127 	{AMDGPU_GFX_TCP_UTCL1_LFIFO_PROBE0, "TCP_UTCL1_LFIFO_PROBE0"},
4128 	{AMDGPU_GFX_TCP_UTCL1_LFIFO_PROBE1, "TCP_UTCL1_LFIFO_PROBE1"},
4129 	{AMDGPU_GFX_TCP_CMD_FIFO, "TCP_CMD_FIFO"},
4130 };
4131 
4132 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_td_mem_list[] = {
4133 	{AMDGPU_GFX_TD_UTD_CS_FIFO_MEM, "TD_UTD_CS_FIFO_MEM"},
4134 	{AMDGPU_GFX_TD_UTD_SS_FIFO_LO_MEM, "TD_UTD_SS_FIFO_LO_MEM"},
4135 	{AMDGPU_GFX_TD_UTD_SS_FIFO_HI_MEM, "TD_UTD_SS_FIFO_HI_MEM"},
4136 };
4137 
4138 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_tcx_mem_list[] = {
4139 	{AMDGPU_GFX_TCX_FIFOD0, "TCX_FIFOD0"},
4140 	{AMDGPU_GFX_TCX_FIFOD1, "TCX_FIFOD1"},
4141 	{AMDGPU_GFX_TCX_FIFOD2, "TCX_FIFOD2"},
4142 	{AMDGPU_GFX_TCX_FIFOD3, "TCX_FIFOD3"},
4143 	{AMDGPU_GFX_TCX_FIFOD4, "TCX_FIFOD4"},
4144 	{AMDGPU_GFX_TCX_FIFOD5, "TCX_FIFOD5"},
4145 	{AMDGPU_GFX_TCX_FIFOD6, "TCX_FIFOD6"},
4146 	{AMDGPU_GFX_TCX_FIFOD7, "TCX_FIFOD7"},
4147 	{AMDGPU_GFX_TCX_FIFOB0, "TCX_FIFOB0"},
4148 	{AMDGPU_GFX_TCX_FIFOB1, "TCX_FIFOB1"},
4149 	{AMDGPU_GFX_TCX_FIFOB2, "TCX_FIFOB2"},
4150 	{AMDGPU_GFX_TCX_FIFOB3, "TCX_FIFOB3"},
4151 	{AMDGPU_GFX_TCX_FIFOB4, "TCX_FIFOB4"},
4152 	{AMDGPU_GFX_TCX_FIFOB5, "TCX_FIFOB5"},
4153 	{AMDGPU_GFX_TCX_FIFOB6, "TCX_FIFOB6"},
4154 	{AMDGPU_GFX_TCX_FIFOB7, "TCX_FIFOB7"},
4155 	{AMDGPU_GFX_TCX_FIFOA0, "TCX_FIFOA0"},
4156 	{AMDGPU_GFX_TCX_FIFOA1, "TCX_FIFOA1"},
4157 	{AMDGPU_GFX_TCX_FIFOA2, "TCX_FIFOA2"},
4158 	{AMDGPU_GFX_TCX_FIFOA3, "TCX_FIFOA3"},
4159 	{AMDGPU_GFX_TCX_FIFOA4, "TCX_FIFOA4"},
4160 	{AMDGPU_GFX_TCX_FIFOA5, "TCX_FIFOA5"},
4161 	{AMDGPU_GFX_TCX_FIFOA6, "TCX_FIFOA6"},
4162 	{AMDGPU_GFX_TCX_FIFOA7, "TCX_FIFOA7"},
4163 	{AMDGPU_GFX_TCX_CFIFO0, "TCX_CFIFO0"},
4164 	{AMDGPU_GFX_TCX_CFIFO1, "TCX_CFIFO1"},
4165 	{AMDGPU_GFX_TCX_CFIFO2, "TCX_CFIFO2"},
4166 	{AMDGPU_GFX_TCX_CFIFO3, "TCX_CFIFO3"},
4167 	{AMDGPU_GFX_TCX_CFIFO4, "TCX_CFIFO4"},
4168 	{AMDGPU_GFX_TCX_CFIFO5, "TCX_CFIFO5"},
4169 	{AMDGPU_GFX_TCX_CFIFO6, "TCX_CFIFO6"},
4170 	{AMDGPU_GFX_TCX_CFIFO7, "TCX_CFIFO7"},
4171 	{AMDGPU_GFX_TCX_FIFO_ACKB0, "TCX_FIFO_ACKB0"},
4172 	{AMDGPU_GFX_TCX_FIFO_ACKB1, "TCX_FIFO_ACKB1"},
4173 	{AMDGPU_GFX_TCX_FIFO_ACKB2, "TCX_FIFO_ACKB2"},
4174 	{AMDGPU_GFX_TCX_FIFO_ACKB3, "TCX_FIFO_ACKB3"},
4175 	{AMDGPU_GFX_TCX_FIFO_ACKB4, "TCX_FIFO_ACKB4"},
4176 	{AMDGPU_GFX_TCX_FIFO_ACKB5, "TCX_FIFO_ACKB5"},
4177 	{AMDGPU_GFX_TCX_FIFO_ACKB6, "TCX_FIFO_ACKB6"},
4178 	{AMDGPU_GFX_TCX_FIFO_ACKB7, "TCX_FIFO_ACKB7"},
4179 	{AMDGPU_GFX_TCX_FIFO_ACKD0, "TCX_FIFO_ACKD0"},
4180 	{AMDGPU_GFX_TCX_FIFO_ACKD1, "TCX_FIFO_ACKD1"},
4181 	{AMDGPU_GFX_TCX_FIFO_ACKD2, "TCX_FIFO_ACKD2"},
4182 	{AMDGPU_GFX_TCX_FIFO_ACKD3, "TCX_FIFO_ACKD3"},
4183 	{AMDGPU_GFX_TCX_FIFO_ACKD4, "TCX_FIFO_ACKD4"},
4184 	{AMDGPU_GFX_TCX_FIFO_ACKD5, "TCX_FIFO_ACKD5"},
4185 	{AMDGPU_GFX_TCX_FIFO_ACKD6, "TCX_FIFO_ACKD6"},
4186 	{AMDGPU_GFX_TCX_FIFO_ACKD7, "TCX_FIFO_ACKD7"},
4187 	{AMDGPU_GFX_TCX_DST_FIFOA0, "TCX_DST_FIFOA0"},
4188 	{AMDGPU_GFX_TCX_DST_FIFOA1, "TCX_DST_FIFOA1"},
4189 	{AMDGPU_GFX_TCX_DST_FIFOA2, "TCX_DST_FIFOA2"},
4190 	{AMDGPU_GFX_TCX_DST_FIFOA3, "TCX_DST_FIFOA3"},
4191 	{AMDGPU_GFX_TCX_DST_FIFOA4, "TCX_DST_FIFOA4"},
4192 	{AMDGPU_GFX_TCX_DST_FIFOA5, "TCX_DST_FIFOA5"},
4193 	{AMDGPU_GFX_TCX_DST_FIFOA6, "TCX_DST_FIFOA6"},
4194 	{AMDGPU_GFX_TCX_DST_FIFOA7, "TCX_DST_FIFOA7"},
4195 	{AMDGPU_GFX_TCX_DST_FIFOB0, "TCX_DST_FIFOB0"},
4196 	{AMDGPU_GFX_TCX_DST_FIFOB1, "TCX_DST_FIFOB1"},
4197 	{AMDGPU_GFX_TCX_DST_FIFOB2, "TCX_DST_FIFOB2"},
4198 	{AMDGPU_GFX_TCX_DST_FIFOB3, "TCX_DST_FIFOB3"},
4199 	{AMDGPU_GFX_TCX_DST_FIFOB4, "TCX_DST_FIFOB4"},
4200 	{AMDGPU_GFX_TCX_DST_FIFOB5, "TCX_DST_FIFOB5"},
4201 	{AMDGPU_GFX_TCX_DST_FIFOB6, "TCX_DST_FIFOB6"},
4202 	{AMDGPU_GFX_TCX_DST_FIFOB7, "TCX_DST_FIFOB7"},
4203 	{AMDGPU_GFX_TCX_DST_FIFOD0, "TCX_DST_FIFOD0"},
4204 	{AMDGPU_GFX_TCX_DST_FIFOD1, "TCX_DST_FIFOD1"},
4205 	{AMDGPU_GFX_TCX_DST_FIFOD2, "TCX_DST_FIFOD2"},
4206 	{AMDGPU_GFX_TCX_DST_FIFOD3, "TCX_DST_FIFOD3"},
4207 	{AMDGPU_GFX_TCX_DST_FIFOD4, "TCX_DST_FIFOD4"},
4208 	{AMDGPU_GFX_TCX_DST_FIFOD5, "TCX_DST_FIFOD5"},
4209 	{AMDGPU_GFX_TCX_DST_FIFOD6, "TCX_DST_FIFOD6"},
4210 	{AMDGPU_GFX_TCX_DST_FIFOD7, "TCX_DST_FIFOD7"},
4211 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB0, "TCX_DST_FIFO_ACKB0"},
4212 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB1, "TCX_DST_FIFO_ACKB1"},
4213 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB2, "TCX_DST_FIFO_ACKB2"},
4214 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB3, "TCX_DST_FIFO_ACKB3"},
4215 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB4, "TCX_DST_FIFO_ACKB4"},
4216 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB5, "TCX_DST_FIFO_ACKB5"},
4217 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB6, "TCX_DST_FIFO_ACKB6"},
4218 	{AMDGPU_GFX_TCX_DST_FIFO_ACKB7, "TCX_DST_FIFO_ACKB7"},
4219 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD0, "TCX_DST_FIFO_ACKD0"},
4220 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD1, "TCX_DST_FIFO_ACKD1"},
4221 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD2, "TCX_DST_FIFO_ACKD2"},
4222 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD3, "TCX_DST_FIFO_ACKD3"},
4223 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD4, "TCX_DST_FIFO_ACKD4"},
4224 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD5, "TCX_DST_FIFO_ACKD5"},
4225 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD6, "TCX_DST_FIFO_ACKD6"},
4226 	{AMDGPU_GFX_TCX_DST_FIFO_ACKD7, "TCX_DST_FIFO_ACKD7"},
4227 };
4228 
4229 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_atc_l2_mem_list[] = {
4230 	{AMDGPU_GFX_ATC_L2_MEM, "ATC_L2_MEM"},
4231 };
4232 
4233 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_utcl2_mem_list[] = {
4234 	{AMDGPU_GFX_UTCL2_MEM, "UTCL2_MEM"},
4235 };
4236 
4237 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_vml2_mem_list[] = {
4238 	{AMDGPU_GFX_VML2_MEM, "VML2_MEM"},
4239 };
4240 
4241 static const struct amdgpu_ras_memory_id_entry gfx_v9_4_3_ras_vml2_walker_mem_list[] = {
4242 	{AMDGPU_GFX_VML2_WALKER_MEM, "VML2_WALKER_MEM"},
4243 };
4244 
4245 static const struct amdgpu_gfx_ras_mem_id_entry gfx_v9_4_3_ras_mem_list_array[AMDGPU_GFX_MEM_TYPE_NUM] = {
4246 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_cp_mem_list)
4247 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_gcea_mem_list)
4248 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_gc_cane_mem_list)
4249 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_gcutcl2_mem_list)
4250 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_gds_mem_list)
4251 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_lds_mem_list)
4252 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_rlc_mem_list)
4253 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_sp_mem_list)
4254 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_spi_mem_list)
4255 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_sqc_mem_list)
4256 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_sq_mem_list)
4257 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_ta_mem_list)
4258 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_tcc_mem_list)
4259 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_tca_mem_list)
4260 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_tci_mem_list)
4261 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_tcp_mem_list)
4262 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_td_mem_list)
4263 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_tcx_mem_list)
4264 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_atc_l2_mem_list)
4265 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_utcl2_mem_list)
4266 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_vml2_mem_list)
4267 	AMDGPU_GFX_MEMID_ENT(gfx_v9_4_3_ras_vml2_walker_mem_list)
4268 };
4269 
4270 static const struct amdgpu_gfx_ras_reg_entry gfx_v9_4_3_ce_reg_list[] = {
4271 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regRLC_CE_ERR_STATUS_LOW, regRLC_CE_ERR_STATUS_HIGH),
4272 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "RLC"},
4273 	    AMDGPU_GFX_RLC_MEM, 1},
4274 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regCPC_CE_ERR_STATUS_LO, regCPC_CE_ERR_STATUS_HI),
4275 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CPC"},
4276 	    AMDGPU_GFX_CP_MEM, 1},
4277 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regCPF_CE_ERR_STATUS_LO, regCPF_CE_ERR_STATUS_HI),
4278 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CPF"},
4279 	    AMDGPU_GFX_CP_MEM, 1},
4280 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regCPG_CE_ERR_STATUS_LO, regCPG_CE_ERR_STATUS_HI),
4281 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CPG"},
4282 	    AMDGPU_GFX_CP_MEM, 1},
4283 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regGDS_CE_ERR_STATUS_LO, regGDS_CE_ERR_STATUS_HI),
4284 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "GDS"},
4285 	    AMDGPU_GFX_GDS_MEM, 1},
4286 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regGC_CANE_CE_ERR_STATUS_LO, regGC_CANE_CE_ERR_STATUS_HI),
4287 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CANE"},
4288 	    AMDGPU_GFX_GC_CANE_MEM, 1},
4289 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSPI_CE_ERR_STATUS_LO, regSPI_CE_ERR_STATUS_HI),
4290 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SPI"},
4291 	    AMDGPU_GFX_SPI_MEM, 1},
4292 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSP0_CE_ERR_STATUS_LO, regSP0_CE_ERR_STATUS_HI),
4293 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SP0"},
4294 	    AMDGPU_GFX_SP_MEM, 4},
4295 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSP1_CE_ERR_STATUS_LO, regSP1_CE_ERR_STATUS_HI),
4296 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SP1"},
4297 	    AMDGPU_GFX_SP_MEM, 4},
4298 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSQ_CE_ERR_STATUS_LO, regSQ_CE_ERR_STATUS_HI),
4299 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SQ"},
4300 	    AMDGPU_GFX_SQ_MEM, 4},
4301 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSQC_CE_EDC_LO, regSQC_CE_EDC_HI),
4302 	    5, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SQC"},
4303 	    AMDGPU_GFX_SQC_MEM, 4},
4304 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCX_CE_ERR_STATUS_LO, regTCX_CE_ERR_STATUS_HI),
4305 	    2, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCX"},
4306 	    AMDGPU_GFX_TCX_MEM, 1},
4307 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCC_CE_ERR_STATUS_LO, regTCC_CE_ERR_STATUS_HI),
4308 	    16, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCC"},
4309 	    AMDGPU_GFX_TCC_MEM, 1},
4310 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTA_CE_EDC_LO, regTA_CE_EDC_HI),
4311 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TA"},
4312 	    AMDGPU_GFX_TA_MEM, 4},
4313 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCI_CE_EDC_LO_REG, regTCI_CE_EDC_HI_REG),
4314 	    27, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCI"},
4315 	    AMDGPU_GFX_TCI_MEM, 1},
4316 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCP_CE_EDC_LO_REG, regTCP_CE_EDC_HI_REG),
4317 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCP"},
4318 	    AMDGPU_GFX_TCP_MEM, 4},
4319 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTD_CE_EDC_LO, regTD_CE_EDC_HI),
4320 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TD"},
4321 	    AMDGPU_GFX_TD_MEM, 4},
4322 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regGCEA_CE_ERR_STATUS_LO, regGCEA_CE_ERR_STATUS_HI),
4323 	    16, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "GCEA"},
4324 	    AMDGPU_GFX_GCEA_MEM, 1},
4325 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regLDS_CE_ERR_STATUS_LO, regLDS_CE_ERR_STATUS_HI),
4326 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "LDS"},
4327 	    AMDGPU_GFX_LDS_MEM, 4},
4328 };
4329 
4330 static const struct amdgpu_gfx_ras_reg_entry gfx_v9_4_3_ue_reg_list[] = {
4331 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regRLC_UE_ERR_STATUS_LOW, regRLC_UE_ERR_STATUS_HIGH),
4332 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "RLC"},
4333 	    AMDGPU_GFX_RLC_MEM, 1},
4334 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regCPC_UE_ERR_STATUS_LO, regCPC_UE_ERR_STATUS_HI),
4335 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CPC"},
4336 	    AMDGPU_GFX_CP_MEM, 1},
4337 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regCPF_UE_ERR_STATUS_LO, regCPF_UE_ERR_STATUS_HI),
4338 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CPF"},
4339 	    AMDGPU_GFX_CP_MEM, 1},
4340 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regCPG_UE_ERR_STATUS_LO, regCPG_UE_ERR_STATUS_HI),
4341 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CPG"},
4342 	    AMDGPU_GFX_CP_MEM, 1},
4343 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regGDS_UE_ERR_STATUS_LO, regGDS_UE_ERR_STATUS_HI),
4344 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "GDS"},
4345 	    AMDGPU_GFX_GDS_MEM, 1},
4346 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regGC_CANE_UE_ERR_STATUS_LO, regGC_CANE_UE_ERR_STATUS_HI),
4347 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "CANE"},
4348 	    AMDGPU_GFX_GC_CANE_MEM, 1},
4349 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSPI_UE_ERR_STATUS_LO, regSPI_UE_ERR_STATUS_HI),
4350 	    1, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SPI"},
4351 	    AMDGPU_GFX_SPI_MEM, 1},
4352 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSP0_UE_ERR_STATUS_LO, regSP0_UE_ERR_STATUS_HI),
4353 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SP0"},
4354 	    AMDGPU_GFX_SP_MEM, 4},
4355 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSP1_UE_ERR_STATUS_LO, regSP1_UE_ERR_STATUS_HI),
4356 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SP1"},
4357 	    AMDGPU_GFX_SP_MEM, 4},
4358 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSQ_UE_ERR_STATUS_LO, regSQ_UE_ERR_STATUS_HI),
4359 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SQ"},
4360 	    AMDGPU_GFX_SQ_MEM, 4},
4361 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regSQC_UE_EDC_LO, regSQC_UE_EDC_HI),
4362 	    5, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "SQC"},
4363 	    AMDGPU_GFX_SQC_MEM, 4},
4364 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCX_UE_ERR_STATUS_LO, regTCX_UE_ERR_STATUS_HI),
4365 	    2, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCX"},
4366 	    AMDGPU_GFX_TCX_MEM, 1},
4367 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCC_UE_ERR_STATUS_LO, regTCC_UE_ERR_STATUS_HI),
4368 	    16, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCC"},
4369 	    AMDGPU_GFX_TCC_MEM, 1},
4370 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTA_UE_EDC_LO, regTA_UE_EDC_HI),
4371 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TA"},
4372 	    AMDGPU_GFX_TA_MEM, 4},
4373 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCI_UE_EDC_LO_REG, regTCI_UE_EDC_HI_REG),
4374 	    27, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCI"},
4375 	    AMDGPU_GFX_TCI_MEM, 1},
4376 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCP_UE_EDC_LO_REG, regTCP_UE_EDC_HI_REG),
4377 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCP"},
4378 	    AMDGPU_GFX_TCP_MEM, 4},
4379 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTD_UE_EDC_LO, regTD_UE_EDC_HI),
4380 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TD"},
4381 	    AMDGPU_GFX_TD_MEM, 4},
4382 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regTCA_UE_ERR_STATUS_LO, regTCA_UE_ERR_STATUS_HI),
4383 	    2, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "TCA"},
4384 	    AMDGPU_GFX_TCA_MEM, 1},
4385 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regGCEA_UE_ERR_STATUS_LO, regGCEA_UE_ERR_STATUS_HI),
4386 	    16, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "GCEA"},
4387 	    AMDGPU_GFX_GCEA_MEM, 1},
4388 	{{AMDGPU_RAS_REG_ENTRY(GC, 0, regLDS_UE_ERR_STATUS_LO, regLDS_UE_ERR_STATUS_HI),
4389 	    10, (AMDGPU_RAS_ERR_INFO_VALID | AMDGPU_RAS_ERR_STATUS_VALID), "LDS"},
4390 	    AMDGPU_GFX_LDS_MEM, 4},
4391 };
4392 
4393 static void gfx_v9_4_3_inst_query_ras_err_count(struct amdgpu_device *adev,
4394 					void *ras_error_status, int xcc_id)
4395 {
4396 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
4397 	unsigned long ce_count = 0, ue_count = 0;
4398 	uint32_t i, j, k;
4399 
4400 	/* NOTE: convert xcc_id to physical XCD ID (XCD0 or XCD1) */
4401 	struct amdgpu_smuio_mcm_config_info mcm_info = {
4402 		.socket_id = adev->smuio.funcs->get_socket_id(adev),
4403 		.die_id = xcc_id & 0x01 ? 1 : 0,
4404 	};
4405 
4406 	mutex_lock(&adev->grbm_idx_mutex);
4407 
4408 	for (i = 0; i < ARRAY_SIZE(gfx_v9_4_3_ce_reg_list); i++) {
4409 		for (j = 0; j < gfx_v9_4_3_ce_reg_list[i].se_num; j++) {
4410 			for (k = 0; k < gfx_v9_4_3_ce_reg_list[i].reg_entry.reg_inst; k++) {
4411 				/* no need to select if instance number is 1 */
4412 				if (gfx_v9_4_3_ce_reg_list[i].se_num > 1 ||
4413 				    gfx_v9_4_3_ce_reg_list[i].reg_entry.reg_inst > 1)
4414 					gfx_v9_4_3_xcc_select_se_sh(adev, j, 0, k, xcc_id);
4415 
4416 				amdgpu_ras_inst_query_ras_error_count(adev,
4417 					&(gfx_v9_4_3_ce_reg_list[i].reg_entry),
4418 					1,
4419 					gfx_v9_4_3_ras_mem_list_array[gfx_v9_4_3_ce_reg_list[i].mem_id_type].mem_id_ent,
4420 					gfx_v9_4_3_ras_mem_list_array[gfx_v9_4_3_ce_reg_list[i].mem_id_type].size,
4421 					GET_INST(GC, xcc_id),
4422 					AMDGPU_RAS_ERROR__SINGLE_CORRECTABLE,
4423 					&ce_count);
4424 
4425 				amdgpu_ras_inst_query_ras_error_count(adev,
4426 					&(gfx_v9_4_3_ue_reg_list[i].reg_entry),
4427 					1,
4428 					gfx_v9_4_3_ras_mem_list_array[gfx_v9_4_3_ue_reg_list[i].mem_id_type].mem_id_ent,
4429 					gfx_v9_4_3_ras_mem_list_array[gfx_v9_4_3_ue_reg_list[i].mem_id_type].size,
4430 					GET_INST(GC, xcc_id),
4431 					AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE,
4432 					&ue_count);
4433 			}
4434 		}
4435 	}
4436 
4437 	/* handle extra register entries of UE */
4438 	for (; i < ARRAY_SIZE(gfx_v9_4_3_ue_reg_list); i++) {
4439 		for (j = 0; j < gfx_v9_4_3_ue_reg_list[i].se_num; j++) {
4440 			for (k = 0; k < gfx_v9_4_3_ue_reg_list[i].reg_entry.reg_inst; k++) {
4441 				/* no need to select if instance number is 1 */
4442 				if (gfx_v9_4_3_ue_reg_list[i].se_num > 1 ||
4443 					gfx_v9_4_3_ue_reg_list[i].reg_entry.reg_inst > 1)
4444 					gfx_v9_4_3_xcc_select_se_sh(adev, j, 0, k, xcc_id);
4445 
4446 				amdgpu_ras_inst_query_ras_error_count(adev,
4447 					&(gfx_v9_4_3_ue_reg_list[i].reg_entry),
4448 					1,
4449 					gfx_v9_4_3_ras_mem_list_array[gfx_v9_4_3_ue_reg_list[i].mem_id_type].mem_id_ent,
4450 					gfx_v9_4_3_ras_mem_list_array[gfx_v9_4_3_ue_reg_list[i].mem_id_type].size,
4451 					GET_INST(GC, xcc_id),
4452 					AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE,
4453 					&ue_count);
4454 			}
4455 		}
4456 	}
4457 
4458 	gfx_v9_4_3_xcc_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff,
4459 			xcc_id);
4460 	mutex_unlock(&adev->grbm_idx_mutex);
4461 
4462 	/* the caller should make sure initialize value of
4463 	 * err_data->ue_count and err_data->ce_count
4464 	 */
4465 	amdgpu_ras_error_statistic_ue_count(err_data, &mcm_info, ue_count);
4466 	amdgpu_ras_error_statistic_ce_count(err_data, &mcm_info, ce_count);
4467 }
4468 
4469 static void gfx_v9_4_3_inst_reset_ras_err_count(struct amdgpu_device *adev,
4470 					void *ras_error_status, int xcc_id)
4471 {
4472 	uint32_t i, j, k;
4473 
4474 	mutex_lock(&adev->grbm_idx_mutex);
4475 
4476 	for (i = 0; i < ARRAY_SIZE(gfx_v9_4_3_ce_reg_list); i++) {
4477 		for (j = 0; j < gfx_v9_4_3_ce_reg_list[i].se_num; j++) {
4478 			for (k = 0; k < gfx_v9_4_3_ce_reg_list[i].reg_entry.reg_inst; k++) {
4479 				/* no need to select if instance number is 1 */
4480 				if (gfx_v9_4_3_ce_reg_list[i].se_num > 1 ||
4481 				    gfx_v9_4_3_ce_reg_list[i].reg_entry.reg_inst > 1)
4482 					gfx_v9_4_3_xcc_select_se_sh(adev, j, 0, k, xcc_id);
4483 
4484 				amdgpu_ras_inst_reset_ras_error_count(adev,
4485 					&(gfx_v9_4_3_ce_reg_list[i].reg_entry),
4486 					1,
4487 					GET_INST(GC, xcc_id));
4488 
4489 				amdgpu_ras_inst_reset_ras_error_count(adev,
4490 					&(gfx_v9_4_3_ue_reg_list[i].reg_entry),
4491 					1,
4492 					GET_INST(GC, xcc_id));
4493 			}
4494 		}
4495 	}
4496 
4497 	/* handle extra register entries of UE */
4498 	for (; i < ARRAY_SIZE(gfx_v9_4_3_ue_reg_list); i++) {
4499 		for (j = 0; j < gfx_v9_4_3_ue_reg_list[i].se_num; j++) {
4500 			for (k = 0; k < gfx_v9_4_3_ue_reg_list[i].reg_entry.reg_inst; k++) {
4501 				/* no need to select if instance number is 1 */
4502 				if (gfx_v9_4_3_ue_reg_list[i].se_num > 1 ||
4503 					gfx_v9_4_3_ue_reg_list[i].reg_entry.reg_inst > 1)
4504 					gfx_v9_4_3_xcc_select_se_sh(adev, j, 0, k, xcc_id);
4505 
4506 				amdgpu_ras_inst_reset_ras_error_count(adev,
4507 					&(gfx_v9_4_3_ue_reg_list[i].reg_entry),
4508 					1,
4509 					GET_INST(GC, xcc_id));
4510 			}
4511 		}
4512 	}
4513 
4514 	gfx_v9_4_3_xcc_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff,
4515 			xcc_id);
4516 	mutex_unlock(&adev->grbm_idx_mutex);
4517 }
4518 
4519 static void gfx_v9_4_3_inst_enable_watchdog_timer(struct amdgpu_device *adev,
4520 					void *ras_error_status, int xcc_id)
4521 {
4522 	uint32_t i;
4523 	uint32_t data;
4524 
4525 	if (amdgpu_sriov_vf(adev))
4526 		return;
4527 
4528 	data = RREG32_SOC15(GC, GET_INST(GC, 0), regSQ_TIMEOUT_CONFIG);
4529 	data = REG_SET_FIELD(data, SQ_TIMEOUT_CONFIG, TIMEOUT_FATAL_DISABLE,
4530 			     amdgpu_watchdog_timer.timeout_fatal_disable ? 1 : 0);
4531 
4532 	if (amdgpu_watchdog_timer.timeout_fatal_disable &&
4533 	    (amdgpu_watchdog_timer.period < 1 ||
4534 	     amdgpu_watchdog_timer.period > 0x23)) {
4535 		dev_warn(adev->dev, "Watchdog period range is 1 to 0x23\n");
4536 		amdgpu_watchdog_timer.period = 0x23;
4537 	}
4538 	data = REG_SET_FIELD(data, SQ_TIMEOUT_CONFIG, PERIOD_SEL,
4539 			     amdgpu_watchdog_timer.period);
4540 
4541 	mutex_lock(&adev->grbm_idx_mutex);
4542 	for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
4543 		gfx_v9_4_3_xcc_select_se_sh(adev, i, 0xffffffff, 0xffffffff, xcc_id);
4544 		WREG32_SOC15(GC, GET_INST(GC, xcc_id), regSQ_TIMEOUT_CONFIG, data);
4545 	}
4546 	gfx_v9_4_3_xcc_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff,
4547 			xcc_id);
4548 	mutex_unlock(&adev->grbm_idx_mutex);
4549 }
4550 
4551 static void gfx_v9_4_3_query_ras_error_count(struct amdgpu_device *adev,
4552 					void *ras_error_status)
4553 {
4554 	amdgpu_gfx_ras_error_func(adev, ras_error_status,
4555 			gfx_v9_4_3_inst_query_ras_err_count);
4556 }
4557 
4558 static void gfx_v9_4_3_reset_ras_error_count(struct amdgpu_device *adev)
4559 {
4560 	amdgpu_gfx_ras_error_func(adev, NULL, gfx_v9_4_3_inst_reset_ras_err_count);
4561 }
4562 
4563 static void gfx_v9_4_3_enable_watchdog_timer(struct amdgpu_device *adev)
4564 {
4565 	amdgpu_gfx_ras_error_func(adev, NULL, gfx_v9_4_3_inst_enable_watchdog_timer);
4566 }
4567 
4568 static void gfx_v9_4_3_ring_insert_nop(struct amdgpu_ring *ring, uint32_t num_nop)
4569 {
4570 	/* Header itself is a NOP packet */
4571 	if (num_nop == 1) {
4572 		amdgpu_ring_write(ring, ring->funcs->nop);
4573 		return;
4574 	}
4575 
4576 	/* Max HW optimization till 0x3ffe, followed by remaining one NOP at a time*/
4577 	amdgpu_ring_write(ring, PACKET3(PACKET3_NOP, min(num_nop - 2, 0x3ffe)));
4578 
4579 	/* Header is at index 0, followed by num_nops - 1 NOP packet's */
4580 	amdgpu_ring_insert_nop(ring, num_nop - 1);
4581 }
4582 
4583 static void gfx_v9_4_3_ip_print(struct amdgpu_ip_block *ip_block, struct drm_printer *p)
4584 {
4585 	struct amdgpu_device *adev = ip_block->adev;
4586 	uint32_t i, j, k;
4587 	uint32_t xcc_id, xcc_offset, inst_offset;
4588 	uint32_t num_xcc, reg, num_inst;
4589 	uint32_t reg_count = ARRAY_SIZE(gc_reg_list_9_4_3);
4590 
4591 	if (!adev->gfx.ip_dump_core)
4592 		return;
4593 
4594 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
4595 	drm_printf(p, "Number of Instances:%d\n", num_xcc);
4596 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
4597 		xcc_offset = xcc_id * reg_count;
4598 		drm_printf(p, "\nInstance id:%d\n", xcc_id);
4599 		for (i = 0; i < reg_count; i++)
4600 			drm_printf(p, "%-50s \t 0x%08x\n",
4601 				   gc_reg_list_9_4_3[i].reg_name,
4602 				   adev->gfx.ip_dump_core[xcc_offset + i]);
4603 	}
4604 
4605 	/* print compute queue registers for all instances */
4606 	if (!adev->gfx.ip_dump_compute_queues)
4607 		return;
4608 
4609 	num_inst = adev->gfx.mec.num_mec * adev->gfx.mec.num_pipe_per_mec *
4610 		adev->gfx.mec.num_queue_per_pipe;
4611 
4612 	reg_count = ARRAY_SIZE(gc_cp_reg_list_9_4_3);
4613 	drm_printf(p, "\nnum_xcc: %d num_mec: %d num_pipe: %d num_queue: %d\n",
4614 		   num_xcc,
4615 		   adev->gfx.mec.num_mec,
4616 		   adev->gfx.mec.num_pipe_per_mec,
4617 		   adev->gfx.mec.num_queue_per_pipe);
4618 
4619 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
4620 		xcc_offset = xcc_id * reg_count * num_inst;
4621 		inst_offset = 0;
4622 		for (i = 0; i < adev->gfx.mec.num_mec; i++) {
4623 			for (j = 0; j < adev->gfx.mec.num_pipe_per_mec; j++) {
4624 				for (k = 0; k < adev->gfx.mec.num_queue_per_pipe; k++) {
4625 					drm_printf(p,
4626 						   "\nxcc:%d mec:%d, pipe:%d, queue:%d\n",
4627 						    xcc_id, i, j, k);
4628 					for (reg = 0; reg < reg_count; reg++) {
4629 						drm_printf(p,
4630 							   "%-50s \t 0x%08x\n",
4631 							   gc_cp_reg_list_9_4_3[reg].reg_name,
4632 							   adev->gfx.ip_dump_compute_queues
4633 								[xcc_offset + inst_offset +
4634 								reg]);
4635 					}
4636 					inst_offset += reg_count;
4637 				}
4638 			}
4639 		}
4640 	}
4641 }
4642 
4643 static void gfx_v9_4_3_ip_dump(struct amdgpu_ip_block *ip_block)
4644 {
4645 	struct amdgpu_device *adev = ip_block->adev;
4646 	uint32_t i, j, k;
4647 	uint32_t num_xcc, reg, num_inst;
4648 	uint32_t xcc_id, xcc_offset, inst_offset;
4649 	uint32_t reg_count = ARRAY_SIZE(gc_reg_list_9_4_3);
4650 
4651 	if (!adev->gfx.ip_dump_core)
4652 		return;
4653 
4654 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
4655 
4656 	amdgpu_gfx_off_ctrl(adev, false);
4657 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
4658 		xcc_offset = xcc_id * reg_count;
4659 		for (i = 0; i < reg_count; i++)
4660 			adev->gfx.ip_dump_core[xcc_offset + i] =
4661 				RREG32(SOC15_REG_ENTRY_OFFSET_INST(gc_reg_list_9_4_3[i],
4662 								   GET_INST(GC, xcc_id)));
4663 	}
4664 	amdgpu_gfx_off_ctrl(adev, true);
4665 
4666 	/* dump compute queue registers for all instances */
4667 	if (!adev->gfx.ip_dump_compute_queues)
4668 		return;
4669 
4670 	num_inst = adev->gfx.mec.num_mec * adev->gfx.mec.num_pipe_per_mec *
4671 		adev->gfx.mec.num_queue_per_pipe;
4672 	reg_count = ARRAY_SIZE(gc_cp_reg_list_9_4_3);
4673 	amdgpu_gfx_off_ctrl(adev, false);
4674 	mutex_lock(&adev->srbm_mutex);
4675 	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
4676 		xcc_offset = xcc_id * reg_count * num_inst;
4677 		inst_offset = 0;
4678 		for (i = 0; i < adev->gfx.mec.num_mec; i++) {
4679 			for (j = 0; j < adev->gfx.mec.num_pipe_per_mec; j++) {
4680 				for (k = 0; k < adev->gfx.mec.num_queue_per_pipe; k++) {
4681 					/* ME0 is for GFX so start from 1 for CP */
4682 					soc15_grbm_select(adev, 1 + i, j, k, 0,
4683 							  GET_INST(GC, xcc_id));
4684 
4685 					for (reg = 0; reg < reg_count; reg++) {
4686 						adev->gfx.ip_dump_compute_queues
4687 							[xcc_offset +
4688 							 inst_offset + reg] =
4689 							RREG32(SOC15_REG_ENTRY_OFFSET_INST(
4690 								gc_cp_reg_list_9_4_3[reg],
4691 								GET_INST(GC, xcc_id)));
4692 					}
4693 					inst_offset += reg_count;
4694 				}
4695 			}
4696 		}
4697 	}
4698 	soc15_grbm_select(adev, 0, 0, 0, 0, 0);
4699 	mutex_unlock(&adev->srbm_mutex);
4700 	amdgpu_gfx_off_ctrl(adev, true);
4701 }
4702 
4703 static void gfx_v9_4_3_ring_emit_cleaner_shader(struct amdgpu_ring *ring)
4704 {
4705 	/* Emit the cleaner shader */
4706 	amdgpu_ring_write(ring, PACKET3(PACKET3_RUN_CLEANER_SHADER, 0));
4707 	amdgpu_ring_write(ring, 0);  /* RESERVED field, programmed to zero */
4708 }
4709 
4710 static const struct amd_ip_funcs gfx_v9_4_3_ip_funcs = {
4711 	.name = "gfx_v9_4_3",
4712 	.early_init = gfx_v9_4_3_early_init,
4713 	.late_init = gfx_v9_4_3_late_init,
4714 	.sw_init = gfx_v9_4_3_sw_init,
4715 	.sw_fini = gfx_v9_4_3_sw_fini,
4716 	.hw_init = gfx_v9_4_3_hw_init,
4717 	.hw_fini = gfx_v9_4_3_hw_fini,
4718 	.suspend = gfx_v9_4_3_suspend,
4719 	.resume = gfx_v9_4_3_resume,
4720 	.is_idle = gfx_v9_4_3_is_idle,
4721 	.wait_for_idle = gfx_v9_4_3_wait_for_idle,
4722 	.soft_reset = gfx_v9_4_3_soft_reset,
4723 	.set_clockgating_state = gfx_v9_4_3_set_clockgating_state,
4724 	.set_powergating_state = gfx_v9_4_3_set_powergating_state,
4725 	.get_clockgating_state = gfx_v9_4_3_get_clockgating_state,
4726 	.dump_ip_state = gfx_v9_4_3_ip_dump,
4727 	.print_ip_state = gfx_v9_4_3_ip_print,
4728 };
4729 
4730 static const struct amdgpu_ring_funcs gfx_v9_4_3_ring_funcs_compute = {
4731 	.type = AMDGPU_RING_TYPE_COMPUTE,
4732 	.align_mask = 0xff,
4733 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
4734 	.support_64bit_ptrs = true,
4735 	.get_rptr = gfx_v9_4_3_ring_get_rptr_compute,
4736 	.get_wptr = gfx_v9_4_3_ring_get_wptr_compute,
4737 	.set_wptr = gfx_v9_4_3_ring_set_wptr_compute,
4738 	.emit_frame_size =
4739 		20 + /* gfx_v9_4_3_ring_emit_gds_switch */
4740 		7 + /* gfx_v9_4_3_ring_emit_hdp_flush */
4741 		5 + /* hdp invalidate */
4742 		7 + /* gfx_v9_4_3_ring_emit_pipeline_sync */
4743 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4744 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4745 		2 + /* gfx_v9_4_3_ring_emit_vm_flush */
4746 		8 + 8 + 8 + /* gfx_v9_4_3_ring_emit_fence x3 for user fence, vm fence */
4747 		7 + /* gfx_v9_4_3_emit_mem_sync */
4748 		5 + /* gfx_v9_4_3_emit_wave_limit for updating regSPI_WCL_PIPE_PERCENT_GFX register */
4749 		15 + /* for updating 3 regSPI_WCL_PIPE_PERCENT_CS registers */
4750 		2, /* gfx_v9_4_3_ring_emit_cleaner_shader */
4751 	.emit_ib_size =	7, /* gfx_v9_4_3_ring_emit_ib_compute */
4752 	.emit_ib = gfx_v9_4_3_ring_emit_ib_compute,
4753 	.emit_fence = gfx_v9_4_3_ring_emit_fence,
4754 	.emit_pipeline_sync = gfx_v9_4_3_ring_emit_pipeline_sync,
4755 	.emit_vm_flush = gfx_v9_4_3_ring_emit_vm_flush,
4756 	.emit_gds_switch = gfx_v9_4_3_ring_emit_gds_switch,
4757 	.emit_hdp_flush = gfx_v9_4_3_ring_emit_hdp_flush,
4758 	.test_ring = gfx_v9_4_3_ring_test_ring,
4759 	.test_ib = gfx_v9_4_3_ring_test_ib,
4760 	.insert_nop = gfx_v9_4_3_ring_insert_nop,
4761 	.pad_ib = amdgpu_ring_generic_pad_ib,
4762 	.emit_wreg = gfx_v9_4_3_ring_emit_wreg,
4763 	.emit_reg_wait = gfx_v9_4_3_ring_emit_reg_wait,
4764 	.emit_reg_write_reg_wait = gfx_v9_4_3_ring_emit_reg_write_reg_wait,
4765 	.soft_recovery = gfx_v9_4_3_ring_soft_recovery,
4766 	.emit_mem_sync = gfx_v9_4_3_emit_mem_sync,
4767 	.emit_wave_limit = gfx_v9_4_3_emit_wave_limit,
4768 	.reset = gfx_v9_4_3_reset_kcq,
4769 	.emit_cleaner_shader = gfx_v9_4_3_ring_emit_cleaner_shader,
4770 	.begin_use = amdgpu_gfx_enforce_isolation_ring_begin_use,
4771 	.end_use = amdgpu_gfx_enforce_isolation_ring_end_use,
4772 };
4773 
4774 static const struct amdgpu_ring_funcs gfx_v9_4_3_ring_funcs_kiq = {
4775 	.type = AMDGPU_RING_TYPE_KIQ,
4776 	.align_mask = 0xff,
4777 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
4778 	.support_64bit_ptrs = true,
4779 	.get_rptr = gfx_v9_4_3_ring_get_rptr_compute,
4780 	.get_wptr = gfx_v9_4_3_ring_get_wptr_compute,
4781 	.set_wptr = gfx_v9_4_3_ring_set_wptr_compute,
4782 	.emit_frame_size =
4783 		20 + /* gfx_v9_4_3_ring_emit_gds_switch */
4784 		7 + /* gfx_v9_4_3_ring_emit_hdp_flush */
4785 		5 + /* hdp invalidate */
4786 		7 + /* gfx_v9_4_3_ring_emit_pipeline_sync */
4787 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
4788 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
4789 		2 + /* gfx_v9_4_3_ring_emit_vm_flush */
4790 		8 + 8 + 8, /* gfx_v9_4_3_ring_emit_fence_kiq x3 for user fence, vm fence */
4791 	.emit_ib_size =	7, /* gfx_v9_4_3_ring_emit_ib_compute */
4792 	.emit_fence = gfx_v9_4_3_ring_emit_fence_kiq,
4793 	.test_ring = gfx_v9_4_3_ring_test_ring,
4794 	.insert_nop = amdgpu_ring_insert_nop,
4795 	.pad_ib = amdgpu_ring_generic_pad_ib,
4796 	.emit_rreg = gfx_v9_4_3_ring_emit_rreg,
4797 	.emit_wreg = gfx_v9_4_3_ring_emit_wreg,
4798 	.emit_reg_wait = gfx_v9_4_3_ring_emit_reg_wait,
4799 	.emit_reg_write_reg_wait = gfx_v9_4_3_ring_emit_reg_write_reg_wait,
4800 };
4801 
4802 static void gfx_v9_4_3_set_ring_funcs(struct amdgpu_device *adev)
4803 {
4804 	int i, j, num_xcc;
4805 
4806 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
4807 	for (i = 0; i < num_xcc; i++) {
4808 		adev->gfx.kiq[i].ring.funcs = &gfx_v9_4_3_ring_funcs_kiq;
4809 
4810 		for (j = 0; j < adev->gfx.num_compute_rings; j++)
4811 			adev->gfx.compute_ring[j + i * adev->gfx.num_compute_rings].funcs
4812 					= &gfx_v9_4_3_ring_funcs_compute;
4813 	}
4814 }
4815 
4816 static const struct amdgpu_irq_src_funcs gfx_v9_4_3_eop_irq_funcs = {
4817 	.set = gfx_v9_4_3_set_eop_interrupt_state,
4818 	.process = gfx_v9_4_3_eop_irq,
4819 };
4820 
4821 static const struct amdgpu_irq_src_funcs gfx_v9_4_3_priv_reg_irq_funcs = {
4822 	.set = gfx_v9_4_3_set_priv_reg_fault_state,
4823 	.process = gfx_v9_4_3_priv_reg_irq,
4824 };
4825 
4826 static const struct amdgpu_irq_src_funcs gfx_v9_4_3_bad_op_irq_funcs = {
4827 	.set = gfx_v9_4_3_set_bad_op_fault_state,
4828 	.process = gfx_v9_4_3_bad_op_irq,
4829 };
4830 
4831 static const struct amdgpu_irq_src_funcs gfx_v9_4_3_priv_inst_irq_funcs = {
4832 	.set = gfx_v9_4_3_set_priv_inst_fault_state,
4833 	.process = gfx_v9_4_3_priv_inst_irq,
4834 };
4835 
4836 static void gfx_v9_4_3_set_irq_funcs(struct amdgpu_device *adev)
4837 {
4838 	adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
4839 	adev->gfx.eop_irq.funcs = &gfx_v9_4_3_eop_irq_funcs;
4840 
4841 	adev->gfx.priv_reg_irq.num_types = 1;
4842 	adev->gfx.priv_reg_irq.funcs = &gfx_v9_4_3_priv_reg_irq_funcs;
4843 
4844 	adev->gfx.bad_op_irq.num_types = 1;
4845 	adev->gfx.bad_op_irq.funcs = &gfx_v9_4_3_bad_op_irq_funcs;
4846 
4847 	adev->gfx.priv_inst_irq.num_types = 1;
4848 	adev->gfx.priv_inst_irq.funcs = &gfx_v9_4_3_priv_inst_irq_funcs;
4849 }
4850 
4851 static void gfx_v9_4_3_set_rlc_funcs(struct amdgpu_device *adev)
4852 {
4853 	adev->gfx.rlc.funcs = &gfx_v9_4_3_rlc_funcs;
4854 }
4855 
4856 
4857 static void gfx_v9_4_3_set_gds_init(struct amdgpu_device *adev)
4858 {
4859 	/* init asci gds info */
4860 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
4861 	case IP_VERSION(9, 4, 3):
4862 	case IP_VERSION(9, 4, 4):
4863 		/* 9.4.3 removed all the GDS internal memory,
4864 		 * only support GWS opcode in kernel, like barrier
4865 		 * semaphore.etc */
4866 		adev->gds.gds_size = 0;
4867 		break;
4868 	default:
4869 		adev->gds.gds_size = 0x10000;
4870 		break;
4871 	}
4872 
4873 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
4874 	case IP_VERSION(9, 4, 3):
4875 	case IP_VERSION(9, 4, 4):
4876 		/* deprecated for 9.4.3, no usage at all */
4877 		adev->gds.gds_compute_max_wave_id = 0;
4878 		break;
4879 	default:
4880 		/* this really depends on the chip */
4881 		adev->gds.gds_compute_max_wave_id = 0x7ff;
4882 		break;
4883 	}
4884 
4885 	adev->gds.gws_size = 64;
4886 	adev->gds.oa_size = 16;
4887 }
4888 
4889 static void gfx_v9_4_3_set_user_cu_inactive_bitmap(struct amdgpu_device *adev,
4890 						 u32 bitmap, int xcc_id)
4891 {
4892 	u32 data;
4893 
4894 	if (!bitmap)
4895 		return;
4896 
4897 	data = bitmap << GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_CUS__SHIFT;
4898 	data &= GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_CUS_MASK;
4899 
4900 	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regGC_USER_SHADER_ARRAY_CONFIG, data);
4901 }
4902 
4903 static u32 gfx_v9_4_3_get_cu_active_bitmap(struct amdgpu_device *adev, int xcc_id)
4904 {
4905 	u32 data, mask;
4906 
4907 	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCC_GC_SHADER_ARRAY_CONFIG);
4908 	data |= RREG32_SOC15(GC, GET_INST(GC, xcc_id), regGC_USER_SHADER_ARRAY_CONFIG);
4909 
4910 	data &= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_CUS_MASK;
4911 	data >>= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_CUS__SHIFT;
4912 
4913 	mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_cu_per_sh);
4914 
4915 	return (~data) & mask;
4916 }
4917 
4918 static int gfx_v9_4_3_get_cu_info(struct amdgpu_device *adev,
4919 				 struct amdgpu_cu_info *cu_info)
4920 {
4921 	int i, j, k, prev_counter, counter, xcc_id, active_cu_number = 0;
4922 	u32 mask, bitmap, ao_bitmap, ao_cu_mask = 0, tmp;
4923 	unsigned disable_masks[4 * 4];
4924 	bool is_symmetric_cus;
4925 
4926 	if (!adev || !cu_info)
4927 		return -EINVAL;
4928 
4929 	/*
4930 	 * 16 comes from bitmap array size 4*4, and it can cover all gfx9 ASICs
4931 	 */
4932 	if (adev->gfx.config.max_shader_engines *
4933 		adev->gfx.config.max_sh_per_se > 16)
4934 		return -EINVAL;
4935 
4936 	amdgpu_gfx_parse_disable_cu(disable_masks,
4937 				    adev->gfx.config.max_shader_engines,
4938 				    adev->gfx.config.max_sh_per_se);
4939 
4940 	mutex_lock(&adev->grbm_idx_mutex);
4941 	for (xcc_id = 0; xcc_id < NUM_XCC(adev->gfx.xcc_mask); xcc_id++) {
4942 		is_symmetric_cus = true;
4943 		for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
4944 			for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
4945 				mask = 1;
4946 				ao_bitmap = 0;
4947 				counter = 0;
4948 				gfx_v9_4_3_xcc_select_se_sh(adev, i, j, 0xffffffff, xcc_id);
4949 				gfx_v9_4_3_set_user_cu_inactive_bitmap(
4950 					adev,
4951 					disable_masks[i * adev->gfx.config.max_sh_per_se + j],
4952 					xcc_id);
4953 				bitmap = gfx_v9_4_3_get_cu_active_bitmap(adev, xcc_id);
4954 
4955 				cu_info->bitmap[xcc_id][i][j] = bitmap;
4956 
4957 				for (k = 0; k < adev->gfx.config.max_cu_per_sh; k++) {
4958 					if (bitmap & mask) {
4959 						if (counter < adev->gfx.config.max_cu_per_sh)
4960 							ao_bitmap |= mask;
4961 						counter++;
4962 					}
4963 					mask <<= 1;
4964 				}
4965 				active_cu_number += counter;
4966 				if (i < 2 && j < 2)
4967 					ao_cu_mask |= (ao_bitmap << (i * 16 + j * 8));
4968 				cu_info->ao_cu_bitmap[i][j] = ao_bitmap;
4969 			}
4970 			if (i && is_symmetric_cus && prev_counter != counter)
4971 				is_symmetric_cus = false;
4972 			prev_counter = counter;
4973 		}
4974 		if (is_symmetric_cus) {
4975 			tmp = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_CPC_DEBUG);
4976 			tmp = REG_SET_FIELD(tmp, CP_CPC_DEBUG, CPC_HARVESTING_RELAUNCH_DISABLE, 1);
4977 			tmp = REG_SET_FIELD(tmp, CP_CPC_DEBUG, CPC_HARVESTING_DISPATCH_DISABLE, 1);
4978 			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regCP_CPC_DEBUG, tmp);
4979 		}
4980 		gfx_v9_4_3_xcc_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff,
4981 					    xcc_id);
4982 	}
4983 	mutex_unlock(&adev->grbm_idx_mutex);
4984 
4985 	cu_info->number = active_cu_number;
4986 	cu_info->ao_cu_mask = ao_cu_mask;
4987 	cu_info->simd_per_cu = NUM_SIMD_PER_CU;
4988 
4989 	return 0;
4990 }
4991 
4992 const struct amdgpu_ip_block_version gfx_v9_4_3_ip_block = {
4993 	.type = AMD_IP_BLOCK_TYPE_GFX,
4994 	.major = 9,
4995 	.minor = 4,
4996 	.rev = 3,
4997 	.funcs = &gfx_v9_4_3_ip_funcs,
4998 };
4999 
5000 static int gfx_v9_4_3_xcp_resume(void *handle, uint32_t inst_mask)
5001 {
5002 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
5003 	uint32_t tmp_mask;
5004 	int i, r;
5005 
5006 	/* TODO : Initialize golden regs */
5007 	/* gfx_v9_4_3_init_golden_registers(adev); */
5008 
5009 	tmp_mask = inst_mask;
5010 	for_each_inst(i, tmp_mask)
5011 		gfx_v9_4_3_xcc_constants_init(adev, i);
5012 
5013 	if (!amdgpu_sriov_vf(adev)) {
5014 		tmp_mask = inst_mask;
5015 		for_each_inst(i, tmp_mask) {
5016 			r = gfx_v9_4_3_xcc_rlc_resume(adev, i);
5017 			if (r)
5018 				return r;
5019 		}
5020 	}
5021 
5022 	tmp_mask = inst_mask;
5023 	for_each_inst(i, tmp_mask) {
5024 		r = gfx_v9_4_3_xcc_cp_resume(adev, i);
5025 		if (r)
5026 			return r;
5027 	}
5028 
5029 	return 0;
5030 }
5031 
5032 static int gfx_v9_4_3_xcp_suspend(void *handle, uint32_t inst_mask)
5033 {
5034 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
5035 	int i;
5036 
5037 	for_each_inst(i, inst_mask)
5038 		gfx_v9_4_3_xcc_fini(adev, i);
5039 
5040 	return 0;
5041 }
5042 
5043 struct amdgpu_xcp_ip_funcs gfx_v9_4_3_xcp_funcs = {
5044 	.suspend = &gfx_v9_4_3_xcp_suspend,
5045 	.resume = &gfx_v9_4_3_xcp_resume
5046 };
5047 
5048 struct amdgpu_ras_block_hw_ops  gfx_v9_4_3_ras_ops = {
5049 	.query_ras_error_count = &gfx_v9_4_3_query_ras_error_count,
5050 	.reset_ras_error_count = &gfx_v9_4_3_reset_ras_error_count,
5051 };
5052 
5053 static int gfx_v9_4_3_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
5054 {
5055 	int r;
5056 
5057 	r = amdgpu_ras_block_late_init(adev, ras_block);
5058 	if (r)
5059 		return r;
5060 
5061 	r = amdgpu_ras_bind_aca(adev, AMDGPU_RAS_BLOCK__GFX,
5062 				&gfx_v9_4_3_aca_info,
5063 				NULL);
5064 	if (r)
5065 		goto late_fini;
5066 
5067 	return 0;
5068 
5069 late_fini:
5070 	amdgpu_ras_block_late_fini(adev, ras_block);
5071 
5072 	return r;
5073 }
5074 
5075 struct amdgpu_gfx_ras gfx_v9_4_3_ras = {
5076 	.ras_block = {
5077 		.hw_ops = &gfx_v9_4_3_ras_ops,
5078 		.ras_late_init = &gfx_v9_4_3_ras_late_init,
5079 	},
5080 	.enable_watchdog_timer = &gfx_v9_4_3_enable_watchdog_timer,
5081 };
5082