2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
6 * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
7 * Copyright 2014 David Gibson, Red Hat.
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #include "qemu/osdep.h"
29 #include "qemu-common.h"
30 #include "qemu/timer.h"
31 #include "sysemu/sysemu.h"
32 #include "hw/ppc/spapr.h"
33 #include "migration/vmstate.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-events-misc-target.h"
36 #include "qemu/cutils.h"
37 #include "qemu/module.h"
39 void spapr_rtc_read(SpaprRtcState
*rtc
, struct tm
*tm
, uint32_t *ns
)
41 int64_t host_ns
= qemu_clock_get_ns(rtc_clock
);
47 guest_ns
= host_ns
+ rtc
->ns_offset
;
48 guest_s
= guest_ns
/ NANOSECONDS_PER_SECOND
;
51 gmtime_r(&guest_s
, tm
);
58 int spapr_rtc_import_offset(SpaprRtcState
*rtc
, int64_t legacy_offset
)
64 rtc
->ns_offset
= legacy_offset
* NANOSECONDS_PER_SECOND
;
69 static void rtas_get_time_of_day(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
70 uint32_t token
, uint32_t nargs
,
72 uint32_t nret
, target_ulong rets
)
77 if ((nargs
!= 0) || (nret
!= 8)) {
78 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
82 spapr_rtc_read(&spapr
->rtc
, &tm
, &ns
);
84 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
85 rtas_st(rets
, 1, tm
.tm_year
+ 1900);
86 rtas_st(rets
, 2, tm
.tm_mon
+ 1);
87 rtas_st(rets
, 3, tm
.tm_mday
);
88 rtas_st(rets
, 4, tm
.tm_hour
);
89 rtas_st(rets
, 5, tm
.tm_min
);
90 rtas_st(rets
, 6, tm
.tm_sec
);
94 static void rtas_set_time_of_day(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
95 uint32_t token
, uint32_t nargs
,
97 uint32_t nret
, target_ulong rets
)
99 SpaprRtcState
*rtc
= &spapr
->rtc
;
104 if ((nargs
!= 7) || (nret
!= 1)) {
105 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
109 tm
.tm_year
= rtas_ld(args
, 0) - 1900;
110 tm
.tm_mon
= rtas_ld(args
, 1) - 1;
111 tm
.tm_mday
= rtas_ld(args
, 2);
112 tm
.tm_hour
= rtas_ld(args
, 3);
113 tm
.tm_min
= rtas_ld(args
, 4);
114 tm
.tm_sec
= rtas_ld(args
, 5);
116 new_s
= mktimegm(&tm
);
118 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
122 /* Generate a monitor event for the change */
123 qapi_event_send_rtc_change(qemu_timedate_diff(&tm
));
125 host_ns
= qemu_clock_get_ns(rtc_clock
);
127 rtc
->ns_offset
= (new_s
* NANOSECONDS_PER_SECOND
) - host_ns
;
129 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
132 static void spapr_rtc_qom_date(Object
*obj
, struct tm
*current_tm
, Error
**errp
)
134 spapr_rtc_read(SPAPR_RTC(obj
), current_tm
, NULL
);
137 static void spapr_rtc_realize(DeviceState
*dev
, Error
**errp
)
139 SpaprRtcState
*rtc
= SPAPR_RTC(dev
);
144 /* Initialize the RTAS RTC from host time */
146 qemu_get_timedate(&tm
, 0);
147 host_s
= mktimegm(&tm
);
148 rtc_ns
= qemu_clock_get_ns(rtc_clock
);
149 rtc
->ns_offset
= host_s
* NANOSECONDS_PER_SECOND
- rtc_ns
;
151 object_property_add_tm(OBJECT(rtc
), "date", spapr_rtc_qom_date
);
154 static const VMStateDescription vmstate_spapr_rtc
= {
157 .minimum_version_id
= 1,
158 .fields
= (VMStateField
[]) {
159 VMSTATE_INT64(ns_offset
, SpaprRtcState
),
160 VMSTATE_END_OF_LIST()
164 static void spapr_rtc_class_init(ObjectClass
*oc
, void *data
)
166 DeviceClass
*dc
= DEVICE_CLASS(oc
);
168 dc
->realize
= spapr_rtc_realize
;
169 dc
->vmsd
= &vmstate_spapr_rtc
;
170 /* Reason: This is an internal device only for handling the hypercalls */
171 dc
->user_creatable
= false;
173 spapr_rtas_register(RTAS_GET_TIME_OF_DAY
, "get-time-of-day",
174 rtas_get_time_of_day
);
175 spapr_rtas_register(RTAS_SET_TIME_OF_DAY
, "set-time-of-day",
176 rtas_set_time_of_day
);
179 static const TypeInfo spapr_rtc_info
= {
180 .name
= TYPE_SPAPR_RTC
,
181 .parent
= TYPE_DEVICE
,
182 .instance_size
= sizeof(SpaprRtcState
),
183 .class_init
= spapr_rtc_class_init
,
186 static void spapr_rtc_register_types(void)
188 type_register_static(&spapr_rtc_info
);
190 type_init(spapr_rtc_register_types
)