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 init_completion(&efi_rts_work.efi_rts_comp); \
65 INIT_WORK(&efi_rts_work.work, efi_call_rts); \
66 efi_rts_work.arg1 = _arg1; \
67 efi_rts_work.arg2 = _arg2; \
68 efi_rts_work.arg3 = _arg3; \
69 efi_rts_work.arg4 = _arg4; \
70 efi_rts_work.arg5 = _arg5; \
71 efi_rts_work.efi_rts_id = _rts; \
74 * queue_work() returns 0 if work was already on queue, \
75 * _ideally_ this should never happen. \
77 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
78 wait_for_completion(&efi_rts_work.efi_rts_comp); \
80 pr_err("Failed to queue work to efi_rts_wq.\n"); \
82 efi_rts_work.status; \
85 void efi_call_virt_check_flags(unsigned long flags, const char *call)
87 unsigned long cur_flags, mismatch;
89 local_save_flags(cur_flags);
91 mismatch = flags ^ cur_flags;
92 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
95 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
96 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
97 flags, cur_flags, call);
98 local_irq_restore(flags);
102 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
103 * reentrant, and there are particular combinations of calls that need to be
104 * serialized. (source: UEFI Specification v2.4A)
106 * Table 31. Rules for Reentry Into Runtime Services
107 * +------------------------------------+-------------------------------+
108 * | If previous call is busy in | Forbidden to call |
109 * +------------------------------------+-------------------------------+
110 * | Any | SetVirtualAddressMap() |
111 * +------------------------------------+-------------------------------+
112 * | ConvertPointer() | ConvertPointer() |
113 * +------------------------------------+-------------------------------+
114 * | SetVariable() | ResetSystem() |
115 * | UpdateCapsule() | |
117 * | SetWakeupTime() | |
118 * | GetNextHighMonotonicCount() | |
119 * +------------------------------------+-------------------------------+
120 * | GetVariable() | GetVariable() |
121 * | GetNextVariableName() | GetNextVariableName() |
122 * | SetVariable() | SetVariable() |
123 * | QueryVariableInfo() | QueryVariableInfo() |
124 * | UpdateCapsule() | UpdateCapsule() |
125 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
126 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
127 * +------------------------------------+-------------------------------+
128 * | GetTime() | GetTime() |
129 * | SetTime() | SetTime() |
130 * | GetWakeupTime() | GetWakeupTime() |
131 * | SetWakeupTime() | SetWakeupTime() |
132 * +------------------------------------+-------------------------------+
134 * Due to the fact that the EFI pstore may write to the variable store in
135 * interrupt context, we need to use a lock for at least the groups that
136 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
137 * none of the remaining functions are actually ever called at runtime.
138 * So let's just use a single lock to serialize all Runtime Services calls.
140 static DEFINE_SEMAPHORE(efi_runtime_lock);
143 * Expose the EFI runtime lock to the UV platform
146 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
150 * Calls the appropriate efi_runtime_service() with the appropriate
153 * Semantics followed by efi_call_rts() to understand efi_runtime_work:
154 * 1. If argument was a pointer, recast it from void pointer to original
156 * 2. If argument was a value, recast it from void pointer to original
157 * pointer type and dereference it.
159 static void efi_call_rts(struct work_struct *work)
161 void *arg1, *arg2, *arg3, *arg4, *arg5;
162 efi_status_t status = EFI_NOT_FOUND;
164 arg1 = efi_rts_work.arg1;
165 arg2 = efi_rts_work.arg2;
166 arg3 = efi_rts_work.arg3;
167 arg4 = efi_rts_work.arg4;
168 arg5 = efi_rts_work.arg5;
170 switch (efi_rts_work.efi_rts_id) {
172 status = efi_call_virt(get_time, (efi_time_t *)arg1,
173 (efi_time_cap_t *)arg2);
176 status = efi_call_virt(set_time, (efi_time_t *)arg1);
178 case GET_WAKEUP_TIME:
179 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
180 (efi_bool_t *)arg2, (efi_time_t *)arg3);
182 case SET_WAKEUP_TIME:
183 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
187 status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
188 (efi_guid_t *)arg2, (u32 *)arg3,
189 (unsigned long *)arg4, (void *)arg5);
191 case GET_NEXT_VARIABLE:
192 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
193 (efi_char16_t *)arg2,
197 status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
198 (efi_guid_t *)arg2, *(u32 *)arg3,
199 *(unsigned long *)arg4, (void *)arg5);
201 case QUERY_VARIABLE_INFO:
202 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
203 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
205 case GET_NEXT_HIGH_MONO_COUNT:
206 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
209 status = efi_call_virt(update_capsule,
210 (efi_capsule_header_t **)arg1,
211 *(unsigned long *)arg2,
212 *(unsigned long *)arg3);
214 case QUERY_CAPSULE_CAPS:
215 status = efi_call_virt(query_capsule_caps,
216 (efi_capsule_header_t **)arg1,
217 *(unsigned long *)arg2, (u64 *)arg3,
222 * Ideally, we should never reach here because a caller of this
223 * function should have put the right efi_runtime_service()
224 * function identifier into efi_rts_work->efi_rts_id
226 pr_err("Requested executing invalid EFI Runtime Service.\n");
228 efi_rts_work.status = status;
229 complete(&efi_rts_work.efi_rts_comp);
232 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
236 if (down_interruptible(&efi_runtime_lock))
238 status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL);
239 up(&efi_runtime_lock);
243 static efi_status_t virt_efi_set_time(efi_time_t *tm)
247 if (down_interruptible(&efi_runtime_lock))
249 status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL);
250 up(&efi_runtime_lock);
254 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
260 if (down_interruptible(&efi_runtime_lock))
262 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL,
264 up(&efi_runtime_lock);
268 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
272 if (down_interruptible(&efi_runtime_lock))
274 status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
276 up(&efi_runtime_lock);
280 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
283 unsigned long *data_size,
288 if (down_interruptible(&efi_runtime_lock))
290 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
292 up(&efi_runtime_lock);
296 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
302 if (down_interruptible(&efi_runtime_lock))
304 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor,
306 up(&efi_runtime_lock);
310 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
313 unsigned long data_size,
318 if (down_interruptible(&efi_runtime_lock))
320 status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size,
322 up(&efi_runtime_lock);
327 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
328 u32 attr, unsigned long data_size,
333 if (down_trylock(&efi_runtime_lock))
334 return EFI_NOT_READY;
336 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
338 up(&efi_runtime_lock);
343 static efi_status_t virt_efi_query_variable_info(u32 attr,
345 u64 *remaining_space,
346 u64 *max_variable_size)
350 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
351 return EFI_UNSUPPORTED;
353 if (down_interruptible(&efi_runtime_lock))
355 status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space,
356 remaining_space, max_variable_size, NULL);
357 up(&efi_runtime_lock);
362 virt_efi_query_variable_info_nonblocking(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_trylock(&efi_runtime_lock))
373 return EFI_NOT_READY;
375 status = efi_call_virt(query_variable_info, attr, storage_space,
376 remaining_space, max_variable_size);
377 up(&efi_runtime_lock);
381 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
385 if (down_interruptible(&efi_runtime_lock))
387 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
389 up(&efi_runtime_lock);
393 static void virt_efi_reset_system(int reset_type,
395 unsigned long data_size,
398 if (down_trylock(&efi_runtime_lock)) {
399 pr_warn("failed to invoke the reset_system() runtime service:\n"
400 "could not get exclusive access to the firmware\n");
403 __efi_call_virt(reset_system, reset_type, status, data_size, data);
404 up(&efi_runtime_lock);
407 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
409 unsigned long sg_list)
413 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
414 return EFI_UNSUPPORTED;
416 if (down_interruptible(&efi_runtime_lock))
418 status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list,
420 up(&efi_runtime_lock);
424 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
431 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
432 return EFI_UNSUPPORTED;
434 if (down_interruptible(&efi_runtime_lock))
436 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count,
437 max_size, reset_type, NULL);
438 up(&efi_runtime_lock);
442 void efi_native_runtime_setup(void)
444 efi.get_time = virt_efi_get_time;
445 efi.set_time = virt_efi_set_time;
446 efi.get_wakeup_time = virt_efi_get_wakeup_time;
447 efi.set_wakeup_time = virt_efi_set_wakeup_time;
448 efi.get_variable = virt_efi_get_variable;
449 efi.get_next_variable = virt_efi_get_next_variable;
450 efi.set_variable = virt_efi_set_variable;
451 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
452 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
453 efi.reset_system = virt_efi_reset_system;
454 efi.query_variable_info = virt_efi_query_variable_info;
455 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
456 efi.update_capsule = virt_efi_update_capsule;
457 efi.query_capsule_caps = virt_efi_query_capsule_caps;