Do not include cpu.h if it's not really necessary
[qemu/ar7.git] / hw / ppc / spapr_rtas.c
blob59dbea4e22b208396e39918cf197aebd90f234aa
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.
28 #include "qemu/osdep.h"
29 #include "qemu/log.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/device_tree.h"
33 #include "sysemu/cpus.h"
34 #include "sysemu/hw_accel.h"
35 #include "sysemu/runstate.h"
36 #include "kvm_ppc.h"
38 #include "hw/ppc/spapr.h"
39 #include "hw/ppc/spapr_vio.h"
40 #include "hw/ppc/spapr_rtas.h"
41 #include "hw/ppc/spapr_cpu_core.h"
42 #include "hw/ppc/ppc.h"
44 #include <libfdt.h>
45 #include "hw/ppc/spapr_drc.h"
46 #include "qemu/cutils.h"
47 #include "trace.h"
48 #include "hw/ppc/fdt.h"
49 #include "target/ppc/mmu-hash64.h"
50 #include "target/ppc/mmu-book3s-v3.h"
51 #include "migration/blocker.h"
53 static void rtas_display_character(PowerPCCPU *cpu, SpaprMachineState *spapr,
54 uint32_t token, uint32_t nargs,
55 target_ulong args,
56 uint32_t nret, target_ulong rets)
58 uint8_t c = rtas_ld(args, 0);
59 SpaprVioDevice *sdev = vty_lookup(spapr, 0);
61 if (!sdev) {
62 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
63 } else {
64 vty_putchars(sdev, &c, sizeof(c));
65 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
69 static void rtas_power_off(PowerPCCPU *cpu, SpaprMachineState *spapr,
70 uint32_t token, uint32_t nargs, target_ulong args,
71 uint32_t nret, target_ulong rets)
73 if (nargs != 2 || nret != 1) {
74 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
75 return;
77 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
78 cpu_stop_current();
79 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
82 static void rtas_system_reboot(PowerPCCPU *cpu, SpaprMachineState *spapr,
83 uint32_t token, uint32_t nargs,
84 target_ulong args,
85 uint32_t nret, target_ulong rets)
87 if (nargs != 0 || nret != 1) {
88 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
89 return;
91 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
92 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
95 static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
96 SpaprMachineState *spapr,
97 uint32_t token, uint32_t nargs,
98 target_ulong args,
99 uint32_t nret, target_ulong rets)
101 target_ulong id;
102 PowerPCCPU *cpu;
104 if (nargs != 1 || nret != 2) {
105 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
106 return;
109 id = rtas_ld(args, 0);
110 cpu = spapr_find_cpu(id);
111 if (cpu != NULL) {
112 if (CPU(cpu)->halted) {
113 rtas_st(rets, 1, 0);
114 } else {
115 rtas_st(rets, 1, 2);
118 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
119 return;
122 /* Didn't find a matching cpu */
123 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
126 static void rtas_start_cpu(PowerPCCPU *callcpu, SpaprMachineState *spapr,
127 uint32_t token, uint32_t nargs,
128 target_ulong args,
129 uint32_t nret, target_ulong rets)
131 target_ulong id, start, r3;
132 PowerPCCPU *newcpu;
133 CPUPPCState *env;
134 PowerPCCPUClass *pcc;
135 target_ulong lpcr;
137 if (nargs != 3 || nret != 1) {
138 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
139 return;
142 id = rtas_ld(args, 0);
143 start = rtas_ld(args, 1);
144 r3 = rtas_ld(args, 2);
146 newcpu = spapr_find_cpu(id);
147 if (!newcpu) {
148 /* Didn't find a matching cpu */
149 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
150 return;
153 env = &newcpu->env;
154 pcc = POWERPC_CPU_GET_CLASS(newcpu);
156 if (!CPU(newcpu)->halted) {
157 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
158 return;
161 cpu_synchronize_state(CPU(newcpu));
163 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
165 /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
166 lpcr = env->spr[SPR_LPCR];
167 if (!pcc->interrupts_big_endian(callcpu)) {
168 lpcr |= LPCR_ILE;
170 if (env->mmu_model == POWERPC_MMU_3_00) {
172 * New cpus are expected to start in the same radix/hash mode
173 * as the existing CPUs
175 if (ppc64_v3_radix(callcpu)) {
176 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
177 } else {
178 lpcr &= ~(LPCR_UPRT | LPCR_GTSE | LPCR_HR);
180 env->spr[SPR_PSSCR] &= ~PSSCR_EC;
182 ppc_store_lpcr(newcpu, lpcr);
185 * Set the timebase offset of the new CPU to that of the invoking
186 * CPU. This helps hotplugged CPU to have the correct timebase
187 * offset.
189 newcpu->env.tb_env->tb_offset = callcpu->env.tb_env->tb_offset;
191 spapr_cpu_set_entry_state(newcpu, start, 0, r3, 0);
193 qemu_cpu_kick(CPU(newcpu));
195 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
198 static void rtas_stop_self(PowerPCCPU *cpu, SpaprMachineState *spapr,
199 uint32_t token, uint32_t nargs,
200 target_ulong args,
201 uint32_t nret, target_ulong rets)
203 CPUState *cs = CPU(cpu);
204 CPUPPCState *env = &cpu->env;
205 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
207 /* Disable Power-saving mode Exit Cause exceptions for the CPU.
208 * This could deliver an interrupt on a dying CPU and crash the
209 * guest.
210 * For the same reason, set PSSCR_EC.
212 ppc_store_lpcr(cpu, env->spr[SPR_LPCR] & ~pcc->lpcr_pm);
213 env->spr[SPR_PSSCR] |= PSSCR_EC;
214 cs->halted = 1;
215 kvmppc_set_reg_ppc_online(cpu, 0);
216 qemu_cpu_kick(cs);
219 static void rtas_ibm_suspend_me(PowerPCCPU *cpu, SpaprMachineState *spapr,
220 uint32_t token, uint32_t nargs,
221 target_ulong args,
222 uint32_t nret, target_ulong rets)
224 CPUState *cs;
226 if (nargs != 0 || nret != 1) {
227 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
228 return;
231 CPU_FOREACH(cs) {
232 PowerPCCPU *c = POWERPC_CPU(cs);
233 CPUPPCState *e = &c->env;
234 if (c == cpu) {
235 continue;
238 /* See h_join */
239 if (!cs->halted || (e->msr & (1ULL << MSR_EE))) {
240 rtas_st(rets, 0, H_MULTI_THREADS_ACTIVE);
241 return;
245 qemu_system_suspend_request();
246 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
249 static inline int sysparm_st(target_ulong addr, target_ulong len,
250 const void *val, uint16_t vallen)
252 hwaddr phys = ppc64_phys_to_real(addr);
254 if (len < 2) {
255 return RTAS_OUT_SYSPARM_PARAM_ERROR;
257 stw_be_phys(&address_space_memory, phys, vallen);
258 cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
259 return RTAS_OUT_SUCCESS;
262 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
263 SpaprMachineState *spapr,
264 uint32_t token, uint32_t nargs,
265 target_ulong args,
266 uint32_t nret, target_ulong rets)
268 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
269 MachineState *ms = MACHINE(spapr);
270 target_ulong parameter = rtas_ld(args, 0);
271 target_ulong buffer = rtas_ld(args, 1);
272 target_ulong length = rtas_ld(args, 2);
273 target_ulong ret;
275 switch (parameter) {
276 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
277 char *param_val = g_strdup_printf("MaxEntCap=%d,"
278 "DesMem=%" PRIu64 ","
279 "DesProcs=%d,"
280 "MaxPlatProcs=%d",
281 ms->smp.max_cpus,
282 ms->ram_size / MiB,
283 ms->smp.cpus,
284 ms->smp.max_cpus);
285 if (pcc->n_host_threads > 0) {
286 char *hostthr_val, *old = param_val;
289 * Add HostThrs property. This property is not present in PAPR but
290 * is expected by some guests to communicate the number of physical
291 * host threads per core on the system so that they can scale
292 * information which varies based on the thread configuration.
294 hostthr_val = g_strdup_printf(",HostThrs=%d", pcc->n_host_threads);
295 param_val = g_strconcat(param_val, hostthr_val, NULL);
296 g_free(hostthr_val);
297 g_free(old);
299 ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
300 g_free(param_val);
301 break;
303 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
304 uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
306 ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
307 break;
309 case RTAS_SYSPARM_UUID:
310 ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
311 (qemu_uuid_set ? 16 : 0));
312 break;
313 default:
314 ret = RTAS_OUT_NOT_SUPPORTED;
317 rtas_st(rets, 0, ret);
320 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
321 SpaprMachineState *spapr,
322 uint32_t token, uint32_t nargs,
323 target_ulong args,
324 uint32_t nret, target_ulong rets)
326 target_ulong parameter = rtas_ld(args, 0);
327 target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
329 switch (parameter) {
330 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
331 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
332 case RTAS_SYSPARM_UUID:
333 ret = RTAS_OUT_NOT_AUTHORIZED;
334 break;
337 rtas_st(rets, 0, ret);
340 static void rtas_ibm_os_term(PowerPCCPU *cpu,
341 SpaprMachineState *spapr,
342 uint32_t token, uint32_t nargs,
343 target_ulong args,
344 uint32_t nret, target_ulong rets)
346 target_ulong msgaddr = rtas_ld(args, 0);
347 char msg[512];
349 cpu_physical_memory_read(msgaddr, msg, sizeof(msg) - 1);
350 msg[sizeof(msg) - 1] = 0;
352 error_report("OS terminated: %s", msg);
353 qemu_system_guest_panicked(NULL);
355 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
358 static void rtas_set_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
359 uint32_t token, uint32_t nargs,
360 target_ulong args, uint32_t nret,
361 target_ulong rets)
363 int32_t power_domain;
365 if (nargs != 2 || nret != 2) {
366 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
367 return;
370 /* we currently only use a single, "live insert" powerdomain for
371 * hotplugged/dlpar'd resources, so the power is always live/full (100)
373 power_domain = rtas_ld(args, 0);
374 if (power_domain != -1) {
375 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
376 return;
379 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
380 rtas_st(rets, 1, 100);
383 static void rtas_get_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
384 uint32_t token, uint32_t nargs,
385 target_ulong args, uint32_t nret,
386 target_ulong rets)
388 int32_t power_domain;
390 if (nargs != 1 || nret != 2) {
391 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
392 return;
395 /* we currently only use a single, "live insert" powerdomain for
396 * hotplugged/dlpar'd resources, so the power is always live/full (100)
398 power_domain = rtas_ld(args, 0);
399 if (power_domain != -1) {
400 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
401 return;
404 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
405 rtas_st(rets, 1, 100);
408 static void rtas_ibm_nmi_register(PowerPCCPU *cpu,
409 SpaprMachineState *spapr,
410 uint32_t token, uint32_t nargs,
411 target_ulong args,
412 uint32_t nret, target_ulong rets)
414 hwaddr rtas_addr;
415 target_ulong sreset_addr, mce_addr;
417 if (spapr_get_cap(spapr, SPAPR_CAP_FWNMI) == SPAPR_CAP_OFF) {
418 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
419 return;
422 rtas_addr = spapr_get_rtas_addr();
423 if (!rtas_addr) {
424 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
425 return;
428 sreset_addr = rtas_ld(args, 0);
429 mce_addr = rtas_ld(args, 1);
431 /* PAPR requires these are in the first 32M of memory and within RMA */
432 if (sreset_addr >= 32 * MiB || sreset_addr >= spapr->rma_size ||
433 mce_addr >= 32 * MiB || mce_addr >= spapr->rma_size) {
434 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
435 return;
438 if (kvm_enabled()) {
439 if (kvmppc_set_fwnmi(cpu) < 0) {
440 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
441 return;
445 spapr->fwnmi_system_reset_addr = sreset_addr;
446 spapr->fwnmi_machine_check_addr = mce_addr;
448 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
451 static void rtas_ibm_nmi_interlock(PowerPCCPU *cpu,
452 SpaprMachineState *spapr,
453 uint32_t token, uint32_t nargs,
454 target_ulong args,
455 uint32_t nret, target_ulong rets)
457 if (spapr_get_cap(spapr, SPAPR_CAP_FWNMI) == SPAPR_CAP_OFF) {
458 rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
459 return;
462 if (spapr->fwnmi_machine_check_addr == -1) {
463 qemu_log_mask(LOG_GUEST_ERROR,
464 "FWNMI: ibm,nmi-interlock RTAS called with FWNMI not registered.\n");
466 /* NMI register not called */
467 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
468 return;
471 if (spapr->fwnmi_machine_check_interlock != cpu->vcpu_id) {
473 * The vCPU that hit the NMI should invoke "ibm,nmi-interlock"
474 * This should be PARAM_ERROR, but Linux calls "ibm,nmi-interlock"
475 * for system reset interrupts, despite them not being interlocked.
476 * PowerVM silently ignores this and returns success here. Returning
477 * failure causes Linux to print the error "FWNMI: nmi-interlock
478 * failed: -3", although no other apparent ill effects, this is a
479 * regression for the user when enabling FWNMI. So for now, match
480 * PowerVM. When most Linux clients are fixed, this could be
481 * changed.
483 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
484 return;
488 * vCPU issuing "ibm,nmi-interlock" is done with NMI handling,
489 * hence unset fwnmi_machine_check_interlock.
491 spapr->fwnmi_machine_check_interlock = -1;
492 qemu_cond_signal(&spapr->fwnmi_machine_check_interlock_cond);
493 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
494 migrate_del_blocker(spapr->fwnmi_migration_blocker);
497 static struct rtas_call {
498 const char *name;
499 spapr_rtas_fn fn;
500 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
502 target_ulong spapr_rtas_call(PowerPCCPU *cpu, SpaprMachineState *spapr,
503 uint32_t token, uint32_t nargs, target_ulong args,
504 uint32_t nret, target_ulong rets)
506 if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
507 struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
509 if (call->fn) {
510 call->fn(cpu, spapr, token, nargs, args, nret, rets);
511 return H_SUCCESS;
515 /* HACK: Some Linux early debug code uses RTAS display-character,
516 * but assumes the token value is 0xa (which it is on some real
517 * machines) without looking it up in the device tree. This
518 * special case makes this work */
519 if (token == 0xa) {
520 rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
521 return H_SUCCESS;
524 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
525 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
526 return H_PARAMETER;
529 uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
530 uint32_t nret, uint64_t rets)
532 int token;
534 for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
535 if (strcmp(cmd, rtas_table[token].name) == 0) {
536 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
537 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
539 rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
540 nargs, args, nret, rets);
541 return H_SUCCESS;
544 return H_PARAMETER;
547 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
549 assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
551 token -= RTAS_TOKEN_BASE;
553 assert(!name || !rtas_table[token].name);
555 rtas_table[token].name = name;
556 rtas_table[token].fn = fn;
559 void spapr_dt_rtas_tokens(void *fdt, int rtas)
561 int i;
563 for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
564 struct rtas_call *call = &rtas_table[i];
566 if (!call->name) {
567 continue;
570 _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
574 hwaddr spapr_get_rtas_addr(void)
576 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
577 int rtas_node;
578 const fdt32_t *rtas_data;
579 void *fdt = spapr->fdt_blob;
581 /* fetch rtas addr from fdt */
582 rtas_node = fdt_path_offset(fdt, "/rtas");
583 if (rtas_node < 0) {
584 return 0;
587 rtas_data = fdt_getprop(fdt, rtas_node, "linux,rtas-base", NULL);
588 if (!rtas_data) {
589 return 0;
593 * We assume that the OS called RTAS instantiate-rtas, but some other
594 * OS might call RTAS instantiate-rtas-64 instead. This fine as of now
595 * as SLOF only supports 32-bit variant.
597 return (hwaddr)fdt32_to_cpu(*rtas_data);
600 static void core_rtas_register_types(void)
602 spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
603 rtas_display_character);
604 spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
605 spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
606 rtas_system_reboot);
607 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
608 rtas_query_cpu_stopped_state);
609 spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
610 spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
611 spapr_rtas_register(RTAS_IBM_SUSPEND_ME, "ibm,suspend-me",
612 rtas_ibm_suspend_me);
613 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
614 "ibm,get-system-parameter",
615 rtas_ibm_get_system_parameter);
616 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
617 "ibm,set-system-parameter",
618 rtas_ibm_set_system_parameter);
619 spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
620 rtas_ibm_os_term);
621 spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
622 rtas_set_power_level);
623 spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
624 rtas_get_power_level);
625 spapr_rtas_register(RTAS_IBM_NMI_REGISTER, "ibm,nmi-register",
626 rtas_ibm_nmi_register);
627 spapr_rtas_register(RTAS_IBM_NMI_INTERLOCK, "ibm,nmi-interlock",
628 rtas_ibm_nmi_interlock);
631 type_init(core_rtas_register_types)