xref: /qemu/target/ppc/misc_helper.c (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
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"
236ff5da16SPhilippe Mathieu-Daudé #include "exec/cputlb.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 */
helper_load_dump_spr(CPUPPCState * env,uint32_t sprn)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 
helper_store_dump_spr(CPUPPCState * env,uint32_t sprn)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 
helper_spr_core_write_generic(CPUPPCState * env,uint32_t sprn,target_ulong val)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 
5250d8cfb9SNicholas Piggin     if (ppc_cpu_core_single_threaded(cs)) {
539cdfd1b9SNicholas Piggin         env->spr[sprn] = val;
549cdfd1b9SNicholas Piggin         return;
559cdfd1b9SNicholas Piggin     }
569cdfd1b9SNicholas Piggin 
579cdfd1b9SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
589cdfd1b9SNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
599cdfd1b9SNicholas Piggin         cenv->spr[sprn] = val;
609cdfd1b9SNicholas Piggin     }
619cdfd1b9SNicholas Piggin }
629cdfd1b9SNicholas Piggin 
helper_spr_write_CTRL(CPUPPCState * env,uint32_t sprn,target_ulong val)63c5d98a7bSNicholas Piggin void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn,
64c5d98a7bSNicholas Piggin                            target_ulong val)
65c5d98a7bSNicholas Piggin {
66c5d98a7bSNicholas Piggin     CPUState *cs = env_cpu(env);
67c5d98a7bSNicholas Piggin     CPUState *ccs;
68c5d98a7bSNicholas Piggin     uint32_t run = val & 1;
69c5d98a7bSNicholas Piggin     uint32_t ts, ts_mask;
70c5d98a7bSNicholas Piggin 
71c5d98a7bSNicholas Piggin     assert(sprn == SPR_CTRL);
72c5d98a7bSNicholas Piggin 
73c5d98a7bSNicholas Piggin     env->spr[sprn] &= ~1U;
74c5d98a7bSNicholas Piggin     env->spr[sprn] |= run;
75c5d98a7bSNicholas Piggin 
76c5d98a7bSNicholas Piggin     ts_mask = ~(1U << (8 + env->spr[SPR_TIR]));
77c5d98a7bSNicholas Piggin     ts = run << (8 + env->spr[SPR_TIR]);
78c5d98a7bSNicholas Piggin 
79c5d98a7bSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
80c5d98a7bSNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
81c5d98a7bSNicholas Piggin 
82c5d98a7bSNicholas Piggin         cenv->spr[sprn] &= ts_mask;
83c5d98a7bSNicholas Piggin         cenv->spr[sprn] |= ts;
84c5d98a7bSNicholas Piggin     }
85c5d98a7bSNicholas Piggin }
86c5d98a7bSNicholas Piggin 
87c5d98a7bSNicholas Piggin 
887019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64
raise_hv_fu_exception(CPUPPCState * env,uint32_t bit,const char * caller,uint32_t cause,uintptr_t raddr)89493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
90493028d8SCédric Le Goater                                   const char *caller, uint32_t cause,
91493028d8SCédric Le Goater                                   uintptr_t raddr)
92493028d8SCédric Le Goater {
93493028d8SCédric Le Goater     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
94493028d8SCédric Le Goater                   bit, caller);
95493028d8SCédric Le Goater 
96493028d8SCédric Le Goater     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
97493028d8SCédric Le Goater 
98493028d8SCédric Le Goater     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
99493028d8SCédric Le Goater }
100493028d8SCédric Le Goater 
raise_fu_exception(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause,uintptr_t raddr)1017019cb3dSAlexey Kardashevskiy static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
10257a2988bSBenjamin Herrenschmidt                                uint32_t sprn, uint32_t cause,
10357a2988bSBenjamin Herrenschmidt                                uintptr_t raddr)
1047019cb3dSAlexey Kardashevskiy {
1057019cb3dSAlexey Kardashevskiy     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
1067019cb3dSAlexey Kardashevskiy 
1077019cb3dSAlexey Kardashevskiy     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
1087019cb3dSAlexey Kardashevskiy     cause &= FSCR_IC_MASK;
1097019cb3dSAlexey Kardashevskiy     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
1107019cb3dSAlexey Kardashevskiy 
11157a2988bSBenjamin Herrenschmidt     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
1127019cb3dSAlexey Kardashevskiy }
1137019cb3dSAlexey Kardashevskiy #endif
1147019cb3dSAlexey Kardashevskiy 
helper_hfscr_facility_check(CPUPPCState * env,uint32_t bit,const char * caller,uint32_t cause)115493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
116493028d8SCédric Le Goater                                  const char *caller, uint32_t cause)
117493028d8SCédric Le Goater {
118493028d8SCédric Le Goater #ifdef TARGET_PPC64
1199de754d3SVíctor Colombo     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
120493028d8SCédric Le Goater                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
121493028d8SCédric Le Goater         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
122493028d8SCédric Le Goater     }
123493028d8SCédric Le Goater #endif
124493028d8SCédric Le Goater }
125493028d8SCédric Le Goater 
helper_fscr_facility_check(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause)1267019cb3dSAlexey Kardashevskiy void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
1277019cb3dSAlexey Kardashevskiy                                 uint32_t sprn, uint32_t cause)
1287019cb3dSAlexey Kardashevskiy {
1297019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64
1307019cb3dSAlexey Kardashevskiy     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
1317019cb3dSAlexey Kardashevskiy         /* Facility is enabled, continue */
1327019cb3dSAlexey Kardashevskiy         return;
1337019cb3dSAlexey Kardashevskiy     }
13457a2988bSBenjamin Herrenschmidt     raise_fu_exception(env, bit, sprn, cause, GETPC());
1357019cb3dSAlexey Kardashevskiy #endif
1367019cb3dSAlexey Kardashevskiy }
1377019cb3dSAlexey Kardashevskiy 
helper_msr_facility_check(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause)138cdcdda27SAlexey Kardashevskiy void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
139cdcdda27SAlexey Kardashevskiy                                uint32_t sprn, uint32_t cause)
140cdcdda27SAlexey Kardashevskiy {
141cdcdda27SAlexey Kardashevskiy #ifdef TARGET_PPC64
142cdcdda27SAlexey Kardashevskiy     if (env->msr & (1ULL << bit)) {
143cdcdda27SAlexey Kardashevskiy         /* Facility is enabled, continue */
144cdcdda27SAlexey Kardashevskiy         return;
145cdcdda27SAlexey Kardashevskiy     }
14657a2988bSBenjamin Herrenschmidt     raise_fu_exception(env, bit, sprn, cause, GETPC());
147cdcdda27SAlexey Kardashevskiy #endif
148cdcdda27SAlexey Kardashevskiy }
149cdcdda27SAlexey Kardashevskiy 
150901c4eafSBlue Swirl #if !defined(CONFIG_USER_ONLY)
151901c4eafSBlue Swirl 
1526bfcf1dcSGlenn Miles #ifdef TARGET_PPC64
helper_mmcr0_facility_check(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause)1536bfcf1dcSGlenn Miles static void helper_mmcr0_facility_check(CPUPPCState *env, uint32_t bit,
1546bfcf1dcSGlenn Miles                                  uint32_t sprn, uint32_t cause)
1556bfcf1dcSGlenn Miles {
1566bfcf1dcSGlenn Miles     if (FIELD_EX64(env->msr, MSR, PR) &&
1576bfcf1dcSGlenn Miles         !(env->spr[SPR_POWER_MMCR0] & (1ULL << bit))) {
1586bfcf1dcSGlenn Miles         raise_fu_exception(env, bit, sprn, cause, GETPC());
1596bfcf1dcSGlenn Miles     }
1606bfcf1dcSGlenn Miles }
1616bfcf1dcSGlenn Miles #endif
1626bfcf1dcSGlenn Miles 
helper_store_sdr1(CPUPPCState * env,target_ulong val)163d523dd00SBlue Swirl void helper_store_sdr1(CPUPPCState *env, target_ulong val)
164901c4eafSBlue Swirl {
1652828c4cdSMark Cave-Ayland     if (env->spr[SPR_SDR1] != val) {
166901c4eafSBlue Swirl         ppc_store_sdr1(env, val);
167db70b311SRichard Henderson         tlb_flush(env_cpu(env));
1682828c4cdSMark Cave-Ayland     }
169901c4eafSBlue Swirl }
170901c4eafSBlue Swirl 
1714a7518e0SCédric Le Goater #if defined(TARGET_PPC64)
helper_store_ptcr(CPUPPCState * env,target_ulong val)1724a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val)
1734a7518e0SCédric Le Goater {
1744a7518e0SCédric Le Goater     if (env->spr[SPR_PTCR] != val) {
1754d2b0ad3SNicholas Piggin         CPUState *cs = env_cpu(env);
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) 
19750d8cfb9SNicholas Piggin         if (ppc_cpu_lpar_single_threaded(cs)) {
19822adb61fSBruno Larsen (billionai)             env->spr[SPR_PTCR] = val;
1994d2b0ad3SNicholas Piggin             tlb_flush(cs);
2004d2b0ad3SNicholas Piggin         } else {
2014d2b0ad3SNicholas Piggin             CPUState *ccs;
2024d2b0ad3SNicholas Piggin 
2034d2b0ad3SNicholas Piggin             THREAD_SIBLING_FOREACH(cs, ccs) {
2044d2b0ad3SNicholas Piggin                 PowerPCCPU *ccpu = POWERPC_CPU(ccs);
2054d2b0ad3SNicholas Piggin                 CPUPPCState *cenv = &ccpu->env;
2064d2b0ad3SNicholas Piggin                 cenv->spr[SPR_PTCR] = val;
2074d2b0ad3SNicholas Piggin                 tlb_flush(ccs);
2084d2b0ad3SNicholas Piggin             }
2094d2b0ad3SNicholas Piggin         }
2104a7518e0SCédric Le Goater     }
2114a7518e0SCédric Le Goater }
2126b375544SJoel Stanley 
helper_store_pcr(CPUPPCState * env,target_ulong value)2136b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value)
2146b375544SJoel Stanley {
215db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
2166b375544SJoel Stanley     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
2176b375544SJoel Stanley 
2186b375544SJoel Stanley     env->spr[SPR_PCR] = value & pcc->pcr_mask;
2196b375544SJoel Stanley }
2205ba7ba1dSCédric Le Goater 
helper_store_ciabr(CPUPPCState * env,target_ulong value)22114192307SNicholas Piggin void helper_store_ciabr(CPUPPCState *env, target_ulong value)
22214192307SNicholas Piggin {
22314192307SNicholas Piggin     ppc_store_ciabr(env, value);
22414192307SNicholas Piggin }
22514192307SNicholas Piggin 
helper_store_dawr0(CPUPPCState * env,target_ulong value)226d5ee641cSNicholas Piggin void helper_store_dawr0(CPUPPCState *env, target_ulong value)
227d5ee641cSNicholas Piggin {
228d5ee641cSNicholas Piggin     ppc_store_dawr0(env, value);
229d5ee641cSNicholas Piggin }
230d5ee641cSNicholas Piggin 
helper_store_dawrx0(CPUPPCState * env,target_ulong value)231d5ee641cSNicholas Piggin void helper_store_dawrx0(CPUPPCState *env, target_ulong value)
232d5ee641cSNicholas Piggin {
233d5ee641cSNicholas Piggin     ppc_store_dawrx0(env, value);
234d5ee641cSNicholas Piggin }
235d5ee641cSNicholas Piggin 
helper_store_dawr1(CPUPPCState * env,target_ulong value)2367ea6e125SShivaprasad G Bhat void helper_store_dawr1(CPUPPCState *env, target_ulong value)
2377ea6e125SShivaprasad G Bhat {
2387ea6e125SShivaprasad G Bhat     ppc_store_dawr1(env, value);
2397ea6e125SShivaprasad G Bhat }
2407ea6e125SShivaprasad G Bhat 
helper_store_dawrx1(CPUPPCState * env,target_ulong value)2417ea6e125SShivaprasad G Bhat void helper_store_dawrx1(CPUPPCState *env, target_ulong value)
2427ea6e125SShivaprasad G Bhat {
2437ea6e125SShivaprasad G Bhat     ppc_store_dawrx1(env, value);
2447ea6e125SShivaprasad G Bhat }
2457ea6e125SShivaprasad G Bhat 
2465ba7ba1dSCédric Le Goater /*
2475ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
2485ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
2495ba7ba1dSCédric Le Goater  */
helper_load_dpdes(CPUPPCState * env)2505ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
2515ba7ba1dSCédric Le Goater {
252d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
253d24e80b2SNicholas Piggin     CPUState *ccs;
2545ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
2555ba7ba1dSCédric Le Goater 
256493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
257493028d8SCédric Le Goater 
25850d8cfb9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
25950d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
260f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2615ba7ba1dSCédric Le Goater             dpdes = 1;
2625ba7ba1dSCédric Le Goater         }
263d24e80b2SNicholas Piggin         return dpdes;
264d24e80b2SNicholas Piggin     }
265d24e80b2SNicholas Piggin 
266195801d7SStefan Hajnoczi     bql_lock();
267d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
268d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
269d24e80b2SNicholas Piggin         CPUPPCState *cenv = &ccpu->env;
270d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
271d24e80b2SNicholas Piggin 
272d24e80b2SNicholas Piggin         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
273d24e80b2SNicholas Piggin             dpdes |= (0x1 << thread_id);
274d24e80b2SNicholas Piggin         }
275d24e80b2SNicholas Piggin     }
276195801d7SStefan Hajnoczi     bql_unlock();
2775ba7ba1dSCédric Le Goater 
2785ba7ba1dSCédric Le Goater     return dpdes;
2795ba7ba1dSCédric Le Goater }
2805ba7ba1dSCédric Le Goater 
helper_store_dpdes(CPUPPCState * env,target_ulong val)2815ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
2825ba7ba1dSCédric Le Goater {
2835ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
284d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
285d24e80b2SNicholas Piggin     CPUState *ccs;
2865ba7ba1dSCédric Le Goater 
287493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
288493028d8SCédric Le Goater 
28950d8cfb9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
29050d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
291d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
2925ba7ba1dSCédric Le Goater         return;
2935ba7ba1dSCédric Le Goater     }
2945ba7ba1dSCédric Le Goater 
295d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
296195801d7SStefan Hajnoczi     bql_lock();
297d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
298d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
299d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
300d24e80b2SNicholas Piggin 
3010324d236SNicholas Piggin         ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
302d24e80b2SNicholas Piggin     }
303195801d7SStefan Hajnoczi     bql_unlock();
3045ba7ba1dSCédric Le Goater }
3052736432fSNicholas Piggin 
30660d30cffSNicholas Piggin /*
30760d30cffSNicholas Piggin  * qemu-user breaks with pnv headers, so they go under ifdefs for now.
30860d30cffSNicholas Piggin  * A clean up may be to move powernv specific registers and helpers into
30960d30cffSNicholas Piggin  * target/ppc/pnv_helper.c
31060d30cffSNicholas Piggin  */
31160d30cffSNicholas Piggin #include "hw/ppc/pnv_core.h"
31260d30cffSNicholas Piggin 
3132736432fSNicholas Piggin /* Indirect SCOM (SPRC/SPRD) access to SCRATCH0-7 are implemented. */
helper_store_sprc(CPUPPCState * env,target_ulong val)3142736432fSNicholas Piggin void helper_store_sprc(CPUPPCState *env, target_ulong val)
3152736432fSNicholas Piggin {
3162736432fSNicholas Piggin     if (val & ~0x3f8ULL) {
3172736432fSNicholas Piggin         qemu_log_mask(LOG_GUEST_ERROR, "Invalid SPRC register value "
3182736432fSNicholas Piggin                       TARGET_FMT_lx"\n", val);
3192736432fSNicholas Piggin         return;
3202736432fSNicholas Piggin     }
3212736432fSNicholas Piggin     env->spr[SPR_POWER_SPRC] = val;
3222736432fSNicholas Piggin }
3232736432fSNicholas Piggin 
helper_load_sprd(CPUPPCState * env)3242736432fSNicholas Piggin target_ulong helper_load_sprd(CPUPPCState *env)
3252736432fSNicholas Piggin {
32660d30cffSNicholas Piggin     /*
32760d30cffSNicholas Piggin      * SPRD is a HV-only register for Power CPUs, so this will only be
32860d30cffSNicholas Piggin      * accessed by powernv machines.
32960d30cffSNicholas Piggin      */
33060d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
33160d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
3322736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
3332736432fSNicholas Piggin 
334*9808ce6dSNicholas Piggin     if (pc->big_core) {
335*9808ce6dSNicholas Piggin         pc = pnv_chip_find_core(pc->chip, CPU_CORE(pc)->core_id & ~0x1);
336*9808ce6dSNicholas Piggin     }
337*9808ce6dSNicholas Piggin 
33860d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
33960d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
34060d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
34160d30cffSNicholas Piggin         return pc->scratch[(sprc >> 3) & 0x7];
34216ffcb34SNicholas Piggin 
34316ffcb34SNicholas Piggin     case 0x1e0: /* core thread state */
34416ffcb34SNicholas Piggin         if (env->excp_model == POWERPC_EXCP_POWER9) {
34516ffcb34SNicholas Piggin             /*
34616ffcb34SNicholas Piggin              * Only implement for POWER9 because skiboot uses it to check
34716ffcb34SNicholas Piggin              * big-core mode. Other bits are unimplemented so we would
34816ffcb34SNicholas Piggin              * prefer to get unimplemented message on POWER10 if it were
34916ffcb34SNicholas Piggin              * used anywhere.
35016ffcb34SNicholas Piggin              */
35116ffcb34SNicholas Piggin             if (pc->big_core) {
35216ffcb34SNicholas Piggin                 return PPC_BIT(63);
35316ffcb34SNicholas Piggin             } else {
35416ffcb34SNicholas Piggin                 return 0;
35516ffcb34SNicholas Piggin             }
35616ffcb34SNicholas Piggin         }
35716ffcb34SNicholas Piggin         /* fallthru */
35816ffcb34SNicholas Piggin 
3592736432fSNicholas Piggin     default:
3602736432fSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mfSPRD: Unimplemented SPRC:0x"
3612736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3622736432fSNicholas Piggin         break;
3632736432fSNicholas Piggin     }
3642736432fSNicholas Piggin     return 0;
3652736432fSNicholas Piggin }
3662736432fSNicholas Piggin 
helper_store_sprd(CPUPPCState * env,target_ulong val)3672736432fSNicholas Piggin void helper_store_sprd(CPUPPCState *env, target_ulong val)
3682736432fSNicholas Piggin {
3692736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
37060d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
37160d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
37260d30cffSNicholas Piggin     int nr;
3732736432fSNicholas Piggin 
374*9808ce6dSNicholas Piggin     if (pc->big_core) {
375*9808ce6dSNicholas Piggin         pc = pnv_chip_find_core(pc->chip, CPU_CORE(pc)->core_id & ~0x1);
376*9808ce6dSNicholas Piggin     }
377*9808ce6dSNicholas Piggin 
37860d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
37960d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
38060d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
38160d30cffSNicholas Piggin         /*
38260d30cffSNicholas Piggin          * Log stores to SCRATCH, because some firmware uses these for
38360d30cffSNicholas Piggin          * debugging and logging, but they would normally be read by the BMC,
38460d30cffSNicholas Piggin          * which is not implemented in QEMU yet. This gives a way to get at the
38560d30cffSNicholas Piggin          * information. Could also dump these upon checkstop.
38660d30cffSNicholas Piggin          */
38760d30cffSNicholas Piggin         nr = (sprc >> 3) & 0x7;
38860d30cffSNicholas Piggin         pc->scratch[nr] = val;
3892736432fSNicholas Piggin         break;
3902736432fSNicholas Piggin     default:
39160d30cffSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mtSPRD: Unimplemented SPRC:0x"
3922736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3932736432fSNicholas Piggin         break;
3942736432fSNicholas Piggin     }
3952736432fSNicholas Piggin }
396d3ce7dc9SNicholas Piggin 
helper_load_pmsr(CPUPPCState * env)397d3ce7dc9SNicholas Piggin target_ulong helper_load_pmsr(CPUPPCState *env)
398d3ce7dc9SNicholas Piggin {
399d3ce7dc9SNicholas Piggin     target_ulong lowerps = extract64(env->spr[SPR_PMCR], PPC_BIT_NR(15), 8);
400d3ce7dc9SNicholas Piggin     target_ulong val = 0;
401d3ce7dc9SNicholas Piggin 
402d3ce7dc9SNicholas Piggin     val |= PPC_BIT(63); /* verion 0x1 (POWER9/10) */
403d3ce7dc9SNicholas Piggin     /* Pmin = 0 */
404d3ce7dc9SNicholas Piggin     /* XXX: POWER9 should be 3 */
405d3ce7dc9SNicholas Piggin     val |= 4ULL << PPC_BIT_NR(31); /* Pmax */
406d3ce7dc9SNicholas Piggin     val |= lowerps << PPC_BIT_NR(15); /* Local actual Pstate */
407d3ce7dc9SNicholas Piggin     val |= lowerps << PPC_BIT_NR(7); /* Global actual Pstate */
408d3ce7dc9SNicholas Piggin 
409d3ce7dc9SNicholas Piggin     return val;
410d3ce7dc9SNicholas Piggin }
411d3ce7dc9SNicholas Piggin 
ppc_set_pmcr(PowerPCCPU * cpu,target_ulong val)412d3ce7dc9SNicholas Piggin static void ppc_set_pmcr(PowerPCCPU *cpu, target_ulong val)
413d3ce7dc9SNicholas Piggin {
414d3ce7dc9SNicholas Piggin     cpu->env.spr[SPR_PMCR] = val;
415d3ce7dc9SNicholas Piggin }
416d3ce7dc9SNicholas Piggin 
helper_store_pmcr(CPUPPCState * env,target_ulong val)417d3ce7dc9SNicholas Piggin void helper_store_pmcr(CPUPPCState *env, target_ulong val)
418d3ce7dc9SNicholas Piggin {
419d3ce7dc9SNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
420d3ce7dc9SNicholas Piggin     CPUState *cs = env_cpu(env);
421d3ce7dc9SNicholas Piggin     CPUState *ccs;
422d3ce7dc9SNicholas Piggin 
423d3ce7dc9SNicholas Piggin     /* Leave version field unchanged (0x1) */
424d3ce7dc9SNicholas Piggin     val &= ~PPC_BITMASK(60, 63);
425d3ce7dc9SNicholas Piggin     val |= PPC_BIT(63);
426d3ce7dc9SNicholas Piggin 
427d3ce7dc9SNicholas Piggin     val &= ~PPC_BITMASK(0, 7); /* UpperPS ignored */
428d3ce7dc9SNicholas Piggin     if (val & PPC_BITMASK(16, 59)) {
429d3ce7dc9SNicholas Piggin         qemu_log_mask(LOG_GUEST_ERROR, "Non-zero PMCR reserved bits "
430d3ce7dc9SNicholas Piggin                       TARGET_FMT_lx"\n", val);
431d3ce7dc9SNicholas Piggin         val &= ~PPC_BITMASK(16, 59);
432d3ce7dc9SNicholas Piggin     }
433d3ce7dc9SNicholas Piggin 
434d3ce7dc9SNicholas Piggin     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
435d3ce7dc9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
436d3ce7dc9SNicholas Piggin         ppc_set_pmcr(cpu, val);
437d3ce7dc9SNicholas Piggin         return;
438d3ce7dc9SNicholas Piggin     }
439d3ce7dc9SNicholas Piggin 
440d3ce7dc9SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
441d3ce7dc9SNicholas Piggin     bql_lock();
442d3ce7dc9SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
443d3ce7dc9SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
444d3ce7dc9SNicholas Piggin         ppc_set_pmcr(ccpu, val);
445d3ce7dc9SNicholas Piggin     }
446d3ce7dc9SNicholas Piggin     bql_unlock();
447d3ce7dc9SNicholas Piggin }
448d3ce7dc9SNicholas Piggin 
4494a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
4504a7518e0SCédric Le Goater 
helper_store_pidr(CPUPPCState * env,target_ulong val)45131b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
45231b2b0f8SSuraj Jitindar Singh {
453fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
454db70b311SRichard Henderson     tlb_flush(env_cpu(env));
45531b2b0f8SSuraj Jitindar Singh }
45631b2b0f8SSuraj Jitindar Singh 
helper_store_lpidr(CPUPPCState * env,target_ulong val)457c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
458c4dae9cdSBenjamin Herrenschmidt {
459fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
460c4dae9cdSBenjamin Herrenschmidt 
461c4dae9cdSBenjamin Herrenschmidt     /*
462c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
463c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
464c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
465c4dae9cdSBenjamin Herrenschmidt      * well.
466c4dae9cdSBenjamin Herrenschmidt      */
467db70b311SRichard Henderson     tlb_flush(env_cpu(env));
468c4dae9cdSBenjamin Herrenschmidt }
469c4dae9cdSBenjamin Herrenschmidt 
helper_store_40x_dbcr0(CPUPPCState * env,target_ulong val)470d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
471901c4eafSBlue Swirl {
4727da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
4737da31f26SRichard Henderson     hreg_compute_hflags(env);
4747da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
475901c4eafSBlue Swirl     store_40x_dbcr0(env, val);
476901c4eafSBlue Swirl }
477901c4eafSBlue Swirl 
helper_store_40x_sler(CPUPPCState * env,target_ulong val)478d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
479901c4eafSBlue Swirl {
480901c4eafSBlue Swirl     store_40x_sler(env, val);
481901c4eafSBlue Swirl }
482901c4eafSBlue Swirl #endif
4838555f71dSBlue Swirl 
4848555f71dSBlue Swirl /*****************************************************************************/
4858555f71dSBlue Swirl /* Special registers manipulation */
4868555f71dSBlue Swirl 
487d81b4327SDavid Gibson /*
488d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
489d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
490d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
491d81b4327SDavid Gibson  * processors.
492f0278900SBenjamin Herrenschmidt  */
helper_fixup_thrm(CPUPPCState * env)493f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env)
494f0278900SBenjamin Herrenschmidt {
495f0278900SBenjamin Herrenschmidt     target_ulong v, t;
496f0278900SBenjamin Herrenschmidt     int i;
497f0278900SBenjamin Herrenschmidt 
498f0278900SBenjamin Herrenschmidt #define THRM1_TIN       (1 << 31)
499f0278900SBenjamin Herrenschmidt #define THRM1_TIV       (1 << 30)
500f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
501f0278900SBenjamin Herrenschmidt #define THRM1_TID       (1 << 2)
502f0278900SBenjamin Herrenschmidt #define THRM1_TIE       (1 << 1)
503f0278900SBenjamin Herrenschmidt #define THRM1_V         (1 << 0)
504f0278900SBenjamin Herrenschmidt #define THRM3_E         (1 << 0)
505f0278900SBenjamin Herrenschmidt 
506f0278900SBenjamin Herrenschmidt     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
507f0278900SBenjamin Herrenschmidt         return;
508f0278900SBenjamin Herrenschmidt     }
509f0278900SBenjamin Herrenschmidt 
510f0278900SBenjamin Herrenschmidt     /* Note: Thermal interrupts are unimplemented */
511f0278900SBenjamin Herrenschmidt     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
512f0278900SBenjamin Herrenschmidt         v = env->spr[i];
513f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_V)) {
514f0278900SBenjamin Herrenschmidt             continue;
515f0278900SBenjamin Herrenschmidt         }
516f0278900SBenjamin Herrenschmidt         v |= THRM1_TIV;
517f0278900SBenjamin Herrenschmidt         v &= ~THRM1_TIN;
518f0278900SBenjamin Herrenschmidt         t = v & THRM1_THRES(127);
519f0278900SBenjamin Herrenschmidt         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
520f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
521f0278900SBenjamin Herrenschmidt         }
522f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
523f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
524f0278900SBenjamin Herrenschmidt         }
525f0278900SBenjamin Herrenschmidt         env->spr[i] = v;
526f0278900SBenjamin Herrenschmidt     }
527f0278900SBenjamin Herrenschmidt }
5286bfcf1dcSGlenn Miles 
5296bfcf1dcSGlenn Miles #if !defined(CONFIG_USER_ONLY)
5306bfcf1dcSGlenn Miles #if defined(TARGET_PPC64)
helper_clrbhrb(CPUPPCState * env)5316bfcf1dcSGlenn Miles void helper_clrbhrb(CPUPPCState *env)
5326bfcf1dcSGlenn Miles {
5336bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB);
5346bfcf1dcSGlenn Miles 
5356bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
5366bfcf1dcSGlenn Miles 
5376bfcf1dcSGlenn Miles     if (env->flags & POWERPC_FLAG_BHRB) {
5386bfcf1dcSGlenn Miles         memset(env->bhrb, 0, sizeof(env->bhrb));
5396bfcf1dcSGlenn Miles     }
5406bfcf1dcSGlenn Miles }
5416bfcf1dcSGlenn Miles 
helper_mfbhrbe(CPUPPCState * env,uint32_t bhrbe)5426bfcf1dcSGlenn Miles uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe)
5436bfcf1dcSGlenn Miles {
5446bfcf1dcSGlenn Miles     unsigned int index;
5456bfcf1dcSGlenn Miles 
5466bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB);
5476bfcf1dcSGlenn Miles 
5486bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
5496bfcf1dcSGlenn Miles 
5506bfcf1dcSGlenn Miles     if (!(env->flags & POWERPC_FLAG_BHRB) ||
5516bfcf1dcSGlenn Miles          (bhrbe >= env->bhrb_num_entries) ||
5526bfcf1dcSGlenn Miles          (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
5536bfcf1dcSGlenn Miles         return 0;
5546bfcf1dcSGlenn Miles     }
5556bfcf1dcSGlenn Miles 
5566bfcf1dcSGlenn Miles     /*
5576bfcf1dcSGlenn Miles      * Note: bhrb_offset is the byte offset for writing the
5586bfcf1dcSGlenn Miles      * next entry (over the oldest entry), which is why we
5596bfcf1dcSGlenn Miles      * must offset bhrbe by 1 to get to the 0th entry.
5606bfcf1dcSGlenn Miles      */
5616bfcf1dcSGlenn Miles     index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) %
5626bfcf1dcSGlenn Miles             env->bhrb_num_entries;
5636bfcf1dcSGlenn Miles     return env->bhrb[index];
5646bfcf1dcSGlenn Miles }
5656bfcf1dcSGlenn Miles #endif
5666bfcf1dcSGlenn Miles #endif
567