xref: /rk3399_ARM-atf/include/lib/el3_runtime/aarch32/context.h (revision e33b78a658bd54a815c780e17c2d0073db6f59db)
1*e33b78a6SSoby Mathew /*
2*e33b78a6SSoby Mathew  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3*e33b78a6SSoby Mathew  *
4*e33b78a6SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5*e33b78a6SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6*e33b78a6SSoby Mathew  *
7*e33b78a6SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8*e33b78a6SSoby Mathew  * list of conditions and the following disclaimer.
9*e33b78a6SSoby Mathew  *
10*e33b78a6SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11*e33b78a6SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12*e33b78a6SSoby Mathew  * and/or other materials provided with the distribution.
13*e33b78a6SSoby Mathew  *
14*e33b78a6SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15*e33b78a6SSoby Mathew  * to endorse or promote products derived from this software without specific
16*e33b78a6SSoby Mathew  * prior written permission.
17*e33b78a6SSoby Mathew  *
18*e33b78a6SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*e33b78a6SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*e33b78a6SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*e33b78a6SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*e33b78a6SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*e33b78a6SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*e33b78a6SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*e33b78a6SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*e33b78a6SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*e33b78a6SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*e33b78a6SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29*e33b78a6SSoby Mathew  */
30*e33b78a6SSoby Mathew 
31*e33b78a6SSoby Mathew #ifndef __CONTEXT_H__
32*e33b78a6SSoby Mathew #define __CONTEXT_H__
33*e33b78a6SSoby Mathew 
34*e33b78a6SSoby Mathew /*******************************************************************************
35*e33b78a6SSoby Mathew  * Constants that allow assembler code to access members of and the 'regs'
36*e33b78a6SSoby Mathew  * structure at their correct offsets.
37*e33b78a6SSoby Mathew  ******************************************************************************/
38*e33b78a6SSoby Mathew #define CTX_REGS_OFFSET		0x0
39*e33b78a6SSoby Mathew #define CTX_GPREG_R0		0x0
40*e33b78a6SSoby Mathew #define CTX_GPREG_R1		0x4
41*e33b78a6SSoby Mathew #define CTX_GPREG_R2		0x8
42*e33b78a6SSoby Mathew #define CTX_GPREG_R3		0xC
43*e33b78a6SSoby Mathew #define CTX_LR			0x10
44*e33b78a6SSoby Mathew #define CTX_SCR			0x14
45*e33b78a6SSoby Mathew #define CTX_SPSR		0x18
46*e33b78a6SSoby Mathew #define CTX_NS_SCTLR		0x1C
47*e33b78a6SSoby Mathew #define CTX_REGS_END		0x20
48*e33b78a6SSoby Mathew 
49*e33b78a6SSoby Mathew #ifndef __ASSEMBLY__
50*e33b78a6SSoby Mathew 
51*e33b78a6SSoby Mathew #include <cassert.h>
52*e33b78a6SSoby Mathew #include <stdint.h>
53*e33b78a6SSoby Mathew 
54*e33b78a6SSoby Mathew /*
55*e33b78a6SSoby Mathew  * Common constants to help define the 'cpu_context' structure and its
56*e33b78a6SSoby Mathew  * members below.
57*e33b78a6SSoby Mathew  */
58*e33b78a6SSoby Mathew #define WORD_SHIFT		2
59*e33b78a6SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs)	\
60*e33b78a6SSoby Mathew 	typedef struct name {			\
61*e33b78a6SSoby Mathew 		uint32_t _regs[num_regs];	\
62*e33b78a6SSoby Mathew 	}  __aligned(8) name##_t
63*e33b78a6SSoby Mathew 
64*e33b78a6SSoby Mathew /* Constants to determine the size of individual context structures */
65*e33b78a6SSoby Mathew #define CTX_REG_ALL		(CTX_REGS_END >> WORD_SHIFT)
66*e33b78a6SSoby Mathew 
67*e33b78a6SSoby Mathew DEFINE_REG_STRUCT(regs, CTX_REG_ALL);
68*e33b78a6SSoby Mathew 
69*e33b78a6SSoby Mathew #undef CTX_REG_ALL
70*e33b78a6SSoby Mathew 
71*e33b78a6SSoby Mathew #define read_ctx_reg(ctx, offset)	((ctx)->_regs[offset >> WORD_SHIFT])
72*e33b78a6SSoby Mathew #define write_ctx_reg(ctx, offset, val)	(((ctx)->_regs[offset >> WORD_SHIFT]) \
73*e33b78a6SSoby Mathew 					 = val)
74*e33b78a6SSoby Mathew typedef struct cpu_context {
75*e33b78a6SSoby Mathew 	regs_t regs_ctx;
76*e33b78a6SSoby Mathew } cpu_context_t;
77*e33b78a6SSoby Mathew 
78*e33b78a6SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */
79*e33b78a6SSoby Mathew #define get_regs_ctx(h)		(&((cpu_context_t *) h)->regs_ctx)
80*e33b78a6SSoby Mathew 
81*e33b78a6SSoby Mathew /*
82*e33b78a6SSoby Mathew  * Compile time assertions related to the 'cpu_context' structure to
83*e33b78a6SSoby Mathew  * ensure that the assembler and the compiler view of the offsets of
84*e33b78a6SSoby Mathew  * the structure members is the same.
85*e33b78a6SSoby Mathew  */
86*e33b78a6SSoby Mathew CASSERT(CTX_REGS_OFFSET == __builtin_offsetof(cpu_context_t, regs_ctx), \
87*e33b78a6SSoby Mathew 	assert_core_context_regs_offset_mismatch);
88*e33b78a6SSoby Mathew 
89*e33b78a6SSoby Mathew #endif /* __ASSEMBLY__ */
90*e33b78a6SSoby Mathew 
91*e33b78a6SSoby Mathew #endif /* __CONTEXT_H__ */
92