virtio-scsi: Allocate op blocker reason before blocking
[qemu/ar7.git] / hw / ppc / spapr_rtas.c
blob2ec2a8e4d1926385cb532805bac2861c5a581857
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * Hypercall based emulated RTAS
6 * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "cpu.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/char.h"
30 #include "hw/qdev.h"
31 #include "sysemu/device_tree.h"
33 #include "hw/ppc/spapr.h"
34 #include "hw/ppc/spapr_vio.h"
35 #include "qapi-event.h"
37 #include <libfdt.h>
39 static void rtas_display_character(PowerPCCPU *cpu, sPAPREnvironment *spapr,
40 uint32_t token, uint32_t nargs,
41 target_ulong args,
42 uint32_t nret, target_ulong rets)
44 uint8_t c = rtas_ld(args, 0);
45 VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
47 if (!sdev) {
48 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
49 } else {
50 vty_putchars(sdev, &c, sizeof(c));
51 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
55 static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
56 uint32_t token, uint32_t nargs,
57 target_ulong args,
58 uint32_t nret, target_ulong rets)
60 struct tm tm;
62 if (nret != 8) {
63 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
64 return;
67 qemu_get_timedate(&tm, spapr->rtc_offset);
69 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
70 rtas_st(rets, 1, tm.tm_year + 1900);
71 rtas_st(rets, 2, tm.tm_mon + 1);
72 rtas_st(rets, 3, tm.tm_mday);
73 rtas_st(rets, 4, tm.tm_hour);
74 rtas_st(rets, 5, tm.tm_min);
75 rtas_st(rets, 6, tm.tm_sec);
76 rtas_st(rets, 7, 0); /* we don't do nanoseconds */
79 static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
80 uint32_t token, uint32_t nargs,
81 target_ulong args,
82 uint32_t nret, target_ulong rets)
84 struct tm tm;
86 tm.tm_year = rtas_ld(args, 0) - 1900;
87 tm.tm_mon = rtas_ld(args, 1) - 1;
88 tm.tm_mday = rtas_ld(args, 2);
89 tm.tm_hour = rtas_ld(args, 3);
90 tm.tm_min = rtas_ld(args, 4);
91 tm.tm_sec = rtas_ld(args, 5);
93 /* Just generate a monitor event for the change */
94 qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
95 spapr->rtc_offset = qemu_timedate_diff(&tm);
97 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
100 static void rtas_power_off(PowerPCCPU *cpu, sPAPREnvironment *spapr,
101 uint32_t token, uint32_t nargs, target_ulong args,
102 uint32_t nret, target_ulong rets)
104 if (nargs != 2 || nret != 1) {
105 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
106 return;
108 qemu_system_shutdown_request();
109 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
112 static void rtas_system_reboot(PowerPCCPU *cpu, sPAPREnvironment *spapr,
113 uint32_t token, uint32_t nargs,
114 target_ulong args,
115 uint32_t nret, target_ulong rets)
117 if (nargs != 0 || nret != 1) {
118 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
119 return;
121 qemu_system_reset_request();
122 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
125 static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
126 sPAPREnvironment *spapr,
127 uint32_t token, uint32_t nargs,
128 target_ulong args,
129 uint32_t nret, target_ulong rets)
131 target_ulong id;
132 PowerPCCPU *cpu;
134 if (nargs != 1 || nret != 2) {
135 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
136 return;
139 id = rtas_ld(args, 0);
140 cpu = ppc_get_vcpu_by_dt_id(id);
141 if (cpu != NULL) {
142 if (CPU(cpu)->halted) {
143 rtas_st(rets, 1, 0);
144 } else {
145 rtas_st(rets, 1, 2);
148 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
149 return;
152 /* Didn't find a matching cpu */
153 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
156 static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
157 uint32_t token, uint32_t nargs,
158 target_ulong args,
159 uint32_t nret, target_ulong rets)
161 target_ulong id, start, r3;
162 PowerPCCPU *cpu;
164 if (nargs != 3 || nret != 1) {
165 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
166 return;
169 id = rtas_ld(args, 0);
170 start = rtas_ld(args, 1);
171 r3 = rtas_ld(args, 2);
173 cpu = ppc_get_vcpu_by_dt_id(id);
174 if (cpu != NULL) {
175 CPUState *cs = CPU(cpu);
176 CPUPPCState *env = &cpu->env;
178 if (!cs->halted) {
179 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
180 return;
183 /* This will make sure qemu state is up to date with kvm, and
184 * mark it dirty so our changes get flushed back before the
185 * new cpu enters */
186 kvm_cpu_synchronize_state(cs);
188 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
189 env->nip = start;
190 env->gpr[3] = r3;
191 cs->halted = 0;
193 qemu_cpu_kick(cs);
195 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
196 return;
199 /* Didn't find a matching cpu */
200 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
203 static void rtas_stop_self(PowerPCCPU *cpu, sPAPREnvironment *spapr,
204 uint32_t token, uint32_t nargs,
205 target_ulong args,
206 uint32_t nret, target_ulong rets)
208 CPUState *cs = CPU(cpu);
209 CPUPPCState *env = &cpu->env;
211 cs->halted = 1;
212 cpu_exit(cs);
214 * While stopping a CPU, the guest calls H_CPPR which
215 * effectively disables interrupts on XICS level.
216 * However decrementer interrupts in TCG can still
217 * wake the CPU up so here we disable interrupts in MSR
218 * as well.
219 * As rtas_start_cpu() resets the whole MSR anyway, there is
220 * no need to bother with specific bits, we just clear it.
222 env->msr = 0;
225 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
226 sPAPREnvironment *spapr,
227 uint32_t token, uint32_t nargs,
228 target_ulong args,
229 uint32_t nret, target_ulong rets)
231 target_ulong parameter = rtas_ld(args, 0);
232 target_ulong buffer = rtas_ld(args, 1);
233 target_ulong length = rtas_ld(args, 2);
234 target_ulong ret = RTAS_OUT_SUCCESS;
236 switch (parameter) {
237 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
238 char *param_val = g_strdup_printf("MaxEntCap=%d,MaxPlatProcs=%d",
239 max_cpus, smp_cpus);
240 rtas_st_buffer(buffer, length, (uint8_t *)param_val, strlen(param_val));
241 g_free(param_val);
242 break;
244 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
245 uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
247 rtas_st_buffer(buffer, length, &param_val, sizeof(param_val));
248 break;
250 case RTAS_SYSPARM_UUID:
251 rtas_st_buffer(buffer, length, qemu_uuid, (qemu_uuid_set ? 16 : 0));
252 break;
253 default:
254 ret = RTAS_OUT_NOT_SUPPORTED;
257 rtas_st(rets, 0, ret);
260 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
261 sPAPREnvironment *spapr,
262 uint32_t token, uint32_t nargs,
263 target_ulong args,
264 uint32_t nret, target_ulong rets)
266 target_ulong parameter = rtas_ld(args, 0);
267 target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
269 switch (parameter) {
270 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
271 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
272 case RTAS_SYSPARM_UUID:
273 ret = RTAS_OUT_NOT_AUTHORIZED;
274 break;
277 rtas_st(rets, 0, ret);
280 static void rtas_ibm_os_term(PowerPCCPU *cpu,
281 sPAPREnvironment *spapr,
282 uint32_t token, uint32_t nargs,
283 target_ulong args,
284 uint32_t nret, target_ulong rets)
286 target_ulong ret = 0;
288 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
290 rtas_st(rets, 0, ret);
293 static struct rtas_call {
294 const char *name;
295 spapr_rtas_fn fn;
296 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
298 target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPREnvironment *spapr,
299 uint32_t token, uint32_t nargs, target_ulong args,
300 uint32_t nret, target_ulong rets)
302 if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
303 struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
305 if (call->fn) {
306 call->fn(cpu, spapr, token, nargs, args, nret, rets);
307 return H_SUCCESS;
311 /* HACK: Some Linux early debug code uses RTAS display-character,
312 * but assumes the token value is 0xa (which it is on some real
313 * machines) without looking it up in the device tree. This
314 * special case makes this work */
315 if (token == 0xa) {
316 rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
317 return H_SUCCESS;
320 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
321 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
322 return H_PARAMETER;
325 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
327 if (!((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX))) {
328 fprintf(stderr, "RTAS invalid token 0x%x\n", token);
329 exit(1);
332 token -= RTAS_TOKEN_BASE;
333 if (rtas_table[token].name) {
334 fprintf(stderr, "RTAS call \"%s\" is registered already as 0x%x\n",
335 rtas_table[token].name, token);
336 exit(1);
339 rtas_table[token].name = name;
340 rtas_table[token].fn = fn;
343 int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
344 hwaddr rtas_size)
346 int ret;
347 int i;
349 ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
350 if (ret < 0) {
351 fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
352 fdt_strerror(ret));
353 return ret;
356 ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-base",
357 rtas_addr);
358 if (ret < 0) {
359 fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
360 fdt_strerror(ret));
361 return ret;
364 ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
365 rtas_addr);
366 if (ret < 0) {
367 fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
368 fdt_strerror(ret));
369 return ret;
372 ret = qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size",
373 rtas_size);
374 if (ret < 0) {
375 fprintf(stderr, "Couldn't add rtas-size property: %s\n",
376 fdt_strerror(ret));
377 return ret;
380 for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
381 struct rtas_call *call = &rtas_table[i];
383 if (!call->name) {
384 continue;
387 ret = qemu_fdt_setprop_cell(fdt, "/rtas", call->name,
388 i + RTAS_TOKEN_BASE);
389 if (ret < 0) {
390 fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
391 call->name, fdt_strerror(ret));
392 return ret;
396 return 0;
399 static void core_rtas_register_types(void)
401 spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
402 rtas_display_character);
403 spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
404 rtas_get_time_of_day);
405 spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
406 rtas_set_time_of_day);
407 spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
408 spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
409 rtas_system_reboot);
410 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
411 rtas_query_cpu_stopped_state);
412 spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
413 spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
414 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
415 "ibm,get-system-parameter",
416 rtas_ibm_get_system_parameter);
417 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
418 "ibm,set-system-parameter",
419 rtas_ibm_set_system_parameter);
420 spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
421 rtas_ibm_os_term);
424 type_init(core_rtas_register_types)