xref: /qemu/hw/ppc/spapr_rtas.c (revision cf83f140059f21d4629ae4b61d468c3baef2bb4c)
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"
32dccfcd0eSPaolo Bonzini #include "sysemu/char.h"
3339ac8455SDavid Gibson #include "hw/qdev.h"
349c17d615SPaolo Bonzini #include "sysemu/device_tree.h"
35db4ef288SBharata B Rao #include "sysemu/cpus.h"
3677ac58ddSPaolo Bonzini #include "sysemu/kvm.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"
41af81cf32SBharata B Rao #include "hw/ppc/ppc.h"
42e010ad8fSWenchao Xia #include "qapi-event.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"
508c8639dfSMike Day 
5128e02042SDavid Gibson static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr,
5246503c2bSMichael Roth                                                     uint32_t drc_index)
5346503c2bSMichael Roth {
5446503c2bSMichael Roth     sPAPRConfigureConnectorState *ccs = NULL;
5546503c2bSMichael Roth 
5646503c2bSMichael Roth     QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) {
5746503c2bSMichael Roth         if (ccs->drc_index == drc_index) {
5846503c2bSMichael Roth             break;
5946503c2bSMichael Roth         }
6046503c2bSMichael Roth     }
6146503c2bSMichael Roth 
6246503c2bSMichael Roth     return ccs;
6346503c2bSMichael Roth }
6446503c2bSMichael Roth 
6528e02042SDavid Gibson static void spapr_ccs_add(sPAPRMachineState *spapr,
6646503c2bSMichael Roth                           sPAPRConfigureConnectorState *ccs)
6746503c2bSMichael Roth {
6846503c2bSMichael Roth     g_assert(!spapr_ccs_find(spapr, ccs->drc_index));
6946503c2bSMichael Roth     QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next);
7046503c2bSMichael Roth }
7146503c2bSMichael Roth 
7228e02042SDavid Gibson static void spapr_ccs_remove(sPAPRMachineState *spapr,
7346503c2bSMichael Roth                              sPAPRConfigureConnectorState *ccs)
7446503c2bSMichael Roth {
7546503c2bSMichael Roth     QTAILQ_REMOVE(&spapr->ccs_list, ccs, next);
7646503c2bSMichael Roth     g_free(ccs);
7746503c2bSMichael Roth }
7846503c2bSMichael Roth 
7946503c2bSMichael Roth void spapr_ccs_reset_hook(void *opaque)
8046503c2bSMichael Roth {
8128e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
8246503c2bSMichael Roth     sPAPRConfigureConnectorState *ccs, *ccs_tmp;
8346503c2bSMichael Roth 
8446503c2bSMichael Roth     QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
8546503c2bSMichael Roth         spapr_ccs_remove(spapr, ccs);
8646503c2bSMichael Roth     }
8746503c2bSMichael Roth }
8839ac8455SDavid Gibson 
8928e02042SDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr,
90821303f5SDavid Gibson                                    uint32_t token, uint32_t nargs,
91821303f5SDavid Gibson                                    target_ulong args,
92821303f5SDavid Gibson                                    uint32_t nret, target_ulong rets)
93821303f5SDavid Gibson {
94821303f5SDavid Gibson     uint8_t c = rtas_ld(args, 0);
955f2e2ba2SDavid Gibson     VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
96821303f5SDavid Gibson 
97821303f5SDavid Gibson     if (!sdev) {
98a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
99821303f5SDavid Gibson     } else {
100821303f5SDavid Gibson         vty_putchars(sdev, &c, sizeof(c));
101a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
102821303f5SDavid Gibson     }
103821303f5SDavid Gibson }
104821303f5SDavid Gibson 
10528e02042SDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
106821303f5SDavid Gibson                            uint32_t token, uint32_t nargs, target_ulong args,
107821303f5SDavid Gibson                            uint32_t nret, target_ulong rets)
108821303f5SDavid Gibson {
109821303f5SDavid Gibson     if (nargs != 2 || nret != 1) {
110a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
111821303f5SDavid Gibson         return;
112821303f5SDavid Gibson     }
113*cf83f140SEric Blake     qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
1148a9c1b77SThomas Huth     cpu_stop_current();
115a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
116821303f5SDavid Gibson }
117821303f5SDavid Gibson 
11828e02042SDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr,
119c821a43cSDavid Gibson                                uint32_t token, uint32_t nargs,
120c821a43cSDavid Gibson                                target_ulong args,
121c821a43cSDavid Gibson                                uint32_t nret, target_ulong rets)
122c821a43cSDavid Gibson {
123c821a43cSDavid Gibson     if (nargs != 0 || nret != 1) {
124a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
125c821a43cSDavid Gibson         return;
126c821a43cSDavid Gibson     }
127*cf83f140SEric Blake     qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
128a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
129c821a43cSDavid Gibson }
130c821a43cSDavid Gibson 
131210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
13228e02042SDavid Gibson                                          sPAPRMachineState *spapr,
133a9f8ad8fSDavid Gibson                                          uint32_t token, uint32_t nargs,
134a9f8ad8fSDavid Gibson                                          target_ulong args,
135a9f8ad8fSDavid Gibson                                          uint32_t nret, target_ulong rets)
136a9f8ad8fSDavid Gibson {
137a9f8ad8fSDavid Gibson     target_ulong id;
1380f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
139a9f8ad8fSDavid Gibson 
140a9f8ad8fSDavid Gibson     if (nargs != 1 || nret != 2) {
141a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
142a9f8ad8fSDavid Gibson         return;
143a9f8ad8fSDavid Gibson     }
144a9f8ad8fSDavid Gibson 
145a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
1460f20ba62SAlexey Kardashevskiy     cpu = ppc_get_vcpu_by_dt_id(id);
14705318a85SAndreas Färber     if (cpu != NULL) {
1480f20ba62SAlexey Kardashevskiy         if (CPU(cpu)->halted) {
149a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 0);
150a9f8ad8fSDavid Gibson         } else {
151a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 2);
152a9f8ad8fSDavid Gibson         }
153a9f8ad8fSDavid Gibson 
154a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
155a9f8ad8fSDavid Gibson         return;
156a9f8ad8fSDavid Gibson     }
157a9f8ad8fSDavid Gibson 
158a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
159a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
160a9f8ad8fSDavid Gibson }
161a9f8ad8fSDavid Gibson 
162af81cf32SBharata B Rao /*
163af81cf32SBharata B Rao  * Set the timebase offset of the CPU to that of first CPU.
164af81cf32SBharata B Rao  * This helps hotplugged CPU to have the correct timebase offset.
165af81cf32SBharata B Rao  */
166af81cf32SBharata B Rao static void spapr_cpu_update_tb_offset(PowerPCCPU *cpu)
167af81cf32SBharata B Rao {
168af81cf32SBharata B Rao     PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
169af81cf32SBharata B Rao 
170af81cf32SBharata B Rao     cpu->env.tb_env->tb_offset = fcpu->env.tb_env->tb_offset;
171af81cf32SBharata B Rao }
172af81cf32SBharata B Rao 
173af81cf32SBharata B Rao static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
174af81cf32SBharata B Rao {
175af81cf32SBharata B Rao     PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
176af81cf32SBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
177af81cf32SBharata B Rao 
178af81cf32SBharata B Rao     if (!pcc->interrupts_big_endian(fcpu)) {
179af81cf32SBharata B Rao         cpu->env.spr[SPR_LPCR] |= LPCR_ILE;
180af81cf32SBharata B Rao     }
181af81cf32SBharata B Rao }
182af81cf32SBharata B Rao 
18328e02042SDavid Gibson static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr,
184a9f8ad8fSDavid Gibson                            uint32_t token, uint32_t nargs,
185a9f8ad8fSDavid Gibson                            target_ulong args,
186a9f8ad8fSDavid Gibson                            uint32_t nret, target_ulong rets)
187a9f8ad8fSDavid Gibson {
188a9f8ad8fSDavid Gibson     target_ulong id, start, r3;
1890f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
190a9f8ad8fSDavid Gibson 
191a9f8ad8fSDavid Gibson     if (nargs != 3 || nret != 1) {
192a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
193a9f8ad8fSDavid Gibson         return;
194a9f8ad8fSDavid Gibson     }
195a9f8ad8fSDavid Gibson 
196a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
197a9f8ad8fSDavid Gibson     start = rtas_ld(args, 1);
198a9f8ad8fSDavid Gibson     r3 = rtas_ld(args, 2);
199a9f8ad8fSDavid Gibson 
2000f20ba62SAlexey Kardashevskiy     cpu = ppc_get_vcpu_by_dt_id(id);
2010f20ba62SAlexey Kardashevskiy     if (cpu != NULL) {
2020f20ba62SAlexey Kardashevskiy         CPUState *cs = CPU(cpu);
203c67e216bSAndreas Färber         CPUPPCState *env = &cpu->env;
204c08d7424SAndreas Färber 
205c67e216bSAndreas Färber         if (!cs->halted) {
206a64d325dSAlexey Kardashevskiy             rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
207a9f8ad8fSDavid Gibson             return;
208a9f8ad8fSDavid Gibson         }
209a9f8ad8fSDavid Gibson 
210048706d9SDavid Gibson         /* This will make sure qemu state is up to date with kvm, and
211048706d9SDavid Gibson          * mark it dirty so our changes get flushed back before the
212048706d9SDavid Gibson          * new cpu enters */
213dd1750d7SAndreas Färber         kvm_cpu_synchronize_state(cs);
214048706d9SDavid Gibson 
215a9f8ad8fSDavid Gibson         env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
216a9f8ad8fSDavid Gibson         env->nip = start;
217a9f8ad8fSDavid Gibson         env->gpr[3] = r3;
218c67e216bSAndreas Färber         cs->halted = 0;
219af81cf32SBharata B Rao         spapr_cpu_set_endianness(cpu);
220af81cf32SBharata B Rao         spapr_cpu_update_tb_offset(cpu);
221a9f8ad8fSDavid Gibson 
222c67e216bSAndreas Färber         qemu_cpu_kick(cs);
223a9f8ad8fSDavid Gibson 
224a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
225a9f8ad8fSDavid Gibson         return;
226a9f8ad8fSDavid Gibson     }
227a9f8ad8fSDavid Gibson 
228a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
229a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
230a9f8ad8fSDavid Gibson }
231a9f8ad8fSDavid Gibson 
23228e02042SDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
23359760f2dSAlexey Kardashevskiy                            uint32_t token, uint32_t nargs,
23459760f2dSAlexey Kardashevskiy                            target_ulong args,
23559760f2dSAlexey Kardashevskiy                            uint32_t nret, target_ulong rets)
23659760f2dSAlexey Kardashevskiy {
23759760f2dSAlexey Kardashevskiy     CPUState *cs = CPU(cpu);
23859760f2dSAlexey Kardashevskiy     CPUPPCState *env = &cpu->env;
23959760f2dSAlexey Kardashevskiy 
24059760f2dSAlexey Kardashevskiy     cs->halted = 1;
2419102dedaSPaolo Bonzini     qemu_cpu_kick(cs);
24259760f2dSAlexey Kardashevskiy     /*
24359760f2dSAlexey Kardashevskiy      * While stopping a CPU, the guest calls H_CPPR which
24459760f2dSAlexey Kardashevskiy      * effectively disables interrupts on XICS level.
24559760f2dSAlexey Kardashevskiy      * However decrementer interrupts in TCG can still
24659760f2dSAlexey Kardashevskiy      * wake the CPU up so here we disable interrupts in MSR
24759760f2dSAlexey Kardashevskiy      * as well.
24859760f2dSAlexey Kardashevskiy      * As rtas_start_cpu() resets the whole MSR anyway, there is
24959760f2dSAlexey Kardashevskiy      * no need to bother with specific bits, we just clear it.
25059760f2dSAlexey Kardashevskiy      */
25159760f2dSAlexey Kardashevskiy     env->msr = 0;
25259760f2dSAlexey Kardashevskiy }
25359760f2dSAlexey Kardashevskiy 
254c920f7b4SDavid Gibson static inline int sysparm_st(target_ulong addr, target_ulong len,
255c920f7b4SDavid Gibson                              const void *val, uint16_t vallen)
256c920f7b4SDavid Gibson {
257c920f7b4SDavid Gibson     hwaddr phys = ppc64_phys_to_real(addr);
258c920f7b4SDavid Gibson 
259c920f7b4SDavid Gibson     if (len < 2) {
260c920f7b4SDavid Gibson         return RTAS_OUT_SYSPARM_PARAM_ERROR;
261c920f7b4SDavid Gibson     }
262c920f7b4SDavid Gibson     stw_be_phys(&address_space_memory, phys, vallen);
263c920f7b4SDavid Gibson     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
264c920f7b4SDavid Gibson     return RTAS_OUT_SUCCESS;
265c920f7b4SDavid Gibson }
266c920f7b4SDavid Gibson 
2673ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
26828e02042SDavid Gibson                                           sPAPRMachineState *spapr,
2693ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2703ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2713ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2723ada6b11SAlexey Kardashevskiy {
2733ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2743ada6b11SAlexey Kardashevskiy     target_ulong buffer = rtas_ld(args, 1);
2753ada6b11SAlexey Kardashevskiy     target_ulong length = rtas_ld(args, 2);
276c920f7b4SDavid Gibson     target_ulong ret;
2773ada6b11SAlexey Kardashevskiy 
2783ada6b11SAlexey Kardashevskiy     switch (parameter) {
2793b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
280e3943228SSam Bobroff         char *param_val = g_strdup_printf("MaxEntCap=%d,"
281e3943228SSam Bobroff                                           "DesMem=%llu,"
282e3943228SSam Bobroff                                           "DesProcs=%d,"
283e3943228SSam Bobroff                                           "MaxPlatProcs=%d",
284e3943228SSam Bobroff                                           max_cpus,
285e3943228SSam Bobroff                                           current_machine->ram_size / M_BYTE,
286e3943228SSam Bobroff                                           smp_cpus,
287e3943228SSam Bobroff                                           max_cpus);
288c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
2893b50d897SSam bobroff         g_free(param_val);
2903b50d897SSam bobroff         break;
2913b50d897SSam bobroff     }
2923052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
2933052d951SSam bobroff         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
2943052d951SSam bobroff 
295c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
2963ada6b11SAlexey Kardashevskiy         break;
2973ada6b11SAlexey Kardashevskiy     }
298b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2999c5ce8dbSFam Zheng         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
3009c5ce8dbSFam Zheng                          (qemu_uuid_set ? 16 : 0));
301b907d7b0SSam bobroff         break;
3023052d951SSam bobroff     default:
3033052d951SSam bobroff         ret = RTAS_OUT_NOT_SUPPORTED;
3043052d951SSam bobroff     }
3053ada6b11SAlexey Kardashevskiy 
3063ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
3073ada6b11SAlexey Kardashevskiy }
3083ada6b11SAlexey Kardashevskiy 
3093ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
31028e02042SDavid Gibson                                           sPAPRMachineState *spapr,
3113ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
3123ada6b11SAlexey Kardashevskiy                                           target_ulong args,
3133ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
3143ada6b11SAlexey Kardashevskiy {
3153ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
3163ada6b11SAlexey Kardashevskiy     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
3173ada6b11SAlexey Kardashevskiy 
3183ada6b11SAlexey Kardashevskiy     switch (parameter) {
3193b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
3203052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
321b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
3223ada6b11SAlexey Kardashevskiy         ret = RTAS_OUT_NOT_AUTHORIZED;
3233ada6b11SAlexey Kardashevskiy         break;
3243ada6b11SAlexey Kardashevskiy     }
3253ada6b11SAlexey Kardashevskiy 
3263ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
3273ada6b11SAlexey Kardashevskiy }
3283ada6b11SAlexey Kardashevskiy 
3292e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu,
33028e02042SDavid Gibson                             sPAPRMachineState *spapr,
3312e14072fSNikunj A Dadhania                             uint32_t token, uint32_t nargs,
3322e14072fSNikunj A Dadhania                             target_ulong args,
3332e14072fSNikunj A Dadhania                             uint32_t nret, target_ulong rets)
3342e14072fSNikunj A Dadhania {
3352e14072fSNikunj A Dadhania     target_ulong ret = 0;
3362e14072fSNikunj A Dadhania 
337c86f106bSAnton Nefedov     qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, false, NULL,
338c86f106bSAnton Nefedov                                    &error_abort);
3392e14072fSNikunj A Dadhania 
3402e14072fSNikunj A Dadhania     rtas_st(rets, 0, ret);
3412e14072fSNikunj A Dadhania }
3422e14072fSNikunj A Dadhania 
34328e02042SDavid Gibson static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
344094d2058SNathan Fontenot                                  uint32_t token, uint32_t nargs,
345094d2058SNathan Fontenot                                  target_ulong args, uint32_t nret,
346094d2058SNathan Fontenot                                  target_ulong rets)
347094d2058SNathan Fontenot {
348094d2058SNathan Fontenot     int32_t power_domain;
349094d2058SNathan Fontenot 
350094d2058SNathan Fontenot     if (nargs != 2 || nret != 2) {
351094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
352094d2058SNathan Fontenot         return;
353094d2058SNathan Fontenot     }
354094d2058SNathan Fontenot 
355094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
356094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
357094d2058SNathan Fontenot      */
358094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
359094d2058SNathan Fontenot     if (power_domain != -1) {
360094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
361094d2058SNathan Fontenot         return;
362094d2058SNathan Fontenot     }
363094d2058SNathan Fontenot 
364094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
365094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
366094d2058SNathan Fontenot }
367094d2058SNathan Fontenot 
36828e02042SDavid Gibson static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
369094d2058SNathan Fontenot                                   uint32_t token, uint32_t nargs,
370094d2058SNathan Fontenot                                   target_ulong args, uint32_t nret,
371094d2058SNathan Fontenot                                   target_ulong rets)
372094d2058SNathan Fontenot {
373094d2058SNathan Fontenot     int32_t power_domain;
374094d2058SNathan Fontenot 
375094d2058SNathan Fontenot     if (nargs != 1 || nret != 2) {
376094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
377094d2058SNathan Fontenot         return;
378094d2058SNathan Fontenot     }
379094d2058SNathan Fontenot 
380094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
381094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
382094d2058SNathan Fontenot      */
383094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
384094d2058SNathan Fontenot     if (power_domain != -1) {
385094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
386094d2058SNathan Fontenot         return;
387094d2058SNathan Fontenot     }
388094d2058SNathan Fontenot 
389094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
390094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
391094d2058SNathan Fontenot }
392094d2058SNathan Fontenot 
3938c8639dfSMike Day static bool sensor_type_is_dr(uint32_t sensor_type)
3948c8639dfSMike Day {
3958c8639dfSMike Day     switch (sensor_type) {
3968c8639dfSMike Day     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
3978c8639dfSMike Day     case RTAS_SENSOR_TYPE_DR:
3988c8639dfSMike Day     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
3998c8639dfSMike Day         return true;
4008c8639dfSMike Day     }
4018c8639dfSMike Day 
4028c8639dfSMike Day     return false;
4038c8639dfSMike Day }
4048c8639dfSMike Day 
40528e02042SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
4068c8639dfSMike Day                                uint32_t token, uint32_t nargs,
4078c8639dfSMike Day                                target_ulong args, uint32_t nret,
4088c8639dfSMike Day                                target_ulong rets)
4098c8639dfSMike Day {
4108c8639dfSMike Day     uint32_t sensor_type;
4118c8639dfSMike Day     uint32_t sensor_index;
4128c8639dfSMike Day     uint32_t sensor_state;
4130cb688d2SMichael Roth     uint32_t ret = RTAS_OUT_SUCCESS;
4148c8639dfSMike Day     sPAPRDRConnector *drc;
4158c8639dfSMike Day     sPAPRDRConnectorClass *drck;
4168c8639dfSMike Day 
4178c8639dfSMike Day     if (nargs != 3 || nret != 1) {
4180cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
4190cb688d2SMichael Roth         goto out;
4208c8639dfSMike Day     }
4218c8639dfSMike Day 
4228c8639dfSMike Day     sensor_type = rtas_ld(args, 0);
4238c8639dfSMike Day     sensor_index = rtas_ld(args, 1);
4248c8639dfSMike Day     sensor_state = rtas_ld(args, 2);
4258c8639dfSMike Day 
4268c8639dfSMike Day     if (!sensor_type_is_dr(sensor_type)) {
4278c8639dfSMike Day         goto out_unimplemented;
4288c8639dfSMike Day     }
4298c8639dfSMike Day 
4308c8639dfSMike Day     /* if this is a DR sensor we can assume sensor_index == drc_index */
4318c8639dfSMike Day     drc = spapr_dr_connector_by_index(sensor_index);
4328c8639dfSMike Day     if (!drc) {
433028ec3ceSLaurent Vivier         trace_spapr_rtas_set_indicator_invalid(sensor_index);
4340cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
4350cb688d2SMichael Roth         goto out;
4368c8639dfSMike Day     }
4378c8639dfSMike Day     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
4388c8639dfSMike Day 
4398c8639dfSMike Day     switch (sensor_type) {
4408c8639dfSMike Day     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
44146503c2bSMichael Roth         /* if the guest is configuring a device attached to this
44246503c2bSMichael Roth          * DRC, we should reset the configuration state at this
44346503c2bSMichael Roth          * point since it may no longer be reliable (guest released
44446503c2bSMichael Roth          * device and needs to start over, or unplug occurred so
44546503c2bSMichael Roth          * the FDT is no longer valid)
44646503c2bSMichael Roth          */
44746503c2bSMichael Roth         if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
44846503c2bSMichael Roth             sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
44946503c2bSMichael Roth                                                                sensor_index);
45046503c2bSMichael Roth             if (ccs) {
45146503c2bSMichael Roth                 spapr_ccs_remove(spapr, ccs);
45246503c2bSMichael Roth             }
45346503c2bSMichael Roth         }
4540cb688d2SMichael Roth         ret = drck->set_isolation_state(drc, sensor_state);
4558c8639dfSMike Day         break;
4568c8639dfSMike Day     case RTAS_SENSOR_TYPE_DR:
4570cb688d2SMichael Roth         ret = drck->set_indicator_state(drc, sensor_state);
4588c8639dfSMike Day         break;
4598c8639dfSMike Day     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
4600cb688d2SMichael Roth         ret = drck->set_allocation_state(drc, sensor_state);
4618c8639dfSMike Day         break;
4628c8639dfSMike Day     default:
4638c8639dfSMike Day         goto out_unimplemented;
4648c8639dfSMike Day     }
4658c8639dfSMike Day 
4660cb688d2SMichael Roth out:
4670cb688d2SMichael Roth     rtas_st(rets, 0, ret);
4688c8639dfSMike Day     return;
4698c8639dfSMike Day 
4708c8639dfSMike Day out_unimplemented:
4718c8639dfSMike Day     /* currently only DR-related sensors are implemented */
472028ec3ceSLaurent Vivier     trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
4738c8639dfSMike Day     rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
4748c8639dfSMike Day }
4758c8639dfSMike Day 
47628e02042SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
477886445a6SMike Day                                   uint32_t token, uint32_t nargs,
478886445a6SMike Day                                   target_ulong args, uint32_t nret,
479886445a6SMike Day                                   target_ulong rets)
480886445a6SMike Day {
481886445a6SMike Day     uint32_t sensor_type;
482886445a6SMike Day     uint32_t sensor_index;
4830cb688d2SMichael Roth     uint32_t sensor_state = 0;
484886445a6SMike Day     sPAPRDRConnector *drc;
485886445a6SMike Day     sPAPRDRConnectorClass *drck;
4860cb688d2SMichael Roth     uint32_t ret = RTAS_OUT_SUCCESS;
487886445a6SMike Day 
488886445a6SMike Day     if (nargs != 2 || nret != 2) {
4890cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
4900cb688d2SMichael Roth         goto out;
491886445a6SMike Day     }
492886445a6SMike Day 
493886445a6SMike Day     sensor_type = rtas_ld(args, 0);
494886445a6SMike Day     sensor_index = rtas_ld(args, 1);
495886445a6SMike Day 
496886445a6SMike Day     if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
497886445a6SMike Day         /* currently only DR-related sensors are implemented */
498028ec3ceSLaurent Vivier         trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
499886445a6SMike Day                                                         sensor_type);
5000cb688d2SMichael Roth         ret = RTAS_OUT_NOT_SUPPORTED;
5010cb688d2SMichael Roth         goto out;
502886445a6SMike Day     }
503886445a6SMike Day 
504886445a6SMike Day     drc = spapr_dr_connector_by_index(sensor_index);
505886445a6SMike Day     if (!drc) {
506028ec3ceSLaurent Vivier         trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
5070cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
5080cb688d2SMichael Roth         goto out;
509886445a6SMike Day     }
510886445a6SMike Day     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
5110cb688d2SMichael Roth     ret = drck->entity_sense(drc, &sensor_state);
512886445a6SMike Day 
5130cb688d2SMichael Roth out:
5140cb688d2SMichael Roth     rtas_st(rets, 0, ret);
5150cb688d2SMichael Roth     rtas_st(rets, 1, sensor_state);
516886445a6SMike Day }
517886445a6SMike Day 
51846503c2bSMichael Roth /* configure-connector work area offsets, int32_t units for field
51946503c2bSMichael Roth  * indexes, bytes for field offset/len values.
52046503c2bSMichael Roth  *
52146503c2bSMichael Roth  * as documented by PAPR+ v2.7, 13.5.3.5
52246503c2bSMichael Roth  */
52346503c2bSMichael Roth #define CC_IDX_NODE_NAME_OFFSET 2
52446503c2bSMichael Roth #define CC_IDX_PROP_NAME_OFFSET 2
52546503c2bSMichael Roth #define CC_IDX_PROP_LEN 3
52646503c2bSMichael Roth #define CC_IDX_PROP_DATA_OFFSET 4
52746503c2bSMichael Roth #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
52846503c2bSMichael Roth #define CC_WA_LEN 4096
52946503c2bSMichael Roth 
530f201987bSDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset,
531f201987bSDavid Gibson                                    const void *buf, size_t len)
532f201987bSDavid Gibson {
533f201987bSDavid Gibson     cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
534f201987bSDavid Gibson                               buf, MIN(len, CC_WA_LEN - offset));
535f201987bSDavid Gibson }
536f201987bSDavid Gibson 
53746503c2bSMichael Roth static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
53828e02042SDavid Gibson                                          sPAPRMachineState *spapr,
53946503c2bSMichael Roth                                          uint32_t token, uint32_t nargs,
54046503c2bSMichael Roth                                          target_ulong args, uint32_t nret,
54146503c2bSMichael Roth                                          target_ulong rets)
54246503c2bSMichael Roth {
54346503c2bSMichael Roth     uint64_t wa_addr;
54446503c2bSMichael Roth     uint64_t wa_offset;
54546503c2bSMichael Roth     uint32_t drc_index;
54646503c2bSMichael Roth     sPAPRDRConnector *drc;
54746503c2bSMichael Roth     sPAPRDRConnectorClass *drck;
54846503c2bSMichael Roth     sPAPRConfigureConnectorState *ccs;
54946503c2bSMichael Roth     sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
55046503c2bSMichael Roth     int rc;
55146503c2bSMichael Roth     const void *fdt;
55246503c2bSMichael Roth 
55346503c2bSMichael Roth     if (nargs != 2 || nret != 1) {
55446503c2bSMichael Roth         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
55546503c2bSMichael Roth         return;
55646503c2bSMichael Roth     }
55746503c2bSMichael Roth 
55846503c2bSMichael Roth     wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
55946503c2bSMichael Roth 
56046503c2bSMichael Roth     drc_index = rtas_ld(wa_addr, 0);
56146503c2bSMichael Roth     drc = spapr_dr_connector_by_index(drc_index);
56246503c2bSMichael Roth     if (!drc) {
563028ec3ceSLaurent Vivier         trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
56446503c2bSMichael Roth         rc = RTAS_OUT_PARAM_ERROR;
56546503c2bSMichael Roth         goto out;
56646503c2bSMichael Roth     }
56746503c2bSMichael Roth 
56846503c2bSMichael Roth     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
56946503c2bSMichael Roth     fdt = drck->get_fdt(drc, NULL);
570e6fc9568SBharata B Rao     if (!fdt) {
571028ec3ceSLaurent Vivier         trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
572e6fc9568SBharata B Rao         rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
573e6fc9568SBharata B Rao         goto out;
574e6fc9568SBharata B Rao     }
57546503c2bSMichael Roth 
57646503c2bSMichael Roth     ccs = spapr_ccs_find(spapr, drc_index);
57746503c2bSMichael Roth     if (!ccs) {
57846503c2bSMichael Roth         ccs = g_new0(sPAPRConfigureConnectorState, 1);
57946503c2bSMichael Roth         (void)drck->get_fdt(drc, &ccs->fdt_offset);
58046503c2bSMichael Roth         ccs->drc_index = drc_index;
58146503c2bSMichael Roth         spapr_ccs_add(spapr, ccs);
58246503c2bSMichael Roth     }
58346503c2bSMichael Roth 
58446503c2bSMichael Roth     do {
58546503c2bSMichael Roth         uint32_t tag;
58646503c2bSMichael Roth         const char *name;
58746503c2bSMichael Roth         const struct fdt_property *prop;
58846503c2bSMichael Roth         int fdt_offset_next, prop_len;
58946503c2bSMichael Roth 
59046503c2bSMichael Roth         tag = fdt_next_tag(fdt, ccs->fdt_offset, &fdt_offset_next);
59146503c2bSMichael Roth 
59246503c2bSMichael Roth         switch (tag) {
59346503c2bSMichael Roth         case FDT_BEGIN_NODE:
59446503c2bSMichael Roth             ccs->fdt_depth++;
59546503c2bSMichael Roth             name = fdt_get_name(fdt, ccs->fdt_offset, NULL);
59646503c2bSMichael Roth 
59746503c2bSMichael Roth             /* provide the name of the next OF node */
59846503c2bSMichael Roth             wa_offset = CC_VAL_DATA_OFFSET;
59946503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
600f201987bSDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
60146503c2bSMichael Roth             resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
60246503c2bSMichael Roth             break;
60346503c2bSMichael Roth         case FDT_END_NODE:
60446503c2bSMichael Roth             ccs->fdt_depth--;
60546503c2bSMichael Roth             if (ccs->fdt_depth == 0) {
60646503c2bSMichael Roth                 /* done sending the device tree, don't need to track
60746503c2bSMichael Roth                  * the state anymore
60846503c2bSMichael Roth                  */
60946503c2bSMichael Roth                 drck->set_configured(drc);
61046503c2bSMichael Roth                 spapr_ccs_remove(spapr, ccs);
61146503c2bSMichael Roth                 ccs = NULL;
61246503c2bSMichael Roth                 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
61346503c2bSMichael Roth             } else {
61446503c2bSMichael Roth                 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
61546503c2bSMichael Roth             }
61646503c2bSMichael Roth             break;
61746503c2bSMichael Roth         case FDT_PROP:
61846503c2bSMichael Roth             prop = fdt_get_property_by_offset(fdt, ccs->fdt_offset,
61946503c2bSMichael Roth                                               &prop_len);
62046503c2bSMichael Roth             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
62146503c2bSMichael Roth 
62246503c2bSMichael Roth             /* provide the name of the next OF property */
62346503c2bSMichael Roth             wa_offset = CC_VAL_DATA_OFFSET;
62446503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
625f201987bSDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
62646503c2bSMichael Roth 
62746503c2bSMichael Roth             /* provide the length and value of the OF property. data gets
62846503c2bSMichael Roth              * placed immediately after NULL terminator of the OF property's
62946503c2bSMichael Roth              * name string
63046503c2bSMichael Roth              */
63146503c2bSMichael Roth             wa_offset += strlen(name) + 1,
63246503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
63346503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
634f201987bSDavid Gibson             configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
63546503c2bSMichael Roth             resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
63646503c2bSMichael Roth             break;
63746503c2bSMichael Roth         case FDT_END:
63846503c2bSMichael Roth             resp = SPAPR_DR_CC_RESPONSE_ERROR;
63946503c2bSMichael Roth         default:
64046503c2bSMichael Roth             /* keep seeking for an actionable tag */
64146503c2bSMichael Roth             break;
64246503c2bSMichael Roth         }
64346503c2bSMichael Roth         if (ccs) {
64446503c2bSMichael Roth             ccs->fdt_offset = fdt_offset_next;
64546503c2bSMichael Roth         }
64646503c2bSMichael Roth     } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
64746503c2bSMichael Roth 
64846503c2bSMichael Roth     rc = resp;
64946503c2bSMichael Roth out:
65046503c2bSMichael Roth     rtas_st(rets, 0, rc);
65146503c2bSMichael Roth }
65246503c2bSMichael Roth 
65339ac8455SDavid Gibson static struct rtas_call {
65439ac8455SDavid Gibson     const char *name;
65539ac8455SDavid Gibson     spapr_rtas_fn fn;
6563a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
65739ac8455SDavid Gibson 
65828e02042SDavid Gibson target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
65939ac8455SDavid Gibson                              uint32_t token, uint32_t nargs, target_ulong args,
66039ac8455SDavid Gibson                              uint32_t nret, target_ulong rets)
66139ac8455SDavid Gibson {
6623a3b8502SAlexey Kardashevskiy     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
6633a3b8502SAlexey Kardashevskiy         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
66439ac8455SDavid Gibson 
66539ac8455SDavid Gibson         if (call->fn) {
666210b580bSAnthony Liguori             call->fn(cpu, spapr, token, nargs, args, nret, rets);
66739ac8455SDavid Gibson             return H_SUCCESS;
66839ac8455SDavid Gibson         }
66939ac8455SDavid Gibson     }
67039ac8455SDavid Gibson 
671821303f5SDavid Gibson     /* HACK: Some Linux early debug code uses RTAS display-character,
672821303f5SDavid Gibson      * but assumes the token value is 0xa (which it is on some real
673821303f5SDavid Gibson      * machines) without looking it up in the device tree.  This
674821303f5SDavid Gibson      * special case makes this work */
675821303f5SDavid Gibson     if (token == 0xa) {
676210b580bSAnthony Liguori         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
677821303f5SDavid Gibson         return H_SUCCESS;
678821303f5SDavid Gibson     }
679821303f5SDavid Gibson 
68039ac8455SDavid Gibson     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
681a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
68239ac8455SDavid Gibson     return H_PARAMETER;
68339ac8455SDavid Gibson }
68439ac8455SDavid Gibson 
685eeddd59fSLaurent Vivier uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
686eeddd59fSLaurent Vivier                          uint32_t nret, uint64_t rets)
687eeddd59fSLaurent Vivier {
688eeddd59fSLaurent Vivier     int token;
689eeddd59fSLaurent Vivier 
690eeddd59fSLaurent Vivier     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
691eeddd59fSLaurent Vivier         if (strcmp(cmd, rtas_table[token].name) == 0) {
692eeddd59fSLaurent Vivier             sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
693eeddd59fSLaurent Vivier             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
694eeddd59fSLaurent Vivier 
695eeddd59fSLaurent Vivier             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
696eeddd59fSLaurent Vivier                                  nargs, args, nret, rets);
697eeddd59fSLaurent Vivier             return H_SUCCESS;
698eeddd59fSLaurent Vivier         }
699eeddd59fSLaurent Vivier     }
700eeddd59fSLaurent Vivier     return H_PARAMETER;
701eeddd59fSLaurent Vivier }
702eeddd59fSLaurent Vivier 
7033a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
70439ac8455SDavid Gibson {
705adf9ac50SDavid Gibson     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
7063a3b8502SAlexey Kardashevskiy 
7073a3b8502SAlexey Kardashevskiy     token -= RTAS_TOKEN_BASE;
708adf9ac50SDavid Gibson 
709adf9ac50SDavid Gibson     assert(!rtas_table[token].name);
710c89d5299SDavid Gibson 
7113a3b8502SAlexey Kardashevskiy     rtas_table[token].name = name;
7123a3b8502SAlexey Kardashevskiy     rtas_table[token].fn = fn;
71339ac8455SDavid Gibson }
71439ac8455SDavid Gibson 
7153f5dabceSDavid Gibson void spapr_dt_rtas_tokens(void *fdt, int rtas)
71639ac8455SDavid Gibson {
71739ac8455SDavid Gibson     int i;
71839ac8455SDavid Gibson 
7193a3b8502SAlexey Kardashevskiy     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
72039ac8455SDavid Gibson         struct rtas_call *call = &rtas_table[i];
72139ac8455SDavid Gibson 
722d36b66f7SBen Herrenschmidt         if (!call->name) {
72339ac8455SDavid Gibson             continue;
72439ac8455SDavid Gibson         }
72539ac8455SDavid Gibson 
7263f5dabceSDavid Gibson         _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE));
72739ac8455SDavid Gibson     }
72839ac8455SDavid Gibson }
729821303f5SDavid Gibson 
7302cac78c1SDavid Gibson void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr)
7312cac78c1SDavid Gibson {
7322cac78c1SDavid Gibson     int rtas_node;
7332cac78c1SDavid Gibson     int ret;
7342cac78c1SDavid Gibson 
7352cac78c1SDavid Gibson     /* Copy RTAS blob into guest RAM */
7362cac78c1SDavid Gibson     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
7372cac78c1SDavid Gibson 
7382cac78c1SDavid Gibson     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
7392cac78c1SDavid Gibson     if (ret < 0) {
7402cac78c1SDavid Gibson         error_report("Couldn't add RTAS reserve entry: %s",
7412cac78c1SDavid Gibson                      fdt_strerror(ret));
7422cac78c1SDavid Gibson         exit(1);
7432cac78c1SDavid Gibson     }
7442cac78c1SDavid Gibson 
7452cac78c1SDavid Gibson     /* Update the device tree with the blob's location */
7462cac78c1SDavid Gibson     rtas_node = fdt_path_offset(fdt, "/rtas");
7472cac78c1SDavid Gibson     assert(rtas_node >= 0);
7482cac78c1SDavid Gibson 
7492cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
7502cac78c1SDavid Gibson     if (ret < 0) {
7512cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-base property: %s",
7522cac78c1SDavid Gibson                      fdt_strerror(ret));
7532cac78c1SDavid Gibson         exit(1);
7542cac78c1SDavid Gibson     }
7552cac78c1SDavid Gibson 
7562cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
7572cac78c1SDavid Gibson     if (ret < 0) {
7582cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-entry property: %s",
7592cac78c1SDavid Gibson                      fdt_strerror(ret));
7602cac78c1SDavid Gibson         exit(1);
7612cac78c1SDavid Gibson     }
7622cac78c1SDavid Gibson 
7632cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
7642cac78c1SDavid Gibson     if (ret < 0) {
7652cac78c1SDavid Gibson         error_report("Couldn't add rtas-size property: %s",
7662cac78c1SDavid Gibson                      fdt_strerror(ret));
7672cac78c1SDavid Gibson         exit(1);
7682cac78c1SDavid Gibson     }
7692cac78c1SDavid Gibson }
7702cac78c1SDavid Gibson 
77183f7d43aSAndreas Färber static void core_rtas_register_types(void)
772821303f5SDavid Gibson {
7733a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
7743a3b8502SAlexey Kardashevskiy                         rtas_display_character);
7753a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
7763a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
7773a3b8502SAlexey Kardashevskiy                         rtas_system_reboot);
7783a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
779a9f8ad8fSDavid Gibson                         rtas_query_cpu_stopped_state);
7803a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
7813a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
7823a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
7833a3b8502SAlexey Kardashevskiy                         "ibm,get-system-parameter",
7843ada6b11SAlexey Kardashevskiy                         rtas_ibm_get_system_parameter);
7853a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
7863a3b8502SAlexey Kardashevskiy                         "ibm,set-system-parameter",
7873ada6b11SAlexey Kardashevskiy                         rtas_ibm_set_system_parameter);
7882e14072fSNikunj A Dadhania     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
7892e14072fSNikunj A Dadhania                         rtas_ibm_os_term);
790094d2058SNathan Fontenot     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
791094d2058SNathan Fontenot                         rtas_set_power_level);
792094d2058SNathan Fontenot     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
793094d2058SNathan Fontenot                         rtas_get_power_level);
7948c8639dfSMike Day     spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
7958c8639dfSMike Day                         rtas_set_indicator);
796886445a6SMike Day     spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
797886445a6SMike Day                         rtas_get_sensor_state);
79846503c2bSMichael Roth     spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
79946503c2bSMichael Roth                         rtas_ibm_configure_connector);
800821303f5SDavid Gibson }
80183f7d43aSAndreas Färber 
80283f7d43aSAndreas Färber type_init(core_rtas_register_types)
803