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>
24 static DEFINE_PER_CPU(struct cpu
, cpu_devices
);
28 #ifdef CONFIG_PPC_MULTIPLATFORM
29 /* default to snooze disabled */
30 DEFINE_PER_CPU(unsigned long, smt_snooze_delay
);
32 static ssize_t
store_smt_snooze_delay(struct sys_device
*dev
, const char *buf
,
35 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
39 ret
= sscanf(buf
, "%lu", &snooze
);
43 per_cpu(smt_snooze_delay
, cpu
->sysdev
.id
) = snooze
;
48 static ssize_t
show_smt_snooze_delay(struct sys_device
*dev
, char *buf
)
50 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
52 return sprintf(buf
, "%lu\n", per_cpu(smt_snooze_delay
, cpu
->sysdev
.id
));
55 static SYSDEV_ATTR(smt_snooze_delay
, 0644, show_smt_snooze_delay
,
56 store_smt_snooze_delay
);
58 /* Only parse OF options if the matching cmdline option was not specified */
59 static int smt_snooze_cmdline
;
61 static int __init
smt_setup(void)
63 struct device_node
*options
;
67 if (!cpu_has_feature(CPU_FTR_SMT
))
70 options
= find_path_device("/options");
74 val
= (unsigned int *)get_property(options
, "ibm,smt-snooze-delay",
76 if (!smt_snooze_cmdline
&& val
) {
78 per_cpu(smt_snooze_delay
, cpu
) = *val
;
83 __initcall(smt_setup
);
85 static int __init
setup_smt_snooze_delay(char *str
)
90 if (!cpu_has_feature(CPU_FTR_SMT
))
93 smt_snooze_cmdline
= 1;
95 if (get_option(&str
, &snooze
)) {
97 per_cpu(smt_snooze_delay
, cpu
) = snooze
;
102 __setup("smt-snooze-delay=", setup_smt_snooze_delay
);
105 * Enabling PMCs will slow partition context switch times so we only do
106 * it the first time we write to the PMCs.
109 static DEFINE_PER_CPU(char, pmcs_enabled
);
111 void ppc64_enable_pmcs(void)
114 #ifdef CONFIG_PPC_PSERIES
115 unsigned long set
, reset
;
116 #endif /* CONFIG_PPC_PSERIES */
118 /* Only need to enable them once */
119 if (__get_cpu_var(pmcs_enabled
))
122 __get_cpu_var(pmcs_enabled
) = 1;
124 switch (systemcfg
->platform
) {
125 case PLATFORM_PSERIES
:
126 case PLATFORM_POWERMAC
:
128 hid0
|= 1UL << (63 - 20);
130 /* POWER4 requires the following sequence */
140 "isync" : "=&r" (hid0
) : "i" (HID0
), "0" (hid0
):
144 #ifdef CONFIG_PPC_PSERIES
145 case PLATFORM_PSERIES_LPAR
:
148 plpar_hcall_norets(H_PERFMON
, set
, reset
);
150 #endif /* CONFIG_PPC_PSERIES */
156 #ifdef CONFIG_PPC_PSERIES
157 /* instruct hypervisor to maintain PMCs */
158 if (firmware_has_feature(FW_FEATURE_SPLPAR
))
159 get_paca()->lppaca
.pmcregs_in_use
= 1;
160 #endif /* CONFIG_PPC_PSERIES */
166 void ppc64_enable_pmcs(void)
168 /* XXX Implement for iseries */
170 #endif /* CONFIG_PPC_MULTIPLATFORM */
172 EXPORT_SYMBOL(ppc64_enable_pmcs
);
174 /* XXX convert to rusty's on_one_cpu */
175 static unsigned long run_on_cpu(unsigned long cpu
,
176 unsigned long (*func
)(unsigned long),
179 cpumask_t old_affinity
= current
->cpus_allowed
;
182 /* should return -EINVAL to userspace */
183 if (set_cpus_allowed(current
, cpumask_of_cpu(cpu
)))
188 set_cpus_allowed(current
, old_affinity
);
193 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
194 static unsigned long read_##NAME(unsigned long junk) \
196 return mfspr(ADDRESS); \
198 static unsigned long write_##NAME(unsigned long val) \
200 ppc64_enable_pmcs(); \
201 mtspr(ADDRESS, val); \
204 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
206 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
207 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
208 return sprintf(buf, "%lx\n", val); \
210 static ssize_t __attribute_used__ \
211 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
213 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
215 int ret = sscanf(buf, "%lx", &val); \
218 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
222 SYSFS_PMCSETUP(mmcr0
, SPRN_MMCR0
);
223 SYSFS_PMCSETUP(mmcr1
, SPRN_MMCR1
);
224 SYSFS_PMCSETUP(mmcra
, SPRN_MMCRA
);
225 SYSFS_PMCSETUP(pmc1
, SPRN_PMC1
);
226 SYSFS_PMCSETUP(pmc2
, SPRN_PMC2
);
227 SYSFS_PMCSETUP(pmc3
, SPRN_PMC3
);
228 SYSFS_PMCSETUP(pmc4
, SPRN_PMC4
);
229 SYSFS_PMCSETUP(pmc5
, SPRN_PMC5
);
230 SYSFS_PMCSETUP(pmc6
, SPRN_PMC6
);
231 SYSFS_PMCSETUP(pmc7
, SPRN_PMC7
);
232 SYSFS_PMCSETUP(pmc8
, SPRN_PMC8
);
233 SYSFS_PMCSETUP(purr
, SPRN_PURR
);
235 static SYSDEV_ATTR(mmcr0
, 0600, show_mmcr0
, store_mmcr0
);
236 static SYSDEV_ATTR(mmcr1
, 0600, show_mmcr1
, store_mmcr1
);
237 static SYSDEV_ATTR(mmcra
, 0600, show_mmcra
, store_mmcra
);
238 static SYSDEV_ATTR(pmc1
, 0600, show_pmc1
, store_pmc1
);
239 static SYSDEV_ATTR(pmc2
, 0600, show_pmc2
, store_pmc2
);
240 static SYSDEV_ATTR(pmc3
, 0600, show_pmc3
, store_pmc3
);
241 static SYSDEV_ATTR(pmc4
, 0600, show_pmc4
, store_pmc4
);
242 static SYSDEV_ATTR(pmc5
, 0600, show_pmc5
, store_pmc5
);
243 static SYSDEV_ATTR(pmc6
, 0600, show_pmc6
, store_pmc6
);
244 static SYSDEV_ATTR(pmc7
, 0600, show_pmc7
, store_pmc7
);
245 static SYSDEV_ATTR(pmc8
, 0600, show_pmc8
, store_pmc8
);
246 static SYSDEV_ATTR(purr
, 0600, show_purr
, NULL
);
248 static void register_cpu_online(unsigned int cpu
)
250 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
251 struct sys_device
*s
= &c
->sysdev
;
253 #ifndef CONFIG_PPC_ISERIES
254 if (cpu_has_feature(CPU_FTR_SMT
))
255 sysdev_create_file(s
, &attr_smt_snooze_delay
);
260 sysdev_create_file(s
, &attr_mmcr0
);
261 sysdev_create_file(s
, &attr_mmcr1
);
263 if (cpu_has_feature(CPU_FTR_MMCRA
))
264 sysdev_create_file(s
, &attr_mmcra
);
266 sysdev_create_file(s
, &attr_pmc1
);
267 sysdev_create_file(s
, &attr_pmc2
);
268 sysdev_create_file(s
, &attr_pmc3
);
269 sysdev_create_file(s
, &attr_pmc4
);
270 sysdev_create_file(s
, &attr_pmc5
);
271 sysdev_create_file(s
, &attr_pmc6
);
273 if (cpu_has_feature(CPU_FTR_PMC8
)) {
274 sysdev_create_file(s
, &attr_pmc7
);
275 sysdev_create_file(s
, &attr_pmc8
);
278 if (cpu_has_feature(CPU_FTR_SMT
))
279 sysdev_create_file(s
, &attr_purr
);
282 #ifdef CONFIG_HOTPLUG_CPU
283 static void unregister_cpu_online(unsigned int cpu
)
285 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
286 struct sys_device
*s
= &c
->sysdev
;
288 BUG_ON(c
->no_control
);
290 #ifndef CONFIG_PPC_ISERIES
291 if (cpu_has_feature(CPU_FTR_SMT
))
292 sysdev_remove_file(s
, &attr_smt_snooze_delay
);
297 sysdev_remove_file(s
, &attr_mmcr0
);
298 sysdev_remove_file(s
, &attr_mmcr1
);
300 if (cpu_has_feature(CPU_FTR_MMCRA
))
301 sysdev_remove_file(s
, &attr_mmcra
);
303 sysdev_remove_file(s
, &attr_pmc1
);
304 sysdev_remove_file(s
, &attr_pmc2
);
305 sysdev_remove_file(s
, &attr_pmc3
);
306 sysdev_remove_file(s
, &attr_pmc4
);
307 sysdev_remove_file(s
, &attr_pmc5
);
308 sysdev_remove_file(s
, &attr_pmc6
);
310 if (cpu_has_feature(CPU_FTR_PMC8
)) {
311 sysdev_remove_file(s
, &attr_pmc7
);
312 sysdev_remove_file(s
, &attr_pmc8
);
315 if (cpu_has_feature(CPU_FTR_SMT
))
316 sysdev_remove_file(s
, &attr_purr
);
318 #endif /* CONFIG_HOTPLUG_CPU */
320 static int __devinit
sysfs_cpu_notify(struct notifier_block
*self
,
321 unsigned long action
, void *hcpu
)
323 unsigned int cpu
= (unsigned int)(long)hcpu
;
327 register_cpu_online(cpu
);
329 #ifdef CONFIG_HOTPLUG_CPU
331 unregister_cpu_online(cpu
);
338 static struct notifier_block __devinitdata sysfs_cpu_nb
= {
339 .notifier_call
= sysfs_cpu_notify
,
345 static struct node node_devices
[MAX_NUMNODES
];
347 static void register_nodes(void)
351 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
352 if (node_online(i
)) {
353 int p_node
= parent_node(i
);
354 struct node
*parent
= NULL
;
357 parent
= &node_devices
[p_node
];
359 register_node(&node_devices
[i
], i
, parent
);
364 static void register_nodes(void)
370 /* Only valid if CPU is present. */
371 static ssize_t
show_physical_id(struct sys_device
*dev
, char *buf
)
373 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
375 return sprintf(buf
, "%d\n", get_hard_smp_processor_id(cpu
->sysdev
.id
));
377 static SYSDEV_ATTR(physical_id
, 0444, show_physical_id
, NULL
);
379 static int __init
topology_init(void)
382 struct node
*parent
= NULL
;
386 register_cpu_notifier(&sysfs_cpu_nb
);
389 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
392 /* The node to which a cpu belongs can't be known
393 * until the cpu is made present.
396 if (cpu_present(cpu
))
397 parent
= &node_devices
[cpu_to_node(cpu
)];
400 * For now, we just see if the system supports making
401 * the RTAS calls for CPU hotplug. But, there may be a
402 * more comprehensive way to do this for an individual
403 * CPU. For instance, the boot cpu might never be valid
409 if (cpu_online(cpu
) || (c
->no_control
== 0)) {
410 register_cpu(c
, cpu
, parent
);
412 sysdev_create_file(&c
->sysdev
, &attr_physical_id
);
416 register_cpu_online(cpu
);
421 __initcall(topology_init
);