Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef __RUNTIME_SVC_H__ |
| 32 | #define __RUNTIME_SVC_H__ |
| 33 | #include <psci.h> |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 34 | #include <bl_common.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 35 | |
| 36 | /******************************************************************************* |
| 37 | * Bit definitions inside the function id as per the SMC calling convention |
| 38 | ******************************************************************************/ |
| 39 | #define FUNCID_TYPE_SHIFT 31 |
| 40 | #define FUNCID_CC_SHIFT 30 |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 41 | #define FUNCID_OEN_SHIFT 24 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 42 | #define FUNCID_NUM_SHIFT 0 |
| 43 | |
| 44 | #define FUNCID_TYPE_MASK 0x1 |
| 45 | #define FUNCID_CC_MASK 0x1 |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 46 | #define FUNCID_OEN_MASK 0x3f |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 47 | #define FUNCID_NUM_MASK 0xffff |
| 48 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 49 | #define FUNCID_TYPE_WIDTH 1 |
| 50 | #define FUNCID_CC_WIDTH 1 |
| 51 | #define FUNCID_OEN_WIDTH 6 |
| 52 | #define FUNCID_NUM_WIDTH 16 |
| 53 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 54 | #define GET_SMC_CC(id) ((id >> FUNCID_CC_SHIFT) & \ |
| 55 | FUNCID_CC_MASK) |
| 56 | |
| 57 | #define SMC_64 1 |
| 58 | #define SMC_32 0 |
| 59 | #define SMC_UNK 0xffffffff |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 60 | #define SMC_TYPE_FAST 1 |
| 61 | #define SMC_TYPE_STD 0 |
| 62 | |
| 63 | /******************************************************************************* |
| 64 | * Owning entity number definitions inside the function id as per the SMC |
| 65 | * calling convention |
| 66 | ******************************************************************************/ |
| 67 | #define OEN_ARM_START 0 |
| 68 | #define OEN_ARM_END 0 |
| 69 | #define OEN_CPU_START 1 |
| 70 | #define OEN_CPU_END 1 |
| 71 | #define OEN_SIP_START 2 |
| 72 | #define OEN_SIP_END 2 |
| 73 | #define OEN_OEM_START 3 |
| 74 | #define OEN_OEM_END 3 |
| 75 | #define OEN_STD_START 4 /* Standard Calls */ |
| 76 | #define OEN_STD_END 4 |
| 77 | #define OEN_TAP_START 48 /* Trusted Applications */ |
| 78 | #define OEN_TAP_END 49 |
| 79 | #define OEN_TOS_START 50 /* Trusted OS */ |
| 80 | #define OEN_TOS_END 63 |
| 81 | #define OEN_LIMIT 64 |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 82 | |
| 83 | /******************************************************************************* |
| 84 | * Constants to indicate type of exception to the common exception handler. |
| 85 | ******************************************************************************/ |
| 86 | #define SYNC_EXCEPTION_SP_EL0 0x0 |
| 87 | #define IRQ_SP_EL0 0x1 |
| 88 | #define FIQ_SP_EL0 0x2 |
| 89 | #define SERROR_SP_EL0 0x3 |
| 90 | #define SYNC_EXCEPTION_SP_ELX 0x4 |
| 91 | #define IRQ_SP_ELX 0x5 |
| 92 | #define FIQ_SP_ELX 0x6 |
| 93 | #define SERROR_SP_ELX 0x7 |
| 94 | #define SYNC_EXCEPTION_AARCH64 0x8 |
| 95 | #define IRQ_AARCH64 0x9 |
| 96 | #define FIQ_AARCH64 0xa |
| 97 | #define SERROR_AARCH64 0xb |
| 98 | #define SYNC_EXCEPTION_AARCH32 0xc |
| 99 | #define IRQ_AARCH32 0xd |
| 100 | #define FIQ_AARCH32 0xe |
| 101 | #define SERROR_AARCH32 0xf |
| 102 | |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 103 | /******************************************************************************* |
| 104 | * Constants that allow assembler code to access members of the 'gp_regs' |
| 105 | * structure at their correct offsets. |
| 106 | ******************************************************************************/ |
| 107 | #define SIZEOF_GPREGS 0x110 |
| 108 | #define GPREGS_X0_OFF 0x0 |
| 109 | #define GPREGS_X1_OFF 0x8 |
| 110 | #define GPREGS_X2_OFF 0x10 |
| 111 | #define GPREGS_X3_OFF 0x18 |
| 112 | #define GPREGS_X4_OFF 0x20 |
| 113 | #define GPREGS_X5_OFF 0x28 |
| 114 | #define GPREGS_X6_OFF 0x30 |
| 115 | #define GPREGS_X7_OFF 0x38 |
| 116 | #define GPREGS_X8_OFF 0x40 |
| 117 | #define GPREGS_X9_OFF 0x48 |
| 118 | #define GPREGS_X10_OFF 0x50 |
| 119 | #define GPREGS_X11_OFF 0x58 |
| 120 | #define GPREGS_X12_OFF 0x60 |
| 121 | #define GPREGS_X13_OFF 0x68 |
| 122 | #define GPREGS_X14_OFF 0x70 |
| 123 | #define GPREGS_X15_OFF 0x78 |
| 124 | #define GPREGS_X16_OFF 0x80 |
| 125 | #define GPREGS_X17_OFF 0x88 |
| 126 | #define GPREGS_X18_OFF 0x90 |
| 127 | #define GPREGS_X19_OFF 0x98 |
| 128 | #define GPREGS_X20_OFF 0xA0 |
| 129 | #define GPREGS_X21_OFF 0xA8 |
| 130 | #define GPREGS_X22_OFF 0xB0 |
| 131 | #define GPREGS_X23_OFF 0xB8 |
| 132 | #define GPREGS_X24_OFF 0xC0 |
| 133 | #define GPREGS_X25_OFF 0xC8 |
| 134 | #define GPREGS_X26_OFF 0xD0 |
| 135 | #define GPREGS_X27_OFF 0xD8 |
| 136 | #define GPREGS_X28_OFF 0xE0 |
| 137 | #define GPREGS_SP_EL0_OFF 0xE8 |
| 138 | #define GPREGS_SPSR_OFF 0xF0 |
| 139 | #define GPREGS_FP_OFF 0x100 |
| 140 | #define GPREGS_LR_OFF 0x108 |
| 141 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 142 | /* |
| 143 | * Constants to allow the assembler access a runtime service |
| 144 | * descriptor |
| 145 | */ |
| 146 | #define SIZEOF_RT_SVC_DESC 32 |
| 147 | #define RT_SVC_DESC_INIT 16 |
| 148 | #define RT_SVC_DESC_HANDLE 24 |
| 149 | |
| 150 | /* |
| 151 | * The function identifier has 6 bits for the owning entity number and |
| 152 | * single bit for the type of smc call. When taken together these |
| 153 | * values limit the maximum number of runtime services to 128. |
| 154 | */ |
| 155 | #define MAX_RT_SVCS 128 |
| 156 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 157 | #ifndef __ASSEMBLY__ |
| 158 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 159 | /* Prototype for runtime service initializing function */ |
| 160 | typedef int32_t (*rt_svc_init)(void); |
| 161 | |
| 162 | /* Convenience macros to return from SMC handler */ |
| 163 | #define SMC_RET1(_h, _x0) { \ |
| 164 | write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X0, (_x0)); \ |
| 165 | return _x0; \ |
| 166 | } |
| 167 | #define SMC_RET2(_h, _x0, _x1) { \ |
| 168 | write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X1, (_x1)); \ |
| 169 | SMC_RET1(_h, (_x0)); \ |
| 170 | } |
| 171 | #define SMC_RET3(_h, _x0, _x1, _x2) { \ |
| 172 | write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X2, (_x2)); \ |
| 173 | SMC_RET2(_h, (_x0), (_x1)); \ |
| 174 | } |
| 175 | #define SMC_RET4(_h, _x0, _x1, _x2, _x3) { \ |
| 176 | write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X3, (_x3)); \ |
| 177 | SMC_RET3(_h, (_x0), (_x1), (_x2)); \ |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * Convenience macros to access general purpose registers using handle provided |
| 183 | * to SMC handler. These takes the offset values defined in context.h |
| 184 | */ |
| 185 | #define SMC_GET_GP(_h, _g) \ |
| 186 | read_ctx_reg(get_gpregs_ctx(_h), (_g)); |
| 187 | #define SMC_SET_GP(_h, _g, _v) \ |
| 188 | write_ctx_reg(get_gpregs_ctx(_h), (_g), (_v)); |
| 189 | |
| 190 | /* |
| 191 | * Convenience macros to access EL3 context registers using handle provided to |
| 192 | * SMC handler. These takes the offset values defined in context.h |
| 193 | */ |
| 194 | #define SMC_GET_EL3(_h, _e) \ |
| 195 | read_ctx_reg(get_el3state_ctx(_h), (_e)); |
| 196 | #define SMC_SET_EL3(_h, _e, _v) \ |
| 197 | write_ctx_reg(get_el3state_ctx(_h), (_e), (_v)); |
| 198 | |
| 199 | /* |
| 200 | * Prototype for runtime service SMC handler function. x0 (SMC Function ID) to |
| 201 | * x4 are as passed by the caller. Rest of the arguments to SMC and the context |
| 202 | * can be accessed using the handle pointer. The cookie parameter is reserved |
| 203 | * for future use |
| 204 | */ |
| 205 | typedef uint64_t (*rt_svc_handle)(uint32_t smc_fid, |
| 206 | uint64_t x1, |
| 207 | uint64_t x2, |
| 208 | uint64_t x3, |
| 209 | uint64_t x4, |
| 210 | void *cookie, |
| 211 | void *handle, |
| 212 | uint64_t flags); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 213 | typedef struct { |
Sandrine Bailleux | bdb774d | 2013-12-17 13:47:14 +0000 | [diff] [blame] | 214 | uint64_t x0; |
| 215 | uint64_t x1; |
| 216 | uint64_t x2; |
| 217 | uint64_t x3; |
| 218 | uint64_t x4; |
| 219 | uint64_t x5; |
| 220 | uint64_t x6; |
| 221 | uint64_t x7; |
| 222 | uint64_t x8; |
| 223 | uint64_t x9; |
| 224 | uint64_t x10; |
| 225 | uint64_t x11; |
| 226 | uint64_t x12; |
| 227 | uint64_t x13; |
| 228 | uint64_t x14; |
| 229 | uint64_t x15; |
| 230 | uint64_t x16; |
| 231 | uint64_t x17; |
| 232 | uint64_t x18; |
| 233 | uint64_t x19; |
| 234 | uint64_t x20; |
| 235 | uint64_t x21; |
| 236 | uint64_t x22; |
| 237 | uint64_t x23; |
| 238 | uint64_t x24; |
| 239 | uint64_t x25; |
| 240 | uint64_t x26; |
| 241 | uint64_t x27; |
| 242 | uint64_t x28; |
| 243 | uint64_t sp_el0; |
| 244 | uint32_t spsr; |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 245 | /* |
| 246 | * Alignment constraint which allows save & restore of fp & lr on the |
| 247 | * stack during exception handling |
| 248 | */ |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 249 | uint64_t fp __aligned(16); |
Sandrine Bailleux | bdb774d | 2013-12-17 13:47:14 +0000 | [diff] [blame] | 250 | uint64_t lr; |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 251 | } __aligned(16) gp_regs; |
| 252 | |
| 253 | typedef struct { |
| 254 | uint8_t start_oen; |
| 255 | uint8_t end_oen; |
| 256 | uint8_t call_type; |
| 257 | const char *name; |
| 258 | rt_svc_init init; |
| 259 | rt_svc_handle handle; |
| 260 | } rt_svc_desc; |
| 261 | |
| 262 | /* |
| 263 | * Convenience macro to declare a service descriptor |
| 264 | */ |
| 265 | #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) \ |
| 266 | static const rt_svc_desc __svc_desc_ ## _name \ |
| 267 | __attribute__ ((section("rt_svc_descs"), used)) = { \ |
| 268 | _start, \ |
| 269 | _end, \ |
| 270 | _type, \ |
| 271 | #_name, \ |
| 272 | _setup, \ |
| 273 | _smch } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 274 | |
Achin Gupta | 4a826dd | 2013-11-25 14:00:56 +0000 | [diff] [blame] | 275 | /******************************************************************************* |
| 276 | * Compile time assertions to ensure that: |
| 277 | * 1) the assembler code's view of the size of the 'gp_regs' data structure is |
| 278 | * the same as the actual size of this data structure. |
| 279 | * 2) the assembler code's view of the offset of the frame pointer member of |
| 280 | * the 'gp_regs' structure is the same as the actual offset of this member. |
| 281 | ******************************************************************************/ |
| 282 | CASSERT((sizeof(gp_regs) == SIZEOF_GPREGS), assert_sizeof_gpregs_mismatch); |
| 283 | CASSERT(GPREGS_FP_OFF == __builtin_offsetof(gp_regs, fp), \ |
| 284 | assert_gpregs_fp_offset_mismatch); |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 285 | /* |
| 286 | * Compile time assertions related to the 'rt_svc_desc' structure to: |
| 287 | * 1. ensure that the assembler and the compiler view of the size |
| 288 | * of the structure are the same. |
| 289 | * 2. ensure that the assembler and the compiler see the initialisation |
| 290 | * routine at the same offset. |
| 291 | * 2. ensure that the assembler and the compiler see the handler |
| 292 | * routine at the same offset. |
| 293 | */ |
| 294 | CASSERT((sizeof(rt_svc_desc) == SIZEOF_RT_SVC_DESC), \ |
| 295 | assert_sizeof_rt_svc_desc_mismatch); |
| 296 | CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc, init), \ |
| 297 | assert_rt_svc_desc_init_offset_mismatch); |
| 298 | CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc, handle), \ |
| 299 | assert_rt_svc_desc_handle_offset_mismatch); |
| 300 | |
| 301 | |
| 302 | /* |
| 303 | * This macro combines the call type and the owning entity number corresponding |
| 304 | * to a runtime service to generate a unique owning entity number. This unique |
| 305 | * oen is used to access an entry in the 'rt_svc_descs_indices' array. The entry |
| 306 | * contains the index of the service descriptor in the 'rt_svc_descs' array. |
| 307 | */ |
| 308 | #define get_unique_oen(oen, call_type) ((oen & FUNCID_OEN_MASK) | \ |
| 309 | ((call_type & FUNCID_TYPE_MASK) \ |
| 310 | << FUNCID_OEN_WIDTH)) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 311 | |
| 312 | /******************************************************************************* |
| 313 | * Function & variable prototypes |
| 314 | ******************************************************************************/ |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame^] | 315 | extern void runtime_svc_init(); |
| 316 | extern uint64_t __RT_SVC_DESCS_START__; |
| 317 | extern uint64_t __RT_SVC_DESCS_END__; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 318 | |
| 319 | #endif /*__ASSEMBLY__*/ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 320 | #endif /* __RUNTIME_SVC_H__ */ |