Blob Blame History Raw
From: Jiang, Yunhong <yunhong.jiang@intel.com>
Subject: xen/acpi: Add memory hotadd to pvops dom0
References: bnc#651066
Patch-mainline: n/a

When memory hotadd event happen, a Xen hook will be called, to notify
hypervisor of the new added memory.

Because xen hypervisor will use the new memory to setup frametable/m2p
table, so dom0 will always return success to acpi bios, and notify xen
hypervisor later.

It add a hook in driver/acpi/acpi_memhotplug.c, but that change is quite
small, not sure if it is acceptable. Other method is to provide a xen
specific acpi_memory_device_driver, but I'm not sure if it worth to add
so much changes, to simply avoid two hooks.

jb: Integrate into base module; cleanup.
Acked-by: jbeulich@novell.com

--- head.orig/drivers/acpi/Kconfig	2011-07-21 12:19:24.000000000 +0200
+++ head/drivers/acpi/Kconfig	2012-04-11 17:03:20.000000000 +0200
@@ -337,7 +337,7 @@ config ACPI_CONTAINER
 
 config ACPI_HOTPLUG_MEMORY
 	tristate "Memory Hotplug"
-	depends on MEMORY_HOTPLUG
+	depends on MEMORY_HOTPLUG || XEN_PRIVILEGED_GUEST
 	default n
 	help
 	  This driver supports ACPI memory hotplug.  The driver
--- head.orig/drivers/acpi/acpi_memhotplug.c	2010-05-16 23:17:36.000000000 +0200
+++ head/drivers/acpi/acpi_memhotplug.c	2011-02-02 15:10:06.000000000 +0100
@@ -88,6 +88,14 @@ struct acpi_memory_device {
 
 static int acpi_hotmem_initialized;
 
+#ifdef CONFIG_XEN
+#include "../xen/core/acpi_memhotplug.c"
+#define memory_add_physaddr_to_nid(start) 0
+#else
+static inline int xen_hotadd_mem_init(void) { return 0; }
+static inline void xen_hotadd_mem_exit(void) {}
+#endif
+
 static acpi_status
 acpi_memory_get_resource(struct acpi_resource *resource, void *context)
 {
@@ -229,6 +237,10 @@ static int acpi_memory_enable_device(str
 		return result;
 	}
 
+#ifdef CONFIG_XEN
+	return xen_hotadd_memory(mem_device);
+#endif
+
 	node = acpi_get_node(mem_device->device->handle);
 	/*
 	 * Tell the VM there is more memory here...
@@ -312,6 +324,10 @@ static int acpi_memory_disable_device(st
 	struct acpi_memory_info *info, *n;
 
 
+#ifdef CONFIG_XEN
+	return -EOPNOTSUPP;
+#endif
+
 	/*
 	 * Ask the VM to offline this memory range.
 	 * Note: Assume that this function returns zero on success
@@ -531,6 +547,10 @@ static int __init acpi_memory_device_ini
 	acpi_status status;
 
 
+	result = xen_hotadd_mem_init();
+	if (result < 0)
+		return result;
+
 	result = acpi_bus_register_driver(&acpi_memory_device_driver);
 
 	if (result < 0)
@@ -570,6 +590,8 @@ static void __exit acpi_memory_device_ex
 
 	acpi_bus_unregister_driver(&acpi_memory_device_driver);
 
+	xen_hotadd_mem_exit();
+
 	return;
 }
 
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ head/drivers/xen/core/acpi_memhotplug.c	2011-02-02 15:10:06.000000000 +0100
@@ -0,0 +1,190 @@
+/*
+ *  xen_acpi_memhotplug.c - interface to notify Xen on memory device hotadd
+ *
+ *  Copyright (C) 2008, Intel corporation
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *
+ */
+
+#include <xen/interface/platform.h>
+#include <asm/hypervisor.h>
+
+struct xen_hotmem_entry {
+	struct list_head hotmem_list;
+	uint64_t start;
+	uint64_t end;
+	uint32_t flags;
+	uint32_t pxm;
+};
+
+struct xen_hotmem_list {
+	struct list_head list;
+	unsigned int entry_nr;
+};
+
+static struct xen_hotmem_list xen_hotmem = {
+	.list = LIST_HEAD_INIT(xen_hotmem.list)
+};
+static DEFINE_SPINLOCK(xen_hotmem_lock);
+
+static int xen_hyper_addmem(struct xen_hotmem_entry *entry)
+{
+	xen_platform_op_t op;
+
+	op.cmd = XENPF_mem_hotadd;
+	op.u.mem_add.spfn = entry->start >> PAGE_SHIFT;
+	op.u.mem_add.epfn = entry->end >> PAGE_SHIFT;
+	op.u.mem_add.flags = entry->flags;
+	op.u.mem_add.pxm = entry->pxm;
+
+	return HYPERVISOR_platform_op(&op);
+}
+
+static int add_hotmem_entry(int pxm, uint64_t start,
+			uint64_t length, uint32_t flags)
+{
+	struct xen_hotmem_entry *entry;
+
+	if (pxm < 0 || !length)
+		return -EINVAL;
+
+	entry = kzalloc(sizeof(struct xen_hotmem_entry), GFP_ATOMIC);
+	if (!entry)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&entry->hotmem_list);
+	entry->start = start;
+	entry->end = start + length;
+	entry->flags = flags;
+	entry->pxm = pxm;
+
+	spin_lock(&xen_hotmem_lock);
+
+	list_add_tail(&entry->hotmem_list, &xen_hotmem.list);
+	xen_hotmem.entry_nr++;
+
+	spin_unlock(&xen_hotmem_lock);
+
+	return 0;
+}
+
+static int free_hotmem_entry(struct xen_hotmem_entry *entry)
+{
+	list_del(&entry->hotmem_list);
+	kfree(entry);
+
+	return 0;
+}
+
+static void xen_hotadd_mem_dpc(struct work_struct *work)
+{
+	struct list_head *elem, *tmp;
+	struct xen_hotmem_entry *entry;
+	unsigned long flags;
+	int ret;
+
+	spin_lock_irqsave(&xen_hotmem_lock, flags);
+	list_for_each_safe(elem, tmp, &xen_hotmem.list) {
+		entry = list_entry(elem, struct xen_hotmem_entry, hotmem_list);
+		ret = xen_hyper_addmem(entry);
+		if (ret)
+			pr_warn("xen addmem failed with %x\n", ret);
+		free_hotmem_entry(entry);
+		xen_hotmem.entry_nr--;
+	}
+	spin_unlock_irqrestore(&xen_hotmem_lock, flags);
+}
+
+static DECLARE_WORK(xen_hotadd_mem_work, xen_hotadd_mem_dpc);
+
+static int xen_acpi_get_pxm(acpi_handle h)
+{
+	unsigned long long pxm;
+	acpi_status status;
+	acpi_handle handle;
+	acpi_handle phandle = h;
+
+	do {
+		handle = phandle;
+		status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
+		if (ACPI_SUCCESS(status))
+			return pxm;
+		status = acpi_get_parent(handle, &phandle);
+	} while (ACPI_SUCCESS(status));
+
+	return -1;
+}
+
+static int xen_hotadd_memory(struct acpi_memory_device *mem_device)
+{
+	int pxm, result;
+	int num_enabled = 0;
+	struct acpi_memory_info *info;
+
+	if (!mem_device)
+		return -EINVAL;
+
+	pxm = xen_acpi_get_pxm(mem_device->device->handle);
+
+	if (pxm < 0)
+		return -EINVAL;
+
+	/*
+	 * Always return success to ACPI driver, and notify hypervisor later
+	 * because hypervisor will utilize the memory in memory hotadd hypercall
+	 */
+	list_for_each_entry(info, &mem_device->res_list, list) {
+		if (info->enabled) { /* just sanity check...*/
+			num_enabled++;
+			continue;
+		}
+		/*
+		 * If the memory block size is zero, please ignore it.
+		 * Don't try to do the following memory hotplug flowchart.
+		 */
+		if (!info->length)
+			continue;
+
+		result = add_hotmem_entry(pxm, info->start_addr,
+					  info->length, 0);
+		if (result)
+			continue;
+		info->enabled = 1;
+		num_enabled++;
+	}
+
+	if (!num_enabled)
+		return -EINVAL;
+
+	schedule_work(&xen_hotadd_mem_work);
+
+	return 0;
+}
+
+static int xen_hotadd_mem_init(void)
+{
+	if (!is_initial_xendomain())
+		return -ENODEV;
+
+	return 0;
+}
+
+static void xen_hotadd_mem_exit(void)
+{
+	flush_scheduled_work();
+}