From nobody Fri Apr 19 02:08:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 152354393799047.39059843414884; Thu, 12 Apr 2018 07:38:57 -0700 (PDT) Received: from localhost ([::1]:44455 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6dNI-0001DR-NE for importer@patchew.org; Thu, 12 Apr 2018 10:38:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37171) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6dLL-0008Iw-FG for qemu-devel@nongnu.org; Thu, 12 Apr 2018 10:36:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f6dLJ-0007bw-15 for qemu-devel@nongnu.org; Thu, 12 Apr 2018 10:36:55 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49914 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f6dLI-0007bG-Sd; Thu, 12 Apr 2018 10:36:52 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 77628402B031; Thu, 12 Apr 2018 14:36:52 +0000 (UTC) Received: from t460s.redhat.com (unknown [10.36.118.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2EE5D10B008B; Thu, 12 Apr 2018 14:36:48 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Thu, 12 Apr 2018 16:36:33 +0200 Message-Id: <20180412143635.19949-2-david@redhat.com> In-Reply-To: <20180412143635.19949-1-david@redhat.com> References: <20180412143635.19949-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 12 Apr 2018 14:36:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 12 Apr 2018 14:36:52 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 1/3] pc-dimm: factor out MemoryDevice interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Markus Armbruster , qemu-s390x@nongnu.org, qemu-ppc@nongnu.org, Paolo Bonzini , Marcel Apfelbaum , Igor Mammedov , David Gibson , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" On the qmp level, we already have the concept of memory devices: "query-memory-devices" Right now, we only support NVDIMM and PCDIMM. We want to map other devices later into the address space of the guest. Such device could e.g. be virtio devices. These devices will have a guest memory range assigned but won't be exposed via e.g. ACPI. We want to make them look like memory device, but not glued to pc-dimm. Especially, it will not always be possible to have TYPE_PC_DIMM as a parent class (e.g. virtio devices). Let's use an interface instead. As a first part, convert handling of - qmp_pc_dimm_device_list - get_plugged_memory_size to our new model. plug/unplug stuff etc. will follow later. A memory device will have to provide the following functions: - get_addr(): Necessary, as the property "addr" can e.g. not be used for virtio devices (already defined). - get_plugged_size(): The amount this device offers to the guest as of now. - get_region_size(): Because this can later on be bigger than the plugged size. - fill_device_info(): Fill MemoryDeviceInfo, e.g. for qmp. Signed-off-by: David Hildenbrand --- hw/i386/acpi-build.c | 3 +- hw/mem/Makefile.objs | 1 + hw/mem/memory-device.c | 119 +++++++++++++++++++++++= ++++ hw/mem/pc-dimm.c | 119 ++++++++++++++---------= ---- hw/ppc/spapr.c | 3 +- hw/ppc/spapr_hcall.c | 1 + include/hw/mem/memory-device.h | 44 ++++++++++ include/hw/mem/pc-dimm.h | 2 - numa.c | 3 +- qmp.c | 4 +- stubs/Makefile.objs | 2 +- stubs/{qmp_pc_dimm.c =3D> qmp_memory_device.c} | 4 +- 12 files changed, 239 insertions(+), 66 deletions(-) create mode 100644 hw/mem/memory-device.c create mode 100644 include/hw/mem/memory-device.h rename stubs/{qmp_pc_dimm.c =3D> qmp_memory_device.c} (61%) diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index 3cf2a1679c..ca3645d57b 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -46,6 +46,7 @@ #include "hw/acpi/vmgenid.h" #include "sysemu/tpm_backend.h" #include "hw/timer/mc146818rtc_regs.h" +#include "hw/mem/memory-device.h" #include "sysemu/numa.h" =20 /* Supported chipsets: */ @@ -2253,7 +2254,7 @@ build_tpm2(GArray *table_data, BIOSLinker *linker, GA= rray *tcpalog) static void build_srat_hotpluggable_memory(GArray *table_data, uint64_t ba= se, uint64_t len, int default_node) { - MemoryDeviceInfoList *info_list =3D qmp_pc_dimm_device_list(); + MemoryDeviceInfoList *info_list =3D qmp_memory_device_list(); MemoryDeviceInfoList *info; MemoryDeviceInfo *mi; PCDIMMDeviceInfo *di; diff --git a/hw/mem/Makefile.objs b/hw/mem/Makefile.objs index f12f8b97a2..10be4df2a2 100644 --- a/hw/mem/Makefile.objs +++ b/hw/mem/Makefile.objs @@ -1,2 +1,3 @@ common-obj-$(CONFIG_MEM_HOTPLUG) +=3D pc-dimm.o +common-obj-$(CONFIG_MEM_HOTPLUG) +=3D memory-device.o common-obj-$(CONFIG_NVDIMM) +=3D nvdimm.o diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c new file mode 100644 index 0000000000..0e0d6b539a --- /dev/null +++ b/hw/mem/memory-device.c @@ -0,0 +1,119 @@ +/* + * Memory Device Interface + * + * Copyright ProfitBricks GmbH 2012 + * Copyright (C) 2014 Red Hat Inc + * Copyright (c) 2018 Red Hat Inc + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/mem/memory-device.h" +#include "hw/qdev.h" +#include "qapi/error.h" +#include "hw/boards.h" +#include "qemu/range.h" + +static gint memory_device_addr_sort(gconstpointer a, gconstpointer b) +{ + MemoryDeviceState *md_a =3D MEMORY_DEVICE(a); + MemoryDeviceState *md_b =3D MEMORY_DEVICE(b); + MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(a); + const uint64_t addr_a =3D mdc->get_addr(md_a); + const uint64_t addr_b =3D mdc->get_addr(md_b); + + if (addr_a > addr_b) { + return 1; + } else if (addr_a < addr_b) { + return -1; + } + return 0; +} + +static int memory_device_built_list(Object *obj, void *opaque) +{ + GSList **list =3D opaque; + + if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { + DeviceState *dev =3D DEVICE(obj); + if (dev->realized) { /* only realized memory devices matter */ + *list =3D g_slist_insert_sorted(*list, dev, memory_device_addr= _sort); + } + } + + object_child_foreach(obj, memory_device_built_list, opaque); + return 0; +} + +MemoryDeviceInfoList *qmp_memory_device_list(void) +{ + GSList *devices =3D NULL, *item; + MemoryDeviceInfoList *list =3D NULL, *prev =3D NULL; + + object_child_foreach(qdev_get_machine(), memory_device_built_list, + &devices); + + for (item =3D devices; item; item =3D g_slist_next(item)) { + MemoryDeviceState *md =3D MEMORY_DEVICE(item->data); + MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(item->data); + MemoryDeviceInfoList *elem =3D g_new0(MemoryDeviceInfoList, 1); + MemoryDeviceInfo *info =3D g_new0(MemoryDeviceInfo, 1); + + mdc->fill_device_info(md, info); + + elem->value =3D info; + elem->next =3D NULL; + if (prev) { + prev->next =3D elem; + } else { + list =3D elem; + } + prev =3D elem; + } + + g_slist_free(devices); + + return list; +} + +static int memory_device_plugged_size(Object *obj, void *opaque) +{ + uint64_t *size =3D opaque; + + if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { + DeviceState *dev =3D DEVICE(obj); + MemoryDeviceState *md =3D MEMORY_DEVICE(obj); + MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(obj); + + if (dev->realized) { + *size +=3D mdc->get_plugged_size(md, &error_abort); + } + } + + object_child_foreach(obj, memory_device_plugged_size, opaque); + return 0; +} + +uint64_t get_plugged_memory_size(void) +{ + uint64_t size =3D 0; + + memory_device_plugged_size(qdev_get_machine(), &size); + + return size; +} + +static const TypeInfo memory_device_info =3D { + .name =3D TYPE_MEMORY_DEVICE, + .parent =3D TYPE_INTERFACE, + .class_size =3D sizeof(MemoryDeviceClass), +}; + +static void memory_device_register_types(void) +{ + type_register_static(&memory_device_info); +} + +type_init(memory_device_register_types) diff --git a/hw/mem/pc-dimm.c b/hw/mem/pc-dimm.c index 51350d9c2d..1dbf699e02 100644 --- a/hw/mem/pc-dimm.c +++ b/hw/mem/pc-dimm.c @@ -21,6 +21,7 @@ #include "qemu/osdep.h" #include "hw/mem/pc-dimm.h" #include "hw/mem/nvdimm.h" +#include "hw/mem/memory-device.h" #include "qapi/error.h" #include "qemu/config-file.h" #include "qapi/visitor.h" @@ -158,11 +159,6 @@ uint64_t pc_existing_dimms_capacity(Error **errp) return cap.size; } =20 -uint64_t get_plugged_memory_size(void) -{ - return pc_existing_dimms_capacity(&error_abort); -} - static int pc_dimm_slot2bitmap(Object *obj, void *opaque) { unsigned long *bitmap =3D opaque; @@ -238,57 +234,6 @@ static int pc_dimm_built_list(Object *obj, void *opaqu= e) return 0; } =20 -MemoryDeviceInfoList *qmp_pc_dimm_device_list(void) -{ - GSList *dimms =3D NULL, *item; - MemoryDeviceInfoList *list =3D NULL, *prev =3D NULL; - - object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &dimms); - - for (item =3D dimms; item; item =3D g_slist_next(item)) { - PCDIMMDevice *dimm =3D PC_DIMM(item->data); - Object *obj =3D OBJECT(dimm); - MemoryDeviceInfoList *elem =3D g_new0(MemoryDeviceInfoList, 1); - MemoryDeviceInfo *info =3D g_new0(MemoryDeviceInfo, 1); - PCDIMMDeviceInfo *di =3D g_new0(PCDIMMDeviceInfo, 1); - bool is_nvdimm =3D object_dynamic_cast(obj, TYPE_NVDIMM); - DeviceClass *dc =3D DEVICE_GET_CLASS(obj); - DeviceState *dev =3D DEVICE(obj); - - if (dev->id) { - di->has_id =3D true; - di->id =3D g_strdup(dev->id); - } - di->hotplugged =3D dev->hotplugged; - di->hotpluggable =3D dc->hotpluggable; - di->addr =3D dimm->addr; - di->slot =3D dimm->slot; - di->node =3D dimm->node; - di->size =3D object_property_get_uint(obj, PC_DIMM_SIZE_PROP, NULL= ); - di->memdev =3D object_get_canonical_path(OBJECT(dimm->hostmem)); - - if (!is_nvdimm) { - info->u.dimm.data =3D di; - info->type =3D MEMORY_DEVICE_INFO_KIND_DIMM; - } else { - info->u.nvdimm.data =3D di; - info->type =3D MEMORY_DEVICE_INFO_KIND_NVDIMM; - } - elem->value =3D info; - elem->next =3D NULL; - if (prev) { - prev->next =3D elem; - } else { - list =3D elem; - } - prev =3D elem; - } - - g_slist_free(dimms); - - return list; -} - uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, uint64_t address_space_size, uint64_t *hint, uint64_t align, uint64_t si= ze, @@ -445,10 +390,62 @@ static MemoryRegion *pc_dimm_get_vmstate_memory_regio= n(PCDIMMDevice *dimm) return host_memory_backend_get_memory(dimm->hostmem, &error_abort); } =20 +static uint64_t pc_dimm_md_get_addr(MemoryDeviceState *md) +{ + PCDIMMDevice *dimm =3D PC_DIMM(md); + + return dimm->addr; +} + +static uint64_t pc_dimm_md_get_region_size(MemoryDeviceState *md, Error **= errp) +{ + PCDIMMDevice *dimm =3D PC_DIMM(md); + PCDIMMDeviceClass *ddc =3D PC_DIMM_GET_CLASS(md); + MemoryRegion *mr; + + mr =3D ddc->get_memory_region(dimm, errp); + if (!mr) { + return 0; + } + + return memory_region_size(mr); +} + +static void pc_dimm_md_fill_device_info(MemoryDeviceState *md, + MemoryDeviceInfo *info) +{ + PCDIMMDeviceInfo *di =3D g_new0(PCDIMMDeviceInfo, 1); + DeviceClass *dc =3D DEVICE_GET_CLASS(md); + PCDIMMDevice *dimm =3D PC_DIMM(md); + DeviceState *dev =3D DEVICE(md); + + if (dev->id) { + di->has_id =3D true; + di->id =3D g_strdup(dev->id); + } + di->hotplugged =3D dev->hotplugged; + di->hotpluggable =3D dc->hotpluggable; + di->addr =3D dimm->addr; + di->slot =3D dimm->slot; + di->node =3D dimm->node; + di->size =3D object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP, + NULL); + di->memdev =3D object_get_canonical_path(OBJECT(dimm->hostmem)); + + if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { + info->u.nvdimm.data =3D di; + info->type =3D MEMORY_DEVICE_INFO_KIND_NVDIMM; + } else { + info->u.dimm.data =3D di; + info->type =3D MEMORY_DEVICE_INFO_KIND_DIMM; + } +} + static void pc_dimm_class_init(ObjectClass *oc, void *data) { DeviceClass *dc =3D DEVICE_CLASS(oc); PCDIMMDeviceClass *ddc =3D PC_DIMM_CLASS(oc); + MemoryDeviceClass *mdc =3D MEMORY_DEVICE_CLASS(oc); =20 dc->realize =3D pc_dimm_realize; dc->unrealize =3D pc_dimm_unrealize; @@ -457,6 +454,12 @@ static void pc_dimm_class_init(ObjectClass *oc, void *= data) =20 ddc->get_memory_region =3D pc_dimm_get_memory_region; ddc->get_vmstate_memory_region =3D pc_dimm_get_vmstate_memory_region; + + mdc->get_addr =3D pc_dimm_md_get_addr; + /* for a dimm plugged_size =3D=3D region_size */ + mdc->get_plugged_size =3D pc_dimm_md_get_region_size; + mdc->get_region_size =3D pc_dimm_md_get_region_size; + mdc->fill_device_info =3D pc_dimm_md_fill_device_info; } =20 static TypeInfo pc_dimm_info =3D { @@ -466,6 +469,10 @@ static TypeInfo pc_dimm_info =3D { .instance_init =3D pc_dimm_init, .class_init =3D pc_dimm_class_init, .class_size =3D sizeof(PCDIMMDeviceClass), + .interfaces =3D (InterfaceInfo[]) { + { TYPE_MEMORY_DEVICE }, + { } + }, }; =20 static void pc_dimm_register_types(void) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index a81570e7c8..a7428f7da7 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -74,6 +74,7 @@ #include "hw/compat.h" #include "qemu/cutils.h" #include "hw/ppc/spapr_cpu_core.h" +#include "hw/mem/memory-device.h" =20 #include =20 @@ -722,7 +723,7 @@ static int spapr_populate_drconf_memory(sPAPRMachineSta= te *spapr, void *fdt) } =20 if (hotplug_lmb_start) { - dimms =3D qmp_pc_dimm_device_list(); + dimms =3D qmp_memory_device_list(); } =20 /* ibm,dynamic-memory */ diff --git a/hw/ppc/spapr_hcall.c b/hw/ppc/spapr_hcall.c index 16bccdd5c0..4cdae3ca3a 100644 --- a/hw/ppc/spapr_hcall.c +++ b/hw/ppc/spapr_hcall.c @@ -14,6 +14,7 @@ #include "kvm_ppc.h" #include "hw/ppc/spapr_ovec.h" #include "mmu-book3s-v3.h" +#include "hw/mem/memory-device.h" =20 struct SPRSyncState { int spr; diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h new file mode 100644 index 0000000000..3e498b2e61 --- /dev/null +++ b/include/hw/mem/memory-device.h @@ -0,0 +1,44 @@ +/* + * Memory Device Interface + * + * Copyright (c) 2018 Red Hat, Inc. + * + * Authors: + * David Hildenbrand + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#ifndef MEMORY_DEVICE_H +#define MEMORY_DEVICE_H + +#include "qom/object.h" +#include "hw/qdev.h" + +#define TYPE_MEMORY_DEVICE "memory-device" + +#define MEMORY_DEVICE_CLASS(klass) \ + OBJECT_CLASS_CHECK(MemoryDeviceClass, (klass), TYPE_MEMORY_DEVICE) +#define MEMORY_DEVICE_GET_CLASS(obj) \ + OBJECT_GET_CLASS(MemoryDeviceClass, (obj), TYPE_MEMORY_DEVICE) +#define MEMORY_DEVICE(obj) \ + INTERFACE_CHECK(MemoryDeviceState, (obj), TYPE_MEMORY_DEVICE) + +typedef struct MemoryDeviceState { + Object parent_obj; +} MemoryDeviceState; + +typedef struct MemoryDeviceClass { + InterfaceClass parent_class; + + uint64_t (*get_addr)(MemoryDeviceState *md); + uint64_t (*get_plugged_size)(MemoryDeviceState *md, Error **errp); + uint64_t (*get_region_size)(MemoryDeviceState *md, Error **errp); + void (*fill_device_info)(MemoryDeviceState *md, MemoryDeviceInfo *info= ); +} MemoryDeviceClass; + +MemoryDeviceInfoList *qmp_memory_device_list(void); +uint64_t get_plugged_memory_size(void); + +#endif diff --git a/include/hw/mem/pc-dimm.h b/include/hw/mem/pc-dimm.h index 1fc479281c..e88073321f 100644 --- a/include/hw/mem/pc-dimm.h +++ b/include/hw/mem/pc-dimm.h @@ -93,9 +93,7 @@ uint64_t pc_dimm_get_free_addr(uint64_t address_space_sta= rt, =20 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp); =20 -MemoryDeviceInfoList *qmp_pc_dimm_device_list(void); uint64_t pc_existing_dimms_capacity(Error **errp); -uint64_t get_plugged_memory_size(void); void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, MemoryRegion *mr, uint64_t align, Error **errp); void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms, diff --git a/numa.c b/numa.c index 1116c90af9..6a0eaebc01 100644 --- a/numa.c +++ b/numa.c @@ -36,6 +36,7 @@ #include "hw/boards.h" #include "sysemu/hostmem.h" #include "hw/mem/pc-dimm.h" +#include "hw/mem/memory-device.h" #include "qemu/option.h" #include "qemu/config-file.h" #include "qemu/cutils.h" @@ -520,7 +521,7 @@ void memory_region_allocate_system_memory(MemoryRegion = *mr, Object *owner, =20 static void numa_stat_memory_devices(NumaNodeMem node_mem[]) { - MemoryDeviceInfoList *info_list =3D qmp_pc_dimm_device_list(); + MemoryDeviceInfoList *info_list =3D qmp_memory_device_list(); MemoryDeviceInfoList *info; PCDIMMDeviceInfo *pcdimm_info; =20 diff --git a/qmp.c b/qmp.c index f72261667f..3de029946a 100644 --- a/qmp.c +++ b/qmp.c @@ -39,7 +39,7 @@ #include "qapi/qobject-input-visitor.h" #include "hw/boards.h" #include "qom/object_interfaces.h" -#include "hw/mem/pc-dimm.h" +#include "hw/mem/memory-device.h" #include "hw/acpi/acpi_dev_interface.h" =20 NameInfo *qmp_query_name(Error **errp) @@ -731,7 +731,7 @@ void qmp_object_del(const char *id, Error **errp) =20 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp) { - return qmp_pc_dimm_device_list(); + return qmp_memory_device_list(); } =20 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp) diff --git a/stubs/Makefile.objs b/stubs/Makefile.objs index 2d59d84091..53d3f32cb2 100644 --- a/stubs/Makefile.objs +++ b/stubs/Makefile.objs @@ -34,7 +34,7 @@ stub-obj-y +=3D uuid.o stub-obj-y +=3D vm-stop.o stub-obj-y +=3D vmstate.o stub-obj-$(CONFIG_WIN32) +=3D fd-register.o -stub-obj-y +=3D qmp_pc_dimm.o +stub-obj-y +=3D qmp_memory_device.o stub-obj-y +=3D target-monitor-defs.o stub-obj-y +=3D target-get-monitor-def.o stub-obj-y +=3D pc_madt_cpu_entry.o diff --git a/stubs/qmp_pc_dimm.c b/stubs/qmp_memory_device.c similarity index 61% rename from stubs/qmp_pc_dimm.c rename to stubs/qmp_memory_device.c index b6b2cca89e..85ff8f2d7e 100644 --- a/stubs/qmp_pc_dimm.c +++ b/stubs/qmp_memory_device.c @@ -1,8 +1,8 @@ #include "qemu/osdep.h" #include "qom/object.h" -#include "hw/mem/pc-dimm.h" +#include "hw/mem/memory-device.h" =20 -MemoryDeviceInfoList *qmp_pc_dimm_device_list(void) +MemoryDeviceInfoList *qmp_memory_device_list(void) { return NULL; } --=20 2.14.3 From nobody Fri Apr 19 02:08:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1523544074599371.02312857236006; Thu, 12 Apr 2018 07:41:14 -0700 (PDT) Received: from localhost ([::1]:44553 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6dPV-0003TV-Pu for importer@patchew.org; Thu, 12 Apr 2018 10:41:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37228) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6dLN-0008KZ-2z for qemu-devel@nongnu.org; Thu, 12 Apr 2018 10:36:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f6dLL-0007df-IJ for qemu-devel@nongnu.org; Thu, 12 Apr 2018 10:36:57 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49932 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f6dLL-0007dJ-At; Thu, 12 Apr 2018 10:36:55 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D290A406805A; Thu, 12 Apr 2018 14:36:54 +0000 (UTC) Received: from t460s.redhat.com (unknown [10.36.118.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B37631208F8E; Thu, 12 Apr 2018 14:36:52 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Thu, 12 Apr 2018 16:36:34 +0200 Message-Id: <20180412143635.19949-3-david@redhat.com> In-Reply-To: <20180412143635.19949-1-david@redhat.com> References: <20180412143635.19949-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 12 Apr 2018 14:36:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 12 Apr 2018 14:36:54 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 2/3] machine: make MemoryHotplugState accessible via the machine X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Markus Armbruster , qemu-s390x@nongnu.org, qemu-ppc@nongnu.org, Paolo Bonzini , Marcel Apfelbaum , Igor Mammedov , David Gibson , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's allow to query the MemoryHotplugState from the machine. This allows us to generically detect if a certain machine has support for memory devices, and to generically manage it (find free address range, plug/unplug a memory region). Signed-off-by: David Hildenbrand --- hw/i386/pc.c | 12 ++++++++++++ hw/ppc/spapr.c | 12 ++++++++++++ include/hw/boards.h | 16 ++++++++++++++++ include/hw/mem/pc-dimm.h | 12 +----------- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index d36bac8c89..fa8862af33 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -2337,6 +2337,17 @@ static void x86_nmi(NMIState *n, int cpu_index, Erro= r **errp) } } =20 +static MemoryHotplugState *pc_machine_get_memory_hotplug_state(MachineStat= e *ms) +{ + PCMachineState *pcms =3D PC_MACHINE(ms); + + if (!pcms->hotplug_memory.base) { + /* hotplug not supported */ + return NULL; + } + return &pcms->hotplug_memory; +} + static void pc_machine_class_init(ObjectClass *oc, void *data) { MachineClass *mc =3D MACHINE_CLASS(oc); @@ -2376,6 +2387,7 @@ static void pc_machine_class_init(ObjectClass *oc, vo= id *data) hc->unplug =3D pc_machine_device_unplug_cb; nc->nmi_monitor_handler =3D x86_nmi; mc->default_cpu_type =3D TARGET_DEFAULT_CPU_TYPE; + mc->get_memory_hotplug_state =3D pc_machine_get_memory_hotplug_state; =20 object_class_property_add(oc, PC_MACHINE_MEMHP_REGION_SIZE, "int", pc_machine_get_hotplug_memory_region_size, NULL, diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index a7428f7da7..7ccdb705b3 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -3868,6 +3868,17 @@ PowerPCCPU *spapr_find_cpu(int vcpu_id) return NULL; } =20 +static MemoryHotplugState *spapr_get_memory_hotplug_state(MachineState *ms) +{ + sPAPRMachineState *spapr =3D SPAPR_MACHINE(ms); + + if (!spapr->hotplug_memory.base) { + /* hotplug not supported */ + return NULL; + } + return &spapr->hotplug_memory; +} + static void spapr_machine_class_init(ObjectClass *oc, void *data) { MachineClass *mc =3D MACHINE_CLASS(oc); @@ -3927,6 +3938,7 @@ static void spapr_machine_class_init(ObjectClass *oc,= void *data) * in which LMBs are represented and hot-added */ mc->numa_mem_align_shift =3D 28; + mc->get_memory_hotplug_state =3D spapr_get_memory_hotplug_state; =20 smc->default_caps.caps[SPAPR_CAP_HTM] =3D SPAPR_CAP_OFF; smc->default_caps.caps[SPAPR_CAP_VSX] =3D SPAPR_CAP_ON; diff --git a/include/hw/boards.h b/include/hw/boards.h index a609239112..447bdc7219 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -105,6 +105,17 @@ typedef struct { CPUArchId cpus[0]; } CPUArchIdList; =20 +/** + * MemoryHotplugState: + * @base: address in guest physical address space where hotplug memory + * address space begins. + * @mr: hotplug memory address space container + */ +typedef struct MemoryHotplugState { + hwaddr base; + MemoryRegion mr; +} MemoryHotplugState; + /** * MachineClass: * @max_cpus: maximum number of CPUs supported. Default: 1 @@ -156,6 +167,10 @@ typedef struct { * should instead use "unimplemented-device" for all memory ranges where * the guest will attempt to probe for a device that QEMU doesn't * implement and a stub device is required. + * @get_memory_hotplug_state: + * If a machine support memory hotplug, the returned data ontains + * information about the portion of guest physical address space + * where memory devices can be mapped to (e.g. to hotplug a pc-dimm). */ struct MachineClass { /*< private >*/ @@ -212,6 +227,7 @@ struct MachineClass { unsigned cpu_inde= x); const CPUArchIdList *(*possible_cpu_arch_ids)(MachineState *machine); int64_t (*get_default_cpu_node_id)(const MachineState *ms, int idx); + MemoryHotplugState *(*get_memory_hotplug_state)(MachineState *ms); }; =20 /** diff --git a/include/hw/mem/pc-dimm.h b/include/hw/mem/pc-dimm.h index e88073321f..8bda37adab 100644 --- a/include/hw/mem/pc-dimm.h +++ b/include/hw/mem/pc-dimm.h @@ -19,6 +19,7 @@ #include "exec/memory.h" #include "sysemu/hostmem.h" #include "hw/qdev.h" +#include "hw/boards.h" =20 #define TYPE_PC_DIMM "pc-dimm" #define PC_DIMM(obj) \ @@ -75,17 +76,6 @@ typedef struct PCDIMMDeviceClass { MemoryRegion *(*get_vmstate_memory_region)(PCDIMMDevice *dimm); } PCDIMMDeviceClass; =20 -/** - * MemoryHotplugState: - * @base: address in guest physical address space where hotplug memory - * address space begins. - * @mr: hotplug memory address space container - */ -typedef struct MemoryHotplugState { - hwaddr base; - MemoryRegion mr; -} MemoryHotplugState; - uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, uint64_t address_space_size, uint64_t *hint, uint64_t align, uint64_t si= ze, --=20 2.14.3 From nobody Fri Apr 19 02:08:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1523543981795452.0486306054238; Thu, 12 Apr 2018 07:39:41 -0700 (PDT) Received: from localhost ([::1]:44460 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6dO0-0001r3-SZ for importer@patchew.org; Thu, 12 Apr 2018 10:39:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37318) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f6dLQ-0008NY-Ed for qemu-devel@nongnu.org; Thu, 12 Apr 2018 10:37:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f6dLN-0007fW-Ti for qemu-devel@nongnu.org; Thu, 12 Apr 2018 10:37:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48612 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f6dLN-0007fG-Ml; Thu, 12 Apr 2018 10:36:57 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 39E9981A88A6; Thu, 12 Apr 2018 14:36:57 +0000 (UTC) Received: from t460s.redhat.com (unknown [10.36.118.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1959B1208F8E; Thu, 12 Apr 2018 14:36:54 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Thu, 12 Apr 2018 16:36:35 +0200 Message-Id: <20180412143635.19949-4-david@redhat.com> In-Reply-To: <20180412143635.19949-1-david@redhat.com> References: <20180412143635.19949-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 12 Apr 2018 14:36:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 12 Apr 2018 14:36:57 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 3/3] pc-dimm: factor out address space logic into MemoryDevice code X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Markus Armbruster , qemu-s390x@nongnu.org, qemu-ppc@nongnu.org, Paolo Bonzini , Marcel Apfelbaum , Igor Mammedov , David Gibson , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" To be able to reuse MemoryDevice logic from other devices besides pc-dimm, factor the relevant stuff out into the MemoryDevice code. As we don't care about slots for memory devices that are not pc-dimm, don't factor that part out. Most of this patch just moves checks and logic around. While at it, make the code properly detect certain error conditions better (e.g. fragmented memory). Signed-off-by: David Hildenbrand --- hw/i386/pc.c | 12 +-- hw/mem/memory-device.c | 162 ++++++++++++++++++++++++++++++++++++ hw/mem/pc-dimm.c | 185 +++----------------------------------= ---- hw/ppc/spapr.c | 9 +- include/hw/mem/memory-device.h | 4 + include/hw/mem/pc-dimm.h | 14 +--- 6 files changed, 185 insertions(+), 201 deletions(-) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index fa8862af33..1c25546a0c 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1711,7 +1711,7 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev, goto out; } =20 - pc_dimm_memory_plug(dev, &pcms->hotplug_memory, mr, align, &local_err); + pc_dimm_memory_plug(dev, align, &local_err); if (local_err) { goto out; } @@ -1761,17 +1761,9 @@ static void pc_dimm_unplug(HotplugHandler *hotplug_d= ev, DeviceState *dev, Error **errp) { PCMachineState *pcms =3D PC_MACHINE(hotplug_dev); - PCDIMMDevice *dimm =3D PC_DIMM(dev); - PCDIMMDeviceClass *ddc =3D PC_DIMM_GET_CLASS(dimm); - MemoryRegion *mr; HotplugHandlerClass *hhc; Error *local_err =3D NULL; =20 - mr =3D ddc->get_memory_region(dimm, &local_err); - if (local_err) { - goto out; - } - hhc =3D HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev); hhc->unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err); =20 @@ -1779,7 +1771,7 @@ static void pc_dimm_unplug(HotplugHandler *hotplug_de= v, goto out; } =20 - pc_dimm_memory_unplug(dev, &pcms->hotplug_memory, mr); + pc_dimm_memory_unplug(dev); object_unparent(OBJECT(dev)); =20 out: diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 0e0d6b539a..611c3252f0 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -15,6 +15,8 @@ #include "qapi/error.h" #include "hw/boards.h" #include "qemu/range.h" +#include "hw/virtio/vhost.h" +#include "sysemu/kvm.h" =20 static gint memory_device_addr_sort(gconstpointer a, gconstpointer b) { @@ -105,6 +107,166 @@ uint64_t get_plugged_memory_size(void) return size; } =20 +static int memory_device_used_region_size_internal(Object *obj, void *opaq= ue) +{ + uint64_t *size =3D opaque; + + if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { + DeviceState *dev =3D DEVICE(obj); + MemoryDeviceState *md =3D MEMORY_DEVICE(obj); + MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(obj); + + if (dev->realized) { + *size +=3D mdc->get_region_size(md, &error_abort); + } + } + + object_child_foreach(obj, memory_device_used_region_size_internal, opa= que); + return 0; +} + +static uint64_t memory_device_used_region_size(void) +{ + uint64_t size =3D 0; + + memory_device_used_region_size_internal(qdev_get_machine(), &size); + + return size; +} + +uint64_t memory_device_get_free_addr(uint64_t *hint, uint64_t align, + uint64_t size, Error **errp) +{ + const uint64_t used_region_size =3D memory_device_used_region_size(); + uint64_t address_space_start, address_space_end; + MachineState *machine =3D MACHINE(qdev_get_machine()); + MachineClass *mc =3D MACHINE_GET_CLASS(machine); + MemoryHotplugState *hpms; + GSList *list =3D NULL, *item; + uint64_t new_addr =3D 0; + + if (!mc->get_memory_hotplug_state) { + error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " + "supported by the machine"); + return 0; + } + + hpms =3D mc->get_memory_hotplug_state(machine); + if (!hpms || !memory_region_size(&hpms->mr)) { + error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " + "enabled, please specify the maxmem option"); + return 0; + } + address_space_start =3D hpms->base; + address_space_end =3D address_space_start + memory_region_size(&hpms->= mr); + g_assert(address_space_end >=3D address_space_start); + + if (used_region_size + size > machine->maxram_size - machine->ram_size= ) { + error_setg(errp, "not enough space, currently 0x%" PRIx64 + " in use of total hot pluggable 0x" RAM_ADDR_FMT, + used_region_size, machine->maxram_size - machine->ram_s= ize); + return 0; + } + + if (hint && QEMU_ALIGN_UP(*hint, align) !=3D *hint) { + error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", + align); + return 0; + } + + if (QEMU_ALIGN_UP(size, align) !=3D size) { + error_setg(errp, "backend memory size must be multiple of 0x%" + PRIx64, align); + return 0; + } + + if (hint) { + new_addr =3D *hint; + if (new_addr < address_space_start) { + error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 + "] at 0x%" PRIx64, new_addr, size, address_space_st= art); + return 0; + } else if ((new_addr + size) > address_space_end) { + error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 + "] beyond 0x%" PRIx64, new_addr, size, + address_space_end); + return 0; + } + } else { + new_addr =3D address_space_start; + } + + /* find address range that will fit new memory device */ + object_child_foreach(qdev_get_machine(), memory_device_built_list, &li= st); + for (item =3D list; item; item =3D g_slist_next(item)) { + MemoryDeviceState *md =3D item->data; + MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(OBJECT(md)); + uint64_t md_size, md_addr; + + md_addr =3D mdc->get_addr(md); + md_size =3D mdc->get_region_size(md, errp); + if (*errp) { + goto out; + } + + if (ranges_overlap(md_addr, md_size, new_addr, size)) { + if (hint) { + DeviceState *d =3D DEVICE(md); + error_setg(errp, "address range conflicts with '%s'", d->i= d); + goto out; + } + new_addr =3D QEMU_ALIGN_UP(md_addr + md_size, align); + } + } + + if (new_addr + size > address_space_end) { + error_setg(errp, "could not find position in guest address space f= or " + "memory device - memory fragmented due to alignments"); + goto out; + } +out: + g_slist_free(list); + return new_addr; +} + +void memory_device_plug_region(MemoryRegion *mr, uint64_t addr, Error **er= rp) +{ + MachineState *machine =3D MACHINE(qdev_get_machine()); + MachineClass *mc =3D MACHINE_GET_CLASS(machine); + MemoryHotplugState *hpms; + + /* we expect a previous call to memory_device_get_free_addr() */ + g_assert(mc->get_memory_hotplug_state); + hpms =3D mc->get_memory_hotplug_state(machine); + g_assert(hpms); + + /* we will need a new memory slot for kvm and vhost */ + if (kvm_enabled() && !kvm_has_free_slot(machine)) { + error_setg(errp, "hypervisor has no free memory slots left"); + return; + } + if (!vhost_has_free_slot()) { + error_setg(errp, "a used vhost backend has no free memory slots le= ft"); + return; + } + + memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); +} + +void memory_device_unplug_region(MemoryRegion *mr) +{ + MachineState *machine =3D MACHINE(qdev_get_machine()); + MachineClass *mc =3D MACHINE_GET_CLASS(machine); + MemoryHotplugState *hpms; + + /* we expect a previous call to memory_device_get_free_addr() */ + g_assert(mc->get_memory_hotplug_state); + hpms =3D mc->get_memory_hotplug_state(machine); + g_assert(hpms); + + memory_region_del_subregion(&hpms->mr, mr); +} + static const TypeInfo memory_device_info =3D { .name =3D TYPE_MEMORY_DEVICE, .parent =3D TYPE_INTERFACE, diff --git a/hw/mem/pc-dimm.c b/hw/mem/pc-dimm.c index 1dbf699e02..cf23ab5d76 100644 --- a/hw/mem/pc-dimm.c +++ b/hw/mem/pc-dimm.c @@ -25,19 +25,10 @@ #include "qapi/error.h" #include "qemu/config-file.h" #include "qapi/visitor.h" -#include "qemu/range.h" #include "sysemu/numa.h" -#include "sysemu/kvm.h" #include "trace.h" -#include "hw/virtio/vhost.h" =20 -typedef struct pc_dimms_capacity { - uint64_t size; - Error **errp; -} pc_dimms_capacity; - -void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, - MemoryRegion *mr, uint64_t align, Error **errp) +void pc_dimm_memory_plug(DeviceState *dev, uint64_t align, Error **errp) { int slot; MachineState *machine =3D MACHINE(qdev_get_machine()); @@ -45,37 +36,26 @@ void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplu= gState *hpms, PCDIMMDeviceClass *ddc =3D PC_DIMM_GET_CLASS(dimm); MemoryRegion *vmstate_mr =3D ddc->get_vmstate_memory_region(dimm); Error *local_err =3D NULL; - uint64_t existing_dimms_capacity =3D 0; + MemoryRegion *mr; uint64_t addr; =20 - addr =3D object_property_get_uint(OBJECT(dimm), - PC_DIMM_ADDR_PROP, &local_err); + mr =3D ddc->get_memory_region(dimm, &local_err); if (local_err) { goto out; } =20 - addr =3D pc_dimm_get_free_addr(hpms->base, - memory_region_size(&hpms->mr), - !addr ? NULL : &addr, align, - memory_region_size(mr), &local_err); + addr =3D object_property_get_uint(OBJECT(dimm), + PC_DIMM_ADDR_PROP, &local_err); if (local_err) { goto out; } =20 - existing_dimms_capacity =3D pc_existing_dimms_capacity(&local_err); + addr =3D memory_device_get_free_addr(!addr ? NULL : &addr, align, + memory_region_size(mr), &local_err); if (local_err) { goto out; } =20 - if (existing_dimms_capacity + memory_region_size(mr) > - machine->maxram_size - machine->ram_size) { - error_setg(&local_err, "not enough space, currently 0x%" PRIx64 - " in use of total hot pluggable 0x" RAM_ADDR_FMT, - existing_dimms_capacity, - machine->maxram_size - machine->ram_size); - goto out; - } - object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_= err); if (local_err) { goto out; @@ -98,67 +78,27 @@ void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplu= gState *hpms, } trace_mhp_pc_dimm_assigned_slot(slot); =20 - if (kvm_enabled() && !kvm_has_free_slot(machine)) { - error_setg(&local_err, "hypervisor has no free memory slots left"); - goto out; - } - - if (!vhost_has_free_slot()) { - error_setg(&local_err, "a used vhost backend has no free" - " memory slots left"); + memory_device_plug_region(mr, addr, &local_err); + if (local_err) { goto out; } - - memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); vmstate_register_ram(vmstate_mr, dev); =20 out: error_propagate(errp, local_err); } =20 -void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms, - MemoryRegion *mr) +void pc_dimm_memory_unplug(DeviceState *dev) { PCDIMMDevice *dimm =3D PC_DIMM(dev); PCDIMMDeviceClass *ddc =3D PC_DIMM_GET_CLASS(dimm); MemoryRegion *vmstate_mr =3D ddc->get_vmstate_memory_region(dimm); + MemoryRegion *mr =3D ddc->get_memory_region(dimm, &error_abort); =20 - memory_region_del_subregion(&hpms->mr, mr); + memory_device_unplug_region(mr); vmstate_unregister_ram(vmstate_mr, dev); } =20 -static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) -{ - pc_dimms_capacity *cap =3D opaque; - uint64_t *size =3D &cap->size; - - if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { - DeviceState *dev =3D DEVICE(obj); - - if (dev->realized) { - (*size) +=3D object_property_get_uint(obj, PC_DIMM_SIZE_PROP, - cap->errp); - } - - if (cap->errp && *cap->errp) { - return 1; - } - } - object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque); - return 0; -} - -uint64_t pc_existing_dimms_capacity(Error **errp) -{ - pc_dimms_capacity cap; - - cap.size =3D 0; - cap.errp =3D errp; - - pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap); - return cap.size; -} - static int pc_dimm_slot2bitmap(Object *obj, void *opaque) { unsigned long *bitmap =3D opaque; @@ -205,107 +145,6 @@ out: return slot; } =20 -static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b) -{ - PCDIMMDevice *x =3D PC_DIMM(a); - PCDIMMDevice *y =3D PC_DIMM(b); - Int128 diff =3D int128_sub(int128_make64(x->addr), int128_make64(y->ad= dr)); - - if (int128_lt(diff, int128_zero())) { - return -1; - } else if (int128_gt(diff, int128_zero())) { - return 1; - } - return 0; -} - -static int pc_dimm_built_list(Object *obj, void *opaque) -{ - GSList **list =3D opaque; - - if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { - DeviceState *dev =3D DEVICE(obj); - if (dev->realized) { /* only realized DIMMs matter */ - *list =3D g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort); - } - } - - object_child_foreach(obj, pc_dimm_built_list, opaque); - return 0; -} - -uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, - uint64_t address_space_size, - uint64_t *hint, uint64_t align, uint64_t si= ze, - Error **errp) -{ - GSList *list =3D NULL, *item; - uint64_t new_addr, ret =3D 0; - uint64_t address_space_end =3D address_space_start + address_space_siz= e; - - g_assert(QEMU_ALIGN_UP(address_space_start, align) =3D=3D address_spac= e_start); - - if (!address_space_size) { - error_setg(errp, "memory hotplug is not enabled, " - "please add maxmem option"); - goto out; - } - - if (hint && QEMU_ALIGN_UP(*hint, align) !=3D *hint) { - error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", - align); - goto out; - } - - if (QEMU_ALIGN_UP(size, align) !=3D size) { - error_setg(errp, "backend memory size must be multiple of 0x%" - PRIx64, align); - goto out; - } - - assert(address_space_end > address_space_start); - object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list); - - if (hint) { - new_addr =3D *hint; - } else { - new_addr =3D address_space_start; - } - - /* find address range that will fit new DIMM */ - for (item =3D list; item; item =3D g_slist_next(item)) { - PCDIMMDevice *dimm =3D item->data; - uint64_t dimm_size =3D object_property_get_uint(OBJECT(dimm), - PC_DIMM_SIZE_PROP, - errp); - if (errp && *errp) { - goto out; - } - - if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) { - if (hint) { - DeviceState *d =3D DEVICE(dimm); - error_setg(errp, "address range conflicts with '%s'", d->i= d); - goto out; - } - new_addr =3D QEMU_ALIGN_UP(dimm->addr + dimm_size, align); - } - } - ret =3D new_addr; - - if (new_addr < address_space_start) { - error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 - "] at 0x%" PRIx64, new_addr, size, address_space_start); - } else if ((new_addr + size) > address_space_end) { - error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 - "] beyond 0x%" PRIx64, new_addr, size, address_space_en= d); - } - -out: - g_slist_free(list); - return ret; -} - static Property pc_dimm_properties[] =3D { DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 7ccdb705b3..7757a49335 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -3041,7 +3041,7 @@ static void spapr_memory_plug(HotplugHandler *hotplug= _dev, DeviceState *dev, align =3D memory_region_get_alignment(mr); size =3D memory_region_size(mr); =20 - pc_dimm_memory_plug(dev, &ms->hotplug_memory, mr, align, &local_err); + pc_dimm_memory_plug(dev, align, &local_err); if (local_err) { goto out; } @@ -3062,7 +3062,7 @@ static void spapr_memory_plug(HotplugHandler *hotplug= _dev, DeviceState *dev, return; =20 out_unplug: - pc_dimm_memory_unplug(dev, &ms->hotplug_memory, mr); + pc_dimm_memory_unplug(dev); out: error_propagate(errp, local_err); } @@ -3180,9 +3180,6 @@ static sPAPRDIMMState *spapr_recover_pending_dimm_sta= te(sPAPRMachineState *ms, void spapr_lmb_release(DeviceState *dev) { sPAPRMachineState *spapr =3D SPAPR_MACHINE(qdev_get_hotplug_handler(de= v)); - PCDIMMDevice *dimm =3D PC_DIMM(dev); - PCDIMMDeviceClass *ddc =3D PC_DIMM_GET_CLASS(dimm); - MemoryRegion *mr =3D ddc->get_memory_region(dimm, &error_abort); sPAPRDIMMState *ds =3D spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(= dev)); =20 /* This information will get lost if a migration occurs @@ -3202,7 +3199,7 @@ void spapr_lmb_release(DeviceState *dev) * Now that all the LMBs have been removed by the guest, call the * pc-dimm unplug handler to cleanup up the pc-dimm device. */ - pc_dimm_memory_unplug(dev, &spapr->hotplug_memory, mr); + pc_dimm_memory_unplug(dev); object_unparent(OBJECT(dev)); spapr_pending_dimm_unplugs_remove(spapr, ds); } diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h index 3e498b2e61..722620da24 100644 --- a/include/hw/mem/memory-device.h +++ b/include/hw/mem/memory-device.h @@ -40,5 +40,9 @@ typedef struct MemoryDeviceClass { =20 MemoryDeviceInfoList *qmp_memory_device_list(void); uint64_t get_plugged_memory_size(void); +uint64_t memory_device_get_free_addr(uint64_t *hint, uint64_t align, + uint64_t size, Error **errp); +void memory_device_plug_region(MemoryRegion *mr, uint64_t addr, Error **er= rp); +void memory_device_unplug_region(MemoryRegion *mr); =20 #endif diff --git a/include/hw/mem/pc-dimm.h b/include/hw/mem/pc-dimm.h index 8bda37adab..2e7c2abe35 100644 --- a/include/hw/mem/pc-dimm.h +++ b/include/hw/mem/pc-dimm.h @@ -19,7 +19,6 @@ #include "exec/memory.h" #include "sysemu/hostmem.h" #include "hw/qdev.h" -#include "hw/boards.h" =20 #define TYPE_PC_DIMM "pc-dimm" #define PC_DIMM(obj) \ @@ -76,16 +75,7 @@ typedef struct PCDIMMDeviceClass { MemoryRegion *(*get_vmstate_memory_region)(PCDIMMDevice *dimm); } PCDIMMDeviceClass; =20 -uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, - uint64_t address_space_size, - uint64_t *hint, uint64_t align, uint64_t si= ze, - Error **errp); - int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp); - -uint64_t pc_existing_dimms_capacity(Error **errp); -void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, - MemoryRegion *mr, uint64_t align, Error **errp); -void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms, - MemoryRegion *mr); +void pc_dimm_memory_plug(DeviceState *dev, uint64_t align, Error **errp); +void pc_dimm_memory_unplug(DeviceState *dev); #endif --=20 2.14.3