xref: /openbmc/qemu/target/sparc/machine.c (revision ff788b70)
1 #include "qemu/osdep.h"
2 #include "cpu.h"
3 #include "exec/exec-all.h"
4 #include "qemu/timer.h"
5 
6 #include "migration/cpu.h"
7 
8 #ifdef TARGET_SPARC64
9 static const VMStateDescription vmstate_cpu_timer = {
10     .name = "cpu_timer",
11     .version_id = 1,
12     .minimum_version_id = 1,
13     .fields = (const VMStateField[]) {
14         VMSTATE_UINT32(frequency, CPUTimer),
15         VMSTATE_UINT32(disabled, CPUTimer),
16         VMSTATE_UINT64(disabled_mask, CPUTimer),
17         VMSTATE_UINT32(npt, CPUTimer),
18         VMSTATE_UINT64(npt_mask, CPUTimer),
19         VMSTATE_INT64(clock_offset, CPUTimer),
20         VMSTATE_TIMER_PTR(qtimer, CPUTimer),
21         VMSTATE_END_OF_LIST()
22     }
23 };
24 
25 #define VMSTATE_CPU_TIMER(_f, _s)                             \
26     VMSTATE_STRUCT_POINTER(_f, _s, vmstate_cpu_timer, CPUTimer)
27 
28 static const VMStateDescription vmstate_trap_state = {
29     .name = "trap_state",
30     .version_id = 1,
31     .minimum_version_id = 1,
32     .fields = (const VMStateField[]) {
33         VMSTATE_UINT64(tpc, trap_state),
34         VMSTATE_UINT64(tnpc, trap_state),
35         VMSTATE_UINT64(tstate, trap_state),
36         VMSTATE_UINT32(tt, trap_state),
37         VMSTATE_END_OF_LIST()
38     }
39 };
40 
41 static const VMStateDescription vmstate_tlb_entry = {
42     .name = "tlb_entry",
43     .version_id = 1,
44     .minimum_version_id = 1,
45     .fields = (const VMStateField[]) {
46         VMSTATE_UINT64(tag, SparcTLBEntry),
47         VMSTATE_UINT64(tte, SparcTLBEntry),
48         VMSTATE_END_OF_LIST()
49     }
50 };
51 #endif
52 
53 static int get_psr(QEMUFile *f, void *opaque, size_t size,
54                    const VMStateField *field)
55 {
56     SPARCCPU *cpu = opaque;
57     CPUSPARCState *env = &cpu->env;
58     uint32_t val = qemu_get_be32(f);
59 
60     /* needed to ensure that the wrapping registers are correctly updated */
61     env->cwp = 0;
62     cpu_put_psr_raw(env, val);
63 
64     return 0;
65 }
66 
67 static int put_psr(QEMUFile *f, void *opaque, size_t size,
68                    const VMStateField *field, JSONWriter *vmdesc)
69 {
70     SPARCCPU *cpu = opaque;
71     CPUSPARCState *env = &cpu->env;
72     uint32_t val;
73 
74     val = cpu_get_psr(env);
75 
76     qemu_put_be32(f, val);
77     return 0;
78 }
79 
80 static const VMStateInfo vmstate_psr = {
81     .name = "psr",
82     .get = get_psr,
83     .put = put_psr,
84 };
85 
86 static int get_fsr(QEMUFile *f, void *opaque, size_t size,
87                    const VMStateField *field)
88 {
89     SPARCCPU *cpu = opaque;
90     target_ulong val = qemu_get_betl(f);
91 
92     cpu_put_fsr(&cpu->env, val);
93     return 0;
94 }
95 
96 static int put_fsr(QEMUFile *f, void *opaque, size_t size,
97                    const VMStateField *field, JSONWriter *vmdesc)
98 {
99     SPARCCPU *cpu = opaque;
100     target_ulong val = cpu_get_fsr(&cpu->env);
101 
102     qemu_put_betl(f, val);
103     return 0;
104 }
105 
106 static const VMStateInfo vmstate_fsr = {
107     .name = "fsr",
108     .get = get_fsr,
109     .put = put_fsr,
110 };
111 
112 #ifdef TARGET_SPARC64
113 static int get_xcc(QEMUFile *f, void *opaque, size_t size,
114                    const VMStateField *field)
115 {
116     SPARCCPU *cpu = opaque;
117     CPUSPARCState *env = &cpu->env;
118     uint32_t val = qemu_get_be32(f);
119 
120     /* Do not clobber icc.[NV] */
121     env->cc_N = deposit64(env->cc_N, 32, 32, -(val & PSR_NEG));
122     env->cc_V = deposit64(env->cc_V, 32, 32, -(val & PSR_OVF));
123     env->xcc_Z = ~val & PSR_ZERO;
124     env->xcc_C = (val >> PSR_CARRY_SHIFT) & 1;
125 
126     return 0;
127 }
128 
129 static int put_xcc(QEMUFile *f, void *opaque, size_t size,
130                    const VMStateField *field, JSONWriter *vmdesc)
131 {
132     SPARCCPU *cpu = opaque;
133     CPUSPARCState *env = &cpu->env;
134     uint32_t val = cpu_get_ccr(env);
135 
136     /* Extract just xcc out of ccr and shift into legacy position. */
137     qemu_put_be32(f, (val & 0xf0) << (20 - 4));
138     return 0;
139 }
140 
141 static const VMStateInfo vmstate_xcc = {
142     .name = "xcc",
143     .get = get_xcc,
144     .put = put_xcc,
145 };
146 #else
147 static bool fq_needed(void *opaque)
148 {
149     SPARCCPU *cpu = opaque;
150     return cpu->env.fsr_qne;
151 }
152 
153 static const VMStateDescription vmstate_fq = {
154     .name = "cpu/fq",
155     .version_id = 1,
156     .minimum_version_id = 1,
157     .needed = fq_needed,
158     .fields = (const VMStateField[]) {
159         VMSTATE_UINT32(env.fq.s.addr, SPARCCPU),
160         VMSTATE_UINT32(env.fq.s.insn, SPARCCPU),
161         VMSTATE_END_OF_LIST()
162     },
163 };
164 #endif
165 
166 static int cpu_pre_save(void *opaque)
167 {
168     SPARCCPU *cpu = opaque;
169     CPUSPARCState *env = &cpu->env;
170 
171     /* if env->cwp == env->nwindows - 1, this will set the ins of the last
172      * window as the outs of the first window
173      */
174     cpu_set_cwp(env, env->cwp);
175 
176     return 0;
177 }
178 
179 /* 32-bit SPARC retains migration compatibility with older versions
180  * of QEMU; 64-bit SPARC has had a migration break since then, so the
181  * versions are different.
182  */
183 #ifndef TARGET_SPARC64
184 #define SPARC_VMSTATE_VER 7
185 #else
186 #define SPARC_VMSTATE_VER 9
187 #endif
188 
189 const VMStateDescription vmstate_sparc_cpu = {
190     .name = "cpu",
191     .version_id = SPARC_VMSTATE_VER,
192     .minimum_version_id = SPARC_VMSTATE_VER,
193     .pre_save = cpu_pre_save,
194     .fields = (const VMStateField[]) {
195         VMSTATE_UINTTL_ARRAY(env.gregs, SPARCCPU, 8),
196         VMSTATE_UINT32(env.nwindows, SPARCCPU),
197         VMSTATE_VARRAY_MULTIPLY(env.regbase, SPARCCPU, env.nwindows, 16,
198                                 vmstate_info_uinttl, target_ulong),
199         VMSTATE_CPUDOUBLE_ARRAY(env.fpr, SPARCCPU, TARGET_DPREGS),
200         VMSTATE_UINTTL(env.pc, SPARCCPU),
201         VMSTATE_UINTTL(env.npc, SPARCCPU),
202         VMSTATE_UINTTL(env.y, SPARCCPU),
203         {
204             .name = "psr",
205             .version_id = 0,
206             .size = sizeof(uint32_t),
207             .info = &vmstate_psr,
208             .flags = VMS_SINGLE,
209             .offset = 0,
210         },
211         {
212             .name = "fsr",
213             .version_id = 0,
214             .size = sizeof(target_ulong),
215             .info = &vmstate_fsr,
216             .flags = VMS_SINGLE,
217             .offset = 0,
218         },
219         VMSTATE_UINTTL(env.tbr, SPARCCPU),
220         VMSTATE_INT32(env.interrupt_index, SPARCCPU),
221         VMSTATE_UINT32(env.pil_in, SPARCCPU),
222 #ifndef TARGET_SPARC64
223         /* MMU */
224         VMSTATE_UINT32(env.wim, SPARCCPU),
225         VMSTATE_UINT32_ARRAY(env.mmuregs, SPARCCPU, 32),
226         VMSTATE_UINT64_ARRAY(env.mxccdata, SPARCCPU, 4),
227         VMSTATE_UINT64_ARRAY(env.mxccregs, SPARCCPU, 8),
228         VMSTATE_UINT32(env.mmubpctrv, SPARCCPU),
229         VMSTATE_UINT32(env.mmubpctrc, SPARCCPU),
230         VMSTATE_UINT32(env.mmubpctrs, SPARCCPU),
231         VMSTATE_UINT64(env.mmubpaction, SPARCCPU),
232         VMSTATE_UINT64_ARRAY(env.mmubpregs, SPARCCPU, 4),
233 #else
234         VMSTATE_UINT64(env.lsu, SPARCCPU),
235         VMSTATE_UINT64_ARRAY(env.immu.mmuregs, SPARCCPU, 16),
236         VMSTATE_UINT64_ARRAY(env.dmmu.mmuregs, SPARCCPU, 16),
237         VMSTATE_STRUCT_ARRAY(env.itlb, SPARCCPU, 64, 0,
238                              vmstate_tlb_entry, SparcTLBEntry),
239         VMSTATE_STRUCT_ARRAY(env.dtlb, SPARCCPU, 64, 0,
240                              vmstate_tlb_entry, SparcTLBEntry),
241         VMSTATE_UINT32(env.mmu_version, SPARCCPU),
242         VMSTATE_STRUCT_ARRAY(env.ts, SPARCCPU, MAXTL_MAX, 0,
243                              vmstate_trap_state, trap_state),
244         {
245             .name = "xcc",
246             .version_id = 0,
247             .size = sizeof(uint32_t),
248             .info = &vmstate_xcc,
249             .flags = VMS_SINGLE,
250             .offset = 0,
251         },
252         VMSTATE_UINT32(env.asi, SPARCCPU),
253         VMSTATE_UINT32(env.pstate, SPARCCPU),
254         VMSTATE_UINT32(env.tl, SPARCCPU),
255         VMSTATE_UINT32(env.cansave, SPARCCPU),
256         VMSTATE_UINT32(env.canrestore, SPARCCPU),
257         VMSTATE_UINT32(env.otherwin, SPARCCPU),
258         VMSTATE_UINT32(env.wstate, SPARCCPU),
259         VMSTATE_UINT32(env.cleanwin, SPARCCPU),
260         VMSTATE_UINT64_ARRAY(env.agregs, SPARCCPU, 8),
261         VMSTATE_UINT64_ARRAY(env.bgregs, SPARCCPU, 8),
262         VMSTATE_UINT64_ARRAY(env.igregs, SPARCCPU, 8),
263         VMSTATE_UINT64_ARRAY(env.mgregs, SPARCCPU, 8),
264         VMSTATE_UNUSED(4), /* was unused high half of uint64_t fprs */
265         VMSTATE_UINT32(env.fprs, SPARCCPU),
266         VMSTATE_UINT64(env.tick_cmpr, SPARCCPU),
267         VMSTATE_UINT64(env.stick_cmpr, SPARCCPU),
268         VMSTATE_CPU_TIMER(env.tick, SPARCCPU),
269         VMSTATE_CPU_TIMER(env.stick, SPARCCPU),
270         VMSTATE_UINT64(env.gsr, SPARCCPU),
271         VMSTATE_UINT32(env.gl, SPARCCPU),
272         VMSTATE_UINT64(env.hpstate, SPARCCPU),
273         VMSTATE_UINT64_ARRAY(env.htstate, SPARCCPU, MAXTL_MAX),
274         VMSTATE_UINT64(env.hintp, SPARCCPU),
275         VMSTATE_UINT64(env.htba, SPARCCPU),
276         VMSTATE_UINT64(env.hver, SPARCCPU),
277         VMSTATE_UINT64(env.hstick_cmpr, SPARCCPU),
278         VMSTATE_UINT64(env.ssr, SPARCCPU),
279         VMSTATE_CPU_TIMER(env.hstick, SPARCCPU),
280         /* On SPARC32 env.psrpil and env.cwp are migrated as part of the PSR */
281         VMSTATE_UINT32(env.psrpil, SPARCCPU),
282         VMSTATE_UINT32(env.cwp, SPARCCPU),
283 #endif
284         VMSTATE_END_OF_LIST()
285     },
286 #ifndef TARGET_SPARC64
287     .subsections = (const VMStateDescription * const []) {
288         &vmstate_fq,
289         NULL
290     },
291 #endif
292 
293 };
294