2010-10-06 16:37:09 +08:00
|
|
|
/*
|
|
|
|
* POWER platform energy management driver
|
|
|
|
* Copyright (C) 2010 IBM Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This pseries platform device driver provides access to
|
|
|
|
* platform energy management capabilities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/seq_file.h>
|
2011-12-22 06:29:42 +08:00
|
|
|
#include <linux/device.h>
|
2010-10-06 16:37:09 +08:00
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <asm/cputhreads.h>
|
|
|
|
#include <asm/page.h>
|
|
|
|
#include <asm/hvcall.h>
|
2012-11-06 22:49:16 +08:00
|
|
|
#include <asm/firmware.h>
|
2017-12-02 07:19:43 +08:00
|
|
|
#include <asm/prom.h>
|
2010-10-06 16:37:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
#define MODULE_VERS "1.0"
|
|
|
|
#define MODULE_NAME "pseries_energy"
|
|
|
|
|
|
|
|
/* Driver flags */
|
|
|
|
|
|
|
|
static int sysfs_entries;
|
|
|
|
|
|
|
|
/* Helper routines */
|
|
|
|
|
|
|
|
/* Helper Routines to convert between drc_index to cpu numbers */
|
|
|
|
|
|
|
|
static u32 cpu_to_drc_index(int cpu)
|
|
|
|
{
|
|
|
|
struct device_node *dn = NULL;
|
2017-12-02 07:19:43 +08:00
|
|
|
int thread_index;
|
2010-10-06 16:37:09 +08:00
|
|
|
int rc = 1;
|
|
|
|
u32 ret = 0;
|
|
|
|
|
|
|
|
dn = of_find_node_by_path("/cpus");
|
|
|
|
if (dn == NULL)
|
|
|
|
goto err;
|
2017-12-02 07:19:43 +08:00
|
|
|
|
2010-10-06 16:37:09 +08:00
|
|
|
/* Convert logical cpu number to core number */
|
2017-12-02 07:19:43 +08:00
|
|
|
thread_index = cpu_core_index_of_thread(cpu);
|
|
|
|
|
|
|
|
if (firmware_has_feature(FW_FEATURE_DRC_INFO)) {
|
|
|
|
struct property *info = NULL;
|
|
|
|
struct of_drc_info drc;
|
|
|
|
int j;
|
|
|
|
u32 num_set_entries;
|
|
|
|
const __be32 *value;
|
|
|
|
|
|
|
|
info = of_find_property(dn, "ibm,drc-info", NULL);
|
|
|
|
if (info == NULL)
|
|
|
|
goto err_of_node_put;
|
|
|
|
|
|
|
|
value = of_prop_next_u32(info, NULL, &num_set_entries);
|
|
|
|
if (!value)
|
|
|
|
goto err_of_node_put;
|
|
|
|
|
|
|
|
for (j = 0; j < num_set_entries; j++) {
|
|
|
|
|
|
|
|
of_read_drc_info_cell(&info, &value, &drc);
|
|
|
|
if (strncmp(drc.drc_type, "CPU", 3))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (thread_index < drc.last_drc_index)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = drc.drc_index_start + (thread_index * drc.sequential_inc);
|
|
|
|
} else {
|
|
|
|
const __be32 *indexes;
|
|
|
|
|
|
|
|
indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
|
|
|
|
if (indexes == NULL)
|
|
|
|
goto err_of_node_put;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The first element indexes[0] is the number of drc_indexes
|
|
|
|
* returned in the list. Hence thread_index+1 will get the
|
|
|
|
* drc_index corresponding to core number thread_index.
|
|
|
|
*/
|
|
|
|
ret = indexes[thread_index + 1];
|
|
|
|
}
|
|
|
|
|
2010-10-06 16:37:09 +08:00
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
err_of_node_put:
|
|
|
|
of_node_put(dn);
|
|
|
|
err:
|
|
|
|
if (rc)
|
|
|
|
printk(KERN_WARNING "cpu_to_drc_index(%d) failed", cpu);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int drc_index_to_cpu(u32 drc_index)
|
|
|
|
{
|
|
|
|
struct device_node *dn = NULL;
|
|
|
|
const int *indexes;
|
2017-12-02 07:19:43 +08:00
|
|
|
int thread_index = 0, cpu = 0;
|
2010-10-06 16:37:09 +08:00
|
|
|
int rc = 1;
|
|
|
|
|
|
|
|
dn = of_find_node_by_path("/cpus");
|
|
|
|
if (dn == NULL)
|
|
|
|
goto err;
|
2017-12-02 07:19:43 +08:00
|
|
|
|
|
|
|
if (firmware_has_feature(FW_FEATURE_DRC_INFO)) {
|
|
|
|
struct property *info = NULL;
|
|
|
|
struct of_drc_info drc;
|
|
|
|
int j;
|
|
|
|
u32 num_set_entries;
|
|
|
|
const __be32 *value;
|
|
|
|
|
|
|
|
info = of_find_property(dn, "ibm,drc-info", NULL);
|
|
|
|
if (info == NULL)
|
|
|
|
goto err_of_node_put;
|
|
|
|
|
|
|
|
value = of_prop_next_u32(info, NULL, &num_set_entries);
|
|
|
|
if (!value)
|
|
|
|
goto err_of_node_put;
|
|
|
|
|
|
|
|
for (j = 0; j < num_set_entries; j++) {
|
|
|
|
|
|
|
|
of_read_drc_info_cell(&info, &value, &drc);
|
|
|
|
if (strncmp(drc.drc_type, "CPU", 3))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (drc_index > drc.last_drc_index) {
|
|
|
|
cpu += drc.num_sequential_elems;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cpu += ((drc_index - drc.drc_index_start) /
|
|
|
|
drc.sequential_inc);
|
|
|
|
|
|
|
|
thread_index = cpu_first_thread_of_core(cpu);
|
|
|
|
rc = 0;
|
2010-10-06 16:37:09 +08:00
|
|
|
break;
|
2017-12-02 07:19:43 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned long int i;
|
|
|
|
|
|
|
|
indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
|
|
|
|
if (indexes == NULL)
|
|
|
|
goto err_of_node_put;
|
|
|
|
/*
|
|
|
|
* First element in the array is the number of drc_indexes
|
|
|
|
* returned. Search through the list to find the matching
|
|
|
|
* drc_index and get the core number
|
|
|
|
*/
|
|
|
|
for (i = 0; i < indexes[0]; i++) {
|
|
|
|
if (indexes[i + 1] == drc_index)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Convert core number to logical cpu number */
|
|
|
|
thread_index = cpu_first_thread_of_core(i);
|
|
|
|
rc = 0;
|
2010-10-06 16:37:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err_of_node_put:
|
|
|
|
of_node_put(dn);
|
|
|
|
err:
|
|
|
|
if (rc)
|
|
|
|
printk(KERN_WARNING "drc_index_to_cpu(%d) failed", drc_index);
|
2017-12-02 07:19:43 +08:00
|
|
|
return thread_index;
|
2010-10-06 16:37:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pseries hypervisor call H_BEST_ENERGY provides hints to OS on
|
|
|
|
* preferred logical cpus to activate or deactivate for optimized
|
|
|
|
* energy consumption.
|
|
|
|
*/
|
|
|
|
|
2013-08-07 00:01:24 +08:00
|
|
|
#define FLAGS_MODE1 0x004E200000080E01UL
|
|
|
|
#define FLAGS_MODE2 0x004E200000080401UL
|
2010-10-06 16:37:09 +08:00
|
|
|
#define FLAGS_ACTIVATE 0x100
|
|
|
|
|
|
|
|
static ssize_t get_best_energy_list(char *page, int activate)
|
|
|
|
{
|
|
|
|
int rc, cnt, i, cpu;
|
|
|
|
unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
|
|
|
|
unsigned long flags = 0;
|
|
|
|
u32 *buf_page;
|
|
|
|
char *s = page;
|
|
|
|
|
|
|
|
buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
|
|
|
|
if (!buf_page)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
flags = FLAGS_MODE1;
|
|
|
|
if (activate)
|
|
|
|
flags |= FLAGS_ACTIVATE;
|
|
|
|
|
|
|
|
rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
|
|
|
|
0, 0, 0, 0, 0, 0);
|
|
|
|
if (rc != H_SUCCESS) {
|
|
|
|
free_page((unsigned long) buf_page);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt = retbuf[0];
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
cpu = drc_index_to_cpu(buf_page[2*i+1]);
|
|
|
|
if ((cpu_online(cpu) && !activate) ||
|
|
|
|
(!cpu_online(cpu) && activate))
|
|
|
|
s += sprintf(s, "%d,", cpu);
|
|
|
|
}
|
|
|
|
if (s > page) { /* Something to show */
|
|
|
|
s--; /* Suppress last comma */
|
|
|
|
s += sprintf(s, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
free_page((unsigned long) buf_page);
|
|
|
|
return s-page;
|
|
|
|
}
|
|
|
|
|
2011-12-22 06:29:42 +08:00
|
|
|
static ssize_t get_best_energy_data(struct device *dev,
|
2010-10-06 16:37:09 +08:00
|
|
|
char *page, int activate)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
|
|
|
|
unsigned long flags = 0;
|
|
|
|
|
|
|
|
flags = FLAGS_MODE2;
|
|
|
|
if (activate)
|
|
|
|
flags |= FLAGS_ACTIVATE;
|
|
|
|
|
|
|
|
rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
|
|
|
|
cpu_to_drc_index(dev->id),
|
|
|
|
0, 0, 0, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
if (rc != H_SUCCESS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return sprintf(page, "%lu\n", retbuf[1] >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapper functions */
|
|
|
|
|
2011-12-22 06:29:42 +08:00
|
|
|
static ssize_t cpu_activate_hint_list_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *page)
|
2010-10-06 16:37:09 +08:00
|
|
|
{
|
|
|
|
return get_best_energy_list(page, 1);
|
|
|
|
}
|
|
|
|
|
2011-12-22 06:29:42 +08:00
|
|
|
static ssize_t cpu_deactivate_hint_list_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *page)
|
2010-10-06 16:37:09 +08:00
|
|
|
{
|
|
|
|
return get_best_energy_list(page, 0);
|
|
|
|
}
|
|
|
|
|
2011-12-22 06:29:42 +08:00
|
|
|
static ssize_t percpu_activate_hint_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *page)
|
2010-10-06 16:37:09 +08:00
|
|
|
{
|
|
|
|
return get_best_energy_data(dev, page, 1);
|
|
|
|
}
|
|
|
|
|
2011-12-22 06:29:42 +08:00
|
|
|
static ssize_t percpu_deactivate_hint_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *page)
|
2010-10-06 16:37:09 +08:00
|
|
|
{
|
|
|
|
return get_best_energy_data(dev, page, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create sysfs interface:
|
|
|
|
* /sys/devices/system/cpu/pseries_activate_hint_list
|
|
|
|
* /sys/devices/system/cpu/pseries_deactivate_hint_list
|
|
|
|
* Comma separated list of cpus to activate or deactivate
|
|
|
|
* /sys/devices/system/cpu/cpuN/pseries_activate_hint
|
|
|
|
* /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
|
|
|
|
* Per-cpu value of the hint
|
|
|
|
*/
|
|
|
|
|
2016-05-18 09:16:49 +08:00
|
|
|
static struct device_attribute attr_cpu_activate_hint_list =
|
2011-12-22 06:29:42 +08:00
|
|
|
__ATTR(pseries_activate_hint_list, 0444,
|
2010-10-06 16:37:09 +08:00
|
|
|
cpu_activate_hint_list_show, NULL);
|
|
|
|
|
2016-05-18 09:16:49 +08:00
|
|
|
static struct device_attribute attr_cpu_deactivate_hint_list =
|
2011-12-22 06:29:42 +08:00
|
|
|
__ATTR(pseries_deactivate_hint_list, 0444,
|
2010-10-06 16:37:09 +08:00
|
|
|
cpu_deactivate_hint_list_show, NULL);
|
|
|
|
|
2016-05-18 09:16:49 +08:00
|
|
|
static struct device_attribute attr_percpu_activate_hint =
|
2011-12-22 06:29:42 +08:00
|
|
|
__ATTR(pseries_activate_hint, 0444,
|
2010-10-06 16:37:09 +08:00
|
|
|
percpu_activate_hint_show, NULL);
|
|
|
|
|
2016-05-18 09:16:49 +08:00
|
|
|
static struct device_attribute attr_percpu_deactivate_hint =
|
2011-12-22 06:29:42 +08:00
|
|
|
__ATTR(pseries_deactivate_hint, 0444,
|
2010-10-06 16:37:09 +08:00
|
|
|
percpu_deactivate_hint_show, NULL);
|
|
|
|
|
|
|
|
static int __init pseries_energy_init(void)
|
|
|
|
{
|
|
|
|
int cpu, err;
|
2011-12-22 06:29:42 +08:00
|
|
|
struct device *cpu_dev;
|
2010-10-06 16:37:09 +08:00
|
|
|
|
2017-07-21 09:16:44 +08:00
|
|
|
if (!firmware_has_feature(FW_FEATURE_BEST_ENERGY))
|
|
|
|
return 0; /* H_BEST_ENERGY hcall not supported */
|
|
|
|
|
2010-10-06 16:37:09 +08:00
|
|
|
/* Create the sysfs files */
|
2011-12-22 06:29:42 +08:00
|
|
|
err = device_create_file(cpu_subsys.dev_root,
|
|
|
|
&attr_cpu_activate_hint_list);
|
2010-10-06 16:37:09 +08:00
|
|
|
if (!err)
|
2011-12-22 06:29:42 +08:00
|
|
|
err = device_create_file(cpu_subsys.dev_root,
|
|
|
|
&attr_cpu_deactivate_hint_list);
|
2010-10-06 16:37:09 +08:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
for_each_possible_cpu(cpu) {
|
2011-12-22 06:29:42 +08:00
|
|
|
cpu_dev = get_cpu_device(cpu);
|
|
|
|
err = device_create_file(cpu_dev,
|
|
|
|
&attr_percpu_activate_hint);
|
2010-10-06 16:37:09 +08:00
|
|
|
if (err)
|
|
|
|
break;
|
2011-12-22 06:29:42 +08:00
|
|
|
err = device_create_file(cpu_dev,
|
|
|
|
&attr_percpu_deactivate_hint);
|
2010-10-06 16:37:09 +08:00
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
sysfs_entries = 1; /* Removed entries on cleanup */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit pseries_energy_cleanup(void)
|
|
|
|
{
|
|
|
|
int cpu;
|
2011-12-22 06:29:42 +08:00
|
|
|
struct device *cpu_dev;
|
2010-10-06 16:37:09 +08:00
|
|
|
|
|
|
|
if (!sysfs_entries)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Remove the sysfs files */
|
2011-12-22 06:29:42 +08:00
|
|
|
device_remove_file(cpu_subsys.dev_root, &attr_cpu_activate_hint_list);
|
|
|
|
device_remove_file(cpu_subsys.dev_root, &attr_cpu_deactivate_hint_list);
|
2010-10-06 16:37:09 +08:00
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
2011-12-22 06:29:42 +08:00
|
|
|
cpu_dev = get_cpu_device(cpu);
|
|
|
|
sysfs_remove_file(&cpu_dev->kobj,
|
2010-10-06 16:37:09 +08:00
|
|
|
&attr_percpu_activate_hint.attr);
|
2011-12-22 06:29:42 +08:00
|
|
|
sysfs_remove_file(&cpu_dev->kobj,
|
2010-10-06 16:37:09 +08:00
|
|
|
&attr_percpu_deactivate_hint.attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(pseries_energy_init);
|
|
|
|
module_exit(pseries_energy_cleanup);
|
|
|
|
MODULE_DESCRIPTION("Driver for pSeries platform energy management");
|
|
|
|
MODULE_AUTHOR("Vaidyanathan Srinivasan");
|
|
|
|
MODULE_LICENSE("GPL");
|