Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 1 | /* |
Jayanth Dodderi Chidanand | 18d9379 | 2023-07-18 14:48:09 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved. |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <stdbool.h> |
| 8 | |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 9 | #include <arch.h> |
Andre Przywara | f3e8cfc | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 10 | #include <arch_features.h> |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 11 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <lib/el3_runtime/pubsub.h> |
| 13 | #include <lib/extensions/spe.h> |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 14 | |
Jayanth Dodderi Chidanand | 18d9379 | 2023-07-18 14:48:09 +0100 | [diff] [blame] | 15 | #include <plat/common/platform.h> |
| 16 | |
| 17 | typedef struct spe_ctx { |
| 18 | u_register_t pmblimitr_el1; |
| 19 | } spe_ctx_t; |
| 20 | |
| 21 | static struct spe_ctx spe_ctxs[PLATFORM_CORE_COUNT]; |
| 22 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 23 | static inline void psb_csync(void) |
| 24 | { |
| 25 | /* |
| 26 | * The assembler does not yet understand the psb csync mnemonic |
| 27 | * so use the equivalent hint instruction. |
| 28 | */ |
| 29 | __asm__ volatile("hint #17"); |
| 30 | } |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 31 | |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 32 | void spe_init_el3(void) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 33 | { |
| 34 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 35 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 36 | /* |
Boyan Karatotev | 6e2fd8b | 2023-02-13 16:38:37 +0000 | [diff] [blame] | 37 | * MDCR_EL3.NSPB (ARM v8.2): SPE enabled in Non-secure state |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 38 | * and disabled in secure state. Accesses to SPE registers at |
| 39 | * S-EL1 generate trap exceptions to EL3. |
Manish V Badarkhe | 67fec3e | 2021-12-31 16:08:51 +0000 | [diff] [blame] | 40 | * |
Boyan Karatotev | 6e2fd8b | 2023-02-13 16:38:37 +0000 | [diff] [blame] | 41 | * MDCR_EL3.NSPBE: Profiling Buffer uses Non-secure Virtual Addresses. |
| 42 | * When FEAT_RME is not implemented, this field is RES0. |
| 43 | * |
Manish V Badarkhe | 67fec3e | 2021-12-31 16:08:51 +0000 | [diff] [blame] | 44 | * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1 |
| 45 | * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented. |
| 46 | * Setting this bit to 1 doesn't have any effect on it when |
| 47 | * FEAT_SPEv1p2 not implemented. |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 48 | */ |
| 49 | v = read_mdcr_el3(); |
Manish V Badarkhe | 67fec3e | 2021-12-31 16:08:51 +0000 | [diff] [blame] | 50 | v |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT; |
Boyan Karatotev | 6e2fd8b | 2023-02-13 16:38:37 +0000 | [diff] [blame] | 51 | v &= ~(MDCR_NSPBE_BIT); |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 52 | write_mdcr_el3(v); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 55 | void spe_init_el2_unused(void) |
| 56 | { |
| 57 | uint64_t v; |
| 58 | |
| 59 | /* |
| 60 | * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical |
| 61 | * profiling controls to EL2. |
| 62 | * |
| 63 | * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure |
| 64 | * state. Accesses to profiling buffer controls at |
| 65 | * Non-secure EL1 are not trapped to EL2. |
| 66 | */ |
| 67 | v = read_mdcr_el2(); |
| 68 | v &= ~MDCR_EL2_TPMS; |
| 69 | v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1); |
| 70 | write_mdcr_el2(v); |
| 71 | } |
| 72 | |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 73 | void spe_disable(void) |
| 74 | { |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 75 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 76 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 77 | /* Drain buffered data */ |
| 78 | psb_csync(); |
| 79 | dsbnsh(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 80 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 81 | /* Disable profiling buffer */ |
| 82 | v = read_pmblimitr_el1(); |
| 83 | v &= ~(1ULL << 0); |
| 84 | write_pmblimitr_el1(v); |
| 85 | isb(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void *spe_drain_buffers_hook(const void *arg) |
| 89 | { |
Andre Przywara | f3e8cfc | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 90 | if (!is_feat_spe_supported()) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 91 | return (void *)-1; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 92 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 93 | /* Drain buffered data */ |
| 94 | psb_csync(); |
| 95 | dsbnsh(); |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 96 | |
| 97 | return (void *)0; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Jayanth Dodderi Chidanand | 18d9379 | 2023-07-18 14:48:09 +0100 | [diff] [blame] | 100 | static void *spe_context_save(const void *arg) |
| 101 | { |
| 102 | unsigned int core_pos; |
| 103 | struct spe_ctx *ctx; |
| 104 | |
| 105 | if (is_feat_spe_supported()) { |
| 106 | core_pos = plat_my_core_pos(); |
| 107 | ctx = &spe_ctxs[core_pos]; |
| 108 | ctx->pmblimitr_el1 = read_pmblimitr_el1(); |
| 109 | } |
| 110 | |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | static void *spe_context_restore(const void *arg) |
| 115 | { |
| 116 | unsigned int core_pos; |
| 117 | struct spe_ctx *ctx; |
| 118 | |
| 119 | if (is_feat_spe_supported()) { |
| 120 | core_pos = plat_my_core_pos(); |
| 121 | ctx = &spe_ctxs[core_pos]; |
| 122 | write_pmblimitr_el1(ctx->pmblimitr_el1); |
| 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 128 | SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook); |
Jayanth Dodderi Chidanand | 18d9379 | 2023-07-18 14:48:09 +0100 | [diff] [blame] | 129 | |
| 130 | SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, spe_context_save); |
| 131 | SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, spe_context_restore); |