2 * QEMU PowerPC pSeries Logical Partition capabilities handling
4 * Copyright (c) 2017 David Gibson, Red Hat Inc.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
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 "target/ppc/mmu-hash64.h"
31 #include "cpu-models.h"
34 #include "hw/ppc/spapr.h"
36 typedef struct sPAPRCapPossible
{
37 int num
; /* size of vals array below */
38 const char *help
; /* help text for vals */
41 * - because of the way compatibility is determined vals MUST be ordered
42 * such that later options are a superset of all preceding options.
43 * - the order of vals must be preserved, that is their index is important,
44 * however vals may be added to the end of the list so long as the above
50 typedef struct sPAPRCapabilityInfo
{
52 const char *description
;
55 /* Getter and Setter Function Pointers */
56 ObjectPropertyAccessor
*get
;
57 ObjectPropertyAccessor
*set
;
59 /* Possible values if this is a custom string type */
60 sPAPRCapPossible
*possible
;
61 /* Make sure the virtual hardware can support this capability */
62 void (*apply
)(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
);
63 void (*cpu_apply
)(sPAPRMachineState
*spapr
, PowerPCCPU
*cpu
,
64 uint8_t val
, Error
**errp
);
65 } sPAPRCapabilityInfo
;
67 static void spapr_cap_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
68 void *opaque
, Error
**errp
)
70 sPAPRCapabilityInfo
*cap
= opaque
;
71 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
72 bool value
= spapr_get_cap(spapr
, cap
->index
) == SPAPR_CAP_ON
;
74 visit_type_bool(v
, name
, &value
, errp
);
77 static void spapr_cap_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
78 void *opaque
, Error
**errp
)
80 sPAPRCapabilityInfo
*cap
= opaque
;
81 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
83 Error
*local_err
= NULL
;
85 visit_type_bool(v
, name
, &value
, &local_err
);
87 error_propagate(errp
, local_err
);
91 spapr
->cmd_line_caps
[cap
->index
] = true;
92 spapr
->eff
.caps
[cap
->index
] = value
? SPAPR_CAP_ON
: SPAPR_CAP_OFF
;
96 static void spapr_cap_get_string(Object
*obj
, Visitor
*v
, const char *name
,
97 void *opaque
, Error
**errp
)
99 sPAPRCapabilityInfo
*cap
= opaque
;
100 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
102 uint8_t value
= spapr_get_cap(spapr
, cap
->index
);
104 if (value
>= cap
->possible
->num
) {
105 error_setg(errp
, "Invalid value (%d) for cap-%s", value
, cap
->name
);
109 val
= g_strdup(cap
->possible
->vals
[value
]);
111 visit_type_str(v
, name
, &val
, errp
);
115 static void spapr_cap_set_string(Object
*obj
, Visitor
*v
, const char *name
,
116 void *opaque
, Error
**errp
)
118 sPAPRCapabilityInfo
*cap
= opaque
;
119 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
120 Error
*local_err
= NULL
;
124 visit_type_str(v
, name
, &val
, &local_err
);
126 error_propagate(errp
, local_err
);
130 if (!strcmp(val
, "?")) {
131 error_setg(errp
, "%s", cap
->possible
->help
);
134 for (i
= 0; i
< cap
->possible
->num
; i
++) {
135 if (!strcasecmp(val
, cap
->possible
->vals
[i
])) {
136 spapr
->cmd_line_caps
[cap
->index
] = true;
137 spapr
->eff
.caps
[cap
->index
] = i
;
142 error_setg(errp
, "Invalid capability mode \"%s\" for cap-%s", val
,
148 static void spapr_cap_get_pagesize(Object
*obj
, Visitor
*v
, const char *name
,
149 void *opaque
, Error
**errp
)
151 sPAPRCapabilityInfo
*cap
= opaque
;
152 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
153 uint8_t val
= spapr_get_cap(spapr
, cap
->index
);
154 uint64_t pagesize
= (1ULL << val
);
156 visit_type_size(v
, name
, &pagesize
, errp
);
159 static void spapr_cap_set_pagesize(Object
*obj
, Visitor
*v
, const char *name
,
160 void *opaque
, Error
**errp
)
162 sPAPRCapabilityInfo
*cap
= opaque
;
163 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
166 Error
*local_err
= NULL
;
168 visit_type_size(v
, name
, &pagesize
, &local_err
);
170 error_propagate(errp
, local_err
);
174 if (!is_power_of_2(pagesize
)) {
175 error_setg(errp
, "cap-%s must be a power of 2", cap
->name
);
179 val
= ctz64(pagesize
);
180 spapr
->cmd_line_caps
[cap
->index
] = true;
181 spapr
->eff
.caps
[cap
->index
] = val
;
184 static void cap_htm_apply(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
)
187 /* TODO: We don't support disabling htm yet */
192 "No Transactional Memory support in TCG, try cap-htm=off");
193 } else if (kvm_enabled() && !kvmppc_has_cap_htm()) {
195 "KVM implementation does not support Transactional Memory, try cap-htm=off"
200 static void cap_vsx_apply(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
)
202 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
203 CPUPPCState
*env
= &cpu
->env
;
206 /* TODO: We don't support disabling vsx yet */
209 /* Allowable CPUs in spapr_cpu_core.c should already have gotten
210 * rid of anything that doesn't do VMX */
211 g_assert(env
->insns_flags
& PPC_ALTIVEC
);
212 if (!(env
->insns_flags2
& PPC2_VSX
)) {
213 error_setg(errp
, "VSX support not available, try cap-vsx=off");
217 static void cap_dfp_apply(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
)
219 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
220 CPUPPCState
*env
= &cpu
->env
;
223 /* TODO: We don't support disabling dfp yet */
226 if (!(env
->insns_flags2
& PPC2_DFP
)) {
227 error_setg(errp
, "DFP support not available, try cap-dfp=off");
231 sPAPRCapPossible cap_cfpc_possible
= {
233 .vals
= {"broken", "workaround", "fixed"},
234 .help
= "broken - no protection, workaround - workaround available,"
235 " fixed - fixed in hardware",
238 static void cap_safe_cache_apply(sPAPRMachineState
*spapr
, uint8_t val
,
241 uint8_t kvm_val
= kvmppc_get_cap_safe_cache();
243 if (tcg_enabled() && val
) {
244 /* TODO - for now only allow broken for TCG */
246 "Requested safe cache capability level not supported by tcg, try a different value for cap-cfpc");
247 } else if (kvm_enabled() && (val
> kvm_val
)) {
249 "Requested safe cache capability level not supported by kvm, try cap-cfpc=%s",
250 cap_cfpc_possible
.vals
[kvm_val
]);
254 sPAPRCapPossible cap_sbbc_possible
= {
256 .vals
= {"broken", "workaround", "fixed"},
257 .help
= "broken - no protection, workaround - workaround available,"
258 " fixed - fixed in hardware",
261 static void cap_safe_bounds_check_apply(sPAPRMachineState
*spapr
, uint8_t val
,
264 uint8_t kvm_val
= kvmppc_get_cap_safe_bounds_check();
266 if (tcg_enabled() && val
) {
267 /* TODO - for now only allow broken for TCG */
269 "Requested safe bounds check capability level not supported by tcg, try a different value for cap-sbbc");
270 } else if (kvm_enabled() && (val
> kvm_val
)) {
272 "Requested safe bounds check capability level not supported by kvm, try cap-sbbc=%s",
273 cap_sbbc_possible
.vals
[kvm_val
]);
277 sPAPRCapPossible cap_ibs_possible
= {
279 /* Note workaround only maintained for compatibility */
280 .vals
= {"broken", "workaround", "fixed-ibs", "fixed-ccd"},
281 .help
= "broken - no protection, fixed-ibs - indirect branch serialisation,"
282 " fixed-ccd - cache count disabled",
285 static void cap_safe_indirect_branch_apply(sPAPRMachineState
*spapr
,
286 uint8_t val
, Error
**errp
)
288 uint8_t kvm_val
= kvmppc_get_cap_safe_indirect_branch();
290 if (val
== SPAPR_CAP_WORKAROUND
) { /* Can only be Broken or Fixed */
292 "Requested safe indirect branch capability level \"workaround\" not valid, try cap-ibs=%s",
293 cap_ibs_possible
.vals
[kvm_val
]);
294 } else if (tcg_enabled() && val
) {
295 /* TODO - for now only allow broken for TCG */
297 "Requested safe indirect branch capability level not supported by tcg, try a different value for cap-ibs");
298 } else if (kvm_enabled() && val
&& (val
!= kvm_val
)) {
300 "Requested safe indirect branch capability level not supported by kvm, try cap-ibs=%s",
301 cap_ibs_possible
.vals
[kvm_val
]);
305 #define VALUE_DESC_TRISTATE " (broken, workaround, fixed)"
307 static void cap_hpt_maxpagesize_apply(sPAPRMachineState
*spapr
,
308 uint8_t val
, Error
**errp
)
311 error_setg(errp
, "Require at least 4kiB hpt-max-page-size");
312 } else if (val
< 16) {
313 warn_report("Many guests require at least 64kiB hpt-max-page-size");
317 sPAPRCapabilityInfo capability_table
[SPAPR_CAP_NUM
] = {
320 .description
= "Allow Hardware Transactional Memory (HTM)",
321 .index
= SPAPR_CAP_HTM
,
322 .get
= spapr_cap_get_bool
,
323 .set
= spapr_cap_set_bool
,
325 .apply
= cap_htm_apply
,
329 .description
= "Allow Vector Scalar Extensions (VSX)",
330 .index
= SPAPR_CAP_VSX
,
331 .get
= spapr_cap_get_bool
,
332 .set
= spapr_cap_set_bool
,
334 .apply
= cap_vsx_apply
,
338 .description
= "Allow Decimal Floating Point (DFP)",
339 .index
= SPAPR_CAP_DFP
,
340 .get
= spapr_cap_get_bool
,
341 .set
= spapr_cap_set_bool
,
343 .apply
= cap_dfp_apply
,
347 .description
= "Cache Flush on Privilege Change" VALUE_DESC_TRISTATE
,
348 .index
= SPAPR_CAP_CFPC
,
349 .get
= spapr_cap_get_string
,
350 .set
= spapr_cap_set_string
,
352 .possible
= &cap_cfpc_possible
,
353 .apply
= cap_safe_cache_apply
,
357 .description
= "Speculation Barrier Bounds Checking" VALUE_DESC_TRISTATE
,
358 .index
= SPAPR_CAP_SBBC
,
359 .get
= spapr_cap_get_string
,
360 .set
= spapr_cap_set_string
,
362 .possible
= &cap_sbbc_possible
,
363 .apply
= cap_safe_bounds_check_apply
,
368 "Indirect Branch Speculation (broken, fixed-ibs, fixed-ccd)",
369 .index
= SPAPR_CAP_IBS
,
370 .get
= spapr_cap_get_string
,
371 .set
= spapr_cap_set_string
,
373 .possible
= &cap_ibs_possible
,
374 .apply
= cap_safe_indirect_branch_apply
,
376 [SPAPR_CAP_HPT_MAXPAGESIZE
] = {
377 .name
= "hpt-max-page-size",
378 .description
= "Maximum page size for Hash Page Table guests",
379 .index
= SPAPR_CAP_HPT_MAXPAGESIZE
,
380 .get
= spapr_cap_get_pagesize
,
381 .set
= spapr_cap_set_pagesize
,
383 .apply
= cap_hpt_maxpagesize_apply
,
387 static sPAPRCapabilities
default_caps_with_cpu(sPAPRMachineState
*spapr
,
390 sPAPRMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
391 sPAPRCapabilities caps
;
393 caps
= smc
->default_caps
;
395 if (!ppc_type_check_compat(cputype
, CPU_POWERPC_LOGICAL_2_07
,
396 0, spapr
->max_compat_pvr
)) {
397 caps
.caps
[SPAPR_CAP_HTM
] = SPAPR_CAP_OFF
;
398 caps
.caps
[SPAPR_CAP_CFPC
] = SPAPR_CAP_BROKEN
;
401 if (!ppc_type_check_compat(cputype
, CPU_POWERPC_LOGICAL_2_06_PLUS
,
402 0, spapr
->max_compat_pvr
)) {
403 caps
.caps
[SPAPR_CAP_SBBC
] = SPAPR_CAP_BROKEN
;
406 if (!ppc_type_check_compat(cputype
, CPU_POWERPC_LOGICAL_2_06
,
407 0, spapr
->max_compat_pvr
)) {
408 caps
.caps
[SPAPR_CAP_VSX
] = SPAPR_CAP_OFF
;
409 caps
.caps
[SPAPR_CAP_DFP
] = SPAPR_CAP_OFF
;
410 caps
.caps
[SPAPR_CAP_IBS
] = SPAPR_CAP_BROKEN
;
416 int spapr_caps_pre_load(void *opaque
)
418 sPAPRMachineState
*spapr
= opaque
;
420 /* Set to default so we can tell if this came in with the migration */
421 spapr
->mig
= spapr
->def
;
425 int spapr_caps_pre_save(void *opaque
)
427 sPAPRMachineState
*spapr
= opaque
;
429 spapr
->mig
= spapr
->eff
;
433 /* This has to be called from the top-level spapr post_load, not the
434 * caps specific one. Otherwise it wouldn't be called when the source
435 * caps are all defaults, which could still conflict with overridden
436 * caps on the destination */
437 int spapr_caps_post_migration(sPAPRMachineState
*spapr
)
441 sPAPRCapabilities dstcaps
= spapr
->eff
;
442 sPAPRCapabilities srccaps
;
444 srccaps
= default_caps_with_cpu(spapr
, MACHINE(spapr
)->cpu_type
);
445 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
446 /* If not default value then assume came in with the migration */
447 if (spapr
->mig
.caps
[i
] != spapr
->def
.caps
[i
]) {
448 srccaps
.caps
[i
] = spapr
->mig
.caps
[i
];
452 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
453 sPAPRCapabilityInfo
*info
= &capability_table
[i
];
455 if (srccaps
.caps
[i
] > dstcaps
.caps
[i
]) {
456 error_report("cap-%s higher level (%d) in incoming stream than on destination (%d)",
457 info
->name
, srccaps
.caps
[i
], dstcaps
.caps
[i
]);
461 if (srccaps
.caps
[i
] < dstcaps
.caps
[i
]) {
462 warn_report("cap-%s lower level (%d) in incoming stream than on destination (%d)",
463 info
->name
, srccaps
.caps
[i
], dstcaps
.caps
[i
]);
467 return ok
? 0 : -EINVAL
;
470 /* Used to generate the migration field and needed function for a spapr cap */
471 #define SPAPR_CAP_MIG_STATE(sname, cap) \
472 static bool spapr_cap_##sname##_needed(void *opaque) \
474 sPAPRMachineState *spapr = opaque; \
476 return spapr->cmd_line_caps[cap] && \
477 (spapr->eff.caps[cap] != \
478 spapr->def.caps[cap]); \
481 const VMStateDescription vmstate_spapr_cap_##sname = { \
482 .name = "spapr/cap/" #sname, \
484 .minimum_version_id = 1, \
485 .needed = spapr_cap_##sname##_needed, \
486 .fields = (VMStateField[]) { \
487 VMSTATE_UINT8(mig.caps[cap], \
488 sPAPRMachineState), \
489 VMSTATE_END_OF_LIST() \
493 SPAPR_CAP_MIG_STATE(htm
, SPAPR_CAP_HTM
);
494 SPAPR_CAP_MIG_STATE(vsx
, SPAPR_CAP_VSX
);
495 SPAPR_CAP_MIG_STATE(dfp
, SPAPR_CAP_DFP
);
496 SPAPR_CAP_MIG_STATE(cfpc
, SPAPR_CAP_CFPC
);
497 SPAPR_CAP_MIG_STATE(sbbc
, SPAPR_CAP_SBBC
);
498 SPAPR_CAP_MIG_STATE(ibs
, SPAPR_CAP_IBS
);
500 void spapr_caps_init(sPAPRMachineState
*spapr
)
502 sPAPRCapabilities default_caps
;
505 /* Compute the actual set of caps we should run with */
506 default_caps
= default_caps_with_cpu(spapr
, MACHINE(spapr
)->cpu_type
);
508 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
509 /* Store the defaults */
510 spapr
->def
.caps
[i
] = default_caps
.caps
[i
];
511 /* If not set on the command line then apply the default value */
512 if (!spapr
->cmd_line_caps
[i
]) {
513 spapr
->eff
.caps
[i
] = default_caps
.caps
[i
];
518 void spapr_caps_apply(sPAPRMachineState
*spapr
)
522 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
523 sPAPRCapabilityInfo
*info
= &capability_table
[i
];
526 * If the apply function can't set the desired level and thinks it's
527 * fatal, it should cause that.
529 info
->apply(spapr
, spapr
->eff
.caps
[i
], &error_fatal
);
533 void spapr_caps_cpu_apply(sPAPRMachineState
*spapr
, PowerPCCPU
*cpu
)
537 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
538 sPAPRCapabilityInfo
*info
= &capability_table
[i
];
541 * If the apply function can't set the desired level and thinks it's
542 * fatal, it should cause that.
544 if (info
->cpu_apply
) {
545 info
->cpu_apply(spapr
, cpu
, spapr
->eff
.caps
[i
], &error_fatal
);
550 void spapr_caps_add_properties(sPAPRMachineClass
*smc
, Error
**errp
)
552 Error
*local_err
= NULL
;
553 ObjectClass
*klass
= OBJECT_CLASS(smc
);
556 for (i
= 0; i
< ARRAY_SIZE(capability_table
); i
++) {
557 sPAPRCapabilityInfo
*cap
= &capability_table
[i
];
558 const char *name
= g_strdup_printf("cap-%s", cap
->name
);
561 object_class_property_add(klass
, name
, cap
->type
,
563 NULL
, cap
, &local_err
);
565 error_propagate(errp
, local_err
);
569 desc
= g_strdup_printf("%s", cap
->description
);
570 object_class_property_set_description(klass
, name
, desc
, &local_err
);
573 error_propagate(errp
, local_err
);