blob: 4349206198b211b8ba2a6291af358e8c94bf9130 [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 Biesheuvel161485e2014-08-04 18:16:00 +020017#include <linux/bug.h>
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020018#include <linux/efi.h>
Ard Biesheuvel161485e2014-08-04 18:16:00 +020019#include <linux/mutex.h>
20#include <linux/spinlock.h>
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020021#include <asm/efi.h>
22
23/*
Ard Biesheuvel161485e2014-08-04 18:16:00 +020024 * According to section 7.1 of the UEFI spec, Runtime Services are not fully
25 * reentrant, and there are particular combinations of calls that need to be
26 * serialized. (source: UEFI Specification v2.4A)
27 *
28 * Table 31. Rules for Reentry Into Runtime Services
29 * +------------------------------------+-------------------------------+
30 * | If previous call is busy in | Forbidden to call |
31 * +------------------------------------+-------------------------------+
32 * | Any | SetVirtualAddressMap() |
33 * +------------------------------------+-------------------------------+
34 * | ConvertPointer() | ConvertPointer() |
35 * +------------------------------------+-------------------------------+
36 * | SetVariable() | ResetSystem() |
37 * | UpdateCapsule() | |
38 * | SetTime() | |
39 * | SetWakeupTime() | |
40 * | GetNextHighMonotonicCount() | |
41 * +------------------------------------+-------------------------------+
42 * | GetVariable() | GetVariable() |
43 * | GetNextVariableName() | GetNextVariableName() |
44 * | SetVariable() | SetVariable() |
45 * | QueryVariableInfo() | QueryVariableInfo() |
46 * | UpdateCapsule() | UpdateCapsule() |
47 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
48 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
49 * +------------------------------------+-------------------------------+
50 * | GetTime() | GetTime() |
51 * | SetTime() | SetTime() |
52 * | GetWakeupTime() | GetWakeupTime() |
53 * | SetWakeupTime() | SetWakeupTime() |
54 * +------------------------------------+-------------------------------+
55 *
56 * Due to the fact that the EFI pstore may write to the variable store in
57 * interrupt context, we need to use a spinlock for at least the groups that
58 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
59 * none of the remaining functions are actually ever called at runtime.
60 * So let's just use a single spinlock to serialize all Runtime Services calls.
61 */
62static DEFINE_SPINLOCK(efi_runtime_lock);
63
64/*
65 * Some runtime services calls can be reentrant under NMI, even if the table
66 * above says they are not. (source: UEFI Specification v2.4A)
67 *
68 * Table 32. Functions that may be called after Machine Check, INIT and NMI
69 * +----------------------------+------------------------------------------+
70 * | Function | Called after Machine Check, INIT and NMI |
71 * +----------------------------+------------------------------------------+
72 * | GetTime() | Yes, even if previously busy. |
73 * | GetVariable() | Yes, even if previously busy |
74 * | GetNextVariableName() | Yes, even if previously busy |
75 * | QueryVariableInfo() | Yes, even if previously busy |
76 * | SetVariable() | Yes, even if previously busy |
77 * | UpdateCapsule() | Yes, even if previously busy |
78 * | QueryCapsuleCapabilities() | Yes, even if previously busy |
79 * | ResetSystem() | Yes, even if previously busy |
80 * +----------------------------+------------------------------------------+
81 *
82 * In order to prevent deadlocks under NMI, the wrappers for these functions
83 * may only grab the efi_runtime_lock or rtc_lock spinlocks if !efi_in_nmi().
84 * However, not all of the services listed are reachable through NMI code paths,
85 * so the the special handling as suggested by the UEFI spec is only implemented
86 * for QueryVariableInfo() and SetVariable(), as these can be reached in NMI
87 * context through efi_pstore_write().
88 */
89#ifndef efi_in_nmi
90#define efi_in_nmi() (0)
91#endif
92
93/*
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020094 * As per commit ef68c8f87ed1 ("x86: Serialize EFI time accesses on rtc_lock"),
95 * the EFI specification requires that callers of the time related runtime
96 * functions serialize with other CMOS accesses in the kernel, as the EFI time
97 * functions may choose to also use the legacy CMOS RTC.
98 */
99__weak DEFINE_SPINLOCK(rtc_lock);
100
101static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
102{
103 unsigned long flags;
104 efi_status_t status;
105
106 spin_lock_irqsave(&rtc_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200107 spin_lock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200108 status = efi_call_virt(get_time, tm, tc);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200109 spin_unlock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200110 spin_unlock_irqrestore(&rtc_lock, flags);
111 return status;
112}
113
114static efi_status_t virt_efi_set_time(efi_time_t *tm)
115{
116 unsigned long flags;
117 efi_status_t status;
118
119 spin_lock_irqsave(&rtc_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200120 spin_lock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200121 status = efi_call_virt(set_time, tm);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200122 spin_unlock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200123 spin_unlock_irqrestore(&rtc_lock, flags);
124 return status;
125}
126
127static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
128 efi_bool_t *pending,
129 efi_time_t *tm)
130{
131 unsigned long flags;
132 efi_status_t status;
133
134 spin_lock_irqsave(&rtc_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200135 spin_lock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200136 status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200137 spin_unlock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200138 spin_unlock_irqrestore(&rtc_lock, flags);
139 return status;
140}
141
142static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
143{
144 unsigned long flags;
145 efi_status_t status;
146
147 spin_lock_irqsave(&rtc_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200148 spin_lock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200149 status = efi_call_virt(set_wakeup_time, enabled, tm);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200150 spin_unlock(&efi_runtime_lock);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200151 spin_unlock_irqrestore(&rtc_lock, flags);
152 return status;
153}
154
155static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156 efi_guid_t *vendor,
157 u32 *attr,
158 unsigned long *data_size,
159 void *data)
160{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200161 unsigned long flags;
162 efi_status_t status;
163
164 spin_lock_irqsave(&efi_runtime_lock, flags);
165 status = efi_call_virt(get_variable, name, vendor, attr, data_size,
166 data);
167 spin_unlock_irqrestore(&efi_runtime_lock, flags);
168 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200169}
170
171static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
172 efi_char16_t *name,
173 efi_guid_t *vendor)
174{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200175 unsigned long flags;
176 efi_status_t status;
177
178 spin_lock_irqsave(&efi_runtime_lock, flags);
179 status = efi_call_virt(get_next_variable, name_size, name, vendor);
180 spin_unlock_irqrestore(&efi_runtime_lock, flags);
181 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200182}
183
184static efi_status_t virt_efi_set_variable(efi_char16_t *name,
185 efi_guid_t *vendor,
186 u32 attr,
187 unsigned long data_size,
188 void *data)
189{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200190 unsigned long flags;
191 efi_status_t status;
192 bool __in_nmi = efi_in_nmi();
193
194 if (!__in_nmi)
195 spin_lock_irqsave(&efi_runtime_lock, flags);
196 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
197 data);
198 if (!__in_nmi)
199 spin_unlock_irqrestore(&efi_runtime_lock, flags);
200 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200201}
202
Matt Fleming6d80dba2014-09-30 21:58:52 +0100203static efi_status_t
204virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
205 u32 attr, unsigned long data_size,
206 void *data)
207{
208 unsigned long flags;
209 efi_status_t status;
210
211 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
212 return EFI_NOT_READY;
213
214 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
215 data);
216 spin_unlock_irqrestore(&efi_runtime_lock, flags);
217 return status;
218}
219
220
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200221static efi_status_t virt_efi_query_variable_info(u32 attr,
222 u64 *storage_space,
223 u64 *remaining_space,
224 u64 *max_variable_size)
225{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200226 unsigned long flags;
227 efi_status_t status;
228 bool __in_nmi = efi_in_nmi();
229
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200230 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
231 return EFI_UNSUPPORTED;
232
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200233 if (!__in_nmi)
234 spin_lock_irqsave(&efi_runtime_lock, flags);
235 status = efi_call_virt(query_variable_info, attr, storage_space,
236 remaining_space, max_variable_size);
237 if (!__in_nmi)
238 spin_unlock_irqrestore(&efi_runtime_lock, flags);
239 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200240}
241
242static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
243{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200244 unsigned long flags;
245 efi_status_t status;
246
247 spin_lock_irqsave(&efi_runtime_lock, flags);
248 status = efi_call_virt(get_next_high_mono_count, count);
249 spin_unlock_irqrestore(&efi_runtime_lock, flags);
250 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200251}
252
253static void virt_efi_reset_system(int reset_type,
254 efi_status_t status,
255 unsigned long data_size,
256 efi_char16_t *data)
257{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200258 unsigned long flags;
259
260 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200261 __efi_call_virt(reset_system, reset_type, status, data_size, data);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200262 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200263}
264
265static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
266 unsigned long count,
267 unsigned long sg_list)
268{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200269 unsigned long flags;
270 efi_status_t status;
271
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200272 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
273 return EFI_UNSUPPORTED;
274
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200275 spin_lock_irqsave(&efi_runtime_lock, flags);
276 status = efi_call_virt(update_capsule, capsules, count, sg_list);
277 spin_unlock_irqrestore(&efi_runtime_lock, flags);
278 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200279}
280
281static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
282 unsigned long count,
283 u64 *max_size,
284 int *reset_type)
285{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200286 unsigned long flags;
287 efi_status_t status;
288
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200289 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
290 return EFI_UNSUPPORTED;
291
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200292 spin_lock_irqsave(&efi_runtime_lock, flags);
293 status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
294 reset_type);
295 spin_unlock_irqrestore(&efi_runtime_lock, flags);
296 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200297}
298
299void efi_native_runtime_setup(void)
300{
301 efi.get_time = virt_efi_get_time;
302 efi.set_time = virt_efi_set_time;
303 efi.get_wakeup_time = virt_efi_get_wakeup_time;
304 efi.set_wakeup_time = virt_efi_set_wakeup_time;
305 efi.get_variable = virt_efi_get_variable;
306 efi.get_next_variable = virt_efi_get_next_variable;
307 efi.set_variable = virt_efi_set_variable;
Matt Fleming6d80dba2014-09-30 21:58:52 +0100308 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200309 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
310 efi.reset_system = virt_efi_reset_system;
311 efi.query_variable_info = virt_efi_query_variable_info;
312 efi.update_capsule = virt_efi_update_capsule;
313 efi.query_capsule_caps = virt_efi_query_capsule_caps;
314}