xref: /qemu/target/ppc/misc_helper.c (revision fbda88f7abdeed3ceebdd18de6909a52df756c1c)
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 
467019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64
47493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
48493028d8SCédric Le Goater                                   const char *caller, uint32_t cause,
49493028d8SCédric Le Goater                                   uintptr_t raddr)
50493028d8SCédric Le Goater {
51493028d8SCédric Le Goater     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
52493028d8SCédric Le Goater                   bit, caller);
53493028d8SCédric Le Goater 
54493028d8SCédric Le Goater     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
55493028d8SCédric Le Goater 
56493028d8SCédric Le Goater     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
57493028d8SCédric Le Goater }
58493028d8SCédric Le Goater 
597019cb3dSAlexey Kardashevskiy static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
6057a2988bSBenjamin Herrenschmidt                                uint32_t sprn, uint32_t cause,
6157a2988bSBenjamin Herrenschmidt                                uintptr_t raddr)
627019cb3dSAlexey Kardashevskiy {
637019cb3dSAlexey Kardashevskiy     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
647019cb3dSAlexey Kardashevskiy 
657019cb3dSAlexey Kardashevskiy     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
667019cb3dSAlexey Kardashevskiy     cause &= FSCR_IC_MASK;
677019cb3dSAlexey Kardashevskiy     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
687019cb3dSAlexey Kardashevskiy 
6957a2988bSBenjamin Herrenschmidt     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
707019cb3dSAlexey Kardashevskiy }
717019cb3dSAlexey Kardashevskiy #endif
727019cb3dSAlexey Kardashevskiy 
73493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
74493028d8SCédric Le Goater                                  const char *caller, uint32_t cause)
75493028d8SCédric Le Goater {
76493028d8SCédric Le Goater #ifdef TARGET_PPC64
779de754d3SVíctor Colombo     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
78493028d8SCédric Le Goater                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
79493028d8SCédric Le Goater         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
80493028d8SCédric Le Goater     }
81493028d8SCédric Le Goater #endif
82493028d8SCédric Le Goater }
83493028d8SCédric Le Goater 
847019cb3dSAlexey Kardashevskiy void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
857019cb3dSAlexey Kardashevskiy                                 uint32_t sprn, uint32_t cause)
867019cb3dSAlexey Kardashevskiy {
877019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64
887019cb3dSAlexey Kardashevskiy     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
897019cb3dSAlexey Kardashevskiy         /* Facility is enabled, continue */
907019cb3dSAlexey Kardashevskiy         return;
917019cb3dSAlexey Kardashevskiy     }
9257a2988bSBenjamin Herrenschmidt     raise_fu_exception(env, bit, sprn, cause, GETPC());
937019cb3dSAlexey Kardashevskiy #endif
947019cb3dSAlexey Kardashevskiy }
957019cb3dSAlexey Kardashevskiy 
96cdcdda27SAlexey Kardashevskiy void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
97cdcdda27SAlexey Kardashevskiy                                uint32_t sprn, uint32_t cause)
98cdcdda27SAlexey Kardashevskiy {
99cdcdda27SAlexey Kardashevskiy #ifdef TARGET_PPC64
100cdcdda27SAlexey Kardashevskiy     if (env->msr & (1ULL << bit)) {
101cdcdda27SAlexey Kardashevskiy         /* Facility is enabled, continue */
102cdcdda27SAlexey Kardashevskiy         return;
103cdcdda27SAlexey Kardashevskiy     }
10457a2988bSBenjamin Herrenschmidt     raise_fu_exception(env, bit, sprn, cause, GETPC());
105cdcdda27SAlexey Kardashevskiy #endif
106cdcdda27SAlexey Kardashevskiy }
107cdcdda27SAlexey Kardashevskiy 
108901c4eafSBlue Swirl #if !defined(CONFIG_USER_ONLY)
109901c4eafSBlue Swirl 
110d523dd00SBlue Swirl void helper_store_sdr1(CPUPPCState *env, target_ulong val)
111901c4eafSBlue Swirl {
1122828c4cdSMark Cave-Ayland     if (env->spr[SPR_SDR1] != val) {
113901c4eafSBlue Swirl         ppc_store_sdr1(env, val);
114db70b311SRichard Henderson         tlb_flush(env_cpu(env));
1152828c4cdSMark Cave-Ayland     }
116901c4eafSBlue Swirl }
117901c4eafSBlue Swirl 
1184a7518e0SCédric Le Goater #if defined(TARGET_PPC64)
1194a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val)
1204a7518e0SCédric Le Goater {
1214a7518e0SCédric Le Goater     if (env->spr[SPR_PTCR] != val) {
12222adb61fSBruno Larsen (billionai)         PowerPCCPU *cpu = env_archcpu(env);
12322adb61fSBruno Larsen (billionai)         target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS;
12422adb61fSBruno Larsen (billionai)         target_ulong patbsize = val & PTCR_PATS;
12522adb61fSBruno Larsen (billionai) 
12622adb61fSBruno Larsen (billionai)         qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val);
12722adb61fSBruno Larsen (billionai) 
12822adb61fSBruno Larsen (billionai)         assert(!cpu->vhyp);
12922adb61fSBruno Larsen (billionai)         assert(env->mmu_model & POWERPC_MMU_3_00);
13022adb61fSBruno Larsen (billionai) 
13122adb61fSBruno Larsen (billionai)         if (val & ~ptcr_mask) {
13222adb61fSBruno Larsen (billionai)             error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR",
13322adb61fSBruno Larsen (billionai)                          val & ~ptcr_mask);
13422adb61fSBruno Larsen (billionai)             val &= ptcr_mask;
13522adb61fSBruno Larsen (billionai)         }
13622adb61fSBruno Larsen (billionai) 
13722adb61fSBruno Larsen (billionai)         if (patbsize > 24) {
13822adb61fSBruno Larsen (billionai)             error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
13922adb61fSBruno Larsen (billionai)                          " stored in PTCR", patbsize);
14022adb61fSBruno Larsen (billionai)             return;
14122adb61fSBruno Larsen (billionai)         }
14222adb61fSBruno Larsen (billionai) 
14322adb61fSBruno Larsen (billionai)         env->spr[SPR_PTCR] = val;
144db70b311SRichard Henderson         tlb_flush(env_cpu(env));
1454a7518e0SCédric Le Goater     }
1464a7518e0SCédric Le Goater }
1476b375544SJoel Stanley 
1486b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value)
1496b375544SJoel Stanley {
150db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1516b375544SJoel Stanley     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1526b375544SJoel Stanley 
1536b375544SJoel Stanley     env->spr[SPR_PCR] = value & pcc->pcr_mask;
1546b375544SJoel Stanley }
1555ba7ba1dSCédric Le Goater 
1565ba7ba1dSCédric Le Goater /*
1575ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
1585ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
1595ba7ba1dSCédric Le Goater  */
1605ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
1615ba7ba1dSCédric Le Goater {
1625ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
1635ba7ba1dSCédric Le Goater 
164493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
165493028d8SCédric Le Goater 
1665ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
167f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1685ba7ba1dSCédric Le Goater         dpdes = 1;
1695ba7ba1dSCédric Le Goater     }
1705ba7ba1dSCédric Le Goater 
1715ba7ba1dSCédric Le Goater     return dpdes;
1725ba7ba1dSCédric Le Goater }
1735ba7ba1dSCédric Le Goater 
1745ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
1755ba7ba1dSCédric Le Goater {
1765ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
1775ba7ba1dSCédric Le Goater 
178493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
179493028d8SCédric Le Goater 
1805ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
1815ba7ba1dSCédric Le Goater     if (val & ~0x1) {
1825ba7ba1dSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value "
1835ba7ba1dSCédric Le Goater                       TARGET_FMT_lx"\n", val);
1845ba7ba1dSCédric Le Goater         return;
1855ba7ba1dSCédric Le Goater     }
1865ba7ba1dSCédric Le Goater 
1877b694df6SMatheus Ferst     ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
1885ba7ba1dSCédric Le Goater }
1894a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
1904a7518e0SCédric Le Goater 
19131b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
19231b2b0f8SSuraj Jitindar Singh {
193*fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
194db70b311SRichard Henderson     tlb_flush(env_cpu(env));
19531b2b0f8SSuraj Jitindar Singh }
19631b2b0f8SSuraj Jitindar Singh 
197c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
198c4dae9cdSBenjamin Herrenschmidt {
199*fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
200c4dae9cdSBenjamin Herrenschmidt 
201c4dae9cdSBenjamin Herrenschmidt     /*
202c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
203c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
204c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
205c4dae9cdSBenjamin Herrenschmidt      * well.
206c4dae9cdSBenjamin Herrenschmidt      */
207db70b311SRichard Henderson     tlb_flush(env_cpu(env));
208c4dae9cdSBenjamin Herrenschmidt }
209c4dae9cdSBenjamin Herrenschmidt 
210d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
211901c4eafSBlue Swirl {
2127da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
2137da31f26SRichard Henderson     hreg_compute_hflags(env);
2147da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
215901c4eafSBlue Swirl     store_40x_dbcr0(env, val);
216901c4eafSBlue Swirl }
217901c4eafSBlue Swirl 
218d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
219901c4eafSBlue Swirl {
220901c4eafSBlue Swirl     store_40x_sler(env, val);
221901c4eafSBlue Swirl }
222901c4eafSBlue Swirl #endif
2238555f71dSBlue Swirl 
2248555f71dSBlue Swirl /*****************************************************************************/
2258555f71dSBlue Swirl /* Special registers manipulation */
2268555f71dSBlue Swirl 
227d81b4327SDavid Gibson /*
228d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
229d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
230d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
231d81b4327SDavid Gibson  * processors.
232f0278900SBenjamin Herrenschmidt  */
233f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env)
234f0278900SBenjamin Herrenschmidt {
235f0278900SBenjamin Herrenschmidt     target_ulong v, t;
236f0278900SBenjamin Herrenschmidt     int i;
237f0278900SBenjamin Herrenschmidt 
238f0278900SBenjamin Herrenschmidt #define THRM1_TIN       (1 << 31)
239f0278900SBenjamin Herrenschmidt #define THRM1_TIV       (1 << 30)
240f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
241f0278900SBenjamin Herrenschmidt #define THRM1_TID       (1 << 2)
242f0278900SBenjamin Herrenschmidt #define THRM1_TIE       (1 << 1)
243f0278900SBenjamin Herrenschmidt #define THRM1_V         (1 << 0)
244f0278900SBenjamin Herrenschmidt #define THRM3_E         (1 << 0)
245f0278900SBenjamin Herrenschmidt 
246f0278900SBenjamin Herrenschmidt     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
247f0278900SBenjamin Herrenschmidt         return;
248f0278900SBenjamin Herrenschmidt     }
249f0278900SBenjamin Herrenschmidt 
250f0278900SBenjamin Herrenschmidt     /* Note: Thermal interrupts are unimplemented */
251f0278900SBenjamin Herrenschmidt     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
252f0278900SBenjamin Herrenschmidt         v = env->spr[i];
253f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_V)) {
254f0278900SBenjamin Herrenschmidt             continue;
255f0278900SBenjamin Herrenschmidt         }
256f0278900SBenjamin Herrenschmidt         v |= THRM1_TIV;
257f0278900SBenjamin Herrenschmidt         v &= ~THRM1_TIN;
258f0278900SBenjamin Herrenschmidt         t = v & THRM1_THRES(127);
259f0278900SBenjamin Herrenschmidt         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
260f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
261f0278900SBenjamin Herrenschmidt         }
262f0278900SBenjamin Herrenschmidt         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
263f0278900SBenjamin Herrenschmidt             v |= THRM1_TIN;
264f0278900SBenjamin Herrenschmidt         }
265f0278900SBenjamin Herrenschmidt         env->spr[i] = v;
266f0278900SBenjamin Herrenschmidt     }
267f0278900SBenjamin Herrenschmidt }
268