1901c4eafSBlue Swirl /* 2901c4eafSBlue Swirl * Miscellaneous PowerPC emulation helpers for QEMU. 3901c4eafSBlue Swirl * 4901c4eafSBlue Swirl * Copyright (c) 2003-2007 Jocelyn Mayer 5901c4eafSBlue Swirl * 6901c4eafSBlue Swirl * This library is free software; you can redistribute it and/or 7901c4eafSBlue Swirl * modify it under the terms of the GNU Lesser General Public 8901c4eafSBlue Swirl * License as published by the Free Software Foundation; either 96bd039cdSChetan Pant * version 2.1 of the License, or (at your option) any later version. 10901c4eafSBlue Swirl * 11901c4eafSBlue Swirl * This library is distributed in the hope that it will be useful, 12901c4eafSBlue Swirl * but WITHOUT ANY WARRANTY; without even the implied warranty of 13901c4eafSBlue Swirl * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14901c4eafSBlue Swirl * Lesser General Public License for more details. 15901c4eafSBlue Swirl * 16901c4eafSBlue Swirl * You should have received a copy of the GNU Lesser General Public 17901c4eafSBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18901c4eafSBlue Swirl */ 19db725815SMarkus Armbruster 200d75590dSPeter Maydell #include "qemu/osdep.h" 21901c4eafSBlue Swirl #include "cpu.h" 2263c91552SPaolo Bonzini #include "exec/exec-all.h" 232ef6175aSRichard Henderson #include "exec/helper-proto.h" 246b375544SJoel Stanley #include "qemu/error-report.h" 25db725815SMarkus Armbruster #include "qemu/main-loop.h" 26*22adb61fSBruno Larsen (billionai) #include "mmu-book3s-v3.h" 27901c4eafSBlue Swirl 28901c4eafSBlue Swirl #include "helper_regs.h" 29901c4eafSBlue Swirl 30901c4eafSBlue Swirl /*****************************************************************************/ 31901c4eafSBlue Swirl /* SPR accesses */ 32d523dd00SBlue Swirl void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn) 33901c4eafSBlue Swirl { 34901c4eafSBlue Swirl qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn, 35901c4eafSBlue Swirl env->spr[sprn]); 36901c4eafSBlue Swirl } 37901c4eafSBlue Swirl 38d523dd00SBlue Swirl void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn) 39901c4eafSBlue Swirl { 40901c4eafSBlue Swirl qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn, 41901c4eafSBlue Swirl env->spr[sprn]); 42901c4eafSBlue Swirl } 437019cb3dSAlexey Kardashevskiy 447019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64 45493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit, 46493028d8SCédric Le Goater const char *caller, uint32_t cause, 47493028d8SCédric Le Goater uintptr_t raddr) 48493028d8SCédric Le Goater { 49493028d8SCédric Le Goater qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n", 50493028d8SCédric Le Goater bit, caller); 51493028d8SCédric Le Goater 52493028d8SCédric Le Goater env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 53493028d8SCédric Le Goater 54493028d8SCédric Le Goater raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr); 55493028d8SCédric Le Goater } 56493028d8SCédric Le Goater 577019cb3dSAlexey Kardashevskiy static void raise_fu_exception(CPUPPCState *env, uint32_t bit, 5857a2988bSBenjamin Herrenschmidt uint32_t sprn, uint32_t cause, 5957a2988bSBenjamin Herrenschmidt uintptr_t raddr) 607019cb3dSAlexey Kardashevskiy { 617019cb3dSAlexey Kardashevskiy qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit); 627019cb3dSAlexey Kardashevskiy 637019cb3dSAlexey Kardashevskiy env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 647019cb3dSAlexey Kardashevskiy cause &= FSCR_IC_MASK; 657019cb3dSAlexey Kardashevskiy env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS; 667019cb3dSAlexey Kardashevskiy 6757a2988bSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr); 687019cb3dSAlexey Kardashevskiy } 697019cb3dSAlexey Kardashevskiy #endif 707019cb3dSAlexey Kardashevskiy 71493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit, 72493028d8SCédric Le Goater const char *caller, uint32_t cause) 73493028d8SCédric Le Goater { 74493028d8SCédric Le Goater #ifdef TARGET_PPC64 75493028d8SCédric Le Goater if ((env->msr_mask & MSR_HVB) && !msr_hv && 76493028d8SCédric Le Goater !(env->spr[SPR_HFSCR] & (1UL << bit))) { 77493028d8SCédric Le Goater raise_hv_fu_exception(env, bit, caller, cause, GETPC()); 78493028d8SCédric Le Goater } 79493028d8SCédric Le Goater #endif 80493028d8SCédric Le Goater } 81493028d8SCédric Le Goater 827019cb3dSAlexey Kardashevskiy void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit, 837019cb3dSAlexey Kardashevskiy uint32_t sprn, uint32_t cause) 847019cb3dSAlexey Kardashevskiy { 857019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64 867019cb3dSAlexey Kardashevskiy if (env->spr[SPR_FSCR] & (1ULL << bit)) { 877019cb3dSAlexey Kardashevskiy /* Facility is enabled, continue */ 887019cb3dSAlexey Kardashevskiy return; 897019cb3dSAlexey Kardashevskiy } 9057a2988bSBenjamin Herrenschmidt raise_fu_exception(env, bit, sprn, cause, GETPC()); 917019cb3dSAlexey Kardashevskiy #endif 927019cb3dSAlexey Kardashevskiy } 937019cb3dSAlexey Kardashevskiy 94cdcdda27SAlexey Kardashevskiy void helper_msr_facility_check(CPUPPCState *env, uint32_t bit, 95cdcdda27SAlexey Kardashevskiy uint32_t sprn, uint32_t cause) 96cdcdda27SAlexey Kardashevskiy { 97cdcdda27SAlexey Kardashevskiy #ifdef TARGET_PPC64 98cdcdda27SAlexey Kardashevskiy if (env->msr & (1ULL << bit)) { 99cdcdda27SAlexey Kardashevskiy /* Facility is enabled, continue */ 100cdcdda27SAlexey Kardashevskiy return; 101cdcdda27SAlexey Kardashevskiy } 10257a2988bSBenjamin Herrenschmidt raise_fu_exception(env, bit, sprn, cause, GETPC()); 103cdcdda27SAlexey Kardashevskiy #endif 104cdcdda27SAlexey Kardashevskiy } 105cdcdda27SAlexey Kardashevskiy 106901c4eafSBlue Swirl #if !defined(CONFIG_USER_ONLY) 107901c4eafSBlue Swirl 108d523dd00SBlue Swirl void helper_store_sdr1(CPUPPCState *env, target_ulong val) 109901c4eafSBlue Swirl { 1102828c4cdSMark Cave-Ayland if (env->spr[SPR_SDR1] != val) { 111901c4eafSBlue Swirl ppc_store_sdr1(env, val); 112db70b311SRichard Henderson tlb_flush(env_cpu(env)); 1132828c4cdSMark Cave-Ayland } 114901c4eafSBlue Swirl } 115901c4eafSBlue Swirl 1164a7518e0SCédric Le Goater #if defined(TARGET_PPC64) 1174a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val) 1184a7518e0SCédric Le Goater { 1194a7518e0SCédric Le Goater if (env->spr[SPR_PTCR] != val) { 120*22adb61fSBruno Larsen (billionai) PowerPCCPU *cpu = env_archcpu(env); 121*22adb61fSBruno Larsen (billionai) target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS; 122*22adb61fSBruno Larsen (billionai) target_ulong patbsize = val & PTCR_PATS; 123*22adb61fSBruno Larsen (billionai) 124*22adb61fSBruno Larsen (billionai) qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val); 125*22adb61fSBruno Larsen (billionai) 126*22adb61fSBruno Larsen (billionai) assert(!cpu->vhyp); 127*22adb61fSBruno Larsen (billionai) assert(env->mmu_model & POWERPC_MMU_3_00); 128*22adb61fSBruno Larsen (billionai) 129*22adb61fSBruno Larsen (billionai) if (val & ~ptcr_mask) { 130*22adb61fSBruno Larsen (billionai) error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR", 131*22adb61fSBruno Larsen (billionai) val & ~ptcr_mask); 132*22adb61fSBruno Larsen (billionai) val &= ptcr_mask; 133*22adb61fSBruno Larsen (billionai) } 134*22adb61fSBruno Larsen (billionai) 135*22adb61fSBruno Larsen (billionai) if (patbsize > 24) { 136*22adb61fSBruno Larsen (billionai) error_report("Invalid Partition Table size 0x" TARGET_FMT_lx 137*22adb61fSBruno Larsen (billionai) " stored in PTCR", patbsize); 138*22adb61fSBruno Larsen (billionai) return; 139*22adb61fSBruno Larsen (billionai) } 140*22adb61fSBruno Larsen (billionai) 141*22adb61fSBruno Larsen (billionai) env->spr[SPR_PTCR] = val; 142db70b311SRichard Henderson tlb_flush(env_cpu(env)); 1434a7518e0SCédric Le Goater } 1444a7518e0SCédric Le Goater } 1456b375544SJoel Stanley 1466b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value) 1476b375544SJoel Stanley { 148db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1496b375544SJoel Stanley PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1506b375544SJoel Stanley 1516b375544SJoel Stanley env->spr[SPR_PCR] = value & pcc->pcr_mask; 1526b375544SJoel Stanley } 1535ba7ba1dSCédric Le Goater 1545ba7ba1dSCédric Le Goater /* 1555ba7ba1dSCédric Le Goater * DPDES register is shared. Each bit reflects the state of the 1565ba7ba1dSCédric Le Goater * doorbell interrupt of a thread of the same core. 1575ba7ba1dSCédric Le Goater */ 1585ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env) 1595ba7ba1dSCédric Le Goater { 1605ba7ba1dSCédric Le Goater target_ulong dpdes = 0; 1615ba7ba1dSCédric Le Goater 162493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP); 163493028d8SCédric Le Goater 1645ba7ba1dSCédric Le Goater /* TODO: TCG supports only one thread */ 1655ba7ba1dSCédric Le Goater if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) { 1665ba7ba1dSCédric Le Goater dpdes = 1; 1675ba7ba1dSCédric Le Goater } 1685ba7ba1dSCédric Le Goater 1695ba7ba1dSCédric Le Goater return dpdes; 1705ba7ba1dSCédric Le Goater } 1715ba7ba1dSCédric Le Goater 1725ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val) 1735ba7ba1dSCédric Le Goater { 1745ba7ba1dSCédric Le Goater PowerPCCPU *cpu = env_archcpu(env); 1755ba7ba1dSCédric Le Goater CPUState *cs = CPU(cpu); 1765ba7ba1dSCédric Le Goater 177493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP); 178493028d8SCédric Le Goater 1795ba7ba1dSCédric Le Goater /* TODO: TCG supports only one thread */ 1805ba7ba1dSCédric Le Goater if (val & ~0x1) { 1815ba7ba1dSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value " 1825ba7ba1dSCédric Le Goater TARGET_FMT_lx"\n", val); 1835ba7ba1dSCédric Le Goater return; 1845ba7ba1dSCédric Le Goater } 1855ba7ba1dSCédric Le Goater 1865ba7ba1dSCédric Le Goater if (val & 0x1) { 1875ba7ba1dSCédric Le Goater env->pending_interrupts |= 1 << PPC_INTERRUPT_DOORBELL; 1885ba7ba1dSCédric Le Goater cpu_interrupt(cs, CPU_INTERRUPT_HARD); 1895ba7ba1dSCédric Le Goater } else { 1905ba7ba1dSCédric Le Goater env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL); 1915ba7ba1dSCédric Le Goater } 1925ba7ba1dSCédric Le Goater } 1934a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */ 1944a7518e0SCédric Le Goater 19531b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val) 19631b2b0f8SSuraj Jitindar Singh { 19731b2b0f8SSuraj Jitindar Singh env->spr[SPR_BOOKS_PID] = val; 198db70b311SRichard Henderson tlb_flush(env_cpu(env)); 19931b2b0f8SSuraj Jitindar Singh } 20031b2b0f8SSuraj Jitindar Singh 201c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val) 202c4dae9cdSBenjamin Herrenschmidt { 203c4dae9cdSBenjamin Herrenschmidt env->spr[SPR_LPIDR] = val; 204c4dae9cdSBenjamin Herrenschmidt 205c4dae9cdSBenjamin Herrenschmidt /* 206c4dae9cdSBenjamin Herrenschmidt * We need to flush the TLB on LPID changes as we only tag HV vs 207c4dae9cdSBenjamin Herrenschmidt * guest in TCG TLB. Also the quadrants means the HV will 208c4dae9cdSBenjamin Herrenschmidt * potentially access and cache entries for the current LPID as 209c4dae9cdSBenjamin Herrenschmidt * well. 210c4dae9cdSBenjamin Herrenschmidt */ 211db70b311SRichard Henderson tlb_flush(env_cpu(env)); 212c4dae9cdSBenjamin Herrenschmidt } 213c4dae9cdSBenjamin Herrenschmidt 214d523dd00SBlue Swirl void helper_store_hid0_601(CPUPPCState *env, target_ulong val) 215901c4eafSBlue Swirl { 216901c4eafSBlue Swirl target_ulong hid0; 217901c4eafSBlue Swirl 218901c4eafSBlue Swirl hid0 = env->spr[SPR_HID0]; 21918285046SRichard Henderson env->spr[SPR_HID0] = (uint32_t)val; 22018285046SRichard Henderson 221901c4eafSBlue Swirl if ((val ^ hid0) & 0x00000008) { 222901c4eafSBlue Swirl /* Change current endianness */ 22318285046SRichard Henderson hreg_compute_hflags(env); 22426c55599SRichard Henderson qemu_log("%s: set endianness to %c => %08x\n", __func__, 225901c4eafSBlue Swirl val & 0x8 ? 'l' : 'b', env->hflags); 226901c4eafSBlue Swirl } 227901c4eafSBlue Swirl } 228901c4eafSBlue Swirl 229d523dd00SBlue Swirl void helper_store_403_pbr(CPUPPCState *env, uint32_t num, target_ulong value) 230901c4eafSBlue Swirl { 231901c4eafSBlue Swirl if (likely(env->pb[num] != value)) { 232901c4eafSBlue Swirl env->pb[num] = value; 233901c4eafSBlue Swirl /* Should be optimized */ 234db70b311SRichard Henderson tlb_flush(env_cpu(env)); 235901c4eafSBlue Swirl } 236901c4eafSBlue Swirl } 237901c4eafSBlue Swirl 238d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val) 239901c4eafSBlue Swirl { 2407da31f26SRichard Henderson /* Bits 26 & 27 affect single-stepping. */ 2417da31f26SRichard Henderson hreg_compute_hflags(env); 2427da31f26SRichard Henderson /* Bits 28 & 29 affect reset or shutdown. */ 243901c4eafSBlue Swirl store_40x_dbcr0(env, val); 244901c4eafSBlue Swirl } 245901c4eafSBlue Swirl 246d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val) 247901c4eafSBlue Swirl { 248901c4eafSBlue Swirl store_40x_sler(env, val); 249901c4eafSBlue Swirl } 250901c4eafSBlue Swirl #endif 251901c4eafSBlue Swirl /*****************************************************************************/ 252901c4eafSBlue Swirl /* PowerPC 601 specific instructions (POWER bridge) */ 253901c4eafSBlue Swirl 254d523dd00SBlue Swirl target_ulong helper_clcs(CPUPPCState *env, uint32_t arg) 255901c4eafSBlue Swirl { 256901c4eafSBlue Swirl switch (arg) { 257901c4eafSBlue Swirl case 0x0CUL: 258901c4eafSBlue Swirl /* Instruction cache line size */ 259901c4eafSBlue Swirl return env->icache_line_size; 260901c4eafSBlue Swirl case 0x0DUL: 261901c4eafSBlue Swirl /* Data cache line size */ 262901c4eafSBlue Swirl return env->dcache_line_size; 263901c4eafSBlue Swirl case 0x0EUL: 264901c4eafSBlue Swirl /* Minimum cache line size */ 265901c4eafSBlue Swirl return (env->icache_line_size < env->dcache_line_size) ? 266901c4eafSBlue Swirl env->icache_line_size : env->dcache_line_size; 267901c4eafSBlue Swirl case 0x0FUL: 268901c4eafSBlue Swirl /* Maximum cache line size */ 269901c4eafSBlue Swirl return (env->icache_line_size > env->dcache_line_size) ? 270901c4eafSBlue Swirl env->icache_line_size : env->dcache_line_size; 271901c4eafSBlue Swirl default: 272901c4eafSBlue Swirl /* Undefined */ 273901c4eafSBlue Swirl return 0; 274901c4eafSBlue Swirl } 275901c4eafSBlue Swirl } 2768555f71dSBlue Swirl 2778555f71dSBlue Swirl /*****************************************************************************/ 2788555f71dSBlue Swirl /* Special registers manipulation */ 2798555f71dSBlue Swirl 280d81b4327SDavid Gibson /* 281d81b4327SDavid Gibson * This code is lifted from MacOnLinux. It is called whenever THRM1,2 282d81b4327SDavid Gibson * or 3 is read an fixes up the values in such a way that will make 283d81b4327SDavid Gibson * MacOS not hang. These registers exist on some 75x and 74xx 284d81b4327SDavid Gibson * processors. 285f0278900SBenjamin Herrenschmidt */ 286f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env) 287f0278900SBenjamin Herrenschmidt { 288f0278900SBenjamin Herrenschmidt target_ulong v, t; 289f0278900SBenjamin Herrenschmidt int i; 290f0278900SBenjamin Herrenschmidt 291f0278900SBenjamin Herrenschmidt #define THRM1_TIN (1 << 31) 292f0278900SBenjamin Herrenschmidt #define THRM1_TIV (1 << 30) 293f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x) (((x) & 0x7f) << 23) 294f0278900SBenjamin Herrenschmidt #define THRM1_TID (1 << 2) 295f0278900SBenjamin Herrenschmidt #define THRM1_TIE (1 << 1) 296f0278900SBenjamin Herrenschmidt #define THRM1_V (1 << 0) 297f0278900SBenjamin Herrenschmidt #define THRM3_E (1 << 0) 298f0278900SBenjamin Herrenschmidt 299f0278900SBenjamin Herrenschmidt if (!(env->spr[SPR_THRM3] & THRM3_E)) { 300f0278900SBenjamin Herrenschmidt return; 301f0278900SBenjamin Herrenschmidt } 302f0278900SBenjamin Herrenschmidt 303f0278900SBenjamin Herrenschmidt /* Note: Thermal interrupts are unimplemented */ 304f0278900SBenjamin Herrenschmidt for (i = SPR_THRM1; i <= SPR_THRM2; i++) { 305f0278900SBenjamin Herrenschmidt v = env->spr[i]; 306f0278900SBenjamin Herrenschmidt if (!(v & THRM1_V)) { 307f0278900SBenjamin Herrenschmidt continue; 308f0278900SBenjamin Herrenschmidt } 309f0278900SBenjamin Herrenschmidt v |= THRM1_TIV; 310f0278900SBenjamin Herrenschmidt v &= ~THRM1_TIN; 311f0278900SBenjamin Herrenschmidt t = v & THRM1_THRES(127); 312f0278900SBenjamin Herrenschmidt if ((v & THRM1_TID) && t < THRM1_THRES(24)) { 313f0278900SBenjamin Herrenschmidt v |= THRM1_TIN; 314f0278900SBenjamin Herrenschmidt } 315f0278900SBenjamin Herrenschmidt if (!(v & THRM1_TID) && t > THRM1_THRES(24)) { 316f0278900SBenjamin Herrenschmidt v |= THRM1_TIN; 317f0278900SBenjamin Herrenschmidt } 318f0278900SBenjamin Herrenschmidt env->spr[i] = v; 319f0278900SBenjamin Herrenschmidt } 320f0278900SBenjamin Herrenschmidt } 321