blob: ae54870b27886b016b0924fb85004b2fc4323700 [file] [log] [blame]
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +02001/*
2 * runtime-wrappers.c - Runtime Services function call wrappers
3 *
4 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
5 *
6 * Split off from arch/x86/platform/efi/efi.c
7 *
8 * Copyright (C) 1999 VA Linux Systems
9 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
10 * Copyright (C) 1999-2002 Hewlett-Packard Co.
11 * Copyright (C) 2005-2008 Intel Co.
12 * Copyright (C) 2013 SuSE Labs
13 *
14 * This file is released under the GPLv2.
15 */
16
Ard Biesheuveldce48e32016-07-15 21:36:31 +020017#define pr_fmt(fmt) "efi: " fmt
18
Ard Biesheuvel161485e2014-08-04 18:16:00 +020019#include <linux/bug.h>
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020020#include <linux/efi.h>
Mark Rutland1d04ba12016-04-25 21:07:08 +010021#include <linux/irqflags.h>
Ard Biesheuvel161485e2014-08-04 18:16:00 +020022#include <linux/mutex.h>
Ard Biesheuveldce48e32016-07-15 21:36:31 +020023#include <linux/semaphore.h>
Mark Rutland1d04ba12016-04-25 21:07:08 +010024#include <linux/stringify.h>
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020025#include <asm/efi.h>
26
Alex Thorlton80e75592016-06-25 08:20:27 +010027/*
28 * Wrap around the new efi_call_virt_generic() macros so that the
29 * code doesn't get too cluttered:
30 */
31#define efi_call_virt(f, args...) \
32 efi_call_virt_pointer(efi.systab->runtime, f, args)
33#define __efi_call_virt(f, args...) \
34 __efi_call_virt_pointer(efi.systab->runtime, f, args)
35
36void efi_call_virt_check_flags(unsigned long flags, const char *call)
Mark Rutland1d04ba12016-04-25 21:07:08 +010037{
38 unsigned long cur_flags, mismatch;
39
40 local_save_flags(cur_flags);
41
42 mismatch = flags ^ cur_flags;
43 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
44 return;
45
46 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
47 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
48 flags, cur_flags, call);
49 local_irq_restore(flags);
50}
Mark Rutland1d04ba12016-04-25 21:07:08 +010051
52/*
Ard Biesheuvel161485e2014-08-04 18:16:00 +020053 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
54 * reentrant, and there are particular combinations of calls that need to be
55 * serialized. (source: UEFI Specification v2.4A)
56 *
57 * Table 31. Rules for Reentry Into Runtime Services
58 * +------------------------------------+-------------------------------+
59 * | If previous call is busy in | Forbidden to call |
60 * +------------------------------------+-------------------------------+
61 * | Any | SetVirtualAddressMap() |
62 * +------------------------------------+-------------------------------+
63 * | ConvertPointer() | ConvertPointer() |
64 * +------------------------------------+-------------------------------+
65 * | SetVariable() | ResetSystem() |
66 * | UpdateCapsule() | |
67 * | SetTime() | |
68 * | SetWakeupTime() | |
69 * | GetNextHighMonotonicCount() | |
70 * +------------------------------------+-------------------------------+
71 * | GetVariable() | GetVariable() |
72 * | GetNextVariableName() | GetNextVariableName() |
73 * | SetVariable() | SetVariable() |
74 * | QueryVariableInfo() | QueryVariableInfo() |
75 * | UpdateCapsule() | UpdateCapsule() |
76 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
77 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
78 * +------------------------------------+-------------------------------+
79 * | GetTime() | GetTime() |
80 * | SetTime() | SetTime() |
81 * | GetWakeupTime() | GetWakeupTime() |
82 * | SetWakeupTime() | SetWakeupTime() |
83 * +------------------------------------+-------------------------------+
84 *
85 * Due to the fact that the EFI pstore may write to the variable store in
Ard Biesheuveldce48e32016-07-15 21:36:31 +020086 * interrupt context, we need to use a lock for at least the groups that
Ard Biesheuvel161485e2014-08-04 18:16:00 +020087 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
88 * none of the remaining functions are actually ever called at runtime.
Ard Biesheuveldce48e32016-07-15 21:36:31 +020089 * So let's just use a single lock to serialize all Runtime Services calls.
Ard Biesheuvel161485e2014-08-04 18:16:00 +020090 */
Ard Biesheuveldce48e32016-07-15 21:36:31 +020091static DEFINE_SEMAPHORE(efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +020092
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020093static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
94{
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020095 efi_status_t status;
96
Ard Biesheuveldce48e32016-07-15 21:36:31 +020097 if (down_interruptible(&efi_runtime_lock))
98 return EFI_ABORTED;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020099 status = efi_call_virt(get_time, tm, tc);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200100 up(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200101 return status;
102}
103
104static efi_status_t virt_efi_set_time(efi_time_t *tm)
105{
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200106 efi_status_t status;
107
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200108 if (down_interruptible(&efi_runtime_lock))
109 return EFI_ABORTED;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200110 status = efi_call_virt(set_time, tm);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200111 up(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200112 return status;
113}
114
115static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
116 efi_bool_t *pending,
117 efi_time_t *tm)
118{
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200119 efi_status_t status;
120
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200121 if (down_interruptible(&efi_runtime_lock))
122 return EFI_ABORTED;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200123 status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200124 up(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200125 return status;
126}
127
128static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
129{
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200130 efi_status_t status;
131
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200132 if (down_interruptible(&efi_runtime_lock))
133 return EFI_ABORTED;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200134 status = efi_call_virt(set_wakeup_time, enabled, tm);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200135 up(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200136 return status;
137}
138
139static efi_status_t virt_efi_get_variable(efi_char16_t *name,
140 efi_guid_t *vendor,
141 u32 *attr,
142 unsigned long *data_size,
143 void *data)
144{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200145 efi_status_t status;
146
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200147 if (down_interruptible(&efi_runtime_lock))
148 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200149 status = efi_call_virt(get_variable, name, vendor, attr, data_size,
150 data);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200151 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200152 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200153}
154
155static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
156 efi_char16_t *name,
157 efi_guid_t *vendor)
158{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200159 efi_status_t status;
160
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200161 if (down_interruptible(&efi_runtime_lock))
162 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200163 status = efi_call_virt(get_next_variable, name_size, name, vendor);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200164 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200165 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200166}
167
168static efi_status_t virt_efi_set_variable(efi_char16_t *name,
169 efi_guid_t *vendor,
170 u32 attr,
171 unsigned long data_size,
172 void *data)
173{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200174 efi_status_t status;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200175
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200176 if (down_interruptible(&efi_runtime_lock))
177 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200178 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
179 data);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200180 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200181 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200182}
183
Matt Fleming6d80dba2014-09-30 21:58:52 +0100184static efi_status_t
185virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
186 u32 attr, unsigned long data_size,
187 void *data)
188{
Matt Fleming6d80dba2014-09-30 21:58:52 +0100189 efi_status_t status;
190
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200191 if (down_trylock(&efi_runtime_lock))
Matt Fleming6d80dba2014-09-30 21:58:52 +0100192 return EFI_NOT_READY;
193
194 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
195 data);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200196 up(&efi_runtime_lock);
Matt Fleming6d80dba2014-09-30 21:58:52 +0100197 return status;
198}
199
200
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200201static efi_status_t virt_efi_query_variable_info(u32 attr,
202 u64 *storage_space,
203 u64 *remaining_space,
204 u64 *max_variable_size)
205{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200206 efi_status_t status;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200207
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200208 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
209 return EFI_UNSUPPORTED;
210
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200211 if (down_interruptible(&efi_runtime_lock))
212 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200213 status = efi_call_virt(query_variable_info, attr, storage_space,
214 remaining_space, max_variable_size);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200215 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200216 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200217}
218
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000219static efi_status_t
220virt_efi_query_variable_info_nonblocking(u32 attr,
221 u64 *storage_space,
222 u64 *remaining_space,
223 u64 *max_variable_size)
224{
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000225 efi_status_t status;
226
227 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
228 return EFI_UNSUPPORTED;
229
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200230 if (down_trylock(&efi_runtime_lock))
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000231 return EFI_NOT_READY;
232
233 status = efi_call_virt(query_variable_info, attr, storage_space,
234 remaining_space, max_variable_size);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200235 up(&efi_runtime_lock);
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000236 return status;
237}
238
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200239static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
240{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200241 efi_status_t status;
242
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200243 if (down_interruptible(&efi_runtime_lock))
244 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200245 status = efi_call_virt(get_next_high_mono_count, count);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200246 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200247 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200248}
249
250static void virt_efi_reset_system(int reset_type,
251 efi_status_t status,
252 unsigned long data_size,
253 efi_char16_t *data)
254{
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200255 if (down_interruptible(&efi_runtime_lock)) {
256 pr_warn("failed to invoke the reset_system() runtime service:\n"
257 "could not get exclusive access to the firmware\n");
258 return;
259 }
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200260 __efi_call_virt(reset_system, reset_type, status, data_size, data);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200261 up(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200262}
263
264static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
265 unsigned long count,
266 unsigned long sg_list)
267{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200268 efi_status_t status;
269
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200270 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
271 return EFI_UNSUPPORTED;
272
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200273 if (down_interruptible(&efi_runtime_lock))
274 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200275 status = efi_call_virt(update_capsule, capsules, count, sg_list);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200276 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200277 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200278}
279
280static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
281 unsigned long count,
282 u64 *max_size,
283 int *reset_type)
284{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200285 efi_status_t status;
286
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200287 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
288 return EFI_UNSUPPORTED;
289
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200290 if (down_interruptible(&efi_runtime_lock))
291 return EFI_ABORTED;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200292 status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
293 reset_type);
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200294 up(&efi_runtime_lock);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200295 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200296}
297
298void efi_native_runtime_setup(void)
299{
300 efi.get_time = virt_efi_get_time;
301 efi.set_time = virt_efi_set_time;
302 efi.get_wakeup_time = virt_efi_get_wakeup_time;
303 efi.set_wakeup_time = virt_efi_set_wakeup_time;
304 efi.get_variable = virt_efi_get_variable;
305 efi.get_next_variable = virt_efi_get_next_variable;
306 efi.set_variable = virt_efi_set_variable;
Matt Fleming6d80dba2014-09-30 21:58:52 +0100307 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200308 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
309 efi.reset_system = virt_efi_reset_system;
310 efi.query_variable_info = virt_efi_query_variable_info;
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000311 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200312 efi.update_capsule = virt_efi_update_capsule;
313 efi.query_capsule_caps = virt_efi_query_capsule_caps;
314}