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