xref: /qemu/hw/ppc/spapr_rtas.c (revision 2e14072f9e859272c7b94b8e189bd30bb4954aa1)
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  */
2739ac8455SDavid Gibson #include "cpu.h"
289c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
29dccfcd0eSPaolo Bonzini #include "sysemu/char.h"
3039ac8455SDavid Gibson #include "hw/qdev.h"
319c17d615SPaolo Bonzini #include "sysemu/device_tree.h"
3239ac8455SDavid Gibson 
330d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
340d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
35e010ad8fSWenchao Xia #include "qapi-event.h"
3639ac8455SDavid Gibson 
3739ac8455SDavid Gibson #include <libfdt.h>
3839ac8455SDavid Gibson 
39210b580bSAnthony Liguori static void rtas_display_character(PowerPCCPU *cpu, sPAPREnvironment *spapr,
40821303f5SDavid Gibson                                    uint32_t token, uint32_t nargs,
41821303f5SDavid Gibson                                    target_ulong args,
42821303f5SDavid Gibson                                    uint32_t nret, target_ulong rets)
43821303f5SDavid Gibson {
44821303f5SDavid Gibson     uint8_t c = rtas_ld(args, 0);
455f2e2ba2SDavid Gibson     VIOsPAPRDevice *sdev = vty_lookup(spapr, 0);
46821303f5SDavid Gibson 
47821303f5SDavid Gibson     if (!sdev) {
48a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
49821303f5SDavid Gibson     } else {
50821303f5SDavid Gibson         vty_putchars(sdev, &c, sizeof(c));
51a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
52821303f5SDavid Gibson     }
53821303f5SDavid Gibson }
54821303f5SDavid Gibson 
55210b580bSAnthony Liguori static void rtas_get_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
56821303f5SDavid Gibson                                  uint32_t token, uint32_t nargs,
57821303f5SDavid Gibson                                  target_ulong args,
58821303f5SDavid Gibson                                  uint32_t nret, target_ulong rets)
59821303f5SDavid Gibson {
60821303f5SDavid Gibson     struct tm tm;
61821303f5SDavid Gibson 
62821303f5SDavid Gibson     if (nret != 8) {
63a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
64821303f5SDavid Gibson         return;
65821303f5SDavid Gibson     }
66821303f5SDavid Gibson 
67ac26f8c3SBreno Leitao     qemu_get_timedate(&tm, spapr->rtc_offset);
68821303f5SDavid Gibson 
69a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
70821303f5SDavid Gibson     rtas_st(rets, 1, tm.tm_year + 1900);
71821303f5SDavid Gibson     rtas_st(rets, 2, tm.tm_mon + 1);
72821303f5SDavid Gibson     rtas_st(rets, 3, tm.tm_mday);
73821303f5SDavid Gibson     rtas_st(rets, 4, tm.tm_hour);
74821303f5SDavid Gibson     rtas_st(rets, 5, tm.tm_min);
75821303f5SDavid Gibson     rtas_st(rets, 6, tm.tm_sec);
76821303f5SDavid Gibson     rtas_st(rets, 7, 0); /* we don't do nanoseconds */
77821303f5SDavid Gibson }
78821303f5SDavid Gibson 
79210b580bSAnthony Liguori static void rtas_set_time_of_day(PowerPCCPU *cpu, sPAPREnvironment *spapr,
80ac26f8c3SBreno Leitao                                  uint32_t token, uint32_t nargs,
81ac26f8c3SBreno Leitao                                  target_ulong args,
82ac26f8c3SBreno Leitao                                  uint32_t nret, target_ulong rets)
83ac26f8c3SBreno Leitao {
84ac26f8c3SBreno Leitao     struct tm tm;
85ac26f8c3SBreno Leitao 
86ac26f8c3SBreno Leitao     tm.tm_year = rtas_ld(args, 0) - 1900;
87ac26f8c3SBreno Leitao     tm.tm_mon = rtas_ld(args, 1) - 1;
88ac26f8c3SBreno Leitao     tm.tm_mday = rtas_ld(args, 2);
89ac26f8c3SBreno Leitao     tm.tm_hour = rtas_ld(args, 3);
90ac26f8c3SBreno Leitao     tm.tm_min = rtas_ld(args, 4);
91ac26f8c3SBreno Leitao     tm.tm_sec = rtas_ld(args, 5);
92ac26f8c3SBreno Leitao 
93ac26f8c3SBreno Leitao     /* Just generate a monitor event for the change */
94e010ad8fSWenchao Xia     qapi_event_send_rtc_change(qemu_timedate_diff(&tm), &error_abort);
95ac26f8c3SBreno Leitao     spapr->rtc_offset = qemu_timedate_diff(&tm);
96ac26f8c3SBreno Leitao 
97a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
98ac26f8c3SBreno Leitao }
99ac26f8c3SBreno Leitao 
100210b580bSAnthony Liguori static void rtas_power_off(PowerPCCPU *cpu, sPAPREnvironment *spapr,
101821303f5SDavid Gibson                            uint32_t token, uint32_t nargs, target_ulong args,
102821303f5SDavid Gibson                            uint32_t nret, target_ulong rets)
103821303f5SDavid Gibson {
104821303f5SDavid Gibson     if (nargs != 2 || nret != 1) {
105a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
106821303f5SDavid Gibson         return;
107821303f5SDavid Gibson     }
108821303f5SDavid Gibson     qemu_system_shutdown_request();
109a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
110821303f5SDavid Gibson }
111821303f5SDavid Gibson 
112210b580bSAnthony Liguori static void rtas_system_reboot(PowerPCCPU *cpu, sPAPREnvironment *spapr,
113c821a43cSDavid Gibson                                uint32_t token, uint32_t nargs,
114c821a43cSDavid Gibson                                target_ulong args,
115c821a43cSDavid Gibson                                uint32_t nret, target_ulong rets)
116c821a43cSDavid Gibson {
117c821a43cSDavid Gibson     if (nargs != 0 || nret != 1) {
118a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
119c821a43cSDavid Gibson         return;
120c821a43cSDavid Gibson     }
121c821a43cSDavid Gibson     qemu_system_reset_request();
122a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
123c821a43cSDavid Gibson }
124c821a43cSDavid Gibson 
125210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_,
126210b580bSAnthony Liguori                                          sPAPREnvironment *spapr,
127a9f8ad8fSDavid Gibson                                          uint32_t token, uint32_t nargs,
128a9f8ad8fSDavid Gibson                                          target_ulong args,
129a9f8ad8fSDavid Gibson                                          uint32_t nret, target_ulong rets)
130a9f8ad8fSDavid Gibson {
131a9f8ad8fSDavid Gibson     target_ulong id;
1320f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
133a9f8ad8fSDavid Gibson 
134a9f8ad8fSDavid Gibson     if (nargs != 1 || nret != 2) {
135a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
136a9f8ad8fSDavid Gibson         return;
137a9f8ad8fSDavid Gibson     }
138a9f8ad8fSDavid Gibson 
139a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
1400f20ba62SAlexey Kardashevskiy     cpu = ppc_get_vcpu_by_dt_id(id);
14105318a85SAndreas Färber     if (cpu != NULL) {
1420f20ba62SAlexey Kardashevskiy         if (CPU(cpu)->halted) {
143a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 0);
144a9f8ad8fSDavid Gibson         } else {
145a9f8ad8fSDavid Gibson             rtas_st(rets, 1, 2);
146a9f8ad8fSDavid Gibson         }
147a9f8ad8fSDavid Gibson 
148a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
149a9f8ad8fSDavid Gibson         return;
150a9f8ad8fSDavid Gibson     }
151a9f8ad8fSDavid Gibson 
152a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
153a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
154a9f8ad8fSDavid Gibson }
155a9f8ad8fSDavid Gibson 
156210b580bSAnthony Liguori static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPREnvironment *spapr,
157a9f8ad8fSDavid Gibson                            uint32_t token, uint32_t nargs,
158a9f8ad8fSDavid Gibson                            target_ulong args,
159a9f8ad8fSDavid Gibson                            uint32_t nret, target_ulong rets)
160a9f8ad8fSDavid Gibson {
161a9f8ad8fSDavid Gibson     target_ulong id, start, r3;
1620f20ba62SAlexey Kardashevskiy     PowerPCCPU *cpu;
163a9f8ad8fSDavid Gibson 
164a9f8ad8fSDavid Gibson     if (nargs != 3 || nret != 1) {
165a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
166a9f8ad8fSDavid Gibson         return;
167a9f8ad8fSDavid Gibson     }
168a9f8ad8fSDavid Gibson 
169a9f8ad8fSDavid Gibson     id = rtas_ld(args, 0);
170a9f8ad8fSDavid Gibson     start = rtas_ld(args, 1);
171a9f8ad8fSDavid Gibson     r3 = rtas_ld(args, 2);
172a9f8ad8fSDavid Gibson 
1730f20ba62SAlexey Kardashevskiy     cpu = ppc_get_vcpu_by_dt_id(id);
1740f20ba62SAlexey Kardashevskiy     if (cpu != NULL) {
1750f20ba62SAlexey Kardashevskiy         CPUState *cs = CPU(cpu);
176c67e216bSAndreas Färber         CPUPPCState *env = &cpu->env;
177c08d7424SAndreas Färber 
178c67e216bSAndreas Färber         if (!cs->halted) {
179a64d325dSAlexey Kardashevskiy             rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
180a9f8ad8fSDavid Gibson             return;
181a9f8ad8fSDavid Gibson         }
182a9f8ad8fSDavid Gibson 
183048706d9SDavid Gibson         /* This will make sure qemu state is up to date with kvm, and
184048706d9SDavid Gibson          * mark it dirty so our changes get flushed back before the
185048706d9SDavid Gibson          * new cpu enters */
186dd1750d7SAndreas Färber         kvm_cpu_synchronize_state(cs);
187048706d9SDavid Gibson 
188a9f8ad8fSDavid Gibson         env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME);
189a9f8ad8fSDavid Gibson         env->nip = start;
190a9f8ad8fSDavid Gibson         env->gpr[3] = r3;
191c67e216bSAndreas Färber         cs->halted = 0;
192a9f8ad8fSDavid Gibson 
193c67e216bSAndreas Färber         qemu_cpu_kick(cs);
194a9f8ad8fSDavid Gibson 
195a64d325dSAlexey Kardashevskiy         rtas_st(rets, 0, RTAS_OUT_SUCCESS);
196a9f8ad8fSDavid Gibson         return;
197a9f8ad8fSDavid Gibson     }
198a9f8ad8fSDavid Gibson 
199a9f8ad8fSDavid Gibson     /* Didn't find a matching cpu */
200a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
201a9f8ad8fSDavid Gibson }
202a9f8ad8fSDavid Gibson 
20359760f2dSAlexey Kardashevskiy static void rtas_stop_self(PowerPCCPU *cpu, sPAPREnvironment *spapr,
20459760f2dSAlexey Kardashevskiy                            uint32_t token, uint32_t nargs,
20559760f2dSAlexey Kardashevskiy                            target_ulong args,
20659760f2dSAlexey Kardashevskiy                            uint32_t nret, target_ulong rets)
20759760f2dSAlexey Kardashevskiy {
20859760f2dSAlexey Kardashevskiy     CPUState *cs = CPU(cpu);
20959760f2dSAlexey Kardashevskiy     CPUPPCState *env = &cpu->env;
21059760f2dSAlexey Kardashevskiy 
21159760f2dSAlexey Kardashevskiy     cs->halted = 1;
21259760f2dSAlexey Kardashevskiy     cpu_exit(cs);
21359760f2dSAlexey Kardashevskiy     /*
21459760f2dSAlexey Kardashevskiy      * While stopping a CPU, the guest calls H_CPPR which
21559760f2dSAlexey Kardashevskiy      * effectively disables interrupts on XICS level.
21659760f2dSAlexey Kardashevskiy      * However decrementer interrupts in TCG can still
21759760f2dSAlexey Kardashevskiy      * wake the CPU up so here we disable interrupts in MSR
21859760f2dSAlexey Kardashevskiy      * as well.
21959760f2dSAlexey Kardashevskiy      * As rtas_start_cpu() resets the whole MSR anyway, there is
22059760f2dSAlexey Kardashevskiy      * no need to bother with specific bits, we just clear it.
22159760f2dSAlexey Kardashevskiy      */
22259760f2dSAlexey Kardashevskiy     env->msr = 0;
22359760f2dSAlexey Kardashevskiy }
22459760f2dSAlexey Kardashevskiy 
2253ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu,
2263ada6b11SAlexey Kardashevskiy                                           sPAPREnvironment *spapr,
2273ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2283ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2293ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2303ada6b11SAlexey Kardashevskiy {
2313ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2323ada6b11SAlexey Kardashevskiy     target_ulong buffer = rtas_ld(args, 1);
2333ada6b11SAlexey Kardashevskiy     target_ulong length = rtas_ld(args, 2);
2343052d951SSam bobroff     target_ulong ret = RTAS_OUT_SUCCESS;
2353ada6b11SAlexey Kardashevskiy 
2363ada6b11SAlexey Kardashevskiy     switch (parameter) {
2373b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: {
2383b50d897SSam bobroff         char *param_val = g_strdup_printf("MaxEntCap=%d,MaxPlatProcs=%d",
2393b50d897SSam bobroff                                           max_cpus, smp_cpus);
2403b50d897SSam bobroff         rtas_st_buffer(buffer, length, (uint8_t *)param_val, strlen(param_val));
2413b50d897SSam bobroff         g_free(param_val);
2423b50d897SSam bobroff         break;
2433b50d897SSam bobroff     }
2443052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: {
2453052d951SSam bobroff         uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED;
2463052d951SSam bobroff 
2473052d951SSam bobroff         rtas_st_buffer(buffer, length, &param_val, sizeof(param_val));
2483ada6b11SAlexey Kardashevskiy         break;
2493ada6b11SAlexey Kardashevskiy     }
250b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
251b907d7b0SSam bobroff         rtas_st_buffer(buffer, length, qemu_uuid, (qemu_uuid_set ? 16 : 0));
252b907d7b0SSam bobroff         break;
2533052d951SSam bobroff     default:
2543052d951SSam bobroff         ret = RTAS_OUT_NOT_SUPPORTED;
2553052d951SSam bobroff     }
2563ada6b11SAlexey Kardashevskiy 
2573ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
2583ada6b11SAlexey Kardashevskiy }
2593ada6b11SAlexey Kardashevskiy 
2603ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu,
2613ada6b11SAlexey Kardashevskiy                                           sPAPREnvironment *spapr,
2623ada6b11SAlexey Kardashevskiy                                           uint32_t token, uint32_t nargs,
2633ada6b11SAlexey Kardashevskiy                                           target_ulong args,
2643ada6b11SAlexey Kardashevskiy                                           uint32_t nret, target_ulong rets)
2653ada6b11SAlexey Kardashevskiy {
2663ada6b11SAlexey Kardashevskiy     target_ulong parameter = rtas_ld(args, 0);
2673ada6b11SAlexey Kardashevskiy     target_ulong ret = RTAS_OUT_NOT_SUPPORTED;
2683ada6b11SAlexey Kardashevskiy 
2693ada6b11SAlexey Kardashevskiy     switch (parameter) {
2703b50d897SSam bobroff     case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS:
2713052d951SSam bobroff     case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE:
272b907d7b0SSam bobroff     case RTAS_SYSPARM_UUID:
2733ada6b11SAlexey Kardashevskiy         ret = RTAS_OUT_NOT_AUTHORIZED;
2743ada6b11SAlexey Kardashevskiy         break;
2753ada6b11SAlexey Kardashevskiy     }
2763ada6b11SAlexey Kardashevskiy 
2773ada6b11SAlexey Kardashevskiy     rtas_st(rets, 0, ret);
2783ada6b11SAlexey Kardashevskiy }
2793ada6b11SAlexey Kardashevskiy 
280*2e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu,
281*2e14072fSNikunj A Dadhania                             sPAPREnvironment *spapr,
282*2e14072fSNikunj A Dadhania                             uint32_t token, uint32_t nargs,
283*2e14072fSNikunj A Dadhania                             target_ulong args,
284*2e14072fSNikunj A Dadhania                             uint32_t nret, target_ulong rets)
285*2e14072fSNikunj A Dadhania {
286*2e14072fSNikunj A Dadhania     target_ulong ret = 0;
287*2e14072fSNikunj A Dadhania 
288*2e14072fSNikunj A Dadhania     qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
289*2e14072fSNikunj A Dadhania 
290*2e14072fSNikunj A Dadhania     rtas_st(rets, 0, ret);
291*2e14072fSNikunj A Dadhania }
292*2e14072fSNikunj A Dadhania 
29339ac8455SDavid Gibson static struct rtas_call {
29439ac8455SDavid Gibson     const char *name;
29539ac8455SDavid Gibson     spapr_rtas_fn fn;
2963a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE];
29739ac8455SDavid Gibson 
298210b580bSAnthony Liguori target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPREnvironment *spapr,
29939ac8455SDavid Gibson                              uint32_t token, uint32_t nargs, target_ulong args,
30039ac8455SDavid Gibson                              uint32_t nret, target_ulong rets)
30139ac8455SDavid Gibson {
3023a3b8502SAlexey Kardashevskiy     if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) {
3033a3b8502SAlexey Kardashevskiy         struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE);
30439ac8455SDavid Gibson 
30539ac8455SDavid Gibson         if (call->fn) {
306210b580bSAnthony Liguori             call->fn(cpu, spapr, token, nargs, args, nret, rets);
30739ac8455SDavid Gibson             return H_SUCCESS;
30839ac8455SDavid Gibson         }
30939ac8455SDavid Gibson     }
31039ac8455SDavid Gibson 
311821303f5SDavid Gibson     /* HACK: Some Linux early debug code uses RTAS display-character,
312821303f5SDavid Gibson      * but assumes the token value is 0xa (which it is on some real
313821303f5SDavid Gibson      * machines) without looking it up in the device tree.  This
314821303f5SDavid Gibson      * special case makes this work */
315821303f5SDavid Gibson     if (token == 0xa) {
316210b580bSAnthony Liguori         rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets);
317821303f5SDavid Gibson         return H_SUCCESS;
318821303f5SDavid Gibson     }
319821303f5SDavid Gibson 
32039ac8455SDavid Gibson     hcall_dprintf("Unknown RTAS token 0x%x\n", token);
321a64d325dSAlexey Kardashevskiy     rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
32239ac8455SDavid Gibson     return H_PARAMETER;
32339ac8455SDavid Gibson }
32439ac8455SDavid Gibson 
3253a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn)
32639ac8455SDavid Gibson {
3273a3b8502SAlexey Kardashevskiy     if (!((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX))) {
3283a3b8502SAlexey Kardashevskiy         fprintf(stderr, "RTAS invalid token 0x%x\n", token);
329c89d5299SDavid Gibson         exit(1);
330c89d5299SDavid Gibson     }
3313a3b8502SAlexey Kardashevskiy 
3323a3b8502SAlexey Kardashevskiy     token -= RTAS_TOKEN_BASE;
3333a3b8502SAlexey Kardashevskiy     if (rtas_table[token].name) {
3343a3b8502SAlexey Kardashevskiy         fprintf(stderr, "RTAS call \"%s\" is registered already as 0x%x\n",
3353a3b8502SAlexey Kardashevskiy                 rtas_table[token].name, token);
3363a3b8502SAlexey Kardashevskiy         exit(1);
337c89d5299SDavid Gibson     }
338c89d5299SDavid Gibson 
3393a3b8502SAlexey Kardashevskiy     rtas_table[token].name = name;
3403a3b8502SAlexey Kardashevskiy     rtas_table[token].fn = fn;
34139ac8455SDavid Gibson }
34239ac8455SDavid Gibson 
343a8170e5eSAvi Kivity int spapr_rtas_device_tree_setup(void *fdt, hwaddr rtas_addr,
344a8170e5eSAvi Kivity                                  hwaddr rtas_size)
34539ac8455SDavid Gibson {
34639ac8455SDavid Gibson     int ret;
34739ac8455SDavid Gibson     int i;
34839ac8455SDavid Gibson 
34939ac8455SDavid Gibson     ret = fdt_add_mem_rsv(fdt, rtas_addr, rtas_size);
35039ac8455SDavid Gibson     if (ret < 0) {
35139ac8455SDavid Gibson         fprintf(stderr, "Couldn't add RTAS reserve entry: %s\n",
35239ac8455SDavid Gibson                 fdt_strerror(ret));
35339ac8455SDavid Gibson         return ret;
35439ac8455SDavid Gibson     }
35539ac8455SDavid Gibson 
3565a4348d1SPeter Crosthwaite     ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-base",
35739ac8455SDavid Gibson                                 rtas_addr);
35839ac8455SDavid Gibson     if (ret < 0) {
35939ac8455SDavid Gibson         fprintf(stderr, "Couldn't add linux,rtas-base property: %s\n",
36039ac8455SDavid Gibson                 fdt_strerror(ret));
36139ac8455SDavid Gibson         return ret;
36239ac8455SDavid Gibson     }
36339ac8455SDavid Gibson 
3645a4348d1SPeter Crosthwaite     ret = qemu_fdt_setprop_cell(fdt, "/rtas", "linux,rtas-entry",
36539ac8455SDavid Gibson                                 rtas_addr);
36639ac8455SDavid Gibson     if (ret < 0) {
36739ac8455SDavid Gibson         fprintf(stderr, "Couldn't add linux,rtas-entry property: %s\n",
36839ac8455SDavid Gibson                 fdt_strerror(ret));
36939ac8455SDavid Gibson         return ret;
37039ac8455SDavid Gibson     }
37139ac8455SDavid Gibson 
3725a4348d1SPeter Crosthwaite     ret = qemu_fdt_setprop_cell(fdt, "/rtas", "rtas-size",
37339ac8455SDavid Gibson                                 rtas_size);
37439ac8455SDavid Gibson     if (ret < 0) {
37539ac8455SDavid Gibson         fprintf(stderr, "Couldn't add rtas-size property: %s\n",
37639ac8455SDavid Gibson                 fdt_strerror(ret));
37739ac8455SDavid Gibson         return ret;
37839ac8455SDavid Gibson     }
37939ac8455SDavid Gibson 
3803a3b8502SAlexey Kardashevskiy     for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) {
38139ac8455SDavid Gibson         struct rtas_call *call = &rtas_table[i];
38239ac8455SDavid Gibson 
383d36b66f7SBen Herrenschmidt         if (!call->name) {
38439ac8455SDavid Gibson             continue;
38539ac8455SDavid Gibson         }
38639ac8455SDavid Gibson 
3875a4348d1SPeter Crosthwaite         ret = qemu_fdt_setprop_cell(fdt, "/rtas", call->name,
3883a3b8502SAlexey Kardashevskiy                                     i + RTAS_TOKEN_BASE);
38939ac8455SDavid Gibson         if (ret < 0) {
39039ac8455SDavid Gibson             fprintf(stderr, "Couldn't add rtas token for %s: %s\n",
39139ac8455SDavid Gibson                     call->name, fdt_strerror(ret));
39239ac8455SDavid Gibson             return ret;
39339ac8455SDavid Gibson         }
39439ac8455SDavid Gibson 
39539ac8455SDavid Gibson     }
39639ac8455SDavid Gibson     return 0;
39739ac8455SDavid Gibson }
398821303f5SDavid Gibson 
39983f7d43aSAndreas Färber static void core_rtas_register_types(void)
400821303f5SDavid Gibson {
4013a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character",
4023a3b8502SAlexey Kardashevskiy                         rtas_display_character);
4033a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_GET_TIME_OF_DAY, "get-time-of-day",
4043a3b8502SAlexey Kardashevskiy                         rtas_get_time_of_day);
4053a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SET_TIME_OF_DAY, "set-time-of-day",
4063a3b8502SAlexey Kardashevskiy                         rtas_set_time_of_day);
4073a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off);
4083a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot",
4093a3b8502SAlexey Kardashevskiy                         rtas_system_reboot);
4103a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state",
411a9f8ad8fSDavid Gibson                         rtas_query_cpu_stopped_state);
4123a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu);
4133a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self);
4143a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER,
4153a3b8502SAlexey Kardashevskiy                         "ibm,get-system-parameter",
4163ada6b11SAlexey Kardashevskiy                         rtas_ibm_get_system_parameter);
4173a3b8502SAlexey Kardashevskiy     spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER,
4183a3b8502SAlexey Kardashevskiy                         "ibm,set-system-parameter",
4193ada6b11SAlexey Kardashevskiy                         rtas_ibm_set_system_parameter);
420*2e14072fSNikunj A Dadhania     spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term",
421*2e14072fSNikunj A Dadhania                         rtas_ibm_os_term);
422821303f5SDavid Gibson }
42383f7d43aSAndreas Färber 
42483f7d43aSAndreas Färber type_init(core_rtas_register_types)
425