Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <suma.hegde@amd.com>
Reviewed-by: Naveen Krishna Chatradhi <nchatrad@amd.com>
Link: https://lore.kernel.org/r/20231010120310.3464066-2-suma.hegde@amd.com
[ij: lseek -> lseek(), dram -> DRAM in dev_err()]
[ij: added period to terminate a documentation sentence]
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

authored by

Suma Hegde and committed by
Ilpo Järvinen
5150542b 01d2adcf

+299 -2
+18
Documentation/arch/x86/amd_hsmp.rst
··· 41 41 * Locking across callers is taken care by the driver. 42 42 43 43 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 + 44 62 An example 45 63 ========== 46 64
+109
arch/x86/include/uapi/asm/amd_hsmp.h
··· 47 47 HSMP_SET_PCI_RATE, /* 20h Control link rate on PCIe devices */ 48 48 HSMP_SET_POWER_MODE, /* 21h Select power efficiency profile policy */ 49 49 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 */ 50 53 HSMP_MSG_ID_MAX, 51 54 }; 52 55 ··· 65 62 HSMP_RSVD = -1, 66 63 HSMP_SET = 0, 67 64 HSMP_GET = 1, 65 + }; 66 + 67 + enum hsmp_proto_versions { 68 + HSMP_PROTO_VER2 = 2, 69 + HSMP_PROTO_VER3, 70 + HSMP_PROTO_VER4, 71 + HSMP_PROTO_VER5, 72 + HSMP_PROTO_VER6 68 73 }; 69 74 70 75 struct hsmp_msg_desc { ··· 306 295 * input: args[0] = min df pstate[15:8] + max df pstate[7:0] 307 296 */ 308 297 {1, 0, HSMP_SET}, 298 + 299 + /* 300 + * HSMP_GET_METRIC_TABLE_VER, num_args = 0, response_sz = 1 301 + * output: args[0] = metrics table version 302 + */ 303 + {0, 1, HSMP_GET}, 304 + 305 + /* 306 + * HSMP_GET_METRIC_TABLE, num_args = 0, response_sz = 0 307 + */ 308 + {0, 0, HSMP_GET}, 309 + 310 + /* 311 + * HSMP_GET_METRIC_TABLE_DRAM_ADDR, num_args = 0, response_sz = 2 312 + * output: args[0] = lower 32 bits of the address 313 + * output: args[1] = upper 32 bits of the address 314 + */ 315 + {0, 2, HSMP_GET}, 316 + }; 317 + 318 + /* Metrics table (supported only with proto version 6) */ 319 + struct hsmp_metric_table { 320 + __u32 accumulation_counter; 321 + 322 + /* TEMPERATURE */ 323 + __u32 max_socket_temperature; 324 + __u32 max_vr_temperature; 325 + __u32 max_hbm_temperature; 326 + __u64 max_socket_temperature_acc; 327 + __u64 max_vr_temperature_acc; 328 + __u64 max_hbm_temperature_acc; 329 + 330 + /* POWER */ 331 + __u32 socket_power_limit; 332 + __u32 max_socket_power_limit; 333 + __u32 socket_power; 334 + 335 + /* ENERGY */ 336 + __u64 timestamp; 337 + __u64 socket_energy_acc; 338 + __u64 ccd_energy_acc; 339 + __u64 xcd_energy_acc; 340 + __u64 aid_energy_acc; 341 + __u64 hbm_energy_acc; 342 + 343 + /* FREQUENCY */ 344 + __u32 cclk_frequency_limit; 345 + __u32 gfxclk_frequency_limit; 346 + __u32 fclk_frequency; 347 + __u32 uclk_frequency; 348 + __u32 socclk_frequency[4]; 349 + __u32 vclk_frequency[4]; 350 + __u32 dclk_frequency[4]; 351 + __u32 lclk_frequency[4]; 352 + __u64 gfxclk_frequency_acc[8]; 353 + __u64 cclk_frequency_acc[96]; 354 + 355 + /* FREQUENCY RANGE */ 356 + __u32 max_cclk_frequency; 357 + __u32 min_cclk_frequency; 358 + __u32 max_gfxclk_frequency; 359 + __u32 min_gfxclk_frequency; 360 + __u32 fclk_frequency_table[4]; 361 + __u32 uclk_frequency_table[4]; 362 + __u32 socclk_frequency_table[4]; 363 + __u32 vclk_frequency_table[4]; 364 + __u32 dclk_frequency_table[4]; 365 + __u32 lclk_frequency_table[4]; 366 + __u32 max_lclk_dpm_range; 367 + __u32 min_lclk_dpm_range; 368 + 369 + /* XGMI */ 370 + __u32 xgmi_width; 371 + __u32 xgmi_bitrate; 372 + __u64 xgmi_read_bandwidth_acc[8]; 373 + __u64 xgmi_write_bandwidth_acc[8]; 374 + 375 + /* ACTIVITY */ 376 + __u32 socket_c0_residency; 377 + __u32 socket_gfx_busy; 378 + __u32 dram_bandwidth_utilization; 379 + __u64 socket_c0_residency_acc; 380 + __u64 socket_gfx_busy_acc; 381 + __u64 dram_bandwidth_acc; 382 + __u32 max_dram_bandwidth; 383 + __u64 dram_bandwidth_utilization_acc; 384 + __u64 pcie_bandwidth_acc[4]; 385 + 386 + /* THROTTLERS */ 387 + __u32 prochot_residency_acc; 388 + __u32 ppt_residency_acc; 389 + __u32 socket_thm_residency_acc; 390 + __u32 vr_thm_residency_acc; 391 + __u32 hbm_thm_residency_acc; 392 + __u32 spare; 393 + 394 + /* New items at the end to maintain driver compatibility */ 395 + __u32 gfxclk_frequency[8]; 309 396 }; 310 397 311 398 /* Reset to default packing */
+172 -2
drivers/platform/x86/amd/hsmp.c
··· 20 20 #include <linux/semaphore.h> 21 21 22 22 #define DRIVER_NAME "amd_hsmp" 23 - #define DRIVER_VERSION "1.0" 23 + #define DRIVER_VERSION "2.0" 24 24 25 25 /* HSMP Status / Error codes */ 26 26 #define HSMP_STATUS_NOT_READY 0x00 ··· 49 49 50 50 #define HSMP_CDEV_NAME "hsmp_cdev" 51 51 #define HSMP_DEVNODE_NAME "hsmp" 52 + #define HSMP_METRICS_TABLE_NAME "metrics_bin" 53 + 54 + #define HSMP_ATTR_GRP_NAME_SIZE 10 52 55 53 56 struct hsmp_socket { 57 + struct bin_attribute hsmp_attr; 58 + void __iomem *metric_tbl_addr; 54 59 struct semaphore hsmp_sem; 60 + char name[HSMP_ATTR_GRP_NAME_SIZE]; 55 61 u16 sock_ind; 56 62 }; 57 63 ··· 65 59 struct miscdevice hsmp_device; 66 60 struct hsmp_socket *sock; 67 61 struct device *dev; 62 + u32 proto_ver; 68 63 u16 num_sockets; 69 64 }; 70 65 ··· 337 330 .compat_ioctl = hsmp_ioctl, 338 331 }; 339 332 333 + static ssize_t hsmp_metric_tbl_read(struct file *filp, struct kobject *kobj, 334 + struct bin_attribute *bin_attr, char *buf, 335 + loff_t off, size_t count) 336 + { 337 + struct hsmp_socket *sock = bin_attr->private; 338 + struct hsmp_message msg = { 0 }; 339 + int ret; 340 + 341 + /* Do not support lseek(), reads entire metric table */ 342 + if (count < bin_attr->size) { 343 + dev_err(plat_dev.dev, "Wrong buffer size\n"); 344 + return -EINVAL; 345 + } 346 + 347 + if (!sock) { 348 + dev_err(plat_dev.dev, "Failed to read attribute private data\n"); 349 + return -EINVAL; 350 + } 351 + 352 + msg.msg_id = HSMP_GET_METRIC_TABLE; 353 + msg.sock_ind = sock->sock_ind; 354 + 355 + ret = hsmp_send_message(&msg); 356 + if (ret) 357 + return ret; 358 + memcpy(buf, sock->metric_tbl_addr, bin_attr->size); 359 + 360 + return bin_attr->size; 361 + } 362 + 363 + static int hsmp_get_tbl_dram_base(u16 sock_ind) 364 + { 365 + struct hsmp_socket *sock = &plat_dev.sock[sock_ind]; 366 + struct hsmp_message msg = { 0 }; 367 + phys_addr_t dram_addr; 368 + int ret; 369 + 370 + msg.sock_ind = sock_ind; 371 + msg.response_sz = hsmp_msg_desc_table[HSMP_GET_METRIC_TABLE_DRAM_ADDR].response_sz; 372 + msg.msg_id = HSMP_GET_METRIC_TABLE_DRAM_ADDR; 373 + 374 + ret = hsmp_send_message(&msg); 375 + if (ret) 376 + return ret; 377 + 378 + /* 379 + * calculate the metric table DRAM address from lower and upper 32 bits 380 + * sent from SMU and ioremap it to virtual address. 381 + */ 382 + dram_addr = msg.args[0] | ((u64)(msg.args[1]) << 32); 383 + if (!dram_addr) { 384 + dev_err(plat_dev.dev, "Invalid DRAM address for metric table\n"); 385 + return -ENOMEM; 386 + } 387 + sock->metric_tbl_addr = devm_ioremap(plat_dev.dev, dram_addr, 388 + sizeof(struct hsmp_metric_table)); 389 + if (!sock->metric_tbl_addr) { 390 + dev_err(plat_dev.dev, "Failed to ioremap metric table addr\n"); 391 + return -ENOMEM; 392 + } 393 + return 0; 394 + } 395 + 396 + static umode_t hsmp_is_sock_attr_visible(struct kobject *kobj, 397 + struct bin_attribute *battr, int id) 398 + { 399 + if (plat_dev.proto_ver == HSMP_PROTO_VER6) 400 + return battr->attr.mode; 401 + else 402 + return 0; 403 + } 404 + 405 + static int hsmp_init_metric_tbl_bin_attr(struct bin_attribute **hattrs, u16 sock_ind) 406 + { 407 + struct bin_attribute *hattr = &plat_dev.sock[sock_ind].hsmp_attr; 408 + 409 + sysfs_bin_attr_init(hattr); 410 + hattr->attr.name = HSMP_METRICS_TABLE_NAME; 411 + hattr->attr.mode = 0444; 412 + hattr->read = hsmp_metric_tbl_read; 413 + hattr->size = sizeof(struct hsmp_metric_table); 414 + hattr->private = &plat_dev.sock[sock_ind]; 415 + hattrs[0] = hattr; 416 + 417 + if (plat_dev.proto_ver == HSMP_PROTO_VER6) 418 + return (hsmp_get_tbl_dram_base(sock_ind)); 419 + else 420 + return 0; 421 + } 422 + 423 + /* One bin sysfs for metrics table*/ 424 + #define NUM_HSMP_ATTRS 1 425 + 426 + static int hsmp_create_sysfs_interface(void) 427 + { 428 + const struct attribute_group **hsmp_attr_grps; 429 + struct bin_attribute **hsmp_bin_attrs; 430 + struct attribute_group *attr_grp; 431 + int ret; 432 + u16 i; 433 + 434 + /* String formatting is currently limited to u8 sockets */ 435 + if (WARN_ON(plat_dev.num_sockets > U8_MAX)) 436 + return -ERANGE; 437 + 438 + hsmp_attr_grps = devm_kzalloc(plat_dev.dev, sizeof(struct attribute_group *) * 439 + (plat_dev.num_sockets + 1), GFP_KERNEL); 440 + if (!hsmp_attr_grps) 441 + return -ENOMEM; 442 + 443 + /* Create a sysfs directory for each socket */ 444 + for (i = 0; i < plat_dev.num_sockets; i++) { 445 + attr_grp = devm_kzalloc(plat_dev.dev, sizeof(struct attribute_group), GFP_KERNEL); 446 + if (!attr_grp) 447 + return -ENOMEM; 448 + 449 + snprintf(plat_dev.sock[i].name, HSMP_ATTR_GRP_NAME_SIZE, "socket%u", (u8)i); 450 + attr_grp->name = plat_dev.sock[i].name; 451 + 452 + /* Null terminated list of attributes */ 453 + hsmp_bin_attrs = devm_kzalloc(plat_dev.dev, sizeof(struct bin_attribute *) * 454 + (NUM_HSMP_ATTRS + 1), GFP_KERNEL); 455 + if (!hsmp_bin_attrs) 456 + return -ENOMEM; 457 + 458 + attr_grp->bin_attrs = hsmp_bin_attrs; 459 + attr_grp->is_bin_visible = hsmp_is_sock_attr_visible; 460 + hsmp_attr_grps[i] = attr_grp; 461 + 462 + /* Now create the leaf nodes */ 463 + ret = hsmp_init_metric_tbl_bin_attr(hsmp_bin_attrs, i); 464 + if (ret) 465 + return ret; 466 + } 467 + return devm_device_add_groups(plat_dev.dev, hsmp_attr_grps); 468 + } 469 + 470 + static int hsmp_cache_proto_ver(void) 471 + { 472 + struct hsmp_message msg = { 0 }; 473 + int ret; 474 + 475 + msg.msg_id = HSMP_GET_PROTO_VER; 476 + msg.sock_ind = 0; 477 + msg.response_sz = hsmp_msg_desc_table[HSMP_GET_PROTO_VER].response_sz; 478 + 479 + ret = hsmp_send_message(&msg); 480 + if (!ret) 481 + plat_dev.proto_ver = msg.args[0]; 482 + 483 + return ret; 484 + } 485 + 340 486 static int hsmp_pltdrv_probe(struct platform_device *pdev) 341 487 { 342 - int i; 488 + int ret, i; 343 489 344 490 plat_dev.sock = devm_kzalloc(&pdev->dev, 345 491 (plat_dev.num_sockets * sizeof(struct hsmp_socket)), ··· 512 352 plat_dev.hsmp_device.parent = &pdev->dev; 513 353 plat_dev.hsmp_device.nodename = HSMP_DEVNODE_NAME; 514 354 plat_dev.hsmp_device.mode = 0644; 355 + 356 + ret = hsmp_cache_proto_ver(); 357 + if (ret) { 358 + dev_err(plat_dev.dev, "Failed to read HSMP protocol version\n"); 359 + return ret; 360 + } 361 + 362 + ret = hsmp_create_sysfs_interface(); 363 + if (ret) 364 + dev_err(plat_dev.dev, "Failed to create HSMP sysfs interface\n"); 515 365 516 366 return misc_register(&plat_dev.hsmp_device); 517 367 }