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 | |
| 7 | #include <arch.h> |
| 8 | #include <arch_helpers.h> |
| 9 | #include <pubsub.h> |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 10 | #include <spe.h> |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * The assembler does not yet understand the psb csync mnemonic |
| 14 | * so use the equivalent hint instruction. |
| 15 | */ |
| 16 | #define psb_csync() asm volatile("hint #17") |
| 17 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 18 | int spe_supported(void) |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 19 | { |
| 20 | uint64_t features; |
| 21 | |
| 22 | features = read_id_aa64dfr0_el1() >> ID_AA64DFR0_PMS_SHIFT; |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 23 | return (features & ID_AA64DFR0_PMS_MASK) == 1; |
| 24 | } |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 25 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 26 | void spe_enable(int el2_unused) |
| 27 | { |
| 28 | uint64_t v; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 29 | |
Dimitris Papastamos | aaa1985 | 2018-02-26 17:56:31 +0000 | [diff] [blame] | 30 | if (spe_supported() == 0) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 31 | return; |
| 32 | |
| 33 | if (el2_unused) { |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 34 | /* |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 35 | * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical |
| 36 | * profiling controls to EL2. |
| 37 | * |
| 38 | * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure |
| 39 | * state. Accesses to profiling buffer controls at |
| 40 | * Non-secure EL1 are not trapped to EL2. |
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 | v = read_mdcr_el2(); |
| 43 | v &= ~MDCR_EL2_TPMS; |
| 44 | v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1); |
| 45 | write_mdcr_el2(v); |
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 | |
| 48 | /* |
| 49 | * MDCR_EL2.NSPB (ARM v8.2): SPE enabled in Non-secure state |
| 50 | * and disabled in secure state. Accesses to SPE registers at |
| 51 | * S-EL1 generate trap exceptions to EL3. |
| 52 | */ |
| 53 | v = read_mdcr_el3(); |
| 54 | v |= MDCR_NSPB(MDCR_NSPB_EL1); |
| 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 | aaa1985 | 2018-02-26 17:56:31 +0000 | [diff] [blame] | 62 | if (spe_supported() == 0) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 63 | return; |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 64 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 65 | /* Drain buffered data */ |
| 66 | psb_csync(); |
| 67 | dsbnsh(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 68 | |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 69 | /* Disable profiling buffer */ |
| 70 | v = read_pmblimitr_el1(); |
| 71 | v &= ~(1ULL << 0); |
| 72 | write_pmblimitr_el1(v); |
| 73 | isb(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void *spe_drain_buffers_hook(const void *arg) |
| 77 | { |
Dimitris Papastamos | aaa1985 | 2018-02-26 17:56:31 +0000 | [diff] [blame] | 78 | if (spe_supported() == 0) |
Dimitris Papastamos | 5e8cd79 | 2018-02-19 14:52:19 +0000 | [diff] [blame] | 79 | return (void *)-1; |
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 | /* Drain buffered data */ |
| 82 | psb_csync(); |
| 83 | dsbnsh(); |
Dimitris Papastamos | 5bdbb47 | 2017-10-13 12:06:06 +0100 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook); |