]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr_rtas.c
spapr-rtas: add ibm, (get|set)-system-parameter
[mirror_qemu.git] / hw / ppc / spapr_rtas.c
CommitLineData
39ac8455
DG
1/*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3 *
4 * Hypercall based emulated RTAS
5 *
6 * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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
24 * THE SOFTWARE.
25 *
26 */
27#include "cpu.h"
9c17d615 28#include "sysemu/sysemu.h"
dccfcd0e 29#include "sysemu/char.h"
39ac8455 30#include "hw/qdev.h"
9c17d615 31#include "sysemu/device_tree.h"
39ac8455 32
0d09e41a
PB
33#include "hw/ppc/spapr.h"
34#include "hw/ppc/spapr_vio.h"
39ac8455
DG
35
36#include <libfdt.h>
37
38#define TOKEN_BASE 0x2000
39#define TOKEN_MAX 0x100
40
210b580b 41static void rtas_display_character(PowerPCCPU *cpu, sPAPREnvironment *spapr,
821303f5
DG
42 uint32_t token, uint32_t nargs,
43 target_ulong args,
44 uint32_t nret, target_ulong rets)
45{
46 uint8_t c = rtas_ld(args, 0);
5f2e2ba2 47 VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
821303f5
DG
48
49 if (!sdev) {
a64d325d 50 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
821303f5
DG
51 } else {
52 vty_putchars(sdev, &c, sizeof(c));
a64d325d 53 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
821303f5
DG
54 }
55}
56
210b580b 57static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
821303f5
DG
58 uint32_t token, uint32_t nargs,
59 target_ulong args,
60 uint32_t nret, target_ulong rets)
61{
62 struct tm tm;
63
64 if (nret != 8) {
a64d325d 65 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
821303f5
DG
66 return;
67 }
68
ac26f8c3 69 qemu_get_timedate(&tm, spapr->rtc_offset);
821303f5 70
a64d325d 71 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
821303f5
DG
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 */
79}
80
210b580b 81static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
ac26f8c3
BL
82 uint32_t token, uint32_t nargs,
83 target_ulong args,
84 uint32_t nret, target_ulong rets)
85{
86 struct tm tm;
87
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);
94
95 /* Just generate a monitor event for the change */
96 rtc_change_mon_event(&tm);
97 spapr->rtc_offset = qemu_timedate_diff(&tm);
98
a64d325d 99 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
ac26f8c3
BL
100}
101
210b580b 102static void rtas_power_off(PowerPCCPU *cpu, sPAPREnvironment *spapr,
821303f5
DG
103 uint32_t token, uint32_t nargs, target_ulong args,
104 uint32_t nret, target_ulong rets)
105{
106 if (nargs != 2 || nret != 1) {
a64d325d 107 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
821303f5
DG
108 return;
109 }
110 qemu_system_shutdown_request();
a64d325d 111 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
821303f5
DG
112}
113
210b580b 114static void rtas_system_reboot(PowerPCCPU *cpu, sPAPREnvironment *spapr,
c821a43c
DG
115 uint32_t token, uint32_t nargs,
116 target_ulong args,
117 uint32_t nret, target_ulong rets)
118{
119 if (nargs != 0 || nret != 1) {
a64d325d 120 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
c821a43c
DG
121 return;
122 }
123 qemu_system_reset_request();
a64d325d 124 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
c821a43c
DG
125}
126
210b580b
AL
127static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
128 sPAPREnvironment *spapr,
a9f8ad8f
DG
129 uint32_t token, uint32_t nargs,
130 target_ulong args,
131 uint32_t nret, target_ulong rets)
132{
133 target_ulong id;
55e5c285 134 CPUState *cpu;
a9f8ad8f
DG
135
136 if (nargs != 1 || nret != 2) {
a64d325d 137 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
a9f8ad8f
DG
138 return;
139 }
140
141 id = rtas_ld(args, 0);
05318a85
AF
142 cpu = qemu_get_cpu(id);
143 if (cpu != NULL) {
259186a7 144 if (cpu->halted) {
a9f8ad8f
DG
145 rtas_st(rets, 1, 0);
146 } else {
147 rtas_st(rets, 1, 2);
148 }
149
a64d325d 150 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
a9f8ad8f
DG
151 return;
152 }
153
154 /* Didn't find a matching cpu */
a64d325d 155 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
a9f8ad8f
DG
156}
157
210b580b 158static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
a9f8ad8f
DG
159 uint32_t token, uint32_t nargs,
160 target_ulong args,
161 uint32_t nret, target_ulong rets)
162{
163 target_ulong id, start, r3;
c67e216b 164 CPUState *cs;
a9f8ad8f
DG
165
166 if (nargs != 3 || nret != 1) {
a64d325d 167 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
a9f8ad8f
DG
168 return;
169 }
170
171 id = rtas_ld(args, 0);
172 start = rtas_ld(args, 1);
173 r3 = rtas_ld(args, 2);
174
c67e216b
AF
175 cs = qemu_get_cpu(id);
176 if (cs != NULL) {
177 PowerPCCPU *cpu = POWERPC_CPU(cs);
178 CPUPPCState *env = &cpu->env;
a9f8ad8f 179
c67e216b 180 if (!cs->halted) {
a64d325d 181 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
a9f8ad8f
DG
182 return;
183 }
184
048706d9
DG
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
187 * new cpu enters */
dd1750d7 188 kvm_cpu_synchronize_state(cs);
048706d9 189
a9f8ad8f
DG
190 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
191 env->nip = start;
192 env->gpr[3] = r3;
c67e216b 193 cs->halted = 0;
a9f8ad8f 194
c67e216b 195 qemu_cpu_kick(cs);
a9f8ad8f 196
a64d325d 197 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
a9f8ad8f
DG
198 return;
199 }
200
201 /* Didn't find a matching cpu */
a64d325d 202 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
a9f8ad8f
DG
203}
204
59760f2d
AK
205static void rtas_stop_self(PowerPCCPU *cpu, sPAPREnvironment *spapr,
206 uint32_t token, uint32_t nargs,
207 target_ulong args,
208 uint32_t nret, target_ulong rets)
209{
210 CPUState *cs = CPU(cpu);
211 CPUPPCState *env = &cpu->env;
212
213 cs->halted = 1;
214 cpu_exit(cs);
215 /*
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
220 * as well.
221 * As rtas_start_cpu() resets the whole MSR anyway, there is
222 * no need to bother with specific bits, we just clear it.
223 */
224 env->msr = 0;
225}
226
3ada6b11
AK
227#define DIAGNOSTICS_RUN_MODE 42
228
229static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
230 sPAPREnvironment *spapr,
231 uint32_t token, uint32_t nargs,
232 target_ulong args,
233 uint32_t nret, target_ulong rets)
234{
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;
239
240 switch (parameter) {
241 case DIAGNOSTICS_RUN_MODE:
242 if (length == 1) {
243 rtas_st(buffer, 0, 0);
244 ret = RTAS_OUT_SUCCESS;
245 }
246 break;
247 }
248
249 rtas_st(rets, 0, ret);
250}
251
252static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
253 sPAPREnvironment *spapr,
254 uint32_t token, uint32_t nargs,
255 target_ulong args,
256 uint32_t nret, target_ulong rets)
257{
258 target_ulong parameter = rtas_ld(args, 0);
259 target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
260
261 switch (parameter) {
262 case DIAGNOSTICS_RUN_MODE:
263 ret = RTAS_OUT_NOT_AUTHORIZED;
264 break;
265 }
266
267 rtas_st(rets, 0, ret);
268}
269
39ac8455
DG
270static struct rtas_call {
271 const char *name;
272 spapr_rtas_fn fn;
273} rtas_table[TOKEN_MAX];
274
275struct rtas_call *rtas_next = rtas_table;
276
210b580b 277target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPREnvironment *spapr,
39ac8455
DG
278 uint32_t token, uint32_t nargs, target_ulong args,
279 uint32_t nret, target_ulong rets)
280{
281 if ((token >= TOKEN_BASE)
282 && ((token - TOKEN_BASE) < TOKEN_MAX)) {
283 struct rtas_call *call = rtas_table + (token - TOKEN_BASE);
284
285 if (call->fn) {
210b580b 286 call->fn(cpu, spapr, token, nargs, args, nret, rets);
39ac8455
DG
287 return H_SUCCESS;
288 }
289 }
290
821303f5
DG
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 */
295 if (token == 0xa) {
210b580b 296 rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
821303f5
DG
297 return H_SUCCESS;
298 }
299
39ac8455 300 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
a64d325d 301 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
39ac8455
DG
302 return H_PARAMETER;
303}
304
4aac82c3 305int spapr_rtas_register(const char *name, spapr_rtas_fn fn)
39ac8455 306{
c89d5299
DG
307 int i;
308
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);
312 exit(1);
313 }
314 }
315
39ac8455
DG
316 assert(rtas_next < (rtas_table + TOKEN_MAX));
317
318 rtas_next->name = name;
319 rtas_next->fn = fn;
320
4aac82c3 321 return (rtas_next++ - rtas_table) + TOKEN_BASE;
39ac8455
DG
322}
323
a8170e5e
AK
324int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
325 hwaddr rtas_size)
39ac8455
DG
326{
327 int ret;
328 int i;
329
330 ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
331 if (ret < 0) {
332 fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
333 fdt_strerror(ret));
334 return ret;
335 }
336
337 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-base",
338 rtas_addr);
339 if (ret < 0) {
340 fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
341 fdt_strerror(ret));
342 return ret;
343 }
344
345 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
346 rtas_addr);
347 if (ret < 0) {
348 fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
349 fdt_strerror(ret));
350 return ret;
351 }
352
353 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "rtas-size",
354 rtas_size);
355 if (ret < 0) {
356 fprintf(stderr, "Couldn't add rtas-size property: %s\n",
357 fdt_strerror(ret));
358 return ret;
359 }
360
361 for (i = 0; i < TOKEN_MAX; i++) {
362 struct rtas_call *call = &rtas_table[i];
363
d36b66f7 364 if (!call->name) {
39ac8455
DG
365 continue;
366 }
367
368 ret = qemu_devtree_setprop_cell(fdt, "/rtas", call->name,
369 i + TOKEN_BASE);
370 if (ret < 0) {
371 fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
372 call->name, fdt_strerror(ret));
373 return ret;
374 }
375
376 }
377 return 0;
378}
821303f5 379
83f7d43a 380static void core_rtas_register_types(void)
821303f5
DG
381{
382 spapr_rtas_register("display-character", rtas_display_character);
383 spapr_rtas_register("get-time-of-day", rtas_get_time_of_day);
ac26f8c3 384 spapr_rtas_register("set-time-of-day", rtas_set_time_of_day);
821303f5 385 spapr_rtas_register("power-off", rtas_power_off);
c821a43c 386 spapr_rtas_register("system-reboot", rtas_system_reboot);
a9f8ad8f
DG
387 spapr_rtas_register("query-cpu-stopped-state",
388 rtas_query_cpu_stopped_state);
389 spapr_rtas_register("start-cpu", rtas_start_cpu);
59760f2d 390 spapr_rtas_register("stop-self", rtas_stop_self);
3ada6b11
AK
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);
821303f5 395}
83f7d43a
AF
396
397type_init(core_rtas_register_types)