xref: /openbmc/qemu/hw/ppc/spapr_caps.c (revision a78b1299)
1 /*
2  * QEMU PowerPC pSeries Logical Partition capabilities handling
3  *
4  * Copyright (c) 2017 David Gibson, Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu/osdep.h"
25 #include "qemu/error-report.h"
26 #include "qapi/error.h"
27 #include "qapi/visitor.h"
28 #include "sysemu/hw_accel.h"
29 #include "target/ppc/cpu.h"
30 #include "cpu-models.h"
31 #include "kvm_ppc.h"
32 
33 #include "hw/ppc/spapr.h"
34 
35 typedef struct sPAPRCapabilityInfo {
36     const char *name;
37     const char *description;
38     int index;
39 
40     /* Getter and Setter Function Pointers */
41     ObjectPropertyAccessor *get;
42     ObjectPropertyAccessor *set;
43     const char *type;
44     /* Make sure the virtual hardware can support this capability */
45     void (*apply)(sPAPRMachineState *spapr, uint8_t val, Error **errp);
46 } sPAPRCapabilityInfo;
47 
48 static void spapr_cap_get_bool(Object *obj, Visitor *v, const char *name,
49                                void *opaque, Error **errp)
50 {
51     sPAPRCapabilityInfo *cap = opaque;
52     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
53     bool value = spapr_get_cap(spapr, cap->index) == SPAPR_CAP_ON;
54 
55     visit_type_bool(v, name, &value, errp);
56 }
57 
58 static void spapr_cap_set_bool(Object *obj, Visitor *v, const char *name,
59                                void *opaque, Error **errp)
60 {
61     sPAPRCapabilityInfo *cap = opaque;
62     sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
63     bool value;
64     Error *local_err = NULL;
65 
66     visit_type_bool(v, name, &value, &local_err);
67     if (local_err) {
68         error_propagate(errp, local_err);
69         return;
70     }
71 
72     spapr->cmd_line_caps[cap->index] = true;
73     spapr->eff.caps[cap->index] = value ? SPAPR_CAP_ON : SPAPR_CAP_OFF;
74 }
75 
76 static void cap_htm_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
77 {
78     if (!val) {
79         /* TODO: We don't support disabling htm yet */
80         return;
81     }
82     if (tcg_enabled()) {
83         error_setg(errp,
84                    "No Transactional Memory support in TCG, try cap-htm=off");
85     } else if (kvm_enabled() && !kvmppc_has_cap_htm()) {
86         error_setg(errp,
87 "KVM implementation does not support Transactional Memory, try cap-htm=off"
88             );
89     }
90 }
91 
92 static void cap_vsx_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
93 {
94     PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
95     CPUPPCState *env = &cpu->env;
96 
97     if (!val) {
98         /* TODO: We don't support disabling vsx yet */
99         return;
100     }
101     /* Allowable CPUs in spapr_cpu_core.c should already have gotten
102      * rid of anything that doesn't do VMX */
103     g_assert(env->insns_flags & PPC_ALTIVEC);
104     if (!(env->insns_flags2 & PPC2_VSX)) {
105         error_setg(errp, "VSX support not available, try cap-vsx=off");
106     }
107 }
108 
109 static void cap_dfp_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
110 {
111     PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
112     CPUPPCState *env = &cpu->env;
113 
114     if (!val) {
115         /* TODO: We don't support disabling dfp yet */
116         return;
117     }
118     if (!(env->insns_flags2 & PPC2_DFP)) {
119         error_setg(errp, "DFP support not available, try cap-dfp=off");
120     }
121 }
122 
123 
124 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
125     [SPAPR_CAP_HTM] = {
126         .name = "htm",
127         .description = "Allow Hardware Transactional Memory (HTM)",
128         .index = SPAPR_CAP_HTM,
129         .get = spapr_cap_get_bool,
130         .set = spapr_cap_set_bool,
131         .type = "bool",
132         .apply = cap_htm_apply,
133     },
134     [SPAPR_CAP_VSX] = {
135         .name = "vsx",
136         .description = "Allow Vector Scalar Extensions (VSX)",
137         .index = SPAPR_CAP_VSX,
138         .get = spapr_cap_get_bool,
139         .set = spapr_cap_set_bool,
140         .type = "bool",
141         .apply = cap_vsx_apply,
142     },
143     [SPAPR_CAP_DFP] = {
144         .name = "dfp",
145         .description = "Allow Decimal Floating Point (DFP)",
146         .index = SPAPR_CAP_DFP,
147         .get = spapr_cap_get_bool,
148         .set = spapr_cap_set_bool,
149         .type = "bool",
150         .apply = cap_dfp_apply,
151     },
152 };
153 
154 static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
155                                                CPUState *cs)
156 {
157     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
158     PowerPCCPU *cpu = POWERPC_CPU(cs);
159     sPAPRCapabilities caps;
160 
161     caps = smc->default_caps;
162 
163     if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_07,
164                           0, spapr->max_compat_pvr)) {
165         caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
166     }
167 
168     if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_06,
169                           0, spapr->max_compat_pvr)) {
170         caps.caps[SPAPR_CAP_VSX] = SPAPR_CAP_OFF;
171         caps.caps[SPAPR_CAP_DFP] = SPAPR_CAP_OFF;
172     }
173 
174     return caps;
175 }
176 
177 int spapr_caps_pre_load(void *opaque)
178 {
179     sPAPRMachineState *spapr = opaque;
180 
181     /* Set to default so we can tell if this came in with the migration */
182     spapr->mig = spapr->def;
183     return 0;
184 }
185 
186 int spapr_caps_pre_save(void *opaque)
187 {
188     sPAPRMachineState *spapr = opaque;
189 
190     spapr->mig = spapr->eff;
191     return 0;
192 }
193 
194 /* This has to be called from the top-level spapr post_load, not the
195  * caps specific one.  Otherwise it wouldn't be called when the source
196  * caps are all defaults, which could still conflict with overridden
197  * caps on the destination */
198 int spapr_caps_post_migration(sPAPRMachineState *spapr)
199 {
200     int i;
201     bool ok = true;
202     sPAPRCapabilities dstcaps = spapr->eff;
203     sPAPRCapabilities srccaps;
204 
205     srccaps = default_caps_with_cpu(spapr, first_cpu);
206     for (i = 0; i < SPAPR_CAP_NUM; i++) {
207         /* If not default value then assume came in with the migration */
208         if (spapr->mig.caps[i] != spapr->def.caps[i]) {
209             srccaps.caps[i] = spapr->mig.caps[i];
210         }
211     }
212 
213     for (i = 0; i < SPAPR_CAP_NUM; i++) {
214         sPAPRCapabilityInfo *info = &capability_table[i];
215 
216         if (srccaps.caps[i] > dstcaps.caps[i]) {
217             error_report("cap-%s higher level (%d) in incoming stream than on destination (%d)",
218                          info->name, srccaps.caps[i], dstcaps.caps[i]);
219             ok = false;
220         }
221 
222         if (srccaps.caps[i] < dstcaps.caps[i]) {
223             warn_report("cap-%s lower level (%d) in incoming stream than on destination (%d)",
224                          info->name, srccaps.caps[i], dstcaps.caps[i]);
225         }
226     }
227 
228     return ok ? 0 : -EINVAL;
229 }
230 
231 /* Used to generate the migration field and needed function for a spapr cap */
232 #define SPAPR_CAP_MIG_STATE(cap, ccap)                  \
233 static bool spapr_cap_##cap##_needed(void *opaque)      \
234 {                                                       \
235     sPAPRMachineState *spapr = opaque;                  \
236                                                         \
237     return spapr->cmd_line_caps[SPAPR_CAP_##ccap] &&    \
238            (spapr->eff.caps[SPAPR_CAP_##ccap] !=        \
239             spapr->def.caps[SPAPR_CAP_##ccap]);         \
240 }                                                       \
241                                                         \
242 const VMStateDescription vmstate_spapr_cap_##cap = {    \
243     .name = "spapr/cap/" #cap,                          \
244     .version_id = 1,                                    \
245     .minimum_version_id = 1,                            \
246     .needed = spapr_cap_##cap##_needed,                 \
247     .fields = (VMStateField[]) {                        \
248         VMSTATE_UINT8(mig.caps[SPAPR_CAP_##ccap],       \
249                       sPAPRMachineState),               \
250         VMSTATE_END_OF_LIST()                           \
251     },                                                  \
252 }
253 
254 SPAPR_CAP_MIG_STATE(htm, HTM);
255 SPAPR_CAP_MIG_STATE(vsx, VSX);
256 SPAPR_CAP_MIG_STATE(dfp, DFP);
257 
258 void spapr_caps_reset(sPAPRMachineState *spapr)
259 {
260     sPAPRCapabilities default_caps;
261     int i;
262 
263     /* First compute the actual set of caps we're running with.. */
264     default_caps = default_caps_with_cpu(spapr, first_cpu);
265 
266     for (i = 0; i < SPAPR_CAP_NUM; i++) {
267         /* Store the defaults */
268         spapr->def.caps[i] = default_caps.caps[i];
269         /* If not set on the command line then apply the default value */
270         if (!spapr->cmd_line_caps[i]) {
271             spapr->eff.caps[i] = default_caps.caps[i];
272         }
273     }
274 
275     /* .. then apply those caps to the virtual hardware */
276 
277     for (i = 0; i < SPAPR_CAP_NUM; i++) {
278         sPAPRCapabilityInfo *info = &capability_table[i];
279 
280         /*
281          * If the apply function can't set the desired level and thinks it's
282          * fatal, it should cause that.
283          */
284         info->apply(spapr, spapr->eff.caps[i], &error_fatal);
285     }
286 }
287 
288 void spapr_caps_add_properties(sPAPRMachineClass *smc, Error **errp)
289 {
290     Error *local_err = NULL;
291     ObjectClass *klass = OBJECT_CLASS(smc);
292     int i;
293 
294     for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
295         sPAPRCapabilityInfo *cap = &capability_table[i];
296         const char *name = g_strdup_printf("cap-%s", cap->name);
297         char *desc;
298 
299         object_class_property_add(klass, name, cap->type,
300                                   cap->get, cap->set,
301                                   NULL, cap, &local_err);
302         if (local_err) {
303             error_propagate(errp, local_err);
304             return;
305         }
306 
307         desc = g_strdup_printf("%s", cap->description);
308         object_class_property_set_description(klass, name, desc, &local_err);
309         g_free(desc);
310         if (local_err) {
311             error_propagate(errp, local_err);
312             return;
313         }
314     }
315 }
316