]> git.proxmox.com Git - mirror_qemu.git/blob - hw/ppc/spapr_caps.c
spapr: Maximum (HPT) pagesize property
[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 "target/ppc/cpu.h"
30 #include "target/ppc/mmu-hash64.h"
31 #include "cpu-models.h"
32 #include "kvm_ppc.h"
33
34 #include "hw/ppc/spapr.h"
35
36 typedef struct sPAPRCapPossible {
37 int num; /* size of vals array below */
38 const char *help; /* help text for vals */
39 /*
40 * Note:
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
45 * point is observed
46 */
47 const char *vals[];
48 } sPAPRCapPossible;
49
50 typedef struct sPAPRCapabilityInfo {
51 const char *name;
52 const char *description;
53 int index;
54
55 /* Getter and Setter Function Pointers */
56 ObjectPropertyAccessor *get;
57 ObjectPropertyAccessor *set;
58 const char *type;
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;
66
67 static void spapr_cap_get_bool(Object *obj, Visitor *v, const char *name,
68 void *opaque, Error **errp)
69 {
70 sPAPRCapabilityInfo *cap = opaque;
71 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
72 bool value = spapr_get_cap(spapr, cap->index) == SPAPR_CAP_ON;
73
74 visit_type_bool(v, name, &value, errp);
75 }
76
77 static void spapr_cap_set_bool(Object *obj, Visitor *v, const char *name,
78 void *opaque, Error **errp)
79 {
80 sPAPRCapabilityInfo *cap = opaque;
81 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
82 bool value;
83 Error *local_err = NULL;
84
85 visit_type_bool(v, name, &value, &local_err);
86 if (local_err) {
87 error_propagate(errp, local_err);
88 return;
89 }
90
91 spapr->cmd_line_caps[cap->index] = true;
92 spapr->eff.caps[cap->index] = value ? SPAPR_CAP_ON : SPAPR_CAP_OFF;
93 }
94
95
96 static void spapr_cap_get_string(Object *obj, Visitor *v, const char *name,
97 void *opaque, Error **errp)
98 {
99 sPAPRCapabilityInfo *cap = opaque;
100 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
101 char *val = NULL;
102 uint8_t value = spapr_get_cap(spapr, cap->index);
103
104 if (value >= cap->possible->num) {
105 error_setg(errp, "Invalid value (%d) for cap-%s", value, cap->name);
106 return;
107 }
108
109 val = g_strdup(cap->possible->vals[value]);
110
111 visit_type_str(v, name, &val, errp);
112 g_free(val);
113 }
114
115 static void spapr_cap_set_string(Object *obj, Visitor *v, const char *name,
116 void *opaque, Error **errp)
117 {
118 sPAPRCapabilityInfo *cap = opaque;
119 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
120 Error *local_err = NULL;
121 uint8_t i;
122 char *val;
123
124 visit_type_str(v, name, &val, &local_err);
125 if (local_err) {
126 error_propagate(errp, local_err);
127 return;
128 }
129
130 if (!strcmp(val, "?")) {
131 error_setg(errp, "%s", cap->possible->help);
132 goto out;
133 }
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;
138 goto out;
139 }
140 }
141
142 error_setg(errp, "Invalid capability mode \"%s\" for cap-%s", val,
143 cap->name);
144 out:
145 g_free(val);
146 }
147
148 static void spapr_cap_get_pagesize(Object *obj, Visitor *v, const char *name,
149 void *opaque, Error **errp)
150 {
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);
155
156 visit_type_size(v, name, &pagesize, errp);
157 }
158
159 static void spapr_cap_set_pagesize(Object *obj, Visitor *v, const char *name,
160 void *opaque, Error **errp)
161 {
162 sPAPRCapabilityInfo *cap = opaque;
163 sPAPRMachineState *spapr = SPAPR_MACHINE(obj);
164 uint64_t pagesize;
165 uint8_t val;
166 Error *local_err = NULL;
167
168 visit_type_size(v, name, &pagesize, &local_err);
169 if (local_err) {
170 error_propagate(errp, local_err);
171 return;
172 }
173
174 if (!is_power_of_2(pagesize)) {
175 error_setg(errp, "cap-%s must be a power of 2", cap->name);
176 return;
177 }
178
179 val = ctz64(pagesize);
180 spapr->cmd_line_caps[cap->index] = true;
181 spapr->eff.caps[cap->index] = val;
182 }
183
184 static void cap_htm_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
185 {
186 if (!val) {
187 /* TODO: We don't support disabling htm yet */
188 return;
189 }
190 if (tcg_enabled()) {
191 error_setg(errp,
192 "No Transactional Memory support in TCG, try cap-htm=off");
193 } else if (kvm_enabled() && !kvmppc_has_cap_htm()) {
194 error_setg(errp,
195 "KVM implementation does not support Transactional Memory, try cap-htm=off"
196 );
197 }
198 }
199
200 static void cap_vsx_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
201 {
202 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
203 CPUPPCState *env = &cpu->env;
204
205 if (!val) {
206 /* TODO: We don't support disabling vsx yet */
207 return;
208 }
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");
214 }
215 }
216
217 static void cap_dfp_apply(sPAPRMachineState *spapr, uint8_t val, Error **errp)
218 {
219 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
220 CPUPPCState *env = &cpu->env;
221
222 if (!val) {
223 /* TODO: We don't support disabling dfp yet */
224 return;
225 }
226 if (!(env->insns_flags2 & PPC2_DFP)) {
227 error_setg(errp, "DFP support not available, try cap-dfp=off");
228 }
229 }
230
231 sPAPRCapPossible cap_cfpc_possible = {
232 .num = 3,
233 .vals = {"broken", "workaround", "fixed"},
234 .help = "broken - no protection, workaround - workaround available,"
235 " fixed - fixed in hardware",
236 };
237
238 static void cap_safe_cache_apply(sPAPRMachineState *spapr, uint8_t val,
239 Error **errp)
240 {
241 uint8_t kvm_val = kvmppc_get_cap_safe_cache();
242
243 if (tcg_enabled() && val) {
244 /* TODO - for now only allow broken for TCG */
245 error_setg(errp,
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)) {
248 error_setg(errp,
249 "Requested safe cache capability level not supported by kvm, try cap-cfpc=%s",
250 cap_cfpc_possible.vals[kvm_val]);
251 }
252 }
253
254 sPAPRCapPossible cap_sbbc_possible = {
255 .num = 3,
256 .vals = {"broken", "workaround", "fixed"},
257 .help = "broken - no protection, workaround - workaround available,"
258 " fixed - fixed in hardware",
259 };
260
261 static void cap_safe_bounds_check_apply(sPAPRMachineState *spapr, uint8_t val,
262 Error **errp)
263 {
264 uint8_t kvm_val = kvmppc_get_cap_safe_bounds_check();
265
266 if (tcg_enabled() && val) {
267 /* TODO - for now only allow broken for TCG */
268 error_setg(errp,
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)) {
271 error_setg(errp,
272 "Requested safe bounds check capability level not supported by kvm, try cap-sbbc=%s",
273 cap_sbbc_possible.vals[kvm_val]);
274 }
275 }
276
277 sPAPRCapPossible cap_ibs_possible = {
278 .num = 4,
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",
283 };
284
285 static void cap_safe_indirect_branch_apply(sPAPRMachineState *spapr,
286 uint8_t val, Error **errp)
287 {
288 uint8_t kvm_val = kvmppc_get_cap_safe_indirect_branch();
289
290 if (val == SPAPR_CAP_WORKAROUND) { /* Can only be Broken or Fixed */
291 error_setg(errp,
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 */
296 error_setg(errp,
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)) {
299 error_setg(errp,
300 "Requested safe indirect branch capability level not supported by kvm, try cap-ibs=%s",
301 cap_ibs_possible.vals[kvm_val]);
302 }
303 }
304
305 #define VALUE_DESC_TRISTATE " (broken, workaround, fixed)"
306
307 static void cap_hpt_maxpagesize_apply(sPAPRMachineState *spapr,
308 uint8_t val, Error **errp)
309 {
310 if (val < 12) {
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");
314 }
315 }
316
317 sPAPRCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
318 [SPAPR_CAP_HTM] = {
319 .name = "htm",
320 .description = "Allow Hardware Transactional Memory (HTM)",
321 .index = SPAPR_CAP_HTM,
322 .get = spapr_cap_get_bool,
323 .set = spapr_cap_set_bool,
324 .type = "bool",
325 .apply = cap_htm_apply,
326 },
327 [SPAPR_CAP_VSX] = {
328 .name = "vsx",
329 .description = "Allow Vector Scalar Extensions (VSX)",
330 .index = SPAPR_CAP_VSX,
331 .get = spapr_cap_get_bool,
332 .set = spapr_cap_set_bool,
333 .type = "bool",
334 .apply = cap_vsx_apply,
335 },
336 [SPAPR_CAP_DFP] = {
337 .name = "dfp",
338 .description = "Allow Decimal Floating Point (DFP)",
339 .index = SPAPR_CAP_DFP,
340 .get = spapr_cap_get_bool,
341 .set = spapr_cap_set_bool,
342 .type = "bool",
343 .apply = cap_dfp_apply,
344 },
345 [SPAPR_CAP_CFPC] = {
346 .name = "cfpc",
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,
351 .type = "string",
352 .possible = &cap_cfpc_possible,
353 .apply = cap_safe_cache_apply,
354 },
355 [SPAPR_CAP_SBBC] = {
356 .name = "sbbc",
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,
361 .type = "string",
362 .possible = &cap_sbbc_possible,
363 .apply = cap_safe_bounds_check_apply,
364 },
365 [SPAPR_CAP_IBS] = {
366 .name = "ibs",
367 .description =
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,
372 .type = "string",
373 .possible = &cap_ibs_possible,
374 .apply = cap_safe_indirect_branch_apply,
375 },
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,
382 .type = "int",
383 .apply = cap_hpt_maxpagesize_apply,
384 },
385 };
386
387 static sPAPRCapabilities default_caps_with_cpu(sPAPRMachineState *spapr,
388 const char *cputype)
389 {
390 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
391 sPAPRCapabilities caps;
392
393 caps = smc->default_caps;
394
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;
399 }
400
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;
404 }
405
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;
411 }
412
413 return caps;
414 }
415
416 int spapr_caps_pre_load(void *opaque)
417 {
418 sPAPRMachineState *spapr = opaque;
419
420 /* Set to default so we can tell if this came in with the migration */
421 spapr->mig = spapr->def;
422 return 0;
423 }
424
425 int spapr_caps_pre_save(void *opaque)
426 {
427 sPAPRMachineState *spapr = opaque;
428
429 spapr->mig = spapr->eff;
430 return 0;
431 }
432
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)
438 {
439 int i;
440 bool ok = true;
441 sPAPRCapabilities dstcaps = spapr->eff;
442 sPAPRCapabilities srccaps;
443
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];
449 }
450 }
451
452 for (i = 0; i < SPAPR_CAP_NUM; i++) {
453 sPAPRCapabilityInfo *info = &capability_table[i];
454
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]);
458 ok = false;
459 }
460
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]);
464 }
465 }
466
467 return ok ? 0 : -EINVAL;
468 }
469
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) \
473 { \
474 sPAPRMachineState *spapr = opaque; \
475 \
476 return spapr->cmd_line_caps[cap] && \
477 (spapr->eff.caps[cap] != \
478 spapr->def.caps[cap]); \
479 } \
480 \
481 const VMStateDescription vmstate_spapr_cap_##sname = { \
482 .name = "spapr/cap/" #sname, \
483 .version_id = 1, \
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() \
490 }, \
491 }
492
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);
499
500 void spapr_caps_init(sPAPRMachineState *spapr)
501 {
502 sPAPRCapabilities default_caps;
503 int i;
504
505 /* Compute the actual set of caps we should run with */
506 default_caps = default_caps_with_cpu(spapr, MACHINE(spapr)->cpu_type);
507
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];
514 }
515 }
516 }
517
518 void spapr_caps_apply(sPAPRMachineState *spapr)
519 {
520 int i;
521
522 for (i = 0; i < SPAPR_CAP_NUM; i++) {
523 sPAPRCapabilityInfo *info = &capability_table[i];
524
525 /*
526 * If the apply function can't set the desired level and thinks it's
527 * fatal, it should cause that.
528 */
529 info->apply(spapr, spapr->eff.caps[i], &error_fatal);
530 }
531 }
532
533 void spapr_caps_cpu_apply(sPAPRMachineState *spapr, PowerPCCPU *cpu)
534 {
535 int i;
536
537 for (i = 0; i < SPAPR_CAP_NUM; i++) {
538 sPAPRCapabilityInfo *info = &capability_table[i];
539
540 /*
541 * If the apply function can't set the desired level and thinks it's
542 * fatal, it should cause that.
543 */
544 if (info->cpu_apply) {
545 info->cpu_apply(spapr, cpu, spapr->eff.caps[i], &error_fatal);
546 }
547 }
548 }
549
550 void spapr_caps_add_properties(sPAPRMachineClass *smc, Error **errp)
551 {
552 Error *local_err = NULL;
553 ObjectClass *klass = OBJECT_CLASS(smc);
554 int i;
555
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);
559 char *desc;
560
561 object_class_property_add(klass, name, cap->type,
562 cap->get, cap->set,
563 NULL, cap, &local_err);
564 if (local_err) {
565 error_propagate(errp, local_err);
566 return;
567 }
568
569 desc = g_strdup_printf("%s", cap->description);
570 object_class_property_set_description(klass, name, desc, &local_err);
571 g_free(desc);
572 if (local_err) {
573 error_propagate(errp, local_err);
574 return;
575 }
576 }
577 }