blob: c91153d17c1bf99958f6283a00fb679a2c15c723 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Boyan Karatotevaa22f412025-05-21 13:25:40 +01002 * Copyright (c) 2018-2025, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __SUSPEND_PRIV_H__
8#define __SUSPEND_PRIV_H__
9
Sandrine Bailleux49180a82019-03-07 16:35:48 +010010/*
11 * Number of system registers we need to save/restore across a CPU suspend:
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010012 * EL1: MAIR, CPACR, TTBR0, TCR, VBAR, SCTLR
Jing Hane538be62024-04-17 13:39:10 +000013 * EL2: MAIR, CPTR, TTBR0, TCR, VBAR, SCTLR, HCR, SMCR
Alexei Fedorov719714f2019-10-03 10:57:53 +010014 * APIAKeyLo_EL1 and APIAKeyHi_EL1 (if enabled).
Jing Hane538be62024-04-17 13:39:10 +000015 * On need basis other registers can be included in tftf_suspend_context.
Sandrine Bailleux49180a82019-03-07 16:35:48 +010016 */
Alexei Fedorov719714f2019-10-03 10:57:53 +010017#if ENABLE_PAUTH
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010018#define NR_CTX_REGS 10
Alexei Fedorov719714f2019-10-03 10:57:53 +010019#else
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010020#define NR_CTX_REGS 8
Alexei Fedorov719714f2019-10-03 10:57:53 +010021#endif
Sandrine Bailleux49180a82019-03-07 16:35:48 +010022
23/* Offsets of the fields in the context structure. Needed by asm code. */
Alexei Fedorov719714f2019-10-03 10:57:53 +010024#define SUSPEND_CTX_MAIR_OFFSET 0
25#define SUSPEND_CTX_TTBR0_OFFSET 16
26#define SUSPEND_CTX_VBAR_OFFSET 32
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010027#define SUSPEND_CTX_HCR_OFFSET 48
Boyan Karatotevaa22f412025-05-21 13:25:40 +010028#define SUSPEND_CTX_SMCR_OFFSET 56
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010029#define SUSPEND_CTX_APIAKEY_OFFSET 64
Alexei Fedorov719714f2019-10-03 10:57:53 +010030
Sandrine Bailleux49180a82019-03-07 16:35:48 +010031#define SUSPEND_CTX_SP_OFFSET (8 * NR_CTX_REGS)
32#define SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET (SUSPEND_CTX_SP_OFFSET + 8)
33
34/*
35 * Size of the context structure.
36 * +8 because of the padding bytes inserted for alignment constraint.
37 */
38#define SUSPEND_CTX_SZ (SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET + 8)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020039
40#ifndef __ASSEMBLY__
41#include <cassert.h>
42#include <power_management.h>
43#include <stdint.h>
44#include <string.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020045
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020046/*
Sandrine Bailleux49180a82019-03-07 16:35:48 +010047 * Architectural context to be saved/restored when entering/exiting suspend
48 * mode.
49 *
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020050 * It must be 16-byte aligned since it is allocated on the stack, which must be
51 * 16-byte aligned on ARMv8 (AArch64). Even though the alignment requirement
52 * is not present in AArch32, we use the same alignment and register width as
53 * it allows the same structure to be reused for AArch32.
54 */
55typedef struct tftf_suspend_context {
56 uint64_t arch_ctx_regs[NR_CTX_REGS];
57 uint64_t stack_pointer;
Sandrine Bailleux49180a82019-03-07 16:35:48 +010058
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020059 /*
60 * Whether the system context is saved and and needs to be restored.
61 * Note that the system context itself is not saved in this structure.
62 */
63 unsigned int save_system_context;
64} __aligned(16) tftf_suspend_ctx_t;
65
66/*
Sandrine Bailleux49180a82019-03-07 16:35:48 +010067 * Ensure consistent view of the context structure layout across asm and C
68 * code.
69 */
70CASSERT(SUSPEND_CTX_SZ == sizeof(tftf_suspend_ctx_t),
71 assert_suspend_context_size_mismatch);
72
73CASSERT(SUSPEND_CTX_SP_OFFSET ==
74 __builtin_offsetof(tftf_suspend_ctx_t, stack_pointer),
75 assert_stack_pointer_location_mismatch_in_suspend_ctx);
76
77CASSERT(SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET ==
78 __builtin_offsetof(tftf_suspend_ctx_t, save_system_context),
79 assert_save_sys_ctx_mismatch_in_suspend_ctx);
80
81/*
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020082 * Saves callee save registers on the stack
83 * Allocate space on stack for CPU context regs
84 * Enters suspend by calling tftf_enter_suspend.
85 * power state: PSCI power state to be sent via SMC
86 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
87 */
88unsigned int __tftf_suspend(const suspend_info_t *power_state);
89
90/*
91 * Saves the architecture context of CPU in the memory
92 * tftf_suspend_context: Pointer to the location for saving the context
93 */
94void __tftf_save_arch_context(struct tftf_suspend_context *ctx);
95
96/*
97 * Calls __tftf_save_arch_context to saves arch context of cpu to the memory
98 * pointed by ctx
99 * Enters suspend by calling the SMC
100 * power state: PSCI power state to be sent via SMC
101 * ctx: Pointer to the location where suspend context can be stored
102 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
103 */
104int32_t tftf_enter_suspend(const suspend_info_t *power_state,
105 tftf_suspend_ctx_t *ctx);
106
107/*
108 * Invokes the appropriate driver functions in the TFTF framework
109 * to save their context prior to a system suspend.
110 */
111void tftf_save_system_ctx(tftf_suspend_ctx_t *ctx);
112
113/*
114 * Invokes the appropriate driver functions in the TFTF framework
115 * to restore their context on wake-up from system suspend.
116 */
117void tftf_restore_system_ctx(tftf_suspend_ctx_t *ctx);
118
119/*
120 * Restores the CPU arch context and callee registers from the location pointed
121 * by X0(context ID).
122 * Returns: PSCI_E_SUCCESS
123 */
124unsigned int __tftf_cpu_resume_ep(void);
125
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200126#endif /* __ASSEMBLY__ */
127
128#endif /* __SUSPEND_PRIV_H__ */