blob: 71e6f56bfead1eda4da895f76c7fc50e586b86a8 [file] [log] [blame]
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02001/*
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02002 * Copyright IBM Corp. 2007
3 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
4 */
5
6#include <linux/kernel.h>
7#include <linux/mm.h>
8#include <linux/init.h>
9#include <linux/device.h>
10#include <linux/bootmem.h>
11#include <linux/sched.h>
12#include <linux/workqueue.h>
13#include <linux/cpu.h>
14#include <linux/smp.h>
Heiko Carstensf414f5f2008-12-25 13:37:59 +010015#include <linux/cpuset.h>
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020016#include <asm/delay.h>
17#include <asm/s390_ext.h>
18#include <asm/sysinfo.h>
19
20#define CPU_BITS 64
Heiko Carstensc10fde02008-04-17 07:46:13 +020021#define NR_MAG 6
22
23#define PTF_HORIZONTAL (0UL)
24#define PTF_VERTICAL (1UL)
25#define PTF_CHECK (2UL)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020026
27struct tl_cpu {
Heiko Carstensc10fde02008-04-17 07:46:13 +020028 unsigned char reserved0[4];
29 unsigned char :6;
30 unsigned char pp:2;
31 unsigned char reserved1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020032 unsigned short origin;
33 unsigned long mask[CPU_BITS / BITS_PER_LONG];
34};
35
36struct tl_container {
37 unsigned char reserved[8];
38};
39
40union tl_entry {
41 unsigned char nl;
42 struct tl_cpu cpu;
43 struct tl_container container;
44};
45
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020046struct tl_info {
47 unsigned char reserved0[2];
48 unsigned short length;
49 unsigned char mag[NR_MAG];
50 unsigned char reserved1;
51 unsigned char mnest;
52 unsigned char reserved2[4];
53 union tl_entry tle[0];
54};
55
56struct core_info {
57 struct core_info *next;
58 cpumask_t mask;
59};
60
Heiko Carstens2b1a61f2008-12-25 13:39:23 +010061static int topology_enabled;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020062static void topology_work_fn(struct work_struct *work);
63static struct tl_info *tl_info;
64static struct core_info core_info;
65static int machine_has_topology;
66static int machine_has_topology_irq;
67static struct timer_list topology_timer;
68static void set_topology_timer(void);
69static DECLARE_WORK(topology_work, topology_work_fn);
Heiko Carstens74af2832008-11-14 18:18:07 +010070/* topology_lock protects the core linked list */
71static DEFINE_SPINLOCK(topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020072
Heiko Carstensd00aa4e2008-04-30 13:38:40 +020073cpumask_t cpu_core_map[NR_CPUS];
74
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020075cpumask_t cpu_coregroup_map(unsigned int cpu)
76{
77 struct core_info *core = &core_info;
Heiko Carstens74af2832008-11-14 18:18:07 +010078 unsigned long flags;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020079 cpumask_t mask;
80
81 cpus_clear(mask);
Heiko Carstens2b1a61f2008-12-25 13:39:23 +010082 if (!topology_enabled || !machine_has_topology)
Heiko Carstens54390502008-12-25 13:37:57 +010083 return cpu_possible_map;
Heiko Carstens74af2832008-11-14 18:18:07 +010084 spin_lock_irqsave(&topology_lock, flags);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020085 while (core) {
86 if (cpu_isset(cpu, core->mask)) {
87 mask = core->mask;
88 break;
89 }
90 core = core->next;
91 }
Heiko Carstens74af2832008-11-14 18:18:07 +010092 spin_unlock_irqrestore(&topology_lock, flags);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020093 if (cpus_empty(mask))
94 mask = cpumask_of_cpu(cpu);
95 return mask;
96}
97
98static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
99{
100 unsigned int cpu;
101
102 for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
103 cpu < CPU_BITS;
104 cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
105 {
106 unsigned int rcpu, lcpu;
107
108 rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
109 for_each_present_cpu(lcpu) {
Heiko Carstensc10fde02008-04-17 07:46:13 +0200110 if (__cpu_logical_map[lcpu] == rcpu) {
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200111 cpu_set(lcpu, core->mask);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200112 smp_cpu_polarization[lcpu] = tl_cpu->pp;
113 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200114 }
115 }
116}
117
118static void clear_cores(void)
119{
120 struct core_info *core = &core_info;
121
122 while (core) {
123 cpus_clear(core->mask);
124 core = core->next;
125 }
126}
127
128static union tl_entry *next_tle(union tl_entry *tle)
129{
130 if (tle->nl)
131 return (union tl_entry *)((struct tl_container *)tle + 1);
132 else
133 return (union tl_entry *)((struct tl_cpu *)tle + 1);
134}
135
136static void tl_to_cores(struct tl_info *info)
137{
138 union tl_entry *tle, *end;
139 struct core_info *core = &core_info;
140
Heiko Carstens74af2832008-11-14 18:18:07 +0100141 spin_lock_irq(&topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200142 clear_cores();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200143 tle = info->tle;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200144 end = (union tl_entry *)((unsigned long)info + info->length);
145 while (tle < end) {
146 switch (tle->nl) {
147 case 5:
148 case 4:
149 case 3:
150 case 2:
151 break;
152 case 1:
153 core = core->next;
154 break;
155 case 0:
156 add_cpus_to_core(&tle->cpu, core);
157 break;
158 default:
159 clear_cores();
160 machine_has_topology = 0;
161 return;
162 }
163 tle = next_tle(tle);
164 }
Heiko Carstens74af2832008-11-14 18:18:07 +0100165 spin_unlock_irq(&topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200166}
167
Heiko Carstensc10fde02008-04-17 07:46:13 +0200168static void topology_update_polarization_simple(void)
169{
170 int cpu;
171
172 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens54390502008-12-25 13:37:57 +0100173 for_each_possible_cpu(cpu)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200174 smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
175 mutex_unlock(&smp_cpu_state_mutex);
176}
177
178static int ptf(unsigned long fc)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200179{
180 int rc;
181
182 asm volatile(
183 " .insn rre,0xb9a20000,%1,%1\n"
184 " ipm %0\n"
185 " srl %0,28\n"
186 : "=d" (rc)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200187 : "d" (fc) : "cc");
188 return rc;
189}
190
191int topology_set_cpu_management(int fc)
192{
193 int cpu;
194 int rc;
195
196 if (!machine_has_topology)
197 return -EOPNOTSUPP;
198 if (fc)
199 rc = ptf(PTF_VERTICAL);
200 else
201 rc = ptf(PTF_HORIZONTAL);
202 if (rc)
203 return -EBUSY;
Heiko Carstens54390502008-12-25 13:37:57 +0100204 for_each_possible_cpu(cpu)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200205 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200206 return rc;
207}
208
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200209static void update_cpu_core_map(void)
210{
211 int cpu;
212
Heiko Carstens54390502008-12-25 13:37:57 +0100213 for_each_possible_cpu(cpu)
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200214 cpu_core_map[cpu] = cpu_coregroup_map(cpu);
215}
216
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200217void arch_update_cpu_topology(void)
218{
219 struct tl_info *info = tl_info;
220 struct sys_device *sysdev;
221 int cpu;
222
Heiko Carstensc10fde02008-04-17 07:46:13 +0200223 if (!machine_has_topology) {
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200224 update_cpu_core_map();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200225 topology_update_polarization_simple();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200226 return;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200227 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200228 stsi(info, 15, 1, 2);
229 tl_to_cores(info);
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200230 update_cpu_core_map();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200231 for_each_online_cpu(cpu) {
232 sysdev = get_cpu_sysdev(cpu);
233 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
234 }
235}
236
Heiko Carstensfd781fa2008-04-30 13:38:41 +0200237static void topology_work_fn(struct work_struct *work)
238{
Heiko Carstensf414f5f2008-12-25 13:37:59 +0100239 rebuild_sched_domains();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200240}
241
Heiko Carstensc10fde02008-04-17 07:46:13 +0200242void topology_schedule_update(void)
243{
244 schedule_work(&topology_work);
245}
246
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200247static void topology_timer_fn(unsigned long ignored)
248{
Heiko Carstensc10fde02008-04-17 07:46:13 +0200249 if (ptf(PTF_CHECK))
250 topology_schedule_update();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200251 set_topology_timer();
252}
253
254static void set_topology_timer(void)
255{
256 topology_timer.function = topology_timer_fn;
257 topology_timer.data = 0;
258 topology_timer.expires = jiffies + 60 * HZ;
259 add_timer(&topology_timer);
260}
261
262static void topology_interrupt(__u16 code)
263{
264 schedule_work(&topology_work);
265}
266
Heiko Carstens2b1a61f2008-12-25 13:39:23 +0100267static int __init early_parse_topology(char *p)
268{
269 if (strncmp(p, "on", 2))
270 return 0;
271 topology_enabled = 1;
272 return 0;
273}
274early_param("topology", early_parse_topology);
275
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200276static int __init init_topology_update(void)
277{
278 int rc;
279
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200280 rc = 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200281 if (!machine_has_topology) {
282 topology_update_polarization_simple();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200283 goto out;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200284 }
285 init_timer_deferrable(&topology_timer);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200286 if (machine_has_topology_irq) {
287 rc = register_external_interrupt(0x2005, topology_interrupt);
288 if (rc)
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200289 goto out;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200290 ctl_set_bit(0, 8);
291 }
292 else
293 set_topology_timer();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200294out:
295 update_cpu_core_map();
296 return rc;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200297}
298__initcall(init_topology_update);
299
300void __init s390_init_cpu_topology(void)
301{
302 unsigned long long facility_bits;
303 struct tl_info *info;
304 struct core_info *core;
305 int nr_cores;
306 int i;
307
308 if (stfle(&facility_bits, 1) <= 0)
309 return;
310 if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
311 return;
312 machine_has_topology = 1;
313
314 if (facility_bits & (1ULL << 51))
315 machine_has_topology_irq = 1;
316
317 tl_info = alloc_bootmem_pages(PAGE_SIZE);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200318 info = tl_info;
319 stsi(info, 15, 1, 2);
320
321 nr_cores = info->mag[NR_MAG - 2];
322 for (i = 0; i < info->mnest - 2; i++)
323 nr_cores *= info->mag[NR_MAG - 3 - i];
324
325 printk(KERN_INFO "CPU topology:");
326 for (i = 0; i < NR_MAG; i++)
327 printk(" %d", info->mag[i]);
328 printk(" / %d\n", info->mnest);
329
330 core = &core_info;
331 for (i = 0; i < nr_cores; i++) {
332 core->next = alloc_bootmem(sizeof(struct core_info));
333 core = core->next;
334 if (!core)
335 goto error;
336 }
337 return;
338error:
339 machine_has_topology = 0;
340 machine_has_topology_irq = 0;
341}