155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2022ee6c5SArd Biesheuvel /*
3022ee6c5SArd Biesheuvel  * runtime-wrappers.c - Runtime Services function call wrappers
4022ee6c5SArd Biesheuvel  *
53eb420e7SSai Praneeth  * Implementation summary:
63eb420e7SSai Praneeth  * -----------------------
73eb420e7SSai Praneeth  * 1. When user/kernel thread requests to execute efi_runtime_service(),
83eb420e7SSai Praneeth  * enqueue work to efi_rts_wq.
93eb420e7SSai Praneeth  * 2. Caller thread waits for completion until the work is finished
103eb420e7SSai Praneeth  * because it's dependent on the return status and execution of
113eb420e7SSai Praneeth  * efi_runtime_service().
123eb420e7SSai Praneeth  * For instance, get_variable() and get_next_variable().
133eb420e7SSai Praneeth  *
14022ee6c5SArd Biesheuvel  * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
15022ee6c5SArd Biesheuvel  *
16022ee6c5SArd Biesheuvel  * Split off from arch/x86/platform/efi/efi.c
17022ee6c5SArd Biesheuvel  *
18022ee6c5SArd Biesheuvel  * Copyright (C) 1999 VA Linux Systems
19022ee6c5SArd Biesheuvel  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
20022ee6c5SArd Biesheuvel  * Copyright (C) 1999-2002 Hewlett-Packard Co.
21022ee6c5SArd Biesheuvel  * Copyright (C) 2005-2008 Intel Co.
22022ee6c5SArd Biesheuvel  * Copyright (C) 2013 SuSE Labs
23022ee6c5SArd Biesheuvel  */
24022ee6c5SArd Biesheuvel 
25dce48e35SArd Biesheuvel #define pr_fmt(fmt)	"efi: " fmt
26dce48e35SArd Biesheuvel 
27161485e8SArd Biesheuvel #include <linux/bug.h>
28022ee6c5SArd Biesheuvel #include <linux/efi.h>
291d04ba17SMark Rutland #include <linux/irqflags.h>
30161485e8SArd Biesheuvel #include <linux/mutex.h>
31dce48e35SArd Biesheuvel #include <linux/semaphore.h>
321d04ba17SMark Rutland #include <linux/stringify.h>
333eb420e7SSai Praneeth #include <linux/workqueue.h>
343eb420e7SSai Praneeth #include <linux/completion.h>
353eb420e7SSai Praneeth 
36022ee6c5SArd Biesheuvel #include <asm/efi.h>
37022ee6c5SArd Biesheuvel 
3880e75596SAlex Thorlton /*
3980e75596SAlex Thorlton  * Wrap around the new efi_call_virt_generic() macros so that the
4080e75596SAlex Thorlton  * code doesn't get too cluttered:
4180e75596SAlex Thorlton  */
4280e75596SAlex Thorlton #define efi_call_virt(f, args...)   \
433c17ae41SArd Biesheuvel 	arch_efi_call_virt(efi.runtime, f, args)
4480e75596SAlex Thorlton 
45c7c7bce0SArd Biesheuvel union efi_rts_args {
46c7c7bce0SArd Biesheuvel 	struct {
47c7c7bce0SArd Biesheuvel 		efi_time_t 	*time;
48c7c7bce0SArd Biesheuvel 		efi_time_cap_t	*capabilities;
49c7c7bce0SArd Biesheuvel 	} GET_TIME;
50c7c7bce0SArd Biesheuvel 
51c7c7bce0SArd Biesheuvel 	struct {
52c7c7bce0SArd Biesheuvel 		efi_time_t	*time;
53c7c7bce0SArd Biesheuvel 	} SET_TIME;
54c7c7bce0SArd Biesheuvel 
55c7c7bce0SArd Biesheuvel 	struct {
56c7c7bce0SArd Biesheuvel 		efi_bool_t	*enabled;
57c7c7bce0SArd Biesheuvel 		efi_bool_t	*pending;
58c7c7bce0SArd Biesheuvel 		efi_time_t	*time;
59c7c7bce0SArd Biesheuvel 	} GET_WAKEUP_TIME;
60c7c7bce0SArd Biesheuvel 
61c7c7bce0SArd Biesheuvel 	struct {
62c7c7bce0SArd Biesheuvel 		efi_bool_t	enable;
63c7c7bce0SArd Biesheuvel 		efi_time_t	*time;
64c7c7bce0SArd Biesheuvel 	} SET_WAKEUP_TIME;
65c7c7bce0SArd Biesheuvel 
66c7c7bce0SArd Biesheuvel 	struct {
67c7c7bce0SArd Biesheuvel 		efi_char16_t	*name;
68c7c7bce0SArd Biesheuvel 		efi_guid_t	*vendor;
69c7c7bce0SArd Biesheuvel 		u32		*attr;
70c7c7bce0SArd Biesheuvel 		unsigned long	*data_size;
71c7c7bce0SArd Biesheuvel 		void		*data;
72c7c7bce0SArd Biesheuvel 	} GET_VARIABLE;
73c7c7bce0SArd Biesheuvel 
74c7c7bce0SArd Biesheuvel 	struct {
75c7c7bce0SArd Biesheuvel 		unsigned long	*name_size;
76c7c7bce0SArd Biesheuvel 		efi_char16_t	*name;
77c7c7bce0SArd Biesheuvel 		efi_guid_t 	*vendor;
78c7c7bce0SArd Biesheuvel 	} GET_NEXT_VARIABLE;
79c7c7bce0SArd Biesheuvel 
80c7c7bce0SArd Biesheuvel 	struct {
81c7c7bce0SArd Biesheuvel 		efi_char16_t	*name;
82c7c7bce0SArd Biesheuvel 		efi_guid_t	*vendor;
83c7c7bce0SArd Biesheuvel 		u32		attr;
84c7c7bce0SArd Biesheuvel 		unsigned long	data_size;
85c7c7bce0SArd Biesheuvel 		void		*data;
86c7c7bce0SArd Biesheuvel 	} SET_VARIABLE;
87c7c7bce0SArd Biesheuvel 
88c7c7bce0SArd Biesheuvel 	struct {
89c7c7bce0SArd Biesheuvel 		u32		attr;
90c7c7bce0SArd Biesheuvel 		u64		*storage_space;
91c7c7bce0SArd Biesheuvel 		u64		*remaining_space;
92c7c7bce0SArd Biesheuvel 		u64		*max_variable_size;
93c7c7bce0SArd Biesheuvel 	} QUERY_VARIABLE_INFO;
94c7c7bce0SArd Biesheuvel 
95c7c7bce0SArd Biesheuvel 	struct {
96c7c7bce0SArd Biesheuvel 		u32		*high_count;
97c7c7bce0SArd Biesheuvel 	} GET_NEXT_HIGH_MONO_COUNT;
98c7c7bce0SArd Biesheuvel 
99c7c7bce0SArd Biesheuvel 	struct {
100c7c7bce0SArd Biesheuvel 		efi_capsule_header_t **capsules;
101c7c7bce0SArd Biesheuvel 		unsigned long	count;
102c7c7bce0SArd Biesheuvel 		unsigned long	sg_list;
103c7c7bce0SArd Biesheuvel 	} UPDATE_CAPSULE;
104c7c7bce0SArd Biesheuvel 
105c7c7bce0SArd Biesheuvel 	struct {
106c7c7bce0SArd Biesheuvel 		efi_capsule_header_t **capsules;
107c7c7bce0SArd Biesheuvel 		unsigned long	count;
108c7c7bce0SArd Biesheuvel 		u64		*max_size;
109c7c7bce0SArd Biesheuvel 		int		*reset_type;
110c7c7bce0SArd Biesheuvel 	} QUERY_CAPSULE_CAPS;
1115894cf57SArd Biesheuvel 
1125894cf57SArd Biesheuvel 	struct {
1135894cf57SArd Biesheuvel 		efi_status_t	(__efiapi *acpi_prm_handler)(u64, void *);
1145894cf57SArd Biesheuvel 		u64		param_buffer_addr;
1155894cf57SArd Biesheuvel 		void		*context;
1165894cf57SArd Biesheuvel 	} ACPI_PRM_HANDLER;
117c7c7bce0SArd Biesheuvel };
118c7c7bce0SArd Biesheuvel 
1199dbbedaaSSai Praneeth struct efi_runtime_work efi_rts_work;
1203eb420e7SSai Praneeth 
1213eb420e7SSai Praneeth /*
122c7c7bce0SArd Biesheuvel  * efi_queue_work:	Queue EFI runtime service call and wait for completion
123c7c7bce0SArd Biesheuvel  * @_rts:		EFI runtime service function identifier
124c7c7bce0SArd Biesheuvel  * @_args:		Arguments to pass to the EFI runtime service
1253eb420e7SSai Praneeth  *
1263eb420e7SSai Praneeth  * Accesses to efi_runtime_services() are serialized by a binary
1273eb420e7SSai Praneeth  * semaphore (efi_runtime_lock) and caller waits until the work is
1283eb420e7SSai Praneeth  * finished, hence _only_ one work is queued at a time and the caller
1293eb420e7SSai Praneeth  * thread waits for completion.
1303eb420e7SSai Praneeth  */
131c7c7bce0SArd Biesheuvel #define efi_queue_work(_rts, _args...)					\
132c99ba6e5SArd Biesheuvel 	__efi_queue_work(EFI_ ## _rts,					\
133c99ba6e5SArd Biesheuvel 			 &(union efi_rts_args){ ._rts = { _args }})
1343eb420e7SSai Praneeth 
13513b210ddSJulien Thierry #ifndef arch_efi_save_flags
13613b210ddSJulien Thierry #define arch_efi_save_flags(state_flags)	local_save_flags(state_flags)
13713b210ddSJulien Thierry #define arch_efi_restore_flags(state_flags)	local_irq_restore(state_flags)
13813b210ddSJulien Thierry #endif
13913b210ddSJulien Thierry 
efi_call_virt_save_flags(void)14013b210ddSJulien Thierry unsigned long efi_call_virt_save_flags(void)
14113b210ddSJulien Thierry {
14213b210ddSJulien Thierry 	unsigned long flags;
14313b210ddSJulien Thierry 
14413b210ddSJulien Thierry 	arch_efi_save_flags(flags);
14513b210ddSJulien Thierry 	return flags;
14613b210ddSJulien Thierry }
14713b210ddSJulien Thierry 
efi_call_virt_check_flags(unsigned long flags,const void * caller)1483c17ae41SArd Biesheuvel void efi_call_virt_check_flags(unsigned long flags, const void *caller)
1491d04ba17SMark Rutland {
1501d04ba17SMark Rutland 	unsigned long cur_flags, mismatch;
1511d04ba17SMark Rutland 
15213b210ddSJulien Thierry 	cur_flags = efi_call_virt_save_flags();
1531d04ba17SMark Rutland 
1541d04ba17SMark Rutland 	mismatch = flags ^ cur_flags;
1551d04ba17SMark Rutland 	if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
1561d04ba17SMark Rutland 		return;
1571d04ba17SMark Rutland 
1581d04ba17SMark Rutland 	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
1593c17ae41SArd Biesheuvel 	pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n",
1603c17ae41SArd Biesheuvel 			   flags, cur_flags, caller ?: __builtin_return_address(0));
16113b210ddSJulien Thierry 	arch_efi_restore_flags(flags);
1621d04ba17SMark Rutland }
1631d04ba17SMark Rutland 
1641d04ba17SMark Rutland /*
165161485e8SArd Biesheuvel  * According to section 7.1 of the UEFI spec, Runtime Services are not fully
166161485e8SArd Biesheuvel  * reentrant, and there are particular combinations of calls that need to be
167161485e8SArd Biesheuvel  * serialized. (source: UEFI Specification v2.4A)
168161485e8SArd Biesheuvel  *
169161485e8SArd Biesheuvel  * Table 31. Rules for Reentry Into Runtime Services
170161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
171161485e8SArd Biesheuvel  * | If previous call is busy in	| Forbidden to call		|
172161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
173161485e8SArd Biesheuvel  * | Any				| SetVirtualAddressMap()	|
174161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
175161485e8SArd Biesheuvel  * | ConvertPointer()			| ConvertPointer()		|
176161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
177161485e8SArd Biesheuvel  * | SetVariable()			| ResetSystem()			|
178161485e8SArd Biesheuvel  * | UpdateCapsule()			|				|
179161485e8SArd Biesheuvel  * | SetTime()				|				|
180161485e8SArd Biesheuvel  * | SetWakeupTime()			|				|
181161485e8SArd Biesheuvel  * | GetNextHighMonotonicCount()	|				|
182161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
183161485e8SArd Biesheuvel  * | GetVariable()			| GetVariable()			|
184161485e8SArd Biesheuvel  * | GetNextVariableName()		| GetNextVariableName()		|
185161485e8SArd Biesheuvel  * | SetVariable()			| SetVariable()			|
186161485e8SArd Biesheuvel  * | QueryVariableInfo()		| QueryVariableInfo()		|
187161485e8SArd Biesheuvel  * | UpdateCapsule()			| UpdateCapsule()		|
188161485e8SArd Biesheuvel  * | QueryCapsuleCapabilities()		| QueryCapsuleCapabilities()	|
189161485e8SArd Biesheuvel  * | GetNextHighMonotonicCount()	| GetNextHighMonotonicCount()	|
190161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
191161485e8SArd Biesheuvel  * | GetTime()				| GetTime()			|
192161485e8SArd Biesheuvel  * | SetTime()				| SetTime()			|
193161485e8SArd Biesheuvel  * | GetWakeupTime()			| GetWakeupTime()		|
194161485e8SArd Biesheuvel  * | SetWakeupTime()			| SetWakeupTime()		|
195161485e8SArd Biesheuvel  * +------------------------------------+-------------------------------+
196161485e8SArd Biesheuvel  *
197161485e8SArd Biesheuvel  * Due to the fact that the EFI pstore may write to the variable store in
198dce48e35SArd Biesheuvel  * interrupt context, we need to use a lock for at least the groups that
199161485e8SArd Biesheuvel  * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
200161485e8SArd Biesheuvel  * none of the remaining functions are actually ever called at runtime.
201dce48e35SArd Biesheuvel  * So let's just use a single lock to serialize all Runtime Services calls.
202161485e8SArd Biesheuvel  */
20348380368SPeter Zijlstra static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
204161485e8SArd Biesheuvel 
2053eb420e7SSai Praneeth /*
206f331e766SHedi Berriche  * Expose the EFI runtime lock to the UV platform
207f331e766SHedi Berriche  */
208f331e766SHedi Berriche #ifdef CONFIG_X86_UV
209f331e766SHedi Berriche extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
210f331e766SHedi Berriche #endif
211f331e766SHedi Berriche 
212f331e766SHedi Berriche /*
2133eb420e7SSai Praneeth  * Calls the appropriate efi_runtime_service() with the appropriate
2143eb420e7SSai Praneeth  * arguments.
2153eb420e7SSai Praneeth  */
efi_call_rts(struct work_struct * work)2163eb420e7SSai Praneeth static void efi_call_rts(struct work_struct *work)
2173eb420e7SSai Praneeth {
218c7c7bce0SArd Biesheuvel 	const union efi_rts_args *args = efi_rts_work.args;
2193eb420e7SSai Praneeth 	efi_status_t status = EFI_NOT_FOUND;
2203c17ae41SArd Biesheuvel 	unsigned long flags;
2213c17ae41SArd Biesheuvel 
2223c17ae41SArd Biesheuvel 	arch_efi_call_virt_setup();
2233c17ae41SArd Biesheuvel 	flags = efi_call_virt_save_flags();
2243eb420e7SSai Praneeth 
2259dbbedaaSSai Praneeth 	switch (efi_rts_work.efi_rts_id) {
2265c418dc7SAnders Roxell 	case EFI_GET_TIME:
227c7c7bce0SArd Biesheuvel 		status = efi_call_virt(get_time,
228c7c7bce0SArd Biesheuvel 				       args->GET_TIME.time,
229c7c7bce0SArd Biesheuvel 				       args->GET_TIME.capabilities);
2303eb420e7SSai Praneeth 		break;
2315c418dc7SAnders Roxell 	case EFI_SET_TIME:
232c7c7bce0SArd Biesheuvel 		status = efi_call_virt(set_time,
233c7c7bce0SArd Biesheuvel 				       args->SET_TIME.time);
2343eb420e7SSai Praneeth 		break;
2355c418dc7SAnders Roxell 	case EFI_GET_WAKEUP_TIME:
236c7c7bce0SArd Biesheuvel 		status = efi_call_virt(get_wakeup_time,
237c7c7bce0SArd Biesheuvel 				       args->GET_WAKEUP_TIME.enabled,
238c7c7bce0SArd Biesheuvel 				       args->GET_WAKEUP_TIME.pending,
239c7c7bce0SArd Biesheuvel 				       args->GET_WAKEUP_TIME.time);
2403eb420e7SSai Praneeth 		break;
2415c418dc7SAnders Roxell 	case EFI_SET_WAKEUP_TIME:
242c7c7bce0SArd Biesheuvel 		status = efi_call_virt(set_wakeup_time,
243c7c7bce0SArd Biesheuvel 				       args->SET_WAKEUP_TIME.enable,
244c7c7bce0SArd Biesheuvel 				       args->SET_WAKEUP_TIME.time);
2453eb420e7SSai Praneeth 		break;
2465c418dc7SAnders Roxell 	case EFI_GET_VARIABLE:
247c7c7bce0SArd Biesheuvel 		status = efi_call_virt(get_variable,
248c7c7bce0SArd Biesheuvel 				       args->GET_VARIABLE.name,
249c7c7bce0SArd Biesheuvel 				       args->GET_VARIABLE.vendor,
250c7c7bce0SArd Biesheuvel 				       args->GET_VARIABLE.attr,
251c7c7bce0SArd Biesheuvel 				       args->GET_VARIABLE.data_size,
252c7c7bce0SArd Biesheuvel 				       args->GET_VARIABLE.data);
2533eb420e7SSai Praneeth 		break;
2545c418dc7SAnders Roxell 	case EFI_GET_NEXT_VARIABLE:
255c7c7bce0SArd Biesheuvel 		status = efi_call_virt(get_next_variable,
256c7c7bce0SArd Biesheuvel 				       args->GET_NEXT_VARIABLE.name_size,
257c7c7bce0SArd Biesheuvel 				       args->GET_NEXT_VARIABLE.name,
258c7c7bce0SArd Biesheuvel 				       args->GET_NEXT_VARIABLE.vendor);
2593eb420e7SSai Praneeth 		break;
2605c418dc7SAnders Roxell 	case EFI_SET_VARIABLE:
261c7c7bce0SArd Biesheuvel 		status = efi_call_virt(set_variable,
262c7c7bce0SArd Biesheuvel 				       args->SET_VARIABLE.name,
263c7c7bce0SArd Biesheuvel 				       args->SET_VARIABLE.vendor,
264c7c7bce0SArd Biesheuvel 				       args->SET_VARIABLE.attr,
265c7c7bce0SArd Biesheuvel 				       args->SET_VARIABLE.data_size,
266c7c7bce0SArd Biesheuvel 				       args->SET_VARIABLE.data);
2673eb420e7SSai Praneeth 		break;
2685c418dc7SAnders Roxell 	case EFI_QUERY_VARIABLE_INFO:
269c7c7bce0SArd Biesheuvel 		status = efi_call_virt(query_variable_info,
270c7c7bce0SArd Biesheuvel 				       args->QUERY_VARIABLE_INFO.attr,
271c7c7bce0SArd Biesheuvel 				       args->QUERY_VARIABLE_INFO.storage_space,
272c7c7bce0SArd Biesheuvel 				       args->QUERY_VARIABLE_INFO.remaining_space,
273c7c7bce0SArd Biesheuvel 				       args->QUERY_VARIABLE_INFO.max_variable_size);
2743eb420e7SSai Praneeth 		break;
2755c418dc7SAnders Roxell 	case EFI_GET_NEXT_HIGH_MONO_COUNT:
276c7c7bce0SArd Biesheuvel 		status = efi_call_virt(get_next_high_mono_count,
277c7c7bce0SArd Biesheuvel 				       args->GET_NEXT_HIGH_MONO_COUNT.high_count);
2783eb420e7SSai Praneeth 		break;
2795c418dc7SAnders Roxell 	case EFI_UPDATE_CAPSULE:
2803eb420e7SSai Praneeth 		status = efi_call_virt(update_capsule,
281c7c7bce0SArd Biesheuvel 				       args->UPDATE_CAPSULE.capsules,
282c7c7bce0SArd Biesheuvel 				       args->UPDATE_CAPSULE.count,
283c7c7bce0SArd Biesheuvel 				       args->UPDATE_CAPSULE.sg_list);
2843eb420e7SSai Praneeth 		break;
2855c418dc7SAnders Roxell 	case EFI_QUERY_CAPSULE_CAPS:
2863eb420e7SSai Praneeth 		status = efi_call_virt(query_capsule_caps,
287c7c7bce0SArd Biesheuvel 				       args->QUERY_CAPSULE_CAPS.capsules,
288c7c7bce0SArd Biesheuvel 				       args->QUERY_CAPSULE_CAPS.count,
289c7c7bce0SArd Biesheuvel 				       args->QUERY_CAPSULE_CAPS.max_size,
290c7c7bce0SArd Biesheuvel 				       args->QUERY_CAPSULE_CAPS.reset_type);
2913eb420e7SSai Praneeth 		break;
2925894cf57SArd Biesheuvel 	case EFI_ACPI_PRM_HANDLER:
2935894cf57SArd Biesheuvel #ifdef CONFIG_ACPI_PRMT
2945894cf57SArd Biesheuvel 		status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler,
2955894cf57SArd Biesheuvel 					    args->ACPI_PRM_HANDLER.param_buffer_addr,
2965894cf57SArd Biesheuvel 					    args->ACPI_PRM_HANDLER.context);
2975894cf57SArd Biesheuvel 		break;
2985894cf57SArd Biesheuvel #endif
2993eb420e7SSai Praneeth 	default:
3003eb420e7SSai Praneeth 		/*
3013eb420e7SSai Praneeth 		 * Ideally, we should never reach here because a caller of this
3023eb420e7SSai Praneeth 		 * function should have put the right efi_runtime_service()
3033eb420e7SSai Praneeth 		 * function identifier into efi_rts_work->efi_rts_id
3043eb420e7SSai Praneeth 		 */
3053eb420e7SSai Praneeth 		pr_err("Requested executing invalid EFI Runtime Service.\n");
3063eb420e7SSai Praneeth 	}
3073c17ae41SArd Biesheuvel 
3083c17ae41SArd Biesheuvel 	efi_call_virt_check_flags(flags, efi_rts_work.caller);
3093c17ae41SArd Biesheuvel 	arch_efi_call_virt_teardown();
3103c17ae41SArd Biesheuvel 
3119dbbedaaSSai Praneeth 	efi_rts_work.status = status;
3129dbbedaaSSai Praneeth 	complete(&efi_rts_work.efi_rts_comp);
3133eb420e7SSai Praneeth }
3143eb420e7SSai Praneeth 
__efi_queue_work(enum efi_rts_ids id,union efi_rts_args * args)315c99ba6e5SArd Biesheuvel static efi_status_t __efi_queue_work(enum efi_rts_ids id,
316c99ba6e5SArd Biesheuvel 				     union efi_rts_args *args)
317c99ba6e5SArd Biesheuvel {
318c99ba6e5SArd Biesheuvel 	efi_rts_work.efi_rts_id = id;
319c99ba6e5SArd Biesheuvel 	efi_rts_work.args = args;
3203c17ae41SArd Biesheuvel 	efi_rts_work.caller = __builtin_return_address(0);
321c99ba6e5SArd Biesheuvel 	efi_rts_work.status = EFI_ABORTED;
322c99ba6e5SArd Biesheuvel 
323c99ba6e5SArd Biesheuvel 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
324c99ba6e5SArd Biesheuvel 		pr_warn_once("EFI Runtime Services are disabled!\n");
325c99ba6e5SArd Biesheuvel 		efi_rts_work.status = EFI_DEVICE_ERROR;
326c99ba6e5SArd Biesheuvel 		goto exit;
327c99ba6e5SArd Biesheuvel 	}
328c99ba6e5SArd Biesheuvel 
329c99ba6e5SArd Biesheuvel 	init_completion(&efi_rts_work.efi_rts_comp);
330c99ba6e5SArd Biesheuvel 	INIT_WORK(&efi_rts_work.work, efi_call_rts);
331c99ba6e5SArd Biesheuvel 
332c99ba6e5SArd Biesheuvel 	/*
333c99ba6e5SArd Biesheuvel 	 * queue_work() returns 0 if work was already on queue,
334c99ba6e5SArd Biesheuvel 	 * _ideally_ this should never happen.
335c99ba6e5SArd Biesheuvel 	 */
336c99ba6e5SArd Biesheuvel 	if (queue_work(efi_rts_wq, &efi_rts_work.work))
337c99ba6e5SArd Biesheuvel 		wait_for_completion(&efi_rts_work.efi_rts_comp);
338c99ba6e5SArd Biesheuvel 	else
339c99ba6e5SArd Biesheuvel 		pr_err("Failed to queue work to efi_rts_wq.\n");
340c99ba6e5SArd Biesheuvel 
341c99ba6e5SArd Biesheuvel 	WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);
342c99ba6e5SArd Biesheuvel exit:
343c99ba6e5SArd Biesheuvel 	efi_rts_work.efi_rts_id = EFI_NONE;
344c99ba6e5SArd Biesheuvel 	return efi_rts_work.status;
345c99ba6e5SArd Biesheuvel }
346c99ba6e5SArd Biesheuvel 
virt_efi_get_time(efi_time_t * tm,efi_time_cap_t * tc)347022ee6c5SArd Biesheuvel static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
348022ee6c5SArd Biesheuvel {
349022ee6c5SArd Biesheuvel 	efi_status_t status;
350022ee6c5SArd Biesheuvel 
351dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
352dce48e35SArd Biesheuvel 		return EFI_ABORTED;
353c7c7bce0SArd Biesheuvel 	status = efi_queue_work(GET_TIME, tm, tc);
354dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
355022ee6c5SArd Biesheuvel 	return status;
356022ee6c5SArd Biesheuvel }
357022ee6c5SArd Biesheuvel 
virt_efi_set_time(efi_time_t * tm)358022ee6c5SArd Biesheuvel static efi_status_t virt_efi_set_time(efi_time_t *tm)
359022ee6c5SArd Biesheuvel {
360022ee6c5SArd Biesheuvel 	efi_status_t status;
361022ee6c5SArd Biesheuvel 
362dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
363dce48e35SArd Biesheuvel 		return EFI_ABORTED;
364c7c7bce0SArd Biesheuvel 	status = efi_queue_work(SET_TIME, tm);
365dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
366022ee6c5SArd Biesheuvel 	return status;
367022ee6c5SArd Biesheuvel }
368022ee6c5SArd Biesheuvel 
virt_efi_get_wakeup_time(efi_bool_t * enabled,efi_bool_t * pending,efi_time_t * tm)369022ee6c5SArd Biesheuvel static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
370022ee6c5SArd Biesheuvel 					     efi_bool_t *pending,
371022ee6c5SArd Biesheuvel 					     efi_time_t *tm)
372022ee6c5SArd Biesheuvel {
373022ee6c5SArd Biesheuvel 	efi_status_t status;
374022ee6c5SArd Biesheuvel 
375dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
376dce48e35SArd Biesheuvel 		return EFI_ABORTED;
377c7c7bce0SArd Biesheuvel 	status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);
378dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
379022ee6c5SArd Biesheuvel 	return status;
380022ee6c5SArd Biesheuvel }
381022ee6c5SArd Biesheuvel 
virt_efi_set_wakeup_time(efi_bool_t enabled,efi_time_t * tm)382022ee6c5SArd Biesheuvel static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
383022ee6c5SArd Biesheuvel {
384022ee6c5SArd Biesheuvel 	efi_status_t status;
385022ee6c5SArd Biesheuvel 
386dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
387dce48e35SArd Biesheuvel 		return EFI_ABORTED;
388c7c7bce0SArd Biesheuvel 	status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);
389dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
390022ee6c5SArd Biesheuvel 	return status;
391022ee6c5SArd Biesheuvel }
392022ee6c5SArd Biesheuvel 
virt_efi_get_variable(efi_char16_t * name,efi_guid_t * vendor,u32 * attr,unsigned long * data_size,void * data)393022ee6c5SArd Biesheuvel static efi_status_t virt_efi_get_variable(efi_char16_t *name,
394022ee6c5SArd Biesheuvel 					  efi_guid_t *vendor,
395022ee6c5SArd Biesheuvel 					  u32 *attr,
396022ee6c5SArd Biesheuvel 					  unsigned long *data_size,
397022ee6c5SArd Biesheuvel 					  void *data)
398022ee6c5SArd Biesheuvel {
399161485e8SArd Biesheuvel 	efi_status_t status;
400161485e8SArd Biesheuvel 
401dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
402dce48e35SArd Biesheuvel 		return EFI_ABORTED;
403c7c7bce0SArd Biesheuvel 	status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
404161485e8SArd Biesheuvel 				data);
405dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
406161485e8SArd Biesheuvel 	return status;
407022ee6c5SArd Biesheuvel }
408022ee6c5SArd Biesheuvel 
virt_efi_get_next_variable(unsigned long * name_size,efi_char16_t * name,efi_guid_t * vendor)409022ee6c5SArd Biesheuvel static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
410022ee6c5SArd Biesheuvel 					       efi_char16_t *name,
411022ee6c5SArd Biesheuvel 					       efi_guid_t *vendor)
412022ee6c5SArd Biesheuvel {
413161485e8SArd Biesheuvel 	efi_status_t status;
414161485e8SArd Biesheuvel 
415dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
416dce48e35SArd Biesheuvel 		return EFI_ABORTED;
417c7c7bce0SArd Biesheuvel 	status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);
418dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
419161485e8SArd Biesheuvel 	return status;
420022ee6c5SArd Biesheuvel }
421022ee6c5SArd Biesheuvel 
virt_efi_set_variable(efi_char16_t * name,efi_guid_t * vendor,u32 attr,unsigned long data_size,void * data)422022ee6c5SArd Biesheuvel static efi_status_t virt_efi_set_variable(efi_char16_t *name,
423022ee6c5SArd Biesheuvel 					  efi_guid_t *vendor,
424022ee6c5SArd Biesheuvel 					  u32 attr,
425022ee6c5SArd Biesheuvel 					  unsigned long data_size,
426022ee6c5SArd Biesheuvel 					  void *data)
427022ee6c5SArd Biesheuvel {
428161485e8SArd Biesheuvel 	efi_status_t status;
429161485e8SArd Biesheuvel 
430dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
431dce48e35SArd Biesheuvel 		return EFI_ABORTED;
432c7c7bce0SArd Biesheuvel 	status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,
433161485e8SArd Biesheuvel 				data);
434dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
435161485e8SArd Biesheuvel 	return status;
436022ee6c5SArd Biesheuvel }
437022ee6c5SArd Biesheuvel 
4386d80dba1SMatt Fleming static efi_status_t
virt_efi_set_variable_nb(efi_char16_t * name,efi_guid_t * vendor,u32 attr,unsigned long data_size,void * data)439*a14198dfSArd Biesheuvel virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr,
440*a14198dfSArd Biesheuvel 			 unsigned long data_size, void *data)
4416d80dba1SMatt Fleming {
4426d80dba1SMatt Fleming 	efi_status_t status;
4436d80dba1SMatt Fleming 
444dce48e35SArd Biesheuvel 	if (down_trylock(&efi_runtime_lock))
4456d80dba1SMatt Fleming 		return EFI_NOT_READY;
4466d80dba1SMatt Fleming 
4473c17ae41SArd Biesheuvel 	status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor,
4483c17ae41SArd Biesheuvel 				       attr, data_size, data);
449dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
4506d80dba1SMatt Fleming 	return status;
4516d80dba1SMatt Fleming }
4526d80dba1SMatt Fleming 
4536d80dba1SMatt Fleming 
virt_efi_query_variable_info(u32 attr,u64 * storage_space,u64 * remaining_space,u64 * max_variable_size)454022ee6c5SArd Biesheuvel static efi_status_t virt_efi_query_variable_info(u32 attr,
455022ee6c5SArd Biesheuvel 						 u64 *storage_space,
456022ee6c5SArd Biesheuvel 						 u64 *remaining_space,
457022ee6c5SArd Biesheuvel 						 u64 *max_variable_size)
458022ee6c5SArd Biesheuvel {
459161485e8SArd Biesheuvel 	efi_status_t status;
460161485e8SArd Biesheuvel 
461022ee6c5SArd Biesheuvel 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
462022ee6c5SArd Biesheuvel 		return EFI_UNSUPPORTED;
463022ee6c5SArd Biesheuvel 
464dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
465dce48e35SArd Biesheuvel 		return EFI_ABORTED;
466c7c7bce0SArd Biesheuvel 	status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,
467c7c7bce0SArd Biesheuvel 				remaining_space, max_variable_size);
468dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
469161485e8SArd Biesheuvel 	return status;
470022ee6c5SArd Biesheuvel }
471022ee6c5SArd Biesheuvel 
472d3cac1f8SArd Biesheuvel static efi_status_t
virt_efi_query_variable_info_nb(u32 attr,u64 * storage_space,u64 * remaining_space,u64 * max_variable_size)473*a14198dfSArd Biesheuvel virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space,
474*a14198dfSArd Biesheuvel 				u64 *remaining_space, u64 *max_variable_size)
475d3cac1f8SArd Biesheuvel {
476d3cac1f8SArd Biesheuvel 	efi_status_t status;
477d3cac1f8SArd Biesheuvel 
478d3cac1f8SArd Biesheuvel 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
479d3cac1f8SArd Biesheuvel 		return EFI_UNSUPPORTED;
480d3cac1f8SArd Biesheuvel 
481dce48e35SArd Biesheuvel 	if (down_trylock(&efi_runtime_lock))
482d3cac1f8SArd Biesheuvel 		return EFI_NOT_READY;
483d3cac1f8SArd Biesheuvel 
4843c17ae41SArd Biesheuvel 	status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr,
4853c17ae41SArd Biesheuvel 				       storage_space, remaining_space,
4863c17ae41SArd Biesheuvel 				       max_variable_size);
487dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
488d3cac1f8SArd Biesheuvel 	return status;
489d3cac1f8SArd Biesheuvel }
490d3cac1f8SArd Biesheuvel 
virt_efi_get_next_high_mono_count(u32 * count)491022ee6c5SArd Biesheuvel static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
492022ee6c5SArd Biesheuvel {
493161485e8SArd Biesheuvel 	efi_status_t status;
494161485e8SArd Biesheuvel 
495dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
496dce48e35SArd Biesheuvel 		return EFI_ABORTED;
497c7c7bce0SArd Biesheuvel 	status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);
498dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
499161485e8SArd Biesheuvel 	return status;
500022ee6c5SArd Biesheuvel }
501022ee6c5SArd Biesheuvel 
virt_efi_reset_system(int reset_type,efi_status_t status,unsigned long data_size,efi_char16_t * data)502022ee6c5SArd Biesheuvel static void virt_efi_reset_system(int reset_type,
503022ee6c5SArd Biesheuvel 				  efi_status_t status,
504022ee6c5SArd Biesheuvel 				  unsigned long data_size,
505022ee6c5SArd Biesheuvel 				  efi_char16_t *data)
506022ee6c5SArd Biesheuvel {
50738fa3206SZhang Jianhua 	if (down_trylock(&efi_runtime_lock)) {
508dce48e35SArd Biesheuvel 		pr_warn("failed to invoke the reset_system() runtime service:\n"
509dce48e35SArd Biesheuvel 			"could not get exclusive access to the firmware\n");
510dce48e35SArd Biesheuvel 		return;
511dce48e35SArd Biesheuvel 	}
512e38abdabSArd Biesheuvel 
513e38abdabSArd Biesheuvel 	arch_efi_call_virt_setup();
5145c418dc7SAnders Roxell 	efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
515e38abdabSArd Biesheuvel 	arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,
516e38abdabSArd Biesheuvel 			   data_size, data);
517e38abdabSArd Biesheuvel 	arch_efi_call_virt_teardown();
518e38abdabSArd Biesheuvel 
519dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
520022ee6c5SArd Biesheuvel }
521022ee6c5SArd Biesheuvel 
virt_efi_update_capsule(efi_capsule_header_t ** capsules,unsigned long count,unsigned long sg_list)522022ee6c5SArd Biesheuvel static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
523022ee6c5SArd Biesheuvel 					    unsigned long count,
524022ee6c5SArd Biesheuvel 					    unsigned long sg_list)
525022ee6c5SArd Biesheuvel {
526161485e8SArd Biesheuvel 	efi_status_t status;
527161485e8SArd Biesheuvel 
528022ee6c5SArd Biesheuvel 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
529022ee6c5SArd Biesheuvel 		return EFI_UNSUPPORTED;
530022ee6c5SArd Biesheuvel 
531dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
532dce48e35SArd Biesheuvel 		return EFI_ABORTED;
533c7c7bce0SArd Biesheuvel 	status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);
534dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
535161485e8SArd Biesheuvel 	return status;
536022ee6c5SArd Biesheuvel }
537022ee6c5SArd Biesheuvel 
virt_efi_query_capsule_caps(efi_capsule_header_t ** capsules,unsigned long count,u64 * max_size,int * reset_type)538022ee6c5SArd Biesheuvel static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
539022ee6c5SArd Biesheuvel 						unsigned long count,
540022ee6c5SArd Biesheuvel 						u64 *max_size,
541022ee6c5SArd Biesheuvel 						int *reset_type)
542022ee6c5SArd Biesheuvel {
543161485e8SArd Biesheuvel 	efi_status_t status;
544161485e8SArd Biesheuvel 
545022ee6c5SArd Biesheuvel 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
546022ee6c5SArd Biesheuvel 		return EFI_UNSUPPORTED;
547022ee6c5SArd Biesheuvel 
548dce48e35SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
549dce48e35SArd Biesheuvel 		return EFI_ABORTED;
550c7c7bce0SArd Biesheuvel 	status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,
551c7c7bce0SArd Biesheuvel 				max_size, reset_type);
552dce48e35SArd Biesheuvel 	up(&efi_runtime_lock);
553161485e8SArd Biesheuvel 	return status;
554022ee6c5SArd Biesheuvel }
555022ee6c5SArd Biesheuvel 
efi_native_runtime_setup(void)556*a14198dfSArd Biesheuvel void __init efi_native_runtime_setup(void)
557022ee6c5SArd Biesheuvel {
558022ee6c5SArd Biesheuvel 	efi.get_time			    = virt_efi_get_time;
559022ee6c5SArd Biesheuvel 	efi.set_time			    = virt_efi_set_time;
560022ee6c5SArd Biesheuvel 	efi.get_wakeup_time		    = virt_efi_get_wakeup_time;
561022ee6c5SArd Biesheuvel 	efi.set_wakeup_time		    = virt_efi_set_wakeup_time;
562022ee6c5SArd Biesheuvel 	efi.get_variable		    = virt_efi_get_variable;
563022ee6c5SArd Biesheuvel 	efi.get_next_variable		    = virt_efi_get_next_variable;
564022ee6c5SArd Biesheuvel 	efi.set_variable		    = virt_efi_set_variable;
565*a14198dfSArd Biesheuvel 	efi.set_variable_nonblocking	    = virt_efi_set_variable_nb;
566022ee6c5SArd Biesheuvel 	efi.get_next_high_mono_count	    = virt_efi_get_next_high_mono_count;
567022ee6c5SArd Biesheuvel 	efi.reset_system 		    = virt_efi_reset_system;
568022ee6c5SArd Biesheuvel 	efi.query_variable_info		    = virt_efi_query_variable_info;
569*a14198dfSArd Biesheuvel 	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb;
570022ee6c5SArd Biesheuvel 	efi.update_capsule		    = virt_efi_update_capsule;
571022ee6c5SArd Biesheuvel 	efi.query_capsule_caps		    = virt_efi_query_capsule_caps;
572022ee6c5SArd Biesheuvel }
5735894cf57SArd Biesheuvel 
5745894cf57SArd Biesheuvel #ifdef CONFIG_ACPI_PRMT
5755894cf57SArd Biesheuvel 
5765894cf57SArd Biesheuvel efi_status_t
efi_call_acpi_prm_handler(efi_status_t (__efiapi * handler_addr)(u64,void *),u64 param_buffer_addr,void * context)5775894cf57SArd Biesheuvel efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *),
5785894cf57SArd Biesheuvel 			  u64 param_buffer_addr, void *context)
5795894cf57SArd Biesheuvel {
5805894cf57SArd Biesheuvel 	efi_status_t status;
5815894cf57SArd Biesheuvel 
5825894cf57SArd Biesheuvel 	if (down_interruptible(&efi_runtime_lock))
5835894cf57SArd Biesheuvel 		return EFI_ABORTED;
5845894cf57SArd Biesheuvel 	status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr,
5855894cf57SArd Biesheuvel 				param_buffer_addr, context);
5865894cf57SArd Biesheuvel 	up(&efi_runtime_lock);
5875894cf57SArd Biesheuvel 	return status;
5885894cf57SArd Biesheuvel }
5895894cf57SArd Biesheuvel 
5905894cf57SArd Biesheuvel #endif
591