topology.c 7.5 KB
Newer Older
1 2 3 4 5
/*
 *    Copyright IBM Corp. 2007
 *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
 */

6 7 8
#define KMSG_COMPONENT "cpu"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

9 10 11 12 13 14 15 16 17
#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>
18
#include <linux/cpuset.h>
19 20 21 22 23
#include <asm/delay.h>
#include <asm/s390_ext.h>
#include <asm/sysinfo.h>

#define CPU_BITS 64
H
Heiko Carstens 已提交
24 25 26 27 28
#define NR_MAG 6

#define PTF_HORIZONTAL	(0UL)
#define PTF_VERTICAL	(1UL)
#define PTF_CHECK	(2UL)
29 30

struct tl_cpu {
H
Heiko Carstens 已提交
31 32 33 34
	unsigned char reserved0[4];
	unsigned char :6;
	unsigned char pp:2;
	unsigned char reserved1;
35 36 37 38 39
	unsigned short origin;
	unsigned long mask[CPU_BITS / BITS_PER_LONG];
};

struct tl_container {
40 41
	unsigned char reserved[7];
	unsigned char id;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
};

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];
};

60 61
struct mask_info {
	struct mask_info *next;
62
	unsigned char id;
63 64 65
	cpumask_t mask;
};

66
static int topology_enabled;
67 68 69 70 71 72
static void topology_work_fn(struct work_struct *work);
static struct tl_info *tl_info;
static int machine_has_topology;
static struct timer_list topology_timer;
static void set_topology_timer(void);
static DECLARE_WORK(topology_work, topology_work_fn);
H
Heiko Carstens 已提交
73 74
/* topology_lock protects the core linked list */
static DEFINE_SPINLOCK(topology_lock);
75

76
static struct mask_info core_info;
77
cpumask_t cpu_core_map[NR_CPUS];
78
unsigned char cpu_core_id[NR_CPUS];
79

80 81 82 83 84 85 86
#ifdef CONFIG_SCHED_BOOK
static struct mask_info book_info;
cpumask_t cpu_book_map[NR_CPUS];
unsigned char cpu_book_id[NR_CPUS];
#endif

static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
87 88 89 90
{
	cpumask_t mask;

	cpus_clear(mask);
91
	if (!topology_enabled || !machine_has_topology)
92
		return cpu_possible_map;
93 94 95
	while (info) {
		if (cpu_isset(cpu, info->mask)) {
			mask = info->mask;
96 97
			break;
		}
98
		info = info->next;
99 100 101 102 103 104
	}
	if (cpus_empty(mask))
		mask = cpumask_of_cpu(cpu);
	return mask;
}

105 106
static void add_cpus_to_mask(struct tl_cpu *tl_cpu, struct mask_info *book,
			     struct mask_info *core)
107 108 109 110 111 112 113 114 115 116 117
{
	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) {
118 119 120 121 122 123 124 125 126
			if (cpu_logical_map(lcpu) != rcpu)
				continue;
#ifdef CONFIG_SCHED_BOOK
			cpu_set(lcpu, book->mask);
			cpu_book_id[lcpu] = book->id;
#endif
			cpu_set(lcpu, core->mask);
			cpu_core_id[lcpu] = core->id;
			smp_cpu_polarization[lcpu] = tl_cpu->pp;
127 128 129 130
		}
	}
}

131
static void clear_masks(void)
132
{
133
	struct mask_info *info;
134

135 136 137 138 139 140 141 142 143 144
	info = &core_info;
	while (info) {
		cpus_clear(info->mask);
		info = info->next;
	}
#ifdef CONFIG_SCHED_BOOK
	info = &book_info;
	while (info) {
		cpus_clear(info->mask);
		info = info->next;
145
	}
146
#endif
147 148 149 150 151 152 153 154 155 156 157 158
}

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)
{
159 160 161 162 163 164
#ifdef CONFIG_SCHED_BOOK
	struct mask_info *book = &book_info;
#else
	struct mask_info *book = NULL;
#endif
	struct mask_info *core = &core_info;
165
	union tl_entry *tle, *end;
166

167

H
Heiko Carstens 已提交
168
	spin_lock_irq(&topology_lock);
169
	clear_masks();
H
Heiko Carstens 已提交
170
	tle = info->tle;
171 172 173
	end = (union tl_entry *)((unsigned long)info + info->length);
	while (tle < end) {
		switch (tle->nl) {
174
#ifdef CONFIG_SCHED_BOOK
175
		case 2:
176 177
			book = book->next;
			book->id = tle->container.id;
178
			break;
179
#endif
180 181
		case 1:
			core = core->next;
182
			core->id = tle->container.id;
183 184
			break;
		case 0:
185
			add_cpus_to_mask(&tle->cpu, book, core);
186 187
			break;
		default:
188
			clear_masks();
189
			machine_has_topology = 0;
190
			goto out;
191 192 193
		}
		tle = next_tle(tle);
	}
194
out:
H
Heiko Carstens 已提交
195
	spin_unlock_irq(&topology_lock);
196 197
}

H
Heiko Carstens 已提交
198 199 200 201 202
static void topology_update_polarization_simple(void)
{
	int cpu;

	mutex_lock(&smp_cpu_state_mutex);
203
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
204 205 206 207 208
		smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
	mutex_unlock(&smp_cpu_state_mutex);
}

static int ptf(unsigned long fc)
209 210 211 212 213 214 215 216
{
	int rc;

	asm volatile(
		"	.insn	rre,0xb9a20000,%1,%1\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (rc)
H
Heiko Carstens 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		: "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;
234
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
235
		smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
236 237 238
	return rc;
}

239 240
static void update_cpu_core_map(void)
{
241
	unsigned long flags;
242 243
	int cpu;

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	spin_lock_irqsave(&topology_lock, flags);
	for_each_possible_cpu(cpu) {
		cpu_core_map[cpu] = cpu_group_map(&core_info, cpu);
#ifdef CONFIG_SCHED_BOOK
		cpu_book_map[cpu] = cpu_group_map(&book_info, cpu);
#endif
	}
	spin_unlock_irqrestore(&topology_lock, flags);
}

static void store_topology(struct tl_info *info)
{
#ifdef CONFIG_SCHED_BOOK
	int rc;

	rc = stsi(info, 15, 1, 3);
	if (rc != -ENOSYS)
		return;
#endif
	stsi(info, 15, 1, 2);
264 265
}

266
int arch_update_cpu_topology(void)
267 268 269 270 271
{
	struct tl_info *info = tl_info;
	struct sys_device *sysdev;
	int cpu;

H
Heiko Carstens 已提交
272
	if (!machine_has_topology) {
273
		update_cpu_core_map();
H
Heiko Carstens 已提交
274
		topology_update_polarization_simple();
275
		return 0;
H
Heiko Carstens 已提交
276
	}
277
	store_topology(info);
278
	tl_to_cores(info);
279
	update_cpu_core_map();
280 281 282 283
	for_each_online_cpu(cpu) {
		sysdev = get_cpu_sysdev(cpu);
		kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
	}
284
	return 1;
285 286
}

287 288
static void topology_work_fn(struct work_struct *work)
{
289
	rebuild_sched_domains();
290 291
}

H
Heiko Carstens 已提交
292 293 294 295 296
void topology_schedule_update(void)
{
	schedule_work(&topology_work);
}

297 298
static void topology_timer_fn(unsigned long ignored)
{
H
Heiko Carstens 已提交
299 300
	if (ptf(PTF_CHECK))
		topology_schedule_update();
301 302 303 304 305 306 307 308 309 310 311
	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);
}

312
static int __init early_parse_topology(char *p)
313
{
314 315 316 317
	if (strncmp(p, "on", 2))
		return 0;
	topology_enabled = 1;
	return 0;
318
}
319
early_param("topology", early_parse_topology);
320 321 322 323 324

static int __init init_topology_update(void)
{
	int rc;

325
	rc = 0;
H
Heiko Carstens 已提交
326 327
	if (!machine_has_topology) {
		topology_update_polarization_simple();
328
		goto out;
H
Heiko Carstens 已提交
329 330
	}
	init_timer_deferrable(&topology_timer);
331
	set_topology_timer();
332 333 334
out:
	update_cpu_core_map();
	return rc;
335 336 337
}
__initcall(init_topology_update);

338 339 340 341 342 343 344 345 346 347 348 349 350 351
static void alloc_masks(struct tl_info *info, struct mask_info *mask, int offset)
{
	int i, nr_masks;

	nr_masks = info->mag[NR_MAG - offset];
	for (i = 0; i < info->mnest - offset; i++)
		nr_masks *= info->mag[NR_MAG - offset - 1 - i];
	nr_masks = max(nr_masks, 1);
	for (i = 0; i < nr_masks; i++) {
		mask->next = alloc_bootmem(sizeof(struct mask_info));
		mask = mask->next;
	}
}

352 353 354 355 356 357 358 359 360 361 362 363 364 365
void __init s390_init_cpu_topology(void)
{
	unsigned long long facility_bits;
	struct tl_info *info;
	int i;

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

	tl_info = alloc_bootmem_pages(PAGE_SIZE);
	info = tl_info;
366
	store_topology(info);
367
	pr_info("The CPU configuration topology of the machine is:");
368 369 370
	for (i = 0; i < NR_MAG; i++)
		printk(" %d", info->mag[i]);
	printk(" / %d\n", info->mnest);
371 372 373 374
	alloc_masks(info, &core_info, 2);
#ifdef CONFIG_SCHED_BOOK
	alloc_masks(info, &book_info, 3);
#endif
375
}