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"
38 #define TOKEN_BASE 0x2000
39 #define TOKEN_MAX 0x100
41 static void rtas_display_character(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
42 uint32_t token
, uint32_t nargs
,
44 uint32_t nret
, target_ulong rets
)
46 uint8_t c
= rtas_ld(args
, 0);
47 VIOsPAPRDevice
*sdev
= vty_lookup(spapr
, 0);
50 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
52 vty_putchars(sdev
, &c
, sizeof(c
));
53 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
57 static void rtas_get_time_of_day(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
58 uint32_t token
, uint32_t nargs
,
60 uint32_t nret
, target_ulong rets
)
65 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
69 qemu_get_timedate(&tm
, spapr
->rtc_offset
);
71 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
72 rtas_st(rets
, 1, tm
.tm_year
+ 1900);
73 rtas_st(rets
, 2, tm
.tm_mon
+ 1);
74 rtas_st(rets
, 3, tm
.tm_mday
);
75 rtas_st(rets
, 4, tm
.tm_hour
);
76 rtas_st(rets
, 5, tm
.tm_min
);
77 rtas_st(rets
, 6, tm
.tm_sec
);
78 rtas_st(rets
, 7, 0); /* we don't do nanoseconds */
81 static void rtas_set_time_of_day(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
82 uint32_t token
, uint32_t nargs
,
84 uint32_t nret
, target_ulong rets
)
88 tm
.tm_year
= rtas_ld(args
, 0) - 1900;
89 tm
.tm_mon
= rtas_ld(args
, 1) - 1;
90 tm
.tm_mday
= rtas_ld(args
, 2);
91 tm
.tm_hour
= rtas_ld(args
, 3);
92 tm
.tm_min
= rtas_ld(args
, 4);
93 tm
.tm_sec
= rtas_ld(args
, 5);
95 /* Just generate a monitor event for the change */
96 rtc_change_mon_event(&tm
);
97 spapr
->rtc_offset
= qemu_timedate_diff(&tm
);
99 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
102 static void rtas_power_off(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
103 uint32_t token
, uint32_t nargs
, target_ulong args
,
104 uint32_t nret
, target_ulong rets
)
106 if (nargs
!= 2 || nret
!= 1) {
107 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
110 qemu_system_shutdown_request();
111 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
114 static void rtas_system_reboot(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
115 uint32_t token
, uint32_t nargs
,
117 uint32_t nret
, target_ulong rets
)
119 if (nargs
!= 0 || nret
!= 1) {
120 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
123 qemu_system_reset_request();
124 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
127 static void rtas_query_cpu_stopped_state(PowerPCCPU
*cpu_
,
128 sPAPREnvironment
*spapr
,
129 uint32_t token
, uint32_t nargs
,
131 uint32_t nret
, target_ulong rets
)
136 if (nargs
!= 1 || nret
!= 2) {
137 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
141 id
= rtas_ld(args
, 0);
142 cpu
= qemu_get_cpu(id
);
150 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
154 /* Didn't find a matching cpu */
155 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
158 static void rtas_start_cpu(PowerPCCPU
*cpu_
, sPAPREnvironment
*spapr
,
159 uint32_t token
, uint32_t nargs
,
161 uint32_t nret
, target_ulong rets
)
163 target_ulong id
, start
, r3
;
166 if (nargs
!= 3 || nret
!= 1) {
167 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
171 id
= rtas_ld(args
, 0);
172 start
= rtas_ld(args
, 1);
173 r3
= rtas_ld(args
, 2);
175 cs
= qemu_get_cpu(id
);
177 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
178 CPUPPCState
*env
= &cpu
->env
;
181 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
185 /* This will make sure qemu state is up to date with kvm, and
186 * mark it dirty so our changes get flushed back before the
188 kvm_cpu_synchronize_state(cs
);
190 env
->msr
= (1ULL << MSR_SF
) | (1ULL << MSR_ME
);
197 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
201 /* Didn't find a matching cpu */
202 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
205 static void rtas_stop_self(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
206 uint32_t token
, uint32_t nargs
,
208 uint32_t nret
, target_ulong rets
)
210 CPUState
*cs
= CPU(cpu
);
211 CPUPPCState
*env
= &cpu
->env
;
216 * While stopping a CPU, the guest calls H_CPPR which
217 * effectively disables interrupts on XICS level.
218 * However decrementer interrupts in TCG can still
219 * wake the CPU up so here we disable interrupts in MSR
221 * As rtas_start_cpu() resets the whole MSR anyway, there is
222 * no need to bother with specific bits, we just clear it.
227 #define DIAGNOSTICS_RUN_MODE 42
229 static void rtas_ibm_get_system_parameter(PowerPCCPU
*cpu
,
230 sPAPREnvironment
*spapr
,
231 uint32_t token
, uint32_t nargs
,
233 uint32_t nret
, target_ulong rets
)
235 target_ulong parameter
= rtas_ld(args
, 0);
236 target_ulong buffer
= rtas_ld(args
, 1);
237 target_ulong length
= rtas_ld(args
, 2);
238 target_ulong ret
= RTAS_OUT_NOT_SUPPORTED
;
241 case DIAGNOSTICS_RUN_MODE
:
243 rtas_st(buffer
, 0, 0);
244 ret
= RTAS_OUT_SUCCESS
;
249 rtas_st(rets
, 0, ret
);
252 static void rtas_ibm_set_system_parameter(PowerPCCPU
*cpu
,
253 sPAPREnvironment
*spapr
,
254 uint32_t token
, uint32_t nargs
,
256 uint32_t nret
, target_ulong rets
)
258 target_ulong parameter
= rtas_ld(args
, 0);
259 target_ulong ret
= RTAS_OUT_NOT_SUPPORTED
;
262 case DIAGNOSTICS_RUN_MODE
:
263 ret
= RTAS_OUT_NOT_AUTHORIZED
;
267 rtas_st(rets
, 0, ret
);
270 static struct rtas_call
{
273 } rtas_table
[TOKEN_MAX
];
275 struct rtas_call
*rtas_next
= rtas_table
;
277 target_ulong
spapr_rtas_call(PowerPCCPU
*cpu
, sPAPREnvironment
*spapr
,
278 uint32_t token
, uint32_t nargs
, target_ulong args
,
279 uint32_t nret
, target_ulong rets
)
281 if ((token
>= TOKEN_BASE
)
282 && ((token
- TOKEN_BASE
) < TOKEN_MAX
)) {
283 struct rtas_call
*call
= rtas_table
+ (token
- TOKEN_BASE
);
286 call
->fn(cpu
, spapr
, token
, nargs
, args
, nret
, rets
);
291 /* HACK: Some Linux early debug code uses RTAS display-character,
292 * but assumes the token value is 0xa (which it is on some real
293 * machines) without looking it up in the device tree. This
294 * special case makes this work */
296 rtas_display_character(cpu
, spapr
, 0xa, nargs
, args
, nret
, rets
);
300 hcall_dprintf("Unknown RTAS token 0x%x\n", token
);
301 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
305 int spapr_rtas_register(const char *name
, spapr_rtas_fn fn
)
309 for (i
= 0; i
< (rtas_next
- rtas_table
); i
++) {
310 if (strcmp(name
, rtas_table
[i
].name
) == 0) {
311 fprintf(stderr
, "RTAS call \"%s\" registered twice\n", name
);
316 assert(rtas_next
< (rtas_table
+ TOKEN_MAX
));
318 rtas_next
->name
= name
;
321 return (rtas_next
++ - rtas_table
) + TOKEN_BASE
;
324 int spapr_rtas_device_tree_setup(void *fdt
, hwaddr rtas_addr
,
330 ret
= fdt_add_mem_rsv(fdt
, rtas_addr
, rtas_size
);
332 fprintf(stderr
, "Couldn't add RTAS reserve entry: %s\n",
337 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", "linux,rtas-base",
340 fprintf(stderr
, "Couldn't add linux,rtas-base property: %s\n",
345 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", "linux,rtas-entry",
348 fprintf(stderr
, "Couldn't add linux,rtas-entry property: %s\n",
353 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", "rtas-size",
356 fprintf(stderr
, "Couldn't add rtas-size property: %s\n",
361 for (i
= 0; i
< TOKEN_MAX
; i
++) {
362 struct rtas_call
*call
= &rtas_table
[i
];
368 ret
= qemu_fdt_setprop_cell(fdt
, "/rtas", call
->name
,
371 fprintf(stderr
, "Couldn't add rtas token for %s: %s\n",
372 call
->name
, fdt_strerror(ret
));
380 static void core_rtas_register_types(void)
382 spapr_rtas_register("display-character", rtas_display_character
);
383 spapr_rtas_register("get-time-of-day", rtas_get_time_of_day
);
384 spapr_rtas_register("set-time-of-day", rtas_set_time_of_day
);
385 spapr_rtas_register("power-off", rtas_power_off
);
386 spapr_rtas_register("system-reboot", rtas_system_reboot
);
387 spapr_rtas_register("query-cpu-stopped-state",
388 rtas_query_cpu_stopped_state
);
389 spapr_rtas_register("start-cpu", rtas_start_cpu
);
390 spapr_rtas_register("stop-self", rtas_stop_self
);
391 spapr_rtas_register("ibm,get-system-parameter",
392 rtas_ibm_get_system_parameter
);
393 spapr_rtas_register("ibm,set-system-parameter",
394 rtas_ibm_set_system_parameter
);
397 type_init(core_rtas_register_types
)