Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 1 | /* |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, 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> |
| 10 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <lib/el3_runtime/pubsub.h> |
| 12 | #include <lib/extensions/spe.h> |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 13 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 14 | static inline void psb_csync(void) |
| 15 | { |
| 16 | /* |
| 17 | * The assembler does not yet understand the psb csync mnemonic |
| 18 | * so use the equivalent hint instruction. |
| 19 | */ |
| 20 | __asm__ volatile("hint #17"); |
| 21 | } |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 22 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 23 | bool spe_supported(void) |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 24 | { |
| 25 | uint64_t features; |
| 26 | |
| 27 | features = read_id_aa64dfr0_el1() >> ID_AA64DFR0_PMS_SHIFT; |
Andre Przywara | 7828f22 | 2020-09-11 09:18:09 +0100 | [diff] [blame] | 28 | return (features & ID_AA64DFR0_PMS_MASK) > 0ULL; |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 29 | } |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 30 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 31 | void spe_enable(bool el2_unused) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 32 | { |
| 33 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 34 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 35 | if (!spe_supported()) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 36 | return; |
| 37 | |
| 38 | if (el2_unused) { |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 39 | /* |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 40 | * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical |
| 41 | * profiling controls to EL2. |
| 42 | * |
| 43 | * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure |
| 44 | * state. Accesses to profiling buffer controls at |
| 45 | * Non-secure EL1 are not trapped to EL2. |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 46 | */ |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 47 | v = read_mdcr_el2(); |
| 48 | v &= ~MDCR_EL2_TPMS; |
| 49 | v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1); |
| 50 | write_mdcr_el2(v); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 51 | } |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * MDCR_EL2.NSPB (ARM v8.2): SPE enabled in Non-secure state |
| 55 | * and disabled in secure state. Accesses to SPE registers at |
| 56 | * S-EL1 generate trap exceptions to EL3. |
| 57 | */ |
| 58 | v = read_mdcr_el3(); |
| 59 | v |= MDCR_NSPB(MDCR_NSPB_EL1); |
| 60 | write_mdcr_el3(v); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void spe_disable(void) |
| 64 | { |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 65 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 66 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 67 | if (!spe_supported()) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 68 | return; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 69 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 70 | /* Drain buffered data */ |
| 71 | psb_csync(); |
| 72 | dsbnsh(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 73 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 74 | /* Disable profiling buffer */ |
| 75 | v = read_pmblimitr_el1(); |
| 76 | v &= ~(1ULL << 0); |
| 77 | write_pmblimitr_el1(v); |
| 78 | isb(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void *spe_drain_buffers_hook(const void *arg) |
| 82 | { |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 83 | if (!spe_supported()) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 84 | return (void *)-1; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 85 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 86 | /* Drain buffered data */ |
| 87 | psb_csync(); |
| 88 | dsbnsh(); |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 89 | |
| 90 | return (void *)0; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook); |