xref: /openbmc/qemu/target/ppc/misc_helper.c (revision 5ba7ba1da096de0b70f65c08df5584a4878012e7)
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
9fcf5ef2aSThomas Huth  * version 2 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"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "exec/exec-all.h"
23fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
246b375544SJoel Stanley #include "qemu/error-report.h"
25db725815SMarkus Armbruster #include "qemu/main-loop.h"
26fcf5ef2aSThomas Huth 
27fcf5ef2aSThomas Huth #include "helper_regs.h"
28fcf5ef2aSThomas Huth 
29fcf5ef2aSThomas Huth /*****************************************************************************/
30fcf5ef2aSThomas Huth /* SPR accesses */
31fcf5ef2aSThomas Huth void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn)
32fcf5ef2aSThomas Huth {
33fcf5ef2aSThomas Huth     qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn,
34fcf5ef2aSThomas Huth              env->spr[sprn]);
35fcf5ef2aSThomas Huth }
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn)
38fcf5ef2aSThomas Huth {
39fcf5ef2aSThomas Huth     qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn,
40fcf5ef2aSThomas Huth              env->spr[sprn]);
41fcf5ef2aSThomas Huth }
42fcf5ef2aSThomas Huth 
43fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
44fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
45fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause,
46fcf5ef2aSThomas Huth                                uintptr_t raddr)
47fcf5ef2aSThomas Huth {
48fcf5ef2aSThomas Huth     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
49fcf5ef2aSThomas Huth 
50fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
51fcf5ef2aSThomas Huth     cause &= FSCR_IC_MASK;
52fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
53fcf5ef2aSThomas Huth 
54fcf5ef2aSThomas Huth     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
55fcf5ef2aSThomas Huth }
56fcf5ef2aSThomas Huth #endif
57fcf5ef2aSThomas Huth 
58fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
59fcf5ef2aSThomas Huth                                 uint32_t sprn, uint32_t cause)
60fcf5ef2aSThomas Huth {
61fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
62fcf5ef2aSThomas Huth     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
63fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
64fcf5ef2aSThomas Huth         return;
65fcf5ef2aSThomas Huth     }
66fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
67fcf5ef2aSThomas Huth #endif
68fcf5ef2aSThomas Huth }
69fcf5ef2aSThomas Huth 
70fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
71fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause)
72fcf5ef2aSThomas Huth {
73fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
74fcf5ef2aSThomas Huth     if (env->msr & (1ULL << bit)) {
75fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
76fcf5ef2aSThomas Huth         return;
77fcf5ef2aSThomas Huth     }
78fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
79fcf5ef2aSThomas Huth #endif
80fcf5ef2aSThomas Huth }
81fcf5ef2aSThomas Huth 
82fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val)
85fcf5ef2aSThomas Huth {
86fcf5ef2aSThomas Huth     if (env->spr[SPR_SDR1] != val) {
87fcf5ef2aSThomas Huth         ppc_store_sdr1(env, val);
88db70b311SRichard Henderson         tlb_flush(env_cpu(env));
89fcf5ef2aSThomas Huth     }
90fcf5ef2aSThomas Huth }
91fcf5ef2aSThomas Huth 
924a7518e0SCédric Le Goater #if defined(TARGET_PPC64)
934a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val)
944a7518e0SCédric Le Goater {
954a7518e0SCédric Le Goater     if (env->spr[SPR_PTCR] != val) {
964a7518e0SCédric Le Goater         ppc_store_ptcr(env, val);
97db70b311SRichard Henderson         tlb_flush(env_cpu(env));
984a7518e0SCédric Le Goater     }
994a7518e0SCédric Le Goater }
1006b375544SJoel Stanley 
1016b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value)
1026b375544SJoel Stanley {
103db70b311SRichard Henderson     PowerPCCPU *cpu = env_archcpu(env);
1046b375544SJoel Stanley     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1056b375544SJoel Stanley 
1066b375544SJoel Stanley     env->spr[SPR_PCR] = value & pcc->pcr_mask;
1076b375544SJoel Stanley }
108*5ba7ba1dSCédric Le Goater 
109*5ba7ba1dSCédric Le Goater /*
110*5ba7ba1dSCédric Le Goater  * DPDES register is shared. Each bit reflects the state of the
111*5ba7ba1dSCédric Le Goater  * doorbell interrupt of a thread of the same core.
112*5ba7ba1dSCédric Le Goater  */
113*5ba7ba1dSCédric Le Goater target_ulong helper_load_dpdes(CPUPPCState *env)
114*5ba7ba1dSCédric Le Goater {
115*5ba7ba1dSCédric Le Goater     target_ulong dpdes = 0;
116*5ba7ba1dSCédric Le Goater 
117*5ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
118*5ba7ba1dSCédric Le Goater     if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
119*5ba7ba1dSCédric Le Goater         dpdes = 1;
120*5ba7ba1dSCédric Le Goater     }
121*5ba7ba1dSCédric Le Goater 
122*5ba7ba1dSCédric Le Goater     return dpdes;
123*5ba7ba1dSCédric Le Goater }
124*5ba7ba1dSCédric Le Goater 
125*5ba7ba1dSCédric Le Goater void helper_store_dpdes(CPUPPCState *env, target_ulong val)
126*5ba7ba1dSCédric Le Goater {
127*5ba7ba1dSCédric Le Goater     PowerPCCPU *cpu = env_archcpu(env);
128*5ba7ba1dSCédric Le Goater     CPUState *cs = CPU(cpu);
129*5ba7ba1dSCédric Le Goater 
130*5ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
131*5ba7ba1dSCédric Le Goater     if (val & ~0x1) {
132*5ba7ba1dSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value "
133*5ba7ba1dSCédric Le Goater                       TARGET_FMT_lx"\n", val);
134*5ba7ba1dSCédric Le Goater         return;
135*5ba7ba1dSCédric Le Goater     }
136*5ba7ba1dSCédric Le Goater 
137*5ba7ba1dSCédric Le Goater     if (val & 0x1) {
138*5ba7ba1dSCédric Le Goater         env->pending_interrupts |= 1 << PPC_INTERRUPT_DOORBELL;
139*5ba7ba1dSCédric Le Goater         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
140*5ba7ba1dSCédric Le Goater     } else {
141*5ba7ba1dSCédric Le Goater         env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
142*5ba7ba1dSCédric Le Goater     }
143*5ba7ba1dSCédric Le Goater }
1444a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
1454a7518e0SCédric Le Goater 
14631b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
14731b2b0f8SSuraj Jitindar Singh {
14831b2b0f8SSuraj Jitindar Singh     env->spr[SPR_BOOKS_PID] = val;
149db70b311SRichard Henderson     tlb_flush(env_cpu(env));
15031b2b0f8SSuraj Jitindar Singh }
15131b2b0f8SSuraj Jitindar Singh 
152c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val)
153c4dae9cdSBenjamin Herrenschmidt {
154c4dae9cdSBenjamin Herrenschmidt     env->spr[SPR_LPIDR] = val;
155c4dae9cdSBenjamin Herrenschmidt 
156c4dae9cdSBenjamin Herrenschmidt     /*
157c4dae9cdSBenjamin Herrenschmidt      * We need to flush the TLB on LPID changes as we only tag HV vs
158c4dae9cdSBenjamin Herrenschmidt      * guest in TCG TLB. Also the quadrants means the HV will
159c4dae9cdSBenjamin Herrenschmidt      * potentially access and cache entries for the current LPID as
160c4dae9cdSBenjamin Herrenschmidt      * well.
161c4dae9cdSBenjamin Herrenschmidt      */
162db70b311SRichard Henderson     tlb_flush(env_cpu(env));
163c4dae9cdSBenjamin Herrenschmidt }
164c4dae9cdSBenjamin Herrenschmidt 
165fcf5ef2aSThomas Huth void helper_store_hid0_601(CPUPPCState *env, target_ulong val)
166fcf5ef2aSThomas Huth {
167fcf5ef2aSThomas Huth     target_ulong hid0;
168fcf5ef2aSThomas Huth 
169fcf5ef2aSThomas Huth     hid0 = env->spr[SPR_HID0];
170fcf5ef2aSThomas Huth     if ((val ^ hid0) & 0x00000008) {
171fcf5ef2aSThomas Huth         /* Change current endianness */
172fcf5ef2aSThomas Huth         env->hflags &= ~(1 << MSR_LE);
173fcf5ef2aSThomas Huth         env->hflags_nmsr &= ~(1 << MSR_LE);
174fcf5ef2aSThomas Huth         env->hflags_nmsr |= (1 << MSR_LE) & (((val >> 3) & 1) << MSR_LE);
175fcf5ef2aSThomas Huth         env->hflags |= env->hflags_nmsr;
176fcf5ef2aSThomas Huth         qemu_log("%s: set endianness to %c => " TARGET_FMT_lx "\n", __func__,
177fcf5ef2aSThomas Huth                  val & 0x8 ? 'l' : 'b', env->hflags);
178fcf5ef2aSThomas Huth     }
179fcf5ef2aSThomas Huth     env->spr[SPR_HID0] = (uint32_t)val;
180fcf5ef2aSThomas Huth }
181fcf5ef2aSThomas Huth 
182fcf5ef2aSThomas Huth void helper_store_403_pbr(CPUPPCState *env, uint32_t num, target_ulong value)
183fcf5ef2aSThomas Huth {
184fcf5ef2aSThomas Huth     if (likely(env->pb[num] != value)) {
185fcf5ef2aSThomas Huth         env->pb[num] = value;
186fcf5ef2aSThomas Huth         /* Should be optimized */
187db70b311SRichard Henderson         tlb_flush(env_cpu(env));
188fcf5ef2aSThomas Huth     }
189fcf5ef2aSThomas Huth }
190fcf5ef2aSThomas Huth 
191fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
192fcf5ef2aSThomas Huth {
193fcf5ef2aSThomas Huth     store_40x_dbcr0(env, val);
194fcf5ef2aSThomas Huth }
195fcf5ef2aSThomas Huth 
196fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
197fcf5ef2aSThomas Huth {
198fcf5ef2aSThomas Huth     store_40x_sler(env, val);
199fcf5ef2aSThomas Huth }
200fcf5ef2aSThomas Huth #endif
201fcf5ef2aSThomas Huth /*****************************************************************************/
202fcf5ef2aSThomas Huth /* PowerPC 601 specific instructions (POWER bridge) */
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth target_ulong helper_clcs(CPUPPCState *env, uint32_t arg)
205fcf5ef2aSThomas Huth {
206fcf5ef2aSThomas Huth     switch (arg) {
207fcf5ef2aSThomas Huth     case 0x0CUL:
208fcf5ef2aSThomas Huth         /* Instruction cache line size */
209fcf5ef2aSThomas Huth         return env->icache_line_size;
210fcf5ef2aSThomas Huth         break;
211fcf5ef2aSThomas Huth     case 0x0DUL:
212fcf5ef2aSThomas Huth         /* Data cache line size */
213fcf5ef2aSThomas Huth         return env->dcache_line_size;
214fcf5ef2aSThomas Huth         break;
215fcf5ef2aSThomas Huth     case 0x0EUL:
216fcf5ef2aSThomas Huth         /* Minimum cache line size */
217fcf5ef2aSThomas Huth         return (env->icache_line_size < env->dcache_line_size) ?
218fcf5ef2aSThomas Huth             env->icache_line_size : env->dcache_line_size;
219fcf5ef2aSThomas Huth         break;
220fcf5ef2aSThomas Huth     case 0x0FUL:
221fcf5ef2aSThomas Huth         /* Maximum cache line size */
222fcf5ef2aSThomas Huth         return (env->icache_line_size > env->dcache_line_size) ?
223fcf5ef2aSThomas Huth             env->icache_line_size : env->dcache_line_size;
224fcf5ef2aSThomas Huth         break;
225fcf5ef2aSThomas Huth     default:
226fcf5ef2aSThomas Huth         /* Undefined */
227fcf5ef2aSThomas Huth         return 0;
228fcf5ef2aSThomas Huth         break;
229fcf5ef2aSThomas Huth     }
230fcf5ef2aSThomas Huth }
231fcf5ef2aSThomas Huth 
232fcf5ef2aSThomas Huth /*****************************************************************************/
233fcf5ef2aSThomas Huth /* Special registers manipulation */
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth /* GDBstub can read and write MSR... */
236fcf5ef2aSThomas Huth void ppc_store_msr(CPUPPCState *env, target_ulong value)
237fcf5ef2aSThomas Huth {
238fcf5ef2aSThomas Huth     hreg_store_msr(env, value, 0);
239fcf5ef2aSThomas Huth }
240fcf5ef2aSThomas Huth 
241d81b4327SDavid Gibson /*
242d81b4327SDavid Gibson  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
243d81b4327SDavid Gibson  * or 3 is read an fixes up the values in such a way that will make
244d81b4327SDavid Gibson  * MacOS not hang. These registers exist on some 75x and 74xx
245d81b4327SDavid Gibson  * processors.
246fcf5ef2aSThomas Huth  */
247fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env)
248fcf5ef2aSThomas Huth {
249fcf5ef2aSThomas Huth     target_ulong v, t;
250fcf5ef2aSThomas Huth     int i;
251fcf5ef2aSThomas Huth 
252fcf5ef2aSThomas Huth #define THRM1_TIN       (1 << 31)
253fcf5ef2aSThomas Huth #define THRM1_TIV       (1 << 30)
254fcf5ef2aSThomas Huth #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
255fcf5ef2aSThomas Huth #define THRM1_TID       (1 << 2)
256fcf5ef2aSThomas Huth #define THRM1_TIE       (1 << 1)
257fcf5ef2aSThomas Huth #define THRM1_V         (1 << 0)
258fcf5ef2aSThomas Huth #define THRM3_E         (1 << 0)
259fcf5ef2aSThomas Huth 
260fcf5ef2aSThomas Huth     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
261fcf5ef2aSThomas Huth         return;
262fcf5ef2aSThomas Huth     }
263fcf5ef2aSThomas Huth 
264fcf5ef2aSThomas Huth     /* Note: Thermal interrupts are unimplemented */
265fcf5ef2aSThomas Huth     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
266fcf5ef2aSThomas Huth         v = env->spr[i];
267fcf5ef2aSThomas Huth         if (!(v & THRM1_V)) {
268fcf5ef2aSThomas Huth             continue;
269fcf5ef2aSThomas Huth         }
270fcf5ef2aSThomas Huth         v |= THRM1_TIV;
271fcf5ef2aSThomas Huth         v &= ~THRM1_TIN;
272fcf5ef2aSThomas Huth         t = v & THRM1_THRES(127);
273fcf5ef2aSThomas Huth         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
274fcf5ef2aSThomas Huth             v |= THRM1_TIN;
275fcf5ef2aSThomas Huth         }
276fcf5ef2aSThomas Huth         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
277fcf5ef2aSThomas Huth             v |= THRM1_TIN;
278fcf5ef2aSThomas Huth         }
279fcf5ef2aSThomas Huth         env->spr[i] = v;
280fcf5ef2aSThomas Huth     }
281fcf5ef2aSThomas Huth }
282