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 "cpu.h" 29 #include "sysemu/sysemu.h" 30 #include "hw/ppc/spapr.h" 31 #include "qapi-event.h" 32 33 #define SPAPR_RTC(obj) \ 34 OBJECT_CHECK(sPAPRRTCState, (obj), TYPE_SPAPR_RTC) 35 36 typedef struct sPAPRRTCState sPAPRRTCState; 37 struct sPAPRRTCState { 38 /*< private >*/ 39 SysBusDevice parent_obj; 40 int64_t ns_offset; 41 }; 42 43 #define NSEC_PER_SEC 1000000000LL 44 45 void spapr_rtc_read(DeviceState *dev, struct tm *tm, uint32_t *ns) 46 { 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; 51 52 assert(rtc); 53 54 guest_ns = host_ns + rtc->ns_offset; 55 guest_s = guest_ns / NSEC_PER_SEC; 56 57 if (tm) { 58 gmtime_r(&guest_s, tm); 59 } 60 if (ns) { 61 *ns = guest_ns; 62 } 63 } 64 65 int spapr_rtc_import_offset(DeviceState *dev, int64_t legacy_offset) 66 { 67 sPAPRRTCState *rtc; 68 69 if (!dev) { 70 return -ENODEV; 71 } 72 73 rtc = SPAPR_RTC(dev); 74 75 rtc->ns_offset = legacy_offset * NSEC_PER_SEC; 76 77 return 0; 78 } 79 80 static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr, 81 uint32_t token, uint32_t nargs, 82 target_ulong args, 83 uint32_t nret, target_ulong rets) 84 { 85 struct tm tm; 86 uint32_t ns; 87 88 if ((nargs != 0) || (nret != 8)) { 89 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 90 return; 91 } 92 93 if (!spapr->rtc) { 94 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 95 return; 96 } 97 98 spapr_rtc_read(spapr->rtc, &tm, &ns); 99 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); 108 } 109 110 static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr, 111 uint32_t token, uint32_t nargs, 112 target_ulong args, 113 uint32_t nret, target_ulong rets) 114 { 115 sPAPRRTCState *rtc; 116 struct tm tm; 117 time_t new_s; 118 int64_t host_ns; 119 120 if ((nargs != 7) || (nret != 1)) { 121 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 122 return; 123 } 124 125 if (!spapr->rtc) { 126 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 127 return; 128 } 129 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); 136 137 new_s = mktimegm(&tm); 138 if (new_s == -1) { 139 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 140 return; 141 } 142 143 /* Generate a monitor event for the change */ 144 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort); 145 146 rtc = SPAPR_RTC(spapr->rtc); 147 148 host_ns = qemu_clock_get_ns(rtc_clock); 149 150 rtc->ns_offset = (new_s * NSEC_PER_SEC) - host_ns; 151 152 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 153 } 154 155 static void spapr_rtc_qom_date(Object *obj, struct tm *current_tm, Error **errp) 156 { 157 spapr_rtc_read(DEVICE(obj), current_tm, NULL); 158 } 159 160 static void spapr_rtc_realize(DeviceState *dev, Error **errp) 161 { 162 sPAPRRTCState *rtc = SPAPR_RTC(dev); 163 struct tm tm; 164 time_t host_s; 165 int64_t rtc_ns; 166 167 /* Initialize the RTAS RTC from host time */ 168 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 * NSEC_PER_SEC - rtc_ns; 173 174 object_property_add_tm(OBJECT(rtc), "date", spapr_rtc_qom_date, NULL); 175 } 176 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() 184 }, 185 }; 186 187 static void spapr_rtc_class_init(ObjectClass *oc, void *data) 188 { 189 DeviceClass *dc = DEVICE_CLASS(oc); 190 191 dc->realize = spapr_rtc_realize; 192 dc->vmsd = &vmstate_spapr_rtc; 193 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); 198 } 199 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_size = sizeof(XICSStateClass), 205 .class_init = spapr_rtc_class_init, 206 }; 207 208 static void spapr_rtc_register_types(void) 209 { 210 type_register_static(&spapr_rtc_info); 211 } 212 type_init(spapr_rtc_register_types) 213