2 * runtime-wrappers.c - Runtime Services function call wrappers
4 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6 * Split off from arch/x86/platform/efi/efi.c
8 * Copyright (C) 1999 VA Linux Systems
9 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
10 * Copyright (C) 1999-2002 Hewlett-Packard Co.
11 * Copyright (C) 2005-2008 Intel Co.
12 * Copyright (C) 2013 SuSE Labs
14 * This file is released under the GPLv2.
17 #include <linux/bug.h>
18 #include <linux/efi.h>
19 #include <linux/irqflags.h>
20 #include <linux/mutex.h>
21 #include <linux/spinlock.h>
22 #include <linux/stringify.h>
26 * Wrap around the new efi_call_virt_generic() macros so that the
27 * code doesn't get too cluttered:
29 #define efi_call_virt(f, args...) \
30 efi_call_virt_pointer(efi.systab->runtime, f, args)
31 #define __efi_call_virt(f, args...) \
32 __efi_call_virt_pointer(efi.systab->runtime, f, args)
34 void efi_call_virt_check_flags(unsigned long flags
, const char *call
)
36 unsigned long cur_flags
, mismatch
;
38 local_save_flags(cur_flags
);
40 mismatch
= flags
^ cur_flags
;
41 if (!WARN_ON_ONCE(mismatch
& ARCH_EFI_IRQ_FLAGS_MASK
))
44 add_taint(TAINT_FIRMWARE_WORKAROUND
, LOCKDEP_NOW_UNRELIABLE
);
45 pr_err_ratelimited(FW_BUG
"IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
46 flags
, cur_flags
, call
);
47 local_irq_restore(flags
);
51 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
52 * reentrant, and there are particular combinations of calls that need to be
53 * serialized. (source: UEFI Specification v2.4A)
55 * Table 31. Rules for Reentry Into Runtime Services
56 * +------------------------------------+-------------------------------+
57 * | If previous call is busy in | Forbidden to call |
58 * +------------------------------------+-------------------------------+
59 * | Any | SetVirtualAddressMap() |
60 * +------------------------------------+-------------------------------+
61 * | ConvertPointer() | ConvertPointer() |
62 * +------------------------------------+-------------------------------+
63 * | SetVariable() | ResetSystem() |
64 * | UpdateCapsule() | |
66 * | SetWakeupTime() | |
67 * | GetNextHighMonotonicCount() | |
68 * +------------------------------------+-------------------------------+
69 * | GetVariable() | GetVariable() |
70 * | GetNextVariableName() | GetNextVariableName() |
71 * | SetVariable() | SetVariable() |
72 * | QueryVariableInfo() | QueryVariableInfo() |
73 * | UpdateCapsule() | UpdateCapsule() |
74 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
75 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
76 * +------------------------------------+-------------------------------+
77 * | GetTime() | GetTime() |
78 * | SetTime() | SetTime() |
79 * | GetWakeupTime() | GetWakeupTime() |
80 * | SetWakeupTime() | SetWakeupTime() |
81 * +------------------------------------+-------------------------------+
83 * Due to the fact that the EFI pstore may write to the variable store in
84 * interrupt context, we need to use a spinlock for at least the groups that
85 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
86 * none of the remaining functions are actually ever called at runtime.
87 * So let's just use a single spinlock to serialize all Runtime Services calls.
89 static DEFINE_SPINLOCK(efi_runtime_lock
);
91 static efi_status_t
virt_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
95 spin_lock(&efi_runtime_lock
);
96 status
= efi_call_virt(get_time
, tm
, tc
);
97 spin_unlock(&efi_runtime_lock
);
101 static efi_status_t
virt_efi_set_time(efi_time_t
*tm
)
105 spin_lock(&efi_runtime_lock
);
106 status
= efi_call_virt(set_time
, tm
);
107 spin_unlock(&efi_runtime_lock
);
111 static efi_status_t
virt_efi_get_wakeup_time(efi_bool_t
*enabled
,
117 spin_lock(&efi_runtime_lock
);
118 status
= efi_call_virt(get_wakeup_time
, enabled
, pending
, tm
);
119 spin_unlock(&efi_runtime_lock
);
123 static efi_status_t
virt_efi_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
127 spin_lock(&efi_runtime_lock
);
128 status
= efi_call_virt(set_wakeup_time
, enabled
, tm
);
129 spin_unlock(&efi_runtime_lock
);
133 static efi_status_t
virt_efi_get_variable(efi_char16_t
*name
,
136 unsigned long *data_size
,
141 spin_lock(&efi_runtime_lock
);
142 status
= efi_call_virt(get_variable
, name
, vendor
, attr
, data_size
,
144 spin_unlock(&efi_runtime_lock
);
148 static efi_status_t
virt_efi_get_next_variable(unsigned long *name_size
,
154 spin_lock(&efi_runtime_lock
);
155 status
= efi_call_virt(get_next_variable
, name_size
, name
, vendor
);
156 spin_unlock(&efi_runtime_lock
);
160 static efi_status_t
virt_efi_set_variable(efi_char16_t
*name
,
163 unsigned long data_size
,
168 spin_lock(&efi_runtime_lock
);
169 status
= efi_call_virt(set_variable
, name
, vendor
, attr
, data_size
,
171 spin_unlock(&efi_runtime_lock
);
176 virt_efi_set_variable_nonblocking(efi_char16_t
*name
, efi_guid_t
*vendor
,
177 u32 attr
, unsigned long data_size
,
182 if (!spin_trylock(&efi_runtime_lock
))
183 return EFI_NOT_READY
;
185 status
= efi_call_virt(set_variable
, name
, vendor
, attr
, data_size
,
187 spin_unlock(&efi_runtime_lock
);
192 static efi_status_t
virt_efi_query_variable_info(u32 attr
,
194 u64
*remaining_space
,
195 u64
*max_variable_size
)
199 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
200 return EFI_UNSUPPORTED
;
202 spin_lock(&efi_runtime_lock
);
203 status
= efi_call_virt(query_variable_info
, attr
, storage_space
,
204 remaining_space
, max_variable_size
);
205 spin_unlock(&efi_runtime_lock
);
210 virt_efi_query_variable_info_nonblocking(u32 attr
,
212 u64
*remaining_space
,
213 u64
*max_variable_size
)
217 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
218 return EFI_UNSUPPORTED
;
220 if (!spin_trylock(&efi_runtime_lock
))
221 return EFI_NOT_READY
;
223 status
= efi_call_virt(query_variable_info
, attr
, storage_space
,
224 remaining_space
, max_variable_size
);
225 spin_unlock(&efi_runtime_lock
);
229 static efi_status_t
virt_efi_get_next_high_mono_count(u32
*count
)
233 spin_lock(&efi_runtime_lock
);
234 status
= efi_call_virt(get_next_high_mono_count
, count
);
235 spin_unlock(&efi_runtime_lock
);
239 static void virt_efi_reset_system(int reset_type
,
241 unsigned long data_size
,
244 spin_lock(&efi_runtime_lock
);
245 __efi_call_virt(reset_system
, reset_type
, status
, data_size
, data
);
246 spin_unlock(&efi_runtime_lock
);
249 static efi_status_t
virt_efi_update_capsule(efi_capsule_header_t
**capsules
,
251 unsigned long sg_list
)
255 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
256 return EFI_UNSUPPORTED
;
258 spin_lock(&efi_runtime_lock
);
259 status
= efi_call_virt(update_capsule
, capsules
, count
, sg_list
);
260 spin_unlock(&efi_runtime_lock
);
264 static efi_status_t
virt_efi_query_capsule_caps(efi_capsule_header_t
**capsules
,
271 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
272 return EFI_UNSUPPORTED
;
274 spin_lock(&efi_runtime_lock
);
275 status
= efi_call_virt(query_capsule_caps
, capsules
, count
, max_size
,
277 spin_unlock(&efi_runtime_lock
);
281 void efi_native_runtime_setup(void)
283 efi
.get_time
= virt_efi_get_time
;
284 efi
.set_time
= virt_efi_set_time
;
285 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
286 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
287 efi
.get_variable
= virt_efi_get_variable
;
288 efi
.get_next_variable
= virt_efi_get_next_variable
;
289 efi
.set_variable
= virt_efi_set_variable
;
290 efi
.set_variable_nonblocking
= virt_efi_set_variable_nonblocking
;
291 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
292 efi
.reset_system
= virt_efi_reset_system
;
293 efi
.query_variable_info
= virt_efi_query_variable_info
;
294 efi
.query_variable_info_nonblocking
= virt_efi_query_variable_info_nonblocking
;
295 efi
.update_capsule
= virt_efi_update_capsule
;
296 efi
.query_capsule_caps
= virt_efi_query_capsule_caps
;