1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "sysemu/hw_accel.h"
22#include "sysemu/kvm.h"
23#include "kvm_ppc.h"
24#include "sysemu/cpus.h"
25#include "qemu/error-report.h"
26#include "qapi/error.h"
27#include "qapi/visitor.h"
28#include "cpu-models.h"
29
30typedef struct {
31 const char *name;
32 uint32_t pvr;
33 uint64_t pcr;
34 uint64_t pcr_level;
35 int max_threads;
36} CompatInfo;
37
38static const CompatInfo compat_table[] = {
39
40
41
42 {
43 .name = "power6",
44 .pvr = CPU_POWERPC_LOGICAL_2_05,
45 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 |
46 PCR_COMPAT_2_05 | PCR_TM_DIS | PCR_VSX_DIS,
47 .pcr_level = PCR_COMPAT_2_05,
48 .max_threads = 2,
49 },
50 {
51 .name = "power7",
52 .pvr = CPU_POWERPC_LOGICAL_2_06,
53 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS,
54 .pcr_level = PCR_COMPAT_2_06,
55 .max_threads = 4,
56 },
57 {
58 .name = "power7+",
59 .pvr = CPU_POWERPC_LOGICAL_2_06_PLUS,
60 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS,
61 .pcr_level = PCR_COMPAT_2_06,
62 .max_threads = 4,
63 },
64 {
65 .name = "power8",
66 .pvr = CPU_POWERPC_LOGICAL_2_07,
67 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07,
68 .pcr_level = PCR_COMPAT_2_07,
69 .max_threads = 8,
70 },
71 {
72 .name = "power9",
73 .pvr = CPU_POWERPC_LOGICAL_3_00,
74 .pcr = PCR_COMPAT_3_00,
75 .pcr_level = PCR_COMPAT_3_00,
76
77
78
79
80
81
82
83 .max_threads = 8,
84 },
85};
86
87static const CompatInfo *compat_by_pvr(uint32_t pvr)
88{
89 int i;
90
91 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
92 if (compat_table[i].pvr == pvr) {
93 return &compat_table[i];
94 }
95 }
96 return NULL;
97}
98
99bool ppc_check_compat(PowerPCCPU *cpu, uint32_t compat_pvr,
100 uint32_t min_compat_pvr, uint32_t max_compat_pvr)
101{
102 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
103 const CompatInfo *compat = compat_by_pvr(compat_pvr);
104 const CompatInfo *min = compat_by_pvr(min_compat_pvr);
105 const CompatInfo *max = compat_by_pvr(max_compat_pvr);
106
107#if !defined(CONFIG_USER_ONLY)
108 g_assert(cpu->vhyp);
109#endif
110 g_assert(!min_compat_pvr || min);
111 g_assert(!max_compat_pvr || max);
112
113 if (!compat) {
114
115 return false;
116 }
117 if ((min && (compat < min)) || (max && (compat > max))) {
118
119 return false;
120 }
121 if (!(pcc->pcr_supported & compat->pcr_level)) {
122
123 return false;
124 }
125 return true;
126}
127
128void ppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr, Error **errp)
129{
130 const CompatInfo *compat = compat_by_pvr(compat_pvr);
131 CPUPPCState *env = &cpu->env;
132 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
133 uint64_t pcr;
134
135 if (!compat_pvr) {
136 pcr = 0;
137 } else if (!compat) {
138 error_setg(errp, "Unknown compatibility PVR 0x%08"PRIx32, compat_pvr);
139 return;
140 } else if (!ppc_check_compat(cpu, compat_pvr, 0, 0)) {
141 error_setg(errp, "Compatibility PVR 0x%08"PRIx32" not valid for CPU",
142 compat_pvr);
143 return;
144 } else {
145 pcr = compat->pcr;
146 }
147
148 cpu_synchronize_state(CPU(cpu));
149
150 if (kvm_enabled() && cpu->compat_pvr != compat_pvr) {
151 int ret = kvmppc_set_compat(cpu, compat_pvr);
152 if (ret < 0) {
153 error_setg_errno(errp, -ret,
154 "Unable to set CPU compatibility mode in KVM");
155 return;
156 }
157 }
158
159 cpu->compat_pvr = compat_pvr;
160 env->spr[SPR_PCR] = pcr & pcc->pcr_mask;
161}
162
163typedef struct {
164 uint32_t compat_pvr;
165 Error *err;
166} SetCompatState;
167
168static void do_set_compat(CPUState *cs, run_on_cpu_data arg)
169{
170 PowerPCCPU *cpu = POWERPC_CPU(cs);
171 SetCompatState *s = arg.host_ptr;
172
173 ppc_set_compat(cpu, s->compat_pvr, &s->err);
174}
175
176void ppc_set_compat_all(uint32_t compat_pvr, Error **errp)
177{
178 CPUState *cs;
179
180 CPU_FOREACH(cs) {
181 SetCompatState s = {
182 .compat_pvr = compat_pvr,
183 .err = NULL,
184 };
185
186 run_on_cpu(cs, do_set_compat, RUN_ON_CPU_HOST_PTR(&s));
187
188 if (s.err) {
189 error_propagate(errp, s.err);
190 return;
191 }
192 }
193}
194
195int ppc_compat_max_threads(PowerPCCPU *cpu)
196{
197 const CompatInfo *compat = compat_by_pvr(cpu->compat_pvr);
198 int n_threads = CPU(cpu)->nr_threads;
199
200 if (cpu->compat_pvr) {
201 g_assert(compat);
202 n_threads = MIN(n_threads, compat->max_threads);
203 }
204
205 return n_threads;
206}
207
208static void ppc_compat_prop_get(Object *obj, Visitor *v, const char *name,
209 void *opaque, Error **errp)
210{
211 uint32_t compat_pvr = *((uint32_t *)opaque);
212 const char *value;
213
214 if (!compat_pvr) {
215 value = "";
216 } else {
217 const CompatInfo *compat = compat_by_pvr(compat_pvr);
218
219 g_assert(compat);
220
221 value = compat->name;
222 }
223
224 visit_type_str(v, name, (char **)&value, errp);
225}
226
227static void ppc_compat_prop_set(Object *obj, Visitor *v, const char *name,
228 void *opaque, Error **errp)
229{
230 Error *local_err = NULL;
231 char *value;
232 uint32_t compat_pvr;
233
234 visit_type_str(v, name, &value, &local_err);
235 if (local_err) {
236 error_propagate(errp, local_err);
237 return;
238 }
239
240 if (strcmp(value, "") == 0) {
241 compat_pvr = 0;
242 } else {
243 int i;
244 const CompatInfo *compat = NULL;
245
246 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
247 if (strcmp(value, compat_table[i].name) == 0) {
248 compat = &compat_table[i];
249 break;
250
251 }
252 }
253
254 if (!compat) {
255 error_setg(errp, "Invalid compatibility mode \"%s\"", value);
256 goto out;
257 }
258 compat_pvr = compat->pvr;
259 }
260
261 *((uint32_t *)opaque) = compat_pvr;
262
263out:
264 g_free(value);
265}
266
267void ppc_compat_add_property(Object *obj, const char *name,
268 uint32_t *compat_pvr, const char *basedesc,
269 Error **errp)
270{
271 Error *local_err = NULL;
272 gchar *namesv[ARRAY_SIZE(compat_table) + 1];
273 gchar *names, *desc;
274 int i;
275
276 object_property_add(obj, name, "string",
277 ppc_compat_prop_get, ppc_compat_prop_set, NULL,
278 compat_pvr, &local_err);
279 if (local_err) {
280 goto out;
281 }
282
283 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
284
285
286
287
288 namesv[i] = (gchar *)compat_table[i].name;
289 }
290 namesv[ARRAY_SIZE(compat_table)] = NULL;
291
292 names = g_strjoinv(", ", namesv);
293 desc = g_strdup_printf("%s. Valid values are %s.", basedesc, names);
294 object_property_set_description(obj, name, desc, &local_err);
295
296 g_free(names);
297 g_free(desc);
298
299out:
300 error_propagate(errp, local_err);
301}
302