-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhwmon.c
More file actions
142 lines (122 loc) · 3.14 KB
/
hwmon.c
File metadata and controls
142 lines (122 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: GPL-2.0
/*
* AMD HSMP hwmon support
* Copyright (c) 2025, AMD.
* All Rights Reserved.
*
* This file provides hwmon implementation for HSMP interface.
*/
#include <linux/device.h>
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/types.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
#include <linux/units.h>
#endif
#include "hsmp.h"
#include "amd_hsmp.h"
#define HSMP_HWMON_NAME "amd_hsmp_hwmon"
static int hsmp_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{
u16 sock_ind = (uintptr_t)dev_get_drvdata(dev);
struct hsmp_message msg = {};
if (type != hwmon_power)
return -EOPNOTSUPP;
if (attr != hwmon_power_cap)
return -EOPNOTSUPP;
msg.num_args = 1;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
msg.args[0] = val / MICROWATT_PER_MILLIWATT;
#else
msg.args[0] = val / 1000L;
#endif
msg.msg_id = HSMP_SET_SOCKET_POWER_LIMIT;
msg.sock_ind = sock_ind;
return hsmp_send_message(&msg);
}
static int hsmp_hwmon_read(struct device *dev,
enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
u16 sock_ind = (uintptr_t)dev_get_drvdata(dev);
struct hsmp_message msg = {};
int ret;
if (type != hwmon_power)
return -EOPNOTSUPP;
msg.sock_ind = sock_ind;
msg.response_sz = 1;
switch (attr) {
case hwmon_power_input:
msg.msg_id = HSMP_GET_SOCKET_POWER;
break;
case hwmon_power_cap:
msg.msg_id = HSMP_GET_SOCKET_POWER_LIMIT;
break;
case hwmon_power_cap_max:
msg.msg_id = HSMP_GET_SOCKET_POWER_LIMIT_MAX;
break;
default:
return -EOPNOTSUPP;
}
ret = hsmp_send_message(&msg);
if (!ret)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
*val = msg.args[0] * MICROWATT_PER_MILLIWATT;
#else
*val = msg.args[0] * 1000L;
#endif
return ret;
}
static umode_t hsmp_hwmon_is_visble(const void *data,
enum hwmon_sensor_types type,
u32 attr, int channel)
{
if (type != hwmon_power)
return 0;
switch (attr) {
case hwmon_power_input:
return 0444;
case hwmon_power_cap:
return 0644;
case hwmon_power_cap_max:
return 0444;
default:
return 0;
}
}
static const struct hwmon_ops hsmp_hwmon_ops = {
.read = hsmp_hwmon_read,
.is_visible = hsmp_hwmon_is_visble,
.write = hsmp_hwmon_write,
};
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
static const struct hwmon_channel_info * const hsmp_info[] = {
HWMON_CHANNEL_INFO(power, HWMON_P_INPUT | HWMON_P_CAP | HWMON_P_CAP_MAX),
NULL
};
#else
static const struct hwmon_channel_info *hsmp_info[] = {
HWMON_CHANNEL_INFO(power, HWMON_P_INPUT | HWMON_P_CAP | HWMON_P_CAP_MAX),
NULL
};
#endif
static const struct hwmon_chip_info hsmp_chip_info = {
.ops = &hsmp_hwmon_ops,
.info = hsmp_info,
};
int hsmp_create_sensor(struct device *dev, u16 sock_ind)
{
struct device *hwmon_dev;
hwmon_dev = devm_hwmon_device_register_with_info(dev, HSMP_HWMON_NAME,
(void *)(uintptr_t)sock_ind,
&hsmp_chip_info,
NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0)
EXPORT_SYMBOL_NS(hsmp_create_sensor, "AMD_HSMP");
#else
EXPORT_SYMBOL_NS(hsmp_create_sensor, AMD_HSMP);
#endif