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);
118 if (hl_device_disabled_or_in_reset(hdev))
124 case hwmon_temp_input:
126 case hwmon_temp_crit:
127 case hwmon_temp_max_hyst:
128 case hwmon_temp_crit_hyst:
129 case hwmon_temp_offset:
130 case hwmon_temp_highest:
136 rc = hl_get_temperature(hdev, channel, attr, val);
143 case hwmon_in_highest:
149 rc = hl_get_voltage(hdev, channel, attr, val);
153 case hwmon_curr_input:
156 case hwmon_curr_highest:
162 rc = hl_get_current(hdev, channel, attr, val);
166 case hwmon_fan_input:
173 rc = hl_get_fan_speed(hdev, channel, attr, val);
177 case hwmon_pwm_input:
178 case hwmon_pwm_enable:
183 rc = hl_get_pwm_info(hdev, channel, attr, val);
191 static int hl_write(struct device *dev, enum hwmon_sensor_types type,
192 u32 attr, int channel, long val)
194 struct hl_device *hdev = dev_get_drvdata(dev);
196 if (hl_device_disabled_or_in_reset(hdev))
202 case hwmon_temp_offset:
203 case hwmon_temp_reset_history:
208 hl_set_temperature(hdev, channel, attr, val);
212 case hwmon_pwm_input:
213 case hwmon_pwm_enable:
218 hl_set_pwm_info(hdev, channel, attr, val);
222 case hwmon_in_reset_history:
227 hl_set_voltage(hdev, channel, attr, val);
231 case hwmon_curr_reset_history:
236 hl_set_current(hdev, channel, attr, val);
244 static umode_t hl_is_visible(const void *data, enum hwmon_sensor_types type,
245 u32 attr, int channel)
250 case hwmon_temp_input:
252 case hwmon_temp_max_hyst:
253 case hwmon_temp_crit:
254 case hwmon_temp_crit_hyst:
255 case hwmon_temp_highest:
257 case hwmon_temp_offset:
259 case hwmon_temp_reset_history:
268 case hwmon_in_highest:
270 case hwmon_in_reset_history:
276 case hwmon_curr_input:
279 case hwmon_curr_highest:
281 case hwmon_curr_reset_history:
287 case hwmon_fan_input:
295 case hwmon_pwm_input:
296 case hwmon_pwm_enable:
306 static const struct hwmon_ops hl_hwmon_ops = {
307 .is_visible = hl_is_visible,
312 int hl_get_temperature(struct hl_device *hdev,
313 int sensor_index, u32 attr, long *value)
315 struct armcp_packet pkt;
318 memset(&pkt, 0, sizeof(pkt));
320 pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEMPERATURE_GET <<
321 ARMCP_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),
326 SENSORS_PKT_TIMEOUT, value);
330 "Failed to get temperature from sensor %d, error %d\n",
338 int hl_set_temperature(struct hl_device *hdev,
339 int sensor_index, u32 attr, long value)
341 struct armcp_packet pkt;
344 memset(&pkt, 0, sizeof(pkt));
346 pkt.ctl = cpu_to_le32(ARMCP_PACKET_TEMPERATURE_SET <<
347 ARMCP_PKT_CTL_OPCODE_SHIFT);
348 pkt.sensor_index = __cpu_to_le16(sensor_index);
349 pkt.type = __cpu_to_le16(attr);
350 pkt.value = __cpu_to_le64(value);
352 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
353 SENSORS_PKT_TIMEOUT, NULL);
357 "Failed to set temperature of sensor %d, error %d\n",
363 int hl_get_voltage(struct hl_device *hdev,
364 int sensor_index, u32 attr, long *value)
366 struct armcp_packet pkt;
369 memset(&pkt, 0, sizeof(pkt));
371 pkt.ctl = cpu_to_le32(ARMCP_PACKET_VOLTAGE_GET <<
372 ARMCP_PKT_CTL_OPCODE_SHIFT);
373 pkt.sensor_index = __cpu_to_le16(sensor_index);
374 pkt.type = __cpu_to_le16(attr);
376 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
377 SENSORS_PKT_TIMEOUT, value);
381 "Failed to get voltage from sensor %d, error %d\n",
389 int hl_get_current(struct hl_device *hdev,
390 int sensor_index, u32 attr, long *value)
392 struct armcp_packet pkt;
395 memset(&pkt, 0, sizeof(pkt));
397 pkt.ctl = cpu_to_le32(ARMCP_PACKET_CURRENT_GET <<
398 ARMCP_PKT_CTL_OPCODE_SHIFT);
399 pkt.sensor_index = __cpu_to_le16(sensor_index);
400 pkt.type = __cpu_to_le16(attr);
402 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
403 SENSORS_PKT_TIMEOUT, value);
407 "Failed to get current from sensor %d, error %d\n",
415 int hl_get_fan_speed(struct hl_device *hdev,
416 int sensor_index, u32 attr, long *value)
418 struct armcp_packet pkt;
421 memset(&pkt, 0, sizeof(pkt));
423 pkt.ctl = cpu_to_le32(ARMCP_PACKET_FAN_SPEED_GET <<
424 ARMCP_PKT_CTL_OPCODE_SHIFT);
425 pkt.sensor_index = __cpu_to_le16(sensor_index);
426 pkt.type = __cpu_to_le16(attr);
428 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
429 SENSORS_PKT_TIMEOUT, value);
433 "Failed to get fan speed from sensor %d, error %d\n",
441 int hl_get_pwm_info(struct hl_device *hdev,
442 int sensor_index, u32 attr, long *value)
444 struct armcp_packet pkt;
447 memset(&pkt, 0, sizeof(pkt));
449 pkt.ctl = cpu_to_le32(ARMCP_PACKET_PWM_GET <<
450 ARMCP_PKT_CTL_OPCODE_SHIFT);
451 pkt.sensor_index = __cpu_to_le16(sensor_index);
452 pkt.type = __cpu_to_le16(attr);
454 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
455 SENSORS_PKT_TIMEOUT, value);
459 "Failed to get pwm info from sensor %d, error %d\n",
467 void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
470 struct armcp_packet pkt;
473 memset(&pkt, 0, sizeof(pkt));
475 pkt.ctl = cpu_to_le32(ARMCP_PACKET_PWM_SET <<
476 ARMCP_PKT_CTL_OPCODE_SHIFT);
477 pkt.sensor_index = __cpu_to_le16(sensor_index);
478 pkt.type = __cpu_to_le16(attr);
479 pkt.value = cpu_to_le64(value);
481 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
482 SENSORS_PKT_TIMEOUT, NULL);
486 "Failed to set pwm info to sensor %d, error %d\n",
490 int hl_set_voltage(struct hl_device *hdev,
491 int sensor_index, u32 attr, long value)
493 struct armcp_packet pkt;
496 memset(&pkt, 0, sizeof(pkt));
498 pkt.ctl = cpu_to_le32(ARMCP_PACKET_VOLTAGE_SET <<
499 ARMCP_PKT_CTL_OPCODE_SHIFT);
500 pkt.sensor_index = __cpu_to_le16(sensor_index);
501 pkt.type = __cpu_to_le16(attr);
502 pkt.value = __cpu_to_le64(value);
504 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
505 SENSORS_PKT_TIMEOUT, NULL);
509 "Failed to set voltage of sensor %d, error %d\n",
515 int hl_set_current(struct hl_device *hdev,
516 int sensor_index, u32 attr, long value)
518 struct armcp_packet pkt;
521 memset(&pkt, 0, sizeof(pkt));
523 pkt.ctl = cpu_to_le32(ARMCP_PACKET_CURRENT_SET <<
524 ARMCP_PKT_CTL_OPCODE_SHIFT);
525 pkt.sensor_index = __cpu_to_le16(sensor_index);
526 pkt.type = __cpu_to_le16(attr);
527 pkt.value = __cpu_to_le64(value);
529 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
530 SENSORS_PKT_TIMEOUT, NULL);
534 "Failed to set current of sensor %d, error %d\n",
540 int hl_hwmon_init(struct hl_device *hdev)
542 struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev;
543 struct asic_fixed_properties *prop = &hdev->asic_prop;
546 if ((hdev->hwmon_initialized) || !(hdev->fw_loading))
549 if (hdev->hl_chip_info->info) {
550 hdev->hl_chip_info->ops = &hl_hwmon_ops;
552 hdev->hwmon_dev = hwmon_device_register_with_info(dev,
553 prop->armcp_info.card_name, hdev,
554 hdev->hl_chip_info, NULL);
555 if (IS_ERR(hdev->hwmon_dev)) {
556 rc = PTR_ERR(hdev->hwmon_dev);
558 "Unable to register hwmon device: %d\n", rc);
562 dev_info(hdev->dev, "%s: add sensors information\n",
563 dev_name(hdev->hwmon_dev));
565 hdev->hwmon_initialized = true;
567 dev_info(hdev->dev, "no available sensors\n");
573 void hl_hwmon_fini(struct hl_device *hdev)
575 if (!hdev->hwmon_initialized)
578 hwmon_device_unregister(hdev->hwmon_dev);