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