blob: 0601cd3231e4c038e61bb4f9b623992ebe3ddec7 [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);
Heiko Carstens74af2832008-11-14 18:18:07 +010068/* topology_lock protects the core linked list */
69static DEFINE_SPINLOCK(topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020070
Heiko Carstensd00aa4e2008-04-30 13:38:40 +020071cpumask_t cpu_core_map[NR_CPUS];
72
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020073cpumask_t cpu_coregroup_map(unsigned int cpu)
74{
75 struct core_info *core = &core_info;
Heiko Carstens74af2832008-11-14 18:18:07 +010076 unsigned long flags;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020077 cpumask_t mask;
78
79 cpus_clear(mask);
80 if (!machine_has_topology)
81 return cpu_present_map;
Heiko Carstens74af2832008-11-14 18:18:07 +010082 spin_lock_irqsave(&topology_lock, flags);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020083 while (core) {
84 if (cpu_isset(cpu, core->mask)) {
85 mask = core->mask;
86 break;
87 }
88 core = core->next;
89 }
Heiko Carstens74af2832008-11-14 18:18:07 +010090 spin_unlock_irqrestore(&topology_lock, flags);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020091 if (cpus_empty(mask))
92 mask = cpumask_of_cpu(cpu);
93 return mask;
94}
95
Rusty Russell9be3eec2008-12-26 22:23:42 +103096const struct cpumask *cpu_coregroup_mask(unsigned int cpu)
97{
98 return &cpu_core_map[cpu];
99}
100
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200101static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
102{
103 unsigned int cpu;
104
105 for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
106 cpu < CPU_BITS;
107 cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
108 {
109 unsigned int rcpu, lcpu;
110
111 rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
112 for_each_present_cpu(lcpu) {
Heiko Carstensc10fde02008-04-17 07:46:13 +0200113 if (__cpu_logical_map[lcpu] == rcpu) {
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200114 cpu_set(lcpu, core->mask);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200115 smp_cpu_polarization[lcpu] = tl_cpu->pp;
116 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200117 }
118 }
119}
120
121static void clear_cores(void)
122{
123 struct core_info *core = &core_info;
124
125 while (core) {
126 cpus_clear(core->mask);
127 core = core->next;
128 }
129}
130
131static union tl_entry *next_tle(union tl_entry *tle)
132{
133 if (tle->nl)
134 return (union tl_entry *)((struct tl_container *)tle + 1);
135 else
136 return (union tl_entry *)((struct tl_cpu *)tle + 1);
137}
138
139static void tl_to_cores(struct tl_info *info)
140{
141 union tl_entry *tle, *end;
142 struct core_info *core = &core_info;
143
Heiko Carstens74af2832008-11-14 18:18:07 +0100144 spin_lock_irq(&topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200145 clear_cores();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200146 tle = info->tle;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200147 end = (union tl_entry *)((unsigned long)info + info->length);
148 while (tle < end) {
149 switch (tle->nl) {
150 case 5:
151 case 4:
152 case 3:
153 case 2:
154 break;
155 case 1:
156 core = core->next;
157 break;
158 case 0:
159 add_cpus_to_core(&tle->cpu, core);
160 break;
161 default:
162 clear_cores();
163 machine_has_topology = 0;
164 return;
165 }
166 tle = next_tle(tle);
167 }
Heiko Carstens74af2832008-11-14 18:18:07 +0100168 spin_unlock_irq(&topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200169}
170
Heiko Carstensc10fde02008-04-17 07:46:13 +0200171static void topology_update_polarization_simple(void)
172{
173 int cpu;
174
175 mutex_lock(&smp_cpu_state_mutex);
176 for_each_present_cpu(cpu)
177 smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
178 mutex_unlock(&smp_cpu_state_mutex);
179}
180
181static int ptf(unsigned long fc)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200182{
183 int rc;
184
185 asm volatile(
186 " .insn rre,0xb9a20000,%1,%1\n"
187 " ipm %0\n"
188 " srl %0,28\n"
189 : "=d" (rc)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200190 : "d" (fc) : "cc");
191 return rc;
192}
193
194int topology_set_cpu_management(int fc)
195{
196 int cpu;
197 int rc;
198
199 if (!machine_has_topology)
200 return -EOPNOTSUPP;
201 if (fc)
202 rc = ptf(PTF_VERTICAL);
203 else
204 rc = ptf(PTF_HORIZONTAL);
205 if (rc)
206 return -EBUSY;
207 for_each_present_cpu(cpu)
208 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200209 return rc;
210}
211
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200212static void update_cpu_core_map(void)
213{
214 int cpu;
215
216 for_each_present_cpu(cpu)
217 cpu_core_map[cpu] = cpu_coregroup_map(cpu);
218}
219
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200220void arch_update_cpu_topology(void)
221{
222 struct tl_info *info = tl_info;
223 struct sys_device *sysdev;
224 int cpu;
225
Heiko Carstensc10fde02008-04-17 07:46:13 +0200226 if (!machine_has_topology) {
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200227 update_cpu_core_map();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200228 topology_update_polarization_simple();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200229 return;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200230 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200231 stsi(info, 15, 1, 2);
232 tl_to_cores(info);
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200233 update_cpu_core_map();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200234 for_each_online_cpu(cpu) {
235 sysdev = get_cpu_sysdev(cpu);
236 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
237 }
238}
239
Heiko Carstensfd781fa2008-04-30 13:38:41 +0200240static void topology_work_fn(struct work_struct *work)
241{
Oleg Nesterov69b895f2008-07-25 01:47:51 -0700242 arch_reinit_sched_domains();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200243}
244
Heiko Carstensc10fde02008-04-17 07:46:13 +0200245void topology_schedule_update(void)
246{
247 schedule_work(&topology_work);
248}
249
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200250static void topology_timer_fn(unsigned long ignored)
251{
Heiko Carstensc10fde02008-04-17 07:46:13 +0200252 if (ptf(PTF_CHECK))
253 topology_schedule_update();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200254 set_topology_timer();
255}
256
257static void set_topology_timer(void)
258{
259 topology_timer.function = topology_timer_fn;
260 topology_timer.data = 0;
261 topology_timer.expires = jiffies + 60 * HZ;
262 add_timer(&topology_timer);
263}
264
265static void topology_interrupt(__u16 code)
266{
267 schedule_work(&topology_work);
268}
269
270static int __init init_topology_update(void)
271{
272 int rc;
273
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200274 rc = 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200275 if (!machine_has_topology) {
276 topology_update_polarization_simple();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200277 goto out;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200278 }
279 init_timer_deferrable(&topology_timer);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200280 if (machine_has_topology_irq) {
281 rc = register_external_interrupt(0x2005, topology_interrupt);
282 if (rc)
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200283 goto out;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200284 ctl_set_bit(0, 8);
285 }
286 else
287 set_topology_timer();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200288out:
289 update_cpu_core_map();
290 return rc;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200291}
292__initcall(init_topology_update);
293
294void __init s390_init_cpu_topology(void)
295{
296 unsigned long long facility_bits;
297 struct tl_info *info;
298 struct core_info *core;
299 int nr_cores;
300 int i;
301
302 if (stfle(&facility_bits, 1) <= 0)
303 return;
304 if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
305 return;
306 machine_has_topology = 1;
307
308 if (facility_bits & (1ULL << 51))
309 machine_has_topology_irq = 1;
310
311 tl_info = alloc_bootmem_pages(PAGE_SIZE);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200312 info = tl_info;
313 stsi(info, 15, 1, 2);
314
315 nr_cores = info->mag[NR_MAG - 2];
316 for (i = 0; i < info->mnest - 2; i++)
317 nr_cores *= info->mag[NR_MAG - 3 - i];
318
319 printk(KERN_INFO "CPU topology:");
320 for (i = 0; i < NR_MAG; i++)
321 printk(" %d", info->mag[i]);
322 printk(" / %d\n", info->mnest);
323
324 core = &core_info;
325 for (i = 0; i < nr_cores; i++) {
326 core->next = alloc_bootmem(sizeof(struct core_info));
327 core = core->next;
328 if (!core)
329 goto error;
330 }
331 return;
332error:
333 machine_has_topology = 0;
334 machine_has_topology_irq = 0;
335}