1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2023 ARM Ltd.
6 #include <linux/jump_label.h>
7 #include <linux/memblock.h>
8 #include <linux/psci.h>
9 #include <linux/swiotlb.h>
10 #include <linux/cc_platform.h>
11 #include <linux/platform_device.h>
14 #include <asm/mem_encrypt.h>
17 static struct realm_config config;
19 unsigned long prot_ns_shared;
20 EXPORT_SYMBOL(prot_ns_shared);
22 DEFINE_STATIC_KEY_FALSE_RO(rsi_present);
23 EXPORT_SYMBOL(rsi_present);
25 bool cc_platform_has(enum cc_attr attr)
28 case CC_ATTR_MEM_ENCRYPT:
29 return is_realm_world();
34 EXPORT_SYMBOL_GPL(cc_platform_has);
36 static bool rsi_version_matches(void)
38 unsigned long ver_lower, ver_higher;
39 unsigned long ret = rsi_request_version(RSI_ABI_VERSION,
43 if (ret == SMCCC_RET_NOT_SUPPORTED)
46 if (ret != RSI_SUCCESS) {
47 pr_err("RME: RMM doesn't support RSI version %lu.%lu. Supported range: %lu.%lu-%lu.%lu\n",
48 RSI_ABI_VERSION_MAJOR, RSI_ABI_VERSION_MINOR,
49 RSI_ABI_VERSION_GET_MAJOR(ver_lower),
50 RSI_ABI_VERSION_GET_MINOR(ver_lower),
51 RSI_ABI_VERSION_GET_MAJOR(ver_higher),
52 RSI_ABI_VERSION_GET_MINOR(ver_higher));
56 pr_info("RME: Using RSI version %lu.%lu\n",
57 RSI_ABI_VERSION_GET_MAJOR(ver_lower),
58 RSI_ABI_VERSION_GET_MINOR(ver_lower));
63 static void __init arm64_rsi_setup_memory(void)
66 phys_addr_t start, end;
69 * Iterate over the available memory ranges and convert the state to
70 * protected memory. We should take extra care to ensure that we DO NOT
71 * permit any "DESTROYED" pages to be converted to "RAM".
73 * panic() is used because if the attempt to switch the memory to
74 * protected has failed here, then future accesses to the memory are
75 * simply going to be reflected as a SEA (Synchronous External Abort)
76 * which we can't handle. Bailing out early prevents the guest limping
79 for_each_mem_range(i, &start, &end) {
80 if (rsi_set_memory_range_protected_safe(start, end)) {
81 panic("Failed to set memory range to protected: %pa-%pa",
87 bool __arm64_is_protected_mmio(phys_addr_t base, size_t size)
93 if (WARN_ON(base + size <= base))
96 end = ALIGN(base + size, RSI_GRANULE_SIZE);
97 base = ALIGN_DOWN(base, RSI_GRANULE_SIZE);
100 if (WARN_ON(rsi_ipa_state_get(base, end, &ripas, &top)))
102 if (WARN_ON(top <= base))
104 if (ripas != RSI_RIPAS_DEV)
111 EXPORT_SYMBOL(__arm64_is_protected_mmio);
113 static int realm_ioremap_hook(phys_addr_t phys, size_t size, pgprot_t *prot)
115 if (__arm64_is_protected_mmio(phys, size))
116 *prot = pgprot_encrypted(*prot);
118 *prot = pgprot_decrypted(*prot);
123 void __init arm64_rsi_init(void)
125 if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_SMC)
127 if (!rsi_version_matches())
129 if (WARN_ON(rsi_get_realm_config(&config)))
131 prot_ns_shared = BIT(config.ipa_bits - 1);
133 if (arm64_ioremap_prot_hook_register(realm_ioremap_hook))
136 if (realm_register_memory_enc_ops())
139 arm64_rsi_setup_memory();
141 static_branch_enable(&rsi_present);
144 static struct platform_device rsi_dev = {
145 .name = RSI_PDEV_NAME,
146 .id = PLATFORM_DEVID_NONE
149 static int __init arm64_create_dummy_rsi_dev(void)
151 if (is_realm_world() &&
152 platform_device_register(&rsi_dev))
153 pr_err("failed to register rsi platform device\n");
157 arch_initcall(arm64_create_dummy_rsi_dev)