1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * AMD HSMP Platform Driver
4 * Copyright (c) 2024, AMD.
5 * All Rights Reserved.
6 *
7 * Header file for HSMP driver
8 */
9
10 #ifndef HSMP_H
11 #define HSMP_H
12
13 #include <linux/compiler_types.h>
14 #include <linux/device.h>
15 #include <linux/hwmon.h>
16 #include <linux/kconfig.h>
17 #include <linux/miscdevice.h>
18 #include <linux/mutex.h>
19 #include <linux/pci.h>
20 #include <linux/rwsem.h>
21 #include <linux/semaphore.h>
22 #include <linux/sysfs.h>
23 #include <linux/types.h>
24
25 #define HSMP_METRICS_TABLE_NAME "metrics_bin"
26
27 #define HSMP_ATTR_GRP_NAME_SIZE 10
28
29 #define HSMP_CDEV_NAME "hsmp_cdev"
30 #define HSMP_DEVNODE_NAME "hsmp"
31 #define ACPI_HSMP_DEVICE_HID "AMDI0097"
32
33 #define DRIVER_VERSION "2.6"
34
35 struct hsmp_mbaddr_info {
36 u32 base_addr;
37 u32 msg_id_off;
38 u32 msg_resp_off;
39 u32 msg_arg_off;
40 u32 size;
41 };
42
43 struct hsmp_socket {
44 struct bin_attribute hsmp_attr;
45 struct hsmp_mbaddr_info mbinfo;
46 void __iomem *metric_tbl_addr;
47 /* Size of the region mapped at @metric_tbl_addr, as reported by SMU */
48 size_t metric_tbl_size;
49 void __iomem *virt_base_addr;
50 struct semaphore hsmp_sem;
51 /* Serializes HSMP_GET_METRIC_TABLE fill-and-copy for this socket */
52 struct mutex metric_read_lock;
53 char name[HSMP_ATTR_GRP_NAME_SIZE];
54 struct device *dev;
55 u16 sock_ind;
56 int (*amd_hsmp_rdwr)(struct hsmp_socket *sock, u32 off, u32 *val, bool rw);
57 };
58
59 struct hsmp_plat_device {
60 struct miscdevice mdev;
61 struct hsmp_socket *sock;
62 u32 proto_ver;
63 u16 num_sockets;
64 };
65
66 int hsmp_cache_proto_ver(u16 sock_ind);
67 int hsmp_test(u16 sock_ind, u32 value);
68 long hsmp_ioctl(struct file *fp, unsigned int cmd, unsigned long arg);
69 void hsmp_misc_deregister(void);
70 int hsmp_misc_register(struct device *dev);
71 int hsmp_get_tbl_dram_base(u16 sock_ind);
72 void hsmp_unmap_metric_tbls(struct hsmp_plat_device *pdev);
73 void hsmp_init_metric_read_locks(struct hsmp_plat_device *pdev);
74 void hsmp_destroy_metric_read_locks(struct hsmp_plat_device *pdev);
75 ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size);
76 struct hsmp_plat_device *get_hsmp_pdev(void);
77 #if IS_ENABLED(CONFIG_HWMON)
78 int hsmp_create_sensor(struct device *dev, u16 sock_ind);
79 #else
hsmp_create_sensor(struct device * dev,u16 sock_ind)80 static inline int hsmp_create_sensor(struct device *dev, u16 sock_ind) { return 0; }
81 #endif
82 int hsmp_msg_get_nargs(u16 sock_ind, u32 msg_id, u32 *data, u8 num_args);
83
84 /*
85 * Gates the HSMP data plane: hsmp_send_message() takes it for read; probe and
86 * remove take it for write to bring sockets up and tear them down.
87 */
88 extern struct rw_semaphore hsmp_sock_rwsem;
89 #endif /* HSMP_H */
90