2 * runtime-wrappers.c - Runtime Services function call wrappers
4 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6 * Split off from arch/x86/platform/efi/efi.c
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
14 * This file is released under the GPLv2.
17 #define pr_fmt(fmt) "efi: " fmt
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>
28 * Wrap around the new efi_call_virt_generic() macros so that the
29 * code doesn't get too cluttered:
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)
36 void efi_call_virt_check_flags(unsigned long flags, const char *call)
38 unsigned long cur_flags, mismatch;
40 local_save_flags(cur_flags);
42 mismatch = flags ^ cur_flags;
43 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
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);
53 * Expose the EFI runtime lock to the UV platform
56 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
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)
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() | |
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 * +------------------------------------+-------------------------------+
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.
98 static DEFINE_SEMAPHORE(efi_runtime_lock);
100 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
104 if (down_interruptible(&efi_runtime_lock))
106 status = efi_call_virt(get_time, tm, tc);
107 up(&efi_runtime_lock);
111 static efi_status_t virt_efi_set_time(efi_time_t *tm)
115 if (down_interruptible(&efi_runtime_lock))
117 status = efi_call_virt(set_time, tm);
118 up(&efi_runtime_lock);
122 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
128 if (down_interruptible(&efi_runtime_lock))
130 status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
131 up(&efi_runtime_lock);
135 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
139 if (down_interruptible(&efi_runtime_lock))
141 status = efi_call_virt(set_wakeup_time, enabled, tm);
142 up(&efi_runtime_lock);
146 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
149 unsigned long *data_size,
154 if (down_interruptible(&efi_runtime_lock))
156 status = efi_call_virt(get_variable, name, vendor, attr, data_size,
158 up(&efi_runtime_lock);
162 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
168 if (down_interruptible(&efi_runtime_lock))
170 status = efi_call_virt(get_next_variable, name_size, name, vendor);
171 up(&efi_runtime_lock);
175 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
178 unsigned long data_size,
183 if (down_interruptible(&efi_runtime_lock))
185 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
187 up(&efi_runtime_lock);
192 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
193 u32 attr, unsigned long data_size,
198 if (down_trylock(&efi_runtime_lock))
199 return EFI_NOT_READY;
201 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
203 up(&efi_runtime_lock);
208 static efi_status_t virt_efi_query_variable_info(u32 attr,
210 u64 *remaining_space,
211 u64 *max_variable_size)
215 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
216 return EFI_UNSUPPORTED;
218 if (down_interruptible(&efi_runtime_lock))
220 status = efi_call_virt(query_variable_info, attr, storage_space,
221 remaining_space, max_variable_size);
222 up(&efi_runtime_lock);
227 virt_efi_query_variable_info_nonblocking(u32 attr,
229 u64 *remaining_space,
230 u64 *max_variable_size)
234 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
235 return EFI_UNSUPPORTED;
237 if (down_trylock(&efi_runtime_lock))
238 return EFI_NOT_READY;
240 status = efi_call_virt(query_variable_info, attr, storage_space,
241 remaining_space, max_variable_size);
242 up(&efi_runtime_lock);
246 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
250 if (down_interruptible(&efi_runtime_lock))
252 status = efi_call_virt(get_next_high_mono_count, count);
253 up(&efi_runtime_lock);
257 static void virt_efi_reset_system(int reset_type,
259 unsigned long data_size,
262 if (down_trylock(&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");
267 __efi_call_virt(reset_system, reset_type, status, data_size, data);
268 up(&efi_runtime_lock);
271 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
273 unsigned long sg_list)
277 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
278 return EFI_UNSUPPORTED;
280 if (down_interruptible(&efi_runtime_lock))
282 status = efi_call_virt(update_capsule, capsules, count, sg_list);
283 up(&efi_runtime_lock);
287 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
294 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
295 return EFI_UNSUPPORTED;
297 if (down_interruptible(&efi_runtime_lock))
299 status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
301 up(&efi_runtime_lock);
305 void efi_native_runtime_setup(void)
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;