Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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