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 "s390x-internal.h" 16 #include "kvm/kvm_s390x.h" 17 #include "sysemu/kvm.h" 18 #include "sysemu/tcg.h" 19 #include "qapi/error.h" 20 #include "qemu/error-report.h" 21 #include "qapi/visitor.h" 22 #include "qemu/module.h" 23 #include "qemu/hw-version.h" 24 #include "qemu/qemu-print.h" 25 #ifndef CONFIG_USER_ONLY 26 #include "sysemu/sysemu.h" 27 #include "target/s390x/kvm/pv.h" 28 #include CONFIG_DEVICES 29 #endif 30 31 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \ 32 { \ 33 .name = _name, \ 34 .type = _type, \ 35 .gen = _gen, \ 36 .ec_ga = _ec_ga, \ 37 .mha_pow = _mha_pow, \ 38 .hmfai = _hmfai, \ 39 .desc = _desc, \ 40 .base_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _BASE }, \ 41 .default_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _DEFAULT }, \ 42 .full_init = { S390_FEAT_LIST_GEN ## _gen ## _GA ## _ec_ga ## _FULL }, \ 43 } 44 45 /* 46 * CPU definition list in order of release. Up to generation 14 base features 47 * of a following release have been a superset of the previous release. With 48 * generation 15 one base feature and one optional feature have been deprecated. 49 */ 50 static S390CPUDef s390_cpu_defs[] = { 51 /* 52 * Linux requires at least z10 nowadays, and IBM only supports recent CPUs 53 * (see https://www.ibm.com/support/pages/ibm-mainframe-life-cycle-history), 54 * so we consider older CPUs as legacy that can optionally be disabled via 55 * the CONFIG_S390X_LEGACY_CPUS config switch. 56 */ 57 #if defined(CONFIG_S390X_LEGACY_CPUS) || defined(CONFIG_USER_ONLY) 58 CPUDEF_INIT(0x2064, 7, 1, 38, 0x00000000U, "z900", "IBM zSeries 900 GA1"), 59 CPUDEF_INIT(0x2064, 7, 2, 38, 0x00000000U, "z900.2", "IBM zSeries 900 GA2"), 60 CPUDEF_INIT(0x2064, 7, 3, 38, 0x00000000U, "z900.3", "IBM zSeries 900 GA3"), 61 CPUDEF_INIT(0x2066, 7, 3, 38, 0x00000000U, "z800", "IBM zSeries 800 GA1"), 62 CPUDEF_INIT(0x2084, 8, 1, 38, 0x00000000U, "z990", "IBM zSeries 990 GA1"), 63 CPUDEF_INIT(0x2084, 8, 2, 38, 0x00000000U, "z990.2", "IBM zSeries 990 GA2"), 64 CPUDEF_INIT(0x2084, 8, 3, 38, 0x00000000U, "z990.3", "IBM zSeries 990 GA3"), 65 CPUDEF_INIT(0x2086, 8, 3, 38, 0x00000000U, "z890", "IBM zSeries 880 GA1"), 66 CPUDEF_INIT(0x2084, 8, 4, 38, 0x00000000U, "z990.4", "IBM zSeries 990 GA4"), 67 CPUDEF_INIT(0x2086, 8, 4, 38, 0x00000000U, "z890.2", "IBM zSeries 880 GA2"), 68 CPUDEF_INIT(0x2084, 8, 5, 38, 0x00000000U, "z990.5", "IBM zSeries 990 GA5"), 69 CPUDEF_INIT(0x2086, 8, 5, 38, 0x00000000U, "z890.3", "IBM zSeries 880 GA3"), 70 CPUDEF_INIT(0x2094, 9, 1, 40, 0x00000000U, "z9EC", "IBM System z9 EC GA1"), 71 CPUDEF_INIT(0x2094, 9, 2, 40, 0x00000000U, "z9EC.2", "IBM System z9 EC GA2"), 72 CPUDEF_INIT(0x2096, 9, 2, 40, 0x00000000U, "z9BC", "IBM System z9 BC GA1"), 73 CPUDEF_INIT(0x2094, 9, 3, 40, 0x00000000U, "z9EC.3", "IBM System z9 EC GA3"), 74 CPUDEF_INIT(0x2096, 9, 3, 40, 0x00000000U, "z9BC.2", "IBM System z9 BC GA2"), 75 #endif 76 CPUDEF_INIT(0x2097, 10, 1, 43, 0x00000000U, "z10EC", "IBM System z10 EC GA1"), 77 CPUDEF_INIT(0x2097, 10, 2, 43, 0x00000000U, "z10EC.2", "IBM System z10 EC GA2"), 78 CPUDEF_INIT(0x2098, 10, 2, 43, 0x00000000U, "z10BC", "IBM System z10 BC GA1"), 79 CPUDEF_INIT(0x2097, 10, 3, 43, 0x00000000U, "z10EC.3", "IBM System z10 EC GA3"), 80 CPUDEF_INIT(0x2098, 10, 3, 43, 0x00000000U, "z10BC.2", "IBM System z10 BC GA2"), 81 CPUDEF_INIT(0x2817, 11, 1, 44, 0x08000000U, "z196", "IBM zEnterprise 196 GA1"), 82 CPUDEF_INIT(0x2817, 11, 2, 44, 0x08000000U, "z196.2", "IBM zEnterprise 196 GA2"), 83 CPUDEF_INIT(0x2818, 11, 2, 44, 0x08000000U, "z114", "IBM zEnterprise 114 GA1"), 84 CPUDEF_INIT(0x2827, 12, 1, 44, 0x08000000U, "zEC12", "IBM zEnterprise EC12 GA1"), 85 CPUDEF_INIT(0x2827, 12, 2, 44, 0x08000000U, "zEC12.2", "IBM zEnterprise EC12 GA2"), 86 CPUDEF_INIT(0x2828, 12, 2, 44, 0x08000000U, "zBC12", "IBM zEnterprise BC12 GA1"), 87 CPUDEF_INIT(0x2964, 13, 1, 47, 0x08000000U, "z13", "IBM z13 GA1"), 88 CPUDEF_INIT(0x2964, 13, 2, 47, 0x08000000U, "z13.2", "IBM z13 GA2"), 89 CPUDEF_INIT(0x2965, 13, 2, 47, 0x08000000U, "z13s", "IBM z13s GA1"), 90 CPUDEF_INIT(0x3906, 14, 1, 47, 0x08000000U, "z14", "IBM z14 GA1"), 91 CPUDEF_INIT(0x3906, 14, 2, 47, 0x08000000U, "z14.2", "IBM z14 GA2"), 92 CPUDEF_INIT(0x3907, 14, 1, 47, 0x08000000U, "z14ZR1", "IBM z14 Model ZR1 GA1"), 93 CPUDEF_INIT(0x8561, 15, 1, 47, 0x08000000U, "gen15a", "IBM z15 T01 GA1"), 94 CPUDEF_INIT(0x8562, 15, 1, 47, 0x08000000U, "gen15b", "IBM z15 T02 GA1"), 95 CPUDEF_INIT(0x3931, 16, 1, 47, 0x08000000U, "gen16a", "IBM 3931 GA1"), 96 CPUDEF_INIT(0x3932, 16, 1, 47, 0x08000000U, "gen16b", "IBM 3932 GA1"), 97 }; 98 99 #define QEMU_MAX_CPU_TYPE 0x8561 100 #define QEMU_MAX_CPU_GEN 15 101 #define QEMU_MAX_CPU_EC_GA 1 102 static S390FeatBitmap qemu_max_cpu_feat; 103 104 /* features part of a base model but not relevant for finding a base model */ 105 S390FeatBitmap ignored_base_feat; 106 107 void s390_cpudef_featoff(uint8_t gen, uint8_t ec_ga, S390Feat feat) 108 { 109 const S390CPUDef *def; 110 111 def = s390_find_cpu_def(0, gen, ec_ga, NULL); 112 clear_bit(feat, (unsigned long *)&def->default_feat); 113 } 114 115 void s390_cpudef_featoff_greater(uint8_t gen, uint8_t ec_ga, S390Feat feat) 116 { 117 int i; 118 119 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 120 const S390CPUDef *def = &s390_cpu_defs[i]; 121 122 if (def->gen < gen) { 123 continue; 124 } 125 if (def->gen == gen && def->ec_ga < ec_ga) { 126 continue; 127 } 128 129 clear_bit(feat, (unsigned long *)&def->default_feat); 130 } 131 } 132 133 void s390_cpudef_group_featoff_greater(uint8_t gen, uint8_t ec_ga, 134 S390FeatGroup group) 135 { 136 const S390FeatGroupDef *group_def = s390_feat_group_def(group); 137 S390FeatBitmap group_def_off; 138 int i; 139 140 bitmap_complement(group_def_off, group_def->feat, S390_FEAT_MAX); 141 142 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 143 const S390CPUDef *cpu_def = &s390_cpu_defs[i]; 144 145 if (cpu_def->gen < gen) { 146 continue; 147 } 148 if (cpu_def->gen == gen && cpu_def->ec_ga < ec_ga) { 149 continue; 150 } 151 152 bitmap_and((unsigned long *)&cpu_def->default_feat, 153 cpu_def->default_feat, group_def_off, S390_FEAT_MAX); 154 } 155 } 156 157 uint32_t s390_get_hmfai(void) 158 { 159 static S390CPU *cpu; 160 161 if (!cpu) { 162 cpu = S390_CPU(qemu_get_cpu(0)); 163 } 164 165 if (!cpu || !cpu->model) { 166 return 0; 167 } 168 return cpu->model->def->hmfai; 169 } 170 171 uint8_t s390_get_mha_pow(void) 172 { 173 static S390CPU *cpu; 174 175 if (!cpu) { 176 cpu = S390_CPU(qemu_get_cpu(0)); 177 } 178 179 if (!cpu || !cpu->model) { 180 return 0; 181 } 182 return cpu->model->def->mha_pow; 183 } 184 185 uint32_t s390_get_ibc_val(void) 186 { 187 uint16_t unblocked_ibc, lowest_ibc; 188 static S390CPU *cpu; 189 190 if (!cpu) { 191 cpu = S390_CPU(qemu_get_cpu(0)); 192 } 193 194 if (!cpu || !cpu->model) { 195 return 0; 196 } 197 unblocked_ibc = s390_ibc_from_cpu_model(cpu->model); 198 lowest_ibc = cpu->model->lowest_ibc; 199 /* the lowest_ibc always has to be <= unblocked_ibc */ 200 if (!lowest_ibc || lowest_ibc > unblocked_ibc) { 201 return 0; 202 } 203 return ((uint32_t) lowest_ibc << 16) | unblocked_ibc; 204 } 205 206 void s390_get_feat_block(S390FeatType type, uint8_t *data) 207 { 208 S390CPU *cpu = S390_CPU(first_cpu); 209 210 if (!cpu || !cpu->model) { 211 return; 212 } 213 s390_fill_feat_block(cpu->model->features, type, data); 214 } 215 216 bool s390_has_feat(S390Feat feat) 217 { 218 static S390CPU *cpu; 219 220 if (!cpu) { 221 cpu = S390_CPU(qemu_get_cpu(0)); 222 } 223 224 if (!cpu || !cpu->model) { 225 #ifdef CONFIG_KVM 226 if (kvm_enabled()) { 227 if (feat == S390_FEAT_VECTOR) { 228 return kvm_check_extension(kvm_state, 229 KVM_CAP_S390_VECTOR_REGISTERS); 230 } 231 if (feat == S390_FEAT_RUNTIME_INSTRUMENTATION) { 232 return kvm_s390_get_ri(); 233 } 234 if (feat == S390_FEAT_MSA_EXT_3) { 235 return true; 236 } 237 } 238 #endif 239 if (feat == S390_FEAT_ZPCI) { 240 return true; 241 } 242 return 0; 243 } 244 245 #ifndef CONFIG_USER_ONLY 246 if (s390_is_pv()) { 247 switch (feat) { 248 case S390_FEAT_DIAG_318: 249 case S390_FEAT_HPMA2: 250 case S390_FEAT_SIE_F2: 251 case S390_FEAT_SIE_SKEY: 252 case S390_FEAT_SIE_GPERE: 253 case S390_FEAT_SIE_SIIF: 254 case S390_FEAT_SIE_SIGPIF: 255 case S390_FEAT_SIE_IB: 256 case S390_FEAT_SIE_CEI: 257 case S390_FEAT_SIE_KSS: 258 case S390_FEAT_SIE_GSLS: 259 case S390_FEAT_SIE_64BSCAO: 260 case S390_FEAT_SIE_CMMA: 261 case S390_FEAT_SIE_PFMFI: 262 case S390_FEAT_SIE_IBS: 263 case S390_FEAT_CONFIGURATION_TOPOLOGY: 264 return false; 265 break; 266 default: 267 break; 268 } 269 } 270 #endif 271 return test_bit(feat, cpu->model->features); 272 } 273 274 uint8_t s390_get_gen_for_cpu_type(uint16_t type) 275 { 276 int i; 277 278 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 279 if (s390_cpu_defs[i].type == type) { 280 return s390_cpu_defs[i].gen; 281 } 282 } 283 return 0; 284 } 285 286 const S390CPUDef *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga, 287 S390FeatBitmap features) 288 { 289 const S390CPUDef *last_compatible = NULL; 290 const S390CPUDef *matching_cpu_type = NULL; 291 int i; 292 293 if (!gen) { 294 ec_ga = 0; 295 } 296 if (!gen && type) { 297 gen = s390_get_gen_for_cpu_type(type); 298 } 299 300 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 301 const S390CPUDef *def = &s390_cpu_defs[i]; 302 S390FeatBitmap missing; 303 304 /* don't even try newer generations if we know the generation */ 305 if (gen) { 306 if (def->gen > gen) { 307 break; 308 } else if (def->gen == gen && ec_ga && def->ec_ga > ec_ga) { 309 break; 310 } 311 } 312 313 if (features) { 314 /* see if the model satisfies the minimum features */ 315 bitmap_andnot(missing, def->base_feat, features, S390_FEAT_MAX); 316 /* 317 * Ignore certain features that are in the base model, but not 318 * relevant for the search (esp. MSA subfunctions). 319 */ 320 bitmap_andnot(missing, missing, ignored_base_feat, S390_FEAT_MAX); 321 if (!bitmap_empty(missing, S390_FEAT_MAX)) { 322 break; 323 } 324 } 325 326 /* stop the search if we found the exact model */ 327 if (def->type == type && def->ec_ga == ec_ga) { 328 return def; 329 } 330 /* remember if we've at least seen one with the same cpu type */ 331 if (def->type == type) { 332 matching_cpu_type = def; 333 } 334 last_compatible = def; 335 } 336 /* prefer the model with the same cpu type, esp. don't take the BC for EC */ 337 if (matching_cpu_type) { 338 return matching_cpu_type; 339 } 340 return last_compatible; 341 } 342 343 static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data) 344 { 345 const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data); 346 CPUClass *cc = CPU_CLASS(scc); 347 char *name = g_strdup(object_class_get_name((ObjectClass *)data)); 348 g_autoptr(GString) details = g_string_new(""); 349 350 if (scc->is_static) { 351 g_string_append(details, "static, "); 352 } 353 if (scc->is_migration_safe) { 354 g_string_append(details, "migration-safe, "); 355 } 356 if (cc->deprecation_note) { 357 g_string_append(details, "deprecated, "); 358 } 359 if (details->len) { 360 /* cull trailing ', ' */ 361 g_string_truncate(details, details->len - 2); 362 } 363 364 /* strip off the -s390x-cpu */ 365 g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0; 366 if (details->len) { 367 qemu_printf(" %-15s %-35s (%s)\n", name, scc->desc, details->str); 368 } else { 369 qemu_printf(" %-15s %-35s\n", name, scc->desc); 370 } 371 g_free(name); 372 } 373 374 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b) 375 { 376 const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a); 377 const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b); 378 const char *name_a = object_class_get_name((ObjectClass *)a); 379 const char *name_b = object_class_get_name((ObjectClass *)b); 380 381 /* 382 * Move qemu, host and max to the top of the list, qemu first, host second, 383 * max third. 384 */ 385 if (name_a[0] == 'q') { 386 return -1; 387 } else if (name_b[0] == 'q') { 388 return 1; 389 } else if (name_a[0] == 'h') { 390 return -1; 391 } else if (name_b[0] == 'h') { 392 return 1; 393 } else if (name_a[0] == 'm') { 394 return -1; 395 } else if (name_b[0] == 'm') { 396 return 1; 397 } 398 399 /* keep the same order we have in our table (sorted by release date) */ 400 if (cc_a->cpu_def != cc_b->cpu_def) { 401 return cc_a->cpu_def - cc_b->cpu_def; 402 } 403 404 /* exact same definition - list base model first */ 405 return cc_a->is_static ? -1 : 1; 406 } 407 408 void s390_cpu_list(void) 409 { 410 S390FeatGroup group; 411 S390Feat feat; 412 GSList *list; 413 414 qemu_printf("Available CPUs:\n"); 415 list = object_class_get_list(TYPE_S390_CPU, false); 416 list = g_slist_sort(list, s390_cpu_list_compare); 417 g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL); 418 g_slist_free(list); 419 420 qemu_printf("\nRecognized feature flags:\n"); 421 for (feat = 0; feat < S390_FEAT_MAX; feat++) { 422 const S390FeatDef *def = s390_feat_def(feat); 423 424 qemu_printf(" %-20s %s\n", def->name, def->desc); 425 } 426 427 qemu_printf("\nRecognized feature groups:\n"); 428 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) { 429 const S390FeatGroupDef *def = s390_feat_group_def(group); 430 431 qemu_printf(" %-20s %s\n", def->name, def->desc); 432 } 433 } 434 435 static void check_consistency(const S390CPUModel *model) 436 { 437 static int dep[][2] = { 438 { S390_FEAT_IPTE_RANGE, S390_FEAT_DAT_ENH }, 439 { S390_FEAT_IDTE_SEGMENT, S390_FEAT_DAT_ENH }, 440 { S390_FEAT_IDTE_REGION, S390_FEAT_DAT_ENH }, 441 { S390_FEAT_IDTE_REGION, S390_FEAT_IDTE_SEGMENT }, 442 { S390_FEAT_LOCAL_TLB_CLEARING, S390_FEAT_DAT_ENH}, 443 { S390_FEAT_LONG_DISPLACEMENT_FAST, S390_FEAT_LONG_DISPLACEMENT }, 444 { S390_FEAT_DFP_FAST, S390_FEAT_DFP }, 445 { S390_FEAT_TRANSACTIONAL_EXE, S390_FEAT_STFLE_49 }, 446 { S390_FEAT_EDAT_2, S390_FEAT_EDAT}, 447 { S390_FEAT_MSA_EXT_5, S390_FEAT_KIMD_SHA_512 }, 448 { S390_FEAT_MSA_EXT_5, S390_FEAT_KLMD_SHA_512 }, 449 { S390_FEAT_MSA_EXT_4, S390_FEAT_MSA_EXT_3 }, 450 { S390_FEAT_SIE_CMMA, S390_FEAT_CMM }, 451 { S390_FEAT_SIE_CMMA, S390_FEAT_SIE_GSLS }, 452 { S390_FEAT_SIE_PFMFI, S390_FEAT_EDAT }, 453 { S390_FEAT_MSA_EXT_8, S390_FEAT_MSA_EXT_3 }, 454 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_3 }, 455 { S390_FEAT_MSA_EXT_9, S390_FEAT_MSA_EXT_4 }, 456 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING }, 457 { S390_FEAT_VECTOR_PACKED_DECIMAL, S390_FEAT_VECTOR }, 458 { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH, S390_FEAT_VECTOR_PACKED_DECIMAL }, 459 { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH }, 460 { S390_FEAT_VECTOR_PACKED_DECIMAL_ENH3, S390_FEAT_VECTOR_PACKED_DECIMAL_ENH2 }, 461 { S390_FEAT_VECTOR_ENH, S390_FEAT_VECTOR }, 462 { S390_FEAT_VECTOR_ENH2, S390_FEAT_VECTOR_ENH }, 463 { S390_FEAT_VECTOR_ENH3, S390_FEAT_VECTOR_ENH2 }, 464 { S390_FEAT_INSTRUCTION_EXEC_PROT, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 }, 465 { S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2, S390_FEAT_ESOP }, 466 { S390_FEAT_CMM_NT, S390_FEAT_CMM }, 467 { S390_FEAT_GUARDED_STORAGE, S390_FEAT_SIDE_EFFECT_ACCESS_ESOP2 }, 468 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_STORE_CLOCK_FAST }, 469 { S390_FEAT_MULTIPLE_EPOCH, S390_FEAT_TOD_CLOCK_STEERING }, 470 { S390_FEAT_SEMAPHORE_ASSIST, S390_FEAT_STFLE_49 }, 471 { S390_FEAT_KIMD_SHA3_224, S390_FEAT_MSA }, 472 { S390_FEAT_KIMD_SHA3_256, S390_FEAT_MSA }, 473 { S390_FEAT_KIMD_SHA3_384, S390_FEAT_MSA }, 474 { S390_FEAT_KIMD_SHA3_512, S390_FEAT_MSA }, 475 { S390_FEAT_KIMD_SHAKE_128, S390_FEAT_MSA }, 476 { S390_FEAT_KIMD_SHAKE_256, S390_FEAT_MSA }, 477 { S390_FEAT_KLMD_SHA3_224, S390_FEAT_MSA }, 478 { S390_FEAT_KLMD_SHA3_256, S390_FEAT_MSA }, 479 { S390_FEAT_KLMD_SHA3_384, S390_FEAT_MSA }, 480 { S390_FEAT_KLMD_SHA3_512, S390_FEAT_MSA }, 481 { S390_FEAT_KLMD_SHAKE_128, S390_FEAT_MSA }, 482 { S390_FEAT_KLMD_SHAKE_256, S390_FEAT_MSA }, 483 { S390_FEAT_KMAC_HMAC_SHA_224, S390_FEAT_MSA_EXT_3 }, 484 { S390_FEAT_KMAC_HMAC_SHA_256, S390_FEAT_MSA_EXT_3 }, 485 { S390_FEAT_KMAC_HMAC_SHA_384, S390_FEAT_MSA_EXT_3 }, 486 { S390_FEAT_KMAC_HMAC_SHA_512, S390_FEAT_MSA_EXT_3 }, 487 { S390_FEAT_KMAC_HMAC_ESHA_224, S390_FEAT_MSA_EXT_3 }, 488 { S390_FEAT_KMAC_HMAC_ESHA_256, S390_FEAT_MSA_EXT_3 }, 489 { S390_FEAT_KMAC_HMAC_ESHA_384, S390_FEAT_MSA_EXT_3 }, 490 { S390_FEAT_KMAC_HMAC_ESHA_512, S390_FEAT_MSA_EXT_3 }, 491 { S390_FEAT_KM_FULL_XTS_AES_128, S390_FEAT_MSA_EXT_4 }, 492 { S390_FEAT_KM_FULL_XTS_AES_256, S390_FEAT_MSA_EXT_4 }, 493 { S390_FEAT_KM_FULL_XTS_EAES_128, S390_FEAT_MSA_EXT_4 }, 494 { S390_FEAT_KM_FULL_XTS_EAES_256, S390_FEAT_MSA_EXT_4 }, 495 { S390_FEAT_PRNO_TRNG_QRTCR, S390_FEAT_MSA_EXT_5 }, 496 { S390_FEAT_PRNO_TRNG, S390_FEAT_MSA_EXT_5 }, 497 { S390_FEAT_SIE_KSS, S390_FEAT_SIE_F2 }, 498 { S390_FEAT_AP_QUERY_CONFIG_INFO, S390_FEAT_AP }, 499 { S390_FEAT_AP_FACILITIES_TEST, S390_FEAT_AP }, 500 { S390_FEAT_PTFF_QSIE, S390_FEAT_MULTIPLE_EPOCH }, 501 { S390_FEAT_PTFF_QTOUE, S390_FEAT_MULTIPLE_EPOCH }, 502 { S390_FEAT_PTFF_STOE, S390_FEAT_MULTIPLE_EPOCH }, 503 { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH }, 504 { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP }, 505 { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB }, 506 { S390_FEAT_NNPA, S390_FEAT_VECTOR }, 507 { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING }, 508 { S390_FEAT_UV_FEAT_AP, S390_FEAT_AP }, 509 { S390_FEAT_UV_FEAT_AP_INTR, S390_FEAT_UV_FEAT_AP }, 510 { S390_FEAT_PFCR_QAF, S390_FEAT_CCF_BASE }, 511 { S390_FEAT_PFCR_CSDST, S390_FEAT_CCF_BASE }, 512 { S390_FEAT_PFCR_CSDSTG, S390_FEAT_CCF_BASE }, 513 { S390_FEAT_PFCR_CSTST, S390_FEAT_CCF_BASE }, 514 { S390_FEAT_PFCR_CSTSTG, S390_FEAT_CCF_BASE }, 515 }; 516 int i; 517 518 for (i = 0; i < ARRAY_SIZE(dep); i++) { 519 if (test_bit(dep[i][0], model->features) && 520 !test_bit(dep[i][1], model->features)) { 521 warn_report("\'%s\' requires \'%s\'.", 522 s390_feat_def(dep[i][0])->name, 523 s390_feat_def(dep[i][1])->name); 524 } 525 } 526 } 527 528 static void error_prepend_missing_feat(const char *name, void *opaque) 529 { 530 error_prepend((Error **) opaque, "%s ", name); 531 } 532 533 static void check_compat_model_failed(Error **errp, 534 const S390CPUModel *max_model, 535 const char *msg) 536 { 537 error_setg(errp, "%s. Maximum supported model in the current configuration: \'%s\'", 538 msg, max_model->def->name); 539 error_append_hint(errp, "Consider a different accelerator, try \"-accel help\"\n"); 540 return; 541 } 542 543 static bool check_compatibility(const S390CPUModel *max_model, 544 const S390CPUModel *model, Error **errp) 545 { 546 ERRP_GUARD(); 547 S390FeatBitmap missing; 548 549 if (model->def->gen > max_model->def->gen) { 550 check_compat_model_failed(errp, max_model, "Selected CPU generation is too new"); 551 return false; 552 } else if (model->def->gen == max_model->def->gen && 553 model->def->ec_ga > max_model->def->ec_ga) { 554 check_compat_model_failed(errp, max_model, "Selected CPU GA level is too new"); 555 return false; 556 } 557 558 #ifndef CONFIG_USER_ONLY 559 if (only_migratable && test_bit(S390_FEAT_UNPACK, model->features)) { 560 error_setg(errp, "The unpack facility is not compatible with " 561 "the --only-migratable option. You must remove either " 562 "the 'unpack' facility or the --only-migratable option"); 563 return false; 564 } 565 #endif 566 567 /* detect the missing features to properly report them */ 568 bitmap_andnot(missing, model->features, max_model->features, S390_FEAT_MAX); 569 if (bitmap_empty(missing, S390_FEAT_MAX)) { 570 return true; 571 } 572 573 error_setg(errp, " "); 574 s390_feat_bitmap_to_ascii(missing, errp, error_prepend_missing_feat); 575 error_prepend(errp, "Some features requested in the CPU model are not " 576 "available in the current configuration: "); 577 error_append_hint(errp, 578 "Consider a different accelerator, QEMU, or kernel version\n"); 579 return false; 580 } 581 582 S390CPUModel *get_max_cpu_model(Error **errp) 583 { 584 static S390CPUModel max_model; 585 static bool cached; 586 587 if (cached) { 588 return &max_model; 589 } 590 591 if (kvm_enabled()) { 592 if (!kvm_s390_get_host_cpu_model(&max_model, errp)) { 593 return NULL; 594 } 595 } else { 596 max_model.def = s390_find_cpu_def(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN, 597 QEMU_MAX_CPU_EC_GA, NULL); 598 bitmap_copy(max_model.features, qemu_max_cpu_feat, S390_FEAT_MAX); 599 } 600 cached = true; 601 return &max_model; 602 } 603 604 void s390_realize_cpu_model(CPUState *cs, Error **errp) 605 { 606 ERRP_GUARD(); 607 S390CPUClass *xcc = S390_CPU_GET_CLASS(cs); 608 S390CPU *cpu = S390_CPU(cs); 609 const S390CPUModel *max_model; 610 611 if (xcc->kvm_required && !kvm_enabled()) { 612 error_setg(errp, "CPU definition requires KVM"); 613 return; 614 } 615 616 if (!cpu->model) { 617 /* no host model support -> perform compatibility stuff */ 618 apply_cpu_model(NULL, errp); 619 return; 620 } 621 622 max_model = get_max_cpu_model(errp); 623 if (!max_model) { 624 error_prepend(errp, "CPU models are not available: "); 625 return; 626 } 627 628 /* copy over properties that can vary */ 629 cpu->model->lowest_ibc = max_model->lowest_ibc; 630 cpu->model->cpu_id = max_model->cpu_id; 631 cpu->model->cpu_id_format = max_model->cpu_id_format; 632 cpu->model->cpu_ver = max_model->cpu_ver; 633 634 check_consistency(cpu->model); 635 if (!check_compatibility(max_model, cpu->model, errp)) { 636 return; 637 } 638 639 apply_cpu_model(cpu->model, errp); 640 641 #if !defined(CONFIG_USER_ONLY) 642 cpu->env.cpuid = s390_cpuid_from_cpu_model(cpu->model); 643 if (tcg_enabled()) { 644 cpu->env.cpuid = deposit64(cpu->env.cpuid, CPU_PHYS_ADDR_SHIFT, 645 CPU_PHYS_ADDR_BITS, cpu->env.core_id); 646 } 647 #endif 648 } 649 650 static void get_feature(Object *obj, Visitor *v, const char *name, 651 void *opaque, Error **errp) 652 { 653 S390Feat feat = (S390Feat) (uintptr_t) opaque; 654 S390CPU *cpu = S390_CPU(obj); 655 bool value; 656 657 if (!cpu->model) { 658 error_setg(errp, "Details about the host CPU model are not available, " 659 "features cannot be queried."); 660 return; 661 } 662 663 value = test_bit(feat, cpu->model->features); 664 visit_type_bool(v, name, &value, errp); 665 } 666 667 static void set_feature(Object *obj, Visitor *v, const char *name, 668 void *opaque, Error **errp) 669 { 670 S390Feat feat = (S390Feat) (uintptr_t) opaque; 671 DeviceState *dev = DEVICE(obj); 672 S390CPU *cpu = S390_CPU(obj); 673 bool value; 674 675 if (dev->realized) { 676 error_setg(errp, "Attempt to set property '%s' on '%s' after " 677 "it was realized", name, object_get_typename(obj)); 678 return; 679 } else if (!cpu->model) { 680 error_setg(errp, "Details about the host CPU model are not available, " 681 "features cannot be changed."); 682 return; 683 } 684 685 if (!visit_type_bool(v, name, &value, errp)) { 686 return; 687 } 688 if (value) { 689 if (!test_bit(feat, cpu->model->def->full_feat)) { 690 error_setg(errp, "Feature '%s' is not available for CPU model '%s'," 691 " it was introduced with later models.", 692 name, cpu->model->def->name); 693 return; 694 } 695 set_bit(feat, cpu->model->features); 696 } else { 697 clear_bit(feat, cpu->model->features); 698 } 699 } 700 701 static void get_feature_group(Object *obj, Visitor *v, const char *name, 702 void *opaque, Error **errp) 703 { 704 S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque; 705 const S390FeatGroupDef *def = s390_feat_group_def(group); 706 S390CPU *cpu = S390_CPU(obj); 707 S390FeatBitmap tmp; 708 bool value; 709 710 if (!cpu->model) { 711 error_setg(errp, "Details about the host CPU model are not available, " 712 "features cannot be queried."); 713 return; 714 } 715 716 /* a group is enabled if all features are enabled */ 717 bitmap_and(tmp, cpu->model->features, def->feat, S390_FEAT_MAX); 718 value = bitmap_equal(tmp, def->feat, S390_FEAT_MAX); 719 visit_type_bool(v, name, &value, errp); 720 } 721 722 static void set_feature_group(Object *obj, Visitor *v, const char *name, 723 void *opaque, Error **errp) 724 { 725 S390FeatGroup group = (S390FeatGroup) (uintptr_t) opaque; 726 const S390FeatGroupDef *def = s390_feat_group_def(group); 727 DeviceState *dev = DEVICE(obj); 728 S390CPU *cpu = S390_CPU(obj); 729 bool value; 730 731 if (dev->realized) { 732 error_setg(errp, "Attempt to set property '%s' on '%s' after " 733 "it was realized", name, object_get_typename(obj)); 734 return; 735 } else if (!cpu->model) { 736 error_setg(errp, "Details about the host CPU model are not available, " 737 "features cannot be changed."); 738 return; 739 } 740 741 if (!visit_type_bool(v, name, &value, errp)) { 742 return; 743 } 744 if (value) { 745 /* groups are added in one shot, so an intersect is sufficient */ 746 if (!bitmap_intersects(def->feat, cpu->model->def->full_feat, 747 S390_FEAT_MAX)) { 748 error_setg(errp, "Group '%s' is not available for CPU model '%s'," 749 " it was introduced with later models.", 750 name, cpu->model->def->name); 751 return; 752 } 753 bitmap_or(cpu->model->features, cpu->model->features, def->feat, 754 S390_FEAT_MAX); 755 } else { 756 bitmap_andnot(cpu->model->features, cpu->model->features, def->feat, 757 S390_FEAT_MAX); 758 } 759 } 760 761 static void s390_cpu_model_initfn(Object *obj) 762 { 763 S390CPU *cpu = S390_CPU(obj); 764 S390CPUClass *xcc = S390_CPU_GET_CLASS(cpu); 765 766 cpu->model = g_malloc0(sizeof(*cpu->model)); 767 /* copy the model, so we can modify it */ 768 cpu->model->def = xcc->cpu_def; 769 if (xcc->is_static) { 770 /* base model - features will never change */ 771 bitmap_copy(cpu->model->features, cpu->model->def->base_feat, 772 S390_FEAT_MAX); 773 } else { 774 /* latest model - features can change */ 775 bitmap_copy(cpu->model->features, 776 cpu->model->def->default_feat, S390_FEAT_MAX); 777 } 778 } 779 780 static S390CPUModel s390_qemu_cpu_model; 781 782 /* Set the qemu CPU model (on machine initialization). Must not be called 783 * once CPUs have been created. 784 */ 785 void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga, 786 const S390FeatInit feat_init) 787 { 788 const S390CPUDef *def = s390_find_cpu_def(type, gen, ec_ga, NULL); 789 790 g_assert(def); 791 g_assert(QTAILQ_EMPTY_RCU(&cpus_queue)); 792 793 /* build the CPU model */ 794 s390_qemu_cpu_model.def = def; 795 bitmap_zero(s390_qemu_cpu_model.features, S390_FEAT_MAX); 796 s390_init_feat_bitmap(feat_init, s390_qemu_cpu_model.features); 797 } 798 799 static void s390_qemu_cpu_model_initfn(Object *obj) 800 { 801 S390CPU *cpu = S390_CPU(obj); 802 803 cpu->model = g_malloc0(sizeof(*cpu->model)); 804 /* copy the CPU model so we can modify it */ 805 memcpy(cpu->model, &s390_qemu_cpu_model, sizeof(*cpu->model)); 806 } 807 808 static void s390_max_cpu_model_initfn(Object *obj) 809 { 810 const S390CPUModel *max_model; 811 S390CPU *cpu = S390_CPU(obj); 812 Error *local_err = NULL; 813 814 if (kvm_enabled() && !kvm_s390_cpu_models_supported()) { 815 /* "max" and "host" always work, even without CPU model support */ 816 return; 817 } 818 819 max_model = get_max_cpu_model(&local_err); 820 if (local_err) { 821 /* we expect errors only under KVM, when actually querying the kernel */ 822 g_assert(kvm_enabled()); 823 error_report_err(local_err); 824 /* fallback to unsupported CPU models */ 825 return; 826 } 827 828 cpu->model = g_new(S390CPUModel, 1); 829 /* copy the CPU model so we can modify it */ 830 memcpy(cpu->model, max_model, sizeof(*cpu->model)); 831 } 832 833 static void s390_cpu_model_finalize(Object *obj) 834 { 835 S390CPU *cpu = S390_CPU(obj); 836 837 g_free(cpu->model); 838 cpu->model = NULL; 839 } 840 841 static bool get_is_migration_safe(Object *obj, Error **errp) 842 { 843 return S390_CPU_GET_CLASS(obj)->is_migration_safe; 844 } 845 846 static bool get_is_static(Object *obj, Error **errp) 847 { 848 return S390_CPU_GET_CLASS(obj)->is_static; 849 } 850 851 static char *get_description(Object *obj, Error **errp) 852 { 853 return g_strdup(S390_CPU_GET_CLASS(obj)->desc); 854 } 855 856 void s390_cpu_model_class_register_props(ObjectClass *oc) 857 { 858 S390FeatGroup group; 859 S390Feat feat; 860 861 object_class_property_add_bool(oc, "migration-safe", get_is_migration_safe, 862 NULL); 863 object_class_property_add_bool(oc, "static", get_is_static, 864 NULL); 865 object_class_property_add_str(oc, "description", get_description, NULL); 866 867 for (feat = 0; feat < S390_FEAT_MAX; feat++) { 868 const S390FeatDef *def = s390_feat_def(feat); 869 object_class_property_add(oc, def->name, "bool", get_feature, 870 set_feature, NULL, (void *) feat); 871 object_class_property_set_description(oc, def->name, def->desc); 872 } 873 for (group = 0; group < S390_FEAT_GROUP_MAX; group++) { 874 const S390FeatGroupDef *def = s390_feat_group_def(group); 875 object_class_property_add(oc, def->name, "bool", get_feature_group, 876 set_feature_group, NULL, (void *) group); 877 object_class_property_set_description(oc, def->name, def->desc); 878 } 879 } 880 881 #ifdef CONFIG_KVM 882 static void s390_host_cpu_model_class_init(ObjectClass *oc, void *data) 883 { 884 S390CPUClass *xcc = S390_CPU_CLASS(oc); 885 886 xcc->kvm_required = true; 887 xcc->desc = "KVM only: All recognized features"; 888 } 889 #endif 890 891 static void s390_base_cpu_model_class_init(ObjectClass *oc, void *data) 892 { 893 S390CPUClass *xcc = S390_CPU_CLASS(oc); 894 895 /* all base models are migration safe */ 896 xcc->cpu_def = (const S390CPUDef *) data; 897 xcc->is_migration_safe = true; 898 xcc->is_static = true; 899 xcc->desc = xcc->cpu_def->desc; 900 } 901 902 static void s390_cpu_model_class_init(ObjectClass *oc, void *data) 903 { 904 S390CPUClass *xcc = S390_CPU_CLASS(oc); 905 906 /* model that can change between QEMU versions */ 907 xcc->cpu_def = (const S390CPUDef *) data; 908 xcc->is_migration_safe = true; 909 xcc->desc = xcc->cpu_def->desc; 910 } 911 912 static void s390_qemu_cpu_model_class_init(ObjectClass *oc, void *data) 913 { 914 S390CPUClass *xcc = S390_CPU_CLASS(oc); 915 916 xcc->is_migration_safe = true; 917 xcc->desc = g_strdup_printf("QEMU Virtual CPU version %s", 918 qemu_hw_version()); 919 } 920 921 static void s390_max_cpu_model_class_init(ObjectClass *oc, void *data) 922 { 923 S390CPUClass *xcc = S390_CPU_CLASS(oc); 924 925 /* 926 * The "max" model is neither static nor migration safe. Under KVM 927 * it represents the "host" model. Under TCG it represents the "qemu" CPU 928 * model of the latest QEMU machine. 929 */ 930 xcc->desc = 931 "Enables all features supported by the accelerator in the current host"; 932 } 933 934 /* Generate type name for a cpu model. Caller has to free the string. */ 935 static char *s390_cpu_type_name(const char *model_name) 936 { 937 return g_strdup_printf(S390_CPU_TYPE_NAME("%s"), model_name); 938 } 939 940 /* Generate type name for a base cpu model. Caller has to free the string. */ 941 static char *s390_base_cpu_type_name(const char *model_name) 942 { 943 return g_strdup_printf(S390_CPU_TYPE_NAME("%s-base"), model_name); 944 } 945 946 ObjectClass *s390_cpu_class_by_name(const char *name) 947 { 948 char *typename = s390_cpu_type_name(name); 949 ObjectClass *oc; 950 951 oc = object_class_by_name(typename); 952 g_free(typename); 953 return oc; 954 } 955 956 static const TypeInfo qemu_s390_cpu_type_info = { 957 .name = S390_CPU_TYPE_NAME("qemu"), 958 .parent = TYPE_S390_CPU, 959 .instance_init = s390_qemu_cpu_model_initfn, 960 .instance_finalize = s390_cpu_model_finalize, 961 .class_init = s390_qemu_cpu_model_class_init, 962 }; 963 964 static const TypeInfo max_s390_cpu_type_info = { 965 .name = S390_CPU_TYPE_NAME("max"), 966 .parent = TYPE_S390_CPU, 967 .instance_init = s390_max_cpu_model_initfn, 968 .instance_finalize = s390_cpu_model_finalize, 969 .class_init = s390_max_cpu_model_class_init, 970 }; 971 972 #ifdef CONFIG_KVM 973 static const TypeInfo host_s390_cpu_type_info = { 974 .name = S390_CPU_TYPE_NAME("host"), 975 .parent = S390_CPU_TYPE_NAME("max"), 976 .class_init = s390_host_cpu_model_class_init, 977 }; 978 #endif 979 980 static void init_ignored_base_feat(void) 981 { 982 static const int feats[] = { 983 /* MSA subfunctions that could not be available on certain machines */ 984 S390_FEAT_KMAC_DEA, 985 S390_FEAT_KMAC_TDEA_128, 986 S390_FEAT_KMAC_TDEA_192, 987 S390_FEAT_KMC_DEA, 988 S390_FEAT_KMC_TDEA_128, 989 S390_FEAT_KMC_TDEA_192, 990 S390_FEAT_KM_DEA, 991 S390_FEAT_KM_TDEA_128, 992 S390_FEAT_KM_TDEA_192, 993 S390_FEAT_KIMD_SHA_1, 994 S390_FEAT_KLMD_SHA_1, 995 /* CSSKE is deprecated on newer generations */ 996 S390_FEAT_CONDITIONAL_SSKE, 997 }; 998 int i; 999 1000 for (i = 0; i < ARRAY_SIZE(feats); i++) { 1001 set_bit(feats[i], ignored_base_feat); 1002 } 1003 } 1004 1005 static void register_types(void) 1006 { 1007 static const S390FeatInit qemu_max_init = { S390_FEAT_LIST_QEMU_MAX }; 1008 int i; 1009 1010 init_ignored_base_feat(); 1011 1012 /* init all bitmaps from generated data initially */ 1013 s390_init_feat_bitmap(qemu_max_init, qemu_max_cpu_feat); 1014 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 1015 s390_init_feat_bitmap(s390_cpu_defs[i].base_init, 1016 s390_cpu_defs[i].base_feat); 1017 s390_init_feat_bitmap(s390_cpu_defs[i].default_init, 1018 s390_cpu_defs[i].default_feat); 1019 s390_init_feat_bitmap(s390_cpu_defs[i].full_init, 1020 s390_cpu_defs[i].full_feat); 1021 } 1022 1023 /* initialize the qemu model with the maximum definition ("max" model) */ 1024 s390_set_qemu_cpu_model(QEMU_MAX_CPU_TYPE, QEMU_MAX_CPU_GEN, 1025 QEMU_MAX_CPU_EC_GA, qemu_max_init); 1026 1027 for (i = 0; i < ARRAY_SIZE(s390_cpu_defs); i++) { 1028 char *base_name = s390_base_cpu_type_name(s390_cpu_defs[i].name); 1029 TypeInfo ti_base = { 1030 .name = base_name, 1031 .parent = TYPE_S390_CPU, 1032 .instance_init = s390_cpu_model_initfn, 1033 .instance_finalize = s390_cpu_model_finalize, 1034 .class_init = s390_base_cpu_model_class_init, 1035 .class_data = (void *) &s390_cpu_defs[i], 1036 }; 1037 char *name = s390_cpu_type_name(s390_cpu_defs[i].name); 1038 TypeInfo ti = { 1039 .name = name, 1040 .parent = TYPE_S390_CPU, 1041 .instance_init = s390_cpu_model_initfn, 1042 .instance_finalize = s390_cpu_model_finalize, 1043 .class_init = s390_cpu_model_class_init, 1044 .class_data = (void *) &s390_cpu_defs[i], 1045 }; 1046 1047 type_register_static(&ti_base); 1048 type_register_static(&ti); 1049 g_free(base_name); 1050 g_free(name); 1051 } 1052 1053 type_register_static(&qemu_s390_cpu_type_info); 1054 type_register_static(&max_s390_cpu_type_info); 1055 #ifdef CONFIG_KVM 1056 type_register_static(&host_s390_cpu_type_info); 1057 #endif 1058 } 1059 1060 type_init(register_types) 1061