11a853370SDavid Cunado /* 22ff8fbf3SDimitris Papastamos * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 31a853370SDavid Cunado * 41a853370SDavid Cunado * SPDX-License-Identifier: BSD-3-Clause 51a853370SDavid Cunado */ 61a853370SDavid Cunado 7*09d40e0eSAntonio Nino Diaz #include <stdbool.h> 8*09d40e0eSAntonio Nino Diaz 91a853370SDavid Cunado #include <arch.h> 101a853370SDavid Cunado #include <arch_helpers.h> 11*09d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub.h> 12*09d40e0eSAntonio Nino Diaz #include <lib/extensions/sve.h> 131a853370SDavid Cunado 1440daecc1SAntonio Nino Diaz bool sve_supported(void) 151a853370SDavid Cunado { 161a853370SDavid Cunado uint64_t features; 171a853370SDavid Cunado 181a853370SDavid Cunado features = read_id_aa64pfr0_el1() >> ID_AA64PFR0_SVE_SHIFT; 1940daecc1SAntonio Nino Diaz return (features & ID_AA64PFR0_SVE_MASK) == 1U; 202ff8fbf3SDimitris Papastamos } 212ff8fbf3SDimitris Papastamos 222ff8fbf3SDimitris Papastamos static void *disable_sve_hook(const void *arg) 232ff8fbf3SDimitris Papastamos { 241a853370SDavid Cunado uint64_t cptr; 251a853370SDavid Cunado 2640daecc1SAntonio Nino Diaz if (!sve_supported()) 272ff8fbf3SDimitris Papastamos return (void *)-1; 282ff8fbf3SDimitris Papastamos 291a853370SDavid Cunado /* 301a853370SDavid Cunado * Disable SVE, SIMD and FP access for the Secure world. 311a853370SDavid Cunado * As the SIMD/FP registers are part of the SVE Z-registers, any 321a853370SDavid Cunado * use of SIMD/FP functionality will corrupt the SVE registers. 331a853370SDavid Cunado * Therefore it is necessary to prevent use of SIMD/FP support 341a853370SDavid Cunado * in the Secure world as well as SVE functionality. 351a853370SDavid Cunado */ 361a853370SDavid Cunado cptr = read_cptr_el3(); 371a853370SDavid Cunado cptr = (cptr | TFP_BIT) & ~(CPTR_EZ_BIT); 381a853370SDavid Cunado write_cptr_el3(cptr); 391a853370SDavid Cunado 401a853370SDavid Cunado /* 411a853370SDavid Cunado * No explicit ISB required here as ERET to switch to Secure 421a853370SDavid Cunado * world covers it 431a853370SDavid Cunado */ 4440daecc1SAntonio Nino Diaz return (void *)0; 451a853370SDavid Cunado } 461a853370SDavid Cunado 471a853370SDavid Cunado static void *enable_sve_hook(const void *arg) 481a853370SDavid Cunado { 491a853370SDavid Cunado uint64_t cptr; 501a853370SDavid Cunado 5140daecc1SAntonio Nino Diaz if (!sve_supported()) 522ff8fbf3SDimitris Papastamos return (void *)-1; 532ff8fbf3SDimitris Papastamos 541a853370SDavid Cunado /* 551a853370SDavid Cunado * Enable SVE, SIMD and FP access for the Non-secure world. 561a853370SDavid Cunado */ 571a853370SDavid Cunado cptr = read_cptr_el3(); 581a853370SDavid Cunado cptr = (cptr | CPTR_EZ_BIT) & ~(TFP_BIT); 591a853370SDavid Cunado write_cptr_el3(cptr); 601a853370SDavid Cunado 611a853370SDavid Cunado /* 621a853370SDavid Cunado * No explicit ISB required here as ERET to switch to Non-secure 631a853370SDavid Cunado * world covers it 641a853370SDavid Cunado */ 6540daecc1SAntonio Nino Diaz return (void *)0; 661a853370SDavid Cunado } 671a853370SDavid Cunado 6840daecc1SAntonio Nino Diaz void sve_enable(bool el2_unused) 691a853370SDavid Cunado { 701a853370SDavid Cunado uint64_t cptr; 712ff8fbf3SDimitris Papastamos 7240daecc1SAntonio Nino Diaz if (!sve_supported()) 732ff8fbf3SDimitris Papastamos return; 742ff8fbf3SDimitris Papastamos 751a853370SDavid Cunado #if CTX_INCLUDE_FPREGS 761a853370SDavid Cunado /* 771a853370SDavid Cunado * CTX_INCLUDE_FPREGS is not supported on SVE enabled systems. 781a853370SDavid Cunado */ 791a853370SDavid Cunado assert(0); 801a853370SDavid Cunado #endif 811a853370SDavid Cunado /* 821a853370SDavid Cunado * Update CPTR_EL3 to enable access to SVE functionality for the 831a853370SDavid Cunado * Non-secure world. 841a853370SDavid Cunado * NOTE - assumed that CPTR_EL3.TFP is set to allow access to 851a853370SDavid Cunado * the SIMD, floating-point and SVE support. 861a853370SDavid Cunado * 871a853370SDavid Cunado * CPTR_EL3.EZ: Set to 1 to enable access to SVE functionality 881a853370SDavid Cunado * in the Non-secure world. 891a853370SDavid Cunado */ 901a853370SDavid Cunado cptr = read_cptr_el3(); 911a853370SDavid Cunado cptr |= CPTR_EZ_BIT; 921a853370SDavid Cunado write_cptr_el3(cptr); 931a853370SDavid Cunado 941a853370SDavid Cunado /* 951a853370SDavid Cunado * Need explicit ISB here to guarantee that update to ZCR_ELx 961a853370SDavid Cunado * and CPTR_EL2.TZ do not result in trap to EL3. 971a853370SDavid Cunado */ 981a853370SDavid Cunado isb(); 991a853370SDavid Cunado 1001a853370SDavid Cunado /* 1011a853370SDavid Cunado * Ensure lower ELs have access to full vector length. 1021a853370SDavid Cunado */ 1031a853370SDavid Cunado write_zcr_el3(ZCR_EL3_LEN_MASK); 1041a853370SDavid Cunado 1051a853370SDavid Cunado if (el2_unused) { 1061a853370SDavid Cunado /* 1071a853370SDavid Cunado * Update CPTR_EL2 to enable access to SVE functionality 1081a853370SDavid Cunado * for Non-secure world, EL2 and Non-secure EL1 and EL0. 1091a853370SDavid Cunado * NOTE - assumed that CPTR_EL2.TFP is set to allow 1101a853370SDavid Cunado * access to the SIMD, floating-point and SVE support. 1111a853370SDavid Cunado * 1121a853370SDavid Cunado * CPTR_EL2.TZ: Set to 0 to enable access to SVE support 1131a853370SDavid Cunado * for EL2 and Non-secure EL1 and EL0. 1141a853370SDavid Cunado */ 1151a853370SDavid Cunado cptr = read_cptr_el2(); 1161a853370SDavid Cunado cptr &= ~(CPTR_EL2_TZ_BIT); 1171a853370SDavid Cunado write_cptr_el2(cptr); 1181a853370SDavid Cunado 1191a853370SDavid Cunado /* 1201a853370SDavid Cunado * Ensure lower ELs have access to full vector length. 1211a853370SDavid Cunado */ 1221a853370SDavid Cunado write_zcr_el2(ZCR_EL2_LEN_MASK); 1231a853370SDavid Cunado } 1241a853370SDavid Cunado /* 1251a853370SDavid Cunado * No explicit ISB required here as ERET to switch to 1261a853370SDavid Cunado * Non-secure world covers it. 1271a853370SDavid Cunado */ 1281a853370SDavid Cunado } 1291a853370SDavid Cunado 1301a853370SDavid Cunado SUBSCRIBE_TO_EVENT(cm_exited_normal_world, disable_sve_hook); 1311a853370SDavid Cunado SUBSCRIBE_TO_EVENT(cm_entering_normal_world, enable_sve_hook); 132