blob: 9243a9051078229172daea01e0d621ac089ac050 [file] [log] [blame]
Daniel Kiperbe81c8a2014-06-30 19:53:02 +02001/*
2 * EFI support for Xen.
3 *
4 * Copyright (C) 1999 VA Linux Systems
5 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
6 * Copyright (C) 1999-2002 Hewlett-Packard Co.
7 * David Mosberger-Tang <davidm@hpl.hp.com>
8 * Stephane Eranian <eranian@hpl.hp.com>
9 * Copyright (C) 2005-2008 Intel Co.
10 * Fenghua Yu <fenghua.yu@intel.com>
11 * Bibo Mao <bibo.mao@intel.com>
12 * Chandramouli Narayanan <mouli@linux.intel.com>
13 * Huang Ying <ying.huang@intel.com>
14 * Copyright (C) 2011 Novell Co.
15 * Jan Beulich <JBeulich@suse.com>
16 * Copyright (C) 2011-2012 Oracle Co.
17 * Liang Tang <liang.tang@oracle.com>
18 * Copyright (c) 2014 Oracle Co., Daniel Kiper
19 */
20
21#include <linux/bug.h>
22#include <linux/efi.h>
23#include <linux/init.h>
24#include <linux/string.h>
25
26#include <xen/interface/xen.h>
27#include <xen/interface/platform.h>
28#include <xen/xen.h>
Julien Gralle371fd72017-04-24 18:58:39 +010029#include <xen/xen-ops.h>
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020030
Daniel Kiper342cd342014-09-08 15:22:18 +020031#include <asm/page.h>
32
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020033#include <asm/xen/hypercall.h>
34
35#define INIT_EFI_OP(name) \
36 {.cmd = XENPF_efi_runtime_call, \
37 .u.efi_runtime_call.function = XEN_EFI_##name, \
38 .u.efi_runtime_call.misc = 0}
39
40#define efi_data(op) (op.u.efi_runtime_call)
41
Shannon Zhaoa62ed502016-04-07 20:03:31 +080042efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020043{
44 struct xen_platform_op op = INIT_EFI_OP(get_time);
45
Stefano Stabellinicfafae92015-11-23 10:36:12 +000046 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020047 return EFI_UNSUPPORTED;
48
49 if (tm) {
50 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_time.time));
51 memcpy(tm, &efi_data(op).u.get_time.time, sizeof(*tm));
52 }
53
54 if (tc) {
55 tc->resolution = efi_data(op).u.get_time.resolution;
56 tc->accuracy = efi_data(op).u.get_time.accuracy;
57 tc->sets_to_zero = !!(efi_data(op).misc &
58 XEN_EFI_GET_TIME_SET_CLEARS_NS);
59 }
60
61 return efi_data(op).status;
62}
Shannon Zhaoa62ed502016-04-07 20:03:31 +080063EXPORT_SYMBOL_GPL(xen_efi_get_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020064
Shannon Zhaoa62ed502016-04-07 20:03:31 +080065efi_status_t xen_efi_set_time(efi_time_t *tm)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020066{
67 struct xen_platform_op op = INIT_EFI_OP(set_time);
68
69 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_time));
70 memcpy(&efi_data(op).u.set_time, tm, sizeof(*tm));
71
Stefano Stabellinicfafae92015-11-23 10:36:12 +000072 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020073 return EFI_UNSUPPORTED;
74
75 return efi_data(op).status;
76}
Shannon Zhaoa62ed502016-04-07 20:03:31 +080077EXPORT_SYMBOL_GPL(xen_efi_set_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020078
Shannon Zhaoa62ed502016-04-07 20:03:31 +080079efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
80 efi_time_t *tm)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020081{
82 struct xen_platform_op op = INIT_EFI_OP(get_wakeup_time);
83
Stefano Stabellinicfafae92015-11-23 10:36:12 +000084 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +020085 return EFI_UNSUPPORTED;
86
87 if (tm) {
88 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_wakeup_time));
89 memcpy(tm, &efi_data(op).u.get_wakeup_time, sizeof(*tm));
90 }
91
92 if (enabled)
93 *enabled = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_ENABLED);
94
95 if (pending)
96 *pending = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_PENDING);
97
98 return efi_data(op).status;
99}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800100EXPORT_SYMBOL_GPL(xen_efi_get_wakeup_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200101
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800102efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200103{
104 struct xen_platform_op op = INIT_EFI_OP(set_wakeup_time);
105
106 BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_wakeup_time));
107 if (enabled)
108 efi_data(op).misc = XEN_EFI_SET_WAKEUP_TIME_ENABLE;
109 if (tm)
110 memcpy(&efi_data(op).u.set_wakeup_time, tm, sizeof(*tm));
111 else
112 efi_data(op).misc |= XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY;
113
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000114 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200115 return EFI_UNSUPPORTED;
116
117 return efi_data(op).status;
118}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800119EXPORT_SYMBOL_GPL(xen_efi_set_wakeup_time);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200120
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800121efi_status_t xen_efi_get_variable(efi_char16_t *name, efi_guid_t *vendor,
122 u32 *attr, unsigned long *data_size,
123 void *data)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200124{
125 struct xen_platform_op op = INIT_EFI_OP(get_variable);
126
127 set_xen_guest_handle(efi_data(op).u.get_variable.name, name);
128 BUILD_BUG_ON(sizeof(*vendor) !=
129 sizeof(efi_data(op).u.get_variable.vendor_guid));
130 memcpy(&efi_data(op).u.get_variable.vendor_guid, vendor, sizeof(*vendor));
131 efi_data(op).u.get_variable.size = *data_size;
132 set_xen_guest_handle(efi_data(op).u.get_variable.data, data);
133
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000134 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200135 return EFI_UNSUPPORTED;
136
137 *data_size = efi_data(op).u.get_variable.size;
138 if (attr)
139 *attr = efi_data(op).misc;
140
141 return efi_data(op).status;
142}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800143EXPORT_SYMBOL_GPL(xen_efi_get_variable);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200144
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800145efi_status_t xen_efi_get_next_variable(unsigned long *name_size,
146 efi_char16_t *name,
147 efi_guid_t *vendor)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200148{
149 struct xen_platform_op op = INIT_EFI_OP(get_next_variable_name);
150
151 efi_data(op).u.get_next_variable_name.size = *name_size;
152 set_xen_guest_handle(efi_data(op).u.get_next_variable_name.name, name);
153 BUILD_BUG_ON(sizeof(*vendor) !=
154 sizeof(efi_data(op).u.get_next_variable_name.vendor_guid));
155 memcpy(&efi_data(op).u.get_next_variable_name.vendor_guid, vendor,
156 sizeof(*vendor));
157
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000158 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200159 return EFI_UNSUPPORTED;
160
161 *name_size = efi_data(op).u.get_next_variable_name.size;
162 memcpy(vendor, &efi_data(op).u.get_next_variable_name.vendor_guid,
163 sizeof(*vendor));
164
165 return efi_data(op).status;
166}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800167EXPORT_SYMBOL_GPL(xen_efi_get_next_variable);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200168
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800169efi_status_t xen_efi_set_variable(efi_char16_t *name, efi_guid_t *vendor,
170 u32 attr, unsigned long data_size,
171 void *data)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200172{
173 struct xen_platform_op op = INIT_EFI_OP(set_variable);
174
175 set_xen_guest_handle(efi_data(op).u.set_variable.name, name);
176 efi_data(op).misc = attr;
177 BUILD_BUG_ON(sizeof(*vendor) !=
178 sizeof(efi_data(op).u.set_variable.vendor_guid));
179 memcpy(&efi_data(op).u.set_variable.vendor_guid, vendor, sizeof(*vendor));
180 efi_data(op).u.set_variable.size = data_size;
181 set_xen_guest_handle(efi_data(op).u.set_variable.data, data);
182
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000183 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200184 return EFI_UNSUPPORTED;
185
186 return efi_data(op).status;
187}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800188EXPORT_SYMBOL_GPL(xen_efi_set_variable);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200189
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800190efi_status_t xen_efi_query_variable_info(u32 attr, u64 *storage_space,
191 u64 *remaining_space,
192 u64 *max_variable_size)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200193{
194 struct xen_platform_op op = INIT_EFI_OP(query_variable_info);
195
196 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
197 return EFI_UNSUPPORTED;
198
199 efi_data(op).u.query_variable_info.attr = attr;
200
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000201 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200202 return EFI_UNSUPPORTED;
203
204 *storage_space = efi_data(op).u.query_variable_info.max_store_size;
205 *remaining_space = efi_data(op).u.query_variable_info.remain_store_size;
206 *max_variable_size = efi_data(op).u.query_variable_info.max_size;
207
208 return efi_data(op).status;
209}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800210EXPORT_SYMBOL_GPL(xen_efi_query_variable_info);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200211
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800212efi_status_t xen_efi_get_next_high_mono_count(u32 *count)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200213{
214 struct xen_platform_op op = INIT_EFI_OP(get_next_high_monotonic_count);
215
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000216 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200217 return EFI_UNSUPPORTED;
218
219 *count = efi_data(op).misc;
220
221 return efi_data(op).status;
222}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800223EXPORT_SYMBOL_GPL(xen_efi_get_next_high_mono_count);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200224
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800225efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules,
226 unsigned long count, unsigned long sg_list)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200227{
228 struct xen_platform_op op = INIT_EFI_OP(update_capsule);
229
230 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
231 return EFI_UNSUPPORTED;
232
233 set_xen_guest_handle(efi_data(op).u.update_capsule.capsule_header_array,
234 capsules);
235 efi_data(op).u.update_capsule.capsule_count = count;
236 efi_data(op).u.update_capsule.sg_list = sg_list;
237
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000238 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200239 return EFI_UNSUPPORTED;
240
241 return efi_data(op).status;
242}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800243EXPORT_SYMBOL_GPL(xen_efi_update_capsule);
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200244
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800245efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules,
246 unsigned long count, u64 *max_size,
247 int *reset_type)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200248{
249 struct xen_platform_op op = INIT_EFI_OP(query_capsule_capabilities);
250
251 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
252 return EFI_UNSUPPORTED;
253
254 set_xen_guest_handle(efi_data(op).u.query_capsule_capabilities.capsule_header_array,
255 capsules);
256 efi_data(op).u.query_capsule_capabilities.capsule_count = count;
257
Stefano Stabellinicfafae92015-11-23 10:36:12 +0000258 if (HYPERVISOR_platform_op(&op) < 0)
Daniel Kiperbe81c8a2014-06-30 19:53:02 +0200259 return EFI_UNSUPPORTED;
260
261 *max_size = efi_data(op).u.query_capsule_capabilities.max_capsule_size;
262 *reset_type = efi_data(op).u.query_capsule_capabilities.reset_type;
263
264 return efi_data(op).status;
265}
Shannon Zhaoa62ed502016-04-07 20:03:31 +0800266EXPORT_SYMBOL_GPL(xen_efi_query_capsule_caps);
Julien Gralle371fd72017-04-24 18:58:39 +0100267
268void xen_efi_reset_system(int reset_type, efi_status_t status,
269 unsigned long data_size, efi_char16_t *data)
270{
271 switch (reset_type) {
272 case EFI_RESET_COLD:
273 case EFI_RESET_WARM:
274 xen_reboot(SHUTDOWN_reboot);
275 break;
276 case EFI_RESET_SHUTDOWN:
277 xen_reboot(SHUTDOWN_poweroff);
278 break;
279 default:
280 BUG();
281 }
282}
283EXPORT_SYMBOL_GPL(xen_efi_reset_system);