blob: 0f510f76064d9ccfaae2a760a77bd6271ad7a9bc [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
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__
Achin Gupta4f6ad662013-10-25 09:08:21 +010033
34/*******************************************************************************
35 * Bit definitions inside the function id as per the SMC calling convention
36 ******************************************************************************/
37#define FUNCID_TYPE_SHIFT 31
38#define FUNCID_CC_SHIFT 30
Achin Gupta7421b462014-02-01 18:53:26 +000039#define FUNCID_OEN_SHIFT 24
Achin Gupta4f6ad662013-10-25 09:08:21 +010040#define FUNCID_NUM_SHIFT 0
41
42#define FUNCID_TYPE_MASK 0x1
43#define FUNCID_CC_MASK 0x1
Achin Gupta7421b462014-02-01 18:53:26 +000044#define FUNCID_OEN_MASK 0x3f
Achin Gupta4f6ad662013-10-25 09:08:21 +010045#define FUNCID_NUM_MASK 0xffff
46
Achin Gupta7421b462014-02-01 18:53:26 +000047#define FUNCID_TYPE_WIDTH 1
48#define FUNCID_CC_WIDTH 1
49#define FUNCID_OEN_WIDTH 6
50#define FUNCID_NUM_WIDTH 16
51
Achin Gupta4f6ad662013-10-25 09:08:21 +010052#define GET_SMC_CC(id) ((id >> FUNCID_CC_SHIFT) & \
53 FUNCID_CC_MASK)
54
55#define SMC_64 1
56#define SMC_32 0
57#define SMC_UNK 0xffffffff
Achin Gupta7421b462014-02-01 18:53:26 +000058#define SMC_TYPE_FAST 1
59#define SMC_TYPE_STD 0
60
61/*******************************************************************************
62 * Owning entity number definitions inside the function id as per the SMC
63 * calling convention
64 ******************************************************************************/
65#define OEN_ARM_START 0
66#define OEN_ARM_END 0
67#define OEN_CPU_START 1
68#define OEN_CPU_END 1
69#define OEN_SIP_START 2
70#define OEN_SIP_END 2
71#define OEN_OEM_START 3
72#define OEN_OEM_END 3
73#define OEN_STD_START 4 /* Standard Calls */
74#define OEN_STD_END 4
75#define OEN_TAP_START 48 /* Trusted Applications */
76#define OEN_TAP_END 49
77#define OEN_TOS_START 50 /* Trusted OS */
78#define OEN_TOS_END 63
79#define OEN_LIMIT 64
Achin Gupta4f6ad662013-10-25 09:08:21 +010080
81/*******************************************************************************
82 * Constants to indicate type of exception to the common exception handler.
83 ******************************************************************************/
84#define SYNC_EXCEPTION_SP_EL0 0x0
85#define IRQ_SP_EL0 0x1
86#define FIQ_SP_EL0 0x2
87#define SERROR_SP_EL0 0x3
88#define SYNC_EXCEPTION_SP_ELX 0x4
89#define IRQ_SP_ELX 0x5
90#define FIQ_SP_ELX 0x6
91#define SERROR_SP_ELX 0x7
92#define SYNC_EXCEPTION_AARCH64 0x8
93#define IRQ_AARCH64 0x9
94#define FIQ_AARCH64 0xa
95#define SERROR_AARCH64 0xb
96#define SYNC_EXCEPTION_AARCH32 0xc
97#define IRQ_AARCH32 0xd
98#define FIQ_AARCH32 0xe
99#define SERROR_AARCH32 0xf
100
Achin Gupta4a826dd2013-11-25 14:00:56 +0000101/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000102 * Structure definition, typedefs & constants for the runtime service framework
Achin Gupta4a826dd2013-11-25 14:00:56 +0000103 ******************************************************************************/
Achin Gupta4a826dd2013-11-25 14:00:56 +0000104
Achin Gupta7421b462014-02-01 18:53:26 +0000105/*
106 * Constants to allow the assembler access a runtime service
107 * descriptor
108 */
Achin Gupta07f4e072014-02-02 12:02:23 +0000109#define RT_SVC_SIZE_LOG2 5
110#define SIZEOF_RT_SVC_DESC (1 << RT_SVC_SIZE_LOG2)
Achin Gupta7421b462014-02-01 18:53:26 +0000111#define RT_SVC_DESC_INIT 16
112#define RT_SVC_DESC_HANDLE 24
113
114/*
115 * The function identifier has 6 bits for the owning entity number and
116 * single bit for the type of smc call. When taken together these
117 * values limit the maximum number of runtime services to 128.
118 */
119#define MAX_RT_SVCS 128
120
Achin Gupta4f6ad662013-10-25 09:08:21 +0100121#ifndef __ASSEMBLY__
122
Dan Handley2bd4ef22014-04-09 13:14:54 +0100123#include <cassert.h>
124#include <context.h>
125#include <stdint.h>
126
Achin Gupta07f4e072014-02-02 12:02:23 +0000127/* Various flags passed to SMC handlers */
128#define SMC_FROM_SECURE (0 << 0)
129#define SMC_FROM_NON_SECURE (1 << 0)
130
131#define is_caller_non_secure(_f) (!!(_f & SMC_FROM_NON_SECURE))
132#define is_caller_secure(_f) (!(is_caller_non_secure(_f)))
133
Achin Gupta7421b462014-02-01 18:53:26 +0000134/* Prototype for runtime service initializing function */
Dan Handleye2712bc2014-04-10 15:37:22 +0100135typedef int32_t (*rt_svc_init_t)(void);
Achin Gupta7421b462014-02-01 18:53:26 +0000136
137/* Convenience macros to return from SMC handler */
Achin Guptaaeaab682014-05-09 13:21:31 +0100138#define SMC_RET0(_h) { \
139 return (uint64_t) (_h); \
140}
Achin Gupta7421b462014-02-01 18:53:26 +0000141#define SMC_RET1(_h, _x0) { \
142 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X0, (_x0)); \
Achin Guptaaeaab682014-05-09 13:21:31 +0100143 SMC_RET0(_h); \
Achin Gupta7421b462014-02-01 18:53:26 +0000144}
145#define SMC_RET2(_h, _x0, _x1) { \
146 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X1, (_x1)); \
147 SMC_RET1(_h, (_x0)); \
148}
149#define SMC_RET3(_h, _x0, _x1, _x2) { \
150 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X2, (_x2)); \
151 SMC_RET2(_h, (_x0), (_x1)); \
152}
153#define SMC_RET4(_h, _x0, _x1, _x2, _x3) { \
154 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X3, (_x3)); \
155 SMC_RET3(_h, (_x0), (_x1), (_x2)); \
156}
157
158
159/*
160 * Convenience macros to access general purpose registers using handle provided
161 * to SMC handler. These takes the offset values defined in context.h
162 */
163#define SMC_GET_GP(_h, _g) \
164 read_ctx_reg(get_gpregs_ctx(_h), (_g));
165#define SMC_SET_GP(_h, _g, _v) \
166 write_ctx_reg(get_gpregs_ctx(_h), (_g), (_v));
167
168/*
169 * Convenience macros to access EL3 context registers using handle provided to
170 * SMC handler. These takes the offset values defined in context.h
171 */
172#define SMC_GET_EL3(_h, _e) \
173 read_ctx_reg(get_el3state_ctx(_h), (_e));
174#define SMC_SET_EL3(_h, _e, _v) \
175 write_ctx_reg(get_el3state_ctx(_h), (_e), (_v));
176
177/*
178 * Prototype for runtime service SMC handler function. x0 (SMC Function ID) to
179 * x4 are as passed by the caller. Rest of the arguments to SMC and the context
180 * can be accessed using the handle pointer. The cookie parameter is reserved
181 * for future use
182 */
Dan Handleye2712bc2014-04-10 15:37:22 +0100183typedef uint64_t (*rt_svc_handle_t)(uint32_t smc_fid,
Achin Gupta7421b462014-02-01 18:53:26 +0000184 uint64_t x1,
185 uint64_t x2,
186 uint64_t x3,
187 uint64_t x4,
188 void *cookie,
189 void *handle,
190 uint64_t flags);
Dan Handleye2712bc2014-04-10 15:37:22 +0100191typedef struct rt_svc_desc {
Achin Gupta7421b462014-02-01 18:53:26 +0000192 uint8_t start_oen;
193 uint8_t end_oen;
194 uint8_t call_type;
195 const char *name;
Dan Handleye2712bc2014-04-10 15:37:22 +0100196 rt_svc_init_t init;
197 rt_svc_handle_t handle;
198} rt_svc_desc_t;
Achin Gupta7421b462014-02-01 18:53:26 +0000199
200/*
201 * Convenience macro to declare a service descriptor
202 */
203#define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) \
Dan Handleye2712bc2014-04-10 15:37:22 +0100204 static const rt_svc_desc_t __svc_desc_ ## _name \
Achin Gupta7421b462014-02-01 18:53:26 +0000205 __attribute__ ((section("rt_svc_descs"), used)) = { \
206 _start, \
207 _end, \
208 _type, \
209 #_name, \
210 _setup, \
211 _smch }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100212
Achin Gupta7421b462014-02-01 18:53:26 +0000213/*
214 * Compile time assertions related to the 'rt_svc_desc' structure to:
215 * 1. ensure that the assembler and the compiler view of the size
216 * of the structure are the same.
217 * 2. ensure that the assembler and the compiler see the initialisation
218 * routine at the same offset.
Achin Gupta07f4e072014-02-02 12:02:23 +0000219 * 3. ensure that the assembler and the compiler see the handler
Achin Gupta7421b462014-02-01 18:53:26 +0000220 * routine at the same offset.
221 */
Dan Handleye2712bc2014-04-10 15:37:22 +0100222CASSERT((sizeof(rt_svc_desc_t) == SIZEOF_RT_SVC_DESC), \
Achin Gupta7421b462014-02-01 18:53:26 +0000223 assert_sizeof_rt_svc_desc_mismatch);
Dan Handleye2712bc2014-04-10 15:37:22 +0100224CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc_t, init), \
Achin Gupta7421b462014-02-01 18:53:26 +0000225 assert_rt_svc_desc_init_offset_mismatch);
Dan Handleye2712bc2014-04-10 15:37:22 +0100226CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc_t, handle), \
Achin Gupta7421b462014-02-01 18:53:26 +0000227 assert_rt_svc_desc_handle_offset_mismatch);
228
229
230/*
231 * This macro combines the call type and the owning entity number corresponding
232 * to a runtime service to generate a unique owning entity number. This unique
233 * oen is used to access an entry in the 'rt_svc_descs_indices' array. The entry
234 * contains the index of the service descriptor in the 'rt_svc_descs' array.
235 */
236#define get_unique_oen(oen, call_type) ((oen & FUNCID_OEN_MASK) | \
237 ((call_type & FUNCID_TYPE_MASK) \
238 << FUNCID_OEN_WIDTH))
Achin Gupta4f6ad662013-10-25 09:08:21 +0100239
Jeenu Viswambharan1814a3e2014-02-28 10:08:33 +0000240
241/*
242 * Macro to define UUID for services. Apart from defining and initializing a
243 * uuid_t structure, this macro verifies that the first word of the defined UUID
244 * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
245 * returned UUID in x0 for an invalid SMC error return
246 */
247#define DEFINE_SVC_UUID(_name, _tl, _tm, _th, _cl, _ch, \
248 _n0, _n1, _n2, _n3, _n4, _n5) \
249 CASSERT(_tl != SMC_UNK, invalid_svc_uuid);\
250 static const uuid_t _name = { \
251 _tl, _tm, _th, _cl, _ch, \
252 { _n0, _n1, _n2, _n3, _n4, _n5 } \
253 }
254
255/* Return a UUID in the SMC return registers */
256#define SMC_UUID_RET(_h, _uuid) \
257 SMC_RET4(handle, ((const uint32_t *) &(_uuid))[0], \
258 ((const uint32_t *) &(_uuid))[1], \
259 ((const uint32_t *) &(_uuid))[2], \
260 ((const uint32_t *) &(_uuid))[3])
261
Achin Gupta4f6ad662013-10-25 09:08:21 +0100262/*******************************************************************************
263 * Function & variable prototypes
264 ******************************************************************************/
Achin Gupta7421b462014-02-01 18:53:26 +0000265extern void runtime_svc_init();
266extern uint64_t __RT_SVC_DESCS_START__;
267extern uint64_t __RT_SVC_DESCS_END__;
Soby Mathew5e5c2072014-04-07 15:28:55 +0100268extern uint64_t get_crash_stack(uint64_t mpidr);
Dan Handleybcd60ba2014-04-17 18:53:42 +0100269extern void runtime_exceptions(void);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100270#endif /*__ASSEMBLY__*/
Achin Gupta4f6ad662013-10-25 09:08:21 +0100271#endif /* __RUNTIME_SVC_H__ */