Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 1 | /* |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2014-2019, 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 | |
| 22 | /* Offset of cpu_ops_ptr, size 8 bytes */ |
| 23 | #define CPU_DATA_CPU_OPS_PTR 0x10 |
| 24 | |
| 25 | #if ENABLE_PAUTH |
| 26 | /* 8-bytes aligned offset of apiakey[2], size 16 bytes */ |
| 27 | #define CPU_DATA_APIAKEY_OFFSET (0x18 + PSCI_CPU_DATA_SIZE_ALIGNED) |
| 28 | #define CPU_DATA_CRASH_BUF_OFFSET (CPU_DATA_APIAKEY_OFFSET + 0x10) |
| 29 | #else |
| 30 | #define CPU_DATA_CRASH_BUF_OFFSET (0x18 + PSCI_CPU_DATA_SIZE_ALIGNED) |
| 31 | #endif /* ENABLE_PAUTH */ |
| 32 | |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 33 | /* need enough space in crash buffer to save 8 registers */ |
| 34 | #define CPU_DATA_CRASH_BUF_SIZE 64 |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 35 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 36 | #else /* !__aarch64__ */ |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 37 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 38 | #if CRASH_REPORTING |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 39 | #error "Crash reporting is not supported in AArch32" |
| 40 | #endif |
| 41 | #define CPU_DATA_CPU_OPS_PTR 0x0 |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 42 | #define CPU_DATA_CRASH_BUF_OFFSET (0x4 + PSCI_CPU_DATA_SIZE) |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 43 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 44 | #endif /* __aarch64__ */ |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 45 | |
| 46 | #if CRASH_REPORTING |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 47 | #define CPU_DATA_CRASH_BUF_END (CPU_DATA_CRASH_BUF_OFFSET + \ |
| 48 | CPU_DATA_CRASH_BUF_SIZE) |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 49 | #else |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 50 | #define CPU_DATA_CRASH_BUF_END CPU_DATA_CRASH_BUF_OFFSET |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 51 | #endif |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 52 | |
Etienne Carriere | 97ad6ce | 2017-09-01 10:22:20 +0200 | [diff] [blame] | 53 | /* cpu_data size is the data size rounded up to the platform cache line size */ |
| 54 | #define CPU_DATA_SIZE (((CPU_DATA_CRASH_BUF_END + \ |
| 55 | CACHE_WRITEBACK_GRANULE - 1) / \ |
| 56 | CACHE_WRITEBACK_GRANULE) * \ |
| 57 | CACHE_WRITEBACK_GRANULE) |
| 58 | |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 59 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 60 | /* Temporary space to store PMF timestamps from assembly code */ |
| 61 | #define CPU_DATA_PMF_TS_COUNT 1 |
| 62 | #define CPU_DATA_PMF_TS0_OFFSET CPU_DATA_CRASH_BUF_END |
| 63 | #define CPU_DATA_PMF_TS0_IDX 0 |
| 64 | #endif |
| 65 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 66 | #ifndef __ASSEMBLER__ |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 67 | |
| 68 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 69 | #include <lib/cassert.h> |
| 70 | #include <lib/psci/psci.h> |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 71 | #include <platform_def.h> |
| 72 | #include <stdint.h> |
| 73 | |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 74 | /* Offsets for the cpu_data structure */ |
| 75 | #define CPU_DATA_PSCI_LOCK_OFFSET __builtin_offsetof\ |
| 76 | (cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info) |
| 77 | |
| 78 | #if PLAT_PCPU_DATA_SIZE |
| 79 | #define CPU_DATA_PLAT_PCPU_OFFSET __builtin_offsetof\ |
| 80 | (cpu_data_t, platform_cpu_data) |
| 81 | #endif |
| 82 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 83 | /******************************************************************************* |
| 84 | * Function & variable prototypes |
| 85 | ******************************************************************************/ |
| 86 | |
| 87 | /******************************************************************************* |
| 88 | * Cache of frequently used per-cpu data: |
Andrew Thoelke | c02dbd6 | 2014-06-02 10:00:25 +0100 | [diff] [blame] | 89 | * Pointers to non-secure and secure security state contexts |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 90 | * Address of the crash stack |
| 91 | * It is aligned to the cache line boundary to allow efficient concurrent |
| 92 | * manipulation of these pointers on different cpus |
| 93 | * |
| 94 | * TODO: Add other commonly used variables to this (tf_issues#90) |
| 95 | * |
| 96 | * The data structure and the _cpu_data accessors should not be used directly |
| 97 | * by components that have per-cpu members. The member access macros should be |
| 98 | * used for this. |
| 99 | ******************************************************************************/ |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 100 | typedef struct cpu_data { |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 101 | #ifdef __aarch64__ |
Andrew Thoelke | c02dbd6 | 2014-06-02 10:00:25 +0100 | [diff] [blame] | 102 | void *cpu_context[2]; |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 103 | #endif |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 104 | uintptr_t cpu_ops_ptr; |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 105 | struct psci_cpu_data psci_svc_cpu_data; |
| 106 | #if ENABLE_PAUTH |
| 107 | uint64_t apiakey[2]; |
| 108 | #endif |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 109 | #if CRASH_REPORTING |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 110 | u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3]; |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 111 | #endif |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 112 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 113 | uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT]; |
| 114 | #endif |
Soby Mathew | 523d633 | 2015-01-08 18:02:19 +0000 | [diff] [blame] | 115 | #if PLAT_PCPU_DATA_SIZE |
| 116 | uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE]; |
| 117 | #endif |
Jeenu Viswambharan | 10a6727 | 2017-09-22 08:32:10 +0100 | [diff] [blame] | 118 | #if defined(IMAGE_BL31) && EL3_EXCEPTION_HANDLING |
| 119 | pe_exc_data_t ehf_data; |
| 120 | #endif |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 121 | } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t; |
| 122 | |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 123 | extern cpu_data_t percpu_data[PLATFORM_CORE_COUNT]; |
| 124 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 125 | #if ENABLE_PAUTH |
| 126 | CASSERT(CPU_DATA_APIAKEY_OFFSET == __builtin_offsetof |
| 127 | (cpu_data_t, apiakey), |
| 128 | assert_cpu_data_crash_stack_offset_mismatch); |
| 129 | #endif |
| 130 | |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 131 | #if CRASH_REPORTING |
| 132 | /* verify assembler offsets match data structures */ |
| 133 | CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof |
| 134 | (cpu_data_t, crash_buf), |
| 135 | assert_cpu_data_crash_stack_offset_mismatch); |
| 136 | #endif |
| 137 | |
Etienne Carriere | 97ad6ce | 2017-09-01 10:22:20 +0200 | [diff] [blame] | 138 | CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t), |
| 139 | assert_cpu_data_size_mismatch); |
Soby Mathew | c1adbbc | 2014-06-25 10:07:40 +0100 | [diff] [blame] | 140 | |
Soby Mathew | c704cbc | 2014-08-14 11:33:56 +0100 | [diff] [blame] | 141 | CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof |
| 142 | (cpu_data_t, cpu_ops_ptr), |
| 143 | assert_cpu_data_cpu_ops_ptr_offset_mismatch); |
| 144 | |
dp-arm | 3cac786 | 2016-09-19 11:18:44 +0100 | [diff] [blame] | 145 | #if ENABLE_RUNTIME_INSTRUMENTATION |
| 146 | CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof |
| 147 | (cpu_data_t, cpu_data_pmf_ts[0]), |
| 148 | assert_cpu_data_pmf_ts0_offset_mismatch); |
| 149 | #endif |
| 150 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 151 | struct cpu_data *_cpu_data_by_index(uint32_t cpu_index); |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 152 | |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 153 | #ifdef __aarch64__ |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 154 | /* Return the cpu_data structure for the current CPU. */ |
| 155 | static inline struct cpu_data *_cpu_data(void) |
| 156 | { |
| 157 | return (cpu_data_t *)read_tpidr_el3(); |
| 158 | } |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 159 | #else |
| 160 | struct cpu_data *_cpu_data(void); |
| 161 | #endif |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 162 | |
| 163 | /************************************************************************** |
| 164 | * APIs for initialising and accessing per-cpu data |
| 165 | *************************************************************************/ |
| 166 | |
| 167 | void init_cpu_data_ptr(void); |
Vikram Kanigiri | 9b38fc8 | 2015-01-29 18:27:38 +0000 | [diff] [blame] | 168 | void init_cpu_ops(void); |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 169 | |
| 170 | #define get_cpu_data(_m) _cpu_data()->_m |
Antonio Nino Diaz | 864ca6f | 2018-10-31 15:25:35 +0000 | [diff] [blame] | 171 | #define set_cpu_data(_m, _v) _cpu_data()->_m = (_v) |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 172 | #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] | 173 | #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] | 174 | /* ((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] | 175 | #define flush_cpu_data(_m) flush_dcache_range((uintptr_t) \ |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 176 | &(_cpu_data()->_m), \ |
| 177 | sizeof(((cpu_data_t *)0)->_m)) |
Soby Mathew | 24ab34f | 2016-05-03 17:11:42 +0100 | [diff] [blame] | 178 | #define inv_cpu_data(_m) inv_dcache_range((uintptr_t) \ |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 179 | &(_cpu_data()->_m), \ |
| 180 | sizeof(((cpu_data_t *)0)->_m)) |
Soby Mathew | 7d861ea | 2014-11-18 10:14:14 +0000 | [diff] [blame] | 181 | #define flush_cpu_data_by_index(_ix, _m) \ |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 182 | flush_dcache_range((uintptr_t) \ |
Soby Mathew | 7d861ea | 2014-11-18 10:14:14 +0000 | [diff] [blame] | 183 | &(_cpu_data_by_index(_ix)->_m), \ |
Joel Hutton | 43a4d57 | 2017-10-20 10:31:14 +0100 | [diff] [blame] | 184 | sizeof(((cpu_data_t *)0)->_m)) |
Achin Gupta | e4b9fa4 | 2014-07-25 14:47:05 +0100 | [diff] [blame] | 185 | |
Andrew Thoelke | 8c28fe0 | 2014-06-02 11:40:35 +0100 | [diff] [blame] | 186 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 187 | #endif /* __ASSEMBLER__ */ |
Antonio Nino Diaz | 9fe40fd | 2018-10-25 17:11:02 +0100 | [diff] [blame] | 188 | #endif /* CPU_DATA_H */ |