runtime-wrappers.c 13.8 KB
Newer Older
1 2 3
/*
 * runtime-wrappers.c - Runtime Services function call wrappers
 *
4 5 6 7 8 9 10 11 12
 * Implementation summary:
 * -----------------------
 * 1. When user/kernel thread requests to execute efi_runtime_service(),
 * enqueue work to efi_rts_wq.
 * 2. Caller thread waits for completion until the work is finished
 * because it's dependent on the return status and execution of
 * efi_runtime_service().
 * For instance, get_variable() and get_next_variable().
 *
13 14 15 16 17 18 19 20 21 22 23 24 25
 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
 *
 * Split off from arch/x86/platform/efi/efi.c
 *
 * Copyright (C) 1999 VA Linux Systems
 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
 * Copyright (C) 1999-2002 Hewlett-Packard Co.
 * Copyright (C) 2005-2008 Intel Co.
 * Copyright (C) 2013 SuSE Labs
 *
 * This file is released under the GPLv2.
 */

26 27
#define pr_fmt(fmt)	"efi: " fmt

28
#include <linux/bug.h>
29
#include <linux/efi.h>
30
#include <linux/irqflags.h>
31
#include <linux/mutex.h>
32
#include <linux/semaphore.h>
33
#include <linux/stringify.h>
34 35 36
#include <linux/workqueue.h>
#include <linux/completion.h>

37 38
#include <asm/efi.h>

39 40 41 42 43 44 45 46 47
/*
 * Wrap around the new efi_call_virt_generic() macros so that the
 * code doesn't get too cluttered:
 */
#define efi_call_virt(f, args...)   \
	efi_call_virt_pointer(efi.systab->runtime, f, args)
#define __efi_call_virt(f, args...) \
	__efi_call_virt_pointer(efi.systab->runtime, f, args)

48
struct efi_runtime_work efi_rts_work;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/*
 * efi_queue_work:	Queue efi_runtime_service() and wait until it's done
 * @rts:		efi_runtime_service() function identifier
 * @rts_arg<1-5>:	efi_runtime_service() function arguments
 *
 * Accesses to efi_runtime_services() are serialized by a binary
 * semaphore (efi_runtime_lock) and caller waits until the work is
 * finished, hence _only_ one work is queued at a time and the caller
 * thread waits for completion.
 */
#define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5)		\
({									\
	efi_rts_work.status = EFI_ABORTED;				\
									\
64 65 66 67 68
	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {			\
		pr_warn_once("EFI Runtime Services are disabled!\n");	\
		goto exit;						\
	}								\
									\
69
	init_completion(&efi_rts_work.efi_rts_comp);			\
70
	INIT_WORK(&efi_rts_work.work, efi_call_rts);			\
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	efi_rts_work.arg1 = _arg1;					\
	efi_rts_work.arg2 = _arg2;					\
	efi_rts_work.arg3 = _arg3;					\
	efi_rts_work.arg4 = _arg4;					\
	efi_rts_work.arg5 = _arg5;					\
	efi_rts_work.efi_rts_id = _rts;					\
									\
	/*								\
	 * queue_work() returns 0 if work was already on queue,         \
	 * _ideally_ this should never happen.                          \
	 */								\
	if (queue_work(efi_rts_wq, &efi_rts_work.work))			\
		wait_for_completion(&efi_rts_work.efi_rts_comp);	\
	else								\
		pr_err("Failed to queue work to efi_rts_wq.\n");	\
									\
87 88
exit:									\
	efi_rts_work.efi_rts_id = NONE;					\
89 90 91
	efi_rts_work.status;						\
})

92
void efi_call_virt_check_flags(unsigned long flags, const char *call)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	unsigned long cur_flags, mismatch;

	local_save_flags(cur_flags);

	mismatch = flags ^ cur_flags;
	if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
		return;

	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
	pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
			   flags, cur_flags, call);
	local_irq_restore(flags);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/*
 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
 * reentrant, and there are particular combinations of calls that need to be
 * serialized. (source: UEFI Specification v2.4A)
 *
 * Table 31. Rules for Reentry Into Runtime Services
 * +------------------------------------+-------------------------------+
 * | If previous call is busy in	| Forbidden to call		|
 * +------------------------------------+-------------------------------+
 * | Any				| SetVirtualAddressMap()	|
 * +------------------------------------+-------------------------------+
 * | ConvertPointer()			| ConvertPointer()		|
 * +------------------------------------+-------------------------------+
 * | SetVariable()			| ResetSystem()			|
 * | UpdateCapsule()			|				|
 * | SetTime()				|				|
 * | SetWakeupTime()			|				|
 * | GetNextHighMonotonicCount()	|				|
 * +------------------------------------+-------------------------------+
 * | GetVariable()			| GetVariable()			|
 * | GetNextVariableName()		| GetNextVariableName()		|
 * | SetVariable()			| SetVariable()			|
 * | QueryVariableInfo()		| QueryVariableInfo()		|
 * | UpdateCapsule()			| UpdateCapsule()		|
 * | QueryCapsuleCapabilities()		| QueryCapsuleCapabilities()	|
 * | GetNextHighMonotonicCount()	| GetNextHighMonotonicCount()	|
 * +------------------------------------+-------------------------------+
 * | GetTime()				| GetTime()			|
 * | SetTime()				| SetTime()			|
 * | GetWakeupTime()			| GetWakeupTime()		|
 * | SetWakeupTime()			| SetWakeupTime()		|
 * +------------------------------------+-------------------------------+
 *
 * Due to the fact that the EFI pstore may write to the variable store in
142
 * interrupt context, we need to use a lock for at least the groups that
143 144
 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
 * none of the remaining functions are actually ever called at runtime.
145
 * So let's just use a single lock to serialize all Runtime Services calls.
146
 */
147
static DEFINE_SEMAPHORE(efi_runtime_lock);
148

149 150 151 152 153 154 155
/*
 * Expose the EFI runtime lock to the UV platform
 */
#ifdef CONFIG_X86_UV
extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
#endif

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/*
 * Calls the appropriate efi_runtime_service() with the appropriate
 * arguments.
 *
 * Semantics followed by efi_call_rts() to understand efi_runtime_work:
 * 1. If argument was a pointer, recast it from void pointer to original
 * pointer type.
 * 2. If argument was a value, recast it from void pointer to original
 * pointer type and dereference it.
 */
static void efi_call_rts(struct work_struct *work)
{
	void *arg1, *arg2, *arg3, *arg4, *arg5;
	efi_status_t status = EFI_NOT_FOUND;

171 172 173 174 175
	arg1 = efi_rts_work.arg1;
	arg2 = efi_rts_work.arg2;
	arg3 = efi_rts_work.arg3;
	arg4 = efi_rts_work.arg4;
	arg5 = efi_rts_work.arg5;
176

177
	switch (efi_rts_work.efi_rts_id) {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	case GET_TIME:
		status = efi_call_virt(get_time, (efi_time_t *)arg1,
				       (efi_time_cap_t *)arg2);
		break;
	case SET_TIME:
		status = efi_call_virt(set_time, (efi_time_t *)arg1);
		break;
	case GET_WAKEUP_TIME:
		status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
				       (efi_bool_t *)arg2, (efi_time_t *)arg3);
		break;
	case SET_WAKEUP_TIME:
		status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
				       (efi_time_t *)arg2);
		break;
	case GET_VARIABLE:
		status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
				       (efi_guid_t *)arg2, (u32 *)arg3,
				       (unsigned long *)arg4, (void *)arg5);
		break;
	case GET_NEXT_VARIABLE:
		status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
				       (efi_char16_t *)arg2,
				       (efi_guid_t *)arg3);
		break;
	case SET_VARIABLE:
		status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
				       (efi_guid_t *)arg2, *(u32 *)arg3,
				       *(unsigned long *)arg4, (void *)arg5);
		break;
	case QUERY_VARIABLE_INFO:
		status = efi_call_virt(query_variable_info, *(u32 *)arg1,
				       (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
		break;
	case GET_NEXT_HIGH_MONO_COUNT:
		status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
		break;
	case UPDATE_CAPSULE:
		status = efi_call_virt(update_capsule,
				       (efi_capsule_header_t **)arg1,
				       *(unsigned long *)arg2,
				       *(unsigned long *)arg3);
		break;
	case QUERY_CAPSULE_CAPS:
		status = efi_call_virt(query_capsule_caps,
				       (efi_capsule_header_t **)arg1,
				       *(unsigned long *)arg2, (u64 *)arg3,
				       (int *)arg4);
		break;
	default:
		/*
		 * Ideally, we should never reach here because a caller of this
		 * function should have put the right efi_runtime_service()
		 * function identifier into efi_rts_work->efi_rts_id
		 */
		pr_err("Requested executing invalid EFI Runtime Service.\n");
	}
235 236
	efi_rts_work.status = status;
	complete(&efi_rts_work.efi_rts_comp);
237 238
}

239 240 241 242
static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
{
	efi_status_t status;

243 244
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
245
	status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL);
246
	up(&efi_runtime_lock);
247 248 249 250 251 252 253
	return status;
}

static efi_status_t virt_efi_set_time(efi_time_t *tm)
{
	efi_status_t status;

254 255
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
256
	status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL);
257
	up(&efi_runtime_lock);
258 259 260 261 262 263 264 265 266
	return status;
}

static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
					     efi_bool_t *pending,
					     efi_time_t *tm)
{
	efi_status_t status;

267 268
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
269 270
	status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL,
				NULL);
271
	up(&efi_runtime_lock);
272 273 274 275 276 277 278
	return status;
}

static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
{
	efi_status_t status;

279 280
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
281 282
	status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
				NULL);
283
	up(&efi_runtime_lock);
284 285 286 287 288 289 290 291 292
	return status;
}

static efi_status_t virt_efi_get_variable(efi_char16_t *name,
					  efi_guid_t *vendor,
					  u32 *attr,
					  unsigned long *data_size,
					  void *data)
{
293 294
	efi_status_t status;

295 296
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
297 298
	status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
				data);
299
	up(&efi_runtime_lock);
300
	return status;
301 302 303 304 305 306
}

static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
					       efi_char16_t *name,
					       efi_guid_t *vendor)
{
307 308
	efi_status_t status;

309 310
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
311 312
	status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor,
				NULL, NULL);
313
	up(&efi_runtime_lock);
314
	return status;
315 316 317 318 319 320 321 322
}

static efi_status_t virt_efi_set_variable(efi_char16_t *name,
					  efi_guid_t *vendor,
					  u32 attr,
					  unsigned long data_size,
					  void *data)
{
323 324
	efi_status_t status;

325 326
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
327 328
	status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size,
				data);
329
	up(&efi_runtime_lock);
330
	return status;
331 332
}

333 334 335 336 337 338 339
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)
{
	efi_status_t status;

340
	if (down_trylock(&efi_runtime_lock))
341 342 343 344
		return EFI_NOT_READY;

	status = efi_call_virt(set_variable, name, vendor, attr, data_size,
			       data);
345
	up(&efi_runtime_lock);
346 347 348 349
	return status;
}


350 351 352 353 354
static efi_status_t virt_efi_query_variable_info(u32 attr,
						 u64 *storage_space,
						 u64 *remaining_space,
						 u64 *max_variable_size)
{
355 356
	efi_status_t status;

357 358 359
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

360 361
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
362 363
	status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space,
				remaining_space, max_variable_size, NULL);
364
	up(&efi_runtime_lock);
365
	return status;
366 367
}

368 369 370 371 372 373 374 375 376 377 378
static efi_status_t
virt_efi_query_variable_info_nonblocking(u32 attr,
					 u64 *storage_space,
					 u64 *remaining_space,
					 u64 *max_variable_size)
{
	efi_status_t status;

	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

379
	if (down_trylock(&efi_runtime_lock))
380 381 382 383
		return EFI_NOT_READY;

	status = efi_call_virt(query_variable_info, attr, storage_space,
			       remaining_space, max_variable_size);
384
	up(&efi_runtime_lock);
385 386 387
	return status;
}

388 389
static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
{
390 391
	efi_status_t status;

392 393
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
394 395
	status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
				NULL, NULL);
396
	up(&efi_runtime_lock);
397
	return status;
398 399 400 401 402 403 404
}

static void virt_efi_reset_system(int reset_type,
				  efi_status_t status,
				  unsigned long data_size,
				  efi_char16_t *data)
{
405 406 407 408 409
	if (down_interruptible(&efi_runtime_lock)) {
		pr_warn("failed to invoke the reset_system() runtime service:\n"
			"could not get exclusive access to the firmware\n");
		return;
	}
410
	efi_rts_work.efi_rts_id = RESET_SYSTEM;
411
	__efi_call_virt(reset_system, reset_type, status, data_size, data);
412
	up(&efi_runtime_lock);
413 414 415 416 417 418
}

static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
					    unsigned long count,
					    unsigned long sg_list)
{
419 420
	efi_status_t status;

421 422 423
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

424 425
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
426 427
	status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list,
				NULL, NULL);
428
	up(&efi_runtime_lock);
429
	return status;
430 431 432 433 434 435 436
}

static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
						unsigned long count,
						u64 *max_size,
						int *reset_type)
{
437 438
	efi_status_t status;

439 440 441
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

442 443
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
444 445
	status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count,
				max_size, reset_type, NULL);
446
	up(&efi_runtime_lock);
447
	return status;
448 449 450 451 452 453 454 455 456 457 458
}

void efi_native_runtime_setup(void)
{
	efi.get_time = virt_efi_get_time;
	efi.set_time = virt_efi_set_time;
	efi.get_wakeup_time = virt_efi_get_wakeup_time;
	efi.set_wakeup_time = virt_efi_set_wakeup_time;
	efi.get_variable = virt_efi_get_variable;
	efi.get_next_variable = virt_efi_get_next_variable;
	efi.set_variable = virt_efi_set_variable;
459
	efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
460 461 462
	efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
	efi.reset_system = virt_efi_reset_system;
	efi.query_variable_info = virt_efi_query_variable_info;
463
	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
464 465 466
	efi.update_capsule = virt_efi_update_capsule;
	efi.query_capsule_caps = virt_efi_query_capsule_caps;
}