xref: /qemu/hw/ppc/spapr_rtas.c (revision 2cac78c12ade9a87b6251f8d854c2e43a30f41bf)
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"
498c8639dfSMike Day 
5028e02042SDavid Gibson static sPAPRConfigureConnectorState *spapr_ccs_find(sPAPRMachineState *spapr,
5146503c2bSMichael Roth                                                     uint32_t drc_index)
5246503c2bSMichael Roth {
5346503c2bSMichael Roth     sPAPRConfigureConnectorState *ccs = NULL;
5446503c2bSMichael Roth 
5546503c2bSMichael Roth     QTAILQ_FOREACH(ccs, &spapr->ccs_list, next) {
5646503c2bSMichael Roth         if (ccs->drc_index == drc_index) {
5746503c2bSMichael Roth             break;
5846503c2bSMichael Roth         }
5946503c2bSMichael Roth     }
6046503c2bSMichael Roth 
6146503c2bSMichael Roth     return ccs;
6246503c2bSMichael Roth }
6346503c2bSMichael Roth 
6428e02042SDavid Gibson static void spapr_ccs_add(sPAPRMachineState *spapr,
6546503c2bSMichael Roth                           sPAPRConfigureConnectorState *ccs)
6646503c2bSMichael Roth {
6746503c2bSMichael Roth     g_assert(!spapr_ccs_find(spapr, ccs->drc_index));
6846503c2bSMichael Roth     QTAILQ_INSERT_HEAD(&spapr->ccs_list, ccs, next);
6946503c2bSMichael Roth }
7046503c2bSMichael Roth 
7128e02042SDavid Gibson static void spapr_ccs_remove(sPAPRMachineState *spapr,
7246503c2bSMichael Roth                              sPAPRConfigureConnectorState *ccs)
7346503c2bSMichael Roth {
7446503c2bSMichael Roth     QTAILQ_REMOVE(&spapr->ccs_list, ccs, next);
7546503c2bSMichael Roth     g_free(ccs);
7646503c2bSMichael Roth }
7746503c2bSMichael Roth 
7846503c2bSMichael Roth void spapr_ccs_reset_hook(void *opaque)
7946503c2bSMichael Roth {
8028e02042SDavid Gibson     sPAPRMachineState *spapr = opaque;
8146503c2bSMichael Roth     sPAPRConfigureConnectorState *ccs, *ccs_tmp;
8246503c2bSMichael Roth 
8346503c2bSMichael Roth     QTAILQ_FOREACH_SAFE(ccs, &spapr->ccs_list, next, ccs_tmp) {
8446503c2bSMichael Roth         spapr_ccs_remove(spapr, ccs);
8546503c2bSMichael Roth     }
8646503c2bSMichael Roth }
8739ac8455SDavid Gibson 
8828e02042SDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr,
89821303f5SDavid Gibson                                    uint32_t token, uint32_t nargs,
90821303f5SDavid Gibson                                    target_ulong args,
91821303f5SDavid Gibson                                    uint32_t nret, target_ulong rets)
92821303f5SDavid Gibson {
93821303f5SDavid Gibson     uint8_t c = rtas_ld(args, 0);
945f2e2ba2SDavid Gibson     VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
95821303f5SDavid Gibson 
96821303f5SDavid Gibson     if (!sdev) {
97a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
98821303f5SDavid Gibson     } else {
99821303f5SDavid Gibson         vty_putchars(sdev, &c, sizeof(c));
100a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
101821303f5SDavid Gibson     }
102821303f5SDavid Gibson }
103821303f5SDavid Gibson 
10428e02042SDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr,
105821303f5SDavid Gibson                            uint32_t token, uint32_t nargs, target_ulong args,
106821303f5SDavid Gibson                            uint32_t nret, target_ulong rets)
107821303f5SDavid Gibson {
108821303f5SDavid Gibson     if (nargs != 2 || nret != 1) {
109a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
110821303f5SDavid Gibson         return;
111821303f5SDavid Gibson     }
112821303f5SDavid Gibson     qemu_system_shutdown_request();
1138a9c1b77SThomas Huth     cpu_stop_current();
114a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
115821303f5SDavid Gibson }
116821303f5SDavid Gibson 
11728e02042SDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr,
118c821a43cSDavid Gibson                                uint32_t token, uint32_t nargs,
119c821a43cSDavid Gibson                                target_ulong args,
120c821a43cSDavid Gibson                                uint32_t nret, target_ulong rets)
121c821a43cSDavid Gibson {
122c821a43cSDavid Gibson     if (nargs != 0 || nret != 1) {
123a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
124c821a43cSDavid Gibson         return;
125c821a43cSDavid Gibson     }
126c821a43cSDavid Gibson     qemu_system_reset_request();
127a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
128c821a43cSDavid Gibson }
129c821a43cSDavid Gibson 
130210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
13128e02042SDavid Gibson                                          sPAPRMachineState *spapr,
132a9f8ad8fSDavid Gibson                                          uint32_t token, uint32_t nargs,
133a9f8ad8fSDavid Gibson                                          target_ulong args,
134a9f8ad8fSDavid Gibson                                          uint32_t nret, target_ulong rets)
135a9f8ad8fSDavid Gibson {
136a9f8ad8fSDavid Gibson     target_ulong id;
1370f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
138a9f8ad8fSDavid Gibson 
139a9f8ad8fSDavid Gibson     if (nargs != 1 || nret != 2) {
140a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
141a9f8ad8fSDavid Gibson         return;
142a9f8ad8fSDavid Gibson     }
143a9f8ad8fSDavid Gibson 
144a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
1450f20ba62SAlexey Kardashevskiy     cpu = ppc_get_vcpu_by_dt_id(id);
14605318a85SAndreas Färber     if (cpu != NULL) {
1470f20ba62SAlexey Kardashevskiy         if (CPU(cpu)->halted) {
148a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 0);
149a9f8ad8fSDavid Gibson         } else {
150a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 2);
151a9f8ad8fSDavid Gibson         }
152a9f8ad8fSDavid Gibson 
153a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
154a9f8ad8fSDavid Gibson         return;
155a9f8ad8fSDavid Gibson     }
156a9f8ad8fSDavid Gibson 
157a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
158a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
159a9f8ad8fSDavid Gibson }
160a9f8ad8fSDavid Gibson 
161af81cf32SBharata B Rao /*
162af81cf32SBharata B Rao  * Set the timebase offset of the CPU to that of first CPU.
163af81cf32SBharata B Rao  * This helps hotplugged CPU to have the correct timebase offset.
164af81cf32SBharata B Rao  */
165af81cf32SBharata B Rao static void spapr_cpu_update_tb_offset(PowerPCCPU *cpu)
166af81cf32SBharata B Rao {
167af81cf32SBharata B Rao     PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
168af81cf32SBharata B Rao 
169af81cf32SBharata B Rao     cpu->env.tb_env->tb_offset = fcpu->env.tb_env->tb_offset;
170af81cf32SBharata B Rao }
171af81cf32SBharata B Rao 
172af81cf32SBharata B Rao static void spapr_cpu_set_endianness(PowerPCCPU *cpu)
173af81cf32SBharata B Rao {
174af81cf32SBharata B Rao     PowerPCCPU *fcpu = POWERPC_CPU(first_cpu);
175af81cf32SBharata B Rao     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu);
176af81cf32SBharata B Rao 
177af81cf32SBharata B Rao     if (!pcc->interrupts_big_endian(fcpu)) {
178af81cf32SBharata B Rao         cpu->env.spr[SPR_LPCR] |= LPCR_ILE;
179af81cf32SBharata B Rao     }
180af81cf32SBharata B Rao }
181af81cf32SBharata B Rao 
18228e02042SDavid Gibson static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr,
183a9f8ad8fSDavid Gibson                            uint32_t token, uint32_t nargs,
184a9f8ad8fSDavid Gibson                            target_ulong args,
185a9f8ad8fSDavid Gibson                            uint32_t nret, target_ulong rets)
186a9f8ad8fSDavid Gibson {
187a9f8ad8fSDavid Gibson     target_ulong id, start, r3;
1880f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
189a9f8ad8fSDavid Gibson 
190a9f8ad8fSDavid Gibson     if (nargs != 3 || nret != 1) {
191a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
192a9f8ad8fSDavid Gibson         return;
193a9f8ad8fSDavid Gibson     }
194a9f8ad8fSDavid Gibson 
195a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
196a9f8ad8fSDavid Gibson     start = rtas_ld(args, 1);
197a9f8ad8fSDavid Gibson     r3 = rtas_ld(args, 2);
198a9f8ad8fSDavid Gibson 
1990f20ba62SAlexey Kardashevskiy     cpu = ppc_get_vcpu_by_dt_id(id);
2000f20ba62SAlexey Kardashevskiy     if (cpu != NULL) {
2010f20ba62SAlexey Kardashevskiy         CPUState *cs = CPU(cpu);
202c67e216bSAndreas Färber         CPUPPCState *env = &cpu->env;
203c08d7424SAndreas Färber 
204c67e216bSAndreas Färber         if (!cs->halted) {
205a64d325dSAlexey Kardashevskiy             rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
206a9f8ad8fSDavid Gibson             return;
207a9f8ad8fSDavid Gibson         }
208a9f8ad8fSDavid Gibson 
209048706d9SDavid Gibson         /* This will make sure qemu state is up to date with kvm, and
210048706d9SDavid Gibson          * mark it dirty so our changes get flushed back before the
211048706d9SDavid Gibson          * new cpu enters */
212dd1750d7SAndreas Färber         kvm_cpu_synchronize_state(cs);
213048706d9SDavid Gibson 
214a9f8ad8fSDavid Gibson         env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
215a9f8ad8fSDavid Gibson         env->nip = start;
216a9f8ad8fSDavid Gibson         env->gpr[3] = r3;
217c67e216bSAndreas Färber         cs->halted = 0;
218af81cf32SBharata B Rao         spapr_cpu_set_endianness(cpu);
219af81cf32SBharata B Rao         spapr_cpu_update_tb_offset(cpu);
220a9f8ad8fSDavid Gibson 
221c67e216bSAndreas Färber         qemu_cpu_kick(cs);
222a9f8ad8fSDavid Gibson 
223a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
224a9f8ad8fSDavid Gibson         return;
225a9f8ad8fSDavid Gibson     }
226a9f8ad8fSDavid Gibson 
227a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
228a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
229a9f8ad8fSDavid Gibson }
230a9f8ad8fSDavid Gibson 
23128e02042SDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr,
23259760f2dSAlexey Kardashevskiy                            uint32_t token, uint32_t nargs,
23359760f2dSAlexey Kardashevskiy                            target_ulong args,
23459760f2dSAlexey Kardashevskiy                            uint32_t nret, target_ulong rets)
23559760f2dSAlexey Kardashevskiy {
23659760f2dSAlexey Kardashevskiy     CPUState *cs = CPU(cpu);
23759760f2dSAlexey Kardashevskiy     CPUPPCState *env = &cpu->env;
23859760f2dSAlexey Kardashevskiy 
23959760f2dSAlexey Kardashevskiy     cs->halted = 1;
2409102dedaSPaolo Bonzini     qemu_cpu_kick(cs);
24159760f2dSAlexey Kardashevskiy     /*
24259760f2dSAlexey Kardashevskiy      * While stopping a CPU, the guest calls H_CPPR which
24359760f2dSAlexey Kardashevskiy      * effectively disables interrupts on XICS level.
24459760f2dSAlexey Kardashevskiy      * However decrementer interrupts in TCG can still
24559760f2dSAlexey Kardashevskiy      * wake the CPU up so here we disable interrupts in MSR
24659760f2dSAlexey Kardashevskiy      * as well.
24759760f2dSAlexey Kardashevskiy      * As rtas_start_cpu() resets the whole MSR anyway, there is
24859760f2dSAlexey Kardashevskiy      * no need to bother with specific bits, we just clear it.
24959760f2dSAlexey Kardashevskiy      */
25059760f2dSAlexey Kardashevskiy     env->msr = 0;
25159760f2dSAlexey Kardashevskiy }
25259760f2dSAlexey Kardashevskiy 
253c920f7b4SDavid Gibson static inline int sysparm_st(target_ulong addr, target_ulong len,
254c920f7b4SDavid Gibson                              const void *val, uint16_t vallen)
255c920f7b4SDavid Gibson {
256c920f7b4SDavid Gibson     hwaddr phys = ppc64_phys_to_real(addr);
257c920f7b4SDavid Gibson 
258c920f7b4SDavid Gibson     if (len < 2) {
259c920f7b4SDavid Gibson         return RTAS_OUT_SYSPARM_PARAM_ERROR;
260c920f7b4SDavid Gibson     }
261c920f7b4SDavid Gibson     stw_be_phys(&address_space_memory, phys, vallen);
262c920f7b4SDavid Gibson     cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen));
263c920f7b4SDavid Gibson     return RTAS_OUT_SUCCESS;
264c920f7b4SDavid Gibson }
265c920f7b4SDavid Gibson 
2663ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
26728e02042SDavid Gibson                                           sPAPRMachineState *spapr,
2683ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2693ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2703ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2713ada6b11SAlexey Kardashevskiy {
2723ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2733ada6b11SAlexey Kardashevskiy     target_ulong buffer = rtas_ld(args, 1);
2743ada6b11SAlexey Kardashevskiy     target_ulong length = rtas_ld(args, 2);
275c920f7b4SDavid Gibson     target_ulong ret;
2763ada6b11SAlexey Kardashevskiy 
2773ada6b11SAlexey Kardashevskiy     switch (parameter) {
2783b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
279e3943228SSam Bobroff         char *param_val = g_strdup_printf("MaxEntCap=%d,"
280e3943228SSam Bobroff                                           "DesMem=%llu,"
281e3943228SSam Bobroff                                           "DesProcs=%d,"
282e3943228SSam Bobroff                                           "MaxPlatProcs=%d",
283e3943228SSam Bobroff                                           max_cpus,
284e3943228SSam Bobroff                                           current_machine->ram_size / M_BYTE,
285e3943228SSam Bobroff                                           smp_cpus,
286e3943228SSam Bobroff                                           max_cpus);
287c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1);
2883b50d897SSam bobroff         g_free(param_val);
2893b50d897SSam bobroff         break;
2903b50d897SSam bobroff     }
2913052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
2923052d951SSam bobroff         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
2933052d951SSam bobroff 
294c920f7b4SDavid Gibson         ret = sysparm_st(buffer, length, &param_val, sizeof(param_val));
2953ada6b11SAlexey Kardashevskiy         break;
2963ada6b11SAlexey Kardashevskiy     }
297b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2989c5ce8dbSFam Zheng         ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid,
2999c5ce8dbSFam Zheng                          (qemu_uuid_set ? 16 : 0));
300b907d7b0SSam bobroff         break;
3013052d951SSam bobroff     default:
3023052d951SSam bobroff         ret = RTAS_OUT_NOT_SUPPORTED;
3033052d951SSam bobroff     }
3043ada6b11SAlexey Kardashevskiy 
3053ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
3063ada6b11SAlexey Kardashevskiy }
3073ada6b11SAlexey Kardashevskiy 
3083ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
30928e02042SDavid Gibson                                           sPAPRMachineState *spapr,
3103ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
3113ada6b11SAlexey Kardashevskiy                                           target_ulong args,
3123ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
3133ada6b11SAlexey Kardashevskiy {
3143ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
3153ada6b11SAlexey Kardashevskiy     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
3163ada6b11SAlexey Kardashevskiy 
3173ada6b11SAlexey Kardashevskiy     switch (parameter) {
3183b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
3193052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
320b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
3213ada6b11SAlexey Kardashevskiy         ret = RTAS_OUT_NOT_AUTHORIZED;
3223ada6b11SAlexey Kardashevskiy         break;
3233ada6b11SAlexey Kardashevskiy     }
3243ada6b11SAlexey Kardashevskiy 
3253ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
3263ada6b11SAlexey Kardashevskiy }
3273ada6b11SAlexey Kardashevskiy 
3282e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu,
32928e02042SDavid Gibson                             sPAPRMachineState *spapr,
3302e14072fSNikunj A Dadhania                             uint32_t token, uint32_t nargs,
3312e14072fSNikunj A Dadhania                             target_ulong args,
3322e14072fSNikunj A Dadhania                             uint32_t nret, target_ulong rets)
3332e14072fSNikunj A Dadhania {
3342e14072fSNikunj A Dadhania     target_ulong ret = 0;
3352e14072fSNikunj A Dadhania 
3362e14072fSNikunj A Dadhania     qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
3372e14072fSNikunj A Dadhania 
3382e14072fSNikunj A Dadhania     rtas_st(rets, 0, ret);
3392e14072fSNikunj A Dadhania }
3402e14072fSNikunj A Dadhania 
34128e02042SDavid Gibson static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
342094d2058SNathan Fontenot                                  uint32_t token, uint32_t nargs,
343094d2058SNathan Fontenot                                  target_ulong args, uint32_t nret,
344094d2058SNathan Fontenot                                  target_ulong rets)
345094d2058SNathan Fontenot {
346094d2058SNathan Fontenot     int32_t power_domain;
347094d2058SNathan Fontenot 
348094d2058SNathan Fontenot     if (nargs != 2 || nret != 2) {
349094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
350094d2058SNathan Fontenot         return;
351094d2058SNathan Fontenot     }
352094d2058SNathan Fontenot 
353094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
354094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
355094d2058SNathan Fontenot      */
356094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
357094d2058SNathan Fontenot     if (power_domain != -1) {
358094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
359094d2058SNathan Fontenot         return;
360094d2058SNathan Fontenot     }
361094d2058SNathan Fontenot 
362094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
363094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
364094d2058SNathan Fontenot }
365094d2058SNathan Fontenot 
36628e02042SDavid Gibson static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr,
367094d2058SNathan Fontenot                                   uint32_t token, uint32_t nargs,
368094d2058SNathan Fontenot                                   target_ulong args, uint32_t nret,
369094d2058SNathan Fontenot                                   target_ulong rets)
370094d2058SNathan Fontenot {
371094d2058SNathan Fontenot     int32_t power_domain;
372094d2058SNathan Fontenot 
373094d2058SNathan Fontenot     if (nargs != 1 || nret != 2) {
374094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
375094d2058SNathan Fontenot         return;
376094d2058SNathan Fontenot     }
377094d2058SNathan Fontenot 
378094d2058SNathan Fontenot     /* we currently only use a single, "live insert" powerdomain for
379094d2058SNathan Fontenot      * hotplugged/dlpar'd resources, so the power is always live/full (100)
380094d2058SNathan Fontenot      */
381094d2058SNathan Fontenot     power_domain = rtas_ld(args, 0);
382094d2058SNathan Fontenot     if (power_domain != -1) {
383094d2058SNathan Fontenot         rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
384094d2058SNathan Fontenot         return;
385094d2058SNathan Fontenot     }
386094d2058SNathan Fontenot 
387094d2058SNathan Fontenot     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
388094d2058SNathan Fontenot     rtas_st(rets, 1, 100);
389094d2058SNathan Fontenot }
390094d2058SNathan Fontenot 
3918c8639dfSMike Day static bool sensor_type_is_dr(uint32_t sensor_type)
3928c8639dfSMike Day {
3938c8639dfSMike Day     switch (sensor_type) {
3948c8639dfSMike Day     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
3958c8639dfSMike Day     case RTAS_SENSOR_TYPE_DR:
3968c8639dfSMike Day     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
3978c8639dfSMike Day         return true;
3988c8639dfSMike Day     }
3998c8639dfSMike Day 
4008c8639dfSMike Day     return false;
4018c8639dfSMike Day }
4028c8639dfSMike Day 
40328e02042SDavid Gibson static void rtas_set_indicator(PowerPCCPU *cpu, sPAPRMachineState *spapr,
4048c8639dfSMike Day                                uint32_t token, uint32_t nargs,
4058c8639dfSMike Day                                target_ulong args, uint32_t nret,
4068c8639dfSMike Day                                target_ulong rets)
4078c8639dfSMike Day {
4088c8639dfSMike Day     uint32_t sensor_type;
4098c8639dfSMike Day     uint32_t sensor_index;
4108c8639dfSMike Day     uint32_t sensor_state;
4110cb688d2SMichael Roth     uint32_t ret = RTAS_OUT_SUCCESS;
4128c8639dfSMike Day     sPAPRDRConnector *drc;
4138c8639dfSMike Day     sPAPRDRConnectorClass *drck;
4148c8639dfSMike Day 
4158c8639dfSMike Day     if (nargs != 3 || nret != 1) {
4160cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
4170cb688d2SMichael Roth         goto out;
4188c8639dfSMike Day     }
4198c8639dfSMike Day 
4208c8639dfSMike Day     sensor_type = rtas_ld(args, 0);
4218c8639dfSMike Day     sensor_index = rtas_ld(args, 1);
4228c8639dfSMike Day     sensor_state = rtas_ld(args, 2);
4238c8639dfSMike Day 
4248c8639dfSMike Day     if (!sensor_type_is_dr(sensor_type)) {
4258c8639dfSMike Day         goto out_unimplemented;
4268c8639dfSMike Day     }
4278c8639dfSMike Day 
4288c8639dfSMike Day     /* if this is a DR sensor we can assume sensor_index == drc_index */
4298c8639dfSMike Day     drc = spapr_dr_connector_by_index(sensor_index);
4308c8639dfSMike Day     if (!drc) {
431028ec3ceSLaurent Vivier         trace_spapr_rtas_set_indicator_invalid(sensor_index);
4320cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
4330cb688d2SMichael Roth         goto out;
4348c8639dfSMike Day     }
4358c8639dfSMike Day     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
4368c8639dfSMike Day 
4378c8639dfSMike Day     switch (sensor_type) {
4388c8639dfSMike Day     case RTAS_SENSOR_TYPE_ISOLATION_STATE:
43946503c2bSMichael Roth         /* if the guest is configuring a device attached to this
44046503c2bSMichael Roth          * DRC, we should reset the configuration state at this
44146503c2bSMichael Roth          * point since it may no longer be reliable (guest released
44246503c2bSMichael Roth          * device and needs to start over, or unplug occurred so
44346503c2bSMichael Roth          * the FDT is no longer valid)
44446503c2bSMichael Roth          */
44546503c2bSMichael Roth         if (sensor_state == SPAPR_DR_ISOLATION_STATE_ISOLATED) {
44646503c2bSMichael Roth             sPAPRConfigureConnectorState *ccs = spapr_ccs_find(spapr,
44746503c2bSMichael Roth                                                                sensor_index);
44846503c2bSMichael Roth             if (ccs) {
44946503c2bSMichael Roth                 spapr_ccs_remove(spapr, ccs);
45046503c2bSMichael Roth             }
45146503c2bSMichael Roth         }
4520cb688d2SMichael Roth         ret = drck->set_isolation_state(drc, sensor_state);
4538c8639dfSMike Day         break;
4548c8639dfSMike Day     case RTAS_SENSOR_TYPE_DR:
4550cb688d2SMichael Roth         ret = drck->set_indicator_state(drc, sensor_state);
4568c8639dfSMike Day         break;
4578c8639dfSMike Day     case RTAS_SENSOR_TYPE_ALLOCATION_STATE:
4580cb688d2SMichael Roth         ret = drck->set_allocation_state(drc, sensor_state);
4598c8639dfSMike Day         break;
4608c8639dfSMike Day     default:
4618c8639dfSMike Day         goto out_unimplemented;
4628c8639dfSMike Day     }
4638c8639dfSMike Day 
4640cb688d2SMichael Roth out:
4650cb688d2SMichael Roth     rtas_st(rets, 0, ret);
4668c8639dfSMike Day     return;
4678c8639dfSMike Day 
4688c8639dfSMike Day out_unimplemented:
4698c8639dfSMike Day     /* currently only DR-related sensors are implemented */
470028ec3ceSLaurent Vivier     trace_spapr_rtas_set_indicator_not_supported(sensor_index, sensor_type);
4718c8639dfSMike Day     rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED);
4728c8639dfSMike Day }
4738c8639dfSMike Day 
47428e02042SDavid Gibson static void rtas_get_sensor_state(PowerPCCPU *cpu, sPAPRMachineState *spapr,
475886445a6SMike Day                                   uint32_t token, uint32_t nargs,
476886445a6SMike Day                                   target_ulong args, uint32_t nret,
477886445a6SMike Day                                   target_ulong rets)
478886445a6SMike Day {
479886445a6SMike Day     uint32_t sensor_type;
480886445a6SMike Day     uint32_t sensor_index;
4810cb688d2SMichael Roth     uint32_t sensor_state = 0;
482886445a6SMike Day     sPAPRDRConnector *drc;
483886445a6SMike Day     sPAPRDRConnectorClass *drck;
4840cb688d2SMichael Roth     uint32_t ret = RTAS_OUT_SUCCESS;
485886445a6SMike Day 
486886445a6SMike Day     if (nargs != 2 || nret != 2) {
4870cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
4880cb688d2SMichael Roth         goto out;
489886445a6SMike Day     }
490886445a6SMike Day 
491886445a6SMike Day     sensor_type = rtas_ld(args, 0);
492886445a6SMike Day     sensor_index = rtas_ld(args, 1);
493886445a6SMike Day 
494886445a6SMike Day     if (sensor_type != RTAS_SENSOR_TYPE_ENTITY_SENSE) {
495886445a6SMike Day         /* currently only DR-related sensors are implemented */
496028ec3ceSLaurent Vivier         trace_spapr_rtas_get_sensor_state_not_supported(sensor_index,
497886445a6SMike Day                                                         sensor_type);
4980cb688d2SMichael Roth         ret = RTAS_OUT_NOT_SUPPORTED;
4990cb688d2SMichael Roth         goto out;
500886445a6SMike Day     }
501886445a6SMike Day 
502886445a6SMike Day     drc = spapr_dr_connector_by_index(sensor_index);
503886445a6SMike Day     if (!drc) {
504028ec3ceSLaurent Vivier         trace_spapr_rtas_get_sensor_state_invalid(sensor_index);
5050cb688d2SMichael Roth         ret = RTAS_OUT_PARAM_ERROR;
5060cb688d2SMichael Roth         goto out;
507886445a6SMike Day     }
508886445a6SMike Day     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
5090cb688d2SMichael Roth     ret = drck->entity_sense(drc, &sensor_state);
510886445a6SMike Day 
5110cb688d2SMichael Roth out:
5120cb688d2SMichael Roth     rtas_st(rets, 0, ret);
5130cb688d2SMichael Roth     rtas_st(rets, 1, sensor_state);
514886445a6SMike Day }
515886445a6SMike Day 
51646503c2bSMichael Roth /* configure-connector work area offsets, int32_t units for field
51746503c2bSMichael Roth  * indexes, bytes for field offset/len values.
51846503c2bSMichael Roth  *
51946503c2bSMichael Roth  * as documented by PAPR+ v2.7, 13.5.3.5
52046503c2bSMichael Roth  */
52146503c2bSMichael Roth #define CC_IDX_NODE_NAME_OFFSET 2
52246503c2bSMichael Roth #define CC_IDX_PROP_NAME_OFFSET 2
52346503c2bSMichael Roth #define CC_IDX_PROP_LEN 3
52446503c2bSMichael Roth #define CC_IDX_PROP_DATA_OFFSET 4
52546503c2bSMichael Roth #define CC_VAL_DATA_OFFSET ((CC_IDX_PROP_DATA_OFFSET + 1) * 4)
52646503c2bSMichael Roth #define CC_WA_LEN 4096
52746503c2bSMichael Roth 
528f201987bSDavid Gibson static void configure_connector_st(target_ulong addr, target_ulong offset,
529f201987bSDavid Gibson                                    const void *buf, size_t len)
530f201987bSDavid Gibson {
531f201987bSDavid Gibson     cpu_physical_memory_write(ppc64_phys_to_real(addr + offset),
532f201987bSDavid Gibson                               buf, MIN(len, CC_WA_LEN - offset));
533f201987bSDavid Gibson }
534f201987bSDavid Gibson 
53546503c2bSMichael Roth static void rtas_ibm_configure_connector(PowerPCCPU *cpu,
53628e02042SDavid Gibson                                          sPAPRMachineState *spapr,
53746503c2bSMichael Roth                                          uint32_t token, uint32_t nargs,
53846503c2bSMichael Roth                                          target_ulong args, uint32_t nret,
53946503c2bSMichael Roth                                          target_ulong rets)
54046503c2bSMichael Roth {
54146503c2bSMichael Roth     uint64_t wa_addr;
54246503c2bSMichael Roth     uint64_t wa_offset;
54346503c2bSMichael Roth     uint32_t drc_index;
54446503c2bSMichael Roth     sPAPRDRConnector *drc;
54546503c2bSMichael Roth     sPAPRDRConnectorClass *drck;
54646503c2bSMichael Roth     sPAPRConfigureConnectorState *ccs;
54746503c2bSMichael Roth     sPAPRDRCCResponse resp = SPAPR_DR_CC_RESPONSE_CONTINUE;
54846503c2bSMichael Roth     int rc;
54946503c2bSMichael Roth     const void *fdt;
55046503c2bSMichael Roth 
55146503c2bSMichael Roth     if (nargs != 2 || nret != 1) {
55246503c2bSMichael Roth         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
55346503c2bSMichael Roth         return;
55446503c2bSMichael Roth     }
55546503c2bSMichael Roth 
55646503c2bSMichael Roth     wa_addr = ((uint64_t)rtas_ld(args, 1) << 32) | rtas_ld(args, 0);
55746503c2bSMichael Roth 
55846503c2bSMichael Roth     drc_index = rtas_ld(wa_addr, 0);
55946503c2bSMichael Roth     drc = spapr_dr_connector_by_index(drc_index);
56046503c2bSMichael Roth     if (!drc) {
561028ec3ceSLaurent Vivier         trace_spapr_rtas_ibm_configure_connector_invalid(drc_index);
56246503c2bSMichael Roth         rc = RTAS_OUT_PARAM_ERROR;
56346503c2bSMichael Roth         goto out;
56446503c2bSMichael Roth     }
56546503c2bSMichael Roth 
56646503c2bSMichael Roth     drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);
56746503c2bSMichael Roth     fdt = drck->get_fdt(drc, NULL);
568e6fc9568SBharata B Rao     if (!fdt) {
569028ec3ceSLaurent Vivier         trace_spapr_rtas_ibm_configure_connector_missing_fdt(drc_index);
570e6fc9568SBharata B Rao         rc = SPAPR_DR_CC_RESPONSE_NOT_CONFIGURABLE;
571e6fc9568SBharata B Rao         goto out;
572e6fc9568SBharata B Rao     }
57346503c2bSMichael Roth 
57446503c2bSMichael Roth     ccs = spapr_ccs_find(spapr, drc_index);
57546503c2bSMichael Roth     if (!ccs) {
57646503c2bSMichael Roth         ccs = g_new0(sPAPRConfigureConnectorState, 1);
57746503c2bSMichael Roth         (void)drck->get_fdt(drc, &ccs->fdt_offset);
57846503c2bSMichael Roth         ccs->drc_index = drc_index;
57946503c2bSMichael Roth         spapr_ccs_add(spapr, ccs);
58046503c2bSMichael Roth     }
58146503c2bSMichael Roth 
58246503c2bSMichael Roth     do {
58346503c2bSMichael Roth         uint32_t tag;
58446503c2bSMichael Roth         const char *name;
58546503c2bSMichael Roth         const struct fdt_property *prop;
58646503c2bSMichael Roth         int fdt_offset_next, prop_len;
58746503c2bSMichael Roth 
58846503c2bSMichael Roth         tag = fdt_next_tag(fdt, ccs->fdt_offset, &fdt_offset_next);
58946503c2bSMichael Roth 
59046503c2bSMichael Roth         switch (tag) {
59146503c2bSMichael Roth         case FDT_BEGIN_NODE:
59246503c2bSMichael Roth             ccs->fdt_depth++;
59346503c2bSMichael Roth             name = fdt_get_name(fdt, ccs->fdt_offset, NULL);
59446503c2bSMichael Roth 
59546503c2bSMichael Roth             /* provide the name of the next OF node */
59646503c2bSMichael Roth             wa_offset = CC_VAL_DATA_OFFSET;
59746503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_NODE_NAME_OFFSET, wa_offset);
598f201987bSDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
59946503c2bSMichael Roth             resp = SPAPR_DR_CC_RESPONSE_NEXT_CHILD;
60046503c2bSMichael Roth             break;
60146503c2bSMichael Roth         case FDT_END_NODE:
60246503c2bSMichael Roth             ccs->fdt_depth--;
60346503c2bSMichael Roth             if (ccs->fdt_depth == 0) {
60446503c2bSMichael Roth                 /* done sending the device tree, don't need to track
60546503c2bSMichael Roth                  * the state anymore
60646503c2bSMichael Roth                  */
60746503c2bSMichael Roth                 drck->set_configured(drc);
60846503c2bSMichael Roth                 spapr_ccs_remove(spapr, ccs);
60946503c2bSMichael Roth                 ccs = NULL;
61046503c2bSMichael Roth                 resp = SPAPR_DR_CC_RESPONSE_SUCCESS;
61146503c2bSMichael Roth             } else {
61246503c2bSMichael Roth                 resp = SPAPR_DR_CC_RESPONSE_PREV_PARENT;
61346503c2bSMichael Roth             }
61446503c2bSMichael Roth             break;
61546503c2bSMichael Roth         case FDT_PROP:
61646503c2bSMichael Roth             prop = fdt_get_property_by_offset(fdt, ccs->fdt_offset,
61746503c2bSMichael Roth                                               &prop_len);
61846503c2bSMichael Roth             name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
61946503c2bSMichael Roth 
62046503c2bSMichael Roth             /* provide the name of the next OF property */
62146503c2bSMichael Roth             wa_offset = CC_VAL_DATA_OFFSET;
62246503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_PROP_NAME_OFFSET, wa_offset);
623f201987bSDavid Gibson             configure_connector_st(wa_addr, wa_offset, name, strlen(name) + 1);
62446503c2bSMichael Roth 
62546503c2bSMichael Roth             /* provide the length and value of the OF property. data gets
62646503c2bSMichael Roth              * placed immediately after NULL terminator of the OF property's
62746503c2bSMichael Roth              * name string
62846503c2bSMichael Roth              */
62946503c2bSMichael Roth             wa_offset += strlen(name) + 1,
63046503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_PROP_LEN, prop_len);
63146503c2bSMichael Roth             rtas_st(wa_addr, CC_IDX_PROP_DATA_OFFSET, wa_offset);
632f201987bSDavid Gibson             configure_connector_st(wa_addr, wa_offset, prop->data, prop_len);
63346503c2bSMichael Roth             resp = SPAPR_DR_CC_RESPONSE_NEXT_PROPERTY;
63446503c2bSMichael Roth             break;
63546503c2bSMichael Roth         case FDT_END:
63646503c2bSMichael Roth             resp = SPAPR_DR_CC_RESPONSE_ERROR;
63746503c2bSMichael Roth         default:
63846503c2bSMichael Roth             /* keep seeking for an actionable tag */
63946503c2bSMichael Roth             break;
64046503c2bSMichael Roth         }
64146503c2bSMichael Roth         if (ccs) {
64246503c2bSMichael Roth             ccs->fdt_offset = fdt_offset_next;
64346503c2bSMichael Roth         }
64446503c2bSMichael Roth     } while (resp == SPAPR_DR_CC_RESPONSE_CONTINUE);
64546503c2bSMichael Roth 
64646503c2bSMichael Roth     rc = resp;
64746503c2bSMichael Roth out:
64846503c2bSMichael Roth     rtas_st(rets, 0, rc);
64946503c2bSMichael Roth }
65046503c2bSMichael Roth 
65139ac8455SDavid Gibson static struct rtas_call {
65239ac8455SDavid Gibson     const char *name;
65339ac8455SDavid Gibson     spapr_rtas_fn fn;
6543a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
65539ac8455SDavid Gibson 
65628e02042SDavid Gibson target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr,
65739ac8455SDavid Gibson                              uint32_t token, uint32_t nargs, target_ulong args,
65839ac8455SDavid Gibson                              uint32_t nret, target_ulong rets)
65939ac8455SDavid Gibson {
6603a3b8502SAlexey Kardashevskiy     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
6613a3b8502SAlexey Kardashevskiy         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
66239ac8455SDavid Gibson 
66339ac8455SDavid Gibson         if (call->fn) {
664210b580bSAnthony Liguori             call->fn(cpu, spapr, token, nargs, args, nret, rets);
66539ac8455SDavid Gibson             return H_SUCCESS;
66639ac8455SDavid Gibson         }
66739ac8455SDavid Gibson     }
66839ac8455SDavid Gibson 
669821303f5SDavid Gibson     /* HACK: Some Linux early debug code uses RTAS display-character,
670821303f5SDavid Gibson      * but assumes the token value is 0xa (which it is on some real
671821303f5SDavid Gibson      * machines) without looking it up in the device tree.  This
672821303f5SDavid Gibson      * special case makes this work */
673821303f5SDavid Gibson     if (token == 0xa) {
674210b580bSAnthony Liguori         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
675821303f5SDavid Gibson         return H_SUCCESS;
676821303f5SDavid Gibson     }
677821303f5SDavid Gibson 
67839ac8455SDavid Gibson     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
679a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
68039ac8455SDavid Gibson     return H_PARAMETER;
68139ac8455SDavid Gibson }
68239ac8455SDavid Gibson 
683eeddd59fSLaurent Vivier uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args,
684eeddd59fSLaurent Vivier                          uint32_t nret, uint64_t rets)
685eeddd59fSLaurent Vivier {
686eeddd59fSLaurent Vivier     int token;
687eeddd59fSLaurent Vivier 
688eeddd59fSLaurent Vivier     for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) {
689eeddd59fSLaurent Vivier         if (strcmp(cmd, rtas_table[token].name) == 0) {
690eeddd59fSLaurent Vivier             sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
691eeddd59fSLaurent Vivier             PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
692eeddd59fSLaurent Vivier 
693eeddd59fSLaurent Vivier             rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE,
694eeddd59fSLaurent Vivier                                  nargs, args, nret, rets);
695eeddd59fSLaurent Vivier             return H_SUCCESS;
696eeddd59fSLaurent Vivier         }
697eeddd59fSLaurent Vivier     }
698eeddd59fSLaurent Vivier     return H_PARAMETER;
699eeddd59fSLaurent Vivier }
700eeddd59fSLaurent Vivier 
7013a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
70239ac8455SDavid Gibson {
703adf9ac50SDavid Gibson     assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX));
7043a3b8502SAlexey Kardashevskiy 
7053a3b8502SAlexey Kardashevskiy     token -= RTAS_TOKEN_BASE;
706adf9ac50SDavid Gibson 
707adf9ac50SDavid Gibson     assert(!rtas_table[token].name);
708c89d5299SDavid Gibson 
7093a3b8502SAlexey Kardashevskiy     rtas_table[token].name = name;
7103a3b8502SAlexey Kardashevskiy     rtas_table[token].fn = fn;
71139ac8455SDavid Gibson }
71239ac8455SDavid Gibson 
713a8170e5eSAvi Kivity int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
714a8170e5eSAvi Kivity                                  hwaddr rtas_size)
71539ac8455SDavid Gibson {
71639ac8455SDavid Gibson     int ret;
71739ac8455SDavid Gibson     int i;
718db4ef288SBharata B Rao     uint32_t lrdr_capacity[5];
719db4ef288SBharata B Rao     MachineState *machine = MACHINE(qdev_get_machine());
720a110655aSBharata B Rao     sPAPRMachineState *spapr = SPAPR_MACHINE(machine);
721a110655aSBharata B Rao     uint64_t max_hotplug_addr = spapr->hotplug_memory.base +
722a110655aSBharata B Rao                                 memory_region_size(&spapr->hotplug_memory.mr);
72339ac8455SDavid Gibson 
7243a3b8502SAlexey Kardashevskiy     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
72539ac8455SDavid Gibson         struct rtas_call *call = &rtas_table[i];
72639ac8455SDavid Gibson 
727d36b66f7SBen Herrenschmidt         if (!call->name) {
72839ac8455SDavid Gibson             continue;
72939ac8455SDavid Gibson         }
73039ac8455SDavid Gibson 
7315a4348d1SPeter Crosthwaite         ret = qemu_fdt_setprop_cell(fdt, "/rtas", call->name,
7323a3b8502SAlexey Kardashevskiy                                     i + RTAS_TOKEN_BASE);
73339ac8455SDavid Gibson         if (ret < 0) {
734ce9863b7SCédric Le Goater             error_report("Couldn't add rtas token for %s: %s",
73539ac8455SDavid Gibson                     call->name, fdt_strerror(ret));
73639ac8455SDavid Gibson             return ret;
73739ac8455SDavid Gibson         }
73839ac8455SDavid Gibson 
73939ac8455SDavid Gibson     }
740db4ef288SBharata B Rao 
741a110655aSBharata B Rao     lrdr_capacity[0] = cpu_to_be32(max_hotplug_addr >> 32);
742a110655aSBharata B Rao     lrdr_capacity[1] = cpu_to_be32(max_hotplug_addr & 0xffffffff);
743db4ef288SBharata B Rao     lrdr_capacity[2] = 0;
744db4ef288SBharata B Rao     lrdr_capacity[3] = cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE);
745db4ef288SBharata B Rao     lrdr_capacity[4] = cpu_to_be32(max_cpus/smp_threads);
746db4ef288SBharata B Rao     ret = qemu_fdt_setprop(fdt, "/rtas", "ibm,lrdr-capacity", lrdr_capacity,
747db4ef288SBharata B Rao                      sizeof(lrdr_capacity));
748db4ef288SBharata B Rao     if (ret < 0) {
749ce9863b7SCédric Le Goater         error_report("Couldn't add ibm,lrdr-capacity rtas property");
750db4ef288SBharata B Rao         return ret;
751db4ef288SBharata B Rao     }
752db4ef288SBharata B Rao 
75339ac8455SDavid Gibson     return 0;
75439ac8455SDavid Gibson }
755821303f5SDavid Gibson 
756*2cac78c1SDavid Gibson void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr)
757*2cac78c1SDavid Gibson {
758*2cac78c1SDavid Gibson     int rtas_node;
759*2cac78c1SDavid Gibson     int ret;
760*2cac78c1SDavid Gibson 
761*2cac78c1SDavid Gibson     /* Copy RTAS blob into guest RAM */
762*2cac78c1SDavid Gibson     cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size);
763*2cac78c1SDavid Gibson 
764*2cac78c1SDavid Gibson     ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size);
765*2cac78c1SDavid Gibson     if (ret < 0) {
766*2cac78c1SDavid Gibson         error_report("Couldn't add RTAS reserve entry: %s",
767*2cac78c1SDavid Gibson                      fdt_strerror(ret));
768*2cac78c1SDavid Gibson         exit(1);
769*2cac78c1SDavid Gibson     }
770*2cac78c1SDavid Gibson 
771*2cac78c1SDavid Gibson     /* Update the device tree with the blob's location */
772*2cac78c1SDavid Gibson     rtas_node = fdt_path_offset(fdt, "/rtas");
773*2cac78c1SDavid Gibson     assert(rtas_node >= 0);
774*2cac78c1SDavid Gibson 
775*2cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr);
776*2cac78c1SDavid Gibson     if (ret < 0) {
777*2cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-base property: %s",
778*2cac78c1SDavid Gibson                      fdt_strerror(ret));
779*2cac78c1SDavid Gibson         exit(1);
780*2cac78c1SDavid Gibson     }
781*2cac78c1SDavid Gibson 
782*2cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr);
783*2cac78c1SDavid Gibson     if (ret < 0) {
784*2cac78c1SDavid Gibson         error_report("Couldn't add linux,rtas-entry property: %s",
785*2cac78c1SDavid Gibson                      fdt_strerror(ret));
786*2cac78c1SDavid Gibson         exit(1);
787*2cac78c1SDavid Gibson     }
788*2cac78c1SDavid Gibson 
789*2cac78c1SDavid Gibson     ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size);
790*2cac78c1SDavid Gibson     if (ret < 0) {
791*2cac78c1SDavid Gibson         error_report("Couldn't add rtas-size property: %s",
792*2cac78c1SDavid Gibson                      fdt_strerror(ret));
793*2cac78c1SDavid Gibson         exit(1);
794*2cac78c1SDavid Gibson     }
795*2cac78c1SDavid Gibson }
796*2cac78c1SDavid Gibson 
79783f7d43aSAndreas Färber static void core_rtas_register_types(void)
798821303f5SDavid Gibson {
7993a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
8003a3b8502SAlexey Kardashevskiy                         rtas_display_character);
8013a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
8023a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
8033a3b8502SAlexey Kardashevskiy                         rtas_system_reboot);
8043a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
805a9f8ad8fSDavid Gibson                         rtas_query_cpu_stopped_state);
8063a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
8073a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
8083a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
8093a3b8502SAlexey Kardashevskiy                         "ibm,get-system-parameter",
8103ada6b11SAlexey Kardashevskiy                         rtas_ibm_get_system_parameter);
8113a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
8123a3b8502SAlexey Kardashevskiy                         "ibm,set-system-parameter",
8133ada6b11SAlexey Kardashevskiy                         rtas_ibm_set_system_parameter);
8142e14072fSNikunj A Dadhania     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
8152e14072fSNikunj A Dadhania                         rtas_ibm_os_term);
816094d2058SNathan Fontenot     spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level",
817094d2058SNathan Fontenot                         rtas_set_power_level);
818094d2058SNathan Fontenot     spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level",
819094d2058SNathan Fontenot                         rtas_get_power_level);
8208c8639dfSMike Day     spapr_rtas_register(RTAS_SET_INDICATOR, "set-indicator",
8218c8639dfSMike Day                         rtas_set_indicator);
822886445a6SMike Day     spapr_rtas_register(RTAS_GET_SENSOR_STATE, "get-sensor-state",
823886445a6SMike Day                         rtas_get_sensor_state);
82446503c2bSMichael Roth     spapr_rtas_register(RTAS_IBM_CONFIGURE_CONNECTOR, "ibm,configure-connector",
82546503c2bSMichael Roth                         rtas_ibm_configure_connector);
826821303f5SDavid Gibson }
82783f7d43aSAndreas Färber 
82883f7d43aSAndreas Färber type_init(core_rtas_register_types)
829