xref: /openbmc/qemu/hw/ppc/spapr_rtas.c (revision 98376843)
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 "qemu/osdep.h"
28 #include "cpu.h"
29 #include "qemu/log.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/sysemu.h"
32 #include "hw/qdev.h"
33 #include "sysemu/device_tree.h"
34 #include "sysemu/cpus.h"
35 #include "sysemu/kvm.h"
36 
37 #include "hw/ppc/spapr.h"
38 #include "hw/ppc/spapr_vio.h"
39 #include "hw/ppc/spapr_rtas.h"
40 #include "hw/ppc/ppc.h"
41 #include "qapi-event.h"
42 #include "hw/boards.h"
43 
44 #include <libfdt.h>
45 #include "hw/ppc/spapr_drc.h"
46 #include "qemu/cutils.h"
47 #include "trace.h"
48 #include "hw/ppc/fdt.h"
49 
50 static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr,
51                                    uint32_t token, uint32_t nargs,
52                                    target_ulong args,
53                                    uint32_t nret, target_ulong rets)
54 {
55     uint8_t c = rtas_ld(args, 0);
56     VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
57 
58     if (!sdev) {
59         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
60     } else {
61         vty_putchars(sdev, &c, sizeof(c));
62         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
63     }
64 }
65 
66 static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
67                            uint32_t token, uint32_t nargs, target_ulong args,
68                            uint32_t nret, target_ulong rets)
69 {
70     if (nargs != 2 || nret != 1) {
71         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
72         return;
73     }
74     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
75     cpu_stop_current();
76     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
77 }
78 
79 static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr,
80                                uint32_t token, uint32_t nargs,
81                                target_ulong args,
82                                uint32_t nret, target_ulong rets)
83 {
84     if (nargs != 0 || nret != 1) {
85         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
86         return;
87     }
88     qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
89     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
90 }
91 
92 static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
93                                          sPAPRMachineState *spapr,
94                                          uint32_t token, uint32_t nargs,
95                                          target_ulong args,
96                                          uint32_t nret, target_ulong rets)
97 {
98     target_ulong id;
99     PowerPCCPU *cpu;
100 
101     if (nargs != 1 || nret != 2) {
102         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
103         return;
104     }
105 
106     id = rtas_ld(args, 0);
107     cpu = spapr_find_cpu(id);
108     if (cpu != NULL) {
109         if (CPU(cpu)->halted) {
110             rtas_st(rets, 1, 0);
111         } else {
112             rtas_st(rets, 1, 2);
113         }
114 
115         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
116         return;
117     }
118 
119     /* Didn't find a matching cpu */
120     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
121 }
122 
123 /*
124  * Set the timebase offset of the CPU to that of first CPU.
125  * This helps hotplugged CPU to have the correct timebase offset.
126  */
127 static void spapr_cpu_update_tb_offset(PowerPCCPU *cpu)
128 {
129     PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
130 
131     cpu->env.tb_env->tb_offset = fcpu->env.tb_env->tb_offset;
132 }
133 
134 static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
135 {
136     PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
137     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
138 
139     if (!pcc->interrupts_big_endian(fcpu)) {
140         cpu->env.spr[SPR_LPCR] |= LPCR_ILE;
141     }
142 }
143 
144 static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr,
145                            uint32_t token, uint32_t nargs,
146                            target_ulong args,
147                            uint32_t nret, target_ulong rets)
148 {
149     target_ulong id, start, r3;
150     PowerPCCPU *cpu;
151 
152     if (nargs != 3 || nret != 1) {
153         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
154         return;
155     }
156 
157     id = rtas_ld(args, 0);
158     start = rtas_ld(args, 1);
159     r3 = rtas_ld(args, 2);
160 
161     cpu = spapr_find_cpu(id);
162     if (cpu != NULL) {
163         CPUState *cs = CPU(cpu);
164         CPUPPCState *env = &cpu->env;
165         PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
166         Error *local_err = NULL;
167 
168         if (!cs->halted) {
169             rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
170             return;
171         }
172 
173         /* This will make sure qemu state is up to date with kvm, and
174          * mark it dirty so our changes get flushed back before the
175          * new cpu enters */
176         kvm_cpu_synchronize_state(cs);
177 
178         /* Set compatibility mode to match existing cpus */
179         ppc_set_compat(cpu, POWERPC_CPU(first_cpu)->compat_pvr, &local_err);
180         if (local_err) {
181             error_report_err(local_err);
182             rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
183             return;
184         }
185 
186         env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
187 
188         /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
189         env->spr[SPR_LPCR] |= pcc->lpcr_pm;
190 
191         env->nip = start;
192         env->gpr[3] = r3;
193         cs->halted = 0;
194         spapr_cpu_set_endianness(cpu);
195         spapr_cpu_update_tb_offset(cpu);
196 
197         qemu_cpu_kick(cs);
198 
199         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
200         return;
201     }
202 
203     /* Didn't find a matching cpu */
204     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
205 }
206 
207 static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
208                            uint32_t token, uint32_t nargs,
209                            target_ulong args,
210                            uint32_t nret, target_ulong rets)
211 {
212     CPUState *cs = CPU(cpu);
213     CPUPPCState *env = &cpu->env;
214     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
215 
216     cs->halted = 1;
217     qemu_cpu_kick(cs);
218 
219     /* Disable Power-saving mode Exit Cause exceptions for the CPU.
220      * This could deliver an interrupt on a dying CPU and crash the
221      * guest */
222     env->spr[SPR_LPCR] &= ~pcc->lpcr_pm;
223 }
224 
225 static inline int sysparm_st(target_ulong addr, target_ulong len,
226                              const void *val, uint16_t vallen)
227 {
228     hwaddr phys = ppc64_phys_to_real(addr);
229 
230     if (len < 2) {
231         return RTAS_OUT_SYSPARM_PARAM_ERROR;
232     }
233     stw_be_phys(&address_space_memory, phys, vallen);
234     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
235     return RTAS_OUT_SUCCESS;
236 }
237 
238 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
239                                           sPAPRMachineState *spapr,
240                                           uint32_t token, uint32_t nargs,
241                                           target_ulong args,
242                                           uint32_t nret, target_ulong rets)
243 {
244     target_ulong parameter = rtas_ld(args, 0);
245     target_ulong buffer = rtas_ld(args, 1);
246     target_ulong length = rtas_ld(args, 2);
247     target_ulong ret;
248 
249     switch (parameter) {
250     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
251         char *param_val = g_strdup_printf("MaxEntCap=%d,"
252                                           "DesMem=%llu,"
253                                           "DesProcs=%d,"
254                                           "MaxPlatProcs=%d",
255                                           max_cpus,
256                                           current_machine->ram_size / M_BYTE,
257                                           smp_cpus,
258                                           max_cpus);
259         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
260         g_free(param_val);
261         break;
262     }
263     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
264         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
265 
266         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
267         break;
268     }
269     case RTAS_SYSPARM_UUID:
270         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
271                          (qemu_uuid_set ? 16 : 0));
272         break;
273     default:
274         ret = RTAS_OUT_NOT_SUPPORTED;
275     }
276 
277     rtas_st(rets, 0, ret);
278 }
279 
280 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
281                                           sPAPRMachineState *spapr,
282                                           uint32_t token, uint32_t nargs,
283                                           target_ulong args,
284                                           uint32_t nret, target_ulong rets)
285 {
286     target_ulong parameter = rtas_ld(args, 0);
287     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
288 
289     switch (parameter) {
290     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
291     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
292     case RTAS_SYSPARM_UUID:
293         ret = RTAS_OUT_NOT_AUTHORIZED;
294         break;
295     }
296 
297     rtas_st(rets, 0, ret);
298 }
299 
300 static void rtas_ibm_os_term(PowerPCCPU *cpu,
301                             sPAPRMachineState *spapr,
302                             uint32_t token, uint32_t nargs,
303                             target_ulong args,
304                             uint32_t nret, target_ulong rets)
305 {
306     qemu_system_guest_panicked(NULL);
307 
308     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
309 }
310 
311 static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
312                                  uint32_t token, uint32_t nargs,
313                                  target_ulong args, uint32_t nret,
314                                  target_ulong rets)
315 {
316     int32_t power_domain;
317 
318     if (nargs != 2 || nret != 2) {
319         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
320         return;
321     }
322 
323     /* we currently only use a single, "live insert" powerdomain for
324      * hotplugged/dlpar'd resources, so the power is always live/full (100)
325      */
326     power_domain = rtas_ld(args, 0);
327     if (power_domain != -1) {
328         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
329         return;
330     }
331 
332     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
333     rtas_st(rets, 1, 100);
334 }
335 
336 static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
337                                   uint32_t token, uint32_t nargs,
338                                   target_ulong args, uint32_t nret,
339                                   target_ulong rets)
340 {
341     int32_t power_domain;
342 
343     if (nargs != 1 || nret != 2) {
344         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
345         return;
346     }
347 
348     /* we currently only use a single, "live insert" powerdomain for
349      * hotplugged/dlpar'd resources, so the power is always live/full (100)
350      */
351     power_domain = rtas_ld(args, 0);
352     if (power_domain != -1) {
353         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
354         return;
355     }
356 
357     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
358     rtas_st(rets, 1, 100);
359 }
360 
361 static struct rtas_call {
362     const char *name;
363     spapr_rtas_fn fn;
364 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
365 
366 target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
367                              uint32_t token, uint32_t nargs, target_ulong args,
368                              uint32_t nret, target_ulong rets)
369 {
370     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
371         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
372 
373         if (call->fn) {
374             call->fn(cpu, spapr, token, nargs, args, nret, rets);
375             return H_SUCCESS;
376         }
377     }
378 
379     /* HACK: Some Linux early debug code uses RTAS display-character,
380      * but assumes the token value is 0xa (which it is on some real
381      * machines) without looking it up in the device tree.  This
382      * special case makes this work */
383     if (token == 0xa) {
384         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
385         return H_SUCCESS;
386     }
387 
388     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
389     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
390     return H_PARAMETER;
391 }
392 
393 uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
394                          uint32_t nret, uint64_t rets)
395 {
396     int token;
397 
398     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
399         if (strcmp(cmd, rtas_table[token].name) == 0) {
400             sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
401             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
402 
403             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
404                                  nargs, args, nret, rets);
405             return H_SUCCESS;
406         }
407     }
408     return H_PARAMETER;
409 }
410 
411 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
412 {
413     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
414 
415     token -= RTAS_TOKEN_BASE;
416 
417     assert(!rtas_table[token].name);
418 
419     rtas_table[token].name = name;
420     rtas_table[token].fn = fn;
421 }
422 
423 void spapr_dt_rtas_tokens(void *fdt, int rtas)
424 {
425     int i;
426 
427     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
428         struct rtas_call *call = &rtas_table[i];
429 
430         if (!call->name) {
431             continue;
432         }
433 
434         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
435     }
436 }
437 
438 void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr)
439 {
440     int rtas_node;
441     int ret;
442 
443     /* Copy RTAS blob into guest RAM */
444     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
445 
446     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
447     if (ret < 0) {
448         error_report("Couldn't add RTAS reserve entry: %s",
449                      fdt_strerror(ret));
450         exit(1);
451     }
452 
453     /* Update the device tree with the blob's location */
454     rtas_node = fdt_path_offset(fdt, "/rtas");
455     assert(rtas_node >= 0);
456 
457     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
458     if (ret < 0) {
459         error_report("Couldn't add linux,rtas-base property: %s",
460                      fdt_strerror(ret));
461         exit(1);
462     }
463 
464     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
465     if (ret < 0) {
466         error_report("Couldn't add linux,rtas-entry property: %s",
467                      fdt_strerror(ret));
468         exit(1);
469     }
470 
471     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
472     if (ret < 0) {
473         error_report("Couldn't add rtas-size property: %s",
474                      fdt_strerror(ret));
475         exit(1);
476     }
477 }
478 
479 static void core_rtas_register_types(void)
480 {
481     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
482                         rtas_display_character);
483     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
484     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
485                         rtas_system_reboot);
486     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
487                         rtas_query_cpu_stopped_state);
488     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
489     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
490     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
491                         "ibm,get-system-parameter",
492                         rtas_ibm_get_system_parameter);
493     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
494                         "ibm,set-system-parameter",
495                         rtas_ibm_set_system_parameter);
496     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
497                         rtas_ibm_os_term);
498     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
499                         rtas_set_power_level);
500     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
501                         rtas_get_power_level);
502 }
503 
504 type_init(core_rtas_register_types)
505