| /* |
| * SPDX-License-Identifier: BSD-3-Clause |
| * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| */ |
| |
| #include <arch_features.h> |
| #include <assert.h> |
| #include <atomics.h> |
| #include <sizes.h> |
| #include <mec.h> |
| #include <smc-rmi.h> |
| #include <smc.h> |
| #include <stdint.h> |
| #include <rmm_el3_ifc.h> |
| |
| #define MECID_WIDTH U(16) |
| #define MEC_MAX_COUNT (U(1) << MECID_WIDTH) |
| |
| #define MECID_INVALID (-1U) |
| |
| #define MECID_ARRAY_SIZE ((MEC_MAX_COUNT) / BITS_PER_UL) |
| |
| /* |
| * Before enabling the MMU, the RMM code is written and read with MECID 0, so |
| * the only possible value at the moment for MECID_SYSTEM is 0. |
| */ |
| #define MECID_SYSTEM 0 |
| |
| /* |
| * Together, the mec_reserved array and the shared_mec value define the |
| * state of all MECs in the system. |
| * |
| * For a given mecid: |
| * |
| * if mecid == shared_mec |
| * MEC state is SHARED |
| * else |
| * if mec_reserved[mecid] == true |
| * MEC state is PRIVATE_ASSIGNED |
| * else |
| * MEC state is PRIVATE_UNASSIGNED |
| */ |
| |
| static unsigned int mecid_count; |
| static unsigned int shared_mec_members; |
| static unsigned int shared_mec = MECID_INVALID; |
| |
| /* |
| * The bitmap for the reserved/used MECID values. |
| */ |
| static unsigned long mec_reserved[MECID_ARRAY_SIZE]; |
| |
| /* Maximum MECID allocatable for Realms */ |
| unsigned int mecid_max(void) |
| { |
| return mecid_count ? (mecid_count - 1) : 0; |
| } |
| |
| bool mec_reserve(unsigned int mecid) |
| { |
| unsigned int offset, bit; |
| |
| assert (mecid <= mecid_max()); |
| |
| offset = mecid / BITS_PER_UL; |
| bit = mecid % BITS_PER_UL; |
| |
| if (!atomic_bit_set_acquire_release_64(&mec_reserved[offset], bit)) { |
| rmm_el3_ifc_allocate_mec_key(mecid); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| bool mec_release(unsigned int mecid) |
| { |
| unsigned int offset, bit; |
| |
| assert (mecid <= mecid_max()); |
| |
| rmm_el3_ifc_release_mec_key(mecid); |
| |
| offset = mecid / BITS_PER_UL; |
| bit = mecid % BITS_PER_UL; |
| atomic_bit_clear_release_64(&mec_reserved[offset], bit); |
| |
| return true; |
| } |
| |
| unsigned long smc_mec_set_shared(unsigned long mecid) |
| { |
| if (!is_feat_mec_present()) { |
| return RMI_ERROR_INPUT; |
| } |
| |
| /* |
| * Check for the requested MECID state, if its not already |
| * shared state then to create a new shared MECID, make sure |
| * there is no realm with existing shared MECID. |
| */ |
| |
| if (mecid > mecid_max()) { |
| return RMI_ERROR_INPUT; |
| } |
| |
| if (shared_mec == MECID_INVALID && mec_reserve(mecid)) { |
| shared_mec = mecid; |
| return RMI_SUCCESS; |
| } |
| |
| return RMI_ERROR_INPUT; |
| } |
| |
| unsigned long smc_mec_set_private(unsigned long mecid) |
| { |
| if (!is_feat_mec_present()) { |
| return RMI_ERROR_INPUT; |
| } |
| |
| if (mecid > mecid_max()) { |
| return RMI_ERROR_INPUT; |
| } |
| |
| if (shared_mec_members == 0 && mecid == shared_mec) { |
| mec_release(mecid); |
| shared_mec = MECID_INVALID; |
| return RMI_SUCCESS; |
| } |
| |
| return RMI_ERROR_INPUT; |
| } |
| |
| bool mec_assign(unsigned int mecid) |
| { |
| if (!is_feat_mec_present()) { |
| return true; |
| } |
| |
| assert (mecid <= mecid_max()); |
| |
| if (mecid == shared_mec) { |
| assert (shared_mec_members < UINT32_MAX); |
| shared_mec_members++; |
| return true; |
| } |
| |
| return mec_reserve(mecid); |
| } |
| |
| bool mec_unassign(unsigned int mecid) |
| { |
| if (!is_feat_mec_present()) { |
| return true; |
| } |
| |
| assert (mecid <= mecid_max()); |
| |
| if (mecid == shared_mec) { |
| if (shared_mec_members > 0) { |
| shared_mec_members--; |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| mec_release(mecid); |
| return true; |
| } |
| |
| void mec_init_mmu(void) |
| { |
| uint16_t mecid; |
| unsigned int offset, bit; |
| |
| if (!is_feat_mec_present()) { |
| return; |
| } |
| |
| /* Assume all CPUs have the same MECID size */ |
| mecid_count = 1U << (EXTRACT(MECIDR_MECIDWIDTHM1, read_mecidr_el2()) + 1); |
| |
| mecid = MECID_SYSTEM; |
| offset = mecid / BITS_PER_UL; |
| bit = mecid % BITS_PER_UL; |
| /* Early alloc, MMU is still disabled */ |
| mec_reserved[offset] |= (1ULL << bit); |
| |
| /* MECID_* reset to UNKNOWN values */ |
| write_mecid_p0_el2(mecid); |
| write_mecid_p1_el2(mecid); |
| write_mecid_a0_el2(mecid); |
| write_mecid_a1_el2(mecid); |
| isb(); |
| |
| write_sctlr2_el2(read_sctlr2_el2() | SCTLR2_ELx_EMEC_BIT); |
| } |