1 /*
2  * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef RUNTIME_SVC_H
8 #define RUNTIME_SVC_H
9 
10 #include <common/bl_common.h>		/* to include exception types */
11 #include <lib/cassert.h>
12 #include <lib/utils_def.h>
13 #include <smccc_helpers.h>	/* to include SMCCC definitions */
14 
15 /*******************************************************************************
16  * Structure definition, typedefs & constants for the runtime service framework
17  ******************************************************************************/
18 
19 /*
20  * Constants to allow the assembler access a runtime service
21  * descriptor
22  */
23 #ifdef __aarch64__
24 #define RT_SVC_SIZE_LOG2	U(5)
25 #define RT_SVC_DESC_INIT	U(16)
26 #define RT_SVC_DESC_HANDLE	U(24)
27 #else
28 #define RT_SVC_SIZE_LOG2	U(4)
29 #define RT_SVC_DESC_INIT	U(8)
30 #define RT_SVC_DESC_HANDLE	U(12)
31 #endif /* __aarch64__ */
32 #define SIZEOF_RT_SVC_DESC	(U(1) << RT_SVC_SIZE_LOG2)
33 
34 
35 /*
36  * In SMCCC 1.X, the function identifier has 6 bits for the owning entity number
37  * and a single bit for the type of smc call. When taken together, those values
38  * limit the maximum number of runtime services to 128.
39  */
40 #define MAX_RT_SVCS		U(128)
41 
42 #ifndef __ASSEMBLER__
43 
44 /* Prototype for runtime service initializing function */
45 typedef int32_t (*rt_svc_init_t)(void);
46 
47 /*
48  * Prototype for runtime service SMC handler function. x0 (SMC Function ID) to
49  * x4 are as passed by the caller. Rest of the arguments to SMC and the context
50  * can be accessed using the handle pointer. The cookie parameter is reserved
51  * for future use
52  */
53 typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid,
54 				  u_register_t x1,
55 				  u_register_t x2,
56 				  u_register_t x3,
57 				  u_register_t x4,
58 				  void *cookie,
59 				  void *handle,
60 				  u_register_t flags);
61 typedef struct rt_svc_desc {
62 	uint8_t start_oen;
63 	uint8_t end_oen;
64 	uint8_t call_type;
65 	const char *name;
66 	rt_svc_init_t init;
67 	rt_svc_handle_t handle;
68 } rt_svc_desc_t;
69 
70 /*
71  * Convenience macros to declare a service descriptor
72  */
73 #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch)	\
74 	static const rt_svc_desc_t __svc_desc_ ## _name			\
75 		__section("rt_svc_descs") __used = {			\
76 			.start_oen = (_start),				\
77 			.end_oen = (_end),				\
78 			.call_type = (_type),				\
79 			.name = #_name,					\
80 			.init = (_setup),				\
81 			.handle = (_smch)				\
82 		}
83 
84 /*
85  * Compile time assertions related to the 'rt_svc_desc' structure to:
86  * 1. ensure that the assembler and the compiler view of the size
87  *    of the structure are the same.
88  * 2. ensure that the assembler and the compiler see the initialisation
89  *    routine at the same offset.
90  * 3. ensure that the assembler and the compiler see the handler
91  *    routine at the same offset.
92  */
93 CASSERT((sizeof(rt_svc_desc_t) == SIZEOF_RT_SVC_DESC), \
94 	assert_sizeof_rt_svc_desc_mismatch);
95 CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc_t, init), \
96 	assert_rt_svc_desc_init_offset_mismatch);
97 CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc_t, handle), \
98 	assert_rt_svc_desc_handle_offset_mismatch);
99 
100 
101 /*
102  * This function combines the call type and the owning entity number
103  * corresponding to a runtime service to generate a unique owning entity number.
104  * This unique oen is used to access an entry in the 'rt_svc_descs_indices'
105  * array. The entry contains the index of the service descriptor in the
106  * 'rt_svc_descs' array.
107  */
get_unique_oen(uint32_t oen,uint32_t call_type)108 static inline uint32_t get_unique_oen(uint32_t oen, uint32_t call_type)
109 {
110 	return ((call_type & FUNCID_TYPE_MASK) << FUNCID_OEN_WIDTH) |
111 		(oen & FUNCID_OEN_MASK);
112 }
113 
114 /*
115  * This function generates the unique owning entity number from the SMC Function
116  * ID. This unique oen is used to access an entry in the 'rt_svc_descs_indices'
117  * array to invoke the corresponding runtime service handler during SMC
118  * handling.
119  */
get_unique_oen_from_smc_fid(uint32_t fid)120 static inline uint32_t get_unique_oen_from_smc_fid(uint32_t fid)
121 {
122 	return get_unique_oen(GET_SMC_OEN(fid), GET_SMC_TYPE(fid));
123 }
124 
125 /*******************************************************************************
126  * Function & variable prototypes
127  ******************************************************************************/
128 void runtime_svc_init(void);
129 uintptr_t handle_runtime_svc(uint32_t smc_fid, void *cookie, void *handle,
130 						unsigned int flags);
131 IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_START__,		RT_SVC_DESCS_START);
132 IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_END__,		RT_SVC_DESCS_END);
133 void init_crash_reporting(void);
134 
135 extern uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
136 
137 #endif /*__ASSEMBLER__*/
138 #endif /* RUNTIME_SVC_H */
139