xref: /openbmc/qemu/target/ppc/misc_helper.c (revision c26504afd5f5cca1addfab5222621bc32a28522f)
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_core_write_generic(CPUPPCState *env, uint32_t sprn,
47                                    target_ulong val)
48 {
49     CPUState *cs = env_cpu(env);
50     CPUState *ccs;
51 
52     if (ppc_cpu_core_single_threaded(cs)) {
53         env->spr[sprn] = val;
54         return;
55     }
56 
57     THREAD_SIBLING_FOREACH(cs, ccs) {
58         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
59         cenv->spr[sprn] = val;
60     }
61 }
62 
63 void helper_spr_write_CTRL(CPUPPCState *env, uint32_t sprn,
64                            target_ulong val)
65 {
66     CPUState *cs = env_cpu(env);
67     CPUState *ccs;
68     uint32_t run = val & 1;
69     uint32_t ts, ts_mask;
70 
71     assert(sprn == SPR_CTRL);
72 
73     env->spr[sprn] &= ~1U;
74     env->spr[sprn] |= run;
75 
76     ts_mask = ~(1U << (8 + env->spr[SPR_TIR]));
77     ts = run << (8 + env->spr[SPR_TIR]);
78 
79     THREAD_SIBLING_FOREACH(cs, ccs) {
80         CPUPPCState *cenv = &POWERPC_CPU(ccs)->env;
81 
82         cenv->spr[sprn] &= ts_mask;
83         cenv->spr[sprn] |= ts;
84     }
85 }
86 
87 
88 #ifdef TARGET_PPC64
89 static void raise_hv_fu_exception(CPUPPCState *env, uint32_t bit,
90                                   const char *caller, uint32_t cause,
91                                   uintptr_t raddr)
92 {
93     qemu_log_mask(CPU_LOG_INT, "HV Facility %d is unavailable (%s)\n",
94                   bit, caller);
95 
96     env->spr[SPR_HFSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
97 
98     raise_exception_err_ra(env, POWERPC_EXCP_HV_FU, cause, raddr);
99 }
100 
101 static void raise_fu_exception(CPUPPCState *env, uint32_t bit,
102                                uint32_t sprn, uint32_t cause,
103                                uintptr_t raddr)
104 {
105     qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit);
106 
107     env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS);
108     cause &= FSCR_IC_MASK;
109     env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS;
110 
111     raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr);
112 }
113 #endif
114 
115 void helper_hfscr_facility_check(CPUPPCState *env, uint32_t bit,
116                                  const char *caller, uint32_t cause)
117 {
118 #ifdef TARGET_PPC64
119     if ((env->msr_mask & MSR_HVB) && !FIELD_EX64(env->msr, MSR, HV) &&
120                                      !(env->spr[SPR_HFSCR] & (1UL << bit))) {
121         raise_hv_fu_exception(env, bit, caller, cause, GETPC());
122     }
123 #endif
124 }
125 
126 void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit,
127                                 uint32_t sprn, uint32_t cause)
128 {
129 #ifdef TARGET_PPC64
130     if (env->spr[SPR_FSCR] & (1ULL << bit)) {
131         /* Facility is enabled, continue */
132         return;
133     }
134     raise_fu_exception(env, bit, sprn, cause, GETPC());
135 #endif
136 }
137 
138 void helper_msr_facility_check(CPUPPCState *env, uint32_t bit,
139                                uint32_t sprn, uint32_t cause)
140 {
141 #ifdef TARGET_PPC64
142     if (env->msr & (1ULL << bit)) {
143         /* Facility is enabled, continue */
144         return;
145     }
146     raise_fu_exception(env, bit, sprn, cause, GETPC());
147 #endif
148 }
149 
150 #if !defined(CONFIG_USER_ONLY)
151 
152 #ifdef TARGET_PPC64
153 static void helper_mmcr0_facility_check(CPUPPCState *env, uint32_t bit,
154                                  uint32_t sprn, uint32_t cause)
155 {
156     if (FIELD_EX64(env->msr, MSR, PR) &&
157         !(env->spr[SPR_POWER_MMCR0] & (1ULL << bit))) {
158         raise_fu_exception(env, bit, sprn, cause, GETPC());
159     }
160 }
161 #endif
162 
163 void helper_store_sdr1(CPUPPCState *env, target_ulong val)
164 {
165     if (env->spr[SPR_SDR1] != val) {
166         ppc_store_sdr1(env, val);
167         tlb_flush(env_cpu(env));
168     }
169 }
170 
171 #if defined(TARGET_PPC64)
172 void helper_store_ptcr(CPUPPCState *env, target_ulong val)
173 {
174     if (env->spr[SPR_PTCR] != val) {
175         CPUState *cs = env_cpu(env);
176         PowerPCCPU *cpu = env_archcpu(env);
177         target_ulong ptcr_mask = PTCR_PATB | PTCR_PATS;
178         target_ulong patbsize = val & PTCR_PATS;
179 
180         qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, val);
181 
182         assert(!cpu->vhyp);
183         assert(env->mmu_model & POWERPC_MMU_3_00);
184 
185         if (val & ~ptcr_mask) {
186             error_report("Invalid bits 0x"TARGET_FMT_lx" set in PTCR",
187                          val & ~ptcr_mask);
188             val &= ptcr_mask;
189         }
190 
191         if (patbsize > 24) {
192             error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
193                          " stored in PTCR", patbsize);
194             return;
195         }
196 
197         if (ppc_cpu_lpar_single_threaded(cs)) {
198             env->spr[SPR_PTCR] = val;
199             tlb_flush(cs);
200         } else {
201             CPUState *ccs;
202 
203             THREAD_SIBLING_FOREACH(cs, ccs) {
204                 PowerPCCPU *ccpu = POWERPC_CPU(ccs);
205                 CPUPPCState *cenv = &ccpu->env;
206                 cenv->spr[SPR_PTCR] = val;
207                 tlb_flush(ccs);
208             }
209         }
210     }
211 }
212 
213 void helper_store_pcr(CPUPPCState *env, target_ulong value)
214 {
215     PowerPCCPU *cpu = env_archcpu(env);
216     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
217 
218     env->spr[SPR_PCR] = value & pcc->pcr_mask;
219 }
220 
221 void helper_store_ciabr(CPUPPCState *env, target_ulong value)
222 {
223     ppc_store_ciabr(env, value);
224 }
225 
226 void helper_store_dawr0(CPUPPCState *env, target_ulong value)
227 {
228     ppc_store_dawr0(env, value);
229 }
230 
231 void helper_store_dawrx0(CPUPPCState *env, target_ulong value)
232 {
233     ppc_store_dawrx0(env, value);
234 }
235 
236 /*
237  * DPDES register is shared. Each bit reflects the state of the
238  * doorbell interrupt of a thread of the same core.
239  */
240 target_ulong helper_load_dpdes(CPUPPCState *env)
241 {
242     CPUState *cs = env_cpu(env);
243     CPUState *ccs;
244     target_ulong dpdes = 0;
245 
246     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
247 
248     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
249     if (ppc_cpu_lpar_single_threaded(cs)) {
250         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
251             dpdes = 1;
252         }
253         return dpdes;
254     }
255 
256     bql_lock();
257     THREAD_SIBLING_FOREACH(cs, ccs) {
258         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
259         CPUPPCState *cenv = &ccpu->env;
260         uint32_t thread_id = ppc_cpu_tir(ccpu);
261 
262         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
263             dpdes |= (0x1 << thread_id);
264         }
265     }
266     bql_unlock();
267 
268     return dpdes;
269 }
270 
271 void helper_store_dpdes(CPUPPCState *env, target_ulong val)
272 {
273     PowerPCCPU *cpu = env_archcpu(env);
274     CPUState *cs = env_cpu(env);
275     CPUState *ccs;
276 
277     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
278 
279     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
280     if (ppc_cpu_lpar_single_threaded(cs)) {
281         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
282         return;
283     }
284 
285     /* Does iothread need to be locked for walking CPU list? */
286     bql_lock();
287     THREAD_SIBLING_FOREACH(cs, ccs) {
288         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
289         uint32_t thread_id = ppc_cpu_tir(ccpu);
290 
291         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
292     }
293     bql_unlock();
294 }
295 
296 /*
297  * qemu-user breaks with pnv headers, so they go under ifdefs for now.
298  * A clean up may be to move powernv specific registers and helpers into
299  * target/ppc/pnv_helper.c
300  */
301 #include "hw/ppc/pnv_core.h"
302 
303 /* Indirect SCOM (SPRC/SPRD) access to SCRATCH0-7 are implemented. */
304 void helper_store_sprc(CPUPPCState *env, target_ulong val)
305 {
306     if (val & ~0x3f8ULL) {
307         qemu_log_mask(LOG_GUEST_ERROR, "Invalid SPRC register value "
308                       TARGET_FMT_lx"\n", val);
309         return;
310     }
311     env->spr[SPR_POWER_SPRC] = val;
312 }
313 
314 target_ulong helper_load_sprd(CPUPPCState *env)
315 {
316     /*
317      * SPRD is a HV-only register for Power CPUs, so this will only be
318      * accessed by powernv machines.
319      */
320     PowerPCCPU *cpu = env_archcpu(env);
321     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
322     target_ulong sprc = env->spr[SPR_POWER_SPRC];
323 
324     switch (sprc & 0x3e0) {
325     case 0: /* SCRATCH0-3 */
326     case 1: /* SCRATCH4-7 */
327         return pc->scratch[(sprc >> 3) & 0x7];
328     default:
329         qemu_log_mask(LOG_UNIMP, "mfSPRD: Unimplemented SPRC:0x"
330                                   TARGET_FMT_lx"\n", sprc);
331         break;
332     }
333     return 0;
334 }
335 
336 void helper_store_sprd(CPUPPCState *env, target_ulong val)
337 {
338     target_ulong sprc = env->spr[SPR_POWER_SPRC];
339     PowerPCCPU *cpu = env_archcpu(env);
340     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
341     int nr;
342 
343     switch (sprc & 0x3e0) {
344     case 0: /* SCRATCH0-3 */
345     case 1: /* SCRATCH4-7 */
346         /*
347          * Log stores to SCRATCH, because some firmware uses these for
348          * debugging and logging, but they would normally be read by the BMC,
349          * which is not implemented in QEMU yet. This gives a way to get at the
350          * information. Could also dump these upon checkstop.
351          */
352         nr = (sprc >> 3) & 0x7;
353         qemu_log("SPRD write 0x" TARGET_FMT_lx " to SCRATCH%d\n", val, nr);
354         pc->scratch[nr] = val;
355         break;
356     default:
357         qemu_log_mask(LOG_UNIMP, "mtSPRD: Unimplemented SPRC:0x"
358                                   TARGET_FMT_lx"\n", sprc);
359         break;
360     }
361 }
362 #endif /* defined(TARGET_PPC64) */
363 
364 void helper_store_pidr(CPUPPCState *env, target_ulong val)
365 {
366     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
367     tlb_flush(env_cpu(env));
368 }
369 
370 void helper_store_lpidr(CPUPPCState *env, target_ulong val)
371 {
372     env->spr[SPR_LPIDR] = (uint32_t)val;
373 
374     /*
375      * We need to flush the TLB on LPID changes as we only tag HV vs
376      * guest in TCG TLB. Also the quadrants means the HV will
377      * potentially access and cache entries for the current LPID as
378      * well.
379      */
380     tlb_flush(env_cpu(env));
381 }
382 
383 void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
384 {
385     /* Bits 26 & 27 affect single-stepping. */
386     hreg_compute_hflags(env);
387     /* Bits 28 & 29 affect reset or shutdown. */
388     store_40x_dbcr0(env, val);
389 }
390 
391 void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
392 {
393     store_40x_sler(env, val);
394 }
395 #endif
396 
397 /*****************************************************************************/
398 /* Special registers manipulation */
399 
400 /*
401  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
402  * or 3 is read an fixes up the values in such a way that will make
403  * MacOS not hang. These registers exist on some 75x and 74xx
404  * processors.
405  */
406 void helper_fixup_thrm(CPUPPCState *env)
407 {
408     target_ulong v, t;
409     int i;
410 
411 #define THRM1_TIN       (1 << 31)
412 #define THRM1_TIV       (1 << 30)
413 #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
414 #define THRM1_TID       (1 << 2)
415 #define THRM1_TIE       (1 << 1)
416 #define THRM1_V         (1 << 0)
417 #define THRM3_E         (1 << 0)
418 
419     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
420         return;
421     }
422 
423     /* Note: Thermal interrupts are unimplemented */
424     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
425         v = env->spr[i];
426         if (!(v & THRM1_V)) {
427             continue;
428         }
429         v |= THRM1_TIV;
430         v &= ~THRM1_TIN;
431         t = v & THRM1_THRES(127);
432         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
433             v |= THRM1_TIN;
434         }
435         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
436             v |= THRM1_TIN;
437         }
438         env->spr[i] = v;
439     }
440 }
441 
442 #if !defined(CONFIG_USER_ONLY)
443 #if defined(TARGET_PPC64)
444 void helper_clrbhrb(CPUPPCState *env)
445 {
446     helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB);
447 
448     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
449 
450     if (env->flags & POWERPC_FLAG_BHRB) {
451         memset(env->bhrb, 0, sizeof(env->bhrb));
452     }
453 }
454 
455 uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe)
456 {
457     unsigned int index;
458 
459     helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB);
460 
461     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
462 
463     if (!(env->flags & POWERPC_FLAG_BHRB) ||
464          (bhrbe >= env->bhrb_num_entries) ||
465          (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
466         return 0;
467     }
468 
469     /*
470      * Note: bhrb_offset is the byte offset for writing the
471      * next entry (over the oldest entry), which is why we
472      * must offset bhrbe by 1 to get to the 0th entry.
473      */
474     index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) %
475             env->bhrb_num_entries;
476     return env->bhrb[index];
477 }
478 #endif
479 #endif
480