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
92void efi_call_virt_check_flags(unsigned long flags, const char *call)
93{
94 unsigned long cur_flags, mismatch;
95
96 local_save_flags(cur_flags);
97
98 mismatch = flags ^ cur_flags;
99 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
100 return;
101
102 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
103 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
104 flags, cur_flags, call);
105 local_irq_restore(flags);
106}
107
108
109
110
111
112
113
114
115
116
117
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
147static DEFINE_SEMAPHORE(efi_runtime_lock);
148
149
150
151
152
153
154
155
156
157
158
159static void efi_call_rts(struct work_struct *work)
160{
161 void *arg1, *arg2, *arg3, *arg4, *arg5;
162 efi_status_t status = EFI_NOT_FOUND;
163
164 arg1 = efi_rts_work.arg1;
165 arg2 = efi_rts_work.arg2;
166 arg3 = efi_rts_work.arg3;
167 arg4 = efi_rts_work.arg4;
168 arg5 = efi_rts_work.arg5;
169
170 switch (efi_rts_work.efi_rts_id) {
171 case GET_TIME:
172 status = efi_call_virt(get_time, (efi_time_t *)arg1,
173 (efi_time_cap_t *)arg2);
174 break;
175 case SET_TIME:
176 status = efi_call_virt(set_time, (efi_time_t *)arg1);
177 break;
178 case GET_WAKEUP_TIME:
179 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
180 (efi_bool_t *)arg2, (efi_time_t *)arg3);
181 break;
182 case SET_WAKEUP_TIME:
183 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
184 (efi_time_t *)arg2);
185 break;
186 case GET_VARIABLE:
187 status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
188 (efi_guid_t *)arg2, (u32 *)arg3,
189 (unsigned long *)arg4, (void *)arg5);
190 break;
191 case GET_NEXT_VARIABLE:
192 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
193 (efi_char16_t *)arg2,
194 (efi_guid_t *)arg3);
195 break;
196 case SET_VARIABLE:
197 status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
198 (efi_guid_t *)arg2, *(u32 *)arg3,
199 *(unsigned long *)arg4, (void *)arg5);
200 break;
201 case QUERY_VARIABLE_INFO:
202 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
203 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
204 break;
205 case GET_NEXT_HIGH_MONO_COUNT:
206 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
207 break;
208 case UPDATE_CAPSULE:
209 status = efi_call_virt(update_capsule,
210 (efi_capsule_header_t **)arg1,
211 *(unsigned long *)arg2,
212 *(unsigned long *)arg3);
213 break;
214 case QUERY_CAPSULE_CAPS:
215 status = efi_call_virt(query_capsule_caps,
216 (efi_capsule_header_t **)arg1,
217 *(unsigned long *)arg2, (u64 *)arg3,
218 (int *)arg4);
219 break;
220 default:
221
222
223
224
225
226 pr_err("Requested executing invalid EFI Runtime Service.\n");
227 }
228 efi_rts_work.status = status;
229 complete(&efi_rts_work.efi_rts_comp);
230}
231
232static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
233{
234 efi_status_t status;
235
236 if (down_interruptible(&efi_runtime_lock))
237 return EFI_ABORTED;
238 status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL);
239 up(&efi_runtime_lock);
240 return status;
241}
242
243static efi_status_t virt_efi_set_time(efi_time_t *tm)
244{
245 efi_status_t status;
246
247 if (down_interruptible(&efi_runtime_lock))
248 return EFI_ABORTED;
249 status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL);
250 up(&efi_runtime_lock);
251 return status;
252}
253
254static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
255 efi_bool_t *pending,
256 efi_time_t *tm)
257{
258 efi_status_t status;
259
260 if (down_interruptible(&efi_runtime_lock))
261 return EFI_ABORTED;
262 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL,
263 NULL);
264 up(&efi_runtime_lock);
265 return status;
266}
267
268static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
269{
270 efi_status_t status;
271
272 if (down_interruptible(&efi_runtime_lock))
273 return EFI_ABORTED;
274 status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
275 NULL);
276 up(&efi_runtime_lock);
277 return status;
278}
279
280static efi_status_t virt_efi_get_variable(efi_char16_t *name,
281 efi_guid_t *vendor,
282 u32 *attr,
283 unsigned long *data_size,
284 void *data)
285{
286 efi_status_t status;
287
288 if (down_interruptible(&efi_runtime_lock))
289 return EFI_ABORTED;
290 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,
291 data);
292 up(&efi_runtime_lock);
293 return status;
294}
295
296static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
297 efi_char16_t *name,
298 efi_guid_t *vendor)
299{
300 efi_status_t status;
301
302 if (down_interruptible(&efi_runtime_lock))
303 return EFI_ABORTED;
304 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor,
305 NULL, NULL);
306 up(&efi_runtime_lock);
307 return status;
308}
309
310static efi_status_t virt_efi_set_variable(efi_char16_t *name,
311 efi_guid_t *vendor,
312 u32 attr,
313 unsigned long data_size,
314 void *data)
315{
316 efi_status_t status;
317
318 if (down_interruptible(&efi_runtime_lock))
319 return EFI_ABORTED;
320 status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size,
321 data);
322 up(&efi_runtime_lock);
323 return status;
324}
325
326static efi_status_t
327virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
328 u32 attr, unsigned long data_size,
329 void *data)
330{
331 efi_status_t status;
332
333 if (down_trylock(&efi_runtime_lock))
334 return EFI_NOT_READY;
335
336 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
337 data);
338 up(&efi_runtime_lock);
339 return status;
340}
341
342
343static efi_status_t virt_efi_query_variable_info(u32 attr,
344 u64 *storage_space,
345 u64 *remaining_space,
346 u64 *max_variable_size)
347{
348 efi_status_t status;
349
350 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
351 return EFI_UNSUPPORTED;
352
353 if (down_interruptible(&efi_runtime_lock))
354 return EFI_ABORTED;
355 status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space,
356 remaining_space, max_variable_size, NULL);
357 up(&efi_runtime_lock);
358 return status;
359}
360
361static efi_status_t
362virt_efi_query_variable_info_nonblocking(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_trylock(&efi_runtime_lock))
373 return EFI_NOT_READY;
374
375 status = efi_call_virt(query_variable_info, attr, storage_space,
376 remaining_space, max_variable_size);
377 up(&efi_runtime_lock);
378 return status;
379}
380
381static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
382{
383 efi_status_t status;
384
385 if (down_interruptible(&efi_runtime_lock))
386 return EFI_ABORTED;
387 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
388 NULL, NULL);
389 up(&efi_runtime_lock);
390 return status;
391}
392
393static void virt_efi_reset_system(int reset_type,
394 efi_status_t status,
395 unsigned long data_size,
396 efi_char16_t *data)
397{
398 if (down_interruptible(&efi_runtime_lock)) {
399 pr_warn("failed to invoke the reset_system() runtime service:\n"
400 "could not get exclusive access to the firmware\n");
401 return;
402 }
403 efi_rts_work.efi_rts_id = RESET_SYSTEM;
404 __efi_call_virt(reset_system, reset_type, status, data_size, data);
405 up(&efi_runtime_lock);
406}
407
408static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
409 unsigned long count,
410 unsigned long sg_list)
411{
412 efi_status_t status;
413
414 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
415 return EFI_UNSUPPORTED;
416
417 if (down_interruptible(&efi_runtime_lock))
418 return EFI_ABORTED;
419 status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list,
420 NULL, NULL);
421 up(&efi_runtime_lock);
422 return status;
423}
424
425static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
426 unsigned long count,
427 u64 *max_size,
428 int *reset_type)
429{
430 efi_status_t status;
431
432 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
433 return EFI_UNSUPPORTED;
434
435 if (down_interruptible(&efi_runtime_lock))
436 return EFI_ABORTED;
437 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count,
438 max_size, reset_type, NULL);
439 up(&efi_runtime_lock);
440 return status;
441}
442
443void efi_native_runtime_setup(void)
444{
445 efi.get_time = virt_efi_get_time;
446 efi.set_time = virt_efi_set_time;
447 efi.get_wakeup_time = virt_efi_get_wakeup_time;
448 efi.set_wakeup_time = virt_efi_set_wakeup_time;
449 efi.get_variable = virt_efi_get_variable;
450 efi.get_next_variable = virt_efi_get_next_variable;
451 efi.set_variable = virt_efi_set_variable;
452 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
453 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
454 efi.reset_system = virt_efi_reset_system;
455 efi.query_variable_info = virt_efi_query_variable_info;
456 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
457 efi.update_capsule = virt_efi_update_capsule;
458 efi.query_capsule_caps = virt_efi_query_capsule_caps;
459}
460