1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2008 4 * Author(s): Martin Schwidefsky ([email protected]) 5 */ 6 7 #define KMSG_COMPONENT "cpu" 8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 9 10 #include <linux/stop_machine.h> 11 #include <linux/cpufeature.h> 12 #include <linux/bitops.h> 13 #include <linux/kernel.h> 14 #include <linux/random.h> 15 #include <linux/sched/mm.h> 16 #include <linux/init.h> 17 #include <linux/seq_file.h> 18 #include <linux/mm_types.h> 19 #include <linux/delay.h> 20 #include <linux/cpu.h> 21 22 #include <asm/diag.h> 23 #include <asm/facility.h> 24 #include <asm/elf.h> 25 #include <asm/lowcore.h> 26 #include <asm/param.h> 27 #include <asm/sclp.h> 28 #include <asm/smp.h> 29 30 unsigned long __read_mostly elf_hwcap; 31 char elf_platform[ELF_PLATFORM_SIZE]; 32 33 unsigned long int_hwcap; 34 35 struct cpu_info { 36 unsigned int cpu_mhz_dynamic; 37 unsigned int cpu_mhz_static; 38 struct cpuid cpu_id; 39 }; 40 41 static DEFINE_PER_CPU(struct cpu_info, cpu_info); 42 static DEFINE_PER_CPU(int, cpu_relax_retry); 43 44 static bool machine_has_cpu_mhz; 45 46 void __init cpu_detect_mhz_feature(void) 47 { 48 if (test_facility(34) && __ecag(ECAG_CPU_ATTRIBUTE, 0) != -1UL) 49 machine_has_cpu_mhz = true; 50 } 51 52 static void update_cpu_mhz(void *arg) 53 { 54 unsigned long mhz; 55 struct cpu_info *c; 56 57 mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0); 58 c = this_cpu_ptr(&cpu_info); 59 c->cpu_mhz_dynamic = mhz >> 32; 60 c->cpu_mhz_static = mhz & 0xffffffff; 61 } 62 63 void s390_update_cpu_mhz(void) 64 { 65 s390_adjust_jiffies(); 66 if (machine_has_cpu_mhz) 67 on_each_cpu(update_cpu_mhz, NULL, 0); 68 } 69 70 void notrace stop_machine_yield(const struct cpumask *cpumask) 71 { 72 int cpu, this_cpu; 73 74 this_cpu = smp_processor_id(); 75 if (__this_cpu_inc_return(cpu_relax_retry) >= spin_retry) { 76 __this_cpu_write(cpu_relax_retry, 0); 77 cpu = cpumask_next_wrap(this_cpu, cpumask, this_cpu, false); 78 if (cpu >= nr_cpu_ids) 79 return; 80 if (arch_vcpu_is_preempted(cpu)) 81 smp_yield_cpu(cpu); 82 } 83 } 84 85 /* 86 * cpu_init - initializes state that is per-CPU. 87 */ 88 void cpu_init(void) 89 { 90 struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id); 91 92 get_cpu_id(id); 93 if (machine_has_cpu_mhz) 94 update_cpu_mhz(NULL); 95 mmgrab(&init_mm); 96 current->active_mm = &init_mm; 97 BUG_ON(current->mm); 98 enter_lazy_tlb(&init_mm, current); 99 } 100 101 /* 102 * cpu_have_feature - Test CPU features on module initialization 103 */ 104 int cpu_have_feature(unsigned int num) 105 { 106 return elf_hwcap & (1UL << num); 107 } 108 EXPORT_SYMBOL(cpu_have_feature); 109 110 static void show_facilities(struct seq_file *m) 111 { 112 unsigned int bit; 113 114 seq_puts(m, "facilities :"); 115 for_each_set_bit_inv(bit, (long *)&stfle_fac_list, MAX_FACILITY_BIT) 116 seq_printf(m, " %d", bit); 117 seq_putc(m, '\n'); 118 } 119 120 static void show_cpu_summary(struct seq_file *m, void *v) 121 { 122 static const char *hwcap_str[] = { 123 [HWCAP_NR_ESAN3] = "esan3", 124 [HWCAP_NR_ZARCH] = "zarch", 125 [HWCAP_NR_STFLE] = "stfle", 126 [HWCAP_NR_MSA] = "msa", 127 [HWCAP_NR_LDISP] = "ldisp", 128 [HWCAP_NR_EIMM] = "eimm", 129 [HWCAP_NR_DFP] = "dfp", 130 [HWCAP_NR_HPAGE] = "edat", 131 [HWCAP_NR_ETF3EH] = "etf3eh", 132 [HWCAP_NR_HIGH_GPRS] = "highgprs", 133 [HWCAP_NR_TE] = "te", 134 [HWCAP_NR_VXRS] = "vx", 135 [HWCAP_NR_VXRS_BCD] = "vxd", 136 [HWCAP_NR_VXRS_EXT] = "vxe", 137 [HWCAP_NR_GS] = "gs", 138 [HWCAP_NR_VXRS_EXT2] = "vxe2", 139 [HWCAP_NR_VXRS_PDE] = "vxp", 140 [HWCAP_NR_SORT] = "sort", 141 [HWCAP_NR_DFLT] = "dflt", 142 [HWCAP_NR_VXRS_PDE2] = "vxp2", 143 [HWCAP_NR_NNPA] = "nnpa", 144 [HWCAP_NR_PCI_MIO] = "pcimio", 145 }; 146 static const char * const int_hwcap_str[] = { 147 [HWCAP_INT_NR_SIE] = "sie", 148 }; 149 int i, cpu; 150 151 BUILD_BUG_ON(ARRAY_SIZE(hwcap_str) != HWCAP_NR_MAX); 152 BUILD_BUG_ON(ARRAY_SIZE(int_hwcap_str) != HWCAP_INT_NR_MAX); 153 seq_printf(m, "vendor_id : IBM/S390\n" 154 "# processors : %i\n" 155 "bogomips per cpu: %lu.%02lu\n", 156 num_online_cpus(), loops_per_jiffy/(500000/HZ), 157 (loops_per_jiffy/(5000/HZ))%100); 158 seq_printf(m, "max thread id : %d\n", smp_cpu_mtid); 159 seq_puts(m, "features\t: "); 160 for (i = 0; i < ARRAY_SIZE(hwcap_str); i++) 161 if (hwcap_str[i] && (elf_hwcap & (1UL << i))) 162 seq_printf(m, "%s ", hwcap_str[i]); 163 for (i = 0; i < ARRAY_SIZE(int_hwcap_str); i++) 164 if (int_hwcap_str[i] && (int_hwcap & (1UL << i))) 165 seq_printf(m, "%s ", int_hwcap_str[i]); 166 seq_puts(m, "\n"); 167 show_facilities(m); 168 show_cacheinfo(m); 169 for_each_online_cpu(cpu) { 170 struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu); 171 172 seq_printf(m, "processor %d: " 173 "version = %02X, " 174 "identification = %06X, " 175 "machine = %04X\n", 176 cpu, id->version, id->ident, id->machine); 177 } 178 } 179 180 static int __init setup_hwcaps(void) 181 { 182 /* instructions named N3, "backported" to esa-mode */ 183 if (test_facility(0)) 184 elf_hwcap |= HWCAP_ESAN3; 185 186 /* z/Architecture mode active */ 187 elf_hwcap |= HWCAP_ZARCH; 188 189 /* store-facility-list-extended */ 190 if (test_facility(7)) 191 elf_hwcap |= HWCAP_STFLE; 192 193 /* message-security assist */ 194 if (test_facility(17)) 195 elf_hwcap |= HWCAP_MSA; 196 197 /* long-displacement */ 198 if (test_facility(19)) 199 elf_hwcap |= HWCAP_LDISP; 200 201 /* extended-immediate */ 202 if (test_facility(21)) 203 elf_hwcap |= HWCAP_EIMM; 204 205 /* extended-translation facility 3 enhancement */ 206 if (test_facility(22) && test_facility(30)) 207 elf_hwcap |= HWCAP_ETF3EH; 208 209 /* decimal floating point & perform floating point operation */ 210 if ((elf_hwcap & (1UL << 2)) && test_facility(42) && test_facility(44)) 211 elf_hwcap |= HWCAP_DFP; 212 213 /* huge page support */ 214 if (MACHINE_HAS_EDAT1) 215 elf_hwcap |= HWCAP_HPAGE; 216 217 /* 64-bit register support for 31-bit processes */ 218 elf_hwcap |= HWCAP_HIGH_GPRS; 219 220 /* transactional execution */ 221 if (MACHINE_HAS_TE) 222 elf_hwcap |= HWCAP_TE; 223 224 /* 225 * Vector extension can be disabled with the "novx" parameter. 226 * Use MACHINE_HAS_VX instead of facility bit 129. 227 */ 228 if (MACHINE_HAS_VX) { 229 elf_hwcap |= HWCAP_VXRS; 230 if (test_facility(134)) 231 elf_hwcap |= HWCAP_VXRS_BCD; 232 if (test_facility(135)) 233 elf_hwcap |= HWCAP_VXRS_EXT; 234 if (test_facility(148)) 235 elf_hwcap |= HWCAP_VXRS_EXT2; 236 if (test_facility(152)) 237 elf_hwcap |= HWCAP_VXRS_PDE; 238 if (test_facility(192)) 239 elf_hwcap |= HWCAP_VXRS_PDE2; 240 } 241 242 if (test_facility(150)) 243 elf_hwcap |= HWCAP_SORT; 244 245 if (test_facility(151)) 246 elf_hwcap |= HWCAP_DFLT; 247 248 if (test_facility(165)) 249 elf_hwcap |= HWCAP_NNPA; 250 251 /* guarded storage */ 252 if (MACHINE_HAS_GS) 253 elf_hwcap |= HWCAP_GS; 254 255 if (MACHINE_HAS_PCI_MIO) 256 elf_hwcap |= HWCAP_PCI_MIO; 257 258 /* virtualization support */ 259 if (sclp.has_sief2) 260 int_hwcap |= HWCAP_INT_SIE; 261 262 return 0; 263 } 264 arch_initcall(setup_hwcaps); 265 266 static int __init setup_elf_platform(void) 267 { 268 struct cpuid cpu_id; 269 270 get_cpu_id(&cpu_id); 271 add_device_randomness(&cpu_id, sizeof(cpu_id)); 272 switch (cpu_id.machine) { 273 case 0x2064: 274 case 0x2066: 275 default: /* Use "z900" as default for 64 bit kernels. */ 276 strcpy(elf_platform, "z900"); 277 break; 278 case 0x2084: 279 case 0x2086: 280 strcpy(elf_platform, "z990"); 281 break; 282 case 0x2094: 283 case 0x2096: 284 strcpy(elf_platform, "z9-109"); 285 break; 286 case 0x2097: 287 case 0x2098: 288 strcpy(elf_platform, "z10"); 289 break; 290 case 0x2817: 291 case 0x2818: 292 strcpy(elf_platform, "z196"); 293 break; 294 case 0x2827: 295 case 0x2828: 296 strcpy(elf_platform, "zEC12"); 297 break; 298 case 0x2964: 299 case 0x2965: 300 strcpy(elf_platform, "z13"); 301 break; 302 case 0x3906: 303 case 0x3907: 304 strcpy(elf_platform, "z14"); 305 break; 306 case 0x8561: 307 case 0x8562: 308 strcpy(elf_platform, "z15"); 309 break; 310 } 311 return 0; 312 } 313 arch_initcall(setup_elf_platform); 314 315 static void show_cpu_topology(struct seq_file *m, unsigned long n) 316 { 317 #ifdef CONFIG_SCHED_TOPOLOGY 318 seq_printf(m, "physical id : %d\n", topology_physical_package_id(n)); 319 seq_printf(m, "core id : %d\n", topology_core_id(n)); 320 seq_printf(m, "book id : %d\n", topology_book_id(n)); 321 seq_printf(m, "drawer id : %d\n", topology_drawer_id(n)); 322 seq_printf(m, "dedicated : %d\n", topology_cpu_dedicated(n)); 323 seq_printf(m, "address : %d\n", smp_cpu_get_cpu_address(n)); 324 seq_printf(m, "siblings : %d\n", cpumask_weight(topology_core_cpumask(n))); 325 seq_printf(m, "cpu cores : %d\n", topology_booted_cores(n)); 326 #endif /* CONFIG_SCHED_TOPOLOGY */ 327 } 328 329 static void show_cpu_ids(struct seq_file *m, unsigned long n) 330 { 331 struct cpuid *id = &per_cpu(cpu_info.cpu_id, n); 332 333 seq_printf(m, "version : %02X\n", id->version); 334 seq_printf(m, "identification : %06X\n", id->ident); 335 seq_printf(m, "machine : %04X\n", id->machine); 336 } 337 338 static void show_cpu_mhz(struct seq_file *m, unsigned long n) 339 { 340 struct cpu_info *c = per_cpu_ptr(&cpu_info, n); 341 342 if (!machine_has_cpu_mhz) 343 return; 344 seq_printf(m, "cpu MHz dynamic : %d\n", c->cpu_mhz_dynamic); 345 seq_printf(m, "cpu MHz static : %d\n", c->cpu_mhz_static); 346 } 347 348 /* 349 * show_cpuinfo - Get information on one CPU for use by procfs. 350 */ 351 static int show_cpuinfo(struct seq_file *m, void *v) 352 { 353 unsigned long n = (unsigned long) v - 1; 354 unsigned long first = cpumask_first(cpu_online_mask); 355 356 if (n == first) 357 show_cpu_summary(m, v); 358 seq_printf(m, "\ncpu number : %ld\n", n); 359 show_cpu_topology(m, n); 360 show_cpu_ids(m, n); 361 show_cpu_mhz(m, n); 362 return 0; 363 } 364 365 static inline void *c_update(loff_t *pos) 366 { 367 if (*pos) 368 *pos = cpumask_next(*pos - 1, cpu_online_mask); 369 else 370 *pos = cpumask_first(cpu_online_mask); 371 return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL; 372 } 373 374 static void *c_start(struct seq_file *m, loff_t *pos) 375 { 376 get_online_cpus(); 377 return c_update(pos); 378 } 379 380 static void *c_next(struct seq_file *m, void *v, loff_t *pos) 381 { 382 ++*pos; 383 return c_update(pos); 384 } 385 386 static void c_stop(struct seq_file *m, void *v) 387 { 388 put_online_cpus(); 389 } 390 391 const struct seq_operations cpuinfo_op = { 392 .start = c_start, 393 .next = c_next, 394 .stop = c_stop, 395 .show = show_cpuinfo, 396 }; 397 398 int s390_isolate_bp(void) 399 { 400 if (!test_facility(82)) 401 return -EOPNOTSUPP; 402 set_thread_flag(TIF_ISOLATE_BP); 403 return 0; 404 } 405 EXPORT_SYMBOL(s390_isolate_bp); 406 407 int s390_isolate_bp_guest(void) 408 { 409 if (!test_facility(82)) 410 return -EOPNOTSUPP; 411 set_thread_flag(TIF_ISOLATE_BP_GUEST); 412 return 0; 413 } 414 EXPORT_SYMBOL(s390_isolate_bp_guest); 415