diff mbox

[v5,1/2] Add device listener interface

Message ID 1417776605-36309-2-git-send-email-paul.durrant@citrix.com
State New
Headers show

Commit Message

Paul Durrant Dec. 5, 2014, 10:50 a.m. UTC
The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device
models explicitly register with Xen for config space accesses. This patch
adds a listener interface into qdev-core which can be used by the Xen
interface code to monitor for arrival and departure of PCI devices.

Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Andreas Faerber" <afaerber@suse.de>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Cc: Thomas Huth <thuth@linux.vnet.ibm.com>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
---
 hw/core/qdev.c          |   53 +++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/qdev-core.h  |   10 +++++++++
 include/qemu/typedefs.h |    1 +
 3 files changed, 64 insertions(+)

Comments

Paolo Bonzini Dec. 5, 2014, 11:44 a.m. UTC | #1
On 05/12/2014 11:50, Paul Durrant wrote:
> The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device
> models explicitly register with Xen for config space accesses. This patch
> adds a listener interface into qdev-core which can be used by the Xen
> interface code to monitor for arrival and departure of PCI devices.
> 
> Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
> Cc: Michael S. Tsirkin <mst@redhat.com>
> Cc: Andreas Faerber" <afaerber@suse.de>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
> Cc: Igor Mammedov <imammedo@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: Thomas Huth <thuth@linux.vnet.ibm.com>
> Cc: Christian Borntraeger <borntraeger@de.ibm.com>
> ---
>  hw/core/qdev.c          |   53 +++++++++++++++++++++++++++++++++++++++++++++++
>  include/hw/qdev-core.h  |   10 +++++++++
>  include/qemu/typedefs.h |    1 +
>  3 files changed, 64 insertions(+)
> 
> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> index 35fd00d..76ff9ef 100644
> --- a/hw/core/qdev.c
> +++ b/hw/core/qdev.c
> @@ -189,6 +189,56 @@ int qdev_init(DeviceState *dev)
>      return 0;
>  }
>  
> +static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
> +    = QTAILQ_HEAD_INITIALIZER(device_listeners);
> +
> +enum ListenerDirection { Forward, Reverse };
> +
> +#define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
> +    do {                                                          \
> +        DeviceListener *_listener;                                \
> +                                                                  \
> +        switch (_direction) {                                     \
> +        case Forward:                                             \
> +            QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
> +                if (_listener->_callback) {                       \
> +                    _listener->_callback(_listener, ##_args);     \
> +                }                                                 \
> +            }                                                     \
> +            break;                                                \
> +        case Reverse:                                             \
> +            QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
> +                                   device_listeners, link) {      \
> +                if (_listener->_callback) {                       \
> +                    _listener->_callback(_listener, ##_args);     \
> +                }                                                 \
> +            }                                                     \
> +            break;                                                \
> +        default:                                                  \
> +            abort();                                              \
> +        }                                                         \
> +    } while (0)
> +
> +static int device_listener_add(DeviceState *dev, void *opaque)
> +{
> +    DEVICE_LISTENER_CALL(realize, Forward, dev);
> +
> +    return 0;
> +}
> +
> +void device_listener_register(DeviceListener *listener)
> +{
> +    QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
> +
> +    qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
> +                       NULL, NULL);
> +}
> +
> +void device_listener_unregister(DeviceListener *listener)
> +{
> +    QTAILQ_REMOVE(&device_listeners, listener, link);
> +}
> +
>  static void device_realize(DeviceState *dev, Error **errp)
>  {
>      DeviceClass *dc = DEVICE_GET_CLASS(dev);
> @@ -994,6 +1044,8 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
>              goto fail;
>          }
>  
> +        DEVICE_LISTENER_CALL(realize, Forward, dev);
> +
>          hotplug_ctrl = qdev_get_hotplug_handler(dev);
>          if (hotplug_ctrl) {
>              hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
> @@ -1035,6 +1087,7 @@ static void device_set_realized(Object *obj, bool value, Error **errp)
>              dc->unrealize(dev, local_errp);
>          }
>          dev->pending_deleted_event = true;
> +        DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
>      }
>  
>      if (local_err != NULL) {
> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> index 589bbe7..15a226f 100644
> --- a/include/hw/qdev-core.h
> +++ b/include/hw/qdev-core.h
> @@ -165,6 +165,12 @@ struct DeviceState {
>      int alias_required_for_version;
>  };
>  
> +struct DeviceListener {
> +    void (*realize)(DeviceListener *listener, DeviceState *dev);
> +    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
> +    QTAILQ_ENTRY(DeviceListener) link;
> +};
> +
>  #define TYPE_BUS "bus"
>  #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
>  #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
> @@ -376,4 +382,8 @@ static inline bool qbus_is_hotpluggable(BusState *bus)
>  {
>     return bus->hotplug_handler;
>  }
> +
> +void device_listener_register(DeviceListener *listener);
> +void device_listener_unregister(DeviceListener *listener);
> +
>  #endif
> diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
> index 3475177..4bb4938 100644
> --- a/include/qemu/typedefs.h
> +++ b/include/qemu/typedefs.h
> @@ -20,6 +20,7 @@ typedef struct Property Property;
>  typedef struct PropertyInfo PropertyInfo;
>  typedef struct CompatProperty CompatProperty;
>  typedef struct DeviceState DeviceState;
> +typedef struct DeviceListener DeviceListener;
>  typedef struct BusState BusState;
>  typedef struct BusClass BusClass;
>  
> 

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

Thanks!

Paolo
Paul Durrant Dec. 8, 2014, 11:12 a.m. UTC | #2
> -----Original Message-----
> From: Paolo Bonzini [mailto:pbonzini@redhat.com]
> Sent: 05 December 2014 11:45
> To: Paul Durrant; qemu-devel@nongnu.org
> Cc: Michael S. Tsirkin; Andreas Faerber"; Peter Crosthwaite; Igor
> Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
> Subject: Re: [PATCH v5 1/2] Add device listener interface
> 
> 
> 
> On 05/12/2014 11:50, Paul Durrant wrote:
> > The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device
> > models explicitly register with Xen for config space accesses. This patch
> > adds a listener interface into qdev-core which can be used by the Xen
> > interface code to monitor for arrival and departure of PCI devices.
> >
> > Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
> > Cc: Michael S. Tsirkin <mst@redhat.com>
> > Cc: Andreas Faerber" <afaerber@suse.de>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
> > Cc: Igor Mammedov <imammedo@redhat.com>
> > Cc: Markus Armbruster <armbru@redhat.com>
> > Cc: Thomas Huth <thuth@linux.vnet.ibm.com>
> > Cc: Christian Borntraeger <borntraeger@de.ibm.com>
> > ---
> >  hw/core/qdev.c          |   53
> +++++++++++++++++++++++++++++++++++++++++++++++
> >  include/hw/qdev-core.h  |   10 +++++++++
> >  include/qemu/typedefs.h |    1 +
> >  3 files changed, 64 insertions(+)
> >
> > diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> > index 35fd00d..76ff9ef 100644
> > --- a/hw/core/qdev.c
> > +++ b/hw/core/qdev.c
> > @@ -189,6 +189,56 @@ int qdev_init(DeviceState *dev)
> >      return 0;
> >  }
> >
> > +static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
> > +    = QTAILQ_HEAD_INITIALIZER(device_listeners);
> > +
> > +enum ListenerDirection { Forward, Reverse };
> > +
> > +#define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
> > +    do {                                                          \
> > +        DeviceListener *_listener;                                \
> > +                                                                  \
> > +        switch (_direction) {                                     \
> > +        case Forward:                                             \
> > +            QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
> > +                if (_listener->_callback) {                       \
> > +                    _listener->_callback(_listener, ##_args);     \
> > +                }                                                 \
> > +            }                                                     \
> > +            break;                                                \
> > +        case Reverse:                                             \
> > +            QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
> > +                                   device_listeners, link) {      \
> > +                if (_listener->_callback) {                       \
> > +                    _listener->_callback(_listener, ##_args);     \
> > +                }                                                 \
> > +            }                                                     \
> > +            break;                                                \
> > +        default:                                                  \
> > +            abort();                                              \
> > +        }                                                         \
> > +    } while (0)
> > +
> > +static int device_listener_add(DeviceState *dev, void *opaque)
> > +{
> > +    DEVICE_LISTENER_CALL(realize, Forward, dev);
> > +
> > +    return 0;
> > +}
> > +
> > +void device_listener_register(DeviceListener *listener)
> > +{
> > +    QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
> > +
> > +    qbus_walk_children(sysbus_get_default(), NULL, NULL,
> device_listener_add,
> > +                       NULL, NULL);
> > +}
> > +
> > +void device_listener_unregister(DeviceListener *listener)
> > +{
> > +    QTAILQ_REMOVE(&device_listeners, listener, link);
> > +}
> > +
> >  static void device_realize(DeviceState *dev, Error **errp)
> >  {
> >      DeviceClass *dc = DEVICE_GET_CLASS(dev);
> > @@ -994,6 +1044,8 @@ static void device_set_realized(Object *obj, bool
> value, Error **errp)
> >              goto fail;
> >          }
> >
> > +        DEVICE_LISTENER_CALL(realize, Forward, dev);
> > +
> >          hotplug_ctrl = qdev_get_hotplug_handler(dev);
> >          if (hotplug_ctrl) {
> >              hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
> > @@ -1035,6 +1087,7 @@ static void device_set_realized(Object *obj, bool
> value, Error **errp)
> >              dc->unrealize(dev, local_errp);
> >          }
> >          dev->pending_deleted_event = true;
> > +        DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
> >      }
> >
> >      if (local_err != NULL) {
> > diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> > index 589bbe7..15a226f 100644
> > --- a/include/hw/qdev-core.h
> > +++ b/include/hw/qdev-core.h
> > @@ -165,6 +165,12 @@ struct DeviceState {
> >      int alias_required_for_version;
> >  };
> >
> > +struct DeviceListener {
> > +    void (*realize)(DeviceListener *listener, DeviceState *dev);
> > +    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
> > +    QTAILQ_ENTRY(DeviceListener) link;
> > +};
> > +
> >  #define TYPE_BUS "bus"
> >  #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
> >  #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
> TYPE_BUS)
> > @@ -376,4 +382,8 @@ static inline bool qbus_is_hotpluggable(BusState
> *bus)
> >  {
> >     return bus->hotplug_handler;
> >  }
> > +
> > +void device_listener_register(DeviceListener *listener);
> > +void device_listener_unregister(DeviceListener *listener);
> > +
> >  #endif
> > diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
> > index 3475177..4bb4938 100644
> > --- a/include/qemu/typedefs.h
> > +++ b/include/qemu/typedefs.h
> > @@ -20,6 +20,7 @@ typedef struct Property Property;
> >  typedef struct PropertyInfo PropertyInfo;
> >  typedef struct CompatProperty CompatProperty;
> >  typedef struct DeviceState DeviceState;
> > +typedef struct DeviceListener DeviceListener;
> >  typedef struct BusState BusState;
> >  typedef struct BusClass BusClass;
> >
> >
> 
> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
> 

Great. Thanks for that. All I need now is an ack on this from a maintainer.

  Paul

> Thanks!
> 
> Paolo
Paolo Bonzini Dec. 9, 2014, 5:40 p.m. UTC | #3
On 08/12/2014 12:12, Paul Durrant wrote:
>> -----Original Message-----
>> From: Paolo Bonzini [mailto:pbonzini@redhat.com]
>> Sent: 05 December 2014 11:45
>> To: Paul Durrant; qemu-devel@nongnu.org
>> Cc: Michael S. Tsirkin; Andreas Faerber"; Peter Crosthwaite; Igor
>> Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
>> Subject: Re: [PATCH v5 1/2] Add device listener interface
>>
>>
>>
>> On 05/12/2014 11:50, Paul Durrant wrote:
>>> The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device
>>> models explicitly register with Xen for config space accesses. This patch
>>> adds a listener interface into qdev-core which can be used by the Xen
>>> interface code to monitor for arrival and departure of PCI devices.
>>>
>>> Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
>>> Cc: Michael S. Tsirkin <mst@redhat.com>
>>> Cc: Andreas Faerber" <afaerber@suse.de>
>>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>>> Cc: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
>>> Cc: Igor Mammedov <imammedo@redhat.com>
>>> Cc: Markus Armbruster <armbru@redhat.com>
>>> Cc: Thomas Huth <thuth@linux.vnet.ibm.com>
>>> Cc: Christian Borntraeger <borntraeger@de.ibm.com>
>>> ---
>>>  hw/core/qdev.c          |   53
>> +++++++++++++++++++++++++++++++++++++++++++++++
>>>  include/hw/qdev-core.h  |   10 +++++++++
>>>  include/qemu/typedefs.h |    1 +
>>>  3 files changed, 64 insertions(+)
>>>
>>> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
>>> index 35fd00d..76ff9ef 100644
>>> --- a/hw/core/qdev.c
>>> +++ b/hw/core/qdev.c
>>> @@ -189,6 +189,56 @@ int qdev_init(DeviceState *dev)
>>>      return 0;
>>>  }
>>>
>>> +static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
>>> +    = QTAILQ_HEAD_INITIALIZER(device_listeners);
>>> +
>>> +enum ListenerDirection { Forward, Reverse };
>>> +
>>> +#define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
>>> +    do {                                                          \
>>> +        DeviceListener *_listener;                                \
>>> +                                                                  \
>>> +        switch (_direction) {                                     \
>>> +        case Forward:                                             \
>>> +            QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
>>> +                if (_listener->_callback) {                       \
>>> +                    _listener->_callback(_listener, ##_args);     \
>>> +                }                                                 \
>>> +            }                                                     \
>>> +            break;                                                \
>>> +        case Reverse:                                             \
>>> +            QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
>>> +                                   device_listeners, link) {      \
>>> +                if (_listener->_callback) {                       \
>>> +                    _listener->_callback(_listener, ##_args);     \
>>> +                }                                                 \
>>> +            }                                                     \
>>> +            break;                                                \
>>> +        default:                                                  \
>>> +            abort();                                              \
>>> +        }                                                         \
>>> +    } while (0)
>>> +
>>> +static int device_listener_add(DeviceState *dev, void *opaque)
>>> +{
>>> +    DEVICE_LISTENER_CALL(realize, Forward, dev);
>>> +
>>> +    return 0;
>>> +}
>>> +
>>> +void device_listener_register(DeviceListener *listener)
>>> +{
>>> +    QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
>>> +
>>> +    qbus_walk_children(sysbus_get_default(), NULL, NULL,
>> device_listener_add,
>>> +                       NULL, NULL);
>>> +}
>>> +
>>> +void device_listener_unregister(DeviceListener *listener)
>>> +{
>>> +    QTAILQ_REMOVE(&device_listeners, listener, link);
>>> +}
>>> +
>>>  static void device_realize(DeviceState *dev, Error **errp)
>>>  {
>>>      DeviceClass *dc = DEVICE_GET_CLASS(dev);
>>> @@ -994,6 +1044,8 @@ static void device_set_realized(Object *obj, bool
>> value, Error **errp)
>>>              goto fail;
>>>          }
>>>
>>> +        DEVICE_LISTENER_CALL(realize, Forward, dev);
>>> +
>>>          hotplug_ctrl = qdev_get_hotplug_handler(dev);
>>>          if (hotplug_ctrl) {
>>>              hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
>>> @@ -1035,6 +1087,7 @@ static void device_set_realized(Object *obj, bool
>> value, Error **errp)
>>>              dc->unrealize(dev, local_errp);
>>>          }
>>>          dev->pending_deleted_event = true;
>>> +        DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
>>>      }
>>>
>>>      if (local_err != NULL) {
>>> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
>>> index 589bbe7..15a226f 100644
>>> --- a/include/hw/qdev-core.h
>>> +++ b/include/hw/qdev-core.h
>>> @@ -165,6 +165,12 @@ struct DeviceState {
>>>      int alias_required_for_version;
>>>  };
>>>
>>> +struct DeviceListener {
>>> +    void (*realize)(DeviceListener *listener, DeviceState *dev);
>>> +    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
>>> +    QTAILQ_ENTRY(DeviceListener) link;
>>> +};
>>> +
>>>  #define TYPE_BUS "bus"
>>>  #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
>>>  #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
>> TYPE_BUS)
>>> @@ -376,4 +382,8 @@ static inline bool qbus_is_hotpluggable(BusState
>> *bus)
>>>  {
>>>     return bus->hotplug_handler;
>>>  }
>>> +
>>> +void device_listener_register(DeviceListener *listener);
>>> +void device_listener_unregister(DeviceListener *listener);
>>> +
>>>  #endif
>>> diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
>>> index 3475177..4bb4938 100644
>>> --- a/include/qemu/typedefs.h
>>> +++ b/include/qemu/typedefs.h
>>> @@ -20,6 +20,7 @@ typedef struct Property Property;
>>>  typedef struct PropertyInfo PropertyInfo;
>>>  typedef struct CompatProperty CompatProperty;
>>>  typedef struct DeviceState DeviceState;
>>> +typedef struct DeviceListener DeviceListener;
>>>  typedef struct BusState BusState;
>>>  typedef struct BusClass BusClass;
>>>
>>>
>>
>> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
>>
> 
> Great. Thanks for that. All I need now is an ack on this from a maintainer.

I think my reviewed-by is pretty close to an Ack.

Stefano, feel free to pick this patch up together with 2/2.

Paolo
Andreas Färber Dec. 9, 2014, 5:54 p.m. UTC | #4
Am 09.12.2014 um 18:40 schrieb Paolo Bonzini:
> On 08/12/2014 12:12, Paul Durrant wrote:
>>> -----Original Message-----
>>> From: Paolo Bonzini [mailto:pbonzini@redhat.com]
>>> Sent: 05 December 2014 11:45
>>> To: Paul Durrant; qemu-devel@nongnu.org
>>> Cc: Michael S. Tsirkin; Andreas Faerber"; Peter Crosthwaite; Igor
>>> Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
>>> Subject: Re: [PATCH v5 1/2] Add device listener interface
>>>
>>>
>>>
>>> On 05/12/2014 11:50, Paul Durrant wrote:
>>>> The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device
>>>> models explicitly register with Xen for config space accesses. This patch
>>>> adds a listener interface into qdev-core which can be used by the Xen
>>>> interface code to monitor for arrival and departure of PCI devices.
>>>>
>>>> Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
>>>> Cc: Michael S. Tsirkin <mst@redhat.com>
>>>> Cc: Andreas Faerber" <afaerber@suse.de>
>>>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>>>> Cc: Peter Crosthwaite <peter.crosthwaite@xilinx.com>
>>>> Cc: Igor Mammedov <imammedo@redhat.com>
>>>> Cc: Markus Armbruster <armbru@redhat.com>
>>>> Cc: Thomas Huth <thuth@linux.vnet.ibm.com>
>>>> Cc: Christian Borntraeger <borntraeger@de.ibm.com>
>>>> ---
>>>>  hw/core/qdev.c          |   53
>>> +++++++++++++++++++++++++++++++++++++++++++++++
>>>>  include/hw/qdev-core.h  |   10 +++++++++
>>>>  include/qemu/typedefs.h |    1 +
>>>>  3 files changed, 64 insertions(+)
>>>>
>>>> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
>>>> index 35fd00d..76ff9ef 100644
>>>> --- a/hw/core/qdev.c
>>>> +++ b/hw/core/qdev.c
>>>> @@ -189,6 +189,56 @@ int qdev_init(DeviceState *dev)
>>>>      return 0;
>>>>  }
>>>>
>>>> +static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
>>>> +    = QTAILQ_HEAD_INITIALIZER(device_listeners);
>>>> +
>>>> +enum ListenerDirection { Forward, Reverse };
>>>> +
>>>> +#define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
>>>> +    do {                                                          \
>>>> +        DeviceListener *_listener;                                \
>>>> +                                                                  \
>>>> +        switch (_direction) {                                     \
>>>> +        case Forward:                                             \
>>>> +            QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
>>>> +                if (_listener->_callback) {                       \
>>>> +                    _listener->_callback(_listener, ##_args);     \
>>>> +                }                                                 \
>>>> +            }                                                     \
>>>> +            break;                                                \
>>>> +        case Reverse:                                             \
>>>> +            QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
>>>> +                                   device_listeners, link) {      \
>>>> +                if (_listener->_callback) {                       \
>>>> +                    _listener->_callback(_listener, ##_args);     \
>>>> +                }                                                 \
>>>> +            }                                                     \
>>>> +            break;                                                \
>>>> +        default:                                                  \
>>>> +            abort();                                              \
>>>> +        }                                                         \
>>>> +    } while (0)
>>>> +
>>>> +static int device_listener_add(DeviceState *dev, void *opaque)
>>>> +{
>>>> +    DEVICE_LISTENER_CALL(realize, Forward, dev);
>>>> +
>>>> +    return 0;
>>>> +}
>>>> +
>>>> +void device_listener_register(DeviceListener *listener)
>>>> +{
>>>> +    QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
>>>> +
>>>> +    qbus_walk_children(sysbus_get_default(), NULL, NULL,
>>> device_listener_add,
>>>> +                       NULL, NULL);
>>>> +}
>>>> +
>>>> +void device_listener_unregister(DeviceListener *listener)
>>>> +{
>>>> +    QTAILQ_REMOVE(&device_listeners, listener, link);
>>>> +}
>>>> +
>>>>  static void device_realize(DeviceState *dev, Error **errp)
>>>>  {
>>>>      DeviceClass *dc = DEVICE_GET_CLASS(dev);
>>>> @@ -994,6 +1044,8 @@ static void device_set_realized(Object *obj, bool
>>> value, Error **errp)
>>>>              goto fail;
>>>>          }
>>>>
>>>> +        DEVICE_LISTENER_CALL(realize, Forward, dev);
>>>> +
>>>>          hotplug_ctrl = qdev_get_hotplug_handler(dev);
>>>>          if (hotplug_ctrl) {
>>>>              hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
>>>> @@ -1035,6 +1087,7 @@ static void device_set_realized(Object *obj, bool
>>> value, Error **errp)
>>>>              dc->unrealize(dev, local_errp);
>>>>          }
>>>>          dev->pending_deleted_event = true;
>>>> +        DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
>>>>      }
>>>>
>>>>      if (local_err != NULL) {
>>>> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
>>>> index 589bbe7..15a226f 100644
>>>> --- a/include/hw/qdev-core.h
>>>> +++ b/include/hw/qdev-core.h
>>>> @@ -165,6 +165,12 @@ struct DeviceState {
>>>>      int alias_required_for_version;
>>>>  };
>>>>
>>>> +struct DeviceListener {
>>>> +    void (*realize)(DeviceListener *listener, DeviceState *dev);
>>>> +    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
>>>> +    QTAILQ_ENTRY(DeviceListener) link;
>>>> +};
>>>> +
>>>>  #define TYPE_BUS "bus"
>>>>  #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
>>>>  #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
>>> TYPE_BUS)
>>>> @@ -376,4 +382,8 @@ static inline bool qbus_is_hotpluggable(BusState
>>> *bus)
>>>>  {
>>>>     return bus->hotplug_handler;
>>>>  }
>>>> +
>>>> +void device_listener_register(DeviceListener *listener);
>>>> +void device_listener_unregister(DeviceListener *listener);
>>>> +
>>>>  #endif
>>>> diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
>>>> index 3475177..4bb4938 100644
>>>> --- a/include/qemu/typedefs.h
>>>> +++ b/include/qemu/typedefs.h
>>>> @@ -20,6 +20,7 @@ typedef struct Property Property;
>>>>  typedef struct PropertyInfo PropertyInfo;
>>>>  typedef struct CompatProperty CompatProperty;
>>>>  typedef struct DeviceState DeviceState;
>>>> +typedef struct DeviceListener DeviceListener;
>>>>  typedef struct BusState BusState;
>>>>  typedef struct BusClass BusClass;
>>>>
>>>>
>>>
>>> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
>>>
>>
>> Great. Thanks for that. All I need now is an ack on this from a maintainer.
> 
> I think my reviewed-by is pretty close to an Ack.
> 
> Stefano, feel free to pick this patch up together with 2/2.

In that case,

Reviewed-by: Andreas Färber <afaerber@suse.de>

If I should still pick it up, just let me know.

Regards,
Andreas
diff mbox

Patch

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 35fd00d..76ff9ef 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -189,6 +189,56 @@  int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(device_listeners, DeviceListener) device_listeners
+    = QTAILQ_HEAD_INITIALIZER(device_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define DEVICE_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                          \
+        DeviceListener *_listener;                                \
+                                                                  \
+        switch (_direction) {                                     \
+        case Forward:                                             \
+            QTAILQ_FOREACH(_listener, &device_listeners, link) {  \
+                if (_listener->_callback) {                       \
+                    _listener->_callback(_listener, ##_args);     \
+                }                                                 \
+            }                                                     \
+            break;                                                \
+        case Reverse:                                             \
+            QTAILQ_FOREACH_REVERSE(_listener, &device_listeners,  \
+                                   device_listeners, link) {      \
+                if (_listener->_callback) {                       \
+                    _listener->_callback(_listener, ##_args);     \
+                }                                                 \
+            }                                                     \
+            break;                                                \
+        default:                                                  \
+            abort();                                              \
+        }                                                         \
+    } while (0)
+
+static int device_listener_add(DeviceState *dev, void *opaque)
+{
+    DEVICE_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void device_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&device_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL, device_listener_add,
+                       NULL, NULL);
+}
+
+void device_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&device_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
@@ -994,6 +1044,8 @@  static void device_set_realized(Object *obj, bool value, Error **errp)
             goto fail;
         }
 
+        DEVICE_LISTENER_CALL(realize, Forward, dev);
+
         hotplug_ctrl = qdev_get_hotplug_handler(dev);
         if (hotplug_ctrl) {
             hotplug_handler_plug(hotplug_ctrl, dev, &local_err);
@@ -1035,6 +1087,7 @@  static void device_set_realized(Object *obj, bool value, Error **errp)
             dc->unrealize(dev, local_errp);
         }
         dev->pending_deleted_event = true;
+        DEVICE_LISTENER_CALL(unrealize, Reverse, dev);
     }
 
     if (local_err != NULL) {
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 589bbe7..15a226f 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -165,6 +165,12 @@  struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
@@ -376,4 +382,8 @@  static inline bool qbus_is_hotpluggable(BusState *bus)
 {
    return bus->hotplug_handler;
 }
+
+void device_listener_register(DeviceListener *listener);
+void device_listener_unregister(DeviceListener *listener);
+
 #endif
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
index 3475177..4bb4938 100644
--- a/include/qemu/typedefs.h
+++ b/include/qemu/typedefs.h
@@ -20,6 +20,7 @@  typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;