1 /*
2  * Copyright (c) 2022-2023, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef MTK_SIP_SVC_H
8 #define MTK_SIP_SVC_H
9 
10 #include <stdint.h>
11 #include <lib/smccc.h>
12 #include <mtk_sip_def.h>
13 
14 /* SMC function IDs for SiP Service queries */
15 #define SIP_SVC_CALL_COUNT		U(0x8200ff00)
16 #define SIP_SVC_UID			U(0x8200ff01)
17 /* 0x8200ff02 is reserved */
18 #define SIP_SVC_VERSION			U(0x8200ff03)
19 
20 /* MediaTek SiP Service Calls version numbers */
21 #define MTK_SIP_SVC_VERSION_MAJOR	U(0x0)
22 #define MTK_SIP_SVC_VERSION_MINOR	U(0x1)
23 
24 /* Number of MediaTek SiP Calls implemented */
25 #define MTK_COMMON_SIP_NUM_CALLS	U(4)
26 
27 /* MediaTek SiP Service Calls function IDs */
28 #define MTK_SIP_SET_AUTHORIZED_SECURE_REG	U(0x82000001)
29 
30 #define SMC_ID_EXPAND_AS_ENUM(_smc_id, _smc_num) \
31 	_smc_id##_AARCH32 = ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT) | \
32 		 ((0) << FUNCID_CC_SHIFT) | \
33 		 (OEN_SIP_START << FUNCID_OEN_SHIFT) | \
34 		 ((_smc_num) << FUNCID_NUM_SHIFT)), \
35 	_smc_id##_AARCH64 = ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT) | \
36 		 ((1) << FUNCID_CC_SHIFT) | \
37 		 (OEN_SIP_START << FUNCID_OEN_SHIFT) | \
38 		 ((_smc_num) << FUNCID_NUM_SHIFT)),
39 
40 #define SMC_ID_EXPAND_AS_EXTERN_SMC_INDEX(_smc_id, _smc_num) \
41 	extern short _smc_id##_descriptor_index;
42 
43 /* Bind SMC handler with SMC ID */
44 #define DECLARE_SMC_HANDLER(_smc_id, _smc_handler) \
45 	const struct smc_descriptor _smc_id##_descriptor \
46 		__used \
47 		__aligned(sizeof(void *)) \
48 		__section(".mtk_smc_descriptor_pool") = { \
49 			.smc_handler = _smc_handler, \
50 			.smc_name = #_smc_id, \
51 			.smc_id_aarch32 = _smc_id##_AARCH32, \
52 			.smc_id_aarch64 = _smc_id##_AARCH64, \
53 			.smc_descriptor_index = &_smc_id##_descriptor_index \
54 		}
55 
56 MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_EXTERN_SMC_INDEX);
57 MTK_SIP_SMC_FROM_NS_EL1_TABLE(SMC_ID_EXPAND_AS_EXTERN_SMC_INDEX);
58 MTK_SIP_SMC_FROM_S_EL1_TABLE(SMC_ID_EXPAND_AS_EXTERN_SMC_INDEX);
59 
60 /* Expand SiP SMC ID table as enum */
61 enum {
62 	MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_ENUM)
63 	MTK_SIP_SMC_FROM_NS_EL1_TABLE(SMC_ID_EXPAND_AS_ENUM)
64 	MTK_SIP_SMC_FROM_S_EL1_TABLE(SMC_ID_EXPAND_AS_ENUM)
65 	MTK_SIP_SMC_MAX_NUMBER
66 };
67 
68 /* MediaTek SiP Calls error code */
69 enum {
70 	MTK_SIP_E_SUCCESS = 0,
71 	MTK_SIP_E_INVALID_PARAM = -1,
72 	MTK_SIP_E_NOT_SUPPORTED = -2,
73 	MTK_SIP_E_INVALID_RANGE = -3,
74 	MTK_SIP_E_PERMISSION_DENY = -4,
75 	MTK_SIP_E_LOCK_FAIL = -5,
76 };
77 
78 struct smccc_res {
79 	uint64_t a1;
80 	uint64_t a2;
81 	uint64_t a3;
82 };
83 
84 typedef uintptr_t (*smc_handler_t)(u_register_t,
85 				   u_register_t,
86 				   u_register_t,
87 				   u_register_t,
88 				   void *,
89 				   struct smccc_res *);
90 
91 struct smc_descriptor {
92 	smc_handler_t smc_handler;
93 	const uint32_t smc_id_aarch32;
94 	const uint32_t smc_id_aarch64;
95 	const char *smc_name;
96 	short *const smc_descriptor_index;
97 };
98 
99 /*
100  * This function should be implemented in MediaTek SOC directory. It fulfills
101  * MTK_SIP_SET_AUTHORIZED_SECURE_REG SiP call by checking the sreg with the
102  * predefined secure register list, if a match was found, set val to sreg.
103  *
104  * Return MTK_SIP_E_SUCCESS on success, and MTK_SIP_E_INVALID_PARAM on failure.
105  */
106 uint64_t mt_sip_set_authorized_sreg(uint32_t sreg, uint32_t val);
107 
108 #endif /* MTK_SIP_SVC_H */
109