]> git.proxmox.com Git - mirror_qemu.git/blame - target/ppc/compat.c
ppc/pnv: consolidate the creation of the ISA bus device tree
[mirror_qemu.git] / target / ppc / compat.c
CommitLineData
9d6f1065
DG
1/*
2 * PowerPC CPU initialization for qemu.
3 *
4 * Copyright 2016, David Gibson, Red Hat Inc. <dgibson@redhat.com>
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 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"
f6f242c7 21#include "sysemu/hw_accel.h"
9d6f1065
DG
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"
7843c0d6 27#include "qapi/visitor.h"
9d6f1065
DG
28#include "cpu-models.h"
29
30typedef struct {
7843c0d6 31 const char *name;
9d6f1065
DG
32 uint32_t pvr;
33 uint64_t pcr;
9d2179d6 34 uint64_t pcr_level;
abbc1247
DG
35
36 /*
37 * Maximum allowed virtual threads per virtual core
38 *
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).
43 */
44 int max_vthreads;
9d6f1065
DG
45} CompatInfo;
46
47static const CompatInfo compat_table[] = {
9d2179d6
DG
48 /*
49 * Ordered from oldest to newest - the code relies on this
50 */
9d6f1065 51 { /* POWER6, ISA2.05 */
7843c0d6 52 .name = "power6",
9d6f1065 53 .pvr = CPU_POWERPC_LOGICAL_2_05,
9b44c836
SJS
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,
9d2179d6 56 .pcr_level = PCR_COMPAT_2_05,
abbc1247 57 .max_vthreads = 2,
9d6f1065
DG
58 },
59 { /* POWER7, ISA2.06 */
7843c0d6 60 .name = "power7",
9d6f1065 61 .pvr = CPU_POWERPC_LOGICAL_2_06,
9b44c836 62 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS,
9d2179d6 63 .pcr_level = PCR_COMPAT_2_06,
abbc1247 64 .max_vthreads = 4,
9d6f1065
DG
65 },
66 {
7843c0d6 67 .name = "power7+",
9d6f1065 68 .pvr = CPU_POWERPC_LOGICAL_2_06_PLUS,
9b44c836 69 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | PCR_COMPAT_2_06 | PCR_TM_DIS,
9d2179d6 70 .pcr_level = PCR_COMPAT_2_06,
abbc1247 71 .max_vthreads = 4,
9d6f1065
DG
72 },
73 { /* POWER8, ISA2.07 */
7843c0d6 74 .name = "power8",
9d6f1065 75 .pvr = CPU_POWERPC_LOGICAL_2_07,
9b44c836 76 .pcr = PCR_COMPAT_3_00 | PCR_COMPAT_2_07,
9d2179d6 77 .pcr_level = PCR_COMPAT_2_07,
abbc1247 78 .max_vthreads = 8,
9d6f1065 79 },
9b44c836 80 { /* POWER9, ISA3.00 */
7843c0d6 81 .name = "power9",
9b44c836
SJS
82 .pvr = CPU_POWERPC_LOGICAL_3_00,
83 .pcr = PCR_COMPAT_3_00,
84 .pcr_level = PCR_COMPAT_3_00,
03ee51d3
JRZ
85 /*
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.
91 */
abbc1247 92 .max_vthreads = 8,
9b44c836 93 },
9d6f1065
DG
94};
95
96static const CompatInfo *compat_by_pvr(uint32_t pvr)
97{
98 int i;
99
100 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
101 if (compat_table[i].pvr == pvr) {
102 return &compat_table[i];
103 }
104 }
105 return NULL;
106}
107
9d2179d6
DG
108bool ppc_check_compat(PowerPCCPU *cpu, uint32_t compat_pvr,
109 uint32_t min_compat_pvr, uint32_t max_compat_pvr)
110{
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);
115
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);
121
122 if (!compat) {
123 /* Not a recognized logical PVR */
124 return false;
125 }
126 if ((min && (compat < min)) || (max && (compat > max))) {
127 /* Outside specified range */
128 return false;
129 }
130 if (!(pcc->pcr_supported & compat->pcr_level)) {
131 /* Not supported by this CPU */
132 return false;
133 }
134 return true;
135}
136
9d6f1065
DG
137void ppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr, Error **errp)
138{
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;
143
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;
9d2179d6
DG
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;
9d6f1065
DG
153 } else {
154 pcr = compat->pcr;
155 }
156
f6f242c7
DG
157 cpu_synchronize_state(CPU(cpu));
158
5dfaa532 159 if (kvm_enabled() && cpu->compat_pvr != compat_pvr) {
e4f0c6bb 160 int ret = kvmppc_set_compat(cpu, compat_pvr);
9d6f1065
DG
161 if (ret < 0) {
162 error_setg_errno(errp, -ret,
163 "Unable to set CPU compatibility mode in KVM");
5dfaa532 164 return;
9d6f1065
DG
165 }
166 }
5dfaa532
GK
167
168 cpu->compat_pvr = compat_pvr;
169 env->spr[SPR_PCR] = pcr & pcc->pcr_mask;
9d6f1065 170}
12dbeb16 171
f6f242c7
DG
172typedef struct {
173 uint32_t compat_pvr;
174 Error *err;
175} SetCompatState;
176
177static void do_set_compat(CPUState *cs, run_on_cpu_data arg)
178{
179 PowerPCCPU *cpu = POWERPC_CPU(cs);
180 SetCompatState *s = arg.host_ptr;
181
182 ppc_set_compat(cpu, s->compat_pvr, &s->err);
183}
184
185void ppc_set_compat_all(uint32_t compat_pvr, Error **errp)
186{
187 CPUState *cs;
188
189 CPU_FOREACH(cs) {
190 SetCompatState s = {
191 .compat_pvr = compat_pvr,
192 .err = NULL,
193 };
194
195 run_on_cpu(cs, do_set_compat, RUN_ON_CPU_HOST_PTR(&s));
196
197 if (s.err) {
198 error_propagate(errp, s.err);
199 return;
200 }
201 }
202}
203
abbc1247 204int ppc_compat_max_vthreads(PowerPCCPU *cpu)
12dbeb16
DG
205{
206 const CompatInfo *compat = compat_by_pvr(cpu->compat_pvr);
207 int n_threads = CPU(cpu)->nr_threads;
208
209 if (cpu->compat_pvr) {
210 g_assert(compat);
abbc1247 211 n_threads = MIN(n_threads, compat->max_vthreads);
12dbeb16
DG
212 }
213
214 return n_threads;
215}
7843c0d6
DG
216
217static void ppc_compat_prop_get(Object *obj, Visitor *v, const char *name,
218 void *opaque, Error **errp)
219{
220 uint32_t compat_pvr = *((uint32_t *)opaque);
221 const char *value;
222
223 if (!compat_pvr) {
224 value = "";
225 } else {
226 const CompatInfo *compat = compat_by_pvr(compat_pvr);
227
228 g_assert(compat);
229
230 value = compat->name;
231 }
232
233 visit_type_str(v, name, (char **)&value, errp);
234}
235
236static void ppc_compat_prop_set(Object *obj, Visitor *v, const char *name,
237 void *opaque, Error **errp)
238{
239 Error *local_err = NULL;
240 char *value;
241 uint32_t compat_pvr;
242
243 visit_type_str(v, name, &value, &local_err);
244 if (local_err) {
245 error_propagate(errp, local_err);
246 return;
247 }
248
249 if (strcmp(value, "") == 0) {
250 compat_pvr = 0;
251 } else {
252 int i;
253 const CompatInfo *compat = NULL;
254
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;
259
260 }
261 }
262
263 if (!compat) {
264 error_setg(errp, "Invalid compatibility mode \"%s\"", value);
265 goto out;
266 }
267 compat_pvr = compat->pvr;
268 }
269
270 *((uint32_t *)opaque) = compat_pvr;
271
272out:
273 g_free(value);
274}
275
276void ppc_compat_add_property(Object *obj, const char *name,
277 uint32_t *compat_pvr, const char *basedesc,
278 Error **errp)
279{
280 Error *local_err = NULL;
281 gchar *namesv[ARRAY_SIZE(compat_table) + 1];
282 gchar *names, *desc;
283 int i;
284
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;
290 }
291
292 for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
293 /*
294 * Have to discard const here, because g_strjoinv() takes
295 * (gchar **), not (const gchar **) :(
296 */
297 namesv[i] = (gchar *)compat_table[i].name;
298 }
299 namesv[ARRAY_SIZE(compat_table)] = NULL;
300
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);
304
305 g_free(names);
306 g_free(desc);
307
308out:
309 error_propagate(errp, local_err);
310}