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().
14 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
16 * Split off from arch/x86/platform/efi/efi.c
18 * Copyright (C) 1999 VA Linux Systems
19 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
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 arch_efi_call_virt(efi.runtime, f, args)
48 efi_time_cap_t *capabilities;
70 unsigned long *data_size;
75 unsigned long *name_size;
84 unsigned long data_size;
92 u64 *max_variable_size;
93 } QUERY_VARIABLE_INFO;
97 } GET_NEXT_HIGH_MONO_COUNT;
100 efi_capsule_header_t **capsules;
102 unsigned long sg_list;
106 efi_capsule_header_t **capsules;
110 } QUERY_CAPSULE_CAPS;
113 efi_status_t (__efiapi *acpi_prm_handler)(u64, void *);
114 u64 param_buffer_addr;
119 struct efi_runtime_work efi_rts_work;
122 * efi_queue_work: Queue EFI runtime service call and wait for completion
123 * @_rts: EFI runtime service function identifier
124 * @_args: Arguments to pass to the EFI runtime service
126 * Accesses to efi_runtime_services() are serialized by a binary
127 * semaphore (efi_runtime_lock) and caller waits until the work is
128 * finished, hence _only_ one work is queued at a time and the caller
129 * thread waits for completion.
131 #define efi_queue_work(_rts, _args...) \
132 __efi_queue_work(EFI_ ## _rts, \
133 &(union efi_rts_args){ ._rts = { _args }})
135 #ifndef arch_efi_save_flags
136 #define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
137 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
140 unsigned long efi_call_virt_save_flags(void)
144 arch_efi_save_flags(flags);
148 void efi_call_virt_check_flags(unsigned long flags, const void *caller)
150 unsigned long cur_flags, mismatch;
152 cur_flags = efi_call_virt_save_flags();
154 mismatch = flags ^ cur_flags;
155 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
158 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
159 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n",
160 flags, cur_flags, caller ?: __builtin_return_address(0));
161 arch_efi_restore_flags(flags);
165 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
166 * reentrant, and there are particular combinations of calls that need to be
167 * serialized. (source: UEFI Specification v2.4A)
169 * Table 31. Rules for Reentry Into Runtime Services
170 * +------------------------------------+-------------------------------+
171 * | If previous call is busy in | Forbidden to call |
172 * +------------------------------------+-------------------------------+
173 * | Any | SetVirtualAddressMap() |
174 * +------------------------------------+-------------------------------+
175 * | ConvertPointer() | ConvertPointer() |
176 * +------------------------------------+-------------------------------+
177 * | SetVariable() | ResetSystem() |
178 * | UpdateCapsule() | |
180 * | SetWakeupTime() | |
181 * | GetNextHighMonotonicCount() | |
182 * +------------------------------------+-------------------------------+
183 * | GetVariable() | GetVariable() |
184 * | GetNextVariableName() | GetNextVariableName() |
185 * | SetVariable() | SetVariable() |
186 * | QueryVariableInfo() | QueryVariableInfo() |
187 * | UpdateCapsule() | UpdateCapsule() |
188 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
189 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
190 * +------------------------------------+-------------------------------+
191 * | GetTime() | GetTime() |
192 * | SetTime() | SetTime() |
193 * | GetWakeupTime() | GetWakeupTime() |
194 * | SetWakeupTime() | SetWakeupTime() |
195 * +------------------------------------+-------------------------------+
197 * Due to the fact that the EFI pstore may write to the variable store in
198 * interrupt context, we need to use a lock for at least the groups that
199 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
200 * none of the remaining functions are actually ever called at runtime.
201 * So let's just use a single lock to serialize all Runtime Services calls.
203 static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
206 * Expose the EFI runtime lock to the UV platform
209 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
213 * Calls the appropriate efi_runtime_service() with the appropriate
216 static void efi_call_rts(struct work_struct *work)
218 const union efi_rts_args *args = efi_rts_work.args;
219 efi_status_t status = EFI_NOT_FOUND;
222 arch_efi_call_virt_setup();
223 flags = efi_call_virt_save_flags();
225 switch (efi_rts_work.efi_rts_id) {
227 status = efi_call_virt(get_time,
229 args->GET_TIME.capabilities);
232 status = efi_call_virt(set_time,
233 args->SET_TIME.time);
235 case EFI_GET_WAKEUP_TIME:
236 status = efi_call_virt(get_wakeup_time,
237 args->GET_WAKEUP_TIME.enabled,
238 args->GET_WAKEUP_TIME.pending,
239 args->GET_WAKEUP_TIME.time);
241 case EFI_SET_WAKEUP_TIME:
242 status = efi_call_virt(set_wakeup_time,
243 args->SET_WAKEUP_TIME.enable,
244 args->SET_WAKEUP_TIME.time);
246 case EFI_GET_VARIABLE:
247 status = efi_call_virt(get_variable,
248 args->GET_VARIABLE.name,
249 args->GET_VARIABLE.vendor,
250 args->GET_VARIABLE.attr,
251 args->GET_VARIABLE.data_size,
252 args->GET_VARIABLE.data);
254 case EFI_GET_NEXT_VARIABLE:
255 status = efi_call_virt(get_next_variable,
256 args->GET_NEXT_VARIABLE.name_size,
257 args->GET_NEXT_VARIABLE.name,
258 args->GET_NEXT_VARIABLE.vendor);
260 case EFI_SET_VARIABLE:
261 status = efi_call_virt(set_variable,
262 args->SET_VARIABLE.name,
263 args->SET_VARIABLE.vendor,
264 args->SET_VARIABLE.attr,
265 args->SET_VARIABLE.data_size,
266 args->SET_VARIABLE.data);
268 case EFI_QUERY_VARIABLE_INFO:
269 status = efi_call_virt(query_variable_info,
270 args->QUERY_VARIABLE_INFO.attr,
271 args->QUERY_VARIABLE_INFO.storage_space,
272 args->QUERY_VARIABLE_INFO.remaining_space,
273 args->QUERY_VARIABLE_INFO.max_variable_size);
275 case EFI_GET_NEXT_HIGH_MONO_COUNT:
276 status = efi_call_virt(get_next_high_mono_count,
277 args->GET_NEXT_HIGH_MONO_COUNT.high_count);
279 case EFI_UPDATE_CAPSULE:
280 status = efi_call_virt(update_capsule,
281 args->UPDATE_CAPSULE.capsules,
282 args->UPDATE_CAPSULE.count,
283 args->UPDATE_CAPSULE.sg_list);
285 case EFI_QUERY_CAPSULE_CAPS:
286 status = efi_call_virt(query_capsule_caps,
287 args->QUERY_CAPSULE_CAPS.capsules,
288 args->QUERY_CAPSULE_CAPS.count,
289 args->QUERY_CAPSULE_CAPS.max_size,
290 args->QUERY_CAPSULE_CAPS.reset_type);
292 case EFI_ACPI_PRM_HANDLER:
293 #ifdef CONFIG_ACPI_PRMT
294 status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler,
295 args->ACPI_PRM_HANDLER.param_buffer_addr,
296 args->ACPI_PRM_HANDLER.context);
301 * Ideally, we should never reach here because a caller of this
302 * function should have put the right efi_runtime_service()
303 * function identifier into efi_rts_work->efi_rts_id
305 pr_err("Requested executing invalid EFI Runtime Service.\n");
308 efi_call_virt_check_flags(flags, efi_rts_work.caller);
309 arch_efi_call_virt_teardown();
311 efi_rts_work.status = status;
312 complete(&efi_rts_work.efi_rts_comp);
315 static efi_status_t __efi_queue_work(enum efi_rts_ids id,
316 union efi_rts_args *args)
318 efi_rts_work.efi_rts_id = id;
319 efi_rts_work.args = args;
320 efi_rts_work.caller = __builtin_return_address(0);
321 efi_rts_work.status = EFI_ABORTED;
323 if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
324 pr_warn_once("EFI Runtime Services are disabled!\n");
325 efi_rts_work.status = EFI_DEVICE_ERROR;
329 init_completion(&efi_rts_work.efi_rts_comp);
330 INIT_WORK(&efi_rts_work.work, efi_call_rts);
333 * queue_work() returns 0 if work was already on queue,
334 * _ideally_ this should never happen.
336 if (queue_work(efi_rts_wq, &efi_rts_work.work))
337 wait_for_completion(&efi_rts_work.efi_rts_comp);
339 pr_err("Failed to queue work to efi_rts_wq.\n");
341 WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);
343 efi_rts_work.efi_rts_id = EFI_NONE;
344 return efi_rts_work.status;
347 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
351 if (down_interruptible(&efi_runtime_lock))
353 status = efi_queue_work(GET_TIME, tm, tc);
354 up(&efi_runtime_lock);
358 static efi_status_t virt_efi_set_time(efi_time_t *tm)
362 if (down_interruptible(&efi_runtime_lock))
364 status = efi_queue_work(SET_TIME, tm);
365 up(&efi_runtime_lock);
369 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
375 if (down_interruptible(&efi_runtime_lock))
377 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);
378 up(&efi_runtime_lock);
382 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
386 if (down_interruptible(&efi_runtime_lock))
388 status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);
389 up(&efi_runtime_lock);
393 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
396 unsigned long *data_size,
401 if (down_interruptible(&efi_runtime_lock))
403 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
405 up(&efi_runtime_lock);
409 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
415 if (down_interruptible(&efi_runtime_lock))
417 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);
418 up(&efi_runtime_lock);
422 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
425 unsigned long data_size,
430 if (down_interruptible(&efi_runtime_lock))
432 status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,
434 up(&efi_runtime_lock);
439 virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
440 unsigned long data_size, void *data)
444 if (down_trylock(&efi_runtime_lock))
445 return EFI_NOT_READY;
447 status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor,
448 attr, data_size, data);
449 up(&efi_runtime_lock);
454 static efi_status_t virt_efi_query_variable_info(u32 attr,
456 u64 *remaining_space,
457 u64 *max_variable_size)
461 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
462 return EFI_UNSUPPORTED;
464 if (down_interruptible(&efi_runtime_lock))
466 status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,
467 remaining_space, max_variable_size);
468 up(&efi_runtime_lock);
473 virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space,
474 u64 *remaining_space, u64 *max_variable_size)
478 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
479 return EFI_UNSUPPORTED;
481 if (down_trylock(&efi_runtime_lock))
482 return EFI_NOT_READY;
484 status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr,
485 storage_space, remaining_space,
487 up(&efi_runtime_lock);
491 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
495 if (down_interruptible(&efi_runtime_lock))
497 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);
498 up(&efi_runtime_lock);
502 static void virt_efi_reset_system(int reset_type,
504 unsigned long data_size,
507 if (down_trylock(&efi_runtime_lock)) {
508 pr_warn("failed to invoke the reset_system() runtime service:\n"
509 "could not get exclusive access to the firmware\n");
513 arch_efi_call_virt_setup();
514 efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
515 arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,
517 arch_efi_call_virt_teardown();
519 up(&efi_runtime_lock);
522 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
524 unsigned long sg_list)
528 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
529 return EFI_UNSUPPORTED;
531 if (down_interruptible(&efi_runtime_lock))
533 status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);
534 up(&efi_runtime_lock);
538 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
545 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
546 return EFI_UNSUPPORTED;
548 if (down_interruptible(&efi_runtime_lock))
550 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,
551 max_size, reset_type);
552 up(&efi_runtime_lock);
556 void __init efi_native_runtime_setup(void)
558 efi.get_time = virt_efi_get_time;
559 efi.set_time = virt_efi_set_time;
560 efi.get_wakeup_time = virt_efi_get_wakeup_time;
561 efi.set_wakeup_time = virt_efi_set_wakeup_time;
562 efi.get_variable = virt_efi_get_variable;
563 efi.get_next_variable = virt_efi_get_next_variable;
564 efi.set_variable = virt_efi_set_variable;
565 efi.set_variable_nonblocking = virt_efi_set_variable_nb;
566 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
567 efi.reset_system = virt_efi_reset_system;
568 efi.query_variable_info = virt_efi_query_variable_info;
569 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb;
570 efi.update_capsule = virt_efi_update_capsule;
571 efi.query_capsule_caps = virt_efi_query_capsule_caps;
574 #ifdef CONFIG_ACPI_PRMT
577 efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *),
578 u64 param_buffer_addr, void *context)
582 if (down_interruptible(&efi_runtime_lock))
584 status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr,
585 param_buffer_addr, context);
586 up(&efi_runtime_lock);