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 
24 #ifndef __SOC15_COMMON_H__
25 #define __SOC15_COMMON_H__
26 
27 /* Register Access Macros */
28 #define SOC15_REG_OFFSET(ip, inst, reg)	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
29 
30 #define __WREG32_SOC15_RLC__(reg, value, flag, hwip) \
31 	((amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.rlcg_reg_access_supported) ? \
32 	 amdgpu_sriov_wreg(adev, reg, value, flag, hwip) : \
33 	 WREG32(reg, value))
34 
35 #define __RREG32_SOC15_RLC__(reg, flag, hwip) \
36 	((amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.rlcg_reg_access_supported) ? \
37 	 amdgpu_sriov_rreg(adev, reg, flag, hwip) : \
38 	 RREG32(reg))
39 
40 #define WREG32_FIELD15(ip, idx, reg, field, val)	\
41 	 __WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg,	\
42 				(__RREG32_SOC15_RLC__( \
43 					adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg, \
44 					0, ip##_HWIP) & \
45 				~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field), \
46 			      0, ip##_HWIP)
47 
48 #define RREG32_SOC15(ip, inst, reg) \
49 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
50 			 0, ip##_HWIP)
51 
52 #define RREG32_SOC15_IP(ip, reg) __RREG32_SOC15_RLC__(reg, 0, ip##_HWIP)
53 
54 #define RREG32_SOC15_IP_NO_KIQ(ip, reg) __RREG32_SOC15_RLC__(reg, AMDGPU_REGS_NO_KIQ, ip##_HWIP)
55 
56 #define RREG32_SOC15_NO_KIQ(ip, inst, reg) \
57 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
58 			 AMDGPU_REGS_NO_KIQ, ip##_HWIP)
59 
60 #define RREG32_SOC15_OFFSET(ip, inst, reg, offset) \
61 	 __RREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, 0, ip##_HWIP)
62 
63 #define WREG32_SOC15(ip, inst, reg, value) \
64 	 __WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), \
65 			  value, 0, ip##_HWIP)
66 
67 #define WREG32_SOC15_IP(ip, reg, value) \
68 	 __WREG32_SOC15_RLC__(reg, value, 0, ip##_HWIP)
69 
70 #define WREG32_SOC15_IP_NO_KIQ(ip, reg, value) \
71 	 __WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_NO_KIQ, ip##_HWIP)
72 
73 #define WREG32_SOC15_NO_KIQ(ip, inst, reg, value) \
74 	__WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
75 			     value, AMDGPU_REGS_NO_KIQ, ip##_HWIP)
76 
77 #define WREG32_SOC15_OFFSET(ip, inst, reg, offset, value) \
78 	 __WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, \
79 			  value, 0, ip##_HWIP)
80 
81 #define SOC15_WAIT_ON_RREG(ip, inst, reg, expected_value, mask) \
82 ({	int ret = 0;						\
83 	do {							\
84 		uint32_t old_ = 0;				\
85 		uint32_t tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
86 		uint32_t loop = adev->usec_timeout;		\
87 		ret = 0;					\
88 		while ((tmp_ & (mask)) != (expected_value)) {	\
89 			if (old_ != tmp_) {			\
90 				loop = adev->usec_timeout;	\
91 				old_ = tmp_;			\
92 			} else					\
93 				udelay(1);			\
94 			tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
95 			loop--;					\
96 			if (!loop) {				\
97 				DRM_WARN("Register(%d) [%s] failed to reach value 0x%08x != 0x%08x\n", \
98 					  inst, #reg, (unsigned)expected_value, (unsigned)(tmp_ & (mask))); \
99 				ret = -ETIMEDOUT;		\
100 				break;				\
101 			}					\
102 		}						\
103 	} while (0);						\
104 	ret;							\
105 })
106 
107 #define WREG32_RLC(reg, value) \
108 	__WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_RLC, GC_HWIP)
109 
110 #define WREG32_RLC_EX(prefix, reg, value) \
111 	do {							\
112 		if (amdgpu_sriov_fullaccess(adev)) {    \
113 			uint32_t i = 0;	\
114 			uint32_t retries = 50000;	\
115 			uint32_t r0 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG0_BASE_IDX] + prefix##SCRATCH_REG0;	\
116 			uint32_t r1 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG1;	\
117 			uint32_t spare_int = adev->reg_offset[GC_HWIP][0][prefix##RLC_SPARE_INT_BASE_IDX] + prefix##RLC_SPARE_INT;	\
118 			WREG32(r0, value);	\
119 			WREG32(r1, (reg | 0x80000000));	\
120 			WREG32(spare_int, 0x1);	\
121 			for (i = 0; i < retries; i++) {	\
122 				u32 tmp = RREG32(r1);	\
123 				if (!(tmp & 0x80000000))	\
124 					break;	\
125 				udelay(10);	\
126 			}	\
127 			if (i >= retries)	\
128 				pr_err("timeout: rlcg program reg:0x%05x failed !\n", reg);	\
129 		} else {	\
130 			WREG32(reg, value); \
131 		}	\
132 	} while (0)
133 
134 /* shadow the registers in the callback function */
135 #define WREG32_SOC15_RLC_SHADOW(ip, inst, reg, value) \
136 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value, AMDGPU_REGS_RLC, GC_HWIP)
137 
138 /* for GC only */
139 #define RREG32_RLC(reg) \
140 	__RREG32_SOC15_RLC__(reg, AMDGPU_REGS_RLC, GC_HWIP)
141 
142 #define WREG32_RLC_NO_KIQ(reg, value, hwip) \
143 	__WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_NO_KIQ | AMDGPU_REGS_RLC, hwip)
144 
145 #define RREG32_RLC_NO_KIQ(reg, hwip) \
146 	__RREG32_SOC15_RLC__(reg, AMDGPU_REGS_NO_KIQ | AMDGPU_REGS_RLC, hwip)
147 
148 #define WREG32_SOC15_RLC_SHADOW_EX(prefix, ip, inst, reg, value) \
149 	do {							\
150 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
151 		if (amdgpu_sriov_fullaccess(adev)) {    \
152 			uint32_t r2 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG2;	\
153 			uint32_t r3 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG3;	\
154 			uint32_t grbm_cntl = adev->reg_offset[GC_HWIP][0][prefix##GRBM_GFX_CNTL_BASE_IDX] + prefix##GRBM_GFX_CNTL;   \
155 			uint32_t grbm_idx = adev->reg_offset[GC_HWIP][0][prefix##GRBM_GFX_INDEX_BASE_IDX] + prefix##GRBM_GFX_INDEX;   \
156 			if (target_reg == grbm_cntl) \
157 				WREG32(r2, value);	\
158 			else if (target_reg == grbm_idx) \
159 				WREG32(r3, value);	\
160 			WREG32(target_reg, value);	\
161 		} else {	\
162 			WREG32(target_reg, value); \
163 		}	\
164 	} while (0)
165 
166 #define RREG32_SOC15_RLC(ip, inst, reg) \
167 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, AMDGPU_REGS_RLC, ip##_HWIP)
168 
169 #define WREG32_SOC15_RLC(ip, inst, reg, value) \
170 	do {							\
171 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][0][reg##_BASE_IDX] + reg;\
172 		__WREG32_SOC15_RLC__(target_reg, value, AMDGPU_REGS_RLC, ip##_HWIP); \
173 	} while (0)
174 
175 #define WREG32_SOC15_RLC_EX(prefix, ip, inst, reg, value) \
176 	do {							\
177 			uint32_t target_reg = adev->reg_offset[GC_HWIP][0][reg##_BASE_IDX] + reg;\
178 			WREG32_RLC_EX(prefix, target_reg, value); \
179 	} while (0)
180 
181 #define WREG32_FIELD15_RLC(ip, idx, reg, field, val)   \
182 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg), \
183 			     (__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg, \
184 						   AMDGPU_REGS_RLC, ip##_HWIP) & \
185 			      ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field), \
186 			     AMDGPU_REGS_RLC, ip##_HWIP)
187 
188 #define WREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset, value) \
189 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, value, AMDGPU_REGS_RLC, ip##_HWIP)
190 
191 #define RREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset) \
192 	__RREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, AMDGPU_REGS_RLC, ip##_HWIP)
193 
194 #endif
195