[POWERPC] Fix mmap of PCI resource with hack for X
[deliverable/linux.git] / arch / powerpc / kernel / sysfs.c
CommitLineData
1da177e4
LT
1#include <linux/sysdev.h>
2#include <linux/cpu.h>
3#include <linux/smp.h>
4#include <linux/percpu.h>
5#include <linux/init.h>
6#include <linux/sched.h>
7#include <linux/module.h>
8#include <linux/nodemask.h>
9#include <linux/cpumask.h>
10#include <linux/notifier.h>
11
12#include <asm/current.h>
13#include <asm/processor.h>
14#include <asm/cputable.h>
1ababe11 15#include <asm/firmware.h>
1da177e4
LT
16#include <asm/hvcall.h>
17#include <asm/prom.h>
1da177e4
LT
18#include <asm/paca.h>
19#include <asm/lppaca.h>
20#include <asm/machdep.h>
2249ca9d 21#include <asm/smp.h>
1da177e4
LT
22
23static DEFINE_PER_CPU(struct cpu, cpu_devices);
24
25/* SMT stuff */
26
27#ifdef CONFIG_PPC_MULTIPLATFORM
0ddd3e7d
AB
28/* Time in microseconds we delay before sleeping in the idle loop */
29DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
1da177e4
LT
30
31static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
32 size_t count)
33{
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
35 ssize_t ret;
36 unsigned long snooze;
37
38 ret = sscanf(buf, "%lu", &snooze);
39 if (ret != 1)
40 return -EINVAL;
41
42 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
43
44 return count;
45}
46
47static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
48{
49 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
50
51 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
52}
53
54static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
55 store_smt_snooze_delay);
56
57/* Only parse OF options if the matching cmdline option was not specified */
58static int smt_snooze_cmdline;
59
60static int __init smt_setup(void)
61{
62 struct device_node *options;
a7f67bdf 63 const unsigned int *val;
1da177e4
LT
64 unsigned int cpu;
65
66 if (!cpu_has_feature(CPU_FTR_SMT))
69ed3324 67 return -ENODEV;
1da177e4
LT
68
69 options = find_path_device("/options");
70 if (!options)
69ed3324 71 return -ENODEV;
1da177e4 72
a7f67bdf 73 val = get_property(options, "ibm,smt-snooze-delay", NULL);
1da177e4 74 if (!smt_snooze_cmdline && val) {
0e551954 75 for_each_possible_cpu(cpu)
1da177e4
LT
76 per_cpu(smt_snooze_delay, cpu) = *val;
77 }
78
69ed3324 79 return 0;
1da177e4
LT
80}
81__initcall(smt_setup);
82
83static int __init setup_smt_snooze_delay(char *str)
84{
85 unsigned int cpu;
86 int snooze;
87
88 if (!cpu_has_feature(CPU_FTR_SMT))
89 return 1;
90
91 smt_snooze_cmdline = 1;
92
93 if (get_option(&str, &snooze)) {
0e551954 94 for_each_possible_cpu(cpu)
1da177e4
LT
95 per_cpu(smt_snooze_delay, cpu) = snooze;
96 }
97
98 return 1;
99}
100__setup("smt-snooze-delay=", setup_smt_snooze_delay);
101
180a3362
ME
102#endif /* CONFIG_PPC_MULTIPLATFORM */
103
1da177e4
LT
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
109static DEFINE_PER_CPU(char, pmcs_enabled);
110
111void ppc64_enable_pmcs(void)
112{
1da177e4
LT
113 /* Only need to enable them once */
114 if (__get_cpu_var(pmcs_enabled))
115 return;
116
117 __get_cpu_var(pmcs_enabled) = 1;
118
180a3362
ME
119 if (ppc_md.enable_pmcs)
120 ppc_md.enable_pmcs();
1da177e4 121}
1da177e4
LT
122EXPORT_SYMBOL(ppc64_enable_pmcs);
123
124/* XXX convert to rusty's on_one_cpu */
125static unsigned long run_on_cpu(unsigned long cpu,
126 unsigned long (*func)(unsigned long),
127 unsigned long arg)
128{
129 cpumask_t old_affinity = current->cpus_allowed;
130 unsigned long ret;
131
132 /* should return -EINVAL to userspace */
133 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
134 return 0;
135
136 ret = func(arg);
137
138 set_cpus_allowed(current, old_affinity);
139
140 return ret;
141}
142
143#define SYSFS_PMCSETUP(NAME, ADDRESS) \
144static unsigned long read_##NAME(unsigned long junk) \
145{ \
146 return mfspr(ADDRESS); \
147} \
148static unsigned long write_##NAME(unsigned long val) \
149{ \
150 ppc64_enable_pmcs(); \
151 mtspr(ADDRESS, val); \
152 return 0; \
153} \
154static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
155{ \
156 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
157 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
158 return sprintf(buf, "%lx\n", val); \
159} \
160static ssize_t __attribute_used__ \
161 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
162{ \
163 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
164 unsigned long val; \
165 int ret = sscanf(buf, "%lx", &val); \
166 if (ret != 1) \
167 return -EINVAL; \
168 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
169 return count; \
170}
171
172SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
173SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
174SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
175SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
176SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
177SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
178SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
179SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
180SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
181SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
182SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
183SYSFS_PMCSETUP(purr, SPRN_PURR);
184
185static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
186static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
187static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
188static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
189static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
190static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
191static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
192static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
193static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
194static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
195static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
196static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
197
198static void register_cpu_online(unsigned int cpu)
199{
200 struct cpu *c = &per_cpu(cpu_devices, cpu);
201 struct sys_device *s = &c->sysdev;
202
ad5cb17f
SR
203 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
204 cpu_has_feature(CPU_FTR_SMT))
1da177e4 205 sysdev_create_file(s, &attr_smt_snooze_delay);
1da177e4
LT
206
207 /* PMC stuff */
208
209 sysdev_create_file(s, &attr_mmcr0);
210 sysdev_create_file(s, &attr_mmcr1);
211
212 if (cpu_has_feature(CPU_FTR_MMCRA))
213 sysdev_create_file(s, &attr_mmcra);
214
fd5b4377
AB
215 if (cur_cpu_spec->num_pmcs >= 1)
216 sysdev_create_file(s, &attr_pmc1);
217 if (cur_cpu_spec->num_pmcs >= 2)
218 sysdev_create_file(s, &attr_pmc2);
219 if (cur_cpu_spec->num_pmcs >= 3)
220 sysdev_create_file(s, &attr_pmc3);
221 if (cur_cpu_spec->num_pmcs >= 4)
222 sysdev_create_file(s, &attr_pmc4);
223 if (cur_cpu_spec->num_pmcs >= 5)
224 sysdev_create_file(s, &attr_pmc5);
225 if (cur_cpu_spec->num_pmcs >= 6)
226 sysdev_create_file(s, &attr_pmc6);
227 if (cur_cpu_spec->num_pmcs >= 7)
1da177e4 228 sysdev_create_file(s, &attr_pmc7);
fd5b4377 229 if (cur_cpu_spec->num_pmcs >= 8)
1da177e4 230 sysdev_create_file(s, &attr_pmc8);
d3d2176a 231
afd05423 232 if (cpu_has_feature(CPU_FTR_PURR))
1da177e4
LT
233 sysdev_create_file(s, &attr_purr);
234}
235
236#ifdef CONFIG_HOTPLUG_CPU
237static void unregister_cpu_online(unsigned int cpu)
238{
239 struct cpu *c = &per_cpu(cpu_devices, cpu);
240 struct sys_device *s = &c->sysdev;
241
72486f1f 242 BUG_ON(!c->hotpluggable);
1da177e4 243
ad5cb17f
SR
244 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
245 cpu_has_feature(CPU_FTR_SMT))
1da177e4 246 sysdev_remove_file(s, &attr_smt_snooze_delay);
1da177e4
LT
247
248 /* PMC stuff */
249
250 sysdev_remove_file(s, &attr_mmcr0);
251 sysdev_remove_file(s, &attr_mmcr1);
252
253 if (cpu_has_feature(CPU_FTR_MMCRA))
254 sysdev_remove_file(s, &attr_mmcra);
255
fd5b4377
AB
256 if (cur_cpu_spec->num_pmcs >= 1)
257 sysdev_remove_file(s, &attr_pmc1);
258 if (cur_cpu_spec->num_pmcs >= 2)
259 sysdev_remove_file(s, &attr_pmc2);
260 if (cur_cpu_spec->num_pmcs >= 3)
261 sysdev_remove_file(s, &attr_pmc3);
262 if (cur_cpu_spec->num_pmcs >= 4)
263 sysdev_remove_file(s, &attr_pmc4);
264 if (cur_cpu_spec->num_pmcs >= 5)
265 sysdev_remove_file(s, &attr_pmc5);
266 if (cur_cpu_spec->num_pmcs >= 6)
267 sysdev_remove_file(s, &attr_pmc6);
268 if (cur_cpu_spec->num_pmcs >= 7)
1da177e4 269 sysdev_remove_file(s, &attr_pmc7);
fd5b4377 270 if (cur_cpu_spec->num_pmcs >= 8)
1da177e4 271 sysdev_remove_file(s, &attr_pmc8);
1da177e4 272
afd05423 273 if (cpu_has_feature(CPU_FTR_PURR))
1da177e4
LT
274 sysdev_remove_file(s, &attr_purr);
275}
276#endif /* CONFIG_HOTPLUG_CPU */
277
8c78f307 278static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
1da177e4
LT
279 unsigned long action, void *hcpu)
280{
281 unsigned int cpu = (unsigned int)(long)hcpu;
282
283 switch (action) {
284 case CPU_ONLINE:
285 register_cpu_online(cpu);
286 break;
287#ifdef CONFIG_HOTPLUG_CPU
288 case CPU_DEAD:
289 unregister_cpu_online(cpu);
290 break;
291#endif
292 }
293 return NOTIFY_OK;
294}
295
8c78f307 296static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
1da177e4
LT
297 .notifier_call = sysfs_cpu_notify,
298};
299
0344c6c5
CK
300static DEFINE_MUTEX(cpu_mutex);
301
302int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
303{
304 int cpu;
305
306 mutex_lock(&cpu_mutex);
307
308 for_each_possible_cpu(cpu) {
309 sysdev_create_file(get_cpu_sysdev(cpu), attr);
310 }
311
312 mutex_unlock(&cpu_mutex);
313 return 0;
314}
315EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
316
317int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
318{
319 int cpu;
320 struct sys_device *sysdev;
321
322 mutex_lock(&cpu_mutex);
323
324 for_each_possible_cpu(cpu) {
325 sysdev = get_cpu_sysdev(cpu);
326 sysfs_create_group(&sysdev->kobj, attrs);
327 }
328
329 mutex_unlock(&cpu_mutex);
330 return 0;
331}
332EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
333
334
335void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
336{
337 int cpu;
338
339 mutex_lock(&cpu_mutex);
340
341 for_each_possible_cpu(cpu) {
342 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
343 }
344
345 mutex_unlock(&cpu_mutex);
346}
347EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
348
349void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
350{
351 int cpu;
352 struct sys_device *sysdev;
353
354 mutex_lock(&cpu_mutex);
355
356 for_each_possible_cpu(cpu) {
357 sysdev = get_cpu_sysdev(cpu);
358 sysfs_remove_group(&sysdev->kobj, attrs);
359 }
360
361 mutex_unlock(&cpu_mutex);
362}
363EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
364
365
1da177e4
LT
366/* NUMA stuff */
367
368#ifdef CONFIG_NUMA
1da177e4
LT
369static void register_nodes(void)
370{
371 int i;
372
0fc44159
YG
373 for (i = 0; i < MAX_NUMNODES; i++)
374 register_one_node(i);
1da177e4 375}
953039c8
JK
376
377int sysfs_add_device_to_node(struct sys_device *dev, int nid)
378{
379 struct node *node = &node_devices[nid];
380 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
381 kobject_name(&dev->kobj));
382}
383
384void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
385{
386 struct node *node = &node_devices[nid];
387 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
388}
389
1da177e4
LT
390#else
391static void register_nodes(void)
392{
393 return;
394}
953039c8 395
1da177e4
LT
396#endif
397
953039c8
JK
398EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
399EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
400
1da177e4
LT
401/* Only valid if CPU is present. */
402static ssize_t show_physical_id(struct sys_device *dev, char *buf)
403{
404 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
405
406 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
407}
408static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
409
410static int __init topology_init(void)
411{
412 int cpu;
1da177e4
LT
413
414 register_nodes();
1da177e4
LT
415 register_cpu_notifier(&sysfs_cpu_nb);
416
0e551954 417 for_each_possible_cpu(cpu) {
1da177e4
LT
418 struct cpu *c = &per_cpu(cpu_devices, cpu);
419
1da177e4
LT
420 /*
421 * For now, we just see if the system supports making
422 * the RTAS calls for CPU hotplug. But, there may be a
423 * more comprehensive way to do this for an individual
424 * CPU. For instance, the boot cpu might never be valid
425 * for hotplugging.
426 */
72486f1f
SS
427 if (ppc_md.cpu_die)
428 c->hotpluggable = 1;
1da177e4 429
72486f1f 430 if (cpu_online(cpu) || c->hotpluggable) {
76b67ed9 431 register_cpu(c, cpu);
1da177e4
LT
432
433 sysdev_create_file(&c->sysdev, &attr_physical_id);
434 }
435
436 if (cpu_online(cpu))
437 register_cpu_online(cpu);
438 }
439
440 return 0;
441}
442__initcall(topology_init);
This page took 0.18303 seconds and 5 git commands to generate.