hwmon: Add amd_energy driver to report energy counters
Subsystems (2)
PATCH 1/2 hwmon: Add amd_energy driver to report energy counters
This patch adds hwmon based amd_energy driver support for family 17h processors from AMD.
The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Uses topology_max_packages() to get number of sockets.
3. Uses num_present_cpus() to get the number of CPUs.
4. Reports the energy units via energy_unit sysfs entry
--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.c
drivers/hwmon/Kconfig +10 -0
@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86
drivers/hwmon/Makefile +1 -0
@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.o
drivers/hwmon/amd_energy.c new file +273 -0
273 lines not on this page · raw message
Signature · 1 line
2.16.4
PATCH 2/2 hwmon: (amd_energy) Add documentation
Document amd_energy driver with all chips supported by it.
--- Documentation/hwmon/amd_energy.rst | 78 ++++++++++++++++++++++++++++++++++++++ Documentation/hwmon/index.rst | 1 + 2 files changed, 79 insertions(+) create mode 100644 Documentation/hwmon/amd_energy.rst
Documentation/hwmon/amd_energy.rst new file +78 -0
@@ -0,0 +1,78 @@ +Kernel driver amd_energy +========================== + +Supported chips: + +* AMD Family 17h Processors + + Prefix: 'amd_energy' + + Addresses used: RAPL MSRs + + Datasheets: + + - Processor Programming Reference (PPR) for + AMD Family 17h Model 01h, Revision B1 Processors + - Preliminary Processor Programming Reference (PPR) for + AMD Family 17h Model 31h, Revision B0 Processors + +Author: Naveen Krishna Chatradhi <[email protected]> + +Description +----------- + +The Energy driver exposes the energy counters that are +reported via the Running Average Power Limit (RAPL) +Model-specific Registers (MSRs) via the hardware monitor +(HWMON) sysfs interface. + +1. Power, Energy and Time Units + * MSR_RAPL_POWER_UNIT/ C001_0299: + -> 32-bit(RO), Configured in BIOS + -> shared with all cores in the socket + + * A sysfs entry "energy_units" is created to show the + [ESU] energy units in joules + +2. Energy consumed by each Core + * MSR_CORE_ENERGY_STATUS/ C001_029A: + -> 32-bitRO, Accumulator, core-level power reporting + + * A sysfs entry "core_energy%d_input" for each core is + created to show the core energy counter value. + +3. Energy consumed by Socket + * MSR_PACKAGE_ENERGY_STATUS/ C001_029B: + -> 32-bitRO, Accumulator, socket-level power reporting, + -> shared with all cores in socket + + * A sysfs entry "socket_energy%d_input" for each socket + is created to show the socket energy counter value. + +Energy Caluclation: + +Energy information (in Joules) is based on the multiplier, +1/2^ESU; where ESU is an unsigned integer read from +MSR_RAPL_POWER_UNIT register. Default value is 10000b, +indicating energy status unit is 15.3 micro-Joules increment. + +These registers are updated every 1ms and cleared on +reset of the system. + +Users calculate power for a given domain by calculating + dEnergy/dTime for that domain. + +Sysfs attributes +---------------- + +======================= ================================= + +core_energy[N]_input Measured input core energy + [one sysfs entry for each core] + +socket_energy[N]_input Measured input socket energy + [one sysfs entry for each socket] + +energy_units ESU Energy Status Units. + +======================= ==================================
Documentation/hwmon/index.rst +1 -0
@@ -39,6 +39,7 @@ Hardware Monitoring Kernel Drivers adt7470 adt7475 amc6821 + amd_energy asb100 asc7621 aspeed-pwm-tacho
Signature · 1 line
2.16.4
Guenter Roeck
maintainerQuoted 69 lines · Naveen Krishna Chatradhi
This patch adds hwmon based
amd_energydriver support for family 17h processors from AMD.The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Usestopology_max_packages()to get number of sockets.
3. Usesnum_present_cpus()to get the number of CPUs.
4. Reports the energy units viaenergy_unitsysfs entryCc Guenter Roeck <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.cdrivers/hwmon/Kconfig +10
-0@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86drivers/hwmon/Makefile +1
-0@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.odrivers/hwmon/amd_energy.c new file +9
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2019 Advanced Micro Devices, Inc. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h>
drivers/hwmon/amd_energy.c line 19
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+#include <linux/types.h> +#include <linux/device.h> +#include <linux/sysfs.h> +#include <linux/cpu.h> +#include <linux/list.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/processor.h> +#include <linux/platform_device.h> +#include <linux/cpumask.h>
Alphabetic include file order, please.
Quoted 36 lines
drivers/hwmon/amd_energy.c new file +36
-0+ +#include <asm/cpu_device_id.h> + +#define DRVNAME "amd_energy" + +#define ENERGY_PWR_UNIT_MSR 0xC0010299 +#define ENERGY_CORE_MSR 0xC001029A +#define ENERGY_PCK_MSR 0xC001029B + +#define AMD_TIME_UNIT_MASK 0xF0000 +#define AMD_ENERGY_UNIT_MASK 0x01F00 +#define AMD_POWER_UNIT_MASK 0x0000F + +#define ENERGY_STATUS_MASK 0xffffffff + +#define AMD_FAM_17 0x17/* ZP, SSP */ + +/* Useful macros */ +#define AMD_CPU_FAM_ANY (_family, _model) \ +{ \ + .vendor = X86_VENDOR_AMD, \ + .family = _family, \ + .model = _model, \ + .feature = X86_FEATURE_ANY, \ +} + +#define AMD_CPU_FAM (_model) \ + AMD_CPU_FAM_ANY(AMD_FAM_##_model, X86_MODEL_ANY) + +static struct device_attribute units_attr; + +struct sensor_data { +unsigned int scale; +union { +unsigned int cpu_nr; +unsigned int sock_nr;
drivers/hwmon/amd_energy.c line 65
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ }; +struct device_attribute dev_attr_input; +char input[32 ]; +}; + +struct amd_energy_sensors { +struct sensor_data *data; +struct attribute **attrs; +struct attribute_group group; +const struct attribute_group *groups[1 ];
Even if acceptable, this would be wrong. The list of groups ends with an empty entry, meaning this array would have to have at least two entries (one for the terminator).
Quoted 21 lines
drivers/hwmon/amd_energy.c new file +21
-0+}; + +static ssize_t amd_units_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +uint64_t rapl_units; +uint64_t energy_unit; +int ret =0 ; + + ret =rdmsrl_safe (ENERGY_PWR_UNIT_MSR , &rapl_units); +if (ret) +return -EAGAIN ; + + energy_unit = (rapl_units &AMD_ENERGY_UNIT_MASK ) >>8 ; + +return snprintf (buffer,24 ,"%llu\n" , energy_unit); +} + +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{
drivers/hwmon/amd_energy.c line 96
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);
It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 49 lines
drivers/hwmon/amd_energy.c new file +49
-0+} + +static ssize_t amd_sock_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; +int cpu, cpu_nr; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + +for_each_possible_cpu (cpu) { +struct cpuinfo_x86 *c = &cpu_data (cpu); + +if (c->initialized && c->logical_die_id == sensor->sock_nr) { + cpu_nr = cpu; +break ; + } + cpu_nr =0 ; + } + + ret =rdmsrl_safe_on_cpu (cpu_nr,ENERGY_PCK_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value); +} + +static int amd_energy_probe (struct platform_device *pdev) +{ +struct amd_energy_sensors *amd_sensors; +struct device *hwdev, *dev = &pdev->dev; +int nr_cpus, nr_socks, idx =0 ; + + nr_cpus =num_present_cpus (); + nr_socks =topology_max_packages (); + + amd_sensors =devm_kzalloc (dev,sizeof (*amd_sensors),GFP_KERNEL ); +if (!amd_sensors) +return -ENOMEM ; + + amd_sensors->data =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->data),GFP_KERNEL ); +if (!amd_sensors->data) +return -ENOMEM ; + + amd_sensors->attrs =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->attrs),GFP_KERNEL );
drivers/hwmon/amd_energy.c line 155
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+if (!amd_sensors->attrs) +return -ENOMEM ; + +/* populate attributes for number of cpus. */ + for (idx = 0; idx < ; idx++) { +struct sensor_data *sensor = &amd_sensors->data[idx]; + +snprintf (sensor->input,sizeof (sensor->input), +"core_energy%d_input" , idx); +
This is unacceptable. Please use standard attributes (energyX_input). If you want to report what the sensor is for, use labels.
Quoted 18 lines
drivers/hwmon/amd_energy.c new file +18
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_core_u64_show; + + sensor->cpu_nr = idx; + amd_sensors->attrs[idx] = &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[idx]); + } + +/* populate attributes for number of sockets. */ +for (idx =0 ; idx < nr_socks; idx++) { +struct sensor_data *sensor = + &amd_sensors->data[nr_cpus + idx]; + +snprintf (sensor->input, +sizeof (sensor->input),"sock_energy%d_input" , idx); +
drivers/hwmon/amd_energy.c line 183
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_sock_u64_show; + + sensor->sock_nr = idx; + amd_sensors->attrs[nr_cpus + idx] = + &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[nr_cpus + idx]); + }
This all makes me wonder what is reported for inactive/disabled CPUs.
drivers/hwmon/amd_energy.c line 191
Quoted 8 lines
drivers/hwmon/amd_energy.c new file +8
-0+ + amd_sensors->group.attrs = amd_sensors->attrs; + amd_sensors->groups[0 ] = &amd_sensors->group; + +platform_set_drvdata (pdev, amd_sensors); + + hwdev =devm_hwmon_device_register_with_groups (dev, +"amd_energy" , amd_sensors, amd_sensors->groups);
Please rework the driver to use the devm_hwmon_device_register_with_info() API.
drivers/hwmon/amd_energy.c line 200
Quoted 9 lines
drivers/hwmon/amd_energy.c new file +9
-0+if (!hwdev) +return PTR_ERR_OR_ZERO (hwdev); + +/* populate attributes for energy units */ + units_attr.attr.name ="energy_units" ; + units_attr.attr.mode =0444 ; + units_attr.show = amd_units_u64_show; + +return sysfs_create_file (&hwdev->kobj, &units_attr.attr);
This is irrelevant for this driver. Units are fixed. Besides, registering sysfs attributes after driver registration is racy and won't be accepted. Non-standard attributes can be added with the groups argument of devm_hwmon_device_register_with_info() (not that I see any here).
Quoted 31 lines
drivers/hwmon/amd_energy.c new file +31
-0+} + +static int amd_energy_remove (struct platform_device *pdev) +{ +sysfs_remove_file (&pdev->dev.kobj, &units_attr.attr); +return 0 ; +} + +static const struct platform_device_id amd_energy_ids[] = { + { .name =DRVNAME , }, + {} +}; +MODULE_DEVICE_TABLE (platform, amd_energy_ids); + +static struct platform_driver amd_energy_driver = { + .probe = amd_energy_probe, + .remove = amd_energy_remove, + .id_table = amd_energy_ids, + .driver = { + .name =DRVNAME , + }, +}; + +static struct platform_device *amd_energy_platdev; + +static const struct x86_cpu_id cpu_ids[]__initconst = { +AMD_CPU_FAM (17 ), + {} +}; +MODULE_DEVICE_TABLE (x86cpu, cpu_ids); +
drivers/hwmon/amd_energy.c line 241
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+static int __init amd_energy_init (void ) +{ +const struct x86_cpu_id *id; +int ret; + + id =x86_match_cpu (cpu_ids); +if (!id) { +pr_err ("driver does not support CPU family %d model %d\n" , + boot_cpu_data.x86, boot_cpu_data.x86_model); +
This is not an error and thus does not warrant an error message.
Quoted 33 lines
drivers/hwmon/amd_energy.c new file +32
-0+return -ENODEV ; + } + + ret =platform_driver_register (&amd_energy_driver); +if (ret <0 ) +return ret; + + amd_energy_platdev =platform_device_alloc (DRVNAME ,0 ); +if (!amd_energy_platdev) +return -ENOMEM ; + + ret =platform_device_add (amd_energy_platdev); +if (ret) { +platform_device_unregister (amd_energy_platdev); +return ret; + } + +return ret; +} + +static void __exit amd_energy_exit(void) +{ +platform_device_unregister (amd_energy_platdev); +platform_driver_unregister (&amd_energy_driver); +} + +module_init (amd_energy_init); +module_exit (amd_energy_exit); + +MODULE_DESCRIPTION ("Driver for AMD Energy reporting from RAPL MSR via HWMON interface" ); +MODULE_AUTHOR ("Naveen Krishna Chatradhi <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Hi,
Thank you for taking time to review and provide feedback. If you can kindly address a few questions, i would start addressing your comments and submit another version.
Quoted 75 lines · Guenter Roeck
Quoted 73 lines · Naveen Krishna Chatradhi
This patch adds hwmon based
amd_energydriver support for family 17h processors from AMD.The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Usestopology_max_packages()to get number of sockets.
3. Usesnum_present_cpus()to get the number of CPUs.
4. Reports the energy units viaenergy_unitsysfs entryCc Guenter Roeck <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.cdrivers/hwmon/Kconfig +10
-0@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86drivers/hwmon/Makefile +1
-0@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.odrivers/hwmon/amd_energy.c new file +13
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2019 Advanced Micro Devices, Inc. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/device.h> +#include <linux/sysfs.h> +#include <linux/cpu.h>
Quoted 8 lines
drivers/hwmon/amd_energy.c line 19
Quoted 6 lines
drivers/hwmon/amd_energy.c new file +6
-0+#include <linux/list.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/processor.h> +#include <linux/platform_device.h> +#include <linux/cpumask.h> Alphabetic include file order, please.
I missed it, will correct it.
Quoted 43 lines
Quoted 43 lines
drivers/hwmon/amd_energy.c new file +42
-0+ +#include <asm/cpu_device_id.h> + +#define DRVNAME "amd_energy" + +#define ENERGY_PWR_UNIT_MSR 0xC0010299 +#define ENERGY_CORE_MSR 0xC001029A +#define ENERGY_PCK_MSR 0xC001029B + +#define AMD_TIME_UNIT_MASK 0xF0000 +#define AMD_ENERGY_UNIT_MASK 0x01F00 +#define AMD_POWER_UNIT_MASK 0x0000F + +#define ENERGY_STATUS_MASK 0xffffffff + +#define AMD_FAM_17 0x17/* ZP, SSP */ + +/* Useful macros */ +#define AMD_CPU_FAM_ANY (_family, _model) \ +{ \ + .vendor = X86_VENDOR_AMD, \ + .family = _family, \ + .model = _model, \ + .feature = X86_FEATURE_ANY, \ +} + +#define AMD_CPU_FAM (_model) \ + AMD_CPU_FAM_ANY(AMD_FAM_##_model, X86_MODEL_ANY) + +static struct device_attribute units_attr; + +struct sensor_data { +unsigned int scale; +union { +unsigned int cpu_nr; +unsigned int sock_nr; + }; +struct device_attribute dev_attr_input; +char input[32 ]; +}; + +struct amd_energy_sensors {
Quoted 8 lines
drivers/hwmon/amd_energy.c line 65
Quoted 4 lines
drivers/hwmon/amd_energy.c new file +4
-0+struct sensor_data *data; +struct attribute **attrs; +struct attribute_group group; +const struct attribute_group *groups[1 ];Even if acceptable, this would be wrong. The list of groups ends with an empty entry, meaning this array would have to have at least two entries (one for the terminator).
Will correct it.
Quoted 27 lines
Quoted 27 lines
drivers/hwmon/amd_energy.c new file +26
-0+}; + +static ssize_t amd_units_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +uint64_t rapl_units; +uint64_t energy_unit; +int ret =0 ; + + ret =rdmsrl_safe (ENERGY_PWR_UNIT_MSR , &rapl_units); +if (ret) +return -EAGAIN ; + + energy_unit = (rapl_units &AMD_ENERGY_UNIT_MASK ) >>8 ; + +return snprintf (buffer,24 ,"%llu\n" , energy_unit); +} + +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input);
Quoted 8 lines
drivers/hwmon/amd_energy.c line 96
Quoted 5 lines
drivers/hwmon/amd_energy.c new file +5
-0+ ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
I was of the opinion that driver provides the raw values and user-space (can use float/double) will be able to scale the value, which would involve calculation of
"1/2^ESU * RAW value"
Quoted 55 lines
Quoted 55 lines
drivers/hwmon/amd_energy.c new file +54
-0+} + +static ssize_t amd_sock_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; +int cpu, cpu_nr; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + +for_each_possible_cpu (cpu) { +struct cpuinfo_x86 *c = &cpu_data (cpu); + +if (c->initialized && c->logical_die_id == sensor->sock_nr) { + cpu_nr = cpu; +break ; + } + cpu_nr =0 ; + } + + ret =rdmsrl_safe_on_cpu (cpu_nr,ENERGY_PCK_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value); +} + +static int amd_energy_probe (struct platform_device *pdev) +{ +struct amd_energy_sensors *amd_sensors; +struct device *hwdev, *dev = &pdev->dev; +int nr_cpus, nr_socks, idx =0 ; + + nr_cpus =num_present_cpus (); + nr_socks =topology_max_packages (); + + amd_sensors =devm_kzalloc (dev,sizeof (*amd_sensors),GFP_KERNEL ); +if (!amd_sensors) +return -ENOMEM ; + + amd_sensors->data =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->data),GFP_KERNEL ); +if (!amd_sensors->data) +return -ENOMEM ; + + amd_sensors->attrs =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->attrs),GFP_KERNEL ); +if (!amd_sensors->attrs) +return -ENOMEM ; + +/* populate attributes for number of cpus. */ + for (idx = 0; idx < ; idx++) {
Quoted 8 lines
drivers/hwmon/amd_energy.c line 155
Quoted 5 lines
drivers/hwmon/amd_energy.c new file +5
-0+struct sensor_data *sensor = &amd_sensors->data[idx]; + +snprintf (sensor->input,sizeof (sensor->input), +"core_energy%d_input" , idx); +This is unacceptable. Please use standard attributes (
energyX_input). If you want to report what the sensor is for, use labels.
Got it.
Quoted 43 lines
Quoted 19 lines
drivers/hwmon/amd_energy.c new file +18
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_core_u64_show; + + sensor->cpu_nr = idx; + amd_sensors->attrs[idx] = &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[idx]); + } + +/* populate attributes for number of sockets. */ +for (idx =0 ; idx < nr_socks; idx++) { +struct sensor_data *sensor = + &amd_sensors->data[nr_cpus + idx]; + +snprintf (sensor->input, +sizeof (sensor->input),"sock_energy%d_input" , idx); +drivers/hwmon/amd_energy.c line 183
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_sock_u64_show; + + sensor->sock_nr = idx; + amd_sensors->attrs[nr_cpus + idx] = + &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[nr_cpus + idx]); + }This all makes me wonder what is reported for inactive/disabled CPUs.
drivers/hwmon/amd_energy.c line 191
Quoted 8 lines
drivers/hwmon/amd_energy.c new file +8
-0+ + amd_sensors->group.attrs = amd_sensors->attrs; + amd_sensors->groups[0 ] = &amd_sensors->group; + +platform_set_drvdata (pdev, amd_sensors); + + hwdev =devm_hwmon_device_register_with_groups (dev, +"amd_energy" , amd_sensors, amd_sensors->groups);Please rework the driver to use the
devm_hwmon_device_register_with_info()API.
Quoted 14 lines
drivers/hwmon/amd_energy.c line 200
Quoted 9 lines
drivers/hwmon/amd_energy.c new file +9
-0+if (!hwdev) +return PTR_ERR_OR_ZERO (hwdev); + +/* populate attributes for energy units */ + units_attr.attr.name ="energy_units" ; + units_attr.attr.mode =0444 ; + units_attr.show = amd_units_u64_show; + +return sysfs_create_file (&hwdev->kobj, &units_attr.attr);This is irrelevant for this driver. Units are fixed. Besides, registering sysfs attributes after driver registration is racy and won't be accepted. Non-standard attributes can be added with the groups argument of
devm_hwmon_device_register_with_info()(not that I see any here).
I will try to change the driver around usingdevm_hwmon_device_register_with_info()
Quoted 36 lines
Quoted 36 lines
drivers/hwmon/amd_energy.c new file +35
-0+} + +static int amd_energy_remove (struct platform_device *pdev) +{ +sysfs_remove_file (&pdev->dev.kobj, &units_attr.attr); +return 0 ; +} + +static const struct platform_device_id amd_energy_ids[] = { + { .name =DRVNAME , }, + {} +}; +MODULE_DEVICE_TABLE (platform, amd_energy_ids); + +static struct platform_driver amd_energy_driver = { + .probe = amd_energy_probe, + .remove = amd_energy_remove, + .id_table = amd_energy_ids, + .driver = { + .name =DRVNAME , + }, +}; + +static struct platform_device *amd_energy_platdev; + +static const struct x86_cpu_id cpu_ids[]__initconst = { +AMD_CPU_FAM (17 ), + {} +}; +MODULE_DEVICE_TABLE (x86cpu, cpu_ids); + +static int __init amd_energy_init (void ) +{ +const struct x86_cpu_id *id; +int ret;
Quoted 8 lines
drivers/hwmon/amd_energy.c line 241
Quoted 6 lines
drivers/hwmon/amd_energy.c new file +6
-0+ + id =x86_match_cpu (cpu_ids); +if (!id) { +pr_err ("driver does not support CPU family %d model %d\n" , + boot_cpu_data.x86, boot_cpu_data.x86_model); +This is not an error and thus does not warrant an error message.
Ok
Quoted 35 lines
Quoted 34 lines
drivers/hwmon/amd_energy.c new file +32
-0+return -ENODEV ; + } + + ret =platform_driver_register (&amd_energy_driver); +if (ret <0 ) +return ret; + + amd_energy_platdev =platform_device_alloc (DRVNAME ,0 ); +if (!amd_energy_platdev) +return -ENOMEM ; + + ret =platform_device_add (amd_energy_platdev); +if (ret) { +platform_device_unregister (amd_energy_platdev); +return ret; + } + +return ret; +} + +static void __exit amd_energy_exit(void) +{ +platform_device_unregister (amd_energy_platdev); +platform_driver_unregister (&amd_energy_driver); +} + +module_init (amd_energy_init); +module_exit (amd_energy_exit); + +MODULE_DESCRIPTION ("Driver for AMD Energy reporting from RAPL MSR via HWMON interface" ); +MODULE_AUTHOR ("Naveen Krishna Chatradhi <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Signature · 2 lines
Shine bright, Naveen
Guenter Roeck
maintainerQuoted 6 lines · Naveen Krishna Ch
Hi,
Thank you for taking time to review and provide feedback. If you can kindly address a few questions, i would start addressing your comments and submit another version.
[ ... ]
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
drivers/hwmon/amd_energy.c new file +9
-0+ +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input);Quoted 8 lines
drivers/hwmon/amd_energy.c line 96
Quoted 5 lines
drivers/hwmon/amd_energy.c new file +5
-0+ ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 5 lines
I was of the opinion that driver provides the raw values and user-space (can use float/double) will be able to scale the value, which would involve calculation of
"1/2^ESU * RAW value"
Please see Documentation/hwmon/sysfs-interface.rst. Sure, there is /etc/sensors3.conf, but that only applies if the kernel driver can not provide scaled values. This is not the case here.
FWIW, I implemented prototype code for this some time ago as extension of the k10temp driver, and the arithmetic wasn't that difficult.
Guenter
Hi Guenter,
Quoted 34 lines · Guenter Roeck
Quoted 6 lines · Naveen Krishna Ch
Hi,
Thank you for taking time to review and provide feedback. If you can kindly address a few questions, i would start addressing your comments and submit another version.
[ ... ]
Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
drivers/hwmon/amd_energy.c new file +9
-0+ +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input);Quoted 8 lines
drivers/hwmon/amd_energy.c line 96
Quoted 5 lines
drivers/hwmon/amd_energy.c new file +5
-0+ ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 5 lines
I was of the opinion that driver provides the raw values and user-space (can use float/double) will be able to scale the value, which would involve calculation of
"1/2^ESU * RAW value"
Quoted 3 lines
Please see
Documentation/hwmon/sysfs-interface.rst. Sure, there is/etc/sensors3.conf, but that only applies if the kernel driver can not provide scaled values. This is not the case here.
I did not refer this page.
Quoted 4 lines
FWIW, I implemented prototype code for this some time ago as extension of the k10temp driver, and the arithmetic wasn't that difficult.
Will take clues and implement.
Quoted 2 lines
Guenter
Thank you.
Signature · 2 lines
Shine bright, (: Nav :)
Hi Guenter
I would be glad, If you could help me with the following query.
Quoted 264 lines · Guenter Roeck
Quoted 69 lines · Naveen Krishna Chatradhi
This patch adds hwmon based
amd_energydriver support for family 17h processors from AMD.The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Usestopology_max_packages()to get number of sockets.
3. Usesnum_present_cpus()to get the number of CPUs.
4. Reports the energy units viaenergy_unitsysfs entryCc Guenter Roeck <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.cdrivers/hwmon/Kconfig +10
-0@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86drivers/hwmon/Makefile +1
-0@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.odrivers/hwmon/amd_energy.c new file +9
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2019 Advanced Micro Devices, Inc. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> drivers/hwmon/amd_energy.c line 19
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+#include <linux/types.h> +#include <linux/device.h> +#include <linux/sysfs.h> +#include <linux/cpu.h> +#include <linux/list.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/processor.h> +#include <linux/platform_device.h> +#include <linux/cpumask.h> Alphabetic include file order, please.
Quoted 36 lines
drivers/hwmon/amd_energy.c new file +36
-0+ +#include <asm/cpu_device_id.h> + +#define DRVNAME "amd_energy" + +#define ENERGY_PWR_UNIT_MSR 0xC0010299 +#define ENERGY_CORE_MSR 0xC001029A +#define ENERGY_PCK_MSR 0xC001029B + +#define AMD_TIME_UNIT_MASK 0xF0000 +#define AMD_ENERGY_UNIT_MASK 0x01F00 +#define AMD_POWER_UNIT_MASK 0x0000F + +#define ENERGY_STATUS_MASK 0xffffffff + +#define AMD_FAM_17 0x17/* ZP, SSP */ + +/* Useful macros */ +#define AMD_CPU_FAM_ANY (_family, _model) \ +{ \ + .vendor = X86_VENDOR_AMD, \ + .family = _family, \ + .model = _model, \ + .feature = X86_FEATURE_ANY, \ +} + +#define AMD_CPU_FAM (_model) \ + AMD_CPU_FAM_ANY(AMD_FAM_##_model, X86_MODEL_ANY) + +static struct device_attribute units_attr; + +struct sensor_data { +unsigned int scale; +union { +unsigned int cpu_nr; +unsigned int sock_nr;drivers/hwmon/amd_energy.c line 65
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ }; +struct device_attribute dev_attr_input; +char input[32 ]; +}; + +struct amd_energy_sensors { +struct sensor_data *data; +struct attribute **attrs; +struct attribute_group group; +const struct attribute_group *groups[1 ];Even if acceptable, this would be wrong. The list of groups ends with an empty entry, meaning this array would have to have at least two entries (one for the terminator).
Quoted 21 lines
drivers/hwmon/amd_energy.c new file +21
-0+}; + +static ssize_t amd_units_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +uint64_t rapl_units; +uint64_t energy_unit; +int ret =0 ; + + ret =rdmsrl_safe (ENERGY_PWR_UNIT_MSR , &rapl_units); +if (ret) +return -EAGAIN ; + + energy_unit = (rapl_units &AMD_ENERGY_UNIT_MASK ) >>8 ; + +return snprintf (buffer,24 ,"%llu\n" , energy_unit); +} + +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{drivers/hwmon/amd_energy.c line 96
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 49 lines
drivers/hwmon/amd_energy.c new file +49
-0+} + +static ssize_t amd_sock_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; +int cpu, cpu_nr; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + +for_each_possible_cpu (cpu) { +struct cpuinfo_x86 *c = &cpu_data (cpu); + +if (c->initialized && c->logical_die_id == sensor->sock_nr) { + cpu_nr = cpu; +break ; + } + cpu_nr =0 ; + } + + ret =rdmsrl_safe_on_cpu (cpu_nr,ENERGY_PCK_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value); +} + +static int amd_energy_probe (struct platform_device *pdev) +{ +struct amd_energy_sensors *amd_sensors; +struct device *hwdev, *dev = &pdev->dev; +int nr_cpus, nr_socks, idx =0 ; + + nr_cpus =num_present_cpus (); + nr_socks =topology_max_packages (); + + amd_sensors =devm_kzalloc (dev,sizeof (*amd_sensors),GFP_KERNEL ); +if (!amd_sensors) +return -ENOMEM ; + + amd_sensors->data =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->data),GFP_KERNEL ); +if (!amd_sensors->data) +return -ENOMEM ; + + amd_sensors->attrs =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->attrs),GFP_KERNEL );drivers/hwmon/amd_energy.c line 155
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+if (!amd_sensors->attrs) +return -ENOMEM ; + +/* populate attributes for number of cpus. */ + for (idx = 0; idx < ; idx++) { +struct sensor_data *sensor = &amd_sensors->data[idx]; + +snprintf (sensor->input,sizeof (sensor->input), +"core_energy%d_input" , idx); +This is unacceptable. Please use standard attributes (
energyX_input). If you want to report what the sensor is for, use labels.Quoted 18 lines
drivers/hwmon/amd_energy.c new file +18
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_core_u64_show; + + sensor->cpu_nr = idx; + amd_sensors->attrs[idx] = &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[idx]); + } + +/* populate attributes for number of sockets. */ +for (idx =0 ; idx < nr_socks; idx++) { +struct sensor_data *sensor = + &amd_sensors->data[nr_cpus + idx]; + +snprintf (sensor->input, +sizeof (sensor->input),"sock_energy%d_input" , idx); +drivers/hwmon/amd_energy.c line 183
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_sock_u64_show; + + sensor->sock_nr = idx; + amd_sensors->attrs[nr_cpus + idx] = + &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[nr_cpus + idx]); + }This all makes me wonder what is reported for inactive/disabled CPUs.
Quoted 10 lines
drivers/hwmon/amd_energy.c line 191
Quoted 8 lines
drivers/hwmon/amd_energy.c new file +8
-0+ + amd_sensors->group.attrs = amd_sensors->attrs; + amd_sensors->groups[0 ] = &amd_sensors->group; + +platform_set_drvdata (pdev, amd_sensors); + + hwdev =devm_hwmon_device_register_with_groups (dev, +"amd_energy" , amd_sensors, amd_sensors->groups);Please rework the driver to use the
devm_hwmon_device_register_with_info()API.
Our current, platform has 64 cores per socket and 2 such sockets on a board. There is a core energy counter register for each core and Package counter for each socket. The topology varies from platform to platform.
To keep this driver reusable for all platforms. I think, we need to define the hwmon_chip_info and channel_info structures dynamically (hwmon_chip_info has const variables). Is there a way to define a pre-processor statement which can create an array for a given platform.
Could you suggest me a way to defining these hwmon_chip_info and channel array dynamically ? This reason a reason for me to use groups instead of the chip_info.
Quoted 94 lines
drivers/hwmon/amd_energy.c line 200
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +9
-0+if (!hwdev) +return PTR_ERR_OR_ZERO (hwdev); + +/* populate attributes for energy units */ + units_attr.attr.name ="energy_units" ; + units_attr.attr.mode =0444 ; + units_attr.show = amd_units_u64_show; + +return sysfs_create_file (&hwdev->kobj, &units_attr.attr);This is irrelevant for this driver. Units are fixed. Besides, registering sysfs attributes after driver registration is racy and won't be accepted. Non-standard attributes can be added with the groups argument of
devm_hwmon_device_register_with_info()(not that I see any here).Quoted 31 lines
drivers/hwmon/amd_energy.c new file +31
-0+} + +static int amd_energy_remove (struct platform_device *pdev) +{ +sysfs_remove_file (&pdev->dev.kobj, &units_attr.attr); +return 0 ; +} + +static const struct platform_device_id amd_energy_ids[] = { + { .name =DRVNAME , }, + {} +}; +MODULE_DEVICE_TABLE (platform, amd_energy_ids); + +static struct platform_driver amd_energy_driver = { + .probe = amd_energy_probe, + .remove = amd_energy_remove, + .id_table = amd_energy_ids, + .driver = { + .name =DRVNAME , + }, +}; + +static struct platform_device *amd_energy_platdev; + +static const struct x86_cpu_id cpu_ids[]__initconst = { +AMD_CPU_FAM (17 ), + {} +}; +MODULE_DEVICE_TABLE (x86cpu, cpu_ids); +drivers/hwmon/amd_energy.c line 241
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+static int __init amd_energy_init (void ) +{ +const struct x86_cpu_id *id; +int ret; + + id =x86_match_cpu (cpu_ids); +if (!id) { +pr_err ("driver does not support CPU family %d model %d\n" , + boot_cpu_data.x86, boot_cpu_data.x86_model); +This is not an error and thus does not warrant an error message.
Quoted 33 lines
drivers/hwmon/amd_energy.c new file +32
-0+return -ENODEV ; + } + + ret =platform_driver_register (&amd_energy_driver); +if (ret <0 ) +return ret; + + amd_energy_platdev =platform_device_alloc (DRVNAME ,0 ); +if (!amd_energy_platdev) +return -ENOMEM ; + + ret =platform_device_add (amd_energy_platdev); +if (ret) { +platform_device_unregister (amd_energy_platdev); +return ret; + } + +return ret; +} + +static void __exit amd_energy_exit(void) +{ +platform_device_unregister (amd_energy_platdev); +platform_driver_unregister (&amd_energy_driver); +} + +module_init (amd_energy_init); +module_exit (amd_energy_exit); + +MODULE_DESCRIPTION ("Driver for AMD Energy reporting from RAPL MSR via HWMON interface" ); +MODULE_AUTHOR ("Naveen Krishna Chatradhi <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Signature · 2 lines
Shine bright, (: Nav :)
Guenter Roeck
maintainerQuoted 290 lines · Naveen Krishna Ch
Hi Guenter
I would be glad, If you could help me with the following query.
Quoted 264 lines · Guenter Roeck
Quoted 69 lines · Naveen Krishna Chatradhi
This patch adds hwmon based
amd_energydriver support for family 17h processors from AMD.The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Usestopology_max_packages()to get number of sockets.
3. Usesnum_present_cpus()to get the number of CPUs.
4. Reports the energy units viaenergy_unitsysfs entryCc Guenter Roeck <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.cdrivers/hwmon/Kconfig +10
-0@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86drivers/hwmon/Makefile +1
-0@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.odrivers/hwmon/amd_energy.c new file +9
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2019 Advanced Micro Devices, Inc. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> drivers/hwmon/amd_energy.c line 19
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+#include <linux/types.h> +#include <linux/device.h> +#include <linux/sysfs.h> +#include <linux/cpu.h> +#include <linux/list.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/processor.h> +#include <linux/platform_device.h> +#include <linux/cpumask.h> Alphabetic include file order, please.
Quoted 36 lines
drivers/hwmon/amd_energy.c new file +36
-0+ +#include <asm/cpu_device_id.h> + +#define DRVNAME "amd_energy" + +#define ENERGY_PWR_UNIT_MSR 0xC0010299 +#define ENERGY_CORE_MSR 0xC001029A +#define ENERGY_PCK_MSR 0xC001029B + +#define AMD_TIME_UNIT_MASK 0xF0000 +#define AMD_ENERGY_UNIT_MASK 0x01F00 +#define AMD_POWER_UNIT_MASK 0x0000F + +#define ENERGY_STATUS_MASK 0xffffffff + +#define AMD_FAM_17 0x17/* ZP, SSP */ + +/* Useful macros */ +#define AMD_CPU_FAM_ANY (_family, _model) \ +{ \ + .vendor = X86_VENDOR_AMD, \ + .family = _family, \ + .model = _model, \ + .feature = X86_FEATURE_ANY, \ +} + +#define AMD_CPU_FAM (_model) \ + AMD_CPU_FAM_ANY(AMD_FAM_##_model, X86_MODEL_ANY) + +static struct device_attribute units_attr; + +struct sensor_data { +unsigned int scale; +union { +unsigned int cpu_nr; +unsigned int sock_nr;drivers/hwmon/amd_energy.c line 65
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ }; +struct device_attribute dev_attr_input; +char input[32 ]; +}; + +struct amd_energy_sensors { +struct sensor_data *data; +struct attribute **attrs; +struct attribute_group group; +const struct attribute_group *groups[1 ];Even if acceptable, this would be wrong. The list of groups ends with an empty entry, meaning this array would have to have at least two entries (one for the terminator).
Quoted 21 lines
drivers/hwmon/amd_energy.c new file +21
-0+}; + +static ssize_t amd_units_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +uint64_t rapl_units; +uint64_t energy_unit; +int ret =0 ; + + ret =rdmsrl_safe (ENERGY_PWR_UNIT_MSR , &rapl_units); +if (ret) +return -EAGAIN ; + + energy_unit = (rapl_units &AMD_ENERGY_UNIT_MASK ) >>8 ; + +return snprintf (buffer,24 ,"%llu\n" , energy_unit); +} + +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{drivers/hwmon/amd_energy.c line 96
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 49 lines
drivers/hwmon/amd_energy.c new file +49
-0+} + +static ssize_t amd_sock_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; +int cpu, cpu_nr; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + +for_each_possible_cpu (cpu) { +struct cpuinfo_x86 *c = &cpu_data (cpu); + +if (c->initialized && c->logical_die_id == sensor->sock_nr) { + cpu_nr = cpu; +break ; + } + cpu_nr =0 ; + } + + ret =rdmsrl_safe_on_cpu (cpu_nr,ENERGY_PCK_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value); +} + +static int amd_energy_probe (struct platform_device *pdev) +{ +struct amd_energy_sensors *amd_sensors; +struct device *hwdev, *dev = &pdev->dev; +int nr_cpus, nr_socks, idx =0 ; + + nr_cpus =num_present_cpus (); + nr_socks =topology_max_packages (); + + amd_sensors =devm_kzalloc (dev,sizeof (*amd_sensors),GFP_KERNEL ); +if (!amd_sensors) +return -ENOMEM ; + + amd_sensors->data =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->data),GFP_KERNEL ); +if (!amd_sensors->data) +return -ENOMEM ; + + amd_sensors->attrs =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->attrs),GFP_KERNEL );drivers/hwmon/amd_energy.c line 155
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+if (!amd_sensors->attrs) +return -ENOMEM ; + +/* populate attributes for number of cpus. */ + for (idx = 0; idx < ; idx++) { +struct sensor_data *sensor = &amd_sensors->data[idx]; + +snprintf (sensor->input,sizeof (sensor->input), +"core_energy%d_input" , idx); +This is unacceptable. Please use standard attributes (
energyX_input). If you want to report what the sensor is for, use labels.Quoted 18 lines
drivers/hwmon/amd_energy.c new file +18
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_core_u64_show; + + sensor->cpu_nr = idx; + amd_sensors->attrs[idx] = &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[idx]); + } + +/* populate attributes for number of sockets. */ +for (idx =0 ; idx < nr_socks; idx++) { +struct sensor_data *sensor = + &amd_sensors->data[nr_cpus + idx]; + +snprintf (sensor->input, +sizeof (sensor->input),"sock_energy%d_input" , idx); +drivers/hwmon/amd_energy.c line 183
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_sock_u64_show; + + sensor->sock_nr = idx; + amd_sensors->attrs[nr_cpus + idx] = + &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[nr_cpus + idx]); + }This all makes me wonder what is reported for inactive/disabled CPUs.
Quoted 10 lines
drivers/hwmon/amd_energy.c line 191
Quoted 8 lines
drivers/hwmon/amd_energy.c new file +8
-0+ + amd_sensors->group.attrs = amd_sensors->attrs; + amd_sensors->groups[0 ] = &amd_sensors->group; + +platform_set_drvdata (pdev, amd_sensors); + + hwdev =devm_hwmon_device_register_with_groups (dev, +"amd_energy" , amd_sensors, amd_sensors->groups);Please rework the driver to use the
devm_hwmon_device_register_with_info()API.Our current, platform has 64 cores per socket and 2 such sockets on a board. There is a core energy counter register for each core and Package counter for each socket. The topology varies from platform to platform.
To keep this driver reusable for all platforms. I think, we need to define the
hwmon_chip_infoandchannel_infostructures dynamically (hwmon_chip_infohas const variables). Is there a way to define a pre-processor statement which can create an array for a given platform.
Quoted 3 lines
Could you suggest me a way to defining these
hwmon_chip_infoand channel array dynamically ? This reason a reason for me to use groups instead of thechip_info.
The approach used by the tmp421 driver should work. 'const' doesn't apply to the pointers, but to the data they contain. But that doesn't mean the data itself has to be const. You can still assign the pointers dynamically. The habanalabs driver does it fully dynamically (I did not review that code, so it may be a bit messy).
Guenter
Hi Guenter
Quoted 296 lines · Guenter Roeck
Quoted 290 lines · Naveen Krishna Ch
Hi Guenter
I would be glad, If you could help me with the following query.
Quoted 264 lines · Guenter Roeck
Quoted 69 lines · Naveen Krishna Chatradhi
This patch adds hwmon based
amd_energydriver support for family 17h processors from AMD.The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Usestopology_max_packages()to get number of sockets.
3. Usesnum_present_cpus()to get the number of CPUs.
4. Reports the energy units viaenergy_unitsysfs entryCc Guenter Roeck <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.cdrivers/hwmon/Kconfig +10
-0@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86drivers/hwmon/Makefile +1
-0@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.odrivers/hwmon/amd_energy.c new file +9
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2019 Advanced Micro Devices, Inc. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> drivers/hwmon/amd_energy.c line 19
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+#include <linux/types.h> +#include <linux/device.h> +#include <linux/sysfs.h> +#include <linux/cpu.h> +#include <linux/list.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/processor.h> +#include <linux/platform_device.h> +#include <linux/cpumask.h> Alphabetic include file order, please.
Quoted 36 lines
drivers/hwmon/amd_energy.c new file +36
-0+ +#include <asm/cpu_device_id.h> + +#define DRVNAME "amd_energy" + +#define ENERGY_PWR_UNIT_MSR 0xC0010299 +#define ENERGY_CORE_MSR 0xC001029A +#define ENERGY_PCK_MSR 0xC001029B + +#define AMD_TIME_UNIT_MASK 0xF0000 +#define AMD_ENERGY_UNIT_MASK 0x01F00 +#define AMD_POWER_UNIT_MASK 0x0000F + +#define ENERGY_STATUS_MASK 0xffffffff + +#define AMD_FAM_17 0x17/* ZP, SSP */ + +/* Useful macros */ +#define AMD_CPU_FAM_ANY (_family, _model) \ +{ \ + .vendor = X86_VENDOR_AMD, \ + .family = _family, \ + .model = _model, \ + .feature = X86_FEATURE_ANY, \ +} + +#define AMD_CPU_FAM (_model) \ + AMD_CPU_FAM_ANY(AMD_FAM_##_model, X86_MODEL_ANY) + +static struct device_attribute units_attr; + +struct sensor_data { +unsigned int scale; +union { +unsigned int cpu_nr; +unsigned int sock_nr;drivers/hwmon/amd_energy.c line 65
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ }; +struct device_attribute dev_attr_input; +char input[32 ]; +}; + +struct amd_energy_sensors { +struct sensor_data *data; +struct attribute **attrs; +struct attribute_group group; +const struct attribute_group *groups[1 ];Even if acceptable, this would be wrong. The list of groups ends with an empty entry, meaning this array would have to have at least two entries (one for the terminator).
Quoted 21 lines
drivers/hwmon/amd_energy.c new file +21
-0+}; + +static ssize_t amd_units_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +uint64_t rapl_units; +uint64_t energy_unit; +int ret =0 ; + + ret =rdmsrl_safe (ENERGY_PWR_UNIT_MSR , &rapl_units); +if (ret) +return -EAGAIN ; + + energy_unit = (rapl_units &AMD_ENERGY_UNIT_MASK ) >>8 ; + +return snprintf (buffer,24 ,"%llu\n" , energy_unit); +} + +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{drivers/hwmon/amd_energy.c line 96
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 49 lines
drivers/hwmon/amd_energy.c new file +49
-0+} + +static ssize_t amd_sock_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; +int cpu, cpu_nr; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + +for_each_possible_cpu (cpu) { +struct cpuinfo_x86 *c = &cpu_data (cpu); + +if (c->initialized && c->logical_die_id == sensor->sock_nr) { + cpu_nr = cpu; +break ; + } + cpu_nr =0 ; + } + + ret =rdmsrl_safe_on_cpu (cpu_nr,ENERGY_PCK_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value); +} + +static int amd_energy_probe (struct platform_device *pdev) +{ +struct amd_energy_sensors *amd_sensors; +struct device *hwdev, *dev = &pdev->dev; +int nr_cpus, nr_socks, idx =0 ; + + nr_cpus =num_present_cpus (); + nr_socks =topology_max_packages (); + + amd_sensors =devm_kzalloc (dev,sizeof (*amd_sensors),GFP_KERNEL ); +if (!amd_sensors) +return -ENOMEM ; + + amd_sensors->data =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->data),GFP_KERNEL ); +if (!amd_sensors->data) +return -ENOMEM ; + + amd_sensors->attrs =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->attrs),GFP_KERNEL );drivers/hwmon/amd_energy.c line 155
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+if (!amd_sensors->attrs) +return -ENOMEM ; + +/* populate attributes for number of cpus. */ + for (idx = 0; idx < ; idx++) { +struct sensor_data *sensor = &amd_sensors->data[idx]; + +snprintf (sensor->input,sizeof (sensor->input), +"core_energy%d_input" , idx); +This is unacceptable. Please use standard attributes (
energyX_input). If you want to report what the sensor is for, use labels.Quoted 18 lines
drivers/hwmon/amd_energy.c new file +18
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_core_u64_show; + + sensor->cpu_nr = idx; + amd_sensors->attrs[idx] = &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[idx]); + } + +/* populate attributes for number of sockets. */ +for (idx =0 ; idx < nr_socks; idx++) { +struct sensor_data *sensor = + &amd_sensors->data[nr_cpus + idx]; + +snprintf (sensor->input, +sizeof (sensor->input),"sock_energy%d_input" , idx); +drivers/hwmon/amd_energy.c line 183
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_sock_u64_show; + + sensor->sock_nr = idx; + amd_sensors->attrs[nr_cpus + idx] = + &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[nr_cpus + idx]); + }This all makes me wonder what is reported for inactive/disabled CPUs.
Quoted 10 lines
drivers/hwmon/amd_energy.c line 191
Quoted 8 lines
drivers/hwmon/amd_energy.c new file +8
-0+ + amd_sensors->group.attrs = amd_sensors->attrs; + amd_sensors->groups[0 ] = &amd_sensors->group; + +platform_set_drvdata (pdev, amd_sensors); + + hwdev =devm_hwmon_device_register_with_groups (dev, +"amd_energy" , amd_sensors, amd_sensors->groups);Please rework the driver to use the
devm_hwmon_device_register_with_info()API.Our current, platform has 64 cores per socket and 2 such sockets on a board. There is a core energy counter register for each core and Package counter for each socket. The topology varies from platform to platform.
To keep this driver reusable for all platforms. I think, we need to define the
hwmon_chip_infoandchannel_infostructures dynamically (hwmon_chip_infohas const variables). Is there a way to define a pre-processor statement which can create an array for a given platform.Quoted 3 lines
Could you suggest me a way to defining these
hwmon_chip_infoand channel array dynamically ? This reason a reason for me to use groups instead of thechip_info.
Quoted 6 lines
The approach used by the tmp421 driver should work. 'const' doesn't apply to the pointers, but to the data they contain. But that doesn't mean the data itself has to be const. You can still assign the pointers dynamically. The habanalabs driver does it fully dynamically (I did not review that code, so it may be a bit messy).
Thank you very much for the pointer. I will go through the code.
Quoted 2 lines
Guenter
Signature · 2 lines
Shine bright, (: Nav :)
Hi Guenter
Quoted 300 lines · Naveen Krishna Ch
Hi Guenter
Quoted 296 lines · Guenter Roeck
Quoted 293 lines · Naveen Krishna Ch
Hi Guenter
I would be glad, If you could help me with the following query.
Quoted 264 lines · Guenter Roeck
Quoted 69 lines · Naveen Krishna Chatradhi
This patch adds hwmon based
amd_energydriver support for family 17h processors from AMD.The driver provides following interface to the userspace
1. Reports the per core and per socket energy consumption via sysfs entries created per core and per socket respectively.
* per core energy consumed via "core_energy%d_input"
* package/socket energy consumed via "sock_energy%d_input".
2. Usestopology_max_packages()to get number of sockets.
3. Usesnum_present_cpus()to get the number of CPUs.
4. Reports the energy units viaenergy_unitsysfs entryCc Guenter Roeck <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/amd_energy.c | 273 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 drivers/hwmon/amd_energy.cdrivers/hwmon/Kconfig +10
-0@@ -324,6 +324,16 @@ config SENSORS_FAM15H_POWER This driver can also be built as a module. If so, the module will be called fam15h_power. +config SENSORS_AMD_ENERGY + tristate "AMD RAPL MSR based Energy driver" + depends on X86 + help + If you say yes here you get support for core and package energy + sensors, based on RAPL MSR for AMD family 17h and above CPUs. + + This driver can also be built as a module. If so, the module + will be called as amd_energy. + config SENSORS_APPLESMC tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)" depends on INPUT && X86drivers/hwmon/Makefile +1
-0@@ -45,6 +45,7 @@ obj-$(CONFIG_SENSORS_ADT7411) += adt7411.o obj-$(CONFIG_SENSORS_ADT7462) += adt7462.o obj-$(CONFIG_SENSORS_ADT7470) += adt7470.o obj-$(CONFIG_SENSORS_ADT7475) += adt7475.o +obj-$(CONFIG_SENSORS_AMD_ENERGY) += amd_energy.o obj-$(CONFIG_SENSORS_APPLESMC) += applesmc.o obj-$(CONFIG_SENSORS_ARM_SCMI) += scmi-hwmon.o obj-$(CONFIG_SENSORS_ARM_SCPI) += scpi-hwmon.odrivers/hwmon/amd_energy.c new file +9
-0@@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (C) 2019 Advanced Micro Devices, Inc. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/slab.h> drivers/hwmon/amd_energy.c line 19
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+#include <linux/types.h> +#include <linux/device.h> +#include <linux/sysfs.h> +#include <linux/cpu.h> +#include <linux/list.h> +#include <linux/hwmon.h> +#include <linux/hwmon-sysfs.h> +#include <linux/processor.h> +#include <linux/platform_device.h> +#include <linux/cpumask.h> Alphabetic include file order, please.
Quoted 36 lines
drivers/hwmon/amd_energy.c new file +36
-0+ +#include <asm/cpu_device_id.h> + +#define DRVNAME "amd_energy" + +#define ENERGY_PWR_UNIT_MSR 0xC0010299 +#define ENERGY_CORE_MSR 0xC001029A +#define ENERGY_PCK_MSR 0xC001029B + +#define AMD_TIME_UNIT_MASK 0xF0000 +#define AMD_ENERGY_UNIT_MASK 0x01F00 +#define AMD_POWER_UNIT_MASK 0x0000F + +#define ENERGY_STATUS_MASK 0xffffffff + +#define AMD_FAM_17 0x17/* ZP, SSP */ + +/* Useful macros */ +#define AMD_CPU_FAM_ANY (_family, _model) \ +{ \ + .vendor = X86_VENDOR_AMD, \ + .family = _family, \ + .model = _model, \ + .feature = X86_FEATURE_ANY, \ +} + +#define AMD_CPU_FAM (_model) \ + AMD_CPU_FAM_ANY(AMD_FAM_##_model, X86_MODEL_ANY) + +static struct device_attribute units_attr; + +struct sensor_data { +unsigned int scale; +union { +unsigned int cpu_nr; +unsigned int sock_nr;drivers/hwmon/amd_energy.c line 65
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ }; +struct device_attribute dev_attr_input; +char input[32 ]; +}; + +struct amd_energy_sensors { +struct sensor_data *data; +struct attribute **attrs; +struct attribute_group group; +const struct attribute_group *groups[1 ];Even if acceptable, this would be wrong. The list of groups ends with an empty entry, meaning this array would have to have at least two entries (one for the terminator).
Quoted 21 lines
drivers/hwmon/amd_energy.c new file +21
-0+}; + +static ssize_t amd_units_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +uint64_t rapl_units; +uint64_t energy_unit; +int ret =0 ; + + ret =rdmsrl_safe (ENERGY_PWR_UNIT_MSR , &rapl_units); +if (ret) +return -EAGAIN ; + + energy_unit = (rapl_units &AMD_ENERGY_UNIT_MASK ) >>8 ; + +return snprintf (buffer,24 ,"%llu\n" , energy_unit); +} + +static ssize_t amd_core_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{drivers/hwmon/amd_energy.c line 96
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + ret =rdmsrl_safe_on_cpu (sensor->cpu_nr,ENERGY_CORE_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value);It seems to me this reports raw values. This is unacceptable. Values need to be scaled to match the ABI. Energy is reported in microJoule.
Quoted 49 lines
drivers/hwmon/amd_energy.c new file +49
-0+} + +static ssize_t amd_sock_u64_show (struct device *dev, +struct device_attribute *dev_attr,char *buffer) +{ +unsigned long long value; +struct sensor_data *sensor; +int ret =0 ; +int cpu, cpu_nr; + + sensor = container_of(dev_attr, struct sensor_data, dev_attr_input); + +for_each_possible_cpu (cpu) { +struct cpuinfo_x86 *c = &cpu_data (cpu); + +if (c->initialized && c->logical_die_id == sensor->sock_nr) { + cpu_nr = cpu; +break ; + } + cpu_nr =0 ; + } + + ret =rdmsrl_safe_on_cpu (cpu_nr,ENERGY_PCK_MSR , &value); +if (ret) +return -EAGAIN ; + +return snprintf (buffer,24 ,"%llu\n" , value); +} + +static int amd_energy_probe (struct platform_device *pdev) +{ +struct amd_energy_sensors *amd_sensors; +struct device *hwdev, *dev = &pdev->dev; +int nr_cpus, nr_socks, idx =0 ; + + nr_cpus =num_present_cpus (); + nr_socks =topology_max_packages (); + + amd_sensors =devm_kzalloc (dev,sizeof (*amd_sensors),GFP_KERNEL ); +if (!amd_sensors) +return -ENOMEM ; + + amd_sensors->data =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->data),GFP_KERNEL ); +if (!amd_sensors->data) +return -ENOMEM ; + + amd_sensors->attrs =devm_kcalloc (dev, nr_cpus + nr_socks, +sizeof (*amd_sensors->attrs),GFP_KERNEL );drivers/hwmon/amd_energy.c line 155
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+if (!amd_sensors->attrs) +return -ENOMEM ; + +/* populate attributes for number of cpus. */ + for (idx = 0; idx < ; idx++) { +struct sensor_data *sensor = &amd_sensors->data[idx]; + +snprintf (sensor->input,sizeof (sensor->input), +"core_energy%d_input" , idx); +This is unacceptable. Please use standard attributes (
energyX_input). If you want to report what the sensor is for, use labels.Quoted 18 lines
drivers/hwmon/amd_energy.c new file +18
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_core_u64_show; + + sensor->cpu_nr = idx; + amd_sensors->attrs[idx] = &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[idx]); + } + +/* populate attributes for number of sockets. */ +for (idx =0 ; idx < nr_socks; idx++) { +struct sensor_data *sensor = + &amd_sensors->data[nr_cpus + idx]; + +snprintf (sensor->input, +sizeof (sensor->input),"sock_energy%d_input" , idx); +drivers/hwmon/amd_energy.c line 183
Quoted 10 lines
drivers/hwmon/amd_energy.c new file +10
-0+ sensor->dev_attr_input.attr.mode =0444 ; + sensor->dev_attr_input.attr.name = sensor->input; + sensor->dev_attr_input.show = amd_sock_u64_show; + + sensor->sock_nr = idx; + amd_sensors->attrs[nr_cpus + idx] = + &sensor->dev_attr_input.attr; + +sysfs_attr_init (amd_sensors->attrs[nr_cpus + idx]); + }This all makes me wonder what is reported for inactive/disabled CPUs.
Quoted 10 lines
drivers/hwmon/amd_energy.c line 191
Quoted 8 lines
drivers/hwmon/amd_energy.c new file +8
-0+ + amd_sensors->group.attrs = amd_sensors->attrs; + amd_sensors->groups[0 ] = &amd_sensors->group; + +platform_set_drvdata (pdev, amd_sensors); + + hwdev =devm_hwmon_device_register_with_groups (dev, +"amd_energy" , amd_sensors, amd_sensors->groups);Please rework the driver to use the
devm_hwmon_device_register_with_info()API.Our current, platform has 64 cores per socket and 2 such sockets on a board. There is a core energy counter register for each core and Package counter for each socket. The topology varies from platform to platform.
To keep this driver reusable for all platforms. I think, we need to define the
hwmon_chip_infoandchannel_infostructures dynamically (hwmon_chip_infohas const variables). Is there a way to define a pre-processor statement which can create an array for a given platform.Could you suggest me a way to defining these
hwmon_chip_infoand channel array dynamically ? This reason a reason for me to use groups instead of thechip_info.
Quoted 7 lines
Quoted 6 lines
The approach used by the tmp421 driver should work. 'const' doesn't apply to the pointers, but to the data they contain. But that doesn't mean the data itself has to be const. You can still assign the pointers dynamically. The habanalabs driver does it fully dynamically (I did not review that code, so it may be a bit messy).
Thank you very much for the pointer. I will go through the code.
I've the few more questions
Constraint:
The platform has 2 sets of energy sensors, one at core level, one at socket level.
If i populate the chip_info type as "hwmon_energy" for both sensors. The naming of the sysfs entries are going to be continuous and the user application
should read the label to identify which entry belongs to which sensor set.
Possible solutions :
I could think of following ways to avoid this
1. Create 2 different hwmon devices
2. Use "hwmon_in" as type for one of the sensor sets and "hwmon_energy" for other.
3. Use "groups" for one of the sensor sets and populate the other via chip. What do you suggest ?
Also, I noticed that the sysfs filename index for the hwmon_energy type is starting with 1,
while hwmon_in starts with 0. Was this a design choice ?
Ex: energy1_input
Your suggestions would be helpful.
Quoted 8 lines
Quoted 2 lines
Guenter
--
Shine bright,
(: Nav :)
--
Shine bright,
(: Nav :)
Guenter Roeck
maintainerQuoted 2 lines · Naveen Krishna Ch
Hi Guenter
[ ... ]
Quoted 9 lines
Constraint:
The platform has 2 sets of energy sensors, one at core level, one at socket level.
If i populate thechip_infotype as "hwmon_energy" for both sensors. The naming of the sysfs entries are going to be continuous and the user application
should read the label to identify which entry belongs to which sensor set.
I am a bit at loss. what is the problem with having multiple _energy attributes (energy1_input, energy2_input, ..., energy100_input) ?
Quoted 3 lines
Possible solutions :
I could think of following ways to avoid this
1. Create 2 different hwmon devices
You could do that, following the approach used by the k10temp driver. This is really your call. The question in that case, though, would be why you would want have a separate driver to start with and not just enhance the k10temp driver to also report energy (and power consumption, though I understand that the registers reporting it are not published).
Quoted 2 lines
2. Use "
hwmon_in" as type for one of the sensor sets and "hwmon_energy" for other.
_in is for voltages. Please don't tell me you plan to report voltages with this driver. If so, please extend the k10temp driver instead.
Quoted 1 line
3. Use "groups" for one of the sensor sets and populate the other via chip.
This is a no-go.
Quoted 5 lines
What do you suggest ?
Also, I noticed that the sysfs filename index for the
hwmon_energytype is starting with 1,
whilehwmon_instarts with 0. Was this a design choice ?
I think this was mostly historic, but it preceeds my involvement with the hwmon subsystem, so I don't really know.
Guenter
Hi Guenter,
Quoted 38 lines · Guenter Roeck
Quoted 2 lines · Naveen Krishna Ch
Hi Guenter
[ ... ]
Quoted 9 lines
Constraint:
The platform has 2 sets of energy sensors, one at core level, one at socket level.
If i populate thechip_infotype as "hwmon_energy" for both sensors. The naming of the sysfs entries are going to be continuous and the user application
should read the label to identify which entry belongs to which sensor set.I am a bit at loss. what is the problem with having multiple
_energyattributes (energy1_input,energy2_input, ...,energy100_input) ?Quoted 3 lines
Possible solutions :
I could think of following ways to avoid this
1. Create 2 different hwmon devicesYou could do that, following the approach used by the k10temp driver. This is really your call. The question in that case, though, would be why you would want have a separate driver to start with and not just enhance the k10temp driver to also report energy (and power consumption, though I understand that the registers reporting it are not published).
Quoted 2 lines
2. Use "
hwmon_in" as type for one of the sensor sets and "hwmon_energy" for other.
_inis for voltages. Please don't tell me you plan to report voltages with this driver. If so, please extend the k10temp driver instead.Quoted 1 line
3. Use "groups" for one of the sensor sets and populate the other via chip.
This is a no-go.
Quoted 8 lines
Quoted 5 lines
What do you suggest ?
Also, I noticed that the sysfs filename index for the
hwmon_energytype is starting with 1,
whilehwmon_instarts with 0. Was this a design choice ?I think this was mostly historic, but it preceeds my involvement with the hwmon subsystem, so I don't really know.
Thanks for your inputs, i've submitted V2 version of the patch with your comments addressed. Could you please review them and let us know your opinion.
https://lore.kernel.org/linux-hwmon/[email protected]/T/#t
Quoted 2 lines
Guenter
Signature · 2 lines
Shine bright, (: Nav :)