platform/x86/amd/hsmp: create plat specific struct
Subsystems (4)
PATCH v4 1/3 platform/x86/amd/hsmp: create plat specific struct
Having a separate platform device structure helps in future, to contain platform specific variables and other data.
Also, define macros for dev nodes
---
Changes since v1:
1. defined HSMP_CDEV_NAME and HSMP_DEVNODE_NAME macros Changes since v2:
1. moved num_sockets variable to plat_dev structure Changes since v3:
1. updated commit message
drivers/platform/x86/amd/hsmp.c | 61 ++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 23 deletions(-)
drivers/platform/x86/amd/hsmp.c +38 -23
@@ -47,9 +47,22 @@#define HSMP_INDEX_REG 0xc4#define HSMP_DATA_REG 0xc8 -static struct semaphore *hsmp_sem; +#define HSMP_CDEV_NAME "hsmp_cdev" +#define HSMP_DEVNODE_NAME "hsmp" -static struct miscdevice hsmp_device; +struct hsmp_socket { +struct semaphore hsmp_sem; +u16 sock_ind; +}; + +struct hsmp_plat_device { +struct miscdevice hsmp_device; +struct hsmp_socket *sock; +struct device *dev; +u16 num_sockets; +}; + +static struct hsmp_plat_device plat_dev;static int amd_hsmp_rdwr (struct pci_dev *root,u32 address,u32 *value,bool write)
@@ -188,6 +201,7 @@ static int validate_message(struct hsmp_message *msg)int hsmp_send_message (struct hsmp_message *msg) { +struct hsmp_socket *sock = &plat_dev.sock[msg->sock_ind];struct amd_northbridge *nb;int ret;
@@ -208,14 +222,13 @@ int hsmp_send_message(struct hsmp_message *msg)* In SMP system timeout of 100 millisecs should * be enough for the previous thread to finish the operation */ - ret =down_timeout (&hsmp_sem[msg->sock_ind], -msecs_to_jiffies (HSMP_MSG_TIMEOUT )); + ret =down_timeout (&sock->hsmp_sem,msecs_to_jiffies (HSMP_MSG_TIMEOUT ));if (ret <0 )return ret; ret =__hsmp_send_message (nb->root, msg); -up (&hsmp_sem[msg->sock_ind]); +up (&sock->hsmp_sem);return ret; }
@@ -321,28 +334,31 @@ static int hsmp_pltdrv_probe(struct platform_device *pdev) {int i; - hsmp_sem =devm_kzalloc (&pdev->dev, - (amd_nb_num () *sizeof (struct semaphore )), -GFP_KERNEL ); -if (!hsmp_sem) + plat_dev.sock =devm_kzalloc (&pdev->dev, + (plat_dev.num_sockets *sizeof (struct hsmp_socket )), +GFP_KERNEL ); +if (!plat_dev.sock)return -ENOMEM ; + plat_dev.dev = &pdev->dev; -for (i =0 ; i <amd_nb_num (); i++) -sema_init (&hsmp_sem[i],1 ); +for (i =0 ; i < plat_dev.num_sockets; i++) { +sema_init (&plat_dev.sock[i].hsmp_sem,1 ); + plat_dev.sock[i].sock_ind = i; + } - hsmp_device.name ="hsmp_cdev" ; - hsmp_device.minor =MISC_DYNAMIC_MINOR ; - hsmp_device.fops = &hsmp_fops; - hsmp_device.parent = &pdev->dev; - hsmp_device.nodename ="hsmp" ; - hsmp_device.mode =0644 ; + plat_dev.hsmp_device.name =HSMP_CDEV_NAME ; + plat_dev.hsmp_device.minor =MISC_DYNAMIC_MINOR ; + plat_dev.hsmp_device.fops = &hsmp_fops; + plat_dev.hsmp_device.parent = &pdev->dev; + plat_dev.hsmp_device.nodename =HSMP_DEVNODE_NAME ; + plat_dev.hsmp_device.mode =0644 ; -return misc_register (&hsmp_device); +return misc_register (&plat_dev.hsmp_device); }static void hsmp_pltdrv_remove (struct platform_device *pdev) { -misc_deregister (&hsmp_device); +misc_deregister (&plat_dev.hsmp_device); }static struct platform_driver amd_hsmp_driver = {
@@ -358,7 +374,6 @@ static struct platform_device *amd_hsmp_platdev;static int __init hsmp_plt_init (void ) {int ret = -ENODEV ; -u16 num_sockets;int i;if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD || boot_cpu_data.x86 <0x19 ) {
@@ -371,12 +386,12 @@ static int __init hsmp_plt_init(void)* amd_nb_num() returns number of SMN/DF interfaces present in the system * if we have N SMN/DF interfaces that ideally means N sockets */ - num_sockets =amd_nb_num (); -if (num_sockets ==0 ) + plat_dev.num_sockets =amd_nb_num (); +if (plat_dev.num_sockets ==0 )return ret;/* Test the hsmp interface on each socket */ -for (i =0 ; i < num_sockets; i++) { +for (i =0 ; i < plat_dev.num_sockets; i++) { ret =hsmp_test (i,0xDEADBEEF );if (ret) { pr_err("HSMP is not supported on Fam:%x model:%x\n",
Signature · 1 line
2.25.1
PATCH v4 2/3 platform/x86/amd/hsmp: add support for metrics tbl
AMD MI300 MCM provides GET_METRICS_TABLE message to retrieve all the system management information from SMU.
The metrics table is made available as hexadecimal sysfs binary file under per socket sysfs directory created at /sys/devices/platform/amd_hsmp/socket%d/metrics_bin
Metrics table definitions will be documented as part of Public PPR. The same is defined in the amd_hsmp.h header.
---
Changes since v1:
1. Remove HSMP_DEVNODE_NAME and HSMP_CDEV_NAME macro definitions in this patch
2. Remove extra space in comments for HSMP_GET_METRIC_TABLE_VER, HSMP_GET_METRIC_TABLE and HSMP_GET_METRIC_TABLE_DRAM_ADDR enum definition in amd_hsmp.h files
3. Change check, count == 0 to !count in hsmp_metric_tbl_read() function
4. Add hsmp_metric_table_visible() function
5. hsmp_create_metric_tbl_sysfs_file() is renamed as hsmp_init_metric_tbl_bin_attr() and code is also modified slightly
6. Modify hsmp_create_sysfs_file() to use devm_device_add_groups()
7. Change from cleanup label to deregister label
8. Add dev_err print in hsmp_get_tbl_dram_base()
9. Reword "Unable to Failed" in hsmp_get_tbl_dram_base()
10. Add HSMP_GRP_NAME_SIZE and NUM_ATTRS macros
11. Remove sysfs cleanup code in hsmp_pltdrv_remove()
12. Correct ATRR typo error
13. Change sprintf to snprintf
14. Check metrics table support only against HSMP_PROTO_VER6
Changes since v2:
1. squash documentation patch into this patch
2. change from num_sockets to plat_dev.num_sockets
Changes since v3:
1. sysfs_attr_init() to sysfs_bin_attr_init(), errors reported by allyesconfig build
2. "C++" style comments in amd_hsmp.h to C style comments, fix errors reported by W=1
3. "i" data type to u8 in hsmp_create_sysfs_file(), fix errors reported by W=1
4. remove devm_kzalloc for attr_grp->name in hsmp_create_sysfs_file()
5. rename HSMP_GRP_NAME_SIZE to HSMP_ATTR_GRP_NAME_SIZE and define value to 10
6. move hsmp_get_tbl_dram_base() call to hsmp_init_metric_tbl_bin_attr()
7. hsmp_metric_table_visible to hsmp_is_sock_attr_visible
8. use condition check "if (count < bin_attr->size)"
in hsmp_metric_tbl_read
9. Reorder misc_register and hsmp_cache_proto_ver calls
10. remove devm_kzalloc in hsmp_init_metric_tbl_bin_attr()
11. Wordings in documentation and header and add some more comments in the code
Documentation/arch/x86/amd_hsmp.rst | 18 +++ arch/x86/include/uapi/asm/amd_hsmp.h | 109 +++++++++++++++++ drivers/platform/x86/amd/hsmp.c | 170 ++++++++++++++++++++++++++- 3 files changed, 295 insertions(+), 2 deletions(-)
Documentation/arch/x86/amd_hsmp.rst +18 -0
@@ -41,6 +41,24 @@ In-kernel integration: * Locking across callers is taken care by the driver. +HSMP sysfs interface +==================== + +1. Metrics table binary sysfs + +AMD MI300A MCM provides GET_METRICS_TABLE message to retrieve +most of the system management information from SMU in one go. + +The metrics table is made available as hexadecimal sysfs binary file +under per socket sysfs directory created at +/sys/devices/platform/amd_hsmp/socket%d/metrics_bin + +Note: lseek is not supported as entire metrics table is read + +Metrics table definitions will be documented as part of Public PPR. +The same is defined in the amd_hsmp.h header. + + An example ==========
arch/x86/include/uapi/asm/amd_hsmp.h +109 -0
127 lines not on this page · raw message
drivers/platform/x86/amd/hsmp.c +168 -2
205 lines not on this page · raw message
Signature · 1 line
2.25.1
Ilpo Järvinen
maintainerRe: [PATCH v4 2/3] platform/x86/amd/hsmp: add support for metrics tbl
Quoted 19 lines · Suma Hegde
AMD MI300 MCM provides
GET_METRICS_TABLEmessage to retrieve all the system management information from SMU.The metrics table is made available as hexadecimal sysfs binary file under per socket sysfs directory created at /sys/devices/platform/amd_hsmp/socket%d/metrics_bin
Metrics table definitions will be documented as part of Public PPR. The same is defined in the
amd_hsmp.hheader.Signed-off-by Suma Hegde <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>drivers/platform/x86/amd/hsmp.c +5
-0+static int hsmp_create_sysfs_interface (void ) +{ +const struct attribute_group **hsmp_attr_grps; +struct bin_attribute **hsmp_bin_attrs; +struct attribute_group *attr_grp;
drivers/platform/x86/amd/hsmp.c static const struct file_operations hsmp_fops = { · line 447
Quoted 10 lines
drivers/platform/x86/amd/hsmp.c +10
-0+int ret; +u8 i; + + hsmp_attr_grps =devm_kzalloc (plat_dev.dev,sizeof (struct attribute_group *) * + (plat_dev.num_sockets +1 ),GFP_KERNEL ); +if (!hsmp_attr_grps) +return -ENOMEM ; + +/* Create a sysfs directory for each socket */ +for (i =0 ; i < plat_dev.num_sockets; i++) {
The change for i to u8 seems not very wise given num_sockets still is u16 as it can turn this into an infinite loop.
Signature · 1 line
i.
Re: [PATCH v4 2/3] platform/x86/amd/hsmp: add support for metrics tbl
Quoted 27 lines · Ilpo Järvinen
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 22 lines · Suma Hegde
AMD MI300 MCM provides
GET_METRICS_TABLEmessage to retrieve all the system management information from SMU.The metrics table is made available as hexadecimal sysfs binary file under per socket sysfs directory created at /sys/devices/platform/amd_hsmp/socket%d/metrics_bin
Metrics table definitions will be documented as part of Public PPR. The same is defined in the
amd_hsmp.hheader.Signed-off-by Suma Hegde <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>drivers/platform/x86/amd/hsmp.c +9
-0+static int hsmp_create_sysfs_interface (void ) +{ +const struct attribute_group **hsmp_attr_grps; +struct bin_attribute **hsmp_bin_attrs; +struct attribute_group *attr_grp; +int ret; +u8 i; + + hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct attribute_group *) *
Quoted 8 lines
drivers/platform/x86/amd/hsmp.c static const struct file_operations hsmp_fops = { · line 447
Quoted 6 lines
drivers/platform/x86/amd/hsmp.c +6
-0+ (plat_dev.num_sockets + 1), GFP_KERNEL); +if (!hsmp_attr_grps) +return -ENOMEM ; + +/* Create a sysfs directory for each socket */ +for (i =0 ; i < plat_dev.num_sockets; i++) {The change for i to u8 seems not very wise given
num_socketsstill is u16 as it can turn this into an infinite loop.
Hi Ilpo,
amd_nb_num() API which we use to identify the number of sockets on a node returns u16. So, we used u16 for plat_dev.num_sockets. u8 should be enough, as present AMD processors support upto 8 sockets on a node.
Coming to the warning raised:
We have defined, HSMP_ATTR_GRP_NAME_SIZE as 10bytes, 6 chars for 'socket', 3 chars for 3digits (socket index) and a null terminator. Socket index may not need more than 3 digits in the foreseeable future. How about, we declare i as u16 and typecast it as (u8) in the snprintf.
int ret;
- u8 i;
+ u16 i;
hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct attribute_group *) *
(plat_dev.num_sockets + 1), GFP_KERNEL); @@ -449,7 +449,7 @@ static int hsmp_create_sysfs_interface(void) if (!attr_grp)
return -ENOMEM;
- snprintf(plat_dev.sock[i].name, HSMP_ATTR_GRP_NAME_SIZE, "socket%u", i); + snprintf(plat_dev.sock[i].name, HSMP_ATTR_GRP_NAME_SIZE,
+ "socket%u", (u8)i);
attr_grp->name = plat_dev.sock[i].name;
Thanks and Regards,
Suma
Quoted 4 lines
--
i.
Ilpo Järvinen
maintainerRe: [PATCH v4 2/3] platform/x86/amd/hsmp: add support for metrics tbl
Quoted 36 lines · Suma
Quoted 26 lines · Ilpo Järvinen
Quoted 24 lines · Suma Hegde
AMD MI300 MCM provides
GET_METRICS_TABLEmessage to retrieve all the system management information from SMU.The metrics table is made available as hexadecimal sysfs binary file under per socket sysfs directory created at /sys/devices/platform/amd_hsmp/socket%d/metrics_bin
Metrics table definitions will be documented as part of Public PPR. The same is defined in the
amd_hsmp.hheader.Signed-off-by Suma Hegde <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>drivers/platform/x86/amd/hsmp.c +9
-0+static int hsmp_create_sysfs_interface (void ) +{ +const struct attribute_group **hsmp_attr_grps; +struct bin_attribute **hsmp_bin_attrs; +struct attribute_group *attr_grp; +int ret; +u8 i; + + hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct
attribute_group*) *
+ (plat_dev.num_sockets + 1),Quoted 8 lines
drivers/platform/x86/amd/hsmp.c static const struct file_operations hsmp_fops = { · line 447
Quoted 6 lines
GFP_KERNEL);drivers/platform/x86/amd/hsmp.c +5
-0+if (!hsmp_attr_grps) +return -ENOMEM ; + +/* Create a sysfs directory for each socket */ +for (i =0 ; i < plat_dev.num_sockets; i++) {The change for i to u8 seems not very wise given
num_socketsstill is u16 as it can turn this into an infinite loop.
Quoted 6 lines
Hi Ilpo,
amd_nb_num()API which we use to identify the number of sockets on a node returns u16. So, we used u16 for plat_dev.num_sockets.
u8 should be enough, as present AMD processors support upto 8 sockets on a node.
I wasn't expecting it to fail at the moment, I just don't want to leave a silent trap for the future.
Quoted 17 lines
Coming to the warning raised:
We have defined,HSMP_ATTR_GRP_NAME_SIZEas 10bytes, 6 chars for 'socket', 3 chars for 3digits (socket index) and a null terminator.
Socket index may not need more than 3 digits in the foreseeable future. How about, we declare i as u16 and typecast it as (u8) in the snprintf.int ret;
- u8 i;
+ u16 i;
hsmp_attr_grps= devm_kzalloc(plat_dev.dev,sizeof(struct attribute_group *)*
(plat_dev.num_sockets + 1),GFP_KERNEL); @@ -449,7 +449,7 @@ static inthsmp_create_sysfs_interface(void)
if (!attr_grp)
return-ENOMEM;
Quoted 5 lines
-
snprintf(plat_dev.sock[i].name, HSMP_ATTR_GRP_NAME_SIZE, "socket%u", i);
+ snprintf(plat_dev.sock[i].name,HSMP_ATTR_GRP_NAME_SIZE,
+ "socket%u", (u8)i);
attr_grp->name= plat_dev.sock[i].name;
This is similarly trappy as it truncates i if num_sockets is > 255.
I suggest you just put this into start of the function:
/* String formatting is currently limited to u8 sockets */ if (WARN_ON(plat_dev.num_sockets > U8_MAX)) return -ERANGE;
to catch the too many sockets case if it is ever going to occur. And explain in the changelog that u8 is enough for foreseeable future and the string formatting triggers a warning if unnecessarily large type is passed to snprintf().
Signature · 1 line
i.
Re: [PATCH v4 2/3] platform/x86/amd/hsmp: add support for metrics tbl
Quoted 73 lines · Ilpo Järvinen
Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 34 lines · Suma
Quoted 25 lines · Ilpo Järvinen
Quoted 23 lines · Suma Hegde
AMD MI300 MCM provides
GET_METRICS_TABLEmessage to retrieve all the system management information from SMU.The metrics table is made available as hexadecimal sysfs binary file under per socket sysfs directory created at /sys/devices/platform/amd_hsmp/socket%d/metrics_bin
Metrics table definitions will be documented as part of Public PPR. The same is defined in the
amd_hsmp.hheader.Signed-off-by Suma Hegde <[email protected]>Reviewed-by Naveen Krishna Chatradhi <[email protected]>drivers/platform/x86/amd/hsmp.c +9
-0+static int hsmp_create_sysfs_interface (void ) +{ +const struct attribute_group **hsmp_attr_grps; +struct bin_attribute **hsmp_bin_attrs; +struct attribute_group *attr_grp; +int ret; +u8 i; + + hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct
attribute_group*) *
+ (plat_dev.num_sockets + 1),Quoted 8 lines
drivers/platform/x86/amd/hsmp.c static const struct file_operations hsmp_fops = { · line 447
Quoted 6 lines
GFP_KERNEL);drivers/platform/x86/amd/hsmp.c +5
-0+if (!hsmp_attr_grps) +return -ENOMEM ; + +/* Create a sysfs directory for each socket */ +for (i =0 ; i < plat_dev.num_sockets; i++) {The change for i to u8 seems not very wise given
num_socketsstill is u16 as it can turn this into an infinite loop.Quoted 6 lines
Hi Ilpo,
amd_nb_num()API which we use to identify the number of sockets on a node returns u16. So, we used u16 for plat_dev.num_sockets.
u8 should be enough, as present AMD processors support upto 8 sockets on a node.I wasn't expecting it to fail at the moment, I just don't want to leave a silent trap for the future.
Quoted 17 lines
Coming to the warning raised:
We have defined,HSMP_ATTR_GRP_NAME_SIZEas 10bytes, 6 chars for 'socket', 3 chars for 3digits (socket index) and a null terminator.
Socket index may not need more than 3 digits in the foreseeable future. How about, we declare i as u16 and typecast it as (u8) in the snprintf.int ret;(quoted diff) +1
-1- u8 i; + u16 i; hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct
attribute_group*) *(plat_dev.num_sockets + 1), GFP_KERNEL); @@ -449,7 +449,7 @@ static int hsmp_create_sysfs_interface(void) if (!attr_grp) return -ENOMEM;Quoted 5 lines
- snprintf(plat_dev.sock[i].name,
HSMP_ATTR_GRP_NAME_SIZE, "socket%u",
i);
+ snprintf(plat_dev.sock[i].name,HSMP_ATTR_GRP_NAME_SIZE,+ "socket%u", (u8)i); attr_grp->name = plat_dev.sock[i].name;This is similarly trappy as it truncates i if
num_socketsis > 255.I suggest you just put this into start of the function:
Quoted 8 lines
/* String formatting is currently limited to u8 sockets */ if (WARN_ON(plat_dev.num_sockets > U8_MAX)) return -ERANGE;to catch the too many sockets case if it is ever going to occur. And explain in the changelog that u8 is enough for foreseeable future and the string formatting triggers a warning if unnecessarily large type is passed to
snprintf().
Ok Ilpo, I will add this WARN_ON and send v5 patch set.
Quoted 3 lines
--
i.
PATCH v4 3/3 platform/x86/amd/hsmp: improve the error log
In reply to Suma Hegde1. Change print message during platform init to a more meaningful clear message.
2. Return the error code returned by hsmp_test() itself, rather then returning a common EOPNOTSUPP error.
---
changes since v3:
none
drivers/platform/x86/amd/hsmp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/platform/x86/amd/hsmp.c +3 -3
@@ -560,10 +560,10 @@ static int __init hsmp_plt_init(void)for (i =0 ; i < plat_dev.num_sockets; i++) { ret =hsmp_test (i,0xDEADBEEF );if (ret) { -pr_err ("HSMP is not supported on Fam:%x model:%x\n" , +pr_err ("HSMP test message failed on Fam:%x model:%x\n" , boot_cpu_data.x86, boot_cpu_data.x86_model); -pr_err ("Or Is HSMP disabled in BIOS ?\n" ); -return -EOPNOTSUPP ; +pr_err ("Is HSMP disabled in BIOS ?\n" ); +return ret; } }
Signature · 1 line
2.25.1