Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 1 | /* |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 2 | * Copyright (c) 2014-2021, Arm Limited and Contributors. All rights reserved. |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | 9fe40fd | 2018-10-25 17:11:02 +0100 | [diff] [blame] | 7 | #ifndef CPU_DATA_H |
| 8 | #define CPU_DATA_H |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 9 | |
Etienne Carriere | 97ad6ce | 2017-09-01 10:22:20 +0200 | [diff] [blame] | 10 | #include <platform_def.h> /* CACHE_WRITEBACK_GRANULE required */ |
| 11 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <bl31/ehf.h> |
| 13 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 14 | /* Size of psci_cpu_data structure */ |
| 15 | #define PSCI_CPU_DATA_SIZE 12 |
| 16 | |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 17 | #ifdef __aarch64__ |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 18 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 19 | /* 8-bytes aligned size of psci_cpu_data structure */ |
| 20 | #define PSCI_CPU_DATA_SIZE_ALIGNED ((PSCI_CPU_DATA_SIZE + 7) & ~7) |
| 21 | |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 22 | #if ENABLE_RME |
| 23 | /* Size of cpu_context array */ |
| 24 | #define CPU_DATA_CONTEXT_NUM 3 |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 25 | /* Offset of cpu_ops_ptr, size 8 bytes */ |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 26 | #define CPU_DATA_CPU_OPS_PTR 0x18 |
| 27 | #else /* ENABLE_RME */ |
| 28 | #define CPU_DATA_CONTEXT_NUM 2 |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 29 | #define CPU_DATA_CPU_OPS_PTR 0x10 |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 30 | #endif /* ENABLE_RME */ |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 31 | |
| 32 | #if ENABLE_PAUTH |
| 33 | /* 8-bytes aligned offset of apiakey[2], size 16 bytes */ |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 34 | #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 Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 41 | |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 42 | /* need enough space in crash buffer to save 8 registers */ |
| 43 | #define CPU_DATA_CRASH_BUF_SIZE 64 |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 44 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 45 | #else /* !__aarch64__ */ |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 46 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 47 | #if CRASH_REPORTING |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 48 | #error "Crash reporting is not supported in AArch32" |
| 49 | #endif |
| 50 | #define CPU_DATA_CPU_OPS_PTR 0x0 |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 51 | #define CPU_DATA_CRASH_BUF_OFFSET (0x4 + PSCI_CPU_DATA_SIZE) |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 52 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 53 | #endif /* __aarch64__ */ |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 54 | |
| 55 | #if CRASH_REPORTING |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 56 | #define CPU_DATA_CRASH_BUF_END (CPU_DATA_CRASH_BUF_OFFSET + \ |
| 57 | CPU_DATA_CRASH_BUF_SIZE) |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 58 | #else |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 59 | #define CPU_DATA_CRASH_BUF_END CPU_DATA_CRASH_BUF_OFFSET |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 60 | #endif |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 61 | |
Etienne Carriere | 97ad6ce | 2017-09-01 10:22:20 +0200 | [diff] [blame] | 62 | /* cpu_data size is the data size rounded up to the platform cache line size */ |
| 63 | #define CPU_DATA_SIZE (((CPU_DATA_CRASH_BUF_END + \ |
| 64 | CACHE_WRITEBACK_GRANULE - 1) / \ |
| 65 | CACHE_WRITEBACK_GRANULE) * \ |
| 66 | CACHE_WRITEBACK_GRANULE) |
| 67 | |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 68 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 69 | /* Temporary space to store PMF timestamps from assembly code */ |
| 70 | #define CPU_DATA_PMF_TS_COUNT 1 |
| 71 | #define CPU_DATA_PMF_TS0_OFFSET CPU_DATA_CRASH_BUF_END |
| 72 | #define CPU_DATA_PMF_TS0_IDX 0 |
| 73 | #endif |
| 74 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 75 | #ifndef __ASSEMBLER__ |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 76 | |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 77 | #include <assert.h> |
| 78 | #include <stdint.h> |
| 79 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 80 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 81 | #include <lib/cassert.h> |
| 82 | #include <lib/psci/psci.h> |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 83 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 84 | #include <platform_def.h> |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 85 | |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 86 | /* Offsets for the cpu_data structure */ |
| 87 | #define CPU_DATA_PSCI_LOCK_OFFSET __builtin_offsetof\ |
| 88 | (cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info) |
| 89 | |
| 90 | #if PLAT_PCPU_DATA_SIZE |
| 91 | #define CPU_DATA_PLAT_PCPU_OFFSET __builtin_offsetof\ |
| 92 | (cpu_data_t, platform_cpu_data) |
| 93 | #endif |
| 94 | |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 95 | typedef enum context_pas { |
| 96 | CPU_CONTEXT_SECURE = 0, |
| 97 | CPU_CONTEXT_NS, |
| 98 | #if ENABLE_RME |
| 99 | CPU_CONTEXT_REALM, |
| 100 | #endif |
| 101 | CPU_CONTEXT_NUM |
| 102 | } context_pas_t; |
| 103 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 104 | /******************************************************************************* |
| 105 | * Function & variable prototypes |
| 106 | ******************************************************************************/ |
| 107 | |
| 108 | /******************************************************************************* |
| 109 | * Cache of frequently used per-cpu data: |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 110 | * Pointers to non-secure, realm, and secure security state contexts |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 111 | * Address of the crash stack |
| 112 | * It is aligned to the cache line boundary to allow efficient concurrent |
| 113 | * manipulation of these pointers on different cpus |
| 114 | * |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 115 | * The data structure and the _cpu_data accessors should not be used directly |
| 116 | * by components that have per-cpu members. The member access macros should be |
| 117 | * used for this. |
| 118 | ******************************************************************************/ |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 119 | typedef struct cpu_data { |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 120 | #ifdef __aarch64__ |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 121 | void *cpu_context[CPU_DATA_CONTEXT_NUM]; |
| 122 | #endif /* __aarch64__ */ |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 123 | uintptr_t cpu_ops_ptr; |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 124 | struct psci_cpu_data psci_svc_cpu_data; |
| 125 | #if ENABLE_PAUTH |
| 126 | uint64_t apiakey[2]; |
| 127 | #endif |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 128 | #if CRASH_REPORTING |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 129 | u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3]; |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 130 | #endif |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 131 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 132 | uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT]; |
| 133 | #endif |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 134 | #if PLAT_PCPU_DATA_SIZE |
| 135 | uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE]; |
| 136 | #endif |
Jeenu Viswambharan | 10a6727 | 2017-09-22 08:32:10 +0100 | [diff] [blame] | 137 | #if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING |
| 138 | pe_exc_data_t ehf_data; |
| 139 | #endif |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 140 | } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t; |
| 141 | |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 142 | extern cpu_data_t percpu_data[PLATFORM_CORE_COUNT]; |
| 143 | |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 144 | #ifdef __aarch64__ |
| 145 | CASSERT(CPU_DATA_CONTEXT_NUM == CPU_CONTEXT_NUM, |
| 146 | assert_cpu_data_context_num_mismatch); |
| 147 | #endif |
| 148 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 149 | #if ENABLE_PAUTH |
| 150 | CASSERT(CPU_DATA_APIAKEY_OFFSET == __builtin_offsetof |
| 151 | (cpu_data_t, apiakey), |
Olivier Deprez | f57491e | 2021-08-19 11:36:26 +0200 | [diff] [blame] | 152 | assert_cpu_data_pauth_stack_offset_mismatch); |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 153 | #endif |
| 154 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 155 | #if CRASH_REPORTING |
| 156 | /* verify assembler offsets match data structures */ |
| 157 | CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof |
| 158 | (cpu_data_t, crash_buf), |
| 159 | assert_cpu_data_crash_stack_offset_mismatch); |
| 160 | #endif |
| 161 | |
Etienne Carriere | 97ad6ce | 2017-09-01 10:22:20 +0200 | [diff] [blame] | 162 | CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t), |
| 163 | assert_cpu_data_size_mismatch); |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 164 | |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 165 | CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof |
| 166 | (cpu_data_t, cpu_ops_ptr), |
| 167 | assert_cpu_data_cpu_ops_ptr_offset_mismatch); |
| 168 | |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 169 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 170 | CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof |
| 171 | (cpu_data_t, cpu_data_pmf_ts[0]), |
| 172 | assert_cpu_data_pmf_ts0_offset_mismatch); |
| 173 | #endif |
| 174 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 175 | struct cpu_data *_cpu_data_by_index(uint32_t cpu_index); |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 176 | |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 177 | #ifdef __aarch64__ |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 178 | /* Return the cpu_data structure for the current CPU. */ |
| 179 | static inline struct cpu_data *_cpu_data(void) |
| 180 | { |
| 181 | return (cpu_data_t *)read_tpidr_el3(); |
| 182 | } |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 183 | #else |
| 184 | struct cpu_data *_cpu_data(void); |
| 185 | #endif |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 186 | |
Zelalem Aweke | b6301e6 | 2021-07-09 17:54:30 -0500 | [diff] [blame] | 187 | /* |
| 188 | * Returns the index of the cpu_context array for the given security state. |
| 189 | * All accesses to cpu_context should be through this helper to make sure |
| 190 | * an access is not out-of-bounds. The function assumes security_state is |
| 191 | * valid. |
| 192 | */ |
| 193 | static inline context_pas_t get_cpu_context_index(uint32_t security_state) |
| 194 | { |
| 195 | if (security_state == SECURE) { |
| 196 | return CPU_CONTEXT_SECURE; |
| 197 | } else { |
| 198 | #if ENABLE_RME |
| 199 | if (security_state == NON_SECURE) { |
| 200 | return CPU_CONTEXT_NS; |
| 201 | } else { |
| 202 | assert(security_state == REALM); |
| 203 | return CPU_CONTEXT_REALM; |
| 204 | } |
| 205 | #else |
| 206 | assert(security_state == NON_SECURE); |
| 207 | return CPU_CONTEXT_NS; |
| 208 | #endif |
| 209 | } |
| 210 | } |
| 211 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 212 | /************************************************************************** |
| 213 | * APIs for initialising and accessing per-cpu data |
| 214 | *************************************************************************/ |
| 215 | |
| 216 | void init_cpu_data_ptr(void); |
Vikram Kanigiri | 9b38fc8 | 2015-01-29 18:27:38 +0000 | [diff] [blame] | 217 | void init_cpu_ops(void); |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 218 | |
| 219 | #define get_cpu_data(_m) _cpu_data()->_m |
Antonio Nino Diaz | 864ca6f | 2018-10-31 15:25:35 +0000 | [diff] [blame] | 220 | #define set_cpu_data(_m, _v) _cpu_data()->_m = (_v) |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 221 | #define get_cpu_data_by_index(_ix, _m) _cpu_data_by_index(_ix)->_m |
Antonio Nino Diaz | 864ca6f | 2018-10-31 15:25:35 +0000 | [diff] [blame] | 222 | #define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = (_v) |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 223 | /* ((cpu_data_t *)0)->_m is a dummy to get the sizeof the struct member _m */ |
Soby Mathew | 24ab34f | 2016-05-03 17:11:42 +0100 | [diff] [blame] | 224 | #define flush_cpu_data(_m) flush_dcache_range((uintptr_t) \ |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 225 | &(_cpu_data()->_m), \ |
| 226 | sizeof(((cpu_data_t *)0)->_m)) |
Soby Mathew | 24ab34f | 2016-05-03 17:11:42 +0100 | [diff] [blame] | 227 | #define inv_cpu_data(_m) inv_dcache_range((uintptr_t) \ |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 228 | &(_cpu_data()->_m), \ |
| 229 | sizeof(((cpu_data_t *)0)->_m)) |
Soby Mathew | 7d861ea | 2014-11-18 10:14:14 +0000 | [diff] [blame] | 230 | #define flush_cpu_data_by_index(_ix, _m) \ |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 231 | flush_dcache_range((uintptr_t) \ |
Soby Mathew | 7d861ea | 2014-11-18 10:14:14 +0000 | [diff] [blame] | 232 | &(_cpu_data_by_index(_ix)->_m), \ |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 233 | sizeof(((cpu_data_t *)0)->_m)) |
Achin Gupta | e4b9fa4 | 2014-07-25 14:47:05 +0100 | [diff] [blame] | 234 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 235 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 236 | #endif /* __ASSEMBLER__ */ |
Antonio Nino Diaz | 9fe40fd | 2018-10-25 17:11:02 +0100 | [diff] [blame] | 237 | #endif /* CPU_DATA_H */ |