Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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