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_operational(hdev, NULL))
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_operational(hdev, NULL))
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;
318 memset(&pkt, 0, sizeof(pkt));
320 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_GET <<
321 CPUCP_PKT_CTL_OPCODE_SHIFT);
322 pkt.sensor_index = __cpu_to_le16(sensor_index);
323 pkt.type = __cpu_to_le16(attr);
325 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
328 *value = (long) result;
332 "Failed to get temperature from sensor %d, error %d\n",
340 int hl_set_temperature(struct hl_device *hdev,
341 int sensor_index, u32 attr, long value)
343 struct cpucp_packet pkt;
346 memset(&pkt, 0, sizeof(pkt));
348 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_SET <<
349 CPUCP_PKT_CTL_OPCODE_SHIFT);
350 pkt.sensor_index = __cpu_to_le16(sensor_index);
351 pkt.type = __cpu_to_le16(attr);
352 pkt.value = __cpu_to_le64(value);
354 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
359 "Failed to set temperature of sensor %d, error %d\n",
365 int hl_get_voltage(struct hl_device *hdev,
366 int sensor_index, u32 attr, long *value)
368 struct cpucp_packet pkt;
372 memset(&pkt, 0, sizeof(pkt));
374 pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_GET <<
375 CPUCP_PKT_CTL_OPCODE_SHIFT);
376 pkt.sensor_index = __cpu_to_le16(sensor_index);
377 pkt.type = __cpu_to_le16(attr);
379 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
382 *value = (long) result;
386 "Failed to get voltage from sensor %d, error %d\n",
394 int hl_get_current(struct hl_device *hdev,
395 int sensor_index, u32 attr, long *value)
397 struct cpucp_packet pkt;
401 memset(&pkt, 0, sizeof(pkt));
403 pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_GET <<
404 CPUCP_PKT_CTL_OPCODE_SHIFT);
405 pkt.sensor_index = __cpu_to_le16(sensor_index);
406 pkt.type = __cpu_to_le16(attr);
408 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
411 *value = (long) result;
415 "Failed to get current from sensor %d, error %d\n",
423 int hl_get_fan_speed(struct hl_device *hdev,
424 int sensor_index, u32 attr, long *value)
426 struct cpucp_packet pkt;
430 memset(&pkt, 0, sizeof(pkt));
432 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FAN_SPEED_GET <<
433 CPUCP_PKT_CTL_OPCODE_SHIFT);
434 pkt.sensor_index = __cpu_to_le16(sensor_index);
435 pkt.type = __cpu_to_le16(attr);
437 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
440 *value = (long) result;
444 "Failed to get fan speed from sensor %d, error %d\n",
452 int hl_get_pwm_info(struct hl_device *hdev,
453 int sensor_index, u32 attr, long *value)
455 struct cpucp_packet pkt;
459 memset(&pkt, 0, sizeof(pkt));
461 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_GET <<
462 CPUCP_PKT_CTL_OPCODE_SHIFT);
463 pkt.sensor_index = __cpu_to_le16(sensor_index);
464 pkt.type = __cpu_to_le16(attr);
466 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
469 *value = (long) result;
473 "Failed to get pwm info from sensor %d, error %d\n",
481 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
484 struct cpucp_packet pkt;
487 memset(&pkt, 0, sizeof(pkt));
489 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_SET <<
490 CPUCP_PKT_CTL_OPCODE_SHIFT);
491 pkt.sensor_index = __cpu_to_le16(sensor_index);
492 pkt.type = __cpu_to_le16(attr);
493 pkt.value = cpu_to_le64(value);
495 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
500 "Failed to set pwm info to sensor %d, error %d\n",
504 int hl_set_voltage(struct hl_device *hdev,
505 int sensor_index, u32 attr, long value)
507 struct cpucp_packet pkt;
510 memset(&pkt, 0, sizeof(pkt));
512 pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_SET <<
513 CPUCP_PKT_CTL_OPCODE_SHIFT);
514 pkt.sensor_index = __cpu_to_le16(sensor_index);
515 pkt.type = __cpu_to_le16(attr);
516 pkt.value = __cpu_to_le64(value);
518 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
523 "Failed to set voltage of sensor %d, error %d\n",
529 int hl_set_current(struct hl_device *hdev,
530 int sensor_index, u32 attr, long value)
532 struct cpucp_packet pkt;
535 memset(&pkt, 0, sizeof(pkt));
537 pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_SET <<
538 CPUCP_PKT_CTL_OPCODE_SHIFT);
539 pkt.sensor_index = __cpu_to_le16(sensor_index);
540 pkt.type = __cpu_to_le16(attr);
541 pkt.value = __cpu_to_le64(value);
543 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
548 "Failed to set current of sensor %d, error %d\n",
554 int hl_hwmon_init(struct hl_device *hdev)
556 struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev;
557 struct asic_fixed_properties *prop = &hdev->asic_prop;
560 if ((hdev->hwmon_initialized) || !(hdev->cpu_queues_enable))
563 if (hdev->hl_chip_info->info) {
564 hdev->hl_chip_info->ops = &hl_hwmon_ops;
566 hdev->hwmon_dev = hwmon_device_register_with_info(dev,
567 prop->cpucp_info.card_name, hdev,
568 hdev->hl_chip_info, NULL);
569 if (IS_ERR(hdev->hwmon_dev)) {
570 rc = PTR_ERR(hdev->hwmon_dev);
572 "Unable to register hwmon device: %d\n", rc);
576 dev_info(hdev->dev, "%s: add sensors information\n",
577 dev_name(hdev->hwmon_dev));
579 hdev->hwmon_initialized = true;
581 dev_info(hdev->dev, "no available sensors\n");
587 void hl_hwmon_fini(struct hl_device *hdev)
589 if (!hdev->hwmon_initialized)
592 hwmon_device_unregister(hdev->hwmon_dev);