xref: /rk3399_ARM-atf/include/lib/el3_runtime/cpu_data.h (revision ed108b56051de5da8024568a06781ce287e86c78)
1532ed618SSoby Mathew /*
2*ed108b56SAlexei Fedorov  * Copyright (c) 2014-2019, 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*ed108b56SAlexei Fedorov /* Size of psci_cpu_data structure */
15*ed108b56SAlexei Fedorov #define PSCI_CPU_DATA_SIZE		12
16*ed108b56SAlexei Fedorov 
17402b3cf8SJulius Werner #ifdef __aarch64__
18e33b78a6SSoby Mathew 
19*ed108b56SAlexei Fedorov /* 8-bytes aligned size of psci_cpu_data structure */
20*ed108b56SAlexei Fedorov #define PSCI_CPU_DATA_SIZE_ALIGNED	((PSCI_CPU_DATA_SIZE + 7) & ~7)
21*ed108b56SAlexei Fedorov 
22*ed108b56SAlexei Fedorov /* Offset of cpu_ops_ptr, size 8 bytes */
23e33b78a6SSoby Mathew #define CPU_DATA_CPU_OPS_PTR		0x10
24e33b78a6SSoby Mathew 
25*ed108b56SAlexei Fedorov #if ENABLE_PAUTH
26*ed108b56SAlexei Fedorov /* 8-bytes aligned offset of apiakey[2], size 16 bytes */
27*ed108b56SAlexei Fedorov #define	CPU_DATA_APIAKEY_OFFSET		(0x18 + PSCI_CPU_DATA_SIZE_ALIGNED)
28*ed108b56SAlexei Fedorov #define CPU_DATA_CRASH_BUF_OFFSET	(CPU_DATA_APIAKEY_OFFSET + 0x10)
29*ed108b56SAlexei Fedorov #else
30*ed108b56SAlexei Fedorov #define CPU_DATA_CRASH_BUF_OFFSET	(0x18 + PSCI_CPU_DATA_SIZE_ALIGNED)
31*ed108b56SAlexei Fedorov #endif	/* ENABLE_PAUTH */
32*ed108b56SAlexei Fedorov 
33*ed108b56SAlexei Fedorov /* need enough space in crash buffer to save 8 registers */
34*ed108b56SAlexei Fedorov #define CPU_DATA_CRASH_BUF_SIZE		64
35*ed108b56SAlexei Fedorov 
36*ed108b56SAlexei Fedorov #else	/* !__aarch64__ */
37402b3cf8SJulius Werner 
38402b3cf8SJulius Werner #if CRASH_REPORTING
39402b3cf8SJulius Werner #error "Crash reporting is not supported in AArch32"
40402b3cf8SJulius Werner #endif
41402b3cf8SJulius Werner #define CPU_DATA_CPU_OPS_PTR		0x0
42*ed108b56SAlexei Fedorov #define CPU_DATA_CRASH_BUF_OFFSET	(0x4 + PSCI_CPU_DATA_SIZE)
43402b3cf8SJulius Werner 
44402b3cf8SJulius Werner #endif	/* __aarch64__ */
45e33b78a6SSoby Mathew 
46532ed618SSoby Mathew #if CRASH_REPORTING
47872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END		(CPU_DATA_CRASH_BUF_OFFSET + \
48872be88aSdp-arm 						CPU_DATA_CRASH_BUF_SIZE)
49532ed618SSoby Mathew #else
50872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END		CPU_DATA_CRASH_BUF_OFFSET
51872be88aSdp-arm #endif
52872be88aSdp-arm 
5386606eb5SEtienne Carriere /* cpu_data size is the data size rounded up to the platform cache line size */
5486606eb5SEtienne Carriere #define CPU_DATA_SIZE			(((CPU_DATA_CRASH_BUF_END + \
5586606eb5SEtienne Carriere 					CACHE_WRITEBACK_GRANULE - 1) / \
5686606eb5SEtienne Carriere 						CACHE_WRITEBACK_GRANULE) * \
5786606eb5SEtienne Carriere 							CACHE_WRITEBACK_GRANULE)
5886606eb5SEtienne Carriere 
59872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
60872be88aSdp-arm /* Temporary space to store PMF timestamps from assembly code */
61872be88aSdp-arm #define CPU_DATA_PMF_TS_COUNT		1
62872be88aSdp-arm #define CPU_DATA_PMF_TS0_OFFSET		CPU_DATA_CRASH_BUF_END
63872be88aSdp-arm #define CPU_DATA_PMF_TS0_IDX		0
64532ed618SSoby Mathew #endif
65532ed618SSoby Mathew 
66d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
67532ed618SSoby Mathew 
68532ed618SSoby Mathew #include <arch_helpers.h>
6909d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
7009d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
71532ed618SSoby Mathew #include <platform_def.h>
72532ed618SSoby Mathew #include <stdint.h>
73532ed618SSoby Mathew 
74532ed618SSoby Mathew /* Offsets for the cpu_data structure */
75532ed618SSoby Mathew #define CPU_DATA_PSCI_LOCK_OFFSET	__builtin_offsetof\
76532ed618SSoby Mathew 		(cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)
77532ed618SSoby Mathew 
78532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
79532ed618SSoby Mathew #define CPU_DATA_PLAT_PCPU_OFFSET	__builtin_offsetof\
80532ed618SSoby Mathew 		(cpu_data_t, platform_cpu_data)
81532ed618SSoby Mathew #endif
82532ed618SSoby Mathew 
83532ed618SSoby Mathew /*******************************************************************************
84532ed618SSoby Mathew  * Function & variable prototypes
85532ed618SSoby Mathew  ******************************************************************************/
86532ed618SSoby Mathew 
87532ed618SSoby Mathew /*******************************************************************************
88532ed618SSoby Mathew  * Cache of frequently used per-cpu data:
89532ed618SSoby Mathew  *   Pointers to non-secure and secure security state contexts
90532ed618SSoby Mathew  *   Address of the crash stack
91532ed618SSoby Mathew  * It is aligned to the cache line boundary to allow efficient concurrent
92532ed618SSoby Mathew  * manipulation of these pointers on different cpus
93532ed618SSoby Mathew  *
94532ed618SSoby Mathew  * TODO: Add other commonly used variables to this (tf_issues#90)
95532ed618SSoby Mathew  *
96532ed618SSoby Mathew  * The data structure and the _cpu_data accessors should not be used directly
97532ed618SSoby Mathew  * by components that have per-cpu members. The member access macros should be
98532ed618SSoby Mathew  * used for this.
99532ed618SSoby Mathew  ******************************************************************************/
100532ed618SSoby Mathew typedef struct cpu_data {
101402b3cf8SJulius Werner #ifdef __aarch64__
102532ed618SSoby Mathew 	void *cpu_context[2];
103e33b78a6SSoby Mathew #endif
104532ed618SSoby Mathew 	uintptr_t cpu_ops_ptr;
105*ed108b56SAlexei Fedorov 	struct psci_cpu_data psci_svc_cpu_data;
106*ed108b56SAlexei Fedorov #if ENABLE_PAUTH
107*ed108b56SAlexei Fedorov 	uint64_t apiakey[2];
108*ed108b56SAlexei Fedorov #endif
109532ed618SSoby Mathew #if CRASH_REPORTING
110532ed618SSoby Mathew 	u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
111532ed618SSoby Mathew #endif
112872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
113872be88aSdp-arm 	uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT];
114872be88aSdp-arm #endif
115532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE
116532ed618SSoby Mathew 	uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
117532ed618SSoby Mathew #endif
11821b818c0SJeenu Viswambharan #if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING
11921b818c0SJeenu Viswambharan 	pe_exc_data_t ehf_data;
12021b818c0SJeenu Viswambharan #endif
121532ed618SSoby Mathew } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;
122532ed618SSoby Mathew 
1237fabe1a8SRoberto Vargas extern cpu_data_t percpu_data[PLATFORM_CORE_COUNT];
1247fabe1a8SRoberto Vargas 
125*ed108b56SAlexei Fedorov #if ENABLE_PAUTH
126*ed108b56SAlexei Fedorov CASSERT(CPU_DATA_APIAKEY_OFFSET == __builtin_offsetof
127*ed108b56SAlexei Fedorov 	(cpu_data_t, apiakey),
128*ed108b56SAlexei Fedorov 	assert_cpu_data_crash_stack_offset_mismatch);
129*ed108b56SAlexei Fedorov #endif
130*ed108b56SAlexei Fedorov 
131532ed618SSoby Mathew #if CRASH_REPORTING
132532ed618SSoby Mathew /* verify assembler offsets match data structures */
133532ed618SSoby Mathew CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof
134532ed618SSoby Mathew 	(cpu_data_t, crash_buf),
135532ed618SSoby Mathew 	assert_cpu_data_crash_stack_offset_mismatch);
136532ed618SSoby Mathew #endif
137532ed618SSoby Mathew 
13886606eb5SEtienne Carriere CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t),
13986606eb5SEtienne Carriere 		assert_cpu_data_size_mismatch);
140532ed618SSoby Mathew 
141532ed618SSoby Mathew CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof
142532ed618SSoby Mathew 		(cpu_data_t, cpu_ops_ptr),
143532ed618SSoby Mathew 		assert_cpu_data_cpu_ops_ptr_offset_mismatch);
144532ed618SSoby Mathew 
145872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION
146872be88aSdp-arm CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof
147872be88aSdp-arm 		(cpu_data_t, cpu_data_pmf_ts[0]),
148872be88aSdp-arm 		assert_cpu_data_pmf_ts0_offset_mismatch);
149872be88aSdp-arm #endif
150872be88aSdp-arm 
151532ed618SSoby Mathew struct cpu_data *_cpu_data_by_index(uint32_t cpu_index);
152532ed618SSoby Mathew 
153402b3cf8SJulius Werner #ifdef __aarch64__
154532ed618SSoby Mathew /* Return the cpu_data structure for the current CPU. */
155532ed618SSoby Mathew static inline struct cpu_data *_cpu_data(void)
156532ed618SSoby Mathew {
157532ed618SSoby Mathew 	return (cpu_data_t *)read_tpidr_el3();
158532ed618SSoby Mathew }
159e33b78a6SSoby Mathew #else
160e33b78a6SSoby Mathew struct cpu_data *_cpu_data(void);
161e33b78a6SSoby Mathew #endif
162532ed618SSoby Mathew 
163532ed618SSoby Mathew /**************************************************************************
164532ed618SSoby Mathew  * APIs for initialising and accessing per-cpu data
165532ed618SSoby Mathew  *************************************************************************/
166532ed618SSoby Mathew 
167532ed618SSoby Mathew void init_cpu_data_ptr(void);
168532ed618SSoby Mathew void init_cpu_ops(void);
169532ed618SSoby Mathew 
170532ed618SSoby Mathew #define get_cpu_data(_m)		   _cpu_data()->_m
171a0fee747SAntonio Nino Diaz #define set_cpu_data(_m, _v)		   _cpu_data()->_m = (_v)
172532ed618SSoby Mathew #define get_cpu_data_by_index(_ix, _m)	   _cpu_data_by_index(_ix)->_m
173a0fee747SAntonio Nino Diaz #define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = (_v)
1742614ea3eSJoel Hutton /* ((cpu_data_t *)0)->_m is a dummy to get the sizeof the struct member _m */
175532ed618SSoby Mathew #define flush_cpu_data(_m)	   flush_dcache_range((uintptr_t)	  \
176532ed618SSoby Mathew 						&(_cpu_data()->_m), \
1772614ea3eSJoel Hutton 						sizeof(((cpu_data_t *)0)->_m))
178532ed618SSoby Mathew #define inv_cpu_data(_m)	   inv_dcache_range((uintptr_t)	  	  \
179532ed618SSoby Mathew 						&(_cpu_data()->_m), \
1802614ea3eSJoel Hutton 						sizeof(((cpu_data_t *)0)->_m))
181532ed618SSoby Mathew #define flush_cpu_data_by_index(_ix, _m)	\
182532ed618SSoby Mathew 				   flush_dcache_range((uintptr_t)	  \
183532ed618SSoby Mathew 					 &(_cpu_data_by_index(_ix)->_m),  \
1842614ea3eSJoel Hutton 						sizeof(((cpu_data_t *)0)->_m))
185532ed618SSoby Mathew 
186532ed618SSoby Mathew 
187d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */
18843534997SAntonio Nino Diaz #endif /* CPU_DATA_H */
189