blob: 7b8b2f2702cade6056420cabe32530c8ae0849fd [file] [log] [blame]
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +02001/*
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
Ard Biesheuvel161485e2014-08-04 18:16:00 +020017#include <linux/bug.h>
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020018#include <linux/efi.h>
Ard Biesheuvel161485e2014-08-04 18:16:00 +020019#include <linux/mutex.h>
20#include <linux/spinlock.h>
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020021#include <asm/efi.h>
22
23/*
Ard Biesheuvel161485e2014-08-04 18:16:00 +020024 * 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 */
62static DEFINE_SPINLOCK(efi_runtime_lock);
63
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020064static 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
Ard Biesheuvel1bb69362016-02-01 22:07:00 +000069 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020070 status = efi_call_virt(get_time, tm, tc);
Ard Biesheuvel1bb69362016-02-01 22:07:00 +000071 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020072 return status;
73}
74
75static efi_status_t virt_efi_set_time(efi_time_t *tm)
76{
77 unsigned long flags;
78 efi_status_t status;
79
Ard Biesheuvel1bb69362016-02-01 22:07:00 +000080 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020081 status = efi_call_virt(set_time, tm);
Ard Biesheuvel1bb69362016-02-01 22:07:00 +000082 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020083 return status;
84}
85
86static 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
Ard Biesheuvel1bb69362016-02-01 22:07:00 +000093 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020094 status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
Ard Biesheuvel1bb69362016-02-01 22:07:00 +000095 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +020096 return status;
97}
98
99static 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
Ard Biesheuvel1bb69362016-02-01 22:07:00 +0000104 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200105 status = efi_call_virt(set_wakeup_time, enabled, tm);
Ard Biesheuvel1bb69362016-02-01 22:07:00 +0000106 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200107 return status;
108}
109
110static 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{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200116 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;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200124}
125
126static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
127 efi_char16_t *name,
128 efi_guid_t *vendor)
129{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200130 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;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200137}
138
139static 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{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200145 unsigned long flags;
146 efi_status_t status;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200147
Matt Fleming60b4dc72014-09-30 15:03:38 +0100148 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200149 status = efi_call_virt(set_variable, name, vendor, attr, data_size,
150 data);
Matt Fleming60b4dc72014-09-30 15:03:38 +0100151 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200152 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200153}
154
Matt Fleming6d80dba2014-09-30 21:58:52 +0100155static efi_status_t
156virt_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
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200173static efi_status_t virt_efi_query_variable_info(u32 attr,
174 u64 *storage_space,
175 u64 *remaining_space,
176 u64 *max_variable_size)
177{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200178 unsigned long flags;
179 efi_status_t status;
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200180
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200181 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
182 return EFI_UNSUPPORTED;
183
Matt Fleming60b4dc72014-09-30 15:03:38 +0100184 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200185 status = efi_call_virt(query_variable_info, attr, storage_space,
186 remaining_space, max_variable_size);
Matt Fleming60b4dc72014-09-30 15:03:38 +0100187 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200188 return status;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200189}
190
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000191static efi_status_t
192virt_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
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200212static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
213{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200214 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;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200221}
222
223static void virt_efi_reset_system(int reset_type,
224 efi_status_t status,
225 unsigned long data_size,
226 efi_char16_t *data)
227{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200228 unsigned long flags;
229
230 spin_lock_irqsave(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200231 __efi_call_virt(reset_system, reset_type, status, data_size, data);
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200232 spin_unlock_irqrestore(&efi_runtime_lock, flags);
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200233}
234
235static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
236 unsigned long count,
237 unsigned long sg_list)
238{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200239 unsigned long flags;
240 efi_status_t status;
241
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200242 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
243 return EFI_UNSUPPORTED;
244
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200245 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;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200249}
250
251static 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{
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200256 unsigned long flags;
257 efi_status_t status;
258
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200259 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
260 return EFI_UNSUPPORTED;
261
Ard Biesheuvel161485e2014-08-04 18:16:00 +0200262 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;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200267}
268
269void 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;
Matt Fleming6d80dba2014-09-30 21:58:52 +0100278 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200279 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;
Ard Biesheuveld3cac1f2016-02-01 22:06:57 +0000282 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200283 efi.update_capsule = virt_efi_update_capsule;
284 efi.query_capsule_caps = virt_efi_query_capsule_caps;
285}