blob: 883e74c0d1b3b53b8a5cd1a003bc4ae30d0659f1 [file] [log] [blame]
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/cpu.h>
3#include <linux/smp.h>
4#include <linux/percpu.h>
5#include <linux/init.h>
6#include <linux/sched.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -04007#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
Stephen Rothwell1ababe12005-08-03 14:35:25 +100015#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/hvcall.h>
17#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/machdep.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110019#include <asm/smp.h>
Paul Mackerrasa6dbf932009-09-09 01:26:03 +000020#include <asm/pmc.h>
Deepthi Dharwar707827f2011-11-30 02:46:42 +000021#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Nathan Lynch93197a32008-12-23 18:55:54 +000023#include "cacheinfo.h"
24
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100025#ifdef CONFIG_PPC64
26#include <asm/paca.h>
27#include <asm/lppaca.h>
28#endif
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static DEFINE_PER_CPU(struct cpu, cpu_devices);
31
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100032/*
33 * SMT snooze delay stuff, 64-bit only for now
34 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100036#ifdef CONFIG_PPC64
37
Anton Blanchard0ddd3e72006-09-22 20:30:14 +100038/* Time in microseconds we delay before sleeping in the idle loop */
Anton Blanchardb878dc02010-05-16 20:02:39 +000039DEFINE_PER_CPU(long, smt_snooze_delay) = { 100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Kay Sievers8a25a2f2011-12-21 14:29:42 -080041static ssize_t store_smt_snooze_delay(struct device *dev,
42 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020043 const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 size_t count)
45{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080046 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 ssize_t ret;
Anton Blanchardb878dc02010-05-16 20:02:39 +000048 long snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Anton Blanchardb878dc02010-05-16 20:02:39 +000050 ret = sscanf(buf, "%ld", &snooze);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (ret != 1)
52 return -EINVAL;
53
Kay Sievers8a25a2f2011-12-21 14:29:42 -080054 per_cpu(smt_snooze_delay, cpu->dev.id) = snooze;
Deepthi Dharwar707827f2011-11-30 02:46:42 +000055 update_smt_snooze_delay(snooze);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 return count;
58}
59
Kay Sievers8a25a2f2011-12-21 14:29:42 -080060static ssize_t show_smt_snooze_delay(struct device *dev,
61 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020062 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080064 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Kay Sievers8a25a2f2011-12-21 14:29:42 -080066 return sprintf(buf, "%ld\n", per_cpu(smt_snooze_delay, cpu->dev.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Kay Sievers8a25a2f2011-12-21 14:29:42 -080069static DEVICE_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 store_smt_snooze_delay);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int __init setup_smt_snooze_delay(char *str)
73{
74 unsigned int cpu;
Anton Blanchardb878dc02010-05-16 20:02:39 +000075 long snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (!cpu_has_feature(CPU_FTR_SMT))
78 return 1;
79
Anton Blanchardb878dc02010-05-16 20:02:39 +000080 snooze = simple_strtol(str, NULL, 10);
81 for_each_possible_cpu(cpu)
82 per_cpu(smt_snooze_delay, cpu) = snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 return 1;
85}
86__setup("smt-snooze-delay=", setup_smt_snooze_delay);
87
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100088#endif /* CONFIG_PPC64 */
Michael Ellerman180a3362005-08-09 11:13:36 +100089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * Enabling PMCs will slow partition context switch times so we only do
92 * it the first time we write to the PMCs.
93 */
94
95static DEFINE_PER_CPU(char, pmcs_enabled);
96
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100097void ppc_enable_pmcs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Paul Mackerrasa6dbf932009-09-09 01:26:03 +000099 ppc_set_pmu_inuse(1);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* Only need to enable them once */
102 if (__get_cpu_var(pmcs_enabled))
103 return;
104
105 __get_cpu_var(pmcs_enabled) = 1;
106
Michael Ellerman180a3362005-08-09 11:13:36 +1000107 if (ppc_md.enable_pmcs)
108 ppc_md.enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000110EXPORT_SYMBOL(ppc_enable_pmcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define SYSFS_PMCSETUP(NAME, ADDRESS) \
Rusty Russell9a371932009-03-11 12:20:05 +0000113static void read_##NAME(void *val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{ \
Benjamin Herrenschmidtec78c8a2009-03-26 19:29:06 +0000115 *(unsigned long *)val = mfspr(ADDRESS); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116} \
Benjamin Herrenschmidtec78c8a2009-03-26 19:29:06 +0000117static void write_##NAME(void *val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{ \
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000119 ppc_enable_pmcs(); \
Rusty Russell9a371932009-03-11 12:20:05 +0000120 mtspr(ADDRESS, *(unsigned long *)val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121} \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800122static ssize_t show_##NAME(struct device *dev, \
123 struct device_attribute *attr, \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200124 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{ \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800126 struct cpu *cpu = container_of(dev, struct cpu, dev); \
Rusty Russell9a371932009-03-11 12:20:05 +0000127 unsigned long val; \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800128 smp_call_function_single(cpu->dev.id, read_##NAME, &val, 1); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return sprintf(buf, "%lx\n", val); \
130} \
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100131static ssize_t __used \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800132 store_##NAME(struct device *dev, struct device_attribute *attr, \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200133 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{ \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800135 struct cpu *cpu = container_of(dev, struct cpu, dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long val; \
137 int ret = sscanf(buf, "%lx", &val); \
138 if (ret != 1) \
139 return -EINVAL; \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800140 smp_call_function_single(cpu->dev.id, write_##NAME, &val, 1); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return count; \
142}
143
Olof Johansson6529c132007-01-28 21:25:57 -0600144
145/* Let's define all possible registers, we'll only hook up the ones
146 * that are implemented on the current processor
147 */
148
Kumar Gala33a7f122008-09-18 17:50:42 -0500149#if defined(CONFIG_PPC64)
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000150#define HAS_PPC_PMC_CLASSIC 1
151#define HAS_PPC_PMC_IBM 1
152#define HAS_PPC_PMC_PA6T 1
Kumar Gala33a7f122008-09-18 17:50:42 -0500153#elif defined(CONFIG_6xx)
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000154#define HAS_PPC_PMC_CLASSIC 1
155#define HAS_PPC_PMC_IBM 1
156#define HAS_PPC_PMC_G4 1
157#endif
158
159
160#ifdef HAS_PPC_PMC_CLASSIC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
162SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
164SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
165SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
166SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
167SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
168SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000169
170#ifdef HAS_PPC_PMC_G4
171SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
172#endif
173
174#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
176SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000177
178SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179SYSFS_PMCSETUP(purr, SPRN_PURR);
Anton Blanchardf0509822006-12-08 17:51:13 +1100180SYSFS_PMCSETUP(spurr, SPRN_SPURR);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100181SYSFS_PMCSETUP(dscr, SPRN_DSCR);
Ananth N Mavinakayanahalli595fe912011-11-10 19:58:53 +0000182SYSFS_PMCSETUP(pir, SPRN_PIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800184static DEVICE_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
185static DEVICE_ATTR(spurr, 0600, show_spurr, NULL);
186static DEVICE_ATTR(dscr, 0600, show_dscr, store_dscr);
187static DEVICE_ATTR(purr, 0600, show_purr, store_purr);
Linus Torvalds7affca32012-01-07 12:03:30 -0800188static DEVICE_ATTR(pir, 0400, show_pir, NULL);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000189
190unsigned long dscr_default = 0;
191EXPORT_SYMBOL(dscr_default);
192
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800193static ssize_t show_dscr_default(struct device *dev,
194 struct device_attribute *attr, char *buf)
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000195{
196 return sprintf(buf, "%lx\n", dscr_default);
197}
198
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800199static ssize_t __used store_dscr_default(struct device *dev,
200 struct device_attribute *attr, const char *buf,
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000201 size_t count)
202{
203 unsigned long val;
204 int ret = 0;
205
206 ret = sscanf(buf, "%lx", &val);
207 if (ret != 1)
208 return -EINVAL;
209 dscr_default = val;
210
211 return count;
212}
213
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800214static DEVICE_ATTR(dscr_default, 0600,
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000215 show_dscr_default, store_dscr_default);
216
217static void sysfs_create_dscr_default(void)
218{
219 int err = 0;
220 if (cpu_has_feature(CPU_FTR_DSCR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800221 err = device_create_file(cpu_subsys.dev_root, &dev_attr_dscr_default);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000222}
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000223#endif /* CONFIG_PPC64 */
224
225#ifdef HAS_PPC_PMC_PA6T
Olof Johansson25fc5302007-04-18 16:38:21 +1000226SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
227SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
228SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
229SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
230SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
231SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
Olof Johansson2e1957f2007-09-05 12:09:06 +1000232#ifdef CONFIG_DEBUG_KERNEL
233SYSFS_PMCSETUP(hid0, SPRN_HID0);
234SYSFS_PMCSETUP(hid1, SPRN_HID1);
235SYSFS_PMCSETUP(hid4, SPRN_HID4);
236SYSFS_PMCSETUP(hid5, SPRN_HID5);
237SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
238SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
239SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
240SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
241SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
242SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
243SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
244SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
245SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
246SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
247SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
248SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
249SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
250SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
251SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
252SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
253SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
254SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
255SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
256SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
257SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
258SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
259SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
260SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
261#endif /* CONFIG_DEBUG_KERNEL */
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000262#endif /* HAS_PPC_PMC_PA6T */
Olof Johansson6529c132007-01-28 21:25:57 -0600263
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000264#ifdef HAS_PPC_PMC_IBM
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800265static struct device_attribute ibm_common_attrs[] = {
266 __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
267 __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
Olof Johansson6529c132007-01-28 21:25:57 -0600268};
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000269#endif /* HAS_PPC_PMC_G4 */
Olof Johansson6529c132007-01-28 21:25:57 -0600270
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000271#ifdef HAS_PPC_PMC_G4
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800272static struct device_attribute g4_common_attrs[] = {
273 __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
274 __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
275 __ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000276};
277#endif /* HAS_PPC_PMC_G4 */
278
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800279static struct device_attribute classic_pmc_attrs[] = {
280 __ATTR(pmc1, 0600, show_pmc1, store_pmc1),
281 __ATTR(pmc2, 0600, show_pmc2, store_pmc2),
282 __ATTR(pmc3, 0600, show_pmc3, store_pmc3),
283 __ATTR(pmc4, 0600, show_pmc4, store_pmc4),
284 __ATTR(pmc5, 0600, show_pmc5, store_pmc5),
285 __ATTR(pmc6, 0600, show_pmc6, store_pmc6),
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000286#ifdef CONFIG_PPC64
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800287 __ATTR(pmc7, 0600, show_pmc7, store_pmc7),
288 __ATTR(pmc8, 0600, show_pmc8, store_pmc8),
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000289#endif
Olof Johansson6529c132007-01-28 21:25:57 -0600290};
291
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000292#ifdef HAS_PPC_PMC_PA6T
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800293static struct device_attribute pa6t_attrs[] = {
294 __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
295 __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
296 __ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
297 __ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
298 __ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
299 __ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
300 __ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
301 __ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
Olof Johansson2e1957f2007-09-05 12:09:06 +1000302#ifdef CONFIG_DEBUG_KERNEL
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800303 __ATTR(hid0, 0600, show_hid0, store_hid0),
304 __ATTR(hid1, 0600, show_hid1, store_hid1),
305 __ATTR(hid4, 0600, show_hid4, store_hid4),
306 __ATTR(hid5, 0600, show_hid5, store_hid5),
307 __ATTR(ima0, 0600, show_ima0, store_ima0),
308 __ATTR(ima1, 0600, show_ima1, store_ima1),
309 __ATTR(ima2, 0600, show_ima2, store_ima2),
310 __ATTR(ima3, 0600, show_ima3, store_ima3),
311 __ATTR(ima4, 0600, show_ima4, store_ima4),
312 __ATTR(ima5, 0600, show_ima5, store_ima5),
313 __ATTR(ima6, 0600, show_ima6, store_ima6),
314 __ATTR(ima7, 0600, show_ima7, store_ima7),
315 __ATTR(ima8, 0600, show_ima8, store_ima8),
316 __ATTR(ima9, 0600, show_ima9, store_ima9),
317 __ATTR(imaat, 0600, show_imaat, store_imaat),
318 __ATTR(btcr, 0600, show_btcr, store_btcr),
319 __ATTR(pccr, 0600, show_pccr, store_pccr),
320 __ATTR(rpccr, 0600, show_rpccr, store_rpccr),
321 __ATTR(der, 0600, show_der, store_der),
322 __ATTR(mer, 0600, show_mer, store_mer),
323 __ATTR(ber, 0600, show_ber, store_ber),
324 __ATTR(ier, 0600, show_ier, store_ier),
325 __ATTR(sier, 0600, show_sier, store_sier),
326 __ATTR(siar, 0600, show_siar, store_siar),
327 __ATTR(tsr0, 0600, show_tsr0, store_tsr0),
328 __ATTR(tsr1, 0600, show_tsr1, store_tsr1),
329 __ATTR(tsr2, 0600, show_tsr2, store_tsr2),
330 __ATTR(tsr3, 0600, show_tsr3, store_tsr3),
Olof Johansson2e1957f2007-09-05 12:09:06 +1000331#endif /* CONFIG_DEBUG_KERNEL */
Olof Johansson6529c132007-01-28 21:25:57 -0600332};
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000333#endif /* HAS_PPC_PMC_PA6T */
334#endif /* HAS_PPC_PMC_CLASSIC */
Olof Johansson6529c132007-01-28 21:25:57 -0600335
Nathan Lynch9ba19842008-07-27 15:24:51 +1000336static void __cpuinit register_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct cpu *c = &per_cpu(cpu_devices, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800339 struct device *s = &c->dev;
340 struct device_attribute *attrs, *pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600341 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000343#ifdef CONFIG_PPC64
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100344 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
345 cpu_has_feature(CPU_FTR_SMT))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800346 device_create_file(s, &dev_attr_smt_snooze_delay);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600350 switch (cur_cpu_spec->pmc_type) {
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000351#ifdef HAS_PPC_PMC_IBM
Olof Johansson6529c132007-01-28 21:25:57 -0600352 case PPC_PMC_IBM:
353 attrs = ibm_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800354 nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000355 pmc_attrs = classic_pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600356 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000357#endif /* HAS_PPC_PMC_IBM */
358#ifdef HAS_PPC_PMC_G4
359 case PPC_PMC_G4:
360 attrs = g4_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800361 nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000362 pmc_attrs = classic_pmc_attrs;
363 break;
364#endif /* HAS_PPC_PMC_G4 */
365#ifdef HAS_PPC_PMC_PA6T
Olof Johansson6529c132007-01-28 21:25:57 -0600366 case PPC_PMC_PA6T:
367 /* PA Semi starts counting at PMC0 */
368 attrs = pa6t_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800369 nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
Olof Johansson6529c132007-01-28 21:25:57 -0600370 pmc_attrs = NULL;
371 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000372#endif /* HAS_PPC_PMC_PA6T */
Olof Johansson6529c132007-01-28 21:25:57 -0600373 default:
374 attrs = NULL;
375 nattrs = 0;
376 pmc_attrs = NULL;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Olof Johansson6529c132007-01-28 21:25:57 -0600379 for (i = 0; i < nattrs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800380 device_create_file(s, &attrs[i]);
Olof Johansson6529c132007-01-28 21:25:57 -0600381
382 if (pmc_attrs)
383 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800384 device_create_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000386#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (cpu_has_feature(CPU_FTR_MMCRA))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800388 device_create_file(s, &dev_attr_mmcra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Michael Neulingafd05422006-07-28 13:58:37 +1000390 if (cpu_has_feature(CPU_FTR_PURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800391 device_create_file(s, &dev_attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100392
Anton Blanchardf0509822006-12-08 17:51:13 +1100393 if (cpu_has_feature(CPU_FTR_SPURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800394 device_create_file(s, &dev_attr_spurr);
Anton Blanchardf0509822006-12-08 17:51:13 +1100395
Anton Blanchard4c1985572006-12-08 17:46:58 +1100396 if (cpu_has_feature(CPU_FTR_DSCR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800397 device_create_file(s, &dev_attr_dscr);
Ananth N Mavinakayanahalli595fe912011-11-10 19:58:53 +0000398
399 if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
Linus Torvalds7affca32012-01-07 12:03:30 -0800400 device_create_file(s, &dev_attr_pir);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000401#endif /* CONFIG_PPC64 */
Nathan Lynch124c27d2008-07-27 15:24:55 +1000402
Nathan Lynch93197a32008-12-23 18:55:54 +0000403 cacheinfo_cpu_online(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406#ifdef CONFIG_HOTPLUG_CPU
407static void unregister_cpu_online(unsigned int cpu)
408{
409 struct cpu *c = &per_cpu(cpu_devices, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800410 struct device *s = &c->dev;
411 struct device_attribute *attrs, *pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600412 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100414 BUG_ON(!c->hotpluggable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Milton Millera1e0eb12008-11-16 11:44:42 +0000416#ifdef CONFIG_PPC64
Stephen Rothwellad5cb172006-11-13 14:46:04 +1100417 if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
418 cpu_has_feature(CPU_FTR_SMT))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800419 device_remove_file(s, &dev_attr_smt_snooze_delay);
Milton Millera1e0eb12008-11-16 11:44:42 +0000420#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600423 switch (cur_cpu_spec->pmc_type) {
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000424#ifdef HAS_PPC_PMC_IBM
Olof Johansson6529c132007-01-28 21:25:57 -0600425 case PPC_PMC_IBM:
426 attrs = ibm_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800427 nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000428 pmc_attrs = classic_pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600429 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000430#endif /* HAS_PPC_PMC_IBM */
431#ifdef HAS_PPC_PMC_G4
432 case PPC_PMC_G4:
433 attrs = g4_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800434 nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000435 pmc_attrs = classic_pmc_attrs;
436 break;
437#endif /* HAS_PPC_PMC_G4 */
438#ifdef HAS_PPC_PMC_PA6T
Olof Johansson6529c132007-01-28 21:25:57 -0600439 case PPC_PMC_PA6T:
440 /* PA Semi starts counting at PMC0 */
441 attrs = pa6t_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800442 nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
Olof Johansson6529c132007-01-28 21:25:57 -0600443 pmc_attrs = NULL;
444 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000445#endif /* HAS_PPC_PMC_PA6T */
Olof Johansson6529c132007-01-28 21:25:57 -0600446 default:
447 attrs = NULL;
448 nattrs = 0;
449 pmc_attrs = NULL;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Olof Johansson6529c132007-01-28 21:25:57 -0600452 for (i = 0; i < nattrs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800453 device_remove_file(s, &attrs[i]);
Olof Johansson6529c132007-01-28 21:25:57 -0600454
455 if (pmc_attrs)
456 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800457 device_remove_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000459#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (cpu_has_feature(CPU_FTR_MMCRA))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800461 device_remove_file(s, &dev_attr_mmcra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Michael Neulingafd05422006-07-28 13:58:37 +1000463 if (cpu_has_feature(CPU_FTR_PURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800464 device_remove_file(s, &dev_attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100465
Anton Blanchardf0509822006-12-08 17:51:13 +1100466 if (cpu_has_feature(CPU_FTR_SPURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800467 device_remove_file(s, &dev_attr_spurr);
Anton Blanchardf0509822006-12-08 17:51:13 +1100468
Anton Blanchard4c1985572006-12-08 17:46:58 +1100469 if (cpu_has_feature(CPU_FTR_DSCR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800470 device_remove_file(s, &dev_attr_dscr);
Ananth N Mavinakayanahalli595fe912011-11-10 19:58:53 +0000471
472 if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
Linus Torvalds7affca32012-01-07 12:03:30 -0800473 device_remove_file(s, &dev_attr_pir);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000474#endif /* CONFIG_PPC64 */
Nathan Lynch124c27d2008-07-27 15:24:55 +1000475
Nathan Lynch93197a32008-12-23 18:55:54 +0000476 cacheinfo_cpu_offline(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
Nathan Fontenot12633e82009-11-25 17:23:25 +0000478
479#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
480ssize_t arch_cpu_probe(const char *buf, size_t count)
481{
482 if (ppc_md.cpu_probe)
483 return ppc_md.cpu_probe(buf, count);
484
485 return -EINVAL;
486}
487
488ssize_t arch_cpu_release(const char *buf, size_t count)
489{
490 if (ppc_md.cpu_release)
491 return ppc_md.cpu_release(buf, count);
492
493 return -EINVAL;
494}
495#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#endif /* CONFIG_HOTPLUG_CPU */
498
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700499static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 unsigned long action, void *hcpu)
501{
502 unsigned int cpu = (unsigned int)(long)hcpu;
503
504 switch (action) {
505 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700506 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 register_cpu_online(cpu);
508 break;
509#ifdef CONFIG_HOTPLUG_CPU
510 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700511 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unregister_cpu_online(cpu);
513 break;
514#endif
515 }
516 return NOTIFY_OK;
517}
518
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700519static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 .notifier_call = sysfs_cpu_notify,
521};
522
Christian Krafft0344c6c2006-10-24 18:31:24 +0200523static DEFINE_MUTEX(cpu_mutex);
524
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800525int cpu_add_dev_attr(struct device_attribute *attr)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200526{
527 int cpu;
528
529 mutex_lock(&cpu_mutex);
530
531 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800532 device_create_file(get_cpu_device(cpu), attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200533 }
534
535 mutex_unlock(&cpu_mutex);
536 return 0;
537}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800538EXPORT_SYMBOL_GPL(cpu_add_dev_attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200539
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800540int cpu_add_dev_attr_group(struct attribute_group *attrs)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200541{
542 int cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800543 struct device *dev;
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000544 int ret;
Christian Krafft0344c6c2006-10-24 18:31:24 +0200545
546 mutex_lock(&cpu_mutex);
547
548 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800549 dev = get_cpu_device(cpu);
550 ret = sysfs_create_group(&dev->kobj, attrs);
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000551 WARN_ON(ret != 0);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200552 }
553
554 mutex_unlock(&cpu_mutex);
555 return 0;
556}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800557EXPORT_SYMBOL_GPL(cpu_add_dev_attr_group);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200558
559
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800560void cpu_remove_dev_attr(struct device_attribute *attr)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200561{
562 int cpu;
563
564 mutex_lock(&cpu_mutex);
565
566 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800567 device_remove_file(get_cpu_device(cpu), attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200568 }
569
570 mutex_unlock(&cpu_mutex);
571}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800572EXPORT_SYMBOL_GPL(cpu_remove_dev_attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200573
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800574void cpu_remove_dev_attr_group(struct attribute_group *attrs)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200575{
576 int cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800577 struct device *dev;
Christian Krafft0344c6c2006-10-24 18:31:24 +0200578
579 mutex_lock(&cpu_mutex);
580
581 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800582 dev = get_cpu_device(cpu);
583 sysfs_remove_group(&dev->kobj, attrs);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200584 }
585
586 mutex_unlock(&cpu_mutex);
587}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800588EXPORT_SYMBOL_GPL(cpu_remove_dev_attr_group);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200589
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/* NUMA stuff */
592
593#ifdef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static void register_nodes(void)
595{
596 int i;
597
Yasunori Goto0fc44152006-06-27 02:53:38 -0700598 for (i = 0; i < MAX_NUMNODES; i++)
599 register_one_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700601
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800602int sysfs_add_device_to_node(struct device *dev, int nid)
Jeremy Kerr953039c2006-05-01 12:16:12 -0700603{
604 struct node *node = &node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -0800605 return sysfs_create_link(&node->dev.kobj, &dev->kobj,
Jeremy Kerr953039c2006-05-01 12:16:12 -0700606 kobject_name(&dev->kobj));
607}
Johannes Berg12654f72007-07-05 19:35:33 +1000608EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700609
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800610void sysfs_remove_device_from_node(struct device *dev, int nid)
Jeremy Kerr953039c2006-05-01 12:16:12 -0700611{
612 struct node *node = &node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -0800613 sysfs_remove_link(&node->dev.kobj, kobject_name(&dev->kobj));
Jeremy Kerr953039c2006-05-01 12:16:12 -0700614}
Johannes Berg12654f72007-07-05 19:35:33 +1000615EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#else
618static void register_nodes(void)
619{
620 return;
621}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623#endif
624
625/* Only valid if CPU is present. */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800626static ssize_t show_physical_id(struct device *dev,
627 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800629 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800631 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->dev.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800633static DEVICE_ATTR(physical_id, 0444, show_physical_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635static int __init topology_init(void)
636{
637 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 register_nodes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 register_cpu_notifier(&sysfs_cpu_nb);
641
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800642 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct cpu *c = &per_cpu(cpu_devices, cpu);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
646 * For now, we just see if the system supports making
647 * the RTAS calls for CPU hotplug. But, there may be a
648 * more comprehensive way to do this for an individual
649 * CPU. For instance, the boot cpu might never be valid
650 * for hotplugging.
651 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100652 if (ppc_md.cpu_die)
653 c->hotpluggable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100655 if (cpu_online(cpu) || c->hotpluggable) {
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700656 register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800658 device_create_file(&c->dev, &dev_attr_physical_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 if (cpu_online(cpu))
662 register_cpu_online(cpu);
663 }
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000664#ifdef CONFIG_PPC64
665 sysfs_create_dscr_default();
666#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 return 0;
669}
Kevin Corrye9e77ce2007-05-03 03:11:49 +1000670subsys_initcall(topology_init);