[PATCH 08/43] qdev: add qdev_find_default_bus()

marcandre.lureau@redhat.com posted 43 patches 1 week, 2 days ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, Christian Schoenebeck <qemu_oss@crudebyte.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Thomas Huth <huth@tuxfamily.org>, Alexandre Ratchov <alex@caoua.org>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Peter Maydell <peter.maydell@linaro.org>, Jan Kiszka <jan.kiszka@web.de>, Alistair Francis <alistair@alistair23.me>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Laurent Vivier <laurent@vivier.eu>, "Michael S. Tsirkin" <mst@redhat.com>, Manos Pitsidianakis <manos.pitsidianakis@linaro.org>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, "Hervé Poussineau" <hpoussin@reactos.org>, BALATON Zoltan <balaton@eik.bme.hu>, Jiaxun Yang <jiaxun.yang@flygoat.com>, "Alex Bennée" <alex.bennee@linaro.org>
There is a newer version of this series
[PATCH 08/43] qdev: add qdev_find_default_bus()
Posted by marcandre.lureau@redhat.com 1 week, 2 days ago
From: Marc-André Lureau <marcandre.lureau@redhat.com>

This helper is used next by -audio code.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/monitor/qdev.h |  3 +++
 system/qdev-monitor.c  | 21 +++++++++++++++++----
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/include/monitor/qdev.h b/include/monitor/qdev.h
index 1d57bf6577..de33637869 100644
--- a/include/monitor/qdev.h
+++ b/include/monitor/qdev.h
@@ -1,6 +1,8 @@
 #ifndef MONITOR_QDEV_H
 #define MONITOR_QDEV_H
 
+#include "hw/qdev-core.h"
+
 /*** monitor commands ***/
 
 void hmp_info_qtree(Monitor *mon, const QDict *qdict);
@@ -11,6 +13,7 @@ int qdev_device_help(QemuOpts *opts);
 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp);
 DeviceState *qdev_device_add_from_qdict(const QDict *opts,
                                         bool from_json, Error **errp);
+BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp);
 
 /**
  * qdev_set_id: parent the device and set its id if provided.
diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c
index 2ac92d0a07..4b732f579a 100644
--- a/system/qdev-monitor.c
+++ b/system/qdev-monitor.c
@@ -621,6 +621,21 @@ const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
     return prop->name;
 }
 
+BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp)
+{
+    BusState *bus = NULL;
+
+    assert(dc->bus_type != NULL);
+    bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
+    if (!bus || qbus_is_full(bus)) {
+        error_setg(errp, "No '%s' bus found for device '%s'",
+                   dc->bus_type, object_class_get_name(OBJECT_CLASS(dc)));
+        return NULL;
+    }
+
+    return bus;
+}
+
 DeviceState *qdev_device_add_from_qdict(const QDict *opts,
                                         bool from_json, Error **errp)
 {
@@ -657,10 +672,8 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,
             return NULL;
         }
     } else if (dc->bus_type != NULL) {
-        bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
-        if (!bus || qbus_is_full(bus)) {
-            error_setg(errp, "No '%s' bus found for device '%s'",
-                       dc->bus_type, driver);
+        bus = qdev_find_default_bus(dc, errp);
+        if (!bus) {
             return NULL;
         }
     }
-- 
2.51.0


Re: [PATCH 08/43] qdev: add qdev_find_default_bus()
Posted by BALATON Zoltan 1 week, 2 days ago
On Tue, 21 Oct 2025, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> This helper is used next by -audio code.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> include/monitor/qdev.h |  3 +++
> system/qdev-monitor.c  | 21 +++++++++++++++++----
> 2 files changed, 20 insertions(+), 4 deletions(-)
>
> diff --git a/include/monitor/qdev.h b/include/monitor/qdev.h
> index 1d57bf6577..de33637869 100644
> --- a/include/monitor/qdev.h
> +++ b/include/monitor/qdev.h
> @@ -1,6 +1,8 @@
> #ifndef MONITOR_QDEV_H
> #define MONITOR_QDEV_H
>
> +#include "hw/qdev-core.h"
> +
> /*** monitor commands ***/
>
> void hmp_info_qtree(Monitor *mon, const QDict *qdict);
> @@ -11,6 +13,7 @@ int qdev_device_help(QemuOpts *opts);
> DeviceState *qdev_device_add(QemuOpts *opts, Error **errp);
> DeviceState *qdev_device_add_from_qdict(const QDict *opts,
>                                         bool from_json, Error **errp);
> +BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp);
>
> /**
>  * qdev_set_id: parent the device and set its id if provided.
> diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c
> index 2ac92d0a07..4b732f579a 100644
> --- a/system/qdev-monitor.c
> +++ b/system/qdev-monitor.c
> @@ -621,6 +621,21 @@ const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
>     return prop->name;
> }
>
> +BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp)
> +{
> +    BusState *bus = NULL;
> +
> +    assert(dc->bus_type != NULL);
> +    bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
> +    if (!bus || qbus_is_full(bus)) {
> +        error_setg(errp, "No '%s' bus found for device '%s'",
> +                   dc->bus_type, object_class_get_name(OBJECT_CLASS(dc)));
> +        return NULL;

Should it differrentiate between no bus and a bus that exists but full and 
return different error to help debugging?

Regards,
BALATON Zoltan

> +    }
> +
> +    return bus;
> +}
> +
> DeviceState *qdev_device_add_from_qdict(const QDict *opts,
>                                         bool from_json, Error **errp)
> {
> @@ -657,10 +672,8 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,
>             return NULL;
>         }
>     } else if (dc->bus_type != NULL) {
> -        bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
> -        if (!bus || qbus_is_full(bus)) {
> -            error_setg(errp, "No '%s' bus found for device '%s'",
> -                       dc->bus_type, driver);
> +        bus = qdev_find_default_bus(dc, errp);
> +        if (!bus) {
>             return NULL;
>         }
>     }
>
Re: [PATCH 08/43] qdev: add qdev_find_default_bus()
Posted by Marc-André Lureau 1 week, 2 days ago
Hi

On Tue, Oct 21, 2025 at 3:24 PM BALATON Zoltan <balaton@eik.bme.hu> wrote:
>
> On Tue, 21 Oct 2025, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > This helper is used next by -audio code.
> >
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> > include/monitor/qdev.h |  3 +++
> > system/qdev-monitor.c  | 21 +++++++++++++++++----
> > 2 files changed, 20 insertions(+), 4 deletions(-)
> >
> > diff --git a/include/monitor/qdev.h b/include/monitor/qdev.h
> > index 1d57bf6577..de33637869 100644
> > --- a/include/monitor/qdev.h
> > +++ b/include/monitor/qdev.h
> > @@ -1,6 +1,8 @@
> > #ifndef MONITOR_QDEV_H
> > #define MONITOR_QDEV_H
> >
> > +#include "hw/qdev-core.h"
> > +
> > /*** monitor commands ***/
> >
> > void hmp_info_qtree(Monitor *mon, const QDict *qdict);
> > @@ -11,6 +13,7 @@ int qdev_device_help(QemuOpts *opts);
> > DeviceState *qdev_device_add(QemuOpts *opts, Error **errp);
> > DeviceState *qdev_device_add_from_qdict(const QDict *opts,
> >                                         bool from_json, Error **errp);
> > +BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp);
> >
> > /**
> >  * qdev_set_id: parent the device and set its id if provided.
> > diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c
> > index 2ac92d0a07..4b732f579a 100644
> > --- a/system/qdev-monitor.c
> > +++ b/system/qdev-monitor.c
> > @@ -621,6 +621,21 @@ const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
> >     return prop->name;
> > }
> >
> > +BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp)
> > +{
> > +    BusState *bus = NULL;
> > +
> > +    assert(dc->bus_type != NULL);
> > +    bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
> > +    if (!bus || qbus_is_full(bus)) {
> > +        error_setg(errp, "No '%s' bus found for device '%s'",
> > +                   dc->bus_type, object_class_get_name(OBJECT_CLASS(dc)));
> > +        return NULL;
>
> Should it differrentiate between no bus and a bus that exists but full and
> return different error to help debugging?
>

qdev_device_add_from_qdict() doesn't distinguish it either, but it
could be useful indeed. I will report a different error.

> Regards,
> BALATON Zoltan
>
> > +    }
> > +
> > +    return bus;
> > +}
> > +
> > DeviceState *qdev_device_add_from_qdict(const QDict *opts,
> >                                         bool from_json, Error **errp)
> > {
> > @@ -657,10 +672,8 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,
> >             return NULL;
> >         }
> >     } else if (dc->bus_type != NULL) {
> > -        bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
> > -        if (!bus || qbus_is_full(bus)) {
> > -            error_setg(errp, "No '%s' bus found for device '%s'",
> > -                       dc->bus_type, driver);
> > +        bus = qdev_find_default_bus(dc, errp);
> > +        if (!bus) {
> >             return NULL;
> >         }
> >     }
> >



-- 
Marc-André Lureau