block: implement the bdrv_reopen_prepare helper for LUKS driver
[qemu/ar7.git] / target / ppc / compat.c
blob807c906f68486e4f8c0c012515007fdf4728fa4d
1 /*
2 * PowerPC CPU initialization for qemu.
4 * Copyright 2016, David Gibson, Red Hat Inc. <dgibson@redhat.com>
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 of the License, or (at your option) any later version.
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.
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/>.
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"
30 typedef struct {
31 const char *name;
32 uint32_t pvr;
33 uint64_t pcr;
34 uint64_t pcr_level;
37 * Maximum allowed virtual threads per virtual core
39 * This is to stop older guests getting confused by seeing more
40 * threads than they think the cpu can support. Usually it's
41 * equal to the number of threads supported on bare metal
42 * hardware, but not always (see POWER9).
44 int max_vthreads;
45 } CompatInfo;
47 static const CompatInfo compat_table[] = {
49 * Ordered from oldest to newest - the code relies on this
51 { /* POWER6, ISA2.05 */
52 .name = "power6",
53 .pvr = CPU_POWERPC_LOGICAL_2_05,
54 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 |
55 PCR_COMPAT_2_05 | PCR_TM_DIS | PCR_VSX_DIS,
56 .pcr_level = PCR_COMPAT_2_05,
57 .max_vthreads = 2,
59 { /* POWER7, ISA2.06 */
60 .name = "power7",
61 .pvr = CPU_POWERPC_LOGICAL_2_06,
62 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS,
63 .pcr_level = PCR_COMPAT_2_06,
64 .max_vthreads = 4,
67 .name = "power7+",
68 .pvr = CPU_POWERPC_LOGICAL_2_06_PLUS,
69 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS,
70 .pcr_level = PCR_COMPAT_2_06,
71 .max_vthreads = 4,
73 { /* POWER8, ISA2.07 */
74 .name = "power8",
75 .pvr = CPU_POWERPC_LOGICAL_2_07,
76 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07,
77 .pcr_level = PCR_COMPAT_2_07,
78 .max_vthreads = 8,
80 { /* POWER9, ISA3.00 */
81 .name = "power9",
82 .pvr = CPU_POWERPC_LOGICAL_3_00,
83 .pcr = PCR_COMPAT_3_00,
84 .pcr_level = PCR_COMPAT_3_00,
86 * POWER9 hardware only supports 4 threads / core, but this
87 * limit is for guests. We need to support 8 vthreads/vcore
88 * on POWER9 for POWER8 compatibility guests, and it's very
89 * confusing if half of the threads disappear from the guest
90 * if it announces it's POWER9 aware at CAS time.
92 .max_vthreads = 8,
96 static const CompatInfo *compat_by_pvr(uint32_t pvr)
98 int i;
100 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
101 if (compat_table[i].pvr == pvr) {
102 return &compat_table[i];
105 return NULL;
108 bool ppc_check_compat(PowerPCCPU *cpu, uint32_t compat_pvr,
109 uint32_t min_compat_pvr, uint32_t max_compat_pvr)
111 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
112 const CompatInfo *compat = compat_by_pvr(compat_pvr);
113 const CompatInfo *min = compat_by_pvr(min_compat_pvr);
114 const CompatInfo *max = compat_by_pvr(max_compat_pvr);
116 #if !defined(CONFIG_USER_ONLY)
117 g_assert(cpu->vhyp);
118 #endif
119 g_assert(!min_compat_pvr || min);
120 g_assert(!max_compat_pvr || max);
122 if (!compat) {
123 /* Not a recognized logical PVR */
124 return false;
126 if ((min && (compat < min)) || (max && (compat > max))) {
127 /* Outside specified range */
128 return false;
130 if (!(pcc->pcr_supported & compat->pcr_level)) {
131 /* Not supported by this CPU */
132 return false;
134 return true;
137 void ppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr, Error **errp)
139 const CompatInfo *compat = compat_by_pvr(compat_pvr);
140 CPUPPCState *env = &cpu->env;
141 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
142 uint64_t pcr;
144 if (!compat_pvr) {
145 pcr = 0;
146 } else if (!compat) {
147 error_setg(errp, "Unknown compatibility PVR 0x%08"PRIx32, compat_pvr);
148 return;
149 } else if (!ppc_check_compat(cpu, compat_pvr, 0, 0)) {
150 error_setg(errp, "Compatibility PVR 0x%08"PRIx32" not valid for CPU",
151 compat_pvr);
152 return;
153 } else {
154 pcr = compat->pcr;
157 cpu_synchronize_state(CPU(cpu));
159 if (kvm_enabled() && cpu->compat_pvr != compat_pvr) {
160 int ret = kvmppc_set_compat(cpu, compat_pvr);
161 if (ret < 0) {
162 error_setg_errno(errp, -ret,
163 "Unable to set CPU compatibility mode in KVM");
164 return;
168 cpu->compat_pvr = compat_pvr;
169 env->spr[SPR_PCR] = pcr & pcc->pcr_mask;
172 typedef struct {
173 uint32_t compat_pvr;
174 Error *err;
175 } SetCompatState;
177 static void do_set_compat(CPUState *cs, run_on_cpu_data arg)
179 PowerPCCPU *cpu = POWERPC_CPU(cs);
180 SetCompatState *s = arg.host_ptr;
182 ppc_set_compat(cpu, s->compat_pvr, &s->err);
185 void ppc_set_compat_all(uint32_t compat_pvr, Error **errp)
187 CPUState *cs;
189 CPU_FOREACH(cs) {
190 SetCompatState s = {
191 .compat_pvr = compat_pvr,
192 .err = NULL,
195 run_on_cpu(cs, do_set_compat, RUN_ON_CPU_HOST_PTR(&s));
197 if (s.err) {
198 error_propagate(errp, s.err);
199 return;
204 int ppc_compat_max_vthreads(PowerPCCPU *cpu)
206 const CompatInfo *compat = compat_by_pvr(cpu->compat_pvr);
207 int n_threads = CPU(cpu)->nr_threads;
209 if (cpu->compat_pvr) {
210 g_assert(compat);
211 n_threads = MIN(n_threads, compat->max_vthreads);
214 return n_threads;
217 static void ppc_compat_prop_get(Object *obj, Visitor *v, const char *name,
218 void *opaque, Error **errp)
220 uint32_t compat_pvr = *((uint32_t *)opaque);
221 const char *value;
223 if (!compat_pvr) {
224 value = "";
225 } else {
226 const CompatInfo *compat = compat_by_pvr(compat_pvr);
228 g_assert(compat);
230 value = compat->name;
233 visit_type_str(v, name, (char **)&value, errp);
236 static void ppc_compat_prop_set(Object *obj, Visitor *v, const char *name,
237 void *opaque, Error **errp)
239 Error *local_err = NULL;
240 char *value;
241 uint32_t compat_pvr;
243 visit_type_str(v, name, &value, &local_err);
244 if (local_err) {
245 error_propagate(errp, local_err);
246 return;
249 if (strcmp(value, "") == 0) {
250 compat_pvr = 0;
251 } else {
252 int i;
253 const CompatInfo *compat = NULL;
255 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
256 if (strcmp(value, compat_table[i].name) == 0) {
257 compat = &compat_table[i];
258 break;
263 if (!compat) {
264 error_setg(errp, "Invalid compatibility mode \"%s\"", value);
265 goto out;
267 compat_pvr = compat->pvr;
270 *((uint32_t *)opaque) = compat_pvr;
272 out:
273 g_free(value);
276 void ppc_compat_add_property(Object *obj, const char *name,
277 uint32_t *compat_pvr, const char *basedesc,
278 Error **errp)
280 Error *local_err = NULL;
281 gchar *namesv[ARRAY_SIZE(compat_table) + 1];
282 gchar *names, *desc;
283 int i;
285 object_property_add(obj, name, "string",
286 ppc_compat_prop_get, ppc_compat_prop_set, NULL,
287 compat_pvr, &local_err);
288 if (local_err) {
289 goto out;
292 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
294 * Have to discard const here, because g_strjoinv() takes
295 * (gchar **), not (const gchar **) :(
297 namesv[i] = (gchar *)compat_table[i].name;
299 namesv[ARRAY_SIZE(compat_table)] = NULL;
301 names = g_strjoinv(", ", namesv);
302 desc = g_strdup_printf("%s. Valid values are %s.", basedesc, names);
303 object_property_set_description(obj, name, desc, &local_err);
305 g_free(names);
306 g_free(desc);
308 out:
309 error_propagate(errp, local_err);