xref: /qemu/hw/ppc/spapr_rtc.c (revision f01c5d84775cfc877dc0b537565a168d043394e9)
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 NSEC_PER_SEC    1000000000LL
34 
35 void spapr_rtc_read(sPAPREnvironment *spapr, struct tm *tm, uint32_t *ns)
36 {
37     int64_t host_ns = qemu_clock_get_ns(rtc_clock);
38     time_t guest_s;
39 
40     guest_s = host_ns / NSEC_PER_SEC + spapr->rtc_offset;
41 
42     if (tm) {
43         gmtime_r(&guest_s, tm);
44     }
45     if (ns) {
46         *ns = host_ns % NSEC_PER_SEC;
47     }
48 }
49 
50 static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
51                                  uint32_t token, uint32_t nargs,
52                                  target_ulong args,
53                                  uint32_t nret, target_ulong rets)
54 {
55     struct tm tm;
56     uint32_t ns;
57 
58     if ((nargs != 0) || (nret != 8)) {
59         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
60         return;
61     }
62 
63     spapr_rtc_read(spapr, &tm, &ns);
64 
65     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
66     rtas_st(rets, 1, tm.tm_year + 1900);
67     rtas_st(rets, 2, tm.tm_mon + 1);
68     rtas_st(rets, 3, tm.tm_mday);
69     rtas_st(rets, 4, tm.tm_hour);
70     rtas_st(rets, 5, tm.tm_min);
71     rtas_st(rets, 6, tm.tm_sec);
72     rtas_st(rets, 7, ns);
73 }
74 
75 static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
76                                  uint32_t token, uint32_t nargs,
77                                  target_ulong args,
78                                  uint32_t nret, target_ulong rets)
79 {
80     struct tm tm;
81     time_t new_s;
82     int64_t host_ns;
83 
84     if ((nargs != 7) || (nret != 1)) {
85         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
86         return;
87     }
88 
89     tm.tm_year = rtas_ld(args, 0) - 1900;
90     tm.tm_mon = rtas_ld(args, 1) - 1;
91     tm.tm_mday = rtas_ld(args, 2);
92     tm.tm_hour = rtas_ld(args, 3);
93     tm.tm_min = rtas_ld(args, 4);
94     tm.tm_sec = rtas_ld(args, 5);
95 
96     new_s = mktimegm(&tm);
97     if (new_s == -1) {
98         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
99         return;
100     }
101 
102     /* Generate a monitor event for the change */
103     qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
104 
105     host_ns = qemu_clock_get_ns(rtc_clock);
106 
107     spapr->rtc_offset = new_s - host_ns / NSEC_PER_SEC;
108 
109     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
110 }
111 
112 void spapr_rtc_init(void)
113 {
114     struct tm tm;
115     time_t host_s;
116     int64_t rtc_ns;
117 
118     /* Initialize the RTAS RTC from host time */
119 
120     qemu_get_timedate(&tm, 0);
121     host_s = mktimegm(&tm);
122     rtc_ns = qemu_clock_get_ns(rtc_clock);
123     spapr->rtc_offset = host_s - rtc_ns / NSEC_PER_SEC;
124 
125     spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
126                         rtas_get_time_of_day);
127     spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
128                         rtas_set_time_of_day);
129 }
130