Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / powerpc / platforms / pseries / pseries_energy.c
blob6ed22127391b6d0a7789bb363476452bf0991a65
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>
25 #include <asm/prom.h>
28 #define MODULE_VERS "1.0"
29 #define MODULE_NAME "pseries_energy"
31 /* Driver flags */
33 static int sysfs_entries;
35 /* Helper routines */
37 /* Helper Routines to convert between drc_index to cpu numbers */
39 static u32 cpu_to_drc_index(int cpu)
41 struct device_node *dn = NULL;
42 int thread_index;
43 int rc = 1;
44 u32 ret = 0;
46 dn = of_find_node_by_path("/cpus");
47 if (dn == NULL)
48 goto err;
50 /* Convert logical cpu number to core number */
51 thread_index = cpu_core_index_of_thread(cpu);
53 if (firmware_has_feature(FW_FEATURE_DRC_INFO)) {
54 struct property *info = NULL;
55 struct of_drc_info drc;
56 int j;
57 u32 num_set_entries;
58 const __be32 *value;
60 info = of_find_property(dn, "ibm,drc-info", NULL);
61 if (info == NULL)
62 goto err_of_node_put;
64 value = of_prop_next_u32(info, NULL, &num_set_entries);
65 if (!value)
66 goto err_of_node_put;
68 for (j = 0; j < num_set_entries; j++) {
70 of_read_drc_info_cell(&info, &value, &drc);
71 if (strncmp(drc.drc_type, "CPU", 3))
72 goto err;
74 if (thread_index < drc.last_drc_index)
75 break;
78 ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
79 } else {
80 const __be32 *indexes;
82 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
83 if (indexes == NULL)
84 goto err_of_node_put;
87 * The first element indexes[0] is the number of drc_indexes
88 * returned in the list. Hence thread_index+1 will get the
89 * drc_index corresponding to core number thread_index.
91 ret = indexes[thread_index + 1];
94 rc = 0;
96 err_of_node_put:
97 of_node_put(dn);
98 err:
99 if (rc)
100 printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
101 return ret;
104 static int drc_index_to_cpu(u32 drc_index)
106 struct device_node *dn = NULL;
107 const int *indexes;
108 int thread_index = 0, cpu = 0;
109 int rc = 1;
111 dn = of_find_node_by_path("/cpus");
112 if (dn == NULL)
113 goto err;
115 if (firmware_has_feature(FW_FEATURE_DRC_INFO)) {
116 struct property *info = NULL;
117 struct of_drc_info drc;
118 int j;
119 u32 num_set_entries;
120 const __be32 *value;
122 info = of_find_property(dn, "ibm,drc-info", NULL);
123 if (info == NULL)
124 goto err_of_node_put;
126 value = of_prop_next_u32(info, NULL, &num_set_entries);
127 if (!value)
128 goto err_of_node_put;
130 for (j = 0; j < num_set_entries; j++) {
132 of_read_drc_info_cell(&info, &value, &drc);
133 if (strncmp(drc.drc_type, "CPU", 3))
134 goto err;
136 if (drc_index > drc.last_drc_index) {
137 cpu += drc.num_sequential_elems;
138 continue;
140 cpu += ((drc_index - drc.drc_index_start) /
141 drc.sequential_inc);
143 thread_index = cpu_first_thread_of_core(cpu);
144 rc = 0;
145 break;
147 } else {
148 unsigned long int i;
150 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
151 if (indexes == NULL)
152 goto err_of_node_put;
154 * First element in the array is the number of drc_indexes
155 * returned. Search through the list to find the matching
156 * drc_index and get the core number
158 for (i = 0; i < indexes[0]; i++) {
159 if (indexes[i + 1] == drc_index)
160 break;
162 /* Convert core number to logical cpu number */
163 thread_index = cpu_first_thread_of_core(i);
164 rc = 0;
167 err_of_node_put:
168 of_node_put(dn);
169 err:
170 if (rc)
171 printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
172 return thread_index;
176 * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
177 * preferred logical cpus to activate or deactivate for optimized
178 * energy consumption.
181 #define FLAGS_MODE1 0x004E200000080E01UL
182 #define FLAGS_MODE2 0x004E200000080401UL
183 #define FLAGS_ACTIVATE 0x100
185 static ssize_t get_best_energy_list(char *page, int activate)
187 int rc, cnt, i, cpu;
188 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
189 unsigned long flags = 0;
190 u32 *buf_page;
191 char *s = page;
193 buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
194 if (!buf_page)
195 return -ENOMEM;
197 flags = FLAGS_MODE1;
198 if (activate)
199 flags |= FLAGS_ACTIVATE;
201 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
202 0, 0, 0, 0, 0, 0);
203 if (rc != H_SUCCESS) {
204 free_page((unsigned long) buf_page);
205 return -EINVAL;
208 cnt = retbuf[0];
209 for (i = 0; i < cnt; i++) {
210 cpu = drc_index_to_cpu(buf_page[2*i+1]);
211 if ((cpu_online(cpu) && !activate) ||
212 (!cpu_online(cpu) && activate))
213 s += sprintf(s, "%d,", cpu);
215 if (s > page) { /* Something to show */
216 s--; /* Suppress last comma */
217 s += sprintf(s, "\n");
220 free_page((unsigned long) buf_page);
221 return s-page;
224 static ssize_t get_best_energy_data(struct device *dev,
225 char *page, int activate)
227 int rc;
228 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
229 unsigned long flags = 0;
231 flags = FLAGS_MODE2;
232 if (activate)
233 flags |= FLAGS_ACTIVATE;
235 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
236 cpu_to_drc_index(dev->id),
237 0, 0, 0, 0, 0, 0, 0);
239 if (rc != H_SUCCESS)
240 return -EINVAL;
242 return sprintf(page, "%lu\n", retbuf[1] >> 32);
245 /* Wrapper functions */
247 static ssize_t cpu_activate_hint_list_show(struct device *dev,
248 struct device_attribute *attr, char *page)
250 return get_best_energy_list(page, 1);
253 static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
254 struct device_attribute *attr, char *page)
256 return get_best_energy_list(page, 0);
259 static ssize_t percpu_activate_hint_show(struct device *dev,
260 struct device_attribute *attr, char *page)
262 return get_best_energy_data(dev, page, 1);
265 static ssize_t percpu_deactivate_hint_show(struct device *dev,
266 struct device_attribute *attr, char *page)
268 return get_best_energy_data(dev, page, 0);
272 * Create sysfs interface:
273 * /sys/devices/system/cpu/pseries_activate_hint_list
274 * /sys/devices/system/cpu/pseries_deactivate_hint_list
275 * Comma separated list of cpus to activate or deactivate
276 * /sys/devices/system/cpu/cpuN/pseries_activate_hint
277 * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
278 * Per-cpu value of the hint
281 static struct device_attribute attr_cpu_activate_hint_list =
282 __ATTR(pseries_activate_hint_list, 0444,
283 cpu_activate_hint_list_show, NULL);
285 static struct device_attribute attr_cpu_deactivate_hint_list =
286 __ATTR(pseries_deactivate_hint_list, 0444,
287 cpu_deactivate_hint_list_show, NULL);
289 static struct device_attribute attr_percpu_activate_hint =
290 __ATTR(pseries_activate_hint, 0444,
291 percpu_activate_hint_show, NULL);
293 static struct device_attribute attr_percpu_deactivate_hint =
294 __ATTR(pseries_deactivate_hint, 0444,
295 percpu_deactivate_hint_show, NULL);
297 static int __init pseries_energy_init(void)
299 int cpu, err;
300 struct device *cpu_dev;
302 if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY))
303 return 0; /* H_BEST_ENERGY hcall not supported */
305 /* Create the sysfs files */
306 err = device_create_file(cpu_subsys.dev_root,
307 &attr_cpu_activate_hint_list);
308 if (!err)
309 err = device_create_file(cpu_subsys.dev_root,
310 &attr_cpu_deactivate_hint_list);
312 if (err)
313 return err;
314 for_each_possible_cpu(cpu) {
315 cpu_dev = get_cpu_device(cpu);
316 err = device_create_file(cpu_dev,
317 &attr_percpu_activate_hint);
318 if (err)
319 break;
320 err = device_create_file(cpu_dev,
321 &attr_percpu_deactivate_hint);
322 if (err)
323 break;
326 if (err)
327 return err;
329 sysfs_entries = 1; /* Removed entries on cleanup */
330 return 0;
334 static void __exit pseries_energy_cleanup(void)
336 int cpu;
337 struct device *cpu_dev;
339 if (!sysfs_entries)
340 return;
342 /* Remove the sysfs files */
343 device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list);
344 device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list);
346 for_each_possible_cpu(cpu) {
347 cpu_dev = get_cpu_device(cpu);
348 sysfs_remove_file(&cpu_dev->kobj,
349 &attr_percpu_activate_hint.attr);
350 sysfs_remove_file(&cpu_dev->kobj,
351 &attr_percpu_deactivate_hint.attr);
355 module_init(pseries_energy_init);
356 module_exit(pseries_energy_cleanup);
357 MODULE_DESCRIPTION("Driver for pSeries platform energy management");
358 MODULE_AUTHOR("Vaidyanathan Srinivasan");
359 MODULE_LICENSE("GPL");