master
c 280 lines 8.54 KB
Raw
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "target/ppc/cpu.h"
19 #include "migration/vmstate.h"
20 #include "trace.h"
21
22 #include "hw/ppc/spapr.h"
23
24 #define FIELD_BE(reg, field, start, len) \
25 FIELD(reg, field, 64 - (start + len), len)
26
27 /*
28 * Bits 47: "leaveOtherWatchdogsRunningOnTimeout", specified on
29 * the "Start watchdog" operation,
30 * 0 - stop out-standing watchdogs on timeout,
31 * 1 - leave outstanding watchdogs running on timeout
32 */
33 FIELD_BE(PSERIES_WDTF, LEAVE_OTHER, 47, 1)
34
35 /* Bits 48-55: "operation" */
36 FIELD_BE(PSERIES_WDTF, OP, 48, 8)
37 #define PSERIES_WDTF_OP_START 0x1
38 #define PSERIES_WDTF_OP_STOP 0x2
39 #define PSERIES_WDTF_OP_QUERY 0x3
40 #define PSERIES_WDTF_OP_QUERY_LPM 0x4
41
42 /* Bits 56-63: "timeoutAction" */
43 FIELD_BE(PSERIES_WDTF, ACTION, 56, 8)
44 #define PSERIES_WDTF_ACTION_HARD_POWER_OFF 0x1
45 #define PSERIES_WDTF_ACTION_HARD_RESTART 0x2
46 #define PSERIES_WDTF_ACTION_DUMP_RESTART 0x3
47
48 FIELD_BE(PSERIES_WDTF, RESERVED, 0, 47)
49
50 /* Special watchdogNumber for the "stop all watchdogs" operation */
51 #define PSERIES_WDT_STOP_ALL ((uint64_t)~0)
52
53 /*
54 * For the "Query watchdog capabilities" operation, a uint64 structure
55 * defined as:
56 * Bits 0-15: The minimum supported timeout in milliseconds
57 * Bits 16-31: The number of watchdogs supported
58 * Bits 32-63: Reserved
59 */
60 FIELD_BE(PSERIES_WDTQ, MIN_TIMEOUT, 0, 16)
61 FIELD_BE(PSERIES_WDTQ, NUM, 16, 16)
62
63 /*
64 * For the "Query watchdog LPM requirement" operation:
65 * 1 = The given "watchdogNumber" must be stopped prior to suspending
66 * 2 = The given "watchdogNumber" does not have to be stopped prior to
67 * suspending
68 */
69 #define PSERIES_WDTQL_STOPPED 1
70 #define PSERIES_WDTQL_QUERY_NOT_STOPPED 2
71
72 #define WDT_MIN_TIMEOUT 1 /* 1ms */
73
74 static target_ulong watchdog_stop(unsigned watchdogNumber, SpaprWatchdog *w)
75 {
76 target_ulong ret = H_NOOP;
77
78 if (timer_pending(&w->timer)) {
79 timer_del(&w->timer);
80 ret = H_SUCCESS;
81 }
82 trace_spapr_watchdog_stop(watchdogNumber, ret);
83
84 return ret;
85 }
86
87 static target_ulong watchdog_stop_all(SpaprMachineState *spapr)
88 {
89 target_ulong ret = H_NOOP;
90 int i;
91
92 for (i = 1; i <= ARRAY_SIZE(spapr->wds); ++i) {
93 target_ulong r = watchdog_stop(i, &spapr->wds[i - 1]);
94
95 if (r != H_NOOP && r != H_SUCCESS) {
96 ret = r;
97 }
98 }
99
100 return ret;
101 }
102
103 static void watchdog_expired(void *pw)
104 {
105 SpaprWatchdog *w = pw;
106 CPUState *cs;
107 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
108 unsigned num = w - spapr->wds;
109
110 g_assert(num < ARRAY_SIZE(spapr->wds));
111 trace_spapr_watchdog_expired(num, w->action);
112 switch (w->action) {
113 case PSERIES_WDTF_ACTION_HARD_POWER_OFF:
114 qemu_system_vmstop_request(RUN_STATE_SHUTDOWN);
115 break;
116 case PSERIES_WDTF_ACTION_HARD_RESTART:
117 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
118 break;
119 case PSERIES_WDTF_ACTION_DUMP_RESTART:
120 CPU_FOREACH(cs) {
121 async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
122 }
123 break;
124 }
125 if (!w->leave_others) {
126 watchdog_stop_all(spapr);
127 }
128 }
129
130 static inline bool watchdog_number_valid(target_ulong watchdogNumber,
131 SpaprMachineState *spapr)
132 {
133 return watchdogNumber >= 1 && watchdogNumber <= ARRAY_SIZE(spapr->wds);
134 }
135
136 static target_ulong h_watchdog(PowerPCCPU *cpu,
137 SpaprMachineState *spapr,
138 target_ulong opcode, target_ulong *args)
139 {
140 target_ulong ret = H_SUCCESS;
141 target_ulong flags = args[0];
142 target_ulong watchdogNumber = args[1]; /* 1-Based per PAPR */
143 target_ulong timeoutInMs = args[2];
144 unsigned operation = FIELD_EX64(flags, PSERIES_WDTF, OP);
145 unsigned timeoutAction = FIELD_EX64(flags, PSERIES_WDTF, ACTION);
146 SpaprWatchdog *w;
147
148 if (FIELD_EX64(flags, PSERIES_WDTF, RESERVED)) {
149 return H_PARAMETER;
150 }
151
152 switch (operation) {
153 case PSERIES_WDTF_OP_START:
154 if (!watchdog_number_valid(watchdogNumber, spapr)) {
155 return H_P2;
156 }
157 if (timeoutInMs <= WDT_MIN_TIMEOUT) {
158 return H_P3;
159 }
160
161 w = &spapr->wds[watchdogNumber - 1];
162 switch (timeoutAction) {
163 case PSERIES_WDTF_ACTION_HARD_POWER_OFF:
164 case PSERIES_WDTF_ACTION_HARD_RESTART:
165 case PSERIES_WDTF_ACTION_DUMP_RESTART:
166 w->action = timeoutAction;
167 break;
168 default:
169 return H_PARAMETER;
170 }
171 w->leave_others = FIELD_EX64(flags, PSERIES_WDTF, LEAVE_OTHER);
172 timer_mod(&w->timer,
173 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + timeoutInMs);
174 trace_spapr_watchdog_start(flags, watchdogNumber, timeoutInMs);
175 break;
176 case PSERIES_WDTF_OP_STOP:
177 if (watchdogNumber == PSERIES_WDT_STOP_ALL) {
178 ret = watchdog_stop_all(spapr);
179 } else if (!watchdog_number_valid(watchdogNumber, spapr)) {
180 return H_P2;
181 } else {
182 ret = watchdog_stop(watchdogNumber,
183 &spapr->wds[watchdogNumber - 1]);
184 }
185 break;
186 case PSERIES_WDTF_OP_QUERY:
187 args[0] = FIELD_DP64(0, PSERIES_WDTQ, MIN_TIMEOUT, WDT_MIN_TIMEOUT);
188 args[0] = FIELD_DP64(args[0], PSERIES_WDTQ, NUM,
189 ARRAY_SIZE(spapr->wds));
190 trace_spapr_watchdog_query(args[0]);
191 break;
192 case PSERIES_WDTF_OP_QUERY_LPM:
193 if (!watchdog_number_valid(watchdogNumber, spapr)) {
194 return H_P2;
195 }
196 args[0] = PSERIES_WDTQL_QUERY_NOT_STOPPED;
197 trace_spapr_watchdog_query_lpm(args[0]);
198 break;
199 default:
200 return H_PARAMETER;
201 }
202
203 return ret;
204 }
205
206 void spapr_watchdog_init(SpaprMachineState *spapr)
207 {
208 int i;
209
210 for (i = 0; i < ARRAY_SIZE(spapr->wds); ++i) {
211 char name[16];
212 SpaprWatchdog *w = &spapr->wds[i];
213
214 snprintf(name, sizeof(name) - 1, "wdt%d", i + 1);
215 object_initialize_child_with_props(OBJECT(spapr), name, w,
216 sizeof(SpaprWatchdog),
217 TYPE_SPAPR_WDT,
218 &error_fatal, NULL);
219 qdev_realize(DEVICE(w), NULL, &error_fatal);
220 }
221 }
222
223 static bool watchdog_needed(void *opaque)
224 {
225 SpaprWatchdog *w = opaque;
226
227 return timer_pending(&w->timer);
228 }
229
230 static const VMStateDescription vmstate_wdt = {
231 .name = "spapr_watchdog",
232 .version_id = 1,
233 .minimum_version_id = 1,
234 .needed = watchdog_needed,
235 .fields = (const VMStateField[]) {
236 VMSTATE_TIMER(timer, SpaprWatchdog),
237 VMSTATE_UINT8(action, SpaprWatchdog),
238 VMSTATE_UINT8(leave_others, SpaprWatchdog),
239 VMSTATE_END_OF_LIST()
240 }
241 };
242
243 static void spapr_wdt_realize(DeviceState *dev, Error **errp)
244 {
245 SpaprWatchdog *w = SPAPR_WDT(dev);
246 Object *o = OBJECT(dev);
247
248 timer_init_ms(&w->timer, QEMU_CLOCK_VIRTUAL, watchdog_expired, w);
249
250 object_property_add_uint64_ptr(o, "expire",
251 (uint64_t *)&w->timer.expire_time,
252 OBJ_PROP_FLAG_READ);
253 object_property_add_uint8_ptr(o, "action", &w->action, OBJ_PROP_FLAG_READ);
254 object_property_add_uint8_ptr(o, "leaveOtherWatchdogsRunningOnTimeout",
255 &w->leave_others, OBJ_PROP_FLAG_READ);
256 }
257
258 static void spapr_wdt_class_init(ObjectClass *oc, const void *data)
259 {
260 DeviceClass *dc = DEVICE_CLASS(oc);
261
262 dc->realize = spapr_wdt_realize;
263 dc->vmsd = &vmstate_wdt;
264 dc->user_creatable = false;
265 }
266
267 static const TypeInfo spapr_wdt_info = {
268 .name = TYPE_SPAPR_WDT,
269 .parent = TYPE_DEVICE,
270 .instance_size = sizeof(SpaprWatchdog),
271 .class_init = spapr_wdt_class_init,
272 };
273
274 static void spapr_watchdog_register_types(void)
275 {
276 spapr_register_hypercall(H_WATCHDOG, h_watchdog);
277 type_register_static(&spapr_wdt_info);
278 }
279
280 type_init(spapr_watchdog_register_types)