xref: /openbmc/qemu/target/ppc/misc_helper.c (revision c5d98a7b3d455204e24212cb769dec8f490e4e1c)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * Miscellaneous PowerPC emulation helpers for QEMU.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2007 Jocelyn Mayer
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19db725815SMarkus Armbruster 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "exec/exec-all.h"
24fcf5ef2aSThomas Huth #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"
29fcf5ef2aSThomas Huth 
30fcf5ef2aSThomas Huth #include "helper_regs.h"
31fcf5ef2aSThomas Huth 
32fcf5ef2aSThomas Huth /*****************************************************************************/
33fcf5ef2aSThomas Huth /* SPR accesses */
34fcf5ef2aSThomas Huth void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn)
35fcf5ef2aSThomas Huth {
36fcf5ef2aSThomas Huth     qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn,
37fcf5ef2aSThomas Huth              env->spr[sprn]);
38fcf5ef2aSThomas Huth }
39fcf5ef2aSThomas Huth 
40fcf5ef2aSThomas Huth void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn)
41fcf5ef2aSThomas Huth {
42fcf5ef2aSThomas Huth     qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn,
43fcf5ef2aSThomas Huth              env->spr[sprn]);
44fcf5ef2aSThomas Huth }
45fcf5ef2aSThomas Huth 
46*c5d98a7bSNicholas Piggin void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn,
47*c5d98a7bSNicholas Piggin                            target_ulong val)
48*c5d98a7bSNicholas Piggin {
49*c5d98a7bSNicholas Piggin     CPUState *cs = env_cpu(env);
50*c5d98a7bSNicholas Piggin     CPUState *ccs;
51*c5d98a7bSNicholas Piggin     uint32_t run = val & 1;
52*c5d98a7bSNicholas Piggin     uint32_t ts, ts_mask;
53*c5d98a7bSNicholas Piggin 
54*c5d98a7bSNicholas Piggin     assert(sprn == SPR_CTRL);
55*c5d98a7bSNicholas Piggin 
56*c5d98a7bSNicholas Piggin     env->spr[sprn] &= ~1U;
57*c5d98a7bSNicholas Piggin     env->spr[sprn] |= run;
58*c5d98a7bSNicholas Piggin 
59*c5d98a7bSNicholas Piggin     ts_mask = ~(1U << (8 + env->spr[SPR_TIR]));
60*c5d98a7bSNicholas Piggin     ts = run << (8 + env->spr[SPR_TIR]);
61*c5d98a7bSNicholas Piggin 
62*c5d98a7bSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
63*c5d98a7bSNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
64*c5d98a7bSNicholas Piggin 
65*c5d98a7bSNicholas Piggin         cenv->spr[sprn] &= ts_mask;
66*c5d98a7bSNicholas Piggin         cenv->spr[sprn] |= ts;
67*c5d98a7bSNicholas Piggin     }
68*c5d98a7bSNicholas Piggin }
69*c5d98a7bSNicholas Piggin 
70*c5d98a7bSNicholas Piggin 
71fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
72493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
73493028d8SCédric Le Goater                                   const char *caller, uint32_t cause,
74493028d8SCédric Le Goater                                   uintptr_t raddr)
75493028d8SCédric Le Goater {
76493028d8SCédric Le Goater     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
77493028d8SCédric Le Goater                   bit, caller);
78493028d8SCédric Le Goater 
79493028d8SCédric Le Goater     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
80493028d8SCédric Le Goater 
81493028d8SCédric Le Goater     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
82493028d8SCédric Le Goater }
83493028d8SCédric Le Goater 
84fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
85fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause,
86fcf5ef2aSThomas Huth                                uintptr_t raddr)
87fcf5ef2aSThomas Huth {
88fcf5ef2aSThomas Huth     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
89fcf5ef2aSThomas Huth 
90fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
91fcf5ef2aSThomas Huth     cause &= FSCR_IC_MASK;
92fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
93fcf5ef2aSThomas Huth 
94fcf5ef2aSThomas Huth     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
95fcf5ef2aSThomas Huth }
96fcf5ef2aSThomas Huth #endif
97fcf5ef2aSThomas Huth 
98493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
99493028d8SCédric Le Goater                                  const char *caller, uint32_t cause)
100493028d8SCédric Le Goater {
101493028d8SCédric Le Goater #ifdef TARGET_PPC64
1029de754d3SVíctor Colombo     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
103493028d8SCédric Le Goater                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
104493028d8SCédric Le Goater         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
105493028d8SCédric Le Goater     }
106493028d8SCédric Le Goater #endif
107493028d8SCédric Le Goater }
108493028d8SCédric Le Goater 
109fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
110fcf5ef2aSThomas Huth                                 uint32_t sprn, uint32_t cause)
111fcf5ef2aSThomas Huth {
112fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
113fcf5ef2aSThomas Huth     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
114fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
115fcf5ef2aSThomas Huth         return;
116fcf5ef2aSThomas Huth     }
117fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
118fcf5ef2aSThomas Huth #endif
119fcf5ef2aSThomas Huth }
120fcf5ef2aSThomas Huth 
121fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
122fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause)
123fcf5ef2aSThomas Huth {
124fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
125fcf5ef2aSThomas Huth     if (env->msr & (1ULL << bit)) {
126fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
127fcf5ef2aSThomas Huth         return;
128fcf5ef2aSThomas Huth     }
129fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
130fcf5ef2aSThomas Huth #endif
131fcf5ef2aSThomas Huth }
132fcf5ef2aSThomas Huth 
133fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
134fcf5ef2aSThomas Huth 
135fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val)
136fcf5ef2aSThomas Huth {
137fcf5ef2aSThomas Huth     if (env->spr[SPR_SDR1] != val) {
138fcf5ef2aSThomas Huth         ppc_store_sdr1(env, val);
139db70b311SRichard Henderson         tlb_flush(env_cpu(env));
140fcf5ef2aSThomas Huth     }
141fcf5ef2aSThomas Huth }
142fcf5ef2aSThomas Huth 
1434a7518e0SCédric Le Goater #if defined(TARGET_PPC64)
1444a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val)
1454a7518e0SCédric Le Goater {
1464a7518e0SCédric Le Goater     if (env->spr[SPR_PTCR] != val) {
14722adb61fSBruno Larsen (billionai)         PowerPCCPU *cpu = env_archcpu(env);
14822adb61fSBruno Larsen (billionai)         target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS;
14922adb61fSBruno Larsen (billionai)         target_ulong patbsize = val & PTCR_PATS;
15022adb61fSBruno Larsen (billionai) 
15122adb61fSBruno Larsen (billionai)         qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val);
15222adb61fSBruno Larsen (billionai) 
15322adb61fSBruno Larsen (billionai)         assert(!cpu->vhyp);
15422adb61fSBruno Larsen (billionai)         assert(env->mmu_model & POWERPC_MMU_3_00);
15522adb61fSBruno Larsen (billionai) 
15622adb61fSBruno Larsen (billionai)         if (val & ~ptcr_mask) {
15722adb61fSBruno Larsen (billionai)             error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR",
15822adb61fSBruno Larsen (billionai)                          val & ~ptcr_mask);
15922adb61fSBruno Larsen (billionai)             val &= ptcr_mask;
16022adb61fSBruno Larsen (billionai)         }
16122adb61fSBruno Larsen (billionai) 
16222adb61fSBruno Larsen (billionai)         if (patbsize > 24) {
16322adb61fSBruno Larsen (billionai)             error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
16422adb61fSBruno Larsen (billionai)                          " stored in PTCR", patbsize);
16522adb61fSBruno Larsen (billionai)             return;
16622adb61fSBruno Larsen (billionai)         }
16722adb61fSBruno Larsen (billionai) 
16822adb61fSBruno Larsen (billionai)         env->spr[SPR_PTCR] = val;
169db70b311SRichard Henderson         tlb_flush(env_cpu(env));
1704a7518e0SCédric Le Goater     }
1714a7518e0SCédric Le Goater }
1726b375544SJoel Stanley 
1736b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value)
1746b375544SJoel Stanley {
175db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1766b375544SJoel Stanley     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1776b375544SJoel Stanley 
1786b375544SJoel Stanley     env->spr[SPR_PCR] = value & pcc->pcr_mask;
1796b375544SJoel Stanley }
1805ba7ba1dSCédric Le Goater 
1815ba7ba1dSCédric Le Goater /*
1825ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
1835ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
1845ba7ba1dSCédric Le Goater  */
1855ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
1865ba7ba1dSCédric Le Goater {
1875ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
1885ba7ba1dSCédric Le Goater 
189493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
190493028d8SCédric Le Goater 
1915ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
192f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1935ba7ba1dSCédric Le Goater         dpdes = 1;
1945ba7ba1dSCédric Le Goater     }
1955ba7ba1dSCédric Le Goater 
1965ba7ba1dSCédric Le Goater     return dpdes;
1975ba7ba1dSCédric Le Goater }
1985ba7ba1dSCédric Le Goater 
1995ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
2005ba7ba1dSCédric Le Goater {
2015ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
2025ba7ba1dSCédric Le Goater 
203493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
204493028d8SCédric Le Goater 
2055ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
2065ba7ba1dSCédric Le Goater     if (val & ~0x1) {
2075ba7ba1dSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value "
2085ba7ba1dSCédric Le Goater                       TARGET_FMT_lx"\n", val);
2095ba7ba1dSCédric Le Goater         return;
2105ba7ba1dSCédric Le Goater     }
2115ba7ba1dSCédric Le Goater 
2127b694df6SMatheus Ferst     ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
2135ba7ba1dSCédric Le Goater }
2144a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
2154a7518e0SCédric Le Goater 
21631b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
21731b2b0f8SSuraj Jitindar Singh {
218fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
219db70b311SRichard Henderson     tlb_flush(env_cpu(env));
22031b2b0f8SSuraj Jitindar Singh }
22131b2b0f8SSuraj Jitindar Singh 
222c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
223c4dae9cdSBenjamin Herrenschmidt {
224fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
225c4dae9cdSBenjamin Herrenschmidt 
226c4dae9cdSBenjamin Herrenschmidt     /*
227c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
228c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
229c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
230c4dae9cdSBenjamin Herrenschmidt      * well.
231c4dae9cdSBenjamin Herrenschmidt      */
232db70b311SRichard Henderson     tlb_flush(env_cpu(env));
233c4dae9cdSBenjamin Herrenschmidt }
234c4dae9cdSBenjamin Herrenschmidt 
235fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
236fcf5ef2aSThomas Huth {
2377da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
2387da31f26SRichard Henderson     hreg_compute_hflags(env);
2397da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
240fcf5ef2aSThomas Huth     store_40x_dbcr0(env, val);
241fcf5ef2aSThomas Huth }
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
244fcf5ef2aSThomas Huth {
245fcf5ef2aSThomas Huth     store_40x_sler(env, val);
246fcf5ef2aSThomas Huth }
247fcf5ef2aSThomas Huth #endif
248fcf5ef2aSThomas Huth 
249fcf5ef2aSThomas Huth /*****************************************************************************/
250fcf5ef2aSThomas Huth /* Special registers manipulation */
251fcf5ef2aSThomas Huth 
252d81b4327SDavid Gibson /*
253d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
254d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
255d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
256d81b4327SDavid Gibson  * processors.
257fcf5ef2aSThomas Huth  */
258fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth     target_ulong v, t;
261fcf5ef2aSThomas Huth     int i;
262fcf5ef2aSThomas Huth 
263fcf5ef2aSThomas Huth #define THRM1_TIN       (1 << 31)
264fcf5ef2aSThomas Huth #define THRM1_TIV       (1 << 30)
265fcf5ef2aSThomas Huth #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
266fcf5ef2aSThomas Huth #define THRM1_TID       (1 << 2)
267fcf5ef2aSThomas Huth #define THRM1_TIE       (1 << 1)
268fcf5ef2aSThomas Huth #define THRM1_V         (1 << 0)
269fcf5ef2aSThomas Huth #define THRM3_E         (1 << 0)
270fcf5ef2aSThomas Huth 
271fcf5ef2aSThomas Huth     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
272fcf5ef2aSThomas Huth         return;
273fcf5ef2aSThomas Huth     }
274fcf5ef2aSThomas Huth 
275fcf5ef2aSThomas Huth     /* Note: Thermal interrupts are unimplemented */
276fcf5ef2aSThomas Huth     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
277fcf5ef2aSThomas Huth         v = env->spr[i];
278fcf5ef2aSThomas Huth         if (!(v & THRM1_V)) {
279fcf5ef2aSThomas Huth             continue;
280fcf5ef2aSThomas Huth         }
281fcf5ef2aSThomas Huth         v |= THRM1_TIV;
282fcf5ef2aSThomas Huth         v &= ~THRM1_TIN;
283fcf5ef2aSThomas Huth         t = v & THRM1_THRES(127);
284fcf5ef2aSThomas Huth         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
285fcf5ef2aSThomas Huth             v |= THRM1_TIN;
286fcf5ef2aSThomas Huth         }
287fcf5ef2aSThomas Huth         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
288fcf5ef2aSThomas Huth             v |= THRM1_TIN;
289fcf5ef2aSThomas Huth         }
290fcf5ef2aSThomas Huth         env->spr[i] = v;
291fcf5ef2aSThomas Huth     }
292fcf5ef2aSThomas Huth }
293