1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#define pr_fmt(fmt) "efi: " fmt
27
28#include <linux/bug.h>
29#include <linux/efi.h>
30#include <linux/irqflags.h>
31#include <linux/mutex.h>
32#include <linux/semaphore.h>
33#include <linux/stringify.h>
34#include <linux/workqueue.h>
35#include <linux/completion.h>
36
37#include <asm/efi.h>
38
39
40
41
42
43#define efi_call_virt(f, args...) \
44 efi_call_virt_pointer(efi.systab->runtime, f, args)
45#define __efi_call_virt(f, args...) \
46 __efi_call_virt_pointer(efi.systab->runtime, f, args)
47
48struct efi_runtime_work efi_rts_work;
49
50
51
52
53
54
55
56
57
58
59
60#define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \
61({ \
62 efi_rts_work.status = EFI_ABORTED; \
63 \
64 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { \
65 pr_warn_once("EFI Runtime Services are disabled!\n"); \
66 goto exit; \
67 } \
68 \
69 init_completion(&efi_rts_work.efi_rts_comp); \
70 INIT_WORK(&efi_rts_work.work, efi_call_rts); \
71 efi_rts_work.arg1 = _arg1; \
72 efi_rts_work.arg2 = _arg2; \
73 efi_rts_work.arg3 = _arg3; \
74 efi_rts_work.arg4 = _arg4; \
75 efi_rts_work.arg5 = _arg5; \
76 efi_rts_work.efi_rts_id = _rts; \
77 \
78
79
80
81 \
82 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \
83 wait_for_completion(&efi_rts_work.efi_rts_comp); \
84 else \
85 pr_err("Failed to queue work to efi_rts_wq.\n"); \
86 \
87exit: \
88 efi_rts_work.efi_rts_id = NONE; \
89 efi_rts_work.status; \
90})
91
92#ifndef arch_efi_save_flags
93#define arch_efi_save_flags(state_flags) local_save_flags(state_flags)
94#define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)
95#endif
96
97unsigned long efi_call_virt_save_flags(void)
98{
99 unsigned long flags;
100
101 arch_efi_save_flags(flags);
102 return flags;
103}
104
105void efi_call_virt_check_flags(unsigned long flags, const char *call)
106{
107 unsigned long cur_flags, mismatch;
108
109 cur_flags = efi_call_virt_save_flags();
110
111 mismatch = flags ^ cur_flags;
112 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
113 return;
114
115 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
116 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
117 flags, cur_flags, call);
118 arch_efi_restore_flags(flags);
119}
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160static DEFINE_SEMAPHORE(efi_runtime_lock);
161
162
163
164
165#ifdef CONFIG_X86_UV
166extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
167#endif
168
169
170
171
172
173
174
175
176
177
178
179static void efi_call_rts(struct work_struct *work)
180{
181 void *arg1, *arg2, *arg3, *arg4, *arg5;
182 efi_status_t status = EFI_NOT_FOUND;
183
184 arg1 = efi_rts_work.arg1;
185 arg2 = efi_rts_work.arg2;
186 arg3 = efi_rts_work.arg3;
187 arg4 = efi_rts_work.arg4;
188 arg5 = efi_rts_work.arg5;
189
190 switch (efi_rts_work.efi_rts_id) {
191 case GET_TIME:
192 status = efi_call_virt(get_time, (efi_time_t *)arg1,
193 (efi_time_cap_t *)arg2);
194 break;
195 case SET_TIME:
196 status = efi_call_virt(set_time, (efi_time_t *)arg1);
197 break;
198 case GET_WAKEUP_TIME:
199 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
200 (efi_bool_t *)arg2, (efi_time_t *)arg3);
201 break;
202 case SET_WAKEUP_TIME:
203 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
204 (efi_time_t *)arg2);
205 break;
206 case GET_VARIABLE:
207 status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
208 (efi_guid_t *)arg2, (u32 *)arg3,
209 (unsigned long *)arg4, (void *)arg5);
210 break;
211 case GET_NEXT_VARIABLE:
212 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
213 (efi_char16_t *)arg2,
214 (efi_guid_t *)arg3);
215 break;
216 case SET_VARIABLE:
217 status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
218 (efi_guid_t *)arg2, *(u32 *)arg3,
219 *(unsigned long *)arg4, (void *)arg5);
220 break;
221 case QUERY_VARIABLE_INFO:
222 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
223 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
224 break;
225 case GET_NEXT_HIGH_MONO_COUNT:
226 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
227 break;
228 case UPDATE_CAPSULE:
229 status = efi_call_virt(update_capsule,
230 (efi_capsule_header_t **)arg1,
231 *(unsigned long *)arg2,
232 *(unsigned long *)arg3);
233 break;
234 case QUERY_CAPSULE_CAPS:
235 status = efi_call_virt(query_capsule_caps,
236 (efi_capsule_header_t **)arg1,
237 *(unsigned long *)arg2, (u64 *)arg3,
238 (int *)arg4);
239 break;
240 default:
241
242
243
244
245
246 pr_err("Requested executing invalid EFI Runtime Service.\n");
247 }
248 efi_rts_work.status = status;
249 complete(&efi_rts_work.efi_rts_comp);
250}
251
252static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
253{
254 efi_status_t status;
255
256 if (down_interruptible(&efi_runtime_lock))
257 return EFI_ABORTED;
258 status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL);
259 up(&efi_runtime_lock);
260 return status;
261}
262
263static efi_status_t virt_efi_set_time(efi_time_t *tm)
264{
265 efi_status_t status;
266
267 if (down_interruptible(&efi_runtime_lock))
268 return EFI_ABORTED;
269 status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL);
270 up(&efi_runtime_lock);
271 return status;
272}
273
274static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
275 efi_bool_t *pending,
276 efi_time_t *tm)
277{
278 efi_status_t status;
279
280 if (down_interruptible(&efi_runtime_lock))
281 return EFI_ABORTED;
282 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL,
283 NULL);
284 up(&efi_runtime_lock);
285 return status;
286}
287
288static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
289{
290 efi_status_t status;
291
292 if (down_interruptible(&efi_runtime_lock))
293 return EFI_ABORTED;
294 status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
295 NULL);
296 up(&efi_runtime_lock);
297 return status;
298}
299
300static efi_status_t virt_efi_get_variable(efi_char16_t *name,
301 efi_guid_t *vendor,
302 u32 *attr,
303 unsigned long *data_size,
304 void *data)
305{
306 efi_status_t status;
307
308 if (down_interruptible(&efi_runtime_lock))
309 return EFI_ABORTED;
310 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
311 data);
312 up(&efi_runtime_lock);
313 return status;
314}
315
316static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
317 efi_char16_t *name,
318 efi_guid_t *vendor)
319{
320 efi_status_t status;
321
322 if (down_interruptible(&efi_runtime_lock))
323 return EFI_ABORTED;
324 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor,
325 NULL, NULL);
326 up(&efi_runtime_lock);
327 return status;
328}
329
330static efi_status_t virt_efi_set_variable(efi_char16_t *name,
331 efi_guid_t *vendor,
332 u32 attr,
333 unsigned long data_size,
334 void *data)
335{
336 efi_status_t status;
337
338 if (down_interruptible(&efi_runtime_lock))
339 return EFI_ABORTED;
340 status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size,
341 data);
342 up(&efi_runtime_lock);
343 return status;
344}
345
346static efi_status_t
347virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
348 u32 attr, unsigned long data_size,
349 void *data)
350{
351 efi_status_t status;
352
353 if (down_trylock(&efi_runtime_lock))
354 return EFI_NOT_READY;
355
356 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
357 data);
358 up(&efi_runtime_lock);
359 return status;
360}
361
362
363static efi_status_t virt_efi_query_variable_info(u32 attr,
364 u64 *storage_space,
365 u64 *remaining_space,
366 u64 *max_variable_size)
367{
368 efi_status_t status;
369
370 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
371 return EFI_UNSUPPORTED;
372
373 if (down_interruptible(&efi_runtime_lock))
374 return EFI_ABORTED;
375 status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space,
376 remaining_space, max_variable_size, NULL);
377 up(&efi_runtime_lock);
378 return status;
379}
380
381static efi_status_t
382virt_efi_query_variable_info_nonblocking(u32 attr,
383 u64 *storage_space,
384 u64 *remaining_space,
385 u64 *max_variable_size)
386{
387 efi_status_t status;
388
389 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
390 return EFI_UNSUPPORTED;
391
392 if (down_trylock(&efi_runtime_lock))
393 return EFI_NOT_READY;
394
395 status = efi_call_virt(query_variable_info, attr, storage_space,
396 remaining_space, max_variable_size);
397 up(&efi_runtime_lock);
398 return status;
399}
400
401static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
402{
403 efi_status_t status;
404
405 if (down_interruptible(&efi_runtime_lock))
406 return EFI_ABORTED;
407 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
408 NULL, NULL);
409 up(&efi_runtime_lock);
410 return status;
411}
412
413static void virt_efi_reset_system(int reset_type,
414 efi_status_t status,
415 unsigned long data_size,
416 efi_char16_t *data)
417{
418 if (down_interruptible(&efi_runtime_lock)) {
419 pr_warn("failed to invoke the reset_system() runtime service:\n"
420 "could not get exclusive access to the firmware\n");
421 return;
422 }
423 efi_rts_work.efi_rts_id = RESET_SYSTEM;
424 __efi_call_virt(reset_system, reset_type, status, data_size, data);
425 up(&efi_runtime_lock);
426}
427
428static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
429 unsigned long count,
430 unsigned long sg_list)
431{
432 efi_status_t status;
433
434 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
435 return EFI_UNSUPPORTED;
436
437 if (down_interruptible(&efi_runtime_lock))
438 return EFI_ABORTED;
439 status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list,
440 NULL, NULL);
441 up(&efi_runtime_lock);
442 return status;
443}
444
445static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
446 unsigned long count,
447 u64 *max_size,
448 int *reset_type)
449{
450 efi_status_t status;
451
452 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
453 return EFI_UNSUPPORTED;
454
455 if (down_interruptible(&efi_runtime_lock))
456 return EFI_ABORTED;
457 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count,
458 max_size, reset_type, NULL);
459 up(&efi_runtime_lock);
460 return status;
461}
462
463void efi_native_runtime_setup(void)
464{
465 efi.get_time = virt_efi_get_time;
466 efi.set_time = virt_efi_set_time;
467 efi.get_wakeup_time = virt_efi_get_wakeup_time;
468 efi.set_wakeup_time = virt_efi_set_wakeup_time;
469 efi.get_variable = virt_efi_get_variable;
470 efi.get_next_variable = virt_efi_get_next_variable;
471 efi.set_variable = virt_efi_set_variable;
472 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
473 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
474 efi.reset_system = virt_efi_reset_system;
475 efi.query_variable_info = virt_efi_query_variable_info;
476 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
477 efi.update_capsule = virt_efi_update_capsule;
478 efi.query_capsule_caps = virt_efi_query_capsule_caps;
479}
480