Skip to contentSkip to conversation

ide: convert to qdev.

PATCH In review Gerd Hoffmann 3 participants 24 messages Sep 11, 2009 – Sep 14, 2009 5 messages not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

cover ide: convert to qdev.

Hi,

Here is a patch series which starts the conversion of ide to qdev. It brings the core infrastructure and converts pci+isa ide adapters to qdev. Also some minor preparatory patches and bugfixes.

cheers,
Gerd

#1 lore raw

PATCH 1/8 qdev/pci: add pci_create_noinit()

Like pci_create_simple() but doesn't call qdev_init(), so one can set properties before initializing the device.

---
 hw/pci.c |   11 ++++++++---
 hw/pci.h |    1 +
 2 files changed, 9 insertions(+), 3 deletions(-)
hw/pci.c +8 -3
index c12b0be..64d70ed 100644
@@ -923,15 +923,20 @@ void pci_qdev_register_many(PCIDeviceInfo *info)
     }
 }
 
-PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
+PCIDevice *pci_create_noinit(PCIBus *bus, int devfn, const char *name)
 {
     DeviceState *dev;
 
     dev = qdev_create(&bus->qbus, name);
     qdev_prop_set_uint32(dev, "addr", devfn);
-    qdev_init(dev);
+    return DO_UPCAST(PCIDevice, qdev, dev);
+}
 
-    return (PCIDevice *)dev;
+PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
+{
+    PCIDevice *dev = pci_create_noinit(bus, devfn, name);
+    qdev_init(&dev->qdev);
+    return dev;
 }
 
 static int pci_find_space(PCIDevice *pdev, uint8_t size)
hw/pci.h +1 -0
index 6196b6a..e7bf33a 100644
@@ -328,6 +328,7 @@ void pci_qdev_register(PCIDeviceInfo *info);
 void pci_qdev_register_many(PCIDeviceInfo *info);
 
 PCIDevice *pci_create(const char *name, const char *devaddr);
+PCIDevice *pci_create_noinit(PCIBus *bus, int devfn, const char *name);
 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
 
 /* lsi53c895a.c */
Signature · 1 line
1.6.2.5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/8] qdev/pci: add pci_create_noinit()

Quoted 17 lines · Gerd Hoffmann

Like pci_create_simple() but doesn't call qdev_init(), so one can set properties before initializing the device.

---
 hw/pci.c |   11 ++++++++---
 hw/pci.h |    1 +
 2 files changed, 9 insertions(+), 3 deletions(-)
hw/pci.c +0 -0
index c12b0be..64d70ed 100644
@@ -923,15 +923,20 @@ void pci_qdev_register_many(PCIDeviceInfo *info)
     }
 }
 

hw/pci.c void pci_qdev_register_many(PCIDeviceInfo *info) · line 933

Quoted 10 lines
hw/pci.c +3 -2
-PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
+PCIDevice *pci_create_noinit(PCIBus *bus, int devfn, const char *name)
 {
     DeviceState *dev;
 
     dev = qdev_create(&bus->qbus, name);
     qdev_prop_set_uint32(dev, "addr", devfn);
-    qdev_init(dev);
+    return DO_UPCAST(PCIDevice, qdev, dev);
+}

Okay, this is qdev_create() specialized for PCI. What about calling it just pci_create()?

Quoted 22 lines
hw/pci.c +5 -1
-    return (PCIDevice *)dev;
+PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
+{
+    PCIDevice *dev = pci_create_noinit(bus, devfn, name);
+    qdev_init(&dev->qdev);
+    return dev;
 }
 
 static int pci_find_space(PCIDevice *pdev, uint8_t size)
hw/pci.h +1 -0
index 6196b6a..e7bf33a 100644
@@ -328,6 +328,7 @@ void pci_qdev_register(PCIDeviceInfo *info);
 void pci_qdev_register_many(PCIDeviceInfo *info);
 
 PCIDevice *pci_create(const char *name, const char *devaddr);
+PCIDevice *pci_create_noinit(PCIBus *bus, int devfn, const char *name);
 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name);
 
 /* lsi53c895a.c */
#3 lore raw

Re: [Qemu-devel] [PATCH 1/8] qdev/pci: add pci_create_noinit()

Quoted 5 lines
Quoted 5 lines
hw/pci.c +1 -1
-PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
+PCIDevice *pci_create_noinit(PCIBus *bus, int devfn, const char *name)
  {
      DeviceState *dev;
Quoted 8 lines

hw/pci.c void pci_qdev_register_many(PCIDeviceInfo *info) · line 933

Quoted 5 lines
      dev = qdev_create(&bus->qbus, name);
      qdev_prop_set_uint32(dev, "addr", devfn);
hw/pci.c +2 -1
-    qdev_init(dev);
+    return DO_UPCAST(PCIDevice, qdev, dev);
+}

Okay, this is qdev_create() specialized for PCI. What about calling it just pci_create()?

pci_create() should go away once it has no more users. It doesn't accept a pcibus parameter as it should.

cheers,
Gerd

#4 lore raw

Re: [Qemu-devel] [PATCH 1/8] qdev/pci: add pci_create_noinit()

Quoted 14 lines · Gerd Hoffmann
Quoted 5 lines
Quoted 5 lines
hw/pci.c +1 -1
-PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
+PCIDevice *pci_create_noinit(PCIBus *bus, int devfn, const char *name)
  {
      DeviceState *dev;
Quoted 8 lines

hw/pci.c void pci_qdev_register_many(PCIDeviceInfo *info) · line 933

Quoted 5 lines
      dev = qdev_create(&bus->qbus, name);
      qdev_prop_set_uint32(dev, "addr", devfn);
hw/pci.c +2 -1
-    qdev_init(dev);
+    return DO_UPCAST(PCIDevice, qdev, dev);
+}

Okay, this is qdev_create() specialized for PCI. What about calling it just pci_create()?

Quoted 2 lines

pci_create() should go away once it has no more users. It doesn't accept a pcibus parameter as it should.

I missed the fact that pci_create() already exists. That's a pity.

#5 lore raw

PATCH 2/8 support media=cdrom for if=none

In reply to Gerd Hoffmann
---
 vl.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
vl.c +1 -1
index c6c6a6b..316dc49 100644
@@ -2187,6 +2187,7 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque,
     case IF_IDE:
     case IF_SCSI:
     case IF_XEN:
+    case IF_NONE:
         switch(media) {
 	case MEDIA_DISK:
             if (cyls != 0) {
@@ -2207,7 +2208,6 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque,
         break;
     case IF_PFLASH:
     case IF_MTD:
-    case IF_NONE:
         break;
     case IF_VIRTIO:
         /* add virtio block device */
Signature · 1 line
1.6.2.5
#6 lore raw

PATCH 3/8 split away drive init from ide_init2()

In reply to Gerd Hoffmann

This allows the ide bus being initialized without drives attached and the drives being attached and initialization later on as separate step.

---
 hw/ide/core.c     |   71 +++++++++++++++++++++++++++++------------------------
 hw/ide/internal.h |    1 +
 2 files changed, 40 insertions(+), 32 deletions(-)
hw/ide/core.c +39 -32
index fe5bd17..bac2d5e 100644
@@ -2505,51 +2505,58 @@ void ide_reset(IDEState *s)
     s->media_changed = 0;
 }
 
+void ide_init_drive(IDEState *s, DriveInfo *dinfo)
+{
+    int cylinders, heads, secs;
+    uint64_t nb_sectors;
+
+    if (dinfo && dinfo->bdrv) {
+        s->bs = dinfo->bdrv;
+        bdrv_get_geometry(s->bs, &nb_sectors);
+        bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
+        s->cylinders = cylinders;
+        s->heads = heads;
+        s->sectors = secs;
+        s->nb_sectors = nb_sectors;
+        /* The SMART values should be preserved across power cycles
+           but they aren't.  */
+        s->smart_enabled = 1;
+        s->smart_autosave = 1;
+        s->smart_errors = 0;
+        s->smart_selftest_count = 0;
+        if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
+            s->is_cdrom = 1;
+            bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
+        }
+        strncpy(s->drive_serial_str, drive_get_serial(s->bs),
+                sizeof(s->drive_serial_str));
+    }
+    if (strlen(s->drive_serial_str) == 0)
+        snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
+                 "QM%05d", s->drive_serial);
+    ide_reset(s);
+}
+
 void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
                qemu_irq irq)
 {
     IDEState *s;
     static int drive_serial = 1;
-    int i, cylinders, heads, secs;
-    uint64_t nb_sectors;
+    int i;
 
     for(i = 0; i < 2; i++) {
         s = bus->ifs + i;
         s->bus = bus;
         s->unit = i;
-        if (i == 0 && hd0)
-            s->bs = hd0->bdrv;
-        if (i == 1 && hd1)
-            s->bs = hd1->bdrv;
-        s->io_buffer = qemu_blockalign(s->bs, IDE_DMA_BUF_SECTORS*512 + 4);
-        if (s->bs) {
-            bdrv_get_geometry(s->bs, &nb_sectors);
-            bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
-            s->cylinders = cylinders;
-            s->heads = heads;
-            s->sectors = secs;
-            s->nb_sectors = nb_sectors;
-	    /* The SMART values should be preserved across power cycles
-	       but they aren't.  */
-	    s->smart_enabled = 1;
-	    s->smart_autosave = 1;
-	    s->smart_errors = 0;
-	    s->smart_selftest_count = 0;
-	    s->smart_selftest_data = qemu_blockalign(s->bs, 512);
-            if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
-                s->is_cdrom = 1;
-		bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
-            }
-        }
         s->drive_serial = drive_serial++;
-        strncpy(s->drive_serial_str, drive_get_serial(s->bs),
-                sizeof(s->drive_serial_str));
-        if (strlen(s->drive_serial_str) == 0)
-            snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
-                    "QM%05d", s->drive_serial);
+        s->io_buffer = qemu_blockalign(s->bs, IDE_DMA_BUF_SECTORS*512 + 4);
+        s->smart_selftest_data = qemu_blockalign(s->bs, 512);
         s->sector_write_timer = qemu_new_timer(vm_clock,
                                                ide_sector_write_timer_cb, s);
-        ide_reset(s);
+        if (i == 0)
+            ide_init_drive(s, hd0);
+        if (i == 1)
+            ide_init_drive(s, hd1);
     }
     bus->irq = irq;
 }
hw/ide/internal.h +1 -0
index b9a7c72..62aef1c 100644
@@ -527,6 +527,7 @@ uint32_t ide_data_readw(void *opaque, uint32_t addr);
 void ide_data_writel(void *opaque, uint32_t addr, uint32_t val);
 uint32_t ide_data_readl(void *opaque, uint32_t addr);
 
+void ide_init_drive(IDEState *s, DriveInfo *dinfo);
 void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
                qemu_irq irq);
 void ide_init_ioport(IDEBus *bus, int iobase, int iobase2);
Signature · 1 line
1.6.2.5
#7 lore raw

Re: [Qemu-devel] [PATCH 3/8] split away drive init from ide_init2()

Quoted 15 lines · Gerd Hoffmann

This allows the ide bus being initialized without drives attached and the drives being attached and initialization later on as separate step.

---
 hw/ide/core.c     |   71 +++++++++++++++++++++++++++++------------------------
 hw/ide/internal.h |    1 +
 2 files changed, 40 insertions(+), 32 deletions(-)
hw/ide/core.c +0 -0
index fe5bd17..bac2d5e 100644

hw/ide/core.c void ide_reset(IDEState *s) · line 2514

Quoted 10 lines
hw/ide/core.c +7 -0
@@ -2505,51 +2505,58 @@ void ide_reset(IDEState *s)
     s->media_changed = 0;
 }
 
+void ide_init_drive(IDEState *s, DriveInfo *dinfo)
+{
+    int cylinders, heads, secs;
+    uint64_t nb_sectors;
+
+    if (dinfo && dinfo->bdrv) {
+        s->bs = dinfo->bdrv;

Before, this assignement was executed even when !dinfo->bdrv. Can this happen? Does it matter?

Quoted 8 lines
hw/ide/core.c +8 -0
+        bdrv_get_geometry(s->bs, &nb_sectors);
+        bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
+        s->cylinders = cylinders;
+        s->heads = heads;
+        s->sectors = secs;
+        s->nb_sectors = nb_sectors;
+        /* The SMART values should be preserved across power cycles
+           but they aren't.  */

hw/ide/core.c void ide_reset(IDEState *s) · line 2532

Quoted 10 lines
hw/ide/core.c +10 -0
+        s->smart_enabled = 1;
+        s->smart_autosave = 1;
+        s->smart_errors = 0;
+        s->smart_selftest_count = 0;
+        if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
+            s->is_cdrom = 1;
+            bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
+        }
+        strncpy(s->drive_serial_str, drive_get_serial(s->bs),
+                sizeof(s->drive_serial_str));

Same for this assignment.

Quoted 49 lines
hw/ide/core.c +8 -30
+    }
+    if (strlen(s->drive_serial_str) == 0)
+        snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),
+                 "QM%05d", s->drive_serial);
+    ide_reset(s);
+}
+
 void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
                qemu_irq irq)
 {
     IDEState *s;
     static int drive_serial = 1;
-    int i, cylinders, heads, secs;
-    uint64_t nb_sectors;
+    int i;
 
     for(i = 0; i < 2; i++) {
         s = bus->ifs + i;
         s->bus = bus;
         s->unit = i;
-        if (i == 0 && hd0)
-            s->bs = hd0->bdrv;
-        if (i == 1 && hd1)
-            s->bs = hd1->bdrv;
-        s->io_buffer = qemu_blockalign(s->bs, IDE_DMA_BUF_SECTORS*512 + 4);
-        if (s->bs) {
-            bdrv_get_geometry(s->bs, &nb_sectors);
-            bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
-            s->cylinders = cylinders;
-            s->heads = heads;
-            s->sectors = secs;
-            s->nb_sectors = nb_sectors;
-	    /* The SMART values should be preserved across power cycles
-	       but they aren't.  */
-	    s->smart_enabled = 1;
-	    s->smart_autosave = 1;
-	    s->smart_errors = 0;
-	    s->smart_selftest_count = 0;
-	    s->smart_selftest_data = qemu_blockalign(s->bs, 512);
-            if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
-                s->is_cdrom = 1;
-		bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
-            }
-        }
         s->drive_serial = drive_serial++;
-        strncpy(s->drive_serial_str, drive_get_serial(s->bs),
-                sizeof(s->drive_serial_str));
-        if (strlen(s->drive_serial_str) == 0)
-            snprintf(s->drive_serial_str, sizeof(s->drive_serial_str),

hw/ide/core.c void ide_reset(IDEState *s) · line 2559

Quoted 10 lines
hw/ide/core.c +6 -2
-                    "QM%05d", s->drive_serial);
+        s->io_buffer = qemu_blockalign(s->bs, IDE_DMA_BUF_SECTORS*512 + 4);
+        s->smart_selftest_data = qemu_blockalign(s->bs, 512);
         s->sector_write_timer = qemu_new_timer(vm_clock,
                                                ide_sector_write_timer_cb, s);
-        ide_reset(s);
+        if (i == 0)
+            ide_init_drive(s, hd0);
+        if (i == 1)
+            ide_init_drive(s, hd1);

I'd write this as

        ide_init_drive(i == 0 ? hd0 : hd1);

because I find it clearer, but it's a matter of taste.

Quoted 15 lines
hw/ide/core.c +0 -0
     }
     bus->irq = irq;
 }
hw/ide/internal.h +1 -0
index b9a7c72..62aef1c 100644
@@ -527,6 +527,7 @@ uint32_t ide_data_readw(void *opaque, uint32_t addr);
 void ide_data_writel(void *opaque, uint32_t addr, uint32_t val);
 uint32_t ide_data_readl(void *opaque, uint32_t addr);
 
+void ide_init_drive(IDEState *s, DriveInfo *dinfo);
 void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
                qemu_irq irq);
 void ide_init_ioport(IDEBus *bus, int iobase, int iobase2);
#8 lore raw

Re: [Qemu-devel] [PATCH 3/8] split away drive init from ide_init2()

Quoted 11 lines

hw/ide/core.c void ide_reset(IDEState *s) · line 2514

Quoted 8 lines
hw/ide/core.c +7 -0
+void ide_init_drive(IDEState *s, DriveInfo *dinfo)
+{
+    int cylinders, heads, secs;
+    uint64_t nb_sectors;
+
+    if (dinfo&&  dinfo->bdrv) {
+        s->bs = dinfo->bdrv;

Before, this assignement was executed even when !dinfo->bdrv. Can this happen?

Running this once with dinfo->bdrv = something, then with dinfo->bdrv = NULL (which is the only case where this matters) can't happen. You can't hot-unplut ide drives ;)

The code used to be called once for initialization. Now it might be called twice for ide busses converted to qdev: Once with dinfo = NULL (when setting up the bus) and once with dinfo != NULL (when attaching the drive).

cheers,
Gerd

#9 lore raw

PATCH 4/8 ide/qdev: add ide bus.

In reply to Gerd Hoffmann
---
 hw/ide/internal.h |   24 ++++++++++-
 hw/ide/qdev.c     |  123 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 146 insertions(+), 1 deletions(-)
 create mode 100644 hw/ide/qdev.c
hw/ide/internal.h +23 -1
index 62aef1c..9df759d 100644
@@ -15,6 +15,8 @@
 #define USE_DMA_CDROM
 
 typedef struct IDEBus IDEBus;
+typedef struct IDEDevice IDEDevice;
+typedef struct IDEDeviceInfo IDEDeviceInfo;
 typedef struct IDEState IDEState;
 typedef struct BMDMAState BMDMAState;
 
@@ -440,6 +442,8 @@ struct IDEState {
 
 struct IDEBus {
     BusState qbus;
+    IDEDevice *master;
+    IDEDevice *slave;
     BMDMAState *bmdma;
     IDEState ifs[2];
     uint8_t unit;
@@ -447,6 +451,20 @@ struct IDEBus {
     qemu_irq irq;
 };
 
+struct IDEDevice {
+    DeviceState qdev;
+    uint32_t unit;
+    DriveInfo *dinfo;
+};
+
+typedef int (*ide_qdev_initfn)(IDEDevice *dev);
+struct IDEDeviceInfo {
+    DeviceInfo qdev;
+    ide_qdev_initfn init;
+    uint32_t unit;
+    DriveInfo *drive;
+};
+
 #define BM_STATUS_DMAING 0x01
 #define BM_STATUS_ERROR  0x02
 #define BM_STATUS_INT    0x04
@@ -500,7 +518,7 @@ static inline void ide_set_irq(IDEBus *bus)
     }
 }
 
-/* ide.c */
+/* hw/ide/core.c */
 void ide_save(QEMUFile* f, IDEState *s);
 void ide_load(QEMUFile* f, IDEState *s, int version_id);
 void idebus_save(QEMUFile* f, IDEBus *bus);
@@ -532,4 +550,8 @@ void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
                qemu_irq irq);
 void ide_init_ioport(IDEBus *bus, int iobase, int iobase2);
 
+/* hw/ide/qdev.c */
+IDEBus *ide_bus_new(DeviceState *dev);
+IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive);
+
 #endif /* HW_IDE_INTERNAL_H */
hw/ide/qdev.c new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
1.6.2.5

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw

[Qemu-devel] Re: [PATCH 4/8] ide/qdev: add ide bus.

Quoted 6 lines

hw/ide/internal.h struct IDEState { · line 446

Quoted 4 lines
  struct IDEBus {
      BusState qbus;
hw/ide/internal.h +2 -0
+    IDEDevice *master;
+    IDEDevice *slave;

Can this ones be embedded, not pointers, please.

No.

Also note that vmstate doesn't has to follow these pointers, so you have no reason in the first place ;)

cheers,
Gerd

#12 lore raw

Re: [Qemu-devel] [PATCH 4/8] ide/qdev: add ide bus.

In reply to Gerd Hoffmann
Quoted 21 lines · Gerd Hoffmann
---
 hw/ide/internal.h |   24 ++++++++++-
 hw/ide/qdev.c     |  123 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 146 insertions(+), 1 deletions(-)
 create mode 100644 hw/ide/qdev.c
hw/ide/internal.h +2 -0
index 62aef1c..9df759d 100644
@@ -15,6 +15,8 @@
 #define USE_DMA_CDROM
 
 typedef struct IDEBus IDEBus;
+typedef struct IDEDevice IDEDevice;
+typedef struct IDEDeviceInfo IDEDeviceInfo;
 typedef struct IDEState IDEState;
 typedef struct BMDMAState BMDMAState;
 

hw/ide/internal.h struct IDEState { · line 449

Quoted 8 lines
hw/ide/internal.h +2 -0
@@ -440,6 +442,8 @@ struct IDEState {
 
 struct IDEBus {
     BusState qbus;
+    IDEDevice *master;
+    IDEDevice *slave;
     BMDMAState *bmdma;
     IDEState ifs[2];
     uint8_t unit;

master corresponds to ifs[0], and slave to ifs[1]. Document?

Quoted 106 lines
hw/ide/internal.h +19 -1
@@ -447,6 +451,20 @@ struct IDEBus {
     qemu_irq irq;
 };
 
+struct IDEDevice {
+    DeviceState qdev;
+    uint32_t unit;
+    DriveInfo *dinfo;
+};
+
+typedef int (*ide_qdev_initfn)(IDEDevice *dev);
+struct IDEDeviceInfo {
+    DeviceInfo qdev;
+    ide_qdev_initfn init;
+    uint32_t unit;
+    DriveInfo *drive;
+};
+
 #define BM_STATUS_DMAING 0x01
 #define BM_STATUS_ERROR  0x02
 #define BM_STATUS_INT    0x04
@@ -500,7 +518,7 @@ static inline void ide_set_irq(IDEBus *bus)
     }
 }
 
-/* ide.c */
+/* hw/ide/core.c */
 void ide_save(QEMUFile* f, IDEState *s);
 void ide_load(QEMUFile* f, IDEState *s, int version_id);
 void idebus_save(QEMUFile* f, IDEBus *bus);
@@ -532,4 +550,8 @@ void ide_init2(IDEBus *bus, DriveInfo *hd0, DriveInfo *hd1,
                qemu_irq irq);
 void ide_init_ioport(IDEBus *bus, int iobase, int iobase2);
 
+/* hw/ide/qdev.c */
+IDEBus *ide_bus_new(DeviceState *dev);
+IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive);
+
 #endif /* HW_IDE_INTERNAL_H */
hw/ide/qdev.c new file +61 -0
new file mode 100644
index 0000000..6026c28
@@ -0,0 +1,123 @@
+/*
+ * ide bus support for qdev.
+ *
+ * Copyright (c) 2009 Gerd Hoffmann <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include <hw/hw.h>
+#include "sysemu.h"
+#include "dma.h"
+
+#include <hw/ide/internal.h>
+
+/* --------------------------------- */
+
+static struct BusInfo ide_bus_info = {
+    .name  = "IDE",
+    .size  = sizeof(IDEBus),
+};
+
+IDEBus *ide_bus_new(DeviceState *dev)
+{
+    IDEBus *idebus;
+
+    idebus = FROM_QBUS(IDEBus, qbus_create(&ide_bus_info, dev, NULL));
+    return idebus;
+}
+
+static int ide_qdev_init(DeviceState *qdev, DeviceInfo *base)
+{
+    IDEDevice *dev = DO_UPCAST(IDEDevice, qdev, qdev);
+    IDEDeviceInfo *info = DO_UPCAST(IDEDeviceInfo, qdev, base);
+    IDEBus *bus = DO_UPCAST(IDEBus, qbus, qdev->parent_bus);
+
+    if (!dev->dinfo) {
+        fprintf(stderr, "%s: no drive specified\n", qdev->info->name);
+        goto err;
+    }
+    if (dev->unit == -1) {
+        dev->unit = bus->master ? 1 : 0;
+    }
+    switch (dev->unit) {
+    case 0:
+        if (bus->master) {
+            fprintf(stderr, "ide: tried to assign master twice\n");
+            goto err;
+        }
+        bus->master = dev;
+        break;
+    case 1:

hw/ide/qdev.c line 71

Quoted 10 lines
hw/ide/qdev.c new file +10 -0
+        if (bus->slave) {
+            fprintf(stderr, "ide: tried to assign slave twice\n");
+            goto err;
+        }
+        bus->slave = dev;
+        break;
+    default:
+        goto err;
+    }
+    return info->init(dev);

Is always ide_drive_initfn() so far, but more callbacks come in later in the series.

Quoted 10 lines
hw/ide/qdev.c new file +10 -0
+
+err:
+    return -1;
+}
+
+static void ide_qdev_register(IDEDeviceInfo *info)
+{
+    info->qdev.init = ide_qdev_init;
+    info->qdev.bus_info = &ide_bus_info;
+    qdev_register(&info->qdev);

hw/ide/qdev.c line 91

Quoted 10 lines
hw/ide/qdev.c new file +10 -0
+}
+
+IDEDevice *ide_create_drive(IDEBus *bus, int unit, DriveInfo *drive)
+{
+    DeviceState *dev;
+
+    dev = qdev_create(&bus->qbus, "ide-drive");
+    qdev_prop_set_uint32(dev, "unit", unit);
+    qdev_prop_set_drive(dev, "drive", drive);
+    qdev_init(dev);

Callback dev->info is ide_qdev_init(), which can fail, so qdev_init() can fail, can't it?

Quoted 32 lines
hw/ide/qdev.c new file +32 -0
+    return DO_UPCAST(IDEDevice, qdev, dev);
+}
+
+/* --------------------------------- */
+
+typedef struct IDEDrive {
+    IDEDevice dev;
+} IDEDrive;
+
+static int ide_drive_initfn(IDEDevice *dev)
+{
+    IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
+    ide_init_drive(bus->ifs + dev->unit, dev->dinfo);
+    return 0;
+}
+
+static IDEDeviceInfo ide_drive_info = {
+    .qdev.name  = "ide-drive",
+    .qdev.size  = sizeof(IDEDrive),
+    .init       = ide_drive_initfn,
+    .qdev.props = (Property[]) {
+        DEFINE_PROP_UINT32("unit", IDEDrive, dev.unit, -1),
+        DEFINE_PROP_DRIVE("drive", IDEDrive, dev.dinfo),
+        DEFINE_PROP_END_OF_LIST(),
+    }
+};
+
+static void ide_drive_register(void)
+{
+    ide_qdev_register(&ide_drive_info);
+}
+device_init(ide_drive_register);
#13 lore raw

Re: [Qemu-devel] [PATCH 4/8] ide/qdev: add ide bus.

Quoted 4 lines
Quoted 1 line

+ qdev_init(dev);

Callback dev->info is ide_qdev_init(), which can fail, so qdev_init() can fail, can't it?

Indeed, will fix.

cheers,
Gerd

#14 lore raw

PATCH 5/8 ide/pci: convert to qdev.

In reply to Gerd Hoffmann

With this patch applied ide drives (when attached to a pci adapter) can be created via -device, like this:

-drive if=none,id=mydisk,file=/path/to/disk.img
-device ide-drive,drive=mydisk,bus=ide.0,unit=0

Note that creating a master on ide1 doesn't work that way. That is a side effect of qemu creating a cdrom automagically even if you don't ask for it.

---
 Makefile.target |   10 ++-
 hw/ide/pci.c    |  205 ++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 133 insertions(+), 82 deletions(-)
Makefile.target +6 -4
index 0fe8b6a..3c71736 100644
@@ -183,7 +183,8 @@ obj-y += e1000.o
 obj-y += wdt_i6300esb.o
 
 # Hardware support
-obj-i386-y = ide/core.o ide/isa.o ide/pci.o pckbd.o $(sound-obj-y) dma.o isa-bus.o
+obj-i386-y = ide/core.o ide/qdev.o ide/isa.o ide/pci.o
+obj-i386-y += pckbd.o $(sound-obj-y) dma.o isa-bus.o
 obj-i386-y += vga.o vga-pci.o vga-isa.o
 obj-i386-y += fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o
 obj-i386-y += cirrus_vga.o apic.o ioapic.o parallel.o acpi.o piix_pci.o
@@ -192,7 +193,7 @@ obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
 obj-i386-y += ne2000-isa.o
 
 # shared objects
-obj-ppc-y = ppc.o ide/core.o ide/isa.o ide/pci.o ide/macio.o
+obj-ppc-y = ppc.o ide/core.o ide/qdev.o ide/isa.o ide/pci.o ide/macio.o
 obj-ppc-y += vga.o vga-pci.o $(sound-obj-y) dma.o isa-bus.o openpic.o
 # PREP target
 obj-ppc-y += pckbd.o serial.o i8259.o i8254.o fdc.o mc146818rtc.o
@@ -215,7 +216,7 @@ obj-mips-y = mips_r4k.o mips_jazz.o mips_malta.o mips_mipssim.o
 obj-mips-y += mips_timer.o mips_int.o dma.o vga.o serial.o i8254.o i8259.o rc4030.o
 obj-mips-y += vga-pci.o vga-isa.o vga-isa-mm.o
 obj-mips-y += g364fb.o jazz_led.o dp8393x.o
-obj-mips-y += ide/core.o ide/isa.o ide/pci.o
+obj-mips-y += ide/core.o ide/qdev.o ide/isa.o ide/pci.o
 obj-mips-y += gt64xxx.o pckbd.o fdc.o mc146818rtc.o usb-uhci.o acpi.o ds1225y.o
 obj-mips-y += piix4.o parallel.o cirrus_vga.o isa-bus.o pcspk.o $(sound-obj-y)
 obj-mips-y += mipsnet.o ne2000-isa.o
@@ -247,7 +248,8 @@ obj-cris-y += etraxfs_ser.o
 obj-cris-y += pflash_cfi02.o
 
 ifeq ($(TARGET_ARCH), sparc64)
-obj-sparc-y = sun4u.o ide/core.o ide/pci.o isa-bus.o pckbd.o apb_pci.o
+obj-sparc-y = sun4u.o isa-bus.o pckbd.o apb_pci.o
+obj-sparc-y += ide/core.o ide/qdev.o ide/pci.o
 obj-sparc-y += vga.o vga-pci.o
 obj-sparc-y += fdc.o mc146818rtc.o serial.o
 obj-sparc-y += cirrus_vga.o parallel.o
hw/ide/pci.c +127 -78

309 lines not on this page · raw message

Signature · 1 line
1.6.2.5

A message that is not in the archive (sent off-list or not yet received): [email protected]

#16 lore raw

[Qemu-devel] Re: [PATCH 5/8] ide/pci: convert to qdev.

Quoted 7 lines · Juan Quintela

hw/ide/pci.c line 54

Quoted 4 lines · Gerd Hoffmann

With this patch applied ide drives (when attached to a pci adapter) can be created via -device, like this:

hw/ide/pci.c +1 -1
-    IDEBus bus[2];
+    IDEBus *bus[2];

You change a nice static array for a pointer to one array.

I can't spot a pointer here.

Quoted 1 line

VMState is happier with embedded arrays and not pointers to arrays.

There also is no pointer to an array. It is an embedded array holding pointers.

cheers,
Gerd

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw

[Qemu-devel] Re: [PATCH 5/8] ide/pci: convert to qdev.

Quoted 11 lines · Juan Quintela
Quoted 2 lines
Quoted 2 lines

hw/ide/pci.c line 54

Quoted 2 lines
hw/ide/pci.c +1 -1
-    IDEBus bus[2];
+    IDEBus *bus[2];

You notice this little thing "*" that changed?
VMState preffers pretty much embedded arrays than pointers (arrays of pointers have the same problem, basically).

Notice the "preffers" part. VMState can follow pointers, just that each VMStateDescription is a series of offsets in one structure. Having pointers brings to you all the problems associated with ioctl's with pointers in the middle of the payload. It is doable, just not nice.

I don't see the problem here.

You'll need a separate VMStateDescription for a IDEBus anyway, then have all the ide drivers use that, just to avoid duplication.

ide will just be
vmstate_ide_bus(bus)

pci will be something like
vmstate_pci()
/* busmaster stuff goes here */
vmstate_ide_bus(bus[0])
vmstate_ide_bus(bus[1])

I fail to see how it makes a big difference whenever the ide bus is referenced or embedded ...

cheers,
Gerd

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw

[Qemu-devel] Re: [PATCH 5/8] ide/pci: convert to qdev.

Quoted 6 lines · Juan Quintela

But now we look at savevm.c::vmstate_load_state()

        if (field->flags&  VMS_POINTER) {
             base_addr = *(void **)base_addr;
        }
Quoted 6 lines

And you see this really nice piece of code. Each time that we follow a pointer, we have to read something for a table, hope that value is right, and follow it.

Do you see know why I want to have the minimal amount of pointers possible to follow?

No.

Sure, you have to dereference the pointer. I still don't see a problem here. You seem to think this is fragile. Why do you think so? Typechecking missing somewhere?

Quoted 4 lines

And yes, I understand why you don't want qdev_create_here() idea, I am pointing this out to make sure that everybody agrees that not having qdev_create_here() and having rest of code use more pointers/malloc/... is the right compromise.

I'm sure you'll need VMS_POINTER anyway. There will be corner cases which don't work without. I think the floppy fifo is one of them.

It is perfectly fine to avoid the pointer indirection if possible. It isn't the most important thing on earth though.

cheers,
Gerd

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw

[Qemu-devel] Re: [PATCH 5/8] ide/pci: convert to qdev.

Hi,

Quoted 6 lines
Quoted 3 lines

Sure, you have to dereference the pointer. I still don't see a problem here. You seem to think this is fragile. Why do you think so?

Pointer not initialized, it is NULL, point nowhere, etc, etc.
No pointer, you don't need to even think about all that kind of porblems/checks.

NULL can easily be checked for, and that should also catch the "not initialized" case. Pointing into nowhere is a clear bug which needs fixing anyway.

Quoted 3 lines

uint8_t *foo[4]
is a different beast ... how do you declare a pointer to one array of 4 uint8_t?

This isn't a pointer to an array, it is a array of pointers ...

cheers,
Gerd

#23 lore raw

Re: [Qemu-devel] [PATCH 5/8] ide/pci: convert to qdev.

In reply to Gerd Hoffmann
Quoted 16 lines · Gerd Hoffmann

With this patch applied ide drives (when attached to a pci adapter) can be created via -device, like this:

-drive if=none,id=mydisk,file=/path/to/disk.img
-device ide-drive,drive=mydisk,bus=ide.0,unit=0

Note that creating a master on ide1 doesn't work that way. That is a side effect of qemu creating a cdrom automagically even if you don't ask for it.

---
 Makefile.target |   10 ++-
 hw/ide/pci.c    |  205 ++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 133 insertions(+), 82 deletions(-)

[...]

Quoted 4 lines
hw/ide/pci.c +0 -0
index 607472b..aa6daf2 100644

[...]

Quoted 39 lines
hw/ide/pci.c +6 -12
@@ -375,19 +391,13 @@ static void cmd646_reset(void *opaque)
 }
 
 /* CMD646 PCI IDE controller */
-void pci_cmd646_ide_init(PCIBus *bus, DriveInfo **hd_table,
-                         int secondary_ide_enabled)
+static int pci_cmd646_ide_initfn(PCIDevice *dev)
 {
-    PCIIDEState *d;
-    uint8_t *pci_conf;
+    PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
+    uint8_t *pci_conf = d->dev.config;
     qemu_irq *irq;
 
-    d = (PCIIDEState *)pci_register_device(bus, "CMD646 IDE",
-                                           sizeof(PCIIDEState),
-                                           -1,
-                                           NULL, NULL);
     d->type = IDE_TYPE_CMD646;
-    pci_conf = d->dev.config;
     pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_CMD);
     pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_CMD_646);
 
@@ -398,31 +408,47 @@ void pci_cmd646_ide_init(PCIBus *bus, DriveInfo **hd_table,
     pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
 
     pci_conf[0x51] = 0x04; // enable IDE0
-    if (secondary_ide_enabled) {
+    if (d->secondary) {
         /* XXX: if not enabled, really disable the seconday IDE controller */
         pci_conf[0x51] |= 0x08; /* enable IDE1 */
     }
+    pci_conf[0x3d] = 0x01; // interrupt on pin 1
 
     pci_register_bar((PCIDevice *)d, 0, 0x8,
-                           PCI_ADDRESS_SPACE_IO, ide_map);
+                     PCI_ADDRESS_SPACE_IO, ide_map);
     pci_register_bar((PCIDevice *)d, 1, 0x4,
-                           PCI_ADDRESS_SPACE_IO, ide_map);

hw/ide/pci.c void pci_cmd646_ide_init(PCIBus *bus, DriveInfo **hd_table, · line 426

Quoted 10 lines
hw/ide/pci.c +4 -3
+                     PCI_ADDRESS_SPACE_IO, ide_map);
     pci_register_bar((PCIDevice *)d, 2, 0x8,
-                           PCI_ADDRESS_SPACE_IO, ide_map);
+                     PCI_ADDRESS_SPACE_IO, ide_map);
     pci_register_bar((PCIDevice *)d, 3, 0x4,
-                           PCI_ADDRESS_SPACE_IO, ide_map);
+                     PCI_ADDRESS_SPACE_IO, ide_map);
     pci_register_bar((PCIDevice *)d, 4, 0x10,
-                           PCI_ADDRESS_SPACE_IO, bmdma_map);
+                     PCI_ADDRESS_SPACE_IO, bmdma_map);

Please don't mix white-space cleanups with complex changes.

hw/ide/pci.c void pci_cmd646_ide_init(PCIBus *bus, DriveInfo **hd_table, · line 417

Quoted 2 lines
hw/ide/pci.c +0 -1
-    pci_conf[0x3d] = 0x01; // interrupt on pin 1

Any particular reason for moving this assignment up?

Quoted 117 lines
hw/ide/pci.c +42 -47
+    register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
 
-    irq = qemu_allocate_irqs(cmd646_set_irq, d, 2);
-    ide_init2(&d->bus[0], hd_table[0], hd_table[1], irq[0]);
-    ide_init2(&d->bus[1], hd_table[2], hd_table[3], irq[1]);
+    d->bus[0] = ide_bus_new(&d->dev.qdev);
+    d->bus[1] = ide_bus_new(&d->dev.qdev);
 
-    register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
     qemu_register_reset(cmd646_reset, d);
     cmd646_reset(d);
+
+    irq = qemu_allocate_irqs(cmd646_set_irq, d, 2);
+    ide_init2(d->bus[0], NULL, NULL, irq[0]);
+    ide_init2(d->bus[1], NULL, NULL, irq[1]);
+    return 0;
+}
+
+void pci_cmd646_ide_init(PCIBus *bus, DriveInfo **hd_table,
+                         int secondary_ide_enabled)
+{
+    PCIDevice *dev;
+
+    dev = pci_create_noinit(bus, -1, "CMD646 IDE");
+    qdev_prop_set_uint32(&dev->qdev, "secondary", secondary_ide_enabled);
+    qdev_init(&dev->qdev);
+
+    pci_ide_create_devs(dev, hd_table);
 }
 
 static void piix3_reset(void *opaque)
@@ -441,23 +467,10 @@ static void piix3_reset(void *opaque)
     pci_conf[0x20] = 0x01; /* BMIBA: 20-23h */
 }
 
-/* hd_table must contain 4 block drivers */
-/* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
-void pci_piix3_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
+static int pci_piix_ide_initfn(PCIIDEState *d)
 {
-    PCIIDEState *d;
-    uint8_t *pci_conf;
-
-    /* register a function 1 of PIIX3 */
-    d = (PCIIDEState *)pci_register_device(bus, "PIIX3 IDE",
-                                           sizeof(PCIIDEState),
-                                           devfn,
-                                           NULL, NULL);
-    d->type = IDE_TYPE_PIIX3;
+    uint8_t *pci_conf = d->dev.config;
 
-    pci_conf = d->dev.config;
-    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
-    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_1);
     pci_conf[0x09] = 0x80; // legacy ATA mode
     pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
     pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
@@ -466,48 +479,84 @@ void pci_piix3_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
     piix3_reset(d);
 
     pci_register_bar((PCIDevice *)d, 4, 0x10,
-                           PCI_ADDRESS_SPACE_IO, bmdma_map);
-
-    ide_init2(&d->bus[0], hd_table[0], hd_table[1], isa_reserve_irq(14));
-    ide_init2(&d->bus[1], hd_table[2], hd_table[3], isa_reserve_irq(15));
-    ide_init_ioport(&d->bus[0], 0x1f0, 0x3f6);
-    ide_init_ioport(&d->bus[1], 0x170, 0x376);
+                     PCI_ADDRESS_SPACE_IO, bmdma_map);
 
     register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
+
+    d->bus[0] = ide_bus_new(&d->dev.qdev);
+    d->bus[1] = ide_bus_new(&d->dev.qdev);
+    ide_init_ioport(d->bus[0], 0x1f0, 0x3f6);
+    ide_init_ioport(d->bus[1], 0x170, 0x376);
+
+    ide_init2(d->bus[0], NULL, NULL, isa_reserve_irq(14));
+    ide_init2(d->bus[1], NULL, NULL, isa_reserve_irq(15));
+    return 0;
 }
 
-/* hd_table must contain 4 block drivers */
-/* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
-void pci_piix4_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
+static int pci_piix3_ide_initfn(PCIDevice *dev)
 {
-    PCIIDEState *d;
-    uint8_t *pci_conf;
-
-    /* register a function 1 of PIIX4 */
-    d = (PCIIDEState *)pci_register_device(bus, "PIIX4 IDE",
-                                           sizeof(PCIIDEState),
-                                           devfn,
-                                           NULL, NULL);
-    d->type = IDE_TYPE_PIIX4;
+    PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
 
-    pci_conf = d->dev.config;
-    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
-    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB);
-    pci_conf[0x09] = 0x80; // legacy ATA mode
-    pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE);
-    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
+    d->type = IDE_TYPE_PIIX3;
+    pci_config_set_vendor_id(d->dev.config, PCI_VENDOR_ID_INTEL);
+    pci_config_set_device_id(d->dev.config, PCI_DEVICE_ID_INTEL_82371SB_1);
+    return pci_piix_ide_initfn(d);
+}
 
-    qemu_register_reset(piix3_reset, d);
-    piix3_reset(d);
+static int pci_piix4_ide_initfn(PCIDevice *dev)
+{
+    PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);
 
-    pci_register_bar((PCIDevice *)d, 4, 0x10,
-                           PCI_ADDRESS_SPACE_IO, bmdma_map);

hw/ide/pci.c void pci_piix3_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn) · line 509

Quoted 10 lines
hw/ide/pci.c +5 -4
+    d->type = IDE_TYPE_PIIX4;
+    pci_config_set_vendor_id(d->dev.config, PCI_VENDOR_ID_INTEL);
+    pci_config_set_device_id(d->dev.config, PCI_DEVICE_ID_INTEL_82371AB);
+    return pci_piix_ide_initfn(d);
+}
 
-    ide_init2(&d->bus[0], hd_table[0], hd_table[1], isa_reserve_irq(14));
-    ide_init2(&d->bus[1], hd_table[2], hd_table[3], isa_reserve_irq(15));
-    ide_init_ioport(&d->bus[0], 0x1f0, 0x3f6);
-    ide_init_ioport(&d->bus[1], 0x170, 0x376);

This part of the diff is a bit confusing because besides qdevification it also factors out common parts of piix3 and piix4 into pci_piix_ide_initfn(). Good move, but would be easier to review as a separate commit.

Quoted 48 lines
hw/ide/pci.c +44 -1
+/* hd_table must contain 4 block drivers */
+/* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
+void pci_piix3_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
+{
+    PCIDevice *dev;
 
-    register_savevm("ide", 0, 3, pci_ide_save, pci_ide_load, d);
+    dev = pci_create_simple(bus, devfn, "PIIX3 IDE");
+    pci_ide_create_devs(dev, hd_table);
+}
+
+/* hd_table must contain 4 block drivers */
+/* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
+void pci_piix4_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn)
+{
+    PCIDevice *dev;
+
+    dev = pci_create_simple(bus, devfn, "PIIX4 IDE");
+    pci_ide_create_devs(dev, hd_table);
 }
 
+static PCIDeviceInfo piix_ide_info[] = {
+    {
+        .qdev.name    = "PIIX3 IDE",
+        .qdev.size    = sizeof(PCIIDEState),
+        .init         = pci_piix3_ide_initfn,
+    },{
+        .qdev.name    = "PIIX4 IDE",
+        .qdev.size    = sizeof(PCIIDEState),
+        .init         = pci_piix4_ide_initfn,
+    },{
+        .qdev.name    = "CMD646 IDE",
+        .qdev.size    = sizeof(PCIIDEState),
+        .init         = pci_cmd646_ide_initfn,
+        .qdev.props   = (Property[]) {
+            DEFINE_PROP_UINT32("secondary", PCIIDEState, secondary, 0),
+            DEFINE_PROP_END_OF_LIST(),
+        },
+    },{
+        /* end of list */
+    }
+};
+
+static void piix_ide_register(void)
+{
+    pci_qdev_register_many(piix_ide_info);
+}
+device_init(piix_ide_register);
#24 lore raw

Re: [Qemu-devel] [PATCH 5/8] ide/pci: convert to qdev.

Hi,

Quoted 3 lines
Quoted 1 line

- pci_conf[0x3d] = 0x01; // interrupt on pin 1

Any particular reason for moving this assignment up?

Probably just because the patch went through a number of revisions ...

cheers,
Gerd

#25 lore raw

PATCH 6/8 ide/isa: convert to qdev.

In reply to Gerd Hoffmann
---
 hw/ide.h      |    4 +-
 hw/ide/isa.c  |   61 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 hw/mips_r4k.c |    2 +-
 hw/pc.c       |    3 +-
 hw/ppc_prep.c |    2 +-
 5 files changed, 58 insertions(+), 14 deletions(-)
hw/ide.h +2 -2
index e0a508b..0e7d540 100644
@@ -4,8 +4,8 @@
 #include "qdev.h"
 
 /* ide-isa.c */
-void isa_ide_init(int iobase, int iobase2, qemu_irq irq,
-                  DriveInfo *hd0, DriveInfo *hd1);
+int isa_ide_init(int iobase, int iobase2, int isairq,
+                 DriveInfo *hd0, DriveInfo *hd1);
 
 /* ide-pci.c */
 void pci_cmd646_ide_init(PCIBus *bus, DriveInfo **hd_table,
hw/ide/isa.c +53 -8

81 lines not on this page · raw message

hw/mips_r4k.c +1 -1
index d801417..fcc7fed 100644
@@ -274,7 +274,7 @@ void mips_r4k_init (ram_addr_t ram_size,
     }
 
     for(i = 0; i < MAX_IDE_BUS; i++)
-        isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
+        isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
                      hd[MAX_IDE_DEVS * i],
 		     hd[MAX_IDE_DEVS * i + 1]);
 
hw/pc.c +1 -2
index d96d756..96907b2 100644
@@ -1367,8 +1367,7 @@ static void pc_init1(ram_addr_t ram_size,
         pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
     } else {
         for(i = 0; i < MAX_IDE_BUS; i++) {
-            isa_ide_init(ide_iobase[i], ide_iobase2[i],
-                         isa_reserve_irq(ide_irq[i]),
+            isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
 	                 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
         }
     }
hw/ppc_prep.c +1 -1
index 1930146..6001c86 100644
@@ -707,7 +707,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
     }
 
     for(i = 0; i < MAX_IDE_BUS; i++) {
-        isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
+        isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
                      hd[2 * i],
 		     hd[2 * i + 1]);
     }
Signature · 1 line
1.6.2.5
#26 lore raw

PATCH 7/8 isa: refine irq reservations

In reply to Gerd Hoffmann

There are a few cases where IRQ sharing on the ISA bus is used and possible. In general only devices of the same kind can do that. A few use cases:

* serial lines 1+3 share irq 4
* serial lines 2+4 share irq 3
* parallel ports share irq 7
* ppc/prep: ide ports share irq 13

This patch refines the irq reservation mechanism for the isa bus to handle those cases. It keeps track of the driver which owns the IRQ in question and allows irq sharing for devices handled by the same driver.

---
 hw/isa-bus.c |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)
hw/isa-bus.c +13 -3
index 4ecc0f8..1d2ca90 100644
@@ -26,6 +26,7 @@ struct ISABus {
     BusState qbus;
     qemu_irq *irqs;
     uint32_t assigned;
+    DeviceInfo *irq_owner[16];
 };
 static ISABus *isabus;
 
@@ -71,7 +72,9 @@ qemu_irq isa_reserve_irq(int isairq)
         exit(1);
     }
     if (isabus->assigned & (1 << isairq)) {
-        fprintf(stderr, "isa irq %d already assigned\n", isairq);
+        DeviceInfo *owner = isabus->irq_owner[isairq];
+        fprintf(stderr, "isa irq %d already assigned (%s)\n",
+                isairq, owner ? owner->name : "unknown");
         exit(1);
     }
     isabus->assigned |= (1 << isairq);
@@ -82,10 +85,17 @@ void isa_init_irq(ISADevice *dev, qemu_irq *p, int isairq)
 {
     assert(dev->nirqs < ARRAY_SIZE(dev->isairq));
     if (isabus->assigned & (1 << isairq)) {
-        fprintf(stderr, "isa irq %d already assigned\n", isairq);
-        exit(1);
+        DeviceInfo *owner = isabus->irq_owner[isairq];
+        if (owner == dev->qdev.info) {
+            /* irq sharing is ok in case the same driver handles both */;
+        } else {
+            fprintf(stderr, "isa irq %d already assigned (%s)\n",
+                    isairq, owner ? owner->name : "unknown");
+            exit(1);
+        }
     }
     isabus->assigned |= (1 << isairq);
+    isabus->irq_owner[isairq] = dev->qdev.info;
     dev->isairq[dev->nirqs] = isairq;
     *p = isabus->irqs[isairq];
     dev->nirqs++;
Signature · 1 line
1.6.2.5
#27 lore raw

PATCH 8/8 unbreak ppc/prep

In reply to Gerd Hoffmann

Changes:
* added isa bus, hooked up to the system bus. Not sure this is correct, but 'info pci' lists lists no pci-isa bridge in the machine ...).
* switches the default cpu to one which actually works.

---
 hw/ppc_prep.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
hw/ppc_prep.c +4 -1
index 6001c86..5392072 100644
@@ -572,7 +572,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
 
     /* init CPUs */
     if (cpu_model == NULL)
-        cpu_model = "default";
+        cpu_model = "602";
     for (i = 0; i < smp_cpus; i++) {
         env = cpu_init(cpu_model);
         if (!env) {
@@ -670,6 +670,9 @@ static void ppc_prep_init (ram_addr_t ram_size,
     }
     i8259 = i8259_init(first_cpu->irq_inputs[PPC6xx_INPUT_INT]);
     pci_bus = pci_prep_init(i8259);
+    /* Hmm, prep has no pci-isa bridge ??? */
+    isa_bus_new(NULL);
+    isa_bus_irqs(i8259);
     //    pci_bus = i440fx_init();
     /* Register 8 MB of ISA IO space (needed for non-contiguous map) */
     PPC_io_memory = cpu_register_io_memory(PPC_prep_io_read,
Signature · 1 line
1.6.2.5
#28 lore raw
In reply to Gerd Hoffmann
Quoted 5 lines · Gerd Hoffmann

Hi,

Here is a patch series which starts the conversion of ide to qdev. It brings the core infrastructure and converts pci+isa ide adapters to qdev. Also some minor preparatory patches and bugfixes.

I like the cleanup. My reservations are with the qdev allocation of memory, not with this series.

Later, Juan.