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, const char *buf,
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
38 ret = sscanf(buf, "%lu", &snooze);
42 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
47 static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
49 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
51 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
54 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
55 store_smt_snooze_delay);
57 /* Only parse OF options if the matching cmdline option was not specified */
58 static int smt_snooze_cmdline;
60 static int __init smt_setup(void)
62 struct device_node *options;
63 const unsigned int *val;
66 if (!cpu_has_feature(CPU_FTR_SMT))
69 options = find_path_device("/options");
73 val = get_property(options, "ibm,smt-snooze-delay", NULL);
74 if (!smt_snooze_cmdline && val) {
75 for_each_possible_cpu(cpu)
76 per_cpu(smt_snooze_delay, cpu) = *val;
81 __initcall(smt_setup);
83 static int __init setup_smt_snooze_delay(char *str)
88 if (!cpu_has_feature(CPU_FTR_SMT))
91 smt_snooze_cmdline = 1;
93 if (get_option(&str, &snooze)) {
94 for_each_possible_cpu(cpu)
95 per_cpu(smt_snooze_delay, cpu) = snooze;
100 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
102 #endif /* CONFIG_PPC_MULTIPLATFORM */
105 * Enabling PMCs will slow partition context switch times so we only do
106 * it the first time we write to the PMCs.
109 static DEFINE_PER_CPU(char, pmcs_enabled);
111 void ppc64_enable_pmcs(void)
113 /* Only need to enable them once */
114 if (__get_cpu_var(pmcs_enabled))
117 __get_cpu_var(pmcs_enabled) = 1;
119 if (ppc_md.enable_pmcs)
120 ppc_md.enable_pmcs();
122 EXPORT_SYMBOL(ppc64_enable_pmcs);
124 /* XXX convert to rusty's on_one_cpu */
125 static unsigned long run_on_cpu(unsigned long cpu,
126 unsigned long (*func)(unsigned long),
129 cpumask_t old_affinity = current->cpus_allowed;
132 /* should return -EINVAL to userspace */
133 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
138 set_cpus_allowed(current, old_affinity);
143 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
144 static unsigned long read_##NAME(unsigned long junk) \
146 return mfspr(ADDRESS); \
148 static unsigned long write_##NAME(unsigned long val) \
150 ppc64_enable_pmcs(); \
151 mtspr(ADDRESS, val); \
154 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
156 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
157 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
158 return sprintf(buf, "%lx\n", val); \
160 static ssize_t __attribute_used__ \
161 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
163 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
165 int ret = sscanf(buf, "%lx", &val); \
168 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
172 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
173 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
174 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
175 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
176 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
177 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
178 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
179 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
180 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
181 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
182 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
183 SYSFS_PMCSETUP(purr, SPRN_PURR);
184 SYSFS_PMCSETUP(spurr, SPRN_SPURR);
185 SYSFS_PMCSETUP(dscr, SPRN_DSCR);
187 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
188 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
189 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
190 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
191 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
192 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
193 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
194 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
195 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
196 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
197 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
198 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
199 static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
200 static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
202 static void register_cpu_online(unsigned int cpu)
204 struct cpu *c = &per_cpu(cpu_devices, cpu);
205 struct sys_device *s = &c->sysdev;
207 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
208 cpu_has_feature(CPU_FTR_SMT))
209 sysdev_create_file(s, &attr_smt_snooze_delay);
213 sysdev_create_file(s, &attr_mmcr0);
214 sysdev_create_file(s, &attr_mmcr1);
216 if (cpu_has_feature(CPU_FTR_MMCRA))
217 sysdev_create_file(s, &attr_mmcra);
219 if (cur_cpu_spec->num_pmcs >= 1)
220 sysdev_create_file(s, &attr_pmc1);
221 if (cur_cpu_spec->num_pmcs >= 2)
222 sysdev_create_file(s, &attr_pmc2);
223 if (cur_cpu_spec->num_pmcs >= 3)
224 sysdev_create_file(s, &attr_pmc3);
225 if (cur_cpu_spec->num_pmcs >= 4)
226 sysdev_create_file(s, &attr_pmc4);
227 if (cur_cpu_spec->num_pmcs >= 5)
228 sysdev_create_file(s, &attr_pmc5);
229 if (cur_cpu_spec->num_pmcs >= 6)
230 sysdev_create_file(s, &attr_pmc6);
231 if (cur_cpu_spec->num_pmcs >= 7)
232 sysdev_create_file(s, &attr_pmc7);
233 if (cur_cpu_spec->num_pmcs >= 8)
234 sysdev_create_file(s, &attr_pmc8);
236 if (cpu_has_feature(CPU_FTR_PURR))
237 sysdev_create_file(s, &attr_purr);
239 if (cpu_has_feature(CPU_FTR_SPURR))
240 sysdev_create_file(s, &attr_spurr);
242 if (cpu_has_feature(CPU_FTR_DSCR))
243 sysdev_create_file(s, &attr_dscr);
246 #ifdef CONFIG_HOTPLUG_CPU
247 static void unregister_cpu_online(unsigned int cpu)
249 struct cpu *c = &per_cpu(cpu_devices, cpu);
250 struct sys_device *s = &c->sysdev;
252 BUG_ON(!c->hotpluggable);
254 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
255 cpu_has_feature(CPU_FTR_SMT))
256 sysdev_remove_file(s, &attr_smt_snooze_delay);
260 sysdev_remove_file(s, &attr_mmcr0);
261 sysdev_remove_file(s, &attr_mmcr1);
263 if (cpu_has_feature(CPU_FTR_MMCRA))
264 sysdev_remove_file(s, &attr_mmcra);
266 if (cur_cpu_spec->num_pmcs >= 1)
267 sysdev_remove_file(s, &attr_pmc1);
268 if (cur_cpu_spec->num_pmcs >= 2)
269 sysdev_remove_file(s, &attr_pmc2);
270 if (cur_cpu_spec->num_pmcs >= 3)
271 sysdev_remove_file(s, &attr_pmc3);
272 if (cur_cpu_spec->num_pmcs >= 4)
273 sysdev_remove_file(s, &attr_pmc4);
274 if (cur_cpu_spec->num_pmcs >= 5)
275 sysdev_remove_file(s, &attr_pmc5);
276 if (cur_cpu_spec->num_pmcs >= 6)
277 sysdev_remove_file(s, &attr_pmc6);
278 if (cur_cpu_spec->num_pmcs >= 7)
279 sysdev_remove_file(s, &attr_pmc7);
280 if (cur_cpu_spec->num_pmcs >= 8)
281 sysdev_remove_file(s, &attr_pmc8);
283 if (cpu_has_feature(CPU_FTR_PURR))
284 sysdev_remove_file(s, &attr_purr);
286 if (cpu_has_feature(CPU_FTR_SPURR))
287 sysdev_remove_file(s, &attr_spurr);
289 if (cpu_has_feature(CPU_FTR_DSCR))
290 sysdev_remove_file(s, &attr_dscr);
292 #endif /* CONFIG_HOTPLUG_CPU */
294 static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
295 unsigned long action, void *hcpu)
297 unsigned int cpu = (unsigned int)(long)hcpu;
301 register_cpu_online(cpu);
303 #ifdef CONFIG_HOTPLUG_CPU
305 unregister_cpu_online(cpu);
312 static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
313 .notifier_call = sysfs_cpu_notify,
316 static DEFINE_MUTEX(cpu_mutex);
318 int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
322 mutex_lock(&cpu_mutex);
324 for_each_possible_cpu(cpu) {
325 sysdev_create_file(get_cpu_sysdev(cpu), attr);
328 mutex_unlock(&cpu_mutex);
331 EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
333 int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
336 struct sys_device *sysdev;
338 mutex_lock(&cpu_mutex);
340 for_each_possible_cpu(cpu) {
341 sysdev = get_cpu_sysdev(cpu);
342 sysfs_create_group(&sysdev->kobj, attrs);
345 mutex_unlock(&cpu_mutex);
348 EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
351 void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
355 mutex_lock(&cpu_mutex);
357 for_each_possible_cpu(cpu) {
358 sysdev_remove_file(get_cpu_sysdev(cpu), attr);
361 mutex_unlock(&cpu_mutex);
363 EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
365 void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
368 struct sys_device *sysdev;
370 mutex_lock(&cpu_mutex);
372 for_each_possible_cpu(cpu) {
373 sysdev = get_cpu_sysdev(cpu);
374 sysfs_remove_group(&sysdev->kobj, attrs);
377 mutex_unlock(&cpu_mutex);
379 EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
385 static void register_nodes(void)
389 for (i = 0; i < MAX_NUMNODES; i++)
390 register_one_node(i);
393 int sysfs_add_device_to_node(struct sys_device *dev, int nid)
395 struct node *node = &node_devices[nid];
396 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
397 kobject_name(&dev->kobj));
400 void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
402 struct node *node = &node_devices[nid];
403 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
407 static void register_nodes(void)
414 EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
415 EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
417 /* Only valid if CPU is present. */
418 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
420 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
422 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
424 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
426 static int __init topology_init(void)
431 register_cpu_notifier(&sysfs_cpu_nb);
433 for_each_possible_cpu(cpu) {
434 struct cpu *c = &per_cpu(cpu_devices, cpu);
437 * For now, we just see if the system supports making
438 * the RTAS calls for CPU hotplug. But, there may be a
439 * more comprehensive way to do this for an individual
440 * CPU. For instance, the boot cpu might never be valid
446 if (cpu_online(cpu) || c->hotpluggable) {
447 register_cpu(c, cpu);
449 sysdev_create_file(&c->sysdev, &attr_physical_id);
453 register_cpu_online(cpu);
458 __initcall(topology_init);