xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/nv.c (revision fced3c3a)
1c6b6a421SHawking Zhang /*
2c6b6a421SHawking Zhang  * Copyright 2019 Advanced Micro Devices, Inc.
3c6b6a421SHawking Zhang  *
4c6b6a421SHawking Zhang  * Permission is hereby granted, free of charge, to any person obtaining a
5c6b6a421SHawking Zhang  * copy of this software and associated documentation files (the "Software"),
6c6b6a421SHawking Zhang  * to deal in the Software without restriction, including without limitation
7c6b6a421SHawking Zhang  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c6b6a421SHawking Zhang  * and/or sell copies of the Software, and to permit persons to whom the
9c6b6a421SHawking Zhang  * Software is furnished to do so, subject to the following conditions:
10c6b6a421SHawking Zhang  *
11c6b6a421SHawking Zhang  * The above copyright notice and this permission notice shall be included in
12c6b6a421SHawking Zhang  * all copies or substantial portions of the Software.
13c6b6a421SHawking Zhang  *
14c6b6a421SHawking Zhang  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c6b6a421SHawking Zhang  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c6b6a421SHawking Zhang  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17c6b6a421SHawking Zhang  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c6b6a421SHawking Zhang  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c6b6a421SHawking Zhang  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c6b6a421SHawking Zhang  * OTHER DEALINGS IN THE SOFTWARE.
21c6b6a421SHawking Zhang  *
22c6b6a421SHawking Zhang  */
23c6b6a421SHawking Zhang #include <linux/firmware.h>
24c6b6a421SHawking Zhang #include <linux/slab.h>
25c6b6a421SHawking Zhang #include <linux/module.h>
26e9eea902SAlex Deucher #include <linux/pci.h>
27e9eea902SAlex Deucher 
28c6b6a421SHawking Zhang #include "amdgpu.h"
29c6b6a421SHawking Zhang #include "amdgpu_atombios.h"
30c6b6a421SHawking Zhang #include "amdgpu_ih.h"
31c6b6a421SHawking Zhang #include "amdgpu_uvd.h"
32c6b6a421SHawking Zhang #include "amdgpu_vce.h"
33c6b6a421SHawking Zhang #include "amdgpu_ucode.h"
34c6b6a421SHawking Zhang #include "amdgpu_psp.h"
35767acabdSKevin Wang #include "amdgpu_smu.h"
36c6b6a421SHawking Zhang #include "atom.h"
37c6b6a421SHawking Zhang #include "amd_pcie.h"
38c6b6a421SHawking Zhang 
39c6b6a421SHawking Zhang #include "gc/gc_10_1_0_offset.h"
40c6b6a421SHawking Zhang #include "gc/gc_10_1_0_sh_mask.h"
41c6b6a421SHawking Zhang #include "hdp/hdp_5_0_0_offset.h"
42c6b6a421SHawking Zhang #include "hdp/hdp_5_0_0_sh_mask.h"
4329bc37b4SAlex Deucher #include "smuio/smuio_11_0_0_offset.h"
443967ae6dSAlex Deucher #include "mp/mp_11_0_offset.h"
45c6b6a421SHawking Zhang 
46c6b6a421SHawking Zhang #include "soc15.h"
47c6b6a421SHawking Zhang #include "soc15_common.h"
48c6b6a421SHawking Zhang #include "gmc_v10_0.h"
49c6b6a421SHawking Zhang #include "gfxhub_v2_0.h"
50c6b6a421SHawking Zhang #include "mmhub_v2_0.h"
51bebc0762SHawking Zhang #include "nbio_v2_3.h"
52c6b6a421SHawking Zhang #include "nv.h"
53c6b6a421SHawking Zhang #include "navi10_ih.h"
54c6b6a421SHawking Zhang #include "gfx_v10_0.h"
55c6b6a421SHawking Zhang #include "sdma_v5_0.h"
56157e72e8SLikun Gao #include "sdma_v5_2.h"
57c6b6a421SHawking Zhang #include "vcn_v2_0.h"
585be45a26SLeo Liu #include "jpeg_v2_0.h"
59b8f10585SLeo Liu #include "vcn_v3_0.h"
604d72dd12SLeo Liu #include "jpeg_v3_0.h"
61c6b6a421SHawking Zhang #include "dce_virtual.h"
62c6b6a421SHawking Zhang #include "mes_v10_1.h"
63b05b6903SJiange Zhao #include "mxgpu_nv.h"
64c6b6a421SHawking Zhang 
65c6b6a421SHawking Zhang static const struct amd_ip_funcs nv_common_ip_funcs;
66c6b6a421SHawking Zhang 
67c6b6a421SHawking Zhang /*
68c6b6a421SHawking Zhang  * Indirect registers accessor
69c6b6a421SHawking Zhang  */
70c6b6a421SHawking Zhang static u32 nv_pcie_rreg(struct amdgpu_device *adev, u32 reg)
71c6b6a421SHawking Zhang {
72705a2b5bSHawking Zhang 	unsigned long address, data;
73bebc0762SHawking Zhang 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
74bebc0762SHawking Zhang 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
75c6b6a421SHawking Zhang 
76705a2b5bSHawking Zhang 	return amdgpu_device_indirect_rreg(adev, address, data, reg);
77c6b6a421SHawking Zhang }
78c6b6a421SHawking Zhang 
79c6b6a421SHawking Zhang static void nv_pcie_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
80c6b6a421SHawking Zhang {
81705a2b5bSHawking Zhang 	unsigned long address, data;
82c6b6a421SHawking Zhang 
83bebc0762SHawking Zhang 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
84bebc0762SHawking Zhang 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
85c6b6a421SHawking Zhang 
86705a2b5bSHawking Zhang 	amdgpu_device_indirect_wreg(adev, address, data, reg, v);
87c6b6a421SHawking Zhang }
88c6b6a421SHawking Zhang 
894922f1bcSJohn Clements static u64 nv_pcie_rreg64(struct amdgpu_device *adev, u32 reg)
904922f1bcSJohn Clements {
91705a2b5bSHawking Zhang 	unsigned long address, data;
924922f1bcSJohn Clements 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
934922f1bcSJohn Clements 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
944922f1bcSJohn Clements 
95705a2b5bSHawking Zhang 	return amdgpu_device_indirect_rreg64(adev, address, data, reg);
964922f1bcSJohn Clements }
974922f1bcSJohn Clements 
984922f1bcSJohn Clements static void nv_pcie_wreg64(struct amdgpu_device *adev, u32 reg, u64 v)
994922f1bcSJohn Clements {
100705a2b5bSHawking Zhang 	unsigned long address, data;
1014922f1bcSJohn Clements 
1024922f1bcSJohn Clements 	address = adev->nbio.funcs->get_pcie_index_offset(adev);
1034922f1bcSJohn Clements 	data = adev->nbio.funcs->get_pcie_data_offset(adev);
1044922f1bcSJohn Clements 
105705a2b5bSHawking Zhang 	amdgpu_device_indirect_wreg64(adev, address, data, reg, v);
1064922f1bcSJohn Clements }
1074922f1bcSJohn Clements 
108c6b6a421SHawking Zhang static u32 nv_didt_rreg(struct amdgpu_device *adev, u32 reg)
109c6b6a421SHawking Zhang {
110c6b6a421SHawking Zhang 	unsigned long flags, address, data;
111c6b6a421SHawking Zhang 	u32 r;
112c6b6a421SHawking Zhang 
113c6b6a421SHawking Zhang 	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
114c6b6a421SHawking Zhang 	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
115c6b6a421SHawking Zhang 
116c6b6a421SHawking Zhang 	spin_lock_irqsave(&adev->didt_idx_lock, flags);
117c6b6a421SHawking Zhang 	WREG32(address, (reg));
118c6b6a421SHawking Zhang 	r = RREG32(data);
119c6b6a421SHawking Zhang 	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
120c6b6a421SHawking Zhang 	return r;
121c6b6a421SHawking Zhang }
122c6b6a421SHawking Zhang 
123c6b6a421SHawking Zhang static void nv_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
124c6b6a421SHawking Zhang {
125c6b6a421SHawking Zhang 	unsigned long flags, address, data;
126c6b6a421SHawking Zhang 
127c6b6a421SHawking Zhang 	address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
128c6b6a421SHawking Zhang 	data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
129c6b6a421SHawking Zhang 
130c6b6a421SHawking Zhang 	spin_lock_irqsave(&adev->didt_idx_lock, flags);
131c6b6a421SHawking Zhang 	WREG32(address, (reg));
132c6b6a421SHawking Zhang 	WREG32(data, (v));
133c6b6a421SHawking Zhang 	spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
134c6b6a421SHawking Zhang }
135c6b6a421SHawking Zhang 
136c6b6a421SHawking Zhang static u32 nv_get_config_memsize(struct amdgpu_device *adev)
137c6b6a421SHawking Zhang {
138bebc0762SHawking Zhang 	return adev->nbio.funcs->get_memsize(adev);
139c6b6a421SHawking Zhang }
140c6b6a421SHawking Zhang 
141c6b6a421SHawking Zhang static u32 nv_get_xclk(struct amdgpu_device *adev)
142c6b6a421SHawking Zhang {
143462a70d8STao Zhou 	return adev->clock.spll.reference_freq;
144c6b6a421SHawking Zhang }
145c6b6a421SHawking Zhang 
146c6b6a421SHawking Zhang 
147c6b6a421SHawking Zhang void nv_grbm_select(struct amdgpu_device *adev,
148c6b6a421SHawking Zhang 		     u32 me, u32 pipe, u32 queue, u32 vmid)
149c6b6a421SHawking Zhang {
150c6b6a421SHawking Zhang 	u32 grbm_gfx_cntl = 0;
151c6b6a421SHawking Zhang 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
152c6b6a421SHawking Zhang 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
153c6b6a421SHawking Zhang 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
154c6b6a421SHawking Zhang 	grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
155c6b6a421SHawking Zhang 
156c6b6a421SHawking Zhang 	WREG32(SOC15_REG_OFFSET(GC, 0, mmGRBM_GFX_CNTL), grbm_gfx_cntl);
157c6b6a421SHawking Zhang }
158c6b6a421SHawking Zhang 
159c6b6a421SHawking Zhang static void nv_vga_set_state(struct amdgpu_device *adev, bool state)
160c6b6a421SHawking Zhang {
161c6b6a421SHawking Zhang 	/* todo */
162c6b6a421SHawking Zhang }
163c6b6a421SHawking Zhang 
164c6b6a421SHawking Zhang static bool nv_read_disabled_bios(struct amdgpu_device *adev)
165c6b6a421SHawking Zhang {
166c6b6a421SHawking Zhang 	/* todo */
167c6b6a421SHawking Zhang 	return false;
168c6b6a421SHawking Zhang }
169c6b6a421SHawking Zhang 
170c6b6a421SHawking Zhang static bool nv_read_bios_from_rom(struct amdgpu_device *adev,
171c6b6a421SHawking Zhang 				  u8 *bios, u32 length_bytes)
172c6b6a421SHawking Zhang {
17329bc37b4SAlex Deucher 	u32 *dw_ptr;
17429bc37b4SAlex Deucher 	u32 i, length_dw;
17529bc37b4SAlex Deucher 
17629bc37b4SAlex Deucher 	if (bios == NULL)
177c6b6a421SHawking Zhang 		return false;
17829bc37b4SAlex Deucher 	if (length_bytes == 0)
17929bc37b4SAlex Deucher 		return false;
18029bc37b4SAlex Deucher 	/* APU vbios image is part of sbios image */
18129bc37b4SAlex Deucher 	if (adev->flags & AMD_IS_APU)
18229bc37b4SAlex Deucher 		return false;
18329bc37b4SAlex Deucher 
18429bc37b4SAlex Deucher 	dw_ptr = (u32 *)bios;
18529bc37b4SAlex Deucher 	length_dw = ALIGN(length_bytes, 4) / 4;
18629bc37b4SAlex Deucher 
18729bc37b4SAlex Deucher 	/* set rom index to 0 */
18829bc37b4SAlex Deucher 	WREG32(SOC15_REG_OFFSET(SMUIO, 0, mmROM_INDEX), 0);
18929bc37b4SAlex Deucher 	/* read out the rom data */
19029bc37b4SAlex Deucher 	for (i = 0; i < length_dw; i++)
19129bc37b4SAlex Deucher 		dw_ptr[i] = RREG32(SOC15_REG_OFFSET(SMUIO, 0, mmROM_DATA));
19229bc37b4SAlex Deucher 
19329bc37b4SAlex Deucher 	return true;
194c6b6a421SHawking Zhang }
195c6b6a421SHawking Zhang 
196c6b6a421SHawking Zhang static struct soc15_allowed_register_entry nv_allowed_read_registers[] = {
197c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)},
198c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)},
199c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)},
200c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)},
201c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)},
202c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)},
203c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)},
204c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)},
205c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STAT)},
206c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)},
207c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)},
208c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)},
209c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)},
210c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)},
211c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)},
212664fe85aSMarek Olšák 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_BUSY_STAT)},
213c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)},
214c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)},
215c6b6a421SHawking Zhang 	{ SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)},
216c6b6a421SHawking Zhang };
217c6b6a421SHawking Zhang 
218c6b6a421SHawking Zhang static uint32_t nv_read_indexed_register(struct amdgpu_device *adev, u32 se_num,
219c6b6a421SHawking Zhang 					 u32 sh_num, u32 reg_offset)
220c6b6a421SHawking Zhang {
221c6b6a421SHawking Zhang 	uint32_t val;
222c6b6a421SHawking Zhang 
223c6b6a421SHawking Zhang 	mutex_lock(&adev->grbm_idx_mutex);
224c6b6a421SHawking Zhang 	if (se_num != 0xffffffff || sh_num != 0xffffffff)
225c6b6a421SHawking Zhang 		amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff);
226c6b6a421SHawking Zhang 
227c6b6a421SHawking Zhang 	val = RREG32(reg_offset);
228c6b6a421SHawking Zhang 
229c6b6a421SHawking Zhang 	if (se_num != 0xffffffff || sh_num != 0xffffffff)
230c6b6a421SHawking Zhang 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
231c6b6a421SHawking Zhang 	mutex_unlock(&adev->grbm_idx_mutex);
232c6b6a421SHawking Zhang 	return val;
233c6b6a421SHawking Zhang }
234c6b6a421SHawking Zhang 
235c6b6a421SHawking Zhang static uint32_t nv_get_register_value(struct amdgpu_device *adev,
236c6b6a421SHawking Zhang 				      bool indexed, u32 se_num,
237c6b6a421SHawking Zhang 				      u32 sh_num, u32 reg_offset)
238c6b6a421SHawking Zhang {
239c6b6a421SHawking Zhang 	if (indexed) {
240c6b6a421SHawking Zhang 		return nv_read_indexed_register(adev, se_num, sh_num, reg_offset);
241c6b6a421SHawking Zhang 	} else {
242c6b6a421SHawking Zhang 		if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG))
243c6b6a421SHawking Zhang 			return adev->gfx.config.gb_addr_config;
244c6b6a421SHawking Zhang 		return RREG32(reg_offset);
245c6b6a421SHawking Zhang 	}
246c6b6a421SHawking Zhang }
247c6b6a421SHawking Zhang 
248c6b6a421SHawking Zhang static int nv_read_register(struct amdgpu_device *adev, u32 se_num,
249c6b6a421SHawking Zhang 			    u32 sh_num, u32 reg_offset, u32 *value)
250c6b6a421SHawking Zhang {
251c6b6a421SHawking Zhang 	uint32_t i;
252c6b6a421SHawking Zhang 	struct soc15_allowed_register_entry  *en;
253c6b6a421SHawking Zhang 
254c6b6a421SHawking Zhang 	*value = 0;
255c6b6a421SHawking Zhang 	for (i = 0; i < ARRAY_SIZE(nv_allowed_read_registers); i++) {
256c6b6a421SHawking Zhang 		en = &nv_allowed_read_registers[i];
257*fced3c3aSHuang Rui 		if ((i == 7 && (adev->sdma.num_instances == 1)) || /* some asics don't have SDMA1 */
258*fced3c3aSHuang Rui 		    reg_offset !=
259c6b6a421SHawking Zhang 		    (adev->reg_offset[en->hwip][en->inst][en->seg] + en->reg_offset))
260c6b6a421SHawking Zhang 			continue;
261c6b6a421SHawking Zhang 
262c6b6a421SHawking Zhang 		*value = nv_get_register_value(adev,
263c6b6a421SHawking Zhang 					       nv_allowed_read_registers[i].grbm_indexed,
264c6b6a421SHawking Zhang 					       se_num, sh_num, reg_offset);
265c6b6a421SHawking Zhang 		return 0;
266c6b6a421SHawking Zhang 	}
267c6b6a421SHawking Zhang 	return -EINVAL;
268c6b6a421SHawking Zhang }
269c6b6a421SHawking Zhang 
2703e2bb60aSKevin Wang static int nv_asic_mode1_reset(struct amdgpu_device *adev)
2713e2bb60aSKevin Wang {
2723e2bb60aSKevin Wang 	u32 i;
2733e2bb60aSKevin Wang 	int ret = 0;
2743e2bb60aSKevin Wang 
2753e2bb60aSKevin Wang 	amdgpu_atombios_scratch_regs_engine_hung(adev, true);
2763e2bb60aSKevin Wang 
2773e2bb60aSKevin Wang 	/* disable BM */
2783e2bb60aSKevin Wang 	pci_clear_master(adev->pdev);
2793e2bb60aSKevin Wang 
280c1dd4aa6SAndrey Grodzovsky 	amdgpu_device_cache_pci_state(adev->pdev);
2813e2bb60aSKevin Wang 
282311531f0SWenhui Sheng 	if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
283311531f0SWenhui Sheng 		dev_info(adev->dev, "GPU smu mode1 reset\n");
284311531f0SWenhui Sheng 		ret = amdgpu_dpm_mode1_reset(adev);
285311531f0SWenhui Sheng 	} else {
286311531f0SWenhui Sheng 		dev_info(adev->dev, "GPU psp mode1 reset\n");
2873e2bb60aSKevin Wang 		ret = psp_gpu_reset(adev);
288311531f0SWenhui Sheng 	}
289311531f0SWenhui Sheng 
2903e2bb60aSKevin Wang 	if (ret)
2913e2bb60aSKevin Wang 		dev_err(adev->dev, "GPU mode1 reset failed\n");
292c1dd4aa6SAndrey Grodzovsky 	amdgpu_device_load_pci_state(adev->pdev);
2933e2bb60aSKevin Wang 
2943e2bb60aSKevin Wang 	/* wait for asic to come out of reset */
2953e2bb60aSKevin Wang 	for (i = 0; i < adev->usec_timeout; i++) {
296bebc0762SHawking Zhang 		u32 memsize = adev->nbio.funcs->get_memsize(adev);
2973e2bb60aSKevin Wang 
2983e2bb60aSKevin Wang 		if (memsize != 0xffffffff)
2993e2bb60aSKevin Wang 			break;
3003e2bb60aSKevin Wang 		udelay(1);
3013e2bb60aSKevin Wang 	}
3023e2bb60aSKevin Wang 
3033e2bb60aSKevin Wang 	amdgpu_atombios_scratch_regs_engine_hung(adev, false);
3043e2bb60aSKevin Wang 
3053e2bb60aSKevin Wang 	return ret;
3063e2bb60aSKevin Wang }
3072ddc6c3eSAlex Deucher 
308ac742616SAlex Deucher static bool nv_asic_supports_baco(struct amdgpu_device *adev)
309ac742616SAlex Deucher {
310ac742616SAlex Deucher 	struct smu_context *smu = &adev->smu;
311ac742616SAlex Deucher 
312ac742616SAlex Deucher 	if (smu_baco_is_support(smu))
313ac742616SAlex Deucher 		return true;
314ac742616SAlex Deucher 	else
315ac742616SAlex Deucher 		return false;
316ac742616SAlex Deucher }
317ac742616SAlex Deucher 
3182ddc6c3eSAlex Deucher static enum amd_reset_method
3192ddc6c3eSAlex Deucher nv_asic_reset_method(struct amdgpu_device *adev)
3202ddc6c3eSAlex Deucher {
3212ddc6c3eSAlex Deucher 	struct smu_context *smu = &adev->smu;
3222ddc6c3eSAlex Deucher 
323273da6ffSWenhui Sheng 	if (amdgpu_reset_method == AMD_RESET_METHOD_MODE1 ||
324273da6ffSWenhui Sheng 	    amdgpu_reset_method == AMD_RESET_METHOD_BACO)
325273da6ffSWenhui Sheng 		return amdgpu_reset_method;
326273da6ffSWenhui Sheng 
327273da6ffSWenhui Sheng 	if (amdgpu_reset_method != -1)
328273da6ffSWenhui Sheng 		dev_warn(adev->dev, "Specified reset method:%d isn't supported, using AUTO instead.\n",
329273da6ffSWenhui Sheng 				  amdgpu_reset_method);
330273da6ffSWenhui Sheng 
331ca6fd7a6SLikun Gao 	switch (adev->asic_type) {
332ca6fd7a6SLikun Gao 	case CHIP_SIENNA_CICHLID:
33322dd44f4SJiansong Chen 	case CHIP_NAVY_FLOUNDER:
334ca6fd7a6SLikun Gao 		return AMD_RESET_METHOD_MODE1;
335ca6fd7a6SLikun Gao 	default:
336311531f0SWenhui Sheng 		if (smu_baco_is_support(smu))
3372ddc6c3eSAlex Deucher 			return AMD_RESET_METHOD_BACO;
3382ddc6c3eSAlex Deucher 		else
3392ddc6c3eSAlex Deucher 			return AMD_RESET_METHOD_MODE1;
3402ddc6c3eSAlex Deucher 	}
341ca6fd7a6SLikun Gao }
3422ddc6c3eSAlex Deucher 
343c6b6a421SHawking Zhang static int nv_asic_reset(struct amdgpu_device *adev)
344c6b6a421SHawking Zhang {
345767acabdSKevin Wang 	int ret = 0;
346767acabdSKevin Wang 	struct smu_context *smu = &adev->smu;
347c6b6a421SHawking Zhang 
348e3526257SMonk Liu 	if (nv_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
34911043b7aSAlex Deucher 		dev_info(adev->dev, "BACO reset\n");
350311531f0SWenhui Sheng 
35111520f27SAlex Deucher 		ret = smu_baco_enter(smu);
35211520f27SAlex Deucher 		if (ret)
35311520f27SAlex Deucher 			return ret;
35411520f27SAlex Deucher 		ret = smu_baco_exit(smu);
35511520f27SAlex Deucher 		if (ret)
35611520f27SAlex Deucher 			return ret;
35711043b7aSAlex Deucher 	} else {
35811043b7aSAlex Deucher 		dev_info(adev->dev, "MODE1 reset\n");
3593e2bb60aSKevin Wang 		ret = nv_asic_mode1_reset(adev);
36011043b7aSAlex Deucher 	}
361767acabdSKevin Wang 
362767acabdSKevin Wang 	return ret;
363c6b6a421SHawking Zhang }
364c6b6a421SHawking Zhang 
365c6b6a421SHawking Zhang static int nv_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
366c6b6a421SHawking Zhang {
367c6b6a421SHawking Zhang 	/* todo */
368c6b6a421SHawking Zhang 	return 0;
369c6b6a421SHawking Zhang }
370c6b6a421SHawking Zhang 
371c6b6a421SHawking Zhang static int nv_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
372c6b6a421SHawking Zhang {
373c6b6a421SHawking Zhang 	/* todo */
374c6b6a421SHawking Zhang 	return 0;
375c6b6a421SHawking Zhang }
376c6b6a421SHawking Zhang 
377c6b6a421SHawking Zhang static void nv_pcie_gen3_enable(struct amdgpu_device *adev)
378c6b6a421SHawking Zhang {
379c6b6a421SHawking Zhang 	if (pci_is_root_bus(adev->pdev->bus))
380c6b6a421SHawking Zhang 		return;
381c6b6a421SHawking Zhang 
382c6b6a421SHawking Zhang 	if (amdgpu_pcie_gen2 == 0)
383c6b6a421SHawking Zhang 		return;
384c6b6a421SHawking Zhang 
385c6b6a421SHawking Zhang 	if (!(adev->pm.pcie_gen_mask & (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
386c6b6a421SHawking Zhang 					CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)))
387c6b6a421SHawking Zhang 		return;
388c6b6a421SHawking Zhang 
389c6b6a421SHawking Zhang 	/* todo */
390c6b6a421SHawking Zhang }
391c6b6a421SHawking Zhang 
392c6b6a421SHawking Zhang static void nv_program_aspm(struct amdgpu_device *adev)
393c6b6a421SHawking Zhang {
394c6b6a421SHawking Zhang 
395c6b6a421SHawking Zhang 	if (amdgpu_aspm == 0)
396c6b6a421SHawking Zhang 		return;
397c6b6a421SHawking Zhang 
398c6b6a421SHawking Zhang 	/* todo */
399c6b6a421SHawking Zhang }
400c6b6a421SHawking Zhang 
401c6b6a421SHawking Zhang static void nv_enable_doorbell_aperture(struct amdgpu_device *adev,
402c6b6a421SHawking Zhang 					bool enable)
403c6b6a421SHawking Zhang {
404bebc0762SHawking Zhang 	adev->nbio.funcs->enable_doorbell_aperture(adev, enable);
405bebc0762SHawking Zhang 	adev->nbio.funcs->enable_doorbell_selfring_aperture(adev, enable);
406c6b6a421SHawking Zhang }
407c6b6a421SHawking Zhang 
408c6b6a421SHawking Zhang static const struct amdgpu_ip_block_version nv_common_ip_block =
409c6b6a421SHawking Zhang {
410c6b6a421SHawking Zhang 	.type = AMD_IP_BLOCK_TYPE_COMMON,
411c6b6a421SHawking Zhang 	.major = 1,
412c6b6a421SHawking Zhang 	.minor = 0,
413c6b6a421SHawking Zhang 	.rev = 0,
414c6b6a421SHawking Zhang 	.funcs = &nv_common_ip_funcs,
415c6b6a421SHawking Zhang };
416c6b6a421SHawking Zhang 
417b5c73856SXiaojie Yuan static int nv_reg_base_init(struct amdgpu_device *adev)
418c6b6a421SHawking Zhang {
419b5c73856SXiaojie Yuan 	int r;
420b5c73856SXiaojie Yuan 
421b5c73856SXiaojie Yuan 	if (amdgpu_discovery) {
422b5c73856SXiaojie Yuan 		r = amdgpu_discovery_reg_base_init(adev);
423b5c73856SXiaojie Yuan 		if (r) {
424b5c73856SXiaojie Yuan 			DRM_WARN("failed to init reg base from ip discovery table, "
425b5c73856SXiaojie Yuan 					"fallback to legacy init method\n");
426b5c73856SXiaojie Yuan 			goto legacy_init;
427b5c73856SXiaojie Yuan 		}
428b5c73856SXiaojie Yuan 
429b5c73856SXiaojie Yuan 		return 0;
430b5c73856SXiaojie Yuan 	}
431b5c73856SXiaojie Yuan 
432b5c73856SXiaojie Yuan legacy_init:
433c6b6a421SHawking Zhang 	switch (adev->asic_type) {
434c6b6a421SHawking Zhang 	case CHIP_NAVI10:
435c6b6a421SHawking Zhang 		navi10_reg_base_init(adev);
436c6b6a421SHawking Zhang 		break;
437a0f6d926SXiaojie Yuan 	case CHIP_NAVI14:
438a0f6d926SXiaojie Yuan 		navi14_reg_base_init(adev);
439a0f6d926SXiaojie Yuan 		break;
44003d0a073SXiaojie Yuan 	case CHIP_NAVI12:
44103d0a073SXiaojie Yuan 		navi12_reg_base_init(adev);
44203d0a073SXiaojie Yuan 		break;
443dccdbf3fSLikun Gao 	case CHIP_SIENNA_CICHLID:
444c8c959f6SJiansong Chen 	case CHIP_NAVY_FLOUNDER:
445dccdbf3fSLikun Gao 		sienna_cichlid_reg_base_init(adev);
446dccdbf3fSLikun Gao 		break;
447026570e6SHuang Rui 	case CHIP_VANGOGH:
448026570e6SHuang Rui 		vangogh_reg_base_init(adev);
449026570e6SHuang Rui 		break;
450c6b6a421SHawking Zhang 	default:
451c6b6a421SHawking Zhang 		return -EINVAL;
452c6b6a421SHawking Zhang 	}
453c6b6a421SHawking Zhang 
454b5c73856SXiaojie Yuan 	return 0;
455b5c73856SXiaojie Yuan }
456b5c73856SXiaojie Yuan 
457c1299461SWenhui Sheng void nv_set_virt_ops(struct amdgpu_device *adev)
458c1299461SWenhui Sheng {
459c1299461SWenhui Sheng 	adev->virt.ops = &xgpu_nv_virt_ops;
460c1299461SWenhui Sheng }
461c1299461SWenhui Sheng 
462b5c73856SXiaojie Yuan int nv_set_ip_blocks(struct amdgpu_device *adev)
463b5c73856SXiaojie Yuan {
464b5c73856SXiaojie Yuan 	int r;
465b5c73856SXiaojie Yuan 
466122078deSMonk Liu 	adev->nbio.funcs = &nbio_v2_3_funcs;
467122078deSMonk Liu 	adev->nbio.hdp_flush_reg = &nbio_v2_3_hdp_flush_reg;
468122078deSMonk Liu 
469c652923aSJohn Clements 	if (adev->asic_type == CHIP_SIENNA_CICHLID)
470c652923aSJohn Clements 		adev->gmc.xgmi.supported = true;
471c652923aSJohn Clements 
472b5c73856SXiaojie Yuan 	/* Set IP register base before any HW register access */
473b5c73856SXiaojie Yuan 	r = nv_reg_base_init(adev);
474b5c73856SXiaojie Yuan 	if (r)
475b5c73856SXiaojie Yuan 		return r;
476b5c73856SXiaojie Yuan 
477c6b6a421SHawking Zhang 	switch (adev->asic_type) {
478c6b6a421SHawking Zhang 	case CHIP_NAVI10:
479d1daf850SAlex Deucher 	case CHIP_NAVI14:
480c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
481c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
482c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
483c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
484c6b6a421SHawking Zhang 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP &&
4859530273eSEvan Quan 		    !amdgpu_sriov_vf(adev))
486c6b6a421SHawking Zhang 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
487c6b6a421SHawking Zhang 		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
488c6b6a421SHawking Zhang 			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
489f8a7976bSAlex Deucher #if defined(CONFIG_DRM_AMD_DC)
490b4f199c7SHarry Wentland 		else if (amdgpu_device_has_dc_support(adev))
491b4f199c7SHarry Wentland 			amdgpu_device_ip_block_add(adev, &dm_ip_block);
492f8a7976bSAlex Deucher #endif
493c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
494c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block);
495c6b6a421SHawking Zhang 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT &&
4969530273eSEvan Quan 		    !amdgpu_sriov_vf(adev))
497c6b6a421SHawking Zhang 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
498c6b6a421SHawking Zhang 		amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block);
4995be45a26SLeo Liu 		amdgpu_device_ip_block_add(adev, &jpeg_v2_0_ip_block);
500c6b6a421SHawking Zhang 		if (adev->enable_mes)
501c6b6a421SHawking Zhang 			amdgpu_device_ip_block_add(adev, &mes_v10_1_ip_block);
502c6b6a421SHawking Zhang 		break;
50344e9e7c9SXiaojie Yuan 	case CHIP_NAVI12:
50444e9e7c9SXiaojie Yuan 		amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
50544e9e7c9SXiaojie Yuan 		amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
50644e9e7c9SXiaojie Yuan 		amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
5076b66ae2eSXiaojie Yuan 		amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
50879bebabbSMonk Liu 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
5097f47efebSXiaojie Yuan 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
51079902029SXiaojie Yuan 		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
51179902029SXiaojie Yuan 			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
51220c14ee1SPetr Cvek #if defined(CONFIG_DRM_AMD_DC)
513078655d9SLeo Li 		else if (amdgpu_device_has_dc_support(adev))
514078655d9SLeo Li 			amdgpu_device_ip_block_add(adev, &dm_ip_block);
51520c14ee1SPetr Cvek #endif
51644e9e7c9SXiaojie Yuan 		amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
51744e9e7c9SXiaojie Yuan 		amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block);
5187f47efebSXiaojie Yuan 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT &&
5199530273eSEvan Quan 		    !amdgpu_sriov_vf(adev))
5207f47efebSXiaojie Yuan 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
5211fbed280SBoyuan Zhang 		amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block);
522fe442491SMonk Liu 		if (!amdgpu_sriov_vf(adev))
5235be45a26SLeo Liu 			amdgpu_device_ip_block_add(adev, &jpeg_v2_0_ip_block);
52444e9e7c9SXiaojie Yuan 		break;
5252e1ba10eSLikun Gao 	case CHIP_SIENNA_CICHLID:
5262e1ba10eSLikun Gao 		amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
5270b3df16bSLikun Gao 		amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
528757b3af8SLikun Gao 		amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
52956304e72SLikun Gao 		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
5305aa02350SLikun Gao 			amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
531b07e5c60SLikun Gao 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP &&
53238d5bbefSshaoyunl 		    is_support_sw_smu(adev) && !amdgpu_sriov_vf(adev))
533b07e5c60SLikun Gao 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
5349a986760SLikun Gao 		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
5359a986760SLikun Gao 			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
536464ab91aSBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC)
537464ab91aSBhawanpreet Lakha 		else if (amdgpu_device_has_dc_support(adev))
538464ab91aSBhawanpreet Lakha 			amdgpu_device_ip_block_add(adev, &dm_ip_block);
539464ab91aSBhawanpreet Lakha #endif
540933c8a93SLikun Gao 		amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
541157e72e8SLikun Gao 		amdgpu_device_ip_block_add(adev, &sdma_v5_2_ip_block);
542b8f10585SLeo Liu 		amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block);
543c45fbe1bSJack Zhang 		if (!amdgpu_sriov_vf(adev))
5444d72dd12SLeo Liu 			amdgpu_device_ip_block_add(adev, &jpeg_v3_0_ip_block);
545c45fbe1bSJack Zhang 
546a346ef86SJack Xiao 		if (adev->enable_mes)
547a346ef86SJack Xiao 			amdgpu_device_ip_block_add(adev, &mes_v10_1_ip_block);
5482e1ba10eSLikun Gao 		break;
5498515e0a4SJiansong Chen 	case CHIP_NAVY_FLOUNDER:
5508515e0a4SJiansong Chen 		amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
551fc8f07daSJiansong Chen 		amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
552026c396bSJiansong Chen 		amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
5537420eab2SJiansong Chen 		if (likely(adev->firmware.load_type == AMDGPU_FW_LOAD_PSP))
5547420eab2SJiansong Chen 			amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
5557420eab2SJiansong Chen 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP &&
5567420eab2SJiansong Chen 		    is_support_sw_smu(adev))
5577420eab2SJiansong Chen 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
5585404f073SJiansong Chen 		if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
5595404f073SJiansong Chen 			amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
560a6c5308fSBhawanpreet Lakha #if defined(CONFIG_DRM_AMD_DC)
561a6c5308fSBhawanpreet Lakha 		else if (amdgpu_device_has_dc_support(adev))
562a6c5308fSBhawanpreet Lakha 			amdgpu_device_ip_block_add(adev, &dm_ip_block);
563a6c5308fSBhawanpreet Lakha #endif
564885eb3faSJiansong Chen 		amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
565df2d15dfSJiansong Chen 		amdgpu_device_ip_block_add(adev, &sdma_v5_2_ip_block);
566290b4ad5SBoyuan Zhang 		amdgpu_device_ip_block_add(adev, &vcn_v3_0_ip_block);
567290b4ad5SBoyuan Zhang 		amdgpu_device_ip_block_add(adev, &jpeg_v3_0_ip_block);
568f4497d10SJiansong Chen 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT &&
569f4497d10SJiansong Chen 		    is_support_sw_smu(adev))
570f4497d10SJiansong Chen 			amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
5718515e0a4SJiansong Chen 		break;
572c6b6a421SHawking Zhang 	default:
573c6b6a421SHawking Zhang 		return -EINVAL;
574c6b6a421SHawking Zhang 	}
575c6b6a421SHawking Zhang 
576c6b6a421SHawking Zhang 	return 0;
577c6b6a421SHawking Zhang }
578c6b6a421SHawking Zhang 
579c6b6a421SHawking Zhang static uint32_t nv_get_rev_id(struct amdgpu_device *adev)
580c6b6a421SHawking Zhang {
581bebc0762SHawking Zhang 	return adev->nbio.funcs->get_rev_id(adev);
582c6b6a421SHawking Zhang }
583c6b6a421SHawking Zhang 
584c6b6a421SHawking Zhang static void nv_flush_hdp(struct amdgpu_device *adev, struct amdgpu_ring *ring)
585c6b6a421SHawking Zhang {
586bebc0762SHawking Zhang 	adev->nbio.funcs->hdp_flush(adev, ring);
587c6b6a421SHawking Zhang }
588c6b6a421SHawking Zhang 
589c6b6a421SHawking Zhang static void nv_invalidate_hdp(struct amdgpu_device *adev,
590c6b6a421SHawking Zhang 				struct amdgpu_ring *ring)
591c6b6a421SHawking Zhang {
592c6b6a421SHawking Zhang 	if (!ring || !ring->funcs->emit_wreg) {
59378f0aef1SStanley.Yang 		WREG32_SOC15_NO_KIQ(HDP, 0, mmHDP_READ_CACHE_INVALIDATE, 1);
594c6b6a421SHawking Zhang 	} else {
595c6b6a421SHawking Zhang 		amdgpu_ring_emit_wreg(ring, SOC15_REG_OFFSET(
596c6b6a421SHawking Zhang 					HDP, 0, mmHDP_READ_CACHE_INVALIDATE), 1);
597c6b6a421SHawking Zhang 	}
598c6b6a421SHawking Zhang }
599c6b6a421SHawking Zhang 
600c6b6a421SHawking Zhang static bool nv_need_full_reset(struct amdgpu_device *adev)
601c6b6a421SHawking Zhang {
602c6b6a421SHawking Zhang 	return true;
603c6b6a421SHawking Zhang }
604c6b6a421SHawking Zhang 
605c6b6a421SHawking Zhang static bool nv_need_reset_on_init(struct amdgpu_device *adev)
606c6b6a421SHawking Zhang {
607c6b6a421SHawking Zhang 	u32 sol_reg;
608c6b6a421SHawking Zhang 
609c6b6a421SHawking Zhang 	if (adev->flags & AMD_IS_APU)
610c6b6a421SHawking Zhang 		return false;
611c6b6a421SHawking Zhang 
612c6b6a421SHawking Zhang 	/* Check sOS sign of life register to confirm sys driver and sOS
613c6b6a421SHawking Zhang 	 * are already been loaded.
614c6b6a421SHawking Zhang 	 */
615c6b6a421SHawking Zhang 	sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
616c6b6a421SHawking Zhang 	if (sol_reg)
617c6b6a421SHawking Zhang 		return true;
6183967ae6dSAlex Deucher 
619c6b6a421SHawking Zhang 	return false;
620c6b6a421SHawking Zhang }
621c6b6a421SHawking Zhang 
6222af81531SKevin Wang static uint64_t nv_get_pcie_replay_count(struct amdgpu_device *adev)
6232af81531SKevin Wang {
6242af81531SKevin Wang 
6252af81531SKevin Wang 	/* TODO
6262af81531SKevin Wang 	 * dummy implement for pcie_replay_count sysfs interface
6272af81531SKevin Wang 	 * */
6282af81531SKevin Wang 
6292af81531SKevin Wang 	return 0;
6302af81531SKevin Wang }
6312af81531SKevin Wang 
632c6b6a421SHawking Zhang static void nv_init_doorbell_index(struct amdgpu_device *adev)
633c6b6a421SHawking Zhang {
634c6b6a421SHawking Zhang 	adev->doorbell_index.kiq = AMDGPU_NAVI10_DOORBELL_KIQ;
635c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring0 = AMDGPU_NAVI10_DOORBELL_MEC_RING0;
636c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring1 = AMDGPU_NAVI10_DOORBELL_MEC_RING1;
637c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring2 = AMDGPU_NAVI10_DOORBELL_MEC_RING2;
638c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring3 = AMDGPU_NAVI10_DOORBELL_MEC_RING3;
639c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring4 = AMDGPU_NAVI10_DOORBELL_MEC_RING4;
640c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring5 = AMDGPU_NAVI10_DOORBELL_MEC_RING5;
641c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring6 = AMDGPU_NAVI10_DOORBELL_MEC_RING6;
642c6b6a421SHawking Zhang 	adev->doorbell_index.mec_ring7 = AMDGPU_NAVI10_DOORBELL_MEC_RING7;
643c6b6a421SHawking Zhang 	adev->doorbell_index.userqueue_start = AMDGPU_NAVI10_DOORBELL_USERQUEUE_START;
644c6b6a421SHawking Zhang 	adev->doorbell_index.userqueue_end = AMDGPU_NAVI10_DOORBELL_USERQUEUE_END;
645c6b6a421SHawking Zhang 	adev->doorbell_index.gfx_ring0 = AMDGPU_NAVI10_DOORBELL_GFX_RING0;
646c6b6a421SHawking Zhang 	adev->doorbell_index.gfx_ring1 = AMDGPU_NAVI10_DOORBELL_GFX_RING1;
64720519232SJack Xiao 	adev->doorbell_index.mes_ring = AMDGPU_NAVI10_DOORBELL_MES_RING;
648c6b6a421SHawking Zhang 	adev->doorbell_index.sdma_engine[0] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE0;
649c6b6a421SHawking Zhang 	adev->doorbell_index.sdma_engine[1] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE1;
650157e72e8SLikun Gao 	adev->doorbell_index.sdma_engine[2] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE2;
651157e72e8SLikun Gao 	adev->doorbell_index.sdma_engine[3] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE3;
652c6b6a421SHawking Zhang 	adev->doorbell_index.ih = AMDGPU_NAVI10_DOORBELL_IH;
653c6b6a421SHawking Zhang 	adev->doorbell_index.vcn.vcn_ring0_1 = AMDGPU_NAVI10_DOORBELL64_VCN0_1;
654c6b6a421SHawking Zhang 	adev->doorbell_index.vcn.vcn_ring2_3 = AMDGPU_NAVI10_DOORBELL64_VCN2_3;
655c6b6a421SHawking Zhang 	adev->doorbell_index.vcn.vcn_ring4_5 = AMDGPU_NAVI10_DOORBELL64_VCN4_5;
656c6b6a421SHawking Zhang 	adev->doorbell_index.vcn.vcn_ring6_7 = AMDGPU_NAVI10_DOORBELL64_VCN6_7;
657c6b6a421SHawking Zhang 	adev->doorbell_index.first_non_cp = AMDGPU_NAVI10_DOORBELL64_FIRST_NON_CP;
658c6b6a421SHawking Zhang 	adev->doorbell_index.last_non_cp = AMDGPU_NAVI10_DOORBELL64_LAST_NON_CP;
659c6b6a421SHawking Zhang 
660c6b6a421SHawking Zhang 	adev->doorbell_index.max_assignment = AMDGPU_NAVI10_DOORBELL_MAX_ASSIGNMENT << 1;
661c6b6a421SHawking Zhang 	adev->doorbell_index.sdma_doorbell_range = 20;
662c6b6a421SHawking Zhang }
663c6b6a421SHawking Zhang 
664a7173731SAlex Deucher static void nv_pre_asic_init(struct amdgpu_device *adev)
665a7173731SAlex Deucher {
666a7173731SAlex Deucher }
667a7173731SAlex Deucher 
668c6b6a421SHawking Zhang static const struct amdgpu_asic_funcs nv_asic_funcs =
669c6b6a421SHawking Zhang {
670c6b6a421SHawking Zhang 	.read_disabled_bios = &nv_read_disabled_bios,
671c6b6a421SHawking Zhang 	.read_bios_from_rom = &nv_read_bios_from_rom,
672c6b6a421SHawking Zhang 	.read_register = &nv_read_register,
673c6b6a421SHawking Zhang 	.reset = &nv_asic_reset,
6742ddc6c3eSAlex Deucher 	.reset_method = &nv_asic_reset_method,
675c6b6a421SHawking Zhang 	.set_vga_state = &nv_vga_set_state,
676c6b6a421SHawking Zhang 	.get_xclk = &nv_get_xclk,
677c6b6a421SHawking Zhang 	.set_uvd_clocks = &nv_set_uvd_clocks,
678c6b6a421SHawking Zhang 	.set_vce_clocks = &nv_set_vce_clocks,
679c6b6a421SHawking Zhang 	.get_config_memsize = &nv_get_config_memsize,
680c6b6a421SHawking Zhang 	.flush_hdp = &nv_flush_hdp,
681c6b6a421SHawking Zhang 	.invalidate_hdp = &nv_invalidate_hdp,
682c6b6a421SHawking Zhang 	.init_doorbell_index = &nv_init_doorbell_index,
683c6b6a421SHawking Zhang 	.need_full_reset = &nv_need_full_reset,
684c6b6a421SHawking Zhang 	.need_reset_on_init = &nv_need_reset_on_init,
6852af81531SKevin Wang 	.get_pcie_replay_count = &nv_get_pcie_replay_count,
686ac742616SAlex Deucher 	.supports_baco = &nv_asic_supports_baco,
687a7173731SAlex Deucher 	.pre_asic_init = &nv_pre_asic_init,
688c6b6a421SHawking Zhang };
689c6b6a421SHawking Zhang 
690c6b6a421SHawking Zhang static int nv_common_early_init(void *handle)
691c6b6a421SHawking Zhang {
692923c087aSYong Zhao #define MMIO_REG_HOLE_OFFSET (0x80000 - PAGE_SIZE)
693c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
694c6b6a421SHawking Zhang 
695923c087aSYong Zhao 	adev->rmmio_remap.reg_offset = MMIO_REG_HOLE_OFFSET;
696923c087aSYong Zhao 	adev->rmmio_remap.bus_addr = adev->rmmio_base + MMIO_REG_HOLE_OFFSET;
697c6b6a421SHawking Zhang 	adev->smc_rreg = NULL;
698c6b6a421SHawking Zhang 	adev->smc_wreg = NULL;
699c6b6a421SHawking Zhang 	adev->pcie_rreg = &nv_pcie_rreg;
700c6b6a421SHawking Zhang 	adev->pcie_wreg = &nv_pcie_wreg;
7014922f1bcSJohn Clements 	adev->pcie_rreg64 = &nv_pcie_rreg64;
7024922f1bcSJohn Clements 	adev->pcie_wreg64 = &nv_pcie_wreg64;
703c6b6a421SHawking Zhang 
704c6b6a421SHawking Zhang 	/* TODO: will add them during VCN v2 implementation */
705c6b6a421SHawking Zhang 	adev->uvd_ctx_rreg = NULL;
706c6b6a421SHawking Zhang 	adev->uvd_ctx_wreg = NULL;
707c6b6a421SHawking Zhang 
708c6b6a421SHawking Zhang 	adev->didt_rreg = &nv_didt_rreg;
709c6b6a421SHawking Zhang 	adev->didt_wreg = &nv_didt_wreg;
710c6b6a421SHawking Zhang 
711c6b6a421SHawking Zhang 	adev->asic_funcs = &nv_asic_funcs;
712c6b6a421SHawking Zhang 
713c6b6a421SHawking Zhang 	adev->rev_id = nv_get_rev_id(adev);
714c6b6a421SHawking Zhang 	adev->external_rev_id = 0xff;
715c6b6a421SHawking Zhang 	switch (adev->asic_type) {
716c6b6a421SHawking Zhang 	case CHIP_NAVI10:
717c6b6a421SHawking Zhang 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
718c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_GFX_CGCG |
719c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_IH_CG |
720c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_HDP_MGCG |
721c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_HDP_LS |
722c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_SDMA_MGCG |
723c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_SDMA_LS |
724c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_MC_MGCG |
725c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_MC_LS |
726c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_ATHUB_MGCG |
727c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_ATHUB_LS |
728c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_VCN_MGCG |
729099d66e4SLeo Liu 			AMD_CG_SUPPORT_JPEG_MGCG |
730c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_BIF_MGCG |
731c6b6a421SHawking Zhang 			AMD_CG_SUPPORT_BIF_LS;
732157710eaSLeo Liu 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
733c12d410fSHuang Rui 			AMD_PG_SUPPORT_VCN_DPG |
734099d66e4SLeo Liu 			AMD_PG_SUPPORT_JPEG |
735a201b6acSHuang Rui 			AMD_PG_SUPPORT_ATHUB;
736c6b6a421SHawking Zhang 		adev->external_rev_id = adev->rev_id + 0x1;
737c6b6a421SHawking Zhang 		break;
7385e71e011SXiaojie Yuan 	case CHIP_NAVI14:
739d0c39f8cSXiaojie Yuan 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
740d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_GFX_CGCG |
741d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_IH_CG |
742d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_HDP_MGCG |
743d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_HDP_LS |
744d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_SDMA_MGCG |
745d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_SDMA_LS |
746d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_MC_MGCG |
747d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_MC_LS |
748d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_ATHUB_MGCG |
749d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_ATHUB_LS |
750d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_VCN_MGCG |
751099d66e4SLeo Liu 			AMD_CG_SUPPORT_JPEG_MGCG |
752d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_BIF_MGCG |
753d0c39f8cSXiaojie Yuan 			AMD_CG_SUPPORT_BIF_LS;
7540377b088SXiaojie Yuan 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
755099d66e4SLeo Liu 			AMD_PG_SUPPORT_JPEG |
7560377b088SXiaojie Yuan 			AMD_PG_SUPPORT_VCN_DPG;
75735ef88faStiancyin 		adev->external_rev_id = adev->rev_id + 20;
7585e71e011SXiaojie Yuan 		break;
75974b5e509SXiaojie Yuan 	case CHIP_NAVI12:
760dca009e7SXiaojie Yuan 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
761dca009e7SXiaojie Yuan 			AMD_CG_SUPPORT_GFX_MGLS |
762dca009e7SXiaojie Yuan 			AMD_CG_SUPPORT_GFX_CGCG |
763dca009e7SXiaojie Yuan 			AMD_CG_SUPPORT_GFX_CP_LS |
7645211c37aSXiaojie Yuan 			AMD_CG_SUPPORT_GFX_RLC_LS |
765fbe0bc57SXiaojie Yuan 			AMD_CG_SUPPORT_IH_CG |
7665211c37aSXiaojie Yuan 			AMD_CG_SUPPORT_HDP_MGCG |
767358ab97fSXiaojie Yuan 			AMD_CG_SUPPORT_HDP_LS |
768358ab97fSXiaojie Yuan 			AMD_CG_SUPPORT_SDMA_MGCG |
7698b797b3dSXiaojie Yuan 			AMD_CG_SUPPORT_SDMA_LS |
7708b797b3dSXiaojie Yuan 			AMD_CG_SUPPORT_MC_MGCG |
771ca51678dSXiaojie Yuan 			AMD_CG_SUPPORT_MC_LS |
772ca51678dSXiaojie Yuan 			AMD_CG_SUPPORT_ATHUB_MGCG |
77365872e59SXiaojie Yuan 			AMD_CG_SUPPORT_ATHUB_LS |
774099d66e4SLeo Liu 			AMD_CG_SUPPORT_VCN_MGCG |
775099d66e4SLeo Liu 			AMD_CG_SUPPORT_JPEG_MGCG;
776c1653ea0SXiaojie Yuan 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
7775ef3b8acSXiaojie Yuan 			AMD_PG_SUPPORT_VCN_DPG |
778099d66e4SLeo Liu 			AMD_PG_SUPPORT_JPEG |
7791b0443b1SLikun Gao 			AMD_PG_SUPPORT_ATHUB;
780df5e984cSTiecheng Zhou 		/* guest vm gets 0xffffffff when reading RCC_DEV0_EPF0_STRAP0,
781df5e984cSTiecheng Zhou 		 * as a consequence, the rev_id and external_rev_id are wrong.
782df5e984cSTiecheng Zhou 		 * workaround it by hardcoding rev_id to 0 (default value).
783df5e984cSTiecheng Zhou 		 */
784df5e984cSTiecheng Zhou 		if (amdgpu_sriov_vf(adev))
785df5e984cSTiecheng Zhou 			adev->rev_id = 0;
78674b5e509SXiaojie Yuan 		adev->external_rev_id = adev->rev_id + 0xa;
78774b5e509SXiaojie Yuan 		break;
788117910edSLikun Gao 	case CHIP_SIENNA_CICHLID:
78900194defSLikun Gao 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
79000194defSLikun Gao 			AMD_CG_SUPPORT_GFX_CGCG |
79100194defSLikun Gao 			AMD_CG_SUPPORT_GFX_3D_CGCG |
79298f8ea29SLikun Gao 			AMD_CG_SUPPORT_MC_MGCG |
79300194defSLikun Gao 			AMD_CG_SUPPORT_VCN_MGCG |
794ca36461fSKenneth Feng 			AMD_CG_SUPPORT_JPEG_MGCG |
795ca36461fSKenneth Feng 			AMD_CG_SUPPORT_HDP_MGCG |
7963a32c25aSKenneth Feng 			AMD_CG_SUPPORT_HDP_LS |
797bcc8367fSKenneth Feng 			AMD_CG_SUPPORT_IH_CG |
798bcc8367fSKenneth Feng 			AMD_CG_SUPPORT_MC_LS;
799b467c4f5SLeo Liu 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
800d00b0fa9SBoyuan Zhang 			AMD_PG_SUPPORT_VCN_DPG |
801b794616dSKenneth Feng 			AMD_PG_SUPPORT_JPEG |
8021b0443b1SLikun Gao 			AMD_PG_SUPPORT_ATHUB |
8031b0443b1SLikun Gao 			AMD_PG_SUPPORT_MMHUB;
804c45fbe1bSJack Zhang 		if (amdgpu_sriov_vf(adev)) {
805c45fbe1bSJack Zhang 			/* hypervisor control CG and PG enablement */
806c45fbe1bSJack Zhang 			adev->cg_flags = 0;
807c45fbe1bSJack Zhang 			adev->pg_flags = 0;
808c45fbe1bSJack Zhang 		}
809117910edSLikun Gao 		adev->external_rev_id = adev->rev_id + 0x28;
810117910edSLikun Gao 		break;
811543aa259SJiansong Chen 	case CHIP_NAVY_FLOUNDER:
81240582e67SJiansong Chen 		adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
81340582e67SJiansong Chen 			AMD_CG_SUPPORT_GFX_CGCG |
81440582e67SJiansong Chen 			AMD_CG_SUPPORT_GFX_3D_CGCG |
81540582e67SJiansong Chen 			AMD_CG_SUPPORT_VCN_MGCG |
81692c73756SJiansong Chen 			AMD_CG_SUPPORT_JPEG_MGCG |
81792c73756SJiansong Chen 			AMD_CG_SUPPORT_MC_MGCG |
8184759f887SJiansong Chen 			AMD_CG_SUPPORT_MC_LS |
8194759f887SJiansong Chen 			AMD_CG_SUPPORT_HDP_MGCG |
82085e7151bSJiansong Chen 			AMD_CG_SUPPORT_HDP_LS |
82185e7151bSJiansong Chen 			AMD_CG_SUPPORT_IH_CG;
822c6e9dd0eSBoyuan Zhang 		adev->pg_flags = AMD_PG_SUPPORT_VCN |
82300740df9SBoyuan Zhang 			AMD_PG_SUPPORT_VCN_DPG |
82447fc894aSJiansong Chen 			AMD_PG_SUPPORT_JPEG |
82547fc894aSJiansong Chen 			AMD_PG_SUPPORT_ATHUB |
82647fc894aSJiansong Chen 			AMD_PG_SUPPORT_MMHUB;
827543aa259SJiansong Chen 		adev->external_rev_id = adev->rev_id + 0x32;
828543aa259SJiansong Chen 		break;
829543aa259SJiansong Chen 
830026570e6SHuang Rui 	case CHIP_VANGOGH:
831026570e6SHuang Rui 		adev->cg_flags = 0;
832026570e6SHuang Rui 		adev->pg_flags = 0;
833026570e6SHuang Rui 		adev->external_rev_id = adev->rev_id + 0x01;
834026570e6SHuang Rui 		break;
835c6b6a421SHawking Zhang 	default:
836c6b6a421SHawking Zhang 		/* FIXME: not supported yet */
837c6b6a421SHawking Zhang 		return -EINVAL;
838c6b6a421SHawking Zhang 	}
839c6b6a421SHawking Zhang 
840b05b6903SJiange Zhao 	if (amdgpu_sriov_vf(adev)) {
841b05b6903SJiange Zhao 		amdgpu_virt_init_setting(adev);
842b05b6903SJiange Zhao 		xgpu_nv_mailbox_set_irq_funcs(adev);
843b05b6903SJiange Zhao 	}
844b05b6903SJiange Zhao 
845c6b6a421SHawking Zhang 	return 0;
846c6b6a421SHawking Zhang }
847c6b6a421SHawking Zhang 
848c6b6a421SHawking Zhang static int nv_common_late_init(void *handle)
849c6b6a421SHawking Zhang {
850b05b6903SJiange Zhao 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
851b05b6903SJiange Zhao 
852b05b6903SJiange Zhao 	if (amdgpu_sriov_vf(adev))
853b05b6903SJiange Zhao 		xgpu_nv_mailbox_get_irq(adev);
854b05b6903SJiange Zhao 
855c6b6a421SHawking Zhang 	return 0;
856c6b6a421SHawking Zhang }
857c6b6a421SHawking Zhang 
858c6b6a421SHawking Zhang static int nv_common_sw_init(void *handle)
859c6b6a421SHawking Zhang {
860b05b6903SJiange Zhao 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
861b05b6903SJiange Zhao 
862b05b6903SJiange Zhao 	if (amdgpu_sriov_vf(adev))
863b05b6903SJiange Zhao 		xgpu_nv_mailbox_add_irq_id(adev);
864b05b6903SJiange Zhao 
865c6b6a421SHawking Zhang 	return 0;
866c6b6a421SHawking Zhang }
867c6b6a421SHawking Zhang 
868c6b6a421SHawking Zhang static int nv_common_sw_fini(void *handle)
869c6b6a421SHawking Zhang {
870c6b6a421SHawking Zhang 	return 0;
871c6b6a421SHawking Zhang }
872c6b6a421SHawking Zhang 
873c6b6a421SHawking Zhang static int nv_common_hw_init(void *handle)
874c6b6a421SHawking Zhang {
875c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
876c6b6a421SHawking Zhang 
877c6b6a421SHawking Zhang 	/* enable pcie gen2/3 link */
878c6b6a421SHawking Zhang 	nv_pcie_gen3_enable(adev);
879c6b6a421SHawking Zhang 	/* enable aspm */
880c6b6a421SHawking Zhang 	nv_program_aspm(adev);
881c6b6a421SHawking Zhang 	/* setup nbio registers */
882bebc0762SHawking Zhang 	adev->nbio.funcs->init_registers(adev);
883923c087aSYong Zhao 	/* remap HDP registers to a hole in mmio space,
884923c087aSYong Zhao 	 * for the purpose of expose those registers
885923c087aSYong Zhao 	 * to process space
886923c087aSYong Zhao 	 */
887923c087aSYong Zhao 	if (adev->nbio.funcs->remap_hdp_registers)
888923c087aSYong Zhao 		adev->nbio.funcs->remap_hdp_registers(adev);
889c6b6a421SHawking Zhang 	/* enable the doorbell aperture */
890c6b6a421SHawking Zhang 	nv_enable_doorbell_aperture(adev, true);
891c6b6a421SHawking Zhang 
892c6b6a421SHawking Zhang 	return 0;
893c6b6a421SHawking Zhang }
894c6b6a421SHawking Zhang 
895c6b6a421SHawking Zhang static int nv_common_hw_fini(void *handle)
896c6b6a421SHawking Zhang {
897c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
898c6b6a421SHawking Zhang 
899c6b6a421SHawking Zhang 	/* disable the doorbell aperture */
900c6b6a421SHawking Zhang 	nv_enable_doorbell_aperture(adev, false);
901c6b6a421SHawking Zhang 
902c6b6a421SHawking Zhang 	return 0;
903c6b6a421SHawking Zhang }
904c6b6a421SHawking Zhang 
905c6b6a421SHawking Zhang static int nv_common_suspend(void *handle)
906c6b6a421SHawking Zhang {
907c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
908c6b6a421SHawking Zhang 
909c6b6a421SHawking Zhang 	return nv_common_hw_fini(adev);
910c6b6a421SHawking Zhang }
911c6b6a421SHawking Zhang 
912c6b6a421SHawking Zhang static int nv_common_resume(void *handle)
913c6b6a421SHawking Zhang {
914c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
915c6b6a421SHawking Zhang 
916c6b6a421SHawking Zhang 	return nv_common_hw_init(adev);
917c6b6a421SHawking Zhang }
918c6b6a421SHawking Zhang 
919c6b6a421SHawking Zhang static bool nv_common_is_idle(void *handle)
920c6b6a421SHawking Zhang {
921c6b6a421SHawking Zhang 	return true;
922c6b6a421SHawking Zhang }
923c6b6a421SHawking Zhang 
924c6b6a421SHawking Zhang static int nv_common_wait_for_idle(void *handle)
925c6b6a421SHawking Zhang {
926c6b6a421SHawking Zhang 	return 0;
927c6b6a421SHawking Zhang }
928c6b6a421SHawking Zhang 
929c6b6a421SHawking Zhang static int nv_common_soft_reset(void *handle)
930c6b6a421SHawking Zhang {
931c6b6a421SHawking Zhang 	return 0;
932c6b6a421SHawking Zhang }
933c6b6a421SHawking Zhang 
934c6b6a421SHawking Zhang static void nv_update_hdp_mem_power_gating(struct amdgpu_device *adev,
935c6b6a421SHawking Zhang 					   bool enable)
936c6b6a421SHawking Zhang {
937c6b6a421SHawking Zhang 	uint32_t hdp_clk_cntl, hdp_clk_cntl1;
938c6b6a421SHawking Zhang 	uint32_t hdp_mem_pwr_cntl;
939c6b6a421SHawking Zhang 
940c6b6a421SHawking Zhang 	if (!(adev->cg_flags & (AMD_CG_SUPPORT_HDP_LS |
941c6b6a421SHawking Zhang 				AMD_CG_SUPPORT_HDP_DS |
942c6b6a421SHawking Zhang 				AMD_CG_SUPPORT_HDP_SD)))
943c6b6a421SHawking Zhang 		return;
944c6b6a421SHawking Zhang 
945c6b6a421SHawking Zhang 	hdp_clk_cntl = hdp_clk_cntl1 = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
946c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL);
947c6b6a421SHawking Zhang 
948c6b6a421SHawking Zhang 	/* Before doing clock/power mode switch,
949c6b6a421SHawking Zhang 	 * forced on IPH & RC clock */
950c6b6a421SHawking Zhang 	hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
951c6b6a421SHawking Zhang 				     IPH_MEM_CLK_SOFT_OVERRIDE, 1);
952c6b6a421SHawking Zhang 	hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
953c6b6a421SHawking Zhang 				     RC_MEM_CLK_SOFT_OVERRIDE, 1);
954c6b6a421SHawking Zhang 	WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
955c6b6a421SHawking Zhang 
956c6b6a421SHawking Zhang 	/* HDP 5.0 doesn't support dynamic power mode switch,
957c6b6a421SHawking Zhang 	 * disable clock and power gating before any changing */
958c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
959c6b6a421SHawking Zhang 					 IPH_MEM_POWER_CTRL_EN, 0);
960c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
961c6b6a421SHawking Zhang 					 IPH_MEM_POWER_LS_EN, 0);
962c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
963c6b6a421SHawking Zhang 					 IPH_MEM_POWER_DS_EN, 0);
964c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
965c6b6a421SHawking Zhang 					 IPH_MEM_POWER_SD_EN, 0);
966c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
967c6b6a421SHawking Zhang 					 RC_MEM_POWER_CTRL_EN, 0);
968c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
969c6b6a421SHawking Zhang 					 RC_MEM_POWER_LS_EN, 0);
970c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
971c6b6a421SHawking Zhang 					 RC_MEM_POWER_DS_EN, 0);
972c6b6a421SHawking Zhang 	hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
973c6b6a421SHawking Zhang 					 RC_MEM_POWER_SD_EN, 0);
974c6b6a421SHawking Zhang 	WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl);
975c6b6a421SHawking Zhang 
976c6b6a421SHawking Zhang 	/* only one clock gating mode (LS/DS/SD) can be enabled */
977c6b6a421SHawking Zhang 	if (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS) {
978c6b6a421SHawking Zhang 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
979c6b6a421SHawking Zhang 						 HDP_MEM_POWER_CTRL,
980c6b6a421SHawking Zhang 						 IPH_MEM_POWER_LS_EN, enable);
981c6b6a421SHawking Zhang 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
982c6b6a421SHawking Zhang 						 HDP_MEM_POWER_CTRL,
983c6b6a421SHawking Zhang 						 RC_MEM_POWER_LS_EN, enable);
984c6b6a421SHawking Zhang 	} else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_DS) {
985c6b6a421SHawking Zhang 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
986c6b6a421SHawking Zhang 						 HDP_MEM_POWER_CTRL,
987c6b6a421SHawking Zhang 						 IPH_MEM_POWER_DS_EN, enable);
988c6b6a421SHawking Zhang 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
989c6b6a421SHawking Zhang 						 HDP_MEM_POWER_CTRL,
990c6b6a421SHawking Zhang 						 RC_MEM_POWER_DS_EN, enable);
991c6b6a421SHawking Zhang 	} else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_SD) {
992c6b6a421SHawking Zhang 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
993c6b6a421SHawking Zhang 						 HDP_MEM_POWER_CTRL,
994c6b6a421SHawking Zhang 						 IPH_MEM_POWER_SD_EN, enable);
995c6b6a421SHawking Zhang 		/* RC should not use shut down mode, fallback to ds */
996c6b6a421SHawking Zhang 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
997c6b6a421SHawking Zhang 						 HDP_MEM_POWER_CTRL,
998c6b6a421SHawking Zhang 						 RC_MEM_POWER_DS_EN, enable);
999c6b6a421SHawking Zhang 	}
1000c6b6a421SHawking Zhang 
100191c6adf8SKenneth Feng 	/* confirmed that IPH_MEM_POWER_CTRL_EN and RC_MEM_POWER_CTRL_EN have to
100291c6adf8SKenneth Feng 	 * be set for SRAM LS/DS/SD */
100391c6adf8SKenneth Feng 	if (adev->cg_flags & (AMD_CG_SUPPORT_HDP_LS | AMD_CG_SUPPORT_HDP_DS |
100491c6adf8SKenneth Feng 							AMD_CG_SUPPORT_HDP_SD)) {
100591c6adf8SKenneth Feng 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
100691c6adf8SKenneth Feng 						IPH_MEM_POWER_CTRL_EN, 1);
100791c6adf8SKenneth Feng 		hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
100891c6adf8SKenneth Feng 						RC_MEM_POWER_CTRL_EN, 1);
100991c6adf8SKenneth Feng 	}
101091c6adf8SKenneth Feng 
1011c6b6a421SHawking Zhang 	WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl);
1012c6b6a421SHawking Zhang 
1013c6b6a421SHawking Zhang 	/* restore IPH & RC clock override after clock/power mode changing */
1014c6b6a421SHawking Zhang 	WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl1);
1015c6b6a421SHawking Zhang }
1016c6b6a421SHawking Zhang 
1017c6b6a421SHawking Zhang static void nv_update_hdp_clock_gating(struct amdgpu_device *adev,
1018c6b6a421SHawking Zhang 				       bool enable)
1019c6b6a421SHawking Zhang {
1020c6b6a421SHawking Zhang 	uint32_t hdp_clk_cntl;
1021c6b6a421SHawking Zhang 
1022c6b6a421SHawking Zhang 	if (!(adev->cg_flags & AMD_CG_SUPPORT_HDP_MGCG))
1023c6b6a421SHawking Zhang 		return;
1024c6b6a421SHawking Zhang 
1025c6b6a421SHawking Zhang 	hdp_clk_cntl = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
1026c6b6a421SHawking Zhang 
1027c6b6a421SHawking Zhang 	if (enable) {
1028c6b6a421SHawking Zhang 		hdp_clk_cntl &=
1029c6b6a421SHawking Zhang 			~(uint32_t)
1030c6b6a421SHawking Zhang 			  (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
1031c6b6a421SHawking Zhang 			   HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
1032c6b6a421SHawking Zhang 			   HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
1033c6b6a421SHawking Zhang 			   HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
1034c6b6a421SHawking Zhang 			   HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
1035c6b6a421SHawking Zhang 			   HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK);
1036c6b6a421SHawking Zhang 	} else {
1037c6b6a421SHawking Zhang 		hdp_clk_cntl |= HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
1038c6b6a421SHawking Zhang 			HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
1039c6b6a421SHawking Zhang 			HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
1040c6b6a421SHawking Zhang 			HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
1041c6b6a421SHawking Zhang 			HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
1042c6b6a421SHawking Zhang 			HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK;
1043c6b6a421SHawking Zhang 	}
1044c6b6a421SHawking Zhang 
1045c6b6a421SHawking Zhang 	WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
1046c6b6a421SHawking Zhang }
1047c6b6a421SHawking Zhang 
1048c6b6a421SHawking Zhang static int nv_common_set_clockgating_state(void *handle,
1049c6b6a421SHawking Zhang 					   enum amd_clockgating_state state)
1050c6b6a421SHawking Zhang {
1051c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1052c6b6a421SHawking Zhang 
1053c6b6a421SHawking Zhang 	if (amdgpu_sriov_vf(adev))
1054c6b6a421SHawking Zhang 		return 0;
1055c6b6a421SHawking Zhang 
1056c6b6a421SHawking Zhang 	switch (adev->asic_type) {
1057c6b6a421SHawking Zhang 	case CHIP_NAVI10:
10585e71e011SXiaojie Yuan 	case CHIP_NAVI14:
10597e17e58bSXiaojie Yuan 	case CHIP_NAVI12:
1060117910edSLikun Gao 	case CHIP_SIENNA_CICHLID:
1061543aa259SJiansong Chen 	case CHIP_NAVY_FLOUNDER:
1062bebc0762SHawking Zhang 		adev->nbio.funcs->update_medium_grain_clock_gating(adev,
1063a9d4fe2fSNirmoy Das 				state == AMD_CG_STATE_GATE);
1064bebc0762SHawking Zhang 		adev->nbio.funcs->update_medium_grain_light_sleep(adev,
1065a9d4fe2fSNirmoy Das 				state == AMD_CG_STATE_GATE);
1066c6b6a421SHawking Zhang 		nv_update_hdp_mem_power_gating(adev,
1067a9d4fe2fSNirmoy Das 				   state == AMD_CG_STATE_GATE);
1068c6b6a421SHawking Zhang 		nv_update_hdp_clock_gating(adev,
1069a9d4fe2fSNirmoy Das 				state == AMD_CG_STATE_GATE);
1070c6b6a421SHawking Zhang 		break;
1071c6b6a421SHawking Zhang 	default:
1072c6b6a421SHawking Zhang 		break;
1073c6b6a421SHawking Zhang 	}
1074c6b6a421SHawking Zhang 	return 0;
1075c6b6a421SHawking Zhang }
1076c6b6a421SHawking Zhang 
1077c6b6a421SHawking Zhang static int nv_common_set_powergating_state(void *handle,
1078c6b6a421SHawking Zhang 					   enum amd_powergating_state state)
1079c6b6a421SHawking Zhang {
1080c6b6a421SHawking Zhang 	/* TODO */
1081c6b6a421SHawking Zhang 	return 0;
1082c6b6a421SHawking Zhang }
1083c6b6a421SHawking Zhang 
1084c6b6a421SHawking Zhang static void nv_common_get_clockgating_state(void *handle, u32 *flags)
1085c6b6a421SHawking Zhang {
1086c6b6a421SHawking Zhang 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1087c6b6a421SHawking Zhang 	uint32_t tmp;
1088c6b6a421SHawking Zhang 
1089c6b6a421SHawking Zhang 	if (amdgpu_sriov_vf(adev))
1090c6b6a421SHawking Zhang 		*flags = 0;
1091c6b6a421SHawking Zhang 
1092bebc0762SHawking Zhang 	adev->nbio.funcs->get_clockgating_state(adev, flags);
1093c6b6a421SHawking Zhang 
1094c6b6a421SHawking Zhang 	/* AMD_CG_SUPPORT_HDP_MGCG */
1095c6b6a421SHawking Zhang 	tmp = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
1096c6b6a421SHawking Zhang 	if (!(tmp & (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
1097c6b6a421SHawking Zhang 		     HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
1098c6b6a421SHawking Zhang 		     HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
1099c6b6a421SHawking Zhang 		     HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
1100c6b6a421SHawking Zhang 		     HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
1101c6b6a421SHawking Zhang 		     HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK)))
1102c6b6a421SHawking Zhang 		*flags |= AMD_CG_SUPPORT_HDP_MGCG;
1103c6b6a421SHawking Zhang 
1104c6b6a421SHawking Zhang 	/* AMD_CG_SUPPORT_HDP_LS/DS/SD */
1105c6b6a421SHawking Zhang 	tmp = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL);
1106c6b6a421SHawking Zhang 	if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK)
1107c6b6a421SHawking Zhang 		*flags |= AMD_CG_SUPPORT_HDP_LS;
1108c6b6a421SHawking Zhang 	else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_DS_EN_MASK)
1109c6b6a421SHawking Zhang 		*flags |= AMD_CG_SUPPORT_HDP_DS;
1110c6b6a421SHawking Zhang 	else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_SD_EN_MASK)
1111c6b6a421SHawking Zhang 		*flags |= AMD_CG_SUPPORT_HDP_SD;
1112c6b6a421SHawking Zhang 
1113c6b6a421SHawking Zhang 	return;
1114c6b6a421SHawking Zhang }
1115c6b6a421SHawking Zhang 
1116c6b6a421SHawking Zhang static const struct amd_ip_funcs nv_common_ip_funcs = {
1117c6b6a421SHawking Zhang 	.name = "nv_common",
1118c6b6a421SHawking Zhang 	.early_init = nv_common_early_init,
1119c6b6a421SHawking Zhang 	.late_init = nv_common_late_init,
1120c6b6a421SHawking Zhang 	.sw_init = nv_common_sw_init,
1121c6b6a421SHawking Zhang 	.sw_fini = nv_common_sw_fini,
1122c6b6a421SHawking Zhang 	.hw_init = nv_common_hw_init,
1123c6b6a421SHawking Zhang 	.hw_fini = nv_common_hw_fini,
1124c6b6a421SHawking Zhang 	.suspend = nv_common_suspend,
1125c6b6a421SHawking Zhang 	.resume = nv_common_resume,
1126c6b6a421SHawking Zhang 	.is_idle = nv_common_is_idle,
1127c6b6a421SHawking Zhang 	.wait_for_idle = nv_common_wait_for_idle,
1128c6b6a421SHawking Zhang 	.soft_reset = nv_common_soft_reset,
1129c6b6a421SHawking Zhang 	.set_clockgating_state = nv_common_set_clockgating_state,
1130c6b6a421SHawking Zhang 	.set_powergating_state = nv_common_set_powergating_state,
1131c6b6a421SHawking Zhang 	.get_clockgating_state = nv_common_get_clockgating_state,
1132c6b6a421SHawking Zhang };
1133