Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Sandrine Bailleux | 49180a8 | 2019-03-07 16:35:48 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef __SUSPEND_PRIV_H__ |
| 8 | #define __SUSPEND_PRIV_H__ |
| 9 | |
Sandrine Bailleux | 49180a8 | 2019-03-07 16:35:48 +0100 | [diff] [blame^] | 10 | /* |
| 11 | * Number of system registers we need to save/restore across a CPU suspend: |
| 12 | * MAIR, CPACR_EL1/HCR_EL2, TTBR0, TCR, VBAR and SCTLR. |
| 13 | */ |
| 14 | #define NR_CTX_REGS 6 |
| 15 | |
| 16 | /* Offsets of the fields in the context structure. Needed by asm code. */ |
| 17 | #define SUSPEND_CTX_SP_OFFSET (8 * NR_CTX_REGS) |
| 18 | #define SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET (SUSPEND_CTX_SP_OFFSET + 8) |
| 19 | |
| 20 | /* |
| 21 | * Size of the context structure. |
| 22 | * +8 because of the padding bytes inserted for alignment constraint. |
| 23 | */ |
| 24 | #define SUSPEND_CTX_SZ (SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET + 8) |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 25 | |
| 26 | #ifndef __ASSEMBLY__ |
| 27 | #include <cassert.h> |
| 28 | #include <power_management.h> |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 31 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 32 | /* |
Sandrine Bailleux | 49180a8 | 2019-03-07 16:35:48 +0100 | [diff] [blame^] | 33 | * Architectural context to be saved/restored when entering/exiting suspend |
| 34 | * mode. |
| 35 | * |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 36 | * It must be 16-byte aligned since it is allocated on the stack, which must be |
| 37 | * 16-byte aligned on ARMv8 (AArch64). Even though the alignment requirement |
| 38 | * is not present in AArch32, we use the same alignment and register width as |
| 39 | * it allows the same structure to be reused for AArch32. |
| 40 | */ |
| 41 | typedef struct tftf_suspend_context { |
| 42 | uint64_t arch_ctx_regs[NR_CTX_REGS]; |
| 43 | uint64_t stack_pointer; |
Sandrine Bailleux | 49180a8 | 2019-03-07 16:35:48 +0100 | [diff] [blame^] | 44 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 45 | /* |
| 46 | * Whether the system context is saved and and needs to be restored. |
| 47 | * Note that the system context itself is not saved in this structure. |
| 48 | */ |
| 49 | unsigned int save_system_context; |
| 50 | } __aligned(16) tftf_suspend_ctx_t; |
| 51 | |
| 52 | /* |
Sandrine Bailleux | 49180a8 | 2019-03-07 16:35:48 +0100 | [diff] [blame^] | 53 | * Ensure consistent view of the context structure layout across asm and C |
| 54 | * code. |
| 55 | */ |
| 56 | CASSERT(SUSPEND_CTX_SZ == sizeof(tftf_suspend_ctx_t), |
| 57 | assert_suspend_context_size_mismatch); |
| 58 | |
| 59 | CASSERT(SUSPEND_CTX_SP_OFFSET == |
| 60 | __builtin_offsetof(tftf_suspend_ctx_t, stack_pointer), |
| 61 | assert_stack_pointer_location_mismatch_in_suspend_ctx); |
| 62 | |
| 63 | CASSERT(SUSPEND_CTX_SAVE_SYSTEM_CTX_OFFSET == |
| 64 | __builtin_offsetof(tftf_suspend_ctx_t, save_system_context), |
| 65 | assert_save_sys_ctx_mismatch_in_suspend_ctx); |
| 66 | |
| 67 | /* |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 68 | * Saves callee save registers on the stack |
| 69 | * Allocate space on stack for CPU context regs |
| 70 | * Enters suspend by calling tftf_enter_suspend. |
| 71 | * power state: PSCI power state to be sent via SMC |
| 72 | * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS |
| 73 | */ |
| 74 | unsigned int __tftf_suspend(const suspend_info_t *power_state); |
| 75 | |
| 76 | /* |
| 77 | * Saves the architecture context of CPU in the memory |
| 78 | * tftf_suspend_context: Pointer to the location for saving the context |
| 79 | */ |
| 80 | void __tftf_save_arch_context(struct tftf_suspend_context *ctx); |
| 81 | |
| 82 | /* |
| 83 | * Calls __tftf_save_arch_context to saves arch context of cpu to the memory |
| 84 | * pointed by ctx |
| 85 | * Enters suspend by calling the SMC |
| 86 | * power state: PSCI power state to be sent via SMC |
| 87 | * ctx: Pointer to the location where suspend context can be stored |
| 88 | * Returns: PSCI_E_SUCCESS or PSCI_E_INVALID_PARAMS |
| 89 | */ |
| 90 | int32_t tftf_enter_suspend(const suspend_info_t *power_state, |
| 91 | tftf_suspend_ctx_t *ctx); |
| 92 | |
| 93 | /* |
| 94 | * Invokes the appropriate driver functions in the TFTF framework |
| 95 | * to save their context prior to a system suspend. |
| 96 | */ |
| 97 | void tftf_save_system_ctx(tftf_suspend_ctx_t *ctx); |
| 98 | |
| 99 | /* |
| 100 | * Invokes the appropriate driver functions in the TFTF framework |
| 101 | * to restore their context on wake-up from system suspend. |
| 102 | */ |
| 103 | void tftf_restore_system_ctx(tftf_suspend_ctx_t *ctx); |
| 104 | |
| 105 | /* |
| 106 | * Restores the CPU arch context and callee registers from the location pointed |
| 107 | * by X0(context ID). |
| 108 | * Returns: PSCI_E_SUCCESS |
| 109 | */ |
| 110 | unsigned int __tftf_cpu_resume_ep(void); |
| 111 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 112 | #endif /* __ASSEMBLY__ */ |
| 113 | |
| 114 | #endif /* __SUSPEND_PRIV_H__ */ |