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
);
104 #endif /* CONFIG_PPC_MULTIPLATFORM */
107 * Enabling PMCs will slow partition context switch times so we only do
108 * it the first time we write to the PMCs.
111 static DEFINE_PER_CPU(char, pmcs_enabled
);
113 void ppc64_enable_pmcs(void)
115 /* Only need to enable them once */
116 if (__get_cpu_var(pmcs_enabled
))
119 __get_cpu_var(pmcs_enabled
) = 1;
121 if (ppc_md
.enable_pmcs
)
122 ppc_md
.enable_pmcs();
124 EXPORT_SYMBOL(ppc64_enable_pmcs
);
126 /* XXX convert to rusty's on_one_cpu */
127 static unsigned long run_on_cpu(unsigned long cpu
,
128 unsigned long (*func
)(unsigned long),
131 cpumask_t old_affinity
= current
->cpus_allowed
;
134 /* should return -EINVAL to userspace */
135 if (set_cpus_allowed(current
, cpumask_of_cpu(cpu
)))
140 set_cpus_allowed(current
, old_affinity
);
145 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
146 static unsigned long read_##NAME(unsigned long junk) \
148 return mfspr(ADDRESS); \
150 static unsigned long write_##NAME(unsigned long val) \
152 ppc64_enable_pmcs(); \
153 mtspr(ADDRESS, val); \
156 static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
158 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
159 unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
160 return sprintf(buf, "%lx\n", val); \
162 static ssize_t __attribute_used__ \
163 store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
165 struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
167 int ret = sscanf(buf, "%lx", &val); \
170 run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
174 SYSFS_PMCSETUP(mmcr0
, SPRN_MMCR0
);
175 SYSFS_PMCSETUP(mmcr1
, SPRN_MMCR1
);
176 SYSFS_PMCSETUP(mmcra
, SPRN_MMCRA
);
177 SYSFS_PMCSETUP(pmc1
, SPRN_PMC1
);
178 SYSFS_PMCSETUP(pmc2
, SPRN_PMC2
);
179 SYSFS_PMCSETUP(pmc3
, SPRN_PMC3
);
180 SYSFS_PMCSETUP(pmc4
, SPRN_PMC4
);
181 SYSFS_PMCSETUP(pmc5
, SPRN_PMC5
);
182 SYSFS_PMCSETUP(pmc6
, SPRN_PMC6
);
183 SYSFS_PMCSETUP(pmc7
, SPRN_PMC7
);
184 SYSFS_PMCSETUP(pmc8
, SPRN_PMC8
);
185 SYSFS_PMCSETUP(purr
, SPRN_PURR
);
187 static SYSDEV_ATTR(mmcr0
, 0600, show_mmcr0
, store_mmcr0
);
188 static SYSDEV_ATTR(mmcr1
, 0600, show_mmcr1
, store_mmcr1
);
189 static SYSDEV_ATTR(mmcra
, 0600, show_mmcra
, store_mmcra
);
190 static SYSDEV_ATTR(pmc1
, 0600, show_pmc1
, store_pmc1
);
191 static SYSDEV_ATTR(pmc2
, 0600, show_pmc2
, store_pmc2
);
192 static SYSDEV_ATTR(pmc3
, 0600, show_pmc3
, store_pmc3
);
193 static SYSDEV_ATTR(pmc4
, 0600, show_pmc4
, store_pmc4
);
194 static SYSDEV_ATTR(pmc5
, 0600, show_pmc5
, store_pmc5
);
195 static SYSDEV_ATTR(pmc6
, 0600, show_pmc6
, store_pmc6
);
196 static SYSDEV_ATTR(pmc7
, 0600, show_pmc7
, store_pmc7
);
197 static SYSDEV_ATTR(pmc8
, 0600, show_pmc8
, store_pmc8
);
198 static SYSDEV_ATTR(purr
, 0600, show_purr
, NULL
);
200 static void register_cpu_online(unsigned int cpu
)
202 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
203 struct sys_device
*s
= &c
->sysdev
;
205 #ifndef CONFIG_PPC_ISERIES
206 if (cpu_has_feature(CPU_FTR_SMT
))
207 sysdev_create_file(s
, &attr_smt_snooze_delay
);
212 sysdev_create_file(s
, &attr_mmcr0
);
213 sysdev_create_file(s
, &attr_mmcr1
);
215 if (cpu_has_feature(CPU_FTR_MMCRA
))
216 sysdev_create_file(s
, &attr_mmcra
);
218 sysdev_create_file(s
, &attr_pmc1
);
219 sysdev_create_file(s
, &attr_pmc2
);
220 sysdev_create_file(s
, &attr_pmc3
);
221 sysdev_create_file(s
, &attr_pmc4
);
222 sysdev_create_file(s
, &attr_pmc5
);
223 sysdev_create_file(s
, &attr_pmc6
);
225 if (cpu_has_feature(CPU_FTR_PMC8
)) {
226 sysdev_create_file(s
, &attr_pmc7
);
227 sysdev_create_file(s
, &attr_pmc8
);
230 if (cpu_has_feature(CPU_FTR_SMT
))
231 sysdev_create_file(s
, &attr_purr
);
234 #ifdef CONFIG_HOTPLUG_CPU
235 static void unregister_cpu_online(unsigned int cpu
)
237 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
238 struct sys_device
*s
= &c
->sysdev
;
240 BUG_ON(c
->no_control
);
242 #ifndef CONFIG_PPC_ISERIES
243 if (cpu_has_feature(CPU_FTR_SMT
))
244 sysdev_remove_file(s
, &attr_smt_snooze_delay
);
249 sysdev_remove_file(s
, &attr_mmcr0
);
250 sysdev_remove_file(s
, &attr_mmcr1
);
252 if (cpu_has_feature(CPU_FTR_MMCRA
))
253 sysdev_remove_file(s
, &attr_mmcra
);
255 sysdev_remove_file(s
, &attr_pmc1
);
256 sysdev_remove_file(s
, &attr_pmc2
);
257 sysdev_remove_file(s
, &attr_pmc3
);
258 sysdev_remove_file(s
, &attr_pmc4
);
259 sysdev_remove_file(s
, &attr_pmc5
);
260 sysdev_remove_file(s
, &attr_pmc6
);
262 if (cpu_has_feature(CPU_FTR_PMC8
)) {
263 sysdev_remove_file(s
, &attr_pmc7
);
264 sysdev_remove_file(s
, &attr_pmc8
);
267 if (cpu_has_feature(CPU_FTR_SMT
))
268 sysdev_remove_file(s
, &attr_purr
);
270 #endif /* CONFIG_HOTPLUG_CPU */
272 static int __devinit
sysfs_cpu_notify(struct notifier_block
*self
,
273 unsigned long action
, void *hcpu
)
275 unsigned int cpu
= (unsigned int)(long)hcpu
;
279 register_cpu_online(cpu
);
281 #ifdef CONFIG_HOTPLUG_CPU
283 unregister_cpu_online(cpu
);
290 static struct notifier_block __devinitdata sysfs_cpu_nb
= {
291 .notifier_call
= sysfs_cpu_notify
,
297 static struct node node_devices
[MAX_NUMNODES
];
299 static void register_nodes(void)
303 for (i
= 0; i
< MAX_NUMNODES
; i
++) {
304 if (node_online(i
)) {
305 int p_node
= parent_node(i
);
306 struct node
*parent
= NULL
;
309 parent
= &node_devices
[p_node
];
311 register_node(&node_devices
[i
], i
, parent
);
316 static void register_nodes(void)
322 /* Only valid if CPU is present. */
323 static ssize_t
show_physical_id(struct sys_device
*dev
, char *buf
)
325 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
327 return sprintf(buf
, "%d\n", get_hard_smp_processor_id(cpu
->sysdev
.id
));
329 static SYSDEV_ATTR(physical_id
, 0444, show_physical_id
, NULL
);
331 static int __init
topology_init(void)
334 struct node
*parent
= NULL
;
338 register_cpu_notifier(&sysfs_cpu_nb
);
341 struct cpu
*c
= &per_cpu(cpu_devices
, cpu
);
344 /* The node to which a cpu belongs can't be known
345 * until the cpu is made present.
348 if (cpu_present(cpu
))
349 parent
= &node_devices
[cpu_to_node(cpu
)];
352 * For now, we just see if the system supports making
353 * the RTAS calls for CPU hotplug. But, there may be a
354 * more comprehensive way to do this for an individual
355 * CPU. For instance, the boot cpu might never be valid
361 if (cpu_online(cpu
) || (c
->no_control
== 0)) {
362 register_cpu(c
, cpu
, parent
);
364 sysdev_create_file(&c
->sysdev
, &attr_physical_id
);
368 register_cpu_online(cpu
);
373 __initcall(topology_init
);