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" 3577ac58ddSPaolo Bonzini #include "sysemu/kvm.h" 3639ac8455SDavid Gibson 370d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h" 380d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h" 39eeddd59fSLaurent Vivier #include "hw/ppc/spapr_rtas.h" 40af81cf32SBharata B Rao #include "hw/ppc/ppc.h" 41e010ad8fSWenchao Xia #include "qapi-event.h" 42e3943228SSam Bobroff #include "hw/boards.h" 4339ac8455SDavid Gibson 4439ac8455SDavid Gibson #include <libfdt.h> 458c8639dfSMike Day #include "hw/ppc/spapr_drc.h" 46f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 47028ec3ceSLaurent Vivier #include "trace.h" 483f5dabceSDavid Gibson #include "hw/ppc/fdt.h" 498c8639dfSMike Day 5028e02042SDavid Gibson static void rtas_display_character(PowerPCCPU *cpu, sPAPRMachineState *spapr, 51821303f5SDavid Gibson uint32_t token, uint32_t nargs, 52821303f5SDavid Gibson target_ulong args, 53821303f5SDavid Gibson uint32_t nret, target_ulong rets) 54821303f5SDavid Gibson { 55821303f5SDavid Gibson uint8_t c = rtas_ld(args, 0); 565f2e2ba2SDavid Gibson VIOsPAPRDevice *sdev = vty_lookup(spapr, 0); 57821303f5SDavid Gibson 58821303f5SDavid Gibson if (!sdev) { 59a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 60821303f5SDavid Gibson } else { 61821303f5SDavid Gibson vty_putchars(sdev, &c, sizeof(c)); 62a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 63821303f5SDavid Gibson } 64821303f5SDavid Gibson } 65821303f5SDavid Gibson 6628e02042SDavid Gibson static void rtas_power_off(PowerPCCPU *cpu, sPAPRMachineState *spapr, 67821303f5SDavid Gibson uint32_t token, uint32_t nargs, target_ulong args, 68821303f5SDavid Gibson uint32_t nret, target_ulong rets) 69821303f5SDavid Gibson { 70821303f5SDavid Gibson if (nargs != 2 || nret != 1) { 71a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 72821303f5SDavid Gibson return; 73821303f5SDavid Gibson } 74cf83f140SEric Blake qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 758a9c1b77SThomas Huth cpu_stop_current(); 76a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 77821303f5SDavid Gibson } 78821303f5SDavid Gibson 7928e02042SDavid Gibson static void rtas_system_reboot(PowerPCCPU *cpu, sPAPRMachineState *spapr, 80c821a43cSDavid Gibson uint32_t token, uint32_t nargs, 81c821a43cSDavid Gibson target_ulong args, 82c821a43cSDavid Gibson uint32_t nret, target_ulong rets) 83c821a43cSDavid Gibson { 84c821a43cSDavid Gibson if (nargs != 0 || nret != 1) { 85a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 86c821a43cSDavid Gibson return; 87c821a43cSDavid Gibson } 88cf83f140SEric Blake qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 89a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 90c821a43cSDavid Gibson } 91c821a43cSDavid Gibson 92210b580bSAnthony Liguori static void rtas_query_cpu_stopped_state(PowerPCCPU *cpu_, 9328e02042SDavid Gibson sPAPRMachineState *spapr, 94a9f8ad8fSDavid Gibson uint32_t token, uint32_t nargs, 95a9f8ad8fSDavid Gibson target_ulong args, 96a9f8ad8fSDavid Gibson uint32_t nret, target_ulong rets) 97a9f8ad8fSDavid Gibson { 98a9f8ad8fSDavid Gibson target_ulong id; 990f20ba62SAlexey Kardashevskiy PowerPCCPU *cpu; 100a9f8ad8fSDavid Gibson 101a9f8ad8fSDavid Gibson if (nargs != 1 || nret != 2) { 102a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 103a9f8ad8fSDavid Gibson return; 104a9f8ad8fSDavid Gibson } 105a9f8ad8fSDavid Gibson 106a9f8ad8fSDavid Gibson id = rtas_ld(args, 0); 1070f20ba62SAlexey Kardashevskiy cpu = ppc_get_vcpu_by_dt_id(id); 10805318a85SAndreas Färber if (cpu != NULL) { 1090f20ba62SAlexey Kardashevskiy if (CPU(cpu)->halted) { 110a9f8ad8fSDavid Gibson rtas_st(rets, 1, 0); 111a9f8ad8fSDavid Gibson } else { 112a9f8ad8fSDavid Gibson rtas_st(rets, 1, 2); 113a9f8ad8fSDavid Gibson } 114a9f8ad8fSDavid Gibson 115a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 116a9f8ad8fSDavid Gibson return; 117a9f8ad8fSDavid Gibson } 118a9f8ad8fSDavid Gibson 119a9f8ad8fSDavid Gibson /* Didn't find a matching cpu */ 120a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 121a9f8ad8fSDavid Gibson } 122a9f8ad8fSDavid Gibson 123af81cf32SBharata B Rao /* 124af81cf32SBharata B Rao * Set the timebase offset of the CPU to that of first CPU. 125af81cf32SBharata B Rao * This helps hotplugged CPU to have the correct timebase offset. 126af81cf32SBharata B Rao */ 127af81cf32SBharata B Rao static void spapr_cpu_update_tb_offset(PowerPCCPU *cpu) 128af81cf32SBharata B Rao { 129af81cf32SBharata B Rao PowerPCCPU *fcpu = POWERPC_CPU(first_cpu); 130af81cf32SBharata B Rao 131af81cf32SBharata B Rao cpu->env.tb_env->tb_offset = fcpu->env.tb_env->tb_offset; 132af81cf32SBharata B Rao } 133af81cf32SBharata B Rao 134af81cf32SBharata B Rao static void spapr_cpu_set_endianness(PowerPCCPU *cpu) 135af81cf32SBharata B Rao { 136af81cf32SBharata B Rao PowerPCCPU *fcpu = POWERPC_CPU(first_cpu); 137af81cf32SBharata B Rao PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(fcpu); 138af81cf32SBharata B Rao 139af81cf32SBharata B Rao if (!pcc->interrupts_big_endian(fcpu)) { 140af81cf32SBharata B Rao cpu->env.spr[SPR_LPCR] |= LPCR_ILE; 141af81cf32SBharata B Rao } 142af81cf32SBharata B Rao } 143af81cf32SBharata B Rao 14428e02042SDavid Gibson static void rtas_start_cpu(PowerPCCPU *cpu_, sPAPRMachineState *spapr, 145a9f8ad8fSDavid Gibson uint32_t token, uint32_t nargs, 146a9f8ad8fSDavid Gibson target_ulong args, 147a9f8ad8fSDavid Gibson uint32_t nret, target_ulong rets) 148a9f8ad8fSDavid Gibson { 149a9f8ad8fSDavid Gibson target_ulong id, start, r3; 1500f20ba62SAlexey Kardashevskiy PowerPCCPU *cpu; 151a9f8ad8fSDavid Gibson 152a9f8ad8fSDavid Gibson if (nargs != 3 || nret != 1) { 153a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 154a9f8ad8fSDavid Gibson return; 155a9f8ad8fSDavid Gibson } 156a9f8ad8fSDavid Gibson 157a9f8ad8fSDavid Gibson id = rtas_ld(args, 0); 158a9f8ad8fSDavid Gibson start = rtas_ld(args, 1); 159a9f8ad8fSDavid Gibson r3 = rtas_ld(args, 2); 160a9f8ad8fSDavid Gibson 1610f20ba62SAlexey Kardashevskiy cpu = ppc_get_vcpu_by_dt_id(id); 1620f20ba62SAlexey Kardashevskiy if (cpu != NULL) { 1630f20ba62SAlexey Kardashevskiy CPUState *cs = CPU(cpu); 164c67e216bSAndreas Färber CPUPPCState *env = &cpu->env; 165c08d7424SAndreas Färber 166c67e216bSAndreas Färber if (!cs->halted) { 167a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 168a9f8ad8fSDavid Gibson return; 169a9f8ad8fSDavid Gibson } 170a9f8ad8fSDavid Gibson 171048706d9SDavid Gibson /* This will make sure qemu state is up to date with kvm, and 172048706d9SDavid Gibson * mark it dirty so our changes get flushed back before the 173048706d9SDavid Gibson * new cpu enters */ 174dd1750d7SAndreas Färber kvm_cpu_synchronize_state(cs); 175048706d9SDavid Gibson 176a9f8ad8fSDavid Gibson env->msr = (1ULL << MSR_SF) | (1ULL << MSR_ME); 177a9f8ad8fSDavid Gibson env->nip = start; 178a9f8ad8fSDavid Gibson env->gpr[3] = r3; 179c67e216bSAndreas Färber cs->halted = 0; 180af81cf32SBharata B Rao spapr_cpu_set_endianness(cpu); 181af81cf32SBharata B Rao spapr_cpu_update_tb_offset(cpu); 182a9f8ad8fSDavid Gibson 183c67e216bSAndreas Färber qemu_cpu_kick(cs); 184a9f8ad8fSDavid Gibson 185a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_SUCCESS); 186a9f8ad8fSDavid Gibson return; 187a9f8ad8fSDavid Gibson } 188a9f8ad8fSDavid Gibson 189a9f8ad8fSDavid Gibson /* Didn't find a matching cpu */ 190a64d325dSAlexey Kardashevskiy rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 191a9f8ad8fSDavid Gibson } 192a9f8ad8fSDavid Gibson 19328e02042SDavid Gibson static void rtas_stop_self(PowerPCCPU *cpu, sPAPRMachineState *spapr, 19459760f2dSAlexey Kardashevskiy uint32_t token, uint32_t nargs, 19559760f2dSAlexey Kardashevskiy target_ulong args, 19659760f2dSAlexey Kardashevskiy uint32_t nret, target_ulong rets) 19759760f2dSAlexey Kardashevskiy { 19859760f2dSAlexey Kardashevskiy CPUState *cs = CPU(cpu); 19959760f2dSAlexey Kardashevskiy CPUPPCState *env = &cpu->env; 20059760f2dSAlexey Kardashevskiy 20159760f2dSAlexey Kardashevskiy cs->halted = 1; 2029102dedaSPaolo Bonzini qemu_cpu_kick(cs); 20359760f2dSAlexey Kardashevskiy /* 20459760f2dSAlexey Kardashevskiy * While stopping a CPU, the guest calls H_CPPR which 20559760f2dSAlexey Kardashevskiy * effectively disables interrupts on XICS level. 20659760f2dSAlexey Kardashevskiy * However decrementer interrupts in TCG can still 20759760f2dSAlexey Kardashevskiy * wake the CPU up so here we disable interrupts in MSR 20859760f2dSAlexey Kardashevskiy * as well. 20959760f2dSAlexey Kardashevskiy * As rtas_start_cpu() resets the whole MSR anyway, there is 21059760f2dSAlexey Kardashevskiy * no need to bother with specific bits, we just clear it. 21159760f2dSAlexey Kardashevskiy */ 21259760f2dSAlexey Kardashevskiy env->msr = 0; 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," 242e3943228SSam Bobroff "DesMem=%llu," 243e3943228SSam Bobroff "DesProcs=%d," 244e3943228SSam Bobroff "MaxPlatProcs=%d", 245e3943228SSam Bobroff max_cpus, 246e3943228SSam Bobroff current_machine->ram_size / M_BYTE, 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 { 296*2c553477SDavid Gibson qemu_system_guest_panicked(NULL); 2972e14072fSNikunj A Dadhania 298*2c553477SDavid 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