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
#include <asm/delay.h>

H
Heiko Carstens 已提交
21 22 23
#define PTF_HORIZONTAL	(0UL)
#define PTF_VERTICAL	(1UL)
#define PTF_CHECK	(2UL)
24

25 26
struct mask_info {
	struct mask_info *next;
27
	unsigned char id;
28 29 30
	cpumask_t mask;
};

H
Heiko Carstens 已提交
31
static int topology_enabled = 1;
32
static void topology_work_fn(struct work_struct *work);
33
static struct sysinfo_15_1_x *tl_info;
34 35 36
static struct timer_list topology_timer;
static void set_topology_timer(void);
static DECLARE_WORK(topology_work, topology_work_fn);
H
Heiko Carstens 已提交
37 38
/* topology_lock protects the core linked list */
static DEFINE_SPINLOCK(topology_lock);
39

40
static struct mask_info core_info;
41
cpumask_t cpu_core_map[NR_CPUS];
42
unsigned char cpu_core_id[NR_CPUS];
43

44 45 46 47 48 49 50
#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)
51 52 53
{
	cpumask_t mask;

54
	cpumask_clear(&mask);
55 56 57 58
	if (!topology_enabled || !MACHINE_HAS_TOPOLOGY) {
		cpumask_copy(&mask, cpumask_of(cpu));
		return mask;
	}
59
	while (info) {
60
		if (cpumask_test_cpu(cpu, &info->mask)) {
61
			mask = info->mask;
62 63
			break;
		}
64
		info = info->next;
65
	}
66 67
	if (cpumask_empty(&mask))
		cpumask_copy(&mask, cpumask_of(cpu));
68 69 70
	return mask;
}

71 72 73 74
static struct mask_info *add_cpus_to_mask(struct topology_cpu *tl_cpu,
					  struct mask_info *book,
					  struct mask_info *core,
					  int z10)
75 76 77
{
	unsigned int cpu;

78 79 80
	for (cpu = find_first_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS);
	     cpu < TOPOLOGY_CPU_BITS;
	     cpu = find_next_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS, cpu + 1))
81 82 83
	{
		unsigned int rcpu, lcpu;

84
		rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin;
85
		for_each_present_cpu(lcpu) {
86 87 88
			if (cpu_logical_map(lcpu) != rcpu)
				continue;
#ifdef CONFIG_SCHED_BOOK
89
			cpumask_set_cpu(lcpu, &book->mask);
90 91
			cpu_book_id[lcpu] = book->id;
#endif
92
			cpumask_set_cpu(lcpu, &core->mask);
93 94 95 96 97 98
			if (z10) {
				cpu_core_id[lcpu] = rcpu;
				core = core->next;
			} else {
				cpu_core_id[lcpu] = core->id;
			}
99
			smp_cpu_polarization[lcpu] = tl_cpu->pp;
100 101
		}
	}
102
	return core;
103 104
}

105
static void clear_masks(void)
106
{
107
	struct mask_info *info;
108

109 110
	info = &core_info;
	while (info) {
111
		cpumask_clear(&info->mask);
112 113 114 115 116
		info = info->next;
	}
#ifdef CONFIG_SCHED_BOOK
	info = &book_info;
	while (info) {
117
		cpumask_clear(&info->mask);
118
		info = info->next;
119
	}
120
#endif
121 122
}

123
static union topology_entry *next_tle(union topology_entry *tle)
124
{
125 126 127
	if (!tle->nl)
		return (union topology_entry *)((struct topology_cpu *)tle + 1);
	return (union topology_entry *)((struct topology_container *)tle + 1);
128 129
}

130
static void tl_to_cores(struct sysinfo_15_1_x *info)
131
{
132 133
#ifdef CONFIG_SCHED_BOOK
	struct mask_info *book = &book_info;
134
	struct cpuid cpu_id;
135 136 137 138
#else
	struct mask_info *book = NULL;
#endif
	struct mask_info *core = &core_info;
139
	union topology_entry *tle, *end;
140
	int z10 = 0;
141

142 143 144 145
#ifdef CONFIG_SCHED_BOOK
	get_cpu_id(&cpu_id);
	z10 = cpu_id.machine == 0x2097 || cpu_id.machine == 0x2098;
#endif
H
Heiko Carstens 已提交
146
	spin_lock_irq(&topology_lock);
147
	clear_masks();
H
Heiko Carstens 已提交
148
	tle = info->tle;
149
	end = (union topology_entry *)((unsigned long)info + info->length);
150
	while (tle < end) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#ifdef CONFIG_SCHED_BOOK
		if (z10) {
			switch (tle->nl) {
			case 1:
				book = book->next;
				book->id = tle->container.id;
				break;
			case 0:
				core = add_cpus_to_mask(&tle->cpu, book, core, z10);
				break;
			default:
				clear_masks();
				goto out;
			}
			tle = next_tle(tle);
			continue;
		}
#endif
169
		switch (tle->nl) {
170
#ifdef CONFIG_SCHED_BOOK
171
		case 2:
172 173
			book = book->next;
			book->id = tle->container.id;
174
			break;
175
#endif
176 177
		case 1:
			core = core->next;
178
			core->id = tle->container.id;
179 180
			break;
		case 0:
181
			add_cpus_to_mask(&tle->cpu, book, core, z10);
182 183
			break;
		default:
184
			clear_masks();
185
			goto out;
186 187 188
		}
		tle = next_tle(tle);
	}
189
out:
H
Heiko Carstens 已提交
190
	spin_unlock_irq(&topology_lock);
191 192
}

H
Heiko Carstens 已提交
193 194 195 196 197
static void topology_update_polarization_simple(void)
{
	int cpu;

	mutex_lock(&smp_cpu_state_mutex);
198
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
199 200 201 202 203
		smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
	mutex_unlock(&smp_cpu_state_mutex);
}

static int ptf(unsigned long fc)
204 205 206 207 208 209 210 211
{
	int rc;

	asm volatile(
		"	.insn	rre,0xb9a20000,%1,%1\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (rc)
H
Heiko Carstens 已提交
212 213 214 215 216 217 218 219 220
		: "d" (fc)  : "cc");
	return rc;
}

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

221
	if (!MACHINE_HAS_TOPOLOGY)
H
Heiko Carstens 已提交
222 223 224 225 226 227 228
		return -EOPNOTSUPP;
	if (fc)
		rc = ptf(PTF_VERTICAL);
	else
		rc = ptf(PTF_HORIZONTAL);
	if (rc)
		return -EBUSY;
229
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
230
		smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
231 232 233
	return rc;
}

234 235
static void update_cpu_core_map(void)
{
236
	unsigned long flags;
237 238
	int cpu;

239 240 241 242 243 244 245 246 247 248
	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);
}

249
void store_topology(struct sysinfo_15_1_x *info)
250 251 252 253 254 255 256 257 258
{
#ifdef CONFIG_SCHED_BOOK
	int rc;

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

261
int arch_update_cpu_topology(void)
262
{
263
	struct sysinfo_15_1_x *info = tl_info;
264 265 266
	struct sys_device *sysdev;
	int cpu;

267
	if (!MACHINE_HAS_TOPOLOGY) {
268
		update_cpu_core_map();
H
Heiko Carstens 已提交
269
		topology_update_polarization_simple();
270
		return 0;
H
Heiko Carstens 已提交
271
	}
272
	store_topology(info);
273
	tl_to_cores(info);
274
	update_cpu_core_map();
275 276 277 278
	for_each_online_cpu(cpu) {
		sysdev = get_cpu_sysdev(cpu);
		kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
	}
279
	return 1;
280 281
}

282 283
static void topology_work_fn(struct work_struct *work)
{
284
	rebuild_sched_domains();
285 286
}

H
Heiko Carstens 已提交
287 288 289 290 291
void topology_schedule_update(void)
{
	schedule_work(&topology_work);
}

292 293
static void topology_timer_fn(unsigned long ignored)
{
H
Heiko Carstens 已提交
294 295
	if (ptf(PTF_CHECK))
		topology_schedule_update();
296 297 298 299 300 301 302 303 304 305 306
	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);
}

307
static int __init early_parse_topology(char *p)
308
{
H
Heiko Carstens 已提交
309
	if (strncmp(p, "off", 3))
310
		return 0;
H
Heiko Carstens 已提交
311
	topology_enabled = 0;
312
	return 0;
313
}
314
early_param("topology", early_parse_topology);
315 316 317 318 319

static int __init init_topology_update(void)
{
	int rc;

320
	rc = 0;
321
	if (!MACHINE_HAS_TOPOLOGY) {
H
Heiko Carstens 已提交
322
		topology_update_polarization_simple();
323
		goto out;
H
Heiko Carstens 已提交
324 325
	}
	init_timer_deferrable(&topology_timer);
326
	set_topology_timer();
327 328 329
out:
	update_cpu_core_map();
	return rc;
330 331 332
}
__initcall(init_topology_update);

333 334
static void __init alloc_masks(struct sysinfo_15_1_x *info,
			       struct mask_info *mask, int offset)
335 336 337
{
	int i, nr_masks;

338
	nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
339
	for (i = 0; i < info->mnest - offset; i++)
340
		nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
341 342 343 344 345 346 347
	nr_masks = max(nr_masks, 1);
	for (i = 0; i < nr_masks; i++) {
		mask->next = alloc_bootmem(sizeof(struct mask_info));
		mask = mask->next;
	}
}

348 349
void __init s390_init_cpu_topology(void)
{
350
	struct sysinfo_15_1_x *info;
351 352
	int i;

353
	if (!MACHINE_HAS_TOPOLOGY)
354 355 356
		return;
	tl_info = alloc_bootmem_pages(PAGE_SIZE);
	info = tl_info;
357
	store_topology(info);
358
	pr_info("The CPU configuration topology of the machine is:");
359
	for (i = 0; i < TOPOLOGY_NR_MAG; i++)
360 361
		printk(" %d", info->mag[i]);
	printk(" / %d\n", info->mnest);
362
	alloc_masks(info, &core_info, 1);
363
#ifdef CONFIG_SCHED_BOOK
364
	alloc_masks(info, &book_info, 2);
365
#endif
366
}