qemu/hw/ppc/spapr_rtc.c
<<
>>
Prefs
   1/*
   2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
   3 *
   4 * RTAS Real Time Clock
   5 *
   6 * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
   7 * Copyright 2014 David Gibson, Red Hat.
   8 *
   9 * Permission is hereby granted, free of charge, to any person obtaining a copy
  10 * of this software and associated documentation files (the "Software"), to deal
  11 * in the Software without restriction, including without limitation the rights
  12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13 * copies of the Software, and to permit persons to whom the Software is
  14 * furnished to do so, subject to the following conditions:
  15 *
  16 * The above copyright notice and this permission notice shall be included in
  17 * all copies or substantial portions of the Software.
  18 *
  19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25 * THE SOFTWARE.
  26 *
  27 */
  28#include "qemu/osdep.h"
  29#include "cpu.h"
  30#include "qemu/timer.h"
  31#include "sysemu/sysemu.h"
  32#include "hw/ppc/spapr.h"
  33#include "qapi-event.h"
  34#include "qemu/cutils.h"
  35
  36#define SPAPR_RTC(obj) \
  37    OBJECT_CHECK(sPAPRRTCState, (obj), TYPE_SPAPR_RTC)
  38
  39typedef struct sPAPRRTCState sPAPRRTCState;
  40struct sPAPRRTCState {
  41    /*< private >*/
  42    SysBusDevice parent_obj;
  43    int64_t ns_offset;
  44};
  45
  46void spapr_rtc_read(DeviceState *dev, struct tm *tm, uint32_t *ns)
  47{
  48    sPAPRRTCState *rtc = SPAPR_RTC(dev);
  49    int64_t host_ns = qemu_clock_get_ns(rtc_clock);
  50    int64_t guest_ns;
  51    time_t guest_s;
  52
  53    assert(rtc);
  54
  55    guest_ns = host_ns + rtc->ns_offset;
  56    guest_s = guest_ns / NANOSECONDS_PER_SECOND;
  57
  58    if (tm) {
  59        gmtime_r(&guest_s, tm);
  60    }
  61    if (ns) {
  62        *ns = guest_ns;
  63    }
  64}
  65
  66int spapr_rtc_import_offset(DeviceState *dev, int64_t legacy_offset)
  67{
  68    sPAPRRTCState *rtc;
  69
  70    if (!dev) {
  71        return -ENODEV;
  72    }
  73
  74    rtc = SPAPR_RTC(dev);
  75
  76    rtc->ns_offset = legacy_offset * NANOSECONDS_PER_SECOND;
  77
  78    return 0;
  79}
  80
  81static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPRMachineState *spapr,
  82                                 uint32_t token, uint32_t nargs,
  83                                 target_ulong args,
  84                                 uint32_t nret, target_ulong rets)
  85{
  86    struct tm tm;
  87    uint32_t ns;
  88
  89    if ((nargs != 0) || (nret != 8)) {
  90        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  91        return;
  92    }
  93
  94    if (!spapr->rtc) {
  95        rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  96        return;
  97    }
  98
  99    spapr_rtc_read(spapr->rtc, &tm, &ns);
 100
 101    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 102    rtas_st(rets, 1, tm.tm_year + 1900);
 103    rtas_st(rets, 2, tm.tm_mon + 1);
 104    rtas_st(rets, 3, tm.tm_mday);
 105    rtas_st(rets, 4, tm.tm_hour);
 106    rtas_st(rets, 5, tm.tm_min);
 107    rtas_st(rets, 6, tm.tm_sec);
 108    rtas_st(rets, 7, ns);
 109}
 110
 111static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 112                                 uint32_t token, uint32_t nargs,
 113                                 target_ulong args,
 114                                 uint32_t nret, target_ulong rets)
 115{
 116    sPAPRRTCState *rtc;
 117    struct tm tm;
 118    time_t new_s;
 119    int64_t host_ns;
 120
 121    if ((nargs != 7) || (nret != 1)) {
 122        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 123        return;
 124    }
 125
 126    if (!spapr->rtc) {
 127        rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
 128        return;
 129    }
 130
 131    tm.tm_year = rtas_ld(args, 0) - 1900;
 132    tm.tm_mon = rtas_ld(args, 1) - 1;
 133    tm.tm_mday = rtas_ld(args, 2);
 134    tm.tm_hour = rtas_ld(args, 3);
 135    tm.tm_min = rtas_ld(args, 4);
 136    tm.tm_sec = rtas_ld(args, 5);
 137
 138    new_s = mktimegm(&tm);
 139    if (new_s == -1) {
 140        rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
 141        return;
 142    }
 143
 144    /* Generate a monitor event for the change */
 145    qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
 146
 147    rtc = SPAPR_RTC(spapr->rtc);
 148
 149    host_ns = qemu_clock_get_ns(rtc_clock);
 150
 151    rtc->ns_offset = (new_s * NANOSECONDS_PER_SECOND) - host_ns;
 152
 153    rtas_st(rets, 0, RTAS_OUT_SUCCESS);
 154}
 155
 156static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp)
 157{
 158    spapr_rtc_read(DEVICE(obj), current_tm, NULL);
 159}
 160
 161static void spapr_rtc_realize(DeviceState *dev, Error **errp)
 162{
 163    sPAPRRTCState *rtc = SPAPR_RTC(dev);
 164    struct tm tm;
 165    time_t host_s;
 166    int64_t rtc_ns;
 167
 168    /* Initialize the RTAS RTC from host time */
 169
 170    qemu_get_timedate(&tm, 0);
 171    host_s = mktimegm(&tm);
 172    rtc_ns = qemu_clock_get_ns(rtc_clock);
 173    rtc->ns_offset = host_s * NANOSECONDS_PER_SECOND - rtc_ns;
 174
 175    object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date, NULL);
 176}
 177
 178static const VMStateDescription vmstate_spapr_rtc = {
 179    .name = "spapr/rtc",
 180    .version_id = 1,
 181    .minimum_version_id = 1,
 182    .fields = (VMStateField[]) {
 183        VMSTATE_INT64(ns_offset, sPAPRRTCState),
 184        VMSTATE_END_OF_LIST()
 185    },
 186};
 187
 188static void spapr_rtc_class_init(ObjectClass *oc, void *data)
 189{
 190    DeviceClass *dc = DEVICE_CLASS(oc);
 191
 192    dc->realize = spapr_rtc_realize;
 193    dc->vmsd = &vmstate_spapr_rtc;
 194
 195    spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
 196                        rtas_get_time_of_day);
 197    spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
 198                        rtas_set_time_of_day);
 199}
 200
 201static const TypeInfo spapr_rtc_info = {
 202    .name          = TYPE_SPAPR_RTC,
 203    .parent        = TYPE_SYS_BUS_DEVICE,
 204    .instance_size = sizeof(sPAPRRTCState),
 205    .class_init    = spapr_rtc_class_init,
 206};
 207
 208static void spapr_rtc_register_types(void)
 209{
 210    type_register_static(&spapr_rtc_info);
 211}
 212type_init(spapr_rtc_register_types)
 213