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