Merge upstream 2.6.13-rc3 into ieee80211 branch of netdev-2.6.
[linux-2.6] / arch / ppc64 / kernel / sysfs.c
1 #include <linux/config.h>
2 #include <linux/sysdev.h>
3 #include <linux/cpu.h>
4 #include <linux/smp.h>
5 #include <linux/percpu.h>
6 #include <linux/init.h>
7 #include <linux/sched.h>
8 #include <linux/module.h>
9 #include <linux/nodemask.h>
10 #include <linux/cpumask.h>
11 #include <linux/notifier.h>
12
13 #include <asm/current.h>
14 #include <asm/processor.h>
15 #include <asm/cputable.h>
16 #include <asm/hvcall.h>
17 #include <asm/prom.h>
18 #include <asm/systemcfg.h>
19 #include <asm/paca.h>
20 #include <asm/lppaca.h>
21 #include <asm/machdep.h>
22
23 static DEFINE_PER_CPU(struct cpu, cpu_devices);
24
25 /* SMT stuff */
26
27 #ifdef CONFIG_PPC_MULTIPLATFORM
28 /* default to snooze disabled */
29 DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
30
31 static 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
47 static 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
54 static 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 */
58 static int smt_snooze_cmdline;
59
60 static 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))
67                 return 1;
68
69         options = find_path_device("/options");
70         if (!options)
71                 return 1;
72
73         val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
74                                            NULL);
75         if (!smt_snooze_cmdline && val) {
76                 for_each_cpu(cpu)
77                         per_cpu(smt_snooze_delay, cpu) = *val;
78         }
79
80         return 1;
81 }
82 __initcall(smt_setup);
83
84 static 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)) {
95                 for_each_cpu(cpu)
96                         per_cpu(smt_snooze_delay, cpu) = snooze;
97         }
98
99         return 1;
100 }
101 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
102
103 /*
104  * Enabling PMCs will slow partition context switch times so we only do
105  * it the first time we write to the PMCs.
106  */
107
108 static DEFINE_PER_CPU(char, pmcs_enabled);
109
110 void ppc64_enable_pmcs(void)
111 {
112         unsigned long hid0;
113 #ifdef CONFIG_PPC_PSERIES
114         unsigned long set, reset;
115 #endif /* CONFIG_PPC_PSERIES */
116
117         /* Only need to enable them once */
118         if (__get_cpu_var(pmcs_enabled))
119                 return;
120
121         __get_cpu_var(pmcs_enabled) = 1;
122
123         switch (systemcfg->platform) {
124         case PLATFORM_PSERIES:
125         case PLATFORM_POWERMAC:
126                 hid0 = mfspr(HID0);
127                 hid0 |= 1UL << (63 - 20);
128
129                 /* POWER4 requires the following sequence */
130                 asm volatile(
131                              "sync\n"
132                              "mtspr     %1, %0\n"
133                              "mfspr     %0, %1\n"
134                              "mfspr     %0, %1\n"
135                              "mfspr     %0, %1\n"
136                              "mfspr     %0, %1\n"
137                              "mfspr     %0, %1\n"
138                              "mfspr     %0, %1\n"
139                              "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
140                              "memory");
141                 break;
142
143 #ifdef CONFIG_PPC_PSERIES
144         case PLATFORM_PSERIES_LPAR:
145                 set = 1UL << 63;
146                 reset = 0;
147                 plpar_hcall_norets(H_PERFMON, set, reset);
148                 break;
149 #endif /* CONFIG_PPC_PSERIES */
150
151         default:
152                 break;
153         }
154
155 #ifdef CONFIG_PPC_PSERIES
156         /* instruct hypervisor to maintain PMCs */
157         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
158                 get_paca()->lppaca.pmcregs_in_use = 1;
159 #endif /* CONFIG_PPC_PSERIES */
160 }
161
162 #else
163
164 /* PMC stuff */
165 void ppc64_enable_pmcs(void)
166 {
167         /* XXX Implement for iseries */
168 }
169 #endif /* CONFIG_PPC_MULTIPLATFORM */
170
171 EXPORT_SYMBOL(ppc64_enable_pmcs);
172
173 /* XXX convert to rusty's on_one_cpu */
174 static unsigned long run_on_cpu(unsigned long cpu,
175                                 unsigned long (*func)(unsigned long),
176                                 unsigned long arg)
177 {
178         cpumask_t old_affinity = current->cpus_allowed;
179         unsigned long ret;
180
181         /* should return -EINVAL to userspace */
182         if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
183                 return 0;
184
185         ret = func(arg);
186
187         set_cpus_allowed(current, old_affinity);
188
189         return ret;
190 }
191
192 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
193 static unsigned long read_##NAME(unsigned long junk) \
194 { \
195         return mfspr(ADDRESS); \
196 } \
197 static unsigned long write_##NAME(unsigned long val) \
198 { \
199         ppc64_enable_pmcs(); \
200         mtspr(ADDRESS, val); \
201         return 0; \
202 } \
203 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
204 { \
205         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
206         unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
207         return sprintf(buf, "%lx\n", val); \
208 } \
209 static ssize_t __attribute_used__ \
210         store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
211 { \
212         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
213         unsigned long val; \
214         int ret = sscanf(buf, "%lx", &val); \
215         if (ret != 1) \
216                 return -EINVAL; \
217         run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
218         return count; \
219 }
220
221 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
222 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
223 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
224 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
225 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
226 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
227 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
228 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
229 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
230 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
231 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
232 SYSFS_PMCSETUP(purr, SPRN_PURR);
233
234 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
235 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
236 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
237 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
238 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
239 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
240 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
241 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
242 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
243 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
244 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
245 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
246
247 static void register_cpu_online(unsigned int cpu)
248 {
249         struct cpu *c = &per_cpu(cpu_devices, cpu);
250         struct sys_device *s = &c->sysdev;
251
252 #ifndef CONFIG_PPC_ISERIES
253         if (cpu_has_feature(CPU_FTR_SMT))
254                 sysdev_create_file(s, &attr_smt_snooze_delay);
255 #endif
256
257         /* PMC stuff */
258
259         sysdev_create_file(s, &attr_mmcr0);
260         sysdev_create_file(s, &attr_mmcr1);
261
262         if (cpu_has_feature(CPU_FTR_MMCRA))
263                 sysdev_create_file(s, &attr_mmcra);
264
265         sysdev_create_file(s, &attr_pmc1);
266         sysdev_create_file(s, &attr_pmc2);
267         sysdev_create_file(s, &attr_pmc3);
268         sysdev_create_file(s, &attr_pmc4);
269         sysdev_create_file(s, &attr_pmc5);
270         sysdev_create_file(s, &attr_pmc6);
271
272         if (cpu_has_feature(CPU_FTR_PMC8)) {
273                 sysdev_create_file(s, &attr_pmc7);
274                 sysdev_create_file(s, &attr_pmc8);
275         }
276
277         if (cpu_has_feature(CPU_FTR_SMT))
278                 sysdev_create_file(s, &attr_purr);
279 }
280
281 #ifdef CONFIG_HOTPLUG_CPU
282 static void unregister_cpu_online(unsigned int cpu)
283 {
284         struct cpu *c = &per_cpu(cpu_devices, cpu);
285         struct sys_device *s = &c->sysdev;
286
287         BUG_ON(c->no_control);
288
289 #ifndef CONFIG_PPC_ISERIES
290         if (cpu_has_feature(CPU_FTR_SMT))
291                 sysdev_remove_file(s, &attr_smt_snooze_delay);
292 #endif
293
294         /* PMC stuff */
295
296         sysdev_remove_file(s, &attr_mmcr0);
297         sysdev_remove_file(s, &attr_mmcr1);
298
299         if (cpu_has_feature(CPU_FTR_MMCRA))
300                 sysdev_remove_file(s, &attr_mmcra);
301
302         sysdev_remove_file(s, &attr_pmc1);
303         sysdev_remove_file(s, &attr_pmc2);
304         sysdev_remove_file(s, &attr_pmc3);
305         sysdev_remove_file(s, &attr_pmc4);
306         sysdev_remove_file(s, &attr_pmc5);
307         sysdev_remove_file(s, &attr_pmc6);
308
309         if (cpu_has_feature(CPU_FTR_PMC8)) {
310                 sysdev_remove_file(s, &attr_pmc7);
311                 sysdev_remove_file(s, &attr_pmc8);
312         }
313
314         if (cpu_has_feature(CPU_FTR_SMT))
315                 sysdev_remove_file(s, &attr_purr);
316 }
317 #endif /* CONFIG_HOTPLUG_CPU */
318
319 static int __devinit sysfs_cpu_notify(struct notifier_block *self,
320                                       unsigned long action, void *hcpu)
321 {
322         unsigned int cpu = (unsigned int)(long)hcpu;
323
324         switch (action) {
325         case CPU_ONLINE:
326                 register_cpu_online(cpu);
327                 break;
328 #ifdef CONFIG_HOTPLUG_CPU
329         case CPU_DEAD:
330                 unregister_cpu_online(cpu);
331                 break;
332 #endif
333         }
334         return NOTIFY_OK;
335 }
336
337 static struct notifier_block __devinitdata sysfs_cpu_nb = {
338         .notifier_call  = sysfs_cpu_notify,
339 };
340
341 /* NUMA stuff */
342
343 #ifdef CONFIG_NUMA
344 static struct node node_devices[MAX_NUMNODES];
345
346 static void register_nodes(void)
347 {
348         int i;
349
350         for (i = 0; i < MAX_NUMNODES; i++) {
351                 if (node_online(i)) {
352                         int p_node = parent_node(i);
353                         struct node *parent = NULL;
354
355                         if (p_node != i)
356                                 parent = &node_devices[p_node];
357
358                         register_node(&node_devices[i], i, parent);
359                 }
360         }
361 }
362 #else
363 static void register_nodes(void)
364 {
365         return;
366 }
367 #endif
368
369 /* Only valid if CPU is present. */
370 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
371 {
372         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
373
374         return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
375 }
376 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
377
378 static int __init topology_init(void)
379 {
380         int cpu;
381         struct node *parent = NULL;
382
383         register_nodes();
384
385         register_cpu_notifier(&sysfs_cpu_nb);
386
387         for_each_cpu(cpu) {
388                 struct cpu *c = &per_cpu(cpu_devices, cpu);
389
390 #ifdef CONFIG_NUMA
391                 /* The node to which a cpu belongs can't be known
392                  * until the cpu is made present.
393                  */
394                 parent = NULL;
395                 if (cpu_present(cpu))
396                         parent = &node_devices[cpu_to_node(cpu)];
397 #endif
398                 /*
399                  * For now, we just see if the system supports making
400                  * the RTAS calls for CPU hotplug.  But, there may be a
401                  * more comprehensive way to do this for an individual
402                  * CPU.  For instance, the boot cpu might never be valid
403                  * for hotplugging.
404                  */
405                 if (!ppc_md.cpu_die)
406                         c->no_control = 1;
407
408                 if (cpu_online(cpu) || (c->no_control == 0)) {
409                         register_cpu(c, cpu, parent);
410
411                         sysdev_create_file(&c->sysdev, &attr_physical_id);
412                 }
413
414                 if (cpu_online(cpu))
415                         register_cpu_online(cpu);
416         }
417
418         return 0;
419 }
420 __initcall(topology_init);