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 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"); \
68 init_completion(&efi_rts_work.efi_rts_comp); \
69 INIT_WORK(&efi_rts_work.work, efi_call_rts); \
70 efi_rts_work.arg1 = _arg1; \
71 efi_rts_work.arg2 = _arg2; \
72 efi_rts_work.arg3 = _arg3; \
73 efi_rts_work.arg4 = _arg4; \
74 efi_rts_work.arg5 = _arg5; \
75 efi_rts_work.efi_rts_id = _rts; \
78 * queue_work() returns 0 if work was already on queue, \
79 * _ideally_ this should never happen. \
81 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
82 wait_for_completion(&efi_rts_work.efi_rts_comp); \
84 pr_err("Failed to queue work to efi_rts_wq.\n"); \
87 efi_rts_work.efi_rts_id = EFI_NONE; \
88 efi_rts_work.status; \
91 #ifndef arch_efi_save_flags
92 #define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
93 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
96 unsigned long efi_call_virt_save_flags(void)
100 arch_efi_save_flags(flags
);
104 void efi_call_virt_check_flags(unsigned long flags
, const char *call
)
106 unsigned long cur_flags
, mismatch
;
108 cur_flags
= efi_call_virt_save_flags();
110 mismatch
= flags
^ cur_flags
;
111 if (!WARN_ON_ONCE(mismatch
& ARCH_EFI_IRQ_FLAGS_MASK
))
114 add_taint(TAINT_FIRMWARE_WORKAROUND
, LOCKDEP_NOW_UNRELIABLE
);
115 pr_err_ratelimited(FW_BUG
"IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
116 flags
, cur_flags
, call
);
117 arch_efi_restore_flags(flags
);
121 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
122 * reentrant, and there are particular combinations of calls that need to be
123 * serialized. (source: UEFI Specification v2.4A)
125 * Table 31. Rules for Reentry Into Runtime Services
126 * +------------------------------------+-------------------------------+
127 * | If previous call is busy in | Forbidden to call |
128 * +------------------------------------+-------------------------------+
129 * | Any | SetVirtualAddressMap() |
130 * +------------------------------------+-------------------------------+
131 * | ConvertPointer() | ConvertPointer() |
132 * +------------------------------------+-------------------------------+
133 * | SetVariable() | ResetSystem() |
134 * | UpdateCapsule() | |
136 * | SetWakeupTime() | |
137 * | GetNextHighMonotonicCount() | |
138 * +------------------------------------+-------------------------------+
139 * | GetVariable() | GetVariable() |
140 * | GetNextVariableName() | GetNextVariableName() |
141 * | SetVariable() | SetVariable() |
142 * | QueryVariableInfo() | QueryVariableInfo() |
143 * | UpdateCapsule() | UpdateCapsule() |
144 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
145 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
146 * +------------------------------------+-------------------------------+
147 * | GetTime() | GetTime() |
148 * | SetTime() | SetTime() |
149 * | GetWakeupTime() | GetWakeupTime() |
150 * | SetWakeupTime() | SetWakeupTime() |
151 * +------------------------------------+-------------------------------+
153 * Due to the fact that the EFI pstore may write to the variable store in
154 * interrupt context, we need to use a lock for at least the groups that
155 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
156 * none of the remaining functions are actually ever called at runtime.
157 * So let's just use a single lock to serialize all Runtime Services calls.
159 static DEFINE_SEMAPHORE(efi_runtime_lock
);
162 * Expose the EFI runtime lock to the UV platform
165 extern struct semaphore __efi_uv_runtime_lock
__alias(efi_runtime_lock
);
169 * Calls the appropriate efi_runtime_service() with the appropriate
172 * Semantics followed by efi_call_rts() to understand efi_runtime_work:
173 * 1. If argument was a pointer, recast it from void pointer to original
175 * 2. If argument was a value, recast it from void pointer to original
176 * pointer type and dereference it.
178 static void efi_call_rts(struct work_struct
*work
)
180 void *arg1
, *arg2
, *arg3
, *arg4
, *arg5
;
181 efi_status_t status
= EFI_NOT_FOUND
;
183 arg1
= efi_rts_work
.arg1
;
184 arg2
= efi_rts_work
.arg2
;
185 arg3
= efi_rts_work
.arg3
;
186 arg4
= efi_rts_work
.arg4
;
187 arg5
= efi_rts_work
.arg5
;
189 switch (efi_rts_work
.efi_rts_id
) {
191 status
= efi_call_virt(get_time
, (efi_time_t
*)arg1
,
192 (efi_time_cap_t
*)arg2
);
195 status
= efi_call_virt(set_time
, (efi_time_t
*)arg1
);
197 case EFI_GET_WAKEUP_TIME
:
198 status
= efi_call_virt(get_wakeup_time
, (efi_bool_t
*)arg1
,
199 (efi_bool_t
*)arg2
, (efi_time_t
*)arg3
);
201 case EFI_SET_WAKEUP_TIME
:
202 status
= efi_call_virt(set_wakeup_time
, *(efi_bool_t
*)arg1
,
205 case EFI_GET_VARIABLE
:
206 status
= efi_call_virt(get_variable
, (efi_char16_t
*)arg1
,
207 (efi_guid_t
*)arg2
, (u32
*)arg3
,
208 (unsigned long *)arg4
, (void *)arg5
);
210 case EFI_GET_NEXT_VARIABLE
:
211 status
= efi_call_virt(get_next_variable
, (unsigned long *)arg1
,
212 (efi_char16_t
*)arg2
,
215 case EFI_SET_VARIABLE
:
216 status
= efi_call_virt(set_variable
, (efi_char16_t
*)arg1
,
217 (efi_guid_t
*)arg2
, *(u32
*)arg3
,
218 *(unsigned long *)arg4
, (void *)arg5
);
220 case EFI_QUERY_VARIABLE_INFO
:
221 status
= efi_call_virt(query_variable_info
, *(u32
*)arg1
,
222 (u64
*)arg2
, (u64
*)arg3
, (u64
*)arg4
);
224 case EFI_GET_NEXT_HIGH_MONO_COUNT
:
225 status
= efi_call_virt(get_next_high_mono_count
, (u32
*)arg1
);
227 case EFI_UPDATE_CAPSULE
:
228 status
= efi_call_virt(update_capsule
,
229 (efi_capsule_header_t
**)arg1
,
230 *(unsigned long *)arg2
,
231 *(unsigned long *)arg3
);
233 case EFI_QUERY_CAPSULE_CAPS
:
234 status
= efi_call_virt(query_capsule_caps
,
235 (efi_capsule_header_t
**)arg1
,
236 *(unsigned long *)arg2
, (u64
*)arg3
,
241 * Ideally, we should never reach here because a caller of this
242 * function should have put the right efi_runtime_service()
243 * function identifier into efi_rts_work->efi_rts_id
245 pr_err("Requested executing invalid EFI Runtime Service.\n");
247 efi_rts_work
.status
= status
;
248 complete(&efi_rts_work
.efi_rts_comp
);
251 static efi_status_t
virt_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
255 if (down_interruptible(&efi_runtime_lock
))
257 status
= efi_queue_work(EFI_GET_TIME
, tm
, tc
, NULL
, NULL
, NULL
);
258 up(&efi_runtime_lock
);
262 static efi_status_t
virt_efi_set_time(efi_time_t
*tm
)
266 if (down_interruptible(&efi_runtime_lock
))
268 status
= efi_queue_work(EFI_SET_TIME
, tm
, NULL
, NULL
, NULL
, NULL
);
269 up(&efi_runtime_lock
);
273 static efi_status_t
virt_efi_get_wakeup_time(efi_bool_t
*enabled
,
279 if (down_interruptible(&efi_runtime_lock
))
281 status
= efi_queue_work(EFI_GET_WAKEUP_TIME
, enabled
, pending
, tm
, NULL
,
283 up(&efi_runtime_lock
);
287 static efi_status_t
virt_efi_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
291 if (down_interruptible(&efi_runtime_lock
))
293 status
= efi_queue_work(EFI_SET_WAKEUP_TIME
, &enabled
, tm
, NULL
, NULL
,
295 up(&efi_runtime_lock
);
299 static efi_status_t
virt_efi_get_variable(efi_char16_t
*name
,
302 unsigned long *data_size
,
307 if (down_interruptible(&efi_runtime_lock
))
309 status
= efi_queue_work(EFI_GET_VARIABLE
, name
, vendor
, attr
, data_size
,
311 up(&efi_runtime_lock
);
315 static efi_status_t
virt_efi_get_next_variable(unsigned long *name_size
,
321 if (down_interruptible(&efi_runtime_lock
))
323 status
= efi_queue_work(EFI_GET_NEXT_VARIABLE
, name_size
, name
, vendor
,
325 up(&efi_runtime_lock
);
329 static efi_status_t
virt_efi_set_variable(efi_char16_t
*name
,
332 unsigned long data_size
,
337 if (down_interruptible(&efi_runtime_lock
))
339 status
= efi_queue_work(EFI_SET_VARIABLE
, name
, vendor
, &attr
, &data_size
,
341 up(&efi_runtime_lock
);
346 virt_efi_set_variable_nonblocking(efi_char16_t
*name
, efi_guid_t
*vendor
,
347 u32 attr
, unsigned long data_size
,
352 if (down_trylock(&efi_runtime_lock
))
353 return EFI_NOT_READY
;
355 status
= efi_call_virt(set_variable
, name
, vendor
, attr
, data_size
,
357 up(&efi_runtime_lock
);
362 static efi_status_t
virt_efi_query_variable_info(u32 attr
,
364 u64
*remaining_space
,
365 u64
*max_variable_size
)
369 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
370 return EFI_UNSUPPORTED
;
372 if (down_interruptible(&efi_runtime_lock
))
374 status
= efi_queue_work(EFI_QUERY_VARIABLE_INFO
, &attr
, storage_space
,
375 remaining_space
, max_variable_size
, NULL
);
376 up(&efi_runtime_lock
);
381 virt_efi_query_variable_info_nonblocking(u32 attr
,
383 u64
*remaining_space
,
384 u64
*max_variable_size
)
388 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
389 return EFI_UNSUPPORTED
;
391 if (down_trylock(&efi_runtime_lock
))
392 return EFI_NOT_READY
;
394 status
= efi_call_virt(query_variable_info
, attr
, storage_space
,
395 remaining_space
, max_variable_size
);
396 up(&efi_runtime_lock
);
400 static efi_status_t
virt_efi_get_next_high_mono_count(u32
*count
)
404 if (down_interruptible(&efi_runtime_lock
))
406 status
= efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT
, count
, NULL
, NULL
,
408 up(&efi_runtime_lock
);
412 static void virt_efi_reset_system(int reset_type
,
414 unsigned long data_size
,
417 if (down_interruptible(&efi_runtime_lock
)) {
418 pr_warn("failed to invoke the reset_system() runtime service:\n"
419 "could not get exclusive access to the firmware\n");
422 efi_rts_work
.efi_rts_id
= EFI_RESET_SYSTEM
;
423 __efi_call_virt(reset_system
, reset_type
, status
, data_size
, data
);
424 up(&efi_runtime_lock
);
427 static efi_status_t
virt_efi_update_capsule(efi_capsule_header_t
**capsules
,
429 unsigned long sg_list
)
433 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
434 return EFI_UNSUPPORTED
;
436 if (down_interruptible(&efi_runtime_lock
))
438 status
= efi_queue_work(EFI_UPDATE_CAPSULE
, capsules
, &count
, &sg_list
,
440 up(&efi_runtime_lock
);
444 static efi_status_t
virt_efi_query_capsule_caps(efi_capsule_header_t
**capsules
,
451 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
452 return EFI_UNSUPPORTED
;
454 if (down_interruptible(&efi_runtime_lock
))
456 status
= efi_queue_work(EFI_QUERY_CAPSULE_CAPS
, capsules
, &count
,
457 max_size
, reset_type
, NULL
);
458 up(&efi_runtime_lock
);
462 void efi_native_runtime_setup(void)
464 efi
.get_time
= virt_efi_get_time
;
465 efi
.set_time
= virt_efi_set_time
;
466 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
467 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
468 efi
.get_variable
= virt_efi_get_variable
;
469 efi
.get_next_variable
= virt_efi_get_next_variable
;
470 efi
.set_variable
= virt_efi_set_variable
;
471 efi
.set_variable_nonblocking
= virt_efi_set_variable_nonblocking
;
472 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
473 efi
.reset_system
= virt_efi_reset_system
;
474 efi
.query_variable_info
= virt_efi_query_variable_info
;
475 efi
.query_variable_info_nonblocking
= virt_efi_query_variable_info_nonblocking
;
476 efi
.update_capsule
= virt_efi_update_capsule
;
477 efi
.query_capsule_caps
= virt_efi_query_capsule_caps
;