2 * Copyright IBM Corp. 2007
3 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
6 #include <linux/kernel.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>
22 #define PTF_HORIZONTAL (0UL)
23 #define PTF_VERTICAL (1UL)
24 #define PTF_CHECK (2UL)
27 unsigned char reserved0
[4];
30 unsigned char reserved1
;
31 unsigned short origin
;
32 unsigned long mask
[CPU_BITS
/ BITS_PER_LONG
];
36 unsigned char reserved
[8];
42 struct tl_container container
;
46 unsigned char reserved0
[2];
47 unsigned short length
;
48 unsigned char mag
[NR_MAG
];
49 unsigned char reserved1
;
51 unsigned char reserved2
[4];
52 union tl_entry tle
[0];
56 struct core_info
*next
;
60 static void topology_work_fn(struct work_struct
*work
);
61 static struct tl_info
*tl_info
;
62 static struct core_info core_info
;
63 static int machine_has_topology
;
64 static int machine_has_topology_irq
;
65 static struct timer_list topology_timer
;
66 static void set_topology_timer(void);
67 static DECLARE_WORK(topology_work
, topology_work_fn
);
68 /* topology_lock protects the core linked list */
69 static DEFINE_SPINLOCK(topology_lock
);
71 cpumask_t cpu_core_map
[NR_CPUS
];
73 cpumask_t
cpu_coregroup_map(unsigned int cpu
)
75 struct core_info
*core
= &core_info
;
80 if (!machine_has_topology
)
81 return cpu_present_map
;
82 spin_lock_irqsave(&topology_lock
, flags
);
84 if (cpu_isset(cpu
, core
->mask
)) {
90 spin_unlock_irqrestore(&topology_lock
, flags
);
92 mask
= cpumask_of_cpu(cpu
);
96 static void add_cpus_to_core(struct tl_cpu
*tl_cpu
, struct core_info
*core
)
100 for (cpu
= find_first_bit(&tl_cpu
->mask
[0], CPU_BITS
);
102 cpu
= find_next_bit(&tl_cpu
->mask
[0], CPU_BITS
, cpu
+ 1))
104 unsigned int rcpu
, lcpu
;
106 rcpu
= CPU_BITS
- 1 - cpu
+ tl_cpu
->origin
;
107 for_each_present_cpu(lcpu
) {
108 if (__cpu_logical_map
[lcpu
] == rcpu
) {
109 cpu_set(lcpu
, core
->mask
);
110 smp_cpu_polarization
[lcpu
] = tl_cpu
->pp
;
116 static void clear_cores(void)
118 struct core_info
*core
= &core_info
;
121 cpus_clear(core
->mask
);
126 static union tl_entry
*next_tle(union tl_entry
*tle
)
129 return (union tl_entry
*)((struct tl_container
*)tle
+ 1);
131 return (union tl_entry
*)((struct tl_cpu
*)tle
+ 1);
134 static void tl_to_cores(struct tl_info
*info
)
136 union tl_entry
*tle
, *end
;
137 struct core_info
*core
= &core_info
;
139 spin_lock_irq(&topology_lock
);
142 end
= (union tl_entry
*)((unsigned long)info
+ info
->length
);
154 add_cpus_to_core(&tle
->cpu
, core
);
158 machine_has_topology
= 0;
163 spin_unlock_irq(&topology_lock
);
166 static void topology_update_polarization_simple(void)
170 mutex_lock(&smp_cpu_state_mutex
);
171 for_each_present_cpu(cpu
)
172 smp_cpu_polarization
[cpu
] = POLARIZATION_HRZ
;
173 mutex_unlock(&smp_cpu_state_mutex
);
176 static int ptf(unsigned long fc
)
181 " .insn rre,0xb9a20000,%1,%1\n"
189 int topology_set_cpu_management(int fc
)
194 if (!machine_has_topology
)
197 rc
= ptf(PTF_VERTICAL
);
199 rc
= ptf(PTF_HORIZONTAL
);
202 for_each_present_cpu(cpu
)
203 smp_cpu_polarization
[cpu
] = POLARIZATION_UNKNWN
;
207 static void update_cpu_core_map(void)
211 for_each_present_cpu(cpu
)
212 cpu_core_map
[cpu
] = cpu_coregroup_map(cpu
);
215 void arch_update_cpu_topology(void)
217 struct tl_info
*info
= tl_info
;
218 struct sys_device
*sysdev
;
221 if (!machine_has_topology
) {
222 update_cpu_core_map();
223 topology_update_polarization_simple();
226 stsi(info
, 15, 1, 2);
228 update_cpu_core_map();
229 for_each_online_cpu(cpu
) {
230 sysdev
= get_cpu_sysdev(cpu
);
231 kobject_uevent(&sysdev
->kobj
, KOBJ_CHANGE
);
235 static void topology_work_fn(struct work_struct
*work
)
237 arch_reinit_sched_domains();
240 void topology_schedule_update(void)
242 schedule_work(&topology_work
);
245 static void topology_timer_fn(unsigned long ignored
)
248 topology_schedule_update();
249 set_topology_timer();
252 static void set_topology_timer(void)
254 topology_timer
.function
= topology_timer_fn
;
255 topology_timer
.data
= 0;
256 topology_timer
.expires
= jiffies
+ 60 * HZ
;
257 add_timer(&topology_timer
);
260 static void topology_interrupt(__u16 code
)
262 schedule_work(&topology_work
);
265 static int __init
init_topology_update(void)
270 if (!machine_has_topology
) {
271 topology_update_polarization_simple();
274 init_timer_deferrable(&topology_timer
);
275 if (machine_has_topology_irq
) {
276 rc
= register_external_interrupt(0x2005, topology_interrupt
);
282 set_topology_timer();
284 update_cpu_core_map();
287 __initcall(init_topology_update
);
289 void __init
s390_init_cpu_topology(void)
291 unsigned long long facility_bits
;
292 struct tl_info
*info
;
293 struct core_info
*core
;
297 if (stfle(&facility_bits
, 1) <= 0)
299 if (!(facility_bits
& (1ULL << 52)) || !(facility_bits
& (1ULL << 61)))
301 machine_has_topology
= 1;
303 if (facility_bits
& (1ULL << 51))
304 machine_has_topology_irq
= 1;
306 tl_info
= alloc_bootmem_pages(PAGE_SIZE
);
308 stsi(info
, 15, 1, 2);
310 nr_cores
= info
->mag
[NR_MAG
- 2];
311 for (i
= 0; i
< info
->mnest
- 2; i
++)
312 nr_cores
*= info
->mag
[NR_MAG
- 3 - i
];
314 printk(KERN_INFO
"CPU topology:");
315 for (i
= 0; i
< NR_MAG
; i
++)
316 printk(" %d", info
->mag
[i
]);
317 printk(" / %d\n", info
->mnest
);
320 for (i
= 0; i
< nr_cores
; i
++) {
321 core
->next
= alloc_bootmem(sizeof(struct core_info
));
328 machine_has_topology
= 0;
329 machine_has_topology_irq
= 0;