Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 1 | /* |
Manish V Badarkhe | 67fec3e | 2021-12-31 16:08:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2022, 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 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 15 | static inline void psb_csync(void) |
| 16 | { |
| 17 | /* |
| 18 | * The assembler does not yet understand the psb csync mnemonic |
| 19 | * so use the equivalent hint instruction. |
| 20 | */ |
| 21 | __asm__ volatile("hint #17"); |
| 22 | } |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 23 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 24 | void spe_enable(bool el2_unused) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 25 | { |
| 26 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 27 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 28 | if (el2_unused) { |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 29 | /* |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 30 | * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical |
| 31 | * profiling controls to EL2. |
| 32 | * |
| 33 | * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure |
| 34 | * state. Accesses to profiling buffer controls at |
| 35 | * Non-secure EL1 are not trapped to EL2. |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 36 | */ |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 37 | v = read_mdcr_el2(); |
| 38 | v &= ~MDCR_EL2_TPMS; |
| 39 | v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1); |
| 40 | write_mdcr_el2(v); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 41 | } |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * MDCR_EL2.NSPB (ARM v8.2): SPE enabled in Non-secure state |
| 45 | * and disabled in secure state. Accesses to SPE registers at |
| 46 | * S-EL1 generate trap exceptions to EL3. |
Manish V Badarkhe | 67fec3e | 2021-12-31 16:08:51 +0000 | [diff] [blame] | 47 | * |
| 48 | * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1 |
| 49 | * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented. |
| 50 | * Setting this bit to 1 doesn't have any effect on it when |
| 51 | * FEAT_SPEv1p2 not implemented. |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 52 | */ |
| 53 | v = read_mdcr_el3(); |
Manish V Badarkhe | 67fec3e | 2021-12-31 16:08:51 +0000 | [diff] [blame] | 54 | v |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT; |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 55 | write_mdcr_el3(v); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void spe_disable(void) |
| 59 | { |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 60 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 61 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 62 | /* Drain buffered data */ |
| 63 | psb_csync(); |
| 64 | dsbnsh(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 65 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 66 | /* Disable profiling buffer */ |
| 67 | v = read_pmblimitr_el1(); |
| 68 | v &= ~(1ULL << 0); |
| 69 | write_pmblimitr_el1(v); |
| 70 | isb(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void *spe_drain_buffers_hook(const void *arg) |
| 74 | { |
Andre Przywara | f3e8cfc | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 75 | if (!is_feat_spe_supported()) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 76 | return (void *)-1; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 77 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 78 | /* Drain buffered data */ |
| 79 | psb_csync(); |
| 80 | dsbnsh(); |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 81 | |
| 82 | return (void *)0; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook); |