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 <asm/current.h>
10 #include <asm/processor.h>
11 #include <asm/cputable.h>
12 #include <asm/hvcall.h>
18 #ifndef CONFIG_PPC_ISERIES
20 /* default to snooze disabled */
21 DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
23 static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
26 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
30 ret = sscanf(buf, "%lu", &snooze);
34 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
39 static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
41 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
43 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
46 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
47 store_smt_snooze_delay);
49 /* Only parse OF options if the matching cmdline option was not specified */
50 static int smt_snooze_cmdline;
52 static int __init smt_setup(void)
54 struct device_node *options;
58 if (!cur_cpu_spec->cpu_features & CPU_FTR_SMT)
61 options = find_path_device("/options");
65 val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
67 if (!smt_snooze_cmdline && val) {
69 per_cpu(smt_snooze_delay, cpu) = *val;
74 __initcall(smt_setup);
76 static int __init setup_smt_snooze_delay(char *str)
81 if (!cur_cpu_spec->cpu_features & CPU_FTR_SMT)
84 smt_snooze_cmdline = 1;
86 if (get_option(&str, &snooze)) {
88 per_cpu(smt_snooze_delay, cpu) = snooze;
93 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
100 #ifdef CONFIG_PPC_ISERIES
101 void ppc64_enable_pmcs(void)
103 /* XXX Implement for iseries */
107 #ifdef CONFIG_PPC_MULTIPLATFORM
109 * Enabling PMCs will slow partition context switch times so we only do
110 * it the first time we write to the PMCs.
113 static DEFINE_PER_CPU(char, pmcs_enabled);
115 void ppc64_enable_pmcs(void)
118 #ifdef CONFIG_PPC_PSERIES
119 unsigned long set, reset;
122 #endif /* CONFIG_PPC_PSERIES */
124 /* Only need to enable them once */
125 if (__get_cpu_var(pmcs_enabled))
128 __get_cpu_var(pmcs_enabled) = 1;
130 switch (systemcfg->platform) {
131 case PLATFORM_PSERIES:
132 case PLATFORM_POWERMAC:
134 hid0 |= 1UL << (63 - 20);
136 /* POWER4 requires the following sequence */
146 "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
150 #ifdef CONFIG_PPC_PSERIES
151 case PLATFORM_PSERIES_LPAR:
154 ret = plpar_hcall_norets(H_PERFMON, set, reset);
156 printk(KERN_ERR "H_PERFMON call on cpu %u "
158 smp_processor_id(), ret);
160 #endif /* CONFIG_PPC_PSERIES */
166 #ifdef CONFIG_PPC_PSERIES
167 /* instruct hypervisor to maintain PMCs */
168 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
169 char *ptr = (char *)&paca[smp_processor_id()].lppaca;
174 * On SMT machines we have to set the run latch in the ctrl register
175 * in order to make PMC6 spin.
177 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT) {
182 #endif /* CONFIG_PPC_PSERIES */
184 #endif /* CONFIG_PPC_MULTIPLATFORM */
186 EXPORT_SYMBOL(ppc64_enable_pmcs);
188 /* XXX convert to rusty's on_one_cpu */
189 static unsigned long run_on_cpu(unsigned long cpu,
190 unsigned long (*func)(unsigned long),
193 cpumask_t old_affinity = current->cpus_allowed;
196 /* should return -EINVAL to userspace */
197 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
202 set_cpus_allowed(current, old_affinity);
207 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
208 static unsigned long read_##NAME(unsigned long junk) \
210 return mfspr(ADDRESS); \
212 static unsigned long write_##NAME(unsigned long val) \
214 ppc64_enable_pmcs(); \
215 mtspr(ADDRESS, val); \
218 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
220 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
221 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
222 return sprintf(buf, "%lx\n", val); \
224 static ssize_t __attribute_used__ \
225 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
227 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
229 int ret = sscanf(buf, "%lx", &val); \
232 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
236 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
237 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
238 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
239 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
240 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
241 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
242 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
243 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
244 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
245 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
246 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
247 SYSFS_PMCSETUP(purr, SPRN_PURR);
249 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
250 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
251 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
252 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
253 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
254 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
255 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
256 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
257 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
258 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
259 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
260 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
262 static void __init register_cpu_pmc(struct sys_device *s)
264 sysdev_create_file(s, &attr_mmcr0);
265 sysdev_create_file(s, &attr_mmcr1);
267 if (cur_cpu_spec->cpu_features & CPU_FTR_MMCRA)
268 sysdev_create_file(s, &attr_mmcra);
270 sysdev_create_file(s, &attr_pmc1);
271 sysdev_create_file(s, &attr_pmc2);
272 sysdev_create_file(s, &attr_pmc3);
273 sysdev_create_file(s, &attr_pmc4);
274 sysdev_create_file(s, &attr_pmc5);
275 sysdev_create_file(s, &attr_pmc6);
277 if (cur_cpu_spec->cpu_features & CPU_FTR_PMC8) {
278 sysdev_create_file(s, &attr_pmc7);
279 sysdev_create_file(s, &attr_pmc8);
282 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
283 sysdev_create_file(s, &attr_purr);
290 static struct node node_devices[MAX_NUMNODES];
292 static void register_nodes(void)
296 for (i = 0; i < MAX_NUMNODES; i++) {
297 if (node_online(i)) {
298 int p_node = parent_node(i);
299 struct node *parent = NULL;
302 parent = &node_devices[p_node];
304 register_node(&node_devices[i], i, parent);
309 static void register_nodes(void)
316 /* Only valid if CPU is online. */
317 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
319 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
321 return sprintf(buf, "%u\n", get_hard_smp_processor_id(cpu->sysdev.id));
323 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
326 static DEFINE_PER_CPU(struct cpu, cpu_devices);
328 static int __init topology_init(void)
331 struct node *parent = NULL;
336 struct cpu *c = &per_cpu(cpu_devices, cpu);
339 parent = &node_devices[cpu_to_node(cpu)];
342 * For now, we just see if the system supports making
343 * the RTAS calls for CPU hotplug. But, there may be a
344 * more comprehensive way to do this for an individual
345 * CPU. For instance, the boot cpu might never be valid
348 if (systemcfg->platform != PLATFORM_PSERIES_LPAR)
351 register_cpu(c, cpu, parent);
353 register_cpu_pmc(&c->sysdev);
355 sysdev_create_file(&c->sysdev, &attr_physical_id);
357 #ifndef CONFIG_PPC_ISERIES
358 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
359 sysdev_create_file(&c->sysdev, &attr_smt_snooze_delay);
365 __initcall(topology_init);