1532ed618SSoby Mathew /* 2532ed618SSoby Mathew * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 7532ed618SSoby Mathew #ifndef __CPU_DATA_H__ 8532ed618SSoby Mathew #define __CPU_DATA_H__ 9532ed618SSoby Mathew 1086606eb5SEtienne Carriere #include <platform_def.h> /* CACHE_WRITEBACK_GRANULE required */ 1186606eb5SEtienne Carriere 12e33b78a6SSoby Mathew #ifdef AARCH32 13e33b78a6SSoby Mathew 14e33b78a6SSoby Mathew #if CRASH_REPORTING 15e33b78a6SSoby Mathew #error "Crash reporting is not supported in AArch32" 16e33b78a6SSoby Mathew #endif 17e33b78a6SSoby Mathew #define CPU_DATA_CPU_OPS_PTR 0x0 1886606eb5SEtienne Carriere #define CPU_DATA_CRASH_BUF_OFFSET 0x4 19e33b78a6SSoby Mathew 20e33b78a6SSoby Mathew #else /* AARCH32 */ 21e33b78a6SSoby Mathew 22532ed618SSoby Mathew /* Offsets for the cpu_data structure */ 23532ed618SSoby Mathew #define CPU_DATA_CRASH_BUF_OFFSET 0x18 24e33b78a6SSoby Mathew /* need enough space in crash buffer to save 8 registers */ 25e33b78a6SSoby Mathew #define CPU_DATA_CRASH_BUF_SIZE 64 26e33b78a6SSoby Mathew #define CPU_DATA_CPU_OPS_PTR 0x10 27e33b78a6SSoby Mathew 28e33b78a6SSoby Mathew #endif /* AARCH32 */ 29e33b78a6SSoby Mathew 30532ed618SSoby Mathew #if CRASH_REPORTING 31872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END (CPU_DATA_CRASH_BUF_OFFSET + \ 32872be88aSdp-arm CPU_DATA_CRASH_BUF_SIZE) 33532ed618SSoby Mathew #else 34872be88aSdp-arm #define CPU_DATA_CRASH_BUF_END CPU_DATA_CRASH_BUF_OFFSET 35872be88aSdp-arm #endif 36872be88aSdp-arm 3786606eb5SEtienne Carriere /* cpu_data size is the data size rounded up to the platform cache line size */ 3886606eb5SEtienne Carriere #define CPU_DATA_SIZE (((CPU_DATA_CRASH_BUF_END + \ 3986606eb5SEtienne Carriere CACHE_WRITEBACK_GRANULE - 1) / \ 4086606eb5SEtienne Carriere CACHE_WRITEBACK_GRANULE) * \ 4186606eb5SEtienne Carriere CACHE_WRITEBACK_GRANULE) 4286606eb5SEtienne Carriere 43872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 44872be88aSdp-arm /* Temporary space to store PMF timestamps from assembly code */ 45872be88aSdp-arm #define CPU_DATA_PMF_TS_COUNT 1 46872be88aSdp-arm #define CPU_DATA_PMF_TS0_OFFSET CPU_DATA_CRASH_BUF_END 47872be88aSdp-arm #define CPU_DATA_PMF_TS0_IDX 0 48532ed618SSoby Mathew #endif 49532ed618SSoby Mathew 50532ed618SSoby Mathew #ifndef __ASSEMBLY__ 51532ed618SSoby Mathew 52532ed618SSoby Mathew #include <arch_helpers.h> 53532ed618SSoby Mathew #include <cassert.h> 54532ed618SSoby Mathew #include <platform_def.h> 55532ed618SSoby Mathew #include <psci.h> 56532ed618SSoby Mathew #include <stdint.h> 57532ed618SSoby Mathew 58532ed618SSoby Mathew /* Offsets for the cpu_data structure */ 59532ed618SSoby Mathew #define CPU_DATA_PSCI_LOCK_OFFSET __builtin_offsetof\ 60532ed618SSoby Mathew (cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info) 61532ed618SSoby Mathew 62532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE 63532ed618SSoby Mathew #define CPU_DATA_PLAT_PCPU_OFFSET __builtin_offsetof\ 64532ed618SSoby Mathew (cpu_data_t, platform_cpu_data) 65532ed618SSoby Mathew #endif 66532ed618SSoby Mathew 67532ed618SSoby Mathew /******************************************************************************* 68532ed618SSoby Mathew * Function & variable prototypes 69532ed618SSoby Mathew ******************************************************************************/ 70532ed618SSoby Mathew 71532ed618SSoby Mathew /******************************************************************************* 72532ed618SSoby Mathew * Cache of frequently used per-cpu data: 73532ed618SSoby Mathew * Pointers to non-secure and secure security state contexts 74532ed618SSoby Mathew * Address of the crash stack 75532ed618SSoby Mathew * It is aligned to the cache line boundary to allow efficient concurrent 76532ed618SSoby Mathew * manipulation of these pointers on different cpus 77532ed618SSoby Mathew * 78532ed618SSoby Mathew * TODO: Add other commonly used variables to this (tf_issues#90) 79532ed618SSoby Mathew * 80532ed618SSoby Mathew * The data structure and the _cpu_data accessors should not be used directly 81532ed618SSoby Mathew * by components that have per-cpu members. The member access macros should be 82532ed618SSoby Mathew * used for this. 83532ed618SSoby Mathew ******************************************************************************/ 84532ed618SSoby Mathew typedef struct cpu_data { 85e33b78a6SSoby Mathew #ifndef AARCH32 86532ed618SSoby Mathew void *cpu_context[2]; 87e33b78a6SSoby Mathew #endif 88532ed618SSoby Mathew uintptr_t cpu_ops_ptr; 89532ed618SSoby Mathew #if CRASH_REPORTING 90532ed618SSoby Mathew u_register_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3]; 91532ed618SSoby Mathew #endif 92872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 93872be88aSdp-arm uint64_t cpu_data_pmf_ts[CPU_DATA_PMF_TS_COUNT]; 94872be88aSdp-arm #endif 95532ed618SSoby Mathew struct psci_cpu_data psci_svc_cpu_data; 96532ed618SSoby Mathew #if PLAT_PCPU_DATA_SIZE 97532ed618SSoby Mathew uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE]; 98532ed618SSoby Mathew #endif 99532ed618SSoby Mathew } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t; 100532ed618SSoby Mathew 101532ed618SSoby Mathew #if CRASH_REPORTING 102532ed618SSoby Mathew /* verify assembler offsets match data structures */ 103532ed618SSoby Mathew CASSERT(CPU_DATA_CRASH_BUF_OFFSET == __builtin_offsetof 104532ed618SSoby Mathew (cpu_data_t, crash_buf), 105532ed618SSoby Mathew assert_cpu_data_crash_stack_offset_mismatch); 106532ed618SSoby Mathew #endif 107532ed618SSoby Mathew 10886606eb5SEtienne Carriere CASSERT(CPU_DATA_SIZE == sizeof(cpu_data_t), 10986606eb5SEtienne Carriere assert_cpu_data_size_mismatch); 110532ed618SSoby Mathew 111532ed618SSoby Mathew CASSERT(CPU_DATA_CPU_OPS_PTR == __builtin_offsetof 112532ed618SSoby Mathew (cpu_data_t, cpu_ops_ptr), 113532ed618SSoby Mathew assert_cpu_data_cpu_ops_ptr_offset_mismatch); 114532ed618SSoby Mathew 115872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 116872be88aSdp-arm CASSERT(CPU_DATA_PMF_TS0_OFFSET == __builtin_offsetof 117872be88aSdp-arm (cpu_data_t, cpu_data_pmf_ts[0]), 118872be88aSdp-arm assert_cpu_data_pmf_ts0_offset_mismatch); 119872be88aSdp-arm #endif 120872be88aSdp-arm 121532ed618SSoby Mathew struct cpu_data *_cpu_data_by_index(uint32_t cpu_index); 122532ed618SSoby Mathew 123e33b78a6SSoby Mathew #ifndef AARCH32 124532ed618SSoby Mathew /* Return the cpu_data structure for the current CPU. */ 125532ed618SSoby Mathew static inline struct cpu_data *_cpu_data(void) 126532ed618SSoby Mathew { 127532ed618SSoby Mathew return (cpu_data_t *)read_tpidr_el3(); 128532ed618SSoby Mathew } 129e33b78a6SSoby Mathew #else 130e33b78a6SSoby Mathew struct cpu_data *_cpu_data(void); 131e33b78a6SSoby Mathew #endif 132532ed618SSoby Mathew 133532ed618SSoby Mathew /************************************************************************** 134532ed618SSoby Mathew * APIs for initialising and accessing per-cpu data 135532ed618SSoby Mathew *************************************************************************/ 136532ed618SSoby Mathew 137532ed618SSoby Mathew void init_cpu_data_ptr(void); 138532ed618SSoby Mathew void init_cpu_ops(void); 139532ed618SSoby Mathew 140532ed618SSoby Mathew #define get_cpu_data(_m) _cpu_data()->_m 141532ed618SSoby Mathew #define set_cpu_data(_m, _v) _cpu_data()->_m = _v 142532ed618SSoby Mathew #define get_cpu_data_by_index(_ix, _m) _cpu_data_by_index(_ix)->_m 143532ed618SSoby Mathew #define set_cpu_data_by_index(_ix, _m, _v) _cpu_data_by_index(_ix)->_m = _v 144*2614ea3eSJoel Hutton /* ((cpu_data_t *)0)->_m is a dummy to get the sizeof the struct member _m */ 145532ed618SSoby Mathew #define flush_cpu_data(_m) flush_dcache_range((uintptr_t) \ 146532ed618SSoby Mathew &(_cpu_data()->_m), \ 147*2614ea3eSJoel Hutton sizeof(((cpu_data_t *)0)->_m)) 148532ed618SSoby Mathew #define inv_cpu_data(_m) inv_dcache_range((uintptr_t) \ 149532ed618SSoby Mathew &(_cpu_data()->_m), \ 150*2614ea3eSJoel Hutton sizeof(((cpu_data_t *)0)->_m)) 151532ed618SSoby Mathew #define flush_cpu_data_by_index(_ix, _m) \ 152532ed618SSoby Mathew flush_dcache_range((uintptr_t) \ 153532ed618SSoby Mathew &(_cpu_data_by_index(_ix)->_m), \ 154*2614ea3eSJoel Hutton sizeof(((cpu_data_t *)0)->_m)) 155532ed618SSoby Mathew 156532ed618SSoby Mathew 157532ed618SSoby Mathew #endif /* __ASSEMBLY__ */ 158532ed618SSoby Mathew #endif /* __CPU_DATA_H__ */ 159