Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / 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         int ret;
116         unsigned int ctrl;
117 #endif /* CONFIG_PPC_PSERIES */
118
119         /* Only need to enable them once */
120         if (__get_cpu_var(pmcs_enabled))
121                 return;
122
123         __get_cpu_var(pmcs_enabled) = 1;
124
125         switch (systemcfg->platform) {
126         case PLATFORM_PSERIES:
127         case PLATFORM_POWERMAC:
128                 hid0 = mfspr(HID0);
129                 hid0 |= 1UL << (63 - 20);
130
131                 /* POWER4 requires the following sequence */
132                 asm volatile(
133                              "sync\n"
134                              "mtspr     %1, %0\n"
135                              "mfspr     %0, %1\n"
136                              "mfspr     %0, %1\n"
137                              "mfspr     %0, %1\n"
138                              "mfspr     %0, %1\n"
139                              "mfspr     %0, %1\n"
140                              "mfspr     %0, %1\n"
141                              "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
142                              "memory");
143                 break;
144
145 #ifdef CONFIG_PPC_PSERIES
146         case PLATFORM_PSERIES_LPAR:
147                 set = 1UL << 63;
148                 reset = 0;
149                 ret = plpar_hcall_norets(H_PERFMON, set, reset);
150                 if (ret)
151                         printk(KERN_ERR "H_PERFMON call on cpu %u "
152                                "returned %d\n",
153                                smp_processor_id(), ret);
154                 break;
155 #endif /* CONFIG_PPC_PSERIES */
156
157         default:
158                 break;
159         }
160
161 #ifdef CONFIG_PPC_PSERIES
162         /* instruct hypervisor to maintain PMCs */
163         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
164                 get_paca()->lppaca.pmcregs_in_use = 1;
165
166         /*
167          * On SMT machines we have to set the run latch in the ctrl register
168          * in order to make PMC6 spin.
169          */
170         if (cpu_has_feature(CPU_FTR_SMT)) {
171                 ctrl = mfspr(CTRLF);
172                 ctrl |= RUNLATCH;
173                 mtspr(CTRLT, ctrl);
174         }
175 #endif /* CONFIG_PPC_PSERIES */
176 }
177
178 #else
179
180 /* PMC stuff */
181 void ppc64_enable_pmcs(void)
182 {
183         /* XXX Implement for iseries */
184 }
185 #endif /* CONFIG_PPC_MULTIPLATFORM */
186
187 EXPORT_SYMBOL(ppc64_enable_pmcs);
188
189 /* XXX convert to rusty's on_one_cpu */
190 static unsigned long run_on_cpu(unsigned long cpu,
191                                 unsigned long (*func)(unsigned long),
192                                 unsigned long arg)
193 {
194         cpumask_t old_affinity = current->cpus_allowed;
195         unsigned long ret;
196
197         /* should return -EINVAL to userspace */
198         if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
199                 return 0;
200
201         ret = func(arg);
202
203         set_cpus_allowed(current, old_affinity);
204
205         return ret;
206 }
207
208 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
209 static unsigned long read_##NAME(unsigned long junk) \
210 { \
211         return mfspr(ADDRESS); \
212 } \
213 static unsigned long write_##NAME(unsigned long val) \
214 { \
215         ppc64_enable_pmcs(); \
216         mtspr(ADDRESS, val); \
217         return 0; \
218 } \
219 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
220 { \
221         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
222         unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
223         return sprintf(buf, "%lx\n", val); \
224 } \
225 static ssize_t __attribute_used__ \
226         store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
227 { \
228         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
229         unsigned long val; \
230         int ret = sscanf(buf, "%lx", &val); \
231         if (ret != 1) \
232                 return -EINVAL; \
233         run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
234         return count; \
235 }
236
237 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
238 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
239 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
240 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
241 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
242 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
243 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
244 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
245 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
246 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
247 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
248 SYSFS_PMCSETUP(purr, SPRN_PURR);
249
250 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
251 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
252 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
253 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
254 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
255 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
256 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
257 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
258 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
259 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
260 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
261 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
262
263 static void register_cpu_online(unsigned int cpu)
264 {
265         struct cpu *c = &per_cpu(cpu_devices, cpu);
266         struct sys_device *s = &c->sysdev;
267
268 #ifndef CONFIG_PPC_ISERIES
269         if (cpu_has_feature(CPU_FTR_SMT))
270                 sysdev_create_file(s, &attr_smt_snooze_delay);
271 #endif
272
273         /* PMC stuff */
274
275         sysdev_create_file(s, &attr_mmcr0);
276         sysdev_create_file(s, &attr_mmcr1);
277
278         if (cpu_has_feature(CPU_FTR_MMCRA))
279                 sysdev_create_file(s, &attr_mmcra);
280
281         sysdev_create_file(s, &attr_pmc1);
282         sysdev_create_file(s, &attr_pmc2);
283         sysdev_create_file(s, &attr_pmc3);
284         sysdev_create_file(s, &attr_pmc4);
285         sysdev_create_file(s, &attr_pmc5);
286         sysdev_create_file(s, &attr_pmc6);
287
288         if (cpu_has_feature(CPU_FTR_PMC8)) {
289                 sysdev_create_file(s, &attr_pmc7);
290                 sysdev_create_file(s, &attr_pmc8);
291         }
292
293         if (cpu_has_feature(CPU_FTR_SMT))
294                 sysdev_create_file(s, &attr_purr);
295 }
296
297 #ifdef CONFIG_HOTPLUG_CPU
298 static void unregister_cpu_online(unsigned int cpu)
299 {
300         struct cpu *c = &per_cpu(cpu_devices, cpu);
301         struct sys_device *s = &c->sysdev;
302
303         BUG_ON(c->no_control);
304
305 #ifndef CONFIG_PPC_ISERIES
306         if (cpu_has_feature(CPU_FTR_SMT))
307                 sysdev_remove_file(s, &attr_smt_snooze_delay);
308 #endif
309
310         /* PMC stuff */
311
312         sysdev_remove_file(s, &attr_mmcr0);
313         sysdev_remove_file(s, &attr_mmcr1);
314
315         if (cpu_has_feature(CPU_FTR_MMCRA))
316                 sysdev_remove_file(s, &attr_mmcra);
317
318         sysdev_remove_file(s, &attr_pmc1);
319         sysdev_remove_file(s, &attr_pmc2);
320         sysdev_remove_file(s, &attr_pmc3);
321         sysdev_remove_file(s, &attr_pmc4);
322         sysdev_remove_file(s, &attr_pmc5);
323         sysdev_remove_file(s, &attr_pmc6);
324
325         if (cpu_has_feature(CPU_FTR_PMC8)) {
326                 sysdev_remove_file(s, &attr_pmc7);
327                 sysdev_remove_file(s, &attr_pmc8);
328         }
329
330         if (cpu_has_feature(CPU_FTR_SMT))
331                 sysdev_remove_file(s, &attr_purr);
332 }
333 #endif /* CONFIG_HOTPLUG_CPU */
334
335 static int __devinit sysfs_cpu_notify(struct notifier_block *self,
336                                       unsigned long action, void *hcpu)
337 {
338         unsigned int cpu = (unsigned int)(long)hcpu;
339
340         switch (action) {
341         case CPU_ONLINE:
342                 register_cpu_online(cpu);
343                 break;
344 #ifdef CONFIG_HOTPLUG_CPU
345         case CPU_DEAD:
346                 unregister_cpu_online(cpu);
347                 break;
348 #endif
349         }
350         return NOTIFY_OK;
351 }
352
353 static struct notifier_block __devinitdata sysfs_cpu_nb = {
354         .notifier_call  = sysfs_cpu_notify,
355 };
356
357 /* NUMA stuff */
358
359 #ifdef CONFIG_NUMA
360 static struct node node_devices[MAX_NUMNODES];
361
362 static void register_nodes(void)
363 {
364         int i;
365
366         for (i = 0; i < MAX_NUMNODES; i++) {
367                 if (node_online(i)) {
368                         int p_node = parent_node(i);
369                         struct node *parent = NULL;
370
371                         if (p_node != i)
372                                 parent = &node_devices[p_node];
373
374                         register_node(&node_devices[i], i, parent);
375                 }
376         }
377 }
378 #else
379 static void register_nodes(void)
380 {
381         return;
382 }
383 #endif
384
385 /* Only valid if CPU is present. */
386 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
387 {
388         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
389
390         return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
391 }
392 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
393
394 static int __init topology_init(void)
395 {
396         int cpu;
397         struct node *parent = NULL;
398
399         register_nodes();
400
401         register_cpu_notifier(&sysfs_cpu_nb);
402
403         for_each_cpu(cpu) {
404                 struct cpu *c = &per_cpu(cpu_devices, cpu);
405
406 #ifdef CONFIG_NUMA
407                 parent = &node_devices[cpu_to_node(cpu)];
408 #endif
409                 /*
410                  * For now, we just see if the system supports making
411                  * the RTAS calls for CPU hotplug.  But, there may be a
412                  * more comprehensive way to do this for an individual
413                  * CPU.  For instance, the boot cpu might never be valid
414                  * for hotplugging.
415                  */
416                 if (!ppc_md.cpu_die)
417                         c->no_control = 1;
418
419                 if (cpu_online(cpu) || (c->no_control == 0)) {
420                         register_cpu(c, cpu, parent);
421
422                         sysdev_create_file(&c->sysdev, &attr_physical_id);
423                 }
424
425                 if (cpu_online(cpu))
426                         register_cpu_online(cpu);
427         }
428
429         return 0;
430 }
431 __initcall(topology_init);