xref: /linux/arch/s390/kernel/topology.c (revision 27c8f12e972d3647e9d759d7cafd4c34fa513432)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2011
4  */
5 
6 #define KMSG_COMPONENT "cpu"
7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
8 
9 #include <linux/workqueue.h>
10 #include <linux/memblock.h>
11 #include <linux/uaccess.h>
12 #include <linux/sysctl.h>
13 #include <linux/cpuset.h>
14 #include <linux/device.h>
15 #include <linux/export.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/sched/topology.h>
19 #include <linux/delay.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/cpu.h>
23 #include <linux/smp.h>
24 #include <linux/mm.h>
25 #include <linux/nodemask.h>
26 #include <linux/node.h>
27 #include <asm/sysinfo.h>
28 
29 #define PTF_HORIZONTAL	(0UL)
30 #define PTF_VERTICAL	(1UL)
31 #define PTF_CHECK	(2UL)
32 
33 enum {
34 	TOPOLOGY_MODE_HW,
35 	TOPOLOGY_MODE_SINGLE,
36 	TOPOLOGY_MODE_PACKAGE,
37 	TOPOLOGY_MODE_UNINITIALIZED
38 };
39 
40 struct mask_info {
41 	struct mask_info *next;
42 	unsigned char id;
43 	cpumask_t mask;
44 };
45 
46 static int topology_mode = TOPOLOGY_MODE_UNINITIALIZED;
47 static void set_topology_timer(void);
48 static void topology_work_fn(struct work_struct *work);
49 static struct sysinfo_15_1_x *tl_info;
50 
51 static DECLARE_WORK(topology_work, topology_work_fn);
52 
53 /*
54  * Socket/Book linked lists and cpu_topology updates are
55  * protected by "sched_domains_mutex".
56  */
57 static struct mask_info socket_info;
58 static struct mask_info book_info;
59 static struct mask_info drawer_info;
60 
61 struct cpu_topology_s390 cpu_topology[NR_CPUS];
62 EXPORT_SYMBOL_GPL(cpu_topology);
63 
64 static void cpu_group_map(cpumask_t *dst, struct mask_info *info, unsigned int cpu)
65 {
66 	static cpumask_t mask;
67 
68 	cpumask_clear(&mask);
69 	if (!cpumask_test_cpu(cpu, &cpu_setup_mask))
70 		goto out;
71 	cpumask_set_cpu(cpu, &mask);
72 	switch (topology_mode) {
73 	case TOPOLOGY_MODE_HW:
74 		while (info) {
75 			if (cpumask_test_cpu(cpu, &info->mask)) {
76 				cpumask_copy(&mask, &info->mask);
77 				break;
78 			}
79 			info = info->next;
80 		}
81 		break;
82 	case TOPOLOGY_MODE_PACKAGE:
83 		cpumask_copy(&mask, cpu_present_mask);
84 		break;
85 	default:
86 		fallthrough;
87 	case TOPOLOGY_MODE_SINGLE:
88 		break;
89 	}
90 	cpumask_and(&mask, &mask, &cpu_setup_mask);
91 out:
92 	cpumask_copy(dst, &mask);
93 }
94 
95 static void cpu_thread_map(cpumask_t *dst, unsigned int cpu)
96 {
97 	static cpumask_t mask;
98 	unsigned int max_cpu;
99 
100 	cpumask_clear(&mask);
101 	if (!cpumask_test_cpu(cpu, &cpu_setup_mask))
102 		goto out;
103 	cpumask_set_cpu(cpu, &mask);
104 	if (topology_mode != TOPOLOGY_MODE_HW)
105 		goto out;
106 	cpu -= cpu % (smp_cpu_mtid + 1);
107 	max_cpu = min(cpu + smp_cpu_mtid, nr_cpu_ids - 1);
108 	for (; cpu <= max_cpu; cpu++) {
109 		if (cpumask_test_cpu(cpu, &cpu_setup_mask))
110 			cpumask_set_cpu(cpu, &mask);
111 	}
112 out:
113 	cpumask_copy(dst, &mask);
114 }
115 
116 #define TOPOLOGY_CORE_BITS	64
117 
118 static void add_cpus_to_mask(struct topology_core *tl_core,
119 			     struct mask_info *drawer,
120 			     struct mask_info *book,
121 			     struct mask_info *socket)
122 {
123 	struct cpu_topology_s390 *topo;
124 	unsigned int core;
125 
126 	for_each_set_bit(core, &tl_core->mask, TOPOLOGY_CORE_BITS) {
127 		unsigned int max_cpu, rcore;
128 		int cpu;
129 
130 		rcore = TOPOLOGY_CORE_BITS - 1 - core + tl_core->origin;
131 		cpu = smp_find_processor_id(rcore << smp_cpu_mt_shift);
132 		if (cpu < 0)
133 			continue;
134 		max_cpu = min(cpu + smp_cpu_mtid, nr_cpu_ids - 1);
135 		for (; cpu <= max_cpu; cpu++) {
136 			topo = &cpu_topology[cpu];
137 			topo->drawer_id = drawer->id;
138 			topo->book_id = book->id;
139 			topo->socket_id = socket->id;
140 			topo->core_id = rcore;
141 			topo->thread_id = cpu;
142 			topo->dedicated = tl_core->d;
143 			cpumask_set_cpu(cpu, &drawer->mask);
144 			cpumask_set_cpu(cpu, &book->mask);
145 			cpumask_set_cpu(cpu, &socket->mask);
146 			smp_cpu_set_polarization(cpu, tl_core->pp);
147 		}
148 	}
149 }
150 
151 static void clear_masks(void)
152 {
153 	struct mask_info *info;
154 
155 	info = &socket_info;
156 	while (info) {
157 		cpumask_clear(&info->mask);
158 		info = info->next;
159 	}
160 	info = &book_info;
161 	while (info) {
162 		cpumask_clear(&info->mask);
163 		info = info->next;
164 	}
165 	info = &drawer_info;
166 	while (info) {
167 		cpumask_clear(&info->mask);
168 		info = info->next;
169 	}
170 }
171 
172 static union topology_entry *next_tle(union topology_entry *tle)
173 {
174 	if (!tle->nl)
175 		return (union topology_entry *)((struct topology_core *)tle + 1);
176 	return (union topology_entry *)((struct topology_container *)tle + 1);
177 }
178 
179 static void tl_to_masks(struct sysinfo_15_1_x *info)
180 {
181 	struct mask_info *socket = &socket_info;
182 	struct mask_info *book = &book_info;
183 	struct mask_info *drawer = &drawer_info;
184 	union topology_entry *tle, *end;
185 
186 	clear_masks();
187 	tle = info->tle;
188 	end = (union topology_entry *)((unsigned long)info + info->length);
189 	while (tle < end) {
190 		switch (tle->nl) {
191 		case 3:
192 			drawer = drawer->next;
193 			drawer->id = tle->container.id;
194 			break;
195 		case 2:
196 			book = book->next;
197 			book->id = tle->container.id;
198 			break;
199 		case 1:
200 			socket = socket->next;
201 			socket->id = tle->container.id;
202 			break;
203 		case 0:
204 			add_cpus_to_mask(&tle->cpu, drawer, book, socket);
205 			break;
206 		default:
207 			clear_masks();
208 			return;
209 		}
210 		tle = next_tle(tle);
211 	}
212 }
213 
214 static void topology_update_polarization_simple(void)
215 {
216 	int cpu;
217 
218 	for_each_possible_cpu(cpu)
219 		smp_cpu_set_polarization(cpu, POLARIZATION_HRZ);
220 }
221 
222 static int ptf(unsigned long fc)
223 {
224 	int rc;
225 
226 	asm volatile(
227 		"	.insn	rre,0xb9a20000,%1,%1\n"
228 		"	ipm	%0\n"
229 		"	srl	%0,28\n"
230 		: "=d" (rc)
231 		: "d" (fc)  : "cc");
232 	return rc;
233 }
234 
235 int topology_set_cpu_management(int fc)
236 {
237 	int cpu, rc;
238 
239 	if (!MACHINE_HAS_TOPOLOGY)
240 		return -EOPNOTSUPP;
241 	if (fc)
242 		rc = ptf(PTF_VERTICAL);
243 	else
244 		rc = ptf(PTF_HORIZONTAL);
245 	if (rc)
246 		return -EBUSY;
247 	for_each_possible_cpu(cpu)
248 		smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
249 	return rc;
250 }
251 
252 void update_cpu_masks(void)
253 {
254 	struct cpu_topology_s390 *topo, *topo_package, *topo_sibling;
255 	int cpu, sibling, pkg_first, smt_first, id;
256 
257 	for_each_possible_cpu(cpu) {
258 		topo = &cpu_topology[cpu];
259 		cpu_thread_map(&topo->thread_mask, cpu);
260 		cpu_group_map(&topo->core_mask, &socket_info, cpu);
261 		cpu_group_map(&topo->book_mask, &book_info, cpu);
262 		cpu_group_map(&topo->drawer_mask, &drawer_info, cpu);
263 		topo->booted_cores = 0;
264 		if (topology_mode != TOPOLOGY_MODE_HW) {
265 			id = topology_mode == TOPOLOGY_MODE_PACKAGE ? 0 : cpu;
266 			topo->thread_id = cpu;
267 			topo->core_id = cpu;
268 			topo->socket_id = id;
269 			topo->book_id = id;
270 			topo->drawer_id = id;
271 		}
272 	}
273 	for_each_online_cpu(cpu) {
274 		topo = &cpu_topology[cpu];
275 		pkg_first = cpumask_first(&topo->core_mask);
276 		topo_package = &cpu_topology[pkg_first];
277 		if (cpu == pkg_first) {
278 			for_each_cpu(sibling, &topo->core_mask) {
279 				topo_sibling = &cpu_topology[sibling];
280 				smt_first = cpumask_first(&topo_sibling->thread_mask);
281 				if (sibling == smt_first)
282 					topo_package->booted_cores++;
283 			}
284 		} else {
285 			topo->booted_cores = topo_package->booted_cores;
286 		}
287 	}
288 }
289 
290 void store_topology(struct sysinfo_15_1_x *info)
291 {
292 	stsi(info, 15, 1, topology_mnest_limit());
293 }
294 
295 static void __arch_update_dedicated_flag(void *arg)
296 {
297 	if (topology_cpu_dedicated(smp_processor_id()))
298 		set_cpu_flag(CIF_DEDICATED_CPU);
299 	else
300 		clear_cpu_flag(CIF_DEDICATED_CPU);
301 }
302 
303 static int __arch_update_cpu_topology(void)
304 {
305 	struct sysinfo_15_1_x *info = tl_info;
306 	int rc = 0;
307 
308 	mutex_lock(&smp_cpu_state_mutex);
309 	if (MACHINE_HAS_TOPOLOGY) {
310 		rc = 1;
311 		store_topology(info);
312 		tl_to_masks(info);
313 	}
314 	update_cpu_masks();
315 	if (!MACHINE_HAS_TOPOLOGY)
316 		topology_update_polarization_simple();
317 	mutex_unlock(&smp_cpu_state_mutex);
318 	return rc;
319 }
320 
321 int arch_update_cpu_topology(void)
322 {
323 	int rc;
324 
325 	rc = __arch_update_cpu_topology();
326 	on_each_cpu(__arch_update_dedicated_flag, NULL, 0);
327 	return rc;
328 }
329 
330 static void topology_work_fn(struct work_struct *work)
331 {
332 	rebuild_sched_domains();
333 }
334 
335 void topology_schedule_update(void)
336 {
337 	schedule_work(&topology_work);
338 }
339 
340 static void topology_flush_work(void)
341 {
342 	flush_work(&topology_work);
343 }
344 
345 static void topology_timer_fn(struct timer_list *unused)
346 {
347 	if (ptf(PTF_CHECK))
348 		topology_schedule_update();
349 	set_topology_timer();
350 }
351 
352 static struct timer_list topology_timer;
353 
354 static atomic_t topology_poll = ATOMIC_INIT(0);
355 
356 static void set_topology_timer(void)
357 {
358 	if (atomic_add_unless(&topology_poll, -1, 0))
359 		mod_timer(&topology_timer, jiffies + msecs_to_jiffies(100));
360 	else
361 		mod_timer(&topology_timer, jiffies + msecs_to_jiffies(60 * MSEC_PER_SEC));
362 }
363 
364 void topology_expect_change(void)
365 {
366 	if (!MACHINE_HAS_TOPOLOGY)
367 		return;
368 	/* This is racy, but it doesn't matter since it is just a heuristic.
369 	 * Worst case is that we poll in a higher frequency for a bit longer.
370 	 */
371 	if (atomic_read(&topology_poll) > 60)
372 		return;
373 	atomic_add(60, &topology_poll);
374 	set_topology_timer();
375 }
376 
377 static int cpu_management;
378 
379 static ssize_t dispatching_show(struct device *dev,
380 				struct device_attribute *attr,
381 				char *buf)
382 {
383 	ssize_t count;
384 
385 	mutex_lock(&smp_cpu_state_mutex);
386 	count = sprintf(buf, "%d\n", cpu_management);
387 	mutex_unlock(&smp_cpu_state_mutex);
388 	return count;
389 }
390 
391 static ssize_t dispatching_store(struct device *dev,
392 				 struct device_attribute *attr,
393 				 const char *buf,
394 				 size_t count)
395 {
396 	int val, rc;
397 	char delim;
398 
399 	if (sscanf(buf, "%d %c", &val, &delim) != 1)
400 		return -EINVAL;
401 	if (val != 0 && val != 1)
402 		return -EINVAL;
403 	rc = 0;
404 	cpus_read_lock();
405 	mutex_lock(&smp_cpu_state_mutex);
406 	if (cpu_management == val)
407 		goto out;
408 	rc = topology_set_cpu_management(val);
409 	if (rc)
410 		goto out;
411 	cpu_management = val;
412 	topology_expect_change();
413 out:
414 	mutex_unlock(&smp_cpu_state_mutex);
415 	cpus_read_unlock();
416 	return rc ? rc : count;
417 }
418 static DEVICE_ATTR_RW(dispatching);
419 
420 static ssize_t cpu_polarization_show(struct device *dev,
421 				     struct device_attribute *attr, char *buf)
422 {
423 	int cpu = dev->id;
424 	ssize_t count;
425 
426 	mutex_lock(&smp_cpu_state_mutex);
427 	switch (smp_cpu_get_polarization(cpu)) {
428 	case POLARIZATION_HRZ:
429 		count = sprintf(buf, "horizontal\n");
430 		break;
431 	case POLARIZATION_VL:
432 		count = sprintf(buf, "vertical:low\n");
433 		break;
434 	case POLARIZATION_VM:
435 		count = sprintf(buf, "vertical:medium\n");
436 		break;
437 	case POLARIZATION_VH:
438 		count = sprintf(buf, "vertical:high\n");
439 		break;
440 	default:
441 		count = sprintf(buf, "unknown\n");
442 		break;
443 	}
444 	mutex_unlock(&smp_cpu_state_mutex);
445 	return count;
446 }
447 static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
448 
449 static struct attribute *topology_cpu_attrs[] = {
450 	&dev_attr_polarization.attr,
451 	NULL,
452 };
453 
454 static struct attribute_group topology_cpu_attr_group = {
455 	.attrs = topology_cpu_attrs,
456 };
457 
458 static ssize_t cpu_dedicated_show(struct device *dev,
459 				  struct device_attribute *attr, char *buf)
460 {
461 	int cpu = dev->id;
462 	ssize_t count;
463 
464 	mutex_lock(&smp_cpu_state_mutex);
465 	count = sprintf(buf, "%d\n", topology_cpu_dedicated(cpu));
466 	mutex_unlock(&smp_cpu_state_mutex);
467 	return count;
468 }
469 static DEVICE_ATTR(dedicated, 0444, cpu_dedicated_show, NULL);
470 
471 static struct attribute *topology_extra_cpu_attrs[] = {
472 	&dev_attr_dedicated.attr,
473 	NULL,
474 };
475 
476 static struct attribute_group topology_extra_cpu_attr_group = {
477 	.attrs = topology_extra_cpu_attrs,
478 };
479 
480 int topology_cpu_init(struct cpu *cpu)
481 {
482 	int rc;
483 
484 	rc = sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
485 	if (rc || !MACHINE_HAS_TOPOLOGY)
486 		return rc;
487 	rc = sysfs_create_group(&cpu->dev.kobj, &topology_extra_cpu_attr_group);
488 	if (rc)
489 		sysfs_remove_group(&cpu->dev.kobj, &topology_cpu_attr_group);
490 	return rc;
491 }
492 
493 static const struct cpumask *cpu_thread_mask(int cpu)
494 {
495 	return &cpu_topology[cpu].thread_mask;
496 }
497 
498 
499 const struct cpumask *cpu_coregroup_mask(int cpu)
500 {
501 	return &cpu_topology[cpu].core_mask;
502 }
503 
504 static const struct cpumask *cpu_book_mask(int cpu)
505 {
506 	return &cpu_topology[cpu].book_mask;
507 }
508 
509 static const struct cpumask *cpu_drawer_mask(int cpu)
510 {
511 	return &cpu_topology[cpu].drawer_mask;
512 }
513 
514 static struct sched_domain_topology_level s390_topology[] = {
515 	{ cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
516 	{ cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
517 	{ cpu_book_mask, SD_INIT_NAME(BOOK) },
518 	{ cpu_drawer_mask, SD_INIT_NAME(DRAWER) },
519 	{ cpu_cpu_mask, SD_INIT_NAME(PKG) },
520 	{ NULL, },
521 };
522 
523 static void __init alloc_masks(struct sysinfo_15_1_x *info,
524 			       struct mask_info *mask, int offset)
525 {
526 	int i, nr_masks;
527 
528 	nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
529 	for (i = 0; i < info->mnest - offset; i++)
530 		nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
531 	nr_masks = max(nr_masks, 1);
532 	for (i = 0; i < nr_masks; i++) {
533 		mask->next = memblock_alloc(sizeof(*mask->next), 8);
534 		if (!mask->next)
535 			panic("%s: Failed to allocate %zu bytes align=0x%x\n",
536 			      __func__, sizeof(*mask->next), 8);
537 		mask = mask->next;
538 	}
539 }
540 
541 void __init topology_init_early(void)
542 {
543 	struct sysinfo_15_1_x *info;
544 
545 	set_sched_topology(s390_topology);
546 	if (topology_mode == TOPOLOGY_MODE_UNINITIALIZED) {
547 		if (MACHINE_HAS_TOPOLOGY)
548 			topology_mode = TOPOLOGY_MODE_HW;
549 		else
550 			topology_mode = TOPOLOGY_MODE_SINGLE;
551 	}
552 	if (!MACHINE_HAS_TOPOLOGY)
553 		goto out;
554 	tl_info = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
555 	if (!tl_info)
556 		panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
557 		      __func__, PAGE_SIZE, PAGE_SIZE);
558 	info = tl_info;
559 	store_topology(info);
560 	pr_info("The CPU configuration topology of the machine is: %d %d %d %d %d %d / %d\n",
561 		info->mag[0], info->mag[1], info->mag[2], info->mag[3],
562 		info->mag[4], info->mag[5], info->mnest);
563 	alloc_masks(info, &socket_info, 1);
564 	alloc_masks(info, &book_info, 2);
565 	alloc_masks(info, &drawer_info, 3);
566 out:
567 	cpumask_set_cpu(0, &cpu_setup_mask);
568 	__arch_update_cpu_topology();
569 	__arch_update_dedicated_flag(NULL);
570 }
571 
572 static inline int topology_get_mode(int enabled)
573 {
574 	if (!enabled)
575 		return TOPOLOGY_MODE_SINGLE;
576 	return MACHINE_HAS_TOPOLOGY ? TOPOLOGY_MODE_HW : TOPOLOGY_MODE_PACKAGE;
577 }
578 
579 static inline int topology_is_enabled(void)
580 {
581 	return topology_mode != TOPOLOGY_MODE_SINGLE;
582 }
583 
584 static int __init topology_setup(char *str)
585 {
586 	bool enabled;
587 	int rc;
588 
589 	rc = kstrtobool(str, &enabled);
590 	if (rc)
591 		return rc;
592 	topology_mode = topology_get_mode(enabled);
593 	return 0;
594 }
595 early_param("topology", topology_setup);
596 
597 static int topology_ctl_handler(const struct ctl_table *ctl, int write,
598 				void *buffer, size_t *lenp, loff_t *ppos)
599 {
600 	int enabled = topology_is_enabled();
601 	int new_mode;
602 	int rc;
603 	struct ctl_table ctl_entry = {
604 		.procname	= ctl->procname,
605 		.data		= &enabled,
606 		.maxlen		= sizeof(int),
607 		.extra1		= SYSCTL_ZERO,
608 		.extra2		= SYSCTL_ONE,
609 	};
610 
611 	rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
612 	if (rc < 0 || !write)
613 		return rc;
614 
615 	mutex_lock(&smp_cpu_state_mutex);
616 	new_mode = topology_get_mode(enabled);
617 	if (topology_mode != new_mode) {
618 		topology_mode = new_mode;
619 		topology_schedule_update();
620 	}
621 	mutex_unlock(&smp_cpu_state_mutex);
622 	topology_flush_work();
623 
624 	return rc;
625 }
626 
627 static struct ctl_table topology_ctl_table[] = {
628 	{
629 		.procname	= "topology",
630 		.mode		= 0644,
631 		.proc_handler	= topology_ctl_handler,
632 	},
633 };
634 
635 static int __init topology_init(void)
636 {
637 	struct device *dev_root;
638 	int rc = 0;
639 
640 	timer_setup(&topology_timer, topology_timer_fn, TIMER_DEFERRABLE);
641 	if (MACHINE_HAS_TOPOLOGY)
642 		set_topology_timer();
643 	else
644 		topology_update_polarization_simple();
645 	register_sysctl("s390", topology_ctl_table);
646 
647 	dev_root = bus_get_dev_root(&cpu_subsys);
648 	if (dev_root) {
649 		rc = device_create_file(dev_root, &dev_attr_dispatching);
650 		put_device(dev_root);
651 	}
652 	return rc;
653 }
654 device_initcall(topology_init);
655