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