From patchwork Mon May 23 11:08:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Nilawar, Badal" X-Patchwork-Id: 12858884 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 38962C433EF for ; Mon, 23 May 2022 11:08:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6469E10F1FD; Mon, 23 May 2022 11:08:24 +0000 (UTC) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4204D10E460 for ; Mon, 23 May 2022 11:08:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653304103; x=1684840103; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sSpT4C/FWiADqrf4sCAkdWQnPcz3UnnliQObh4Rntpw=; b=HlUD/8G7U9VkP6YV4OLfYSL1CphzaDp+yFEQgBU5JKT0+1URnTth5LhN p2E/fEEeHJod0jNUGXkKWxOZGxH7tB3rHBV6vuo6/m0gPN9nyWfRsm5A9 zsHP14HZskJyL/EdcJd0iWDUgIri5DIOVeaLBj/bBCOUMohA/mxnfeKMz Vf797YrDUfYT+tIk1FUF1ymWQRQNH2Lr7vjAD/QnpgWyroHproi8LxRcf T+PZ3lyM5USXeeMDUjaSGXuWTE4EembFxjxGOrz1lXnzkcd4jSsJH9x5U mPui/a3alsp9DVR0axMnnulqIUlJth604HLRIhE8n01pSVFWbB6fXznWV w==; X-IronPort-AV: E=McAfee;i="6400,9594,10355"; a="359579397" X-IronPort-AV: E=Sophos;i="5.91,246,1647327600"; d="scan'208";a="359579397" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2022 04:08:22 -0700 X-IronPort-AV: E=Sophos;i="5.91,246,1647327600"; d="scan'208";a="600589642" Received: from bnilawar-desk.iind.intel.com ([10.145.162.36]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2022 04:08:19 -0700 From: Badal Nilawar To: intel-gfx@lists.freedesktop.org Date: Mon, 23 May 2022 16:38:39 +0530 Message-Id: <20220523110841.1151431-2-badal.nilawar@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523110841.1151431-1-badal.nilawar@intel.com> References: <20220523110841.1151431-1-badal.nilawar@intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH 1/3] drm/i915/hwmon: Add HWMON power sensor support X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Dale B Stimson As part of the System Managemenent Interface (SMI), use the HWMON subsystem to display power utilization. Signed-off-by: Dale B Stimson Signed-off-by: Ashutosh Dixit Signed-off-by: Riana Tauro Signed-off-by: Badal Nilawar --- .../ABI/testing/sysfs-driver-intel-i915-hwmon | 19 + drivers/gpu/drm/i915/Kconfig | 1 + drivers/gpu/drm/i915/Makefile | 1 + drivers/gpu/drm/i915/i915_driver.c | 5 + drivers/gpu/drm/i915/i915_drv.h | 2 + drivers/gpu/drm/i915/i915_hwmon.c | 410 ++++++++++++++++++ drivers/gpu/drm/i915/i915_hwmon.h | 44 ++ drivers/gpu/drm/i915/i915_reg.h | 15 + drivers/gpu/drm/i915/intel_mchbar_regs.h | 9 + 9 files changed, 506 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon create mode 100644 drivers/gpu/drm/i915/i915_hwmon.c create mode 100644 drivers/gpu/drm/i915/i915_hwmon.h diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon new file mode 100644 index 000000000000..c680dd842358 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon @@ -0,0 +1,19 @@ +What: /sys/devices/.../hwmon/hwmon/power1_max +Date: June 2021 +KernelVersion: 5.14 +Contact: dri-devel@lists.freedesktop.org +Description: RW. Card reactive sustained (PL1/Tau) power limit in microwatts. + + The power controller will throttle the operating frequency + if the power averaged over a window (typically seconds) + exceeds this limit. + + Only supported for particular Intel i915 graphics platforms. + +What: /sys/devices/.../hwmon/hwmon/power1_max_default +Date: June 2021 +KernelVersion: 5.14 +Contact: dri-devel@lists.freedesktop.org +Description: RO. Card default power limit (default TDP setting). + + Only supported for particular Intel i915 graphics platforms. diff --git a/drivers/gpu/drm/i915/Kconfig b/drivers/gpu/drm/i915/Kconfig index 7ae3b7d67fcf..c60cfacd4c47 100644 --- a/drivers/gpu/drm/i915/Kconfig +++ b/drivers/gpu/drm/i915/Kconfig @@ -19,6 +19,7 @@ config DRM_I915 select DRM_MIPI_DSI select RELAY select IRQ_WORK + select HWMON # i915 depends on ACPI_VIDEO when ACPI is enabled # but for select to work, need to select ACPI_VIDEO's dependencies, ick select BACKLIGHT_CLASS_DEVICE if ACPI diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile index d2b18f03a33c..e1d5912ea879 100644 --- a/drivers/gpu/drm/i915/Makefile +++ b/drivers/gpu/drm/i915/Makefile @@ -36,6 +36,7 @@ i915-y += i915_driver.o \ i915_drm_client.o \ i915_config.o \ i915_getparam.o \ + i915_hwmon.o \ i915_ioctl.o \ i915_irq.o \ i915_mitigations.o \ diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c index b47746152d97..6f678587a771 100644 --- a/drivers/gpu/drm/i915/i915_driver.c +++ b/drivers/gpu/drm/i915/i915_driver.c @@ -80,6 +80,7 @@ #include "i915_drm_client.h" #include "i915_drv.h" #include "i915_getparam.h" +#include "i915_hwmon.h" #include "i915_ioc32.h" #include "i915_ioctl.h" #include "i915_irq.h" @@ -702,6 +703,8 @@ static void i915_driver_register(struct drm_i915_private *dev_priv) intel_gt_driver_register(to_gt(dev_priv)); + i915_hwmon_register(dev_priv); + intel_display_driver_register(dev_priv); intel_power_domains_enable(dev_priv); @@ -728,6 +731,8 @@ static void i915_driver_unregister(struct drm_i915_private *dev_priv) intel_display_driver_unregister(dev_priv); + i915_hwmon_unregister(dev_priv); + intel_gt_driver_unregister(to_gt(dev_priv)); i915_perf_unregister(dev_priv); diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h index c5fc402d9c50..e16bedba2d84 100644 --- a/drivers/gpu/drm/i915/i915_drv.h +++ b/drivers/gpu/drm/i915/i915_drv.h @@ -769,6 +769,8 @@ struct drm_i915_private { struct i915_perf perf; + struct i915_hwmon *hwmon; + /* Abstract the submission mechanism (legacy ringbuffer or execlists) away */ struct intel_gt gt0; diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c new file mode 100644 index 000000000000..b94c11f2517f --- /dev/null +++ b/drivers/gpu/drm/i915/i915_hwmon.c @@ -0,0 +1,410 @@ +// SPDX-License-Identifier: MIT +/* + * Copyright © 2020 Intel Corporation + */ + +/* + * Power-related hwmon entries. + */ + +#include +#include +#include + +#include "i915_drv.h" +#include "i915_hwmon.h" +#include "intel_mchbar_regs.h" + +/* + * SF_* - scale factors for particular quantities according to hwmon spec. + * - power - microwatts + */ +#define SF_POWER 1000000 + +#define FIELD_SHIFT(__mask) \ + (BUILD_BUG_ON_ZERO(!__builtin_constant_p(__mask)) + \ + BUILD_BUG_ON_ZERO((__mask) == 0) + \ + __bf_shf(__mask)) + +static void +_locked_with_pm_intel_uncore_rmw(struct i915_hwmon_drvdata *ddat, + i915_reg_t reg, u32 clear, u32 set) +{ + struct i915_hwmon *hwmon = ddat->dd_hwmon; + struct intel_uncore *uncore = ddat->dd_uncore; + intel_wakeref_t wakeref; + + mutex_lock(&hwmon->hwmon_lock); + + with_intel_runtime_pm(uncore->rpm, wakeref) + intel_uncore_rmw(uncore, reg, clear, set); + + mutex_unlock(&hwmon->hwmon_lock); +} + +static u64 +_scale_and_shift(u32 in, u32 scale_factor, int nshift) +{ + u64 out = mul_u32_u32(scale_factor, in); + + /* Shift, rounding to nearest */ + if (nshift > 0) + out = (out + (1 << (nshift - 1))) >> nshift; + return out; +} + +/* + * This function's return type of u64 allows for the case where the scaling + * of the field taken from the 32-bit register value might cause a result to + * exceed 32 bits. + */ +static u64 +_field_read_and_scale(struct i915_hwmon_drvdata *ddat, i915_reg_t rgadr, + u32 field_msk, int field_shift, + int nshift, u32 scale_factor) +{ + struct intel_uncore *uncore = ddat->dd_uncore; + intel_wakeref_t wakeref; + u32 reg_value; + + with_intel_runtime_pm(uncore->rpm, wakeref) + reg_value = intel_uncore_read(uncore, rgadr); + + reg_value = (reg_value & field_msk) >> field_shift; + + return _scale_and_shift(reg_value, scale_factor, nshift); +} + +static void +_field_scale_and_write(struct i915_hwmon_drvdata *ddat, i915_reg_t rgadr, + u32 field_msk, int field_shift, + int nshift, unsigned int scale_factor, long lval) +{ + u32 nval; + u32 bits_to_clear; + u32 bits_to_set; + + /* Computation in 64-bits to avoid overflow. Round to nearest. */ + nval = DIV_ROUND_CLOSEST_ULL((u64)lval << nshift, scale_factor); + + bits_to_clear = field_msk; + bits_to_set = (nval << field_shift) & field_msk; + + _locked_with_pm_intel_uncore_rmw(ddat, rgadr, + bits_to_clear, bits_to_set); +} + +static ssize_t +i915_power1_max_default_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct i915_hwmon_drvdata *ddat = dev_get_drvdata(dev); + struct i915_hwmon *hwmon = ddat->dd_hwmon; + u64 val = 0; /* uapi specifies to keep visible but return 0 if unsupported */ + + if (i915_mmio_reg_valid(hwmon->rg.pkg_power_sku)) + val = _field_read_and_scale(ddat, + hwmon->rg.pkg_power_sku, + PKG_PKG_TDP, + FIELD_SHIFT(PKG_PKG_TDP), + hwmon->scl_shift_power, + SF_POWER); + return sysfs_emit(buf, "%llu\n", val); +} + +static SENSOR_DEVICE_ATTR(power1_max_default, 0444, + i915_power1_max_default_show, NULL, 0); + +static struct attribute *hwmon_attributes[] = { + &sensor_dev_attr_power1_max_default.dev_attr.attr, + NULL +}; + +static umode_t hwmon_attributes_visible(struct kobject *kobj, + struct attribute *attr, int index) +{ + struct device *dev = kobj_to_dev(kobj); + struct i915_hwmon_drvdata *ddat = dev_get_drvdata(dev); + struct drm_i915_private *i915 = ddat->dd_uncore->i915; + i915_reg_t rgadr; + + if (attr == &sensor_dev_attr_power1_max_default.dev_attr.attr) + return IS_DGFX(i915) ? attr->mode : 0; + else + return 0; + + if (!i915_mmio_reg_valid(rgadr)) + return 0; + + return attr->mode; +} + +static const struct attribute_group hwmon_attrgroup = { + .attrs = hwmon_attributes, + .is_visible = hwmon_attributes_visible, +}; + +static const struct attribute_group *hwmon_groups[] = { + &hwmon_attrgroup, + NULL +}; + +/* + * HWMON SENSOR TYPE = hwmon_power + * - Sustained Power (power1_max) + */ +static const u32 i915_config_power[] = { + HWMON_P_MAX, + 0 +}; + +static const struct hwmon_channel_info i915_power = { + .type = hwmon_power, + .config = i915_config_power, +}; + +static const struct hwmon_channel_info *i915_info[] = { + &i915_power, + NULL +}; + +static umode_t +i915_power_is_visible(const struct i915_hwmon_drvdata *ddat, u32 attr, int chan) +{ + struct i915_hwmon *hwmon = ddat->dd_hwmon; + i915_reg_t rgadr; + + switch (attr) { + case hwmon_power_max: + rgadr = hwmon->rg.pkg_rapl_limit; + break; + default: + return 0; + } + + if (!i915_mmio_reg_valid(rgadr)) + return 0; + + return 0664; +} + +static int +i915_power_read(struct i915_hwmon_drvdata *ddat, u32 attr, int chan, long *val) +{ + struct i915_hwmon *hwmon = ddat->dd_hwmon; + int ret = 0; + + switch (attr) { + case hwmon_power_max: + *val = _field_read_and_scale(ddat, + hwmon->rg.pkg_rapl_limit, + PKG_PWR_LIM_1, + FIELD_SHIFT(PKG_PWR_LIM_1), + hwmon->scl_shift_power, + SF_POWER); + break; + default: + ret = -EOPNOTSUPP; + } + + return ret; +} + +static int +i915_power_write(struct i915_hwmon_drvdata *ddat, u32 attr, int chan, long val) +{ + struct i915_hwmon *hwmon = ddat->dd_hwmon; + int ret = 0; + + switch (attr) { + case hwmon_power_max: + _field_scale_and_write(ddat, + hwmon->rg.pkg_rapl_limit, + PKG_PWR_LIM_1, + FIELD_SHIFT(PKG_PWR_LIM_1), + hwmon->scl_shift_power, + SF_POWER, val); + break; + default: + ret = -EOPNOTSUPP; + } + + return ret; +} + +static umode_t +i915_is_visible(const void *drvdata, enum hwmon_sensor_types type, + u32 attr, int channel) +{ + struct i915_hwmon_drvdata *ddat = (struct i915_hwmon_drvdata *)drvdata; + + switch (type) { + case hwmon_power: + return i915_power_is_visible(ddat, attr, channel); + default: + return 0; + } +} + +static int +i915_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, + int channel, long *val) +{ + struct i915_hwmon_drvdata *ddat = dev_get_drvdata(dev); + + switch (type) { + case hwmon_power: + return i915_power_read(ddat, attr, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static int +i915_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, + int channel, long val) +{ + struct i915_hwmon_drvdata *ddat = dev_get_drvdata(dev); + + switch (type) { + case hwmon_power: + return i915_power_write(ddat, attr, channel, val); + default: + return -EOPNOTSUPP; + } +} + +static const struct hwmon_ops i915_hwmon_ops = { + .is_visible = i915_is_visible, + .read = i915_read, + .write = i915_write, +}; + +static const struct hwmon_chip_info i915_chip_info = { + .ops = &i915_hwmon_ops, + .info = i915_info, +}; + +static void +i915_hwmon_get_preregistration_info(struct drm_i915_private *i915) +{ + struct i915_hwmon *hwmon = i915->hwmon; + struct intel_uncore *uncore = &i915->uncore; + struct i915_hwmon_drvdata *ddat = &hwmon->ddat; + intel_wakeref_t wakeref; + u32 val_sku_unit; + __le32 le_sku_unit; + + if (IS_DG1(i915) || IS_DG2(i915)) { + hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT; + hwmon->rg.pkg_power_sku = INVALID_MMIO_REG; + hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT; + } else { + 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; + } + + wakeref = intel_runtime_pm_get(uncore->rpm); + + /* + * The contents of register hwmon->rg.pkg_power_sku_unit do not change, + * so read it once and store the shift values. + * + * For some platforms, this value is defined as available "for all + * tiles", with the values consistent across all tiles. + * In this case, use the tile 0 value for all. + */ + if (i915_mmio_reg_valid(hwmon->rg.pkg_power_sku_unit)) + val_sku_unit = intel_uncore_read(uncore, + hwmon->rg.pkg_power_sku_unit); + else + val_sku_unit = 0; + + intel_runtime_pm_put(uncore->rpm, wakeref); + + le_sku_unit = cpu_to_le32(val_sku_unit); + hwmon->scl_shift_power = le32_get_bits(le_sku_unit, PKG_PWR_UNIT); + + /* + * The value of power1_max is reset to the default on reboot, but is + * not reset by a module unload/load sequence. To allow proper + * functioning after a module reload, the value for power1_max is + * restored to its original value at module unload time in + * i915_hwmon_unregister(). + */ + hwmon->power_max_initial_value = + (u32)_field_read_and_scale(ddat, + hwmon->rg.pkg_rapl_limit, + PKG_PWR_LIM_1, + FIELD_SHIFT(PKG_PWR_LIM_1), + hwmon->scl_shift_power, SF_POWER); +} + +void i915_hwmon_register(struct drm_i915_private *i915) +{ + struct device *dev = i915->drm.dev; + struct i915_hwmon *hwmon; + struct device *hwmon_dev; + struct i915_hwmon_drvdata *ddat; + + hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL); + if (!hwmon) + return; + + i915->hwmon = hwmon; + + mutex_init(&hwmon->hwmon_lock); + + ddat = &hwmon->ddat; + + ddat->dd_hwmon = hwmon; + ddat->dd_uncore = &i915->uncore; + snprintf(ddat->dd_name, sizeof(ddat->dd_name), "i915"); + + i915_hwmon_get_preregistration_info(i915); + + /* hwmon_dev points to device hwmon */ + hwmon_dev = hwmon_device_register_with_info(dev, ddat->dd_name, + ddat, + &i915_chip_info, + hwmon_groups); + + if (IS_ERR(hwmon_dev)) { + mutex_destroy(&hwmon->hwmon_lock); + i915->hwmon = NULL; + kfree(hwmon); + return; + } + + ddat->dd_hwmon_dev = hwmon_dev; +} + +void i915_hwmon_unregister(struct drm_i915_private *i915) +{ + struct i915_hwmon *hwmon; + struct i915_hwmon_drvdata *ddat; + + hwmon = fetch_and_zero(&i915->hwmon); + if (!hwmon) + return; + + ddat = &hwmon->ddat; + + if (hwmon->power_max_initial_value) { + /* Restore power1_max. */ + _field_scale_and_write(ddat, hwmon->rg.pkg_rapl_limit, + PKG_PWR_LIM_1, + FIELD_SHIFT(PKG_PWR_LIM_1), + hwmon->scl_shift_power, + SF_POWER, + hwmon->power_max_initial_value); + } + + if (ddat->dd_hwmon_dev) + hwmon_device_unregister(ddat->dd_hwmon_dev); + + mutex_destroy(&hwmon->hwmon_lock); + + kfree(hwmon); +} diff --git a/drivers/gpu/drm/i915/i915_hwmon.h b/drivers/gpu/drm/i915/i915_hwmon.h new file mode 100644 index 000000000000..396b758aefce --- /dev/null +++ b/drivers/gpu/drm/i915/i915_hwmon.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: MIT */ + +/* + * Copyright © 2020 Intel Corporation + */ + +#ifndef __INTEL_HWMON_H__ +#define __INTEL_HWMON_H__ + +#include +#include +#include +#include "i915_reg.h" + +struct drm_i915_private; + +struct i915_hwmon_reg { + i915_reg_t pkg_power_sku_unit; + i915_reg_t pkg_power_sku; + i915_reg_t pkg_rapl_limit; +}; + +struct i915_hwmon_drvdata { + struct i915_hwmon *dd_hwmon; + struct intel_uncore *dd_uncore; + struct device *dd_hwmon_dev; + char dd_name[12]; +}; + +struct i915_hwmon { + struct i915_hwmon_drvdata ddat; + + struct mutex hwmon_lock; /* counter overflow logic and rmw */ + + struct i915_hwmon_reg rg; + + u32 power_max_initial_value; + + int scl_shift_power; +}; + +void i915_hwmon_register(struct drm_i915_private *i915); +void i915_hwmon_unregister(struct drm_i915_private *i915); +#endif diff --git a/drivers/gpu/drm/i915/i915_reg.h b/drivers/gpu/drm/i915/i915_reg.h index d8579ab9384c..1c570c706ff8 100644 --- a/drivers/gpu/drm/i915/i915_reg.h +++ b/drivers/gpu/drm/i915/i915_reg.h @@ -1866,6 +1866,21 @@ #define POWER_LIMIT_4_MASK REG_BIT(9) #define POWER_LIMIT_1_MASK REG_BIT(11) #define POWER_LIMIT_2_MASK REG_BIT(12) +/* + * *_PACKAGE_POWER_SKU - SKU power and timing parameters. + * Used herein as a 64-bit register. + * These masks are defined using GENMASK_ULL as REG_GENMASK is limited to u32 + * and as GENMASK is "long" and therefore 32-bits on a 32-bit system. + * PKG_PKG_TDP, PKG_MIN_PWR, and PKG_MAX_PWR are scaled in the same way as + * PKG_PWR_LIM_*, above. + * PKG_MAX_WIN has sub-fields for x and y, and has the value: is 1.x * 2^y. + */ +#define PKG_PKG_TDP GENMASK_ULL(14, 0) +#define PKG_MIN_PWR GENMASK_ULL(30, 16) +#define PKG_MAX_PWR GENMASK_ULL(46, 32) +#define PKG_MAX_WIN GENMASK_ULL(54, 48) +#define PKG_MAX_WIN_Y GENMASK_ULL(54, 53) +#define PKG_MAX_WIN_X GENMASK_ULL(52, 48) #define CHV_CLK_CTL1 _MMIO(0x101100) #define VLV_CLK_CTL2 _MMIO(0x101104) diff --git a/drivers/gpu/drm/i915/intel_mchbar_regs.h b/drivers/gpu/drm/i915/intel_mchbar_regs.h index 2aad2f0cc8db..247561d7974c 100644 --- a/drivers/gpu/drm/i915/intel_mchbar_regs.h +++ b/drivers/gpu/drm/i915/intel_mchbar_regs.h @@ -191,11 +191,20 @@ #define GEN6_GT_PERF_STATUS _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5948) #define GEN6_RP_STATE_LIMITS _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5994) +#define PCU_PACKAGE_POWER_SKU_UNIT _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5938) +#define PKG_PWR_UNIT REG_GENMASK(3, 0) +#define PKG_TIME_UNIT REG_GENMASK(19, 16) + #define GEN6_RP_STATE_CAP _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5998) #define RP0_CAP_MASK REG_GENMASK(7, 0) #define RP1_CAP_MASK REG_GENMASK(15, 8) #define RPN_CAP_MASK REG_GENMASK(23, 16) +#define PCU_PACKAGE_RAPL_LIMIT _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x59a0) +#define PKG_PWR_LIM_1 REG_GENMASK(14, 0) +#define PKG_PWR_LIM_1_EN REG_BIT(15) +#define PKG_PWR_LIM_1_TIME REG_GENMASK(23, 17) + /* snb MCH registers for priority tuning */ #define MCH_SSKPD _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5d10) #define SSKPD_NEW_WM0_MASK_HSW REG_GENMASK64(63, 56) From patchwork Mon May 23 11:08:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nilawar, Badal" X-Patchwork-Id: 12858886 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 782C0C433EF for ; Mon, 23 May 2022 11:08:32 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3483D10E460; Mon, 23 May 2022 11:08:29 +0000 (UTC) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id E4D5F10F21D for ; Mon, 23 May 2022 11:08:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653304104; x=1684840104; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MHtJmkRpMQD+vIHVdihUBvInVnf7tqKJ9VQAVJPmlIA=; b=egRm/tQrFMx2jgaed5gwICFpg5MeEmswaxN17ioR+Vv/GgjXfNi9jGme UWFCnW8OtrqmAvOpnNl349qlNNJMl/bec2xaaZcPMjBFJ7/llnhvvwDlO 9RVsDZn/yOR3DPLBOLNtGdk2sVpjEa25Op3MgkRo9DpxIkbd9+LaVVcJv 6h1tRKdA61o7O2uFam05vmItEzP5PGK53/7QQhCU31s3Mmixg7coVs4w9 k40xOrL7rfjG5sH6RRCl748KJHnm45kEgF53Z2KBWzsF9g9QO0R19j1hX nGezKv1GGHDu/vaHjdarpTDQ5+29p6MCvRn78uM0Fmg4oz7GrN01Rp78v A==; X-IronPort-AV: E=McAfee;i="6400,9594,10355"; a="359579409" X-IronPort-AV: E=Sophos;i="5.91,246,1647327600"; d="scan'208";a="359579409" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2022 04:08:24 -0700 X-IronPort-AV: E=Sophos;i="5.91,246,1647327600"; d="scan'208";a="600589649" Received: from bnilawar-desk.iind.intel.com ([10.145.162.36]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2022 04:08:22 -0700 From: Badal Nilawar To: intel-gfx@lists.freedesktop.org Date: Mon, 23 May 2022 16:38:40 +0530 Message-Id: <20220523110841.1151431-3-badal.nilawar@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523110841.1151431-1-badal.nilawar@intel.com> References: <20220523110841.1151431-1-badal.nilawar@intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH 2/3] drm/i915/hwmon: Add HWMON energy support X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Dale B Stimson As part of the System Managemenent Interface (SMI), use the HWMON subsystem to display energy utilization Signed-off-by: Dale B Stimson Signed-off-by: Ashutosh Dixit Signed-off-by: Riana Tauro Signed-off-by: Badal Nilawar --- .../ABI/testing/sysfs-driver-intel-i915-hwmon | 16 ++ drivers/gpu/drm/i915/i915_hwmon.c | 155 +++++++++++++++++- drivers/gpu/drm/i915/i915_hwmon.h | 11 ++ drivers/gpu/drm/i915/intel_mchbar_regs.h | 2 + 4 files changed, 183 insertions(+), 1 deletion(-) diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon index c680dd842358..7574421c326f 100644 --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon @@ -1,3 +1,19 @@ +What: /sys/devices/.../hwmon/hwmon/energy1_input +Date: June 2021 +KernelVersion: 5.14 +Contact: dri-devel@lists.freedesktop.org +Description: RO. Energy input of device in microjoules. + + The returned textual representation is an unsigned integer + number that can be stored in 64-bits. Warning: The hardware + register is 32-bits wide and can overflow by wrapping around. + A single wrap-around between calls to read this value can + be detected and will be accounted for in the returned value. + At a power consumption of 1 watt, the 32-bit hardware register + would wrap-around approximately every 3 days. + + Only supported for particular Intel i915 graphics platforms. + What: /sys/devices/.../hwmon/hwmon/power1_max Date: June 2021 KernelVersion: 5.14 diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c index b94c11f2517f..b35c4de73f30 100644 --- a/drivers/gpu/drm/i915/i915_hwmon.c +++ b/drivers/gpu/drm/i915/i915_hwmon.c @@ -18,8 +18,10 @@ /* * SF_* - scale factors for particular quantities according to hwmon spec. * - power - microwatts + * - energy - microjoules */ #define SF_POWER 1000000 +#define SF_ENERGY 1000000 #define FIELD_SHIFT(__mask) \ (BUILD_BUG_ON_ZERO(!__builtin_constant_p(__mask)) + \ @@ -94,6 +96,136 @@ _field_scale_and_write(struct i915_hwmon_drvdata *ddat, i915_reg_t rgadr, bits_to_clear, bits_to_set); } +/* + * _i915_energy1_input_sub - A custom function to obtain energy1_input. + * Use a custom function instead of the usual hwmon helpers in order to + * guarantee 64-bits of result to user-space. + * Units are microjoules. + * + * The underlying hardware register is 32-bits and is subject to overflow. + * This function compensates for overflow of the 32-bit register by detecting + * wrap-around and incrementing an overflow counter. + * This only works if the register is sampled often enough to avoid + * missing an instance of overflow - achieved either by repeated + * queries through the API, or via a possible timer (future - TBD) that + * ensures values are read often enough to catch all overflows. + * + * How long before overflow? For example, with an example scaling bit + * shift of 14 bits (see register *PACKAGE_POWER_SKU_UNIT) and a power draw of + * 1000 watts, the 32-bit counter will overflow in approximately 4.36 minutes. + * + * Examples: + * 1 watt: (2^32 >> 14) / 1 W / (60 * 60 * 24) secs/day -> 3 days + * 1000 watts: (2^32 >> 14) / 1000 W / 60 secs/min -> 4.36 minutes + */ +static int +_i915_energy1_input_sub(struct i915_hwmon_drvdata *ddat, u64 *energy) +{ + struct intel_uncore *uncore = ddat->dd_uncore; + struct i915_hwmon *hwmon = ddat->dd_hwmon; + struct i915_energy_info *pei = &ddat->dd_ei; + int nshift = hwmon->scl_shift_energy; + intel_wakeref_t wakeref; + u32 reg_value; + u64 vlo; + u64 vhi; + i915_reg_t rgaddr; + + rgaddr = hwmon->rg.energy_status_all; + + if (!i915_mmio_reg_valid(rgaddr)) + return -EOPNOTSUPP; + + mutex_lock(&hwmon->hwmon_lock); + + with_intel_runtime_pm(uncore->rpm, wakeref) + reg_value = intel_uncore_read(uncore, rgaddr); + + /* + * The u32 register concatenated with the u32 overflow counter + * gives an effective energy counter size of 64-bits. However, the + * computations below are done modulo 2^96 to avoid overflow during + * scaling in the conversion to microjoules. + * + * The low-order 64-bits of the resulting quantity are returned to + * the caller in units of microjoules, encoded into a decimal string. + * + * For a power of 1000 watts, 64 bits in units of microjoules will + * overflow after 584 years. + */ + + if (pei->energy_counter_prev > reg_value) + pei->energy_counter_overflow++; + + pei->energy_counter_prev = reg_value; + + /* + * 64-bit variables vlo and vhi are used for the scaling process. + * The 96-bit counter value is composed from the two 64-bit variables + * vhi and vlo thusly: counter == vhi << 32 + vlo . + * The 32-bits of overlap between the two variables is convenient for + * handling overflows out of vlo. + */ + + vlo = reg_value; + vhi = pei->energy_counter_overflow; + + mutex_unlock(&hwmon->hwmon_lock); + + vlo = SF_ENERGY * vlo; + + /* Prepare to round to nearest */ + if (nshift > 0) + vlo += 1 << (nshift - 1); + + /* + * Anything in the upper-32 bits of vlo gets added into vhi here, + * and then cleared from vlo. + */ + vhi = (SF_ENERGY * vhi) + (vlo >> 32); + vlo &= 0xffffffffULL; + + /* + * Apply the right shift. + * - vlo shifted by itself. + * - vlo receiving what's shifted out of vhi. + * - vhi shifted by itself + */ + vlo = vlo >> nshift; + vlo |= (vhi << (32 - nshift)) & 0xffffffffULL; + vhi = vhi >> nshift; + + /* Combined to get a 64-bit result in vlo. */ + vlo |= (vhi << 32); + + *energy = vlo; + + return 0; +} + +static ssize_t +i915_energy1_input_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct i915_hwmon_drvdata *ddat = dev_get_drvdata(dev); + ssize_t ret = 0; + u64 energy; + + if (!_i915_energy1_input_sub(ddat, &energy)) + ret = sysfs_emit(buf, "%llu\n", energy); + + return ret; +} + +int +i915_energy_status_get(struct drm_i915_private *i915, u64 *energy) +{ + struct i915_hwmon *hwmon = i915->hwmon; + struct i915_hwmon_drvdata *ddat = &hwmon->ddat; + + return _i915_energy1_input_sub(ddat, energy); +} + static ssize_t i915_power1_max_default_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -114,9 +246,12 @@ i915_power1_max_default_show(struct device *dev, struct device_attribute *attr, static SENSOR_DEVICE_ATTR(power1_max_default, 0444, i915_power1_max_default_show, NULL, 0); +static SENSOR_DEVICE_ATTR(energy1_input, 0444, + i915_energy1_input_show, NULL, 0); static struct attribute *hwmon_attributes[] = { &sensor_dev_attr_power1_max_default.dev_attr.attr, + &sensor_dev_attr_energy1_input.dev_attr.attr, NULL }; @@ -126,9 +261,12 @@ static umode_t hwmon_attributes_visible(struct kobject *kobj, struct device *dev = kobj_to_dev(kobj); struct i915_hwmon_drvdata *ddat = dev_get_drvdata(dev); struct drm_i915_private *i915 = ddat->dd_uncore->i915; + struct i915_hwmon *hwmon = ddat->dd_hwmon; i915_reg_t rgadr; - if (attr == &sensor_dev_attr_power1_max_default.dev_attr.attr) + if (attr == &sensor_dev_attr_energy1_input.dev_attr.attr) + rgadr = hwmon->rg.energy_status_all; + else if (attr == &sensor_dev_attr_power1_max_default.dev_attr.attr) return IS_DGFX(i915) ? attr->mode : 0; else return 0; @@ -291,6 +429,7 @@ i915_hwmon_get_preregistration_info(struct drm_i915_private *i915) struct i915_hwmon *hwmon = i915->hwmon; struct intel_uncore *uncore = &i915->uncore; struct i915_hwmon_drvdata *ddat = &hwmon->ddat; + struct i915_energy_info *pei; intel_wakeref_t wakeref; u32 val_sku_unit; __le32 le_sku_unit; @@ -299,10 +438,14 @@ i915_hwmon_get_preregistration_info(struct drm_i915_private *i915) hwmon->rg.pkg_power_sku_unit = PCU_PACKAGE_POWER_SKU_UNIT; hwmon->rg.pkg_power_sku = INVALID_MMIO_REG; hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT; + hwmon->rg.energy_status_all = PCU_PACKAGE_ENERGY_STATUS; + hwmon->rg.energy_status_tile = INVALID_MMIO_REG; } else { 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; + hwmon->rg.energy_status_all = INVALID_MMIO_REG; + hwmon->rg.energy_status_tile = INVALID_MMIO_REG; } wakeref = intel_runtime_pm_get(uncore->rpm); @@ -321,10 +464,20 @@ i915_hwmon_get_preregistration_info(struct drm_i915_private *i915) else val_sku_unit = 0; + pei = &ddat->dd_ei; + pei->energy_counter_overflow = 0; + + if (i915_mmio_reg_valid(hwmon->rg.energy_status_all)) + pei->energy_counter_prev = + intel_uncore_read(uncore, hwmon->rg.energy_status_all); + else + pei->energy_counter_prev = 0; + intel_runtime_pm_put(uncore->rpm, wakeref); le_sku_unit = cpu_to_le32(val_sku_unit); hwmon->scl_shift_power = le32_get_bits(le_sku_unit, PKG_PWR_UNIT); + hwmon->scl_shift_energy = le32_get_bits(le_sku_unit, PKG_ENERGY_UNIT); /* * The value of power1_max is reset to the default on reboot, but is diff --git a/drivers/gpu/drm/i915/i915_hwmon.h b/drivers/gpu/drm/i915/i915_hwmon.h index 396b758aefce..1c70a7056be7 100644 --- a/drivers/gpu/drm/i915/i915_hwmon.h +++ b/drivers/gpu/drm/i915/i915_hwmon.h @@ -18,12 +18,20 @@ struct i915_hwmon_reg { i915_reg_t pkg_power_sku_unit; i915_reg_t pkg_power_sku; i915_reg_t pkg_rapl_limit; + i915_reg_t energy_status_all; + i915_reg_t energy_status_tile; +}; + +struct i915_energy_info { + u32 energy_counter_overflow; + u32 energy_counter_prev; }; struct i915_hwmon_drvdata { struct i915_hwmon *dd_hwmon; struct intel_uncore *dd_uncore; struct device *dd_hwmon_dev; + struct i915_energy_info dd_ei; /* Energy info for energy1_input */ char dd_name[12]; }; @@ -37,8 +45,11 @@ struct i915_hwmon { u32 power_max_initial_value; int scl_shift_power; + int scl_shift_energy; }; void i915_hwmon_register(struct drm_i915_private *i915); void i915_hwmon_unregister(struct drm_i915_private *i915); + +int i915_energy_status_get(struct drm_i915_private *i915, u64 *energy); #endif diff --git a/drivers/gpu/drm/i915/intel_mchbar_regs.h b/drivers/gpu/drm/i915/intel_mchbar_regs.h index 247561d7974c..d6dc6bc95627 100644 --- a/drivers/gpu/drm/i915/intel_mchbar_regs.h +++ b/drivers/gpu/drm/i915/intel_mchbar_regs.h @@ -193,7 +193,9 @@ #define GEN6_RP_STATE_LIMITS _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5994) #define PCU_PACKAGE_POWER_SKU_UNIT _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5938) #define PKG_PWR_UNIT REG_GENMASK(3, 0) +#define PKG_ENERGY_UNIT REG_GENMASK(12, 8) #define PKG_TIME_UNIT REG_GENMASK(19, 16) +#define PCU_PACKAGE_ENERGY_STATUS _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x593c) #define GEN6_RP_STATE_CAP _MMIO(MCHBAR_MIRROR_BASE_SNB + 0x5998) #define RP0_CAP_MASK REG_GENMASK(7, 0) From patchwork Mon May 23 11:08:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Nilawar, Badal" X-Patchwork-Id: 12858885 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 96563C433F5 for ; Mon, 23 May 2022 11:08:29 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 68282891A4; Mon, 23 May 2022 11:08:28 +0000 (UTC) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3EFF610E460 for ; Mon, 23 May 2022 11:08:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653304107; x=1684840107; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7b5xgNaDjOb9hpgC8ttRXuW0UACfLLHag3n0ViQ1uQ0=; b=B2BU1d/nKPiMs5TA5tKHtAmbC7vH11sZ7Lqn77CE9Tn8xF8bQPGvP3Vh 4f0wjsRvF2WOOoHgGUx/5xldiZMfkdxf7073f9Gi/bmCyPAfAGNc2QQ++ a+KrYAEIccFXszQ7oKpRcF5j04g3PEORtzr3bXfoxqt1w98ngNPyFqbxp y2Nn4ilWv+/gEHmfz/Y/+WoOhnaA890Mc3XzO85Ky1xi3l+he5vumRlfu KKZuVeHNELKxIrCVNZAZwtwqLo3X6z5DvZcR/NM83y/9eEzVm67eD9JgU uIdr8+aFaqpXgNfSXUQMG+HWYLoWNefaFdeufhN2EH/L4tR56odQxhcny g==; X-IronPort-AV: E=McAfee;i="6400,9594,10355"; a="359579425" X-IronPort-AV: E=Sophos;i="5.91,246,1647327600"; d="scan'208";a="359579425" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2022 04:08:26 -0700 X-IronPort-AV: E=Sophos;i="5.91,246,1647327600"; d="scan'208";a="600589657" Received: from bnilawar-desk.iind.intel.com ([10.145.162.36]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2022 04:08:24 -0700 From: Badal Nilawar To: intel-gfx@lists.freedesktop.org Date: Mon, 23 May 2022 16:38:41 +0530 Message-Id: <20220523110841.1151431-4-badal.nilawar@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220523110841.1151431-1-badal.nilawar@intel.com> References: <20220523110841.1151431-1-badal.nilawar@intel.com> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH 3/3] drm/i915/hwmon: Add HWMON current voltage support X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Riana Tauro As part of the System Managemenent Interface (SMI), use the HWMON subsystem to display current voltage Cc: Anshuman Gupta Signed-off-by: Riana Tauro Signed-off-by: Badal Nilawar --- .../ABI/testing/sysfs-driver-intel-i915-hwmon | 8 +++ drivers/gpu/drm/i915/gt/intel_gt_regs.h | 4 ++ drivers/gpu/drm/i915/i915_hwmon.c | 57 +++++++++++++++++++ drivers/gpu/drm/i915/i915_hwmon.h | 1 + 4 files changed, 70 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon index 7574421c326f..c70e06dd0333 100644 --- a/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon +++ b/Documentation/ABI/testing/sysfs-driver-intel-i915-hwmon @@ -33,3 +33,11 @@ Contact: dri-devel@lists.freedesktop.org Description: RO. Card default power limit (default TDP setting). Only supported for particular Intel i915 graphics platforms. + +What: /sys/devices/.../hwmon/hwmon/in0_input +Date: May 2022 +KernelVersion: 5.18 +Contact: dri-devel@lists.freedesktop.org +Description: RO. Current Voltage in millivolt. + + Only supported for particular Intel i915 graphics platforms. diff --git a/drivers/gpu/drm/i915/gt/intel_gt_regs.h b/drivers/gpu/drm/i915/gt/intel_gt_regs.h index 7246eb870c7e..0adeca083de1 100644 --- a/drivers/gpu/drm/i915/gt/intel_gt_regs.h +++ b/drivers/gpu/drm/i915/gt/intel_gt_regs.h @@ -1475,6 +1475,10 @@ #define GEN6_GT_GFX_RC6p _MMIO(0x13810c) #define GEN6_GT_GFX_RC6pp _MMIO(0x138110) #define VLV_RENDER_C0_COUNT _MMIO(0x138118) + +#define GEN12_RPSTAT1 _MMIO(0x1381b4) +#define GEN12_VOLTAGE_MASK REG_GENMASK(10, 0) + #define VLV_MEDIA_C0_COUNT _MMIO(0x13811c) #define GEN11_GT_INTR_DW(x) _MMIO(0x190018 + ((x) * 4)) diff --git a/drivers/gpu/drm/i915/i915_hwmon.c b/drivers/gpu/drm/i915/i915_hwmon.c index b35c4de73f30..e5f4293a8fdb 100644 --- a/drivers/gpu/drm/i915/i915_hwmon.c +++ b/drivers/gpu/drm/i915/i915_hwmon.c @@ -12,6 +12,7 @@ #include #include "i915_drv.h" +#include "gt/intel_gt_regs.h" #include "i915_hwmon.h" #include "intel_mchbar_regs.h" @@ -301,8 +302,23 @@ static const struct hwmon_channel_info i915_power = { .config = i915_config_power, }; +/* + * HWMON SENSOR TYPE = hwmon_in + * - Voltage Input value (in0_input) + */ +static const u32 i915_config_in[] = { + HWMON_I_INPUT, + 0 +}; + +static const struct hwmon_channel_info i915_in = { + .type = hwmon_in, + .config = i915_config_in, +}; + static const struct hwmon_channel_info *i915_info[] = { &i915_power, + &i915_in, NULL }; @@ -370,6 +386,41 @@ i915_power_write(struct i915_hwmon_drvdata *ddat, u32 attr, int chan, long val) return ret; } +static umode_t +i915_in_is_visible(const struct i915_hwmon_drvdata *ddat, u32 attr) +{ + struct drm_i915_private *i915 = ddat->dd_uncore->i915; + + switch (attr) { + case hwmon_in_input: + return (IS_DG1(i915) || IS_DG2(i915)) ? 0444 : 0; + default: + return 0; + } + + return 0444; +} + +static int +i915_in_read(struct i915_hwmon_drvdata *ddat, u32 attr, long *val) +{ + struct i915_hwmon *hwmon = ddat->dd_hwmon; + intel_wakeref_t wakeref; + u32 reg_value; + + switch (attr) { + case hwmon_in_input: + with_intel_runtime_pm(ddat->dd_uncore->rpm, wakeref) + reg_value = intel_uncore_read(ddat->dd_uncore, hwmon->rg.gt_perf_status); + *val = DIV_ROUND_CLOSEST(REG_FIELD_GET(GEN12_VOLTAGE_MASK, reg_value) * 25, 10); + break; + default: + return -EOPNOTSUPP; + } + + return 0; +} + static umode_t i915_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, int channel) @@ -379,6 +430,8 @@ i915_is_visible(const void *drvdata, enum hwmon_sensor_types type, switch (type) { case hwmon_power: return i915_power_is_visible(ddat, attr, channel); + case hwmon_in: + return i915_in_is_visible(ddat, attr); default: return 0; } @@ -393,6 +446,8 @@ i915_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, switch (type) { case hwmon_power: return i915_power_read(ddat, attr, channel, val); + case hwmon_in: + return i915_in_read(ddat, attr, val); default: return -EOPNOTSUPP; } @@ -440,12 +495,14 @@ i915_hwmon_get_preregistration_info(struct drm_i915_private *i915) hwmon->rg.pkg_rapl_limit = PCU_PACKAGE_RAPL_LIMIT; hwmon->rg.energy_status_all = PCU_PACKAGE_ENERGY_STATUS; hwmon->rg.energy_status_tile = INVALID_MMIO_REG; + hwmon->rg.gt_perf_status = GEN12_RPSTAT1; } else { 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; hwmon->rg.energy_status_all = INVALID_MMIO_REG; hwmon->rg.energy_status_tile = INVALID_MMIO_REG; + hwmon->rg.gt_perf_status = INVALID_MMIO_REG; } wakeref = intel_runtime_pm_get(uncore->rpm); diff --git a/drivers/gpu/drm/i915/i915_hwmon.h b/drivers/gpu/drm/i915/i915_hwmon.h index 1c70a7056be7..90ae4acae74e 100644 --- a/drivers/gpu/drm/i915/i915_hwmon.h +++ b/drivers/gpu/drm/i915/i915_hwmon.h @@ -20,6 +20,7 @@ struct i915_hwmon_reg { i915_reg_t pkg_rapl_limit; i915_reg_t energy_status_all; i915_reg_t energy_status_tile; + i915_reg_t gt_perf_status; }; struct i915_energy_info {