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