blob: 2ccf7a377e7b4d15befaebe64dab86dbf811b578 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +01002 * Copyright (c) 2018-2023, 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
13 * EL2: MAIR, CPTR, TTBR0, TCR, VBAR, SCTLR, HCR
Alexei Fedorov719714f2019-10-03 10:57:53 +010014 * APIAKeyLo_EL1 and APIAKeyHi_EL1 (if enabled).
Sandrine Bailleux49180a82019-03-07 16:35:48 +010015 */
Alexei Fedorov719714f2019-10-03 10:57:53 +010016#if ENABLE_PAUTH
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010017#define NR_CTX_REGS 10
Alexei Fedorov719714f2019-10-03 10:57:53 +010018#else
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010019#define NR_CTX_REGS 8
Alexei Fedorov719714f2019-10-03 10:57:53 +010020#endif
Sandrine Bailleux49180a82019-03-07 16:35:48 +010021
22/* Offsets of the fields in the context structure. Needed by asm code. */
Alexei Fedorov719714f2019-10-03 10:57:53 +010023#define SUSPEND_CTX_MAIR_OFFSET 0
24#define SUSPEND_CTX_TTBR0_OFFSET 16
25#define SUSPEND_CTX_VBAR_OFFSET 32
Arunachalam Ganapathy92f18682023-09-02 01:41:28 +010026#define SUSPEND_CTX_HCR_OFFSET 48
27#define SUSPEND_CTX_APIAKEY_OFFSET 64
Alexei Fedorov719714f2019-10-03 10:57:53 +010028
Sandrine Bailleux49180a82019-03-07 16:35:48 +010029#define SUSPEND_CTX_SP_OFFSET (8 * NR_CTX_REGS)
30#define SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET (SUSPEND_CTX_SP_OFFSET + 8)
31
32/*
33 * Size of the context structure.
34 * +8 because of the padding bytes inserted for alignment constraint.
35 */
36#define SUSPEND_CTX_SZ (SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET + 8)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020037
38#ifndef __ASSEMBLY__
39#include <cassert.h>
40#include <power_management.h>
41#include <stdint.h>
42#include <string.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020043
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020044/*
Sandrine Bailleux49180a82019-03-07 16:35:48 +010045 * Architectural context to be saved/restored when entering/exiting suspend
46 * mode.
47 *
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020048 * It must be 16-byte aligned since it is allocated on the stack, which must be
49 * 16-byte aligned on ARMv8 (AArch64). Even though the alignment requirement
50 * is not present in AArch32, we use the same alignment and register width as
51 * it allows the same structure to be reused for AArch32.
52 */
53typedef struct tftf_suspend_context {
54 uint64_t arch_ctx_regs[NR_CTX_REGS];
55 uint64_t stack_pointer;
Sandrine Bailleux49180a82019-03-07 16:35:48 +010056
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020057 /*
58 * Whether the system context is saved and and needs to be restored.
59 * Note that the system context itself is not saved in this structure.
60 */
61 unsigned int save_system_context;
62} __aligned(16) tftf_suspend_ctx_t;
63
64/*
Sandrine Bailleux49180a82019-03-07 16:35:48 +010065 * Ensure consistent view of the context structure layout across asm and C
66 * code.
67 */
68CASSERT(SUSPEND_CTX_SZ == sizeof(tftf_suspend_ctx_t),
69 assert_suspend_context_size_mismatch);
70
71CASSERT(SUSPEND_CTX_SP_OFFSET ==
72 __builtin_offsetof(tftf_suspend_ctx_t, stack_pointer),
73 assert_stack_pointer_location_mismatch_in_suspend_ctx);
74
75CASSERT(SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET ==
76 __builtin_offsetof(tftf_suspend_ctx_t, save_system_context),
77 assert_save_sys_ctx_mismatch_in_suspend_ctx);
78
79/*
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020080 * Saves callee save registers on the stack
81 * Allocate space on stack for CPU context regs
82 * Enters suspend by calling tftf_enter_suspend.
83 * power state: PSCI power state to be sent via SMC
84 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
85 */
86unsigned int __tftf_suspend(const suspend_info_t *power_state);
87
88/*
89 * Saves the architecture context of CPU in the memory
90 * tftf_suspend_context: Pointer to the location for saving the context
91 */
92void __tftf_save_arch_context(struct tftf_suspend_context *ctx);
93
94/*
95 * Calls __tftf_save_arch_context to saves arch context of cpu to the memory
96 * pointed by ctx
97 * Enters suspend by calling the SMC
98 * power state: PSCI power state to be sent via SMC
99 * ctx: Pointer to the location where suspend context can be stored
100 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
101 */
102int32_t tftf_enter_suspend(const suspend_info_t *power_state,
103 tftf_suspend_ctx_t *ctx);
104
105/*
106 * Invokes the appropriate driver functions in the TFTF framework
107 * to save their context prior to a system suspend.
108 */
109void tftf_save_system_ctx(tftf_suspend_ctx_t *ctx);
110
111/*
112 * Invokes the appropriate driver functions in the TFTF framework
113 * to restore their context on wake-up from system suspend.
114 */
115void tftf_restore_system_ctx(tftf_suspend_ctx_t *ctx);
116
117/*
118 * Restores the CPU arch context and callee registers from the location pointed
119 * by X0(context ID).
120 * Returns: PSCI_E_SUCCESS
121 */
122unsigned int __tftf_cpu_resume_ep(void);
123
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200124#endif /* __ASSEMBLY__ */
125
126#endif /* __SUSPEND_PRIV_H__ */