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 /* default to snooze disabled */
29 DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
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;
66 if (!cpu_has_feature(CPU_FTR_SMT))
69 options = find_path_device("/options");
73 val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
75 if (!smt_snooze_cmdline && val) {
76 for_each_possible_cpu(cpu)
77 per_cpu(smt_snooze_delay, cpu) = *val;
82 __initcall(smt_setup);
84 static int __init setup_smt_snooze_delay(char *str)
89 if (!cpu_has_feature(CPU_FTR_SMT))
92 smt_snooze_cmdline = 1;
94 if (get_option(&str, &snooze)) {
95 for_each_possible_cpu(cpu)
96 per_cpu(smt_snooze_delay, cpu) = snooze;
101 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
103 #endif /* CONFIG_PPC_MULTIPLATFORM */
106 * Enabling PMCs will slow partition context switch times so we only do
107 * it the first time we write to the PMCs.
110 static DEFINE_PER_CPU(char, pmcs_enabled);
112 void ppc64_enable_pmcs(void)
114 /* Only need to enable them once */
115 if (__get_cpu_var(pmcs_enabled))
118 __get_cpu_var(pmcs_enabled) = 1;
120 if (ppc_md.enable_pmcs)
121 ppc_md.enable_pmcs();
123 EXPORT_SYMBOL(ppc64_enable_pmcs);
125 /* XXX convert to rusty's on_one_cpu */
126 static unsigned long run_on_cpu(unsigned long cpu,
127 unsigned long (*func)(unsigned long),
130 cpumask_t old_affinity = current->cpus_allowed;
133 /* should return -EINVAL to userspace */
134 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
139 set_cpus_allowed(current, old_affinity);
144 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
145 static unsigned long read_##NAME(unsigned long junk) \
147 return mfspr(ADDRESS); \
149 static unsigned long write_##NAME(unsigned long val) \
151 ppc64_enable_pmcs(); \
152 mtspr(ADDRESS, val); \
155 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
157 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
158 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
159 return sprintf(buf, "%lx\n", val); \
161 static ssize_t __attribute_used__ \
162 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
164 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
166 int ret = sscanf(buf, "%lx", &val); \
169 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
173 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
174 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
175 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
176 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
177 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
178 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
179 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
180 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
181 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
182 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
183 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
184 SYSFS_PMCSETUP(purr, SPRN_PURR);
186 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
187 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
188 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
189 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
190 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
191 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
192 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
193 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
194 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
195 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
196 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
197 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
199 static void register_cpu_online(unsigned int cpu)
201 struct cpu *c = &per_cpu(cpu_devices, cpu);
202 struct sys_device *s = &c->sysdev;
204 #ifndef CONFIG_PPC_ISERIES
205 if (cpu_has_feature(CPU_FTR_SMT))
206 sysdev_create_file(s, &attr_smt_snooze_delay);
211 sysdev_create_file(s, &attr_mmcr0);
212 sysdev_create_file(s, &attr_mmcr1);
214 if (cpu_has_feature(CPU_FTR_MMCRA))
215 sysdev_create_file(s, &attr_mmcra);
217 if (cur_cpu_spec->num_pmcs >= 1)
218 sysdev_create_file(s, &attr_pmc1);
219 if (cur_cpu_spec->num_pmcs >= 2)
220 sysdev_create_file(s, &attr_pmc2);
221 if (cur_cpu_spec->num_pmcs >= 3)
222 sysdev_create_file(s, &attr_pmc3);
223 if (cur_cpu_spec->num_pmcs >= 4)
224 sysdev_create_file(s, &attr_pmc4);
225 if (cur_cpu_spec->num_pmcs >= 5)
226 sysdev_create_file(s, &attr_pmc5);
227 if (cur_cpu_spec->num_pmcs >= 6)
228 sysdev_create_file(s, &attr_pmc6);
229 if (cur_cpu_spec->num_pmcs >= 7)
230 sysdev_create_file(s, &attr_pmc7);
231 if (cur_cpu_spec->num_pmcs >= 8)
232 sysdev_create_file(s, &attr_pmc8);
234 if (cpu_has_feature(CPU_FTR_SMT))
235 sysdev_create_file(s, &attr_purr);
238 #ifdef CONFIG_HOTPLUG_CPU
239 static void unregister_cpu_online(unsigned int cpu)
241 struct cpu *c = &per_cpu(cpu_devices, cpu);
242 struct sys_device *s = &c->sysdev;
244 BUG_ON(c->no_control);
246 #ifndef CONFIG_PPC_ISERIES
247 if (cpu_has_feature(CPU_FTR_SMT))
248 sysdev_remove_file(s, &attr_smt_snooze_delay);
253 sysdev_remove_file(s, &attr_mmcr0);
254 sysdev_remove_file(s, &attr_mmcr1);
256 if (cpu_has_feature(CPU_FTR_MMCRA))
257 sysdev_remove_file(s, &attr_mmcra);
259 if (cur_cpu_spec->num_pmcs >= 1)
260 sysdev_remove_file(s, &attr_pmc1);
261 if (cur_cpu_spec->num_pmcs >= 2)
262 sysdev_remove_file(s, &attr_pmc2);
263 if (cur_cpu_spec->num_pmcs >= 3)
264 sysdev_remove_file(s, &attr_pmc3);
265 if (cur_cpu_spec->num_pmcs >= 4)
266 sysdev_remove_file(s, &attr_pmc4);
267 if (cur_cpu_spec->num_pmcs >= 5)
268 sysdev_remove_file(s, &attr_pmc5);
269 if (cur_cpu_spec->num_pmcs >= 6)
270 sysdev_remove_file(s, &attr_pmc6);
271 if (cur_cpu_spec->num_pmcs >= 7)
272 sysdev_remove_file(s, &attr_pmc7);
273 if (cur_cpu_spec->num_pmcs >= 8)
274 sysdev_remove_file(s, &attr_pmc8);
276 if (cpu_has_feature(CPU_FTR_SMT))
277 sysdev_remove_file(s, &attr_purr);
279 #endif /* CONFIG_HOTPLUG_CPU */
281 static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
282 unsigned long action, void *hcpu)
284 unsigned int cpu = (unsigned int)(long)hcpu;
288 register_cpu_online(cpu);
290 #ifdef CONFIG_HOTPLUG_CPU
292 unregister_cpu_online(cpu);
299 static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
300 .notifier_call = sysfs_cpu_notify,
306 static void register_nodes(void)
310 for (i = 0; i < MAX_NUMNODES; i++)
311 register_one_node(i);
314 int sysfs_add_device_to_node(struct sys_device *dev, int nid)
316 struct node *node = &node_devices[nid];
317 return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
318 kobject_name(&dev->kobj));
321 void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
323 struct node *node = &node_devices[nid];
324 sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
328 static void register_nodes(void)
335 EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
336 EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
338 /* Only valid if CPU is present. */
339 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
341 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
343 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
345 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
347 static int __init topology_init(void)
352 register_cpu_notifier(&sysfs_cpu_nb);
354 for_each_possible_cpu(cpu) {
355 struct cpu *c = &per_cpu(cpu_devices, cpu);
358 * For now, we just see if the system supports making
359 * the RTAS calls for CPU hotplug. But, there may be a
360 * more comprehensive way to do this for an individual
361 * CPU. For instance, the boot cpu might never be valid
367 if (cpu_online(cpu) || (c->no_control == 0)) {
368 register_cpu(c, cpu);
370 sysdev_create_file(&c->sysdev, &attr_physical_id);
374 register_cpu_online(cpu);
379 __initcall(topology_init);