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