blob: 89d60f8e3c180835fb7e7e9091210bfbf7db7b34 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Daniel Kiperbe81c8a2014-06-30 19:53:02 +02002/*
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>
Julien Gralle371fd72017-04-24 18:58:39 +010030#include <xen/xen-ops.h>
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020031
Daniel Kiper342cd342014-09-08 15:22:18 +020032#include <asm/page.h>
33
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020034#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
Shannon Zhaoa62ed502016-04-07 20:03:31 +080043efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020044{
45 struct xen_platform_op op = INIT_EFI_OP(get_time);
46
Stefano Stabellinicfafae92015-11-23 10:36:12 +000047 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020048 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}
Shannon Zhaoa62ed502016-04-07 20:03:31 +080064EXPORT_SYMBOL_GPL(xen_efi_get_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020065
Shannon Zhaoa62ed502016-04-07 20:03:31 +080066efi_status_t xen_efi_set_time(efi_time_t *tm)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020067{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +000073 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020074 return EFI_UNSUPPORTED;
75
76 return efi_data(op).status;
77}
Shannon Zhaoa62ed502016-04-07 20:03:31 +080078EXPORT_SYMBOL_GPL(xen_efi_set_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020079
Shannon Zhaoa62ed502016-04-07 20:03:31 +080080efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
81 efi_time_t *tm)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020082{
83 struct xen_platform_op op = INIT_EFI_OP(get_wakeup_time);
84
Stefano Stabellinicfafae92015-11-23 10:36:12 +000085 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020086 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}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800101EXPORT_SYMBOL_GPL(xen_efi_get_wakeup_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200102
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800103efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200104{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000115 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200116 return EFI_UNSUPPORTED;
117
118 return efi_data(op).status;
119}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800120EXPORT_SYMBOL_GPL(xen_efi_set_wakeup_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200121
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800122efi_status_t xen_efi_get_variable(efi_char16_t *name, efi_guid_t *vendor,
123 u32 *attr, unsigned long *data_size,
124 void *data)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200125{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000135 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200136 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}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800144EXPORT_SYMBOL_GPL(xen_efi_get_variable);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200145
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800146efi_status_t xen_efi_get_next_variable(unsigned long *name_size,
147 efi_char16_t *name,
148 efi_guid_t *vendor)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200149{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000159 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200160 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}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800168EXPORT_SYMBOL_GPL(xen_efi_get_next_variable);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200169
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800170efi_status_t xen_efi_set_variable(efi_char16_t *name, efi_guid_t *vendor,
171 u32 attr, unsigned long data_size,
172 void *data)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200173{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000184 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200185 return EFI_UNSUPPORTED;
186
187 return efi_data(op).status;
188}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800189EXPORT_SYMBOL_GPL(xen_efi_set_variable);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200190
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800191efi_status_t xen_efi_query_variable_info(u32 attr, u64 *storage_space,
192 u64 *remaining_space,
193 u64 *max_variable_size)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200194{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000202 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200203 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}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800211EXPORT_SYMBOL_GPL(xen_efi_query_variable_info);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200212
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800213efi_status_t xen_efi_get_next_high_mono_count(u32 *count)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200214{
215 struct xen_platform_op op = INIT_EFI_OP(get_next_high_monotonic_count);
216
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000217 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200218 return EFI_UNSUPPORTED;
219
220 *count = efi_data(op).misc;
221
222 return efi_data(op).status;
223}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800224EXPORT_SYMBOL_GPL(xen_efi_get_next_high_mono_count);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200225
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800226efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules,
227 unsigned long count, unsigned long sg_list)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200228{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000239 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200240 return EFI_UNSUPPORTED;
241
242 return efi_data(op).status;
243}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800244EXPORT_SYMBOL_GPL(xen_efi_update_capsule);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200245
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800246efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules,
247 unsigned long count, u64 *max_size,
248 int *reset_type)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200249{
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
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000259 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200260 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}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800267EXPORT_SYMBOL_GPL(xen_efi_query_capsule_caps);
Julien Gralle371fd72017-04-24 18:58:39 +0100268
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);