Commit 8de0ae8c authored by Robert Lippert's avatar Robert Lippert Committed by Greg Kroah-Hartman

hwmon: (pmbus) Use 64bit math for DIRECT format values


[ Upstream commit bd467e4e ]

Power values in the 100s of watt range can easily blow past
32bit math limits when processing everything in microwatts.

Use 64bit math instead to avoid these issues on common 32bit ARM
BMC platforms.

Fixes: 442aba78 ("hwmon: PMBus device driver")
Signed-off-by: default avatarRobert Lippert <rlippert@google.com>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarSasha Levin <alexander.levin@microsoft.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 17375b27
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
*/ */
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/math64.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/err.h> #include <linux/err.h>
...@@ -476,8 +477,8 @@ static long pmbus_reg2data_linear(struct pmbus_data *data, ...@@ -476,8 +477,8 @@ static long pmbus_reg2data_linear(struct pmbus_data *data,
static long pmbus_reg2data_direct(struct pmbus_data *data, static long pmbus_reg2data_direct(struct pmbus_data *data,
struct pmbus_sensor *sensor) struct pmbus_sensor *sensor)
{ {
long val = (s16) sensor->data; s64 b, val = (s16)sensor->data;
long m, b, R; s32 m, R;
m = data->info->m[sensor->class]; m = data->info->m[sensor->class];
b = data->info->b[sensor->class]; b = data->info->b[sensor->class];
...@@ -505,11 +506,12 @@ static long pmbus_reg2data_direct(struct pmbus_data *data, ...@@ -505,11 +506,12 @@ static long pmbus_reg2data_direct(struct pmbus_data *data,
R--; R--;
} }
while (R < 0) { while (R < 0) {
val = DIV_ROUND_CLOSEST(val, 10); val = div_s64(val + 5LL, 10L); /* round closest */
R++; R++;
} }
return (val - b) / m; val = div_s64(val - b, m);
return clamp_val(val, LONG_MIN, LONG_MAX);
} }
/* /*
...@@ -629,7 +631,8 @@ static u16 pmbus_data2reg_linear(struct pmbus_data *data, ...@@ -629,7 +631,8 @@ static u16 pmbus_data2reg_linear(struct pmbus_data *data,
static u16 pmbus_data2reg_direct(struct pmbus_data *data, static u16 pmbus_data2reg_direct(struct pmbus_data *data,
struct pmbus_sensor *sensor, long val) struct pmbus_sensor *sensor, long val)
{ {
long m, b, R; s64 b, val64 = val;
s32 m, R;
m = data->info->m[sensor->class]; m = data->info->m[sensor->class];
b = data->info->b[sensor->class]; b = data->info->b[sensor->class];
...@@ -646,18 +649,18 @@ static u16 pmbus_data2reg_direct(struct pmbus_data *data, ...@@ -646,18 +649,18 @@ static u16 pmbus_data2reg_direct(struct pmbus_data *data,
R -= 3; /* Adjust R and b for data in milli-units */ R -= 3; /* Adjust R and b for data in milli-units */
b *= 1000; b *= 1000;
} }
val = val * m + b; val64 = val64 * m + b;
while (R > 0) { while (R > 0) {
val *= 10; val64 *= 10;
R--; R--;
} }
while (R < 0) { while (R < 0) {
val = DIV_ROUND_CLOSEST(val, 10); val64 = div_s64(val64 + 5LL, 10L); /* round closest */
R++; R++;
} }
return val; return (u16)clamp_val(val64, S16_MIN, S16_MAX);
} }
static u16 pmbus_data2reg_vid(struct pmbus_data *data, static u16 pmbus_data2reg_vid(struct pmbus_data *data,
......
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