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" 287b694df6SMatheus Ferst #include "hw/ppc/ppc.h" 29fcf5ef2aSThomas Huth 30fcf5ef2aSThomas Huth #include "helper_regs.h" 31fcf5ef2aSThomas Huth 32fcf5ef2aSThomas Huth /*****************************************************************************/ 33fcf5ef2aSThomas Huth /* SPR accesses */ 34fcf5ef2aSThomas Huth void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn) 35fcf5ef2aSThomas Huth { 36fcf5ef2aSThomas Huth qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn, 37fcf5ef2aSThomas Huth env->spr[sprn]); 38fcf5ef2aSThomas Huth } 39fcf5ef2aSThomas Huth 40fcf5ef2aSThomas Huth void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn) 41fcf5ef2aSThomas Huth { 42fcf5ef2aSThomas Huth qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn, 43fcf5ef2aSThomas Huth env->spr[sprn]); 44fcf5ef2aSThomas Huth } 45fcf5ef2aSThomas Huth 46c5d98a7bSNicholas Piggin void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn, 47c5d98a7bSNicholas Piggin target_ulong val) 48c5d98a7bSNicholas Piggin { 49c5d98a7bSNicholas Piggin CPUState *cs = env_cpu(env); 50c5d98a7bSNicholas Piggin CPUState *ccs; 51c5d98a7bSNicholas Piggin uint32_t run = val & 1; 52c5d98a7bSNicholas Piggin uint32_t ts, ts_mask; 53c5d98a7bSNicholas Piggin 54c5d98a7bSNicholas Piggin assert(sprn == SPR_CTRL); 55c5d98a7bSNicholas Piggin 56c5d98a7bSNicholas Piggin env->spr[sprn] &= ~1U; 57c5d98a7bSNicholas Piggin env->spr[sprn] |= run; 58c5d98a7bSNicholas Piggin 59c5d98a7bSNicholas Piggin ts_mask = ~(1U << (8 + env->spr[SPR_TIR])); 60c5d98a7bSNicholas Piggin ts = run << (8 + env->spr[SPR_TIR]); 61c5d98a7bSNicholas Piggin 62c5d98a7bSNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 63c5d98a7bSNicholas Piggin CPUPPCState *cenv = &POWERPC_CPU(ccs)->env; 64c5d98a7bSNicholas Piggin 65c5d98a7bSNicholas Piggin cenv->spr[sprn] &= ts_mask; 66c5d98a7bSNicholas Piggin cenv->spr[sprn] |= ts; 67c5d98a7bSNicholas Piggin } 68c5d98a7bSNicholas Piggin } 69c5d98a7bSNicholas Piggin 70c5d98a7bSNicholas Piggin 71fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 72493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit, 73493028d8SCédric Le Goater const char *caller, uint32_t cause, 74493028d8SCédric Le Goater uintptr_t raddr) 75493028d8SCédric Le Goater { 76493028d8SCédric Le Goater qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n", 77493028d8SCédric Le Goater bit, caller); 78493028d8SCédric Le Goater 79493028d8SCédric Le Goater env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 80493028d8SCédric Le Goater 81493028d8SCédric Le Goater raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr); 82493028d8SCédric Le Goater } 83493028d8SCédric Le Goater 84fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit, 85fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause, 86fcf5ef2aSThomas Huth uintptr_t raddr) 87fcf5ef2aSThomas Huth { 88fcf5ef2aSThomas Huth qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit); 89fcf5ef2aSThomas Huth 90fcf5ef2aSThomas Huth env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 91fcf5ef2aSThomas Huth cause &= FSCR_IC_MASK; 92fcf5ef2aSThomas Huth env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS; 93fcf5ef2aSThomas Huth 94fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr); 95fcf5ef2aSThomas Huth } 96fcf5ef2aSThomas Huth #endif 97fcf5ef2aSThomas Huth 98493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit, 99493028d8SCédric Le Goater const char *caller, uint32_t cause) 100493028d8SCédric Le Goater { 101493028d8SCédric Le Goater #ifdef TARGET_PPC64 1029de754d3SVíctor Colombo if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) && 103493028d8SCédric Le Goater !(env->spr[SPR_HFSCR] & (1UL << bit))) { 104493028d8SCédric Le Goater raise_hv_fu_exception(env, bit, caller, cause, GETPC()); 105493028d8SCédric Le Goater } 106493028d8SCédric Le Goater #endif 107493028d8SCédric Le Goater } 108493028d8SCédric Le Goater 109fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit, 110fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause) 111fcf5ef2aSThomas Huth { 112fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 113fcf5ef2aSThomas Huth if (env->spr[SPR_FSCR] & (1ULL << bit)) { 114fcf5ef2aSThomas Huth /* Facility is enabled, continue */ 115fcf5ef2aSThomas Huth return; 116fcf5ef2aSThomas Huth } 117fcf5ef2aSThomas Huth raise_fu_exception(env, bit, sprn, cause, GETPC()); 118fcf5ef2aSThomas Huth #endif 119fcf5ef2aSThomas Huth } 120fcf5ef2aSThomas Huth 121fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit, 122fcf5ef2aSThomas Huth uint32_t sprn, uint32_t cause) 123fcf5ef2aSThomas Huth { 124fcf5ef2aSThomas Huth #ifdef TARGET_PPC64 125fcf5ef2aSThomas Huth if (env->msr & (1ULL << bit)) { 126fcf5ef2aSThomas Huth /* Facility is enabled, continue */ 127fcf5ef2aSThomas Huth return; 128fcf5ef2aSThomas Huth } 129fcf5ef2aSThomas Huth raise_fu_exception(env, bit, sprn, cause, GETPC()); 130fcf5ef2aSThomas Huth #endif 131fcf5ef2aSThomas Huth } 132fcf5ef2aSThomas Huth 133fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY) 134fcf5ef2aSThomas Huth 135fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val) 136fcf5ef2aSThomas Huth { 137fcf5ef2aSThomas Huth if (env->spr[SPR_SDR1] != val) { 138fcf5ef2aSThomas Huth ppc_store_sdr1(env, val); 139db70b311SRichard Henderson tlb_flush(env_cpu(env)); 140fcf5ef2aSThomas Huth } 141fcf5ef2aSThomas Huth } 142fcf5ef2aSThomas Huth 1434a7518e0SCédric Le Goater #if defined(TARGET_PPC64) 1444a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val) 1454a7518e0SCédric Le Goater { 1464a7518e0SCédric Le Goater if (env->spr[SPR_PTCR] != val) { 14722adb61fSBruno Larsen (billionai) PowerPCCPU *cpu = env_archcpu(env); 14822adb61fSBruno Larsen (billionai) target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS; 14922adb61fSBruno Larsen (billionai) target_ulong patbsize = val & PTCR_PATS; 15022adb61fSBruno Larsen (billionai) 15122adb61fSBruno Larsen (billionai) qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val); 15222adb61fSBruno Larsen (billionai) 15322adb61fSBruno Larsen (billionai) assert(!cpu->vhyp); 15422adb61fSBruno Larsen (billionai) assert(env->mmu_model & POWERPC_MMU_3_00); 15522adb61fSBruno Larsen (billionai) 15622adb61fSBruno Larsen (billionai) if (val & ~ptcr_mask) { 15722adb61fSBruno Larsen (billionai) error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR", 15822adb61fSBruno Larsen (billionai) val & ~ptcr_mask); 15922adb61fSBruno Larsen (billionai) val &= ptcr_mask; 16022adb61fSBruno Larsen (billionai) } 16122adb61fSBruno Larsen (billionai) 16222adb61fSBruno Larsen (billionai) if (patbsize > 24) { 16322adb61fSBruno Larsen (billionai) error_report("Invalid Partition Table size 0x" TARGET_FMT_lx 16422adb61fSBruno Larsen (billionai) " stored in PTCR", patbsize); 16522adb61fSBruno Larsen (billionai) return; 16622adb61fSBruno Larsen (billionai) } 16722adb61fSBruno Larsen (billionai) 16822adb61fSBruno Larsen (billionai) env->spr[SPR_PTCR] = val; 169db70b311SRichard Henderson tlb_flush(env_cpu(env)); 1704a7518e0SCédric Le Goater } 1714a7518e0SCédric Le Goater } 1726b375544SJoel Stanley 1736b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value) 1746b375544SJoel Stanley { 175db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1766b375544SJoel Stanley PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1776b375544SJoel Stanley 1786b375544SJoel Stanley env->spr[SPR_PCR] = value & pcc->pcr_mask; 1796b375544SJoel Stanley } 1805ba7ba1dSCédric Le Goater 1815ba7ba1dSCédric Le Goater /* 1825ba7ba1dSCédric Le Goater * DPDES register is shared. Each bit reflects the state of the 1835ba7ba1dSCédric Le Goater * doorbell interrupt of a thread of the same core. 1845ba7ba1dSCédric Le Goater */ 1855ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env) 1865ba7ba1dSCédric Le Goater { 187d24e80b2SNicholas Piggin CPUState *cs = env_cpu(env); 188d24e80b2SNicholas Piggin CPUState *ccs; 189d24e80b2SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 1905ba7ba1dSCédric Le Goater target_ulong dpdes = 0; 1915ba7ba1dSCédric Le Goater 192493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP); 193493028d8SCédric Le Goater 194*3401ea3cSNicholas Piggin if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) { 195*3401ea3cSNicholas Piggin nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */ 196*3401ea3cSNicholas Piggin } 197*3401ea3cSNicholas Piggin 198d24e80b2SNicholas Piggin if (nr_threads == 1) { 199f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 2005ba7ba1dSCédric Le Goater dpdes = 1; 2015ba7ba1dSCédric Le Goater } 202d24e80b2SNicholas Piggin return dpdes; 203d24e80b2SNicholas Piggin } 204d24e80b2SNicholas Piggin 205d24e80b2SNicholas Piggin qemu_mutex_lock_iothread(); 206d24e80b2SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 207d24e80b2SNicholas Piggin PowerPCCPU *ccpu = POWERPC_CPU(ccs); 208d24e80b2SNicholas Piggin CPUPPCState *cenv = &ccpu->env; 209d24e80b2SNicholas Piggin uint32_t thread_id = ppc_cpu_tir(ccpu); 210d24e80b2SNicholas Piggin 211d24e80b2SNicholas Piggin if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 212d24e80b2SNicholas Piggin dpdes |= (0x1 << thread_id); 213d24e80b2SNicholas Piggin } 214d24e80b2SNicholas Piggin } 215d24e80b2SNicholas Piggin qemu_mutex_unlock_iothread(); 2165ba7ba1dSCédric Le Goater 2175ba7ba1dSCédric Le Goater return dpdes; 2185ba7ba1dSCédric Le Goater } 2195ba7ba1dSCédric Le Goater 2205ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val) 2215ba7ba1dSCédric Le Goater { 2225ba7ba1dSCédric Le Goater PowerPCCPU *cpu = env_archcpu(env); 223d24e80b2SNicholas Piggin CPUState *cs = env_cpu(env); 224d24e80b2SNicholas Piggin CPUState *ccs; 225d24e80b2SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 2265ba7ba1dSCédric Le Goater 227493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP); 228493028d8SCédric Le Goater 229*3401ea3cSNicholas Piggin if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) { 230*3401ea3cSNicholas Piggin nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */ 231*3401ea3cSNicholas Piggin } 232*3401ea3cSNicholas Piggin 233d24e80b2SNicholas Piggin if (val & ~(nr_threads - 1)) { 2345ba7ba1dSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value " 2355ba7ba1dSCédric Le Goater TARGET_FMT_lx"\n", val); 236d24e80b2SNicholas Piggin val &= (nr_threads - 1); /* Ignore the invalid bits */ 237d24e80b2SNicholas Piggin } 238d24e80b2SNicholas Piggin 239d24e80b2SNicholas Piggin if (nr_threads == 1) { 240d24e80b2SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1); 2415ba7ba1dSCédric Le Goater return; 2425ba7ba1dSCédric Le Goater } 2435ba7ba1dSCédric Le Goater 244d24e80b2SNicholas Piggin /* Does iothread need to be locked for walking CPU list? */ 245d24e80b2SNicholas Piggin qemu_mutex_lock_iothread(); 246d24e80b2SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 247d24e80b2SNicholas Piggin PowerPCCPU *ccpu = POWERPC_CPU(ccs); 248d24e80b2SNicholas Piggin uint32_t thread_id = ppc_cpu_tir(ccpu); 249d24e80b2SNicholas Piggin 250d24e80b2SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id)); 251d24e80b2SNicholas Piggin } 252d24e80b2SNicholas Piggin qemu_mutex_unlock_iothread(); 2535ba7ba1dSCédric Le Goater } 2544a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */ 2554a7518e0SCédric Le Goater 25631b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val) 25731b2b0f8SSuraj Jitindar Singh { 258fbda88f7SNicholas Piggin env->spr[SPR_BOOKS_PID] = (uint32_t)val; 259db70b311SRichard Henderson tlb_flush(env_cpu(env)); 26031b2b0f8SSuraj Jitindar Singh } 26131b2b0f8SSuraj Jitindar Singh 262c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val) 263c4dae9cdSBenjamin Herrenschmidt { 264fbda88f7SNicholas Piggin env->spr[SPR_LPIDR] = (uint32_t)val; 265c4dae9cdSBenjamin Herrenschmidt 266c4dae9cdSBenjamin Herrenschmidt /* 267c4dae9cdSBenjamin Herrenschmidt * We need to flush the TLB on LPID changes as we only tag HV vs 268c4dae9cdSBenjamin Herrenschmidt * guest in TCG TLB. Also the quadrants means the HV will 269c4dae9cdSBenjamin Herrenschmidt * potentially access and cache entries for the current LPID as 270c4dae9cdSBenjamin Herrenschmidt * well. 271c4dae9cdSBenjamin Herrenschmidt */ 272db70b311SRichard Henderson tlb_flush(env_cpu(env)); 273c4dae9cdSBenjamin Herrenschmidt } 274c4dae9cdSBenjamin Herrenschmidt 275fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val) 276fcf5ef2aSThomas Huth { 2777da31f26SRichard Henderson /* Bits 26 & 27 affect single-stepping. */ 2787da31f26SRichard Henderson hreg_compute_hflags(env); 2797da31f26SRichard Henderson /* Bits 28 & 29 affect reset or shutdown. */ 280fcf5ef2aSThomas Huth store_40x_dbcr0(env, val); 281fcf5ef2aSThomas Huth } 282fcf5ef2aSThomas Huth 283fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val) 284fcf5ef2aSThomas Huth { 285fcf5ef2aSThomas Huth store_40x_sler(env, val); 286fcf5ef2aSThomas Huth } 287fcf5ef2aSThomas Huth #endif 288fcf5ef2aSThomas Huth 289fcf5ef2aSThomas Huth /*****************************************************************************/ 290fcf5ef2aSThomas Huth /* Special registers manipulation */ 291fcf5ef2aSThomas Huth 292d81b4327SDavid Gibson /* 293d81b4327SDavid Gibson * This code is lifted from MacOnLinux. It is called whenever THRM1,2 294d81b4327SDavid Gibson * or 3 is read an fixes up the values in such a way that will make 295d81b4327SDavid Gibson * MacOS not hang. These registers exist on some 75x and 74xx 296d81b4327SDavid Gibson * processors. 297fcf5ef2aSThomas Huth */ 298fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env) 299fcf5ef2aSThomas Huth { 300fcf5ef2aSThomas Huth target_ulong v, t; 301fcf5ef2aSThomas Huth int i; 302fcf5ef2aSThomas Huth 303fcf5ef2aSThomas Huth #define THRM1_TIN (1 << 31) 304fcf5ef2aSThomas Huth #define THRM1_TIV (1 << 30) 305fcf5ef2aSThomas Huth #define THRM1_THRES(x) (((x) & 0x7f) << 23) 306fcf5ef2aSThomas Huth #define THRM1_TID (1 << 2) 307fcf5ef2aSThomas Huth #define THRM1_TIE (1 << 1) 308fcf5ef2aSThomas Huth #define THRM1_V (1 << 0) 309fcf5ef2aSThomas Huth #define THRM3_E (1 << 0) 310fcf5ef2aSThomas Huth 311fcf5ef2aSThomas Huth if (!(env->spr[SPR_THRM3] & THRM3_E)) { 312fcf5ef2aSThomas Huth return; 313fcf5ef2aSThomas Huth } 314fcf5ef2aSThomas Huth 315fcf5ef2aSThomas Huth /* Note: Thermal interrupts are unimplemented */ 316fcf5ef2aSThomas Huth for (i = SPR_THRM1; i <= SPR_THRM2; i++) { 317fcf5ef2aSThomas Huth v = env->spr[i]; 318fcf5ef2aSThomas Huth if (!(v & THRM1_V)) { 319fcf5ef2aSThomas Huth continue; 320fcf5ef2aSThomas Huth } 321fcf5ef2aSThomas Huth v |= THRM1_TIV; 322fcf5ef2aSThomas Huth v &= ~THRM1_TIN; 323fcf5ef2aSThomas Huth t = v & THRM1_THRES(127); 324fcf5ef2aSThomas Huth if ((v & THRM1_TID) && t < THRM1_THRES(24)) { 325fcf5ef2aSThomas Huth v |= THRM1_TIN; 326fcf5ef2aSThomas Huth } 327fcf5ef2aSThomas Huth if (!(v & THRM1_TID) && t > THRM1_THRES(24)) { 328fcf5ef2aSThomas Huth v |= THRM1_TIN; 329fcf5ef2aSThomas Huth } 330fcf5ef2aSThomas Huth env->spr[i] = v; 331fcf5ef2aSThomas Huth } 332fcf5ef2aSThomas Huth } 333