xref: /linux/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 /*
2  * Copyright 2021 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/firmware.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include "amdgpu.h"
29 #include "amdgpu_gfx.h"
30 #include "amdgpu_psp.h"
31 #include "amdgpu_smu.h"
32 #include "amdgpu_atomfirmware.h"
33 #include "imu_v11_0.h"
34 #include "soc21.h"
35 #include "nvd.h"
36 
37 #include "gc/gc_11_0_0_offset.h"
38 #include "gc/gc_11_0_0_sh_mask.h"
39 #include "smuio/smuio_13_0_6_offset.h"
40 #include "smuio/smuio_13_0_6_sh_mask.h"
41 #include "navi10_enum.h"
42 #include "ivsrcid/gfx/irqsrcs_gfx_11_0_0.h"
43 
44 #include "soc15.h"
45 #include "soc15d.h"
46 #include "clearstate_gfx11.h"
47 #include "v11_structs.h"
48 #include "gfx_v11_0.h"
49 #include "gfx_v11_0_3.h"
50 #include "nbio_v4_3.h"
51 #include "mes_v11_0.h"
52 
53 #define GFX11_NUM_GFX_RINGS		1
54 #define GFX11_MEC_HPD_SIZE	2048
55 
56 #define RLCG_UCODE_LOADING_START_ADDRESS	0x00002000L
57 #define RLC_PG_DELAY_3_DEFAULT_GC_11_0_1	0x1388
58 
59 #define regCGTT_WD_CLK_CTRL		0x5086
60 #define regCGTT_WD_CLK_CTRL_BASE_IDX	1
61 #define regRLC_RLCS_BOOTLOAD_STATUS_gc_11_0_1	0x4e7e
62 #define regRLC_RLCS_BOOTLOAD_STATUS_gc_11_0_1_BASE_IDX	1
63 #define regPC_CONFIG_CNTL_1		0x194d
64 #define regPC_CONFIG_CNTL_1_BASE_IDX	1
65 
66 MODULE_FIRMWARE("amdgpu/gc_11_0_0_pfp.bin");
67 MODULE_FIRMWARE("amdgpu/gc_11_0_0_me.bin");
68 MODULE_FIRMWARE("amdgpu/gc_11_0_0_mec.bin");
69 MODULE_FIRMWARE("amdgpu/gc_11_0_0_rlc.bin");
70 MODULE_FIRMWARE("amdgpu/gc_11_0_0_rlc_1.bin");
71 MODULE_FIRMWARE("amdgpu/gc_11_0_0_toc.bin");
72 MODULE_FIRMWARE("amdgpu/gc_11_0_1_pfp.bin");
73 MODULE_FIRMWARE("amdgpu/gc_11_0_1_me.bin");
74 MODULE_FIRMWARE("amdgpu/gc_11_0_1_mec.bin");
75 MODULE_FIRMWARE("amdgpu/gc_11_0_1_rlc.bin");
76 MODULE_FIRMWARE("amdgpu/gc_11_0_2_pfp.bin");
77 MODULE_FIRMWARE("amdgpu/gc_11_0_2_me.bin");
78 MODULE_FIRMWARE("amdgpu/gc_11_0_2_mec.bin");
79 MODULE_FIRMWARE("amdgpu/gc_11_0_2_rlc.bin");
80 MODULE_FIRMWARE("amdgpu/gc_11_0_3_pfp.bin");
81 MODULE_FIRMWARE("amdgpu/gc_11_0_3_me.bin");
82 MODULE_FIRMWARE("amdgpu/gc_11_0_3_mec.bin");
83 MODULE_FIRMWARE("amdgpu/gc_11_0_3_rlc.bin");
84 MODULE_FIRMWARE("amdgpu/gc_11_0_4_pfp.bin");
85 MODULE_FIRMWARE("amdgpu/gc_11_0_4_me.bin");
86 MODULE_FIRMWARE("amdgpu/gc_11_0_4_mec.bin");
87 MODULE_FIRMWARE("amdgpu/gc_11_0_4_rlc.bin");
88 MODULE_FIRMWARE("amdgpu/gc_11_5_0_pfp.bin");
89 MODULE_FIRMWARE("amdgpu/gc_11_5_0_me.bin");
90 MODULE_FIRMWARE("amdgpu/gc_11_5_0_mec.bin");
91 MODULE_FIRMWARE("amdgpu/gc_11_5_0_rlc.bin");
92 
93 static const struct soc15_reg_golden golden_settings_gc_11_0[] = {
94 	SOC15_REG_GOLDEN_VALUE(GC, 0, regTCP_CNTL, 0x20000000, 0x20000000)
95 };
96 
97 static const struct soc15_reg_golden golden_settings_gc_11_0_1[] =
98 {
99 	SOC15_REG_GOLDEN_VALUE(GC, 0, regCGTT_GS_NGG_CLK_CTRL, 0x9fff8fff, 0x00000010),
100 	SOC15_REG_GOLDEN_VALUE(GC, 0, regCGTT_WD_CLK_CTRL, 0xffff8fff, 0x00000010),
101 	SOC15_REG_GOLDEN_VALUE(GC, 0, regCPF_GCR_CNTL, 0x0007ffff, 0x0000c200),
102 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGL2C_CTRL3, 0xffff001b, 0x00f01988),
103 	SOC15_REG_GOLDEN_VALUE(GC, 0, regPA_CL_ENHANCE, 0xf0ffffff, 0x00880007),
104 	SOC15_REG_GOLDEN_VALUE(GC, 0, regPA_SC_ENHANCE_3, 0xfffffffd, 0x00000008),
105 	SOC15_REG_GOLDEN_VALUE(GC, 0, regPA_SC_VRS_SURFACE_CNTL_1, 0xfff891ff, 0x55480100),
106 	SOC15_REG_GOLDEN_VALUE(GC, 0, regTA_CNTL_AUX, 0xf7f7ffff, 0x01030000),
107 	SOC15_REG_GOLDEN_VALUE(GC, 0, regTCP_CNTL2, 0xfcffffff, 0x0000000a)
108 };
109 
110 static const struct soc15_reg_golden golden_settings_gc_11_5_0[] = {
111 	SOC15_REG_GOLDEN_VALUE(GC, 0, regDB_DEBUG5, 0xffffffff, 0x00000800),
112 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGB_ADDR_CONFIG, 0x0c1807ff, 0x00000242),
113 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGCR_GENERAL_CNTL, 0x1ff1ffff, 0x00000500),
114 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGL2A_ADDR_MATCH_MASK, 0xffffffff, 0xfffffff3),
115 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGL2C_ADDR_MATCH_MASK, 0xffffffff, 0xfffffff3),
116 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGL2C_CTRL, 0xffffffff, 0xf37fff3f),
117 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGL2C_CTRL3, 0xfffffffb, 0x00f40188),
118 	SOC15_REG_GOLDEN_VALUE(GC, 0, regGL2C_CTRL4, 0xf0ffffff, 0x80009007),
119 	SOC15_REG_GOLDEN_VALUE(GC, 0, regPA_CL_ENHANCE, 0xf1ffffff, 0x00880007),
120 	SOC15_REG_GOLDEN_VALUE(GC, 0, regPC_CONFIG_CNTL_1, 0xffffffff, 0x00010000),
121 	SOC15_REG_GOLDEN_VALUE(GC, 0, regTA_CNTL_AUX, 0xf7f7ffff, 0x01030000),
122 	SOC15_REG_GOLDEN_VALUE(GC, 0, regTA_CNTL2, 0x007f0000, 0x00000000),
123 	SOC15_REG_GOLDEN_VALUE(GC, 0, regTCP_CNTL2, 0xffcfffff, 0x0000200a),
124 	SOC15_REG_GOLDEN_VALUE(GC, 0, regUTCL1_CTRL_2, 0xffffffff, 0x0000048f)
125 };
126 
127 #define DEFAULT_SH_MEM_CONFIG \
128 	((SH_MEM_ADDRESS_MODE_64 << SH_MEM_CONFIG__ADDRESS_MODE__SHIFT) | \
129 	 (SH_MEM_ALIGNMENT_MODE_UNALIGNED << SH_MEM_CONFIG__ALIGNMENT_MODE__SHIFT) | \
130 	 (3 << SH_MEM_CONFIG__INITIAL_INST_PREFETCH__SHIFT))
131 
132 static void gfx_v11_0_disable_gpa_mode(struct amdgpu_device *adev);
133 static void gfx_v11_0_set_ring_funcs(struct amdgpu_device *adev);
134 static void gfx_v11_0_set_irq_funcs(struct amdgpu_device *adev);
135 static void gfx_v11_0_set_gds_init(struct amdgpu_device *adev);
136 static void gfx_v11_0_set_rlc_funcs(struct amdgpu_device *adev);
137 static void gfx_v11_0_set_mqd_funcs(struct amdgpu_device *adev);
138 static void gfx_v11_0_set_imu_funcs(struct amdgpu_device *adev);
139 static int gfx_v11_0_get_cu_info(struct amdgpu_device *adev,
140                                  struct amdgpu_cu_info *cu_info);
141 static uint64_t gfx_v11_0_get_gpu_clock_counter(struct amdgpu_device *adev);
142 static void gfx_v11_0_select_se_sh(struct amdgpu_device *adev, u32 se_num,
143 				   u32 sh_num, u32 instance, int xcc_id);
144 static u32 gfx_v11_0_get_wgp_active_bitmap_per_sh(struct amdgpu_device *adev);
145 
146 static void gfx_v11_0_ring_emit_de_meta(struct amdgpu_ring *ring, bool resume);
147 static void gfx_v11_0_ring_emit_frame_cntl(struct amdgpu_ring *ring, bool start, bool secure);
148 static void gfx_v11_0_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg,
149 				     uint32_t val);
150 static int gfx_v11_0_wait_for_rlc_autoload_complete(struct amdgpu_device *adev);
151 static void gfx_v11_0_ring_invalidate_tlbs(struct amdgpu_ring *ring,
152 					   uint16_t pasid, uint32_t flush_type,
153 					   bool all_hub, uint8_t dst_sel);
154 static void gfx_v11_0_set_safe_mode(struct amdgpu_device *adev, int xcc_id);
155 static void gfx_v11_0_unset_safe_mode(struct amdgpu_device *adev, int xcc_id);
156 static void gfx_v11_0_update_perf_clk(struct amdgpu_device *adev,
157 				      bool enable);
158 
159 static void gfx11_kiq_set_resources(struct amdgpu_ring *kiq_ring, uint64_t queue_mask)
160 {
161 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_RESOURCES, 6));
162 	amdgpu_ring_write(kiq_ring, PACKET3_SET_RESOURCES_VMID_MASK(0) |
163 			  PACKET3_SET_RESOURCES_UNMAP_LATENTY(0xa) | /* unmap_latency: 0xa (~ 1s) */
164 			  PACKET3_SET_RESOURCES_QUEUE_TYPE(0));	/* vmid_mask:0 queue_type:0 (KIQ) */
165 	amdgpu_ring_write(kiq_ring, lower_32_bits(queue_mask));	/* queue mask lo */
166 	amdgpu_ring_write(kiq_ring, upper_32_bits(queue_mask));	/* queue mask hi */
167 	amdgpu_ring_write(kiq_ring, 0);	/* gws mask lo */
168 	amdgpu_ring_write(kiq_ring, 0);	/* gws mask hi */
169 	amdgpu_ring_write(kiq_ring, 0);	/* oac mask */
170 	amdgpu_ring_write(kiq_ring, 0);	/* gds heap base:0, gds heap size:0 */
171 }
172 
173 static void gfx11_kiq_map_queues(struct amdgpu_ring *kiq_ring,
174 				 struct amdgpu_ring *ring)
175 {
176 	uint64_t mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
177 	uint64_t wptr_addr = ring->wptr_gpu_addr;
178 	uint32_t me = 0, eng_sel = 0;
179 
180 	switch (ring->funcs->type) {
181 	case AMDGPU_RING_TYPE_COMPUTE:
182 		me = 1;
183 		eng_sel = 0;
184 		break;
185 	case AMDGPU_RING_TYPE_GFX:
186 		me = 0;
187 		eng_sel = 4;
188 		break;
189 	case AMDGPU_RING_TYPE_MES:
190 		me = 2;
191 		eng_sel = 5;
192 		break;
193 	default:
194 		WARN_ON(1);
195 	}
196 
197 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_MAP_QUEUES, 5));
198 	/* Q_sel:0, vmid:0, vidmem: 1, engine:0, num_Q:1*/
199 	amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
200 			  PACKET3_MAP_QUEUES_QUEUE_SEL(0) | /* Queue_Sel */
201 			  PACKET3_MAP_QUEUES_VMID(0) | /* VMID */
202 			  PACKET3_MAP_QUEUES_QUEUE(ring->queue) |
203 			  PACKET3_MAP_QUEUES_PIPE(ring->pipe) |
204 			  PACKET3_MAP_QUEUES_ME((me)) |
205 			  PACKET3_MAP_QUEUES_QUEUE_TYPE(0) | /*queue_type: normal compute queue */
206 			  PACKET3_MAP_QUEUES_ALLOC_FORMAT(0) | /* alloc format: all_on_one_pipe */
207 			  PACKET3_MAP_QUEUES_ENGINE_SEL(eng_sel) |
208 			  PACKET3_MAP_QUEUES_NUM_QUEUES(1)); /* num_queues: must be 1 */
209 	amdgpu_ring_write(kiq_ring, PACKET3_MAP_QUEUES_DOORBELL_OFFSET(ring->doorbell_index));
210 	amdgpu_ring_write(kiq_ring, lower_32_bits(mqd_addr));
211 	amdgpu_ring_write(kiq_ring, upper_32_bits(mqd_addr));
212 	amdgpu_ring_write(kiq_ring, lower_32_bits(wptr_addr));
213 	amdgpu_ring_write(kiq_ring, upper_32_bits(wptr_addr));
214 }
215 
216 static void gfx11_kiq_unmap_queues(struct amdgpu_ring *kiq_ring,
217 				   struct amdgpu_ring *ring,
218 				   enum amdgpu_unmap_queues_action action,
219 				   u64 gpu_addr, u64 seq)
220 {
221 	struct amdgpu_device *adev = kiq_ring->adev;
222 	uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
223 
224 	if (adev->enable_mes && !adev->gfx.kiq[0].ring.sched.ready) {
225 		amdgpu_mes_unmap_legacy_queue(adev, ring, action, gpu_addr, seq);
226 		return;
227 	}
228 
229 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_UNMAP_QUEUES, 4));
230 	amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
231 			  PACKET3_UNMAP_QUEUES_ACTION(action) |
232 			  PACKET3_UNMAP_QUEUES_QUEUE_SEL(0) |
233 			  PACKET3_UNMAP_QUEUES_ENGINE_SEL(eng_sel) |
234 			  PACKET3_UNMAP_QUEUES_NUM_QUEUES(1));
235 	amdgpu_ring_write(kiq_ring,
236 		  PACKET3_UNMAP_QUEUES_DOORBELL_OFFSET0(ring->doorbell_index));
237 
238 	if (action == PREEMPT_QUEUES_NO_UNMAP) {
239 		amdgpu_ring_write(kiq_ring, lower_32_bits(gpu_addr));
240 		amdgpu_ring_write(kiq_ring, upper_32_bits(gpu_addr));
241 		amdgpu_ring_write(kiq_ring, seq);
242 	} else {
243 		amdgpu_ring_write(kiq_ring, 0);
244 		amdgpu_ring_write(kiq_ring, 0);
245 		amdgpu_ring_write(kiq_ring, 0);
246 	}
247 }
248 
249 static void gfx11_kiq_query_status(struct amdgpu_ring *kiq_ring,
250 				   struct amdgpu_ring *ring,
251 				   u64 addr,
252 				   u64 seq)
253 {
254 	uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
255 
256 	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_QUERY_STATUS, 5));
257 	amdgpu_ring_write(kiq_ring,
258 			  PACKET3_QUERY_STATUS_CONTEXT_ID(0) |
259 			  PACKET3_QUERY_STATUS_INTERRUPT_SEL(0) |
260 			  PACKET3_QUERY_STATUS_COMMAND(2));
261 	amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
262 			  PACKET3_QUERY_STATUS_DOORBELL_OFFSET(ring->doorbell_index) |
263 			  PACKET3_QUERY_STATUS_ENG_SEL(eng_sel));
264 	amdgpu_ring_write(kiq_ring, lower_32_bits(addr));
265 	amdgpu_ring_write(kiq_ring, upper_32_bits(addr));
266 	amdgpu_ring_write(kiq_ring, lower_32_bits(seq));
267 	amdgpu_ring_write(kiq_ring, upper_32_bits(seq));
268 }
269 
270 static void gfx11_kiq_invalidate_tlbs(struct amdgpu_ring *kiq_ring,
271 				uint16_t pasid, uint32_t flush_type,
272 				bool all_hub)
273 {
274 	gfx_v11_0_ring_invalidate_tlbs(kiq_ring, pasid, flush_type, all_hub, 1);
275 }
276 
277 static const struct kiq_pm4_funcs gfx_v11_0_kiq_pm4_funcs = {
278 	.kiq_set_resources = gfx11_kiq_set_resources,
279 	.kiq_map_queues = gfx11_kiq_map_queues,
280 	.kiq_unmap_queues = gfx11_kiq_unmap_queues,
281 	.kiq_query_status = gfx11_kiq_query_status,
282 	.kiq_invalidate_tlbs = gfx11_kiq_invalidate_tlbs,
283 	.set_resources_size = 8,
284 	.map_queues_size = 7,
285 	.unmap_queues_size = 6,
286 	.query_status_size = 7,
287 	.invalidate_tlbs_size = 2,
288 };
289 
290 static void gfx_v11_0_set_kiq_pm4_funcs(struct amdgpu_device *adev)
291 {
292 	adev->gfx.kiq[0].pmf = &gfx_v11_0_kiq_pm4_funcs;
293 }
294 
295 static void gfx_v11_0_init_golden_registers(struct amdgpu_device *adev)
296 {
297 	if (amdgpu_sriov_vf(adev))
298 		return;
299 
300 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
301 	case IP_VERSION(11, 0, 1):
302 	case IP_VERSION(11, 0, 4):
303 		soc15_program_register_sequence(adev,
304 						golden_settings_gc_11_0_1,
305 						(const u32)ARRAY_SIZE(golden_settings_gc_11_0_1));
306 		break;
307 	case IP_VERSION(11, 5, 0):
308 		soc15_program_register_sequence(adev,
309 						golden_settings_gc_11_5_0,
310 						(const u32)ARRAY_SIZE(golden_settings_gc_11_5_0));
311 		break;
312 	default:
313 		break;
314 	}
315 	soc15_program_register_sequence(adev,
316 					golden_settings_gc_11_0,
317 					(const u32)ARRAY_SIZE(golden_settings_gc_11_0));
318 
319 }
320 
321 static void gfx_v11_0_write_data_to_reg(struct amdgpu_ring *ring, int eng_sel,
322 				       bool wc, uint32_t reg, uint32_t val)
323 {
324 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
325 	amdgpu_ring_write(ring, WRITE_DATA_ENGINE_SEL(eng_sel) |
326 			  WRITE_DATA_DST_SEL(0) | (wc ? WR_CONFIRM : 0));
327 	amdgpu_ring_write(ring, reg);
328 	amdgpu_ring_write(ring, 0);
329 	amdgpu_ring_write(ring, val);
330 }
331 
332 static void gfx_v11_0_wait_reg_mem(struct amdgpu_ring *ring, int eng_sel,
333 				  int mem_space, int opt, uint32_t addr0,
334 				  uint32_t addr1, uint32_t ref, uint32_t mask,
335 				  uint32_t inv)
336 {
337 	amdgpu_ring_write(ring, PACKET3(PACKET3_WAIT_REG_MEM, 5));
338 	amdgpu_ring_write(ring,
339 			  /* memory (1) or register (0) */
340 			  (WAIT_REG_MEM_MEM_SPACE(mem_space) |
341 			   WAIT_REG_MEM_OPERATION(opt) | /* wait */
342 			   WAIT_REG_MEM_FUNCTION(3) |  /* equal */
343 			   WAIT_REG_MEM_ENGINE(eng_sel)));
344 
345 	if (mem_space)
346 		BUG_ON(addr0 & 0x3); /* Dword align */
347 	amdgpu_ring_write(ring, addr0);
348 	amdgpu_ring_write(ring, addr1);
349 	amdgpu_ring_write(ring, ref);
350 	amdgpu_ring_write(ring, mask);
351 	amdgpu_ring_write(ring, inv); /* poll interval */
352 }
353 
354 static int gfx_v11_0_ring_test_ring(struct amdgpu_ring *ring)
355 {
356 	struct amdgpu_device *adev = ring->adev;
357 	uint32_t scratch = SOC15_REG_OFFSET(GC, 0, regSCRATCH_REG0);
358 	uint32_t tmp = 0;
359 	unsigned i;
360 	int r;
361 
362 	WREG32(scratch, 0xCAFEDEAD);
363 	r = amdgpu_ring_alloc(ring, 5);
364 	if (r) {
365 		DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
366 			  ring->idx, r);
367 		return r;
368 	}
369 
370 	if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) {
371 		gfx_v11_0_ring_emit_wreg(ring, scratch, 0xDEADBEEF);
372 	} else {
373 		amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
374 		amdgpu_ring_write(ring, scratch -
375 				  PACKET3_SET_UCONFIG_REG_START);
376 		amdgpu_ring_write(ring, 0xDEADBEEF);
377 	}
378 	amdgpu_ring_commit(ring);
379 
380 	for (i = 0; i < adev->usec_timeout; i++) {
381 		tmp = RREG32(scratch);
382 		if (tmp == 0xDEADBEEF)
383 			break;
384 		if (amdgpu_emu_mode == 1)
385 			msleep(1);
386 		else
387 			udelay(1);
388 	}
389 
390 	if (i >= adev->usec_timeout)
391 		r = -ETIMEDOUT;
392 	return r;
393 }
394 
395 static int gfx_v11_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
396 {
397 	struct amdgpu_device *adev = ring->adev;
398 	struct amdgpu_ib ib;
399 	struct dma_fence *f = NULL;
400 	unsigned index;
401 	uint64_t gpu_addr;
402 	volatile uint32_t *cpu_ptr;
403 	long r;
404 
405 	/* MES KIQ fw hasn't indirect buffer support for now */
406 	if (adev->enable_mes_kiq &&
407 	    ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
408 		return 0;
409 
410 	memset(&ib, 0, sizeof(ib));
411 
412 	if (ring->is_mes_queue) {
413 		uint32_t padding, offset;
414 
415 		offset = amdgpu_mes_ctx_get_offs(ring, AMDGPU_MES_CTX_IB_OFFS);
416 		padding = amdgpu_mes_ctx_get_offs(ring,
417 						  AMDGPU_MES_CTX_PADDING_OFFS);
418 
419 		ib.gpu_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
420 		ib.ptr = amdgpu_mes_ctx_get_offs_cpu_addr(ring, offset);
421 
422 		gpu_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, padding);
423 		cpu_ptr = amdgpu_mes_ctx_get_offs_cpu_addr(ring, padding);
424 		*cpu_ptr = cpu_to_le32(0xCAFEDEAD);
425 	} else {
426 		r = amdgpu_device_wb_get(adev, &index);
427 		if (r)
428 			return r;
429 
430 		gpu_addr = adev->wb.gpu_addr + (index * 4);
431 		adev->wb.wb[index] = cpu_to_le32(0xCAFEDEAD);
432 		cpu_ptr = &adev->wb.wb[index];
433 
434 		r = amdgpu_ib_get(adev, NULL, 20, AMDGPU_IB_POOL_DIRECT, &ib);
435 		if (r) {
436 			DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
437 			goto err1;
438 		}
439 	}
440 
441 	ib.ptr[0] = PACKET3(PACKET3_WRITE_DATA, 3);
442 	ib.ptr[1] = WRITE_DATA_DST_SEL(5) | WR_CONFIRM;
443 	ib.ptr[2] = lower_32_bits(gpu_addr);
444 	ib.ptr[3] = upper_32_bits(gpu_addr);
445 	ib.ptr[4] = 0xDEADBEEF;
446 	ib.length_dw = 5;
447 
448 	r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
449 	if (r)
450 		goto err2;
451 
452 	r = dma_fence_wait_timeout(f, false, timeout);
453 	if (r == 0) {
454 		r = -ETIMEDOUT;
455 		goto err2;
456 	} else if (r < 0) {
457 		goto err2;
458 	}
459 
460 	if (le32_to_cpu(*cpu_ptr) == 0xDEADBEEF)
461 		r = 0;
462 	else
463 		r = -EINVAL;
464 err2:
465 	if (!ring->is_mes_queue)
466 		amdgpu_ib_free(adev, &ib, NULL);
467 	dma_fence_put(f);
468 err1:
469 	if (!ring->is_mes_queue)
470 		amdgpu_device_wb_free(adev, index);
471 	return r;
472 }
473 
474 static void gfx_v11_0_free_microcode(struct amdgpu_device *adev)
475 {
476 	amdgpu_ucode_release(&adev->gfx.pfp_fw);
477 	amdgpu_ucode_release(&adev->gfx.me_fw);
478 	amdgpu_ucode_release(&adev->gfx.rlc_fw);
479 	amdgpu_ucode_release(&adev->gfx.mec_fw);
480 
481 	kfree(adev->gfx.rlc.register_list_format);
482 }
483 
484 static int gfx_v11_0_init_toc_microcode(struct amdgpu_device *adev, const char *ucode_prefix)
485 {
486 	const struct psp_firmware_header_v1_0 *toc_hdr;
487 	int err = 0;
488 	char fw_name[40];
489 
490 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", ucode_prefix);
491 	err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
492 	if (err)
493 		goto out;
494 
495 	toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
496 	adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
497 	adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
498 	adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
499 	adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
500 				le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
501 	return 0;
502 out:
503 	amdgpu_ucode_release(&adev->psp.toc_fw);
504 	return err;
505 }
506 
507 static void gfx_v11_0_check_fw_cp_gfx_shadow(struct amdgpu_device *adev)
508 {
509 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
510 	case IP_VERSION(11, 0, 0):
511 	case IP_VERSION(11, 0, 2):
512 	case IP_VERSION(11, 0, 3):
513 		if ((adev->gfx.me_fw_version >= 1505) &&
514 		    (adev->gfx.pfp_fw_version >= 1600) &&
515 		    (adev->gfx.mec_fw_version >= 512)) {
516 			if (amdgpu_sriov_vf(adev))
517 				adev->gfx.cp_gfx_shadow = true;
518 			else
519 				adev->gfx.cp_gfx_shadow = false;
520 		}
521 		break;
522 	default:
523 		adev->gfx.cp_gfx_shadow = false;
524 		break;
525 	}
526 }
527 
528 static int gfx_v11_0_init_microcode(struct amdgpu_device *adev)
529 {
530 	char fw_name[40];
531 	char ucode_prefix[30];
532 	int err;
533 	const struct rlc_firmware_header_v2_0 *rlc_hdr;
534 	uint16_t version_major;
535 	uint16_t version_minor;
536 
537 	DRM_DEBUG("\n");
538 
539 	amdgpu_ucode_ip_version_decode(adev, GC_HWIP, ucode_prefix, sizeof(ucode_prefix));
540 
541 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_pfp.bin", ucode_prefix);
542 	err = amdgpu_ucode_request(adev, &adev->gfx.pfp_fw, fw_name);
543 	if (err)
544 		goto out;
545 	/* check pfp fw hdr version to decide if enable rs64 for gfx11.*/
546 	adev->gfx.rs64_enable = amdgpu_ucode_hdr_version(
547 				(union amdgpu_firmware_header *)
548 				adev->gfx.pfp_fw->data, 2, 0);
549 	if (adev->gfx.rs64_enable) {
550 		dev_info(adev->dev, "CP RS64 enable\n");
551 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_PFP);
552 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK);
553 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK);
554 	} else {
555 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_PFP);
556 	}
557 
558 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_me.bin", ucode_prefix);
559 	err = amdgpu_ucode_request(adev, &adev->gfx.me_fw, fw_name);
560 	if (err)
561 		goto out;
562 	if (adev->gfx.rs64_enable) {
563 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_ME);
564 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK);
565 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK);
566 	} else {
567 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_ME);
568 	}
569 
570 	if (!amdgpu_sriov_vf(adev)) {
571 		if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 0, 0) &&
572 		    adev->pdev->revision == 0xCE)
573 			snprintf(fw_name, sizeof(fw_name), "amdgpu/gc_11_0_0_rlc_1.bin");
574 		else
575 			snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_rlc.bin", ucode_prefix);
576 		err = amdgpu_ucode_request(adev, &adev->gfx.rlc_fw, fw_name);
577 		if (err)
578 			goto out;
579 		rlc_hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
580 		version_major = le16_to_cpu(rlc_hdr->header.header_version_major);
581 		version_minor = le16_to_cpu(rlc_hdr->header.header_version_minor);
582 		err = amdgpu_gfx_rlc_init_microcode(adev, version_major, version_minor);
583 		if (err)
584 			goto out;
585 	}
586 
587 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mec.bin", ucode_prefix);
588 	err = amdgpu_ucode_request(adev, &adev->gfx.mec_fw, fw_name);
589 	if (err)
590 		goto out;
591 	if (adev->gfx.rs64_enable) {
592 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_MEC);
593 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK);
594 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK);
595 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK);
596 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK);
597 	} else {
598 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_MEC1);
599 		amdgpu_gfx_cp_init_microcode(adev, AMDGPU_UCODE_ID_CP_MEC1_JT);
600 	}
601 
602 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO)
603 		err = gfx_v11_0_init_toc_microcode(adev, ucode_prefix);
604 
605 	/* only one MEC for gfx 11.0.0. */
606 	adev->gfx.mec2_fw = NULL;
607 
608 	gfx_v11_0_check_fw_cp_gfx_shadow(adev);
609 
610 	if (adev->gfx.imu.funcs && adev->gfx.imu.funcs->init_microcode) {
611 		err = adev->gfx.imu.funcs->init_microcode(adev);
612 		if (err)
613 			DRM_ERROR("Failed to init imu firmware!\n");
614 		return err;
615 	}
616 
617 out:
618 	if (err) {
619 		amdgpu_ucode_release(&adev->gfx.pfp_fw);
620 		amdgpu_ucode_release(&adev->gfx.me_fw);
621 		amdgpu_ucode_release(&adev->gfx.rlc_fw);
622 		amdgpu_ucode_release(&adev->gfx.mec_fw);
623 	}
624 
625 	return err;
626 }
627 
628 static u32 gfx_v11_0_get_csb_size(struct amdgpu_device *adev)
629 {
630 	u32 count = 0;
631 	const struct cs_section_def *sect = NULL;
632 	const struct cs_extent_def *ext = NULL;
633 
634 	/* begin clear state */
635 	count += 2;
636 	/* context control state */
637 	count += 3;
638 
639 	for (sect = gfx11_cs_data; sect->section != NULL; ++sect) {
640 		for (ext = sect->section; ext->extent != NULL; ++ext) {
641 			if (sect->id == SECT_CONTEXT)
642 				count += 2 + ext->reg_count;
643 			else
644 				return 0;
645 		}
646 	}
647 
648 	/* set PA_SC_TILE_STEERING_OVERRIDE */
649 	count += 3;
650 	/* end clear state */
651 	count += 2;
652 	/* clear state */
653 	count += 2;
654 
655 	return count;
656 }
657 
658 static void gfx_v11_0_get_csb_buffer(struct amdgpu_device *adev,
659 				    volatile u32 *buffer)
660 {
661 	u32 count = 0, i;
662 	const struct cs_section_def *sect = NULL;
663 	const struct cs_extent_def *ext = NULL;
664 	int ctx_reg_offset;
665 
666 	if (adev->gfx.rlc.cs_data == NULL)
667 		return;
668 	if (buffer == NULL)
669 		return;
670 
671 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
672 	buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
673 
674 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CONTEXT_CONTROL, 1));
675 	buffer[count++] = cpu_to_le32(0x80000000);
676 	buffer[count++] = cpu_to_le32(0x80000000);
677 
678 	for (sect = adev->gfx.rlc.cs_data; sect->section != NULL; ++sect) {
679 		for (ext = sect->section; ext->extent != NULL; ++ext) {
680 			if (sect->id == SECT_CONTEXT) {
681 				buffer[count++] =
682 					cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, ext->reg_count));
683 				buffer[count++] = cpu_to_le32(ext->reg_index -
684 						PACKET3_SET_CONTEXT_REG_START);
685 				for (i = 0; i < ext->reg_count; i++)
686 					buffer[count++] = cpu_to_le32(ext->extent[i]);
687 			} else {
688 				return;
689 			}
690 		}
691 	}
692 
693 	ctx_reg_offset =
694 		SOC15_REG_OFFSET(GC, 0, regPA_SC_TILE_STEERING_OVERRIDE) - PACKET3_SET_CONTEXT_REG_START;
695 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, 1));
696 	buffer[count++] = cpu_to_le32(ctx_reg_offset);
697 	buffer[count++] = cpu_to_le32(adev->gfx.config.pa_sc_tile_steering_override);
698 
699 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
700 	buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_END_CLEAR_STATE);
701 
702 	buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CLEAR_STATE, 0));
703 	buffer[count++] = cpu_to_le32(0);
704 }
705 
706 static void gfx_v11_0_rlc_fini(struct amdgpu_device *adev)
707 {
708 	/* clear state block */
709 	amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
710 			&adev->gfx.rlc.clear_state_gpu_addr,
711 			(void **)&adev->gfx.rlc.cs_ptr);
712 
713 	/* jump table block */
714 	amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
715 			&adev->gfx.rlc.cp_table_gpu_addr,
716 			(void **)&adev->gfx.rlc.cp_table_ptr);
717 }
718 
719 static void gfx_v11_0_init_rlcg_reg_access_ctrl(struct amdgpu_device *adev)
720 {
721 	struct amdgpu_rlcg_reg_access_ctrl *reg_access_ctrl;
722 
723 	reg_access_ctrl = &adev->gfx.rlc.reg_access_ctrl[0];
724 	reg_access_ctrl->scratch_reg0 = SOC15_REG_OFFSET(GC, 0, regSCRATCH_REG0);
725 	reg_access_ctrl->scratch_reg1 = SOC15_REG_OFFSET(GC, 0, regSCRATCH_REG1);
726 	reg_access_ctrl->scratch_reg2 = SOC15_REG_OFFSET(GC, 0, regSCRATCH_REG2);
727 	reg_access_ctrl->scratch_reg3 = SOC15_REG_OFFSET(GC, 0, regSCRATCH_REG3);
728 	reg_access_ctrl->grbm_cntl = SOC15_REG_OFFSET(GC, 0, regGRBM_GFX_CNTL);
729 	reg_access_ctrl->grbm_idx = SOC15_REG_OFFSET(GC, 0, regGRBM_GFX_INDEX);
730 	reg_access_ctrl->spare_int = SOC15_REG_OFFSET(GC, 0, regRLC_SPARE_INT_0);
731 	adev->gfx.rlc.rlcg_reg_access_supported = true;
732 }
733 
734 static int gfx_v11_0_rlc_init(struct amdgpu_device *adev)
735 {
736 	const struct cs_section_def *cs_data;
737 	int r;
738 
739 	adev->gfx.rlc.cs_data = gfx11_cs_data;
740 
741 	cs_data = adev->gfx.rlc.cs_data;
742 
743 	if (cs_data) {
744 		/* init clear state block */
745 		r = amdgpu_gfx_rlc_init_csb(adev);
746 		if (r)
747 			return r;
748 	}
749 
750 	/* init spm vmid with 0xf */
751 	if (adev->gfx.rlc.funcs->update_spm_vmid)
752 		adev->gfx.rlc.funcs->update_spm_vmid(adev, 0xf);
753 
754 	return 0;
755 }
756 
757 static void gfx_v11_0_mec_fini(struct amdgpu_device *adev)
758 {
759 	amdgpu_bo_free_kernel(&adev->gfx.mec.hpd_eop_obj, NULL, NULL);
760 	amdgpu_bo_free_kernel(&adev->gfx.mec.mec_fw_obj, NULL, NULL);
761 	amdgpu_bo_free_kernel(&adev->gfx.mec.mec_fw_data_obj, NULL, NULL);
762 }
763 
764 static void gfx_v11_0_me_init(struct amdgpu_device *adev)
765 {
766 	bitmap_zero(adev->gfx.me.queue_bitmap, AMDGPU_MAX_GFX_QUEUES);
767 
768 	amdgpu_gfx_graphics_queue_acquire(adev);
769 }
770 
771 static int gfx_v11_0_mec_init(struct amdgpu_device *adev)
772 {
773 	int r;
774 	u32 *hpd;
775 	size_t mec_hpd_size;
776 
777 	bitmap_zero(adev->gfx.mec_bitmap[0].queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
778 
779 	/* take ownership of the relevant compute queues */
780 	amdgpu_gfx_compute_queue_acquire(adev);
781 	mec_hpd_size = adev->gfx.num_compute_rings * GFX11_MEC_HPD_SIZE;
782 
783 	if (mec_hpd_size) {
784 		r = amdgpu_bo_create_reserved(adev, mec_hpd_size, PAGE_SIZE,
785 					      AMDGPU_GEM_DOMAIN_GTT,
786 					      &adev->gfx.mec.hpd_eop_obj,
787 					      &adev->gfx.mec.hpd_eop_gpu_addr,
788 					      (void **)&hpd);
789 		if (r) {
790 			dev_warn(adev->dev, "(%d) create HDP EOP bo failed\n", r);
791 			gfx_v11_0_mec_fini(adev);
792 			return r;
793 		}
794 
795 		memset(hpd, 0, mec_hpd_size);
796 
797 		amdgpu_bo_kunmap(adev->gfx.mec.hpd_eop_obj);
798 		amdgpu_bo_unreserve(adev->gfx.mec.hpd_eop_obj);
799 	}
800 
801 	return 0;
802 }
803 
804 static uint32_t wave_read_ind(struct amdgpu_device *adev, uint32_t wave, uint32_t address)
805 {
806 	WREG32_SOC15(GC, 0, regSQ_IND_INDEX,
807 		(wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
808 		(address << SQ_IND_INDEX__INDEX__SHIFT));
809 	return RREG32_SOC15(GC, 0, regSQ_IND_DATA);
810 }
811 
812 static void wave_read_regs(struct amdgpu_device *adev, uint32_t wave,
813 			   uint32_t thread, uint32_t regno,
814 			   uint32_t num, uint32_t *out)
815 {
816 	WREG32_SOC15(GC, 0, regSQ_IND_INDEX,
817 		(wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
818 		(regno << SQ_IND_INDEX__INDEX__SHIFT) |
819 		(thread << SQ_IND_INDEX__WORKITEM_ID__SHIFT) |
820 		(SQ_IND_INDEX__AUTO_INCR_MASK));
821 	while (num--)
822 		*(out++) = RREG32_SOC15(GC, 0, regSQ_IND_DATA);
823 }
824 
825 static void gfx_v11_0_read_wave_data(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd, uint32_t wave, uint32_t *dst, int *no_fields)
826 {
827 	/* in gfx11 the SIMD_ID is specified as part of the INSTANCE
828 	 * field when performing a select_se_sh so it should be
829 	 * zero here */
830 	WARN_ON(simd != 0);
831 
832 	/* type 3 wave data */
833 	dst[(*no_fields)++] = 3;
834 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_STATUS);
835 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_PC_LO);
836 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_PC_HI);
837 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_EXEC_LO);
838 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_EXEC_HI);
839 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_HW_ID1);
840 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_HW_ID2);
841 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_GPR_ALLOC);
842 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_LDS_ALLOC);
843 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_TRAPSTS);
844 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_IB_STS);
845 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_IB_STS2);
846 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_IB_DBG1);
847 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_M0);
848 	dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_MODE);
849 }
850 
851 static void gfx_v11_0_read_wave_sgprs(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd,
852 				     uint32_t wave, uint32_t start,
853 				     uint32_t size, uint32_t *dst)
854 {
855 	WARN_ON(simd != 0);
856 
857 	wave_read_regs(
858 		adev, wave, 0, start + SQIND_WAVE_SGPRS_OFFSET, size,
859 		dst);
860 }
861 
862 static void gfx_v11_0_read_wave_vgprs(struct amdgpu_device *adev, uint32_t xcc_id, uint32_t simd,
863 				      uint32_t wave, uint32_t thread,
864 				      uint32_t start, uint32_t size,
865 				      uint32_t *dst)
866 {
867 	wave_read_regs(
868 		adev, wave, thread,
869 		start + SQIND_WAVE_VGPRS_OFFSET, size, dst);
870 }
871 
872 static void gfx_v11_0_select_me_pipe_q(struct amdgpu_device *adev,
873 					u32 me, u32 pipe, u32 q, u32 vm, u32 xcc_id)
874 {
875 	soc21_grbm_select(adev, me, pipe, q, vm);
876 }
877 
878 /* all sizes are in bytes */
879 #define MQD_SHADOW_BASE_SIZE      73728
880 #define MQD_SHADOW_BASE_ALIGNMENT 256
881 #define MQD_FWWORKAREA_SIZE       484
882 #define MQD_FWWORKAREA_ALIGNMENT  256
883 
884 static int gfx_v11_0_get_gfx_shadow_info(struct amdgpu_device *adev,
885 					 struct amdgpu_gfx_shadow_info *shadow_info)
886 {
887 	if (adev->gfx.cp_gfx_shadow) {
888 		shadow_info->shadow_size = MQD_SHADOW_BASE_SIZE;
889 		shadow_info->shadow_alignment = MQD_SHADOW_BASE_ALIGNMENT;
890 		shadow_info->csa_size = MQD_FWWORKAREA_SIZE;
891 		shadow_info->csa_alignment = MQD_FWWORKAREA_ALIGNMENT;
892 		return 0;
893 	} else {
894 		memset(shadow_info, 0, sizeof(struct amdgpu_gfx_shadow_info));
895 		return -ENOTSUPP;
896 	}
897 }
898 
899 static const struct amdgpu_gfx_funcs gfx_v11_0_gfx_funcs = {
900 	.get_gpu_clock_counter = &gfx_v11_0_get_gpu_clock_counter,
901 	.select_se_sh = &gfx_v11_0_select_se_sh,
902 	.read_wave_data = &gfx_v11_0_read_wave_data,
903 	.read_wave_sgprs = &gfx_v11_0_read_wave_sgprs,
904 	.read_wave_vgprs = &gfx_v11_0_read_wave_vgprs,
905 	.select_me_pipe_q = &gfx_v11_0_select_me_pipe_q,
906 	.update_perfmon_mgcg = &gfx_v11_0_update_perf_clk,
907 	.get_gfx_shadow_info = &gfx_v11_0_get_gfx_shadow_info,
908 };
909 
910 static int gfx_v11_0_gpu_early_init(struct amdgpu_device *adev)
911 {
912 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
913 	case IP_VERSION(11, 0, 0):
914 	case IP_VERSION(11, 0, 2):
915 		adev->gfx.config.max_hw_contexts = 8;
916 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
917 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
918 		adev->gfx.config.sc_hiz_tile_fifo_size = 0;
919 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
920 		break;
921 	case IP_VERSION(11, 0, 3):
922 		adev->gfx.ras = &gfx_v11_0_3_ras;
923 		adev->gfx.config.max_hw_contexts = 8;
924 		adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
925 		adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
926 		adev->gfx.config.sc_hiz_tile_fifo_size = 0;
927 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
928 		break;
929 	case IP_VERSION(11, 0, 1):
930 	case IP_VERSION(11, 0, 4):
931 	case IP_VERSION(11, 5, 0):
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 = 0x80;
936 		adev->gfx.config.sc_earlyz_tile_fifo_size = 0x300;
937 		break;
938 	default:
939 		BUG();
940 		break;
941 	}
942 
943 	return 0;
944 }
945 
946 static int gfx_v11_0_gfx_ring_init(struct amdgpu_device *adev, int ring_id,
947 				   int me, int pipe, int queue)
948 {
949 	int r;
950 	struct amdgpu_ring *ring;
951 	unsigned int irq_type;
952 
953 	ring = &adev->gfx.gfx_ring[ring_id];
954 
955 	ring->me = me;
956 	ring->pipe = pipe;
957 	ring->queue = queue;
958 
959 	ring->ring_obj = NULL;
960 	ring->use_doorbell = true;
961 
962 	if (!ring_id)
963 		ring->doorbell_index = adev->doorbell_index.gfx_ring0 << 1;
964 	else
965 		ring->doorbell_index = adev->doorbell_index.gfx_ring1 << 1;
966 	ring->vm_hub = AMDGPU_GFXHUB(0);
967 	sprintf(ring->name, "gfx_%d.%d.%d", ring->me, ring->pipe, ring->queue);
968 
969 	irq_type = AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP + ring->pipe;
970 	r = amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq, irq_type,
971 			     AMDGPU_RING_PRIO_DEFAULT, NULL);
972 	if (r)
973 		return r;
974 	return 0;
975 }
976 
977 static int gfx_v11_0_compute_ring_init(struct amdgpu_device *adev, int ring_id,
978 				       int mec, int pipe, int queue)
979 {
980 	int r;
981 	unsigned irq_type;
982 	struct amdgpu_ring *ring;
983 	unsigned int hw_prio;
984 
985 	ring = &adev->gfx.compute_ring[ring_id];
986 
987 	/* mec0 is me1 */
988 	ring->me = mec + 1;
989 	ring->pipe = pipe;
990 	ring->queue = queue;
991 
992 	ring->ring_obj = NULL;
993 	ring->use_doorbell = true;
994 	ring->doorbell_index = (adev->doorbell_index.mec_ring0 + ring_id) << 1;
995 	ring->eop_gpu_addr = adev->gfx.mec.hpd_eop_gpu_addr
996 				+ (ring_id * GFX11_MEC_HPD_SIZE);
997 	ring->vm_hub = AMDGPU_GFXHUB(0);
998 	sprintf(ring->name, "comp_%d.%d.%d", ring->me, ring->pipe, ring->queue);
999 
1000 	irq_type = AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP
1001 		+ ((ring->me - 1) * adev->gfx.mec.num_pipe_per_mec)
1002 		+ ring->pipe;
1003 	hw_prio = amdgpu_gfx_is_high_priority_compute_queue(adev, ring) ?
1004 			AMDGPU_GFX_PIPE_PRIO_HIGH : AMDGPU_GFX_PIPE_PRIO_NORMAL;
1005 	/* type-2 packets are deprecated on MEC, use type-3 instead */
1006 	r = amdgpu_ring_init(adev, ring, 1024, &adev->gfx.eop_irq, irq_type,
1007 			     hw_prio, NULL);
1008 	if (r)
1009 		return r;
1010 
1011 	return 0;
1012 }
1013 
1014 static struct {
1015 	SOC21_FIRMWARE_ID	id;
1016 	unsigned int		offset;
1017 	unsigned int		size;
1018 } rlc_autoload_info[SOC21_FIRMWARE_ID_MAX];
1019 
1020 static void gfx_v11_0_parse_rlc_toc(struct amdgpu_device *adev, void *rlc_toc)
1021 {
1022 	RLC_TABLE_OF_CONTENT *ucode = rlc_toc;
1023 
1024 	while (ucode && (ucode->id > SOC21_FIRMWARE_ID_INVALID) &&
1025 			(ucode->id < SOC21_FIRMWARE_ID_MAX)) {
1026 		rlc_autoload_info[ucode->id].id = ucode->id;
1027 		rlc_autoload_info[ucode->id].offset = ucode->offset * 4;
1028 		rlc_autoload_info[ucode->id].size = ucode->size * 4;
1029 
1030 		ucode++;
1031 	}
1032 }
1033 
1034 static uint32_t gfx_v11_0_calc_toc_total_size(struct amdgpu_device *adev)
1035 {
1036 	uint32_t total_size = 0;
1037 	SOC21_FIRMWARE_ID id;
1038 
1039 	gfx_v11_0_parse_rlc_toc(adev, adev->psp.toc.start_addr);
1040 
1041 	for (id = SOC21_FIRMWARE_ID_RLC_G_UCODE; id < SOC21_FIRMWARE_ID_MAX; id++)
1042 		total_size += rlc_autoload_info[id].size;
1043 
1044 	/* In case the offset in rlc toc ucode is aligned */
1045 	if (total_size < rlc_autoload_info[SOC21_FIRMWARE_ID_MAX-1].offset)
1046 		total_size = rlc_autoload_info[SOC21_FIRMWARE_ID_MAX-1].offset +
1047 			rlc_autoload_info[SOC21_FIRMWARE_ID_MAX-1].size;
1048 
1049 	return total_size;
1050 }
1051 
1052 static int gfx_v11_0_rlc_autoload_buffer_init(struct amdgpu_device *adev)
1053 {
1054 	int r;
1055 	uint32_t total_size;
1056 
1057 	total_size = gfx_v11_0_calc_toc_total_size(adev);
1058 
1059 	r = amdgpu_bo_create_reserved(adev, total_size, 64 * 1024,
1060 				      AMDGPU_GEM_DOMAIN_VRAM |
1061 				      AMDGPU_GEM_DOMAIN_GTT,
1062 				      &adev->gfx.rlc.rlc_autoload_bo,
1063 				      &adev->gfx.rlc.rlc_autoload_gpu_addr,
1064 				      (void **)&adev->gfx.rlc.rlc_autoload_ptr);
1065 
1066 	if (r) {
1067 		dev_err(adev->dev, "(%d) failed to create fw autoload bo\n", r);
1068 		return r;
1069 	}
1070 
1071 	return 0;
1072 }
1073 
1074 static void gfx_v11_0_rlc_backdoor_autoload_copy_ucode(struct amdgpu_device *adev,
1075 					      SOC21_FIRMWARE_ID id,
1076 			    		      const void *fw_data,
1077 					      uint32_t fw_size,
1078 					      uint32_t *fw_autoload_mask)
1079 {
1080 	uint32_t toc_offset;
1081 	uint32_t toc_fw_size;
1082 	char *ptr = adev->gfx.rlc.rlc_autoload_ptr;
1083 
1084 	if (id <= SOC21_FIRMWARE_ID_INVALID || id >= SOC21_FIRMWARE_ID_MAX)
1085 		return;
1086 
1087 	toc_offset = rlc_autoload_info[id].offset;
1088 	toc_fw_size = rlc_autoload_info[id].size;
1089 
1090 	if (fw_size == 0)
1091 		fw_size = toc_fw_size;
1092 
1093 	if (fw_size > toc_fw_size)
1094 		fw_size = toc_fw_size;
1095 
1096 	memcpy(ptr + toc_offset, fw_data, fw_size);
1097 
1098 	if (fw_size < toc_fw_size)
1099 		memset(ptr + toc_offset + fw_size, 0, toc_fw_size - fw_size);
1100 
1101 	if ((id != SOC21_FIRMWARE_ID_RS64_PFP) && (id != SOC21_FIRMWARE_ID_RS64_ME))
1102 		*(uint64_t *)fw_autoload_mask |= 1ULL << id;
1103 }
1104 
1105 static void gfx_v11_0_rlc_backdoor_autoload_copy_toc_ucode(struct amdgpu_device *adev,
1106 							uint32_t *fw_autoload_mask)
1107 {
1108 	void *data;
1109 	uint32_t size;
1110 	uint64_t *toc_ptr;
1111 
1112 	*(uint64_t *)fw_autoload_mask |= 0x1;
1113 
1114 	DRM_DEBUG("rlc autoload enabled fw: 0x%llx\n", *(uint64_t *)fw_autoload_mask);
1115 
1116 	data = adev->psp.toc.start_addr;
1117 	size = rlc_autoload_info[SOC21_FIRMWARE_ID_RLC_TOC].size;
1118 
1119 	toc_ptr = (uint64_t *)data + size / 8 - 1;
1120 	*toc_ptr = *(uint64_t *)fw_autoload_mask;
1121 
1122 	gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RLC_TOC,
1123 					data, size, fw_autoload_mask);
1124 }
1125 
1126 static void gfx_v11_0_rlc_backdoor_autoload_copy_gfx_ucode(struct amdgpu_device *adev,
1127 							uint32_t *fw_autoload_mask)
1128 {
1129 	const __le32 *fw_data;
1130 	uint32_t fw_size;
1131 	const struct gfx_firmware_header_v1_0 *cp_hdr;
1132 	const struct gfx_firmware_header_v2_0 *cpv2_hdr;
1133 	const struct rlc_firmware_header_v2_0 *rlc_hdr;
1134 	const struct rlc_firmware_header_v2_2 *rlcv22_hdr;
1135 	uint16_t version_major, version_minor;
1136 
1137 	if (adev->gfx.rs64_enable) {
1138 		/* pfp ucode */
1139 		cpv2_hdr = (const struct gfx_firmware_header_v2_0 *)
1140 			adev->gfx.pfp_fw->data;
1141 		/* instruction */
1142 		fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
1143 			le32_to_cpu(cpv2_hdr->ucode_offset_bytes));
1144 		fw_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
1145 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_PFP,
1146 						fw_data, fw_size, fw_autoload_mask);
1147 		/* data */
1148 		fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
1149 			le32_to_cpu(cpv2_hdr->data_offset_bytes));
1150 		fw_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1151 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_PFP_P0_STACK,
1152 						fw_data, fw_size, fw_autoload_mask);
1153 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_PFP_P1_STACK,
1154 						fw_data, fw_size, fw_autoload_mask);
1155 		/* me ucode */
1156 		cpv2_hdr = (const struct gfx_firmware_header_v2_0 *)
1157 			adev->gfx.me_fw->data;
1158 		/* instruction */
1159 		fw_data = (const __le32 *)(adev->gfx.me_fw->data +
1160 			le32_to_cpu(cpv2_hdr->ucode_offset_bytes));
1161 		fw_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
1162 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_ME,
1163 						fw_data, fw_size, fw_autoload_mask);
1164 		/* data */
1165 		fw_data = (const __le32 *)(adev->gfx.me_fw->data +
1166 			le32_to_cpu(cpv2_hdr->data_offset_bytes));
1167 		fw_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1168 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_ME_P0_STACK,
1169 						fw_data, fw_size, fw_autoload_mask);
1170 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_ME_P1_STACK,
1171 						fw_data, fw_size, fw_autoload_mask);
1172 		/* mec ucode */
1173 		cpv2_hdr = (const struct gfx_firmware_header_v2_0 *)
1174 			adev->gfx.mec_fw->data;
1175 		/* instruction */
1176 		fw_data = (const __le32 *) (adev->gfx.mec_fw->data +
1177 			le32_to_cpu(cpv2_hdr->ucode_offset_bytes));
1178 		fw_size = le32_to_cpu(cpv2_hdr->ucode_size_bytes);
1179 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_MEC,
1180 						fw_data, fw_size, fw_autoload_mask);
1181 		/* data */
1182 		fw_data = (const __le32 *) (adev->gfx.mec_fw->data +
1183 			le32_to_cpu(cpv2_hdr->data_offset_bytes));
1184 		fw_size = le32_to_cpu(cpv2_hdr->data_size_bytes);
1185 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_MEC_P0_STACK,
1186 						fw_data, fw_size, fw_autoload_mask);
1187 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_MEC_P1_STACK,
1188 						fw_data, fw_size, fw_autoload_mask);
1189 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_MEC_P2_STACK,
1190 						fw_data, fw_size, fw_autoload_mask);
1191 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RS64_MEC_P3_STACK,
1192 						fw_data, fw_size, fw_autoload_mask);
1193 	} else {
1194 		/* pfp ucode */
1195 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1196 			adev->gfx.pfp_fw->data;
1197 		fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
1198 				le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
1199 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1200 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_CP_PFP,
1201 						fw_data, fw_size, fw_autoload_mask);
1202 
1203 		/* me ucode */
1204 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1205 			adev->gfx.me_fw->data;
1206 		fw_data = (const __le32 *)(adev->gfx.me_fw->data +
1207 				le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
1208 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1209 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_CP_ME,
1210 						fw_data, fw_size, fw_autoload_mask);
1211 
1212 		/* mec ucode */
1213 		cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1214 			adev->gfx.mec_fw->data;
1215 		fw_data = (const __le32 *) (adev->gfx.mec_fw->data +
1216 				le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
1217 		fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1218 			cp_hdr->jt_size * 4;
1219 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_CP_MEC,
1220 						fw_data, fw_size, fw_autoload_mask);
1221 	}
1222 
1223 	/* rlc ucode */
1224 	rlc_hdr = (const struct rlc_firmware_header_v2_0 *)
1225 		adev->gfx.rlc_fw->data;
1226 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1227 			le32_to_cpu(rlc_hdr->header.ucode_array_offset_bytes));
1228 	fw_size = le32_to_cpu(rlc_hdr->header.ucode_size_bytes);
1229 	gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RLC_G_UCODE,
1230 					fw_data, fw_size, fw_autoload_mask);
1231 
1232 	version_major = le16_to_cpu(rlc_hdr->header.header_version_major);
1233 	version_minor = le16_to_cpu(rlc_hdr->header.header_version_minor);
1234 	if (version_major == 2) {
1235 		if (version_minor >= 2) {
1236 			rlcv22_hdr = (const struct rlc_firmware_header_v2_2 *)adev->gfx.rlc_fw->data;
1237 
1238 			fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1239 					le32_to_cpu(rlcv22_hdr->rlc_iram_ucode_offset_bytes));
1240 			fw_size = le32_to_cpu(rlcv22_hdr->rlc_iram_ucode_size_bytes);
1241 			gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RLX6_UCODE,
1242 					fw_data, fw_size, fw_autoload_mask);
1243 
1244 			fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1245 					le32_to_cpu(rlcv22_hdr->rlc_dram_ucode_offset_bytes));
1246 			fw_size = le32_to_cpu(rlcv22_hdr->rlc_dram_ucode_size_bytes);
1247 			gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev, SOC21_FIRMWARE_ID_RLX6_DRAM_BOOT,
1248 					fw_data, fw_size, fw_autoload_mask);
1249 		}
1250 	}
1251 }
1252 
1253 static void gfx_v11_0_rlc_backdoor_autoload_copy_sdma_ucode(struct amdgpu_device *adev,
1254 							uint32_t *fw_autoload_mask)
1255 {
1256 	const __le32 *fw_data;
1257 	uint32_t fw_size;
1258 	const struct sdma_firmware_header_v2_0 *sdma_hdr;
1259 
1260 	sdma_hdr = (const struct sdma_firmware_header_v2_0 *)
1261 		adev->sdma.instance[0].fw->data;
1262 	fw_data = (const __le32 *) (adev->sdma.instance[0].fw->data +
1263 			le32_to_cpu(sdma_hdr->header.ucode_array_offset_bytes));
1264 	fw_size = le32_to_cpu(sdma_hdr->ctx_ucode_size_bytes);
1265 
1266 	gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev,
1267 			SOC21_FIRMWARE_ID_SDMA_UCODE_TH0, fw_data, fw_size, fw_autoload_mask);
1268 
1269 	fw_data = (const __le32 *) (adev->sdma.instance[0].fw->data +
1270 			le32_to_cpu(sdma_hdr->ctl_ucode_offset));
1271 	fw_size = le32_to_cpu(sdma_hdr->ctl_ucode_size_bytes);
1272 
1273 	gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev,
1274 			SOC21_FIRMWARE_ID_SDMA_UCODE_TH1, fw_data, fw_size, fw_autoload_mask);
1275 }
1276 
1277 static void gfx_v11_0_rlc_backdoor_autoload_copy_mes_ucode(struct amdgpu_device *adev,
1278 							uint32_t *fw_autoload_mask)
1279 {
1280 	const __le32 *fw_data;
1281 	unsigned fw_size;
1282 	const struct mes_firmware_header_v1_0 *mes_hdr;
1283 	int pipe, ucode_id, data_id;
1284 
1285 	for (pipe = 0; pipe < 2; pipe++) {
1286 		if (pipe==0) {
1287 			ucode_id = SOC21_FIRMWARE_ID_RS64_MES_P0;
1288 			data_id  = SOC21_FIRMWARE_ID_RS64_MES_P0_STACK;
1289 		} else {
1290 			ucode_id = SOC21_FIRMWARE_ID_RS64_MES_P1;
1291 			data_id  = SOC21_FIRMWARE_ID_RS64_MES_P1_STACK;
1292 		}
1293 
1294 		mes_hdr = (const struct mes_firmware_header_v1_0 *)
1295 			adev->mes.fw[pipe]->data;
1296 
1297 		fw_data = (const __le32 *)(adev->mes.fw[pipe]->data +
1298 				le32_to_cpu(mes_hdr->mes_ucode_offset_bytes));
1299 		fw_size = le32_to_cpu(mes_hdr->mes_ucode_size_bytes);
1300 
1301 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev,
1302 				ucode_id, fw_data, fw_size, fw_autoload_mask);
1303 
1304 		fw_data = (const __le32 *)(adev->mes.fw[pipe]->data +
1305 				le32_to_cpu(mes_hdr->mes_ucode_data_offset_bytes));
1306 		fw_size = le32_to_cpu(mes_hdr->mes_ucode_data_size_bytes);
1307 
1308 		gfx_v11_0_rlc_backdoor_autoload_copy_ucode(adev,
1309 				data_id, fw_data, fw_size, fw_autoload_mask);
1310 	}
1311 }
1312 
1313 static int gfx_v11_0_rlc_backdoor_autoload_enable(struct amdgpu_device *adev)
1314 {
1315 	uint32_t rlc_g_offset, rlc_g_size;
1316 	uint64_t gpu_addr;
1317 	uint32_t autoload_fw_id[2];
1318 
1319 	memset(autoload_fw_id, 0, sizeof(uint32_t) * 2);
1320 
1321 	/* RLC autoload sequence 2: copy ucode */
1322 	gfx_v11_0_rlc_backdoor_autoload_copy_sdma_ucode(adev, autoload_fw_id);
1323 	gfx_v11_0_rlc_backdoor_autoload_copy_gfx_ucode(adev, autoload_fw_id);
1324 	gfx_v11_0_rlc_backdoor_autoload_copy_mes_ucode(adev, autoload_fw_id);
1325 	gfx_v11_0_rlc_backdoor_autoload_copy_toc_ucode(adev, autoload_fw_id);
1326 
1327 	rlc_g_offset = rlc_autoload_info[SOC21_FIRMWARE_ID_RLC_G_UCODE].offset;
1328 	rlc_g_size = rlc_autoload_info[SOC21_FIRMWARE_ID_RLC_G_UCODE].size;
1329 	gpu_addr = adev->gfx.rlc.rlc_autoload_gpu_addr + rlc_g_offset;
1330 
1331 	WREG32_SOC15(GC, 0, regGFX_IMU_RLC_BOOTLOADER_ADDR_HI, upper_32_bits(gpu_addr));
1332 	WREG32_SOC15(GC, 0, regGFX_IMU_RLC_BOOTLOADER_ADDR_LO, lower_32_bits(gpu_addr));
1333 
1334 	WREG32_SOC15(GC, 0, regGFX_IMU_RLC_BOOTLOADER_SIZE, rlc_g_size);
1335 
1336 	/* RLC autoload sequence 3: load IMU fw */
1337 	if (adev->gfx.imu.funcs->load_microcode)
1338 		adev->gfx.imu.funcs->load_microcode(adev);
1339 	/* RLC autoload sequence 4 init IMU fw */
1340 	if (adev->gfx.imu.funcs->setup_imu)
1341 		adev->gfx.imu.funcs->setup_imu(adev);
1342 	if (adev->gfx.imu.funcs->start_imu)
1343 		adev->gfx.imu.funcs->start_imu(adev);
1344 
1345 	/* RLC autoload sequence 5 disable gpa mode */
1346 	gfx_v11_0_disable_gpa_mode(adev);
1347 
1348 	return 0;
1349 }
1350 
1351 static int gfx_v11_0_sw_init(void *handle)
1352 {
1353 	int i, j, k, r, ring_id = 0;
1354 	struct amdgpu_kiq *kiq;
1355 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1356 
1357 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
1358 	case IP_VERSION(11, 0, 0):
1359 	case IP_VERSION(11, 0, 2):
1360 	case IP_VERSION(11, 0, 3):
1361 		adev->gfx.me.num_me = 1;
1362 		adev->gfx.me.num_pipe_per_me = 1;
1363 		adev->gfx.me.num_queue_per_pipe = 1;
1364 		adev->gfx.mec.num_mec = 2;
1365 		adev->gfx.mec.num_pipe_per_mec = 4;
1366 		adev->gfx.mec.num_queue_per_pipe = 4;
1367 		break;
1368 	case IP_VERSION(11, 0, 1):
1369 	case IP_VERSION(11, 0, 4):
1370 	case IP_VERSION(11, 5, 0):
1371 		adev->gfx.me.num_me = 1;
1372 		adev->gfx.me.num_pipe_per_me = 1;
1373 		adev->gfx.me.num_queue_per_pipe = 1;
1374 		adev->gfx.mec.num_mec = 1;
1375 		adev->gfx.mec.num_pipe_per_mec = 4;
1376 		adev->gfx.mec.num_queue_per_pipe = 4;
1377 		break;
1378 	default:
1379 		adev->gfx.me.num_me = 1;
1380 		adev->gfx.me.num_pipe_per_me = 1;
1381 		adev->gfx.me.num_queue_per_pipe = 1;
1382 		adev->gfx.mec.num_mec = 1;
1383 		adev->gfx.mec.num_pipe_per_mec = 4;
1384 		adev->gfx.mec.num_queue_per_pipe = 8;
1385 		break;
1386 	}
1387 
1388 	/* Enable CG flag in one VF mode for enabling RLC safe mode enter/exit */
1389 	if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 0, 3) &&
1390 	    amdgpu_sriov_is_pp_one_vf(adev))
1391 		adev->cg_flags = AMD_CG_SUPPORT_GFX_CGCG;
1392 
1393 	/* EOP Event */
1394 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_GRBM_CP,
1395 			      GFX_11_0_0__SRCID__CP_EOP_INTERRUPT,
1396 			      &adev->gfx.eop_irq);
1397 	if (r)
1398 		return r;
1399 
1400 	/* Privileged reg */
1401 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_GRBM_CP,
1402 			      GFX_11_0_0__SRCID__CP_PRIV_REG_FAULT,
1403 			      &adev->gfx.priv_reg_irq);
1404 	if (r)
1405 		return r;
1406 
1407 	/* Privileged inst */
1408 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_GRBM_CP,
1409 			      GFX_11_0_0__SRCID__CP_PRIV_INSTR_FAULT,
1410 			      &adev->gfx.priv_inst_irq);
1411 	if (r)
1412 		return r;
1413 
1414 	/* FED error */
1415 	r = amdgpu_irq_add_id(adev, SOC21_IH_CLIENTID_GFX,
1416 				  GFX_11_0_0__SRCID__RLC_GC_FED_INTERRUPT,
1417 				  &adev->gfx.rlc_gc_fed_irq);
1418 	if (r)
1419 		return r;
1420 
1421 	adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
1422 
1423 	gfx_v11_0_me_init(adev);
1424 
1425 	r = gfx_v11_0_rlc_init(adev);
1426 	if (r) {
1427 		DRM_ERROR("Failed to init rlc BOs!\n");
1428 		return r;
1429 	}
1430 
1431 	r = gfx_v11_0_mec_init(adev);
1432 	if (r) {
1433 		DRM_ERROR("Failed to init MEC BOs!\n");
1434 		return r;
1435 	}
1436 
1437 	/* set up the gfx ring */
1438 	for (i = 0; i < adev->gfx.me.num_me; i++) {
1439 		for (j = 0; j < adev->gfx.me.num_queue_per_pipe; j++) {
1440 			for (k = 0; k < adev->gfx.me.num_pipe_per_me; k++) {
1441 				if (!amdgpu_gfx_is_me_queue_enabled(adev, i, k, j))
1442 					continue;
1443 
1444 				r = gfx_v11_0_gfx_ring_init(adev, ring_id,
1445 							    i, k, j);
1446 				if (r)
1447 					return r;
1448 				ring_id++;
1449 			}
1450 		}
1451 	}
1452 
1453 	ring_id = 0;
1454 	/* set up the compute queues - allocate horizontally across pipes */
1455 	for (i = 0; i < adev->gfx.mec.num_mec; ++i) {
1456 		for (j = 0; j < adev->gfx.mec.num_queue_per_pipe; j++) {
1457 			for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
1458 				if (!amdgpu_gfx_is_mec_queue_enabled(adev, 0, i,
1459 								     k, j))
1460 					continue;
1461 
1462 				r = gfx_v11_0_compute_ring_init(adev, ring_id,
1463 								i, k, j);
1464 				if (r)
1465 					return r;
1466 
1467 				ring_id++;
1468 			}
1469 		}
1470 	}
1471 
1472 	if (!adev->enable_mes_kiq) {
1473 		r = amdgpu_gfx_kiq_init(adev, GFX11_MEC_HPD_SIZE, 0);
1474 		if (r) {
1475 			DRM_ERROR("Failed to init KIQ BOs!\n");
1476 			return r;
1477 		}
1478 
1479 		kiq = &adev->gfx.kiq[0];
1480 		r = amdgpu_gfx_kiq_init_ring(adev, &kiq->ring, &kiq->irq, 0);
1481 		if (r)
1482 			return r;
1483 	}
1484 
1485 	r = amdgpu_gfx_mqd_sw_init(adev, sizeof(struct v11_compute_mqd), 0);
1486 	if (r)
1487 		return r;
1488 
1489 	/* allocate visible FB for rlc auto-loading fw */
1490 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
1491 		r = gfx_v11_0_rlc_autoload_buffer_init(adev);
1492 		if (r)
1493 			return r;
1494 	}
1495 
1496 	r = gfx_v11_0_gpu_early_init(adev);
1497 	if (r)
1498 		return r;
1499 
1500 	if (amdgpu_gfx_ras_sw_init(adev)) {
1501 		dev_err(adev->dev, "Failed to initialize gfx ras block!\n");
1502 		return -EINVAL;
1503 	}
1504 
1505 	return 0;
1506 }
1507 
1508 static void gfx_v11_0_pfp_fini(struct amdgpu_device *adev)
1509 {
1510 	amdgpu_bo_free_kernel(&adev->gfx.pfp.pfp_fw_obj,
1511 			      &adev->gfx.pfp.pfp_fw_gpu_addr,
1512 			      (void **)&adev->gfx.pfp.pfp_fw_ptr);
1513 
1514 	amdgpu_bo_free_kernel(&adev->gfx.pfp.pfp_fw_data_obj,
1515 			      &adev->gfx.pfp.pfp_fw_data_gpu_addr,
1516 			      (void **)&adev->gfx.pfp.pfp_fw_data_ptr);
1517 }
1518 
1519 static void gfx_v11_0_me_fini(struct amdgpu_device *adev)
1520 {
1521 	amdgpu_bo_free_kernel(&adev->gfx.me.me_fw_obj,
1522 			      &adev->gfx.me.me_fw_gpu_addr,
1523 			      (void **)&adev->gfx.me.me_fw_ptr);
1524 
1525 	amdgpu_bo_free_kernel(&adev->gfx.me.me_fw_data_obj,
1526 			       &adev->gfx.me.me_fw_data_gpu_addr,
1527 			       (void **)&adev->gfx.me.me_fw_data_ptr);
1528 }
1529 
1530 static void gfx_v11_0_rlc_autoload_buffer_fini(struct amdgpu_device *adev)
1531 {
1532 	amdgpu_bo_free_kernel(&adev->gfx.rlc.rlc_autoload_bo,
1533 			&adev->gfx.rlc.rlc_autoload_gpu_addr,
1534 			(void **)&adev->gfx.rlc.rlc_autoload_ptr);
1535 }
1536 
1537 static int gfx_v11_0_sw_fini(void *handle)
1538 {
1539 	int i;
1540 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1541 
1542 	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
1543 		amdgpu_ring_fini(&adev->gfx.gfx_ring[i]);
1544 	for (i = 0; i < adev->gfx.num_compute_rings; i++)
1545 		amdgpu_ring_fini(&adev->gfx.compute_ring[i]);
1546 
1547 	amdgpu_gfx_mqd_sw_fini(adev, 0);
1548 
1549 	if (!adev->enable_mes_kiq) {
1550 		amdgpu_gfx_kiq_free_ring(&adev->gfx.kiq[0].ring);
1551 		amdgpu_gfx_kiq_fini(adev, 0);
1552 	}
1553 
1554 	gfx_v11_0_pfp_fini(adev);
1555 	gfx_v11_0_me_fini(adev);
1556 	gfx_v11_0_rlc_fini(adev);
1557 	gfx_v11_0_mec_fini(adev);
1558 
1559 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO)
1560 		gfx_v11_0_rlc_autoload_buffer_fini(adev);
1561 
1562 	gfx_v11_0_free_microcode(adev);
1563 
1564 	return 0;
1565 }
1566 
1567 static void gfx_v11_0_select_se_sh(struct amdgpu_device *adev, u32 se_num,
1568 				   u32 sh_num, u32 instance, int xcc_id)
1569 {
1570 	u32 data;
1571 
1572 	if (instance == 0xffffffff)
1573 		data = REG_SET_FIELD(0, GRBM_GFX_INDEX,
1574 				     INSTANCE_BROADCAST_WRITES, 1);
1575 	else
1576 		data = REG_SET_FIELD(0, GRBM_GFX_INDEX, INSTANCE_INDEX,
1577 				     instance);
1578 
1579 	if (se_num == 0xffffffff)
1580 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_BROADCAST_WRITES,
1581 				     1);
1582 	else
1583 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_INDEX, se_num);
1584 
1585 	if (sh_num == 0xffffffff)
1586 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SA_BROADCAST_WRITES,
1587 				     1);
1588 	else
1589 		data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SA_INDEX, sh_num);
1590 
1591 	WREG32_SOC15(GC, 0, regGRBM_GFX_INDEX, data);
1592 }
1593 
1594 static u32 gfx_v11_0_get_sa_active_bitmap(struct amdgpu_device *adev)
1595 {
1596 	u32 gc_disabled_sa_mask, gc_user_disabled_sa_mask, sa_mask;
1597 
1598 	gc_disabled_sa_mask = RREG32_SOC15(GC, 0, regCC_GC_SA_UNIT_DISABLE);
1599 	gc_disabled_sa_mask = REG_GET_FIELD(gc_disabled_sa_mask,
1600 					   CC_GC_SA_UNIT_DISABLE,
1601 					   SA_DISABLE);
1602 	gc_user_disabled_sa_mask = RREG32_SOC15(GC, 0, regGC_USER_SA_UNIT_DISABLE);
1603 	gc_user_disabled_sa_mask = REG_GET_FIELD(gc_user_disabled_sa_mask,
1604 						 GC_USER_SA_UNIT_DISABLE,
1605 						 SA_DISABLE);
1606 	sa_mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_sh_per_se *
1607 					    adev->gfx.config.max_shader_engines);
1608 
1609 	return sa_mask & (~(gc_disabled_sa_mask | gc_user_disabled_sa_mask));
1610 }
1611 
1612 static u32 gfx_v11_0_get_rb_active_bitmap(struct amdgpu_device *adev)
1613 {
1614 	u32 gc_disabled_rb_mask, gc_user_disabled_rb_mask;
1615 	u32 rb_mask;
1616 
1617 	gc_disabled_rb_mask = RREG32_SOC15(GC, 0, regCC_RB_BACKEND_DISABLE);
1618 	gc_disabled_rb_mask = REG_GET_FIELD(gc_disabled_rb_mask,
1619 					    CC_RB_BACKEND_DISABLE,
1620 					    BACKEND_DISABLE);
1621 	gc_user_disabled_rb_mask = RREG32_SOC15(GC, 0, regGC_USER_RB_BACKEND_DISABLE);
1622 	gc_user_disabled_rb_mask = REG_GET_FIELD(gc_user_disabled_rb_mask,
1623 						 GC_USER_RB_BACKEND_DISABLE,
1624 						 BACKEND_DISABLE);
1625 	rb_mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_backends_per_se *
1626 					    adev->gfx.config.max_shader_engines);
1627 
1628 	return rb_mask & (~(gc_disabled_rb_mask | gc_user_disabled_rb_mask));
1629 }
1630 
1631 static void gfx_v11_0_setup_rb(struct amdgpu_device *adev)
1632 {
1633 	u32 rb_bitmap_width_per_sa;
1634 	u32 max_sa;
1635 	u32 active_sa_bitmap;
1636 	u32 global_active_rb_bitmap;
1637 	u32 active_rb_bitmap = 0;
1638 	u32 i;
1639 
1640 	/* query sa bitmap from SA_UNIT_DISABLE registers */
1641 	active_sa_bitmap = gfx_v11_0_get_sa_active_bitmap(adev);
1642 	/* query rb bitmap from RB_BACKEND_DISABLE registers */
1643 	global_active_rb_bitmap = gfx_v11_0_get_rb_active_bitmap(adev);
1644 
1645 	/* generate active rb bitmap according to active sa bitmap */
1646 	max_sa = adev->gfx.config.max_shader_engines *
1647 		 adev->gfx.config.max_sh_per_se;
1648 	rb_bitmap_width_per_sa = adev->gfx.config.max_backends_per_se /
1649 				 adev->gfx.config.max_sh_per_se;
1650 	for (i = 0; i < max_sa; i++) {
1651 		if (active_sa_bitmap & (1 << i))
1652 			active_rb_bitmap |= (0x3 << (i * rb_bitmap_width_per_sa));
1653 	}
1654 
1655 	active_rb_bitmap |= global_active_rb_bitmap;
1656 	adev->gfx.config.backend_enable_mask = active_rb_bitmap;
1657 	adev->gfx.config.num_rbs = hweight32(active_rb_bitmap);
1658 }
1659 
1660 #define DEFAULT_SH_MEM_BASES	(0x6000)
1661 #define LDS_APP_BASE           0x1
1662 #define SCRATCH_APP_BASE       0x2
1663 
1664 static void gfx_v11_0_init_compute_vmid(struct amdgpu_device *adev)
1665 {
1666 	int i;
1667 	uint32_t sh_mem_bases;
1668 	uint32_t data;
1669 
1670 	/*
1671 	 * Configure apertures:
1672 	 * LDS:         0x60000000'00000000 - 0x60000001'00000000 (4GB)
1673 	 * Scratch:     0x60000001'00000000 - 0x60000002'00000000 (4GB)
1674 	 * GPUVM:       0x60010000'00000000 - 0x60020000'00000000 (1TB)
1675 	 */
1676 	sh_mem_bases = (LDS_APP_BASE << SH_MEM_BASES__SHARED_BASE__SHIFT) |
1677 			SCRATCH_APP_BASE;
1678 
1679 	mutex_lock(&adev->srbm_mutex);
1680 	for (i = adev->vm_manager.first_kfd_vmid; i < AMDGPU_NUM_VMID; i++) {
1681 		soc21_grbm_select(adev, 0, 0, 0, i);
1682 		/* CP and shaders */
1683 		WREG32_SOC15(GC, 0, regSH_MEM_CONFIG, DEFAULT_SH_MEM_CONFIG);
1684 		WREG32_SOC15(GC, 0, regSH_MEM_BASES, sh_mem_bases);
1685 
1686 		/* Enable trap for each kfd vmid. */
1687 		data = RREG32_SOC15(GC, 0, regSPI_GDBG_PER_VMID_CNTL);
1688 		data = REG_SET_FIELD(data, SPI_GDBG_PER_VMID_CNTL, TRAP_EN, 1);
1689 		WREG32_SOC15(GC, 0, regSPI_GDBG_PER_VMID_CNTL, data);
1690 	}
1691 	soc21_grbm_select(adev, 0, 0, 0, 0);
1692 	mutex_unlock(&adev->srbm_mutex);
1693 
1694 	/* Initialize all compute VMIDs to have no GDS, GWS, or OA
1695 	   acccess. These should be enabled by FW for target VMIDs. */
1696 	for (i = adev->vm_manager.first_kfd_vmid; i < AMDGPU_NUM_VMID; i++) {
1697 		WREG32_SOC15_OFFSET(GC, 0, regGDS_VMID0_BASE, 2 * i, 0);
1698 		WREG32_SOC15_OFFSET(GC, 0, regGDS_VMID0_SIZE, 2 * i, 0);
1699 		WREG32_SOC15_OFFSET(GC, 0, regGDS_GWS_VMID0, i, 0);
1700 		WREG32_SOC15_OFFSET(GC, 0, regGDS_OA_VMID0, i, 0);
1701 	}
1702 }
1703 
1704 static void gfx_v11_0_init_gds_vmid(struct amdgpu_device *adev)
1705 {
1706 	int vmid;
1707 
1708 	/*
1709 	 * Initialize all compute and user-gfx VMIDs to have no GDS, GWS, or OA
1710 	 * access. Compute VMIDs should be enabled by FW for target VMIDs,
1711 	 * the driver can enable them for graphics. VMID0 should maintain
1712 	 * access so that HWS firmware can save/restore entries.
1713 	 */
1714 	for (vmid = 1; vmid < 16; vmid++) {
1715 		WREG32_SOC15_OFFSET(GC, 0, regGDS_VMID0_BASE, 2 * vmid, 0);
1716 		WREG32_SOC15_OFFSET(GC, 0, regGDS_VMID0_SIZE, 2 * vmid, 0);
1717 		WREG32_SOC15_OFFSET(GC, 0, regGDS_GWS_VMID0, vmid, 0);
1718 		WREG32_SOC15_OFFSET(GC, 0, regGDS_OA_VMID0, vmid, 0);
1719 	}
1720 }
1721 
1722 static void gfx_v11_0_tcp_harvest(struct amdgpu_device *adev)
1723 {
1724 	/* TODO: harvest feature to be added later. */
1725 }
1726 
1727 static void gfx_v11_0_get_tcc_info(struct amdgpu_device *adev)
1728 {
1729 	/* TCCs are global (not instanced). */
1730 	uint32_t tcc_disable = RREG32_SOC15(GC, 0, regCGTS_TCC_DISABLE) |
1731 			       RREG32_SOC15(GC, 0, regCGTS_USER_TCC_DISABLE);
1732 
1733 	adev->gfx.config.tcc_disabled_mask =
1734 		REG_GET_FIELD(tcc_disable, CGTS_TCC_DISABLE, TCC_DISABLE) |
1735 		(REG_GET_FIELD(tcc_disable, CGTS_TCC_DISABLE, HI_TCC_DISABLE) << 16);
1736 }
1737 
1738 static void gfx_v11_0_constants_init(struct amdgpu_device *adev)
1739 {
1740 	u32 tmp;
1741 	int i;
1742 
1743 	if (!amdgpu_sriov_vf(adev))
1744 		WREG32_FIELD15_PREREG(GC, 0, GRBM_CNTL, READ_TIMEOUT, 0xff);
1745 
1746 	gfx_v11_0_setup_rb(adev);
1747 	gfx_v11_0_get_cu_info(adev, &adev->gfx.cu_info);
1748 	gfx_v11_0_get_tcc_info(adev);
1749 	adev->gfx.config.pa_sc_tile_steering_override = 0;
1750 
1751 	/* Set whether texture coordinate truncation is conformant. */
1752 	tmp = RREG32_SOC15(GC, 0, regTA_CNTL2);
1753 	adev->gfx.config.ta_cntl2_truncate_coord_mode =
1754 		REG_GET_FIELD(tmp, TA_CNTL2, TRUNCATE_COORD_MODE);
1755 
1756 	/* XXX SH_MEM regs */
1757 	/* where to put LDS, scratch, GPUVM in FSA64 space */
1758 	mutex_lock(&adev->srbm_mutex);
1759 	for (i = 0; i < adev->vm_manager.id_mgr[AMDGPU_GFXHUB(0)].num_ids; i++) {
1760 		soc21_grbm_select(adev, 0, 0, 0, i);
1761 		/* CP and shaders */
1762 		WREG32_SOC15(GC, 0, regSH_MEM_CONFIG, DEFAULT_SH_MEM_CONFIG);
1763 		if (i != 0) {
1764 			tmp = REG_SET_FIELD(0, SH_MEM_BASES, PRIVATE_BASE,
1765 				(adev->gmc.private_aperture_start >> 48));
1766 			tmp = REG_SET_FIELD(tmp, SH_MEM_BASES, SHARED_BASE,
1767 				(adev->gmc.shared_aperture_start >> 48));
1768 			WREG32_SOC15(GC, 0, regSH_MEM_BASES, tmp);
1769 		}
1770 	}
1771 	soc21_grbm_select(adev, 0, 0, 0, 0);
1772 
1773 	mutex_unlock(&adev->srbm_mutex);
1774 
1775 	gfx_v11_0_init_compute_vmid(adev);
1776 	gfx_v11_0_init_gds_vmid(adev);
1777 }
1778 
1779 static void gfx_v11_0_enable_gui_idle_interrupt(struct amdgpu_device *adev,
1780 					       bool enable)
1781 {
1782 	u32 tmp;
1783 
1784 	if (amdgpu_sriov_vf(adev))
1785 		return;
1786 
1787 	tmp = RREG32_SOC15(GC, 0, regCP_INT_CNTL_RING0);
1788 
1789 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_BUSY_INT_ENABLE,
1790 			    enable ? 1 : 0);
1791 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_EMPTY_INT_ENABLE,
1792 			    enable ? 1 : 0);
1793 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CMP_BUSY_INT_ENABLE,
1794 			    enable ? 1 : 0);
1795 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, GFX_IDLE_INT_ENABLE,
1796 			    enable ? 1 : 0);
1797 
1798 	WREG32_SOC15(GC, 0, regCP_INT_CNTL_RING0, tmp);
1799 }
1800 
1801 static int gfx_v11_0_init_csb(struct amdgpu_device *adev)
1802 {
1803 	adev->gfx.rlc.funcs->get_csb_buffer(adev, adev->gfx.rlc.cs_ptr);
1804 
1805 	WREG32_SOC15(GC, 0, regRLC_CSIB_ADDR_HI,
1806 			adev->gfx.rlc.clear_state_gpu_addr >> 32);
1807 	WREG32_SOC15(GC, 0, regRLC_CSIB_ADDR_LO,
1808 			adev->gfx.rlc.clear_state_gpu_addr & 0xfffffffc);
1809 	WREG32_SOC15(GC, 0, regRLC_CSIB_LENGTH, adev->gfx.rlc.clear_state_size);
1810 
1811 	return 0;
1812 }
1813 
1814 static void gfx_v11_0_rlc_stop(struct amdgpu_device *adev)
1815 {
1816 	u32 tmp = RREG32_SOC15(GC, 0, regRLC_CNTL);
1817 
1818 	tmp = REG_SET_FIELD(tmp, RLC_CNTL, RLC_ENABLE_F32, 0);
1819 	WREG32_SOC15(GC, 0, regRLC_CNTL, tmp);
1820 }
1821 
1822 static void gfx_v11_0_rlc_reset(struct amdgpu_device *adev)
1823 {
1824 	WREG32_FIELD15_PREREG(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
1825 	udelay(50);
1826 	WREG32_FIELD15_PREREG(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 0);
1827 	udelay(50);
1828 }
1829 
1830 static void gfx_v11_0_rlc_smu_handshake_cntl(struct amdgpu_device *adev,
1831 					     bool enable)
1832 {
1833 	uint32_t rlc_pg_cntl;
1834 
1835 	rlc_pg_cntl = RREG32_SOC15(GC, 0, regRLC_PG_CNTL);
1836 
1837 	if (!enable) {
1838 		/* RLC_PG_CNTL[23] = 0 (default)
1839 		 * RLC will wait for handshake acks with SMU
1840 		 * GFXOFF will be enabled
1841 		 * RLC_PG_CNTL[23] = 1
1842 		 * RLC will not issue any message to SMU
1843 		 * hence no handshake between SMU & RLC
1844 		 * GFXOFF will be disabled
1845 		 */
1846 		rlc_pg_cntl |= RLC_PG_CNTL__SMU_HANDSHAKE_DISABLE_MASK;
1847 	} else
1848 		rlc_pg_cntl &= ~RLC_PG_CNTL__SMU_HANDSHAKE_DISABLE_MASK;
1849 	WREG32_SOC15(GC, 0, regRLC_PG_CNTL, rlc_pg_cntl);
1850 }
1851 
1852 static void gfx_v11_0_rlc_start(struct amdgpu_device *adev)
1853 {
1854 	/* TODO: enable rlc & smu handshake until smu
1855 	 * and gfxoff feature works as expected */
1856 	if (!(amdgpu_pp_feature_mask & PP_GFXOFF_MASK))
1857 		gfx_v11_0_rlc_smu_handshake_cntl(adev, false);
1858 
1859 	WREG32_FIELD15_PREREG(GC, 0, RLC_CNTL, RLC_ENABLE_F32, 1);
1860 	udelay(50);
1861 }
1862 
1863 static void gfx_v11_0_rlc_enable_srm(struct amdgpu_device *adev)
1864 {
1865 	uint32_t tmp;
1866 
1867 	/* enable Save Restore Machine */
1868 	tmp = RREG32(SOC15_REG_OFFSET(GC, 0, regRLC_SRM_CNTL));
1869 	tmp |= RLC_SRM_CNTL__AUTO_INCR_ADDR_MASK;
1870 	tmp |= RLC_SRM_CNTL__SRM_ENABLE_MASK;
1871 	WREG32(SOC15_REG_OFFSET(GC, 0, regRLC_SRM_CNTL), tmp);
1872 }
1873 
1874 static void gfx_v11_0_load_rlcg_microcode(struct amdgpu_device *adev)
1875 {
1876 	const struct rlc_firmware_header_v2_0 *hdr;
1877 	const __le32 *fw_data;
1878 	unsigned i, fw_size;
1879 
1880 	hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
1881 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1882 			   le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1883 	fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
1884 
1885 	WREG32_SOC15(GC, 0, regRLC_GPM_UCODE_ADDR,
1886 		     RLCG_UCODE_LOADING_START_ADDRESS);
1887 
1888 	for (i = 0; i < fw_size; i++)
1889 		WREG32_SOC15(GC, 0, regRLC_GPM_UCODE_DATA,
1890 			     le32_to_cpup(fw_data++));
1891 
1892 	WREG32_SOC15(GC, 0, regRLC_GPM_UCODE_ADDR, adev->gfx.rlc_fw_version);
1893 }
1894 
1895 static void gfx_v11_0_load_rlc_iram_dram_microcode(struct amdgpu_device *adev)
1896 {
1897 	const struct rlc_firmware_header_v2_2 *hdr;
1898 	const __le32 *fw_data;
1899 	unsigned i, fw_size;
1900 	u32 tmp;
1901 
1902 	hdr = (const struct rlc_firmware_header_v2_2 *)adev->gfx.rlc_fw->data;
1903 
1904 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1905 			le32_to_cpu(hdr->rlc_iram_ucode_offset_bytes));
1906 	fw_size = le32_to_cpu(hdr->rlc_iram_ucode_size_bytes) / 4;
1907 
1908 	WREG32_SOC15(GC, 0, regRLC_LX6_IRAM_ADDR, 0);
1909 
1910 	for (i = 0; i < fw_size; i++) {
1911 		if ((amdgpu_emu_mode == 1) && (i % 100 == 99))
1912 			msleep(1);
1913 		WREG32_SOC15(GC, 0, regRLC_LX6_IRAM_DATA,
1914 				le32_to_cpup(fw_data++));
1915 	}
1916 
1917 	WREG32_SOC15(GC, 0, regRLC_LX6_IRAM_ADDR, adev->gfx.rlc_fw_version);
1918 
1919 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1920 			le32_to_cpu(hdr->rlc_dram_ucode_offset_bytes));
1921 	fw_size = le32_to_cpu(hdr->rlc_dram_ucode_size_bytes) / 4;
1922 
1923 	WREG32_SOC15(GC, 0, regRLC_LX6_DRAM_ADDR, 0);
1924 	for (i = 0; i < fw_size; i++) {
1925 		if ((amdgpu_emu_mode == 1) && (i % 100 == 99))
1926 			msleep(1);
1927 		WREG32_SOC15(GC, 0, regRLC_LX6_DRAM_DATA,
1928 				le32_to_cpup(fw_data++));
1929 	}
1930 
1931 	WREG32_SOC15(GC, 0, regRLC_LX6_IRAM_ADDR, adev->gfx.rlc_fw_version);
1932 
1933 	tmp = RREG32_SOC15(GC, 0, regRLC_LX6_CNTL);
1934 	tmp = REG_SET_FIELD(tmp, RLC_LX6_CNTL, PDEBUG_ENABLE, 1);
1935 	tmp = REG_SET_FIELD(tmp, RLC_LX6_CNTL, BRESET, 0);
1936 	WREG32_SOC15(GC, 0, regRLC_LX6_CNTL, tmp);
1937 }
1938 
1939 static void gfx_v11_0_load_rlcp_rlcv_microcode(struct amdgpu_device *adev)
1940 {
1941 	const struct rlc_firmware_header_v2_3 *hdr;
1942 	const __le32 *fw_data;
1943 	unsigned i, fw_size;
1944 	u32 tmp;
1945 
1946 	hdr = (const struct rlc_firmware_header_v2_3 *)adev->gfx.rlc_fw->data;
1947 
1948 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1949 			le32_to_cpu(hdr->rlcp_ucode_offset_bytes));
1950 	fw_size = le32_to_cpu(hdr->rlcp_ucode_size_bytes) / 4;
1951 
1952 	WREG32_SOC15(GC, 0, regRLC_PACE_UCODE_ADDR, 0);
1953 
1954 	for (i = 0; i < fw_size; i++) {
1955 		if ((amdgpu_emu_mode == 1) && (i % 100 == 99))
1956 			msleep(1);
1957 		WREG32_SOC15(GC, 0, regRLC_PACE_UCODE_DATA,
1958 				le32_to_cpup(fw_data++));
1959 	}
1960 
1961 	WREG32_SOC15(GC, 0, regRLC_PACE_UCODE_ADDR, adev->gfx.rlc_fw_version);
1962 
1963 	tmp = RREG32_SOC15(GC, 0, regRLC_GPM_THREAD_ENABLE);
1964 	tmp = REG_SET_FIELD(tmp, RLC_GPM_THREAD_ENABLE, THREAD1_ENABLE, 1);
1965 	WREG32_SOC15(GC, 0, regRLC_GPM_THREAD_ENABLE, tmp);
1966 
1967 	fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1968 			le32_to_cpu(hdr->rlcv_ucode_offset_bytes));
1969 	fw_size = le32_to_cpu(hdr->rlcv_ucode_size_bytes) / 4;
1970 
1971 	WREG32_SOC15(GC, 0, regRLC_GPU_IOV_UCODE_ADDR, 0);
1972 
1973 	for (i = 0; i < fw_size; i++) {
1974 		if ((amdgpu_emu_mode == 1) && (i % 100 == 99))
1975 			msleep(1);
1976 		WREG32_SOC15(GC, 0, regRLC_GPU_IOV_UCODE_DATA,
1977 				le32_to_cpup(fw_data++));
1978 	}
1979 
1980 	WREG32_SOC15(GC, 0, regRLC_GPU_IOV_UCODE_ADDR, adev->gfx.rlc_fw_version);
1981 
1982 	tmp = RREG32_SOC15(GC, 0, regRLC_GPU_IOV_F32_CNTL);
1983 	tmp = REG_SET_FIELD(tmp, RLC_GPU_IOV_F32_CNTL, ENABLE, 1);
1984 	WREG32_SOC15(GC, 0, regRLC_GPU_IOV_F32_CNTL, tmp);
1985 }
1986 
1987 static int gfx_v11_0_rlc_load_microcode(struct amdgpu_device *adev)
1988 {
1989 	const struct rlc_firmware_header_v2_0 *hdr;
1990 	uint16_t version_major;
1991 	uint16_t version_minor;
1992 
1993 	if (!adev->gfx.rlc_fw)
1994 		return -EINVAL;
1995 
1996 	hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
1997 	amdgpu_ucode_print_rlc_hdr(&hdr->header);
1998 
1999 	version_major = le16_to_cpu(hdr->header.header_version_major);
2000 	version_minor = le16_to_cpu(hdr->header.header_version_minor);
2001 
2002 	if (version_major == 2) {
2003 		gfx_v11_0_load_rlcg_microcode(adev);
2004 		if (amdgpu_dpm == 1) {
2005 			if (version_minor >= 2)
2006 				gfx_v11_0_load_rlc_iram_dram_microcode(adev);
2007 			if (version_minor == 3)
2008 				gfx_v11_0_load_rlcp_rlcv_microcode(adev);
2009 		}
2010 
2011 		return 0;
2012 	}
2013 
2014 	return -EINVAL;
2015 }
2016 
2017 static int gfx_v11_0_rlc_resume(struct amdgpu_device *adev)
2018 {
2019 	int r;
2020 
2021 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
2022 		gfx_v11_0_init_csb(adev);
2023 
2024 		if (!amdgpu_sriov_vf(adev)) /* enable RLC SRM */
2025 			gfx_v11_0_rlc_enable_srm(adev);
2026 	} else {
2027 		if (amdgpu_sriov_vf(adev)) {
2028 			gfx_v11_0_init_csb(adev);
2029 			return 0;
2030 		}
2031 
2032 		adev->gfx.rlc.funcs->stop(adev);
2033 
2034 		/* disable CG */
2035 		WREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL, 0);
2036 
2037 		/* disable PG */
2038 		WREG32_SOC15(GC, 0, regRLC_PG_CNTL, 0);
2039 
2040 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
2041 			/* legacy rlc firmware loading */
2042 			r = gfx_v11_0_rlc_load_microcode(adev);
2043 			if (r)
2044 				return r;
2045 		}
2046 
2047 		gfx_v11_0_init_csb(adev);
2048 
2049 		adev->gfx.rlc.funcs->start(adev);
2050 	}
2051 	return 0;
2052 }
2053 
2054 static int gfx_v11_0_config_me_cache(struct amdgpu_device *adev, uint64_t addr)
2055 {
2056 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2057 	uint32_t tmp;
2058 	int i;
2059 
2060 	/* Trigger an invalidation of the L1 instruction caches */
2061 	tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
2062 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2063 	WREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL, tmp);
2064 
2065 	/* Wait for invalidation complete */
2066 	for (i = 0; i < usec_timeout; i++) {
2067 		tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
2068 		if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
2069 					INVALIDATE_CACHE_COMPLETE))
2070 			break;
2071 		udelay(1);
2072 	}
2073 
2074 	if (i >= usec_timeout) {
2075 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2076 		return -EINVAL;
2077 	}
2078 
2079 	if (amdgpu_emu_mode == 1)
2080 		adev->hdp.funcs->flush_hdp(adev, NULL);
2081 
2082 	tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_BASE_CNTL);
2083 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, VMID, 0);
2084 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, CACHE_POLICY, 0);
2085 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, EXE_DISABLE, 0);
2086 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2087 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_CNTL, tmp);
2088 
2089 	/* Program me ucode address into intruction cache address register */
2090 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_LO,
2091 			lower_32_bits(addr) & 0xFFFFF000);
2092 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_HI,
2093 			upper_32_bits(addr));
2094 
2095 	return 0;
2096 }
2097 
2098 static int gfx_v11_0_config_pfp_cache(struct amdgpu_device *adev, uint64_t addr)
2099 {
2100 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2101 	uint32_t tmp;
2102 	int i;
2103 
2104 	/* Trigger an invalidation of the L1 instruction caches */
2105 	tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2106 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2107 	WREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL, tmp);
2108 
2109 	/* Wait for invalidation complete */
2110 	for (i = 0; i < usec_timeout; i++) {
2111 		tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2112 		if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2113 					INVALIDATE_CACHE_COMPLETE))
2114 			break;
2115 		udelay(1);
2116 	}
2117 
2118 	if (i >= usec_timeout) {
2119 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2120 		return -EINVAL;
2121 	}
2122 
2123 	if (amdgpu_emu_mode == 1)
2124 		adev->hdp.funcs->flush_hdp(adev, NULL);
2125 
2126 	tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_CNTL);
2127 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, VMID, 0);
2128 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, CACHE_POLICY, 0);
2129 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, EXE_DISABLE, 0);
2130 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2131 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_CNTL, tmp);
2132 
2133 	/* Program pfp ucode address into intruction cache address register */
2134 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_LO,
2135 			lower_32_bits(addr) & 0xFFFFF000);
2136 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_HI,
2137 			upper_32_bits(addr));
2138 
2139 	return 0;
2140 }
2141 
2142 static int gfx_v11_0_config_mec_cache(struct amdgpu_device *adev, uint64_t addr)
2143 {
2144 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2145 	uint32_t tmp;
2146 	int i;
2147 
2148 	/* Trigger an invalidation of the L1 instruction caches */
2149 	tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL);
2150 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2151 
2152 	WREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL, tmp);
2153 
2154 	/* Wait for invalidation complete */
2155 	for (i = 0; i < usec_timeout; i++) {
2156 		tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL);
2157 		if (1 == REG_GET_FIELD(tmp, CP_CPC_IC_OP_CNTL,
2158 					INVALIDATE_CACHE_COMPLETE))
2159 			break;
2160 		udelay(1);
2161 	}
2162 
2163 	if (i >= usec_timeout) {
2164 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2165 		return -EINVAL;
2166 	}
2167 
2168 	if (amdgpu_emu_mode == 1)
2169 		adev->hdp.funcs->flush_hdp(adev, NULL);
2170 
2171 	tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_CNTL);
2172 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
2173 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, EXE_DISABLE, 0);
2174 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2175 	WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_CNTL, tmp);
2176 
2177 	/* Program mec1 ucode address into intruction cache address register */
2178 	WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_LO,
2179 			lower_32_bits(addr) & 0xFFFFF000);
2180 	WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_HI,
2181 			upper_32_bits(addr));
2182 
2183 	return 0;
2184 }
2185 
2186 static int gfx_v11_0_config_pfp_cache_rs64(struct amdgpu_device *adev, uint64_t addr, uint64_t addr2)
2187 {
2188 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2189 	uint32_t tmp;
2190 	unsigned i, pipe_id;
2191 	const struct gfx_firmware_header_v2_0 *pfp_hdr;
2192 
2193 	pfp_hdr = (const struct gfx_firmware_header_v2_0 *)
2194 		adev->gfx.pfp_fw->data;
2195 
2196 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_LO,
2197 		lower_32_bits(addr));
2198 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_HI,
2199 		upper_32_bits(addr));
2200 
2201 	tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_CNTL);
2202 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, VMID, 0);
2203 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, CACHE_POLICY, 0);
2204 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, EXE_DISABLE, 0);
2205 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_CNTL, tmp);
2206 
2207 	/*
2208 	 * Programming any of the CP_PFP_IC_BASE registers
2209 	 * forces invalidation of the ME L1 I$. Wait for the
2210 	 * invalidation complete
2211 	 */
2212 	for (i = 0; i < usec_timeout; i++) {
2213 		tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2214 		if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2215 			INVALIDATE_CACHE_COMPLETE))
2216 			break;
2217 		udelay(1);
2218 	}
2219 
2220 	if (i >= usec_timeout) {
2221 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2222 		return -EINVAL;
2223 	}
2224 
2225 	/* Prime the L1 instruction caches */
2226 	tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2227 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_OP_CNTL, PRIME_ICACHE, 1);
2228 	WREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL, tmp);
2229 	/* Waiting for cache primed*/
2230 	for (i = 0; i < usec_timeout; i++) {
2231 		tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2232 		if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2233 			ICACHE_PRIMED))
2234 			break;
2235 		udelay(1);
2236 	}
2237 
2238 	if (i >= usec_timeout) {
2239 		dev_err(adev->dev, "failed to prime instruction cache\n");
2240 		return -EINVAL;
2241 	}
2242 
2243 	mutex_lock(&adev->srbm_mutex);
2244 	for (pipe_id = 0; pipe_id < adev->gfx.me.num_pipe_per_me; pipe_id++) {
2245 		soc21_grbm_select(adev, 0, pipe_id, 0, 0);
2246 		WREG32_SOC15(GC, 0, regCP_PFP_PRGRM_CNTR_START,
2247 			(pfp_hdr->ucode_start_addr_hi << 30) |
2248 			(pfp_hdr->ucode_start_addr_lo >> 2));
2249 		WREG32_SOC15(GC, 0, regCP_PFP_PRGRM_CNTR_START_HI,
2250 			pfp_hdr->ucode_start_addr_hi >> 2);
2251 
2252 		/*
2253 		 * Program CP_ME_CNTL to reset given PIPE to take
2254 		 * effect of CP_PFP_PRGRM_CNTR_START.
2255 		 */
2256 		tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
2257 		if (pipe_id == 0)
2258 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2259 					PFP_PIPE0_RESET, 1);
2260 		else
2261 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2262 					PFP_PIPE1_RESET, 1);
2263 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2264 
2265 		/* Clear pfp pipe0 reset bit. */
2266 		if (pipe_id == 0)
2267 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2268 					PFP_PIPE0_RESET, 0);
2269 		else
2270 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2271 					PFP_PIPE1_RESET, 0);
2272 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2273 
2274 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE0_LO,
2275 			lower_32_bits(addr2));
2276 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE0_HI,
2277 			upper_32_bits(addr2));
2278 	}
2279 	soc21_grbm_select(adev, 0, 0, 0, 0);
2280 	mutex_unlock(&adev->srbm_mutex);
2281 
2282 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL);
2283 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, VMID, 0);
2284 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, CACHE_POLICY, 0);
2285 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL, tmp);
2286 
2287 	/* Invalidate the data caches */
2288 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
2289 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL, INVALIDATE_DCACHE, 1);
2290 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL, tmp);
2291 
2292 	for (i = 0; i < usec_timeout; i++) {
2293 		tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
2294 		if (1 == REG_GET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL,
2295 			INVALIDATE_DCACHE_COMPLETE))
2296 			break;
2297 		udelay(1);
2298 	}
2299 
2300 	if (i >= usec_timeout) {
2301 		dev_err(adev->dev, "failed to invalidate RS64 data cache\n");
2302 		return -EINVAL;
2303 	}
2304 
2305 	return 0;
2306 }
2307 
2308 static int gfx_v11_0_config_me_cache_rs64(struct amdgpu_device *adev, uint64_t addr, uint64_t addr2)
2309 {
2310 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2311 	uint32_t tmp;
2312 	unsigned i, pipe_id;
2313 	const struct gfx_firmware_header_v2_0 *me_hdr;
2314 
2315 	me_hdr = (const struct gfx_firmware_header_v2_0 *)
2316 		adev->gfx.me_fw->data;
2317 
2318 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_LO,
2319 		lower_32_bits(addr));
2320 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_HI,
2321 		upper_32_bits(addr));
2322 
2323 	tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_BASE_CNTL);
2324 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, VMID, 0);
2325 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, CACHE_POLICY, 0);
2326 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, EXE_DISABLE, 0);
2327 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_CNTL, tmp);
2328 
2329 	/*
2330 	 * Programming any of the CP_ME_IC_BASE registers
2331 	 * forces invalidation of the ME L1 I$. Wait for the
2332 	 * invalidation complete
2333 	 */
2334 	for (i = 0; i < usec_timeout; i++) {
2335 		tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
2336 		if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
2337 			INVALIDATE_CACHE_COMPLETE))
2338 			break;
2339 		udelay(1);
2340 	}
2341 
2342 	if (i >= usec_timeout) {
2343 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2344 		return -EINVAL;
2345 	}
2346 
2347 	/* Prime the instruction caches */
2348 	tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
2349 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_OP_CNTL, PRIME_ICACHE, 1);
2350 	WREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL, tmp);
2351 
2352 	/* Waiting for instruction cache primed*/
2353 	for (i = 0; i < usec_timeout; i++) {
2354 		tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
2355 		if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
2356 			ICACHE_PRIMED))
2357 			break;
2358 		udelay(1);
2359 	}
2360 
2361 	if (i >= usec_timeout) {
2362 		dev_err(adev->dev, "failed to prime instruction cache\n");
2363 		return -EINVAL;
2364 	}
2365 
2366 	mutex_lock(&adev->srbm_mutex);
2367 	for (pipe_id = 0; pipe_id < adev->gfx.me.num_pipe_per_me; pipe_id++) {
2368 		soc21_grbm_select(adev, 0, pipe_id, 0, 0);
2369 		WREG32_SOC15(GC, 0, regCP_ME_PRGRM_CNTR_START,
2370 			(me_hdr->ucode_start_addr_hi << 30) |
2371 			(me_hdr->ucode_start_addr_lo >> 2) );
2372 		WREG32_SOC15(GC, 0, regCP_ME_PRGRM_CNTR_START_HI,
2373 			me_hdr->ucode_start_addr_hi>>2);
2374 
2375 		/*
2376 		 * Program CP_ME_CNTL to reset given PIPE to take
2377 		 * effect of CP_PFP_PRGRM_CNTR_START.
2378 		 */
2379 		tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
2380 		if (pipe_id == 0)
2381 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2382 					ME_PIPE0_RESET, 1);
2383 		else
2384 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2385 					ME_PIPE1_RESET, 1);
2386 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2387 
2388 		/* Clear pfp pipe0 reset bit. */
2389 		if (pipe_id == 0)
2390 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2391 					ME_PIPE0_RESET, 0);
2392 		else
2393 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2394 					ME_PIPE1_RESET, 0);
2395 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2396 
2397 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE1_LO,
2398 			lower_32_bits(addr2));
2399 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE1_HI,
2400 			upper_32_bits(addr2));
2401 	}
2402 	soc21_grbm_select(adev, 0, 0, 0, 0);
2403 	mutex_unlock(&adev->srbm_mutex);
2404 
2405 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL);
2406 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, VMID, 0);
2407 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, CACHE_POLICY, 0);
2408 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL, tmp);
2409 
2410 	/* Invalidate the data caches */
2411 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
2412 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL, INVALIDATE_DCACHE, 1);
2413 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL, tmp);
2414 
2415 	for (i = 0; i < usec_timeout; i++) {
2416 		tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
2417 		if (1 == REG_GET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL,
2418 			INVALIDATE_DCACHE_COMPLETE))
2419 			break;
2420 		udelay(1);
2421 	}
2422 
2423 	if (i >= usec_timeout) {
2424 		dev_err(adev->dev, "failed to invalidate RS64 data cache\n");
2425 		return -EINVAL;
2426 	}
2427 
2428 	return 0;
2429 }
2430 
2431 static int gfx_v11_0_config_mec_cache_rs64(struct amdgpu_device *adev, uint64_t addr, uint64_t addr2)
2432 {
2433 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2434 	uint32_t tmp;
2435 	unsigned i;
2436 	const struct gfx_firmware_header_v2_0 *mec_hdr;
2437 
2438 	mec_hdr = (const struct gfx_firmware_header_v2_0 *)
2439 		adev->gfx.mec_fw->data;
2440 
2441 	tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_CNTL);
2442 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, VMID, 0);
2443 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, EXE_DISABLE, 0);
2444 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
2445 	WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_CNTL, tmp);
2446 
2447 	tmp = RREG32_SOC15(GC, 0, regCP_MEC_DC_BASE_CNTL);
2448 	tmp = REG_SET_FIELD(tmp, CP_MEC_DC_BASE_CNTL, VMID, 0);
2449 	tmp = REG_SET_FIELD(tmp, CP_MEC_DC_BASE_CNTL, CACHE_POLICY, 0);
2450 	WREG32_SOC15(GC, 0, regCP_MEC_DC_BASE_CNTL, tmp);
2451 
2452 	mutex_lock(&adev->srbm_mutex);
2453 	for (i = 0; i < adev->gfx.mec.num_pipe_per_mec; i++) {
2454 		soc21_grbm_select(adev, 1, i, 0, 0);
2455 
2456 		WREG32_SOC15(GC, 0, regCP_MEC_MDBASE_LO, addr2);
2457 		WREG32_SOC15(GC, 0, regCP_MEC_MDBASE_HI,
2458 		     upper_32_bits(addr2));
2459 
2460 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_PRGRM_CNTR_START,
2461 					mec_hdr->ucode_start_addr_lo >> 2 |
2462 					mec_hdr->ucode_start_addr_hi << 30);
2463 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_PRGRM_CNTR_START_HI,
2464 					mec_hdr->ucode_start_addr_hi >> 2);
2465 
2466 		WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_LO, addr);
2467 		WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_HI,
2468 		     upper_32_bits(addr));
2469 	}
2470 	mutex_unlock(&adev->srbm_mutex);
2471 	soc21_grbm_select(adev, 0, 0, 0, 0);
2472 
2473 	/* Trigger an invalidation of the L1 instruction caches */
2474 	tmp = RREG32_SOC15(GC, 0, regCP_MEC_DC_OP_CNTL);
2475 	tmp = REG_SET_FIELD(tmp, CP_MEC_DC_OP_CNTL, INVALIDATE_DCACHE, 1);
2476 	WREG32_SOC15(GC, 0, regCP_MEC_DC_OP_CNTL, tmp);
2477 
2478 	/* Wait for invalidation complete */
2479 	for (i = 0; i < usec_timeout; i++) {
2480 		tmp = RREG32_SOC15(GC, 0, regCP_MEC_DC_OP_CNTL);
2481 		if (1 == REG_GET_FIELD(tmp, CP_MEC_DC_OP_CNTL,
2482 				       INVALIDATE_DCACHE_COMPLETE))
2483 			break;
2484 		udelay(1);
2485 	}
2486 
2487 	if (i >= usec_timeout) {
2488 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2489 		return -EINVAL;
2490 	}
2491 
2492 	/* Trigger an invalidation of the L1 instruction caches */
2493 	tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL);
2494 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2495 	WREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL, tmp);
2496 
2497 	/* Wait for invalidation complete */
2498 	for (i = 0; i < usec_timeout; i++) {
2499 		tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL);
2500 		if (1 == REG_GET_FIELD(tmp, CP_CPC_IC_OP_CNTL,
2501 				       INVALIDATE_CACHE_COMPLETE))
2502 			break;
2503 		udelay(1);
2504 	}
2505 
2506 	if (i >= usec_timeout) {
2507 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2508 		return -EINVAL;
2509 	}
2510 
2511 	return 0;
2512 }
2513 
2514 static void gfx_v11_0_config_gfx_rs64(struct amdgpu_device *adev)
2515 {
2516 	const struct gfx_firmware_header_v2_0 *pfp_hdr;
2517 	const struct gfx_firmware_header_v2_0 *me_hdr;
2518 	const struct gfx_firmware_header_v2_0 *mec_hdr;
2519 	uint32_t pipe_id, tmp;
2520 
2521 	mec_hdr = (const struct gfx_firmware_header_v2_0 *)
2522 		adev->gfx.mec_fw->data;
2523 	me_hdr = (const struct gfx_firmware_header_v2_0 *)
2524 		adev->gfx.me_fw->data;
2525 	pfp_hdr = (const struct gfx_firmware_header_v2_0 *)
2526 		adev->gfx.pfp_fw->data;
2527 
2528 	/* config pfp program start addr */
2529 	for (pipe_id = 0; pipe_id < 2; pipe_id++) {
2530 		soc21_grbm_select(adev, 0, pipe_id, 0, 0);
2531 		WREG32_SOC15(GC, 0, regCP_PFP_PRGRM_CNTR_START,
2532 			(pfp_hdr->ucode_start_addr_hi << 30) |
2533 			(pfp_hdr->ucode_start_addr_lo >> 2));
2534 		WREG32_SOC15(GC, 0, regCP_PFP_PRGRM_CNTR_START_HI,
2535 			pfp_hdr->ucode_start_addr_hi >> 2);
2536 	}
2537 	soc21_grbm_select(adev, 0, 0, 0, 0);
2538 
2539 	/* reset pfp pipe */
2540 	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
2541 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_PIPE0_RESET, 1);
2542 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_PIPE1_RESET, 1);
2543 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2544 
2545 	/* clear pfp pipe reset */
2546 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_PIPE0_RESET, 0);
2547 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_PIPE1_RESET, 0);
2548 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2549 
2550 	/* config me program start addr */
2551 	for (pipe_id = 0; pipe_id < 2; pipe_id++) {
2552 		soc21_grbm_select(adev, 0, pipe_id, 0, 0);
2553 		WREG32_SOC15(GC, 0, regCP_ME_PRGRM_CNTR_START,
2554 			(me_hdr->ucode_start_addr_hi << 30) |
2555 			(me_hdr->ucode_start_addr_lo >> 2) );
2556 		WREG32_SOC15(GC, 0, regCP_ME_PRGRM_CNTR_START_HI,
2557 			me_hdr->ucode_start_addr_hi>>2);
2558 	}
2559 	soc21_grbm_select(adev, 0, 0, 0, 0);
2560 
2561 	/* reset me pipe */
2562 	tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
2563 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_PIPE0_RESET, 1);
2564 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_PIPE1_RESET, 1);
2565 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2566 
2567 	/* clear me pipe reset */
2568 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_PIPE0_RESET, 0);
2569 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_PIPE1_RESET, 0);
2570 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2571 
2572 	/* config mec program start addr */
2573 	for (pipe_id = 0; pipe_id < 4; pipe_id++) {
2574 		soc21_grbm_select(adev, 1, pipe_id, 0, 0);
2575 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_PRGRM_CNTR_START,
2576 					mec_hdr->ucode_start_addr_lo >> 2 |
2577 					mec_hdr->ucode_start_addr_hi << 30);
2578 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_PRGRM_CNTR_START_HI,
2579 					mec_hdr->ucode_start_addr_hi >> 2);
2580 	}
2581 	soc21_grbm_select(adev, 0, 0, 0, 0);
2582 
2583 	/* reset mec pipe */
2584 	tmp = RREG32_SOC15(GC, 0, regCP_MEC_RS64_CNTL);
2585 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE0_RESET, 1);
2586 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE1_RESET, 1);
2587 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE2_RESET, 1);
2588 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE3_RESET, 1);
2589 	WREG32_SOC15(GC, 0, regCP_MEC_RS64_CNTL, tmp);
2590 
2591 	/* clear mec pipe reset */
2592 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE0_RESET, 0);
2593 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE1_RESET, 0);
2594 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE2_RESET, 0);
2595 	tmp = REG_SET_FIELD(tmp, CP_MEC_RS64_CNTL, MEC_PIPE3_RESET, 0);
2596 	WREG32_SOC15(GC, 0, regCP_MEC_RS64_CNTL, tmp);
2597 }
2598 
2599 static int gfx_v11_0_wait_for_rlc_autoload_complete(struct amdgpu_device *adev)
2600 {
2601 	uint32_t cp_status;
2602 	uint32_t bootload_status;
2603 	int i, r;
2604 	uint64_t addr, addr2;
2605 
2606 	for (i = 0; i < adev->usec_timeout; i++) {
2607 		cp_status = RREG32_SOC15(GC, 0, regCP_STAT);
2608 
2609 		if (amdgpu_ip_version(adev, GC_HWIP, 0) ==
2610 			    IP_VERSION(11, 0, 1) ||
2611 		    amdgpu_ip_version(adev, GC_HWIP, 0) ==
2612 			    IP_VERSION(11, 0, 4) ||
2613 		    amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 5, 0))
2614 			bootload_status = RREG32_SOC15(GC, 0,
2615 					regRLC_RLCS_BOOTLOAD_STATUS_gc_11_0_1);
2616 		else
2617 			bootload_status = RREG32_SOC15(GC, 0, regRLC_RLCS_BOOTLOAD_STATUS);
2618 
2619 		if ((cp_status == 0) &&
2620 		    (REG_GET_FIELD(bootload_status,
2621 			RLC_RLCS_BOOTLOAD_STATUS, BOOTLOAD_COMPLETE) == 1)) {
2622 			break;
2623 		}
2624 		udelay(1);
2625 	}
2626 
2627 	if (i >= adev->usec_timeout) {
2628 		dev_err(adev->dev, "rlc autoload: gc ucode autoload timeout\n");
2629 		return -ETIMEDOUT;
2630 	}
2631 
2632 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
2633 		if (adev->gfx.rs64_enable) {
2634 			addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2635 				rlc_autoload_info[SOC21_FIRMWARE_ID_RS64_ME].offset;
2636 			addr2 = adev->gfx.rlc.rlc_autoload_gpu_addr +
2637 				rlc_autoload_info[SOC21_FIRMWARE_ID_RS64_ME_P0_STACK].offset;
2638 			r = gfx_v11_0_config_me_cache_rs64(adev, addr, addr2);
2639 			if (r)
2640 				return r;
2641 			addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2642 				rlc_autoload_info[SOC21_FIRMWARE_ID_RS64_PFP].offset;
2643 			addr2 = adev->gfx.rlc.rlc_autoload_gpu_addr +
2644 				rlc_autoload_info[SOC21_FIRMWARE_ID_RS64_PFP_P0_STACK].offset;
2645 			r = gfx_v11_0_config_pfp_cache_rs64(adev, addr, addr2);
2646 			if (r)
2647 				return r;
2648 			addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2649 				rlc_autoload_info[SOC21_FIRMWARE_ID_RS64_MEC].offset;
2650 			addr2 = adev->gfx.rlc.rlc_autoload_gpu_addr +
2651 				rlc_autoload_info[SOC21_FIRMWARE_ID_RS64_MEC_P0_STACK].offset;
2652 			r = gfx_v11_0_config_mec_cache_rs64(adev, addr, addr2);
2653 			if (r)
2654 				return r;
2655 		} else {
2656 			addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2657 				rlc_autoload_info[SOC21_FIRMWARE_ID_CP_ME].offset;
2658 			r = gfx_v11_0_config_me_cache(adev, addr);
2659 			if (r)
2660 				return r;
2661 			addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2662 				rlc_autoload_info[SOC21_FIRMWARE_ID_CP_PFP].offset;
2663 			r = gfx_v11_0_config_pfp_cache(adev, addr);
2664 			if (r)
2665 				return r;
2666 			addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2667 				rlc_autoload_info[SOC21_FIRMWARE_ID_CP_MEC].offset;
2668 			r = gfx_v11_0_config_mec_cache(adev, addr);
2669 			if (r)
2670 				return r;
2671 		}
2672 	}
2673 
2674 	return 0;
2675 }
2676 
2677 static int gfx_v11_0_cp_gfx_enable(struct amdgpu_device *adev, bool enable)
2678 {
2679 	int i;
2680 	u32 tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
2681 
2682 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, enable ? 0 : 1);
2683 	tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, enable ? 0 : 1);
2684 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2685 
2686 	for (i = 0; i < adev->usec_timeout; i++) {
2687 		if (RREG32_SOC15(GC, 0, regCP_STAT) == 0)
2688 			break;
2689 		udelay(1);
2690 	}
2691 
2692 	if (i >= adev->usec_timeout)
2693 		DRM_ERROR("failed to %s cp gfx\n", enable ? "unhalt" : "halt");
2694 
2695 	return 0;
2696 }
2697 
2698 static int gfx_v11_0_cp_gfx_load_pfp_microcode(struct amdgpu_device *adev)
2699 {
2700 	int r;
2701 	const struct gfx_firmware_header_v1_0 *pfp_hdr;
2702 	const __le32 *fw_data;
2703 	unsigned i, fw_size;
2704 
2705 	pfp_hdr = (const struct gfx_firmware_header_v1_0 *)
2706 		adev->gfx.pfp_fw->data;
2707 
2708 	amdgpu_ucode_print_gfx_hdr(&pfp_hdr->header);
2709 
2710 	fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
2711 		le32_to_cpu(pfp_hdr->header.ucode_array_offset_bytes));
2712 	fw_size = le32_to_cpu(pfp_hdr->header.ucode_size_bytes);
2713 
2714 	r = amdgpu_bo_create_reserved(adev, pfp_hdr->header.ucode_size_bytes,
2715 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
2716 				      &adev->gfx.pfp.pfp_fw_obj,
2717 				      &adev->gfx.pfp.pfp_fw_gpu_addr,
2718 				      (void **)&adev->gfx.pfp.pfp_fw_ptr);
2719 	if (r) {
2720 		dev_err(adev->dev, "(%d) failed to create pfp fw bo\n", r);
2721 		gfx_v11_0_pfp_fini(adev);
2722 		return r;
2723 	}
2724 
2725 	memcpy(adev->gfx.pfp.pfp_fw_ptr, fw_data, fw_size);
2726 
2727 	amdgpu_bo_kunmap(adev->gfx.pfp.pfp_fw_obj);
2728 	amdgpu_bo_unreserve(adev->gfx.pfp.pfp_fw_obj);
2729 
2730 	gfx_v11_0_config_pfp_cache(adev, adev->gfx.pfp.pfp_fw_gpu_addr);
2731 
2732 	WREG32_SOC15(GC, 0, regCP_HYP_PFP_UCODE_ADDR, 0);
2733 
2734 	for (i = 0; i < pfp_hdr->jt_size; i++)
2735 		WREG32_SOC15(GC, 0, regCP_HYP_PFP_UCODE_DATA,
2736 			     le32_to_cpup(fw_data + pfp_hdr->jt_offset + i));
2737 
2738 	WREG32_SOC15(GC, 0, regCP_HYP_PFP_UCODE_ADDR, adev->gfx.pfp_fw_version);
2739 
2740 	return 0;
2741 }
2742 
2743 static int gfx_v11_0_cp_gfx_load_pfp_microcode_rs64(struct amdgpu_device *adev)
2744 {
2745 	int r;
2746 	const struct gfx_firmware_header_v2_0 *pfp_hdr;
2747 	const __le32 *fw_ucode, *fw_data;
2748 	unsigned i, pipe_id, fw_ucode_size, fw_data_size;
2749 	uint32_t tmp;
2750 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2751 
2752 	pfp_hdr = (const struct gfx_firmware_header_v2_0 *)
2753 		adev->gfx.pfp_fw->data;
2754 
2755 	amdgpu_ucode_print_gfx_hdr(&pfp_hdr->header);
2756 
2757 	/* instruction */
2758 	fw_ucode = (const __le32 *)(adev->gfx.pfp_fw->data +
2759 		le32_to_cpu(pfp_hdr->ucode_offset_bytes));
2760 	fw_ucode_size = le32_to_cpu(pfp_hdr->ucode_size_bytes);
2761 	/* data */
2762 	fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
2763 		le32_to_cpu(pfp_hdr->data_offset_bytes));
2764 	fw_data_size = le32_to_cpu(pfp_hdr->data_size_bytes);
2765 
2766 	/* 64kb align */
2767 	r = amdgpu_bo_create_reserved(adev, fw_ucode_size,
2768 				      64 * 1024,
2769 				      AMDGPU_GEM_DOMAIN_VRAM |
2770 				      AMDGPU_GEM_DOMAIN_GTT,
2771 				      &adev->gfx.pfp.pfp_fw_obj,
2772 				      &adev->gfx.pfp.pfp_fw_gpu_addr,
2773 				      (void **)&adev->gfx.pfp.pfp_fw_ptr);
2774 	if (r) {
2775 		dev_err(adev->dev, "(%d) failed to create pfp ucode fw bo\n", r);
2776 		gfx_v11_0_pfp_fini(adev);
2777 		return r;
2778 	}
2779 
2780 	r = amdgpu_bo_create_reserved(adev, fw_data_size,
2781 				      64 * 1024,
2782 				      AMDGPU_GEM_DOMAIN_VRAM |
2783 				      AMDGPU_GEM_DOMAIN_GTT,
2784 				      &adev->gfx.pfp.pfp_fw_data_obj,
2785 				      &adev->gfx.pfp.pfp_fw_data_gpu_addr,
2786 				      (void **)&adev->gfx.pfp.pfp_fw_data_ptr);
2787 	if (r) {
2788 		dev_err(adev->dev, "(%d) failed to create pfp data fw bo\n", r);
2789 		gfx_v11_0_pfp_fini(adev);
2790 		return r;
2791 	}
2792 
2793 	memcpy(adev->gfx.pfp.pfp_fw_ptr, fw_ucode, fw_ucode_size);
2794 	memcpy(adev->gfx.pfp.pfp_fw_data_ptr, fw_data, fw_data_size);
2795 
2796 	amdgpu_bo_kunmap(adev->gfx.pfp.pfp_fw_obj);
2797 	amdgpu_bo_kunmap(adev->gfx.pfp.pfp_fw_data_obj);
2798 	amdgpu_bo_unreserve(adev->gfx.pfp.pfp_fw_obj);
2799 	amdgpu_bo_unreserve(adev->gfx.pfp.pfp_fw_data_obj);
2800 
2801 	if (amdgpu_emu_mode == 1)
2802 		adev->hdp.funcs->flush_hdp(adev, NULL);
2803 
2804 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_LO,
2805 		lower_32_bits(adev->gfx.pfp.pfp_fw_gpu_addr));
2806 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_HI,
2807 		upper_32_bits(adev->gfx.pfp.pfp_fw_gpu_addr));
2808 
2809 	tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_CNTL);
2810 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, VMID, 0);
2811 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, CACHE_POLICY, 0);
2812 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, EXE_DISABLE, 0);
2813 	WREG32_SOC15(GC, 0, regCP_PFP_IC_BASE_CNTL, tmp);
2814 
2815 	/*
2816 	 * Programming any of the CP_PFP_IC_BASE registers
2817 	 * forces invalidation of the ME L1 I$. Wait for the
2818 	 * invalidation complete
2819 	 */
2820 	for (i = 0; i < usec_timeout; i++) {
2821 		tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2822 		if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2823 			INVALIDATE_CACHE_COMPLETE))
2824 			break;
2825 		udelay(1);
2826 	}
2827 
2828 	if (i >= usec_timeout) {
2829 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
2830 		return -EINVAL;
2831 	}
2832 
2833 	/* Prime the L1 instruction caches */
2834 	tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2835 	tmp = REG_SET_FIELD(tmp, CP_PFP_IC_OP_CNTL, PRIME_ICACHE, 1);
2836 	WREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL, tmp);
2837 	/* Waiting for cache primed*/
2838 	for (i = 0; i < usec_timeout; i++) {
2839 		tmp = RREG32_SOC15(GC, 0, regCP_PFP_IC_OP_CNTL);
2840 		if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2841 			ICACHE_PRIMED))
2842 			break;
2843 		udelay(1);
2844 	}
2845 
2846 	if (i >= usec_timeout) {
2847 		dev_err(adev->dev, "failed to prime instruction cache\n");
2848 		return -EINVAL;
2849 	}
2850 
2851 	mutex_lock(&adev->srbm_mutex);
2852 	for (pipe_id = 0; pipe_id < adev->gfx.me.num_pipe_per_me; pipe_id++) {
2853 		soc21_grbm_select(adev, 0, pipe_id, 0, 0);
2854 		WREG32_SOC15(GC, 0, regCP_PFP_PRGRM_CNTR_START,
2855 			(pfp_hdr->ucode_start_addr_hi << 30) |
2856 			(pfp_hdr->ucode_start_addr_lo >> 2) );
2857 		WREG32_SOC15(GC, 0, regCP_PFP_PRGRM_CNTR_START_HI,
2858 			pfp_hdr->ucode_start_addr_hi>>2);
2859 
2860 		/*
2861 		 * Program CP_ME_CNTL to reset given PIPE to take
2862 		 * effect of CP_PFP_PRGRM_CNTR_START.
2863 		 */
2864 		tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
2865 		if (pipe_id == 0)
2866 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2867 					PFP_PIPE0_RESET, 1);
2868 		else
2869 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2870 					PFP_PIPE1_RESET, 1);
2871 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2872 
2873 		/* Clear pfp pipe0 reset bit. */
2874 		if (pipe_id == 0)
2875 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2876 					PFP_PIPE0_RESET, 0);
2877 		else
2878 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
2879 					PFP_PIPE1_RESET, 0);
2880 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
2881 
2882 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE0_LO,
2883 			lower_32_bits(adev->gfx.pfp.pfp_fw_data_gpu_addr));
2884 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE0_HI,
2885 			upper_32_bits(adev->gfx.pfp.pfp_fw_data_gpu_addr));
2886 	}
2887 	soc21_grbm_select(adev, 0, 0, 0, 0);
2888 	mutex_unlock(&adev->srbm_mutex);
2889 
2890 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL);
2891 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, VMID, 0);
2892 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, CACHE_POLICY, 0);
2893 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL, tmp);
2894 
2895 	/* Invalidate the data caches */
2896 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
2897 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL, INVALIDATE_DCACHE, 1);
2898 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL, tmp);
2899 
2900 	for (i = 0; i < usec_timeout; i++) {
2901 		tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
2902 		if (1 == REG_GET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL,
2903 			INVALIDATE_DCACHE_COMPLETE))
2904 			break;
2905 		udelay(1);
2906 	}
2907 
2908 	if (i >= usec_timeout) {
2909 		dev_err(adev->dev, "failed to invalidate RS64 data cache\n");
2910 		return -EINVAL;
2911 	}
2912 
2913 	return 0;
2914 }
2915 
2916 static int gfx_v11_0_cp_gfx_load_me_microcode(struct amdgpu_device *adev)
2917 {
2918 	int r;
2919 	const struct gfx_firmware_header_v1_0 *me_hdr;
2920 	const __le32 *fw_data;
2921 	unsigned i, fw_size;
2922 
2923 	me_hdr = (const struct gfx_firmware_header_v1_0 *)
2924 		adev->gfx.me_fw->data;
2925 
2926 	amdgpu_ucode_print_gfx_hdr(&me_hdr->header);
2927 
2928 	fw_data = (const __le32 *)(adev->gfx.me_fw->data +
2929 		le32_to_cpu(me_hdr->header.ucode_array_offset_bytes));
2930 	fw_size = le32_to_cpu(me_hdr->header.ucode_size_bytes);
2931 
2932 	r = amdgpu_bo_create_reserved(adev, me_hdr->header.ucode_size_bytes,
2933 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
2934 				      &adev->gfx.me.me_fw_obj,
2935 				      &adev->gfx.me.me_fw_gpu_addr,
2936 				      (void **)&adev->gfx.me.me_fw_ptr);
2937 	if (r) {
2938 		dev_err(adev->dev, "(%d) failed to create me fw bo\n", r);
2939 		gfx_v11_0_me_fini(adev);
2940 		return r;
2941 	}
2942 
2943 	memcpy(adev->gfx.me.me_fw_ptr, fw_data, fw_size);
2944 
2945 	amdgpu_bo_kunmap(adev->gfx.me.me_fw_obj);
2946 	amdgpu_bo_unreserve(adev->gfx.me.me_fw_obj);
2947 
2948 	gfx_v11_0_config_me_cache(adev, adev->gfx.me.me_fw_gpu_addr);
2949 
2950 	WREG32_SOC15(GC, 0, regCP_HYP_ME_UCODE_ADDR, 0);
2951 
2952 	for (i = 0; i < me_hdr->jt_size; i++)
2953 		WREG32_SOC15(GC, 0, regCP_HYP_ME_UCODE_DATA,
2954 			     le32_to_cpup(fw_data + me_hdr->jt_offset + i));
2955 
2956 	WREG32_SOC15(GC, 0, regCP_HYP_ME_UCODE_ADDR, adev->gfx.me_fw_version);
2957 
2958 	return 0;
2959 }
2960 
2961 static int gfx_v11_0_cp_gfx_load_me_microcode_rs64(struct amdgpu_device *adev)
2962 {
2963 	int r;
2964 	const struct gfx_firmware_header_v2_0 *me_hdr;
2965 	const __le32 *fw_ucode, *fw_data;
2966 	unsigned i, pipe_id, fw_ucode_size, fw_data_size;
2967 	uint32_t tmp;
2968 	uint32_t usec_timeout = 50000;  /* wait for 50ms */
2969 
2970 	me_hdr = (const struct gfx_firmware_header_v2_0 *)
2971 		adev->gfx.me_fw->data;
2972 
2973 	amdgpu_ucode_print_gfx_hdr(&me_hdr->header);
2974 
2975 	/* instruction */
2976 	fw_ucode = (const __le32 *)(adev->gfx.me_fw->data +
2977 		le32_to_cpu(me_hdr->ucode_offset_bytes));
2978 	fw_ucode_size = le32_to_cpu(me_hdr->ucode_size_bytes);
2979 	/* data */
2980 	fw_data = (const __le32 *)(adev->gfx.me_fw->data +
2981 		le32_to_cpu(me_hdr->data_offset_bytes));
2982 	fw_data_size = le32_to_cpu(me_hdr->data_size_bytes);
2983 
2984 	/* 64kb align*/
2985 	r = amdgpu_bo_create_reserved(adev, fw_ucode_size,
2986 				      64 * 1024,
2987 				      AMDGPU_GEM_DOMAIN_VRAM |
2988 				      AMDGPU_GEM_DOMAIN_GTT,
2989 				      &adev->gfx.me.me_fw_obj,
2990 				      &adev->gfx.me.me_fw_gpu_addr,
2991 				      (void **)&adev->gfx.me.me_fw_ptr);
2992 	if (r) {
2993 		dev_err(adev->dev, "(%d) failed to create me ucode bo\n", r);
2994 		gfx_v11_0_me_fini(adev);
2995 		return r;
2996 	}
2997 
2998 	r = amdgpu_bo_create_reserved(adev, fw_data_size,
2999 				      64 * 1024,
3000 				      AMDGPU_GEM_DOMAIN_VRAM |
3001 				      AMDGPU_GEM_DOMAIN_GTT,
3002 				      &adev->gfx.me.me_fw_data_obj,
3003 				      &adev->gfx.me.me_fw_data_gpu_addr,
3004 				      (void **)&adev->gfx.me.me_fw_data_ptr);
3005 	if (r) {
3006 		dev_err(adev->dev, "(%d) failed to create me data bo\n", r);
3007 		gfx_v11_0_pfp_fini(adev);
3008 		return r;
3009 	}
3010 
3011 	memcpy(adev->gfx.me.me_fw_ptr, fw_ucode, fw_ucode_size);
3012 	memcpy(adev->gfx.me.me_fw_data_ptr, fw_data, fw_data_size);
3013 
3014 	amdgpu_bo_kunmap(adev->gfx.me.me_fw_obj);
3015 	amdgpu_bo_kunmap(adev->gfx.me.me_fw_data_obj);
3016 	amdgpu_bo_unreserve(adev->gfx.me.me_fw_obj);
3017 	amdgpu_bo_unreserve(adev->gfx.me.me_fw_data_obj);
3018 
3019 	if (amdgpu_emu_mode == 1)
3020 		adev->hdp.funcs->flush_hdp(adev, NULL);
3021 
3022 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_LO,
3023 		lower_32_bits(adev->gfx.me.me_fw_gpu_addr));
3024 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_HI,
3025 		upper_32_bits(adev->gfx.me.me_fw_gpu_addr));
3026 
3027 	tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_BASE_CNTL);
3028 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, VMID, 0);
3029 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, CACHE_POLICY, 0);
3030 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, EXE_DISABLE, 0);
3031 	WREG32_SOC15(GC, 0, regCP_ME_IC_BASE_CNTL, tmp);
3032 
3033 	/*
3034 	 * Programming any of the CP_ME_IC_BASE registers
3035 	 * forces invalidation of the ME L1 I$. Wait for the
3036 	 * invalidation complete
3037 	 */
3038 	for (i = 0; i < usec_timeout; i++) {
3039 		tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
3040 		if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
3041 			INVALIDATE_CACHE_COMPLETE))
3042 			break;
3043 		udelay(1);
3044 	}
3045 
3046 	if (i >= usec_timeout) {
3047 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
3048 		return -EINVAL;
3049 	}
3050 
3051 	/* Prime the instruction caches */
3052 	tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
3053 	tmp = REG_SET_FIELD(tmp, CP_ME_IC_OP_CNTL, PRIME_ICACHE, 1);
3054 	WREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL, tmp);
3055 
3056 	/* Waiting for instruction cache primed*/
3057 	for (i = 0; i < usec_timeout; i++) {
3058 		tmp = RREG32_SOC15(GC, 0, regCP_ME_IC_OP_CNTL);
3059 		if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
3060 			ICACHE_PRIMED))
3061 			break;
3062 		udelay(1);
3063 	}
3064 
3065 	if (i >= usec_timeout) {
3066 		dev_err(adev->dev, "failed to prime instruction cache\n");
3067 		return -EINVAL;
3068 	}
3069 
3070 	mutex_lock(&adev->srbm_mutex);
3071 	for (pipe_id = 0; pipe_id < adev->gfx.me.num_pipe_per_me; pipe_id++) {
3072 		soc21_grbm_select(adev, 0, pipe_id, 0, 0);
3073 		WREG32_SOC15(GC, 0, regCP_ME_PRGRM_CNTR_START,
3074 			(me_hdr->ucode_start_addr_hi << 30) |
3075 			(me_hdr->ucode_start_addr_lo >> 2) );
3076 		WREG32_SOC15(GC, 0, regCP_ME_PRGRM_CNTR_START_HI,
3077 			me_hdr->ucode_start_addr_hi>>2);
3078 
3079 		/*
3080 		 * Program CP_ME_CNTL to reset given PIPE to take
3081 		 * effect of CP_PFP_PRGRM_CNTR_START.
3082 		 */
3083 		tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
3084 		if (pipe_id == 0)
3085 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
3086 					ME_PIPE0_RESET, 1);
3087 		else
3088 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
3089 					ME_PIPE1_RESET, 1);
3090 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
3091 
3092 		/* Clear pfp pipe0 reset bit. */
3093 		if (pipe_id == 0)
3094 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
3095 					ME_PIPE0_RESET, 0);
3096 		else
3097 			tmp = REG_SET_FIELD(tmp, CP_ME_CNTL,
3098 					ME_PIPE1_RESET, 0);
3099 		WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
3100 
3101 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE1_LO,
3102 			lower_32_bits(adev->gfx.me.me_fw_data_gpu_addr));
3103 		WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE1_HI,
3104 			upper_32_bits(adev->gfx.me.me_fw_data_gpu_addr));
3105 	}
3106 	soc21_grbm_select(adev, 0, 0, 0, 0);
3107 	mutex_unlock(&adev->srbm_mutex);
3108 
3109 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL);
3110 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, VMID, 0);
3111 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_BASE_CNTL, CACHE_POLICY, 0);
3112 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_BASE_CNTL, tmp);
3113 
3114 	/* Invalidate the data caches */
3115 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
3116 	tmp = REG_SET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL, INVALIDATE_DCACHE, 1);
3117 	WREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL, tmp);
3118 
3119 	for (i = 0; i < usec_timeout; i++) {
3120 		tmp = RREG32_SOC15(GC, 0, regCP_GFX_RS64_DC_OP_CNTL);
3121 		if (1 == REG_GET_FIELD(tmp, CP_GFX_RS64_DC_OP_CNTL,
3122 			INVALIDATE_DCACHE_COMPLETE))
3123 			break;
3124 		udelay(1);
3125 	}
3126 
3127 	if (i >= usec_timeout) {
3128 		dev_err(adev->dev, "failed to invalidate RS64 data cache\n");
3129 		return -EINVAL;
3130 	}
3131 
3132 	return 0;
3133 }
3134 
3135 static int gfx_v11_0_cp_gfx_load_microcode(struct amdgpu_device *adev)
3136 {
3137 	int r;
3138 
3139 	if (!adev->gfx.me_fw || !adev->gfx.pfp_fw)
3140 		return -EINVAL;
3141 
3142 	gfx_v11_0_cp_gfx_enable(adev, false);
3143 
3144 	if (adev->gfx.rs64_enable)
3145 		r = gfx_v11_0_cp_gfx_load_pfp_microcode_rs64(adev);
3146 	else
3147 		r = gfx_v11_0_cp_gfx_load_pfp_microcode(adev);
3148 	if (r) {
3149 		dev_err(adev->dev, "(%d) failed to load pfp fw\n", r);
3150 		return r;
3151 	}
3152 
3153 	if (adev->gfx.rs64_enable)
3154 		r = gfx_v11_0_cp_gfx_load_me_microcode_rs64(adev);
3155 	else
3156 		r = gfx_v11_0_cp_gfx_load_me_microcode(adev);
3157 	if (r) {
3158 		dev_err(adev->dev, "(%d) failed to load me fw\n", r);
3159 		return r;
3160 	}
3161 
3162 	return 0;
3163 }
3164 
3165 static int gfx_v11_0_cp_gfx_start(struct amdgpu_device *adev)
3166 {
3167 	struct amdgpu_ring *ring;
3168 	const struct cs_section_def *sect = NULL;
3169 	const struct cs_extent_def *ext = NULL;
3170 	int r, i;
3171 	int ctx_reg_offset;
3172 
3173 	/* init the CP */
3174 	WREG32_SOC15(GC, 0, regCP_MAX_CONTEXT,
3175 		     adev->gfx.config.max_hw_contexts - 1);
3176 	WREG32_SOC15(GC, 0, regCP_DEVICE_ID, 1);
3177 
3178 	if (!amdgpu_async_gfx_ring)
3179 		gfx_v11_0_cp_gfx_enable(adev, true);
3180 
3181 	ring = &adev->gfx.gfx_ring[0];
3182 	r = amdgpu_ring_alloc(ring, gfx_v11_0_get_csb_size(adev));
3183 	if (r) {
3184 		DRM_ERROR("amdgpu: cp failed to lock ring (%d).\n", r);
3185 		return r;
3186 	}
3187 
3188 	amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
3189 	amdgpu_ring_write(ring, PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
3190 
3191 	amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
3192 	amdgpu_ring_write(ring, 0x80000000);
3193 	amdgpu_ring_write(ring, 0x80000000);
3194 
3195 	for (sect = gfx11_cs_data; sect->section != NULL; ++sect) {
3196 		for (ext = sect->section; ext->extent != NULL; ++ext) {
3197 			if (sect->id == SECT_CONTEXT) {
3198 				amdgpu_ring_write(ring,
3199 						  PACKET3(PACKET3_SET_CONTEXT_REG,
3200 							  ext->reg_count));
3201 				amdgpu_ring_write(ring, ext->reg_index -
3202 						  PACKET3_SET_CONTEXT_REG_START);
3203 				for (i = 0; i < ext->reg_count; i++)
3204 					amdgpu_ring_write(ring, ext->extent[i]);
3205 			}
3206 		}
3207 	}
3208 
3209 	ctx_reg_offset =
3210 		SOC15_REG_OFFSET(GC, 0, regPA_SC_TILE_STEERING_OVERRIDE) - PACKET3_SET_CONTEXT_REG_START;
3211 	amdgpu_ring_write(ring, PACKET3(PACKET3_SET_CONTEXT_REG, 1));
3212 	amdgpu_ring_write(ring, ctx_reg_offset);
3213 	amdgpu_ring_write(ring, adev->gfx.config.pa_sc_tile_steering_override);
3214 
3215 	amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
3216 	amdgpu_ring_write(ring, PACKET3_PREAMBLE_END_CLEAR_STATE);
3217 
3218 	amdgpu_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
3219 	amdgpu_ring_write(ring, 0);
3220 
3221 	amdgpu_ring_commit(ring);
3222 
3223 	/* submit cs packet to copy state 0 to next available state */
3224 	if (adev->gfx.num_gfx_rings > 1) {
3225 		/* maximum supported gfx ring is 2 */
3226 		ring = &adev->gfx.gfx_ring[1];
3227 		r = amdgpu_ring_alloc(ring, 2);
3228 		if (r) {
3229 			DRM_ERROR("amdgpu: cp failed to lock ring (%d).\n", r);
3230 			return r;
3231 		}
3232 
3233 		amdgpu_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
3234 		amdgpu_ring_write(ring, 0);
3235 
3236 		amdgpu_ring_commit(ring);
3237 	}
3238 	return 0;
3239 }
3240 
3241 static void gfx_v11_0_cp_gfx_switch_pipe(struct amdgpu_device *adev,
3242 					 CP_PIPE_ID pipe)
3243 {
3244 	u32 tmp;
3245 
3246 	tmp = RREG32_SOC15(GC, 0, regGRBM_GFX_CNTL);
3247 	tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, PIPEID, pipe);
3248 
3249 	WREG32_SOC15(GC, 0, regGRBM_GFX_CNTL, tmp);
3250 }
3251 
3252 static void gfx_v11_0_cp_gfx_set_doorbell(struct amdgpu_device *adev,
3253 					  struct amdgpu_ring *ring)
3254 {
3255 	u32 tmp;
3256 
3257 	tmp = RREG32_SOC15(GC, 0, regCP_RB_DOORBELL_CONTROL);
3258 	if (ring->use_doorbell) {
3259 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
3260 				    DOORBELL_OFFSET, ring->doorbell_index);
3261 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
3262 				    DOORBELL_EN, 1);
3263 	} else {
3264 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
3265 				    DOORBELL_EN, 0);
3266 	}
3267 	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_CONTROL, tmp);
3268 
3269 	tmp = REG_SET_FIELD(0, CP_RB_DOORBELL_RANGE_LOWER,
3270 			    DOORBELL_RANGE_LOWER, ring->doorbell_index);
3271 	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_RANGE_LOWER, tmp);
3272 
3273 	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_RANGE_UPPER,
3274 		     CP_RB_DOORBELL_RANGE_UPPER__DOORBELL_RANGE_UPPER_MASK);
3275 }
3276 
3277 static int gfx_v11_0_cp_gfx_resume(struct amdgpu_device *adev)
3278 {
3279 	struct amdgpu_ring *ring;
3280 	u32 tmp;
3281 	u32 rb_bufsz;
3282 	u64 rb_addr, rptr_addr, wptr_gpu_addr;
3283 
3284 	/* Set the write pointer delay */
3285 	WREG32_SOC15(GC, 0, regCP_RB_WPTR_DELAY, 0);
3286 
3287 	/* set the RB to use vmid 0 */
3288 	WREG32_SOC15(GC, 0, regCP_RB_VMID, 0);
3289 
3290 	/* Init gfx ring 0 for pipe 0 */
3291 	mutex_lock(&adev->srbm_mutex);
3292 	gfx_v11_0_cp_gfx_switch_pipe(adev, PIPE_ID0);
3293 
3294 	/* Set ring buffer size */
3295 	ring = &adev->gfx.gfx_ring[0];
3296 	rb_bufsz = order_base_2(ring->ring_size / 8);
3297 	tmp = REG_SET_FIELD(0, CP_RB0_CNTL, RB_BUFSZ, rb_bufsz);
3298 	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, RB_BLKSZ, rb_bufsz - 2);
3299 	WREG32_SOC15(GC, 0, regCP_RB0_CNTL, tmp);
3300 
3301 	/* Initialize the ring buffer's write pointers */
3302 	ring->wptr = 0;
3303 	WREG32_SOC15(GC, 0, regCP_RB0_WPTR, lower_32_bits(ring->wptr));
3304 	WREG32_SOC15(GC, 0, regCP_RB0_WPTR_HI, upper_32_bits(ring->wptr));
3305 
3306 	/* set the wb address wether it's enabled or not */
3307 	rptr_addr = ring->rptr_gpu_addr;
3308 	WREG32_SOC15(GC, 0, regCP_RB0_RPTR_ADDR, lower_32_bits(rptr_addr));
3309 	WREG32_SOC15(GC, 0, regCP_RB0_RPTR_ADDR_HI, upper_32_bits(rptr_addr) &
3310 		     CP_RB_RPTR_ADDR_HI__RB_RPTR_ADDR_HI_MASK);
3311 
3312 	wptr_gpu_addr = ring->wptr_gpu_addr;
3313 	WREG32_SOC15(GC, 0, regCP_RB_WPTR_POLL_ADDR_LO,
3314 		     lower_32_bits(wptr_gpu_addr));
3315 	WREG32_SOC15(GC, 0, regCP_RB_WPTR_POLL_ADDR_HI,
3316 		     upper_32_bits(wptr_gpu_addr));
3317 
3318 	mdelay(1);
3319 	WREG32_SOC15(GC, 0, regCP_RB0_CNTL, tmp);
3320 
3321 	rb_addr = ring->gpu_addr >> 8;
3322 	WREG32_SOC15(GC, 0, regCP_RB0_BASE, rb_addr);
3323 	WREG32_SOC15(GC, 0, regCP_RB0_BASE_HI, upper_32_bits(rb_addr));
3324 
3325 	WREG32_SOC15(GC, 0, regCP_RB_ACTIVE, 1);
3326 
3327 	gfx_v11_0_cp_gfx_set_doorbell(adev, ring);
3328 	mutex_unlock(&adev->srbm_mutex);
3329 
3330 	/* Init gfx ring 1 for pipe 1 */
3331 	if (adev->gfx.num_gfx_rings > 1) {
3332 		mutex_lock(&adev->srbm_mutex);
3333 		gfx_v11_0_cp_gfx_switch_pipe(adev, PIPE_ID1);
3334 		/* maximum supported gfx ring is 2 */
3335 		ring = &adev->gfx.gfx_ring[1];
3336 		rb_bufsz = order_base_2(ring->ring_size / 8);
3337 		tmp = REG_SET_FIELD(0, CP_RB1_CNTL, RB_BUFSZ, rb_bufsz);
3338 		tmp = REG_SET_FIELD(tmp, CP_RB1_CNTL, RB_BLKSZ, rb_bufsz - 2);
3339 		WREG32_SOC15(GC, 0, regCP_RB1_CNTL, tmp);
3340 		/* Initialize the ring buffer's write pointers */
3341 		ring->wptr = 0;
3342 		WREG32_SOC15(GC, 0, regCP_RB1_WPTR, lower_32_bits(ring->wptr));
3343 		WREG32_SOC15(GC, 0, regCP_RB1_WPTR_HI, upper_32_bits(ring->wptr));
3344 		/* Set the wb address wether it's enabled or not */
3345 		rptr_addr = ring->rptr_gpu_addr;
3346 		WREG32_SOC15(GC, 0, regCP_RB1_RPTR_ADDR, lower_32_bits(rptr_addr));
3347 		WREG32_SOC15(GC, 0, regCP_RB1_RPTR_ADDR_HI, upper_32_bits(rptr_addr) &
3348 			     CP_RB1_RPTR_ADDR_HI__RB_RPTR_ADDR_HI_MASK);
3349 		wptr_gpu_addr = ring->wptr_gpu_addr;
3350 		WREG32_SOC15(GC, 0, regCP_RB_WPTR_POLL_ADDR_LO,
3351 			     lower_32_bits(wptr_gpu_addr));
3352 		WREG32_SOC15(GC, 0, regCP_RB_WPTR_POLL_ADDR_HI,
3353 			     upper_32_bits(wptr_gpu_addr));
3354 
3355 		mdelay(1);
3356 		WREG32_SOC15(GC, 0, regCP_RB1_CNTL, tmp);
3357 
3358 		rb_addr = ring->gpu_addr >> 8;
3359 		WREG32_SOC15(GC, 0, regCP_RB1_BASE, rb_addr);
3360 		WREG32_SOC15(GC, 0, regCP_RB1_BASE_HI, upper_32_bits(rb_addr));
3361 		WREG32_SOC15(GC, 0, regCP_RB1_ACTIVE, 1);
3362 
3363 		gfx_v11_0_cp_gfx_set_doorbell(adev, ring);
3364 		mutex_unlock(&adev->srbm_mutex);
3365 	}
3366 	/* Switch to pipe 0 */
3367 	mutex_lock(&adev->srbm_mutex);
3368 	gfx_v11_0_cp_gfx_switch_pipe(adev, PIPE_ID0);
3369 	mutex_unlock(&adev->srbm_mutex);
3370 
3371 	/* start the ring */
3372 	gfx_v11_0_cp_gfx_start(adev);
3373 
3374 	return 0;
3375 }
3376 
3377 static void gfx_v11_0_cp_compute_enable(struct amdgpu_device *adev, bool enable)
3378 {
3379 	u32 data;
3380 
3381 	if (adev->gfx.rs64_enable) {
3382 		data = RREG32_SOC15(GC, 0, regCP_MEC_RS64_CNTL);
3383 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_INVALIDATE_ICACHE,
3384 							 enable ? 0 : 1);
3385 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE0_RESET,
3386 							 enable ? 0 : 1);
3387 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE1_RESET,
3388 							 enable ? 0 : 1);
3389 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE2_RESET,
3390 							 enable ? 0 : 1);
3391 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE3_RESET,
3392 							 enable ? 0 : 1);
3393 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE0_ACTIVE,
3394 							 enable ? 1 : 0);
3395 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE1_ACTIVE,
3396 				                         enable ? 1 : 0);
3397 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE2_ACTIVE,
3398 							 enable ? 1 : 0);
3399 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_PIPE3_ACTIVE,
3400 							 enable ? 1 : 0);
3401 		data = REG_SET_FIELD(data, CP_MEC_RS64_CNTL, MEC_HALT,
3402 							 enable ? 0 : 1);
3403 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_CNTL, data);
3404 	} else {
3405 		data = RREG32_SOC15(GC, 0, regCP_MEC_CNTL);
3406 
3407 		if (enable) {
3408 			data = REG_SET_FIELD(data, CP_MEC_CNTL, MEC_ME1_HALT, 0);
3409 			if (!adev->enable_mes_kiq)
3410 				data = REG_SET_FIELD(data, CP_MEC_CNTL,
3411 						     MEC_ME2_HALT, 0);
3412 		} else {
3413 			data = REG_SET_FIELD(data, CP_MEC_CNTL, MEC_ME1_HALT, 1);
3414 			data = REG_SET_FIELD(data, CP_MEC_CNTL, MEC_ME2_HALT, 1);
3415 		}
3416 		WREG32_SOC15(GC, 0, regCP_MEC_CNTL, data);
3417 	}
3418 
3419 	udelay(50);
3420 }
3421 
3422 static int gfx_v11_0_cp_compute_load_microcode(struct amdgpu_device *adev)
3423 {
3424 	const struct gfx_firmware_header_v1_0 *mec_hdr;
3425 	const __le32 *fw_data;
3426 	unsigned i, fw_size;
3427 	u32 *fw = NULL;
3428 	int r;
3429 
3430 	if (!adev->gfx.mec_fw)
3431 		return -EINVAL;
3432 
3433 	gfx_v11_0_cp_compute_enable(adev, false);
3434 
3435 	mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
3436 	amdgpu_ucode_print_gfx_hdr(&mec_hdr->header);
3437 
3438 	fw_data = (const __le32 *)
3439 		(adev->gfx.mec_fw->data +
3440 		 le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
3441 	fw_size = le32_to_cpu(mec_hdr->header.ucode_size_bytes);
3442 
3443 	r = amdgpu_bo_create_reserved(adev, mec_hdr->header.ucode_size_bytes,
3444 					  PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
3445 					  &adev->gfx.mec.mec_fw_obj,
3446 					  &adev->gfx.mec.mec_fw_gpu_addr,
3447 					  (void **)&fw);
3448 	if (r) {
3449 		dev_err(adev->dev, "(%d) failed to create mec fw bo\n", r);
3450 		gfx_v11_0_mec_fini(adev);
3451 		return r;
3452 	}
3453 
3454 	memcpy(fw, fw_data, fw_size);
3455 
3456 	amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_obj);
3457 	amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_obj);
3458 
3459 	gfx_v11_0_config_mec_cache(adev, adev->gfx.mec.mec_fw_gpu_addr);
3460 
3461 	/* MEC1 */
3462 	WREG32_SOC15(GC, 0, regCP_MEC_ME1_UCODE_ADDR, 0);
3463 
3464 	for (i = 0; i < mec_hdr->jt_size; i++)
3465 		WREG32_SOC15(GC, 0, regCP_MEC_ME1_UCODE_DATA,
3466 			     le32_to_cpup(fw_data + mec_hdr->jt_offset + i));
3467 
3468 	WREG32_SOC15(GC, 0, regCP_MEC_ME1_UCODE_ADDR, adev->gfx.mec_fw_version);
3469 
3470 	return 0;
3471 }
3472 
3473 static int gfx_v11_0_cp_compute_load_microcode_rs64(struct amdgpu_device *adev)
3474 {
3475 	const struct gfx_firmware_header_v2_0 *mec_hdr;
3476 	const __le32 *fw_ucode, *fw_data;
3477 	u32 tmp, fw_ucode_size, fw_data_size;
3478 	u32 i, usec_timeout = 50000; /* Wait for 50 ms */
3479 	u32 *fw_ucode_ptr, *fw_data_ptr;
3480 	int r;
3481 
3482 	if (!adev->gfx.mec_fw)
3483 		return -EINVAL;
3484 
3485 	gfx_v11_0_cp_compute_enable(adev, false);
3486 
3487 	mec_hdr = (const struct gfx_firmware_header_v2_0 *)adev->gfx.mec_fw->data;
3488 	amdgpu_ucode_print_gfx_hdr(&mec_hdr->header);
3489 
3490 	fw_ucode = (const __le32 *) (adev->gfx.mec_fw->data +
3491 				le32_to_cpu(mec_hdr->ucode_offset_bytes));
3492 	fw_ucode_size = le32_to_cpu(mec_hdr->ucode_size_bytes);
3493 
3494 	fw_data = (const __le32 *) (adev->gfx.mec_fw->data +
3495 				le32_to_cpu(mec_hdr->data_offset_bytes));
3496 	fw_data_size = le32_to_cpu(mec_hdr->data_size_bytes);
3497 
3498 	r = amdgpu_bo_create_reserved(adev, fw_ucode_size,
3499 				      64 * 1024,
3500 				      AMDGPU_GEM_DOMAIN_VRAM |
3501 				      AMDGPU_GEM_DOMAIN_GTT,
3502 				      &adev->gfx.mec.mec_fw_obj,
3503 				      &adev->gfx.mec.mec_fw_gpu_addr,
3504 				      (void **)&fw_ucode_ptr);
3505 	if (r) {
3506 		dev_err(adev->dev, "(%d) failed to create mec fw ucode bo\n", r);
3507 		gfx_v11_0_mec_fini(adev);
3508 		return r;
3509 	}
3510 
3511 	r = amdgpu_bo_create_reserved(adev, fw_data_size,
3512 				      64 * 1024,
3513 				      AMDGPU_GEM_DOMAIN_VRAM |
3514 				      AMDGPU_GEM_DOMAIN_GTT,
3515 				      &adev->gfx.mec.mec_fw_data_obj,
3516 				      &adev->gfx.mec.mec_fw_data_gpu_addr,
3517 				      (void **)&fw_data_ptr);
3518 	if (r) {
3519 		dev_err(adev->dev, "(%d) failed to create mec fw ucode bo\n", r);
3520 		gfx_v11_0_mec_fini(adev);
3521 		return r;
3522 	}
3523 
3524 	memcpy(fw_ucode_ptr, fw_ucode, fw_ucode_size);
3525 	memcpy(fw_data_ptr, fw_data, fw_data_size);
3526 
3527 	amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_obj);
3528 	amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_data_obj);
3529 	amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_obj);
3530 	amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_data_obj);
3531 
3532 	tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_CNTL);
3533 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, VMID, 0);
3534 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, EXE_DISABLE, 0);
3535 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
3536 	WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_CNTL, tmp);
3537 
3538 	tmp = RREG32_SOC15(GC, 0, regCP_MEC_DC_BASE_CNTL);
3539 	tmp = REG_SET_FIELD(tmp, CP_MEC_DC_BASE_CNTL, VMID, 0);
3540 	tmp = REG_SET_FIELD(tmp, CP_MEC_DC_BASE_CNTL, CACHE_POLICY, 0);
3541 	WREG32_SOC15(GC, 0, regCP_MEC_DC_BASE_CNTL, tmp);
3542 
3543 	mutex_lock(&adev->srbm_mutex);
3544 	for (i = 0; i < adev->gfx.mec.num_pipe_per_mec; i++) {
3545 		soc21_grbm_select(adev, 1, i, 0, 0);
3546 
3547 		WREG32_SOC15(GC, 0, regCP_MEC_MDBASE_LO, adev->gfx.mec.mec_fw_data_gpu_addr);
3548 		WREG32_SOC15(GC, 0, regCP_MEC_MDBASE_HI,
3549 		     upper_32_bits(adev->gfx.mec.mec_fw_data_gpu_addr));
3550 
3551 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_PRGRM_CNTR_START,
3552 					mec_hdr->ucode_start_addr_lo >> 2 |
3553 					mec_hdr->ucode_start_addr_hi << 30);
3554 		WREG32_SOC15(GC, 0, regCP_MEC_RS64_PRGRM_CNTR_START_HI,
3555 					mec_hdr->ucode_start_addr_hi >> 2);
3556 
3557 		WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_LO, adev->gfx.mec.mec_fw_gpu_addr);
3558 		WREG32_SOC15(GC, 0, regCP_CPC_IC_BASE_HI,
3559 		     upper_32_bits(adev->gfx.mec.mec_fw_gpu_addr));
3560 	}
3561 	mutex_unlock(&adev->srbm_mutex);
3562 	soc21_grbm_select(adev, 0, 0, 0, 0);
3563 
3564 	/* Trigger an invalidation of the L1 instruction caches */
3565 	tmp = RREG32_SOC15(GC, 0, regCP_MEC_DC_OP_CNTL);
3566 	tmp = REG_SET_FIELD(tmp, CP_MEC_DC_OP_CNTL, INVALIDATE_DCACHE, 1);
3567 	WREG32_SOC15(GC, 0, regCP_MEC_DC_OP_CNTL, tmp);
3568 
3569 	/* Wait for invalidation complete */
3570 	for (i = 0; i < usec_timeout; i++) {
3571 		tmp = RREG32_SOC15(GC, 0, regCP_MEC_DC_OP_CNTL);
3572 		if (1 == REG_GET_FIELD(tmp, CP_MEC_DC_OP_CNTL,
3573 				       INVALIDATE_DCACHE_COMPLETE))
3574 			break;
3575 		udelay(1);
3576 	}
3577 
3578 	if (i >= usec_timeout) {
3579 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
3580 		return -EINVAL;
3581 	}
3582 
3583 	/* Trigger an invalidation of the L1 instruction caches */
3584 	tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL);
3585 	tmp = REG_SET_FIELD(tmp, CP_CPC_IC_OP_CNTL, INVALIDATE_CACHE, 1);
3586 	WREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL, tmp);
3587 
3588 	/* Wait for invalidation complete */
3589 	for (i = 0; i < usec_timeout; i++) {
3590 		tmp = RREG32_SOC15(GC, 0, regCP_CPC_IC_OP_CNTL);
3591 		if (1 == REG_GET_FIELD(tmp, CP_CPC_IC_OP_CNTL,
3592 				       INVALIDATE_CACHE_COMPLETE))
3593 			break;
3594 		udelay(1);
3595 	}
3596 
3597 	if (i >= usec_timeout) {
3598 		dev_err(adev->dev, "failed to invalidate instruction cache\n");
3599 		return -EINVAL;
3600 	}
3601 
3602 	return 0;
3603 }
3604 
3605 static void gfx_v11_0_kiq_setting(struct amdgpu_ring *ring)
3606 {
3607 	uint32_t tmp;
3608 	struct amdgpu_device *adev = ring->adev;
3609 
3610 	/* tell RLC which is KIQ queue */
3611 	tmp = RREG32_SOC15(GC, 0, regRLC_CP_SCHEDULERS);
3612 	tmp &= 0xffffff00;
3613 	tmp |= (ring->me << 5) | (ring->pipe << 3) | (ring->queue);
3614 	WREG32_SOC15(GC, 0, regRLC_CP_SCHEDULERS, tmp);
3615 	tmp |= 0x80;
3616 	WREG32_SOC15(GC, 0, regRLC_CP_SCHEDULERS, tmp);
3617 }
3618 
3619 static void gfx_v11_0_cp_set_doorbell_range(struct amdgpu_device *adev)
3620 {
3621 	/* set graphics engine doorbell range */
3622 	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_RANGE_LOWER,
3623 		     (adev->doorbell_index.gfx_ring0 * 2) << 2);
3624 	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_RANGE_UPPER,
3625 		     (adev->doorbell_index.gfx_userqueue_end * 2) << 2);
3626 
3627 	/* set compute engine doorbell range */
3628 	WREG32_SOC15(GC, 0, regCP_MEC_DOORBELL_RANGE_LOWER,
3629 		     (adev->doorbell_index.kiq * 2) << 2);
3630 	WREG32_SOC15(GC, 0, regCP_MEC_DOORBELL_RANGE_UPPER,
3631 		     (adev->doorbell_index.userqueue_end * 2) << 2);
3632 }
3633 
3634 static int gfx_v11_0_gfx_mqd_init(struct amdgpu_device *adev, void *m,
3635 				  struct amdgpu_mqd_prop *prop)
3636 {
3637 	struct v11_gfx_mqd *mqd = m;
3638 	uint64_t hqd_gpu_addr, wb_gpu_addr;
3639 	uint32_t tmp;
3640 	uint32_t rb_bufsz;
3641 
3642 	/* set up gfx hqd wptr */
3643 	mqd->cp_gfx_hqd_wptr = 0;
3644 	mqd->cp_gfx_hqd_wptr_hi = 0;
3645 
3646 	/* set the pointer to the MQD */
3647 	mqd->cp_mqd_base_addr = prop->mqd_gpu_addr & 0xfffffffc;
3648 	mqd->cp_mqd_base_addr_hi = upper_32_bits(prop->mqd_gpu_addr);
3649 
3650 	/* set up mqd control */
3651 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_MQD_CONTROL);
3652 	tmp = REG_SET_FIELD(tmp, CP_GFX_MQD_CONTROL, VMID, 0);
3653 	tmp = REG_SET_FIELD(tmp, CP_GFX_MQD_CONTROL, PRIV_STATE, 1);
3654 	tmp = REG_SET_FIELD(tmp, CP_GFX_MQD_CONTROL, CACHE_POLICY, 0);
3655 	mqd->cp_gfx_mqd_control = tmp;
3656 
3657 	/* set up gfx_hqd_vimd with 0x0 to indicate the ring buffer's vmid */
3658 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_HQD_VMID);
3659 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_VMID, VMID, 0);
3660 	mqd->cp_gfx_hqd_vmid = 0;
3661 
3662 	/* set up default queue priority level
3663 	 * 0x0 = low priority, 0x1 = high priority */
3664 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_HQD_QUEUE_PRIORITY);
3665 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_QUEUE_PRIORITY, PRIORITY_LEVEL, 0);
3666 	mqd->cp_gfx_hqd_queue_priority = tmp;
3667 
3668 	/* set up time quantum */
3669 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_HQD_QUANTUM);
3670 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_QUANTUM, QUANTUM_EN, 1);
3671 	mqd->cp_gfx_hqd_quantum = tmp;
3672 
3673 	/* set up gfx hqd base. this is similar as CP_RB_BASE */
3674 	hqd_gpu_addr = prop->hqd_base_gpu_addr >> 8;
3675 	mqd->cp_gfx_hqd_base = hqd_gpu_addr;
3676 	mqd->cp_gfx_hqd_base_hi = upper_32_bits(hqd_gpu_addr);
3677 
3678 	/* set up hqd_rptr_addr/_hi, similar as CP_RB_RPTR */
3679 	wb_gpu_addr = prop->rptr_gpu_addr;
3680 	mqd->cp_gfx_hqd_rptr_addr = wb_gpu_addr & 0xfffffffc;
3681 	mqd->cp_gfx_hqd_rptr_addr_hi =
3682 		upper_32_bits(wb_gpu_addr) & 0xffff;
3683 
3684 	/* set up rb_wptr_poll addr */
3685 	wb_gpu_addr = prop->wptr_gpu_addr;
3686 	mqd->cp_rb_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffffc;
3687 	mqd->cp_rb_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
3688 
3689 	/* set up the gfx_hqd_control, similar as CP_RB0_CNTL */
3690 	rb_bufsz = order_base_2(prop->queue_size / 4) - 1;
3691 	tmp = RREG32_SOC15(GC, 0, regCP_GFX_HQD_CNTL);
3692 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BUFSZ, rb_bufsz);
3693 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BLKSZ, rb_bufsz - 2);
3694 #ifdef __BIG_ENDIAN
3695 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, BUF_SWAP, 1);
3696 #endif
3697 	mqd->cp_gfx_hqd_cntl = tmp;
3698 
3699 	/* set up cp_doorbell_control */
3700 	tmp = RREG32_SOC15(GC, 0, regCP_RB_DOORBELL_CONTROL);
3701 	if (prop->use_doorbell) {
3702 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
3703 				    DOORBELL_OFFSET, prop->doorbell_index);
3704 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
3705 				    DOORBELL_EN, 1);
3706 	} else
3707 		tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
3708 				    DOORBELL_EN, 0);
3709 	mqd->cp_rb_doorbell_control = tmp;
3710 
3711 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
3712 	mqd->cp_gfx_hqd_rptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_RPTR);
3713 
3714 	/* active the queue */
3715 	mqd->cp_gfx_hqd_active = 1;
3716 
3717 	return 0;
3718 }
3719 
3720 static int gfx_v11_0_gfx_init_queue(struct amdgpu_ring *ring)
3721 {
3722 	struct amdgpu_device *adev = ring->adev;
3723 	struct v11_gfx_mqd *mqd = ring->mqd_ptr;
3724 	int mqd_idx = ring - &adev->gfx.gfx_ring[0];
3725 
3726 	if (!amdgpu_in_reset(adev) && !adev->in_suspend) {
3727 		memset((void *)mqd, 0, sizeof(*mqd));
3728 		mutex_lock(&adev->srbm_mutex);
3729 		soc21_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
3730 		amdgpu_ring_init_mqd(ring);
3731 		soc21_grbm_select(adev, 0, 0, 0, 0);
3732 		mutex_unlock(&adev->srbm_mutex);
3733 		if (adev->gfx.me.mqd_backup[mqd_idx])
3734 			memcpy_fromio(adev->gfx.me.mqd_backup[mqd_idx], mqd, sizeof(*mqd));
3735 	} else {
3736 		/* restore mqd with the backup copy */
3737 		if (adev->gfx.me.mqd_backup[mqd_idx])
3738 			memcpy_toio(mqd, adev->gfx.me.mqd_backup[mqd_idx], sizeof(*mqd));
3739 		/* reset the ring */
3740 		ring->wptr = 0;
3741 		*ring->wptr_cpu_addr = 0;
3742 		amdgpu_ring_clear_ring(ring);
3743 	}
3744 
3745 	return 0;
3746 }
3747 
3748 static int gfx_v11_0_cp_async_gfx_ring_resume(struct amdgpu_device *adev)
3749 {
3750 	int r, i;
3751 	struct amdgpu_ring *ring;
3752 
3753 	for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
3754 		ring = &adev->gfx.gfx_ring[i];
3755 
3756 		r = amdgpu_bo_reserve(ring->mqd_obj, false);
3757 		if (unlikely(r != 0))
3758 			return r;
3759 
3760 		r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
3761 		if (!r) {
3762 			r = gfx_v11_0_gfx_init_queue(ring);
3763 			amdgpu_bo_kunmap(ring->mqd_obj);
3764 			ring->mqd_ptr = NULL;
3765 		}
3766 		amdgpu_bo_unreserve(ring->mqd_obj);
3767 		if (r)
3768 			return r;
3769 	}
3770 
3771 	r = amdgpu_gfx_enable_kgq(adev, 0);
3772 	if (r)
3773 		return r;
3774 
3775 	return gfx_v11_0_cp_gfx_start(adev);
3776 }
3777 
3778 static int gfx_v11_0_compute_mqd_init(struct amdgpu_device *adev, void *m,
3779 				      struct amdgpu_mqd_prop *prop)
3780 {
3781 	struct v11_compute_mqd *mqd = m;
3782 	uint64_t hqd_gpu_addr, wb_gpu_addr, eop_base_addr;
3783 	uint32_t tmp;
3784 
3785 	mqd->header = 0xC0310800;
3786 	mqd->compute_pipelinestat_enable = 0x00000001;
3787 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
3788 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
3789 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
3790 	mqd->compute_static_thread_mgmt_se3 = 0xffffffff;
3791 	mqd->compute_misc_reserved = 0x00000007;
3792 
3793 	eop_base_addr = prop->eop_gpu_addr >> 8;
3794 	mqd->cp_hqd_eop_base_addr_lo = eop_base_addr;
3795 	mqd->cp_hqd_eop_base_addr_hi = upper_32_bits(eop_base_addr);
3796 
3797 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
3798 	tmp = RREG32_SOC15(GC, 0, regCP_HQD_EOP_CONTROL);
3799 	tmp = REG_SET_FIELD(tmp, CP_HQD_EOP_CONTROL, EOP_SIZE,
3800 			(order_base_2(GFX11_MEC_HPD_SIZE / 4) - 1));
3801 
3802 	mqd->cp_hqd_eop_control = tmp;
3803 
3804 	/* enable doorbell? */
3805 	tmp = RREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL);
3806 
3807 	if (prop->use_doorbell) {
3808 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3809 				    DOORBELL_OFFSET, prop->doorbell_index);
3810 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3811 				    DOORBELL_EN, 1);
3812 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3813 				    DOORBELL_SOURCE, 0);
3814 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3815 				    DOORBELL_HIT, 0);
3816 	} else {
3817 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3818 				    DOORBELL_EN, 0);
3819 	}
3820 
3821 	mqd->cp_hqd_pq_doorbell_control = tmp;
3822 
3823 	/* disable the queue if it's active */
3824 	mqd->cp_hqd_dequeue_request = 0;
3825 	mqd->cp_hqd_pq_rptr = 0;
3826 	mqd->cp_hqd_pq_wptr_lo = 0;
3827 	mqd->cp_hqd_pq_wptr_hi = 0;
3828 
3829 	/* set the pointer to the MQD */
3830 	mqd->cp_mqd_base_addr_lo = prop->mqd_gpu_addr & 0xfffffffc;
3831 	mqd->cp_mqd_base_addr_hi = upper_32_bits(prop->mqd_gpu_addr);
3832 
3833 	/* set MQD vmid to 0 */
3834 	tmp = RREG32_SOC15(GC, 0, regCP_MQD_CONTROL);
3835 	tmp = REG_SET_FIELD(tmp, CP_MQD_CONTROL, VMID, 0);
3836 	mqd->cp_mqd_control = tmp;
3837 
3838 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
3839 	hqd_gpu_addr = prop->hqd_base_gpu_addr >> 8;
3840 	mqd->cp_hqd_pq_base_lo = hqd_gpu_addr;
3841 	mqd->cp_hqd_pq_base_hi = upper_32_bits(hqd_gpu_addr);
3842 
3843 	/* set up the HQD, this is similar to CP_RB0_CNTL */
3844 	tmp = RREG32_SOC15(GC, 0, regCP_HQD_PQ_CONTROL);
3845 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, QUEUE_SIZE,
3846 			    (order_base_2(prop->queue_size / 4) - 1));
3847 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, RPTR_BLOCK_SIZE,
3848 			    (order_base_2(AMDGPU_GPU_PAGE_SIZE / 4) - 1));
3849 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, UNORD_DISPATCH, 0);
3850 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, TUNNEL_DISPATCH,
3851 			    prop->allow_tunneling);
3852 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, PRIV_STATE, 1);
3853 	tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_CONTROL, KMD_QUEUE, 1);
3854 	mqd->cp_hqd_pq_control = tmp;
3855 
3856 	/* set the wb address whether it's enabled or not */
3857 	wb_gpu_addr = prop->rptr_gpu_addr;
3858 	mqd->cp_hqd_pq_rptr_report_addr_lo = wb_gpu_addr & 0xfffffffc;
3859 	mqd->cp_hqd_pq_rptr_report_addr_hi =
3860 		upper_32_bits(wb_gpu_addr) & 0xffff;
3861 
3862 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
3863 	wb_gpu_addr = prop->wptr_gpu_addr;
3864 	mqd->cp_hqd_pq_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffffc;
3865 	mqd->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
3866 
3867 	tmp = 0;
3868 	/* enable the doorbell if requested */
3869 	if (prop->use_doorbell) {
3870 		tmp = RREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL);
3871 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3872 				DOORBELL_OFFSET, prop->doorbell_index);
3873 
3874 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3875 				    DOORBELL_EN, 1);
3876 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3877 				    DOORBELL_SOURCE, 0);
3878 		tmp = REG_SET_FIELD(tmp, CP_HQD_PQ_DOORBELL_CONTROL,
3879 				    DOORBELL_HIT, 0);
3880 	}
3881 
3882 	mqd->cp_hqd_pq_doorbell_control = tmp;
3883 
3884 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
3885 	mqd->cp_hqd_pq_rptr = RREG32_SOC15(GC, 0, regCP_HQD_PQ_RPTR);
3886 
3887 	/* set the vmid for the queue */
3888 	mqd->cp_hqd_vmid = 0;
3889 
3890 	tmp = RREG32_SOC15(GC, 0, regCP_HQD_PERSISTENT_STATE);
3891 	tmp = REG_SET_FIELD(tmp, CP_HQD_PERSISTENT_STATE, PRELOAD_SIZE, 0x55);
3892 	mqd->cp_hqd_persistent_state = tmp;
3893 
3894 	/* set MIN_IB_AVAIL_SIZE */
3895 	tmp = RREG32_SOC15(GC, 0, regCP_HQD_IB_CONTROL);
3896 	tmp = REG_SET_FIELD(tmp, CP_HQD_IB_CONTROL, MIN_IB_AVAIL_SIZE, 3);
3897 	mqd->cp_hqd_ib_control = tmp;
3898 
3899 	/* set static priority for a compute queue/ring */
3900 	mqd->cp_hqd_pipe_priority = prop->hqd_pipe_priority;
3901 	mqd->cp_hqd_queue_priority = prop->hqd_queue_priority;
3902 
3903 	mqd->cp_hqd_active = prop->hqd_active;
3904 
3905 	return 0;
3906 }
3907 
3908 static int gfx_v11_0_kiq_init_register(struct amdgpu_ring *ring)
3909 {
3910 	struct amdgpu_device *adev = ring->adev;
3911 	struct v11_compute_mqd *mqd = ring->mqd_ptr;
3912 	int j;
3913 
3914 	/* inactivate the queue */
3915 	if (amdgpu_sriov_vf(adev))
3916 		WREG32_SOC15(GC, 0, regCP_HQD_ACTIVE, 0);
3917 
3918 	/* disable wptr polling */
3919 	WREG32_FIELD15_PREREG(GC, 0, CP_PQ_WPTR_POLL_CNTL, EN, 0);
3920 
3921 	/* write the EOP addr */
3922 	WREG32_SOC15(GC, 0, regCP_HQD_EOP_BASE_ADDR,
3923 	       mqd->cp_hqd_eop_base_addr_lo);
3924 	WREG32_SOC15(GC, 0, regCP_HQD_EOP_BASE_ADDR_HI,
3925 	       mqd->cp_hqd_eop_base_addr_hi);
3926 
3927 	/* set the EOP size, register value is 2^(EOP_SIZE+1) dwords */
3928 	WREG32_SOC15(GC, 0, regCP_HQD_EOP_CONTROL,
3929 	       mqd->cp_hqd_eop_control);
3930 
3931 	/* enable doorbell? */
3932 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL,
3933 	       mqd->cp_hqd_pq_doorbell_control);
3934 
3935 	/* disable the queue if it's active */
3936 	if (RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) & 1) {
3937 		WREG32_SOC15(GC, 0, regCP_HQD_DEQUEUE_REQUEST, 1);
3938 		for (j = 0; j < adev->usec_timeout; j++) {
3939 			if (!(RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) & 1))
3940 				break;
3941 			udelay(1);
3942 		}
3943 		WREG32_SOC15(GC, 0, regCP_HQD_DEQUEUE_REQUEST,
3944 		       mqd->cp_hqd_dequeue_request);
3945 		WREG32_SOC15(GC, 0, regCP_HQD_PQ_RPTR,
3946 		       mqd->cp_hqd_pq_rptr);
3947 		WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_LO,
3948 		       mqd->cp_hqd_pq_wptr_lo);
3949 		WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_HI,
3950 		       mqd->cp_hqd_pq_wptr_hi);
3951 	}
3952 
3953 	/* set the pointer to the MQD */
3954 	WREG32_SOC15(GC, 0, regCP_MQD_BASE_ADDR,
3955 	       mqd->cp_mqd_base_addr_lo);
3956 	WREG32_SOC15(GC, 0, regCP_MQD_BASE_ADDR_HI,
3957 	       mqd->cp_mqd_base_addr_hi);
3958 
3959 	/* set MQD vmid to 0 */
3960 	WREG32_SOC15(GC, 0, regCP_MQD_CONTROL,
3961 	       mqd->cp_mqd_control);
3962 
3963 	/* set the pointer to the HQD, this is similar CP_RB0_BASE/_HI */
3964 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_BASE,
3965 	       mqd->cp_hqd_pq_base_lo);
3966 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_BASE_HI,
3967 	       mqd->cp_hqd_pq_base_hi);
3968 
3969 	/* set up the HQD, this is similar to CP_RB0_CNTL */
3970 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_CONTROL,
3971 	       mqd->cp_hqd_pq_control);
3972 
3973 	/* set the wb address whether it's enabled or not */
3974 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_RPTR_REPORT_ADDR,
3975 		mqd->cp_hqd_pq_rptr_report_addr_lo);
3976 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_RPTR_REPORT_ADDR_HI,
3977 		mqd->cp_hqd_pq_rptr_report_addr_hi);
3978 
3979 	/* only used if CP_PQ_WPTR_POLL_CNTL.CP_PQ_WPTR_POLL_CNTL__EN_MASK=1 */
3980 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_POLL_ADDR,
3981 	       mqd->cp_hqd_pq_wptr_poll_addr_lo);
3982 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_POLL_ADDR_HI,
3983 	       mqd->cp_hqd_pq_wptr_poll_addr_hi);
3984 
3985 	/* enable the doorbell if requested */
3986 	if (ring->use_doorbell) {
3987 		WREG32_SOC15(GC, 0, regCP_MEC_DOORBELL_RANGE_LOWER,
3988 			(adev->doorbell_index.kiq * 2) << 2);
3989 		WREG32_SOC15(GC, 0, regCP_MEC_DOORBELL_RANGE_UPPER,
3990 			(adev->doorbell_index.userqueue_end * 2) << 2);
3991 	}
3992 
3993 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL,
3994 	       mqd->cp_hqd_pq_doorbell_control);
3995 
3996 	/* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
3997 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_LO,
3998 	       mqd->cp_hqd_pq_wptr_lo);
3999 	WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_HI,
4000 	       mqd->cp_hqd_pq_wptr_hi);
4001 
4002 	/* set the vmid for the queue */
4003 	WREG32_SOC15(GC, 0, regCP_HQD_VMID, mqd->cp_hqd_vmid);
4004 
4005 	WREG32_SOC15(GC, 0, regCP_HQD_PERSISTENT_STATE,
4006 	       mqd->cp_hqd_persistent_state);
4007 
4008 	/* activate the queue */
4009 	WREG32_SOC15(GC, 0, regCP_HQD_ACTIVE,
4010 	       mqd->cp_hqd_active);
4011 
4012 	if (ring->use_doorbell)
4013 		WREG32_FIELD15_PREREG(GC, 0, CP_PQ_STATUS, DOORBELL_ENABLE, 1);
4014 
4015 	return 0;
4016 }
4017 
4018 static int gfx_v11_0_kiq_init_queue(struct amdgpu_ring *ring)
4019 {
4020 	struct amdgpu_device *adev = ring->adev;
4021 	struct v11_compute_mqd *mqd = ring->mqd_ptr;
4022 
4023 	gfx_v11_0_kiq_setting(ring);
4024 
4025 	if (amdgpu_in_reset(adev)) { /* for GPU_RESET case */
4026 		/* reset MQD to a clean status */
4027 		if (adev->gfx.kiq[0].mqd_backup)
4028 			memcpy_toio(mqd, adev->gfx.kiq[0].mqd_backup, sizeof(*mqd));
4029 
4030 		/* reset ring buffer */
4031 		ring->wptr = 0;
4032 		amdgpu_ring_clear_ring(ring);
4033 
4034 		mutex_lock(&adev->srbm_mutex);
4035 		soc21_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
4036 		gfx_v11_0_kiq_init_register(ring);
4037 		soc21_grbm_select(adev, 0, 0, 0, 0);
4038 		mutex_unlock(&adev->srbm_mutex);
4039 	} else {
4040 		memset((void *)mqd, 0, sizeof(*mqd));
4041 		if (amdgpu_sriov_vf(adev) && adev->in_suspend)
4042 			amdgpu_ring_clear_ring(ring);
4043 		mutex_lock(&adev->srbm_mutex);
4044 		soc21_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
4045 		amdgpu_ring_init_mqd(ring);
4046 		gfx_v11_0_kiq_init_register(ring);
4047 		soc21_grbm_select(adev, 0, 0, 0, 0);
4048 		mutex_unlock(&adev->srbm_mutex);
4049 
4050 		if (adev->gfx.kiq[0].mqd_backup)
4051 			memcpy_fromio(adev->gfx.kiq[0].mqd_backup, mqd, sizeof(*mqd));
4052 	}
4053 
4054 	return 0;
4055 }
4056 
4057 static int gfx_v11_0_kcq_init_queue(struct amdgpu_ring *ring)
4058 {
4059 	struct amdgpu_device *adev = ring->adev;
4060 	struct v11_compute_mqd *mqd = ring->mqd_ptr;
4061 	int mqd_idx = ring - &adev->gfx.compute_ring[0];
4062 
4063 	if (!amdgpu_in_reset(adev) && !adev->in_suspend) {
4064 		memset((void *)mqd, 0, sizeof(*mqd));
4065 		mutex_lock(&adev->srbm_mutex);
4066 		soc21_grbm_select(adev, ring->me, ring->pipe, ring->queue, 0);
4067 		amdgpu_ring_init_mqd(ring);
4068 		soc21_grbm_select(adev, 0, 0, 0, 0);
4069 		mutex_unlock(&adev->srbm_mutex);
4070 
4071 		if (adev->gfx.mec.mqd_backup[mqd_idx])
4072 			memcpy_fromio(adev->gfx.mec.mqd_backup[mqd_idx], mqd, sizeof(*mqd));
4073 	} else {
4074 		/* restore MQD to a clean status */
4075 		if (adev->gfx.mec.mqd_backup[mqd_idx])
4076 			memcpy_toio(mqd, adev->gfx.mec.mqd_backup[mqd_idx], sizeof(*mqd));
4077 		/* reset ring buffer */
4078 		ring->wptr = 0;
4079 		atomic64_set((atomic64_t *)ring->wptr_cpu_addr, 0);
4080 		amdgpu_ring_clear_ring(ring);
4081 	}
4082 
4083 	return 0;
4084 }
4085 
4086 static int gfx_v11_0_kiq_resume(struct amdgpu_device *adev)
4087 {
4088 	struct amdgpu_ring *ring;
4089 	int r;
4090 
4091 	ring = &adev->gfx.kiq[0].ring;
4092 
4093 	r = amdgpu_bo_reserve(ring->mqd_obj, false);
4094 	if (unlikely(r != 0))
4095 		return r;
4096 
4097 	r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
4098 	if (unlikely(r != 0)) {
4099 		amdgpu_bo_unreserve(ring->mqd_obj);
4100 		return r;
4101 	}
4102 
4103 	gfx_v11_0_kiq_init_queue(ring);
4104 	amdgpu_bo_kunmap(ring->mqd_obj);
4105 	ring->mqd_ptr = NULL;
4106 	amdgpu_bo_unreserve(ring->mqd_obj);
4107 	ring->sched.ready = true;
4108 	return 0;
4109 }
4110 
4111 static int gfx_v11_0_kcq_resume(struct amdgpu_device *adev)
4112 {
4113 	struct amdgpu_ring *ring = NULL;
4114 	int r = 0, i;
4115 
4116 	if (!amdgpu_async_gfx_ring)
4117 		gfx_v11_0_cp_compute_enable(adev, true);
4118 
4119 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
4120 		ring = &adev->gfx.compute_ring[i];
4121 
4122 		r = amdgpu_bo_reserve(ring->mqd_obj, false);
4123 		if (unlikely(r != 0))
4124 			goto done;
4125 		r = amdgpu_bo_kmap(ring->mqd_obj, (void **)&ring->mqd_ptr);
4126 		if (!r) {
4127 			r = gfx_v11_0_kcq_init_queue(ring);
4128 			amdgpu_bo_kunmap(ring->mqd_obj);
4129 			ring->mqd_ptr = NULL;
4130 		}
4131 		amdgpu_bo_unreserve(ring->mqd_obj);
4132 		if (r)
4133 			goto done;
4134 	}
4135 
4136 	r = amdgpu_gfx_enable_kcq(adev, 0);
4137 done:
4138 	return r;
4139 }
4140 
4141 static int gfx_v11_0_cp_resume(struct amdgpu_device *adev)
4142 {
4143 	int r, i;
4144 	struct amdgpu_ring *ring;
4145 
4146 	if (!(adev->flags & AMD_IS_APU))
4147 		gfx_v11_0_enable_gui_idle_interrupt(adev, false);
4148 
4149 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
4150 		/* legacy firmware loading */
4151 		r = gfx_v11_0_cp_gfx_load_microcode(adev);
4152 		if (r)
4153 			return r;
4154 
4155 		if (adev->gfx.rs64_enable)
4156 			r = gfx_v11_0_cp_compute_load_microcode_rs64(adev);
4157 		else
4158 			r = gfx_v11_0_cp_compute_load_microcode(adev);
4159 		if (r)
4160 			return r;
4161 	}
4162 
4163 	gfx_v11_0_cp_set_doorbell_range(adev);
4164 
4165 	if (amdgpu_async_gfx_ring) {
4166 		gfx_v11_0_cp_compute_enable(adev, true);
4167 		gfx_v11_0_cp_gfx_enable(adev, true);
4168 	}
4169 
4170 	if (adev->enable_mes_kiq && adev->mes.kiq_hw_init)
4171 		r = amdgpu_mes_kiq_hw_init(adev);
4172 	else
4173 		r = gfx_v11_0_kiq_resume(adev);
4174 	if (r)
4175 		return r;
4176 
4177 	r = gfx_v11_0_kcq_resume(adev);
4178 	if (r)
4179 		return r;
4180 
4181 	if (!amdgpu_async_gfx_ring) {
4182 		r = gfx_v11_0_cp_gfx_resume(adev);
4183 		if (r)
4184 			return r;
4185 	} else {
4186 		r = gfx_v11_0_cp_async_gfx_ring_resume(adev);
4187 		if (r)
4188 			return r;
4189 	}
4190 
4191 	for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
4192 		ring = &adev->gfx.gfx_ring[i];
4193 		r = amdgpu_ring_test_helper(ring);
4194 		if (r)
4195 			return r;
4196 	}
4197 
4198 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
4199 		ring = &adev->gfx.compute_ring[i];
4200 		r = amdgpu_ring_test_helper(ring);
4201 		if (r)
4202 			return r;
4203 	}
4204 
4205 	return 0;
4206 }
4207 
4208 static void gfx_v11_0_cp_enable(struct amdgpu_device *adev, bool enable)
4209 {
4210 	gfx_v11_0_cp_gfx_enable(adev, enable);
4211 	gfx_v11_0_cp_compute_enable(adev, enable);
4212 }
4213 
4214 static int gfx_v11_0_gfxhub_enable(struct amdgpu_device *adev)
4215 {
4216 	int r;
4217 	bool value;
4218 
4219 	r = adev->gfxhub.funcs->gart_enable(adev);
4220 	if (r)
4221 		return r;
4222 
4223 	adev->hdp.funcs->flush_hdp(adev, NULL);
4224 
4225 	value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
4226 		false : true;
4227 
4228 	adev->gfxhub.funcs->set_fault_enable_default(adev, value);
4229 	amdgpu_gmc_flush_gpu_tlb(adev, 0, AMDGPU_GFXHUB(0), 0);
4230 
4231 	return 0;
4232 }
4233 
4234 static void gfx_v11_0_select_cp_fw_arch(struct amdgpu_device *adev)
4235 {
4236 	u32 tmp;
4237 
4238 	/* select RS64 */
4239 	if (adev->gfx.rs64_enable) {
4240 		tmp = RREG32_SOC15(GC, 0, regCP_GFX_CNTL);
4241 		tmp = REG_SET_FIELD(tmp, CP_GFX_CNTL, ENGINE_SEL, 1);
4242 		WREG32_SOC15(GC, 0, regCP_GFX_CNTL, tmp);
4243 
4244 		tmp = RREG32_SOC15(GC, 0, regCP_MEC_ISA_CNTL);
4245 		tmp = REG_SET_FIELD(tmp, CP_MEC_ISA_CNTL, ISA_MODE, 1);
4246 		WREG32_SOC15(GC, 0, regCP_MEC_ISA_CNTL, tmp);
4247 	}
4248 
4249 	if (amdgpu_emu_mode == 1)
4250 		msleep(100);
4251 }
4252 
4253 static int get_gb_addr_config(struct amdgpu_device * adev)
4254 {
4255 	u32 gb_addr_config;
4256 
4257 	gb_addr_config = RREG32_SOC15(GC, 0, regGB_ADDR_CONFIG);
4258 	if (gb_addr_config == 0)
4259 		return -EINVAL;
4260 
4261 	adev->gfx.config.gb_addr_config_fields.num_pkrs =
4262 		1 << REG_GET_FIELD(gb_addr_config, GB_ADDR_CONFIG, NUM_PKRS);
4263 
4264 	adev->gfx.config.gb_addr_config = gb_addr_config;
4265 
4266 	adev->gfx.config.gb_addr_config_fields.num_pipes = 1 <<
4267 			REG_GET_FIELD(adev->gfx.config.gb_addr_config,
4268 				      GB_ADDR_CONFIG, NUM_PIPES);
4269 
4270 	adev->gfx.config.max_tile_pipes =
4271 		adev->gfx.config.gb_addr_config_fields.num_pipes;
4272 
4273 	adev->gfx.config.gb_addr_config_fields.max_compress_frags = 1 <<
4274 			REG_GET_FIELD(adev->gfx.config.gb_addr_config,
4275 				      GB_ADDR_CONFIG, MAX_COMPRESSED_FRAGS);
4276 	adev->gfx.config.gb_addr_config_fields.num_rb_per_se = 1 <<
4277 			REG_GET_FIELD(adev->gfx.config.gb_addr_config,
4278 				      GB_ADDR_CONFIG, NUM_RB_PER_SE);
4279 	adev->gfx.config.gb_addr_config_fields.num_se = 1 <<
4280 			REG_GET_FIELD(adev->gfx.config.gb_addr_config,
4281 				      GB_ADDR_CONFIG, NUM_SHADER_ENGINES);
4282 	adev->gfx.config.gb_addr_config_fields.pipe_interleave_size = 1 << (8 +
4283 			REG_GET_FIELD(adev->gfx.config.gb_addr_config,
4284 				      GB_ADDR_CONFIG, PIPE_INTERLEAVE_SIZE));
4285 
4286 	return 0;
4287 }
4288 
4289 static void gfx_v11_0_disable_gpa_mode(struct amdgpu_device *adev)
4290 {
4291 	uint32_t data;
4292 
4293 	data = RREG32_SOC15(GC, 0, regCPC_PSP_DEBUG);
4294 	data |= CPC_PSP_DEBUG__GPA_OVERRIDE_MASK;
4295 	WREG32_SOC15(GC, 0, regCPC_PSP_DEBUG, data);
4296 
4297 	data = RREG32_SOC15(GC, 0, regCPG_PSP_DEBUG);
4298 	data |= CPG_PSP_DEBUG__GPA_OVERRIDE_MASK;
4299 	WREG32_SOC15(GC, 0, regCPG_PSP_DEBUG, data);
4300 }
4301 
4302 static int gfx_v11_0_hw_init(void *handle)
4303 {
4304 	int r;
4305 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4306 
4307 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
4308 		if (adev->gfx.imu.funcs) {
4309 			/* RLC autoload sequence 1: Program rlc ram */
4310 			if (adev->gfx.imu.funcs->program_rlc_ram)
4311 				adev->gfx.imu.funcs->program_rlc_ram(adev);
4312 		}
4313 		/* rlc autoload firmware */
4314 		r = gfx_v11_0_rlc_backdoor_autoload_enable(adev);
4315 		if (r)
4316 			return r;
4317 	} else {
4318 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
4319 			if (adev->gfx.imu.funcs && (amdgpu_dpm > 0)) {
4320 				if (adev->gfx.imu.funcs->load_microcode)
4321 					adev->gfx.imu.funcs->load_microcode(adev);
4322 				if (adev->gfx.imu.funcs->setup_imu)
4323 					adev->gfx.imu.funcs->setup_imu(adev);
4324 				if (adev->gfx.imu.funcs->start_imu)
4325 					adev->gfx.imu.funcs->start_imu(adev);
4326 			}
4327 
4328 			/* disable gpa mode in backdoor loading */
4329 			gfx_v11_0_disable_gpa_mode(adev);
4330 		}
4331 	}
4332 
4333 	if ((adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) ||
4334 	    (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
4335 		r = gfx_v11_0_wait_for_rlc_autoload_complete(adev);
4336 		if (r) {
4337 			dev_err(adev->dev, "(%d) failed to wait rlc autoload complete\n", r);
4338 			return r;
4339 		}
4340 	}
4341 
4342 	adev->gfx.is_poweron = true;
4343 
4344 	if(get_gb_addr_config(adev))
4345 		DRM_WARN("Invalid gb_addr_config !\n");
4346 
4347 	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP &&
4348 	    adev->gfx.rs64_enable)
4349 		gfx_v11_0_config_gfx_rs64(adev);
4350 
4351 	r = gfx_v11_0_gfxhub_enable(adev);
4352 	if (r)
4353 		return r;
4354 
4355 	if (!amdgpu_emu_mode)
4356 		gfx_v11_0_init_golden_registers(adev);
4357 
4358 	if ((adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) ||
4359 	    (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO && amdgpu_dpm == 1)) {
4360 		/**
4361 		 * For gfx 11, rlc firmware loading relies on smu firmware is
4362 		 * loaded firstly, so in direct type, it has to load smc ucode
4363 		 * here before rlc.
4364 		 */
4365 		if (!(adev->flags & AMD_IS_APU)) {
4366 			r = amdgpu_pm_load_smu_firmware(adev, NULL);
4367 			if (r)
4368 				return r;
4369 		}
4370 	}
4371 
4372 	gfx_v11_0_constants_init(adev);
4373 
4374 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
4375 		gfx_v11_0_select_cp_fw_arch(adev);
4376 
4377 	if (adev->nbio.funcs->gc_doorbell_init)
4378 		adev->nbio.funcs->gc_doorbell_init(adev);
4379 
4380 	r = gfx_v11_0_rlc_resume(adev);
4381 	if (r)
4382 		return r;
4383 
4384 	/*
4385 	 * init golden registers and rlc resume may override some registers,
4386 	 * reconfig them here
4387 	 */
4388 	gfx_v11_0_tcp_harvest(adev);
4389 
4390 	r = gfx_v11_0_cp_resume(adev);
4391 	if (r)
4392 		return r;
4393 
4394 	/* get IMU version from HW if it's not set */
4395 	if (!adev->gfx.imu_fw_version)
4396 		adev->gfx.imu_fw_version = RREG32_SOC15(GC, 0, regGFX_IMU_SCRATCH_0);
4397 
4398 	return r;
4399 }
4400 
4401 static int gfx_v11_0_hw_fini(void *handle)
4402 {
4403 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4404 
4405 	amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
4406 	amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
4407 
4408 	if (!adev->no_hw_access) {
4409 		if (amdgpu_async_gfx_ring) {
4410 			if (amdgpu_gfx_disable_kgq(adev, 0))
4411 				DRM_ERROR("KGQ disable failed\n");
4412 		}
4413 
4414 		if (amdgpu_gfx_disable_kcq(adev, 0))
4415 			DRM_ERROR("KCQ disable failed\n");
4416 
4417 		amdgpu_mes_kiq_hw_fini(adev);
4418 	}
4419 
4420 	if (amdgpu_sriov_vf(adev))
4421 		/* Remove the steps disabling CPG and clearing KIQ position,
4422 		 * so that CP could perform IDLE-SAVE during switch. Those
4423 		 * steps are necessary to avoid a DMAR error in gfx9 but it is
4424 		 * not reproduced on gfx11.
4425 		 */
4426 		return 0;
4427 
4428 	gfx_v11_0_cp_enable(adev, false);
4429 	gfx_v11_0_enable_gui_idle_interrupt(adev, false);
4430 
4431 	adev->gfxhub.funcs->gart_disable(adev);
4432 
4433 	adev->gfx.is_poweron = false;
4434 
4435 	return 0;
4436 }
4437 
4438 static int gfx_v11_0_suspend(void *handle)
4439 {
4440 	return gfx_v11_0_hw_fini(handle);
4441 }
4442 
4443 static int gfx_v11_0_resume(void *handle)
4444 {
4445 	return gfx_v11_0_hw_init(handle);
4446 }
4447 
4448 static bool gfx_v11_0_is_idle(void *handle)
4449 {
4450 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4451 
4452 	if (REG_GET_FIELD(RREG32_SOC15(GC, 0, regGRBM_STATUS),
4453 				GRBM_STATUS, GUI_ACTIVE))
4454 		return false;
4455 	else
4456 		return true;
4457 }
4458 
4459 static int gfx_v11_0_wait_for_idle(void *handle)
4460 {
4461 	unsigned i;
4462 	u32 tmp;
4463 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4464 
4465 	for (i = 0; i < adev->usec_timeout; i++) {
4466 		/* read MC_STATUS */
4467 		tmp = RREG32_SOC15(GC, 0, regGRBM_STATUS) &
4468 			GRBM_STATUS__GUI_ACTIVE_MASK;
4469 
4470 		if (!REG_GET_FIELD(tmp, GRBM_STATUS, GUI_ACTIVE))
4471 			return 0;
4472 		udelay(1);
4473 	}
4474 	return -ETIMEDOUT;
4475 }
4476 
4477 static int gfx_v11_0_request_gfx_index_mutex(struct amdgpu_device *adev,
4478 					     int req)
4479 {
4480 	u32 i, tmp, val;
4481 
4482 	for (i = 0; i < adev->usec_timeout; i++) {
4483 		/* Request with MeId=2, PipeId=0 */
4484 		tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, req);
4485 		tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
4486 		WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
4487 
4488 		val = RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX);
4489 		if (req) {
4490 			if (val == tmp)
4491 				break;
4492 		} else {
4493 			tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX,
4494 					    REQUEST, 1);
4495 
4496 			/* unlocked or locked by firmware */
4497 			if (val != tmp)
4498 				break;
4499 		}
4500 		udelay(1);
4501 	}
4502 
4503 	if (i >= adev->usec_timeout)
4504 		return -EINVAL;
4505 
4506 	return 0;
4507 }
4508 
4509 static int gfx_v11_0_soft_reset(void *handle)
4510 {
4511 	u32 grbm_soft_reset = 0;
4512 	u32 tmp;
4513 	int r, i, j, k;
4514 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4515 
4516 	tmp = RREG32_SOC15(GC, 0, regCP_INT_CNTL);
4517 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, CMP_BUSY_INT_ENABLE, 0);
4518 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, CNTX_BUSY_INT_ENABLE, 0);
4519 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, CNTX_EMPTY_INT_ENABLE, 0);
4520 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, GFX_IDLE_INT_ENABLE, 0);
4521 	WREG32_SOC15(GC, 0, regCP_INT_CNTL, tmp);
4522 
4523 	gfx_v11_0_set_safe_mode(adev, 0);
4524 
4525 	for (i = 0; i < adev->gfx.mec.num_mec; ++i) {
4526 		for (j = 0; j < adev->gfx.mec.num_queue_per_pipe; j++) {
4527 			for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
4528 				tmp = RREG32_SOC15(GC, 0, regGRBM_GFX_CNTL);
4529 				tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, MEID, i);
4530 				tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, QUEUEID, j);
4531 				tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, PIPEID, k);
4532 				WREG32_SOC15(GC, 0, regGRBM_GFX_CNTL, tmp);
4533 
4534 				WREG32_SOC15(GC, 0, regCP_HQD_DEQUEUE_REQUEST, 0x2);
4535 				WREG32_SOC15(GC, 0, regSPI_COMPUTE_QUEUE_RESET, 0x1);
4536 			}
4537 		}
4538 	}
4539 	for (i = 0; i < adev->gfx.me.num_me; ++i) {
4540 		for (j = 0; j < adev->gfx.me.num_queue_per_pipe; j++) {
4541 			for (k = 0; k < adev->gfx.me.num_pipe_per_me; k++) {
4542 				tmp = RREG32_SOC15(GC, 0, regGRBM_GFX_CNTL);
4543 				tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, MEID, i);
4544 				tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, QUEUEID, j);
4545 				tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, PIPEID, k);
4546 				WREG32_SOC15(GC, 0, regGRBM_GFX_CNTL, tmp);
4547 
4548 				WREG32_SOC15(GC, 0, regCP_GFX_HQD_DEQUEUE_REQUEST, 0x1);
4549 			}
4550 		}
4551 	}
4552 
4553 	/* Try to acquire the gfx mutex before access to CP_VMID_RESET */
4554 	r = gfx_v11_0_request_gfx_index_mutex(adev, 1);
4555 	if (r) {
4556 		DRM_ERROR("Failed to acquire the gfx mutex during soft reset\n");
4557 		return r;
4558 	}
4559 
4560 	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
4561 
4562 	// Read CP_VMID_RESET register three times.
4563 	// to get sufficient time for GFX_HQD_ACTIVE reach 0
4564 	RREG32_SOC15(GC, 0, regCP_VMID_RESET);
4565 	RREG32_SOC15(GC, 0, regCP_VMID_RESET);
4566 	RREG32_SOC15(GC, 0, regCP_VMID_RESET);
4567 
4568 	/* release the gfx mutex */
4569 	r = gfx_v11_0_request_gfx_index_mutex(adev, 0);
4570 	if (r) {
4571 		DRM_ERROR("Failed to release the gfx mutex during soft reset\n");
4572 		return r;
4573 	}
4574 
4575 	for (i = 0; i < adev->usec_timeout; i++) {
4576 		if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
4577 		    !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))
4578 			break;
4579 		udelay(1);
4580 	}
4581 	if (i >= adev->usec_timeout) {
4582 		printk("Failed to wait all pipes clean\n");
4583 		return -EINVAL;
4584 	}
4585 
4586 	/**********  trigger soft reset  ***********/
4587 	grbm_soft_reset = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
4588 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4589 					SOFT_RESET_CP, 1);
4590 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4591 					SOFT_RESET_GFX, 1);
4592 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4593 					SOFT_RESET_CPF, 1);
4594 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4595 					SOFT_RESET_CPC, 1);
4596 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4597 					SOFT_RESET_CPG, 1);
4598 	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, grbm_soft_reset);
4599 	/**********  exit soft reset  ***********/
4600 	grbm_soft_reset = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
4601 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4602 					SOFT_RESET_CP, 0);
4603 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4604 					SOFT_RESET_GFX, 0);
4605 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4606 					SOFT_RESET_CPF, 0);
4607 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4608 					SOFT_RESET_CPC, 0);
4609 	grbm_soft_reset = REG_SET_FIELD(grbm_soft_reset, GRBM_SOFT_RESET,
4610 					SOFT_RESET_CPG, 0);
4611 	WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, grbm_soft_reset);
4612 
4613 	tmp = RREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL);
4614 	tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, CMP_HQD_REG_RESET, 0x1);
4615 	WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
4616 
4617 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, 0x0);
4618 	WREG32_SOC15(GC, 0, regCP_MEC_RS64_CNTL, 0x0);
4619 
4620 	for (i = 0; i < adev->usec_timeout; i++) {
4621 		if (!RREG32_SOC15(GC, 0, regCP_VMID_RESET))
4622 			break;
4623 		udelay(1);
4624 	}
4625 	if (i >= adev->usec_timeout) {
4626 		printk("Failed to wait CP_VMID_RESET to 0\n");
4627 		return -EINVAL;
4628 	}
4629 
4630 	tmp = RREG32_SOC15(GC, 0, regCP_INT_CNTL);
4631 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, CMP_BUSY_INT_ENABLE, 1);
4632 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, CNTX_BUSY_INT_ENABLE, 1);
4633 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, CNTX_EMPTY_INT_ENABLE, 1);
4634 	tmp = REG_SET_FIELD(tmp, CP_INT_CNTL, GFX_IDLE_INT_ENABLE, 1);
4635 	WREG32_SOC15(GC, 0, regCP_INT_CNTL, tmp);
4636 
4637 	gfx_v11_0_unset_safe_mode(adev, 0);
4638 
4639 	return gfx_v11_0_cp_resume(adev);
4640 }
4641 
4642 static bool gfx_v11_0_check_soft_reset(void *handle)
4643 {
4644 	int i, r;
4645 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4646 	struct amdgpu_ring *ring;
4647 	long tmo = msecs_to_jiffies(1000);
4648 
4649 	for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
4650 		ring = &adev->gfx.gfx_ring[i];
4651 		r = amdgpu_ring_test_ib(ring, tmo);
4652 		if (r)
4653 			return true;
4654 	}
4655 
4656 	for (i = 0; i < adev->gfx.num_compute_rings; i++) {
4657 		ring = &adev->gfx.compute_ring[i];
4658 		r = amdgpu_ring_test_ib(ring, tmo);
4659 		if (r)
4660 			return true;
4661 	}
4662 
4663 	return false;
4664 }
4665 
4666 static int gfx_v11_0_post_soft_reset(void *handle)
4667 {
4668 	/**
4669 	 * GFX soft reset will impact MES, need resume MES when do GFX soft reset
4670 	 */
4671 	return amdgpu_mes_resume((struct amdgpu_device *)handle);
4672 }
4673 
4674 static uint64_t gfx_v11_0_get_gpu_clock_counter(struct amdgpu_device *adev)
4675 {
4676 	uint64_t clock;
4677 	uint64_t clock_counter_lo, clock_counter_hi_pre, clock_counter_hi_after;
4678 
4679 	if (amdgpu_sriov_vf(adev)) {
4680 		amdgpu_gfx_off_ctrl(adev, false);
4681 		mutex_lock(&adev->gfx.gpu_clock_mutex);
4682 		clock_counter_hi_pre = (uint64_t)RREG32_SOC15(GC, 0, regCP_MES_MTIME_HI);
4683 		clock_counter_lo = (uint64_t)RREG32_SOC15(GC, 0, regCP_MES_MTIME_LO);
4684 		clock_counter_hi_after = (uint64_t)RREG32_SOC15(GC, 0, regCP_MES_MTIME_HI);
4685 		if (clock_counter_hi_pre != clock_counter_hi_after)
4686 			clock_counter_lo = (uint64_t)RREG32_SOC15(GC, 0, regCP_MES_MTIME_LO);
4687 		mutex_unlock(&adev->gfx.gpu_clock_mutex);
4688 		amdgpu_gfx_off_ctrl(adev, true);
4689 	} else {
4690 		preempt_disable();
4691 		clock_counter_hi_pre = (uint64_t)RREG32_SOC15(SMUIO, 0, regGOLDEN_TSC_COUNT_UPPER);
4692 		clock_counter_lo = (uint64_t)RREG32_SOC15(SMUIO, 0, regGOLDEN_TSC_COUNT_LOWER);
4693 		clock_counter_hi_after = (uint64_t)RREG32_SOC15(SMUIO, 0, regGOLDEN_TSC_COUNT_UPPER);
4694 		if (clock_counter_hi_pre != clock_counter_hi_after)
4695 			clock_counter_lo = (uint64_t)RREG32_SOC15(SMUIO, 0, regGOLDEN_TSC_COUNT_LOWER);
4696 		preempt_enable();
4697 	}
4698 	clock = clock_counter_lo | (clock_counter_hi_after << 32ULL);
4699 
4700 	return clock;
4701 }
4702 
4703 static void gfx_v11_0_ring_emit_gds_switch(struct amdgpu_ring *ring,
4704 					   uint32_t vmid,
4705 					   uint32_t gds_base, uint32_t gds_size,
4706 					   uint32_t gws_base, uint32_t gws_size,
4707 					   uint32_t oa_base, uint32_t oa_size)
4708 {
4709 	struct amdgpu_device *adev = ring->adev;
4710 
4711 	/* GDS Base */
4712 	gfx_v11_0_write_data_to_reg(ring, 0, false,
4713 				    SOC15_REG_OFFSET(GC, 0, regGDS_VMID0_BASE) + 2 * vmid,
4714 				    gds_base);
4715 
4716 	/* GDS Size */
4717 	gfx_v11_0_write_data_to_reg(ring, 0, false,
4718 				    SOC15_REG_OFFSET(GC, 0, regGDS_VMID0_SIZE) + 2 * vmid,
4719 				    gds_size);
4720 
4721 	/* GWS */
4722 	gfx_v11_0_write_data_to_reg(ring, 0, false,
4723 				    SOC15_REG_OFFSET(GC, 0, regGDS_GWS_VMID0) + vmid,
4724 				    gws_size << GDS_GWS_VMID0__SIZE__SHIFT | gws_base);
4725 
4726 	/* OA */
4727 	gfx_v11_0_write_data_to_reg(ring, 0, false,
4728 				    SOC15_REG_OFFSET(GC, 0, regGDS_OA_VMID0) + vmid,
4729 				    (1 << (oa_size + oa_base)) - (1 << oa_base));
4730 }
4731 
4732 static int gfx_v11_0_early_init(void *handle)
4733 {
4734 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4735 
4736 	adev->gfx.funcs = &gfx_v11_0_gfx_funcs;
4737 
4738 	adev->gfx.num_gfx_rings = GFX11_NUM_GFX_RINGS;
4739 	adev->gfx.num_compute_rings = min(amdgpu_gfx_get_num_kcq(adev),
4740 					  AMDGPU_MAX_COMPUTE_RINGS);
4741 
4742 	gfx_v11_0_set_kiq_pm4_funcs(adev);
4743 	gfx_v11_0_set_ring_funcs(adev);
4744 	gfx_v11_0_set_irq_funcs(adev);
4745 	gfx_v11_0_set_gds_init(adev);
4746 	gfx_v11_0_set_rlc_funcs(adev);
4747 	gfx_v11_0_set_mqd_funcs(adev);
4748 	gfx_v11_0_set_imu_funcs(adev);
4749 
4750 	gfx_v11_0_init_rlcg_reg_access_ctrl(adev);
4751 
4752 	return gfx_v11_0_init_microcode(adev);
4753 }
4754 
4755 static int gfx_v11_0_late_init(void *handle)
4756 {
4757 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4758 	int r;
4759 
4760 	r = amdgpu_irq_get(adev, &adev->gfx.priv_reg_irq, 0);
4761 	if (r)
4762 		return r;
4763 
4764 	r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
4765 	if (r)
4766 		return r;
4767 
4768 	return 0;
4769 }
4770 
4771 static bool gfx_v11_0_is_rlc_enabled(struct amdgpu_device *adev)
4772 {
4773 	uint32_t rlc_cntl;
4774 
4775 	/* if RLC is not enabled, do nothing */
4776 	rlc_cntl = RREG32_SOC15(GC, 0, regRLC_CNTL);
4777 	return (REG_GET_FIELD(rlc_cntl, RLC_CNTL, RLC_ENABLE_F32)) ? true : false;
4778 }
4779 
4780 static void gfx_v11_0_set_safe_mode(struct amdgpu_device *adev, int xcc_id)
4781 {
4782 	uint32_t data;
4783 	unsigned i;
4784 
4785 	data = RLC_SAFE_MODE__CMD_MASK;
4786 	data |= (1 << RLC_SAFE_MODE__MESSAGE__SHIFT);
4787 
4788 	WREG32_SOC15(GC, 0, regRLC_SAFE_MODE, data);
4789 
4790 	/* wait for RLC_SAFE_MODE */
4791 	for (i = 0; i < adev->usec_timeout; i++) {
4792 		if (!REG_GET_FIELD(RREG32_SOC15(GC, 0, regRLC_SAFE_MODE),
4793 				   RLC_SAFE_MODE, CMD))
4794 			break;
4795 		udelay(1);
4796 	}
4797 }
4798 
4799 static void gfx_v11_0_unset_safe_mode(struct amdgpu_device *adev, int xcc_id)
4800 {
4801 	WREG32_SOC15(GC, 0, regRLC_SAFE_MODE, RLC_SAFE_MODE__CMD_MASK);
4802 }
4803 
4804 static void gfx_v11_0_update_perf_clk(struct amdgpu_device *adev,
4805 				      bool enable)
4806 {
4807 	uint32_t def, data;
4808 
4809 	if (!(adev->cg_flags & AMD_CG_SUPPORT_GFX_PERF_CLK))
4810 		return;
4811 
4812 	def = data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
4813 
4814 	if (enable)
4815 		data &= ~RLC_CGTT_MGCG_OVERRIDE__PERFMON_CLOCK_STATE_MASK;
4816 	else
4817 		data |= RLC_CGTT_MGCG_OVERRIDE__PERFMON_CLOCK_STATE_MASK;
4818 
4819 	if (def != data)
4820 		WREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE, data);
4821 }
4822 
4823 static void gfx_v11_0_update_sram_fgcg(struct amdgpu_device *adev,
4824 				       bool enable)
4825 {
4826 	uint32_t def, data;
4827 
4828 	if (!(adev->cg_flags & AMD_CG_SUPPORT_GFX_FGCG))
4829 		return;
4830 
4831 	def = data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
4832 
4833 	if (enable)
4834 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_FGCG_OVERRIDE_MASK;
4835 	else
4836 		data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_FGCG_OVERRIDE_MASK;
4837 
4838 	if (def != data)
4839 		WREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE, data);
4840 }
4841 
4842 static void gfx_v11_0_update_repeater_fgcg(struct amdgpu_device *adev,
4843 					   bool enable)
4844 {
4845 	uint32_t def, data;
4846 
4847 	if (!(adev->cg_flags & AMD_CG_SUPPORT_REPEATER_FGCG))
4848 		return;
4849 
4850 	def = data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
4851 
4852 	if (enable)
4853 		data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_REPEATER_FGCG_OVERRIDE_MASK;
4854 	else
4855 		data |= RLC_CGTT_MGCG_OVERRIDE__GFXIP_REPEATER_FGCG_OVERRIDE_MASK;
4856 
4857 	if (def != data)
4858 		WREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE, data);
4859 }
4860 
4861 static void gfx_v11_0_update_medium_grain_clock_gating(struct amdgpu_device *adev,
4862 						       bool enable)
4863 {
4864 	uint32_t data, def;
4865 
4866 	if (!(adev->cg_flags & (AMD_CG_SUPPORT_GFX_MGCG | AMD_CG_SUPPORT_GFX_MGLS)))
4867 		return;
4868 
4869 	/* It is disabled by HW by default */
4870 	if (enable) {
4871 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGCG) {
4872 			/* 1 - RLC_CGTT_MGCG_OVERRIDE */
4873 			def = data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
4874 
4875 			data &= ~(RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
4876 				  RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK |
4877 				  RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK);
4878 
4879 			if (def != data)
4880 				WREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE, data);
4881 		}
4882 	} else {
4883 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_MGCG) {
4884 			def = data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
4885 
4886 			data |= (RLC_CGTT_MGCG_OVERRIDE__RLC_CGTT_SCLK_OVERRIDE_MASK |
4887 				 RLC_CGTT_MGCG_OVERRIDE__GRBM_CGTT_SCLK_OVERRIDE_MASK |
4888 				 RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK);
4889 
4890 			if (def != data)
4891 				WREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE, data);
4892 		}
4893 	}
4894 }
4895 
4896 static void gfx_v11_0_update_coarse_grain_clock_gating(struct amdgpu_device *adev,
4897 						       bool enable)
4898 {
4899 	uint32_t def, data;
4900 
4901 	if (!(adev->cg_flags &
4902 	      (AMD_CG_SUPPORT_GFX_CGCG |
4903 	      AMD_CG_SUPPORT_GFX_CGLS |
4904 	      AMD_CG_SUPPORT_GFX_3D_CGCG |
4905 	      AMD_CG_SUPPORT_GFX_3D_CGLS)))
4906 		return;
4907 
4908 	if (enable) {
4909 		def = data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
4910 
4911 		/* unset CGCG override */
4912 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGCG)
4913 			data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGCG_OVERRIDE_MASK;
4914 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
4915 			data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_CGLS_OVERRIDE_MASK;
4916 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGCG ||
4917 		    adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGLS)
4918 			data &= ~RLC_CGTT_MGCG_OVERRIDE__GFXIP_GFX3D_CG_OVERRIDE_MASK;
4919 
4920 		/* update CGCG override bits */
4921 		if (def != data)
4922 			WREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE, data);
4923 
4924 		/* enable cgcg FSM(0x0000363F) */
4925 		def = data = RREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL);
4926 
4927 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGCG) {
4928 			data &= ~RLC_CGCG_CGLS_CTRL__CGCG_GFX_IDLE_THRESHOLD_MASK;
4929 			data |= (0x36 << RLC_CGCG_CGLS_CTRL__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
4930 				 RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK;
4931 		}
4932 
4933 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS) {
4934 			data &= ~RLC_CGCG_CGLS_CTRL__CGLS_REP_COMPANSAT_DELAY_MASK;
4935 			data |= (0x000F << RLC_CGCG_CGLS_CTRL__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
4936 				 RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK;
4937 		}
4938 
4939 		if (def != data)
4940 			WREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL, data);
4941 
4942 		/* Program RLC_CGCG_CGLS_CTRL_3D */
4943 		def = data = RREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL_3D);
4944 
4945 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGCG) {
4946 			data &= ~RLC_CGCG_CGLS_CTRL_3D__CGCG_GFX_IDLE_THRESHOLD_MASK;
4947 			data |= (0x36 << RLC_CGCG_CGLS_CTRL_3D__CGCG_GFX_IDLE_THRESHOLD__SHIFT) |
4948 				 RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK;
4949 		}
4950 
4951 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGLS) {
4952 			data &= ~RLC_CGCG_CGLS_CTRL_3D__CGLS_REP_COMPANSAT_DELAY_MASK;
4953 			data |= (0xf << RLC_CGCG_CGLS_CTRL_3D__CGLS_REP_COMPANSAT_DELAY__SHIFT) |
4954 				 RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK;
4955 		}
4956 
4957 		if (def != data)
4958 			WREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL_3D, data);
4959 
4960 		/* set IDLE_POLL_COUNT(0x00900100) */
4961 		def = data = RREG32_SOC15(GC, 0, regCP_RB_WPTR_POLL_CNTL);
4962 
4963 		data &= ~(CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY_MASK | CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT_MASK);
4964 		data |= (0x0100 << CP_RB_WPTR_POLL_CNTL__POLL_FREQUENCY__SHIFT) |
4965 			(0x0090 << CP_RB_WPTR_POLL_CNTL__IDLE_POLL_COUNT__SHIFT);
4966 
4967 		if (def != data)
4968 			WREG32_SOC15(GC, 0, regCP_RB_WPTR_POLL_CNTL, data);
4969 
4970 		data = RREG32_SOC15(GC, 0, regCP_INT_CNTL);
4971 		data = REG_SET_FIELD(data, CP_INT_CNTL, CNTX_BUSY_INT_ENABLE, 1);
4972 		data = REG_SET_FIELD(data, CP_INT_CNTL, CNTX_EMPTY_INT_ENABLE, 1);
4973 		data = REG_SET_FIELD(data, CP_INT_CNTL, CMP_BUSY_INT_ENABLE, 1);
4974 		data = REG_SET_FIELD(data, CP_INT_CNTL, GFX_IDLE_INT_ENABLE, 1);
4975 		WREG32_SOC15(GC, 0, regCP_INT_CNTL, data);
4976 
4977 		data = RREG32_SOC15(GC, 0, regSDMA0_RLC_CGCG_CTRL);
4978 		data = REG_SET_FIELD(data, SDMA0_RLC_CGCG_CTRL, CGCG_INT_ENABLE, 1);
4979 		WREG32_SOC15(GC, 0, regSDMA0_RLC_CGCG_CTRL, data);
4980 
4981 		/* Some ASICs only have one SDMA instance, not need to configure SDMA1 */
4982 		if (adev->sdma.num_instances > 1) {
4983 			data = RREG32_SOC15(GC, 0, regSDMA1_RLC_CGCG_CTRL);
4984 			data = REG_SET_FIELD(data, SDMA1_RLC_CGCG_CTRL, CGCG_INT_ENABLE, 1);
4985 			WREG32_SOC15(GC, 0, regSDMA1_RLC_CGCG_CTRL, data);
4986 		}
4987 	} else {
4988 		/* Program RLC_CGCG_CGLS_CTRL */
4989 		def = data = RREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL);
4990 
4991 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGCG)
4992 			data &= ~RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK;
4993 
4994 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_CGLS)
4995 			data &= ~RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK;
4996 
4997 		if (def != data)
4998 			WREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL, data);
4999 
5000 		/* Program RLC_CGCG_CGLS_CTRL_3D */
5001 		def = data = RREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL_3D);
5002 
5003 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGCG)
5004 			data &= ~RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK;
5005 		if (adev->cg_flags & AMD_CG_SUPPORT_GFX_3D_CGLS)
5006 			data &= ~RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK;
5007 
5008 		if (def != data)
5009 			WREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL_3D, data);
5010 
5011 		data = RREG32_SOC15(GC, 0, regSDMA0_RLC_CGCG_CTRL);
5012 		data &= ~SDMA0_RLC_CGCG_CTRL__CGCG_INT_ENABLE_MASK;
5013 		WREG32_SOC15(GC, 0, regSDMA0_RLC_CGCG_CTRL, data);
5014 
5015 		/* Some ASICs only have one SDMA instance, not need to configure SDMA1 */
5016 		if (adev->sdma.num_instances > 1) {
5017 			data = RREG32_SOC15(GC, 0, regSDMA1_RLC_CGCG_CTRL);
5018 			data &= ~SDMA1_RLC_CGCG_CTRL__CGCG_INT_ENABLE_MASK;
5019 			WREG32_SOC15(GC, 0, regSDMA1_RLC_CGCG_CTRL, data);
5020 		}
5021 	}
5022 }
5023 
5024 static int gfx_v11_0_update_gfx_clock_gating(struct amdgpu_device *adev,
5025 					    bool enable)
5026 {
5027 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
5028 
5029 	gfx_v11_0_update_coarse_grain_clock_gating(adev, enable);
5030 
5031 	gfx_v11_0_update_medium_grain_clock_gating(adev, enable);
5032 
5033 	gfx_v11_0_update_repeater_fgcg(adev, enable);
5034 
5035 	gfx_v11_0_update_sram_fgcg(adev, enable);
5036 
5037 	gfx_v11_0_update_perf_clk(adev, enable);
5038 
5039 	if (adev->cg_flags &
5040 	    (AMD_CG_SUPPORT_GFX_MGCG |
5041 	     AMD_CG_SUPPORT_GFX_CGLS |
5042 	     AMD_CG_SUPPORT_GFX_CGCG |
5043 	     AMD_CG_SUPPORT_GFX_3D_CGCG |
5044 	     AMD_CG_SUPPORT_GFX_3D_CGLS))
5045 	        gfx_v11_0_enable_gui_idle_interrupt(adev, enable);
5046 
5047 	amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
5048 
5049 	return 0;
5050 }
5051 
5052 static void gfx_v11_0_update_spm_vmid(struct amdgpu_device *adev, unsigned vmid)
5053 {
5054 	u32 data;
5055 
5056 	amdgpu_gfx_off_ctrl(adev, false);
5057 
5058 	data = RREG32_SOC15_NO_KIQ(GC, 0, regRLC_SPM_MC_CNTL);
5059 
5060 	data &= ~RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK;
5061 	data |= (vmid & RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK) << RLC_SPM_MC_CNTL__RLC_SPM_VMID__SHIFT;
5062 
5063 	WREG32_SOC15_NO_KIQ(GC, 0, regRLC_SPM_MC_CNTL, data);
5064 
5065 	amdgpu_gfx_off_ctrl(adev, true);
5066 }
5067 
5068 static const struct amdgpu_rlc_funcs gfx_v11_0_rlc_funcs = {
5069 	.is_rlc_enabled = gfx_v11_0_is_rlc_enabled,
5070 	.set_safe_mode = gfx_v11_0_set_safe_mode,
5071 	.unset_safe_mode = gfx_v11_0_unset_safe_mode,
5072 	.init = gfx_v11_0_rlc_init,
5073 	.get_csb_size = gfx_v11_0_get_csb_size,
5074 	.get_csb_buffer = gfx_v11_0_get_csb_buffer,
5075 	.resume = gfx_v11_0_rlc_resume,
5076 	.stop = gfx_v11_0_rlc_stop,
5077 	.reset = gfx_v11_0_rlc_reset,
5078 	.start = gfx_v11_0_rlc_start,
5079 	.update_spm_vmid = gfx_v11_0_update_spm_vmid,
5080 };
5081 
5082 static void gfx_v11_cntl_power_gating(struct amdgpu_device *adev, bool enable)
5083 {
5084 	u32 data = RREG32_SOC15(GC, 0, regRLC_PG_CNTL);
5085 
5086 	if (enable && (adev->pg_flags & AMD_PG_SUPPORT_GFX_PG))
5087 		data |= RLC_PG_CNTL__GFX_POWER_GATING_ENABLE_MASK;
5088 	else
5089 		data &= ~RLC_PG_CNTL__GFX_POWER_GATING_ENABLE_MASK;
5090 
5091 	WREG32_SOC15(GC, 0, regRLC_PG_CNTL, data);
5092 
5093 	// Program RLC_PG_DELAY3 for CGPG hysteresis
5094 	if (enable && (adev->pg_flags & AMD_PG_SUPPORT_GFX_PG)) {
5095 		switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
5096 		case IP_VERSION(11, 0, 1):
5097 		case IP_VERSION(11, 0, 4):
5098 		case IP_VERSION(11, 5, 0):
5099 			WREG32_SOC15(GC, 0, regRLC_PG_DELAY_3, RLC_PG_DELAY_3_DEFAULT_GC_11_0_1);
5100 			break;
5101 		default:
5102 			break;
5103 		}
5104 	}
5105 }
5106 
5107 static void gfx_v11_cntl_pg(struct amdgpu_device *adev, bool enable)
5108 {
5109 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
5110 
5111 	gfx_v11_cntl_power_gating(adev, enable);
5112 
5113 	amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
5114 }
5115 
5116 static int gfx_v11_0_set_powergating_state(void *handle,
5117 					   enum amd_powergating_state state)
5118 {
5119 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
5120 	bool enable = (state == AMD_PG_STATE_GATE);
5121 
5122 	if (amdgpu_sriov_vf(adev))
5123 		return 0;
5124 
5125 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
5126 	case IP_VERSION(11, 0, 0):
5127 	case IP_VERSION(11, 0, 2):
5128 	case IP_VERSION(11, 0, 3):
5129 		amdgpu_gfx_off_ctrl(adev, enable);
5130 		break;
5131 	case IP_VERSION(11, 0, 1):
5132 	case IP_VERSION(11, 0, 4):
5133 	case IP_VERSION(11, 5, 0):
5134 		if (!enable)
5135 			amdgpu_gfx_off_ctrl(adev, false);
5136 
5137 		gfx_v11_cntl_pg(adev, enable);
5138 
5139 		if (enable)
5140 			amdgpu_gfx_off_ctrl(adev, true);
5141 
5142 		break;
5143 	default:
5144 		break;
5145 	}
5146 
5147 	return 0;
5148 }
5149 
5150 static int gfx_v11_0_set_clockgating_state(void *handle,
5151 					  enum amd_clockgating_state state)
5152 {
5153 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
5154 
5155 	if (amdgpu_sriov_vf(adev))
5156 	        return 0;
5157 
5158 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
5159 	case IP_VERSION(11, 0, 0):
5160 	case IP_VERSION(11, 0, 1):
5161 	case IP_VERSION(11, 0, 2):
5162 	case IP_VERSION(11, 0, 3):
5163 	case IP_VERSION(11, 0, 4):
5164 	case IP_VERSION(11, 5, 0):
5165 	        gfx_v11_0_update_gfx_clock_gating(adev,
5166 	                        state ==  AMD_CG_STATE_GATE);
5167 	        break;
5168 	default:
5169 	        break;
5170 	}
5171 
5172 	return 0;
5173 }
5174 
5175 static void gfx_v11_0_get_clockgating_state(void *handle, u64 *flags)
5176 {
5177 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
5178 	int data;
5179 
5180 	/* AMD_CG_SUPPORT_GFX_MGCG */
5181 	data = RREG32_SOC15(GC, 0, regRLC_CGTT_MGCG_OVERRIDE);
5182 	if (!(data & RLC_CGTT_MGCG_OVERRIDE__GFXIP_MGCG_OVERRIDE_MASK))
5183 		*flags |= AMD_CG_SUPPORT_GFX_MGCG;
5184 
5185 	/* AMD_CG_SUPPORT_REPEATER_FGCG */
5186 	if (!(data & RLC_CGTT_MGCG_OVERRIDE__GFXIP_REPEATER_FGCG_OVERRIDE_MASK))
5187 		*flags |= AMD_CG_SUPPORT_REPEATER_FGCG;
5188 
5189 	/* AMD_CG_SUPPORT_GFX_FGCG */
5190 	if (!(data & RLC_CGTT_MGCG_OVERRIDE__GFXIP_FGCG_OVERRIDE_MASK))
5191 		*flags |= AMD_CG_SUPPORT_GFX_FGCG;
5192 
5193 	/* AMD_CG_SUPPORT_GFX_PERF_CLK */
5194 	if (!(data & RLC_CGTT_MGCG_OVERRIDE__PERFMON_CLOCK_STATE_MASK))
5195 		*flags |= AMD_CG_SUPPORT_GFX_PERF_CLK;
5196 
5197 	/* AMD_CG_SUPPORT_GFX_CGCG */
5198 	data = RREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL);
5199 	if (data & RLC_CGCG_CGLS_CTRL__CGCG_EN_MASK)
5200 		*flags |= AMD_CG_SUPPORT_GFX_CGCG;
5201 
5202 	/* AMD_CG_SUPPORT_GFX_CGLS */
5203 	if (data & RLC_CGCG_CGLS_CTRL__CGLS_EN_MASK)
5204 		*flags |= AMD_CG_SUPPORT_GFX_CGLS;
5205 
5206 	/* AMD_CG_SUPPORT_GFX_3D_CGCG */
5207 	data = RREG32_SOC15(GC, 0, regRLC_CGCG_CGLS_CTRL_3D);
5208 	if (data & RLC_CGCG_CGLS_CTRL_3D__CGCG_EN_MASK)
5209 		*flags |= AMD_CG_SUPPORT_GFX_3D_CGCG;
5210 
5211 	/* AMD_CG_SUPPORT_GFX_3D_CGLS */
5212 	if (data & RLC_CGCG_CGLS_CTRL_3D__CGLS_EN_MASK)
5213 		*flags |= AMD_CG_SUPPORT_GFX_3D_CGLS;
5214 }
5215 
5216 static u64 gfx_v11_0_ring_get_rptr_gfx(struct amdgpu_ring *ring)
5217 {
5218 	/* gfx11 is 32bit rptr*/
5219 	return *(uint32_t *)ring->rptr_cpu_addr;
5220 }
5221 
5222 static u64 gfx_v11_0_ring_get_wptr_gfx(struct amdgpu_ring *ring)
5223 {
5224 	struct amdgpu_device *adev = ring->adev;
5225 	u64 wptr;
5226 
5227 	/* XXX check if swapping is necessary on BE */
5228 	if (ring->use_doorbell) {
5229 		wptr = atomic64_read((atomic64_t *)ring->wptr_cpu_addr);
5230 	} else {
5231 		wptr = RREG32_SOC15(GC, 0, regCP_RB0_WPTR);
5232 		wptr += (u64)RREG32_SOC15(GC, 0, regCP_RB0_WPTR_HI) << 32;
5233 	}
5234 
5235 	return wptr;
5236 }
5237 
5238 static void gfx_v11_0_ring_set_wptr_gfx(struct amdgpu_ring *ring)
5239 {
5240 	struct amdgpu_device *adev = ring->adev;
5241 
5242 	if (ring->use_doorbell) {
5243 		/* XXX check if swapping is necessary on BE */
5244 		atomic64_set((atomic64_t *)ring->wptr_cpu_addr,
5245 			     ring->wptr);
5246 		WDOORBELL64(ring->doorbell_index, ring->wptr);
5247 	} else {
5248 		WREG32_SOC15(GC, 0, regCP_RB0_WPTR,
5249 			     lower_32_bits(ring->wptr));
5250 		WREG32_SOC15(GC, 0, regCP_RB0_WPTR_HI,
5251 			     upper_32_bits(ring->wptr));
5252 	}
5253 }
5254 
5255 static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
5256 {
5257 	/* gfx11 hardware is 32bit rptr */
5258 	return *(uint32_t *)ring->rptr_cpu_addr;
5259 }
5260 
5261 static u64 gfx_v11_0_ring_get_wptr_compute(struct amdgpu_ring *ring)
5262 {
5263 	u64 wptr;
5264 
5265 	/* XXX check if swapping is necessary on BE */
5266 	if (ring->use_doorbell)
5267 		wptr = atomic64_read((atomic64_t *)ring->wptr_cpu_addr);
5268 	else
5269 		BUG();
5270 	return wptr;
5271 }
5272 
5273 static void gfx_v11_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
5274 {
5275 	struct amdgpu_device *adev = ring->adev;
5276 
5277 	/* XXX check if swapping is necessary on BE */
5278 	if (ring->use_doorbell) {
5279 		atomic64_set((atomic64_t *)ring->wptr_cpu_addr,
5280 			     ring->wptr);
5281 		WDOORBELL64(ring->doorbell_index, ring->wptr);
5282 	} else {
5283 		BUG(); /* only DOORBELL method supported on gfx11 now */
5284 	}
5285 }
5286 
5287 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
5288 {
5289 	struct amdgpu_device *adev = ring->adev;
5290 	u32 ref_and_mask, reg_mem_engine;
5291 	const struct nbio_hdp_flush_reg *nbio_hf_reg = adev->nbio.hdp_flush_reg;
5292 
5293 	if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) {
5294 		switch (ring->me) {
5295 		case 1:
5296 			ref_and_mask = nbio_hf_reg->ref_and_mask_cp2 << ring->pipe;
5297 			break;
5298 		case 2:
5299 			ref_and_mask = nbio_hf_reg->ref_and_mask_cp6 << ring->pipe;
5300 			break;
5301 		default:
5302 			return;
5303 		}
5304 		reg_mem_engine = 0;
5305 	} else {
5306 		ref_and_mask = nbio_hf_reg->ref_and_mask_cp0;
5307 		reg_mem_engine = 1; /* pfp */
5308 	}
5309 
5310 	gfx_v11_0_wait_reg_mem(ring, reg_mem_engine, 0, 1,
5311 			       adev->nbio.funcs->get_hdp_flush_req_offset(adev),
5312 			       adev->nbio.funcs->get_hdp_flush_done_offset(adev),
5313 			       ref_and_mask, ref_and_mask, 0x20);
5314 }
5315 
5316 static void gfx_v11_0_ring_emit_ib_gfx(struct amdgpu_ring *ring,
5317 				       struct amdgpu_job *job,
5318 				       struct amdgpu_ib *ib,
5319 				       uint32_t flags)
5320 {
5321 	unsigned vmid = AMDGPU_JOB_GET_VMID(job);
5322 	u32 header, control = 0;
5323 
5324 	BUG_ON(ib->flags & AMDGPU_IB_FLAG_CE);
5325 
5326 	header = PACKET3(PACKET3_INDIRECT_BUFFER, 2);
5327 
5328 	control |= ib->length_dw | (vmid << 24);
5329 
5330 	if (ring->adev->gfx.mcbp && (ib->flags & AMDGPU_IB_FLAG_PREEMPT)) {
5331 		control |= INDIRECT_BUFFER_PRE_ENB(1);
5332 
5333 		if (flags & AMDGPU_IB_PREEMPTED)
5334 			control |= INDIRECT_BUFFER_PRE_RESUME(1);
5335 
5336 		if (vmid)
5337 			gfx_v11_0_ring_emit_de_meta(ring,
5338 				    (!amdgpu_sriov_vf(ring->adev) && flags & AMDGPU_IB_PREEMPTED) ? true : false);
5339 	}
5340 
5341 	if (ring->is_mes_queue)
5342 		/* inherit vmid from mqd */
5343 		control |= 0x400000;
5344 
5345 	amdgpu_ring_write(ring, header);
5346 	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
5347 	amdgpu_ring_write(ring,
5348 #ifdef __BIG_ENDIAN
5349 		(2 << 0) |
5350 #endif
5351 		lower_32_bits(ib->gpu_addr));
5352 	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
5353 	amdgpu_ring_write(ring, control);
5354 }
5355 
5356 static void gfx_v11_0_ring_emit_ib_compute(struct amdgpu_ring *ring,
5357 					   struct amdgpu_job *job,
5358 					   struct amdgpu_ib *ib,
5359 					   uint32_t flags)
5360 {
5361 	unsigned vmid = AMDGPU_JOB_GET_VMID(job);
5362 	u32 control = INDIRECT_BUFFER_VALID | ib->length_dw | (vmid << 24);
5363 
5364 	if (ring->is_mes_queue)
5365 		/* inherit vmid from mqd */
5366 		control |= 0x40000000;
5367 
5368 	/* Currently, there is a high possibility to get wave ID mismatch
5369 	 * between ME and GDS, leading to a hw deadlock, because ME generates
5370 	 * different wave IDs than the GDS expects. This situation happens
5371 	 * randomly when at least 5 compute pipes use GDS ordered append.
5372 	 * The wave IDs generated by ME are also wrong after suspend/resume.
5373 	 * Those are probably bugs somewhere else in the kernel driver.
5374 	 *
5375 	 * Writing GDS_COMPUTE_MAX_WAVE_ID resets wave ID counters in ME and
5376 	 * GDS to 0 for this ring (me/pipe).
5377 	 */
5378 	if (ib->flags & AMDGPU_IB_FLAG_RESET_GDS_MAX_WAVE_ID) {
5379 		amdgpu_ring_write(ring, PACKET3(PACKET3_SET_CONFIG_REG, 1));
5380 		amdgpu_ring_write(ring, regGDS_COMPUTE_MAX_WAVE_ID);
5381 		amdgpu_ring_write(ring, ring->adev->gds.gds_compute_max_wave_id);
5382 	}
5383 
5384 	amdgpu_ring_write(ring, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
5385 	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
5386 	amdgpu_ring_write(ring,
5387 #ifdef __BIG_ENDIAN
5388 				(2 << 0) |
5389 #endif
5390 				lower_32_bits(ib->gpu_addr));
5391 	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
5392 	amdgpu_ring_write(ring, control);
5393 }
5394 
5395 static void gfx_v11_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr,
5396 				     u64 seq, unsigned flags)
5397 {
5398 	bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
5399 	bool int_sel = flags & AMDGPU_FENCE_FLAG_INT;
5400 
5401 	/* RELEASE_MEM - flush caches, send int */
5402 	amdgpu_ring_write(ring, PACKET3(PACKET3_RELEASE_MEM, 6));
5403 	amdgpu_ring_write(ring, (PACKET3_RELEASE_MEM_GCR_SEQ |
5404 				 PACKET3_RELEASE_MEM_GCR_GL2_WB |
5405 				 PACKET3_RELEASE_MEM_GCR_GL2_INV |
5406 				 PACKET3_RELEASE_MEM_GCR_GL2_US |
5407 				 PACKET3_RELEASE_MEM_GCR_GL1_INV |
5408 				 PACKET3_RELEASE_MEM_GCR_GLV_INV |
5409 				 PACKET3_RELEASE_MEM_GCR_GLM_INV |
5410 				 PACKET3_RELEASE_MEM_GCR_GLM_WB |
5411 				 PACKET3_RELEASE_MEM_CACHE_POLICY(3) |
5412 				 PACKET3_RELEASE_MEM_EVENT_TYPE(CACHE_FLUSH_AND_INV_TS_EVENT) |
5413 				 PACKET3_RELEASE_MEM_EVENT_INDEX(5)));
5414 	amdgpu_ring_write(ring, (PACKET3_RELEASE_MEM_DATA_SEL(write64bit ? 2 : 1) |
5415 				 PACKET3_RELEASE_MEM_INT_SEL(int_sel ? 2 : 0)));
5416 
5417 	/*
5418 	 * the address should be Qword aligned if 64bit write, Dword
5419 	 * aligned if only send 32bit data low (discard data high)
5420 	 */
5421 	if (write64bit)
5422 		BUG_ON(addr & 0x7);
5423 	else
5424 		BUG_ON(addr & 0x3);
5425 	amdgpu_ring_write(ring, lower_32_bits(addr));
5426 	amdgpu_ring_write(ring, upper_32_bits(addr));
5427 	amdgpu_ring_write(ring, lower_32_bits(seq));
5428 	amdgpu_ring_write(ring, upper_32_bits(seq));
5429 	amdgpu_ring_write(ring, ring->is_mes_queue ?
5430 			 (ring->hw_queue_id | AMDGPU_FENCE_MES_QUEUE_FLAG) : 0);
5431 }
5432 
5433 static void gfx_v11_0_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
5434 {
5435 	int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
5436 	uint32_t seq = ring->fence_drv.sync_seq;
5437 	uint64_t addr = ring->fence_drv.gpu_addr;
5438 
5439 	gfx_v11_0_wait_reg_mem(ring, usepfp, 1, 0, lower_32_bits(addr),
5440 			       upper_32_bits(addr), seq, 0xffffffff, 4);
5441 }
5442 
5443 static void gfx_v11_0_ring_invalidate_tlbs(struct amdgpu_ring *ring,
5444 				   uint16_t pasid, uint32_t flush_type,
5445 				   bool all_hub, uint8_t dst_sel)
5446 {
5447 	amdgpu_ring_write(ring, PACKET3(PACKET3_INVALIDATE_TLBS, 0));
5448 	amdgpu_ring_write(ring,
5449 			  PACKET3_INVALIDATE_TLBS_DST_SEL(dst_sel) |
5450 			  PACKET3_INVALIDATE_TLBS_ALL_HUB(all_hub) |
5451 			  PACKET3_INVALIDATE_TLBS_PASID(pasid) |
5452 			  PACKET3_INVALIDATE_TLBS_FLUSH_TYPE(flush_type));
5453 }
5454 
5455 static void gfx_v11_0_ring_emit_vm_flush(struct amdgpu_ring *ring,
5456 					 unsigned vmid, uint64_t pd_addr)
5457 {
5458 	if (ring->is_mes_queue)
5459 		gfx_v11_0_ring_invalidate_tlbs(ring, 0, 0, false, 0);
5460 	else
5461 		amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
5462 
5463 	/* compute doesn't have PFP */
5464 	if (ring->funcs->type == AMDGPU_RING_TYPE_GFX) {
5465 		/* sync PFP to ME, otherwise we might get invalid PFP reads */
5466 		amdgpu_ring_write(ring, PACKET3(PACKET3_PFP_SYNC_ME, 0));
5467 		amdgpu_ring_write(ring, 0x0);
5468 	}
5469 }
5470 
5471 static void gfx_v11_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
5472 					  u64 seq, unsigned int flags)
5473 {
5474 	struct amdgpu_device *adev = ring->adev;
5475 
5476 	/* we only allocate 32bit for each seq wb address */
5477 	BUG_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
5478 
5479 	/* write fence seq to the "addr" */
5480 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
5481 	amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
5482 				 WRITE_DATA_DST_SEL(5) | WR_CONFIRM));
5483 	amdgpu_ring_write(ring, lower_32_bits(addr));
5484 	amdgpu_ring_write(ring, upper_32_bits(addr));
5485 	amdgpu_ring_write(ring, lower_32_bits(seq));
5486 
5487 	if (flags & AMDGPU_FENCE_FLAG_INT) {
5488 		/* set register to trigger INT */
5489 		amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
5490 		amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(0) |
5491 					 WRITE_DATA_DST_SEL(0) | WR_CONFIRM));
5492 		amdgpu_ring_write(ring, SOC15_REG_OFFSET(GC, 0, regCPC_INT_STATUS));
5493 		amdgpu_ring_write(ring, 0);
5494 		amdgpu_ring_write(ring, 0x20000000); /* src_id is 178 */
5495 	}
5496 }
5497 
5498 static void gfx_v11_0_ring_emit_cntxcntl(struct amdgpu_ring *ring,
5499 					 uint32_t flags)
5500 {
5501 	uint32_t dw2 = 0;
5502 
5503 	dw2 |= 0x80000000; /* set load_enable otherwise this package is just NOPs */
5504 	if (flags & AMDGPU_HAVE_CTX_SWITCH) {
5505 		/* set load_global_config & load_global_uconfig */
5506 		dw2 |= 0x8001;
5507 		/* set load_cs_sh_regs */
5508 		dw2 |= 0x01000000;
5509 		/* set load_per_context_state & load_gfx_sh_regs for GFX */
5510 		dw2 |= 0x10002;
5511 	}
5512 
5513 	amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
5514 	amdgpu_ring_write(ring, dw2);
5515 	amdgpu_ring_write(ring, 0);
5516 }
5517 
5518 static void gfx_v11_0_ring_emit_gfx_shadow(struct amdgpu_ring *ring,
5519 					   u64 shadow_va, u64 csa_va,
5520 					   u64 gds_va, bool init_shadow,
5521 					   int vmid)
5522 {
5523 	struct amdgpu_device *adev = ring->adev;
5524 
5525 	if (!adev->gfx.cp_gfx_shadow)
5526 		return;
5527 
5528 	amdgpu_ring_write(ring, PACKET3(PACKET3_SET_Q_PREEMPTION_MODE, 7));
5529 	amdgpu_ring_write(ring, lower_32_bits(shadow_va));
5530 	amdgpu_ring_write(ring, upper_32_bits(shadow_va));
5531 	amdgpu_ring_write(ring, lower_32_bits(gds_va));
5532 	amdgpu_ring_write(ring, upper_32_bits(gds_va));
5533 	amdgpu_ring_write(ring, lower_32_bits(csa_va));
5534 	amdgpu_ring_write(ring, upper_32_bits(csa_va));
5535 	amdgpu_ring_write(ring, shadow_va ?
5536 			  PACKET3_SET_Q_PREEMPTION_MODE_IB_VMID(vmid) : 0);
5537 	amdgpu_ring_write(ring, init_shadow ?
5538 			  PACKET3_SET_Q_PREEMPTION_MODE_INIT_SHADOW_MEM : 0);
5539 }
5540 
5541 static unsigned gfx_v11_0_ring_emit_init_cond_exec(struct amdgpu_ring *ring)
5542 {
5543 	unsigned ret;
5544 
5545 	amdgpu_ring_write(ring, PACKET3(PACKET3_COND_EXEC, 3));
5546 	amdgpu_ring_write(ring, lower_32_bits(ring->cond_exe_gpu_addr));
5547 	amdgpu_ring_write(ring, upper_32_bits(ring->cond_exe_gpu_addr));
5548 	amdgpu_ring_write(ring, 0); /* discard following DWs if *cond_exec_gpu_addr==0 */
5549 	ret = ring->wptr & ring->buf_mask;
5550 	amdgpu_ring_write(ring, 0x55aa55aa); /* patch dummy value later */
5551 
5552 	return ret;
5553 }
5554 
5555 static void gfx_v11_0_ring_emit_patch_cond_exec(struct amdgpu_ring *ring, unsigned offset)
5556 {
5557 	unsigned cur;
5558 	BUG_ON(offset > ring->buf_mask);
5559 	BUG_ON(ring->ring[offset] != 0x55aa55aa);
5560 
5561 	cur = (ring->wptr - 1) & ring->buf_mask;
5562 	if (likely(cur > offset))
5563 		ring->ring[offset] = cur - offset;
5564 	else
5565 		ring->ring[offset] = (ring->buf_mask + 1) - offset + cur;
5566 }
5567 
5568 static int gfx_v11_0_ring_preempt_ib(struct amdgpu_ring *ring)
5569 {
5570 	int i, r = 0;
5571 	struct amdgpu_device *adev = ring->adev;
5572 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[0];
5573 	struct amdgpu_ring *kiq_ring = &kiq->ring;
5574 	unsigned long flags;
5575 
5576 	if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
5577 		return -EINVAL;
5578 
5579 	spin_lock_irqsave(&kiq->ring_lock, flags);
5580 
5581 	if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size)) {
5582 		spin_unlock_irqrestore(&kiq->ring_lock, flags);
5583 		return -ENOMEM;
5584 	}
5585 
5586 	/* assert preemption condition */
5587 	amdgpu_ring_set_preempt_cond_exec(ring, false);
5588 
5589 	/* assert IB preemption, emit the trailing fence */
5590 	kiq->pmf->kiq_unmap_queues(kiq_ring, ring, PREEMPT_QUEUES_NO_UNMAP,
5591 				   ring->trail_fence_gpu_addr,
5592 				   ++ring->trail_seq);
5593 	amdgpu_ring_commit(kiq_ring);
5594 
5595 	spin_unlock_irqrestore(&kiq->ring_lock, flags);
5596 
5597 	/* poll the trailing fence */
5598 	for (i = 0; i < adev->usec_timeout; i++) {
5599 		if (ring->trail_seq ==
5600 		    le32_to_cpu(*(ring->trail_fence_cpu_addr)))
5601 			break;
5602 		udelay(1);
5603 	}
5604 
5605 	if (i >= adev->usec_timeout) {
5606 		r = -EINVAL;
5607 		DRM_ERROR("ring %d failed to preempt ib\n", ring->idx);
5608 	}
5609 
5610 	/* deassert preemption condition */
5611 	amdgpu_ring_set_preempt_cond_exec(ring, true);
5612 	return r;
5613 }
5614 
5615 static void gfx_v11_0_ring_emit_de_meta(struct amdgpu_ring *ring, bool resume)
5616 {
5617 	struct amdgpu_device *adev = ring->adev;
5618 	struct v10_de_ib_state de_payload = {0};
5619 	uint64_t offset, gds_addr, de_payload_gpu_addr;
5620 	void *de_payload_cpu_addr;
5621 	int cnt;
5622 
5623 	if (ring->is_mes_queue) {
5624 		offset = offsetof(struct amdgpu_mes_ctx_meta_data,
5625 				  gfx[0].gfx_meta_data) +
5626 			offsetof(struct v10_gfx_meta_data, de_payload);
5627 		de_payload_gpu_addr =
5628 			amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
5629 		de_payload_cpu_addr =
5630 			amdgpu_mes_ctx_get_offs_cpu_addr(ring, offset);
5631 
5632 		offset = offsetof(struct amdgpu_mes_ctx_meta_data,
5633 				  gfx[0].gds_backup) +
5634 			offsetof(struct v10_gfx_meta_data, de_payload);
5635 		gds_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
5636 	} else {
5637 		offset = offsetof(struct v10_gfx_meta_data, de_payload);
5638 		de_payload_gpu_addr = amdgpu_csa_vaddr(ring->adev) + offset;
5639 		de_payload_cpu_addr = adev->virt.csa_cpu_addr + offset;
5640 
5641 		gds_addr = ALIGN(amdgpu_csa_vaddr(ring->adev) +
5642 				 AMDGPU_CSA_SIZE - adev->gds.gds_size,
5643 				 PAGE_SIZE);
5644 	}
5645 
5646 	de_payload.gds_backup_addrlo = lower_32_bits(gds_addr);
5647 	de_payload.gds_backup_addrhi = upper_32_bits(gds_addr);
5648 
5649 	cnt = (sizeof(de_payload) >> 2) + 4 - 2;
5650 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, cnt));
5651 	amdgpu_ring_write(ring, (WRITE_DATA_ENGINE_SEL(1) |
5652 				 WRITE_DATA_DST_SEL(8) |
5653 				 WR_CONFIRM) |
5654 				 WRITE_DATA_CACHE_POLICY(0));
5655 	amdgpu_ring_write(ring, lower_32_bits(de_payload_gpu_addr));
5656 	amdgpu_ring_write(ring, upper_32_bits(de_payload_gpu_addr));
5657 
5658 	if (resume)
5659 		amdgpu_ring_write_multiple(ring, de_payload_cpu_addr,
5660 					   sizeof(de_payload) >> 2);
5661 	else
5662 		amdgpu_ring_write_multiple(ring, (void *)&de_payload,
5663 					   sizeof(de_payload) >> 2);
5664 }
5665 
5666 static void gfx_v11_0_ring_emit_frame_cntl(struct amdgpu_ring *ring, bool start,
5667 				    bool secure)
5668 {
5669 	uint32_t v = secure ? FRAME_TMZ : 0;
5670 
5671 	amdgpu_ring_write(ring, PACKET3(PACKET3_FRAME_CONTROL, 0));
5672 	amdgpu_ring_write(ring, v | FRAME_CMD(start ? 0 : 1));
5673 }
5674 
5675 static void gfx_v11_0_ring_emit_rreg(struct amdgpu_ring *ring, uint32_t reg,
5676 				     uint32_t reg_val_offs)
5677 {
5678 	struct amdgpu_device *adev = ring->adev;
5679 
5680 	amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
5681 	amdgpu_ring_write(ring, 0 |	/* src: register*/
5682 				(5 << 8) |	/* dst: memory */
5683 				(1 << 20));	/* write confirm */
5684 	amdgpu_ring_write(ring, reg);
5685 	amdgpu_ring_write(ring, 0);
5686 	amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
5687 				reg_val_offs * 4));
5688 	amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
5689 				reg_val_offs * 4));
5690 }
5691 
5692 static void gfx_v11_0_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg,
5693 				   uint32_t val)
5694 {
5695 	uint32_t cmd = 0;
5696 
5697 	switch (ring->funcs->type) {
5698 	case AMDGPU_RING_TYPE_GFX:
5699 		cmd = WRITE_DATA_ENGINE_SEL(1) | WR_CONFIRM;
5700 		break;
5701 	case AMDGPU_RING_TYPE_KIQ:
5702 		cmd = (1 << 16); /* no inc addr */
5703 		break;
5704 	default:
5705 		cmd = WR_CONFIRM;
5706 		break;
5707 	}
5708 	amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
5709 	amdgpu_ring_write(ring, cmd);
5710 	amdgpu_ring_write(ring, reg);
5711 	amdgpu_ring_write(ring, 0);
5712 	amdgpu_ring_write(ring, val);
5713 }
5714 
5715 static void gfx_v11_0_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
5716 					uint32_t val, uint32_t mask)
5717 {
5718 	gfx_v11_0_wait_reg_mem(ring, 0, 0, 0, reg, 0, val, mask, 0x20);
5719 }
5720 
5721 static void gfx_v11_0_ring_emit_reg_write_reg_wait(struct amdgpu_ring *ring,
5722 						   uint32_t reg0, uint32_t reg1,
5723 						   uint32_t ref, uint32_t mask)
5724 {
5725 	int usepfp = (ring->funcs->type == AMDGPU_RING_TYPE_GFX);
5726 
5727 	gfx_v11_0_wait_reg_mem(ring, usepfp, 0, 1, reg0, reg1,
5728 			       ref, mask, 0x20);
5729 }
5730 
5731 static void gfx_v11_0_ring_soft_recovery(struct amdgpu_ring *ring,
5732 					 unsigned vmid)
5733 {
5734 	struct amdgpu_device *adev = ring->adev;
5735 	uint32_t value = 0;
5736 
5737 	value = REG_SET_FIELD(value, SQ_CMD, CMD, 0x03);
5738 	value = REG_SET_FIELD(value, SQ_CMD, MODE, 0x01);
5739 	value = REG_SET_FIELD(value, SQ_CMD, CHECK_VMID, 1);
5740 	value = REG_SET_FIELD(value, SQ_CMD, VM_ID, vmid);
5741 	WREG32_SOC15(GC, 0, regSQ_CMD, value);
5742 }
5743 
5744 static void
5745 gfx_v11_0_set_gfx_eop_interrupt_state(struct amdgpu_device *adev,
5746 				      uint32_t me, uint32_t pipe,
5747 				      enum amdgpu_interrupt_state state)
5748 {
5749 	uint32_t cp_int_cntl, cp_int_cntl_reg;
5750 
5751 	if (!me) {
5752 		switch (pipe) {
5753 		case 0:
5754 			cp_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, regCP_INT_CNTL_RING0);
5755 			break;
5756 		case 1:
5757 			cp_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, regCP_INT_CNTL_RING1);
5758 			break;
5759 		default:
5760 			DRM_DEBUG("invalid pipe %d\n", pipe);
5761 			return;
5762 		}
5763 	} else {
5764 		DRM_DEBUG("invalid me %d\n", me);
5765 		return;
5766 	}
5767 
5768 	switch (state) {
5769 	case AMDGPU_IRQ_STATE_DISABLE:
5770 		cp_int_cntl = RREG32_SOC15_IP(GC, cp_int_cntl_reg);
5771 		cp_int_cntl = REG_SET_FIELD(cp_int_cntl, CP_INT_CNTL_RING0,
5772 					    TIME_STAMP_INT_ENABLE, 0);
5773 		cp_int_cntl = REG_SET_FIELD(cp_int_cntl, CP_INT_CNTL_RING0,
5774 					    GENERIC0_INT_ENABLE, 0);
5775 		WREG32_SOC15_IP(GC, cp_int_cntl_reg, cp_int_cntl);
5776 		break;
5777 	case AMDGPU_IRQ_STATE_ENABLE:
5778 		cp_int_cntl = RREG32_SOC15_IP(GC, cp_int_cntl_reg);
5779 		cp_int_cntl = REG_SET_FIELD(cp_int_cntl, CP_INT_CNTL_RING0,
5780 					    TIME_STAMP_INT_ENABLE, 1);
5781 		cp_int_cntl = REG_SET_FIELD(cp_int_cntl, CP_INT_CNTL_RING0,
5782 					    GENERIC0_INT_ENABLE, 1);
5783 		WREG32_SOC15_IP(GC, cp_int_cntl_reg, cp_int_cntl);
5784 		break;
5785 	default:
5786 		break;
5787 	}
5788 }
5789 
5790 static void gfx_v11_0_set_compute_eop_interrupt_state(struct amdgpu_device *adev,
5791 						     int me, int pipe,
5792 						     enum amdgpu_interrupt_state state)
5793 {
5794 	u32 mec_int_cntl, mec_int_cntl_reg;
5795 
5796 	/*
5797 	 * amdgpu controls only the first MEC. That's why this function only
5798 	 * handles the setting of interrupts for this specific MEC. All other
5799 	 * pipes' interrupts are set by amdkfd.
5800 	 */
5801 
5802 	if (me == 1) {
5803 		switch (pipe) {
5804 		case 0:
5805 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, regCP_ME1_PIPE0_INT_CNTL);
5806 			break;
5807 		case 1:
5808 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, regCP_ME1_PIPE1_INT_CNTL);
5809 			break;
5810 		case 2:
5811 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, regCP_ME1_PIPE2_INT_CNTL);
5812 			break;
5813 		case 3:
5814 			mec_int_cntl_reg = SOC15_REG_OFFSET(GC, 0, regCP_ME1_PIPE3_INT_CNTL);
5815 			break;
5816 		default:
5817 			DRM_DEBUG("invalid pipe %d\n", pipe);
5818 			return;
5819 		}
5820 	} else {
5821 		DRM_DEBUG("invalid me %d\n", me);
5822 		return;
5823 	}
5824 
5825 	switch (state) {
5826 	case AMDGPU_IRQ_STATE_DISABLE:
5827 		mec_int_cntl = RREG32_SOC15_IP(GC, mec_int_cntl_reg);
5828 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
5829 					     TIME_STAMP_INT_ENABLE, 0);
5830 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
5831 					     GENERIC0_INT_ENABLE, 0);
5832 		WREG32_SOC15_IP(GC, mec_int_cntl_reg, mec_int_cntl);
5833 		break;
5834 	case AMDGPU_IRQ_STATE_ENABLE:
5835 		mec_int_cntl = RREG32_SOC15_IP(GC, mec_int_cntl_reg);
5836 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
5837 					     TIME_STAMP_INT_ENABLE, 1);
5838 		mec_int_cntl = REG_SET_FIELD(mec_int_cntl, CP_ME1_PIPE0_INT_CNTL,
5839 					     GENERIC0_INT_ENABLE, 1);
5840 		WREG32_SOC15_IP(GC, mec_int_cntl_reg, mec_int_cntl);
5841 		break;
5842 	default:
5843 		break;
5844 	}
5845 }
5846 
5847 static int gfx_v11_0_set_eop_interrupt_state(struct amdgpu_device *adev,
5848 					    struct amdgpu_irq_src *src,
5849 					    unsigned type,
5850 					    enum amdgpu_interrupt_state state)
5851 {
5852 	switch (type) {
5853 	case AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP:
5854 		gfx_v11_0_set_gfx_eop_interrupt_state(adev, 0, 0, state);
5855 		break;
5856 	case AMDGPU_CP_IRQ_GFX_ME0_PIPE1_EOP:
5857 		gfx_v11_0_set_gfx_eop_interrupt_state(adev, 0, 1, state);
5858 		break;
5859 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP:
5860 		gfx_v11_0_set_compute_eop_interrupt_state(adev, 1, 0, state);
5861 		break;
5862 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE1_EOP:
5863 		gfx_v11_0_set_compute_eop_interrupt_state(adev, 1, 1, state);
5864 		break;
5865 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE2_EOP:
5866 		gfx_v11_0_set_compute_eop_interrupt_state(adev, 1, 2, state);
5867 		break;
5868 	case AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE3_EOP:
5869 		gfx_v11_0_set_compute_eop_interrupt_state(adev, 1, 3, state);
5870 		break;
5871 	default:
5872 		break;
5873 	}
5874 	return 0;
5875 }
5876 
5877 static int gfx_v11_0_eop_irq(struct amdgpu_device *adev,
5878 			     struct amdgpu_irq_src *source,
5879 			     struct amdgpu_iv_entry *entry)
5880 {
5881 	int i;
5882 	u8 me_id, pipe_id, queue_id;
5883 	struct amdgpu_ring *ring;
5884 	uint32_t mes_queue_id = entry->src_data[0];
5885 
5886 	DRM_DEBUG("IH: CP EOP\n");
5887 
5888 	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
5889 		struct amdgpu_mes_queue *queue;
5890 
5891 		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
5892 
5893 		spin_lock(&adev->mes.queue_id_lock);
5894 		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
5895 		if (queue) {
5896 			DRM_DEBUG("process mes queue id = %d\n", mes_queue_id);
5897 			amdgpu_fence_process(queue->ring);
5898 		}
5899 		spin_unlock(&adev->mes.queue_id_lock);
5900 	} else {
5901 		me_id = (entry->ring_id & 0x0c) >> 2;
5902 		pipe_id = (entry->ring_id & 0x03) >> 0;
5903 		queue_id = (entry->ring_id & 0x70) >> 4;
5904 
5905 		switch (me_id) {
5906 		case 0:
5907 			if (pipe_id == 0)
5908 				amdgpu_fence_process(&adev->gfx.gfx_ring[0]);
5909 			else
5910 				amdgpu_fence_process(&adev->gfx.gfx_ring[1]);
5911 			break;
5912 		case 1:
5913 		case 2:
5914 			for (i = 0; i < adev->gfx.num_compute_rings; i++) {
5915 				ring = &adev->gfx.compute_ring[i];
5916 				/* Per-queue interrupt is supported for MEC starting from VI.
5917 				 * The interrupt can only be enabled/disabled per pipe instead
5918 				 * of per queue.
5919 				 */
5920 				if ((ring->me == me_id) &&
5921 				    (ring->pipe == pipe_id) &&
5922 				    (ring->queue == queue_id))
5923 					amdgpu_fence_process(ring);
5924 			}
5925 			break;
5926 		}
5927 	}
5928 
5929 	return 0;
5930 }
5931 
5932 static int gfx_v11_0_set_priv_reg_fault_state(struct amdgpu_device *adev,
5933 					      struct amdgpu_irq_src *source,
5934 					      unsigned type,
5935 					      enum amdgpu_interrupt_state state)
5936 {
5937 	switch (state) {
5938 	case AMDGPU_IRQ_STATE_DISABLE:
5939 	case AMDGPU_IRQ_STATE_ENABLE:
5940 		WREG32_FIELD15_PREREG(GC, 0, CP_INT_CNTL_RING0,
5941 			       PRIV_REG_INT_ENABLE,
5942 			       state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
5943 		break;
5944 	default:
5945 		break;
5946 	}
5947 
5948 	return 0;
5949 }
5950 
5951 static int gfx_v11_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
5952 					       struct amdgpu_irq_src *source,
5953 					       unsigned type,
5954 					       enum amdgpu_interrupt_state state)
5955 {
5956 	switch (state) {
5957 	case AMDGPU_IRQ_STATE_DISABLE:
5958 	case AMDGPU_IRQ_STATE_ENABLE:
5959 		WREG32_FIELD15_PREREG(GC, 0, CP_INT_CNTL_RING0,
5960 			       PRIV_INSTR_INT_ENABLE,
5961 			       state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
5962 		break;
5963 	default:
5964 		break;
5965 	}
5966 
5967 	return 0;
5968 }
5969 
5970 static void gfx_v11_0_handle_priv_fault(struct amdgpu_device *adev,
5971 					struct amdgpu_iv_entry *entry)
5972 {
5973 	u8 me_id, pipe_id, queue_id;
5974 	struct amdgpu_ring *ring;
5975 	int i;
5976 
5977 	me_id = (entry->ring_id & 0x0c) >> 2;
5978 	pipe_id = (entry->ring_id & 0x03) >> 0;
5979 	queue_id = (entry->ring_id & 0x70) >> 4;
5980 
5981 	switch (me_id) {
5982 	case 0:
5983 		for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
5984 			ring = &adev->gfx.gfx_ring[i];
5985 			/* we only enabled 1 gfx queue per pipe for now */
5986 			if (ring->me == me_id && ring->pipe == pipe_id)
5987 				drm_sched_fault(&ring->sched);
5988 		}
5989 		break;
5990 	case 1:
5991 	case 2:
5992 		for (i = 0; i < adev->gfx.num_compute_rings; i++) {
5993 			ring = &adev->gfx.compute_ring[i];
5994 			if (ring->me == me_id && ring->pipe == pipe_id &&
5995 			    ring->queue == queue_id)
5996 				drm_sched_fault(&ring->sched);
5997 		}
5998 		break;
5999 	default:
6000 		BUG();
6001 		break;
6002 	}
6003 }
6004 
6005 static int gfx_v11_0_priv_reg_irq(struct amdgpu_device *adev,
6006 				  struct amdgpu_irq_src *source,
6007 				  struct amdgpu_iv_entry *entry)
6008 {
6009 	DRM_ERROR("Illegal register access in command stream\n");
6010 	gfx_v11_0_handle_priv_fault(adev, entry);
6011 	return 0;
6012 }
6013 
6014 static int gfx_v11_0_priv_inst_irq(struct amdgpu_device *adev,
6015 				   struct amdgpu_irq_src *source,
6016 				   struct amdgpu_iv_entry *entry)
6017 {
6018 	DRM_ERROR("Illegal instruction in command stream\n");
6019 	gfx_v11_0_handle_priv_fault(adev, entry);
6020 	return 0;
6021 }
6022 
6023 static int gfx_v11_0_rlc_gc_fed_irq(struct amdgpu_device *adev,
6024 				  struct amdgpu_irq_src *source,
6025 				  struct amdgpu_iv_entry *entry)
6026 {
6027 	if (adev->gfx.ras && adev->gfx.ras->rlc_gc_fed_irq)
6028 		return adev->gfx.ras->rlc_gc_fed_irq(adev, source, entry);
6029 
6030 	return 0;
6031 }
6032 
6033 #if 0
6034 static int gfx_v11_0_kiq_set_interrupt_state(struct amdgpu_device *adev,
6035 					     struct amdgpu_irq_src *src,
6036 					     unsigned int type,
6037 					     enum amdgpu_interrupt_state state)
6038 {
6039 	uint32_t tmp, target;
6040 	struct amdgpu_ring *ring = &(adev->gfx.kiq[0].ring);
6041 
6042 	target = SOC15_REG_OFFSET(GC, 0, regCP_ME1_PIPE0_INT_CNTL);
6043 	target += ring->pipe;
6044 
6045 	switch (type) {
6046 	case AMDGPU_CP_KIQ_IRQ_DRIVER0:
6047 		if (state == AMDGPU_IRQ_STATE_DISABLE) {
6048 			tmp = RREG32_SOC15(GC, 0, regCPC_INT_CNTL);
6049 			tmp = REG_SET_FIELD(tmp, CPC_INT_CNTL,
6050 					    GENERIC2_INT_ENABLE, 0);
6051 			WREG32_SOC15(GC, 0, regCPC_INT_CNTL, tmp);
6052 
6053 			tmp = RREG32_SOC15_IP(GC, target);
6054 			tmp = REG_SET_FIELD(tmp, CP_ME1_PIPE0_INT_CNTL,
6055 					    GENERIC2_INT_ENABLE, 0);
6056 			WREG32_SOC15_IP(GC, target, tmp);
6057 		} else {
6058 			tmp = RREG32_SOC15(GC, 0, regCPC_INT_CNTL);
6059 			tmp = REG_SET_FIELD(tmp, CPC_INT_CNTL,
6060 					    GENERIC2_INT_ENABLE, 1);
6061 			WREG32_SOC15(GC, 0, regCPC_INT_CNTL, tmp);
6062 
6063 			tmp = RREG32_SOC15_IP(GC, target);
6064 			tmp = REG_SET_FIELD(tmp, CP_ME1_PIPE0_INT_CNTL,
6065 					    GENERIC2_INT_ENABLE, 1);
6066 			WREG32_SOC15_IP(GC, target, tmp);
6067 		}
6068 		break;
6069 	default:
6070 		BUG(); /* kiq only support GENERIC2_INT now */
6071 		break;
6072 	}
6073 	return 0;
6074 }
6075 #endif
6076 
6077 static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring *ring)
6078 {
6079 	const unsigned int gcr_cntl =
6080 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GL2_INV(1) |
6081 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GL2_WB(1) |
6082 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GLM_INV(1) |
6083 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GLM_WB(1) |
6084 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GL1_INV(1) |
6085 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GLV_INV(1) |
6086 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GLK_INV(1) |
6087 			PACKET3_ACQUIRE_MEM_GCR_CNTL_GLI_INV(1);
6088 
6089 	/* ACQUIRE_MEM - make one or more surfaces valid for use by the subsequent operations */
6090 	amdgpu_ring_write(ring, PACKET3(PACKET3_ACQUIRE_MEM, 6));
6091 	amdgpu_ring_write(ring, 0); /* CP_COHER_CNTL */
6092 	amdgpu_ring_write(ring, 0xffffffff);  /* CP_COHER_SIZE */
6093 	amdgpu_ring_write(ring, 0xffffff);  /* CP_COHER_SIZE_HI */
6094 	amdgpu_ring_write(ring, 0); /* CP_COHER_BASE */
6095 	amdgpu_ring_write(ring, 0);  /* CP_COHER_BASE_HI */
6096 	amdgpu_ring_write(ring, 0x0000000A); /* POLL_INTERVAL */
6097 	amdgpu_ring_write(ring, gcr_cntl); /* GCR_CNTL */
6098 }
6099 
6100 static const struct amd_ip_funcs gfx_v11_0_ip_funcs = {
6101 	.name = "gfx_v11_0",
6102 	.early_init = gfx_v11_0_early_init,
6103 	.late_init = gfx_v11_0_late_init,
6104 	.sw_init = gfx_v11_0_sw_init,
6105 	.sw_fini = gfx_v11_0_sw_fini,
6106 	.hw_init = gfx_v11_0_hw_init,
6107 	.hw_fini = gfx_v11_0_hw_fini,
6108 	.suspend = gfx_v11_0_suspend,
6109 	.resume = gfx_v11_0_resume,
6110 	.is_idle = gfx_v11_0_is_idle,
6111 	.wait_for_idle = gfx_v11_0_wait_for_idle,
6112 	.soft_reset = gfx_v11_0_soft_reset,
6113 	.check_soft_reset = gfx_v11_0_check_soft_reset,
6114 	.post_soft_reset = gfx_v11_0_post_soft_reset,
6115 	.set_clockgating_state = gfx_v11_0_set_clockgating_state,
6116 	.set_powergating_state = gfx_v11_0_set_powergating_state,
6117 	.get_clockgating_state = gfx_v11_0_get_clockgating_state,
6118 };
6119 
6120 static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_gfx = {
6121 	.type = AMDGPU_RING_TYPE_GFX,
6122 	.align_mask = 0xff,
6123 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
6124 	.support_64bit_ptrs = true,
6125 	.secure_submission_supported = true,
6126 	.get_rptr = gfx_v11_0_ring_get_rptr_gfx,
6127 	.get_wptr = gfx_v11_0_ring_get_wptr_gfx,
6128 	.set_wptr = gfx_v11_0_ring_set_wptr_gfx,
6129 	.emit_frame_size = /* totally 242 maximum if 16 IBs */
6130 		5 + /* COND_EXEC */
6131 		9 + /* SET_Q_PREEMPTION_MODE */
6132 		7 + /* PIPELINE_SYNC */
6133 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
6134 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
6135 		2 + /* VM_FLUSH */
6136 		8 + /* FENCE for VM_FLUSH */
6137 		20 + /* GDS switch */
6138 		5 + /* COND_EXEC */
6139 		7 + /* HDP_flush */
6140 		4 + /* VGT_flush */
6141 		31 + /*	DE_META */
6142 		3 + /* CNTX_CTRL */
6143 		5 + /* HDP_INVL */
6144 		8 + 8 + /* FENCE x2 */
6145 		8, /* gfx_v11_0_emit_mem_sync */
6146 	.emit_ib_size =	4, /* gfx_v11_0_ring_emit_ib_gfx */
6147 	.emit_ib = gfx_v11_0_ring_emit_ib_gfx,
6148 	.emit_fence = gfx_v11_0_ring_emit_fence,
6149 	.emit_pipeline_sync = gfx_v11_0_ring_emit_pipeline_sync,
6150 	.emit_vm_flush = gfx_v11_0_ring_emit_vm_flush,
6151 	.emit_gds_switch = gfx_v11_0_ring_emit_gds_switch,
6152 	.emit_hdp_flush = gfx_v11_0_ring_emit_hdp_flush,
6153 	.test_ring = gfx_v11_0_ring_test_ring,
6154 	.test_ib = gfx_v11_0_ring_test_ib,
6155 	.insert_nop = amdgpu_ring_insert_nop,
6156 	.pad_ib = amdgpu_ring_generic_pad_ib,
6157 	.emit_cntxcntl = gfx_v11_0_ring_emit_cntxcntl,
6158 	.emit_gfx_shadow = gfx_v11_0_ring_emit_gfx_shadow,
6159 	.init_cond_exec = gfx_v11_0_ring_emit_init_cond_exec,
6160 	.patch_cond_exec = gfx_v11_0_ring_emit_patch_cond_exec,
6161 	.preempt_ib = gfx_v11_0_ring_preempt_ib,
6162 	.emit_frame_cntl = gfx_v11_0_ring_emit_frame_cntl,
6163 	.emit_wreg = gfx_v11_0_ring_emit_wreg,
6164 	.emit_reg_wait = gfx_v11_0_ring_emit_reg_wait,
6165 	.emit_reg_write_reg_wait = gfx_v11_0_ring_emit_reg_write_reg_wait,
6166 	.soft_recovery = gfx_v11_0_ring_soft_recovery,
6167 	.emit_mem_sync = gfx_v11_0_emit_mem_sync,
6168 };
6169 
6170 static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
6171 	.type = AMDGPU_RING_TYPE_COMPUTE,
6172 	.align_mask = 0xff,
6173 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
6174 	.support_64bit_ptrs = true,
6175 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
6176 	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
6177 	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
6178 	.emit_frame_size =
6179 		20 + /* gfx_v11_0_ring_emit_gds_switch */
6180 		7 + /* gfx_v11_0_ring_emit_hdp_flush */
6181 		5 + /* hdp invalidate */
6182 		7 + /* gfx_v11_0_ring_emit_pipeline_sync */
6183 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
6184 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
6185 		2 + /* gfx_v11_0_ring_emit_vm_flush */
6186 		8 + 8 + 8 + /* gfx_v11_0_ring_emit_fence x3 for user fence, vm fence */
6187 		8, /* gfx_v11_0_emit_mem_sync */
6188 	.emit_ib_size =	7, /* gfx_v11_0_ring_emit_ib_compute */
6189 	.emit_ib = gfx_v11_0_ring_emit_ib_compute,
6190 	.emit_fence = gfx_v11_0_ring_emit_fence,
6191 	.emit_pipeline_sync = gfx_v11_0_ring_emit_pipeline_sync,
6192 	.emit_vm_flush = gfx_v11_0_ring_emit_vm_flush,
6193 	.emit_gds_switch = gfx_v11_0_ring_emit_gds_switch,
6194 	.emit_hdp_flush = gfx_v11_0_ring_emit_hdp_flush,
6195 	.test_ring = gfx_v11_0_ring_test_ring,
6196 	.test_ib = gfx_v11_0_ring_test_ib,
6197 	.insert_nop = amdgpu_ring_insert_nop,
6198 	.pad_ib = amdgpu_ring_generic_pad_ib,
6199 	.emit_wreg = gfx_v11_0_ring_emit_wreg,
6200 	.emit_reg_wait = gfx_v11_0_ring_emit_reg_wait,
6201 	.emit_reg_write_reg_wait = gfx_v11_0_ring_emit_reg_write_reg_wait,
6202 	.emit_mem_sync = gfx_v11_0_emit_mem_sync,
6203 };
6204 
6205 static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
6206 	.type = AMDGPU_RING_TYPE_KIQ,
6207 	.align_mask = 0xff,
6208 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
6209 	.support_64bit_ptrs = true,
6210 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
6211 	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
6212 	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
6213 	.emit_frame_size =
6214 		20 + /* gfx_v11_0_ring_emit_gds_switch */
6215 		7 + /* gfx_v11_0_ring_emit_hdp_flush */
6216 		5 + /*hdp invalidate */
6217 		7 + /* gfx_v11_0_ring_emit_pipeline_sync */
6218 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 5 +
6219 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 7 +
6220 		2 + /* gfx_v11_0_ring_emit_vm_flush */
6221 		8 + 8 + 8, /* gfx_v11_0_ring_emit_fence_kiq x3 for user fence, vm fence */
6222 	.emit_ib_size =	7, /* gfx_v11_0_ring_emit_ib_compute */
6223 	.emit_ib = gfx_v11_0_ring_emit_ib_compute,
6224 	.emit_fence = gfx_v11_0_ring_emit_fence_kiq,
6225 	.test_ring = gfx_v11_0_ring_test_ring,
6226 	.test_ib = gfx_v11_0_ring_test_ib,
6227 	.insert_nop = amdgpu_ring_insert_nop,
6228 	.pad_ib = amdgpu_ring_generic_pad_ib,
6229 	.emit_rreg = gfx_v11_0_ring_emit_rreg,
6230 	.emit_wreg = gfx_v11_0_ring_emit_wreg,
6231 	.emit_reg_wait = gfx_v11_0_ring_emit_reg_wait,
6232 	.emit_reg_write_reg_wait = gfx_v11_0_ring_emit_reg_write_reg_wait,
6233 };
6234 
6235 static void gfx_v11_0_set_ring_funcs(struct amdgpu_device *adev)
6236 {
6237 	int i;
6238 
6239 	adev->gfx.kiq[0].ring.funcs = &gfx_v11_0_ring_funcs_kiq;
6240 
6241 	for (i = 0; i < adev->gfx.num_gfx_rings; i++)
6242 		adev->gfx.gfx_ring[i].funcs = &gfx_v11_0_ring_funcs_gfx;
6243 
6244 	for (i = 0; i < adev->gfx.num_compute_rings; i++)
6245 		adev->gfx.compute_ring[i].funcs = &gfx_v11_0_ring_funcs_compute;
6246 }
6247 
6248 static const struct amdgpu_irq_src_funcs gfx_v11_0_eop_irq_funcs = {
6249 	.set = gfx_v11_0_set_eop_interrupt_state,
6250 	.process = gfx_v11_0_eop_irq,
6251 };
6252 
6253 static const struct amdgpu_irq_src_funcs gfx_v11_0_priv_reg_irq_funcs = {
6254 	.set = gfx_v11_0_set_priv_reg_fault_state,
6255 	.process = gfx_v11_0_priv_reg_irq,
6256 };
6257 
6258 static const struct amdgpu_irq_src_funcs gfx_v11_0_priv_inst_irq_funcs = {
6259 	.set = gfx_v11_0_set_priv_inst_fault_state,
6260 	.process = gfx_v11_0_priv_inst_irq,
6261 };
6262 
6263 static const struct amdgpu_irq_src_funcs gfx_v11_0_rlc_gc_fed_irq_funcs = {
6264 	.process = gfx_v11_0_rlc_gc_fed_irq,
6265 };
6266 
6267 static void gfx_v11_0_set_irq_funcs(struct amdgpu_device *adev)
6268 {
6269 	adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
6270 	adev->gfx.eop_irq.funcs = &gfx_v11_0_eop_irq_funcs;
6271 
6272 	adev->gfx.priv_reg_irq.num_types = 1;
6273 	adev->gfx.priv_reg_irq.funcs = &gfx_v11_0_priv_reg_irq_funcs;
6274 
6275 	adev->gfx.priv_inst_irq.num_types = 1;
6276 	adev->gfx.priv_inst_irq.funcs = &gfx_v11_0_priv_inst_irq_funcs;
6277 
6278 	adev->gfx.rlc_gc_fed_irq.num_types = 1; /* 0x80 FED error */
6279 	adev->gfx.rlc_gc_fed_irq.funcs = &gfx_v11_0_rlc_gc_fed_irq_funcs;
6280 
6281 }
6282 
6283 static void gfx_v11_0_set_imu_funcs(struct amdgpu_device *adev)
6284 {
6285 	if (adev->flags & AMD_IS_APU)
6286 		adev->gfx.imu.mode = MISSION_MODE;
6287 	else
6288 		adev->gfx.imu.mode = DEBUG_MODE;
6289 
6290 	adev->gfx.imu.funcs = &gfx_v11_0_imu_funcs;
6291 }
6292 
6293 static void gfx_v11_0_set_rlc_funcs(struct amdgpu_device *adev)
6294 {
6295 	adev->gfx.rlc.funcs = &gfx_v11_0_rlc_funcs;
6296 }
6297 
6298 static void gfx_v11_0_set_gds_init(struct amdgpu_device *adev)
6299 {
6300 	unsigned total_cu = adev->gfx.config.max_cu_per_sh *
6301 			    adev->gfx.config.max_sh_per_se *
6302 			    adev->gfx.config.max_shader_engines;
6303 
6304 	adev->gds.gds_size = 0x1000;
6305 	adev->gds.gds_compute_max_wave_id = total_cu * 32 - 1;
6306 	adev->gds.gws_size = 64;
6307 	adev->gds.oa_size = 16;
6308 }
6309 
6310 static void gfx_v11_0_set_mqd_funcs(struct amdgpu_device *adev)
6311 {
6312 	/* set gfx eng mqd */
6313 	adev->mqds[AMDGPU_HW_IP_GFX].mqd_size =
6314 		sizeof(struct v11_gfx_mqd);
6315 	adev->mqds[AMDGPU_HW_IP_GFX].init_mqd =
6316 		gfx_v11_0_gfx_mqd_init;
6317 	/* set compute eng mqd */
6318 	adev->mqds[AMDGPU_HW_IP_COMPUTE].mqd_size =
6319 		sizeof(struct v11_compute_mqd);
6320 	adev->mqds[AMDGPU_HW_IP_COMPUTE].init_mqd =
6321 		gfx_v11_0_compute_mqd_init;
6322 }
6323 
6324 static void gfx_v11_0_set_user_wgp_inactive_bitmap_per_sh(struct amdgpu_device *adev,
6325 							  u32 bitmap)
6326 {
6327 	u32 data;
6328 
6329 	if (!bitmap)
6330 		return;
6331 
6332 	data = bitmap << GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_WGPS__SHIFT;
6333 	data &= GC_USER_SHADER_ARRAY_CONFIG__INACTIVE_WGPS_MASK;
6334 
6335 	WREG32_SOC15(GC, 0, regGC_USER_SHADER_ARRAY_CONFIG, data);
6336 }
6337 
6338 static u32 gfx_v11_0_get_wgp_active_bitmap_per_sh(struct amdgpu_device *adev)
6339 {
6340 	u32 data, wgp_bitmask;
6341 	data = RREG32_SOC15(GC, 0, regCC_GC_SHADER_ARRAY_CONFIG);
6342 	data |= RREG32_SOC15(GC, 0, regGC_USER_SHADER_ARRAY_CONFIG);
6343 
6344 	data &= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_WGPS_MASK;
6345 	data >>= CC_GC_SHADER_ARRAY_CONFIG__INACTIVE_WGPS__SHIFT;
6346 
6347 	wgp_bitmask =
6348 		amdgpu_gfx_create_bitmask(adev->gfx.config.max_cu_per_sh >> 1);
6349 
6350 	return (~data) & wgp_bitmask;
6351 }
6352 
6353 static u32 gfx_v11_0_get_cu_active_bitmap_per_sh(struct amdgpu_device *adev)
6354 {
6355 	u32 wgp_idx, wgp_active_bitmap;
6356 	u32 cu_bitmap_per_wgp, cu_active_bitmap;
6357 
6358 	wgp_active_bitmap = gfx_v11_0_get_wgp_active_bitmap_per_sh(adev);
6359 	cu_active_bitmap = 0;
6360 
6361 	for (wgp_idx = 0; wgp_idx < 16; wgp_idx++) {
6362 		/* if there is one WGP enabled, it means 2 CUs will be enabled */
6363 		cu_bitmap_per_wgp = 3 << (2 * wgp_idx);
6364 		if (wgp_active_bitmap & (1 << wgp_idx))
6365 			cu_active_bitmap |= cu_bitmap_per_wgp;
6366 	}
6367 
6368 	return cu_active_bitmap;
6369 }
6370 
6371 static int gfx_v11_0_get_cu_info(struct amdgpu_device *adev,
6372 				 struct amdgpu_cu_info *cu_info)
6373 {
6374 	int i, j, k, counter, active_cu_number = 0;
6375 	u32 mask, bitmap;
6376 	unsigned disable_masks[8 * 2];
6377 
6378 	if (!adev || !cu_info)
6379 		return -EINVAL;
6380 
6381 	amdgpu_gfx_parse_disable_cu(disable_masks, 8, 2);
6382 
6383 	mutex_lock(&adev->grbm_idx_mutex);
6384 	for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
6385 		for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
6386 			bitmap = i * adev->gfx.config.max_sh_per_se + j;
6387 			if (!((gfx_v11_0_get_sa_active_bitmap(adev) >> bitmap) & 1))
6388 				continue;
6389 			mask = 1;
6390 			counter = 0;
6391 			gfx_v11_0_select_se_sh(adev, i, j, 0xffffffff, 0);
6392 			if (i < 8 && j < 2)
6393 				gfx_v11_0_set_user_wgp_inactive_bitmap_per_sh(
6394 					adev, disable_masks[i * 2 + j]);
6395 			bitmap = gfx_v11_0_get_cu_active_bitmap_per_sh(adev);
6396 
6397 			/**
6398 			 * GFX11 could support more than 4 SEs, while the bitmap
6399 			 * in cu_info struct is 4x4 and ioctl interface struct
6400 			 * drm_amdgpu_info_device should keep stable.
6401 			 * So we use last two columns of bitmap to store cu mask for
6402 			 * SEs 4 to 7, the layout of the bitmap is as below:
6403 			 *    SE0: {SH0,SH1} --> {bitmap[0][0], bitmap[0][1]}
6404 			 *    SE1: {SH0,SH1} --> {bitmap[1][0], bitmap[1][1]}
6405 			 *    SE2: {SH0,SH1} --> {bitmap[2][0], bitmap[2][1]}
6406 			 *    SE3: {SH0,SH1} --> {bitmap[3][0], bitmap[3][1]}
6407 			 *    SE4: {SH0,SH1} --> {bitmap[0][2], bitmap[0][3]}
6408 			 *    SE5: {SH0,SH1} --> {bitmap[1][2], bitmap[1][3]}
6409 			 *    SE6: {SH0,SH1} --> {bitmap[2][2], bitmap[2][3]}
6410 			 *    SE7: {SH0,SH1} --> {bitmap[3][2], bitmap[3][3]}
6411 			 */
6412 			cu_info->bitmap[0][i % 4][j + (i / 4) * 2] = bitmap;
6413 
6414 			for (k = 0; k < adev->gfx.config.max_cu_per_sh; k++) {
6415 				if (bitmap & mask)
6416 					counter++;
6417 
6418 				mask <<= 1;
6419 			}
6420 			active_cu_number += counter;
6421 		}
6422 	}
6423 	gfx_v11_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
6424 	mutex_unlock(&adev->grbm_idx_mutex);
6425 
6426 	cu_info->number = active_cu_number;
6427 	cu_info->simd_per_cu = NUM_SIMD_PER_CU;
6428 
6429 	return 0;
6430 }
6431 
6432 const struct amdgpu_ip_block_version gfx_v11_0_ip_block =
6433 {
6434 	.type = AMD_IP_BLOCK_TYPE_GFX,
6435 	.major = 11,
6436 	.minor = 0,
6437 	.rev = 0,
6438 	.funcs = &gfx_v11_0_ip_funcs,
6439 };
6440