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>
21 #define PTF_HORIZONTAL (0UL)
22 #define PTF_VERTICAL (1UL)
23 #define PTF_CHECK (2UL)
26 struct mask_info
*next
;
31 static int topology_enabled
= 1;
32 static void topology_work_fn(struct work_struct
*work
);
33 static struct sysinfo_15_1_x
*tl_info
;
34 static struct timer_list topology_timer
;
35 static void set_topology_timer(void);
36 static DECLARE_WORK(topology_work
, topology_work_fn
);
37 /* topology_lock protects the core linked list */
38 static DEFINE_SPINLOCK(topology_lock
);
40 static struct mask_info core_info
;
41 cpumask_t cpu_core_map
[NR_CPUS
];
42 unsigned char cpu_core_id
[NR_CPUS
];
44 #ifdef CONFIG_SCHED_BOOK
45 static struct mask_info book_info
;
46 cpumask_t cpu_book_map
[NR_CPUS
];
47 unsigned char cpu_book_id
[NR_CPUS
];
50 static cpumask_t
cpu_group_map(struct mask_info
*info
, unsigned int cpu
)
55 if (!topology_enabled
|| !MACHINE_HAS_TOPOLOGY
) {
56 cpumask_copy(&mask
, cpumask_of(cpu
));
60 if (cpumask_test_cpu(cpu
, &info
->mask
)) {
66 if (cpumask_empty(&mask
))
67 cpumask_copy(&mask
, cpumask_of(cpu
));
71 static void add_cpus_to_mask(struct topology_cpu
*tl_cpu
,
72 struct mask_info
*book
, struct mask_info
*core
)
76 for (cpu
= find_first_bit(&tl_cpu
->mask
[0], TOPOLOGY_CPU_BITS
);
77 cpu
< TOPOLOGY_CPU_BITS
;
78 cpu
= find_next_bit(&tl_cpu
->mask
[0], TOPOLOGY_CPU_BITS
, cpu
+ 1))
80 unsigned int rcpu
, lcpu
;
82 rcpu
= TOPOLOGY_CPU_BITS
- 1 - cpu
+ tl_cpu
->origin
;
83 for_each_present_cpu(lcpu
) {
84 if (cpu_logical_map(lcpu
) != rcpu
)
86 #ifdef CONFIG_SCHED_BOOK
87 cpumask_set_cpu(lcpu
, &book
->mask
);
88 cpu_book_id
[lcpu
] = book
->id
;
90 cpumask_set_cpu(lcpu
, &core
->mask
);
91 cpu_core_id
[lcpu
] = core
->id
;
92 smp_cpu_polarization
[lcpu
] = tl_cpu
->pp
;
97 static void clear_masks(void)
99 struct mask_info
*info
;
103 cpumask_clear(&info
->mask
);
106 #ifdef CONFIG_SCHED_BOOK
109 cpumask_clear(&info
->mask
);
115 static union topology_entry
*next_tle(union topology_entry
*tle
)
118 return (union topology_entry
*)((struct topology_cpu
*)tle
+ 1);
119 return (union topology_entry
*)((struct topology_container
*)tle
+ 1);
122 static void tl_to_cores(struct sysinfo_15_1_x
*info
)
124 #ifdef CONFIG_SCHED_BOOK
125 struct mask_info
*book
= &book_info
;
127 struct mask_info
*book
= NULL
;
129 struct mask_info
*core
= &core_info
;
130 union topology_entry
*tle
, *end
;
133 spin_lock_irq(&topology_lock
);
136 end
= (union topology_entry
*)((unsigned long)info
+ info
->length
);
139 #ifdef CONFIG_SCHED_BOOK
142 book
->id
= tle
->container
.id
;
147 core
->id
= tle
->container
.id
;
150 add_cpus_to_mask(&tle
->cpu
, book
, core
);
159 spin_unlock_irq(&topology_lock
);
162 static void topology_update_polarization_simple(void)
166 mutex_lock(&smp_cpu_state_mutex
);
167 for_each_possible_cpu(cpu
)
168 smp_cpu_polarization
[cpu
] = POLARIZATION_HRZ
;
169 mutex_unlock(&smp_cpu_state_mutex
);
172 static int ptf(unsigned long fc
)
177 " .insn rre,0xb9a20000,%1,%1\n"
185 int topology_set_cpu_management(int fc
)
190 if (!MACHINE_HAS_TOPOLOGY
)
193 rc
= ptf(PTF_VERTICAL
);
195 rc
= ptf(PTF_HORIZONTAL
);
198 for_each_possible_cpu(cpu
)
199 smp_cpu_polarization
[cpu
] = POLARIZATION_UNKNWN
;
203 static void update_cpu_core_map(void)
208 spin_lock_irqsave(&topology_lock
, flags
);
209 for_each_possible_cpu(cpu
) {
210 cpu_core_map
[cpu
] = cpu_group_map(&core_info
, cpu
);
211 #ifdef CONFIG_SCHED_BOOK
212 cpu_book_map
[cpu
] = cpu_group_map(&book_info
, cpu
);
215 spin_unlock_irqrestore(&topology_lock
, flags
);
218 void store_topology(struct sysinfo_15_1_x
*info
)
220 #ifdef CONFIG_SCHED_BOOK
223 rc
= stsi(info
, 15, 1, 3);
227 stsi(info
, 15, 1, 2);
230 int arch_update_cpu_topology(void)
232 struct sysinfo_15_1_x
*info
= tl_info
;
233 struct sys_device
*sysdev
;
236 if (!MACHINE_HAS_TOPOLOGY
) {
237 update_cpu_core_map();
238 topology_update_polarization_simple();
241 store_topology(info
);
243 update_cpu_core_map();
244 for_each_online_cpu(cpu
) {
245 sysdev
= get_cpu_sysdev(cpu
);
246 kobject_uevent(&sysdev
->kobj
, KOBJ_CHANGE
);
251 static void topology_work_fn(struct work_struct
*work
)
253 rebuild_sched_domains();
256 void topology_schedule_update(void)
258 schedule_work(&topology_work
);
261 static void topology_timer_fn(unsigned long ignored
)
264 topology_schedule_update();
265 set_topology_timer();
268 static void set_topology_timer(void)
270 topology_timer
.function
= topology_timer_fn
;
271 topology_timer
.data
= 0;
272 topology_timer
.expires
= jiffies
+ 60 * HZ
;
273 add_timer(&topology_timer
);
276 static int __init
early_parse_topology(char *p
)
278 if (strncmp(p
, "off", 3))
280 topology_enabled
= 0;
283 early_param("topology", early_parse_topology
);
285 static int __init
init_topology_update(void)
290 if (!MACHINE_HAS_TOPOLOGY
) {
291 topology_update_polarization_simple();
294 init_timer_deferrable(&topology_timer
);
295 set_topology_timer();
297 update_cpu_core_map();
300 __initcall(init_topology_update
);
302 static void alloc_masks(struct sysinfo_15_1_x
*info
, struct mask_info
*mask
,
307 nr_masks
= info
->mag
[TOPOLOGY_NR_MAG
- offset
];
308 for (i
= 0; i
< info
->mnest
- offset
; i
++)
309 nr_masks
*= info
->mag
[TOPOLOGY_NR_MAG
- offset
- 1 - i
];
310 nr_masks
= max(nr_masks
, 1);
311 for (i
= 0; i
< nr_masks
; i
++) {
312 mask
->next
= alloc_bootmem(sizeof(struct mask_info
));
317 void __init
s390_init_cpu_topology(void)
319 struct sysinfo_15_1_x
*info
;
322 if (!MACHINE_HAS_TOPOLOGY
)
324 tl_info
= alloc_bootmem_pages(PAGE_SIZE
);
326 store_topology(info
);
327 pr_info("The CPU configuration topology of the machine is:");
328 for (i
= 0; i
< TOPOLOGY_NR_MAG
; i
++)
329 printk(" %d", info
->mag
[i
]);
330 printk(" / %d\n", info
->mnest
);
331 alloc_masks(info
, &core_info
, 2);
332 #ifdef CONFIG_SCHED_BOOK
333 alloc_masks(info
, &book_info
, 3);