2 * runtime-wrappers.c - Runtime Services function call wrappers
4 * Implementation summary:
5 * -----------------------
6 * 1. When user/kernel thread requests to execute efi_runtime_service(),
7 * enqueue work to efi_rts_wq.
8 * 2. Caller thread waits for completion until the work is finished
9 * because it's dependent on the return status and execution of
10 * efi_runtime_service().
11 * For instance, get_variable() and get_next_variable().
13 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
15 * Split off from arch/x86/platform/efi/efi.c
17 * Copyright (C) 1999 VA Linux Systems
18 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
19 * Copyright (C) 1999-2002 Hewlett-Packard Co.
20 * Copyright (C) 2005-2008 Intel Co.
21 * Copyright (C) 2013 SuSE Labs
23 * This file is released under the GPLv2.
26 #define pr_fmt(fmt) "efi: " fmt
28 #include <linux/bug.h>
29 #include <linux/efi.h>
30 #include <linux/irqflags.h>
31 #include <linux/mutex.h>
32 #include <linux/semaphore.h>
33 #include <linux/stringify.h>
34 #include <linux/workqueue.h>
35 #include <linux/completion.h>
40 * Wrap around the new efi_call_virt_generic() macros so that the
41 * code doesn't get too cluttered:
43 #define efi_call_virt(f, args...) \
44 efi_call_virt_pointer(efi.systab->runtime, f, args)
45 #define __efi_call_virt(f, args...) \
46 __efi_call_virt_pointer(efi.systab->runtime, f, args)
48 struct efi_runtime_work efi_rts_work
;
51 * efi_queue_work: Queue efi_runtime_service() and wait until it's done
52 * @rts: efi_runtime_service() function identifier
53 * @rts_arg<1-5>: efi_runtime_service() function arguments
55 * Accesses to efi_runtime_services() are serialized by a binary
56 * semaphore (efi_runtime_lock) and caller waits until the work is
57 * finished, hence _only_ one work is queued at a time and the caller
58 * thread waits for completion.
60 #define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \
62 efi_rts_work.status = EFI_ABORTED; \
64 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { \
65 pr_warn_once("EFI Runtime Services are disabled!\n"); \
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"); \
88 efi_rts_work.efi_rts_id = EFI_NONE; \
89 efi_rts_work.status; \
92 #ifndef arch_efi_save_flags
93 #define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
94 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
97 unsigned long efi_call_virt_save_flags(void)
101 arch_efi_save_flags(flags
);
105 void efi_call_virt_check_flags(unsigned long flags
, const char *call
)
107 unsigned long cur_flags
, mismatch
;
109 cur_flags
= efi_call_virt_save_flags();
111 mismatch
= flags
^ cur_flags
;
112 if (!WARN_ON_ONCE(mismatch
& ARCH_EFI_IRQ_FLAGS_MASK
))
115 add_taint(TAINT_FIRMWARE_WORKAROUND
, LOCKDEP_NOW_UNRELIABLE
);
116 pr_err_ratelimited(FW_BUG
"IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
117 flags
, cur_flags
, call
);
118 arch_efi_restore_flags(flags
);
122 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
123 * reentrant, and there are particular combinations of calls that need to be
124 * serialized. (source: UEFI Specification v2.4A)
126 * Table 31. Rules for Reentry Into Runtime Services
127 * +------------------------------------+-------------------------------+
128 * | If previous call is busy in | Forbidden to call |
129 * +------------------------------------+-------------------------------+
130 * | Any | SetVirtualAddressMap() |
131 * +------------------------------------+-------------------------------+
132 * | ConvertPointer() | ConvertPointer() |
133 * +------------------------------------+-------------------------------+
134 * | SetVariable() | ResetSystem() |
135 * | UpdateCapsule() | |
137 * | SetWakeupTime() | |
138 * | GetNextHighMonotonicCount() | |
139 * +------------------------------------+-------------------------------+
140 * | GetVariable() | GetVariable() |
141 * | GetNextVariableName() | GetNextVariableName() |
142 * | SetVariable() | SetVariable() |
143 * | QueryVariableInfo() | QueryVariableInfo() |
144 * | UpdateCapsule() | UpdateCapsule() |
145 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
146 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
147 * +------------------------------------+-------------------------------+
148 * | GetTime() | GetTime() |
149 * | SetTime() | SetTime() |
150 * | GetWakeupTime() | GetWakeupTime() |
151 * | SetWakeupTime() | SetWakeupTime() |
152 * +------------------------------------+-------------------------------+
154 * Due to the fact that the EFI pstore may write to the variable store in
155 * interrupt context, we need to use a lock for at least the groups that
156 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
157 * none of the remaining functions are actually ever called at runtime.
158 * So let's just use a single lock to serialize all Runtime Services calls.
160 static DEFINE_SEMAPHORE(efi_runtime_lock
);
163 * Expose the EFI runtime lock to the UV platform
166 extern struct semaphore __efi_uv_runtime_lock
__alias(efi_runtime_lock
);
170 * Calls the appropriate efi_runtime_service() with the appropriate
173 * Semantics followed by efi_call_rts() to understand efi_runtime_work:
174 * 1. If argument was a pointer, recast it from void pointer to original
176 * 2. If argument was a value, recast it from void pointer to original
177 * pointer type and dereference it.
179 static void efi_call_rts(struct work_struct
*work
)
181 void *arg1
, *arg2
, *arg3
, *arg4
, *arg5
;
182 efi_status_t status
= EFI_NOT_FOUND
;
184 arg1
= efi_rts_work
.arg1
;
185 arg2
= efi_rts_work
.arg2
;
186 arg3
= efi_rts_work
.arg3
;
187 arg4
= efi_rts_work
.arg4
;
188 arg5
= efi_rts_work
.arg5
;
190 switch (efi_rts_work
.efi_rts_id
) {
192 status
= efi_call_virt(get_time
, (efi_time_t
*)arg1
,
193 (efi_time_cap_t
*)arg2
);
196 status
= efi_call_virt(set_time
, (efi_time_t
*)arg1
);
198 case EFI_GET_WAKEUP_TIME
:
199 status
= efi_call_virt(get_wakeup_time
, (efi_bool_t
*)arg1
,
200 (efi_bool_t
*)arg2
, (efi_time_t
*)arg3
);
202 case EFI_SET_WAKEUP_TIME
:
203 status
= efi_call_virt(set_wakeup_time
, *(efi_bool_t
*)arg1
,
206 case EFI_GET_VARIABLE
:
207 status
= efi_call_virt(get_variable
, (efi_char16_t
*)arg1
,
208 (efi_guid_t
*)arg2
, (u32
*)arg3
,
209 (unsigned long *)arg4
, (void *)arg5
);
211 case EFI_GET_NEXT_VARIABLE
:
212 status
= efi_call_virt(get_next_variable
, (unsigned long *)arg1
,
213 (efi_char16_t
*)arg2
,
216 case EFI_SET_VARIABLE
:
217 status
= efi_call_virt(set_variable
, (efi_char16_t
*)arg1
,
218 (efi_guid_t
*)arg2
, *(u32
*)arg3
,
219 *(unsigned long *)arg4
, (void *)arg5
);
221 case EFI_QUERY_VARIABLE_INFO
:
222 status
= efi_call_virt(query_variable_info
, *(u32
*)arg1
,
223 (u64
*)arg2
, (u64
*)arg3
, (u64
*)arg4
);
225 case EFI_GET_NEXT_HIGH_MONO_COUNT
:
226 status
= efi_call_virt(get_next_high_mono_count
, (u32
*)arg1
);
228 case EFI_UPDATE_CAPSULE
:
229 status
= efi_call_virt(update_capsule
,
230 (efi_capsule_header_t
**)arg1
,
231 *(unsigned long *)arg2
,
232 *(unsigned long *)arg3
);
234 case EFI_QUERY_CAPSULE_CAPS
:
235 status
= efi_call_virt(query_capsule_caps
,
236 (efi_capsule_header_t
**)arg1
,
237 *(unsigned long *)arg2
, (u64
*)arg3
,
242 * Ideally, we should never reach here because a caller of this
243 * function should have put the right efi_runtime_service()
244 * function identifier into efi_rts_work->efi_rts_id
246 pr_err("Requested executing invalid EFI Runtime Service.\n");
248 efi_rts_work
.status
= status
;
249 complete(&efi_rts_work
.efi_rts_comp
);
252 static efi_status_t
virt_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
256 if (down_interruptible(&efi_runtime_lock
))
258 status
= efi_queue_work(EFI_GET_TIME
, tm
, tc
, NULL
, NULL
, NULL
);
259 up(&efi_runtime_lock
);
263 static efi_status_t
virt_efi_set_time(efi_time_t
*tm
)
267 if (down_interruptible(&efi_runtime_lock
))
269 status
= efi_queue_work(EFI_SET_TIME
, tm
, NULL
, NULL
, NULL
, NULL
);
270 up(&efi_runtime_lock
);
274 static efi_status_t
virt_efi_get_wakeup_time(efi_bool_t
*enabled
,
280 if (down_interruptible(&efi_runtime_lock
))
282 status
= efi_queue_work(EFI_GET_WAKEUP_TIME
, enabled
, pending
, tm
, NULL
,
284 up(&efi_runtime_lock
);
288 static efi_status_t
virt_efi_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
292 if (down_interruptible(&efi_runtime_lock
))
294 status
= efi_queue_work(EFI_SET_WAKEUP_TIME
, &enabled
, tm
, NULL
, NULL
,
296 up(&efi_runtime_lock
);
300 static efi_status_t
virt_efi_get_variable(efi_char16_t
*name
,
303 unsigned long *data_size
,
308 if (down_interruptible(&efi_runtime_lock
))
310 status
= efi_queue_work(EFI_GET_VARIABLE
, name
, vendor
, attr
, data_size
,
312 up(&efi_runtime_lock
);
316 static efi_status_t
virt_efi_get_next_variable(unsigned long *name_size
,
322 if (down_interruptible(&efi_runtime_lock
))
324 status
= efi_queue_work(EFI_GET_NEXT_VARIABLE
, name_size
, name
, vendor
,
326 up(&efi_runtime_lock
);
330 static efi_status_t
virt_efi_set_variable(efi_char16_t
*name
,
333 unsigned long data_size
,
338 if (down_interruptible(&efi_runtime_lock
))
340 status
= efi_queue_work(EFI_SET_VARIABLE
, name
, vendor
, &attr
, &data_size
,
342 up(&efi_runtime_lock
);
347 virt_efi_set_variable_nonblocking(efi_char16_t
*name
, efi_guid_t
*vendor
,
348 u32 attr
, unsigned long data_size
,
353 if (down_trylock(&efi_runtime_lock
))
354 return EFI_NOT_READY
;
356 status
= efi_call_virt(set_variable
, name
, vendor
, attr
, data_size
,
358 up(&efi_runtime_lock
);
363 static efi_status_t
virt_efi_query_variable_info(u32 attr
,
365 u64
*remaining_space
,
366 u64
*max_variable_size
)
370 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
371 return EFI_UNSUPPORTED
;
373 if (down_interruptible(&efi_runtime_lock
))
375 status
= efi_queue_work(EFI_QUERY_VARIABLE_INFO
, &attr
, storage_space
,
376 remaining_space
, max_variable_size
, NULL
);
377 up(&efi_runtime_lock
);
382 virt_efi_query_variable_info_nonblocking(u32 attr
,
384 u64
*remaining_space
,
385 u64
*max_variable_size
)
389 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
390 return EFI_UNSUPPORTED
;
392 if (down_trylock(&efi_runtime_lock
))
393 return EFI_NOT_READY
;
395 status
= efi_call_virt(query_variable_info
, attr
, storage_space
,
396 remaining_space
, max_variable_size
);
397 up(&efi_runtime_lock
);
401 static efi_status_t
virt_efi_get_next_high_mono_count(u32
*count
)
405 if (down_interruptible(&efi_runtime_lock
))
407 status
= efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT
, count
, NULL
, NULL
,
409 up(&efi_runtime_lock
);
413 static void virt_efi_reset_system(int reset_type
,
415 unsigned long data_size
,
418 if (down_interruptible(&efi_runtime_lock
)) {
419 pr_warn("failed to invoke the reset_system() runtime service:\n"
420 "could not get exclusive access to the firmware\n");
423 efi_rts_work
.efi_rts_id
= EFI_RESET_SYSTEM
;
424 __efi_call_virt(reset_system
, reset_type
, status
, data_size
, data
);
425 up(&efi_runtime_lock
);
428 static efi_status_t
virt_efi_update_capsule(efi_capsule_header_t
**capsules
,
430 unsigned long sg_list
)
434 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
435 return EFI_UNSUPPORTED
;
437 if (down_interruptible(&efi_runtime_lock
))
439 status
= efi_queue_work(EFI_UPDATE_CAPSULE
, capsules
, &count
, &sg_list
,
441 up(&efi_runtime_lock
);
445 static efi_status_t
virt_efi_query_capsule_caps(efi_capsule_header_t
**capsules
,
452 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
453 return EFI_UNSUPPORTED
;
455 if (down_interruptible(&efi_runtime_lock
))
457 status
= efi_queue_work(EFI_QUERY_CAPSULE_CAPS
, capsules
, &count
,
458 max_size
, reset_type
, NULL
);
459 up(&efi_runtime_lock
);
463 void efi_native_runtime_setup(void)
465 efi
.get_time
= virt_efi_get_time
;
466 efi
.set_time
= virt_efi_set_time
;
467 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
468 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
469 efi
.get_variable
= virt_efi_get_variable
;
470 efi
.get_next_variable
= virt_efi_get_next_variable
;
471 efi
.set_variable
= virt_efi_set_variable
;
472 efi
.set_variable_nonblocking
= virt_efi_set_variable_nonblocking
;
473 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
474 efi
.reset_system
= virt_efi_reset_system
;
475 efi
.query_variable_info
= virt_efi_query_variable_info
;
476 efi
.query_variable_info_nonblocking
= virt_efi_query_variable_info_nonblocking
;
477 efi
.update_capsule
= virt_efi_update_capsule
;
478 efi
.query_capsule_caps
= virt_efi_query_capsule_caps
;