xref: /qemu/hw/ppc/spapr_rtas.c (revision 93eac7b8f44738f7d2f4ba4460d67b04af5b0b99)
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  */
2754d31236SMarkus Armbruster 
280d75590dSPeter Maydell #include "qemu/osdep.h"
2939ac8455SDavid Gibson #include "cpu.h"
3003dd024fSPaolo Bonzini #include "qemu/log.h"
31ce9863b7SCédric Le Goater #include "qemu/error-report.h"
329c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
339c17d615SPaolo Bonzini #include "sysemu/device_tree.h"
34db4ef288SBharata B Rao #include "sysemu/cpus.h"
35cf116ad4SDavid Gibson #include "sysemu/hw_accel.h"
3654d31236SMarkus Armbruster #include "sysemu/runstate.h"
37a84f7179SNikunj A Dadhania #include "kvm_ppc.h"
3839ac8455SDavid Gibson 
390d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
400d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
41eeddd59fSLaurent Vivier #include "hw/ppc/spapr_rtas.h"
4284369f63SDavid Gibson #include "hw/ppc/spapr_cpu_core.h"
43af81cf32SBharata B Rao #include "hw/ppc/ppc.h"
44e3943228SSam Bobroff #include "hw/boards.h"
4539ac8455SDavid Gibson 
4639ac8455SDavid Gibson #include <libfdt.h>
478c8639dfSMike Day #include "hw/ppc/spapr_drc.h"
48f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
49028ec3ceSLaurent Vivier #include "trace.h"
503f5dabceSDavid Gibson #include "hw/ppc/fdt.h"
51cf116ad4SDavid Gibson #include "target/ppc/mmu-hash64.h"
52f00bed95SDavid Gibson #include "target/ppc/mmu-book3s-v3.h"
538c8639dfSMike Day 
54ce2918cbSDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, SpaprMachineState *spapr,
55821303f5SDavid Gibson                                    uint32_t token, uint32_t nargs,
56821303f5SDavid Gibson                                    target_ulong args,
57821303f5SDavid Gibson                                    uint32_t nret, target_ulong rets)
58821303f5SDavid Gibson {
59821303f5SDavid Gibson     uint8_t c = rtas_ld(args, 0);
60ce2918cbSDavid Gibson     SpaprVioDevice *sdev = vty_lookup(spapr, 0);
61821303f5SDavid Gibson 
62821303f5SDavid Gibson     if (!sdev) {
63a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
64821303f5SDavid Gibson     } else {
65821303f5SDavid Gibson         vty_putchars(sdev, &c, sizeof(c));
66a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
67821303f5SDavid Gibson     }
68821303f5SDavid Gibson }
69821303f5SDavid Gibson 
70ce2918cbSDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, SpaprMachineState *spapr,
71821303f5SDavid Gibson                            uint32_t token, uint32_t nargs, target_ulong args,
72821303f5SDavid Gibson                            uint32_t nret, target_ulong rets)
73821303f5SDavid Gibson {
74821303f5SDavid Gibson     if (nargs != 2 || nret != 1) {
75a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
76821303f5SDavid Gibson         return;
77821303f5SDavid Gibson     }
78cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
798a9c1b77SThomas Huth     cpu_stop_current();
80a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
81821303f5SDavid Gibson }
82821303f5SDavid Gibson 
83ce2918cbSDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, SpaprMachineState *spapr,
84c821a43cSDavid Gibson                                uint32_t token, uint32_t nargs,
85c821a43cSDavid Gibson                                target_ulong args,
86c821a43cSDavid Gibson                                uint32_t nret, target_ulong rets)
87c821a43cSDavid Gibson {
88c821a43cSDavid Gibson     if (nargs != 0 || nret != 1) {
89a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
90c821a43cSDavid Gibson         return;
91c821a43cSDavid Gibson     }
92cf83f140SEric Blake     qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
93a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
94c821a43cSDavid Gibson }
95c821a43cSDavid Gibson 
96210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
97ce2918cbSDavid Gibson                                          SpaprMachineState *spapr,
98a9f8ad8fSDavid Gibson                                          uint32_t token, uint32_t nargs,
99a9f8ad8fSDavid Gibson                                          target_ulong args,
100a9f8ad8fSDavid Gibson                                          uint32_t nret, target_ulong rets)
101a9f8ad8fSDavid Gibson {
102a9f8ad8fSDavid Gibson     target_ulong id;
1030f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
104a9f8ad8fSDavid Gibson 
105a9f8ad8fSDavid Gibson     if (nargs != 1 || nret != 2) {
106a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
107a9f8ad8fSDavid Gibson         return;
108a9f8ad8fSDavid Gibson     }
109a9f8ad8fSDavid Gibson 
110a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
1112e886fb3SSam Bobroff     cpu = spapr_find_cpu(id);
11205318a85SAndreas Färber     if (cpu != NULL) {
1130f20ba62SAlexey Kardashevskiy         if (CPU(cpu)->halted) {
114a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 0);
115a9f8ad8fSDavid Gibson         } else {
116a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 2);
117a9f8ad8fSDavid Gibson         }
118a9f8ad8fSDavid Gibson 
119a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
120a9f8ad8fSDavid Gibson         return;
121a9f8ad8fSDavid Gibson     }
122a9f8ad8fSDavid Gibson 
123a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
124a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
125a9f8ad8fSDavid Gibson }
126a9f8ad8fSDavid Gibson 
127ce2918cbSDavid Gibson static void rtas_start_cpu(PowerPCCPU *callcpu, SpaprMachineState *spapr,
128a9f8ad8fSDavid Gibson                            uint32_t token, uint32_t nargs,
129a9f8ad8fSDavid Gibson                            target_ulong args,
130a9f8ad8fSDavid Gibson                            uint32_t nret, target_ulong rets)
131a9f8ad8fSDavid Gibson {
132a9f8ad8fSDavid Gibson     target_ulong id, start, r3;
133cf116ad4SDavid Gibson     PowerPCCPU *newcpu;
134cf116ad4SDavid Gibson     CPUPPCState *env;
135cf116ad4SDavid Gibson     PowerPCCPUClass *pcc;
13698248918SDavid Gibson     target_ulong lpcr;
137a9f8ad8fSDavid Gibson 
138a9f8ad8fSDavid Gibson     if (nargs != 3 || nret != 1) {
139a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
140a9f8ad8fSDavid Gibson         return;
141a9f8ad8fSDavid Gibson     }
142a9f8ad8fSDavid Gibson 
143a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
144a9f8ad8fSDavid Gibson     start = rtas_ld(args, 1);
145a9f8ad8fSDavid Gibson     r3 = rtas_ld(args, 2);
146a9f8ad8fSDavid Gibson 
147cf116ad4SDavid Gibson     newcpu = spapr_find_cpu(id);
148cf116ad4SDavid Gibson     if (!newcpu) {
149cf116ad4SDavid Gibson         /* Didn't find a matching cpu */
150cf116ad4SDavid Gibson         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
151cf116ad4SDavid Gibson         return;
152cf116ad4SDavid Gibson     }
153c08d7424SAndreas Färber 
154cf116ad4SDavid Gibson     env = &newcpu->env;
155cf116ad4SDavid Gibson     pcc = POWERPC_CPU_GET_CLASS(newcpu);
156cf116ad4SDavid Gibson 
157cf116ad4SDavid Gibson     if (!CPU(newcpu)->halted) {
158a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
159a9f8ad8fSDavid Gibson         return;
160a9f8ad8fSDavid Gibson     }
161a9f8ad8fSDavid Gibson 
162cf116ad4SDavid Gibson     cpu_synchronize_state(CPU(newcpu));
163048706d9SDavid Gibson 
164a9f8ad8fSDavid Gibson     env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
16598248918SDavid Gibson 
1669a94ee5bSCédric Le Goater     /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
16747a9b551SDavid Gibson     lpcr = env->spr[SPR_LPCR];
16898248918SDavid Gibson     if (!pcc->interrupts_big_endian(callcpu)) {
16998248918SDavid Gibson         lpcr |= LPCR_ILE;
17098248918SDavid Gibson     }
171f00bed95SDavid Gibson     if (env->mmu_model == POWERPC_MMU_3_00) {
172f00bed95SDavid Gibson         /*
173f00bed95SDavid Gibson          * New cpus are expected to start in the same radix/hash mode
174f00bed95SDavid Gibson          * as the existing CPUs
175f00bed95SDavid Gibson          */
17600fd075eSBenjamin Herrenschmidt         if (ppc64_v3_radix(callcpu)) {
17700fd075eSBenjamin Herrenschmidt             lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
178f00bed95SDavid Gibson         } else {
17900fd075eSBenjamin Herrenschmidt             lpcr &= ~(LPCR_UPRT | LPCR_GTSE | LPCR_HR);
180f00bed95SDavid Gibson         }
18170de0967SSuraj Jitindar Singh         env->spr[SPR_PSSCR] &= ~PSSCR_EC;
182f00bed95SDavid Gibson     }
18398248918SDavid Gibson     ppc_store_lpcr(newcpu, lpcr);
18498248918SDavid Gibson 
18598248918SDavid Gibson     /*
18698248918SDavid Gibson      * Set the timebase offset of the new CPU to that of the invoking
18798248918SDavid Gibson      * CPU.  This helps hotplugged CPU to have the correct timebase
18898248918SDavid Gibson      * offset.
18998248918SDavid Gibson      */
19098248918SDavid Gibson     newcpu->env.tb_env->tb_offset = callcpu->env.tb_env->tb_offset;
1919a94ee5bSCédric Le Goater 
19284369f63SDavid Gibson     spapr_cpu_set_entry_state(newcpu, start, r3);
193cf116ad4SDavid Gibson 
194cf116ad4SDavid Gibson     qemu_cpu_kick(CPU(newcpu));
195a9f8ad8fSDavid Gibson 
196a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
197a9f8ad8fSDavid Gibson }
198a9f8ad8fSDavid Gibson 
199ce2918cbSDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, SpaprMachineState *spapr,
20059760f2dSAlexey Kardashevskiy                            uint32_t token, uint32_t nargs,
20159760f2dSAlexey Kardashevskiy                            target_ulong args,
20259760f2dSAlexey Kardashevskiy                            uint32_t nret, target_ulong rets)
20359760f2dSAlexey Kardashevskiy {
20459760f2dSAlexey Kardashevskiy     CPUState *cs = CPU(cpu);
20559760f2dSAlexey Kardashevskiy     CPUPPCState *env = &cpu->env;
2069a94ee5bSCédric Le Goater     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
20759760f2dSAlexey Kardashevskiy 
2089a94ee5bSCédric Le Goater     /* Disable Power-saving mode Exit Cause exceptions for the CPU.
2099a94ee5bSCédric Le Goater      * This could deliver an interrupt on a dying CPU and crash the
21070de0967SSuraj Jitindar Singh      * guest.
21170de0967SSuraj Jitindar Singh      * For the same reason, set PSSCR_EC.
21270de0967SSuraj Jitindar Singh      */
213cf116ad4SDavid Gibson     ppc_store_lpcr(cpu, env->spr[SPR_LPCR] & ~pcc->lpcr_pm);
21470de0967SSuraj Jitindar Singh     env->spr[SPR_PSSCR] |= PSSCR_EC;
215cf116ad4SDavid Gibson     cs->halted = 1;
216a84f7179SNikunj A Dadhania     kvmppc_set_reg_ppc_online(cpu, 0);
217cf116ad4SDavid Gibson     qemu_cpu_kick(cs);
21859760f2dSAlexey Kardashevskiy }
21959760f2dSAlexey Kardashevskiy 
220*93eac7b8SNicholas Piggin static void rtas_ibm_suspend_me(PowerPCCPU *cpu, SpaprMachineState *spapr,
221*93eac7b8SNicholas Piggin                            uint32_t token, uint32_t nargs,
222*93eac7b8SNicholas Piggin                            target_ulong args,
223*93eac7b8SNicholas Piggin                            uint32_t nret, target_ulong rets)
224*93eac7b8SNicholas Piggin {
225*93eac7b8SNicholas Piggin     CPUState *cs;
226*93eac7b8SNicholas Piggin 
227*93eac7b8SNicholas Piggin     if (nargs != 0 || nret != 1) {
228*93eac7b8SNicholas Piggin         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
229*93eac7b8SNicholas Piggin         return;
230*93eac7b8SNicholas Piggin     }
231*93eac7b8SNicholas Piggin 
232*93eac7b8SNicholas Piggin     CPU_FOREACH(cs) {
233*93eac7b8SNicholas Piggin         PowerPCCPU *c = POWERPC_CPU(cs);
234*93eac7b8SNicholas Piggin         CPUPPCState *e = &c->env;
235*93eac7b8SNicholas Piggin         if (c == cpu) {
236*93eac7b8SNicholas Piggin             continue;
237*93eac7b8SNicholas Piggin         }
238*93eac7b8SNicholas Piggin 
239*93eac7b8SNicholas Piggin         /* See h_join */
240*93eac7b8SNicholas Piggin         if (!cs->halted || (e->msr & (1ULL << MSR_EE))) {
241*93eac7b8SNicholas Piggin             rtas_st(rets, 0, H_MULTI_THREADS_ACTIVE);
242*93eac7b8SNicholas Piggin             return;
243*93eac7b8SNicholas Piggin         }
244*93eac7b8SNicholas Piggin     }
245*93eac7b8SNicholas Piggin 
246*93eac7b8SNicholas Piggin     qemu_system_suspend_request();
247*93eac7b8SNicholas Piggin     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
248*93eac7b8SNicholas Piggin }
249*93eac7b8SNicholas Piggin 
250c920f7b4SDavid Gibson static inline int sysparm_st(target_ulong addr, target_ulong len,
251c920f7b4SDavid Gibson                              const void *val, uint16_t vallen)
252c920f7b4SDavid Gibson {
253c920f7b4SDavid Gibson     hwaddr phys = ppc64_phys_to_real(addr);
254c920f7b4SDavid Gibson 
255c920f7b4SDavid Gibson     if (len < 2) {
256c920f7b4SDavid Gibson         return RTAS_OUT_SYSPARM_PARAM_ERROR;
257c920f7b4SDavid Gibson     }
258c920f7b4SDavid Gibson     stw_be_phys(&address_space_memory, phys, vallen);
259c920f7b4SDavid Gibson     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
260c920f7b4SDavid Gibson     return RTAS_OUT_SUCCESS;
261c920f7b4SDavid Gibson }
262c920f7b4SDavid Gibson 
2633ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
264ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
2653ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2663ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2673ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2683ada6b11SAlexey Kardashevskiy {
269fe6b6346SLike Xu     MachineState *ms = MACHINE(qdev_get_machine());
270fe6b6346SLike Xu     unsigned int max_cpus = ms->smp.max_cpus;
2713ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2723ada6b11SAlexey Kardashevskiy     target_ulong buffer = rtas_ld(args, 1);
2733ada6b11SAlexey Kardashevskiy     target_ulong length = rtas_ld(args, 2);
274c920f7b4SDavid Gibson     target_ulong ret;
2753ada6b11SAlexey Kardashevskiy 
2763ada6b11SAlexey Kardashevskiy     switch (parameter) {
2773b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
278e3943228SSam Bobroff         char *param_val = g_strdup_printf("MaxEntCap=%d,"
279ab3dd749SPhilippe Mathieu-Daudé                                           "DesMem=%" PRIu64 ","
280e3943228SSam Bobroff                                           "DesProcs=%d,"
281e3943228SSam Bobroff                                           "MaxPlatProcs=%d",
282e3943228SSam Bobroff                                           max_cpus,
283d23b6caaSPhilippe Mathieu-Daudé                                           current_machine->ram_size / MiB,
284fe6b6346SLike Xu                                           ms->smp.cpus,
285e3943228SSam Bobroff                                           max_cpus);
286c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
2873b50d897SSam bobroff         g_free(param_val);
2883b50d897SSam bobroff         break;
2893b50d897SSam bobroff     }
2903052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
2913052d951SSam bobroff         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
2923052d951SSam bobroff 
293c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
2943ada6b11SAlexey Kardashevskiy         break;
2953ada6b11SAlexey Kardashevskiy     }
296b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2979c5ce8dbSFam Zheng         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
2989c5ce8dbSFam Zheng                          (qemu_uuid_set ? 16 : 0));
299b907d7b0SSam bobroff         break;
3003052d951SSam bobroff     default:
3013052d951SSam bobroff         ret = RTAS_OUT_NOT_SUPPORTED;
3023052d951SSam bobroff     }
3033ada6b11SAlexey Kardashevskiy 
3043ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
3053ada6b11SAlexey Kardashevskiy }
3063ada6b11SAlexey Kardashevskiy 
3073ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
308ce2918cbSDavid Gibson                                           SpaprMachineState *spapr,
3093ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
3103ada6b11SAlexey Kardashevskiy                                           target_ulong args,
3113ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
3123ada6b11SAlexey Kardashevskiy {
3133ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
3143ada6b11SAlexey Kardashevskiy     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
3153ada6b11SAlexey Kardashevskiy 
3163ada6b11SAlexey Kardashevskiy     switch (parameter) {
3173b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
3183052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
319b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
3203ada6b11SAlexey Kardashevskiy         ret = RTAS_OUT_NOT_AUTHORIZED;
3213ada6b11SAlexey Kardashevskiy         break;
3223ada6b11SAlexey Kardashevskiy     }
3233ada6b11SAlexey Kardashevskiy 
3243ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
3253ada6b11SAlexey Kardashevskiy }
3263ada6b11SAlexey Kardashevskiy 
3272e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu,
328ce2918cbSDavid Gibson                             SpaprMachineState *spapr,
3292e14072fSNikunj A Dadhania                             uint32_t token, uint32_t nargs,
3302e14072fSNikunj A Dadhania                             target_ulong args,
3312e14072fSNikunj A Dadhania                             uint32_t nret, target_ulong rets)
3322e14072fSNikunj A Dadhania {
3332c553477SDavid Gibson     qemu_system_guest_panicked(NULL);
3342e14072fSNikunj A Dadhania 
3352c553477SDavid Gibson     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
3362e14072fSNikunj A Dadhania }
3372e14072fSNikunj A Dadhania 
338ce2918cbSDavid Gibson static void rtas_set_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
339094d2058SNathan Fontenot                                  uint32_t token, uint32_t nargs,
340094d2058SNathan Fontenot                                  target_ulong args, uint32_t nret,
341094d2058SNathan Fontenot                                  target_ulong rets)
342094d2058SNathan Fontenot {
343094d2058SNathan Fontenot     int32_t power_domain;
344094d2058SNathan Fontenot 
345094d2058SNathan Fontenot     if (nargs != 2 || nret != 2) {
346094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
347094d2058SNathan Fontenot         return;
348094d2058SNathan Fontenot     }
349094d2058SNathan Fontenot 
350094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
351094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
352094d2058SNathan Fontenot      */
353094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
354094d2058SNathan Fontenot     if (power_domain != -1) {
355094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
356094d2058SNathan Fontenot         return;
357094d2058SNathan Fontenot     }
358094d2058SNathan Fontenot 
359094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
360094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
361094d2058SNathan Fontenot }
362094d2058SNathan Fontenot 
363ce2918cbSDavid Gibson static void rtas_get_power_level(PowerPCCPU *cpu, SpaprMachineState *spapr,
364094d2058SNathan Fontenot                                   uint32_t token, uint32_t nargs,
365094d2058SNathan Fontenot                                   target_ulong args, uint32_t nret,
366094d2058SNathan Fontenot                                   target_ulong rets)
367094d2058SNathan Fontenot {
368094d2058SNathan Fontenot     int32_t power_domain;
369094d2058SNathan Fontenot 
370094d2058SNathan Fontenot     if (nargs != 1 || nret != 2) {
371094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
372094d2058SNathan Fontenot         return;
373094d2058SNathan Fontenot     }
374094d2058SNathan Fontenot 
375094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
376094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
377094d2058SNathan Fontenot      */
378094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
379094d2058SNathan Fontenot     if (power_domain != -1) {
380094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
381094d2058SNathan Fontenot         return;
382094d2058SNathan Fontenot     }
383094d2058SNathan Fontenot 
384094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
385094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
386094d2058SNathan Fontenot }
387094d2058SNathan Fontenot 
38839ac8455SDavid Gibson static struct rtas_call {
38939ac8455SDavid Gibson     const char *name;
39039ac8455SDavid Gibson     spapr_rtas_fn fn;
3913a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
39239ac8455SDavid Gibson 
393ce2918cbSDavid Gibson target_ulong spapr_rtas_call(PowerPCCPU *cpu, SpaprMachineState *spapr,
39439ac8455SDavid Gibson                              uint32_t token, uint32_t nargs, target_ulong args,
39539ac8455SDavid Gibson                              uint32_t nret, target_ulong rets)
39639ac8455SDavid Gibson {
3973a3b8502SAlexey Kardashevskiy     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
3983a3b8502SAlexey Kardashevskiy         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
39939ac8455SDavid Gibson 
40039ac8455SDavid Gibson         if (call->fn) {
401210b580bSAnthony Liguori             call->fn(cpu, spapr, token, nargs, args, nret, rets);
40239ac8455SDavid Gibson             return H_SUCCESS;
40339ac8455SDavid Gibson         }
40439ac8455SDavid Gibson     }
40539ac8455SDavid Gibson 
406821303f5SDavid Gibson     /* HACK: Some Linux early debug code uses RTAS display-character,
407821303f5SDavid Gibson      * but assumes the token value is 0xa (which it is on some real
408821303f5SDavid Gibson      * machines) without looking it up in the device tree.  This
409821303f5SDavid Gibson      * special case makes this work */
410821303f5SDavid Gibson     if (token == 0xa) {
411210b580bSAnthony Liguori         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
412821303f5SDavid Gibson         return H_SUCCESS;
413821303f5SDavid Gibson     }
414821303f5SDavid Gibson 
41539ac8455SDavid Gibson     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
416a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
41739ac8455SDavid Gibson     return H_PARAMETER;
41839ac8455SDavid Gibson }
41939ac8455SDavid Gibson 
420eeddd59fSLaurent Vivier uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
421eeddd59fSLaurent Vivier                          uint32_t nret, uint64_t rets)
422eeddd59fSLaurent Vivier {
423eeddd59fSLaurent Vivier     int token;
424eeddd59fSLaurent Vivier 
425eeddd59fSLaurent Vivier     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
426eeddd59fSLaurent Vivier         if (strcmp(cmd, rtas_table[token].name) == 0) {
427ce2918cbSDavid Gibson             SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
428eeddd59fSLaurent Vivier             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
429eeddd59fSLaurent Vivier 
430eeddd59fSLaurent Vivier             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
431eeddd59fSLaurent Vivier                                  nargs, args, nret, rets);
432eeddd59fSLaurent Vivier             return H_SUCCESS;
433eeddd59fSLaurent Vivier         }
434eeddd59fSLaurent Vivier     }
435eeddd59fSLaurent Vivier     return H_PARAMETER;
436eeddd59fSLaurent Vivier }
437eeddd59fSLaurent Vivier 
4383a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
43939ac8455SDavid Gibson {
440adf9ac50SDavid Gibson     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
4413a3b8502SAlexey Kardashevskiy 
4423a3b8502SAlexey Kardashevskiy     token -= RTAS_TOKEN_BASE;
443adf9ac50SDavid Gibson 
44464db6c70SCédric Le Goater     assert(!name || !rtas_table[token].name);
445c89d5299SDavid Gibson 
4463a3b8502SAlexey Kardashevskiy     rtas_table[token].name = name;
4473a3b8502SAlexey Kardashevskiy     rtas_table[token].fn = fn;
44839ac8455SDavid Gibson }
44939ac8455SDavid Gibson 
4503f5dabceSDavid Gibson void spapr_dt_rtas_tokens(void *fdt, int rtas)
45139ac8455SDavid Gibson {
45239ac8455SDavid Gibson     int i;
45339ac8455SDavid Gibson 
4543a3b8502SAlexey Kardashevskiy     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
45539ac8455SDavid Gibson         struct rtas_call *call = &rtas_table[i];
45639ac8455SDavid Gibson 
457d36b66f7SBen Herrenschmidt         if (!call->name) {
45839ac8455SDavid Gibson             continue;
45939ac8455SDavid Gibson         }
46039ac8455SDavid Gibson 
4613f5dabceSDavid Gibson         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
46239ac8455SDavid Gibson     }
46339ac8455SDavid Gibson }
464821303f5SDavid Gibson 
465ce2918cbSDavid Gibson void spapr_load_rtas(SpaprMachineState *spapr, void *fdt, hwaddr addr)
4662cac78c1SDavid Gibson {
4672cac78c1SDavid Gibson     int rtas_node;
4682cac78c1SDavid Gibson     int ret;
4692cac78c1SDavid Gibson 
4702cac78c1SDavid Gibson     /* Copy RTAS blob into guest RAM */
4712cac78c1SDavid Gibson     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
4722cac78c1SDavid Gibson 
4732cac78c1SDavid Gibson     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
4742cac78c1SDavid Gibson     if (ret < 0) {
4752cac78c1SDavid Gibson         error_report("Couldn't add RTAS reserve entry: %s",
4762cac78c1SDavid Gibson                      fdt_strerror(ret));
4772cac78c1SDavid Gibson         exit(1);
4782cac78c1SDavid Gibson     }
4792cac78c1SDavid Gibson 
4802cac78c1SDavid Gibson     /* Update the device tree with the blob's location */
4812cac78c1SDavid Gibson     rtas_node = fdt_path_offset(fdt, "/rtas");
4822cac78c1SDavid Gibson     assert(rtas_node >= 0);
4832cac78c1SDavid Gibson 
4842cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
4852cac78c1SDavid Gibson     if (ret < 0) {
4862cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-base property: %s",
4872cac78c1SDavid Gibson                      fdt_strerror(ret));
4882cac78c1SDavid Gibson         exit(1);
4892cac78c1SDavid Gibson     }
4902cac78c1SDavid Gibson 
4912cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
4922cac78c1SDavid Gibson     if (ret < 0) {
4932cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-entry property: %s",
4942cac78c1SDavid Gibson                      fdt_strerror(ret));
4952cac78c1SDavid Gibson         exit(1);
4962cac78c1SDavid Gibson     }
4972cac78c1SDavid Gibson 
4982cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
4992cac78c1SDavid Gibson     if (ret < 0) {
5002cac78c1SDavid Gibson         error_report("Couldn't add rtas-size property: %s",
5012cac78c1SDavid Gibson                      fdt_strerror(ret));
5022cac78c1SDavid Gibson         exit(1);
5032cac78c1SDavid Gibson     }
5042cac78c1SDavid Gibson }
5052cac78c1SDavid Gibson 
50683f7d43aSAndreas Färber static void core_rtas_register_types(void)
507821303f5SDavid Gibson {
5083a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
5093a3b8502SAlexey Kardashevskiy                         rtas_display_character);
5103a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
5113a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
5123a3b8502SAlexey Kardashevskiy                         rtas_system_reboot);
5133a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
514a9f8ad8fSDavid Gibson                         rtas_query_cpu_stopped_state);
5153a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
5163a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
517*93eac7b8SNicholas Piggin     spapr_rtas_register(RTAS_IBM_SUSPEND_ME, "ibm,suspend-me",
518*93eac7b8SNicholas Piggin                         rtas_ibm_suspend_me);
5193a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
5203a3b8502SAlexey Kardashevskiy                         "ibm,get-system-parameter",
5213ada6b11SAlexey Kardashevskiy                         rtas_ibm_get_system_parameter);
5223a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
5233a3b8502SAlexey Kardashevskiy                         "ibm,set-system-parameter",
5243ada6b11SAlexey Kardashevskiy                         rtas_ibm_set_system_parameter);
5252e14072fSNikunj A Dadhania     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
5262e14072fSNikunj A Dadhania                         rtas_ibm_os_term);
527094d2058SNathan Fontenot     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
528094d2058SNathan Fontenot                         rtas_set_power_level);
529094d2058SNathan Fontenot     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
530094d2058SNathan Fontenot                         rtas_get_power_level);
531821303f5SDavid Gibson }
53283f7d43aSAndreas Färber 
53383f7d43aSAndreas Färber type_init(core_rtas_register_types)
534