blob: b1fe39f5892fc4d56bbea21341201ce609f8bd5a [file] [log] [blame]
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +01001/*
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +00002 * Copyright (c) 2017-2022, 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
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010024void spe_enable(bool el2_unused)
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 if (el2_unused) {
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010029 /*
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000030 * 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 Papastamos5bdbb472017-10-13 12:06:06 +010036 */
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000037 v = read_mdcr_el2();
38 v &= ~MDCR_EL2_TPMS;
39 v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
40 write_mdcr_el2(v);
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010041 }
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000042
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 Badarkhe67fec3e2021-12-31 16:08:51 +000047 *
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 Papastamos5e8cd792018-02-19 14:52:19 +000052 */
53 v = read_mdcr_el3();
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +000054 v |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000055 write_mdcr_el3(v);
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010056}
57
58void spe_disable(void)
59{
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000060 uint64_t v;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010061
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000062 /* Drain buffered data */
63 psb_csync();
64 dsbnsh();
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010065
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000066 /* Disable profiling buffer */
67 v = read_pmblimitr_el1();
68 v &= ~(1ULL << 0);
69 write_pmblimitr_el1(v);
70 isb();
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010071}
72
73static void *spe_drain_buffers_hook(const void *arg)
74{
Andre Przywaraf3e8cfc2022-11-17 16:42:09 +000075 if (!is_feat_spe_supported())
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000076 return (void *)-1;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010077
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000078 /* Drain buffered data */
79 psb_csync();
80 dsbnsh();
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010081
82 return (void *)0;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010083}
84
85SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);