blob: 26e8d6f283bc5d23c1108714b1ed314772c9928f [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Antonio Nino Diaz0e402d32019-01-30 16:01:49 +00002 * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Sathees Balya50905c72018-10-05 13:30:59 +01007#ifndef RUNTIME_SVC_H
8#define RUNTIME_SVC_H
Achin Gupta4f6ad662013-10-25 09:08:21 +01009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <common/bl_common.h> /* to include exception types */
11#include <lib/cassert.h>
12#include <lib/utils_def.h>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000013#include <smccc_helpers.h> /* to include SMCCC definitions */
Achin Gupta4f6ad662013-10-25 09:08:21 +010014
Achin Gupta4a826dd2013-11-25 14:00:56 +000015/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000016 * Structure definition, typedefs & constants for the runtime service framework
Achin Gupta4a826dd2013-11-25 14:00:56 +000017 ******************************************************************************/
Achin Gupta4a826dd2013-11-25 14:00:56 +000018
Achin Gupta7421b462014-02-01 18:53:26 +000019/*
20 * Constants to allow the assembler access a runtime service
21 * descriptor
22 */
Julius Werner8e0ef0f2019-07-09 14:02:43 -070023#ifdef __aarch64__
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010024#define RT_SVC_SIZE_LOG2 U(5)
25#define RT_SVC_DESC_INIT U(16)
26#define RT_SVC_DESC_HANDLE U(24)
Julius Werner8e0ef0f2019-07-09 14:02:43 -070027#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__ */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010032#define SIZEOF_RT_SVC_DESC (U(1) << RT_SVC_SIZE_LOG2)
Soby Mathewa9482df2016-05-05 12:49:09 +010033
Achin Gupta7421b462014-02-01 18:53:26 +000034
35/*
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +010036 * 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.
Achin Gupta7421b462014-02-01 18:53:26 +000039 */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010040#define MAX_RT_SVCS U(128)
Achin Gupta7421b462014-02-01 18:53:26 +000041
Julius Werner53456fc2019-07-09 13:49:11 -070042#ifndef __ASSEMBLER__
Achin Gupta4f6ad662013-10-25 09:08:21 +010043
Achin Gupta7421b462014-02-01 18:53:26 +000044/* Prototype for runtime service initializing function */
Dan Handleye2712bc2014-04-10 15:37:22 +010045typedef int32_t (*rt_svc_init_t)(void);
Achin Gupta7421b462014-02-01 18:53:26 +000046
Achin Gupta7421b462014-02-01 18:53:26 +000047/*
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 */
Soby Mathewa0fedc42016-06-16 14:52:04 +010053typedef 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,
Achin Gupta7421b462014-02-01 18:53:26 +000058 void *cookie,
59 void *handle,
Soby Mathewa0fedc42016-06-16 14:52:04 +010060 u_register_t flags);
Dan Handleye2712bc2014-04-10 15:37:22 +010061typedef struct rt_svc_desc {
Achin Gupta7421b462014-02-01 18:53:26 +000062 uint8_t start_oen;
63 uint8_t end_oen;
64 uint8_t call_type;
65 const char *name;
Dan Handleye2712bc2014-04-10 15:37:22 +010066 rt_svc_init_t init;
67 rt_svc_handle_t handle;
68} rt_svc_desc_t;
Achin Gupta7421b462014-02-01 18:53:26 +000069
70/*
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +010071 * Convenience macros to declare a service descriptor
72 */
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +010073#define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) \
74 static const rt_svc_desc_t __svc_desc_ ## _name \
Chris Kay33bfc5e2023-02-14 11:30:04 +000075 __section(".rt_svc_descs") __used = { \
Sathees Balya50905c72018-10-05 13:30:59 +010076 .start_oen = (_start), \
77 .end_oen = (_end), \
78 .call_type = (_type), \
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +010079 .name = #_name, \
Sathees Balya50905c72018-10-05 13:30:59 +010080 .init = (_setup), \
81 .handle = (_smch) \
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +010082 }
83
Achin Gupta7421b462014-02-01 18:53:26 +000084/*
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.
Achin Gupta07f4e072014-02-02 12:02:23 +000090 * 3. ensure that the assembler and the compiler see the handler
Achin Gupta7421b462014-02-01 18:53:26 +000091 * routine at the same offset.
92 */
Elyes Haouas183638f2023-02-13 10:05:41 +010093CASSERT((sizeof(rt_svc_desc_t) == SIZEOF_RT_SVC_DESC),
Achin Gupta7421b462014-02-01 18:53:26 +000094 assert_sizeof_rt_svc_desc_mismatch);
Elyes Haouas183638f2023-02-13 10:05:41 +010095CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc_t, init),
Achin Gupta7421b462014-02-01 18:53:26 +000096 assert_rt_svc_desc_init_offset_mismatch);
Elyes Haouas183638f2023-02-13 10:05:41 +010097CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc_t, handle),
Achin Gupta7421b462014-02-01 18:53:26 +000098 assert_rt_svc_desc_handle_offset_mismatch);
99
100
101/*
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100102 * 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.
Achin Gupta7421b462014-02-01 18:53:26 +0000107 */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100108static 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}
Achin Gupta4f6ad662013-10-25 09:08:21 +0100113
Soby Mathewa9482df2016-05-05 12:49:09 +0100114/*
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100115 * This function generates the unique owning entity number from the SMC Function
Antonio Nino Diaze8811472018-04-17 15:10:18 +0100116 * 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.
Soby Mathewa9482df2016-05-05 12:49:09 +0100119 */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100120static 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}
Antonio Nino Diaz35c8cfc2018-04-23 15:43:29 +0100124
Achin Gupta4f6ad662013-10-25 09:08:21 +0100125/*******************************************************************************
126 * Function & variable prototypes
127 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100128void runtime_svc_init(void);
Soby Mathewa9482df2016-05-05 12:49:09 +0100129uintptr_t handle_runtime_svc(uint32_t smc_fid, void *cookie, void *handle,
130 unsigned int flags);
Joel Hutton5cc3bc82018-03-21 11:40:57 +0000131IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_START__, RT_SVC_DESCS_START);
132IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_END__, RT_SVC_DESCS_END);
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100133void init_crash_reporting(void);
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100134
Roberto Vargas05712702018-02-12 12:36:17 +0000135extern uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
136
Julius Werner53456fc2019-07-09 13:49:11 -0700137#endif /*__ASSEMBLER__*/
Sathees Balya50905c72018-10-05 13:30:59 +0100138#endif /* RUNTIME_SVC_H */