Linux 4.1.18
[linux/fpc-iii.git] / arch / powerpc / platforms / pseries / pseries_energy.c
blob92767791f93b647e11cac5ec5156a0ab7f34bd49
1 /*
2 * POWER platform energy management driver
3 * Copyright (C) 2010 IBM Corporation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This pseries platform device driver provides access to
10 * platform energy management capabilities.
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/seq_file.h>
18 #include <linux/device.h>
19 #include <linux/cpu.h>
20 #include <linux/of.h>
21 #include <asm/cputhreads.h>
22 #include <asm/page.h>
23 #include <asm/hvcall.h>
24 #include <asm/firmware.h>
27 #define MODULE_VERS "1.0"
28 #define MODULE_NAME "pseries_energy"
30 /* Driver flags */
32 static int sysfs_entries;
34 /* Helper routines */
36 /* Helper Routines to convert between drc_index to cpu numbers */
38 static u32 cpu_to_drc_index(int cpu)
40 struct device_node *dn = NULL;
41 const int *indexes;
42 int i;
43 int rc = 1;
44 u32 ret = 0;
46 dn = of_find_node_by_path("/cpus");
47 if (dn == NULL)
48 goto err;
49 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
50 if (indexes == NULL)
51 goto err_of_node_put;
52 /* Convert logical cpu number to core number */
53 i = cpu_core_index_of_thread(cpu);
55 * The first element indexes[0] is the number of drc_indexes
56 * returned in the list. Hence i+1 will get the drc_index
57 * corresponding to core number i.
59 WARN_ON(i > indexes[0]);
60 ret = indexes[i + 1];
61 rc = 0;
63 err_of_node_put:
64 of_node_put(dn);
65 err:
66 if (rc)
67 printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
68 return ret;
71 static int drc_index_to_cpu(u32 drc_index)
73 struct device_node *dn = NULL;
74 const int *indexes;
75 int i, cpu = 0;
76 int rc = 1;
78 dn = of_find_node_by_path("/cpus");
79 if (dn == NULL)
80 goto err;
81 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
82 if (indexes == NULL)
83 goto err_of_node_put;
85 * First element in the array is the number of drc_indexes
86 * returned. Search through the list to find the matching
87 * drc_index and get the core number
89 for (i = 0; i < indexes[0]; i++) {
90 if (indexes[i + 1] == drc_index)
91 break;
93 /* Convert core number to logical cpu number */
94 cpu = cpu_first_thread_of_core(i);
95 rc = 0;
97 err_of_node_put:
98 of_node_put(dn);
99 err:
100 if (rc)
101 printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
102 return cpu;
106 * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
107 * preferred logical cpus to activate or deactivate for optimized
108 * energy consumption.
111 #define FLAGS_MODE1 0x004E200000080E01UL
112 #define FLAGS_MODE2 0x004E200000080401UL
113 #define FLAGS_ACTIVATE 0x100
115 static ssize_t get_best_energy_list(char *page, int activate)
117 int rc, cnt, i, cpu;
118 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
119 unsigned long flags = 0;
120 u32 *buf_page;
121 char *s = page;
123 buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
124 if (!buf_page)
125 return -ENOMEM;
127 flags = FLAGS_MODE1;
128 if (activate)
129 flags |= FLAGS_ACTIVATE;
131 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
132 0, 0, 0, 0, 0, 0);
133 if (rc != H_SUCCESS) {
134 free_page((unsigned long) buf_page);
135 return -EINVAL;
138 cnt = retbuf[0];
139 for (i = 0; i < cnt; i++) {
140 cpu = drc_index_to_cpu(buf_page[2*i+1]);
141 if ((cpu_online(cpu) && !activate) ||
142 (!cpu_online(cpu) && activate))
143 s += sprintf(s, "%d,", cpu);
145 if (s > page) { /* Something to show */
146 s--; /* Suppress last comma */
147 s += sprintf(s, "\n");
150 free_page((unsigned long) buf_page);
151 return s-page;
154 static ssize_t get_best_energy_data(struct device *dev,
155 char *page, int activate)
157 int rc;
158 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
159 unsigned long flags = 0;
161 flags = FLAGS_MODE2;
162 if (activate)
163 flags |= FLAGS_ACTIVATE;
165 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
166 cpu_to_drc_index(dev->id),
167 0, 0, 0, 0, 0, 0, 0);
169 if (rc != H_SUCCESS)
170 return -EINVAL;
172 return sprintf(page, "%lu\n", retbuf[1] >> 32);
175 /* Wrapper functions */
177 static ssize_t cpu_activate_hint_list_show(struct device *dev,
178 struct device_attribute *attr, char *page)
180 return get_best_energy_list(page, 1);
183 static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
184 struct device_attribute *attr, char *page)
186 return get_best_energy_list(page, 0);
189 static ssize_t percpu_activate_hint_show(struct device *dev,
190 struct device_attribute *attr, char *page)
192 return get_best_energy_data(dev, page, 1);
195 static ssize_t percpu_deactivate_hint_show(struct device *dev,
196 struct device_attribute *attr, char *page)
198 return get_best_energy_data(dev, page, 0);
202 * Create sysfs interface:
203 * /sys/devices/system/cpu/pseries_activate_hint_list
204 * /sys/devices/system/cpu/pseries_deactivate_hint_list
205 * Comma separated list of cpus to activate or deactivate
206 * /sys/devices/system/cpu/cpuN/pseries_activate_hint
207 * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
208 * Per-cpu value of the hint
211 struct device_attribute attr_cpu_activate_hint_list =
212 __ATTR(pseries_activate_hint_list, 0444,
213 cpu_activate_hint_list_show, NULL);
215 struct device_attribute attr_cpu_deactivate_hint_list =
216 __ATTR(pseries_deactivate_hint_list, 0444,
217 cpu_deactivate_hint_list_show, NULL);
219 struct device_attribute attr_percpu_activate_hint =
220 __ATTR(pseries_activate_hint, 0444,
221 percpu_activate_hint_show, NULL);
223 struct device_attribute attr_percpu_deactivate_hint =
224 __ATTR(pseries_deactivate_hint, 0444,
225 percpu_deactivate_hint_show, NULL);
227 static int __init pseries_energy_init(void)
229 int cpu, err;
230 struct device *cpu_dev;
232 if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY)) {
233 printk(KERN_INFO "Hypercall H_BEST_ENERGY not supported\n");
234 return 0;
236 /* Create the sysfs files */
237 err = device_create_file(cpu_subsys.dev_root,
238 &attr_cpu_activate_hint_list);
239 if (!err)
240 err = device_create_file(cpu_subsys.dev_root,
241 &attr_cpu_deactivate_hint_list);
243 if (err)
244 return err;
245 for_each_possible_cpu(cpu) {
246 cpu_dev = get_cpu_device(cpu);
247 err = device_create_file(cpu_dev,
248 &attr_percpu_activate_hint);
249 if (err)
250 break;
251 err = device_create_file(cpu_dev,
252 &attr_percpu_deactivate_hint);
253 if (err)
254 break;
257 if (err)
258 return err;
260 sysfs_entries = 1; /* Removed entries on cleanup */
261 return 0;
265 static void __exit pseries_energy_cleanup(void)
267 int cpu;
268 struct device *cpu_dev;
270 if (!sysfs_entries)
271 return;
273 /* Remove the sysfs files */
274 device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list);
275 device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list);
277 for_each_possible_cpu(cpu) {
278 cpu_dev = get_cpu_device(cpu);
279 sysfs_remove_file(&cpu_dev->kobj,
280 &attr_percpu_activate_hint.attr);
281 sysfs_remove_file(&cpu_dev->kobj,
282 &attr_percpu_deactivate_hint.attr);
286 module_init(pseries_energy_init);
287 module_exit(pseries_energy_cleanup);
288 MODULE_DESCRIPTION("Driver for pSeries platform energy management");
289 MODULE_AUTHOR("Vaidyanathan Srinivasan");
290 MODULE_LICENSE("GPL");