xref: /rk3399_ARM-atf/include/lib/el3_runtime/cpu_data.h (revision 402b3cf8766fe2cb4ae462f7ee7761d08a1ba56c)
1532ed618SSoby Mathew /*
27fabe1a8SRoberto Vargas  * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
743534997SAntonio Nino Diaz #ifndef CPU_DATA_H
843534997SAntonio Nino Diaz #define CPU_DATA_H
9532ed618SSoby Mathew 
1086606eb5SEtienne Carriere #include <platform_def.h>	/* CACHE_WRITEBACK_GRANULE required */
1186606eb5SEtienne Carriere 
1209d40e0eSAntonio Nino Diaz #include <bl31/ehf.h>
1309d40e0eSAntonio Nino Diaz 
14*402b3cf8SJulius Werner #ifdef __aarch64__
15e33b78a6SSoby Mathew 
16532ed618SSoby Mathew /* Offsets for the cpu_data structure */
17532ed618SSoby Mathew #define CPU_DATA_CRASH_BUF_OFFSET	0x18
18e33b78a6SSoby Mathew /* need enough space in crash buffer to save 8 registers */
19e33b78a6SSoby Mathew #define CPU_DATA_CRASH_BUF_SIZE		64
20e33b78a6SSoby Mathew #define CPU_DATA_CPU_OPS_PTR		0x10
21e33b78a6SSoby Mathew 
22*402b3cf8SJulius Werner #else /* __aarch64__ */
23*402b3cf8SJulius Werner 
24*402b3cf8SJulius Werner #if CRASH_REPORTING
25*402b3cf8SJulius Werner #error "Crash reporting is not supported in AArch32"
26*402b3cf8SJulius Werner #endif
27*402b3cf8SJulius Werner #define CPU_DATA_CPU_OPS_PTR		0x0
28*402b3cf8SJulius Werner #define CPU_DATA_CRASH_BUF_OFFSET	0x4
29*402b3cf8SJulius Werner 
30*402b3cf8SJulius Werner #endif /* __aarch64__ */
31e33b78a6SSoby Mathew 
32532ed618SSoby Mathew #if CRASH_REPORTING
33872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END		(CPU_DATA_CRASH_BUF_OFFSET + \
34872be88aSdp-arm 						CPU_DATA_CRASH_BUF_SIZE)
35532ed618SSoby Mathew #else
36872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END		CPU_DATA_CRASH_BUF_OFFSET
37872be88aSdp-arm #endif
38872be88aSdp-arm 
3986606eb5SEtienne Carriere /* cpu_data size is the data size rounded up to the platform cache line size */
4086606eb5SEtienne Carriere #define CPU_DATA_SIZE			(((CPU_DATA_CRASH_BUF_END + \
4186606eb5SEtienne Carriere 					CACHE_WRITEBACK_GRANULE - 1) / \
4286606eb5SEtienne Carriere 						CACHE_WRITEBACK_GRANULE) * \
4386606eb5SEtienne Carriere 							CACHE_WRITEBACK_GRANULE)
4486606eb5SEtienne Carriere 
45872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
46872be88aSdp-arm /* Temporary space to store PMF timestamps from assembly code */
47872be88aSdp-arm #define CPU_DATA_PMF_TS_COUNT		1
48872be88aSdp-arm #define CPU_DATA_PMF_TS0_OFFSET		CPU_DATA_CRASH_BUF_END
49872be88aSdp-arm #define CPU_DATA_PMF_TS0_IDX		0
50532ed618SSoby Mathew #endif
51532ed618SSoby Mathew 
52d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
53532ed618SSoby Mathew 
54532ed618SSoby Mathew #include <arch_helpers.h>
5509d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
5609d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
57532ed618SSoby Mathew #include <platform_def.h>
58532ed618SSoby Mathew #include <stdint.h>
59532ed618SSoby Mathew 
60532ed618SSoby Mathew /* Offsets for the cpu_data structure */
61532ed618SSoby Mathew #define CPU_DATA_PSCI_LOCK_OFFSET	__builtin_offsetof\
62532ed618SSoby Mathew 		(cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)
63532ed618SSoby Mathew 
64532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
65532ed618SSoby Mathew #define CPU_DATA_PLAT_PCPU_OFFSET	__builtin_offsetof\
66532ed618SSoby Mathew 		(cpu_data_t, platform_cpu_data)
67532ed618SSoby Mathew #endif
68532ed618SSoby Mathew 
69532ed618SSoby Mathew /*******************************************************************************
70532ed618SSoby Mathew  * Function & variable prototypes
71532ed618SSoby Mathew  ******************************************************************************/
72532ed618SSoby Mathew 
73532ed618SSoby Mathew /*******************************************************************************
74532ed618SSoby Mathew  * Cache of frequently used per-cpu data:
75532ed618SSoby Mathew  *   Pointers to non-secure and secure security state contexts
76532ed618SSoby Mathew  *   Address of the crash stack
77532ed618SSoby Mathew  * It is aligned to the cache line boundary to allow efficient concurrent
78532ed618SSoby Mathew  * manipulation of these pointers on different cpus
79532ed618SSoby Mathew  *
80532ed618SSoby Mathew  * TODO: Add other commonly used variables to this (tf_issues#90)
81532ed618SSoby Mathew  *
82532ed618SSoby Mathew  * The data structure and the _cpu_data accessors should not be used directly
83532ed618SSoby Mathew  * by components that have per-cpu members. The member access macros should be
84532ed618SSoby Mathew  * used for this.
85532ed618SSoby Mathew  ******************************************************************************/
86532ed618SSoby Mathew typedef struct cpu_data {
87*402b3cf8SJulius Werner #ifdef __aarch64__
88532ed618SSoby Mathew 	void *cpu_context[2];
89e33b78a6SSoby Mathew #endif
90532ed618SSoby Mathew 	uintptr_t cpu_ops_ptr;
91532ed618SSoby Mathew #if CRASH_REPORTING
92532ed618SSoby Mathew 	u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
93532ed618SSoby Mathew #endif
94872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
95872be88aSdp-arm 	uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT];
96872be88aSdp-arm #endif
97532ed618SSoby Mathew 	struct psci_cpu_data psci_svc_cpu_data;
98532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
99532ed618SSoby Mathew 	uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
100532ed618SSoby Mathew #endif
10121b818c0SJeenu Viswambharan #if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING
10221b818c0SJeenu Viswambharan 	pe_exc_data_t ehf_data;
10321b818c0SJeenu Viswambharan #endif
104532ed618SSoby Mathew } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;
105532ed618SSoby Mathew 
1067fabe1a8SRoberto Vargas extern cpu_data_t percpu_data[PLATFORM_CORE_COUNT];
1077fabe1a8SRoberto Vargas 
108532ed618SSoby Mathew #if CRASH_REPORTING
109532ed618SSoby Mathew /* verify assembler offsets match data structures */
110532ed618SSoby Mathew CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof
111532ed618SSoby Mathew 	(cpu_data_t, crash_buf),
112532ed618SSoby Mathew 	assert_cpu_data_crash_stack_offset_mismatch);
113532ed618SSoby Mathew #endif
114532ed618SSoby Mathew 
11586606eb5SEtienne Carriere CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t),
11686606eb5SEtienne Carriere 		assert_cpu_data_size_mismatch);
117532ed618SSoby Mathew 
118532ed618SSoby Mathew CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof
119532ed618SSoby Mathew 		(cpu_data_t, cpu_ops_ptr),
120532ed618SSoby Mathew 		assert_cpu_data_cpu_ops_ptr_offset_mismatch);
121532ed618SSoby Mathew 
122872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
123872be88aSdp-arm CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof
124872be88aSdp-arm 		(cpu_data_t, cpu_data_pmf_ts[0]),
125872be88aSdp-arm 		assert_cpu_data_pmf_ts0_offset_mismatch);
126872be88aSdp-arm #endif
127872be88aSdp-arm 
128532ed618SSoby Mathew struct cpu_data *_cpu_data_by_index(uint32_t cpu_index);
129532ed618SSoby Mathew 
130*402b3cf8SJulius Werner #ifdef __aarch64__
131532ed618SSoby Mathew /* Return the cpu_data structure for the current CPU. */
132532ed618SSoby Mathew static inline struct cpu_data *_cpu_data(void)
133532ed618SSoby Mathew {
134532ed618SSoby Mathew 	return (cpu_data_t *)read_tpidr_el3();
135532ed618SSoby Mathew }
136e33b78a6SSoby Mathew #else
137e33b78a6SSoby Mathew struct cpu_data *_cpu_data(void);
138e33b78a6SSoby Mathew #endif
139532ed618SSoby Mathew 
140532ed618SSoby Mathew /**************************************************************************
141532ed618SSoby Mathew  * APIs for initialising and accessing per-cpu data
142532ed618SSoby Mathew  *************************************************************************/
143532ed618SSoby Mathew 
144532ed618SSoby Mathew void init_cpu_data_ptr(void);
145532ed618SSoby Mathew void init_cpu_ops(void);
146532ed618SSoby Mathew 
147532ed618SSoby Mathew #define get_cpu_data(_m)		   _cpu_data()->_m
148a0fee747SAntonio Nino Diaz #define set_cpu_data(_m, _v)		   _cpu_data()->_m = (_v)
149532ed618SSoby Mathew #define get_cpu_data_by_index(_ix, _m)	   _cpu_data_by_index(_ix)->_m
150a0fee747SAntonio Nino Diaz #define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = (_v)
1512614ea3eSJoel Hutton /* ((cpu_data_t *)0)->_m is a dummy to get the sizeof the struct member _m */
152532ed618SSoby Mathew #define flush_cpu_data(_m)	   flush_dcache_range((uintptr_t)	  \
153532ed618SSoby Mathew 						&(_cpu_data()->_m), \
1542614ea3eSJoel Hutton 						sizeof(((cpu_data_t *)0)->_m))
155532ed618SSoby Mathew #define inv_cpu_data(_m)	   inv_dcache_range((uintptr_t)	  	  \
156532ed618SSoby Mathew 						&(_cpu_data()->_m), \
1572614ea3eSJoel Hutton 						sizeof(((cpu_data_t *)0)->_m))
158532ed618SSoby Mathew #define flush_cpu_data_by_index(_ix, _m)	\
159532ed618SSoby Mathew 				   flush_dcache_range((uintptr_t)	  \
160532ed618SSoby Mathew 					 &(_cpu_data_by_index(_ix)->_m),  \
1612614ea3eSJoel Hutton 						sizeof(((cpu_data_t *)0)->_m))
162532ed618SSoby Mathew 
163532ed618SSoby Mathew 
164d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */
16543534997SAntonio Nino Diaz #endif /* CPU_DATA_H */
166