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 HWMON_NR_SENSOR_TYPES (hwmon_pwm + 1)
15 int hl_build_hwmon_channel_info(struct hl_device *hdev,
16 struct cpucp_sensor *sensors_arr)
18 u32 counts[HWMON_NR_SENSOR_TYPES] = {0};
19 u32 *sensors_by_type[HWMON_NR_SENSOR_TYPES] = {NULL};
20 u32 sensors_by_type_next_index[HWMON_NR_SENSOR_TYPES] = {0};
21 struct hwmon_channel_info **channels_info;
22 u32 num_sensors_for_type, num_active_sensor_types = 0,
23 arr_size = 0, *curr_arr;
24 enum hwmon_sensor_types type;
27 for (i = 0 ; i < CPUCP_MAX_SENSORS ; i++) {
28 type = le32_to_cpu(sensors_arr[i].type);
30 if ((type == 0) && (sensors_arr[i].flags == 0))
33 if (type >= HWMON_NR_SENSOR_TYPES) {
35 "Got wrong sensor type %d from device\n", type);
43 for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
47 num_sensors_for_type = counts[i] + 1;
48 curr_arr = kcalloc(num_sensors_for_type, sizeof(*curr_arr),
52 goto sensors_type_err;
55 num_active_sensor_types++;
56 sensors_by_type[i] = curr_arr;
59 for (i = 0 ; i < arr_size ; i++) {
60 type = le32_to_cpu(sensors_arr[i].type);
61 curr_arr = sensors_by_type[type];
62 curr_arr[sensors_by_type_next_index[type]++] =
63 le32_to_cpu(sensors_arr[i].flags);
66 channels_info = kcalloc(num_active_sensor_types + 1,
67 sizeof(*channels_info), GFP_KERNEL);
70 goto channels_info_array_err;
73 for (i = 0 ; i < num_active_sensor_types ; i++) {
74 channels_info[i] = kzalloc(sizeof(*channels_info[i]),
76 if (!channels_info[i]) {
78 goto channel_info_err;
82 for (i = 0, j = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
83 if (!sensors_by_type[i])
86 channels_info[j]->type = i;
87 channels_info[j]->config = sensors_by_type[i];
91 hdev->hl_chip_info->info =
92 (const struct hwmon_channel_info **)channels_info;
97 for (i = 0 ; i < num_active_sensor_types ; i++)
98 if (channels_info[i]) {
99 kfree(channels_info[i]->config);
100 kfree(channels_info[i]);
102 kfree(channels_info);
103 channels_info_array_err:
105 for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++)
106 kfree(sensors_by_type[i]);
111 static int hl_read(struct device *dev, enum hwmon_sensor_types type,
112 u32 attr, int channel, long *val)
114 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:
128 case hwmon_temp_offset:
129 case hwmon_temp_highest:
135 rc = hl_get_temperature(hdev, channel, attr, val);
142 case hwmon_in_highest:
148 rc = hl_get_voltage(hdev, channel, attr, val);
152 case hwmon_curr_input:
155 case hwmon_curr_highest:
161 rc = hl_get_current(hdev, channel, attr, val);
165 case hwmon_fan_input:
172 rc = hl_get_fan_speed(hdev, channel, attr, val);
176 case hwmon_pwm_input:
177 case hwmon_pwm_enable:
182 rc = hl_get_pwm_info(hdev, channel, attr, val);
190 static int hl_write(struct device *dev, enum hwmon_sensor_types type,
191 u32 attr, int channel, long val)
193 struct hl_device *hdev = dev_get_drvdata(dev);
195 if (hl_device_disabled_or_in_reset(hdev))
201 case hwmon_temp_offset:
202 case hwmon_temp_reset_history:
207 hl_set_temperature(hdev, channel, attr, val);
211 case hwmon_pwm_input:
212 case hwmon_pwm_enable:
217 hl_set_pwm_info(hdev, channel, attr, val);
221 case hwmon_in_reset_history:
226 hl_set_voltage(hdev, channel, attr, val);
230 case hwmon_curr_reset_history:
235 hl_set_current(hdev, channel, attr, val);
243 static umode_t hl_is_visible(const void *data, enum hwmon_sensor_types type,
244 u32 attr, int channel)
249 case hwmon_temp_input:
251 case hwmon_temp_max_hyst:
252 case hwmon_temp_crit:
253 case hwmon_temp_crit_hyst:
254 case hwmon_temp_highest:
256 case hwmon_temp_offset:
258 case hwmon_temp_reset_history:
267 case hwmon_in_highest:
269 case hwmon_in_reset_history:
275 case hwmon_curr_input:
278 case hwmon_curr_highest:
280 case hwmon_curr_reset_history:
286 case hwmon_fan_input:
294 case hwmon_pwm_input:
295 case hwmon_pwm_enable:
305 static const struct hwmon_ops hl_hwmon_ops = {
306 .is_visible = hl_is_visible,
311 int hl_get_temperature(struct hl_device *hdev,
312 int sensor_index, u32 attr, long *value)
314 struct cpucp_packet pkt;
317 memset(&pkt, 0, sizeof(pkt));
319 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_GET <<
320 CPUCP_PKT_CTL_OPCODE_SHIFT);
321 pkt.sensor_index = __cpu_to_le16(sensor_index);
322 pkt.type = __cpu_to_le16(attr);
324 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
329 "Failed to get temperature from sensor %d, error %d\n",
337 int hl_set_temperature(struct hl_device *hdev,
338 int sensor_index, u32 attr, long value)
340 struct cpucp_packet pkt;
343 memset(&pkt, 0, sizeof(pkt));
345 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_SET <<
346 CPUCP_PKT_CTL_OPCODE_SHIFT);
347 pkt.sensor_index = __cpu_to_le16(sensor_index);
348 pkt.type = __cpu_to_le16(attr);
349 pkt.value = __cpu_to_le64(value);
351 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
356 "Failed to set temperature of sensor %d, error %d\n",
362 int hl_get_voltage(struct hl_device *hdev,
363 int sensor_index, u32 attr, long *value)
365 struct cpucp_packet pkt;
368 memset(&pkt, 0, sizeof(pkt));
370 pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_GET <<
371 CPUCP_PKT_CTL_OPCODE_SHIFT);
372 pkt.sensor_index = __cpu_to_le16(sensor_index);
373 pkt.type = __cpu_to_le16(attr);
375 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
380 "Failed to get voltage from sensor %d, error %d\n",
388 int hl_get_current(struct hl_device *hdev,
389 int sensor_index, u32 attr, long *value)
391 struct cpucp_packet pkt;
394 memset(&pkt, 0, sizeof(pkt));
396 pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_GET <<
397 CPUCP_PKT_CTL_OPCODE_SHIFT);
398 pkt.sensor_index = __cpu_to_le16(sensor_index);
399 pkt.type = __cpu_to_le16(attr);
401 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
406 "Failed to get current from sensor %d, error %d\n",
414 int hl_get_fan_speed(struct hl_device *hdev,
415 int sensor_index, u32 attr, long *value)
417 struct cpucp_packet pkt;
420 memset(&pkt, 0, sizeof(pkt));
422 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FAN_SPEED_GET <<
423 CPUCP_PKT_CTL_OPCODE_SHIFT);
424 pkt.sensor_index = __cpu_to_le16(sensor_index);
425 pkt.type = __cpu_to_le16(attr);
427 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
432 "Failed to get fan speed from sensor %d, error %d\n",
440 int hl_get_pwm_info(struct hl_device *hdev,
441 int sensor_index, u32 attr, long *value)
443 struct cpucp_packet pkt;
446 memset(&pkt, 0, sizeof(pkt));
448 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_GET <<
449 CPUCP_PKT_CTL_OPCODE_SHIFT);
450 pkt.sensor_index = __cpu_to_le16(sensor_index);
451 pkt.type = __cpu_to_le16(attr);
453 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
458 "Failed to get pwm info from sensor %d, error %d\n",
466 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
469 struct cpucp_packet pkt;
472 memset(&pkt, 0, sizeof(pkt));
474 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_SET <<
475 CPUCP_PKT_CTL_OPCODE_SHIFT);
476 pkt.sensor_index = __cpu_to_le16(sensor_index);
477 pkt.type = __cpu_to_le16(attr);
478 pkt.value = cpu_to_le64(value);
480 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
485 "Failed to set pwm info to sensor %d, error %d\n",
489 int hl_set_voltage(struct hl_device *hdev,
490 int sensor_index, u32 attr, long value)
492 struct cpucp_packet pkt;
495 memset(&pkt, 0, sizeof(pkt));
497 pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_SET <<
498 CPUCP_PKT_CTL_OPCODE_SHIFT);
499 pkt.sensor_index = __cpu_to_le16(sensor_index);
500 pkt.type = __cpu_to_le16(attr);
501 pkt.value = __cpu_to_le64(value);
503 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
508 "Failed to set voltage of sensor %d, error %d\n",
514 int hl_set_current(struct hl_device *hdev,
515 int sensor_index, u32 attr, long value)
517 struct cpucp_packet pkt;
520 memset(&pkt, 0, sizeof(pkt));
522 pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_SET <<
523 CPUCP_PKT_CTL_OPCODE_SHIFT);
524 pkt.sensor_index = __cpu_to_le16(sensor_index);
525 pkt.type = __cpu_to_le16(attr);
526 pkt.value = __cpu_to_le64(value);
528 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
533 "Failed to set current of sensor %d, error %d\n",
539 int hl_hwmon_init(struct hl_device *hdev)
541 struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev;
542 struct asic_fixed_properties *prop = &hdev->asic_prop;
545 if ((hdev->hwmon_initialized) || !(hdev->fw_loading))
548 if (hdev->hl_chip_info->info) {
549 hdev->hl_chip_info->ops = &hl_hwmon_ops;
551 hdev->hwmon_dev = hwmon_device_register_with_info(dev,
552 prop->cpucp_info.card_name, hdev,
553 hdev->hl_chip_info, NULL);
554 if (IS_ERR(hdev->hwmon_dev)) {
555 rc = PTR_ERR(hdev->hwmon_dev);
557 "Unable to register hwmon device: %d\n", rc);
561 dev_info(hdev->dev, "%s: add sensors information\n",
562 dev_name(hdev->hwmon_dev));
564 hdev->hwmon_initialized = true;
566 dev_info(hdev->dev, "no available sensors\n");
572 void hl_hwmon_fini(struct hl_device *hdev)
574 if (!hdev->hwmon_initialized)
577 hwmon_device_unregister(hdev->hwmon_dev);