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