xref: /linux/drivers/gpu/drm/amd/amdgpu/soc15_common.h (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
18e3153baSKen Wang /*
28e3153baSKen Wang  * Copyright 2016 Advanced Micro Devices, Inc.
38e3153baSKen Wang  *
48e3153baSKen Wang  * Permission is hereby granted, free of charge, to any person obtaining a
58e3153baSKen Wang  * copy of this software and associated documentation files (the "Software"),
68e3153baSKen Wang  * to deal in the Software without restriction, including without limitation
78e3153baSKen Wang  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88e3153baSKen Wang  * and/or sell copies of the Software, and to permit persons to whom the
98e3153baSKen Wang  * Software is furnished to do so, subject to the following conditions:
108e3153baSKen Wang  *
118e3153baSKen Wang  * The above copyright notice and this permission notice shall be included in
128e3153baSKen Wang  * all copies or substantial portions of the Software.
138e3153baSKen Wang  *
148e3153baSKen Wang  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158e3153baSKen Wang  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168e3153baSKen Wang  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178e3153baSKen Wang  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188e3153baSKen Wang  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198e3153baSKen Wang  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208e3153baSKen Wang  * OTHER DEALINGS IN THE SOFTWARE.
218e3153baSKen Wang  *
228e3153baSKen Wang  */
238e3153baSKen Wang 
248e3153baSKen Wang #ifndef __SOC15_COMMON_H__
258e3153baSKen Wang #define __SOC15_COMMON_H__
268e3153baSKen Wang 
27659a4ab8SLijo Lazar /* GET_INST returns the physical instance corresponding to a logical instance */
28af2ba368STao Zhou #define GET_INST(ip, inst) \
29af2ba368STao Zhou 	(adev->ip_map.logical_to_dev_inst ? \
30af2ba368STao Zhou 	adev->ip_map.logical_to_dev_inst(adev, ip##_HWIP, inst) : inst)
31af2ba368STao Zhou #define GET_MASK(ip, mask) \
32af2ba368STao Zhou 	(adev->ip_map.logical_to_dev_mask ? \
33af2ba368STao Zhou 	adev->ip_map.logical_to_dev_mask(adev, ip##_HWIP, mask) : mask)
34659a4ab8SLijo Lazar 
35b1bb8c01STom St Denis /* Register Access Macros */
36cd29253fSShaoyun Liu #define SOC15_REG_OFFSET(ip, inst, reg)	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
3781283feeSJames Zhu #define SOC15_REG_OFFSET1(ip, inst, reg, offset) \
3881283feeSJames Zhu 	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg)+(offset))
398e3153baSKen Wang 
408ed49dd1SVictor Lu #define __WREG32_SOC15_RLC__(reg, value, flag, hwip, inst) \
411b2dc99eSHawking Zhang 	((amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.rlcg_reg_access_supported) ? \
428ed49dd1SVictor Lu 	 amdgpu_sriov_wreg(adev, reg, value, flag, hwip, inst) : \
43a5504e9aSPeng Ju Zhou 	 WREG32(reg, value))
44a5504e9aSPeng Ju Zhou 
458ed49dd1SVictor Lu #define __RREG32_SOC15_RLC__(reg, flag, hwip, inst) \
461b2dc99eSHawking Zhang 	((amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.rlcg_reg_access_supported) ? \
478ed49dd1SVictor Lu 	 amdgpu_sriov_rreg(adev, reg, flag, hwip, inst) : \
48a5504e9aSPeng Ju Zhou 	 RREG32(reg))
49a5504e9aSPeng Ju Zhou 
50b1bb8c01STom St Denis #define WREG32_FIELD15(ip, idx, reg, field, val)	\
51a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg,	\
52a5504e9aSPeng Ju Zhou 				(__RREG32_SOC15_RLC__( \
53a5504e9aSPeng Ju Zhou 					adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg, \
548ed49dd1SVictor Lu 					0, ip##_HWIP, idx) & \
55a5504e9aSPeng Ju Zhou 				~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field), \
568ed49dd1SVictor Lu 			      0, ip##_HWIP, idx)
57b1bb8c01STom St Denis 
58ba9e7a4aSStanley.Yang #define WREG32_FIELD15_PREREG(ip, idx, reg_name, field, val)        \
59ba9e7a4aSStanley.Yang 	__WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][reg##reg_name##_BASE_IDX] + reg##reg_name,   \
60ba9e7a4aSStanley.Yang 			(__RREG32_SOC15_RLC__( \
61ba9e7a4aSStanley.Yang 					adev->reg_offset[ip##_HWIP][idx][reg##reg_name##_BASE_IDX] + reg##reg_name, \
628ed49dd1SVictor Lu 					0, ip##_HWIP, idx) & \
63ba9e7a4aSStanley.Yang 					~REG_FIELD_MASK(reg_name, field)) | (val) << REG_FIELD_SHIFT(reg_name, field), \
648ed49dd1SVictor Lu 			0, ip##_HWIP, idx)
65ba9e7a4aSStanley.Yang 
66b1bb8c01STom St Denis #define RREG32_SOC15(ip, inst, reg) \
67a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
688ed49dd1SVictor Lu 			 0, ip##_HWIP, inst)
69a5504e9aSPeng Ju Zhou 
708ed49dd1SVictor Lu #define RREG32_SOC15_IP(ip, reg) __RREG32_SOC15_RLC__(reg, 0, ip##_HWIP, 0)
71b1bb8c01STom St Denis 
72bc3c5660SVictor Lu #define RREG32_SOC15_IP_NO_KIQ(ip, reg, inst) __RREG32_SOC15_RLC__(reg, AMDGPU_REGS_NO_KIQ, ip##_HWIP, inst)
730da6f6e5SVictor Skvortsov 
74c2ce6aebSMonk Liu #define RREG32_SOC15_NO_KIQ(ip, inst, reg) \
75a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
768ed49dd1SVictor Lu 			 AMDGPU_REGS_NO_KIQ, ip##_HWIP, inst)
77c2ce6aebSMonk Liu 
78496828e7STom St Denis #define RREG32_SOC15_OFFSET(ip, inst, reg, offset) \
7955ff23d9SSonny Jiang 	 __RREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg)) + \
808ed49dd1SVictor Lu 			 (offset), 0, ip##_HWIP, inst)
81496828e7STom St Denis 
82b1bb8c01STom St Denis #define WREG32_SOC15(ip, inst, reg, value) \
83a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), \
848ed49dd1SVictor Lu 			  value, 0, ip##_HWIP, inst)
85a5504e9aSPeng Ju Zhou 
86a5504e9aSPeng Ju Zhou #define WREG32_SOC15_IP(ip, reg, value) \
878ed49dd1SVictor Lu 	 __WREG32_SOC15_RLC__(reg, value, 0, ip##_HWIP, 0)
88b1bb8c01STom St Denis 
89bc3c5660SVictor Lu #define WREG32_SOC15_IP_NO_KIQ(ip, reg, value, inst) \
90bc3c5660SVictor Lu 	 __WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_NO_KIQ, ip##_HWIP, inst)
910da6f6e5SVictor Skvortsov 
92c708535eSShaoyun Liu #define WREG32_SOC15_NO_KIQ(ip, inst, reg, value) \
93a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
948ed49dd1SVictor Lu 			     value, AMDGPU_REGS_NO_KIQ, ip##_HWIP, inst)
95c708535eSShaoyun Liu 
96496828e7STom St Denis #define WREG32_SOC15_OFFSET(ip, inst, reg, offset, value) \
97a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, \
988ed49dd1SVictor Lu 			  value, 0, ip##_HWIP, inst)
99496828e7STom St Denis 
100450da2efSJames Zhu #define SOC15_WAIT_ON_RREG(ip, inst, reg, expected_value, mask)      \
10181283feeSJames Zhu 	amdgpu_device_wait_on_rreg(adev, inst,                       \
10281283feeSJames Zhu 	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg)), \
10381283feeSJames Zhu 	#reg, expected_value, mask)
10481283feeSJames Zhu 
10581283feeSJames Zhu #define SOC15_WAIT_ON_RREG_OFFSET(ip, inst, reg, offset, expected_value, mask)  \
10681283feeSJames Zhu 	amdgpu_device_wait_on_rreg(adev, inst,                                  \
10781283feeSJames Zhu 	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg) + (offset)), \
10881283feeSJames Zhu 	#reg, expected_value, mask)
109ac06b4cfSRex Zhu 
1106b1ff3ddSTrigger Huang #define WREG32_RLC(reg, value) \
1118ed49dd1SVictor Lu 	__WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_RLC, GC_HWIP, 0)
1126b1ff3ddSTrigger Huang 
1138ed49dd1SVictor Lu #define WREG32_RLC_EX(prefix, reg, value, inst) \
11488f8575bSDennis Li 	do {							\
11588f8575bSDennis Li 		if (amdgpu_sriov_fullaccess(adev)) {    \
11688f8575bSDennis Li 			uint32_t i = 0;	\
11788f8575bSDennis Li 			uint32_t retries = 50000;	\
1188ed49dd1SVictor Lu 			uint32_t r0 = adev->reg_offset[GC_HWIP][inst][prefix##SCRATCH_REG0_BASE_IDX] + prefix##SCRATCH_REG0;	\
1198ed49dd1SVictor Lu 			uint32_t r1 = adev->reg_offset[GC_HWIP][inst][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG1;	\
1208ed49dd1SVictor Lu 			uint32_t spare_int = adev->reg_offset[GC_HWIP][inst][prefix##RLC_SPARE_INT_BASE_IDX] + prefix##RLC_SPARE_INT;	\
12188f8575bSDennis Li 			WREG32(r0, value);	\
12288f8575bSDennis Li 			WREG32(r1, (reg | 0x80000000));	\
12388f8575bSDennis Li 			WREG32(spare_int, 0x1);	\
12488f8575bSDennis Li 			for (i = 0; i < retries; i++) {	\
12588f8575bSDennis Li 				u32 tmp = RREG32(r1);	\
12688f8575bSDennis Li 				if (!(tmp & 0x80000000))	\
12788f8575bSDennis Li 					break;	\
12888f8575bSDennis Li 				udelay(10);	\
12988f8575bSDennis Li 			}	\
13088f8575bSDennis Li 			if (i >= retries)	\
13188f8575bSDennis Li 				pr_err("timeout: rlcg program reg:0x%05x failed !\n", reg);	\
13288f8575bSDennis Li 		} else {	\
13388f8575bSDennis Li 			WREG32(reg, value); \
13488f8575bSDennis Li 		}	\
13588f8575bSDennis Li 	} while (0)
13688f8575bSDennis Li 
137a5504e9aSPeng Ju Zhou /* shadow the registers in the callback function */
1386b1ff3ddSTrigger Huang #define WREG32_SOC15_RLC_SHADOW(ip, inst, reg, value) \
1398ed49dd1SVictor Lu 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value, AMDGPU_REGS_RLC, GC_HWIP, inst)
1405e025531SPeng Ju Zhou 
141a5504e9aSPeng Ju Zhou /* for GC only */
1425e025531SPeng Ju Zhou #define RREG32_RLC(reg) \
143*19726428SVictor Lu 	__RREG32_SOC15_RLC__(reg, AMDGPU_REGS_RLC, GC_HWIP, 0)
1445e025531SPeng Ju Zhou 
145a5504e9aSPeng Ju Zhou #define WREG32_RLC_NO_KIQ(reg, value, hwip) \
1468ed49dd1SVictor Lu 	__WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_NO_KIQ | AMDGPU_REGS_RLC, hwip, 0)
1476b1ff3ddSTrigger Huang 
148a5504e9aSPeng Ju Zhou #define RREG32_RLC_NO_KIQ(reg, hwip) \
1498ed49dd1SVictor Lu 	__RREG32_SOC15_RLC__(reg, AMDGPU_REGS_NO_KIQ | AMDGPU_REGS_RLC, hwip, 0)
1505e025531SPeng Ju Zhou 
15122616eb5SDennis Li #define WREG32_SOC15_RLC_SHADOW_EX(prefix, ip, inst, reg, value) \
15222616eb5SDennis Li 	do {							\
15322616eb5SDennis Li 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
15422616eb5SDennis Li 		if (amdgpu_sriov_fullaccess(adev)) {    \
15599951878SShiwu Zhang 			uint32_t r2 = adev->reg_offset[GC_HWIP][inst][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG2;	\
15699951878SShiwu Zhang 			uint32_t r3 = adev->reg_offset[GC_HWIP][inst][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG3;	\
15799951878SShiwu Zhang 			uint32_t grbm_cntl = adev->reg_offset[GC_HWIP][inst][prefix##GRBM_GFX_CNTL_BASE_IDX] + prefix##GRBM_GFX_CNTL;   \
15899951878SShiwu Zhang 			uint32_t grbm_idx = adev->reg_offset[GC_HWIP][inst][prefix##GRBM_GFX_INDEX_BASE_IDX] + prefix##GRBM_GFX_INDEX;   \
15922616eb5SDennis Li 			if (target_reg == grbm_cntl) \
16022616eb5SDennis Li 				WREG32(r2, value);	\
16122616eb5SDennis Li 			else if (target_reg == grbm_idx) \
16222616eb5SDennis Li 				WREG32(r3, value);	\
16322616eb5SDennis Li 			WREG32(target_reg, value);	\
16422616eb5SDennis Li 		} else {	\
16522616eb5SDennis Li 			WREG32(target_reg, value); \
16622616eb5SDennis Li 		}	\
16722616eb5SDennis Li 	} while (0)
16822616eb5SDennis Li 
1695e025531SPeng Ju Zhou #define RREG32_SOC15_RLC(ip, inst, reg) \
1708ed49dd1SVictor Lu 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, AMDGPU_REGS_RLC, ip##_HWIP, inst)
1715e025531SPeng Ju Zhou 
1726b1ff3ddSTrigger Huang #define WREG32_SOC15_RLC(ip, inst, reg, value) \
1736b1ff3ddSTrigger Huang 	do {							\
17499951878SShiwu Zhang 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
1758ed49dd1SVictor Lu 		__WREG32_SOC15_RLC__(target_reg, value, AMDGPU_REGS_RLC, ip##_HWIP, inst); \
1766b1ff3ddSTrigger Huang 	} while (0)
1776b1ff3ddSTrigger Huang 
17888f8575bSDennis Li #define WREG32_SOC15_RLC_EX(prefix, ip, inst, reg, value) \
17988f8575bSDennis Li 	do {							\
18099951878SShiwu Zhang 			uint32_t target_reg = adev->reg_offset[GC_HWIP][inst][reg##_BASE_IDX] + reg;\
1818ed49dd1SVictor Lu 			WREG32_RLC_EX(prefix, target_reg, value, inst); \
18288f8575bSDennis Li 	} while (0)
18388f8575bSDennis Li 
1846b1ff3ddSTrigger Huang #define WREG32_FIELD15_RLC(ip, idx, reg, field, val)   \
185a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg), \
186a5504e9aSPeng Ju Zhou 			     (__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg, \
1878ed49dd1SVictor Lu 						   AMDGPU_REGS_RLC, ip##_HWIP, idx) & \
188a5504e9aSPeng Ju Zhou 			      ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field), \
1898ed49dd1SVictor Lu 			     AMDGPU_REGS_RLC, ip##_HWIP, idx)
1906b1ff3ddSTrigger Huang 
1916b1ff3ddSTrigger Huang #define WREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset, value) \
1928ed49dd1SVictor Lu 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, value, AMDGPU_REGS_RLC, ip##_HWIP, inst)
1936b1ff3ddSTrigger Huang 
1945e025531SPeng Ju Zhou #define RREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset) \
1958ed49dd1SVictor Lu 	__RREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, AMDGPU_REGS_RLC, ip##_HWIP, inst)
1965e025531SPeng Ju Zhou 
1972fa480d3SLe Ma /* inst equals to ext for some IPs */
1982fa480d3SLe Ma #define RREG32_SOC15_EXT(ip, inst, reg, ext) \
1992fa480d3SLe Ma 	RREG32_PCIE_EXT((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) * 4 \
2002fa480d3SLe Ma 			+ adev->asic_funcs->encode_ext_smn_addressing(ext)) \
2012fa480d3SLe Ma 
2022fa480d3SLe Ma #define WREG32_SOC15_EXT(ip, inst, reg, ext, value) \
2032fa480d3SLe Ma 	WREG32_PCIE_EXT((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) * 4 \
2042fa480d3SLe Ma 			+ adev->asic_funcs->encode_ext_smn_addressing(ext), \
2052fa480d3SLe Ma 			value) \
2062fa480d3SLe Ma 
20727d80f7dSYang Wang #define RREG64_MCA(ext, mca_base, idx) \
20827d80f7dSYang Wang 	RREG64_PCIE_EXT(adev->asic_funcs->encode_ext_smn_addressing(ext) + mca_base + (idx * 8))
20927d80f7dSYang Wang 
21027d80f7dSYang Wang #define WREG64_MCA(ext, mca_base, idx, val) \
21127d80f7dSYang Wang 	WREG64_PCIE_EXT(adev->asic_funcs->encode_ext_smn_addressing(ext) + mca_base + (idx * 8), val)
21227d80f7dSYang Wang 
2138e3153baSKen Wang #endif
214