Skip to contentSkip to conversation

hw/timer/i8254: Un-inline and simplify IRQs

PATCH In review Philippe Mathieu-Daudé 3 participants 12 messages Feb 15, 2023 – Mar 1, 2023 KVM

lore
Share & tools
lore
#0 lore raw

cover hw/timer/i8254: Un-inline and simplify IRQs

i8254_pit_init() uses a odd pattern of "use this IRQ output line if non-NULL, otherwise use the ISA IRQ #number as output".

Rework as simply "Use this IRQ output".

Un-inline/rename/document functions.

Based-on: <[email protected]>
"hw/ide: Untangle ISA/PCI abuses of ide_init_ioport" v2
https://lore.kernel.org/qemu-devel/[email protected]/

Philippe Mathieu-Daudé (5):
hw/timer/hpet: Include missing 'hw/qdev-properties.h' header hw/timer/i8254: Factor i8254_pit_create() out and document hw/i386/pc: Un-inline i8254_pit_init() hw/timer/i8254: Really inline i8254_pit_init() hw/i386/kvm: Factor i8254_pit_create_try_kvm() out

 hw/i386/kvm/i8254.c        | 18 ++++++++++++++
 hw/i386/microvm.c          |  6 +----
 hw/i386/pc.c               | 15 +++++-------
 hw/isa/i82378.c            |  2 +-
 hw/isa/piix4.c             |  4 ++--
 hw/isa/vt82c686.c          |  2 +-
 hw/mips/jazz.c             |  2 +-
 hw/timer/hpet.c            |  1 +
 hw/timer/i8254.c           | 16 +++++++++++++
 include/hw/timer/i8254.h   | 48 +++++++++++++-------------------------
 target/i386/kvm/kvm-stub.c |  6 +++++
 11 files changed, 69 insertions(+), 51 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/5 hw/timer/hpet: Include missing 'hw/qdev-properties.h' header

Avoid when refactoring unrelated headers:

hw/timer/hpet.c:776:39: error: array has incomplete element type 'Property' (aka 'struct Property')

  static Property hpet_device_properties[] = {
                                        ^

hw/timer/hpet.c:777:5: error: implicit declaration of function 'DEFINE_PROP_UINT8' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS),
^

---
 hw/timer/hpet.c | 1 +
 1 file changed, 1 insertion(+)
hw/timer/hpet.c +1 -0
index 9520471be2..214d6a0501 100644
@@ -30,6 +30,7 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "hw/qdev-properties.h"
 #include "hw/timer/hpet.h"
 #include "hw/sysbus.h"
 #include "hw/rtc/mc146818rtc.h"
Signature · 1 line
2.38.1
Acked-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] hw/timer/hpet: Include missing 'hw/qdev-properties.h' header

Quoted 19 lines · Philippe Mathieu-Daudé

Avoid when refactoring unrelated headers:

hw/timer/hpet.c:776:39: error: array has incomplete element type 'Property' (aka 'struct Property')

   static Property hpet_device_properties[] = {
                                         ^

hw/timer/hpet.c:777:5: error: implicit declaration of function 'DEFINE_PROP_UINT8' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS),
^

---
  hw/timer/hpet.c | 1 +
  1 file changed, 1 insertion(+)
hw/timer/hpet.c +0 -0
index 9520471be2..214d6a0501 100644

hw/timer/hpet.c line 36

Quoted 7 lines
hw/timer/hpet.c +1 -0
@@ -30,6 +30,7 @@
  #include "qapi/error.h"
  #include "qemu/error-report.h"
  #include "qemu/timer.h"
+#include "hw/qdev-properties.h"
  #include "hw/timer/hpet.h"
  #include "hw/sysbus.h"
  #include "hw/rtc/mc146818rtc.h"

r~

#3 lore raw

PATCH 2/5 hw/timer/i8254: Factor i8254_pit_create() out and document

In reply to Philippe Mathieu-Daudé
---
 hw/timer/i8254.c         | 16 ++++++++++++++++
 include/hw/timer/i8254.h | 24 +++++++++++++-----------
 2 files changed, 29 insertions(+), 11 deletions(-)
hw/timer/i8254.c +16 -0
index c8388ea432..9b6932ab08 100644
@@ -26,9 +26,11 @@
 #include "hw/irq.h"
 #include "qemu/module.h"
 #include "qemu/timer.h"
+#include "hw/qdev-properties.h"
 #include "hw/timer/i8254.h"
 #include "hw/timer/i8254_internal.h"
 #include "qom/object.h"
+#include "qapi/error.h"
 
 //#define DEBUG_PIT
 
@@ -47,6 +49,20 @@ struct PITClass {
     DeviceRealize parent_realize;
 };
 
+ISADevice *i8254_pit_create(ISABus *bus, int iobase, qemu_irq irq_in)
+{
+    DeviceState *dev;
+    ISADevice *d;
+
+    d = isa_new(TYPE_I8254);
+    dev = DEVICE(d);
+    qdev_prop_set_uint32(dev, "iobase", iobase);
+    isa_realize_and_unref(d, bus, &error_fatal);
+    qdev_connect_gpio_out(dev, 0, irq_in);
+
+    return d;
+}
+
 static void pit_irq_timer_update(PITChannelState *s, int64_t current_time);
 
 static int pit_get_count(PITChannelState *s)
include/hw/timer/i8254.h +13 -11
index 8402caad30..a0843cae07 100644
@@ -45,21 +45,23 @@ OBJECT_DECLARE_TYPE(PITCommonState, PITCommonClass, PIT_COMMON)
 #define TYPE_I8254 "isa-pit"
 #define TYPE_KVM_I8254 "kvm-pit"
 
+/**
+ * Create and realize a I8254 PIT device on the heap.
+ * @bus: the #ISABus to put it on.
+ * @iobase: the base I/O port.
+ * @irq_in: qemu_irq to connect the PIT output IRQ to.
+ *
+ * Create the device state structure, initialize it, put it on the
+ * specified ISA @bus, and drop the reference to it (the device is realized).
+ */
+ISADevice *i8254_pit_create(ISABus *bus, int iobase, qemu_irq irq_in);
+
 static inline ISADevice *i8254_pit_init(ISABus *bus, int base, int isa_irq,
                                         qemu_irq alt_irq)
 {
-    DeviceState *dev;
-    ISADevice *d;
-
-    d = isa_new(TYPE_I8254);
-    dev = DEVICE(d);
-    qdev_prop_set_uint32(dev, "iobase", base);
-    isa_realize_and_unref(d, bus, &error_fatal);
-    qdev_connect_gpio_out(dev, 0,
-                          isa_irq >= 0 ? isa_bus_get_irq(bus, isa_irq)
+    return i8254_pit_create(bus, base, isa_irq >= 0
+                                       ? isa_bus_get_irq(bus, isa_irq)
                                        : alt_irq);
-
-    return d;
 }
 
 static inline ISADevice *kvm_pit_init(ISABus *bus, int base)
Signature · 1 line
2.38.1
#5 lore raw

PATCH 3/5 hw/i386/pc: Un-inline i8254_pit_init()

In reply to Philippe Mathieu-Daudé

pc_basic_device_init() is the single caller of i8254_pit_init() with a non-NULL 'alt_irq' argument. Open-code i8254_pit_init() by direclty calling i8254_pit_create().

To confirm all other callers pass a NULL 'alt_irq', add an assertion in i8254_pit_init().

---
 hw/i386/pc.c             | 10 +++++-----
 include/hw/timer/i8254.h |  5 ++---
 2 files changed, 7 insertions(+), 8 deletions(-)
hw/i386/pc.c +5 -5
index 79297a6ecd..fe95f6e9f2 100644
@@ -1258,8 +1258,7 @@ void pc_basic_device_init(struct PCMachineState *pcms,
 {
     int i;
     DeviceState *hpet = NULL;
-    int pit_isa_irq = 0;
-    qemu_irq pit_alt_irq = NULL;
+    qemu_irq pit_irq;
     qemu_irq rtc_irq = NULL;
     ISADevice *pit = NULL;
     MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
@@ -1301,9 +1300,10 @@ void pc_basic_device_init(struct PCMachineState *pcms,
         for (i = 0; i < GSI_NUM_PINS; i++) {
             sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
         }
-        pit_isa_irq = -1;
-        pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
+        pit_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
         rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
+    } else {
+        pit_irq = isa_bus_get_irq(isa_bus, 0);
     }
     *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
 
@@ -1314,7 +1314,7 @@ void pc_basic_device_init(struct PCMachineState *pcms,
         if (kvm_pit_in_kernel()) {
             pit = kvm_pit_init(isa_bus, 0x40);
         } else {
-            pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
+            pit = i8254_pit_create(isa_bus, 0x40, pit_irq);
         }
         if (hpet) {
             /* connect PIT to output control line of the HPET */
include/hw/timer/i8254.h +2 -3
index a0843cae07..0d837f3f41 100644
@@ -59,9 +59,8 @@ ISADevice *i8254_pit_create(ISABus *bus, int iobase, qemu_irq irq_in);
 static inline ISADevice *i8254_pit_init(ISABus *bus, int base, int isa_irq,
                                         qemu_irq alt_irq)
 {
-    return i8254_pit_create(bus, base, isa_irq >= 0
-                                       ? isa_bus_get_irq(bus, isa_irq)
-                                       : alt_irq);
+    assert(isa_irq == 0 && alt_irq == NULL);
+    return i8254_pit_create(bus, base, isa_bus_get_irq(bus, 0));
 }
 
 static inline ISADevice *kvm_pit_init(ISABus *bus, int base)
Signature · 1 line
2.38.1
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] hw/i386/pc: Un-inline i8254_pit_init()

Quoted 4 lines · Philippe Mathieu-Daudé

pc_basic_device_init() is the single caller of i8254_pit_init() with a non-NULL 'alt_irq' argument. Open-code i8254_pit_init() by direclty calling i8254_pit_create().

Quoted 8 lines

To confirm all other callers pass a NULL 'alt_irq', add an assertion in i8254_pit_init().

---
  hw/i386/pc.c             | 10 +++++-----
  include/hw/timer/i8254.h |  5 ++---
  2 files changed, 7 insertions(+), 8 deletions(-)

r~

#7 lore raw

PATCH 4/5 hw/timer/i8254: Really inline i8254_pit_init()

In reply to Philippe Mathieu-Daudé

In-line the one-line i8254_pit_create() call.

---
 hw/i386/microvm.c        | 2 +-
 hw/isa/i82378.c          | 2 +-
 hw/isa/piix4.c           | 4 ++--
 hw/isa/vt82c686.c        | 2 +-
 hw/mips/jazz.c           | 2 +-
 include/hw/timer/i8254.h | 7 -------
 6 files changed, 6 insertions(+), 13 deletions(-)
hw/i386/microvm.c +1 -1
index 29f30dd6d3..9204bb4ff2 100644
@@ -261,7 +261,7 @@ static void microvm_devices_init(MicrovmMachineState *mms)
         if (kvm_pit_in_kernel()) {
             kvm_pit_init(isa_bus, 0x40);
         } else {
-            i8254_pit_init(isa_bus, 0x40, 0, NULL);
+            i8254_pit_create(isa_bus, 0x40, isa_bus_get_irq(isa_bus, 0));
         }
     }
 
hw/isa/i82378.c +1 -1
index e3322e03bf..f75f5a23ba 100644
@@ -99,7 +99,7 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
     isa_bus_irqs(isabus, s->i8259);
 
     /* 1 82C54 (pit) */
-    pit = i8254_pit_init(isabus, 0x40, 0, NULL);
+    pit = i8254_pit_create(isabus, 0x40, isa_bus_get_irq(isabus, 0));
 
     /* speaker */
     pcspk_init(isa_new(TYPE_PC_SPEAKER), isabus, pit);
hw/isa/piix4.c +2 -2
index 94e5dc7825..8ff118445f 100644
@@ -214,8 +214,8 @@ static void piix4_realize(PCIDevice *dev, Error **errp)
     /* initialize ISA irqs */
     isa_bus_irqs(isa_bus, s->isa);
 
-    /* initialize pit */
-    i8254_pit_init(isa_bus, 0x40, 0, NULL);
+    /* PIT */
+    i8254_pit_create(isa_bus, 0x40, isa_bus_get_irq(isa_bus, 0));
 
     /* DMA */
     i8257_dma_init(isa_bus, 0);
hw/isa/vt82c686.c +1 -1
index 3f9bd0c04d..297bcda420 100644
@@ -624,7 +624,7 @@ static void via_isa_realize(PCIDevice *d, Error **errp)
 
     s->isa_irqs = i8259_init(isa_bus, *isa_irq);
     isa_bus_irqs(isa_bus, s->isa_irqs);
-    i8254_pit_init(isa_bus, 0x40, 0, NULL);
+    i8254_pit_create(isa_bus, 0x40, isa_bus_get_irq(isa_bus, 0));
     i8257_dma_init(isa_bus, 0);
 
     /* RTC */
hw/mips/jazz.c +1 -1
index 6aefe9a61b..856acfe2a7 100644
@@ -251,7 +251,7 @@ static void mips_jazz_init(MachineState *machine,
     i8259 = i8259_init(isa_bus, env->irq[4]);
     isa_bus_irqs(isa_bus, i8259);
     i8257_dma_init(isa_bus, 0);
-    pit = i8254_pit_init(isa_bus, 0x40, 0, NULL);
+    pit = i8254_pit_create(isa_bus, 0x40, isa_bus_get_irq(isa_bus, 0));
     pcspk_init(isa_new(TYPE_PC_SPEAKER), isa_bus, pit);
 
     /* Video card */
include/hw/timer/i8254.h +0 -7
index 0d837f3f41..aa48c44d39 100644
@@ -56,13 +56,6 @@ OBJECT_DECLARE_TYPE(PITCommonState, PITCommonClass, PIT_COMMON)
  */
 ISADevice *i8254_pit_create(ISABus *bus, int iobase, qemu_irq irq_in);
 
-static inline ISADevice *i8254_pit_init(ISABus *bus, int base, int isa_irq,
-                                        qemu_irq alt_irq)
-{
-    assert(isa_irq == 0 && alt_irq == NULL);
-    return i8254_pit_create(bus, base, isa_bus_get_irq(bus, 0));
-}
-
 static inline ISADevice *kvm_pit_init(ISABus *bus, int base)
 {
     DeviceState *dev;
Signature · 1 line
2.38.1
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] hw/timer/i8254: Really inline i8254_pit_init()

Quoted 11 lines · Philippe Mathieu-Daudé

In-line the one-line i8254_pit_create() call.

---
  hw/i386/microvm.c        | 2 +-
  hw/isa/i82378.c          | 2 +-
  hw/isa/piix4.c           | 4 ++--
  hw/isa/vt82c686.c        | 2 +-
  hw/mips/jazz.c           | 2 +-
  include/hw/timer/i8254.h | 7 -------
  6 files changed, 6 i

r~

#9 lore raw

PATCH 5/5 hw/i386/kvm: Factor i8254_pit_create_try_kvm() out

In reply to Philippe Mathieu-Daudé

Factor a new i8254_pit_create_try_kvm() helper out of the following patter:

  if (kvm_pit_in_kernel()) {
      kvm_pit_init(...);
  } else }
    i8254_pit_create(...);
  }

(adding a stub for non-KVM builds).

Since kvm_pit_init() is only used once, un-inline it and remove the now unused headers from "hw/timer/i8254.h".

---
 hw/i386/kvm/i8254.c        | 18 ++++++++++++++++++
 hw/i386/microvm.c          |  6 +-----
 hw/i386/pc.c               |  7 ++-----
 include/hw/timer/i8254.h   | 22 ++++++----------------
 target/i386/kvm/kvm-stub.c |  6 ++++++
 5 files changed, 33 insertions(+), 26 deletions(-)
hw/i386/kvm/i8254.c +18 -0
index 191a26fa57..9520c98c76 100644
@@ -35,6 +35,7 @@
 #include "hw/qdev-properties-system.h"
 #include "sysemu/kvm.h"
 #include "qom/object.h"
+#include "kvm/kvm_i386.h"
 
 #define KVM_PIT_REINJECT_BIT 0
 
@@ -59,6 +60,23 @@ struct KVMPITClass {
     DeviceRealize parent_realize;
 };
 
+ISADevice *i8254_pit_create_try_kvm(ISABus *bus, int iobase, qemu_irq irq_in)
+{
+    DeviceState *dev;
+    ISADevice *d;
+
+    if (!kvm_pit_in_kernel()) {
+        return i8254_pit_create(bus, iobase, irq_in);
+    }
+
+    d = isa_new(TYPE_KVM_I8254);
+    dev = DEVICE(d);
+    qdev_prop_set_uint32(dev, "iobase", iobase);
+    isa_realize_and_unref(d, bus, &error_fatal);
+
+    return d;
+}
+
 static void kvm_pit_update_clock_offset(KVMPITState *s)
 {
     int64_t offset, clock_offset;
hw/i386/microvm.c +1 -5
index 9204bb4ff2..d93dc8a263 100644
@@ -258,11 +258,7 @@ static void microvm_devices_init(MicrovmMachineState *mms)
     }
 
     if (x86ms->pit == ON_OFF_AUTO_ON || x86ms->pit == ON_OFF_AUTO_AUTO) {
-        if (kvm_pit_in_kernel()) {
-            kvm_pit_init(isa_bus, 0x40);
-        } else {
-            i8254_pit_create(isa_bus, 0x40, isa_bus_get_irq(isa_bus, 0));
-        }
+        i8254_pit_create_try_kvm(isa_bus, 0x40, isa_bus_get_irq(isa_bus, 0));
     }
 
     if (mms->rtc == ON_OFF_AUTO_ON ||
hw/i386/pc.c +2 -5
index fe95f6e9f2..7d9f57c695 100644
@@ -1311,11 +1311,8 @@ void pc_basic_device_init(struct PCMachineState *pcms,
 
     if (!xen_enabled() &&
         (x86ms->pit == ON_OFF_AUTO_AUTO || x86ms->pit == ON_OFF_AUTO_ON)) {
-        if (kvm_pit_in_kernel()) {
-            pit = kvm_pit_init(isa_bus, 0x40);
-        } else {
-            pit = i8254_pit_create(isa_bus, 0x40, pit_irq);
-        }
+        pit = i8254_pit_create_try_kvm(isa_bus, 0x40, pit_irq);
+
         if (hpet) {
             /* connect PIT to output control line of the HPET */
             qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
include/hw/timer/i8254.h +6 -16
index aa48c44d39..9fb18c4ff4 100644
@@ -25,9 +25,6 @@
 #ifndef HW_I8254_H
 #define HW_I8254_H
 
-#include "hw/qdev-properties.h"
-#include "hw/isa/isa.h"
-#include "qapi/error.h"
 #include "qom/object.h"
 
 #define PIT_FREQ 1193182
@@ -55,19 +52,12 @@ OBJECT_DECLARE_TYPE(PITCommonState, PITCommonClass, PIT_COMMON)
  * specified ISA @bus, and drop the reference to it (the device is realized).
  */
 ISADevice *i8254_pit_create(ISABus *bus, int iobase, qemu_irq irq_in);
-
-static inline ISADevice *kvm_pit_init(ISABus *bus, int base)
-{
-    DeviceState *dev;
-    ISADevice *d;
-
-    d = isa_new(TYPE_KVM_I8254);
-    dev = DEVICE(d);
-    qdev_prop_set_uint32(dev, "iobase", base);
-    isa_realize_and_unref(d, bus, &error_fatal);
-
-    return d;
-}
+/**
+ * Try to create and realize a in-kernel I8254 PIT device on the heap.
+ * If KVM is not available or doesn't have in-kernel PIT support, a
+ * emulated PIT is used. See i8254_pit_create.
+ */
+ISADevice *i8254_pit_create_try_kvm(ISABus *bus, int iobase, qemu_irq irq_in);
 
 void pit_set_gate(ISADevice *dev, int channel, int val);
 void pit_get_channel_info(ISADevice *dev, int channel, PITChannelInfo *info);
target/i386/kvm/kvm-stub.c +6 -0
index e052f1c7b0..396da89f55 100644
@@ -10,6 +10,7 @@
  *
  */
 #include "qemu/osdep.h"
+#include "hw/timer/i8254.h"
 #include "cpu.h"
 #include "kvm_i386.h"
 
@@ -49,3 +50,8 @@ void kvm_set_max_apic_id(uint32_t max_apic_id)
 {
     return;
 }
+
+ISADevice *i8254_pit_create_try_kvm(ISABus *bus, int iobase, qemu_irq irq_in)
+{
+    return i8254_pit_create(bus, iobase, irq_in);
+}
Signature · 1 line
2.38.1
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] hw/i386/kvm: Factor i8254_pit_create_try_kvm() out

Quoted 2 lines · Philippe Mathieu-Daudé

Factor a new i8254_pit_create_try_kvm() helper out of the following patter:

pattern

Quoted 12 lines
   if (kvm_pit_in_kernel()) {
       kvm_pit_init(...);
   } else }
     i8254_pit_create(...);
   }

(adding a stub for non-KVM builds).

Since kvm_pit_init() is only used once, un-inline it and remove the now unused headers from "hw/timer/i8254.h".

Quoted 8 lines
---
  hw/i386/kvm/i8254.c        | 18 ++++++++++++++++++
  hw/i386/microvm.c          |  6 +-----
  hw/i386/pc.c               |  7 ++-----
  include/hw/timer/i8254.h   | 22 ++++++----------------
  target/i386/kvm/kvm-stub.c |  6 ++++++
  5 files changed, 33 insertions(+), 26 deletions(-)

r~

Acked-by on the v1 series
#11 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

i8254_pit_init() uses a odd pattern of "use this IRQ output line if non-NULL, otherwise use the ISA IRQ #number as output".

Rework as simply "Use this IRQ output".

Given it also affects KVM I will let Paolo merge this.

Quoted 28 lines

Un-inline/rename/document functions.

Based-on: <[email protected]>
"hw/ide: Untangle ISA/PCI abuses of ide_init_ioport" v2
https://lore.kernel.org/qemu-devel/[email protected]/

Philippe Mathieu-Daudé (5):
hw/timer/hpet: Include missing 'hw/qdev-properties.h' header hw/timer/i8254: Factor i8254_pit_create() out and document hw/i386/pc: Un-inline i8254_pit_init() hw/timer/i8254: Really inline i8254_pit_init() hw/i386/kvm: Factor i8254_pit_create_try_kvm() out

 hw/i386/kvm/i8254.c        | 18 ++++++++++++++
 hw/i386/microvm.c          |  6 +----
 hw/i386/pc.c               | 15 +++++-------
 hw/isa/i82378.c            |  2 +-
 hw/isa/piix4.c             |  4 ++--
 hw/isa/vt82c686.c          |  2 +-
 hw/mips/jazz.c             |  2 +-
 hw/timer/hpet.c            |  1 +
 hw/timer/i8254.c           | 16 +++++++++++++
 include/hw/timer/i8254.h   | 48 +++++++++++++-------------------------
 target/i386/kvm/kvm-stub.c |  6 +++++
 11 files changed, 69 insertions(+), 51 deletions(-)
Signature · 1 line
2.38.1