blob: 0becefcf19b97750f880237831570ae159ff259f [file] [log] [blame]
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02001/*
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02002 * Copyright IBM Corp. 2007
3 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
4 */
5
6#include <linux/kernel.h>
7#include <linux/mm.h>
8#include <linux/init.h>
9#include <linux/device.h>
10#include <linux/bootmem.h>
11#include <linux/sched.h>
12#include <linux/workqueue.h>
13#include <linux/cpu.h>
14#include <linux/smp.h>
15#include <asm/delay.h>
16#include <asm/s390_ext.h>
17#include <asm/sysinfo.h>
18
19#define CPU_BITS 64
Heiko Carstensc10fde02008-04-17 07:46:13 +020020#define NR_MAG 6
21
22#define PTF_HORIZONTAL (0UL)
23#define PTF_VERTICAL (1UL)
24#define PTF_CHECK (2UL)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020025
26struct tl_cpu {
Heiko Carstensc10fde02008-04-17 07:46:13 +020027 unsigned char reserved0[4];
28 unsigned char :6;
29 unsigned char pp:2;
30 unsigned char reserved1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020031 unsigned short origin;
32 unsigned long mask[CPU_BITS / BITS_PER_LONG];
33};
34
35struct tl_container {
36 unsigned char reserved[8];
37};
38
39union tl_entry {
40 unsigned char nl;
41 struct tl_cpu cpu;
42 struct tl_container container;
43};
44
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020045struct tl_info {
46 unsigned char reserved0[2];
47 unsigned short length;
48 unsigned char mag[NR_MAG];
49 unsigned char reserved1;
50 unsigned char mnest;
51 unsigned char reserved2[4];
52 union tl_entry tle[0];
53};
54
55struct core_info {
56 struct core_info *next;
57 cpumask_t mask;
58};
59
60static void topology_work_fn(struct work_struct *work);
61static struct tl_info *tl_info;
62static struct core_info core_info;
63static int machine_has_topology;
64static int machine_has_topology_irq;
65static struct timer_list topology_timer;
66static void set_topology_timer(void);
67static DECLARE_WORK(topology_work, topology_work_fn);
68
Heiko Carstensd00aa4e2008-04-30 13:38:40 +020069cpumask_t cpu_core_map[NR_CPUS];
70
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020071cpumask_t cpu_coregroup_map(unsigned int cpu)
72{
73 struct core_info *core = &core_info;
74 cpumask_t mask;
75
76 cpus_clear(mask);
77 if (!machine_has_topology)
78 return cpu_present_map;
79 mutex_lock(&smp_cpu_state_mutex);
80 while (core) {
81 if (cpu_isset(cpu, core->mask)) {
82 mask = core->mask;
83 break;
84 }
85 core = core->next;
86 }
87 mutex_unlock(&smp_cpu_state_mutex);
88 if (cpus_empty(mask))
89 mask = cpumask_of_cpu(cpu);
90 return mask;
91}
92
93static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
94{
95 unsigned int cpu;
96
97 for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
98 cpu < CPU_BITS;
99 cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
100 {
101 unsigned int rcpu, lcpu;
102
103 rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
104 for_each_present_cpu(lcpu) {
Heiko Carstensc10fde02008-04-17 07:46:13 +0200105 if (__cpu_logical_map[lcpu] == rcpu) {
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200106 cpu_set(lcpu, core->mask);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200107 smp_cpu_polarization[lcpu] = tl_cpu->pp;
108 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200109 }
110 }
111}
112
113static void clear_cores(void)
114{
115 struct core_info *core = &core_info;
116
117 while (core) {
118 cpus_clear(core->mask);
119 core = core->next;
120 }
121}
122
123static union tl_entry *next_tle(union tl_entry *tle)
124{
125 if (tle->nl)
126 return (union tl_entry *)((struct tl_container *)tle + 1);
127 else
128 return (union tl_entry *)((struct tl_cpu *)tle + 1);
129}
130
131static void tl_to_cores(struct tl_info *info)
132{
133 union tl_entry *tle, *end;
134 struct core_info *core = &core_info;
135
136 mutex_lock(&smp_cpu_state_mutex);
137 clear_cores();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200138 tle = info->tle;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200139 end = (union tl_entry *)((unsigned long)info + info->length);
140 while (tle < end) {
141 switch (tle->nl) {
142 case 5:
143 case 4:
144 case 3:
145 case 2:
146 break;
147 case 1:
148 core = core->next;
149 break;
150 case 0:
151 add_cpus_to_core(&tle->cpu, core);
152 break;
153 default:
154 clear_cores();
155 machine_has_topology = 0;
156 return;
157 }
158 tle = next_tle(tle);
159 }
160 mutex_unlock(&smp_cpu_state_mutex);
161}
162
Heiko Carstensc10fde02008-04-17 07:46:13 +0200163static void topology_update_polarization_simple(void)
164{
165 int cpu;
166
167 mutex_lock(&smp_cpu_state_mutex);
168 for_each_present_cpu(cpu)
169 smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
170 mutex_unlock(&smp_cpu_state_mutex);
171}
172
173static int ptf(unsigned long fc)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200174{
175 int rc;
176
177 asm volatile(
178 " .insn rre,0xb9a20000,%1,%1\n"
179 " ipm %0\n"
180 " srl %0,28\n"
181 : "=d" (rc)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200182 : "d" (fc) : "cc");
183 return rc;
184}
185
186int topology_set_cpu_management(int fc)
187{
188 int cpu;
189 int rc;
190
191 if (!machine_has_topology)
192 return -EOPNOTSUPP;
193 if (fc)
194 rc = ptf(PTF_VERTICAL);
195 else
196 rc = ptf(PTF_HORIZONTAL);
197 if (rc)
198 return -EBUSY;
199 for_each_present_cpu(cpu)
200 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200201 return rc;
202}
203
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200204static void update_cpu_core_map(void)
205{
206 int cpu;
207
208 for_each_present_cpu(cpu)
209 cpu_core_map[cpu] = cpu_coregroup_map(cpu);
210}
211
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200212void arch_update_cpu_topology(void)
213{
214 struct tl_info *info = tl_info;
215 struct sys_device *sysdev;
216 int cpu;
217
Heiko Carstensc10fde02008-04-17 07:46:13 +0200218 if (!machine_has_topology) {
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200219 update_cpu_core_map();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200220 topology_update_polarization_simple();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200221 return;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200222 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200223 stsi(info, 15, 1, 2);
224 tl_to_cores(info);
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200225 update_cpu_core_map();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200226 for_each_online_cpu(cpu) {
227 sysdev = get_cpu_sysdev(cpu);
228 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
229 }
230}
231
232static void topology_work_fn(struct work_struct *work)
233{
234 arch_reinit_sched_domains();
235}
236
Heiko Carstensc10fde02008-04-17 07:46:13 +0200237void topology_schedule_update(void)
238{
239 schedule_work(&topology_work);
240}
241
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200242static void topology_timer_fn(unsigned long ignored)
243{
Heiko Carstensc10fde02008-04-17 07:46:13 +0200244 if (ptf(PTF_CHECK))
245 topology_schedule_update();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200246 set_topology_timer();
247}
248
249static void set_topology_timer(void)
250{
251 topology_timer.function = topology_timer_fn;
252 topology_timer.data = 0;
253 topology_timer.expires = jiffies + 60 * HZ;
254 add_timer(&topology_timer);
255}
256
257static void topology_interrupt(__u16 code)
258{
259 schedule_work(&topology_work);
260}
261
262static int __init init_topology_update(void)
263{
264 int rc;
265
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200266 rc = 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200267 if (!machine_has_topology) {
268 topology_update_polarization_simple();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200269 goto out;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200270 }
271 init_timer_deferrable(&topology_timer);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200272 if (machine_has_topology_irq) {
273 rc = register_external_interrupt(0x2005, topology_interrupt);
274 if (rc)
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200275 goto out;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200276 ctl_set_bit(0, 8);
277 }
278 else
279 set_topology_timer();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200280out:
281 update_cpu_core_map();
282 return rc;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200283}
284__initcall(init_topology_update);
285
286void __init s390_init_cpu_topology(void)
287{
288 unsigned long long facility_bits;
289 struct tl_info *info;
290 struct core_info *core;
291 int nr_cores;
292 int i;
293
294 if (stfle(&facility_bits, 1) <= 0)
295 return;
296 if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
297 return;
298 machine_has_topology = 1;
299
300 if (facility_bits & (1ULL << 51))
301 machine_has_topology_irq = 1;
302
303 tl_info = alloc_bootmem_pages(PAGE_SIZE);
304 if (!tl_info)
305 goto error;
306 info = tl_info;
307 stsi(info, 15, 1, 2);
308
309 nr_cores = info->mag[NR_MAG - 2];
310 for (i = 0; i < info->mnest - 2; i++)
311 nr_cores *= info->mag[NR_MAG - 3 - i];
312
313 printk(KERN_INFO "CPU topology:");
314 for (i = 0; i < NR_MAG; i++)
315 printk(" %d", info->mag[i]);
316 printk(" / %d\n", info->mnest);
317
318 core = &core_info;
319 for (i = 0; i < nr_cores; i++) {
320 core->next = alloc_bootmem(sizeof(struct core_info));
321 core = core->next;
322 if (!core)
323 goto error;
324 }
325 return;
326error:
327 machine_has_topology = 0;
328 machine_has_topology_irq = 0;
329}