nbd: always query export list in fixed new style protocol
[qemu/ar7.git] / hw / ppc / spapr_rtc.c
blob02fa373aeeb290a5768dcfb756dea73e23ad534f
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * RTAS Real Time Clock
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
25 * THE SOFTWARE.
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"
35 #define SPAPR_RTC(obj) \
36 OBJECT_CHECK(sPAPRRTCState, (obj), TYPE_SPAPR_RTC)
38 typedef struct sPAPRRTCState sPAPRRTCState;
39 struct sPAPRRTCState {
40 /*< private >*/
41 SysBusDevice parent_obj;
42 int64_t ns_offset;
45 void spapr_rtc_read(DeviceState *dev, struct tm *tm, uint32_t *ns)
47 sPAPRRTCState *rtc = SPAPR_RTC(dev);
48 int64_t host_ns = qemu_clock_get_ns(rtc_clock);
49 int64_t guest_ns;
50 time_t guest_s;
52 assert(rtc);
54 guest_ns = host_ns + rtc->ns_offset;
55 guest_s = guest_ns / NANOSECONDS_PER_SECOND;
57 if (tm) {
58 gmtime_r(&guest_s, tm);
60 if (ns) {
61 *ns = guest_ns;
65 int spapr_rtc_import_offset(DeviceState *dev, int64_t legacy_offset)
67 sPAPRRTCState *rtc;
69 if (!dev) {
70 return -ENODEV;
73 rtc = SPAPR_RTC(dev);
75 rtc->ns_offset = legacy_offset * NANOSECONDS_PER_SECOND;
77 return 0;
80 static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPRMachineState *spapr,
81 uint32_t token, uint32_t nargs,
82 target_ulong args,
83 uint32_t nret, target_ulong rets)
85 struct tm tm;
86 uint32_t ns;
88 if ((nargs != 0) || (nret != 8)) {
89 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
90 return;
93 if (!spapr->rtc) {
94 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
95 return;
98 spapr_rtc_read(spapr->rtc, &tm, &ns);
100 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
101 rtas_st(rets, 1, tm.tm_year + 1900);
102 rtas_st(rets, 2, tm.tm_mon + 1);
103 rtas_st(rets, 3, tm.tm_mday);
104 rtas_st(rets, 4, tm.tm_hour);
105 rtas_st(rets, 5, tm.tm_min);
106 rtas_st(rets, 6, tm.tm_sec);
107 rtas_st(rets, 7, ns);
110 static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPRMachineState *spapr,
111 uint32_t token, uint32_t nargs,
112 target_ulong args,
113 uint32_t nret, target_ulong rets)
115 sPAPRRTCState *rtc;
116 struct tm tm;
117 time_t new_s;
118 int64_t host_ns;
120 if ((nargs != 7) || (nret != 1)) {
121 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
122 return;
125 if (!spapr->rtc) {
126 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
127 return;
130 tm.tm_year = rtas_ld(args, 0) - 1900;
131 tm.tm_mon = rtas_ld(args, 1) - 1;
132 tm.tm_mday = rtas_ld(args, 2);
133 tm.tm_hour = rtas_ld(args, 3);
134 tm.tm_min = rtas_ld(args, 4);
135 tm.tm_sec = rtas_ld(args, 5);
137 new_s = mktimegm(&tm);
138 if (new_s == -1) {
139 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
140 return;
143 /* Generate a monitor event for the change */
144 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
146 rtc = SPAPR_RTC(spapr->rtc);
148 host_ns = qemu_clock_get_ns(rtc_clock);
150 rtc->ns_offset = (new_s * NANOSECONDS_PER_SECOND) - host_ns;
152 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
155 static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp)
157 spapr_rtc_read(DEVICE(obj), current_tm, NULL);
160 static void spapr_rtc_realize(DeviceState *dev, Error **errp)
162 sPAPRRTCState *rtc = SPAPR_RTC(dev);
163 struct tm tm;
164 time_t host_s;
165 int64_t rtc_ns;
167 /* Initialize the RTAS RTC from host time */
169 qemu_get_timedate(&tm, 0);
170 host_s = mktimegm(&tm);
171 rtc_ns = qemu_clock_get_ns(rtc_clock);
172 rtc->ns_offset = host_s * NANOSECONDS_PER_SECOND - rtc_ns;
174 object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date, NULL);
177 static const VMStateDescription vmstate_spapr_rtc = {
178 .name = "spapr/rtc",
179 .version_id = 1,
180 .minimum_version_id = 1,
181 .fields = (VMStateField[]) {
182 VMSTATE_INT64(ns_offset, sPAPRRTCState),
183 VMSTATE_END_OF_LIST()
187 static void spapr_rtc_class_init(ObjectClass *oc, void *data)
189 DeviceClass *dc = DEVICE_CLASS(oc);
191 dc->realize = spapr_rtc_realize;
192 dc->vmsd = &vmstate_spapr_rtc;
194 spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
195 rtas_get_time_of_day);
196 spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
197 rtas_set_time_of_day);
200 static const TypeInfo spapr_rtc_info = {
201 .name = TYPE_SPAPR_RTC,
202 .parent = TYPE_SYS_BUS_DEVICE,
203 .instance_size = sizeof(sPAPRRTCState),
204 .class_init = spapr_rtc_class_init,
207 static void spapr_rtc_register_types(void)
209 type_register_static(&spapr_rtc_info);
211 type_init(spapr_rtc_register_types)