diff mbox series

[v1] drm/i915/hwmon: expose package temperature

Message ID 20240828044512.2710381-1-raag.jadav@intel.com (mailing list archive)
State Handled Elsewhere
Headers show
Series [v1] drm/i915/hwmon: expose package temperature | expand

Commit Message

Raag Jadav Aug. 28, 2024, 4:45 a.m. UTC
Add hwmon support for temp1_input attribute, which will expose package
temperature in millidegree Celsius. With this in place we can monitor
package temperature using lm-sensors tool.

$ sensors
i915-pci-0300
Adapter: PCI adapter
in0:         990.00 mV
fan1:        1260 RPM
temp1:        +45.0°C
power1:           N/A  (max =  35.00 W)
energy1:      12.62 kJ

Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
Signed-off-by: Raag Jadav <raag.jadav@intel.com>
---
 .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
 drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
 drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
 3 files changed, 51 insertions(+)

Comments

Andy Shevchenko Aug. 28, 2024, 1:59 p.m. UTC | #1
On Wed, Aug 28, 2024 at 10:15:12AM +0530, Raag Jadav wrote:
> Add hwmon support for temp1_input attribute, which will expose package
> temperature in millidegree Celsius. With this in place we can monitor
> package temperature using lm-sensors tool.
> 
> $ sensors
> i915-pci-0300
> Adapter: PCI adapter
> in0:         990.00 mV
> fan1:        1260 RPM
> temp1:        +45.0°C
> power1:           N/A  (max =  35.00 W)
> energy1:      12.62 kJ

...

> +static umode_t
> +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> +{
> +	struct i915_hwmon *hwmon = ddat->hwmon;
> +
> +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> +		return 0444;
> +
> +	return 0;

Just a question (I'm fine with this implementation): is the style in this file
to check for correct cases first and return an err/etc at the end?

> +}
> +
> +static int
> +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> +{
> +	struct i915_hwmon *hwmon = ddat->hwmon;
> +	intel_wakeref_t wakeref;
> +	u32 reg_val;
> +
> +	if (attr == hwmon_temp_input) {
> +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> +
> +		/* HW register value is in degrees, convert to millidegrees. */
> +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> +		return 0;
> +	}

...because here we may drop an indentation level by doing it opposite

	if (x != y)
		return -E...;

	...

> +	return -EOPNOTSUPP;
> +}
Raag Jadav Aug. 29, 2024, 7:50 a.m. UTC | #2
On Wed, Aug 28, 2024 at 04:59:00PM +0300, Andy Shevchenko wrote:
> On Wed, Aug 28, 2024 at 10:15:12AM +0530, Raag Jadav wrote:
> > Add hwmon support for temp1_input attribute, which will expose package
> > temperature in millidegree Celsius. With this in place we can monitor
> > package temperature using lm-sensors tool.
> > 
> > $ sensors
> > i915-pci-0300
> > Adapter: PCI adapter
> > in0:         990.00 mV
> > fan1:        1260 RPM
> > temp1:        +45.0°C
> > power1:           N/A  (max =  35.00 W)
> > energy1:      12.62 kJ
> 
> ...
> 
> > +static umode_t
> > +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> > +{
> > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > +
> > +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> > +		return 0444;
> > +
> > +	return 0;
> 
> Just a question (I'm fine with this implementation): is the style in this file
> to check for correct cases first and return an err/etc at the end?

The convention is to use switch case with err being the default, so I'd say yes.

> > +}
> > +
> > +static int
> > +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> > +{
> > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > +	intel_wakeref_t wakeref;
> > +	u32 reg_val;
> > +
> > +	if (attr == hwmon_temp_input) {
> > +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> > +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> > +
> > +		/* HW register value is in degrees, convert to millidegrees. */
> > +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> > +		return 0;
> > +	}
> 
> ...because here we may drop an indentation level by doing it opposite
> 
> 	if (x != y)
> 		return -E...;
> 

True, but the idea is to allow more cases in the future with minimal changes.

Raag
Nilawar, Badal Sept. 5, 2024, 6:26 a.m. UTC | #3
On 28-08-2024 10:15, Raag Jadav wrote:
> Add hwmon support for temp1_input attribute, which will expose package
> temperature in millidegree Celsius. With this in place we can monitor
> package temperature using lm-sensors tool.
> 
> $ sensors
> i915-pci-0300
> Adapter: PCI adapter
> in0:         990.00 mV
> fan1:        1260 RPM
> temp1:        +45.0°C
> power1:           N/A  (max =  35.00 W)
> energy1:      12.62 kJ
> 
> Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
> Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> ---
>   .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
>   drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
>   drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
>   3 files changed, 51 insertions(+)
> 
> diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> index be4141a7522f..a885e5316d02 100644
> --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
>   Description:	RO. Fan speed of device in RPM.
>   
>   		Only supported for particular Intel i915 graphics platforms.
> +
> +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
> +Date:		November 2024
> +KernelVersion:	6.12
> +Contact:	intel-gfx@lists.freedesktop.org
> +Description:	RO. GPU package temperature in millidegree Celsius.
> +
> +		Only supported for particular Intel i915 graphics platforms.
> diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> index 17d30f6b84b0..9f1a2300510b 100644
> --- a/drivers/gpu/drm/i915/i915_hwmon.c
> +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> @@ -7,6 +7,7 @@
>   #include <linux/hwmon-sysfs.h>
>   #include <linux/jiffies.h>
>   #include <linux/types.h>
> +#include <linux/units.h>
>   
>   #include "i915_drv.h"
>   #include "i915_hwmon.h"
> @@ -32,6 +33,7 @@
>   
>   struct hwm_reg {
>   	i915_reg_t gt_perf_status;
> +	i915_reg_t pkg_temp;
>   	i915_reg_t pkg_power_sku_unit;
>   	i915_reg_t pkg_power_sku;
>   	i915_reg_t pkg_rapl_limit;
> @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
>   };
>   
>   static const struct hwmon_channel_info * const hwm_info[] = {
> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
>   	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
>   	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
>   	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
> @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
>   				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
>   }
>   
> +static umode_t
> +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> +{
> +	struct i915_hwmon *hwmon = ddat->hwmon;
> +
> +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> +		return 0444;
> +
> +	return 0;
> +}
> +
> +static int
> +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> +{
> +	struct i915_hwmon *hwmon = ddat->hwmon;
> +	intel_wakeref_t wakeref;
> +	u32 reg_val;
> +
> +	if (attr == hwmon_temp_input) {
> +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> +
> +		/* HW register value is in degrees, convert to millidegrees. */
> +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> +		return 0;
> +	}
> +
> +	return -EOPNOTSUPP;
> +}
Let's try to have synergy between previous attribute, such as 
hwm_fan_input, and this one.

Regards,
Badal
> +
>   static umode_t
>   hwm_in_is_visible(const struct hwm_drvdata *ddat, u32 attr)
>   {
> @@ -692,6 +725,8 @@ hwm_is_visible(const void *drvdata, enum hwmon_sensor_types type,
>   	struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata;
>   
>   	switch (type) {
> +	case hwmon_temp:
> +		return hwm_temp_is_visible(ddat, attr);
>   	case hwmon_in:
>   		return hwm_in_is_visible(ddat, attr);
>   	case hwmon_power:
> @@ -714,6 +749,8 @@ hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
>   	struct hwm_drvdata *ddat = dev_get_drvdata(dev);
>   
>   	switch (type) {
> +	case hwmon_temp:
> +		return hwm_temp_read(ddat, attr, val);
>   	case hwmon_in:
>   		return hwm_in_read(ddat, attr, val);
>   	case hwmon_power:
> @@ -810,6 +847,7 @@ hwm_get_preregistration_info(struct drm_i915_private *i915)
>   	hwmon->rg.gt_perf_status = GEN12_RPSTAT1;
>   
>   	if (IS_DG1(i915) || IS_DG2(i915)) {
> +		hwmon->rg.pkg_temp = PCU_PACKAGE_TEMPERATURE;
>   		hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT;
>   		hwmon->rg.pkg_power_sku = PCU_PACKAGE_POWER_SKU;
>   		hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT;
> @@ -817,6 +855,7 @@ hwm_get_preregistration_info(struct drm_i915_private *i915)
>   		hwmon->rg.energy_status_tile = INVALID_MMIO_REG;
>   		hwmon->rg.fan_speed = PCU_PWM_FAN_SPEED;
>   	} else {
> +		hwmon->rg.pkg_temp = INVALID_MMIO_REG;
>   		hwmon->rg.pkg_power_sku_unit = INVALID_MMIO_REG;
>   		hwmon->rg.pkg_power_sku = INVALID_MMIO_REG;
>   		hwmon->rg.pkg_rapl_limit = INVALID_MMIO_REG;
> diff --git a/drivers/gpu/drm/i915/intel_mchbar_regs.h b/drivers/gpu/drm/i915/intel_mchbar_regs.h
> index 73900c098d59..dc2477179c3e 100644
> --- a/drivers/gpu/drm/i915/intel_mchbar_regs.h
> +++ b/drivers/gpu/drm/i915/intel_mchbar_regs.h
> @@ -207,6 +207,10 @@
>   #define PCU_PACKAGE_ENERGY_STATUS              _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x593c)
>   
>   #define GEN6_GT_PERF_STATUS			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5948)
> +
> +#define PCU_PACKAGE_TEMPERATURE			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5978)
> +#define   TEMP_MASK				REG_GENMASK(7, 0)
> +
>   #define GEN6_RP_STATE_LIMITS			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5994)
>   #define GEN6_RP_STATE_CAP			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5998)
>   #define   RP0_CAP_MASK				REG_GENMASK(7, 0)
Raag Jadav Sept. 5, 2024, 8:55 a.m. UTC | #4
On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
> 
> 
> On 28-08-2024 10:15, Raag Jadav wrote:
> > Add hwmon support for temp1_input attribute, which will expose package
> > temperature in millidegree Celsius. With this in place we can monitor
> > package temperature using lm-sensors tool.
> > 
> > $ sensors
> > i915-pci-0300
> > Adapter: PCI adapter
> > in0:         990.00 mV
> > fan1:        1260 RPM
> > temp1:        +45.0°C
> > power1:           N/A  (max =  35.00 W)
> > energy1:      12.62 kJ
> > 
> > Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
> > Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> > ---
> >   .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
> >   drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
> >   drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
> >   3 files changed, 51 insertions(+)
> > 
> > diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > index be4141a7522f..a885e5316d02 100644
> > --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
> >   Description:	RO. Fan speed of device in RPM.
> >   		Only supported for particular Intel i915 graphics platforms.
> > +
> > +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
> > +Date:		November 2024
> > +KernelVersion:	6.12
> > +Contact:	intel-gfx@lists.freedesktop.org
> > +Description:	RO. GPU package temperature in millidegree Celsius.
> > +
> > +		Only supported for particular Intel i915 graphics platforms.
> > diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> > index 17d30f6b84b0..9f1a2300510b 100644
> > --- a/drivers/gpu/drm/i915/i915_hwmon.c
> > +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> > @@ -7,6 +7,7 @@
> >   #include <linux/hwmon-sysfs.h>
> >   #include <linux/jiffies.h>
> >   #include <linux/types.h>
> > +#include <linux/units.h>
> >   #include "i915_drv.h"
> >   #include "i915_hwmon.h"
> > @@ -32,6 +33,7 @@
> >   struct hwm_reg {
> >   	i915_reg_t gt_perf_status;
> > +	i915_reg_t pkg_temp;
> >   	i915_reg_t pkg_power_sku_unit;
> >   	i915_reg_t pkg_power_sku;
> >   	i915_reg_t pkg_rapl_limit;
> > @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
> >   };
> >   static const struct hwmon_channel_info * const hwm_info[] = {
> > +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
> >   	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
> >   	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
> >   	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
> > @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
> >   				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
> >   }
> > +static umode_t
> > +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> > +{
> > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > +
> > +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> > +		return 0444;
> > +
> > +	return 0;
> > +}
> > +
> > +static int
> > +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> > +{
> > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > +	intel_wakeref_t wakeref;
> > +	u32 reg_val;
> > +
> > +	if (attr == hwmon_temp_input) {
> > +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> > +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> > +
> > +		/* HW register value is in degrees, convert to millidegrees. */
> > +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> > +		return 0;
> > +	}
> > +
> > +	return -EOPNOTSUPP;
> > +}
> Let's try to have synergy between previous attribute, such as hwm_fan_input,
> and this one.

This one's simple enough to be inline IMHO.
Besides, it's already in synergy with hwm_in_read() which has similar
implementation.

Raag
Gupta, Anshuman Sept. 5, 2024, 2:09 p.m. UTC | #5
On 2024-09-05 at 11:55:23 +0300, Raag Jadav wrote:
> On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
> > 
> > 
> > On 28-08-2024 10:15, Raag Jadav wrote:
> > > Add hwmon support for temp1_input attribute, which will expose package
> > > temperature in millidegree Celsius. With this in place we can monitor
> > > package temperature using lm-sensors tool.
> > > 
> > > $ sensors
> > > i915-pci-0300
> > > Adapter: PCI adapter
> > > in0:         990.00 mV
> > > fan1:        1260 RPM
> > > temp1:        +45.0°C
> > > power1:           N/A  (max =  35.00 W)
> > > energy1:      12.62 kJ
> > > 
> > > Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
> > > Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> > > ---
> > >   .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
> > >   drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
> > >   drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
> > >   3 files changed, 51 insertions(+)
> > > 
> > > diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > index be4141a7522f..a885e5316d02 100644
> > > --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
> > >   Description:	RO. Fan speed of device in RPM.
> > >   		Only supported for particular Intel i915 graphics platforms.
> > > +
> > > +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
> > > +Date:		November 2024
> > > +KernelVersion:	6.12
> > > +Contact:	intel-gfx@lists.freedesktop.org
> > > +Description:	RO. GPU package temperature in millidegree Celsius.
> > > +
> > > +		Only supported for particular Intel i915 graphics platforms.
> > > diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> > > index 17d30f6b84b0..9f1a2300510b 100644
> > > --- a/drivers/gpu/drm/i915/i915_hwmon.c
> > > +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> > > @@ -7,6 +7,7 @@
> > >   #include <linux/hwmon-sysfs.h>
> > >   #include <linux/jiffies.h>
> > >   #include <linux/types.h>
> > > +#include <linux/units.h>
> > >   #include "i915_drv.h"
> > >   #include "i915_hwmon.h"
> > > @@ -32,6 +33,7 @@
> > >   struct hwm_reg {
> > >   	i915_reg_t gt_perf_status;
> > > +	i915_reg_t pkg_temp;
> > >   	i915_reg_t pkg_power_sku_unit;
> > >   	i915_reg_t pkg_power_sku;
> > >   	i915_reg_t pkg_rapl_limit;
> > > @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
> > >   };
> > >   static const struct hwmon_channel_info * const hwm_info[] = {
> > > +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
> > >   	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
> > >   	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
> > >   	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
> > > @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
> > >   				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
> > >   }
> > > +static umode_t
> > > +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> > > +{
> > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > +
> > > +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> > > +		return 0444;
> > > +
> > > +	return 0;
> > > +}
> > > +
> > > +static int
> > > +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> > > +{
> > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > +	intel_wakeref_t wakeref;
> > > +	u32 reg_val;
> > > +
> > > +	if (attr == hwmon_temp_input) {
> > > +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> > > +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> > > +
> > > +		/* HW register value is in degrees, convert to millidegrees. */
> > > +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> > > +		return 0;
> > > +	}
> > > +
> > > +	return -EOPNOTSUPP;
> > > +}
> > Let's try to have synergy between previous attribute, such as hwm_fan_input,
> > and this one.
> 
> This one's simple enough to be inline IMHO.
> Besides, it's already in synergy with hwm_in_read() which has similar
> implementation.
Agree this is pretty simple to have an any helper but IMO it would have been cleaner to have a switch
like hwm_in_read() to return -EOPNOTSUPP in default case. i think that was reason switch case was 
used in entire file.

Thanks,
Anshuman

> 
> Raag
Raag Jadav Sept. 5, 2024, 7:18 p.m. UTC | #6
On Thu, Sep 05, 2024 at 07:39:31PM +0530, Anshuman Gupta wrote:
> On 2024-09-05 at 11:55:23 +0300, Raag Jadav wrote:
> > On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
> > > 
> > > 
> > > On 28-08-2024 10:15, Raag Jadav wrote:
> > > > Add hwmon support for temp1_input attribute, which will expose package
> > > > temperature in millidegree Celsius. With this in place we can monitor
> > > > package temperature using lm-sensors tool.
> > > > 
> > > > $ sensors
> > > > i915-pci-0300
> > > > Adapter: PCI adapter
> > > > in0:         990.00 mV
> > > > fan1:        1260 RPM
> > > > temp1:        +45.0°C
> > > > power1:           N/A  (max =  35.00 W)
> > > > energy1:      12.62 kJ
> > > > 
> > > > Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
> > > > Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> > > > ---
> > > >   .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
> > > >   drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
> > > >   drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
> > > >   3 files changed, 51 insertions(+)
> > > > 
> > > > diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > index be4141a7522f..a885e5316d02 100644
> > > > --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
> > > >   Description:	RO. Fan speed of device in RPM.
> > > >   		Only supported for particular Intel i915 graphics platforms.
> > > > +
> > > > +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
> > > > +Date:		November 2024
> > > > +KernelVersion:	6.12
> > > > +Contact:	intel-gfx@lists.freedesktop.org
> > > > +Description:	RO. GPU package temperature in millidegree Celsius.
> > > > +
> > > > +		Only supported for particular Intel i915 graphics platforms.
> > > > diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> > > > index 17d30f6b84b0..9f1a2300510b 100644
> > > > --- a/drivers/gpu/drm/i915/i915_hwmon.c
> > > > +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> > > > @@ -7,6 +7,7 @@
> > > >   #include <linux/hwmon-sysfs.h>
> > > >   #include <linux/jiffies.h>
> > > >   #include <linux/types.h>
> > > > +#include <linux/units.h>
> > > >   #include "i915_drv.h"
> > > >   #include "i915_hwmon.h"
> > > > @@ -32,6 +33,7 @@
> > > >   struct hwm_reg {
> > > >   	i915_reg_t gt_perf_status;
> > > > +	i915_reg_t pkg_temp;
> > > >   	i915_reg_t pkg_power_sku_unit;
> > > >   	i915_reg_t pkg_power_sku;
> > > >   	i915_reg_t pkg_rapl_limit;
> > > > @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
> > > >   };
> > > >   static const struct hwmon_channel_info * const hwm_info[] = {
> > > > +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
> > > >   	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
> > > >   	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
> > > >   	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
> > > > @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
> > > >   				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
> > > >   }
> > > > +static umode_t
> > > > +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> > > > +{
> > > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > > +
> > > > +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> > > > +		return 0444;
> > > > +
> > > > +	return 0;
> > > > +}
> > > > +
> > > > +static int
> > > > +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> > > > +{
> > > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > > +	intel_wakeref_t wakeref;
> > > > +	u32 reg_val;
> > > > +
> > > > +	if (attr == hwmon_temp_input) {
> > > > +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> > > > +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> > > > +
> > > > +		/* HW register value is in degrees, convert to millidegrees. */
> > > > +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> > > > +		return 0;
> > > > +	}
> > > > +
> > > > +	return -EOPNOTSUPP;
> > > > +}
> > > Let's try to have synergy between previous attribute, such as hwm_fan_input,
> > > and this one.
> > 
> > This one's simple enough to be inline IMHO.
> > Besides, it's already in synergy with hwm_in_read() which has similar
> > implementation.
> Agree this is pretty simple to have an any helper but IMO it would have been cleaner to have a switch
> like hwm_in_read() to return -EOPNOTSUPP in default case. i think that was reason switch case was 
> used in entire file.

Extending on the simplicity argument above, if() makes more sense for a single case.

Raag
Gupta, Anshuman Sept. 6, 2024, 6:26 a.m. UTC | #7
On 2024-09-05 at 22:18:17 +0300, Raag Jadav wrote:
> On Thu, Sep 05, 2024 at 07:39:31PM +0530, Anshuman Gupta wrote:
> > On 2024-09-05 at 11:55:23 +0300, Raag Jadav wrote:
> > > On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
> > > > 
> > > > 
> > > > On 28-08-2024 10:15, Raag Jadav wrote:
> > > > > Add hwmon support for temp1_input attribute, which will expose package
> > > > > temperature in millidegree Celsius. With this in place we can monitor
> > > > > package temperature using lm-sensors tool.
> > > > > 
> > > > > $ sensors
> > > > > i915-pci-0300
> > > > > Adapter: PCI adapter
> > > > > in0:         990.00 mV
> > > > > fan1:        1260 RPM
> > > > > temp1:        +45.0°C
> > > > > power1:           N/A  (max =  35.00 W)
> > > > > energy1:      12.62 kJ
> > > > > 
> > > > > Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
> > > > > Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> > > > > ---
> > > > >   .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
> > > > >   drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
> > > > >   drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
> > > > >   3 files changed, 51 insertions(+)
> > > > > 
> > > > > diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > > index be4141a7522f..a885e5316d02 100644
> > > > > --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > > +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > > @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
> > > > >   Description:	RO. Fan speed of device in RPM.
> > > > >   		Only supported for particular Intel i915 graphics platforms.
> > > > > +
> > > > > +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
> > > > > +Date:		November 2024
> > > > > +KernelVersion:	6.12
> > > > > +Contact:	intel-gfx@lists.freedesktop.org
> > > > > +Description:	RO. GPU package temperature in millidegree Celsius.
> > > > > +
> > > > > +		Only supported for particular Intel i915 graphics platforms.
> > > > > diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> > > > > index 17d30f6b84b0..9f1a2300510b 100644
> > > > > --- a/drivers/gpu/drm/i915/i915_hwmon.c
> > > > > +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> > > > > @@ -7,6 +7,7 @@
> > > > >   #include <linux/hwmon-sysfs.h>
> > > > >   #include <linux/jiffies.h>
> > > > >   #include <linux/types.h>
> > > > > +#include <linux/units.h>
> > > > >   #include "i915_drv.h"
> > > > >   #include "i915_hwmon.h"
> > > > > @@ -32,6 +33,7 @@
> > > > >   struct hwm_reg {
> > > > >   	i915_reg_t gt_perf_status;
> > > > > +	i915_reg_t pkg_temp;
> > > > >   	i915_reg_t pkg_power_sku_unit;
> > > > >   	i915_reg_t pkg_power_sku;
> > > > >   	i915_reg_t pkg_rapl_limit;
> > > > > @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
> > > > >   };
> > > > >   static const struct hwmon_channel_info * const hwm_info[] = {
> > > > > +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
> > > > >   	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
> > > > >   	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
> > > > >   	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
> > > > > @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
> > > > >   				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
> > > > >   }
> > > > > +static umode_t
> > > > > +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> > > > > +{
> > > > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > > > +
> > > > > +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> > > > > +		return 0444;
> > > > > +
> > > > > +	return 0;
> > > > > +}
> > > > > +
> > > > > +static int
> > > > > +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> > > > > +{
> > > > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > > > +	intel_wakeref_t wakeref;
> > > > > +	u32 reg_val;
> > > > > +
> > > > > +	if (attr == hwmon_temp_input) {
> > > > > +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> > > > > +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> > > > > +
> > > > > +		/* HW register value is in degrees, convert to millidegrees. */
> > > > > +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> > > > > +		return 0;
> > > > > +	}
> > > > > +
> > > > > +	return -EOPNOTSUPP;
> > > > > +}
> > > > Let's try to have synergy between previous attribute, such as hwm_fan_input,
> > > > and this one.
> > > 
> > > This one's simple enough to be inline IMHO.
> > > Besides, it's already in synergy with hwm_in_read() which has similar
> > > implementation.
> > Agree this is pretty simple to have an any helper but IMO it would have been cleaner to have a switch
> > like hwm_in_read() to return -EOPNOTSUPP in default case. i think that was reason switch case was 
> > used in entire file.
> 
> Extending on the simplicity argument above, if() makes more sense for a single case.
IMO lets prefer the style which was used in this entire file,
that is more readable along with other attributes.
Idea behind switch was scalable attribute for future. 
It is something related to individual preference therefore
 let's prefer the symmetry with other hwmon attributes.

With that
Reviewed-by: Anshuman Gupta <anshuman.gupta@intel.com>
> 
> Raag
Nilawar, Badal Sept. 6, 2024, 11:03 a.m. UTC | #8
On 06-09-2024 11:56, Anshuman Gupta wrote:
> On 2024-09-05 at 22:18:17 +0300, Raag Jadav wrote:
>> On Thu, Sep 05, 2024 at 07:39:31PM +0530, Anshuman Gupta wrote:
>>> On 2024-09-05 at 11:55:23 +0300, Raag Jadav wrote:
>>>> On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
>>>>>
>>>>>
>>>>> On 28-08-2024 10:15, Raag Jadav wrote:
>>>>>> Add hwmon support for temp1_input attribute, which will expose package
>>>>>> temperature in millidegree Celsius. With this in place we can monitor
>>>>>> package temperature using lm-sensors tool.
>>>>>>
>>>>>> $ sensors
>>>>>> i915-pci-0300
>>>>>> Adapter: PCI adapter
>>>>>> in0:         990.00 mV
>>>>>> fan1:        1260 RPM
>>>>>> temp1:        +45.0°C
>>>>>> power1:           N/A  (max =  35.00 W)
>>>>>> energy1:      12.62 kJ
>>>>>>
>>>>>> Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
>>>>>> Signed-off-by: Raag Jadav <raag.jadav@intel.com>
>>>>>> ---
>>>>>>    .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
>>>>>>    drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
>>>>>>    drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
>>>>>>    3 files changed, 51 insertions(+)
>>>>>>
>>>>>> diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
>>>>>> index be4141a7522f..a885e5316d02 100644
>>>>>> --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
>>>>>> +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
>>>>>> @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
>>>>>>    Description:	RO. Fan speed of device in RPM.
>>>>>>    		Only supported for particular Intel i915 graphics platforms.
>>>>>> +
>>>>>> +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
>>>>>> +Date:		November 2024
>>>>>> +KernelVersion:	6.12
>>>>>> +Contact:	intel-gfx@lists.freedesktop.org
>>>>>> +Description:	RO. GPU package temperature in millidegree Celsius.
>>>>>> +
>>>>>> +		Only supported for particular Intel i915 graphics platforms.
>>>>>> diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
>>>>>> index 17d30f6b84b0..9f1a2300510b 100644
>>>>>> --- a/drivers/gpu/drm/i915/i915_hwmon.c
>>>>>> +++ b/drivers/gpu/drm/i915/i915_hwmon.c
>>>>>> @@ -7,6 +7,7 @@
>>>>>>    #include <linux/hwmon-sysfs.h>
>>>>>>    #include <linux/jiffies.h>
>>>>>>    #include <linux/types.h>
>>>>>> +#include <linux/units.h>
>>>>>>    #include "i915_drv.h"
>>>>>>    #include "i915_hwmon.h"
>>>>>> @@ -32,6 +33,7 @@
>>>>>>    struct hwm_reg {
>>>>>>    	i915_reg_t gt_perf_status;
>>>>>> +	i915_reg_t pkg_temp;
>>>>>>    	i915_reg_t pkg_power_sku_unit;
>>>>>>    	i915_reg_t pkg_power_sku;
>>>>>>    	i915_reg_t pkg_rapl_limit;
>>>>>> @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
>>>>>>    };
>>>>>>    static const struct hwmon_channel_info * const hwm_info[] = {
>>>>>> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
>>>>>>    	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
>>>>>>    	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
>>>>>>    	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
>>>>>> @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
>>>>>>    				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
>>>>>>    }
>>>>>> +static umode_t
>>>>>> +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
>>>>>> +{
>>>>>> +	struct i915_hwmon *hwmon = ddat->hwmon;
>>>>>> +
>>>>>> +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
>>>>>> +		return 0444;
>>>>>> +
>>>>>> +	return 0;
>>>>>> +}
>>>>>> +
>>>>>> +static int
>>>>>> +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
>>>>>> +{
>>>>>> +	struct i915_hwmon *hwmon = ddat->hwmon;
>>>>>> +	intel_wakeref_t wakeref;
>>>>>> +	u32 reg_val;
>>>>>> +
>>>>>> +	if (attr == hwmon_temp_input) {
>>>>>> +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
>>>>>> +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
>>>>>> +
>>>>>> +		/* HW register value is in degrees, convert to millidegrees. */
>>>>>> +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
>>>>>> +		return 0;
>>>>>> +	}
>>>>>> +
>>>>>> +	return -EOPNOTSUPP;
>>>>>> +}
>>>>> Let's try to have synergy between previous attribute, such as hwm_fan_input,
>>>>> and this one.
>>>>
>>>> This one's simple enough to be inline IMHO.
>>>> Besides, it's already in synergy with hwm_in_read() which has similar
>>>> implementation.
>>> Agree this is pretty simple to have an any helper but IMO it would have been cleaner to have a switch
>>> like hwm_in_read() to return -EOPNOTSUPP in default case. i think that was reason switch case was
>>> used in entire file.
>>
>> Extending on the simplicity argument above, if() makes more sense for a single case.
> IMO lets prefer the style which was used in this entire file,
> that is more readable along with other attributes.
> Idea behind switch was scalable attribute for future.
> It is something related to individual preference therefore
>   let's prefer the symmetry with other hwmon attributes.
I agree with this, but even if this approach is used file-wide, there 
were concerns about using a switch case for a single case while 
implementing the fan_input attribute. 
https://patchwork.freedesktop.org/patch/607642/?series=136036&rev=4"
So I suggested to implement temp_input the way fan_input is implemented, 
at least we should follow this approach to maintain symmetry with new 
attributes. But in case if there is agreement to use file wide approach 
then please follow that approach for fan_input as well.

Regards,
Badal
> 
> With that
> Reviewed-by: Anshuman Gupta <anshuman.gupta@intel.com>
>>
>> Raag
Raag Jadav Sept. 7, 2024, 11:18 a.m. UTC | #9
On Fri, Sep 06, 2024 at 04:33:12PM +0530, Nilawar, Badal wrote:
> On 06-09-2024 11:56, Anshuman Gupta wrote:
> > On 2024-09-05 at 22:18:17 +0300, Raag Jadav wrote:
> > > On Thu, Sep 05, 2024 at 07:39:31PM +0530, Anshuman Gupta wrote:
> > > > On 2024-09-05 at 11:55:23 +0300, Raag Jadav wrote:
> > > > > On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
> > > > > > 
> > > > > > 
> > > > > > On 28-08-2024 10:15, Raag Jadav wrote:
> > > > > > > Add hwmon support for temp1_input attribute, which will expose package
> > > > > > > temperature in millidegree Celsius. With this in place we can monitor
> > > > > > > package temperature using lm-sensors tool.
> > > > > > > 
> > > > > > > $ sensors
> > > > > > > i915-pci-0300
> > > > > > > Adapter: PCI adapter
> > > > > > > in0:         990.00 mV
> > > > > > > fan1:        1260 RPM
> > > > > > > temp1:        +45.0°C
> > > > > > > power1:           N/A  (max =  35.00 W)
> > > > > > > energy1:      12.62 kJ
> > > > > > > 
> > > > > > > Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
> > > > > > > Signed-off-by: Raag Jadav <raag.jadav@intel.com>
> > > > > > > ---
> > > > > > >    .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
> > > > > > >    drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
> > > > > > >    drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
> > > > > > >    3 files changed, 51 insertions(+)
> > > > > > > 
> > > > > > > diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > > > > index be4141a7522f..a885e5316d02 100644
> > > > > > > --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > > > > +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
> > > > > > > @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
> > > > > > >    Description:	RO. Fan speed of device in RPM.
> > > > > > >    		Only supported for particular Intel i915 graphics platforms.
> > > > > > > +
> > > > > > > +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
> > > > > > > +Date:		November 2024
> > > > > > > +KernelVersion:	6.12
> > > > > > > +Contact:	intel-gfx@lists.freedesktop.org
> > > > > > > +Description:	RO. GPU package temperature in millidegree Celsius.
> > > > > > > +
> > > > > > > +		Only supported for particular Intel i915 graphics platforms.
> > > > > > > diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
> > > > > > > index 17d30f6b84b0..9f1a2300510b 100644
> > > > > > > --- a/drivers/gpu/drm/i915/i915_hwmon.c
> > > > > > > +++ b/drivers/gpu/drm/i915/i915_hwmon.c
> > > > > > > @@ -7,6 +7,7 @@
> > > > > > >    #include <linux/hwmon-sysfs.h>
> > > > > > >    #include <linux/jiffies.h>
> > > > > > >    #include <linux/types.h>
> > > > > > > +#include <linux/units.h>
> > > > > > >    #include "i915_drv.h"
> > > > > > >    #include "i915_hwmon.h"
> > > > > > > @@ -32,6 +33,7 @@
> > > > > > >    struct hwm_reg {
> > > > > > >    	i915_reg_t gt_perf_status;
> > > > > > > +	i915_reg_t pkg_temp;
> > > > > > >    	i915_reg_t pkg_power_sku_unit;
> > > > > > >    	i915_reg_t pkg_power_sku;
> > > > > > >    	i915_reg_t pkg_rapl_limit;
> > > > > > > @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
> > > > > > >    };
> > > > > > >    static const struct hwmon_channel_info * const hwm_info[] = {
> > > > > > > +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
> > > > > > >    	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
> > > > > > >    	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
> > > > > > >    	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
> > > > > > > @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
> > > > > > >    				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
> > > > > > >    }
> > > > > > > +static umode_t
> > > > > > > +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
> > > > > > > +{
> > > > > > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > > > > > +
> > > > > > > +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
> > > > > > > +		return 0444;
> > > > > > > +
> > > > > > > +	return 0;
> > > > > > > +}
> > > > > > > +
> > > > > > > +static int
> > > > > > > +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
> > > > > > > +{
> > > > > > > +	struct i915_hwmon *hwmon = ddat->hwmon;
> > > > > > > +	intel_wakeref_t wakeref;
> > > > > > > +	u32 reg_val;
> > > > > > > +
> > > > > > > +	if (attr == hwmon_temp_input) {
> > > > > > > +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
> > > > > > > +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
> > > > > > > +
> > > > > > > +		/* HW register value is in degrees, convert to millidegrees. */
> > > > > > > +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
> > > > > > > +		return 0;
> > > > > > > +	}
> > > > > > > +
> > > > > > > +	return -EOPNOTSUPP;
> > > > > > > +}
> > > > > > Let's try to have synergy between previous attribute, such as hwm_fan_input,
> > > > > > and this one.
> > > > > 
> > > > > This one's simple enough to be inline IMHO.
> > > > > Besides, it's already in synergy with hwm_in_read() which has similar
> > > > > implementation.
> > > > Agree this is pretty simple to have an any helper but IMO it would have been cleaner to have a switch
> > > > like hwm_in_read() to return -EOPNOTSUPP in default case. i think that was reason switch case was
> > > > used in entire file.
> > > 
> > > Extending on the simplicity argument above, if() makes more sense for a single case.
> > IMO lets prefer the style which was used in this entire file,
> > that is more readable along with other attributes.
> > Idea behind switch was scalable attribute for future.
> > It is something related to individual preference therefore
> >   let's prefer the symmetry with other hwmon attributes.
> I agree with this, but even if this approach is used file-wide, there were
> concerns about using a switch case for a single case while implementing the
> fan_input attribute.
> https://patchwork.freedesktop.org/patch/607642/?series=136036&rev=4"
> So I suggested to implement temp_input the way fan_input is implemented, at
> least we should follow this approach to maintain symmetry with new
> attributes. But in case if there is agreement to use file wide approach then
> please follow that approach for fan_input as well.

Let's try to keep it simple and use whatever works for the case.
I'm sure the driver is much easier to maintain with a few less lines.

Raag
Nilawar, Badal Sept. 10, 2024, 4:33 a.m. UTC | #10
On 07-09-2024 16:48, Raag Jadav wrote:
> On Fri, Sep 06, 2024 at 04:33:12PM +0530, Nilawar, Badal wrote:
>> On 06-09-2024 11:56, Anshuman Gupta wrote:
>>> On 2024-09-05 at 22:18:17 +0300, Raag Jadav wrote:
>>>> On Thu, Sep 05, 2024 at 07:39:31PM +0530, Anshuman Gupta wrote:
>>>>> On 2024-09-05 at 11:55:23 +0300, Raag Jadav wrote:
>>>>>> On Thu, Sep 05, 2024 at 11:56:15AM +0530, Nilawar, Badal wrote:
>>>>>>>
>>>>>>>
>>>>>>> On 28-08-2024 10:15, Raag Jadav wrote:
>>>>>>>> Add hwmon support for temp1_input attribute, which will expose package
>>>>>>>> temperature in millidegree Celsius. With this in place we can monitor
>>>>>>>> package temperature using lm-sensors tool.
>>>>>>>>
>>>>>>>> $ sensors
>>>>>>>> i915-pci-0300
>>>>>>>> Adapter: PCI adapter
>>>>>>>> in0:         990.00 mV
>>>>>>>> fan1:        1260 RPM
>>>>>>>> temp1:        +45.0°C
>>>>>>>> power1:           N/A  (max =  35.00 W)
>>>>>>>> energy1:      12.62 kJ
>>>>>>>>
>>>>>>>> Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/11276
>>>>>>>> Signed-off-by: Raag Jadav <raag.jadav@intel.com>
>>>>>>>> ---
>>>>>>>>     .../ABI/testing/sysfs-driver-intel-i915-hwmon |  8 ++++
>>>>>>>>     drivers/gpu/drm/i915/i915_hwmon.c             | 39 +++++++++++++++++++
>>>>>>>>     drivers/gpu/drm/i915/intel_mchbar_regs.h      |  4 ++
>>>>>>>>     3 files changed, 51 insertions(+)
>>>>>>>>
>>>>>>>> diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
>>>>>>>> index be4141a7522f..a885e5316d02 100644
>>>>>>>> --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
>>>>>>>> +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
>>>>>>>> @@ -83,3 +83,11 @@ Contact:	intel-gfx@lists.freedesktop.org
>>>>>>>>     Description:	RO. Fan speed of device in RPM.
>>>>>>>>     		Only supported for particular Intel i915 graphics platforms.
>>>>>>>> +
>>>>>>>> +What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
>>>>>>>> +Date:		November 2024
>>>>>>>> +KernelVersion:	6.12
>>>>>>>> +Contact:	intel-gfx@lists.freedesktop.org
>>>>>>>> +Description:	RO. GPU package temperature in millidegree Celsius.
>>>>>>>> +
>>>>>>>> +		Only supported for particular Intel i915 graphics platforms.
>>>>>>>> diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
>>>>>>>> index 17d30f6b84b0..9f1a2300510b 100644
>>>>>>>> --- a/drivers/gpu/drm/i915/i915_hwmon.c
>>>>>>>> +++ b/drivers/gpu/drm/i915/i915_hwmon.c
>>>>>>>> @@ -7,6 +7,7 @@
>>>>>>>>     #include <linux/hwmon-sysfs.h>
>>>>>>>>     #include <linux/jiffies.h>
>>>>>>>>     #include <linux/types.h>
>>>>>>>> +#include <linux/units.h>
>>>>>>>>     #include "i915_drv.h"
>>>>>>>>     #include "i915_hwmon.h"
>>>>>>>> @@ -32,6 +33,7 @@
>>>>>>>>     struct hwm_reg {
>>>>>>>>     	i915_reg_t gt_perf_status;
>>>>>>>> +	i915_reg_t pkg_temp;
>>>>>>>>     	i915_reg_t pkg_power_sku_unit;
>>>>>>>>     	i915_reg_t pkg_power_sku;
>>>>>>>>     	i915_reg_t pkg_rapl_limit;
>>>>>>>> @@ -280,6 +282,7 @@ static const struct attribute_group *hwm_groups[] = {
>>>>>>>>     };
>>>>>>>>     static const struct hwmon_channel_info * const hwm_info[] = {
>>>>>>>> +	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
>>>>>>>>     	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
>>>>>>>>     	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
>>>>>>>>     	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
>>>>>>>> @@ -310,6 +313,36 @@ static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
>>>>>>>>     				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
>>>>>>>>     }
>>>>>>>> +static umode_t
>>>>>>>> +hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
>>>>>>>> +{
>>>>>>>> +	struct i915_hwmon *hwmon = ddat->hwmon;
>>>>>>>> +
>>>>>>>> +	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
>>>>>>>> +		return 0444;
>>>>>>>> +
>>>>>>>> +	return 0;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +static int
>>>>>>>> +hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
>>>>>>>> +{
>>>>>>>> +	struct i915_hwmon *hwmon = ddat->hwmon;
>>>>>>>> +	intel_wakeref_t wakeref;
>>>>>>>> +	u32 reg_val;
>>>>>>>> +
>>>>>>>> +	if (attr == hwmon_temp_input) {
>>>>>>>> +		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
>>>>>>>> +			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
>>>>>>>> +
>>>>>>>> +		/* HW register value is in degrees, convert to millidegrees. */
>>>>>>>> +		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
>>>>>>>> +		return 0;
>>>>>>>> +	}
>>>>>>>> +
>>>>>>>> +	return -EOPNOTSUPP;
>>>>>>>> +}
>>>>>>> Let's try to have synergy between previous attribute, such as hwm_fan_input,
>>>>>>> and this one.
>>>>>>
>>>>>> This one's simple enough to be inline IMHO.
>>>>>> Besides, it's already in synergy with hwm_in_read() which has similar
>>>>>> implementation.
>>>>> Agree this is pretty simple to have an any helper but IMO it would have been cleaner to have a switch
>>>>> like hwm_in_read() to return -EOPNOTSUPP in default case. i think that was reason switch case was
>>>>> used in entire file.
>>>>
>>>> Extending on the simplicity argument above, if() makes more sense for a single case.
>>> IMO lets prefer the style which was used in this entire file,
>>> that is more readable along with other attributes.
>>> Idea behind switch was scalable attribute for future.
>>> It is something related to individual preference therefore
>>>    let's prefer the symmetry with other hwmon attributes.
>> I agree with this, but even if this approach is used file-wide, there were
>> concerns about using a switch case for a single case while implementing the
>> fan_input attribute.
>> https://patchwork.freedesktop.org/patch/607642/?series=136036&rev=4"
>> So I suggested to implement temp_input the way fan_input is implemented, at
>> least we should follow this approach to maintain symmetry with new
>> attributes. But in case if there is agreement to use file wide approach then
>> please follow that approach for fan_input as well.
> 
> Let's try to keep it simple and use whatever works for the case.
> I'm sure the driver is much easier to maintain with a few less lines.
Let's not break symmetry, whichever approach is being followed please 
maintain it file wide.

Regards,
Badal
> 
> Raag
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
index be4141a7522f..a885e5316d02 100644
--- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
+++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon
@@ -83,3 +83,11 @@  Contact:	intel-gfx@lists.freedesktop.org
 Description:	RO. Fan speed of device in RPM.
 
 		Only supported for particular Intel i915 graphics platforms.
+
+What:		/sys/bus/pci/drivers/i915/.../hwmon/hwmon<i>/temp1_input
+Date:		November 2024
+KernelVersion:	6.12
+Contact:	intel-gfx@lists.freedesktop.org
+Description:	RO. GPU package temperature in millidegree Celsius.
+
+		Only supported for particular Intel i915 graphics platforms.
diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c
index 17d30f6b84b0..9f1a2300510b 100644
--- a/drivers/gpu/drm/i915/i915_hwmon.c
+++ b/drivers/gpu/drm/i915/i915_hwmon.c
@@ -7,6 +7,7 @@ 
 #include <linux/hwmon-sysfs.h>
 #include <linux/jiffies.h>
 #include <linux/types.h>
+#include <linux/units.h>
 
 #include "i915_drv.h"
 #include "i915_hwmon.h"
@@ -32,6 +33,7 @@ 
 
 struct hwm_reg {
 	i915_reg_t gt_perf_status;
+	i915_reg_t pkg_temp;
 	i915_reg_t pkg_power_sku_unit;
 	i915_reg_t pkg_power_sku;
 	i915_reg_t pkg_rapl_limit;
@@ -280,6 +282,7 @@  static const struct attribute_group *hwm_groups[] = {
 };
 
 static const struct hwmon_channel_info * const hwm_info[] = {
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
 	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
 	HWMON_CHANNEL_INFO(power, HWMON_P_MAX | HWMON_P_RATED_MAX | HWMON_P_CRIT),
 	HWMON_CHANNEL_INFO(energy, HWMON_E_INPUT),
@@ -310,6 +313,36 @@  static int hwm_pcode_write_i1(struct drm_i915_private *i915, u32 uval)
 				  POWER_SETUP_SUBCOMMAND_WRITE_I1, 0, uval);
 }
 
+static umode_t
+hwm_temp_is_visible(const struct hwm_drvdata *ddat, u32 attr)
+{
+	struct i915_hwmon *hwmon = ddat->hwmon;
+
+	if (attr == hwmon_temp_input && i915_mmio_reg_valid(hwmon->rg.pkg_temp))
+		return 0444;
+
+	return 0;
+}
+
+static int
+hwm_temp_read(struct hwm_drvdata *ddat, u32 attr, long *val)
+{
+	struct i915_hwmon *hwmon = ddat->hwmon;
+	intel_wakeref_t wakeref;
+	u32 reg_val;
+
+	if (attr == hwmon_temp_input) {
+		with_intel_runtime_pm(ddat->uncore->rpm, wakeref)
+			reg_val = intel_uncore_read(ddat->uncore, hwmon->rg.pkg_temp);
+
+		/* HW register value is in degrees, convert to millidegrees. */
+		*val = REG_FIELD_GET(TEMP_MASK, reg_val) * MILLIDEGREE_PER_DEGREE;
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+
 static umode_t
 hwm_in_is_visible(const struct hwm_drvdata *ddat, u32 attr)
 {
@@ -692,6 +725,8 @@  hwm_is_visible(const void *drvdata, enum hwmon_sensor_types type,
 	struct hwm_drvdata *ddat = (struct hwm_drvdata *)drvdata;
 
 	switch (type) {
+	case hwmon_temp:
+		return hwm_temp_is_visible(ddat, attr);
 	case hwmon_in:
 		return hwm_in_is_visible(ddat, attr);
 	case hwmon_power:
@@ -714,6 +749,8 @@  hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
 	struct hwm_drvdata *ddat = dev_get_drvdata(dev);
 
 	switch (type) {
+	case hwmon_temp:
+		return hwm_temp_read(ddat, attr, val);
 	case hwmon_in:
 		return hwm_in_read(ddat, attr, val);
 	case hwmon_power:
@@ -810,6 +847,7 @@  hwm_get_preregistration_info(struct drm_i915_private *i915)
 	hwmon->rg.gt_perf_status = GEN12_RPSTAT1;
 
 	if (IS_DG1(i915) || IS_DG2(i915)) {
+		hwmon->rg.pkg_temp = PCU_PACKAGE_TEMPERATURE;
 		hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT;
 		hwmon->rg.pkg_power_sku = PCU_PACKAGE_POWER_SKU;
 		hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT;
@@ -817,6 +855,7 @@  hwm_get_preregistration_info(struct drm_i915_private *i915)
 		hwmon->rg.energy_status_tile = INVALID_MMIO_REG;
 		hwmon->rg.fan_speed = PCU_PWM_FAN_SPEED;
 	} else {
+		hwmon->rg.pkg_temp = INVALID_MMIO_REG;
 		hwmon->rg.pkg_power_sku_unit = INVALID_MMIO_REG;
 		hwmon->rg.pkg_power_sku = INVALID_MMIO_REG;
 		hwmon->rg.pkg_rapl_limit = INVALID_MMIO_REG;
diff --git a/drivers/gpu/drm/i915/intel_mchbar_regs.h b/drivers/gpu/drm/i915/intel_mchbar_regs.h
index 73900c098d59..dc2477179c3e 100644
--- a/drivers/gpu/drm/i915/intel_mchbar_regs.h
+++ b/drivers/gpu/drm/i915/intel_mchbar_regs.h
@@ -207,6 +207,10 @@ 
 #define PCU_PACKAGE_ENERGY_STATUS              _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x593c)
 
 #define GEN6_GT_PERF_STATUS			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5948)
+
+#define PCU_PACKAGE_TEMPERATURE			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5978)
+#define   TEMP_MASK				REG_GENMASK(7, 0)
+
 #define GEN6_RP_STATE_LIMITS			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5994)
 #define GEN6_RP_STATE_CAP			_MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5998)
 #define   RP0_CAP_MASK				REG_GENMASK(7, 0)