blob: 69004411a93dd40248691b07e3f296d6cf9c1598 [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
Martin Schwidefsky395d31d2008-12-25 13:39:50 +01006#define KMSG_COMPONENT "cpu"
7#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
8
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02009#include <linux/kernel.h>
10#include <linux/mm.h>
11#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/bootmem.h>
14#include <linux/sched.h>
15#include <linux/workqueue.h>
16#include <linux/cpu.h>
17#include <linux/smp.h>
Heiko Carstensf414f5f2008-12-25 13:37:59 +010018#include <linux/cpuset.h>
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020019#include <asm/delay.h>
20#include <asm/s390_ext.h>
21#include <asm/sysinfo.h>
22
23#define CPU_BITS 64
Heiko Carstensc10fde02008-04-17 07:46:13 +020024#define NR_MAG 6
25
26#define PTF_HORIZONTAL (0UL)
27#define PTF_VERTICAL (1UL)
28#define PTF_CHECK (2UL)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020029
30struct tl_cpu {
Heiko Carstensc10fde02008-04-17 07:46:13 +020031 unsigned char reserved0[4];
32 unsigned char :6;
33 unsigned char pp:2;
34 unsigned char reserved1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020035 unsigned short origin;
36 unsigned long mask[CPU_BITS / BITS_PER_LONG];
37};
38
39struct tl_container {
Heiko Carstens10d38582010-05-17 10:00:12 +020040 unsigned char reserved[7];
41 unsigned char id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020042};
43
44union tl_entry {
45 unsigned char nl;
46 struct tl_cpu cpu;
47 struct tl_container container;
48};
49
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020050struct tl_info {
51 unsigned char reserved0[2];
52 unsigned short length;
53 unsigned char mag[NR_MAG];
54 unsigned char reserved1;
55 unsigned char mnest;
56 unsigned char reserved2[4];
57 union tl_entry tle[0];
58};
59
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020060struct mask_info {
61 struct mask_info *next;
Heiko Carstens10d38582010-05-17 10:00:12 +020062 unsigned char id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020063 cpumask_t mask;
64};
65
Heiko Carstensc9af3fa2010-10-25 16:10:43 +020066static int topology_enabled = 1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020067static void topology_work_fn(struct work_struct *work);
68static struct tl_info *tl_info;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020069static struct timer_list topology_timer;
70static void set_topology_timer(void);
71static DECLARE_WORK(topology_work, topology_work_fn);
Heiko Carstens74af2832008-11-14 18:18:07 +010072/* topology_lock protects the core linked list */
73static DEFINE_SPINLOCK(topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020074
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020075static struct mask_info core_info;
Heiko Carstensd00aa4e2008-04-30 13:38:40 +020076cpumask_t cpu_core_map[NR_CPUS];
Heiko Carstens10d38582010-05-17 10:00:12 +020077unsigned char cpu_core_id[NR_CPUS];
Heiko Carstensd00aa4e2008-04-30 13:38:40 +020078
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020079#ifdef CONFIG_SCHED_BOOK
80static struct mask_info book_info;
81cpumask_t cpu_book_map[NR_CPUS];
82unsigned char cpu_book_id[NR_CPUS];
83#endif
84
85static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020086{
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020087 cpumask_t mask;
88
89 cpus_clear(mask);
Heiko Carstens9186d7a2010-10-25 16:10:52 +020090 if (!topology_enabled || !MACHINE_HAS_TOPOLOGY)
Heiko Carstens54390502008-12-25 13:37:57 +010091 return cpu_possible_map;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020092 while (info) {
93 if (cpu_isset(cpu, info->mask)) {
94 mask = info->mask;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020095 break;
96 }
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020097 info = info->next;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020098 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020099 if (cpus_empty(mask))
100 mask = cpumask_of_cpu(cpu);
101 return mask;
102}
103
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200104static void add_cpus_to_mask(struct tl_cpu *tl_cpu, struct mask_info *book,
105 struct mask_info *core)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200106{
107 unsigned int cpu;
108
109 for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
110 cpu < CPU_BITS;
111 cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
112 {
113 unsigned int rcpu, lcpu;
114
115 rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
116 for_each_present_cpu(lcpu) {
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200117 if (cpu_logical_map(lcpu) != rcpu)
118 continue;
119#ifdef CONFIG_SCHED_BOOK
120 cpu_set(lcpu, book->mask);
121 cpu_book_id[lcpu] = book->id;
122#endif
123 cpu_set(lcpu, core->mask);
124 cpu_core_id[lcpu] = core->id;
125 smp_cpu_polarization[lcpu] = tl_cpu->pp;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200126 }
127 }
128}
129
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200130static void clear_masks(void)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200131{
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200132 struct mask_info *info;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200133
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200134 info = &core_info;
135 while (info) {
136 cpus_clear(info->mask);
137 info = info->next;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200138 }
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200139#ifdef CONFIG_SCHED_BOOK
140 info = &book_info;
141 while (info) {
142 cpus_clear(info->mask);
143 info = info->next;
144 }
145#endif
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200146}
147
148static union tl_entry *next_tle(union tl_entry *tle)
149{
150 if (tle->nl)
151 return (union tl_entry *)((struct tl_container *)tle + 1);
152 else
153 return (union tl_entry *)((struct tl_cpu *)tle + 1);
154}
155
156static void tl_to_cores(struct tl_info *info)
157{
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200158#ifdef CONFIG_SCHED_BOOK
159 struct mask_info *book = &book_info;
160#else
161 struct mask_info *book = NULL;
162#endif
163 struct mask_info *core = &core_info;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200164 union tl_entry *tle, *end;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200165
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200166
Heiko Carstens74af2832008-11-14 18:18:07 +0100167 spin_lock_irq(&topology_lock);
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200168 clear_masks();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200169 tle = info->tle;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200170 end = (union tl_entry *)((unsigned long)info + info->length);
171 while (tle < end) {
172 switch (tle->nl) {
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200173#ifdef CONFIG_SCHED_BOOK
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200174 case 2:
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200175 book = book->next;
176 book->id = tle->container.id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200177 break;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200178#endif
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200179 case 1:
180 core = core->next;
Heiko Carstens10d38582010-05-17 10:00:12 +0200181 core->id = tle->container.id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200182 break;
183 case 0:
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200184 add_cpus_to_mask(&tle->cpu, book, core);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200185 break;
186 default:
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200187 clear_masks();
Julia Lawalld7015c12010-04-09 13:42:58 +0200188 goto out;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200189 }
190 tle = next_tle(tle);
191 }
Julia Lawalld7015c12010-04-09 13:42:58 +0200192out:
Heiko Carstens74af2832008-11-14 18:18:07 +0100193 spin_unlock_irq(&topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200194}
195
Heiko Carstensc10fde02008-04-17 07:46:13 +0200196static void topology_update_polarization_simple(void)
197{
198 int cpu;
199
200 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens54390502008-12-25 13:37:57 +0100201 for_each_possible_cpu(cpu)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200202 smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
203 mutex_unlock(&smp_cpu_state_mutex);
204}
205
206static int ptf(unsigned long fc)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200207{
208 int rc;
209
210 asm volatile(
211 " .insn rre,0xb9a20000,%1,%1\n"
212 " ipm %0\n"
213 " srl %0,28\n"
214 : "=d" (rc)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200215 : "d" (fc) : "cc");
216 return rc;
217}
218
219int topology_set_cpu_management(int fc)
220{
221 int cpu;
222 int rc;
223
Heiko Carstens9186d7a2010-10-25 16:10:52 +0200224 if (!MACHINE_HAS_TOPOLOGY)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200225 return -EOPNOTSUPP;
226 if (fc)
227 rc = ptf(PTF_VERTICAL);
228 else
229 rc = ptf(PTF_HORIZONTAL);
230 if (rc)
231 return -EBUSY;
Heiko Carstens54390502008-12-25 13:37:57 +0100232 for_each_possible_cpu(cpu)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200233 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200234 return rc;
235}
236
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200237static void update_cpu_core_map(void)
238{
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200239 unsigned long flags;
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200240 int cpu;
241
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200242 spin_lock_irqsave(&topology_lock, flags);
243 for_each_possible_cpu(cpu) {
244 cpu_core_map[cpu] = cpu_group_map(&core_info, cpu);
245#ifdef CONFIG_SCHED_BOOK
246 cpu_book_map[cpu] = cpu_group_map(&book_info, cpu);
247#endif
248 }
249 spin_unlock_irqrestore(&topology_lock, flags);
250}
251
252static void store_topology(struct tl_info *info)
253{
254#ifdef CONFIG_SCHED_BOOK
255 int rc;
256
257 rc = stsi(info, 15, 1, 3);
258 if (rc != -ENOSYS)
259 return;
260#endif
261 stsi(info, 15, 1, 2);
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200262}
263
Heiko Carstensee79d1b2008-12-09 18:49:50 +0100264int arch_update_cpu_topology(void)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200265{
266 struct tl_info *info = tl_info;
267 struct sys_device *sysdev;
268 int cpu;
269
Heiko Carstens9186d7a2010-10-25 16:10:52 +0200270 if (!MACHINE_HAS_TOPOLOGY) {
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200271 update_cpu_core_map();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200272 topology_update_polarization_simple();
Heiko Carstensee79d1b2008-12-09 18:49:50 +0100273 return 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200274 }
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200275 store_topology(info);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200276 tl_to_cores(info);
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200277 update_cpu_core_map();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200278 for_each_online_cpu(cpu) {
279 sysdev = get_cpu_sysdev(cpu);
280 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
281 }
Heiko Carstensee79d1b2008-12-09 18:49:50 +0100282 return 1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200283}
284
Heiko Carstensfd781fa2008-04-30 13:38:41 +0200285static void topology_work_fn(struct work_struct *work)
286{
Heiko Carstensf414f5f2008-12-25 13:37:59 +0100287 rebuild_sched_domains();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200288}
289
Heiko Carstensc10fde02008-04-17 07:46:13 +0200290void topology_schedule_update(void)
291{
292 schedule_work(&topology_work);
293}
294
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200295static void topology_timer_fn(unsigned long ignored)
296{
Heiko Carstensc10fde02008-04-17 07:46:13 +0200297 if (ptf(PTF_CHECK))
298 topology_schedule_update();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200299 set_topology_timer();
300}
301
302static void set_topology_timer(void)
303{
304 topology_timer.function = topology_timer_fn;
305 topology_timer.data = 0;
306 topology_timer.expires = jiffies + 60 * HZ;
307 add_timer(&topology_timer);
308}
309
Heiko Carstens2b1a61f2008-12-25 13:39:23 +0100310static int __init early_parse_topology(char *p)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200311{
Heiko Carstensc9af3fa2010-10-25 16:10:43 +0200312 if (strncmp(p, "off", 3))
Heiko Carstens2b1a61f2008-12-25 13:39:23 +0100313 return 0;
Heiko Carstensc9af3fa2010-10-25 16:10:43 +0200314 topology_enabled = 0;
Heiko Carstens2b1a61f2008-12-25 13:39:23 +0100315 return 0;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200316}
Heiko Carstens2b1a61f2008-12-25 13:39:23 +0100317early_param("topology", early_parse_topology);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200318
319static int __init init_topology_update(void)
320{
321 int rc;
322
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200323 rc = 0;
Heiko Carstens9186d7a2010-10-25 16:10:52 +0200324 if (!MACHINE_HAS_TOPOLOGY) {
Heiko Carstensc10fde02008-04-17 07:46:13 +0200325 topology_update_polarization_simple();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200326 goto out;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200327 }
328 init_timer_deferrable(&topology_timer);
Heiko Carstens349f1b62008-12-25 13:39:24 +0100329 set_topology_timer();
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200330out:
331 update_cpu_core_map();
332 return rc;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200333}
334__initcall(init_topology_update);
335
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200336static void alloc_masks(struct tl_info *info, struct mask_info *mask, int offset)
337{
338 int i, nr_masks;
339
340 nr_masks = info->mag[NR_MAG - offset];
341 for (i = 0; i < info->mnest - offset; i++)
342 nr_masks *= info->mag[NR_MAG - offset - 1 - i];
343 nr_masks = max(nr_masks, 1);
344 for (i = 0; i < nr_masks; i++) {
345 mask->next = alloc_bootmem(sizeof(struct mask_info));
346 mask = mask->next;
347 }
348}
349
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200350void __init s390_init_cpu_topology(void)
351{
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200352 struct tl_info *info;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200353 int i;
354
Heiko Carstens9186d7a2010-10-25 16:10:52 +0200355 if (!MACHINE_HAS_TOPOLOGY)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200356 return;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200357 tl_info = alloc_bootmem_pages(PAGE_SIZE);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200358 info = tl_info;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200359 store_topology(info);
Martin Schwidefsky395d31d2008-12-25 13:39:50 +0100360 pr_info("The CPU configuration topology of the machine is:");
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200361 for (i = 0; i < NR_MAG; i++)
362 printk(" %d", info->mag[i]);
363 printk(" / %d\n", info->mnest);
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200364 alloc_masks(info, &core_info, 2);
365#ifdef CONFIG_SCHED_BOOK
366 alloc_masks(info, &book_info, 3);
367#endif
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200368}