cpu_data.h 6.11 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2019, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
8
#ifndef CPU_DATA_H
#define CPU_DATA_H
9

10
11
#include <platform_def.h>	/* CACHE_WRITEBACK_GRANULE required */

12
13
#include <bl31/ehf.h>

14
15
16
/* Size of psci_cpu_data structure */
#define PSCI_CPU_DATA_SIZE		12

17
#ifdef __aarch64__
18

19
20
21
22
/* 8-bytes aligned size of psci_cpu_data structure */
#define PSCI_CPU_DATA_SIZE_ALIGNED	((PSCI_CPU_DATA_SIZE + 7) & ~7)

/* Offset of cpu_ops_ptr, size 8 bytes */
johpow01's avatar
johpow01 committed
23
24
25
#if ENABLE_RME
#define CPU_DATA_CPU_OPS_PTR		0x18
#else /* ENABLE_RME */
26
#define CPU_DATA_CPU_OPS_PTR		0x10
johpow01's avatar
johpow01 committed
27
#endif /* ENABLE_RME */
28
29
30

#if ENABLE_PAUTH
/* 8-bytes aligned offset of apiakey[2], size 16 bytes */
johpow01's avatar
johpow01 committed
31
32
33
34
35
36
37
38
39
40
#if ENABLE_RME
#define	CPU_DATA_APIAKEY_OFFSET		(0x20 + PSCI_CPU_DATA_SIZE_ALIGNED)
#else /* ENABLE_RME */
#define CPU_DATA_APIAKEY_OFFSET		(0x18 + PSCI_CPU_DATA_SIZE_ALIGNED)
#endif /* ENABLE_RME */
#define CPU_DATA_CRASH_BUF_OFFSET	(0x10 + CPU_DATA_APIAKEY_OFFSET)
#else /* ENABLE_PAUTH */
#if ENABLE_RME
#define CPU_DATA_CRASH_BUF_OFFSET	(0x20 + PSCI_CPU_DATA_SIZE_ALIGNED)
#else /* ENABLE_RME */
41
#define CPU_DATA_CRASH_BUF_OFFSET	(0x18 + PSCI_CPU_DATA_SIZE_ALIGNED)
johpow01's avatar
johpow01 committed
42
43
#endif /* ENABLE_RME */
#endif /* ENABLE_PAUTH */
44

45
46
47
/* need enough space in crash buffer to save 8 registers */
#define CPU_DATA_CRASH_BUF_SIZE		64

48
#else	/* !__aarch64__ */
49
50
51
52
53

#if CRASH_REPORTING
#error "Crash reporting is not supported in AArch32"
#endif
#define CPU_DATA_CPU_OPS_PTR		0x0
54
#define CPU_DATA_CRASH_BUF_OFFSET	(0x4 + PSCI_CPU_DATA_SIZE)
55

56
#endif	/* __aarch64__ */
57

58
#if CRASH_REPORTING
dp-arm's avatar
dp-arm committed
59
60
#define CPU_DATA_CRASH_BUF_END		(CPU_DATA_CRASH_BUF_OFFSET + \
						CPU_DATA_CRASH_BUF_SIZE)
61
#else
dp-arm's avatar
dp-arm committed
62
63
64
#define CPU_DATA_CRASH_BUF_END		CPU_DATA_CRASH_BUF_OFFSET
#endif

65
66
67
68
69
70
/* cpu_data size is the data size rounded up to the platform cache line size */
#define CPU_DATA_SIZE			(((CPU_DATA_CRASH_BUF_END + \
					CACHE_WRITEBACK_GRANULE - 1) / \
						CACHE_WRITEBACK_GRANULE) * \
							CACHE_WRITEBACK_GRANULE)

dp-arm's avatar
dp-arm committed
71
72
73
74
75
#if ENABLE_RUNTIME_INSTRUMENTATION
/* Temporary space to store PMF timestamps from assembly code */
#define CPU_DATA_PMF_TS_COUNT		1
#define CPU_DATA_PMF_TS0_OFFSET		CPU_DATA_CRASH_BUF_END
#define CPU_DATA_PMF_TS0_IDX		0
76
#endif
77

78
#ifndef __ASSEMBLER__
79
80

#include <arch_helpers.h>
81
82
#include <lib/cassert.h>
#include <lib/psci/psci.h>
83
84
85
#include <platform_def.h>
#include <stdint.h>

86
87
88
89
90
91
92
93
94
/* Offsets for the cpu_data structure */
#define CPU_DATA_PSCI_LOCK_OFFSET	__builtin_offsetof\
		(cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)

#if PLAT_PCPU_DATA_SIZE
#define CPU_DATA_PLAT_PCPU_OFFSET	__builtin_offsetof\
		(cpu_data_t, platform_cpu_data)
#endif

95
96
97
98
99
100
/*******************************************************************************
 * Function & variable prototypes
 ******************************************************************************/

/*******************************************************************************
 * Cache of frequently used per-cpu data:
johpow01's avatar
johpow01 committed
101
 *   Pointers to non-secure, realm, and secure security state contexts
102
103
104
105
106
107
108
109
110
 *   Address of the crash stack
 * It is aligned to the cache line boundary to allow efficient concurrent
 * manipulation of these pointers on different cpus
 *
 * The data structure and the _cpu_data accessors should not be used directly
 * by components that have per-cpu members. The member access macros should be
 * used for this.
 ******************************************************************************/
typedef struct cpu_data {
111
#ifdef __aarch64__
johpow01's avatar
johpow01 committed
112
113
114
#if ENABLE_RME
	void *cpu_context[3];
#else /* ENABLE_RME */
115
	void *cpu_context[2];
johpow01's avatar
johpow01 committed
116
117
#endif /* ENABLE_RME */
#endif /* __aarch64__ */
118
	uintptr_t cpu_ops_ptr;
119
120
121
122
	struct psci_cpu_data psci_svc_cpu_data;
#if ENABLE_PAUTH
	uint64_t apiakey[2];
#endif
123
#if CRASH_REPORTING
124
	u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
dp-arm's avatar
dp-arm committed
125
126
127
#endif
#if ENABLE_RUNTIME_INSTRUMENTATION
	uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT];
128
129
130
#endif
#if PLAT_PCPU_DATA_SIZE
	uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
131
#endif
132
133
134
#if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING
	pe_exc_data_t ehf_data;
#endif
135
136
} __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;

137
138
extern cpu_data_t percpu_data[PLATFORM_CORE_COUNT];

139
140
141
142
143
144
#if ENABLE_PAUTH
CASSERT(CPU_DATA_APIAKEY_OFFSET == __builtin_offsetof
	(cpu_data_t, apiakey),
	assert_cpu_data_crash_stack_offset_mismatch);
#endif

145
146
147
148
149
150
151
#if CRASH_REPORTING
/* verify assembler offsets match data structures */
CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof
	(cpu_data_t, crash_buf),
	assert_cpu_data_crash_stack_offset_mismatch);
#endif

152
153
CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t),
		assert_cpu_data_size_mismatch);
154

155
156
157
158
CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof
		(cpu_data_t, cpu_ops_ptr),
		assert_cpu_data_cpu_ops_ptr_offset_mismatch);

dp-arm's avatar
dp-arm committed
159
160
161
162
163
164
#if ENABLE_RUNTIME_INSTRUMENTATION
CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof
		(cpu_data_t, cpu_data_pmf_ts[0]),
		assert_cpu_data_pmf_ts0_offset_mismatch);
#endif

165
166
struct cpu_data *_cpu_data_by_index(uint32_t cpu_index);

167
#ifdef __aarch64__
168
169
170
171
172
/* Return the cpu_data structure for the current CPU. */
static inline struct cpu_data *_cpu_data(void)
{
	return (cpu_data_t *)read_tpidr_el3();
}
173
174
175
#else
struct cpu_data *_cpu_data(void);
#endif
176
177
178
179
180
181

/**************************************************************************
 * APIs for initialising and accessing per-cpu data
 *************************************************************************/

void init_cpu_data_ptr(void);
182
void init_cpu_ops(void);
183
184

#define get_cpu_data(_m)		   _cpu_data()->_m
185
#define set_cpu_data(_m, _v)		   _cpu_data()->_m = (_v)
186
#define get_cpu_data_by_index(_ix, _m)	   _cpu_data_by_index(_ix)->_m
187
#define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = (_v)
188
/* ((cpu_data_t *)0)->_m is a dummy to get the sizeof the struct member _m */
Soby Mathew's avatar
Soby Mathew committed
189
#define flush_cpu_data(_m)	   flush_dcache_range((uintptr_t)	  \
190
191
						&(_cpu_data()->_m), \
						sizeof(((cpu_data_t *)0)->_m))
Soby Mathew's avatar
Soby Mathew committed
192
#define inv_cpu_data(_m)	   inv_dcache_range((uintptr_t)	  	  \
193
194
						&(_cpu_data()->_m), \
						sizeof(((cpu_data_t *)0)->_m))
195
#define flush_cpu_data_by_index(_ix, _m)	\
196
				   flush_dcache_range((uintptr_t)	  \
197
					 &(_cpu_data_by_index(_ix)->_m),  \
198
						sizeof(((cpu_data_t *)0)->_m))
Achin Gupta's avatar
Achin Gupta committed
199

200

201
#endif /* __ASSEMBLER__ */
202
#endif /* CPU_DATA_H */