xref: /openbmc/linux/arch/s390/kernel/processor.c (revision 83946783)
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 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 struct cpu_info {
34 	unsigned int cpu_mhz_dynamic;
35 	unsigned int cpu_mhz_static;
36 	struct cpuid cpu_id;
37 };
38 
39 static DEFINE_PER_CPU(struct cpu_info, cpu_info);
40 static DEFINE_PER_CPU(int, cpu_relax_retry);
41 
42 static bool machine_has_cpu_mhz;
43 
44 void __init cpu_detect_mhz_feature(void)
45 {
46 	if (test_facility(34) && __ecag(ECAG_CPU_ATTRIBUTE, 0) != -1UL)
47 		machine_has_cpu_mhz = true;
48 }
49 
50 static void update_cpu_mhz(void *arg)
51 {
52 	unsigned long mhz;
53 	struct cpu_info *c;
54 
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;
59 }
60 
61 void s390_update_cpu_mhz(void)
62 {
63 	s390_adjust_jiffies();
64 	if (machine_has_cpu_mhz)
65 		on_each_cpu(update_cpu_mhz, NULL, 0);
66 }
67 
68 void notrace stop_machine_yield(const struct cpumask *cpumask)
69 {
70 	int cpu, this_cpu;
71 
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)
77 			return;
78 		if (arch_vcpu_is_preempted(cpu))
79 			smp_yield_cpu(cpu);
80 	}
81 }
82 
83 /*
84  * cpu_init - initializes state that is per-CPU.
85  */
86 void cpu_init(void)
87 {
88 	struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id);
89 
90 	get_cpu_id(id);
91 	if (machine_has_cpu_mhz)
92 		update_cpu_mhz(NULL);
93 	mmgrab(&init_mm);
94 	current->active_mm = &init_mm;
95 	BUG_ON(current->mm);
96 	enter_lazy_tlb(&init_mm, current);
97 }
98 
99 /*
100  * cpu_have_feature - Test CPU features on module initialization
101  */
102 int cpu_have_feature(unsigned int num)
103 {
104 	return elf_hwcap & (1UL << num);
105 }
106 EXPORT_SYMBOL(cpu_have_feature);
107 
108 static void show_facilities(struct seq_file *m)
109 {
110 	unsigned int bit;
111 
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);
115 	seq_putc(m, '\n');
116 }
117 
118 static void show_cpu_summary(struct seq_file *m, void *v)
119 {
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",
144 	};
145 	int i, cpu;
146 
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]);
158 	seq_puts(m, "\n");
159 	show_facilities(m);
160 	show_cacheinfo(m);
161 	for_each_online_cpu(cpu) {
162 		struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu);
163 
164 		seq_printf(m, "processor %d: "
165 			   "version = %02X,  "
166 			   "identification = %06X,  "
167 			   "machine = %04X\n",
168 			   cpu, id->version, id->ident, id->machine);
169 	}
170 }
171 
172 static int __init setup_hwcaps(void)
173 {
174 	/* instructions named N3, "backported" to esa-mode */
175 	if (test_facility(0))
176 		elf_hwcap |= HWCAP_ESAN3;
177 
178 	/* z/Architecture mode active */
179 	elf_hwcap |= HWCAP_ZARCH;
180 
181 	/* store-facility-list-extended */
182 	if (test_facility(7))
183 		elf_hwcap |= HWCAP_STFLE;
184 
185 	/* message-security assist */
186 	if (test_facility(17))
187 		elf_hwcap |= HWCAP_MSA;
188 
189 	/* long-displacement */
190 	if (test_facility(19))
191 		elf_hwcap |= HWCAP_LDISP;
192 
193 	/* extended-immediate */
194 	if (test_facility(21))
195 		elf_hwcap |= HWCAP_EIMM;
196 
197 	/* extended-translation facility 3 enhancement */
198 	if (test_facility(22) && test_facility(30))
199 		elf_hwcap |= HWCAP_ETF3EH;
200 
201 	/* decimal floating point & perform floating point operation */
202 	if (test_facility(42) && test_facility(44))
203 		elf_hwcap |= HWCAP_DFP;
204 
205 	/* huge page support */
206 	if (MACHINE_HAS_EDAT1)
207 		elf_hwcap |= HWCAP_HPAGE;
208 
209 	/* 64-bit register support for 31-bit processes */
210 	elf_hwcap |= HWCAP_HIGH_GPRS;
211 
212 	/* transactional execution */
213 	if (MACHINE_HAS_TE)
214 		elf_hwcap |= HWCAP_TE;
215 
216 	/*
217 	 * Vector extension can be disabled with the "novx" parameter.
218 	 * Use MACHINE_HAS_VX instead of facility bit 129.
219 	 */
220 	if (MACHINE_HAS_VX) {
221 		elf_hwcap |= HWCAP_VXRS;
222 		if (test_facility(134))
223 			elf_hwcap |= HWCAP_VXRS_BCD;
224 		if (test_facility(135))
225 			elf_hwcap |= HWCAP_VXRS_EXT;
226 		if (test_facility(148))
227 			elf_hwcap |= HWCAP_VXRS_EXT2;
228 		if (test_facility(152))
229 			elf_hwcap |= HWCAP_VXRS_PDE;
230 		if (test_facility(192))
231 			elf_hwcap |= HWCAP_VXRS_PDE2;
232 	}
233 
234 	if (test_facility(150))
235 		elf_hwcap |= HWCAP_SORT;
236 
237 	if (test_facility(151))
238 		elf_hwcap |= HWCAP_DFLT;
239 
240 	if (test_facility(165))
241 		elf_hwcap |= HWCAP_NNPA;
242 
243 	/* guarded storage */
244 	if (MACHINE_HAS_GS)
245 		elf_hwcap |= HWCAP_GS;
246 
247 	if (MACHINE_HAS_PCI_MIO)
248 		elf_hwcap |= HWCAP_PCI_MIO;
249 
250 	/* virtualization support */
251 	if (sclp.has_sief2)
252 		elf_hwcap |= HWCAP_SIE;
253 
254 	return 0;
255 }
256 arch_initcall(setup_hwcaps);
257 
258 static int __init setup_elf_platform(void)
259 {
260 	struct cpuid cpu_id;
261 
262 	get_cpu_id(&cpu_id);
263 	add_device_randomness(&cpu_id, sizeof(cpu_id));
264 	switch (cpu_id.machine) {
265 	case 0x2064:
266 	case 0x2066:
267 	default:	/* Use "z900" as default for 64 bit kernels. */
268 		strcpy(elf_platform, "z900");
269 		break;
270 	case 0x2084:
271 	case 0x2086:
272 		strcpy(elf_platform, "z990");
273 		break;
274 	case 0x2094:
275 	case 0x2096:
276 		strcpy(elf_platform, "z9-109");
277 		break;
278 	case 0x2097:
279 	case 0x2098:
280 		strcpy(elf_platform, "z10");
281 		break;
282 	case 0x2817:
283 	case 0x2818:
284 		strcpy(elf_platform, "z196");
285 		break;
286 	case 0x2827:
287 	case 0x2828:
288 		strcpy(elf_platform, "zEC12");
289 		break;
290 	case 0x2964:
291 	case 0x2965:
292 		strcpy(elf_platform, "z13");
293 		break;
294 	case 0x3906:
295 	case 0x3907:
296 		strcpy(elf_platform, "z14");
297 		break;
298 	case 0x8561:
299 	case 0x8562:
300 		strcpy(elf_platform, "z15");
301 		break;
302 	}
303 	return 0;
304 }
305 arch_initcall(setup_elf_platform);
306 
307 static void show_cpu_topology(struct seq_file *m, unsigned long n)
308 {
309 #ifdef CONFIG_SCHED_TOPOLOGY
310 	seq_printf(m, "physical id     : %d\n", topology_physical_package_id(n));
311 	seq_printf(m, "core id         : %d\n", topology_core_id(n));
312 	seq_printf(m, "book id         : %d\n", topology_book_id(n));
313 	seq_printf(m, "drawer id       : %d\n", topology_drawer_id(n));
314 	seq_printf(m, "dedicated       : %d\n", topology_cpu_dedicated(n));
315 	seq_printf(m, "address         : %d\n", smp_cpu_get_cpu_address(n));
316 	seq_printf(m, "siblings        : %d\n", cpumask_weight(topology_core_cpumask(n)));
317 	seq_printf(m, "cpu cores       : %d\n", topology_booted_cores(n));
318 #endif /* CONFIG_SCHED_TOPOLOGY */
319 }
320 
321 static void show_cpu_ids(struct seq_file *m, unsigned long n)
322 {
323 	struct cpuid *id = &per_cpu(cpu_info.cpu_id, n);
324 
325 	seq_printf(m, "version         : %02X\n", id->version);
326 	seq_printf(m, "identification  : %06X\n", id->ident);
327 	seq_printf(m, "machine         : %04X\n", id->machine);
328 }
329 
330 static void show_cpu_mhz(struct seq_file *m, unsigned long n)
331 {
332 	struct cpu_info *c = per_cpu_ptr(&cpu_info, n);
333 
334 	if (!machine_has_cpu_mhz)
335 		return;
336 	seq_printf(m, "cpu MHz dynamic : %d\n", c->cpu_mhz_dynamic);
337 	seq_printf(m, "cpu MHz static  : %d\n", c->cpu_mhz_static);
338 }
339 
340 /*
341  * show_cpuinfo - Get information on one CPU for use by procfs.
342  */
343 static int show_cpuinfo(struct seq_file *m, void *v)
344 {
345 	unsigned long n = (unsigned long) v - 1;
346 	unsigned long first = cpumask_first(cpu_online_mask);
347 
348 	if (n == first)
349 		show_cpu_summary(m, v);
350 	seq_printf(m, "\ncpu number      : %ld\n", n);
351 	show_cpu_topology(m, n);
352 	show_cpu_ids(m, n);
353 	show_cpu_mhz(m, n);
354 	return 0;
355 }
356 
357 static inline void *c_update(loff_t *pos)
358 {
359 	if (*pos)
360 		*pos = cpumask_next(*pos - 1, cpu_online_mask);
361 	else
362 		*pos = cpumask_first(cpu_online_mask);
363 	return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL;
364 }
365 
366 static void *c_start(struct seq_file *m, loff_t *pos)
367 {
368 	cpus_read_lock();
369 	return c_update(pos);
370 }
371 
372 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
373 {
374 	++*pos;
375 	return c_update(pos);
376 }
377 
378 static void c_stop(struct seq_file *m, void *v)
379 {
380 	cpus_read_unlock();
381 }
382 
383 const struct seq_operations cpuinfo_op = {
384 	.start	= c_start,
385 	.next	= c_next,
386 	.stop	= c_stop,
387 	.show	= show_cpuinfo,
388 };
389 
390 int s390_isolate_bp(void)
391 {
392 	if (!test_facility(82))
393 		return -EOPNOTSUPP;
394 	set_thread_flag(TIF_ISOLATE_BP);
395 	return 0;
396 }
397 EXPORT_SYMBOL(s390_isolate_bp);
398 
399 int s390_isolate_bp_guest(void)
400 {
401 	if (!test_facility(82))
402 		return -EOPNOTSUPP;
403 	set_thread_flag(TIF_ISOLATE_BP_GUEST);
404 	return 0;
405 }
406 EXPORT_SYMBOL(s390_isolate_bp_guest);
407