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
28 #include "qemu/osdep.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"
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"
45 #include "hw/ppc/spapr_drc.h"
46 #include "qemu/cutils.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
,
56 uint32_t nret
, target_ulong rets
)
58 uint8_t c
= rtas_ld(args
, 0);
59 SpaprVioDevice
*sdev
= vty_lookup(spapr
, 0);
62 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
);
77 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
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
,
85 uint32_t nret
, target_ulong rets
)
87 if (nargs
!= 0 || nret
!= 1) {
88 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
,
99 uint32_t nret
, target_ulong rets
)
104 if (nargs
!= 1 || nret
!= 2) {
105 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
109 id
= rtas_ld(args
, 0);
110 cpu
= spapr_find_cpu(id
);
112 if (CPU(cpu
)->halted
) {
118 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
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
,
129 uint32_t nret
, target_ulong rets
)
131 target_ulong id
, start
, r3
;
134 PowerPCCPUClass
*pcc
;
137 if (nargs
!= 3 || nret
!= 1) {
138 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
142 id
= rtas_ld(args
, 0);
143 start
= rtas_ld(args
, 1);
144 r3
= rtas_ld(args
, 2);
146 newcpu
= spapr_find_cpu(id
);
148 /* Didn't find a matching cpu */
149 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
154 pcc
= POWERPC_CPU_GET_CLASS(newcpu
);
156 if (!CPU(newcpu
)->halted
) {
157 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
)) {
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
;
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
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
,
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
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
;
215 kvmppc_set_reg_ppc_online(cpu
, 0);
219 static void rtas_ibm_suspend_me(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
220 uint32_t token
, uint32_t nargs
,
222 uint32_t nret
, target_ulong rets
)
226 if (nargs
!= 0 || nret
!= 1) {
227 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
232 PowerPCCPU
*c
= POWERPC_CPU(cs
);
233 CPUPPCState
*e
= &c
->env
;
239 if (!cs
->halted
|| (e
->msr
& (1ULL << MSR_EE
))) {
240 rtas_st(rets
, 0, H_MULTI_THREADS_ACTIVE
);
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
);
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
,
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);
276 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
: {
277 char *param_val
= g_strdup_printf("MaxEntCap=%d,"
278 "DesMem=%" PRIu64
","
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
);
299 ret
= sysparm_st(buffer
, length
, param_val
, strlen(param_val
) + 1);
303 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
: {
304 uint8_t param_val
= DIAGNOSTICS_RUN_MODE_DISABLED
;
306 ret
= sysparm_st(buffer
, length
, ¶m_val
, sizeof(param_val
));
309 case RTAS_SYSPARM_UUID
:
310 ret
= sysparm_st(buffer
, length
, (unsigned char *)&qemu_uuid
,
311 (qemu_uuid_set
? 16 : 0));
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
,
324 uint32_t nret
, target_ulong rets
)
326 target_ulong parameter
= rtas_ld(args
, 0);
327 target_ulong ret
= RTAS_OUT_NOT_SUPPORTED
;
330 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
:
331 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
:
332 case RTAS_SYSPARM_UUID
:
333 ret
= RTAS_OUT_NOT_AUTHORIZED
;
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
,
344 uint32_t nret
, target_ulong rets
)
346 target_ulong msgaddr
= rtas_ld(args
, 0);
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
,
363 int32_t power_domain
;
365 if (nargs
!= 2 || nret
!= 2) {
366 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
);
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
,
388 int32_t power_domain
;
390 if (nargs
!= 1 || nret
!= 2) {
391 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
);
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
,
412 uint32_t nret
, target_ulong rets
)
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
);
422 rtas_addr
= spapr_get_rtas_addr();
424 rtas_st(rets
, 0, RTAS_OUT_NOT_SUPPORTED
);
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
);
439 if (kvmppc_set_fwnmi(cpu
) < 0) {
440 rtas_st(rets
, 0, RTAS_OUT_NOT_SUPPORTED
);
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
,
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
);
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
);
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
483 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
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
{
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
);
510 call
->fn(cpu
, spapr
, token
, nargs
, args
, nret
, rets
);
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 */
520 rtas_display_character(cpu
, spapr
, 0xa, nargs
, args
, nret
, rets
);
524 hcall_dprintf("Unknown RTAS token 0x%x\n", token
);
525 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
529 uint64_t qtest_rtas_call(char *cmd
, uint32_t nargs
, uint64_t args
,
530 uint32_t nret
, uint64_t rets
)
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
);
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
)
563 for (i
= 0; i
< RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
; i
++) {
564 struct rtas_call
*call
= &rtas_table
[i
];
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());
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");
587 rtas_data
= fdt_getprop(fdt
, rtas_node
, "linux,rtas-base", NULL
);
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",
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",
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
)