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);
69 cpumask_t cpu_core_map[NR_CPUS];
71 cpumask_t cpu_coregroup_map(unsigned int cpu)
73 struct core_info *core = &core_info;
77 if (!machine_has_topology)
78 return cpu_present_map;
79 mutex_lock(&smp_cpu_state_mutex);
81 if (cpu_isset(cpu, core->mask)) {
87 mutex_unlock(&smp_cpu_state_mutex);
89 mask = cpumask_of_cpu(cpu);
93 static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
97 for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
99 cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
101 unsigned int rcpu, lcpu;
103 rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
104 for_each_present_cpu(lcpu) {
105 if (__cpu_logical_map[lcpu] == rcpu) {
106 cpu_set(lcpu, core->mask);
107 smp_cpu_polarization[lcpu] = tl_cpu->pp;
113 static void clear_cores(void)
115 struct core_info *core = &core_info;
118 cpus_clear(core->mask);
123 static union tl_entry *next_tle(union tl_entry *tle)
126 return (union tl_entry *)((struct tl_container *)tle + 1);
128 return (union tl_entry *)((struct tl_cpu *)tle + 1);
131 static void tl_to_cores(struct tl_info *info)
133 union tl_entry *tle, *end;
134 struct core_info *core = &core_info;
136 mutex_lock(&smp_cpu_state_mutex);
139 end = (union tl_entry *)((unsigned long)info + info->length);
151 add_cpus_to_core(&tle->cpu, core);
155 machine_has_topology = 0;
160 mutex_unlock(&smp_cpu_state_mutex);
163 static void topology_update_polarization_simple(void)
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);
173 static int ptf(unsigned long fc)
178 " .insn rre,0xb9a20000,%1,%1\n"
186 int topology_set_cpu_management(int fc)
191 if (!machine_has_topology)
194 rc = ptf(PTF_VERTICAL);
196 rc = ptf(PTF_HORIZONTAL);
199 for_each_present_cpu(cpu)
200 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
204 static void update_cpu_core_map(void)
208 for_each_present_cpu(cpu)
209 cpu_core_map[cpu] = cpu_coregroup_map(cpu);
212 void arch_update_cpu_topology(void)
214 struct tl_info *info = tl_info;
215 struct sys_device *sysdev;
218 if (!machine_has_topology) {
219 update_cpu_core_map();
220 topology_update_polarization_simple();
223 stsi(info, 15, 1, 2);
225 update_cpu_core_map();
226 for_each_online_cpu(cpu) {
227 sysdev = get_cpu_sysdev(cpu);
228 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
232 static void topology_work_fn(struct work_struct *work)
234 arch_reinit_sched_domains();
237 void topology_schedule_update(void)
239 schedule_work(&topology_work);
242 static void topology_timer_fn(unsigned long ignored)
245 topology_schedule_update();
246 set_topology_timer();
249 static void set_topology_timer(void)
251 topology_timer.function = topology_timer_fn;
252 topology_timer.data = 0;
253 topology_timer.expires = jiffies + 60 * HZ;
254 add_timer(&topology_timer);
257 static void topology_interrupt(__u16 code)
259 schedule_work(&topology_work);
262 static int __init init_topology_update(void)
267 if (!machine_has_topology) {
268 topology_update_polarization_simple();
271 init_timer_deferrable(&topology_timer);
272 if (machine_has_topology_irq) {
273 rc = register_external_interrupt(0x2005, topology_interrupt);
279 set_topology_timer();
281 update_cpu_core_map();
284 __initcall(init_topology_update);
286 void __init s390_init_cpu_topology(void)
288 unsigned long long facility_bits;
289 struct tl_info *info;
290 struct core_info *core;
294 if (stfle(&facility_bits, 1) <= 0)
296 if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
298 machine_has_topology = 1;
300 if (facility_bits & (1ULL << 51))
301 machine_has_topology_irq = 1;
303 tl_info = alloc_bootmem_pages(PAGE_SIZE);
305 stsi(info, 15, 1, 2);
307 nr_cores = info->mag[NR_MAG - 2];
308 for (i = 0; i < info->mnest - 2; i++)
309 nr_cores *= info->mag[NR_MAG - 3 - i];
311 printk(KERN_INFO "CPU topology:");
312 for (i = 0; i < NR_MAG; i++)
313 printk(" %d", info->mag[i]);
314 printk(" / %d\n", info->mnest);
317 for (i = 0; i < nr_cores; i++) {
318 core->next = alloc_bootmem(sizeof(struct core_info));
325 machine_has_topology = 0;
326 machine_has_topology_irq = 0;