1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2022 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
11 #include <linux/types.h>
13 static ssize_t clk_max_freq_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
15 struct hl_device *hdev = dev_get_drvdata(dev);
18 if (!hl_device_operational(hdev, NULL))
21 value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, false);
25 hdev->asic_prop.max_freq_value = value;
27 return sprintf(buf, "%lu\n", (value / 1000 / 1000));
30 static ssize_t clk_max_freq_mhz_store(struct device *dev, struct device_attribute *attr,
31 const char *buf, size_t count)
33 struct hl_device *hdev = dev_get_drvdata(dev);
37 if (!hl_device_operational(hdev, NULL)) {
42 rc = kstrtoull(buf, 0, &value);
48 hdev->asic_prop.max_freq_value = value * 1000 * 1000;
50 hl_fw_set_frequency(hdev, hdev->asic_prop.clk_pll_index, hdev->asic_prop.max_freq_value);
56 static ssize_t clk_cur_freq_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
58 struct hl_device *hdev = dev_get_drvdata(dev);
61 if (!hl_device_operational(hdev, NULL))
64 value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, true);
68 return sprintf(buf, "%lu\n", (value / 1000 / 1000));
71 static DEVICE_ATTR_RW(clk_max_freq_mhz);
72 static DEVICE_ATTR_RO(clk_cur_freq_mhz);
74 static struct attribute *hl_dev_clk_attrs[] = {
75 &dev_attr_clk_max_freq_mhz.attr,
76 &dev_attr_clk_cur_freq_mhz.attr,
80 static ssize_t vrm_ver_show(struct device *dev, struct device_attribute *attr, char *buf)
82 struct hl_device *hdev = dev_get_drvdata(dev);
83 struct cpucp_info *cpucp_info;
84 u32 infineon_second_stage_version;
85 u32 infineon_second_stage_first_instance;
86 u32 infineon_second_stage_second_instance;
87 u32 infineon_second_stage_third_instance;
90 cpucp_info = &hdev->asic_prop.cpucp_info;
92 infineon_second_stage_version = le32_to_cpu(cpucp_info->infineon_second_stage_version);
93 infineon_second_stage_first_instance = infineon_second_stage_version & mask;
94 infineon_second_stage_second_instance =
95 (infineon_second_stage_version >> 8) & mask;
96 infineon_second_stage_third_instance =
97 (infineon_second_stage_version >> 16) & mask;
99 if (cpucp_info->infineon_second_stage_version)
100 return sprintf(buf, "%#04x %#04x:%#04x:%#04x\n",
101 le32_to_cpu(cpucp_info->infineon_version),
102 infineon_second_stage_first_instance,
103 infineon_second_stage_second_instance,
104 infineon_second_stage_third_instance);
106 return sprintf(buf, "%#04x\n", le32_to_cpu(cpucp_info->infineon_version));
109 static DEVICE_ATTR_RO(vrm_ver);
111 static struct attribute *hl_dev_vrm_attrs[] = {
112 &dev_attr_vrm_ver.attr,
116 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
119 struct hl_device *hdev = dev_get_drvdata(dev);
121 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
124 static ssize_t armcp_kernel_ver_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 struct hl_device *hdev = dev_get_drvdata(dev);
129 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
132 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
135 struct hl_device *hdev = dev_get_drvdata(dev);
137 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
140 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
143 struct hl_device *hdev = dev_get_drvdata(dev);
145 return sprintf(buf, "0x%08x\n",
146 le32_to_cpu(hdev->asic_prop.cpucp_info.cpld_version));
149 static ssize_t cpucp_kernel_ver_show(struct device *dev,
150 struct device_attribute *attr, char *buf)
152 struct hl_device *hdev = dev_get_drvdata(dev);
154 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
157 static ssize_t cpucp_ver_show(struct device *dev, struct device_attribute *attr,
160 struct hl_device *hdev = dev_get_drvdata(dev);
162 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
165 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
168 struct hl_device *hdev = dev_get_drvdata(dev);
170 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.fuse_version);
173 static ssize_t thermal_ver_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct hl_device *hdev = dev_get_drvdata(dev);
178 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.thermal_version);
181 static ssize_t fw_os_ver_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
184 struct hl_device *hdev = dev_get_drvdata(dev);
186 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.fw_os_version);
189 static ssize_t preboot_btl_ver_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
192 struct hl_device *hdev = dev_get_drvdata(dev);
194 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
197 static ssize_t soft_reset_store(struct device *dev,
198 struct device_attribute *attr, const char *buf,
201 struct hl_device *hdev = dev_get_drvdata(dev);
205 rc = kstrtoul(buf, 0, &value);
212 if (!hdev->asic_prop.allow_inference_soft_reset) {
213 dev_err(hdev->dev, "Device does not support inference soft-reset\n");
217 dev_warn(hdev->dev, "Inference Soft-Reset requested through sysfs\n");
219 hl_device_reset(hdev, 0);
225 static ssize_t hard_reset_store(struct device *dev,
226 struct device_attribute *attr,
227 const char *buf, size_t count)
229 struct hl_device *hdev = dev_get_drvdata(dev);
233 rc = kstrtoul(buf, 0, &value);
240 dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
242 hl_device_reset(hdev, HL_DRV_RESET_HARD);
248 static ssize_t device_type_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct hl_device *hdev = dev_get_drvdata(dev);
254 switch (hdev->asic_type) {
274 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
279 return sprintf(buf, "%s\n", str);
282 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
285 struct hl_device *hdev = dev_get_drvdata(dev);
287 return sprintf(buf, "%04x:%02x:%02x.%x\n",
288 pci_domain_nr(hdev->pdev->bus),
289 hdev->pdev->bus->number,
290 PCI_SLOT(hdev->pdev->devfn),
291 PCI_FUNC(hdev->pdev->devfn));
294 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
297 struct hl_device *hdev = dev_get_drvdata(dev);
298 char str[HL_STR_MAX];
300 strscpy(str, hdev->status[hl_device_status(hdev)], HL_STR_MAX);
302 /* use uppercase for backward compatibility */
303 str[0] = 'A' + (str[0] - 'a');
305 return sprintf(buf, "%s\n", str);
308 static ssize_t soft_reset_cnt_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 struct hl_device *hdev = dev_get_drvdata(dev);
313 return sprintf(buf, "%d\n", hdev->reset_info.compute_reset_cnt);
316 static ssize_t hard_reset_cnt_show(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 struct hl_device *hdev = dev_get_drvdata(dev);
321 return sprintf(buf, "%d\n", hdev->reset_info.hard_reset_cnt);
324 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
327 struct hl_device *hdev = dev_get_drvdata(dev);
330 if (!hl_device_operational(hdev, NULL))
333 val = hl_fw_get_max_power(hdev);
337 return sprintf(buf, "%lu\n", val);
340 static ssize_t max_power_store(struct device *dev,
341 struct device_attribute *attr, const char *buf, size_t count)
343 struct hl_device *hdev = dev_get_drvdata(dev);
347 if (!hl_device_operational(hdev, NULL)) {
352 rc = kstrtoul(buf, 0, &value);
359 hdev->max_power = value;
360 hl_fw_set_max_power(hdev);
366 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
367 struct bin_attribute *attr, char *buf, loff_t offset,
370 struct device *dev = kobj_to_dev(kobj);
371 struct hl_device *hdev = dev_get_drvdata(dev);
375 if (!hl_device_operational(hdev, NULL))
381 data = kzalloc(max_size, GFP_KERNEL);
385 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
389 memcpy(buf, data, max_size);
397 static ssize_t security_enabled_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 struct hl_device *hdev = dev_get_drvdata(dev);
402 return sprintf(buf, "%d\n", hdev->asic_prop.fw_security_enabled);
405 static ssize_t module_id_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
408 struct hl_device *hdev = dev_get_drvdata(dev);
410 return sprintf(buf, "%u\n", le32_to_cpu(hdev->asic_prop.cpucp_info.card_location));
413 static ssize_t parent_device_show(struct device *dev, struct device_attribute *attr, char *buf)
415 struct hl_device *hdev = dev_get_drvdata(dev);
417 return sprintf(buf, "%s\n", HL_DEV_NAME(hdev));
420 static DEVICE_ATTR_RO(armcp_kernel_ver);
421 static DEVICE_ATTR_RO(armcp_ver);
422 static DEVICE_ATTR_RO(cpld_ver);
423 static DEVICE_ATTR_RO(cpucp_kernel_ver);
424 static DEVICE_ATTR_RO(cpucp_ver);
425 static DEVICE_ATTR_RO(device_type);
426 static DEVICE_ATTR_RO(fuse_ver);
427 static DEVICE_ATTR_WO(hard_reset);
428 static DEVICE_ATTR_RO(hard_reset_cnt);
429 static DEVICE_ATTR_RW(max_power);
430 static DEVICE_ATTR_RO(pci_addr);
431 static DEVICE_ATTR_RO(preboot_btl_ver);
432 static DEVICE_ATTR_WO(soft_reset);
433 static DEVICE_ATTR_RO(soft_reset_cnt);
434 static DEVICE_ATTR_RO(status);
435 static DEVICE_ATTR_RO(thermal_ver);
436 static DEVICE_ATTR_RO(uboot_ver);
437 static DEVICE_ATTR_RO(fw_os_ver);
438 static DEVICE_ATTR_RO(security_enabled);
439 static DEVICE_ATTR_RO(module_id);
440 static DEVICE_ATTR_RO(parent_device);
442 static struct bin_attribute bin_attr_eeprom = {
443 .attr = {.name = "eeprom", .mode = (0444)},
445 .read = eeprom_read_handler
448 static struct attribute *hl_dev_attrs[] = {
449 &dev_attr_armcp_kernel_ver.attr,
450 &dev_attr_armcp_ver.attr,
451 &dev_attr_cpld_ver.attr,
452 &dev_attr_cpucp_kernel_ver.attr,
453 &dev_attr_cpucp_ver.attr,
454 &dev_attr_device_type.attr,
455 &dev_attr_fuse_ver.attr,
456 &dev_attr_hard_reset.attr,
457 &dev_attr_hard_reset_cnt.attr,
458 &dev_attr_max_power.attr,
459 &dev_attr_pci_addr.attr,
460 &dev_attr_preboot_btl_ver.attr,
461 &dev_attr_status.attr,
462 &dev_attr_thermal_ver.attr,
463 &dev_attr_uboot_ver.attr,
464 &dev_attr_fw_os_ver.attr,
465 &dev_attr_security_enabled.attr,
466 &dev_attr_module_id.attr,
467 &dev_attr_parent_device.attr,
471 static struct bin_attribute *hl_dev_bin_attrs[] = {
476 static struct attribute_group hl_dev_attr_group = {
477 .attrs = hl_dev_attrs,
478 .bin_attrs = hl_dev_bin_attrs,
481 static struct attribute_group hl_dev_clks_attr_group;
482 static struct attribute_group hl_dev_vrm_attr_group;
484 static const struct attribute_group *hl_dev_attr_groups[] = {
486 &hl_dev_clks_attr_group,
487 &hl_dev_vrm_attr_group,
491 static struct attribute *hl_dev_inference_attrs[] = {
492 &dev_attr_soft_reset.attr,
493 &dev_attr_soft_reset_cnt.attr,
497 static struct attribute_group hl_dev_inference_attr_group = {
498 .attrs = hl_dev_inference_attrs,
501 static const struct attribute_group *hl_dev_inference_attr_groups[] = {
502 &hl_dev_inference_attr_group,
506 void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp)
508 dev_clk_attr_grp->attrs = hl_dev_clk_attrs;
511 void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp)
513 dev_vrm_attr_grp->attrs = hl_dev_vrm_attrs;
516 int hl_sysfs_init(struct hl_device *hdev)
520 hdev->max_power = hdev->asic_prop.max_power_default;
522 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group, &hl_dev_vrm_attr_group);
524 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
527 "Failed to add groups to device, error %d\n", rc);
531 if (!hdev->asic_prop.allow_inference_soft_reset)
534 rc = device_add_groups(hdev->dev, hl_dev_inference_attr_groups);
537 "Failed to add groups to device, error %d\n", rc);
544 device_remove_groups(hdev->dev, hl_dev_attr_groups);
548 void hl_sysfs_fini(struct hl_device *hdev)
550 device_remove_groups(hdev->dev, hl_dev_attr_groups);
552 if (!hdev->asic_prop.allow_inference_soft_reset)
555 device_remove_groups(hdev->dev, hl_dev_inference_attr_groups);