qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro
cover qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro
Experiment after discussing with Thomas around qdev_get_parent_bus:
https://lore.kernel.org/qemu-devel/[email protected]/
When a QDev plug on a QBus, we'll always use qdev_get_parent_bus() at least once with this type. Why not provide a consistent defined macro instead of:
1/ adding an inlined helper such usb_bus_from_device()
or scsi_bus_from_device() with different type checks,
2/ open-code calls to qdev_get_parent_bus() with unsafe casts
?
This RFC series introduce a QDev-equivalent of QOM DECLARE_TYPES macro, to be used with a (device, bus) tuple, and declaring the equivalent device_GET_BUS() macro.
hw/usb/ is converted as an example.
Philippe Mathieu-Daudé (3):
hw/qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro hw/usb: Declare QOM macros using QDEV_DECLARE_DEV_BUS_TYPES() hw/usb: Use USB_DEVICE_GET_BUS() macro
hw/usb/bus.c | 10 +++++----- hw/usb/core.c | 6 +++--- hw/usb/dev-hub.c | 4 ++-- hw/usb/dev-serial.c | 10 +++++----- hw/usb/hcd-xhci.c | 2 +- include/hw/qdev-core.h | 28 ++++++++++++++++++++++++++++ include/hw/usb.h | 13 ++++--------- 7 files changed, 48 insertions(+), 25 deletions(-)
Signature · 1 line
2.38.1
RFC PATCH 1/3 hw/qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro
Similarly to QOM OBJECT_DECLARE_TYPE() equivalent, introduce a QDev macro to declare common helpers for device sitting on a bus.
--- include/hw/qdev-core.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
include/hw/qdev-core.h +28 -0
@@ -717,6 +717,34 @@ void qdev_pass_gpios(DeviceState *dev, DeviceState *container,BusState *qdev_get_parent_bus (const DeviceState *dev); +/** +* QDEV_DECLARE_DEV_BUS_TYPES: +* @DeviceInstanceType: device instance struct name +* @DeviceClassType: device class struct name +* @DEVICE_OBJ_NAME: the device name in uppercase with underscore separators +* @BusInstanceType: bus instance struct name +* @DeviceClassType: bus class struct name +* @BUS_OBJ_NAME: the bus name in uppercase with underscore separators +* +* This macro is typically used in a header file, and will: +* +* - create the typedefs for the object and class structs +* - register the type for use with g_autoptr +* - provide four standard type cast functions +* +* The device state struct, device class struct, bus state struct need +* to be declared manually. +*/ +#define QDEV_DECLARE_DEV_BUS_TYPES (DeviceInstanceType, DeviceClassType, \ +DEVICE_OBJ_NAME , \ + BusInstanceType,BUS_OBJ_NAME ) \ + OBJECT_DECLARE_TYPE(DeviceInstanceType, DeviceClassType, DEVICE_OBJ_NAME) \ + OBJECT_DECLARE_SIMPLE_TYPE(BusInstanceType, BUS_OBJ_NAME) \ + \ + static inline G_GNUC_UNUSED BusInstanceType * \ + DEVICE_OBJ_NAME##_GET_BUS(const DeviceInstanceType *dev) \ + { return BUS_OBJ_NAME(qdev_get_parent_bus(DEVICE(dev))); } +/*** BUS API. ***/ DeviceState *qdev_find_recursive (BusState *bus,const char *id);
Signature · 1 line
2.38.1
Re: [RFC PATCH 1/3] hw/qdev: Introduce QDEV_DECLARE_DEV_BUS_TYPES() macro
Quoted 30 lines · Philippe Mathieu-Daudé
Similarly to QOM
OBJECT_DECLARE_TYPE()equivalent, introduce a QDev macro to declare common helpers for device sitting on a bus.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/hw/qdev-core.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)include/hw/qdev-core.h +13
-0@@ -717,6 +717,34 @@ void qdev_pass_gpios(DeviceState *dev, DeviceState *container,BusState *qdev_get_parent_bus (const DeviceState *dev); +/** +* QDEV_DECLARE_DEV_BUS_TYPES: +* @DeviceInstanceType: device instance struct name +* @DeviceClassType: device class struct name +* @DEVICE_OBJ_NAME: the device name in uppercase with underscore separators +* @BusInstanceType: bus instance struct name +* @DeviceClassType: bus class struct name +* @BUS_OBJ_NAME: the bus name in uppercase with underscore separators +* +* This macro is typically used in a header file, and will: +* +* - create the typedefs for the object and class structs +* - register the type for use with g_autoptr
include/hw/qdev-core.h void qdev_pass_gpios(DeviceState *dev, DeviceState *container, · line 742
Quoted 10 lines
include/hw/qdev-core.h +10
-0+* - provide four standard type cast functions +* +* The device state struct, device class struct, bus state struct need +* to be declared manually. +*/ +#define QDEV_DECLARE_DEV_BUS_TYPES (DeviceInstanceType, DeviceClassType, \ +DEVICE_OBJ_NAME , \ + BusInstanceType,BUS_OBJ_NAME ) \ + OBJECT_DECLARE_TYPE(DeviceInstanceType, DeviceClassType, DEVICE_OBJ_NAME) \ + OBJECT_DECLARE_SIMPLE_TYPE(BusInstanceType, BUS_OBJ_NAME) \
I'm not especially a fan of putting the declaration of two distinct types behind one macro. This also makes it mismatch with the need to use distinct OBJECT_DEFINE macros for each of the types.
Quoted 11 lines
include/hw/qdev-core.h +5
-0+ \ + static inline G_GNUC_UNUSED BusInstanceType * \ + DEVICE_OBJ_NAME##_GET_BUS(const DeviceInstanceType *dev) \ + {return BUS_OBJ_NAME (qdev_get_parent_bus (DEVICE (dev))); } +/*** BUS API. ***/ DeviceState *qdev_find_recursive (BusState *bus,const char *id);Signature · 1 line
2.38.1
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
RFC PATCH 2/3 hw/usb: Declare QOM macros using QDEV_DECLARE_DEV_BUS_TYPES()
In reply to Philippe Mathieu-Daudé--- include/hw/usb.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/hw/usb.h +4 -4
@@ -266,8 +266,11 @@ struct USBDevice {const USBDescIface *ifaces[USB_MAX_INTERFACES ]; }; +#define TYPE_USB_BUS "usb-bus"#define TYPE_USB_DEVICE "usb-device" -OBJECT_DECLARE_TYPE(USBDevice, USBDeviceClass, USB_DEVICE) + +QDEV_DECLARE_DEV_BUS_TYPES (USBDevice, USBDeviceClass,USB_DEVICE , + USBBus, USB_BUS)typedef void (*USBDeviceRealize )(USBDevice *dev,Error **errp);typedef void (*USBDeviceUnrealize )(USBDevice *dev);
@@ -473,9 +476,6 @@ void hmp_info_usbhost(Monitor *mon, const QDict *qdict);/* usb-bus.c */ -#define TYPE_USB_BUS "usb-bus" -OBJECT_DECLARE_SIMPLE_TYPE(USBBus, USB_BUS) -struct USBBus {BusState qbus;USBBusOps *ops;
Signature · 1 line
2.38.1
RFC PATCH 3/3 hw/usb: Use USB_DEVICE_GET_BUS() macro
In reply to Philippe Mathieu-DaudéAutomatic conversion running:
$ sed -i -e s/usb_bus_from_device/USB_DEVICE_GET_BUS/g \
$(git grep -wl usb_bus_from_device)then removing the usb_bus_from_device() function declaration.
--- hw/usb/bus.c | 10 +++++----- hw/usb/core.c | 6 +++--- hw/usb/dev-hub.c | 4 ++-- hw/usb/dev-serial.c | 10 +++++----- hw/usb/hcd-xhci.c | 2 +- include/hw/usb.h | 5 ----- 6 files changed, 16 insertions(+), 21 deletions(-)
hw/usb/bus.c +5 -5
@@ -427,7 +427,7 @@ void usb_unregister_port(USBBus *bus, USBPort *port)void usb_claim_port (USBDevice *dev,Error **errp) { -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);USBPort *port;USBDevice *hub;
@@ -473,7 +473,7 @@ void usb_claim_port(USBDevice *dev, Error **errp)void usb_release_port (USBDevice *dev) { -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);USBPort *port = dev->port;assert (port !=NULL );
@@ -517,7 +517,7 @@ static void usb_mask_to_str(char *dest, size_t size,void usb_check_attach (USBDevice *dev,Error **errp) { -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);USBPort *port = dev->port;char devspeed[32 ], portspeed[32 ];
@@ -555,7 +555,7 @@ void usb_device_attach(USBDevice *dev, Error **errp)int usb_device_detach (USBDevice *dev) { -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);USBPort *port = dev->port;assert (port !=NULL );
@@ -583,7 +583,7 @@ static const char *usb_speed(unsigned int speed)static void usb_bus_dev_print (Monitor *mon,DeviceState *qdev,int indent) {USBDevice *dev =USB_DEVICE (qdev); -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev); monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n", indent, "", bus->busnr, dev->addr,
hw/usb/core.c +3 -3
@@ -95,7 +95,7 @@ void usb_device_reset(USBDevice *dev)void usb_wakeup (USBEndpoint *ep,unsigned int stream) {USBDevice *dev = ep->dev; -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);if (!phase_check (PHASE_MACHINE_READY )) {/*
@@ -556,7 +556,7 @@ void usb_packet_check_state(USBPacket *p, USBPacketState expected)return ; } dev = p->ep->dev; - bus =usb_bus_from_device (dev); + bus =USB_DEVICE_GET_BUS (dev);trace_usb_packet_state_fault (bus->busnr, dev->port->path, p->ep->nr, p,usb_packet_state_name (p->state),usb_packet_state_name (expected));
@@ -567,7 +567,7 @@ void usb_packet_set_state(USBPacket *p, USBPacketState state) {if (p->ep) {USBDevice *dev = p->ep->dev; -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);trace_usb_packet_state_change (bus->busnr, dev->port->path, p->ep->nr, p,usb_packet_state_name (p->state),usb_packet_state_name (state));
hw/usb/dev-hub.c +2 -2
@@ -572,7 +572,7 @@ static void usb_hub_unrealize(USBDevice *dev)int i;for (i =0 ; i < s->num_ports; i++) { -usb_unregister_port (usb_bus_from_device (dev), +usb_unregister_port (USB_DEVICE_GET_BUS (dev), &s->ports[i].port); }
@@ -611,7 +611,7 @@ static void usb_hub_realize(USBDevice *dev, Error **errp) s->intr =usb_ep_get (dev,USB_TOKEN_IN ,1 );for (i =0 ; i < s->num_ports; i++) { port = &s->ports[i]; -usb_register_port (usb_bus_from_device (dev), +usb_register_port (USB_DEVICE_GET_BUS (dev), &port->port, s, i, &usb_hub_port_ops,USB_SPEED_MASK_LOW |USB_SPEED_MASK_FULL );usb_port_location (&port->port, dev->port, i+1 );
hw/usb/dev-serial.c +5 -5
@@ -190,7 +190,7 @@ static void usb_serial_set_flow_control(USBSerialState *s, uint8_t flow_control) {USBDevice *dev =USB_DEVICE (s); -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);/* TODO: ioctl */ s->flow_control = flow_control;
@@ -200,7 +200,7 @@ static void usb_serial_set_flow_control(USBSerialState *s,static void usb_serial_set_xonxoff (USBSerialState *s,int xonxoff) {USBDevice *dev =USB_DEVICE (s); -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev); s->xon = xonxoff &0xff ; s->xoff = (xonxoff >>8 ) &0xff ;
@@ -221,7 +221,7 @@ static void usb_serial_reset(USBSerialState *s)static void usb_serial_handle_reset (USBDevice *dev) {USBSerialState *s =USB_SERIAL (dev); -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);trace_usb_serial_reset (bus->busnr, dev->addr);
@@ -261,7 +261,7 @@ static void usb_serial_handle_control(USBDevice *dev, USBPacket *p, int length, uint8_t *data) { USBSerialState *s = USB_SERIAL(dev); -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);int ret;trace_usb_serial_handle_control (bus->busnr, dev->addr, request, value);
@@ -479,7 +479,7 @@ static void usb_serial_token_in(USBSerialState *s, USBPacket *p)static void usb_serial_handle_data (USBDevice *dev,USBPacket *p) {USBSerialState *s =USB_SERIAL (dev); -USBBus *bus =usb_bus_from_device (dev); +USBBus *bus =USB_DEVICE_GET_BUS (dev);uint8_t devep = p->ep->nr;struct iovec *iov;int i;
hw/usb/hcd-xhci.c +1 -1
@@ -3268,7 +3268,7 @@ static void xhci_complete(USBPort *port, USBPacket *packet)static void xhci_child_detach (USBPort *uport,USBDevice *child) { -USBBus *bus =usb_bus_from_device (child); +USBBus *bus =USB_DEVICE_GET_BUS (child);XHCIState *xhci =container_of (bus, XHCIState, bus);xhci_detach_slot (xhci, child->port);
include/hw/usb.h +0 -5
@@ -518,11 +518,6 @@ void usb_device_attach(USBDevice *dev, Error **errp);int usb_device_detach (USBDevice *dev);void usb_check_attach (USBDevice *dev,Error **errp); -static inline USBBus *usb_bus_from_device (USBDevice *d) -{ -return DO_UPCAST (USBBus, qbus,qdev_get_parent_bus (DEVICE (d))); -} -extern const VMStateDescription vmstate_usb_device;#define VMSTATE_USB_DEVICE (_field, _state) { \
Signature · 1 line
2.38.1
Quoted 11 lines · On Mon, 13 Feb 2023 11:56:06 +0100 Philippe Mathieu-Daudé <[email protected]>…
Experiment after discussing with Thomas around
qdev_get_parent_bus:
https://lore.kernel.org/qemu-devel/[email protected]/When a QDev plug on a QBus, we'll always use
qdev_get_parent_bus()at least once with this type. Why not provide a consistent defined macro instead of:
1/ adding an inlined helper suchusb_bus_from_device()
orscsi_bus_from_device()with different type checks,
2/ open-code calls toqdev_get_parent_bus()with unsafe casts
?
Quoted 3 lines
This RFC series introduce a QDev-equivalent of QOM
DECLARE_TYPESmacro, to be used with a (device, bus) tuple, and declaring the equivalentdevice_GET_BUS()macro.
it's already bad having 2 ways to declare types (though SIMPLE was a huge LOC saving) so question is where do we stop (API explosion ain't a good thing either).
I my opinion this is just code churn for nor tangible benefit, given how qdev_get_parent_bus() is used.
Fixing unsafe casts and getting rid of DO_UPCAST you mentioned before, would better use of resources.
Quoted 16 lines
hw/usb/ is converted as an example.
Philippe Mathieu-Daudé (3):
hw/qdev: IntroduceQDEV_DECLARE_DEV_BUS_TYPES()macro hw/usb: Declare QOM macros usingQDEV_DECLARE_DEV_BUS_TYPES()hw/usb: UseUSB_DEVICE_GET_BUS()macrohw/usb/bus.c | 10 +++++----- hw/usb/core.c | 6 +++--- hw/usb/dev-hub.c | 4 ++-- hw/usb/dev-serial.c | 10 +++++----- hw/usb/hcd-xhci.c | 2 +- include/hw/qdev-core.h | 28 ++++++++++++++++++++++++++++ include/hw/usb.h | 13 ++++--------- 7 files changed, 48 insertions(+), 25 deletions(-)
Quoted 14 lines · Igor Mammedov
Quoted 11 lines · On Mon, 13 Feb 2023 11:56:06 +0100 Philippe Mathieu-Daudé <[email protected]>…
Experiment after discussing with Thomas around
qdev_get_parent_bus:
https://lore.kernel.org/qemu-devel/[email protected]/When a QDev plug on a QBus, we'll always use
qdev_get_parent_bus()at least once with this type. Why not provide a consistent defined macro instead of:
1/ adding an inlined helper suchusb_bus_from_device()
orscsi_bus_from_device()with different type checks,
2/ open-code calls toqdev_get_parent_bus()with unsafe casts
?
Quoted 8 lines
Quoted 3 lines
This RFC series introduce a QDev-equivalent of QOM
DECLARE_TYPESmacro, to be used with a (device, bus) tuple, and declaring the equivalentdevice_GET_BUS()macro.it's already bad having 2 ways to declare types (though SIMPLE was a huge LOC saving) so question is where do we stop (API explosion ain't a good thing either).
I my opinion this is just code churn for nor tangible benefit, given how
qdev_get_parent_bus()is used.
Right so in the USB conversion the only improvement is the elimination of this method:
-static inline USBBus *usb_bus_from_device(USBDevice *d) -{
- return DO_UPCAST(USBBus, qbus, qdev_get_parent_bus(DEVICE(d)));
-}
It strikes me that we don't have especially many (bus-type, device-type) pairs that would benefit from this macro.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|