blob: 6a07b013251b7f77178446279992df67ec729ce1 [file] [log] [blame]
Yatharth Kochar6c0566c2015-10-02 17:56:48 +01001/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
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 __SMCC_HELPERS_H__
32#define __SMCC_HELPERS_H__
33
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
39#define FUNCID_OEN_SHIFT 24
40#define FUNCID_NUM_SHIFT 0
41
42#define FUNCID_TYPE_MASK 0x1
43#define FUNCID_CC_MASK 0x1
44#define FUNCID_OEN_MASK 0x3f
45#define FUNCID_NUM_MASK 0xffff
46
47#define FUNCID_TYPE_WIDTH 1
48#define FUNCID_CC_WIDTH 1
49#define FUNCID_OEN_WIDTH 6
50#define FUNCID_NUM_WIDTH 16
51
52#define GET_SMC_CC(id) ((id >> FUNCID_CC_SHIFT) & \
53 FUNCID_CC_MASK)
54#define GET_SMC_TYPE(id) ((id >> FUNCID_TYPE_SHIFT) & \
55 FUNCID_TYPE_MASK)
56
57#define SMC_64 1
58#define SMC_32 0
59#define SMC_UNK 0xffffffff
60#define SMC_TYPE_FAST 1
61#define SMC_TYPE_STD 0
62#define SMC_PREEMPTED 0xfffffffe
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
82
83#ifndef __ASSEMBLY__
84
85#include <cassert.h>
86#include <context.h>
87#include <stdint.h>
88
89/* Various flags passed to SMC handlers */
90#define SMC_FROM_SECURE (0 << 0)
91#define SMC_FROM_NON_SECURE (1 << 0)
92
93#define is_caller_non_secure(_f) (!!(_f & SMC_FROM_NON_SECURE))
94#define is_caller_secure(_f) (!(is_caller_non_secure(_f)))
95
96/* Convenience macros to return from SMC handler */
97#define SMC_RET0(_h) { \
98 return (uint64_t) (_h); \
99}
100#define SMC_RET1(_h, _x0) { \
101 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X0, (_x0)); \
102 SMC_RET0(_h); \
103}
104#define SMC_RET2(_h, _x0, _x1) { \
105 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X1, (_x1)); \
106 SMC_RET1(_h, (_x0)); \
107}
108#define SMC_RET3(_h, _x0, _x1, _x2) { \
109 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X2, (_x2)); \
110 SMC_RET2(_h, (_x0), (_x1)); \
111}
112#define SMC_RET4(_h, _x0, _x1, _x2, _x3) { \
113 write_ctx_reg(get_gpregs_ctx(_h), CTX_GPREG_X3, (_x3)); \
114 SMC_RET3(_h, (_x0), (_x1), (_x2)); \
115}
116
117/*
118 * Convenience macros to access general purpose registers using handle provided
119 * to SMC handler. These takes the offset values defined in context.h
120 */
121#define SMC_GET_GP(_h, _g) \
122 read_ctx_reg(get_gpregs_ctx(_h), (_g));
123#define SMC_SET_GP(_h, _g, _v) \
124 write_ctx_reg(get_gpregs_ctx(_h), (_g), (_v));
125
126/*
127 * Convenience macros to access EL3 context registers using handle provided to
128 * SMC handler. These takes the offset values defined in context.h
129 */
130#define SMC_GET_EL3(_h, _e) \
131 read_ctx_reg(get_el3state_ctx(_h), (_e));
132#define SMC_SET_EL3(_h, _e, _v) \
133 write_ctx_reg(get_el3state_ctx(_h), (_e), (_v));
134
135/* The macro below is used to identify a Standard Service SMC call */
136#define is_std_svc_call(_fid) ((((_fid) >> FUNCID_OEN_SHIFT) & \
137 FUNCID_OEN_MASK) == OEN_STD_START)
138
139/* The macro below is used to identify a valid Fast SMC call */
140#define is_valid_fast_smc(_fid) ((!(((_fid) >> 16) & 0xff)) && \
141 (GET_SMC_TYPE(_fid) == SMC_TYPE_FAST))
142
143/*
144 * Macro to define UUID for services. Apart from defining and initializing a
145 * uuid_t structure, this macro verifies that the first word of the defined UUID
146 * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
147 * returned UUID in x0 for an invalid SMC error return
148 */
149#define DEFINE_SVC_UUID(_name, _tl, _tm, _th, _cl, _ch, \
150 _n0, _n1, _n2, _n3, _n4, _n5) \
151 CASSERT(_tl != SMC_UNK, invalid_svc_uuid);\
152 static const uuid_t _name = { \
153 _tl, _tm, _th, _cl, _ch, \
154 { _n0, _n1, _n2, _n3, _n4, _n5 } \
155 }
156
157/* Return a UUID in the SMC return registers */
158#define SMC_UUID_RET(_h, _uuid) \
159 SMC_RET4(handle, ((const uint32_t *) &(_uuid))[0], \
160 ((const uint32_t *) &(_uuid))[1], \
161 ((const uint32_t *) &(_uuid))[2], \
162 ((const uint32_t *) &(_uuid))[3])
163
164#endif /*__ASSEMBLY__*/
165#endif /* __SMCC_HELPERS_H__ */