xref: /linux/drivers/gpu/drm/amd/amdgpu/soc15_common.h (revision 81283fee15ea6afb1e32defb369c3b96d2d6765c)
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)
29*81283feeSJames Zhu #define SOC15_REG_OFFSET1(ip, inst, reg, offset) \
30*81283feeSJames Zhu 	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg)+(offset))
318e3153baSKen Wang 
32a5504e9aSPeng Ju Zhou #define __WREG32_SOC15_RLC__(reg, value, flag, hwip) \
331b2dc99eSHawking Zhang 	((amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.rlcg_reg_access_supported) ? \
341b2dc99eSHawking Zhang 	 amdgpu_sriov_wreg(adev, reg, value, flag, hwip) : \
35a5504e9aSPeng Ju Zhou 	 WREG32(reg, value))
36a5504e9aSPeng Ju Zhou 
37a5504e9aSPeng Ju Zhou #define __RREG32_SOC15_RLC__(reg, flag, hwip) \
381b2dc99eSHawking Zhang 	((amdgpu_sriov_vf(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.rlcg_reg_access_supported) ? \
391b2dc99eSHawking Zhang 	 amdgpu_sriov_rreg(adev, reg, flag, hwip) : \
40a5504e9aSPeng Ju Zhou 	 RREG32(reg))
41a5504e9aSPeng Ju Zhou 
42b1bb8c01STom St Denis #define WREG32_FIELD15(ip, idx, reg, field, val)	\
43a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg,	\
44a5504e9aSPeng Ju Zhou 				(__RREG32_SOC15_RLC__( \
45a5504e9aSPeng Ju Zhou 					adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg, \
46a5504e9aSPeng Ju Zhou 					0, ip##_HWIP) & \
47a5504e9aSPeng Ju Zhou 				~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field), \
48a5504e9aSPeng Ju Zhou 			      0, ip##_HWIP)
49b1bb8c01STom St Denis 
50ba9e7a4aSStanley.Yang #define WREG32_FIELD15_PREREG(ip, idx, reg_name, field, val)        \
51ba9e7a4aSStanley.Yang 	__WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][reg##reg_name##_BASE_IDX] + reg##reg_name,   \
52ba9e7a4aSStanley.Yang 			(__RREG32_SOC15_RLC__( \
53ba9e7a4aSStanley.Yang 					adev->reg_offset[ip##_HWIP][idx][reg##reg_name##_BASE_IDX] + reg##reg_name, \
54ba9e7a4aSStanley.Yang 					0, ip##_HWIP) & \
55ba9e7a4aSStanley.Yang 					~REG_FIELD_MASK(reg_name, field)) | (val) << REG_FIELD_SHIFT(reg_name, field), \
56ba9e7a4aSStanley.Yang 			0, ip##_HWIP)
57ba9e7a4aSStanley.Yang 
58b1bb8c01STom St Denis #define RREG32_SOC15(ip, inst, reg) \
59a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
60a5504e9aSPeng Ju Zhou 			 0, ip##_HWIP)
61a5504e9aSPeng Ju Zhou 
62a5504e9aSPeng Ju Zhou #define RREG32_SOC15_IP(ip, reg) __RREG32_SOC15_RLC__(reg, 0, ip##_HWIP)
63b1bb8c01STom St Denis 
640da6f6e5SVictor Skvortsov #define RREG32_SOC15_IP_NO_KIQ(ip, reg) __RREG32_SOC15_RLC__(reg, AMDGPU_REGS_NO_KIQ, ip##_HWIP)
650da6f6e5SVictor Skvortsov 
66c2ce6aebSMonk Liu #define RREG32_SOC15_NO_KIQ(ip, inst, reg) \
67a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
68a5504e9aSPeng Ju Zhou 			 AMDGPU_REGS_NO_KIQ, ip##_HWIP)
69c2ce6aebSMonk Liu 
70496828e7STom St Denis #define RREG32_SOC15_OFFSET(ip, inst, reg, offset) \
71a5504e9aSPeng Ju Zhou 	 __RREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, 0, ip##_HWIP)
72496828e7STom St Denis 
73b1bb8c01STom St Denis #define WREG32_SOC15(ip, inst, reg, value) \
74a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), \
75a5504e9aSPeng Ju Zhou 			  value, 0, ip##_HWIP)
76a5504e9aSPeng Ju Zhou 
77a5504e9aSPeng Ju Zhou #define WREG32_SOC15_IP(ip, reg, value) \
78a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__(reg, value, 0, ip##_HWIP)
79b1bb8c01STom St Denis 
800da6f6e5SVictor Skvortsov #define WREG32_SOC15_IP_NO_KIQ(ip, reg, value) \
810da6f6e5SVictor Skvortsov 	 __WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_NO_KIQ, ip##_HWIP)
820da6f6e5SVictor Skvortsov 
83c708535eSShaoyun Liu #define WREG32_SOC15_NO_KIQ(ip, inst, reg, value) \
84a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, \
85a5504e9aSPeng Ju Zhou 			     value, AMDGPU_REGS_NO_KIQ, ip##_HWIP)
86c708535eSShaoyun Liu 
87496828e7STom St Denis #define WREG32_SOC15_OFFSET(ip, inst, reg, offset, value) \
88a5504e9aSPeng Ju Zhou 	 __WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, \
89a5504e9aSPeng Ju Zhou 			  value, 0, ip##_HWIP)
90496828e7STom St Denis 
91450da2efSJames Zhu #define SOC15_WAIT_ON_RREG(ip, inst, reg, expected_value, mask)      \
92*81283feeSJames Zhu 	amdgpu_device_wait_on_rreg(adev, inst,                       \
93*81283feeSJames Zhu 	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg)), \
94*81283feeSJames Zhu 	#reg, expected_value, mask)
95*81283feeSJames Zhu 
96*81283feeSJames Zhu #define SOC15_WAIT_ON_RREG_OFFSET(ip, inst, reg, offset, expected_value, mask)  \
97*81283feeSJames Zhu 	amdgpu_device_wait_on_rreg(adev, inst,                                  \
98*81283feeSJames Zhu 	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + (reg) + (offset)), \
99*81283feeSJames Zhu 	#reg, expected_value, mask)
100ac06b4cfSRex Zhu 
1016b1ff3ddSTrigger Huang #define WREG32_RLC(reg, value) \
102a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_RLC, GC_HWIP)
1036b1ff3ddSTrigger Huang 
10488f8575bSDennis Li #define WREG32_RLC_EX(prefix, reg, value) \
10588f8575bSDennis Li 	do {							\
10688f8575bSDennis Li 		if (amdgpu_sriov_fullaccess(adev)) {    \
10788f8575bSDennis Li 			uint32_t i = 0;	\
10888f8575bSDennis Li 			uint32_t retries = 50000;	\
10988f8575bSDennis Li 			uint32_t r0 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG0_BASE_IDX] + prefix##SCRATCH_REG0;	\
11088f8575bSDennis Li 			uint32_t r1 = adev->reg_offset[GC_HWIP][0][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG1;	\
11188f8575bSDennis Li 			uint32_t spare_int = adev->reg_offset[GC_HWIP][0][prefix##RLC_SPARE_INT_BASE_IDX] + prefix##RLC_SPARE_INT;	\
11288f8575bSDennis Li 			WREG32(r0, value);	\
11388f8575bSDennis Li 			WREG32(r1, (reg | 0x80000000));	\
11488f8575bSDennis Li 			WREG32(spare_int, 0x1);	\
11588f8575bSDennis Li 			for (i = 0; i < retries; i++) {	\
11688f8575bSDennis Li 				u32 tmp = RREG32(r1);	\
11788f8575bSDennis Li 				if (!(tmp & 0x80000000))	\
11888f8575bSDennis Li 					break;	\
11988f8575bSDennis Li 				udelay(10);	\
12088f8575bSDennis Li 			}	\
12188f8575bSDennis Li 			if (i >= retries)	\
12288f8575bSDennis Li 				pr_err("timeout: rlcg program reg:0x%05x failed !\n", reg);	\
12388f8575bSDennis Li 		} else {	\
12488f8575bSDennis Li 			WREG32(reg, value); \
12588f8575bSDennis Li 		}	\
12688f8575bSDennis Li 	} while (0)
12788f8575bSDennis Li 
128a5504e9aSPeng Ju Zhou /* shadow the registers in the callback function */
1296b1ff3ddSTrigger Huang #define WREG32_SOC15_RLC_SHADOW(ip, inst, reg, value) \
130a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value, AMDGPU_REGS_RLC, GC_HWIP)
1315e025531SPeng Ju Zhou 
132a5504e9aSPeng Ju Zhou /* for GC only */
1335e025531SPeng Ju Zhou #define RREG32_RLC(reg) \
134a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(reg, AMDGPU_REGS_RLC, GC_HWIP)
1355e025531SPeng Ju Zhou 
136a5504e9aSPeng Ju Zhou #define WREG32_RLC_NO_KIQ(reg, value, hwip) \
137a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__(reg, value, AMDGPU_REGS_NO_KIQ | AMDGPU_REGS_RLC, hwip)
1386b1ff3ddSTrigger Huang 
139a5504e9aSPeng Ju Zhou #define RREG32_RLC_NO_KIQ(reg, hwip) \
140a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(reg, AMDGPU_REGS_NO_KIQ | AMDGPU_REGS_RLC, hwip)
1415e025531SPeng Ju Zhou 
14222616eb5SDennis Li #define WREG32_SOC15_RLC_SHADOW_EX(prefix, ip, inst, reg, value) \
14322616eb5SDennis Li 	do {							\
14422616eb5SDennis Li 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
14522616eb5SDennis Li 		if (amdgpu_sriov_fullaccess(adev)) {    \
14699951878SShiwu Zhang 			uint32_t r2 = adev->reg_offset[GC_HWIP][inst][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG2;	\
14799951878SShiwu Zhang 			uint32_t r3 = adev->reg_offset[GC_HWIP][inst][prefix##SCRATCH_REG1_BASE_IDX] + prefix##SCRATCH_REG3;	\
14899951878SShiwu Zhang 			uint32_t grbm_cntl = adev->reg_offset[GC_HWIP][inst][prefix##GRBM_GFX_CNTL_BASE_IDX] + prefix##GRBM_GFX_CNTL;   \
14999951878SShiwu Zhang 			uint32_t grbm_idx = adev->reg_offset[GC_HWIP][inst][prefix##GRBM_GFX_INDEX_BASE_IDX] + prefix##GRBM_GFX_INDEX;   \
15022616eb5SDennis Li 			if (target_reg == grbm_cntl) \
15122616eb5SDennis Li 				WREG32(r2, value);	\
15222616eb5SDennis Li 			else if (target_reg == grbm_idx) \
15322616eb5SDennis Li 				WREG32(r3, value);	\
15422616eb5SDennis Li 			WREG32(target_reg, value);	\
15522616eb5SDennis Li 		} else {	\
15622616eb5SDennis Li 			WREG32(target_reg, value); \
15722616eb5SDennis Li 		}	\
15822616eb5SDennis Li 	} while (0)
15922616eb5SDennis Li 
1605e025531SPeng Ju Zhou #define RREG32_SOC15_RLC(ip, inst, reg) \
161a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg, AMDGPU_REGS_RLC, ip##_HWIP)
1625e025531SPeng Ju Zhou 
1636b1ff3ddSTrigger Huang #define WREG32_SOC15_RLC(ip, inst, reg, value) \
1646b1ff3ddSTrigger Huang 	do {							\
16599951878SShiwu Zhang 		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
166a5504e9aSPeng Ju Zhou 		__WREG32_SOC15_RLC__(target_reg, value, AMDGPU_REGS_RLC, ip##_HWIP); \
1676b1ff3ddSTrigger Huang 	} while (0)
1686b1ff3ddSTrigger Huang 
16988f8575bSDennis Li #define WREG32_SOC15_RLC_EX(prefix, ip, inst, reg, value) \
17088f8575bSDennis Li 	do {							\
17199951878SShiwu Zhang 			uint32_t target_reg = adev->reg_offset[GC_HWIP][inst][reg##_BASE_IDX] + reg;\
17288f8575bSDennis Li 			WREG32_RLC_EX(prefix, target_reg, value); \
17388f8575bSDennis Li 	} while (0)
17488f8575bSDennis Li 
1756b1ff3ddSTrigger Huang #define WREG32_FIELD15_RLC(ip, idx, reg, field, val)   \
176a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg), \
177a5504e9aSPeng Ju Zhou 			     (__RREG32_SOC15_RLC__(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg, \
178a5504e9aSPeng Ju Zhou 						   AMDGPU_REGS_RLC, ip##_HWIP) & \
179a5504e9aSPeng Ju Zhou 			      ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field), \
180a5504e9aSPeng Ju Zhou 			     AMDGPU_REGS_RLC, ip##_HWIP)
1816b1ff3ddSTrigger Huang 
1826b1ff3ddSTrigger Huang #define WREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset, value) \
183a5504e9aSPeng Ju Zhou 	__WREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, value, AMDGPU_REGS_RLC, ip##_HWIP)
1846b1ff3ddSTrigger Huang 
1855e025531SPeng Ju Zhou #define RREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset) \
186a5504e9aSPeng Ju Zhou 	__RREG32_SOC15_RLC__((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, AMDGPU_REGS_RLC, ip##_HWIP)
1875e025531SPeng Ju Zhou 
1888e3153baSKen Wang #endif
189