blob: fa4ac77582976ad5efdc7d8a88a178a42f1d1752 [file] [log] [blame]
David Cunadoce88eee2017-10-20 11:30:57 +01001/*
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
David Cunadoce88eee2017-10-20 11:30:57 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <stdbool.h>
8
David Cunadoce88eee2017-10-20 11:30:57 +01009#include <arch.h>
10#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <lib/el3_runtime/pubsub.h>
12#include <lib/extensions/sve.h>
David Cunadoce88eee2017-10-20 11:30:57 +010013
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010014bool sve_supported(void)
David Cunadoce88eee2017-10-20 11:30:57 +010015{
16 uint64_t features;
17
18 features = read_id_aa64pfr0_el1() >> ID_AA64PFR0_SVE_SHIFT;
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010019 return (features & ID_AA64PFR0_SVE_MASK) == 1U;
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000020}
David Cunadoce88eee2017-10-20 11:30:57 +010021
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000022static void *disable_sve_hook(const void *arg)
23{
24 uint64_t cptr;
David Cunadoce88eee2017-10-20 11:30:57 +010025
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010026 if (!sve_supported())
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000027 return (void *)-1;
28
29 /*
30 * Disable SVE, SIMD and FP access for the Secure world.
31 * As the SIMD/FP registers are part of the SVE Z-registers, any
32 * use of SIMD/FP functionality will corrupt the SVE registers.
33 * Therefore it is necessary to prevent use of SIMD/FP support
34 * in the Secure world as well as SVE functionality.
35 */
36 cptr = read_cptr_el3();
37 cptr = (cptr | TFP_BIT) & ~(CPTR_EZ_BIT);
38 write_cptr_el3(cptr);
39
40 /*
41 * No explicit ISB required here as ERET to switch to Secure
42 * world covers it
43 */
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010044 return (void *)0;
David Cunadoce88eee2017-10-20 11:30:57 +010045}
46
47static void *enable_sve_hook(const void *arg)
48{
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000049 uint64_t cptr;
David Cunadoce88eee2017-10-20 11:30:57 +010050
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010051 if (!sve_supported())
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000052 return (void *)-1;
David Cunadoce88eee2017-10-20 11:30:57 +010053
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000054 /*
55 * Enable SVE, SIMD and FP access for the Non-secure world.
56 */
57 cptr = read_cptr_el3();
58 cptr = (cptr | CPTR_EZ_BIT) & ~(TFP_BIT);
59 write_cptr_el3(cptr);
David Cunadoce88eee2017-10-20 11:30:57 +010060
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000061 /*
62 * No explicit ISB required here as ERET to switch to Non-secure
63 * world covers it
64 */
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010065 return (void *)0;
David Cunadoce88eee2017-10-20 11:30:57 +010066}
67
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010068void sve_enable(bool el2_unused)
David Cunadoce88eee2017-10-20 11:30:57 +010069{
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000070 uint64_t cptr;
David Cunadoce88eee2017-10-20 11:30:57 +010071
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010072 if (!sve_supported())
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000073 return;
74
David Cunadoce88eee2017-10-20 11:30:57 +010075#if CTX_INCLUDE_FPREGS
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000076 /*
77 * CTX_INCLUDE_FPREGS is not supported on SVE enabled systems.
78 */
79 assert(0);
David Cunadoce88eee2017-10-20 11:30:57 +010080#endif
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000081 /*
82 * Update CPTR_EL3 to enable access to SVE functionality for the
83 * Non-secure world.
84 * NOTE - assumed that CPTR_EL3.TFP is set to allow access to
85 * the SIMD, floating-point and SVE support.
86 *
87 * CPTR_EL3.EZ: Set to 1 to enable access to SVE functionality
88 * in the Non-secure world.
89 */
90 cptr = read_cptr_el3();
91 cptr |= CPTR_EZ_BIT;
92 write_cptr_el3(cptr);
David Cunadoce88eee2017-10-20 11:30:57 +010093
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000094 /*
95 * Need explicit ISB here to guarantee that update to ZCR_ELx
96 * and CPTR_EL2.TZ do not result in trap to EL3.
97 */
98 isb();
99
100 /*
101 * Ensure lower ELs have access to full vector length.
102 */
103 write_zcr_el3(ZCR_EL3_LEN_MASK);
David Cunadoce88eee2017-10-20 11:30:57 +0100104
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000105 if (el2_unused) {
David Cunadoce88eee2017-10-20 11:30:57 +0100106 /*
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000107 * Update CPTR_EL2 to enable access to SVE functionality
108 * for Non-secure world, EL2 and Non-secure EL1 and EL0.
109 * NOTE - assumed that CPTR_EL2.TFP is set to allow
110 * access to the SIMD, floating-point and SVE support.
111 *
112 * CPTR_EL2.TZ: Set to 0 to enable access to SVE support
113 * for EL2 and Non-secure EL1 and EL0.
David Cunadoce88eee2017-10-20 11:30:57 +0100114 */
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000115 cptr = read_cptr_el2();
116 cptr &= ~(CPTR_EL2_TZ_BIT);
117 write_cptr_el2(cptr);
David Cunadoce88eee2017-10-20 11:30:57 +0100118
David Cunadoce88eee2017-10-20 11:30:57 +0100119 /*
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000120 * Ensure lower ELs have access to full vector length.
David Cunadoce88eee2017-10-20 11:30:57 +0100121 */
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000122 write_zcr_el2(ZCR_EL2_LEN_MASK);
David Cunadoce88eee2017-10-20 11:30:57 +0100123 }
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +0000124 /*
125 * No explicit ISB required here as ERET to switch to
126 * Non-secure world covers it.
127 */
David Cunadoce88eee2017-10-20 11:30:57 +0100128}
129
130SUBSCRIBE_TO_EVENT(cm_exited_normal_world, disable_sve_hook);
131SUBSCRIBE_TO_EVENT(cm_entering_normal_world, enable_sve_hook);