topology.c 6.9 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
#include <asm/delay.h>
#include <asm/s390_ext.h>
#include <asm/sysinfo.h>

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

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

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

42
static struct mask_info core_info;
43
cpumask_t cpu_core_map[NR_CPUS];
44
unsigned char cpu_core_id[NR_CPUS];
45

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

	cpus_clear(mask);
57
	if (!topology_enabled || !MACHINE_HAS_TOPOLOGY)
58
		return cpu_possible_map;
59 60 61
	while (info) {
		if (cpu_isset(cpu, info->mask)) {
			mask = info->mask;
62 63
			break;
		}
64
		info = info->next;
65 66 67 68 69 70
	}
	if (cpus_empty(mask))
		mask = cpumask_of_cpu(cpu);
	return mask;
}

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

76 77 78
	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))
79 80 81
	{
		unsigned int rcpu, lcpu;

82
		rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin;
83
		for_each_present_cpu(lcpu) {
84 85 86 87 88 89 90 91 92
			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;
93 94 95 96
		}
	}
}

97
static void clear_masks(void)
98
{
99
	struct mask_info *info;
100

101 102 103 104 105 106 107 108 109 110
	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;
111
	}
112
#endif
113 114
}

115
static union topology_entry *next_tle(union topology_entry *tle)
116
{
117 118 119
	if (!tle->nl)
		return (union topology_entry *)((struct topology_cpu *)tle + 1);
	return (union topology_entry *)((struct topology_container *)tle + 1);
120 121
}

122
static void tl_to_cores(struct sysinfo_15_1_x *info)
123
{
124 125 126 127 128 129
#ifdef CONFIG_SCHED_BOOK
	struct mask_info *book = &book_info;
#else
	struct mask_info *book = NULL;
#endif
	struct mask_info *core = &core_info;
130
	union topology_entry *tle, *end;
131

132

H
Heiko Carstens 已提交
133
	spin_lock_irq(&topology_lock);
134
	clear_masks();
H
Heiko Carstens 已提交
135
	tle = info->tle;
136
	end = (union topology_entry *)((unsigned long)info + info->length);
137 138
	while (tle < end) {
		switch (tle->nl) {
139
#ifdef CONFIG_SCHED_BOOK
140
		case 2:
141 142
			book = book->next;
			book->id = tle->container.id;
143
			break;
144
#endif
145 146
		case 1:
			core = core->next;
147
			core->id = tle->container.id;
148 149
			break;
		case 0:
150
			add_cpus_to_mask(&tle->cpu, book, core);
151 152
			break;
		default:
153
			clear_masks();
154
			goto out;
155 156 157
		}
		tle = next_tle(tle);
	}
158
out:
H
Heiko Carstens 已提交
159
	spin_unlock_irq(&topology_lock);
160 161
}

H
Heiko Carstens 已提交
162 163 164 165 166
static void topology_update_polarization_simple(void)
{
	int cpu;

	mutex_lock(&smp_cpu_state_mutex);
167
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
168 169 170 171 172
		smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
	mutex_unlock(&smp_cpu_state_mutex);
}

static int ptf(unsigned long fc)
173 174 175 176 177 178 179 180
{
	int rc;

	asm volatile(
		"	.insn	rre,0xb9a20000,%1,%1\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (rc)
H
Heiko Carstens 已提交
181 182 183 184 185 186 187 188 189
		: "d" (fc)  : "cc");
	return rc;
}

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

190
	if (!MACHINE_HAS_TOPOLOGY)
H
Heiko Carstens 已提交
191 192 193 194 195 196 197
		return -EOPNOTSUPP;
	if (fc)
		rc = ptf(PTF_VERTICAL);
	else
		rc = ptf(PTF_HORIZONTAL);
	if (rc)
		return -EBUSY;
198
	for_each_possible_cpu(cpu)
H
Heiko Carstens 已提交
199
		smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
200 201 202
	return rc;
}

203 204
static void update_cpu_core_map(void)
{
205
	unsigned long flags;
206 207
	int cpu;

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	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);
228 229
}

230
int arch_update_cpu_topology(void)
231
{
232
	struct sysinfo_15_1_x *info = tl_info;
233 234 235
	struct sys_device *sysdev;
	int cpu;

236
	if (!MACHINE_HAS_TOPOLOGY) {
237
		update_cpu_core_map();
H
Heiko Carstens 已提交
238
		topology_update_polarization_simple();
239
		return 0;
H
Heiko Carstens 已提交
240
	}
241
	store_topology(info);
242
	tl_to_cores(info);
243
	update_cpu_core_map();
244 245 246 247
	for_each_online_cpu(cpu) {
		sysdev = get_cpu_sysdev(cpu);
		kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
	}
248
	return 1;
249 250
}

251 252
static void topology_work_fn(struct work_struct *work)
{
253
	rebuild_sched_domains();
254 255
}

H
Heiko Carstens 已提交
256 257 258 259 260
void topology_schedule_update(void)
{
	schedule_work(&topology_work);
}

261 262
static void topology_timer_fn(unsigned long ignored)
{
H
Heiko Carstens 已提交
263 264
	if (ptf(PTF_CHECK))
		topology_schedule_update();
265 266 267 268 269 270 271 272 273 274 275
	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);
}

276
static int __init early_parse_topology(char *p)
277
{
H
Heiko Carstens 已提交
278
	if (strncmp(p, "off", 3))
279
		return 0;
H
Heiko Carstens 已提交
280
	topology_enabled = 0;
281
	return 0;
282
}
283
early_param("topology", early_parse_topology);
284 285 286 287 288

static int __init init_topology_update(void)
{
	int rc;

289
	rc = 0;
290
	if (!MACHINE_HAS_TOPOLOGY) {
H
Heiko Carstens 已提交
291
		topology_update_polarization_simple();
292
		goto out;
H
Heiko Carstens 已提交
293 294
	}
	init_timer_deferrable(&topology_timer);
295
	set_topology_timer();
296 297 298
out:
	update_cpu_core_map();
	return rc;
299 300 301
}
__initcall(init_topology_update);

302 303
static void alloc_masks(struct sysinfo_15_1_x *info, struct mask_info *mask,
			int offset)
304 305 306
{
	int i, nr_masks;

307
	nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
308
	for (i = 0; i < info->mnest - offset; i++)
309
		nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
310 311 312 313 314 315 316
	nr_masks = max(nr_masks, 1);
	for (i = 0; i < nr_masks; i++) {
		mask->next = alloc_bootmem(sizeof(struct mask_info));
		mask = mask->next;
	}
}

317 318
void __init s390_init_cpu_topology(void)
{
319
	struct sysinfo_15_1_x *info;
320 321
	int i;

322
	if (!MACHINE_HAS_TOPOLOGY)
323 324 325
		return;
	tl_info = alloc_bootmem_pages(PAGE_SIZE);
	info = tl_info;
326
	store_topology(info);
327
	pr_info("The CPU configuration topology of the machine is:");
328
	for (i = 0; i < TOPOLOGY_NR_MAG; i++)
329 330
		printk(" %d", info->mag[i]);
	printk(" / %d\n", info->mnest);
331 332 333 334
	alloc_masks(info, &core_info, 2);
#ifdef CONFIG_SCHED_BOOK
	alloc_masks(info, &book_info, 3);
#endif
335
}