hw/core: Extract hotplug-related functions to qdev-hotplug.c

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Yanan Wang <wangyanan55@huawei.com>
Acked-by: Eduardo Habkost <ehabkost@redhat.com>
Message-Id: <20211028150521.1973821-4-philmd@redhat.com>
This commit is contained in:
Philippe Mathieu-Daudé 2021-10-28 16:36:38 +02:00
parent f73fb06395
commit 81c7b38155
3 changed files with 74 additions and 60 deletions

View File

@ -11,6 +11,7 @@ hwcore_ss.add(files(
'irq.c', 'irq.c',
'clock.c', 'clock.c',
'qdev-clock.c', 'qdev-clock.c',
'qdev-hotplug.c',
)) ))
common_ss.add(files('cpu-common.c')) common_ss.add(files('cpu-common.c'))

73
hw/core/qdev-hotplug.c Normal file
View File

@ -0,0 +1,73 @@
/*
* QDev Hotplug handlers
*
* Copyright (c) Red Hat
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "hw/qdev-core.h"
#include "hw/boards.h"
HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->get_hotplug_handler) {
return mc->get_hotplug_handler(machine, dev);
}
}
return NULL;
}
bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->hotplug_allowed) {
return mc->hotplug_allowed(machine, dev, errp);
}
}
return true;
}
HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
{
if (dev->parent_bus) {
return dev->parent_bus->hotplug_handler;
}
return NULL;
}
HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
{
HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
if (hotplug_ctrl == NULL && dev->parent_bus) {
hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
}
return hotplug_ctrl;
}
/* can be used as ->unplug() callback for the simple cases */
void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
DeviceState *dev, Error **errp)
{
qdev_unrealize(dev);
}

View File

@ -33,7 +33,6 @@
#include "qapi/visitor.h" #include "qapi/visitor.h"
#include "qemu/error-report.h" #include "qemu/error-report.h"
#include "qemu/option.h" #include "qemu/option.h"
#include "hw/hotplug.h"
#include "hw/irq.h" #include "hw/irq.h"
#include "hw/qdev-properties.h" #include "hw/qdev-properties.h"
#include "hw/boards.h" #include "hw/boards.h"
@ -238,58 +237,6 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
dev->alias_required_for_version = required_for_version; dev->alias_required_for_version = required_for_version;
} }
HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->get_hotplug_handler) {
return mc->get_hotplug_handler(machine, dev);
}
}
return NULL;
}
bool qdev_hotplug_allowed(DeviceState *dev, Error **errp)
{
MachineState *machine;
MachineClass *mc;
Object *m_obj = qdev_get_machine();
if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {
machine = MACHINE(m_obj);
mc = MACHINE_GET_CLASS(machine);
if (mc->hotplug_allowed) {
return mc->hotplug_allowed(machine, dev, errp);
}
}
return true;
}
HotplugHandler *qdev_get_bus_hotplug_handler(DeviceState *dev)
{
if (dev->parent_bus) {
return dev->parent_bus->hotplug_handler;
}
return NULL;
}
HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)
{
HotplugHandler *hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);
if (hotplug_ctrl == NULL && dev->parent_bus) {
hotplug_ctrl = qdev_get_bus_hotplug_handler(dev);
}
return hotplug_ctrl;
}
static int qdev_prereset(DeviceState *dev, void *opaque) static int qdev_prereset(DeviceState *dev, void *opaque)
{ {
trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev))); trace_qdev_reset_tree(dev, object_get_typename(OBJECT(dev)));
@ -371,13 +318,6 @@ static void device_reset_child_foreach(Object *obj, ResettableChildCallback cb,
} }
} }
/* can be used as ->unplug() callback for the simple cases */
void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
DeviceState *dev, Error **errp)
{
qdev_unrealize(dev);
}
bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
{ {
assert(!dev->realized && !dev->parent_bus); assert(!dev->realized && !dev->parent_bus);