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
27 #include "qemu/osdep.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/sysemu.h"
33 #include "sysemu/device_tree.h"
34 #include "sysemu/cpus.h"
35 #include "sysemu/hw_accel.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"
43 #include "hw/boards.h"
46 #include "hw/ppc/spapr_drc.h"
47 #include "qemu/cutils.h"
49 #include "hw/ppc/fdt.h"
50 #include "target/ppc/mmu-hash64.h"
51 #include "target/ppc/mmu-book3s-v3.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 VIOsPAPRDevice
*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_radix_guest(callcpu
)) {
176 lpcr
|= LPCR_UPRT
| LPCR_GTSE
;
178 lpcr
&= ~(LPCR_UPRT
| LPCR_GTSE
);
181 ppc_store_lpcr(newcpu
, lpcr
);
184 * Set the timebase offset of the new CPU to that of the invoking
185 * CPU. This helps hotplugged CPU to have the correct timebase
188 newcpu
->env
.tb_env
->tb_offset
= callcpu
->env
.tb_env
->tb_offset
;
190 spapr_cpu_set_entry_state(newcpu
, start
, r3
);
192 qemu_cpu_kick(CPU(newcpu
));
194 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
197 static void rtas_stop_self(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
198 uint32_t token
, uint32_t nargs
,
200 uint32_t nret
, target_ulong rets
)
202 CPUState
*cs
= CPU(cpu
);
203 CPUPPCState
*env
= &cpu
->env
;
204 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
206 /* Disable Power-saving mode Exit Cause exceptions for the CPU.
207 * This could deliver an interrupt on a dying CPU and crash the
209 ppc_store_lpcr(cpu
, env
->spr
[SPR_LPCR
] & ~pcc
->lpcr_pm
);
211 kvmppc_set_reg_ppc_online(cpu
, 0);
215 static inline int sysparm_st(target_ulong addr
, target_ulong len
,
216 const void *val
, uint16_t vallen
)
218 hwaddr phys
= ppc64_phys_to_real(addr
);
221 return RTAS_OUT_SYSPARM_PARAM_ERROR
;
223 stw_be_phys(&address_space_memory
, phys
, vallen
);
224 cpu_physical_memory_write(phys
+ 2, val
, MIN(len
- 2, vallen
));
225 return RTAS_OUT_SUCCESS
;
228 static void rtas_ibm_get_system_parameter(PowerPCCPU
*cpu
,
229 sPAPRMachineState
*spapr
,
230 uint32_t token
, uint32_t nargs
,
232 uint32_t nret
, target_ulong rets
)
234 target_ulong parameter
= rtas_ld(args
, 0);
235 target_ulong buffer
= rtas_ld(args
, 1);
236 target_ulong length
= rtas_ld(args
, 2);
240 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
: {
241 char *param_val
= g_strdup_printf("MaxEntCap=%d,"
242 "DesMem=%" PRIu64
","
246 current_machine
->ram_size
/ MiB
,
249 ret
= sysparm_st(buffer
, length
, param_val
, strlen(param_val
) + 1);
253 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
: {
254 uint8_t param_val
= DIAGNOSTICS_RUN_MODE_DISABLED
;
256 ret
= sysparm_st(buffer
, length
, ¶m_val
, sizeof(param_val
));
259 case RTAS_SYSPARM_UUID
:
260 ret
= sysparm_st(buffer
, length
, (unsigned char *)&qemu_uuid
,
261 (qemu_uuid_set
? 16 : 0));
264 ret
= RTAS_OUT_NOT_SUPPORTED
;
267 rtas_st(rets
, 0, ret
);
270 static void rtas_ibm_set_system_parameter(PowerPCCPU
*cpu
,
271 sPAPRMachineState
*spapr
,
272 uint32_t token
, uint32_t nargs
,
274 uint32_t nret
, target_ulong rets
)
276 target_ulong parameter
= rtas_ld(args
, 0);
277 target_ulong ret
= RTAS_OUT_NOT_SUPPORTED
;
280 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
:
281 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
:
282 case RTAS_SYSPARM_UUID
:
283 ret
= RTAS_OUT_NOT_AUTHORIZED
;
287 rtas_st(rets
, 0, ret
);
290 static void rtas_ibm_os_term(PowerPCCPU
*cpu
,
291 sPAPRMachineState
*spapr
,
292 uint32_t token
, uint32_t nargs
,
294 uint32_t nret
, target_ulong rets
)
296 qemu_system_guest_panicked(NULL
);
298 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
301 static void rtas_set_power_level(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
302 uint32_t token
, uint32_t nargs
,
303 target_ulong args
, uint32_t nret
,
306 int32_t power_domain
;
308 if (nargs
!= 2 || nret
!= 2) {
309 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
313 /* we currently only use a single, "live insert" powerdomain for
314 * hotplugged/dlpar'd resources, so the power is always live/full (100)
316 power_domain
= rtas_ld(args
, 0);
317 if (power_domain
!= -1) {
318 rtas_st(rets
, 0, RTAS_OUT_NOT_SUPPORTED
);
322 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
323 rtas_st(rets
, 1, 100);
326 static void rtas_get_power_level(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
327 uint32_t token
, uint32_t nargs
,
328 target_ulong args
, uint32_t nret
,
331 int32_t power_domain
;
333 if (nargs
!= 1 || nret
!= 2) {
334 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
338 /* we currently only use a single, "live insert" powerdomain for
339 * hotplugged/dlpar'd resources, so the power is always live/full (100)
341 power_domain
= rtas_ld(args
, 0);
342 if (power_domain
!= -1) {
343 rtas_st(rets
, 0, RTAS_OUT_NOT_SUPPORTED
);
347 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
348 rtas_st(rets
, 1, 100);
351 static struct rtas_call
{
354 } rtas_table
[RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
];
356 target_ulong
spapr_rtas_call(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
357 uint32_t token
, uint32_t nargs
, target_ulong args
,
358 uint32_t nret
, target_ulong rets
)
360 if ((token
>= RTAS_TOKEN_BASE
) && (token
< RTAS_TOKEN_MAX
)) {
361 struct rtas_call
*call
= rtas_table
+ (token
- RTAS_TOKEN_BASE
);
364 call
->fn(cpu
, spapr
, token
, nargs
, args
, nret
, rets
);
369 /* HACK: Some Linux early debug code uses RTAS display-character,
370 * but assumes the token value is 0xa (which it is on some real
371 * machines) without looking it up in the device tree. This
372 * special case makes this work */
374 rtas_display_character(cpu
, spapr
, 0xa, nargs
, args
, nret
, rets
);
378 hcall_dprintf("Unknown RTAS token 0x%x\n", token
);
379 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
383 uint64_t qtest_rtas_call(char *cmd
, uint32_t nargs
, uint64_t args
,
384 uint32_t nret
, uint64_t rets
)
388 for (token
= 0; token
< RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
; token
++) {
389 if (strcmp(cmd
, rtas_table
[token
].name
) == 0) {
390 sPAPRMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
391 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
393 rtas_table
[token
].fn(cpu
, spapr
, token
+ RTAS_TOKEN_BASE
,
394 nargs
, args
, nret
, rets
);
401 void spapr_rtas_register(int token
, const char *name
, spapr_rtas_fn fn
)
403 assert((token
>= RTAS_TOKEN_BASE
) && (token
< RTAS_TOKEN_MAX
));
405 token
-= RTAS_TOKEN_BASE
;
407 assert(!rtas_table
[token
].name
);
409 rtas_table
[token
].name
= name
;
410 rtas_table
[token
].fn
= fn
;
413 void spapr_dt_rtas_tokens(void *fdt
, int rtas
)
417 for (i
= 0; i
< RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
; i
++) {
418 struct rtas_call
*call
= &rtas_table
[i
];
424 _FDT(fdt_setprop_cell(fdt
, rtas
, call
->name
, i
+ RTAS_TOKEN_BASE
));
428 void spapr_load_rtas(sPAPRMachineState
*spapr
, void *fdt
, hwaddr addr
)
433 /* Copy RTAS blob into guest RAM */
434 cpu_physical_memory_write(addr
, spapr
->rtas_blob
, spapr
->rtas_size
);
436 ret
= fdt_add_mem_rsv(fdt
, addr
, spapr
->rtas_size
);
438 error_report("Couldn't add RTAS reserve entry: %s",
443 /* Update the device tree with the blob's location */
444 rtas_node
= fdt_path_offset(fdt
, "/rtas");
445 assert(rtas_node
>= 0);
447 ret
= fdt_setprop_cell(fdt
, rtas_node
, "linux,rtas-base", addr
);
449 error_report("Couldn't add linux,rtas-base property: %s",
454 ret
= fdt_setprop_cell(fdt
, rtas_node
, "linux,rtas-entry", addr
);
456 error_report("Couldn't add linux,rtas-entry property: %s",
461 ret
= fdt_setprop_cell(fdt
, rtas_node
, "rtas-size", spapr
->rtas_size
);
463 error_report("Couldn't add rtas-size property: %s",
469 static void core_rtas_register_types(void)
471 spapr_rtas_register(RTAS_DISPLAY_CHARACTER
, "display-character",
472 rtas_display_character
);
473 spapr_rtas_register(RTAS_POWER_OFF
, "power-off", rtas_power_off
);
474 spapr_rtas_register(RTAS_SYSTEM_REBOOT
, "system-reboot",
476 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE
, "query-cpu-stopped-state",
477 rtas_query_cpu_stopped_state
);
478 spapr_rtas_register(RTAS_START_CPU
, "start-cpu", rtas_start_cpu
);
479 spapr_rtas_register(RTAS_STOP_SELF
, "stop-self", rtas_stop_self
);
480 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER
,
481 "ibm,get-system-parameter",
482 rtas_ibm_get_system_parameter
);
483 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER
,
484 "ibm,set-system-parameter",
485 rtas_ibm_set_system_parameter
);
486 spapr_rtas_register(RTAS_IBM_OS_TERM
, "ibm,os-term",
488 spapr_rtas_register(RTAS_SET_POWER_LEVEL
, "set-power-level",
489 rtas_set_power_level
);
490 spapr_rtas_register(RTAS_GET_POWER_LEVEL
, "get-power-level",
491 rtas_get_power_level
);
494 type_init(core_rtas_register_types
)