@@ -12,6 +12,7 @@
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "hw/mem/memory-device.h"
+#include "hw/mem/pc-dimm.h"
#include "qapi/error.h"
#include "hw/boards.h"
#include "qemu/range.h"
@@ -166,6 +167,16 @@ void memory_devices_notify_vhost_device_added(void)
memory_devices_check_memslot_soft_limit(ms);
}
+static void memory_device_set_suggested_memslot_limit(MemoryDeviceState *md,
+ unsigned int limit)
+{
+ const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
+
+ if (mdc->set_suggested_memslot_limit) {
+ mdc->set_suggested_memslot_limit(md, limit);
+ }
+}
+
static unsigned int memory_device_get_memslots(MemoryDeviceState *md)
{
const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md);
@@ -176,13 +187,58 @@ static unsigned int memory_device_get_memslots(MemoryDeviceState *md)
return 1;
}
+/*
+ * Suggested maximum number of memslots for a memory device with the given
+ * region size. Not exceeding this number will make most setups not run
+ * into the soft limit or even out of available memslots, even when multiple
+ * memory devices automatically determine the number of memslots to use.
+ */
+static unsigned int memory_device_suggested_memslot_limit(MachineState *ms,
+ MemoryRegion *mr)
+{
+ const unsigned int soft_limit = memory_devices_memslot_soft_limit(ms);
+ const unsigned int free_dimm_slots = pc_dimm_get_free_slots(ms);
+ const uint64_t size = memory_region_size(mr);
+ uint64_t available_space;
+ unsigned int memslots;
+
+ /* Consider the soft-limit for all memory devices. */
+ if (soft_limit <= ms->device_memory->required_memslots) {
+ return 1;
+ }
+ memslots = soft_limit - ms->device_memory->required_memslots;
+
+ /* Consider the actually available memslots. */
+ memslots = MIN(memslots, get_available_memslots(ms));
+
+ /* It's the single memory device? We cannot plug something else. */
+ if (size == ms->maxram_size - ms->ram_size) {
+ return memslots;
+ }
+
+ /* Try setting one memmemslots for each empty DIMM slot aside. */
+ if (memslots <= free_dimm_slots) {
+ return 1;
+ }
+ memslots -= free_dimm_slots;
+
+ /*
+ * Simple heuristic: equally distribute the memslots over the space
+ * still available for memory devices.
+ */
+ available_space = ms->maxram_size - ms->ram_size -
+ ms->device_memory->used_region_size;
+ memslots = (double)memslots * size / available_space;
+ return memslots < 1 ? 1 : memslots;
+}
+
static void memory_device_check_addable(MachineState *ms, MemoryDeviceState *md,
MemoryRegion *mr, Error **errp)
{
const uint64_t used_region_size = ms->device_memory->used_region_size;
const unsigned int available_memslots = get_available_memslots(ms);
const uint64_t size = memory_region_size(mr);
- unsigned int required_memslots;
+ unsigned int required_memslots, suggested_memslot_limit;
/* will we exceed the total amount of memory specified */
if (used_region_size + size < used_region_size ||
@@ -193,6 +249,14 @@ static void memory_device_check_addable(MachineState *ms, MemoryDeviceState *md,
return;
}
+ /*
+ * Determine the per-device memslot limit for this device and
+ * communicate it to the device such that it can determine the number
+ * of memslots to use before we query them.
+ */
+ suggested_memslot_limit = memory_device_suggested_memslot_limit(ms, mr);
+ memory_device_set_suggested_memslot_limit(md, suggested_memslot_limit);
+
/* ... are there still sufficient memslots available? */
required_memslots = memory_device_get_memslots(md);
if (available_memslots < required_memslots) {
@@ -100,6 +100,16 @@ struct MemoryDeviceClass {
*/
MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp);
+ /*
+ * Optional: Set the suggested memslot limit, such that a device than
+ * can auto-detect the number of memslots to use based on this limit.
+ *
+ * Called exactly once when pre-plugging the memory device, before
+ * querying the number of memslots using @get_memslots the first time.
+ */
+ void (*set_suggested_memslot_limit)(MemoryDeviceState *md,
+ unsigned int limit);
+
/*
* Optional for memory devices that consume only a single memslot,
* required for all other memory devices: Return the number of memslots