blob: d6532224ad862aa4617dff8b69c32cc5a18c3e8a [file] [log] [blame]
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +01001/*
Jayanth Dodderi Chidanand18d93792023-07-18 14:48:09 +01002 * Copyright (c) 2017-2024, 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
Jayanth Dodderi Chidanand18d93792023-07-18 14:48:09 +010015#include <plat/common/platform.h>
16
17typedef struct spe_ctx {
18 u_register_t pmblimitr_el1;
19} spe_ctx_t;
20
21static struct spe_ctx spe_ctxs[PLATFORM_CORE_COUNT];
22
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010023static inline void psb_csync(void)
24{
25 /*
26 * The assembler does not yet understand the psb csync mnemonic
27 * so use the equivalent hint instruction.
28 */
29 __asm__ volatile("hint #17");
30}
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010031
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +010032void spe_enable(cpu_context_t *ctx)
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000033{
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +010034 el3_state_t *state = get_el3state_ctx(ctx);
35 u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010036
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000037 /*
Boyan Karatotev6e2fd8b2023-02-13 16:38:37 +000038 * MDCR_EL3.NSPB (ARM v8.2): SPE enabled in Non-secure state
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000039 * and disabled in secure state. Accesses to SPE registers at
40 * S-EL1 generate trap exceptions to EL3.
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +000041 *
Boyan Karatotev6e2fd8b2023-02-13 16:38:37 +000042 * MDCR_EL3.NSPBE: Profiling Buffer uses Non-secure Virtual Addresses.
43 * When FEAT_RME is not implemented, this field is RES0.
44 *
Manish V Badarkhe67fec3e2021-12-31 16:08:51 +000045 * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1
46 * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented.
47 * Setting this bit to 1 doesn't have any effect on it when
48 * FEAT_SPEv1p2 not implemented.
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000049 */
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +010050 mdcr_el3_val |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
51 mdcr_el3_val &= ~(MDCR_NSPBE_BIT);
52 write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010053}
54
Manish Pandeye01b4222024-07-18 16:17:45 +010055void spe_disable(cpu_context_t *ctx)
56{
57 el3_state_t *state = get_el3state_ctx(ctx);
58 u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
59
60 /*
61 * MDCR_EL3.NSPB: Clear these bits to disable SPE feature, as it was enabled
62 * for Non-secure state only. After clearing these bits Secure state owns
63 * the Profiling Buffer and accesses to Statistical Profiling and Profiling
64 * Buffer control registers at EL2 and EL1 generate Trap exceptions to EL3
65 *
66 * MDCR_EL3.NSPBE: Don't care as it was cleared during spe_enable and setting
67 * this to 1 does not make sense as NSPBE{1} and NSPB{0b0x} is RESERVED.
68 *
69 * MDCR_EL3.EnPMSN (ARM v8.7): Clear the bit to trap access of PMSNEVFR_EL1
70 * from EL2/EL1 to EL3.
71 */
72 mdcr_el3_val &= ~(MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT);
73 write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
74}
75
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000076void spe_init_el2_unused(void)
77{
78 uint64_t v;
79
80 /*
81 * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
82 * profiling controls to EL2.
83 *
84 * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
85 * state. Accesses to profiling buffer controls at
86 * Non-secure EL1 are not trapped to EL2.
87 */
88 v = read_mdcr_el2();
89 v &= ~MDCR_EL2_TPMS;
90 v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
91 write_mdcr_el2(v);
92}
93
Manish Pandey46dff392024-07-18 16:07:21 +010094void spe_stop(void)
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010095{
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000096 uint64_t v;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +010097
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000098 /* Drain buffered data */
99 psb_csync();
100 dsbnsh();
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +0100101
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000102 /* Disable profiling buffer */
103 v = read_pmblimitr_el1();
104 v &= ~(1ULL << 0);
105 write_pmblimitr_el1(v);
106 isb();
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +0100107}
108
109static void *spe_drain_buffers_hook(const void *arg)
110{
Andre Przywaraf3e8cfc2022-11-17 16:42:09 +0000111 if (!is_feat_spe_supported())
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000112 return (void *)-1;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +0100113
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000114 /* Drain buffered data */
115 psb_csync();
116 dsbnsh();
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +0100117
118 return (void *)0;
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +0100119}
120
Jayanth Dodderi Chidanand18d93792023-07-18 14:48:09 +0100121static void *spe_context_save(const void *arg)
122{
123 unsigned int core_pos;
124 struct spe_ctx *ctx;
125
126 if (is_feat_spe_supported()) {
127 core_pos = plat_my_core_pos();
128 ctx = &spe_ctxs[core_pos];
129 ctx->pmblimitr_el1 = read_pmblimitr_el1();
130 }
131
132 return NULL;
133}
134
135static void *spe_context_restore(const void *arg)
136{
137 unsigned int core_pos;
138 struct spe_ctx *ctx;
139
140 if (is_feat_spe_supported()) {
141 core_pos = plat_my_core_pos();
142 ctx = &spe_ctxs[core_pos];
143 write_pmblimitr_el1(ctx->pmblimitr_el1);
144 }
145
146 return NULL;
147}
148
Dimitris Papastamos5bdbb472017-10-13 12:06:06 +0100149SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);
Jayanth Dodderi Chidanand18d93792023-07-18 14:48:09 +0100150
151SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, spe_context_save);
152SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, spe_context_restore);