Make mc146818rtc more self-contained
cover Make mc146818rtc more self-contained
The internal state of mc146818rtc is currently exported as RTCState. This violates encapsulation. Resolve it by first converting the last external user, then by unexporting RTCState.
Bernhard Beschow (2):
isa/piix4: Resolve RTCState attribute
mc146818rtc: Unexport RTCState
hw/isa/piix4.c | 15 +-------------- hw/rtc/mc146818rtc.c | 34 ++++++++++++++++++++++++++++++++++ include/hw/rtc/mc146818rtc.h | 35 +---------------------------------- 3 files changed, 36 insertions(+), 48 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/2 isa/piix4: Resolve RTCState attribute
Assuming that mc146818_rtc_init() is "syntactic sugar" for code that could be converted into configuration in the future, this patch is a step towards this future by freeing piix4 to care about the inner details of mc146818.
Furthermore, by reusing mc146818_rtc_init(), piix4's code becomes more homogenious to other code using the mc146818. So piix4 can be refactored in the same way as code already using mc146818_rtc_init().
--- hw/isa/piix4.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
hw/isa/piix4.c +1 -14
@@ -46,7 +46,6 @@ struct PIIX4State {qemu_irq cpu_intr;qemu_irq *isa; -RTCState rtc;/* Reset Control Register */ MemoryRegion rcr_mem;uint8_t rcr;
@@ -193,22 +192,11 @@ static void piix4_realize(PCIDevice *dev, Error **errp)i8257_dma_init (isa_bus,0 );/* RTC */ -qdev_prop_set_int32 (DEVICE (&s->rtc),"base_year" ,2000 ); -if (!qdev_realize (DEVICE (&s->rtc),BUS (isa_bus), errp)) { -return ; - } -isa_init_irq (ISA_DEVICE (&s->rtc), &s->rtc.irq,RTC_ISA_IRQ ); +mc146818_rtc_init (isa_bus,2000 ,NULL ); piix4_dev = dev; } -static void piix4_init (Object *obj) -{ -PIIX4State *s =PIIX4_PCI_DEVICE (obj); - -object_initialize (&s->rtc,sizeof (s->rtc),TYPE_MC146818_RTC ); -} -static void piix4_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass);
@@ -233,7 +221,6 @@ static const TypeInfo piix4_info = { .name = TYPE_PIIX4_PCI_DEVICE, .parent = TYPE_PCI_DEVICE, .instance_size = sizeof(PIIX4State), - .instance_init = piix4_init, .class_init = piix4_class_init, .interfaces = (InterfaceInfo []) { {INTERFACE_CONVENTIONAL_PCI_DEVICE },
Signature · 1 line
2.35.1
Re: [PATCH 1/2] isa/piix4: Resolve RTCState attribute
Quoted 51 lines · Bernhard Beschow
Assuming that
mc146818_rtc_init()is "syntactic sugar" for code that could be converted into configuration in the future, this patch is a step towards this future by freeing piix4 to care about the inner details of mc146818.Furthermore, by reusing
mc146818_rtc_init(), piix4's code becomes more homogenious to other code using the mc146818. So piix4 can be refactored in the same way as code already usingmc146818_rtc_init().Signed-off-by Bernhard Beschow <[email protected]>--- hw/isa/piix4.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)hw/isa/piix4.c +1
-13@@ -46,7 +46,6 @@ struct PIIX4State {qemu_irq cpu_intr;qemu_irq *isa; -RTCState rtc;/* Reset Control Register */ MemoryRegion rcr_mem;uint8_t rcr;@@ -193,22 +192,11 @@ static void piix4_realize(PCIDevice *dev, Error **errp)i8257_dma_init (isa_bus,0 );/* RTC */ -qdev_prop_set_int32 (DEVICE (&s->rtc),"base_year" ,2000 ); -if (!qdev_realize (DEVICE (&s->rtc),BUS (isa_bus), errp)) { -return ; - } -isa_init_irq (ISA_DEVICE (&s->rtc), &s->rtc.irq,RTC_ISA_IRQ ); +mc146818_rtc_init (isa_bus,2000 ,NULL ); piix4_dev = dev; } -static void piix4_init (Object *obj) -{ -PIIX4State *s =PIIX4_PCI_DEVICE (obj); - -object_initialize (&s->rtc,sizeof (s->rtc),TYPE_MC146818_RTC ); -} -static void piix4_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass);
hw/isa/piix4.c static const TypeInfo piix4_info = { · line 226
Quoted 7 lines
hw/isa/piix4.c +0
-1@@ -233,7 +221,6 @@ static const TypeInfo piix4_info = { .name = TYPE_PIIX4_PCI_DEVICE, .parent = TYPE_PCI_DEVICE, .instance_size = sizeof(PIIX4State), - .instance_init = piix4_init, .class_init = piix4_class_init, .interfaces = (InterfaceInfo []) { {INTERFACE_CONVENTIONAL_PCI_DEVICE },
This looks like it's going backwards from the way we'd usually write code for devices that contain other devices these days. The "we have an init function that does stuff" is the older style. The newer style has the inner-device as an embedded struct in the container-device struct, which is initialized, configured and realized using standard functions like object_initialize and qdev_realize. (I do wonder whether that ought to be object_initialize_child() here, incidentally, but haven't checked the details to be certain.)
The existing uses of mc146818_rtc_init() are mostly in older code, and also in board-initialization code, which traditionally didn't have a convenient struct to embed the device-struct into. hw/isa/vt82c686.c is the only use in another device model (which could in theory be refactored to the embed-the-device-struct style, though the benefit of making the change isn't large, which is one reason we still have the mix of both in the tree).
thanks
-- PMM
Re: [PATCH 1/2] isa/piix4: Resolve RTCState attribute
Quoted 70 lines · Peter Maydell
Quoted 51 lines · Bernhard Beschow
Assuming that
mc146818_rtc_init()is "syntactic sugar" for code that could be converted into configuration in the future, this patch is a step towards this future by freeing piix4 to care about the inner details of mc146818.Furthermore, by reusing
mc146818_rtc_init(), piix4's code becomes more homogenious to other code using the mc146818. So piix4 can be refactored in the same way as code already usingmc146818_rtc_init().Signed-off-by Bernhard Beschow <[email protected]>--- hw/isa/piix4.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)hw/isa/piix4.c +1
-13@@ -46,7 +46,6 @@ struct PIIX4State {qemu_irq cpu_intr;qemu_irq *isa; -RTCState rtc;/* Reset Control Register */ MemoryRegion rcr_mem;uint8_t rcr;@@ -193,22 +192,11 @@ static void piix4_realize(PCIDevice *dev, Error **errp)i8257_dma_init (isa_bus,0 );/* RTC */ -qdev_prop_set_int32 (DEVICE (&s->rtc),"base_year" ,2000 ); -if (!qdev_realize (DEVICE (&s->rtc),BUS (isa_bus), errp)) { -return ; - } -isa_init_irq (ISA_DEVICE (&s->rtc), &s->rtc.irq,RTC_ISA_IRQ ); +mc146818_rtc_init (isa_bus,2000 ,NULL ); piix4_dev = dev; } -static void piix4_init (Object *obj) -{ -PIIX4State *s =PIIX4_PCI_DEVICE (obj); - -object_initialize (&s->rtc,sizeof (s->rtc),TYPE_MC146818_RTC ); -} -static void piix4_class_init (ObjectClass *klass,void *data) {DeviceClass *dc =DEVICE_CLASS (klass);hw/isa/piix4.c static const TypeInfo piix4_info = { · line 226
Quoted 7 lines
hw/isa/piix4.c +0
-1@@ -233,7 +221,6 @@ static const TypeInfo piix4_info = { .name = TYPE_PIIX4_PCI_DEVICE, .parent = TYPE_PCI_DEVICE, .instance_size = sizeof(PIIX4State), - .instance_init = piix4_init, .class_init = piix4_class_init, .interfaces = (InterfaceInfo []) { {INTERFACE_CONVENTIONAL_PCI_DEVICE },This looks like it's going backwards from the way we'd usually write code for devices that contain other devices these days. The "we have an init function that does stuff" is the older style. The newer style has the inner-device as an embedded struct in the container-device struct, which is initialized, configured and realized using standard functions like
object_initializeandqdev_realize. (I do wonder whether that ought to beobject_initialize_child()here, incidentally, but haven't checked the details to be certain.)
Quoted 7 lines
The existing uses of
mc146818_rtc_init()are mostly in older code, and also in board-initialization code, which traditionally didn't have a convenient struct to embed the device-struct into.hw/isa/vt82c686.cis the only use in another device model (which could in theory be refactored to the embed-the-device-struct style, though the benefit of making the change isn't large, which is one reason we still have the mix of both in the tree).
I agree with Peter. The "future" is to remove the ${device}_init() helpers. Some contributors are helping toward that goal, but 1/ it involve work and 2/ there is no coordination; this is a "best effort" project maintenance.
Maybe we should at least list these 'to be removed' functions as a bit-sized task in GitLab issues.
PATCH 2/2 mc146818rtc: Unexport RTCState
In reply to Bernhard BeschowNow that RTCState isn't used outside mc146818rtc.c any more, it can be unexported to prevent outside code to depend on its details.
--- hw/rtc/mc146818rtc.c | 34 ++++++++++++++++++++++++++++++++++ include/hw/rtc/mc146818rtc.h | 35 +---------------------------------- 2 files changed, 35 insertions(+), 34 deletions(-)
hw/rtc/mc146818rtc.c +34 -0
@@ -26,6 +26,7 @@#include "qemu/cutils.h" #include "qemu/module.h" #include "qemu/bcd.h" +#include "qemu/queue.h" #include "hw/acpi/aml-build.h" #include "hw/irq.h" #include "hw/qdev-properties.h"
@@ -41,7 +42,9 @@#include "migration/vmstate.h" #include "qapi/error.h" #include "qapi/qapi-events-misc-target.h" +#include "qapi/qapi-types-machine.h" #include "qapi/visitor.h" +#include "qom/object.h" #include "hw/rtc/mc146818rtc_regs.h" #ifdef TARGET_I386
@@ -74,6 +77,37 @@#define RTC_CLOCK_RATE 32768#define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768) +OBJECT_DECLARE_SIMPLE_TYPE(RTCState, MC146818_RTC) + +struct RTCState { +ISADevice parent_obj; + +MemoryRegion io; +MemoryRegion coalesced_io; +uint8_t cmos_data[128 ]; +uint8_t cmos_index; +int32_t base_year; +uint64_t base_rtc; +uint64_t last_update; +int64_t offset; +qemu_irq irq; +int it_shift; +/* periodic timer */ +QEMUTimer *periodic_timer; +int64_t next_periodic_time; +/* update-ended timer */ +QEMUTimer *update_timer; +uint64_t next_alarm_time; +uint16_t irq_reinject_on_ack_count; +uint32_t irq_coalesced; +uint32_t period; +QEMUTimer *coalesced_timer; +Notifier clock_reset_notifier; +LostTickPolicy lost_tick_policy; +Notifier suspend_notifier; +QLIST_ENTRY (RTCState) link; +}; +static void rtc_set_time (RTCState *s);static void rtc_update_time (RTCState *s);static void rtc_set_cmos (RTCState *s,const struct tm *tm);
include/hw/rtc/mc146818rtc.h +1 -34
@@ -9,43 +9,10 @@#ifndef HW_RTC_MC146818RTC_H #define HW_RTC_MC146818RTC_H -#include "qapi/qapi-types-machine.h" -#include "qemu/queue.h" -#include "qemu/timer.h" #include "hw/isa/isa.h" -#include "qom/object.h" +#include "qemu/typedefs.h" #define TYPE_MC146818_RTC "mc146818rtc" -OBJECT_DECLARE_SIMPLE_TYPE(RTCState, MC146818_RTC) - -struct RTCState { -ISADevice parent_obj; - -MemoryRegion io; -MemoryRegion coalesced_io; -uint8_t cmos_data[128 ]; -uint8_t cmos_index; -int32_t base_year; -uint64_t base_rtc; -uint64_t last_update; -int64_t offset; -qemu_irq irq; -int it_shift; -/* periodic timer */ -QEMUTimer *periodic_timer; -int64_t next_periodic_time; -/* update-ended timer */ -QEMUTimer *update_timer; -uint64_t next_alarm_time; -uint16_t irq_reinject_on_ack_count; -uint32_t irq_coalesced; -uint32_t period; -QEMUTimer *coalesced_timer; -Notifier clock_reset_notifier; -LostTickPolicy lost_tick_policy; -Notifier suspend_notifier; -QLIST_ENTRY (RTCState) link; -};#define RTC_ISA_IRQ 8#define RTC_ISA_BASE 0x70
Signature · 1 line
2.35.1