xref: /openbmc/qemu/target/ppc/misc_helper.c (revision 1b65b4f5)
1 /*
2  * Miscellaneous PowerPC emulation helpers for QEMU.
3  *
4  *  Copyright (c) 2003-2007 Jocelyn Mayer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/log.h"
22 #include "cpu.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/error-report.h"
26 #include "qemu/main-loop.h"
27 #include "mmu-book3s-v3.h"
28 #include "hw/ppc/ppc.h"
29 
30 #include "helper_regs.h"
31 
32 /*****************************************************************************/
33 /* SPR accesses */
34 void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn)
35 {
36     qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn,
37              env->spr[sprn]);
38 }
39 
40 void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn)
41 {
42     qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn,
43              env->spr[sprn]);
44 }
45 
46 void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn,
47                            target_ulong val)
48 {
49     CPUState *cs = env_cpu(env);
50     CPUState *ccs;
51     uint32_t run = val & 1;
52     uint32_t ts, ts_mask;
53 
54     assert(sprn == SPR_CTRL);
55 
56     env->spr[sprn] &= ~1U;
57     env->spr[sprn] |= run;
58 
59     ts_mask = ~(1U << (8 + env->spr[SPR_TIR]));
60     ts = run << (8 + env->spr[SPR_TIR]);
61 
62     THREAD_SIBLING_FOREACH(cs, ccs) {
63         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
64 
65         cenv->spr[sprn] &= ts_mask;
66         cenv->spr[sprn] |= ts;
67     }
68 }
69 
70 
71 #ifdef TARGET_PPC64
72 static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
73                                   const char *caller, uint32_t cause,
74                                   uintptr_t raddr)
75 {
76     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
77                   bit, caller);
78 
79     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
80 
81     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
82 }
83 
84 static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
85                                uint32_t sprn, uint32_t cause,
86                                uintptr_t raddr)
87 {
88     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
89 
90     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
91     cause &= FSCR_IC_MASK;
92     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
93 
94     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
95 }
96 #endif
97 
98 void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
99                                  const char *caller, uint32_t cause)
100 {
101 #ifdef TARGET_PPC64
102     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
103                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
104         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
105     }
106 #endif
107 }
108 
109 void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
110                                 uint32_t sprn, uint32_t cause)
111 {
112 #ifdef TARGET_PPC64
113     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
114         /* Facility is enabled, continue */
115         return;
116     }
117     raise_fu_exception(env, bit, sprn, cause, GETPC());
118 #endif
119 }
120 
121 void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
122                                uint32_t sprn, uint32_t cause)
123 {
124 #ifdef TARGET_PPC64
125     if (env->msr & (1ULL << bit)) {
126         /* Facility is enabled, continue */
127         return;
128     }
129     raise_fu_exception(env, bit, sprn, cause, GETPC());
130 #endif
131 }
132 
133 #if !defined(CONFIG_USER_ONLY)
134 
135 void helper_store_sdr1(CPUPPCState *env, target_ulong val)
136 {
137     if (env->spr[SPR_SDR1] != val) {
138         ppc_store_sdr1(env, val);
139         tlb_flush(env_cpu(env));
140     }
141 }
142 
143 #if defined(TARGET_PPC64)
144 void helper_store_ptcr(CPUPPCState *env, target_ulong val)
145 {
146     if (env->spr[SPR_PTCR] != val) {
147         PowerPCCPU *cpu = env_archcpu(env);
148         target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS;
149         target_ulong patbsize = val & PTCR_PATS;
150 
151         qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val);
152 
153         assert(!cpu->vhyp);
154         assert(env->mmu_model & POWERPC_MMU_3_00);
155 
156         if (val & ~ptcr_mask) {
157             error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR",
158                          val & ~ptcr_mask);
159             val &= ptcr_mask;
160         }
161 
162         if (patbsize > 24) {
163             error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
164                          " stored in PTCR", patbsize);
165             return;
166         }
167 
168         env->spr[SPR_PTCR] = val;
169         tlb_flush(env_cpu(env));
170     }
171 }
172 
173 void helper_store_pcr(CPUPPCState *env, target_ulong value)
174 {
175     PowerPCCPU *cpu = env_archcpu(env);
176     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
177 
178     env->spr[SPR_PCR] = value & pcc->pcr_mask;
179 }
180 
181 /*
182  * DPDES register is shared. Each bit reflects the state of the
183  * doorbell interrupt of a thread of the same core.
184  */
185 target_ulong helper_load_dpdes(CPUPPCState *env)
186 {
187     CPUState *cs = env_cpu(env);
188     CPUState *ccs;
189     uint32_t nr_threads = cs->nr_threads;
190     target_ulong dpdes = 0;
191 
192     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
193 
194     if (nr_threads == 1) {
195         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
196             dpdes = 1;
197         }
198         return dpdes;
199     }
200 
201     qemu_mutex_lock_iothread();
202     THREAD_SIBLING_FOREACH(cs, ccs) {
203         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
204         CPUPPCState *cenv = &ccpu->env;
205         uint32_t thread_id = ppc_cpu_tir(ccpu);
206 
207         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
208             dpdes |= (0x1 << thread_id);
209         }
210     }
211     qemu_mutex_unlock_iothread();
212 
213     return dpdes;
214 }
215 
216 void helper_store_dpdes(CPUPPCState *env, target_ulong val)
217 {
218     PowerPCCPU *cpu = env_archcpu(env);
219     CPUState *cs = env_cpu(env);
220     CPUState *ccs;
221     uint32_t nr_threads = cs->nr_threads;
222 
223     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
224 
225     if (val & ~(nr_threads - 1)) {
226         qemu_log_mask(LOG_GUEST_ERROR, "Invalid DPDES register value "
227                       TARGET_FMT_lx"\n", val);
228         val &= (nr_threads - 1); /* Ignore the invalid bits */
229     }
230 
231     if (nr_threads == 1) {
232         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
233         return;
234     }
235 
236     /* Does iothread need to be locked for walking CPU list? */
237     qemu_mutex_lock_iothread();
238     THREAD_SIBLING_FOREACH(cs, ccs) {
239         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
240         uint32_t thread_id = ppc_cpu_tir(ccpu);
241 
242         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
243     }
244     qemu_mutex_unlock_iothread();
245 }
246 #endif /* defined(TARGET_PPC64) */
247 
248 void helper_store_pidr(CPUPPCState *env, target_ulong val)
249 {
250     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
251     tlb_flush(env_cpu(env));
252 }
253 
254 void helper_store_lpidr(CPUPPCState *env, target_ulong val)
255 {
256     env->spr[SPR_LPIDR] = (uint32_t)val;
257 
258     /*
259      * We need to flush the TLB on LPID changes as we only tag HV vs
260      * guest in TCG TLB. Also the quadrants means the HV will
261      * potentially access and cache entries for the current LPID as
262      * well.
263      */
264     tlb_flush(env_cpu(env));
265 }
266 
267 void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
268 {
269     /* Bits 26 & 27 affect single-stepping. */
270     hreg_compute_hflags(env);
271     /* Bits 28 & 29 affect reset or shutdown. */
272     store_40x_dbcr0(env, val);
273 }
274 
275 void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
276 {
277     store_40x_sler(env, val);
278 }
279 #endif
280 
281 /*****************************************************************************/
282 /* Special registers manipulation */
283 
284 /*
285  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
286  * or 3 is read an fixes up the values in such a way that will make
287  * MacOS not hang. These registers exist on some 75x and 74xx
288  * processors.
289  */
290 void helper_fixup_thrm(CPUPPCState *env)
291 {
292     target_ulong v, t;
293     int i;
294 
295 #define THRM1_TIN       (1 << 31)
296 #define THRM1_TIV       (1 << 30)
297 #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
298 #define THRM1_TID       (1 << 2)
299 #define THRM1_TIE       (1 << 1)
300 #define THRM1_V         (1 << 0)
301 #define THRM3_E         (1 << 0)
302 
303     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
304         return;
305     }
306 
307     /* Note: Thermal interrupts are unimplemented */
308     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
309         v = env->spr[i];
310         if (!(v & THRM1_V)) {
311             continue;
312         }
313         v |= THRM1_TIV;
314         v &= ~THRM1_TIN;
315         t = v & THRM1_THRES(127);
316         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
317             v |= THRM1_TIN;
318         }
319         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
320             v |= THRM1_TIN;
321         }
322         env->spr[i] = v;
323     }
324 }
325