2 * Copyright IBM Corp. 2007, 2011
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/workqueue.h>
10 #include <linux/bootmem.h>
11 #include <linux/cpuset.h>
12 #include <linux/device.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/cpu.h>
20 #include <linux/smp.h>
22 #include <linux/nodemask.h>
23 #include <linux/node.h>
24 #include <asm/sysinfo.h>
27 #define PTF_HORIZONTAL (0UL)
28 #define PTF_VERTICAL (1UL)
29 #define PTF_CHECK (2UL)
32 struct mask_info
*next
;
37 static void set_topology_timer(void);
38 static void topology_work_fn(struct work_struct
*work
);
39 static struct sysinfo_15_1_x
*tl_info
;
41 static bool topology_enabled
= true;
42 static DECLARE_WORK(topology_work
, topology_work_fn
);
45 * Socket/Book linked lists and cpu_topology updates are
46 * protected by "sched_domains_mutex".
48 static struct mask_info socket_info
;
49 static struct mask_info book_info
;
50 static struct mask_info drawer_info
;
52 struct cpu_topology_s390 cpu_topology
[NR_CPUS
];
53 EXPORT_SYMBOL_GPL(cpu_topology
);
55 cpumask_t cpus_with_topology
;
57 static cpumask_t
cpu_group_map(struct mask_info
*info
, unsigned int cpu
)
61 cpumask_copy(&mask
, cpumask_of(cpu
));
62 if (!topology_enabled
|| !MACHINE_HAS_TOPOLOGY
)
64 for (; info
; info
= info
->next
) {
65 if (cpumask_test_cpu(cpu
, &info
->mask
))
71 static cpumask_t
cpu_thread_map(unsigned int cpu
)
76 cpumask_copy(&mask
, cpumask_of(cpu
));
77 if (!topology_enabled
|| !MACHINE_HAS_TOPOLOGY
)
79 cpu
-= cpu
% (smp_cpu_mtid
+ 1);
80 for (i
= 0; i
<= smp_cpu_mtid
; i
++)
81 if (cpu_present(cpu
+ i
))
82 cpumask_set_cpu(cpu
+ i
, &mask
);
86 static void add_cpus_to_mask(struct topology_core
*tl_core
,
87 struct mask_info
*drawer
,
88 struct mask_info
*book
,
89 struct mask_info
*socket
)
91 struct cpu_topology_s390
*topo
;
94 for_each_set_bit(core
, &tl_core
->mask
[0], TOPOLOGY_CORE_BITS
) {
98 rcore
= TOPOLOGY_CORE_BITS
- 1 - core
+ tl_core
->origin
;
99 lcpu
= smp_find_processor_id(rcore
<< smp_cpu_mt_shift
);
102 for (i
= 0; i
<= smp_cpu_mtid
; i
++) {
103 topo
= &cpu_topology
[lcpu
+ i
];
104 topo
->drawer_id
= drawer
->id
;
105 topo
->book_id
= book
->id
;
106 topo
->socket_id
= socket
->id
;
107 topo
->core_id
= rcore
;
108 topo
->thread_id
= lcpu
+ i
;
109 cpumask_set_cpu(lcpu
+ i
, &drawer
->mask
);
110 cpumask_set_cpu(lcpu
+ i
, &book
->mask
);
111 cpumask_set_cpu(lcpu
+ i
, &socket
->mask
);
112 cpumask_set_cpu(lcpu
+ i
, &cpus_with_topology
);
113 smp_cpu_set_polarization(lcpu
+ i
, tl_core
->pp
);
118 static void clear_masks(void)
120 struct mask_info
*info
;
124 cpumask_clear(&info
->mask
);
129 cpumask_clear(&info
->mask
);
134 cpumask_clear(&info
->mask
);
139 static union topology_entry
*next_tle(union topology_entry
*tle
)
142 return (union topology_entry
*)((struct topology_core
*)tle
+ 1);
143 return (union topology_entry
*)((struct topology_container
*)tle
+ 1);
146 static void tl_to_masks(struct sysinfo_15_1_x
*info
)
148 struct mask_info
*socket
= &socket_info
;
149 struct mask_info
*book
= &book_info
;
150 struct mask_info
*drawer
= &drawer_info
;
151 union topology_entry
*tle
, *end
;
155 end
= (union topology_entry
*)((unsigned long)info
+ info
->length
);
159 drawer
= drawer
->next
;
160 drawer
->id
= tle
->container
.id
;
164 book
->id
= tle
->container
.id
;
167 socket
= socket
->next
;
168 socket
->id
= tle
->container
.id
;
171 add_cpus_to_mask(&tle
->cpu
, drawer
, book
, socket
);
181 static void topology_update_polarization_simple(void)
185 mutex_lock(&smp_cpu_state_mutex
);
186 for_each_possible_cpu(cpu
)
187 smp_cpu_set_polarization(cpu
, POLARIZATION_HRZ
);
188 mutex_unlock(&smp_cpu_state_mutex
);
191 static int ptf(unsigned long fc
)
196 " .insn rre,0xb9a20000,%1,%1\n"
204 int topology_set_cpu_management(int fc
)
208 if (!MACHINE_HAS_TOPOLOGY
)
211 rc
= ptf(PTF_VERTICAL
);
213 rc
= ptf(PTF_HORIZONTAL
);
216 for_each_possible_cpu(cpu
)
217 smp_cpu_set_polarization(cpu
, POLARIZATION_UNKNOWN
);
221 static void update_cpu_masks(void)
223 struct cpu_topology_s390
*topo
;
226 for_each_possible_cpu(cpu
) {
227 topo
= &cpu_topology
[cpu
];
228 topo
->thread_mask
= cpu_thread_map(cpu
);
229 topo
->core_mask
= cpu_group_map(&socket_info
, cpu
);
230 topo
->book_mask
= cpu_group_map(&book_info
, cpu
);
231 topo
->drawer_mask
= cpu_group_map(&drawer_info
, cpu
);
232 if (!MACHINE_HAS_TOPOLOGY
) {
233 topo
->thread_id
= cpu
;
235 topo
->socket_id
= cpu
;
237 topo
->drawer_id
= cpu
;
238 if (cpu_present(cpu
))
239 cpumask_set_cpu(cpu
, &cpus_with_topology
);
242 numa_update_cpu_topology();
245 void store_topology(struct sysinfo_15_1_x
*info
)
247 stsi(info
, 15, 1, min(topology_max_mnest
, 4));
250 static int __arch_update_cpu_topology(void)
252 struct sysinfo_15_1_x
*info
= tl_info
;
255 cpumask_clear(&cpus_with_topology
);
256 if (MACHINE_HAS_TOPOLOGY
) {
258 store_topology(info
);
262 if (!MACHINE_HAS_TOPOLOGY
)
263 topology_update_polarization_simple();
267 int arch_update_cpu_topology(void)
272 rc
= __arch_update_cpu_topology();
273 for_each_online_cpu(cpu
) {
274 dev
= get_cpu_device(cpu
);
275 kobject_uevent(&dev
->kobj
, KOBJ_CHANGE
);
280 static void topology_work_fn(struct work_struct
*work
)
282 rebuild_sched_domains();
285 void topology_schedule_update(void)
287 schedule_work(&topology_work
);
290 static void topology_timer_fn(unsigned long ignored
)
293 topology_schedule_update();
294 set_topology_timer();
297 static struct timer_list topology_timer
=
298 TIMER_DEFERRED_INITIALIZER(topology_timer_fn
, 0, 0);
300 static atomic_t topology_poll
= ATOMIC_INIT(0);
302 static void set_topology_timer(void)
304 if (atomic_add_unless(&topology_poll
, -1, 0))
305 mod_timer(&topology_timer
, jiffies
+ HZ
/ 10);
307 mod_timer(&topology_timer
, jiffies
+ HZ
* 60);
310 void topology_expect_change(void)
312 if (!MACHINE_HAS_TOPOLOGY
)
314 /* This is racy, but it doesn't matter since it is just a heuristic.
315 * Worst case is that we poll in a higher frequency for a bit longer.
317 if (atomic_read(&topology_poll
) > 60)
319 atomic_add(60, &topology_poll
);
320 set_topology_timer();
323 static int cpu_management
;
325 static ssize_t
dispatching_show(struct device
*dev
,
326 struct device_attribute
*attr
,
331 mutex_lock(&smp_cpu_state_mutex
);
332 count
= sprintf(buf
, "%d\n", cpu_management
);
333 mutex_unlock(&smp_cpu_state_mutex
);
337 static ssize_t
dispatching_store(struct device
*dev
,
338 struct device_attribute
*attr
,
345 if (sscanf(buf
, "%d %c", &val
, &delim
) != 1)
347 if (val
!= 0 && val
!= 1)
351 mutex_lock(&smp_cpu_state_mutex
);
352 if (cpu_management
== val
)
354 rc
= topology_set_cpu_management(val
);
357 cpu_management
= val
;
358 topology_expect_change();
360 mutex_unlock(&smp_cpu_state_mutex
);
362 return rc
? rc
: count
;
364 static DEVICE_ATTR(dispatching
, 0644, dispatching_show
,
367 static ssize_t
cpu_polarization_show(struct device
*dev
,
368 struct device_attribute
*attr
, char *buf
)
373 mutex_lock(&smp_cpu_state_mutex
);
374 switch (smp_cpu_get_polarization(cpu
)) {
375 case POLARIZATION_HRZ
:
376 count
= sprintf(buf
, "horizontal\n");
378 case POLARIZATION_VL
:
379 count
= sprintf(buf
, "vertical:low\n");
381 case POLARIZATION_VM
:
382 count
= sprintf(buf
, "vertical:medium\n");
384 case POLARIZATION_VH
:
385 count
= sprintf(buf
, "vertical:high\n");
388 count
= sprintf(buf
, "unknown\n");
391 mutex_unlock(&smp_cpu_state_mutex
);
394 static DEVICE_ATTR(polarization
, 0444, cpu_polarization_show
, NULL
);
396 static struct attribute
*topology_cpu_attrs
[] = {
397 &dev_attr_polarization
.attr
,
401 static struct attribute_group topology_cpu_attr_group
= {
402 .attrs
= topology_cpu_attrs
,
405 int topology_cpu_init(struct cpu
*cpu
)
407 return sysfs_create_group(&cpu
->dev
.kobj
, &topology_cpu_attr_group
);
410 static const struct cpumask
*cpu_thread_mask(int cpu
)
412 return &cpu_topology
[cpu
].thread_mask
;
416 const struct cpumask
*cpu_coregroup_mask(int cpu
)
418 return &cpu_topology
[cpu
].core_mask
;
421 static const struct cpumask
*cpu_book_mask(int cpu
)
423 return &cpu_topology
[cpu
].book_mask
;
426 static const struct cpumask
*cpu_drawer_mask(int cpu
)
428 return &cpu_topology
[cpu
].drawer_mask
;
431 static int __init
early_parse_topology(char *p
)
433 return kstrtobool(p
, &topology_enabled
);
435 early_param("topology", early_parse_topology
);
437 static struct sched_domain_topology_level s390_topology
[] = {
438 { cpu_thread_mask
, cpu_smt_flags
, SD_INIT_NAME(SMT
) },
439 { cpu_coregroup_mask
, cpu_core_flags
, SD_INIT_NAME(MC
) },
440 { cpu_book_mask
, SD_INIT_NAME(BOOK
) },
441 { cpu_drawer_mask
, SD_INIT_NAME(DRAWER
) },
442 { cpu_cpu_mask
, SD_INIT_NAME(DIE
) },
446 static void __init
alloc_masks(struct sysinfo_15_1_x
*info
,
447 struct mask_info
*mask
, int offset
)
451 nr_masks
= info
->mag
[TOPOLOGY_NR_MAG
- offset
];
452 for (i
= 0; i
< info
->mnest
- offset
; i
++)
453 nr_masks
*= info
->mag
[TOPOLOGY_NR_MAG
- offset
- 1 - i
];
454 nr_masks
= max(nr_masks
, 1);
455 for (i
= 0; i
< nr_masks
; i
++) {
456 mask
->next
= memblock_virt_alloc(sizeof(*mask
->next
), 8);
461 void __init
topology_init_early(void)
463 struct sysinfo_15_1_x
*info
;
466 set_sched_topology(s390_topology
);
467 if (!MACHINE_HAS_TOPOLOGY
)
469 tl_info
= memblock_virt_alloc(sizeof(*tl_info
), PAGE_SIZE
);
471 store_topology(info
);
472 pr_info("The CPU configuration topology of the machine is:");
473 for (i
= 0; i
< TOPOLOGY_NR_MAG
; i
++)
474 printk(KERN_CONT
" %d", info
->mag
[i
]);
475 printk(KERN_CONT
" / %d\n", info
->mnest
);
476 alloc_masks(info
, &socket_info
, 1);
477 alloc_masks(info
, &book_info
, 2);
478 alloc_masks(info
, &drawer_info
, 3);
480 __arch_update_cpu_topology();
483 static int __init
topology_init(void)
485 if (MACHINE_HAS_TOPOLOGY
)
486 set_topology_timer();
488 topology_update_polarization_simple();
489 return device_create_file(cpu_subsys
.dev_root
, &dev_attr_dispatching
);
491 device_initcall(topology_init
);