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 "exec/ram_addr.h"
30 #include "target/ppc/cpu.h"
31 #include "target/ppc/mmu-hash64.h"
32 #include "cpu-models.h"
35 #include "hw/ppc/spapr.h"
37 typedef struct sPAPRCapPossible
{
38 int num
; /* size of vals array below */
39 const char *help
; /* help text for vals */
42 * - because of the way compatibility is determined vals MUST be ordered
43 * such that later options are a superset of all preceding options.
44 * - the order of vals must be preserved, that is their index is important,
45 * however vals may be added to the end of the list so long as the above
51 typedef struct sPAPRCapabilityInfo
{
53 const char *description
;
56 /* Getter and Setter Function Pointers */
57 ObjectPropertyAccessor
*get
;
58 ObjectPropertyAccessor
*set
;
60 /* Possible values if this is a custom string type */
61 sPAPRCapPossible
*possible
;
62 /* Make sure the virtual hardware can support this capability */
63 void (*apply
)(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
);
64 void (*cpu_apply
)(sPAPRMachineState
*spapr
, PowerPCCPU
*cpu
,
65 uint8_t val
, Error
**errp
);
66 } sPAPRCapabilityInfo
;
68 static void spapr_cap_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
69 void *opaque
, Error
**errp
)
71 sPAPRCapabilityInfo
*cap
= opaque
;
72 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
73 bool value
= spapr_get_cap(spapr
, cap
->index
) == SPAPR_CAP_ON
;
75 visit_type_bool(v
, name
, &value
, errp
);
78 static void spapr_cap_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
79 void *opaque
, Error
**errp
)
81 sPAPRCapabilityInfo
*cap
= opaque
;
82 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
84 Error
*local_err
= NULL
;
86 visit_type_bool(v
, name
, &value
, &local_err
);
88 error_propagate(errp
, local_err
);
92 spapr
->cmd_line_caps
[cap
->index
] = true;
93 spapr
->eff
.caps
[cap
->index
] = value
? SPAPR_CAP_ON
: SPAPR_CAP_OFF
;
97 static void spapr_cap_get_string(Object
*obj
, Visitor
*v
, const char *name
,
98 void *opaque
, Error
**errp
)
100 sPAPRCapabilityInfo
*cap
= opaque
;
101 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
103 uint8_t value
= spapr_get_cap(spapr
, cap
->index
);
105 if (value
>= cap
->possible
->num
) {
106 error_setg(errp
, "Invalid value (%d) for cap-%s", value
, cap
->name
);
110 val
= g_strdup(cap
->possible
->vals
[value
]);
112 visit_type_str(v
, name
, &val
, errp
);
116 static void spapr_cap_set_string(Object
*obj
, Visitor
*v
, const char *name
,
117 void *opaque
, Error
**errp
)
119 sPAPRCapabilityInfo
*cap
= opaque
;
120 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
121 Error
*local_err
= NULL
;
125 visit_type_str(v
, name
, &val
, &local_err
);
127 error_propagate(errp
, local_err
);
131 if (!strcmp(val
, "?")) {
132 error_setg(errp
, "%s", cap
->possible
->help
);
135 for (i
= 0; i
< cap
->possible
->num
; i
++) {
136 if (!strcasecmp(val
, cap
->possible
->vals
[i
])) {
137 spapr
->cmd_line_caps
[cap
->index
] = true;
138 spapr
->eff
.caps
[cap
->index
] = i
;
143 error_setg(errp
, "Invalid capability mode \"%s\" for cap-%s", val
,
149 static void spapr_cap_get_pagesize(Object
*obj
, Visitor
*v
, const char *name
,
150 void *opaque
, Error
**errp
)
152 sPAPRCapabilityInfo
*cap
= opaque
;
153 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
154 uint8_t val
= spapr_get_cap(spapr
, cap
->index
);
155 uint64_t pagesize
= (1ULL << val
);
157 visit_type_size(v
, name
, &pagesize
, errp
);
160 static void spapr_cap_set_pagesize(Object
*obj
, Visitor
*v
, const char *name
,
161 void *opaque
, Error
**errp
)
163 sPAPRCapabilityInfo
*cap
= opaque
;
164 sPAPRMachineState
*spapr
= SPAPR_MACHINE(obj
);
167 Error
*local_err
= NULL
;
169 visit_type_size(v
, name
, &pagesize
, &local_err
);
171 error_propagate(errp
, local_err
);
175 if (!is_power_of_2(pagesize
)) {
176 error_setg(errp
, "cap-%s must be a power of 2", cap
->name
);
180 val
= ctz64(pagesize
);
181 spapr
->cmd_line_caps
[cap
->index
] = true;
182 spapr
->eff
.caps
[cap
->index
] = val
;
185 static void cap_htm_apply(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
)
188 /* TODO: We don't support disabling htm yet */
193 "No Transactional Memory support in TCG, try cap-htm=off");
194 } else if (kvm_enabled() && !kvmppc_has_cap_htm()) {
196 "KVM implementation does not support Transactional Memory, try cap-htm=off"
201 static void cap_vsx_apply(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
)
203 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
204 CPUPPCState
*env
= &cpu
->env
;
207 /* TODO: We don't support disabling vsx yet */
210 /* Allowable CPUs in spapr_cpu_core.c should already have gotten
211 * rid of anything that doesn't do VMX */
212 g_assert(env
->insns_flags
& PPC_ALTIVEC
);
213 if (!(env
->insns_flags2
& PPC2_VSX
)) {
214 error_setg(errp
, "VSX support not available, try cap-vsx=off");
218 static void cap_dfp_apply(sPAPRMachineState
*spapr
, uint8_t val
, Error
**errp
)
220 PowerPCCPU
*cpu
= POWERPC_CPU(first_cpu
);
221 CPUPPCState
*env
= &cpu
->env
;
224 /* TODO: We don't support disabling dfp yet */
227 if (!(env
->insns_flags2
& PPC2_DFP
)) {
228 error_setg(errp
, "DFP support not available, try cap-dfp=off");
232 sPAPRCapPossible cap_cfpc_possible
= {
234 .vals
= {"broken", "workaround", "fixed"},
235 .help
= "broken - no protection, workaround - workaround available,"
236 " fixed - fixed in hardware",
239 static void cap_safe_cache_apply(sPAPRMachineState
*spapr
, uint8_t val
,
242 uint8_t kvm_val
= kvmppc_get_cap_safe_cache();
244 if (tcg_enabled() && val
) {
245 /* TODO - for now only allow broken for TCG */
247 "Requested safe cache capability level not supported by tcg, try a different value for cap-cfpc");
248 } else if (kvm_enabled() && (val
> kvm_val
)) {
250 "Requested safe cache capability level not supported by kvm, try cap-cfpc=%s",
251 cap_cfpc_possible
.vals
[kvm_val
]);
255 sPAPRCapPossible cap_sbbc_possible
= {
257 .vals
= {"broken", "workaround", "fixed"},
258 .help
= "broken - no protection, workaround - workaround available,"
259 " fixed - fixed in hardware",
262 static void cap_safe_bounds_check_apply(sPAPRMachineState
*spapr
, uint8_t val
,
265 uint8_t kvm_val
= kvmppc_get_cap_safe_bounds_check();
267 if (tcg_enabled() && val
) {
268 /* TODO - for now only allow broken for TCG */
270 "Requested safe bounds check capability level not supported by tcg, try a different value for cap-sbbc");
271 } else if (kvm_enabled() && (val
> kvm_val
)) {
273 "Requested safe bounds check capability level not supported by kvm, try cap-sbbc=%s",
274 cap_sbbc_possible
.vals
[kvm_val
]);
278 sPAPRCapPossible cap_ibs_possible
= {
280 /* Note workaround only maintained for compatibility */
281 .vals
= {"broken", "workaround", "fixed-ibs", "fixed-ccd"},
282 .help
= "broken - no protection, fixed-ibs - indirect branch serialisation,"
283 " fixed-ccd - cache count disabled",
286 static void cap_safe_indirect_branch_apply(sPAPRMachineState
*spapr
,
287 uint8_t val
, Error
**errp
)
289 uint8_t kvm_val
= kvmppc_get_cap_safe_indirect_branch();
291 if (val
== SPAPR_CAP_WORKAROUND
) { /* Can only be Broken or Fixed */
293 "Requested safe indirect branch capability level \"workaround\" not valid, try cap-ibs=%s",
294 cap_ibs_possible
.vals
[kvm_val
]);
295 } else if (tcg_enabled() && val
) {
296 /* TODO - for now only allow broken for TCG */
298 "Requested safe indirect branch capability level not supported by tcg, try a different value for cap-ibs");
299 } else if (kvm_enabled() && val
&& (val
!= kvm_val
)) {
301 "Requested safe indirect branch capability level not supported by kvm, try cap-ibs=%s",
302 cap_ibs_possible
.vals
[kvm_val
]);
306 #define VALUE_DESC_TRISTATE " (broken, workaround, fixed)"
308 void spapr_check_pagesize(sPAPRMachineState
*spapr
, hwaddr pagesize
,
311 hwaddr maxpagesize
= (1ULL << spapr
->eff
.caps
[SPAPR_CAP_HPT_MAXPAGESIZE
]);
313 if (!kvmppc_hpt_needs_host_contiguous_pages()) {
317 if (maxpagesize
> pagesize
) {
319 "Can't support %"HWADDR_PRIu
" kiB guest pages with %"
320 HWADDR_PRIu
" kiB host pages with this KVM implementation",
321 maxpagesize
>> 10, pagesize
>> 10);
325 static void cap_hpt_maxpagesize_apply(sPAPRMachineState
*spapr
,
326 uint8_t val
, Error
**errp
)
329 error_setg(errp
, "Require at least 4kiB hpt-max-page-size");
331 } else if (val
< 16) {
332 warn_report("Many guests require at least 64kiB hpt-max-page-size");
335 spapr_check_pagesize(spapr
, qemu_getrampagesize(), errp
);
338 static bool spapr_pagesize_cb(void *opaque
, uint32_t seg_pshift
,
341 unsigned maxshift
= *((unsigned *)opaque
);
343 assert(pshift
>= seg_pshift
);
345 /* Don't allow the guest to use pages bigger than the configured
347 if (pshift
> maxshift
) {
351 /* For whatever reason, KVM doesn't allow multiple pagesizes
352 * within a segment, *except* for the case of 16M pages in a 4k or
353 * 64k segment. Always exclude other cases, so that TCG and KVM
354 * guests see a consistent environment */
355 if ((pshift
!= seg_pshift
) && (pshift
!= 24)) {
362 static void cap_hpt_maxpagesize_cpu_apply(sPAPRMachineState
*spapr
,
364 uint8_t val
, Error
**errp
)
366 unsigned maxshift
= val
;
368 ppc_hash64_filter_pagesizes(cpu
, spapr_pagesize_cb
, &maxshift
);
371 static void cap_nested_kvm_hv_apply(sPAPRMachineState
*spapr
,
372 uint8_t val
, Error
**errp
)
375 /* capability disabled by default */
381 "No Nested KVM-HV support in tcg, try cap-nested-hv=off");
382 } else if (kvm_enabled()) {
383 if (!kvmppc_has_cap_nested_kvm_hv()) {
385 "KVM implementation does not support Nested KVM-HV, try cap-nested-hv=off");
386 } else if (kvmppc_set_cap_nested_kvm_hv(val
) < 0) {
388 "Error enabling cap-nested-hv with KVM, try cap-nested-hv=off");
393 sPAPRCapabilityInfo capability_table
[SPAPR_CAP_NUM
] = {
396 .description
= "Allow Hardware Transactional Memory (HTM)",
397 .index
= SPAPR_CAP_HTM
,
398 .get
= spapr_cap_get_bool
,
399 .set
= spapr_cap_set_bool
,
401 .apply
= cap_htm_apply
,
405 .description
= "Allow Vector Scalar Extensions (VSX)",
406 .index
= SPAPR_CAP_VSX
,
407 .get
= spapr_cap_get_bool
,
408 .set
= spapr_cap_set_bool
,
410 .apply
= cap_vsx_apply
,
414 .description
= "Allow Decimal Floating Point (DFP)",
415 .index
= SPAPR_CAP_DFP
,
416 .get
= spapr_cap_get_bool
,
417 .set
= spapr_cap_set_bool
,
419 .apply
= cap_dfp_apply
,
423 .description
= "Cache Flush on Privilege Change" VALUE_DESC_TRISTATE
,
424 .index
= SPAPR_CAP_CFPC
,
425 .get
= spapr_cap_get_string
,
426 .set
= spapr_cap_set_string
,
428 .possible
= &cap_cfpc_possible
,
429 .apply
= cap_safe_cache_apply
,
433 .description
= "Speculation Barrier Bounds Checking" VALUE_DESC_TRISTATE
,
434 .index
= SPAPR_CAP_SBBC
,
435 .get
= spapr_cap_get_string
,
436 .set
= spapr_cap_set_string
,
438 .possible
= &cap_sbbc_possible
,
439 .apply
= cap_safe_bounds_check_apply
,
444 "Indirect Branch Speculation (broken, fixed-ibs, fixed-ccd)",
445 .index
= SPAPR_CAP_IBS
,
446 .get
= spapr_cap_get_string
,
447 .set
= spapr_cap_set_string
,
449 .possible
= &cap_ibs_possible
,
450 .apply
= cap_safe_indirect_branch_apply
,
452 [SPAPR_CAP_HPT_MAXPAGESIZE
] = {
453 .name
= "hpt-max-page-size",
454 .description
= "Maximum page size for Hash Page Table guests",
455 .index
= SPAPR_CAP_HPT_MAXPAGESIZE
,
456 .get
= spapr_cap_get_pagesize
,
457 .set
= spapr_cap_set_pagesize
,
459 .apply
= cap_hpt_maxpagesize_apply
,
460 .cpu_apply
= cap_hpt_maxpagesize_cpu_apply
,
462 [SPAPR_CAP_NESTED_KVM_HV
] = {
464 .description
= "Allow Nested KVM-HV",
465 .index
= SPAPR_CAP_NESTED_KVM_HV
,
466 .get
= spapr_cap_get_bool
,
467 .set
= spapr_cap_set_bool
,
469 .apply
= cap_nested_kvm_hv_apply
,
473 static sPAPRCapabilities
default_caps_with_cpu(sPAPRMachineState
*spapr
,
476 sPAPRMachineClass
*smc
= SPAPR_MACHINE_GET_CLASS(spapr
);
477 sPAPRCapabilities caps
;
479 caps
= smc
->default_caps
;
481 if (!ppc_type_check_compat(cputype
, CPU_POWERPC_LOGICAL_2_07
,
482 0, spapr
->max_compat_pvr
)) {
483 caps
.caps
[SPAPR_CAP_HTM
] = SPAPR_CAP_OFF
;
484 caps
.caps
[SPAPR_CAP_CFPC
] = SPAPR_CAP_BROKEN
;
487 if (!ppc_type_check_compat(cputype
, CPU_POWERPC_LOGICAL_2_06_PLUS
,
488 0, spapr
->max_compat_pvr
)) {
489 caps
.caps
[SPAPR_CAP_SBBC
] = SPAPR_CAP_BROKEN
;
492 if (!ppc_type_check_compat(cputype
, CPU_POWERPC_LOGICAL_2_06
,
493 0, spapr
->max_compat_pvr
)) {
494 caps
.caps
[SPAPR_CAP_VSX
] = SPAPR_CAP_OFF
;
495 caps
.caps
[SPAPR_CAP_DFP
] = SPAPR_CAP_OFF
;
496 caps
.caps
[SPAPR_CAP_IBS
] = SPAPR_CAP_BROKEN
;
499 /* This is for pseries-2.12 and older */
500 if (smc
->default_caps
.caps
[SPAPR_CAP_HPT_MAXPAGESIZE
] == 0) {
503 if (kvmppc_hpt_needs_host_contiguous_pages()) {
504 mps
= ctz64(qemu_getrampagesize());
506 mps
= 34; /* allow everything up to 16GiB, i.e. everything */
509 caps
.caps
[SPAPR_CAP_HPT_MAXPAGESIZE
] = mps
;
515 int spapr_caps_pre_load(void *opaque
)
517 sPAPRMachineState
*spapr
= opaque
;
519 /* Set to default so we can tell if this came in with the migration */
520 spapr
->mig
= spapr
->def
;
524 int spapr_caps_pre_save(void *opaque
)
526 sPAPRMachineState
*spapr
= opaque
;
528 spapr
->mig
= spapr
->eff
;
532 /* This has to be called from the top-level spapr post_load, not the
533 * caps specific one. Otherwise it wouldn't be called when the source
534 * caps are all defaults, which could still conflict with overridden
535 * caps on the destination */
536 int spapr_caps_post_migration(sPAPRMachineState
*spapr
)
540 sPAPRCapabilities dstcaps
= spapr
->eff
;
541 sPAPRCapabilities srccaps
;
543 srccaps
= default_caps_with_cpu(spapr
, MACHINE(spapr
)->cpu_type
);
544 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
545 /* If not default value then assume came in with the migration */
546 if (spapr
->mig
.caps
[i
] != spapr
->def
.caps
[i
]) {
547 srccaps
.caps
[i
] = spapr
->mig
.caps
[i
];
551 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
552 sPAPRCapabilityInfo
*info
= &capability_table
[i
];
554 if (srccaps
.caps
[i
] > dstcaps
.caps
[i
]) {
555 error_report("cap-%s higher level (%d) in incoming stream than on destination (%d)",
556 info
->name
, srccaps
.caps
[i
], dstcaps
.caps
[i
]);
560 if (srccaps
.caps
[i
] < dstcaps
.caps
[i
]) {
561 warn_report("cap-%s lower level (%d) in incoming stream than on destination (%d)",
562 info
->name
, srccaps
.caps
[i
], dstcaps
.caps
[i
]);
566 return ok
? 0 : -EINVAL
;
569 /* Used to generate the migration field and needed function for a spapr cap */
570 #define SPAPR_CAP_MIG_STATE(sname, cap) \
571 static bool spapr_cap_##sname##_needed(void *opaque) \
573 sPAPRMachineState *spapr = opaque; \
575 return spapr->cmd_line_caps[cap] && \
576 (spapr->eff.caps[cap] != \
577 spapr->def.caps[cap]); \
580 const VMStateDescription vmstate_spapr_cap_##sname = { \
581 .name = "spapr/cap/" #sname, \
583 .minimum_version_id = 1, \
584 .needed = spapr_cap_##sname##_needed, \
585 .fields = (VMStateField[]) { \
586 VMSTATE_UINT8(mig.caps[cap], \
587 sPAPRMachineState), \
588 VMSTATE_END_OF_LIST() \
592 SPAPR_CAP_MIG_STATE(htm
, SPAPR_CAP_HTM
);
593 SPAPR_CAP_MIG_STATE(vsx
, SPAPR_CAP_VSX
);
594 SPAPR_CAP_MIG_STATE(dfp
, SPAPR_CAP_DFP
);
595 SPAPR_CAP_MIG_STATE(cfpc
, SPAPR_CAP_CFPC
);
596 SPAPR_CAP_MIG_STATE(sbbc
, SPAPR_CAP_SBBC
);
597 SPAPR_CAP_MIG_STATE(ibs
, SPAPR_CAP_IBS
);
598 SPAPR_CAP_MIG_STATE(nested_kvm_hv
, SPAPR_CAP_NESTED_KVM_HV
);
600 void spapr_caps_init(sPAPRMachineState
*spapr
)
602 sPAPRCapabilities default_caps
;
605 /* Compute the actual set of caps we should run with */
606 default_caps
= default_caps_with_cpu(spapr
, MACHINE(spapr
)->cpu_type
);
608 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
609 /* Store the defaults */
610 spapr
->def
.caps
[i
] = default_caps
.caps
[i
];
611 /* If not set on the command line then apply the default value */
612 if (!spapr
->cmd_line_caps
[i
]) {
613 spapr
->eff
.caps
[i
] = default_caps
.caps
[i
];
618 void spapr_caps_apply(sPAPRMachineState
*spapr
)
622 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
623 sPAPRCapabilityInfo
*info
= &capability_table
[i
];
626 * If the apply function can't set the desired level and thinks it's
627 * fatal, it should cause that.
629 info
->apply(spapr
, spapr
->eff
.caps
[i
], &error_fatal
);
633 void spapr_caps_cpu_apply(sPAPRMachineState
*spapr
, PowerPCCPU
*cpu
)
637 for (i
= 0; i
< SPAPR_CAP_NUM
; i
++) {
638 sPAPRCapabilityInfo
*info
= &capability_table
[i
];
641 * If the apply function can't set the desired level and thinks it's
642 * fatal, it should cause that.
644 if (info
->cpu_apply
) {
645 info
->cpu_apply(spapr
, cpu
, spapr
->eff
.caps
[i
], &error_fatal
);
650 void spapr_caps_add_properties(sPAPRMachineClass
*smc
, Error
**errp
)
652 Error
*local_err
= NULL
;
653 ObjectClass
*klass
= OBJECT_CLASS(smc
);
656 for (i
= 0; i
< ARRAY_SIZE(capability_table
); i
++) {
657 sPAPRCapabilityInfo
*cap
= &capability_table
[i
];
658 const char *name
= g_strdup_printf("cap-%s", cap
->name
);
661 object_class_property_add(klass
, name
, cap
->type
,
663 NULL
, cap
, &local_err
);
665 error_propagate(errp
, local_err
);
669 desc
= g_strdup_printf("%s", cap
->description
);
670 object_class_property_set_description(klass
, name
, desc
, &local_err
);
673 error_propagate(errp
, local_err
);