2017-10-13 12:06:06 +01:00
|
|
|
/*
|
2018-02-19 14:52:19 +00:00
|
|
|
* Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
|
2017-10-13 12:06:06 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <arch.h>
|
|
|
|
#include <arch_helpers.h>
|
|
|
|
#include <pubsub.h>
|
2018-02-12 12:36:17 +00:00
|
|
|
#include <spe.h>
|
2017-10-13 12:06:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The assembler does not yet understand the psb csync mnemonic
|
|
|
|
* so use the equivalent hint instruction.
|
|
|
|
*/
|
|
|
|
#define psb_csync() asm volatile("hint #17")
|
|
|
|
|
2018-02-19 14:52:19 +00:00
|
|
|
int spe_supported(void)
|
2017-10-13 12:06:06 +01:00
|
|
|
{
|
|
|
|
uint64_t features;
|
|
|
|
|
|
|
|
features = read_id_aa64dfr0_el1() >> ID_AA64DFR0_PMS_SHIFT;
|
2018-02-19 14:52:19 +00:00
|
|
|
return (features & ID_AA64DFR0_PMS_MASK) == 1;
|
|
|
|
}
|
2017-10-13 12:06:06 +01:00
|
|
|
|
2018-02-19 14:52:19 +00:00
|
|
|
void spe_enable(int el2_unused)
|
|
|
|
{
|
|
|
|
uint64_t v;
|
2017-10-13 12:06:06 +01:00
|
|
|
|
2018-02-26 17:56:31 +00:00
|
|
|
if (spe_supported() == 0)
|
2018-02-19 14:52:19 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (el2_unused) {
|
2017-10-13 12:06:06 +01:00
|
|
|
/*
|
2018-02-19 14:52:19 +00:00
|
|
|
* 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.
|
2017-10-13 12:06:06 +01:00
|
|
|
*/
|
2018-02-19 14:52:19 +00:00
|
|
|
v = read_mdcr_el2();
|
|
|
|
v &= ~MDCR_EL2_TPMS;
|
|
|
|
v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
|
|
|
|
write_mdcr_el2(v);
|
2017-10-13 12:06:06 +01:00
|
|
|
}
|
2018-02-19 14:52:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2017-10-13 12:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spe_disable(void)
|
|
|
|
{
|
2018-02-19 14:52:19 +00:00
|
|
|
uint64_t v;
|
2017-10-13 12:06:06 +01:00
|
|
|
|
2018-02-26 17:56:31 +00:00
|
|
|
if (spe_supported() == 0)
|
2018-02-19 14:52:19 +00:00
|
|
|
return;
|
2017-10-13 12:06:06 +01:00
|
|
|
|
2018-02-19 14:52:19 +00:00
|
|
|
/* Drain buffered data */
|
|
|
|
psb_csync();
|
|
|
|
dsbnsh();
|
2017-10-13 12:06:06 +01:00
|
|
|
|
2018-02-19 14:52:19 +00:00
|
|
|
/* Disable profiling buffer */
|
|
|
|
v = read_pmblimitr_el1();
|
|
|
|
v &= ~(1ULL << 0);
|
|
|
|
write_pmblimitr_el1(v);
|
|
|
|
isb();
|
2017-10-13 12:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *spe_drain_buffers_hook(const void *arg)
|
|
|
|
{
|
2018-02-26 17:56:31 +00:00
|
|
|
if (spe_supported() == 0)
|
2018-02-19 14:52:19 +00:00
|
|
|
return (void *)-1;
|
2017-10-13 12:06:06 +01:00
|
|
|
|
2018-02-19 14:52:19 +00:00
|
|
|
/* Drain buffered data */
|
|
|
|
psb_csync();
|
|
|
|
dsbnsh();
|
2017-10-13 12:06:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);
|