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);
101 * Enabling PMCs will slow partition context switch times so we only do
102 * it the first time we write to the PMCs.
105 static DEFINE_PER_CPU(char, pmcs_enabled);
107 #ifdef CONFIG_PPC_ISERIES
108 void ppc64_enable_pmcs(void)
110 /* XXX Implement for iseries */
113 void ppc64_enable_pmcs(void)
116 unsigned long set, reset;
120 /* Only need to enable them once */
121 if (__get_cpu_var(pmcs_enabled))
124 __get_cpu_var(pmcs_enabled) = 1;
126 switch (systemcfg->platform) {
127 case PLATFORM_PSERIES:
129 hid0 |= 1UL << (63 - 20);
131 /* POWER4 requires the following sequence */
141 "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
145 case PLATFORM_PSERIES_LPAR:
148 ret = plpar_hcall_norets(H_PERFMON, set, reset);
150 printk(KERN_ERR "H_PERFMON call returned %d",
158 /* instruct hypervisor to maintain PMCs */
159 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
160 char *ptr = (char *)&paca[smp_processor_id()].lppaca;
165 * On SMT machines we have to set the run latch in the ctrl register
166 * in order to make PMC6 spin.
168 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT) {
176 EXPORT_SYMBOL_GPL(ppc64_enable_pmcs);
178 /* XXX convert to rusty's on_one_cpu */
179 static unsigned long run_on_cpu(unsigned long cpu,
180 unsigned long (*func)(unsigned long),
183 cpumask_t old_affinity = current->cpus_allowed;
186 /* should return -EINVAL to userspace */
187 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
192 set_cpus_allowed(current, old_affinity);
197 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
198 static unsigned long read_##NAME(unsigned long junk) \
200 return mfspr(ADDRESS); \
202 static unsigned long write_##NAME(unsigned long val) \
204 ppc64_enable_pmcs(); \
205 mtspr(ADDRESS, val); \
208 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
210 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
211 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
212 return sprintf(buf, "%lx\n", val); \
214 static ssize_t __attribute_used__ \
215 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
217 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
219 int ret = sscanf(buf, "%lx", &val); \
222 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
226 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
227 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
228 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
229 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
230 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
231 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
232 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
233 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
234 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
235 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
236 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
237 SYSFS_PMCSETUP(purr, SPRN_PURR);
239 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
240 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
241 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
242 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
243 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
244 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
245 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
246 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
247 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
248 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
249 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
250 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
252 static void __init register_cpu_pmc(struct sys_device *s)
254 sysdev_create_file(s, &attr_mmcr0);
255 sysdev_create_file(s, &attr_mmcr1);
257 if (cur_cpu_spec->cpu_features & CPU_FTR_MMCRA)
258 sysdev_create_file(s, &attr_mmcra);
260 sysdev_create_file(s, &attr_pmc1);
261 sysdev_create_file(s, &attr_pmc2);
262 sysdev_create_file(s, &attr_pmc3);
263 sysdev_create_file(s, &attr_pmc4);
264 sysdev_create_file(s, &attr_pmc5);
265 sysdev_create_file(s, &attr_pmc6);
267 if (cur_cpu_spec->cpu_features & CPU_FTR_PMC8) {
268 sysdev_create_file(s, &attr_pmc7);
269 sysdev_create_file(s, &attr_pmc8);
272 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
273 sysdev_create_file(s, &attr_purr);
280 static struct node node_devices[MAX_NUMNODES];
282 static void register_nodes(void)
286 for (i = 0; i < MAX_NUMNODES; i++) {
287 if (node_online(i)) {
288 int p_node = parent_node(i);
289 struct node *parent = NULL;
292 parent = &node_devices[p_node];
294 register_node(&node_devices[i], i, parent);
299 static void register_nodes(void)
306 /* Only valid if CPU is online. */
307 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
309 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
311 return sprintf(buf, "%u\n", get_hard_smp_processor_id(cpu->sysdev.id));
313 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
316 static DEFINE_PER_CPU(struct cpu, cpu_devices);
318 static int __init topology_init(void)
321 struct node *parent = NULL;
326 struct cpu *c = &per_cpu(cpu_devices, cpu);
329 parent = &node_devices[cpu_to_node(cpu)];
332 * For now, we just see if the system supports making
333 * the RTAS calls for CPU hotplug. But, there may be a
334 * more comprehensive way to do this for an individual
335 * CPU. For instance, the boot cpu might never be valid
338 if (systemcfg->platform != PLATFORM_PSERIES_LPAR)
341 register_cpu(c, cpu, parent);
343 register_cpu_pmc(&c->sysdev);
345 sysdev_create_file(&c->sysdev, &attr_physical_id);
347 #ifndef CONFIG_PPC_ISERIES
348 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
349 sysdev_create_file(&c->sysdev, &attr_smt_snooze_delay);
355 __initcall(topology_init);