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 
47 struct 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 									\
86 	WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);		\
87 exit:									\
88 	efi_rts_work.efi_rts_id = EFI_NONE;				\
89 	efi_rts_work.status;						\
90 })
91 
92 #ifndef arch_efi_save_flags
93 #define arch_efi_save_flags(state_flags)	local_save_flags(state_flags)
94 #define arch_efi_restore_flags(state_flags)	local_irq_restore(state_flags)
95 #endif
96 
97 unsigned long efi_call_virt_save_flags(void)
98 {
99 	unsigned long flags;
100 
101 	arch_efi_save_flags(flags);
102 	return flags;
103 }
104 
105 void efi_call_virt_check_flags(unsigned long flags, const char *call)
106 {
107 	unsigned long cur_flags, mismatch;
108 
109 	cur_flags = efi_call_virt_save_flags();
110 
111 	mismatch = flags ^ cur_flags;
112 	if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
113 		return;
114 
115 	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
116 	pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
117 			   flags, cur_flags, call);
118 	arch_efi_restore_flags(flags);
119 }
120 
121 /*
122  * According to section 7.1 of the UEFI spec, Runtime Services are not fully
123  * reentrant, and there are particular combinations of calls that need to be
124  * serialized. (source: UEFI Specification v2.4A)
125  *
126  * Table 31. Rules for Reentry Into Runtime Services
127  * +------------------------------------+-------------------------------+
128  * | If previous call is busy in	| Forbidden to call		|
129  * +------------------------------------+-------------------------------+
130  * | Any				| SetVirtualAddressMap()	|
131  * +------------------------------------+-------------------------------+
132  * | ConvertPointer()			| ConvertPointer()		|
133  * +------------------------------------+-------------------------------+
134  * | SetVariable()			| ResetSystem()			|
135  * | UpdateCapsule()			|				|
136  * | SetTime()				|				|
137  * | SetWakeupTime()			|				|
138  * | GetNextHighMonotonicCount()	|				|
139  * +------------------------------------+-------------------------------+
140  * | GetVariable()			| GetVariable()			|
141  * | GetNextVariableName()		| GetNextVariableName()		|
142  * | SetVariable()			| SetVariable()			|
143  * | QueryVariableInfo()		| QueryVariableInfo()		|
144  * | UpdateCapsule()			| UpdateCapsule()		|
145  * | QueryCapsuleCapabilities()		| QueryCapsuleCapabilities()	|
146  * | GetNextHighMonotonicCount()	| GetNextHighMonotonicCount()	|
147  * +------------------------------------+-------------------------------+
148  * | GetTime()				| GetTime()			|
149  * | SetTime()				| SetTime()			|
150  * | GetWakeupTime()			| GetWakeupTime()		|
151  * | SetWakeupTime()			| SetWakeupTime()		|
152  * +------------------------------------+-------------------------------+
153  *
154  * Due to the fact that the EFI pstore may write to the variable store in
155  * interrupt context, we need to use a lock for at least the groups that
156  * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
157  * none of the remaining functions are actually ever called at runtime.
158  * So let's just use a single lock to serialize all Runtime Services calls.
159  */
160 static DEFINE_SEMAPHORE(efi_runtime_lock);
161 
162 /*
163  * Expose the EFI runtime lock to the UV platform
164  */
165 #ifdef CONFIG_X86_UV
166 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
167 #endif
168 
169 /*
170  * Calls the appropriate efi_runtime_service() with the appropriate
171  * arguments.
172  *
173  * Semantics followed by efi_call_rts() to understand efi_runtime_work:
174  * 1. If argument was a pointer, recast it from void pointer to original
175  * pointer type.
176  * 2. If argument was a value, recast it from void pointer to original
177  * pointer type and dereference it.
178  */
179 static void efi_call_rts(struct work_struct *work)
180 {
181 	void *arg1, *arg2, *arg3, *arg4, *arg5;
182 	efi_status_t status = EFI_NOT_FOUND;
183 
184 	arg1 = efi_rts_work.arg1;
185 	arg2 = efi_rts_work.arg2;
186 	arg3 = efi_rts_work.arg3;
187 	arg4 = efi_rts_work.arg4;
188 	arg5 = efi_rts_work.arg5;
189 
190 	switch (efi_rts_work.efi_rts_id) {
191 	case EFI_GET_TIME:
192 		status = efi_call_virt(get_time, (efi_time_t *)arg1,
193 				       (efi_time_cap_t *)arg2);
194 		break;
195 	case EFI_SET_TIME:
196 		status = efi_call_virt(set_time, (efi_time_t *)arg1);
197 		break;
198 	case EFI_GET_WAKEUP_TIME:
199 		status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
200 				       (efi_bool_t *)arg2, (efi_time_t *)arg3);
201 		break;
202 	case EFI_SET_WAKEUP_TIME:
203 		status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
204 				       (efi_time_t *)arg2);
205 		break;
206 	case EFI_GET_VARIABLE:
207 		status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
208 				       (efi_guid_t *)arg2, (u32 *)arg3,
209 				       (unsigned long *)arg4, (void *)arg5);
210 		break;
211 	case EFI_GET_NEXT_VARIABLE:
212 		status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
213 				       (efi_char16_t *)arg2,
214 				       (efi_guid_t *)arg3);
215 		break;
216 	case EFI_SET_VARIABLE:
217 		status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
218 				       (efi_guid_t *)arg2, *(u32 *)arg3,
219 				       *(unsigned long *)arg4, (void *)arg5);
220 		break;
221 	case EFI_QUERY_VARIABLE_INFO:
222 		status = efi_call_virt(query_variable_info, *(u32 *)arg1,
223 				       (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
224 		break;
225 	case EFI_GET_NEXT_HIGH_MONO_COUNT:
226 		status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
227 		break;
228 	case EFI_UPDATE_CAPSULE:
229 		status = efi_call_virt(update_capsule,
230 				       (efi_capsule_header_t **)arg1,
231 				       *(unsigned long *)arg2,
232 				       *(unsigned long *)arg3);
233 		break;
234 	case EFI_QUERY_CAPSULE_CAPS:
235 		status = efi_call_virt(query_capsule_caps,
236 				       (efi_capsule_header_t **)arg1,
237 				       *(unsigned long *)arg2, (u64 *)arg3,
238 				       (int *)arg4);
239 		break;
240 	default:
241 		/*
242 		 * Ideally, we should never reach here because a caller of this
243 		 * function should have put the right efi_runtime_service()
244 		 * function identifier into efi_rts_work->efi_rts_id
245 		 */
246 		pr_err("Requested executing invalid EFI Runtime Service.\n");
247 	}
248 	efi_rts_work.status = status;
249 	complete(&efi_rts_work.efi_rts_comp);
250 }
251 
252 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
253 {
254 	efi_status_t status;
255 
256 	if (down_interruptible(&efi_runtime_lock))
257 		return EFI_ABORTED;
258 	status = efi_queue_work(EFI_GET_TIME, tm, tc, NULL, NULL, NULL);
259 	up(&efi_runtime_lock);
260 	return status;
261 }
262 
263 static efi_status_t virt_efi_set_time(efi_time_t *tm)
264 {
265 	efi_status_t status;
266 
267 	if (down_interruptible(&efi_runtime_lock))
268 		return EFI_ABORTED;
269 	status = efi_queue_work(EFI_SET_TIME, tm, NULL, NULL, NULL, NULL);
270 	up(&efi_runtime_lock);
271 	return status;
272 }
273 
274 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
275 					     efi_bool_t *pending,
276 					     efi_time_t *tm)
277 {
278 	efi_status_t status;
279 
280 	if (down_interruptible(&efi_runtime_lock))
281 		return EFI_ABORTED;
282 	status = efi_queue_work(EFI_GET_WAKEUP_TIME, enabled, pending, tm, NULL,
283 				NULL);
284 	up(&efi_runtime_lock);
285 	return status;
286 }
287 
288 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
289 {
290 	efi_status_t status;
291 
292 	if (down_interruptible(&efi_runtime_lock))
293 		return EFI_ABORTED;
294 	status = efi_queue_work(EFI_SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
295 				NULL);
296 	up(&efi_runtime_lock);
297 	return status;
298 }
299 
300 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
301 					  efi_guid_t *vendor,
302 					  u32 *attr,
303 					  unsigned long *data_size,
304 					  void *data)
305 {
306 	efi_status_t status;
307 
308 	if (down_interruptible(&efi_runtime_lock))
309 		return EFI_ABORTED;
310 	status = efi_queue_work(EFI_GET_VARIABLE, name, vendor, attr, data_size,
311 				data);
312 	up(&efi_runtime_lock);
313 	return status;
314 }
315 
316 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
317 					       efi_char16_t *name,
318 					       efi_guid_t *vendor)
319 {
320 	efi_status_t status;
321 
322 	if (down_interruptible(&efi_runtime_lock))
323 		return EFI_ABORTED;
324 	status = efi_queue_work(EFI_GET_NEXT_VARIABLE, name_size, name, vendor,
325 				NULL, NULL);
326 	up(&efi_runtime_lock);
327 	return status;
328 }
329 
330 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
331 					  efi_guid_t *vendor,
332 					  u32 attr,
333 					  unsigned long data_size,
334 					  void *data)
335 {
336 	efi_status_t status;
337 
338 	if (down_interruptible(&efi_runtime_lock))
339 		return EFI_ABORTED;
340 	status = efi_queue_work(EFI_SET_VARIABLE, name, vendor, &attr, &data_size,
341 				data);
342 	up(&efi_runtime_lock);
343 	return status;
344 }
345 
346 static efi_status_t
347 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
348 				  u32 attr, unsigned long data_size,
349 				  void *data)
350 {
351 	efi_status_t status;
352 
353 	if (down_trylock(&efi_runtime_lock))
354 		return EFI_NOT_READY;
355 
356 	status = efi_call_virt(set_variable, name, vendor, attr, data_size,
357 			       data);
358 	up(&efi_runtime_lock);
359 	return status;
360 }
361 
362 
363 static efi_status_t virt_efi_query_variable_info(u32 attr,
364 						 u64 *storage_space,
365 						 u64 *remaining_space,
366 						 u64 *max_variable_size)
367 {
368 	efi_status_t status;
369 
370 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
371 		return EFI_UNSUPPORTED;
372 
373 	if (down_interruptible(&efi_runtime_lock))
374 		return EFI_ABORTED;
375 	status = efi_queue_work(EFI_QUERY_VARIABLE_INFO, &attr, storage_space,
376 				remaining_space, max_variable_size, NULL);
377 	up(&efi_runtime_lock);
378 	return status;
379 }
380 
381 static efi_status_t
382 virt_efi_query_variable_info_nonblocking(u32 attr,
383 					 u64 *storage_space,
384 					 u64 *remaining_space,
385 					 u64 *max_variable_size)
386 {
387 	efi_status_t status;
388 
389 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
390 		return EFI_UNSUPPORTED;
391 
392 	if (down_trylock(&efi_runtime_lock))
393 		return EFI_NOT_READY;
394 
395 	status = efi_call_virt(query_variable_info, attr, storage_space,
396 			       remaining_space, max_variable_size);
397 	up(&efi_runtime_lock);
398 	return status;
399 }
400 
401 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
402 {
403 	efi_status_t status;
404 
405 	if (down_interruptible(&efi_runtime_lock))
406 		return EFI_ABORTED;
407 	status = efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
408 				NULL, NULL);
409 	up(&efi_runtime_lock);
410 	return status;
411 }
412 
413 static void virt_efi_reset_system(int reset_type,
414 				  efi_status_t status,
415 				  unsigned long data_size,
416 				  efi_char16_t *data)
417 {
418 	if (down_trylock(&efi_runtime_lock)) {
419 		pr_warn("failed to invoke the reset_system() runtime service:\n"
420 			"could not get exclusive access to the firmware\n");
421 		return;
422 	}
423 	efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
424 	__efi_call_virt(reset_system, reset_type, status, data_size, data);
425 	up(&efi_runtime_lock);
426 }
427 
428 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
429 					    unsigned long count,
430 					    unsigned long sg_list)
431 {
432 	efi_status_t status;
433 
434 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
435 		return EFI_UNSUPPORTED;
436 
437 	if (down_interruptible(&efi_runtime_lock))
438 		return EFI_ABORTED;
439 	status = efi_queue_work(EFI_UPDATE_CAPSULE, capsules, &count, &sg_list,
440 				NULL, NULL);
441 	up(&efi_runtime_lock);
442 	return status;
443 }
444 
445 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
446 						unsigned long count,
447 						u64 *max_size,
448 						int *reset_type)
449 {
450 	efi_status_t status;
451 
452 	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
453 		return EFI_UNSUPPORTED;
454 
455 	if (down_interruptible(&efi_runtime_lock))
456 		return EFI_ABORTED;
457 	status = efi_queue_work(EFI_QUERY_CAPSULE_CAPS, capsules, &count,
458 				max_size, reset_type, NULL);
459 	up(&efi_runtime_lock);
460 	return status;
461 }
462 
463 void efi_native_runtime_setup(void)
464 {
465 	efi.get_time = virt_efi_get_time;
466 	efi.set_time = virt_efi_set_time;
467 	efi.get_wakeup_time = virt_efi_get_wakeup_time;
468 	efi.set_wakeup_time = virt_efi_set_wakeup_time;
469 	efi.get_variable = virt_efi_get_variable;
470 	efi.get_next_variable = virt_efi_get_next_variable;
471 	efi.set_variable = virt_efi_set_variable;
472 	efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
473 	efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
474 	efi.reset_system = virt_efi_reset_system;
475 	efi.query_variable_info = virt_efi_query_variable_info;
476 	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
477 	efi.update_capsule = virt_efi_update_capsule;
478 	efi.query_capsule_caps = virt_efi_query_capsule_caps;
479 }
480