]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/firmware/efi/runtime-wrappers.c
Merge remote-tracking branch 'usb/usb-next'
[karo-tx-linux.git] / drivers / firmware / efi / runtime-wrappers.c
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
17 #include <linux/bug.h>
18 #include <linux/efi.h>
19 #include <linux/mutex.h>
20 #include <linux/spinlock.h>
21 #include <asm/efi.h>
22
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 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
65 {
66         unsigned long flags;
67         efi_status_t status;
68
69         spin_lock_irqsave(&efi_runtime_lock, flags);
70         status = efi_call_virt(get_time, tm, tc);
71         spin_unlock_irqrestore(&efi_runtime_lock, flags);
72         return status;
73 }
74
75 static efi_status_t virt_efi_set_time(efi_time_t *tm)
76 {
77         unsigned long flags;
78         efi_status_t status;
79
80         spin_lock_irqsave(&efi_runtime_lock, flags);
81         status = efi_call_virt(set_time, tm);
82         spin_unlock_irqrestore(&efi_runtime_lock, flags);
83         return status;
84 }
85
86 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
87                                              efi_bool_t *pending,
88                                              efi_time_t *tm)
89 {
90         unsigned long flags;
91         efi_status_t status;
92
93         spin_lock_irqsave(&efi_runtime_lock, flags);
94         status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
95         spin_unlock_irqrestore(&efi_runtime_lock, flags);
96         return status;
97 }
98
99 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
100 {
101         unsigned long flags;
102         efi_status_t status;
103
104         spin_lock_irqsave(&efi_runtime_lock, flags);
105         status = efi_call_virt(set_wakeup_time, enabled, tm);
106         spin_unlock_irqrestore(&efi_runtime_lock, flags);
107         return status;
108 }
109
110 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
111                                           efi_guid_t *vendor,
112                                           u32 *attr,
113                                           unsigned long *data_size,
114                                           void *data)
115 {
116         unsigned long flags;
117         efi_status_t status;
118
119         spin_lock_irqsave(&efi_runtime_lock, flags);
120         status = efi_call_virt(get_variable, name, vendor, attr, data_size,
121                                data);
122         spin_unlock_irqrestore(&efi_runtime_lock, flags);
123         return status;
124 }
125
126 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
127                                                efi_char16_t *name,
128                                                efi_guid_t *vendor)
129 {
130         unsigned long flags;
131         efi_status_t status;
132
133         spin_lock_irqsave(&efi_runtime_lock, flags);
134         status = efi_call_virt(get_next_variable, name_size, name, vendor);
135         spin_unlock_irqrestore(&efi_runtime_lock, flags);
136         return status;
137 }
138
139 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
140                                           efi_guid_t *vendor,
141                                           u32 attr,
142                                           unsigned long data_size,
143                                           void *data)
144 {
145         unsigned long flags;
146         efi_status_t status;
147
148         spin_lock_irqsave(&efi_runtime_lock, flags);
149         status = efi_call_virt(set_variable, name, vendor, attr, data_size,
150                                data);
151         spin_unlock_irqrestore(&efi_runtime_lock, flags);
152         return status;
153 }
154
155 static efi_status_t
156 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
157                                   u32 attr, unsigned long data_size,
158                                   void *data)
159 {
160         unsigned long flags;
161         efi_status_t status;
162
163         if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
164                 return EFI_NOT_READY;
165
166         status = efi_call_virt(set_variable, name, vendor, attr, data_size,
167                                data);
168         spin_unlock_irqrestore(&efi_runtime_lock, flags);
169         return status;
170 }
171
172
173 static efi_status_t virt_efi_query_variable_info(u32 attr,
174                                                  u64 *storage_space,
175                                                  u64 *remaining_space,
176                                                  u64 *max_variable_size)
177 {
178         unsigned long flags;
179         efi_status_t status;
180
181         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
182                 return EFI_UNSUPPORTED;
183
184         spin_lock_irqsave(&efi_runtime_lock, flags);
185         status = efi_call_virt(query_variable_info, attr, storage_space,
186                                remaining_space, max_variable_size);
187         spin_unlock_irqrestore(&efi_runtime_lock, flags);
188         return status;
189 }
190
191 static efi_status_t
192 virt_efi_query_variable_info_nonblocking(u32 attr,
193                                          u64 *storage_space,
194                                          u64 *remaining_space,
195                                          u64 *max_variable_size)
196 {
197         unsigned long flags;
198         efi_status_t status;
199
200         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
201                 return EFI_UNSUPPORTED;
202
203         if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
204                 return EFI_NOT_READY;
205
206         status = efi_call_virt(query_variable_info, attr, storage_space,
207                                remaining_space, max_variable_size);
208         spin_unlock_irqrestore(&efi_runtime_lock, flags);
209         return status;
210 }
211
212 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
213 {
214         unsigned long flags;
215         efi_status_t status;
216
217         spin_lock_irqsave(&efi_runtime_lock, flags);
218         status = efi_call_virt(get_next_high_mono_count, count);
219         spin_unlock_irqrestore(&efi_runtime_lock, flags);
220         return status;
221 }
222
223 static void virt_efi_reset_system(int reset_type,
224                                   efi_status_t status,
225                                   unsigned long data_size,
226                                   efi_char16_t *data)
227 {
228         unsigned long flags;
229
230         spin_lock_irqsave(&efi_runtime_lock, flags);
231         __efi_call_virt(reset_system, reset_type, status, data_size, data);
232         spin_unlock_irqrestore(&efi_runtime_lock, flags);
233 }
234
235 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
236                                             unsigned long count,
237                                             unsigned long sg_list)
238 {
239         unsigned long flags;
240         efi_status_t status;
241
242         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
243                 return EFI_UNSUPPORTED;
244
245         spin_lock_irqsave(&efi_runtime_lock, flags);
246         status = efi_call_virt(update_capsule, capsules, count, sg_list);
247         spin_unlock_irqrestore(&efi_runtime_lock, flags);
248         return status;
249 }
250
251 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
252                                                 unsigned long count,
253                                                 u64 *max_size,
254                                                 int *reset_type)
255 {
256         unsigned long flags;
257         efi_status_t status;
258
259         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
260                 return EFI_UNSUPPORTED;
261
262         spin_lock_irqsave(&efi_runtime_lock, flags);
263         status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
264                                reset_type);
265         spin_unlock_irqrestore(&efi_runtime_lock, flags);
266         return status;
267 }
268
269 void efi_native_runtime_setup(void)
270 {
271         efi.get_time = virt_efi_get_time;
272         efi.set_time = virt_efi_set_time;
273         efi.get_wakeup_time = virt_efi_get_wakeup_time;
274         efi.set_wakeup_time = virt_efi_set_wakeup_time;
275         efi.get_variable = virt_efi_get_variable;
276         efi.get_next_variable = virt_efi_get_next_variable;
277         efi.set_variable = virt_efi_set_variable;
278         efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
279         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
280         efi.reset_system = virt_efi_reset_system;
281         efi.query_variable_info = virt_efi_query_variable_info;
282         efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
283         efi.update_capsule = virt_efi_update_capsule;
284         efi.query_capsule_caps = virt_efi_query_capsule_caps;
285 }