s390/vdso: drop unnecessary cc-ldoption
[linux-2.6-microblaze.git] / arch / powerpc / platforms / pseries / pseries_energy.c
1 /*
2  * POWER platform energy management driver
3  * Copyright (C) 2010 IBM Corporation
4  *
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.
8  *
9  * This pseries platform device driver provides access to
10  * platform energy management capabilities.
11  */
12
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>
26
27
28 #define MODULE_VERS "1.0"
29 #define MODULE_NAME "pseries_energy"
30
31 /* Driver flags */
32
33 static int sysfs_entries;
34
35 /* Helper routines */
36
37 /* Helper Routines to convert between drc_index to cpu numbers */
38
39 static u32 cpu_to_drc_index(int cpu)
40 {
41         struct device_node *dn = NULL;
42         int thread_index;
43         int rc = 1;
44         u32 ret = 0;
45
46         dn = of_find_node_by_path("/cpus");
47         if (dn == NULL)
48                 goto err;
49
50         /* Convert logical cpu number to core number */
51         thread_index = cpu_core_index_of_thread(cpu);
52
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;
59
60                 info = of_find_property(dn, "ibm,drc-info", NULL);
61                 if (info == NULL)
62                         goto err_of_node_put;
63
64                 value = of_prop_next_u32(info, NULL, &num_set_entries);
65                 if (!value)
66                         goto err_of_node_put;
67
68                 for (j = 0; j < num_set_entries; j++) {
69
70                         of_read_drc_info_cell(&info, &value, &drc);
71                         if (strncmp(drc.drc_type, "CPU", 3))
72                                 goto err;
73
74                         if (thread_index < drc.last_drc_index)
75                                 break;
76                 }
77
78                 ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
79         } else {
80                 const __be32 *indexes;
81
82                 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
83                 if (indexes == NULL)
84                         goto err_of_node_put;
85
86                 /*
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.
90                  */
91                 ret = indexes[thread_index + 1];
92         }
93
94         rc = 0;
95
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;
102 }
103
104 static int drc_index_to_cpu(u32 drc_index)
105 {
106         struct device_node *dn = NULL;
107         const int *indexes;
108         int thread_index = 0, cpu = 0;
109         int rc = 1;
110
111         dn = of_find_node_by_path("/cpus");
112         if (dn == NULL)
113                 goto err;
114
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;
121
122                 info = of_find_property(dn, "ibm,drc-info", NULL);
123                 if (info == NULL)
124                         goto err_of_node_put;
125
126                 value = of_prop_next_u32(info, NULL, &num_set_entries);
127                 if (!value)
128                         goto err_of_node_put;
129
130                 for (j = 0; j < num_set_entries; j++) {
131
132                         of_read_drc_info_cell(&info, &value, &drc);
133                         if (strncmp(drc.drc_type, "CPU", 3))
134                                 goto err;
135
136                         if (drc_index > drc.last_drc_index) {
137                                 cpu += drc.num_sequential_elems;
138                                 continue;
139                         }
140                         cpu += ((drc_index - drc.drc_index_start) /
141                                 drc.sequential_inc);
142
143                         thread_index = cpu_first_thread_of_core(cpu);
144                         rc = 0;
145                         break;
146                 }
147         } else {
148                 unsigned long int i;
149
150                 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
151                 if (indexes == NULL)
152                         goto err_of_node_put;
153                 /*
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
157                  */
158                 for (i = 0; i < indexes[0]; i++) {
159                         if (indexes[i + 1] == drc_index)
160                                 break;
161                 }
162                 /* Convert core number to logical cpu number */
163                 thread_index = cpu_first_thread_of_core(i);
164                 rc = 0;
165         }
166
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;
173 }
174
175 /*
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.
179  */
180
181 #define FLAGS_MODE1     0x004E200000080E01UL
182 #define FLAGS_MODE2     0x004E200000080401UL
183 #define FLAGS_ACTIVATE  0x100
184
185 static ssize_t get_best_energy_list(char *page, int activate)
186 {
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;
192
193         buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
194         if (!buf_page)
195                 return -ENOMEM;
196
197         flags = FLAGS_MODE1;
198         if (activate)
199                 flags |= FLAGS_ACTIVATE;
200
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;
206         }
207
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);
214         }
215         if (s > page) { /* Something to show */
216                 s--; /* Suppress last comma */
217                 s += sprintf(s, "\n");
218         }
219
220         free_page((unsigned long) buf_page);
221         return s-page;
222 }
223
224 static ssize_t get_best_energy_data(struct device *dev,
225                                         char *page, int activate)
226 {
227         int rc;
228         unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
229         unsigned long flags = 0;
230
231         flags = FLAGS_MODE2;
232         if (activate)
233                 flags |= FLAGS_ACTIVATE;
234
235         rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
236                                 cpu_to_drc_index(dev->id),
237                                 0, 0, 0, 0, 0, 0, 0);
238
239         if (rc != H_SUCCESS)
240                 return -EINVAL;
241
242         return sprintf(page, "%lu\n", retbuf[1] >> 32);
243 }
244
245 /* Wrapper functions */
246
247 static ssize_t cpu_activate_hint_list_show(struct device *dev,
248                         struct device_attribute *attr, char *page)
249 {
250         return get_best_energy_list(page, 1);
251 }
252
253 static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
254                         struct device_attribute *attr, char *page)
255 {
256         return get_best_energy_list(page, 0);
257 }
258
259 static ssize_t percpu_activate_hint_show(struct device *dev,
260                         struct device_attribute *attr, char *page)
261 {
262         return get_best_energy_data(dev, page, 1);
263 }
264
265 static ssize_t percpu_deactivate_hint_show(struct device *dev,
266                         struct device_attribute *attr, char *page)
267 {
268         return get_best_energy_data(dev, page, 0);
269 }
270
271 /*
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
279  */
280
281 static struct device_attribute attr_cpu_activate_hint_list =
282                 __ATTR(pseries_activate_hint_list, 0444,
283                 cpu_activate_hint_list_show, NULL);
284
285 static struct device_attribute attr_cpu_deactivate_hint_list =
286                 __ATTR(pseries_deactivate_hint_list, 0444,
287                 cpu_deactivate_hint_list_show, NULL);
288
289 static struct device_attribute attr_percpu_activate_hint =
290                 __ATTR(pseries_activate_hint, 0444,
291                 percpu_activate_hint_show, NULL);
292
293 static struct device_attribute attr_percpu_deactivate_hint =
294                 __ATTR(pseries_deactivate_hint, 0444,
295                 percpu_deactivate_hint_show, NULL);
296
297 static int __init pseries_energy_init(void)
298 {
299         int cpu, err;
300         struct device *cpu_dev;
301
302         if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY))
303                 return 0; /* H_BEST_ENERGY hcall not supported */
304
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);
311
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;
324         }
325
326         if (err)
327                 return err;
328
329         sysfs_entries = 1; /* Removed entries on cleanup */
330         return 0;
331
332 }
333
334 static void __exit pseries_energy_cleanup(void)
335 {
336         int cpu;
337         struct device *cpu_dev;
338
339         if (!sysfs_entries)
340                 return;
341
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);
345
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);
352         }
353 }
354
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");