xref: /openbmc/qemu/hw/ppc/spapr_caps.c (revision ebe15582)
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 
25 #include "qemu/osdep.h"
26 #include "qemu/error-report.h"
27 #include "qapi/error.h"
28 #include "qapi/visitor.h"
29 #include "sysemu/hw_accel.h"
30 #include "exec/ram_addr.h"
31 #include "target/ppc/cpu.h"
32 #include "target/ppc/mmu-hash64.h"
33 #include "cpu-models.h"
34 #include "kvm_ppc.h"
35 #include "migration/vmstate.h"
36 #include "sysemu/qtest.h"
37 #include "sysemu/tcg.h"
38 
39 #include "hw/ppc/spapr.h"
40 
41 typedef struct SpaprCapPossible {
42     int num;            /* size of vals array below */
43     const char *help;   /* help text for vals */
44     /*
45      * Note:
46      * - because of the way compatibility is determined vals MUST be ordered
47      *   such that later options are a superset of all preceding options.
48      * - the order of vals must be preserved, that is their index is important,
49      *   however vals may be added to the end of the list so long as the above
50      *   point is observed
51      */
52     const char *vals[];
53 } SpaprCapPossible;
54 
55 typedef struct SpaprCapabilityInfo {
56     const char *name;
57     const char *description;
58     int index;
59 
60     /* Getter and Setter Function Pointers */
61     ObjectPropertyAccessor *get;
62     ObjectPropertyAccessor *set;
63     const char *type;
64     /* Possible values if this is a custom string type */
65     SpaprCapPossible *possible;
66     /* Make sure the virtual hardware can support this capability */
67     void (*apply)(SpaprMachineState *spapr, uint8_t val, Error **errp);
68     void (*cpu_apply)(SpaprMachineState *spapr, PowerPCCPU *cpu,
69                       uint8_t val, Error **errp);
70     bool (*migrate_needed)(void *opaque);
71 } SpaprCapabilityInfo;
72 
73 static void spapr_cap_get_bool(Object *obj, Visitor *v, const char *name,
74                                void *opaque, Error **errp)
75 {
76     SpaprCapabilityInfo *cap = opaque;
77     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
78     bool value = spapr_get_cap(spapr, cap->index) == SPAPR_CAP_ON;
79 
80     visit_type_bool(v, name, &value, errp);
81 }
82 
83 static void spapr_cap_set_bool(Object *obj, Visitor *v, const char *name,
84                                void *opaque, Error **errp)
85 {
86     SpaprCapabilityInfo *cap = opaque;
87     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
88     bool value;
89     Error *local_err = NULL;
90 
91     visit_type_bool(v, name, &value, &local_err);
92     if (local_err) {
93         error_propagate(errp, local_err);
94         return;
95     }
96 
97     spapr->cmd_line_caps[cap->index] = true;
98     spapr->eff.caps[cap->index] = value ? SPAPR_CAP_ON : SPAPR_CAP_OFF;
99 }
100 
101 
102 static void  spapr_cap_get_string(Object *obj, Visitor *v, const char *name,
103                                   void *opaque, Error **errp)
104 {
105     SpaprCapabilityInfo *cap = opaque;
106     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
107     char *val = NULL;
108     uint8_t value = spapr_get_cap(spapr, cap->index);
109 
110     if (value >= cap->possible->num) {
111         error_setg(errp, "Invalid value (%d) for cap-%s", value, cap->name);
112         return;
113     }
114 
115     val = g_strdup(cap->possible->vals[value]);
116 
117     visit_type_str(v, name, &val, errp);
118     g_free(val);
119 }
120 
121 static void spapr_cap_set_string(Object *obj, Visitor *v, const char *name,
122                                  void *opaque, Error **errp)
123 {
124     SpaprCapabilityInfo *cap = opaque;
125     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
126     Error *local_err = NULL;
127     uint8_t i;
128     char *val;
129 
130     visit_type_str(v, name, &val, &local_err);
131     if (local_err) {
132         error_propagate(errp, local_err);
133         return;
134     }
135 
136     if (!strcmp(val, "?")) {
137         error_setg(errp, "%s", cap->possible->help);
138         goto out;
139     }
140     for (i = 0; i < cap->possible->num; i++) {
141         if (!strcasecmp(val, cap->possible->vals[i])) {
142             spapr->cmd_line_caps[cap->index] = true;
143             spapr->eff.caps[cap->index] = i;
144             goto out;
145         }
146     }
147 
148     error_setg(errp, "Invalid capability mode \"%s\" for cap-%s", val,
149                cap->name);
150 out:
151     g_free(val);
152 }
153 
154 static void spapr_cap_get_pagesize(Object *obj, Visitor *v, const char *name,
155                                    void *opaque, Error **errp)
156 {
157     SpaprCapabilityInfo *cap = opaque;
158     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
159     uint8_t val = spapr_get_cap(spapr, cap->index);
160     uint64_t pagesize = (1ULL << val);
161 
162     visit_type_size(v, name, &pagesize, errp);
163 }
164 
165 static void spapr_cap_set_pagesize(Object *obj, Visitor *v, const char *name,
166                                    void *opaque, Error **errp)
167 {
168     SpaprCapabilityInfo *cap = opaque;
169     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
170     uint64_t pagesize;
171     uint8_t val;
172     Error *local_err = NULL;
173 
174     visit_type_size(v, name, &pagesize, &local_err);
175     if (local_err) {
176         error_propagate(errp, local_err);
177         return;
178     }
179 
180     if (!is_power_of_2(pagesize)) {
181         error_setg(errp, "cap-%s must be a power of 2", cap->name);
182         return;
183     }
184 
185     val = ctz64(pagesize);
186     spapr->cmd_line_caps[cap->index] = true;
187     spapr->eff.caps[cap->index] = val;
188 }
189 
190 static void cap_htm_apply(SpaprMachineState *spapr, uint8_t val, Error **errp)
191 {
192     if (!val) {
193         /* TODO: We don't support disabling htm yet */
194         return;
195     }
196     if (tcg_enabled()) {
197         error_setg(errp,
198                    "No Transactional Memory support in TCG,"
199                    " try appending -machine cap-htm=off");
200     } else if (kvm_enabled() && !kvmppc_has_cap_htm()) {
201         error_setg(errp,
202 "KVM implementation does not support Transactional Memory,"
203                    " try appending -machine cap-htm=off"
204             );
205     }
206 }
207 
208 static void cap_vsx_apply(SpaprMachineState *spapr, uint8_t val, Error **errp)
209 {
210     PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
211     CPUPPCState *env = &cpu->env;
212 
213     if (!val) {
214         /* TODO: We don't support disabling vsx yet */
215         return;
216     }
217     /* Allowable CPUs in spapr_cpu_core.c should already have gotten
218      * rid of anything that doesn't do VMX */
219     g_assert(env->insns_flags & PPC_ALTIVEC);
220     if (!(env->insns_flags2 & PPC2_VSX)) {
221         error_setg(errp, "VSX support not available,"
222                    " try appending -machine cap-vsx=off");
223     }
224 }
225 
226 static void cap_dfp_apply(SpaprMachineState *spapr, uint8_t val, Error **errp)
227 {
228     PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
229     CPUPPCState *env = &cpu->env;
230 
231     if (!val) {
232         /* TODO: We don't support disabling dfp yet */
233         return;
234     }
235     if (!(env->insns_flags2 & PPC2_DFP)) {
236         error_setg(errp, "DFP support not available,"
237                    " try appending -machine cap-dfp=off");
238     }
239 }
240 
241 SpaprCapPossible cap_cfpc_possible = {
242     .num = 3,
243     .vals = {"broken", "workaround", "fixed"},
244     .help = "broken - no protection, workaround - workaround available,"
245             " fixed - fixed in hardware",
246 };
247 
248 static void cap_safe_cache_apply(SpaprMachineState *spapr, uint8_t val,
249                                  Error **errp)
250 {
251     Error *local_err = NULL;
252     uint8_t kvm_val =  kvmppc_get_cap_safe_cache();
253 
254     if (tcg_enabled() && val) {
255         /* TCG only supports broken, allow other values and print a warning */
256         error_setg(&local_err,
257                    "TCG doesn't support requested feature, cap-cfpc=%s",
258                    cap_cfpc_possible.vals[val]);
259     } else if (kvm_enabled() && (val > kvm_val)) {
260         error_setg(errp,
261                    "Requested safe cache capability level not supported by kvm,"
262                    " try appending -machine cap-cfpc=%s",
263                    cap_cfpc_possible.vals[kvm_val]);
264     }
265 
266     if (local_err != NULL)
267         warn_report_err(local_err);
268 }
269 
270 SpaprCapPossible cap_sbbc_possible = {
271     .num = 3,
272     .vals = {"broken", "workaround", "fixed"},
273     .help = "broken - no protection, workaround - workaround available,"
274             " fixed - fixed in hardware",
275 };
276 
277 static void cap_safe_bounds_check_apply(SpaprMachineState *spapr, uint8_t val,
278                                         Error **errp)
279 {
280     Error *local_err = NULL;
281     uint8_t kvm_val =  kvmppc_get_cap_safe_bounds_check();
282 
283     if (tcg_enabled() && val) {
284         /* TCG only supports broken, allow other values and print a warning */
285         error_setg(&local_err,
286                    "TCG doesn't support requested feature, cap-sbbc=%s",
287                    cap_sbbc_possible.vals[val]);
288     } else if (kvm_enabled() && (val > kvm_val)) {
289         error_setg(errp,
290 "Requested safe bounds check capability level not supported by kvm,"
291                    " try appending -machine cap-sbbc=%s",
292                    cap_sbbc_possible.vals[kvm_val]);
293     }
294 
295     if (local_err != NULL)
296         warn_report_err(local_err);
297 }
298 
299 SpaprCapPossible cap_ibs_possible = {
300     .num = 5,
301     /* Note workaround only maintained for compatibility */
302     .vals = {"broken", "workaround", "fixed-ibs", "fixed-ccd", "fixed-na"},
303     .help = "broken - no protection, workaround - count cache flush"
304             ", fixed-ibs - indirect branch serialisation,"
305             " fixed-ccd - cache count disabled,"
306             " fixed-na - fixed in hardware (no longer applicable)",
307 };
308 
309 static void cap_safe_indirect_branch_apply(SpaprMachineState *spapr,
310                                            uint8_t val, Error **errp)
311 {
312     Error *local_err = NULL;
313     uint8_t kvm_val = kvmppc_get_cap_safe_indirect_branch();
314 
315     if (tcg_enabled() && val) {
316         /* TCG only supports broken, allow other values and print a warning */
317         error_setg(&local_err,
318                    "TCG doesn't support requested feature, cap-ibs=%s",
319                    cap_ibs_possible.vals[val]);
320     } else if (kvm_enabled() && (val > kvm_val)) {
321         error_setg(errp,
322 "Requested safe indirect branch capability level not supported by kvm,"
323                    " try appending -machine cap-ibs=%s",
324                    cap_ibs_possible.vals[kvm_val]);
325     }
326 
327     if (local_err != NULL) {
328         warn_report_err(local_err);
329     }
330 }
331 
332 #define VALUE_DESC_TRISTATE     " (broken, workaround, fixed)"
333 
334 void spapr_check_pagesize(SpaprMachineState *spapr, hwaddr pagesize,
335                           Error **errp)
336 {
337     hwaddr maxpagesize = (1ULL << spapr->eff.caps[SPAPR_CAP_HPT_MAXPAGESIZE]);
338 
339     if (!kvmppc_hpt_needs_host_contiguous_pages()) {
340         return;
341     }
342 
343     if (maxpagesize > pagesize) {
344         error_setg(errp,
345                    "Can't support %"HWADDR_PRIu" kiB guest pages with %"
346                    HWADDR_PRIu" kiB host pages with this KVM implementation",
347                    maxpagesize >> 10, pagesize >> 10);
348     }
349 }
350 
351 static void cap_hpt_maxpagesize_apply(SpaprMachineState *spapr,
352                                       uint8_t val, Error **errp)
353 {
354     if (val < 12) {
355         error_setg(errp, "Require at least 4kiB hpt-max-page-size");
356         return;
357     } else if (val < 16) {
358         warn_report("Many guests require at least 64kiB hpt-max-page-size");
359     }
360 
361     spapr_check_pagesize(spapr, qemu_minrampagesize(), errp);
362 }
363 
364 static bool cap_hpt_maxpagesize_migrate_needed(void *opaque)
365 {
366     return !SPAPR_MACHINE_GET_CLASS(opaque)->pre_4_1_migration;
367 }
368 
369 static bool spapr_pagesize_cb(void *opaque, uint32_t seg_pshift,
370                               uint32_t pshift)
371 {
372     unsigned maxshift = *((unsigned *)opaque);
373 
374     assert(pshift >= seg_pshift);
375 
376     /* Don't allow the guest to use pages bigger than the configured
377      * maximum size */
378     if (pshift > maxshift) {
379         return false;
380     }
381 
382     /* For whatever reason, KVM doesn't allow multiple pagesizes
383      * within a segment, *except* for the case of 16M pages in a 4k or
384      * 64k segment.  Always exclude other cases, so that TCG and KVM
385      * guests see a consistent environment */
386     if ((pshift != seg_pshift) && (pshift != 24)) {
387         return false;
388     }
389 
390     return true;
391 }
392 
393 static void cap_hpt_maxpagesize_cpu_apply(SpaprMachineState *spapr,
394                                           PowerPCCPU *cpu,
395                                           uint8_t val, Error **errp)
396 {
397     unsigned maxshift = val;
398 
399     ppc_hash64_filter_pagesizes(cpu, spapr_pagesize_cb, &maxshift);
400 }
401 
402 static void cap_nested_kvm_hv_apply(SpaprMachineState *spapr,
403                                     uint8_t val, Error **errp)
404 {
405     if (!val) {
406         /* capability disabled by default */
407         return;
408     }
409 
410     if (tcg_enabled()) {
411         error_setg(errp,
412                    "No Nested KVM-HV support in tcg,"
413                    " try appending -machine cap-nested-hv=off");
414     } else if (kvm_enabled()) {
415         if (!kvmppc_has_cap_nested_kvm_hv()) {
416             error_setg(errp,
417 "KVM implementation does not support Nested KVM-HV,"
418                        " try appending -machine cap-nested-hv=off");
419         } else if (kvmppc_set_cap_nested_kvm_hv(val) < 0) {
420                 error_setg(errp,
421 "Error enabling cap-nested-hv with KVM, try cap-nested-hv=off");
422         }
423     }
424 }
425 
426 static void cap_large_decr_apply(SpaprMachineState *spapr,
427                                  uint8_t val, Error **errp)
428 {
429     PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
430     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
431 
432     if (!val) {
433         return; /* Disabled by default */
434     }
435 
436     if (tcg_enabled()) {
437         if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
438                               spapr->max_compat_pvr)) {
439             error_setg(errp,
440                 "Large decrementer only supported on POWER9, try -cpu POWER9");
441             return;
442         }
443     } else if (kvm_enabled()) {
444         int kvm_nr_bits = kvmppc_get_cap_large_decr();
445 
446         if (!kvm_nr_bits) {
447             error_setg(errp,
448                        "No large decrementer support,"
449                         " try appending -machine cap-large-decr=off");
450         } else if (pcc->lrg_decr_bits != kvm_nr_bits) {
451             error_setg(errp,
452 "KVM large decrementer size (%d) differs to model (%d),"
453                 " try appending -machine cap-large-decr=off",
454                 kvm_nr_bits, pcc->lrg_decr_bits);
455         }
456     }
457 }
458 
459 static void cap_large_decr_cpu_apply(SpaprMachineState *spapr,
460                                      PowerPCCPU *cpu,
461                                      uint8_t val, Error **errp)
462 {
463     CPUPPCState *env = &cpu->env;
464     target_ulong lpcr = env->spr[SPR_LPCR];
465 
466     if (kvm_enabled()) {
467         if (kvmppc_enable_cap_large_decr(cpu, val)) {
468             error_setg(errp,
469                        "No large decrementer support,"
470                        " try appending -machine cap-large-decr=off");
471         }
472     }
473 
474     if (val) {
475         lpcr |= LPCR_LD;
476     } else {
477         lpcr &= ~LPCR_LD;
478     }
479     ppc_store_lpcr(cpu, lpcr);
480 }
481 
482 static void cap_ccf_assist_apply(SpaprMachineState *spapr, uint8_t val,
483                                  Error **errp)
484 {
485     uint8_t kvm_val = kvmppc_get_cap_count_cache_flush_assist();
486 
487     if (tcg_enabled() && val) {
488         /* TODO - for now only allow broken for TCG */
489         error_setg(errp,
490 "Requested count cache flush assist capability level not supported by tcg,"
491                    " try appending -machine cap-ccf-assist=off");
492     } else if (kvm_enabled() && (val > kvm_val)) {
493         error_setg(errp,
494 "Requested count cache flush assist capability level not supported by kvm,"
495                    " try appending -machine cap-ccf-assist=off");
496     }
497 }
498 
499 SpaprCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
500     [SPAPR_CAP_HTM] = {
501         .name = "htm",
502         .description = "Allow Hardware Transactional Memory (HTM)",
503         .index = SPAPR_CAP_HTM,
504         .get = spapr_cap_get_bool,
505         .set = spapr_cap_set_bool,
506         .type = "bool",
507         .apply = cap_htm_apply,
508     },
509     [SPAPR_CAP_VSX] = {
510         .name = "vsx",
511         .description = "Allow Vector Scalar Extensions (VSX)",
512         .index = SPAPR_CAP_VSX,
513         .get = spapr_cap_get_bool,
514         .set = spapr_cap_set_bool,
515         .type = "bool",
516         .apply = cap_vsx_apply,
517     },
518     [SPAPR_CAP_DFP] = {
519         .name = "dfp",
520         .description = "Allow Decimal Floating Point (DFP)",
521         .index = SPAPR_CAP_DFP,
522         .get = spapr_cap_get_bool,
523         .set = spapr_cap_set_bool,
524         .type = "bool",
525         .apply = cap_dfp_apply,
526     },
527     [SPAPR_CAP_CFPC] = {
528         .name = "cfpc",
529         .description = "Cache Flush on Privilege Change" VALUE_DESC_TRISTATE,
530         .index = SPAPR_CAP_CFPC,
531         .get = spapr_cap_get_string,
532         .set = spapr_cap_set_string,
533         .type = "string",
534         .possible = &cap_cfpc_possible,
535         .apply = cap_safe_cache_apply,
536     },
537     [SPAPR_CAP_SBBC] = {
538         .name = "sbbc",
539         .description = "Speculation Barrier Bounds Checking" VALUE_DESC_TRISTATE,
540         .index = SPAPR_CAP_SBBC,
541         .get = spapr_cap_get_string,
542         .set = spapr_cap_set_string,
543         .type = "string",
544         .possible = &cap_sbbc_possible,
545         .apply = cap_safe_bounds_check_apply,
546     },
547     [SPAPR_CAP_IBS] = {
548         .name = "ibs",
549         .description =
550             "Indirect Branch Speculation (broken, workaround, fixed-ibs,"
551             "fixed-ccd, fixed-na)",
552         .index = SPAPR_CAP_IBS,
553         .get = spapr_cap_get_string,
554         .set = spapr_cap_set_string,
555         .type = "string",
556         .possible = &cap_ibs_possible,
557         .apply = cap_safe_indirect_branch_apply,
558     },
559     [SPAPR_CAP_HPT_MAXPAGESIZE] = {
560         .name = "hpt-max-page-size",
561         .description = "Maximum page size for Hash Page Table guests",
562         .index = SPAPR_CAP_HPT_MAXPAGESIZE,
563         .get = spapr_cap_get_pagesize,
564         .set = spapr_cap_set_pagesize,
565         .type = "int",
566         .apply = cap_hpt_maxpagesize_apply,
567         .cpu_apply = cap_hpt_maxpagesize_cpu_apply,
568         .migrate_needed = cap_hpt_maxpagesize_migrate_needed,
569     },
570     [SPAPR_CAP_NESTED_KVM_HV] = {
571         .name = "nested-hv",
572         .description = "Allow Nested KVM-HV",
573         .index = SPAPR_CAP_NESTED_KVM_HV,
574         .get = spapr_cap_get_bool,
575         .set = spapr_cap_set_bool,
576         .type = "bool",
577         .apply = cap_nested_kvm_hv_apply,
578     },
579     [SPAPR_CAP_LARGE_DECREMENTER] = {
580         .name = "large-decr",
581         .description = "Allow Large Decrementer",
582         .index = SPAPR_CAP_LARGE_DECREMENTER,
583         .get = spapr_cap_get_bool,
584         .set = spapr_cap_set_bool,
585         .type = "bool",
586         .apply = cap_large_decr_apply,
587         .cpu_apply = cap_large_decr_cpu_apply,
588     },
589     [SPAPR_CAP_CCF_ASSIST] = {
590         .name = "ccf-assist",
591         .description = "Count Cache Flush Assist via HW Instruction",
592         .index = SPAPR_CAP_CCF_ASSIST,
593         .get = spapr_cap_get_bool,
594         .set = spapr_cap_set_bool,
595         .type = "bool",
596         .apply = cap_ccf_assist_apply,
597     },
598 };
599 
600 static SpaprCapabilities default_caps_with_cpu(SpaprMachineState *spapr,
601                                                const char *cputype)
602 {
603     SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
604     SpaprCapabilities caps;
605 
606     caps = smc->default_caps;
607 
608     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_3_00,
609                                0, spapr->max_compat_pvr)) {
610         caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = SPAPR_CAP_OFF;
611     }
612 
613     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_07,
614                                0, spapr->max_compat_pvr)) {
615         caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
616         caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_BROKEN;
617     }
618 
619     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_06_PLUS,
620                                0, spapr->max_compat_pvr)) {
621         caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_BROKEN;
622     }
623 
624     if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_06,
625                                0, spapr->max_compat_pvr)) {
626         caps.caps[SPAPR_CAP_VSX] = SPAPR_CAP_OFF;
627         caps.caps[SPAPR_CAP_DFP] = SPAPR_CAP_OFF;
628         caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
629     }
630 
631     /* This is for pseries-2.12 and older */
632     if (smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] == 0) {
633         uint8_t mps;
634 
635         if (kvmppc_hpt_needs_host_contiguous_pages()) {
636             mps = ctz64(qemu_minrampagesize());
637         } else {
638             mps = 34; /* allow everything up to 16GiB, i.e. everything */
639         }
640 
641         caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = mps;
642     }
643 
644     return caps;
645 }
646 
647 int spapr_caps_pre_load(void *opaque)
648 {
649     SpaprMachineState *spapr = opaque;
650 
651     /* Set to default so we can tell if this came in with the migration */
652     spapr->mig = spapr->def;
653     return 0;
654 }
655 
656 int spapr_caps_pre_save(void *opaque)
657 {
658     SpaprMachineState *spapr = opaque;
659 
660     spapr->mig = spapr->eff;
661     return 0;
662 }
663 
664 /* This has to be called from the top-level spapr post_load, not the
665  * caps specific one.  Otherwise it wouldn't be called when the source
666  * caps are all defaults, which could still conflict with overridden
667  * caps on the destination */
668 int spapr_caps_post_migration(SpaprMachineState *spapr)
669 {
670     int i;
671     bool ok = true;
672     SpaprCapabilities dstcaps = spapr->eff;
673     SpaprCapabilities srccaps;
674 
675     srccaps = default_caps_with_cpu(spapr, MACHINE(spapr)->cpu_type);
676     for (i = 0; i < SPAPR_CAP_NUM; i++) {
677         /* If not default value then assume came in with the migration */
678         if (spapr->mig.caps[i] != spapr->def.caps[i]) {
679             srccaps.caps[i] = spapr->mig.caps[i];
680         }
681     }
682 
683     for (i = 0; i < SPAPR_CAP_NUM; i++) {
684         SpaprCapabilityInfo *info = &capability_table[i];
685 
686         if (srccaps.caps[i] > dstcaps.caps[i]) {
687             error_report("cap-%s higher level (%d) in incoming stream than on destination (%d)",
688                          info->name, srccaps.caps[i], dstcaps.caps[i]);
689             ok = false;
690         }
691 
692         if (srccaps.caps[i] < dstcaps.caps[i]) {
693             warn_report("cap-%s lower level (%d) in incoming stream than on destination (%d)",
694                          info->name, srccaps.caps[i], dstcaps.caps[i]);
695         }
696     }
697 
698     return ok ? 0 : -EINVAL;
699 }
700 
701 /* Used to generate the migration field and needed function for a spapr cap */
702 #define SPAPR_CAP_MIG_STATE(sname, cap)                 \
703 static bool spapr_cap_##sname##_needed(void *opaque)    \
704 {                                                       \
705     SpaprMachineState *spapr = opaque;                  \
706     bool (*needed)(void *opaque) =                      \
707         capability_table[cap].migrate_needed;           \
708                                                         \
709     return needed ? needed(opaque) : true &&            \
710            spapr->cmd_line_caps[cap] &&                 \
711            (spapr->eff.caps[cap] !=                     \
712             spapr->def.caps[cap]);                      \
713 }                                                       \
714                                                         \
715 const VMStateDescription vmstate_spapr_cap_##sname = {  \
716     .name = "spapr/cap/" #sname,                        \
717     .version_id = 1,                                    \
718     .minimum_version_id = 1,                            \
719     .needed = spapr_cap_##sname##_needed,               \
720     .fields = (VMStateField[]) {                        \
721         VMSTATE_UINT8(mig.caps[cap],                    \
722                       SpaprMachineState),               \
723         VMSTATE_END_OF_LIST()                           \
724     },                                                  \
725 }
726 
727 SPAPR_CAP_MIG_STATE(htm, SPAPR_CAP_HTM);
728 SPAPR_CAP_MIG_STATE(vsx, SPAPR_CAP_VSX);
729 SPAPR_CAP_MIG_STATE(dfp, SPAPR_CAP_DFP);
730 SPAPR_CAP_MIG_STATE(cfpc, SPAPR_CAP_CFPC);
731 SPAPR_CAP_MIG_STATE(sbbc, SPAPR_CAP_SBBC);
732 SPAPR_CAP_MIG_STATE(ibs, SPAPR_CAP_IBS);
733 SPAPR_CAP_MIG_STATE(hpt_maxpagesize, SPAPR_CAP_HPT_MAXPAGESIZE);
734 SPAPR_CAP_MIG_STATE(nested_kvm_hv, SPAPR_CAP_NESTED_KVM_HV);
735 SPAPR_CAP_MIG_STATE(large_decr, SPAPR_CAP_LARGE_DECREMENTER);
736 SPAPR_CAP_MIG_STATE(ccf_assist, SPAPR_CAP_CCF_ASSIST);
737 
738 void spapr_caps_init(SpaprMachineState *spapr)
739 {
740     SpaprCapabilities default_caps;
741     int i;
742 
743     /* Compute the actual set of caps we should run with */
744     default_caps = default_caps_with_cpu(spapr, MACHINE(spapr)->cpu_type);
745 
746     for (i = 0; i < SPAPR_CAP_NUM; i++) {
747         /* Store the defaults */
748         spapr->def.caps[i] = default_caps.caps[i];
749         /* If not set on the command line then apply the default value */
750         if (!spapr->cmd_line_caps[i]) {
751             spapr->eff.caps[i] = default_caps.caps[i];
752         }
753     }
754 }
755 
756 void spapr_caps_apply(SpaprMachineState *spapr)
757 {
758     int i;
759 
760     for (i = 0; i < SPAPR_CAP_NUM; i++) {
761         SpaprCapabilityInfo *info = &capability_table[i];
762 
763         /*
764          * If the apply function can't set the desired level and thinks it's
765          * fatal, it should cause that.
766          */
767         info->apply(spapr, spapr->eff.caps[i], &error_fatal);
768     }
769 }
770 
771 void spapr_caps_cpu_apply(SpaprMachineState *spapr, PowerPCCPU *cpu)
772 {
773     int i;
774 
775     for (i = 0; i < SPAPR_CAP_NUM; i++) {
776         SpaprCapabilityInfo *info = &capability_table[i];
777 
778         /*
779          * If the apply function can't set the desired level and thinks it's
780          * fatal, it should cause that.
781          */
782         if (info->cpu_apply) {
783             info->cpu_apply(spapr, cpu, spapr->eff.caps[i], &error_fatal);
784         }
785     }
786 }
787 
788 void spapr_caps_add_properties(SpaprMachineClass *smc, Error **errp)
789 {
790     Error *local_err = NULL;
791     ObjectClass *klass = OBJECT_CLASS(smc);
792     int i;
793 
794     for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
795         SpaprCapabilityInfo *cap = &capability_table[i];
796         char *name = g_strdup_printf("cap-%s", cap->name);
797         char *desc;
798 
799         object_class_property_add(klass, name, cap->type,
800                                   cap->get, cap->set,
801                                   NULL, cap, &local_err);
802         if (local_err) {
803             error_propagate(errp, local_err);
804             g_free(name);
805             return;
806         }
807 
808         desc = g_strdup_printf("%s", cap->description);
809         object_class_property_set_description(klass, name, desc, &local_err);
810         g_free(name);
811         g_free(desc);
812         if (local_err) {
813             error_propagate(errp, local_err);
814             return;
815         }
816     }
817 }
818