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