Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #include <linux/sysdev.h> |
2 | #include <linux/cpu.h> | |
3 | #include <linux/smp.h> | |
4 | #include <linux/percpu.h> | |
5 | #include <linux/init.h> | |
6 | #include <linux/sched.h> | |
7 | #include <linux/module.h> | |
8 | #include <linux/nodemask.h> | |
9 | #include <linux/cpumask.h> | |
10 | #include <linux/notifier.h> | |
11 | ||
12 | #include <asm/current.h> | |
13 | #include <asm/processor.h> | |
14 | #include <asm/cputable.h> | |
1ababe11 | 15 | #include <asm/firmware.h> |
1da177e4 LT |
16 | #include <asm/hvcall.h> |
17 | #include <asm/prom.h> | |
1da177e4 LT |
18 | #include <asm/paca.h> |
19 | #include <asm/lppaca.h> | |
20 | #include <asm/machdep.h> | |
2249ca9d | 21 | #include <asm/smp.h> |
1da177e4 LT |
22 | |
23 | static DEFINE_PER_CPU(struct cpu, cpu_devices); | |
24 | ||
124c27d3 NL |
25 | static DEFINE_PER_CPU(struct kobject *, cache_toplevel); |
26 | ||
1da177e4 LT |
27 | /* SMT stuff */ |
28 | ||
29 | #ifdef CONFIG_PPC_MULTIPLATFORM | |
0ddd3e7d AB |
30 | /* Time in microseconds we delay before sleeping in the idle loop */ |
31 | DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 }; | |
1da177e4 | 32 | |
4a0b2b4d AK |
33 | static ssize_t store_smt_snooze_delay(struct sys_device *dev, |
34 | struct sysdev_attribute *attr, | |
35 | const char *buf, | |
1da177e4 LT |
36 | size_t count) |
37 | { | |
38 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); | |
39 | ssize_t ret; | |
40 | unsigned long snooze; | |
41 | ||
42 | ret = sscanf(buf, "%lu", &snooze); | |
43 | if (ret != 1) | |
44 | return -EINVAL; | |
45 | ||
46 | per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze; | |
47 | ||
48 | return count; | |
49 | } | |
50 | ||
4a0b2b4d AK |
51 | static ssize_t show_smt_snooze_delay(struct sys_device *dev, |
52 | struct sysdev_attribute *attr, | |
53 | char *buf) | |
1da177e4 LT |
54 | { |
55 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); | |
56 | ||
57 | return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id)); | |
58 | } | |
59 | ||
60 | static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay, | |
61 | store_smt_snooze_delay); | |
62 | ||
63 | /* Only parse OF options if the matching cmdline option was not specified */ | |
64 | static int smt_snooze_cmdline; | |
65 | ||
66 | static int __init smt_setup(void) | |
67 | { | |
68 | struct device_node *options; | |
a7f67bdf | 69 | const unsigned int *val; |
1da177e4 LT |
70 | unsigned int cpu; |
71 | ||
72 | if (!cpu_has_feature(CPU_FTR_SMT)) | |
69ed3324 | 73 | return -ENODEV; |
1da177e4 | 74 | |
8c8dc322 | 75 | options = of_find_node_by_path("/options"); |
1da177e4 | 76 | if (!options) |
69ed3324 | 77 | return -ENODEV; |
1da177e4 | 78 | |
e2eb6392 | 79 | val = of_get_property(options, "ibm,smt-snooze-delay", NULL); |
1da177e4 | 80 | if (!smt_snooze_cmdline && val) { |
0e551954 | 81 | for_each_possible_cpu(cpu) |
1da177e4 LT |
82 | per_cpu(smt_snooze_delay, cpu) = *val; |
83 | } | |
84 | ||
8c8dc322 | 85 | of_node_put(options); |
69ed3324 | 86 | return 0; |
1da177e4 LT |
87 | } |
88 | __initcall(smt_setup); | |
89 | ||
90 | static int __init setup_smt_snooze_delay(char *str) | |
91 | { | |
92 | unsigned int cpu; | |
93 | int snooze; | |
94 | ||
95 | if (!cpu_has_feature(CPU_FTR_SMT)) | |
96 | return 1; | |
97 | ||
98 | smt_snooze_cmdline = 1; | |
99 | ||
100 | if (get_option(&str, &snooze)) { | |
0e551954 | 101 | for_each_possible_cpu(cpu) |
1da177e4 LT |
102 | per_cpu(smt_snooze_delay, cpu) = snooze; |
103 | } | |
104 | ||
105 | return 1; | |
106 | } | |
107 | __setup("smt-snooze-delay=", setup_smt_snooze_delay); | |
108 | ||
180a3362 ME |
109 | #endif /* CONFIG_PPC_MULTIPLATFORM */ |
110 | ||
1da177e4 LT |
111 | /* |
112 | * Enabling PMCs will slow partition context switch times so we only do | |
113 | * it the first time we write to the PMCs. | |
114 | */ | |
115 | ||
116 | static DEFINE_PER_CPU(char, pmcs_enabled); | |
117 | ||
118 | void ppc64_enable_pmcs(void) | |
119 | { | |
1da177e4 LT |
120 | /* Only need to enable them once */ |
121 | if (__get_cpu_var(pmcs_enabled)) | |
122 | return; | |
123 | ||
124 | __get_cpu_var(pmcs_enabled) = 1; | |
125 | ||
180a3362 ME |
126 | if (ppc_md.enable_pmcs) |
127 | ppc_md.enable_pmcs(); | |
1da177e4 | 128 | } |
1da177e4 LT |
129 | EXPORT_SYMBOL(ppc64_enable_pmcs); |
130 | ||
131 | /* XXX convert to rusty's on_one_cpu */ | |
132 | static unsigned long run_on_cpu(unsigned long cpu, | |
133 | unsigned long (*func)(unsigned long), | |
134 | unsigned long arg) | |
135 | { | |
136 | cpumask_t old_affinity = current->cpus_allowed; | |
137 | unsigned long ret; | |
138 | ||
139 | /* should return -EINVAL to userspace */ | |
140 | if (set_cpus_allowed(current, cpumask_of_cpu(cpu))) | |
141 | return 0; | |
142 | ||
143 | ret = func(arg); | |
144 | ||
145 | set_cpus_allowed(current, old_affinity); | |
146 | ||
147 | return ret; | |
148 | } | |
149 | ||
150 | #define SYSFS_PMCSETUP(NAME, ADDRESS) \ | |
151 | static unsigned long read_##NAME(unsigned long junk) \ | |
152 | { \ | |
153 | return mfspr(ADDRESS); \ | |
154 | } \ | |
155 | static unsigned long write_##NAME(unsigned long val) \ | |
156 | { \ | |
157 | ppc64_enable_pmcs(); \ | |
158 | mtspr(ADDRESS, val); \ | |
159 | return 0; \ | |
160 | } \ | |
4a0b2b4d AK |
161 | static ssize_t show_##NAME(struct sys_device *dev, \ |
162 | struct sysdev_attribute *attr, \ | |
163 | char *buf) \ | |
1da177e4 LT |
164 | { \ |
165 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); \ | |
166 | unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \ | |
167 | return sprintf(buf, "%lx\n", val); \ | |
168 | } \ | |
3ff6eecc | 169 | static ssize_t __used \ |
4a0b2b4d AK |
170 | store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \ |
171 | const char *buf, size_t count) \ | |
1da177e4 LT |
172 | { \ |
173 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); \ | |
174 | unsigned long val; \ | |
175 | int ret = sscanf(buf, "%lx", &val); \ | |
176 | if (ret != 1) \ | |
177 | return -EINVAL; \ | |
178 | run_on_cpu(cpu->sysdev.id, write_##NAME, val); \ | |
179 | return count; \ | |
180 | } | |
181 | ||
6529c13d OJ |
182 | |
183 | /* Let's define all possible registers, we'll only hook up the ones | |
184 | * that are implemented on the current processor | |
185 | */ | |
186 | ||
1da177e4 LT |
187 | SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0); |
188 | SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1); | |
189 | SYSFS_PMCSETUP(mmcra, SPRN_MMCRA); | |
190 | SYSFS_PMCSETUP(pmc1, SPRN_PMC1); | |
191 | SYSFS_PMCSETUP(pmc2, SPRN_PMC2); | |
192 | SYSFS_PMCSETUP(pmc3, SPRN_PMC3); | |
193 | SYSFS_PMCSETUP(pmc4, SPRN_PMC4); | |
194 | SYSFS_PMCSETUP(pmc5, SPRN_PMC5); | |
195 | SYSFS_PMCSETUP(pmc6, SPRN_PMC6); | |
196 | SYSFS_PMCSETUP(pmc7, SPRN_PMC7); | |
197 | SYSFS_PMCSETUP(pmc8, SPRN_PMC8); | |
198 | SYSFS_PMCSETUP(purr, SPRN_PURR); | |
f050982a | 199 | SYSFS_PMCSETUP(spurr, SPRN_SPURR); |
4c198557 | 200 | SYSFS_PMCSETUP(dscr, SPRN_DSCR); |
1da177e4 | 201 | |
25fc530e OJ |
202 | SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0); |
203 | SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1); | |
204 | SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2); | |
205 | SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3); | |
206 | SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4); | |
207 | SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5); | |
6529c13d | 208 | |
2e1957fd OJ |
209 | #ifdef CONFIG_DEBUG_KERNEL |
210 | SYSFS_PMCSETUP(hid0, SPRN_HID0); | |
211 | SYSFS_PMCSETUP(hid1, SPRN_HID1); | |
212 | SYSFS_PMCSETUP(hid4, SPRN_HID4); | |
213 | SYSFS_PMCSETUP(hid5, SPRN_HID5); | |
214 | SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0); | |
215 | SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1); | |
216 | SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2); | |
217 | SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3); | |
218 | SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4); | |
219 | SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5); | |
220 | SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6); | |
221 | SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7); | |
222 | SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8); | |
223 | SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9); | |
224 | SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT); | |
225 | SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR); | |
226 | SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR); | |
227 | SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR); | |
228 | SYSFS_PMCSETUP(der, SPRN_PA6T_DER); | |
229 | SYSFS_PMCSETUP(mer, SPRN_PA6T_MER); | |
230 | SYSFS_PMCSETUP(ber, SPRN_PA6T_BER); | |
231 | SYSFS_PMCSETUP(ier, SPRN_PA6T_IER); | |
232 | SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER); | |
233 | SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR); | |
234 | SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0); | |
235 | SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1); | |
236 | SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2); | |
237 | SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3); | |
238 | #endif /* CONFIG_DEBUG_KERNEL */ | |
6529c13d | 239 | |
1da177e4 | 240 | static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra); |
f050982a | 241 | static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL); |
4c198557 | 242 | static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr); |
6529c13d OJ |
243 | static SYSDEV_ATTR(purr, 0600, show_purr, store_purr); |
244 | ||
245 | static struct sysdev_attribute ibm_common_attrs[] = { | |
246 | _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0), | |
247 | _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1), | |
248 | }; | |
249 | ||
250 | static struct sysdev_attribute ibm_pmc_attrs[] = { | |
251 | _SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1), | |
252 | _SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2), | |
253 | _SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3), | |
254 | _SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4), | |
255 | _SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5), | |
256 | _SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6), | |
257 | _SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7), | |
258 | _SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8), | |
259 | }; | |
260 | ||
261 | static struct sysdev_attribute pa6t_attrs[] = { | |
262 | _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0), | |
263 | _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1), | |
264 | _SYSDEV_ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0), | |
265 | _SYSDEV_ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1), | |
266 | _SYSDEV_ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2), | |
267 | _SYSDEV_ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3), | |
268 | _SYSDEV_ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4), | |
269 | _SYSDEV_ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5), | |
2e1957fd OJ |
270 | #ifdef CONFIG_DEBUG_KERNEL |
271 | _SYSDEV_ATTR(hid0, 0600, show_hid0, store_hid0), | |
272 | _SYSDEV_ATTR(hid1, 0600, show_hid1, store_hid1), | |
273 | _SYSDEV_ATTR(hid4, 0600, show_hid4, store_hid4), | |
274 | _SYSDEV_ATTR(hid5, 0600, show_hid5, store_hid5), | |
275 | _SYSDEV_ATTR(ima0, 0600, show_ima0, store_ima0), | |
276 | _SYSDEV_ATTR(ima1, 0600, show_ima1, store_ima1), | |
277 | _SYSDEV_ATTR(ima2, 0600, show_ima2, store_ima2), | |
278 | _SYSDEV_ATTR(ima3, 0600, show_ima3, store_ima3), | |
279 | _SYSDEV_ATTR(ima4, 0600, show_ima4, store_ima4), | |
280 | _SYSDEV_ATTR(ima5, 0600, show_ima5, store_ima5), | |
281 | _SYSDEV_ATTR(ima6, 0600, show_ima6, store_ima6), | |
282 | _SYSDEV_ATTR(ima7, 0600, show_ima7, store_ima7), | |
283 | _SYSDEV_ATTR(ima8, 0600, show_ima8, store_ima8), | |
284 | _SYSDEV_ATTR(ima9, 0600, show_ima9, store_ima9), | |
285 | _SYSDEV_ATTR(imaat, 0600, show_imaat, store_imaat), | |
286 | _SYSDEV_ATTR(btcr, 0600, show_btcr, store_btcr), | |
287 | _SYSDEV_ATTR(pccr, 0600, show_pccr, store_pccr), | |
288 | _SYSDEV_ATTR(rpccr, 0600, show_rpccr, store_rpccr), | |
289 | _SYSDEV_ATTR(der, 0600, show_der, store_der), | |
290 | _SYSDEV_ATTR(mer, 0600, show_mer, store_mer), | |
291 | _SYSDEV_ATTR(ber, 0600, show_ber, store_ber), | |
292 | _SYSDEV_ATTR(ier, 0600, show_ier, store_ier), | |
293 | _SYSDEV_ATTR(sier, 0600, show_sier, store_sier), | |
294 | _SYSDEV_ATTR(siar, 0600, show_siar, store_siar), | |
295 | _SYSDEV_ATTR(tsr0, 0600, show_tsr0, store_tsr0), | |
296 | _SYSDEV_ATTR(tsr1, 0600, show_tsr1, store_tsr1), | |
297 | _SYSDEV_ATTR(tsr2, 0600, show_tsr2, store_tsr2), | |
298 | _SYSDEV_ATTR(tsr3, 0600, show_tsr3, store_tsr3), | |
299 | #endif /* CONFIG_DEBUG_KERNEL */ | |
6529c13d OJ |
300 | }; |
301 | ||
124c27d3 NL |
302 | struct cache_desc { |
303 | struct kobject kobj; | |
304 | struct cache_desc *next; | |
305 | const char *type; /* Instruction, Data, or Unified */ | |
306 | u32 size; /* total cache size in KB */ | |
307 | u32 line_size; /* in bytes */ | |
308 | u32 nr_sets; /* number of sets */ | |
309 | u32 level; /* e.g. 1, 2, 3... */ | |
310 | u32 associativity; /* e.g. 8-way... 0 is fully associative */ | |
311 | }; | |
312 | ||
313 | DEFINE_PER_CPU(struct cache_desc *, cache_desc); | |
314 | ||
315 | static struct cache_desc *kobj_to_cache_desc(struct kobject *k) | |
316 | { | |
317 | return container_of(k, struct cache_desc, kobj); | |
318 | } | |
319 | ||
320 | static void cache_desc_release(struct kobject *k) | |
321 | { | |
322 | struct cache_desc *desc = kobj_to_cache_desc(k); | |
323 | ||
324 | pr_debug("%s: releasing %s\n", __func__, kobject_name(k)); | |
325 | ||
326 | if (desc->next) | |
327 | kobject_put(&desc->next->kobj); | |
328 | ||
329 | kfree(kobj_to_cache_desc(k)); | |
330 | } | |
331 | ||
332 | static ssize_t cache_desc_show(struct kobject *k, struct attribute *attr, char *buf) | |
333 | { | |
334 | struct kobj_attribute *kobj_attr; | |
335 | ||
336 | kobj_attr = container_of(attr, struct kobj_attribute, attr); | |
337 | ||
338 | return kobj_attr->show(k, kobj_attr, buf); | |
339 | } | |
340 | ||
341 | static struct sysfs_ops cache_desc_sysfs_ops = { | |
342 | .show = cache_desc_show, | |
343 | }; | |
344 | ||
345 | static struct kobj_type cache_desc_type = { | |
346 | .release = cache_desc_release, | |
347 | .sysfs_ops = &cache_desc_sysfs_ops, | |
348 | }; | |
349 | ||
350 | static ssize_t cache_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) | |
351 | { | |
352 | struct cache_desc *cache = kobj_to_cache_desc(k); | |
353 | ||
354 | return sprintf(buf, "%uK\n", cache->size); | |
355 | } | |
356 | ||
357 | static struct kobj_attribute cache_size_attr = | |
358 | __ATTR(size, 0444, cache_size_show, NULL); | |
359 | ||
360 | static ssize_t cache_line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) | |
361 | { | |
362 | struct cache_desc *cache = kobj_to_cache_desc(k); | |
363 | ||
364 | return sprintf(buf, "%u\n", cache->line_size); | |
365 | } | |
366 | ||
367 | static struct kobj_attribute cache_line_size_attr = | |
368 | __ATTR(coherency_line_size, 0444, cache_line_size_show, NULL); | |
369 | ||
370 | static ssize_t cache_nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf) | |
371 | { | |
372 | struct cache_desc *cache = kobj_to_cache_desc(k); | |
373 | ||
374 | return sprintf(buf, "%u\n", cache->nr_sets); | |
375 | } | |
376 | ||
377 | static struct kobj_attribute cache_nr_sets_attr = | |
378 | __ATTR(number_of_sets, 0444, cache_nr_sets_show, NULL); | |
379 | ||
380 | static ssize_t cache_type_show(struct kobject *k, struct kobj_attribute *attr, char *buf) | |
381 | { | |
382 | struct cache_desc *cache = kobj_to_cache_desc(k); | |
383 | ||
384 | return sprintf(buf, "%s\n", cache->type); | |
385 | } | |
386 | ||
387 | static struct kobj_attribute cache_type_attr = | |
388 | __ATTR(type, 0444, cache_type_show, NULL); | |
389 | ||
390 | static ssize_t cache_level_show(struct kobject *k, struct kobj_attribute *attr, char *buf) | |
391 | { | |
392 | struct cache_desc *cache = kobj_to_cache_desc(k); | |
393 | ||
394 | return sprintf(buf, "%u\n", cache->level); | |
395 | } | |
396 | ||
397 | static struct kobj_attribute cache_level_attr = | |
398 | __ATTR(level, 0444, cache_level_show, NULL); | |
399 | ||
400 | static ssize_t cache_assoc_show(struct kobject *k, struct kobj_attribute *attr, char *buf) | |
401 | { | |
402 | struct cache_desc *cache = kobj_to_cache_desc(k); | |
403 | ||
404 | return sprintf(buf, "%u\n", cache->associativity); | |
405 | } | |
406 | ||
407 | static struct kobj_attribute cache_assoc_attr = | |
408 | __ATTR(ways_of_associativity, 0444, cache_assoc_show, NULL); | |
409 | ||
410 | struct cache_desc_info { | |
411 | const char *type; | |
412 | const char *size_prop; | |
413 | const char *line_size_prop; | |
414 | const char *nr_sets_prop; | |
415 | }; | |
416 | ||
417 | /* PowerPC Processor binding says the [di]-cache-* must be equal on | |
418 | * unified caches, so just use d-cache properties. */ | |
419 | static struct cache_desc_info ucache_info = { | |
420 | .type = "Unified", | |
421 | .size_prop = "d-cache-size", | |
422 | .line_size_prop = "d-cache-line-size", | |
423 | .nr_sets_prop = "d-cache-sets", | |
424 | }; | |
425 | ||
426 | static struct cache_desc_info dcache_info = { | |
427 | .type = "Data", | |
428 | .size_prop = "d-cache-size", | |
429 | .line_size_prop = "d-cache-line-size", | |
430 | .nr_sets_prop = "d-cache-sets", | |
431 | }; | |
432 | ||
433 | static struct cache_desc_info icache_info = { | |
434 | .type = "Instruction", | |
435 | .size_prop = "i-cache-size", | |
436 | .line_size_prop = "i-cache-line-size", | |
437 | .nr_sets_prop = "i-cache-sets", | |
438 | }; | |
439 | ||
440 | static struct cache_desc * __cpuinit create_cache_desc(struct device_node *np, struct kobject *parent, int index, int level, struct cache_desc_info *info) | |
441 | { | |
442 | const u32 *cache_line_size; | |
443 | struct cache_desc *new; | |
444 | const u32 *cache_size; | |
445 | const u32 *nr_sets; | |
446 | int rc; | |
447 | ||
448 | new = kzalloc(sizeof(*new), GFP_KERNEL); | |
449 | if (!new) | |
450 | return NULL; | |
451 | ||
452 | rc = kobject_init_and_add(&new->kobj, &cache_desc_type, parent, | |
453 | "index%d", index); | |
454 | if (rc) | |
455 | goto err; | |
456 | ||
457 | /* type */ | |
458 | new->type = info->type; | |
459 | rc = sysfs_create_file(&new->kobj, &cache_type_attr.attr); | |
460 | WARN_ON(rc); | |
461 | ||
462 | /* level */ | |
463 | new->level = level; | |
464 | rc = sysfs_create_file(&new->kobj, &cache_level_attr.attr); | |
465 | WARN_ON(rc); | |
466 | ||
467 | /* size */ | |
468 | cache_size = of_get_property(np, info->size_prop, NULL); | |
469 | if (cache_size) { | |
470 | new->size = *cache_size / 1024; | |
471 | rc = sysfs_create_file(&new->kobj, | |
472 | &cache_size_attr.attr); | |
473 | WARN_ON(rc); | |
474 | } | |
475 | ||
476 | /* coherency_line_size */ | |
477 | cache_line_size = of_get_property(np, info->line_size_prop, NULL); | |
478 | if (cache_line_size) { | |
479 | new->line_size = *cache_line_size; | |
480 | rc = sysfs_create_file(&new->kobj, | |
481 | &cache_line_size_attr.attr); | |
482 | WARN_ON(rc); | |
483 | } | |
484 | ||
485 | /* number_of_sets */ | |
486 | nr_sets = of_get_property(np, info->nr_sets_prop, NULL); | |
487 | if (nr_sets) { | |
488 | new->nr_sets = *nr_sets; | |
489 | rc = sysfs_create_file(&new->kobj, | |
490 | &cache_nr_sets_attr.attr); | |
491 | WARN_ON(rc); | |
492 | } | |
493 | ||
494 | /* ways_of_associativity */ | |
495 | if (new->nr_sets == 1) { | |
496 | /* fully associative */ | |
497 | new->associativity = 0; | |
498 | goto create_assoc; | |
499 | } | |
500 | ||
501 | if (new->nr_sets && new->size && new->line_size) { | |
502 | /* If we have values for all of these we can derive | |
503 | * the associativity. */ | |
504 | new->associativity = | |
505 | ((new->size * 1024) / new->nr_sets) / new->line_size; | |
506 | create_assoc: | |
507 | rc = sysfs_create_file(&new->kobj, | |
508 | &cache_assoc_attr.attr); | |
509 | WARN_ON(rc); | |
510 | } | |
511 | ||
512 | return new; | |
513 | err: | |
514 | kfree(new); | |
515 | return NULL; | |
516 | } | |
517 | ||
518 | static bool cache_is_unified(struct device_node *np) | |
519 | { | |
520 | return of_get_property(np, "cache-unified", NULL); | |
521 | } | |
522 | ||
523 | static struct cache_desc * __cpuinit create_cache_index_info(struct device_node *np, struct kobject *parent, int index, int level) | |
524 | { | |
525 | const phandle *next_cache_phandle; | |
526 | struct device_node *next_cache; | |
527 | struct cache_desc *new, **end; | |
528 | ||
529 | pr_debug("%s(node = %s, index = %d)\n", __func__, np->full_name, index); | |
530 | ||
531 | if (cache_is_unified(np)) { | |
532 | new = create_cache_desc(np, parent, index, level, | |
533 | &ucache_info); | |
534 | } else { | |
535 | new = create_cache_desc(np, parent, index, level, | |
536 | &dcache_info); | |
537 | if (new) { | |
538 | index++; | |
539 | new->next = create_cache_desc(np, parent, index, level, | |
540 | &icache_info); | |
541 | } | |
542 | } | |
543 | if (!new) | |
544 | return NULL; | |
545 | ||
546 | end = &new->next; | |
547 | while (*end) | |
548 | end = &(*end)->next; | |
549 | ||
550 | next_cache_phandle = of_get_property(np, "l2-cache", NULL); | |
551 | if (!next_cache_phandle) | |
552 | goto out; | |
553 | ||
554 | next_cache = of_find_node_by_phandle(*next_cache_phandle); | |
555 | if (!next_cache) | |
556 | goto out; | |
557 | ||
558 | *end = create_cache_index_info(next_cache, parent, ++index, ++level); | |
559 | ||
560 | of_node_put(next_cache); | |
561 | out: | |
562 | return new; | |
563 | } | |
564 | ||
565 | static void __cpuinit create_cache_info(struct sys_device *sysdev) | |
566 | { | |
567 | struct kobject *cache_toplevel; | |
568 | struct device_node *np = NULL; | |
569 | int cpu = sysdev->id; | |
570 | ||
571 | cache_toplevel = kobject_create_and_add("cache", &sysdev->kobj); | |
572 | if (!cache_toplevel) | |
573 | return; | |
574 | per_cpu(cache_toplevel, cpu) = cache_toplevel; | |
575 | np = of_get_cpu_node(cpu, NULL); | |
576 | if (np != NULL) { | |
577 | per_cpu(cache_desc, cpu) = | |
578 | create_cache_index_info(np, cache_toplevel, 0, 1); | |
579 | of_node_put(np); | |
580 | } | |
581 | return; | |
582 | } | |
1da177e4 | 583 | |
9ba1984e | 584 | static void __cpuinit register_cpu_online(unsigned int cpu) |
1da177e4 LT |
585 | { |
586 | struct cpu *c = &per_cpu(cpu_devices, cpu); | |
587 | struct sys_device *s = &c->sysdev; | |
6529c13d OJ |
588 | struct sysdev_attribute *attrs, *pmc_attrs; |
589 | int i, nattrs; | |
1da177e4 | 590 | |
ad5cb17f SR |
591 | if (!firmware_has_feature(FW_FEATURE_ISERIES) && |
592 | cpu_has_feature(CPU_FTR_SMT)) | |
1da177e4 | 593 | sysdev_create_file(s, &attr_smt_snooze_delay); |
1da177e4 LT |
594 | |
595 | /* PMC stuff */ | |
6529c13d OJ |
596 | switch (cur_cpu_spec->pmc_type) { |
597 | case PPC_PMC_IBM: | |
598 | attrs = ibm_common_attrs; | |
599 | nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute); | |
600 | pmc_attrs = ibm_pmc_attrs; | |
601 | break; | |
602 | case PPC_PMC_PA6T: | |
603 | /* PA Semi starts counting at PMC0 */ | |
604 | attrs = pa6t_attrs; | |
605 | nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute); | |
606 | pmc_attrs = NULL; | |
607 | break; | |
608 | default: | |
609 | attrs = NULL; | |
610 | nattrs = 0; | |
611 | pmc_attrs = NULL; | |
612 | } | |
613 | ||
614 | for (i = 0; i < nattrs; i++) | |
615 | sysdev_create_file(s, &attrs[i]); | |
1da177e4 | 616 | |
6529c13d OJ |
617 | if (pmc_attrs) |
618 | for (i = 0; i < cur_cpu_spec->num_pmcs; i++) | |
619 | sysdev_create_file(s, &pmc_attrs[i]); | |
1da177e4 LT |
620 | |
621 | if (cpu_has_feature(CPU_FTR_MMCRA)) | |
622 | sysdev_create_file(s, &attr_mmcra); | |
623 | ||
afd05423 | 624 | if (cpu_has_feature(CPU_FTR_PURR)) |
1da177e4 | 625 | sysdev_create_file(s, &attr_purr); |
4c198557 | 626 | |
f050982a AB |
627 | if (cpu_has_feature(CPU_FTR_SPURR)) |
628 | sysdev_create_file(s, &attr_spurr); | |
629 | ||
4c198557 AB |
630 | if (cpu_has_feature(CPU_FTR_DSCR)) |
631 | sysdev_create_file(s, &attr_dscr); | |
124c27d3 NL |
632 | |
633 | create_cache_info(s); | |
1da177e4 LT |
634 | } |
635 | ||
636 | #ifdef CONFIG_HOTPLUG_CPU | |
124c27d3 NL |
637 | static void remove_cache_info(struct sys_device *sysdev) |
638 | { | |
639 | struct kobject *cache_toplevel; | |
640 | struct cache_desc *cache_desc; | |
641 | int cpu = sysdev->id; | |
642 | ||
643 | cache_desc = per_cpu(cache_desc, cpu); | |
f3d3d307 | 644 | if (cache_desc != NULL) |
124c27d3 | 645 | kobject_put(&cache_desc->kobj); |
f3d3d307 | 646 | |
124c27d3 NL |
647 | cache_toplevel = per_cpu(cache_toplevel, cpu); |
648 | if (cache_toplevel != NULL) | |
649 | kobject_put(cache_toplevel); | |
650 | } | |
651 | ||
1da177e4 LT |
652 | static void unregister_cpu_online(unsigned int cpu) |
653 | { | |
654 | struct cpu *c = &per_cpu(cpu_devices, cpu); | |
655 | struct sys_device *s = &c->sysdev; | |
6529c13d OJ |
656 | struct sysdev_attribute *attrs, *pmc_attrs; |
657 | int i, nattrs; | |
1da177e4 | 658 | |
72486f1f | 659 | BUG_ON(!c->hotpluggable); |
1da177e4 | 660 | |
ad5cb17f SR |
661 | if (!firmware_has_feature(FW_FEATURE_ISERIES) && |
662 | cpu_has_feature(CPU_FTR_SMT)) | |
1da177e4 | 663 | sysdev_remove_file(s, &attr_smt_snooze_delay); |
1da177e4 LT |
664 | |
665 | /* PMC stuff */ | |
6529c13d OJ |
666 | switch (cur_cpu_spec->pmc_type) { |
667 | case PPC_PMC_IBM: | |
668 | attrs = ibm_common_attrs; | |
669 | nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute); | |
670 | pmc_attrs = ibm_pmc_attrs; | |
671 | break; | |
672 | case PPC_PMC_PA6T: | |
673 | /* PA Semi starts counting at PMC0 */ | |
674 | attrs = pa6t_attrs; | |
675 | nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute); | |
676 | pmc_attrs = NULL; | |
677 | break; | |
678 | default: | |
679 | attrs = NULL; | |
680 | nattrs = 0; | |
681 | pmc_attrs = NULL; | |
682 | } | |
1da177e4 | 683 | |
6529c13d OJ |
684 | for (i = 0; i < nattrs; i++) |
685 | sysdev_remove_file(s, &attrs[i]); | |
686 | ||
687 | if (pmc_attrs) | |
688 | for (i = 0; i < cur_cpu_spec->num_pmcs; i++) | |
689 | sysdev_remove_file(s, &pmc_attrs[i]); | |
1da177e4 LT |
690 | |
691 | if (cpu_has_feature(CPU_FTR_MMCRA)) | |
692 | sysdev_remove_file(s, &attr_mmcra); | |
693 | ||
afd05423 | 694 | if (cpu_has_feature(CPU_FTR_PURR)) |
1da177e4 | 695 | sysdev_remove_file(s, &attr_purr); |
4c198557 | 696 | |
f050982a AB |
697 | if (cpu_has_feature(CPU_FTR_SPURR)) |
698 | sysdev_remove_file(s, &attr_spurr); | |
699 | ||
4c198557 AB |
700 | if (cpu_has_feature(CPU_FTR_DSCR)) |
701 | sysdev_remove_file(s, &attr_dscr); | |
124c27d3 NL |
702 | |
703 | remove_cache_info(s); | |
1da177e4 LT |
704 | } |
705 | #endif /* CONFIG_HOTPLUG_CPU */ | |
706 | ||
8c78f307 | 707 | static int __cpuinit sysfs_cpu_notify(struct notifier_block *self, |
1da177e4 LT |
708 | unsigned long action, void *hcpu) |
709 | { | |
710 | unsigned int cpu = (unsigned int)(long)hcpu; | |
711 | ||
712 | switch (action) { | |
713 | case CPU_ONLINE: | |
8bb78442 | 714 | case CPU_ONLINE_FROZEN: |
1da177e4 LT |
715 | register_cpu_online(cpu); |
716 | break; | |
717 | #ifdef CONFIG_HOTPLUG_CPU | |
718 | case CPU_DEAD: | |
8bb78442 | 719 | case CPU_DEAD_FROZEN: |
1da177e4 LT |
720 | unregister_cpu_online(cpu); |
721 | break; | |
722 | #endif | |
723 | } | |
724 | return NOTIFY_OK; | |
725 | } | |
726 | ||
8c78f307 | 727 | static struct notifier_block __cpuinitdata sysfs_cpu_nb = { |
1da177e4 LT |
728 | .notifier_call = sysfs_cpu_notify, |
729 | }; | |
730 | ||
0344c6c5 CK |
731 | static DEFINE_MUTEX(cpu_mutex); |
732 | ||
733 | int cpu_add_sysdev_attr(struct sysdev_attribute *attr) | |
734 | { | |
735 | int cpu; | |
736 | ||
737 | mutex_lock(&cpu_mutex); | |
738 | ||
739 | for_each_possible_cpu(cpu) { | |
740 | sysdev_create_file(get_cpu_sysdev(cpu), attr); | |
741 | } | |
742 | ||
743 | mutex_unlock(&cpu_mutex); | |
744 | return 0; | |
745 | } | |
746 | EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr); | |
747 | ||
748 | int cpu_add_sysdev_attr_group(struct attribute_group *attrs) | |
749 | { | |
750 | int cpu; | |
751 | struct sys_device *sysdev; | |
6bcc4c01 | 752 | int ret; |
0344c6c5 CK |
753 | |
754 | mutex_lock(&cpu_mutex); | |
755 | ||
756 | for_each_possible_cpu(cpu) { | |
757 | sysdev = get_cpu_sysdev(cpu); | |
6bcc4c01 OJ |
758 | ret = sysfs_create_group(&sysdev->kobj, attrs); |
759 | WARN_ON(ret != 0); | |
0344c6c5 CK |
760 | } |
761 | ||
762 | mutex_unlock(&cpu_mutex); | |
763 | return 0; | |
764 | } | |
765 | EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group); | |
766 | ||
767 | ||
768 | void cpu_remove_sysdev_attr(struct sysdev_attribute *attr) | |
769 | { | |
770 | int cpu; | |
771 | ||
772 | mutex_lock(&cpu_mutex); | |
773 | ||
774 | for_each_possible_cpu(cpu) { | |
775 | sysdev_remove_file(get_cpu_sysdev(cpu), attr); | |
776 | } | |
777 | ||
778 | mutex_unlock(&cpu_mutex); | |
779 | } | |
780 | EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr); | |
781 | ||
782 | void cpu_remove_sysdev_attr_group(struct attribute_group *attrs) | |
783 | { | |
784 | int cpu; | |
785 | struct sys_device *sysdev; | |
786 | ||
787 | mutex_lock(&cpu_mutex); | |
788 | ||
789 | for_each_possible_cpu(cpu) { | |
790 | sysdev = get_cpu_sysdev(cpu); | |
791 | sysfs_remove_group(&sysdev->kobj, attrs); | |
792 | } | |
793 | ||
794 | mutex_unlock(&cpu_mutex); | |
795 | } | |
796 | EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group); | |
797 | ||
798 | ||
1da177e4 LT |
799 | /* NUMA stuff */ |
800 | ||
801 | #ifdef CONFIG_NUMA | |
1da177e4 LT |
802 | static void register_nodes(void) |
803 | { | |
804 | int i; | |
805 | ||
0fc44159 YG |
806 | for (i = 0; i < MAX_NUMNODES; i++) |
807 | register_one_node(i); | |
1da177e4 | 808 | } |
953039c8 JK |
809 | |
810 | int sysfs_add_device_to_node(struct sys_device *dev, int nid) | |
811 | { | |
812 | struct node *node = &node_devices[nid]; | |
813 | return sysfs_create_link(&node->sysdev.kobj, &dev->kobj, | |
814 | kobject_name(&dev->kobj)); | |
815 | } | |
12654f77 | 816 | EXPORT_SYMBOL_GPL(sysfs_add_device_to_node); |
953039c8 JK |
817 | |
818 | void sysfs_remove_device_from_node(struct sys_device *dev, int nid) | |
819 | { | |
820 | struct node *node = &node_devices[nid]; | |
821 | sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj)); | |
822 | } | |
12654f77 | 823 | EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node); |
953039c8 | 824 | |
1da177e4 LT |
825 | #else |
826 | static void register_nodes(void) | |
827 | { | |
828 | return; | |
829 | } | |
953039c8 | 830 | |
1da177e4 LT |
831 | #endif |
832 | ||
833 | /* Only valid if CPU is present. */ | |
00bf6e90 SR |
834 | static ssize_t show_physical_id(struct sys_device *dev, |
835 | struct sysdev_attribute *attr, char *buf) | |
1da177e4 LT |
836 | { |
837 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); | |
838 | ||
839 | return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id)); | |
840 | } | |
841 | static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL); | |
842 | ||
843 | static int __init topology_init(void) | |
844 | { | |
845 | int cpu; | |
1da177e4 LT |
846 | |
847 | register_nodes(); | |
1da177e4 LT |
848 | register_cpu_notifier(&sysfs_cpu_nb); |
849 | ||
0e551954 | 850 | for_each_possible_cpu(cpu) { |
1da177e4 LT |
851 | struct cpu *c = &per_cpu(cpu_devices, cpu); |
852 | ||
1da177e4 LT |
853 | /* |
854 | * For now, we just see if the system supports making | |
855 | * the RTAS calls for CPU hotplug. But, there may be a | |
856 | * more comprehensive way to do this for an individual | |
857 | * CPU. For instance, the boot cpu might never be valid | |
858 | * for hotplugging. | |
859 | */ | |
72486f1f SS |
860 | if (ppc_md.cpu_die) |
861 | c->hotpluggable = 1; | |
1da177e4 | 862 | |
72486f1f | 863 | if (cpu_online(cpu) || c->hotpluggable) { |
76b67ed9 | 864 | register_cpu(c, cpu); |
1da177e4 LT |
865 | |
866 | sysdev_create_file(&c->sysdev, &attr_physical_id); | |
867 | } | |
868 | ||
869 | if (cpu_online(cpu)) | |
870 | register_cpu_online(cpu); | |
871 | } | |
872 | ||
873 | return 0; | |
874 | } | |
e9e77ce8 | 875 | subsys_initcall(topology_init); |