1532ed618SSoby Mathew /*
20709055eSAntonio Nino Diaz * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew */
6532ed618SSoby Mathew
7e02f469fSSathees Balya #ifndef RUNTIME_SVC_H
8e02f469fSSathees Balya #define RUNTIME_SVC_H
9532ed618SSoby Mathew
1009d40e0eSAntonio Nino Diaz #include <common/bl_common.h> /* to include exception types */
1109d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
1209d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
13085e80ecSAntonio Nino Diaz #include <smccc_helpers.h> /* to include SMCCC definitions */
14532ed618SSoby Mathew
15532ed618SSoby Mathew /*******************************************************************************
16532ed618SSoby Mathew * Structure definition, typedefs & constants for the runtime service framework
17532ed618SSoby Mathew ******************************************************************************/
18532ed618SSoby Mathew
19532ed618SSoby Mathew /*
20532ed618SSoby Mathew * Constants to allow the assembler access a runtime service
21532ed618SSoby Mathew * descriptor
22532ed618SSoby Mathew */
23402b3cf8SJulius Werner #ifdef __aarch64__
2481542c00SAntonio Nino Diaz #define RT_SVC_SIZE_LOG2 U(5)
2581542c00SAntonio Nino Diaz #define RT_SVC_DESC_INIT U(16)
2681542c00SAntonio Nino Diaz #define RT_SVC_DESC_HANDLE U(24)
27402b3cf8SJulius Werner #else
28402b3cf8SJulius Werner #define RT_SVC_SIZE_LOG2 U(4)
29402b3cf8SJulius Werner #define RT_SVC_DESC_INIT U(8)
30402b3cf8SJulius Werner #define RT_SVC_DESC_HANDLE U(12)
31402b3cf8SJulius Werner #endif /* __aarch64__ */
3281542c00SAntonio Nino Diaz #define SIZEOF_RT_SVC_DESC (U(1) << RT_SVC_SIZE_LOG2)
331ae0a49aSSoby Mathew
34532ed618SSoby Mathew
35532ed618SSoby Mathew /*
362f370465SAntonio Nino Diaz * In SMCCC 1.X, the function identifier has 6 bits for the owning entity number
372f370465SAntonio Nino Diaz * and a single bit for the type of smc call. When taken together, those values
382f370465SAntonio Nino Diaz * limit the maximum number of runtime services to 128.
39532ed618SSoby Mathew */
4081542c00SAntonio Nino Diaz #define MAX_RT_SVCS U(128)
41532ed618SSoby Mathew
42d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
43532ed618SSoby Mathew
44532ed618SSoby Mathew /* Prototype for runtime service initializing function */
45532ed618SSoby Mathew typedef int32_t (*rt_svc_init_t)(void);
46532ed618SSoby Mathew
47532ed618SSoby Mathew /*
48532ed618SSoby Mathew * Prototype for runtime service SMC handler function. x0 (SMC Function ID) to
49532ed618SSoby Mathew * x4 are as passed by the caller. Rest of the arguments to SMC and the context
50532ed618SSoby Mathew * can be accessed using the handle pointer. The cookie parameter is reserved
51532ed618SSoby Mathew * for future use
52532ed618SSoby Mathew */
53532ed618SSoby Mathew typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid,
54532ed618SSoby Mathew u_register_t x1,
55532ed618SSoby Mathew u_register_t x2,
56532ed618SSoby Mathew u_register_t x3,
57532ed618SSoby Mathew u_register_t x4,
58532ed618SSoby Mathew void *cookie,
59532ed618SSoby Mathew void *handle,
60532ed618SSoby Mathew u_register_t flags);
61532ed618SSoby Mathew typedef struct rt_svc_desc {
62532ed618SSoby Mathew uint8_t start_oen;
63532ed618SSoby Mathew uint8_t end_oen;
64532ed618SSoby Mathew uint8_t call_type;
65532ed618SSoby Mathew const char *name;
66532ed618SSoby Mathew rt_svc_init_t init;
67532ed618SSoby Mathew rt_svc_handle_t handle;
68532ed618SSoby Mathew } rt_svc_desc_t;
69532ed618SSoby Mathew
70532ed618SSoby Mathew /*
712f370465SAntonio Nino Diaz * Convenience macros to declare a service descriptor
72532ed618SSoby Mathew */
73532ed618SSoby Mathew #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) \
74532ed618SSoby Mathew static const rt_svc_desc_t __svc_desc_ ## _name \
75da04341eSChris Kay __section(".rt_svc_descs") __used = { \
76e02f469fSSathees Balya .start_oen = (_start), \
77e02f469fSSathees Balya .end_oen = (_end), \
78e02f469fSSathees Balya .call_type = (_type), \
79532ed618SSoby Mathew .name = #_name, \
80e02f469fSSathees Balya .init = (_setup), \
81e02f469fSSathees Balya .handle = (_smch) \
822f370465SAntonio Nino Diaz }
832f370465SAntonio Nino Diaz
84532ed618SSoby Mathew /*
85532ed618SSoby Mathew * Compile time assertions related to the 'rt_svc_desc' structure to:
86532ed618SSoby Mathew * 1. ensure that the assembler and the compiler view of the size
87532ed618SSoby Mathew * of the structure are the same.
88532ed618SSoby Mathew * 2. ensure that the assembler and the compiler see the initialisation
89532ed618SSoby Mathew * routine at the same offset.
90532ed618SSoby Mathew * 3. ensure that the assembler and the compiler see the handler
91532ed618SSoby Mathew * routine at the same offset.
92532ed618SSoby Mathew */
93*9a90d720SElyes Haouas CASSERT((sizeof(rt_svc_desc_t) == SIZEOF_RT_SVC_DESC),
94532ed618SSoby Mathew assert_sizeof_rt_svc_desc_mismatch);
95*9a90d720SElyes Haouas CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc_t, init),
96532ed618SSoby Mathew assert_rt_svc_desc_init_offset_mismatch);
97*9a90d720SElyes Haouas CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc_t, handle),
98532ed618SSoby Mathew assert_rt_svc_desc_handle_offset_mismatch);
99532ed618SSoby Mathew
100532ed618SSoby Mathew
101532ed618SSoby Mathew /*
10281542c00SAntonio Nino Diaz * This function combines the call type and the owning entity number
10381542c00SAntonio Nino Diaz * corresponding to a runtime service to generate a unique owning entity number.
10481542c00SAntonio Nino Diaz * This unique oen is used to access an entry in the 'rt_svc_descs_indices'
10581542c00SAntonio Nino Diaz * array. The entry contains the index of the service descriptor in the
10681542c00SAntonio Nino Diaz * 'rt_svc_descs' array.
107532ed618SSoby Mathew */
get_unique_oen(uint32_t oen,uint32_t call_type)10881542c00SAntonio Nino Diaz static inline uint32_t get_unique_oen(uint32_t oen, uint32_t call_type)
10981542c00SAntonio Nino Diaz {
11081542c00SAntonio Nino Diaz return ((call_type & FUNCID_TYPE_MASK) << FUNCID_OEN_WIDTH) |
11181542c00SAntonio Nino Diaz (oen & FUNCID_OEN_MASK);
11281542c00SAntonio Nino Diaz }
113532ed618SSoby Mathew
1141ae0a49aSSoby Mathew /*
11581542c00SAntonio Nino Diaz * This function generates the unique owning entity number from the SMC Function
116b3323cd6SAntonio Nino Diaz * ID. This unique oen is used to access an entry in the 'rt_svc_descs_indices'
117b3323cd6SAntonio Nino Diaz * array to invoke the corresponding runtime service handler during SMC
118b3323cd6SAntonio Nino Diaz * handling.
1191ae0a49aSSoby Mathew */
get_unique_oen_from_smc_fid(uint32_t fid)12081542c00SAntonio Nino Diaz static inline uint32_t get_unique_oen_from_smc_fid(uint32_t fid)
12181542c00SAntonio Nino Diaz {
12281542c00SAntonio Nino Diaz return get_unique_oen(GET_SMC_OEN(fid), GET_SMC_TYPE(fid));
12381542c00SAntonio Nino Diaz }
1242f370465SAntonio Nino Diaz
125532ed618SSoby Mathew /*******************************************************************************
126532ed618SSoby Mathew * Function & variable prototypes
127532ed618SSoby Mathew ******************************************************************************/
128532ed618SSoby Mathew void runtime_svc_init(void);
1291ae0a49aSSoby Mathew uintptr_t handle_runtime_svc(uint32_t smc_fid, void *cookie, void *handle,
1301ae0a49aSSoby Mathew unsigned int flags);
1319f85f9e3SJoel Hutton IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_START__, RT_SVC_DESCS_START);
1329f85f9e3SJoel Hutton IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_END__, RT_SVC_DESCS_END);
133532ed618SSoby Mathew void init_crash_reporting(void);
134532ed618SSoby Mathew
1357fabe1a8SRoberto Vargas extern uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
1367fabe1a8SRoberto Vargas
137d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/
138e02f469fSSathees Balya #endif /* RUNTIME_SVC_H */
139