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