1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * Miscellaneous PowerPC emulation helpers for QEMU. 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright (c) 2003-2007 Jocelyn Mayer 5fcf5ef2aSThomas Huth * 6fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or 7fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public 8fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either 96bd039cdSChetan Pant * version 2.1 of the License, or (at your option) any later version. 10fcf5ef2aSThomas Huth * 11fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful, 12fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 13fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14fcf5ef2aSThomas Huth * Lesser General Public License for more details. 15fcf5ef2aSThomas Huth * 16fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public 17fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18fcf5ef2aSThomas Huth */ 19db725815SMarkus Armbruster 20fcf5ef2aSThomas Huth #include "qemu/osdep.h" 21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h" 22fcf5ef2aSThomas Huth #include "cpu.h" 23fcf5ef2aSThomas Huth #include "exec/exec-all.h" 24fcf5ef2aSThomas Huth #include "exec/helper-proto.h" 256b375544SJoel Stanley #include "qemu/error-report.h" 26db725815SMarkus Armbruster #include "qemu/main-loop.h" 2722adb61fSBruno Larsen (billionai) #include "mmu-book3s-v3.h" 28fcf5ef2aSThomas Huth 29fcf5ef2aSThomas Huth #include "helper_regs.h" 30fcf5ef2aSThomas Huth 31fcf5ef2aSThomas Huth /*****************************************************************************/ 32fcf5ef2aSThomas Huth /* SPR accesses */ 33fcf5ef2aSThomas Huth void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn) 34fcf5ef2aSThomas Huth { 35fcf5ef2aSThomas Huth qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn, 36fcf5ef2aSThomas Huth env->spr[sprn]); 37fcf5ef2aSThomas Huth } 38fcf5ef2aSThomas Huth 39fcf5ef2aSThomas Huth void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn) 40fcf5ef2aSThomas Huth { 41fcf5ef2aSThomas Huth qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn, 42fcf5ef2aSThomas Huth env->spr[sprn]); 43fcf5ef2aSThomas Huth } 44fcf5ef2aSThomas Huth 45fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 46493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit, 47493028d8SCédric Le Goater const char *caller, uint32_t cause, 48493028d8SCédric Le Goater uintptr_t raddr) 49493028d8SCédric Le Goater { 50493028d8SCédric Le Goater qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n", 51493028d8SCédric Le Goater bit, caller); 52493028d8SCédric Le Goater 53493028d8SCédric Le Goater env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 54493028d8SCédric Le Goater 55493028d8SCédric Le Goater raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr); 56493028d8SCédric Le Goater } 57493028d8SCédric Le Goater 58fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit, 59fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause, 60fcf5ef2aSThomas Huth uintptr_t raddr) 61fcf5ef2aSThomas Huth { 62fcf5ef2aSThomas Huth qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit); 63fcf5ef2aSThomas Huth 64fcf5ef2aSThomas Huth env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 65fcf5ef2aSThomas Huth cause &= FSCR_IC_MASK; 66fcf5ef2aSThomas Huth env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS; 67fcf5ef2aSThomas Huth 68fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr); 69fcf5ef2aSThomas Huth } 70fcf5ef2aSThomas Huth #endif 71fcf5ef2aSThomas Huth 72493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit, 73493028d8SCédric Le Goater const char *caller, uint32_t cause) 74493028d8SCédric Le Goater { 75493028d8SCédric Le Goater #ifdef TARGET_PPC64 769de754d3SVíctor Colombo if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) && 77493028d8SCédric Le Goater !(env->spr[SPR_HFSCR] & (1UL << bit))) { 78493028d8SCédric Le Goater raise_hv_fu_exception(env, bit, caller, cause, GETPC()); 79493028d8SCédric Le Goater } 80493028d8SCédric Le Goater #endif 81493028d8SCédric Le Goater } 82493028d8SCédric Le Goater 83fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit, 84fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause) 85fcf5ef2aSThomas Huth { 86fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 87fcf5ef2aSThomas Huth if (env->spr[SPR_FSCR] & (1ULL << bit)) { 88fcf5ef2aSThomas Huth /* Facility is enabled, continue */ 89fcf5ef2aSThomas Huth return; 90fcf5ef2aSThomas Huth } 91fcf5ef2aSThomas Huth raise_fu_exception(env, bit, sprn, cause, GETPC()); 92fcf5ef2aSThomas Huth #endif 93fcf5ef2aSThomas Huth } 94fcf5ef2aSThomas Huth 95fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit, 96fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause) 97fcf5ef2aSThomas Huth { 98fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 99fcf5ef2aSThomas Huth if (env->msr & (1ULL << bit)) { 100fcf5ef2aSThomas Huth /* Facility is enabled, continue */ 101fcf5ef2aSThomas Huth return; 102fcf5ef2aSThomas Huth } 103fcf5ef2aSThomas Huth raise_fu_exception(env, bit, sprn, cause, GETPC()); 104fcf5ef2aSThomas Huth #endif 105fcf5ef2aSThomas Huth } 106fcf5ef2aSThomas Huth 107fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY) 108fcf5ef2aSThomas Huth 109fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val) 110fcf5ef2aSThomas Huth { 111fcf5ef2aSThomas Huth if (env->spr[SPR_SDR1] != val) { 112fcf5ef2aSThomas Huth ppc_store_sdr1(env, val); 113db70b311SRichard Henderson tlb_flush(env_cpu(env)); 114fcf5ef2aSThomas Huth } 115fcf5ef2aSThomas Huth } 116fcf5ef2aSThomas Huth 1174a7518e0SCédric Le Goater #if defined(TARGET_PPC64) 1184a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val) 1194a7518e0SCédric Le Goater { 1204a7518e0SCédric Le Goater if (env->spr[SPR_PTCR] != val) { 12122adb61fSBruno Larsen (billionai) PowerPCCPU *cpu = env_archcpu(env); 12222adb61fSBruno Larsen (billionai) target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS; 12322adb61fSBruno Larsen (billionai) target_ulong patbsize = val & PTCR_PATS; 12422adb61fSBruno Larsen (billionai) 12522adb61fSBruno Larsen (billionai) qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val); 12622adb61fSBruno Larsen (billionai) 12722adb61fSBruno Larsen (billionai) assert(!cpu->vhyp); 12822adb61fSBruno Larsen (billionai) assert(env->mmu_model & POWERPC_MMU_3_00); 12922adb61fSBruno Larsen (billionai) 13022adb61fSBruno Larsen (billionai) if (val & ~ptcr_mask) { 13122adb61fSBruno Larsen (billionai) error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR", 13222adb61fSBruno Larsen (billionai) val & ~ptcr_mask); 13322adb61fSBruno Larsen (billionai) val &= ptcr_mask; 13422adb61fSBruno Larsen (billionai) } 13522adb61fSBruno Larsen (billionai) 13622adb61fSBruno Larsen (billionai) if (patbsize > 24) { 13722adb61fSBruno Larsen (billionai) error_report("Invalid Partition Table size 0x" TARGET_FMT_lx 13822adb61fSBruno Larsen (billionai) " stored in PTCR", patbsize); 13922adb61fSBruno Larsen (billionai) return; 14022adb61fSBruno Larsen (billionai) } 14122adb61fSBruno Larsen (billionai) 14222adb61fSBruno Larsen (billionai) env->spr[SPR_PTCR] = val; 143db70b311SRichard Henderson tlb_flush(env_cpu(env)); 1444a7518e0SCédric Le Goater } 1454a7518e0SCédric Le Goater } 1466b375544SJoel Stanley 1476b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value) 1486b375544SJoel Stanley { 149db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1506b375544SJoel Stanley PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1516b375544SJoel Stanley 1526b375544SJoel Stanley env->spr[SPR_PCR] = value & pcc->pcr_mask; 1536b375544SJoel Stanley } 1545ba7ba1dSCédric Le Goater 1555ba7ba1dSCédric Le Goater /* 1565ba7ba1dSCédric Le Goater * DPDES register is shared. Each bit reflects the state of the 1575ba7ba1dSCédric Le Goater * doorbell interrupt of a thread of the same core. 1585ba7ba1dSCédric Le Goater */ 1595ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env) 1605ba7ba1dSCédric Le Goater { 1615ba7ba1dSCédric Le Goater target_ulong dpdes = 0; 1625ba7ba1dSCédric Le Goater 163493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP); 164493028d8SCédric Le Goater 1655ba7ba1dSCédric Le Goater /* TODO: TCG supports only one thread */ 166*f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 1675ba7ba1dSCédric Le Goater dpdes = 1; 1685ba7ba1dSCédric Le Goater } 1695ba7ba1dSCédric Le Goater 1705ba7ba1dSCédric Le Goater return dpdes; 1715ba7ba1dSCédric Le Goater } 1725ba7ba1dSCédric Le Goater 1735ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val) 1745ba7ba1dSCédric Le Goater { 1755ba7ba1dSCédric Le Goater PowerPCCPU *cpu = env_archcpu(env); 1765ba7ba1dSCédric Le Goater CPUState *cs = CPU(cpu); 1775ba7ba1dSCédric Le Goater 178493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP); 179493028d8SCédric Le Goater 1805ba7ba1dSCédric Le Goater /* TODO: TCG supports only one thread */ 1815ba7ba1dSCédric Le Goater if (val & ~0x1) { 1825ba7ba1dSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value " 1835ba7ba1dSCédric Le Goater TARGET_FMT_lx"\n", val); 1845ba7ba1dSCédric Le Goater return; 1855ba7ba1dSCédric Le Goater } 1865ba7ba1dSCédric Le Goater 1875ba7ba1dSCédric Le Goater if (val & 0x1) { 188*f003109fSMatheus Ferst env->pending_interrupts |= PPC_INTERRUPT_DOORBELL; 1895ba7ba1dSCédric Le Goater cpu_interrupt(cs, CPU_INTERRUPT_HARD); 1905ba7ba1dSCédric Le Goater } else { 191*f003109fSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 1925ba7ba1dSCédric Le Goater } 1935ba7ba1dSCédric Le Goater } 1944a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */ 1954a7518e0SCédric Le Goater 19631b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val) 19731b2b0f8SSuraj Jitindar Singh { 19831b2b0f8SSuraj Jitindar Singh env->spr[SPR_BOOKS_PID] = val; 199db70b311SRichard Henderson tlb_flush(env_cpu(env)); 20031b2b0f8SSuraj Jitindar Singh } 20131b2b0f8SSuraj Jitindar Singh 202c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val) 203c4dae9cdSBenjamin Herrenschmidt { 204c4dae9cdSBenjamin Herrenschmidt env->spr[SPR_LPIDR] = val; 205c4dae9cdSBenjamin Herrenschmidt 206c4dae9cdSBenjamin Herrenschmidt /* 207c4dae9cdSBenjamin Herrenschmidt * We need to flush the TLB on LPID changes as we only tag HV vs 208c4dae9cdSBenjamin Herrenschmidt * guest in TCG TLB. Also the quadrants means the HV will 209c4dae9cdSBenjamin Herrenschmidt * potentially access and cache entries for the current LPID as 210c4dae9cdSBenjamin Herrenschmidt * well. 211c4dae9cdSBenjamin Herrenschmidt */ 212db70b311SRichard Henderson tlb_flush(env_cpu(env)); 213c4dae9cdSBenjamin Herrenschmidt } 214c4dae9cdSBenjamin Herrenschmidt 215fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val) 216fcf5ef2aSThomas Huth { 2177da31f26SRichard Henderson /* Bits 26 & 27 affect single-stepping. */ 2187da31f26SRichard Henderson hreg_compute_hflags(env); 2197da31f26SRichard Henderson /* Bits 28 & 29 affect reset or shutdown. */ 220fcf5ef2aSThomas Huth store_40x_dbcr0(env, val); 221fcf5ef2aSThomas Huth } 222fcf5ef2aSThomas Huth 223fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val) 224fcf5ef2aSThomas Huth { 225fcf5ef2aSThomas Huth store_40x_sler(env, val); 226fcf5ef2aSThomas Huth } 227fcf5ef2aSThomas Huth #endif 228fcf5ef2aSThomas Huth 229fcf5ef2aSThomas Huth /*****************************************************************************/ 230fcf5ef2aSThomas Huth /* Special registers manipulation */ 231fcf5ef2aSThomas Huth 232d81b4327SDavid Gibson /* 233d81b4327SDavid Gibson * This code is lifted from MacOnLinux. It is called whenever THRM1,2 234d81b4327SDavid Gibson * or 3 is read an fixes up the values in such a way that will make 235d81b4327SDavid Gibson * MacOS not hang. These registers exist on some 75x and 74xx 236d81b4327SDavid Gibson * processors. 237fcf5ef2aSThomas Huth */ 238fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env) 239fcf5ef2aSThomas Huth { 240fcf5ef2aSThomas Huth target_ulong v, t; 241fcf5ef2aSThomas Huth int i; 242fcf5ef2aSThomas Huth 243fcf5ef2aSThomas Huth #define THRM1_TIN (1 << 31) 244fcf5ef2aSThomas Huth #define THRM1_TIV (1 << 30) 245fcf5ef2aSThomas Huth #define THRM1_THRES(x) (((x) & 0x7f) << 23) 246fcf5ef2aSThomas Huth #define THRM1_TID (1 << 2) 247fcf5ef2aSThomas Huth #define THRM1_TIE (1 << 1) 248fcf5ef2aSThomas Huth #define THRM1_V (1 << 0) 249fcf5ef2aSThomas Huth #define THRM3_E (1 << 0) 250fcf5ef2aSThomas Huth 251fcf5ef2aSThomas Huth if (!(env->spr[SPR_THRM3] & THRM3_E)) { 252fcf5ef2aSThomas Huth return; 253fcf5ef2aSThomas Huth } 254fcf5ef2aSThomas Huth 255fcf5ef2aSThomas Huth /* Note: Thermal interrupts are unimplemented */ 256fcf5ef2aSThomas Huth for (i = SPR_THRM1; i <= SPR_THRM2; i++) { 257fcf5ef2aSThomas Huth v = env->spr[i]; 258fcf5ef2aSThomas Huth if (!(v & THRM1_V)) { 259fcf5ef2aSThomas Huth continue; 260fcf5ef2aSThomas Huth } 261fcf5ef2aSThomas Huth v |= THRM1_TIV; 262fcf5ef2aSThomas Huth v &= ~THRM1_TIN; 263fcf5ef2aSThomas Huth t = v & THRM1_THRES(127); 264fcf5ef2aSThomas Huth if ((v & THRM1_TID) && t < THRM1_THRES(24)) { 265fcf5ef2aSThomas Huth v |= THRM1_TIN; 266fcf5ef2aSThomas Huth } 267fcf5ef2aSThomas Huth if (!(v & THRM1_TID) && t > THRM1_THRES(24)) { 268fcf5ef2aSThomas Huth v |= THRM1_TIN; 269fcf5ef2aSThomas Huth } 270fcf5ef2aSThomas Huth env->spr[i] = v; 271fcf5ef2aSThomas Huth } 272fcf5ef2aSThomas Huth } 273