xref: /openbmc/qemu/target/ppc/misc_helper.c (revision fbda88f7abdeed3ceebdd18de6909a52df756c1c)
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 
46fcf5ef2aSThomas Huth #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 
59fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
60fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause,
61fcf5ef2aSThomas Huth                                uintptr_t raddr)
62fcf5ef2aSThomas Huth {
63fcf5ef2aSThomas Huth     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
64fcf5ef2aSThomas Huth 
65fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
66fcf5ef2aSThomas Huth     cause &= FSCR_IC_MASK;
67fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
68fcf5ef2aSThomas Huth 
69fcf5ef2aSThomas Huth     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
70fcf5ef2aSThomas Huth }
71fcf5ef2aSThomas Huth #endif
72fcf5ef2aSThomas Huth 
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 
84fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
85fcf5ef2aSThomas Huth                                 uint32_t sprn, uint32_t cause)
86fcf5ef2aSThomas Huth {
87fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
88fcf5ef2aSThomas Huth     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
89fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
90fcf5ef2aSThomas Huth         return;
91fcf5ef2aSThomas Huth     }
92fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
93fcf5ef2aSThomas Huth #endif
94fcf5ef2aSThomas Huth }
95fcf5ef2aSThomas Huth 
96fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
97fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause)
98fcf5ef2aSThomas Huth {
99fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
100fcf5ef2aSThomas Huth     if (env->msr & (1ULL << bit)) {
101fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
102fcf5ef2aSThomas Huth         return;
103fcf5ef2aSThomas Huth     }
104fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
105fcf5ef2aSThomas Huth #endif
106fcf5ef2aSThomas Huth }
107fcf5ef2aSThomas Huth 
108fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
109fcf5ef2aSThomas Huth 
110fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val)
111fcf5ef2aSThomas Huth {
112fcf5ef2aSThomas Huth     if (env->spr[SPR_SDR1] != val) {
113fcf5ef2aSThomas Huth         ppc_store_sdr1(env, val);
114db70b311SRichard Henderson         tlb_flush(env_cpu(env));
115fcf5ef2aSThomas Huth     }
116fcf5ef2aSThomas Huth }
117fcf5ef2aSThomas Huth 
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 
210fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
211fcf5ef2aSThomas Huth {
2127da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
2137da31f26SRichard Henderson     hreg_compute_hflags(env);
2147da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
215fcf5ef2aSThomas Huth     store_40x_dbcr0(env, val);
216fcf5ef2aSThomas Huth }
217fcf5ef2aSThomas Huth 
218fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
219fcf5ef2aSThomas Huth {
220fcf5ef2aSThomas Huth     store_40x_sler(env, val);
221fcf5ef2aSThomas Huth }
222fcf5ef2aSThomas Huth #endif
223fcf5ef2aSThomas Huth 
224fcf5ef2aSThomas Huth /*****************************************************************************/
225fcf5ef2aSThomas Huth /* Special registers manipulation */
226fcf5ef2aSThomas Huth 
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.
232fcf5ef2aSThomas Huth  */
233fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env)
234fcf5ef2aSThomas Huth {
235fcf5ef2aSThomas Huth     target_ulong v, t;
236fcf5ef2aSThomas Huth     int i;
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth #define THRM1_TIN       (1 << 31)
239fcf5ef2aSThomas Huth #define THRM1_TIV       (1 << 30)
240fcf5ef2aSThomas Huth #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
241fcf5ef2aSThomas Huth #define THRM1_TID       (1 << 2)
242fcf5ef2aSThomas Huth #define THRM1_TIE       (1 << 1)
243fcf5ef2aSThomas Huth #define THRM1_V         (1 << 0)
244fcf5ef2aSThomas Huth #define THRM3_E         (1 << 0)
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
247fcf5ef2aSThomas Huth         return;
248fcf5ef2aSThomas Huth     }
249fcf5ef2aSThomas Huth 
250fcf5ef2aSThomas Huth     /* Note: Thermal interrupts are unimplemented */
251fcf5ef2aSThomas Huth     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
252fcf5ef2aSThomas Huth         v = env->spr[i];
253fcf5ef2aSThomas Huth         if (!(v & THRM1_V)) {
254fcf5ef2aSThomas Huth             continue;
255fcf5ef2aSThomas Huth         }
256fcf5ef2aSThomas Huth         v |= THRM1_TIV;
257fcf5ef2aSThomas Huth         v &= ~THRM1_TIN;
258fcf5ef2aSThomas Huth         t = v & THRM1_THRES(127);
259fcf5ef2aSThomas Huth         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
260fcf5ef2aSThomas Huth             v |= THRM1_TIN;
261fcf5ef2aSThomas Huth         }
262fcf5ef2aSThomas Huth         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
263fcf5ef2aSThomas Huth             v |= THRM1_TIN;
264fcf5ef2aSThomas Huth         }
265fcf5ef2aSThomas Huth         env->spr[i] = v;
266fcf5ef2aSThomas Huth     }
267fcf5ef2aSThomas Huth }
268