topology.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *    Copyright IBM Corp. 2007
 *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
 */

#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/bootmem.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/cpu.h>
#include <linux/smp.h>
#include <asm/delay.h>
#include <asm/s390_ext.h>
#include <asm/sysinfo.h>

#define CPU_BITS 64
H
Heiko Carstens 已提交
20 21 22 23 24
#define NR_MAG 6

#define PTF_HORIZONTAL	(0UL)
#define PTF_VERTICAL	(1UL)
#define PTF_CHECK	(2UL)
25 26

struct tl_cpu {
H
Heiko Carstens 已提交
27 28 29 30
	unsigned char reserved0[4];
	unsigned char :6;
	unsigned char pp:2;
	unsigned char reserved1;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	unsigned short origin;
	unsigned long mask[CPU_BITS / BITS_PER_LONG];
};

struct tl_container {
	unsigned char reserved[8];
};

union tl_entry {
	unsigned char nl;
	struct tl_cpu cpu;
	struct tl_container container;
};

struct tl_info {
	unsigned char reserved0[2];
	unsigned short length;
	unsigned char mag[NR_MAG];
	unsigned char reserved1;
	unsigned char mnest;
	unsigned char reserved2[4];
	union tl_entry tle[0];
};

struct core_info {
	struct core_info *next;
	cpumask_t mask;
};

static void topology_work_fn(struct work_struct *work);
static struct tl_info *tl_info;
static struct core_info core_info;
static int machine_has_topology;
static int machine_has_topology_irq;
static struct timer_list topology_timer;
static void set_topology_timer(void);
static DECLARE_WORK(topology_work, topology_work_fn);
H
Heiko Carstens 已提交
68 69
/* topology_lock protects the core linked list */
static DEFINE_SPINLOCK(topology_lock);
70

71 72
cpumask_t cpu_core_map[NR_CPUS];

73 74 75
cpumask_t cpu_coregroup_map(unsigned int cpu)
{
	struct core_info *core = &core_info;
H
Heiko Carstens 已提交
76
	unsigned long flags;
77 78 79 80
	cpumask_t mask;

	cpus_clear(mask);
	if (!machine_has_topology)
81
		return cpu_possible_map;
H
Heiko Carstens 已提交
82
	spin_lock_irqsave(&topology_lock, flags);
83 84 85 86 87 88 89
	while (core) {
		if (cpu_isset(cpu, core->mask)) {
			mask = core->mask;
			break;
		}
		core = core->next;
	}
H
Heiko Carstens 已提交
90
	spin_unlock_irqrestore(&topology_lock, flags);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	if (cpus_empty(mask))
		mask = cpumask_of_cpu(cpu);
	return mask;
}

static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
{
	unsigned int cpu;

	for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
	     cpu < CPU_BITS;
	     cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
	{
		unsigned int rcpu, lcpu;

		rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
		for_each_present_cpu(lcpu) {
H
Heiko Carstens 已提交
108
			if (__cpu_logical_map[lcpu] == rcpu) {
109
				cpu_set(lcpu, core->mask);
H
Heiko Carstens 已提交
110 111
				smp_cpu_polarization[lcpu] = tl_cpu->pp;
			}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		}
	}
}

static void clear_cores(void)
{
	struct core_info *core = &core_info;

	while (core) {
		cpus_clear(core->mask);
		core = core->next;
	}
}

static union tl_entry *next_tle(union tl_entry *tle)
{
	if (tle->nl)
		return (union tl_entry *)((struct tl_container *)tle + 1);
	else
		return (union tl_entry *)((struct tl_cpu *)tle + 1);
}

static void tl_to_cores(struct tl_info *info)
{
	union tl_entry *tle, *end;
	struct core_info *core = &core_info;

H
Heiko Carstens 已提交
139
	spin_lock_irq(&topology_lock);
140
	clear_cores();
H
Heiko Carstens 已提交
141
	tle = info->tle;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	end = (union tl_entry *)((unsigned long)info + info->length);
	while (tle < end) {
		switch (tle->nl) {
		case 5:
		case 4:
		case 3:
		case 2:
			break;
		case 1:
			core = core->next;
			break;
		case 0:
			add_cpus_to_core(&tle->cpu, core);
			break;
		default:
			clear_cores();
			machine_has_topology = 0;
			return;
		}
		tle = next_tle(tle);
	}
H
Heiko Carstens 已提交
163
	spin_unlock_irq(&topology_lock);
164 165
}

H
Heiko Carstens 已提交
166 167 168 169 170
static void topology_update_polarization_simple(void)
{
	int cpu;

	mutex_lock(&smp_cpu_state_mutex);
171
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
172 173 174 175 176
		smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
	mutex_unlock(&smp_cpu_state_mutex);
}

static int ptf(unsigned long fc)
177 178 179 180 181 182 183 184
{
	int rc;

	asm volatile(
		"	.insn	rre,0xb9a20000,%1,%1\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (rc)
H
Heiko Carstens 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
		: "d" (fc)  : "cc");
	return rc;
}

int topology_set_cpu_management(int fc)
{
	int cpu;
	int rc;

	if (!machine_has_topology)
		return -EOPNOTSUPP;
	if (fc)
		rc = ptf(PTF_VERTICAL);
	else
		rc = ptf(PTF_HORIZONTAL);
	if (rc)
		return -EBUSY;
202
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
203
		smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
204 205 206
	return rc;
}

207 208 209 210
static void update_cpu_core_map(void)
{
	int cpu;

211
	for_each_possible_cpu(cpu)
212 213 214
		cpu_core_map[cpu] = cpu_coregroup_map(cpu);
}

215 216 217 218 219 220
void arch_update_cpu_topology(void)
{
	struct tl_info *info = tl_info;
	struct sys_device *sysdev;
	int cpu;

H
Heiko Carstens 已提交
221
	if (!machine_has_topology) {
222
		update_cpu_core_map();
H
Heiko Carstens 已提交
223
		topology_update_polarization_simple();
224
		return;
H
Heiko Carstens 已提交
225
	}
226 227
	stsi(info, 15, 1, 2);
	tl_to_cores(info);
228
	update_cpu_core_map();
229 230 231 232 233 234
	for_each_online_cpu(cpu) {
		sysdev = get_cpu_sysdev(cpu);
		kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
	}
}

235 236
static void topology_work_fn(struct work_struct *work)
{
237
	arch_reinit_sched_domains();
238 239
}

H
Heiko Carstens 已提交
240 241 242 243 244
void topology_schedule_update(void)
{
	schedule_work(&topology_work);
}

245 246
static void topology_timer_fn(unsigned long ignored)
{
H
Heiko Carstens 已提交
247 248
	if (ptf(PTF_CHECK))
		topology_schedule_update();
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	set_topology_timer();
}

static void set_topology_timer(void)
{
	topology_timer.function = topology_timer_fn;
	topology_timer.data = 0;
	topology_timer.expires = jiffies + 60 * HZ;
	add_timer(&topology_timer);
}

static void topology_interrupt(__u16 code)
{
	schedule_work(&topology_work);
}

static int __init init_topology_update(void)
{
	int rc;

269
	rc = 0;
H
Heiko Carstens 已提交
270 271
	if (!machine_has_topology) {
		topology_update_polarization_simple();
272
		goto out;
H
Heiko Carstens 已提交
273 274
	}
	init_timer_deferrable(&topology_timer);
275 276 277
	if (machine_has_topology_irq) {
		rc = register_external_interrupt(0x2005, topology_interrupt);
		if (rc)
278
			goto out;
279 280 281 282
		ctl_set_bit(0, 8);
	}
	else
		set_topology_timer();
283 284 285
out:
	update_cpu_core_map();
	return rc;
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
}
__initcall(init_topology_update);

void __init s390_init_cpu_topology(void)
{
	unsigned long long facility_bits;
	struct tl_info *info;
	struct core_info *core;
	int nr_cores;
	int i;

	if (stfle(&facility_bits, 1) <= 0)
		return;
	if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
		return;
	machine_has_topology = 1;

	if (facility_bits & (1ULL << 51))
		machine_has_topology_irq = 1;

	tl_info = alloc_bootmem_pages(PAGE_SIZE);
	info = tl_info;
	stsi(info, 15, 1, 2);

	nr_cores = info->mag[NR_MAG - 2];
	for (i = 0; i < info->mnest - 2; i++)
		nr_cores *= info->mag[NR_MAG - 3 - i];

	printk(KERN_INFO "CPU topology:");
	for (i = 0; i < NR_MAG; i++)
		printk(" %d", info->mag[i]);
	printk(" / %d\n", info->mnest);

	core = &core_info;
	for (i = 0; i < nr_cores; i++) {
		core->next = alloc_bootmem(sizeof(struct core_info));
		core = core->next;
		if (!core)
			goto error;
	}
	return;
error:
	machine_has_topology = 0;
	machine_has_topology_irq = 0;
}