xref: /qemu/hw/ppc/spapr_rtas.c (revision fe6b6346e997ff2ecef1e69a0ab9b1e521b68003)
139ac8455SDavid Gibson /*
239ac8455SDavid Gibson  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
339ac8455SDavid Gibson  *
439ac8455SDavid Gibson  * Hypercall based emulated RTAS
539ac8455SDavid Gibson  *
639ac8455SDavid Gibson  * Copyright (c) 2010-2011 David Gibson, IBM Corporation.
739ac8455SDavid Gibson  *
839ac8455SDavid Gibson  * Permission is hereby granted, free of charge, to any person obtaining a copy
939ac8455SDavid Gibson  * of this software and associated documentation files (the "Software"), to deal
1039ac8455SDavid Gibson  * in the Software without restriction, including without limitation the rights
1139ac8455SDavid Gibson  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1239ac8455SDavid Gibson  * copies of the Software, and to permit persons to whom the Software is
1339ac8455SDavid Gibson  * furnished to do so, subject to the following conditions:
1439ac8455SDavid Gibson  *
1539ac8455SDavid Gibson  * The above copyright notice and this permission notice shall be included in
1639ac8455SDavid Gibson  * all copies or substantial portions of the Software.
1739ac8455SDavid Gibson  *
1839ac8455SDavid Gibson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1939ac8455SDavid Gibson  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2039ac8455SDavid Gibson  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2139ac8455SDavid Gibson  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2239ac8455SDavid Gibson  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2339ac8455SDavid Gibson  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2439ac8455SDavid Gibson  * THE SOFTWARE.
2539ac8455SDavid Gibson  *
2639ac8455SDavid Gibson  */
270d75590dSPeter Maydell #include "qemu/osdep.h"
2839ac8455SDavid Gibson #include "cpu.h"
2903dd024fSPaolo Bonzini #include "qemu/log.h"
30ce9863b7SCédric Le Goater #include "qemu/error-report.h"
319c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
3239ac8455SDavid Gibson #include "hw/qdev.h"
339c17d615SPaolo Bonzini #include "sysemu/device_tree.h"
34db4ef288SBharata B Rao #include "sysemu/cpus.h"
35cf116ad4SDavid Gibson #include "sysemu/hw_accel.h"
36a84f7179SNikunj A Dadhania #include "kvm_ppc.h"
3739ac8455SDavid Gibson 
380d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
390d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
40eeddd59fSLaurent Vivier #include "hw/ppc/spapr_rtas.h"
4184369f63SDavid Gibson #include "hw/ppc/spapr_cpu_core.h"
42af81cf32SBharata B Rao #include "hw/ppc/ppc.h"
43e3943228SSam Bobroff #include "hw/boards.h"
4439ac8455SDavid Gibson 
4539ac8455SDavid Gibson #include <libfdt.h>
468c8639dfSMike Day #include "hw/ppc/spapr_drc.h"
47f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
48028ec3ceSLaurent Vivier #include "trace.h"
493f5dabceSDavid Gibson #include "hw/ppc/fdt.h"
50cf116ad4SDavid Gibson #include "target/ppc/mmu-hash64.h"
51f00bed95SDavid Gibson #include "target/ppc/mmu-book3s-v3.h"
528c8639dfSMike Day 
53ce2918cbSDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, SpaprMachineState *spapr,
54821303f5SDavid Gibson                                    uint32_t token, uint32_t nargs,
55821303f5SDavid Gibson                                    target_ulong args,
56821303f5SDavid Gibson                                    uint32_t nret, target_ulong rets)
57821303f5SDavid Gibson {
58821303f5SDavid Gibson     uint8_t c = rtas_ld(args, 0);
59ce2918cbSDavid Gibson     SpaprVioDevice *sdev = vty_lookup(spapr, 0);
60821303f5SDavid Gibson 
61821303f5SDavid Gibson     if (!sdev) {
62a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
63821303f5SDavid Gibson     } else {
64821303f5SDavid Gibson         vty_putchars(sdev, &c, sizeof(c));
65a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
66821303f5SDavid Gibson     }
67821303f5SDavid Gibson }
68821303f5SDavid Gibson 
69ce2918cbSDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, SpaprMachineState *spapr,
70821303f5SDavid Gibson                            uint32_t token, uint32_t nargs, target_ulong args,
71821303f5SDavid Gibson                            uint32_t nret, target_ulong rets)
72821303f5SDavid Gibson {
73821303f5SDavid Gibson     if (nargs != 2 || nret != 1) {
74a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
75821303f5SDavid Gibson         return;
76821303f5SDavid Gibson     }
77cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
788a9c1b77SThomas Huth     cpu_stop_current();
79a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
80821303f5SDavid Gibson }
81821303f5SDavid Gibson 
82ce2918cbSDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, SpaprMachineState *spapr,
83c821a43cSDavid Gibson                                uint32_t token, uint32_t nargs,
84c821a43cSDavid Gibson                                target_ulong args,
85c821a43cSDavid Gibson                                uint32_t nret, target_ulong rets)
86c821a43cSDavid Gibson {
87c821a43cSDavid Gibson     if (nargs != 0 || nret != 1) {
88a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
89c821a43cSDavid Gibson         return;
90c821a43cSDavid Gibson     }
91cf83f140SEric Blake     qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
92a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
93c821a43cSDavid Gibson }
94c821a43cSDavid Gibson 
95210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
96ce2918cbSDavid Gibson                                          SpaprMachineState *spapr,
97a9f8ad8fSDavid Gibson                                          uint32_t token, uint32_t nargs,
98a9f8ad8fSDavid Gibson                                          target_ulong args,
99a9f8ad8fSDavid Gibson                                          uint32_t nret, target_ulong rets)
100a9f8ad8fSDavid Gibson {
101a9f8ad8fSDavid Gibson     target_ulong id;
1020f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
103a9f8ad8fSDavid Gibson 
104a9f8ad8fSDavid Gibson     if (nargs != 1 || nret != 2) {
105a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
106a9f8ad8fSDavid Gibson         return;
107a9f8ad8fSDavid Gibson     }
108a9f8ad8fSDavid Gibson 
109a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
1102e886fb3SSam Bobroff     cpu = spapr_find_cpu(id);
11105318a85SAndreas Färber     if (cpu != NULL) {
1120f20ba62SAlexey Kardashevskiy         if (CPU(cpu)->halted) {
113a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 0);
114a9f8ad8fSDavid Gibson         } else {
115a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 2);
116a9f8ad8fSDavid Gibson         }
117a9f8ad8fSDavid Gibson 
118a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
119a9f8ad8fSDavid Gibson         return;
120a9f8ad8fSDavid Gibson     }
121a9f8ad8fSDavid Gibson 
122a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
123a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
124a9f8ad8fSDavid Gibson }
125a9f8ad8fSDavid Gibson 
126ce2918cbSDavid Gibson static void rtas_start_cpu(PowerPCCPU *callcpu, SpaprMachineState *spapr,
127a9f8ad8fSDavid Gibson                            uint32_t token, uint32_t nargs,
128a9f8ad8fSDavid Gibson                            target_ulong args,
129a9f8ad8fSDavid Gibson                            uint32_t nret, target_ulong rets)
130a9f8ad8fSDavid Gibson {
131a9f8ad8fSDavid Gibson     target_ulong id, start, r3;
132cf116ad4SDavid Gibson     PowerPCCPU *newcpu;
133cf116ad4SDavid Gibson     CPUPPCState *env;
134cf116ad4SDavid Gibson     PowerPCCPUClass *pcc;
13598248918SDavid Gibson     target_ulong lpcr;
136a9f8ad8fSDavid Gibson 
137a9f8ad8fSDavid Gibson     if (nargs != 3 || nret != 1) {
138a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
139a9f8ad8fSDavid Gibson         return;
140a9f8ad8fSDavid Gibson     }
141a9f8ad8fSDavid Gibson 
142a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
143a9f8ad8fSDavid Gibson     start = rtas_ld(args, 1);
144a9f8ad8fSDavid Gibson     r3 = rtas_ld(args, 2);
145a9f8ad8fSDavid Gibson 
146cf116ad4SDavid Gibson     newcpu = spapr_find_cpu(id);
147cf116ad4SDavid Gibson     if (!newcpu) {
148cf116ad4SDavid Gibson         /* Didn't find a matching cpu */
149cf116ad4SDavid Gibson         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
150cf116ad4SDavid Gibson         return;
151cf116ad4SDavid Gibson     }
152c08d7424SAndreas Färber 
153cf116ad4SDavid Gibson     env = &newcpu->env;
154cf116ad4SDavid Gibson     pcc = POWERPC_CPU_GET_CLASS(newcpu);
155cf116ad4SDavid Gibson 
156cf116ad4SDavid Gibson     if (!CPU(newcpu)->halted) {
157a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
158a9f8ad8fSDavid Gibson         return;
159a9f8ad8fSDavid Gibson     }
160a9f8ad8fSDavid Gibson 
161cf116ad4SDavid Gibson     cpu_synchronize_state(CPU(newcpu));
162048706d9SDavid Gibson 
163a9f8ad8fSDavid Gibson     env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
16498248918SDavid Gibson 
1659a94ee5bSCédric Le Goater     /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
16647a9b551SDavid Gibson     lpcr = env->spr[SPR_LPCR];
16798248918SDavid Gibson     if (!pcc->interrupts_big_endian(callcpu)) {
16898248918SDavid Gibson         lpcr |= LPCR_ILE;
16998248918SDavid Gibson     }
170f00bed95SDavid Gibson     if (env->mmu_model == POWERPC_MMU_3_00) {
171f00bed95SDavid Gibson         /*
172f00bed95SDavid Gibson          * New cpus are expected to start in the same radix/hash mode
173f00bed95SDavid Gibson          * as the existing CPUs
174f00bed95SDavid Gibson          */
17500fd075eSBenjamin Herrenschmidt         if (ppc64_v3_radix(callcpu)) {
17600fd075eSBenjamin Herrenschmidt             lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
177f00bed95SDavid Gibson         } else {
17800fd075eSBenjamin Herrenschmidt             lpcr &= ~(LPCR_UPRT | LPCR_GTSE | LPCR_HR);
179f00bed95SDavid Gibson         }
18070de0967SSuraj Jitindar Singh         env->spr[SPR_PSSCR] &= ~PSSCR_EC;
181f00bed95SDavid Gibson     }
18298248918SDavid Gibson     ppc_store_lpcr(newcpu, lpcr);
18398248918SDavid Gibson 
18498248918SDavid Gibson     /*
18598248918SDavid Gibson      * Set the timebase offset of the new CPU to that of the invoking
18698248918SDavid Gibson      * CPU.  This helps hotplugged CPU to have the correct timebase
18798248918SDavid Gibson      * offset.
18898248918SDavid Gibson      */
18998248918SDavid Gibson     newcpu->env.tb_env->tb_offset = callcpu->env.tb_env->tb_offset;
1909a94ee5bSCédric Le Goater 
19184369f63SDavid Gibson     spapr_cpu_set_entry_state(newcpu, start, r3);
192cf116ad4SDavid Gibson 
193cf116ad4SDavid Gibson     qemu_cpu_kick(CPU(newcpu));
194a9f8ad8fSDavid Gibson 
195a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
196a9f8ad8fSDavid Gibson }
197a9f8ad8fSDavid Gibson 
198ce2918cbSDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, SpaprMachineState *spapr,
19959760f2dSAlexey Kardashevskiy                            uint32_t token, uint32_t nargs,
20059760f2dSAlexey Kardashevskiy                            target_ulong args,
20159760f2dSAlexey Kardashevskiy                            uint32_t nret, target_ulong rets)
20259760f2dSAlexey Kardashevskiy {
20359760f2dSAlexey Kardashevskiy     CPUState *cs = CPU(cpu);
20459760f2dSAlexey Kardashevskiy     CPUPPCState *env = &cpu->env;
2059a94ee5bSCédric Le Goater     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
20659760f2dSAlexey Kardashevskiy 
2079a94ee5bSCédric Le Goater     /* Disable Power-saving mode Exit Cause exceptions for the CPU.
2089a94ee5bSCédric Le Goater      * This could deliver an interrupt on a dying CPU and crash the
20970de0967SSuraj Jitindar Singh      * guest.
21070de0967SSuraj Jitindar Singh      * For the same reason, set PSSCR_EC.
21170de0967SSuraj Jitindar Singh      */
212cf116ad4SDavid Gibson     ppc_store_lpcr(cpu, env->spr[SPR_LPCR] & ~pcc->lpcr_pm);
21370de0967SSuraj Jitindar Singh     env->spr[SPR_PSSCR] |= PSSCR_EC;
214cf116ad4SDavid Gibson     cs->halted = 1;
215a84f7179SNikunj A Dadhania     kvmppc_set_reg_ppc_online(cpu, 0);
216cf116ad4SDavid Gibson     qemu_cpu_kick(cs);
21759760f2dSAlexey Kardashevskiy }
21859760f2dSAlexey Kardashevskiy 
219c920f7b4SDavid Gibson static inline int sysparm_st(target_ulong addr, target_ulong len,
220c920f7b4SDavid Gibson                              const void *val, uint16_t vallen)
221c920f7b4SDavid Gibson {
222c920f7b4SDavid Gibson     hwaddr phys = ppc64_phys_to_real(addr);
223c920f7b4SDavid Gibson 
224c920f7b4SDavid Gibson     if (len < 2) {
225c920f7b4SDavid Gibson         return RTAS_OUT_SYSPARM_PARAM_ERROR;
226c920f7b4SDavid Gibson     }
227c920f7b4SDavid Gibson     stw_be_phys(&address_space_memory, phys, vallen);
228c920f7b4SDavid Gibson     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
229c920f7b4SDavid Gibson     return RTAS_OUT_SUCCESS;
230c920f7b4SDavid Gibson }
231c920f7b4SDavid Gibson 
2323ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
233ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
2343ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2353ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2363ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2373ada6b11SAlexey Kardashevskiy {
238*fe6b6346SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
239*fe6b6346SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
2403ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2413ada6b11SAlexey Kardashevskiy     target_ulong buffer = rtas_ld(args, 1);
2423ada6b11SAlexey Kardashevskiy     target_ulong length = rtas_ld(args, 2);
243c920f7b4SDavid Gibson     target_ulong ret;
2443ada6b11SAlexey Kardashevskiy 
2453ada6b11SAlexey Kardashevskiy     switch (parameter) {
2463b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
247e3943228SSam Bobroff         char *param_val = g_strdup_printf("MaxEntCap=%d,"
248ab3dd749SPhilippe Mathieu-Daudé                                           "DesMem=%" PRIu64 ","
249e3943228SSam Bobroff                                           "DesProcs=%d,"
250e3943228SSam Bobroff                                           "MaxPlatProcs=%d",
251e3943228SSam Bobroff                                           max_cpus,
252d23b6caaSPhilippe Mathieu-Daudé                                           current_machine->ram_size / MiB,
253*fe6b6346SLike Xu                                           ms->smp.cpus,
254e3943228SSam Bobroff                                           max_cpus);
255c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
2563b50d897SSam bobroff         g_free(param_val);
2573b50d897SSam bobroff         break;
2583b50d897SSam bobroff     }
2593052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
2603052d951SSam bobroff         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
2613052d951SSam bobroff 
262c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
2633ada6b11SAlexey Kardashevskiy         break;
2643ada6b11SAlexey Kardashevskiy     }
265b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2669c5ce8dbSFam Zheng         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
2679c5ce8dbSFam Zheng                          (qemu_uuid_set ? 16 : 0));
268b907d7b0SSam bobroff         break;
2693052d951SSam bobroff     default:
2703052d951SSam bobroff         ret = RTAS_OUT_NOT_SUPPORTED;
2713052d951SSam bobroff     }
2723ada6b11SAlexey Kardashevskiy 
2733ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
2743ada6b11SAlexey Kardashevskiy }
2753ada6b11SAlexey Kardashevskiy 
2763ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
277ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
2783ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2793ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2803ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2813ada6b11SAlexey Kardashevskiy {
2823ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2833ada6b11SAlexey Kardashevskiy     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
2843ada6b11SAlexey Kardashevskiy 
2853ada6b11SAlexey Kardashevskiy     switch (parameter) {
2863b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
2873052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
288b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2893ada6b11SAlexey Kardashevskiy         ret = RTAS_OUT_NOT_AUTHORIZED;
2903ada6b11SAlexey Kardashevskiy         break;
2913ada6b11SAlexey Kardashevskiy     }
2923ada6b11SAlexey Kardashevskiy 
2933ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
2943ada6b11SAlexey Kardashevskiy }
2953ada6b11SAlexey Kardashevskiy 
2962e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu,
297ce2918cbSDavid Gibson                             SpaprMachineState *spapr,
2982e14072fSNikunj A Dadhania                             uint32_t token, uint32_t nargs,
2992e14072fSNikunj A Dadhania                             target_ulong args,
3002e14072fSNikunj A Dadhania                             uint32_t nret, target_ulong rets)
3012e14072fSNikunj A Dadhania {
3022c553477SDavid Gibson     qemu_system_guest_panicked(NULL);
3032e14072fSNikunj A Dadhania 
3042c553477SDavid Gibson     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
3052e14072fSNikunj A Dadhania }
3062e14072fSNikunj A Dadhania 
307ce2918cbSDavid Gibson static void rtas_set_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
308094d2058SNathan Fontenot                                  uint32_t token, uint32_t nargs,
309094d2058SNathan Fontenot                                  target_ulong args, uint32_t nret,
310094d2058SNathan Fontenot                                  target_ulong rets)
311094d2058SNathan Fontenot {
312094d2058SNathan Fontenot     int32_t power_domain;
313094d2058SNathan Fontenot 
314094d2058SNathan Fontenot     if (nargs != 2 || nret != 2) {
315094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
316094d2058SNathan Fontenot         return;
317094d2058SNathan Fontenot     }
318094d2058SNathan Fontenot 
319094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
320094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
321094d2058SNathan Fontenot      */
322094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
323094d2058SNathan Fontenot     if (power_domain != -1) {
324094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
325094d2058SNathan Fontenot         return;
326094d2058SNathan Fontenot     }
327094d2058SNathan Fontenot 
328094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
329094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
330094d2058SNathan Fontenot }
331094d2058SNathan Fontenot 
332ce2918cbSDavid Gibson static void rtas_get_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
333094d2058SNathan Fontenot                                   uint32_t token, uint32_t nargs,
334094d2058SNathan Fontenot                                   target_ulong args, uint32_t nret,
335094d2058SNathan Fontenot                                   target_ulong rets)
336094d2058SNathan Fontenot {
337094d2058SNathan Fontenot     int32_t power_domain;
338094d2058SNathan Fontenot 
339094d2058SNathan Fontenot     if (nargs != 1 || nret != 2) {
340094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
341094d2058SNathan Fontenot         return;
342094d2058SNathan Fontenot     }
343094d2058SNathan Fontenot 
344094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
345094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
346094d2058SNathan Fontenot      */
347094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
348094d2058SNathan Fontenot     if (power_domain != -1) {
349094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
350094d2058SNathan Fontenot         return;
351094d2058SNathan Fontenot     }
352094d2058SNathan Fontenot 
353094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
354094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
355094d2058SNathan Fontenot }
356094d2058SNathan Fontenot 
35739ac8455SDavid Gibson static struct rtas_call {
35839ac8455SDavid Gibson     const char *name;
35939ac8455SDavid Gibson     spapr_rtas_fn fn;
3603a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
36139ac8455SDavid Gibson 
362ce2918cbSDavid Gibson target_ulong spapr_rtas_call(PowerPCCPU *cpu, SpaprMachineState *spapr,
36339ac8455SDavid Gibson                              uint32_t token, uint32_t nargs, target_ulong args,
36439ac8455SDavid Gibson                              uint32_t nret, target_ulong rets)
36539ac8455SDavid Gibson {
3663a3b8502SAlexey Kardashevskiy     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
3673a3b8502SAlexey Kardashevskiy         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
36839ac8455SDavid Gibson 
36939ac8455SDavid Gibson         if (call->fn) {
370210b580bSAnthony Liguori             call->fn(cpu, spapr, token, nargs, args, nret, rets);
37139ac8455SDavid Gibson             return H_SUCCESS;
37239ac8455SDavid Gibson         }
37339ac8455SDavid Gibson     }
37439ac8455SDavid Gibson 
375821303f5SDavid Gibson     /* HACK: Some Linux early debug code uses RTAS display-character,
376821303f5SDavid Gibson      * but assumes the token value is 0xa (which it is on some real
377821303f5SDavid Gibson      * machines) without looking it up in the device tree.  This
378821303f5SDavid Gibson      * special case makes this work */
379821303f5SDavid Gibson     if (token == 0xa) {
380210b580bSAnthony Liguori         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
381821303f5SDavid Gibson         return H_SUCCESS;
382821303f5SDavid Gibson     }
383821303f5SDavid Gibson 
38439ac8455SDavid Gibson     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
385a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
38639ac8455SDavid Gibson     return H_PARAMETER;
38739ac8455SDavid Gibson }
38839ac8455SDavid Gibson 
389eeddd59fSLaurent Vivier uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
390eeddd59fSLaurent Vivier                          uint32_t nret, uint64_t rets)
391eeddd59fSLaurent Vivier {
392eeddd59fSLaurent Vivier     int token;
393eeddd59fSLaurent Vivier 
394eeddd59fSLaurent Vivier     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
395eeddd59fSLaurent Vivier         if (strcmp(cmd, rtas_table[token].name) == 0) {
396ce2918cbSDavid Gibson             SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
397eeddd59fSLaurent Vivier             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
398eeddd59fSLaurent Vivier 
399eeddd59fSLaurent Vivier             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
400eeddd59fSLaurent Vivier                                  nargs, args, nret, rets);
401eeddd59fSLaurent Vivier             return H_SUCCESS;
402eeddd59fSLaurent Vivier         }
403eeddd59fSLaurent Vivier     }
404eeddd59fSLaurent Vivier     return H_PARAMETER;
405eeddd59fSLaurent Vivier }
406eeddd59fSLaurent Vivier 
4073a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
40839ac8455SDavid Gibson {
409adf9ac50SDavid Gibson     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
4103a3b8502SAlexey Kardashevskiy 
4113a3b8502SAlexey Kardashevskiy     token -= RTAS_TOKEN_BASE;
412adf9ac50SDavid Gibson 
41364db6c70SCédric Le Goater     assert(!name || !rtas_table[token].name);
414c89d5299SDavid Gibson 
4153a3b8502SAlexey Kardashevskiy     rtas_table[token].name = name;
4163a3b8502SAlexey Kardashevskiy     rtas_table[token].fn = fn;
41739ac8455SDavid Gibson }
41839ac8455SDavid Gibson 
4193f5dabceSDavid Gibson void spapr_dt_rtas_tokens(void *fdt, int rtas)
42039ac8455SDavid Gibson {
42139ac8455SDavid Gibson     int i;
42239ac8455SDavid Gibson 
4233a3b8502SAlexey Kardashevskiy     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
42439ac8455SDavid Gibson         struct rtas_call *call = &rtas_table[i];
42539ac8455SDavid Gibson 
426d36b66f7SBen Herrenschmidt         if (!call->name) {
42739ac8455SDavid Gibson             continue;
42839ac8455SDavid Gibson         }
42939ac8455SDavid Gibson 
4303f5dabceSDavid Gibson         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
43139ac8455SDavid Gibson     }
43239ac8455SDavid Gibson }
433821303f5SDavid Gibson 
434ce2918cbSDavid Gibson void spapr_load_rtas(SpaprMachineState *spapr, void *fdt, hwaddr addr)
4352cac78c1SDavid Gibson {
4362cac78c1SDavid Gibson     int rtas_node;
4372cac78c1SDavid Gibson     int ret;
4382cac78c1SDavid Gibson 
4392cac78c1SDavid Gibson     /* Copy RTAS blob into guest RAM */
4402cac78c1SDavid Gibson     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
4412cac78c1SDavid Gibson 
4422cac78c1SDavid Gibson     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
4432cac78c1SDavid Gibson     if (ret < 0) {
4442cac78c1SDavid Gibson         error_report("Couldn't add RTAS reserve entry: %s",
4452cac78c1SDavid Gibson                      fdt_strerror(ret));
4462cac78c1SDavid Gibson         exit(1);
4472cac78c1SDavid Gibson     }
4482cac78c1SDavid Gibson 
4492cac78c1SDavid Gibson     /* Update the device tree with the blob's location */
4502cac78c1SDavid Gibson     rtas_node = fdt_path_offset(fdt, "/rtas");
4512cac78c1SDavid Gibson     assert(rtas_node >= 0);
4522cac78c1SDavid Gibson 
4532cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
4542cac78c1SDavid Gibson     if (ret < 0) {
4552cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-base property: %s",
4562cac78c1SDavid Gibson                      fdt_strerror(ret));
4572cac78c1SDavid Gibson         exit(1);
4582cac78c1SDavid Gibson     }
4592cac78c1SDavid Gibson 
4602cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
4612cac78c1SDavid Gibson     if (ret < 0) {
4622cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-entry property: %s",
4632cac78c1SDavid Gibson                      fdt_strerror(ret));
4642cac78c1SDavid Gibson         exit(1);
4652cac78c1SDavid Gibson     }
4662cac78c1SDavid Gibson 
4672cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
4682cac78c1SDavid Gibson     if (ret < 0) {
4692cac78c1SDavid Gibson         error_report("Couldn't add rtas-size property: %s",
4702cac78c1SDavid Gibson                      fdt_strerror(ret));
4712cac78c1SDavid Gibson         exit(1);
4722cac78c1SDavid Gibson     }
4732cac78c1SDavid Gibson }
4742cac78c1SDavid Gibson 
47583f7d43aSAndreas Färber static void core_rtas_register_types(void)
476821303f5SDavid Gibson {
4773a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
4783a3b8502SAlexey Kardashevskiy                         rtas_display_character);
4793a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
4803a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
4813a3b8502SAlexey Kardashevskiy                         rtas_system_reboot);
4823a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
483a9f8ad8fSDavid Gibson                         rtas_query_cpu_stopped_state);
4843a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
4853a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
4863a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
4873a3b8502SAlexey Kardashevskiy                         "ibm,get-system-parameter",
4883ada6b11SAlexey Kardashevskiy                         rtas_ibm_get_system_parameter);
4893a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
4903a3b8502SAlexey Kardashevskiy                         "ibm,set-system-parameter",
4913ada6b11SAlexey Kardashevskiy                         rtas_ibm_set_system_parameter);
4922e14072fSNikunj A Dadhania     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
4932e14072fSNikunj A Dadhania                         rtas_ibm_os_term);
494094d2058SNathan Fontenot     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
495094d2058SNathan Fontenot                         rtas_set_power_level);
496094d2058SNathan Fontenot     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
497094d2058SNathan Fontenot                         rtas_get_power_level);
498821303f5SDavid Gibson }
49983f7d43aSAndreas Färber 
50083f7d43aSAndreas Färber type_init(core_rtas_register_types)
501