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