1 // SPDX-License-Identifier: GPL-2.0-only
3 * POWER platform energy management driver
4 * Copyright (C) 2010 IBM Corporation
6 * This pseries platform device driver provides access to
7 * platform energy management capabilities.
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/seq_file.h>
15 #include <linux/device.h>
16 #include <linux/cpu.h>
18 #include <asm/cputhreads.h>
20 #include <asm/hvcall.h>
21 #include <asm/firmware.h>
25 #define MODULE_VERS "1.0"
26 #define MODULE_NAME "pseries_energy"
30 static int sysfs_entries;
34 /* Helper Routines to convert between drc_index to cpu numbers */
36 static u32 cpu_to_drc_index(int cpu)
38 struct device_node *dn = NULL;
43 dn = of_find_node_by_path("/cpus");
47 /* Convert logical cpu number to core number */
48 thread_index = cpu_core_index_of_thread(cpu);
50 if (firmware_has_feature(FW_FEATURE_DRC_INFO)) {
51 struct property *info = NULL;
52 struct of_drc_info drc;
57 info = of_find_property(dn, "ibm,drc-info", NULL);
61 value = of_prop_next_u32(info, NULL, &num_set_entries);
65 for (j = 0; j < num_set_entries; j++) {
67 of_read_drc_info_cell(&info, &value, &drc);
68 if (strncmp(drc.drc_type, "CPU", 3))
71 if (thread_index < drc.last_drc_index)
75 ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
77 u32 nr_drc_indexes, thread_drc_index;
80 * The first element of ibm,drc-indexes array is the
81 * number of drc_indexes returned in the list. Hence
82 * thread_index+1 will get the drc_index corresponding
83 * to core number thread_index.
85 rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
90 WARN_ON_ONCE(thread_index > nr_drc_indexes);
91 rc = of_property_read_u32_index(dn, "ibm,drc-indexes",
97 ret = thread_drc_index;
106 printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
110 static int drc_index_to_cpu(u32 drc_index)
112 struct device_node *dn = NULL;
114 int thread_index = 0, cpu = 0;
117 dn = of_find_node_by_path("/cpus");
121 if (firmware_has_feature(FW_FEATURE_DRC_INFO)) {
122 struct property *info = NULL;
123 struct of_drc_info drc;
128 info = of_find_property(dn, "ibm,drc-info", NULL);
130 goto err_of_node_put;
132 value = of_prop_next_u32(info, NULL, &num_set_entries);
134 goto err_of_node_put;
136 for (j = 0; j < num_set_entries; j++) {
138 of_read_drc_info_cell(&info, &value, &drc);
139 if (strncmp(drc.drc_type, "CPU", 3))
142 if (drc_index > drc.last_drc_index) {
143 cpu += drc.num_sequential_elems;
146 cpu += ((drc_index - drc.drc_index_start) /
149 thread_index = cpu_first_thread_of_core(cpu);
156 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
158 goto err_of_node_put;
160 * First element in the array is the number of drc_indexes
161 * returned. Search through the list to find the matching
162 * drc_index and get the core number
164 for (i = 0; i < indexes[0]; i++) {
165 if (indexes[i + 1] == drc_index)
168 /* Convert core number to logical cpu number */
169 thread_index = cpu_first_thread_of_core(i);
177 printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
182 * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
183 * preferred logical cpus to activate or deactivate for optimized
184 * energy consumption.
187 #define FLAGS_MODE1 0x004E200000080E01UL
188 #define FLAGS_MODE2 0x004E200000080401UL
189 #define FLAGS_ACTIVATE 0x100
191 static ssize_t get_best_energy_list(char *page, int activate)
194 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
195 unsigned long flags = 0;
199 buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
205 flags |= FLAGS_ACTIVATE;
207 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
209 if (rc != H_SUCCESS) {
210 free_page((unsigned long) buf_page);
215 for (i = 0; i < cnt; i++) {
216 cpu = drc_index_to_cpu(buf_page[2*i+1]);
217 if ((cpu_online(cpu) && !activate) ||
218 (!cpu_online(cpu) && activate))
219 s += sprintf(s, "%d,", cpu);
221 if (s > page) { /* Something to show */
222 s--; /* Suppress last comma */
223 s += sprintf(s, "\n");
226 free_page((unsigned long) buf_page);
230 static ssize_t get_best_energy_data(struct device *dev,
231 char *page, int activate)
234 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
235 unsigned long flags = 0;
239 flags |= FLAGS_ACTIVATE;
241 rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
242 cpu_to_drc_index(dev->id),
243 0, 0, 0, 0, 0, 0, 0);
248 return sprintf(page, "%lu\n", retbuf[1] >> 32);
251 /* Wrapper functions */
253 static ssize_t cpu_activate_hint_list_show(struct device *dev,
254 struct device_attribute *attr, char *page)
256 return get_best_energy_list(page, 1);
259 static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
260 struct device_attribute *attr, char *page)
262 return get_best_energy_list(page, 0);
265 static ssize_t percpu_activate_hint_show(struct device *dev,
266 struct device_attribute *attr, char *page)
268 return get_best_energy_data(dev, page, 1);
271 static ssize_t percpu_deactivate_hint_show(struct device *dev,
272 struct device_attribute *attr, char *page)
274 return get_best_energy_data(dev, page, 0);
278 * Create sysfs interface:
279 * /sys/devices/system/cpu/pseries_activate_hint_list
280 * /sys/devices/system/cpu/pseries_deactivate_hint_list
281 * Comma separated list of cpus to activate or deactivate
282 * /sys/devices/system/cpu/cpuN/pseries_activate_hint
283 * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
284 * Per-cpu value of the hint
287 static struct device_attribute attr_cpu_activate_hint_list =
288 __ATTR(pseries_activate_hint_list, 0444,
289 cpu_activate_hint_list_show, NULL);
291 static struct device_attribute attr_cpu_deactivate_hint_list =
292 __ATTR(pseries_deactivate_hint_list, 0444,
293 cpu_deactivate_hint_list_show, NULL);
295 static struct device_attribute attr_percpu_activate_hint =
296 __ATTR(pseries_activate_hint, 0444,
297 percpu_activate_hint_show, NULL);
299 static struct device_attribute attr_percpu_deactivate_hint =
300 __ATTR(pseries_deactivate_hint, 0444,
301 percpu_deactivate_hint_show, NULL);
303 static int __init pseries_energy_init(void)
306 struct device *cpu_dev;
308 if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY))
309 return 0; /* H_BEST_ENERGY hcall not supported */
311 /* Create the sysfs files */
312 err = device_create_file(cpu_subsys.dev_root,
313 &attr_cpu_activate_hint_list);
315 err = device_create_file(cpu_subsys.dev_root,
316 &attr_cpu_deactivate_hint_list);
320 for_each_possible_cpu(cpu) {
321 cpu_dev = get_cpu_device(cpu);
322 err = device_create_file(cpu_dev,
323 &attr_percpu_activate_hint);
326 err = device_create_file(cpu_dev,
327 &attr_percpu_deactivate_hint);
335 sysfs_entries = 1; /* Removed entries on cleanup */
340 static void __exit pseries_energy_cleanup(void)
343 struct device *cpu_dev;
348 /* Remove the sysfs files */
349 device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list);
350 device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list);
352 for_each_possible_cpu(cpu) {
353 cpu_dev = get_cpu_device(cpu);
354 sysfs_remove_file(&cpu_dev->kobj,
355 &attr_percpu_activate_hint.attr);
356 sysfs_remove_file(&cpu_dev->kobj,
357 &attr_percpu_deactivate_hint.attr);
361 module_init(pseries_energy_init);
362 module_exit(pseries_energy_cleanup);
363 MODULE_DESCRIPTION("Driver for pSeries platform energy management");
364 MODULE_AUTHOR("Vaidyanathan Srinivasan");
365 MODULE_LICENSE("GPL");