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" 21fcf5ef2aSThomas Huth #include "cpu.h" 22fcf5ef2aSThomas Huth #include "exec/exec-all.h" 23fcf5ef2aSThomas Huth #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" 27fcf5ef2aSThomas Huth 28fcf5ef2aSThomas Huth #include "helper_regs.h" 29fcf5ef2aSThomas Huth 30fcf5ef2aSThomas Huth /*****************************************************************************/ 31fcf5ef2aSThomas Huth /* SPR accesses */ 32fcf5ef2aSThomas Huth void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn) 33fcf5ef2aSThomas Huth { 34fcf5ef2aSThomas Huth qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn, 35fcf5ef2aSThomas Huth env->spr[sprn]); 36fcf5ef2aSThomas Huth } 37fcf5ef2aSThomas Huth 38fcf5ef2aSThomas Huth void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn) 39fcf5ef2aSThomas Huth { 40fcf5ef2aSThomas Huth qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn, 41fcf5ef2aSThomas Huth env->spr[sprn]); 42fcf5ef2aSThomas Huth } 43fcf5ef2aSThomas Huth 44fcf5ef2aSThomas Huth #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 57fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit, 58fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause, 59fcf5ef2aSThomas Huth uintptr_t raddr) 60fcf5ef2aSThomas Huth { 61fcf5ef2aSThomas Huth qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit); 62fcf5ef2aSThomas Huth 63fcf5ef2aSThomas Huth env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 64fcf5ef2aSThomas Huth cause &= FSCR_IC_MASK; 65fcf5ef2aSThomas Huth env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS; 66fcf5ef2aSThomas Huth 67fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr); 68fcf5ef2aSThomas Huth } 69fcf5ef2aSThomas Huth #endif 70fcf5ef2aSThomas Huth 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 82fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit, 83fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause) 84fcf5ef2aSThomas Huth { 85fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 86fcf5ef2aSThomas Huth if (env->spr[SPR_FSCR] & (1ULL << bit)) { 87fcf5ef2aSThomas Huth /* Facility is enabled, continue */ 88fcf5ef2aSThomas Huth return; 89fcf5ef2aSThomas Huth } 90fcf5ef2aSThomas Huth raise_fu_exception(env, bit, sprn, cause, GETPC()); 91fcf5ef2aSThomas Huth #endif 92fcf5ef2aSThomas Huth } 93fcf5ef2aSThomas Huth 94fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit, 95fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause) 96fcf5ef2aSThomas Huth { 97fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 98fcf5ef2aSThomas Huth if (env->msr & (1ULL << bit)) { 99fcf5ef2aSThomas Huth /* Facility is enabled, continue */ 100fcf5ef2aSThomas Huth return; 101fcf5ef2aSThomas Huth } 102fcf5ef2aSThomas Huth raise_fu_exception(env, bit, sprn, cause, GETPC()); 103fcf5ef2aSThomas Huth #endif 104fcf5ef2aSThomas Huth } 105fcf5ef2aSThomas Huth 106fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY) 107fcf5ef2aSThomas Huth 108fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val) 109fcf5ef2aSThomas Huth { 110fcf5ef2aSThomas Huth if (env->spr[SPR_SDR1] != val) { 111fcf5ef2aSThomas Huth ppc_store_sdr1(env, val); 112db70b311SRichard Henderson tlb_flush(env_cpu(env)); 113fcf5ef2aSThomas Huth } 114fcf5ef2aSThomas Huth } 115fcf5ef2aSThomas Huth 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 214fcf5ef2aSThomas Huth void helper_store_hid0_601(CPUPPCState *env, target_ulong val) 215fcf5ef2aSThomas Huth { 216fcf5ef2aSThomas Huth target_ulong hid0; 217fcf5ef2aSThomas Huth 218fcf5ef2aSThomas Huth hid0 = env->spr[SPR_HID0]; 21918285046SRichard Henderson env->spr[SPR_HID0] = (uint32_t)val; 22018285046SRichard Henderson 221fcf5ef2aSThomas Huth if ((val ^ hid0) & 0x00000008) { 222fcf5ef2aSThomas Huth /* Change current endianness */ 22318285046SRichard Henderson hreg_compute_hflags(env); 22426c55599SRichard Henderson qemu_log("%s: set endianness to %c => %08x\n", __func__, 225fcf5ef2aSThomas Huth val & 0x8 ? 'l' : 'b', env->hflags); 226fcf5ef2aSThomas Huth } 227fcf5ef2aSThomas Huth } 228fcf5ef2aSThomas Huth 229fcf5ef2aSThomas Huth void helper_store_403_pbr(CPUPPCState *env, uint32_t num, target_ulong value) 230fcf5ef2aSThomas Huth { 231fcf5ef2aSThomas Huth if (likely(env->pb[num] != value)) { 232fcf5ef2aSThomas Huth env->pb[num] = value; 233fcf5ef2aSThomas Huth /* Should be optimized */ 234db70b311SRichard Henderson tlb_flush(env_cpu(env)); 235fcf5ef2aSThomas Huth } 236fcf5ef2aSThomas Huth } 237fcf5ef2aSThomas Huth 238fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val) 239fcf5ef2aSThomas Huth { 2407da31f26SRichard Henderson /* Bits 26 & 27 affect single-stepping. */ 2417da31f26SRichard Henderson hreg_compute_hflags(env); 2427da31f26SRichard Henderson /* Bits 28 & 29 affect reset or shutdown. */ 243fcf5ef2aSThomas Huth store_40x_dbcr0(env, val); 244fcf5ef2aSThomas Huth } 245fcf5ef2aSThomas Huth 246fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val) 247fcf5ef2aSThomas Huth { 248fcf5ef2aSThomas Huth store_40x_sler(env, val); 249fcf5ef2aSThomas Huth } 250fcf5ef2aSThomas Huth #endif 251fcf5ef2aSThomas Huth /*****************************************************************************/ 252fcf5ef2aSThomas Huth /* PowerPC 601 specific instructions (POWER bridge) */ 253fcf5ef2aSThomas Huth 254fcf5ef2aSThomas Huth target_ulong helper_clcs(CPUPPCState *env, uint32_t arg) 255fcf5ef2aSThomas Huth { 256fcf5ef2aSThomas Huth switch (arg) { 257fcf5ef2aSThomas Huth case 0x0CUL: 258fcf5ef2aSThomas Huth /* Instruction cache line size */ 259fcf5ef2aSThomas Huth return env->icache_line_size; 260fcf5ef2aSThomas Huth case 0x0DUL: 261fcf5ef2aSThomas Huth /* Data cache line size */ 262fcf5ef2aSThomas Huth return env->dcache_line_size; 263fcf5ef2aSThomas Huth case 0x0EUL: 264fcf5ef2aSThomas Huth /* Minimum cache line size */ 265fcf5ef2aSThomas Huth return (env->icache_line_size < env->dcache_line_size) ? 266fcf5ef2aSThomas Huth env->icache_line_size : env->dcache_line_size; 267fcf5ef2aSThomas Huth case 0x0FUL: 268fcf5ef2aSThomas Huth /* Maximum cache line size */ 269fcf5ef2aSThomas Huth return (env->icache_line_size > env->dcache_line_size) ? 270fcf5ef2aSThomas Huth env->icache_line_size : env->dcache_line_size; 271fcf5ef2aSThomas Huth default: 272fcf5ef2aSThomas Huth /* Undefined */ 273fcf5ef2aSThomas Huth return 0; 274fcf5ef2aSThomas Huth } 275fcf5ef2aSThomas Huth } 276fcf5ef2aSThomas Huth 277fcf5ef2aSThomas Huth /*****************************************************************************/ 278fcf5ef2aSThomas Huth /* Special registers manipulation */ 279fcf5ef2aSThomas Huth 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. 285fcf5ef2aSThomas Huth */ 286fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env) 287fcf5ef2aSThomas Huth { 288fcf5ef2aSThomas Huth target_ulong v, t; 289fcf5ef2aSThomas Huth int i; 290fcf5ef2aSThomas Huth 291fcf5ef2aSThomas Huth #define THRM1_TIN (1 << 31) 292fcf5ef2aSThomas Huth #define THRM1_TIV (1 << 30) 293fcf5ef2aSThomas Huth #define THRM1_THRES(x) (((x) & 0x7f) << 23) 294fcf5ef2aSThomas Huth #define THRM1_TID (1 << 2) 295fcf5ef2aSThomas Huth #define THRM1_TIE (1 << 1) 296fcf5ef2aSThomas Huth #define THRM1_V (1 << 0) 297fcf5ef2aSThomas Huth #define THRM3_E (1 << 0) 298fcf5ef2aSThomas Huth 299fcf5ef2aSThomas Huth if (!(env->spr[SPR_THRM3] & THRM3_E)) { 300fcf5ef2aSThomas Huth return; 301fcf5ef2aSThomas Huth } 302fcf5ef2aSThomas Huth 303fcf5ef2aSThomas Huth /* Note: Thermal interrupts are unimplemented */ 304fcf5ef2aSThomas Huth for (i = SPR_THRM1; i <= SPR_THRM2; i++) { 305fcf5ef2aSThomas Huth v = env->spr[i]; 306fcf5ef2aSThomas Huth if (!(v & THRM1_V)) { 307fcf5ef2aSThomas Huth continue; 308fcf5ef2aSThomas Huth } 309fcf5ef2aSThomas Huth v |= THRM1_TIV; 310fcf5ef2aSThomas Huth v &= ~THRM1_TIN; 311fcf5ef2aSThomas Huth t = v & THRM1_THRES(127); 312fcf5ef2aSThomas Huth if ((v & THRM1_TID) && t < THRM1_THRES(24)) { 313fcf5ef2aSThomas Huth v |= THRM1_TIN; 314fcf5ef2aSThomas Huth } 315fcf5ef2aSThomas Huth if (!(v & THRM1_TID) && t > THRM1_THRES(24)) { 316fcf5ef2aSThomas Huth v |= THRM1_TIN; 317fcf5ef2aSThomas Huth } 318fcf5ef2aSThomas Huth env->spr[i] = v; 319fcf5ef2aSThomas Huth } 320fcf5ef2aSThomas Huth } 321