xref: /linux/arch/s390/kernel/processor.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright IBM Corp. 2008
4  *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
5  */
6 
7 #define pr_fmt(fmt) "cpu: " fmt
8 
9 #include <linux/stop_machine.h>
10 #include <linux/cpufeature.h>
11 #include <linux/bitops.h>
12 #include <linux/kernel.h>
13 #include <linux/random.h>
14 #include <linux/sched/mm.h>
15 #include <linux/init.h>
16 #include <linux/seq_file.h>
17 #include <linux/mm_types.h>
18 #include <linux/delay.h>
19 #include <linux/cpu.h>
20 #include <linux/smp.h>
21 #include <asm/text-patching.h>
22 #include <asm/machine.h>
23 #include <asm/diag.h>
24 #include <asm/facility.h>
25 #include <asm/elf.h>
26 #include <asm/lowcore.h>
27 #include <asm/param.h>
28 #include <asm/sclp.h>
29 #include <asm/smp.h>
30 
31 unsigned long __read_mostly elf_hwcap;
32 char elf_platform[ELF_PLATFORM_SIZE];
33 
34 struct cpu_info {
35 	unsigned int cpu_mhz_dynamic;
36 	unsigned int cpu_mhz_static;
37 	struct cpuid cpu_id;
38 };
39 
40 static DEFINE_PER_CPU(struct cpu_info, cpu_info);
41 static DEFINE_PER_CPU(int, cpu_relax_retry);
42 
43 static bool machine_has_cpu_mhz;
44 
45 void __init cpu_detect_mhz_feature(void)
46 {
47 	if (test_facility(34) && __ecag(ECAG_CPU_ATTRIBUTE, 0) != -1UL)
48 		machine_has_cpu_mhz = true;
49 }
50 
51 static void update_cpu_mhz(void *arg)
52 {
53 	unsigned long mhz;
54 	struct cpu_info *c;
55 
56 	mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
57 	c = this_cpu_ptr(&cpu_info);
58 	c->cpu_mhz_dynamic = mhz >> 32;
59 	c->cpu_mhz_static = mhz & 0xffffffff;
60 }
61 
62 void s390_update_cpu_mhz(void)
63 {
64 	s390_adjust_jiffies();
65 	if (machine_has_cpu_mhz)
66 		on_each_cpu(update_cpu_mhz, NULL, 0);
67 }
68 
69 void notrace stop_machine_yield(const struct cpumask *cpumask)
70 {
71 	int cpu, this_cpu;
72 
73 	this_cpu = smp_processor_id();
74 	if (__this_cpu_inc_return(cpu_relax_retry) >= spin_retry) {
75 		__this_cpu_write(cpu_relax_retry, 0);
76 		cpu = cpumask_next_wrap(this_cpu, cpumask);
77 		if (cpu >= nr_cpu_ids)
78 			return;
79 		if (arch_vcpu_is_preempted(cpu))
80 			smp_yield_cpu(cpu);
81 	}
82 }
83 
84 static void do_sync_core(void *info)
85 {
86 	sync_core();
87 }
88 
89 void text_poke_sync(void)
90 {
91 	on_each_cpu(do_sync_core, NULL, 1);
92 }
93 
94 void text_poke_sync_lock(void)
95 {
96 	cpus_read_lock();
97 	text_poke_sync();
98 	cpus_read_unlock();
99 }
100 
101 /*
102  * cpu_init - initializes state that is per-CPU.
103  */
104 void cpu_init(void)
105 {
106 	struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id);
107 
108 	get_cpu_id(id);
109 	if (machine_has_cpu_mhz)
110 		update_cpu_mhz(NULL);
111 	mmgrab(&init_mm);
112 	current->active_mm = &init_mm;
113 	BUG_ON(current->mm);
114 	enter_lazy_tlb(&init_mm, current);
115 }
116 
117 static void show_facilities(struct seq_file *m)
118 {
119 	unsigned int bit;
120 
121 	seq_puts(m, "facilities      :");
122 	for_each_set_bit_inv(bit, (long *)&stfle_fac_list, MAX_FACILITY_BIT)
123 		seq_printf(m, " %d", bit);
124 	seq_putc(m, '\n');
125 }
126 
127 static void show_cpu_summary(struct seq_file *m, void *v)
128 {
129 	static const char *hwcap_str[] = {
130 		[HWCAP_NR_ESAN3]	= "esan3",
131 		[HWCAP_NR_ZARCH]	= "zarch",
132 		[HWCAP_NR_STFLE]	= "stfle",
133 		[HWCAP_NR_MSA]		= "msa",
134 		[HWCAP_NR_LDISP]	= "ldisp",
135 		[HWCAP_NR_EIMM]		= "eimm",
136 		[HWCAP_NR_DFP]		= "dfp",
137 		[HWCAP_NR_HPAGE]	= "edat",
138 		[HWCAP_NR_ETF3EH]	= "etf3eh",
139 		[HWCAP_NR_HIGH_GPRS]	= "highgprs",
140 		[HWCAP_NR_TE]		= "te",
141 		[HWCAP_NR_VXRS]		= "vx",
142 		[HWCAP_NR_VXRS_BCD]	= "vxd",
143 		[HWCAP_NR_VXRS_EXT]	= "vxe",
144 		[HWCAP_NR_GS]		= "gs",
145 		[HWCAP_NR_VXRS_EXT2]	= "vxe2",
146 		[HWCAP_NR_VXRS_PDE]	= "vxp",
147 		[HWCAP_NR_SORT]		= "sort",
148 		[HWCAP_NR_DFLT]		= "dflt",
149 		[HWCAP_NR_VXRS_PDE2]	= "vxp2",
150 		[HWCAP_NR_NNPA]		= "nnpa",
151 		[HWCAP_NR_PCI_MIO]	= "pcimio",
152 		[HWCAP_NR_SIE]		= "sie",
153 		[HWCAP_NR_SAE]		= "sae",
154 	};
155 	int i, cpu;
156 
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]);
168 	seq_puts(m, "\n");
169 	show_facilities(m);
170 	show_cacheinfo(m);
171 	for_each_online_cpu(cpu) {
172 		struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu);
173 
174 		seq_printf(m, "processor %d: "
175 			   "version = %02X,  "
176 			   "identification = %06X,  "
177 			   "machine = %04X\n",
178 			   cpu, id->version, id->ident, id->machine);
179 	}
180 }
181 
182 static int __init setup_hwcaps(void)
183 {
184 	/* instructions named N3, "backported" to esa-mode */
185 	elf_hwcap |= HWCAP_ESAN3;
186 
187 	/* z/Architecture mode active */
188 	elf_hwcap |= HWCAP_ZARCH;
189 
190 	/* store-facility-list-extended */
191 	if (test_facility(7))
192 		elf_hwcap |= HWCAP_STFLE;
193 
194 	/* message-security assist */
195 	if (test_facility(17))
196 		elf_hwcap |= HWCAP_MSA;
197 
198 	/* long-displacement */
199 	if (test_facility(19))
200 		elf_hwcap |= HWCAP_LDISP;
201 
202 	/* extended-immediate */
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 (test_facility(42) && test_facility(44))
211 		elf_hwcap |= HWCAP_DFP;
212 
213 	/* huge page support */
214 	if (cpu_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_tx())
222 		elf_hwcap |= HWCAP_TE;
223 
224 	/* vector */
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;
237 	}
238 
239 	if (test_facility(150))
240 		elf_hwcap |= HWCAP_SORT;
241 
242 	if (test_facility(151))
243 		elf_hwcap |= HWCAP_DFLT;
244 
245 	if (test_facility(165))
246 		elf_hwcap |= HWCAP_NNPA;
247 
248 	/* guarded storage */
249 	if (cpu_has_gs())
250 		elf_hwcap |= HWCAP_GS;
251 
252 	if (test_machine_feature(MFEATURE_PCI_MIO))
253 		elf_hwcap |= HWCAP_PCI_MIO;
254 
255 	/* virtualization support */
256 	if (sclp.has_sief2)
257 		elf_hwcap |= HWCAP_SIE;
258 	if (sclp.has_aef)
259 		elf_hwcap |= HWCAP_SAE;
260 
261 	return 0;
262 }
263 arch_initcall(setup_hwcaps);
264 
265 static int __init setup_elf_platform(void)
266 {
267 	struct cpuid cpu_id;
268 
269 	get_cpu_id(&cpu_id);
270 	add_device_randomness(&cpu_id, sizeof(cpu_id));
271 	switch (cpu_id.machine) {
272 	default:	/* Use "z10" as default. */
273 		strscpy(elf_platform, "z10");
274 		break;
275 	case 0x2817:
276 	case 0x2818:
277 		strscpy(elf_platform, "z196");
278 		break;
279 	case 0x2827:
280 	case 0x2828:
281 		strscpy(elf_platform, "zEC12");
282 		break;
283 	case 0x2964:
284 	case 0x2965:
285 		strscpy(elf_platform, "z13");
286 		break;
287 	case 0x3906:
288 	case 0x3907:
289 		strscpy(elf_platform, "z14");
290 		break;
291 	case 0x8561:
292 	case 0x8562:
293 		strscpy(elf_platform, "z15");
294 		break;
295 	case 0x3931:
296 	case 0x3932:
297 		strscpy(elf_platform, "z16");
298 		break;
299 	case 0x9175:
300 	case 0x9176:
301 		strscpy(elf_platform, "z17");
302 		break;
303 	}
304 	return 0;
305 }
306 arch_initcall(setup_elf_platform);
307 
308 static void show_cpu_topology(struct seq_file *m, unsigned long n)
309 {
310 #ifdef CONFIG_SCHED_TOPOLOGY
311 	seq_printf(m, "physical id     : %d\n", topology_physical_package_id(n));
312 	seq_printf(m, "core id         : %d\n", topology_core_id(n));
313 	seq_printf(m, "book id         : %d\n", topology_book_id(n));
314 	seq_printf(m, "drawer id       : %d\n", topology_drawer_id(n));
315 	seq_printf(m, "dedicated       : %d\n", topology_cpu_dedicated(n));
316 	seq_printf(m, "address         : %d\n", smp_cpu_get_cpu_address(n));
317 	seq_printf(m, "siblings        : %d\n", cpumask_weight(topology_core_cpumask(n)));
318 	seq_printf(m, "cpu cores       : %d\n", topology_booted_cores(n));
319 #endif /* CONFIG_SCHED_TOPOLOGY */
320 }
321 
322 static void show_cpu_ids(struct seq_file *m, unsigned long n)
323 {
324 	struct cpuid *id = &per_cpu(cpu_info.cpu_id, n);
325 
326 	seq_printf(m, "version         : %02X\n", id->version);
327 	seq_printf(m, "identification  : %06X\n", id->ident);
328 	seq_printf(m, "machine         : %04X\n", id->machine);
329 }
330 
331 static void show_cpu_mhz(struct seq_file *m, unsigned long n)
332 {
333 	struct cpu_info *c = per_cpu_ptr(&cpu_info, n);
334 
335 	if (!machine_has_cpu_mhz)
336 		return;
337 	seq_printf(m, "cpu MHz dynamic : %d\n", c->cpu_mhz_dynamic);
338 	seq_printf(m, "cpu MHz static  : %d\n", c->cpu_mhz_static);
339 }
340 
341 /*
342  * show_cpuinfo - Get information on one CPU for use by procfs.
343  */
344 static int show_cpuinfo(struct seq_file *m, void *v)
345 {
346 	unsigned long n = (unsigned long) v - 1;
347 	unsigned long first = cpumask_first(cpu_online_mask);
348 
349 	if (n == first)
350 		show_cpu_summary(m, v);
351 	seq_printf(m, "\ncpu number      : %ld\n", n);
352 	show_cpu_topology(m, n);
353 	show_cpu_ids(m, n);
354 	show_cpu_mhz(m, n);
355 	return 0;
356 }
357 
358 static inline void *c_update(loff_t *pos)
359 {
360 	if (*pos)
361 		*pos = cpumask_next(*pos - 1, cpu_online_mask);
362 	else
363 		*pos = cpumask_first(cpu_online_mask);
364 	return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL;
365 }
366 
367 static void *c_start(struct seq_file *m, loff_t *pos)
368 {
369 	cpus_read_lock();
370 	return c_update(pos);
371 }
372 
373 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
374 {
375 	++*pos;
376 	return c_update(pos);
377 }
378 
379 static void c_stop(struct seq_file *m, void *v)
380 {
381 	cpus_read_unlock();
382 }
383 
384 const struct seq_operations cpuinfo_op = {
385 	.start	= c_start,
386 	.next	= c_next,
387 	.stop	= c_stop,
388 	.show	= show_cpuinfo,
389 };
390