]> Git Repo - J-linux.git/blob - drivers/firmware/efi/runtime-wrappers.c
Merge tag 'amd-drm-next-6.5-2023-06-09' of https://gitlab.freedesktop.org/agd5f/linux...
[J-linux.git] / drivers / firmware / efi / runtime-wrappers.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * runtime-wrappers.c - Runtime Services function call wrappers
4  *
5  * Implementation summary:
6  * -----------------------
7  * 1. When user/kernel thread requests to execute efi_runtime_service(),
8  * enqueue work to efi_rts_wq.
9  * 2. Caller thread waits for completion until the work is finished
10  * because it's dependent on the return status and execution of
11  * efi_runtime_service().
12  * For instance, get_variable() and get_next_variable().
13  *
14  * Copyright (C) 2014 Linaro Ltd. <[email protected]>
15  *
16  * Split off from arch/x86/platform/efi/efi.c
17  *
18  * Copyright (C) 1999 VA Linux Systems
19  * Copyright (C) 1999 Walt Drummond <[email protected]>
20  * Copyright (C) 1999-2002 Hewlett-Packard Co.
21  * Copyright (C) 2005-2008 Intel Co.
22  * Copyright (C) 2013 SuSE Labs
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  * Wrap around the new efi_call_virt_generic() macros so that the
40  * code doesn't get too cluttered:
41  */
42 #define efi_call_virt(f, args...)   \
43         efi_call_virt_pointer(efi.runtime, f, args)
44 #define __efi_call_virt(f, args...) \
45         __efi_call_virt_pointer(efi.runtime, f, args)
46
47 struct efi_runtime_work efi_rts_work;
48
49 /*
50  * efi_queue_work:      Queue efi_runtime_service() and wait until it's done
51  * @rts:                efi_runtime_service() function identifier
52  * @rts_arg<1-5>:       efi_runtime_service() function arguments
53  *
54  * Accesses to efi_runtime_services() are serialized by a binary
55  * semaphore (efi_runtime_lock) and caller waits until the work is
56  * finished, hence _only_ one work is queued at a time and the caller
57  * thread waits for completion.
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                 efi_rts_work.status = EFI_DEVICE_ERROR;                 \
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          * queue_work() returns 0 if work was already on queue,         \
80          * _ideally_ this should never happen.                          \
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                                                                         \
87         WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);               \
88 exit:                                                                   \
89         efi_rts_work.efi_rts_id = EFI_NONE;                             \
90         efi_rts_work.status;                                            \
91 })
92
93 #ifndef arch_efi_save_flags
94 #define arch_efi_save_flags(state_flags)        local_save_flags(state_flags)
95 #define arch_efi_restore_flags(state_flags)     local_irq_restore(state_flags)
96 #endif
97
98 unsigned long efi_call_virt_save_flags(void)
99 {
100         unsigned long flags;
101
102         arch_efi_save_flags(flags);
103         return flags;
104 }
105
106 void efi_call_virt_check_flags(unsigned long flags, const char *call)
107 {
108         unsigned long cur_flags, mismatch;
109
110         cur_flags = efi_call_virt_save_flags();
111
112         mismatch = flags ^ cur_flags;
113         if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
114                 return;
115
116         add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
117         pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
118                            flags, cur_flags, call);
119         arch_efi_restore_flags(flags);
120 }
121
122 /*
123  * According to section 7.1 of the UEFI spec, Runtime Services are not fully
124  * reentrant, and there are particular combinations of calls that need to be
125  * serialized. (source: UEFI Specification v2.4A)
126  *
127  * Table 31. Rules for Reentry Into Runtime Services
128  * +------------------------------------+-------------------------------+
129  * | If previous call is busy in        | Forbidden to call             |
130  * +------------------------------------+-------------------------------+
131  * | Any                                | SetVirtualAddressMap()        |
132  * +------------------------------------+-------------------------------+
133  * | ConvertPointer()                   | ConvertPointer()              |
134  * +------------------------------------+-------------------------------+
135  * | SetVariable()                      | ResetSystem()                 |
136  * | UpdateCapsule()                    |                               |
137  * | SetTime()                          |                               |
138  * | SetWakeupTime()                    |                               |
139  * | GetNextHighMonotonicCount()        |                               |
140  * +------------------------------------+-------------------------------+
141  * | GetVariable()                      | GetVariable()                 |
142  * | GetNextVariableName()              | GetNextVariableName()         |
143  * | SetVariable()                      | SetVariable()                 |
144  * | QueryVariableInfo()                | QueryVariableInfo()           |
145  * | UpdateCapsule()                    | UpdateCapsule()               |
146  * | QueryCapsuleCapabilities()         | QueryCapsuleCapabilities()    |
147  * | GetNextHighMonotonicCount()        | GetNextHighMonotonicCount()   |
148  * +------------------------------------+-------------------------------+
149  * | GetTime()                          | GetTime()                     |
150  * | SetTime()                          | SetTime()                     |
151  * | GetWakeupTime()                    | GetWakeupTime()               |
152  * | SetWakeupTime()                    | SetWakeupTime()               |
153  * +------------------------------------+-------------------------------+
154  *
155  * Due to the fact that the EFI pstore may write to the variable store in
156  * interrupt context, we need to use a lock for at least the groups that
157  * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
158  * none of the remaining functions are actually ever called at runtime.
159  * So let's just use a single lock to serialize all Runtime Services calls.
160  */
161 static DEFINE_SEMAPHORE(efi_runtime_lock, 1);
162
163 /*
164  * Expose the EFI runtime lock to the UV platform
165  */
166 #ifdef CONFIG_X86_UV
167 extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);
168 #endif
169
170 /*
171  * Calls the appropriate efi_runtime_service() with the appropriate
172  * arguments.
173  *
174  * Semantics followed by efi_call_rts() to understand efi_runtime_work:
175  * 1. If argument was a pointer, recast it from void pointer to original
176  * pointer type.
177  * 2. If argument was a value, recast it from void pointer to original
178  * pointer type and dereference it.
179  */
180 static void efi_call_rts(struct work_struct *work)
181 {
182         void *arg1, *arg2, *arg3, *arg4, *arg5;
183         efi_status_t status = EFI_NOT_FOUND;
184
185         arg1 = efi_rts_work.arg1;
186         arg2 = efi_rts_work.arg2;
187         arg3 = efi_rts_work.arg3;
188         arg4 = efi_rts_work.arg4;
189         arg5 = efi_rts_work.arg5;
190
191         switch (efi_rts_work.efi_rts_id) {
192         case EFI_GET_TIME:
193                 status = efi_call_virt(get_time, (efi_time_t *)arg1,
194                                        (efi_time_cap_t *)arg2);
195                 break;
196         case EFI_SET_TIME:
197                 status = efi_call_virt(set_time, (efi_time_t *)arg1);
198                 break;
199         case EFI_GET_WAKEUP_TIME:
200                 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1,
201                                        (efi_bool_t *)arg2, (efi_time_t *)arg3);
202                 break;
203         case EFI_SET_WAKEUP_TIME:
204                 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1,
205                                        (efi_time_t *)arg2);
206                 break;
207         case EFI_GET_VARIABLE:
208                 status = efi_call_virt(get_variable, (efi_char16_t *)arg1,
209                                        (efi_guid_t *)arg2, (u32 *)arg3,
210                                        (unsigned long *)arg4, (void *)arg5);
211                 break;
212         case EFI_GET_NEXT_VARIABLE:
213                 status = efi_call_virt(get_next_variable, (unsigned long *)arg1,
214                                        (efi_char16_t *)arg2,
215                                        (efi_guid_t *)arg3);
216                 break;
217         case EFI_SET_VARIABLE:
218                 status = efi_call_virt(set_variable, (efi_char16_t *)arg1,
219                                        (efi_guid_t *)arg2, *(u32 *)arg3,
220                                        *(unsigned long *)arg4, (void *)arg5);
221                 break;
222         case EFI_QUERY_VARIABLE_INFO:
223                 status = efi_call_virt(query_variable_info, *(u32 *)arg1,
224                                        (u64 *)arg2, (u64 *)arg3, (u64 *)arg4);
225                 break;
226         case EFI_GET_NEXT_HIGH_MONO_COUNT:
227                 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1);
228                 break;
229         case EFI_UPDATE_CAPSULE:
230                 status = efi_call_virt(update_capsule,
231                                        (efi_capsule_header_t **)arg1,
232                                        *(unsigned long *)arg2,
233                                        *(unsigned long *)arg3);
234                 break;
235         case EFI_QUERY_CAPSULE_CAPS:
236                 status = efi_call_virt(query_capsule_caps,
237                                        (efi_capsule_header_t **)arg1,
238                                        *(unsigned long *)arg2, (u64 *)arg3,
239                                        (int *)arg4);
240                 break;
241         default:
242                 /*
243                  * Ideally, we should never reach here because a caller of this
244                  * function should have put the right efi_runtime_service()
245                  * function identifier into efi_rts_work->efi_rts_id
246                  */
247                 pr_err("Requested executing invalid EFI Runtime Service.\n");
248         }
249         efi_rts_work.status = status;
250         complete(&efi_rts_work.efi_rts_comp);
251 }
252
253 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
254 {
255         efi_status_t status;
256
257         if (down_interruptible(&efi_runtime_lock))
258                 return EFI_ABORTED;
259         status = efi_queue_work(EFI_GET_TIME, tm, tc, NULL, NULL, NULL);
260         up(&efi_runtime_lock);
261         return status;
262 }
263
264 static efi_status_t virt_efi_set_time(efi_time_t *tm)
265 {
266         efi_status_t status;
267
268         if (down_interruptible(&efi_runtime_lock))
269                 return EFI_ABORTED;
270         status = efi_queue_work(EFI_SET_TIME, tm, NULL, NULL, NULL, NULL);
271         up(&efi_runtime_lock);
272         return status;
273 }
274
275 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
276                                              efi_bool_t *pending,
277                                              efi_time_t *tm)
278 {
279         efi_status_t status;
280
281         if (down_interruptible(&efi_runtime_lock))
282                 return EFI_ABORTED;
283         status = efi_queue_work(EFI_GET_WAKEUP_TIME, enabled, pending, tm, NULL,
284                                 NULL);
285         up(&efi_runtime_lock);
286         return status;
287 }
288
289 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
290 {
291         efi_status_t status;
292
293         if (down_interruptible(&efi_runtime_lock))
294                 return EFI_ABORTED;
295         status = efi_queue_work(EFI_SET_WAKEUP_TIME, &enabled, tm, NULL, NULL,
296                                 NULL);
297         up(&efi_runtime_lock);
298         return status;
299 }
300
301 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
302                                           efi_guid_t *vendor,
303                                           u32 *attr,
304                                           unsigned long *data_size,
305                                           void *data)
306 {
307         efi_status_t status;
308
309         if (down_interruptible(&efi_runtime_lock))
310                 return EFI_ABORTED;
311         status = efi_queue_work(EFI_GET_VARIABLE, name, vendor, attr, data_size,
312                                 data);
313         up(&efi_runtime_lock);
314         return status;
315 }
316
317 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
318                                                efi_char16_t *name,
319                                                efi_guid_t *vendor)
320 {
321         efi_status_t status;
322
323         if (down_interruptible(&efi_runtime_lock))
324                 return EFI_ABORTED;
325         status = efi_queue_work(EFI_GET_NEXT_VARIABLE, name_size, name, vendor,
326                                 NULL, NULL);
327         up(&efi_runtime_lock);
328         return status;
329 }
330
331 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
332                                           efi_guid_t *vendor,
333                                           u32 attr,
334                                           unsigned long data_size,
335                                           void *data)
336 {
337         efi_status_t status;
338
339         if (down_interruptible(&efi_runtime_lock))
340                 return EFI_ABORTED;
341         status = efi_queue_work(EFI_SET_VARIABLE, name, vendor, &attr, &data_size,
342                                 data);
343         up(&efi_runtime_lock);
344         return status;
345 }
346
347 static efi_status_t
348 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
349                                   u32 attr, unsigned long data_size,
350                                   void *data)
351 {
352         efi_status_t status;
353
354         if (down_trylock(&efi_runtime_lock))
355                 return EFI_NOT_READY;
356
357         status = efi_call_virt(set_variable, name, vendor, attr, data_size,
358                                data);
359         up(&efi_runtime_lock);
360         return status;
361 }
362
363
364 static efi_status_t virt_efi_query_variable_info(u32 attr,
365                                                  u64 *storage_space,
366                                                  u64 *remaining_space,
367                                                  u64 *max_variable_size)
368 {
369         efi_status_t status;
370
371         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
372                 return EFI_UNSUPPORTED;
373
374         if (down_interruptible(&efi_runtime_lock))
375                 return EFI_ABORTED;
376         status = efi_queue_work(EFI_QUERY_VARIABLE_INFO, &attr, storage_space,
377                                 remaining_space, max_variable_size, NULL);
378         up(&efi_runtime_lock);
379         return status;
380 }
381
382 static efi_status_t
383 virt_efi_query_variable_info_nonblocking(u32 attr,
384                                          u64 *storage_space,
385                                          u64 *remaining_space,
386                                          u64 *max_variable_size)
387 {
388         efi_status_t status;
389
390         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
391                 return EFI_UNSUPPORTED;
392
393         if (down_trylock(&efi_runtime_lock))
394                 return EFI_NOT_READY;
395
396         status = efi_call_virt(query_variable_info, attr, storage_space,
397                                remaining_space, max_variable_size);
398         up(&efi_runtime_lock);
399         return status;
400 }
401
402 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
403 {
404         efi_status_t status;
405
406         if (down_interruptible(&efi_runtime_lock))
407                 return EFI_ABORTED;
408         status = efi_queue_work(EFI_GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL,
409                                 NULL, NULL);
410         up(&efi_runtime_lock);
411         return status;
412 }
413
414 static void virt_efi_reset_system(int reset_type,
415                                   efi_status_t status,
416                                   unsigned long data_size,
417                                   efi_char16_t *data)
418 {
419         if (down_trylock(&efi_runtime_lock)) {
420                 pr_warn("failed to invoke the reset_system() runtime service:\n"
421                         "could not get exclusive access to the firmware\n");
422                 return;
423         }
424         efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;
425         __efi_call_virt(reset_system, reset_type, status, data_size, data);
426         up(&efi_runtime_lock);
427 }
428
429 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
430                                             unsigned long count,
431                                             unsigned long sg_list)
432 {
433         efi_status_t status;
434
435         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
436                 return EFI_UNSUPPORTED;
437
438         if (down_interruptible(&efi_runtime_lock))
439                 return EFI_ABORTED;
440         status = efi_queue_work(EFI_UPDATE_CAPSULE, capsules, &count, &sg_list,
441                                 NULL, NULL);
442         up(&efi_runtime_lock);
443         return status;
444 }
445
446 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
447                                                 unsigned long count,
448                                                 u64 *max_size,
449                                                 int *reset_type)
450 {
451         efi_status_t status;
452
453         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
454                 return EFI_UNSUPPORTED;
455
456         if (down_interruptible(&efi_runtime_lock))
457                 return EFI_ABORTED;
458         status = efi_queue_work(EFI_QUERY_CAPSULE_CAPS, capsules, &count,
459                                 max_size, reset_type, NULL);
460         up(&efi_runtime_lock);
461         return status;
462 }
463
464 void efi_native_runtime_setup(void)
465 {
466         efi.get_time = virt_efi_get_time;
467         efi.set_time = virt_efi_set_time;
468         efi.get_wakeup_time = virt_efi_get_wakeup_time;
469         efi.set_wakeup_time = virt_efi_set_wakeup_time;
470         efi.get_variable = virt_efi_get_variable;
471         efi.get_next_variable = virt_efi_get_next_variable;
472         efi.set_variable = virt_efi_set_variable;
473         efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
474         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
475         efi.reset_system = virt_efi_reset_system;
476         efi.query_variable_info = virt_efi_query_variable_info;
477         efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
478         efi.update_capsule = virt_efi_update_capsule;
479         efi.query_capsule_caps = virt_efi_query_capsule_caps;
480 }
This page took 0.058364 seconds and 4 git commands to generate.