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