blob: 632b2a793cb8029401ddb4eddf686015352731bf [file] [log] [blame]
johpow0150ccb552020-11-10 19:22:13 -06001/*
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +00002 * Copyright (c) 2021-2023, ARM Limited and Contributors. All rights reserved.
johpow0150ccb552020-11-10 19:22:13 -06003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdbool.h>
8#include <stdio.h>
9
10#include <arch.h>
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000011#include <arch_features.h>
johpow0150ccb552020-11-10 19:22:13 -060012#include <arch_helpers.h>
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000013#include <debug.h>
johpow0150ccb552020-11-10 19:22:13 -060014#include <lib/extensions/sme.h>
15
johpow0150ccb552020-11-10 19:22:13 -060016/*
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000017 * Function : sme_enable
18 * Enable SME for nonsecure use at EL2 for TFTF cases.
johpow0150ccb552020-11-10 19:22:13 -060019 */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000020void sme_enable(void)
johpow0150ccb552020-11-10 19:22:13 -060021{
22 u_register_t reg;
23
johpow0150ccb552020-11-10 19:22:13 -060024 /*
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000025 * Make sure SME accesses don't cause traps by setting appropriate
26 * fields in CPTR_EL2.
johpow0150ccb552020-11-10 19:22:13 -060027 */
28 reg = read_cptr_el2();
29 if ((read_hcr_el2() & HCR_E2H_BIT) == 0U) {
30 /* When HCR_EL2.E2H == 0, clear TSM bit in CPTR_EL2. */
31 reg = reg & ~CPTR_EL2_TSM_BIT;
32 } else {
33 /* When HCR_EL2.E2H == 1, set SMEN bits in CPTR_EL2. */
34 reg = reg | (CPTR_EL2_SMEN_MASK << CPTR_EL2_SMEN_SHIFT);
35 }
36 write_cptr_el2(reg);
37
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000038 isb();
39
johpow0150ccb552020-11-10 19:22:13 -060040}
41
42/*
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000043 * Function: sme_smstart
44 * This function enables streaming mode and ZA array storage access
45 * independently or together based on the type of instruction variant.
46 *
johpow0150ccb552020-11-10 19:22:13 -060047 * Parameters
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000048 * smstart_type: If SMSTART, streaming mode and ZA access is enabled.
49 * If SMSTART_SM, streaming mode enabled.
50 * If SMSTART_ZA enables SME ZA storage and, ZT0 storage access.
johpow0150ccb552020-11-10 19:22:13 -060051 */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000052void sme_smstart(smestart_instruction_type_t smstart_type)
johpow0150ccb552020-11-10 19:22:13 -060053{
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000054 u_register_t svcr = 0ULL;
johpow0150ccb552020-11-10 19:22:13 -060055
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000056 switch (smstart_type) {
57 case SMSTART:
58 svcr = (SVCR_SM_BIT | SVCR_ZA_BIT);
59 break;
60
61 case SMSTART_SM:
62 svcr = SVCR_SM_BIT;
63 break;
64
65 case SMSTART_ZA:
66 svcr = SVCR_ZA_BIT;
67 break;
68
69 default:
70 ERROR("Illegal SMSTART Instruction Variant\n");
71 break;
johpow0150ccb552020-11-10 19:22:13 -060072 }
johpow0150ccb552020-11-10 19:22:13 -060073 write_svcr(read_svcr() | svcr);
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000074
75 isb();
johpow0150ccb552020-11-10 19:22:13 -060076}
77
78/*
79 * sme_smstop
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000080 * This function exits streaming mode and disables ZA array storage access
81 * independently or together based on the type of instruction variant.
82 *
johpow0150ccb552020-11-10 19:22:13 -060083 * Parameters
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000084 * smstop_type: If SMSTOP, exits streaming mode and ZA access is disabled
85 * If SMSTOP_SM, exits streaming mode.
86 * If SMSTOP_ZA disables SME ZA storage and, ZT0 storage access.
johpow0150ccb552020-11-10 19:22:13 -060087 */
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000088void sme_smstop(smestop_instruction_type_t smstop_type)
johpow0150ccb552020-11-10 19:22:13 -060089{
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000090 u_register_t svcr = 0ULL;
johpow0150ccb552020-11-10 19:22:13 -060091
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000092 switch (smstop_type) {
93 case SMSTOP:
94 svcr = (~SVCR_SM_BIT) & (~SVCR_ZA_BIT);
95 break;
96
97 case SMSTOP_SM:
johpow0150ccb552020-11-10 19:22:13 -060098 svcr = ~SVCR_SM_BIT;
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000099 break;
100
101 case SMSTOP_ZA:
102 svcr = ~SVCR_ZA_BIT;
103 break;
104
105 default:
106 ERROR("Illegal SMSTOP Instruction Variant\n");
107 break;
johpow0150ccb552020-11-10 19:22:13 -0600108 }
johpow0150ccb552020-11-10 19:22:13 -0600109 write_svcr(read_svcr() & svcr);
johpow0150ccb552020-11-10 19:22:13 -0600110
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000111 isb();
112}