Commit | Line | Data |
---|---|---|
022ee6c5 AB |
1 | /* |
2 | * runtime-wrappers.c - Runtime Services function call wrappers | |
3 | * | |
4 | * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org> | |
5 | * | |
6 | * Split off from arch/x86/platform/efi/efi.c | |
7 | * | |
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 | |
13 | * | |
14 | * This file is released under the GPLv2. | |
15 | */ | |
16 | ||
161485e8 | 17 | #include <linux/bug.h> |
022ee6c5 | 18 | #include <linux/efi.h> |
1d04ba17 | 19 | #include <linux/irqflags.h> |
161485e8 AB |
20 | #include <linux/mutex.h> |
21 | #include <linux/spinlock.h> | |
1d04ba17 | 22 | #include <linux/stringify.h> |
022ee6c5 AB |
23 | #include <asm/efi.h> |
24 | ||
80e75596 AT |
25 | /* |
26 | * Wrap around the new efi_call_virt_generic() macros so that the | |
27 | * code doesn't get too cluttered: | |
28 | */ | |
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) | |
33 | ||
34 | void efi_call_virt_check_flags(unsigned long flags, const char *call) | |
1d04ba17 MR |
35 | { |
36 | unsigned long cur_flags, mismatch; | |
37 | ||
38 | local_save_flags(cur_flags); | |
39 | ||
40 | mismatch = flags ^ cur_flags; | |
41 | if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK)) | |
42 | return; | |
43 | ||
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); | |
48 | } | |
1d04ba17 | 49 | |
161485e8 AB |
50 | /* |
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) | |
54 | * | |
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() | | | |
65 | * | SetTime() | | | |
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 | * +------------------------------------+-------------------------------+ | |
82 | * | |
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. | |
88 | */ | |
89 | static DEFINE_SPINLOCK(efi_runtime_lock); | |
90 | ||
022ee6c5 AB |
91 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
92 | { | |
022ee6c5 AB |
93 | efi_status_t status; |
94 | ||
fe324494 | 95 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 96 | status = efi_call_virt(get_time, tm, tc); |
fe324494 | 97 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
98 | return status; |
99 | } | |
100 | ||
101 | static efi_status_t virt_efi_set_time(efi_time_t *tm) | |
102 | { | |
022ee6c5 AB |
103 | efi_status_t status; |
104 | ||
fe324494 | 105 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 106 | status = efi_call_virt(set_time, tm); |
fe324494 | 107 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
108 | return status; |
109 | } | |
110 | ||
111 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, | |
112 | efi_bool_t *pending, | |
113 | efi_time_t *tm) | |
114 | { | |
022ee6c5 AB |
115 | efi_status_t status; |
116 | ||
fe324494 | 117 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 118 | status = efi_call_virt(get_wakeup_time, enabled, pending, tm); |
fe324494 | 119 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
120 | return status; |
121 | } | |
122 | ||
123 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) | |
124 | { | |
022ee6c5 AB |
125 | efi_status_t status; |
126 | ||
fe324494 | 127 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 128 | status = efi_call_virt(set_wakeup_time, enabled, tm); |
fe324494 | 129 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
130 | return status; |
131 | } | |
132 | ||
133 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, | |
134 | efi_guid_t *vendor, | |
135 | u32 *attr, | |
136 | unsigned long *data_size, | |
137 | void *data) | |
138 | { | |
161485e8 AB |
139 | efi_status_t status; |
140 | ||
fe324494 | 141 | spin_lock(&efi_runtime_lock); |
161485e8 AB |
142 | status = efi_call_virt(get_variable, name, vendor, attr, data_size, |
143 | data); | |
fe324494 | 144 | spin_unlock(&efi_runtime_lock); |
161485e8 | 145 | return status; |
022ee6c5 AB |
146 | } |
147 | ||
148 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, | |
149 | efi_char16_t *name, | |
150 | efi_guid_t *vendor) | |
151 | { | |
161485e8 AB |
152 | efi_status_t status; |
153 | ||
fe324494 | 154 | spin_lock(&efi_runtime_lock); |
161485e8 | 155 | status = efi_call_virt(get_next_variable, name_size, name, vendor); |
fe324494 | 156 | spin_unlock(&efi_runtime_lock); |
161485e8 | 157 | return status; |
022ee6c5 AB |
158 | } |
159 | ||
160 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, | |
161 | efi_guid_t *vendor, | |
162 | u32 attr, | |
163 | unsigned long data_size, | |
164 | void *data) | |
165 | { | |
161485e8 | 166 | efi_status_t status; |
161485e8 | 167 | |
fe324494 | 168 | spin_lock(&efi_runtime_lock); |
161485e8 AB |
169 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, |
170 | data); | |
fe324494 | 171 | spin_unlock(&efi_runtime_lock); |
161485e8 | 172 | return status; |
022ee6c5 AB |
173 | } |
174 | ||
6d80dba1 MF |
175 | static efi_status_t |
176 | virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor, | |
177 | u32 attr, unsigned long data_size, | |
178 | void *data) | |
179 | { | |
6d80dba1 MF |
180 | efi_status_t status; |
181 | ||
fe324494 | 182 | if (!spin_trylock(&efi_runtime_lock)) |
6d80dba1 MF |
183 | return EFI_NOT_READY; |
184 | ||
185 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, | |
186 | data); | |
fe324494 | 187 | spin_unlock(&efi_runtime_lock); |
6d80dba1 MF |
188 | return status; |
189 | } | |
190 | ||
191 | ||
022ee6c5 AB |
192 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
193 | u64 *storage_space, | |
194 | u64 *remaining_space, | |
195 | u64 *max_variable_size) | |
196 | { | |
161485e8 | 197 | efi_status_t status; |
161485e8 | 198 | |
022ee6c5 AB |
199 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
200 | return EFI_UNSUPPORTED; | |
201 | ||
fe324494 | 202 | spin_lock(&efi_runtime_lock); |
161485e8 AB |
203 | status = efi_call_virt(query_variable_info, attr, storage_space, |
204 | remaining_space, max_variable_size); | |
fe324494 | 205 | spin_unlock(&efi_runtime_lock); |
161485e8 | 206 | return status; |
022ee6c5 AB |
207 | } |
208 | ||
d3cac1f8 AB |
209 | static efi_status_t |
210 | virt_efi_query_variable_info_nonblocking(u32 attr, | |
211 | u64 *storage_space, | |
212 | u64 *remaining_space, | |
213 | u64 *max_variable_size) | |
214 | { | |
d3cac1f8 AB |
215 | efi_status_t status; |
216 | ||
217 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
218 | return EFI_UNSUPPORTED; | |
219 | ||
fe324494 | 220 | if (!spin_trylock(&efi_runtime_lock)) |
d3cac1f8 AB |
221 | return EFI_NOT_READY; |
222 | ||
223 | status = efi_call_virt(query_variable_info, attr, storage_space, | |
224 | remaining_space, max_variable_size); | |
fe324494 | 225 | spin_unlock(&efi_runtime_lock); |
d3cac1f8 AB |
226 | return status; |
227 | } | |
228 | ||
022ee6c5 AB |
229 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
230 | { | |
161485e8 AB |
231 | efi_status_t status; |
232 | ||
fe324494 | 233 | spin_lock(&efi_runtime_lock); |
161485e8 | 234 | status = efi_call_virt(get_next_high_mono_count, count); |
fe324494 | 235 | spin_unlock(&efi_runtime_lock); |
161485e8 | 236 | return status; |
022ee6c5 AB |
237 | } |
238 | ||
239 | static void virt_efi_reset_system(int reset_type, | |
240 | efi_status_t status, | |
241 | unsigned long data_size, | |
242 | efi_char16_t *data) | |
243 | { | |
fe324494 | 244 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 245 | __efi_call_virt(reset_system, reset_type, status, data_size, data); |
fe324494 | 246 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
247 | } |
248 | ||
249 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, | |
250 | unsigned long count, | |
251 | unsigned long sg_list) | |
252 | { | |
161485e8 AB |
253 | efi_status_t status; |
254 | ||
022ee6c5 AB |
255 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
256 | return EFI_UNSUPPORTED; | |
257 | ||
fe324494 | 258 | spin_lock(&efi_runtime_lock); |
161485e8 | 259 | status = efi_call_virt(update_capsule, capsules, count, sg_list); |
fe324494 | 260 | spin_unlock(&efi_runtime_lock); |
161485e8 | 261 | return status; |
022ee6c5 AB |
262 | } |
263 | ||
264 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, | |
265 | unsigned long count, | |
266 | u64 *max_size, | |
267 | int *reset_type) | |
268 | { | |
161485e8 AB |
269 | efi_status_t status; |
270 | ||
022ee6c5 AB |
271 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
272 | return EFI_UNSUPPORTED; | |
273 | ||
fe324494 | 274 | spin_lock(&efi_runtime_lock); |
161485e8 AB |
275 | status = efi_call_virt(query_capsule_caps, capsules, count, max_size, |
276 | reset_type); | |
fe324494 | 277 | spin_unlock(&efi_runtime_lock); |
161485e8 | 278 | return status; |
022ee6c5 AB |
279 | } |
280 | ||
281 | void efi_native_runtime_setup(void) | |
282 | { | |
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; | |
6d80dba1 | 290 | efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking; |
022ee6c5 AB |
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; | |
d3cac1f8 | 294 | efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking; |
022ee6c5 AB |
295 | efi.update_capsule = virt_efi_update_capsule; |
296 | efi.query_capsule_caps = virt_efi_query_capsule_caps; | |
297 | } |