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 <asm/current.h>
9 #include <asm/processor.h>
10 #include <asm/cputable.h>
11 #include <asm/hvcall.h>
17 #ifndef CONFIG_PPC_ISERIES
19 /* default to snooze disabled */
20 DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
22 static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
25 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
29 ret = sscanf(buf, "%lu", &snooze);
33 per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
38 static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
40 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
42 return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
45 static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
46 store_smt_snooze_delay);
48 /* Only parse OF options if the matching cmdline option was not specified */
49 static int smt_snooze_cmdline;
51 static int __init smt_setup(void)
53 struct device_node *options;
57 if (!cur_cpu_spec->cpu_features & CPU_FTR_SMT)
60 options = find_path_device("/options");
64 val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
66 if (!smt_snooze_cmdline && val) {
68 per_cpu(smt_snooze_delay, cpu) = *val;
73 __initcall(smt_setup);
75 static int __init setup_smt_snooze_delay(char *str)
80 if (!cur_cpu_spec->cpu_features & CPU_FTR_SMT)
83 smt_snooze_cmdline = 1;
85 if (get_option(&str, &snooze)) {
87 per_cpu(smt_snooze_delay, cpu) = snooze;
92 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
100 * Enabling PMCs will slow partition context switch times so we only do
101 * it the first time we write to the PMCs.
104 static DEFINE_PER_CPU(char, pmcs_enabled);
106 #ifdef CONFIG_PPC_ISERIES
107 void ppc64_enable_pmcs(void)
109 /* XXX Implement for iseries */
112 void ppc64_enable_pmcs(void)
115 unsigned long set, reset;
119 /* Only need to enable them once */
120 if (__get_cpu_var(pmcs_enabled))
123 __get_cpu_var(pmcs_enabled) = 1;
125 switch (systemcfg->platform) {
126 case PLATFORM_PSERIES:
128 hid0 |= 1UL << (63 - 20);
130 /* POWER4 requires the following sequence */
140 "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
144 case PLATFORM_PSERIES_LPAR:
147 ret = plpar_hcall_norets(H_PERFMON, set, reset);
149 printk(KERN_ERR "H_PERFMON call returned %d",
157 /* instruct hypervisor to maintain PMCs */
158 if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR) {
159 char *ptr = (char *)&paca[smp_processor_id()].lppaca;
164 * On SMT machines we have to set the run latch in the ctrl register
165 * in order to make PMC6 spin.
167 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT) {
175 /* XXX convert to rusty's on_one_cpu */
176 static unsigned long run_on_cpu(unsigned long cpu,
177 unsigned long (*func)(unsigned long),
180 cpumask_t old_affinity = current->cpus_allowed;
183 /* should return -EINVAL to userspace */
184 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
189 set_cpus_allowed(current, old_affinity);
194 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
195 static unsigned long read_##NAME(unsigned long junk) \
197 return mfspr(ADDRESS); \
199 static unsigned long write_##NAME(unsigned long val) \
201 ppc64_enable_pmcs(); \
202 mtspr(ADDRESS, val); \
205 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
207 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
208 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
209 return sprintf(buf, "%lx\n", val); \
211 static ssize_t __attribute_used__ \
212 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
214 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
216 int ret = sscanf(buf, "%lx", &val); \
219 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
223 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
224 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
225 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
226 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
227 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
228 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
229 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
230 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
231 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
232 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
233 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
234 SYSFS_PMCSETUP(purr, SPRN_PURR);
236 static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
237 static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
238 static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
239 static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
240 static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
241 static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
242 static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
243 static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
244 static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
245 static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
246 static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
247 static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
249 static void __init register_cpu_pmc(struct sys_device *s)
251 sysdev_create_file(s, &attr_mmcr0);
252 sysdev_create_file(s, &attr_mmcr1);
254 if (cur_cpu_spec->cpu_features & CPU_FTR_MMCRA)
255 sysdev_create_file(s, &attr_mmcra);
257 sysdev_create_file(s, &attr_pmc1);
258 sysdev_create_file(s, &attr_pmc2);
259 sysdev_create_file(s, &attr_pmc3);
260 sysdev_create_file(s, &attr_pmc4);
261 sysdev_create_file(s, &attr_pmc5);
262 sysdev_create_file(s, &attr_pmc6);
264 if (cur_cpu_spec->cpu_features & CPU_FTR_PMC8) {
265 sysdev_create_file(s, &attr_pmc7);
266 sysdev_create_file(s, &attr_pmc8);
269 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
270 sysdev_create_file(s, &attr_purr);
277 static struct node node_devices[MAX_NUMNODES];
279 static void register_nodes(void)
283 for (i = 0; i < MAX_NUMNODES; i++) {
284 if (node_online(i)) {
285 int p_node = parent_node(i);
286 struct node *parent = NULL;
289 parent = &node_devices[p_node];
291 register_node(&node_devices[i], i, parent);
296 static void register_nodes(void)
303 /* Only valid if CPU is online. */
304 static ssize_t show_physical_id(struct sys_device *dev, char *buf)
306 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
308 return sprintf(buf, "%u\n", get_hard_smp_processor_id(cpu->sysdev.id));
310 static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
313 static DEFINE_PER_CPU(struct cpu, cpu_devices);
315 static int __init topology_init(void)
318 struct node *parent = NULL;
323 struct cpu *c = &per_cpu(cpu_devices, cpu);
326 parent = &node_devices[cpu_to_node(cpu)];
329 * For now, we just see if the system supports making
330 * the RTAS calls for CPU hotplug. But, there may be a
331 * more comprehensive way to do this for an individual
332 * CPU. For instance, the boot cpu might never be valid
335 if (systemcfg->platform != PLATFORM_PSERIES_LPAR)
338 register_cpu(c, cpu, parent);
340 register_cpu_pmc(&c->sysdev);
342 sysdev_create_file(&c->sysdev, &attr_physical_id);
344 #ifndef CONFIG_PPC_ISERIES
345 if (cur_cpu_spec->cpu_features & CPU_FTR_SMT)
346 sysdev_create_file(&c->sysdev, &attr_smt_snooze_delay);
352 __initcall(topology_init);