xref: /openbmc/qemu/target/ppc/misc_helper.c (revision d5ee641cfc5c3cbd51282d0c6e996f990b9d62a3)
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 
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     uint32_t core_id = env->spr[SPR_PIR] & ~(nr_threads - 1);
539cdfd1b9SNicholas Piggin 
549cdfd1b9SNicholas Piggin     assert(core_id == env->spr[SPR_PIR] - env->spr[SPR_TIR]);
559cdfd1b9SNicholas Piggin 
569cdfd1b9SNicholas Piggin     if (nr_threads == 1) {
579cdfd1b9SNicholas Piggin         env->spr[sprn] = val;
589cdfd1b9SNicholas Piggin         return;
599cdfd1b9SNicholas Piggin     }
609cdfd1b9SNicholas Piggin 
619cdfd1b9SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
629cdfd1b9SNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
639cdfd1b9SNicholas Piggin         cenv->spr[sprn] = val;
649cdfd1b9SNicholas Piggin     }
659cdfd1b9SNicholas Piggin }
669cdfd1b9SNicholas Piggin 
67c5d98a7bSNicholas Piggin void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn,
68c5d98a7bSNicholas Piggin                            target_ulong val)
69c5d98a7bSNicholas Piggin {
70c5d98a7bSNicholas Piggin     CPUState *cs = env_cpu(env);
71c5d98a7bSNicholas Piggin     CPUState *ccs;
72c5d98a7bSNicholas Piggin     uint32_t run = val & 1;
73c5d98a7bSNicholas Piggin     uint32_t ts, ts_mask;
74c5d98a7bSNicholas Piggin 
75c5d98a7bSNicholas Piggin     assert(sprn == SPR_CTRL);
76c5d98a7bSNicholas Piggin 
77c5d98a7bSNicholas Piggin     env->spr[sprn] &= ~1U;
78c5d98a7bSNicholas Piggin     env->spr[sprn] |= run;
79c5d98a7bSNicholas Piggin 
80c5d98a7bSNicholas Piggin     ts_mask = ~(1U << (8 + env->spr[SPR_TIR]));
81c5d98a7bSNicholas Piggin     ts = run << (8 + env->spr[SPR_TIR]);
82c5d98a7bSNicholas Piggin 
83c5d98a7bSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
84c5d98a7bSNicholas Piggin         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
85c5d98a7bSNicholas Piggin 
86c5d98a7bSNicholas Piggin         cenv->spr[sprn] &= ts_mask;
87c5d98a7bSNicholas Piggin         cenv->spr[sprn] |= ts;
88c5d98a7bSNicholas Piggin     }
89c5d98a7bSNicholas Piggin }
90c5d98a7bSNicholas Piggin 
91c5d98a7bSNicholas Piggin 
92fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
93493028d8SCédric Le Goater static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
94493028d8SCédric Le Goater                                   const char *caller, uint32_t cause,
95493028d8SCédric Le Goater                                   uintptr_t raddr)
96493028d8SCédric Le Goater {
97493028d8SCédric Le Goater     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
98493028d8SCédric Le Goater                   bit, caller);
99493028d8SCédric Le Goater 
100493028d8SCédric Le Goater     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
101493028d8SCédric Le Goater 
102493028d8SCédric Le Goater     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
103493028d8SCédric Le Goater }
104493028d8SCédric Le Goater 
105fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
106fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause,
107fcf5ef2aSThomas Huth                                uintptr_t raddr)
108fcf5ef2aSThomas Huth {
109fcf5ef2aSThomas Huth     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
110fcf5ef2aSThomas Huth 
111fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
112fcf5ef2aSThomas Huth     cause &= FSCR_IC_MASK;
113fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
116fcf5ef2aSThomas Huth }
117fcf5ef2aSThomas Huth #endif
118fcf5ef2aSThomas Huth 
119493028d8SCédric Le Goater void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
120493028d8SCédric Le Goater                                  const char *caller, uint32_t cause)
121493028d8SCédric Le Goater {
122493028d8SCédric Le Goater #ifdef TARGET_PPC64
1239de754d3SVíctor Colombo     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
124493028d8SCédric Le Goater                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
125493028d8SCédric Le Goater         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
126493028d8SCédric Le Goater     }
127493028d8SCédric Le Goater #endif
128493028d8SCédric Le Goater }
129493028d8SCédric Le Goater 
130fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
131fcf5ef2aSThomas Huth                                 uint32_t sprn, uint32_t cause)
132fcf5ef2aSThomas Huth {
133fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
134fcf5ef2aSThomas Huth     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
135fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
136fcf5ef2aSThomas Huth         return;
137fcf5ef2aSThomas Huth     }
138fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
139fcf5ef2aSThomas Huth #endif
140fcf5ef2aSThomas Huth }
141fcf5ef2aSThomas Huth 
142fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
143fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause)
144fcf5ef2aSThomas Huth {
145fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
146fcf5ef2aSThomas Huth     if (env->msr & (1ULL << bit)) {
147fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
148fcf5ef2aSThomas Huth         return;
149fcf5ef2aSThomas Huth     }
150fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
151fcf5ef2aSThomas Huth #endif
152fcf5ef2aSThomas Huth }
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
155fcf5ef2aSThomas Huth 
156fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val)
157fcf5ef2aSThomas Huth {
158fcf5ef2aSThomas Huth     if (env->spr[SPR_SDR1] != val) {
159fcf5ef2aSThomas Huth         ppc_store_sdr1(env, val);
160db70b311SRichard Henderson         tlb_flush(env_cpu(env));
161fcf5ef2aSThomas Huth     }
162fcf5ef2aSThomas Huth }
163fcf5ef2aSThomas Huth 
1644a7518e0SCédric Le Goater #if defined(TARGET_PPC64)
1654a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val)
1664a7518e0SCédric Le Goater {
1674a7518e0SCédric Le Goater     if (env->spr[SPR_PTCR] != val) {
16822adb61fSBruno Larsen (billionai)         PowerPCCPU *cpu = env_archcpu(env);
16922adb61fSBruno Larsen (billionai)         target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS;
17022adb61fSBruno Larsen (billionai)         target_ulong patbsize = val & PTCR_PATS;
17122adb61fSBruno Larsen (billionai) 
17222adb61fSBruno Larsen (billionai)         qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val);
17322adb61fSBruno Larsen (billionai) 
17422adb61fSBruno Larsen (billionai)         assert(!cpu->vhyp);
17522adb61fSBruno Larsen (billionai)         assert(env->mmu_model & POWERPC_MMU_3_00);
17622adb61fSBruno Larsen (billionai) 
17722adb61fSBruno Larsen (billionai)         if (val & ~ptcr_mask) {
17822adb61fSBruno Larsen (billionai)             error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR",
17922adb61fSBruno Larsen (billionai)                          val & ~ptcr_mask);
18022adb61fSBruno Larsen (billionai)             val &= ptcr_mask;
18122adb61fSBruno Larsen (billionai)         }
18222adb61fSBruno Larsen (billionai) 
18322adb61fSBruno Larsen (billionai)         if (patbsize > 24) {
18422adb61fSBruno Larsen (billionai)             error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
18522adb61fSBruno Larsen (billionai)                          " stored in PTCR", patbsize);
18622adb61fSBruno Larsen (billionai)             return;
18722adb61fSBruno Larsen (billionai)         }
18822adb61fSBruno Larsen (billionai) 
18922adb61fSBruno Larsen (billionai)         env->spr[SPR_PTCR] = val;
190db70b311SRichard Henderson         tlb_flush(env_cpu(env));
1914a7518e0SCédric Le Goater     }
1924a7518e0SCédric Le Goater }
1936b375544SJoel Stanley 
1946b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value)
1956b375544SJoel Stanley {
196db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1976b375544SJoel Stanley     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1986b375544SJoel Stanley 
1996b375544SJoel Stanley     env->spr[SPR_PCR] = value & pcc->pcr_mask;
2006b375544SJoel Stanley }
2015ba7ba1dSCédric Le Goater 
20214192307SNicholas Piggin void helper_store_ciabr(CPUPPCState *env, target_ulong value)
20314192307SNicholas Piggin {
20414192307SNicholas Piggin     ppc_store_ciabr(env, value);
20514192307SNicholas Piggin }
20614192307SNicholas Piggin 
207*d5ee641cSNicholas Piggin void helper_store_dawr0(CPUPPCState *env, target_ulong value)
208*d5ee641cSNicholas Piggin {
209*d5ee641cSNicholas Piggin     ppc_store_dawr0(env, value);
210*d5ee641cSNicholas Piggin }
211*d5ee641cSNicholas Piggin 
212*d5ee641cSNicholas Piggin void helper_store_dawrx0(CPUPPCState *env, target_ulong value)
213*d5ee641cSNicholas Piggin {
214*d5ee641cSNicholas Piggin     ppc_store_dawrx0(env, value);
215*d5ee641cSNicholas Piggin }
216*d5ee641cSNicholas Piggin 
2175ba7ba1dSCédric Le Goater /*
2185ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
2195ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
2205ba7ba1dSCédric Le Goater  */
2215ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
2225ba7ba1dSCédric Le Goater {
223d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
224d24e80b2SNicholas Piggin     CPUState *ccs;
225d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
2265ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
2275ba7ba1dSCédric Le Goater 
228493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
229493028d8SCédric Le Goater 
2303401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
2313401ea3cSNicholas Piggin         nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */
2323401ea3cSNicholas Piggin     }
2333401ea3cSNicholas Piggin 
234d24e80b2SNicholas Piggin     if (nr_threads == 1) {
235f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2365ba7ba1dSCédric Le Goater             dpdes = 1;
2375ba7ba1dSCédric Le Goater         }
238d24e80b2SNicholas Piggin         return dpdes;
239d24e80b2SNicholas Piggin     }
240d24e80b2SNicholas Piggin 
241d24e80b2SNicholas Piggin     qemu_mutex_lock_iothread();
242d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
243d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
244d24e80b2SNicholas Piggin         CPUPPCState *cenv = &ccpu->env;
245d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
246d24e80b2SNicholas Piggin 
247d24e80b2SNicholas Piggin         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
248d24e80b2SNicholas Piggin             dpdes |= (0x1 << thread_id);
249d24e80b2SNicholas Piggin         }
250d24e80b2SNicholas Piggin     }
251d24e80b2SNicholas Piggin     qemu_mutex_unlock_iothread();
2525ba7ba1dSCédric Le Goater 
2535ba7ba1dSCédric Le Goater     return dpdes;
2545ba7ba1dSCédric Le Goater }
2555ba7ba1dSCédric Le Goater 
2565ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
2575ba7ba1dSCédric Le Goater {
2585ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
259d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
260d24e80b2SNicholas Piggin     CPUState *ccs;
261d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
2625ba7ba1dSCédric Le Goater 
263493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
264493028d8SCédric Le Goater 
2653401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
2663401ea3cSNicholas Piggin         nr_threads = 1; /* DPDES behaves as 1-thread in LPAR-per-thread mode */
2673401ea3cSNicholas Piggin     }
2683401ea3cSNicholas Piggin 
269d24e80b2SNicholas Piggin     if (val & ~(nr_threads - 1)) {
2705ba7ba1dSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value "
2715ba7ba1dSCédric Le Goater                       TARGET_FMT_lx"\n", val);
272d24e80b2SNicholas Piggin         val &= (nr_threads - 1); /* Ignore the invalid bits */
273d24e80b2SNicholas Piggin     }
274d24e80b2SNicholas Piggin 
275d24e80b2SNicholas Piggin     if (nr_threads == 1) {
276d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
2775ba7ba1dSCédric Le Goater         return;
2785ba7ba1dSCédric Le Goater     }
2795ba7ba1dSCédric Le Goater 
280d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
281d24e80b2SNicholas Piggin     qemu_mutex_lock_iothread();
282d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
283d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
284d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
285d24e80b2SNicholas Piggin 
286d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
287d24e80b2SNicholas Piggin     }
288d24e80b2SNicholas Piggin     qemu_mutex_unlock_iothread();
2895ba7ba1dSCédric Le Goater }
2904a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
2914a7518e0SCédric Le Goater 
29231b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
29331b2b0f8SSuraj Jitindar Singh {
294fbda88f7SNicholas Piggin     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
295db70b311SRichard Henderson     tlb_flush(env_cpu(env));
29631b2b0f8SSuraj Jitindar Singh }
29731b2b0f8SSuraj Jitindar Singh 
298c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
299c4dae9cdSBenjamin Herrenschmidt {
300fbda88f7SNicholas Piggin     env->spr[SPR_LPIDR] = (uint32_t)val;
301c4dae9cdSBenjamin Herrenschmidt 
302c4dae9cdSBenjamin Herrenschmidt     /*
303c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
304c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
305c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
306c4dae9cdSBenjamin Herrenschmidt      * well.
307c4dae9cdSBenjamin Herrenschmidt      */
308db70b311SRichard Henderson     tlb_flush(env_cpu(env));
309c4dae9cdSBenjamin Herrenschmidt }
310c4dae9cdSBenjamin Herrenschmidt 
311fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
312fcf5ef2aSThomas Huth {
3137da31f26SRichard Henderson     /* Bits 26 & 27 affect single-stepping. */
3147da31f26SRichard Henderson     hreg_compute_hflags(env);
3157da31f26SRichard Henderson     /* Bits 28 & 29 affect reset or shutdown. */
316fcf5ef2aSThomas Huth     store_40x_dbcr0(env, val);
317fcf5ef2aSThomas Huth }
318fcf5ef2aSThomas Huth 
319fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
320fcf5ef2aSThomas Huth {
321fcf5ef2aSThomas Huth     store_40x_sler(env, val);
322fcf5ef2aSThomas Huth }
323fcf5ef2aSThomas Huth #endif
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth /*****************************************************************************/
326fcf5ef2aSThomas Huth /* Special registers manipulation */
327fcf5ef2aSThomas Huth 
328d81b4327SDavid Gibson /*
329d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
330d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
331d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
332d81b4327SDavid Gibson  * processors.
333fcf5ef2aSThomas Huth  */
334fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env)
335fcf5ef2aSThomas Huth {
336fcf5ef2aSThomas Huth     target_ulong v, t;
337fcf5ef2aSThomas Huth     int i;
338fcf5ef2aSThomas Huth 
339fcf5ef2aSThomas Huth #define THRM1_TIN       (1 << 31)
340fcf5ef2aSThomas Huth #define THRM1_TIV       (1 << 30)
341fcf5ef2aSThomas Huth #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
342fcf5ef2aSThomas Huth #define THRM1_TID       (1 << 2)
343fcf5ef2aSThomas Huth #define THRM1_TIE       (1 << 1)
344fcf5ef2aSThomas Huth #define THRM1_V         (1 << 0)
345fcf5ef2aSThomas Huth #define THRM3_E         (1 << 0)
346fcf5ef2aSThomas Huth 
347fcf5ef2aSThomas Huth     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
348fcf5ef2aSThomas Huth         return;
349fcf5ef2aSThomas Huth     }
350fcf5ef2aSThomas Huth 
351fcf5ef2aSThomas Huth     /* Note: Thermal interrupts are unimplemented */
352fcf5ef2aSThomas Huth     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
353fcf5ef2aSThomas Huth         v = env->spr[i];
354fcf5ef2aSThomas Huth         if (!(v & THRM1_V)) {
355fcf5ef2aSThomas Huth             continue;
356fcf5ef2aSThomas Huth         }
357fcf5ef2aSThomas Huth         v |= THRM1_TIV;
358fcf5ef2aSThomas Huth         v &= ~THRM1_TIN;
359fcf5ef2aSThomas Huth         t = v & THRM1_THRES(127);
360fcf5ef2aSThomas Huth         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
361fcf5ef2aSThomas Huth             v |= THRM1_TIN;
362fcf5ef2aSThomas Huth         }
363fcf5ef2aSThomas Huth         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
364fcf5ef2aSThomas Huth             v |= THRM1_TIN;
365fcf5ef2aSThomas Huth         }
366fcf5ef2aSThomas Huth         env->spr[i] = v;
367fcf5ef2aSThomas Huth     }
368fcf5ef2aSThomas Huth }
369