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