1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2007, 2011
6 #define KMSG_COMPONENT "cpu"
7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9 #include <linux/cpufeature.h>
10 #include <linux/workqueue.h>
11 #include <linux/memblock.h>
12 #include <linux/uaccess.h>
13 #include <linux/sysctl.h>
14 #include <linux/cpuset.h>
15 #include <linux/device.h>
16 #include <linux/export.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/sched/topology.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/cpu.h>
24 #include <linux/smp.h>
26 #include <linux/nodemask.h>
27 #include <linux/node.h>
28 #include <asm/hiperdispatch.h>
29 #include <asm/sysinfo.h>
32 #define PTF_HORIZONTAL (0UL)
33 #define PTF_VERTICAL (1UL)
34 #define PTF_CHECK (2UL)
39 TOPOLOGY_MODE_PACKAGE,
40 TOPOLOGY_MODE_UNINITIALIZED
44 struct mask_info *next;
49 static int topology_mode = TOPOLOGY_MODE_UNINITIALIZED;
50 static void set_topology_timer(void);
51 static void topology_work_fn(struct work_struct *work);
52 static struct sysinfo_15_1_x *tl_info;
53 static int cpu_management;
55 static DECLARE_WORK(topology_work, topology_work_fn);
58 * Socket/Book linked lists and cpu_topology updates are
59 * protected by "sched_domains_mutex".
61 static struct mask_info socket_info;
62 static struct mask_info book_info;
63 static struct mask_info drawer_info;
65 struct cpu_topology_s390 cpu_topology[NR_CPUS];
66 EXPORT_SYMBOL_GPL(cpu_topology);
68 static void cpu_group_map(cpumask_t *dst, struct mask_info *info, unsigned int cpu)
70 static cpumask_t mask;
73 if (!cpumask_test_cpu(cpu, &cpu_setup_mask))
75 cpumask_set_cpu(cpu, &mask);
76 switch (topology_mode) {
77 case TOPOLOGY_MODE_HW:
79 if (cpumask_test_cpu(cpu, &info->mask)) {
80 cpumask_copy(&mask, &info->mask);
86 case TOPOLOGY_MODE_PACKAGE:
87 cpumask_copy(&mask, cpu_present_mask);
91 case TOPOLOGY_MODE_SINGLE:
94 cpumask_and(&mask, &mask, &cpu_setup_mask);
96 cpumask_copy(dst, &mask);
99 static void cpu_thread_map(cpumask_t *dst, unsigned int cpu)
101 static cpumask_t mask;
102 unsigned int max_cpu;
104 cpumask_clear(&mask);
105 if (!cpumask_test_cpu(cpu, &cpu_setup_mask))
107 cpumask_set_cpu(cpu, &mask);
108 if (topology_mode != TOPOLOGY_MODE_HW)
110 cpu -= cpu % (smp_cpu_mtid + 1);
111 max_cpu = min(cpu + smp_cpu_mtid, nr_cpu_ids - 1);
112 for (; cpu <= max_cpu; cpu++) {
113 if (cpumask_test_cpu(cpu, &cpu_setup_mask))
114 cpumask_set_cpu(cpu, &mask);
117 cpumask_copy(dst, &mask);
120 #define TOPOLOGY_CORE_BITS 64
122 static void add_cpus_to_mask(struct topology_core *tl_core,
123 struct mask_info *drawer,
124 struct mask_info *book,
125 struct mask_info *socket)
127 struct cpu_topology_s390 *topo;
130 for_each_set_bit(core, &tl_core->mask, TOPOLOGY_CORE_BITS) {
131 unsigned int max_cpu, rcore;
134 rcore = TOPOLOGY_CORE_BITS - 1 - core + tl_core->origin;
135 cpu = smp_find_processor_id(rcore << smp_cpu_mt_shift);
138 max_cpu = min(cpu + smp_cpu_mtid, nr_cpu_ids - 1);
139 for (; cpu <= max_cpu; cpu++) {
140 topo = &cpu_topology[cpu];
141 topo->drawer_id = drawer->id;
142 topo->book_id = book->id;
143 topo->socket_id = socket->id;
144 topo->core_id = rcore;
145 topo->thread_id = cpu;
146 topo->dedicated = tl_core->d;
147 cpumask_set_cpu(cpu, &drawer->mask);
148 cpumask_set_cpu(cpu, &book->mask);
149 cpumask_set_cpu(cpu, &socket->mask);
150 smp_cpu_set_polarization(cpu, tl_core->pp);
151 smp_cpu_set_capacity(cpu, CPU_CAPACITY_HIGH);
156 static void clear_masks(void)
158 struct mask_info *info;
162 cpumask_clear(&info->mask);
167 cpumask_clear(&info->mask);
172 cpumask_clear(&info->mask);
177 static union topology_entry *next_tle(union topology_entry *tle)
180 return (union topology_entry *)((struct topology_core *)tle + 1);
181 return (union topology_entry *)((struct topology_container *)tle + 1);
184 static void tl_to_masks(struct sysinfo_15_1_x *info)
186 struct mask_info *socket = &socket_info;
187 struct mask_info *book = &book_info;
188 struct mask_info *drawer = &drawer_info;
189 union topology_entry *tle, *end;
193 end = (union topology_entry *)((unsigned long)info + info->length);
197 drawer = drawer->next;
198 drawer->id = tle->container.id;
202 book->id = tle->container.id;
205 socket = socket->next;
206 socket->id = tle->container.id;
209 add_cpus_to_mask(&tle->cpu, drawer, book, socket);
219 static void topology_update_polarization_simple(void)
223 for_each_possible_cpu(cpu)
224 smp_cpu_set_polarization(cpu, POLARIZATION_HRZ);
227 static int ptf(unsigned long fc)
232 " .insn rre,0xb9a20000,%[fc],%[fc]\n"
237 return CC_TRANSFORM(cc);
240 int topology_set_cpu_management(int fc)
244 if (!cpu_has_topology())
247 rc = ptf(PTF_VERTICAL);
249 rc = ptf(PTF_HORIZONTAL);
252 for_each_possible_cpu(cpu)
253 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
257 void update_cpu_masks(void)
259 struct cpu_topology_s390 *topo, *topo_package, *topo_sibling;
260 int cpu, sibling, pkg_first, smt_first, id;
262 for_each_possible_cpu(cpu) {
263 topo = &cpu_topology[cpu];
264 cpu_thread_map(&topo->thread_mask, cpu);
265 cpu_group_map(&topo->core_mask, &socket_info, cpu);
266 cpu_group_map(&topo->book_mask, &book_info, cpu);
267 cpu_group_map(&topo->drawer_mask, &drawer_info, cpu);
268 topo->booted_cores = 0;
269 if (topology_mode != TOPOLOGY_MODE_HW) {
270 id = topology_mode == TOPOLOGY_MODE_PACKAGE ? 0 : cpu;
271 topo->thread_id = cpu;
273 topo->socket_id = id;
275 topo->drawer_id = id;
279 for_each_online_cpu(cpu) {
280 topo = &cpu_topology[cpu];
281 pkg_first = cpumask_first(&topo->core_mask);
282 topo_package = &cpu_topology[pkg_first];
283 if (cpu == pkg_first) {
284 for_each_cpu(sibling, &topo->core_mask) {
285 topo_sibling = &cpu_topology[sibling];
286 smt_first = cpumask_first(&topo_sibling->thread_mask);
287 if (sibling == smt_first) {
288 topo_package->booted_cores++;
289 hd_add_core(sibling);
293 topo->booted_cores = topo_package->booted_cores;
298 void store_topology(struct sysinfo_15_1_x *info)
300 stsi(info, 15, 1, topology_mnest_limit());
303 static void __arch_update_dedicated_flag(void *arg)
305 if (topology_cpu_dedicated(smp_processor_id()))
306 set_cpu_flag(CIF_DEDICATED_CPU);
308 clear_cpu_flag(CIF_DEDICATED_CPU);
311 static int __arch_update_cpu_topology(void)
313 struct sysinfo_15_1_x *info = tl_info;
318 mutex_lock(&smp_cpu_state_mutex);
319 if (cpu_has_topology()) {
321 store_topology(info);
325 if (!cpu_has_topology())
326 topology_update_polarization_simple();
327 if (cpu_management == 1)
328 hd_status = hd_enable_hiperdispatch();
329 mutex_unlock(&smp_cpu_state_mutex);
331 hd_disable_hiperdispatch();
335 int arch_update_cpu_topology(void)
339 rc = __arch_update_cpu_topology();
340 on_each_cpu(__arch_update_dedicated_flag, NULL, 0);
344 static void topology_work_fn(struct work_struct *work)
346 rebuild_sched_domains();
349 void topology_schedule_update(void)
351 schedule_work(&topology_work);
354 static void topology_flush_work(void)
356 flush_work(&topology_work);
359 static void topology_timer_fn(struct timer_list *unused)
362 topology_schedule_update();
363 set_topology_timer();
366 static struct timer_list topology_timer;
368 static atomic_t topology_poll = ATOMIC_INIT(0);
370 static void set_topology_timer(void)
372 if (atomic_add_unless(&topology_poll, -1, 0))
373 mod_timer(&topology_timer, jiffies + msecs_to_jiffies(100));
375 mod_timer(&topology_timer, jiffies + secs_to_jiffies(60));
378 void topology_expect_change(void)
380 if (!cpu_has_topology())
382 /* This is racy, but it doesn't matter since it is just a heuristic.
383 * Worst case is that we poll in a higher frequency for a bit longer.
385 if (atomic_read(&topology_poll) > 60)
387 atomic_add(60, &topology_poll);
388 set_topology_timer();
391 static int set_polarization(int polarization)
396 mutex_lock(&smp_cpu_state_mutex);
397 if (cpu_management == polarization)
399 rc = topology_set_cpu_management(polarization);
402 cpu_management = polarization;
403 topology_expect_change();
405 mutex_unlock(&smp_cpu_state_mutex);
410 static ssize_t dispatching_show(struct device *dev,
411 struct device_attribute *attr,
416 mutex_lock(&smp_cpu_state_mutex);
417 count = sysfs_emit(buf, "%d\n", cpu_management);
418 mutex_unlock(&smp_cpu_state_mutex);
422 static ssize_t dispatching_store(struct device *dev,
423 struct device_attribute *attr,
430 if (sscanf(buf, "%d %c", &val, &delim) != 1)
432 if (val != 0 && val != 1)
434 rc = set_polarization(val);
435 return rc ? rc : count;
437 static DEVICE_ATTR_RW(dispatching);
439 static ssize_t cpu_polarization_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
445 mutex_lock(&smp_cpu_state_mutex);
446 switch (smp_cpu_get_polarization(cpu)) {
447 case POLARIZATION_HRZ:
448 count = sysfs_emit(buf, "horizontal\n");
450 case POLARIZATION_VL:
451 count = sysfs_emit(buf, "vertical:low\n");
453 case POLARIZATION_VM:
454 count = sysfs_emit(buf, "vertical:medium\n");
456 case POLARIZATION_VH:
457 count = sysfs_emit(buf, "vertical:high\n");
460 count = sysfs_emit(buf, "unknown\n");
463 mutex_unlock(&smp_cpu_state_mutex);
466 static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
468 static struct attribute *topology_cpu_attrs[] = {
469 &dev_attr_polarization.attr,
473 static struct attribute_group topology_cpu_attr_group = {
474 .attrs = topology_cpu_attrs,
477 static ssize_t cpu_dedicated_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
483 mutex_lock(&smp_cpu_state_mutex);
484 count = sysfs_emit(buf, "%d\n", topology_cpu_dedicated(cpu));
485 mutex_unlock(&smp_cpu_state_mutex);
488 static DEVICE_ATTR(dedicated, 0444, cpu_dedicated_show, NULL);
490 static struct attribute *topology_extra_cpu_attrs[] = {
491 &dev_attr_dedicated.attr,
495 static struct attribute_group topology_extra_cpu_attr_group = {
496 .attrs = topology_extra_cpu_attrs,
499 int topology_cpu_init(struct cpu *cpu)
503 rc = sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
504 if (rc || !cpu_has_topology())
506 rc = sysfs_create_group(&cpu->dev.kobj, &topology_extra_cpu_attr_group);
508 sysfs_remove_group(&cpu->dev.kobj, &topology_cpu_attr_group);
512 static const struct cpumask *cpu_thread_mask(int cpu)
514 return &cpu_topology[cpu].thread_mask;
518 const struct cpumask *cpu_coregroup_mask(int cpu)
520 return &cpu_topology[cpu].core_mask;
523 static const struct cpumask *cpu_book_mask(int cpu)
525 return &cpu_topology[cpu].book_mask;
528 static const struct cpumask *cpu_drawer_mask(int cpu)
530 return &cpu_topology[cpu].drawer_mask;
533 static struct sched_domain_topology_level s390_topology[] = {
534 { cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
535 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
536 { cpu_book_mask, SD_INIT_NAME(BOOK) },
537 { cpu_drawer_mask, SD_INIT_NAME(DRAWER) },
538 { cpu_cpu_mask, SD_INIT_NAME(PKG) },
542 static void __init alloc_masks(struct sysinfo_15_1_x *info,
543 struct mask_info *mask, int offset)
547 nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
548 for (i = 0; i < info->mnest - offset; i++)
549 nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
550 nr_masks = max(nr_masks, 1);
551 for (i = 0; i < nr_masks; i++) {
552 mask->next = memblock_alloc_or_panic(sizeof(*mask->next), 8);
557 static int __init detect_polarization(union topology_entry *tle)
559 struct topology_core *tl_core;
563 tl_core = (struct topology_core *)tle;
564 return tl_core->pp != POLARIZATION_HRZ;
567 void __init topology_init_early(void)
569 struct sysinfo_15_1_x *info;
571 set_sched_topology(s390_topology);
572 if (topology_mode == TOPOLOGY_MODE_UNINITIALIZED) {
573 if (cpu_has_topology())
574 topology_mode = TOPOLOGY_MODE_HW;
576 topology_mode = TOPOLOGY_MODE_SINGLE;
578 if (!cpu_has_topology())
580 tl_info = memblock_alloc_or_panic(PAGE_SIZE, PAGE_SIZE);
582 store_topology(info);
583 cpu_management = detect_polarization(info->tle);
584 pr_info("The CPU configuration topology of the machine is: %d %d %d %d %d %d / %d\n",
585 info->mag[0], info->mag[1], info->mag[2], info->mag[3],
586 info->mag[4], info->mag[5], info->mnest);
587 alloc_masks(info, &socket_info, 1);
588 alloc_masks(info, &book_info, 2);
589 alloc_masks(info, &drawer_info, 3);
591 cpumask_set_cpu(0, &cpu_setup_mask);
592 __arch_update_cpu_topology();
593 __arch_update_dedicated_flag(NULL);
596 static inline int topology_get_mode(int enabled)
599 return TOPOLOGY_MODE_SINGLE;
600 return cpu_has_topology() ? TOPOLOGY_MODE_HW : TOPOLOGY_MODE_PACKAGE;
603 static inline int topology_is_enabled(void)
605 return topology_mode != TOPOLOGY_MODE_SINGLE;
608 static int __init topology_setup(char *str)
613 rc = kstrtobool(str, &enabled);
616 topology_mode = topology_get_mode(enabled);
619 early_param("topology", topology_setup);
621 static int topology_ctl_handler(const struct ctl_table *ctl, int write,
622 void *buffer, size_t *lenp, loff_t *ppos)
624 int enabled = topology_is_enabled();
627 struct ctl_table ctl_entry = {
628 .procname = ctl->procname,
630 .maxlen = sizeof(int),
631 .extra1 = SYSCTL_ZERO,
632 .extra2 = SYSCTL_ONE,
635 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
636 if (rc < 0 || !write)
639 mutex_lock(&smp_cpu_state_mutex);
640 new_mode = topology_get_mode(enabled);
641 if (topology_mode != new_mode) {
642 topology_mode = new_mode;
643 topology_schedule_update();
645 mutex_unlock(&smp_cpu_state_mutex);
646 topology_flush_work();
651 static int polarization_ctl_handler(const struct ctl_table *ctl, int write,
652 void *buffer, size_t *lenp, loff_t *ppos)
656 struct ctl_table ctl_entry = {
657 .procname = ctl->procname,
658 .data = &polarization,
659 .maxlen = sizeof(int),
660 .extra1 = SYSCTL_ZERO,
661 .extra2 = SYSCTL_ONE,
664 polarization = cpu_management;
665 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
666 if (rc < 0 || !write)
668 return set_polarization(polarization);
671 static const struct ctl_table topology_ctl_table[] = {
673 .procname = "topology",
675 .proc_handler = topology_ctl_handler,
678 .procname = "polarization",
680 .proc_handler = polarization_ctl_handler,
684 static int __init topology_init(void)
686 struct device *dev_root;
689 timer_setup(&topology_timer, topology_timer_fn, TIMER_DEFERRABLE);
690 if (cpu_has_topology())
691 set_topology_timer();
693 topology_update_polarization_simple();
694 if (IS_ENABLED(CONFIG_SCHED_TOPOLOGY_VERTICAL))
696 register_sysctl("s390", topology_ctl_table);
698 dev_root = bus_get_dev_root(&cpu_subsys);
700 rc = device_create_file(dev_root, &dev_attr_dispatching);
701 put_device(dev_root);
705 device_initcall(topology_init);