1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * runtime-wrappers.c - Runtime Services function call wrappers
4 *
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().
13 *
14 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
15 *
16 * Split off from arch/x86/platform/efi/efi.c
17 *
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
23 */
24
25 #define pr_fmt(fmt) "efi: " fmt
26
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>
35
36 #include <asm/efi.h>
37
38 /*
39 * Wrap around the new efi_call_virt_generic() macros so that the
40 * code doesn't get too cluttered:
41 */
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)
46
47 struct efi_runtime_work efi_rts_work;
48
49 /*
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
53 *
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.
58 */
59 #define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \
60 ({ \
61 efi_rts_work.status = EFI_ABORTED; \
62 \
63 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { \
64 pr_warn_once("EFI Runtime Services are disabled!\n"); \
65 goto exit; \
66 } \
67 \
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; \
76 \
77 /* \
78 * queue_work() returns 0 if work was already on queue, \
79 * _ideally_ this should never happen. \
80 */ \
81 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
82 wait_for_completion(&efi_rts_work.efi_rts_comp); \
83 else \
84 pr_err("Failed to queue work to efi_rts_wq.\n"); \
85 \
86 exit: \
87 efi_rts_work.efi_rts_id = EFI_NONE; \
88 efi_rts_work.status; \
89 })
90
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)
94 #endif
95
efi_call_virt_save_flags(void)96 unsigned long efi_call_virt_save_flags(void)
97 {
98 unsigned long flags;
99
100 arch_efi_save_flags(flags);
101 return flags;
102 }
103
efi_call_virt_check_flags(unsigned long flags,const char * call)104 void efi_call_virt_check_flags(unsigned long flags, const char *call)
105 {
106 unsigned long cur_flags, mismatch;
107
108 cur_flags = efi_call_virt_save_flags();
109
110 mismatch = flags ^ cur_flags;
111 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
112 return;
113
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);
118 }
119
120 /*
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)
124 *
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() | |
135 * | SetTime() | |
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 * +------------------------------------+-------------------------------+
152 *
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.
158 */
159 static DEFINE_SEMAPHORE(efi_runtime_lock);
160
161 /*
162 * Expose the EFI runtime lock to the UV platform
163 */
164 #ifdef CONFIG_X86_UV
165 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
166 #endif
167
168 /*
169 * Calls the appropriate efi_runtime_service() with the appropriate
170 * arguments.
171 *
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
174 * pointer type.
175 * 2. If argument was a value, recast it from void pointer to original
176 * pointer type and dereference it.
177 */
efi_call_rts(struct work_struct * work)178 static void efi_call_rts(struct work_struct *work)
179 {
180 void *arg1, *arg2, *arg3, *arg4, *arg5;
181 efi_status_t status = EFI_NOT_FOUND;
182
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;
188
189 switch (efi_rts_work.efi_rts_id) {
190 case EFI_GET_TIME:
191 status = efi_call_virt(get_time, (efi_time_t *)arg1,
192 (efi_time_cap_t *)arg2);
193 break;
194 case EFI_SET_TIME:
195 status = efi_call_virt(set_time, (efi_time_t *)arg1);
196 break;
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);
200 break;
201 case EFI_SET_WAKEUP_TIME:
202 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
203 (efi_time_t *)arg2);
204 break;
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);
209 break;
210 case EFI_GET_NEXT_VARIABLE:
211 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
212 (efi_char16_t *)arg2,
213 (efi_guid_t *)arg3);
214 break;
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);
219 break;
220 case EFI_QUERY_VARIABLE_INFO:
221 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
222 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
223 break;
224 case EFI_GET_NEXT_HIGH_MONO_COUNT:
225 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
226 break;
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);
232 break;
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,
237 (int *)arg4);
238 break;
239 default:
240 /*
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
244 */
245 pr_err("Requested executing invalid EFI Runtime Service.\n");
246 }
247 efi_rts_work.status = status;
248 complete(&efi_rts_work.efi_rts_comp);
249 }
250
virt_efi_get_time(efi_time_t * tm,efi_time_cap_t * tc)251 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
252 {
253 efi_status_t status;
254
255 if (down_interruptible(&efi_runtime_lock))
256 return EFI_ABORTED;
257 status = efi_queue_work(EFI_GET_TIME, tm, tc, NULL, NULL, NULL);
258 up(&efi_runtime_lock);
259 return status;
260 }
261
virt_efi_set_time(efi_time_t * tm)262 static efi_status_t virt_efi_set_time(efi_time_t *tm)
263 {
264 efi_status_t status;
265
266 if (down_interruptible(&efi_runtime_lock))
267 return EFI_ABORTED;
268 status = efi_queue_work(EFI_SET_TIME, tm, NULL, NULL, NULL, NULL);
269 up(&efi_runtime_lock);
270 return status;
271 }
272
virt_efi_get_wakeup_time(efi_bool_t * enabled,efi_bool_t * pending,efi_time_t * tm)273 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
274 efi_bool_t *pending,
275 efi_time_t *tm)
276 {
277 efi_status_t status;
278
279 if (down_interruptible(&efi_runtime_lock))
280 return EFI_ABORTED;
281 status = efi_queue_work(EFI_GET_WAKEUP_TIME, enabled, pending, tm, NULL,
282 NULL);
283 up(&efi_runtime_lock);
284 return status;
285 }
286
virt_efi_set_wakeup_time(efi_bool_t enabled,efi_time_t * tm)287 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
288 {
289 efi_status_t status;
290
291 if (down_interruptible(&efi_runtime_lock))
292 return EFI_ABORTED;
293 status = efi_queue_work(EFI_SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
294 NULL);
295 up(&efi_runtime_lock);
296 return status;
297 }
298
virt_efi_get_variable(efi_char16_t * name,efi_guid_t * vendor,u32 * attr,unsigned long * data_size,void * data)299 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
300 efi_guid_t *vendor,
301 u32 *attr,
302 unsigned long *data_size,
303 void *data)
304 {
305 efi_status_t status;
306
307 if (down_interruptible(&efi_runtime_lock))
308 return EFI_ABORTED;
309 status = efi_queue_work(EFI_GET_VARIABLE, name, vendor, attr, data_size,
310 data);
311 up(&efi_runtime_lock);
312 return status;
313 }
314
virt_efi_get_next_variable(unsigned long * name_size,efi_char16_t * name,efi_guid_t * vendor)315 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
316 efi_char16_t *name,
317 efi_guid_t *vendor)
318 {
319 efi_status_t status;
320
321 if (down_interruptible(&efi_runtime_lock))
322 return EFI_ABORTED;
323 status = efi_queue_work(EFI_GET_NEXT_VARIABLE, name_size, name, vendor,
324 NULL, NULL);
325 up(&efi_runtime_lock);
326 return status;
327 }
328
virt_efi_set_variable(efi_char16_t * name,efi_guid_t * vendor,u32 attr,unsigned long data_size,void * data)329 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
330 efi_guid_t *vendor,
331 u32 attr,
332 unsigned long data_size,
333 void *data)
334 {
335 efi_status_t status;
336
337 if (down_interruptible(&efi_runtime_lock))
338 return EFI_ABORTED;
339 status = efi_queue_work(EFI_SET_VARIABLE, name, vendor, &attr, &data_size,
340 data);
341 up(&efi_runtime_lock);
342 return status;
343 }
344
345 static efi_status_t
virt_efi_set_variable_nonblocking(efi_char16_t * name,efi_guid_t * vendor,u32 attr,unsigned long data_size,void * data)346 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
347 u32 attr, unsigned long data_size,
348 void *data)
349 {
350 efi_status_t status;
351
352 if (down_trylock(&efi_runtime_lock))
353 return EFI_NOT_READY;
354
355 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
356 data);
357 up(&efi_runtime_lock);
358 return status;
359 }
360
361
virt_efi_query_variable_info(u32 attr,u64 * storage_space,u64 * remaining_space,u64 * max_variable_size)362 static efi_status_t virt_efi_query_variable_info(u32 attr,
363 u64 *storage_space,
364 u64 *remaining_space,
365 u64 *max_variable_size)
366 {
367 efi_status_t status;
368
369 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
370 return EFI_UNSUPPORTED;
371
372 if (down_interruptible(&efi_runtime_lock))
373 return EFI_ABORTED;
374 status = efi_queue_work(EFI_QUERY_VARIABLE_INFO, &attr, storage_space,
375 remaining_space, max_variable_size, NULL);
376 up(&efi_runtime_lock);
377 return status;
378 }
379
380 static efi_status_t
virt_efi_query_variable_info_nonblocking(u32 attr,u64 * storage_space,u64 * remaining_space,u64 * max_variable_size)381 virt_efi_query_variable_info_nonblocking(u32 attr,
382 u64 *storage_space,
383 u64 *remaining_space,
384 u64 *max_variable_size)
385 {
386 efi_status_t status;
387
388 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
389 return EFI_UNSUPPORTED;
390
391 if (down_trylock(&efi_runtime_lock))
392 return EFI_NOT_READY;
393
394 status = efi_call_virt(query_variable_info, attr, storage_space,
395 remaining_space, max_variable_size);
396 up(&efi_runtime_lock);
397 return status;
398 }
399
virt_efi_get_next_high_mono_count(u32 * count)400 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
401 {
402 efi_status_t status;
403
404 if (down_interruptible(&efi_runtime_lock))
405 return EFI_ABORTED;
406 status = efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
407 NULL, NULL);
408 up(&efi_runtime_lock);
409 return status;
410 }
411
virt_efi_reset_system(int reset_type,efi_status_t status,unsigned long data_size,efi_char16_t * data)412 static void virt_efi_reset_system(int reset_type,
413 efi_status_t status,
414 unsigned long data_size,
415 efi_char16_t *data)
416 {
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");
420 return;
421 }
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);
425 }
426
virt_efi_update_capsule(efi_capsule_header_t ** capsules,unsigned long count,unsigned long sg_list)427 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
428 unsigned long count,
429 unsigned long sg_list)
430 {
431 efi_status_t status;
432
433 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
434 return EFI_UNSUPPORTED;
435
436 if (down_interruptible(&efi_runtime_lock))
437 return EFI_ABORTED;
438 status = efi_queue_work(EFI_UPDATE_CAPSULE, capsules, &count, &sg_list,
439 NULL, NULL);
440 up(&efi_runtime_lock);
441 return status;
442 }
443
virt_efi_query_capsule_caps(efi_capsule_header_t ** capsules,unsigned long count,u64 * max_size,int * reset_type)444 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
445 unsigned long count,
446 u64 *max_size,
447 int *reset_type)
448 {
449 efi_status_t status;
450
451 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
452 return EFI_UNSUPPORTED;
453
454 if (down_interruptible(&efi_runtime_lock))
455 return EFI_ABORTED;
456 status = efi_queue_work(EFI_QUERY_CAPSULE_CAPS, capsules, &count,
457 max_size, reset_type, NULL);
458 up(&efi_runtime_lock);
459 return status;
460 }
461
efi_native_runtime_setup(void)462 void efi_native_runtime_setup(void)
463 {
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;
478 }
479