blob: 400ab2b946e760964bd164f282ec1f270579ac6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/sysdev.h>
2#include <linux/cpu.h>
3#include <linux/smp.h>
4#include <linux/percpu.h>
5#include <linux/init.h>
6#include <linux/sched.h>
7#include <linux/module.h>
8#include <linux/nodemask.h>
9#include <linux/cpumask.h>
10#include <linux/notifier.h>
11
12#include <asm/current.h>
13#include <asm/processor.h>
14#include <asm/cputable.h>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100015#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/hvcall.h>
17#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/paca.h>
19#include <asm/lppaca.h>
20#include <asm/machdep.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110021#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static DEFINE_PER_CPU(struct cpu, cpu_devices);
24
25/* SMT stuff */
26
27#ifdef CONFIG_PPC_MULTIPLATFORM
Anton Blanchard0ddd3e72006-09-22 20:30:14 +100028/* Time in microseconds we delay before sleeping in the idle loop */
29DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
32 size_t count)
33{
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
35 ssize_t ret;
36 unsigned long snooze;
37
38 ret = sscanf(buf, "%lu", &snooze);
39 if (ret != 1)
40 return -EINVAL;
41
42 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
43
44 return count;
45}
46
47static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
48{
49 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
50
51 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
52}
53
54static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
55 store_smt_snooze_delay);
56
57/* Only parse OF options if the matching cmdline option was not specified */
58static int smt_snooze_cmdline;
59
60static int __init smt_setup(void)
61{
62 struct device_node *options;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100063 const unsigned int *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 unsigned int cpu;
65
66 if (!cpu_has_feature(CPU_FTR_SMT))
Anton Blanchard69ed3322006-03-28 14:08:39 +110067 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 options = find_path_device("/options");
70 if (!options)
Anton Blanchard69ed3322006-03-28 14:08:39 +110071 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100073 val = get_property(options, "ibm,smt-snooze-delay", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (!smt_snooze_cmdline && val) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080075 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 per_cpu(smt_snooze_delay, cpu) = *val;
77 }
78
Anton Blanchard69ed3322006-03-28 14:08:39 +110079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81__initcall(smt_setup);
82
83static int __init setup_smt_snooze_delay(char *str)
84{
85 unsigned int cpu;
86 int snooze;
87
88 if (!cpu_has_feature(CPU_FTR_SMT))
89 return 1;
90
91 smt_snooze_cmdline = 1;
92
93 if (get_option(&str, &snooze)) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080094 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 per_cpu(smt_snooze_delay, cpu) = snooze;
96 }
97
98 return 1;
99}
100__setup("smt-snooze-delay=", setup_smt_snooze_delay);
101
Michael Ellerman180a3362005-08-09 11:13:36 +1000102#endif /* CONFIG_PPC_MULTIPLATFORM */
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Enabling PMCs will slow partition context switch times so we only do
106 * it the first time we write to the PMCs.
107 */
108
109static DEFINE_PER_CPU(char, pmcs_enabled);
110
111void ppc64_enable_pmcs(void)
112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Only need to enable them once */
114 if (__get_cpu_var(pmcs_enabled))
115 return;
116
117 __get_cpu_var(pmcs_enabled) = 1;
118
Michael Ellerman180a3362005-08-09 11:13:36 +1000119 if (ppc_md.enable_pmcs)
120 ppc_md.enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122EXPORT_SYMBOL(ppc64_enable_pmcs);
123
124/* XXX convert to rusty's on_one_cpu */
125static unsigned long run_on_cpu(unsigned long cpu,
126 unsigned long (*func)(unsigned long),
127 unsigned long arg)
128{
129 cpumask_t old_affinity = current->cpus_allowed;
130 unsigned long ret;
131
132 /* should return -EINVAL to userspace */
133 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
134 return 0;
135
136 ret = func(arg);
137
138 set_cpus_allowed(current, old_affinity);
139
140 return ret;
141}
142
143#define SYSFS_PMCSETUP(NAME, ADDRESS) \
144static unsigned long read_##NAME(unsigned long junk) \
145{ \
146 return mfspr(ADDRESS); \
147} \
148static unsigned long write_##NAME(unsigned long val) \
149{ \
150 ppc64_enable_pmcs(); \
151 mtspr(ADDRESS, val); \
152 return 0; \
153} \
154static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
155{ \
156 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
157 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
158 return sprintf(buf, "%lx\n", val); \
159} \
160static ssize_t __attribute_used__ \
161 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
162{ \
163 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
164 unsigned long val; \
165 int ret = sscanf(buf, "%lx", &val); \
166 if (ret != 1) \
167 return -EINVAL; \
168 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
169 return count; \
170}
171
172SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
173SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
174SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
175SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
176SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
177SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
178SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
179SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
180SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
181SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
182SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
183SYSFS_PMCSETUP(purr, SPRN_PURR);
Anton Blanchardf0509822006-12-08 17:51:13 +1100184SYSFS_PMCSETUP(spurr, SPRN_SPURR);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100185SYSFS_PMCSETUP(dscr, SPRN_DSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
188static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
189static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
190static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
191static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
192static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
193static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
194static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
195static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
196static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
197static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
198static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
Anton Blanchardf0509822006-12-08 17:51:13 +1100199static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100200static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202static void register_cpu_online(unsigned int cpu)
203{
204 struct cpu *c = &per_cpu(cpu_devices, cpu);
205 struct sys_device *s = &c->sysdev;
206
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100207 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
208 cpu_has_feature(CPU_FTR_SMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sysdev_create_file(s, &attr_smt_snooze_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* PMC stuff */
212
213 sysdev_create_file(s, &attr_mmcr0);
214 sysdev_create_file(s, &attr_mmcr1);
215
216 if (cpu_has_feature(CPU_FTR_MMCRA))
217 sysdev_create_file(s, &attr_mmcra);
218
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000219 if (cur_cpu_spec->num_pmcs >= 1)
220 sysdev_create_file(s, &attr_pmc1);
221 if (cur_cpu_spec->num_pmcs >= 2)
222 sysdev_create_file(s, &attr_pmc2);
223 if (cur_cpu_spec->num_pmcs >= 3)
224 sysdev_create_file(s, &attr_pmc3);
225 if (cur_cpu_spec->num_pmcs >= 4)
226 sysdev_create_file(s, &attr_pmc4);
227 if (cur_cpu_spec->num_pmcs >= 5)
228 sysdev_create_file(s, &attr_pmc5);
229 if (cur_cpu_spec->num_pmcs >= 6)
230 sysdev_create_file(s, &attr_pmc6);
231 if (cur_cpu_spec->num_pmcs >= 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sysdev_create_file(s, &attr_pmc7);
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000233 if (cur_cpu_spec->num_pmcs >= 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sysdev_create_file(s, &attr_pmc8);
David Gibsond3d21762005-11-10 15:26:20 +1100235
Michael Neulingafd05422006-07-28 13:58:37 +1000236 if (cpu_has_feature(CPU_FTR_PURR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 sysdev_create_file(s, &attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100238
Anton Blanchardf0509822006-12-08 17:51:13 +1100239 if (cpu_has_feature(CPU_FTR_SPURR))
240 sysdev_create_file(s, &attr_spurr);
241
Anton Blanchard4c1985572006-12-08 17:46:58 +1100242 if (cpu_has_feature(CPU_FTR_DSCR))
243 sysdev_create_file(s, &attr_dscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246#ifdef CONFIG_HOTPLUG_CPU
247static void unregister_cpu_online(unsigned int cpu)
248{
249 struct cpu *c = &per_cpu(cpu_devices, cpu);
250 struct sys_device *s = &c->sysdev;
251
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100252 BUG_ON(!c->hotpluggable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100254 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
255 cpu_has_feature(CPU_FTR_SMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 sysdev_remove_file(s, &attr_smt_snooze_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* PMC stuff */
259
260 sysdev_remove_file(s, &attr_mmcr0);
261 sysdev_remove_file(s, &attr_mmcr1);
262
263 if (cpu_has_feature(CPU_FTR_MMCRA))
264 sysdev_remove_file(s, &attr_mmcra);
265
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000266 if (cur_cpu_spec->num_pmcs >= 1)
267 sysdev_remove_file(s, &attr_pmc1);
268 if (cur_cpu_spec->num_pmcs >= 2)
269 sysdev_remove_file(s, &attr_pmc2);
270 if (cur_cpu_spec->num_pmcs >= 3)
271 sysdev_remove_file(s, &attr_pmc3);
272 if (cur_cpu_spec->num_pmcs >= 4)
273 sysdev_remove_file(s, &attr_pmc4);
274 if (cur_cpu_spec->num_pmcs >= 5)
275 sysdev_remove_file(s, &attr_pmc5);
276 if (cur_cpu_spec->num_pmcs >= 6)
277 sysdev_remove_file(s, &attr_pmc6);
278 if (cur_cpu_spec->num_pmcs >= 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sysdev_remove_file(s, &attr_pmc7);
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000280 if (cur_cpu_spec->num_pmcs >= 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sysdev_remove_file(s, &attr_pmc8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Michael Neulingafd05422006-07-28 13:58:37 +1000283 if (cpu_has_feature(CPU_FTR_PURR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sysdev_remove_file(s, &attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100285
Anton Blanchardf0509822006-12-08 17:51:13 +1100286 if (cpu_has_feature(CPU_FTR_SPURR))
287 sysdev_remove_file(s, &attr_spurr);
288
Anton Blanchard4c1985572006-12-08 17:46:58 +1100289 if (cpu_has_feature(CPU_FTR_DSCR))
290 sysdev_remove_file(s, &attr_dscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292#endif /* CONFIG_HOTPLUG_CPU */
293
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700294static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned long action, void *hcpu)
296{
297 unsigned int cpu = (unsigned int)(long)hcpu;
298
299 switch (action) {
300 case CPU_ONLINE:
301 register_cpu_online(cpu);
302 break;
303#ifdef CONFIG_HOTPLUG_CPU
304 case CPU_DEAD:
305 unregister_cpu_online(cpu);
306 break;
307#endif
308 }
309 return NOTIFY_OK;
310}
311
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700312static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .notifier_call = sysfs_cpu_notify,
314};
315
Christian Krafft0344c6c2006-10-24 18:31:24 +0200316static DEFINE_MUTEX(cpu_mutex);
317
318int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
319{
320 int cpu;
321
322 mutex_lock(&cpu_mutex);
323
324 for_each_possible_cpu(cpu) {
325 sysdev_create_file(get_cpu_sysdev(cpu), attr);
326 }
327
328 mutex_unlock(&cpu_mutex);
329 return 0;
330}
331EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
332
333int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
334{
335 int cpu;
336 struct sys_device *sysdev;
337
338 mutex_lock(&cpu_mutex);
339
340 for_each_possible_cpu(cpu) {
341 sysdev = get_cpu_sysdev(cpu);
342 sysfs_create_group(&sysdev->kobj, attrs);
343 }
344
345 mutex_unlock(&cpu_mutex);
346 return 0;
347}
348EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
349
350
351void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
352{
353 int cpu;
354
355 mutex_lock(&cpu_mutex);
356
357 for_each_possible_cpu(cpu) {
358 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
359 }
360
361 mutex_unlock(&cpu_mutex);
362}
363EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
364
365void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
366{
367 int cpu;
368 struct sys_device *sysdev;
369
370 mutex_lock(&cpu_mutex);
371
372 for_each_possible_cpu(cpu) {
373 sysdev = get_cpu_sysdev(cpu);
374 sysfs_remove_group(&sysdev->kobj, attrs);
375 }
376
377 mutex_unlock(&cpu_mutex);
378}
379EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
380
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/* NUMA stuff */
383
384#ifdef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static void register_nodes(void)
386{
387 int i;
388
Yasunori Goto0fc44152006-06-27 02:53:38 -0700389 for (i = 0; i < MAX_NUMNODES; i++)
390 register_one_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700392
393int sysfs_add_device_to_node(struct sys_device *dev, int nid)
394{
395 struct node *node = &node_devices[nid];
396 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
397 kobject_name(&dev->kobj));
398}
399
400void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
401{
402 struct node *node = &node_devices[nid];
403 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#else
407static void register_nodes(void)
408{
409 return;
410}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#endif
413
Jeremy Kerr953039c2006-05-01 12:16:12 -0700414EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
415EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/* Only valid if CPU is present. */
418static ssize_t show_physical_id(struct sys_device *dev, char *buf)
419{
420 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
421
422 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
423}
424static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
425
426static int __init topology_init(void)
427{
428 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 register_nodes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 register_cpu_notifier(&sysfs_cpu_nb);
432
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800433 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct cpu *c = &per_cpu(cpu_devices, cpu);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * For now, we just see if the system supports making
438 * the RTAS calls for CPU hotplug. But, there may be a
439 * more comprehensive way to do this for an individual
440 * CPU. For instance, the boot cpu might never be valid
441 * for hotplugging.
442 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100443 if (ppc_md.cpu_die)
444 c->hotpluggable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100446 if (cpu_online(cpu) || c->hotpluggable) {
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700447 register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 sysdev_create_file(&c->sysdev, &attr_physical_id);
450 }
451
452 if (cpu_online(cpu))
453 register_cpu_online(cpu);
454 }
455
456 return 0;
457}
458__initcall(topology_init);