1 // SPDX-License-Identifier: GPL-2.0-only
3 * runtime-wrappers.c - Runtime Services function call wrappers
5 * Implementation summary:
6 * -----------------------
7 * 1. When user/kernel thread requests to execute efi_runtime_service(),
8 * enqueue work to efi_rts_wq.
9 * 2. Caller thread waits for completion until the work is finished
10 * because it's dependent on the return status and execution of
11 * efi_runtime_service().
12 * For instance, get_variable() and get_next_variable().
16 * Split off from arch/x86/platform/efi/efi.c
18 * Copyright (C) 1999 VA Linux Systems
20 * Copyright (C) 1999-2002 Hewlett-Packard Co.
21 * Copyright (C) 2005-2008 Intel Co.
22 * Copyright (C) 2013 SuSE Labs
25 #define pr_fmt(fmt) "efi: " fmt
27 #include <linux/bug.h>
28 #include <linux/efi.h>
29 #include <linux/irqflags.h>
30 #include <linux/mutex.h>
31 #include <linux/semaphore.h>
32 #include <linux/stringify.h>
33 #include <linux/workqueue.h>
34 #include <linux/completion.h>
39 * Wrap around the new efi_call_virt_generic() macros so that the
40 * code doesn't get too cluttered:
42 #define efi_call_virt(f, args...) \
43 efi_call_virt_pointer(efi.runtime, f, args)
44 #define __efi_call_virt(f, args...) \
45 __efi_call_virt_pointer(efi.runtime, f, args)
47 struct efi_runtime_work efi_rts_work;
50 * efi_queue_work: Queue efi_runtime_service() and wait until it's done
51 * @rts: efi_runtime_service() function identifier
52 * @rts_arg<1-5>: efi_runtime_service() function arguments
54 * Accesses to efi_runtime_services() are serialized by a binary
55 * semaphore (efi_runtime_lock) and caller waits until the work is
56 * finished, hence _only_ one work is queued at a time and the caller
57 * thread waits for completion.
59 #define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \
61 efi_rts_work.status = EFI_ABORTED; \
63 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { \
64 pr_warn_once("EFI Runtime Services are disabled!\n"); \
65 efi_rts_work.status = EFI_DEVICE_ERROR; \
69 init_completion(&efi_rts_work.efi_rts_comp); \
70 INIT_WORK(&efi_rts_work.work, efi_call_rts); \
71 efi_rts_work.arg1 = _arg1; \
72 efi_rts_work.arg2 = _arg2; \
73 efi_rts_work.arg3 = _arg3; \
74 efi_rts_work.arg4 = _arg4; \
75 efi_rts_work.arg5 = _arg5; \
76 efi_rts_work.efi_rts_id = _rts; \
79 * queue_work() returns 0 if work was already on queue, \
80 * _ideally_ this should never happen. \
82 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
83 wait_for_completion(&efi_rts_work.efi_rts_comp); \
85 pr_err("Failed to queue work to efi_rts_wq.\n"); \
87 WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED); \
89 efi_rts_work.efi_rts_id = EFI_NONE; \
90 efi_rts_work.status; \
93 #ifndef arch_efi_save_flags
94 #define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
95 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
98 unsigned long efi_call_virt_save_flags(void)
102 arch_efi_save_flags(flags);
106 void efi_call_virt_check_flags(unsigned long flags, const char *call)
108 unsigned long cur_flags, mismatch;
110 cur_flags = efi_call_virt_save_flags();
112 mismatch = flags ^ cur_flags;
113 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
116 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
117 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
118 flags, cur_flags, call);
119 arch_efi_restore_flags(flags);
123 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
124 * reentrant, and there are particular combinations of calls that need to be
125 * serialized. (source: UEFI Specification v2.4A)
127 * Table 31. Rules for Reentry Into Runtime Services
128 * +------------------------------------+-------------------------------+
129 * | If previous call is busy in | Forbidden to call |
130 * +------------------------------------+-------------------------------+
131 * | Any | SetVirtualAddressMap() |
132 * +------------------------------------+-------------------------------+
133 * | ConvertPointer() | ConvertPointer() |
134 * +------------------------------------+-------------------------------+
135 * | SetVariable() | ResetSystem() |
136 * | UpdateCapsule() | |
138 * | SetWakeupTime() | |
139 * | GetNextHighMonotonicCount() | |
140 * +------------------------------------+-------------------------------+
141 * | GetVariable() | GetVariable() |
142 * | GetNextVariableName() | GetNextVariableName() |
143 * | SetVariable() | SetVariable() |
144 * | QueryVariableInfo() | QueryVariableInfo() |
145 * | UpdateCapsule() | UpdateCapsule() |
146 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
147 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
148 * +------------------------------------+-------------------------------+
149 * | GetTime() | GetTime() |
150 * | SetTime() | SetTime() |
151 * | GetWakeupTime() | GetWakeupTime() |
152 * | SetWakeupTime() | SetWakeupTime() |
153 * +------------------------------------+-------------------------------+
155 * Due to the fact that the EFI pstore may write to the variable store in
156 * interrupt context, we need to use a lock for at least the groups that
157 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
158 * none of the remaining functions are actually ever called at runtime.
159 * So let's just use a single lock to serialize all Runtime Services calls.
161 static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
164 * Expose the EFI runtime lock to the UV platform
167 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
171 * Calls the appropriate efi_runtime_service() with the appropriate
174 * Semantics followed by efi_call_rts() to understand efi_runtime_work:
175 * 1. If argument was a pointer, recast it from void pointer to original
177 * 2. If argument was a value, recast it from void pointer to original
178 * pointer type and dereference it.
180 static void efi_call_rts(struct work_struct *work)
182 void *arg1, *arg2, *arg3, *arg4, *arg5;
183 efi_status_t status = EFI_NOT_FOUND;
185 arg1 = efi_rts_work.arg1;
186 arg2 = efi_rts_work.arg2;
187 arg3 = efi_rts_work.arg3;
188 arg4 = efi_rts_work.arg4;
189 arg5 = efi_rts_work.arg5;
191 switch (efi_rts_work.efi_rts_id) {
193 status = efi_call_virt(get_time, (efi_time_t *)arg1,
194 (efi_time_cap_t *)arg2);
197 status = efi_call_virt(set_time, (efi_time_t *)arg1);
199 case EFI_GET_WAKEUP_TIME:
200 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
201 (efi_bool_t *)arg2, (efi_time_t *)arg3);
203 case EFI_SET_WAKEUP_TIME:
204 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
207 case EFI_GET_VARIABLE:
208 status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
209 (efi_guid_t *)arg2, (u32 *)arg3,
210 (unsigned long *)arg4, (void *)arg5);
212 case EFI_GET_NEXT_VARIABLE:
213 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
214 (efi_char16_t *)arg2,
217 case EFI_SET_VARIABLE:
218 status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
219 (efi_guid_t *)arg2, *(u32 *)arg3,
220 *(unsigned long *)arg4, (void *)arg5);
222 case EFI_QUERY_VARIABLE_INFO:
223 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
224 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
226 case EFI_GET_NEXT_HIGH_MONO_COUNT:
227 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
229 case EFI_UPDATE_CAPSULE:
230 status = efi_call_virt(update_capsule,
231 (efi_capsule_header_t **)arg1,
232 *(unsigned long *)arg2,
233 *(unsigned long *)arg3);
235 case EFI_QUERY_CAPSULE_CAPS:
236 status = efi_call_virt(query_capsule_caps,
237 (efi_capsule_header_t **)arg1,
238 *(unsigned long *)arg2, (u64 *)arg3,
243 * Ideally, we should never reach here because a caller of this
244 * function should have put the right efi_runtime_service()
245 * function identifier into efi_rts_work->efi_rts_id
247 pr_err("Requested executing invalid EFI Runtime Service.\n");
249 efi_rts_work.status = status;
250 complete(&efi_rts_work.efi_rts_comp);
253 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
257 if (down_interruptible(&efi_runtime_lock))
259 status = efi_queue_work(EFI_GET_TIME, tm, tc, NULL, NULL, NULL);
260 up(&efi_runtime_lock);
264 static efi_status_t virt_efi_set_time(efi_time_t *tm)
268 if (down_interruptible(&efi_runtime_lock))
270 status = efi_queue_work(EFI_SET_TIME, tm, NULL, NULL, NULL, NULL);
271 up(&efi_runtime_lock);
275 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
281 if (down_interruptible(&efi_runtime_lock))
283 status = efi_queue_work(EFI_GET_WAKEUP_TIME, enabled, pending, tm, NULL,
285 up(&efi_runtime_lock);
289 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
293 if (down_interruptible(&efi_runtime_lock))
295 status = efi_queue_work(EFI_SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
297 up(&efi_runtime_lock);
301 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
304 unsigned long *data_size,
309 if (down_interruptible(&efi_runtime_lock))
311 status = efi_queue_work(EFI_GET_VARIABLE, name, vendor, attr, data_size,
313 up(&efi_runtime_lock);
317 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
323 if (down_interruptible(&efi_runtime_lock))
325 status = efi_queue_work(EFI_GET_NEXT_VARIABLE, name_size, name, vendor,
327 up(&efi_runtime_lock);
331 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
334 unsigned long data_size,
339 if (down_interruptible(&efi_runtime_lock))
341 status = efi_queue_work(EFI_SET_VARIABLE, name, vendor, &attr, &data_size,
343 up(&efi_runtime_lock);
348 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
349 u32 attr, unsigned long data_size,
354 if (down_trylock(&efi_runtime_lock))
355 return EFI_NOT_READY;
357 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
359 up(&efi_runtime_lock);
364 static efi_status_t virt_efi_query_variable_info(u32 attr,
366 u64 *remaining_space,
367 u64 *max_variable_size)
371 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
372 return EFI_UNSUPPORTED;
374 if (down_interruptible(&efi_runtime_lock))
376 status = efi_queue_work(EFI_QUERY_VARIABLE_INFO, &attr, storage_space,
377 remaining_space, max_variable_size, NULL);
378 up(&efi_runtime_lock);
383 virt_efi_query_variable_info_nonblocking(u32 attr,
385 u64 *remaining_space,
386 u64 *max_variable_size)
390 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
391 return EFI_UNSUPPORTED;
393 if (down_trylock(&efi_runtime_lock))
394 return EFI_NOT_READY;
396 status = efi_call_virt(query_variable_info, attr, storage_space,
397 remaining_space, max_variable_size);
398 up(&efi_runtime_lock);
402 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
406 if (down_interruptible(&efi_runtime_lock))
408 status = efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
410 up(&efi_runtime_lock);
414 static void virt_efi_reset_system(int reset_type,
416 unsigned long data_size,
419 if (down_trylock(&efi_runtime_lock)) {
420 pr_warn("failed to invoke the reset_system() runtime service:\n"
421 "could not get exclusive access to the firmware\n");
424 efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
425 __efi_call_virt(reset_system, reset_type, status, data_size, data);
426 up(&efi_runtime_lock);
429 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
431 unsigned long sg_list)
435 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
436 return EFI_UNSUPPORTED;
438 if (down_interruptible(&efi_runtime_lock))
440 status = efi_queue_work(EFI_UPDATE_CAPSULE, capsules, &count, &sg_list,
442 up(&efi_runtime_lock);
446 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
453 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
454 return EFI_UNSUPPORTED;
456 if (down_interruptible(&efi_runtime_lock))
458 status = efi_queue_work(EFI_QUERY_CAPSULE_CAPS, capsules, &count,
459 max_size, reset_type, NULL);
460 up(&efi_runtime_lock);
464 void efi_native_runtime_setup(void)
466 efi.get_time = virt_efi_get_time;
467 efi.set_time = virt_efi_set_time;
468 efi.get_wakeup_time = virt_efi_get_wakeup_time;
469 efi.set_wakeup_time = virt_efi_set_wakeup_time;
470 efi.get_variable = virt_efi_get_variable;
471 efi.get_next_variable = virt_efi_get_next_variable;
472 efi.set_variable = virt_efi_set_variable;
473 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
474 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
475 efi.reset_system = virt_efi_reset_system;
476 efi.query_variable_info = virt_efi_query_variable_info;
477 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
478 efi.update_capsule = virt_efi_update_capsule;
479 efi.query_capsule_caps = virt_efi_query_capsule_caps;