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 "sysemu/sysemu.h"
29 #include "sysemu/char.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"
39 static void rtas_display_character(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
40 uint32_t token
, uint32_t nargs
,
42 uint32_t nret
, target_ulong rets
)
44 uint8_t c
= rtas_ld(args
, 0);
45 VIOsPAPRDevice
*sdev
= vty_lookup(spapr
, 0);
48 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
,
58 uint32_t nret
, target_ulong rets
)
63 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
,
82 uint32_t nret
, target_ulong rets
)
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
);
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
,
115 uint32_t nret
, target_ulong rets
)
117 if (nargs
!= 0 || nret
!= 1) {
118 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
,
129 uint32_t nret
, target_ulong rets
)
134 if (nargs
!= 1 || nret
!= 2) {
135 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
139 id
= rtas_ld(args
, 0);
140 cpu
= ppc_get_vcpu_by_dt_id(id
);
142 if (CPU(cpu
)->halted
) {
148 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
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
,
159 uint32_t nret
, target_ulong rets
)
161 target_ulong id
, start
, r3
;
164 if (nargs
!= 3 || nret
!= 1) {
165 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
);
175 CPUState
*cs
= CPU(cpu
);
176 CPUPPCState
*env
= &cpu
->env
;
179 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
186 kvm_cpu_synchronize_state(cs
);
188 env
->msr
= (1ULL << MSR_SF
) | (1ULL << MSR_ME
);
195 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
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
,
206 uint32_t nret
, target_ulong rets
)
208 CPUState
*cs
= CPU(cpu
);
209 CPUPPCState
*env
= &cpu
->env
;
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
219 * As rtas_start_cpu() resets the whole MSR anyway, there is
220 * no need to bother with specific bits, we just clear it.
225 static void rtas_ibm_get_system_parameter(PowerPCCPU
*cpu
,
226 sPAPREnvironment
*spapr
,
227 uint32_t token
, uint32_t nargs
,
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
;
237 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
: {
238 char *param_val
= g_strdup_printf("MaxEntCap=%d,MaxPlatProcs=%d",
240 rtas_st_buffer(buffer
, length
, (uint8_t *)param_val
, strlen(param_val
));
244 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
: {
245 uint8_t param_val
= DIAGNOSTICS_RUN_MODE_DISABLED
;
247 rtas_st_buffer(buffer
, length
, ¶m_val
, sizeof(param_val
));
250 case RTAS_SYSPARM_UUID
:
251 rtas_st_buffer(buffer
, length
, qemu_uuid
, (qemu_uuid_set
? 16 : 0));
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
,
264 uint32_t nret
, target_ulong rets
)
266 target_ulong parameter
= rtas_ld(args
, 0);
267 target_ulong ret
= RTAS_OUT_NOT_SUPPORTED
;
270 case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS
:
271 case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE
:
272 case RTAS_SYSPARM_UUID
:
273 ret
= RTAS_OUT_NOT_AUTHORIZED
;
277 rtas_st(rets
, 0, ret
);
280 static struct rtas_call
{
283 } rtas_table
[RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
];
285 target_ulong
spapr_rtas_call(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
286 uint32_t token
, uint32_t nargs
, target_ulong args
,
287 uint32_t nret
, target_ulong rets
)
289 if ((token
>= RTAS_TOKEN_BASE
) && (token
< RTAS_TOKEN_MAX
)) {
290 struct rtas_call
*call
= rtas_table
+ (token
- RTAS_TOKEN_BASE
);
293 call
->fn(cpu
, spapr
, token
, nargs
, args
, nret
, rets
);
298 /* HACK: Some Linux early debug code uses RTAS display-character,
299 * but assumes the token value is 0xa (which it is on some real
300 * machines) without looking it up in the device tree. This
301 * special case makes this work */
303 rtas_display_character(cpu
, spapr
, 0xa, nargs
, args
, nret
, rets
);
307 hcall_dprintf("Unknown RTAS token 0x%x\n", token
);
308 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
312 void spapr_rtas_register(int token
, const char *name
, spapr_rtas_fn fn
)
314 if (!((token
>= RTAS_TOKEN_BASE
) && (token
< RTAS_TOKEN_MAX
))) {
315 fprintf(stderr
, "RTAS invalid token 0x%x\n", token
);
319 token
-= RTAS_TOKEN_BASE
;
320 if (rtas_table
[token
].name
) {
321 fprintf(stderr
, "RTAS call \"%s\" is registered already as 0x%x\n",
322 rtas_table
[token
].name
, token
);
326 rtas_table
[token
].name
= name
;
327 rtas_table
[token
].fn
= fn
;
330 int spapr_rtas_device_tree_setup(void *fdt
, hwaddr rtas_addr
,
336 ret
= fdt_add_mem_rsv(fdt
, rtas_addr
, rtas_size
);
338 fprintf(stderr
, "Couldn't add RTAS reserve entry: %s\n",
343 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", "linux,rtas-base",
346 fprintf(stderr
, "Couldn't add linux,rtas-base property: %s\n",
351 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", "linux,rtas-entry",
354 fprintf(stderr
, "Couldn't add linux,rtas-entry property: %s\n",
359 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", "rtas-size",
362 fprintf(stderr
, "Couldn't add rtas-size property: %s\n",
367 for (i
= 0; i
< RTAS_TOKEN_MAX
- RTAS_TOKEN_BASE
; i
++) {
368 struct rtas_call
*call
= &rtas_table
[i
];
374 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", call
->name
,
375 i
+ RTAS_TOKEN_BASE
);
377 fprintf(stderr
, "Couldn't add rtas token for %s: %s\n",
378 call
->name
, fdt_strerror(ret
));
386 static void core_rtas_register_types(void)
388 spapr_rtas_register(RTAS_DISPLAY_CHARACTER
, "display-character",
389 rtas_display_character
);
390 spapr_rtas_register(RTAS_GET_TIME_OF_DAY
, "get-time-of-day",
391 rtas_get_time_of_day
);
392 spapr_rtas_register(RTAS_SET_TIME_OF_DAY
, "set-time-of-day",
393 rtas_set_time_of_day
);
394 spapr_rtas_register(RTAS_POWER_OFF
, "power-off", rtas_power_off
);
395 spapr_rtas_register(RTAS_SYSTEM_REBOOT
, "system-reboot",
397 spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE
, "query-cpu-stopped-state",
398 rtas_query_cpu_stopped_state
);
399 spapr_rtas_register(RTAS_START_CPU
, "start-cpu", rtas_start_cpu
);
400 spapr_rtas_register(RTAS_STOP_SELF
, "stop-self", rtas_stop_self
);
401 spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER
,
402 "ibm,get-system-parameter",
403 rtas_ibm_get_system_parameter
);
404 spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER
,
405 "ibm,set-system-parameter",
406 rtas_ibm_set_system_parameter
);
409 type_init(core_rtas_register_types
)