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

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

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

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

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

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

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

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

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

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

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

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

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

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

131

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

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

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

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

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

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

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

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

207 208 209 210 211 212 213 214 215 216
	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);
}

217
void store_topology(struct sysinfo_15_1_x *info)
218 219 220 221 222 223 224 225 226
{
#ifdef CONFIG_SCHED_BOOK
	int rc;

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

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

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

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

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

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

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

static int __init init_topology_update(void)
{
	int rc;

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

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

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

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

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