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>
21 #include <asm/cputhreads.h>
23 #include <asm/hvcall.h>
26 #define MODULE_VERS "1.0"
27 #define MODULE_NAME "pseries_energy"
31 static int sysfs_entries
;
36 * Routine to detect firmware support for hcall
37 * return 1 if H_BEST_ENERGY is supported
41 static int check_for_h_best_energy(void)
43 struct device_node
*rtas
= NULL
;
44 const char *hypertas
, *s
;
48 rtas
= of_find_node_by_path("/rtas");
52 hypertas
= of_get_property(rtas
, "ibm,hypertas-functions", &length
);
58 /* hypertas will have list of strings with hcall names */
59 for (s
= hypertas
; s
< hypertas
+ length
; s
+= strlen(s
) + 1) {
60 if (!strncmp("hcall-best-energy-1", s
, 19)) {
61 rc
= 1; /* Found the string */
69 /* Helper Routines to convert between drc_index to cpu numbers */
71 static u32
cpu_to_drc_index(int cpu
)
73 struct device_node
*dn
= NULL
;
79 dn
= of_find_node_by_path("/cpus");
82 indexes
= of_get_property(dn
, "ibm,drc-indexes", NULL
);
85 /* Convert logical cpu number to core number */
86 i
= cpu_core_index_of_thread(cpu
);
88 * The first element indexes[0] is the number of drc_indexes
89 * returned in the list. Hence i+1 will get the drc_index
90 * corresponding to core number i.
92 WARN_ON(i
> indexes
[0]);
100 printk(KERN_WARNING
"cpu_to_drc_index(%d) failed", cpu
);
104 static int drc_index_to_cpu(u32 drc_index
)
106 struct device_node
*dn
= NULL
;
111 dn
= of_find_node_by_path("/cpus");
114 indexes
= of_get_property(dn
, "ibm,drc-indexes", NULL
);
116 goto err_of_node_put
;
118 * First element in the array is the number of drc_indexes
119 * returned. Search through the list to find the matching
120 * drc_index and get the core number
122 for (i
= 0; i
< indexes
[0]; i
++) {
123 if (indexes
[i
+ 1] == drc_index
)
126 /* Convert core number to logical cpu number */
127 cpu
= cpu_first_thread_of_core(i
);
134 printk(KERN_WARNING
"drc_index_to_cpu(%d) failed", drc_index
);
139 * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
140 * preferred logical cpus to activate or deactivate for optimized
141 * energy consumption.
144 #define FLAGS_MODE1 0x004E200000080E01
145 #define FLAGS_MODE2 0x004E200000080401
146 #define FLAGS_ACTIVATE 0x100
148 static ssize_t
get_best_energy_list(char *page
, int activate
)
151 unsigned long retbuf
[PLPAR_HCALL9_BUFSIZE
];
152 unsigned long flags
= 0;
156 buf_page
= (u32
*) get_zeroed_page(GFP_KERNEL
);
162 flags
|= FLAGS_ACTIVATE
;
164 rc
= plpar_hcall9(H_BEST_ENERGY
, retbuf
, flags
, 0, __pa(buf_page
),
166 if (rc
!= H_SUCCESS
) {
167 free_page((unsigned long) buf_page
);
172 for (i
= 0; i
< cnt
; i
++) {
173 cpu
= drc_index_to_cpu(buf_page
[2*i
+1]);
174 if ((cpu_online(cpu
) && !activate
) ||
175 (!cpu_online(cpu
) && activate
))
176 s
+= sprintf(s
, "%d,", cpu
);
178 if (s
> page
) { /* Something to show */
179 s
--; /* Suppress last comma */
180 s
+= sprintf(s
, "\n");
183 free_page((unsigned long) buf_page
);
187 static ssize_t
get_best_energy_data(struct device
*dev
,
188 char *page
, int activate
)
191 unsigned long retbuf
[PLPAR_HCALL9_BUFSIZE
];
192 unsigned long flags
= 0;
196 flags
|= FLAGS_ACTIVATE
;
198 rc
= plpar_hcall9(H_BEST_ENERGY
, retbuf
, flags
,
199 cpu_to_drc_index(dev
->id
),
200 0, 0, 0, 0, 0, 0, 0);
205 return sprintf(page
, "%lu\n", retbuf
[1] >> 32);
208 /* Wrapper functions */
210 static ssize_t
cpu_activate_hint_list_show(struct device
*dev
,
211 struct device_attribute
*attr
, char *page
)
213 return get_best_energy_list(page
, 1);
216 static ssize_t
cpu_deactivate_hint_list_show(struct device
*dev
,
217 struct device_attribute
*attr
, char *page
)
219 return get_best_energy_list(page
, 0);
222 static ssize_t
percpu_activate_hint_show(struct device
*dev
,
223 struct device_attribute
*attr
, char *page
)
225 return get_best_energy_data(dev
, page
, 1);
228 static ssize_t
percpu_deactivate_hint_show(struct device
*dev
,
229 struct device_attribute
*attr
, char *page
)
231 return get_best_energy_data(dev
, page
, 0);
235 * Create sysfs interface:
236 * /sys/devices/system/cpu/pseries_activate_hint_list
237 * /sys/devices/system/cpu/pseries_deactivate_hint_list
238 * Comma separated list of cpus to activate or deactivate
239 * /sys/devices/system/cpu/cpuN/pseries_activate_hint
240 * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
241 * Per-cpu value of the hint
244 struct device_attribute attr_cpu_activate_hint_list
=
245 __ATTR(pseries_activate_hint_list
, 0444,
246 cpu_activate_hint_list_show
, NULL
);
248 struct device_attribute attr_cpu_deactivate_hint_list
=
249 __ATTR(pseries_deactivate_hint_list
, 0444,
250 cpu_deactivate_hint_list_show
, NULL
);
252 struct device_attribute attr_percpu_activate_hint
=
253 __ATTR(pseries_activate_hint
, 0444,
254 percpu_activate_hint_show
, NULL
);
256 struct device_attribute attr_percpu_deactivate_hint
=
257 __ATTR(pseries_deactivate_hint
, 0444,
258 percpu_deactivate_hint_show
, NULL
);
260 static int __init
pseries_energy_init(void)
263 struct device
*cpu_dev
;
265 if (!check_for_h_best_energy()) {
266 printk(KERN_INFO
"Hypercall H_BEST_ENERGY not supported\n");
269 /* Create the sysfs files */
270 err
= device_create_file(cpu_subsys
.dev_root
,
271 &attr_cpu_activate_hint_list
);
273 err
= device_create_file(cpu_subsys
.dev_root
,
274 &attr_cpu_deactivate_hint_list
);
278 for_each_possible_cpu(cpu
) {
279 cpu_dev
= get_cpu_device(cpu
);
280 err
= device_create_file(cpu_dev
,
281 &attr_percpu_activate_hint
);
284 err
= device_create_file(cpu_dev
,
285 &attr_percpu_deactivate_hint
);
293 sysfs_entries
= 1; /* Removed entries on cleanup */
298 static void __exit
pseries_energy_cleanup(void)
301 struct device
*cpu_dev
;
306 /* Remove the sysfs files */
307 device_remove_file(cpu_subsys
.dev_root
, &attr_cpu_activate_hint_list
);
308 device_remove_file(cpu_subsys
.dev_root
, &attr_cpu_deactivate_hint_list
);
310 for_each_possible_cpu(cpu
) {
311 cpu_dev
= get_cpu_device(cpu
);
312 sysfs_remove_file(&cpu_dev
->kobj
,
313 &attr_percpu_activate_hint
.attr
);
314 sysfs_remove_file(&cpu_dev
->kobj
,
315 &attr_percpu_deactivate_hint
.attr
);
319 module_init(pseries_energy_init
);
320 module_exit(pseries_energy_cleanup
);
321 MODULE_DESCRIPTION("Driver for pSeries platform energy management");
322 MODULE_AUTHOR("Vaidyanathan Srinivasan");
323 MODULE_LICENSE("GPL");