memory-device: Refactor memory_device_pre_plug()

Let's move memory_device_check_addable() and basic checks out of
memory_device_get_free_addr() directly into memory_device_pre_plug().

Separating basic checks from address assignment is cleaner and
prepares for further changes.

As all memory device users now use memory_devices_init(), and that
function enforces that the size is 0, we can drop the check for an empty
region.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
Message-Id: <20230623124553.400585-10-david@redhat.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
This commit is contained in:
David Hildenbrand 2023-06-23 14:45:52 +02:00
parent a8e67ce35b
commit d7f4891c85

View file

@ -69,9 +69,10 @@ static int memory_device_used_region_size(Object *obj, void *opaque)
return 0;
}
static void memory_device_check_addable(MachineState *ms, uint64_t size,
static void memory_device_check_addable(MachineState *ms, MemoryRegion *mr,
Error **errp)
{
const uint64_t size = memory_region_size(mr);
uint64_t used_region_size = 0;
/* we will need a new memory slot for kvm and vhost */
@ -101,16 +102,9 @@ static uint64_t memory_device_get_free_addr(MachineState *ms,
uint64_t align, uint64_t size,
Error **errp)
{
Error *err = NULL;
GSList *list = NULL, *item;
Range as, new = range_empty;
if (!ms->device_memory || !memory_region_size(&ms->device_memory->mr)) {
error_setg(errp, "the configuration is not prepared for memory devices"
" (e.g., for memory hotplug), consider specifying the"
" maxmem option");
return 0;
}
range_init_nofail(&as, ms->device_memory->base,
memory_region_size(&ms->device_memory->mr));
@ -122,12 +116,6 @@ static uint64_t memory_device_get_free_addr(MachineState *ms,
align);
}
memory_device_check_addable(ms, size, &err);
if (err) {
error_propagate(errp, err);
return 0;
}
if (hint && !QEMU_IS_ALIGNED(*hint, align)) {
error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
align);
@ -251,11 +239,23 @@ void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms,
uint64_t addr, align = 0;
MemoryRegion *mr;
if (!ms->device_memory) {
error_setg(errp, "the configuration is not prepared for memory devices"
" (e.g., for memory hotplug), consider specifying the"
" maxmem option");
return;
}
mr = mdc->get_memory_region(md, &local_err);
if (local_err) {
goto out;
}
memory_device_check_addable(ms, mr, &local_err);
if (local_err) {
goto out;
}
if (legacy_align) {
align = *legacy_align;
} else {