18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2016 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#ifndef __SOC15_COMMON_H__
258c2ecf20Sopenharmony_ci#define __SOC15_COMMON_H__
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Register Access Macros */
288c2ecf20Sopenharmony_ci#define SOC15_REG_OFFSET(ip, inst, reg)	(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define WREG32_FIELD15(ip, idx, reg, field, val)	\
318c2ecf20Sopenharmony_ci	WREG32(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg,	\
328c2ecf20Sopenharmony_ci	(RREG32(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg)	\
338c2ecf20Sopenharmony_ci	& ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define RREG32_SOC15(ip, inst, reg) \
368c2ecf20Sopenharmony_ci	RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define RREG32_SOC15_NO_KIQ(ip, inst, reg) \
398c2ecf20Sopenharmony_ci	RREG32_NO_KIQ(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define RREG32_SOC15_OFFSET(ip, inst, reg, offset) \
428c2ecf20Sopenharmony_ci	RREG32((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define WREG32_SOC15(ip, inst, reg, value) \
458c2ecf20Sopenharmony_ci	WREG32((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define WREG32_SOC15_NO_KIQ(ip, inst, reg, value) \
488c2ecf20Sopenharmony_ci	WREG32_NO_KIQ((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg), value)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define WREG32_SOC15_OFFSET(ip, inst, reg, offset, value) \
518c2ecf20Sopenharmony_ci	WREG32((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset, value)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define SOC15_WAIT_ON_RREG(ip, inst, reg, expected_value, mask) \
548c2ecf20Sopenharmony_ci({	int ret = 0;						\
558c2ecf20Sopenharmony_ci	do {							\
568c2ecf20Sopenharmony_ci		uint32_t old_ = 0;				\
578c2ecf20Sopenharmony_ci		uint32_t tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
588c2ecf20Sopenharmony_ci		uint32_t loop = adev->usec_timeout;		\
598c2ecf20Sopenharmony_ci		ret = 0;					\
608c2ecf20Sopenharmony_ci		while ((tmp_ & (mask)) != (expected_value)) {	\
618c2ecf20Sopenharmony_ci			if (old_ != tmp_) {			\
628c2ecf20Sopenharmony_ci				loop = adev->usec_timeout;	\
638c2ecf20Sopenharmony_ci				old_ = tmp_;			\
648c2ecf20Sopenharmony_ci			} else					\
658c2ecf20Sopenharmony_ci				udelay(1);			\
668c2ecf20Sopenharmony_ci			tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
678c2ecf20Sopenharmony_ci			loop--;					\
688c2ecf20Sopenharmony_ci			if (!loop) {				\
698c2ecf20Sopenharmony_ci				DRM_WARN("Register(%d) [%s] failed to reach value 0x%08x != 0x%08x\n", \
708c2ecf20Sopenharmony_ci					  inst, #reg, (unsigned)expected_value, (unsigned)(tmp_ & (mask))); \
718c2ecf20Sopenharmony_ci				ret = -ETIMEDOUT;		\
728c2ecf20Sopenharmony_ci				break;				\
738c2ecf20Sopenharmony_ci			}					\
748c2ecf20Sopenharmony_ci		}						\
758c2ecf20Sopenharmony_ci	} while (0);						\
768c2ecf20Sopenharmony_ci	ret;							\
778c2ecf20Sopenharmony_ci})
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define WREG32_RLC(reg, value) \
808c2ecf20Sopenharmony_ci	do {							\
818c2ecf20Sopenharmony_ci		if (amdgpu_sriov_fullaccess(adev)) {    \
828c2ecf20Sopenharmony_ci			uint32_t i = 0;	\
838c2ecf20Sopenharmony_ci			uint32_t retries = 50000;	\
848c2ecf20Sopenharmony_ci			uint32_t r0 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG0_BASE_IDX] + mmSCRATCH_REG0;	\
858c2ecf20Sopenharmony_ci			uint32_t r1 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG1_BASE_IDX] + mmSCRATCH_REG1;	\
868c2ecf20Sopenharmony_ci			uint32_t spare_int = adev->reg_offset[GC_HWIP][0][mmRLC_SPARE_INT_BASE_IDX] + mmRLC_SPARE_INT;	\
878c2ecf20Sopenharmony_ci			WREG32(r0, value);	\
888c2ecf20Sopenharmony_ci			WREG32(r1, (reg | 0x80000000));	\
898c2ecf20Sopenharmony_ci			WREG32(spare_int, 0x1);	\
908c2ecf20Sopenharmony_ci			for (i = 0; i < retries; i++) {	\
918c2ecf20Sopenharmony_ci				u32 tmp = RREG32(r1);	\
928c2ecf20Sopenharmony_ci				if (!(tmp & 0x80000000))	\
938c2ecf20Sopenharmony_ci					break;	\
948c2ecf20Sopenharmony_ci				udelay(10);	\
958c2ecf20Sopenharmony_ci			}	\
968c2ecf20Sopenharmony_ci			if (i >= retries)	\
978c2ecf20Sopenharmony_ci				pr_err("timeout: rlcg program reg:0x%05x failed !\n", reg);	\
988c2ecf20Sopenharmony_ci		} else {	\
998c2ecf20Sopenharmony_ci			WREG32(reg, value); \
1008c2ecf20Sopenharmony_ci		}	\
1018c2ecf20Sopenharmony_ci	} while (0)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define WREG32_SOC15_RLC_SHADOW(ip, inst, reg, value) \
1048c2ecf20Sopenharmony_ci	do {							\
1058c2ecf20Sopenharmony_ci		uint32_t target_reg = adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg;\
1068c2ecf20Sopenharmony_ci		if (amdgpu_sriov_fullaccess(adev)) {    \
1078c2ecf20Sopenharmony_ci			uint32_t r2 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG1_BASE_IDX] + mmSCRATCH_REG2;	\
1088c2ecf20Sopenharmony_ci			uint32_t r3 = adev->reg_offset[GC_HWIP][0][mmSCRATCH_REG1_BASE_IDX] + mmSCRATCH_REG3;	\
1098c2ecf20Sopenharmony_ci			uint32_t grbm_cntl = adev->reg_offset[GC_HWIP][0][mmGRBM_GFX_CNTL_BASE_IDX] + mmGRBM_GFX_CNTL;   \
1108c2ecf20Sopenharmony_ci			uint32_t grbm_idx = adev->reg_offset[GC_HWIP][0][mmGRBM_GFX_INDEX_BASE_IDX] + mmGRBM_GFX_INDEX;   \
1118c2ecf20Sopenharmony_ci			if (target_reg == grbm_cntl) \
1128c2ecf20Sopenharmony_ci				WREG32(r2, value);	\
1138c2ecf20Sopenharmony_ci			else if (target_reg == grbm_idx) \
1148c2ecf20Sopenharmony_ci				WREG32(r3, value);	\
1158c2ecf20Sopenharmony_ci			WREG32(target_reg, value);	\
1168c2ecf20Sopenharmony_ci		} else {	\
1178c2ecf20Sopenharmony_ci			WREG32(target_reg, value); \
1188c2ecf20Sopenharmony_ci		}	\
1198c2ecf20Sopenharmony_ci	} while (0)
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define WREG32_SOC15_RLC(ip, inst, reg, value) \
1228c2ecf20Sopenharmony_ci	do {							\
1238c2ecf20Sopenharmony_ci			uint32_t target_reg = adev->reg_offset[GC_HWIP][0][reg##_BASE_IDX] + reg;\
1248c2ecf20Sopenharmony_ci			WREG32_RLC(target_reg, value); \
1258c2ecf20Sopenharmony_ci	} while (0)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define WREG32_FIELD15_RLC(ip, idx, reg, field, val)   \
1288c2ecf20Sopenharmony_ci    WREG32_RLC((adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg), \
1298c2ecf20Sopenharmony_ci    (RREG32(adev->reg_offset[ip##_HWIP][idx][mm##reg##_BASE_IDX] + mm##reg) \
1308c2ecf20Sopenharmony_ci    & ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define WREG32_SOC15_OFFSET_RLC(ip, inst, reg, offset, value) \
1338c2ecf20Sopenharmony_ci    WREG32_RLC(((adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg) + offset), value)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#endif
136