Commit | Line | Data |
---|---|---|
022ee6c5 AB |
1 | /* |
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 | ||
161485e8 | 17 | #include <linux/bug.h> |
022ee6c5 | 18 | #include <linux/efi.h> |
161485e8 AB |
19 | #include <linux/mutex.h> |
20 | #include <linux/spinlock.h> | |
022ee6c5 AB |
21 | #include <asm/efi.h> |
22 | ||
161485e8 AB |
23 | /* |
24 | * 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 | */ | |
62 | static 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 | */ | |
161485e8 | 89 | |
022ee6c5 AB |
90 | /* |
91 | * As per commit ef68c8f87ed1 ("x86: Serialize EFI time accesses on rtc_lock"), | |
92 | * the EFI specification requires that callers of the time related runtime | |
93 | * functions serialize with other CMOS accesses in the kernel, as the EFI time | |
94 | * functions may choose to also use the legacy CMOS RTC. | |
95 | */ | |
96 | __weak DEFINE_SPINLOCK(rtc_lock); | |
97 | ||
98 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) | |
99 | { | |
100 | unsigned long flags; | |
101 | efi_status_t status; | |
102 | ||
103 | spin_lock_irqsave(&rtc_lock, flags); | |
161485e8 | 104 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 105 | status = efi_call_virt(get_time, tm, tc); |
161485e8 | 106 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
107 | spin_unlock_irqrestore(&rtc_lock, flags); |
108 | return status; | |
109 | } | |
110 | ||
111 | static efi_status_t virt_efi_set_time(efi_time_t *tm) | |
112 | { | |
113 | unsigned long flags; | |
114 | efi_status_t status; | |
115 | ||
116 | spin_lock_irqsave(&rtc_lock, flags); | |
161485e8 | 117 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 118 | status = efi_call_virt(set_time, tm); |
161485e8 | 119 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
120 | spin_unlock_irqrestore(&rtc_lock, flags); |
121 | return status; | |
122 | } | |
123 | ||
124 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, | |
125 | efi_bool_t *pending, | |
126 | efi_time_t *tm) | |
127 | { | |
128 | unsigned long flags; | |
129 | efi_status_t status; | |
130 | ||
131 | spin_lock_irqsave(&rtc_lock, flags); | |
161485e8 | 132 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 133 | status = efi_call_virt(get_wakeup_time, enabled, pending, tm); |
161485e8 | 134 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
135 | spin_unlock_irqrestore(&rtc_lock, flags); |
136 | return status; | |
137 | } | |
138 | ||
139 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) | |
140 | { | |
141 | unsigned long flags; | |
142 | efi_status_t status; | |
143 | ||
144 | spin_lock_irqsave(&rtc_lock, flags); | |
161485e8 | 145 | spin_lock(&efi_runtime_lock); |
022ee6c5 | 146 | status = efi_call_virt(set_wakeup_time, enabled, tm); |
161485e8 | 147 | spin_unlock(&efi_runtime_lock); |
022ee6c5 AB |
148 | spin_unlock_irqrestore(&rtc_lock, flags); |
149 | return status; | |
150 | } | |
151 | ||
152 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, | |
153 | efi_guid_t *vendor, | |
154 | u32 *attr, | |
155 | unsigned long *data_size, | |
156 | void *data) | |
157 | { | |
161485e8 AB |
158 | unsigned long flags; |
159 | efi_status_t status; | |
160 | ||
161 | spin_lock_irqsave(&efi_runtime_lock, flags); | |
162 | status = efi_call_virt(get_variable, name, vendor, attr, data_size, | |
163 | data); | |
164 | spin_unlock_irqrestore(&efi_runtime_lock, flags); | |
165 | return status; | |
022ee6c5 AB |
166 | } |
167 | ||
168 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, | |
169 | efi_char16_t *name, | |
170 | efi_guid_t *vendor) | |
171 | { | |
161485e8 AB |
172 | unsigned long flags; |
173 | efi_status_t status; | |
174 | ||
175 | spin_lock_irqsave(&efi_runtime_lock, flags); | |
176 | status = efi_call_virt(get_next_variable, name_size, name, vendor); | |
177 | spin_unlock_irqrestore(&efi_runtime_lock, flags); | |
178 | return status; | |
022ee6c5 AB |
179 | } |
180 | ||
181 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, | |
182 | efi_guid_t *vendor, | |
183 | u32 attr, | |
184 | unsigned long data_size, | |
185 | void *data) | |
186 | { | |
161485e8 AB |
187 | unsigned long flags; |
188 | efi_status_t status; | |
161485e8 | 189 | |
60b4dc77 | 190 | spin_lock_irqsave(&efi_runtime_lock, flags); |
161485e8 AB |
191 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, |
192 | data); | |
60b4dc77 | 193 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
161485e8 | 194 | return status; |
022ee6c5 AB |
195 | } |
196 | ||
6d80dba1 MF |
197 | static efi_status_t |
198 | virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor, | |
199 | u32 attr, unsigned long data_size, | |
200 | void *data) | |
201 | { | |
202 | unsigned long flags; | |
203 | efi_status_t status; | |
204 | ||
205 | if (!spin_trylock_irqsave(&efi_runtime_lock, flags)) | |
206 | return EFI_NOT_READY; | |
207 | ||
208 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, | |
209 | data); | |
210 | spin_unlock_irqrestore(&efi_runtime_lock, flags); | |
211 | return status; | |
212 | } | |
213 | ||
214 | ||
022ee6c5 AB |
215 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
216 | u64 *storage_space, | |
217 | u64 *remaining_space, | |
218 | u64 *max_variable_size) | |
219 | { | |
161485e8 AB |
220 | unsigned long flags; |
221 | efi_status_t status; | |
161485e8 | 222 | |
022ee6c5 AB |
223 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
224 | return EFI_UNSUPPORTED; | |
225 | ||
60b4dc77 | 226 | spin_lock_irqsave(&efi_runtime_lock, flags); |
161485e8 AB |
227 | status = efi_call_virt(query_variable_info, attr, storage_space, |
228 | remaining_space, max_variable_size); | |
60b4dc77 | 229 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
161485e8 | 230 | return status; |
022ee6c5 AB |
231 | } |
232 | ||
233 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) | |
234 | { | |
161485e8 AB |
235 | unsigned long flags; |
236 | efi_status_t status; | |
237 | ||
238 | spin_lock_irqsave(&efi_runtime_lock, flags); | |
239 | status = efi_call_virt(get_next_high_mono_count, count); | |
240 | spin_unlock_irqrestore(&efi_runtime_lock, flags); | |
241 | return status; | |
022ee6c5 AB |
242 | } |
243 | ||
244 | static void virt_efi_reset_system(int reset_type, | |
245 | efi_status_t status, | |
246 | unsigned long data_size, | |
247 | efi_char16_t *data) | |
248 | { | |
161485e8 AB |
249 | unsigned long flags; |
250 | ||
251 | spin_lock_irqsave(&efi_runtime_lock, flags); | |
022ee6c5 | 252 | __efi_call_virt(reset_system, reset_type, status, data_size, data); |
161485e8 | 253 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
022ee6c5 AB |
254 | } |
255 | ||
256 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, | |
257 | unsigned long count, | |
258 | unsigned long sg_list) | |
259 | { | |
161485e8 AB |
260 | unsigned long flags; |
261 | efi_status_t status; | |
262 | ||
022ee6c5 AB |
263 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
264 | return EFI_UNSUPPORTED; | |
265 | ||
161485e8 AB |
266 | spin_lock_irqsave(&efi_runtime_lock, flags); |
267 | status = efi_call_virt(update_capsule, capsules, count, sg_list); | |
268 | spin_unlock_irqrestore(&efi_runtime_lock, flags); | |
269 | return status; | |
022ee6c5 AB |
270 | } |
271 | ||
272 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, | |
273 | unsigned long count, | |
274 | u64 *max_size, | |
275 | int *reset_type) | |
276 | { | |
161485e8 AB |
277 | unsigned long flags; |
278 | efi_status_t status; | |
279 | ||
022ee6c5 AB |
280 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
281 | return EFI_UNSUPPORTED; | |
282 | ||
161485e8 AB |
283 | spin_lock_irqsave(&efi_runtime_lock, flags); |
284 | status = efi_call_virt(query_capsule_caps, capsules, count, max_size, | |
285 | reset_type); | |
286 | spin_unlock_irqrestore(&efi_runtime_lock, flags); | |
287 | return status; | |
022ee6c5 AB |
288 | } |
289 | ||
290 | void efi_native_runtime_setup(void) | |
291 | { | |
292 | efi.get_time = virt_efi_get_time; | |
293 | efi.set_time = virt_efi_set_time; | |
294 | efi.get_wakeup_time = virt_efi_get_wakeup_time; | |
295 | efi.set_wakeup_time = virt_efi_set_wakeup_time; | |
296 | efi.get_variable = virt_efi_get_variable; | |
297 | efi.get_next_variable = virt_efi_get_next_variable; | |
298 | efi.set_variable = virt_efi_set_variable; | |
6d80dba1 | 299 | efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking; |
022ee6c5 AB |
300 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
301 | efi.reset_system = virt_efi_reset_system; | |
302 | efi.query_variable_info = virt_efi_query_variable_info; | |
303 | efi.update_capsule = virt_efi_update_capsule; | |
304 | efi.query_capsule_caps = virt_efi_query_capsule_caps; | |
305 | } |