xref: /openbmc/qemu/target/ppc/cpu.c (revision a3f5c315)
100b70788SNikunj A Dadhania /*
200b70788SNikunj A Dadhania  *  PowerPC CPU routines for qemu.
300b70788SNikunj A Dadhania  *
400b70788SNikunj A Dadhania  * Copyright (c) 2017 Nikunj A Dadhania, IBM Corporation.
500b70788SNikunj A Dadhania  *
600b70788SNikunj A Dadhania  * This library is free software; you can redistribute it and/or
700b70788SNikunj A Dadhania  * modify it under the terms of the GNU Lesser General Public
800b70788SNikunj A Dadhania  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
1000b70788SNikunj A Dadhania  *
1100b70788SNikunj A Dadhania  * This library is distributed in the hope that it will be useful,
1200b70788SNikunj A Dadhania  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1300b70788SNikunj A Dadhania  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1400b70788SNikunj A Dadhania  * Lesser General Public License for more details.
1500b70788SNikunj A Dadhania  *
1600b70788SNikunj A Dadhania  * You should have received a copy of the GNU Lesser General Public
1700b70788SNikunj A Dadhania  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1800b70788SNikunj A Dadhania  */
1900b70788SNikunj A Dadhania 
2000b70788SNikunj A Dadhania #include "qemu/osdep.h"
2100b70788SNikunj A Dadhania #include "cpu.h"
2200b70788SNikunj A Dadhania #include "cpu-models.h"
23172d74efSBruno Larsen (billionai) #include "cpu-qom.h"
24172d74efSBruno Larsen (billionai) #include "exec/log.h"
25c19940dbSBruno Larsen (billionai) #include "fpu/softfloat-helpers.h"
26172d74efSBruno Larsen (billionai) #include "mmu-hash64.h"
27*a3f5c315SBruno Larsen (billionai) #include "helper_regs.h"
2800b70788SNikunj A Dadhania 
2900b70788SNikunj A Dadhania target_ulong cpu_read_xer(CPUPPCState *env)
3000b70788SNikunj A Dadhania {
31dd09c361SNikunj A Dadhania     if (is_isa300(env)) {
32dd09c361SNikunj A Dadhania         return env->xer | (env->so << XER_SO) |
33dd09c361SNikunj A Dadhania             (env->ov << XER_OV) | (env->ca << XER_CA) |
34dd09c361SNikunj A Dadhania             (env->ov32 << XER_OV32) | (env->ca32 << XER_CA32);
35dd09c361SNikunj A Dadhania     }
36dd09c361SNikunj A Dadhania 
3700b70788SNikunj A Dadhania     return env->xer | (env->so << XER_SO) | (env->ov << XER_OV) |
3800b70788SNikunj A Dadhania         (env->ca << XER_CA);
3900b70788SNikunj A Dadhania }
4000b70788SNikunj A Dadhania 
4100b70788SNikunj A Dadhania void cpu_write_xer(CPUPPCState *env, target_ulong xer)
4200b70788SNikunj A Dadhania {
4300b70788SNikunj A Dadhania     env->so = (xer >> XER_SO) & 1;
4400b70788SNikunj A Dadhania     env->ov = (xer >> XER_OV) & 1;
4500b70788SNikunj A Dadhania     env->ca = (xer >> XER_CA) & 1;
46dd09c361SNikunj A Dadhania     /* write all the flags, while reading back check of isa300 */
47dd09c361SNikunj A Dadhania     env->ov32 = (xer >> XER_OV32) & 1;
48dd09c361SNikunj A Dadhania     env->ca32 = (xer >> XER_CA32) & 1;
49dd09c361SNikunj A Dadhania     env->xer = xer & ~((1ul << XER_SO) |
50dd09c361SNikunj A Dadhania                        (1ul << XER_OV) | (1ul << XER_CA) |
51dd09c361SNikunj A Dadhania                        (1ul << XER_OV32) | (1ul << XER_CA32));
5200b70788SNikunj A Dadhania }
53c19940dbSBruno Larsen (billionai) 
54c19940dbSBruno Larsen (billionai) void ppc_store_vscr(CPUPPCState *env, uint32_t vscr)
55c19940dbSBruno Larsen (billionai) {
56c19940dbSBruno Larsen (billionai)     env->vscr = vscr & ~(1u << VSCR_SAT);
57c19940dbSBruno Larsen (billionai)     /* Which bit we set is completely arbitrary, but clear the rest.  */
58c19940dbSBruno Larsen (billionai)     env->vscr_sat.u64[0] = vscr & (1u << VSCR_SAT);
59c19940dbSBruno Larsen (billionai)     env->vscr_sat.u64[1] = 0;
60c19940dbSBruno Larsen (billionai)     set_flush_to_zero((vscr >> VSCR_NJ) & 1, &env->vec_status);
61c19940dbSBruno Larsen (billionai) }
62c19940dbSBruno Larsen (billionai) 
63c19940dbSBruno Larsen (billionai) uint32_t ppc_get_vscr(CPUPPCState *env)
64c19940dbSBruno Larsen (billionai) {
65c19940dbSBruno Larsen (billionai)     uint32_t sat = (env->vscr_sat.u64[0] | env->vscr_sat.u64[1]) != 0;
66c19940dbSBruno Larsen (billionai)     return env->vscr | (sat << VSCR_SAT);
67c19940dbSBruno Larsen (billionai) }
68172d74efSBruno Larsen (billionai) 
69172d74efSBruno Larsen (billionai) #ifdef CONFIG_SOFTMMU
70172d74efSBruno Larsen (billionai) void ppc_store_sdr1(CPUPPCState *env, target_ulong value)
71172d74efSBruno Larsen (billionai) {
72172d74efSBruno Larsen (billionai)     PowerPCCPU *cpu = env_archcpu(env);
73172d74efSBruno Larsen (billionai)     qemu_log_mask(CPU_LOG_MMU, "%s: " TARGET_FMT_lx "\n", __func__, value);
74172d74efSBruno Larsen (billionai)     assert(!cpu->vhyp);
75172d74efSBruno Larsen (billionai) #if defined(TARGET_PPC64)
76172d74efSBruno Larsen (billionai)     if (mmu_is_64bit(env->mmu_model)) {
77172d74efSBruno Larsen (billionai)         target_ulong sdr_mask = SDR_64_HTABORG | SDR_64_HTABSIZE;
78172d74efSBruno Larsen (billionai)         target_ulong htabsize = value & SDR_64_HTABSIZE;
79172d74efSBruno Larsen (billionai) 
80172d74efSBruno Larsen (billionai)         if (value & ~sdr_mask) {
81dbf2e70aSBruno Larsen (billionai)             qemu_log_mask(LOG_GUEST_ERROR, "Invalid bits 0x"TARGET_FMT_lx
82dbf2e70aSBruno Larsen (billionai)                      " set in SDR1", value & ~sdr_mask);
83172d74efSBruno Larsen (billionai)             value &= sdr_mask;
84172d74efSBruno Larsen (billionai)         }
85172d74efSBruno Larsen (billionai)         if (htabsize > 28) {
86dbf2e70aSBruno Larsen (billionai)             qemu_log_mask(LOG_GUEST_ERROR, "Invalid HTABSIZE 0x" TARGET_FMT_lx
87dbf2e70aSBruno Larsen (billionai)                      " stored in SDR1", htabsize);
88172d74efSBruno Larsen (billionai)             return;
89172d74efSBruno Larsen (billionai)         }
90172d74efSBruno Larsen (billionai)     }
91172d74efSBruno Larsen (billionai) #endif /* defined(TARGET_PPC64) */
92172d74efSBruno Larsen (billionai)     /* FIXME: Should check for valid HTABMASK values in 32-bit case */
93172d74efSBruno Larsen (billionai)     env->spr[SPR_SDR1] = value;
94172d74efSBruno Larsen (billionai) }
95172d74efSBruno Larsen (billionai) #endif /* CONFIG_SOFTMMU */
96*a3f5c315SBruno Larsen (billionai) 
97*a3f5c315SBruno Larsen (billionai) /* GDBstub can read and write MSR... */
98*a3f5c315SBruno Larsen (billionai) void ppc_store_msr(CPUPPCState *env, target_ulong value)
99*a3f5c315SBruno Larsen (billionai) {
100*a3f5c315SBruno Larsen (billionai)     hreg_store_msr(env, value, 0);
101*a3f5c315SBruno Larsen (billionai) }
102*a3f5c315SBruno Larsen (billionai) 
103*a3f5c315SBruno Larsen (billionai) void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val)
104*a3f5c315SBruno Larsen (billionai) {
105*a3f5c315SBruno Larsen (billionai)     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
106*a3f5c315SBruno Larsen (billionai)     CPUPPCState *env = &cpu->env;
107*a3f5c315SBruno Larsen (billionai) 
108*a3f5c315SBruno Larsen (billionai)     env->spr[SPR_LPCR] = val & pcc->lpcr_mask;
109*a3f5c315SBruno Larsen (billionai)     /* The gtse bit affects hflags */
110*a3f5c315SBruno Larsen (billionai)     hreg_compute_hflags(env);
111*a3f5c315SBruno Larsen (billionai) }
112