xref: /openbmc/qemu/hw/ppc/spapr_rtas.c (revision f764718d)
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 
167         if (!cs->halted) {
168             rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
169             return;
170         }
171 
172         /* This will make sure qemu state is up to date with kvm, and
173          * mark it dirty so our changes get flushed back before the
174          * new cpu enters */
175         kvm_cpu_synchronize_state(cs);
176 
177         env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
178 
179         /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
180         env->spr[SPR_LPCR] |= pcc->lpcr_pm;
181 
182         env->nip = start;
183         env->gpr[3] = r3;
184         cs->halted = 0;
185         spapr_cpu_set_endianness(cpu);
186         spapr_cpu_update_tb_offset(cpu);
187 
188         qemu_cpu_kick(cs);
189 
190         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
191         return;
192     }
193 
194     /* Didn't find a matching cpu */
195     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
196 }
197 
198 static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
199                            uint32_t token, uint32_t nargs,
200                            target_ulong args,
201                            uint32_t nret, target_ulong rets)
202 {
203     CPUState *cs = CPU(cpu);
204     CPUPPCState *env = &cpu->env;
205     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
206 
207     cs->halted = 1;
208     qemu_cpu_kick(cs);
209 
210     /* Disable Power-saving mode Exit Cause exceptions for the CPU.
211      * This could deliver an interrupt on a dying CPU and crash the
212      * guest */
213     env->spr[SPR_LPCR] &= ~pcc->lpcr_pm;
214 }
215 
216 static inline int sysparm_st(target_ulong addr, target_ulong len,
217                              const void *val, uint16_t vallen)
218 {
219     hwaddr phys = ppc64_phys_to_real(addr);
220 
221     if (len < 2) {
222         return RTAS_OUT_SYSPARM_PARAM_ERROR;
223     }
224     stw_be_phys(&address_space_memory, phys, vallen);
225     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
226     return RTAS_OUT_SUCCESS;
227 }
228 
229 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
230                                           sPAPRMachineState *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;
239 
240     switch (parameter) {
241     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
242         char *param_val = g_strdup_printf("MaxEntCap=%d,"
243                                           "DesMem=%llu,"
244                                           "DesProcs=%d,"
245                                           "MaxPlatProcs=%d",
246                                           max_cpus,
247                                           current_machine->ram_size / M_BYTE,
248                                           smp_cpus,
249                                           max_cpus);
250         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
251         g_free(param_val);
252         break;
253     }
254     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
255         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
256 
257         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
258         break;
259     }
260     case RTAS_SYSPARM_UUID:
261         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
262                          (qemu_uuid_set ? 16 : 0));
263         break;
264     default:
265         ret = RTAS_OUT_NOT_SUPPORTED;
266     }
267 
268     rtas_st(rets, 0, ret);
269 }
270 
271 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
272                                           sPAPRMachineState *spapr,
273                                           uint32_t token, uint32_t nargs,
274                                           target_ulong args,
275                                           uint32_t nret, target_ulong rets)
276 {
277     target_ulong parameter = rtas_ld(args, 0);
278     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
279 
280     switch (parameter) {
281     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
282     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
283     case RTAS_SYSPARM_UUID:
284         ret = RTAS_OUT_NOT_AUTHORIZED;
285         break;
286     }
287 
288     rtas_st(rets, 0, ret);
289 }
290 
291 static void rtas_ibm_os_term(PowerPCCPU *cpu,
292                             sPAPRMachineState *spapr,
293                             uint32_t token, uint32_t nargs,
294                             target_ulong args,
295                             uint32_t nret, target_ulong rets)
296 {
297     qemu_system_guest_panicked(NULL);
298 
299     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
300 }
301 
302 static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
303                                  uint32_t token, uint32_t nargs,
304                                  target_ulong args, uint32_t nret,
305                                  target_ulong rets)
306 {
307     int32_t power_domain;
308 
309     if (nargs != 2 || nret != 2) {
310         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
311         return;
312     }
313 
314     /* we currently only use a single, "live insert" powerdomain for
315      * hotplugged/dlpar'd resources, so the power is always live/full (100)
316      */
317     power_domain = rtas_ld(args, 0);
318     if (power_domain != -1) {
319         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
320         return;
321     }
322 
323     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
324     rtas_st(rets, 1, 100);
325 }
326 
327 static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
328                                   uint32_t token, uint32_t nargs,
329                                   target_ulong args, uint32_t nret,
330                                   target_ulong rets)
331 {
332     int32_t power_domain;
333 
334     if (nargs != 1 || nret != 2) {
335         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
336         return;
337     }
338 
339     /* we currently only use a single, "live insert" powerdomain for
340      * hotplugged/dlpar'd resources, so the power is always live/full (100)
341      */
342     power_domain = rtas_ld(args, 0);
343     if (power_domain != -1) {
344         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
345         return;
346     }
347 
348     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
349     rtas_st(rets, 1, 100);
350 }
351 
352 static struct rtas_call {
353     const char *name;
354     spapr_rtas_fn fn;
355 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
356 
357 target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
358                              uint32_t token, uint32_t nargs, target_ulong args,
359                              uint32_t nret, target_ulong rets)
360 {
361     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
362         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
363 
364         if (call->fn) {
365             call->fn(cpu, spapr, token, nargs, args, nret, rets);
366             return H_SUCCESS;
367         }
368     }
369 
370     /* HACK: Some Linux early debug code uses RTAS display-character,
371      * but assumes the token value is 0xa (which it is on some real
372      * machines) without looking it up in the device tree.  This
373      * special case makes this work */
374     if (token == 0xa) {
375         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
376         return H_SUCCESS;
377     }
378 
379     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
380     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
381     return H_PARAMETER;
382 }
383 
384 uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
385                          uint32_t nret, uint64_t rets)
386 {
387     int token;
388 
389     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
390         if (strcmp(cmd, rtas_table[token].name) == 0) {
391             sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
392             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
393 
394             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
395                                  nargs, args, nret, rets);
396             return H_SUCCESS;
397         }
398     }
399     return H_PARAMETER;
400 }
401 
402 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
403 {
404     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
405 
406     token -= RTAS_TOKEN_BASE;
407 
408     assert(!rtas_table[token].name);
409 
410     rtas_table[token].name = name;
411     rtas_table[token].fn = fn;
412 }
413 
414 void spapr_dt_rtas_tokens(void *fdt, int rtas)
415 {
416     int i;
417 
418     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
419         struct rtas_call *call = &rtas_table[i];
420 
421         if (!call->name) {
422             continue;
423         }
424 
425         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
426     }
427 }
428 
429 void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr)
430 {
431     int rtas_node;
432     int ret;
433 
434     /* Copy RTAS blob into guest RAM */
435     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
436 
437     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
438     if (ret < 0) {
439         error_report("Couldn't add RTAS reserve entry: %s",
440                      fdt_strerror(ret));
441         exit(1);
442     }
443 
444     /* Update the device tree with the blob's location */
445     rtas_node = fdt_path_offset(fdt, "/rtas");
446     assert(rtas_node >= 0);
447 
448     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
449     if (ret < 0) {
450         error_report("Couldn't add linux,rtas-base property: %s",
451                      fdt_strerror(ret));
452         exit(1);
453     }
454 
455     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
456     if (ret < 0) {
457         error_report("Couldn't add linux,rtas-entry property: %s",
458                      fdt_strerror(ret));
459         exit(1);
460     }
461 
462     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
463     if (ret < 0) {
464         error_report("Couldn't add rtas-size property: %s",
465                      fdt_strerror(ret));
466         exit(1);
467     }
468 }
469 
470 static void core_rtas_register_types(void)
471 {
472     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
473                         rtas_display_character);
474     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
475     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
476                         rtas_system_reboot);
477     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
478                         rtas_query_cpu_stopped_state);
479     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
480     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
481     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
482                         "ibm,get-system-parameter",
483                         rtas_ibm_get_system_parameter);
484     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
485                         "ibm,set-system-parameter",
486                         rtas_ibm_set_system_parameter);
487     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
488                         rtas_ibm_os_term);
489     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
490                         rtas_set_power_level);
491     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
492                         rtas_get_power_level);
493 }
494 
495 type_init(core_rtas_register_types)
496