spe.c 1.78 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
8
#include <stdbool.h>

9
10
#include <arch.h>
#include <arch_helpers.h>
11
12
#include <lib/el3_runtime/pubsub.h>
#include <lib/extensions/spe.h>
13

14
15
16
17
18
19
20
21
static inline void psb_csync(void)
{
	/*
	 * The assembler does not yet understand the psb csync mnemonic
	 * so use the equivalent hint instruction.
	 */
	__asm__ volatile("hint #17");
}
22

23
bool spe_supported(void)
24
25
26
27
{
	uint64_t features;

	features = read_id_aa64dfr0_el1() >> ID_AA64DFR0_PMS_SHIFT;
28
	return (features & ID_AA64DFR0_PMS_MASK) == 1U;
29
}
30

31
void spe_enable(bool el2_unused)
32
33
{
	uint64_t v;
34

35
	if (!spe_supported())
36
37
38
		return;

	if (el2_unused) {
39
		/*
40
41
42
43
44
45
		 * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
		 * profiling controls to EL2.
		 *
		 * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
		 * state. Accesses to profiling buffer controls at
		 * Non-secure EL1 are not trapped to EL2.
46
		 */
47
48
49
50
		v = read_mdcr_el2();
		v &= ~MDCR_EL2_TPMS;
		v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
		write_mdcr_el2(v);
51
	}
52
53
54
55
56
57
58
59
60

	/*
	 * MDCR_EL2.NSPB (ARM v8.2): SPE enabled in Non-secure state
	 * and disabled in secure state. Accesses to SPE registers at
	 * S-EL1 generate trap exceptions to EL3.
	 */
	v = read_mdcr_el3();
	v |= MDCR_NSPB(MDCR_NSPB_EL1);
	write_mdcr_el3(v);
61
62
63
64
}

void spe_disable(void)
{
65
	uint64_t v;
66

67
	if (!spe_supported())
68
		return;
69

70
71
72
	/* Drain buffered data */
	psb_csync();
	dsbnsh();
73

74
75
76
77
78
	/* Disable profiling buffer */
	v = read_pmblimitr_el1();
	v &= ~(1ULL << 0);
	write_pmblimitr_el1(v);
	isb();
79
80
81
82
}

static void *spe_drain_buffers_hook(const void *arg)
{
83
	if (!spe_supported())
84
		return (void *)-1;
85

86
87
88
	/* Drain buffered data */
	psb_csync();
	dsbnsh();
89
90

	return (void *)0;
91
92
93
}

SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);