Skip to contentSkip to conversation

pseries: Fix and extend PAPR RTC implementation

PATCH Superseded David Gibson 2 participants 18 messages Dec 23, 2014 – Jan 28, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover pseries: Fix and extend PAPR RTC implementation

At the moment, the PAPR RTC implementation (actually a paravirt firmware interface, rather than a normal device) works directly off host time, and so doesn't respect the options such as clock=vm which can be specified in the -rtc command line option.

Extending the PAPR RTC in this way makes it clearer it should probably be its own separate qdev, so this series also implements that.

Finally it moves the code from mc146818rtc for publishing the RTC time via QOM into generic QOM helpers, and extends the PAPR RTC driver to export in a similar way.

I've done some basic sanity testing on this, but not much. In particular I haven't tested the migration code much yet, so that might need revision still.

Changes since v1:
* Actually create a qdev device for the PAPR RTC
* Instead of duplicating the QOM publishing code from mc146818, move it into a generic helper

#1 lore raw

PATCH v2 1/8 pseries: Move sPAPR RTC code into its own file

At the moment the RTAS (firmware/hypervisor) time of day functions are implemented in spapr_rtas.c along with a bunch of other things. Since we're going to be expanding these a bit, move the RTAS RTC related code out into new file spapr_rtc.c. Also add its own initialization function, spapr_rtc_init() called from the main machine init routine.

---
 hw/ppc/Makefile.objs   |  2 +-
 hw/ppc/spapr.c         |  3 ++
 hw/ppc/spapr_rtas.c    | 49 -----------------------------
 hw/ppc/spapr_rtc.c     | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/spapr.h |  1 +
 5 files changed, 88 insertions(+), 50 deletions(-)
 create mode 100644 hw/ppc/spapr_rtc.c
hw/ppc/Makefile.objs +1 -1
index 19d9920..437955d 100644
@@ -3,7 +3,7 @@ obj-y += ppc.o ppc_booke.o
 # IBM pSeries (sPAPR)
 obj-$(CONFIG_PSERIES) += spapr.o spapr_vio.o spapr_events.o
 obj-$(CONFIG_PSERIES) += spapr_hcall.o spapr_iommu.o spapr_rtas.o
-obj-$(CONFIG_PSERIES) += spapr_pci.o
+obj-$(CONFIG_PSERIES) += spapr_pci.o spapr_rtc.o
 ifeq ($(CONFIG_PCI)$(CONFIG_PSERIES)$(CONFIG_LINUX), yyy)
 obj-y += spapr_pci_vfio.o
 endif
hw/ppc/spapr.c +3 -0
index 30de25d..16377a3 100644
@@ -1446,6 +1446,9 @@ static void ppc_spapr_init(MachineState *machine)
     /* Set up EPOW events infrastructure */
     spapr_events_init(spapr);
 
+    /* Set up the RTC RTAS interfaces */
+    spapr_rtc_init();
+
     /* Set up VIO bus */
     spapr->vio_bus = spapr_vio_bus_init();
 
hw/ppc/spapr_rtas.c +0 -49

61 lines not on this page · raw message

hw/ppc/spapr_rtc.c new file +83 -0

83 lines not on this page · raw message

include/hw/ppc/spapr.h +1 -0
index 749daf4..9a468bc 100644
@@ -479,5 +479,6 @@ int spapr_dma_dt(void *fdt, int node_off, const char *propname,
                  uint32_t liobn, uint64_t window, uint32_t size);
 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
                       sPAPRTCETable *tcet);
+void spapr_rtc_init(void);
 
 #endif /* !defined (__HW_SPAPR_H__) */
Signature · 1 line
2.1.0
#2 lore raw

PATCH v2 2/8 pseries: Add more parameter validation in RTAS time of day functions

In reply to David Gibson

Currently, the RTAS time of day functions only partially validate the number of parameters they receive and return. Because of how the parameters are used, this is unlikely to lead to a crash, but it's messy.

This patch adds the missing checks.

---
 hw/ppc/spapr_rtc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/ppc/spapr_rtc.c +6 -1
index e290ac0..13eeab8 100644
@@ -36,7 +36,7 @@ static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
 {
     struct tm tm;
 
-    if (nret != 8) {
+    if ((nargs != 0) || (nret != 8)) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
         return;
     }
@@ -60,6 +60,11 @@ static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
 {
     struct tm tm;
 
+    if ((nargs != 7) || (nret != 1)) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
     tm.tm_year = rtas_ld(args, 0) - 1900;
     tm.tm_mon = rtas_ld(args, 1) - 1;
     tm.tm_mday = rtas_ld(args, 2);
Signature · 1 line
2.1.0
#3 lore raw

PATCH v2 3/8 pseries: Add spapr_rtc_read() helper function

In reply to David Gibson

The virtual RTC time is used in two places in the pseries machine. First is in the RTAS get-time-of-day function which returns the RTC time to the guest. Second is in the spapr events code which is used to timestamp event messages from the hypervisor to the guest.

Currently both call qemu_get_timedate() directly, but we want to change that so we can properly handle the various -rtc options. In preparation, create a helper function to return the virtual RTC time.

---
 hw/ppc/spapr_events.c  |  2 +-
 hw/ppc/spapr_rtc.c     | 13 +++++++++++--
 include/hw/ppc/spapr.h |  1 +
 3 files changed, 13 insertions(+), 3 deletions(-)
hw/ppc/spapr_events.c +1 -1
index 1b6157d..80c0266 100644
@@ -246,7 +246,7 @@ static void spapr_powerdown_req(Notifier *n, void *opaque)
     maina->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINA);
     maina->hdr.section_length = cpu_to_be16(sizeof(*maina));
     /* FIXME: section version, subtype and creator id? */
-    qemu_get_timedate(&tm, spapr->rtc_offset);
+    spapr_rtc_read(spapr, &tm, NULL);
     year = tm.tm_year + 1900;
     maina->creation_date = cpu_to_be32((to_bcd(year / 100) << 24)
                                        | (to_bcd(year % 100) << 16)
hw/ppc/spapr_rtc.c +11 -2
index 13eeab8..793368f 100644
@@ -29,19 +29,28 @@
 #include "hw/ppc/spapr.h"
 #include "qapi-event.h"
 
+void spapr_rtc_read(sPAPREnvironment *spapr, struct tm *tm, uint32_t *ns)
+{
+    qemu_get_timedate(tm, spapr->rtc_offset);
+    if (ns) {
+        *ns = 0; /* we don't do nanoseconds, yet */
+    }
+}
+
 static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
                                  uint32_t token, uint32_t nargs,
                                  target_ulong args,
                                  uint32_t nret, target_ulong rets)
 {
     struct tm tm;
+    uint32_t ns;
 
     if ((nargs != 0) || (nret != 8)) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
         return;
     }
 
-    qemu_get_timedate(&tm, spapr->rtc_offset);
+    spapr_rtc_read(spapr, &tm, &ns);
 
     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
     rtas_st(rets, 1, tm.tm_year + 1900);
@@ -50,7 +59,7 @@ static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
     rtas_st(rets, 4, tm.tm_hour);
     rtas_st(rets, 5, tm.tm_min);
     rtas_st(rets, 6, tm.tm_sec);
-    rtas_st(rets, 7, 0); /* we don't do nanoseconds */
+    rtas_st(rets, 7, ns);
 }
 
 static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
include/hw/ppc/spapr.h +1 -0
index 9a468bc..80fafa9 100644
@@ -480,5 +480,6 @@ int spapr_dma_dt(void *fdt, int node_off, const char *propname,
 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
                       sPAPRTCETable *tcet);
 void spapr_rtc_init(void);
+void spapr_rtc_read(sPAPREnvironment *spapr, struct tm *tm, uint32_t *ns);
 
 #endif /* !defined (__HW_SPAPR_H__) */
Signature · 1 line
2.1.0
#4 lore raw

PATCH v2 4/8 Generalize QOM publishing of date and time from mc146818rtc.c

In reply to David Gibson

The mc146818rtc driver exposes the current RTC date and time via the "date" property in QOM (which is also aliased to the machine's "rtc-time" property). Currently it uses a custom visitor function rtc_get_date to do this.

This patch introduces new helpers to the QOM core to expose struct tm valued properties via a getter function, so that this functionality can be more easily duplicated in other RTC implementations.

---
 hw/timer/mc146818rtc.c | 44 ++--------------------------
 include/qom/object.h   | 14 +++++++++
 qom/object.c           | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+), 41 deletions(-)
hw/timer/mc146818rtc.c +3 -41
index f18d128..ecd576d 100644
@@ -831,49 +831,12 @@ static const MemoryRegionOps cmos_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
-static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
-                         const char *name, Error **errp)
+static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp)
 {
-    Error *err = NULL;
     RTCState *s = MC146818_RTC(obj);
-    struct tm current_tm;
 
     rtc_update_time(s);
-    rtc_get_time(s, &current_tm);
-    visit_start_struct(v, NULL, "struct tm", name, 0, &err);
-    if (err) {
-        goto out;
-    }
-    visit_type_int32(v, &current_tm.tm_year, "tm_year", &err);
-    if (err) {
-        goto out_end;
-    }
-    visit_type_int32(v, &current_tm.tm_mon, "tm_mon", &err);
-    if (err) {
-        goto out_end;
-    }
-    visit_type_int32(v, &current_tm.tm_mday, "tm_mday", &err);
-    if (err) {
-        goto out_end;
-    }
-    visit_type_int32(v, &current_tm.tm_hour, "tm_hour", &err);
-    if (err) {
-        goto out_end;
-    }
-    visit_type_int32(v, &current_tm.tm_min, "tm_min", &err);
-    if (err) {
-        goto out_end;
-    }
-    visit_type_int32(v, &current_tm.tm_sec, "tm_sec", &err);
-    if (err) {
-        goto out_end;
-    }
-out_end:
-    error_propagate(errp, err);
-    err = NULL;
-    visit_end_struct(v, errp);
-out:
-    error_propagate(errp, err);
+    rtc_get_time(s, current_tm);
 }
 
 static void rtc_realizefn(DeviceState *dev, Error **errp)
@@ -932,8 +895,7 @@ static void rtc_realizefn(DeviceState *dev, Error **errp)
     qdev_set_legacy_instance_id(dev, base, 3);
     qemu_register_reset(rtc_reset, s);
 
-    object_property_add(OBJECT(s), "date", "struct tm",
-                        rtc_get_date, NULL, NULL, s, NULL);
+    object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL);
 
     object_property_add_alias(qdev_get_machine(), "rtc-time",
                               OBJECT(s), "date", NULL);
include/qom/object.h +14 -0
index 89c3092..ba17146 100644
@@ -1204,6 +1204,20 @@ void object_property_add_bool(Object *obj, const char *name,
                               Error **errp);
 
 /**
+ * object_property_add_tm:
+ * @obj: the object to add a property to
+ * @name: the name of the property
+ * @get: the getter or NULL if the property is write-only.
+ * @errp: if an error occurs, a pointer to an area to store the error
+ *
+ * Add a read-only struct tm valued property using a getter function.
+ * This function will add a property of type 'struct tm'.
+ */
+void object_property_add_tm(Object *obj, const char *name,
+                            void (*get)(Object *, struct tm *, Error **),
+                            Error **errp);
+
+/**
  * object_property_add_uint8_ptr:
  * @obj: the object to add a property to
  * @name: the name of the property
qom/object.c +79 -0

85 lines not on this page · raw message

Signature · 1 line
2.1.0
#5 lore raw

PATCH v2 5/8 pseries: Make RTAS time of day functions respect -rtc options

In reply to David Gibson

In the 'pseries' machine the real time clock is provided by a paravirtualized firmware interface rather than a device per se; the RTAS get-time-of-day and set-time-of-day calls.

Out current implementations of those work directly off host time (with an offset), not respecting options such as clock=vm which can be specified in the -rtc command line option.

This patch reworks the RTAS RTC code to respect those options, primarily by basing them on the qemu_clock_get_ns(rtc_clock) function instead of directly on qemu_get_timedate() (which essentially handles host time, not virtual rtc time).

As a bonus, this means our get-time-of-day function now also returns nanoseconds.

---
 hw/ppc/spapr_rtc.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)
hw/ppc/spapr_rtc.c +36 -4
index 793368f..d6c7a22 100644
@@ -26,14 +26,24 @@
  *
  */
 #include "cpu.h"
+#include "sysemu/sysemu.h"
 #include "hw/ppc/spapr.h"
 #include "qapi-event.h"
 
+#define NSEC_PER_SEC    1000000000LL
+
 void spapr_rtc_read(sPAPREnvironment *spapr, struct tm *tm, uint32_t *ns)
 {
-    qemu_get_timedate(tm, spapr->rtc_offset);
+    int64_t host_ns = qemu_clock_get_ns(rtc_clock);
+    time_t guest_s;
+
+    guest_s = host_ns / NSEC_PER_SEC + spapr->rtc_offset;
+
+    if (tm) {
+        gmtime_r(&guest_s, tm);
+    }
     if (ns) {
-        *ns = 0; /* we don't do nanoseconds, yet */
+        *ns = host_ns % NSEC_PER_SEC;
     }
 }
 
@@ -68,6 +78,8 @@ static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
                                  uint32_t nret, target_ulong rets)
 {
     struct tm tm;
+    time_t new_s;
+    int64_t host_ns;
 
     if ((nargs != 7) || (nret != 1)) {
         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
@@ -81,15 +93,35 @@ static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
     tm.tm_min = rtas_ld(args, 4);
     tm.tm_sec = rtas_ld(args, 5);
 
-    /* Just generate a monitor event for the change */
+    new_s = mktimegm(&tm);
+    if (new_s == -1) {
+        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
+        return;
+    }
+
+    /* Generate a monitor event for the change */
     qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
-    spapr->rtc_offset = qemu_timedate_diff(&tm);
+
+    host_ns = qemu_clock_get_ns(rtc_clock);
+
+    spapr->rtc_offset = new_s - host_ns / NSEC_PER_SEC;
 
     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 }
 
 void spapr_rtc_init(void)
 {
+    struct tm tm;
+    time_t host_s;
+    int64_t rtc_ns;
+
+    /* Initialize the RTAS RTC from host time */
+
+    qemu_get_timedate(&tm, 0);
+    host_s = mktimegm(&tm);
+    rtc_ns = qemu_clock_get_ns(rtc_clock);
+    spapr->rtc_offset = host_s - rtc_ns / NSEC_PER_SEC;
+
     spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
                         rtas_get_time_of_day);
     spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
Signature · 1 line
2.1.0
#6 lore raw

PATCH v2 6/8 pseries: Make the PAPR RTC a qdev device

In reply to David Gibson

At present the PAPR RTC isn't a "device" as such - it's accessed only via firmware/hypervisor calls, and is handled in the sPAPR core code. This becomes inconvenient as we extend it in various ways.

This patch makes the PAPR RTC a separate device in the qemu device model.

For now, the only piece of device state - the rtc_offset - is still kept in the global sPAPREnvironment structure. That's clearly wrong, but leaving it to be fixed in a following patch makes for a clearer separation between the internal re-organization of the device, and the behavioural changes (because the migration stream format needs to change slightly when the offset is moved into the device's own state).

---
 hw/ppc/spapr.c         | 10 +++++++++-
 hw/ppc/spapr_events.c  |  2 +-
 hw/ppc/spapr_rtc.c     | 49 ++++++++++++++++++++++++++++++++++++++++++++++---
 include/hw/ppc/spapr.h |  7 +++++--
 4 files changed, 61 insertions(+), 7 deletions(-)
hw/ppc/spapr.c +9 -1
index 16377a3..9722b42 100644
@@ -955,6 +955,14 @@ static void spapr_create_nvram(sPAPREnvironment *spapr)
     spapr->nvram = (struct sPAPRNVRAM *)dev;
 }
 
+static void spapr_rtc_create(sPAPREnvironment *spapr)
+{
+    DeviceState *dev = qdev_create(NULL, TYPE_SPAPR_RTC);
+
+    qdev_init_nofail(dev);
+    spapr->rtc = dev;
+}
+
 /* Returns whether we want to use VGA or not */
 static int spapr_vga_init(PCIBus *pci_bus)
 {
@@ -1447,7 +1455,7 @@ static void ppc_spapr_init(MachineState *machine)
     spapr_events_init(spapr);
 
     /* Set up the RTC RTAS interfaces */
-    spapr_rtc_init();
+    spapr_rtc_create(spapr);
 
     /* Set up VIO bus */
     spapr->vio_bus = spapr_vio_bus_init();
hw/ppc/spapr_events.c +1 -1
index 80c0266..283e96b 100644
@@ -246,7 +246,7 @@ static void spapr_powerdown_req(Notifier *n, void *opaque)
     maina->hdr.section_id = cpu_to_be16(RTAS_LOG_V6_SECTION_ID_MAINA);
     maina->hdr.section_length = cpu_to_be16(sizeof(*maina));
     /* FIXME: section version, subtype and creator id? */
-    spapr_rtc_read(spapr, &tm, NULL);
+    spapr_rtc_read(spapr->rtc, &tm, NULL);
     year = tm.tm_year + 1900;
     maina->creation_date = cpu_to_be32((to_bcd(year / 100) << 24)
                                        | (to_bcd(year % 100) << 16)
hw/ppc/spapr_rtc.c +46 -3

88 lines not on this page · raw message

include/hw/ppc/spapr.h +5 -2
index 80fafa9..9b7cc9d 100644
@@ -15,6 +15,7 @@ typedef struct sPAPREnvironment {
     QLIST_HEAD(, sPAPRPHBState) phbs;
     struct sPAPRNVRAM *nvram;
     XICSState *icp;
+    DeviceState *rtc;
 
     hwaddr ram_limit;
     void *htab;
@@ -479,7 +480,9 @@ int spapr_dma_dt(void *fdt, int node_off, const char *propname,
                  uint32_t liobn, uint64_t window, uint32_t size);
 int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
                       sPAPRTCETable *tcet);
-void spapr_rtc_init(void);
-void spapr_rtc_read(sPAPREnvironment *spapr, struct tm *tm, uint32_t *ns);
+
+#define TYPE_SPAPR_RTC "spapr-rtc"
+
+void spapr_rtc_read(DeviceState *dev, struct tm *tm, uint32_t *ns);
 
 #endif /* !defined (__HW_SPAPR_H__) */
Signature · 1 line
2.1.0
#7 lore raw

PATCH v2 7/8 pseries: Move rtc_offset into RTC device's state structure

In reply to David Gibson

The initial creation of the PAPR RTC qdev class left a wart - the rtc's offset was left in the sPAPREnvironment structure, accessed via a global.

This patch moves it into the RTC device's own state structure, were it belongs. This requires a small change to the migration stream format. In order to handle incoming streams from older versions, we also need to retain the rtc_offset field in the sPAPREnvironment structure, so that it can be loaded into via the vmsd, then pushed into the RTC device.

Since we're changing the migration format, this also takes the opportunity to change the rtc offset from a value in seconds to a value in nanoseconds, allowing nanosecond offsets between host and guest rtc time, if desired.

---
 hw/ppc/spapr.c         | 19 ++++++++++++++++++-
 hw/ppc/spapr_rtc.c     | 41 +++++++++++++++++++++++++++++++++++++----
 include/hw/ppc/spapr.h |  3 ++-
 3 files changed, 57 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +18 -1
index 9722b42..3070be0 100644
@@ -980,10 +980,27 @@ static int spapr_vga_init(PCIBus *pci_bus)
     }
 }
 
+static int spapr_post_load(void *opaque, int version_id)
+{
+    sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
+    int err = 0;
+
+    /* In earlier versions, there was no seperate qdev for the PAPR
+     * RTC, so the RTC offset was stored directly in sPAPREnvironment.
+     * So when migrating from those versions, poke the incoming offset
+     * value into the RTC device */
+    if (version_id < 3) {
+        err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);
+    }
+
+    return err;
+}
+
 static const VMStateDescription vmstate_spapr = {
     .name = "spapr",
-    .version_id = 2,
+    .version_id = 3,
     .minimum_version_id = 1,
+    .post_load = spapr_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UNUSED(4), /* used to be @next_irq */
 
hw/ppc/spapr_rtc.c +37 -4

98 lines not on this page · raw message

include/hw/ppc/spapr.h +2 -1
index 9b7cc9d..e07c3a2 100644
@@ -27,7 +27,7 @@ typedef struct sPAPREnvironment {
     void *rtas_blob;
     void *fdt_skel;
     target_ulong entry_point;
-    uint64_t rtc_offset;
+    uint64_t rtc_offset; /* Now used only during incoming migration */
     struct PPCTimebase tb;
     bool has_graphics;
 
@@ -484,5 +484,6 @@ int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
 #define TYPE_SPAPR_RTC "spapr-rtc"
 
 void spapr_rtc_read(DeviceState *dev, struct tm *tm, uint32_t *ns);
+int spapr_rtc_import_offset(DeviceState *dev, int64_t legacy_offset);
 
 #endif /* !defined (__HW_SPAPR_H__) */
Signature · 1 line
2.1.0
#8 lore raw

Re: [Qemu-devel] [PATCHv2 7/8] pseries: Move rtc_offset into RTC device's state structure

Quoted 29 lines · David Gibson

The initial creation of the PAPR RTC qdev class left a wart - the rtc's offset was left in the sPAPREnvironment structure, accessed via a global.

This patch moves it into the RTC device's own state structure, were it belongs. This requires a small change to the migration stream format. In order to handle incoming streams from older versions, we also need to retain the rtc_offset field in the sPAPREnvironment structure, so that it can be loaded into via the vmsd, then pushed into the RTC device.

Since we're changing the migration format, this also takes the opportunity to change the rtc offset from a value in seconds to a value in nanoseconds, allowing nanosecond offsets between host and guest rtc time, if desired.

---
 hw/ppc/spapr.c         | 19 ++++++++++++++++++-
 hw/ppc/spapr_rtc.c     | 41 +++++++++++++++++++++++++++++++++++++----
 include/hw/ppc/spapr.h |  3 ++-
 3 files changed, 57 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +1 -0
index 9722b42..3070be0 100644
@@ -980,10 +980,27 @@ static int spapr_vga_init(PCIBus *pci_bus)
     }
 }
 
+static int spapr_post_load(void *opaque, int version_id)

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 993

Quoted 10 lines
hw/ppc/spapr.c +10 -0
+{
+    sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
+    int err = 0;
+
+    /* In earlier versions, there was no seperate qdev for the PAPR
+     * RTC, so the RTC offset was stored directly in sPAPREnvironment.
+     * So when migrating from those versions, poke the incoming offset
+     * value into the RTC device */
+    if (version_id < 3) {
+        err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);

Do you think you could do this via a qom property set rather than an explicit function call into the rtc code base instead?

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 1005

Quoted 13 lines
hw/ppc/spapr.c +7 -1
+    }
+
+    return err;
+}
+
 static const VMStateDescription vmstate_spapr = {
     .name = "spapr",
-    .version_id = 2,
+    .version_id = 3,
     .minimum_version_id = 1,
+    .post_load = spapr_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UNUSED(4), /* used to be @next_irq */

Shouldn't we make the rtc_offset field conditional on version < 3 as well here? In fact, you could do the same for the legacy next_irq. That way we don't need to move data over the wire that doesn't get used anymore.

Alex

#9 lore raw

Re: [Qemu-devel] [PATCHv2 7/8] pseries: Move rtc_offset into RTC device's state structure

Quoted 31 lines · Alexander Graf
Quoted 28 lines · David Gibson

The initial creation of the PAPR RTC qdev class left a wart - the rtc's offset was left in the sPAPREnvironment structure, accessed via a global.

This patch moves it into the RTC device's own state structure, were it belongs. This requires a small change to the migration stream format. In order to handle incoming streams from older versions, we also need to retain the rtc_offset field in the sPAPREnvironment structure, so that it can be loaded into via the vmsd, then pushed into the RTC device.

Since we're changing the migration format, this also takes the opportunity to change the rtc offset from a value in seconds to a value in nanoseconds, allowing nanosecond offsets between host and guest rtc time, if desired.

---
 hw/ppc/spapr.c         | 19 ++++++++++++++++++-
 hw/ppc/spapr_rtc.c     | 41 +++++++++++++++++++++++++++++++++++++----
 include/hw/ppc/spapr.h |  3 ++-
 3 files changed, 57 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +0 -0
index 9722b42..3070be0 100644
@@ -980,10 +980,27 @@ static int spapr_vga_init(PCIBus *pci_bus)
     }
 }
 
Quoted 14 lines

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 993

Quoted 11 lines
hw/ppc/spapr.c +11 -0
+static int spapr_post_load(void *opaque, int version_id)
+{
+    sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
+    int err = 0;
+
+    /* In earlier versions, there was no seperate qdev for the PAPR
+     * RTC, so the RTC offset was stored directly in sPAPREnvironment.
+     * So when migrating from those versions, poke the incoming offset
+     * value into the RTC device */
+    if (version_id < 3) {
+        err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);

Do you think you could do this via a qom property set rather than an explicit function call into the rtc code base instead?

Hrm. So, I could expose ns_offset as a property, but I'm not sure it's a good idea. Apart from this one instance to handle backwards compat migration, it's a purely internal value - seeting it from the command line is unlikely to have the desired effect, since it will get overwritten by spapr_rtc_realize().

Quoted 10 lines
Quoted 10 lines
hw/ppc/spapr.c +6 -1
+    }
+
+    return err;
+}
+
 static const VMStateDescription vmstate_spapr = {
     .name = "spapr",
-    .version_id = 2,
+    .version_id = 3,
Quoted 8 lines

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 1005

Quoted 4 lines
hw/ppc/spapr.c +1 -0
     .minimum_version_id = 1,
+    .post_load = spapr_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UNUSED(4), /* used to be @next_irq */

Shouldn't we make the rtc_offset field conditional on version < 3 as well here? In fact, you could do the same for the legacy next_irq. That way we don't need to move data over the wire that doesn't get used anymore.

Uh, yeah, I guess we can do that. It's just a bit awkward, because the vmstatedescription includes built in handling for "earliest version" but not "last version", so I'll need to write a custom test function.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#10 lore raw

Re: [Qemu-devel] [PATCHv2 7/8] pseries: Move rtc_offset into RTC device's state structure

Quoted 52 lines

Am 23.12.2014 um 04:56 schrieb David Gibson <[email protected]>:

Quoted 32 lines · Alexander Graf
Quoted 31 lines · David Gibson

The initial creation of the PAPR RTC qdev class left a wart - the rtc's offset was left in the sPAPREnvironment structure, accessed via a global.

This patch moves it into the RTC device's own state structure, were it belongs. This requires a small change to the migration stream format. In order to handle incoming streams from older versions, we also need to retain the rtc_offset field in the sPAPREnvironment structure, so that it can be loaded into via the vmsd, then pushed into the RTC device.

Since we're changing the migration format, this also takes the opportunity to change the rtc offset from a value in seconds to a value in nanoseconds, allowing nanosecond offsets between host and guest rtc time, if desired.

---

hw/ppc/spapr.c         | 19 ++++++++++++++++++-
hw/ppc/spapr_rtc.c     | 41 +++++++++++++++++++++++++++++++++++++----
include/hw/ppc/spapr.h |  3 ++-
3 files changed, 57 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +0 -0
index 9722b42..3070be0 100644
@@ -980,10 +980,27 @@ static int spapr_vga_init(PCIBus *pci_bus)
    }
 }
 
Quoted 14 lines

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 993

Quoted 11 lines
hw/ppc/spapr.c +11 -0
+static int spapr_post_load(void *opaque, int version_id)
+{
+    sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
+    int err = 0;
+
+    /* In earlier versions, there was no seperate qdev for the PAPR
+     * RTC, so the RTC offset was stored directly in sPAPREnvironment.
+     * So when migrating from those versions, poke the incoming offset
+     * value into the RTC device */
+    if (version_id < 3) {
+        err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);

Do you think you could do this via a qom property set rather than an explicit function call into the rtc code base instead?

Quoted 5 lines

Hrm. So, I could expose ns_offset as a property, but I'm not sure it's a good idea. Apart from this one instance to handle backwards compat migration, it's a purely internal value - seeting it from the command line is unlikely to have the desired effect, since it will get overwritten by spapr_rtc_realize().

It's not about setting it on the command line, it's a question on how we separate our interfaces. The way you handle it right now we need to make the RTAS code link with the RTC code. With QOM properties this would happen with a by-name convention both sides use.

If you can show me that the code looks cleaner with native C exported functions, I could be persuaded otherwise though.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
hw/ppc/spapr.c +6 -1
+    }
+
+    return err;
+}
+
 static const VMStateDescription vmstate_spapr = {
    .name = "spapr",
-    .version_id = 2,
+    .version_id = 3,
Quoted 8 lines

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 1005

Quoted 4 lines
hw/ppc/spapr.c +1 -0
    .minimum_version_id = 1,
+    .post_load = spapr_post_load,
    .fields = (VMStateField[]) {
        VMSTATE_UNUSED(4), /* used to be @next_irq */

Shouldn't we make the rtc_offset field conditional on version < 3 as well here? In fact, you could do the same for the legacy next_irq. That way we don't need to move data over the wire that doesn't get used anymore.

Quoted 4 lines

Uh, yeah, I guess we can do that. It's just a bit awkward, because the vmstatedescription includes built in handling for "earliest version" but not "last version", so I'll need to write a custom test function.

Yeah, though I wouldn't call it awkward ;).

Alex

#11 lore raw

Re: [Qemu-devel] [PATCHv2 7/8] pseries: Move rtc_offset into RTC device's state structure

Quoted 52 lines · Alexander Graf
Quoted 52 lines

Am 23.12.2014 um 04:56 schrieb David Gibson <[email protected]>:

Quoted 46 lines · Alexander Graf
Quoted 32 lines · David Gibson

The initial creation of the PAPR RTC qdev class left a wart - the rtc's offset was left in the sPAPREnvironment structure, accessed via a global.

This patch moves it into the RTC device's own state structure, were it belongs. This requires a small change to the migration stream format. In order to handle incoming streams from older versions, we also need to retain the rtc_offset field in the sPAPREnvironment structure, so that it can be loaded into via the vmsd, then pushed into the RTC device.

Since we're changing the migration format, this also takes the opportunity to change the rtc offset from a value in seconds to a value in nanoseconds, allowing nanosecond offsets between host and guest rtc time, if desired.

---

hw/ppc/spapr.c         | 19 ++++++++++++++++++-
hw/ppc/spapr_rtc.c     | 41 +++++++++++++++++++++++++++++++++++++----
include/hw/ppc/spapr.h |  3 ++-
3 files changed, 57 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +1 -0
index 9722b42..3070be0 100644
@@ -980,10 +980,27 @@ static int spapr_vga_init(PCIBus *pci_bus)
    }
 }
 
+static int spapr_post_load(void *opaque, int version_id)

hw/ppc/spapr.c static int spapr_vga_init(PCIBus *pci_bus) · line 993

Quoted 10 lines
hw/ppc/spapr.c +10 -0
+{
+    sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
+    int err = 0;
+
+    /* In earlier versions, there was no seperate qdev for the PAPR
+     * RTC, so the RTC offset was stored directly in sPAPREnvironment.
+     * So when migrating from those versions, poke the incoming offset
+     * value into the RTC device */
+    if (version_id < 3) {
+        err = spapr_rtc_import_offset(spapr->rtc, spapr->rtc_offset);

Do you think you could do this via a qom property set rather than an explicit function call into the rtc code base instead?

Quoted 7 lines
Quoted 5 lines

Hrm. So, I could expose ns_offset as a property, but I'm not sure it's a good idea. Apart from this one instance to handle backwards compat migration, it's a purely internal value - seeting it from the command line is unlikely to have the desired effect, since it will get overwritten by spapr_rtc_realize().

It's not about setting it on the command line,

I realise that, but as I understand it, putting the qom property there inevitably means that it *could* be set from the command line, and that doesn't sound like a good thing to me.

Quoted 5 lines

it's a question on
how we separate our interfaces. The way you handle it right now we need to make the RTAS code link with the RTC code. With QOM properties this would happen with a by-name convention both sides use.

Well, yes, and my point is that that there *should not* be a general interface to set the offset, because setting the offset never makes sense, except for this backwards compatibility shim.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#12 lore raw

PATCH v2 8/8 pseries: Export RTC time via QOM

In reply to David Gibson

On x86, the guest's RTC can be read with QMP, either from the RTC device's "date" property or via the "rtc-time" property on the machine (which is an alias to the former). This is set up in the mc146818rtc driver, and doesn't work on other targets.

This patch adds a similar "date" property to the pseries machine's RTAS RTC and adds a compatible alias to the machine.

---
 hw/ppc/spapr.c     | 3 +++
 hw/ppc/spapr_rtc.c | 7 +++++++
 2 files changed, 10 insertions(+)
hw/ppc/spapr.c +3 -0
index 3070be0..88fabc8 100644
@@ -961,6 +961,9 @@ static void spapr_rtc_create(sPAPREnvironment *spapr)
 
     qdev_init_nofail(dev);
     spapr->rtc = dev;
+
+    object_property_add_alias(qdev_get_machine(), "rtc-time",
+                              OBJECT(spapr->rtc), "date", NULL);
 }
 
 /* Returns whether we want to use VGA or not */
hw/ppc/spapr_rtc.c +7 -0
index 5ad0823..83eb7c1 100644
@@ -152,6 +152,11 @@ static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 }
 
+static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp)
+{
+    spapr_rtc_read(DEVICE(obj), current_tm, NULL);
+}
+
 static void spapr_rtc_realize(DeviceState *dev, Error **errp)
 {
     sPAPRRTCState *rtc = SPAPR_RTC(dev);
@@ -165,6 +170,8 @@ static void spapr_rtc_realize(DeviceState *dev, Error **errp)
     host_s = mktimegm(&tm);
     rtc_ns = qemu_clock_get_ns(rtc_clock);
     rtc->ns_offset = host_s * NSEC_PER_SEC - rtc_ns;
+
+    object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date, NULL);
 }
 
 static const VMStateDescription vmstate_spapr_rtc = {
Signature · 1 line
2.1.0
#13 lore raw

Re: [Qemu-devel] [PATCHv2 8/8] pseries: Export RTC time via QOM

Quoted 9 lines · David Gibson

On x86, the guest's RTC can be read with QMP, either from the RTC device's "date" property or via the "rtc-time" property on the machine (which is an alias to the former). This is set up in the mc146818rtc driver, and doesn't work on other targets.

This patch adds a similar "date" property to the pseries machine's RTAS RTC and adds a compatible alias to the machine.

Very nice, can we somehow get rid of an exported spapr_rtc_read() with this as well?

Alex

#14 lore raw

Re: [Qemu-devel] [PATCHv2 8/8] pseries: Export RTC time via QOM

Quoted 8 lines · Alexander Graf
Quoted 5 lines · David Gibson

On x86, the guest's RTC can be read with QMP, either from the RTC device's "date" property or via the "rtc-time" property on the machine (which is an alias to the former). This is set up in the mc146818rtc driver, and doesn't work on other targets.

Quoted 7 lines
Quoted 4 lines

This patch adds a similar "date" property to the pseries machine's RTAS RTC and adds a compatible alias to the machine.

Very nice, can we somehow get rid of an exported spapr_rtc_read() with this as well?

Uhhh.. we could, but I really don't like the idea.

It seems perverse to encode the current time into JSON, just so we can decode it into a struct tm again in the events code.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#15 lore raw

Re: [Qemu-devel] [PATCHv2 8/8] pseries: Export RTC time via QOM

Quoted 19 lines

Am 23.12.2014 um 04:14 schrieb David Gibson <[email protected]>:

Quoted 14 lines · Alexander Graf
Quoted 12 lines · David Gibson

On x86, the guest's RTC can be read with QMP, either from the RTC device's "date" property or via the "rtc-time" property on the machine (which is an alias to the former). This is set up in the mc146818rtc driver, and doesn't work on other targets.

This patch adds a similar "date" property to the pseries machine's RTAS RTC and adds a compatible alias to the machine.

Quoted 7 lines
Quoted 2 lines

Very nice, can we somehow get rid of an exported spapr_rtc_read() with this as well?

Uhhh.. we could, but I really don't like the idea.

It seems perverse to encode the current time into JSON, just so we can decode it into a struct tm again in the events code.

I don't understand - what does JSON have to do with it?

Alex

#16 lore raw

Re: [Qemu-devel] [PATCHv2 8/8] pseries: Export RTC time via QOM

Quoted 22 lines · Alexander Graf
Quoted 22 lines

Am 23.12.2014 um 04:14 schrieb David Gibson <[email protected]>:

Quoted 16 lines · Alexander Graf
Quoted 8 lines · David Gibson

On x86, the guest's RTC can be read with QMP, either from the RTC device's "date" property or via the "rtc-time" property on the machine (which is an alias to the former). This is set up in the mc146818rtc driver, and doesn't work on other targets.

Quoted 4 lines

This patch adds a similar "date" property to the pseries machine's RTAS RTC and adds a compatible alias to the machine.

Very nice, can we somehow get rid of an exported spapr_rtc_read() with this as well?

Quoted 6 lines
Quoted 4 lines

Uhhh.. we could, but I really don't like the idea.

It seems perverse to encode the current time into JSON, just so we can decode it into a struct tm again in the events code.

I don't understand - what does JSON have to do with it?

My misunderstanding, it doesn't actually deal with JSON here. But if I'm following how this would work, it would be a way of getting a struct tm from point A to point B, by:
1. building a temporary visitor object (object_property_get_qobject)
2. finding and calling the general property getter callback (object_property_get)
3. Making multiple callbacks into the visitor with pieces of point A's struct tm (property_get_tm)
4. In those visitor callbacks building an associative array (qmp_output_start_struct), then filling it with object wrappers around integers (qmp_output_type_int)
5. Digging through the nested qobject thus constructed to extract the various ints and put them into point B's struct tm (hypothetical object_property_get_tm)
6. Cleaning up the various temporaries created during the process

Seriously, w. t. f.!?

Maybe there's a simpler way of doing this via the object property mechanism, but it's sure as hell not obvious to me.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#17 lore raw
In reply to David Gibson
Quoted 8 lines · David Gibson

At the moment, the PAPR RTC implementation (actually a paravirt firmware interface, rather than a normal device) works directly off host time, and so doesn't respect the options such as clock=vm which can be specified in the -rtc command line option.

Extending the PAPR RTC in this way makes it clearer it should probably be its own separate qdev, so this series also implements that.

Quoted 7 lines

Finally it moves the code from mc146818rtc for publishing the RTC time via QOM into generic QOM helpers, and extends the PAPR RTC driver to export in a similar way.

I've done some basic sanity testing on this, but not much. In particular I haven't tested the migration code much yet, so that might need revision still.

I think we're on the right track. After this patch set, I would like to see no exported functions related to RTC in include/hw/ppc/spapr.h left though :). I'm fairly sure you can get all of them handled via qom properties instead.

Alex