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"
37 #include "hw/ppc/spapr.h"
38 #include "hw/ppc/spapr_vio.h"
39 #include "hw/ppc/spapr_rtas.h"
40 #include "hw/ppc/spapr_cpu_core.h"
41 #include "hw/ppc/ppc.h"
42 #include "hw/boards.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"
52 static void rtas_display_character(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
53 uint32_t token
, uint32_t nargs
,
55 uint32_t nret
, target_ulong rets
)
57 uint8_t c
= rtas_ld(args
, 0);
58 VIOsPAPRDevice
*sdev
= vty_lookup(spapr
, 0);
61 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
63 vty_putchars(sdev
, &c
, sizeof(c
));
64 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
68 static void rtas_power_off(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
69 uint32_t token
, uint32_t nargs
, target_ulong args
,
70 uint32_t nret
, target_ulong rets
)
72 if (nargs
!= 2 || nret
!= 1) {
73 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
76 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
78 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
81 static void rtas_system_reboot(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
82 uint32_t token
, uint32_t nargs
,
84 uint32_t nret
, target_ulong rets
)
86 if (nargs
!= 0 || nret
!= 1) {
87 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
90 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
91 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
94 static void rtas_query_cpu_stopped_state(PowerPCCPU
*cpu_
,
95 sPAPRMachineState
*spapr
,
96 uint32_t token
, uint32_t nargs
,
98 uint32_t nret
, target_ulong rets
)
103 if (nargs
!= 1 || nret
!= 2) {
104 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
108 id
= rtas_ld(args
, 0);
109 cpu
= spapr_find_cpu(id
);
111 if (CPU(cpu
)->halted
) {
117 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
121 /* Didn't find a matching cpu */
122 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
125 static void rtas_start_cpu(PowerPCCPU
*callcpu
, sPAPRMachineState
*spapr
,
126 uint32_t token
, uint32_t nargs
,
128 uint32_t nret
, target_ulong rets
)
130 target_ulong id
, start
, r3
;
133 PowerPCCPUClass
*pcc
;
136 if (nargs
!= 3 || nret
!= 1) {
137 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
141 id
= rtas_ld(args
, 0);
142 start
= rtas_ld(args
, 1);
143 r3
= rtas_ld(args
, 2);
145 newcpu
= spapr_find_cpu(id
);
147 /* Didn't find a matching cpu */
148 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
153 pcc
= POWERPC_CPU_GET_CLASS(newcpu
);
155 if (!CPU(newcpu
)->halted
) {
156 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
160 cpu_synchronize_state(CPU(newcpu
));
162 env
->msr
= (1ULL << MSR_SF
) | (1ULL << MSR_ME
);
164 /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
165 lpcr
= env
->spr
[SPR_LPCR
];
166 if (!pcc
->interrupts_big_endian(callcpu
)) {
169 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
171 * New cpus are expected to start in the same radix/hash mode
172 * as the existing CPUs
174 if (ppc64_radix_guest(callcpu
)) {
175 lpcr
|= LPCR_UPRT
| LPCR_GTSE
;
177 lpcr
&= ~(LPCR_UPRT
| LPCR_GTSE
);
180 ppc_store_lpcr(newcpu
, lpcr
);
183 * Set the timebase offset of the new CPU to that of the invoking
184 * CPU. This helps hotplugged CPU to have the correct timebase
187 newcpu
->env
.tb_env
->tb_offset
= callcpu
->env
.tb_env
->tb_offset
;
189 spapr_cpu_set_entry_state(newcpu
, start
, r3
);
191 qemu_cpu_kick(CPU(newcpu
));
193 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
196 static void rtas_stop_self(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
197 uint32_t token
, uint32_t nargs
,
199 uint32_t nret
, target_ulong rets
)
201 CPUState
*cs
= CPU(cpu
);
202 CPUPPCState
*env
= &cpu
->env
;
203 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
205 /* Disable Power-saving mode Exit Cause exceptions for the CPU.
206 * This could deliver an interrupt on a dying CPU and crash the
208 ppc_store_lpcr(cpu
, env
->spr
[SPR_LPCR
] & ~pcc
->lpcr_pm
);
213 static inline int sysparm_st(target_ulong addr
, target_ulong len
,
214 const void *val
, uint16_t vallen
)
216 hwaddr phys
= ppc64_phys_to_real(addr
);
219 return RTAS_OUT_SYSPARM_PARAM_ERROR
;
221 stw_be_phys(&address_space_memory
, phys
, vallen
);
222 cpu_physical_memory_write(phys
+ 2, val
, MIN(len
- 2, vallen
));
223 return RTAS_OUT_SUCCESS
;
226 static void rtas_ibm_get_system_parameter(PowerPCCPU
*cpu
,
227 sPAPRMachineState
*spapr
,
228 uint32_t token
, uint32_t nargs
,
230 uint32_t nret
, target_ulong rets
)
232 target_ulong parameter
= rtas_ld(args
, 0);
233 target_ulong buffer
= rtas_ld(args
, 1);
234 target_ulong length
= rtas_ld(args
, 2);
238 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
: {
239 char *param_val
= g_strdup_printf("MaxEntCap=%d,"
244 current_machine
->ram_size
/ M_BYTE
,
247 ret
= sysparm_st(buffer
, length
, param_val
, strlen(param_val
) + 1);
251 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
: {
252 uint8_t param_val
= DIAGNOSTICS_RUN_MODE_DISABLED
;
254 ret
= sysparm_st(buffer
, length
, ¶m_val
, sizeof(param_val
));
257 case RTAS_SYSPARM_UUID
:
258 ret
= sysparm_st(buffer
, length
, (unsigned char *)&qemu_uuid
,
259 (qemu_uuid_set
? 16 : 0));
262 ret
= RTAS_OUT_NOT_SUPPORTED
;
265 rtas_st(rets
, 0, ret
);
268 static void rtas_ibm_set_system_parameter(PowerPCCPU
*cpu
,
269 sPAPRMachineState
*spapr
,
270 uint32_t token
, uint32_t nargs
,
272 uint32_t nret
, target_ulong rets
)
274 target_ulong parameter
= rtas_ld(args
, 0);
275 target_ulong ret
= RTAS_OUT_NOT_SUPPORTED
;
278 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
:
279 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
:
280 case RTAS_SYSPARM_UUID
:
281 ret
= RTAS_OUT_NOT_AUTHORIZED
;
285 rtas_st(rets
, 0, ret
);
288 static void rtas_ibm_os_term(PowerPCCPU
*cpu
,
289 sPAPRMachineState
*spapr
,
290 uint32_t token
, uint32_t nargs
,
292 uint32_t nret
, target_ulong rets
)
294 qemu_system_guest_panicked(NULL
);
296 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
299 static void rtas_set_power_level(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
300 uint32_t token
, uint32_t nargs
,
301 target_ulong args
, uint32_t nret
,
304 int32_t power_domain
;
306 if (nargs
!= 2 || nret
!= 2) {
307 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
311 /* we currently only use a single, "live insert" powerdomain for
312 * hotplugged/dlpar'd resources, so the power is always live/full (100)
314 power_domain
= rtas_ld(args
, 0);
315 if (power_domain
!= -1) {
316 rtas_st(rets
, 0, RTAS_OUT_NOT_SUPPORTED
);
320 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
321 rtas_st(rets
, 1, 100);
324 static void rtas_get_power_level(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
325 uint32_t token
, uint32_t nargs
,
326 target_ulong args
, uint32_t nret
,
329 int32_t power_domain
;
331 if (nargs
!= 1 || nret
!= 2) {
332 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
336 /* we currently only use a single, "live insert" powerdomain for
337 * hotplugged/dlpar'd resources, so the power is always live/full (100)
339 power_domain
= rtas_ld(args
, 0);
340 if (power_domain
!= -1) {
341 rtas_st(rets
, 0, RTAS_OUT_NOT_SUPPORTED
);
345 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
346 rtas_st(rets
, 1, 100);
349 static struct rtas_call
{
352 } rtas_table
[RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
];
354 target_ulong
spapr_rtas_call(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
355 uint32_t token
, uint32_t nargs
, target_ulong args
,
356 uint32_t nret
, target_ulong rets
)
358 if ((token
>= RTAS_TOKEN_BASE
) && (token
< RTAS_TOKEN_MAX
)) {
359 struct rtas_call
*call
= rtas_table
+ (token
- RTAS_TOKEN_BASE
);
362 call
->fn(cpu
, spapr
, token
, nargs
, args
, nret
, rets
);
367 /* HACK: Some Linux early debug code uses RTAS display-character,
368 * but assumes the token value is 0xa (which it is on some real
369 * machines) without looking it up in the device tree. This
370 * special case makes this work */
372 rtas_display_character(cpu
, spapr
, 0xa, nargs
, args
, nret
, rets
);
376 hcall_dprintf("Unknown RTAS token 0x%x\n", token
);
377 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
381 uint64_t qtest_rtas_call(char *cmd
, uint32_t nargs
, uint64_t args
,
382 uint32_t nret
, uint64_t rets
)
386 for (token
= 0; token
< RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
; token
++) {
387 if (strcmp(cmd
, rtas_table
[token
].name
) == 0) {
388 sPAPRMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
389 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
391 rtas_table
[token
].fn(cpu
, spapr
, token
+ RTAS_TOKEN_BASE
,
392 nargs
, args
, nret
, rets
);
399 void spapr_rtas_register(int token
, const char *name
, spapr_rtas_fn fn
)
401 assert((token
>= RTAS_TOKEN_BASE
) && (token
< RTAS_TOKEN_MAX
));
403 token
-= RTAS_TOKEN_BASE
;
405 assert(!rtas_table
[token
].name
);
407 rtas_table
[token
].name
= name
;
408 rtas_table
[token
].fn
= fn
;
411 void spapr_dt_rtas_tokens(void *fdt
, int rtas
)
415 for (i
= 0; i
< RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
; i
++) {
416 struct rtas_call
*call
= &rtas_table
[i
];
422 _FDT(fdt_setprop_cell(fdt
, rtas
, call
->name
, i
+ RTAS_TOKEN_BASE
));
426 void spapr_load_rtas(sPAPRMachineState
*spapr
, void *fdt
, hwaddr addr
)
431 /* Copy RTAS blob into guest RAM */
432 cpu_physical_memory_write(addr
, spapr
->rtas_blob
, spapr
->rtas_size
);
434 ret
= fdt_add_mem_rsv(fdt
, addr
, spapr
->rtas_size
);
436 error_report("Couldn't add RTAS reserve entry: %s",
441 /* Update the device tree with the blob's location */
442 rtas_node
= fdt_path_offset(fdt
, "/rtas");
443 assert(rtas_node
>= 0);
445 ret
= fdt_setprop_cell(fdt
, rtas_node
, "linux,rtas-base", addr
);
447 error_report("Couldn't add linux,rtas-base property: %s",
452 ret
= fdt_setprop_cell(fdt
, rtas_node
, "linux,rtas-entry", addr
);
454 error_report("Couldn't add linux,rtas-entry property: %s",
459 ret
= fdt_setprop_cell(fdt
, rtas_node
, "rtas-size", spapr
->rtas_size
);
461 error_report("Couldn't add rtas-size property: %s",
467 static void core_rtas_register_types(void)
469 spapr_rtas_register(RTAS_DISPLAY_CHARACTER
, "display-character",
470 rtas_display_character
);
471 spapr_rtas_register(RTAS_POWER_OFF
, "power-off", rtas_power_off
);
472 spapr_rtas_register(RTAS_SYSTEM_REBOOT
, "system-reboot",
474 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE
, "query-cpu-stopped-state",
475 rtas_query_cpu_stopped_state
);
476 spapr_rtas_register(RTAS_START_CPU
, "start-cpu", rtas_start_cpu
);
477 spapr_rtas_register(RTAS_STOP_SELF
, "stop-self", rtas_stop_self
);
478 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER
,
479 "ibm,get-system-parameter",
480 rtas_ibm_get_system_parameter
);
481 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER
,
482 "ibm,set-system-parameter",
483 rtas_ibm_set_system_parameter
);
484 spapr_rtas_register(RTAS_IBM_OS_TERM
, "ibm,os-term",
486 spapr_rtas_register(RTAS_SET_POWER_LEVEL
, "set-power-level",
487 rtas_set_power_level
);
488 spapr_rtas_register(RTAS_GET_POWER_LEVEL
, "get-power-level",
489 rtas_get_power_level
);
492 type_init(core_rtas_register_types
)