xref: /openbmc/linux/arch/powerpc/platforms/pseries/pseries_energy.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25742bd85SVaidyanathan Srinivasan /*
35742bd85SVaidyanathan Srinivasan  * POWER platform energy management driver
45742bd85SVaidyanathan Srinivasan  * Copyright (C) 2010 IBM Corporation
55742bd85SVaidyanathan Srinivasan  *
65742bd85SVaidyanathan Srinivasan  * This pseries platform device driver provides access to
75742bd85SVaidyanathan Srinivasan  * platform energy management capabilities.
85742bd85SVaidyanathan Srinivasan  */
95742bd85SVaidyanathan Srinivasan 
105742bd85SVaidyanathan Srinivasan #include <linux/module.h>
115742bd85SVaidyanathan Srinivasan #include <linux/types.h>
125742bd85SVaidyanathan Srinivasan #include <linux/errno.h>
135742bd85SVaidyanathan Srinivasan #include <linux/init.h>
145742bd85SVaidyanathan Srinivasan #include <linux/seq_file.h>
158a25a2fdSKay Sievers #include <linux/device.h>
165742bd85SVaidyanathan Srinivasan #include <linux/cpu.h>
175742bd85SVaidyanathan Srinivasan #include <linux/of.h>
185742bd85SVaidyanathan Srinivasan #include <asm/cputhreads.h>
195742bd85SVaidyanathan Srinivasan #include <asm/page.h>
205742bd85SVaidyanathan Srinivasan #include <asm/hvcall.h>
210388c79cSMichael Neuling #include <asm/firmware.h>
22e83636acSMichael Bringmann #include <asm/prom.h>
235742bd85SVaidyanathan Srinivasan 
245742bd85SVaidyanathan Srinivasan 
255742bd85SVaidyanathan Srinivasan #define MODULE_VERS "1.0"
265742bd85SVaidyanathan Srinivasan #define MODULE_NAME "pseries_energy"
275742bd85SVaidyanathan Srinivasan 
285742bd85SVaidyanathan Srinivasan /* Driver flags */
295742bd85SVaidyanathan Srinivasan 
305742bd85SVaidyanathan Srinivasan static int sysfs_entries;
315742bd85SVaidyanathan Srinivasan 
325742bd85SVaidyanathan Srinivasan /* Helper routines */
335742bd85SVaidyanathan Srinivasan 
345742bd85SVaidyanathan Srinivasan /* Helper Routines to convert between drc_index to cpu numbers */
355742bd85SVaidyanathan Srinivasan 
cpu_to_drc_index(int cpu)365742bd85SVaidyanathan Srinivasan static u32 cpu_to_drc_index(int cpu)
375742bd85SVaidyanathan Srinivasan {
385742bd85SVaidyanathan Srinivasan 	struct device_node *dn = NULL;
39775fa495STyrel Datwyler 	struct property *info;
40e83636acSMichael Bringmann 	int thread_index;
415742bd85SVaidyanathan Srinivasan 	int rc = 1;
425742bd85SVaidyanathan Srinivasan 	u32 ret = 0;
435742bd85SVaidyanathan Srinivasan 
445742bd85SVaidyanathan Srinivasan 	dn = of_find_node_by_path("/cpus");
455742bd85SVaidyanathan Srinivasan 	if (dn == NULL)
465742bd85SVaidyanathan Srinivasan 		goto err;
47e83636acSMichael Bringmann 
48e83636acSMichael Bringmann 	/* Convert logical cpu number to core number */
49e83636acSMichael Bringmann 	thread_index = cpu_core_index_of_thread(cpu);
50e83636acSMichael Bringmann 
51775fa495STyrel Datwyler 	info = of_find_property(dn, "ibm,drc-info", NULL);
52775fa495STyrel Datwyler 	if (info) {
53e83636acSMichael Bringmann 		struct of_drc_info drc;
54e83636acSMichael Bringmann 		int j;
55e83636acSMichael Bringmann 		u32 num_set_entries;
56e83636acSMichael Bringmann 		const __be32 *value;
57e83636acSMichael Bringmann 
58e83636acSMichael Bringmann 		value = of_prop_next_u32(info, NULL, &num_set_entries);
59e83636acSMichael Bringmann 		if (!value)
60e83636acSMichael Bringmann 			goto err_of_node_put;
61775fa495STyrel Datwyler 		else
62775fa495STyrel Datwyler 			value++;
63e83636acSMichael Bringmann 
64e83636acSMichael Bringmann 		for (j = 0; j < num_set_entries; j++) {
65e83636acSMichael Bringmann 
66e83636acSMichael Bringmann 			of_read_drc_info_cell(&info, &value, &drc);
67e83636acSMichael Bringmann 			if (strncmp(drc.drc_type, "CPU", 3))
68e83636acSMichael Bringmann 				goto err;
69e83636acSMichael Bringmann 
70e83636acSMichael Bringmann 			if (thread_index < drc.last_drc_index)
71e83636acSMichael Bringmann 				break;
72e83636acSMichael Bringmann 		}
73e83636acSMichael Bringmann 
74e83636acSMichael Bringmann 		ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
75e83636acSMichael Bringmann 	} else {
76ce9afe08SGautham R. Shenoy 		u32 nr_drc_indexes, thread_drc_index;
77e83636acSMichael Bringmann 
785742bd85SVaidyanathan Srinivasan 		/*
79ce9afe08SGautham R. Shenoy 		 * The first element of ibm,drc-indexes array is the
80ce9afe08SGautham R. Shenoy 		 * number of drc_indexes returned in the list.  Hence
81ce9afe08SGautham R. Shenoy 		 * thread_index+1 will get the drc_index corresponding
82ce9afe08SGautham R. Shenoy 		 * to core number thread_index.
835742bd85SVaidyanathan Srinivasan 		 */
84ce9afe08SGautham R. Shenoy 		rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
85ce9afe08SGautham R. Shenoy 						0, &nr_drc_indexes);
86ce9afe08SGautham R. Shenoy 		if (rc)
87ce9afe08SGautham R. Shenoy 			goto err_of_node_put;
88ce9afe08SGautham R. Shenoy 
89ce9afe08SGautham R. Shenoy 		WARN_ON_ONCE(thread_index > nr_drc_indexes);
90ce9afe08SGautham R. Shenoy 		rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
91ce9afe08SGautham R. Shenoy 						thread_index + 1,
92ce9afe08SGautham R. Shenoy 						&thread_drc_index);
93ce9afe08SGautham R. Shenoy 		if (rc)
94ce9afe08SGautham R. Shenoy 			goto err_of_node_put;
95ce9afe08SGautham R. Shenoy 
96ce9afe08SGautham R. Shenoy 		ret = thread_drc_index;
97e83636acSMichael Bringmann 	}
98e83636acSMichael Bringmann 
995742bd85SVaidyanathan Srinivasan 	rc = 0;
1005742bd85SVaidyanathan Srinivasan 
1015742bd85SVaidyanathan Srinivasan err_of_node_put:
1025742bd85SVaidyanathan Srinivasan 	of_node_put(dn);
1035742bd85SVaidyanathan Srinivasan err:
1045742bd85SVaidyanathan Srinivasan 	if (rc)
1055742bd85SVaidyanathan Srinivasan 		printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
1065742bd85SVaidyanathan Srinivasan 	return ret;
1075742bd85SVaidyanathan Srinivasan }
1085742bd85SVaidyanathan Srinivasan 
drc_index_to_cpu(u32 drc_index)1095742bd85SVaidyanathan Srinivasan static int drc_index_to_cpu(u32 drc_index)
1105742bd85SVaidyanathan Srinivasan {
1115742bd85SVaidyanathan Srinivasan 	struct device_node *dn = NULL;
112775fa495STyrel Datwyler 	struct property *info;
1135742bd85SVaidyanathan Srinivasan 	const int *indexes;
114e83636acSMichael Bringmann 	int thread_index = 0, cpu = 0;
1155742bd85SVaidyanathan Srinivasan 	int rc = 1;
1165742bd85SVaidyanathan Srinivasan 
1175742bd85SVaidyanathan Srinivasan 	dn = of_find_node_by_path("/cpus");
1185742bd85SVaidyanathan Srinivasan 	if (dn == NULL)
1195742bd85SVaidyanathan Srinivasan 		goto err;
120775fa495STyrel Datwyler 	info = of_find_property(dn, "ibm,drc-info", NULL);
121775fa495STyrel Datwyler 	if (info) {
122e83636acSMichael Bringmann 		struct of_drc_info drc;
123e83636acSMichael Bringmann 		int j;
124e83636acSMichael Bringmann 		u32 num_set_entries;
125e83636acSMichael Bringmann 		const __be32 *value;
126e83636acSMichael Bringmann 
127e83636acSMichael Bringmann 		value = of_prop_next_u32(info, NULL, &num_set_entries);
128e83636acSMichael Bringmann 		if (!value)
129e83636acSMichael Bringmann 			goto err_of_node_put;
130775fa495STyrel Datwyler 		else
131775fa495STyrel Datwyler 			value++;
132e83636acSMichael Bringmann 
133e83636acSMichael Bringmann 		for (j = 0; j < num_set_entries; j++) {
134e83636acSMichael Bringmann 
135e83636acSMichael Bringmann 			of_read_drc_info_cell(&info, &value, &drc);
136e83636acSMichael Bringmann 			if (strncmp(drc.drc_type, "CPU", 3))
137e83636acSMichael Bringmann 				goto err;
138e83636acSMichael Bringmann 
139e83636acSMichael Bringmann 			if (drc_index > drc.last_drc_index) {
140e83636acSMichael Bringmann 				cpu += drc.num_sequential_elems;
141e83636acSMichael Bringmann 				continue;
142e83636acSMichael Bringmann 			}
143e83636acSMichael Bringmann 			cpu += ((drc_index - drc.drc_index_start) /
144e83636acSMichael Bringmann 				drc.sequential_inc);
145e83636acSMichael Bringmann 
146e83636acSMichael Bringmann 			thread_index = cpu_first_thread_of_core(cpu);
147e83636acSMichael Bringmann 			rc = 0;
148e83636acSMichael Bringmann 			break;
149e83636acSMichael Bringmann 		}
150e83636acSMichael Bringmann 	} else {
151e83636acSMichael Bringmann 		unsigned long int i;
152e83636acSMichael Bringmann 
1535742bd85SVaidyanathan Srinivasan 		indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
1545742bd85SVaidyanathan Srinivasan 		if (indexes == NULL)
1555742bd85SVaidyanathan Srinivasan 			goto err_of_node_put;
1565742bd85SVaidyanathan Srinivasan 		/*
1575742bd85SVaidyanathan Srinivasan 		 * First element in the array is the number of drc_indexes
1585742bd85SVaidyanathan Srinivasan 		 * returned.  Search through the list to find the matching
1595742bd85SVaidyanathan Srinivasan 		 * drc_index and get the core number
1605742bd85SVaidyanathan Srinivasan 		 */
1615742bd85SVaidyanathan Srinivasan 		for (i = 0; i < indexes[0]; i++) {
1625742bd85SVaidyanathan Srinivasan 			if (indexes[i + 1] == drc_index)
1635742bd85SVaidyanathan Srinivasan 				break;
1645742bd85SVaidyanathan Srinivasan 		}
1655742bd85SVaidyanathan Srinivasan 		/* Convert core number to logical cpu number */
166e83636acSMichael Bringmann 		thread_index = cpu_first_thread_of_core(i);
1675742bd85SVaidyanathan Srinivasan 		rc = 0;
168e83636acSMichael Bringmann 	}
1695742bd85SVaidyanathan Srinivasan 
1705742bd85SVaidyanathan Srinivasan err_of_node_put:
1715742bd85SVaidyanathan Srinivasan 	of_node_put(dn);
1725742bd85SVaidyanathan Srinivasan err:
1735742bd85SVaidyanathan Srinivasan 	if (rc)
1745742bd85SVaidyanathan Srinivasan 		printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
175e83636acSMichael Bringmann 	return thread_index;
1765742bd85SVaidyanathan Srinivasan }
1775742bd85SVaidyanathan Srinivasan 
1785742bd85SVaidyanathan Srinivasan /*
1795742bd85SVaidyanathan Srinivasan  * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
1805742bd85SVaidyanathan Srinivasan  * preferred logical cpus to activate or deactivate for optimized
1815742bd85SVaidyanathan Srinivasan  * energy consumption.
1825742bd85SVaidyanathan Srinivasan  */
1835742bd85SVaidyanathan Srinivasan 
184b0d436c7SAnton Blanchard #define FLAGS_MODE1	0x004E200000080E01UL
185b0d436c7SAnton Blanchard #define FLAGS_MODE2	0x004E200000080401UL
1865742bd85SVaidyanathan Srinivasan #define FLAGS_ACTIVATE  0x100
1875742bd85SVaidyanathan Srinivasan 
get_best_energy_list(char * page,int activate)1885742bd85SVaidyanathan Srinivasan static ssize_t get_best_energy_list(char *page, int activate)
1895742bd85SVaidyanathan Srinivasan {
1905742bd85SVaidyanathan Srinivasan 	int rc, cnt, i, cpu;
1915742bd85SVaidyanathan Srinivasan 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1925742bd85SVaidyanathan Srinivasan 	unsigned long flags = 0;
1935742bd85SVaidyanathan Srinivasan 	u32 *buf_page;
1945742bd85SVaidyanathan Srinivasan 	char *s = page;
1955742bd85SVaidyanathan Srinivasan 
1965742bd85SVaidyanathan Srinivasan 	buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
1975742bd85SVaidyanathan Srinivasan 	if (!buf_page)
1985742bd85SVaidyanathan Srinivasan 		return -ENOMEM;
1995742bd85SVaidyanathan Srinivasan 
2005742bd85SVaidyanathan Srinivasan 	flags = FLAGS_MODE1;
2015742bd85SVaidyanathan Srinivasan 	if (activate)
2025742bd85SVaidyanathan Srinivasan 		flags |= FLAGS_ACTIVATE;
2035742bd85SVaidyanathan Srinivasan 
2045742bd85SVaidyanathan Srinivasan 	rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
2055742bd85SVaidyanathan Srinivasan 				0, 0, 0, 0, 0, 0);
2065742bd85SVaidyanathan Srinivasan 	if (rc != H_SUCCESS) {
2075742bd85SVaidyanathan Srinivasan 		free_page((unsigned long) buf_page);
2085742bd85SVaidyanathan Srinivasan 		return -EINVAL;
2095742bd85SVaidyanathan Srinivasan 	}
2105742bd85SVaidyanathan Srinivasan 
2115742bd85SVaidyanathan Srinivasan 	cnt = retbuf[0];
2125742bd85SVaidyanathan Srinivasan 	for (i = 0; i < cnt; i++) {
2135742bd85SVaidyanathan Srinivasan 		cpu = drc_index_to_cpu(buf_page[2*i+1]);
2145742bd85SVaidyanathan Srinivasan 		if ((cpu_online(cpu) && !activate) ||
2155742bd85SVaidyanathan Srinivasan 		    (!cpu_online(cpu) && activate))
2165742bd85SVaidyanathan Srinivasan 			s += sprintf(s, "%d,", cpu);
2175742bd85SVaidyanathan Srinivasan 	}
2185742bd85SVaidyanathan Srinivasan 	if (s > page) { /* Something to show */
2195742bd85SVaidyanathan Srinivasan 		s--; /* Suppress last comma */
2205742bd85SVaidyanathan Srinivasan 		s += sprintf(s, "\n");
2215742bd85SVaidyanathan Srinivasan 	}
2225742bd85SVaidyanathan Srinivasan 
2235742bd85SVaidyanathan Srinivasan 	free_page((unsigned long) buf_page);
2245742bd85SVaidyanathan Srinivasan 	return s-page;
2255742bd85SVaidyanathan Srinivasan }
2265742bd85SVaidyanathan Srinivasan 
get_best_energy_data(struct device * dev,char * page,int activate)2278a25a2fdSKay Sievers static ssize_t get_best_energy_data(struct device *dev,
2285742bd85SVaidyanathan Srinivasan 					char *page, int activate)
2295742bd85SVaidyanathan Srinivasan {
2305742bd85SVaidyanathan Srinivasan 	int rc;
2315742bd85SVaidyanathan Srinivasan 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
2325742bd85SVaidyanathan Srinivasan 	unsigned long flags = 0;
2335742bd85SVaidyanathan Srinivasan 
2345742bd85SVaidyanathan Srinivasan 	flags = FLAGS_MODE2;
2355742bd85SVaidyanathan Srinivasan 	if (activate)
2365742bd85SVaidyanathan Srinivasan 		flags |= FLAGS_ACTIVATE;
2375742bd85SVaidyanathan Srinivasan 
2385742bd85SVaidyanathan Srinivasan 	rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
2395742bd85SVaidyanathan Srinivasan 				cpu_to_drc_index(dev->id),
2405742bd85SVaidyanathan Srinivasan 				0, 0, 0, 0, 0, 0, 0);
2415742bd85SVaidyanathan Srinivasan 
2425742bd85SVaidyanathan Srinivasan 	if (rc != H_SUCCESS)
2435742bd85SVaidyanathan Srinivasan 		return -EINVAL;
2445742bd85SVaidyanathan Srinivasan 
2455742bd85SVaidyanathan Srinivasan 	return sprintf(page, "%lu\n", retbuf[1] >> 32);
2465742bd85SVaidyanathan Srinivasan }
2475742bd85SVaidyanathan Srinivasan 
2485742bd85SVaidyanathan Srinivasan /* Wrapper functions */
2495742bd85SVaidyanathan Srinivasan 
cpu_activate_hint_list_show(struct device * dev,struct device_attribute * attr,char * page)2508a25a2fdSKay Sievers static ssize_t cpu_activate_hint_list_show(struct device *dev,
2518a25a2fdSKay Sievers 			struct device_attribute *attr, char *page)
2525742bd85SVaidyanathan Srinivasan {
2535742bd85SVaidyanathan Srinivasan 	return get_best_energy_list(page, 1);
2545742bd85SVaidyanathan Srinivasan }
2555742bd85SVaidyanathan Srinivasan 
cpu_deactivate_hint_list_show(struct device * dev,struct device_attribute * attr,char * page)2568a25a2fdSKay Sievers static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
2578a25a2fdSKay Sievers 			struct device_attribute *attr, char *page)
2585742bd85SVaidyanathan Srinivasan {
2595742bd85SVaidyanathan Srinivasan 	return get_best_energy_list(page, 0);
2605742bd85SVaidyanathan Srinivasan }
2615742bd85SVaidyanathan Srinivasan 
percpu_activate_hint_show(struct device * dev,struct device_attribute * attr,char * page)2628a25a2fdSKay Sievers static ssize_t percpu_activate_hint_show(struct device *dev,
2638a25a2fdSKay Sievers 			struct device_attribute *attr, char *page)
2645742bd85SVaidyanathan Srinivasan {
2655742bd85SVaidyanathan Srinivasan 	return get_best_energy_data(dev, page, 1);
2665742bd85SVaidyanathan Srinivasan }
2675742bd85SVaidyanathan Srinivasan 
percpu_deactivate_hint_show(struct device * dev,struct device_attribute * attr,char * page)2688a25a2fdSKay Sievers static ssize_t percpu_deactivate_hint_show(struct device *dev,
2698a25a2fdSKay Sievers 			struct device_attribute *attr, char *page)
2705742bd85SVaidyanathan Srinivasan {
2715742bd85SVaidyanathan Srinivasan 	return get_best_energy_data(dev, page, 0);
2725742bd85SVaidyanathan Srinivasan }
2735742bd85SVaidyanathan Srinivasan 
2745742bd85SVaidyanathan Srinivasan /*
2755742bd85SVaidyanathan Srinivasan  * Create sysfs interface:
2765742bd85SVaidyanathan Srinivasan  * /sys/devices/system/cpu/pseries_activate_hint_list
2775742bd85SVaidyanathan Srinivasan  * /sys/devices/system/cpu/pseries_deactivate_hint_list
2785742bd85SVaidyanathan Srinivasan  *	Comma separated list of cpus to activate or deactivate
2795742bd85SVaidyanathan Srinivasan  * /sys/devices/system/cpu/cpuN/pseries_activate_hint
2805742bd85SVaidyanathan Srinivasan  * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
2815742bd85SVaidyanathan Srinivasan  *	Per-cpu value of the hint
2825742bd85SVaidyanathan Srinivasan  */
2835742bd85SVaidyanathan Srinivasan 
28434852ed5SDaniel Axtens static struct device_attribute attr_cpu_activate_hint_list =
2858a25a2fdSKay Sievers 		__ATTR(pseries_activate_hint_list, 0444,
2865742bd85SVaidyanathan Srinivasan 		cpu_activate_hint_list_show, NULL);
2875742bd85SVaidyanathan Srinivasan 
28834852ed5SDaniel Axtens static struct device_attribute attr_cpu_deactivate_hint_list =
2898a25a2fdSKay Sievers 		__ATTR(pseries_deactivate_hint_list, 0444,
2905742bd85SVaidyanathan Srinivasan 		cpu_deactivate_hint_list_show, NULL);
2915742bd85SVaidyanathan Srinivasan 
29234852ed5SDaniel Axtens static struct device_attribute attr_percpu_activate_hint =
2938a25a2fdSKay Sievers 		__ATTR(pseries_activate_hint, 0444,
2945742bd85SVaidyanathan Srinivasan 		percpu_activate_hint_show, NULL);
2955742bd85SVaidyanathan Srinivasan 
29634852ed5SDaniel Axtens static struct device_attribute attr_percpu_deactivate_hint =
2978a25a2fdSKay Sievers 		__ATTR(pseries_deactivate_hint, 0444,
2985742bd85SVaidyanathan Srinivasan 		percpu_deactivate_hint_show, NULL);
2995742bd85SVaidyanathan Srinivasan 
pseries_energy_init(void)3005742bd85SVaidyanathan Srinivasan static int __init pseries_energy_init(void)
3015742bd85SVaidyanathan Srinivasan {
3025742bd85SVaidyanathan Srinivasan 	int cpu, err;
303*2a9efef8SGreg Kroah-Hartman 	struct device *cpu_dev, *dev_root;
3045742bd85SVaidyanathan Srinivasan 
305a70a0b9fSNicholas Piggin 	if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY))
306a70a0b9fSNicholas Piggin 		return 0; /* H_BEST_ENERGY hcall not supported */
307a70a0b9fSNicholas Piggin 
3085742bd85SVaidyanathan Srinivasan 	/* Create the sysfs files */
309*2a9efef8SGreg Kroah-Hartman 	dev_root = bus_get_dev_root(&cpu_subsys);
310*2a9efef8SGreg Kroah-Hartman 	if (dev_root) {
311*2a9efef8SGreg Kroah-Hartman 		err = device_create_file(dev_root, &attr_cpu_activate_hint_list);
3125742bd85SVaidyanathan Srinivasan 		if (!err)
313*2a9efef8SGreg Kroah-Hartman 			err = device_create_file(dev_root, &attr_cpu_deactivate_hint_list);
314*2a9efef8SGreg Kroah-Hartman 		put_device(dev_root);
3155742bd85SVaidyanathan Srinivasan 		if (err)
3165742bd85SVaidyanathan Srinivasan 			return err;
317*2a9efef8SGreg Kroah-Hartman 	}
318*2a9efef8SGreg Kroah-Hartman 
3195742bd85SVaidyanathan Srinivasan 	for_each_possible_cpu(cpu) {
3208a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(cpu);
3218a25a2fdSKay Sievers 		err = device_create_file(cpu_dev,
3228a25a2fdSKay Sievers 				&attr_percpu_activate_hint);
3235742bd85SVaidyanathan Srinivasan 		if (err)
3245742bd85SVaidyanathan Srinivasan 			break;
3258a25a2fdSKay Sievers 		err = device_create_file(cpu_dev,
3268a25a2fdSKay Sievers 				&attr_percpu_deactivate_hint);
3275742bd85SVaidyanathan Srinivasan 		if (err)
3285742bd85SVaidyanathan Srinivasan 			break;
3295742bd85SVaidyanathan Srinivasan 	}
3305742bd85SVaidyanathan Srinivasan 
3315742bd85SVaidyanathan Srinivasan 	if (err)
3325742bd85SVaidyanathan Srinivasan 		return err;
3335742bd85SVaidyanathan Srinivasan 
3345742bd85SVaidyanathan Srinivasan 	sysfs_entries = 1; /* Removed entries on cleanup */
3355742bd85SVaidyanathan Srinivasan 	return 0;
3365742bd85SVaidyanathan Srinivasan 
3375742bd85SVaidyanathan Srinivasan }
3385742bd85SVaidyanathan Srinivasan 
pseries_energy_cleanup(void)3395742bd85SVaidyanathan Srinivasan static void __exit pseries_energy_cleanup(void)
3405742bd85SVaidyanathan Srinivasan {
3415742bd85SVaidyanathan Srinivasan 	int cpu;
342*2a9efef8SGreg Kroah-Hartman 	struct device *cpu_dev, *dev_root;
3435742bd85SVaidyanathan Srinivasan 
3445742bd85SVaidyanathan Srinivasan 	if (!sysfs_entries)
3455742bd85SVaidyanathan Srinivasan 		return;
3465742bd85SVaidyanathan Srinivasan 
3475742bd85SVaidyanathan Srinivasan 	/* Remove the sysfs files */
348*2a9efef8SGreg Kroah-Hartman 	dev_root = bus_get_dev_root(&cpu_subsys);
349*2a9efef8SGreg Kroah-Hartman 	if (dev_root) {
350*2a9efef8SGreg Kroah-Hartman 		device_remove_file(dev_root, &attr_cpu_activate_hint_list);
351*2a9efef8SGreg Kroah-Hartman 		device_remove_file(dev_root, &attr_cpu_deactivate_hint_list);
352*2a9efef8SGreg Kroah-Hartman 		put_device(dev_root);
353*2a9efef8SGreg Kroah-Hartman 	}
3545742bd85SVaidyanathan Srinivasan 
3555742bd85SVaidyanathan Srinivasan 	for_each_possible_cpu(cpu) {
3568a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(cpu);
3578a25a2fdSKay Sievers 		sysfs_remove_file(&cpu_dev->kobj,
3585742bd85SVaidyanathan Srinivasan 				&attr_percpu_activate_hint.attr);
3598a25a2fdSKay Sievers 		sysfs_remove_file(&cpu_dev->kobj,
3605742bd85SVaidyanathan Srinivasan 				&attr_percpu_deactivate_hint.attr);
3615742bd85SVaidyanathan Srinivasan 	}
3625742bd85SVaidyanathan Srinivasan }
3635742bd85SVaidyanathan Srinivasan 
3645742bd85SVaidyanathan Srinivasan module_init(pseries_energy_init);
3655742bd85SVaidyanathan Srinivasan module_exit(pseries_energy_cleanup);
3665742bd85SVaidyanathan Srinivasan MODULE_DESCRIPTION("Driver for pSeries platform energy management");
3675742bd85SVaidyanathan Srinivasan MODULE_AUTHOR("Vaidyanathan Srinivasan");
3685742bd85SVaidyanathan Srinivasan MODULE_LICENSE("GPL");
369