2 * Copyright IBM Corp. 2007
3 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
6 #define KMSG_COMPONENT "cpu"
7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9 #include <linux/kernel.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>
22 #define PTF_HORIZONTAL (0UL)
23 #define PTF_VERTICAL (1UL)
24 #define PTF_CHECK (2UL)
27 struct mask_info
*next
;
32 static int topology_enabled
= 1;
33 static void topology_work_fn(struct work_struct
*work
);
34 static struct sysinfo_15_1_x
*tl_info
;
35 static struct timer_list topology_timer
;
36 static void set_topology_timer(void);
37 static DECLARE_WORK(topology_work
, topology_work_fn
);
38 /* topology_lock protects the core linked list */
39 static DEFINE_SPINLOCK(topology_lock
);
41 static struct mask_info core_info
;
42 cpumask_t cpu_core_map
[NR_CPUS
];
43 unsigned char cpu_core_id
[NR_CPUS
];
45 #ifdef CONFIG_SCHED_BOOK
46 static struct mask_info book_info
;
47 cpumask_t cpu_book_map
[NR_CPUS
];
48 unsigned char cpu_book_id
[NR_CPUS
];
51 static cpumask_t
cpu_group_map(struct mask_info
*info
, unsigned int cpu
)
56 if (!topology_enabled
|| !MACHINE_HAS_TOPOLOGY
) {
57 cpumask_copy(&mask
, cpumask_of(cpu
));
61 if (cpu_isset(cpu
, info
->mask
)) {
68 mask
= cpumask_of_cpu(cpu
);
72 static void add_cpus_to_mask(struct topology_cpu
*tl_cpu
,
73 struct mask_info
*book
, struct mask_info
*core
)
77 for (cpu
= find_first_bit(&tl_cpu
->mask
[0], TOPOLOGY_CPU_BITS
);
78 cpu
< TOPOLOGY_CPU_BITS
;
79 cpu
= find_next_bit(&tl_cpu
->mask
[0], TOPOLOGY_CPU_BITS
, cpu
+ 1))
81 unsigned int rcpu
, lcpu
;
83 rcpu
= TOPOLOGY_CPU_BITS
- 1 - cpu
+ tl_cpu
->origin
;
84 for_each_present_cpu(lcpu
) {
85 if (cpu_logical_map(lcpu
) != rcpu
)
87 #ifdef CONFIG_SCHED_BOOK
88 cpu_set(lcpu
, book
->mask
);
89 cpu_book_id
[lcpu
] = book
->id
;
91 cpu_set(lcpu
, core
->mask
);
92 cpu_core_id
[lcpu
] = core
->id
;
93 smp_cpu_polarization
[lcpu
] = tl_cpu
->pp
;
98 static void clear_masks(void)
100 struct mask_info
*info
;
104 cpus_clear(info
->mask
);
107 #ifdef CONFIG_SCHED_BOOK
110 cpus_clear(info
->mask
);
116 static union topology_entry
*next_tle(union topology_entry
*tle
)
119 return (union topology_entry
*)((struct topology_cpu
*)tle
+ 1);
120 return (union topology_entry
*)((struct topology_container
*)tle
+ 1);
123 static void tl_to_cores(struct sysinfo_15_1_x
*info
)
125 #ifdef CONFIG_SCHED_BOOK
126 struct mask_info
*book
= &book_info
;
128 struct mask_info
*book
= NULL
;
130 struct mask_info
*core
= &core_info
;
131 union topology_entry
*tle
, *end
;
134 spin_lock_irq(&topology_lock
);
137 end
= (union topology_entry
*)((unsigned long)info
+ info
->length
);
140 #ifdef CONFIG_SCHED_BOOK
143 book
->id
= tle
->container
.id
;
148 core
->id
= tle
->container
.id
;
151 add_cpus_to_mask(&tle
->cpu
, book
, core
);
160 spin_unlock_irq(&topology_lock
);
163 static void topology_update_polarization_simple(void)
167 mutex_lock(&smp_cpu_state_mutex
);
168 for_each_possible_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_possible_cpu(cpu
)
200 smp_cpu_polarization
[cpu
] = POLARIZATION_UNKNWN
;
204 static void update_cpu_core_map(void)
209 spin_lock_irqsave(&topology_lock
, flags
);
210 for_each_possible_cpu(cpu
) {
211 cpu_core_map
[cpu
] = cpu_group_map(&core_info
, cpu
);
212 #ifdef CONFIG_SCHED_BOOK
213 cpu_book_map
[cpu
] = cpu_group_map(&book_info
, cpu
);
216 spin_unlock_irqrestore(&topology_lock
, flags
);
219 void store_topology(struct sysinfo_15_1_x
*info
)
221 #ifdef CONFIG_SCHED_BOOK
224 rc
= stsi(info
, 15, 1, 3);
228 stsi(info
, 15, 1, 2);
231 int arch_update_cpu_topology(void)
233 struct sysinfo_15_1_x
*info
= tl_info
;
234 struct sys_device
*sysdev
;
237 if (!MACHINE_HAS_TOPOLOGY
) {
238 update_cpu_core_map();
239 topology_update_polarization_simple();
242 store_topology(info
);
244 update_cpu_core_map();
245 for_each_online_cpu(cpu
) {
246 sysdev
= get_cpu_sysdev(cpu
);
247 kobject_uevent(&sysdev
->kobj
, KOBJ_CHANGE
);
252 static void topology_work_fn(struct work_struct
*work
)
254 rebuild_sched_domains();
257 void topology_schedule_update(void)
259 schedule_work(&topology_work
);
262 static void topology_timer_fn(unsigned long ignored
)
265 topology_schedule_update();
266 set_topology_timer();
269 static void set_topology_timer(void)
271 topology_timer
.function
= topology_timer_fn
;
272 topology_timer
.data
= 0;
273 topology_timer
.expires
= jiffies
+ 60 * HZ
;
274 add_timer(&topology_timer
);
277 static int __init
early_parse_topology(char *p
)
279 if (strncmp(p
, "off", 3))
281 topology_enabled
= 0;
284 early_param("topology", early_parse_topology
);
286 static int __init
init_topology_update(void)
291 if (!MACHINE_HAS_TOPOLOGY
) {
292 topology_update_polarization_simple();
295 init_timer_deferrable(&topology_timer
);
296 set_topology_timer();
298 update_cpu_core_map();
301 __initcall(init_topology_update
);
303 static void alloc_masks(struct sysinfo_15_1_x
*info
, struct mask_info
*mask
,
308 nr_masks
= info
->mag
[TOPOLOGY_NR_MAG
- offset
];
309 for (i
= 0; i
< info
->mnest
- offset
; i
++)
310 nr_masks
*= info
->mag
[TOPOLOGY_NR_MAG
- offset
- 1 - i
];
311 nr_masks
= max(nr_masks
, 1);
312 for (i
= 0; i
< nr_masks
; i
++) {
313 mask
->next
= alloc_bootmem(sizeof(struct mask_info
));
318 void __init
s390_init_cpu_topology(void)
320 struct sysinfo_15_1_x
*info
;
323 if (!MACHINE_HAS_TOPOLOGY
)
325 tl_info
= alloc_bootmem_pages(PAGE_SIZE
);
327 store_topology(info
);
328 pr_info("The CPU configuration topology of the machine is:");
329 for (i
= 0; i
< TOPOLOGY_NR_MAG
; i
++)
330 printk(" %d", info
->mag
[i
]);
331 printk(" / %d\n", info
->mnest
);
332 alloc_masks(info
, &core_info
, 2);
333 #ifdef CONFIG_SCHED_BOOK
334 alloc_masks(info
, &book_info
, 3);