Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 0e402d3 | 2019-01-30 16:01:49 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 7 | #ifndef RUNTIME_SVC_H |
| 8 | #define RUNTIME_SVC_H |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <common/bl_common.h> /* to include exception types */ |
| 11 | #include <lib/cassert.h> |
| 12 | #include <lib/utils_def.h> |
Antonio Nino Diaz | 3c817f4 | 2018-03-21 10:49:27 +0000 | [diff] [blame] | 13 | #include <smccc_helpers.h> /* to include SMCCC definitions */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 14 | |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 15 | /******************************************************************************* |
Jeenu Viswambharan | caa8493 | 2014-02-06 10:36:15 +0000 | [diff] [blame] | 16 | * Structure definition, typedefs & constants for the runtime service framework |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 17 | ******************************************************************************/ |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 18 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 19 | /* |
| 20 | * Constants to allow the assembler access a runtime service |
| 21 | * descriptor |
| 22 | */ |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 23 | #ifdef __aarch64__ |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 24 | #define RT_SVC_SIZE_LOG2 U(5) |
| 25 | #define RT_SVC_DESC_INIT U(16) |
| 26 | #define RT_SVC_DESC_HANDLE U(24) |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 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__ */ |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 32 | #define SIZEOF_RT_SVC_DESC (U(1) << RT_SVC_SIZE_LOG2) |
Soby Mathew | a9482df | 2016-05-05 12:49:09 +0100 | [diff] [blame] | 33 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 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. |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 39 | */ |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 40 | #define MAX_RT_SVCS U(128) |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 41 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 42 | #ifndef __ASSEMBLER__ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 43 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 44 | /* Prototype for runtime service initializing function */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 45 | typedef int32_t (*rt_svc_init_t)(void); |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 46 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 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 | */ |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 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, |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 58 | void *cookie, |
| 59 | void *handle, |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 60 | u_register_t flags); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 61 | typedef struct rt_svc_desc { |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 62 | uint8_t start_oen; |
| 63 | uint8_t end_oen; |
| 64 | uint8_t call_type; |
| 65 | const char *name; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 66 | rt_svc_init_t init; |
| 67 | rt_svc_handle_t handle; |
| 68 | } rt_svc_desc_t; |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 71 | * Convenience macros to declare a service descriptor |
| 72 | */ |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 73 | #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) \ |
| 74 | static const rt_svc_desc_t __svc_desc_ ## _name \ |
Chris Kay | 33bfc5e | 2023-02-14 11:30:04 +0000 | [diff] [blame] | 75 | __section(".rt_svc_descs") __used = { \ |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 76 | .start_oen = (_start), \ |
| 77 | .end_oen = (_end), \ |
| 78 | .call_type = (_type), \ |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 79 | .name = #_name, \ |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 80 | .init = (_setup), \ |
| 81 | .handle = (_smch) \ |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 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. |
Achin Gupta | 07f4e07 | 2014-02-02 12:02:23 +0000 | [diff] [blame] | 90 | * 3. ensure that the assembler and the compiler see the handler |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 91 | * routine at the same offset. |
| 92 | */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 93 | CASSERT((sizeof(rt_svc_desc_t) == SIZEOF_RT_SVC_DESC), \ |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 94 | assert_sizeof_rt_svc_desc_mismatch); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 95 | CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc_t, init), \ |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 96 | assert_rt_svc_desc_init_offset_mismatch); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 97 | CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc_t, handle), \ |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 98 | assert_rt_svc_desc_handle_offset_mismatch); |
| 99 | |
| 100 | |
| 101 | /* |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 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. |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 107 | */ |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 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 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 113 | |
Soby Mathew | a9482df | 2016-05-05 12:49:09 +0100 | [diff] [blame] | 114 | /* |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 115 | * This function generates the unique owning entity number from the SMC Function |
Antonio Nino Diaz | e881147 | 2018-04-17 15:10:18 +0100 | [diff] [blame] | 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. |
Soby Mathew | a9482df | 2016-05-05 12:49:09 +0100 | [diff] [blame] | 119 | */ |
Antonio Nino Diaz | f0b14cf | 2018-10-04 09:55:23 +0100 | [diff] [blame] | 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 | } |
Antonio Nino Diaz | 35c8cfc | 2018-04-23 15:43:29 +0100 | [diff] [blame] | 124 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 125 | /******************************************************************************* |
| 126 | * Function & variable prototypes |
| 127 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 128 | void runtime_svc_init(void); |
Soby Mathew | a9482df | 2016-05-05 12:49:09 +0100 | [diff] [blame] | 129 | uintptr_t handle_runtime_svc(uint32_t smc_fid, void *cookie, void *handle, |
| 130 | unsigned int flags); |
Joel Hutton | 5cc3bc8 | 2018-03-21 11:40:57 +0000 | [diff] [blame] | 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); |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 133 | void init_crash_reporting(void); |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 134 | |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 135 | extern uint8_t rt_svc_descs_indices[MAX_RT_SVCS]; |
| 136 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 137 | #endif /*__ASSEMBLER__*/ |
Sathees Balya | 50905c7 | 2018-10-05 13:30:59 +0100 | [diff] [blame] | 138 | #endif /* RUNTIME_SVC_H */ |