xref: /openbmc/qemu/target/ppc/misc_helper.c (revision 4a7518e0fdaa20525730ae0709a4afa0960a6c67)
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  */
19fcf5ef2aSThomas Huth #include "qemu/osdep.h"
20fcf5ef2aSThomas Huth #include "cpu.h"
21fcf5ef2aSThomas Huth #include "exec/exec-all.h"
22fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
23fcf5ef2aSThomas Huth 
24fcf5ef2aSThomas Huth #include "helper_regs.h"
25fcf5ef2aSThomas Huth 
26fcf5ef2aSThomas Huth /*****************************************************************************/
27fcf5ef2aSThomas Huth /* SPR accesses */
28fcf5ef2aSThomas Huth void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn)
29fcf5ef2aSThomas Huth {
30fcf5ef2aSThomas Huth     qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn,
31fcf5ef2aSThomas Huth              env->spr[sprn]);
32fcf5ef2aSThomas Huth }
33fcf5ef2aSThomas Huth 
34fcf5ef2aSThomas Huth void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn)
35fcf5ef2aSThomas Huth {
36fcf5ef2aSThomas Huth     qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn,
37fcf5ef2aSThomas Huth              env->spr[sprn]);
38fcf5ef2aSThomas Huth }
39fcf5ef2aSThomas Huth 
40fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
41fcf5ef2aSThomas Huth static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
42fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause,
43fcf5ef2aSThomas Huth                                uintptr_t raddr)
44fcf5ef2aSThomas Huth {
45fcf5ef2aSThomas Huth     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
46fcf5ef2aSThomas Huth 
47fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
48fcf5ef2aSThomas Huth     cause &= FSCR_IC_MASK;
49fcf5ef2aSThomas Huth     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
52fcf5ef2aSThomas Huth }
53fcf5ef2aSThomas Huth #endif
54fcf5ef2aSThomas Huth 
55fcf5ef2aSThomas Huth void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
56fcf5ef2aSThomas Huth                                 uint32_t sprn, uint32_t cause)
57fcf5ef2aSThomas Huth {
58fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
59fcf5ef2aSThomas Huth     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
60fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
61fcf5ef2aSThomas Huth         return;
62fcf5ef2aSThomas Huth     }
63fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
64fcf5ef2aSThomas Huth #endif
65fcf5ef2aSThomas Huth }
66fcf5ef2aSThomas Huth 
67fcf5ef2aSThomas Huth void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
68fcf5ef2aSThomas Huth                                uint32_t sprn, uint32_t cause)
69fcf5ef2aSThomas Huth {
70fcf5ef2aSThomas Huth #ifdef TARGET_PPC64
71fcf5ef2aSThomas Huth     if (env->msr & (1ULL << bit)) {
72fcf5ef2aSThomas Huth         /* Facility is enabled, continue */
73fcf5ef2aSThomas Huth         return;
74fcf5ef2aSThomas Huth     }
75fcf5ef2aSThomas Huth     raise_fu_exception(env, bit, sprn, cause, GETPC());
76fcf5ef2aSThomas Huth #endif
77fcf5ef2aSThomas Huth }
78fcf5ef2aSThomas Huth 
79fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
80fcf5ef2aSThomas Huth 
81fcf5ef2aSThomas Huth void helper_store_sdr1(CPUPPCState *env, target_ulong val)
82fcf5ef2aSThomas Huth {
83fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
84fcf5ef2aSThomas Huth 
85fcf5ef2aSThomas Huth     if (env->spr[SPR_SDR1] != val) {
86fcf5ef2aSThomas Huth         ppc_store_sdr1(env, val);
87d10eb08fSAlex Bennée         tlb_flush(CPU(cpu));
88fcf5ef2aSThomas Huth     }
89fcf5ef2aSThomas Huth }
90fcf5ef2aSThomas Huth 
91*4a7518e0SCédric Le Goater #if defined(TARGET_PPC64)
92*4a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val)
93*4a7518e0SCédric Le Goater {
94*4a7518e0SCédric Le Goater     PowerPCCPU *cpu = ppc_env_get_cpu(env);
95*4a7518e0SCédric Le Goater 
96*4a7518e0SCédric Le Goater     if (env->spr[SPR_PTCR] != val) {
97*4a7518e0SCédric Le Goater         ppc_store_ptcr(env, val);
98*4a7518e0SCédric Le Goater         tlb_flush(CPU(cpu));
99*4a7518e0SCédric Le Goater     }
100*4a7518e0SCédric Le Goater }
101*4a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */
102*4a7518e0SCédric Le Goater 
10331b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val)
10431b2b0f8SSuraj Jitindar Singh {
10531b2b0f8SSuraj Jitindar Singh     PowerPCCPU *cpu = ppc_env_get_cpu(env);
10631b2b0f8SSuraj Jitindar Singh 
10731b2b0f8SSuraj Jitindar Singh     env->spr[SPR_BOOKS_PID] = val;
10831b2b0f8SSuraj Jitindar Singh     tlb_flush(CPU(cpu));
10931b2b0f8SSuraj Jitindar Singh }
11031b2b0f8SSuraj Jitindar Singh 
111fcf5ef2aSThomas Huth void helper_store_hid0_601(CPUPPCState *env, target_ulong val)
112fcf5ef2aSThomas Huth {
113fcf5ef2aSThomas Huth     target_ulong hid0;
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth     hid0 = env->spr[SPR_HID0];
116fcf5ef2aSThomas Huth     if ((val ^ hid0) & 0x00000008) {
117fcf5ef2aSThomas Huth         /* Change current endianness */
118fcf5ef2aSThomas Huth         env->hflags &= ~(1 << MSR_LE);
119fcf5ef2aSThomas Huth         env->hflags_nmsr &= ~(1 << MSR_LE);
120fcf5ef2aSThomas Huth         env->hflags_nmsr |= (1 << MSR_LE) & (((val >> 3) & 1) << MSR_LE);
121fcf5ef2aSThomas Huth         env->hflags |= env->hflags_nmsr;
122fcf5ef2aSThomas Huth         qemu_log("%s: set endianness to %c => " TARGET_FMT_lx "\n", __func__,
123fcf5ef2aSThomas Huth                  val & 0x8 ? 'l' : 'b', env->hflags);
124fcf5ef2aSThomas Huth     }
125fcf5ef2aSThomas Huth     env->spr[SPR_HID0] = (uint32_t)val;
126fcf5ef2aSThomas Huth }
127fcf5ef2aSThomas Huth 
128fcf5ef2aSThomas Huth void helper_store_403_pbr(CPUPPCState *env, uint32_t num, target_ulong value)
129fcf5ef2aSThomas Huth {
130fcf5ef2aSThomas Huth     PowerPCCPU *cpu = ppc_env_get_cpu(env);
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth     if (likely(env->pb[num] != value)) {
133fcf5ef2aSThomas Huth         env->pb[num] = value;
134fcf5ef2aSThomas Huth         /* Should be optimized */
135d10eb08fSAlex Bennée         tlb_flush(CPU(cpu));
136fcf5ef2aSThomas Huth     }
137fcf5ef2aSThomas Huth }
138fcf5ef2aSThomas Huth 
139fcf5ef2aSThomas Huth void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
140fcf5ef2aSThomas Huth {
141fcf5ef2aSThomas Huth     store_40x_dbcr0(env, val);
142fcf5ef2aSThomas Huth }
143fcf5ef2aSThomas Huth 
144fcf5ef2aSThomas Huth void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
145fcf5ef2aSThomas Huth {
146fcf5ef2aSThomas Huth     store_40x_sler(env, val);
147fcf5ef2aSThomas Huth }
148fcf5ef2aSThomas Huth #endif
149fcf5ef2aSThomas Huth /*****************************************************************************/
150fcf5ef2aSThomas Huth /* PowerPC 601 specific instructions (POWER bridge) */
151fcf5ef2aSThomas Huth 
152fcf5ef2aSThomas Huth target_ulong helper_clcs(CPUPPCState *env, uint32_t arg)
153fcf5ef2aSThomas Huth {
154fcf5ef2aSThomas Huth     switch (arg) {
155fcf5ef2aSThomas Huth     case 0x0CUL:
156fcf5ef2aSThomas Huth         /* Instruction cache line size */
157fcf5ef2aSThomas Huth         return env->icache_line_size;
158fcf5ef2aSThomas Huth         break;
159fcf5ef2aSThomas Huth     case 0x0DUL:
160fcf5ef2aSThomas Huth         /* Data cache line size */
161fcf5ef2aSThomas Huth         return env->dcache_line_size;
162fcf5ef2aSThomas Huth         break;
163fcf5ef2aSThomas Huth     case 0x0EUL:
164fcf5ef2aSThomas Huth         /* Minimum cache line size */
165fcf5ef2aSThomas Huth         return (env->icache_line_size < env->dcache_line_size) ?
166fcf5ef2aSThomas Huth             env->icache_line_size : env->dcache_line_size;
167fcf5ef2aSThomas Huth         break;
168fcf5ef2aSThomas Huth     case 0x0FUL:
169fcf5ef2aSThomas Huth         /* Maximum cache line size */
170fcf5ef2aSThomas Huth         return (env->icache_line_size > env->dcache_line_size) ?
171fcf5ef2aSThomas Huth             env->icache_line_size : env->dcache_line_size;
172fcf5ef2aSThomas Huth         break;
173fcf5ef2aSThomas Huth     default:
174fcf5ef2aSThomas Huth         /* Undefined */
175fcf5ef2aSThomas Huth         return 0;
176fcf5ef2aSThomas Huth         break;
177fcf5ef2aSThomas Huth     }
178fcf5ef2aSThomas Huth }
179fcf5ef2aSThomas Huth 
180fcf5ef2aSThomas Huth /*****************************************************************************/
181fcf5ef2aSThomas Huth /* Special registers manipulation */
182fcf5ef2aSThomas Huth 
183fcf5ef2aSThomas Huth /* GDBstub can read and write MSR... */
184fcf5ef2aSThomas Huth void ppc_store_msr(CPUPPCState *env, target_ulong value)
185fcf5ef2aSThomas Huth {
186fcf5ef2aSThomas Huth     hreg_store_msr(env, value, 0);
187fcf5ef2aSThomas Huth }
188fcf5ef2aSThomas Huth 
189fcf5ef2aSThomas Huth /* This code is lifted from MacOnLinux. It is called whenever
190fcf5ef2aSThomas Huth  * THRM1,2 or 3 is read an fixes up the values in such a way
191fcf5ef2aSThomas Huth  * that will make MacOS not hang. These registers exist on some
192fcf5ef2aSThomas Huth  * 75x and 74xx processors.
193fcf5ef2aSThomas Huth  */
194fcf5ef2aSThomas Huth void helper_fixup_thrm(CPUPPCState *env)
195fcf5ef2aSThomas Huth {
196fcf5ef2aSThomas Huth     target_ulong v, t;
197fcf5ef2aSThomas Huth     int i;
198fcf5ef2aSThomas Huth 
199fcf5ef2aSThomas Huth #define THRM1_TIN       (1 << 31)
200fcf5ef2aSThomas Huth #define THRM1_TIV       (1 << 30)
201fcf5ef2aSThomas Huth #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
202fcf5ef2aSThomas Huth #define THRM1_TID       (1 << 2)
203fcf5ef2aSThomas Huth #define THRM1_TIE       (1 << 1)
204fcf5ef2aSThomas Huth #define THRM1_V         (1 << 0)
205fcf5ef2aSThomas Huth #define THRM3_E         (1 << 0)
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
208fcf5ef2aSThomas Huth         return;
209fcf5ef2aSThomas Huth     }
210fcf5ef2aSThomas Huth 
211fcf5ef2aSThomas Huth     /* Note: Thermal interrupts are unimplemented */
212fcf5ef2aSThomas Huth     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
213fcf5ef2aSThomas Huth         v = env->spr[i];
214fcf5ef2aSThomas Huth         if (!(v & THRM1_V)) {
215fcf5ef2aSThomas Huth             continue;
216fcf5ef2aSThomas Huth         }
217fcf5ef2aSThomas Huth         v |= THRM1_TIV;
218fcf5ef2aSThomas Huth         v &= ~THRM1_TIN;
219fcf5ef2aSThomas Huth         t = v & THRM1_THRES(127);
220fcf5ef2aSThomas Huth         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
221fcf5ef2aSThomas Huth             v |= THRM1_TIN;
222fcf5ef2aSThomas Huth         }
223fcf5ef2aSThomas Huth         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
224fcf5ef2aSThomas Huth             v |= THRM1_TIN;
225fcf5ef2aSThomas Huth         }
226fcf5ef2aSThomas Huth         env->spr[i] = v;
227fcf5ef2aSThomas Huth     }
228fcf5ef2aSThomas Huth }
229