blob: 8b548067491310870bc9c32ecbfa2226f54d972f [file] [log] [blame]
Andrew Thoelke8c28fe02014-06-02 11:40:35 +01001/*
Omkar Anand Kulkarni91ed5ae2024-01-11 15:50:11 +05302 * Copyright (c) 2014-2024, Arm Limited and Contributors. All rights reserved.
Andrew Thoelke8c28fe02014-06-02 11:40:35 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Andrew Thoelke8c28fe02014-06-02 11:40:35 +01005 */
6
Antonio Nino Diaz9fe40fd2018-10-25 17:11:02 +01007#ifndef CPU_DATA_H
8#define CPU_DATA_H
Andrew Thoelke8c28fe02014-06-02 11:40:35 +01009
Etienne Carriere97ad6ce2017-09-01 10:22:20 +020010#include <platform_def.h> /* CACHE_WRITEBACK_GRANULE required */
11
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <bl31/ehf.h>
13
Alexei Fedorovf41355c2019-09-13 14:11:59 +010014/* Size of psci_cpu_data structure */
15#define PSCI_CPU_DATA_SIZE 12
16
Julius Werner8e0ef0f2019-07-09 14:02:43 -070017#ifdef __aarch64__
Soby Mathew748be1d2016-05-05 14:10:46 +010018
Alexei Fedorovf41355c2019-09-13 14:11:59 +010019/* 8-bytes aligned size of psci_cpu_data structure */
20#define PSCI_CPU_DATA_SIZE_ALIGNED ((PSCI_CPU_DATA_SIZE + 7) & ~7)
21
Zelalem Awekeb6301e62021-07-09 17:54:30 -050022#if ENABLE_RME
23/* Size of cpu_context array */
24#define CPU_DATA_CONTEXT_NUM 3
Alexei Fedorovf41355c2019-09-13 14:11:59 +010025/* Offset of cpu_ops_ptr, size 8 bytes */
Zelalem Awekeb6301e62021-07-09 17:54:30 -050026#define CPU_DATA_CPU_OPS_PTR 0x18
27#else /* ENABLE_RME */
28#define CPU_DATA_CONTEXT_NUM 2
Alexei Fedorovf41355c2019-09-13 14:11:59 +010029#define CPU_DATA_CPU_OPS_PTR 0x10
Zelalem Awekeb6301e62021-07-09 17:54:30 -050030#endif /* ENABLE_RME */
Alexei Fedorovf41355c2019-09-13 14:11:59 +010031
32#if ENABLE_PAUTH
33/* 8-bytes aligned offset of apiakey[2], size 16 bytes */
Zelalem Awekeb6301e62021-07-09 17:54:30 -050034#define CPU_DATA_APIAKEY_OFFSET (0x8 + PSCI_CPU_DATA_SIZE_ALIGNED \
35 + CPU_DATA_CPU_OPS_PTR)
36#define CPU_DATA_CRASH_BUF_OFFSET (0x10 + CPU_DATA_APIAKEY_OFFSET)
37#else /* ENABLE_PAUTH */
38#define CPU_DATA_CRASH_BUF_OFFSET (0x8 + PSCI_CPU_DATA_SIZE_ALIGNED \
39 + CPU_DATA_CPU_OPS_PTR)
40#endif /* ENABLE_PAUTH */
Alexei Fedorovf41355c2019-09-13 14:11:59 +010041
Soby Mathew748be1d2016-05-05 14:10:46 +010042/* need enough space in crash buffer to save 8 registers */
43#define CPU_DATA_CRASH_BUF_SIZE 64
Soby Mathew748be1d2016-05-05 14:10:46 +010044
Alexei Fedorovf41355c2019-09-13 14:11:59 +010045#else /* !__aarch64__ */
Soby Mathew748be1d2016-05-05 14:10:46 +010046
Soby Mathewc1adbbc2014-06-25 10:07:40 +010047#if CRASH_REPORTING
Julius Werner8e0ef0f2019-07-09 14:02:43 -070048#error "Crash reporting is not supported in AArch32"
49#endif
50#define CPU_DATA_CPU_OPS_PTR 0x0
Alexei Fedorovf41355c2019-09-13 14:11:59 +010051#define CPU_DATA_CRASH_BUF_OFFSET (0x4 + PSCI_CPU_DATA_SIZE)
Julius Werner8e0ef0f2019-07-09 14:02:43 -070052
Alexei Fedorovf41355c2019-09-13 14:11:59 +010053#endif /* __aarch64__ */
Julius Werner8e0ef0f2019-07-09 14:02:43 -070054
55#if CRASH_REPORTING
dp-arm3cac7862016-09-19 11:18:44 +010056#define CPU_DATA_CRASH_BUF_END (CPU_DATA_CRASH_BUF_OFFSET + \
57 CPU_DATA_CRASH_BUF_SIZE)
Soby Mathewc1adbbc2014-06-25 10:07:40 +010058#else
dp-arm3cac7862016-09-19 11:18:44 +010059#define CPU_DATA_CRASH_BUF_END CPU_DATA_CRASH_BUF_OFFSET
Soby Mathewc1adbbc2014-06-25 10:07:40 +010060#endif
Soby Mathewc704cbc2014-08-14 11:33:56 +010061
Omkar Anand Kulkarni91ed5ae2024-01-11 15:50:11 +053062/* buffer space for EHF data is sizeof(pe_exc_data_t) */
63#define CPU_DATA_EHF_DATA_SIZE 8
64#define CPU_DATA_EHF_DATA_BUF_OFFSET CPU_DATA_CRASH_BUF_END
65
66#if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING
67#define CPU_DATA_EHF_DATA_BUF_END (CPU_DATA_EHF_DATA_BUF_OFFSET + \
68 CPU_DATA_EHF_DATA_SIZE)
69#else
70#define CPU_DATA_EHF_DATA_BUF_END CPU_DATA_EHF_DATA_BUF_OFFSET
71#endif /* EL3_EXCEPTION_HANDLING */
72
Etienne Carriere97ad6ce2017-09-01 10:22:20 +020073/* cpu_data size is the data size rounded up to the platform cache line size */
Omkar Anand Kulkarni91ed5ae2024-01-11 15:50:11 +053074#define CPU_DATA_SIZE (((CPU_DATA_EHF_DATA_BUF_END + \
Etienne Carriere97ad6ce2017-09-01 10:22:20 +020075 CACHE_WRITEBACK_GRANULE - 1) / \
76 CACHE_WRITEBACK_GRANULE) * \
77 CACHE_WRITEBACK_GRANULE)
78
dp-arm3cac7862016-09-19 11:18:44 +010079#if ENABLE_RUNTIME_INSTRUMENTATION
80/* Temporary space to store PMF timestamps from assembly code */
81#define CPU_DATA_PMF_TS_COUNT 1
Omkar Anand Kulkarni91ed5ae2024-01-11 15:50:11 +053082#define CPU_DATA_PMF_TS0_OFFSET CPU_DATA_EHF_DATA_BUF_END
dp-arm3cac7862016-09-19 11:18:44 +010083#define CPU_DATA_PMF_TS0_IDX 0
84#endif
85
Julius Werner53456fc2019-07-09 13:49:11 -070086#ifndef __ASSEMBLER__
Andrew Thoelke8c28fe02014-06-02 11:40:35 +010087
Zelalem Awekeb6301e62021-07-09 17:54:30 -050088#include <assert.h>
89#include <stdint.h>
90
Andrew Thoelke8c28fe02014-06-02 11:40:35 +010091#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000092#include <lib/cassert.h>
93#include <lib/psci/psci.h>
Zelalem Awekeb6301e62021-07-09 17:54:30 -050094
Andrew Thoelke8c28fe02014-06-02 11:40:35 +010095#include <platform_def.h>
Andrew Thoelke8c28fe02014-06-02 11:40:35 +010096
Soby Mathew523d6332015-01-08 18:02:19 +000097/* Offsets for the cpu_data structure */
98#define CPU_DATA_PSCI_LOCK_OFFSET __builtin_offsetof\
99 (cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)
100
101#if PLAT_PCPU_DATA_SIZE
102#define CPU_DATA_PLAT_PCPU_OFFSET __builtin_offsetof\
103 (cpu_data_t, platform_cpu_data)
104#endif
105
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500106typedef enum context_pas {
107 CPU_CONTEXT_SECURE = 0,
108 CPU_CONTEXT_NS,
109#if ENABLE_RME
110 CPU_CONTEXT_REALM,
111#endif
112 CPU_CONTEXT_NUM
113} context_pas_t;
114
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100115/*******************************************************************************
116 * Function & variable prototypes
117 ******************************************************************************/
118
119/*******************************************************************************
120 * Cache of frequently used per-cpu data:
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500121 * Pointers to non-secure, realm, and secure security state contexts
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100122 * Address of the crash stack
123 * It is aligned to the cache line boundary to allow efficient concurrent
124 * manipulation of these pointers on different cpus
125 *
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100126 * The data structure and the _cpu_data accessors should not be used directly
127 * by components that have per-cpu members. The member access macros should be
128 * used for this.
129 ******************************************************************************/
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100130typedef struct cpu_data {
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700131#ifdef __aarch64__
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500132 void *cpu_context[CPU_DATA_CONTEXT_NUM];
133#endif /* __aarch64__ */
Soby Mathewa0fedc42016-06-16 14:52:04 +0100134 uintptr_t cpu_ops_ptr;
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100135 struct psci_cpu_data psci_svc_cpu_data;
136#if ENABLE_PAUTH
137 uint64_t apiakey[2];
138#endif
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100139#if CRASH_REPORTING
Soby Mathewa0fedc42016-06-16 14:52:04 +0100140 u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100141#endif
dp-arm3cac7862016-09-19 11:18:44 +0100142#if ENABLE_RUNTIME_INSTRUMENTATION
143 uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT];
144#endif
Soby Mathew523d6332015-01-08 18:02:19 +0000145#if PLAT_PCPU_DATA_SIZE
146 uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
147#endif
Jeenu Viswambharan10a67272017-09-22 08:32:10 +0100148#if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING
149 pe_exc_data_t ehf_data;
150#endif
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100151} __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;
152
Roberto Vargas05712702018-02-12 12:36:17 +0000153extern cpu_data_t percpu_data[PLATFORM_CORE_COUNT];
154
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500155#ifdef __aarch64__
156CASSERT(CPU_DATA_CONTEXT_NUM == CPU_CONTEXT_NUM,
157 assert_cpu_data_context_num_mismatch);
158#endif
159
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100160#if ENABLE_PAUTH
161CASSERT(CPU_DATA_APIAKEY_OFFSET == __builtin_offsetof
162 (cpu_data_t, apiakey),
Olivier Deprezf57491e2021-08-19 11:36:26 +0200163 assert_cpu_data_pauth_stack_offset_mismatch);
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100164#endif
165
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100166#if CRASH_REPORTING
167/* verify assembler offsets match data structures */
168CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof
169 (cpu_data_t, crash_buf),
170 assert_cpu_data_crash_stack_offset_mismatch);
171#endif
172
Omkar Anand Kulkarni91ed5ae2024-01-11 15:50:11 +0530173#if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING
174CASSERT(CPU_DATA_EHF_DATA_BUF_OFFSET == __builtin_offsetof
175 (cpu_data_t, ehf_data),
176 assert_cpu_data_ehf_stack_offset_mismatch);
177#endif
178
Etienne Carriere97ad6ce2017-09-01 10:22:20 +0200179CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t),
180 assert_cpu_data_size_mismatch);
Soby Mathewc1adbbc2014-06-25 10:07:40 +0100181
Soby Mathewc704cbc2014-08-14 11:33:56 +0100182CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof
183 (cpu_data_t, cpu_ops_ptr),
184 assert_cpu_data_cpu_ops_ptr_offset_mismatch);
185
dp-arm3cac7862016-09-19 11:18:44 +0100186#if ENABLE_RUNTIME_INSTRUMENTATION
187CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof
188 (cpu_data_t, cpu_data_pmf_ts[0]),
189 assert_cpu_data_pmf_ts0_offset_mismatch);
190#endif
191
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100192struct cpu_data *_cpu_data_by_index(uint32_t cpu_index);
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100193
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700194#ifdef __aarch64__
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100195/* Return the cpu_data structure for the current CPU. */
196static inline struct cpu_data *_cpu_data(void)
197{
198 return (cpu_data_t *)read_tpidr_el3();
199}
Soby Mathew748be1d2016-05-05 14:10:46 +0100200#else
201struct cpu_data *_cpu_data(void);
202#endif
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100203
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500204/*
205 * Returns the index of the cpu_context array for the given security state.
206 * All accesses to cpu_context should be through this helper to make sure
207 * an access is not out-of-bounds. The function assumes security_state is
208 * valid.
209 */
210static inline context_pas_t get_cpu_context_index(uint32_t security_state)
211{
212 if (security_state == SECURE) {
213 return CPU_CONTEXT_SECURE;
214 } else {
215#if ENABLE_RME
216 if (security_state == NON_SECURE) {
217 return CPU_CONTEXT_NS;
218 } else {
219 assert(security_state == REALM);
220 return CPU_CONTEXT_REALM;
221 }
222#else
223 assert(security_state == NON_SECURE);
224 return CPU_CONTEXT_NS;
225#endif
226 }
227}
228
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100229/**************************************************************************
230 * APIs for initialising and accessing per-cpu data
231 *************************************************************************/
232
233void init_cpu_data_ptr(void);
Vikram Kanigiri9b38fc82015-01-29 18:27:38 +0000234void init_cpu_ops(void);
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100235
236#define get_cpu_data(_m) _cpu_data()->_m
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000237#define set_cpu_data(_m, _v) _cpu_data()->_m = (_v)
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100238#define get_cpu_data_by_index(_ix, _m) _cpu_data_by_index(_ix)->_m
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000239#define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = (_v)
Joel Hutton43a4d572017-10-20 10:31:14 +0100240/* ((cpu_data_t *)0)->_m is a dummy to get the sizeof the struct member _m */
Soby Mathew24ab34f2016-05-03 17:11:42 +0100241#define flush_cpu_data(_m) flush_dcache_range((uintptr_t) \
Joel Hutton43a4d572017-10-20 10:31:14 +0100242 &(_cpu_data()->_m), \
243 sizeof(((cpu_data_t *)0)->_m))
Soby Mathew24ab34f2016-05-03 17:11:42 +0100244#define inv_cpu_data(_m) inv_dcache_range((uintptr_t) \
Joel Hutton43a4d572017-10-20 10:31:14 +0100245 &(_cpu_data()->_m), \
246 sizeof(((cpu_data_t *)0)->_m))
Soby Mathew7d861ea2014-11-18 10:14:14 +0000247#define flush_cpu_data_by_index(_ix, _m) \
Soby Mathewa0fedc42016-06-16 14:52:04 +0100248 flush_dcache_range((uintptr_t) \
Soby Mathew7d861ea2014-11-18 10:14:14 +0000249 &(_cpu_data_by_index(_ix)->_m), \
Joel Hutton43a4d572017-10-20 10:31:14 +0100250 sizeof(((cpu_data_t *)0)->_m))
Achin Guptae4b9fa42014-07-25 14:47:05 +0100251
Andrew Thoelke8c28fe02014-06-02 11:40:35 +0100252
Julius Werner53456fc2019-07-09 13:49:11 -0700253#endif /* __ASSEMBLER__ */
Antonio Nino Diaz9fe40fd2018-10-25 17:11:02 +0100254#endif /* CPU_DATA_H */