1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2008
4 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 #define KMSG_COMPONENT "cpu"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
23 #include <asm/facility.h>
25 #include <asm/lowcore.h>
26 #include <asm/param.h>
30 unsigned long __read_mostly elf_hwcap;
31 char elf_platform[ELF_PLATFORM_SIZE];
33 unsigned long int_hwcap;
36 unsigned int cpu_mhz_dynamic;
37 unsigned int cpu_mhz_static;
41 static DEFINE_PER_CPU(struct cpu_info, cpu_info);
42 static DEFINE_PER_CPU(int, cpu_relax_retry);
44 static bool machine_has_cpu_mhz;
46 void __init cpu_detect_mhz_feature(void)
48 if (test_facility(34) && __ecag(ECAG_CPU_ATTRIBUTE, 0) != -1UL)
49 machine_has_cpu_mhz = true;
52 static void update_cpu_mhz(void *arg)
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;
63 void s390_update_cpu_mhz(void)
65 s390_adjust_jiffies();
66 if (machine_has_cpu_mhz)
67 on_each_cpu(update_cpu_mhz, NULL, 0);
70 void notrace stop_machine_yield(const struct cpumask *cpumask)
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)
80 if (arch_vcpu_is_preempted(cpu))
86 * cpu_init - initializes state that is per-CPU.
90 struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id);
93 if (machine_has_cpu_mhz)
96 current->active_mm = &init_mm;
98 enter_lazy_tlb(&init_mm, current);
102 * cpu_have_feature - Test CPU features on module initialization
104 int cpu_have_feature(unsigned int num)
106 return elf_hwcap & (1UL << num);
108 EXPORT_SYMBOL(cpu_have_feature);
110 static void show_facilities(struct seq_file *m)
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);
120 static void show_cpu_summary(struct seq_file *m, void *v)
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",
146 static const char * const int_hwcap_str[] = {
147 [HWCAP_INT_NR_SIE] = "sie",
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]);
169 for_each_online_cpu(cpu) {
170 struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu);
172 seq_printf(m, "processor %d: "
174 "identification = %06X, "
176 cpu, id->version, id->ident, id->machine);
180 static int __init setup_hwcaps(void)
182 /* instructions named N3, "backported" to esa-mode */
183 if (test_facility(0))
184 elf_hwcap |= HWCAP_ESAN3;
186 /* z/Architecture mode active */
187 elf_hwcap |= HWCAP_ZARCH;
189 /* store-facility-list-extended */
190 if (test_facility(7))
191 elf_hwcap |= HWCAP_STFLE;
193 /* message-security assist */
194 if (test_facility(17))
195 elf_hwcap |= HWCAP_MSA;
197 /* long-displacement */
198 if (test_facility(19))
199 elf_hwcap |= HWCAP_LDISP;
201 /* extended-immediate */
202 if (test_facility(21))
203 elf_hwcap |= HWCAP_EIMM;
205 /* extended-translation facility 3 enhancement */
206 if (test_facility(22) && test_facility(30))
207 elf_hwcap |= HWCAP_ETF3EH;
209 /* decimal floating point & perform floating point operation */
210 if (test_facility(42) && test_facility(44))
211 elf_hwcap |= HWCAP_DFP;
213 /* huge page support */
214 if (MACHINE_HAS_EDAT1)
215 elf_hwcap |= HWCAP_HPAGE;
217 /* 64-bit register support for 31-bit processes */
218 elf_hwcap |= HWCAP_HIGH_GPRS;
220 /* transactional execution */
222 elf_hwcap |= HWCAP_TE;
225 * Vector extension can be disabled with the "novx" parameter.
226 * Use MACHINE_HAS_VX instead of facility bit 129.
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;
242 if (test_facility(150))
243 elf_hwcap |= HWCAP_SORT;
245 if (test_facility(151))
246 elf_hwcap |= HWCAP_DFLT;
248 if (test_facility(165))
249 elf_hwcap |= HWCAP_NNPA;
251 /* guarded storage */
253 elf_hwcap |= HWCAP_GS;
255 if (MACHINE_HAS_PCI_MIO)
256 elf_hwcap |= HWCAP_PCI_MIO;
258 /* virtualization support */
260 int_hwcap |= HWCAP_INT_SIE;
264 arch_initcall(setup_hwcaps);
266 static int __init setup_elf_platform(void)
271 add_device_randomness(&cpu_id, sizeof(cpu_id));
272 switch (cpu_id.machine) {
275 default: /* Use "z900" as default for 64 bit kernels. */
276 strcpy(elf_platform, "z900");
280 strcpy(elf_platform, "z990");
284 strcpy(elf_platform, "z9-109");
288 strcpy(elf_platform, "z10");
292 strcpy(elf_platform, "z196");
296 strcpy(elf_platform, "zEC12");
300 strcpy(elf_platform, "z13");
304 strcpy(elf_platform, "z14");
308 strcpy(elf_platform, "z15");
313 arch_initcall(setup_elf_platform);
315 static void show_cpu_topology(struct seq_file *m, unsigned long n)
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 */
329 static void show_cpu_ids(struct seq_file *m, unsigned long n)
331 struct cpuid *id = &per_cpu(cpu_info.cpu_id, n);
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);
338 static void show_cpu_mhz(struct seq_file *m, unsigned long n)
340 struct cpu_info *c = per_cpu_ptr(&cpu_info, n);
342 if (!machine_has_cpu_mhz)
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);
349 * show_cpuinfo - Get information on one CPU for use by procfs.
351 static int show_cpuinfo(struct seq_file *m, void *v)
353 unsigned long n = (unsigned long) v - 1;
354 unsigned long first = cpumask_first(cpu_online_mask);
357 show_cpu_summary(m, v);
358 seq_printf(m, "\ncpu number : %ld\n", n);
359 show_cpu_topology(m, n);
365 static inline void *c_update(loff_t *pos)
368 *pos = cpumask_next(*pos - 1, cpu_online_mask);
370 *pos = cpumask_first(cpu_online_mask);
371 return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL;
374 static void *c_start(struct seq_file *m, loff_t *pos)
377 return c_update(pos);
380 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
383 return c_update(pos);
386 static void c_stop(struct seq_file *m, void *v)
391 const struct seq_operations cpuinfo_op = {
395 .show = show_cpuinfo,
398 int s390_isolate_bp(void)
400 if (!test_facility(82))
402 set_thread_flag(TIF_ISOLATE_BP);
405 EXPORT_SYMBOL(s390_isolate_bp);
407 int s390_isolate_bp_guest(void)
409 if (!test_facility(82))
411 set_thread_flag(TIF_ISOLATE_BP_GUEST);
414 EXPORT_SYMBOL(s390_isolate_bp_guest);