xref: /linux/drivers/gpu/drm/amd/amdgpu/soc15_common.h (revision 88f8575bca5fc70ba8608cfc49811f9b4d1eb6f9)
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 
27b1bb8c01STom St Denis /* Register Access Macros */
28cd29253fSShaoyun Liu #define SOC15_REG_OFFSET(ip, inst, reg)	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
298e3153baSKen Wang 
30b1bb8c01STom St Denis #define WREG32_FIELD15(ip, idx, reg, field, val)	\
31b466107eSShaoyun Liu 	WREG32(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg,	\
32b466107eSShaoyun Liu 	(RREG32(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg)	\
33b466107eSShaoyun Liu 	& ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
34b1bb8c01STom St Denis 
35b1bb8c01STom St Denis #define RREG32_SOC15(ip, inst, reg) \
36b466107eSShaoyun Liu 	RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
37b1bb8c01STom St Denis 
38c2ce6aebSMonk Liu #define RREG32_SOC15_NO_KIQ(ip, inst, reg) \
39c2ce6aebSMonk Liu 	RREG32_NO_KIQ(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
40c2ce6aebSMonk Liu 
41496828e7STom St Denis #define RREG32_SOC15_OFFSET(ip, inst, reg, offset) \
42b466107eSShaoyun Liu 	RREG32((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset)
43496828e7STom St Denis 
44b1bb8c01STom St Denis #define WREG32_SOC15(ip, inst, reg, value) \
45b466107eSShaoyun Liu 	WREG32((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value)
46b1bb8c01STom St Denis 
47c708535eSShaoyun Liu #define WREG32_SOC15_NO_KIQ(ip, inst, reg, value) \
48b466107eSShaoyun Liu 	WREG32_NO_KIQ((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value)
49c708535eSShaoyun Liu 
50496828e7STom St Denis #define WREG32_SOC15_OFFSET(ip, inst, reg, offset, value) \
51b466107eSShaoyun Liu 	WREG32((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, value)
52496828e7STom St Denis 
53450da2efSJames Zhu #define SOC15_WAIT_ON_RREG(ip, inst, reg, expected_value, mask) \
54450da2efSJames Zhu ({	int ret = 0;						\
55ac06b4cfSRex Zhu 	do {							\
567ab3f021SJames Zhu 		uint32_t old_ = 0;				\
57ac06b4cfSRex Zhu 		uint32_t tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
58ac06b4cfSRex Zhu 		uint32_t loop = adev->usec_timeout;		\
59a63141e3SNathan Chancellor 		ret = 0;					\
60ac06b4cfSRex Zhu 		while ((tmp_ & (mask)) != (expected_value)) {	\
617ab3f021SJames Zhu 			if (old_ != tmp_) {			\
627ab3f021SJames Zhu 				loop = adev->usec_timeout;	\
637ab3f021SJames Zhu 				old_ = tmp_;			\
647ab3f021SJames Zhu 			} else					\
657ab3f021SJames Zhu 				udelay(1);			\
66ac06b4cfSRex Zhu 			tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
67ac06b4cfSRex Zhu 			loop--;					\
68ac06b4cfSRex Zhu 			if (!loop) {				\
697ab3f021SJames Zhu 				DRM_WARN("Register(%d) [%s] failed to reach value 0x%08x != 0x%08x\n", \
7081bb773fSAlex Deucher 					  inst, #reg, (unsigned)expected_value, (unsigned)(tmp_ & (mask))); \
71ac06b4cfSRex Zhu 				ret = -ETIMEDOUT;		\
72ac06b4cfSRex Zhu 				break;				\
73ac06b4cfSRex Zhu 			}					\
74ac06b4cfSRex Zhu 		}						\
75450da2efSJames Zhu 	} while (0);						\
76450da2efSJames Zhu 	ret;							\
77450da2efSJames Zhu })
78ac06b4cfSRex Zhu 
796b1ff3ddSTrigger Huang #define WREG32_RLC(reg, value) \
806b1ff3ddSTrigger Huang 	do {							\
812e0cc4d4SMonk Liu 		if (amdgpu_sriov_fullaccess(adev)) {    \
826b1ff3ddSTrigger Huang 			uint32_t i = 0;	\
836b1ff3ddSTrigger Huang 			uint32_t retries = 50000;	\
846b1ff3ddSTrigger Huang 			uint32_t r0 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG0_BASE_IDX] + mmSCRATCH_REG0;	\
856b1ff3ddSTrigger Huang 			uint32_t r1 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG1_BASE_IDX] + mmSCRATCH_REG1;	\
866b1ff3ddSTrigger Huang 			uint32_t spare_int = adev->reg_offset[GC_HWIP][0][mmRLC_SPARE_INT_BASE_IDX] + mmRLC_SPARE_INT;	\
876b1ff3ddSTrigger Huang 			WREG32(r0, value);	\
886b1ff3ddSTrigger Huang 			WREG32(r1, (reg | 0x80000000));	\
896b1ff3ddSTrigger Huang 			WREG32(spare_int, 0x1);	\
906b1ff3ddSTrigger Huang 			for (i = 0; i < retries; i++) {	\
916b1ff3ddSTrigger Huang 				u32 tmp = RREG32(r1);	\
926b1ff3ddSTrigger Huang 				if (!(tmp & 0x80000000))	\
936b1ff3ddSTrigger Huang 					break;	\
946b1ff3ddSTrigger Huang 				udelay(10);	\
956b1ff3ddSTrigger Huang 			}	\
966b1ff3ddSTrigger Huang 			if (i >= retries)	\
976b1ff3ddSTrigger Huang 				pr_err("timeout: rlcg program reg:0x%05x failed !\n", reg);	\
986b1ff3ddSTrigger Huang 		} else {	\
996b1ff3ddSTrigger Huang 			WREG32(reg, value); \
1006b1ff3ddSTrigger Huang 		}	\
1016b1ff3ddSTrigger Huang 	} while (0)
1026b1ff3ddSTrigger Huang 
103*88f8575bSDennis Li #define WREG32_RLC_EX(prefix, reg, value) \
104*88f8575bSDennis Li 	do {							\
105*88f8575bSDennis Li 		if (amdgpu_sriov_fullaccess(adev)) {    \
106*88f8575bSDennis Li 			uint32_t i = 0;	\
107*88f8575bSDennis Li 			uint32_t retries = 50000;	\
108*88f8575bSDennis Li 			uint32_t r0 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG0_BASE_IDX] + prefix##SCRATCH_REG0;	\
109*88f8575bSDennis Li 			uint32_t r1 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG1;	\
110*88f8575bSDennis Li 			uint32_t spare_int = adev->reg_offset[GC_HWIP][0][prefix##RLC_SPARE_INT_BASE_IDX] + prefix##RLC_SPARE_INT;	\
111*88f8575bSDennis Li 			WREG32(r0, value);	\
112*88f8575bSDennis Li 			WREG32(r1, (reg | 0x80000000));	\
113*88f8575bSDennis Li 			WREG32(spare_int, 0x1);	\
114*88f8575bSDennis Li 			for (i = 0; i < retries; i++) {	\
115*88f8575bSDennis Li 				u32 tmp = RREG32(r1);	\
116*88f8575bSDennis Li 				if (!(tmp & 0x80000000))	\
117*88f8575bSDennis Li 					break;	\
118*88f8575bSDennis Li 				udelay(10);	\
119*88f8575bSDennis Li 			}	\
120*88f8575bSDennis Li 			if (i >= retries)	\
121*88f8575bSDennis Li 				pr_err("timeout: rlcg program reg:0x%05x failed !\n", reg);	\
122*88f8575bSDennis Li 		} else {	\
123*88f8575bSDennis Li 			WREG32(reg, value); \
124*88f8575bSDennis Li 		}	\
125*88f8575bSDennis Li 	} while (0)
126*88f8575bSDennis Li 
1276b1ff3ddSTrigger Huang #define WREG32_SOC15_RLC_SHADOW(ip, inst, reg, value) \
1286b1ff3ddSTrigger Huang 	do {							\
1296b1ff3ddSTrigger Huang 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
1302e0cc4d4SMonk Liu 		if (amdgpu_sriov_fullaccess(adev)) {    \
1316b1ff3ddSTrigger Huang 			uint32_t r2 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG1_BASE_IDX] + mmSCRATCH_REG2;	\
1326b1ff3ddSTrigger Huang 			uint32_t r3 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG1_BASE_IDX] + mmSCRATCH_REG3;	\
1336b1ff3ddSTrigger Huang 			uint32_t grbm_cntl = adev->reg_offset[GC_HWIP][0][mmGRBM_GFX_CNTL_BASE_IDX] + mmGRBM_GFX_CNTL;   \
1346b1ff3ddSTrigger Huang 			uint32_t grbm_idx = adev->reg_offset[GC_HWIP][0][mmGRBM_GFX_INDEX_BASE_IDX] + mmGRBM_GFX_INDEX;   \
1356b1ff3ddSTrigger Huang 			if (target_reg == grbm_cntl) \
1366b1ff3ddSTrigger Huang 				WREG32(r2, value);	\
1376b1ff3ddSTrigger Huang 			else if (target_reg == grbm_idx) \
1386b1ff3ddSTrigger Huang 				WREG32(r3, value);	\
1396b1ff3ddSTrigger Huang 			WREG32(target_reg, value);	\
1406b1ff3ddSTrigger Huang 		} else {	\
1416b1ff3ddSTrigger Huang 			WREG32(target_reg, value); \
1426b1ff3ddSTrigger Huang 		}	\
1436b1ff3ddSTrigger Huang 	} while (0)
1446b1ff3ddSTrigger Huang 
14522616eb5SDennis Li #define WREG32_SOC15_RLC_SHADOW_EX(prefix, ip, inst, reg, value) \
14622616eb5SDennis Li 	do {							\
14722616eb5SDennis Li 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
14822616eb5SDennis Li 		if (amdgpu_sriov_fullaccess(adev)) {    \
14922616eb5SDennis Li 			uint32_t r2 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG2;	\
15022616eb5SDennis Li 			uint32_t r3 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG3;	\
15122616eb5SDennis Li 			uint32_t grbm_cntl = adev->reg_offset[GC_HWIP][0][prefix##GRBM_GFX_CNTL_BASE_IDX] + prefix##GRBM_GFX_CNTL;   \
15222616eb5SDennis Li 			uint32_t grbm_idx = adev->reg_offset[GC_HWIP][0][prefix##GRBM_GFX_INDEX_BASE_IDX] + prefix##GRBM_GFX_INDEX;   \
15322616eb5SDennis Li 			if (target_reg == grbm_cntl) \
15422616eb5SDennis Li 				WREG32(r2, value);	\
15522616eb5SDennis Li 			else if (target_reg == grbm_idx) \
15622616eb5SDennis Li 				WREG32(r3, value);	\
15722616eb5SDennis Li 			WREG32(target_reg, value);	\
15822616eb5SDennis Li 		} else {	\
15922616eb5SDennis Li 			WREG32(target_reg, value); \
16022616eb5SDennis Li 		}	\
16122616eb5SDennis Li 	} while (0)
16222616eb5SDennis Li 
1636b1ff3ddSTrigger Huang #define WREG32_SOC15_RLC(ip, inst, reg, value) \
1646b1ff3ddSTrigger Huang 	do {							\
1656b1ff3ddSTrigger Huang 			uint32_t target_reg = adev->reg_offset[GC_HWIP][0][reg##_BASE_IDX] + reg;\
1666b1ff3ddSTrigger Huang 			WREG32_RLC(target_reg, value); \
1676b1ff3ddSTrigger Huang 	} while (0)
1686b1ff3ddSTrigger Huang 
169*88f8575bSDennis Li #define WREG32_SOC15_RLC_EX(prefix, ip, inst, reg, value) \
170*88f8575bSDennis Li 	do {							\
171*88f8575bSDennis Li 			uint32_t target_reg = adev->reg_offset[GC_HWIP][0][reg##_BASE_IDX] + reg;\
172*88f8575bSDennis Li 			WREG32_RLC_EX(prefix, target_reg, value); \
173*88f8575bSDennis Li 	} while (0)
174*88f8575bSDennis Li 
1756b1ff3ddSTrigger Huang #define WREG32_FIELD15_RLC(ip, idx, reg, field, val)   \
1766b1ff3ddSTrigger Huang     WREG32_RLC((adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg), \
1776b1ff3ddSTrigger Huang     (RREG32(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg) \
1786b1ff3ddSTrigger Huang     & ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
1796b1ff3ddSTrigger Huang 
1806b1ff3ddSTrigger Huang #define WREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset, value) \
1816b1ff3ddSTrigger Huang     WREG32_RLC(((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset), value)
1826b1ff3ddSTrigger Huang 
1838e3153baSKen Wang #endif
184