xref: /linux/drivers/firmware/efi/runtime-wrappers.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
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 	arch_efi_call_virt(efi.runtime, f, args)
44 
45 union efi_rts_args {
46 	struct {
47 		efi_time_t 	*time;
48 		efi_time_cap_t	*capabilities;
49 	} GET_TIME;
50 
51 	struct {
52 		efi_time_t	*time;
53 	} SET_TIME;
54 
55 	struct {
56 		efi_bool_t	*enabled;
57 		efi_bool_t	*pending;
58 		efi_time_t	*time;
59 	} GET_WAKEUP_TIME;
60 
61 	struct {
62 		efi_bool_t	enable;
63 		efi_time_t	*time;
64 	} SET_WAKEUP_TIME;
65 
66 	struct {
67 		efi_char16_t	*name;
68 		efi_guid_t	*vendor;
69 		u32		*attr;
70 		unsigned long	*data_size;
71 		void		*data;
72 	} GET_VARIABLE;
73 
74 	struct {
75 		unsigned long	*name_size;
76 		efi_char16_t	*name;
77 		efi_guid_t 	*vendor;
78 	} GET_NEXT_VARIABLE;
79 
80 	struct {
81 		efi_char16_t	*name;
82 		efi_guid_t	*vendor;
83 		u32		attr;
84 		unsigned long	data_size;
85 		void		*data;
86 	} SET_VARIABLE;
87 
88 	struct {
89 		u32		attr;
90 		u64		*storage_space;
91 		u64		*remaining_space;
92 		u64		*max_variable_size;
93 	} QUERY_VARIABLE_INFO;
94 
95 	struct {
96 		u32		*high_count;
97 	} GET_NEXT_HIGH_MONO_COUNT;
98 
99 	struct {
100 		efi_capsule_header_t **capsules;
101 		unsigned long	count;
102 		unsigned long	sg_list;
103 	} UPDATE_CAPSULE;
104 
105 	struct {
106 		efi_capsule_header_t **capsules;
107 		unsigned long	count;
108 		u64		*max_size;
109 		int		*reset_type;
110 	} QUERY_CAPSULE_CAPS;
111 
112 	struct {
113 		efi_status_t	(__efiapi *acpi_prm_handler)(u64, void *);
114 		u64		param_buffer_addr;
115 		void		*context;
116 	} ACPI_PRM_HANDLER;
117 };
118 
119 struct efi_runtime_work efi_rts_work;
120 
121 /*
122  * Upper bound on how long we wait for a single EFI runtime service
123  * call to finish before declaring firmware wedged. Chosen to be longer
124  * than any plausible legitimate call (including UpdateCapsule on slow
125  * SPI-NOR) while still bounding userspace wait time.
126  */
127 #define EFI_RTS_TIMEOUT		(120 * HZ)
128 
129 /*
130  * Set when __efi_queue_work() gives up waiting for a call that is wedged in
131  * firmware. EFI runtime services are disabled for good at that point.
132  */
133 static bool efi_rts_abandoned;
134 
135 /*
136  * efi_queue_work:	Queue EFI runtime service call and wait for completion
137  * @_rts:		EFI runtime service function identifier
138  * @_args:		Arguments to pass to the EFI runtime service
139  *
140  * Accesses to efi_runtime_services() are serialized by a binary
141  * semaphore (efi_runtime_lock) and caller waits until the work is
142  * finished, hence _only_ one work is queued at a time and the caller
143  * thread waits for completion.
144  */
145 #define efi_queue_work(_rts, _args...)					\
146 	__efi_queue_work(EFI_ ## _rts,					\
147 			 &(union efi_rts_args){ ._rts = { _args }})
148 
149 #ifndef arch_efi_save_flags
150 #define arch_efi_save_flags(state_flags)	local_save_flags(state_flags)
151 #define arch_efi_restore_flags(state_flags)	local_irq_restore(state_flags)
152 #endif
153 
154 unsigned long efi_call_virt_save_flags(void)
155 {
156 	unsigned long flags;
157 
158 	arch_efi_save_flags(flags);
159 	return flags;
160 }
161 
162 void efi_call_virt_check_flags(unsigned long flags, const void *caller)
163 {
164 	unsigned long cur_flags, mismatch;
165 
166 	cur_flags = efi_call_virt_save_flags();
167 
168 	mismatch = flags ^ cur_flags;
169 	if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
170 		return;
171 
172 	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
173 	pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n",
174 			   flags, cur_flags, caller ?: __builtin_return_address(0));
175 	arch_efi_restore_flags(flags);
176 }
177 
178 /*
179  * According to section 7.1 of the UEFI spec, Runtime Services are not fully
180  * reentrant, and there are particular combinations of calls that need to be
181  * serialized. (source: UEFI Specification v2.4A)
182  *
183  * Table 31. Rules for Reentry Into Runtime Services
184  * +------------------------------------+-------------------------------+
185  * | If previous call is busy in	| Forbidden to call		|
186  * +------------------------------------+-------------------------------+
187  * | Any				| SetVirtualAddressMap()	|
188  * +------------------------------------+-------------------------------+
189  * | ConvertPointer()			| ConvertPointer()		|
190  * +------------------------------------+-------------------------------+
191  * | SetVariable()			| ResetSystem()			|
192  * | UpdateCapsule()			|				|
193  * | SetTime()				|				|
194  * | SetWakeupTime()			|				|
195  * | GetNextHighMonotonicCount()	|				|
196  * +------------------------------------+-------------------------------+
197  * | GetVariable()			| GetVariable()			|
198  * | GetNextVariableName()		| GetNextVariableName()		|
199  * | SetVariable()			| SetVariable()			|
200  * | QueryVariableInfo()		| QueryVariableInfo()		|
201  * | UpdateCapsule()			| UpdateCapsule()		|
202  * | QueryCapsuleCapabilities()		| QueryCapsuleCapabilities()	|
203  * | GetNextHighMonotonicCount()	| GetNextHighMonotonicCount()	|
204  * +------------------------------------+-------------------------------+
205  * | GetTime()				| GetTime()			|
206  * | SetTime()				| SetTime()			|
207  * | GetWakeupTime()			| GetWakeupTime()		|
208  * | SetWakeupTime()			| SetWakeupTime()		|
209  * +------------------------------------+-------------------------------+
210  *
211  * Due to the fact that the EFI pstore may write to the variable store in
212  * interrupt context, we need to use a lock for at least the groups that
213  * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
214  * none of the remaining functions are actually ever called at runtime.
215  * So let's just use a single lock to serialize all Runtime Services calls.
216  */
217 static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
218 
219 static struct task_struct *efi_runtime_lock_owner __used;
220 
221 /*
222  * Expose the EFI runtime lock to the UV platform
223  */
224 #ifdef CONFIG_X86_UV
225 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
226 #endif
227 
228 /*
229  * Park a worker that must never run efi_rts_wq again: EFI runtime services
230  * have been disabled and its efi_rts_work is abandoned. Loop in schedule()
231  * so a spurious wakeup cannot resume it.
232  */
233 void __noreturn efi_rts_park_worker(void)
234 {
235 	for (;;) {
236 		set_current_state(TASK_IDLE);
237 		schedule();
238 	}
239 }
240 
241 /*
242  * Calls the appropriate efi_runtime_service() with the appropriate
243  * arguments.
244  */
245 static void __nocfi efi_call_rts(struct work_struct *work)
246 {
247 	const union efi_rts_args *args = efi_rts_work.args;
248 	efi_status_t status = EFI_NOT_FOUND;
249 	unsigned long flags;
250 
251 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
252 		efi_rts_park_worker();
253 
254 	efi_runtime_lock_owner = current;
255 
256 	arch_efi_call_virt_setup();
257 	flags = efi_call_virt_save_flags();
258 
259 	switch (efi_rts_work.efi_rts_id) {
260 	case EFI_GET_TIME:
261 		status = efi_call_virt(get_time,
262 				       args->GET_TIME.time,
263 				       args->GET_TIME.capabilities);
264 		break;
265 	case EFI_SET_TIME:
266 		status = efi_call_virt(set_time,
267 				       args->SET_TIME.time);
268 		break;
269 	case EFI_GET_WAKEUP_TIME:
270 		status = efi_call_virt(get_wakeup_time,
271 				       args->GET_WAKEUP_TIME.enabled,
272 				       args->GET_WAKEUP_TIME.pending,
273 				       args->GET_WAKEUP_TIME.time);
274 		break;
275 	case EFI_SET_WAKEUP_TIME:
276 		status = efi_call_virt(set_wakeup_time,
277 				       args->SET_WAKEUP_TIME.enable,
278 				       args->SET_WAKEUP_TIME.time);
279 		break;
280 	case EFI_GET_VARIABLE:
281 		status = efi_call_virt(get_variable,
282 				       args->GET_VARIABLE.name,
283 				       args->GET_VARIABLE.vendor,
284 				       args->GET_VARIABLE.attr,
285 				       args->GET_VARIABLE.data_size,
286 				       args->GET_VARIABLE.data);
287 		break;
288 	case EFI_GET_NEXT_VARIABLE:
289 		status = efi_call_virt(get_next_variable,
290 				       args->GET_NEXT_VARIABLE.name_size,
291 				       args->GET_NEXT_VARIABLE.name,
292 				       args->GET_NEXT_VARIABLE.vendor);
293 		break;
294 	case EFI_SET_VARIABLE:
295 		status = efi_call_virt(set_variable,
296 				       args->SET_VARIABLE.name,
297 				       args->SET_VARIABLE.vendor,
298 				       args->SET_VARIABLE.attr,
299 				       args->SET_VARIABLE.data_size,
300 				       args->SET_VARIABLE.data);
301 		break;
302 	case EFI_QUERY_VARIABLE_INFO:
303 		status = efi_call_virt(query_variable_info,
304 				       args->QUERY_VARIABLE_INFO.attr,
305 				       args->QUERY_VARIABLE_INFO.storage_space,
306 				       args->QUERY_VARIABLE_INFO.remaining_space,
307 				       args->QUERY_VARIABLE_INFO.max_variable_size);
308 		break;
309 	case EFI_GET_NEXT_HIGH_MONO_COUNT:
310 		status = efi_call_virt(get_next_high_mono_count,
311 				       args->GET_NEXT_HIGH_MONO_COUNT.high_count);
312 		break;
313 	case EFI_UPDATE_CAPSULE:
314 		status = efi_call_virt(update_capsule,
315 				       args->UPDATE_CAPSULE.capsules,
316 				       args->UPDATE_CAPSULE.count,
317 				       args->UPDATE_CAPSULE.sg_list);
318 		break;
319 	case EFI_QUERY_CAPSULE_CAPS:
320 		status = efi_call_virt(query_capsule_caps,
321 				       args->QUERY_CAPSULE_CAPS.capsules,
322 				       args->QUERY_CAPSULE_CAPS.count,
323 				       args->QUERY_CAPSULE_CAPS.max_size,
324 				       args->QUERY_CAPSULE_CAPS.reset_type);
325 		break;
326 	case EFI_ACPI_PRM_HANDLER:
327 #ifdef CONFIG_ACPI_PRMT
328 		status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler,
329 					    args->ACPI_PRM_HANDLER.param_buffer_addr,
330 					    args->ACPI_PRM_HANDLER.context);
331 		break;
332 #endif
333 	default:
334 		/*
335 		 * Ideally, we should never reach here because a caller of this
336 		 * function should have put the right efi_runtime_service()
337 		 * function identifier into efi_rts_work->efi_rts_id
338 		 */
339 		pr_err("Requested executing invalid EFI Runtime Service.\n");
340 		break;
341 
342 #if IS_ENABLED(CONFIG_LKDTM)
343 	case EFI_LKDTM_RUNTIME_RO_TEST:
344 		/*
345 		 * Call GetTime() with the output argument pointing to
346 		 * executable memory, which should be mapped read-only in the
347 		 * EFI page tables.
348 		 */
349 		status = efi_call_virt(get_time,
350 				       (void *)efi.runtime->set_virtual_address_map,
351 				       NULL);
352 		break;
353 	case EFI_LKDTM_RUNTIME_NX_TEST:
354 		/*
355 		 * Call efi.runtime, which points to a data object in a EFI
356 		 * runtime region, as if it were a function pointer.
357 		 */
358 		{
359 			struct { efi_status_t (*f)(int); } *s = (void *)&efi.runtime;
360 
361 			status = arch_efi_call_virt(s, f, 0);
362 		}
363 		break;
364 #endif
365 	}
366 
367 	efi_call_virt_check_flags(flags, efi_rts_work.caller);
368 	arch_efi_call_virt_teardown();
369 
370 	/*
371 	 * EFI runtime services may also have been disabled because the
372 	 * firmware faulted, but the caller is still waiting for us in that
373 	 * case. Only park the worker if the caller has given up on it.
374 	 */
375 	if (READ_ONCE(efi_rts_abandoned))
376 		efi_rts_park_worker();
377 
378 	efi_rts_work.status = status;
379 	complete(&efi_rts_work.efi_rts_comp);
380 	efi_runtime_lock_owner = NULL;
381 }
382 
383 static efi_status_t __efi_queue_work(enum efi_rts_ids id,
384 				     union efi_rts_args *args)
385 {
386 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
387 		pr_warn_once("EFI Runtime Services are disabled!\n");
388 		return EFI_DEVICE_ERROR;
389 	}
390 
391 	efi_rts_work.efi_rts_id = id;
392 	efi_rts_work.args = args;
393 	efi_rts_work.caller = __builtin_return_address(0);
394 	efi_rts_work.status = EFI_ABORTED;
395 
396 	init_completion(&efi_rts_work.efi_rts_comp);
397 	INIT_WORK(&efi_rts_work.work, efi_call_rts);
398 
399 	/*
400 	 * queue_work() returns 0 if work was already on queue,
401 	 * _ideally_ this should never happen.
402 	 */
403 	if (!queue_work(efi_rts_wq, &efi_rts_work.work)) {
404 		pr_err("Failed to queue work to efi_rts_wq.\n");
405 		goto exit;
406 	}
407 
408 	if (!wait_for_completion_timeout(&efi_rts_work.efi_rts_comp,
409 					 EFI_RTS_TIMEOUT)) {
410 		pr_err("EFI runtime service %d wedged in firmware; disabling EFI runtime services\n",
411 		       id);
412 		WRITE_ONCE(efi_rts_abandoned, true);
413 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
414 		return EFI_ABORTED;
415 	}
416 
417 	WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);
418 exit:
419 	efi_rts_work.efi_rts_id = EFI_NONE;
420 	return efi_rts_work.status;
421 }
422 
423 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
424 {
425 	efi_status_t status;
426 
427 	if (down_interruptible(&efi_runtime_lock))
428 		return EFI_ABORTED;
429 	status = efi_queue_work(GET_TIME, tm, tc);
430 	up(&efi_runtime_lock);
431 	return status;
432 }
433 
434 static efi_status_t virt_efi_set_time(efi_time_t *tm)
435 {
436 	efi_status_t status;
437 
438 	if (down_interruptible(&efi_runtime_lock))
439 		return EFI_ABORTED;
440 	status = efi_queue_work(SET_TIME, tm);
441 	up(&efi_runtime_lock);
442 	return status;
443 }
444 
445 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
446 					     efi_bool_t *pending,
447 					     efi_time_t *tm)
448 {
449 	efi_status_t status;
450 
451 	if (down_interruptible(&efi_runtime_lock))
452 		return EFI_ABORTED;
453 	status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);
454 	up(&efi_runtime_lock);
455 	return status;
456 }
457 
458 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
459 {
460 	efi_status_t status;
461 
462 	if (down_interruptible(&efi_runtime_lock))
463 		return EFI_ABORTED;
464 	status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);
465 	up(&efi_runtime_lock);
466 	return status;
467 }
468 
469 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
470 					  efi_guid_t *vendor,
471 					  u32 *attr,
472 					  unsigned long *data_size,
473 					  void *data)
474 {
475 	efi_status_t status;
476 
477 	if (down_interruptible(&efi_runtime_lock))
478 		return EFI_ABORTED;
479 	status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
480 				data);
481 	up(&efi_runtime_lock);
482 	return status;
483 }
484 
485 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
486 					       efi_char16_t *name,
487 					       efi_guid_t *vendor)
488 {
489 	efi_status_t status;
490 
491 	if (down_interruptible(&efi_runtime_lock))
492 		return EFI_ABORTED;
493 	status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);
494 	up(&efi_runtime_lock);
495 	return status;
496 }
497 
498 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
499 					  efi_guid_t *vendor,
500 					  u32 attr,
501 					  unsigned long data_size,
502 					  void *data)
503 {
504 	efi_status_t status;
505 
506 	if (down_interruptible(&efi_runtime_lock))
507 		return EFI_ABORTED;
508 	status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,
509 				data);
510 	up(&efi_runtime_lock);
511 	return status;
512 }
513 
514 static efi_status_t __nocfi
515 virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
516 			 unsigned long data_size, void *data)
517 {
518 	efi_status_t status;
519 
520 	if (down_trylock(&efi_runtime_lock))
521 		return EFI_NOT_READY;
522 
523 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
524 		up(&efi_runtime_lock);
525 		return EFI_DEVICE_ERROR;
526 	}
527 
528 	efi_runtime_lock_owner = current;
529 	status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor,
530 				       attr, data_size, data);
531 	efi_runtime_lock_owner = NULL;
532 	up(&efi_runtime_lock);
533 	return status;
534 }
535 
536 
537 static efi_status_t virt_efi_query_variable_info(u32 attr,
538 						 u64 *storage_space,
539 						 u64 *remaining_space,
540 						 u64 *max_variable_size)
541 {
542 	efi_status_t status;
543 
544 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
545 		return EFI_UNSUPPORTED;
546 
547 	if (down_interruptible(&efi_runtime_lock))
548 		return EFI_ABORTED;
549 	status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,
550 				remaining_space, max_variable_size);
551 	up(&efi_runtime_lock);
552 	return status;
553 }
554 
555 static efi_status_t __nocfi
556 virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space,
557 				u64 *remaining_space, u64 *max_variable_size)
558 {
559 	efi_status_t status;
560 
561 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
562 		return EFI_UNSUPPORTED;
563 
564 	if (down_trylock(&efi_runtime_lock))
565 		return EFI_NOT_READY;
566 
567 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
568 		up(&efi_runtime_lock);
569 		return EFI_DEVICE_ERROR;
570 	}
571 
572 	efi_runtime_lock_owner = current;
573 	status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr,
574 				       storage_space, remaining_space,
575 				       max_variable_size);
576 	efi_runtime_lock_owner = NULL;
577 	up(&efi_runtime_lock);
578 	return status;
579 }
580 
581 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
582 {
583 	efi_status_t status;
584 
585 	if (down_interruptible(&efi_runtime_lock))
586 		return EFI_ABORTED;
587 	status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);
588 	up(&efi_runtime_lock);
589 	return status;
590 }
591 
592 static void __nocfi
593 virt_efi_reset_system(int reset_type, efi_status_t status,
594 		      unsigned long data_size, efi_char16_t *data)
595 {
596 	if (down_trylock(&efi_runtime_lock)) {
597 		pr_warn("failed to invoke the reset_system() runtime service:\n"
598 			"could not get exclusive access to the firmware\n");
599 		return;
600 	}
601 
602 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
603 		pr_warn("EFI Runtime Services are disabled, not invoking reset_system()\n");
604 		up(&efi_runtime_lock);
605 		return;
606 	}
607 
608 	efi_runtime_lock_owner = current;
609 	arch_efi_call_virt_setup();
610 	efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
611 	arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,
612 			   data_size, data);
613 	arch_efi_call_virt_teardown();
614 	efi_runtime_lock_owner = NULL;
615 	up(&efi_runtime_lock);
616 }
617 
618 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
619 					    unsigned long count,
620 					    unsigned long sg_list)
621 {
622 	efi_status_t status;
623 
624 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
625 		return EFI_UNSUPPORTED;
626 
627 	if (down_interruptible(&efi_runtime_lock))
628 		return EFI_ABORTED;
629 	status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);
630 	up(&efi_runtime_lock);
631 	return status;
632 }
633 
634 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
635 						unsigned long count,
636 						u64 *max_size,
637 						int *reset_type)
638 {
639 	efi_status_t status;
640 
641 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
642 		return EFI_UNSUPPORTED;
643 
644 	if (down_interruptible(&efi_runtime_lock))
645 		return EFI_ABORTED;
646 	status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,
647 				max_size, reset_type);
648 	up(&efi_runtime_lock);
649 	return status;
650 }
651 
652 #if IS_ENABLED(CONFIG_LKDTM)
653 static efi_status_t efi_lkdtm_rt_test(enum efi_rts_ids id)
654 {
655 	efi_status_t status;
656 
657 	if (!efi_enabled(EFI_RUNTIME_SERVICES) || !efi_enabled(EFI_MEM_ATTR))
658 		return EFI_NOT_READY;
659 
660 	switch (id) {
661 	case EFI_LKDTM_RUNTIME_NX_TEST:
662 		break;
663 	case EFI_LKDTM_RUNTIME_RO_TEST:
664 		if (efi.get_time &&
665 		    efi_rt_services_supported(EFI_RT_SUPPORTED_GET_TIME))
666 			break;
667 		fallthrough;
668 	default:
669 		return EFI_UNSUPPORTED;
670 	}
671 
672 	if (down_interruptible(&efi_runtime_lock))
673 		return EFI_NOT_READY;
674 	status = __efi_queue_work(id, NULL);
675 	up(&efi_runtime_lock);
676 	return status;
677 }
678 #endif
679 
680 void __init efi_native_runtime_setup(void)
681 {
682 	efi.get_time			    = virt_efi_get_time;
683 	efi.set_time			    = virt_efi_set_time;
684 	efi.get_wakeup_time		    = virt_efi_get_wakeup_time;
685 	efi.set_wakeup_time		    = virt_efi_set_wakeup_time;
686 	efi.get_variable		    = virt_efi_get_variable;
687 	efi.get_next_variable		    = virt_efi_get_next_variable;
688 	efi.set_variable		    = virt_efi_set_variable;
689 	efi.set_variable_nonblocking	    = virt_efi_set_variable_nb;
690 	efi.get_next_high_mono_count	    = virt_efi_get_next_high_mono_count;
691 	efi.reset_system 		    = virt_efi_reset_system;
692 	efi.query_variable_info		    = virt_efi_query_variable_info;
693 	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb;
694 	efi.update_capsule		    = virt_efi_update_capsule;
695 	efi.query_capsule_caps		    = virt_efi_query_capsule_caps;
696 
697 #if IS_ENABLED(CONFIG_LKDTM)
698 	efi.lkdtm_rt_test		    = efi_lkdtm_rt_test;
699 #endif
700 }
701 
702 #ifdef CONFIG_ACPI_PRMT
703 
704 efi_status_t
705 efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *),
706 			  u64 param_buffer_addr, void *context)
707 {
708 	efi_status_t status;
709 
710 	if (down_interruptible(&efi_runtime_lock))
711 		return EFI_ABORTED;
712 	status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr,
713 				param_buffer_addr, context);
714 	up(&efi_runtime_lock);
715 	return status;
716 }
717 
718 #endif
719 
720 void efi_runtime_assert_lock_held(void)
721 {
722 	WARN_ON(efi_runtime_lock_owner != current);
723 }
724 
725 efi_status_t efi_runtime_set_enable_flag(bool enable)
726 {
727 	static bool runtime_disabled;
728 	efi_status_t ret = EFI_NOT_READY;
729 
730 	if (down_interruptible(&efi_runtime_lock))
731 		return EFI_ABORTED;
732 
733 	if (enable) {
734 		/* It could be enabled only if it is disabled here */
735 		if (runtime_disabled) {
736 			set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
737 			runtime_disabled = false;
738 			ret = EFI_SUCCESS;
739 		}
740 	} else {
741 		if (efi_enabled(EFI_RUNTIME_SERVICES)) {
742 			clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
743 			runtime_disabled = true;
744 			ret = EFI_SUCCESS;
745 		}
746 	}
747 
748 	up(&efi_runtime_lock);
749 
750 	return ret;
751 }
752