sbrmi: Use regmap subsystem
PATCH 1/2 sbrmi: Use regmap subsystem
From: Naveen Krishna Chatradhi <[email protected]>
- regmap subsystem provides multiple benefits over direct smbus APIs
- The susbsytem can be helpful in following cases
- Differnet types of bus (i2c/i3c)
- Different Register address size (1byte/2byte)
--- drivers/hwmon/Kconfig | 1 + drivers/hwmon/sbrmi.c | 61 ++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 27 deletions(-)
drivers/hwmon/Kconfig +1 -0
@@ -1759,6 +1759,7 @@ config SENSORS_SBTSI config SENSORS_SBRMI tristate "Emulated SB-RMI sensor" depends on I2C + select REGMAP_I2C help If you say yes here you get support for emulated RMI sensors on AMD SoCs with APML interface connected to a BMC device.
drivers/hwmon/sbrmi.c +34 -27
162 lines not on this page · raw message
--
2.25.1
PATCH 2/2 sbrmi: Add support for APML protocols
From: Naveen Krishna Chatradhi <[email protected]>
The present sbrmi module only support reporting power. However, AMD data center processors support various system management functionality Out-of-band over Advanced Platform Management Link APML.
Register a miscdevice, which creates a device /dev/sbrmiX with an IOCTL interface for the user space to invoke the following protocols.
- Mailbox read/write (already defined in sbrmi_mailbox_xfer())
- CPUID read
- MCAMSR read
While at it, move caching of Max power limit from probe to hwmon_power_cap_max
- This will avoid sbrmi loading errors in the BMC, when the processors are down.
--- drivers/hwmon/Makefile | 3 +- drivers/hwmon/sbrmi-common.c | 392 +++++++++++++++++++++++++++++++++ drivers/hwmon/sbrmi-common.h | 37 ++++ drivers/hwmon/sbrmi.c | 398 +++++++++++++++++----------------- include/uapi/linux/amd-apml.h | 74 +++++++ 5 files changed, 709 insertions(+), 195 deletions(-) create mode 100644 drivers/hwmon/sbrmi-common.c create mode 100644 drivers/hwmon/sbrmi-common.h create mode 100644 include/uapi/linux/amd-apml.h
drivers/hwmon/Makefile +2 -1
@@ -183,7 +183,8 @@ obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o obj-$(CONFIG_SENSORS_PWM_FAN) += pwm-fan.o obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON) += raspberrypi-hwmon.o obj-$(CONFIG_SENSORS_SBTSI) += sbtsi_temp.o -obj-$(CONFIG_SENSORS_SBRMI) += sbrmi.o +apml_sbrmi-objs = sbrmi.o sbrmi-common.o +obj-$(CONFIG_SENSORS_SBRMI) += apml_sbrmi.o obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o
drivers/hwmon/sbrmi-common.c new file +392 -0
392 lines not on this page · raw message
drivers/hwmon/sbrmi-common.h new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2021-2024 Advanced Micro Devices, Inc. +*/ + +#ifndef _AMD_APML_SBRMI_H_ +#define _AMD_APML_SBRMI_H_ + +#include <linux/miscdevice.h> +#include <uapi/linux/amd-apml.h> + +/* Each client has this additional data */ +/* in_progress: set during any transaction, mailbox/cpuid/mcamsr/readreg, +* to indicate a transaction is in progress. +* no_new_trans: set in rmmod/unbind path to indicate, +* not to accept new transactions +*/ +struct apml_sbrmi_device { +struct miscdevice sbrmi_misc_dev; +struct completion misc_fops_done; +struct regmap *regmap; +/* Mutex locking */ +struct mutex lock; +atomic_t in_progress; +atomic_t no_new_trans; +u32 pwr_limit_max; +u8 rev; +u8 dev_static_addr; +} __packed; + +int sbrmi_mca_msr_read (struct apml_sbrmi_device *rmi_dev, +struct apml_message *msg); +int sbrmi_cpuid_read (struct apml_sbrmi_device *rmi_dev, +struct apml_message *msg); +int sbrmi_mailbox_xfer (struct apml_sbrmi_device *rmi_dev, +struct apml_message *msg); +#endif /*_AMD_APML_SBRMI_H_*/
drivers/hwmon/sbrmi.c +204 -194
507 lines not on this page · raw message
include/uapi/linux/amd-apml.h new file +74 -0
74 lines not on this page · raw message
--
2.25.1
Guenter Roeck
maintainerRe: [PATCH 2/2] sbrmi: Add support for APML protocols
Quoted 6 lines · Naveen Krishna Chatradhi
From: Naveen Krishna Chatradhi <[email protected]>
The present sbrmi module only support reporting power. However, AMD data center processors support various system management functionality Out-of-band over Advanced Platform Management Link APML.
Quoted 6 lines
Register a miscdevice, which creates a device
/dev/sbrmiXwith an IOCTL interface for the user space to invoke the following protocols.
- Mailbox read/write (already defined insbrmi_mailbox_xfer())
- CPUID read
- MCAMSR read
This is not hardware monitoring functionality and would have to reside elsewhere, outside the hwmon subsystem.
Guenter
Re: [PATCH 2/2] sbrmi: Add support for APML protocols
+ @Misc and @MFD maintainers in CC
Hi
Quoted 17 lines · Guenter Roeck
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 6 lines · Naveen Krishna Chatradhi
From: Naveen Krishna Chatradhi <[email protected]>
The present sbrmi module only support reporting power. However, AMD data center processors support various system management functionality Out-of-band over Advanced Platform Management Link APML.
Quoted 6 lines
Register a miscdevice, which creates a device
/dev/sbrmiXwith an IOCTL interface for the user space to invoke the following protocols.
- Mailbox read/write (already defined insbrmi_mailbox_xfer())
- CPUID read
- MCAMSR read
Quoted 2 lines
This is not hardware monitoring functionality and would have to reside elsewhere, outside the hwmon subsystem.
I thought as much, please provide your opinion on the following approach.
Background: Present sbrmi under hwmon subsystem is probed as an i2c driver and reports power.
However, APML interface defines few other protocols to support OOB system management functionality.
As adding the core functionality of the APML interface in drivers/hwmon/sbrmi is not the correct approach.
We would like do the following
1. Move the i2c client probe, misc device registration and rmi_mailbox_xfer() function to a drivers/misc.
2. Add an MFD device with a cell, which probes the hwmon/sbrmi and continues to report power using the symbols exported by the misc/sbrmi.
3. Define an ioctl for user-space to access other system management functionality.
a. The open-sourced https://github.com/amd/esmi_oob_library will continue to provide user space programmable API
Regards,
naveenk
Quoted 3 lines
Guenter
Guenter Roeck
maintainerRe: [PATCH 2/2] sbrmi: Add support for APML protocols
Quoted 31 lines · Naveen Krishna
+ @Misc and @MFD maintainers in CC
Hi
Quoted 17 lines · Guenter Roeck
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 6 lines · Naveen Krishna Chatradhi
From: Naveen Krishna Chatradhi <[email protected]>
The present sbrmi module only support reporting power. However, AMD data center processors support various system management functionality Out-of-band over Advanced Platform Management Link APML.
Quoted 6 lines
Register a miscdevice, which creates a device
/dev/sbrmiXwith an IOCTL interface for the user space to invoke the following protocols.
- Mailbox read/write (already defined insbrmi_mailbox_xfer())
- CPUID read
- MCAMSR readQuoted 2 lines
This is not hardware monitoring functionality and would have to reside elsewhere, outside the hwmon subsystem.
I thought as much, please provide your opinion on the following approach.
Background: Present sbrmi under hwmon subsystem is probed as an i2c driver and reports power.
However, APML interface defines few other protocols to support OOB system management functionality.
Quoted 8 lines
As adding the core functionality of the APML interface in
drivers/hwmon/sbrmiis not the correct approach.We would like do the following
1. Move the i2c client probe, misc device registration and
rmi_mailbox_xfer()function to adrivers/misc.2. Add an MFD device with a cell, which probes the hwmon/sbrmi and continues to report power using the symbols exported by the misc/sbrmi.
afaik mfd API function must not be used outside drivers/mfd. The mfd maintainer is (or at least used to be) pretty strict on that. The core code of a multi-function device might better be implemented in drivers/mfd, with drivers in drivers/misc (for the misc device) and drivers/hwmon/ (for hwmon functionality). The misc and hwmon drivers could then communicate with the core using regmap.
drivers/mailbox/ supports mailbox style communication. I don't know if that would apply to the mailbox functionality implemented here.
Guenter
Quoted 11 lines
3. Define an ioctl for user-space to access other system management functionality.
a. The open-sourced https://github.com/amd/esmi_oob_library will continue to provide user space programmable API
Regards,
naveenk
Quoted 3 lines
Guenter
Re: [PATCH 2/2] sbrmi: Add support for APML protocols
Quoted 41 lines · Guenter Roeck
Quoted 31 lines · Naveen Krishna
+ @Misc and @MFD maintainers in CC
Hi
Quoted 17 lines · Guenter Roeck
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 6 lines · Naveen Krishna Chatradhi
From: Naveen Krishna Chatradhi <[email protected]>
The present sbrmi module only support reporting power. However, AMD data center processors support various system management functionality Out-of-band over Advanced Platform Management Link APML.
Quoted 6 lines
Register a miscdevice, which creates a device
/dev/sbrmiXwith an IOCTL interface for the user space to invoke the following protocols.
- Mailbox read/write (already defined insbrmi_mailbox_xfer())
- CPUID read
- MCAMSR readQuoted 2 lines
This is not hardware monitoring functionality and would have to reside elsewhere, outside the hwmon subsystem.
I thought as much, please provide your opinion on the following approach.
Background: Present sbrmi under hwmon subsystem is probed as an i2c driver and reports power.
However, APML interface defines few other protocols to support OOB system management functionality.
Quoted 8 lines
As adding the core functionality of the APML interface in
drivers/hwmon/sbrmiis not the correct approach.We would like do the following
1. Move the i2c client probe, misc device registration and
rmi_mailbox_xfer()function to adrivers/misc.2. Add an MFD device with a cell, which probes the hwmon/sbrmi and continues to report power using the symbols exported by the misc/sbrmi.
Quoted 6 lines
afaik mfd API function must not be used outside
drivers/mfd. The mfd maintainer is (or at least used to be) pretty strict on that. The core code of a multi-function device might better be implemented indrivers/mfd, with drivers indrivers/misc(for the misc device) anddrivers/hwmon/(for hwmon functionality). The misc and hwmon drivers could then communicate with the core using regmap.
Yes, please only use the MFD API from drivers/mfd.
There are 'offenders' that slipped by me, but in general if you need to create an MFD then it should be located in the MFD subsystem.
Quoted 2 lines
drivers/mailbox/supports mailbox style communication. I don't know if that would apply to the mailbox functionality implemented here.
Signature · 1 line
Lee Jones [李琼斯]
Re: [PATCH 2/2] sbrmi: Add support for APML protocols
Quoted 49 lines · Lee Jones
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 44 lines · Guenter Roeck
Quoted 29 lines · Naveen Krishna
+ @Misc and @MFD maintainers in CC
Hi
Quoted 10 lines · Guenter Roeck
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 6 lines · Naveen Krishna Chatradhi
From: Naveen Krishna Chatradhi <[email protected]>
The present sbrmi module only support reporting power. However, AMD data center processors support various system management functionality Out-of-band over Advanced Platform Management Link APML.
Quoted 8 lines
Quoted 6 lines
Register a miscdevice, which creates a device
/dev/sbrmiXwith an IOCTL interface for the user space to invoke the following protocols.
- Mailbox read/write (already defined insbrmi_mailbox_xfer())
- CPUID read
- MCAMSR readThis is not hardware monitoring functionality and would have to reside elsewhere, outside the hwmon subsystem.
I thought as much, please provide your opinion on the following approach.
Background: Present sbrmi under hwmon subsystem is probed as an i2c driver and reports power.
However, APML interface defines few other protocols to support OOB system management functionality.
Quoted 8 lines
As adding the core functionality of the APML interface in
drivers/hwmon/sbrmiis not the correct approach.We would like do the following
1. Move the i2c client probe, misc device registration and
rmi_mailbox_xfer()function to adrivers/misc.2. Add an MFD device with a cell, which probes the hwmon/sbrmi and continues to report power using the symbols exported by the misc/sbrmi.
afaik mfd API function must not be used outside
drivers/mfd. The mfd maintainer is (or at least used to be) pretty strict on that. The core code of a multi-function device might better be implemented indrivers/mfd, with drivers indrivers/misc(for the misc device) anddrivers/hwmon/(for hwmon functionality). The misc and hwmon drivers could then communicate with the core using regmap.
Thanks Guenter, for the inputs.
Quoted 4 lines
Yes, please only use the MFD API from
drivers/mfd.There are 'offenders' that slipped by me, but in general if you need to create an MFD then it should be located in the MFD subsystem.
Thanks Lee, how about
1. Move the i2c client probe, misc device registration and rmi_mailbox_xfer() function from existing hwmon/sbrmi.c to a drivers/mfd (Instead of drivers/misc)
a. Provide an ioctl interface through misc device node /dev/sbrmiX for the user space to invoke the following protocols.
- Mailbox xfer (already defined in sbrmi_mailbox_xfer())
- CPUID access
- MCAMSR access
2. Add an MFD device with a cell, which probes the hwmon/sbrmi and continues to report power using the symbols exported by the mfd/sbrmi.
Quoted 3 lines
Quoted 3 lines
drivers/mailbox/supports mailbox style communication. I don't know if that would apply to the mailbox functionality implemented here.
I've explored that path, and APML mailbox does not fit well into the design, we do not have a dedicated interrupt line as well.
Quoted 2 lines
--
Lee Jones [李琼斯]
regards,
Naveenk
Quoted 1 line
Guenter Roeck
maintainerQuoted 39 lines · Naveen Krishna Chatradhi
From: Naveen Krishna Chatradhi <[email protected]>
- regmap subsystem provides multiple benefits over direct smbus APIs
- The susbsytem can be helpful in following cases
- Differnet types of bus (i2c/i3c)
- Different Register address size (1byte/2byte)Signed-off-by Akshay Gupta <[email protected]>Signed-off-by Naveen Krishna Chatradhi <[email protected]>--- drivers/hwmon/Kconfig | 1 + drivers/hwmon/sbrmi.c | 61 ++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 27 deletions(-)drivers/hwmon/Kconfig +1
-0@@ -1759,6 +1759,7 @@ config SENSORS_SBTSI config SENSORS_SBRMI tristate "Emulated SB-RMI sensor" depends on I2C + select REGMAP_I2C help If you say yes here you get support for emulated RMI sensors on AMD SoCs with APML interface connected to a BMC device.drivers/hwmon/sbrmi.c +1
-0@@ -14,6 +14,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/of.h> +#include <linux/regmap.h> /* Do not allow setting negative power limit */ #define SBRMI_PWR_MIN 0
drivers/hwmon/sbrmi.c enum sbrmi_reg { · line 65
Quoted 2 lines
drivers/hwmon/sbrmi.c +0
-0@@ -63,6 +64,7 @@ enum sbrmi_reg {struct sbrmi_data {struct i2c_client *client;
There is only one user of this structure left, and it is for a questionable error message (questionable since all other errors are handled silently). I would suggest to drop both.
Quoted 6 lines
drivers/hwmon/sbrmi.c +1
-0struct mutex lock; +struct regmap *regmap;u32 pwr_limit_max; };
drivers/hwmon/sbrmi.c struct sbrmi_mailbox_msg { · line 81
Quoted 9 lines
drivers/hwmon/sbrmi.c +2
-2@@ -73,22 +75,21 @@ struct sbrmi_mailbox_msg {u32 data_out; }; -static int sbrmi_enable_alert (struct i2c_client *client) +static int sbrmi_enable_alert (struct sbrmi_data *data) { -int ctrl; +int ctrl, ret =0 ;
Unnecessary initialization
Quoted 20 lines
drivers/hwmon/sbrmi.c +4
-5/* * Enable the SB-RMI Software alert status * by writing 0 to bit 4 of Control register(0x1) */ - ctrl =i2c_smbus_read_byte_data (client,SBRMI_CTRL ); -if (ctrl <0 ) -return ctrl; + ret =regmap_read (data->regmap,SBRMI_STATUS , &ctrl); +if (ret <0 ) +return ret;if (ctrl &0x10 ) { ctrl &= ~0x10 ; -return i2c_smbus_write_byte_data (client, -SBRMI_CTRL , ctrl); +return regmap_write (data->regmap,SBRMI_CTRL , ctrl); }return 0 ;
drivers/hwmon/sbrmi.c static int sbrmi_enable_alert(struct i2c_client *client) · line 101
Quoted 4 lines
drivers/hwmon/sbrmi.c +1
-0@@ -97,6 +98,7 @@ static int sbrmi_enable_alert(struct i2c_client *client)static int rmi_mailbox_xfer (struct sbrmi_data *data,struct sbrmi_mailbox_msg *msg) { +unsigned int bytes =0 ;
Unnecessary initialization
Quoted 112 lines
drivers/hwmon/sbrmi.c +19
-18int i, ret, retry =10 ;int sw_status;u8 byte;@@ -104,14 +106,12 @@ static int rmi_mailbox_xfer(struct sbrmi_data *data,mutex_lock (&data->lock);/* Indicate firmware a command is to be serviced */ - ret =i2c_smbus_write_byte_data (data->client, -SBRMI_INBNDMSG7 ,START_CMD ); + ret =regmap_write (data->regmap,SBRMI_INBNDMSG7 ,START_CMD );if (ret <0 ) goto exit_unlock;/* Write the command to SBRMI::InBndMsg_inst0 */ - ret =i2c_smbus_write_byte_data (data->client, -SBRMI_INBNDMSG0 , msg->cmd); + ret =regmap_write (data->regmap,SBRMI_INBNDMSG0 , msg->cmd);if (ret <0 ) goto exit_unlock;@@ -122,8 +122,7 @@ static int rmi_mailbox_xfer(struct sbrmi_data *data,*/ for (i =0 ; i <4 ; i++) { byte = (msg->data_in >> i *8 ) &0xff ; - ret =i2c_smbus_write_byte_data (data->client, -SBRMI_INBNDMSG1 + i, byte); + ret =regmap_write (data->regmap,SBRMI_INBNDMSG1 + i, byte);if (ret <0 ) goto exit_unlock; }@@ -132,8 +131,7 @@ static int rmi_mailbox_xfer(struct sbrmi_data *data,* Write 0x01 to SBRMI::SoftwareInterrupt to notify firmware to * perform the requested read or write command */ - ret =i2c_smbus_write_byte_data (data->client, -SBRMI_SW_INTERRUPT ,TRIGGER_MAILBOX ); + ret =regmap_write (data->regmap,SBRMI_SW_INTERRUPT ,TRIGGER_MAILBOX );if (ret <0 ) goto exit_unlock;@@ -143,8 +141,7 @@ static int rmi_mailbox_xfer(struct sbrmi_data *data,* of the requested command */ do { - sw_status = i2c_smbus_read_byte_data(data->client, - SBRMI_STATUS); + ret = regmap_read(data->regmap, SBRMI_STATUS, &sw_status); if (sw_status < 0) { ret = sw_status; goto exit_unlock;@@ -168,11 +165,11 @@ static int rmi_mailbox_xfer(struct sbrmi_data *data,*/ if (msg->read) {for (i =0 ; i <4 ; i++) { - ret =i2c_smbus_read_byte_data (data->client, -SBRMI_OUTBNDMSG1 + i); + ret =regmap_read (data->regmap, +SBRMI_OUTBNDMSG1 + i, &bytes);if (ret <0 ) goto exit_unlock; - msg->data_out |= ret << i *8 ; + msg->data_out |= bytes << i *8 ; } }@@ -180,9 +177,8 @@ static int rmi_mailbox_xfer(struct sbrmi_data *data,* BMC must write 1'b1 to SBRMI::Status[SwAlertSts] to clear the * ALERT to initiator */ - ret =i2c_smbus_write_byte_data (data->client,SBRMI_STATUS , - sw_status |SW_ALERT_MASK ); - + ret =regmap_write (data->regmap,SBRMI_STATUS , + sw_status |SW_ALERT_MASK ); exit_unlock:mutex_unlock (&data->lock);return ret;@@ -265,7 +261,7 @@ static umode_t sbrmi_is_visible(const void *data,return 0 ; } -static const struct hwmon_channel_info *const sbrmi_info[] = { +static const struct hwmon_channel_info *sbrmi_info[] = {HWMON_CHANNEL_INFO (power,HWMON_P_INPUT |HWMON_P_CAP |HWMON_P_CAP_MAX ),NULL @@ -302,6 +298,10 @@ static int sbrmi_probe(struct i2c_client *client)struct device *dev = &client->dev;struct device *hwmon_dev;struct sbrmi_data *data; +struct regmap_config sbrmi_i2c_regmap_config = { + .reg_bits =8 , + .val_bits =8 , + };int ret; data =devm_kzalloc (dev,sizeof (struct sbrmi_data ),GFP_KERNEL );@@ -310,9 +310,12 @@ static int sbrmi_probe(struct i2c_client *client) data->client = client;mutex_init (&data->lock); + data->regmap =devm_regmap_init_i2c (client, &sbrmi_i2c_regmap_config); +if (IS_ERR (data->regmap)) +return PTR_ERR (data->regmap);/* Enable alert for SB-RMI sequence */ - ret =sbrmi_enable_alert (client); + ret =sbrmi_enable_alert (data);if (ret <0 )return ret;
drivers/hwmon/sbrmi.c static int sbrmi_probe(struct i2c_client *client) · line 328
Quoted 7 lines
drivers/hwmon/sbrmi.c +2
-2@@ -321,8 +324,12 @@ static int sbrmi_probe(struct i2c_client *client)if (ret <0 )return ret; - hwmon_dev =devm_hwmon_device_register_with_info (dev, client->name, data, - &sbrmi_chip_info,NULL ); +dev_set_drvdata (dev, (void *)data); +
Why would this be needed ? On a side note, the typecast is unnecessary.
Guenter