Commit b42ff046 authored by Badal Nilawar's avatar Badal Nilawar Committed by Rodrigo Vivi

drm/xe/hwmon: Add kernel doc and refactor xe hwmon

Add kernel doc and refactor some of the hwmon functions, there is
no functionality change.

Cc: Anshuman Gupta <anshuman.gupta@intel.com>
Cc: Ashutosh Dixit <ashutosh.dixit@intel.com>
Signed-off-by: default avatarBadal Nilawar <badal.nilawar@intel.com>
Reviewed-by: default avatarAnshuman Gupta <anshuman.gupta@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20231030115618.1382200-2-badal.nilawar@intel.comSigned-off-by: default avatarRodrigo Vivi <rodrigo.vivi@intel.com>
parent 503a6f4e
...@@ -26,9 +26,9 @@ enum xe_hwmon_reg { ...@@ -26,9 +26,9 @@ enum xe_hwmon_reg {
}; };
enum xe_hwmon_reg_operation { enum xe_hwmon_reg_operation {
REG_READ, REG_READ32,
REG_WRITE, REG_RMW32,
REG_RMW, REG_READ64,
}; };
/* /*
...@@ -39,18 +39,32 @@ enum xe_hwmon_reg_operation { ...@@ -39,18 +39,32 @@ enum xe_hwmon_reg_operation {
#define SF_VOLTAGE 1000 /* millivolts */ #define SF_VOLTAGE 1000 /* millivolts */
#define SF_ENERGY 1000000 /* microjoules */ #define SF_ENERGY 1000000 /* microjoules */
/**
* struct xe_hwmon_energy_info - to accumulate energy
*/
struct xe_hwmon_energy_info { struct xe_hwmon_energy_info {
/** @reg_val_prev: previous energy reg val */
u32 reg_val_prev; u32 reg_val_prev;
long accum_energy; /* Accumulated energy for energy1_input */ /** @accum_energy: accumulated energy */
long accum_energy;
}; };
/**
* struct xe_hwmon - xe hwmon data structure
*/
struct xe_hwmon { struct xe_hwmon {
/** @hwmon_dev: hwmon device for xe */
struct device *hwmon_dev; struct device *hwmon_dev;
/** @gt: primary gt */
struct xe_gt *gt; struct xe_gt *gt;
struct mutex hwmon_lock; /* rmw operations*/ /** @hwmon_lock: lock for rmw operations */
struct mutex hwmon_lock;
/** @scl_shift_power: pkg power unit */
int scl_shift_power; int scl_shift_power;
/** @scl_shift_energy: pkg energy unit */
int scl_shift_energy; int scl_shift_energy;
struct xe_hwmon_energy_info ei; /* Energy info for energy1_input */ /** @ei: Energy info for energy1_input */
struct xe_hwmon_energy_info ei;
}; };
static u32 xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg) static u32 xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg)
...@@ -95,8 +109,8 @@ static u32 xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg) ...@@ -95,8 +109,8 @@ static u32 xe_hwmon_get_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg)
return reg.raw; return reg.raw;
} }
static int xe_hwmon_process_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg, static void xe_hwmon_process_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_reg,
enum xe_hwmon_reg_operation operation, u32 *value, enum xe_hwmon_reg_operation operation, u64 *value,
u32 clr, u32 set) u32 clr, u32 set)
{ {
struct xe_reg reg; struct xe_reg reg;
...@@ -104,40 +118,25 @@ static int xe_hwmon_process_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_ ...@@ -104,40 +118,25 @@ static int xe_hwmon_process_reg(struct xe_hwmon *hwmon, enum xe_hwmon_reg hwmon_
reg.raw = xe_hwmon_get_reg(hwmon, hwmon_reg); reg.raw = xe_hwmon_get_reg(hwmon, hwmon_reg);
if (!reg.raw) if (!reg.raw)
return -EOPNOTSUPP; return;
switch (operation) { switch (operation) {
case REG_READ: case REG_READ32:
*value = xe_mmio_read32(hwmon->gt, reg); *value = xe_mmio_read32(hwmon->gt, reg);
return 0; break;
case REG_WRITE: case REG_RMW32:
xe_mmio_write32(hwmon->gt, reg, *value);
return 0;
case REG_RMW:
*value = xe_mmio_rmw32(hwmon->gt, reg, clr, set); *value = xe_mmio_rmw32(hwmon->gt, reg, clr, set);
return 0; break;
case REG_READ64:
*value = xe_mmio_read64_2x32(hwmon->gt, reg);
break;
default: default:
drm_warn(&gt_to_xe(hwmon->gt)->drm, "Invalid xe hwmon reg operation: %d\n", drm_warn(&gt_to_xe(hwmon->gt)->drm, "Invalid xe hwmon reg operation: %d\n",
operation); operation);
return -EOPNOTSUPP; break;
} }
} }
static int xe_hwmon_process_reg_read64(struct xe_hwmon *hwmon,
enum xe_hwmon_reg hwmon_reg, u64 *value)
{
struct xe_reg reg;
reg.raw = xe_hwmon_get_reg(hwmon, hwmon_reg);
if (!reg.raw)
return -EOPNOTSUPP;
*value = xe_mmio_read64_2x32(hwmon->gt, reg);
return 0;
}
#define PL1_DISABLE 0 #define PL1_DISABLE 0
/* /*
...@@ -146,42 +145,39 @@ static int xe_hwmon_process_reg_read64(struct xe_hwmon *hwmon, ...@@ -146,42 +145,39 @@ static int xe_hwmon_process_reg_read64(struct xe_hwmon *hwmon,
* same pattern for sysfs, allow arbitrary PL1 limits to be set but display * same pattern for sysfs, allow arbitrary PL1 limits to be set but display
* clamped values when read. * clamped values when read.
*/ */
static int xe_hwmon_power_max_read(struct xe_hwmon *hwmon, long *value) static void xe_hwmon_power_max_read(struct xe_hwmon *hwmon, long *value)
{ {
u32 reg_val; u64 reg_val, min, max;
u64 reg_val64, min, max;
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_READ, &reg_val, 0, 0); xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_READ32, &reg_val, 0, 0);
/* Check if PL1 limit is disabled */ /* Check if PL1 limit is disabled */
if (!(reg_val & PKG_PWR_LIM_1_EN)) { if (!(reg_val & PKG_PWR_LIM_1_EN)) {
*value = PL1_DISABLE; *value = PL1_DISABLE;
return 0; return;
} }
reg_val = REG_FIELD_GET(PKG_PWR_LIM_1, reg_val); reg_val = REG_FIELD_GET(PKG_PWR_LIM_1, reg_val);
*value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power); *value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power);
xe_hwmon_process_reg_read64(hwmon, REG_PKG_POWER_SKU, &reg_val64); xe_hwmon_process_reg(hwmon, REG_PKG_POWER_SKU, REG_READ64, &reg_val, 0, 0);
min = REG_FIELD_GET(PKG_MIN_PWR, reg_val64); min = REG_FIELD_GET(PKG_MIN_PWR, reg_val);
min = mul_u64_u32_shr(min, SF_POWER, hwmon->scl_shift_power); min = mul_u64_u32_shr(min, SF_POWER, hwmon->scl_shift_power);
max = REG_FIELD_GET(PKG_MAX_PWR, reg_val64); max = REG_FIELD_GET(PKG_MAX_PWR, reg_val);
max = mul_u64_u32_shr(max, SF_POWER, hwmon->scl_shift_power); max = mul_u64_u32_shr(max, SF_POWER, hwmon->scl_shift_power);
if (min && max) if (min && max)
*value = clamp_t(u64, *value, min, max); *value = clamp_t(u64, *value, min, max);
return 0;
} }
static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, long value) static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, long value)
{ {
u32 reg_val; u64 reg_val;
/* Disable PL1 limit and verify, as limit cannot be disabled on all platforms */ /* Disable PL1 limit and verify, as limit cannot be disabled on all platforms */
if (value == PL1_DISABLE) { if (value == PL1_DISABLE) {
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_RMW, &reg_val, xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_RMW32, &reg_val,
PKG_PWR_LIM_1_EN, 0); PKG_PWR_LIM_1_EN, 0);
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_READ, &reg_val, xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_READ32, &reg_val,
PKG_PWR_LIM_1_EN, 0); PKG_PWR_LIM_1_EN, 0);
if (reg_val & PKG_PWR_LIM_1_EN) if (reg_val & PKG_PWR_LIM_1_EN)
...@@ -192,21 +188,19 @@ static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, long value) ...@@ -192,21 +188,19 @@ static int xe_hwmon_power_max_write(struct xe_hwmon *hwmon, long value)
reg_val = DIV_ROUND_CLOSEST_ULL((u64)value << hwmon->scl_shift_power, SF_POWER); reg_val = DIV_ROUND_CLOSEST_ULL((u64)value << hwmon->scl_shift_power, SF_POWER);
reg_val = PKG_PWR_LIM_1_EN | REG_FIELD_PREP(PKG_PWR_LIM_1, reg_val); reg_val = PKG_PWR_LIM_1_EN | REG_FIELD_PREP(PKG_PWR_LIM_1, reg_val);
xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_RMW, &reg_val, xe_hwmon_process_reg(hwmon, REG_PKG_RAPL_LIMIT, REG_RMW32, &reg_val,
PKG_PWR_LIM_1_EN | PKG_PWR_LIM_1, reg_val); PKG_PWR_LIM_1_EN | PKG_PWR_LIM_1, reg_val);
return 0; return 0;
} }
static int xe_hwmon_power_rated_max_read(struct xe_hwmon *hwmon, long *value) static void xe_hwmon_power_rated_max_read(struct xe_hwmon *hwmon, long *value)
{ {
u32 reg_val; u64 reg_val;
xe_hwmon_process_reg(hwmon, REG_PKG_POWER_SKU, REG_READ, &reg_val, 0, 0); xe_hwmon_process_reg(hwmon, REG_PKG_POWER_SKU, REG_READ32, &reg_val, 0, 0);
reg_val = REG_FIELD_GET(PKG_TDP, reg_val); reg_val = REG_FIELD_GET(PKG_TDP, reg_val);
*value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power); *value = mul_u64_u32_shr(reg_val, SF_POWER, hwmon->scl_shift_power);
return 0;
} }
/* /*
...@@ -233,13 +227,11 @@ static void ...@@ -233,13 +227,11 @@ static void
xe_hwmon_energy_get(struct xe_hwmon *hwmon, long *energy) xe_hwmon_energy_get(struct xe_hwmon *hwmon, long *energy)
{ {
struct xe_hwmon_energy_info *ei = &hwmon->ei; struct xe_hwmon_energy_info *ei = &hwmon->ei;
u32 reg_val; u64 reg_val;
xe_device_mem_access_get(gt_to_xe(hwmon->gt));
mutex_lock(&hwmon->hwmon_lock); mutex_lock(&hwmon->hwmon_lock);
xe_hwmon_process_reg(hwmon, REG_PKG_ENERGY_STATUS, REG_READ, xe_hwmon_process_reg(hwmon, REG_PKG_ENERGY_STATUS, REG_READ32,
&reg_val, 0, 0); &reg_val, 0, 0);
if (reg_val >= ei->reg_val_prev) if (reg_val >= ei->reg_val_prev)
...@@ -253,8 +245,6 @@ xe_hwmon_energy_get(struct xe_hwmon *hwmon, long *energy) ...@@ -253,8 +245,6 @@ xe_hwmon_energy_get(struct xe_hwmon *hwmon, long *energy)
hwmon->scl_shift_energy); hwmon->scl_shift_energy);
mutex_unlock(&hwmon->hwmon_lock); mutex_unlock(&hwmon->hwmon_lock);
xe_device_mem_access_put(gt_to_xe(hwmon->gt));
} }
static const struct hwmon_channel_info *hwmon_info[] = { static const struct hwmon_channel_info *hwmon_info[] = {
...@@ -284,16 +274,39 @@ static int xe_hwmon_pcode_write_i1(struct xe_gt *gt, u32 uval) ...@@ -284,16 +274,39 @@ static int xe_hwmon_pcode_write_i1(struct xe_gt *gt, u32 uval)
uval); uval);
} }
static int xe_hwmon_get_voltage(struct xe_hwmon *hwmon, long *value) static int xe_hwmon_power_curr_crit_read(struct xe_hwmon *hwmon, long *value, u32 scale_factor)
{
int ret;
u32 uval;
ret = xe_hwmon_pcode_read_i1(hwmon->gt, &uval);
if (ret)
return ret;
*value = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
scale_factor, POWER_SETUP_I1_SHIFT);
return ret;
}
static int xe_hwmon_power_curr_crit_write(struct xe_hwmon *hwmon, long value, u32 scale_factor)
{
int ret;
u32 uval;
uval = DIV_ROUND_CLOSEST_ULL(value << POWER_SETUP_I1_SHIFT, scale_factor);
ret = xe_hwmon_pcode_write_i1(hwmon->gt, uval);
return ret;
}
static void xe_hwmon_get_voltage(struct xe_hwmon *hwmon, long *value)
{ {
u32 reg_val; u64 reg_val;
xe_hwmon_process_reg(hwmon, REG_GT_PERF_STATUS, xe_hwmon_process_reg(hwmon, REG_GT_PERF_STATUS,
REG_READ, &reg_val, 0, 0); REG_READ32, &reg_val, 0, 0);
/* HW register value in units of 2.5 millivolt */ /* HW register value in units of 2.5 millivolt */
*value = DIV_ROUND_CLOSEST(REG_FIELD_GET(VOLTAGE_MASK, reg_val) * 2500, SF_VOLTAGE); *value = DIV_ROUND_CLOSEST(REG_FIELD_GET(VOLTAGE_MASK, reg_val) * 2500, SF_VOLTAGE);
return 0;
} }
static umode_t static umode_t
...@@ -317,23 +330,15 @@ xe_hwmon_power_is_visible(struct xe_hwmon *hwmon, u32 attr, int chan) ...@@ -317,23 +330,15 @@ xe_hwmon_power_is_visible(struct xe_hwmon *hwmon, u32 attr, int chan)
static int static int
xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int chan, long *val) xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int chan, long *val)
{ {
int ret;
u32 uval;
switch (attr) { switch (attr) {
case hwmon_power_max: case hwmon_power_max:
return xe_hwmon_power_max_read(hwmon, val); xe_hwmon_power_max_read(hwmon, val);
return 0;
case hwmon_power_rated_max: case hwmon_power_rated_max:
return xe_hwmon_power_rated_max_read(hwmon, val); xe_hwmon_power_rated_max_read(hwmon, val);
case hwmon_power_crit:
ret = xe_hwmon_pcode_read_i1(hwmon->gt, &uval);
if (ret)
return ret;
if (!(uval & POWER_SETUP_I1_WATTS))
return -ENODEV;
*val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
SF_POWER, POWER_SETUP_I1_SHIFT);
return 0; return 0;
case hwmon_power_crit:
return xe_hwmon_power_curr_crit_read(hwmon, val, SF_POWER);
default: default:
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
...@@ -342,14 +347,11 @@ xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int chan, long *val) ...@@ -342,14 +347,11 @@ xe_hwmon_power_read(struct xe_hwmon *hwmon, u32 attr, int chan, long *val)
static int static int
xe_hwmon_power_write(struct xe_hwmon *hwmon, u32 attr, int chan, long val) xe_hwmon_power_write(struct xe_hwmon *hwmon, u32 attr, int chan, long val)
{ {
u32 uval;
switch (attr) { switch (attr) {
case hwmon_power_max: case hwmon_power_max:
return xe_hwmon_power_max_write(hwmon, val); return xe_hwmon_power_max_write(hwmon, val);
case hwmon_power_crit: case hwmon_power_crit:
uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_POWER); return xe_hwmon_power_curr_crit_write(hwmon, val, SF_POWER);
return xe_hwmon_pcode_write_i1(hwmon->gt, uval);
default: default:
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
...@@ -372,19 +374,9 @@ xe_hwmon_curr_is_visible(const struct xe_hwmon *hwmon, u32 attr) ...@@ -372,19 +374,9 @@ xe_hwmon_curr_is_visible(const struct xe_hwmon *hwmon, u32 attr)
static int static int
xe_hwmon_curr_read(struct xe_hwmon *hwmon, u32 attr, long *val) xe_hwmon_curr_read(struct xe_hwmon *hwmon, u32 attr, long *val)
{ {
int ret;
u32 uval;
switch (attr) { switch (attr) {
case hwmon_curr_crit: case hwmon_curr_crit:
ret = xe_hwmon_pcode_read_i1(hwmon->gt, &uval); return xe_hwmon_power_curr_crit_read(hwmon, val, SF_CURR);
if (ret)
return ret;
if (uval & POWER_SETUP_I1_WATTS)
return -ENODEV;
*val = mul_u64_u32_shr(REG_FIELD_GET(POWER_SETUP_I1_DATA_MASK, uval),
SF_CURR, POWER_SETUP_I1_SHIFT);
return 0;
default: default:
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
...@@ -393,12 +385,9 @@ xe_hwmon_curr_read(struct xe_hwmon *hwmon, u32 attr, long *val) ...@@ -393,12 +385,9 @@ xe_hwmon_curr_read(struct xe_hwmon *hwmon, u32 attr, long *val)
static int static int
xe_hwmon_curr_write(struct xe_hwmon *hwmon, u32 attr, long val) xe_hwmon_curr_write(struct xe_hwmon *hwmon, u32 attr, long val)
{ {
u32 uval;
switch (attr) { switch (attr) {
case hwmon_curr_crit: case hwmon_curr_crit:
uval = DIV_ROUND_CLOSEST_ULL(val << POWER_SETUP_I1_SHIFT, SF_CURR); return xe_hwmon_power_curr_crit_write(hwmon, val, SF_CURR);
return xe_hwmon_pcode_write_i1(hwmon->gt, uval);
default: default:
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
...@@ -418,21 +407,13 @@ xe_hwmon_in_is_visible(struct xe_hwmon *hwmon, u32 attr) ...@@ -418,21 +407,13 @@ xe_hwmon_in_is_visible(struct xe_hwmon *hwmon, u32 attr)
static int static int
xe_hwmon_in_read(struct xe_hwmon *hwmon, u32 attr, long *val) xe_hwmon_in_read(struct xe_hwmon *hwmon, u32 attr, long *val)
{ {
int ret;
xe_device_mem_access_get(gt_to_xe(hwmon->gt));
switch (attr) { switch (attr) {
case hwmon_in_input: case hwmon_in_input:
ret = xe_hwmon_get_voltage(hwmon, val); xe_hwmon_get_voltage(hwmon, val);
break; return 0;
default: default:
ret = -EOPNOTSUPP; return -EOPNOTSUPP;
} }
xe_device_mem_access_put(gt_to_xe(hwmon->gt));
return ret;
} }
static umode_t static umode_t
...@@ -564,15 +545,15 @@ xe_hwmon_get_preregistration_info(struct xe_device *xe) ...@@ -564,15 +545,15 @@ xe_hwmon_get_preregistration_info(struct xe_device *xe)
{ {
struct xe_hwmon *hwmon = xe->hwmon; struct xe_hwmon *hwmon = xe->hwmon;
long energy; long energy;
u32 val_sku_unit = 0; u64 val_sku_unit = 0;
int ret;
ret = xe_hwmon_process_reg(hwmon, REG_PKG_POWER_SKU_UNIT, REG_READ, &val_sku_unit, 0, 0);
/* /*
* The contents of register PKG_POWER_SKU_UNIT do not change, * The contents of register PKG_POWER_SKU_UNIT do not change,
* so read it once and store the shift values. * so read it once and store the shift values.
*/ */
if (!ret) { if (xe_hwmon_get_reg(hwmon, REG_PKG_POWER_SKU_UNIT)) {
xe_hwmon_process_reg(hwmon, REG_PKG_POWER_SKU_UNIT,
REG_READ32, &val_sku_unit, 0, 0);
hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit); hwmon->scl_shift_power = REG_FIELD_GET(PKG_PWR_UNIT, val_sku_unit);
hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit); hwmon->scl_shift_energy = REG_FIELD_GET(PKG_ENERGY_UNIT, val_sku_unit);
} }
......
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