1 #include <linux/config.h>
2 #include <linux/sysdev.h>
5 #include <linux/percpu.h>
6 #include <linux/init.h>
7 #include <linux/sched.h>
8 #include <linux/module.h>
9 #include <linux/nodemask.h>
10 #include <linux/cpumask.h>
11 #include <linux/notifier.h>
13 #include <asm/current.h>
14 #include <asm/processor.h>
15 #include <asm/cputable.h>
16 #include <asm/firmware.h>
17 #include <asm/hvcall.h>
19 #include <asm/systemcfg.h>
21 #include <asm/lppaca.h>
22 #include <asm/machdep.h>
25 static DEFINE_PER_CPU(struct cpu, cpu_devices);
29 #ifdef CONFIG_PPC_MULTIPLATFORM
30 /* default to snooze disabled */
31 DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
33 static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
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, char *buf)
51 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
53 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
56 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
57 store_smt_snooze_delay);
59 /* Only parse OF options if the matching cmdline option was not specified */
60 static int smt_snooze_cmdline;
62 static int __init smt_setup(void)
64 struct device_node *options;
68 if (!cpu_has_feature(CPU_FTR_SMT))
71 options = find_path_device("/options");
75 val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
77 if (!smt_snooze_cmdline && val) {
79 per_cpu(smt_snooze_delay, cpu) = *val;
84 __initcall(smt_setup);
86 static int __init setup_smt_snooze_delay(char *str)
91 if (!cpu_has_feature(CPU_FTR_SMT))
94 smt_snooze_cmdline = 1;
96 if (get_option(&str, &snooze)) {
98 per_cpu(smt_snooze_delay, cpu) = snooze;
103 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
105 #endif /* CONFIG_PPC_MULTIPLATFORM */
108 * Enabling PMCs will slow partition context switch times so we only do
109 * it the first time we write to the PMCs.
112 static DEFINE_PER_CPU(char, pmcs_enabled);
114 void ppc64_enable_pmcs(void)
116 /* Only need to enable them once */
117 if (__get_cpu_var(pmcs_enabled))
120 __get_cpu_var(pmcs_enabled) = 1;
122 if (ppc_md.enable_pmcs)
123 ppc_md.enable_pmcs();
125 EXPORT_SYMBOL(ppc64_enable_pmcs);
127 /* XXX convert to rusty's on_one_cpu */
128 static unsigned long run_on_cpu(unsigned long cpu,
129 unsigned long (*func)(unsigned long),
132 cpumask_t old_affinity = current->cpus_allowed;
135 /* should return -EINVAL to userspace */
136 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
141 set_cpus_allowed(current, old_affinity);
146 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
147 static unsigned long read_##NAME(unsigned long junk) \
149 return mfspr(ADDRESS); \
151 static unsigned long write_##NAME(unsigned long val) \
153 ppc64_enable_pmcs(); \
154 mtspr(ADDRESS, val); \
157 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
159 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
160 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
161 return sprintf(buf, "%lx\n", val); \
163 static ssize_t __attribute_used__ \
164 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
166 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
168 int ret = sscanf(buf, "%lx", &val); \
171 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
175 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
176 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
177 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
178 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
179 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
180 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
181 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
182 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
183 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
184 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
185 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
186 SYSFS_PMCSETUP(purr, SPRN_PURR);
188 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
189 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
190 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
191 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
192 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
193 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
194 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
195 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
196 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
197 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
198 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
199 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
201 static void register_cpu_online(unsigned int cpu)
203 struct cpu *c = &per_cpu(cpu_devices, cpu);
204 struct sys_device *s = &c->sysdev;
206 #ifndef CONFIG_PPC_ISERIES
207 if (cpu_has_feature(CPU_FTR_SMT))
208 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_SMT))
237 sysdev_create_file(s, &attr_purr);
240 #ifdef CONFIG_HOTPLUG_CPU
241 static void unregister_cpu_online(unsigned int cpu)
243 struct cpu *c = &per_cpu(cpu_devices, cpu);
244 struct sys_device *s = &c->sysdev;
246 BUG_ON(c->no_control);
248 #ifndef CONFIG_PPC_ISERIES
249 if (cpu_has_feature(CPU_FTR_SMT))
250 sysdev_remove_file(s, &attr_smt_snooze_delay);
255 sysdev_remove_file(s, &attr_mmcr0);
256 sysdev_remove_file(s, &attr_mmcr1);
258 if (cpu_has_feature(CPU_FTR_MMCRA))
259 sysdev_remove_file(s, &attr_mmcra);
261 if (cur_cpu_spec->num_pmcs >= 1)
262 sysdev_remove_file(s, &attr_pmc1);
263 if (cur_cpu_spec->num_pmcs >= 2)
264 sysdev_remove_file(s, &attr_pmc2);
265 if (cur_cpu_spec->num_pmcs >= 3)
266 sysdev_remove_file(s, &attr_pmc3);
267 if (cur_cpu_spec->num_pmcs >= 4)
268 sysdev_remove_file(s, &attr_pmc4);
269 if (cur_cpu_spec->num_pmcs >= 5)
270 sysdev_remove_file(s, &attr_pmc5);
271 if (cur_cpu_spec->num_pmcs >= 6)
272 sysdev_remove_file(s, &attr_pmc6);
273 if (cur_cpu_spec->num_pmcs >= 7)
274 sysdev_remove_file(s, &attr_pmc7);
275 if (cur_cpu_spec->num_pmcs >= 8)
276 sysdev_remove_file(s, &attr_pmc8);
278 if (cpu_has_feature(CPU_FTR_SMT))
279 sysdev_remove_file(s, &attr_purr);
281 #endif /* CONFIG_HOTPLUG_CPU */
283 static int __devinit sysfs_cpu_notify(struct notifier_block *self,
284 unsigned long action, void *hcpu)
286 unsigned int cpu = (unsigned int)(long)hcpu;
290 register_cpu_online(cpu);
292 #ifdef CONFIG_HOTPLUG_CPU
294 unregister_cpu_online(cpu);
301 static struct notifier_block __devinitdata sysfs_cpu_nb = {
302 .notifier_call = sysfs_cpu_notify,
308 static struct node node_devices[MAX_NUMNODES];
310 static void register_nodes(void)
314 for (i = 0; i < MAX_NUMNODES; i++) {
315 if (node_online(i)) {
316 int p_node = parent_node(i);
317 struct node *parent = NULL;
320 parent = &node_devices[p_node];
322 register_node(&node_devices[i], i, parent);
327 static void register_nodes(void)
333 /* Only valid if CPU is present. */
334 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
336 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
338 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
340 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
342 static int __init topology_init(void)
345 struct node *parent = NULL;
349 register_cpu_notifier(&sysfs_cpu_nb);
352 struct cpu *c = &per_cpu(cpu_devices, cpu);
355 /* The node to which a cpu belongs can't be known
356 * until the cpu is made present.
359 if (cpu_present(cpu))
360 parent = &node_devices[cpu_to_node(cpu)];
363 * For now, we just see if the system supports making
364 * the RTAS calls for CPU hotplug. But, there may be a
365 * more comprehensive way to do this for an individual
366 * CPU. For instance, the boot cpu might never be valid
372 if (cpu_online(cpu) || (c->no_control == 0)) {
373 register_cpu(c, cpu, parent);
375 sysdev_create_file(&c->sysdev, &attr_physical_id);
379 register_cpu_online(cpu);
384 __initcall(topology_init);