blob: fec228cd0163d69d60c9b5a12e3938596d0b04f2 [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
28/* default to snooze disabled */
29DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
30
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;
63 unsigned int *val;
64 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
73 val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
74 NULL);
75 if (!smt_snooze_cmdline && val) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080076 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 per_cpu(smt_snooze_delay, cpu) = *val;
78 }
79
Anton Blanchard69ed3322006-03-28 14:08:39 +110080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82__initcall(smt_setup);
83
84static int __init setup_smt_snooze_delay(char *str)
85{
86 unsigned int cpu;
87 int snooze;
88
89 if (!cpu_has_feature(CPU_FTR_SMT))
90 return 1;
91
92 smt_snooze_cmdline = 1;
93
94 if (get_option(&str, &snooze)) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080095 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 per_cpu(smt_snooze_delay, cpu) = snooze;
97 }
98
99 return 1;
100}
101__setup("smt-snooze-delay=", setup_smt_snooze_delay);
102
Michael Ellerman180a3362005-08-09 11:13:36 +1000103#endif /* CONFIG_PPC_MULTIPLATFORM */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * Enabling PMCs will slow partition context switch times so we only do
107 * it the first time we write to the PMCs.
108 */
109
110static DEFINE_PER_CPU(char, pmcs_enabled);
111
112void ppc64_enable_pmcs(void)
113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /* Only need to enable them once */
115 if (__get_cpu_var(pmcs_enabled))
116 return;
117
118 __get_cpu_var(pmcs_enabled) = 1;
119
Michael Ellerman180a3362005-08-09 11:13:36 +1000120 if (ppc_md.enable_pmcs)
121 ppc_md.enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123EXPORT_SYMBOL(ppc64_enable_pmcs);
124
125/* XXX convert to rusty's on_one_cpu */
126static unsigned long run_on_cpu(unsigned long cpu,
127 unsigned long (*func)(unsigned long),
128 unsigned long arg)
129{
130 cpumask_t old_affinity = current->cpus_allowed;
131 unsigned long ret;
132
133 /* should return -EINVAL to userspace */
134 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
135 return 0;
136
137 ret = func(arg);
138
139 set_cpus_allowed(current, old_affinity);
140
141 return ret;
142}
143
144#define SYSFS_PMCSETUP(NAME, ADDRESS) \
145static unsigned long read_##NAME(unsigned long junk) \
146{ \
147 return mfspr(ADDRESS); \
148} \
149static unsigned long write_##NAME(unsigned long val) \
150{ \
151 ppc64_enable_pmcs(); \
152 mtspr(ADDRESS, val); \
153 return 0; \
154} \
155static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
156{ \
157 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
158 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
159 return sprintf(buf, "%lx\n", val); \
160} \
161static ssize_t __attribute_used__ \
162 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
163{ \
164 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
165 unsigned long val; \
166 int ret = sscanf(buf, "%lx", &val); \
167 if (ret != 1) \
168 return -EINVAL; \
169 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
170 return count; \
171}
172
173SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
174SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
175SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
176SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
177SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
178SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
179SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
180SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
181SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
182SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
183SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
184SYSFS_PMCSETUP(purr, SPRN_PURR);
185
186static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
187static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
188static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
189static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
190static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
191static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
192static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
193static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
194static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
195static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
196static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
197static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
198
199static void register_cpu_online(unsigned int cpu)
200{
201 struct cpu *c = &per_cpu(cpu_devices, cpu);
202 struct sys_device *s = &c->sysdev;
203
204#ifndef CONFIG_PPC_ISERIES
205 if (cpu_has_feature(CPU_FTR_SMT))
206 sysdev_create_file(s, &attr_smt_snooze_delay);
207#endif
208
209 /* PMC stuff */
210
211 sysdev_create_file(s, &attr_mmcr0);
212 sysdev_create_file(s, &attr_mmcr1);
213
214 if (cpu_has_feature(CPU_FTR_MMCRA))
215 sysdev_create_file(s, &attr_mmcra);
216
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000217 if (cur_cpu_spec->num_pmcs >= 1)
218 sysdev_create_file(s, &attr_pmc1);
219 if (cur_cpu_spec->num_pmcs >= 2)
220 sysdev_create_file(s, &attr_pmc2);
221 if (cur_cpu_spec->num_pmcs >= 3)
222 sysdev_create_file(s, &attr_pmc3);
223 if (cur_cpu_spec->num_pmcs >= 4)
224 sysdev_create_file(s, &attr_pmc4);
225 if (cur_cpu_spec->num_pmcs >= 5)
226 sysdev_create_file(s, &attr_pmc5);
227 if (cur_cpu_spec->num_pmcs >= 6)
228 sysdev_create_file(s, &attr_pmc6);
229 if (cur_cpu_spec->num_pmcs >= 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 sysdev_create_file(s, &attr_pmc7);
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000231 if (cur_cpu_spec->num_pmcs >= 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sysdev_create_file(s, &attr_pmc8);
David Gibsond3d21762005-11-10 15:26:20 +1100233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (cpu_has_feature(CPU_FTR_SMT))
235 sysdev_create_file(s, &attr_purr);
236}
237
238#ifdef CONFIG_HOTPLUG_CPU
239static void unregister_cpu_online(unsigned int cpu)
240{
241 struct cpu *c = &per_cpu(cpu_devices, cpu);
242 struct sys_device *s = &c->sysdev;
243
244 BUG_ON(c->no_control);
245
246#ifndef CONFIG_PPC_ISERIES
247 if (cpu_has_feature(CPU_FTR_SMT))
248 sysdev_remove_file(s, &attr_smt_snooze_delay);
249#endif
250
251 /* PMC stuff */
252
253 sysdev_remove_file(s, &attr_mmcr0);
254 sysdev_remove_file(s, &attr_mmcr1);
255
256 if (cpu_has_feature(CPU_FTR_MMCRA))
257 sysdev_remove_file(s, &attr_mmcra);
258
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000259 if (cur_cpu_spec->num_pmcs >= 1)
260 sysdev_remove_file(s, &attr_pmc1);
261 if (cur_cpu_spec->num_pmcs >= 2)
262 sysdev_remove_file(s, &attr_pmc2);
263 if (cur_cpu_spec->num_pmcs >= 3)
264 sysdev_remove_file(s, &attr_pmc3);
265 if (cur_cpu_spec->num_pmcs >= 4)
266 sysdev_remove_file(s, &attr_pmc4);
267 if (cur_cpu_spec->num_pmcs >= 5)
268 sysdev_remove_file(s, &attr_pmc5);
269 if (cur_cpu_spec->num_pmcs >= 6)
270 sysdev_remove_file(s, &attr_pmc6);
271 if (cur_cpu_spec->num_pmcs >= 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sysdev_remove_file(s, &attr_pmc7);
Anton Blanchardfd5b4372005-09-06 14:47:49 +1000273 if (cur_cpu_spec->num_pmcs >= 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 sysdev_remove_file(s, &attr_pmc8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (cpu_has_feature(CPU_FTR_SMT))
277 sysdev_remove_file(s, &attr_purr);
278}
279#endif /* CONFIG_HOTPLUG_CPU */
280
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700281static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 unsigned long action, void *hcpu)
283{
284 unsigned int cpu = (unsigned int)(long)hcpu;
285
286 switch (action) {
287 case CPU_ONLINE:
288 register_cpu_online(cpu);
289 break;
290#ifdef CONFIG_HOTPLUG_CPU
291 case CPU_DEAD:
292 unregister_cpu_online(cpu);
293 break;
294#endif
295 }
296 return NOTIFY_OK;
297}
298
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700299static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 .notifier_call = sysfs_cpu_notify,
301};
302
303/* NUMA stuff */
304
305#ifdef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static void register_nodes(void)
307{
308 int i;
309
Yasunori Goto0fc44152006-06-27 02:53:38 -0700310 for (i = 0; i < MAX_NUMNODES; i++)
311 register_one_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700313
314int sysfs_add_device_to_node(struct sys_device *dev, int nid)
315{
316 struct node *node = &node_devices[nid];
317 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
318 kobject_name(&dev->kobj));
319}
320
321void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
322{
323 struct node *node = &node_devices[nid];
324 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#else
328static void register_nodes(void)
329{
330 return;
331}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#endif
334
Jeremy Kerr953039c2006-05-01 12:16:12 -0700335EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
336EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/* Only valid if CPU is present. */
339static ssize_t show_physical_id(struct sys_device *dev, char *buf)
340{
341 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
342
343 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
344}
345static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
346
347static int __init topology_init(void)
348{
349 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 register_nodes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 register_cpu_notifier(&sysfs_cpu_nb);
353
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800354 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct cpu *c = &per_cpu(cpu_devices, cpu);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * For now, we just see if the system supports making
359 * the RTAS calls for CPU hotplug. But, there may be a
360 * more comprehensive way to do this for an individual
361 * CPU. For instance, the boot cpu might never be valid
362 * for hotplugging.
363 */
364 if (!ppc_md.cpu_die)
365 c->no_control = 1;
366
367 if (cpu_online(cpu) || (c->no_control == 0)) {
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700368 register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 sysdev_create_file(&c->sysdev, &attr_physical_id);
371 }
372
373 if (cpu_online(cpu))
374 register_cpu_online(cpu);
375 }
376
377 return 0;
378}
379__initcall(topology_init);