Hi

On Mon, Jun 18, 2018 at 6:17 PM, Marc-André Lureau
<marcandre.lur...@redhat.com> wrote:
> Add base classes that are common to vhost-user-gpu-pci and
> vhost-user-vga.
>

Here is a bit more details on the base class split:

Both virtio-gpu-pci and virtio-vga are inheriting from virtio-pci.
vhost-user-gpu devices will share very similar setup and integration
with qemu than virtio-gpu, except that the virtio backend differs
(VhostUserGPU vs VirtIOGPU), and vhost-user-gpu devices take an extra
"vhost-user" property (aliased to the backend property).

Extract virtio-gpu-pci-base and virtio-vga-base classes, they are
inherited by the respective virtio & vhost-user devices with the few
differences mentionned.

> Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com>
> ---
>  hw/display/virtio-vga.h     |  22 +++++++
>  hw/virtio/virtio-pci.h      |  16 ++---
>  hw/display/virtio-gpu-pci.c |  39 +++++++++---
>  hw/display/virtio-vga.c     | 122 +++++++++++++++++++-----------------
>  MAINTAINERS                 |   2 +-
>  5 files changed, 126 insertions(+), 75 deletions(-)
>  create mode 100644 hw/display/virtio-vga.h
>
> diff --git a/hw/display/virtio-vga.h b/hw/display/virtio-vga.h
> new file mode 100644
> index 0000000000..212624449c
> --- /dev/null
> +++ b/hw/display/virtio-vga.h
> @@ -0,0 +1,22 @@
> +#ifndef VIRTIO_VGA_H_
> +#define VIRTIO_VGA_H_
> +
> +#include "hw/virtio/virtio-pci.h"
> +#include "vga_int.h"
> +
> +/*
> + * virtio-vga-base: This extends VirtioPCIProxy.
> + */
> +#define TYPE_VIRTIO_VGA_BASE "virtio-vga-base"
> +#define VIRTIO_VGA_BASE(obj)                                \
> +    OBJECT_CHECK(VirtIOVGABase, (obj), TYPE_VIRTIO_VGA_BASE)
> +
> +typedef struct VirtIOVGABase {
> +    VirtIOPCIProxy parent_obj;
> +
> +    VirtIOGPUBase *vgpu;
> +    VGACommonState vga;
> +    MemoryRegion   vga_mrs[3];
> +} VirtIOVGABase;
> +
> +#endif /* VIRTIO_VGA_H_ */
> diff --git a/hw/virtio/virtio-pci.h b/hw/virtio/virtio-pci.h
> index c7e28e1b9c..42240f47b3 100644
> --- a/hw/virtio/virtio-pci.h
> +++ b/hw/virtio/virtio-pci.h
> @@ -386,17 +386,19 @@ struct VHostUserInputPCI {
>      VHostUserInput vhi;
>  };
>
> +
>  /*
> - * virtio-gpu-pci: This extends VirtioPCIProxy.
> + * virtio-gpu-pci-base: This extends VirtioPCIProxy.
>   */
> -#define TYPE_VIRTIO_GPU_PCI "virtio-gpu-pci"
> -#define VIRTIO_GPU_PCI(obj) \
> -        OBJECT_CHECK(VirtIOGPUPCI, (obj), TYPE_VIRTIO_GPU_PCI)
> +#define TYPE_VIRTIO_GPU_PCI_BASE "virtio-gpu-pci-base"
> +#define VIRTIO_GPU_PCI_BASE(obj)                                 \
> +    OBJECT_CHECK(VirtIOGPUPCIBase, (obj), TYPE_VIRTIO_GPU_PCI_BASE)
>
> -struct VirtIOGPUPCI {
> +typedef struct VirtIOGPUPCIBase {
>      VirtIOPCIProxy parent_obj;
> -    VirtIOGPU vdev;
> -};
> +
> +    VirtIOGPUBase *vgpu;
> +} VirtIOGPUPCIBase;
>
>  #ifdef CONFIG_VHOST_VSOCK
>  /*
> diff --git a/hw/display/virtio-gpu-pci.c b/hw/display/virtio-gpu-pci.c
> index 741badd909..5db6ad890d 100644
> --- a/hw/display/virtio-gpu-pci.c
> +++ b/hw/display/virtio-gpu-pci.c
> @@ -19,16 +19,16 @@
>  #include "hw/virtio/virtio-pci.h"
>  #include "hw/virtio/virtio-gpu.h"
>
> -static Property virtio_gpu_pci_properties[] = {
> +static Property virtio_gpu_pci_base_properties[] = {
>      DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy),
>      DEFINE_PROP_END_OF_LIST(),
>  };
>
> -static void virtio_gpu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
> +static void virtio_gpu_pci_base_realize(VirtIOPCIProxy *vpci_dev, Error 
> **errp)
>  {
> -    VirtIOGPUPCI *vgpu = VIRTIO_GPU_PCI(vpci_dev);
> -    VirtIOGPUBase *g = VIRTIO_GPU_BASE(&vgpu->vdev);
> -    DeviceState *vdev = DEVICE(&vgpu->vdev);
> +    VirtIOGPUPCIBase *vgpu = VIRTIO_GPU_PCI_BASE(vpci_dev);
> +    VirtIOGPUBase *g = vgpu->vgpu;
> +    DeviceState *vdev = DEVICE(g);
>      int i;
>      Error *local_error = NULL;
>
> @@ -48,37 +48,56 @@ static void virtio_gpu_pci_realize(VirtIOPCIProxy 
> *vpci_dev, Error **errp)
>      }
>  }
>
> -static void virtio_gpu_pci_class_init(ObjectClass *klass, void *data)
> +static void virtio_gpu_pci_base_class_init(ObjectClass *klass, void *data)
>  {
>      DeviceClass *dc = DEVICE_CLASS(klass);
>      VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
>      PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
>
>      set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
> -    dc->props = virtio_gpu_pci_properties;
> +    dc->props = virtio_gpu_pci_base_properties;
>      dc->hotpluggable = false;
> -    k->realize = virtio_gpu_pci_realize;
> +    k->realize = virtio_gpu_pci_base_realize;
>      pcidev_k->class_id = PCI_CLASS_DISPLAY_OTHER;
>  }
>
> +static const TypeInfo virtio_gpu_pci_base_info = {
> +    .name = TYPE_VIRTIO_GPU_PCI_BASE,
> +    .parent = TYPE_VIRTIO_PCI,
> +    .instance_size = sizeof(VirtIOGPUPCIBase),
> +    .class_init = virtio_gpu_pci_base_class_init,
> +    .abstract = true
> +};
> +
> +#define TYPE_VIRTIO_GPU_PCI "virtio-gpu-pci"
> +#define VIRTIO_GPU_PCI(obj)                                 \
> +    OBJECT_CHECK(VirtIOGPUPCI, (obj), TYPE_VIRTIO_GPU_PCI)
> +
> +struct VirtIOGPUPCI {
> +    VirtIOGPUPCIBase parent_obj;
> +    VirtIOGPU vdev;
> +};
> +
>  static void virtio_gpu_initfn(Object *obj)
>  {
>      VirtIOGPUPCI *dev = VIRTIO_GPU_PCI(obj);
>
>      virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
>                                  TYPE_VIRTIO_GPU);
> +    VIRTIO_GPU_PCI_BASE(obj)->vgpu = VIRTIO_GPU_BASE(&dev->vdev);
>  }
>
>  static const TypeInfo virtio_gpu_pci_info = {
>      .name = TYPE_VIRTIO_GPU_PCI,
> -    .parent = TYPE_VIRTIO_PCI,
> +    .parent = TYPE_VIRTIO_GPU_PCI_BASE,
>      .instance_size = sizeof(VirtIOGPUPCI),
>      .instance_init = virtio_gpu_initfn,
> -    .class_init = virtio_gpu_pci_class_init,
>  };
>
>  static void virtio_gpu_pci_register_types(void)
>  {
> +    type_register_static(&virtio_gpu_pci_base_info);
>      type_register_static(&virtio_gpu_pci_info);
>  }
> +
>  type_init(virtio_gpu_pci_register_types)
> diff --git a/hw/display/virtio-vga.c b/hw/display/virtio-vga.c
> index 98388762f6..0c503460af 100644
> --- a/hw/display/virtio-vga.c
> +++ b/hw/display/virtio-vga.c
> @@ -1,56 +1,41 @@
>  #include "qemu/osdep.h"
>  #include "hw/hw.h"
>  #include "hw/pci/pci.h"
> -#include "vga_int.h"
> -#include "hw/virtio/virtio-pci.h"
>  #include "qapi/error.h"
> +#include "virtio-vga.h"
>
> -/*
> - * virtio-vga: This extends VirtioPCIProxy.
> - */
> -#define TYPE_VIRTIO_VGA "virtio-vga"
> -#define VIRTIO_VGA(obj) \
> -        OBJECT_CHECK(VirtIOVGA, (obj), TYPE_VIRTIO_VGA)
> -
> -typedef struct VirtIOVGA {
> -    VirtIOPCIProxy parent_obj;
> -    VirtIOGPU      vdev;
> -    VGACommonState vga;
> -    MemoryRegion   vga_mrs[3];
> -} VirtIOVGA;
> -
> -static void virtio_vga_invalidate_display(void *opaque)
> +static void virtio_vga_base_invalidate_display(void *opaque)
>  {
> -    VirtIOVGA *vvga = opaque;
> -    VirtIOGPUBase *g = VIRTIO_GPU_BASE(&vvga->vdev);
> +    VirtIOVGABase *vvga = opaque;
> +    VirtIOGPUBase *g = vvga->vgpu;
>
>      if (g->enable) {
> -        virtio_gpu_ops.invalidate(&vvga->vdev);
> +        virtio_gpu_ops.invalidate(g);
>      } else {
>          vvga->vga.hw_ops->invalidate(&vvga->vga);
>      }
>  }
>
> -static void virtio_vga_update_display(void *opaque)
> +static void virtio_vga_base_update_display(void *opaque)
>  {
> -    VirtIOVGA *vvga = opaque;
> -    VirtIOGPUBase *g = VIRTIO_GPU_BASE(&vvga->vdev);
> +    VirtIOVGABase *vvga = opaque;
> +    VirtIOGPUBase *g = vvga->vgpu;
>
>      if (g->enable) {
> -        virtio_gpu_ops.gfx_update(&vvga->vdev);
> +        virtio_gpu_ops.gfx_update(g);
>      } else {
>          vvga->vga.hw_ops->gfx_update(&vvga->vga);
>      }
>  }
>
> -static void virtio_vga_text_update(void *opaque, console_ch_t *chardata)
> +static void virtio_vga_base_text_update(void *opaque, console_ch_t *chardata)
>  {
> -    VirtIOVGA *vvga = opaque;
> -    VirtIOGPUBase *g = VIRTIO_GPU_BASE(&vvga->vdev);
> +    VirtIOVGABase *vvga = opaque;
> +    VirtIOGPUBase *g = vvga->vgpu;
>
>      if (g->enable) {
>          if (virtio_gpu_ops.text_update) {
> -            virtio_gpu_ops.text_update(&vvga->vdev, chardata);
> +            virtio_gpu_ops.text_update(g, chardata);
>          }
>      } else {
>          if (vvga->vga.hw_ops->text_update) {
> @@ -59,49 +44,52 @@ static void virtio_vga_text_update(void *opaque, 
> console_ch_t *chardata)
>      }
>  }
>
> -static int virtio_vga_ui_info(void *opaque, uint32_t idx, QemuUIInfo *info)
> +static int virtio_vga_base_ui_info(void *opaque, uint32_t idx, QemuUIInfo 
> *info)
>  {
> -    VirtIOVGA *vvga = opaque;
> +    VirtIOVGABase *vvga = opaque;
> +    VirtIOGPUBase *g = vvga->vgpu;
>
>      if (virtio_gpu_ops.ui_info) {
> -        return virtio_gpu_ops.ui_info(&vvga->vdev, idx, info);
> +        return virtio_gpu_ops.ui_info(g, idx, info);
>      }
>      return -1;
>  }
>
> -static void virtio_vga_gl_block(void *opaque, bool block)
> +static void virtio_vga_base_gl_block(void *opaque, bool block)
>  {
> -    VirtIOVGA *vvga = opaque;
> +    VirtIOVGABase *vvga = opaque;
> +    VirtIOGPUBase *g = vvga->vgpu;
>
>      if (virtio_gpu_ops.gl_block) {
> -        virtio_gpu_ops.gl_block(&vvga->vdev, block);
> +        virtio_gpu_ops.gl_block(g, block);
>      }
>  }
>
> -static const GraphicHwOps virtio_vga_ops = {
> -    .invalidate = virtio_vga_invalidate_display,
> -    .gfx_update = virtio_vga_update_display,
> -    .text_update = virtio_vga_text_update,
> -    .ui_info = virtio_vga_ui_info,
> -    .gl_block = virtio_vga_gl_block,
> +static const GraphicHwOps virtio_vga_base_ops = {
> +    .invalidate = virtio_vga_base_invalidate_display,
> +    .gfx_update = virtio_vga_base_update_display,
> +    .text_update = virtio_vga_base_text_update,
> +    .ui_info = virtio_vga_base_ui_info,
> +    .gl_block = virtio_vga_base_gl_block,
>  };
>
> -static const VMStateDescription vmstate_virtio_vga = {
> +static const VMStateDescription vmstate_virtio_vga_base = {
>      .name = "virtio-vga",
>      .version_id = 2,
>      .minimum_version_id = 2,
>      .fields = (VMStateField[]) {
>          /* no pci stuff here, saving the virtio device will handle that */
> -        VMSTATE_STRUCT(vga, VirtIOVGA, 0, vmstate_vga_common, 
> VGACommonState),
> +        VMSTATE_STRUCT(vga, VirtIOVGABase, 0,
> +                       vmstate_vga_common, VGACommonState),
>          VMSTATE_END_OF_LIST()
>      }
>  };
>
>  /* VGA device wrapper around PCI device around virtio GPU */
> -static void virtio_vga_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
> +static void virtio_vga_base_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
>  {
> -    VirtIOVGA *vvga = VIRTIO_VGA(vpci_dev);
> -    VirtIOGPUBase *g = VIRTIO_GPU_BASE(&vvga->vdev);
> +    VirtIOVGABase *vvga = VIRTIO_VGA_BASE(vpci_dev);
> +    VirtIOGPUBase *g = vvga->vgpu;
>      VGACommonState *vga = &vvga->vga;
>      Error *err = NULL;
>      uint32_t offset;
> @@ -159,7 +147,7 @@ static void virtio_vga_realize(VirtIOPCIProxy *vpci_dev, 
> Error **errp)
>                                   vvga->vga_mrs, true);
>
>      vga->con = g->scanout[0].con;
> -    graphic_console_set_hwops(vga->con, &virtio_vga_ops, vvga);
> +    graphic_console_set_hwops(vga->con, &virtio_vga_base_ops, vvga);
>
>      for (i = 0; i < g->conf.max_outputs; i++) {
>          object_property_set_link(OBJECT(g->scanout[i].con),
> @@ -168,56 +156,76 @@ static void virtio_vga_realize(VirtIOPCIProxy 
> *vpci_dev, Error **errp)
>      }
>  }
>
> -static void virtio_vga_reset(DeviceState *dev)
> +static void virtio_vga_base_reset(DeviceState *dev)
>  {
> -    VirtIOVGA *vvga = VIRTIO_VGA(dev);
> -    VirtIOGPUBase *g = VIRTIO_GPU_BASE(&vvga->vdev);
> +    VirtIOVGABase *vvga = VIRTIO_VGA_BASE(dev);
> +    VirtIOGPUBase *g = vvga->vgpu;
>
>      g->enable = 0;
>
>      vga_dirty_log_start(&vvga->vga);
>  }
>
> -static Property virtio_vga_properties[] = {
> +static Property virtio_vga_base_properties[] = {
>      DEFINE_VIRTIO_GPU_PCI_PROPERTIES(VirtIOPCIProxy),
>      DEFINE_PROP_END_OF_LIST(),
>  };
>
> -static void virtio_vga_class_init(ObjectClass *klass, void *data)
> +static void virtio_vga_base_class_init(ObjectClass *klass, void *data)
>  {
>      DeviceClass *dc = DEVICE_CLASS(klass);
>      VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
>      PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
>
>      set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
> -    dc->props = virtio_vga_properties;
> -    dc->reset = virtio_vga_reset;
> -    dc->vmsd = &vmstate_virtio_vga;
> +    dc->props = virtio_vga_base_properties;
> +    dc->reset = virtio_vga_base_reset;
> +    dc->vmsd = &vmstate_virtio_vga_base;
>      dc->hotpluggable = false;
>
> -    k->realize = virtio_vga_realize;
> +    k->realize = virtio_vga_base_realize;
>      pcidev_k->romfile = "vgabios-virtio.bin";
>      pcidev_k->class_id = PCI_CLASS_DISPLAY_VGA;
>  }
>
> +static TypeInfo virtio_vga_base_info = {
> +    .name          = TYPE_VIRTIO_VGA_BASE,
> +    .parent        = TYPE_VIRTIO_PCI,
> +    .instance_size = sizeof(struct VirtIOVGABase),
> +    .class_init    = virtio_vga_base_class_init,
> +    .abstract      = true,
> +};
> +
> +#define TYPE_VIRTIO_VGA "virtio-vga"
> +
> +#define VIRTIO_VGA(obj)                             \
> +    OBJECT_CHECK(VirtIOVGA, (obj), TYPE_VIRTIO_VGA)
> +
> +typedef struct VirtIOVGA {
> +    VirtIOVGABase parent_obj;
> +
> +    VirtIOGPU     vdev;
> +} VirtIOVGA;
> +
>  static void virtio_vga_inst_initfn(Object *obj)
>  {
>      VirtIOVGA *dev = VIRTIO_VGA(obj);
>
>      virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
>                                  TYPE_VIRTIO_GPU);
> +    VIRTIO_VGA_BASE(dev)->vgpu = VIRTIO_GPU_BASE(&dev->vdev);
>  }
>
>  static TypeInfo virtio_vga_info = {
>      .name          = TYPE_VIRTIO_VGA,
> -    .parent        = TYPE_VIRTIO_PCI,
> +    .parent        = TYPE_VIRTIO_VGA_BASE,
>      .instance_size = sizeof(struct VirtIOVGA),
>      .instance_init = virtio_vga_inst_initfn,
> -    .class_init    = virtio_vga_class_init,
>  };
>
>  static void virtio_vga_register_types(void)
>  {
> +    type_register_static(&virtio_vga_base_info);
>      type_register_static(&virtio_vga_info);
>  }
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 5dc312f7de..c958fff474 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -1336,7 +1336,7 @@ virtio-gpu
>  M: Gerd Hoffmann <kra...@redhat.com>
>  S: Maintained
>  F: hw/display/virtio-gpu*
> -F: hw/display/virtio-vga.c
> +F: hw/display/virtio-vga.*
>  F: include/hw/virtio/virtio-gpu.h
>
>  vhost-user-gpu
> --
> 2.18.0.rc1
>

Reply via email to