xref: /openbmc/qemu/hw/ppc/spapr_rtas.c (revision ce7cdebd)
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 void rtas_ibm_suspend_me(PowerPCCPU *cpu, SpaprMachineState *spapr,
221                            uint32_t token, uint32_t nargs,
222                            target_ulong args,
223                            uint32_t nret, target_ulong rets)
224 {
225     CPUState *cs;
226 
227     if (nargs != 0 || nret != 1) {
228         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
229         return;
230     }
231 
232     CPU_FOREACH(cs) {
233         PowerPCCPU *c = POWERPC_CPU(cs);
234         CPUPPCState *e = &c->env;
235         if (c == cpu) {
236             continue;
237         }
238 
239         /* See h_join */
240         if (!cs->halted || (e->msr & (1ULL << MSR_EE))) {
241             rtas_st(rets, 0, H_MULTI_THREADS_ACTIVE);
242             return;
243         }
244     }
245 
246     qemu_system_suspend_request();
247     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
248 }
249 
250 static inline int sysparm_st(target_ulong addr, target_ulong len,
251                              const void *val, uint16_t vallen)
252 {
253     hwaddr phys = ppc64_phys_to_real(addr);
254 
255     if (len < 2) {
256         return RTAS_OUT_SYSPARM_PARAM_ERROR;
257     }
258     stw_be_phys(&address_space_memory, phys, vallen);
259     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
260     return RTAS_OUT_SUCCESS;
261 }
262 
263 static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
264                                           SpaprMachineState *spapr,
265                                           uint32_t token, uint32_t nargs,
266                                           target_ulong args,
267                                           uint32_t nret, target_ulong rets)
268 {
269     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
270     MachineState *ms = MACHINE(spapr);
271     target_ulong parameter = rtas_ld(args, 0);
272     target_ulong buffer = rtas_ld(args, 1);
273     target_ulong length = rtas_ld(args, 2);
274     target_ulong ret;
275 
276     switch (parameter) {
277     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
278         char *param_val = g_strdup_printf("MaxEntCap=%d,"
279                                           "DesMem=%" PRIu64 ","
280                                           "DesProcs=%d,"
281                                           "MaxPlatProcs=%d",
282                                           ms->smp.max_cpus,
283                                           ms->ram_size / MiB,
284                                           ms->smp.cpus,
285                                           ms->smp.max_cpus);
286         if (pcc->n_host_threads > 0) {
287             char *hostthr_val, *old = param_val;
288 
289             /*
290              * Add HostThrs property. This property is not present in PAPR but
291              * is expected by some guests to communicate the number of physical
292              * host threads per core on the system so that they can scale
293              * information which varies based on the thread configuration.
294              */
295             hostthr_val = g_strdup_printf(",HostThrs=%d", pcc->n_host_threads);
296             param_val = g_strconcat(param_val, hostthr_val, NULL);
297             g_free(hostthr_val);
298             g_free(old);
299         }
300         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
301         g_free(param_val);
302         break;
303     }
304     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
305         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
306 
307         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
308         break;
309     }
310     case RTAS_SYSPARM_UUID:
311         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
312                          (qemu_uuid_set ? 16 : 0));
313         break;
314     default:
315         ret = RTAS_OUT_NOT_SUPPORTED;
316     }
317 
318     rtas_st(rets, 0, ret);
319 }
320 
321 static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
322                                           SpaprMachineState *spapr,
323                                           uint32_t token, uint32_t nargs,
324                                           target_ulong args,
325                                           uint32_t nret, target_ulong rets)
326 {
327     target_ulong parameter = rtas_ld(args, 0);
328     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
329 
330     switch (parameter) {
331     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
332     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
333     case RTAS_SYSPARM_UUID:
334         ret = RTAS_OUT_NOT_AUTHORIZED;
335         break;
336     }
337 
338     rtas_st(rets, 0, ret);
339 }
340 
341 static void rtas_ibm_os_term(PowerPCCPU *cpu,
342                             SpaprMachineState *spapr,
343                             uint32_t token, uint32_t nargs,
344                             target_ulong args,
345                             uint32_t nret, target_ulong rets)
346 {
347     qemu_system_guest_panicked(NULL);
348 
349     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
350 }
351 
352 static void rtas_set_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
353                                  uint32_t token, uint32_t nargs,
354                                  target_ulong args, uint32_t nret,
355                                  target_ulong rets)
356 {
357     int32_t power_domain;
358 
359     if (nargs != 2 || nret != 2) {
360         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
361         return;
362     }
363 
364     /* we currently only use a single, "live insert" powerdomain for
365      * hotplugged/dlpar'd resources, so the power is always live/full (100)
366      */
367     power_domain = rtas_ld(args, 0);
368     if (power_domain != -1) {
369         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
370         return;
371     }
372 
373     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
374     rtas_st(rets, 1, 100);
375 }
376 
377 static void rtas_get_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
378                                   uint32_t token, uint32_t nargs,
379                                   target_ulong args, uint32_t nret,
380                                   target_ulong rets)
381 {
382     int32_t power_domain;
383 
384     if (nargs != 1 || nret != 2) {
385         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
386         return;
387     }
388 
389     /* we currently only use a single, "live insert" powerdomain for
390      * hotplugged/dlpar'd resources, so the power is always live/full (100)
391      */
392     power_domain = rtas_ld(args, 0);
393     if (power_domain != -1) {
394         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
395         return;
396     }
397 
398     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
399     rtas_st(rets, 1, 100);
400 }
401 
402 static struct rtas_call {
403     const char *name;
404     spapr_rtas_fn fn;
405 } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
406 
407 target_ulong spapr_rtas_call(PowerPCCPU *cpu, SpaprMachineState *spapr,
408                              uint32_t token, uint32_t nargs, target_ulong args,
409                              uint32_t nret, target_ulong rets)
410 {
411     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
412         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
413 
414         if (call->fn) {
415             call->fn(cpu, spapr, token, nargs, args, nret, rets);
416             return H_SUCCESS;
417         }
418     }
419 
420     /* HACK: Some Linux early debug code uses RTAS display-character,
421      * but assumes the token value is 0xa (which it is on some real
422      * machines) without looking it up in the device tree.  This
423      * special case makes this work */
424     if (token == 0xa) {
425         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
426         return H_SUCCESS;
427     }
428 
429     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
430     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
431     return H_PARAMETER;
432 }
433 
434 uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
435                          uint32_t nret, uint64_t rets)
436 {
437     int token;
438 
439     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
440         if (strcmp(cmd, rtas_table[token].name) == 0) {
441             SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
442             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
443 
444             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
445                                  nargs, args, nret, rets);
446             return H_SUCCESS;
447         }
448     }
449     return H_PARAMETER;
450 }
451 
452 void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
453 {
454     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
455 
456     token -= RTAS_TOKEN_BASE;
457 
458     assert(!name || !rtas_table[token].name);
459 
460     rtas_table[token].name = name;
461     rtas_table[token].fn = fn;
462 }
463 
464 void spapr_dt_rtas_tokens(void *fdt, int rtas)
465 {
466     int i;
467 
468     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
469         struct rtas_call *call = &rtas_table[i];
470 
471         if (!call->name) {
472             continue;
473         }
474 
475         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
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_SUSPEND_ME, "ibm,suspend-me",
491                         rtas_ibm_suspend_me);
492     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
493                         "ibm,get-system-parameter",
494                         rtas_ibm_get_system_parameter);
495     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
496                         "ibm,set-system-parameter",
497                         rtas_ibm_set_system_parameter);
498     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
499                         rtas_ibm_os_term);
500     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
501                         rtas_set_power_level);
502     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
503                         rtas_get_power_level);
504 }
505 
506 type_init(core_rtas_register_types)
507