xref: /qemu/target/ppc/compat.c (revision 6402cbbb)
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"
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 
30 typedef 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 
38 static const CompatInfo compat_table[] = {
39     /*
40      * Ordered from oldest to newest - the code relies on this
41      */
42     { /* POWER6, ISA2.05 */
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     { /* POWER7, ISA2.06 */
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     { /* POWER8, ISA2.07 */
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     { /* POWER9, ISA3.00 */
72         .name = "power9",
73         .pvr = CPU_POWERPC_LOGICAL_3_00,
74         .pcr = PCR_COMPAT_3_00,
75         .pcr_level = PCR_COMPAT_3_00,
76         .max_threads = 4,
77     },
78 };
79 
80 static const CompatInfo *compat_by_pvr(uint32_t pvr)
81 {
82     int i;
83 
84     for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
85         if (compat_table[i].pvr == pvr) {
86             return &compat_table[i];
87         }
88     }
89     return NULL;
90 }
91 
92 bool ppc_check_compat(PowerPCCPU *cpu, uint32_t compat_pvr,
93                       uint32_t min_compat_pvr, uint32_t max_compat_pvr)
94 {
95     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
96     const CompatInfo *compat = compat_by_pvr(compat_pvr);
97     const CompatInfo *min = compat_by_pvr(min_compat_pvr);
98     const CompatInfo *max = compat_by_pvr(max_compat_pvr);
99 
100 #if !defined(CONFIG_USER_ONLY)
101     g_assert(cpu->vhyp);
102 #endif
103     g_assert(!min_compat_pvr || min);
104     g_assert(!max_compat_pvr || max);
105 
106     if (!compat) {
107         /* Not a recognized logical PVR */
108         return false;
109     }
110     if ((min && (compat < min)) || (max && (compat > max))) {
111         /* Outside specified range */
112         return false;
113     }
114     if (!(pcc->pcr_supported & compat->pcr_level)) {
115         /* Not supported by this CPU */
116         return false;
117     }
118     return true;
119 }
120 
121 void ppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr, Error **errp)
122 {
123     const CompatInfo *compat = compat_by_pvr(compat_pvr);
124     CPUPPCState *env = &cpu->env;
125     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
126     uint64_t pcr;
127 
128     if (!compat_pvr) {
129         pcr = 0;
130     } else if (!compat) {
131         error_setg(errp, "Unknown compatibility PVR 0x%08"PRIx32, compat_pvr);
132         return;
133     } else if (!ppc_check_compat(cpu, compat_pvr, 0, 0)) {
134         error_setg(errp, "Compatibility PVR 0x%08"PRIx32" not valid for CPU",
135                    compat_pvr);
136         return;
137     } else {
138         pcr = compat->pcr;
139     }
140 
141     cpu_synchronize_state(CPU(cpu));
142 
143     if (kvm_enabled() && cpu->compat_pvr != compat_pvr) {
144         int ret = kvmppc_set_compat(cpu, cpu->compat_pvr);
145         if (ret < 0) {
146             error_setg_errno(errp, -ret,
147                              "Unable to set CPU compatibility mode in KVM");
148             return;
149         }
150     }
151 
152     cpu->compat_pvr = compat_pvr;
153     env->spr[SPR_PCR] = pcr & pcc->pcr_mask;
154 }
155 
156 typedef struct {
157     uint32_t compat_pvr;
158     Error *err;
159 } SetCompatState;
160 
161 static void do_set_compat(CPUState *cs, run_on_cpu_data arg)
162 {
163     PowerPCCPU *cpu = POWERPC_CPU(cs);
164     SetCompatState *s = arg.host_ptr;
165 
166     ppc_set_compat(cpu, s->compat_pvr, &s->err);
167 }
168 
169 void ppc_set_compat_all(uint32_t compat_pvr, Error **errp)
170 {
171     CPUState *cs;
172 
173     CPU_FOREACH(cs) {
174         SetCompatState s = {
175             .compat_pvr = compat_pvr,
176             .err = NULL,
177         };
178 
179         run_on_cpu(cs, do_set_compat, RUN_ON_CPU_HOST_PTR(&s));
180 
181         if (s.err) {
182             error_propagate(errp, s.err);
183             return;
184         }
185     }
186 }
187 
188 int ppc_compat_max_threads(PowerPCCPU *cpu)
189 {
190     const CompatInfo *compat = compat_by_pvr(cpu->compat_pvr);
191     int n_threads = CPU(cpu)->nr_threads;
192 
193     if (cpu->compat_pvr) {
194         g_assert(compat);
195         n_threads = MIN(n_threads, compat->max_threads);
196     }
197 
198     return n_threads;
199 }
200 
201 static void ppc_compat_prop_get(Object *obj, Visitor *v, const char *name,
202                                 void *opaque, Error **errp)
203 {
204     uint32_t compat_pvr = *((uint32_t *)opaque);
205     const char *value;
206 
207     if (!compat_pvr) {
208         value = "";
209     } else {
210         const CompatInfo *compat = compat_by_pvr(compat_pvr);
211 
212         g_assert(compat);
213 
214         value = compat->name;
215     }
216 
217     visit_type_str(v, name, (char **)&value, errp);
218 }
219 
220 static void ppc_compat_prop_set(Object *obj, Visitor *v, const char *name,
221                                 void *opaque, Error **errp)
222 {
223     Error *local_err = NULL;
224     char *value;
225     uint32_t compat_pvr;
226 
227     visit_type_str(v, name, &value, &local_err);
228     if (local_err) {
229         error_propagate(errp, local_err);
230         return;
231     }
232 
233     if (strcmp(value, "") == 0) {
234         compat_pvr = 0;
235     } else {
236         int i;
237         const CompatInfo *compat = NULL;
238 
239         for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
240             if (strcmp(value, compat_table[i].name) == 0) {
241                 compat = &compat_table[i];
242                 break;
243 
244             }
245         }
246 
247         if (!compat) {
248             error_setg(errp, "Invalid compatibility mode \"%s\"", value);
249             goto out;
250         }
251         compat_pvr = compat->pvr;
252     }
253 
254     *((uint32_t *)opaque) = compat_pvr;
255 
256 out:
257     g_free(value);
258 }
259 
260 void ppc_compat_add_property(Object *obj, const char *name,
261                              uint32_t *compat_pvr, const char *basedesc,
262                              Error **errp)
263 {
264     Error *local_err = NULL;
265     gchar *namesv[ARRAY_SIZE(compat_table) + 1];
266     gchar *names, *desc;
267     int i;
268 
269     object_property_add(obj, name, "string",
270                         ppc_compat_prop_get, ppc_compat_prop_set, NULL,
271                         compat_pvr, &local_err);
272     if (local_err) {
273         goto out;
274     }
275 
276     for (i = 0; i < ARRAY_SIZE(compat_table); i++) {
277         /*
278          * Have to discard const here, because g_strjoinv() takes
279          * (gchar **), not (const gchar **) :(
280          */
281         namesv[i] = (gchar *)compat_table[i].name;
282     }
283     namesv[ARRAY_SIZE(compat_table)] = NULL;
284 
285     names = g_strjoinv(", ", namesv);
286     desc = g_strdup_printf("%s. Valid values are %s.", basedesc, names);
287     object_property_set_description(obj, name, desc, &local_err);
288 
289     g_free(names);
290     g_free(desc);
291 
292 out:
293     error_propagate(errp, local_err);
294 }
295