139ac8455SDavid Gibson /* 239ac8455SDavid Gibson * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 339ac8455SDavid Gibson * 439ac8455SDavid Gibson * Hypercall based emulated RTAS 539ac8455SDavid Gibson * 639ac8455SDavid Gibson * Copyright (c) 2010-2011 David Gibson, IBM Corporation. 739ac8455SDavid Gibson * 839ac8455SDavid Gibson * Permission is hereby granted, free of charge, to any person obtaining a copy 939ac8455SDavid Gibson * of this software and associated documentation files (the "Software"), to deal 1039ac8455SDavid Gibson * in the Software without restriction, including without limitation the rights 1139ac8455SDavid Gibson * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1239ac8455SDavid Gibson * copies of the Software, and to permit persons to whom the Software is 1339ac8455SDavid Gibson * furnished to do so, subject to the following conditions: 1439ac8455SDavid Gibson * 1539ac8455SDavid Gibson * The above copyright notice and this permission notice shall be included in 1639ac8455SDavid Gibson * all copies or substantial portions of the Software. 1739ac8455SDavid Gibson * 1839ac8455SDavid Gibson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1939ac8455SDavid Gibson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2039ac8455SDavid Gibson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2139ac8455SDavid Gibson * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2239ac8455SDavid Gibson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2339ac8455SDavid Gibson * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2439ac8455SDavid Gibson * THE SOFTWARE. 2539ac8455SDavid Gibson * 2639ac8455SDavid Gibson */ 270d75590dSPeter Maydell #include "qemu/osdep.h" 2839ac8455SDavid Gibson #include "cpu.h" 2903dd024fSPaolo Bonzini #include "qemu/log.h" 30ce9863b7SCédric Le Goater #include "qemu/error-report.h" 319c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 3239ac8455SDavid Gibson #include "hw/qdev.h" 339c17d615SPaolo Bonzini #include "sysemu/device_tree.h" 34db4ef288SBharata B Rao #include "sysemu/cpus.h" 35cf116ad4SDavid Gibson #include "sysemu/hw_accel.h" 36*a84f7179SNikunj A Dadhania #include "kvm_ppc.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" 4184369f63SDavid Gibson #include "hw/ppc/spapr_cpu_core.h" 42af81cf32SBharata B Rao #include "hw/ppc/ppc.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" 50cf116ad4SDavid Gibson #include "target/ppc/mmu-hash64.h" 51f00bed95SDavid Gibson #include "target/ppc/mmu-book3s-v3.h" 528c8639dfSMike Day 5328e02042SDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr, 54821303f5SDavid Gibson uint32_t token, uint32_t nargs, 55821303f5SDavid Gibson target_ulong args, 56821303f5SDavid Gibson uint32_t nret, target_ulong rets) 57821303f5SDavid Gibson { 58821303f5SDavid Gibson uint8_t c = rtas_ld(args, 0); 595f2e2ba2SDavid Gibson VIOsPAPRDevice *sdev = vty_lookup(spapr, 0); 60821303f5SDavid Gibson 61821303f5SDavid Gibson if (!sdev) { 62a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 63821303f5SDavid Gibson } else { 64821303f5SDavid Gibson vty_putchars(sdev, &c, sizeof(c)); 65a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 66821303f5SDavid Gibson } 67821303f5SDavid Gibson } 68821303f5SDavid Gibson 6928e02042SDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr, 70821303f5SDavid Gibson uint32_t token, uint32_t nargs, target_ulong args, 71821303f5SDavid Gibson uint32_t nret, target_ulong rets) 72821303f5SDavid Gibson { 73821303f5SDavid Gibson if (nargs != 2 || nret != 1) { 74a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 75821303f5SDavid Gibson return; 76821303f5SDavid Gibson } 77cf83f140SEric Blake qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 788a9c1b77SThomas Huth cpu_stop_current(); 79a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 80821303f5SDavid Gibson } 81821303f5SDavid Gibson 8228e02042SDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr, 83c821a43cSDavid Gibson uint32_t token, uint32_t nargs, 84c821a43cSDavid Gibson target_ulong args, 85c821a43cSDavid Gibson uint32_t nret, target_ulong rets) 86c821a43cSDavid Gibson { 87c821a43cSDavid Gibson if (nargs != 0 || nret != 1) { 88a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 89c821a43cSDavid Gibson return; 90c821a43cSDavid Gibson } 91cf83f140SEric Blake qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 92a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 93c821a43cSDavid Gibson } 94c821a43cSDavid Gibson 95210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_, 9628e02042SDavid Gibson sPAPRMachineState *spapr, 97a9f8ad8fSDavid Gibson uint32_t token, uint32_t nargs, 98a9f8ad8fSDavid Gibson target_ulong args, 99a9f8ad8fSDavid Gibson uint32_t nret, target_ulong rets) 100a9f8ad8fSDavid Gibson { 101a9f8ad8fSDavid Gibson target_ulong id; 1020f20ba62SAlexey Kardashevskiy PowerPCCPU *cpu; 103a9f8ad8fSDavid Gibson 104a9f8ad8fSDavid Gibson if (nargs != 1 || nret != 2) { 105a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 106a9f8ad8fSDavid Gibson return; 107a9f8ad8fSDavid Gibson } 108a9f8ad8fSDavid Gibson 109a9f8ad8fSDavid Gibson id = rtas_ld(args, 0); 1102e886fb3SSam Bobroff cpu = spapr_find_cpu(id); 11105318a85SAndreas Färber if (cpu != NULL) { 1120f20ba62SAlexey Kardashevskiy if (CPU(cpu)->halted) { 113a9f8ad8fSDavid Gibson rtas_st(rets, 1, 0); 114a9f8ad8fSDavid Gibson } else { 115a9f8ad8fSDavid Gibson rtas_st(rets, 1, 2); 116a9f8ad8fSDavid Gibson } 117a9f8ad8fSDavid Gibson 118a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 119a9f8ad8fSDavid Gibson return; 120a9f8ad8fSDavid Gibson } 121a9f8ad8fSDavid Gibson 122a9f8ad8fSDavid Gibson /* Didn't find a matching cpu */ 123a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 124a9f8ad8fSDavid Gibson } 125a9f8ad8fSDavid Gibson 126cf116ad4SDavid Gibson static void rtas_start_cpu(PowerPCCPU *callcpu, sPAPRMachineState *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, start, r3; 132cf116ad4SDavid Gibson PowerPCCPU *newcpu; 133cf116ad4SDavid Gibson CPUPPCState *env; 134cf116ad4SDavid Gibson PowerPCCPUClass *pcc; 13598248918SDavid Gibson target_ulong lpcr; 136a9f8ad8fSDavid Gibson 137a9f8ad8fSDavid Gibson if (nargs != 3 || nret != 1) { 138a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 139a9f8ad8fSDavid Gibson return; 140a9f8ad8fSDavid Gibson } 141a9f8ad8fSDavid Gibson 142a9f8ad8fSDavid Gibson id = rtas_ld(args, 0); 143a9f8ad8fSDavid Gibson start = rtas_ld(args, 1); 144a9f8ad8fSDavid Gibson r3 = rtas_ld(args, 2); 145a9f8ad8fSDavid Gibson 146cf116ad4SDavid Gibson newcpu = spapr_find_cpu(id); 147cf116ad4SDavid Gibson if (!newcpu) { 148cf116ad4SDavid Gibson /* Didn't find a matching cpu */ 149cf116ad4SDavid Gibson rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 150cf116ad4SDavid Gibson return; 151cf116ad4SDavid Gibson } 152c08d7424SAndreas Färber 153cf116ad4SDavid Gibson env = &newcpu->env; 154cf116ad4SDavid Gibson pcc = POWERPC_CPU_GET_CLASS(newcpu); 155cf116ad4SDavid Gibson 156cf116ad4SDavid Gibson if (!CPU(newcpu)->halted) { 157a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 158a9f8ad8fSDavid Gibson return; 159a9f8ad8fSDavid Gibson } 160a9f8ad8fSDavid Gibson 161cf116ad4SDavid Gibson cpu_synchronize_state(CPU(newcpu)); 162048706d9SDavid Gibson 163a9f8ad8fSDavid Gibson env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME); 16498248918SDavid Gibson 1659a94ee5bSCédric Le Goater /* Enable Power-saving mode Exit Cause exceptions for the new CPU */ 16647a9b551SDavid Gibson lpcr = env->spr[SPR_LPCR]; 16798248918SDavid Gibson if (!pcc->interrupts_big_endian(callcpu)) { 16898248918SDavid Gibson lpcr |= LPCR_ILE; 16998248918SDavid Gibson } 170f00bed95SDavid Gibson if (env->mmu_model == POWERPC_MMU_3_00) { 171f00bed95SDavid Gibson /* 172f00bed95SDavid Gibson * New cpus are expected to start in the same radix/hash mode 173f00bed95SDavid Gibson * as the existing CPUs 174f00bed95SDavid Gibson */ 175f00bed95SDavid Gibson if (ppc64_radix_guest(callcpu)) { 176f00bed95SDavid Gibson lpcr |= LPCR_UPRT | LPCR_GTSE; 177f00bed95SDavid Gibson } else { 178f00bed95SDavid Gibson lpcr &= ~(LPCR_UPRT | LPCR_GTSE); 179f00bed95SDavid Gibson } 180f00bed95SDavid Gibson } 18198248918SDavid Gibson ppc_store_lpcr(newcpu, lpcr); 18298248918SDavid Gibson 18398248918SDavid Gibson /* 18498248918SDavid Gibson * Set the timebase offset of the new CPU to that of the invoking 18598248918SDavid Gibson * CPU. This helps hotplugged CPU to have the correct timebase 18698248918SDavid Gibson * offset. 18798248918SDavid Gibson */ 18898248918SDavid Gibson newcpu->env.tb_env->tb_offset = callcpu->env.tb_env->tb_offset; 1899a94ee5bSCédric Le Goater 19084369f63SDavid Gibson spapr_cpu_set_entry_state(newcpu, start, r3); 191cf116ad4SDavid Gibson 192cf116ad4SDavid Gibson qemu_cpu_kick(CPU(newcpu)); 193a9f8ad8fSDavid Gibson 194a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 195a9f8ad8fSDavid Gibson } 196a9f8ad8fSDavid Gibson 19728e02042SDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr, 19859760f2dSAlexey Kardashevskiy uint32_t token, uint32_t nargs, 19959760f2dSAlexey Kardashevskiy target_ulong args, 20059760f2dSAlexey Kardashevskiy uint32_t nret, target_ulong rets) 20159760f2dSAlexey Kardashevskiy { 20259760f2dSAlexey Kardashevskiy CPUState *cs = CPU(cpu); 20359760f2dSAlexey Kardashevskiy CPUPPCState *env = &cpu->env; 2049a94ee5bSCédric Le Goater PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 20559760f2dSAlexey Kardashevskiy 2069a94ee5bSCédric Le Goater /* Disable Power-saving mode Exit Cause exceptions for the CPU. 2079a94ee5bSCédric Le Goater * This could deliver an interrupt on a dying CPU and crash the 2089a94ee5bSCédric Le Goater * guest */ 209cf116ad4SDavid Gibson ppc_store_lpcr(cpu, env->spr[SPR_LPCR] & ~pcc->lpcr_pm); 210cf116ad4SDavid Gibson cs->halted = 1; 211*a84f7179SNikunj A Dadhania kvmppc_set_reg_ppc_online(cpu, 0); 212cf116ad4SDavid Gibson qemu_cpu_kick(cs); 21359760f2dSAlexey Kardashevskiy } 21459760f2dSAlexey Kardashevskiy 215c920f7b4SDavid Gibson static inline int sysparm_st(target_ulong addr, target_ulong len, 216c920f7b4SDavid Gibson const void *val, uint16_t vallen) 217c920f7b4SDavid Gibson { 218c920f7b4SDavid Gibson hwaddr phys = ppc64_phys_to_real(addr); 219c920f7b4SDavid Gibson 220c920f7b4SDavid Gibson if (len < 2) { 221c920f7b4SDavid Gibson return RTAS_OUT_SYSPARM_PARAM_ERROR; 222c920f7b4SDavid Gibson } 223c920f7b4SDavid Gibson stw_be_phys(&address_space_memory, phys, vallen); 224c920f7b4SDavid Gibson cpu_physical_memory_write(phys + 2, val, MIN(len - 2, vallen)); 225c920f7b4SDavid Gibson return RTAS_OUT_SUCCESS; 226c920f7b4SDavid Gibson } 227c920f7b4SDavid Gibson 2283ada6b11SAlexey Kardashevskiy static void rtas_ibm_get_system_parameter(PowerPCCPU *cpu, 22928e02042SDavid Gibson sPAPRMachineState *spapr, 2303ada6b11SAlexey Kardashevskiy uint32_t token, uint32_t nargs, 2313ada6b11SAlexey Kardashevskiy target_ulong args, 2323ada6b11SAlexey Kardashevskiy uint32_t nret, target_ulong rets) 2333ada6b11SAlexey Kardashevskiy { 2343ada6b11SAlexey Kardashevskiy target_ulong parameter = rtas_ld(args, 0); 2353ada6b11SAlexey Kardashevskiy target_ulong buffer = rtas_ld(args, 1); 2363ada6b11SAlexey Kardashevskiy target_ulong length = rtas_ld(args, 2); 237c920f7b4SDavid Gibson target_ulong ret; 2383ada6b11SAlexey Kardashevskiy 2393ada6b11SAlexey Kardashevskiy switch (parameter) { 2403b50d897SSam bobroff case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: { 241e3943228SSam Bobroff char *param_val = g_strdup_printf("MaxEntCap=%d," 242ab3dd749SPhilippe Mathieu-Daudé "DesMem=%" PRIu64 "," 243e3943228SSam Bobroff "DesProcs=%d," 244e3943228SSam Bobroff "MaxPlatProcs=%d", 245e3943228SSam Bobroff max_cpus, 246d23b6caaSPhilippe Mathieu-Daudé current_machine->ram_size / MiB, 247e3943228SSam Bobroff smp_cpus, 248e3943228SSam Bobroff max_cpus); 249c920f7b4SDavid Gibson ret = sysparm_st(buffer, length, param_val, strlen(param_val) + 1); 2503b50d897SSam bobroff g_free(param_val); 2513b50d897SSam bobroff break; 2523b50d897SSam bobroff } 2533052d951SSam bobroff case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: { 2543052d951SSam bobroff uint8_t param_val = DIAGNOSTICS_RUN_MODE_DISABLED; 2553052d951SSam bobroff 256c920f7b4SDavid Gibson ret = sysparm_st(buffer, length, ¶m_val, sizeof(param_val)); 2573ada6b11SAlexey Kardashevskiy break; 2583ada6b11SAlexey Kardashevskiy } 259b907d7b0SSam bobroff case RTAS_SYSPARM_UUID: 2609c5ce8dbSFam Zheng ret = sysparm_st(buffer, length, (unsigned char *)&qemu_uuid, 2619c5ce8dbSFam Zheng (qemu_uuid_set ? 16 : 0)); 262b907d7b0SSam bobroff break; 2633052d951SSam bobroff default: 2643052d951SSam bobroff ret = RTAS_OUT_NOT_SUPPORTED; 2653052d951SSam bobroff } 2663ada6b11SAlexey Kardashevskiy 2673ada6b11SAlexey Kardashevskiy rtas_st(rets, 0, ret); 2683ada6b11SAlexey Kardashevskiy } 2693ada6b11SAlexey Kardashevskiy 2703ada6b11SAlexey Kardashevskiy static void rtas_ibm_set_system_parameter(PowerPCCPU *cpu, 27128e02042SDavid Gibson sPAPRMachineState *spapr, 2723ada6b11SAlexey Kardashevskiy uint32_t token, uint32_t nargs, 2733ada6b11SAlexey Kardashevskiy target_ulong args, 2743ada6b11SAlexey Kardashevskiy uint32_t nret, target_ulong rets) 2753ada6b11SAlexey Kardashevskiy { 2763ada6b11SAlexey Kardashevskiy target_ulong parameter = rtas_ld(args, 0); 2773ada6b11SAlexey Kardashevskiy target_ulong ret = RTAS_OUT_NOT_SUPPORTED; 2783ada6b11SAlexey Kardashevskiy 2793ada6b11SAlexey Kardashevskiy switch (parameter) { 2803b50d897SSam bobroff case RTAS_SYSPARM_SPLPAR_CHARACTERISTICS: 2813052d951SSam bobroff case RTAS_SYSPARM_DIAGNOSTICS_RUN_MODE: 282b907d7b0SSam bobroff case RTAS_SYSPARM_UUID: 2833ada6b11SAlexey Kardashevskiy ret = RTAS_OUT_NOT_AUTHORIZED; 2843ada6b11SAlexey Kardashevskiy break; 2853ada6b11SAlexey Kardashevskiy } 2863ada6b11SAlexey Kardashevskiy 2873ada6b11SAlexey Kardashevskiy rtas_st(rets, 0, ret); 2883ada6b11SAlexey Kardashevskiy } 2893ada6b11SAlexey Kardashevskiy 2902e14072fSNikunj A Dadhania static void rtas_ibm_os_term(PowerPCCPU *cpu, 29128e02042SDavid Gibson sPAPRMachineState *spapr, 2922e14072fSNikunj A Dadhania uint32_t token, uint32_t nargs, 2932e14072fSNikunj A Dadhania target_ulong args, 2942e14072fSNikunj A Dadhania uint32_t nret, target_ulong rets) 2952e14072fSNikunj A Dadhania { 2962c553477SDavid Gibson qemu_system_guest_panicked(NULL); 2972e14072fSNikunj A Dadhania 2982c553477SDavid Gibson rtas_st(rets, 0, RTAS_OUT_SUCCESS); 2992e14072fSNikunj A Dadhania } 3002e14072fSNikunj A Dadhania 30128e02042SDavid Gibson static void rtas_set_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr, 302094d2058SNathan Fontenot uint32_t token, uint32_t nargs, 303094d2058SNathan Fontenot target_ulong args, uint32_t nret, 304094d2058SNathan Fontenot target_ulong rets) 305094d2058SNathan Fontenot { 306094d2058SNathan Fontenot int32_t power_domain; 307094d2058SNathan Fontenot 308094d2058SNathan Fontenot if (nargs != 2 || nret != 2) { 309094d2058SNathan Fontenot rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 310094d2058SNathan Fontenot return; 311094d2058SNathan Fontenot } 312094d2058SNathan Fontenot 313094d2058SNathan Fontenot /* we currently only use a single, "live insert" powerdomain for 314094d2058SNathan Fontenot * hotplugged/dlpar'd resources, so the power is always live/full (100) 315094d2058SNathan Fontenot */ 316094d2058SNathan Fontenot power_domain = rtas_ld(args, 0); 317094d2058SNathan Fontenot if (power_domain != -1) { 318094d2058SNathan Fontenot rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED); 319094d2058SNathan Fontenot return; 320094d2058SNathan Fontenot } 321094d2058SNathan Fontenot 322094d2058SNathan Fontenot rtas_st(rets, 0, RTAS_OUT_SUCCESS); 323094d2058SNathan Fontenot rtas_st(rets, 1, 100); 324094d2058SNathan Fontenot } 325094d2058SNathan Fontenot 32628e02042SDavid Gibson static void rtas_get_power_level(PowerPCCPU *cpu, sPAPRMachineState *spapr, 327094d2058SNathan Fontenot uint32_t token, uint32_t nargs, 328094d2058SNathan Fontenot target_ulong args, uint32_t nret, 329094d2058SNathan Fontenot target_ulong rets) 330094d2058SNathan Fontenot { 331094d2058SNathan Fontenot int32_t power_domain; 332094d2058SNathan Fontenot 333094d2058SNathan Fontenot if (nargs != 1 || nret != 2) { 334094d2058SNathan Fontenot rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 335094d2058SNathan Fontenot return; 336094d2058SNathan Fontenot } 337094d2058SNathan Fontenot 338094d2058SNathan Fontenot /* we currently only use a single, "live insert" powerdomain for 339094d2058SNathan Fontenot * hotplugged/dlpar'd resources, so the power is always live/full (100) 340094d2058SNathan Fontenot */ 341094d2058SNathan Fontenot power_domain = rtas_ld(args, 0); 342094d2058SNathan Fontenot if (power_domain != -1) { 343094d2058SNathan Fontenot rtas_st(rets, 0, RTAS_OUT_NOT_SUPPORTED); 344094d2058SNathan Fontenot return; 345094d2058SNathan Fontenot } 346094d2058SNathan Fontenot 347094d2058SNathan Fontenot rtas_st(rets, 0, RTAS_OUT_SUCCESS); 348094d2058SNathan Fontenot rtas_st(rets, 1, 100); 349094d2058SNathan Fontenot } 350094d2058SNathan Fontenot 35139ac8455SDavid Gibson static struct rtas_call { 35239ac8455SDavid Gibson const char *name; 35339ac8455SDavid Gibson spapr_rtas_fn fn; 3543a3b8502SAlexey Kardashevskiy } rtas_table[RTAS_TOKEN_MAX - RTAS_TOKEN_BASE]; 35539ac8455SDavid Gibson 35628e02042SDavid Gibson target_ulong spapr_rtas_call(PowerPCCPU *cpu, sPAPRMachineState *spapr, 35739ac8455SDavid Gibson uint32_t token, uint32_t nargs, target_ulong args, 35839ac8455SDavid Gibson uint32_t nret, target_ulong rets) 35939ac8455SDavid Gibson { 3603a3b8502SAlexey Kardashevskiy if ((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)) { 3613a3b8502SAlexey Kardashevskiy struct rtas_call *call = rtas_table + (token - RTAS_TOKEN_BASE); 36239ac8455SDavid Gibson 36339ac8455SDavid Gibson if (call->fn) { 364210b580bSAnthony Liguori call->fn(cpu, spapr, token, nargs, args, nret, rets); 36539ac8455SDavid Gibson return H_SUCCESS; 36639ac8455SDavid Gibson } 36739ac8455SDavid Gibson } 36839ac8455SDavid Gibson 369821303f5SDavid Gibson /* HACK: Some Linux early debug code uses RTAS display-character, 370821303f5SDavid Gibson * but assumes the token value is 0xa (which it is on some real 371821303f5SDavid Gibson * machines) without looking it up in the device tree. This 372821303f5SDavid Gibson * special case makes this work */ 373821303f5SDavid Gibson if (token == 0xa) { 374210b580bSAnthony Liguori rtas_display_character(cpu, spapr, 0xa, nargs, args, nret, rets); 375821303f5SDavid Gibson return H_SUCCESS; 376821303f5SDavid Gibson } 377821303f5SDavid Gibson 37839ac8455SDavid Gibson hcall_dprintf("Unknown RTAS token 0x%x\n", token); 379a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 38039ac8455SDavid Gibson return H_PARAMETER; 38139ac8455SDavid Gibson } 38239ac8455SDavid Gibson 383eeddd59fSLaurent Vivier uint64_t qtest_rtas_call(char *cmd, uint32_t nargs, uint64_t args, 384eeddd59fSLaurent Vivier uint32_t nret, uint64_t rets) 385eeddd59fSLaurent Vivier { 386eeddd59fSLaurent Vivier int token; 387eeddd59fSLaurent Vivier 388eeddd59fSLaurent Vivier for (token = 0; token < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; token++) { 389eeddd59fSLaurent Vivier if (strcmp(cmd, rtas_table[token].name) == 0) { 390eeddd59fSLaurent Vivier sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 391eeddd59fSLaurent Vivier PowerPCCPU *cpu = POWERPC_CPU(first_cpu); 392eeddd59fSLaurent Vivier 393eeddd59fSLaurent Vivier rtas_table[token].fn(cpu, spapr, token + RTAS_TOKEN_BASE, 394eeddd59fSLaurent Vivier nargs, args, nret, rets); 395eeddd59fSLaurent Vivier return H_SUCCESS; 396eeddd59fSLaurent Vivier } 397eeddd59fSLaurent Vivier } 398eeddd59fSLaurent Vivier return H_PARAMETER; 399eeddd59fSLaurent Vivier } 400eeddd59fSLaurent Vivier 4013a3b8502SAlexey Kardashevskiy void spapr_rtas_register(int token, const char *name, spapr_rtas_fn fn) 40239ac8455SDavid Gibson { 403adf9ac50SDavid Gibson assert((token >= RTAS_TOKEN_BASE) && (token < RTAS_TOKEN_MAX)); 4043a3b8502SAlexey Kardashevskiy 4053a3b8502SAlexey Kardashevskiy token -= RTAS_TOKEN_BASE; 406adf9ac50SDavid Gibson 407adf9ac50SDavid Gibson assert(!rtas_table[token].name); 408c89d5299SDavid Gibson 4093a3b8502SAlexey Kardashevskiy rtas_table[token].name = name; 4103a3b8502SAlexey Kardashevskiy rtas_table[token].fn = fn; 41139ac8455SDavid Gibson } 41239ac8455SDavid Gibson 4133f5dabceSDavid Gibson void spapr_dt_rtas_tokens(void *fdt, int rtas) 41439ac8455SDavid Gibson { 41539ac8455SDavid Gibson int i; 41639ac8455SDavid Gibson 4173a3b8502SAlexey Kardashevskiy for (i = 0; i < RTAS_TOKEN_MAX - RTAS_TOKEN_BASE; i++) { 41839ac8455SDavid Gibson struct rtas_call *call = &rtas_table[i]; 41939ac8455SDavid Gibson 420d36b66f7SBen Herrenschmidt if (!call->name) { 42139ac8455SDavid Gibson continue; 42239ac8455SDavid Gibson } 42339ac8455SDavid Gibson 4243f5dabceSDavid Gibson _FDT(fdt_setprop_cell(fdt, rtas, call->name, i + RTAS_TOKEN_BASE)); 42539ac8455SDavid Gibson } 42639ac8455SDavid Gibson } 427821303f5SDavid Gibson 4282cac78c1SDavid Gibson void spapr_load_rtas(sPAPRMachineState *spapr, void *fdt, hwaddr addr) 4292cac78c1SDavid Gibson { 4302cac78c1SDavid Gibson int rtas_node; 4312cac78c1SDavid Gibson int ret; 4322cac78c1SDavid Gibson 4332cac78c1SDavid Gibson /* Copy RTAS blob into guest RAM */ 4342cac78c1SDavid Gibson cpu_physical_memory_write(addr, spapr->rtas_blob, spapr->rtas_size); 4352cac78c1SDavid Gibson 4362cac78c1SDavid Gibson ret = fdt_add_mem_rsv(fdt, addr, spapr->rtas_size); 4372cac78c1SDavid Gibson if (ret < 0) { 4382cac78c1SDavid Gibson error_report("Couldn't add RTAS reserve entry: %s", 4392cac78c1SDavid Gibson fdt_strerror(ret)); 4402cac78c1SDavid Gibson exit(1); 4412cac78c1SDavid Gibson } 4422cac78c1SDavid Gibson 4432cac78c1SDavid Gibson /* Update the device tree with the blob's location */ 4442cac78c1SDavid Gibson rtas_node = fdt_path_offset(fdt, "/rtas"); 4452cac78c1SDavid Gibson assert(rtas_node >= 0); 4462cac78c1SDavid Gibson 4472cac78c1SDavid Gibson ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-base", addr); 4482cac78c1SDavid Gibson if (ret < 0) { 4492cac78c1SDavid Gibson error_report("Couldn't add linux,rtas-base property: %s", 4502cac78c1SDavid Gibson fdt_strerror(ret)); 4512cac78c1SDavid Gibson exit(1); 4522cac78c1SDavid Gibson } 4532cac78c1SDavid Gibson 4542cac78c1SDavid Gibson ret = fdt_setprop_cell(fdt, rtas_node, "linux,rtas-entry", addr); 4552cac78c1SDavid Gibson if (ret < 0) { 4562cac78c1SDavid Gibson error_report("Couldn't add linux,rtas-entry property: %s", 4572cac78c1SDavid Gibson fdt_strerror(ret)); 4582cac78c1SDavid Gibson exit(1); 4592cac78c1SDavid Gibson } 4602cac78c1SDavid Gibson 4612cac78c1SDavid Gibson ret = fdt_setprop_cell(fdt, rtas_node, "rtas-size", spapr->rtas_size); 4622cac78c1SDavid Gibson if (ret < 0) { 4632cac78c1SDavid Gibson error_report("Couldn't add rtas-size property: %s", 4642cac78c1SDavid Gibson fdt_strerror(ret)); 4652cac78c1SDavid Gibson exit(1); 4662cac78c1SDavid Gibson } 4672cac78c1SDavid Gibson } 4682cac78c1SDavid Gibson 46983f7d43aSAndreas Färber static void core_rtas_register_types(void) 470821303f5SDavid Gibson { 4713a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_DISPLAY_CHARACTER, "display-character", 4723a3b8502SAlexey Kardashevskiy rtas_display_character); 4733a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_POWER_OFF, "power-off", rtas_power_off); 4743a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_SYSTEM_REBOOT, "system-reboot", 4753a3b8502SAlexey Kardashevskiy rtas_system_reboot); 4763a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_QUERY_CPU_STOPPED_STATE, "query-cpu-stopped-state", 477a9f8ad8fSDavid Gibson rtas_query_cpu_stopped_state); 4783a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_START_CPU, "start-cpu", rtas_start_cpu); 4793a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_STOP_SELF, "stop-self", rtas_stop_self); 4803a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_IBM_GET_SYSTEM_PARAMETER, 4813a3b8502SAlexey Kardashevskiy "ibm,get-system-parameter", 4823ada6b11SAlexey Kardashevskiy rtas_ibm_get_system_parameter); 4833a3b8502SAlexey Kardashevskiy spapr_rtas_register(RTAS_IBM_SET_SYSTEM_PARAMETER, 4843a3b8502SAlexey Kardashevskiy "ibm,set-system-parameter", 4853ada6b11SAlexey Kardashevskiy rtas_ibm_set_system_parameter); 4862e14072fSNikunj A Dadhania spapr_rtas_register(RTAS_IBM_OS_TERM, "ibm,os-term", 4872e14072fSNikunj A Dadhania rtas_ibm_os_term); 488094d2058SNathan Fontenot spapr_rtas_register(RTAS_SET_POWER_LEVEL, "set-power-level", 489094d2058SNathan Fontenot rtas_set_power_level); 490094d2058SNathan Fontenot spapr_rtas_register(RTAS_GET_POWER_LEVEL, "get-power-level", 491094d2058SNathan Fontenot rtas_get_power_level); 492821303f5SDavid Gibson } 49383f7d43aSAndreas Färber 49483f7d43aSAndreas Färber type_init(core_rtas_register_types) 495