Merge to Fedora kernel-2.6.7-1.441
[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 <asm/current.h>
10 #include <asm/processor.h>
11 #include <asm/cputable.h>
12 #include <asm/hvcall.h>
13 #include <asm/prom.h>
14
15
16 /* SMT stuff */
17
18 #ifndef CONFIG_PPC_ISERIES
19
20 /* default to snooze disabled */
21 DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
22
23 static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
24                                       size_t count)
25 {
26         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
27         ssize_t ret;
28         unsigned long snooze;
29
30         ret = sscanf(buf, "%lu", &snooze);
31         if (ret != 1)
32                 return -EINVAL;
33
34         per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
35
36         return count;
37 }
38
39 static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
40 {
41         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
42
43         return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
44 }
45
46 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
47                    store_smt_snooze_delay);
48
49 /* Only parse OF options if the matching cmdline option was not specified */
50 static int smt_snooze_cmdline;
51
52 static int __init smt_setup(void)
53 {
54         struct device_node *options;
55         unsigned int *val;
56         unsigned int cpu;
57
58         if (!cur_cpu_spec->cpu_features & CPU_FTR_SMT)
59                 return 1;
60
61         options = find_path_device("/options");
62         if (!options)
63                 return 1;
64
65         val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
66                                            NULL);
67         if (!smt_snooze_cmdline && val) {
68                 for_each_cpu(cpu)
69                         per_cpu(smt_snooze_delay, cpu) = *val;
70         }
71
72         return 1;
73 }
74 __initcall(smt_setup);
75
76 static int __init setup_smt_snooze_delay(char *str)
77 {
78         unsigned int cpu;
79         int snooze;
80
81         if (!cur_cpu_spec->cpu_features & CPU_FTR_SMT)
82                 return 1;
83
84         smt_snooze_cmdline = 1;
85
86         if (get_option(&str, &snooze)) {
87                 for_each_cpu(cpu)
88                         per_cpu(smt_snooze_delay, cpu) = snooze;
89         }
90
91         return 1;
92 }
93 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
94
95 #endif
96
97
98 /* PMC stuff */
99
100 /*
101  * Enabling PMCs will slow partition context switch times so we only do
102  * it the first time we write to the PMCs.
103  */
104
105 static DEFINE_PER_CPU(char, pmcs_enabled);
106
107 #ifdef CONFIG_PPC_ISERIES
108 void ppc64_enable_pmcs(void)
109 {
110         /* XXX Implement for iseries */
111 }
112 #else
113 void ppc64_enable_pmcs(void)
114 {
115         unsigned long hid0;
116         unsigned long set, reset;
117         int ret;
118         unsigned int ctrl;
119
120         /* Only need to enable them once */
121         if (__get_cpu_var(pmcs_enabled))
122                 return;
123
124         __get_cpu_var(pmcs_enabled) = 1;
125
126         switch (systemcfg->platform) {
127                 case PLATFORM_PSERIES:
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                 case PLATFORM_PSERIES_LPAR:
146                         set = 1UL << 63;
147                         reset = 0;
148                         ret = plpar_hcall_norets(H_PERFMON, set, reset);
149                         if (ret)
150                                 printk(KERN_ERR "H_PERFMON call returned %d",
151                                        ret);
152                         break;
153
154                 default:
155                         break;
156         }
157
158         /* instruct hypervisor to maintain PMCs */
159         if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
160                 char *ptr = (char *)&paca[smp_processor_id()].xLpPaca;
161                 ptr[0xBB] = 1;
162         }
163
164         /*
165          * On SMT machines we have to set the run latch in the ctrl register
166          * in order to make PMC6 spin.
167          */
168         if (cur_cpu_spec->cpu_features & CPU_FTR_SMT) {
169                 ctrl = mfspr(CTRLF);
170                 ctrl |= RUNLATCH;
171                 mtspr(CTRLT, ctrl);
172         }
173 }
174 #endif
175
176 EXPORT_SYMBOL_GPL(ppc64_enable_pmcs);
177
178 /* XXX convert to rusty's on_one_cpu */
179 static unsigned long run_on_cpu(unsigned long cpu,
180                                 unsigned long (*func)(unsigned long),
181                                 unsigned long arg)
182 {
183         cpumask_t old_affinity = current->cpus_allowed;
184         unsigned long ret;
185
186         /* should return -EINVAL to userspace */
187         if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
188                 return 0;
189
190         ret = func(arg);
191
192         set_cpus_allowed(current, old_affinity);
193
194         return ret;
195 }
196
197 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
198 static unsigned long read_##NAME(unsigned long junk) \
199 { \
200         return mfspr(ADDRESS); \
201 } \
202 static unsigned long write_##NAME(unsigned long val) \
203 { \
204         ppc64_enable_pmcs(); \
205         mtspr(ADDRESS, val); \
206         return 0; \
207 } \
208 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
209 { \
210         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
211         unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
212         return sprintf(buf, "%lx\n", val); \
213 } \
214 static ssize_t store_##NAME(struct sys_device *dev, const char *buf, \
215                             size_t count) \
216 { \
217         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
218         unsigned long val; \
219         int ret = sscanf(buf, "%lx", &val); \
220         if (ret != 1) \
221                 return -EINVAL; \
222         run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
223         return count; \
224 }
225
226 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
227 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
228 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
229 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
230 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
231 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
232 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
233 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
234 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
235 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
236 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
237 SYSFS_PMCSETUP(purr, SPRN_PURR);
238
239 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
240 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
241 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
242 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
243 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
244 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
245 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
246 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
247 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
248 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
249 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
250 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
251
252 static void __init register_cpu_pmc(struct sys_device *s)
253 {
254         sysdev_create_file(s, &attr_mmcr0);
255         sysdev_create_file(s, &attr_mmcr1);
256
257         if (cur_cpu_spec->cpu_features & CPU_FTR_MMCRA)
258                 sysdev_create_file(s, &attr_mmcra);
259
260         sysdev_create_file(s, &attr_pmc1);
261         sysdev_create_file(s, &attr_pmc2);
262         sysdev_create_file(s, &attr_pmc3);
263         sysdev_create_file(s, &attr_pmc4);
264         sysdev_create_file(s, &attr_pmc5);
265         sysdev_create_file(s, &attr_pmc6);
266
267         if (cur_cpu_spec->cpu_features & CPU_FTR_PMC8) {
268                 sysdev_create_file(s, &attr_pmc7);
269                 sysdev_create_file(s, &attr_pmc8);
270         }
271
272         if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
273                 sysdev_create_file(s, &attr_purr);
274 }
275
276
277 /* NUMA stuff */
278
279 #ifdef CONFIG_NUMA
280 static struct node node_devices[MAX_NUMNODES];
281
282 static void register_nodes(void)
283 {
284         int i;
285
286         for (i = 0; i < MAX_NUMNODES; i++) {
287                 if (node_online(i)) {
288                         int p_node = parent_node(i);
289                         struct node *parent = NULL;
290
291                         if (p_node != i)
292                                 parent = &node_devices[p_node];
293
294                         register_node(&node_devices[i], i, parent);
295                 }
296         }
297 }
298 #else
299 static void register_nodes(void)
300 {
301         return;
302 }
303 #endif
304
305
306 /* Only valid if CPU is online. */
307 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
308 {
309         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
310
311         return sprintf(buf, "%u\n", get_hard_smp_processor_id(cpu->sysdev.id));
312 }
313 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
314
315
316 static DEFINE_PER_CPU(struct cpu, cpu_devices);
317
318 static int __init topology_init(void)
319 {
320         int cpu;
321         struct node *parent = NULL;
322
323         register_nodes();
324
325         for_each_cpu(cpu) {
326                 struct cpu *c = &per_cpu(cpu_devices, cpu);
327
328 #ifdef CONFIG_NUMA
329                 parent = &node_devices[cpu_to_node(cpu)];
330 #endif
331                 /*
332                  * For now, we just see if the system supports making
333                  * the RTAS calls for CPU hotplug.  But, there may be a
334                  * more comprehensive way to do this for an individual
335                  * CPU.  For instance, the boot cpu might never be valid
336                  * for hotplugging.
337                  */
338                 if (systemcfg->platform != PLATFORM_PSERIES_LPAR)
339                         c->no_control = 1;
340
341                 register_cpu(c, cpu, parent);
342
343                 register_cpu_pmc(&c->sysdev);
344
345                 sysdev_create_file(&c->sysdev, &attr_physical_id);
346
347 #ifndef CONFIG_PPC_ISERIES
348                 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
349                         sysdev_create_file(&c->sysdev, &attr_smt_snooze_delay);
350 #endif
351         }
352
353         return 0;
354 }
355 __initcall(topology_init);