1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
12 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr)
14 struct armcp_packet pkt;
18 memset(&pkt, 0, sizeof(pkt));
21 pkt.ctl = cpu_to_le32(ARMCP_PACKET_FREQUENCY_CURR_GET <<
22 ARMCP_PKT_CTL_OPCODE_SHIFT);
24 pkt.ctl = cpu_to_le32(ARMCP_PACKET_FREQUENCY_GET <<
25 ARMCP_PKT_CTL_OPCODE_SHIFT);
26 pkt.pll_index = cpu_to_le32(pll_index);
28 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
33 "Failed to get frequency of PLL %d, error %d\n",
41 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
43 struct armcp_packet pkt;
46 memset(&pkt, 0, sizeof(pkt));
48 pkt.ctl = cpu_to_le32(ARMCP_PACKET_FREQUENCY_SET <<
49 ARMCP_PKT_CTL_OPCODE_SHIFT);
50 pkt.pll_index = cpu_to_le32(pll_index);
51 pkt.value = cpu_to_le64(freq);
53 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
58 "Failed to set frequency to PLL %d, error %d\n",
62 u64 hl_get_max_power(struct hl_device *hdev)
64 struct armcp_packet pkt;
68 memset(&pkt, 0, sizeof(pkt));
70 pkt.ctl = cpu_to_le32(ARMCP_PACKET_MAX_POWER_GET <<
71 ARMCP_PKT_CTL_OPCODE_SHIFT);
73 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
77 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
84 void hl_set_max_power(struct hl_device *hdev)
86 struct armcp_packet pkt;
89 memset(&pkt, 0, sizeof(pkt));
91 pkt.ctl = cpu_to_le32(ARMCP_PACKET_MAX_POWER_SET <<
92 ARMCP_PKT_CTL_OPCODE_SHIFT);
93 pkt.value = cpu_to_le64(hdev->max_power);
95 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
99 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
102 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
105 struct hl_device *hdev = dev_get_drvdata(dev);
107 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
110 static ssize_t armcp_kernel_ver_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
113 struct hl_device *hdev = dev_get_drvdata(dev);
115 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.kernel_version);
118 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
121 struct hl_device *hdev = dev_get_drvdata(dev);
123 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.armcp_version);
126 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
129 struct hl_device *hdev = dev_get_drvdata(dev);
131 return sprintf(buf, "0x%08x\n",
132 hdev->asic_prop.armcp_info.cpld_version);
135 static ssize_t infineon_ver_show(struct device *dev,
136 struct device_attribute *attr, char *buf)
138 struct hl_device *hdev = dev_get_drvdata(dev);
140 return sprintf(buf, "0x%04x\n",
141 hdev->asic_prop.armcp_info.infineon_version);
144 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
147 struct hl_device *hdev = dev_get_drvdata(dev);
149 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.fuse_version);
152 static ssize_t thermal_ver_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct hl_device *hdev = dev_get_drvdata(dev);
157 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.thermal_version);
160 static ssize_t preboot_btl_ver_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct hl_device *hdev = dev_get_drvdata(dev);
165 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
168 static ssize_t soft_reset_store(struct device *dev,
169 struct device_attribute *attr, const char *buf,
172 struct hl_device *hdev = dev_get_drvdata(dev);
176 rc = kstrtoul(buf, 0, &value);
183 if (!hdev->supports_soft_reset) {
184 dev_err(hdev->dev, "Device does not support soft-reset\n");
188 dev_warn(hdev->dev, "Soft-Reset requested through sysfs\n");
190 hl_device_reset(hdev, false, false);
196 static ssize_t hard_reset_store(struct device *dev,
197 struct device_attribute *attr,
198 const char *buf, size_t count)
200 struct hl_device *hdev = dev_get_drvdata(dev);
204 rc = kstrtoul(buf, 0, &value);
211 dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
213 hl_device_reset(hdev, true, false);
219 static ssize_t device_type_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 struct hl_device *hdev = dev_get_drvdata(dev);
225 switch (hdev->asic_type) {
233 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
238 return sprintf(buf, "%s\n", str);
241 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
244 struct hl_device *hdev = dev_get_drvdata(dev);
246 return sprintf(buf, "%04x:%02x:%02x.%x\n",
247 pci_domain_nr(hdev->pdev->bus),
248 hdev->pdev->bus->number,
249 PCI_SLOT(hdev->pdev->devfn),
250 PCI_FUNC(hdev->pdev->devfn));
253 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
256 struct hl_device *hdev = dev_get_drvdata(dev);
259 if (atomic_read(&hdev->in_reset))
261 else if (hdev->disabled)
266 return sprintf(buf, "%s\n", str);
269 static ssize_t soft_reset_cnt_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
272 struct hl_device *hdev = dev_get_drvdata(dev);
274 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
277 static ssize_t hard_reset_cnt_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct hl_device *hdev = dev_get_drvdata(dev);
282 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
285 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
288 struct hl_device *hdev = dev_get_drvdata(dev);
291 if (hl_device_disabled_or_in_reset(hdev))
294 val = hl_get_max_power(hdev);
296 return sprintf(buf, "%lu\n", val);
299 static ssize_t max_power_store(struct device *dev,
300 struct device_attribute *attr, const char *buf, size_t count)
302 struct hl_device *hdev = dev_get_drvdata(dev);
306 if (hl_device_disabled_or_in_reset(hdev)) {
311 rc = kstrtoul(buf, 0, &value);
318 hdev->max_power = value;
319 hl_set_max_power(hdev);
325 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
326 struct bin_attribute *attr, char *buf, loff_t offset,
329 struct device *dev = container_of(kobj, struct device, kobj);
330 struct hl_device *hdev = dev_get_drvdata(dev);
334 if (hl_device_disabled_or_in_reset(hdev))
340 data = kzalloc(max_size, GFP_KERNEL);
344 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
348 memcpy(buf, data, max_size);
356 static DEVICE_ATTR_RO(armcp_kernel_ver);
357 static DEVICE_ATTR_RO(armcp_ver);
358 static DEVICE_ATTR_RO(cpld_ver);
359 static DEVICE_ATTR_RO(device_type);
360 static DEVICE_ATTR_RO(fuse_ver);
361 static DEVICE_ATTR_WO(hard_reset);
362 static DEVICE_ATTR_RO(hard_reset_cnt);
363 static DEVICE_ATTR_RO(infineon_ver);
364 static DEVICE_ATTR_RW(max_power);
365 static DEVICE_ATTR_RO(pci_addr);
366 static DEVICE_ATTR_RO(preboot_btl_ver);
367 static DEVICE_ATTR_WO(soft_reset);
368 static DEVICE_ATTR_RO(soft_reset_cnt);
369 static DEVICE_ATTR_RO(status);
370 static DEVICE_ATTR_RO(thermal_ver);
371 static DEVICE_ATTR_RO(uboot_ver);
373 static struct bin_attribute bin_attr_eeprom = {
374 .attr = {.name = "eeprom", .mode = (0444)},
376 .read = eeprom_read_handler
379 static struct attribute *hl_dev_attrs[] = {
380 &dev_attr_armcp_kernel_ver.attr,
381 &dev_attr_armcp_ver.attr,
382 &dev_attr_cpld_ver.attr,
383 &dev_attr_device_type.attr,
384 &dev_attr_fuse_ver.attr,
385 &dev_attr_hard_reset.attr,
386 &dev_attr_hard_reset_cnt.attr,
387 &dev_attr_infineon_ver.attr,
388 &dev_attr_max_power.attr,
389 &dev_attr_pci_addr.attr,
390 &dev_attr_preboot_btl_ver.attr,
391 &dev_attr_soft_reset.attr,
392 &dev_attr_soft_reset_cnt.attr,
393 &dev_attr_status.attr,
394 &dev_attr_thermal_ver.attr,
395 &dev_attr_uboot_ver.attr,
399 static struct bin_attribute *hl_dev_bin_attrs[] = {
404 static struct attribute_group hl_dev_attr_group = {
405 .attrs = hl_dev_attrs,
406 .bin_attrs = hl_dev_bin_attrs,
409 static struct attribute_group hl_dev_clks_attr_group;
411 static const struct attribute_group *hl_dev_attr_groups[] = {
413 &hl_dev_clks_attr_group,
417 int hl_sysfs_init(struct hl_device *hdev)
421 if (hdev->asic_type == ASIC_GOYA)
422 hdev->pm_mng_profile = PM_AUTO;
424 hdev->pm_mng_profile = PM_MANUAL;
426 hdev->max_power = hdev->asic_prop.max_power_default;
428 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
430 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
433 "Failed to add groups to device, error %d\n", rc);
440 void hl_sysfs_fini(struct hl_device *hdev)
442 device_remove_groups(hdev->dev, hl_dev_attr_groups);