pci_bus.h: tweak include guards
[qemu/ar7.git] / hw / spapr_rtas.c
blob6d5c48a740062dd0e3fd9c0abd6a1588296faa7a
1 /*
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
24 * THE SOFTWARE.
27 #include "cpu.h"
28 #include "sysemu.h"
29 #include "qemu-char.h"
30 #include "hw/qdev.h"
31 #include "device_tree.h"
33 #include "hw/spapr.h"
34 #include "hw/spapr_vio.h"
36 #include <libfdt.h>
38 #define TOKEN_BASE 0x2000
39 #define TOKEN_MAX 0x100
41 static void rtas_display_character(sPAPREnvironment *spapr,
42 uint32_t token, uint32_t nargs,
43 target_ulong args,
44 uint32_t nret, target_ulong rets)
46 uint8_t c = rtas_ld(args, 0);
47 VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
49 if (!sdev) {
50 rtas_st(rets, 0, -1);
51 } else {
52 vty_putchars(sdev, &c, sizeof(c));
53 rtas_st(rets, 0, 0);
57 static 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)
62 struct tm tm;
64 if (nret != 8) {
65 rtas_st(rets, 0, -3);
66 return;
69 qemu_get_timedate(&tm, spapr->rtc_offset);
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 */
81 static 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)
86 struct tm tm;
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, 0); /* Success */
102 static void rtas_power_off(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, -3);
108 return;
110 qemu_system_shutdown_request();
111 rtas_st(rets, 0, 0);
114 static void rtas_system_reboot(sPAPREnvironment *spapr,
115 uint32_t token, uint32_t nargs,
116 target_ulong args,
117 uint32_t nret, target_ulong rets)
119 if (nargs != 0 || nret != 1) {
120 rtas_st(rets, 0, -3);
121 return;
123 qemu_system_reset_request();
124 rtas_st(rets, 0, 0);
127 static 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)
132 target_ulong id;
133 CPUPPCState *env;
135 if (nargs != 1 || nret != 2) {
136 rtas_st(rets, 0, -3);
137 return;
140 id = rtas_ld(args, 0);
141 for (env = first_cpu; env; env = env->next_cpu) {
142 if (env->cpu_index != id) {
143 continue;
146 if (env->halted) {
147 rtas_st(rets, 1, 0);
148 } else {
149 rtas_st(rets, 1, 2);
152 rtas_st(rets, 0, 0);
153 return;
156 /* Didn't find a matching cpu */
157 rtas_st(rets, 0, -3);
160 static void rtas_start_cpu(sPAPREnvironment *spapr,
161 uint32_t token, uint32_t nargs,
162 target_ulong args,
163 uint32_t nret, target_ulong rets)
165 target_ulong id, start, r3;
166 CPUState *cpu;
167 CPUPPCState *env;
169 if (nargs != 3 || nret != 1) {
170 rtas_st(rets, 0, -3);
171 return;
174 id = rtas_ld(args, 0);
175 start = rtas_ld(args, 1);
176 r3 = rtas_ld(args, 2);
178 for (env = first_cpu; env; env = env->next_cpu) {
179 cpu = ENV_GET_CPU(env);
181 if (env->cpu_index != id) {
182 continue;
185 if (!env->halted) {
186 rtas_st(rets, 0, -1);
187 return;
190 /* This will make sure qemu state is up to date with kvm, and
191 * mark it dirty so our changes get flushed back before the
192 * new cpu enters */
193 kvm_cpu_synchronize_state(env);
195 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
196 env->nip = start;
197 env->gpr[3] = r3;
198 env->halted = 0;
200 qemu_cpu_kick(cpu);
202 rtas_st(rets, 0, 0);
203 return;
206 /* Didn't find a matching cpu */
207 rtas_st(rets, 0, -3);
210 static struct rtas_call {
211 const char *name;
212 spapr_rtas_fn fn;
213 } rtas_table[TOKEN_MAX];
215 struct rtas_call *rtas_next = rtas_table;
217 target_ulong spapr_rtas_call(sPAPREnvironment *spapr,
218 uint32_t token, uint32_t nargs, target_ulong args,
219 uint32_t nret, target_ulong rets)
221 if ((token >= TOKEN_BASE)
222 && ((token - TOKEN_BASE) < TOKEN_MAX)) {
223 struct rtas_call *call = rtas_table + (token - TOKEN_BASE);
225 if (call->fn) {
226 call->fn(spapr, token, nargs, args, nret, rets);
227 return H_SUCCESS;
231 /* HACK: Some Linux early debug code uses RTAS display-character,
232 * but assumes the token value is 0xa (which it is on some real
233 * machines) without looking it up in the device tree. This
234 * special case makes this work */
235 if (token == 0xa) {
236 rtas_display_character(spapr, 0xa, nargs, args, nret, rets);
237 return H_SUCCESS;
240 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
241 rtas_st(rets, 0, -3);
242 return H_PARAMETER;
245 void spapr_rtas_register(const char *name, spapr_rtas_fn fn)
247 int i;
249 for (i = 0; i < (rtas_next - rtas_table); i++) {
250 if (strcmp(name, rtas_table[i].name) == 0) {
251 fprintf(stderr, "RTAS call \"%s\" registered twice\n", name);
252 exit(1);
256 assert(rtas_next < (rtas_table + TOKEN_MAX));
258 rtas_next->name = name;
259 rtas_next->fn = fn;
261 rtas_next++;
264 int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
265 hwaddr rtas_size)
267 int ret;
268 int i;
270 ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
271 if (ret < 0) {
272 fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
273 fdt_strerror(ret));
274 return ret;
277 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-base",
278 rtas_addr);
279 if (ret < 0) {
280 fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
281 fdt_strerror(ret));
282 return ret;
285 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
286 rtas_addr);
287 if (ret < 0) {
288 fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
289 fdt_strerror(ret));
290 return ret;
293 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "rtas-size",
294 rtas_size);
295 if (ret < 0) {
296 fprintf(stderr, "Couldn't add rtas-size property: %s\n",
297 fdt_strerror(ret));
298 return ret;
301 for (i = 0; i < TOKEN_MAX; i++) {
302 struct rtas_call *call = &rtas_table[i];
304 if (!call->fn) {
305 continue;
308 ret = qemu_devtree_setprop_cell(fdt, "/rtas", call->name,
309 i + TOKEN_BASE);
310 if (ret < 0) {
311 fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
312 call->name, fdt_strerror(ret));
313 return ret;
317 return 0;
320 static void core_rtas_register_types(void)
322 spapr_rtas_register("display-character", rtas_display_character);
323 spapr_rtas_register("get-time-of-day", rtas_get_time_of_day);
324 spapr_rtas_register("set-time-of-day", rtas_set_time_of_day);
325 spapr_rtas_register("power-off", rtas_power_off);
326 spapr_rtas_register("system-reboot", rtas_system_reboot);
327 spapr_rtas_register("query-cpu-stopped-state",
328 rtas_query_cpu_stopped_state);
329 spapr_rtas_register("start-cpu", rtas_start_cpu);
332 type_init(core_rtas_register_types)