1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
36void spapr_rtc_read(sPAPRRTCState *rtc, struct tm *tm, uint32_t *ns)
37{
38 int64_t host_ns = qemu_clock_get_ns(rtc_clock);
39 int64_t guest_ns;
40 time_t guest_s;
41
42 assert(rtc);
43
44 guest_ns = host_ns + rtc->ns_offset;
45 guest_s = guest_ns / NANOSECONDS_PER_SECOND;
46
47 if (tm) {
48 gmtime_r(&guest_s, tm);
49 }
50 if (ns) {
51 *ns = guest_ns;
52 }
53}
54
55int spapr_rtc_import_offset(sPAPRRTCState *rtc, int64_t legacy_offset)
56{
57 if (!rtc) {
58 return -ENODEV;
59 }
60
61 rtc->ns_offset = legacy_offset * NANOSECONDS_PER_SECOND;
62
63 return 0;
64}
65
66static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPRMachineState *spapr,
67 uint32_t token, uint32_t nargs,
68 target_ulong args,
69 uint32_t nret, target_ulong rets)
70{
71 struct tm tm;
72 uint32_t ns;
73
74 if ((nargs != 0) || (nret != 8)) {
75 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
76 return;
77 }
78
79 spapr_rtc_read(&spapr->rtc, &tm, &ns);
80
81 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
82 rtas_st(rets, 1, tm.tm_year + 1900);
83 rtas_st(rets, 2, tm.tm_mon + 1);
84 rtas_st(rets, 3, tm.tm_mday);
85 rtas_st(rets, 4, tm.tm_hour);
86 rtas_st(rets, 5, tm.tm_min);
87 rtas_st(rets, 6, tm.tm_sec);
88 rtas_st(rets, 7, ns);
89}
90
91static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPRMachineState *spapr,
92 uint32_t token, uint32_t nargs,
93 target_ulong args,
94 uint32_t nret, target_ulong rets)
95{
96 sPAPRRTCState *rtc = &spapr->rtc;
97 struct tm tm;
98 time_t new_s;
99 int64_t host_ns;
100
101 if ((nargs != 7) || (nret != 1)) {
102 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
103 return;
104 }
105
106 tm.tm_year = rtas_ld(args, 0) - 1900;
107 tm.tm_mon = rtas_ld(args, 1) - 1;
108 tm.tm_mday = rtas_ld(args, 2);
109 tm.tm_hour = rtas_ld(args, 3);
110 tm.tm_min = rtas_ld(args, 4);
111 tm.tm_sec = rtas_ld(args, 5);
112
113 new_s = mktimegm(&tm);
114 if (new_s == -1) {
115 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
116 return;
117 }
118
119
120 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
121
122 host_ns = qemu_clock_get_ns(rtc_clock);
123
124 rtc->ns_offset = (new_s * NANOSECONDS_PER_SECOND) - host_ns;
125
126 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
127}
128
129static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp)
130{
131 spapr_rtc_read(SPAPR_RTC(obj), current_tm, NULL);
132}
133
134static void spapr_rtc_realize(DeviceState *dev, Error **errp)
135{
136 sPAPRRTCState *rtc = SPAPR_RTC(dev);
137 struct tm tm;
138 time_t host_s;
139 int64_t rtc_ns;
140
141
142
143 qemu_get_timedate(&tm, 0);
144 host_s = mktimegm(&tm);
145 rtc_ns = qemu_clock_get_ns(rtc_clock);
146 rtc->ns_offset = host_s * NANOSECONDS_PER_SECOND - rtc_ns;
147
148 object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date, NULL);
149}
150
151static const VMStateDescription vmstate_spapr_rtc = {
152 .name = "spapr/rtc",
153 .version_id = 1,
154 .minimum_version_id = 1,
155 .fields = (VMStateField[]) {
156 VMSTATE_INT64(ns_offset, sPAPRRTCState),
157 VMSTATE_END_OF_LIST()
158 },
159};
160
161static void spapr_rtc_class_init(ObjectClass *oc, void *data)
162{
163 DeviceClass *dc = DEVICE_CLASS(oc);
164
165 dc->realize = spapr_rtc_realize;
166 dc->vmsd = &vmstate_spapr_rtc;
167
168 dc->user_creatable = false;
169
170 spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
171 rtas_get_time_of_day);
172 spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
173 rtas_set_time_of_day);
174}
175
176static const TypeInfo spapr_rtc_info = {
177 .name = TYPE_SPAPR_RTC,
178 .parent = TYPE_DEVICE,
179 .instance_size = sizeof(sPAPRRTCState),
180 .class_init = spapr_rtc_class_init,
181};
182
183static void spapr_rtc_register_types(void)
184{
185 type_register_static(&spapr_rtc_info);
186}
187type_init(spapr_rtc_register_types)
188