Skip to content

Commit 5150542

Browse files
sumachidanandij-intel
authored andcommitted
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. Signed-off-by: Suma Hegde <[email protected]> Reviewed-by: Naveen Krishna Chatradhi <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ij: lseek -> lseek(), dram -> DRAM in dev_err()] [ij: added period to terminate a documentation sentence] Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: Ilpo Järvinen <[email protected]>
1 parent 01d2adc commit 5150542

File tree

3 files changed

+299
-2
lines changed

3 files changed

+299
-2
lines changed

Documentation/arch/x86/amd_hsmp.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ In-kernel integration:
4141
* Locking across callers is taken care by the driver.
4242

4343

44+
HSMP sysfs interface
45+
====================
46+
47+
1. Metrics table binary sysfs
48+
49+
AMD MI300A MCM provides GET_METRICS_TABLE message to retrieve
50+
most of the system management information from SMU in one go.
51+
52+
The metrics table is made available as hexadecimal sysfs binary file
53+
under per socket sysfs directory created at
54+
/sys/devices/platform/amd_hsmp/socket%d/metrics_bin
55+
56+
Note: lseek() is not supported as entire metrics table is read.
57+
58+
Metrics table definitions will be documented as part of Public PPR.
59+
The same is defined in the amd_hsmp.h header.
60+
61+
4462
An example
4563
==========
4664

arch/x86/include/uapi/asm/amd_hsmp.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ enum hsmp_message_ids {
4747
HSMP_SET_PCI_RATE, /* 20h Control link rate on PCIe devices */
4848
HSMP_SET_POWER_MODE, /* 21h Select power efficiency profile policy */
4949
HSMP_SET_PSTATE_MAX_MIN, /* 22h Set the max and min DF P-State */
50+
HSMP_GET_METRIC_TABLE_VER, /* 23h Get metrics table version */
51+
HSMP_GET_METRIC_TABLE, /* 24h Get metrics table */
52+
HSMP_GET_METRIC_TABLE_DRAM_ADDR,/* 25h Get metrics table dram address */
5053
HSMP_MSG_ID_MAX,
5154
};
5255

@@ -64,6 +67,14 @@ enum hsmp_msg_type {
6467
HSMP_GET = 1,
6568
};
6669

70+
enum hsmp_proto_versions {
71+
HSMP_PROTO_VER2 = 2,
72+
HSMP_PROTO_VER3,
73+
HSMP_PROTO_VER4,
74+
HSMP_PROTO_VER5,
75+
HSMP_PROTO_VER6
76+
};
77+
6778
struct hsmp_msg_desc {
6879
int num_args;
6980
int response_sz;
@@ -295,6 +306,104 @@ static const struct hsmp_msg_desc hsmp_msg_desc_table[] = {
295306
* input: args[0] = min df pstate[15:8] + max df pstate[7:0]
296307
*/
297308
{1, 0, HSMP_SET},
309+
310+
/*
311+
* HSMP_GET_METRIC_TABLE_VER, num_args = 0, response_sz = 1
312+
* output: args[0] = metrics table version
313+
*/
314+
{0, 1, HSMP_GET},
315+
316+
/*
317+
* HSMP_GET_METRIC_TABLE, num_args = 0, response_sz = 0
318+
*/
319+
{0, 0, HSMP_GET},
320+
321+
/*
322+
* HSMP_GET_METRIC_TABLE_DRAM_ADDR, num_args = 0, response_sz = 2
323+
* output: args[0] = lower 32 bits of the address
324+
* output: args[1] = upper 32 bits of the address
325+
*/
326+
{0, 2, HSMP_GET},
327+
};
328+
329+
/* Metrics table (supported only with proto version 6) */
330+
struct hsmp_metric_table {
331+
__u32 accumulation_counter;
332+
333+
/* TEMPERATURE */
334+
__u32 max_socket_temperature;
335+
__u32 max_vr_temperature;
336+
__u32 max_hbm_temperature;
337+
__u64 max_socket_temperature_acc;
338+
__u64 max_vr_temperature_acc;
339+
__u64 max_hbm_temperature_acc;
340+
341+
/* POWER */
342+
__u32 socket_power_limit;
343+
__u32 max_socket_power_limit;
344+
__u32 socket_power;
345+
346+
/* ENERGY */
347+
__u64 timestamp;
348+
__u64 socket_energy_acc;
349+
__u64 ccd_energy_acc;
350+
__u64 xcd_energy_acc;
351+
__u64 aid_energy_acc;
352+
__u64 hbm_energy_acc;
353+
354+
/* FREQUENCY */
355+
__u32 cclk_frequency_limit;
356+
__u32 gfxclk_frequency_limit;
357+
__u32 fclk_frequency;
358+
__u32 uclk_frequency;
359+
__u32 socclk_frequency[4];
360+
__u32 vclk_frequency[4];
361+
__u32 dclk_frequency[4];
362+
__u32 lclk_frequency[4];
363+
__u64 gfxclk_frequency_acc[8];
364+
__u64 cclk_frequency_acc[96];
365+
366+
/* FREQUENCY RANGE */
367+
__u32 max_cclk_frequency;
368+
__u32 min_cclk_frequency;
369+
__u32 max_gfxclk_frequency;
370+
__u32 min_gfxclk_frequency;
371+
__u32 fclk_frequency_table[4];
372+
__u32 uclk_frequency_table[4];
373+
__u32 socclk_frequency_table[4];
374+
__u32 vclk_frequency_table[4];
375+
__u32 dclk_frequency_table[4];
376+
__u32 lclk_frequency_table[4];
377+
__u32 max_lclk_dpm_range;
378+
__u32 min_lclk_dpm_range;
379+
380+
/* XGMI */
381+
__u32 xgmi_width;
382+
__u32 xgmi_bitrate;
383+
__u64 xgmi_read_bandwidth_acc[8];
384+
__u64 xgmi_write_bandwidth_acc[8];
385+
386+
/* ACTIVITY */
387+
__u32 socket_c0_residency;
388+
__u32 socket_gfx_busy;
389+
__u32 dram_bandwidth_utilization;
390+
__u64 socket_c0_residency_acc;
391+
__u64 socket_gfx_busy_acc;
392+
__u64 dram_bandwidth_acc;
393+
__u32 max_dram_bandwidth;
394+
__u64 dram_bandwidth_utilization_acc;
395+
__u64 pcie_bandwidth_acc[4];
396+
397+
/* THROTTLERS */
398+
__u32 prochot_residency_acc;
399+
__u32 ppt_residency_acc;
400+
__u32 socket_thm_residency_acc;
401+
__u32 vr_thm_residency_acc;
402+
__u32 hbm_thm_residency_acc;
403+
__u32 spare;
404+
405+
/* New items at the end to maintain driver compatibility */
406+
__u32 gfxclk_frequency[8];
298407
};
299408

300409
/* Reset to default packing */

drivers/platform/x86/amd/hsmp.c

Lines changed: 172 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <linux/semaphore.h>
2121

2222
#define DRIVER_NAME "amd_hsmp"
23-
#define DRIVER_VERSION "1.0"
23+
#define DRIVER_VERSION "2.0"
2424

2525
/* HSMP Status / Error codes */
2626
#define HSMP_STATUS_NOT_READY 0x00
@@ -49,16 +49,23 @@
4949

5050
#define HSMP_CDEV_NAME "hsmp_cdev"
5151
#define HSMP_DEVNODE_NAME "hsmp"
52+
#define HSMP_METRICS_TABLE_NAME "metrics_bin"
53+
54+
#define HSMP_ATTR_GRP_NAME_SIZE 10
5255

5356
struct hsmp_socket {
57+
struct bin_attribute hsmp_attr;
58+
void __iomem *metric_tbl_addr;
5459
struct semaphore hsmp_sem;
60+
char name[HSMP_ATTR_GRP_NAME_SIZE];
5561
u16 sock_ind;
5662
};
5763

5864
struct hsmp_plat_device {
5965
struct miscdevice hsmp_device;
6066
struct hsmp_socket *sock;
6167
struct device *dev;
68+
u32 proto_ver;
6269
u16 num_sockets;
6370
};
6471

@@ -330,9 +337,162 @@ static const struct file_operations hsmp_fops = {
330337
.compat_ioctl = hsmp_ioctl,
331338
};
332339

340+
static ssize_t hsmp_metric_tbl_read(struct file *filp, struct kobject *kobj,
341+
struct bin_attribute *bin_attr, char *buf,
342+
loff_t off, size_t count)
343+
{
344+
struct hsmp_socket *sock = bin_attr->private;
345+
struct hsmp_message msg = { 0 };
346+
int ret;
347+
348+
/* Do not support lseek(), reads entire metric table */
349+
if (count < bin_attr->size) {
350+
dev_err(plat_dev.dev, "Wrong buffer size\n");
351+
return -EINVAL;
352+
}
353+
354+
if (!sock) {
355+
dev_err(plat_dev.dev, "Failed to read attribute private data\n");
356+
return -EINVAL;
357+
}
358+
359+
msg.msg_id = HSMP_GET_METRIC_TABLE;
360+
msg.sock_ind = sock->sock_ind;
361+
362+
ret = hsmp_send_message(&msg);
363+
if (ret)
364+
return ret;
365+
memcpy(buf, sock->metric_tbl_addr, bin_attr->size);
366+
367+
return bin_attr->size;
368+
}
369+
370+
static int hsmp_get_tbl_dram_base(u16 sock_ind)
371+
{
372+
struct hsmp_socket *sock = &plat_dev.sock[sock_ind];
373+
struct hsmp_message msg = { 0 };
374+
phys_addr_t dram_addr;
375+
int ret;
376+
377+
msg.sock_ind = sock_ind;
378+
msg.response_sz = hsmp_msg_desc_table[HSMP_GET_METRIC_TABLE_DRAM_ADDR].response_sz;
379+
msg.msg_id = HSMP_GET_METRIC_TABLE_DRAM_ADDR;
380+
381+
ret = hsmp_send_message(&msg);
382+
if (ret)
383+
return ret;
384+
385+
/*
386+
* calculate the metric table DRAM address from lower and upper 32 bits
387+
* sent from SMU and ioremap it to virtual address.
388+
*/
389+
dram_addr = msg.args[0] | ((u64)(msg.args[1]) << 32);
390+
if (!dram_addr) {
391+
dev_err(plat_dev.dev, "Invalid DRAM address for metric table\n");
392+
return -ENOMEM;
393+
}
394+
sock->metric_tbl_addr = devm_ioremap(plat_dev.dev, dram_addr,
395+
sizeof(struct hsmp_metric_table));
396+
if (!sock->metric_tbl_addr) {
397+
dev_err(plat_dev.dev, "Failed to ioremap metric table addr\n");
398+
return -ENOMEM;
399+
}
400+
return 0;
401+
}
402+
403+
static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj,
404+
struct bin_attribute *battr, int id)
405+
{
406+
if (plat_dev.proto_ver == HSMP_PROTO_VER6)
407+
return battr->attr.mode;
408+
else
409+
return 0;
410+
}
411+
412+
static int hsmp_init_metric_tbl_bin_attr(struct bin_attribute **hattrs, u16 sock_ind)
413+
{
414+
struct bin_attribute *hattr = &plat_dev.sock[sock_ind].hsmp_attr;
415+
416+
sysfs_bin_attr_init(hattr);
417+
hattr->attr.name = HSMP_METRICS_TABLE_NAME;
418+
hattr->attr.mode = 0444;
419+
hattr->read = hsmp_metric_tbl_read;
420+
hattr->size = sizeof(struct hsmp_metric_table);
421+
hattr->private = &plat_dev.sock[sock_ind];
422+
hattrs[0] = hattr;
423+
424+
if (plat_dev.proto_ver == HSMP_PROTO_VER6)
425+
return (hsmp_get_tbl_dram_base(sock_ind));
426+
else
427+
return 0;
428+
}
429+
430+
/* One bin sysfs for metrics table*/
431+
#define NUM_HSMP_ATTRS 1
432+
433+
static int hsmp_create_sysfs_interface(void)
434+
{
435+
const struct attribute_group **hsmp_attr_grps;
436+
struct bin_attribute **hsmp_bin_attrs;
437+
struct attribute_group *attr_grp;
438+
int ret;
439+
u16 i;
440+
441+
/* String formatting is currently limited to u8 sockets */
442+
if (WARN_ON(plat_dev.num_sockets > U8_MAX))
443+
return -ERANGE;
444+
445+
hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct attribute_group *) *
446+
(plat_dev.num_sockets + 1), GFP_KERNEL);
447+
if (!hsmp_attr_grps)
448+
return -ENOMEM;
449+
450+
/* Create a sysfs directory for each socket */
451+
for (i = 0; i < plat_dev.num_sockets; i++) {
452+
attr_grp = devm_kzalloc(plat_dev.dev, sizeof(struct attribute_group), GFP_KERNEL);
453+
if (!attr_grp)
454+
return -ENOMEM;
455+
456+
snprintf(plat_dev.sock[i].name, HSMP_ATTR_GRP_NAME_SIZE, "socket%u", (u8)i);
457+
attr_grp->name = plat_dev.sock[i].name;
458+
459+
/* Null terminated list of attributes */
460+
hsmp_bin_attrs = devm_kzalloc(plat_dev.dev, sizeof(struct bin_attribute *) *
461+
(NUM_HSMP_ATTRS + 1), GFP_KERNEL);
462+
if (!hsmp_bin_attrs)
463+
return -ENOMEM;
464+
465+
attr_grp->bin_attrs = hsmp_bin_attrs;
466+
attr_grp->is_bin_visible = hsmp_is_sock_attr_visible;
467+
hsmp_attr_grps[i] = attr_grp;
468+
469+
/* Now create the leaf nodes */
470+
ret = hsmp_init_metric_tbl_bin_attr(hsmp_bin_attrs, i);
471+
if (ret)
472+
return ret;
473+
}
474+
return devm_device_add_groups(plat_dev.dev, hsmp_attr_grps);
475+
}
476+
477+
static int hsmp_cache_proto_ver(void)
478+
{
479+
struct hsmp_message msg = { 0 };
480+
int ret;
481+
482+
msg.msg_id = HSMP_GET_PROTO_VER;
483+
msg.sock_ind = 0;
484+
msg.response_sz = hsmp_msg_desc_table[HSMP_GET_PROTO_VER].response_sz;
485+
486+
ret = hsmp_send_message(&msg);
487+
if (!ret)
488+
plat_dev.proto_ver = msg.args[0];
489+
490+
return ret;
491+
}
492+
333493
static int hsmp_pltdrv_probe(struct platform_device *pdev)
334494
{
335-
int i;
495+
int ret, i;
336496

337497
plat_dev.sock = devm_kzalloc(&pdev->dev,
338498
(plat_dev.num_sockets * sizeof(struct hsmp_socket)),
@@ -353,6 +513,16 @@ static int hsmp_pltdrv_probe(struct platform_device *pdev)
353513
plat_dev.hsmp_device.nodename = HSMP_DEVNODE_NAME;
354514
plat_dev.hsmp_device.mode = 0644;
355515

516+
ret = hsmp_cache_proto_ver();
517+
if (ret) {
518+
dev_err(plat_dev.dev, "Failed to read HSMP protocol version\n");
519+
return ret;
520+
}
521+
522+
ret = hsmp_create_sysfs_interface();
523+
if (ret)
524+
dev_err(plat_dev.dev, "Failed to create HSMP sysfs interface\n");
525+
356526
return misc_register(&plat_dev.hsmp_device);
357527
}
358528

0 commit comments

Comments
 (0)