Blob Blame History Raw
Subject: support booting Xen from EFI
From: jbeulich@novell.com
Patch-mainline: n/a
References: fate#311376, fate#311529, bnc#578927, bnc#628554

--- head.orig/arch/x86/Kconfig	2014-01-07 17:30:52.000000000 +0100
+++ head/arch/x86/Kconfig	2014-01-07 17:30:58.000000000 +0100
@@ -1571,7 +1571,7 @@ config X86_SMAP
 
 config EFI
 	bool "EFI runtime service support"
-	depends on ACPI && !XEN
+	depends on ACPI && !XEN_UNPRIVILEGED_GUEST
 	select UCS2_STRING
 	---help---
 	  This enables the kernel to use EFI runtime services that are
@@ -1586,7 +1586,7 @@ config EFI
 
 config EFI_STUB
        bool "EFI stub support"
-       depends on EFI
+       depends on EFI && !XEN
        select RELOCATABLE
        ---help---
           This kernel feature allows a bzImage to be loaded directly
--- head.orig/arch/x86/include/mach-xen/asm/setup.h	2011-02-01 14:54:13.000000000 +0100
+++ head/arch/x86/include/mach-xen/asm/setup.h	2011-07-04 12:32:43.000000000 +0200
@@ -3,6 +3,12 @@
 void xen_start_kernel(void);
 void xen_arch_setup(void);
 
+#ifdef CONFIG_EFI
+void efi_probe(void);
+#else
+#define efi_probe() ((void)0)
+#endif
+
 #endif
 
 #include_next <asm/setup.h>
--- head.orig/arch/x86/kernel/rtc.c	2013-12-02 17:19:30.000000000 +0100
+++ head/arch/x86/kernel/rtc.c	2013-08-12 17:57:09.000000000 +0200
@@ -7,6 +7,7 @@
 #include <linux/bcd.h>
 #include <linux/export.h>
 #include <linux/pnp.h>
+#include <linux/efi.h>
 #include <linux/of.h>
 
 #include <asm/vsyscall.h>
@@ -191,6 +192,12 @@ static __init int add_rtc_cmos(void)
 	if (of_have_populated_dt())
 		return 0;
 
+#ifdef CONFIG_XEN
+	/* EFI-based systems should not access CMOS directly. */
+	if (efi_enabled(EFI_RUNTIME_SERVICES))
+		return -ENODEV;
+#endif
+
 	/* Intel MID platforms don't have ioport rtc */
 	if (intel_mid_identify_cpu())
 		return -ENODEV;
--- head.orig/arch/x86/kernel/setup-xen.c	2013-12-11 11:21:49.000000000 +0100
+++ head/arch/x86/kernel/setup-xen.c	2013-12-11 11:25:16.000000000 +0100
@@ -1105,6 +1105,8 @@ void __init setup_arch(char **cmdline_p)
 		                      xen_start_info->console.dom0.info_size);
 		xen_start_info->console.domU.mfn = 0;
 		xen_start_info->console.domU.evtchn = 0;
+
+		efi_probe();
 	} else
 		screen_info.orig_video_isVGA = 0;
 	copy_edid();
--- head.orig/arch/x86/kernel/time-xen.c	2014-01-28 16:22:52.000000000 +0100
+++ head/arch/x86/kernel/time-xen.c	2014-01-28 16:22:57.000000000 +0100
@@ -19,6 +19,7 @@
 #include <linux/posix-timers.h>
 #include <linux/cpufreq.h>
 #include <linux/clocksource.h>
+#include <linux/efi.h>
 
 #include <asm/vsyscall.h>
 #include <asm/delay.h>
@@ -327,6 +328,9 @@ int xen_write_wallclock(const struct tim
 	mod_timer(&sync_xen_wallclock_timer, jiffies + 1);
 #endif
 
+	if (efi_enabled(EFI_RUNTIME_SERVICES))
+		return efi_set_rtc_mmss(now);
+
 	return mach_set_rtc_mmss(now);
 }
 
--- head.orig/arch/x86/platform/efi/Makefile	2014-06-08 20:19:54.000000000 +0200
+++ head/arch/x86/platform/efi/Makefile	2014-05-02 16:30:28.000000000 +0200
@@ -2,3 +2,5 @@ obj-$(CONFIG_EFI) 		+= efi.o efi_$(BITS)
 obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o
 obj-$(CONFIG_EARLY_PRINTK_EFI)	+= early_printk.o
 obj-$(CONFIG_EFI_MIXED)		+= efi_thunk_$(BITS).o
+ccflags-$(CONFIG_XEN)		+= -fshort-wchar
+disabled-obj-$(CONFIG_XEN)	:= efi_%$(BITS).o
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ head/arch/x86/platform/efi/efi-xen.c	2014-04-30 11:55:13.000000000 +0200
@@ -0,0 +1,602 @@
+/*
+ * Common EFI (Extensible Firmware Interface) support functions
+ * Based on Extensible Firmware Interface Specification version 1.0
+ *
+ * Copyright (C) 1999 VA Linux Systems
+ * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
+ * Copyright (C) 1999-2002 Hewlett-Packard Co.
+ *	David Mosberger-Tang <davidm@hpl.hp.com>
+ *	Stephane Eranian <eranian@hpl.hp.com>
+ * Copyright (C) 2005-2008 Intel Co.
+ *	Fenghua Yu <fenghua.yu@intel.com>
+ *	Bibo Mao <bibo.mao@intel.com>
+ *	Chandramouli Narayanan <mouli@linux.intel.com>
+ *	Huang Ying <ying.huang@intel.com>
+ * Copyright (C) 2013 SuSE Labs
+ *	Borislav Petkov <bp@suse.de> - runtime services VA mapping
+ *
+ * Copied from efi_32.c to eliminate the duplicated code between EFI
+ * 32/64 support code. --ying 2007-10-26
+ *
+ * All EFI Runtime Services are not implemented yet as EFI only
+ * supports physical mode addressing on SoftSDV. This is to be fixed
+ * in a future version.  --drummond 1999-07-20
+ *
+ * Implemented EFI runtime services and virtual mode calls.  --davidm
+ *
+ * Goutham Rao: <goutham.rao@intel.com>
+ *	Skip non-WB memory and ignore empty memory ranges.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/efi.h>
+#include <linux/efi-bgrt.h>
+#include <linux/export.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/time.h>
+
+#include <asm/setup.h>
+#include <asm/efi.h>
+#include <asm/time.h>
+#include <asm/cacheflush.h>
+#include <asm/tlbflush.h>
+#include <asm/x86_init.h>
+#include <asm/rtc.h>
+
+#include <xen/interface/platform.h>
+
+#define EFI_DEBUG
+
+#define EFI_MIN_RESERVE 5120
+
+#define EFI_DUMMY_GUID \
+	EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
+
+static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
+
+static efi_config_table_type_t arch_tables[] __initdata = {
+#ifdef CONFIG_X86_UV
+	{UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
+#endif
+	{NULL_GUID, NULL, NULL},
+};
+
+static int __init setup_noefi(char *arg)
+{
+	__clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
+	return 0;
+}
+early_param("noefi", setup_noefi);
+
+static bool efi_no_storage_paranoia;
+
+static int __init setup_storage_paranoia(char *arg)
+{
+	efi_no_storage_paranoia = true;
+	return 0;
+}
+early_param("efi_no_storage_paranoia", setup_storage_paranoia);
+
+#define call op.u.efi_runtime_call
+#define DECLARE_CALL(what) \
+	struct xen_platform_op op; \
+	op.cmd = XENPF_efi_runtime_call; \
+	call.function = XEN_EFI_##what; \
+	call.misc = 0
+
+static efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
+{
+	int err;
+	DECLARE_CALL(get_time);
+
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	if (tm) {
+		BUILD_BUG_ON(sizeof(*tm) != sizeof(call.u.get_time.time));
+		memcpy(tm, &call.u.get_time.time, sizeof(*tm));
+	}
+
+	if (tc) {
+		tc->resolution = call.u.get_time.resolution;
+		tc->accuracy = call.u.get_time.accuracy;
+		tc->sets_to_zero = !!(call.misc &
+				      XEN_EFI_GET_TIME_SET_CLEARS_NS);
+	}
+
+	return call.status;
+}
+
+static efi_status_t xen_efi_set_time(efi_time_t *tm)
+{
+	DECLARE_CALL(set_time);
+
+	BUILD_BUG_ON(sizeof(*tm) != sizeof(call.u.set_time));
+	memcpy(&call.u.set_time, tm, sizeof(*tm));
+
+	return HYPERVISOR_platform_op(&op) ? EFI_UNSUPPORTED : call.status;
+}
+
+static efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled,
+					    efi_bool_t *pending,
+					    efi_time_t *tm)
+{
+	int err;
+	DECLARE_CALL(get_wakeup_time);
+
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	if (tm) {
+		BUILD_BUG_ON(sizeof(*tm) != sizeof(call.u.get_wakeup_time));
+		memcpy(tm, &call.u.get_wakeup_time, sizeof(*tm));
+	}
+
+	if (enabled)
+		*enabled = !!(call.misc & XEN_EFI_GET_WAKEUP_TIME_ENABLED);
+
+	if (pending)
+		*pending = !!(call.misc & XEN_EFI_GET_WAKEUP_TIME_PENDING);
+
+	return call.status;
+}
+
+static efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
+{
+	DECLARE_CALL(set_wakeup_time);
+
+	BUILD_BUG_ON(sizeof(*tm) != sizeof(call.u.set_wakeup_time));
+	if (enabled)
+		call.misc = XEN_EFI_SET_WAKEUP_TIME_ENABLE;
+	if (tm)
+		memcpy(&call.u.set_wakeup_time, tm, sizeof(*tm));
+	else
+		call.misc |= XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY;
+
+	return HYPERVISOR_platform_op(&op) ? EFI_UNSUPPORTED : call.status;
+}
+
+static efi_status_t xen_efi_get_variable(efi_char16_t *name,
+					 efi_guid_t *vendor,
+					 u32 *attr,
+					 unsigned long *data_size,
+					 void *data)
+{
+	int err;
+	DECLARE_CALL(get_variable);
+
+	set_xen_guest_handle(call.u.get_variable.name, name);
+	BUILD_BUG_ON(sizeof(*vendor) !=
+		     sizeof(call.u.get_variable.vendor_guid));
+	memcpy(&call.u.get_variable.vendor_guid, vendor, sizeof(*vendor));
+	call.u.get_variable.size = *data_size;
+	set_xen_guest_handle(call.u.get_variable.data, data);
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	*data_size = call.u.get_variable.size;
+	if (attr)
+		*attr = call.misc;
+
+	return call.status;
+}
+
+static efi_status_t xen_efi_get_next_variable(unsigned long *name_size,
+					      efi_char16_t *name,
+					      efi_guid_t *vendor)
+{
+	int err;
+	DECLARE_CALL(get_next_variable_name);
+
+	call.u.get_next_variable_name.size = *name_size;
+	set_xen_guest_handle(call.u.get_next_variable_name.name, name);
+	BUILD_BUG_ON(sizeof(*vendor) !=
+		     sizeof(call.u.get_next_variable_name.vendor_guid));
+	memcpy(&call.u.get_next_variable_name.vendor_guid, vendor,
+	       sizeof(*vendor));
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	*name_size = call.u.get_next_variable_name.size;
+	memcpy(vendor, &call.u.get_next_variable_name.vendor_guid,
+	       sizeof(*vendor));
+
+	return call.status;
+}
+
+static efi_status_t xen_efi_set_variable(efi_char16_t *name,
+					 efi_guid_t *vendor,
+					 u32 attr,
+					 unsigned long data_size,
+					 void *data)
+{
+	DECLARE_CALL(set_variable);
+
+	set_xen_guest_handle(call.u.set_variable.name, name);
+	call.misc = attr;
+	BUILD_BUG_ON(sizeof(*vendor) !=
+		     sizeof(call.u.set_variable.vendor_guid));
+	memcpy(&call.u.set_variable.vendor_guid, vendor, sizeof(*vendor));
+	call.u.set_variable.size = data_size;
+	set_xen_guest_handle(call.u.set_variable.data, data);
+
+	return HYPERVISOR_platform_op(&op) ? EFI_UNSUPPORTED : call.status;
+}
+
+static efi_status_t xen_efi_query_variable_info(u32 attr,
+						u64 *storage_space,
+						u64 *remaining_space,
+						u64 *max_variable_size)
+{
+	int err;
+	DECLARE_CALL(query_variable_info);
+
+	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
+		return EFI_UNSUPPORTED;
+
+	call.u.query_variable_info.attr = attr;
+
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	*storage_space = call.u.query_variable_info.max_store_size;
+	*remaining_space = call.u.query_variable_info.remain_store_size;
+	*max_variable_size = call.u.query_variable_info.max_size;
+
+	return call.status;
+}
+
+static efi_status_t xen_efi_get_next_high_mono_count(u32 *count)
+{
+	int err;
+	DECLARE_CALL(get_next_high_monotonic_count);
+
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	*count = call.misc;
+
+	return call.status;
+}
+
+static efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules,
+					   unsigned long count,
+					   unsigned long sg_list)
+{
+	DECLARE_CALL(update_capsule);
+
+	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
+		return EFI_UNSUPPORTED;
+
+	set_xen_guest_handle(call.u.update_capsule.capsule_header_array,
+			     capsules);
+	call.u.update_capsule.capsule_count = count;
+	call.u.update_capsule.sg_list = sg_list;
+
+	return HYPERVISOR_platform_op(&op) ? EFI_UNSUPPORTED : call.status;
+}
+
+static efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules,
+					       unsigned long count,
+					       u64 *max_size,
+					       int *reset_type)
+{
+	int err;
+	DECLARE_CALL(query_capsule_capabilities);
+
+	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
+		return EFI_UNSUPPORTED;
+
+	set_xen_guest_handle(call.u.query_capsule_capabilities.capsule_header_array,
+			     capsules);
+	call.u.query_capsule_capabilities.capsule_count = count;
+
+	err = HYPERVISOR_platform_op(&op);
+	if (err)
+		return EFI_UNSUPPORTED;
+
+	*max_size = call.u.query_capsule_capabilities.max_capsule_size;
+	*reset_type = call.u.query_capsule_capabilities.reset_type;
+
+	return call.status;
+}
+
+int efi_set_rtc_mmss(const struct timespec *now)
+{
+	unsigned long nowtime = now->tv_sec;
+	efi_status_t	status;
+	efi_time_t	eft;
+	efi_time_cap_t	cap;
+	struct rtc_time	tm;
+
+	status = efi.get_time(&eft, &cap);
+	if (status != EFI_SUCCESS) {
+		pr_err("Oops: efitime: can't read time!\n");
+		return -1;
+	}
+
+	rtc_time_to_tm(nowtime, &tm);
+	if (!rtc_valid_tm(&tm)) {
+		eft.year = tm.tm_year + 1900;
+		eft.month = tm.tm_mon + 1;
+		eft.day = tm.tm_mday;
+		eft.minute = tm.tm_min;
+		eft.second = tm.tm_sec;
+		eft.nanosecond = 0;
+	} else {
+		pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
+		       __func__, nowtime);
+		return -1;
+	}
+
+	status = efi.set_time(&eft);
+	if (status != EFI_SUCCESS) {
+		pr_err("Oops: efitime: can't write time!\n");
+		return -1;
+	}
+	return 0;
+}
+
+void efi_get_time(struct timespec *now)
+{
+	efi_status_t status;
+	efi_time_t eft;
+	efi_time_cap_t cap;
+
+	status = efi.get_time(&eft, &cap);
+	if (status != EFI_SUCCESS) {
+		pr_err("Oops: efitime: can't read time!\n");
+		mach_get_cmos_time(now);
+		return;
+	}
+
+	now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
+			     eft.minute, eft.second);
+	now->tv_nsec = 0;
+}
+
+void __init efi_probe(void)
+{
+	static struct xen_platform_op __initdata op = {
+		.cmd = XENPF_firmware_info,
+		.u.firmware_info = {
+			.type = XEN_FW_EFI_INFO,
+			.index = XEN_FW_EFI_CONFIG_TABLE
+		}
+	};
+
+	if (HYPERVISOR_platform_op(&op) == 0) {
+		efi.get_time                 = xen_efi_get_time;
+		efi.set_time                 = xen_efi_set_time;
+		efi.get_wakeup_time          = xen_efi_get_wakeup_time;
+		efi.set_wakeup_time          = xen_efi_set_wakeup_time;
+		efi.get_variable             = xen_efi_get_variable;
+		efi.get_next_variable        = xen_efi_get_next_variable;
+		efi.set_variable             = xen_efi_set_variable;
+		efi.get_next_high_mono_count = xen_efi_get_next_high_mono_count;
+		efi.query_variable_info      = xen_efi_query_variable_info;
+		efi.update_capsule           = xen_efi_update_capsule;
+		efi.query_capsule_caps       = xen_efi_query_capsule_caps;
+
+		__set_bit(EFI_BOOT, &efi.flags);
+#ifdef CONFIG_64BIT
+		__set_bit(EFI_64BIT, &efi.flags);
+#endif
+		__set_bit(EFI_SYSTEM_TABLES, &efi.flags);
+		__set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
+		__set_bit(EFI_MEMMAP, &efi.flags);
+	}
+}
+
+void __init efi_reserve_boot_services(void) { }
+void __init efi_free_boot_services(void) { }
+
+void __init efi_init(void)
+{
+	efi_char16_t c16[100];
+	char vendor[ARRAY_SIZE(c16)] = "unknown";
+	int ret, i;
+	struct xen_platform_op op;
+	union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+
+	op.cmd = XENPF_firmware_info;
+	op.u.firmware_info.type = XEN_FW_EFI_INFO;
+
+	/*
+	 * Show what we know for posterity
+	 */
+	op.u.firmware_info.index = XEN_FW_EFI_VENDOR;
+	info->vendor.bufsz = sizeof(c16);
+	set_xen_guest_handle(info->vendor.name, c16);
+	ret = HYPERVISOR_platform_op(&op);
+	if (!ret) {
+		for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
+			vendor[i] = c16[i];
+		vendor[i] = '\0';
+	} else
+		pr_err("Could not get the firmware vendor!\n");
+
+	op.u.firmware_info.index = XEN_FW_EFI_VERSION;
+	ret = HYPERVISOR_platform_op(&op);
+	if (!ret)
+		pr_info("EFI v%u.%.02u by %s\n",
+			info->version >> 16,
+			info->version & 0xffff, vendor);
+	else
+		pr_err("Could not get EFI revision!\n");
+
+	op.u.firmware_info.index = XEN_FW_EFI_RT_VERSION;
+	ret = HYPERVISOR_platform_op(&op);
+	if (!ret)
+		efi.runtime_version = info->version;
+	else
+		pr_warn("Could not get runtime services revision.\n");
+
+	x86_platform.get_wallclock = efi_get_time;
+
+	op.u.firmware_info.index = XEN_FW_EFI_CONFIG_TABLE;
+	if (HYPERVISOR_platform_op(&op))
+		BUG();
+	if (efi_config_init(info->cfg.addr, info->cfg.nent, arch_tables))
+		return;
+
+	__set_bit(EFI_CONFIG_TABLES, &efi.flags);
+}
+
+#undef DECLARE_CALL
+#undef call
+
+void __init efi_late_init(void)
+{
+	efi_bgrt_init();
+}
+
+void __init efi_enter_virtual_mode(void)
+{
+	/* clean DUMMY object */
+	xen_efi_set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
+			     EFI_VARIABLE_NON_VOLATILE |
+			     EFI_VARIABLE_BOOTSERVICE_ACCESS |
+			     EFI_VARIABLE_RUNTIME_ACCESS,
+			     0, NULL);
+}
+
+static struct platform_device rtc_efi_dev = {
+	.name = "rtc-efi",
+	.id = -1,
+};
+
+static int __init rtc_init(void)
+{
+	if (!efi_enabled(EFI_RUNTIME_SERVICES))
+		return -ENODEV;
+
+	if (platform_device_register(&rtc_efi_dev) < 0)
+		pr_err("unable to register EFI RTC device...\n");
+
+	/* not necessarily an error */
+	return 0;
+}
+arch_initcall(rtc_init);
+
+/*
+ * Convenience functions to obtain memory types and attributes
+ */
+u32 efi_mem_type(unsigned long phys_addr)
+{
+	struct xen_platform_op op;
+	union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+
+	if (!efi_enabled(EFI_MEMMAP))
+		return 0;
+
+	op.cmd = XENPF_firmware_info;
+	op.u.firmware_info.type = XEN_FW_EFI_INFO;
+	op.u.firmware_info.index = XEN_FW_EFI_MEM_INFO;
+	info->mem.addr = phys_addr;
+	info->mem.size = 0;
+	return HYPERVISOR_platform_op(&op) ? 0 : info->mem.type;
+}
+
+u64 efi_mem_attributes(unsigned long phys_addr)
+{
+	struct xen_platform_op op;
+	union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
+
+	op.cmd = XENPF_firmware_info;
+	op.u.firmware_info.type = XEN_FW_EFI_INFO;
+	op.u.firmware_info.index = XEN_FW_EFI_MEM_INFO;
+	info->mem.addr = phys_addr;
+	info->mem.size = 0;
+	return HYPERVISOR_platform_op(&op) ? 0 : info->mem.attr;
+}
+
+/*
+ * Some firmware implementations refuse to boot if there's insufficient space
+ * in the variable store. Ensure that we never use more than a safe limit.
+ *
+ * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
+ * store.
+ */
+efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
+{
+	efi_status_t status;
+	u64 storage_size, remaining_size, max_size;
+
+	if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
+		return 0;
+
+	status = xen_efi_query_variable_info(attributes, &storage_size,
+					     &remaining_size, &max_size);
+	if (status != EFI_SUCCESS)
+		return status;
+
+	/*
+	 * We account for that by refusing the write if permitting it would
+	 * reduce the available space to under 5KB. This figure was provided by
+	 * Samsung, so should be safe.
+	 */
+	if ((remaining_size - size < EFI_MIN_RESERVE) &&
+		!efi_no_storage_paranoia) {
+
+		/*
+		 * Triggering garbage collection may require that the firmware
+		 * generate a real EFI_OUT_OF_RESOURCES error. We can force
+		 * that by attempting to use more space than is available.
+		 */
+		unsigned long dummy_size = remaining_size + 1024;
+		void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
+
+		if (!dummy)
+			return EFI_OUT_OF_RESOURCES;
+
+		status = xen_efi_set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
+					      EFI_VARIABLE_NON_VOLATILE |
+					      EFI_VARIABLE_BOOTSERVICE_ACCESS |
+					      EFI_VARIABLE_RUNTIME_ACCESS,
+					      dummy_size, dummy);
+		kfree(dummy);
+
+		if (status == EFI_SUCCESS) {
+			/*
+			 * This should have failed, so if it didn't make sure
+			 * that we delete it...
+			 */
+			xen_efi_set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
+					     EFI_VARIABLE_NON_VOLATILE |
+					     EFI_VARIABLE_BOOTSERVICE_ACCESS |
+					     EFI_VARIABLE_RUNTIME_ACCESS,
+					     0, NULL);
+		}
+
+		/*
+		 * The runtime code may now have triggered a garbage collection
+		 * run, so check the variable info again
+		 */
+		status = xen_efi_query_variable_info(attributes, &storage_size,
+						     &remaining_size, &max_size);
+
+		if (status != EFI_SUCCESS)
+			return status;
+
+		/*
+		 * There still isn't enough room, so return an error
+		 */
+		if (remaining_size - size < EFI_MIN_RESERVE)
+			return EFI_OUT_OF_RESOURCES;
+	}
+
+	return EFI_SUCCESS;
+}
+EXPORT_SYMBOL_GPL(efi_query_variable_store);
--- head.orig/drivers/firmware/efi/efi.c	2014-07-29 10:32:22.000000000 +0200
+++ head/drivers/firmware/efi/efi.c	2014-05-02 16:30:39.000000000 +0200
@@ -202,6 +202,7 @@ subsys_initcall(efisubsys_init);
  */
 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
 {
+#ifndef CONFIG_XEN
 	struct efi_memory_map *map;
 	void *p;
 	map = efi.memmap;
@@ -224,6 +225,7 @@ void __iomem *efi_lookup_mapped_addr(u64
 			return (__force void __iomem *)(unsigned long)phys_addr;
 		}
 	}
+#endif
 	return NULL;
 }
 
@@ -263,12 +265,24 @@ static __init int match_config_table(efi
 	return 0;
 }
 
-int __init efi_config_init(efi_config_table_type_t *arch_tables)
+int __init efi_config_init(
+#ifdef CONFIG_XEN
+			   u64 tables, unsigned int nr_tables,
+#endif
+			   efi_config_table_type_t *arch_tables)
 {
+#ifndef CONFIG_XEN
+	u64 tables = efi.systab->tables;
+	unsigned int nr_tables = efi.systab->nr_tables;
+	const bool efi_64bit = efi_enabled(EFI_64BIT);
+#else
+	const bool efi_64bit = true;
+#define early_memremap early_ioremap
+#endif
 	void *config_tables, *tablep;
 	int i, sz;
 
-	if (efi_enabled(EFI_64BIT))
+	if (efi_64bit)
 		sz = sizeof(efi_config_table_64_t);
 	else
 		sz = sizeof(efi_config_table_32_t);
@@ -276,8 +290,7 @@ int __init efi_config_init(efi_config_ta
 	/*
 	 * Let's see what config tables the firmware passed to us.
 	 */
-	config_tables = early_memremap(efi.systab->tables,
-				       efi.systab->nr_tables * sz);
+	config_tables = early_memremap(tables, nr_tables * sz);
 	if (config_tables == NULL) {
 		pr_err("Could not map Configuration table!\n");
 		return -ENOMEM;
@@ -285,11 +298,11 @@ int __init efi_config_init(efi_config_ta
 
 	tablep = config_tables;
 	pr_info("");
-	for (i = 0; i < efi.systab->nr_tables; i++) {
+	for (i = 0; i < nr_tables; i++) {
 		efi_guid_t guid;
 		unsigned long table;
 
-		if (efi_enabled(EFI_64BIT)) {
+		if (efi_64bit) {
 			u64 table64;
 			guid = ((efi_config_table_64_t *)tablep)->guid;
 			table64 = ((efi_config_table_64_t *)tablep)->table;
@@ -298,8 +311,7 @@ int __init efi_config_init(efi_config_ta
 			if (table64 >> 32) {
 				pr_cont("\n");
 				pr_err("Table located above 4GB, disabling EFI.\n");
-				early_iounmap(config_tables,
-					       efi.systab->nr_tables * sz);
+				early_iounmap(config_tables, nr_tables * sz);
 				return -EINVAL;
 			}
 #endif
@@ -314,7 +326,8 @@ int __init efi_config_init(efi_config_ta
 		tablep += sz;
 	}
 	pr_cont("\n");
-	early_iounmap(config_tables, efi.systab->nr_tables * sz);
+	early_iounmap(config_tables, nr_tables * sz);
+#undef early_memremap
 
 	set_bit(EFI_CONFIG_TABLES, &efi.flags);
 
--- head.orig/drivers/rtc/Kconfig	2014-06-26 11:20:44.000000000 +0200
+++ head/drivers/rtc/Kconfig	2013-12-11 11:25:07.000000000 +0100
@@ -789,7 +789,7 @@ config RTC_DRV_DA9063
 
 config RTC_DRV_EFI
 	tristate "EFI RTC"
-	depends on IA64
+	depends on IA64 || (XEN && EFI)
 	help
 	  If you say yes here you will get support for the EFI
 	  Real Time Clock.
--- head.orig/drivers/rtc/rtc-cmos.c	2014-06-26 11:20:44.000000000 +0200
+++ head/drivers/rtc/rtc-cmos.c	2014-02-20 15:49:55.000000000 +0100
@@ -36,6 +36,7 @@
 #include <linux/platform_device.h>
 #include <linux/log2.h>
 #include <linux/pm.h>
+#include <linux/efi.h>
 #include <linux/of.h>
 #include <linux/of_platform.h>
 #include <linux/dmi.h>
@@ -1223,6 +1224,11 @@ static int __init cmos_init(void)
 {
 	int retval = 0;
 
+#ifdef CONFIG_XEN
+	if (efi_enabled(EFI_RUNTIME_SERVICES))
+		return -ENODEV;
+#endif
+
 #ifdef	CONFIG_PNP
 	retval = pnp_register_driver(&cmos_pnp_driver);
 	if (retval == 0)
--- head.orig/drivers/rtc/rtc-efi.c	2014-06-26 11:20:44.000000000 +0200
+++ head/drivers/rtc/rtc-efi.c	2013-08-12 17:57:27.000000000 +0200
@@ -213,3 +213,6 @@ module_platform_driver_probe(efi_rtc_dri
 MODULE_AUTHOR("dann frazier <dannf@hp.com>");
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("EFI RTC driver");
+#ifdef CONFIG_XEN
+MODULE_ALIAS("platform:rtc-efi");
+#endif
--- head.orig/drivers/xen/console/console.c	2012-03-22 14:31:13.000000000 +0100
+++ head/drivers/xen/console/console.c	2012-03-22 15:29:12.000000000 +0100
@@ -313,6 +313,7 @@ void __init dom0_init_screen_info(const 
 		break;
 
 	case XEN_VGATYPE_VESA_LFB:
+	case XEN_VGATYPE_EFI_LFB:
 		if (size < offsetof(struct dom0_vga_console_info,
 		                    u.vesa_lfb.gbl_caps))
 			break;
@@ -331,6 +332,10 @@ void __init dom0_init_screen_info(const 
 		screen_info.blue_pos = info->u.vesa_lfb.blue_pos;
 		screen_info.rsvd_size = info->u.vesa_lfb.rsvd_size;
 		screen_info.rsvd_pos = info->u.vesa_lfb.rsvd_pos;
+		if (info->video_type == XEN_VGATYPE_EFI_LFB) {
+			screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
+			break;
+		}
 		if (size >= offsetof(struct dom0_vga_console_info,
 		                     u.vesa_lfb.gbl_caps)
 		            + sizeof(info->u.vesa_lfb.gbl_caps))
--- head.orig/include/linux/efi.h	2014-06-26 11:21:11.000000000 +0200
+++ head/include/linux/efi.h	2014-05-02 16:30:29.000000000 +0200
@@ -797,7 +797,9 @@ typedef struct _efi_file_io_interface {
  * All runtime access to EFI goes through this structure:
  */
 extern struct efi {
+#ifndef CONFIG_XEN
 	efi_system_table_t *systab;	/* EFI system table */
+#endif
 	unsigned int runtime_version;	/* Runtime services version */
 	unsigned long mps;		/* MPS table */
 	unsigned long acpi;		/* ACPI table  (IA64 ext 0.71) */
@@ -822,9 +824,11 @@ extern struct efi {
 	efi_update_capsule_t *update_capsule;
 	efi_query_capsule_caps_t *query_capsule_caps;
 	efi_get_next_high_mono_count_t *get_next_high_mono_count;
+#ifndef CONFIG_XEN
 	efi_reset_system_t *reset_system;
 	efi_set_virtual_address_map_t *set_virtual_address_map;
 	struct efi_memory_map *memmap;
+#endif
 	unsigned long flags;
 } efi;
 
@@ -861,7 +865,11 @@ static inline efi_status_t efi_query_var
 }
 #endif
 extern void __iomem *efi_lookup_mapped_addr(u64 phys_addr);
-extern int efi_config_init(efi_config_table_type_t *arch_tables);
+extern int efi_config_init(
+#ifdef CONFIG_XEN
+			   u64 tables, unsigned int nr_tables,
+#endif
+			   efi_config_table_type_t *arch_tables);
 extern u64 efi_get_iobase (void);
 extern u32 efi_mem_type (unsigned long phys_addr);
 extern u64 efi_mem_attributes (unsigned long phys_addr);