1 /* 2 * CPU models for s390x 3 * 4 * Copyright 2016 IBM Corp. 5 * 6 * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com> 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or (at 9 * your option) any later version. See the COPYING file in the top-level 10 * directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "cpu.h" 15 #include "gen-features.h" 16 #include "qapi/error.h" 17 #include "qapi/visitor.h" 18 #include "qemu/error-report.h" 19 #include "qapi/qmp/qerror.h" 20 #include "qapi/qobject-input-visitor.h" 21 #include "qapi/qmp/qbool.h" 22 #ifndef CONFIG_USER_ONLY 23 #include "sysemu/arch_init.h" 24 #endif 25 26 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \ 27 { \ 28 .name = _name, \ 29 .type = _type, \ 30 .gen = _gen, \ 31 .ec_ga = _ec_ga, \ 32 .mha_pow = _mha_pow, \ 33 .hmfai = _hmfai, \ 34 .desc = _desc, \ 35 .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE }, \ 36 .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT }, \ 37 .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL }, \ 38 } 39 40 /* 41 * CPU definiton list in order of release. For now, base features of a 42 * following release are always a subset of base features of the previous 43 * release. Same is correct for the other feature sets. 44 * A BC release always follows the corresponding EC release. 45 */ 46 static S390CPUDef s390_cpu_defs[] = { 47 CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"), 48 CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"), 49 CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"), 50 CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"), 51 CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"), 52 CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"), 53 CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"), 54 CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"), 55 CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"), 56 CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"), 57 CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"), 58 CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"), 59 CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"), 60 CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"), 61 CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"), 62 CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"), 63 CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"), 64 CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"), 65 CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"), 66 CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"), 67 CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"), 68 CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"), 69 CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"), 70 CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"), 71 CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"), 72 CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"), 73 CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"), 74 CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"), 75 CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"), 76 CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"), 77 CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"), 78 }; 79 80 uint32_t s390_get_hmfai(void) 81 { 82 static S390CPU *cpu; 83 84 if (!cpu) { 85 cpu = S390_CPU(qemu_get_cpu(0)); 86 } 87 88 if (!cpu || !cpu->model) { 89 return 0; 90 } 91 return cpu->model->def->hmfai; 92 } 93 94 uint8_t s390_get_mha_pow(void) 95 { 96 static S390CPU *cpu; 97 98 if (!cpu) { 99 cpu = S390_CPU(qemu_get_cpu(0)); 100 } 101 102 if (!cpu || !cpu->model) { 103 return 0; 104 } 105 return cpu->model->def->mha_pow; 106 } 107 108 uint32_t s390_get_ibc_val(void) 109 { 110 uint16_t unblocked_ibc, lowest_ibc; 111 static S390CPU *cpu; 112 113 if (!cpu) { 114 cpu = S390_CPU(qemu_get_cpu(0)); 115 } 116 117 if (!cpu || !cpu->model) { 118 return 0; 119 } 120 unblocked_ibc = s390_ibc_from_cpu_model(cpu->model); 121 lowest_ibc = cpu->model->lowest_ibc; 122 /* the lowest_ibc always has to be <= unblocked_ibc */ 123 if (!lowest_ibc || lowest_ibc > unblocked_ibc) { 124 return 0; 125 } 126 return ((uint32_t) lowest_ibc << 16) | unblocked_ibc; 127 } 128 129 void s390_get_feat_block(S390FeatType type, uint8_t *data) 130 { 131 static S390CPU *cpu; 132 133 if (!cpu) { 134 cpu = S390_CPU(qemu_get_cpu(0)); 135 } 136 137 if (!cpu || !cpu->model) { 138 return; 139 } 140 s390_fill_feat_block(cpu->model->features, type, data); 141 } 142 143 bool s390_has_feat(S390Feat feat) 144 { 145 static S390CPU *cpu; 146 147 if (!cpu) { 148 cpu = S390_CPU(qemu_get_cpu(0)); 149 } 150 151 if (!cpu || !cpu->model) { 152 #ifdef CONFIG_KVM 153 if (kvm_enabled()) { 154 if (feat == S390_FEAT_VECTOR) { 155 return kvm_check_extension(kvm_state, 156 KVM_CAP_S390_VECTOR_REGISTERS); 157 } 158 if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) { 159 return kvm_s390_get_ri(); 160 } 161 if (feat == S390_FEAT_MSA_EXT_3) { 162 return true; 163 } 164 } 165 #endif 166 return 0; 167 } 168 return test_bit(feat, cpu->model->features); 169 } 170 171 uint8_t s390_get_gen_for_cpu_type(uint16_t type) 172 { 173 int i; 174 175 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 176 if (s390_cpu_defs[i].type == type) { 177 return s390_cpu_defs[i].gen; 178 } 179 } 180 return 0; 181 } 182 183 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga, 184 S390FeatBitmap features) 185 { 186 const S390CPUDef *last_compatible = NULL; 187 const S390CPUDef *matching_cpu_type = NULL; 188 int i; 189 190 if (!gen) { 191 ec_ga = 0; 192 } 193 if (!gen && type) { 194 gen = s390_get_gen_for_cpu_type(type); 195 } 196 197 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 198 const S390CPUDef *def = &s390_cpu_defs[i]; 199 S390FeatBitmap missing; 200 201 /* don't even try newer generations if we know the generation */ 202 if (gen) { 203 if (def->gen > gen) { 204 break; 205 } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) { 206 break; 207 } 208 } 209 210 if (features) { 211 /* see if the model satisfies the minimum features */ 212 bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX); 213 if (!bitmap_empty(missing, S390_FEAT_MAX)) { 214 break; 215 } 216 } 217 218 /* stop the search if we found the exact model */ 219 if (def->type == type && def->ec_ga == ec_ga) { 220 return def; 221 } 222 /* remember if we've at least seen one with the same cpu type */ 223 if (def->type == type) { 224 matching_cpu_type = def; 225 } 226 last_compatible = def; 227 } 228 /* prefer the model with the same cpu type, esp. don't take the BC for EC */ 229 if (matching_cpu_type) { 230 return matching_cpu_type; 231 } 232 return last_compatible; 233 } 234 235 struct S390PrintCpuListInfo { 236 FILE *f; 237 fprintf_function print; 238 }; 239 240 static void print_cpu_model_list(ObjectClass *klass, void *opaque) 241 { 242 struct S390PrintCpuListInfo *info = opaque; 243 S390CPUClass *scc = S390_CPU_CLASS(klass); 244 char *name = g_strdup(object_class_get_name(klass)); 245 const char *details = ""; 246 247 if (scc->is_static) { 248 details = "(static, migration-safe)"; 249 } else if (scc->is_migration_safe) { 250 details = "(migration-safe)"; 251 } 252 253 /* strip off the -s390-cpu */ 254 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0; 255 (*info->print)(info->f, "s390 %-15s %-35s %s\n", name, scc->desc, 256 details); 257 g_free(name); 258 } 259 260 void s390_cpu_list(FILE *f, fprintf_function print) 261 { 262 struct S390PrintCpuListInfo info = { 263 .f = f, 264 .print = print, 265 }; 266 S390FeatGroup group; 267 S390Feat feat; 268 269 object_class_foreach(print_cpu_model_list, TYPE_S390_CPU, false, &info); 270 271 (*print)(f, "\nRecognized feature flags:\n"); 272 for (feat = 0; feat < S390_FEAT_MAX; feat++) { 273 const S390FeatDef *def = s390_feat_def(feat); 274 275 (*print)(f, "%-20s %-50s\n", def->name, def->desc); 276 } 277 278 (*print)(f, "\nRecognized feature groups:\n"); 279 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) { 280 const S390FeatGroupDef *def = s390_feat_group_def(group); 281 282 (*print)(f, "%-20s %-50s\n", def->name, def->desc); 283 } 284 } 285 286 static S390CPUModel *get_max_cpu_model(Error **errp); 287 288 #ifndef CONFIG_USER_ONLY 289 static void list_add_feat(const char *name, void *opaque); 290 291 static void check_unavailable_features(const S390CPUModel *max_model, 292 const S390CPUModel *model, 293 strList **unavailable) 294 { 295 S390FeatBitmap missing; 296 297 /* check general model compatibility */ 298 if (max_model->def->gen < model->def->gen || 299 (max_model->def->gen == model->def->gen && 300 max_model->def->ec_ga < model->def->ec_ga)) { 301 list_add_feat("type", unavailable); 302 } 303 304 /* detect missing features if any to properly report them */ 305 bitmap_andnot(missing, model->features, max_model->features, 306 S390_FEAT_MAX); 307 if (!bitmap_empty(missing, S390_FEAT_MAX)) { 308 s390_feat_bitmap_to_ascii(missing, unavailable, list_add_feat); 309 } 310 } 311 312 struct CpuDefinitionInfoListData { 313 CpuDefinitionInfoList *list; 314 S390CPUModel *model; 315 }; 316 317 static void create_cpu_model_list(ObjectClass *klass, void *opaque) 318 { 319 struct CpuDefinitionInfoListData *cpu_list_data = opaque; 320 CpuDefinitionInfoList **cpu_list = &cpu_list_data->list; 321 CpuDefinitionInfoList *entry; 322 CpuDefinitionInfo *info; 323 char *name = g_strdup(object_class_get_name(klass)); 324 S390CPUClass *scc = S390_CPU_CLASS(klass); 325 326 /* strip off the -s390-cpu */ 327 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0; 328 info = g_malloc0(sizeof(*info)); 329 info->name = name; 330 info->has_migration_safe = true; 331 info->migration_safe = scc->is_migration_safe; 332 info->q_static = scc->is_static; 333 info->q_typename = g_strdup(object_class_get_name(klass)); 334 /* check for unavailable features */ 335 if (cpu_list_data->model) { 336 Object *obj; 337 S390CPU *sc; 338 obj = object_new(object_class_get_name(klass)); 339 sc = S390_CPU(obj); 340 if (sc->model) { 341 info->has_unavailable_features = true; 342 check_unavailable_features(cpu_list_data->model, sc->model, 343 &info->unavailable_features); 344 } 345 object_unref(obj); 346 } 347 348 entry = g_malloc0(sizeof(*entry)); 349 entry->value = info; 350 entry->next = *cpu_list; 351 *cpu_list = entry; 352 } 353 354 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) 355 { 356 struct CpuDefinitionInfoListData list_data = { 357 .list = NULL, 358 }; 359 360 list_data.model = get_max_cpu_model(errp); 361 if (*errp) { 362 error_free(*errp); 363 *errp = NULL; 364 } 365 366 object_class_foreach(create_cpu_model_list, TYPE_S390_CPU, false, 367 &list_data); 368 369 return list_data.list; 370 } 371 372 static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info, 373 Error **errp) 374 { 375 const QDict *qdict = NULL; 376 const QDictEntry *e; 377 Visitor *visitor; 378 ObjectClass *oc; 379 S390CPU *cpu; 380 Object *obj; 381 382 if (info->props) { 383 qdict = qobject_to_qdict(info->props); 384 if (!qdict) { 385 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict"); 386 return; 387 } 388 } 389 390 oc = cpu_class_by_name(TYPE_S390_CPU, info->name); 391 if (!oc) { 392 error_setg(errp, "The CPU definition \'%s\' is unknown.", info->name); 393 return; 394 } 395 if (S390_CPU_CLASS(oc)->kvm_required && !kvm_enabled()) { 396 error_setg(errp, "The CPU definition '%s' requires KVM", info->name); 397 return; 398 } 399 obj = object_new(object_class_get_name(oc)); 400 cpu = S390_CPU(obj); 401 402 if (!cpu->model) { 403 error_setg(errp, "Details about the host CPU model are not available, " 404 "it cannot be used."); 405 object_unref(obj); 406 return; 407 } 408 409 if (qdict) { 410 visitor = qobject_input_visitor_new(info->props); 411 visit_start_struct(visitor, NULL, NULL, 0, errp); 412 if (*errp) { 413 object_unref(obj); 414 return; 415 } 416 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { 417 object_property_set(obj, visitor, e->key, errp); 418 if (*errp) { 419 break; 420 } 421 } 422 if (!*errp) { 423 visit_check_struct(visitor, errp); 424 } 425 visit_end_struct(visitor, NULL); 426 visit_free(visitor); 427 if (*errp) { 428 object_unref(obj); 429 return; 430 } 431 } 432 433 /* copy the model and throw the cpu away */ 434 memcpy(model, cpu->model, sizeof(*model)); 435 object_unref(obj); 436 } 437 438 static void qdict_add_disabled_feat(const char *name, void *opaque) 439 { 440 qdict_put_bool(opaque, name, false); 441 } 442 443 static void qdict_add_enabled_feat(const char *name, void *opaque) 444 { 445 qdict_put_bool(opaque, name, true); 446 } 447 448 /* convert S390CPUDef into a static CpuModelInfo */ 449 static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model, 450 bool delta_changes) 451 { 452 QDict *qdict = qdict_new(); 453 S390FeatBitmap bitmap; 454 455 /* always fallback to the static base model */ 456 info->name = g_strdup_printf("%s-base", model->def->name); 457 458 if (delta_changes) { 459 /* features deleted from the base feature set */ 460 bitmap_andnot(bitmap, model->def->base_feat, model->features, 461 S390_FEAT_MAX); 462 if (!bitmap_empty(bitmap, S390_FEAT_MAX)) { 463 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat); 464 } 465 466 /* features added to the base feature set */ 467 bitmap_andnot(bitmap, model->features, model->def->base_feat, 468 S390_FEAT_MAX); 469 if (!bitmap_empty(bitmap, S390_FEAT_MAX)) { 470 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_enabled_feat); 471 } 472 } else { 473 /* expand all features */ 474 s390_feat_bitmap_to_ascii(model->features, qdict, 475 qdict_add_enabled_feat); 476 bitmap_complement(bitmap, model->features, S390_FEAT_MAX); 477 s390_feat_bitmap_to_ascii(bitmap, qdict, qdict_add_disabled_feat); 478 } 479 480 if (!qdict_size(qdict)) { 481 QDECREF(qdict); 482 } else { 483 info->props = QOBJECT(qdict); 484 info->has_props = true; 485 } 486 } 487 488 CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type, 489 CpuModelInfo *model, 490 Error **errp) 491 { 492 CpuModelExpansionInfo *expansion_info = NULL; 493 S390CPUModel s390_model; 494 bool delta_changes = false; 495 496 /* convert it to our internal representation */ 497 cpu_model_from_info(&s390_model, model, errp); 498 if (*errp) { 499 return NULL; 500 } 501 502 if (type == CPU_MODEL_EXPANSION_TYPE_STATIC) { 503 delta_changes = true; 504 } else if (type != CPU_MODEL_EXPANSION_TYPE_FULL) { 505 error_setg(errp, "The requested expansion type is not supported."); 506 return NULL; 507 } 508 509 /* convert it back to a static representation */ 510 expansion_info = g_malloc0(sizeof(*expansion_info)); 511 expansion_info->model = g_malloc0(sizeof(*expansion_info->model)); 512 cpu_info_from_model(expansion_info->model, &s390_model, delta_changes); 513 return expansion_info; 514 } 515 516 static void list_add_feat(const char *name, void *opaque) 517 { 518 strList **last = (strList **) opaque; 519 strList *entry; 520 521 entry = g_malloc0(sizeof(*entry)); 522 entry->value = g_strdup(name); 523 entry->next = *last; 524 *last = entry; 525 } 526 527 CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa, 528 CpuModelInfo *infob, 529 Error **errp) 530 { 531 CpuModelCompareResult feat_result, gen_result; 532 CpuModelCompareInfo *compare_info; 533 S390FeatBitmap missing, added; 534 S390CPUModel modela, modelb; 535 536 /* convert both models to our internal representation */ 537 cpu_model_from_info(&modela, infoa, errp); 538 if (*errp) { 539 return NULL; 540 } 541 cpu_model_from_info(&modelb, infob, errp); 542 if (*errp) { 543 return NULL; 544 } 545 compare_info = g_malloc0(sizeof(*compare_info)); 546 547 /* check the cpu generation and ga level */ 548 if (modela.def->gen == modelb.def->gen) { 549 if (modela.def->ec_ga == modelb.def->ec_ga) { 550 /* ec and corresponding bc are identical */ 551 gen_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL; 552 } else if (modela.def->ec_ga < modelb.def->ec_ga) { 553 gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET; 554 } else { 555 gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET; 556 } 557 } else if (modela.def->gen < modelb.def->gen) { 558 gen_result = CPU_MODEL_COMPARE_RESULT_SUBSET; 559 } else { 560 gen_result = CPU_MODEL_COMPARE_RESULT_SUPERSET; 561 } 562 if (gen_result != CPU_MODEL_COMPARE_RESULT_IDENTICAL) { 563 /* both models cannot be made identical */ 564 list_add_feat("type", &compare_info->responsible_properties); 565 } 566 567 /* check the feature set */ 568 if (bitmap_equal(modela.features, modelb.features, S390_FEAT_MAX)) { 569 feat_result = CPU_MODEL_COMPARE_RESULT_IDENTICAL; 570 } else { 571 bitmap_andnot(missing, modela.features, modelb.features, S390_FEAT_MAX); 572 s390_feat_bitmap_to_ascii(missing, 573 &compare_info->responsible_properties, 574 list_add_feat); 575 bitmap_andnot(added, modelb.features, modela.features, S390_FEAT_MAX); 576 s390_feat_bitmap_to_ascii(added, &compare_info->responsible_properties, 577 list_add_feat); 578 if (bitmap_empty(missing, S390_FEAT_MAX)) { 579 feat_result = CPU_MODEL_COMPARE_RESULT_SUBSET; 580 } else if (bitmap_empty(added, S390_FEAT_MAX)) { 581 feat_result = CPU_MODEL_COMPARE_RESULT_SUPERSET; 582 } else { 583 feat_result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE; 584 } 585 } 586 587 /* combine the results */ 588 if (gen_result == feat_result) { 589 compare_info->result = gen_result; 590 } else if (feat_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) { 591 compare_info->result = gen_result; 592 } else if (gen_result == CPU_MODEL_COMPARE_RESULT_IDENTICAL) { 593 compare_info->result = feat_result; 594 } else { 595 compare_info->result = CPU_MODEL_COMPARE_RESULT_INCOMPATIBLE; 596 } 597 return compare_info; 598 } 599 600 CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *infoa, 601 CpuModelInfo *infob, 602 Error **errp) 603 { 604 CpuModelBaselineInfo *baseline_info; 605 S390CPUModel modela, modelb, model; 606 uint16_t cpu_type; 607 uint8_t max_gen_ga; 608 uint8_t max_gen; 609 610 /* convert both models to our internal representation */ 611 cpu_model_from_info(&modela, infoa, errp); 612 if (*errp) { 613 return NULL; 614 } 615 616 cpu_model_from_info(&modelb, infob, errp); 617 if (*errp) { 618 return NULL; 619 } 620 621 /* features both models support */ 622 bitmap_and(model.features, modela.features, modelb.features, S390_FEAT_MAX); 623 624 /* detect the maximum model not regarding features */ 625 if (modela.def->gen == modelb.def->gen) { 626 if (modela.def->type == modelb.def->type) { 627 cpu_type = modela.def->type; 628 } else { 629 cpu_type = 0; 630 } 631 max_gen = modela.def->gen; 632 max_gen_ga = MIN(modela.def->ec_ga, modelb.def->ec_ga); 633 } else if (modela.def->gen > modelb.def->gen) { 634 cpu_type = modelb.def->type; 635 max_gen = modelb.def->gen; 636 max_gen_ga = modelb.def->ec_ga; 637 } else { 638 cpu_type = modela.def->type; 639 max_gen = modela.def->gen; 640 max_gen_ga = modela.def->ec_ga; 641 } 642 643 model.def = s390_find_cpu_def(cpu_type, max_gen, max_gen_ga, 644 model.features); 645 /* strip off features not part of the max model */ 646 bitmap_and(model.features, model.features, model.def->full_feat, 647 S390_FEAT_MAX); 648 649 baseline_info = g_malloc0(sizeof(*baseline_info)); 650 baseline_info->model = g_malloc0(sizeof(*baseline_info->model)); 651 cpu_info_from_model(baseline_info->model, &model, true); 652 return baseline_info; 653 } 654 #endif 655 656 static void check_consistency(const S390CPUModel *model) 657 { 658 static int dep[][2] = { 659 { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH }, 660 { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH }, 661 { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH }, 662 { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT }, 663 { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH}, 664 { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT }, 665 { S390_FEAT_DFP_FAST, S390_FEAT_DFP }, 666 { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 }, 667 { S390_FEAT_EDAT_2, S390_FEAT_EDAT}, 668 { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 }, 669 { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 }, 670 { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 }, 671 { S390_FEAT_SIE_CMMA, S390_FEAT_CMM }, 672 { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS }, 673 { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT }, 674 }; 675 int i; 676 677 for (i = 0; i < ARRAY_SIZE(dep); i++) { 678 if (test_bit(dep[i][0], model->features) && 679 !test_bit(dep[i][1], model->features)) { 680 error_report("Warning: \'%s\' requires \'%s\'.", 681 s390_feat_def(dep[i][0])->name, 682 s390_feat_def(dep[i][1])->name); 683 } 684 } 685 } 686 687 static void error_prepend_missing_feat(const char *name, void *opaque) 688 { 689 error_prepend((Error **) opaque, "%s ", name); 690 } 691 692 static void check_compatibility(const S390CPUModel *max_model, 693 const S390CPUModel *model, Error **errp) 694 { 695 S390FeatBitmap missing; 696 697 if (model->def->gen > max_model->def->gen) { 698 error_setg(errp, "Selected CPU generation is too new. Maximum " 699 "supported model in the configuration: \'%s\'", 700 max_model->def->name); 701 return; 702 } else if (model->def->gen == max_model->def->gen && 703 model->def->ec_ga > max_model->def->ec_ga) { 704 error_setg(errp, "Selected CPU GA level is too new. Maximum " 705 "supported model in the configuration: \'%s\'", 706 max_model->def->name); 707 return; 708 } 709 710 /* detect the missing features to properly report them */ 711 bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX); 712 if (bitmap_empty(missing, S390_FEAT_MAX)) { 713 return; 714 } 715 716 error_setg(errp, " "); 717 s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat); 718 error_prepend(errp, "Some features requested in the CPU model are not " 719 "available in the configuration: "); 720 } 721 722 /** 723 * The base TCG CPU model "qemu" is based on the z900. However, we already 724 * can also emulate some additional features of later CPU generations, so 725 * we add these additional feature bits here. 726 */ 727 static void add_qemu_cpu_model_features(S390FeatBitmap fbm) 728 { 729 static const int feats[] = { 730 S390_FEAT_DAT_ENH, 731 S390_FEAT_STFLE, 732 S390_FEAT_EXTENDED_IMMEDIATE, 733 S390_FEAT_EXTENDED_TRANSLATION_2, 734 S390_FEAT_LONG_DISPLACEMENT, 735 S390_FEAT_LONG_DISPLACEMENT_FAST, 736 S390_FEAT_ETF2_ENH, 737 S390_FEAT_STORE_CLOCK_FAST, 738 S390_FEAT_MOVE_WITH_OPTIONAL_SPEC, 739 S390_FEAT_GENERAL_INSTRUCTIONS_EXT, 740 S390_FEAT_EXECUTE_EXT, 741 S390_FEAT_FLOATING_POINT_SUPPPORT_ENH, 742 S390_FEAT_STFLE_45, 743 S390_FEAT_STFLE_49, 744 S390_FEAT_LOCAL_TLB_CLEARING, 745 S390_FEAT_STFLE_53, 746 }; 747 int i; 748 749 for (i = 0; i < ARRAY_SIZE(feats); i++) { 750 set_bit(feats[i], fbm); 751 } 752 } 753 754 static S390CPUModel *get_max_cpu_model(Error **errp) 755 { 756 static S390CPUModel max_model; 757 static bool cached; 758 759 if (cached) { 760 return &max_model; 761 } 762 763 if (kvm_enabled()) { 764 kvm_s390_get_host_cpu_model(&max_model, errp); 765 } else { 766 /* TCG emulates a z900 (with some optional additional features) */ 767 max_model.def = &s390_cpu_defs[0]; 768 bitmap_copy(max_model.features, max_model.def->default_feat, 769 S390_FEAT_MAX); 770 add_qemu_cpu_model_features(max_model.features); 771 } 772 if (!*errp) { 773 cached = true; 774 return &max_model; 775 } 776 return NULL; 777 } 778 779 static inline void apply_cpu_model(const S390CPUModel *model, Error **errp) 780 { 781 #ifndef CONFIG_USER_ONLY 782 static S390CPUModel applied_model; 783 static bool applied; 784 785 /* 786 * We have the same model for all VCPUs. KVM can only be configured before 787 * any VCPUs are defined in KVM. 788 */ 789 if (applied) { 790 if (model && memcmp(&applied_model, model, sizeof(S390CPUModel))) { 791 error_setg(errp, "Mixed CPU models are not supported on s390x."); 792 } 793 return; 794 } 795 796 if (kvm_enabled()) { 797 kvm_s390_apply_cpu_model(model, errp); 798 } 799 800 if (!*errp) { 801 applied = true; 802 if (model) { 803 applied_model = *model; 804 } 805 } 806 #endif 807 } 808 809 void s390_realize_cpu_model(CPUState *cs, Error **errp) 810 { 811 S390CPUClass *xcc = S390_CPU_GET_CLASS(cs); 812 S390CPU *cpu = S390_CPU(cs); 813 const S390CPUModel *max_model; 814 815 if (xcc->kvm_required && !kvm_enabled()) { 816 error_setg(errp, "CPU definition requires KVM"); 817 return; 818 } 819 820 if (!cpu->model) { 821 /* no host model support -> perform compatibility stuff */ 822 apply_cpu_model(NULL, errp); 823 return; 824 } 825 826 max_model = get_max_cpu_model(errp); 827 if (*errp) { 828 error_prepend(errp, "CPU models are not available: "); 829 return; 830 } 831 832 /* copy over properties that can vary */ 833 cpu->model->lowest_ibc = max_model->lowest_ibc; 834 cpu->model->cpu_id = max_model->cpu_id; 835 cpu->model->cpu_id_format = max_model->cpu_id_format; 836 cpu->model->cpu_ver = max_model->cpu_ver; 837 838 check_consistency(cpu->model); 839 check_compatibility(max_model, cpu->model, errp); 840 if (*errp) { 841 return; 842 } 843 844 apply_cpu_model(cpu->model, errp); 845 846 cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model); 847 if (tcg_enabled()) { 848 /* basic mode, write the cpu address into the first 4 bit of the ID */ 849 cpu->env.cpuid = deposit64(cpu->env.cpuid, 54, 4, cpu->env.cpu_num); 850 } 851 } 852 853 static void get_feature(Object *obj, Visitor *v, const char *name, 854 void *opaque, Error **errp) 855 { 856 S390Feat feat = (S390Feat) opaque; 857 S390CPU *cpu = S390_CPU(obj); 858 bool value; 859 860 if (!cpu->model) { 861 error_setg(errp, "Details about the host CPU model are not available, " 862 "features cannot be queried."); 863 return; 864 } 865 866 value = test_bit(feat, cpu->model->features); 867 visit_type_bool(v, name, &value, errp); 868 } 869 870 static void set_feature(Object *obj, Visitor *v, const char *name, 871 void *opaque, Error **errp) 872 { 873 S390Feat feat = (S390Feat) opaque; 874 DeviceState *dev = DEVICE(obj); 875 S390CPU *cpu = S390_CPU(obj); 876 bool value; 877 878 if (dev->realized) { 879 error_setg(errp, "Attempt to set property '%s' on '%s' after " 880 "it was realized", name, object_get_typename(obj)); 881 return; 882 } else if (!cpu->model) { 883 error_setg(errp, "Details about the host CPU model are not available, " 884 "features cannot be changed."); 885 return; 886 } 887 888 visit_type_bool(v, name, &value, errp); 889 if (*errp) { 890 return; 891 } 892 if (value) { 893 if (!test_bit(feat, cpu->model->def->full_feat)) { 894 error_setg(errp, "Feature '%s' is not available for CPU model '%s'," 895 " it was introduced with later models.", 896 name, cpu->model->def->name); 897 return; 898 } 899 set_bit(feat, cpu->model->features); 900 } else { 901 clear_bit(feat, cpu->model->features); 902 } 903 } 904 905 static void get_feature_group(Object *obj, Visitor *v, const char *name, 906 void *opaque, Error **errp) 907 { 908 S390FeatGroup group = (S390FeatGroup) opaque; 909 const S390FeatGroupDef *def = s390_feat_group_def(group); 910 S390CPU *cpu = S390_CPU(obj); 911 S390FeatBitmap tmp; 912 bool value; 913 914 if (!cpu->model) { 915 error_setg(errp, "Details about the host CPU model are not available, " 916 "features cannot be queried."); 917 return; 918 } 919 920 /* a group is enabled if all features are enabled */ 921 bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX); 922 value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX); 923 visit_type_bool(v, name, &value, errp); 924 } 925 926 static void set_feature_group(Object *obj, Visitor *v, const char *name, 927 void *opaque, Error **errp) 928 { 929 S390FeatGroup group = (S390FeatGroup) opaque; 930 const S390FeatGroupDef *def = s390_feat_group_def(group); 931 DeviceState *dev = DEVICE(obj); 932 S390CPU *cpu = S390_CPU(obj); 933 bool value; 934 935 if (dev->realized) { 936 error_setg(errp, "Attempt to set property '%s' on '%s' after " 937 "it was realized", name, object_get_typename(obj)); 938 return; 939 } else if (!cpu->model) { 940 error_setg(errp, "Details about the host CPU model are not available, " 941 "features cannot be changed."); 942 return; 943 } 944 945 visit_type_bool(v, name, &value, errp); 946 if (*errp) { 947 return; 948 } 949 if (value) { 950 /* groups are added in one shot, so an intersect is sufficient */ 951 if (!bitmap_intersects(def->feat, cpu->model->def->full_feat, 952 S390_FEAT_MAX)) { 953 error_setg(errp, "Group '%s' is not available for CPU model '%s'," 954 " it was introduced with later models.", 955 name, cpu->model->def->name); 956 return; 957 } 958 bitmap_or(cpu->model->features, cpu->model->features, def->feat, 959 S390_FEAT_MAX); 960 } else { 961 bitmap_andnot(cpu->model->features, cpu->model->features, def->feat, 962 S390_FEAT_MAX); 963 } 964 } 965 966 void s390_cpu_model_register_props(Object *obj) 967 { 968 S390FeatGroup group; 969 S390Feat feat; 970 971 for (feat = 0; feat < S390_FEAT_MAX; feat++) { 972 const S390FeatDef *def = s390_feat_def(feat); 973 object_property_add(obj, def->name, "bool", get_feature, 974 set_feature, NULL, (void *) feat, NULL); 975 object_property_set_description(obj, def->name, def->desc , NULL); 976 } 977 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) { 978 const S390FeatGroupDef *def = s390_feat_group_def(group); 979 object_property_add(obj, def->name, "bool", get_feature_group, 980 set_feature_group, NULL, (void *) group, NULL); 981 object_property_set_description(obj, def->name, def->desc , NULL); 982 } 983 } 984 985 static void s390_cpu_model_initfn(Object *obj) 986 { 987 S390CPU *cpu = S390_CPU(obj); 988 S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu); 989 990 cpu->model = g_malloc0(sizeof(*cpu->model)); 991 /* copy the model, so we can modify it */ 992 cpu->model->def = xcc->cpu_def; 993 if (xcc->is_static) { 994 /* base model - features will never change */ 995 bitmap_copy(cpu->model->features, cpu->model->def->base_feat, 996 S390_FEAT_MAX); 997 } else { 998 /* latest model - features can change */ 999 bitmap_copy(cpu->model->features, 1000 cpu->model->def->default_feat, S390_FEAT_MAX); 1001 } 1002 } 1003 1004 #ifdef CONFIG_KVM 1005 static void s390_host_cpu_model_initfn(Object *obj) 1006 { 1007 S390CPU *cpu = S390_CPU(obj); 1008 Error *err = NULL; 1009 1010 if (!kvm_enabled() || !kvm_s390_cpu_models_supported()) { 1011 return; 1012 } 1013 1014 cpu->model = g_malloc0(sizeof(*cpu->model)); 1015 kvm_s390_get_host_cpu_model(cpu->model, &err); 1016 if (err) { 1017 error_report_err(err); 1018 g_free(cpu->model); 1019 /* fallback to unsupported cpu models */ 1020 cpu->model = NULL; 1021 } 1022 } 1023 #endif 1024 1025 static void s390_qemu_cpu_model_initfn(Object *obj) 1026 { 1027 static S390CPUDef s390_qemu_cpu_defs; 1028 S390CPU *cpu = S390_CPU(obj); 1029 1030 cpu->model = g_malloc0(sizeof(*cpu->model)); 1031 /* TCG emulates a z900 (with some optional additional features) */ 1032 memcpy(&s390_qemu_cpu_defs, &s390_cpu_defs[0], sizeof(s390_qemu_cpu_defs)); 1033 add_qemu_cpu_model_features(s390_qemu_cpu_defs.full_feat); 1034 cpu->model->def = &s390_qemu_cpu_defs; 1035 bitmap_copy(cpu->model->features, cpu->model->def->default_feat, 1036 S390_FEAT_MAX); 1037 } 1038 1039 static void s390_cpu_model_finalize(Object *obj) 1040 { 1041 S390CPU *cpu = S390_CPU(obj); 1042 1043 g_free(cpu->model); 1044 cpu->model = NULL; 1045 } 1046 1047 static bool get_is_migration_safe(Object *obj, Error **errp) 1048 { 1049 return S390_CPU_GET_CLASS(obj)->is_migration_safe; 1050 } 1051 1052 static bool get_is_static(Object *obj, Error **errp) 1053 { 1054 return S390_CPU_GET_CLASS(obj)->is_static; 1055 } 1056 1057 static char *get_description(Object *obj, Error **errp) 1058 { 1059 return g_strdup(S390_CPU_GET_CLASS(obj)->desc); 1060 } 1061 1062 void s390_cpu_model_class_register_props(ObjectClass *oc) 1063 { 1064 object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe, 1065 NULL, NULL); 1066 object_class_property_add_bool(oc, "static", get_is_static, 1067 NULL, NULL); 1068 object_class_property_add_str(oc, "description", get_description, NULL, 1069 NULL); 1070 } 1071 1072 #ifdef CONFIG_KVM 1073 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data) 1074 { 1075 S390CPUClass *xcc = S390_CPU_CLASS(oc); 1076 1077 xcc->kvm_required = true; 1078 xcc->desc = "KVM only: All recognized features"; 1079 } 1080 #endif 1081 1082 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data) 1083 { 1084 S390CPUClass *xcc = S390_CPU_CLASS(oc); 1085 1086 /* all base models are migration safe */ 1087 xcc->cpu_def = (const S390CPUDef *) data; 1088 xcc->is_migration_safe = true; 1089 xcc->is_static = true; 1090 xcc->desc = xcc->cpu_def->desc; 1091 } 1092 1093 static void s390_cpu_model_class_init(ObjectClass *oc, void *data) 1094 { 1095 S390CPUClass *xcc = S390_CPU_CLASS(oc); 1096 1097 /* model that can change between QEMU versions */ 1098 xcc->cpu_def = (const S390CPUDef *) data; 1099 xcc->is_migration_safe = true; 1100 xcc->desc = xcc->cpu_def->desc; 1101 } 1102 1103 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data) 1104 { 1105 S390CPUClass *xcc = S390_CPU_CLASS(oc); 1106 1107 xcc->is_migration_safe = true; 1108 xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s", 1109 qemu_hw_version()); 1110 } 1111 1112 #define S390_CPU_TYPE_SUFFIX "-" TYPE_S390_CPU 1113 #define S390_CPU_TYPE_NAME(name) (name S390_CPU_TYPE_SUFFIX) 1114 1115 /* Generate type name for a cpu model. Caller has to free the string. */ 1116 static char *s390_cpu_type_name(const char *model_name) 1117 { 1118 return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name); 1119 } 1120 1121 /* Generate type name for a base cpu model. Caller has to free the string. */ 1122 static char *s390_base_cpu_type_name(const char *model_name) 1123 { 1124 return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name); 1125 } 1126 1127 ObjectClass *s390_cpu_class_by_name(const char *name) 1128 { 1129 char *typename = s390_cpu_type_name(name); 1130 ObjectClass *oc; 1131 1132 oc = object_class_by_name(typename); 1133 g_free(typename); 1134 return oc; 1135 } 1136 1137 static const TypeInfo qemu_s390_cpu_type_info = { 1138 .name = S390_CPU_TYPE_NAME("qemu"), 1139 .parent = TYPE_S390_CPU, 1140 .instance_init = s390_qemu_cpu_model_initfn, 1141 .instance_finalize = s390_cpu_model_finalize, 1142 .class_init = s390_qemu_cpu_model_class_init, 1143 }; 1144 1145 #ifdef CONFIG_KVM 1146 static const TypeInfo host_s390_cpu_type_info = { 1147 .name = S390_CPU_TYPE_NAME("host"), 1148 .parent = TYPE_S390_CPU, 1149 .instance_init = s390_host_cpu_model_initfn, 1150 .instance_finalize = s390_cpu_model_finalize, 1151 .class_init = s390_host_cpu_model_class_init, 1152 }; 1153 #endif 1154 1155 static void register_types(void) 1156 { 1157 int i; 1158 1159 /* init all bitmaps from gnerated data initially */ 1160 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 1161 s390_init_feat_bitmap(s390_cpu_defs[i].base_init, 1162 s390_cpu_defs[i].base_feat); 1163 s390_init_feat_bitmap(s390_cpu_defs[i].default_init, 1164 s390_cpu_defs[i].default_feat); 1165 s390_init_feat_bitmap(s390_cpu_defs[i].full_init, 1166 s390_cpu_defs[i].full_feat); 1167 } 1168 1169 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 1170 char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name); 1171 TypeInfo ti_base = { 1172 .name = base_name, 1173 .parent = TYPE_S390_CPU, 1174 .instance_init = s390_cpu_model_initfn, 1175 .instance_finalize = s390_cpu_model_finalize, 1176 .class_init = s390_base_cpu_model_class_init, 1177 .class_data = (void *) &s390_cpu_defs[i], 1178 }; 1179 char *name = s390_cpu_type_name(s390_cpu_defs[i].name); 1180 TypeInfo ti = { 1181 .name = name, 1182 .parent = TYPE_S390_CPU, 1183 .instance_init = s390_cpu_model_initfn, 1184 .instance_finalize = s390_cpu_model_finalize, 1185 .class_init = s390_cpu_model_class_init, 1186 .class_data = (void *) &s390_cpu_defs[i], 1187 }; 1188 1189 type_register_static(&ti_base); 1190 type_register_static(&ti); 1191 g_free(base_name); 1192 g_free(name); 1193 } 1194 1195 type_register_static(&qemu_s390_cpu_type_info); 1196 #ifdef CONFIG_KVM 1197 type_register_static(&host_s390_cpu_type_info); 1198 #endif 1199 } 1200 1201 type_init(register_types) 1202