xref: /qemu/hw/ppc/spapr_rtas.c (revision d23b6caadbfaf56092593e8ff22fb5797db38488)
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"
3639ac8455SDavid Gibson 
370d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
380d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
39eeddd59fSLaurent Vivier #include "hw/ppc/spapr_rtas.h"
4084369f63SDavid Gibson #include "hw/ppc/spapr_cpu_core.h"
41af81cf32SBharata B Rao #include "hw/ppc/ppc.h"
42e3943228SSam Bobroff #include "hw/boards.h"
4339ac8455SDavid Gibson 
4439ac8455SDavid Gibson #include <libfdt.h>
458c8639dfSMike Day #include "hw/ppc/spapr_drc.h"
46f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
47028ec3ceSLaurent Vivier #include "trace.h"
483f5dabceSDavid Gibson #include "hw/ppc/fdt.h"
49cf116ad4SDavid Gibson #include "target/ppc/mmu-hash64.h"
50f00bed95SDavid Gibson #include "target/ppc/mmu-book3s-v3.h"
518c8639dfSMike Day 
5228e02042SDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr,
53821303f5SDavid Gibson                                    uint32_t token, uint32_t nargs,
54821303f5SDavid Gibson                                    target_ulong args,
55821303f5SDavid Gibson                                    uint32_t nret, target_ulong rets)
56821303f5SDavid Gibson {
57821303f5SDavid Gibson     uint8_t c = rtas_ld(args, 0);
585f2e2ba2SDavid Gibson     VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
59821303f5SDavid Gibson 
60821303f5SDavid Gibson     if (!sdev) {
61a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
62821303f5SDavid Gibson     } else {
63821303f5SDavid Gibson         vty_putchars(sdev, &c, sizeof(c));
64a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
65821303f5SDavid Gibson     }
66821303f5SDavid Gibson }
67821303f5SDavid Gibson 
6828e02042SDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
69821303f5SDavid Gibson                            uint32_t token, uint32_t nargs, target_ulong args,
70821303f5SDavid Gibson                            uint32_t nret, target_ulong rets)
71821303f5SDavid Gibson {
72821303f5SDavid Gibson     if (nargs != 2 || nret != 1) {
73a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
74821303f5SDavid Gibson         return;
75821303f5SDavid Gibson     }
76cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
778a9c1b77SThomas Huth     cpu_stop_current();
78a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
79821303f5SDavid Gibson }
80821303f5SDavid Gibson 
8128e02042SDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr,
82c821a43cSDavid Gibson                                uint32_t token, uint32_t nargs,
83c821a43cSDavid Gibson                                target_ulong args,
84c821a43cSDavid Gibson                                uint32_t nret, target_ulong rets)
85c821a43cSDavid Gibson {
86c821a43cSDavid Gibson     if (nargs != 0 || nret != 1) {
87a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
88c821a43cSDavid Gibson         return;
89c821a43cSDavid Gibson     }
90cf83f140SEric Blake     qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
91a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
92c821a43cSDavid Gibson }
93c821a43cSDavid Gibson 
94210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
9528e02042SDavid Gibson                                          sPAPRMachineState *spapr,
96a9f8ad8fSDavid Gibson                                          uint32_t token, uint32_t nargs,
97a9f8ad8fSDavid Gibson                                          target_ulong args,
98a9f8ad8fSDavid Gibson                                          uint32_t nret, target_ulong rets)
99a9f8ad8fSDavid Gibson {
100a9f8ad8fSDavid Gibson     target_ulong id;
1010f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
102a9f8ad8fSDavid Gibson 
103a9f8ad8fSDavid Gibson     if (nargs != 1 || nret != 2) {
104a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
105a9f8ad8fSDavid Gibson         return;
106a9f8ad8fSDavid Gibson     }
107a9f8ad8fSDavid Gibson 
108a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
1092e886fb3SSam Bobroff     cpu = spapr_find_cpu(id);
11005318a85SAndreas Färber     if (cpu != NULL) {
1110f20ba62SAlexey Kardashevskiy         if (CPU(cpu)->halted) {
112a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 0);
113a9f8ad8fSDavid Gibson         } else {
114a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 2);
115a9f8ad8fSDavid Gibson         }
116a9f8ad8fSDavid Gibson 
117a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
118a9f8ad8fSDavid Gibson         return;
119a9f8ad8fSDavid Gibson     }
120a9f8ad8fSDavid Gibson 
121a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
122a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
123a9f8ad8fSDavid Gibson }
124a9f8ad8fSDavid Gibson 
125cf116ad4SDavid Gibson static void rtas_start_cpu(PowerPCCPU *callcpu, sPAPRMachineState *spapr,
126a9f8ad8fSDavid Gibson                            uint32_t token, uint32_t nargs,
127a9f8ad8fSDavid Gibson                            target_ulong args,
128a9f8ad8fSDavid Gibson                            uint32_t nret, target_ulong rets)
129a9f8ad8fSDavid Gibson {
130a9f8ad8fSDavid Gibson     target_ulong id, start, r3;
131cf116ad4SDavid Gibson     PowerPCCPU *newcpu;
132cf116ad4SDavid Gibson     CPUPPCState *env;
133cf116ad4SDavid Gibson     PowerPCCPUClass *pcc;
13498248918SDavid Gibson     target_ulong lpcr;
135a9f8ad8fSDavid Gibson 
136a9f8ad8fSDavid Gibson     if (nargs != 3 || nret != 1) {
137a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
138a9f8ad8fSDavid Gibson         return;
139a9f8ad8fSDavid Gibson     }
140a9f8ad8fSDavid Gibson 
141a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
142a9f8ad8fSDavid Gibson     start = rtas_ld(args, 1);
143a9f8ad8fSDavid Gibson     r3 = rtas_ld(args, 2);
144a9f8ad8fSDavid Gibson 
145cf116ad4SDavid Gibson     newcpu = spapr_find_cpu(id);
146cf116ad4SDavid Gibson     if (!newcpu) {
147cf116ad4SDavid Gibson         /* Didn't find a matching cpu */
148cf116ad4SDavid Gibson         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
149cf116ad4SDavid Gibson         return;
150cf116ad4SDavid Gibson     }
151c08d7424SAndreas Färber 
152cf116ad4SDavid Gibson     env = &newcpu->env;
153cf116ad4SDavid Gibson     pcc = POWERPC_CPU_GET_CLASS(newcpu);
154cf116ad4SDavid Gibson 
155cf116ad4SDavid Gibson     if (!CPU(newcpu)->halted) {
156a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
157a9f8ad8fSDavid Gibson         return;
158a9f8ad8fSDavid Gibson     }
159a9f8ad8fSDavid Gibson 
160cf116ad4SDavid Gibson     cpu_synchronize_state(CPU(newcpu));
161048706d9SDavid Gibson 
162a9f8ad8fSDavid Gibson     env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
16398248918SDavid Gibson 
1649a94ee5bSCédric Le Goater     /* Enable Power-saving mode Exit Cause exceptions for the new CPU */
16547a9b551SDavid Gibson     lpcr = env->spr[SPR_LPCR];
16698248918SDavid Gibson     if (!pcc->interrupts_big_endian(callcpu)) {
16798248918SDavid Gibson         lpcr |= LPCR_ILE;
16898248918SDavid Gibson     }
169f00bed95SDavid Gibson     if (env->mmu_model == POWERPC_MMU_3_00) {
170f00bed95SDavid Gibson         /*
171f00bed95SDavid Gibson          * New cpus are expected to start in the same radix/hash mode
172f00bed95SDavid Gibson          * as the existing CPUs
173f00bed95SDavid Gibson          */
174f00bed95SDavid Gibson         if (ppc64_radix_guest(callcpu)) {
175f00bed95SDavid Gibson             lpcr |= LPCR_UPRT | LPCR_GTSE;
176f00bed95SDavid Gibson         } else {
177f00bed95SDavid Gibson             lpcr &= ~(LPCR_UPRT | LPCR_GTSE);
178f00bed95SDavid Gibson         }
179f00bed95SDavid Gibson     }
18098248918SDavid Gibson     ppc_store_lpcr(newcpu, lpcr);
18198248918SDavid Gibson 
18298248918SDavid Gibson     /*
18398248918SDavid Gibson      * Set the timebase offset of the new CPU to that of the invoking
18498248918SDavid Gibson      * CPU.  This helps hotplugged CPU to have the correct timebase
18598248918SDavid Gibson      * offset.
18698248918SDavid Gibson      */
18798248918SDavid Gibson     newcpu->env.tb_env->tb_offset = callcpu->env.tb_env->tb_offset;
1889a94ee5bSCédric Le Goater 
18984369f63SDavid Gibson     spapr_cpu_set_entry_state(newcpu, start, r3);
190cf116ad4SDavid Gibson 
191cf116ad4SDavid Gibson     qemu_cpu_kick(CPU(newcpu));
192a9f8ad8fSDavid Gibson 
193a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
194a9f8ad8fSDavid Gibson }
195a9f8ad8fSDavid Gibson 
19628e02042SDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
19759760f2dSAlexey Kardashevskiy                            uint32_t token, uint32_t nargs,
19859760f2dSAlexey Kardashevskiy                            target_ulong args,
19959760f2dSAlexey Kardashevskiy                            uint32_t nret, target_ulong rets)
20059760f2dSAlexey Kardashevskiy {
20159760f2dSAlexey Kardashevskiy     CPUState *cs = CPU(cpu);
20259760f2dSAlexey Kardashevskiy     CPUPPCState *env = &cpu->env;
2039a94ee5bSCédric Le Goater     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
20459760f2dSAlexey Kardashevskiy 
2059a94ee5bSCédric Le Goater     /* Disable Power-saving mode Exit Cause exceptions for the CPU.
2069a94ee5bSCédric Le Goater      * This could deliver an interrupt on a dying CPU and crash the
2079a94ee5bSCédric Le Goater      * guest */
208cf116ad4SDavid Gibson     ppc_store_lpcr(cpu, env->spr[SPR_LPCR] & ~pcc->lpcr_pm);
209cf116ad4SDavid Gibson     cs->halted = 1;
210cf116ad4SDavid Gibson     qemu_cpu_kick(cs);
21159760f2dSAlexey Kardashevskiy }
21259760f2dSAlexey Kardashevskiy 
213c920f7b4SDavid Gibson static inline int sysparm_st(target_ulong addr, target_ulong len,
214c920f7b4SDavid Gibson                              const void *val, uint16_t vallen)
215c920f7b4SDavid Gibson {
216c920f7b4SDavid Gibson     hwaddr phys = ppc64_phys_to_real(addr);
217c920f7b4SDavid Gibson 
218c920f7b4SDavid Gibson     if (len < 2) {
219c920f7b4SDavid Gibson         return RTAS_OUT_SYSPARM_PARAM_ERROR;
220c920f7b4SDavid Gibson     }
221c920f7b4SDavid Gibson     stw_be_phys(&address_space_memory, phys, vallen);
222c920f7b4SDavid Gibson     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
223c920f7b4SDavid Gibson     return RTAS_OUT_SUCCESS;
224c920f7b4SDavid Gibson }
225c920f7b4SDavid Gibson 
2263ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
22728e02042SDavid Gibson                                           sPAPRMachineState *spapr,
2283ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2293ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2303ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2313ada6b11SAlexey Kardashevskiy {
2323ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2333ada6b11SAlexey Kardashevskiy     target_ulong buffer = rtas_ld(args, 1);
2343ada6b11SAlexey Kardashevskiy     target_ulong length = rtas_ld(args, 2);
235c920f7b4SDavid Gibson     target_ulong ret;
2363ada6b11SAlexey Kardashevskiy 
2373ada6b11SAlexey Kardashevskiy     switch (parameter) {
2383b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
239e3943228SSam Bobroff         char *param_val = g_strdup_printf("MaxEntCap=%d,"
240e3943228SSam Bobroff                                           "DesMem=%llu,"
241e3943228SSam Bobroff                                           "DesProcs=%d,"
242e3943228SSam Bobroff                                           "MaxPlatProcs=%d",
243e3943228SSam Bobroff                                           max_cpus,
244*d23b6caaSPhilippe Mathieu-Daudé                                           current_machine->ram_size / MiB,
245e3943228SSam Bobroff                                           smp_cpus,
246e3943228SSam Bobroff                                           max_cpus);
247c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
2483b50d897SSam bobroff         g_free(param_val);
2493b50d897SSam bobroff         break;
2503b50d897SSam bobroff     }
2513052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
2523052d951SSam bobroff         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
2533052d951SSam bobroff 
254c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
2553ada6b11SAlexey Kardashevskiy         break;
2563ada6b11SAlexey Kardashevskiy     }
257b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2589c5ce8dbSFam Zheng         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
2599c5ce8dbSFam Zheng                          (qemu_uuid_set ? 16 : 0));
260b907d7b0SSam bobroff         break;
2613052d951SSam bobroff     default:
2623052d951SSam bobroff         ret = RTAS_OUT_NOT_SUPPORTED;
2633052d951SSam bobroff     }
2643ada6b11SAlexey Kardashevskiy 
2653ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
2663ada6b11SAlexey Kardashevskiy }
2673ada6b11SAlexey Kardashevskiy 
2683ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
26928e02042SDavid Gibson                                           sPAPRMachineState *spapr,
2703ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2713ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2723ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2733ada6b11SAlexey Kardashevskiy {
2743ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2753ada6b11SAlexey Kardashevskiy     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
2763ada6b11SAlexey Kardashevskiy 
2773ada6b11SAlexey Kardashevskiy     switch (parameter) {
2783b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
2793052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
280b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2813ada6b11SAlexey Kardashevskiy         ret = RTAS_OUT_NOT_AUTHORIZED;
2823ada6b11SAlexey Kardashevskiy         break;
2833ada6b11SAlexey Kardashevskiy     }
2843ada6b11SAlexey Kardashevskiy 
2853ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
2863ada6b11SAlexey Kardashevskiy }
2873ada6b11SAlexey Kardashevskiy 
2882e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu,
28928e02042SDavid Gibson                             sPAPRMachineState *spapr,
2902e14072fSNikunj A Dadhania                             uint32_t token, uint32_t nargs,
2912e14072fSNikunj A Dadhania                             target_ulong args,
2922e14072fSNikunj A Dadhania                             uint32_t nret, target_ulong rets)
2932e14072fSNikunj A Dadhania {
2942c553477SDavid Gibson     qemu_system_guest_panicked(NULL);
2952e14072fSNikunj A Dadhania 
2962c553477SDavid Gibson     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
2972e14072fSNikunj A Dadhania }
2982e14072fSNikunj A Dadhania 
29928e02042SDavid Gibson static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
300094d2058SNathan Fontenot                                  uint32_t token, uint32_t nargs,
301094d2058SNathan Fontenot                                  target_ulong args, uint32_t nret,
302094d2058SNathan Fontenot                                  target_ulong rets)
303094d2058SNathan Fontenot {
304094d2058SNathan Fontenot     int32_t power_domain;
305094d2058SNathan Fontenot 
306094d2058SNathan Fontenot     if (nargs != 2 || nret != 2) {
307094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
308094d2058SNathan Fontenot         return;
309094d2058SNathan Fontenot     }
310094d2058SNathan Fontenot 
311094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
312094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
313094d2058SNathan Fontenot      */
314094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
315094d2058SNathan Fontenot     if (power_domain != -1) {
316094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
317094d2058SNathan Fontenot         return;
318094d2058SNathan Fontenot     }
319094d2058SNathan Fontenot 
320094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
321094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
322094d2058SNathan Fontenot }
323094d2058SNathan Fontenot 
32428e02042SDavid Gibson static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
325094d2058SNathan Fontenot                                   uint32_t token, uint32_t nargs,
326094d2058SNathan Fontenot                                   target_ulong args, uint32_t nret,
327094d2058SNathan Fontenot                                   target_ulong rets)
328094d2058SNathan Fontenot {
329094d2058SNathan Fontenot     int32_t power_domain;
330094d2058SNathan Fontenot 
331094d2058SNathan Fontenot     if (nargs != 1 || nret != 2) {
332094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
333094d2058SNathan Fontenot         return;
334094d2058SNathan Fontenot     }
335094d2058SNathan Fontenot 
336094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
337094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
338094d2058SNathan Fontenot      */
339094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
340094d2058SNathan Fontenot     if (power_domain != -1) {
341094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
342094d2058SNathan Fontenot         return;
343094d2058SNathan Fontenot     }
344094d2058SNathan Fontenot 
345094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
346094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
347094d2058SNathan Fontenot }
348094d2058SNathan Fontenot 
34939ac8455SDavid Gibson static struct rtas_call {
35039ac8455SDavid Gibson     const char *name;
35139ac8455SDavid Gibson     spapr_rtas_fn fn;
3523a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
35339ac8455SDavid Gibson 
35428e02042SDavid Gibson target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
35539ac8455SDavid Gibson                              uint32_t token, uint32_t nargs, target_ulong args,
35639ac8455SDavid Gibson                              uint32_t nret, target_ulong rets)
35739ac8455SDavid Gibson {
3583a3b8502SAlexey Kardashevskiy     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
3593a3b8502SAlexey Kardashevskiy         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
36039ac8455SDavid Gibson 
36139ac8455SDavid Gibson         if (call->fn) {
362210b580bSAnthony Liguori             call->fn(cpu, spapr, token, nargs, args, nret, rets);
36339ac8455SDavid Gibson             return H_SUCCESS;
36439ac8455SDavid Gibson         }
36539ac8455SDavid Gibson     }
36639ac8455SDavid Gibson 
367821303f5SDavid Gibson     /* HACK: Some Linux early debug code uses RTAS display-character,
368821303f5SDavid Gibson      * but assumes the token value is 0xa (which it is on some real
369821303f5SDavid Gibson      * machines) without looking it up in the device tree.  This
370821303f5SDavid Gibson      * special case makes this work */
371821303f5SDavid Gibson     if (token == 0xa) {
372210b580bSAnthony Liguori         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
373821303f5SDavid Gibson         return H_SUCCESS;
374821303f5SDavid Gibson     }
375821303f5SDavid Gibson 
37639ac8455SDavid Gibson     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
377a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
37839ac8455SDavid Gibson     return H_PARAMETER;
37939ac8455SDavid Gibson }
38039ac8455SDavid Gibson 
381eeddd59fSLaurent Vivier uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
382eeddd59fSLaurent Vivier                          uint32_t nret, uint64_t rets)
383eeddd59fSLaurent Vivier {
384eeddd59fSLaurent Vivier     int token;
385eeddd59fSLaurent Vivier 
386eeddd59fSLaurent Vivier     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
387eeddd59fSLaurent Vivier         if (strcmp(cmd, rtas_table[token].name) == 0) {
388eeddd59fSLaurent Vivier             sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
389eeddd59fSLaurent Vivier             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
390eeddd59fSLaurent Vivier 
391eeddd59fSLaurent Vivier             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
392eeddd59fSLaurent Vivier                                  nargs, args, nret, rets);
393eeddd59fSLaurent Vivier             return H_SUCCESS;
394eeddd59fSLaurent Vivier         }
395eeddd59fSLaurent Vivier     }
396eeddd59fSLaurent Vivier     return H_PARAMETER;
397eeddd59fSLaurent Vivier }
398eeddd59fSLaurent Vivier 
3993a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
40039ac8455SDavid Gibson {
401adf9ac50SDavid Gibson     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
4023a3b8502SAlexey Kardashevskiy 
4033a3b8502SAlexey Kardashevskiy     token -= RTAS_TOKEN_BASE;
404adf9ac50SDavid Gibson 
405adf9ac50SDavid Gibson     assert(!rtas_table[token].name);
406c89d5299SDavid Gibson 
4073a3b8502SAlexey Kardashevskiy     rtas_table[token].name = name;
4083a3b8502SAlexey Kardashevskiy     rtas_table[token].fn = fn;
40939ac8455SDavid Gibson }
41039ac8455SDavid Gibson 
4113f5dabceSDavid Gibson void spapr_dt_rtas_tokens(void *fdt, int rtas)
41239ac8455SDavid Gibson {
41339ac8455SDavid Gibson     int i;
41439ac8455SDavid Gibson 
4153a3b8502SAlexey Kardashevskiy     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
41639ac8455SDavid Gibson         struct rtas_call *call = &rtas_table[i];
41739ac8455SDavid Gibson 
418d36b66f7SBen Herrenschmidt         if (!call->name) {
41939ac8455SDavid Gibson             continue;
42039ac8455SDavid Gibson         }
42139ac8455SDavid Gibson 
4223f5dabceSDavid Gibson         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
42339ac8455SDavid Gibson     }
42439ac8455SDavid Gibson }
425821303f5SDavid Gibson 
4262cac78c1SDavid Gibson void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr)
4272cac78c1SDavid Gibson {
4282cac78c1SDavid Gibson     int rtas_node;
4292cac78c1SDavid Gibson     int ret;
4302cac78c1SDavid Gibson 
4312cac78c1SDavid Gibson     /* Copy RTAS blob into guest RAM */
4322cac78c1SDavid Gibson     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
4332cac78c1SDavid Gibson 
4342cac78c1SDavid Gibson     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
4352cac78c1SDavid Gibson     if (ret < 0) {
4362cac78c1SDavid Gibson         error_report("Couldn't add RTAS reserve entry: %s",
4372cac78c1SDavid Gibson                      fdt_strerror(ret));
4382cac78c1SDavid Gibson         exit(1);
4392cac78c1SDavid Gibson     }
4402cac78c1SDavid Gibson 
4412cac78c1SDavid Gibson     /* Update the device tree with the blob's location */
4422cac78c1SDavid Gibson     rtas_node = fdt_path_offset(fdt, "/rtas");
4432cac78c1SDavid Gibson     assert(rtas_node >= 0);
4442cac78c1SDavid Gibson 
4452cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
4462cac78c1SDavid Gibson     if (ret < 0) {
4472cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-base property: %s",
4482cac78c1SDavid Gibson                      fdt_strerror(ret));
4492cac78c1SDavid Gibson         exit(1);
4502cac78c1SDavid Gibson     }
4512cac78c1SDavid Gibson 
4522cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
4532cac78c1SDavid Gibson     if (ret < 0) {
4542cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-entry property: %s",
4552cac78c1SDavid Gibson                      fdt_strerror(ret));
4562cac78c1SDavid Gibson         exit(1);
4572cac78c1SDavid Gibson     }
4582cac78c1SDavid Gibson 
4592cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
4602cac78c1SDavid Gibson     if (ret < 0) {
4612cac78c1SDavid Gibson         error_report("Couldn't add rtas-size property: %s",
4622cac78c1SDavid Gibson                      fdt_strerror(ret));
4632cac78c1SDavid Gibson         exit(1);
4642cac78c1SDavid Gibson     }
4652cac78c1SDavid Gibson }
4662cac78c1SDavid Gibson 
46783f7d43aSAndreas Färber static void core_rtas_register_types(void)
468821303f5SDavid Gibson {
4693a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
4703a3b8502SAlexey Kardashevskiy                         rtas_display_character);
4713a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
4723a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
4733a3b8502SAlexey Kardashevskiy                         rtas_system_reboot);
4743a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
475a9f8ad8fSDavid Gibson                         rtas_query_cpu_stopped_state);
4763a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
4773a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
4783a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
4793a3b8502SAlexey Kardashevskiy                         "ibm,get-system-parameter",
4803ada6b11SAlexey Kardashevskiy                         rtas_ibm_get_system_parameter);
4813a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
4823a3b8502SAlexey Kardashevskiy                         "ibm,set-system-parameter",
4833ada6b11SAlexey Kardashevskiy                         rtas_ibm_set_system_parameter);
4842e14072fSNikunj A Dadhania     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
4852e14072fSNikunj A Dadhania                         rtas_ibm_os_term);
486094d2058SNathan Fontenot     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
487094d2058SNathan Fontenot                         rtas_set_power_level);
488094d2058SNathan Fontenot     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
489094d2058SNathan Fontenot                         rtas_get_power_level);
490821303f5SDavid Gibson }
49183f7d43aSAndreas Färber 
49283f7d43aSAndreas Färber type_init(core_rtas_register_types)
493