1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
11 #include <linux/hwmon.h>
13 #define SENSORS_PKT_TIMEOUT 1000000 /* 1s */
14 #define HWMON_NR_SENSOR_TYPES (hwmon_pwm + 1)
16 int hl_build_hwmon_channel_info(struct hl_device
*hdev
,
17 struct armcp_sensor
*sensors_arr
)
19 u32 counts
[HWMON_NR_SENSOR_TYPES
] = {0};
20 u32
*sensors_by_type
[HWMON_NR_SENSOR_TYPES
] = {NULL
};
21 u32 sensors_by_type_next_index
[HWMON_NR_SENSOR_TYPES
] = {0};
22 struct hwmon_channel_info
**channels_info
;
23 u32 num_sensors_for_type
, num_active_sensor_types
= 0,
24 arr_size
= 0, *curr_arr
;
25 enum hwmon_sensor_types type
;
28 for (i
= 0 ; i
< ARMCP_MAX_SENSORS
; i
++) {
29 type
= le32_to_cpu(sensors_arr
[i
].type
);
31 if ((type
== 0) && (sensors_arr
[i
].flags
== 0))
34 if (type
>= HWMON_NR_SENSOR_TYPES
) {
36 "Got wrong sensor type %d from device\n", type
);
44 for (i
= 0 ; i
< HWMON_NR_SENSOR_TYPES
; i
++) {
48 num_sensors_for_type
= counts
[i
] + 1;
49 curr_arr
= kcalloc(num_sensors_for_type
, sizeof(*curr_arr
),
53 goto sensors_type_err
;
56 num_active_sensor_types
++;
57 sensors_by_type
[i
] = curr_arr
;
60 for (i
= 0 ; i
< arr_size
; i
++) {
61 type
= le32_to_cpu(sensors_arr
[i
].type
);
62 curr_arr
= sensors_by_type
[type
];
63 curr_arr
[sensors_by_type_next_index
[type
]++] =
64 le32_to_cpu(sensors_arr
[i
].flags
);
67 channels_info
= kcalloc(num_active_sensor_types
+ 1,
68 sizeof(*channels_info
), GFP_KERNEL
);
71 goto channels_info_array_err
;
74 for (i
= 0 ; i
< num_active_sensor_types
; i
++) {
75 channels_info
[i
] = kzalloc(sizeof(*channels_info
[i
]),
77 if (!channels_info
[i
]) {
79 goto channel_info_err
;
83 for (i
= 0, j
= 0 ; i
< HWMON_NR_SENSOR_TYPES
; i
++) {
84 if (!sensors_by_type
[i
])
87 channels_info
[j
]->type
= i
;
88 channels_info
[j
]->config
= sensors_by_type
[i
];
92 hdev
->hl_chip_info
->info
=
93 (const struct hwmon_channel_info
**)channels_info
;
98 for (i
= 0 ; i
< num_active_sensor_types
; i
++)
99 if (channels_info
[i
]) {
100 kfree(channels_info
[i
]->config
);
101 kfree(channels_info
[i
]);
103 kfree(channels_info
);
104 channels_info_array_err
:
106 for (i
= 0 ; i
< HWMON_NR_SENSOR_TYPES
; i
++)
107 kfree(sensors_by_type
[i
]);
112 static int hl_read(struct device
*dev
, enum hwmon_sensor_types type
,
113 u32 attr
, int channel
, long *val
)
115 struct hl_device
*hdev
= dev_get_drvdata(dev
);
117 if (hl_device_disabled_or_in_reset(hdev
))
123 case hwmon_temp_input
:
125 case hwmon_temp_crit
:
126 case hwmon_temp_max_hyst
:
127 case hwmon_temp_crit_hyst
:
133 *val
= hl_get_temperature(hdev
, channel
, attr
);
145 *val
= hl_get_voltage(hdev
, channel
, attr
);
149 case hwmon_curr_input
:
157 *val
= hl_get_current(hdev
, channel
, attr
);
161 case hwmon_fan_input
:
168 *val
= hl_get_fan_speed(hdev
, channel
, attr
);
172 case hwmon_pwm_input
:
173 case hwmon_pwm_enable
:
178 *val
= hl_get_pwm_info(hdev
, channel
, attr
);
186 static int hl_write(struct device
*dev
, enum hwmon_sensor_types type
,
187 u32 attr
, int channel
, long val
)
189 struct hl_device
*hdev
= dev_get_drvdata(dev
);
191 if (hl_device_disabled_or_in_reset(hdev
))
197 case hwmon_pwm_input
:
198 case hwmon_pwm_enable
:
203 hl_set_pwm_info(hdev
, channel
, attr
, val
);
211 static umode_t
hl_is_visible(const void *data
, enum hwmon_sensor_types type
,
212 u32 attr
, int channel
)
217 case hwmon_temp_input
:
219 case hwmon_temp_max_hyst
:
220 case hwmon_temp_crit
:
221 case hwmon_temp_crit_hyst
:
235 case hwmon_curr_input
:
243 case hwmon_fan_input
:
251 case hwmon_pwm_input
:
252 case hwmon_pwm_enable
:
262 static const struct hwmon_ops hl_hwmon_ops
= {
263 .is_visible
= hl_is_visible
,
268 long hl_get_temperature(struct hl_device
*hdev
, int sensor_index
, u32 attr
)
270 struct armcp_packet pkt
;
274 memset(&pkt
, 0, sizeof(pkt
));
276 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_TEMPERATURE_GET
<<
277 ARMCP_PKT_CTL_OPCODE_SHIFT
);
278 pkt
.sensor_index
= __cpu_to_le16(sensor_index
);
279 pkt
.type
= __cpu_to_le16(attr
);
281 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
282 SENSORS_PKT_TIMEOUT
, &result
);
286 "Failed to get temperature from sensor %d, error %d\n",
294 long hl_get_voltage(struct hl_device
*hdev
, int sensor_index
, u32 attr
)
296 struct armcp_packet pkt
;
300 memset(&pkt
, 0, sizeof(pkt
));
302 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_VOLTAGE_GET
<<
303 ARMCP_PKT_CTL_OPCODE_SHIFT
);
304 pkt
.sensor_index
= __cpu_to_le16(sensor_index
);
305 pkt
.type
= __cpu_to_le16(attr
);
307 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
308 SENSORS_PKT_TIMEOUT
, &result
);
312 "Failed to get voltage from sensor %d, error %d\n",
320 long hl_get_current(struct hl_device
*hdev
, int sensor_index
, u32 attr
)
322 struct armcp_packet pkt
;
326 memset(&pkt
, 0, sizeof(pkt
));
328 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_CURRENT_GET
<<
329 ARMCP_PKT_CTL_OPCODE_SHIFT
);
330 pkt
.sensor_index
= __cpu_to_le16(sensor_index
);
331 pkt
.type
= __cpu_to_le16(attr
);
333 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
334 SENSORS_PKT_TIMEOUT
, &result
);
338 "Failed to get current from sensor %d, error %d\n",
346 long hl_get_fan_speed(struct hl_device
*hdev
, int sensor_index
, u32 attr
)
348 struct armcp_packet pkt
;
352 memset(&pkt
, 0, sizeof(pkt
));
354 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_FAN_SPEED_GET
<<
355 ARMCP_PKT_CTL_OPCODE_SHIFT
);
356 pkt
.sensor_index
= __cpu_to_le16(sensor_index
);
357 pkt
.type
= __cpu_to_le16(attr
);
359 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
360 SENSORS_PKT_TIMEOUT
, &result
);
364 "Failed to get fan speed from sensor %d, error %d\n",
372 long hl_get_pwm_info(struct hl_device
*hdev
, int sensor_index
, u32 attr
)
374 struct armcp_packet pkt
;
378 memset(&pkt
, 0, sizeof(pkt
));
380 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_PWM_GET
<<
381 ARMCP_PKT_CTL_OPCODE_SHIFT
);
382 pkt
.sensor_index
= __cpu_to_le16(sensor_index
);
383 pkt
.type
= __cpu_to_le16(attr
);
385 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
386 SENSORS_PKT_TIMEOUT
, &result
);
390 "Failed to get pwm info from sensor %d, error %d\n",
398 void hl_set_pwm_info(struct hl_device
*hdev
, int sensor_index
, u32 attr
,
401 struct armcp_packet pkt
;
404 memset(&pkt
, 0, sizeof(pkt
));
406 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_PWM_SET
<<
407 ARMCP_PKT_CTL_OPCODE_SHIFT
);
408 pkt
.sensor_index
= __cpu_to_le16(sensor_index
);
409 pkt
.type
= __cpu_to_le16(attr
);
410 pkt
.value
= cpu_to_le64(value
);
412 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
413 SENSORS_PKT_TIMEOUT
, NULL
);
417 "Failed to set pwm info to sensor %d, error %d\n",
421 int hl_hwmon_init(struct hl_device
*hdev
)
423 struct device
*dev
= hdev
->pdev
? &hdev
->pdev
->dev
: hdev
->dev
;
424 struct asic_fixed_properties
*prop
= &hdev
->asic_prop
;
427 if ((hdev
->hwmon_initialized
) || !(hdev
->fw_loading
))
430 if (hdev
->hl_chip_info
->info
) {
431 hdev
->hl_chip_info
->ops
= &hl_hwmon_ops
;
433 hdev
->hwmon_dev
= hwmon_device_register_with_info(dev
,
434 prop
->armcp_info
.card_name
, hdev
,
435 hdev
->hl_chip_info
, NULL
);
436 if (IS_ERR(hdev
->hwmon_dev
)) {
437 rc
= PTR_ERR(hdev
->hwmon_dev
);
439 "Unable to register hwmon device: %d\n", rc
);
443 dev_info(hdev
->dev
, "%s: add sensors information\n",
444 dev_name(hdev
->hwmon_dev
));
446 hdev
->hwmon_initialized
= true;
448 dev_info(hdev
->dev
, "no available sensors\n");
454 void hl_hwmon_fini(struct hl_device
*hdev
)
456 if (!hdev
->hwmon_initialized
)
459 hwmon_device_unregister(hdev
->hwmon_dev
);