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