blob: 12058db700955efba0b036e7f1fb422f17a15fab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100015#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/hvcall.h>
17#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/paca.h>
19#include <asm/lppaca.h>
20#include <asm/machdep.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110021#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static DEFINE_PER_CPU(struct cpu, cpu_devices);
24
Nathan Lynch124c27d2008-07-27 15:24:55 +100025static DEFINE_PER_CPU(struct kobject *, cache_toplevel);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* SMT stuff */
28
29#ifdef CONFIG_PPC_MULTIPLATFORM
Anton Blanchard0ddd3e72006-09-22 20:30:14 +100030/* Time in microseconds we delay before sleeping in the idle loop */
31DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Andi Kleen4a0b2b42008-07-01 18:48:41 +020033static ssize_t store_smt_snooze_delay(struct sys_device *dev,
34 struct sysdev_attribute *attr,
35 const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 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
Andi Kleen4a0b2b42008-07-01 18:48:41 +020051static ssize_t show_smt_snooze_delay(struct sys_device *dev,
52 struct sysdev_attribute *attr,
53 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
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
60static 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 */
64static int smt_snooze_cmdline;
65
66static int __init smt_setup(void)
67{
68 struct device_node *options;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100069 const unsigned int *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned int cpu;
71
72 if (!cpu_has_feature(CPU_FTR_SMT))
Anton Blanchard69ed3322006-03-28 14:08:39 +110073 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Stephen Rothwell8c8dc322007-04-24 13:50:55 +100075 options = of_find_node_by_path("/options");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (!options)
Anton Blanchard69ed3322006-03-28 14:08:39 +110077 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100079 val = of_get_property(options, "ibm,smt-snooze-delay", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!smt_snooze_cmdline && val) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080081 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 per_cpu(smt_snooze_delay, cpu) = *val;
83 }
84
Stephen Rothwell8c8dc322007-04-24 13:50:55 +100085 of_node_put(options);
Anton Blanchard69ed3322006-03-28 14:08:39 +110086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88__initcall(smt_setup);
89
90static 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)) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800101 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 per_cpu(smt_snooze_delay, cpu) = snooze;
103 }
104
105 return 1;
106}
107__setup("smt-snooze-delay=", setup_smt_snooze_delay);
108
Michael Ellerman180a3362005-08-09 11:13:36 +1000109#endif /* CONFIG_PPC_MULTIPLATFORM */
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
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
116static DEFINE_PER_CPU(char, pmcs_enabled);
117
118void ppc64_enable_pmcs(void)
119{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* Only need to enable them once */
121 if (__get_cpu_var(pmcs_enabled))
122 return;
123
124 __get_cpu_var(pmcs_enabled) = 1;
125
Michael Ellerman180a3362005-08-09 11:13:36 +1000126 if (ppc_md.enable_pmcs)
127 ppc_md.enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129EXPORT_SYMBOL(ppc64_enable_pmcs);
130
131/* XXX convert to rusty's on_one_cpu */
132static 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) \
151static unsigned long read_##NAME(unsigned long junk) \
152{ \
153 return mfspr(ADDRESS); \
154} \
155static unsigned long write_##NAME(unsigned long val) \
156{ \
157 ppc64_enable_pmcs(); \
158 mtspr(ADDRESS, val); \
159 return 0; \
160} \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200161static ssize_t show_##NAME(struct sys_device *dev, \
162 struct sysdev_attribute *attr, \
163 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{ \
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} \
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100169static ssize_t __used \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200170 store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \
171 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{ \
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
Olof Johansson6529c132007-01-28 21:25:57 -0600182
183/* Let's define all possible registers, we'll only hook up the ones
184 * that are implemented on the current processor
185 */
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
188SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
189SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
190SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
191SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
192SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
193SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
194SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
195SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
196SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
197SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
198SYSFS_PMCSETUP(purr, SPRN_PURR);
Anton Blanchardf0509822006-12-08 17:51:13 +1100199SYSFS_PMCSETUP(spurr, SPRN_SPURR);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100200SYSFS_PMCSETUP(dscr, SPRN_DSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Olof Johansson25fc5302007-04-18 16:38:21 +1000202SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
203SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
204SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
205SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
206SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
207SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
Olof Johansson6529c132007-01-28 21:25:57 -0600208
Olof Johansson2e1957f2007-09-05 12:09:06 +1000209#ifdef CONFIG_DEBUG_KERNEL
210SYSFS_PMCSETUP(hid0, SPRN_HID0);
211SYSFS_PMCSETUP(hid1, SPRN_HID1);
212SYSFS_PMCSETUP(hid4, SPRN_HID4);
213SYSFS_PMCSETUP(hid5, SPRN_HID5);
214SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
215SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
216SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
217SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
218SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
219SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
220SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
221SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
222SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
223SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
224SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
225SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
226SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
227SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
228SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
229SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
230SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
231SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
232SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
233SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
234SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
235SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
236SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
237SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
238#endif /* CONFIG_DEBUG_KERNEL */
Olof Johansson6529c132007-01-28 21:25:57 -0600239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
Anton Blanchardf0509822006-12-08 17:51:13 +1100241static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100242static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
Olof Johansson6529c132007-01-28 21:25:57 -0600243static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
244
245static 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
250static 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
261static 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),
Olof Johansson2e1957f2007-09-05 12:09:06 +1000270#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 */
Olof Johansson6529c132007-01-28 21:25:57 -0600300};
301
Nathan Lynch124c27d2008-07-27 15:24:55 +1000302struct 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
313DEFINE_PER_CPU(struct cache_desc *, cache_desc);
314
315static struct cache_desc *kobj_to_cache_desc(struct kobject *k)
316{
317 return container_of(k, struct cache_desc, kobj);
318}
319
320static 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
332static 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
341static struct sysfs_ops cache_desc_sysfs_ops = {
342 .show = cache_desc_show,
343};
344
345static struct kobj_type cache_desc_type = {
346 .release = cache_desc_release,
347 .sysfs_ops = &cache_desc_sysfs_ops,
348};
349
350static 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
357static struct kobj_attribute cache_size_attr =
358 __ATTR(size, 0444, cache_size_show, NULL);
359
360static 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
367static struct kobj_attribute cache_line_size_attr =
368 __ATTR(coherency_line_size, 0444, cache_line_size_show, NULL);
369
370static 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
377static struct kobj_attribute cache_nr_sets_attr =
378 __ATTR(number_of_sets, 0444, cache_nr_sets_show, NULL);
379
380static 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
387static struct kobj_attribute cache_type_attr =
388 __ATTR(type, 0444, cache_type_show, NULL);
389
390static 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
397static struct kobj_attribute cache_level_attr =
398 __ATTR(level, 0444, cache_level_show, NULL);
399
400static 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
407static struct kobj_attribute cache_assoc_attr =
408 __ATTR(ways_of_associativity, 0444, cache_assoc_show, NULL);
409
410struct 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. */
419static 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
426static 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
433static 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
440static 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;
506create_assoc:
507 rc = sysfs_create_file(&new->kobj,
508 &cache_assoc_attr.attr);
509 WARN_ON(rc);
510 }
511
512 return new;
513err:
514 kfree(new);
515 return NULL;
516}
517
518static bool cache_is_unified(struct device_node *np)
519{
520 return of_get_property(np, "cache-unified", NULL);
521}
522
523static 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);
561out:
562 return new;
563}
564
565static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Nathan Lynch9ba19842008-07-27 15:24:51 +1000584static void __cpuinit register_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct cpu *c = &per_cpu(cpu_devices, cpu);
587 struct sys_device *s = &c->sysdev;
Olof Johansson6529c132007-01-28 21:25:57 -0600588 struct sysdev_attribute *attrs, *pmc_attrs;
589 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100591 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
592 cpu_has_feature(CPU_FTR_SMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 sysdev_create_file(s, &attr_smt_snooze_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600596 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Olof Johansson6529c132007-01-28 21:25:57 -0600614 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (cpu_has_feature(CPU_FTR_MMCRA))
622 sysdev_create_file(s, &attr_mmcra);
623
Michael Neulingafd05422006-07-28 13:58:37 +1000624 if (cpu_has_feature(CPU_FTR_PURR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sysdev_create_file(s, &attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100626
Anton Blanchardf0509822006-12-08 17:51:13 +1100627 if (cpu_has_feature(CPU_FTR_SPURR))
628 sysdev_create_file(s, &attr_spurr);
629
Anton Blanchard4c1985572006-12-08 17:46:58 +1100630 if (cpu_has_feature(CPU_FTR_DSCR))
631 sysdev_create_file(s, &attr_dscr);
Nathan Lynch124c27d2008-07-27 15:24:55 +1000632
633 create_cache_info(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636#ifdef CONFIG_HOTPLUG_CPU
Nathan Lynch124c27d2008-07-27 15:24:55 +1000637static 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);
Nathan Lynchf3d3d302008-08-13 07:34:57 +1000644 if (cache_desc != NULL)
Nathan Lynch124c27d2008-07-27 15:24:55 +1000645 kobject_put(&cache_desc->kobj);
Nathan Lynchf3d3d302008-08-13 07:34:57 +1000646
Nathan Lynch124c27d2008-07-27 15:24:55 +1000647 cache_toplevel = per_cpu(cache_toplevel, cpu);
648 if (cache_toplevel != NULL)
649 kobject_put(cache_toplevel);
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static void unregister_cpu_online(unsigned int cpu)
653{
654 struct cpu *c = &per_cpu(cpu_devices, cpu);
655 struct sys_device *s = &c->sysdev;
Olof Johansson6529c132007-01-28 21:25:57 -0600656 struct sysdev_attribute *attrs, *pmc_attrs;
657 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100659 BUG_ON(!c->hotpluggable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100661 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
662 cpu_has_feature(CPU_FTR_SMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sysdev_remove_file(s, &attr_smt_snooze_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600666 switch (cur_cpu_spec->pmc_type) {
667 case PPC_PMC_IBM:
668 attrs = ibm_common_attrs;
669 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
670 pmc_attrs = ibm_pmc_attrs;
671 break;
672 case PPC_PMC_PA6T:
673 /* PA Semi starts counting at PMC0 */
674 attrs = pa6t_attrs;
675 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
676 pmc_attrs = NULL;
677 break;
678 default:
679 attrs = NULL;
680 nattrs = 0;
681 pmc_attrs = NULL;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Olof Johansson6529c132007-01-28 21:25:57 -0600684 for (i = 0; i < nattrs; i++)
685 sysdev_remove_file(s, &attrs[i]);
686
687 if (pmc_attrs)
688 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
689 sysdev_remove_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (cpu_has_feature(CPU_FTR_MMCRA))
692 sysdev_remove_file(s, &attr_mmcra);
693
Michael Neulingafd05422006-07-28 13:58:37 +1000694 if (cpu_has_feature(CPU_FTR_PURR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 sysdev_remove_file(s, &attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100696
Anton Blanchardf0509822006-12-08 17:51:13 +1100697 if (cpu_has_feature(CPU_FTR_SPURR))
698 sysdev_remove_file(s, &attr_spurr);
699
Anton Blanchard4c1985572006-12-08 17:46:58 +1100700 if (cpu_has_feature(CPU_FTR_DSCR))
701 sysdev_remove_file(s, &attr_dscr);
Nathan Lynch124c27d2008-07-27 15:24:55 +1000702
703 remove_cache_info(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705#endif /* CONFIG_HOTPLUG_CPU */
706
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700707static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned long action, void *hcpu)
709{
710 unsigned int cpu = (unsigned int)(long)hcpu;
711
712 switch (action) {
713 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700714 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 register_cpu_online(cpu);
716 break;
717#ifdef CONFIG_HOTPLUG_CPU
718 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700719 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unregister_cpu_online(cpu);
721 break;
722#endif
723 }
724 return NOTIFY_OK;
725}
726
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700727static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .notifier_call = sysfs_cpu_notify,
729};
730
Christian Krafft0344c6c2006-10-24 18:31:24 +0200731static DEFINE_MUTEX(cpu_mutex);
732
733int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
734{
735 int cpu;
736
737 mutex_lock(&cpu_mutex);
738
739 for_each_possible_cpu(cpu) {
740 sysdev_create_file(get_cpu_sysdev(cpu), attr);
741 }
742
743 mutex_unlock(&cpu_mutex);
744 return 0;
745}
746EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
747
748int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
749{
750 int cpu;
751 struct sys_device *sysdev;
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000752 int ret;
Christian Krafft0344c6c2006-10-24 18:31:24 +0200753
754 mutex_lock(&cpu_mutex);
755
756 for_each_possible_cpu(cpu) {
757 sysdev = get_cpu_sysdev(cpu);
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000758 ret = sysfs_create_group(&sysdev->kobj, attrs);
759 WARN_ON(ret != 0);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200760 }
761
762 mutex_unlock(&cpu_mutex);
763 return 0;
764}
765EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
766
767
768void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
769{
770 int cpu;
771
772 mutex_lock(&cpu_mutex);
773
774 for_each_possible_cpu(cpu) {
775 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
776 }
777
778 mutex_unlock(&cpu_mutex);
779}
780EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
781
782void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
783{
784 int cpu;
785 struct sys_device *sysdev;
786
787 mutex_lock(&cpu_mutex);
788
789 for_each_possible_cpu(cpu) {
790 sysdev = get_cpu_sysdev(cpu);
791 sysfs_remove_group(&sysdev->kobj, attrs);
792 }
793
794 mutex_unlock(&cpu_mutex);
795}
796EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
797
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/* NUMA stuff */
800
801#ifdef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static void register_nodes(void)
803{
804 int i;
805
Yasunori Goto0fc44152006-06-27 02:53:38 -0700806 for (i = 0; i < MAX_NUMNODES; i++)
807 register_one_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700809
810int sysfs_add_device_to_node(struct sys_device *dev, int nid)
811{
812 struct node *node = &node_devices[nid];
813 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
814 kobject_name(&dev->kobj));
815}
Johannes Berg12654f72007-07-05 19:35:33 +1000816EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700817
818void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
819{
820 struct node *node = &node_devices[nid];
821 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
822}
Johannes Berg12654f72007-07-05 19:35:33 +1000823EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#else
826static void register_nodes(void)
827{
828 return;
829}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#endif
832
833/* Only valid if CPU is present. */
Stephen Rothwell00bf6e92008-07-23 10:44:58 +1000834static ssize_t show_physical_id(struct sys_device *dev,
835 struct sysdev_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
838
839 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
840}
841static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
842
843static int __init topology_init(void)
844{
845 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 register_nodes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 register_cpu_notifier(&sysfs_cpu_nb);
849
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800850 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct cpu *c = &per_cpu(cpu_devices, cpu);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * For now, we just see if the system supports making
855 * the RTAS calls for CPU hotplug. But, there may be a
856 * more comprehensive way to do this for an individual
857 * CPU. For instance, the boot cpu might never be valid
858 * for hotplugging.
859 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100860 if (ppc_md.cpu_die)
861 c->hotpluggable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100863 if (cpu_online(cpu) || c->hotpluggable) {
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700864 register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 sysdev_create_file(&c->sysdev, &attr_physical_id);
867 }
868
869 if (cpu_online(cpu))
870 register_cpu_online(cpu);
871 }
872
873 return 0;
874}
Kevin Corrye9e77ce2007-05-03 03:11:49 +1000875subsys_initcall(topology_init);