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 
45 union efi_rts_args {
46 	struct {
47 		efi_time_t 	*time;
48 		efi_time_cap_t	*capabilities;
49 	} GET_TIME;
50 
51 	struct {
52 		efi_time_t	*time;
53 	} SET_TIME;
54 
55 	struct {
56 		efi_bool_t	*enabled;
57 		efi_bool_t	*pending;
58 		efi_time_t	*time;
59 	} GET_WAKEUP_TIME;
60 
61 	struct {
62 		efi_bool_t	enable;
63 		efi_time_t	*time;
64 	} SET_WAKEUP_TIME;
65 
66 	struct {
67 		efi_char16_t	*name;
68 		efi_guid_t	*vendor;
69 		u32		*attr;
70 		unsigned long	*data_size;
71 		void		*data;
72 	} GET_VARIABLE;
73 
74 	struct {
75 		unsigned long	*name_size;
76 		efi_char16_t	*name;
77 		efi_guid_t 	*vendor;
78 	} GET_NEXT_VARIABLE;
79 
80 	struct {
81 		efi_char16_t	*name;
82 		efi_guid_t	*vendor;
83 		u32		attr;
84 		unsigned long	data_size;
85 		void		*data;
86 	} SET_VARIABLE;
87 
88 	struct {
89 		u32		attr;
90 		u64		*storage_space;
91 		u64		*remaining_space;
92 		u64		*max_variable_size;
93 	} QUERY_VARIABLE_INFO;
94 
95 	struct {
96 		u32		*high_count;
97 	} GET_NEXT_HIGH_MONO_COUNT;
98 
99 	struct {
100 		efi_capsule_header_t **capsules;
101 		unsigned long	count;
102 		unsigned long	sg_list;
103 	} UPDATE_CAPSULE;
104 
105 	struct {
106 		efi_capsule_header_t **capsules;
107 		unsigned long	count;
108 		u64		*max_size;
109 		int		*reset_type;
110 	} QUERY_CAPSULE_CAPS;
111 };
112 
113 struct efi_runtime_work efi_rts_work;
114 
115 /*
116  * efi_queue_work:	Queue EFI runtime service call and wait for completion
117  * @_rts:		EFI runtime service function identifier
118  * @_args:		Arguments to pass to the EFI runtime service
119  *
120  * Accesses to efi_runtime_services() are serialized by a binary
121  * semaphore (efi_runtime_lock) and caller waits until the work is
122  * finished, hence _only_ one work is queued at a time and the caller
123  * thread waits for completion.
124  */
125 #define efi_queue_work(_rts, _args...)					\
126 	__efi_queue_work(EFI_ ## _rts,					\
127 			 &(union efi_rts_args){ ._rts = { _args }})
128 
129 #ifndef arch_efi_save_flags
130 #define arch_efi_save_flags(state_flags)	local_save_flags(state_flags)
131 #define arch_efi_restore_flags(state_flags)	local_irq_restore(state_flags)
132 #endif
133 
134 unsigned long efi_call_virt_save_flags(void)
135 {
136 	unsigned long flags;
137 
138 	arch_efi_save_flags(flags);
139 	return flags;
140 }
141 
142 void efi_call_virt_check_flags(unsigned long flags, const char *call)
143 {
144 	unsigned long cur_flags, mismatch;
145 
146 	cur_flags = efi_call_virt_save_flags();
147 
148 	mismatch = flags ^ cur_flags;
149 	if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
150 		return;
151 
152 	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
153 	pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
154 			   flags, cur_flags, call);
155 	arch_efi_restore_flags(flags);
156 }
157 
158 /*
159  * According to section 7.1 of the UEFI spec, Runtime Services are not fully
160  * reentrant, and there are particular combinations of calls that need to be
161  * serialized. (source: UEFI Specification v2.4A)
162  *
163  * Table 31. Rules for Reentry Into Runtime Services
164  * +------------------------------------+-------------------------------+
165  * | If previous call is busy in	| Forbidden to call		|
166  * +------------------------------------+-------------------------------+
167  * | Any				| SetVirtualAddressMap()	|
168  * +------------------------------------+-------------------------------+
169  * | ConvertPointer()			| ConvertPointer()		|
170  * +------------------------------------+-------------------------------+
171  * | SetVariable()			| ResetSystem()			|
172  * | UpdateCapsule()			|				|
173  * | SetTime()				|				|
174  * | SetWakeupTime()			|				|
175  * | GetNextHighMonotonicCount()	|				|
176  * +------------------------------------+-------------------------------+
177  * | GetVariable()			| GetVariable()			|
178  * | GetNextVariableName()		| GetNextVariableName()		|
179  * | SetVariable()			| SetVariable()			|
180  * | QueryVariableInfo()		| QueryVariableInfo()		|
181  * | UpdateCapsule()			| UpdateCapsule()		|
182  * | QueryCapsuleCapabilities()		| QueryCapsuleCapabilities()	|
183  * | GetNextHighMonotonicCount()	| GetNextHighMonotonicCount()	|
184  * +------------------------------------+-------------------------------+
185  * | GetTime()				| GetTime()			|
186  * | SetTime()				| SetTime()			|
187  * | GetWakeupTime()			| GetWakeupTime()		|
188  * | SetWakeupTime()			| SetWakeupTime()		|
189  * +------------------------------------+-------------------------------+
190  *
191  * Due to the fact that the EFI pstore may write to the variable store in
192  * interrupt context, we need to use a lock for at least the groups that
193  * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
194  * none of the remaining functions are actually ever called at runtime.
195  * So let's just use a single lock to serialize all Runtime Services calls.
196  */
197 static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
198 
199 /*
200  * Expose the EFI runtime lock to the UV platform
201  */
202 #ifdef CONFIG_X86_UV
203 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
204 #endif
205 
206 /*
207  * Calls the appropriate efi_runtime_service() with the appropriate
208  * arguments.
209  */
210 static void efi_call_rts(struct work_struct *work)
211 {
212 	const union efi_rts_args *args = efi_rts_work.args;
213 	efi_status_t status = EFI_NOT_FOUND;
214 
215 	switch (efi_rts_work.efi_rts_id) {
216 	case EFI_GET_TIME:
217 		status = efi_call_virt(get_time,
218 				       args->GET_TIME.time,
219 				       args->GET_TIME.capabilities);
220 		break;
221 	case EFI_SET_TIME:
222 		status = efi_call_virt(set_time,
223 				       args->SET_TIME.time);
224 		break;
225 	case EFI_GET_WAKEUP_TIME:
226 		status = efi_call_virt(get_wakeup_time,
227 				       args->GET_WAKEUP_TIME.enabled,
228 				       args->GET_WAKEUP_TIME.pending,
229 				       args->GET_WAKEUP_TIME.time);
230 		break;
231 	case EFI_SET_WAKEUP_TIME:
232 		status = efi_call_virt(set_wakeup_time,
233 				       args->SET_WAKEUP_TIME.enable,
234 				       args->SET_WAKEUP_TIME.time);
235 		break;
236 	case EFI_GET_VARIABLE:
237 		status = efi_call_virt(get_variable,
238 				       args->GET_VARIABLE.name,
239 				       args->GET_VARIABLE.vendor,
240 				       args->GET_VARIABLE.attr,
241 				       args->GET_VARIABLE.data_size,
242 				       args->GET_VARIABLE.data);
243 		break;
244 	case EFI_GET_NEXT_VARIABLE:
245 		status = efi_call_virt(get_next_variable,
246 				       args->GET_NEXT_VARIABLE.name_size,
247 				       args->GET_NEXT_VARIABLE.name,
248 				       args->GET_NEXT_VARIABLE.vendor);
249 		break;
250 	case EFI_SET_VARIABLE:
251 		status = efi_call_virt(set_variable,
252 				       args->SET_VARIABLE.name,
253 				       args->SET_VARIABLE.vendor,
254 				       args->SET_VARIABLE.attr,
255 				       args->SET_VARIABLE.data_size,
256 				       args->SET_VARIABLE.data);
257 		break;
258 	case EFI_QUERY_VARIABLE_INFO:
259 		status = efi_call_virt(query_variable_info,
260 				       args->QUERY_VARIABLE_INFO.attr,
261 				       args->QUERY_VARIABLE_INFO.storage_space,
262 				       args->QUERY_VARIABLE_INFO.remaining_space,
263 				       args->QUERY_VARIABLE_INFO.max_variable_size);
264 		break;
265 	case EFI_GET_NEXT_HIGH_MONO_COUNT:
266 		status = efi_call_virt(get_next_high_mono_count,
267 				       args->GET_NEXT_HIGH_MONO_COUNT.high_count);
268 		break;
269 	case EFI_UPDATE_CAPSULE:
270 		status = efi_call_virt(update_capsule,
271 				       args->UPDATE_CAPSULE.capsules,
272 				       args->UPDATE_CAPSULE.count,
273 				       args->UPDATE_CAPSULE.sg_list);
274 		break;
275 	case EFI_QUERY_CAPSULE_CAPS:
276 		status = efi_call_virt(query_capsule_caps,
277 				       args->QUERY_CAPSULE_CAPS.capsules,
278 				       args->QUERY_CAPSULE_CAPS.count,
279 				       args->QUERY_CAPSULE_CAPS.max_size,
280 				       args->QUERY_CAPSULE_CAPS.reset_type);
281 		break;
282 	default:
283 		/*
284 		 * Ideally, we should never reach here because a caller of this
285 		 * function should have put the right efi_runtime_service()
286 		 * function identifier into efi_rts_work->efi_rts_id
287 		 */
288 		pr_err("Requested executing invalid EFI Runtime Service.\n");
289 	}
290 	efi_rts_work.status = status;
291 	complete(&efi_rts_work.efi_rts_comp);
292 }
293 
294 static efi_status_t __efi_queue_work(enum efi_rts_ids id,
295 				     union efi_rts_args *args)
296 {
297 	efi_rts_work.efi_rts_id = id;
298 	efi_rts_work.args = args;
299 	efi_rts_work.status = EFI_ABORTED;
300 
301 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
302 		pr_warn_once("EFI Runtime Services are disabled!\n");
303 		efi_rts_work.status = EFI_DEVICE_ERROR;
304 		goto exit;
305 	}
306 
307 	init_completion(&efi_rts_work.efi_rts_comp);
308 	INIT_WORK(&efi_rts_work.work, efi_call_rts);
309 
310 	/*
311 	 * queue_work() returns 0 if work was already on queue,
312 	 * _ideally_ this should never happen.
313 	 */
314 	if (queue_work(efi_rts_wq, &efi_rts_work.work))
315 		wait_for_completion(&efi_rts_work.efi_rts_comp);
316 	else
317 		pr_err("Failed to queue work to efi_rts_wq.\n");
318 
319 	WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);
320 exit:
321 	efi_rts_work.efi_rts_id = EFI_NONE;
322 	return efi_rts_work.status;
323 }
324 
325 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
326 {
327 	efi_status_t status;
328 
329 	if (down_interruptible(&efi_runtime_lock))
330 		return EFI_ABORTED;
331 	status = efi_queue_work(GET_TIME, tm, tc);
332 	up(&efi_runtime_lock);
333 	return status;
334 }
335 
336 static efi_status_t virt_efi_set_time(efi_time_t *tm)
337 {
338 	efi_status_t status;
339 
340 	if (down_interruptible(&efi_runtime_lock))
341 		return EFI_ABORTED;
342 	status = efi_queue_work(SET_TIME, tm);
343 	up(&efi_runtime_lock);
344 	return status;
345 }
346 
347 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
348 					     efi_bool_t *pending,
349 					     efi_time_t *tm)
350 {
351 	efi_status_t status;
352 
353 	if (down_interruptible(&efi_runtime_lock))
354 		return EFI_ABORTED;
355 	status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);
356 	up(&efi_runtime_lock);
357 	return status;
358 }
359 
360 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
361 {
362 	efi_status_t status;
363 
364 	if (down_interruptible(&efi_runtime_lock))
365 		return EFI_ABORTED;
366 	status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);
367 	up(&efi_runtime_lock);
368 	return status;
369 }
370 
371 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
372 					  efi_guid_t *vendor,
373 					  u32 *attr,
374 					  unsigned long *data_size,
375 					  void *data)
376 {
377 	efi_status_t status;
378 
379 	if (down_interruptible(&efi_runtime_lock))
380 		return EFI_ABORTED;
381 	status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
382 				data);
383 	up(&efi_runtime_lock);
384 	return status;
385 }
386 
387 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
388 					       efi_char16_t *name,
389 					       efi_guid_t *vendor)
390 {
391 	efi_status_t status;
392 
393 	if (down_interruptible(&efi_runtime_lock))
394 		return EFI_ABORTED;
395 	status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);
396 	up(&efi_runtime_lock);
397 	return status;
398 }
399 
400 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
401 					  efi_guid_t *vendor,
402 					  u32 attr,
403 					  unsigned long data_size,
404 					  void *data)
405 {
406 	efi_status_t status;
407 
408 	if (down_interruptible(&efi_runtime_lock))
409 		return EFI_ABORTED;
410 	status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,
411 				data);
412 	up(&efi_runtime_lock);
413 	return status;
414 }
415 
416 static efi_status_t
417 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
418 				  u32 attr, unsigned long data_size,
419 				  void *data)
420 {
421 	efi_status_t status;
422 
423 	if (down_trylock(&efi_runtime_lock))
424 		return EFI_NOT_READY;
425 
426 	status = efi_call_virt(set_variable, name, vendor, attr, data_size,
427 			       data);
428 	up(&efi_runtime_lock);
429 	return status;
430 }
431 
432 
433 static efi_status_t virt_efi_query_variable_info(u32 attr,
434 						 u64 *storage_space,
435 						 u64 *remaining_space,
436 						 u64 *max_variable_size)
437 {
438 	efi_status_t status;
439 
440 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
441 		return EFI_UNSUPPORTED;
442 
443 	if (down_interruptible(&efi_runtime_lock))
444 		return EFI_ABORTED;
445 	status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,
446 				remaining_space, max_variable_size);
447 	up(&efi_runtime_lock);
448 	return status;
449 }
450 
451 static efi_status_t
452 virt_efi_query_variable_info_nonblocking(u32 attr,
453 					 u64 *storage_space,
454 					 u64 *remaining_space,
455 					 u64 *max_variable_size)
456 {
457 	efi_status_t status;
458 
459 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
460 		return EFI_UNSUPPORTED;
461 
462 	if (down_trylock(&efi_runtime_lock))
463 		return EFI_NOT_READY;
464 
465 	status = efi_call_virt(query_variable_info, attr, storage_space,
466 			       remaining_space, max_variable_size);
467 	up(&efi_runtime_lock);
468 	return status;
469 }
470 
471 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
472 {
473 	efi_status_t status;
474 
475 	if (down_interruptible(&efi_runtime_lock))
476 		return EFI_ABORTED;
477 	status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);
478 	up(&efi_runtime_lock);
479 	return status;
480 }
481 
482 static void virt_efi_reset_system(int reset_type,
483 				  efi_status_t status,
484 				  unsigned long data_size,
485 				  efi_char16_t *data)
486 {
487 	if (down_trylock(&efi_runtime_lock)) {
488 		pr_warn("failed to invoke the reset_system() runtime service:\n"
489 			"could not get exclusive access to the firmware\n");
490 		return;
491 	}
492 
493 	arch_efi_call_virt_setup();
494 	efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
495 	arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,
496 			   data_size, data);
497 	arch_efi_call_virt_teardown();
498 
499 	up(&efi_runtime_lock);
500 }
501 
502 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
503 					    unsigned long count,
504 					    unsigned long sg_list)
505 {
506 	efi_status_t status;
507 
508 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
509 		return EFI_UNSUPPORTED;
510 
511 	if (down_interruptible(&efi_runtime_lock))
512 		return EFI_ABORTED;
513 	status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);
514 	up(&efi_runtime_lock);
515 	return status;
516 }
517 
518 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
519 						unsigned long count,
520 						u64 *max_size,
521 						int *reset_type)
522 {
523 	efi_status_t status;
524 
525 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
526 		return EFI_UNSUPPORTED;
527 
528 	if (down_interruptible(&efi_runtime_lock))
529 		return EFI_ABORTED;
530 	status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,
531 				max_size, reset_type);
532 	up(&efi_runtime_lock);
533 	return status;
534 }
535 
536 void efi_native_runtime_setup(void)
537 {
538 	efi.get_time = virt_efi_get_time;
539 	efi.set_time = virt_efi_set_time;
540 	efi.get_wakeup_time = virt_efi_get_wakeup_time;
541 	efi.set_wakeup_time = virt_efi_set_wakeup_time;
542 	efi.get_variable = virt_efi_get_variable;
543 	efi.get_next_variable = virt_efi_get_next_variable;
544 	efi.set_variable = virt_efi_set_variable;
545 	efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
546 	efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
547 	efi.reset_system = virt_efi_reset_system;
548 	efi.query_variable_info = virt_efi_query_variable_info;
549 	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
550 	efi.update_capsule = virt_efi_update_capsule;
551 	efi.query_capsule_caps = virt_efi_query_capsule_caps;
552 }
553