blob: 5728c5bd44a8241b0d2fd3c79c770154194501ba [file] [log] [blame]
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * Copyright IBM Corp. 2007, 2011
Heiko Carstensdbd70fb2008-04-17 07:46:12 +02003 * 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/workqueue.h>
Heiko Carstens83a24e32011-12-27 11:27:09 +010010#include <linux/cpuset.h>
11#include <linux/device.h>
Heiko Carstens80020fb2013-01-07 13:58:38 +010012#include <linux/export.h>
Heiko Carstens83a24e32011-12-27 11:27:09 +010013#include <linux/kernel.h>
14#include <linux/sched.h>
Heiko Carstens83a24e32011-12-27 11:27:09 +010015#include <linux/delay.h>
Heiko Carstensd05d15d2015-02-04 14:21:31 +010016#include <linux/init.h>
17#include <linux/slab.h>
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020018#include <linux/cpu.h>
19#include <linux/smp.h>
Heiko Carstens83a24e32011-12-27 11:27:09 +010020#include <linux/mm.h>
Heiko Carstens78609132012-09-03 14:11:32 +020021#include <asm/sysinfo.h>
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020022
Heiko Carstensc10fde02008-04-17 07:46:13 +020023#define PTF_HORIZONTAL (0UL)
24#define PTF_VERTICAL (1UL)
25#define PTF_CHECK (2UL)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020026
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020027struct mask_info {
28 struct mask_info *next;
Heiko Carstens10d38582010-05-17 10:00:12 +020029 unsigned char id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020030 cpumask_t mask;
31};
32
Heiko Carstensd1e575082012-11-12 10:03:25 +010033static void set_topology_timer(void);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020034static void topology_work_fn(struct work_struct *work);
Heiko Carstensc30f91b2010-10-25 16:10:53 +020035static struct sysinfo_15_1_x *tl_info;
Heiko Carstensd1e575082012-11-12 10:03:25 +010036
37static int topology_enabled = 1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020038static DECLARE_WORK(topology_work, topology_work_fn);
Heiko Carstensd1e575082012-11-12 10:03:25 +010039
40/* topology_lock protects the socket and book linked lists */
Heiko Carstens74af2832008-11-14 18:18:07 +010041static DEFINE_SPINLOCK(topology_lock);
Heiko Carstensd1e575082012-11-12 10:03:25 +010042static struct mask_info socket_info;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020043static struct mask_info book_info;
Heiko Carstensd1e575082012-11-12 10:03:25 +010044
Heiko Carstensda0c6362015-02-04 14:48:25 +010045DEFINE_PER_CPU(struct cpu_topology_s390, cpu_topology);
46EXPORT_PER_CPU_SYMBOL_GPL(cpu_topology);
Heiko Carstens83a24e32011-12-27 11:27:09 +010047
Heiko Carstens4cb14bc2010-08-31 10:28:18 +020048static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020049{
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020050 cpumask_t mask;
51
Heiko Carstensd1e575082012-11-12 10:03:25 +010052 cpumask_copy(&mask, cpumask_of(cpu));
53 if (!topology_enabled || !MACHINE_HAS_TOPOLOGY)
Heiko Carstens0b527832010-10-29 16:50:38 +020054 return mask;
Heiko Carstensd1e575082012-11-12 10:03:25 +010055 for (; info; info = info->next) {
56 if (cpumask_test_cpu(cpu, &info->mask))
57 return info->mask;
Heiko Carstens0b527832010-10-29 16:50:38 +020058 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020059 return mask;
60}
61
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010062static cpumask_t cpu_thread_map(unsigned int cpu)
63{
64 cpumask_t mask;
65 int i;
66
67 cpumask_copy(&mask, cpumask_of(cpu));
68 if (!topology_enabled || !MACHINE_HAS_TOPOLOGY)
69 return mask;
70 cpu -= cpu % (smp_cpu_mtid + 1);
71 for (i = 0; i <= smp_cpu_mtid; i++)
72 if (cpu_present(cpu + i))
73 cpumask_set_cpu(cpu + i, &mask);
74 return mask;
75}
76
77static struct mask_info *add_cpus_to_mask(struct topology_core *tl_core,
Heiko Carstensf6bf1a82011-11-14 11:19:08 +010078 struct mask_info *book,
Heiko Carstensd1e575082012-11-12 10:03:25 +010079 struct mask_info *socket,
80 int one_socket_per_cpu)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020081{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010082 unsigned int core;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020083
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010084 for_each_set_bit(core, &tl_core->mask[0], TOPOLOGY_CORE_BITS) {
85 unsigned int rcore;
86 int lcpu, i;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +020087
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010088 rcore = TOPOLOGY_CORE_BITS - 1 - core + tl_core->origin;
89 lcpu = smp_find_processor_id(rcore << smp_cpu_mt_shift);
Heiko Carstensd1e575082012-11-12 10:03:25 +010090 if (lcpu < 0)
91 continue;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010092 for (i = 0; i <= smp_cpu_mtid; i++) {
Heiko Carstensda0c6362015-02-04 14:48:25 +010093 per_cpu(cpu_topology, lcpu + i).book_id = book->id;
94 per_cpu(cpu_topology, lcpu + i).core_id = rcore;
95 per_cpu(cpu_topology, lcpu + i).thread_id = lcpu + i;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +010096 cpumask_set_cpu(lcpu + i, &book->mask);
97 cpumask_set_cpu(lcpu + i, &socket->mask);
98 if (one_socket_per_cpu)
Heiko Carstensda0c6362015-02-04 14:48:25 +010099 per_cpu(cpu_topology, lcpu + i).socket_id = rcore;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100100 else
Heiko Carstensda0c6362015-02-04 14:48:25 +0100101 per_cpu(cpu_topology, lcpu + i).socket_id = socket->id;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100102 smp_cpu_set_polarization(lcpu + i, tl_core->pp);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200103 }
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100104 if (one_socket_per_cpu)
105 socket = socket->next;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200106 }
Heiko Carstensd1e575082012-11-12 10:03:25 +0100107 return socket;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200108}
109
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200110static void clear_masks(void)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200111{
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200112 struct mask_info *info;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200113
Heiko Carstensd1e575082012-11-12 10:03:25 +0100114 info = &socket_info;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200115 while (info) {
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200116 cpumask_clear(&info->mask);
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200117 info = info->next;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200118 }
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200119 info = &book_info;
120 while (info) {
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200121 cpumask_clear(&info->mask);
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200122 info = info->next;
123 }
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200124}
125
Heiko Carstensc30f91b2010-10-25 16:10:53 +0200126static union topology_entry *next_tle(union topology_entry *tle)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200127{
Heiko Carstensc30f91b2010-10-25 16:10:53 +0200128 if (!tle->nl)
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100129 return (union topology_entry *)((struct topology_core *)tle + 1);
Heiko Carstensc30f91b2010-10-25 16:10:53 +0200130 return (union topology_entry *)((struct topology_container *)tle + 1);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200131}
132
Heiko Carstensd1e575082012-11-12 10:03:25 +0100133static void __tl_to_masks_generic(struct sysinfo_15_1_x *info)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200134{
Heiko Carstensd1e575082012-11-12 10:03:25 +0100135 struct mask_info *socket = &socket_info;
Heiko Carstens83a24e32011-12-27 11:27:09 +0100136 struct mask_info *book = &book_info;
Heiko Carstensc30f91b2010-10-25 16:10:53 +0200137 union topology_entry *tle, *end;
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200138
Heiko Carstensc10fde02008-04-17 07:46:13 +0200139 tle = info->tle;
Heiko Carstensc30f91b2010-10-25 16:10:53 +0200140 end = (union topology_entry *)((unsigned long)info + info->length);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200141 while (tle < end) {
142 switch (tle->nl) {
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200143 case 2:
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200144 book = book->next;
145 book->id = tle->container.id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200146 break;
147 case 1:
Heiko Carstensd1e575082012-11-12 10:03:25 +0100148 socket = socket->next;
149 socket->id = tle->container.id;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200150 break;
151 case 0:
Heiko Carstensd1e575082012-11-12 10:03:25 +0100152 add_cpus_to_mask(&tle->cpu, book, socket, 0);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200153 break;
154 default:
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200155 clear_masks();
Heiko Carstens4baeb962011-12-27 11:27:12 +0100156 return;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200157 }
158 tle = next_tle(tle);
159 }
Heiko Carstens4baeb962011-12-27 11:27:12 +0100160}
161
Heiko Carstensd1e575082012-11-12 10:03:25 +0100162static void __tl_to_masks_z10(struct sysinfo_15_1_x *info)
Heiko Carstens4baeb962011-12-27 11:27:12 +0100163{
Heiko Carstensd1e575082012-11-12 10:03:25 +0100164 struct mask_info *socket = &socket_info;
Heiko Carstens4baeb962011-12-27 11:27:12 +0100165 struct mask_info *book = &book_info;
166 union topology_entry *tle, *end;
167
168 tle = info->tle;
169 end = (union topology_entry *)((unsigned long)info + info->length);
170 while (tle < end) {
171 switch (tle->nl) {
172 case 1:
173 book = book->next;
174 book->id = tle->container.id;
175 break;
176 case 0:
Heiko Carstensd1e575082012-11-12 10:03:25 +0100177 socket = add_cpus_to_mask(&tle->cpu, book, socket, 1);
Heiko Carstens4baeb962011-12-27 11:27:12 +0100178 break;
179 default:
180 clear_masks();
181 return;
182 }
183 tle = next_tle(tle);
184 }
185}
186
Heiko Carstensd1e575082012-11-12 10:03:25 +0100187static void tl_to_masks(struct sysinfo_15_1_x *info)
Heiko Carstens4baeb962011-12-27 11:27:12 +0100188{
189 struct cpuid cpu_id;
190
Heiko Carstens4baeb962011-12-27 11:27:12 +0100191 spin_lock_irq(&topology_lock);
Heiko Carstensd1e575082012-11-12 10:03:25 +0100192 get_cpu_id(&cpu_id);
Heiko Carstens4baeb962011-12-27 11:27:12 +0100193 clear_masks();
194 switch (cpu_id.machine) {
195 case 0x2097:
196 case 0x2098:
Heiko Carstensd1e575082012-11-12 10:03:25 +0100197 __tl_to_masks_z10(info);
Heiko Carstens4baeb962011-12-27 11:27:12 +0100198 break;
199 default:
Heiko Carstensd1e575082012-11-12 10:03:25 +0100200 __tl_to_masks_generic(info);
Heiko Carstens4baeb962011-12-27 11:27:12 +0100201 }
Heiko Carstens74af2832008-11-14 18:18:07 +0100202 spin_unlock_irq(&topology_lock);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200203}
204
Heiko Carstensc10fde02008-04-17 07:46:13 +0200205static void topology_update_polarization_simple(void)
206{
207 int cpu;
208
209 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens54390502008-12-25 13:37:57 +0100210 for_each_possible_cpu(cpu)
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200211 smp_cpu_set_polarization(cpu, POLARIZATION_HRZ);
Heiko Carstensc10fde02008-04-17 07:46:13 +0200212 mutex_unlock(&smp_cpu_state_mutex);
213}
214
215static int ptf(unsigned long fc)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200216{
217 int rc;
218
219 asm volatile(
220 " .insn rre,0xb9a20000,%1,%1\n"
221 " ipm %0\n"
222 " srl %0,28\n"
223 : "=d" (rc)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200224 : "d" (fc) : "cc");
225 return rc;
226}
227
228int topology_set_cpu_management(int fc)
229{
Heiko Carstens83a24e32011-12-27 11:27:09 +0100230 int cpu, rc;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200231
Heiko Carstens9186d7a2010-10-25 16:10:52 +0200232 if (!MACHINE_HAS_TOPOLOGY)
Heiko Carstensc10fde02008-04-17 07:46:13 +0200233 return -EOPNOTSUPP;
234 if (fc)
235 rc = ptf(PTF_VERTICAL);
236 else
237 rc = ptf(PTF_HORIZONTAL);
238 if (rc)
239 return -EBUSY;
Heiko Carstens54390502008-12-25 13:37:57 +0100240 for_each_possible_cpu(cpu)
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200241 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200242 return rc;
243}
244
Heiko Carstensd1e575082012-11-12 10:03:25 +0100245static void update_cpu_masks(void)
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200246{
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200247 unsigned long flags;
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200248 int cpu;
249
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200250 spin_lock_irqsave(&topology_lock, flags);
251 for_each_possible_cpu(cpu) {
Heiko Carstensda0c6362015-02-04 14:48:25 +0100252 per_cpu(cpu_topology, cpu).thread_mask = cpu_thread_map(cpu);
253 per_cpu(cpu_topology, cpu).core_mask = cpu_group_map(&socket_info, cpu);
254 per_cpu(cpu_topology, cpu).book_mask = cpu_group_map(&book_info, cpu);
Heiko Carstensd1e575082012-11-12 10:03:25 +0100255 if (!MACHINE_HAS_TOPOLOGY) {
Heiko Carstensda0c6362015-02-04 14:48:25 +0100256 per_cpu(cpu_topology, cpu).thread_id = cpu;
257 per_cpu(cpu_topology, cpu).core_id = cpu;
258 per_cpu(cpu_topology, cpu).socket_id = cpu;
259 per_cpu(cpu_topology, cpu).book_id = cpu;
Heiko Carstensd1e575082012-11-12 10:03:25 +0100260 }
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200261 }
262 spin_unlock_irqrestore(&topology_lock, flags);
263}
264
Heiko Carstens96f4a702010-10-25 16:10:54 +0200265void store_topology(struct sysinfo_15_1_x *info)
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200266{
Heiko Carstensfade4dc2012-09-04 14:26:03 +0200267 if (topology_max_mnest >= 3)
268 stsi(info, 15, 1, 3);
269 else
270 stsi(info, 15, 1, 2);
Heiko Carstensd00aa4e2008-04-30 13:38:40 +0200271}
272
Heiko Carstensee79d1b2008-12-09 18:49:50 +0100273int arch_update_cpu_topology(void)
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200274{
Heiko Carstensc30f91b2010-10-25 16:10:53 +0200275 struct sysinfo_15_1_x *info = tl_info;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800276 struct device *dev;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200277 int cpu;
278
Heiko Carstens9186d7a2010-10-25 16:10:52 +0200279 if (!MACHINE_HAS_TOPOLOGY) {
Heiko Carstensd1e575082012-11-12 10:03:25 +0100280 update_cpu_masks();
Heiko Carstensc10fde02008-04-17 07:46:13 +0200281 topology_update_polarization_simple();
Heiko Carstensee79d1b2008-12-09 18:49:50 +0100282 return 0;
Heiko Carstensc10fde02008-04-17 07:46:13 +0200283 }
Heiko Carstens4cb14bc2010-08-31 10:28:18 +0200284 store_topology(info);
Heiko Carstensd1e575082012-11-12 10:03:25 +0100285 tl_to_masks(info);
286 update_cpu_masks();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200287 for_each_online_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800288 dev = get_cpu_device(cpu);
289 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200290 }
Heiko Carstensee79d1b2008-12-09 18:49:50 +0100291 return 1;
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200292}
293
Heiko Carstensfd781fa2008-04-30 13:38:41 +0200294static void topology_work_fn(struct work_struct *work)
295{
Heiko Carstensf414f5f2008-12-25 13:37:59 +0100296 rebuild_sched_domains();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200297}
298
Heiko Carstensc10fde02008-04-17 07:46:13 +0200299void topology_schedule_update(void)
300{
301 schedule_work(&topology_work);
302}
303
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200304static void topology_timer_fn(unsigned long ignored)
305{
Heiko Carstensc10fde02008-04-17 07:46:13 +0200306 if (ptf(PTF_CHECK))
307 topology_schedule_update();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200308 set_topology_timer();
309}
310
Heiko Carstensd68bddb2011-12-27 11:27:16 +0100311static struct timer_list topology_timer =
312 TIMER_DEFERRED_INITIALIZER(topology_timer_fn, 0, 0);
313
314static atomic_t topology_poll = ATOMIC_INIT(0);
315
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200316static void set_topology_timer(void)
317{
Heiko Carstensd68bddb2011-12-27 11:27:16 +0100318 if (atomic_add_unless(&topology_poll, -1, 0))
319 mod_timer(&topology_timer, jiffies + HZ / 10);
320 else
321 mod_timer(&topology_timer, jiffies + HZ * 60);
322}
323
324void topology_expect_change(void)
325{
326 if (!MACHINE_HAS_TOPOLOGY)
327 return;
328 /* This is racy, but it doesn't matter since it is just a heuristic.
329 * Worst case is that we poll in a higher frequency for a bit longer.
330 */
331 if (atomic_read(&topology_poll) > 60)
332 return;
333 atomic_add(60, &topology_poll);
334 set_topology_timer();
Heiko Carstensdbd70fb2008-04-17 07:46:12 +0200335}
336
Heiko Carstens83a24e32011-12-27 11:27:09 +0100337static int cpu_management;
338
Linus Torvalds72f31882012-01-09 08:11:13 -0800339static ssize_t dispatching_show(struct device *dev,
340 struct device_attribute *attr,
Heiko Carstens83a24e32011-12-27 11:27:09 +0100341 char *buf)
342{
343 ssize_t count;
344
345 mutex_lock(&smp_cpu_state_mutex);
346 count = sprintf(buf, "%d\n", cpu_management);
347 mutex_unlock(&smp_cpu_state_mutex);
348 return count;
349}
350
Linus Torvalds72f31882012-01-09 08:11:13 -0800351static ssize_t dispatching_store(struct device *dev,
352 struct device_attribute *attr,
Heiko Carstens83a24e32011-12-27 11:27:09 +0100353 const char *buf,
354 size_t count)
355{
356 int val, rc;
357 char delim;
358
359 if (sscanf(buf, "%d %c", &val, &delim) != 1)
360 return -EINVAL;
361 if (val != 0 && val != 1)
362 return -EINVAL;
363 rc = 0;
364 get_online_cpus();
365 mutex_lock(&smp_cpu_state_mutex);
366 if (cpu_management == val)
367 goto out;
368 rc = topology_set_cpu_management(val);
Heiko Carstensd68bddb2011-12-27 11:27:16 +0100369 if (rc)
370 goto out;
371 cpu_management = val;
372 topology_expect_change();
Heiko Carstens83a24e32011-12-27 11:27:09 +0100373out:
374 mutex_unlock(&smp_cpu_state_mutex);
375 put_online_cpus();
376 return rc ? rc : count;
377}
Linus Torvalds72f31882012-01-09 08:11:13 -0800378static DEVICE_ATTR(dispatching, 0644, dispatching_show,
Heiko Carstens83a24e32011-12-27 11:27:09 +0100379 dispatching_store);
380
Linus Torvalds72f31882012-01-09 08:11:13 -0800381static ssize_t cpu_polarization_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
Heiko Carstens83a24e32011-12-27 11:27:09 +0100383{
384 int cpu = dev->id;
385 ssize_t count;
386
387 mutex_lock(&smp_cpu_state_mutex);
Heiko Carstens50ab9a92012-09-04 17:36:16 +0200388 switch (smp_cpu_get_polarization(cpu)) {
Heiko Carstens83a24e32011-12-27 11:27:09 +0100389 case POLARIZATION_HRZ:
390 count = sprintf(buf, "horizontal\n");
391 break;
392 case POLARIZATION_VL:
393 count = sprintf(buf, "vertical:low\n");
394 break;
395 case POLARIZATION_VM:
396 count = sprintf(buf, "vertical:medium\n");
397 break;
398 case POLARIZATION_VH:
399 count = sprintf(buf, "vertical:high\n");
400 break;
401 default:
402 count = sprintf(buf, "unknown\n");
403 break;
404 }
405 mutex_unlock(&smp_cpu_state_mutex);
406 return count;
407}
Linus Torvalds72f31882012-01-09 08:11:13 -0800408static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
Heiko Carstens83a24e32011-12-27 11:27:09 +0100409
410static struct attribute *topology_cpu_attrs[] = {
Linus Torvalds72f31882012-01-09 08:11:13 -0800411 &dev_attr_polarization.attr,
Heiko Carstens83a24e32011-12-27 11:27:09 +0100412 NULL,
413};
414
415static struct attribute_group topology_cpu_attr_group = {
416 .attrs = topology_cpu_attrs,
417};
418
419int topology_cpu_init(struct cpu *cpu)
420{
Linus Torvalds72f31882012-01-09 08:11:13 -0800421 return sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
Heiko Carstens83a24e32011-12-27 11:27:09 +0100422}
423
Heiko Carstens3ddb1b72015-03-16 12:44:10 +0100424static const struct cpumask *cpu_thread_mask(int cpu)
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100425{
Heiko Carstensda0c6362015-02-04 14:48:25 +0100426 return &per_cpu(cpu_topology, cpu).thread_mask;
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100427}
428
429
Vincent Guittot2dfd7472014-04-11 11:44:38 +0200430const struct cpumask *cpu_coregroup_mask(int cpu)
431{
Heiko Carstensda0c6362015-02-04 14:48:25 +0100432 return &per_cpu(cpu_topology, cpu).core_mask;
Vincent Guittot2dfd7472014-04-11 11:44:38 +0200433}
434
435static const struct cpumask *cpu_book_mask(int cpu)
436{
Heiko Carstensda0c6362015-02-04 14:48:25 +0100437 return &per_cpu(cpu_topology, cpu).book_mask;
Vincent Guittot2dfd7472014-04-11 11:44:38 +0200438}
439
Heiko Carstensd05d15d2015-02-04 14:21:31 +0100440static int __init early_parse_topology(char *p)
441{
442 if (strncmp(p, "off", 3))
443 return 0;
444 topology_enabled = 0;
445 return 0;
446}
447early_param("topology", early_parse_topology);
448
Vincent Guittot2dfd7472014-04-11 11:44:38 +0200449static struct sched_domain_topology_level s390_topology[] = {
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100450 { cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
Vincent Guittot2dfd7472014-04-11 11:44:38 +0200451 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
452 { cpu_book_mask, SD_INIT_NAME(BOOK) },
453 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
454 { NULL, },
455};
456
Heiko Carstensd05d15d2015-02-04 14:21:31 +0100457static void __init alloc_masks(struct sysinfo_15_1_x *info,
458 struct mask_info *mask, int offset)
459{
460 int i, nr_masks;
461
462 nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
463 for (i = 0; i < info->mnest - offset; i++)
464 nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
465 nr_masks = max(nr_masks, 1);
466 for (i = 0; i < nr_masks; i++) {
467 mask->next = kzalloc(sizeof(*mask->next), GFP_KERNEL);
468 mask = mask->next;
469 }
470}
471
472static int __init s390_topology_init(void)
473{
474 struct sysinfo_15_1_x *info;
475 int i;
476
477 if (!MACHINE_HAS_TOPOLOGY)
478 return 0;
479 tl_info = (struct sysinfo_15_1_x *)__get_free_page(GFP_KERNEL);
480 info = tl_info;
481 store_topology(info);
482 pr_info("The CPU configuration topology of the machine is:");
483 for (i = 0; i < TOPOLOGY_NR_MAG; i++)
484 printk(KERN_CONT " %d", info->mag[i]);
485 printk(KERN_CONT " / %d\n", info->mnest);
486 alloc_masks(info, &socket_info, 1);
487 alloc_masks(info, &book_info, 2);
488 set_sched_topology(s390_topology);
489 return 0;
490}
491early_initcall(s390_topology_init);
492
Heiko Carstens83a24e32011-12-27 11:27:09 +0100493static int __init topology_init(void)
494{
Martin Schwidefsky48e9a6c2014-09-24 16:37:20 +0200495 if (MACHINE_HAS_TOPOLOGY)
496 set_topology_timer();
497 else
Heiko Carstens83a24e32011-12-27 11:27:09 +0100498 topology_update_polarization_simple();
Linus Torvalds72f31882012-01-09 08:11:13 -0800499 return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching);
Heiko Carstens83a24e32011-12-27 11:27:09 +0100500}
501device_initcall(topology_init);