|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (C) 2023 ARM Ltd. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <linux/jump_label.h> |
| 7 | +#include <linux/memblock.h> |
| 8 | +#include <linux/psci.h> |
| 9 | +#include <asm/rsi.h> |
| 10 | + |
| 11 | +DEFINE_STATIC_KEY_FALSE_RO(rsi_present); |
| 12 | +EXPORT_SYMBOL(rsi_present); |
| 13 | + |
| 14 | +static bool rsi_version_matches(void) |
| 15 | +{ |
| 16 | + unsigned long ver_lower, ver_higher; |
| 17 | + unsigned long ret = rsi_request_version(RSI_ABI_VERSION, |
| 18 | + &ver_lower, |
| 19 | + &ver_higher); |
| 20 | + |
| 21 | + if (ret == SMCCC_RET_NOT_SUPPORTED) |
| 22 | + return false; |
| 23 | + |
| 24 | + if (ret != RSI_SUCCESS) { |
| 25 | + pr_err("RME: RMM doesn't support RSI version %lu.%lu. Supported range: %lu.%lu-%lu.%lu\n", |
| 26 | + RSI_ABI_VERSION_MAJOR, RSI_ABI_VERSION_MINOR, |
| 27 | + RSI_ABI_VERSION_GET_MAJOR(ver_lower), |
| 28 | + RSI_ABI_VERSION_GET_MINOR(ver_lower), |
| 29 | + RSI_ABI_VERSION_GET_MAJOR(ver_higher), |
| 30 | + RSI_ABI_VERSION_GET_MINOR(ver_higher)); |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + pr_info("RME: Using RSI version %lu.%lu\n", |
| 35 | + RSI_ABI_VERSION_GET_MAJOR(ver_lower), |
| 36 | + RSI_ABI_VERSION_GET_MINOR(ver_lower)); |
| 37 | + |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +static void __init arm64_rsi_setup_memory(void) |
| 42 | +{ |
| 43 | + u64 i; |
| 44 | + phys_addr_t start, end; |
| 45 | + |
| 46 | + /* |
| 47 | + * Iterate over the available memory ranges and convert the state to |
| 48 | + * protected memory. We should take extra care to ensure that we DO NOT |
| 49 | + * permit any "DESTROYED" pages to be converted to "RAM". |
| 50 | + * |
| 51 | + * panic() is used because if the attempt to switch the memory to |
| 52 | + * protected has failed here, then future accesses to the memory are |
| 53 | + * simply going to be reflected as a SEA (Synchronous External Abort) |
| 54 | + * which we can't handle. Bailing out early prevents the guest limping |
| 55 | + * on and dying later. |
| 56 | + */ |
| 57 | + for_each_mem_range(i, &start, &end) { |
| 58 | + if (rsi_set_memory_range_protected_safe(start, end)) { |
| 59 | + panic("Failed to set memory range to protected: %pa-%pa", |
| 60 | + &start, &end); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void __init arm64_rsi_init(void) |
| 66 | +{ |
| 67 | + if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_SMC) |
| 68 | + return; |
| 69 | + if (!rsi_version_matches()) |
| 70 | + return; |
| 71 | + |
| 72 | + arm64_rsi_setup_memory(); |
| 73 | + |
| 74 | + static_branch_enable(&rsi_present); |
| 75 | +} |
| 76 | + |
0 commit comments