blob: 1606abead22ccd5285f493ea5ec28e7175488434 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * runtime-wrappers.c - Runtime Services function call wrappers
3 *
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().
12 *
13 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
14 *
15 * Split off from arch/x86/platform/efi/efi.c
16 *
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
22 *
23 * This file is released under the GPLv2.
24 */
25
26#define pr_fmt(fmt) "efi: " fmt
27
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>
36
37#include <asm/efi.h>
38
39/*
40 * Wrap around the new efi_call_virt_generic() macros so that the
41 * code doesn't get too cluttered:
42 */
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)
47
48/* efi_runtime_service() function identifiers */
49enum efi_rts_ids {
50 GET_TIME,
51 SET_TIME,
52 GET_WAKEUP_TIME,
53 SET_WAKEUP_TIME,
54 GET_VARIABLE,
55 GET_NEXT_VARIABLE,
56 SET_VARIABLE,
57 QUERY_VARIABLE_INFO,
58 GET_NEXT_HIGH_MONO_COUNT,
59 UPDATE_CAPSULE,
60 QUERY_CAPSULE_CAPS,
61};
62
63/*
64 * efi_runtime_work: Details of EFI Runtime Service work
65 * @arg<1-5>: EFI Runtime Service function arguments
66 * @status: Status of executing EFI Runtime Service
67 * @efi_rts_id: EFI Runtime Service function identifier
68 * @efi_rts_comp: Struct used for handling completions
69 */
70struct efi_runtime_work {
71 void *arg1;
72 void *arg2;
73 void *arg3;
74 void *arg4;
75 void *arg5;
76 efi_status_t status;
77 struct work_struct work;
78 enum efi_rts_ids efi_rts_id;
79 struct completion efi_rts_comp;
80};
81
82/*
83 * efi_queue_work: Queue efi_runtime_service() and wait until it's done
84 * @rts: efi_runtime_service() function identifier
85 * @rts_arg<1-5>: efi_runtime_service() function arguments
86 *
87 * Accesses to efi_runtime_services() are serialized by a binary
88 * semaphore (efi_runtime_lock) and caller waits until the work is
89 * finished, hence _only_ one work is queued at a time and the caller
90 * thread waits for completion.
91 */
92#define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \
93({ \
94 struct efi_runtime_work efi_rts_work; \
95 efi_rts_work.status = EFI_ABORTED; \
96 \
97 init_completion(&efi_rts_work.efi_rts_comp); \
98 INIT_WORK(&efi_rts_work.work, efi_call_rts); \
99 efi_rts_work.arg1 = _arg1; \
100 efi_rts_work.arg2 = _arg2; \
101 efi_rts_work.arg3 = _arg3; \
102 efi_rts_work.arg4 = _arg4; \
103 efi_rts_work.arg5 = _arg5; \
104 efi_rts_work.efi_rts_id = _rts; \
105 \
106 /* \
107 * queue_work() returns 0 if work was already on queue, \
108 * _ideally_ this should never happen. \
109 */ \
110 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
111 wait_for_completion(&efi_rts_work.efi_rts_comp); \
112 else \
113 pr_err("Failed to queue work to efi_rts_wq.\n"); \
114 \
115 efi_rts_work.status; \
116})
117
118void efi_call_virt_check_flags(unsigned long flags, const char *call)
119{
120 unsigned long cur_flags, mismatch;
121
122 local_save_flags(cur_flags);
123
124 mismatch = flags ^ cur_flags;
125 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
126 return;
127
128 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
129 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
130 flags, cur_flags, call);
131 local_irq_restore(flags);
132}
133
134/*
135 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
136 * reentrant, and there are particular combinations of calls that need to be
137 * serialized. (source: UEFI Specification v2.4A)
138 *
139 * Table 31. Rules for Reentry Into Runtime Services
140 * +------------------------------------+-------------------------------+
141 * | If previous call is busy in | Forbidden to call |
142 * +------------------------------------+-------------------------------+
143 * | Any | SetVirtualAddressMap() |
144 * +------------------------------------+-------------------------------+
145 * | ConvertPointer() | ConvertPointer() |
146 * +------------------------------------+-------------------------------+
147 * | SetVariable() | ResetSystem() |
148 * | UpdateCapsule() | |
149 * | SetTime() | |
150 * | SetWakeupTime() | |
151 * | GetNextHighMonotonicCount() | |
152 * +------------------------------------+-------------------------------+
153 * | GetVariable() | GetVariable() |
154 * | GetNextVariableName() | GetNextVariableName() |
155 * | SetVariable() | SetVariable() |
156 * | QueryVariableInfo() | QueryVariableInfo() |
157 * | UpdateCapsule() | UpdateCapsule() |
158 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
159 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
160 * +------------------------------------+-------------------------------+
161 * | GetTime() | GetTime() |
162 * | SetTime() | SetTime() |
163 * | GetWakeupTime() | GetWakeupTime() |
164 * | SetWakeupTime() | SetWakeupTime() |
165 * +------------------------------------+-------------------------------+
166 *
167 * Due to the fact that the EFI pstore may write to the variable store in
168 * interrupt context, we need to use a lock for at least the groups that
169 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
170 * none of the remaining functions are actually ever called at runtime.
171 * So let's just use a single lock to serialize all Runtime Services calls.
172 */
173static DEFINE_SEMAPHORE(efi_runtime_lock);
174
175/*
176 * Expose the EFI runtime lock to the UV platform
177 */
178#ifdef CONFIG_X86_UV
179extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
180#endif
181
182/*
183 * Calls the appropriate efi_runtime_service() with the appropriate
184 * arguments.
185 *
186 * Semantics followed by efi_call_rts() to understand efi_runtime_work:
187 * 1. If argument was a pointer, recast it from void pointer to original
188 * pointer type.
189 * 2. If argument was a value, recast it from void pointer to original
190 * pointer type and dereference it.
191 */
192static void efi_call_rts(struct work_struct *work)
193{
194 struct efi_runtime_work *efi_rts_work;
195 void *arg1, *arg2, *arg3, *arg4, *arg5;
196 efi_status_t status = EFI_NOT_FOUND;
197
198 efi_rts_work = container_of(work, struct efi_runtime_work, work);
199 arg1 = efi_rts_work->arg1;
200 arg2 = efi_rts_work->arg2;
201 arg3 = efi_rts_work->arg3;
202 arg4 = efi_rts_work->arg4;
203 arg5 = efi_rts_work->arg5;
204
205 switch (efi_rts_work->efi_rts_id) {
206 case GET_TIME:
207 status = efi_call_virt(get_time, (efi_time_t *)arg1,
208 (efi_time_cap_t *)arg2);
209 break;
210 case SET_TIME:
211 status = efi_call_virt(set_time, (efi_time_t *)arg1);
212 break;
213 case GET_WAKEUP_TIME:
214 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
215 (efi_bool_t *)arg2, (efi_time_t *)arg3);
216 break;
217 case SET_WAKEUP_TIME:
218 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
219 (efi_time_t *)arg2);
220 break;
221 case GET_VARIABLE:
222 status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
223 (efi_guid_t *)arg2, (u32 *)arg3,
224 (unsigned long *)arg4, (void *)arg5);
225 break;
226 case GET_NEXT_VARIABLE:
227 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
228 (efi_char16_t *)arg2,
229 (efi_guid_t *)arg3);
230 break;
231 case SET_VARIABLE:
232 status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
233 (efi_guid_t *)arg2, *(u32 *)arg3,
234 *(unsigned long *)arg4, (void *)arg5);
235 break;
236 case QUERY_VARIABLE_INFO:
237 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
238 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
239 break;
240 case GET_NEXT_HIGH_MONO_COUNT:
241 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
242 break;
243 case UPDATE_CAPSULE:
244 status = efi_call_virt(update_capsule,
245 (efi_capsule_header_t **)arg1,
246 *(unsigned long *)arg2,
247 *(unsigned long *)arg3);
248 break;
249 case QUERY_CAPSULE_CAPS:
250 status = efi_call_virt(query_capsule_caps,
251 (efi_capsule_header_t **)arg1,
252 *(unsigned long *)arg2, (u64 *)arg3,
253 (int *)arg4);
254 break;
255 default:
256 /*
257 * Ideally, we should never reach here because a caller of this
258 * function should have put the right efi_runtime_service()
259 * function identifier into efi_rts_work->efi_rts_id
260 */
261 pr_err("Requested executing invalid EFI Runtime Service.\n");
262 }
263 efi_rts_work->status = status;
264 complete(&efi_rts_work->efi_rts_comp);
265}
266
267static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
268{
269 efi_status_t status;
270
271 if (down_interruptible(&efi_runtime_lock))
272 return EFI_ABORTED;
273 status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL);
274 up(&efi_runtime_lock);
275 return status;
276}
277
278static efi_status_t virt_efi_set_time(efi_time_t *tm)
279{
280 efi_status_t status;
281
282 if (down_interruptible(&efi_runtime_lock))
283 return EFI_ABORTED;
284 status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL);
285 up(&efi_runtime_lock);
286 return status;
287}
288
289static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
290 efi_bool_t *pending,
291 efi_time_t *tm)
292{
293 efi_status_t status;
294
295 if (down_interruptible(&efi_runtime_lock))
296 return EFI_ABORTED;
297 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL,
298 NULL);
299 up(&efi_runtime_lock);
300 return status;
301}
302
303static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
304{
305 efi_status_t status;
306
307 if (down_interruptible(&efi_runtime_lock))
308 return EFI_ABORTED;
309 status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
310 NULL);
311 up(&efi_runtime_lock);
312 return status;
313}
314
315static efi_status_t virt_efi_get_variable(efi_char16_t *name,
316 efi_guid_t *vendor,
317 u32 *attr,
318 unsigned long *data_size,
319 void *data)
320{
321 efi_status_t status;
322
323 if (down_interruptible(&efi_runtime_lock))
324 return EFI_ABORTED;
325 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
326 data);
327 up(&efi_runtime_lock);
328 return status;
329}
330
331static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
332 efi_char16_t *name,
333 efi_guid_t *vendor)
334{
335 efi_status_t status;
336
337 if (down_interruptible(&efi_runtime_lock))
338 return EFI_ABORTED;
339 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor,
340 NULL, NULL);
341 up(&efi_runtime_lock);
342 return status;
343}
344
345static efi_status_t virt_efi_set_variable(efi_char16_t *name,
346 efi_guid_t *vendor,
347 u32 attr,
348 unsigned long data_size,
349 void *data)
350{
351 efi_status_t status;
352
353 if (down_interruptible(&efi_runtime_lock))
354 return EFI_ABORTED;
355 status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size,
356 data);
357 up(&efi_runtime_lock);
358 return status;
359}
360
361static efi_status_t
362virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
363 u32 attr, unsigned long data_size,
364 void *data)
365{
366 efi_status_t status;
367
368 if (down_trylock(&efi_runtime_lock))
369 return EFI_NOT_READY;
370
371 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
372 data);
373 up(&efi_runtime_lock);
374 return status;
375}
376
377
378static efi_status_t virt_efi_query_variable_info(u32 attr,
379 u64 *storage_space,
380 u64 *remaining_space,
381 u64 *max_variable_size)
382{
383 efi_status_t status;
384
385 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
386 return EFI_UNSUPPORTED;
387
388 if (down_interruptible(&efi_runtime_lock))
389 return EFI_ABORTED;
390 status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space,
391 remaining_space, max_variable_size, NULL);
392 up(&efi_runtime_lock);
393 return status;
394}
395
396static efi_status_t
397virt_efi_query_variable_info_nonblocking(u32 attr,
398 u64 *storage_space,
399 u64 *remaining_space,
400 u64 *max_variable_size)
401{
402 efi_status_t status;
403
404 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
405 return EFI_UNSUPPORTED;
406
407 if (down_trylock(&efi_runtime_lock))
408 return EFI_NOT_READY;
409
410 status = efi_call_virt(query_variable_info, attr, storage_space,
411 remaining_space, max_variable_size);
412 up(&efi_runtime_lock);
413 return status;
414}
415
416static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
417{
418 efi_status_t status;
419
420 if (down_interruptible(&efi_runtime_lock))
421 return EFI_ABORTED;
422 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
423 NULL, NULL);
424 up(&efi_runtime_lock);
425 return status;
426}
427
428static void virt_efi_reset_system(int reset_type,
429 efi_status_t status,
430 unsigned long data_size,
431 efi_char16_t *data)
432{
433 if (down_interruptible(&efi_runtime_lock)) {
434 pr_warn("failed to invoke the reset_system() runtime service:\n"
435 "could not get exclusive access to the firmware\n");
436 return;
437 }
438 __efi_call_virt(reset_system, reset_type, status, data_size, data);
439 up(&efi_runtime_lock);
440}
441
442static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
443 unsigned long count,
444 unsigned long sg_list)
445{
446 efi_status_t status;
447
448 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
449 return EFI_UNSUPPORTED;
450
451 if (down_interruptible(&efi_runtime_lock))
452 return EFI_ABORTED;
453 status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list,
454 NULL, NULL);
455 up(&efi_runtime_lock);
456 return status;
457}
458
459static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
460 unsigned long count,
461 u64 *max_size,
462 int *reset_type)
463{
464 efi_status_t status;
465
466 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
467 return EFI_UNSUPPORTED;
468
469 if (down_interruptible(&efi_runtime_lock))
470 return EFI_ABORTED;
471 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count,
472 max_size, reset_type, NULL);
473 up(&efi_runtime_lock);
474 return status;
475}
476
477void efi_native_runtime_setup(void)
478{
479 efi.get_time = virt_efi_get_time;
480 efi.set_time = virt_efi_set_time;
481 efi.get_wakeup_time = virt_efi_get_wakeup_time;
482 efi.set_wakeup_time = virt_efi_set_wakeup_time;
483 efi.get_variable = virt_efi_get_variable;
484 efi.get_next_variable = virt_efi_get_next_variable;
485 efi.set_variable = virt_efi_set_variable;
486 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
487 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
488 efi.reset_system = virt_efi_reset_system;
489 efi.query_variable_info = virt_efi_query_variable_info;
490 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
491 efi.update_capsule = virt_efi_update_capsule;
492 efi.query_capsule_caps = virt_efi_query_capsule_caps;
493}