xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/soc15.c (revision 51c7b447)
1 /*
2  * Copyright 2016 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 #include <linux/firmware.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <drm/drmP.h>
27 #include "amdgpu.h"
28 #include "amdgpu_atombios.h"
29 #include "amdgpu_ih.h"
30 #include "amdgpu_uvd.h"
31 #include "amdgpu_vce.h"
32 #include "amdgpu_ucode.h"
33 #include "amdgpu_psp.h"
34 #include "atom.h"
35 #include "amd_pcie.h"
36 
37 #include "uvd/uvd_7_0_offset.h"
38 #include "gc/gc_9_0_offset.h"
39 #include "gc/gc_9_0_sh_mask.h"
40 #include "sdma0/sdma0_4_0_offset.h"
41 #include "sdma1/sdma1_4_0_offset.h"
42 #include "hdp/hdp_4_0_offset.h"
43 #include "hdp/hdp_4_0_sh_mask.h"
44 #include "smuio/smuio_9_0_offset.h"
45 #include "smuio/smuio_9_0_sh_mask.h"
46 #include "nbio/nbio_7_0_default.h"
47 #include "nbio/nbio_7_0_sh_mask.h"
48 #include "nbio/nbio_7_0_smn.h"
49 #include "mp/mp_9_0_offset.h"
50 
51 #include "soc15.h"
52 #include "soc15_common.h"
53 #include "gfx_v9_0.h"
54 #include "gmc_v9_0.h"
55 #include "gfxhub_v1_0.h"
56 #include "mmhub_v1_0.h"
57 #include "df_v1_7.h"
58 #include "df_v3_6.h"
59 #include "vega10_ih.h"
60 #include "sdma_v4_0.h"
61 #include "uvd_v7_0.h"
62 #include "vce_v4_0.h"
63 #include "vcn_v1_0.h"
64 #include "dce_virtual.h"
65 #include "mxgpu_ai.h"
66 #include "amdgpu_smu.h"
67 
68 #define mmMP0_MISC_CGTT_CTRL0                                                                   0x01b9
69 #define mmMP0_MISC_CGTT_CTRL0_BASE_IDX                                                          0
70 #define mmMP0_MISC_LIGHT_SLEEP_CTRL                                                             0x01ba
71 #define mmMP0_MISC_LIGHT_SLEEP_CTRL_BASE_IDX                                                    0
72 
73 /* for Vega20 register name change */
74 #define mmHDP_MEM_POWER_CTRL	0x00d4
75 #define HDP_MEM_POWER_CTRL__IPH_MEM_POWER_CTRL_EN_MASK	0x00000001L
76 #define HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK	0x00000002L
77 #define HDP_MEM_POWER_CTRL__RC_MEM_POWER_CTRL_EN_MASK	0x00010000L
78 #define HDP_MEM_POWER_CTRL__RC_MEM_POWER_LS_EN_MASK		0x00020000L
79 #define mmHDP_MEM_POWER_CTRL_BASE_IDX	0
80 /*
81  * Indirect registers accessor
82  */
83 static u32 soc15_pcie_rreg(struct amdgpu_device *adev, u32 reg)
84 {
85 	unsigned long flags, address, data;
86 	u32 r;
87 	address = adev->nbio_funcs->get_pcie_index_offset(adev);
88 	data = adev->nbio_funcs->get_pcie_data_offset(adev);
89 
90 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
91 	WREG32(address, reg);
92 	(void)RREG32(address);
93 	r = RREG32(data);
94 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
95 	return r;
96 }
97 
98 static void soc15_pcie_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
99 {
100 	unsigned long flags, address, data;
101 
102 	address = adev->nbio_funcs->get_pcie_index_offset(adev);
103 	data = adev->nbio_funcs->get_pcie_data_offset(adev);
104 
105 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
106 	WREG32(address, reg);
107 	(void)RREG32(address);
108 	WREG32(data, v);
109 	(void)RREG32(data);
110 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
111 }
112 
113 static u32 soc15_uvd_ctx_rreg(struct amdgpu_device *adev, u32 reg)
114 {
115 	unsigned long flags, address, data;
116 	u32 r;
117 
118 	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
119 	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
120 
121 	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
122 	WREG32(address, ((reg) & 0x1ff));
123 	r = RREG32(data);
124 	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
125 	return r;
126 }
127 
128 static void soc15_uvd_ctx_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
129 {
130 	unsigned long flags, address, data;
131 
132 	address = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_INDEX);
133 	data = SOC15_REG_OFFSET(UVD, 0, mmUVD_CTX_DATA);
134 
135 	spin_lock_irqsave(&adev->uvd_ctx_idx_lock, flags);
136 	WREG32(address, ((reg) & 0x1ff));
137 	WREG32(data, (v));
138 	spin_unlock_irqrestore(&adev->uvd_ctx_idx_lock, flags);
139 }
140 
141 static u32 soc15_didt_rreg(struct amdgpu_device *adev, u32 reg)
142 {
143 	unsigned long flags, address, data;
144 	u32 r;
145 
146 	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
147 	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
148 
149 	spin_lock_irqsave(&adev->didt_idx_lock, flags);
150 	WREG32(address, (reg));
151 	r = RREG32(data);
152 	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
153 	return r;
154 }
155 
156 static void soc15_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
157 {
158 	unsigned long flags, address, data;
159 
160 	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
161 	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
162 
163 	spin_lock_irqsave(&adev->didt_idx_lock, flags);
164 	WREG32(address, (reg));
165 	WREG32(data, (v));
166 	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
167 }
168 
169 static u32 soc15_gc_cac_rreg(struct amdgpu_device *adev, u32 reg)
170 {
171 	unsigned long flags;
172 	u32 r;
173 
174 	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
175 	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
176 	r = RREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA);
177 	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
178 	return r;
179 }
180 
181 static void soc15_gc_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
182 {
183 	unsigned long flags;
184 
185 	spin_lock_irqsave(&adev->gc_cac_idx_lock, flags);
186 	WREG32_SOC15(GC, 0, mmGC_CAC_IND_INDEX, (reg));
187 	WREG32_SOC15(GC, 0, mmGC_CAC_IND_DATA, (v));
188 	spin_unlock_irqrestore(&adev->gc_cac_idx_lock, flags);
189 }
190 
191 static u32 soc15_se_cac_rreg(struct amdgpu_device *adev, u32 reg)
192 {
193 	unsigned long flags;
194 	u32 r;
195 
196 	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
197 	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
198 	r = RREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA);
199 	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
200 	return r;
201 }
202 
203 static void soc15_se_cac_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
204 {
205 	unsigned long flags;
206 
207 	spin_lock_irqsave(&adev->se_cac_idx_lock, flags);
208 	WREG32_SOC15(GC, 0, mmSE_CAC_IND_INDEX, (reg));
209 	WREG32_SOC15(GC, 0, mmSE_CAC_IND_DATA, (v));
210 	spin_unlock_irqrestore(&adev->se_cac_idx_lock, flags);
211 }
212 
213 static u32 soc15_get_config_memsize(struct amdgpu_device *adev)
214 {
215 	return adev->nbio_funcs->get_memsize(adev);
216 }
217 
218 static u32 soc15_get_xclk(struct amdgpu_device *adev)
219 {
220 	return adev->clock.spll.reference_freq;
221 }
222 
223 
224 void soc15_grbm_select(struct amdgpu_device *adev,
225 		     u32 me, u32 pipe, u32 queue, u32 vmid)
226 {
227 	u32 grbm_gfx_cntl = 0;
228 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
229 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
230 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
231 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
232 
233 	WREG32(SOC15_REG_OFFSET(GC, 0, mmGRBM_GFX_CNTL), grbm_gfx_cntl);
234 }
235 
236 static void soc15_vga_set_state(struct amdgpu_device *adev, bool state)
237 {
238 	/* todo */
239 }
240 
241 static bool soc15_read_disabled_bios(struct amdgpu_device *adev)
242 {
243 	/* todo */
244 	return false;
245 }
246 
247 static bool soc15_read_bios_from_rom(struct amdgpu_device *adev,
248 				     u8 *bios, u32 length_bytes)
249 {
250 	u32 *dw_ptr;
251 	u32 i, length_dw;
252 
253 	if (bios == NULL)
254 		return false;
255 	if (length_bytes == 0)
256 		return false;
257 	/* APU vbios image is part of sbios image */
258 	if (adev->flags & AMD_IS_APU)
259 		return false;
260 
261 	dw_ptr = (u32 *)bios;
262 	length_dw = ALIGN(length_bytes, 4) / 4;
263 
264 	/* set rom index to 0 */
265 	WREG32(SOC15_REG_OFFSET(SMUIO, 0, mmROM_INDEX), 0);
266 	/* read out the rom data */
267 	for (i = 0; i < length_dw; i++)
268 		dw_ptr[i] = RREG32(SOC15_REG_OFFSET(SMUIO, 0, mmROM_DATA));
269 
270 	return true;
271 }
272 
273 struct soc15_allowed_register_entry {
274 	uint32_t hwip;
275 	uint32_t inst;
276 	uint32_t seg;
277 	uint32_t reg_offset;
278 	bool grbm_indexed;
279 };
280 
281 
282 static struct soc15_allowed_register_entry soc15_allowed_read_registers[] = {
283 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)},
284 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)},
285 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)},
286 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)},
287 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)},
288 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)},
289 	{ SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)},
290 	{ SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)},
291 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STAT)},
292 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)},
293 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)},
294 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)},
295 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)},
296 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)},
297 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)},
298 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)},
299 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)},
300 	{ SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)},
301 	{ SOC15_REG_ENTRY(GC, 0, mmDB_DEBUG2)},
302 };
303 
304 static uint32_t soc15_read_indexed_register(struct amdgpu_device *adev, u32 se_num,
305 					 u32 sh_num, u32 reg_offset)
306 {
307 	uint32_t val;
308 
309 	mutex_lock(&adev->grbm_idx_mutex);
310 	if (se_num != 0xffffffff || sh_num != 0xffffffff)
311 		amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff);
312 
313 	val = RREG32(reg_offset);
314 
315 	if (se_num != 0xffffffff || sh_num != 0xffffffff)
316 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
317 	mutex_unlock(&adev->grbm_idx_mutex);
318 	return val;
319 }
320 
321 static uint32_t soc15_get_register_value(struct amdgpu_device *adev,
322 					 bool indexed, u32 se_num,
323 					 u32 sh_num, u32 reg_offset)
324 {
325 	if (indexed) {
326 		return soc15_read_indexed_register(adev, se_num, sh_num, reg_offset);
327 	} else {
328 		if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG))
329 			return adev->gfx.config.gb_addr_config;
330 		else if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmDB_DEBUG2))
331 			return adev->gfx.config.db_debug2;
332 		return RREG32(reg_offset);
333 	}
334 }
335 
336 static int soc15_read_register(struct amdgpu_device *adev, u32 se_num,
337 			    u32 sh_num, u32 reg_offset, u32 *value)
338 {
339 	uint32_t i;
340 	struct soc15_allowed_register_entry  *en;
341 
342 	*value = 0;
343 	for (i = 0; i < ARRAY_SIZE(soc15_allowed_read_registers); i++) {
344 		en = &soc15_allowed_read_registers[i];
345 		if (reg_offset != (adev->reg_offset[en->hwip][en->inst][en->seg]
346 					+ en->reg_offset))
347 			continue;
348 
349 		*value = soc15_get_register_value(adev,
350 						  soc15_allowed_read_registers[i].grbm_indexed,
351 						  se_num, sh_num, reg_offset);
352 		return 0;
353 	}
354 	return -EINVAL;
355 }
356 
357 
358 /**
359  * soc15_program_register_sequence - program an array of registers.
360  *
361  * @adev: amdgpu_device pointer
362  * @regs: pointer to the register array
363  * @array_size: size of the register array
364  *
365  * Programs an array or registers with and and or masks.
366  * This is a helper for setting golden registers.
367  */
368 
369 void soc15_program_register_sequence(struct amdgpu_device *adev,
370 					     const struct soc15_reg_golden *regs,
371 					     const u32 array_size)
372 {
373 	const struct soc15_reg_golden *entry;
374 	u32 tmp, reg;
375 	int i;
376 
377 	for (i = 0; i < array_size; ++i) {
378 		entry = &regs[i];
379 		reg =  adev->reg_offset[entry->hwip][entry->instance][entry->segment] + entry->reg;
380 
381 		if (entry->and_mask == 0xffffffff) {
382 			tmp = entry->or_mask;
383 		} else {
384 			tmp = RREG32(reg);
385 			tmp &= ~(entry->and_mask);
386 			tmp |= entry->or_mask;
387 		}
388 		WREG32(reg, tmp);
389 	}
390 
391 }
392 
393 static int soc15_asic_mode1_reset(struct amdgpu_device *adev)
394 {
395 	u32 i;
396 	int ret = 0;
397 
398 	amdgpu_atombios_scratch_regs_engine_hung(adev, true);
399 
400 	dev_info(adev->dev, "GPU mode1 reset\n");
401 
402 	/* disable BM */
403 	pci_clear_master(adev->pdev);
404 
405 	pci_save_state(adev->pdev);
406 
407 	ret = psp_gpu_reset(adev);
408 	if (ret)
409 		dev_err(adev->dev, "GPU mode1 reset failed\n");
410 
411 	pci_restore_state(adev->pdev);
412 
413 	/* wait for asic to come out of reset */
414 	for (i = 0; i < adev->usec_timeout; i++) {
415 		u32 memsize = adev->nbio_funcs->get_memsize(adev);
416 
417 		if (memsize != 0xffffffff)
418 			break;
419 		udelay(1);
420 	}
421 
422 	amdgpu_atombios_scratch_regs_engine_hung(adev, false);
423 
424 	return ret;
425 }
426 
427 static int soc15_asic_get_baco_capability(struct amdgpu_device *adev, bool *cap)
428 {
429 	void *pp_handle = adev->powerplay.pp_handle;
430 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
431 
432 	if (!pp_funcs || !pp_funcs->get_asic_baco_capability) {
433 		*cap = false;
434 		return -ENOENT;
435 	}
436 
437 	return pp_funcs->get_asic_baco_capability(pp_handle, cap);
438 }
439 
440 static int soc15_asic_baco_reset(struct amdgpu_device *adev)
441 {
442 	void *pp_handle = adev->powerplay.pp_handle;
443 	const struct amd_pm_funcs *pp_funcs = adev->powerplay.pp_funcs;
444 
445 	if (!pp_funcs ||!pp_funcs->get_asic_baco_state ||!pp_funcs->set_asic_baco_state)
446 		return -ENOENT;
447 
448 	/* enter BACO state */
449 	if (pp_funcs->set_asic_baco_state(pp_handle, 1))
450 		return -EIO;
451 
452 	/* exit BACO state */
453 	if (pp_funcs->set_asic_baco_state(pp_handle, 0))
454 		return -EIO;
455 
456 	dev_info(adev->dev, "GPU BACO reset\n");
457 
458 	adev->in_baco_reset = 1;
459 
460 	return 0;
461 }
462 
463 static int soc15_asic_reset(struct amdgpu_device *adev)
464 {
465 	int ret;
466 	bool baco_reset;
467 
468 	switch (adev->asic_type) {
469 	case CHIP_VEGA10:
470 	case CHIP_VEGA12:
471 		soc15_asic_get_baco_capability(adev, &baco_reset);
472 		break;
473 	default:
474 		baco_reset = false;
475 		break;
476 	}
477 
478 	if (baco_reset)
479 		ret = soc15_asic_baco_reset(adev);
480 	else
481 		ret = soc15_asic_mode1_reset(adev);
482 
483 	return ret;
484 }
485 
486 /*static int soc15_set_uvd_clock(struct amdgpu_device *adev, u32 clock,
487 			u32 cntl_reg, u32 status_reg)
488 {
489 	return 0;
490 }*/
491 
492 static int soc15_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
493 {
494 	/*int r;
495 
496 	r = soc15_set_uvd_clock(adev, vclk, ixCG_VCLK_CNTL, ixCG_VCLK_STATUS);
497 	if (r)
498 		return r;
499 
500 	r = soc15_set_uvd_clock(adev, dclk, ixCG_DCLK_CNTL, ixCG_DCLK_STATUS);
501 	*/
502 	return 0;
503 }
504 
505 static int soc15_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
506 {
507 	/* todo */
508 
509 	return 0;
510 }
511 
512 static void soc15_pcie_gen3_enable(struct amdgpu_device *adev)
513 {
514 	if (pci_is_root_bus(adev->pdev->bus))
515 		return;
516 
517 	if (amdgpu_pcie_gen2 == 0)
518 		return;
519 
520 	if (adev->flags & AMD_IS_APU)
521 		return;
522 
523 	if (!(adev->pm.pcie_gen_mask & (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
524 					CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)))
525 		return;
526 
527 	/* todo */
528 }
529 
530 static void soc15_program_aspm(struct amdgpu_device *adev)
531 {
532 
533 	if (amdgpu_aspm == 0)
534 		return;
535 
536 	/* todo */
537 }
538 
539 static void soc15_enable_doorbell_aperture(struct amdgpu_device *adev,
540 					   bool enable)
541 {
542 	adev->nbio_funcs->enable_doorbell_aperture(adev, enable);
543 	adev->nbio_funcs->enable_doorbell_selfring_aperture(adev, enable);
544 }
545 
546 static const struct amdgpu_ip_block_version vega10_common_ip_block =
547 {
548 	.type = AMD_IP_BLOCK_TYPE_COMMON,
549 	.major = 2,
550 	.minor = 0,
551 	.rev = 0,
552 	.funcs = &soc15_common_ip_funcs,
553 };
554 
555 static uint32_t soc15_get_rev_id(struct amdgpu_device *adev)
556 {
557 	return adev->nbio_funcs->get_rev_id(adev);
558 }
559 
560 int soc15_set_ip_blocks(struct amdgpu_device *adev)
561 {
562 	/* Set IP register base before any HW register access */
563 	switch (adev->asic_type) {
564 	case CHIP_VEGA10:
565 	case CHIP_VEGA12:
566 	case CHIP_RAVEN:
567 		vega10_reg_base_init(adev);
568 		break;
569 	case CHIP_VEGA20:
570 		vega20_reg_base_init(adev);
571 		break;
572 	default:
573 		return -EINVAL;
574 	}
575 
576 	if (adev->asic_type == CHIP_VEGA20)
577 		adev->gmc.xgmi.supported = true;
578 
579 	if (adev->flags & AMD_IS_APU)
580 		adev->nbio_funcs = &nbio_v7_0_funcs;
581 	else if (adev->asic_type == CHIP_VEGA20)
582 		adev->nbio_funcs = &nbio_v7_4_funcs;
583 	else
584 		adev->nbio_funcs = &nbio_v6_1_funcs;
585 
586 	if (adev->asic_type == CHIP_VEGA20)
587 		adev->df_funcs = &df_v3_6_funcs;
588 	else
589 		adev->df_funcs = &df_v1_7_funcs;
590 
591 	adev->rev_id = soc15_get_rev_id(adev);
592 	adev->nbio_funcs->detect_hw_virt(adev);
593 
594 	if (amdgpu_sriov_vf(adev))
595 		adev->virt.ops = &xgpu_ai_virt_ops;
596 
597 	switch (adev->asic_type) {
598 	case CHIP_VEGA10:
599 	case CHIP_VEGA12:
600 	case CHIP_VEGA20:
601 		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
602 		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
603 		amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
604 		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)) {
605 			if (adev->asic_type == CHIP_VEGA20)
606 				amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
607 			else
608 				amdgpu_device_ip_block_add(adev, &psp_v3_1_ip_block);
609 		}
610 		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
611 		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
612 		if (!amdgpu_sriov_vf(adev)) {
613 			if (is_support_sw_smu(adev))
614 				amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
615 			else
616 				amdgpu_device_ip_block_add(adev, &pp_smu_ip_block);
617 		}
618 		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
619 			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
620 #if defined(CONFIG_DRM_AMD_DC)
621 		else if (amdgpu_device_has_dc_support(adev))
622 			amdgpu_device_ip_block_add(adev, &dm_ip_block);
623 #else
624 #	warning "Enable CONFIG_DRM_AMD_DC for display support on SOC15."
625 #endif
626 		if (!(adev->asic_type == CHIP_VEGA20 && amdgpu_sriov_vf(adev))) {
627 			amdgpu_device_ip_block_add(adev, &uvd_v7_0_ip_block);
628 			amdgpu_device_ip_block_add(adev, &vce_v4_0_ip_block);
629 		}
630 		break;
631 	case CHIP_RAVEN:
632 		amdgpu_device_ip_block_add(adev, &vega10_common_ip_block);
633 		amdgpu_device_ip_block_add(adev, &gmc_v9_0_ip_block);
634 		amdgpu_device_ip_block_add(adev, &vega10_ih_ip_block);
635 		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
636 			amdgpu_device_ip_block_add(adev, &psp_v10_0_ip_block);
637 		amdgpu_device_ip_block_add(adev, &gfx_v9_0_ip_block);
638 		amdgpu_device_ip_block_add(adev, &sdma_v4_0_ip_block);
639 		amdgpu_device_ip_block_add(adev, &pp_smu_ip_block);
640 		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
641 			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
642 #if defined(CONFIG_DRM_AMD_DC)
643 		else if (amdgpu_device_has_dc_support(adev))
644 			amdgpu_device_ip_block_add(adev, &dm_ip_block);
645 #else
646 #	warning "Enable CONFIG_DRM_AMD_DC for display support on SOC15."
647 #endif
648 		amdgpu_device_ip_block_add(adev, &vcn_v1_0_ip_block);
649 		break;
650 	default:
651 		return -EINVAL;
652 	}
653 
654 	return 0;
655 }
656 
657 static void soc15_flush_hdp(struct amdgpu_device *adev, struct amdgpu_ring *ring)
658 {
659 	adev->nbio_funcs->hdp_flush(adev, ring);
660 }
661 
662 static void soc15_invalidate_hdp(struct amdgpu_device *adev,
663 				 struct amdgpu_ring *ring)
664 {
665 	if (!ring || !ring->funcs->emit_wreg)
666 		WREG32_SOC15_NO_KIQ(NBIO, 0, mmHDP_READ_CACHE_INVALIDATE, 1);
667 	else
668 		amdgpu_ring_emit_wreg(ring, SOC15_REG_OFFSET(
669 			HDP, 0, mmHDP_READ_CACHE_INVALIDATE), 1);
670 }
671 
672 static bool soc15_need_full_reset(struct amdgpu_device *adev)
673 {
674 	/* change this when we implement soft reset */
675 	return true;
676 }
677 static void soc15_get_pcie_usage(struct amdgpu_device *adev, uint64_t *count0,
678 				 uint64_t *count1)
679 {
680 	uint32_t perfctr = 0;
681 	uint64_t cnt0_of, cnt1_of;
682 	int tmp;
683 
684 	/* This reports 0 on APUs, so return to avoid writing/reading registers
685 	 * that may or may not be different from their GPU counterparts
686 	 */
687 	 if (adev->flags & AMD_IS_APU)
688 		 return;
689 
690 	/* Set the 2 events that we wish to watch, defined above */
691 	/* Reg 40 is # received msgs, Reg 104 is # of posted requests sent */
692 	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT0_SEL, 40);
693 	perfctr = REG_SET_FIELD(perfctr, PCIE_PERF_CNTL_TXCLK, EVENT1_SEL, 104);
694 
695 	/* Write to enable desired perf counters */
696 	WREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK, perfctr);
697 	/* Zero out and enable the perf counters
698 	 * Write 0x5:
699 	 * Bit 0 = Start all counters(1)
700 	 * Bit 2 = Global counter reset enable(1)
701 	 */
702 	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000005);
703 
704 	msleep(1000);
705 
706 	/* Load the shadow and disable the perf counters
707 	 * Write 0x2:
708 	 * Bit 0 = Stop counters(0)
709 	 * Bit 1 = Load the shadow counters(1)
710 	 */
711 	WREG32_PCIE(smnPCIE_PERF_COUNT_CNTL, 0x00000002);
712 
713 	/* Read register values to get any >32bit overflow */
714 	tmp = RREG32_PCIE(smnPCIE_PERF_CNTL_TXCLK);
715 	cnt0_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER0_UPPER);
716 	cnt1_of = REG_GET_FIELD(tmp, PCIE_PERF_CNTL_TXCLK, COUNTER1_UPPER);
717 
718 	/* Get the values and add the overflow */
719 	*count0 = RREG32_PCIE(smnPCIE_PERF_COUNT0_TXCLK) | (cnt0_of << 32);
720 	*count1 = RREG32_PCIE(smnPCIE_PERF_COUNT1_TXCLK) | (cnt1_of << 32);
721 }
722 
723 static bool soc15_need_reset_on_init(struct amdgpu_device *adev)
724 {
725 	u32 sol_reg;
726 
727 	if (adev->flags & AMD_IS_APU)
728 		return false;
729 
730 	/* Check sOS sign of life register to confirm sys driver and sOS
731 	 * are already been loaded.
732 	 */
733 	sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
734 	if (sol_reg)
735 		return true;
736 
737 	return false;
738 }
739 
740 static const struct amdgpu_asic_funcs soc15_asic_funcs =
741 {
742 	.read_disabled_bios = &soc15_read_disabled_bios,
743 	.read_bios_from_rom = &soc15_read_bios_from_rom,
744 	.read_register = &soc15_read_register,
745 	.reset = &soc15_asic_reset,
746 	.set_vga_state = &soc15_vga_set_state,
747 	.get_xclk = &soc15_get_xclk,
748 	.set_uvd_clocks = &soc15_set_uvd_clocks,
749 	.set_vce_clocks = &soc15_set_vce_clocks,
750 	.get_config_memsize = &soc15_get_config_memsize,
751 	.flush_hdp = &soc15_flush_hdp,
752 	.invalidate_hdp = &soc15_invalidate_hdp,
753 	.need_full_reset = &soc15_need_full_reset,
754 	.init_doorbell_index = &vega10_doorbell_index_init,
755 	.get_pcie_usage = &soc15_get_pcie_usage,
756 	.need_reset_on_init = &soc15_need_reset_on_init,
757 };
758 
759 static const struct amdgpu_asic_funcs vega20_asic_funcs =
760 {
761 	.read_disabled_bios = &soc15_read_disabled_bios,
762 	.read_bios_from_rom = &soc15_read_bios_from_rom,
763 	.read_register = &soc15_read_register,
764 	.reset = &soc15_asic_reset,
765 	.set_vga_state = &soc15_vga_set_state,
766 	.get_xclk = &soc15_get_xclk,
767 	.set_uvd_clocks = &soc15_set_uvd_clocks,
768 	.set_vce_clocks = &soc15_set_vce_clocks,
769 	.get_config_memsize = &soc15_get_config_memsize,
770 	.flush_hdp = &soc15_flush_hdp,
771 	.invalidate_hdp = &soc15_invalidate_hdp,
772 	.need_full_reset = &soc15_need_full_reset,
773 	.init_doorbell_index = &vega20_doorbell_index_init,
774 	.get_pcie_usage = &soc15_get_pcie_usage,
775 	.need_reset_on_init = &soc15_need_reset_on_init,
776 };
777 
778 static int soc15_common_early_init(void *handle)
779 {
780 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
781 
782 	adev->smc_rreg = NULL;
783 	adev->smc_wreg = NULL;
784 	adev->pcie_rreg = &soc15_pcie_rreg;
785 	adev->pcie_wreg = &soc15_pcie_wreg;
786 	adev->uvd_ctx_rreg = &soc15_uvd_ctx_rreg;
787 	adev->uvd_ctx_wreg = &soc15_uvd_ctx_wreg;
788 	adev->didt_rreg = &soc15_didt_rreg;
789 	adev->didt_wreg = &soc15_didt_wreg;
790 	adev->gc_cac_rreg = &soc15_gc_cac_rreg;
791 	adev->gc_cac_wreg = &soc15_gc_cac_wreg;
792 	adev->se_cac_rreg = &soc15_se_cac_rreg;
793 	adev->se_cac_wreg = &soc15_se_cac_wreg;
794 
795 
796 	adev->external_rev_id = 0xFF;
797 	switch (adev->asic_type) {
798 	case CHIP_VEGA10:
799 		adev->asic_funcs = &soc15_asic_funcs;
800 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
801 			AMD_CG_SUPPORT_GFX_MGLS |
802 			AMD_CG_SUPPORT_GFX_RLC_LS |
803 			AMD_CG_SUPPORT_GFX_CP_LS |
804 			AMD_CG_SUPPORT_GFX_3D_CGCG |
805 			AMD_CG_SUPPORT_GFX_3D_CGLS |
806 			AMD_CG_SUPPORT_GFX_CGCG |
807 			AMD_CG_SUPPORT_GFX_CGLS |
808 			AMD_CG_SUPPORT_BIF_MGCG |
809 			AMD_CG_SUPPORT_BIF_LS |
810 			AMD_CG_SUPPORT_HDP_LS |
811 			AMD_CG_SUPPORT_DRM_MGCG |
812 			AMD_CG_SUPPORT_DRM_LS |
813 			AMD_CG_SUPPORT_ROM_MGCG |
814 			AMD_CG_SUPPORT_DF_MGCG |
815 			AMD_CG_SUPPORT_SDMA_MGCG |
816 			AMD_CG_SUPPORT_SDMA_LS |
817 			AMD_CG_SUPPORT_MC_MGCG |
818 			AMD_CG_SUPPORT_MC_LS;
819 		adev->pg_flags = 0;
820 		adev->external_rev_id = 0x1;
821 		break;
822 	case CHIP_VEGA12:
823 		adev->asic_funcs = &soc15_asic_funcs;
824 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
825 			AMD_CG_SUPPORT_GFX_MGLS |
826 			AMD_CG_SUPPORT_GFX_CGCG |
827 			AMD_CG_SUPPORT_GFX_CGLS |
828 			AMD_CG_SUPPORT_GFX_3D_CGCG |
829 			AMD_CG_SUPPORT_GFX_3D_CGLS |
830 			AMD_CG_SUPPORT_GFX_CP_LS |
831 			AMD_CG_SUPPORT_MC_LS |
832 			AMD_CG_SUPPORT_MC_MGCG |
833 			AMD_CG_SUPPORT_SDMA_MGCG |
834 			AMD_CG_SUPPORT_SDMA_LS |
835 			AMD_CG_SUPPORT_BIF_MGCG |
836 			AMD_CG_SUPPORT_BIF_LS |
837 			AMD_CG_SUPPORT_HDP_MGCG |
838 			AMD_CG_SUPPORT_HDP_LS |
839 			AMD_CG_SUPPORT_ROM_MGCG |
840 			AMD_CG_SUPPORT_VCE_MGCG |
841 			AMD_CG_SUPPORT_UVD_MGCG;
842 		adev->pg_flags = 0;
843 		adev->external_rev_id = adev->rev_id + 0x14;
844 		break;
845 	case CHIP_VEGA20:
846 		adev->asic_funcs = &vega20_asic_funcs;
847 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
848 			AMD_CG_SUPPORT_GFX_MGLS |
849 			AMD_CG_SUPPORT_GFX_CGCG |
850 			AMD_CG_SUPPORT_GFX_CGLS |
851 			AMD_CG_SUPPORT_GFX_3D_CGCG |
852 			AMD_CG_SUPPORT_GFX_3D_CGLS |
853 			AMD_CG_SUPPORT_GFX_CP_LS |
854 			AMD_CG_SUPPORT_MC_LS |
855 			AMD_CG_SUPPORT_MC_MGCG |
856 			AMD_CG_SUPPORT_SDMA_MGCG |
857 			AMD_CG_SUPPORT_SDMA_LS |
858 			AMD_CG_SUPPORT_BIF_MGCG |
859 			AMD_CG_SUPPORT_BIF_LS |
860 			AMD_CG_SUPPORT_HDP_MGCG |
861 			AMD_CG_SUPPORT_HDP_LS |
862 			AMD_CG_SUPPORT_ROM_MGCG |
863 			AMD_CG_SUPPORT_VCE_MGCG |
864 			AMD_CG_SUPPORT_UVD_MGCG;
865 		adev->pg_flags = 0;
866 		adev->external_rev_id = adev->rev_id + 0x28;
867 		break;
868 	case CHIP_RAVEN:
869 		adev->asic_funcs = &soc15_asic_funcs;
870 		if (adev->rev_id >= 0x8)
871 			adev->external_rev_id = adev->rev_id + 0x79;
872 		else if (adev->pdev->device == 0x15d8)
873 			adev->external_rev_id = adev->rev_id + 0x41;
874 		else if (adev->rev_id == 1)
875 			adev->external_rev_id = adev->rev_id + 0x20;
876 		else
877 			adev->external_rev_id = adev->rev_id + 0x01;
878 
879 		if (adev->rev_id >= 0x8) {
880 			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
881 				AMD_CG_SUPPORT_GFX_MGLS |
882 				AMD_CG_SUPPORT_GFX_CP_LS |
883 				AMD_CG_SUPPORT_GFX_3D_CGCG |
884 				AMD_CG_SUPPORT_GFX_3D_CGLS |
885 				AMD_CG_SUPPORT_GFX_CGCG |
886 				AMD_CG_SUPPORT_GFX_CGLS |
887 				AMD_CG_SUPPORT_BIF_LS |
888 				AMD_CG_SUPPORT_HDP_LS |
889 				AMD_CG_SUPPORT_ROM_MGCG |
890 				AMD_CG_SUPPORT_MC_MGCG |
891 				AMD_CG_SUPPORT_MC_LS |
892 				AMD_CG_SUPPORT_SDMA_MGCG |
893 				AMD_CG_SUPPORT_SDMA_LS |
894 				AMD_CG_SUPPORT_VCN_MGCG;
895 
896 			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
897 		} else if (adev->pdev->device == 0x15d8) {
898 			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGLS |
899 				AMD_CG_SUPPORT_GFX_CP_LS |
900 				AMD_CG_SUPPORT_GFX_3D_CGCG |
901 				AMD_CG_SUPPORT_GFX_3D_CGLS |
902 				AMD_CG_SUPPORT_GFX_CGCG |
903 				AMD_CG_SUPPORT_GFX_CGLS |
904 				AMD_CG_SUPPORT_BIF_LS |
905 				AMD_CG_SUPPORT_HDP_LS |
906 				AMD_CG_SUPPORT_ROM_MGCG |
907 				AMD_CG_SUPPORT_MC_MGCG |
908 				AMD_CG_SUPPORT_MC_LS |
909 				AMD_CG_SUPPORT_SDMA_MGCG |
910 				AMD_CG_SUPPORT_SDMA_LS;
911 
912 			adev->pg_flags = AMD_PG_SUPPORT_SDMA |
913 				AMD_PG_SUPPORT_MMHUB |
914 				AMD_PG_SUPPORT_VCN |
915 				AMD_PG_SUPPORT_VCN_DPG;
916 		} else {
917 			adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
918 				AMD_CG_SUPPORT_GFX_MGLS |
919 				AMD_CG_SUPPORT_GFX_RLC_LS |
920 				AMD_CG_SUPPORT_GFX_CP_LS |
921 				AMD_CG_SUPPORT_GFX_3D_CGCG |
922 				AMD_CG_SUPPORT_GFX_3D_CGLS |
923 				AMD_CG_SUPPORT_GFX_CGCG |
924 				AMD_CG_SUPPORT_GFX_CGLS |
925 				AMD_CG_SUPPORT_BIF_MGCG |
926 				AMD_CG_SUPPORT_BIF_LS |
927 				AMD_CG_SUPPORT_HDP_MGCG |
928 				AMD_CG_SUPPORT_HDP_LS |
929 				AMD_CG_SUPPORT_DRM_MGCG |
930 				AMD_CG_SUPPORT_DRM_LS |
931 				AMD_CG_SUPPORT_ROM_MGCG |
932 				AMD_CG_SUPPORT_MC_MGCG |
933 				AMD_CG_SUPPORT_MC_LS |
934 				AMD_CG_SUPPORT_SDMA_MGCG |
935 				AMD_CG_SUPPORT_SDMA_LS |
936 				AMD_CG_SUPPORT_VCN_MGCG;
937 
938 			adev->pg_flags = AMD_PG_SUPPORT_SDMA | AMD_PG_SUPPORT_VCN;
939 		}
940 
941 		if (adev->pm.pp_feature & PP_GFXOFF_MASK)
942 			adev->pg_flags |= AMD_PG_SUPPORT_GFX_PG |
943 				AMD_PG_SUPPORT_CP |
944 				AMD_PG_SUPPORT_RLC_SMU_HS;
945 		break;
946 	default:
947 		/* FIXME: not supported yet */
948 		return -EINVAL;
949 	}
950 
951 	if (amdgpu_sriov_vf(adev)) {
952 		amdgpu_virt_init_setting(adev);
953 		xgpu_ai_mailbox_set_irq_funcs(adev);
954 	}
955 
956 	return 0;
957 }
958 
959 static int soc15_common_late_init(void *handle)
960 {
961 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
962 
963 	if (amdgpu_sriov_vf(adev))
964 		xgpu_ai_mailbox_get_irq(adev);
965 
966 	return 0;
967 }
968 
969 static int soc15_common_sw_init(void *handle)
970 {
971 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
972 
973 	if (amdgpu_sriov_vf(adev))
974 		xgpu_ai_mailbox_add_irq_id(adev);
975 
976 	return 0;
977 }
978 
979 static int soc15_common_sw_fini(void *handle)
980 {
981 	return 0;
982 }
983 
984 static void soc15_doorbell_range_init(struct amdgpu_device *adev)
985 {
986 	int i;
987 	struct amdgpu_ring *ring;
988 
989 	for (i = 0; i < adev->sdma.num_instances; i++) {
990 		ring = &adev->sdma.instance[i].ring;
991 		adev->nbio_funcs->sdma_doorbell_range(adev, i,
992 			ring->use_doorbell, ring->doorbell_index,
993 			adev->doorbell_index.sdma_doorbell_range);
994 	}
995 
996 	adev->nbio_funcs->ih_doorbell_range(adev, adev->irq.ih.use_doorbell,
997 						adev->irq.ih.doorbell_index);
998 }
999 
1000 static int soc15_common_hw_init(void *handle)
1001 {
1002 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1003 
1004 	/* enable pcie gen2/3 link */
1005 	soc15_pcie_gen3_enable(adev);
1006 	/* enable aspm */
1007 	soc15_program_aspm(adev);
1008 	/* setup nbio registers */
1009 	adev->nbio_funcs->init_registers(adev);
1010 	/* enable the doorbell aperture */
1011 	soc15_enable_doorbell_aperture(adev, true);
1012 	/* HW doorbell routing policy: doorbell writing not
1013 	 * in SDMA/IH/MM/ACV range will be routed to CP. So
1014 	 * we need to init SDMA/IH/MM/ACV doorbell range prior
1015 	 * to CP ip block init and ring test.
1016 	 */
1017 	soc15_doorbell_range_init(adev);
1018 
1019 	return 0;
1020 }
1021 
1022 static int soc15_common_hw_fini(void *handle)
1023 {
1024 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1025 
1026 	/* disable the doorbell aperture */
1027 	soc15_enable_doorbell_aperture(adev, false);
1028 	if (amdgpu_sriov_vf(adev))
1029 		xgpu_ai_mailbox_put_irq(adev);
1030 
1031 	return 0;
1032 }
1033 
1034 static int soc15_common_suspend(void *handle)
1035 {
1036 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1037 
1038 	return soc15_common_hw_fini(adev);
1039 }
1040 
1041 static int soc15_common_resume(void *handle)
1042 {
1043 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1044 
1045 	return soc15_common_hw_init(adev);
1046 }
1047 
1048 static bool soc15_common_is_idle(void *handle)
1049 {
1050 	return true;
1051 }
1052 
1053 static int soc15_common_wait_for_idle(void *handle)
1054 {
1055 	return 0;
1056 }
1057 
1058 static int soc15_common_soft_reset(void *handle)
1059 {
1060 	return 0;
1061 }
1062 
1063 static void soc15_update_hdp_light_sleep(struct amdgpu_device *adev, bool enable)
1064 {
1065 	uint32_t def, data;
1066 
1067 	if (adev->asic_type == CHIP_VEGA20) {
1068 		def = data = RREG32(SOC15_REG_OFFSET(HDP, 0, mmHDP_MEM_POWER_CTRL));
1069 
1070 		if (enable && (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS))
1071 			data |= HDP_MEM_POWER_CTRL__IPH_MEM_POWER_CTRL_EN_MASK |
1072 				HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK |
1073 				HDP_MEM_POWER_CTRL__RC_MEM_POWER_CTRL_EN_MASK |
1074 				HDP_MEM_POWER_CTRL__RC_MEM_POWER_LS_EN_MASK;
1075 		else
1076 			data &= ~(HDP_MEM_POWER_CTRL__IPH_MEM_POWER_CTRL_EN_MASK |
1077 				HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK |
1078 				HDP_MEM_POWER_CTRL__RC_MEM_POWER_CTRL_EN_MASK |
1079 				HDP_MEM_POWER_CTRL__RC_MEM_POWER_LS_EN_MASK);
1080 
1081 		if (def != data)
1082 			WREG32(SOC15_REG_OFFSET(HDP, 0, mmHDP_MEM_POWER_CTRL), data);
1083 	} else {
1084 		def = data = RREG32(SOC15_REG_OFFSET(HDP, 0, mmHDP_MEM_POWER_LS));
1085 
1086 		if (enable && (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS))
1087 			data |= HDP_MEM_POWER_LS__LS_ENABLE_MASK;
1088 		else
1089 			data &= ~HDP_MEM_POWER_LS__LS_ENABLE_MASK;
1090 
1091 		if (def != data)
1092 			WREG32(SOC15_REG_OFFSET(HDP, 0, mmHDP_MEM_POWER_LS), data);
1093 	}
1094 }
1095 
1096 static void soc15_update_drm_clock_gating(struct amdgpu_device *adev, bool enable)
1097 {
1098 	uint32_t def, data;
1099 
1100 	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1101 
1102 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_MGCG))
1103 		data &= ~(0x01000000 |
1104 			  0x02000000 |
1105 			  0x04000000 |
1106 			  0x08000000 |
1107 			  0x10000000 |
1108 			  0x20000000 |
1109 			  0x40000000 |
1110 			  0x80000000);
1111 	else
1112 		data |= (0x01000000 |
1113 			 0x02000000 |
1114 			 0x04000000 |
1115 			 0x08000000 |
1116 			 0x10000000 |
1117 			 0x20000000 |
1118 			 0x40000000 |
1119 			 0x80000000);
1120 
1121 	if (def != data)
1122 		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0), data);
1123 }
1124 
1125 static void soc15_update_drm_light_sleep(struct amdgpu_device *adev, bool enable)
1126 {
1127 	uint32_t def, data;
1128 
1129 	def = data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1130 
1131 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_DRM_LS))
1132 		data |= 1;
1133 	else
1134 		data &= ~1;
1135 
1136 	if (def != data)
1137 		WREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL), data);
1138 }
1139 
1140 static void soc15_update_rom_medium_grain_clock_gating(struct amdgpu_device *adev,
1141 						       bool enable)
1142 {
1143 	uint32_t def, data;
1144 
1145 	def = data = RREG32(SOC15_REG_OFFSET(SMUIO, 0, mmCGTT_ROM_CLK_CTRL0));
1146 
1147 	if (enable && (adev->cg_flags & AMD_CG_SUPPORT_ROM_MGCG))
1148 		data &= ~(CGTT_ROM_CLK_CTRL0__SOFT_OVERRIDE0_MASK |
1149 			CGTT_ROM_CLK_CTRL0__SOFT_OVERRIDE1_MASK);
1150 	else
1151 		data |= CGTT_ROM_CLK_CTRL0__SOFT_OVERRIDE0_MASK |
1152 			CGTT_ROM_CLK_CTRL0__SOFT_OVERRIDE1_MASK;
1153 
1154 	if (def != data)
1155 		WREG32(SOC15_REG_OFFSET(SMUIO, 0, mmCGTT_ROM_CLK_CTRL0), data);
1156 }
1157 
1158 static int soc15_common_set_clockgating_state(void *handle,
1159 					    enum amd_clockgating_state state)
1160 {
1161 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1162 
1163 	if (amdgpu_sriov_vf(adev))
1164 		return 0;
1165 
1166 	switch (adev->asic_type) {
1167 	case CHIP_VEGA10:
1168 	case CHIP_VEGA12:
1169 	case CHIP_VEGA20:
1170 		adev->nbio_funcs->update_medium_grain_clock_gating(adev,
1171 				state == AMD_CG_STATE_GATE ? true : false);
1172 		adev->nbio_funcs->update_medium_grain_light_sleep(adev,
1173 				state == AMD_CG_STATE_GATE ? true : false);
1174 		soc15_update_hdp_light_sleep(adev,
1175 				state == AMD_CG_STATE_GATE ? true : false);
1176 		soc15_update_drm_clock_gating(adev,
1177 				state == AMD_CG_STATE_GATE ? true : false);
1178 		soc15_update_drm_light_sleep(adev,
1179 				state == AMD_CG_STATE_GATE ? true : false);
1180 		soc15_update_rom_medium_grain_clock_gating(adev,
1181 				state == AMD_CG_STATE_GATE ? true : false);
1182 		adev->df_funcs->update_medium_grain_clock_gating(adev,
1183 				state == AMD_CG_STATE_GATE ? true : false);
1184 		break;
1185 	case CHIP_RAVEN:
1186 		adev->nbio_funcs->update_medium_grain_clock_gating(adev,
1187 				state == AMD_CG_STATE_GATE ? true : false);
1188 		adev->nbio_funcs->update_medium_grain_light_sleep(adev,
1189 				state == AMD_CG_STATE_GATE ? true : false);
1190 		soc15_update_hdp_light_sleep(adev,
1191 				state == AMD_CG_STATE_GATE ? true : false);
1192 		soc15_update_drm_clock_gating(adev,
1193 				state == AMD_CG_STATE_GATE ? true : false);
1194 		soc15_update_drm_light_sleep(adev,
1195 				state == AMD_CG_STATE_GATE ? true : false);
1196 		soc15_update_rom_medium_grain_clock_gating(adev,
1197 				state == AMD_CG_STATE_GATE ? true : false);
1198 		break;
1199 	default:
1200 		break;
1201 	}
1202 	return 0;
1203 }
1204 
1205 static void soc15_common_get_clockgating_state(void *handle, u32 *flags)
1206 {
1207 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1208 	int data;
1209 
1210 	if (amdgpu_sriov_vf(adev))
1211 		*flags = 0;
1212 
1213 	adev->nbio_funcs->get_clockgating_state(adev, flags);
1214 
1215 	/* AMD_CG_SUPPORT_HDP_LS */
1216 	data = RREG32(SOC15_REG_OFFSET(HDP, 0, mmHDP_MEM_POWER_LS));
1217 	if (data & HDP_MEM_POWER_LS__LS_ENABLE_MASK)
1218 		*flags |= AMD_CG_SUPPORT_HDP_LS;
1219 
1220 	/* AMD_CG_SUPPORT_DRM_MGCG */
1221 	data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_CGTT_CTRL0));
1222 	if (!(data & 0x01000000))
1223 		*flags |= AMD_CG_SUPPORT_DRM_MGCG;
1224 
1225 	/* AMD_CG_SUPPORT_DRM_LS */
1226 	data = RREG32(SOC15_REG_OFFSET(MP0, 0, mmMP0_MISC_LIGHT_SLEEP_CTRL));
1227 	if (data & 0x1)
1228 		*flags |= AMD_CG_SUPPORT_DRM_LS;
1229 
1230 	/* AMD_CG_SUPPORT_ROM_MGCG */
1231 	data = RREG32(SOC15_REG_OFFSET(SMUIO, 0, mmCGTT_ROM_CLK_CTRL0));
1232 	if (!(data & CGTT_ROM_CLK_CTRL0__SOFT_OVERRIDE0_MASK))
1233 		*flags |= AMD_CG_SUPPORT_ROM_MGCG;
1234 
1235 	adev->df_funcs->get_clockgating_state(adev, flags);
1236 }
1237 
1238 static int soc15_common_set_powergating_state(void *handle,
1239 					    enum amd_powergating_state state)
1240 {
1241 	/* todo */
1242 	return 0;
1243 }
1244 
1245 const struct amd_ip_funcs soc15_common_ip_funcs = {
1246 	.name = "soc15_common",
1247 	.early_init = soc15_common_early_init,
1248 	.late_init = soc15_common_late_init,
1249 	.sw_init = soc15_common_sw_init,
1250 	.sw_fini = soc15_common_sw_fini,
1251 	.hw_init = soc15_common_hw_init,
1252 	.hw_fini = soc15_common_hw_fini,
1253 	.suspend = soc15_common_suspend,
1254 	.resume = soc15_common_resume,
1255 	.is_idle = soc15_common_is_idle,
1256 	.wait_for_idle = soc15_common_wait_for_idle,
1257 	.soft_reset = soc15_common_soft_reset,
1258 	.set_clockgating_state = soc15_common_set_clockgating_state,
1259 	.set_powergating_state = soc15_common_set_powergating_state,
1260 	.get_clockgating_state= soc15_common_get_clockgating_state,
1261 };
1262