1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2008
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];
34 unsigned int cpu_mhz_dynamic;
35 unsigned int cpu_mhz_static;
39 static DEFINE_PER_CPU(struct cpu_info, cpu_info);
40 static DEFINE_PER_CPU(int, cpu_relax_retry);
42 static bool machine_has_cpu_mhz;
44 void __init cpu_detect_mhz_feature(void)
46 if (test_facility(34) && __ecag(ECAG_CPU_ATTRIBUTE, 0) != -1UL)
47 machine_has_cpu_mhz = true;
50 static void update_cpu_mhz(void *arg)
55 mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
56 c = this_cpu_ptr(&cpu_info);
57 c->cpu_mhz_dynamic = mhz >> 32;
58 c->cpu_mhz_static = mhz & 0xffffffff;
61 void s390_update_cpu_mhz(void)
63 s390_adjust_jiffies();
64 if (machine_has_cpu_mhz)
65 on_each_cpu(update_cpu_mhz, NULL, 0);
68 void notrace stop_machine_yield(const struct cpumask *cpumask)
72 this_cpu = smp_processor_id();
73 if (__this_cpu_inc_return(cpu_relax_retry) >= spin_retry) {
74 __this_cpu_write(cpu_relax_retry, 0);
75 cpu = cpumask_next_wrap(this_cpu, cpumask, this_cpu, false);
76 if (cpu >= nr_cpu_ids)
78 if (arch_vcpu_is_preempted(cpu))
84 * cpu_init - initializes state that is per-CPU.
88 struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id);
91 if (machine_has_cpu_mhz)
94 current->active_mm = &init_mm;
96 enter_lazy_tlb(&init_mm, current);
100 * cpu_have_feature - Test CPU features on module initialization
102 int cpu_have_feature(unsigned int num)
104 return elf_hwcap & (1UL << num);
106 EXPORT_SYMBOL(cpu_have_feature);
108 static void show_facilities(struct seq_file *m)
112 seq_puts(m, "facilities :");
113 for_each_set_bit_inv(bit, (long *)&stfle_fac_list, MAX_FACILITY_BIT)
114 seq_printf(m, " %d", bit);
118 static void show_cpu_summary(struct seq_file *m, void *v)
120 static const char *hwcap_str[] = {
121 [HWCAP_NR_ESAN3] = "esan3",
122 [HWCAP_NR_ZARCH] = "zarch",
123 [HWCAP_NR_STFLE] = "stfle",
124 [HWCAP_NR_MSA] = "msa",
125 [HWCAP_NR_LDISP] = "ldisp",
126 [HWCAP_NR_EIMM] = "eimm",
127 [HWCAP_NR_DFP] = "dfp",
128 [HWCAP_NR_HPAGE] = "edat",
129 [HWCAP_NR_ETF3EH] = "etf3eh",
130 [HWCAP_NR_HIGH_GPRS] = "highgprs",
131 [HWCAP_NR_TE] = "te",
132 [HWCAP_NR_VXRS] = "vx",
133 [HWCAP_NR_VXRS_BCD] = "vxd",
134 [HWCAP_NR_VXRS_EXT] = "vxe",
135 [HWCAP_NR_GS] = "gs",
136 [HWCAP_NR_VXRS_EXT2] = "vxe2",
137 [HWCAP_NR_VXRS_PDE] = "vxp",
138 [HWCAP_NR_SORT] = "sort",
139 [HWCAP_NR_DFLT] = "dflt",
140 [HWCAP_NR_VXRS_PDE2] = "vxp2",
141 [HWCAP_NR_NNPA] = "nnpa",
142 [HWCAP_NR_PCI_MIO] = "pcimio",
143 [HWCAP_NR_SIE] = "sie",
147 BUILD_BUG_ON(ARRAY_SIZE(hwcap_str) != HWCAP_NR_MAX);
148 seq_printf(m, "vendor_id : IBM/S390\n"
149 "# processors : %i\n"
150 "bogomips per cpu: %lu.%02lu\n",
151 num_online_cpus(), loops_per_jiffy/(500000/HZ),
152 (loops_per_jiffy/(5000/HZ))%100);
153 seq_printf(m, "max thread id : %d\n", smp_cpu_mtid);
154 seq_puts(m, "features\t: ");
155 for (i = 0; i < ARRAY_SIZE(hwcap_str); i++)
156 if (hwcap_str[i] && (elf_hwcap & (1UL << i)))
157 seq_printf(m, "%s ", hwcap_str[i]);
161 for_each_online_cpu(cpu) {
162 struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu);
164 seq_printf(m, "processor %d: "
166 "identification = %06X, "
168 cpu, id->version, id->ident, id->machine);
172 static int __init setup_hwcaps(void)
174 /* instructions named N3, "backported" to esa-mode */
175 elf_hwcap |= HWCAP_ESAN3;
177 /* z/Architecture mode active */
178 elf_hwcap |= HWCAP_ZARCH;
180 /* store-facility-list-extended */
181 if (test_facility(7))
182 elf_hwcap |= HWCAP_STFLE;
184 /* message-security assist */
185 if (test_facility(17))
186 elf_hwcap |= HWCAP_MSA;
188 /* long-displacement */
189 if (test_facility(19))
190 elf_hwcap |= HWCAP_LDISP;
192 /* extended-immediate */
193 elf_hwcap |= HWCAP_EIMM;
195 /* extended-translation facility 3 enhancement */
196 if (test_facility(22) && test_facility(30))
197 elf_hwcap |= HWCAP_ETF3EH;
199 /* decimal floating point & perform floating point operation */
200 if (test_facility(42) && test_facility(44))
201 elf_hwcap |= HWCAP_DFP;
203 /* huge page support */
204 if (MACHINE_HAS_EDAT1)
205 elf_hwcap |= HWCAP_HPAGE;
207 /* 64-bit register support for 31-bit processes */
208 elf_hwcap |= HWCAP_HIGH_GPRS;
210 /* transactional execution */
212 elf_hwcap |= HWCAP_TE;
215 * Vector extension can be disabled with the "novx" parameter.
216 * Use MACHINE_HAS_VX instead of facility bit 129.
218 if (MACHINE_HAS_VX) {
219 elf_hwcap |= HWCAP_VXRS;
220 if (test_facility(134))
221 elf_hwcap |= HWCAP_VXRS_BCD;
222 if (test_facility(135))
223 elf_hwcap |= HWCAP_VXRS_EXT;
224 if (test_facility(148))
225 elf_hwcap |= HWCAP_VXRS_EXT2;
226 if (test_facility(152))
227 elf_hwcap |= HWCAP_VXRS_PDE;
228 if (test_facility(192))
229 elf_hwcap |= HWCAP_VXRS_PDE2;
232 if (test_facility(150))
233 elf_hwcap |= HWCAP_SORT;
235 if (test_facility(151))
236 elf_hwcap |= HWCAP_DFLT;
238 if (test_facility(165))
239 elf_hwcap |= HWCAP_NNPA;
241 /* guarded storage */
243 elf_hwcap |= HWCAP_GS;
245 if (MACHINE_HAS_PCI_MIO)
246 elf_hwcap |= HWCAP_PCI_MIO;
248 /* virtualization support */
250 elf_hwcap |= HWCAP_SIE;
254 arch_initcall(setup_hwcaps);
256 static int __init setup_elf_platform(void)
261 add_device_randomness(&cpu_id, sizeof(cpu_id));
262 switch (cpu_id.machine) {
263 default: /* Use "z10" as default. */
264 strcpy(elf_platform, "z10");
268 strcpy(elf_platform, "z196");
272 strcpy(elf_platform, "zEC12");
276 strcpy(elf_platform, "z13");
280 strcpy(elf_platform, "z14");
284 strcpy(elf_platform, "z15");
288 strcpy(elf_platform, "z16");
293 arch_initcall(setup_elf_platform);
295 static void show_cpu_topology(struct seq_file *m, unsigned long n)
297 #ifdef CONFIG_SCHED_TOPOLOGY
298 seq_printf(m, "physical id : %d\n", topology_physical_package_id(n));
299 seq_printf(m, "core id : %d\n", topology_core_id(n));
300 seq_printf(m, "book id : %d\n", topology_book_id(n));
301 seq_printf(m, "drawer id : %d\n", topology_drawer_id(n));
302 seq_printf(m, "dedicated : %d\n", topology_cpu_dedicated(n));
303 seq_printf(m, "address : %d\n", smp_cpu_get_cpu_address(n));
304 seq_printf(m, "siblings : %d\n", cpumask_weight(topology_core_cpumask(n)));
305 seq_printf(m, "cpu cores : %d\n", topology_booted_cores(n));
306 #endif /* CONFIG_SCHED_TOPOLOGY */
309 static void show_cpu_ids(struct seq_file *m, unsigned long n)
311 struct cpuid *id = &per_cpu(cpu_info.cpu_id, n);
313 seq_printf(m, "version : %02X\n", id->version);
314 seq_printf(m, "identification : %06X\n", id->ident);
315 seq_printf(m, "machine : %04X\n", id->machine);
318 static void show_cpu_mhz(struct seq_file *m, unsigned long n)
320 struct cpu_info *c = per_cpu_ptr(&cpu_info, n);
322 if (!machine_has_cpu_mhz)
324 seq_printf(m, "cpu MHz dynamic : %d\n", c->cpu_mhz_dynamic);
325 seq_printf(m, "cpu MHz static : %d\n", c->cpu_mhz_static);
329 * show_cpuinfo - Get information on one CPU for use by procfs.
331 static int show_cpuinfo(struct seq_file *m, void *v)
333 unsigned long n = (unsigned long) v - 1;
334 unsigned long first = cpumask_first(cpu_online_mask);
337 show_cpu_summary(m, v);
338 seq_printf(m, "\ncpu number : %ld\n", n);
339 show_cpu_topology(m, n);
345 static inline void *c_update(loff_t *pos)
348 *pos = cpumask_next(*pos - 1, cpu_online_mask);
350 *pos = cpumask_first(cpu_online_mask);
351 return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL;
354 static void *c_start(struct seq_file *m, loff_t *pos)
357 return c_update(pos);
360 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
363 return c_update(pos);
366 static void c_stop(struct seq_file *m, void *v)
371 const struct seq_operations cpuinfo_op = {
375 .show = show_cpuinfo,
378 int s390_isolate_bp(void)
380 if (!test_facility(82))
382 set_thread_flag(TIF_ISOLATE_BP);
385 EXPORT_SYMBOL(s390_isolate_bp);
387 int s390_isolate_bp_guest(void)
389 if (!test_facility(82))
391 set_thread_flag(TIF_ISOLATE_BP_GUEST);
394 EXPORT_SYMBOL(s390_isolate_bp_guest);