rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * runtime-wrappers.c - Runtime Services function call wrappers |
| 3 | * |
| 4 | * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org> |
| 5 | * |
| 6 | * Split off from arch/x86/platform/efi/efi.c |
| 7 | * |
| 8 | * Copyright (C) 1999 VA Linux Systems |
| 9 | * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> |
| 10 | * Copyright (C) 1999-2002 Hewlett-Packard Co. |
| 11 | * Copyright (C) 2005-2008 Intel Co. |
| 12 | * Copyright (C) 2013 SuSE Labs |
| 13 | * |
| 14 | * This file is released under the GPLv2. |
| 15 | */ |
| 16 | |
| 17 | #define pr_fmt(fmt) "efi: " fmt |
| 18 | |
| 19 | #include <linux/bug.h> |
| 20 | #include <linux/efi.h> |
| 21 | #include <linux/irqflags.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/semaphore.h> |
| 24 | #include <linux/stringify.h> |
| 25 | #include <asm/efi.h> |
| 26 | |
| 27 | /* |
| 28 | * Wrap around the new efi_call_virt_generic() macros so that the |
| 29 | * code doesn't get too cluttered: |
| 30 | */ |
| 31 | #define efi_call_virt(f, args...) \ |
| 32 | efi_call_virt_pointer(efi.systab->runtime, f, args) |
| 33 | #define __efi_call_virt(f, args...) \ |
| 34 | __efi_call_virt_pointer(efi.systab->runtime, f, args) |
| 35 | |
| 36 | void efi_call_virt_check_flags(unsigned long flags, const char *call) |
| 37 | { |
| 38 | unsigned long cur_flags, mismatch; |
| 39 | |
| 40 | local_save_flags(cur_flags); |
| 41 | |
| 42 | mismatch = flags ^ cur_flags; |
| 43 | if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK)) |
| 44 | return; |
| 45 | |
| 46 | add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE); |
| 47 | pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n", |
| 48 | flags, cur_flags, call); |
| 49 | local_irq_restore(flags); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Expose the EFI runtime lock to the UV platform |
| 54 | */ |
| 55 | #ifdef CONFIG_X86_UV |
| 56 | extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock); |
| 57 | #endif |
| 58 | |
| 59 | /* |
| 60 | * According to section 7.1 of the UEFI spec, Runtime Services are not fully |
| 61 | * reentrant, and there are particular combinations of calls that need to be |
| 62 | * serialized. (source: UEFI Specification v2.4A) |
| 63 | * |
| 64 | * Table 31. Rules for Reentry Into Runtime Services |
| 65 | * +------------------------------------+-------------------------------+ |
| 66 | * | If previous call is busy in | Forbidden to call | |
| 67 | * +------------------------------------+-------------------------------+ |
| 68 | * | Any | SetVirtualAddressMap() | |
| 69 | * +------------------------------------+-------------------------------+ |
| 70 | * | ConvertPointer() | ConvertPointer() | |
| 71 | * +------------------------------------+-------------------------------+ |
| 72 | * | SetVariable() | ResetSystem() | |
| 73 | * | UpdateCapsule() | | |
| 74 | * | SetTime() | | |
| 75 | * | SetWakeupTime() | | |
| 76 | * | GetNextHighMonotonicCount() | | |
| 77 | * +------------------------------------+-------------------------------+ |
| 78 | * | GetVariable() | GetVariable() | |
| 79 | * | GetNextVariableName() | GetNextVariableName() | |
| 80 | * | SetVariable() | SetVariable() | |
| 81 | * | QueryVariableInfo() | QueryVariableInfo() | |
| 82 | * | UpdateCapsule() | UpdateCapsule() | |
| 83 | * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() | |
| 84 | * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() | |
| 85 | * +------------------------------------+-------------------------------+ |
| 86 | * | GetTime() | GetTime() | |
| 87 | * | SetTime() | SetTime() | |
| 88 | * | GetWakeupTime() | GetWakeupTime() | |
| 89 | * | SetWakeupTime() | SetWakeupTime() | |
| 90 | * +------------------------------------+-------------------------------+ |
| 91 | * |
| 92 | * Due to the fact that the EFI pstore may write to the variable store in |
| 93 | * interrupt context, we need to use a lock for at least the groups that |
| 94 | * contain SetVariable() and QueryVariableInfo(). That leaves little else, as |
| 95 | * none of the remaining functions are actually ever called at runtime. |
| 96 | * So let's just use a single lock to serialize all Runtime Services calls. |
| 97 | */ |
| 98 | static DEFINE_SEMAPHORE(efi_runtime_lock); |
| 99 | |
| 100 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
| 101 | { |
| 102 | efi_status_t status; |
| 103 | |
| 104 | if (down_interruptible(&efi_runtime_lock)) |
| 105 | return EFI_ABORTED; |
| 106 | status = efi_call_virt(get_time, tm, tc); |
| 107 | up(&efi_runtime_lock); |
| 108 | return status; |
| 109 | } |
| 110 | |
| 111 | static efi_status_t virt_efi_set_time(efi_time_t *tm) |
| 112 | { |
| 113 | efi_status_t status; |
| 114 | |
| 115 | if (down_interruptible(&efi_runtime_lock)) |
| 116 | return EFI_ABORTED; |
| 117 | status = efi_call_virt(set_time, tm); |
| 118 | up(&efi_runtime_lock); |
| 119 | return status; |
| 120 | } |
| 121 | |
| 122 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, |
| 123 | efi_bool_t *pending, |
| 124 | efi_time_t *tm) |
| 125 | { |
| 126 | efi_status_t status; |
| 127 | |
| 128 | if (down_interruptible(&efi_runtime_lock)) |
| 129 | return EFI_ABORTED; |
| 130 | status = efi_call_virt(get_wakeup_time, enabled, pending, tm); |
| 131 | up(&efi_runtime_lock); |
| 132 | return status; |
| 133 | } |
| 134 | |
| 135 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
| 136 | { |
| 137 | efi_status_t status; |
| 138 | |
| 139 | if (down_interruptible(&efi_runtime_lock)) |
| 140 | return EFI_ABORTED; |
| 141 | status = efi_call_virt(set_wakeup_time, enabled, tm); |
| 142 | up(&efi_runtime_lock); |
| 143 | return status; |
| 144 | } |
| 145 | |
| 146 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, |
| 147 | efi_guid_t *vendor, |
| 148 | u32 *attr, |
| 149 | unsigned long *data_size, |
| 150 | void *data) |
| 151 | { |
| 152 | efi_status_t status; |
| 153 | |
| 154 | if (down_interruptible(&efi_runtime_lock)) |
| 155 | return EFI_ABORTED; |
| 156 | status = efi_call_virt(get_variable, name, vendor, attr, data_size, |
| 157 | data); |
| 158 | up(&efi_runtime_lock); |
| 159 | return status; |
| 160 | } |
| 161 | |
| 162 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, |
| 163 | efi_char16_t *name, |
| 164 | efi_guid_t *vendor) |
| 165 | { |
| 166 | efi_status_t status; |
| 167 | |
| 168 | if (down_interruptible(&efi_runtime_lock)) |
| 169 | return EFI_ABORTED; |
| 170 | status = efi_call_virt(get_next_variable, name_size, name, vendor); |
| 171 | up(&efi_runtime_lock); |
| 172 | return status; |
| 173 | } |
| 174 | |
| 175 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, |
| 176 | efi_guid_t *vendor, |
| 177 | u32 attr, |
| 178 | unsigned long data_size, |
| 179 | void *data) |
| 180 | { |
| 181 | efi_status_t status; |
| 182 | |
| 183 | if (down_interruptible(&efi_runtime_lock)) |
| 184 | return EFI_ABORTED; |
| 185 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, |
| 186 | data); |
| 187 | up(&efi_runtime_lock); |
| 188 | return status; |
| 189 | } |
| 190 | |
| 191 | static efi_status_t |
| 192 | virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor, |
| 193 | u32 attr, unsigned long data_size, |
| 194 | void *data) |
| 195 | { |
| 196 | efi_status_t status; |
| 197 | |
| 198 | if (down_trylock(&efi_runtime_lock)) |
| 199 | return EFI_NOT_READY; |
| 200 | |
| 201 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, |
| 202 | data); |
| 203 | up(&efi_runtime_lock); |
| 204 | return status; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
| 209 | u64 *storage_space, |
| 210 | u64 *remaining_space, |
| 211 | u64 *max_variable_size) |
| 212 | { |
| 213 | efi_status_t status; |
| 214 | |
| 215 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 216 | return EFI_UNSUPPORTED; |
| 217 | |
| 218 | if (down_interruptible(&efi_runtime_lock)) |
| 219 | return EFI_ABORTED; |
| 220 | status = efi_call_virt(query_variable_info, attr, storage_space, |
| 221 | remaining_space, max_variable_size); |
| 222 | up(&efi_runtime_lock); |
| 223 | return status; |
| 224 | } |
| 225 | |
| 226 | static efi_status_t |
| 227 | virt_efi_query_variable_info_nonblocking(u32 attr, |
| 228 | u64 *storage_space, |
| 229 | u64 *remaining_space, |
| 230 | u64 *max_variable_size) |
| 231 | { |
| 232 | efi_status_t status; |
| 233 | |
| 234 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 235 | return EFI_UNSUPPORTED; |
| 236 | |
| 237 | if (down_trylock(&efi_runtime_lock)) |
| 238 | return EFI_NOT_READY; |
| 239 | |
| 240 | status = efi_call_virt(query_variable_info, attr, storage_space, |
| 241 | remaining_space, max_variable_size); |
| 242 | up(&efi_runtime_lock); |
| 243 | return status; |
| 244 | } |
| 245 | |
| 246 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
| 247 | { |
| 248 | efi_status_t status; |
| 249 | |
| 250 | if (down_interruptible(&efi_runtime_lock)) |
| 251 | return EFI_ABORTED; |
| 252 | status = efi_call_virt(get_next_high_mono_count, count); |
| 253 | up(&efi_runtime_lock); |
| 254 | return status; |
| 255 | } |
| 256 | |
| 257 | static void virt_efi_reset_system(int reset_type, |
| 258 | efi_status_t status, |
| 259 | unsigned long data_size, |
| 260 | efi_char16_t *data) |
| 261 | { |
| 262 | if (down_interruptible(&efi_runtime_lock)) { |
| 263 | pr_warn("failed to invoke the reset_system() runtime service:\n" |
| 264 | "could not get exclusive access to the firmware\n"); |
| 265 | return; |
| 266 | } |
| 267 | __efi_call_virt(reset_system, reset_type, status, data_size, data); |
| 268 | up(&efi_runtime_lock); |
| 269 | } |
| 270 | |
| 271 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
| 272 | unsigned long count, |
| 273 | unsigned long sg_list) |
| 274 | { |
| 275 | efi_status_t status; |
| 276 | |
| 277 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 278 | return EFI_UNSUPPORTED; |
| 279 | |
| 280 | if (down_interruptible(&efi_runtime_lock)) |
| 281 | return EFI_ABORTED; |
| 282 | status = efi_call_virt(update_capsule, capsules, count, sg_list); |
| 283 | up(&efi_runtime_lock); |
| 284 | return status; |
| 285 | } |
| 286 | |
| 287 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, |
| 288 | unsigned long count, |
| 289 | u64 *max_size, |
| 290 | int *reset_type) |
| 291 | { |
| 292 | efi_status_t status; |
| 293 | |
| 294 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 295 | return EFI_UNSUPPORTED; |
| 296 | |
| 297 | if (down_interruptible(&efi_runtime_lock)) |
| 298 | return EFI_ABORTED; |
| 299 | status = efi_call_virt(query_capsule_caps, capsules, count, max_size, |
| 300 | reset_type); |
| 301 | up(&efi_runtime_lock); |
| 302 | return status; |
| 303 | } |
| 304 | |
| 305 | void efi_native_runtime_setup(void) |
| 306 | { |
| 307 | efi.get_time = virt_efi_get_time; |
| 308 | efi.set_time = virt_efi_set_time; |
| 309 | efi.get_wakeup_time = virt_efi_get_wakeup_time; |
| 310 | efi.set_wakeup_time = virt_efi_set_wakeup_time; |
| 311 | efi.get_variable = virt_efi_get_variable; |
| 312 | efi.get_next_variable = virt_efi_get_next_variable; |
| 313 | efi.set_variable = virt_efi_set_variable; |
| 314 | efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking; |
| 315 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
| 316 | efi.reset_system = virt_efi_reset_system; |
| 317 | efi.query_variable_info = virt_efi_query_variable_info; |
| 318 | efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking; |
| 319 | efi.update_capsule = virt_efi_update_capsule; |
| 320 | efi.query_capsule_caps = virt_efi_query_capsule_caps; |
| 321 | } |