xref: /qemu/target/ppc/misc_helper.c (revision 60d30cff8472c0bf05a40b0f55221fb4efb768e2)
1901c4eafSBlue Swirl /*
2901c4eafSBlue Swirl  * Miscellaneous PowerPC emulation helpers for QEMU.
3901c4eafSBlue Swirl  *
4901c4eafSBlue Swirl  *  Copyright (c) 2003-2007 Jocelyn Mayer
5901c4eafSBlue Swirl  *
6901c4eafSBlue Swirl  * This library is free software; you can redistribute it and/or
7901c4eafSBlue Swirl  * modify it under the terms of the GNU Lesser General Public
8901c4eafSBlue Swirl  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10901c4eafSBlue Swirl  *
11901c4eafSBlue Swirl  * This library is distributed in the hope that it will be useful,
12901c4eafSBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13901c4eafSBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14901c4eafSBlue Swirl  * Lesser General Public License for more details.
15901c4eafSBlue Swirl  *
16901c4eafSBlue Swirl  * You should have received a copy of the GNU Lesser General Public
17901c4eafSBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18901c4eafSBlue Swirl  */
19db725815SMarkus Armbruster 
200d75590dSPeter Maydell #include "qemu/osdep.h"
21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
22901c4eafSBlue Swirl #include "cpu.h"
2363c91552SPaolo Bonzini #include "exec/exec-all.h"
242ef6175aSRichard Henderson #include "exec/helper-proto.h"
256b375544SJoel Stanley #include "qemu/error-report.h"
26db725815SMarkus Armbruster #include "qemu/main-loop.h"
2722adb61fSBruno Larsen (billionai) #include "mmu-book3s-v3.h"
287b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
29901c4eafSBlue Swirl 
30901c4eafSBlue Swirl #include "helper_regs.h"
31901c4eafSBlue Swirl 
32901c4eafSBlue Swirl /*****************************************************************************/
33901c4eafSBlue Swirl /* SPR accesses */
34d523dd00SBlue Swirl void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn)
35901c4eafSBlue Swirl {
36901c4eafSBlue Swirl     qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn,
37901c4eafSBlue Swirl              env->spr[sprn]);
38901c4eafSBlue Swirl }
39901c4eafSBlue Swirl 
40d523dd00SBlue Swirl void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn)
41901c4eafSBlue Swirl {
42901c4eafSBlue Swirl     qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn,
43901c4eafSBlue Swirl              env->spr[sprn]);
44901c4eafSBlue Swirl }
457019cb3dSAlexey Kardashevskiy 
469cdfd1b9SNicholas Piggin void helper_spr_core_write_generic(CPUPPCState *env, uint32_t sprn,
479cdfd1b9SNicholas Piggin                                    target_ulong val)
489cdfd1b9SNicholas Piggin {
499cdfd1b9SNicholas Piggin     CPUState *cs = env_cpu(env);
509cdfd1b9SNicholas Piggin     CPUState *ccs;
519cdfd1b9SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
529cdfd1b9SNicholas Piggin 
539cdfd1b9SNicholas Piggin     if (nr_threads == 1) {
549cdfd1b9SNicholas Piggin         env->spr[sprn] = val;
559cdfd1b9SNicholas Piggin         return;
569cdfd1b9SNicholas Piggin     }
579cdfd1b9SNicholas Piggin 
589cdfd1b9SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
599cdfd1b9SNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
609cdfd1b9SNicholas Piggin         cenv->spr[sprn] = val;
619cdfd1b9SNicholas Piggin     }
629cdfd1b9SNicholas Piggin }
639cdfd1b9SNicholas Piggin 
64c5d98a7bSNicholas Piggin void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn,
65c5d98a7bSNicholas Piggin                            target_ulong val)
66c5d98a7bSNicholas Piggin {
67c5d98a7bSNicholas Piggin     CPUState *cs = env_cpu(env);
68c5d98a7bSNicholas Piggin     CPUState *ccs;
69c5d98a7bSNicholas Piggin     uint32_t run = val & 1;
70c5d98a7bSNicholas Piggin     uint32_t ts, ts_mask;
71c5d98a7bSNicholas Piggin 
72c5d98a7bSNicholas Piggin     assert(sprn == SPR_CTRL);
73c5d98a7bSNicholas Piggin 
74c5d98a7bSNicholas Piggin     env->spr[sprn] &= ~1U;
75c5d98a7bSNicholas Piggin     env->spr[sprn] |= run;
76c5d98a7bSNicholas Piggin 
77c5d98a7bSNicholas Piggin     ts_mask = ~(1U << (8 + env->spr[SPR_TIR]));
78c5d98a7bSNicholas Piggin     ts = run << (8 + env->spr[SPR_TIR]);
79c5d98a7bSNicholas Piggin 
80c5d98a7bSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
81c5d98a7bSNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
82c5d98a7bSNicholas Piggin 
83c5d98a7bSNicholas Piggin         cenv->spr[sprn] &= ts_mask;
84c5d98a7bSNicholas Piggin         cenv->spr[sprn] |= ts;
85c5d98a7bSNicholas Piggin     }
86c5d98a7bSNicholas Piggin }
87c5d98a7bSNicholas Piggin 
88c5d98a7bSNicholas Piggin 
897019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64
90493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
91493028d8SCédric Le Goater                                   const char *caller, uint32_t cause,
92493028d8SCédric Le Goater                                   uintptr_t raddr)
93493028d8SCédric Le Goater {
94493028d8SCédric Le Goater     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
95493028d8SCédric Le Goater                   bit, caller);
96493028d8SCédric Le Goater 
97493028d8SCédric Le Goater     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
98493028d8SCédric Le Goater 
99493028d8SCédric Le Goater     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
100493028d8SCédric Le Goater }
101493028d8SCédric Le Goater 
1027019cb3dSAlexey Kardashevskiy static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
10357a2988bSBenjamin Herrenschmidt                                uint32_t sprn, uint32_t cause,
10457a2988bSBenjamin Herrenschmidt                                uintptr_t raddr)
1057019cb3dSAlexey Kardashevskiy {
1067019cb3dSAlexey Kardashevskiy     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
1077019cb3dSAlexey Kardashevskiy 
1087019cb3dSAlexey Kardashevskiy     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
1097019cb3dSAlexey Kardashevskiy     cause &= FSCR_IC_MASK;
1107019cb3dSAlexey Kardashevskiy     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
1117019cb3dSAlexey Kardashevskiy 
11257a2988bSBenjamin Herrenschmidt     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
1137019cb3dSAlexey Kardashevskiy }
1147019cb3dSAlexey Kardashevskiy #endif
1157019cb3dSAlexey Kardashevskiy 
116493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
117493028d8SCédric Le Goater                                  const char *caller, uint32_t cause)
118493028d8SCédric Le Goater {
119493028d8SCédric Le Goater #ifdef TARGET_PPC64
1209de754d3SVíctor Colombo     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
121493028d8SCédric Le Goater                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
122493028d8SCédric Le Goater         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
123493028d8SCédric Le Goater     }
124493028d8SCédric Le Goater #endif
125493028d8SCédric Le Goater }
126493028d8SCédric Le Goater 
1277019cb3dSAlexey Kardashevskiy void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
1287019cb3dSAlexey Kardashevskiy                                 uint32_t sprn, uint32_t cause)
1297019cb3dSAlexey Kardashevskiy {
1307019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64
1317019cb3dSAlexey Kardashevskiy     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
1327019cb3dSAlexey Kardashevskiy         /* Facility is enabled, continue */
1337019cb3dSAlexey Kardashevskiy         return;
1347019cb3dSAlexey Kardashevskiy     }
13557a2988bSBenjamin Herrenschmidt     raise_fu_exception(env, bit, sprn, cause, GETPC());
1367019cb3dSAlexey Kardashevskiy #endif
1377019cb3dSAlexey Kardashevskiy }
1387019cb3dSAlexey Kardashevskiy 
139cdcdda27SAlexey Kardashevskiy void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
140cdcdda27SAlexey Kardashevskiy                                uint32_t sprn, uint32_t cause)
141cdcdda27SAlexey Kardashevskiy {
142cdcdda27SAlexey Kardashevskiy #ifdef TARGET_PPC64
143cdcdda27SAlexey Kardashevskiy     if (env->msr & (1ULL << bit)) {
144cdcdda27SAlexey Kardashevskiy         /* Facility is enabled, continue */
145cdcdda27SAlexey Kardashevskiy         return;
146cdcdda27SAlexey Kardashevskiy     }
14757a2988bSBenjamin Herrenschmidt     raise_fu_exception(env, bit, sprn, cause, GETPC());
148cdcdda27SAlexey Kardashevskiy #endif
149cdcdda27SAlexey Kardashevskiy }
150cdcdda27SAlexey Kardashevskiy 
151901c4eafSBlue Swirl #if !defined(CONFIG_USER_ONLY)
152901c4eafSBlue Swirl 
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) 
1984d2b0ad3SNicholas Piggin         if (cs->nr_threads == 1 || !(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
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;
245d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
2465ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
2475ba7ba1dSCédric Le Goater 
248493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
249493028d8SCédric Le Goater 
2503401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
2513401ea3cSNicholas Piggin         nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */
2523401ea3cSNicholas Piggin     }
2533401ea3cSNicholas Piggin 
254d24e80b2SNicholas Piggin     if (nr_threads == 1) {
255f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2565ba7ba1dSCédric Le Goater             dpdes = 1;
2575ba7ba1dSCédric Le Goater         }
258d24e80b2SNicholas Piggin         return dpdes;
259d24e80b2SNicholas Piggin     }
260d24e80b2SNicholas Piggin 
261195801d7SStefan Hajnoczi     bql_lock();
262d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
263d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
264d24e80b2SNicholas Piggin         CPUPPCState *cenv = &ccpu->env;
265d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
266d24e80b2SNicholas Piggin 
267d24e80b2SNicholas Piggin         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
268d24e80b2SNicholas Piggin             dpdes |= (0x1 << thread_id);
269d24e80b2SNicholas Piggin         }
270d24e80b2SNicholas Piggin     }
271195801d7SStefan Hajnoczi     bql_unlock();
2725ba7ba1dSCédric Le Goater 
2735ba7ba1dSCédric Le Goater     return dpdes;
2745ba7ba1dSCédric Le Goater }
2755ba7ba1dSCédric Le Goater 
2765ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
2775ba7ba1dSCédric Le Goater {
2785ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
279d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
280d24e80b2SNicholas Piggin     CPUState *ccs;
281d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
2825ba7ba1dSCédric Le Goater 
283493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
284493028d8SCédric Le Goater 
2853401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
2863401ea3cSNicholas Piggin         nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */
2873401ea3cSNicholas Piggin     }
2883401ea3cSNicholas Piggin 
289d24e80b2SNicholas Piggin     if (val & ~(nr_threads - 1)) {
2905ba7ba1dSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value "
2915ba7ba1dSCédric Le Goater                       TARGET_FMT_lx"\n", val);
292d24e80b2SNicholas Piggin         val &= (nr_threads - 1); /* Ignore the invalid bits */
293d24e80b2SNicholas Piggin     }
294d24e80b2SNicholas Piggin 
295d24e80b2SNicholas Piggin     if (nr_threads == 1) {
296d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
2975ba7ba1dSCédric Le Goater         return;
2985ba7ba1dSCédric Le Goater     }
2995ba7ba1dSCédric Le Goater 
300d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
301195801d7SStefan Hajnoczi     bql_lock();
302d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
303d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
304d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
305d24e80b2SNicholas Piggin 
306d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
307d24e80b2SNicholas Piggin     }
308195801d7SStefan Hajnoczi     bql_unlock();
3095ba7ba1dSCédric Le Goater }
3102736432fSNicholas Piggin 
311*60d30cffSNicholas Piggin /*
312*60d30cffSNicholas Piggin  * qemu-user breaks with pnv headers, so they go under ifdefs for now.
313*60d30cffSNicholas Piggin  * A clean up may be to move powernv specific registers and helpers into
314*60d30cffSNicholas Piggin  * target/ppc/pnv_helper.c
315*60d30cffSNicholas Piggin  */
316*60d30cffSNicholas Piggin #include "hw/ppc/pnv_core.h"
317*60d30cffSNicholas Piggin 
3182736432fSNicholas Piggin /* Indirect SCOM (SPRC/SPRD) access to SCRATCH0-7 are implemented. */
3192736432fSNicholas Piggin void helper_store_sprc(CPUPPCState *env, target_ulong val)
3202736432fSNicholas Piggin {
3212736432fSNicholas Piggin     if (val & ~0x3f8ULL) {
3222736432fSNicholas Piggin         qemu_log_mask(LOG_GUEST_ERROR, "Invalid SPRC register value "
3232736432fSNicholas Piggin                       TARGET_FMT_lx"\n", val);
3242736432fSNicholas Piggin         return;
3252736432fSNicholas Piggin     }
3262736432fSNicholas Piggin     env->spr[SPR_POWER_SPRC] = val;
3272736432fSNicholas Piggin }
3282736432fSNicholas Piggin 
3292736432fSNicholas Piggin target_ulong helper_load_sprd(CPUPPCState *env)
3302736432fSNicholas Piggin {
331*60d30cffSNicholas Piggin     /*
332*60d30cffSNicholas Piggin      * SPRD is a HV-only register for Power CPUs, so this will only be
333*60d30cffSNicholas Piggin      * accessed by powernv machines.
334*60d30cffSNicholas Piggin      */
335*60d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
336*60d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
3372736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
3382736432fSNicholas Piggin 
339*60d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
340*60d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
341*60d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
342*60d30cffSNicholas Piggin         return pc->scratch[(sprc >> 3) & 0x7];
3432736432fSNicholas Piggin     default:
3442736432fSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mfSPRD: Unimplemented SPRC:0x"
3452736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3462736432fSNicholas Piggin         break;
3472736432fSNicholas Piggin     }
3482736432fSNicholas Piggin     return 0;
3492736432fSNicholas Piggin }
3502736432fSNicholas Piggin 
3512736432fSNicholas Piggin void helper_store_sprd(CPUPPCState *env, target_ulong val)
3522736432fSNicholas Piggin {
3532736432fSNicholas Piggin     target_ulong sprc = env->spr[SPR_POWER_SPRC];
354*60d30cffSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
355*60d30cffSNicholas Piggin     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
356*60d30cffSNicholas Piggin     int nr;
3572736432fSNicholas Piggin 
358*60d30cffSNicholas Piggin     switch (sprc & 0x3e0) {
359*60d30cffSNicholas Piggin     case 0: /* SCRATCH0-3 */
360*60d30cffSNicholas Piggin     case 1: /* SCRATCH4-7 */
361*60d30cffSNicholas Piggin         /*
362*60d30cffSNicholas Piggin          * Log stores to SCRATCH, because some firmware uses these for
363*60d30cffSNicholas Piggin          * debugging and logging, but they would normally be read by the BMC,
364*60d30cffSNicholas Piggin          * which is not implemented in QEMU yet. This gives a way to get at the
365*60d30cffSNicholas Piggin          * information. Could also dump these upon checkstop.
366*60d30cffSNicholas Piggin          */
367*60d30cffSNicholas Piggin         nr = (sprc >> 3) & 0x7;
368*60d30cffSNicholas Piggin         qemu_log("SPRD write 0x" TARGET_FMT_lx " to SCRATCH%d\n", val, nr);
369*60d30cffSNicholas Piggin         pc->scratch[nr] = val;
3702736432fSNicholas Piggin         break;
3712736432fSNicholas Piggin     default:
372*60d30cffSNicholas Piggin         qemu_log_mask(LOG_UNIMP, "mtSPRD: Unimplemented SPRC:0x"
3732736432fSNicholas Piggin                                   TARGET_FMT_lx"\n", sprc);
3742736432fSNicholas Piggin         break;
3752736432fSNicholas Piggin     }
3762736432fSNicholas Piggin }
3774a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
3784a7518e0SCédric Le Goater 
37931b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
38031b2b0f8SSuraj Jitindar Singh {
381fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
382db70b311SRichard Henderson     tlb_flush(env_cpu(env));
38331b2b0f8SSuraj Jitindar Singh }
38431b2b0f8SSuraj Jitindar Singh 
385c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
386c4dae9cdSBenjamin Herrenschmidt {
387fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
388c4dae9cdSBenjamin Herrenschmidt 
389c4dae9cdSBenjamin Herrenschmidt     /*
390c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
391c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
392c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
393c4dae9cdSBenjamin Herrenschmidt      * well.
394c4dae9cdSBenjamin Herrenschmidt      */
395db70b311SRichard Henderson     tlb_flush(env_cpu(env));
396c4dae9cdSBenjamin Herrenschmidt }
397c4dae9cdSBenjamin Herrenschmidt 
398d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
399901c4eafSBlue Swirl {
4007da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
4017da31f26SRichard Henderson     hreg_compute_hflags(env);
4027da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
403901c4eafSBlue Swirl     store_40x_dbcr0(env, val);
404901c4eafSBlue Swirl }
405901c4eafSBlue Swirl 
406d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
407901c4eafSBlue Swirl {
408901c4eafSBlue Swirl     store_40x_sler(env, val);
409901c4eafSBlue Swirl }
410901c4eafSBlue Swirl #endif
4118555f71dSBlue Swirl 
4128555f71dSBlue Swirl /*****************************************************************************/
4138555f71dSBlue Swirl /* Special registers manipulation */
4148555f71dSBlue Swirl 
415d81b4327SDavid Gibson /*
416d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
417d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
418d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
419d81b4327SDavid Gibson  * processors.
420f0278900SBenjamin Herrenschmidt  */
421f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env)
422f0278900SBenjamin Herrenschmidt {
423f0278900SBenjamin Herrenschmidt     target_ulong v, t;
424f0278900SBenjamin Herrenschmidt     int i;
425f0278900SBenjamin Herrenschmidt 
426f0278900SBenjamin Herrenschmidt #define THRM1_TIN       (1 << 31)
427f0278900SBenjamin Herrenschmidt #define THRM1_TIV       (1 << 30)
428f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
429f0278900SBenjamin Herrenschmidt #define THRM1_TID       (1 << 2)
430f0278900SBenjamin Herrenschmidt #define THRM1_TIE       (1 << 1)
431f0278900SBenjamin Herrenschmidt #define THRM1_V         (1 << 0)
432f0278900SBenjamin Herrenschmidt #define THRM3_E         (1 << 0)
433f0278900SBenjamin Herrenschmidt 
434f0278900SBenjamin Herrenschmidt     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
435f0278900SBenjamin Herrenschmidt         return;
436f0278900SBenjamin Herrenschmidt     }
437f0278900SBenjamin Herrenschmidt 
438f0278900SBenjamin Herrenschmidt     /* Note: Thermal interrupts are unimplemented */
439f0278900SBenjamin Herrenschmidt     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
440f0278900SBenjamin Herrenschmidt         v = env->spr[i];
441f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_V)) {
442f0278900SBenjamin Herrenschmidt             continue;
443f0278900SBenjamin Herrenschmidt         }
444f0278900SBenjamin Herrenschmidt         v |= THRM1_TIV;
445f0278900SBenjamin Herrenschmidt         v &= ~THRM1_TIN;
446f0278900SBenjamin Herrenschmidt         t = v & THRM1_THRES(127);
447f0278900SBenjamin Herrenschmidt         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
448f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
449f0278900SBenjamin Herrenschmidt         }
450f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
451f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
452f0278900SBenjamin Herrenschmidt         }
453f0278900SBenjamin Herrenschmidt         env->spr[i] = v;
454f0278900SBenjamin Herrenschmidt     }
455f0278900SBenjamin Herrenschmidt }
4566bfcf1dcSGlenn Miles 
4576bfcf1dcSGlenn Miles #if !defined(CONFIG_USER_ONLY)
4586bfcf1dcSGlenn Miles #if defined(TARGET_PPC64)
4596bfcf1dcSGlenn Miles void helper_clrbhrb(CPUPPCState *env)
4606bfcf1dcSGlenn Miles {
4616bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB);
4626bfcf1dcSGlenn Miles 
4636bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
4646bfcf1dcSGlenn Miles 
4656bfcf1dcSGlenn Miles     if (env->flags & POWERPC_FLAG_BHRB) {
4666bfcf1dcSGlenn Miles         memset(env->bhrb, 0, sizeof(env->bhrb));
4676bfcf1dcSGlenn Miles     }
4686bfcf1dcSGlenn Miles }
4696bfcf1dcSGlenn Miles 
4706bfcf1dcSGlenn Miles uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe)
4716bfcf1dcSGlenn Miles {
4726bfcf1dcSGlenn Miles     unsigned int index;
4736bfcf1dcSGlenn Miles 
4746bfcf1dcSGlenn Miles     helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB);
4756bfcf1dcSGlenn Miles 
4766bfcf1dcSGlenn Miles     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
4776bfcf1dcSGlenn Miles 
4786bfcf1dcSGlenn Miles     if (!(env->flags & POWERPC_FLAG_BHRB) ||
4796bfcf1dcSGlenn Miles          (bhrbe >= env->bhrb_num_entries) ||
4806bfcf1dcSGlenn Miles          (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
4816bfcf1dcSGlenn Miles         return 0;
4826bfcf1dcSGlenn Miles     }
4836bfcf1dcSGlenn Miles 
4846bfcf1dcSGlenn Miles     /*
4856bfcf1dcSGlenn Miles      * Note: bhrb_offset is the byte offset for writing the
4866bfcf1dcSGlenn Miles      * next entry (over the oldest entry), which is why we
4876bfcf1dcSGlenn Miles      * must offset bhrbe by 1 to get to the 0th entry.
4886bfcf1dcSGlenn Miles      */
4896bfcf1dcSGlenn Miles     index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) %
4906bfcf1dcSGlenn Miles             env->bhrb_num_entries;
4916bfcf1dcSGlenn Miles     return env->bhrb[index];
4926bfcf1dcSGlenn Miles }
4936bfcf1dcSGlenn Miles #endif
4946bfcf1dcSGlenn Miles #endif
495