1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "cpu.h"
28#include "sysemu/sysemu.h"
29#include "char/char.h"
30#include "hw/qdev.h"
31#include "sysemu/device_tree.h"
32
33#include "hw/spapr.h"
34#include "hw/spapr_vio.h"
35
36#include <libfdt.h>
37
38#define TOKEN_BASE 0x2000
39#define TOKEN_MAX 0x100
40
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);
47 VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
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
69 qemu_get_timedate(&tm, spapr->rtc_offset);
70
71 rtas_st(rets, 0, 0);
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);
79}
80
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
96 rtc_change_mon_event(&tm);
97 spapr->rtc_offset = qemu_timedate_diff(&tm);
98
99 rtas_st(rets, 0, 0);
100}
101
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
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
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;
133 CPUPPCState *env;
134 CPUState *cpu;
135
136 if (nargs != 1 || nret != 2) {
137 rtas_st(rets, 0, -3);
138 return;
139 }
140
141 id = rtas_ld(args, 0);
142 for (env = first_cpu; env; env = env->next_cpu) {
143 cpu = CPU(ppc_env_get_cpu(env));
144 if (cpu->cpu_index != id) {
145 continue;
146 }
147
148 if (env->halted) {
149 rtas_st(rets, 1, 0);
150 } else {
151 rtas_st(rets, 1, 2);
152 }
153
154 rtas_st(rets, 0, 0);
155 return;
156 }
157
158
159 rtas_st(rets, 0, -3);
160}
161
162static void rtas_start_cpu(sPAPREnvironment *spapr,
163 uint32_t token, uint32_t nargs,
164 target_ulong args,
165 uint32_t nret, target_ulong rets)
166{
167 target_ulong id, start, r3;
168 CPUState *cpu;
169 CPUPPCState *env;
170
171 if (nargs != 3 || nret != 1) {
172 rtas_st(rets, 0, -3);
173 return;
174 }
175
176 id = rtas_ld(args, 0);
177 start = rtas_ld(args, 1);
178 r3 = rtas_ld(args, 2);
179
180 for (env = first_cpu; env; env = env->next_cpu) {
181 cpu = CPU(ppc_env_get_cpu(env));
182
183 if (cpu->cpu_index != id) {
184 continue;
185 }
186
187 if (!env->halted) {
188 rtas_st(rets, 0, -1);
189 return;
190 }
191
192
193
194
195 kvm_cpu_synchronize_state(env);
196
197 env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
198 env->nip = start;
199 env->gpr[3] = r3;
200 env->halted = 0;
201
202 qemu_cpu_kick(cpu);
203
204 rtas_st(rets, 0, 0);
205 return;
206 }
207
208
209 rtas_st(rets, 0, -3);
210}
211
212static struct rtas_call {
213 const char *name;
214 spapr_rtas_fn fn;
215} rtas_table[TOKEN_MAX];
216
217struct rtas_call *rtas_next = rtas_table;
218
219target_ulong spapr_rtas_call(sPAPREnvironment *spapr,
220 uint32_t token, uint32_t nargs, target_ulong args,
221 uint32_t nret, target_ulong rets)
222{
223 if ((token >= TOKEN_BASE)
224 && ((token - TOKEN_BASE) < TOKEN_MAX)) {
225 struct rtas_call *call = rtas_table + (token - TOKEN_BASE);
226
227 if (call->fn) {
228 call->fn(spapr, token, nargs, args, nret, rets);
229 return H_SUCCESS;
230 }
231 }
232
233
234
235
236
237 if (token == 0xa) {
238 rtas_display_character(spapr, 0xa, nargs, args, nret, rets);
239 return H_SUCCESS;
240 }
241
242 hcall_dprintf("Unknown RTAS token 0x%x\n", token);
243 rtas_st(rets, 0, -3);
244 return H_PARAMETER;
245}
246
247int spapr_rtas_register(const char *name, spapr_rtas_fn fn)
248{
249 int i;
250
251 for (i = 0; i < (rtas_next - rtas_table); i++) {
252 if (strcmp(name, rtas_table[i].name) == 0) {
253 fprintf(stderr, "RTAS call \"%s\" registered twice\n", name);
254 exit(1);
255 }
256 }
257
258 assert(rtas_next < (rtas_table + TOKEN_MAX));
259
260 rtas_next->name = name;
261 rtas_next->fn = fn;
262
263 return (rtas_next++ - rtas_table) + TOKEN_BASE;
264}
265
266int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
267 hwaddr rtas_size)
268{
269 int ret;
270 int i;
271
272 ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
273 if (ret < 0) {
274 fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
275 fdt_strerror(ret));
276 return ret;
277 }
278
279 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-base",
280 rtas_addr);
281 if (ret < 0) {
282 fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
283 fdt_strerror(ret));
284 return ret;
285 }
286
287 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
288 rtas_addr);
289 if (ret < 0) {
290 fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
291 fdt_strerror(ret));
292 return ret;
293 }
294
295 ret = qemu_devtree_setprop_cell(fdt, "/rtas", "rtas-size",
296 rtas_size);
297 if (ret < 0) {
298 fprintf(stderr, "Couldn't add rtas-size property: %s\n",
299 fdt_strerror(ret));
300 return ret;
301 }
302
303 for (i = 0; i < TOKEN_MAX; i++) {
304 struct rtas_call *call = &rtas_table[i];
305
306 if (!call->name) {
307 continue;
308 }
309
310 ret = qemu_devtree_setprop_cell(fdt, "/rtas", call->name,
311 i + TOKEN_BASE);
312 if (ret < 0) {
313 fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
314 call->name, fdt_strerror(ret));
315 return ret;
316 }
317
318 }
319 return 0;
320}
321
322static void core_rtas_register_types(void)
323{
324 spapr_rtas_register("display-character", rtas_display_character);
325 spapr_rtas_register("get-time-of-day", rtas_get_time_of_day);
326 spapr_rtas_register("set-time-of-day", rtas_set_time_of_day);
327 spapr_rtas_register("power-off", rtas_power_off);
328 spapr_rtas_register("system-reboot", rtas_system_reboot);
329 spapr_rtas_register("query-cpu-stopped-state",
330 rtas_query_cpu_stopped_state);
331 spapr_rtas_register("start-cpu", rtas_start_cpu);
332}
333
334type_init(core_rtas_register_types)
335