Path: blob/master/drivers/accel/habanalabs/common/sysfs.c
29281 views
// SPDX-License-Identifier: GPL-2.012/*3* Copyright 2016-2022 HabanaLabs, Ltd.4* All Rights Reserved.5*/67#include "habanalabs.h"89#include <linux/pci.h>10#include <linux/types.h>1112static ssize_t clk_max_freq_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)13{14struct hl_device *hdev = dev_get_drvdata(dev);15long value;1617if (!hl_device_operational(hdev, NULL))18return -ENODEV;1920value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, false);21if (value < 0)22return value;2324hdev->asic_prop.max_freq_value = value;2526return sprintf(buf, "%lu\n", (value / 1000 / 1000));27}2829static ssize_t clk_max_freq_mhz_store(struct device *dev, struct device_attribute *attr,30const char *buf, size_t count)31{32struct hl_device *hdev = dev_get_drvdata(dev);33int rc;34u64 value;3536if (!hl_device_operational(hdev, NULL)) {37count = -ENODEV;38goto fail;39}4041rc = kstrtoull(buf, 0, &value);42if (rc) {43count = -EINVAL;44goto fail;45}4647hdev->asic_prop.max_freq_value = value * 1000 * 1000;4849hl_fw_set_frequency(hdev, hdev->asic_prop.clk_pll_index, hdev->asic_prop.max_freq_value);5051fail:52return count;53}5455static ssize_t clk_cur_freq_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)56{57struct hl_device *hdev = dev_get_drvdata(dev);58long value;5960if (!hl_device_operational(hdev, NULL))61return -ENODEV;6263value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, true);64if (value < 0)65return value;6667return sprintf(buf, "%lu\n", (value / 1000 / 1000));68}6970static DEVICE_ATTR_RW(clk_max_freq_mhz);71static DEVICE_ATTR_RO(clk_cur_freq_mhz);7273static struct attribute *hl_dev_clk_attrs[] = {74&dev_attr_clk_max_freq_mhz.attr,75&dev_attr_clk_cur_freq_mhz.attr,76NULL,77};7879static ssize_t vrm_ver_show(struct device *dev, struct device_attribute *attr, char *buf)80{81struct hl_device *hdev = dev_get_drvdata(dev);82struct cpucp_info *cpucp_info;83u32 infineon_second_stage_version;84u32 infineon_second_stage_first_instance;85u32 infineon_second_stage_second_instance;86u32 infineon_second_stage_third_instance;87u32 mask = 0xff;8889cpucp_info = &hdev->asic_prop.cpucp_info;9091infineon_second_stage_version = le32_to_cpu(cpucp_info->infineon_second_stage_version);92infineon_second_stage_first_instance = infineon_second_stage_version & mask;93infineon_second_stage_second_instance =94(infineon_second_stage_version >> 8) & mask;95infineon_second_stage_third_instance =96(infineon_second_stage_version >> 16) & mask;9798if (cpucp_info->infineon_version && cpucp_info->infineon_second_stage_version)99return sprintf(buf, "%#04x %#04x:%#04x:%#04x\n",100le32_to_cpu(cpucp_info->infineon_version),101infineon_second_stage_first_instance,102infineon_second_stage_second_instance,103infineon_second_stage_third_instance);104else if (cpucp_info->infineon_second_stage_version)105return sprintf(buf, "%#04x:%#04x:%#04x\n",106infineon_second_stage_first_instance,107infineon_second_stage_second_instance,108infineon_second_stage_third_instance);109else if (cpucp_info->infineon_version)110return sprintf(buf, "%#04x\n", le32_to_cpu(cpucp_info->infineon_version));111112return 0;113}114115static DEVICE_ATTR_RO(vrm_ver);116117static struct attribute *hl_dev_vrm_attrs[] = {118&dev_attr_vrm_ver.attr,119NULL,120};121122static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,123char *buf)124{125struct hl_device *hdev = dev_get_drvdata(dev);126127return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);128}129130static ssize_t armcp_kernel_ver_show(struct device *dev,131struct device_attribute *attr, char *buf)132{133struct hl_device *hdev = dev_get_drvdata(dev);134135return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);136}137138static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,139char *buf)140{141struct hl_device *hdev = dev_get_drvdata(dev);142143return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);144}145146static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,147char *buf)148{149struct hl_device *hdev = dev_get_drvdata(dev);150151return sprintf(buf, "0x%08x%08x\n",152le32_to_cpu(hdev->asic_prop.cpucp_info.cpld_timestamp),153le32_to_cpu(hdev->asic_prop.cpucp_info.cpld_version));154}155156static ssize_t cpucp_kernel_ver_show(struct device *dev,157struct device_attribute *attr, char *buf)158{159struct hl_device *hdev = dev_get_drvdata(dev);160161return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);162}163164static ssize_t cpucp_ver_show(struct device *dev, struct device_attribute *attr,165char *buf)166{167struct hl_device *hdev = dev_get_drvdata(dev);168169return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);170}171172static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,173char *buf)174{175struct hl_device *hdev = dev_get_drvdata(dev);176177return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.fuse_version);178}179180static ssize_t thermal_ver_show(struct device *dev,181struct device_attribute *attr, char *buf)182{183struct hl_device *hdev = dev_get_drvdata(dev);184185return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.thermal_version);186}187188static ssize_t fw_os_ver_show(struct device *dev,189struct device_attribute *attr, char *buf)190{191struct hl_device *hdev = dev_get_drvdata(dev);192193return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.fw_os_version);194}195196static ssize_t preboot_btl_ver_show(struct device *dev,197struct device_attribute *attr, char *buf)198{199struct hl_device *hdev = dev_get_drvdata(dev);200201return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);202}203204static ssize_t soft_reset_store(struct device *dev,205struct device_attribute *attr, const char *buf,206size_t count)207{208struct hl_device *hdev = dev_get_drvdata(dev);209long value;210int rc;211212rc = kstrtoul(buf, 0, &value);213214if (rc) {215count = -EINVAL;216goto out;217}218219if (!hdev->asic_prop.allow_inference_soft_reset) {220dev_err(hdev->dev, "Device does not support inference soft-reset\n");221goto out;222}223224dev_warn(hdev->dev, "Inference Soft-Reset requested through sysfs\n");225226hl_device_reset(hdev, 0);227228out:229return count;230}231232static ssize_t hard_reset_store(struct device *dev,233struct device_attribute *attr,234const char *buf, size_t count)235{236struct hl_device *hdev = dev_get_drvdata(dev);237long value;238int rc;239240rc = kstrtoul(buf, 0, &value);241242if (rc) {243count = -EINVAL;244goto out;245}246247dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");248249hl_device_reset(hdev, HL_DRV_RESET_HARD);250251out:252return count;253}254255static ssize_t device_type_show(struct device *dev,256struct device_attribute *attr, char *buf)257{258struct hl_device *hdev = dev_get_drvdata(dev);259char *str;260261switch (hdev->asic_type) {262case ASIC_GOYA:263str = "GOYA";264break;265case ASIC_GAUDI:266str = "GAUDI";267break;268case ASIC_GAUDI_SEC:269str = "GAUDI SEC";270break;271case ASIC_GAUDI2:272str = "GAUDI2";273break;274case ASIC_GAUDI2B:275str = "GAUDI2B";276break;277case ASIC_GAUDI2C:278str = "GAUDI2C";279break;280case ASIC_GAUDI2D:281str = "GAUDI2D";282break;283default:284dev_err(hdev->dev, "Unrecognized ASIC type %d\n",285hdev->asic_type);286return -EINVAL;287}288289return sprintf(buf, "%s\n", str);290}291292static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,293char *buf)294{295struct hl_device *hdev = dev_get_drvdata(dev);296297return sprintf(buf, "%04x:%02x:%02x.%x\n",298pci_domain_nr(hdev->pdev->bus),299hdev->pdev->bus->number,300PCI_SLOT(hdev->pdev->devfn),301PCI_FUNC(hdev->pdev->devfn));302}303304static ssize_t status_show(struct device *dev, struct device_attribute *attr,305char *buf)306{307struct hl_device *hdev = dev_get_drvdata(dev);308char str[HL_STR_MAX];309310strscpy(str, hdev->status[hl_device_status(hdev)], HL_STR_MAX);311312/* use uppercase for backward compatibility */313str[0] = 'A' + (str[0] - 'a');314315return sprintf(buf, "%s\n", str);316}317318static ssize_t soft_reset_cnt_show(struct device *dev,319struct device_attribute *attr, char *buf)320{321struct hl_device *hdev = dev_get_drvdata(dev);322323return sprintf(buf, "%d\n", hdev->reset_info.compute_reset_cnt);324}325326static ssize_t hard_reset_cnt_show(struct device *dev,327struct device_attribute *attr, char *buf)328{329struct hl_device *hdev = dev_get_drvdata(dev);330331return sprintf(buf, "%d\n", hdev->reset_info.hard_reset_cnt);332}333334static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,335char *buf)336{337struct hl_device *hdev = dev_get_drvdata(dev);338long val;339340if (!hl_device_operational(hdev, NULL))341return -ENODEV;342343val = hl_fw_get_max_power(hdev);344if (val < 0)345return val;346347return sprintf(buf, "%lu\n", val);348}349350static ssize_t max_power_store(struct device *dev,351struct device_attribute *attr, const char *buf, size_t count)352{353struct hl_device *hdev = dev_get_drvdata(dev);354unsigned long value;355int rc;356357if (!hl_device_operational(hdev, NULL)) {358count = -ENODEV;359goto out;360}361362rc = kstrtoul(buf, 0, &value);363364if (rc) {365count = -EINVAL;366goto out;367}368369hdev->max_power = value;370hl_fw_set_max_power(hdev);371372out:373return count;374}375376static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,377const struct bin_attribute *attr, char *buf, loff_t offset,378size_t max_size)379{380struct device *dev = kobj_to_dev(kobj);381struct hl_device *hdev = dev_get_drvdata(dev);382char *data;383int rc;384385if (!hl_device_operational(hdev, NULL))386return -ENODEV;387388if (!max_size)389return -EINVAL;390391data = kzalloc(max_size, GFP_KERNEL);392if (!data)393return -ENOMEM;394395rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);396if (rc)397goto out;398399memcpy(buf, data, max_size);400401out:402kfree(data);403404return max_size;405}406407static ssize_t security_enabled_show(struct device *dev,408struct device_attribute *attr, char *buf)409{410struct hl_device *hdev = dev_get_drvdata(dev);411412return sprintf(buf, "%d\n", hdev->asic_prop.fw_security_enabled);413}414415static ssize_t module_id_show(struct device *dev,416struct device_attribute *attr, char *buf)417{418struct hl_device *hdev = dev_get_drvdata(dev);419420return sprintf(buf, "%u\n", le32_to_cpu(hdev->asic_prop.cpucp_info.card_location));421}422423static ssize_t parent_device_show(struct device *dev, struct device_attribute *attr, char *buf)424{425struct hl_device *hdev = dev_get_drvdata(dev);426427return sprintf(buf, "%s\n", HL_DEV_NAME(hdev));428}429430static DEVICE_ATTR_RO(armcp_kernel_ver);431static DEVICE_ATTR_RO(armcp_ver);432static DEVICE_ATTR_RO(cpld_ver);433static DEVICE_ATTR_RO(cpucp_kernel_ver);434static DEVICE_ATTR_RO(cpucp_ver);435static DEVICE_ATTR_RO(device_type);436static DEVICE_ATTR_RO(fuse_ver);437static DEVICE_ATTR_WO(hard_reset);438static DEVICE_ATTR_RO(hard_reset_cnt);439static DEVICE_ATTR_RW(max_power);440static DEVICE_ATTR_RO(pci_addr);441static DEVICE_ATTR_RO(preboot_btl_ver);442static DEVICE_ATTR_WO(soft_reset);443static DEVICE_ATTR_RO(soft_reset_cnt);444static DEVICE_ATTR_RO(status);445static DEVICE_ATTR_RO(thermal_ver);446static DEVICE_ATTR_RO(uboot_ver);447static DEVICE_ATTR_RO(fw_os_ver);448static DEVICE_ATTR_RO(security_enabled);449static DEVICE_ATTR_RO(module_id);450static DEVICE_ATTR_RO(parent_device);451452static const struct bin_attribute bin_attr_eeprom = {453.attr = {.name = "eeprom", .mode = (0444)},454.size = PAGE_SIZE,455.read = eeprom_read_handler456};457458static struct attribute *hl_dev_attrs[] = {459&dev_attr_armcp_kernel_ver.attr,460&dev_attr_armcp_ver.attr,461&dev_attr_cpld_ver.attr,462&dev_attr_cpucp_kernel_ver.attr,463&dev_attr_cpucp_ver.attr,464&dev_attr_device_type.attr,465&dev_attr_fuse_ver.attr,466&dev_attr_hard_reset.attr,467&dev_attr_hard_reset_cnt.attr,468&dev_attr_max_power.attr,469&dev_attr_pci_addr.attr,470&dev_attr_preboot_btl_ver.attr,471&dev_attr_status.attr,472&dev_attr_thermal_ver.attr,473&dev_attr_uboot_ver.attr,474&dev_attr_fw_os_ver.attr,475&dev_attr_security_enabled.attr,476&dev_attr_module_id.attr,477&dev_attr_parent_device.attr,478NULL,479};480481static const struct bin_attribute *const hl_dev_bin_attrs[] = {482&bin_attr_eeprom,483NULL484};485486static struct attribute_group hl_dev_attr_group = {487.attrs = hl_dev_attrs,488.bin_attrs = hl_dev_bin_attrs,489};490491static struct attribute_group hl_dev_clks_attr_group;492static struct attribute_group hl_dev_vrm_attr_group;493494static const struct attribute_group *hl_dev_attr_groups[] = {495&hl_dev_attr_group,496&hl_dev_clks_attr_group,497&hl_dev_vrm_attr_group,498NULL,499};500501static struct attribute *hl_dev_inference_attrs[] = {502&dev_attr_soft_reset.attr,503&dev_attr_soft_reset_cnt.attr,504NULL,505};506507static struct attribute_group hl_dev_inference_attr_group = {508.attrs = hl_dev_inference_attrs,509};510511static const struct attribute_group *hl_dev_inference_attr_groups[] = {512&hl_dev_inference_attr_group,513NULL,514};515516void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp)517{518dev_clk_attr_grp->attrs = hl_dev_clk_attrs;519}520521void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp)522{523dev_vrm_attr_grp->attrs = hl_dev_vrm_attrs;524}525526int hl_sysfs_init(struct hl_device *hdev)527{528int rc;529530hdev->max_power = hdev->asic_prop.max_power_default;531532hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group, &hl_dev_vrm_attr_group);533534rc = device_add_groups(hdev->dev, hl_dev_attr_groups);535if (rc) {536dev_err(hdev->dev,537"Failed to add groups to device, error %d\n", rc);538return rc;539}540541if (!hdev->asic_prop.allow_inference_soft_reset)542return 0;543544rc = device_add_groups(hdev->dev, hl_dev_inference_attr_groups);545if (rc) {546dev_err(hdev->dev,547"Failed to add groups to device, error %d\n", rc);548goto remove_groups;549}550551return 0;552553remove_groups:554device_remove_groups(hdev->dev, hl_dev_attr_groups);555return rc;556}557558void hl_sysfs_fini(struct hl_device *hdev)559{560device_remove_groups(hdev->dev, hl_dev_attr_groups);561562if (!hdev->asic_prop.allow_inference_soft_reset)563return;564565device_remove_groups(hdev->dev, hl_dev_inference_attr_groups);566}567568569