blob: 156808086cacd6135b6f1048e2a5762a1c89bedf [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
25/* SMT stuff */
26
27#ifdef CONFIG_PPC_MULTIPLATFORM
Anton Blanchard0ddd3e72006-09-22 20:30:14 +100028/* Time in microseconds we delay before sleeping in the idle loop */
29DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Andi Kleen4a0b2b42008-07-01 18:48:41 +020031static ssize_t store_smt_snooze_delay(struct sys_device *dev,
32 struct sysdev_attribute *attr,
33 const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 size_t count)
35{
36 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
37 ssize_t ret;
38 unsigned long snooze;
39
40 ret = sscanf(buf, "%lu", &snooze);
41 if (ret != 1)
42 return -EINVAL;
43
44 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
45
46 return count;
47}
48
Andi Kleen4a0b2b42008-07-01 18:48:41 +020049static ssize_t show_smt_snooze_delay(struct sys_device *dev,
50 struct sysdev_attribute *attr,
51 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
54
55 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
56}
57
58static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
59 store_smt_snooze_delay);
60
61/* Only parse OF options if the matching cmdline option was not specified */
62static int smt_snooze_cmdline;
63
64static int __init smt_setup(void)
65{
66 struct device_node *options;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100067 const unsigned int *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned int cpu;
69
70 if (!cpu_has_feature(CPU_FTR_SMT))
Anton Blanchard69ed3322006-03-28 14:08:39 +110071 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Stephen Rothwell8c8dc322007-04-24 13:50:55 +100073 options = of_find_node_by_path("/options");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (!options)
Anton Blanchard69ed3322006-03-28 14:08:39 +110075 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100077 val = of_get_property(options, "ibm,smt-snooze-delay", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!smt_snooze_cmdline && val) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080079 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 per_cpu(smt_snooze_delay, cpu) = *val;
81 }
82
Stephen Rothwell8c8dc322007-04-24 13:50:55 +100083 of_node_put(options);
Anton Blanchard69ed3322006-03-28 14:08:39 +110084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86__initcall(smt_setup);
87
88static int __init setup_smt_snooze_delay(char *str)
89{
90 unsigned int cpu;
91 int snooze;
92
93 if (!cpu_has_feature(CPU_FTR_SMT))
94 return 1;
95
96 smt_snooze_cmdline = 1;
97
98 if (get_option(&str, &snooze)) {
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -080099 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 per_cpu(smt_snooze_delay, cpu) = snooze;
101 }
102
103 return 1;
104}
105__setup("smt-snooze-delay=", setup_smt_snooze_delay);
106
Michael Ellerman180a3362005-08-09 11:13:36 +1000107#endif /* CONFIG_PPC_MULTIPLATFORM */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Enabling PMCs will slow partition context switch times so we only do
111 * it the first time we write to the PMCs.
112 */
113
114static DEFINE_PER_CPU(char, pmcs_enabled);
115
116void ppc64_enable_pmcs(void)
117{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* Only need to enable them once */
119 if (__get_cpu_var(pmcs_enabled))
120 return;
121
122 __get_cpu_var(pmcs_enabled) = 1;
123
Michael Ellerman180a3362005-08-09 11:13:36 +1000124 if (ppc_md.enable_pmcs)
125 ppc_md.enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127EXPORT_SYMBOL(ppc64_enable_pmcs);
128
129/* XXX convert to rusty's on_one_cpu */
130static unsigned long run_on_cpu(unsigned long cpu,
131 unsigned long (*func)(unsigned long),
132 unsigned long arg)
133{
134 cpumask_t old_affinity = current->cpus_allowed;
135 unsigned long ret;
136
137 /* should return -EINVAL to userspace */
138 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
139 return 0;
140
141 ret = func(arg);
142
143 set_cpus_allowed(current, old_affinity);
144
145 return ret;
146}
147
148#define SYSFS_PMCSETUP(NAME, ADDRESS) \
149static unsigned long read_##NAME(unsigned long junk) \
150{ \
151 return mfspr(ADDRESS); \
152} \
153static unsigned long write_##NAME(unsigned long val) \
154{ \
155 ppc64_enable_pmcs(); \
156 mtspr(ADDRESS, val); \
157 return 0; \
158} \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200159static ssize_t show_##NAME(struct sys_device *dev, \
160 struct sysdev_attribute *attr, \
161 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{ \
163 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
164 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
165 return sprintf(buf, "%lx\n", val); \
166} \
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100167static ssize_t __used \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200168 store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \
169 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{ \
171 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
172 unsigned long val; \
173 int ret = sscanf(buf, "%lx", &val); \
174 if (ret != 1) \
175 return -EINVAL; \
176 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
177 return count; \
178}
179
Olof Johansson6529c132007-01-28 21:25:57 -0600180
181/* Let's define all possible registers, we'll only hook up the ones
182 * that are implemented on the current processor
183 */
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
186SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
187SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
188SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
189SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
190SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
191SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
192SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
193SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
194SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
195SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
196SYSFS_PMCSETUP(purr, SPRN_PURR);
Anton Blanchardf0509822006-12-08 17:51:13 +1100197SYSFS_PMCSETUP(spurr, SPRN_SPURR);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100198SYSFS_PMCSETUP(dscr, SPRN_DSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Olof Johansson25fc5302007-04-18 16:38:21 +1000200SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
201SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
202SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
203SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
204SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
205SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
Olof Johansson6529c132007-01-28 21:25:57 -0600206
Olof Johansson2e1957f2007-09-05 12:09:06 +1000207#ifdef CONFIG_DEBUG_KERNEL
208SYSFS_PMCSETUP(hid0, SPRN_HID0);
209SYSFS_PMCSETUP(hid1, SPRN_HID1);
210SYSFS_PMCSETUP(hid4, SPRN_HID4);
211SYSFS_PMCSETUP(hid5, SPRN_HID5);
212SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
213SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
214SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
215SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
216SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
217SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
218SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
219SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
220SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
221SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
222SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
223SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
224SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
225SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
226SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
227SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
228SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
229SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
230SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
231SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
232SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
233SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
234SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
235SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
236#endif /* CONFIG_DEBUG_KERNEL */
Olof Johansson6529c132007-01-28 21:25:57 -0600237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
Anton Blanchardf0509822006-12-08 17:51:13 +1100239static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100240static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
Olof Johansson6529c132007-01-28 21:25:57 -0600241static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
242
243static struct sysdev_attribute ibm_common_attrs[] = {
244 _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
245 _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
246};
247
248static struct sysdev_attribute ibm_pmc_attrs[] = {
249 _SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1),
250 _SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2),
251 _SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3),
252 _SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4),
253 _SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5),
254 _SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6),
255 _SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7),
256 _SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8),
257};
258
259static struct sysdev_attribute pa6t_attrs[] = {
260 _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
261 _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
262 _SYSDEV_ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
263 _SYSDEV_ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
264 _SYSDEV_ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
265 _SYSDEV_ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
266 _SYSDEV_ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
267 _SYSDEV_ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
Olof Johansson2e1957f2007-09-05 12:09:06 +1000268#ifdef CONFIG_DEBUG_KERNEL
269 _SYSDEV_ATTR(hid0, 0600, show_hid0, store_hid0),
270 _SYSDEV_ATTR(hid1, 0600, show_hid1, store_hid1),
271 _SYSDEV_ATTR(hid4, 0600, show_hid4, store_hid4),
272 _SYSDEV_ATTR(hid5, 0600, show_hid5, store_hid5),
273 _SYSDEV_ATTR(ima0, 0600, show_ima0, store_ima0),
274 _SYSDEV_ATTR(ima1, 0600, show_ima1, store_ima1),
275 _SYSDEV_ATTR(ima2, 0600, show_ima2, store_ima2),
276 _SYSDEV_ATTR(ima3, 0600, show_ima3, store_ima3),
277 _SYSDEV_ATTR(ima4, 0600, show_ima4, store_ima4),
278 _SYSDEV_ATTR(ima5, 0600, show_ima5, store_ima5),
279 _SYSDEV_ATTR(ima6, 0600, show_ima6, store_ima6),
280 _SYSDEV_ATTR(ima7, 0600, show_ima7, store_ima7),
281 _SYSDEV_ATTR(ima8, 0600, show_ima8, store_ima8),
282 _SYSDEV_ATTR(ima9, 0600, show_ima9, store_ima9),
283 _SYSDEV_ATTR(imaat, 0600, show_imaat, store_imaat),
284 _SYSDEV_ATTR(btcr, 0600, show_btcr, store_btcr),
285 _SYSDEV_ATTR(pccr, 0600, show_pccr, store_pccr),
286 _SYSDEV_ATTR(rpccr, 0600, show_rpccr, store_rpccr),
287 _SYSDEV_ATTR(der, 0600, show_der, store_der),
288 _SYSDEV_ATTR(mer, 0600, show_mer, store_mer),
289 _SYSDEV_ATTR(ber, 0600, show_ber, store_ber),
290 _SYSDEV_ATTR(ier, 0600, show_ier, store_ier),
291 _SYSDEV_ATTR(sier, 0600, show_sier, store_sier),
292 _SYSDEV_ATTR(siar, 0600, show_siar, store_siar),
293 _SYSDEV_ATTR(tsr0, 0600, show_tsr0, store_tsr0),
294 _SYSDEV_ATTR(tsr1, 0600, show_tsr1, store_tsr1),
295 _SYSDEV_ATTR(tsr2, 0600, show_tsr2, store_tsr2),
296 _SYSDEV_ATTR(tsr3, 0600, show_tsr3, store_tsr3),
297#endif /* CONFIG_DEBUG_KERNEL */
Olof Johansson6529c132007-01-28 21:25:57 -0600298};
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Nathan Lynch9ba19842008-07-27 15:24:51 +1000301static void __cpuinit register_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct cpu *c = &per_cpu(cpu_devices, cpu);
304 struct sys_device *s = &c->sysdev;
Olof Johansson6529c132007-01-28 21:25:57 -0600305 struct sysdev_attribute *attrs, *pmc_attrs;
306 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100308 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
309 cpu_has_feature(CPU_FTR_SMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sysdev_create_file(s, &attr_smt_snooze_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600313 switch (cur_cpu_spec->pmc_type) {
314 case PPC_PMC_IBM:
315 attrs = ibm_common_attrs;
316 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
317 pmc_attrs = ibm_pmc_attrs;
318 break;
319 case PPC_PMC_PA6T:
320 /* PA Semi starts counting at PMC0 */
321 attrs = pa6t_attrs;
322 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
323 pmc_attrs = NULL;
324 break;
325 default:
326 attrs = NULL;
327 nattrs = 0;
328 pmc_attrs = NULL;
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Olof Johansson6529c132007-01-28 21:25:57 -0600331 for (i = 0; i < nattrs; i++)
332 sysdev_create_file(s, &attrs[i]);
333
334 if (pmc_attrs)
335 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
336 sysdev_create_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (cpu_has_feature(CPU_FTR_MMCRA))
339 sysdev_create_file(s, &attr_mmcra);
340
Michael Neulingafd05422006-07-28 13:58:37 +1000341 if (cpu_has_feature(CPU_FTR_PURR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sysdev_create_file(s, &attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100343
Anton Blanchardf0509822006-12-08 17:51:13 +1100344 if (cpu_has_feature(CPU_FTR_SPURR))
345 sysdev_create_file(s, &attr_spurr);
346
Anton Blanchard4c1985572006-12-08 17:46:58 +1100347 if (cpu_has_feature(CPU_FTR_DSCR))
348 sysdev_create_file(s, &attr_dscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351#ifdef CONFIG_HOTPLUG_CPU
352static void unregister_cpu_online(unsigned int cpu)
353{
354 struct cpu *c = &per_cpu(cpu_devices, cpu);
355 struct sys_device *s = &c->sysdev;
Olof Johansson6529c132007-01-28 21:25:57 -0600356 struct sysdev_attribute *attrs, *pmc_attrs;
357 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100359 BUG_ON(!c->hotpluggable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100361 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
362 cpu_has_feature(CPU_FTR_SMT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sysdev_remove_file(s, &attr_smt_snooze_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600366 switch (cur_cpu_spec->pmc_type) {
367 case PPC_PMC_IBM:
368 attrs = ibm_common_attrs;
369 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
370 pmc_attrs = ibm_pmc_attrs;
371 break;
372 case PPC_PMC_PA6T:
373 /* PA Semi starts counting at PMC0 */
374 attrs = pa6t_attrs;
375 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
376 pmc_attrs = NULL;
377 break;
378 default:
379 attrs = NULL;
380 nattrs = 0;
381 pmc_attrs = NULL;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Olof Johansson6529c132007-01-28 21:25:57 -0600384 for (i = 0; i < nattrs; i++)
385 sysdev_remove_file(s, &attrs[i]);
386
387 if (pmc_attrs)
388 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
389 sysdev_remove_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (cpu_has_feature(CPU_FTR_MMCRA))
392 sysdev_remove_file(s, &attr_mmcra);
393
Michael Neulingafd05422006-07-28 13:58:37 +1000394 if (cpu_has_feature(CPU_FTR_PURR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sysdev_remove_file(s, &attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100396
Anton Blanchardf0509822006-12-08 17:51:13 +1100397 if (cpu_has_feature(CPU_FTR_SPURR))
398 sysdev_remove_file(s, &attr_spurr);
399
Anton Blanchard4c1985572006-12-08 17:46:58 +1100400 if (cpu_has_feature(CPU_FTR_DSCR))
401 sysdev_remove_file(s, &attr_dscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403#endif /* CONFIG_HOTPLUG_CPU */
404
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700405static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 unsigned long action, void *hcpu)
407{
408 unsigned int cpu = (unsigned int)(long)hcpu;
409
410 switch (action) {
411 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700412 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 register_cpu_online(cpu);
414 break;
415#ifdef CONFIG_HOTPLUG_CPU
416 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700417 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unregister_cpu_online(cpu);
419 break;
420#endif
421 }
422 return NOTIFY_OK;
423}
424
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700425static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .notifier_call = sysfs_cpu_notify,
427};
428
Christian Krafft0344c6c2006-10-24 18:31:24 +0200429static DEFINE_MUTEX(cpu_mutex);
430
431int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
432{
433 int cpu;
434
435 mutex_lock(&cpu_mutex);
436
437 for_each_possible_cpu(cpu) {
438 sysdev_create_file(get_cpu_sysdev(cpu), attr);
439 }
440
441 mutex_unlock(&cpu_mutex);
442 return 0;
443}
444EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
445
446int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
447{
448 int cpu;
449 struct sys_device *sysdev;
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000450 int ret;
Christian Krafft0344c6c2006-10-24 18:31:24 +0200451
452 mutex_lock(&cpu_mutex);
453
454 for_each_possible_cpu(cpu) {
455 sysdev = get_cpu_sysdev(cpu);
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000456 ret = sysfs_create_group(&sysdev->kobj, attrs);
457 WARN_ON(ret != 0);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200458 }
459
460 mutex_unlock(&cpu_mutex);
461 return 0;
462}
463EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
464
465
466void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
467{
468 int cpu;
469
470 mutex_lock(&cpu_mutex);
471
472 for_each_possible_cpu(cpu) {
473 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
474 }
475
476 mutex_unlock(&cpu_mutex);
477}
478EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
479
480void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
481{
482 int cpu;
483 struct sys_device *sysdev;
484
485 mutex_lock(&cpu_mutex);
486
487 for_each_possible_cpu(cpu) {
488 sysdev = get_cpu_sysdev(cpu);
489 sysfs_remove_group(&sysdev->kobj, attrs);
490 }
491
492 mutex_unlock(&cpu_mutex);
493}
494EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
495
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/* NUMA stuff */
498
499#ifdef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static void register_nodes(void)
501{
502 int i;
503
Yasunori Goto0fc44152006-06-27 02:53:38 -0700504 for (i = 0; i < MAX_NUMNODES; i++)
505 register_one_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700507
508int sysfs_add_device_to_node(struct sys_device *dev, int nid)
509{
510 struct node *node = &node_devices[nid];
511 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
512 kobject_name(&dev->kobj));
513}
Johannes Berg12654f72007-07-05 19:35:33 +1000514EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700515
516void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
517{
518 struct node *node = &node_devices[nid];
519 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
520}
Johannes Berg12654f72007-07-05 19:35:33 +1000521EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#else
524static void register_nodes(void)
525{
526 return;
527}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#endif
530
531/* Only valid if CPU is present. */
Stephen Rothwell00bf6e92008-07-23 10:44:58 +1000532static ssize_t show_physical_id(struct sys_device *dev,
533 struct sysdev_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
536
537 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
538}
539static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
540
541static int __init topology_init(void)
542{
543 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 register_nodes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 register_cpu_notifier(&sysfs_cpu_nb);
547
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800548 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct cpu *c = &per_cpu(cpu_devices, cpu);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * For now, we just see if the system supports making
553 * the RTAS calls for CPU hotplug. But, there may be a
554 * more comprehensive way to do this for an individual
555 * CPU. For instance, the boot cpu might never be valid
556 * for hotplugging.
557 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100558 if (ppc_md.cpu_die)
559 c->hotpluggable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100561 if (cpu_online(cpu) || c->hotpluggable) {
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700562 register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 sysdev_create_file(&c->sysdev, &attr_physical_id);
565 }
566
567 if (cpu_online(cpu))
568 register_cpu_online(cpu);
569 }
570
571 return 0;
572}
Kevin Corrye9e77ce2007-05-03 03:11:49 +1000573subsys_initcall(topology_init);