Patchwork [RFC,v1,12/20] piix3, ich9: create the RTC through composition

login
register
mail settings
Submitter Hu Tao
Date May 22, 2013, 5:33 a.m.
Message ID <4896c887077133a7b7e406c5de3a5ec7d0b35b64.1369193012.git.hutao@cn.fujitsu.com>
Download mbox | patch
Permalink /patch/245521/
State New
Headers show

Comments

Hu Tao - May 22, 2013, 5:33 a.m.
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
---
 hw/i386/pc.c           |  8 +-------
 hw/i386/pc_piix.c      |  6 +++++-
 hw/i386/pc_q35.c       |  6 +++++-
 hw/isa/lpc_ich9.c      | 15 +++++++++++++++
 hw/pci-host/piix.c     | 15 +++++++++++++++
 hw/timer/mc146818rtc.c |  2 +-
 include/hw/i386/ich9.h |  1 +
 include/hw/i386/pc.h   |  2 +-
 8 files changed, 44 insertions(+), 11 deletions(-)

Patch

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index d0e7a41..97bfad4 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -290,7 +290,7 @@  static int set_boot_dev(ISADevice *s, const char *boot_device, int fd_bootchk)
     return(0);
 }
 
-static int pc_boot_set(void *opaque, const char *boot_device)
+int pc_boot_set(void *opaque, const char *boot_device)
 {
     return set_boot_dev(opaque, boot_device, 0);
 }
@@ -1123,14 +1123,12 @@  static const MemoryRegionOps ioportF0_io_ops = {
 };
 
 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
-                          ISADevice **rtc_state,
                           ISADevice **floppy,
                           bool no_vmport)
 {
     int i;
     DriveInfo *fd[MAX_FD];
     DeviceState *hpet = NULL;
-    qemu_irq rtc_irq = NULL;
     qemu_irq *a20_line;
     ISADevice *i8042, *port92, *vmmouse, *pit = NULL;
     qemu_irq *cpu_exit_irq;
@@ -1143,10 +1141,6 @@  void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
     memory_region_init_io(ioportF0_io, &ioportF0_io_ops, NULL, "ioportF0", 1);
     memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
 
-    *rtc_state = rtc_init(isa_bus, 2000, rtc_irq);
-
-    qemu_register_boot_set(pc_boot_set, *rtc_state);
-
     if (!xen_enabled()) {
         if (kvm_irqchip_in_kernel()) {
             pit = kvm_pit_init(isa_bus, 0x40);
diff --git a/hw/i386/pc_piix.c b/hw/i386/pc_piix.c
index 8c9ea6f..80e29d2 100644
--- a/hw/i386/pc_piix.c
+++ b/hw/i386/pc_piix.c
@@ -177,7 +177,7 @@  static void pc_init1(MemoryRegion *system_memory,
     }
 
     /* init basic PC hardware */
-    pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled());
+    pc_basic_device_init(isa_bus, gsi, &floppy, xen_enabled());
 
     pc_nic_init(isa_bus, pci_bus);
 
@@ -201,6 +201,10 @@  static void pc_init1(MemoryRegion *system_memory,
         }
     }
 
+    /* FIXME */
+    rtc_state = ISA_DEVICE(object_resolve_path("rtc", NULL));
+    qemu_register_boot_set(pc_boot_set, rtc_state);
+
     pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                  floppy, idebus[0], idebus[1], rtc_state);
 
diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c
index 4797118..7673b8a 100644
--- a/hw/i386/pc_q35.c
+++ b/hw/i386/pc_q35.c
@@ -172,7 +172,7 @@  static void pc_q35_init(QEMUMachineInitArgs *args)
     pc_register_ferr_irq(gsi[13]);
 
     /* init basic PC hardware */
-    pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, false);
+    pc_basic_device_init(isa_bus, gsi, &floppy, false);
 
     /* connect pm stuff to lpc */
     ich9_lpc_pm_init(lpc);
@@ -196,6 +196,10 @@  static void pc_q35_init(QEMUMachineInitArgs *args)
                                     0xb100),
                       8, NULL, 0);
 
+    /* FIXME */
+    rtc_state = ISA_DEVICE(object_resolve_path("rtc", NULL));
+    qemu_register_boot_set(pc_boot_set, rtc_state);
+
     pc_cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device,
                  floppy, idebus[0], idebus[1], rtc_state);
 
diff --git a/hw/isa/lpc_ich9.c b/hw/isa/lpc_ich9.c
index 8d6da8d..c50795a 100644
--- a/hw/isa/lpc_ich9.c
+++ b/hw/isa/lpc_ich9.c
@@ -45,6 +45,7 @@ 
 #include "exec/address-spaces.h"
 #include "sysemu/sysemu.h"
 #include "hw/timer/hpet.h"
+#include "hw/timer/mc146818rtc.h"
 
 static int ich9_lpc_sci_irq(ICH9LPCState *lpc);
 
@@ -528,6 +529,7 @@  static const MemoryRegionOps ich9_rst_cnt_ops = {
 static int ich9_lpc_realize(PCIDevice *d)
 {
     ICH9LPCState *lpc = ICH9_LPC_DEVICE(d);
+    qemu_irq rtc_irq;
     ISABus *isa_bus;
 
     isa_bus = isa_bus_new(&d->qdev, get_system_io());
@@ -552,6 +554,10 @@  static int ich9_lpc_realize(PCIDevice *d)
                                         ICH9_RST_CNT_IOPORT, &lpc->rst_cnt_mem,
                                         1);
 
+    /* Realize the RTC */
+    qdev_set_parent_bus(DEVICE(lpc->rtc), BUS(lpc->isa_bus));
+    qdev_init_nofail(DEVICE(lpc->rtc));
+
     /* Realize HPET */
     if (lpc->hpet) {
         int i;
@@ -565,8 +571,13 @@  static int ich9_lpc_realize(PCIDevice *d)
         for (i = 0; i < GSI_NUM_PINS; i++) {
             sysbus_connect_irq(SYS_BUS_DEVICE(lpc->hpet), i, lpc->pic[i]);
         }
+        rtc_irq = qdev_get_gpio_in(lpc->hpet, HPET_LEGACY_RTC_INT);
+    } else {
+        isa_init_irq(lpc->rtc, &rtc_irq, RTC_ISA_IRQ);
     }
 
+    rtc_set_irq(lpc->rtc, rtc_irq);
+
     return 0;
 }
 
@@ -624,6 +635,10 @@  static void ich9_lpc_initfn(Object *obj)
         s->hpet = DEVICE(object_new(TYPE_HPET));
         object_property_add_child(obj, "hpet", OBJECT(s->hpet), NULL);
     }
+
+    s->rtc = ISA_DEVICE(object_new(TYPE_MC146818_RTC));
+    qdev_prop_set_int32(DEVICE(s->rtc), "base_year", 2000);
+    object_property_add_child(obj, "rtc", OBJECT(s->rtc), NULL);
 }
 
 static void ich9_lpc_class_init(ObjectClass *klass, void *data)
diff --git a/hw/pci-host/piix.c b/hw/pci-host/piix.c
index b6bfdf0..60b16d8 100644
--- a/hw/pci-host/piix.c
+++ b/hw/pci-host/piix.c
@@ -33,6 +33,7 @@ 
 #include "hw/pci-host/pam.h"
 #include "sysemu/sysemu.h"
 #include "hw/timer/hpet.h"
+#include "hw/timer/mc146818rtc.h"
 
 /*
  * I440FX chipset data sheet.
@@ -72,6 +73,7 @@  typedef struct PIIX3State {
 
     ISABus *bus;
     DeviceState *hpet;
+    ISADevice *rtc;
 
     qemu_irq *pic;
 
@@ -579,9 +581,14 @@  static const MemoryRegionOps rcr_ops = {
 static int piix3_realize(PCIDevice *dev)
 {
     PIIX3State *s = PIIX3(dev);
+    qemu_irq rtc_irq;
 
     s->bus = isa_bus_new(DEVICE(s), pci_address_space_io(dev));
 
+    /* Realize the RTC */
+    qdev_set_parent_bus(DEVICE(s->rtc), BUS(s->bus));
+    qdev_init_nofail(DEVICE(s->rtc));
+
     /* Realize HPET */
     if (s->hpet) {
         int i;
@@ -595,8 +602,13 @@  static int piix3_realize(PCIDevice *dev)
         for (i = 0; i < GSI_NUM_PINS; i++) {
             sysbus_connect_irq(SYS_BUS_DEVICE(s->hpet), i, s->pic[i]);
         }
+        rtc_irq = qdev_get_gpio_in(s->hpet, HPET_LEGACY_RTC_INT);
+    } else {
+        isa_init_irq(s->rtc, &rtc_irq, RTC_ISA_IRQ);
     }
 
+    rtc_set_irq(s->rtc, rtc_irq);
+
     memory_region_init_io(&s->rcr_mem, &rcr_ops, s, "piix3-reset-control", 1);
     memory_region_add_subregion_overlap(pci_address_space_io(dev), RCR_IOPORT,
                                         &s->rcr_mem, 1);
@@ -619,6 +631,9 @@  static void piix3_initfn(Object *obj)
         object_property_add_child(obj, "hpet", OBJECT(s->hpet), NULL);
     }
 
+    s->rtc = ISA_DEVICE(object_new(TYPE_MC146818_RTC));
+    qdev_prop_set_int32(DEVICE(s->rtc), "base_year", 2000);
+    object_property_add_child(obj, "rtc", OBJECT(s->rtc), NULL);
 }
 
 static void piix3_class_init(ObjectClass *klass, void *data)
diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c
index 2fdae55..19429d0 100644
--- a/hw/timer/mc146818rtc.c
+++ b/hw/timer/mc146818rtc.c
@@ -729,7 +729,7 @@  static int rtc_post_load(void *opaque, int version_id)
 }
 
 static const VMStateDescription vmstate_rtc = {
-    .name = "mc146818rtc",
+    .name = TYPE_MC146818_RTC,
     .version_id = 3,
     .minimum_version_id = 1,
     .minimum_version_id_old = 1,
diff --git a/include/hw/i386/ich9.h b/include/hw/i386/ich9.h
index 8dc7d51..1d9f26a 100644
--- a/include/hw/i386/ich9.h
+++ b/include/hw/i386/ich9.h
@@ -63,6 +63,7 @@  typedef struct ICH9LPCState {
     Notifier machine_ready;
 
     DeviceState *hpet;
+    ISADevice *rtc;
 
     qemu_irq *pic;
     qemu_irq *ioapic;
diff --git a/include/hw/i386/pc.h b/include/hw/i386/pc.h
index edd4618..99fef1c 100644
--- a/include/hw/i386/pc.h
+++ b/include/hw/i386/pc.h
@@ -92,7 +92,6 @@  void *pc_memory_init(MemoryRegion *system_memory,
 qemu_irq *pc_allocate_cpu_irq(void);
 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus);
 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
-                          ISADevice **rtc_state,
                           ISADevice **floppy,
                           bool no_vmport);
 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd);
@@ -102,6 +101,7 @@  void pc_cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
                   ISADevice *s);
 void pc_nic_init(ISABus *isa_bus, PCIBus *pci_bus);
 void pc_pci_device_init(PCIBus *pci_bus);
+int pc_boot_set(void *opaque, const char *boot_device);
 
 typedef void (*cpu_set_smm_t)(int smm, void *arg);
 void cpu_smm_register(cpu_set_smm_t callback, void *arg);