runtime-wrappers.c 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * runtime-wrappers.c - Runtime Services function call wrappers
 *
 * 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.
 */

17 18
#define pr_fmt(fmt)	"efi: " fmt

19
#include <linux/bug.h>
20
#include <linux/efi.h>
21
#include <linux/irqflags.h>
22
#include <linux/mutex.h>
23
#include <linux/semaphore.h>
24
#include <linux/stringify.h>
25 26
#include <asm/efi.h>

27 28 29 30 31 32 33 34 35 36
/*
 * 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)

void efi_call_virt_check_flags(unsigned long flags, const char *call)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
	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);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * 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
86
 * interrupt context, we need to use a lock for at least the groups that
87 88
 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
 * none of the remaining functions are actually ever called at runtime.
89
 * So let's just use a single lock to serialize all Runtime Services calls.
90
 */
91
static DEFINE_SEMAPHORE(efi_runtime_lock);
92

93 94 95 96
static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
{
	efi_status_t status;

97 98
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
99
	status = efi_call_virt(get_time, tm, tc);
100
	up(&efi_runtime_lock);
101 102 103 104 105 106 107
	return status;
}

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

108 109
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
110
	status = efi_call_virt(set_time, tm);
111
	up(&efi_runtime_lock);
112 113 114 115 116 117 118 119 120
	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;

121 122
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
123
	status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
124
	up(&efi_runtime_lock);
125 126 127 128 129 130 131
	return status;
}

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

132 133
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
134
	status = efi_call_virt(set_wakeup_time, enabled, tm);
135
	up(&efi_runtime_lock);
136 137 138 139 140 141 142 143 144
	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)
{
145 146
	efi_status_t status;

147 148
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
149 150
	status = efi_call_virt(get_variable, name, vendor, attr, data_size,
			       data);
151
	up(&efi_runtime_lock);
152
	return status;
153 154 155 156 157 158
}

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

161 162
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
163
	status = efi_call_virt(get_next_variable, name_size, name, vendor);
164
	up(&efi_runtime_lock);
165
	return status;
166 167 168 169 170 171 172 173
}

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

176 177
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
178 179
	status = efi_call_virt(set_variable, name, vendor, attr, data_size,
			       data);
180
	up(&efi_runtime_lock);
181
	return status;
182 183
}

184 185 186 187 188 189 190
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;

191
	if (down_trylock(&efi_runtime_lock))
192 193 194 195
		return EFI_NOT_READY;

	status = efi_call_virt(set_variable, name, vendor, attr, data_size,
			       data);
196
	up(&efi_runtime_lock);
197 198 199 200
	return status;
}


201 202 203 204 205
static efi_status_t virt_efi_query_variable_info(u32 attr,
						 u64 *storage_space,
						 u64 *remaining_space,
						 u64 *max_variable_size)
{
206 207
	efi_status_t status;

208 209 210
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

211 212
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
213 214
	status = efi_call_virt(query_variable_info, attr, storage_space,
			       remaining_space, max_variable_size);
215
	up(&efi_runtime_lock);
216
	return status;
217 218
}

219 220 221 222 223 224 225 226 227 228 229
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;

230
	if (down_trylock(&efi_runtime_lock))
231 232 233 234
		return EFI_NOT_READY;

	status = efi_call_virt(query_variable_info, attr, storage_space,
			       remaining_space, max_variable_size);
235
	up(&efi_runtime_lock);
236 237 238
	return status;
}

239 240
static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
{
241 242
	efi_status_t status;

243 244
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
245
	status = efi_call_virt(get_next_high_mono_count, count);
246
	up(&efi_runtime_lock);
247
	return status;
248 249 250 251 252 253 254
}

static void virt_efi_reset_system(int reset_type,
				  efi_status_t status,
				  unsigned long data_size,
				  efi_char16_t *data)
{
255 256 257 258 259
	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;
	}
260
	__efi_call_virt(reset_system, reset_type, status, data_size, data);
261
	up(&efi_runtime_lock);
262 263 264 265 266 267
}

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

270 271 272
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

273 274
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
275
	status = efi_call_virt(update_capsule, capsules, count, sg_list);
276
	up(&efi_runtime_lock);
277
	return status;
278 279 280 281 282 283 284
}

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

287 288 289
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

290 291
	if (down_interruptible(&efi_runtime_lock))
		return EFI_ABORTED;
292 293
	status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
			       reset_type);
294
	up(&efi_runtime_lock);
295
	return status;
296 297 298 299 300 301 302 303 304 305 306
}

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;
307
	efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
308 309 310
	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;
311
	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
312 313 314
	efi.update_capsule = virt_efi_update_capsule;
	efi.query_capsule_caps = virt_efi_query_capsule_caps;
}
新手
引导
客服 返回
顶部