Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[pandora-kernel.git] / arch / powerpc / kernel / sysfs.c
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>
15 #include <asm/firmware.h>
16 #include <asm/hvcall.h>
17 #include <asm/prom.h>
18 #include <asm/paca.h>
19 #include <asm/lppaca.h>
20 #include <asm/machdep.h>
21 #include <asm/smp.h>
22
23 static DEFINE_PER_CPU(struct cpu, cpu_devices);
24
25 static DEFINE_PER_CPU(struct kobject *, cache_toplevel);
26
27 /* SMT stuff */
28
29 #ifdef CONFIG_PPC_MULTIPLATFORM
30 /* Time in microseconds we delay before sleeping in the idle loop */
31 DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
32
33 static ssize_t store_smt_snooze_delay(struct sys_device *dev,
34                                       struct sysdev_attribute *attr,
35                                       const char *buf,
36                                       size_t count)
37 {
38         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
39         ssize_t ret;
40         unsigned long snooze;
41
42         ret = sscanf(buf, "%lu", &snooze);
43         if (ret != 1)
44                 return -EINVAL;
45
46         per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
47
48         return count;
49 }
50
51 static ssize_t show_smt_snooze_delay(struct sys_device *dev,
52                                      struct sysdev_attribute *attr,
53                                      char *buf)
54 {
55         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
56
57         return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
58 }
59
60 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
61                    store_smt_snooze_delay);
62
63 /* Only parse OF options if the matching cmdline option was not specified */
64 static int smt_snooze_cmdline;
65
66 static int __init smt_setup(void)
67 {
68         struct device_node *options;
69         const unsigned int *val;
70         unsigned int cpu;
71
72         if (!cpu_has_feature(CPU_FTR_SMT))
73                 return -ENODEV;
74
75         options = of_find_node_by_path("/options");
76         if (!options)
77                 return -ENODEV;
78
79         val = of_get_property(options, "ibm,smt-snooze-delay", NULL);
80         if (!smt_snooze_cmdline && val) {
81                 for_each_possible_cpu(cpu)
82                         per_cpu(smt_snooze_delay, cpu) = *val;
83         }
84
85         of_node_put(options);
86         return 0;
87 }
88 __initcall(smt_setup);
89
90 static int __init setup_smt_snooze_delay(char *str)
91 {
92         unsigned int cpu;
93         int snooze;
94
95         if (!cpu_has_feature(CPU_FTR_SMT))
96                 return 1;
97
98         smt_snooze_cmdline = 1;
99
100         if (get_option(&str, &snooze)) {
101                 for_each_possible_cpu(cpu)
102                         per_cpu(smt_snooze_delay, cpu) = snooze;
103         }
104
105         return 1;
106 }
107 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
108
109 #endif /* CONFIG_PPC_MULTIPLATFORM */
110
111 /*
112  * Enabling PMCs will slow partition context switch times so we only do
113  * it the first time we write to the PMCs.
114  */
115
116 static DEFINE_PER_CPU(char, pmcs_enabled);
117
118 void ppc64_enable_pmcs(void)
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         if (ppc_md.enable_pmcs)
127                 ppc_md.enable_pmcs();
128 }
129 EXPORT_SYMBOL(ppc64_enable_pmcs);
130
131 /* XXX convert to rusty's on_one_cpu */
132 static unsigned long run_on_cpu(unsigned long cpu,
133                                 unsigned long (*func)(unsigned long),
134                                 unsigned long arg)
135 {
136         cpumask_t old_affinity = current->cpus_allowed;
137         unsigned long ret;
138
139         /* should return -EINVAL to userspace */
140         if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
141                 return 0;
142
143         ret = func(arg);
144
145         set_cpus_allowed(current, old_affinity);
146
147         return ret;
148 }
149
150 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
151 static unsigned long read_##NAME(unsigned long junk) \
152 { \
153         return mfspr(ADDRESS); \
154 } \
155 static unsigned long write_##NAME(unsigned long val) \
156 { \
157         ppc64_enable_pmcs(); \
158         mtspr(ADDRESS, val); \
159         return 0; \
160 } \
161 static ssize_t show_##NAME(struct sys_device *dev, \
162                         struct sysdev_attribute *attr, \
163                         char *buf) \
164 { \
165         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
166         unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
167         return sprintf(buf, "%lx\n", val); \
168 } \
169 static ssize_t __used \
170         store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \
171                         const char *buf, size_t count) \
172 { \
173         struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
174         unsigned long val; \
175         int ret = sscanf(buf, "%lx", &val); \
176         if (ret != 1) \
177                 return -EINVAL; \
178         run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
179         return count; \
180 }
181
182
183 /* Let's define all possible registers, we'll only hook up the ones
184  * that are implemented on the current processor
185  */
186
187 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
188 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
189 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
190 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
191 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
192 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
193 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
194 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
195 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
196 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
197 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
198 SYSFS_PMCSETUP(purr, SPRN_PURR);
199 SYSFS_PMCSETUP(spurr, SPRN_SPURR);
200 SYSFS_PMCSETUP(dscr, SPRN_DSCR);
201
202 SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
203 SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
204 SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
205 SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
206 SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
207 SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
208
209 #ifdef CONFIG_DEBUG_KERNEL
210 SYSFS_PMCSETUP(hid0, SPRN_HID0);
211 SYSFS_PMCSETUP(hid1, SPRN_HID1);
212 SYSFS_PMCSETUP(hid4, SPRN_HID4);
213 SYSFS_PMCSETUP(hid5, SPRN_HID5);
214 SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
215 SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
216 SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
217 SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
218 SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
219 SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
220 SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
221 SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
222 SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
223 SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
224 SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
225 SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
226 SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
227 SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
228 SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
229 SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
230 SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
231 SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
232 SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
233 SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
234 SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
235 SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
236 SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
237 SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
238 #endif /* CONFIG_DEBUG_KERNEL */
239
240 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
241 static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
242 static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
243 static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
244
245 static struct sysdev_attribute ibm_common_attrs[] = {
246         _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
247         _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
248 };
249
250 static struct sysdev_attribute ibm_pmc_attrs[] = {
251         _SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1),
252         _SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2),
253         _SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3),
254         _SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4),
255         _SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5),
256         _SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6),
257         _SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7),
258         _SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8),
259 };
260
261 static struct sysdev_attribute pa6t_attrs[] = {
262         _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
263         _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
264         _SYSDEV_ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
265         _SYSDEV_ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
266         _SYSDEV_ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
267         _SYSDEV_ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
268         _SYSDEV_ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
269         _SYSDEV_ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
270 #ifdef CONFIG_DEBUG_KERNEL
271         _SYSDEV_ATTR(hid0, 0600, show_hid0, store_hid0),
272         _SYSDEV_ATTR(hid1, 0600, show_hid1, store_hid1),
273         _SYSDEV_ATTR(hid4, 0600, show_hid4, store_hid4),
274         _SYSDEV_ATTR(hid5, 0600, show_hid5, store_hid5),
275         _SYSDEV_ATTR(ima0, 0600, show_ima0, store_ima0),
276         _SYSDEV_ATTR(ima1, 0600, show_ima1, store_ima1),
277         _SYSDEV_ATTR(ima2, 0600, show_ima2, store_ima2),
278         _SYSDEV_ATTR(ima3, 0600, show_ima3, store_ima3),
279         _SYSDEV_ATTR(ima4, 0600, show_ima4, store_ima4),
280         _SYSDEV_ATTR(ima5, 0600, show_ima5, store_ima5),
281         _SYSDEV_ATTR(ima6, 0600, show_ima6, store_ima6),
282         _SYSDEV_ATTR(ima7, 0600, show_ima7, store_ima7),
283         _SYSDEV_ATTR(ima8, 0600, show_ima8, store_ima8),
284         _SYSDEV_ATTR(ima9, 0600, show_ima9, store_ima9),
285         _SYSDEV_ATTR(imaat, 0600, show_imaat, store_imaat),
286         _SYSDEV_ATTR(btcr, 0600, show_btcr, store_btcr),
287         _SYSDEV_ATTR(pccr, 0600, show_pccr, store_pccr),
288         _SYSDEV_ATTR(rpccr, 0600, show_rpccr, store_rpccr),
289         _SYSDEV_ATTR(der, 0600, show_der, store_der),
290         _SYSDEV_ATTR(mer, 0600, show_mer, store_mer),
291         _SYSDEV_ATTR(ber, 0600, show_ber, store_ber),
292         _SYSDEV_ATTR(ier, 0600, show_ier, store_ier),
293         _SYSDEV_ATTR(sier, 0600, show_sier, store_sier),
294         _SYSDEV_ATTR(siar, 0600, show_siar, store_siar),
295         _SYSDEV_ATTR(tsr0, 0600, show_tsr0, store_tsr0),
296         _SYSDEV_ATTR(tsr1, 0600, show_tsr1, store_tsr1),
297         _SYSDEV_ATTR(tsr2, 0600, show_tsr2, store_tsr2),
298         _SYSDEV_ATTR(tsr3, 0600, show_tsr3, store_tsr3),
299 #endif /* CONFIG_DEBUG_KERNEL */
300 };
301
302 struct cache_desc {
303         struct kobject kobj;
304         struct cache_desc *next;
305         const char *type;       /* Instruction, Data, or Unified */
306         u32 size;               /* total cache size in KB */
307         u32 line_size;          /* in bytes */
308         u32 nr_sets;            /* number of sets */
309         u32 level;              /* e.g. 1, 2, 3... */
310         u32 associativity;      /* e.g. 8-way... 0 is fully associative */
311 };
312
313 DEFINE_PER_CPU(struct cache_desc *, cache_desc);
314
315 static struct cache_desc *kobj_to_cache_desc(struct kobject *k)
316 {
317         return container_of(k, struct cache_desc, kobj);
318 }
319
320 static void cache_desc_release(struct kobject *k)
321 {
322         struct cache_desc *desc = kobj_to_cache_desc(k);
323
324         pr_debug("%s: releasing %s\n", __func__, kobject_name(k));
325
326         if (desc->next)
327                 kobject_put(&desc->next->kobj);
328
329         kfree(kobj_to_cache_desc(k));
330 }
331
332 static ssize_t cache_desc_show(struct kobject *k, struct attribute *attr, char *buf)
333 {
334         struct kobj_attribute *kobj_attr;
335
336         kobj_attr = container_of(attr, struct kobj_attribute, attr);
337
338         return kobj_attr->show(k, kobj_attr, buf);
339 }
340
341 static struct sysfs_ops cache_desc_sysfs_ops = {
342         .show = cache_desc_show,
343 };
344
345 static struct kobj_type cache_desc_type = {
346         .release = cache_desc_release,
347         .sysfs_ops = &cache_desc_sysfs_ops,
348 };
349
350 static ssize_t cache_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
351 {
352         struct cache_desc *cache = kobj_to_cache_desc(k);
353
354         return sprintf(buf, "%uK\n", cache->size);
355 }
356
357 static struct kobj_attribute cache_size_attr =
358         __ATTR(size, 0444, cache_size_show, NULL);
359
360 static ssize_t cache_line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
361 {
362         struct cache_desc *cache = kobj_to_cache_desc(k);
363
364         return sprintf(buf, "%u\n", cache->line_size);
365 }
366
367 static struct kobj_attribute cache_line_size_attr =
368         __ATTR(coherency_line_size, 0444, cache_line_size_show, NULL);
369
370 static ssize_t cache_nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
371 {
372         struct cache_desc *cache = kobj_to_cache_desc(k);
373
374         return sprintf(buf, "%u\n", cache->nr_sets);
375 }
376
377 static struct kobj_attribute cache_nr_sets_attr =
378         __ATTR(number_of_sets, 0444, cache_nr_sets_show, NULL);
379
380 static ssize_t cache_type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
381 {
382         struct cache_desc *cache = kobj_to_cache_desc(k);
383
384         return sprintf(buf, "%s\n", cache->type);
385 }
386
387 static struct kobj_attribute cache_type_attr =
388         __ATTR(type, 0444, cache_type_show, NULL);
389
390 static ssize_t cache_level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
391 {
392         struct cache_desc *cache = kobj_to_cache_desc(k);
393
394         return sprintf(buf, "%u\n", cache->level);
395 }
396
397 static struct kobj_attribute cache_level_attr =
398         __ATTR(level, 0444, cache_level_show, NULL);
399
400 static ssize_t cache_assoc_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
401 {
402         struct cache_desc *cache = kobj_to_cache_desc(k);
403
404         return sprintf(buf, "%u\n", cache->associativity);
405 }
406
407 static struct kobj_attribute cache_assoc_attr =
408         __ATTR(ways_of_associativity, 0444, cache_assoc_show, NULL);
409
410 struct cache_desc_info {
411         const char *type;
412         const char *size_prop;
413         const char *line_size_prop;
414         const char *nr_sets_prop;
415 };
416
417 /* PowerPC Processor binding says the [di]-cache-* must be equal on
418  * unified caches, so just use d-cache properties. */
419 static struct cache_desc_info ucache_info = {
420         .type = "Unified",
421         .size_prop = "d-cache-size",
422         .line_size_prop = "d-cache-line-size",
423         .nr_sets_prop = "d-cache-sets",
424 };
425
426 static struct cache_desc_info dcache_info = {
427         .type = "Data",
428         .size_prop = "d-cache-size",
429         .line_size_prop = "d-cache-line-size",
430         .nr_sets_prop = "d-cache-sets",
431 };
432
433 static struct cache_desc_info icache_info = {
434         .type = "Instruction",
435         .size_prop = "i-cache-size",
436         .line_size_prop = "i-cache-line-size",
437         .nr_sets_prop = "i-cache-sets",
438 };
439
440 static struct cache_desc * __cpuinit create_cache_desc(struct device_node *np, struct kobject *parent, int index, int level, struct cache_desc_info *info)
441 {
442         const u32 *cache_line_size;
443         struct cache_desc *new;
444         const u32 *cache_size;
445         const u32 *nr_sets;
446         int rc;
447
448         new = kzalloc(sizeof(*new), GFP_KERNEL);
449         if (!new)
450                 return NULL;
451
452         rc = kobject_init_and_add(&new->kobj, &cache_desc_type, parent,
453                                   "index%d", index);
454         if (rc)
455                 goto err;
456
457         /* type */
458         new->type = info->type;
459         rc = sysfs_create_file(&new->kobj, &cache_type_attr.attr);
460         WARN_ON(rc);
461
462         /* level */
463         new->level = level;
464         rc = sysfs_create_file(&new->kobj, &cache_level_attr.attr);
465         WARN_ON(rc);
466
467         /* size */
468         cache_size = of_get_property(np, info->size_prop, NULL);
469         if (cache_size) {
470                 new->size = *cache_size / 1024;
471                 rc = sysfs_create_file(&new->kobj,
472                                        &cache_size_attr.attr);
473                 WARN_ON(rc);
474         }
475
476         /* coherency_line_size */
477         cache_line_size = of_get_property(np, info->line_size_prop, NULL);
478         if (cache_line_size) {
479                 new->line_size = *cache_line_size;
480                 rc = sysfs_create_file(&new->kobj,
481                                        &cache_line_size_attr.attr);
482                 WARN_ON(rc);
483         }
484
485         /* number_of_sets */
486         nr_sets = of_get_property(np, info->nr_sets_prop, NULL);
487         if (nr_sets) {
488                 new->nr_sets = *nr_sets;
489                 rc = sysfs_create_file(&new->kobj,
490                                        &cache_nr_sets_attr.attr);
491                 WARN_ON(rc);
492         }
493
494         /* ways_of_associativity */
495         if (new->nr_sets == 1) {
496                 /* fully associative */
497                 new->associativity = 0;
498                 goto create_assoc;
499         }
500
501         if (new->nr_sets && new->size && new->line_size) {
502                 /* If we have values for all of these we can derive
503                  * the associativity. */
504                 new->associativity =
505                         ((new->size * 1024) / new->nr_sets) / new->line_size;
506 create_assoc:
507                 rc = sysfs_create_file(&new->kobj,
508                                        &cache_assoc_attr.attr);
509                 WARN_ON(rc);
510         }
511
512         return new;
513 err:
514         kfree(new);
515         return NULL;
516 }
517
518 static bool cache_is_unified(struct device_node *np)
519 {
520         return of_get_property(np, "cache-unified", NULL);
521 }
522
523 static struct cache_desc * __cpuinit create_cache_index_info(struct device_node *np, struct kobject *parent, int index, int level)
524 {
525         const phandle *next_cache_phandle;
526         struct device_node *next_cache;
527         struct cache_desc *new, **end;
528
529         pr_debug("%s(node = %s, index = %d)\n", __func__, np->full_name, index);
530
531         if (cache_is_unified(np)) {
532                 new = create_cache_desc(np, parent, index, level,
533                                         &ucache_info);
534         } else {
535                 new = create_cache_desc(np, parent, index, level,
536                                         &dcache_info);
537                 if (new) {
538                         index++;
539                         new->next = create_cache_desc(np, parent, index, level,
540                                                       &icache_info);
541                 }
542         }
543         if (!new)
544                 return NULL;
545
546         end = &new->next;
547         while (*end)
548                 end = &(*end)->next;
549
550         next_cache_phandle = of_get_property(np, "l2-cache", NULL);
551         if (!next_cache_phandle)
552                 goto out;
553
554         next_cache = of_find_node_by_phandle(*next_cache_phandle);
555         if (!next_cache)
556                 goto out;
557
558         *end = create_cache_index_info(next_cache, parent, ++index, ++level);
559
560         of_node_put(next_cache);
561 out:
562         return new;
563 }
564
565 static void __cpuinit create_cache_info(struct sys_device *sysdev)
566 {
567         struct kobject *cache_toplevel;
568         struct device_node *np = NULL;
569         int cpu = sysdev->id;
570
571         cache_toplevel = kobject_create_and_add("cache", &sysdev->kobj);
572         if (!cache_toplevel)
573                 return;
574         per_cpu(cache_toplevel, cpu) = cache_toplevel;
575         np = of_get_cpu_node(cpu, NULL);
576         if (np != NULL) {
577                 per_cpu(cache_desc, cpu) =
578                         create_cache_index_info(np, cache_toplevel, 0, 1);
579                 of_node_put(np);
580         }
581         return;
582 }
583
584 static void __cpuinit register_cpu_online(unsigned int cpu)
585 {
586         struct cpu *c = &per_cpu(cpu_devices, cpu);
587         struct sys_device *s = &c->sysdev;
588         struct sysdev_attribute *attrs, *pmc_attrs;
589         int i, nattrs;
590
591         if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
592                         cpu_has_feature(CPU_FTR_SMT))
593                 sysdev_create_file(s, &attr_smt_snooze_delay);
594
595         /* PMC stuff */
596         switch (cur_cpu_spec->pmc_type) {
597         case PPC_PMC_IBM:
598                 attrs = ibm_common_attrs;
599                 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
600                 pmc_attrs = ibm_pmc_attrs;
601                 break;
602         case PPC_PMC_PA6T:
603                 /* PA Semi starts counting at PMC0 */
604                 attrs = pa6t_attrs;
605                 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
606                 pmc_attrs = NULL;
607                 break;
608         default:
609                 attrs = NULL;
610                 nattrs = 0;
611                 pmc_attrs = NULL;
612         }
613
614         for (i = 0; i < nattrs; i++)
615                 sysdev_create_file(s, &attrs[i]);
616
617         if (pmc_attrs)
618                 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
619                         sysdev_create_file(s, &pmc_attrs[i]);
620
621         if (cpu_has_feature(CPU_FTR_MMCRA))
622                 sysdev_create_file(s, &attr_mmcra);
623
624         if (cpu_has_feature(CPU_FTR_PURR))
625                 sysdev_create_file(s, &attr_purr);
626
627         if (cpu_has_feature(CPU_FTR_SPURR))
628                 sysdev_create_file(s, &attr_spurr);
629
630         if (cpu_has_feature(CPU_FTR_DSCR))
631                 sysdev_create_file(s, &attr_dscr);
632
633         create_cache_info(s);
634 }
635
636 #ifdef CONFIG_HOTPLUG_CPU
637 static void remove_cache_info(struct sys_device *sysdev)
638 {
639         struct kobject *cache_toplevel;
640         struct cache_desc *cache_desc;
641         int cpu = sysdev->id;
642
643         cache_desc = per_cpu(cache_desc, cpu);
644         if (cache_desc != NULL) {
645                 sysfs_remove_file(&cache_desc->kobj, &cache_size_attr.attr);
646                 sysfs_remove_file(&cache_desc->kobj, &cache_line_size_attr.attr);
647                 sysfs_remove_file(&cache_desc->kobj, &cache_type_attr.attr);
648                 sysfs_remove_file(&cache_desc->kobj, &cache_level_attr.attr);
649                 sysfs_remove_file(&cache_desc->kobj, &cache_nr_sets_attr.attr);
650                 sysfs_remove_file(&cache_desc->kobj, &cache_assoc_attr.attr);
651
652                 kobject_put(&cache_desc->kobj);
653         }
654         cache_toplevel = per_cpu(cache_toplevel, cpu);
655         if (cache_toplevel != NULL)
656                 kobject_put(cache_toplevel);
657 }
658
659 static void unregister_cpu_online(unsigned int cpu)
660 {
661         struct cpu *c = &per_cpu(cpu_devices, cpu);
662         struct sys_device *s = &c->sysdev;
663         struct sysdev_attribute *attrs, *pmc_attrs;
664         int i, nattrs;
665
666         BUG_ON(!c->hotpluggable);
667
668         if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
669                         cpu_has_feature(CPU_FTR_SMT))
670                 sysdev_remove_file(s, &attr_smt_snooze_delay);
671
672         /* PMC stuff */
673         switch (cur_cpu_spec->pmc_type) {
674         case PPC_PMC_IBM:
675                 attrs = ibm_common_attrs;
676                 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
677                 pmc_attrs = ibm_pmc_attrs;
678                 break;
679         case PPC_PMC_PA6T:
680                 /* PA Semi starts counting at PMC0 */
681                 attrs = pa6t_attrs;
682                 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
683                 pmc_attrs = NULL;
684                 break;
685         default:
686                 attrs = NULL;
687                 nattrs = 0;
688                 pmc_attrs = NULL;
689         }
690
691         for (i = 0; i < nattrs; i++)
692                 sysdev_remove_file(s, &attrs[i]);
693
694         if (pmc_attrs)
695                 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
696                         sysdev_remove_file(s, &pmc_attrs[i]);
697
698         if (cpu_has_feature(CPU_FTR_MMCRA))
699                 sysdev_remove_file(s, &attr_mmcra);
700
701         if (cpu_has_feature(CPU_FTR_PURR))
702                 sysdev_remove_file(s, &attr_purr);
703
704         if (cpu_has_feature(CPU_FTR_SPURR))
705                 sysdev_remove_file(s, &attr_spurr);
706
707         if (cpu_has_feature(CPU_FTR_DSCR))
708                 sysdev_remove_file(s, &attr_dscr);
709
710         remove_cache_info(s);
711 }
712 #endif /* CONFIG_HOTPLUG_CPU */
713
714 static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
715                                       unsigned long action, void *hcpu)
716 {
717         unsigned int cpu = (unsigned int)(long)hcpu;
718
719         switch (action) {
720         case CPU_ONLINE:
721         case CPU_ONLINE_FROZEN:
722                 register_cpu_online(cpu);
723                 break;
724 #ifdef CONFIG_HOTPLUG_CPU
725         case CPU_DEAD:
726         case CPU_DEAD_FROZEN:
727                 unregister_cpu_online(cpu);
728                 break;
729 #endif
730         }
731         return NOTIFY_OK;
732 }
733
734 static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
735         .notifier_call  = sysfs_cpu_notify,
736 };
737
738 static DEFINE_MUTEX(cpu_mutex);
739
740 int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
741 {
742         int cpu;
743
744         mutex_lock(&cpu_mutex);
745
746         for_each_possible_cpu(cpu) {
747                 sysdev_create_file(get_cpu_sysdev(cpu), attr);
748         }
749
750         mutex_unlock(&cpu_mutex);
751         return 0;
752 }
753 EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
754
755 int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
756 {
757         int cpu;
758         struct sys_device *sysdev;
759         int ret;
760
761         mutex_lock(&cpu_mutex);
762
763         for_each_possible_cpu(cpu) {
764                 sysdev = get_cpu_sysdev(cpu);
765                 ret = sysfs_create_group(&sysdev->kobj, attrs);
766                 WARN_ON(ret != 0);
767         }
768
769         mutex_unlock(&cpu_mutex);
770         return 0;
771 }
772 EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
773
774
775 void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
776 {
777         int cpu;
778
779         mutex_lock(&cpu_mutex);
780
781         for_each_possible_cpu(cpu) {
782                 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
783         }
784
785         mutex_unlock(&cpu_mutex);
786 }
787 EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
788
789 void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
790 {
791         int cpu;
792         struct sys_device *sysdev;
793
794         mutex_lock(&cpu_mutex);
795
796         for_each_possible_cpu(cpu) {
797                 sysdev = get_cpu_sysdev(cpu);
798                 sysfs_remove_group(&sysdev->kobj, attrs);
799         }
800
801         mutex_unlock(&cpu_mutex);
802 }
803 EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
804
805
806 /* NUMA stuff */
807
808 #ifdef CONFIG_NUMA
809 static void register_nodes(void)
810 {
811         int i;
812
813         for (i = 0; i < MAX_NUMNODES; i++)
814                 register_one_node(i);
815 }
816
817 int sysfs_add_device_to_node(struct sys_device *dev, int nid)
818 {
819         struct node *node = &node_devices[nid];
820         return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
821                         kobject_name(&dev->kobj));
822 }
823 EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
824
825 void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
826 {
827         struct node *node = &node_devices[nid];
828         sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
829 }
830 EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
831
832 #else
833 static void register_nodes(void)
834 {
835         return;
836 }
837
838 #endif
839
840 /* Only valid if CPU is present. */
841 static ssize_t show_physical_id(struct sys_device *dev,
842                                 struct sysdev_attribute *attr, char *buf)
843 {
844         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
845
846         return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
847 }
848 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
849
850 static int __init topology_init(void)
851 {
852         int cpu;
853
854         register_nodes();
855         register_cpu_notifier(&sysfs_cpu_nb);
856
857         for_each_possible_cpu(cpu) {
858                 struct cpu *c = &per_cpu(cpu_devices, cpu);
859
860                 /*
861                  * For now, we just see if the system supports making
862                  * the RTAS calls for CPU hotplug.  But, there may be a
863                  * more comprehensive way to do this for an individual
864                  * CPU.  For instance, the boot cpu might never be valid
865                  * for hotplugging.
866                  */
867                 if (ppc_md.cpu_die)
868                         c->hotpluggable = 1;
869
870                 if (cpu_online(cpu) || c->hotpluggable) {
871                         register_cpu(c, cpu);
872
873                         sysdev_create_file(&c->sysdev, &attr_physical_id);
874                 }
875
876                 if (cpu_online(cpu))
877                         register_cpu_online(cpu);
878         }
879
880         return 0;
881 }
882 subsys_initcall(topology_init);