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