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