]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/s390/kernel/topology.c
[S390] cpu topology: introduce kernel parameter
[linux-2.6-omap-h63xx.git] / arch / s390 / kernel / topology.c
1 /*
2  *    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 <linux/cpuset.h>
16 #include <asm/delay.h>
17 #include <asm/s390_ext.h>
18 #include <asm/sysinfo.h>
19
20 #define CPU_BITS 64
21 #define NR_MAG 6
22
23 #define PTF_HORIZONTAL  (0UL)
24 #define PTF_VERTICAL    (1UL)
25 #define PTF_CHECK       (2UL)
26
27 struct tl_cpu {
28         unsigned char reserved0[4];
29         unsigned char :6;
30         unsigned char pp:2;
31         unsigned char reserved1;
32         unsigned short origin;
33         unsigned long mask[CPU_BITS / BITS_PER_LONG];
34 };
35
36 struct tl_container {
37         unsigned char reserved[8];
38 };
39
40 union tl_entry {
41         unsigned char nl;
42         struct tl_cpu cpu;
43         struct tl_container container;
44 };
45
46 struct tl_info {
47         unsigned char reserved0[2];
48         unsigned short length;
49         unsigned char mag[NR_MAG];
50         unsigned char reserved1;
51         unsigned char mnest;
52         unsigned char reserved2[4];
53         union tl_entry tle[0];
54 };
55
56 struct core_info {
57         struct core_info *next;
58         cpumask_t mask;
59 };
60
61 static int topology_enabled;
62 static void topology_work_fn(struct work_struct *work);
63 static struct tl_info *tl_info;
64 static struct core_info core_info;
65 static int machine_has_topology;
66 static int machine_has_topology_irq;
67 static struct timer_list topology_timer;
68 static void set_topology_timer(void);
69 static DECLARE_WORK(topology_work, topology_work_fn);
70 /* topology_lock protects the core linked list */
71 static DEFINE_SPINLOCK(topology_lock);
72
73 cpumask_t cpu_core_map[NR_CPUS];
74
75 cpumask_t cpu_coregroup_map(unsigned int cpu)
76 {
77         struct core_info *core = &core_info;
78         unsigned long flags;
79         cpumask_t mask;
80
81         cpus_clear(mask);
82         if (!topology_enabled || !machine_has_topology)
83                 return cpu_possible_map;
84         spin_lock_irqsave(&topology_lock, flags);
85         while (core) {
86                 if (cpu_isset(cpu, core->mask)) {
87                         mask = core->mask;
88                         break;
89                 }
90                 core = core->next;
91         }
92         spin_unlock_irqrestore(&topology_lock, flags);
93         if (cpus_empty(mask))
94                 mask = cpumask_of_cpu(cpu);
95         return mask;
96 }
97
98 static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
99 {
100         unsigned int cpu;
101
102         for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
103              cpu < CPU_BITS;
104              cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
105         {
106                 unsigned int rcpu, lcpu;
107
108                 rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
109                 for_each_present_cpu(lcpu) {
110                         if (__cpu_logical_map[lcpu] == rcpu) {
111                                 cpu_set(lcpu, core->mask);
112                                 smp_cpu_polarization[lcpu] = tl_cpu->pp;
113                         }
114                 }
115         }
116 }
117
118 static void clear_cores(void)
119 {
120         struct core_info *core = &core_info;
121
122         while (core) {
123                 cpus_clear(core->mask);
124                 core = core->next;
125         }
126 }
127
128 static union tl_entry *next_tle(union tl_entry *tle)
129 {
130         if (tle->nl)
131                 return (union tl_entry *)((struct tl_container *)tle + 1);
132         else
133                 return (union tl_entry *)((struct tl_cpu *)tle + 1);
134 }
135
136 static void tl_to_cores(struct tl_info *info)
137 {
138         union tl_entry *tle, *end;
139         struct core_info *core = &core_info;
140
141         spin_lock_irq(&topology_lock);
142         clear_cores();
143         tle = info->tle;
144         end = (union tl_entry *)((unsigned long)info + info->length);
145         while (tle < end) {
146                 switch (tle->nl) {
147                 case 5:
148                 case 4:
149                 case 3:
150                 case 2:
151                         break;
152                 case 1:
153                         core = core->next;
154                         break;
155                 case 0:
156                         add_cpus_to_core(&tle->cpu, core);
157                         break;
158                 default:
159                         clear_cores();
160                         machine_has_topology = 0;
161                         return;
162                 }
163                 tle = next_tle(tle);
164         }
165         spin_unlock_irq(&topology_lock);
166 }
167
168 static void topology_update_polarization_simple(void)
169 {
170         int cpu;
171
172         mutex_lock(&smp_cpu_state_mutex);
173         for_each_possible_cpu(cpu)
174                 smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
175         mutex_unlock(&smp_cpu_state_mutex);
176 }
177
178 static int ptf(unsigned long fc)
179 {
180         int rc;
181
182         asm volatile(
183                 "       .insn   rre,0xb9a20000,%1,%1\n"
184                 "       ipm     %0\n"
185                 "       srl     %0,28\n"
186                 : "=d" (rc)
187                 : "d" (fc)  : "cc");
188         return rc;
189 }
190
191 int topology_set_cpu_management(int fc)
192 {
193         int cpu;
194         int rc;
195
196         if (!machine_has_topology)
197                 return -EOPNOTSUPP;
198         if (fc)
199                 rc = ptf(PTF_VERTICAL);
200         else
201                 rc = ptf(PTF_HORIZONTAL);
202         if (rc)
203                 return -EBUSY;
204         for_each_possible_cpu(cpu)
205                 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
206         return rc;
207 }
208
209 static void update_cpu_core_map(void)
210 {
211         int cpu;
212
213         for_each_possible_cpu(cpu)
214                 cpu_core_map[cpu] = cpu_coregroup_map(cpu);
215 }
216
217 void arch_update_cpu_topology(void)
218 {
219         struct tl_info *info = tl_info;
220         struct sys_device *sysdev;
221         int cpu;
222
223         if (!machine_has_topology) {
224                 update_cpu_core_map();
225                 topology_update_polarization_simple();
226                 return;
227         }
228         stsi(info, 15, 1, 2);
229         tl_to_cores(info);
230         update_cpu_core_map();
231         for_each_online_cpu(cpu) {
232                 sysdev = get_cpu_sysdev(cpu);
233                 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
234         }
235 }
236
237 static void topology_work_fn(struct work_struct *work)
238 {
239         rebuild_sched_domains();
240 }
241
242 void topology_schedule_update(void)
243 {
244         schedule_work(&topology_work);
245 }
246
247 static void topology_timer_fn(unsigned long ignored)
248 {
249         if (ptf(PTF_CHECK))
250                 topology_schedule_update();
251         set_topology_timer();
252 }
253
254 static void set_topology_timer(void)
255 {
256         topology_timer.function = topology_timer_fn;
257         topology_timer.data = 0;
258         topology_timer.expires = jiffies + 60 * HZ;
259         add_timer(&topology_timer);
260 }
261
262 static void topology_interrupt(__u16 code)
263 {
264         schedule_work(&topology_work);
265 }
266
267 static int __init early_parse_topology(char *p)
268 {
269         if (strncmp(p, "on", 2))
270                 return 0;
271         topology_enabled = 1;
272         return 0;
273 }
274 early_param("topology", early_parse_topology);
275
276 static int __init init_topology_update(void)
277 {
278         int rc;
279
280         rc = 0;
281         if (!machine_has_topology) {
282                 topology_update_polarization_simple();
283                 goto out;
284         }
285         init_timer_deferrable(&topology_timer);
286         if (machine_has_topology_irq) {
287                 rc = register_external_interrupt(0x2005, topology_interrupt);
288                 if (rc)
289                         goto out;
290                 ctl_set_bit(0, 8);
291         }
292         else
293                 set_topology_timer();
294 out:
295         update_cpu_core_map();
296         return rc;
297 }
298 __initcall(init_topology_update);
299
300 void __init s390_init_cpu_topology(void)
301 {
302         unsigned long long facility_bits;
303         struct tl_info *info;
304         struct core_info *core;
305         int nr_cores;
306         int i;
307
308         if (stfle(&facility_bits, 1) <= 0)
309                 return;
310         if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
311                 return;
312         machine_has_topology = 1;
313
314         if (facility_bits & (1ULL << 51))
315                 machine_has_topology_irq = 1;
316
317         tl_info = alloc_bootmem_pages(PAGE_SIZE);
318         info = tl_info;
319         stsi(info, 15, 1, 2);
320
321         nr_cores = info->mag[NR_MAG - 2];
322         for (i = 0; i < info->mnest - 2; i++)
323                 nr_cores *= info->mag[NR_MAG - 3 - i];
324
325         printk(KERN_INFO "CPU topology:");
326         for (i = 0; i < NR_MAG; i++)
327                 printk(" %d", info->mag[i]);
328         printk(" / %d\n", info->mnest);
329
330         core = &core_info;
331         for (i = 0; i < nr_cores; i++) {
332                 core->next = alloc_bootmem(sizeof(struct core_info));
333                 core = core->next;
334                 if (!core)
335                         goto error;
336         }
337         return;
338 error:
339         machine_has_topology = 0;
340         machine_has_topology_irq = 0;
341 }