runtime-wrappers.c 8.8 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
#include <linux/bug.h>
18
#include <linux/efi.h>
19 20
#include <linux/mutex.h>
#include <linux/spinlock.h>
21 22
#include <asm/efi.h>

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * Arch code can implement the following three template macros, avoiding
 * reptition for the void/non-void return cases of {__,}efi_call_virt:
 *
 *  * arch_efi_call_virt_setup
 *
 *    Sets up the environment for the call (e.g. switching page tables,
 *    allowing kernel-mode use of floating point, if required).
 *
 *  * arch_efi_call_virt
 *
 *    Performs the call. The last expression in the macro must be the call
 *    itself, allowing the logic to be shared by the void and non-void
 *    cases.
 *
 *  * arch_efi_call_virt_teardown
 *
 *    Restores the usual kernel environment once the call has returned.
 */

#define efi_call_virt(f, args...)					\
({									\
	efi_status_t __s;						\
	arch_efi_call_virt_setup();					\
	__s = arch_efi_call_virt(f, args);				\
	arch_efi_call_virt_teardown();					\
	__s;								\
})

#define __efi_call_virt(f, args...)					\
({									\
	arch_efi_call_virt_setup();					\
	arch_efi_call_virt(f, args);					\
	arch_efi_call_virt_teardown();					\
})

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/*
 * 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
 * interrupt context, we need to use a spinlock for at least the groups that
 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
 * none of the remaining functions are actually ever called at runtime.
 * So let's just use a single spinlock to serialize all Runtime Services calls.
 */
static DEFINE_SPINLOCK(efi_runtime_lock);

100 101 102 103
static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
{
	efi_status_t status;

104
	spin_lock(&efi_runtime_lock);
105
	status = efi_call_virt(get_time, tm, tc);
106
	spin_unlock(&efi_runtime_lock);
107 108 109 110 111 112 113
	return status;
}

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

114
	spin_lock(&efi_runtime_lock);
115
	status = efi_call_virt(set_time, tm);
116
	spin_unlock(&efi_runtime_lock);
117 118 119 120 121 122 123 124 125
	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;

126
	spin_lock(&efi_runtime_lock);
127
	status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
128
	spin_unlock(&efi_runtime_lock);
129 130 131 132 133 134 135
	return status;
}

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

136
	spin_lock(&efi_runtime_lock);
137
	status = efi_call_virt(set_wakeup_time, enabled, tm);
138
	spin_unlock(&efi_runtime_lock);
139 140 141 142 143 144 145 146 147
	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)
{
148 149
	efi_status_t status;

150
	spin_lock(&efi_runtime_lock);
151 152
	status = efi_call_virt(get_variable, name, vendor, attr, data_size,
			       data);
153
	spin_unlock(&efi_runtime_lock);
154
	return status;
155 156 157 158 159 160
}

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

163
	spin_lock(&efi_runtime_lock);
164
	status = efi_call_virt(get_next_variable, name_size, name, vendor);
165
	spin_unlock(&efi_runtime_lock);
166
	return status;
167 168 169 170 171 172 173 174
}

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

177
	spin_lock(&efi_runtime_lock);
178 179
	status = efi_call_virt(set_variable, name, vendor, attr, data_size,
			       data);
180
	spin_unlock(&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 (!spin_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
	spin_unlock(&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
	spin_lock(&efi_runtime_lock);
212 213
	status = efi_call_virt(query_variable_info, attr, storage_space,
			       remaining_space, max_variable_size);
214
	spin_unlock(&efi_runtime_lock);
215
	return status;
216 217
}

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

229
	if (!spin_trylock(&efi_runtime_lock))
230 231 232 233
		return EFI_NOT_READY;

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

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

242
	spin_lock(&efi_runtime_lock);
243
	status = efi_call_virt(get_next_high_mono_count, count);
244
	spin_unlock(&efi_runtime_lock);
245
	return status;
246 247 248 249 250 251 252
}

static void virt_efi_reset_system(int reset_type,
				  efi_status_t status,
				  unsigned long data_size,
				  efi_char16_t *data)
{
253
	spin_lock(&efi_runtime_lock);
254
	__efi_call_virt(reset_system, reset_type, status, data_size, data);
255
	spin_unlock(&efi_runtime_lock);
256 257 258 259 260 261
}

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

264 265 266
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

267
	spin_lock(&efi_runtime_lock);
268
	status = efi_call_virt(update_capsule, capsules, count, sg_list);
269
	spin_unlock(&efi_runtime_lock);
270
	return status;
271 272 273 274 275 276 277
}

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

280 281 282
	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

283
	spin_lock(&efi_runtime_lock);
284 285
	status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
			       reset_type);
286
	spin_unlock(&efi_runtime_lock);
287
	return status;
288 289 290 291 292 293 294 295 296 297 298
}

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;
299
	efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
300 301 302
	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;
303
	efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
304 305 306
	efi.update_capsule = virt_efi_update_capsule;
	efi.query_capsule_caps = virt_efi_query_capsule_caps;
}
新手
引导
客服 返回
顶部