xref: /qemu/target/ppc/misc_helper.c (revision 9808ce6d5cb75a4f9db76a3d9b508560efdf5ac2)
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"
246ff5da16SPhilippe 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 
2377ea6e125SShivaprasad G Bhat void helper_store_dawr1(CPUPPCState *env, target_ulong value)
2387ea6e125SShivaprasad G Bhat {
2397ea6e125SShivaprasad G Bhat     ppc_store_dawr1(env, value);
2407ea6e125SShivaprasad G Bhat }
2417ea6e125SShivaprasad G Bhat 
2427ea6e125SShivaprasad G Bhat void helper_store_dawrx1(CPUPPCState *env, target_ulong value)
2437ea6e125SShivaprasad G Bhat {
2447ea6e125SShivaprasad G Bhat     ppc_store_dawrx1(env, value);
2457ea6e125SShivaprasad G Bhat }
2467ea6e125SShivaprasad G Bhat 
2475ba7ba1dSCédric Le Goater /*
2485ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
2495ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
2505ba7ba1dSCédric Le Goater  */
2515ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
2525ba7ba1dSCédric Le Goater {
253d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
254d24e80b2SNicholas Piggin     CPUState *ccs;
2555ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
2565ba7ba1dSCédric Le Goater 
257493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
258493028d8SCédric Le Goater 
25950d8cfb9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
26050d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
261f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2625ba7ba1dSCédric Le Goater             dpdes = 1;
2635ba7ba1dSCédric Le Goater         }
264d24e80b2SNicholas Piggin         return dpdes;
265d24e80b2SNicholas Piggin     }
266d24e80b2SNicholas Piggin 
267195801d7SStefan Hajnoczi     bql_lock();
268d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
269d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
270d24e80b2SNicholas Piggin         CPUPPCState *cenv = &ccpu->env;
271d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
272d24e80b2SNicholas Piggin 
273d24e80b2SNicholas Piggin         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
274d24e80b2SNicholas Piggin             dpdes |= (0x1 << thread_id);
275d24e80b2SNicholas Piggin         }
276d24e80b2SNicholas Piggin     }
277195801d7SStefan Hajnoczi     bql_unlock();
2785ba7ba1dSCédric Le Goater 
2795ba7ba1dSCédric Le Goater     return dpdes;
2805ba7ba1dSCédric Le Goater }
2815ba7ba1dSCédric Le Goater 
2825ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
2835ba7ba1dSCédric Le Goater {
2845ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
285d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
286d24e80b2SNicholas Piggin     CPUState *ccs;
2875ba7ba1dSCédric Le Goater 
288493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
289493028d8SCédric Le Goater 
29050d8cfb9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
29150d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
292d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
2935ba7ba1dSCédric Le Goater         return;
2945ba7ba1dSCédric Le Goater     }
2955ba7ba1dSCédric Le Goater 
296d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
297195801d7SStefan Hajnoczi     bql_lock();
298d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
299d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
300d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
301d24e80b2SNicholas Piggin 
3020324d236SNicholas Piggin         ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
303d24e80b2SNicholas Piggin     }
304195801d7SStefan Hajnoczi     bql_unlock();
3055ba7ba1dSCédric Le Goater }
3062736432fSNicholas Piggin 
30760d30cffSNicholas Piggin /*
30860d30cffSNicholas Piggin  * qemu-user breaks with pnv headers, so they go under ifdefs for now.
30960d30cffSNicholas Piggin  * A clean up may be to move powernv specific registers and helpers into
31060d30cffSNicholas Piggin  * target/ppc/pnv_helper.c
31160d30cffSNicholas Piggin  */
31260d30cffSNicholas Piggin #include "hw/ppc/pnv_core.h"
31360d30cffSNicholas Piggin 
3142736432fSNicholas Piggin /* Indirect SCOM (SPRC/SPRD) access to SCRATCH0-7 are implemented. */
3152736432fSNicholas Piggin void helper_store_sprc(CPUPPCState *env, target_ulong val)
3162736432fSNicholas Piggin {
3172736432fSNicholas Piggin     if (val & ~0x3f8ULL) {
3182736432fSNicholas Piggin         qemu_log_mask(LOG_GUEST_ERROR, "Invalid SPRC register value "
3192736432fSNicholas Piggin                       TARGET_FMT_lx"\n", val);
3202736432fSNicholas Piggin         return;
3212736432fSNicholas Piggin     }
3222736432fSNicholas Piggin     env->spr[SPR_POWER_SPRC] = val;
3232736432fSNicholas Piggin }
3242736432fSNicholas Piggin 
3252736432fSNicholas Piggin target_ulong helper_load_sprd(CPUPPCState *env)
3262736432fSNicholas Piggin {
32760d30cffSNicholas Piggin     /*
32860d30cffSNicholas Piggin      * SPRD is a HV-only register for Power CPUs, so this will only be
32960d30cffSNicholas Piggin      * accessed by powernv machines.
33060d30cffSNicholas Piggin      */
33160d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
33260d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
3332736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
3342736432fSNicholas Piggin 
335*9808ce6dSNicholas Piggin     if (pc->big_core) {
336*9808ce6dSNicholas Piggin         pc = pnv_chip_find_core(pc->chip, CPU_CORE(pc)->core_id & ~0x1);
337*9808ce6dSNicholas Piggin     }
338*9808ce6dSNicholas Piggin 
33960d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
34060d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
34160d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
34260d30cffSNicholas Piggin         return pc->scratch[(sprc >> 3) & 0x7];
34316ffcb34SNicholas Piggin 
34416ffcb34SNicholas Piggin     case 0x1e0: /* core thread state */
34516ffcb34SNicholas Piggin         if (env->excp_model == POWERPC_EXCP_POWER9) {
34616ffcb34SNicholas Piggin             /*
34716ffcb34SNicholas Piggin              * Only implement for POWER9 because skiboot uses it to check
34816ffcb34SNicholas Piggin              * big-core mode. Other bits are unimplemented so we would
34916ffcb34SNicholas Piggin              * prefer to get unimplemented message on POWER10 if it were
35016ffcb34SNicholas Piggin              * used anywhere.
35116ffcb34SNicholas Piggin              */
35216ffcb34SNicholas Piggin             if (pc->big_core) {
35316ffcb34SNicholas Piggin                 return PPC_BIT(63);
35416ffcb34SNicholas Piggin             } else {
35516ffcb34SNicholas Piggin                 return 0;
35616ffcb34SNicholas Piggin             }
35716ffcb34SNicholas Piggin         }
35816ffcb34SNicholas Piggin         /* fallthru */
35916ffcb34SNicholas Piggin 
3602736432fSNicholas Piggin     default:
3612736432fSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mfSPRD: Unimplemented SPRC:0x"
3622736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3632736432fSNicholas Piggin         break;
3642736432fSNicholas Piggin     }
3652736432fSNicholas Piggin     return 0;
3662736432fSNicholas Piggin }
3672736432fSNicholas Piggin 
3682736432fSNicholas Piggin void helper_store_sprd(CPUPPCState *env, target_ulong val)
3692736432fSNicholas Piggin {
3702736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
37160d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
37260d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
37360d30cffSNicholas Piggin     int nr;
3742736432fSNicholas Piggin 
375*9808ce6dSNicholas Piggin     if (pc->big_core) {
376*9808ce6dSNicholas Piggin         pc = pnv_chip_find_core(pc->chip, CPU_CORE(pc)->core_id & ~0x1);
377*9808ce6dSNicholas Piggin     }
378*9808ce6dSNicholas Piggin 
37960d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
38060d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
38160d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
38260d30cffSNicholas Piggin         /*
38360d30cffSNicholas Piggin          * Log stores to SCRATCH, because some firmware uses these for
38460d30cffSNicholas Piggin          * debugging and logging, but they would normally be read by the BMC,
38560d30cffSNicholas Piggin          * which is not implemented in QEMU yet. This gives a way to get at the
38660d30cffSNicholas Piggin          * information. Could also dump these upon checkstop.
38760d30cffSNicholas Piggin          */
38860d30cffSNicholas Piggin         nr = (sprc >> 3) & 0x7;
38960d30cffSNicholas Piggin         pc->scratch[nr] = val;
3902736432fSNicholas Piggin         break;
3912736432fSNicholas Piggin     default:
39260d30cffSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mtSPRD: Unimplemented SPRC:0x"
3932736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3942736432fSNicholas Piggin         break;
3952736432fSNicholas Piggin     }
3962736432fSNicholas Piggin }
397d3ce7dc9SNicholas Piggin 
398d3ce7dc9SNicholas Piggin target_ulong helper_load_pmsr(CPUPPCState *env)
399d3ce7dc9SNicholas Piggin {
400d3ce7dc9SNicholas Piggin     target_ulong lowerps = extract64(env->spr[SPR_PMCR], PPC_BIT_NR(15), 8);
401d3ce7dc9SNicholas Piggin     target_ulong val = 0;
402d3ce7dc9SNicholas Piggin 
403d3ce7dc9SNicholas Piggin     val |= PPC_BIT(63); /* verion 0x1 (POWER9/10) */
404d3ce7dc9SNicholas Piggin     /* Pmin = 0 */
405d3ce7dc9SNicholas Piggin     /* XXX: POWER9 should be 3 */
406d3ce7dc9SNicholas Piggin     val |= 4ULL << PPC_BIT_NR(31); /* Pmax */
407d3ce7dc9SNicholas Piggin     val |= lowerps << PPC_BIT_NR(15); /* Local actual Pstate */
408d3ce7dc9SNicholas Piggin     val |= lowerps << PPC_BIT_NR(7); /* Global actual Pstate */
409d3ce7dc9SNicholas Piggin 
410d3ce7dc9SNicholas Piggin     return val;
411d3ce7dc9SNicholas Piggin }
412d3ce7dc9SNicholas Piggin 
413d3ce7dc9SNicholas Piggin static void ppc_set_pmcr(PowerPCCPU *cpu, target_ulong val)
414d3ce7dc9SNicholas Piggin {
415d3ce7dc9SNicholas Piggin     cpu->env.spr[SPR_PMCR] = val;
416d3ce7dc9SNicholas Piggin }
417d3ce7dc9SNicholas Piggin 
418d3ce7dc9SNicholas Piggin void helper_store_pmcr(CPUPPCState *env, target_ulong val)
419d3ce7dc9SNicholas Piggin {
420d3ce7dc9SNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
421d3ce7dc9SNicholas Piggin     CPUState *cs = env_cpu(env);
422d3ce7dc9SNicholas Piggin     CPUState *ccs;
423d3ce7dc9SNicholas Piggin 
424d3ce7dc9SNicholas Piggin     /* Leave version field unchanged (0x1) */
425d3ce7dc9SNicholas Piggin     val &= ~PPC_BITMASK(60, 63);
426d3ce7dc9SNicholas Piggin     val |= PPC_BIT(63);
427d3ce7dc9SNicholas Piggin 
428d3ce7dc9SNicholas Piggin     val &= ~PPC_BITMASK(0, 7); /* UpperPS ignored */
429d3ce7dc9SNicholas Piggin     if (val & PPC_BITMASK(16, 59)) {
430d3ce7dc9SNicholas Piggin         qemu_log_mask(LOG_GUEST_ERROR, "Non-zero PMCR reserved bits "
431d3ce7dc9SNicholas Piggin                       TARGET_FMT_lx"\n", val);
432d3ce7dc9SNicholas Piggin         val &= ~PPC_BITMASK(16, 59);
433d3ce7dc9SNicholas Piggin     }
434d3ce7dc9SNicholas Piggin 
435d3ce7dc9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
436d3ce7dc9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
437d3ce7dc9SNicholas Piggin         ppc_set_pmcr(cpu, val);
438d3ce7dc9SNicholas Piggin         return;
439d3ce7dc9SNicholas Piggin     }
440d3ce7dc9SNicholas Piggin 
441d3ce7dc9SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
442d3ce7dc9SNicholas Piggin     bql_lock();
443d3ce7dc9SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
444d3ce7dc9SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
445d3ce7dc9SNicholas Piggin         ppc_set_pmcr(ccpu, val);
446d3ce7dc9SNicholas Piggin     }
447d3ce7dc9SNicholas Piggin     bql_unlock();
448d3ce7dc9SNicholas Piggin }
449d3ce7dc9SNicholas Piggin 
4504a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
4514a7518e0SCédric Le Goater 
45231b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
45331b2b0f8SSuraj Jitindar Singh {
454fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
455db70b311SRichard Henderson     tlb_flush(env_cpu(env));
45631b2b0f8SSuraj Jitindar Singh }
45731b2b0f8SSuraj Jitindar Singh 
458c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
459c4dae9cdSBenjamin Herrenschmidt {
460fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
461c4dae9cdSBenjamin Herrenschmidt 
462c4dae9cdSBenjamin Herrenschmidt     /*
463c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
464c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
465c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
466c4dae9cdSBenjamin Herrenschmidt      * well.
467c4dae9cdSBenjamin Herrenschmidt      */
468db70b311SRichard Henderson     tlb_flush(env_cpu(env));
469c4dae9cdSBenjamin Herrenschmidt }
470c4dae9cdSBenjamin Herrenschmidt 
471d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
472901c4eafSBlue Swirl {
4737da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
4747da31f26SRichard Henderson     hreg_compute_hflags(env);
4757da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
476901c4eafSBlue Swirl     store_40x_dbcr0(env, val);
477901c4eafSBlue Swirl }
478901c4eafSBlue Swirl 
479d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
480901c4eafSBlue Swirl {
481901c4eafSBlue Swirl     store_40x_sler(env, val);
482901c4eafSBlue Swirl }
483901c4eafSBlue Swirl #endif
4848555f71dSBlue Swirl 
4858555f71dSBlue Swirl /*****************************************************************************/
4868555f71dSBlue Swirl /* Special registers manipulation */
4878555f71dSBlue Swirl 
488d81b4327SDavid Gibson /*
489d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
490d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
491d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
492d81b4327SDavid Gibson  * processors.
493f0278900SBenjamin Herrenschmidt  */
494f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env)
495f0278900SBenjamin Herrenschmidt {
496f0278900SBenjamin Herrenschmidt     target_ulong v, t;
497f0278900SBenjamin Herrenschmidt     int i;
498f0278900SBenjamin Herrenschmidt 
499f0278900SBenjamin Herrenschmidt #define THRM1_TIN       (1 << 31)
500f0278900SBenjamin Herrenschmidt #define THRM1_TIV       (1 << 30)
501f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
502f0278900SBenjamin Herrenschmidt #define THRM1_TID       (1 << 2)
503f0278900SBenjamin Herrenschmidt #define THRM1_TIE       (1 << 1)
504f0278900SBenjamin Herrenschmidt #define THRM1_V         (1 << 0)
505f0278900SBenjamin Herrenschmidt #define THRM3_E         (1 << 0)
506f0278900SBenjamin Herrenschmidt 
507f0278900SBenjamin Herrenschmidt     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
508f0278900SBenjamin Herrenschmidt         return;
509f0278900SBenjamin Herrenschmidt     }
510f0278900SBenjamin Herrenschmidt 
511f0278900SBenjamin Herrenschmidt     /* Note: Thermal interrupts are unimplemented */
512f0278900SBenjamin Herrenschmidt     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
513f0278900SBenjamin Herrenschmidt         v = env->spr[i];
514f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_V)) {
515f0278900SBenjamin Herrenschmidt             continue;
516f0278900SBenjamin Herrenschmidt         }
517f0278900SBenjamin Herrenschmidt         v |= THRM1_TIV;
518f0278900SBenjamin Herrenschmidt         v &= ~THRM1_TIN;
519f0278900SBenjamin Herrenschmidt         t = v & THRM1_THRES(127);
520f0278900SBenjamin Herrenschmidt         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
521f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
522f0278900SBenjamin Herrenschmidt         }
523f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
524f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
525f0278900SBenjamin Herrenschmidt         }
526f0278900SBenjamin Herrenschmidt         env->spr[i] = v;
527f0278900SBenjamin Herrenschmidt     }
528f0278900SBenjamin Herrenschmidt }
5296bfcf1dcSGlenn Miles 
5306bfcf1dcSGlenn Miles #if !defined(CONFIG_USER_ONLY)
5316bfcf1dcSGlenn Miles #if defined(TARGET_PPC64)
5326bfcf1dcSGlenn Miles void helper_clrbhrb(CPUPPCState *env)
5336bfcf1dcSGlenn Miles {
5346bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB);
5356bfcf1dcSGlenn Miles 
5366bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
5376bfcf1dcSGlenn Miles 
5386bfcf1dcSGlenn Miles     if (env->flags & POWERPC_FLAG_BHRB) {
5396bfcf1dcSGlenn Miles         memset(env->bhrb, 0, sizeof(env->bhrb));
5406bfcf1dcSGlenn Miles     }
5416bfcf1dcSGlenn Miles }
5426bfcf1dcSGlenn Miles 
5436bfcf1dcSGlenn Miles uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe)
5446bfcf1dcSGlenn Miles {
5456bfcf1dcSGlenn Miles     unsigned int index;
5466bfcf1dcSGlenn Miles 
5476bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB);
5486bfcf1dcSGlenn Miles 
5496bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
5506bfcf1dcSGlenn Miles 
5516bfcf1dcSGlenn Miles     if (!(env->flags & POWERPC_FLAG_BHRB) ||
5526bfcf1dcSGlenn Miles          (bhrbe >= env->bhrb_num_entries) ||
5536bfcf1dcSGlenn Miles          (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
5546bfcf1dcSGlenn Miles         return 0;
5556bfcf1dcSGlenn Miles     }
5566bfcf1dcSGlenn Miles 
5576bfcf1dcSGlenn Miles     /*
5586bfcf1dcSGlenn Miles      * Note: bhrb_offset is the byte offset for writing the
5596bfcf1dcSGlenn Miles      * next entry (over the oldest entry), which is why we
5606bfcf1dcSGlenn Miles      * must offset bhrbe by 1 to get to the 0th entry.
5616bfcf1dcSGlenn Miles      */
5626bfcf1dcSGlenn Miles     index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) %
5636bfcf1dcSGlenn Miles             env->bhrb_num_entries;
5646bfcf1dcSGlenn Miles     return env->bhrb[index];
5656bfcf1dcSGlenn Miles }
5666bfcf1dcSGlenn Miles #endif
5676bfcf1dcSGlenn Miles #endif
568