blob: 2c25a9db777031708c18070b5876bd9a29350737 [file] [log] [blame]
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +01001/*
Boyan Karatotev6468d4a2023-02-16 15:12:45 +00002 * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <stdbool.h>
8
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +01009#include <arch.h>
Andre Przywaraf3e8cfc2022-11-17 16:42:09 +000010#include <arch_features.h>
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010011#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <lib/el3_runtime/pubsub.h>
13#include <lib/extensions/spe.h>
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010014
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010015static 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 Papastamos5bdbb472017-10-13 12:06:06 +010023
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000024void spe_init_el3(void)
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000025{
26 uint64_t v;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010027
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000028 /*
Boyan Karatotev6e2fd8b2023-02-13 16:38:37 +000029 * MDCR_EL3.NSPB (ARM v8.2): SPE enabled in Non-secure state
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000030 * and disabled in secure state. Accesses to SPE registers at
31 * S-EL1 generate trap exceptions to EL3.
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +000032 *
Boyan Karatotev6e2fd8b2023-02-13 16:38:37 +000033 * MDCR_EL3.NSPBE: Profiling Buffer uses Non-secure Virtual Addresses.
34 * When FEAT_RME is not implemented, this field is RES0.
35 *
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +000036 * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1
37 * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented.
38 * Setting this bit to 1 doesn't have any effect on it when
39 * FEAT_SPEv1p2 not implemented.
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000040 */
41 v = read_mdcr_el3();
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +000042 v |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
Boyan Karatotev6e2fd8b2023-02-13 16:38:37 +000043 v &= ~(MDCR_NSPBE_BIT);
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000044 write_mdcr_el3(v);
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010045}
46
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000047void spe_init_el2_unused(void)
48{
49 uint64_t v;
50
51 /*
52 * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
53 * profiling controls to EL2.
54 *
55 * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
56 * state. Accesses to profiling buffer controls at
57 * Non-secure EL1 are not trapped to EL2.
58 */
59 v = read_mdcr_el2();
60 v &= ~MDCR_EL2_TPMS;
61 v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
62 write_mdcr_el2(v);
63}
64
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010065void spe_disable(void)
66{
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000067 uint64_t v;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010068
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000069 /* Drain buffered data */
70 psb_csync();
71 dsbnsh();
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010072
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000073 /* Disable profiling buffer */
74 v = read_pmblimitr_el1();
75 v &= ~(1ULL << 0);
76 write_pmblimitr_el1(v);
77 isb();
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010078}
79
80static void *spe_drain_buffers_hook(const void *arg)
81{
Andre Przywaraf3e8cfc2022-11-17 16:42:09 +000082 if (!is_feat_spe_supported())
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000083 return (void *)-1;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010084
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000085 /* Drain buffered data */
86 psb_csync();
87 dsbnsh();
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010088
89 return (void *)0;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010090}
91
92SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);