1 #include <linux/sysdev.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>
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>
19 #include <asm/lppaca.h>
20 #include <asm/machdep.h>
23 static DEFINE_PER_CPU(struct cpu, cpu_devices);
27 #ifdef CONFIG_PPC_MULTIPLATFORM
28 /* Time in microseconds we delay before sleeping in the idle loop */
29 DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
31 static ssize_t store_smt_snooze_delay(struct sys_device *dev,
32 struct sysdev_attribute *attr,
36 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
40 ret = sscanf(buf, "%lu", &snooze);
44 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
49 static ssize_t show_smt_snooze_delay(struct sys_device *dev,
50 struct sysdev_attribute *attr,
53 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
55 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
58 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
59 store_smt_snooze_delay);
61 /* Only parse OF options if the matching cmdline option was not specified */
62 static int smt_snooze_cmdline;
64 static int __init smt_setup(void)
66 struct device_node *options;
67 const unsigned int *val;
70 if (!cpu_has_feature(CPU_FTR_SMT))
73 options = of_find_node_by_path("/options");
77 val = of_get_property(options, "ibm,smt-snooze-delay", NULL);
78 if (!smt_snooze_cmdline && val) {
79 for_each_possible_cpu(cpu)
80 per_cpu(smt_snooze_delay, cpu) = *val;
86 __initcall(smt_setup);
88 static int __init setup_smt_snooze_delay(char *str)
93 if (!cpu_has_feature(CPU_FTR_SMT))
96 smt_snooze_cmdline = 1;
98 if (get_option(&str, &snooze)) {
99 for_each_possible_cpu(cpu)
100 per_cpu(smt_snooze_delay, cpu) = snooze;
105 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
107 #endif /* CONFIG_PPC_MULTIPLATFORM */
110 * Enabling PMCs will slow partition context switch times so we only do
111 * it the first time we write to the PMCs.
114 static DEFINE_PER_CPU(char, pmcs_enabled);
116 void ppc64_enable_pmcs(void)
118 /* Only need to enable them once */
119 if (__get_cpu_var(pmcs_enabled))
122 __get_cpu_var(pmcs_enabled) = 1;
124 if (ppc_md.enable_pmcs)
125 ppc_md.enable_pmcs();
127 EXPORT_SYMBOL(ppc64_enable_pmcs);
129 /* XXX convert to rusty's on_one_cpu */
130 static unsigned long run_on_cpu(unsigned long cpu,
131 unsigned long (*func)(unsigned long),
134 cpumask_t old_affinity = current->cpus_allowed;
137 /* should return -EINVAL to userspace */
138 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
143 set_cpus_allowed(current, old_affinity);
148 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
149 static unsigned long read_##NAME(unsigned long junk) \
151 return mfspr(ADDRESS); \
153 static unsigned long write_##NAME(unsigned long val) \
155 ppc64_enable_pmcs(); \
156 mtspr(ADDRESS, val); \
159 static ssize_t show_##NAME(struct sys_device *dev, \
160 struct sysdev_attribute *attr, \
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); \
167 static ssize_t __used \
168 store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \
169 const char *buf, size_t count) \
171 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
173 int ret = sscanf(buf, "%lx", &val); \
176 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
181 /* Let's define all possible registers, we'll only hook up the ones
182 * that are implemented on the current processor
185 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
186 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
187 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
188 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
189 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
190 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
191 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
192 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
193 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
194 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
195 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
196 SYSFS_PMCSETUP(purr, SPRN_PURR);
197 SYSFS_PMCSETUP(spurr, SPRN_SPURR);
198 SYSFS_PMCSETUP(dscr, SPRN_DSCR);
200 SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
201 SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
202 SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
203 SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
204 SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
205 SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
207 #ifdef CONFIG_DEBUG_KERNEL
208 SYSFS_PMCSETUP(hid0, SPRN_HID0);
209 SYSFS_PMCSETUP(hid1, SPRN_HID1);
210 SYSFS_PMCSETUP(hid4, SPRN_HID4);
211 SYSFS_PMCSETUP(hid5, SPRN_HID5);
212 SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
213 SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
214 SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
215 SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
216 SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
217 SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
218 SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
219 SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
220 SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
221 SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
222 SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
223 SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
224 SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
225 SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
226 SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
227 SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
228 SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
229 SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
230 SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
231 SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
232 SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
233 SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
234 SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
235 SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
236 #endif /* CONFIG_DEBUG_KERNEL */
238 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
239 static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
240 static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
241 static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
243 static struct sysdev_attribute ibm_common_attrs[] = {
244 _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
245 _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
248 static 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),
259 static 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),
268 #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 */
301 static void register_cpu_online(unsigned int cpu)
303 struct cpu *c = &per_cpu(cpu_devices, cpu);
304 struct sys_device *s = &c->sysdev;
305 struct sysdev_attribute *attrs, *pmc_attrs;
308 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
309 cpu_has_feature(CPU_FTR_SMT))
310 sysdev_create_file(s, &attr_smt_snooze_delay);
313 switch (cur_cpu_spec->pmc_type) {
315 attrs = ibm_common_attrs;
316 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
317 pmc_attrs = ibm_pmc_attrs;
320 /* PA Semi starts counting at PMC0 */
322 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
331 for (i = 0; i < nattrs; i++)
332 sysdev_create_file(s, &attrs[i]);
335 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
336 sysdev_create_file(s, &pmc_attrs[i]);
338 if (cpu_has_feature(CPU_FTR_MMCRA))
339 sysdev_create_file(s, &attr_mmcra);
341 if (cpu_has_feature(CPU_FTR_PURR))
342 sysdev_create_file(s, &attr_purr);
344 if (cpu_has_feature(CPU_FTR_SPURR))
345 sysdev_create_file(s, &attr_spurr);
347 if (cpu_has_feature(CPU_FTR_DSCR))
348 sysdev_create_file(s, &attr_dscr);
351 #ifdef CONFIG_HOTPLUG_CPU
352 static void unregister_cpu_online(unsigned int cpu)
354 struct cpu *c = &per_cpu(cpu_devices, cpu);
355 struct sys_device *s = &c->sysdev;
356 struct sysdev_attribute *attrs, *pmc_attrs;
359 BUG_ON(!c->hotpluggable);
361 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
362 cpu_has_feature(CPU_FTR_SMT))
363 sysdev_remove_file(s, &attr_smt_snooze_delay);
366 switch (cur_cpu_spec->pmc_type) {
368 attrs = ibm_common_attrs;
369 nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
370 pmc_attrs = ibm_pmc_attrs;
373 /* PA Semi starts counting at PMC0 */
375 nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
384 for (i = 0; i < nattrs; i++)
385 sysdev_remove_file(s, &attrs[i]);
388 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
389 sysdev_remove_file(s, &pmc_attrs[i]);
391 if (cpu_has_feature(CPU_FTR_MMCRA))
392 sysdev_remove_file(s, &attr_mmcra);
394 if (cpu_has_feature(CPU_FTR_PURR))
395 sysdev_remove_file(s, &attr_purr);
397 if (cpu_has_feature(CPU_FTR_SPURR))
398 sysdev_remove_file(s, &attr_spurr);
400 if (cpu_has_feature(CPU_FTR_DSCR))
401 sysdev_remove_file(s, &attr_dscr);
403 #endif /* CONFIG_HOTPLUG_CPU */
405 static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
406 unsigned long action, void *hcpu)
408 unsigned int cpu = (unsigned int)(long)hcpu;
412 case CPU_ONLINE_FROZEN:
413 register_cpu_online(cpu);
415 #ifdef CONFIG_HOTPLUG_CPU
417 case CPU_DEAD_FROZEN:
418 unregister_cpu_online(cpu);
425 static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
426 .notifier_call = sysfs_cpu_notify,
429 static DEFINE_MUTEX(cpu_mutex);
431 int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
435 mutex_lock(&cpu_mutex);
437 for_each_possible_cpu(cpu) {
438 sysdev_create_file(get_cpu_sysdev(cpu), attr);
441 mutex_unlock(&cpu_mutex);
444 EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
446 int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
449 struct sys_device *sysdev;
452 mutex_lock(&cpu_mutex);
454 for_each_possible_cpu(cpu) {
455 sysdev = get_cpu_sysdev(cpu);
456 ret = sysfs_create_group(&sysdev->kobj, attrs);
460 mutex_unlock(&cpu_mutex);
463 EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
466 void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
470 mutex_lock(&cpu_mutex);
472 for_each_possible_cpu(cpu) {
473 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
476 mutex_unlock(&cpu_mutex);
478 EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
480 void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
483 struct sys_device *sysdev;
485 mutex_lock(&cpu_mutex);
487 for_each_possible_cpu(cpu) {
488 sysdev = get_cpu_sysdev(cpu);
489 sysfs_remove_group(&sysdev->kobj, attrs);
492 mutex_unlock(&cpu_mutex);
494 EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
500 static void register_nodes(void)
504 for (i = 0; i < MAX_NUMNODES; i++)
505 register_one_node(i);
508 int sysfs_add_device_to_node(struct sys_device *dev, int nid)
510 struct node *node = &node_devices[nid];
511 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
512 kobject_name(&dev->kobj));
514 EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
516 void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
518 struct node *node = &node_devices[nid];
519 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
521 EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
524 static void register_nodes(void)
531 /* Only valid if CPU is present. */
532 static ssize_t show_physical_id(struct sys_device *dev,
533 struct sysdev_attribute *attr, char *buf)
535 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
537 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
539 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
541 static int __init topology_init(void)
546 register_cpu_notifier(&sysfs_cpu_nb);
548 for_each_possible_cpu(cpu) {
549 struct cpu *c = &per_cpu(cpu_devices, cpu);
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
561 if (cpu_online(cpu) || c->hotpluggable) {
562 register_cpu(c, cpu);
564 sysdev_create_file(&c->sysdev, &attr_physical_id);
568 register_cpu_online(cpu);
573 subsys_initcall(topology_init);