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