]> git.proxmox.com Git - qemu.git/blame - hw/ppc/spapr_rtas.c
Merge remote-tracking branch 'mjt/trivial-patches-next' into staging
[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
821303f5
DG
41static void rtas_display_character(sPAPREnvironment *spapr,
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) {
50 rtas_st(rets, 0, -1);
51 } else {
52 vty_putchars(sdev, &c, sizeof(c));
53 rtas_st(rets, 0, 0);
54 }
55}
56
57static void rtas_get_time_of_day(sPAPREnvironment *spapr,
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) {
65 rtas_st(rets, 0, -3);
66 return;
67 }
68
ac26f8c3 69 qemu_get_timedate(&tm, spapr->rtc_offset);
821303f5
DG
70
71 rtas_st(rets, 0, 0); /* 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 */
79}
80
ac26f8c3
BL
81static void rtas_set_time_of_day(sPAPREnvironment *spapr,
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
99 rtas_st(rets, 0, 0); /* Success */
100}
101
821303f5
DG
102static void rtas_power_off(sPAPREnvironment *spapr,
103 uint32_t token, uint32_t nargs, target_ulong args,
104 uint32_t nret, target_ulong rets)
105{
106 if (nargs != 2 || nret != 1) {
107 rtas_st(rets, 0, -3);
108 return;
109 }
110 qemu_system_shutdown_request();
111 rtas_st(rets, 0, 0);
112}
113
c821a43c
DG
114static void rtas_system_reboot(sPAPREnvironment *spapr,
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) {
120 rtas_st(rets, 0, -3);
121 return;
122 }
123 qemu_system_reset_request();
124 rtas_st(rets, 0, 0);
125}
126
a9f8ad8f
DG
127static void rtas_query_cpu_stopped_state(sPAPREnvironment *spapr,
128 uint32_t token, uint32_t nargs,
129 target_ulong args,
130 uint32_t nret, target_ulong rets)
131{
132 target_ulong id;
55e5c285 133 CPUState *cpu;
a9f8ad8f
DG
134
135 if (nargs != 1 || nret != 2) {
136 rtas_st(rets, 0, -3);
137 return;
138 }
139
140 id = rtas_ld(args, 0);
05318a85
AF
141 cpu = qemu_get_cpu(id);
142 if (cpu != NULL) {
259186a7 143 if (cpu->halted) {
a9f8ad8f
DG
144 rtas_st(rets, 1, 0);
145 } else {
146 rtas_st(rets, 1, 2);
147 }
148
149 rtas_st(rets, 0, 0);
150 return;
151 }
152
153 /* Didn't find a matching cpu */
154 rtas_st(rets, 0, -3);
155}
156
157static void rtas_start_cpu(sPAPREnvironment *spapr,
158 uint32_t token, uint32_t nargs,
159 target_ulong args,
160 uint32_t nret, target_ulong rets)
161{
162 target_ulong id, start, r3;
c67e216b 163 CPUState *cs;
a9f8ad8f
DG
164
165 if (nargs != 3 || nret != 1) {
166 rtas_st(rets, 0, -3);
167 return;
168 }
169
170 id = rtas_ld(args, 0);
171 start = rtas_ld(args, 1);
172 r3 = rtas_ld(args, 2);
173
c67e216b
AF
174 cs = qemu_get_cpu(id);
175 if (cs != NULL) {
176 PowerPCCPU *cpu = POWERPC_CPU(cs);
177 CPUPPCState *env = &cpu->env;
a9f8ad8f 178
c67e216b 179 if (!cs->halted) {
a9f8ad8f
DG
180 rtas_st(rets, 0, -1);
181 return;
182 }
183
048706d9
DG
184 /* This will make sure qemu state is up to date with kvm, and
185 * mark it dirty so our changes get flushed back before the
186 * new cpu enters */
187 kvm_cpu_synchronize_state(env);
188
a9f8ad8f
DG
189 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
190 env->nip = start;
191 env->gpr[3] = r3;
c67e216b 192 cs->halted = 0;
a9f8ad8f 193
c67e216b 194 qemu_cpu_kick(cs);
a9f8ad8f
DG
195
196 rtas_st(rets, 0, 0);
197 return;
198 }
199
200 /* Didn't find a matching cpu */
201 rtas_st(rets, 0, -3);
202}
203
39ac8455
DG
204static struct rtas_call {
205 const char *name;
206 spapr_rtas_fn fn;
207} rtas_table[TOKEN_MAX];
208
209struct rtas_call *rtas_next = rtas_table;
210
211target_ulong spapr_rtas_call(sPAPREnvironment *spapr,
212 uint32_t token, uint32_t nargs, target_ulong args,
213 uint32_t nret, target_ulong rets)
214{
215 if ((token >= TOKEN_BASE)
216 && ((token - TOKEN_BASE) < TOKEN_MAX)) {
217 struct rtas_call *call = rtas_table + (token - TOKEN_BASE);
218
219 if (call->fn) {
220 call->fn(spapr, token, nargs, args, nret, rets);
221 return H_SUCCESS;
222 }
223 }
224
821303f5
DG
225 /* HACK: Some Linux early debug code uses RTAS display-character,
226 * but assumes the token value is 0xa (which it is on some real
227 * machines) without looking it up in the device tree. This
228 * special case makes this work */
229 if (token == 0xa) {
230 rtas_display_character(spapr, 0xa, nargs, args, nret, rets);
231 return H_SUCCESS;
232 }
233
39ac8455
DG
234 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
235 rtas_st(rets, 0, -3);
236 return H_PARAMETER;
237}
238
4aac82c3 239int spapr_rtas_register(const char *name, spapr_rtas_fn fn)
39ac8455 240{
c89d5299
DG
241 int i;
242
243 for (i = 0; i < (rtas_next - rtas_table); i++) {
244 if (strcmp(name, rtas_table[i].name) == 0) {
245 fprintf(stderr, "RTAS call \"%s\" registered twice\n", name);
246 exit(1);
247 }
248 }
249
39ac8455
DG
250 assert(rtas_next < (rtas_table + TOKEN_MAX));
251
252 rtas_next->name = name;
253 rtas_next->fn = fn;
254
4aac82c3 255 return (rtas_next++ - rtas_table) + TOKEN_BASE;
39ac8455
DG
256}
257
a8170e5e
AK
258int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
259 hwaddr rtas_size)
39ac8455
DG
260{
261 int ret;
262 int i;
263
264 ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
265 if (ret < 0) {
266 fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
267 fdt_strerror(ret));
268 return ret;
269 }
270
271 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-base",
272 rtas_addr);
273 if (ret < 0) {
274 fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
275 fdt_strerror(ret));
276 return ret;
277 }
278
279 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
280 rtas_addr);
281 if (ret < 0) {
282 fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
283 fdt_strerror(ret));
284 return ret;
285 }
286
287 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "rtas-size",
288 rtas_size);
289 if (ret < 0) {
290 fprintf(stderr, "Couldn't add rtas-size property: %s\n",
291 fdt_strerror(ret));
292 return ret;
293 }
294
295 for (i = 0; i < TOKEN_MAX; i++) {
296 struct rtas_call *call = &rtas_table[i];
297
d36b66f7 298 if (!call->name) {
39ac8455
DG
299 continue;
300 }
301
302 ret = qemu_devtree_setprop_cell(fdt, "/rtas", call->name,
303 i + TOKEN_BASE);
304 if (ret < 0) {
305 fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
306 call->name, fdt_strerror(ret));
307 return ret;
308 }
309
310 }
311 return 0;
312}
821303f5 313
83f7d43a 314static void core_rtas_register_types(void)
821303f5
DG
315{
316 spapr_rtas_register("display-character", rtas_display_character);
317 spapr_rtas_register("get-time-of-day", rtas_get_time_of_day);
ac26f8c3 318 spapr_rtas_register("set-time-of-day", rtas_set_time_of_day);
821303f5 319 spapr_rtas_register("power-off", rtas_power_off);
c821a43c 320 spapr_rtas_register("system-reboot", rtas_system_reboot);
a9f8ad8f
DG
321 spapr_rtas_register("query-cpu-stopped-state",
322 rtas_query_cpu_stopped_state);
323 spapr_rtas_register("start-cpu", rtas_start_cpu);
821303f5 324}
83f7d43a
AF
325
326type_init(core_rtas_register_types)