Commit b75394c1 authored by Naveen Krishna Chatradhi's avatar Naveen Krishna Chatradhi Committed by Guenter Roeck

hwmon: (amd_energy) Improve the accumulation logic

Factor out the common code in the accumulation functions for core and
socket accumulation.

While at it, handle the return value of the amd_create_sensor() function.
Signed-off-by: default avatarNaveen Krishna Chatradhi <nchatrad@amd.com>
Link: https://lore.kernel.org/r/20200929105322.8919-4-nchatrad@amd.comSigned-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
parent 514db2b4
...@@ -74,108 +74,67 @@ static void get_energy_units(struct amd_energy_data *data) ...@@ -74,108 +74,67 @@ static void get_energy_units(struct amd_energy_data *data)
data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8; data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;
} }
static void accumulate_socket_delta(struct amd_energy_data *data, static void accumulate_delta(struct amd_energy_data *data,
int sock, int cpu) int channel, int cpu, u32 reg)
{ {
struct sensor_accumulator *s_accum; struct sensor_accumulator *accum;
u64 input; u64 input;
mutex_lock(&data->lock); mutex_lock(&data->lock);
rdmsrl_safe_on_cpu(cpu, ENERGY_PKG_MSR, &input); rdmsrl_safe_on_cpu(cpu, reg, &input);
input &= AMD_ENERGY_MASK; input &= AMD_ENERGY_MASK;
s_accum = &data->accums[data->nr_cpus + sock]; accum = &data->accums[channel];
if (input >= s_accum->prev_value) if (input >= accum->prev_value)
s_accum->energy_ctr += accum->energy_ctr +=
input - s_accum->prev_value; input - accum->prev_value;
else else
s_accum->energy_ctr += UINT_MAX - accum->energy_ctr += UINT_MAX -
s_accum->prev_value + input; accum->prev_value + input;
s_accum->prev_value = input; accum->prev_value = input;
mutex_unlock(&data->lock); mutex_unlock(&data->lock);
} }
static void accumulate_core_delta(struct amd_energy_data *data) static void read_accumulate(struct amd_energy_data *data)
{ {
struct sensor_accumulator *c_accum; int sock, scpu, cpu;
u64 input;
int cpu; for (sock = 0; sock < data->nr_socks; sock++) {
scpu = cpumask_first_and(cpu_online_mask,
cpumask_of_node(sock));
accumulate_delta(data, data->nr_cpus + sock,
scpu, ENERGY_PKG_MSR);
}
mutex_lock(&data->lock);
if (data->core_id >= data->nr_cpus) if (data->core_id >= data->nr_cpus)
data->core_id = 0; data->core_id = 0;
cpu = data->core_id; cpu = data->core_id;
if (cpu_online(cpu))
accumulate_delta(data, cpu, cpu, ENERGY_CORE_MSR);
if (!cpu_online(cpu))
goto out;
rdmsrl_safe_on_cpu(cpu, ENERGY_CORE_MSR, &input);
input &= AMD_ENERGY_MASK;
c_accum = &data->accums[cpu];
if (input >= c_accum->prev_value)
c_accum->energy_ctr +=
input - c_accum->prev_value;
else
c_accum->energy_ctr += UINT_MAX -
c_accum->prev_value + input;
c_accum->prev_value = input;
out:
data->core_id++; data->core_id++;
mutex_unlock(&data->lock);
}
static void read_accumulate(struct amd_energy_data *data)
{
int sock;
for (sock = 0; sock < data->nr_socks; sock++) {
int cpu;
cpu = cpumask_first_and(cpu_online_mask,
cpumask_of_node(sock));
accumulate_socket_delta(data, sock, cpu);
}
accumulate_core_delta(data);
} }
static void amd_add_delta(struct amd_energy_data *data, int ch, static void amd_add_delta(struct amd_energy_data *data, int ch,
int cpu, long *val, bool is_core) int cpu, long *val, u32 reg)
{ {
struct sensor_accumulator *s_accum, *c_accum; struct sensor_accumulator *accum;
u64 input; u64 input;
mutex_lock(&data->lock); mutex_lock(&data->lock);
if (!is_core) { rdmsrl_safe_on_cpu(cpu, reg, &input);
rdmsrl_safe_on_cpu(cpu, ENERGY_PKG_MSR, &input); input &= AMD_ENERGY_MASK;
input &= AMD_ENERGY_MASK;
s_accum = &data->accums[ch];
if (input >= s_accum->prev_value)
input += s_accum->energy_ctr -
s_accum->prev_value;
else
input += UINT_MAX - s_accum->prev_value +
s_accum->energy_ctr;
} else {
rdmsrl_safe_on_cpu(cpu, ENERGY_CORE_MSR, &input);
input &= AMD_ENERGY_MASK;
c_accum = &data->accums[ch]; accum = &data->accums[ch];
if (input >= c_accum->prev_value) if (input >= accum->prev_value)
input += c_accum->energy_ctr - input += accum->energy_ctr -
c_accum->prev_value; accum->prev_value;
else else
input += UINT_MAX - c_accum->prev_value + input += UINT_MAX - accum->prev_value +
c_accum->energy_ctr; accum->energy_ctr;
}
/* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */ /* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */
*val = div64_ul(input * 1000000UL, BIT(data->energy_units)); *val = div64_ul(input * 1000000UL, BIT(data->energy_units));
...@@ -188,20 +147,22 @@ static int amd_energy_read(struct device *dev, ...@@ -188,20 +147,22 @@ static int amd_energy_read(struct device *dev,
u32 attr, int channel, long *val) u32 attr, int channel, long *val)
{ {
struct amd_energy_data *data = dev_get_drvdata(dev); struct amd_energy_data *data = dev_get_drvdata(dev);
u32 reg;
int cpu; int cpu;
if (channel >= data->nr_cpus) { if (channel >= data->nr_cpus) {
cpu = cpumask_first_and(cpu_online_mask, cpu = cpumask_first_and(cpu_online_mask,
cpumask_of_node cpumask_of_node
(channel - data->nr_cpus)); (channel - data->nr_cpus));
amd_add_delta(data, channel, cpu, val, false); reg = ENERGY_PKG_MSR;
} else { } else {
cpu = channel; cpu = channel;
if (!cpu_online(cpu)) if (!cpu_online(cpu))
return -ENODEV; return -ENODEV;
amd_add_delta(data, channel, cpu, val, true); reg = ENERGY_CORE_MSR;
} }
amd_add_delta(data, channel, cpu, val, reg);
return 0; return 0;
} }
...@@ -242,7 +203,7 @@ static const struct hwmon_ops amd_energy_ops = { ...@@ -242,7 +203,7 @@ static const struct hwmon_ops amd_energy_ops = {
static int amd_create_sensor(struct device *dev, static int amd_create_sensor(struct device *dev,
struct amd_energy_data *data, struct amd_energy_data *data,
u8 type, u32 config) enum hwmon_sensor_types type, u32 config)
{ {
struct hwmon_channel_info *info = &data->energy_info; struct hwmon_channel_info *info = &data->energy_info;
struct sensor_accumulator *accums; struct sensor_accumulator *accums;
...@@ -301,6 +262,7 @@ static int amd_energy_probe(struct platform_device *pdev) ...@@ -301,6 +262,7 @@ static int amd_energy_probe(struct platform_device *pdev)
struct device *hwmon_dev; struct device *hwmon_dev;
struct amd_energy_data *data; struct amd_energy_data *data;
struct device *dev = &pdev->dev; struct device *dev = &pdev->dev;
int ret;
data = devm_kzalloc(dev, data = devm_kzalloc(dev,
sizeof(struct amd_energy_data), GFP_KERNEL); sizeof(struct amd_energy_data), GFP_KERNEL);
...@@ -313,8 +275,10 @@ static int amd_energy_probe(struct platform_device *pdev) ...@@ -313,8 +275,10 @@ static int amd_energy_probe(struct platform_device *pdev)
dev_set_drvdata(dev, data); dev_set_drvdata(dev, data);
/* Populate per-core energy reporting */ /* Populate per-core energy reporting */
data->info[0] = &data->energy_info; data->info[0] = &data->energy_info;
amd_create_sensor(dev, data, hwmon_energy, ret = amd_create_sensor(dev, data, hwmon_energy,
HWMON_E_INPUT | HWMON_E_LABEL); HWMON_E_INPUT | HWMON_E_LABEL);
if (ret)
return ret;
mutex_init(&data->lock); mutex_init(&data->lock);
get_energy_units(data); get_energy_units(data);
...@@ -335,9 +299,6 @@ static int amd_energy_probe(struct platform_device *pdev) ...@@ -335,9 +299,6 @@ static int amd_energy_probe(struct platform_device *pdev)
data->wrap_accumulate = kthread_run(energy_accumulator, data, data->wrap_accumulate = kthread_run(energy_accumulator, data,
"%s", dev_name(hwmon_dev)); "%s", dev_name(hwmon_dev));
if (IS_ERR(data->wrap_accumulate))
return PTR_ERR(data->wrap_accumulate);
return PTR_ERR_OR_ZERO(data->wrap_accumulate); return PTR_ERR_OR_ZERO(data->wrap_accumulate);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment