xref: /rk3399_ARM-atf/include/lib/el3_runtime/cpu_data.h (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
1532ed618SSoby Mathew /*
2532ed618SSoby Mathew  * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
4*82cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7532ed618SSoby Mathew #ifndef __CPU_DATA_H__
8532ed618SSoby Mathew #define __CPU_DATA_H__
9532ed618SSoby Mathew 
10e33b78a6SSoby Mathew #ifdef AARCH32
11e33b78a6SSoby Mathew 
12e33b78a6SSoby Mathew #if CRASH_REPORTING
13e33b78a6SSoby Mathew #error "Crash reporting is not supported in AArch32"
14e33b78a6SSoby Mathew #endif
15e33b78a6SSoby Mathew #define CPU_DATA_CPU_OPS_PTR		0x0
16e33b78a6SSoby Mathew 
17e33b78a6SSoby Mathew #else /* AARCH32 */
18e33b78a6SSoby Mathew 
19532ed618SSoby Mathew /* Offsets for the cpu_data structure */
20532ed618SSoby Mathew #define CPU_DATA_CRASH_BUF_OFFSET	0x18
21e33b78a6SSoby Mathew /* need enough space in crash buffer to save 8 registers */
22e33b78a6SSoby Mathew #define CPU_DATA_CRASH_BUF_SIZE		64
23e33b78a6SSoby Mathew #define CPU_DATA_CPU_OPS_PTR		0x10
24e33b78a6SSoby Mathew 
25e33b78a6SSoby Mathew #endif /* AARCH32 */
26e33b78a6SSoby Mathew 
27532ed618SSoby Mathew #if CRASH_REPORTING
28532ed618SSoby Mathew #define CPU_DATA_LOG2SIZE		7
29872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END		(CPU_DATA_CRASH_BUF_OFFSET + \
30872be88aSdp-arm 						CPU_DATA_CRASH_BUF_SIZE)
31532ed618SSoby Mathew #else
32532ed618SSoby Mathew #define CPU_DATA_LOG2SIZE		6
33872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END		CPU_DATA_CRASH_BUF_OFFSET
34872be88aSdp-arm #endif
35872be88aSdp-arm 
36872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
37872be88aSdp-arm /* Temporary space to store PMF timestamps from assembly code */
38872be88aSdp-arm #define CPU_DATA_PMF_TS_COUNT		1
39872be88aSdp-arm #define CPU_DATA_PMF_TS0_OFFSET		CPU_DATA_CRASH_BUF_END
40872be88aSdp-arm #define CPU_DATA_PMF_TS0_IDX		0
41532ed618SSoby Mathew #endif
42532ed618SSoby Mathew 
43532ed618SSoby Mathew #ifndef __ASSEMBLY__
44532ed618SSoby Mathew 
45532ed618SSoby Mathew #include <arch_helpers.h>
46532ed618SSoby Mathew #include <cassert.h>
47532ed618SSoby Mathew #include <platform_def.h>
48532ed618SSoby Mathew #include <psci.h>
49532ed618SSoby Mathew #include <stdint.h>
50532ed618SSoby Mathew 
51532ed618SSoby Mathew /* Offsets for the cpu_data structure */
52532ed618SSoby Mathew #define CPU_DATA_PSCI_LOCK_OFFSET	__builtin_offsetof\
53532ed618SSoby Mathew 		(cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)
54532ed618SSoby Mathew 
55532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
56532ed618SSoby Mathew #define CPU_DATA_PLAT_PCPU_OFFSET	__builtin_offsetof\
57532ed618SSoby Mathew 		(cpu_data_t, platform_cpu_data)
58532ed618SSoby Mathew #endif
59532ed618SSoby Mathew 
60532ed618SSoby Mathew /*******************************************************************************
61532ed618SSoby Mathew  * Function & variable prototypes
62532ed618SSoby Mathew  ******************************************************************************/
63532ed618SSoby Mathew 
64532ed618SSoby Mathew /*******************************************************************************
65532ed618SSoby Mathew  * Cache of frequently used per-cpu data:
66532ed618SSoby Mathew  *   Pointers to non-secure and secure security state contexts
67532ed618SSoby Mathew  *   Address of the crash stack
68532ed618SSoby Mathew  * It is aligned to the cache line boundary to allow efficient concurrent
69532ed618SSoby Mathew  * manipulation of these pointers on different cpus
70532ed618SSoby Mathew  *
71532ed618SSoby Mathew  * TODO: Add other commonly used variables to this (tf_issues#90)
72532ed618SSoby Mathew  *
73532ed618SSoby Mathew  * The data structure and the _cpu_data accessors should not be used directly
74532ed618SSoby Mathew  * by components that have per-cpu members. The member access macros should be
75532ed618SSoby Mathew  * used for this.
76532ed618SSoby Mathew  ******************************************************************************/
77532ed618SSoby Mathew typedef struct cpu_data {
78e33b78a6SSoby Mathew #ifndef AARCH32
79532ed618SSoby Mathew 	void *cpu_context[2];
80e33b78a6SSoby Mathew #endif
81532ed618SSoby Mathew 	uintptr_t cpu_ops_ptr;
82532ed618SSoby Mathew #if CRASH_REPORTING
83532ed618SSoby Mathew 	u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
84532ed618SSoby Mathew #endif
85872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
86872be88aSdp-arm 	uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT];
87872be88aSdp-arm #endif
88532ed618SSoby Mathew 	struct psci_cpu_data psci_svc_cpu_data;
89532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
90532ed618SSoby Mathew 	uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
91532ed618SSoby Mathew #endif
92532ed618SSoby Mathew } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;
93532ed618SSoby Mathew 
94532ed618SSoby Mathew #if CRASH_REPORTING
95532ed618SSoby Mathew /* verify assembler offsets match data structures */
96532ed618SSoby Mathew CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof
97532ed618SSoby Mathew 	(cpu_data_t, crash_buf),
98532ed618SSoby Mathew 	assert_cpu_data_crash_stack_offset_mismatch);
99532ed618SSoby Mathew #endif
100532ed618SSoby Mathew 
101532ed618SSoby Mathew CASSERT((1 << CPU_DATA_LOG2SIZE) == sizeof(cpu_data_t),
102532ed618SSoby Mathew 	assert_cpu_data_log2size_mismatch);
103532ed618SSoby Mathew 
104532ed618SSoby Mathew CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof
105532ed618SSoby Mathew 		(cpu_data_t, cpu_ops_ptr),
106532ed618SSoby Mathew 		assert_cpu_data_cpu_ops_ptr_offset_mismatch);
107532ed618SSoby Mathew 
108872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
109872be88aSdp-arm CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof
110872be88aSdp-arm 		(cpu_data_t, cpu_data_pmf_ts[0]),
111872be88aSdp-arm 		assert_cpu_data_pmf_ts0_offset_mismatch);
112872be88aSdp-arm #endif
113872be88aSdp-arm 
114532ed618SSoby Mathew struct cpu_data *_cpu_data_by_index(uint32_t cpu_index);
115532ed618SSoby Mathew 
116e33b78a6SSoby Mathew #ifndef AARCH32
117532ed618SSoby Mathew /* Return the cpu_data structure for the current CPU. */
118532ed618SSoby Mathew static inline struct cpu_data *_cpu_data(void)
119532ed618SSoby Mathew {
120532ed618SSoby Mathew 	return (cpu_data_t *)read_tpidr_el3();
121532ed618SSoby Mathew }
122e33b78a6SSoby Mathew #else
123e33b78a6SSoby Mathew struct cpu_data *_cpu_data(void);
124e33b78a6SSoby Mathew #endif
125532ed618SSoby Mathew 
126532ed618SSoby Mathew /**************************************************************************
127532ed618SSoby Mathew  * APIs for initialising and accessing per-cpu data
128532ed618SSoby Mathew  *************************************************************************/
129532ed618SSoby Mathew 
130532ed618SSoby Mathew void init_cpu_data_ptr(void);
131532ed618SSoby Mathew void init_cpu_ops(void);
132532ed618SSoby Mathew 
133532ed618SSoby Mathew #define get_cpu_data(_m)		   _cpu_data()->_m
134532ed618SSoby Mathew #define set_cpu_data(_m, _v)		   _cpu_data()->_m = _v
135532ed618SSoby Mathew #define get_cpu_data_by_index(_ix, _m)	   _cpu_data_by_index(_ix)->_m
136532ed618SSoby Mathew #define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = _v
137532ed618SSoby Mathew 
138532ed618SSoby Mathew #define flush_cpu_data(_m)	   flush_dcache_range((uintptr_t)	  \
139532ed618SSoby Mathew 						      &(_cpu_data()->_m), \
140532ed618SSoby Mathew 						      sizeof(_cpu_data()->_m))
141532ed618SSoby Mathew #define inv_cpu_data(_m)	   inv_dcache_range((uintptr_t)	  	  \
142532ed618SSoby Mathew 						      &(_cpu_data()->_m), \
143532ed618SSoby Mathew 						      sizeof(_cpu_data()->_m))
144532ed618SSoby Mathew #define flush_cpu_data_by_index(_ix, _m)	\
145532ed618SSoby Mathew 				   flush_dcache_range((uintptr_t)	  \
146532ed618SSoby Mathew 					 &(_cpu_data_by_index(_ix)->_m),  \
147532ed618SSoby Mathew 					 sizeof(_cpu_data_by_index(_ix)->_m))
148532ed618SSoby Mathew 
149532ed618SSoby Mathew 
150532ed618SSoby Mathew #endif /* __ASSEMBLY__ */
151532ed618SSoby Mathew #endif /* __CPU_DATA_H__ */
152