xref: /qemu/target/ppc/misc_helper.c (revision 6ff5da16000f908140723e164d33a0b51a6c4162)
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"
24*6ff5da16SPhilippe Mathieu-Daudé #include "exec/cputlb.h"
252ef6175aSRichard Henderson #include "exec/helper-proto.h"
266b375544SJoel Stanley #include "qemu/error-report.h"
27db725815SMarkus Armbruster #include "qemu/main-loop.h"
2822adb61fSBruno Larsen (billionai) #include "mmu-book3s-v3.h"
297b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
30901c4eafSBlue Swirl 
31901c4eafSBlue Swirl #include "helper_regs.h"
32901c4eafSBlue Swirl 
33901c4eafSBlue Swirl /*****************************************************************************/
34901c4eafSBlue Swirl /* SPR accesses */
35d523dd00SBlue Swirl void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn)
36901c4eafSBlue Swirl {
37901c4eafSBlue Swirl     qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn,
38901c4eafSBlue Swirl              env->spr[sprn]);
39901c4eafSBlue Swirl }
40901c4eafSBlue Swirl 
41d523dd00SBlue Swirl void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn)
42901c4eafSBlue Swirl {
43901c4eafSBlue Swirl     qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn,
44901c4eafSBlue Swirl              env->spr[sprn]);
45901c4eafSBlue Swirl }
467019cb3dSAlexey Kardashevskiy 
479cdfd1b9SNicholas Piggin void helper_spr_core_write_generic(CPUPPCState *env, uint32_t sprn,
489cdfd1b9SNicholas Piggin                                    target_ulong val)
499cdfd1b9SNicholas Piggin {
509cdfd1b9SNicholas Piggin     CPUState *cs = env_cpu(env);
519cdfd1b9SNicholas Piggin     CPUState *ccs;
529cdfd1b9SNicholas Piggin 
5350d8cfb9SNicholas Piggin     if (ppc_cpu_core_single_threaded(cs)) {
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 
1536bfcf1dcSGlenn Miles #ifdef TARGET_PPC64
1546bfcf1dcSGlenn Miles static void helper_mmcr0_facility_check(CPUPPCState *env, uint32_t bit,
1556bfcf1dcSGlenn Miles                                  uint32_t sprn, uint32_t cause)
1566bfcf1dcSGlenn Miles {
1576bfcf1dcSGlenn Miles     if (FIELD_EX64(env->msr, MSR, PR) &&
1586bfcf1dcSGlenn Miles         !(env->spr[SPR_POWER_MMCR0] & (1ULL << bit))) {
1596bfcf1dcSGlenn Miles         raise_fu_exception(env, bit, sprn, cause, GETPC());
1606bfcf1dcSGlenn Miles     }
1616bfcf1dcSGlenn Miles }
1626bfcf1dcSGlenn Miles #endif
1636bfcf1dcSGlenn 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) {
1764d2b0ad3SNicholas Piggin         CPUState *cs = env_cpu(env);
17722adb61fSBruno Larsen (billionai)         PowerPCCPU *cpu = env_archcpu(env);
17822adb61fSBruno Larsen (billionai)         target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS;
17922adb61fSBruno Larsen (billionai)         target_ulong patbsize = val & PTCR_PATS;
18022adb61fSBruno Larsen (billionai) 
18122adb61fSBruno Larsen (billionai)         qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val);
18222adb61fSBruno Larsen (billionai) 
18322adb61fSBruno Larsen (billionai)         assert(!cpu->vhyp);
18422adb61fSBruno Larsen (billionai)         assert(env->mmu_model & POWERPC_MMU_3_00);
18522adb61fSBruno Larsen (billionai) 
18622adb61fSBruno Larsen (billionai)         if (val & ~ptcr_mask) {
18722adb61fSBruno Larsen (billionai)             error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR",
18822adb61fSBruno Larsen (billionai)                          val & ~ptcr_mask);
18922adb61fSBruno Larsen (billionai)             val &= ptcr_mask;
19022adb61fSBruno Larsen (billionai)         }
19122adb61fSBruno Larsen (billionai) 
19222adb61fSBruno Larsen (billionai)         if (patbsize > 24) {
19322adb61fSBruno Larsen (billionai)             error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
19422adb61fSBruno Larsen (billionai)                          " stored in PTCR", patbsize);
19522adb61fSBruno Larsen (billionai)             return;
19622adb61fSBruno Larsen (billionai)         }
19722adb61fSBruno Larsen (billionai) 
19850d8cfb9SNicholas Piggin         if (ppc_cpu_lpar_single_threaded(cs)) {
19922adb61fSBruno Larsen (billionai)             env->spr[SPR_PTCR] = val;
2004d2b0ad3SNicholas Piggin             tlb_flush(cs);
2014d2b0ad3SNicholas Piggin         } else {
2024d2b0ad3SNicholas Piggin             CPUState *ccs;
2034d2b0ad3SNicholas Piggin 
2044d2b0ad3SNicholas Piggin             THREAD_SIBLING_FOREACH(cs, ccs) {
2054d2b0ad3SNicholas Piggin                 PowerPCCPU *ccpu = POWERPC_CPU(ccs);
2064d2b0ad3SNicholas Piggin                 CPUPPCState *cenv = &ccpu->env;
2074d2b0ad3SNicholas Piggin                 cenv->spr[SPR_PTCR] = val;
2084d2b0ad3SNicholas Piggin                 tlb_flush(ccs);
2094d2b0ad3SNicholas Piggin             }
2104d2b0ad3SNicholas Piggin         }
2114a7518e0SCédric Le Goater     }
2124a7518e0SCédric Le Goater }
2136b375544SJoel Stanley 
2146b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value)
2156b375544SJoel Stanley {
216db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
2176b375544SJoel Stanley     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
2186b375544SJoel Stanley 
2196b375544SJoel Stanley     env->spr[SPR_PCR] = value & pcc->pcr_mask;
2206b375544SJoel Stanley }
2215ba7ba1dSCédric Le Goater 
22214192307SNicholas Piggin void helper_store_ciabr(CPUPPCState *env, target_ulong value)
22314192307SNicholas Piggin {
22414192307SNicholas Piggin     ppc_store_ciabr(env, value);
22514192307SNicholas Piggin }
22614192307SNicholas Piggin 
227d5ee641cSNicholas Piggin void helper_store_dawr0(CPUPPCState *env, target_ulong value)
228d5ee641cSNicholas Piggin {
229d5ee641cSNicholas Piggin     ppc_store_dawr0(env, value);
230d5ee641cSNicholas Piggin }
231d5ee641cSNicholas Piggin 
232d5ee641cSNicholas Piggin void helper_store_dawrx0(CPUPPCState *env, target_ulong value)
233d5ee641cSNicholas Piggin {
234d5ee641cSNicholas Piggin     ppc_store_dawrx0(env, value);
235d5ee641cSNicholas Piggin }
236d5ee641cSNicholas Piggin 
2375ba7ba1dSCédric Le Goater /*
2385ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
2395ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
2405ba7ba1dSCédric Le Goater  */
2415ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
2425ba7ba1dSCédric Le Goater {
243d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
244d24e80b2SNicholas Piggin     CPUState *ccs;
2455ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
2465ba7ba1dSCédric Le Goater 
247493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
248493028d8SCédric Le Goater 
24950d8cfb9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
25050d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
251f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2525ba7ba1dSCédric Le Goater             dpdes = 1;
2535ba7ba1dSCédric Le Goater         }
254d24e80b2SNicholas Piggin         return dpdes;
255d24e80b2SNicholas Piggin     }
256d24e80b2SNicholas Piggin 
257195801d7SStefan Hajnoczi     bql_lock();
258d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
259d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
260d24e80b2SNicholas Piggin         CPUPPCState *cenv = &ccpu->env;
261d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
262d24e80b2SNicholas Piggin 
263d24e80b2SNicholas Piggin         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
264d24e80b2SNicholas Piggin             dpdes |= (0x1 << thread_id);
265d24e80b2SNicholas Piggin         }
266d24e80b2SNicholas Piggin     }
267195801d7SStefan Hajnoczi     bql_unlock();
2685ba7ba1dSCédric Le Goater 
2695ba7ba1dSCédric Le Goater     return dpdes;
2705ba7ba1dSCédric Le Goater }
2715ba7ba1dSCédric Le Goater 
2725ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
2735ba7ba1dSCédric Le Goater {
2745ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
275d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
276d24e80b2SNicholas Piggin     CPUState *ccs;
2775ba7ba1dSCédric Le Goater 
278493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
279493028d8SCédric Le Goater 
28050d8cfb9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
28150d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
282d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
2835ba7ba1dSCédric Le Goater         return;
2845ba7ba1dSCédric Le Goater     }
2855ba7ba1dSCédric Le Goater 
286d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
287195801d7SStefan Hajnoczi     bql_lock();
288d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
289d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
290d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
291d24e80b2SNicholas Piggin 
2920324d236SNicholas Piggin         ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
293d24e80b2SNicholas Piggin     }
294195801d7SStefan Hajnoczi     bql_unlock();
2955ba7ba1dSCédric Le Goater }
2962736432fSNicholas Piggin 
29760d30cffSNicholas Piggin /*
29860d30cffSNicholas Piggin  * qemu-user breaks with pnv headers, so they go under ifdefs for now.
29960d30cffSNicholas Piggin  * A clean up may be to move powernv specific registers and helpers into
30060d30cffSNicholas Piggin  * target/ppc/pnv_helper.c
30160d30cffSNicholas Piggin  */
30260d30cffSNicholas Piggin #include "hw/ppc/pnv_core.h"
30360d30cffSNicholas Piggin 
3042736432fSNicholas Piggin /* Indirect SCOM (SPRC/SPRD) access to SCRATCH0-7 are implemented. */
3052736432fSNicholas Piggin void helper_store_sprc(CPUPPCState *env, target_ulong val)
3062736432fSNicholas Piggin {
3072736432fSNicholas Piggin     if (val & ~0x3f8ULL) {
3082736432fSNicholas Piggin         qemu_log_mask(LOG_GUEST_ERROR, "Invalid SPRC register value "
3092736432fSNicholas Piggin                       TARGET_FMT_lx"\n", val);
3102736432fSNicholas Piggin         return;
3112736432fSNicholas Piggin     }
3122736432fSNicholas Piggin     env->spr[SPR_POWER_SPRC] = val;
3132736432fSNicholas Piggin }
3142736432fSNicholas Piggin 
3152736432fSNicholas Piggin target_ulong helper_load_sprd(CPUPPCState *env)
3162736432fSNicholas Piggin {
31760d30cffSNicholas Piggin     /*
31860d30cffSNicholas Piggin      * SPRD is a HV-only register for Power CPUs, so this will only be
31960d30cffSNicholas Piggin      * accessed by powernv machines.
32060d30cffSNicholas Piggin      */
32160d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
32260d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
3232736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
3242736432fSNicholas Piggin 
32560d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
32660d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
32760d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
32860d30cffSNicholas Piggin         return pc->scratch[(sprc >> 3) & 0x7];
32916ffcb34SNicholas Piggin 
33016ffcb34SNicholas Piggin     case 0x1e0: /* core thread state */
33116ffcb34SNicholas Piggin         if (env->excp_model == POWERPC_EXCP_POWER9) {
33216ffcb34SNicholas Piggin             /*
33316ffcb34SNicholas Piggin              * Only implement for POWER9 because skiboot uses it to check
33416ffcb34SNicholas Piggin              * big-core mode. Other bits are unimplemented so we would
33516ffcb34SNicholas Piggin              * prefer to get unimplemented message on POWER10 if it were
33616ffcb34SNicholas Piggin              * used anywhere.
33716ffcb34SNicholas Piggin              */
33816ffcb34SNicholas Piggin             if (pc->big_core) {
33916ffcb34SNicholas Piggin                 return PPC_BIT(63);
34016ffcb34SNicholas Piggin             } else {
34116ffcb34SNicholas Piggin                 return 0;
34216ffcb34SNicholas Piggin             }
34316ffcb34SNicholas Piggin         }
34416ffcb34SNicholas Piggin         /* fallthru */
34516ffcb34SNicholas Piggin 
3462736432fSNicholas Piggin     default:
3472736432fSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mfSPRD: Unimplemented SPRC:0x"
3482736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3492736432fSNicholas Piggin         break;
3502736432fSNicholas Piggin     }
3512736432fSNicholas Piggin     return 0;
3522736432fSNicholas Piggin }
3532736432fSNicholas Piggin 
3542736432fSNicholas Piggin void helper_store_sprd(CPUPPCState *env, target_ulong val)
3552736432fSNicholas Piggin {
3562736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
35760d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
35860d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
35960d30cffSNicholas Piggin     int nr;
3602736432fSNicholas Piggin 
36160d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
36260d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
36360d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
36460d30cffSNicholas Piggin         /*
36560d30cffSNicholas Piggin          * Log stores to SCRATCH, because some firmware uses these for
36660d30cffSNicholas Piggin          * debugging and logging, but they would normally be read by the BMC,
36760d30cffSNicholas Piggin          * which is not implemented in QEMU yet. This gives a way to get at the
36860d30cffSNicholas Piggin          * information. Could also dump these upon checkstop.
36960d30cffSNicholas Piggin          */
37060d30cffSNicholas Piggin         nr = (sprc >> 3) & 0x7;
37160d30cffSNicholas Piggin         qemu_log("SPRD write 0x" TARGET_FMT_lx " to SCRATCH%d\n", val, nr);
37260d30cffSNicholas Piggin         pc->scratch[nr] = val;
3732736432fSNicholas Piggin         break;
3742736432fSNicholas Piggin     default:
37560d30cffSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mtSPRD: Unimplemented SPRC:0x"
3762736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3772736432fSNicholas Piggin         break;
3782736432fSNicholas Piggin     }
3792736432fSNicholas Piggin }
3804a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
3814a7518e0SCédric Le Goater 
38231b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
38331b2b0f8SSuraj Jitindar Singh {
384fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
385db70b311SRichard Henderson     tlb_flush(env_cpu(env));
38631b2b0f8SSuraj Jitindar Singh }
38731b2b0f8SSuraj Jitindar Singh 
388c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
389c4dae9cdSBenjamin Herrenschmidt {
390fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
391c4dae9cdSBenjamin Herrenschmidt 
392c4dae9cdSBenjamin Herrenschmidt     /*
393c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
394c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
395c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
396c4dae9cdSBenjamin Herrenschmidt      * well.
397c4dae9cdSBenjamin Herrenschmidt      */
398db70b311SRichard Henderson     tlb_flush(env_cpu(env));
399c4dae9cdSBenjamin Herrenschmidt }
400c4dae9cdSBenjamin Herrenschmidt 
401d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
402901c4eafSBlue Swirl {
4037da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
4047da31f26SRichard Henderson     hreg_compute_hflags(env);
4057da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
406901c4eafSBlue Swirl     store_40x_dbcr0(env, val);
407901c4eafSBlue Swirl }
408901c4eafSBlue Swirl 
409d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
410901c4eafSBlue Swirl {
411901c4eafSBlue Swirl     store_40x_sler(env, val);
412901c4eafSBlue Swirl }
413901c4eafSBlue Swirl #endif
4148555f71dSBlue Swirl 
4158555f71dSBlue Swirl /*****************************************************************************/
4168555f71dSBlue Swirl /* Special registers manipulation */
4178555f71dSBlue Swirl 
418d81b4327SDavid Gibson /*
419d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
420d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
421d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
422d81b4327SDavid Gibson  * processors.
423f0278900SBenjamin Herrenschmidt  */
424f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env)
425f0278900SBenjamin Herrenschmidt {
426f0278900SBenjamin Herrenschmidt     target_ulong v, t;
427f0278900SBenjamin Herrenschmidt     int i;
428f0278900SBenjamin Herrenschmidt 
429f0278900SBenjamin Herrenschmidt #define THRM1_TIN       (1 << 31)
430f0278900SBenjamin Herrenschmidt #define THRM1_TIV       (1 << 30)
431f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
432f0278900SBenjamin Herrenschmidt #define THRM1_TID       (1 << 2)
433f0278900SBenjamin Herrenschmidt #define THRM1_TIE       (1 << 1)
434f0278900SBenjamin Herrenschmidt #define THRM1_V         (1 << 0)
435f0278900SBenjamin Herrenschmidt #define THRM3_E         (1 << 0)
436f0278900SBenjamin Herrenschmidt 
437f0278900SBenjamin Herrenschmidt     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
438f0278900SBenjamin Herrenschmidt         return;
439f0278900SBenjamin Herrenschmidt     }
440f0278900SBenjamin Herrenschmidt 
441f0278900SBenjamin Herrenschmidt     /* Note: Thermal interrupts are unimplemented */
442f0278900SBenjamin Herrenschmidt     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
443f0278900SBenjamin Herrenschmidt         v = env->spr[i];
444f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_V)) {
445f0278900SBenjamin Herrenschmidt             continue;
446f0278900SBenjamin Herrenschmidt         }
447f0278900SBenjamin Herrenschmidt         v |= THRM1_TIV;
448f0278900SBenjamin Herrenschmidt         v &= ~THRM1_TIN;
449f0278900SBenjamin Herrenschmidt         t = v & THRM1_THRES(127);
450f0278900SBenjamin Herrenschmidt         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
451f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
452f0278900SBenjamin Herrenschmidt         }
453f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
454f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
455f0278900SBenjamin Herrenschmidt         }
456f0278900SBenjamin Herrenschmidt         env->spr[i] = v;
457f0278900SBenjamin Herrenschmidt     }
458f0278900SBenjamin Herrenschmidt }
4596bfcf1dcSGlenn Miles 
4606bfcf1dcSGlenn Miles #if !defined(CONFIG_USER_ONLY)
4616bfcf1dcSGlenn Miles #if defined(TARGET_PPC64)
4626bfcf1dcSGlenn Miles void helper_clrbhrb(CPUPPCState *env)
4636bfcf1dcSGlenn Miles {
4646bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB);
4656bfcf1dcSGlenn Miles 
4666bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
4676bfcf1dcSGlenn Miles 
4686bfcf1dcSGlenn Miles     if (env->flags & POWERPC_FLAG_BHRB) {
4696bfcf1dcSGlenn Miles         memset(env->bhrb, 0, sizeof(env->bhrb));
4706bfcf1dcSGlenn Miles     }
4716bfcf1dcSGlenn Miles }
4726bfcf1dcSGlenn Miles 
4736bfcf1dcSGlenn Miles uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe)
4746bfcf1dcSGlenn Miles {
4756bfcf1dcSGlenn Miles     unsigned int index;
4766bfcf1dcSGlenn Miles 
4776bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB);
4786bfcf1dcSGlenn Miles 
4796bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
4806bfcf1dcSGlenn Miles 
4816bfcf1dcSGlenn Miles     if (!(env->flags & POWERPC_FLAG_BHRB) ||
4826bfcf1dcSGlenn Miles          (bhrbe >= env->bhrb_num_entries) ||
4836bfcf1dcSGlenn Miles          (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
4846bfcf1dcSGlenn Miles         return 0;
4856bfcf1dcSGlenn Miles     }
4866bfcf1dcSGlenn Miles 
4876bfcf1dcSGlenn Miles     /*
4886bfcf1dcSGlenn Miles      * Note: bhrb_offset is the byte offset for writing the
4896bfcf1dcSGlenn Miles      * next entry (over the oldest entry), which is why we
4906bfcf1dcSGlenn Miles      * must offset bhrbe by 1 to get to the 0th entry.
4916bfcf1dcSGlenn Miles      */
4926bfcf1dcSGlenn Miles     index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) %
4936bfcf1dcSGlenn Miles             env->bhrb_num_entries;
4946bfcf1dcSGlenn Miles     return env->bhrb[index];
4956bfcf1dcSGlenn Miles }
4966bfcf1dcSGlenn Miles #endif
4976bfcf1dcSGlenn Miles #endif
498