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