xref: /rk3399_ARM-atf/lib/extensions/spe/spe.c (revision 4d0b66323b242323ff738431c523aeb6d18dd3d5)
1 /*
2  * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdbool.h>
8 
9 #include <arch.h>
10 #include <arch_features.h>
11 #include <arch_helpers.h>
12 #include <lib/el3_runtime/pubsub.h>
13 #include <lib/extensions/spe.h>
14 
15 static 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 }
23 
24 void spe_init_el3(void)
25 {
26 	uint64_t v;
27 
28 	/*
29 	 * MDCR_EL2.NSPB (ARM v8.2): SPE enabled in Non-secure state
30 	 * and disabled in secure state. Accesses to SPE registers at
31 	 * S-EL1 generate trap exceptions to EL3.
32 	 *
33 	 * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1
34 	 * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented.
35 	 * Setting this bit to 1 doesn't have any effect on it when
36 	 * FEAT_SPEv1p2 not implemented.
37 	 */
38 	v = read_mdcr_el3();
39 	v |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
40 	write_mdcr_el3(v);
41 }
42 
43 void spe_init_el2_unused(void)
44 {
45 	uint64_t v;
46 
47 	/*
48 	 * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
49 	 * profiling controls to EL2.
50 	 *
51 	 * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
52 	 * state. Accesses to profiling buffer controls at
53 	 * Non-secure EL1 are not trapped to EL2.
54 	 */
55 	v = read_mdcr_el2();
56 	v &= ~MDCR_EL2_TPMS;
57 	v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
58 	write_mdcr_el2(v);
59 }
60 
61 void spe_disable(void)
62 {
63 	uint64_t v;
64 
65 	/* Drain buffered data */
66 	psb_csync();
67 	dsbnsh();
68 
69 	/* Disable profiling buffer */
70 	v = read_pmblimitr_el1();
71 	v &= ~(1ULL << 0);
72 	write_pmblimitr_el1(v);
73 	isb();
74 }
75 
76 static void *spe_drain_buffers_hook(const void *arg)
77 {
78 	if (!is_feat_spe_supported())
79 		return (void *)-1;
80 
81 	/* Drain buffered data */
82 	psb_csync();
83 	dsbnsh();
84 
85 	return (void *)0;
86 }
87 
88 SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);
89