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,
15 struct cpucp_packet pkt;
20 rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
24 memset(&pkt, 0, sizeof(pkt));
27 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET <<
28 CPUCP_PKT_CTL_OPCODE_SHIFT);
30 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_GET <<
31 CPUCP_PKT_CTL_OPCODE_SHIFT);
32 pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
34 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
39 "Failed to get frequency of PLL %d, error %d\n",
47 void hl_set_frequency(struct hl_device *hdev, u32 pll_index,
50 struct cpucp_packet pkt;
54 rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
58 memset(&pkt, 0, sizeof(pkt));
60 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_SET <<
61 CPUCP_PKT_CTL_OPCODE_SHIFT);
62 pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
63 pkt.value = cpu_to_le64(freq);
65 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
70 "Failed to set frequency to PLL %d, error %d\n",
74 u64 hl_get_max_power(struct hl_device *hdev)
76 struct cpucp_packet pkt;
80 memset(&pkt, 0, sizeof(pkt));
82 pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_GET <<
83 CPUCP_PKT_CTL_OPCODE_SHIFT);
85 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
89 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
96 void hl_set_max_power(struct hl_device *hdev)
98 struct cpucp_packet pkt;
101 memset(&pkt, 0, sizeof(pkt));
103 pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_SET <<
104 CPUCP_PKT_CTL_OPCODE_SHIFT);
105 pkt.value = cpu_to_le64(hdev->max_power);
107 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
111 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
114 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
117 struct hl_device *hdev = dev_get_drvdata(dev);
119 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
122 static ssize_t armcp_kernel_ver_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
125 struct hl_device *hdev = dev_get_drvdata(dev);
127 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
130 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
133 struct hl_device *hdev = dev_get_drvdata(dev);
135 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
138 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
141 struct hl_device *hdev = dev_get_drvdata(dev);
143 return sprintf(buf, "0x%08x\n",
144 hdev->asic_prop.cpucp_info.cpld_version);
147 static ssize_t cpucp_kernel_ver_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
150 struct hl_device *hdev = dev_get_drvdata(dev);
152 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
155 static ssize_t cpucp_ver_show(struct device *dev, struct device_attribute *attr,
158 struct hl_device *hdev = dev_get_drvdata(dev);
160 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
163 static ssize_t infineon_ver_show(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 struct hl_device *hdev = dev_get_drvdata(dev);
168 return sprintf(buf, "0x%04x\n",
169 hdev->asic_prop.cpucp_info.infineon_version);
172 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
175 struct hl_device *hdev = dev_get_drvdata(dev);
177 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.fuse_version);
180 static ssize_t thermal_ver_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
183 struct hl_device *hdev = dev_get_drvdata(dev);
185 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.thermal_version);
188 static ssize_t preboot_btl_ver_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
191 struct hl_device *hdev = dev_get_drvdata(dev);
193 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
196 static ssize_t soft_reset_store(struct device *dev,
197 struct device_attribute *attr, const char *buf,
200 struct hl_device *hdev = dev_get_drvdata(dev);
204 rc = kstrtoul(buf, 0, &value);
211 if (!hdev->allow_external_soft_reset) {
212 dev_err(hdev->dev, "Device does not support soft-reset\n");
216 dev_warn(hdev->dev, "Soft-Reset requested through sysfs\n");
218 hl_device_reset(hdev, 0);
224 static ssize_t hard_reset_store(struct device *dev,
225 struct device_attribute *attr,
226 const char *buf, size_t count)
228 struct hl_device *hdev = dev_get_drvdata(dev);
232 rc = kstrtoul(buf, 0, &value);
239 dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
241 hl_device_reset(hdev, HL_RESET_HARD);
247 static ssize_t device_type_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct hl_device *hdev = dev_get_drvdata(dev);
253 switch (hdev->asic_type) {
264 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
269 return sprintf(buf, "%s\n", str);
272 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
275 struct hl_device *hdev = dev_get_drvdata(dev);
277 return sprintf(buf, "%04x:%02x:%02x.%x\n",
278 pci_domain_nr(hdev->pdev->bus),
279 hdev->pdev->bus->number,
280 PCI_SLOT(hdev->pdev->devfn),
281 PCI_FUNC(hdev->pdev->devfn));
284 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
287 struct hl_device *hdev = dev_get_drvdata(dev);
290 if (atomic_read(&hdev->in_reset))
292 else if (hdev->disabled)
294 else if (hdev->needs_reset)
299 return sprintf(buf, "%s\n", str);
302 static ssize_t soft_reset_cnt_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct hl_device *hdev = dev_get_drvdata(dev);
307 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
310 static ssize_t hard_reset_cnt_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct hl_device *hdev = dev_get_drvdata(dev);
315 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
318 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
321 struct hl_device *hdev = dev_get_drvdata(dev);
324 if (!hl_device_operational(hdev, NULL))
327 val = hl_get_max_power(hdev);
329 return sprintf(buf, "%lu\n", val);
332 static ssize_t max_power_store(struct device *dev,
333 struct device_attribute *attr, const char *buf, size_t count)
335 struct hl_device *hdev = dev_get_drvdata(dev);
339 if (!hl_device_operational(hdev, NULL)) {
344 rc = kstrtoul(buf, 0, &value);
351 hdev->max_power = value;
352 hl_set_max_power(hdev);
358 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
359 struct bin_attribute *attr, char *buf, loff_t offset,
362 struct device *dev = kobj_to_dev(kobj);
363 struct hl_device *hdev = dev_get_drvdata(dev);
367 if (!hl_device_operational(hdev, NULL))
373 data = kzalloc(max_size, GFP_KERNEL);
377 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
381 memcpy(buf, data, max_size);
389 static DEVICE_ATTR_RO(armcp_kernel_ver);
390 static DEVICE_ATTR_RO(armcp_ver);
391 static DEVICE_ATTR_RO(cpld_ver);
392 static DEVICE_ATTR_RO(cpucp_kernel_ver);
393 static DEVICE_ATTR_RO(cpucp_ver);
394 static DEVICE_ATTR_RO(device_type);
395 static DEVICE_ATTR_RO(fuse_ver);
396 static DEVICE_ATTR_WO(hard_reset);
397 static DEVICE_ATTR_RO(hard_reset_cnt);
398 static DEVICE_ATTR_RO(infineon_ver);
399 static DEVICE_ATTR_RW(max_power);
400 static DEVICE_ATTR_RO(pci_addr);
401 static DEVICE_ATTR_RO(preboot_btl_ver);
402 static DEVICE_ATTR_WO(soft_reset);
403 static DEVICE_ATTR_RO(soft_reset_cnt);
404 static DEVICE_ATTR_RO(status);
405 static DEVICE_ATTR_RO(thermal_ver);
406 static DEVICE_ATTR_RO(uboot_ver);
408 static struct bin_attribute bin_attr_eeprom = {
409 .attr = {.name = "eeprom", .mode = (0444)},
411 .read = eeprom_read_handler
414 static struct attribute *hl_dev_attrs[] = {
415 &dev_attr_armcp_kernel_ver.attr,
416 &dev_attr_armcp_ver.attr,
417 &dev_attr_cpld_ver.attr,
418 &dev_attr_cpucp_kernel_ver.attr,
419 &dev_attr_cpucp_ver.attr,
420 &dev_attr_device_type.attr,
421 &dev_attr_fuse_ver.attr,
422 &dev_attr_hard_reset.attr,
423 &dev_attr_hard_reset_cnt.attr,
424 &dev_attr_infineon_ver.attr,
425 &dev_attr_max_power.attr,
426 &dev_attr_pci_addr.attr,
427 &dev_attr_preboot_btl_ver.attr,
428 &dev_attr_soft_reset.attr,
429 &dev_attr_soft_reset_cnt.attr,
430 &dev_attr_status.attr,
431 &dev_attr_thermal_ver.attr,
432 &dev_attr_uboot_ver.attr,
436 static struct bin_attribute *hl_dev_bin_attrs[] = {
441 static struct attribute_group hl_dev_attr_group = {
442 .attrs = hl_dev_attrs,
443 .bin_attrs = hl_dev_bin_attrs,
446 static struct attribute_group hl_dev_clks_attr_group;
448 static const struct attribute_group *hl_dev_attr_groups[] = {
450 &hl_dev_clks_attr_group,
454 int hl_sysfs_init(struct hl_device *hdev)
458 if (hdev->asic_type == ASIC_GOYA)
459 hdev->pm_mng_profile = PM_AUTO;
461 hdev->pm_mng_profile = PM_MANUAL;
463 hdev->max_power = hdev->asic_prop.max_power_default;
465 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
467 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
470 "Failed to add groups to device, error %d\n", rc);
477 void hl_sysfs_fini(struct hl_device *hdev)
479 device_remove_groups(hdev->dev, hl_dev_attr_groups);