xref: /linux/arch/s390/kernel/processor.c (revision a3a4a816b4b194c45d0217e8b9e08b2639802cda)
1 /*
2  *  Copyright IBM Corp. 2008
3  *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
4  */
5 
6 #define KMSG_COMPONENT "cpu"
7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
8 
9 #include <linux/cpufeature.h>
10 #include <linux/kernel.h>
11 #include <linux/sched/mm.h>
12 #include <linux/init.h>
13 #include <linux/seq_file.h>
14 #include <linux/mm_types.h>
15 #include <linux/delay.h>
16 #include <linux/cpu.h>
17 
18 #include <asm/diag.h>
19 #include <asm/facility.h>
20 #include <asm/elf.h>
21 #include <asm/lowcore.h>
22 #include <asm/param.h>
23 #include <asm/smp.h>
24 
25 struct cpu_info {
26 	unsigned int cpu_mhz_dynamic;
27 	unsigned int cpu_mhz_static;
28 	struct cpuid cpu_id;
29 };
30 
31 static DEFINE_PER_CPU(struct cpu_info, cpu_info);
32 
33 static bool machine_has_cpu_mhz;
34 
35 void __init cpu_detect_mhz_feature(void)
36 {
37 	if (test_facility(34) && __ecag(ECAG_CPU_ATTRIBUTE, 0) != -1UL)
38 		machine_has_cpu_mhz = true;
39 }
40 
41 static void update_cpu_mhz(void *arg)
42 {
43 	unsigned long mhz;
44 	struct cpu_info *c;
45 
46 	mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0);
47 	c = this_cpu_ptr(&cpu_info);
48 	c->cpu_mhz_dynamic = mhz >> 32;
49 	c->cpu_mhz_static = mhz & 0xffffffff;
50 }
51 
52 void s390_update_cpu_mhz(void)
53 {
54 	s390_adjust_jiffies();
55 	if (machine_has_cpu_mhz)
56 		on_each_cpu(update_cpu_mhz, NULL, 0);
57 }
58 
59 void notrace cpu_relax_yield(void)
60 {
61 	if (!smp_cpu_mtid && MACHINE_HAS_DIAG44) {
62 		diag_stat_inc(DIAG_STAT_X044);
63 		asm volatile("diag 0,0,0x44");
64 	}
65 	barrier();
66 }
67 EXPORT_SYMBOL(cpu_relax_yield);
68 
69 /*
70  * cpu_init - initializes state that is per-CPU.
71  */
72 void cpu_init(void)
73 {
74 	struct cpuid *id = this_cpu_ptr(&cpu_info.cpu_id);
75 
76 	get_cpu_id(id);
77 	if (machine_has_cpu_mhz)
78 		update_cpu_mhz(NULL);
79 	mmgrab(&init_mm);
80 	current->active_mm = &init_mm;
81 	BUG_ON(current->mm);
82 	enter_lazy_tlb(&init_mm, current);
83 }
84 
85 /*
86  * cpu_have_feature - Test CPU features on module initialization
87  */
88 int cpu_have_feature(unsigned int num)
89 {
90 	return elf_hwcap & (1UL << num);
91 }
92 EXPORT_SYMBOL(cpu_have_feature);
93 
94 static void show_cpu_summary(struct seq_file *m, void *v)
95 {
96 	static const char *hwcap_str[] = {
97 		"esan3", "zarch", "stfle", "msa", "ldisp", "eimm", "dfp",
98 		"edat", "etf3eh", "highgprs", "te", "vx", "vxd", "vxe"
99 	};
100 	static const char * const int_hwcap_str[] = {
101 		"sie"
102 	};
103 	int i, cpu;
104 
105 	seq_printf(m, "vendor_id       : IBM/S390\n"
106 		   "# processors    : %i\n"
107 		   "bogomips per cpu: %lu.%02lu\n",
108 		   num_online_cpus(), loops_per_jiffy/(500000/HZ),
109 		   (loops_per_jiffy/(5000/HZ))%100);
110 	seq_printf(m, "max thread id   : %d\n", smp_cpu_mtid);
111 	seq_puts(m, "features\t: ");
112 	for (i = 0; i < ARRAY_SIZE(hwcap_str); i++)
113 		if (hwcap_str[i] && (elf_hwcap & (1UL << i)))
114 			seq_printf(m, "%s ", hwcap_str[i]);
115 	for (i = 0; i < ARRAY_SIZE(int_hwcap_str); i++)
116 		if (int_hwcap_str[i] && (int_hwcap & (1UL << i)))
117 			seq_printf(m, "%s ", int_hwcap_str[i]);
118 	seq_puts(m, "\n");
119 	show_cacheinfo(m);
120 	for_each_online_cpu(cpu) {
121 		struct cpuid *id = &per_cpu(cpu_info.cpu_id, cpu);
122 
123 		seq_printf(m, "processor %d: "
124 			   "version = %02X,  "
125 			   "identification = %06X,  "
126 			   "machine = %04X\n",
127 			   cpu, id->version, id->ident, id->machine);
128 	}
129 }
130 
131 static void show_cpu_mhz(struct seq_file *m, unsigned long n)
132 {
133 	struct cpu_info *c = per_cpu_ptr(&cpu_info, n);
134 
135 	seq_printf(m, "cpu MHz dynamic : %d\n", c->cpu_mhz_dynamic);
136 	seq_printf(m, "cpu MHz static  : %d\n", c->cpu_mhz_static);
137 }
138 
139 /*
140  * show_cpuinfo - Get information on one CPU for use by procfs.
141  */
142 static int show_cpuinfo(struct seq_file *m, void *v)
143 {
144 	unsigned long n = (unsigned long) v - 1;
145 
146 	if (!n)
147 		show_cpu_summary(m, v);
148 	if (!machine_has_cpu_mhz)
149 		return 0;
150 	seq_printf(m, "\ncpu number      : %ld\n", n);
151 	show_cpu_mhz(m, n);
152 	return 0;
153 }
154 
155 static inline void *c_update(loff_t *pos)
156 {
157 	if (*pos)
158 		*pos = cpumask_next(*pos - 1, cpu_online_mask);
159 	return *pos < nr_cpu_ids ? (void *)*pos + 1 : NULL;
160 }
161 
162 static void *c_start(struct seq_file *m, loff_t *pos)
163 {
164 	get_online_cpus();
165 	return c_update(pos);
166 }
167 
168 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
169 {
170 	++*pos;
171 	return c_update(pos);
172 }
173 
174 static void c_stop(struct seq_file *m, void *v)
175 {
176 	put_online_cpus();
177 }
178 
179 const struct seq_operations cpuinfo_op = {
180 	.start	= c_start,
181 	.next	= c_next,
182 	.stop	= c_stop,
183 	.show	= show_cpuinfo,
184 };
185