>From 2af81231d7a5479f70b1444589b01a47d37145c2 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 4 Apr 2014 15:36:42 +0200 Subject: [PATCH] dimm: add busy address check and address auto-allocation - if 'start' property is not specified on -device/device_add command, treat default value as request for assigning DimmDevice to the first free memory region. - if 'start' is provided with -device/device_add command, attempt to use it or fail command if it's already occupied or falls inside of an existing DimmDevice memory region. Signed-off-by: Igor Mammedov Signed-off-by: Tang Chen --- hw/i386/pc.c | 16 +++++++++++- hw/mem/dimm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/mem/dimm.h | 5 ++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 4038e2c..29382ec 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1483,15 +1483,29 @@ void qemu_register_pc_machine(QEMUMachine *m) static void pc_dimm_plug(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp) { + Error *local_err = NULL; PCMachineState *pcms = PC_MACHINE(hotplug_dev); DimmDevice *dimm = DIMM(dev); DimmDeviceClass *ddc = DIMM_GET_CLASS(dimm); MemoryRegion *mr = ddc->get_memory_region(dimm); + ram_addr_t addr = dimm->start; + + addr = dimm_get_free_addr(pcms->hotplug_memory_base, + memory_region_size(&pcms->hotplug_memory), + !addr ? NULL : &addr, + memory_region_size(mr), &local_err); + if (local_err) { + goto out; + } + object_property_set_int(OBJECT(dev), addr, "start", &local_err); memory_region_add_subregion(&pcms->hotplug_memory, - dimm->start - pcms->hotplug_memory_base, + addr - pcms->hotplug_memory_base, mr); vmstate_register_ram(mr, dev); + +out: + error_propagate(errp, local_err); } static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev, diff --git a/hw/mem/dimm.c b/hw/mem/dimm.c index 65d4cd0..d8a3c5e 100644 --- a/hw/mem/dimm.c +++ b/hw/mem/dimm.c @@ -21,6 +21,76 @@ #include "hw/mem/dimm.h" #include "qemu/config-file.h" #include "qapi/visitor.h" +#include "qemu/range.h" + +static gint dimm_addr_sort(gconstpointer a, gconstpointer b) +{ + DimmDevice *x = DIMM(a); + DimmDevice *y = DIMM(b); + + return x->start - y->start; +} + +static int dimm_built_list(Object *obj, void *opaque) +{ + GSList **list = opaque; + + if (object_dynamic_cast(obj, TYPE_DIMM)) { + DeviceState *dev = DEVICE(obj); + if (dev->realized) { /* only realized DIMMs matter */ + *list = g_slist_insert_sorted(*list, dev, dimm_addr_sort); + } + } + + object_child_foreach(obj, dimm_built_list, opaque); + return 0; +} + +uint64_t dimm_get_free_addr(uint64_t address_space_start, + uint64_t address_space_size, + uint64_t *hint, uint64_t size, + Error **errp) +{ + GSList *list = NULL, *item; + uint64_t new_start, ret = 0; + uint64_t address_space_end = address_space_start + address_space_size; + + object_child_foreach(qdev_get_machine(), dimm_built_list, &list); + + if (hint) { + new_start = *hint; + } else { + new_start = address_space_start; + } + + /* find address range that will fit new DIMM */ + for (item = list; item; item = g_slist_next(item)) { + DimmDevice *dimm = item->data; + uint64_t dimm_size = object_property_get_int(OBJECT(dimm), + "size", &error_abort); + if (ranges_overlap(dimm->start, dimm_size, new_start, size)) { + if (hint) { + DeviceState *d = DEVICE(dimm); + error_setg(errp, "address range conflicts with '%s'", d->id); + goto out; + } + new_start = dimm->start + dimm_size; + } + } + ret = new_start; + + if (new_start < address_space_start) { + error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 + "] at 0x%" PRIx64, new_start, size, address_space_start); + } else if ((new_start + size) > address_space_end) { + error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 + "] beyond 0x%" PRIx64, new_start, size, address_space_end); + } + +out: + g_slist_free(list); + return ret; +} static Property dimm_properties[] = { DEFINE_PROP_UINT64("start", DimmDevice, start, 0), diff --git a/include/hw/mem/dimm.h b/include/hw/mem/dimm.h index 4bbf0ed..d6e4d65 100644 --- a/include/hw/mem/dimm.h +++ b/include/hw/mem/dimm.h @@ -62,4 +62,9 @@ typedef struct DimmDeviceClass { MemoryRegion *(*get_memory_region)(DimmDevice *dimm); } DimmDeviceClass; + +uint64_t dimm_get_free_addr(uint64_t address_space_start, + uint64_t address_space_size, + uint64_t *hint, uint64_t size, + Error **errp); #endif -- 1.8.0