11a853370SDavid Cunado /* 2*2ff8fbf3SDimitris 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 71a853370SDavid Cunado #include <arch.h> 81a853370SDavid Cunado #include <arch_helpers.h> 91a853370SDavid Cunado #include <pubsub.h> 101a853370SDavid Cunado #include <sve.h> 111a853370SDavid Cunado 12*2ff8fbf3SDimitris Papastamos int sve_supported(void) 131a853370SDavid Cunado { 141a853370SDavid Cunado uint64_t features; 151a853370SDavid Cunado 161a853370SDavid Cunado features = read_id_aa64pfr0_el1() >> ID_AA64PFR0_SVE_SHIFT; 17*2ff8fbf3SDimitris Papastamos return (features & ID_AA64PFR0_SVE_MASK) == 1; 18*2ff8fbf3SDimitris Papastamos } 19*2ff8fbf3SDimitris Papastamos 20*2ff8fbf3SDimitris Papastamos static void *disable_sve_hook(const void *arg) 21*2ff8fbf3SDimitris Papastamos { 221a853370SDavid Cunado uint64_t cptr; 231a853370SDavid Cunado 24*2ff8fbf3SDimitris Papastamos if (!sve_supported()) 25*2ff8fbf3SDimitris Papastamos return (void *)-1; 26*2ff8fbf3SDimitris Papastamos 271a853370SDavid Cunado /* 281a853370SDavid Cunado * Disable SVE, SIMD and FP access for the Secure world. 291a853370SDavid Cunado * As the SIMD/FP registers are part of the SVE Z-registers, any 301a853370SDavid Cunado * use of SIMD/FP functionality will corrupt the SVE registers. 311a853370SDavid Cunado * Therefore it is necessary to prevent use of SIMD/FP support 321a853370SDavid Cunado * in the Secure world as well as SVE functionality. 331a853370SDavid Cunado */ 341a853370SDavid Cunado cptr = read_cptr_el3(); 351a853370SDavid Cunado cptr = (cptr | TFP_BIT) & ~(CPTR_EZ_BIT); 361a853370SDavid Cunado write_cptr_el3(cptr); 371a853370SDavid Cunado 381a853370SDavid Cunado /* 391a853370SDavid Cunado * No explicit ISB required here as ERET to switch to Secure 401a853370SDavid Cunado * world covers it 411a853370SDavid Cunado */ 421a853370SDavid Cunado return 0; 431a853370SDavid Cunado } 441a853370SDavid Cunado 451a853370SDavid Cunado static void *enable_sve_hook(const void *arg) 461a853370SDavid Cunado { 471a853370SDavid Cunado uint64_t cptr; 481a853370SDavid Cunado 49*2ff8fbf3SDimitris Papastamos if (!sve_supported()) 50*2ff8fbf3SDimitris Papastamos return (void *)-1; 51*2ff8fbf3SDimitris Papastamos 521a853370SDavid Cunado /* 531a853370SDavid Cunado * Enable SVE, SIMD and FP access for the Non-secure world. 541a853370SDavid Cunado */ 551a853370SDavid Cunado cptr = read_cptr_el3(); 561a853370SDavid Cunado cptr = (cptr | CPTR_EZ_BIT) & ~(TFP_BIT); 571a853370SDavid Cunado write_cptr_el3(cptr); 581a853370SDavid Cunado 591a853370SDavid Cunado /* 601a853370SDavid Cunado * No explicit ISB required here as ERET to switch to Non-secure 611a853370SDavid Cunado * world covers it 621a853370SDavid Cunado */ 631a853370SDavid Cunado return 0; 641a853370SDavid Cunado } 651a853370SDavid Cunado 661a853370SDavid Cunado void sve_enable(int el2_unused) 671a853370SDavid Cunado { 681a853370SDavid Cunado uint64_t cptr; 69*2ff8fbf3SDimitris Papastamos 70*2ff8fbf3SDimitris Papastamos if (!sve_supported()) 71*2ff8fbf3SDimitris Papastamos return; 72*2ff8fbf3SDimitris Papastamos 731a853370SDavid Cunado #if CTX_INCLUDE_FPREGS 741a853370SDavid Cunado /* 751a853370SDavid Cunado * CTX_INCLUDE_FPREGS is not supported on SVE enabled systems. 761a853370SDavid Cunado */ 771a853370SDavid Cunado assert(0); 781a853370SDavid Cunado #endif 791a853370SDavid Cunado /* 801a853370SDavid Cunado * Update CPTR_EL3 to enable access to SVE functionality for the 811a853370SDavid Cunado * Non-secure world. 821a853370SDavid Cunado * NOTE - assumed that CPTR_EL3.TFP is set to allow access to 831a853370SDavid Cunado * the SIMD, floating-point and SVE support. 841a853370SDavid Cunado * 851a853370SDavid Cunado * CPTR_EL3.EZ: Set to 1 to enable access to SVE functionality 861a853370SDavid Cunado * in the Non-secure world. 871a853370SDavid Cunado */ 881a853370SDavid Cunado cptr = read_cptr_el3(); 891a853370SDavid Cunado cptr |= CPTR_EZ_BIT; 901a853370SDavid Cunado write_cptr_el3(cptr); 911a853370SDavid Cunado 921a853370SDavid Cunado /* 931a853370SDavid Cunado * Need explicit ISB here to guarantee that update to ZCR_ELx 941a853370SDavid Cunado * and CPTR_EL2.TZ do not result in trap to EL3. 951a853370SDavid Cunado */ 961a853370SDavid Cunado isb(); 971a853370SDavid Cunado 981a853370SDavid Cunado /* 991a853370SDavid Cunado * Ensure lower ELs have access to full vector length. 1001a853370SDavid Cunado */ 1011a853370SDavid Cunado write_zcr_el3(ZCR_EL3_LEN_MASK); 1021a853370SDavid Cunado 1031a853370SDavid Cunado if (el2_unused) { 1041a853370SDavid Cunado /* 1051a853370SDavid Cunado * Update CPTR_EL2 to enable access to SVE functionality 1061a853370SDavid Cunado * for Non-secure world, EL2 and Non-secure EL1 and EL0. 1071a853370SDavid Cunado * NOTE - assumed that CPTR_EL2.TFP is set to allow 1081a853370SDavid Cunado * access to the SIMD, floating-point and SVE support. 1091a853370SDavid Cunado * 1101a853370SDavid Cunado * CPTR_EL2.TZ: Set to 0 to enable access to SVE support 1111a853370SDavid Cunado * for EL2 and Non-secure EL1 and EL0. 1121a853370SDavid Cunado */ 1131a853370SDavid Cunado cptr = read_cptr_el2(); 1141a853370SDavid Cunado cptr &= ~(CPTR_EL2_TZ_BIT); 1151a853370SDavid Cunado write_cptr_el2(cptr); 1161a853370SDavid Cunado 1171a853370SDavid Cunado /* 1181a853370SDavid Cunado * Ensure lower ELs have access to full vector length. 1191a853370SDavid Cunado */ 1201a853370SDavid Cunado write_zcr_el2(ZCR_EL2_LEN_MASK); 1211a853370SDavid Cunado } 1221a853370SDavid Cunado /* 1231a853370SDavid Cunado * No explicit ISB required here as ERET to switch to 1241a853370SDavid Cunado * Non-secure world covers it. 1251a853370SDavid Cunado */ 1261a853370SDavid Cunado } 1271a853370SDavid Cunado 1281a853370SDavid Cunado SUBSCRIBE_TO_EVENT(cm_exited_normal_world, disable_sve_hook); 1291a853370SDavid Cunado SUBSCRIBE_TO_EVENT(cm_entering_normal_world, enable_sve_hook); 130