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>
21 #include <linux/smp.h>
22 #include <asm/text-patching.h>
23 #include <asm/machine.h>
25 #include <asm/facility.h>
27 #include <asm/lowcore.h>
28 #include <asm/param.h>
32 unsigned long __read_mostly elf_hwcap;
33 char elf_platform[ELF_PLATFORM_SIZE];
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);
78 if (cpu >= nr_cpu_ids)
80 if (arch_vcpu_is_preempted(cpu))
85 static void do_sync_core(void *info)
90 void text_poke_sync(void)
92 on_each_cpu(do_sync_core, NULL, 1);
95 void text_poke_sync_lock(void)
103 * cpu_init - initializes state that is per-CPU.
107 struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id);
110 if (machine_has_cpu_mhz)
111 update_cpu_mhz(NULL);
113 current->active_mm = &init_mm;
115 enter_lazy_tlb(&init_mm, current);
118 static void show_facilities(struct seq_file *m)
122 seq_puts(m, "facilities :");
123 for_each_set_bit_inv(bit, (long *)&stfle_fac_list, MAX_FACILITY_BIT)
124 seq_printf(m, " %d", bit);
128 static void show_cpu_summary(struct seq_file *m, void *v)
130 static const char *hwcap_str[] = {
131 [HWCAP_NR_ESAN3] = "esan3",
132 [HWCAP_NR_ZARCH] = "zarch",
133 [HWCAP_NR_STFLE] = "stfle",
134 [HWCAP_NR_MSA] = "msa",
135 [HWCAP_NR_LDISP] = "ldisp",
136 [HWCAP_NR_EIMM] = "eimm",
137 [HWCAP_NR_DFP] = "dfp",
138 [HWCAP_NR_HPAGE] = "edat",
139 [HWCAP_NR_ETF3EH] = "etf3eh",
140 [HWCAP_NR_HIGH_GPRS] = "highgprs",
141 [HWCAP_NR_TE] = "te",
142 [HWCAP_NR_VXRS] = "vx",
143 [HWCAP_NR_VXRS_BCD] = "vxd",
144 [HWCAP_NR_VXRS_EXT] = "vxe",
145 [HWCAP_NR_GS] = "gs",
146 [HWCAP_NR_VXRS_EXT2] = "vxe2",
147 [HWCAP_NR_VXRS_PDE] = "vxp",
148 [HWCAP_NR_SORT] = "sort",
149 [HWCAP_NR_DFLT] = "dflt",
150 [HWCAP_NR_VXRS_PDE2] = "vxp2",
151 [HWCAP_NR_NNPA] = "nnpa",
152 [HWCAP_NR_PCI_MIO] = "pcimio",
153 [HWCAP_NR_SIE] = "sie",
157 BUILD_BUG_ON(ARRAY_SIZE(hwcap_str) != HWCAP_NR_MAX);
158 seq_printf(m, "vendor_id : IBM/S390\n"
159 "# processors : %i\n"
160 "bogomips per cpu: %lu.%02lu\n",
161 num_online_cpus(), loops_per_jiffy/(500000/HZ),
162 (loops_per_jiffy/(5000/HZ))%100);
163 seq_printf(m, "max thread id : %d\n", smp_cpu_mtid);
164 seq_puts(m, "features\t: ");
165 for (i = 0; i < ARRAY_SIZE(hwcap_str); i++)
166 if (hwcap_str[i] && (elf_hwcap & (1UL << i)))
167 seq_printf(m, "%s ", hwcap_str[i]);
171 for_each_online_cpu(cpu) {
172 struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu);
174 seq_printf(m, "processor %d: "
176 "identification = %06X, "
178 cpu, id->version, id->ident, id->machine);
182 static int __init setup_hwcaps(void)
184 /* instructions named N3, "backported" to esa-mode */
185 elf_hwcap |= HWCAP_ESAN3;
187 /* z/Architecture mode active */
188 elf_hwcap |= HWCAP_ZARCH;
190 /* store-facility-list-extended */
191 if (test_facility(7))
192 elf_hwcap |= HWCAP_STFLE;
194 /* message-security assist */
195 if (test_facility(17))
196 elf_hwcap |= HWCAP_MSA;
198 /* long-displacement */
199 if (test_facility(19))
200 elf_hwcap |= HWCAP_LDISP;
202 /* extended-immediate */
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 */
215 elf_hwcap |= HWCAP_HPAGE;
217 /* 64-bit register support for 31-bit processes */
218 elf_hwcap |= HWCAP_HIGH_GPRS;
220 /* transactional execution */
221 if (machine_has_tx())
222 elf_hwcap |= HWCAP_TE;
225 if (test_facility(129)) {
226 elf_hwcap |= HWCAP_VXRS;
227 if (test_facility(134))
228 elf_hwcap |= HWCAP_VXRS_BCD;
229 if (test_facility(135))
230 elf_hwcap |= HWCAP_VXRS_EXT;
231 if (test_facility(148))
232 elf_hwcap |= HWCAP_VXRS_EXT2;
233 if (test_facility(152))
234 elf_hwcap |= HWCAP_VXRS_PDE;
235 if (test_facility(192))
236 elf_hwcap |= HWCAP_VXRS_PDE2;
239 if (test_facility(150))
240 elf_hwcap |= HWCAP_SORT;
242 if (test_facility(151))
243 elf_hwcap |= HWCAP_DFLT;
245 if (test_facility(165))
246 elf_hwcap |= HWCAP_NNPA;
248 /* guarded storage */
250 elf_hwcap |= HWCAP_GS;
252 if (test_machine_feature(MFEATURE_PCI_MIO))
253 elf_hwcap |= HWCAP_PCI_MIO;
255 /* virtualization support */
257 elf_hwcap |= HWCAP_SIE;
261 arch_initcall(setup_hwcaps);
263 static int __init setup_elf_platform(void)
268 add_device_randomness(&cpu_id, sizeof(cpu_id));
269 switch (cpu_id.machine) {
270 default: /* Use "z10" as default. */
271 strscpy(elf_platform, "z10");
275 strscpy(elf_platform, "z196");
279 strscpy(elf_platform, "zEC12");
283 strscpy(elf_platform, "z13");
287 strscpy(elf_platform, "z14");
291 strscpy(elf_platform, "z15");
295 strscpy(elf_platform, "z16");
299 strscpy(elf_platform, "z17");
304 arch_initcall(setup_elf_platform);
306 static void show_cpu_topology(struct seq_file *m, unsigned long n)
308 #ifdef CONFIG_SCHED_TOPOLOGY
309 seq_printf(m, "physical id : %d\n", topology_physical_package_id(n));
310 seq_printf(m, "core id : %d\n", topology_core_id(n));
311 seq_printf(m, "book id : %d\n", topology_book_id(n));
312 seq_printf(m, "drawer id : %d\n", topology_drawer_id(n));
313 seq_printf(m, "dedicated : %d\n", topology_cpu_dedicated(n));
314 seq_printf(m, "address : %d\n", smp_cpu_get_cpu_address(n));
315 seq_printf(m, "siblings : %d\n", cpumask_weight(topology_core_cpumask(n)));
316 seq_printf(m, "cpu cores : %d\n", topology_booted_cores(n));
317 #endif /* CONFIG_SCHED_TOPOLOGY */
320 static void show_cpu_ids(struct seq_file *m, unsigned long n)
322 struct cpuid *id = &per_cpu(cpu_info.cpu_id, n);
324 seq_printf(m, "version : %02X\n", id->version);
325 seq_printf(m, "identification : %06X\n", id->ident);
326 seq_printf(m, "machine : %04X\n", id->machine);
329 static void show_cpu_mhz(struct seq_file *m, unsigned long n)
331 struct cpu_info *c = per_cpu_ptr(&cpu_info, n);
333 if (!machine_has_cpu_mhz)
335 seq_printf(m, "cpu MHz dynamic : %d\n", c->cpu_mhz_dynamic);
336 seq_printf(m, "cpu MHz static : %d\n", c->cpu_mhz_static);
340 * show_cpuinfo - Get information on one CPU for use by procfs.
342 static int show_cpuinfo(struct seq_file *m, void *v)
344 unsigned long n = (unsigned long) v - 1;
345 unsigned long first = cpumask_first(cpu_online_mask);
348 show_cpu_summary(m, v);
349 seq_printf(m, "\ncpu number : %ld\n", n);
350 show_cpu_topology(m, n);
356 static inline void *c_update(loff_t *pos)
359 *pos = cpumask_next(*pos - 1, cpu_online_mask);
361 *pos = cpumask_first(cpu_online_mask);
362 return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL;
365 static void *c_start(struct seq_file *m, loff_t *pos)
368 return c_update(pos);
371 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
374 return c_update(pos);
377 static void c_stop(struct seq_file *m, void *v)
382 const struct seq_operations cpuinfo_op = {
386 .show = show_cpuinfo,