xref: /qemu/target/ppc/misc_helper.c (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
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/cputlb.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 */
helper_load_dump_spr(CPUPPCState * env,uint32_t sprn)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 
helper_store_dump_spr(CPUPPCState * env,uint32_t sprn)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 
helper_spr_core_write_generic(CPUPPCState * env,uint32_t sprn,target_ulong val)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 
helper_spr_write_CTRL(CPUPPCState * env,uint32_t sprn,target_ulong val)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
raise_hv_fu_exception(CPUPPCState * env,uint32_t bit,const char * caller,uint32_t cause,uintptr_t raddr)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 
raise_fu_exception(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause,uintptr_t raddr)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 
helper_hfscr_facility_check(CPUPPCState * env,uint32_t bit,const char * caller,uint32_t cause)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 
helper_fscr_facility_check(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause)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 
helper_msr_facility_check(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause)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
helper_mmcr0_facility_check(CPUPPCState * env,uint32_t bit,uint32_t sprn,uint32_t cause)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 
helper_store_sdr1(CPUPPCState * env,target_ulong val)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)
helper_store_ptcr(CPUPPCState * env,target_ulong val)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 
helper_store_pcr(CPUPPCState * env,target_ulong value)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 
helper_store_ciabr(CPUPPCState * env,target_ulong value)221 void helper_store_ciabr(CPUPPCState *env, target_ulong value)
222 {
223     ppc_store_ciabr(env, value);
224 }
225 
helper_store_dawr0(CPUPPCState * env,target_ulong value)226 void helper_store_dawr0(CPUPPCState *env, target_ulong value)
227 {
228     ppc_store_dawr0(env, value);
229 }
230 
helper_store_dawrx0(CPUPPCState * env,target_ulong value)231 void helper_store_dawrx0(CPUPPCState *env, target_ulong value)
232 {
233     ppc_store_dawrx0(env, value);
234 }
235 
helper_store_dawr1(CPUPPCState * env,target_ulong value)236 void helper_store_dawr1(CPUPPCState *env, target_ulong value)
237 {
238     ppc_store_dawr1(env, value);
239 }
240 
helper_store_dawrx1(CPUPPCState * env,target_ulong value)241 void helper_store_dawrx1(CPUPPCState *env, target_ulong value)
242 {
243     ppc_store_dawrx1(env, value);
244 }
245 
246 /*
247  * DPDES register is shared. Each bit reflects the state of the
248  * doorbell interrupt of a thread of the same core.
249  */
helper_load_dpdes(CPUPPCState * env)250 target_ulong helper_load_dpdes(CPUPPCState *env)
251 {
252     CPUState *cs = env_cpu(env);
253     CPUState *ccs;
254     target_ulong dpdes = 0;
255 
256     helper_hfscr_facility_check(env, HFSCR_MSGP, "load DPDES", HFSCR_IC_MSGP);
257 
258     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
259     if (ppc_cpu_lpar_single_threaded(cs)) {
260         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
261             dpdes = 1;
262         }
263         return dpdes;
264     }
265 
266     bql_lock();
267     THREAD_SIBLING_FOREACH(cs, ccs) {
268         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
269         CPUPPCState *cenv = &ccpu->env;
270         uint32_t thread_id = ppc_cpu_tir(ccpu);
271 
272         if (cenv->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
273             dpdes |= (0x1 << thread_id);
274         }
275     }
276     bql_unlock();
277 
278     return dpdes;
279 }
280 
helper_store_dpdes(CPUPPCState * env,target_ulong val)281 void helper_store_dpdes(CPUPPCState *env, target_ulong val)
282 {
283     PowerPCCPU *cpu = env_archcpu(env);
284     CPUState *cs = env_cpu(env);
285     CPUState *ccs;
286 
287     helper_hfscr_facility_check(env, HFSCR_MSGP, "store DPDES", HFSCR_IC_MSGP);
288 
289     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
290     if (ppc_cpu_lpar_single_threaded(cs)) {
291         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, val & 0x1);
292         return;
293     }
294 
295     /* Does iothread need to be locked for walking CPU list? */
296     bql_lock();
297     THREAD_SIBLING_FOREACH(cs, ccs) {
298         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
299         uint32_t thread_id = ppc_cpu_tir(ccpu);
300 
301         ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, val & (0x1 << thread_id));
302     }
303     bql_unlock();
304 }
305 
306 /*
307  * qemu-user breaks with pnv headers, so they go under ifdefs for now.
308  * A clean up may be to move powernv specific registers and helpers into
309  * target/ppc/pnv_helper.c
310  */
311 #include "hw/ppc/pnv_core.h"
312 
313 /* Indirect SCOM (SPRC/SPRD) access to SCRATCH0-7 are implemented. */
helper_store_sprc(CPUPPCState * env,target_ulong val)314 void helper_store_sprc(CPUPPCState *env, target_ulong val)
315 {
316     if (val & ~0x3f8ULL) {
317         qemu_log_mask(LOG_GUEST_ERROR, "Invalid SPRC register value "
318                       TARGET_FMT_lx"\n", val);
319         return;
320     }
321     env->spr[SPR_POWER_SPRC] = val;
322 }
323 
helper_load_sprd(CPUPPCState * env)324 target_ulong helper_load_sprd(CPUPPCState *env)
325 {
326     /*
327      * SPRD is a HV-only register for Power CPUs, so this will only be
328      * accessed by powernv machines.
329      */
330     PowerPCCPU *cpu = env_archcpu(env);
331     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
332     target_ulong sprc = env->spr[SPR_POWER_SPRC];
333 
334     if (pc->big_core) {
335         pc = pnv_chip_find_core(pc->chip, CPU_CORE(pc)->core_id & ~0x1);
336     }
337 
338     switch (sprc & 0x3e0) {
339     case 0: /* SCRATCH0-3 */
340     case 1: /* SCRATCH4-7 */
341         return pc->scratch[(sprc >> 3) & 0x7];
342 
343     case 0x1e0: /* core thread state */
344         if (env->excp_model == POWERPC_EXCP_POWER9) {
345             /*
346              * Only implement for POWER9 because skiboot uses it to check
347              * big-core mode. Other bits are unimplemented so we would
348              * prefer to get unimplemented message on POWER10 if it were
349              * used anywhere.
350              */
351             if (pc->big_core) {
352                 return PPC_BIT(63);
353             } else {
354                 return 0;
355             }
356         }
357         /* fallthru */
358 
359     default:
360         qemu_log_mask(LOG_UNIMP, "mfSPRD: Unimplemented SPRC:0x"
361                                   TARGET_FMT_lx"\n", sprc);
362         break;
363     }
364     return 0;
365 }
366 
helper_store_sprd(CPUPPCState * env,target_ulong val)367 void helper_store_sprd(CPUPPCState *env, target_ulong val)
368 {
369     target_ulong sprc = env->spr[SPR_POWER_SPRC];
370     PowerPCCPU *cpu = env_archcpu(env);
371     PnvCore *pc = pnv_cpu_state(cpu)->pnv_core;
372     int nr;
373 
374     if (pc->big_core) {
375         pc = pnv_chip_find_core(pc->chip, CPU_CORE(pc)->core_id & ~0x1);
376     }
377 
378     switch (sprc & 0x3e0) {
379     case 0: /* SCRATCH0-3 */
380     case 1: /* SCRATCH4-7 */
381         /*
382          * Log stores to SCRATCH, because some firmware uses these for
383          * debugging and logging, but they would normally be read by the BMC,
384          * which is not implemented in QEMU yet. This gives a way to get at the
385          * information. Could also dump these upon checkstop.
386          */
387         nr = (sprc >> 3) & 0x7;
388         pc->scratch[nr] = val;
389         break;
390     default:
391         qemu_log_mask(LOG_UNIMP, "mtSPRD: Unimplemented SPRC:0x"
392                                   TARGET_FMT_lx"\n", sprc);
393         break;
394     }
395 }
396 
helper_load_pmsr(CPUPPCState * env)397 target_ulong helper_load_pmsr(CPUPPCState *env)
398 {
399     target_ulong lowerps = extract64(env->spr[SPR_PMCR], PPC_BIT_NR(15), 8);
400     target_ulong val = 0;
401 
402     val |= PPC_BIT(63); /* verion 0x1 (POWER9/10) */
403     /* Pmin = 0 */
404     /* XXX: POWER9 should be 3 */
405     val |= 4ULL << PPC_BIT_NR(31); /* Pmax */
406     val |= lowerps << PPC_BIT_NR(15); /* Local actual Pstate */
407     val |= lowerps << PPC_BIT_NR(7); /* Global actual Pstate */
408 
409     return val;
410 }
411 
ppc_set_pmcr(PowerPCCPU * cpu,target_ulong val)412 static void ppc_set_pmcr(PowerPCCPU *cpu, target_ulong val)
413 {
414     cpu->env.spr[SPR_PMCR] = val;
415 }
416 
helper_store_pmcr(CPUPPCState * env,target_ulong val)417 void helper_store_pmcr(CPUPPCState *env, target_ulong val)
418 {
419     PowerPCCPU *cpu = env_archcpu(env);
420     CPUState *cs = env_cpu(env);
421     CPUState *ccs;
422 
423     /* Leave version field unchanged (0x1) */
424     val &= ~PPC_BITMASK(60, 63);
425     val |= PPC_BIT(63);
426 
427     val &= ~PPC_BITMASK(0, 7); /* UpperPS ignored */
428     if (val & PPC_BITMASK(16, 59)) {
429         qemu_log_mask(LOG_GUEST_ERROR, "Non-zero PMCR reserved bits "
430                       TARGET_FMT_lx"\n", val);
431         val &= ~PPC_BITMASK(16, 59);
432     }
433 
434     /* DPDES behaves as 1-thread in LPAR-per-thread mode */
435     if (ppc_cpu_lpar_single_threaded(cs)) {
436         ppc_set_pmcr(cpu, val);
437         return;
438     }
439 
440     /* Does iothread need to be locked for walking CPU list? */
441     bql_lock();
442     THREAD_SIBLING_FOREACH(cs, ccs) {
443         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
444         ppc_set_pmcr(ccpu, val);
445     }
446     bql_unlock();
447 }
448 
449 #endif /* defined(TARGET_PPC64) */
450 
helper_store_pidr(CPUPPCState * env,target_ulong val)451 void helper_store_pidr(CPUPPCState *env, target_ulong val)
452 {
453     env->spr[SPR_BOOKS_PID] = (uint32_t)val;
454     tlb_flush(env_cpu(env));
455 }
456 
helper_store_lpidr(CPUPPCState * env,target_ulong val)457 void helper_store_lpidr(CPUPPCState *env, target_ulong val)
458 {
459     env->spr[SPR_LPIDR] = (uint32_t)val;
460 
461     /*
462      * We need to flush the TLB on LPID changes as we only tag HV vs
463      * guest in TCG TLB. Also the quadrants means the HV will
464      * potentially access and cache entries for the current LPID as
465      * well.
466      */
467     tlb_flush(env_cpu(env));
468 }
469 
helper_store_40x_dbcr0(CPUPPCState * env,target_ulong val)470 void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val)
471 {
472     /* Bits 26 & 27 affect single-stepping. */
473     hreg_compute_hflags(env);
474     /* Bits 28 & 29 affect reset or shutdown. */
475     store_40x_dbcr0(env, val);
476 }
477 
helper_store_40x_sler(CPUPPCState * env,target_ulong val)478 void helper_store_40x_sler(CPUPPCState *env, target_ulong val)
479 {
480     store_40x_sler(env, val);
481 }
482 #endif
483 
484 /*****************************************************************************/
485 /* Special registers manipulation */
486 
487 /*
488  * This code is lifted from MacOnLinux. It is called whenever THRM1,2
489  * or 3 is read an fixes up the values in such a way that will make
490  * MacOS not hang. These registers exist on some 75x and 74xx
491  * processors.
492  */
helper_fixup_thrm(CPUPPCState * env)493 void helper_fixup_thrm(CPUPPCState *env)
494 {
495     target_ulong v, t;
496     int i;
497 
498 #define THRM1_TIN       (1 << 31)
499 #define THRM1_TIV       (1 << 30)
500 #define THRM1_THRES(x)  (((x) & 0x7f) << 23)
501 #define THRM1_TID       (1 << 2)
502 #define THRM1_TIE       (1 << 1)
503 #define THRM1_V         (1 << 0)
504 #define THRM3_E         (1 << 0)
505 
506     if (!(env->spr[SPR_THRM3] & THRM3_E)) {
507         return;
508     }
509 
510     /* Note: Thermal interrupts are unimplemented */
511     for (i = SPR_THRM1; i <= SPR_THRM2; i++) {
512         v = env->spr[i];
513         if (!(v & THRM1_V)) {
514             continue;
515         }
516         v |= THRM1_TIV;
517         v &= ~THRM1_TIN;
518         t = v & THRM1_THRES(127);
519         if ((v & THRM1_TID) && t < THRM1_THRES(24)) {
520             v |= THRM1_TIN;
521         }
522         if (!(v & THRM1_TID) && t > THRM1_THRES(24)) {
523             v |= THRM1_TIN;
524         }
525         env->spr[i] = v;
526     }
527 }
528 
529 #if !defined(CONFIG_USER_ONLY)
530 #if defined(TARGET_PPC64)
helper_clrbhrb(CPUPPCState * env)531 void helper_clrbhrb(CPUPPCState *env)
532 {
533     helper_hfscr_facility_check(env, HFSCR_BHRB, "clrbhrb", FSCR_IC_BHRB);
534 
535     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
536 
537     if (env->flags & POWERPC_FLAG_BHRB) {
538         memset(env->bhrb, 0, sizeof(env->bhrb));
539     }
540 }
541 
helper_mfbhrbe(CPUPPCState * env,uint32_t bhrbe)542 uint64_t helper_mfbhrbe(CPUPPCState *env, uint32_t bhrbe)
543 {
544     unsigned int index;
545 
546     helper_hfscr_facility_check(env, HFSCR_BHRB, "mfbhrbe", FSCR_IC_BHRB);
547 
548     helper_mmcr0_facility_check(env, MMCR0_BHRBA_NR, 0, FSCR_IC_BHRB);
549 
550     if (!(env->flags & POWERPC_FLAG_BHRB) ||
551          (bhrbe >= env->bhrb_num_entries) ||
552          (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
553         return 0;
554     }
555 
556     /*
557      * Note: bhrb_offset is the byte offset for writing the
558      * next entry (over the oldest entry), which is why we
559      * must offset bhrbe by 1 to get to the 0th entry.
560      */
561     index = ((env->bhrb_offset / sizeof(uint64_t)) - (bhrbe + 1)) %
562             env->bhrb_num_entries;
563     return env->bhrb[index];
564 }
565 #endif
566 #endif
567