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" 21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h" 22901c4eafSBlue Swirl #include "cpu.h" 2363c91552SPaolo Bonzini #include "exec/exec-all.h" 242ef6175aSRichard Henderson #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" 29901c4eafSBlue Swirl 30901c4eafSBlue Swirl #include "helper_regs.h" 31901c4eafSBlue Swirl 32901c4eafSBlue Swirl /*****************************************************************************/ 33901c4eafSBlue Swirl /* SPR accesses */ 34d523dd00SBlue Swirl void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn) 35901c4eafSBlue Swirl { 36901c4eafSBlue Swirl qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn, 37901c4eafSBlue Swirl env->spr[sprn]); 38901c4eafSBlue Swirl } 39901c4eafSBlue Swirl 40d523dd00SBlue Swirl void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn) 41901c4eafSBlue Swirl { 42901c4eafSBlue Swirl qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn, 43901c4eafSBlue Swirl env->spr[sprn]); 44901c4eafSBlue Swirl } 457019cb3dSAlexey Kardashevskiy 469cdfd1b9SNicholas Piggin void helper_spr_core_write_generic(CPUPPCState *env, uint32_t sprn, 479cdfd1b9SNicholas Piggin target_ulong val) 489cdfd1b9SNicholas Piggin { 499cdfd1b9SNicholas Piggin CPUState *cs = env_cpu(env); 509cdfd1b9SNicholas Piggin CPUState *ccs; 519cdfd1b9SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 529cdfd1b9SNicholas Piggin 539cdfd1b9SNicholas Piggin if (nr_threads == 1) { 549cdfd1b9SNicholas Piggin env->spr[sprn] = val; 559cdfd1b9SNicholas Piggin return; 569cdfd1b9SNicholas Piggin } 579cdfd1b9SNicholas Piggin 589cdfd1b9SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 599cdfd1b9SNicholas Piggin CPUPPCState *cenv = &POWERPC_CPU(ccs)->env; 609cdfd1b9SNicholas Piggin cenv->spr[sprn] = val; 619cdfd1b9SNicholas Piggin } 629cdfd1b9SNicholas Piggin } 639cdfd1b9SNicholas Piggin 64c5d98a7bSNicholas Piggin void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn, 65c5d98a7bSNicholas Piggin target_ulong val) 66c5d98a7bSNicholas Piggin { 67c5d98a7bSNicholas Piggin CPUState *cs = env_cpu(env); 68c5d98a7bSNicholas Piggin CPUState *ccs; 69c5d98a7bSNicholas Piggin uint32_t run = val & 1; 70c5d98a7bSNicholas Piggin uint32_t ts, ts_mask; 71c5d98a7bSNicholas Piggin 72c5d98a7bSNicholas Piggin assert(sprn == SPR_CTRL); 73c5d98a7bSNicholas Piggin 74c5d98a7bSNicholas Piggin env->spr[sprn] &= ~1U; 75c5d98a7bSNicholas Piggin env->spr[sprn] |= run; 76c5d98a7bSNicholas Piggin 77c5d98a7bSNicholas Piggin ts_mask = ~(1U << (8 + env->spr[SPR_TIR])); 78c5d98a7bSNicholas Piggin ts = run << (8 + env->spr[SPR_TIR]); 79c5d98a7bSNicholas Piggin 80c5d98a7bSNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 81c5d98a7bSNicholas Piggin CPUPPCState *cenv = &POWERPC_CPU(ccs)->env; 82c5d98a7bSNicholas Piggin 83c5d98a7bSNicholas Piggin cenv->spr[sprn] &= ts_mask; 84c5d98a7bSNicholas Piggin cenv->spr[sprn] |= ts; 85c5d98a7bSNicholas Piggin } 86c5d98a7bSNicholas Piggin } 87c5d98a7bSNicholas Piggin 88c5d98a7bSNicholas Piggin 897019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64 90493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit, 91493028d8SCédric Le Goater const char *caller, uint32_t cause, 92493028d8SCédric Le Goater uintptr_t raddr) 93493028d8SCédric Le Goater { 94493028d8SCédric Le Goater qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n", 95493028d8SCédric Le Goater bit, caller); 96493028d8SCédric Le Goater 97493028d8SCédric Le Goater env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 98493028d8SCédric Le Goater 99493028d8SCédric Le Goater raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr); 100493028d8SCédric Le Goater } 101493028d8SCédric Le Goater 1027019cb3dSAlexey Kardashevskiy static void raise_fu_exception(CPUPPCState *env, uint32_t bit, 10357a2988bSBenjamin Herrenschmidt uint32_t sprn, uint32_t cause, 10457a2988bSBenjamin Herrenschmidt uintptr_t raddr) 1057019cb3dSAlexey Kardashevskiy { 1067019cb3dSAlexey Kardashevskiy qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit); 1077019cb3dSAlexey Kardashevskiy 1087019cb3dSAlexey Kardashevskiy env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 1097019cb3dSAlexey Kardashevskiy cause &= FSCR_IC_MASK; 1107019cb3dSAlexey Kardashevskiy env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS; 1117019cb3dSAlexey Kardashevskiy 11257a2988bSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr); 1137019cb3dSAlexey Kardashevskiy } 1147019cb3dSAlexey Kardashevskiy #endif 1157019cb3dSAlexey Kardashevskiy 116493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit, 117493028d8SCédric Le Goater const char *caller, uint32_t cause) 118493028d8SCédric Le Goater { 119493028d8SCédric Le Goater #ifdef TARGET_PPC64 1209de754d3SVíctor Colombo if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) && 121493028d8SCédric Le Goater !(env->spr[SPR_HFSCR] & (1UL << bit))) { 122493028d8SCédric Le Goater raise_hv_fu_exception(env, bit, caller, cause, GETPC()); 123493028d8SCédric Le Goater } 124493028d8SCédric Le Goater #endif 125493028d8SCédric Le Goater } 126493028d8SCédric Le Goater 1277019cb3dSAlexey Kardashevskiy void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit, 1287019cb3dSAlexey Kardashevskiy uint32_t sprn, uint32_t cause) 1297019cb3dSAlexey Kardashevskiy { 1307019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64 1317019cb3dSAlexey Kardashevskiy if (env->spr[SPR_FSCR] & (1ULL << bit)) { 1327019cb3dSAlexey Kardashevskiy /* Facility is enabled, continue */ 1337019cb3dSAlexey Kardashevskiy return; 1347019cb3dSAlexey Kardashevskiy } 13557a2988bSBenjamin Herrenschmidt raise_fu_exception(env, bit, sprn, cause, GETPC()); 1367019cb3dSAlexey Kardashevskiy #endif 1377019cb3dSAlexey Kardashevskiy } 1387019cb3dSAlexey Kardashevskiy 139cdcdda27SAlexey Kardashevskiy void helper_msr_facility_check(CPUPPCState *env, uint32_t bit, 140cdcdda27SAlexey Kardashevskiy uint32_t sprn, uint32_t cause) 141cdcdda27SAlexey Kardashevskiy { 142cdcdda27SAlexey Kardashevskiy #ifdef TARGET_PPC64 143cdcdda27SAlexey Kardashevskiy if (env->msr & (1ULL << bit)) { 144cdcdda27SAlexey Kardashevskiy /* Facility is enabled, continue */ 145cdcdda27SAlexey Kardashevskiy return; 146cdcdda27SAlexey Kardashevskiy } 14757a2988bSBenjamin Herrenschmidt raise_fu_exception(env, bit, sprn, cause, GETPC()); 148cdcdda27SAlexey Kardashevskiy #endif 149cdcdda27SAlexey Kardashevskiy } 150cdcdda27SAlexey Kardashevskiy 151901c4eafSBlue Swirl #if !defined(CONFIG_USER_ONLY) 152901c4eafSBlue Swirl 153*6bfcf1dcSGlenn Miles #ifdef TARGET_PPC64 154*6bfcf1dcSGlenn Miles static void helper_mmcr0_facility_check(CPUPPCState *env, uint32_t bit, 155*6bfcf1dcSGlenn Miles uint32_t sprn, uint32_t cause) 156*6bfcf1dcSGlenn Miles { 157*6bfcf1dcSGlenn Miles if (FIELD_EX64(env->msr, MSR, PR) && 158*6bfcf1dcSGlenn Miles !(env->spr[SPR_POWER_MMCR0] & (1ULL << bit))) { 159*6bfcf1dcSGlenn Miles raise_fu_exception(env, bit, sprn, cause, GETPC()); 160*6bfcf1dcSGlenn Miles } 161*6bfcf1dcSGlenn Miles } 162*6bfcf1dcSGlenn Miles #endif 163*6bfcf1dcSGlenn Miles 164d523dd00SBlue Swirl void helper_store_sdr1(CPUPPCState *env, target_ulong val) 165901c4eafSBlue Swirl { 1662828c4cdSMark Cave-Ayland if (env->spr[SPR_SDR1] != val) { 167901c4eafSBlue Swirl ppc_store_sdr1(env, val); 168db70b311SRichard Henderson tlb_flush(env_cpu(env)); 1692828c4cdSMark Cave-Ayland } 170901c4eafSBlue Swirl } 171901c4eafSBlue Swirl 1724a7518e0SCédric Le Goater #if defined(TARGET_PPC64) 1734a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val) 1744a7518e0SCédric Le Goater { 1754a7518e0SCédric Le Goater if (env->spr[SPR_PTCR] != val) { 17622adb61fSBruno Larsen (billionai) PowerPCCPU *cpu = env_archcpu(env); 17722adb61fSBruno Larsen (billionai) target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS; 17822adb61fSBruno Larsen (billionai) target_ulong patbsize = val & PTCR_PATS; 17922adb61fSBruno Larsen (billionai) 18022adb61fSBruno Larsen (billionai) qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val); 18122adb61fSBruno Larsen (billionai) 18222adb61fSBruno Larsen (billionai) assert(!cpu->vhyp); 18322adb61fSBruno Larsen (billionai) assert(env->mmu_model & POWERPC_MMU_3_00); 18422adb61fSBruno Larsen (billionai) 18522adb61fSBruno Larsen (billionai) if (val & ~ptcr_mask) { 18622adb61fSBruno Larsen (billionai) error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR", 18722adb61fSBruno Larsen (billionai) val & ~ptcr_mask); 18822adb61fSBruno Larsen (billionai) val &= ptcr_mask; 18922adb61fSBruno Larsen (billionai) } 19022adb61fSBruno Larsen (billionai) 19122adb61fSBruno Larsen (billionai) if (patbsize > 24) { 19222adb61fSBruno Larsen (billionai) error_report("Invalid Partition Table size 0x" TARGET_FMT_lx 19322adb61fSBruno Larsen (billionai) " stored in PTCR", patbsize); 19422adb61fSBruno Larsen (billionai) return; 19522adb61fSBruno Larsen (billionai) } 19622adb61fSBruno Larsen (billionai) 19722adb61fSBruno Larsen (billionai) env->spr[SPR_PTCR] = val; 198db70b311SRichard Henderson tlb_flush(env_cpu(env)); 1994a7518e0SCédric Le Goater } 2004a7518e0SCédric Le Goater } 2016b375544SJoel Stanley 2026b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value) 2036b375544SJoel Stanley { 204db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 2056b375544SJoel Stanley PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 2066b375544SJoel Stanley 2076b375544SJoel Stanley env->spr[SPR_PCR] = value & pcc->pcr_mask; 2086b375544SJoel Stanley } 2095ba7ba1dSCédric Le Goater 21014192307SNicholas Piggin void helper_store_ciabr(CPUPPCState *env, target_ulong value) 21114192307SNicholas Piggin { 21214192307SNicholas Piggin ppc_store_ciabr(env, value); 21314192307SNicholas Piggin } 21414192307SNicholas Piggin 215d5ee641cSNicholas Piggin void helper_store_dawr0(CPUPPCState *env, target_ulong value) 216d5ee641cSNicholas Piggin { 217d5ee641cSNicholas Piggin ppc_store_dawr0(env, value); 218d5ee641cSNicholas Piggin } 219d5ee641cSNicholas Piggin 220d5ee641cSNicholas Piggin void helper_store_dawrx0(CPUPPCState *env, target_ulong value) 221d5ee641cSNicholas Piggin { 222d5ee641cSNicholas Piggin ppc_store_dawrx0(env, value); 223d5ee641cSNicholas Piggin } 224d5ee641cSNicholas Piggin 2255ba7ba1dSCédric Le Goater /* 2265ba7ba1dSCédric Le Goater * DPDES register is shared. Each bit reflects the state of the 2275ba7ba1dSCédric Le Goater * doorbell interrupt of a thread of the same core. 2285ba7ba1dSCédric Le Goater */ 2295ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env) 2305ba7ba1dSCédric Le Goater { 231d24e80b2SNicholas Piggin CPUState *cs = env_cpu(env); 232d24e80b2SNicholas Piggin CPUState *ccs; 233d24e80b2SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 2345ba7ba1dSCédric Le Goater target_ulong dpdes = 0; 2355ba7ba1dSCédric Le Goater 236493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP); 237493028d8SCédric Le Goater 2383401ea3cSNicholas Piggin if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) { 2393401ea3cSNicholas Piggin nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */ 2403401ea3cSNicholas Piggin } 2413401ea3cSNicholas Piggin 242d24e80b2SNicholas Piggin if (nr_threads == 1) { 243f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 2445ba7ba1dSCédric Le Goater dpdes = 1; 2455ba7ba1dSCédric Le Goater } 246d24e80b2SNicholas Piggin return dpdes; 247d24e80b2SNicholas Piggin } 248d24e80b2SNicholas Piggin 249195801d7SStefan Hajnoczi bql_lock(); 250d24e80b2SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 251d24e80b2SNicholas Piggin PowerPCCPU *ccpu = POWERPC_CPU(ccs); 252d24e80b2SNicholas Piggin CPUPPCState *cenv = &ccpu->env; 253d24e80b2SNicholas Piggin uint32_t thread_id = ppc_cpu_tir(ccpu); 254d24e80b2SNicholas Piggin 255d24e80b2SNicholas Piggin if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 256d24e80b2SNicholas Piggin dpdes |= (0x1 << thread_id); 257d24e80b2SNicholas Piggin } 258d24e80b2SNicholas Piggin } 259195801d7SStefan Hajnoczi bql_unlock(); 2605ba7ba1dSCédric Le Goater 2615ba7ba1dSCédric Le Goater return dpdes; 2625ba7ba1dSCédric Le Goater } 2635ba7ba1dSCédric Le Goater 2645ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val) 2655ba7ba1dSCédric Le Goater { 2665ba7ba1dSCédric Le Goater PowerPCCPU *cpu = env_archcpu(env); 267d24e80b2SNicholas Piggin CPUState *cs = env_cpu(env); 268d24e80b2SNicholas Piggin CPUState *ccs; 269d24e80b2SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 2705ba7ba1dSCédric Le Goater 271493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP); 272493028d8SCédric Le Goater 2733401ea3cSNicholas Piggin if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) { 2743401ea3cSNicholas Piggin nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */ 2753401ea3cSNicholas Piggin } 2763401ea3cSNicholas Piggin 277d24e80b2SNicholas Piggin if (val & ~(nr_threads - 1)) { 2785ba7ba1dSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value " 2795ba7ba1dSCédric Le Goater TARGET_FMT_lx"\n", val); 280d24e80b2SNicholas Piggin val &= (nr_threads - 1); /* Ignore the invalid bits */ 281d24e80b2SNicholas Piggin } 282d24e80b2SNicholas Piggin 283d24e80b2SNicholas Piggin if (nr_threads == 1) { 284d24e80b2SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1); 2855ba7ba1dSCédric Le Goater return; 2865ba7ba1dSCédric Le Goater } 2875ba7ba1dSCédric Le Goater 288d24e80b2SNicholas Piggin /* Does iothread need to be locked for walking CPU list? */ 289195801d7SStefan Hajnoczi bql_lock(); 290d24e80b2SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 291d24e80b2SNicholas Piggin PowerPCCPU *ccpu = POWERPC_CPU(ccs); 292d24e80b2SNicholas Piggin uint32_t thread_id = ppc_cpu_tir(ccpu); 293d24e80b2SNicholas Piggin 294d24e80b2SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id)); 295d24e80b2SNicholas Piggin } 296195801d7SStefan Hajnoczi bql_unlock(); 2975ba7ba1dSCédric Le Goater } 2984a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */ 2994a7518e0SCédric Le Goater 30031b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val) 30131b2b0f8SSuraj Jitindar Singh { 302fbda88f7SNicholas Piggin env->spr[SPR_BOOKS_PID] = (uint32_t)val; 303db70b311SRichard Henderson tlb_flush(env_cpu(env)); 30431b2b0f8SSuraj Jitindar Singh } 30531b2b0f8SSuraj Jitindar Singh 306c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val) 307c4dae9cdSBenjamin Herrenschmidt { 308fbda88f7SNicholas Piggin env->spr[SPR_LPIDR] = (uint32_t)val; 309c4dae9cdSBenjamin Herrenschmidt 310c4dae9cdSBenjamin Herrenschmidt /* 311c4dae9cdSBenjamin Herrenschmidt * We need to flush the TLB on LPID changes as we only tag HV vs 312c4dae9cdSBenjamin Herrenschmidt * guest in TCG TLB. Also the quadrants means the HV will 313c4dae9cdSBenjamin Herrenschmidt * potentially access and cache entries for the current LPID as 314c4dae9cdSBenjamin Herrenschmidt * well. 315c4dae9cdSBenjamin Herrenschmidt */ 316db70b311SRichard Henderson tlb_flush(env_cpu(env)); 317c4dae9cdSBenjamin Herrenschmidt } 318c4dae9cdSBenjamin Herrenschmidt 319d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val) 320901c4eafSBlue Swirl { 3217da31f26SRichard Henderson /* Bits 26 & 27 affect single-stepping. */ 3227da31f26SRichard Henderson hreg_compute_hflags(env); 3237da31f26SRichard Henderson /* Bits 28 & 29 affect reset or shutdown. */ 324901c4eafSBlue Swirl store_40x_dbcr0(env, val); 325901c4eafSBlue Swirl } 326901c4eafSBlue Swirl 327d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val) 328901c4eafSBlue Swirl { 329901c4eafSBlue Swirl store_40x_sler(env, val); 330901c4eafSBlue Swirl } 331901c4eafSBlue Swirl #endif 3328555f71dSBlue Swirl 3338555f71dSBlue Swirl /*****************************************************************************/ 3348555f71dSBlue Swirl /* Special registers manipulation */ 3358555f71dSBlue Swirl 336d81b4327SDavid Gibson /* 337d81b4327SDavid Gibson * This code is lifted from MacOnLinux. It is called whenever THRM1,2 338d81b4327SDavid Gibson * or 3 is read an fixes up the values in such a way that will make 339d81b4327SDavid Gibson * MacOS not hang. These registers exist on some 75x and 74xx 340d81b4327SDavid Gibson * processors. 341f0278900SBenjamin Herrenschmidt */ 342f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env) 343f0278900SBenjamin Herrenschmidt { 344f0278900SBenjamin Herrenschmidt target_ulong v, t; 345f0278900SBenjamin Herrenschmidt int i; 346f0278900SBenjamin Herrenschmidt 347f0278900SBenjamin Herrenschmidt #define THRM1_TIN (1 << 31) 348f0278900SBenjamin Herrenschmidt #define THRM1_TIV (1 << 30) 349f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x) (((x) & 0x7f) << 23) 350f0278900SBenjamin Herrenschmidt #define THRM1_TID (1 << 2) 351f0278900SBenjamin Herrenschmidt #define THRM1_TIE (1 << 1) 352f0278900SBenjamin Herrenschmidt #define THRM1_V (1 << 0) 353f0278900SBenjamin Herrenschmidt #define THRM3_E (1 << 0) 354f0278900SBenjamin Herrenschmidt 355f0278900SBenjamin Herrenschmidt if (!(env->spr[SPR_THRM3] & THRM3_E)) { 356f0278900SBenjamin Herrenschmidt return; 357f0278900SBenjamin Herrenschmidt } 358f0278900SBenjamin Herrenschmidt 359f0278900SBenjamin Herrenschmidt /* Note: Thermal interrupts are unimplemented */ 360f0278900SBenjamin Herrenschmidt for (i = SPR_THRM1; i <= SPR_THRM2; i++) { 361f0278900SBenjamin Herrenschmidt v = env->spr[i]; 362f0278900SBenjamin Herrenschmidt if (!(v & THRM1_V)) { 363f0278900SBenjamin Herrenschmidt continue; 364f0278900SBenjamin Herrenschmidt } 365f0278900SBenjamin Herrenschmidt v |= THRM1_TIV; 366f0278900SBenjamin Herrenschmidt v &= ~THRM1_TIN; 367f0278900SBenjamin Herrenschmidt t = v & THRM1_THRES(127); 368f0278900SBenjamin Herrenschmidt if ((v & THRM1_TID) && t < THRM1_THRES(24)) { 369f0278900SBenjamin Herrenschmidt v |= THRM1_TIN; 370f0278900SBenjamin Herrenschmidt } 371f0278900SBenjamin Herrenschmidt if (!(v & THRM1_TID) && t > THRM1_THRES(24)) { 372f0278900SBenjamin Herrenschmidt v |= THRM1_TIN; 373f0278900SBenjamin Herrenschmidt } 374f0278900SBenjamin Herrenschmidt env->spr[i] = v; 375f0278900SBenjamin Herrenschmidt } 376f0278900SBenjamin Herrenschmidt } 377*6bfcf1dcSGlenn Miles 378*6bfcf1dcSGlenn Miles #if !defined(CONFIG_USER_ONLY) 379*6bfcf1dcSGlenn Miles #if defined(TARGET_PPC64) 380*6bfcf1dcSGlenn Miles void helper_clrbhrb(CPUPPCState *env) 381*6bfcf1dcSGlenn Miles { 382*6bfcf1dcSGlenn Miles helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB); 383*6bfcf1dcSGlenn Miles 384*6bfcf1dcSGlenn Miles helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB); 385*6bfcf1dcSGlenn Miles 386*6bfcf1dcSGlenn Miles if (env->flags & POWERPC_FLAG_BHRB) { 387*6bfcf1dcSGlenn Miles memset(env->bhrb, 0, sizeof(env->bhrb)); 388*6bfcf1dcSGlenn Miles } 389*6bfcf1dcSGlenn Miles } 390*6bfcf1dcSGlenn Miles 391*6bfcf1dcSGlenn Miles uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe) 392*6bfcf1dcSGlenn Miles { 393*6bfcf1dcSGlenn Miles unsigned int index; 394*6bfcf1dcSGlenn Miles 395*6bfcf1dcSGlenn Miles helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB); 396*6bfcf1dcSGlenn Miles 397*6bfcf1dcSGlenn Miles helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB); 398*6bfcf1dcSGlenn Miles 399*6bfcf1dcSGlenn Miles if (!(env->flags & POWERPC_FLAG_BHRB) || 400*6bfcf1dcSGlenn Miles (bhrbe >= env->bhrb_num_entries) || 401*6bfcf1dcSGlenn Miles (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) { 402*6bfcf1dcSGlenn Miles return 0; 403*6bfcf1dcSGlenn Miles } 404*6bfcf1dcSGlenn Miles 405*6bfcf1dcSGlenn Miles /* 406*6bfcf1dcSGlenn Miles * Note: bhrb_offset is the byte offset for writing the 407*6bfcf1dcSGlenn Miles * next entry (over the oldest entry), which is why we 408*6bfcf1dcSGlenn Miles * must offset bhrbe by 1 to get to the 0th entry. 409*6bfcf1dcSGlenn Miles */ 410*6bfcf1dcSGlenn Miles index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) % 411*6bfcf1dcSGlenn Miles env->bhrb_num_entries; 412*6bfcf1dcSGlenn Miles return env->bhrb[index]; 413*6bfcf1dcSGlenn Miles } 414*6bfcf1dcSGlenn Miles #endif 415*6bfcf1dcSGlenn Miles #endif 416