xref: /rk3399_ARM-atf/include/lib/el3_runtime/cpu_data.h (revision 532ed6183868036e4a4f83cd7a71b93266a3bdb7)
1*532ed618SSoby Mathew /*
2*532ed618SSoby Mathew  * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
3*532ed618SSoby Mathew  *
4*532ed618SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5*532ed618SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6*532ed618SSoby Mathew  *
7*532ed618SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8*532ed618SSoby Mathew  * list of conditions and the following disclaimer.
9*532ed618SSoby Mathew  *
10*532ed618SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11*532ed618SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12*532ed618SSoby Mathew  * and/or other materials provided with the distribution.
13*532ed618SSoby Mathew  *
14*532ed618SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15*532ed618SSoby Mathew  * to endorse or promote products derived from this software without specific
16*532ed618SSoby Mathew  * prior written permission.
17*532ed618SSoby Mathew  *
18*532ed618SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*532ed618SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*532ed618SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*532ed618SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*532ed618SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*532ed618SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*532ed618SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*532ed618SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*532ed618SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*532ed618SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*532ed618SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29*532ed618SSoby Mathew  */
30*532ed618SSoby Mathew 
31*532ed618SSoby Mathew #ifndef __CPU_DATA_H__
32*532ed618SSoby Mathew #define __CPU_DATA_H__
33*532ed618SSoby Mathew 
34*532ed618SSoby Mathew /* Offsets for the cpu_data structure */
35*532ed618SSoby Mathew #define CPU_DATA_CRASH_BUF_OFFSET	0x18
36*532ed618SSoby Mathew #if CRASH_REPORTING
37*532ed618SSoby Mathew #define CPU_DATA_LOG2SIZE		7
38*532ed618SSoby Mathew #else
39*532ed618SSoby Mathew #define CPU_DATA_LOG2SIZE		6
40*532ed618SSoby Mathew #endif
41*532ed618SSoby Mathew /* need enough space in crash buffer to save 8 registers */
42*532ed618SSoby Mathew #define CPU_DATA_CRASH_BUF_SIZE		64
43*532ed618SSoby Mathew #define CPU_DATA_CPU_OPS_PTR		0x10
44*532ed618SSoby Mathew 
45*532ed618SSoby Mathew #ifndef __ASSEMBLY__
46*532ed618SSoby Mathew 
47*532ed618SSoby Mathew #include <arch_helpers.h>
48*532ed618SSoby Mathew #include <cassert.h>
49*532ed618SSoby Mathew #include <platform_def.h>
50*532ed618SSoby Mathew #include <psci.h>
51*532ed618SSoby Mathew #include <stdint.h>
52*532ed618SSoby Mathew 
53*532ed618SSoby Mathew /* Offsets for the cpu_data structure */
54*532ed618SSoby Mathew #define CPU_DATA_PSCI_LOCK_OFFSET	__builtin_offsetof\
55*532ed618SSoby Mathew 		(cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)
56*532ed618SSoby Mathew 
57*532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
58*532ed618SSoby Mathew #define CPU_DATA_PLAT_PCPU_OFFSET	__builtin_offsetof\
59*532ed618SSoby Mathew 		(cpu_data_t, platform_cpu_data)
60*532ed618SSoby Mathew #endif
61*532ed618SSoby Mathew 
62*532ed618SSoby Mathew /*******************************************************************************
63*532ed618SSoby Mathew  * Function & variable prototypes
64*532ed618SSoby Mathew  ******************************************************************************/
65*532ed618SSoby Mathew 
66*532ed618SSoby Mathew /*******************************************************************************
67*532ed618SSoby Mathew  * Cache of frequently used per-cpu data:
68*532ed618SSoby Mathew  *   Pointers to non-secure and secure security state contexts
69*532ed618SSoby Mathew  *   Address of the crash stack
70*532ed618SSoby Mathew  * It is aligned to the cache line boundary to allow efficient concurrent
71*532ed618SSoby Mathew  * manipulation of these pointers on different cpus
72*532ed618SSoby Mathew  *
73*532ed618SSoby Mathew  * TODO: Add other commonly used variables to this (tf_issues#90)
74*532ed618SSoby Mathew  *
75*532ed618SSoby Mathew  * The data structure and the _cpu_data accessors should not be used directly
76*532ed618SSoby Mathew  * by components that have per-cpu members. The member access macros should be
77*532ed618SSoby Mathew  * used for this.
78*532ed618SSoby Mathew  ******************************************************************************/
79*532ed618SSoby Mathew typedef struct cpu_data {
80*532ed618SSoby Mathew 	void *cpu_context[2];
81*532ed618SSoby Mathew 	uintptr_t cpu_ops_ptr;
82*532ed618SSoby Mathew #if CRASH_REPORTING
83*532ed618SSoby Mathew 	u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
84*532ed618SSoby Mathew #endif
85*532ed618SSoby Mathew 	struct psci_cpu_data psci_svc_cpu_data;
86*532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
87*532ed618SSoby Mathew 	uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
88*532ed618SSoby Mathew #endif
89*532ed618SSoby Mathew } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;
90*532ed618SSoby Mathew 
91*532ed618SSoby Mathew #if CRASH_REPORTING
92*532ed618SSoby Mathew /* verify assembler offsets match data structures */
93*532ed618SSoby Mathew CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof
94*532ed618SSoby Mathew 	(cpu_data_t, crash_buf),
95*532ed618SSoby Mathew 	assert_cpu_data_crash_stack_offset_mismatch);
96*532ed618SSoby Mathew #endif
97*532ed618SSoby Mathew 
98*532ed618SSoby Mathew CASSERT((1 << CPU_DATA_LOG2SIZE) == sizeof(cpu_data_t),
99*532ed618SSoby Mathew 	assert_cpu_data_log2size_mismatch);
100*532ed618SSoby Mathew 
101*532ed618SSoby Mathew CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof
102*532ed618SSoby Mathew 		(cpu_data_t, cpu_ops_ptr),
103*532ed618SSoby Mathew 		assert_cpu_data_cpu_ops_ptr_offset_mismatch);
104*532ed618SSoby Mathew 
105*532ed618SSoby Mathew struct cpu_data *_cpu_data_by_index(uint32_t cpu_index);
106*532ed618SSoby Mathew 
107*532ed618SSoby Mathew /* Return the cpu_data structure for the current CPU. */
108*532ed618SSoby Mathew static inline struct cpu_data *_cpu_data(void)
109*532ed618SSoby Mathew {
110*532ed618SSoby Mathew 	return (cpu_data_t *)read_tpidr_el3();
111*532ed618SSoby Mathew }
112*532ed618SSoby Mathew 
113*532ed618SSoby Mathew 
114*532ed618SSoby Mathew /**************************************************************************
115*532ed618SSoby Mathew  * APIs for initialising and accessing per-cpu data
116*532ed618SSoby Mathew  *************************************************************************/
117*532ed618SSoby Mathew 
118*532ed618SSoby Mathew void init_cpu_data_ptr(void);
119*532ed618SSoby Mathew void init_cpu_ops(void);
120*532ed618SSoby Mathew 
121*532ed618SSoby Mathew #define get_cpu_data(_m)		   _cpu_data()->_m
122*532ed618SSoby Mathew #define set_cpu_data(_m, _v)		   _cpu_data()->_m = _v
123*532ed618SSoby Mathew #define get_cpu_data_by_index(_ix, _m)	   _cpu_data_by_index(_ix)->_m
124*532ed618SSoby Mathew #define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = _v
125*532ed618SSoby Mathew 
126*532ed618SSoby Mathew #define flush_cpu_data(_m)	   flush_dcache_range((uintptr_t)	  \
127*532ed618SSoby Mathew 						      &(_cpu_data()->_m), \
128*532ed618SSoby Mathew 						      sizeof(_cpu_data()->_m))
129*532ed618SSoby Mathew #define inv_cpu_data(_m)	   inv_dcache_range((uintptr_t)	  	  \
130*532ed618SSoby Mathew 						      &(_cpu_data()->_m), \
131*532ed618SSoby Mathew 						      sizeof(_cpu_data()->_m))
132*532ed618SSoby Mathew #define flush_cpu_data_by_index(_ix, _m)	\
133*532ed618SSoby Mathew 				   flush_dcache_range((uintptr_t)	  \
134*532ed618SSoby Mathew 					 &(_cpu_data_by_index(_ix)->_m),  \
135*532ed618SSoby Mathew 					 sizeof(_cpu_data_by_index(_ix)->_m))
136*532ed618SSoby Mathew 
137*532ed618SSoby Mathew 
138*532ed618SSoby Mathew #endif /* __ASSEMBLY__ */
139*532ed618SSoby Mathew #endif /* __CPU_DATA_H__ */
140