blob: debd84f9407c883399d66ba98c02ff13d6d77278 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Jing Hane538be62024-04-17 13:39:10 +00002 * Copyright (c) 2018-2024, 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
28#define SUSPEND_CTX_APIAKEY_OFFSET 64
Alexei Fedorov719714f2019-10-03 10:57:53 +010029
Sandrine Bailleux49180a82019-03-07 16:35:48 +010030#define SUSPEND_CTX_SP_OFFSET (8 * NR_CTX_REGS)
31#define SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET (SUSPEND_CTX_SP_OFFSET + 8)
32
33/*
34 * Size of the context structure.
35 * +8 because of the padding bytes inserted for alignment constraint.
36 */
37#define SUSPEND_CTX_SZ (SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET + 8)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020038
39#ifndef __ASSEMBLY__
40#include <cassert.h>
41#include <power_management.h>
42#include <stdint.h>
43#include <string.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020044
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020045/*
Sandrine Bailleux49180a82019-03-07 16:35:48 +010046 * Architectural context to be saved/restored when entering/exiting suspend
47 * mode.
48 *
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020049 * It must be 16-byte aligned since it is allocated on the stack, which must be
50 * 16-byte aligned on ARMv8 (AArch64). Even though the alignment requirement
51 * is not present in AArch32, we use the same alignment and register width as
52 * it allows the same structure to be reused for AArch32.
53 */
54typedef struct tftf_suspend_context {
55 uint64_t arch_ctx_regs[NR_CTX_REGS];
56 uint64_t stack_pointer;
Sandrine Bailleux49180a82019-03-07 16:35:48 +010057
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020058 /*
59 * Whether the system context is saved and and needs to be restored.
60 * Note that the system context itself is not saved in this structure.
61 */
62 unsigned int save_system_context;
63} __aligned(16) tftf_suspend_ctx_t;
64
65/*
Sandrine Bailleux49180a82019-03-07 16:35:48 +010066 * Ensure consistent view of the context structure layout across asm and C
67 * code.
68 */
69CASSERT(SUSPEND_CTX_SZ == sizeof(tftf_suspend_ctx_t),
70 assert_suspend_context_size_mismatch);
71
72CASSERT(SUSPEND_CTX_SP_OFFSET ==
73 __builtin_offsetof(tftf_suspend_ctx_t, stack_pointer),
74 assert_stack_pointer_location_mismatch_in_suspend_ctx);
75
76CASSERT(SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET ==
77 __builtin_offsetof(tftf_suspend_ctx_t, save_system_context),
78 assert_save_sys_ctx_mismatch_in_suspend_ctx);
79
80/*
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020081 * Saves callee save registers on the stack
82 * Allocate space on stack for CPU context regs
83 * Enters suspend by calling tftf_enter_suspend.
84 * power state: PSCI power state to be sent via SMC
85 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
86 */
87unsigned int __tftf_suspend(const suspend_info_t *power_state);
88
89/*
90 * Saves the architecture context of CPU in the memory
91 * tftf_suspend_context: Pointer to the location for saving the context
92 */
93void __tftf_save_arch_context(struct tftf_suspend_context *ctx);
94
95/*
96 * Calls __tftf_save_arch_context to saves arch context of cpu to the memory
97 * pointed by ctx
98 * Enters suspend by calling the SMC
99 * power state: PSCI power state to be sent via SMC
100 * ctx: Pointer to the location where suspend context can be stored
101 * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS
102 */
103int32_t tftf_enter_suspend(const suspend_info_t *power_state,
104 tftf_suspend_ctx_t *ctx);
105
106/*
107 * Invokes the appropriate driver functions in the TFTF framework
108 * to save their context prior to a system suspend.
109 */
110void tftf_save_system_ctx(tftf_suspend_ctx_t *ctx);
111
112/*
113 * Invokes the appropriate driver functions in the TFTF framework
114 * to restore their context on wake-up from system suspend.
115 */
116void tftf_restore_system_ctx(tftf_suspend_ctx_t *ctx);
117
118/*
119 * Restores the CPU arch context and callee registers from the location pointed
120 * by X0(context ID).
121 * Returns: PSCI_E_SUCCESS
122 */
123unsigned int __tftf_cpu_resume_ep(void);
124
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200125#endif /* __ASSEMBLY__ */
126
127#endif /* __SUSPEND_PRIV_H__ */