xref: /linux/arch/powerpc/kernel/sysfs.c (revision ff2632d7d08edc11e8bd0629e9fcfebab25c78b4)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28a25a2fdSKay Sievers #include <linux/device.h>
3d3d2176aSDavid Gibson #include <linux/cpu.h>
4d3d2176aSDavid Gibson #include <linux/smp.h>
5d3d2176aSDavid Gibson #include <linux/percpu.h>
6d3d2176aSDavid Gibson #include <linux/init.h>
7d3d2176aSDavid Gibson #include <linux/sched.h>
84b16f8e2SPaul Gortmaker #include <linux/export.h>
9d3d2176aSDavid Gibson #include <linux/nodemask.h>
10d3d2176aSDavid Gibson #include <linux/cpumask.h>
11d3d2176aSDavid Gibson #include <linux/notifier.h>
12e6f6390aSChristophe Leroy #include <linux/of.h>
13d3d2176aSDavid Gibson 
14d3d2176aSDavid Gibson #include <asm/current.h>
15d3d2176aSDavid Gibson #include <asm/processor.h>
16d3d2176aSDavid Gibson #include <asm/cputable.h>
17d3d2176aSDavid Gibson #include <asm/hvcall.h>
18d3d2176aSDavid Gibson #include <asm/machdep.h>
19d3d2176aSDavid Gibson #include <asm/smp.h>
20a6dbf93aSPaul Mackerras #include <asm/pmc.h>
21d1211af3SMadhavan Srinivasan #include <asm/firmware.h>
226909f179SGautham R. Shenoy #include <asm/idle.h>
23734560acSRyan Grimm #include <asm/svm.h>
24d3d2176aSDavid Gibson 
2593197a36SNathan Lynch #include "cacheinfo.h"
26c0abd0c7SNicholas Piggin #include "setup.h"
2793197a36SNathan Lynch 
28b950bdd0SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
29b950bdd0SBenjamin Herrenschmidt #include <asm/paca.h>
30b950bdd0SBenjamin Herrenschmidt #include <asm/lppaca.h>
31b950bdd0SBenjamin Herrenschmidt #endif
32b950bdd0SBenjamin Herrenschmidt 
33d3d2176aSDavid Gibson static DEFINE_PER_CPU(struct cpu, cpu_devices);
34d3d2176aSDavid Gibson 
35b950bdd0SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
36b950bdd0SBenjamin Herrenschmidt 
37a02f6d42SJoel Stanley /*
38a02f6d42SJoel Stanley  * Snooze delay has not been hooked up since 3fa8cad82b94 ("powerpc/pseries/cpuidle:
39a02f6d42SJoel Stanley  * smt-snooze-delay cleanup.") and has been broken even longer. As was foretold in
40a02f6d42SJoel Stanley  * 2014:
41a02f6d42SJoel Stanley  *
42a02f6d42SJoel Stanley  *  "ppc64_util currently utilises it. Once we fix ppc64_util, propose to clean
43a02f6d42SJoel Stanley  *  up the kernel code."
44a02f6d42SJoel Stanley  *
45a02f6d42SJoel Stanley  * powerpc-utils stopped using it as of 1.3.8. At some point in the future this
46a02f6d42SJoel Stanley  * code should be removed.
47a02f6d42SJoel Stanley  */
48d3d2176aSDavid Gibson 
store_smt_snooze_delay(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)498a25a2fdSKay Sievers static ssize_t store_smt_snooze_delay(struct device *dev,
508a25a2fdSKay Sievers 				      struct device_attribute *attr,
514a0b2b4dSAndi Kleen 				      const char *buf,
52d3d2176aSDavid Gibson 				      size_t count)
53d3d2176aSDavid Gibson {
54a02f6d42SJoel Stanley 	pr_warn_once("%s (%d) stored to unsupported smt_snooze_delay, which has no effect.\n",
55a02f6d42SJoel Stanley 		     current->comm, current->pid);
56d3d2176aSDavid Gibson 	return count;
57d3d2176aSDavid Gibson }
58d3d2176aSDavid Gibson 
show_smt_snooze_delay(struct device * dev,struct device_attribute * attr,char * buf)598a25a2fdSKay Sievers static ssize_t show_smt_snooze_delay(struct device *dev,
608a25a2fdSKay Sievers 				     struct device_attribute *attr,
614a0b2b4dSAndi Kleen 				     char *buf)
62d3d2176aSDavid Gibson {
63a02f6d42SJoel Stanley 	pr_warn_once("%s (%d) read from unsupported smt_snooze_delay\n",
64a02f6d42SJoel Stanley 		     current->comm, current->pid);
65a02f6d42SJoel Stanley 	return sprintf(buf, "100\n");
66d3d2176aSDavid Gibson }
67d3d2176aSDavid Gibson 
688a25a2fdSKay Sievers static DEVICE_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
69d3d2176aSDavid Gibson 		   store_smt_snooze_delay);
70d3d2176aSDavid Gibson 
setup_smt_snooze_delay(char * str)71d3d2176aSDavid Gibson static int __init setup_smt_snooze_delay(char *str)
72d3d2176aSDavid Gibson {
73d3d2176aSDavid Gibson 	if (!cpu_has_feature(CPU_FTR_SMT))
74d3d2176aSDavid Gibson 		return 1;
75d3d2176aSDavid Gibson 
76a02f6d42SJoel Stanley 	pr_warn("smt-snooze-delay command line option has no effect\n");
77d3d2176aSDavid Gibson 	return 1;
78d3d2176aSDavid Gibson }
79d3d2176aSDavid Gibson __setup("smt-snooze-delay=", setup_smt_snooze_delay);
80d3d2176aSDavid Gibson 
81b950bdd0SBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
82d3d2176aSDavid Gibson 
83fcdb524dSMadhavan Srinivasan #define __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, EXTRA) \
84fcdb524dSMadhavan Srinivasan static void read_##NAME(void *val) \
85fcdb524dSMadhavan Srinivasan { \
86fcdb524dSMadhavan Srinivasan 	*(unsigned long *)val = mfspr(ADDRESS);	\
87fcdb524dSMadhavan Srinivasan } \
88fcdb524dSMadhavan Srinivasan static void write_##NAME(void *val) \
89fcdb524dSMadhavan Srinivasan { \
90fcdb524dSMadhavan Srinivasan 	EXTRA; \
91fcdb524dSMadhavan Srinivasan 	mtspr(ADDRESS, *(unsigned long *)val);	\
92fcdb524dSMadhavan Srinivasan }
93fcdb524dSMadhavan Srinivasan 
94fcdb524dSMadhavan Srinivasan #define __SYSFS_SPRSETUP_SHOW_STORE(NAME) \
95fcdb524dSMadhavan Srinivasan static ssize_t show_##NAME(struct device *dev, \
96fcdb524dSMadhavan Srinivasan 			struct device_attribute *attr, \
97fcdb524dSMadhavan Srinivasan 			char *buf) \
98fcdb524dSMadhavan Srinivasan { \
99fcdb524dSMadhavan Srinivasan 	struct cpu *cpu = container_of(dev, struct cpu, dev); \
100fcdb524dSMadhavan Srinivasan 	unsigned long val; \
101fcdb524dSMadhavan Srinivasan 	smp_call_function_single(cpu->dev.id, read_##NAME, &val, 1);	\
102fcdb524dSMadhavan Srinivasan 	return sprintf(buf, "%lx\n", val); \
103fcdb524dSMadhavan Srinivasan } \
104fcdb524dSMadhavan Srinivasan static ssize_t __used \
105fcdb524dSMadhavan Srinivasan 	store_##NAME(struct device *dev, struct device_attribute *attr, \
106fcdb524dSMadhavan Srinivasan 			const char *buf, size_t count) \
107fcdb524dSMadhavan Srinivasan { \
108fcdb524dSMadhavan Srinivasan 	struct cpu *cpu = container_of(dev, struct cpu, dev); \
109fcdb524dSMadhavan Srinivasan 	unsigned long val; \
110fcdb524dSMadhavan Srinivasan 	int ret = sscanf(buf, "%lx", &val); \
111fcdb524dSMadhavan Srinivasan 	if (ret != 1) \
112fcdb524dSMadhavan Srinivasan 		return -EINVAL; \
113fcdb524dSMadhavan Srinivasan 	smp_call_function_single(cpu->dev.id, write_##NAME, &val, 1); \
114fcdb524dSMadhavan Srinivasan 	return count; \
115fcdb524dSMadhavan Srinivasan }
116fcdb524dSMadhavan Srinivasan 
117fcdb524dSMadhavan Srinivasan #define SYSFS_PMCSETUP(NAME, ADDRESS) \
118fcdb524dSMadhavan Srinivasan 	__SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, ppc_enable_pmcs()) \
119fcdb524dSMadhavan Srinivasan 	__SYSFS_SPRSETUP_SHOW_STORE(NAME)
120fcdb524dSMadhavan Srinivasan #define SYSFS_SPRSETUP(NAME, ADDRESS) \
121fcdb524dSMadhavan Srinivasan 	__SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, ) \
122fcdb524dSMadhavan Srinivasan 	__SYSFS_SPRSETUP_SHOW_STORE(NAME)
123fcdb524dSMadhavan Srinivasan 
124fcdb524dSMadhavan Srinivasan #define SYSFS_SPRSETUP_SHOW_STORE(NAME) \
125fcdb524dSMadhavan Srinivasan 	__SYSFS_SPRSETUP_SHOW_STORE(NAME)
126fcdb524dSMadhavan Srinivasan 
127fcdb524dSMadhavan Srinivasan #ifdef CONFIG_PPC64
128fcdb524dSMadhavan Srinivasan 
129fcdb524dSMadhavan Srinivasan /*
130fcdb524dSMadhavan Srinivasan  * This is the system wide DSCR register default value. Any
131fcdb524dSMadhavan Srinivasan  * change to this default value through the sysfs interface
132fcdb524dSMadhavan Srinivasan  * will update all per cpu DSCR default values across the
133fcdb524dSMadhavan Srinivasan  * system stored in their respective PACA structures.
134fcdb524dSMadhavan Srinivasan  */
135fcdb524dSMadhavan Srinivasan static unsigned long dscr_default;
136fcdb524dSMadhavan Srinivasan 
137fcdb524dSMadhavan Srinivasan /**
138fcdb524dSMadhavan Srinivasan  * read_dscr() - Fetch the cpu specific DSCR default
139fcdb524dSMadhavan Srinivasan  * @val:	Returned cpu specific DSCR default value
140fcdb524dSMadhavan Srinivasan  *
141fcdb524dSMadhavan Srinivasan  * This function returns the per cpu DSCR default value
142*0ddbbb89SBjorn Helgaas  * for any cpu which is contained in its PACA structure.
143fcdb524dSMadhavan Srinivasan  */
read_dscr(void * val)144fcdb524dSMadhavan Srinivasan static void read_dscr(void *val)
145fcdb524dSMadhavan Srinivasan {
146fcdb524dSMadhavan Srinivasan 	*(unsigned long *)val = get_paca()->dscr_default;
147fcdb524dSMadhavan Srinivasan }
148fcdb524dSMadhavan Srinivasan 
149fcdb524dSMadhavan Srinivasan 
150fcdb524dSMadhavan Srinivasan /**
151fcdb524dSMadhavan Srinivasan  * write_dscr() - Update the cpu specific DSCR default
152fcdb524dSMadhavan Srinivasan  * @val:	New cpu specific DSCR default value to update
153fcdb524dSMadhavan Srinivasan  *
154fcdb524dSMadhavan Srinivasan  * This function updates the per cpu DSCR default value
155*0ddbbb89SBjorn Helgaas  * for any cpu which is contained in its PACA structure.
156fcdb524dSMadhavan Srinivasan  */
write_dscr(void * val)157fcdb524dSMadhavan Srinivasan static void write_dscr(void *val)
158fcdb524dSMadhavan Srinivasan {
159fcdb524dSMadhavan Srinivasan 	get_paca()->dscr_default = *(unsigned long *)val;
160fcdb524dSMadhavan Srinivasan 	if (!current->thread.dscr_inherit) {
161fcdb524dSMadhavan Srinivasan 		current->thread.dscr = *(unsigned long *)val;
162fcdb524dSMadhavan Srinivasan 		mtspr(SPRN_DSCR, *(unsigned long *)val);
163fcdb524dSMadhavan Srinivasan 	}
164fcdb524dSMadhavan Srinivasan }
165fcdb524dSMadhavan Srinivasan 
166fcdb524dSMadhavan Srinivasan SYSFS_SPRSETUP_SHOW_STORE(dscr);
167fcdb524dSMadhavan Srinivasan static DEVICE_ATTR(dscr, 0600, show_dscr, store_dscr);
168fcdb524dSMadhavan Srinivasan 
add_write_permission_dev_attr(struct device_attribute * attr)169fcdb524dSMadhavan Srinivasan static void add_write_permission_dev_attr(struct device_attribute *attr)
170fcdb524dSMadhavan Srinivasan {
171fcdb524dSMadhavan Srinivasan 	attr->attr.mode |= 0200;
172fcdb524dSMadhavan Srinivasan }
173fcdb524dSMadhavan Srinivasan 
174fcdb524dSMadhavan Srinivasan /**
175fcdb524dSMadhavan Srinivasan  * show_dscr_default() - Fetch the system wide DSCR default
176fcdb524dSMadhavan Srinivasan  * @dev:	Device structure
177fcdb524dSMadhavan Srinivasan  * @attr:	Device attribute structure
178fcdb524dSMadhavan Srinivasan  * @buf:	Interface buffer
179fcdb524dSMadhavan Srinivasan  *
180fcdb524dSMadhavan Srinivasan  * This function returns the system wide DSCR default value.
181fcdb524dSMadhavan Srinivasan  */
show_dscr_default(struct device * dev,struct device_attribute * attr,char * buf)182fcdb524dSMadhavan Srinivasan static ssize_t show_dscr_default(struct device *dev,
183fcdb524dSMadhavan Srinivasan 		struct device_attribute *attr, char *buf)
184fcdb524dSMadhavan Srinivasan {
185fcdb524dSMadhavan Srinivasan 	return sprintf(buf, "%lx\n", dscr_default);
186fcdb524dSMadhavan Srinivasan }
187fcdb524dSMadhavan Srinivasan 
188fcdb524dSMadhavan Srinivasan /**
189fcdb524dSMadhavan Srinivasan  * store_dscr_default() - Update the system wide DSCR default
190fcdb524dSMadhavan Srinivasan  * @dev:	Device structure
191fcdb524dSMadhavan Srinivasan  * @attr:	Device attribute structure
192fcdb524dSMadhavan Srinivasan  * @buf:	Interface buffer
193fcdb524dSMadhavan Srinivasan  * @count:	Size of the update
194fcdb524dSMadhavan Srinivasan  *
195fcdb524dSMadhavan Srinivasan  * This function updates the system wide DSCR default value.
196fcdb524dSMadhavan Srinivasan  */
store_dscr_default(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)197fcdb524dSMadhavan Srinivasan static ssize_t __used store_dscr_default(struct device *dev,
198fcdb524dSMadhavan Srinivasan 		struct device_attribute *attr, const char *buf,
199fcdb524dSMadhavan Srinivasan 		size_t count)
200fcdb524dSMadhavan Srinivasan {
201fcdb524dSMadhavan Srinivasan 	unsigned long val;
202fcdb524dSMadhavan Srinivasan 	int ret = 0;
203fcdb524dSMadhavan Srinivasan 
204fcdb524dSMadhavan Srinivasan 	ret = sscanf(buf, "%lx", &val);
205fcdb524dSMadhavan Srinivasan 	if (ret != 1)
206fcdb524dSMadhavan Srinivasan 		return -EINVAL;
207fcdb524dSMadhavan Srinivasan 	dscr_default = val;
208fcdb524dSMadhavan Srinivasan 
209fcdb524dSMadhavan Srinivasan 	on_each_cpu(write_dscr, &val, 1);
210fcdb524dSMadhavan Srinivasan 
211fcdb524dSMadhavan Srinivasan 	return count;
212fcdb524dSMadhavan Srinivasan }
213fcdb524dSMadhavan Srinivasan 
214fcdb524dSMadhavan Srinivasan static DEVICE_ATTR(dscr_default, 0600,
215fcdb524dSMadhavan Srinivasan 		show_dscr_default, store_dscr_default);
216fcdb524dSMadhavan Srinivasan 
sysfs_create_dscr_default(void)217d276960dSNick Child static void __init sysfs_create_dscr_default(void)
218fcdb524dSMadhavan Srinivasan {
219fcdb524dSMadhavan Srinivasan 	if (cpu_has_feature(CPU_FTR_DSCR)) {
2201b401896SGreg Kroah-Hartman 		struct device *dev_root;
221fcdb524dSMadhavan Srinivasan 		int cpu;
222fcdb524dSMadhavan Srinivasan 
223fcdb524dSMadhavan Srinivasan 		dscr_default = spr_default_dscr;
224fcdb524dSMadhavan Srinivasan 		for_each_possible_cpu(cpu)
225fcdb524dSMadhavan Srinivasan 			paca_ptrs[cpu]->dscr_default = dscr_default;
226fcdb524dSMadhavan Srinivasan 
2271b401896SGreg Kroah-Hartman 		dev_root = bus_get_dev_root(&cpu_subsys);
2281b401896SGreg Kroah-Hartman 		if (dev_root) {
2291b401896SGreg Kroah-Hartman 			device_create_file(dev_root, &dev_attr_dscr_default);
2301b401896SGreg Kroah-Hartman 			put_device(dev_root);
2311b401896SGreg Kroah-Hartman 		}
232fcdb524dSMadhavan Srinivasan 	}
233fcdb524dSMadhavan Srinivasan }
234fcdb524dSMadhavan Srinivasan #endif /* CONFIG_PPC64 */
235fcdb524dSMadhavan Srinivasan 
2363e731858SChristophe Leroy #ifdef CONFIG_PPC_E500
237a7189483SWang Dongsheng #define MAX_BIT				63
238a7189483SWang Dongsheng 
239a7189483SWang Dongsheng static u64 pw20_wt;
240a7189483SWang Dongsheng static u64 altivec_idle_wt;
241a7189483SWang Dongsheng 
get_idle_ticks_bit(u64 ns)242a7189483SWang Dongsheng static unsigned int get_idle_ticks_bit(u64 ns)
243a7189483SWang Dongsheng {
244a7189483SWang Dongsheng 	u64 cycle;
245a7189483SWang Dongsheng 
246a7189483SWang Dongsheng 	if (ns >= 10000)
247a7189483SWang Dongsheng 		cycle = div_u64(ns + 500, 1000) * tb_ticks_per_usec;
248a7189483SWang Dongsheng 	else
249a7189483SWang Dongsheng 		cycle = div_u64(ns * tb_ticks_per_usec, 1000);
250a7189483SWang Dongsheng 
251a7189483SWang Dongsheng 	if (!cycle)
252a7189483SWang Dongsheng 		return 0;
253a7189483SWang Dongsheng 
254a7189483SWang Dongsheng 	return ilog2(cycle);
255a7189483SWang Dongsheng }
256a7189483SWang Dongsheng 
do_show_pwrmgtcr0(void * val)257a7189483SWang Dongsheng static void do_show_pwrmgtcr0(void *val)
258a7189483SWang Dongsheng {
259a7189483SWang Dongsheng 	u32 *value = val;
260a7189483SWang Dongsheng 
261a7189483SWang Dongsheng 	*value = mfspr(SPRN_PWRMGTCR0);
262a7189483SWang Dongsheng }
263a7189483SWang Dongsheng 
show_pw20_state(struct device * dev,struct device_attribute * attr,char * buf)264a7189483SWang Dongsheng static ssize_t show_pw20_state(struct device *dev,
265a7189483SWang Dongsheng 				struct device_attribute *attr, char *buf)
266a7189483SWang Dongsheng {
267a7189483SWang Dongsheng 	u32 value;
268a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
269a7189483SWang Dongsheng 
270a7189483SWang Dongsheng 	smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
271a7189483SWang Dongsheng 
272a7189483SWang Dongsheng 	value &= PWRMGTCR0_PW20_WAIT;
273a7189483SWang Dongsheng 
274a7189483SWang Dongsheng 	return sprintf(buf, "%u\n", value ? 1 : 0);
275a7189483SWang Dongsheng }
276a7189483SWang Dongsheng 
do_store_pw20_state(void * val)277a7189483SWang Dongsheng static void do_store_pw20_state(void *val)
278a7189483SWang Dongsheng {
279a7189483SWang Dongsheng 	u32 *value = val;
280a7189483SWang Dongsheng 	u32 pw20_state;
281a7189483SWang Dongsheng 
282a7189483SWang Dongsheng 	pw20_state = mfspr(SPRN_PWRMGTCR0);
283a7189483SWang Dongsheng 
284a7189483SWang Dongsheng 	if (*value)
285a7189483SWang Dongsheng 		pw20_state |= PWRMGTCR0_PW20_WAIT;
286a7189483SWang Dongsheng 	else
287a7189483SWang Dongsheng 		pw20_state &= ~PWRMGTCR0_PW20_WAIT;
288a7189483SWang Dongsheng 
289a7189483SWang Dongsheng 	mtspr(SPRN_PWRMGTCR0, pw20_state);
290a7189483SWang Dongsheng }
291a7189483SWang Dongsheng 
store_pw20_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)292a7189483SWang Dongsheng static ssize_t store_pw20_state(struct device *dev,
293a7189483SWang Dongsheng 				struct device_attribute *attr,
294a7189483SWang Dongsheng 				const char *buf, size_t count)
295a7189483SWang Dongsheng {
296a7189483SWang Dongsheng 	u32 value;
297a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
298a7189483SWang Dongsheng 
299a7189483SWang Dongsheng 	if (kstrtou32(buf, 0, &value))
300a7189483SWang Dongsheng 		return -EINVAL;
301a7189483SWang Dongsheng 
302a7189483SWang Dongsheng 	if (value > 1)
303a7189483SWang Dongsheng 		return -EINVAL;
304a7189483SWang Dongsheng 
305a7189483SWang Dongsheng 	smp_call_function_single(cpu, do_store_pw20_state, &value, 1);
306a7189483SWang Dongsheng 
307a7189483SWang Dongsheng 	return count;
308a7189483SWang Dongsheng }
309a7189483SWang Dongsheng 
show_pw20_wait_time(struct device * dev,struct device_attribute * attr,char * buf)310a7189483SWang Dongsheng static ssize_t show_pw20_wait_time(struct device *dev,
311a7189483SWang Dongsheng 				struct device_attribute *attr, char *buf)
312a7189483SWang Dongsheng {
313a7189483SWang Dongsheng 	u32 value;
314a7189483SWang Dongsheng 	u64 tb_cycle = 1;
315a7189483SWang Dongsheng 	u64 time;
316a7189483SWang Dongsheng 
317a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
318a7189483SWang Dongsheng 
319a7189483SWang Dongsheng 	if (!pw20_wt) {
320a7189483SWang Dongsheng 		smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
321a7189483SWang Dongsheng 		value = (value & PWRMGTCR0_PW20_ENT) >>
322a7189483SWang Dongsheng 					PWRMGTCR0_PW20_ENT_SHIFT;
323a7189483SWang Dongsheng 
324a7189483SWang Dongsheng 		tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
325a7189483SWang Dongsheng 		/* convert ms to ns */
326a7189483SWang Dongsheng 		if (tb_ticks_per_usec > 1000) {
327a7189483SWang Dongsheng 			time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
328a7189483SWang Dongsheng 		} else {
329a7189483SWang Dongsheng 			u32 rem_us;
330a7189483SWang Dongsheng 
331a7189483SWang Dongsheng 			time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
332a7189483SWang Dongsheng 						&rem_us);
333a7189483SWang Dongsheng 			time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
334a7189483SWang Dongsheng 		}
335a7189483SWang Dongsheng 	} else {
336a7189483SWang Dongsheng 		time = pw20_wt;
337a7189483SWang Dongsheng 	}
338a7189483SWang Dongsheng 
339a7189483SWang Dongsheng 	return sprintf(buf, "%llu\n", time > 0 ? time : 0);
340a7189483SWang Dongsheng }
341a7189483SWang Dongsheng 
set_pw20_wait_entry_bit(void * val)342a7189483SWang Dongsheng static void set_pw20_wait_entry_bit(void *val)
343a7189483SWang Dongsheng {
344a7189483SWang Dongsheng 	u32 *value = val;
345a7189483SWang Dongsheng 	u32 pw20_idle;
346a7189483SWang Dongsheng 
347a7189483SWang Dongsheng 	pw20_idle = mfspr(SPRN_PWRMGTCR0);
348a7189483SWang Dongsheng 
349a7189483SWang Dongsheng 	/* Set Automatic PW20 Core Idle Count */
350a7189483SWang Dongsheng 	/* clear count */
351a7189483SWang Dongsheng 	pw20_idle &= ~PWRMGTCR0_PW20_ENT;
352a7189483SWang Dongsheng 
353a7189483SWang Dongsheng 	/* set count */
354a7189483SWang Dongsheng 	pw20_idle |= ((MAX_BIT - *value) << PWRMGTCR0_PW20_ENT_SHIFT);
355a7189483SWang Dongsheng 
356a7189483SWang Dongsheng 	mtspr(SPRN_PWRMGTCR0, pw20_idle);
357a7189483SWang Dongsheng }
358a7189483SWang Dongsheng 
store_pw20_wait_time(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)359a7189483SWang Dongsheng static ssize_t store_pw20_wait_time(struct device *dev,
360a7189483SWang Dongsheng 				struct device_attribute *attr,
361a7189483SWang Dongsheng 				const char *buf, size_t count)
362a7189483SWang Dongsheng {
363a7189483SWang Dongsheng 	u32 entry_bit;
364a7189483SWang Dongsheng 	u64 value;
365a7189483SWang Dongsheng 
366a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
367a7189483SWang Dongsheng 
368a7189483SWang Dongsheng 	if (kstrtou64(buf, 0, &value))
369a7189483SWang Dongsheng 		return -EINVAL;
370a7189483SWang Dongsheng 
371a7189483SWang Dongsheng 	if (!value)
372a7189483SWang Dongsheng 		return -EINVAL;
373a7189483SWang Dongsheng 
374a7189483SWang Dongsheng 	entry_bit = get_idle_ticks_bit(value);
375a7189483SWang Dongsheng 	if (entry_bit > MAX_BIT)
376a7189483SWang Dongsheng 		return -EINVAL;
377a7189483SWang Dongsheng 
378a7189483SWang Dongsheng 	pw20_wt = value;
379a7189483SWang Dongsheng 
380a7189483SWang Dongsheng 	smp_call_function_single(cpu, set_pw20_wait_entry_bit,
381a7189483SWang Dongsheng 				&entry_bit, 1);
382a7189483SWang Dongsheng 
383a7189483SWang Dongsheng 	return count;
384a7189483SWang Dongsheng }
385a7189483SWang Dongsheng 
show_altivec_idle(struct device * dev,struct device_attribute * attr,char * buf)386a7189483SWang Dongsheng static ssize_t show_altivec_idle(struct device *dev,
387a7189483SWang Dongsheng 				struct device_attribute *attr, char *buf)
388a7189483SWang Dongsheng {
389a7189483SWang Dongsheng 	u32 value;
390a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
391a7189483SWang Dongsheng 
392a7189483SWang Dongsheng 	smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
393a7189483SWang Dongsheng 
394a7189483SWang Dongsheng 	value &= PWRMGTCR0_AV_IDLE_PD_EN;
395a7189483SWang Dongsheng 
396a7189483SWang Dongsheng 	return sprintf(buf, "%u\n", value ? 1 : 0);
397a7189483SWang Dongsheng }
398a7189483SWang Dongsheng 
do_store_altivec_idle(void * val)399a7189483SWang Dongsheng static void do_store_altivec_idle(void *val)
400a7189483SWang Dongsheng {
401a7189483SWang Dongsheng 	u32 *value = val;
402a7189483SWang Dongsheng 	u32 altivec_idle;
403a7189483SWang Dongsheng 
404a7189483SWang Dongsheng 	altivec_idle = mfspr(SPRN_PWRMGTCR0);
405a7189483SWang Dongsheng 
406a7189483SWang Dongsheng 	if (*value)
407a7189483SWang Dongsheng 		altivec_idle |= PWRMGTCR0_AV_IDLE_PD_EN;
408a7189483SWang Dongsheng 	else
409a7189483SWang Dongsheng 		altivec_idle &= ~PWRMGTCR0_AV_IDLE_PD_EN;
410a7189483SWang Dongsheng 
411a7189483SWang Dongsheng 	mtspr(SPRN_PWRMGTCR0, altivec_idle);
412a7189483SWang Dongsheng }
413a7189483SWang Dongsheng 
store_altivec_idle(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)414a7189483SWang Dongsheng static ssize_t store_altivec_idle(struct device *dev,
415a7189483SWang Dongsheng 				struct device_attribute *attr,
416a7189483SWang Dongsheng 				const char *buf, size_t count)
417a7189483SWang Dongsheng {
418a7189483SWang Dongsheng 	u32 value;
419a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
420a7189483SWang Dongsheng 
421a7189483SWang Dongsheng 	if (kstrtou32(buf, 0, &value))
422a7189483SWang Dongsheng 		return -EINVAL;
423a7189483SWang Dongsheng 
424a7189483SWang Dongsheng 	if (value > 1)
425a7189483SWang Dongsheng 		return -EINVAL;
426a7189483SWang Dongsheng 
427a7189483SWang Dongsheng 	smp_call_function_single(cpu, do_store_altivec_idle, &value, 1);
428a7189483SWang Dongsheng 
429a7189483SWang Dongsheng 	return count;
430a7189483SWang Dongsheng }
431a7189483SWang Dongsheng 
show_altivec_idle_wait_time(struct device * dev,struct device_attribute * attr,char * buf)432a7189483SWang Dongsheng static ssize_t show_altivec_idle_wait_time(struct device *dev,
433a7189483SWang Dongsheng 				struct device_attribute *attr, char *buf)
434a7189483SWang Dongsheng {
435a7189483SWang Dongsheng 	u32 value;
436a7189483SWang Dongsheng 	u64 tb_cycle = 1;
437a7189483SWang Dongsheng 	u64 time;
438a7189483SWang Dongsheng 
439a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
440a7189483SWang Dongsheng 
441a7189483SWang Dongsheng 	if (!altivec_idle_wt) {
442a7189483SWang Dongsheng 		smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
443a7189483SWang Dongsheng 		value = (value & PWRMGTCR0_AV_IDLE_CNT) >>
444a7189483SWang Dongsheng 					PWRMGTCR0_AV_IDLE_CNT_SHIFT;
445a7189483SWang Dongsheng 
446a7189483SWang Dongsheng 		tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
447a7189483SWang Dongsheng 		/* convert ms to ns */
448a7189483SWang Dongsheng 		if (tb_ticks_per_usec > 1000) {
449a7189483SWang Dongsheng 			time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
450a7189483SWang Dongsheng 		} else {
451a7189483SWang Dongsheng 			u32 rem_us;
452a7189483SWang Dongsheng 
453a7189483SWang Dongsheng 			time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
454a7189483SWang Dongsheng 						&rem_us);
455a7189483SWang Dongsheng 			time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
456a7189483SWang Dongsheng 		}
457a7189483SWang Dongsheng 	} else {
458a7189483SWang Dongsheng 		time = altivec_idle_wt;
459a7189483SWang Dongsheng 	}
460a7189483SWang Dongsheng 
461a7189483SWang Dongsheng 	return sprintf(buf, "%llu\n", time > 0 ? time : 0);
462a7189483SWang Dongsheng }
463a7189483SWang Dongsheng 
set_altivec_idle_wait_entry_bit(void * val)464a7189483SWang Dongsheng static void set_altivec_idle_wait_entry_bit(void *val)
465a7189483SWang Dongsheng {
466a7189483SWang Dongsheng 	u32 *value = val;
467a7189483SWang Dongsheng 	u32 altivec_idle;
468a7189483SWang Dongsheng 
469a7189483SWang Dongsheng 	altivec_idle = mfspr(SPRN_PWRMGTCR0);
470a7189483SWang Dongsheng 
471a7189483SWang Dongsheng 	/* Set Automatic AltiVec Idle Count */
472a7189483SWang Dongsheng 	/* clear count */
473a7189483SWang Dongsheng 	altivec_idle &= ~PWRMGTCR0_AV_IDLE_CNT;
474a7189483SWang Dongsheng 
475a7189483SWang Dongsheng 	/* set count */
476a7189483SWang Dongsheng 	altivec_idle |= ((MAX_BIT - *value) << PWRMGTCR0_AV_IDLE_CNT_SHIFT);
477a7189483SWang Dongsheng 
478a7189483SWang Dongsheng 	mtspr(SPRN_PWRMGTCR0, altivec_idle);
479a7189483SWang Dongsheng }
480a7189483SWang Dongsheng 
store_altivec_idle_wait_time(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)481a7189483SWang Dongsheng static ssize_t store_altivec_idle_wait_time(struct device *dev,
482a7189483SWang Dongsheng 				struct device_attribute *attr,
483a7189483SWang Dongsheng 				const char *buf, size_t count)
484a7189483SWang Dongsheng {
485a7189483SWang Dongsheng 	u32 entry_bit;
486a7189483SWang Dongsheng 	u64 value;
487a7189483SWang Dongsheng 
488a7189483SWang Dongsheng 	unsigned int cpu = dev->id;
489a7189483SWang Dongsheng 
490a7189483SWang Dongsheng 	if (kstrtou64(buf, 0, &value))
491a7189483SWang Dongsheng 		return -EINVAL;
492a7189483SWang Dongsheng 
493a7189483SWang Dongsheng 	if (!value)
494a7189483SWang Dongsheng 		return -EINVAL;
495a7189483SWang Dongsheng 
496a7189483SWang Dongsheng 	entry_bit = get_idle_ticks_bit(value);
497a7189483SWang Dongsheng 	if (entry_bit > MAX_BIT)
498a7189483SWang Dongsheng 		return -EINVAL;
499a7189483SWang Dongsheng 
500a7189483SWang Dongsheng 	altivec_idle_wt = value;
501a7189483SWang Dongsheng 
502a7189483SWang Dongsheng 	smp_call_function_single(cpu, set_altivec_idle_wait_entry_bit,
503a7189483SWang Dongsheng 				&entry_bit, 1);
504a7189483SWang Dongsheng 
505a7189483SWang Dongsheng 	return count;
506a7189483SWang Dongsheng }
507a7189483SWang Dongsheng 
508a7189483SWang Dongsheng /*
509a7189483SWang Dongsheng  * Enable/Disable interface:
510a7189483SWang Dongsheng  * 0, disable. 1, enable.
511a7189483SWang Dongsheng  */
512a7189483SWang Dongsheng static DEVICE_ATTR(pw20_state, 0600, show_pw20_state, store_pw20_state);
513a7189483SWang Dongsheng static DEVICE_ATTR(altivec_idle, 0600, show_altivec_idle, store_altivec_idle);
514a7189483SWang Dongsheng 
515a7189483SWang Dongsheng /*
516a7189483SWang Dongsheng  * Set wait time interface:(Nanosecond)
517a7189483SWang Dongsheng  * Example: Base on TBfreq is 41MHZ.
518a7189483SWang Dongsheng  * 1~48(ns): TB[63]
519a7189483SWang Dongsheng  * 49~97(ns): TB[62]
520a7189483SWang Dongsheng  * 98~195(ns): TB[61]
521a7189483SWang Dongsheng  * 196~390(ns): TB[60]
522a7189483SWang Dongsheng  * 391~780(ns): TB[59]
523a7189483SWang Dongsheng  * 781~1560(ns): TB[58]
524a7189483SWang Dongsheng  * ...
525a7189483SWang Dongsheng  */
526a7189483SWang Dongsheng static DEVICE_ATTR(pw20_wait_time, 0600,
527a7189483SWang Dongsheng 			show_pw20_wait_time,
528a7189483SWang Dongsheng 			store_pw20_wait_time);
529a7189483SWang Dongsheng static DEVICE_ATTR(altivec_idle_wait_time, 0600,
530a7189483SWang Dongsheng 			show_altivec_idle_wait_time,
531a7189483SWang Dongsheng 			store_altivec_idle_wait_time);
532a7189483SWang Dongsheng #endif
533a7189483SWang Dongsheng 
534d3d2176aSDavid Gibson /*
535d3d2176aSDavid Gibson  * Enabling PMCs will slow partition context switch times so we only do
536d3d2176aSDavid Gibson  * it the first time we write to the PMCs.
537d3d2176aSDavid Gibson  */
538d3d2176aSDavid Gibson 
539d3d2176aSDavid Gibson static DEFINE_PER_CPU(char, pmcs_enabled);
540d3d2176aSDavid Gibson 
ppc_enable_pmcs(void)541b950bdd0SBenjamin Herrenschmidt void ppc_enable_pmcs(void)
542d3d2176aSDavid Gibson {
543a6dbf93aSPaul Mackerras 	ppc_set_pmu_inuse(1);
544a6dbf93aSPaul Mackerras 
545d3d2176aSDavid Gibson 	/* Only need to enable them once */
54669111bacSChristoph Lameter 	if (__this_cpu_read(pmcs_enabled))
547d3d2176aSDavid Gibson 		return;
548d3d2176aSDavid Gibson 
54969111bacSChristoph Lameter 	__this_cpu_write(pmcs_enabled, 1);
550d3d2176aSDavid Gibson 
551d3d2176aSDavid Gibson 	if (ppc_md.enable_pmcs)
552d3d2176aSDavid Gibson 		ppc_md.enable_pmcs();
553d3d2176aSDavid Gibson }
554b950bdd0SBenjamin Herrenschmidt EXPORT_SYMBOL(ppc_enable_pmcs);
555d3d2176aSDavid Gibson 
55639a360efSSam bobroff 
5576529c13dSOlof Johansson 
5586529c13dSOlof Johansson /* Let's define all possible registers, we'll only hook up the ones
5596529c13dSOlof Johansson  * that are implemented on the current processor
5606529c13dSOlof Johansson  */
5616529c13dSOlof Johansson 
56222697da3SKajol Jain #ifdef CONFIG_PMU_SYSFS
563fcdb524dSMadhavan Srinivasan #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_BOOK3S_32)
564b950bdd0SBenjamin Herrenschmidt #define HAS_PPC_PMC_CLASSIC	1
565b950bdd0SBenjamin Herrenschmidt #define HAS_PPC_PMC_IBM		1
566b950bdd0SBenjamin Herrenschmidt #endif
567b950bdd0SBenjamin Herrenschmidt 
568b950bdd0SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
569fcdb524dSMadhavan Srinivasan #define HAS_PPC_PMC_PA6T	1
570fcdb524dSMadhavan Srinivasan #define HAS_PPC_PMC56          1
571fcdb524dSMadhavan Srinivasan #endif
572b950bdd0SBenjamin Herrenschmidt 
573fcdb524dSMadhavan Srinivasan #ifdef CONFIG_PPC_BOOK3S_32
574fcdb524dSMadhavan Srinivasan #define HAS_PPC_PMC_G4		1
575fcdb524dSMadhavan Srinivasan #endif
57622697da3SKajol Jain #endif /* CONFIG_PMU_SYSFS */
577fcdb524dSMadhavan Srinivasan 
578fcdb524dSMadhavan Srinivasan #if defined(CONFIG_PPC64) && defined(CONFIG_DEBUG_MISC)
579fcdb524dSMadhavan Srinivasan #define HAS_PPC_PA6T
580fcdb524dSMadhavan Srinivasan #endif
581fcdb524dSMadhavan Srinivasan /*
582fcdb524dSMadhavan Srinivasan  * SPRs which are not related to PMU.
583fcdb524dSMadhavan Srinivasan  */
584fcdb524dSMadhavan Srinivasan #ifdef CONFIG_PPC64
585fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(purr, SPRN_PURR);
586fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(spurr, SPRN_SPURR);
587fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(pir, SPRN_PIR);
588b6d34eb4SAnton Blanchard SYSFS_SPRSETUP(tscr, SPRN_TSCR);
589d3d2176aSDavid Gibson 
590d1211af3SMadhavan Srinivasan /*
591d1211af3SMadhavan Srinivasan   Lets only enable read for phyp resources and
592d1211af3SMadhavan Srinivasan   enable write when needed with a separate function.
593d1211af3SMadhavan Srinivasan   Lets be conservative and default to pseries.
594d1211af3SMadhavan Srinivasan */
595d5dae721SBenjamin Herrenschmidt static DEVICE_ATTR(spurr, 0400, show_spurr, NULL);
596d1211af3SMadhavan Srinivasan static DEVICE_ATTR(purr, 0400, show_purr, store_purr);
5977affca35SLinus Torvalds static DEVICE_ATTR(pir, 0400, show_pir, NULL);
598b6d34eb4SAnton Blanchard static DEVICE_ATTR(tscr, 0600, show_tscr, store_tscr);
599b950bdd0SBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
600b950bdd0SBenjamin Herrenschmidt 
601fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PMC_CLASSIC
602fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
603fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
604fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
605fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
606fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
607fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
608fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
609fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
610fcdb524dSMadhavan Srinivasan #endif
611fcdb524dSMadhavan Srinivasan 
612fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PMC_G4
613fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
614fcdb524dSMadhavan Srinivasan #endif
615fcdb524dSMadhavan Srinivasan 
616fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PMC56
617fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
618fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
619fcdb524dSMadhavan Srinivasan 
620fcdb524dSMadhavan Srinivasan SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
621c718547eSMadhavan Srinivasan SYSFS_PMCSETUP(mmcr3, SPRN_MMCR3);
622fcdb524dSMadhavan Srinivasan 
623fcdb524dSMadhavan Srinivasan static DEVICE_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
624c718547eSMadhavan Srinivasan static DEVICE_ATTR(mmcr3, 0600, show_mmcr3, store_mmcr3);
625fcdb524dSMadhavan Srinivasan #endif /* HAS_PPC_PMC56 */
626fcdb524dSMadhavan Srinivasan 
627fcdb524dSMadhavan Srinivasan 
628fcdb524dSMadhavan Srinivasan 
629fcdb524dSMadhavan Srinivasan 
630b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_PA6T
63125fc530eSOlof Johansson SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
63225fc530eSOlof Johansson SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
63325fc530eSOlof Johansson SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
63425fc530eSOlof Johansson SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
63525fc530eSOlof Johansson SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
63625fc530eSOlof Johansson SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
637fcdb524dSMadhavan Srinivasan #endif
638fcdb524dSMadhavan Srinivasan 
639fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PA6T
640fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(hid0, SPRN_HID0);
641fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(hid1, SPRN_HID1);
642fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(hid4, SPRN_HID4);
643fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(hid5, SPRN_HID5);
644fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima0, SPRN_PA6T_IMA0);
645fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima1, SPRN_PA6T_IMA1);
646fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima2, SPRN_PA6T_IMA2);
647fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima3, SPRN_PA6T_IMA3);
648fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima4, SPRN_PA6T_IMA4);
649fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima5, SPRN_PA6T_IMA5);
650fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima6, SPRN_PA6T_IMA6);
651fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima7, SPRN_PA6T_IMA7);
652fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima8, SPRN_PA6T_IMA8);
653fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ima9, SPRN_PA6T_IMA9);
654fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(imaat, SPRN_PA6T_IMAAT);
655fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(btcr, SPRN_PA6T_BTCR);
656fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(pccr, SPRN_PA6T_PCCR);
657fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(rpccr, SPRN_PA6T_RPCCR);
658fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(der, SPRN_PA6T_DER);
659fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(mer, SPRN_PA6T_MER);
660fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ber, SPRN_PA6T_BER);
661fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(ier, SPRN_PA6T_IER);
662fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(sier, SPRN_PA6T_SIER);
663fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(siar, SPRN_PA6T_SIAR);
664fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(tsr0, SPRN_PA6T_TSR0);
665fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(tsr1, SPRN_PA6T_TSR1);
666fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(tsr2, SPRN_PA6T_TSR2);
667fd7e4296SMadhavan Srinivasan SYSFS_SPRSETUP(tsr3, SPRN_PA6T_TSR3);
668fcdb524dSMadhavan Srinivasan #endif /* HAS_PPC_PA6T */
6696529c13dSOlof Johansson 
670b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_IBM
6718a25a2fdSKay Sievers static struct device_attribute ibm_common_attrs[] = {
6728a25a2fdSKay Sievers 	__ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
6738a25a2fdSKay Sievers 	__ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
6746529c13dSOlof Johansson };
675fcdb524dSMadhavan Srinivasan #endif /* HAS_PPC_PMC_IBM */
6766529c13dSOlof Johansson 
677b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_G4
6788a25a2fdSKay Sievers static struct device_attribute g4_common_attrs[] = {
6798a25a2fdSKay Sievers 	__ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
6808a25a2fdSKay Sievers 	__ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
6818a25a2fdSKay Sievers 	__ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
682b950bdd0SBenjamin Herrenschmidt };
683b950bdd0SBenjamin Herrenschmidt #endif /* HAS_PPC_PMC_G4 */
684b950bdd0SBenjamin Herrenschmidt 
685fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PMC_CLASSIC
6868a25a2fdSKay Sievers static struct device_attribute classic_pmc_attrs[] = {
6878a25a2fdSKay Sievers 	__ATTR(pmc1, 0600, show_pmc1, store_pmc1),
6888a25a2fdSKay Sievers 	__ATTR(pmc2, 0600, show_pmc2, store_pmc2),
6898a25a2fdSKay Sievers 	__ATTR(pmc3, 0600, show_pmc3, store_pmc3),
6908a25a2fdSKay Sievers 	__ATTR(pmc4, 0600, show_pmc4, store_pmc4),
6918a25a2fdSKay Sievers 	__ATTR(pmc5, 0600, show_pmc5, store_pmc5),
6928a25a2fdSKay Sievers 	__ATTR(pmc6, 0600, show_pmc6, store_pmc6),
693fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PMC56
6948a25a2fdSKay Sievers 	__ATTR(pmc7, 0600, show_pmc7, store_pmc7),
6958a25a2fdSKay Sievers 	__ATTR(pmc8, 0600, show_pmc8, store_pmc8),
696b950bdd0SBenjamin Herrenschmidt #endif
6976529c13dSOlof Johansson };
698fcdb524dSMadhavan Srinivasan #endif
6996529c13dSOlof Johansson 
700fcdb524dSMadhavan Srinivasan #if defined(HAS_PPC_PMC_PA6T) || defined(HAS_PPC_PA6T)
7018a25a2fdSKay Sievers static struct device_attribute pa6t_attrs[] = {
702fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PMC_PA6T
7038a25a2fdSKay Sievers 	__ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
7048a25a2fdSKay Sievers 	__ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
7058a25a2fdSKay Sievers 	__ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
7068a25a2fdSKay Sievers 	__ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
7078a25a2fdSKay Sievers 	__ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
7088a25a2fdSKay Sievers 	__ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
7098a25a2fdSKay Sievers 	__ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
7108a25a2fdSKay Sievers 	__ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
711fcdb524dSMadhavan Srinivasan #endif
712fcdb524dSMadhavan Srinivasan #ifdef HAS_PPC_PA6T
7138a25a2fdSKay Sievers 	__ATTR(hid0, 0600, show_hid0, store_hid0),
7148a25a2fdSKay Sievers 	__ATTR(hid1, 0600, show_hid1, store_hid1),
7158a25a2fdSKay Sievers 	__ATTR(hid4, 0600, show_hid4, store_hid4),
7168a25a2fdSKay Sievers 	__ATTR(hid5, 0600, show_hid5, store_hid5),
7178a25a2fdSKay Sievers 	__ATTR(ima0, 0600, show_ima0, store_ima0),
7188a25a2fdSKay Sievers 	__ATTR(ima1, 0600, show_ima1, store_ima1),
7198a25a2fdSKay Sievers 	__ATTR(ima2, 0600, show_ima2, store_ima2),
7208a25a2fdSKay Sievers 	__ATTR(ima3, 0600, show_ima3, store_ima3),
7218a25a2fdSKay Sievers 	__ATTR(ima4, 0600, show_ima4, store_ima4),
7228a25a2fdSKay Sievers 	__ATTR(ima5, 0600, show_ima5, store_ima5),
7238a25a2fdSKay Sievers 	__ATTR(ima6, 0600, show_ima6, store_ima6),
7248a25a2fdSKay Sievers 	__ATTR(ima7, 0600, show_ima7, store_ima7),
7258a25a2fdSKay Sievers 	__ATTR(ima8, 0600, show_ima8, store_ima8),
7268a25a2fdSKay Sievers 	__ATTR(ima9, 0600, show_ima9, store_ima9),
7278a25a2fdSKay Sievers 	__ATTR(imaat, 0600, show_imaat, store_imaat),
7288a25a2fdSKay Sievers 	__ATTR(btcr, 0600, show_btcr, store_btcr),
7298a25a2fdSKay Sievers 	__ATTR(pccr, 0600, show_pccr, store_pccr),
7308a25a2fdSKay Sievers 	__ATTR(rpccr, 0600, show_rpccr, store_rpccr),
7318a25a2fdSKay Sievers 	__ATTR(der, 0600, show_der, store_der),
7328a25a2fdSKay Sievers 	__ATTR(mer, 0600, show_mer, store_mer),
7338a25a2fdSKay Sievers 	__ATTR(ber, 0600, show_ber, store_ber),
7348a25a2fdSKay Sievers 	__ATTR(ier, 0600, show_ier, store_ier),
7358a25a2fdSKay Sievers 	__ATTR(sier, 0600, show_sier, store_sier),
7368a25a2fdSKay Sievers 	__ATTR(siar, 0600, show_siar, store_siar),
7378a25a2fdSKay Sievers 	__ATTR(tsr0, 0600, show_tsr0, store_tsr0),
7388a25a2fdSKay Sievers 	__ATTR(tsr1, 0600, show_tsr1, store_tsr1),
7398a25a2fdSKay Sievers 	__ATTR(tsr2, 0600, show_tsr2, store_tsr2),
7408a25a2fdSKay Sievers 	__ATTR(tsr3, 0600, show_tsr3, store_tsr3),
741fcdb524dSMadhavan Srinivasan #endif /* HAS_PPC_PA6T */
7426529c13dSOlof Johansson };
743fcdb524dSMadhavan Srinivasan #endif
7446529c13dSOlof Johansson 
745734560acSRyan Grimm #ifdef CONFIG_PPC_SVM
show_svm(struct device * dev,struct device_attribute * attr,char * buf)746734560acSRyan Grimm static ssize_t show_svm(struct device *dev, struct device_attribute *attr, char *buf)
747734560acSRyan Grimm {
748734560acSRyan Grimm 	return sprintf(buf, "%u\n", is_secure_guest());
749734560acSRyan Grimm }
750734560acSRyan Grimm static DEVICE_ATTR(svm, 0444, show_svm, NULL);
751734560acSRyan Grimm 
create_svm_file(void)752d276960dSNick Child static void __init create_svm_file(void)
753734560acSRyan Grimm {
7541b401896SGreg Kroah-Hartman 	struct device *dev_root = bus_get_dev_root(&cpu_subsys);
7551b401896SGreg Kroah-Hartman 
7561b401896SGreg Kroah-Hartman 	if (dev_root) {
7571b401896SGreg Kroah-Hartman 		device_create_file(dev_root, &dev_attr_svm);
7581b401896SGreg Kroah-Hartman 		put_device(dev_root);
7591b401896SGreg Kroah-Hartman 	}
760734560acSRyan Grimm }
761734560acSRyan Grimm #else
create_svm_file(void)762d276960dSNick Child static void __init create_svm_file(void)
763734560acSRyan Grimm {
764734560acSRyan Grimm }
765734560acSRyan Grimm #endif /* CONFIG_PPC_SVM */
766734560acSRyan Grimm 
7676909f179SGautham R. Shenoy #ifdef CONFIG_PPC_PSERIES
read_idle_purr(void * val)7686909f179SGautham R. Shenoy static void read_idle_purr(void *val)
7696909f179SGautham R. Shenoy {
7706909f179SGautham R. Shenoy 	u64 *ret = val;
7716909f179SGautham R. Shenoy 
7726909f179SGautham R. Shenoy 	*ret = read_this_idle_purr();
7736909f179SGautham R. Shenoy }
7746909f179SGautham R. Shenoy 
idle_purr_show(struct device * dev,struct device_attribute * attr,char * buf)7756909f179SGautham R. Shenoy static ssize_t idle_purr_show(struct device *dev,
7766909f179SGautham R. Shenoy 			      struct device_attribute *attr, char *buf)
7776909f179SGautham R. Shenoy {
7786909f179SGautham R. Shenoy 	struct cpu *cpu = container_of(dev, struct cpu, dev);
7796909f179SGautham R. Shenoy 	u64 val;
7806909f179SGautham R. Shenoy 
7816909f179SGautham R. Shenoy 	smp_call_function_single(cpu->dev.id, read_idle_purr, &val, 1);
7826909f179SGautham R. Shenoy 	return sprintf(buf, "%llx\n", val);
7836909f179SGautham R. Shenoy }
7846909f179SGautham R. Shenoy static DEVICE_ATTR(idle_purr, 0400, idle_purr_show, NULL);
7856909f179SGautham R. Shenoy 
create_idle_purr_file(struct device * s)7866909f179SGautham R. Shenoy static void create_idle_purr_file(struct device *s)
7876909f179SGautham R. Shenoy {
7886909f179SGautham R. Shenoy 	if (firmware_has_feature(FW_FEATURE_LPAR))
7896909f179SGautham R. Shenoy 		device_create_file(s, &dev_attr_idle_purr);
7906909f179SGautham R. Shenoy }
7916909f179SGautham R. Shenoy 
remove_idle_purr_file(struct device * s)7926909f179SGautham R. Shenoy static void remove_idle_purr_file(struct device *s)
7936909f179SGautham R. Shenoy {
7946909f179SGautham R. Shenoy 	if (firmware_has_feature(FW_FEATURE_LPAR))
7956909f179SGautham R. Shenoy 		device_remove_file(s, &dev_attr_idle_purr);
7966909f179SGautham R. Shenoy }
7976909f179SGautham R. Shenoy 
read_idle_spurr(void * val)7986909f179SGautham R. Shenoy static void read_idle_spurr(void *val)
7996909f179SGautham R. Shenoy {
8006909f179SGautham R. Shenoy 	u64 *ret = val;
8016909f179SGautham R. Shenoy 
8026909f179SGautham R. Shenoy 	*ret = read_this_idle_spurr();
8036909f179SGautham R. Shenoy }
8046909f179SGautham R. Shenoy 
idle_spurr_show(struct device * dev,struct device_attribute * attr,char * buf)8056909f179SGautham R. Shenoy static ssize_t idle_spurr_show(struct device *dev,
8066909f179SGautham R. Shenoy 			       struct device_attribute *attr, char *buf)
8076909f179SGautham R. Shenoy {
8086909f179SGautham R. Shenoy 	struct cpu *cpu = container_of(dev, struct cpu, dev);
8096909f179SGautham R. Shenoy 	u64 val;
8106909f179SGautham R. Shenoy 
8116909f179SGautham R. Shenoy 	smp_call_function_single(cpu->dev.id, read_idle_spurr, &val, 1);
8126909f179SGautham R. Shenoy 	return sprintf(buf, "%llx\n", val);
8136909f179SGautham R. Shenoy }
8146909f179SGautham R. Shenoy static DEVICE_ATTR(idle_spurr, 0400, idle_spurr_show, NULL);
8156909f179SGautham R. Shenoy 
create_idle_spurr_file(struct device * s)8166909f179SGautham R. Shenoy static void create_idle_spurr_file(struct device *s)
8176909f179SGautham R. Shenoy {
8186909f179SGautham R. Shenoy 	if (firmware_has_feature(FW_FEATURE_LPAR))
8196909f179SGautham R. Shenoy 		device_create_file(s, &dev_attr_idle_spurr);
8206909f179SGautham R. Shenoy }
8216909f179SGautham R. Shenoy 
remove_idle_spurr_file(struct device * s)8226909f179SGautham R. Shenoy static void remove_idle_spurr_file(struct device *s)
8236909f179SGautham R. Shenoy {
8246909f179SGautham R. Shenoy 	if (firmware_has_feature(FW_FEATURE_LPAR))
8256909f179SGautham R. Shenoy 		device_remove_file(s, &dev_attr_idle_spurr);
8266909f179SGautham R. Shenoy }
8276909f179SGautham R. Shenoy 
8286909f179SGautham R. Shenoy #else /* CONFIG_PPC_PSERIES */
8296909f179SGautham R. Shenoy #define create_idle_purr_file(s)
8306909f179SGautham R. Shenoy #define remove_idle_purr_file(s)
8316909f179SGautham R. Shenoy #define create_idle_spurr_file(s)
8326909f179SGautham R. Shenoy #define remove_idle_spurr_file(s)
8336909f179SGautham R. Shenoy #endif /* CONFIG_PPC_PSERIES */
8346909f179SGautham R. Shenoy 
register_cpu_online(unsigned int cpu)835977ab257SSebastian Andrzej Siewior static int register_cpu_online(unsigned int cpu)
836d3d2176aSDavid Gibson {
837d3d2176aSDavid Gibson 	struct cpu *c = &per_cpu(cpu_devices, cpu);
8388a25a2fdSKay Sievers 	struct device *s = &c->dev;
8398a25a2fdSKay Sievers 	struct device_attribute *attrs, *pmc_attrs;
8406529c13dSOlof Johansson 	int i, nattrs;
841d3d2176aSDavid Gibson 
842e76ca277STyrel Datwyler 	/* For cpus present at boot a reference was already grabbed in register_cpu() */
843e76ca277STyrel Datwyler 	if (!s->of_node)
844e76ca277STyrel Datwyler 		s->of_node = of_get_cpu_node(cpu, NULL);
845e76ca277STyrel Datwyler 
846b950bdd0SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
847f5339277SStephen Rothwell 	if (cpu_has_feature(CPU_FTR_SMT))
8488a25a2fdSKay Sievers 		device_create_file(s, &dev_attr_smt_snooze_delay);
849b950bdd0SBenjamin Herrenschmidt #endif
850d3d2176aSDavid Gibson 
851d3d2176aSDavid Gibson 	/* PMC stuff */
8526529c13dSOlof Johansson 	switch (cur_cpu_spec->pmc_type) {
853b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_IBM
8546529c13dSOlof Johansson 	case PPC_PMC_IBM:
8556529c13dSOlof Johansson 		attrs = ibm_common_attrs;
856590e1e42SJason Wang 		nattrs = ARRAY_SIZE(ibm_common_attrs);
857b950bdd0SBenjamin Herrenschmidt 		pmc_attrs = classic_pmc_attrs;
8586529c13dSOlof Johansson 		break;
859b950bdd0SBenjamin Herrenschmidt #endif /* HAS_PPC_PMC_IBM */
860b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_G4
861b950bdd0SBenjamin Herrenschmidt 	case PPC_PMC_G4:
862b950bdd0SBenjamin Herrenschmidt 		attrs = g4_common_attrs;
863590e1e42SJason Wang 		nattrs = ARRAY_SIZE(g4_common_attrs);
864b950bdd0SBenjamin Herrenschmidt 		pmc_attrs = classic_pmc_attrs;
865b950bdd0SBenjamin Herrenschmidt 		break;
866b950bdd0SBenjamin Herrenschmidt #endif /* HAS_PPC_PMC_G4 */
867fcdb524dSMadhavan Srinivasan #if defined(HAS_PPC_PMC_PA6T) || defined(HAS_PPC_PA6T)
8686529c13dSOlof Johansson 	case PPC_PMC_PA6T:
8696529c13dSOlof Johansson 		/* PA Semi starts counting at PMC0 */
8706529c13dSOlof Johansson 		attrs = pa6t_attrs;
871590e1e42SJason Wang 		nattrs = ARRAY_SIZE(pa6t_attrs);
8726529c13dSOlof Johansson 		pmc_attrs = NULL;
8736529c13dSOlof Johansson 		break;
874fcdb524dSMadhavan Srinivasan #endif
8756529c13dSOlof Johansson 	default:
8766529c13dSOlof Johansson 		attrs = NULL;
8776529c13dSOlof Johansson 		nattrs = 0;
8786529c13dSOlof Johansson 		pmc_attrs = NULL;
8796529c13dSOlof Johansson 	}
880d3d2176aSDavid Gibson 
8816529c13dSOlof Johansson 	for (i = 0; i < nattrs; i++)
8828a25a2fdSKay Sievers 		device_create_file(s, &attrs[i]);
8836529c13dSOlof Johansson 
8846529c13dSOlof Johansson 	if (pmc_attrs)
8856529c13dSOlof Johansson 		for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
8868a25a2fdSKay Sievers 			device_create_file(s, &pmc_attrs[i]);
887d3d2176aSDavid Gibson 
888b950bdd0SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
88922697da3SKajol Jain #ifdef	CONFIG_PMU_SYSFS
890d3d2176aSDavid Gibson 	if (cpu_has_feature(CPU_FTR_MMCRA))
8918a25a2fdSKay Sievers 		device_create_file(s, &dev_attr_mmcra);
892c718547eSMadhavan Srinivasan 
893c718547eSMadhavan Srinivasan 	if (cpu_has_feature(CPU_FTR_ARCH_31))
894c718547eSMadhavan Srinivasan 		device_create_file(s, &dev_attr_mmcr3);
89522697da3SKajol Jain #endif /* CONFIG_PMU_SYSFS */
896d3d2176aSDavid Gibson 
897d1211af3SMadhavan Srinivasan 	if (cpu_has_feature(CPU_FTR_PURR)) {
898d1211af3SMadhavan Srinivasan 		if (!firmware_has_feature(FW_FEATURE_LPAR))
899d1211af3SMadhavan Srinivasan 			add_write_permission_dev_attr(&dev_attr_purr);
9008a25a2fdSKay Sievers 		device_create_file(s, &dev_attr_purr);
9016909f179SGautham R. Shenoy 		create_idle_purr_file(s);
902d1211af3SMadhavan Srinivasan 	}
9034c198557SAnton Blanchard 
9046909f179SGautham R. Shenoy 	if (cpu_has_feature(CPU_FTR_SPURR)) {
9058a25a2fdSKay Sievers 		device_create_file(s, &dev_attr_spurr);
9066909f179SGautham R. Shenoy 		create_idle_spurr_file(s);
9076909f179SGautham R. Shenoy 	}
908f050982aSAnton Blanchard 
9094c198557SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_DSCR))
9108a25a2fdSKay Sievers 		device_create_file(s, &dev_attr_dscr);
911595fe914SAnanth N Mavinakayanahalli 
912595fe914SAnanth N Mavinakayanahalli 	if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
9137affca35SLinus Torvalds 		device_create_file(s, &dev_attr_pir);
914b6d34eb4SAnton Blanchard 
915c134f0d5SCyril Bur 	if (cpu_has_feature(CPU_FTR_ARCH_206) &&
916c134f0d5SCyril Bur 		!firmware_has_feature(FW_FEATURE_LPAR))
917b6d34eb4SAnton Blanchard 		device_create_file(s, &dev_attr_tscr);
918b950bdd0SBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
919124c27d3SNathan Lynch 
9203e731858SChristophe Leroy #ifdef CONFIG_PPC_E500
921a7189483SWang Dongsheng 	if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
922a7189483SWang Dongsheng 		device_create_file(s, &dev_attr_pw20_state);
923a7189483SWang Dongsheng 		device_create_file(s, &dev_attr_pw20_wait_time);
924a7189483SWang Dongsheng 
925a7189483SWang Dongsheng 		device_create_file(s, &dev_attr_altivec_idle);
926a7189483SWang Dongsheng 		device_create_file(s, &dev_attr_altivec_idle_wait_time);
927a7189483SWang Dongsheng 	}
928a7189483SWang Dongsheng #endif
92993197a36SNathan Lynch 	cacheinfo_cpu_online(cpu);
930977ab257SSebastian Andrzej Siewior 	return 0;
931d3d2176aSDavid Gibson }
932d3d2176aSDavid Gibson 
9333f2290e1SMichael Ellerman #ifdef CONFIG_HOTPLUG_CPU
unregister_cpu_online(unsigned int cpu)934977ab257SSebastian Andrzej Siewior static int unregister_cpu_online(unsigned int cpu)
935d3d2176aSDavid Gibson {
936d3d2176aSDavid Gibson 	struct cpu *c = &per_cpu(cpu_devices, cpu);
9378a25a2fdSKay Sievers 	struct device *s = &c->dev;
9388a25a2fdSKay Sievers 	struct device_attribute *attrs, *pmc_attrs;
9396529c13dSOlof Johansson 	int i, nattrs;
940d3d2176aSDavid Gibson 
941983f9101SNathan Lynch 	if (WARN_RATELIMIT(!c->hotpluggable, "cpu %d can't be offlined\n", cpu))
942983f9101SNathan Lynch 		return -EBUSY;
943d3d2176aSDavid Gibson 
944a1e0eb10SMilton Miller #ifdef CONFIG_PPC64
945f5339277SStephen Rothwell 	if (cpu_has_feature(CPU_FTR_SMT))
9468a25a2fdSKay Sievers 		device_remove_file(s, &dev_attr_smt_snooze_delay);
947a1e0eb10SMilton Miller #endif
948d3d2176aSDavid Gibson 
949d3d2176aSDavid Gibson 	/* PMC stuff */
9506529c13dSOlof Johansson 	switch (cur_cpu_spec->pmc_type) {
951b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_IBM
9526529c13dSOlof Johansson 	case PPC_PMC_IBM:
9536529c13dSOlof Johansson 		attrs = ibm_common_attrs;
954590e1e42SJason Wang 		nattrs = ARRAY_SIZE(ibm_common_attrs);
955b950bdd0SBenjamin Herrenschmidt 		pmc_attrs = classic_pmc_attrs;
9566529c13dSOlof Johansson 		break;
957b950bdd0SBenjamin Herrenschmidt #endif /* HAS_PPC_PMC_IBM */
958b950bdd0SBenjamin Herrenschmidt #ifdef HAS_PPC_PMC_G4
959b950bdd0SBenjamin Herrenschmidt 	case PPC_PMC_G4:
960b950bdd0SBenjamin Herrenschmidt 		attrs = g4_common_attrs;
961590e1e42SJason Wang 		nattrs = ARRAY_SIZE(g4_common_attrs);
962b950bdd0SBenjamin Herrenschmidt 		pmc_attrs = classic_pmc_attrs;
963b950bdd0SBenjamin Herrenschmidt 		break;
964b950bdd0SBenjamin Herrenschmidt #endif /* HAS_PPC_PMC_G4 */
965fcdb524dSMadhavan Srinivasan #if defined(HAS_PPC_PMC_PA6T) || defined(HAS_PPC_PA6T)
9666529c13dSOlof Johansson 	case PPC_PMC_PA6T:
9676529c13dSOlof Johansson 		/* PA Semi starts counting at PMC0 */
9686529c13dSOlof Johansson 		attrs = pa6t_attrs;
969590e1e42SJason Wang 		nattrs = ARRAY_SIZE(pa6t_attrs);
9706529c13dSOlof Johansson 		pmc_attrs = NULL;
9716529c13dSOlof Johansson 		break;
972fcdb524dSMadhavan Srinivasan #endif
9736529c13dSOlof Johansson 	default:
9746529c13dSOlof Johansson 		attrs = NULL;
9756529c13dSOlof Johansson 		nattrs = 0;
9766529c13dSOlof Johansson 		pmc_attrs = NULL;
9776529c13dSOlof Johansson 	}
978d3d2176aSDavid Gibson 
9796529c13dSOlof Johansson 	for (i = 0; i < nattrs; i++)
9808a25a2fdSKay Sievers 		device_remove_file(s, &attrs[i]);
9816529c13dSOlof Johansson 
9826529c13dSOlof Johansson 	if (pmc_attrs)
9836529c13dSOlof Johansson 		for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
9848a25a2fdSKay Sievers 			device_remove_file(s, &pmc_attrs[i]);
985d3d2176aSDavid Gibson 
986b950bdd0SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
98722697da3SKajol Jain #ifdef CONFIG_PMU_SYSFS
988d3d2176aSDavid Gibson 	if (cpu_has_feature(CPU_FTR_MMCRA))
9898a25a2fdSKay Sievers 		device_remove_file(s, &dev_attr_mmcra);
990c718547eSMadhavan Srinivasan 
991c718547eSMadhavan Srinivasan 	if (cpu_has_feature(CPU_FTR_ARCH_31))
992c718547eSMadhavan Srinivasan 		device_remove_file(s, &dev_attr_mmcr3);
99322697da3SKajol Jain #endif /* CONFIG_PMU_SYSFS */
994d3d2176aSDavid Gibson 
9956909f179SGautham R. Shenoy 	if (cpu_has_feature(CPU_FTR_PURR)) {
9968a25a2fdSKay Sievers 		device_remove_file(s, &dev_attr_purr);
9976909f179SGautham R. Shenoy 		remove_idle_purr_file(s);
9986909f179SGautham R. Shenoy 	}
9994c198557SAnton Blanchard 
10006909f179SGautham R. Shenoy 	if (cpu_has_feature(CPU_FTR_SPURR)) {
10018a25a2fdSKay Sievers 		device_remove_file(s, &dev_attr_spurr);
10026909f179SGautham R. Shenoy 		remove_idle_spurr_file(s);
10036909f179SGautham R. Shenoy 	}
1004f050982aSAnton Blanchard 
10054c198557SAnton Blanchard 	if (cpu_has_feature(CPU_FTR_DSCR))
10068a25a2fdSKay Sievers 		device_remove_file(s, &dev_attr_dscr);
1007595fe914SAnanth N Mavinakayanahalli 
1008595fe914SAnanth N Mavinakayanahalli 	if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
10097affca35SLinus Torvalds 		device_remove_file(s, &dev_attr_pir);
1010b6d34eb4SAnton Blanchard 
1011c134f0d5SCyril Bur 	if (cpu_has_feature(CPU_FTR_ARCH_206) &&
1012c134f0d5SCyril Bur 		!firmware_has_feature(FW_FEATURE_LPAR))
1013b6d34eb4SAnton Blanchard 		device_remove_file(s, &dev_attr_tscr);
1014b950bdd0SBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
1015124c27d3SNathan Lynch 
10163e731858SChristophe Leroy #ifdef CONFIG_PPC_E500
1017a7189483SWang Dongsheng 	if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
1018a7189483SWang Dongsheng 		device_remove_file(s, &dev_attr_pw20_state);
1019a7189483SWang Dongsheng 		device_remove_file(s, &dev_attr_pw20_wait_time);
1020a7189483SWang Dongsheng 
1021a7189483SWang Dongsheng 		device_remove_file(s, &dev_attr_altivec_idle);
1022a7189483SWang Dongsheng 		device_remove_file(s, &dev_attr_altivec_idle_wait_time);
1023a7189483SWang Dongsheng 	}
1024a7189483SWang Dongsheng #endif
102593197a36SNathan Lynch 	cacheinfo_cpu_offline(cpu);
1026e76ca277STyrel Datwyler 	of_node_put(s->of_node);
1027e76ca277STyrel Datwyler 	s->of_node = NULL;
1028977ab257SSebastian Andrzej Siewior 	return 0;
1029d3d2176aSDavid Gibson }
10303f2290e1SMichael Ellerman #else /* !CONFIG_HOTPLUG_CPU */
10313f2290e1SMichael Ellerman #define unregister_cpu_online NULL
10323f2290e1SMichael Ellerman #endif
103312633e80SNathan Fontenot 
103412633e80SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
arch_cpu_probe(const char * buf,size_t count)103512633e80SNathan Fontenot ssize_t arch_cpu_probe(const char *buf, size_t count)
103612633e80SNathan Fontenot {
103712633e80SNathan Fontenot 	if (ppc_md.cpu_probe)
103812633e80SNathan Fontenot 		return ppc_md.cpu_probe(buf, count);
103912633e80SNathan Fontenot 
104012633e80SNathan Fontenot 	return -EINVAL;
104112633e80SNathan Fontenot }
104212633e80SNathan Fontenot 
arch_cpu_release(const char * buf,size_t count)104312633e80SNathan Fontenot ssize_t arch_cpu_release(const char *buf, size_t count)
104412633e80SNathan Fontenot {
104512633e80SNathan Fontenot 	if (ppc_md.cpu_release)
104612633e80SNathan Fontenot 		return ppc_md.cpu_release(buf, count);
104712633e80SNathan Fontenot 
104812633e80SNathan Fontenot 	return -EINVAL;
104912633e80SNathan Fontenot }
105012633e80SNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
105112633e80SNathan Fontenot 
10520344c6c5SChristian Krafft static DEFINE_MUTEX(cpu_mutex);
10530344c6c5SChristian Krafft 
cpu_add_dev_attr(struct device_attribute * attr)10548a25a2fdSKay Sievers int cpu_add_dev_attr(struct device_attribute *attr)
10550344c6c5SChristian Krafft {
10560344c6c5SChristian Krafft 	int cpu;
10570344c6c5SChristian Krafft 
10580344c6c5SChristian Krafft 	mutex_lock(&cpu_mutex);
10590344c6c5SChristian Krafft 
10600344c6c5SChristian Krafft 	for_each_possible_cpu(cpu) {
10618a25a2fdSKay Sievers 		device_create_file(get_cpu_device(cpu), attr);
10620344c6c5SChristian Krafft 	}
10630344c6c5SChristian Krafft 
10640344c6c5SChristian Krafft 	mutex_unlock(&cpu_mutex);
10650344c6c5SChristian Krafft 	return 0;
10660344c6c5SChristian Krafft }
10678a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(cpu_add_dev_attr);
10680344c6c5SChristian Krafft 
cpu_add_dev_attr_group(struct attribute_group * attrs)10698a25a2fdSKay Sievers int cpu_add_dev_attr_group(struct attribute_group *attrs)
10700344c6c5SChristian Krafft {
10710344c6c5SChristian Krafft 	int cpu;
10728a25a2fdSKay Sievers 	struct device *dev;
10736bcc4c01SOlof Johansson 	int ret;
10740344c6c5SChristian Krafft 
10750344c6c5SChristian Krafft 	mutex_lock(&cpu_mutex);
10760344c6c5SChristian Krafft 
10770344c6c5SChristian Krafft 	for_each_possible_cpu(cpu) {
10788a25a2fdSKay Sievers 		dev = get_cpu_device(cpu);
10798a25a2fdSKay Sievers 		ret = sysfs_create_group(&dev->kobj, attrs);
10806bcc4c01SOlof Johansson 		WARN_ON(ret != 0);
10810344c6c5SChristian Krafft 	}
10820344c6c5SChristian Krafft 
10830344c6c5SChristian Krafft 	mutex_unlock(&cpu_mutex);
10840344c6c5SChristian Krafft 	return 0;
10850344c6c5SChristian Krafft }
10868a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(cpu_add_dev_attr_group);
10870344c6c5SChristian Krafft 
10880344c6c5SChristian Krafft 
cpu_remove_dev_attr(struct device_attribute * attr)10898a25a2fdSKay Sievers void cpu_remove_dev_attr(struct device_attribute *attr)
10900344c6c5SChristian Krafft {
10910344c6c5SChristian Krafft 	int cpu;
10920344c6c5SChristian Krafft 
10930344c6c5SChristian Krafft 	mutex_lock(&cpu_mutex);
10940344c6c5SChristian Krafft 
10950344c6c5SChristian Krafft 	for_each_possible_cpu(cpu) {
10968a25a2fdSKay Sievers 		device_remove_file(get_cpu_device(cpu), attr);
10970344c6c5SChristian Krafft 	}
10980344c6c5SChristian Krafft 
10990344c6c5SChristian Krafft 	mutex_unlock(&cpu_mutex);
11000344c6c5SChristian Krafft }
11018a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(cpu_remove_dev_attr);
11020344c6c5SChristian Krafft 
cpu_remove_dev_attr_group(struct attribute_group * attrs)11038a25a2fdSKay Sievers void cpu_remove_dev_attr_group(struct attribute_group *attrs)
11040344c6c5SChristian Krafft {
11050344c6c5SChristian Krafft 	int cpu;
11068a25a2fdSKay Sievers 	struct device *dev;
11070344c6c5SChristian Krafft 
11080344c6c5SChristian Krafft 	mutex_lock(&cpu_mutex);
11090344c6c5SChristian Krafft 
11100344c6c5SChristian Krafft 	for_each_possible_cpu(cpu) {
11118a25a2fdSKay Sievers 		dev = get_cpu_device(cpu);
11128a25a2fdSKay Sievers 		sysfs_remove_group(&dev->kobj, attrs);
11130344c6c5SChristian Krafft 	}
11140344c6c5SChristian Krafft 
11150344c6c5SChristian Krafft 	mutex_unlock(&cpu_mutex);
11160344c6c5SChristian Krafft }
11178a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(cpu_remove_dev_attr_group);
11180344c6c5SChristian Krafft 
11190344c6c5SChristian Krafft 
1120d3d2176aSDavid Gibson /* NUMA stuff */
1121d3d2176aSDavid Gibson 
1122d3d2176aSDavid Gibson #ifdef CONFIG_NUMA
sysfs_add_device_to_node(struct device * dev,int nid)11238a25a2fdSKay Sievers int sysfs_add_device_to_node(struct device *dev, int nid)
1124953039c8SJeremy Kerr {
11258732794bSWen Congyang 	struct node *node = node_devices[nid];
112610fbcf4cSKay Sievers 	return sysfs_create_link(&node->dev.kobj, &dev->kobj,
1127953039c8SJeremy Kerr 			kobject_name(&dev->kobj));
1128953039c8SJeremy Kerr }
112912654f77SJohannes Berg EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
1130953039c8SJeremy Kerr 
sysfs_remove_device_from_node(struct device * dev,int nid)11318a25a2fdSKay Sievers void sysfs_remove_device_from_node(struct device *dev, int nid)
1132953039c8SJeremy Kerr {
11338732794bSWen Congyang 	struct node *node = node_devices[nid];
113410fbcf4cSKay Sievers 	sysfs_remove_link(&node->dev.kobj, kobject_name(&dev->kobj));
1135953039c8SJeremy Kerr }
113612654f77SJohannes Berg EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
1137d3d2176aSDavid Gibson #endif
1138d3d2176aSDavid Gibson 
1139d3d2176aSDavid Gibson /* Only valid if CPU is present. */
show_physical_id(struct device * dev,struct device_attribute * attr,char * buf)11408a25a2fdSKay Sievers static ssize_t show_physical_id(struct device *dev,
11418a25a2fdSKay Sievers 				struct device_attribute *attr, char *buf)
1142d3d2176aSDavid Gibson {
11438a25a2fdSKay Sievers 	struct cpu *cpu = container_of(dev, struct cpu, dev);
1144d3d2176aSDavid Gibson 
11458a25a2fdSKay Sievers 	return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->dev.id));
1146d3d2176aSDavid Gibson }
11478a25a2fdSKay Sievers static DEVICE_ATTR(physical_id, 0444, show_physical_id, NULL);
1148d3d2176aSDavid Gibson 
topology_init(void)1149d3d2176aSDavid Gibson static int __init topology_init(void)
1150d3d2176aSDavid Gibson {
1151977ab257SSebastian Andrzej Siewior 	int cpu, r;
1152d3d2176aSDavid Gibson 
11530e551954SKAMEZAWA Hiroyuki 	for_each_possible_cpu(cpu) {
1154d3d2176aSDavid Gibson 		struct cpu *c = &per_cpu(cpu_devices, cpu);
1155d3d2176aSDavid Gibson 
115639f87561SMichael Ellerman #ifdef CONFIG_HOTPLUG_CPU
1157d3d2176aSDavid Gibson 		/*
1158d3d2176aSDavid Gibson 		 * For now, we just see if the system supports making
1159d3d2176aSDavid Gibson 		 * the RTAS calls for CPU hotplug.  But, there may be a
1160d3d2176aSDavid Gibson 		 * more comprehensive way to do this for an individual
1161d3d2176aSDavid Gibson 		 * CPU.  For instance, the boot cpu might never be valid
1162d3d2176aSDavid Gibson 		 * for hotplugging.
1163d3d2176aSDavid Gibson 		 */
116482414615SChristophe Leroy 		if (smp_ops && smp_ops->cpu_offline_self)
116572486f1fSSiddha, Suresh B 			c->hotpluggable = 1;
116639f87561SMichael Ellerman #endif
1167d3d2176aSDavid Gibson 
116872486f1fSSiddha, Suresh B 		if (cpu_online(cpu) || c->hotpluggable) {
116976b67ed9SKAMEZAWA Hiroyuki 			register_cpu(c, cpu);
1170d3d2176aSDavid Gibson 
11718a25a2fdSKay Sievers 			device_create_file(&c->dev, &dev_attr_physical_id);
1172d3d2176aSDavid Gibson 		}
1173d3d2176aSDavid Gibson 	}
1174977ab257SSebastian Andrzej Siewior 	r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/topology:online",
1175977ab257SSebastian Andrzej Siewior 			      register_cpu_online, unregister_cpu_online);
1176977ab257SSebastian Andrzej Siewior 	WARN_ON(r < 0);
1177efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1178efcac658SAlexey Kardashevskiy 	sysfs_create_dscr_default();
1179efcac658SAlexey Kardashevskiy #endif /* CONFIG_PPC64 */
1180d3d2176aSDavid Gibson 
1181734560acSRyan Grimm 	create_svm_file();
1182734560acSRyan Grimm 
1183d3d2176aSDavid Gibson 	return 0;
1184d3d2176aSDavid Gibson }
1185e9e77ce8SKevin Corry subsys_initcall(topology_init);
1186