qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 18/35] dimm: add busy slot check and slot auto-alloc


From: Igor Mammedov
Subject: [Qemu-devel] [PATCH 18/35] dimm: add busy slot check and slot auto-allocation
Date: Fri, 4 Apr 2014 15:36:43 +0200

- if slot property is not specified on -device/device_add command,
treat default value as request for assigning DimmDevice to
the first free slot.

- if slot is provided with -device/device_add command, attempt to
use it or fail command if it's already occupied.

Signed-off-by: Igor Mammedov <address@hidden>
---
 hw/i386/pc.c          | 15 +++++++++++++++
 hw/mem/dimm.c         | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/mem/dimm.h |  2 ++
 3 files changed, 64 insertions(+)

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 29382ec..e3e63bb 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -1489,6 +1489,8 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev,
     DimmDeviceClass *ddc = DIMM_GET_CLASS(dimm);
     MemoryRegion *mr = ddc->get_memory_region(dimm);
     ram_addr_t addr = dimm->start;
+    MachineState *machine = MACHINE(hotplug_dev);
+    int slot = dimm->slot;
 
     addr = dimm_get_free_addr(pcms->hotplug_memory_base,
                               memory_region_size(&pcms->hotplug_memory),
@@ -1498,6 +1500,19 @@ static void pc_dimm_plug(HotplugHandler *hotplug_dev,
         goto out;
     }
     object_property_set_int(OBJECT(dev), addr, "start", &local_err);
+    if (local_err) {
+        goto out;
+    }
+
+    slot = dimm_get_free_slot(slot < 0 ? NULL : &slot,
+                              machine->init_args.ram_slots, &local_err);
+    if (local_err) {
+        goto out;
+    }
+    object_property_set_int(OBJECT(dev), slot, "slot", &local_err);
+    if (local_err) {
+        goto out;
+    }
 
     memory_region_add_subregion(&pcms->hotplug_memory,
                                 addr - pcms->hotplug_memory_base,
diff --git a/hw/mem/dimm.c b/hw/mem/dimm.c
index a10bd07..16fdf62 100644
--- a/hw/mem/dimm.c
+++ b/hw/mem/dimm.c
@@ -23,6 +23,53 @@
 #include "qapi/visitor.h"
 #include "qemu/range.h"
 
+static int dimm_slot2bitmap(Object *obj, void *opaque)
+{
+    unsigned long *bitmap = opaque;
+
+    if (object_dynamic_cast(obj, TYPE_DIMM)) {
+        DeviceState *dev = DEVICE(obj);
+        if (dev->realized) { /* count only realized DIMMs */
+            DimmDevice *d = DIMM(obj);
+            set_bit(d->slot, bitmap);
+        }
+    }
+
+    object_child_foreach(obj, dimm_slot2bitmap, opaque);
+    return 0;
+}
+
+int dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
+{
+    unsigned long *bitmap = bitmap_new(max_slots);
+    int slot = 0;
+
+    object_child_foreach(qdev_get_machine(), dimm_slot2bitmap, bitmap);
+
+    /* check if requested slot is not occupied */
+    if (hint) {
+        if (*hint >= max_slots) {
+            error_setg(errp, "invalid slot# %d, should be less than %d",
+                       *hint, max_slots);
+        }
+        if (!test_bit(*hint, bitmap)) {
+            slot = *hint;
+        } else {
+            error_setg(errp, "slot %d is busy", *hint);
+        }
+        goto out;
+    }
+
+    /* search for free slot */
+    slot = find_first_zero_bit(bitmap, max_slots);
+    if (slot == max_slots) {
+        error_setg(errp, "no free slots available");
+    }
+out:
+    g_free(bitmap);
+    return slot;
+}
+
 static gint dimm_addr_sort(gconstpointer a, gconstpointer b)
 {
     DimmDevice *x = DIMM(a);
diff --git a/include/hw/mem/dimm.h b/include/hw/mem/dimm.h
index d6e4d65..fa27208 100644
--- a/include/hw/mem/dimm.h
+++ b/include/hw/mem/dimm.h
@@ -67,4 +67,6 @@ uint64_t dimm_get_free_addr(uint64_t address_space_start,
                             uint64_t address_space_size,
                             uint64_t *hint, uint64_t size,
                             Error **errp);
+
+int dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
 #endif
-- 
1.9.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]