1 /*
2  * Copyright (c) 2022, 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 
59 /* Expand SiP SMC ID table as enum */
60 enum {
61 	MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_ENUM)
62 	MTK_SIP_SMC_FROM_NS_EL1_TABLE(SMC_ID_EXPAND_AS_ENUM)
63 	MTK_SIP_SMC_MAX_NUMBER
64 };
65 
66 /* MediaTek SiP Calls error code */
67 enum {
68 	MTK_SIP_E_SUCCESS = 0,
69 	MTK_SIP_E_INVALID_PARAM = -1,
70 	MTK_SIP_E_NOT_SUPPORTED = -2,
71 	MTK_SIP_E_INVALID_RANGE = -3,
72 	MTK_SIP_E_PERMISSION_DENY = -4,
73 	MTK_SIP_E_LOCK_FAIL = -5,
74 };
75 
76 struct smccc_res {
77 	uint64_t a1;
78 	uint64_t a2;
79 	uint64_t a3;
80 };
81 
82 typedef uintptr_t (*smc_handler_t)(u_register_t,
83 				   u_register_t,
84 				   u_register_t,
85 				   u_register_t,
86 				   void *,
87 				   struct smccc_res *);
88 
89 struct smc_descriptor {
90 	smc_handler_t smc_handler;
91 	const uint32_t smc_id_aarch32;
92 	const uint32_t smc_id_aarch64;
93 	const char *smc_name;
94 	short *const smc_descriptor_index;
95 };
96 
97 /*
98  * This function should be implemented in MediaTek SOC directory. It fullfills
99  * MTK_SIP_SET_AUTHORIZED_SECURE_REG SiP call by checking the sreg with the
100  * predefined secure register list, if a match was found, set val to sreg.
101  *
102  * Return MTK_SIP_E_SUCCESS on success, and MTK_SIP_E_INVALID_PARAM on failure.
103  */
104 uint64_t mt_sip_set_authorized_sreg(uint32_t sreg, uint32_t val);
105 
106 #endif /* MTK_SIP_SVC_H */
107