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_max)
15 #ifdef _HAS_HWMON_HWMON_T_ENABLE
17 static u32 fixup_flags_legacy_fw(struct hl_device *hdev, enum hwmon_sensor_types type,
24 flags = (cpucp_flags << 1) | HWMON_T_ENABLE;
28 flags = (cpucp_flags << 1) | HWMON_I_ENABLE;
32 flags = (cpucp_flags << 1) | HWMON_C_ENABLE;
36 flags = (cpucp_flags << 1) | HWMON_F_ENABLE;
40 flags = (cpucp_flags << 1) | HWMON_P_ENABLE;
44 /* enable bit was here from day 1, so no need to adjust */
49 dev_err(hdev->dev, "unsupported h/w sensor type %d\n", type);
57 static u32 fixup_attr_legacy_fw(u32 attr)
64 static u32 fixup_flags_legacy_fw(struct hl_device *hdev, enum hwmon_sensor_types type,
70 static u32 fixup_attr_legacy_fw(u32 attr)
75 #endif /* !_HAS_HWMON_HWMON_T_ENABLE */
77 static u32 adjust_hwmon_flags(struct hl_device *hdev, enum hwmon_sensor_types type, u32 cpucp_flags)
79 u32 flags, cpucp_input_val;
82 use_cpucp_enum = (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
83 CPU_BOOT_DEV_STS0_MAP_HWMON_EN) ? true : false;
85 /* If f/w is using it's own enum, we need to check if the properties values are aligned.
86 * If not, it means we need to adjust the values to the new format that is used in the
87 * kernel since 5.6 (enum values were incremented by 1 by adding a new enable value).
92 cpucp_input_val = cpucp_temp_input;
93 if (cpucp_input_val == hwmon_temp_input)
96 flags = (cpucp_flags << 1) | HWMON_T_ENABLE;
100 cpucp_input_val = cpucp_in_input;
101 if (cpucp_input_val == hwmon_in_input)
104 flags = (cpucp_flags << 1) | HWMON_I_ENABLE;
108 cpucp_input_val = cpucp_curr_input;
109 if (cpucp_input_val == hwmon_curr_input)
112 flags = (cpucp_flags << 1) | HWMON_C_ENABLE;
116 cpucp_input_val = cpucp_fan_input;
117 if (cpucp_input_val == hwmon_fan_input)
120 flags = (cpucp_flags << 1) | HWMON_F_ENABLE;
124 /* enable bit was here from day 1, so no need to adjust */
129 cpucp_input_val = CPUCP_POWER_INPUT;
130 if (cpucp_input_val == hwmon_power_input)
133 flags = (cpucp_flags << 1) | HWMON_P_ENABLE;
137 dev_err(hdev->dev, "unsupported h/w sensor type %d\n", type);
142 flags = fixup_flags_legacy_fw(hdev, type, cpucp_flags);
148 int hl_build_hwmon_channel_info(struct hl_device *hdev, struct cpucp_sensor *sensors_arr)
150 u32 num_sensors_for_type, flags, num_active_sensor_types = 0, arr_size = 0, *curr_arr;
151 u32 sensors_by_type_next_index[HWMON_NR_SENSOR_TYPES] = {0};
152 u32 *sensors_by_type[HWMON_NR_SENSOR_TYPES] = {NULL};
153 struct hwmon_channel_info **channels_info;
154 u32 counts[HWMON_NR_SENSOR_TYPES] = {0};
155 enum hwmon_sensor_types type;
158 for (i = 0 ; i < CPUCP_MAX_SENSORS ; i++) {
159 type = le32_to_cpu(sensors_arr[i].type);
161 if ((type == 0) && (sensors_arr[i].flags == 0))
164 if (type >= HWMON_NR_SENSOR_TYPES) {
165 dev_err(hdev->dev, "Got wrong sensor type %d from device\n", type);
173 for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
177 num_sensors_for_type = counts[i] + 1;
178 dev_dbg(hdev->dev, "num_sensors_for_type %d = %d\n", i, num_sensors_for_type);
180 curr_arr = kcalloc(num_sensors_for_type, sizeof(*curr_arr), GFP_KERNEL);
183 goto sensors_type_err;
186 num_active_sensor_types++;
187 sensors_by_type[i] = curr_arr;
190 for (i = 0 ; i < arr_size ; i++) {
191 type = le32_to_cpu(sensors_arr[i].type);
192 curr_arr = sensors_by_type[type];
193 flags = adjust_hwmon_flags(hdev, type, le32_to_cpu(sensors_arr[i].flags));
194 curr_arr[sensors_by_type_next_index[type]++] = flags;
197 channels_info = kcalloc(num_active_sensor_types + 1, sizeof(struct hwmon_channel_info *),
199 if (!channels_info) {
201 goto channels_info_array_err;
204 for (i = 0 ; i < num_active_sensor_types ; i++) {
205 channels_info[i] = kzalloc(sizeof(*channels_info[i]), GFP_KERNEL);
206 if (!channels_info[i]) {
208 goto channel_info_err;
212 for (i = 0, j = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
213 if (!sensors_by_type[i])
216 channels_info[j]->type = i;
217 channels_info[j]->config = sensors_by_type[i];
221 hdev->hl_chip_info->info = (const struct hwmon_channel_info **)channels_info;
226 for (i = 0 ; i < num_active_sensor_types ; i++) {
227 if (channels_info[i]) {
228 kfree(channels_info[i]->config);
229 kfree(channels_info[i]);
232 kfree(channels_info);
234 channels_info_array_err:
236 for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++)
237 kfree(sensors_by_type[i]);
242 static int hl_read(struct device *dev, enum hwmon_sensor_types type,
243 u32 attr, int channel, long *val)
245 struct hl_device *hdev = dev_get_drvdata(dev);
250 if (!hl_device_operational(hdev, NULL))
253 use_cpucp_enum = (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
254 CPU_BOOT_DEV_STS0_MAP_HWMON_EN) ? true : false;
259 case hwmon_temp_input:
260 cpucp_attr = cpucp_temp_input;
263 cpucp_attr = cpucp_temp_max;
265 case hwmon_temp_crit:
266 cpucp_attr = cpucp_temp_crit;
268 case hwmon_temp_max_hyst:
269 cpucp_attr = cpucp_temp_max_hyst;
271 case hwmon_temp_crit_hyst:
272 cpucp_attr = cpucp_temp_crit_hyst;
274 case hwmon_temp_offset:
275 cpucp_attr = cpucp_temp_offset;
277 case hwmon_temp_highest:
278 cpucp_attr = cpucp_temp_highest;
285 rc = hl_get_temperature(hdev, channel, cpucp_attr, val);
287 rc = hl_get_temperature(hdev, channel, fixup_attr_legacy_fw(attr), val);
292 cpucp_attr = cpucp_in_input;
295 cpucp_attr = cpucp_in_min;
298 cpucp_attr = cpucp_in_max;
300 case hwmon_in_highest:
301 cpucp_attr = cpucp_in_highest;
308 rc = hl_get_voltage(hdev, channel, cpucp_attr, val);
310 rc = hl_get_voltage(hdev, channel, fixup_attr_legacy_fw(attr), val);
314 case hwmon_curr_input:
315 cpucp_attr = cpucp_curr_input;
318 cpucp_attr = cpucp_curr_min;
321 cpucp_attr = cpucp_curr_max;
323 case hwmon_curr_highest:
324 cpucp_attr = cpucp_curr_highest;
331 rc = hl_get_current(hdev, channel, cpucp_attr, val);
333 rc = hl_get_current(hdev, channel, fixup_attr_legacy_fw(attr), val);
337 case hwmon_fan_input:
338 cpucp_attr = cpucp_fan_input;
341 cpucp_attr = cpucp_fan_min;
344 cpucp_attr = cpucp_fan_max;
351 rc = hl_get_fan_speed(hdev, channel, cpucp_attr, val);
353 rc = hl_get_fan_speed(hdev, channel, fixup_attr_legacy_fw(attr), val);
357 case hwmon_pwm_input:
358 cpucp_attr = cpucp_pwm_input;
360 case hwmon_pwm_enable:
361 cpucp_attr = cpucp_pwm_enable;
368 rc = hl_get_pwm_info(hdev, channel, cpucp_attr, val);
370 /* no need for fixup as pwm was aligned from day 1 */
371 rc = hl_get_pwm_info(hdev, channel, attr, val);
375 case hwmon_power_input:
376 cpucp_attr = CPUCP_POWER_INPUT;
378 case hwmon_power_input_highest:
379 cpucp_attr = CPUCP_POWER_INPUT_HIGHEST;
386 rc = hl_get_power(hdev, channel, cpucp_attr, val);
388 rc = hl_get_power(hdev, channel, fixup_attr_legacy_fw(attr), val);
396 static int hl_write(struct device *dev, enum hwmon_sensor_types type,
397 u32 attr, int channel, long val)
399 struct hl_device *hdev = dev_get_drvdata(dev);
401 bool use_cpucp_enum = (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
402 CPU_BOOT_DEV_STS0_MAP_HWMON_EN) ? true : false;
404 if (!hl_device_operational(hdev, NULL))
410 case hwmon_temp_offset:
411 cpucp_attr = cpucp_temp_offset;
413 case hwmon_temp_reset_history:
414 cpucp_attr = cpucp_temp_reset_history;
421 hl_set_temperature(hdev, channel, cpucp_attr, val);
423 hl_set_temperature(hdev, channel, fixup_attr_legacy_fw(attr), val);
427 case hwmon_pwm_input:
428 cpucp_attr = cpucp_pwm_input;
430 case hwmon_pwm_enable:
431 cpucp_attr = cpucp_pwm_enable;
438 hl_set_pwm_info(hdev, channel, cpucp_attr, val);
440 /* no need for fixup as pwm was aligned from day 1 */
441 hl_set_pwm_info(hdev, channel, attr, val);
445 case hwmon_in_reset_history:
446 cpucp_attr = cpucp_in_reset_history;
453 hl_set_voltage(hdev, channel, cpucp_attr, val);
455 hl_set_voltage(hdev, channel, fixup_attr_legacy_fw(attr), val);
459 case hwmon_curr_reset_history:
460 cpucp_attr = cpucp_curr_reset_history;
467 hl_set_current(hdev, channel, cpucp_attr, val);
469 hl_set_current(hdev, channel, fixup_attr_legacy_fw(attr), val);
473 case hwmon_power_reset_history:
474 cpucp_attr = CPUCP_POWER_RESET_INPUT_HISTORY;
481 hl_set_power(hdev, channel, cpucp_attr, val);
483 hl_set_power(hdev, channel, fixup_attr_legacy_fw(attr), val);
491 static umode_t hl_is_visible(const void *data, enum hwmon_sensor_types type,
492 u32 attr, int channel)
497 case hwmon_temp_input:
499 case hwmon_temp_max_hyst:
500 case hwmon_temp_crit:
501 case hwmon_temp_crit_hyst:
502 case hwmon_temp_highest:
504 case hwmon_temp_offset:
506 case hwmon_temp_reset_history:
515 case hwmon_in_highest:
517 case hwmon_in_reset_history:
523 case hwmon_curr_input:
526 case hwmon_curr_highest:
528 case hwmon_curr_reset_history:
534 case hwmon_fan_input:
542 case hwmon_pwm_input:
543 case hwmon_pwm_enable:
549 case hwmon_power_input:
550 case hwmon_power_input_highest:
552 case hwmon_power_reset_history:
562 static const struct hwmon_ops hl_hwmon_ops = {
563 .is_visible = hl_is_visible,
568 int hl_get_temperature(struct hl_device *hdev,
569 int sensor_index, u32 attr, long *value)
571 struct cpucp_packet pkt;
575 memset(&pkt, 0, sizeof(pkt));
577 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_GET <<
578 CPUCP_PKT_CTL_OPCODE_SHIFT);
579 pkt.sensor_index = __cpu_to_le16(sensor_index);
580 pkt.type = __cpu_to_le16(attr);
581 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
584 *value = (long) result;
588 "Failed to get temperature from sensor %d, error %d\n",
596 int hl_set_temperature(struct hl_device *hdev,
597 int sensor_index, u32 attr, long value)
599 struct cpucp_packet pkt;
602 memset(&pkt, 0, sizeof(pkt));
604 pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_SET <<
605 CPUCP_PKT_CTL_OPCODE_SHIFT);
606 pkt.sensor_index = __cpu_to_le16(sensor_index);
607 pkt.type = __cpu_to_le16(attr);
608 pkt.value = __cpu_to_le64(value);
610 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
615 "Failed to set temperature of sensor %d, error %d\n",
621 int hl_get_voltage(struct hl_device *hdev,
622 int sensor_index, u32 attr, long *value)
624 struct cpucp_packet pkt;
628 memset(&pkt, 0, sizeof(pkt));
630 pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_GET <<
631 CPUCP_PKT_CTL_OPCODE_SHIFT);
632 pkt.sensor_index = __cpu_to_le16(sensor_index);
633 pkt.type = __cpu_to_le16(attr);
635 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
638 *value = (long) result;
642 "Failed to get voltage from sensor %d, error %d\n",
650 int hl_get_current(struct hl_device *hdev,
651 int sensor_index, u32 attr, long *value)
653 struct cpucp_packet pkt;
657 memset(&pkt, 0, sizeof(pkt));
659 pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_GET <<
660 CPUCP_PKT_CTL_OPCODE_SHIFT);
661 pkt.sensor_index = __cpu_to_le16(sensor_index);
662 pkt.type = __cpu_to_le16(attr);
664 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
667 *value = (long) result;
671 "Failed to get current from sensor %d, error %d\n",
679 int hl_get_fan_speed(struct hl_device *hdev,
680 int sensor_index, u32 attr, long *value)
682 struct cpucp_packet pkt;
686 memset(&pkt, 0, sizeof(pkt));
688 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FAN_SPEED_GET <<
689 CPUCP_PKT_CTL_OPCODE_SHIFT);
690 pkt.sensor_index = __cpu_to_le16(sensor_index);
691 pkt.type = __cpu_to_le16(attr);
693 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
696 *value = (long) result;
700 "Failed to get fan speed from sensor %d, error %d\n",
708 int hl_get_pwm_info(struct hl_device *hdev,
709 int sensor_index, u32 attr, long *value)
711 struct cpucp_packet pkt;
715 memset(&pkt, 0, sizeof(pkt));
717 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_GET <<
718 CPUCP_PKT_CTL_OPCODE_SHIFT);
719 pkt.sensor_index = __cpu_to_le16(sensor_index);
720 pkt.type = __cpu_to_le16(attr);
722 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
725 *value = (long) result;
729 "Failed to get pwm info from sensor %d, error %d\n",
737 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
740 struct cpucp_packet pkt;
743 memset(&pkt, 0, sizeof(pkt));
745 pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_SET <<
746 CPUCP_PKT_CTL_OPCODE_SHIFT);
747 pkt.sensor_index = __cpu_to_le16(sensor_index);
748 pkt.type = __cpu_to_le16(attr);
749 pkt.value = cpu_to_le64(value);
751 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
756 "Failed to set pwm info to sensor %d, error %d\n",
760 int hl_set_voltage(struct hl_device *hdev,
761 int sensor_index, u32 attr, long value)
763 struct cpucp_packet pkt;
766 memset(&pkt, 0, sizeof(pkt));
768 pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_SET <<
769 CPUCP_PKT_CTL_OPCODE_SHIFT);
770 pkt.sensor_index = __cpu_to_le16(sensor_index);
771 pkt.type = __cpu_to_le16(attr);
772 pkt.value = __cpu_to_le64(value);
774 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
779 "Failed to set voltage of sensor %d, error %d\n",
785 int hl_set_current(struct hl_device *hdev,
786 int sensor_index, u32 attr, long value)
788 struct cpucp_packet pkt;
791 memset(&pkt, 0, sizeof(pkt));
793 pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_SET <<
794 CPUCP_PKT_CTL_OPCODE_SHIFT);
795 pkt.sensor_index = __cpu_to_le16(sensor_index);
796 pkt.type = __cpu_to_le16(attr);
797 pkt.value = __cpu_to_le64(value);
799 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
804 "Failed to set current of sensor %d, error %d\n",
810 int hl_set_power(struct hl_device *hdev,
811 int sensor_index, u32 attr, long value)
813 struct cpucp_packet pkt;
814 struct asic_fixed_properties *prop = &hdev->asic_prop;
817 memset(&pkt, 0, sizeof(pkt));
819 if (prop->use_get_power_for_reset_history)
820 pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
821 CPUCP_PKT_CTL_OPCODE_SHIFT);
823 pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_SET <<
824 CPUCP_PKT_CTL_OPCODE_SHIFT);
826 pkt.sensor_index = __cpu_to_le16(sensor_index);
827 pkt.type = __cpu_to_le16(attr);
828 pkt.value = __cpu_to_le64(value);
830 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
835 "Failed to set power of sensor %d, error %d\n",
841 int hl_get_power(struct hl_device *hdev,
842 int sensor_index, u32 attr, long *value)
844 struct cpucp_packet pkt;
848 memset(&pkt, 0, sizeof(pkt));
850 pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
851 CPUCP_PKT_CTL_OPCODE_SHIFT);
852 pkt.sensor_index = __cpu_to_le16(sensor_index);
853 pkt.type = __cpu_to_le16(attr);
855 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
858 *value = (long) result;
862 "Failed to get power of sensor %d, error %d\n",
870 int hl_hwmon_init(struct hl_device *hdev)
872 struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev;
873 struct asic_fixed_properties *prop = &hdev->asic_prop;
876 if ((hdev->hwmon_initialized) || !(hdev->cpu_queues_enable))
879 if (hdev->hl_chip_info->info) {
880 hdev->hl_chip_info->ops = &hl_hwmon_ops;
882 hdev->hwmon_dev = hwmon_device_register_with_info(dev,
883 prop->cpucp_info.card_name, hdev,
884 hdev->hl_chip_info, NULL);
885 if (IS_ERR(hdev->hwmon_dev)) {
886 rc = PTR_ERR(hdev->hwmon_dev);
888 "Unable to register hwmon device: %d\n", rc);
892 dev_info(hdev->dev, "%s: add sensors information\n",
893 dev_name(hdev->hwmon_dev));
895 hdev->hwmon_initialized = true;
897 dev_info(hdev->dev, "no available sensors\n");
903 void hl_hwmon_fini(struct hl_device *hdev)
905 if (!hdev->hwmon_initialized)
908 hwmon_device_unregister(hdev->hwmon_dev);
911 void hl_hwmon_release_resources(struct hl_device *hdev)
913 const struct hwmon_channel_info * const *channel_info_arr;
916 if (!hdev->hl_chip_info->info)
919 channel_info_arr = hdev->hl_chip_info->info;
921 while (channel_info_arr[i]) {
922 kfree(channel_info_arr[i]->config);
923 kfree(channel_info_arr[i]);
927 kfree(channel_info_arr);
929 hdev->hl_chip_info->info = NULL;