treewide: Add SPDX license identifier for missed files
[linux-block.git] / drivers / xen / efi.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
be81c8a1
DK
2/*
3 * EFI support for Xen.
4 *
5 * Copyright (C) 1999 VA Linux Systems
6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7 * Copyright (C) 1999-2002 Hewlett-Packard Co.
8 * David Mosberger-Tang <davidm@hpl.hp.com>
9 * Stephane Eranian <eranian@hpl.hp.com>
10 * Copyright (C) 2005-2008 Intel Co.
11 * Fenghua Yu <fenghua.yu@intel.com>
12 * Bibo Mao <bibo.mao@intel.com>
13 * Chandramouli Narayanan <mouli@linux.intel.com>
14 * Huang Ying <ying.huang@intel.com>
15 * Copyright (C) 2011 Novell Co.
16 * Jan Beulich <JBeulich@suse.com>
17 * Copyright (C) 2011-2012 Oracle Co.
18 * Liang Tang <liang.tang@oracle.com>
19 * Copyright (c) 2014 Oracle Co., Daniel Kiper
20 */
21
22#include <linux/bug.h>
23#include <linux/efi.h>
24#include <linux/init.h>
25#include <linux/string.h>
26
27#include <xen/interface/xen.h>
28#include <xen/interface/platform.h>
29#include <xen/xen.h>
e371fd76 30#include <xen/xen-ops.h>
be81c8a1 31
342cd340
DK
32#include <asm/page.h>
33
be81c8a1
DK
34#include <asm/xen/hypercall.h>
35
36#define INIT_EFI_OP(name) \
37 {.cmd = XENPF_efi_runtime_call, \
38 .u.efi_runtime_call.function = XEN_EFI_##name, \
39 .u.efi_runtime_call.misc = 0}
40
41#define efi_data(op) (op.u.efi_runtime_call)
42
a62ed500 43efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
be81c8a1
DK
44{
45 struct xen_platform_op op = INIT_EFI_OP(get_time);
46
cfafae94 47 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
48 return EFI_UNSUPPORTED;
49
50 if (tm) {
51 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_time.time));
52 memcpy(tm, &efi_data(op).u.get_time.time, sizeof(*tm));
53 }
54
55 if (tc) {
56 tc->resolution = efi_data(op).u.get_time.resolution;
57 tc->accuracy = efi_data(op).u.get_time.accuracy;
58 tc->sets_to_zero = !!(efi_data(op).misc &
59 XEN_EFI_GET_TIME_SET_CLEARS_NS);
60 }
61
62 return efi_data(op).status;
63}
a62ed500 64EXPORT_SYMBOL_GPL(xen_efi_get_time);
be81c8a1 65
a62ed500 66efi_status_t xen_efi_set_time(efi_time_t *tm)
be81c8a1
DK
67{
68 struct xen_platform_op op = INIT_EFI_OP(set_time);
69
70 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_time));
71 memcpy(&efi_data(op).u.set_time, tm, sizeof(*tm));
72
cfafae94 73 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
74 return EFI_UNSUPPORTED;
75
76 return efi_data(op).status;
77}
a62ed500 78EXPORT_SYMBOL_GPL(xen_efi_set_time);
be81c8a1 79
a62ed500
SZ
80efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
81 efi_time_t *tm)
be81c8a1
DK
82{
83 struct xen_platform_op op = INIT_EFI_OP(get_wakeup_time);
84
cfafae94 85 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
86 return EFI_UNSUPPORTED;
87
88 if (tm) {
89 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_wakeup_time));
90 memcpy(tm, &efi_data(op).u.get_wakeup_time, sizeof(*tm));
91 }
92
93 if (enabled)
94 *enabled = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_ENABLED);
95
96 if (pending)
97 *pending = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_PENDING);
98
99 return efi_data(op).status;
100}
a62ed500 101EXPORT_SYMBOL_GPL(xen_efi_get_wakeup_time);
be81c8a1 102
a62ed500 103efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
be81c8a1
DK
104{
105 struct xen_platform_op op = INIT_EFI_OP(set_wakeup_time);
106
107 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_wakeup_time));
108 if (enabled)
109 efi_data(op).misc = XEN_EFI_SET_WAKEUP_TIME_ENABLE;
110 if (tm)
111 memcpy(&efi_data(op).u.set_wakeup_time, tm, sizeof(*tm));
112 else
113 efi_data(op).misc |= XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY;
114
cfafae94 115 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
116 return EFI_UNSUPPORTED;
117
118 return efi_data(op).status;
119}
a62ed500 120EXPORT_SYMBOL_GPL(xen_efi_set_wakeup_time);
be81c8a1 121
a62ed500
SZ
122efi_status_t xen_efi_get_variable(efi_char16_t *name, efi_guid_t *vendor,
123 u32 *attr, unsigned long *data_size,
124 void *data)
be81c8a1
DK
125{
126 struct xen_platform_op op = INIT_EFI_OP(get_variable);
127
128 set_xen_guest_handle(efi_data(op).u.get_variable.name, name);
129 BUILD_BUG_ON(sizeof(*vendor) !=
130 sizeof(efi_data(op).u.get_variable.vendor_guid));
131 memcpy(&efi_data(op).u.get_variable.vendor_guid, vendor, sizeof(*vendor));
132 efi_data(op).u.get_variable.size = *data_size;
133 set_xen_guest_handle(efi_data(op).u.get_variable.data, data);
134
cfafae94 135 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
136 return EFI_UNSUPPORTED;
137
138 *data_size = efi_data(op).u.get_variable.size;
139 if (attr)
140 *attr = efi_data(op).misc;
141
142 return efi_data(op).status;
143}
a62ed500 144EXPORT_SYMBOL_GPL(xen_efi_get_variable);
be81c8a1 145
a62ed500
SZ
146efi_status_t xen_efi_get_next_variable(unsigned long *name_size,
147 efi_char16_t *name,
148 efi_guid_t *vendor)
be81c8a1
DK
149{
150 struct xen_platform_op op = INIT_EFI_OP(get_next_variable_name);
151
152 efi_data(op).u.get_next_variable_name.size = *name_size;
153 set_xen_guest_handle(efi_data(op).u.get_next_variable_name.name, name);
154 BUILD_BUG_ON(sizeof(*vendor) !=
155 sizeof(efi_data(op).u.get_next_variable_name.vendor_guid));
156 memcpy(&efi_data(op).u.get_next_variable_name.vendor_guid, vendor,
157 sizeof(*vendor));
158
cfafae94 159 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
160 return EFI_UNSUPPORTED;
161
162 *name_size = efi_data(op).u.get_next_variable_name.size;
163 memcpy(vendor, &efi_data(op).u.get_next_variable_name.vendor_guid,
164 sizeof(*vendor));
165
166 return efi_data(op).status;
167}
a62ed500 168EXPORT_SYMBOL_GPL(xen_efi_get_next_variable);
be81c8a1 169
a62ed500
SZ
170efi_status_t xen_efi_set_variable(efi_char16_t *name, efi_guid_t *vendor,
171 u32 attr, unsigned long data_size,
172 void *data)
be81c8a1
DK
173{
174 struct xen_platform_op op = INIT_EFI_OP(set_variable);
175
176 set_xen_guest_handle(efi_data(op).u.set_variable.name, name);
177 efi_data(op).misc = attr;
178 BUILD_BUG_ON(sizeof(*vendor) !=
179 sizeof(efi_data(op).u.set_variable.vendor_guid));
180 memcpy(&efi_data(op).u.set_variable.vendor_guid, vendor, sizeof(*vendor));
181 efi_data(op).u.set_variable.size = data_size;
182 set_xen_guest_handle(efi_data(op).u.set_variable.data, data);
183
cfafae94 184 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
185 return EFI_UNSUPPORTED;
186
187 return efi_data(op).status;
188}
a62ed500 189EXPORT_SYMBOL_GPL(xen_efi_set_variable);
be81c8a1 190
a62ed500
SZ
191efi_status_t xen_efi_query_variable_info(u32 attr, u64 *storage_space,
192 u64 *remaining_space,
193 u64 *max_variable_size)
be81c8a1
DK
194{
195 struct xen_platform_op op = INIT_EFI_OP(query_variable_info);
196
197 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
198 return EFI_UNSUPPORTED;
199
200 efi_data(op).u.query_variable_info.attr = attr;
201
cfafae94 202 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
203 return EFI_UNSUPPORTED;
204
205 *storage_space = efi_data(op).u.query_variable_info.max_store_size;
206 *remaining_space = efi_data(op).u.query_variable_info.remain_store_size;
207 *max_variable_size = efi_data(op).u.query_variable_info.max_size;
208
209 return efi_data(op).status;
210}
a62ed500 211EXPORT_SYMBOL_GPL(xen_efi_query_variable_info);
be81c8a1 212
a62ed500 213efi_status_t xen_efi_get_next_high_mono_count(u32 *count)
be81c8a1
DK
214{
215 struct xen_platform_op op = INIT_EFI_OP(get_next_high_monotonic_count);
216
cfafae94 217 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
218 return EFI_UNSUPPORTED;
219
220 *count = efi_data(op).misc;
221
222 return efi_data(op).status;
223}
a62ed500 224EXPORT_SYMBOL_GPL(xen_efi_get_next_high_mono_count);
be81c8a1 225
a62ed500
SZ
226efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules,
227 unsigned long count, unsigned long sg_list)
be81c8a1
DK
228{
229 struct xen_platform_op op = INIT_EFI_OP(update_capsule);
230
231 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
232 return EFI_UNSUPPORTED;
233
234 set_xen_guest_handle(efi_data(op).u.update_capsule.capsule_header_array,
235 capsules);
236 efi_data(op).u.update_capsule.capsule_count = count;
237 efi_data(op).u.update_capsule.sg_list = sg_list;
238
cfafae94 239 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
240 return EFI_UNSUPPORTED;
241
242 return efi_data(op).status;
243}
a62ed500 244EXPORT_SYMBOL_GPL(xen_efi_update_capsule);
be81c8a1 245
a62ed500
SZ
246efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules,
247 unsigned long count, u64 *max_size,
248 int *reset_type)
be81c8a1
DK
249{
250 struct xen_platform_op op = INIT_EFI_OP(query_capsule_capabilities);
251
252 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
253 return EFI_UNSUPPORTED;
254
255 set_xen_guest_handle(efi_data(op).u.query_capsule_capabilities.capsule_header_array,
256 capsules);
257 efi_data(op).u.query_capsule_capabilities.capsule_count = count;
258
cfafae94 259 if (HYPERVISOR_platform_op(&op) < 0)
be81c8a1
DK
260 return EFI_UNSUPPORTED;
261
262 *max_size = efi_data(op).u.query_capsule_capabilities.max_capsule_size;
263 *reset_type = efi_data(op).u.query_capsule_capabilities.reset_type;
264
265 return efi_data(op).status;
266}
a62ed500 267EXPORT_SYMBOL_GPL(xen_efi_query_capsule_caps);
e371fd76
JG
268
269void xen_efi_reset_system(int reset_type, efi_status_t status,
270 unsigned long data_size, efi_char16_t *data)
271{
272 switch (reset_type) {
273 case EFI_RESET_COLD:
274 case EFI_RESET_WARM:
275 xen_reboot(SHUTDOWN_reboot);
276 break;
277 case EFI_RESET_SHUTDOWN:
278 xen_reboot(SHUTDOWN_poweroff);
279 break;
280 default:
281 BUG();
282 }
283}
284EXPORT_SYMBOL_GPL(xen_efi_reset_system);