Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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> |
Julien Grall | e371fd7 | 2017-04-24 18:58:39 +0100 | [diff] [blame] | 30 | #include <xen/xen-ops.h> |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 31 | |
Daniel Kiper | 342cd34 | 2014-09-08 15:22:18 +0200 | [diff] [blame] | 32 | #include <asm/page.h> |
| 33 | |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 43 | efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 44 | { |
| 45 | struct xen_platform_op op = INIT_EFI_OP(get_time); |
| 46 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 47 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(xen_efi_get_time); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 65 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 66 | efi_status_t xen_efi_set_time(efi_time_t *tm) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 73 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 74 | return EFI_UNSUPPORTED; |
| 75 | |
| 76 | return efi_data(op).status; |
| 77 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(xen_efi_set_time); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 79 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 80 | efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending, |
| 81 | efi_time_t *tm) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 82 | { |
| 83 | struct xen_platform_op op = INIT_EFI_OP(get_wakeup_time); |
| 84 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 85 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(xen_efi_get_wakeup_time); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 102 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 103 | efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 115 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 116 | return EFI_UNSUPPORTED; |
| 117 | |
| 118 | return efi_data(op).status; |
| 119 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 120 | EXPORT_SYMBOL_GPL(xen_efi_set_wakeup_time); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 121 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 122 | efi_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 Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 135 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 144 | EXPORT_SYMBOL_GPL(xen_efi_get_variable); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 145 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 146 | efi_status_t xen_efi_get_next_variable(unsigned long *name_size, |
| 147 | efi_char16_t *name, |
| 148 | efi_guid_t *vendor) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 159 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(xen_efi_get_next_variable); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 169 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 170 | efi_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 Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 184 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 185 | return EFI_UNSUPPORTED; |
| 186 | |
| 187 | return efi_data(op).status; |
| 188 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 189 | EXPORT_SYMBOL_GPL(xen_efi_set_variable); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 190 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 191 | efi_status_t xen_efi_query_variable_info(u32 attr, u64 *storage_space, |
| 192 | u64 *remaining_space, |
| 193 | u64 *max_variable_size) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 202 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 211 | EXPORT_SYMBOL_GPL(xen_efi_query_variable_info); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 212 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 213 | efi_status_t xen_efi_get_next_high_mono_count(u32 *count) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 214 | { |
| 215 | struct xen_platform_op op = INIT_EFI_OP(get_next_high_monotonic_count); |
| 216 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 217 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 218 | return EFI_UNSUPPORTED; |
| 219 | |
| 220 | *count = efi_data(op).misc; |
| 221 | |
| 222 | return efi_data(op).status; |
| 223 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 224 | EXPORT_SYMBOL_GPL(xen_efi_get_next_high_mono_count); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 225 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 226 | efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules, |
| 227 | unsigned long count, unsigned long sg_list) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 239 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 240 | return EFI_UNSUPPORTED; |
| 241 | |
| 242 | return efi_data(op).status; |
| 243 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(xen_efi_update_capsule); |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 245 | |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 246 | efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules, |
| 247 | unsigned long count, u64 *max_size, |
| 248 | int *reset_type) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | |
Stefano Stabellini | cfafae9 | 2015-11-23 10:36:12 +0000 | [diff] [blame] | 259 | if (HYPERVISOR_platform_op(&op) < 0) |
Daniel Kiper | be81c8a | 2014-06-30 19:53:02 +0200 | [diff] [blame] | 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 | } |
Shannon Zhao | a62ed50 | 2016-04-07 20:03:31 +0800 | [diff] [blame] | 267 | EXPORT_SYMBOL_GPL(xen_efi_query_capsule_caps); |
Julien Grall | e371fd7 | 2017-04-24 18:58:39 +0100 | [diff] [blame] | 268 | |
| 269 | void 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 | } |
| 284 | EXPORT_SYMBOL_GPL(xen_efi_reset_system); |