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 cpucp_packet pkt;
18 memset(&pkt, 0, sizeof(pkt));
21 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET <<
22 CPUCP_PKT_CTL_OPCODE_SHIFT);
24 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_GET <<
25 CPUCP_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 cpucp_packet pkt;
46 memset(&pkt, 0, sizeof(pkt));
48 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_SET <<
49 CPUCP_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 cpucp_packet pkt;
68 memset(&pkt, 0, sizeof(pkt));
70 pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_GET <<
71 CPUCP_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 cpucp_packet pkt;
89 memset(&pkt, 0, sizeof(pkt));
91 pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_SET <<
92 CPUCP_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.cpucp_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.cpucp_info.cpucp_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.cpucp_info.cpld_version);
135 static ssize_t cpucp_kernel_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, "%s", hdev->asic_prop.cpucp_info.kernel_version);
143 static ssize_t cpucp_ver_show(struct device *dev, struct device_attribute *attr,
146 struct hl_device *hdev = dev_get_drvdata(dev);
148 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
151 static ssize_t infineon_ver_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
154 struct hl_device *hdev = dev_get_drvdata(dev);
156 return sprintf(buf, "0x%04x\n",
157 hdev->asic_prop.cpucp_info.infineon_version);
160 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
163 struct hl_device *hdev = dev_get_drvdata(dev);
165 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.fuse_version);
168 static ssize_t thermal_ver_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
171 struct hl_device *hdev = dev_get_drvdata(dev);
173 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.thermal_version);
176 static ssize_t preboot_btl_ver_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
179 struct hl_device *hdev = dev_get_drvdata(dev);
181 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
184 static ssize_t soft_reset_store(struct device *dev,
185 struct device_attribute *attr, const char *buf,
188 struct hl_device *hdev = dev_get_drvdata(dev);
192 rc = kstrtoul(buf, 0, &value);
199 if (!hdev->supports_soft_reset) {
200 dev_err(hdev->dev, "Device does not support soft-reset\n");
204 dev_warn(hdev->dev, "Soft-Reset requested through sysfs\n");
206 hl_device_reset(hdev, false, false);
212 static ssize_t hard_reset_store(struct device *dev,
213 struct device_attribute *attr,
214 const char *buf, size_t count)
216 struct hl_device *hdev = dev_get_drvdata(dev);
220 rc = kstrtoul(buf, 0, &value);
227 dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
229 hl_device_reset(hdev, true, false);
235 static ssize_t device_type_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 struct hl_device *hdev = dev_get_drvdata(dev);
241 switch (hdev->asic_type) {
249 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
254 return sprintf(buf, "%s\n", str);
257 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
260 struct hl_device *hdev = dev_get_drvdata(dev);
262 return sprintf(buf, "%04x:%02x:%02x.%x\n",
263 pci_domain_nr(hdev->pdev->bus),
264 hdev->pdev->bus->number,
265 PCI_SLOT(hdev->pdev->devfn),
266 PCI_FUNC(hdev->pdev->devfn));
269 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
272 struct hl_device *hdev = dev_get_drvdata(dev);
275 if (atomic_read(&hdev->in_reset))
277 else if (hdev->disabled)
282 return sprintf(buf, "%s\n", str);
285 static ssize_t soft_reset_cnt_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct hl_device *hdev = dev_get_drvdata(dev);
290 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
293 static ssize_t hard_reset_cnt_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct hl_device *hdev = dev_get_drvdata(dev);
298 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
301 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
304 struct hl_device *hdev = dev_get_drvdata(dev);
307 if (hl_device_disabled_or_in_reset(hdev))
310 val = hl_get_max_power(hdev);
312 return sprintf(buf, "%lu\n", val);
315 static ssize_t max_power_store(struct device *dev,
316 struct device_attribute *attr, const char *buf, size_t count)
318 struct hl_device *hdev = dev_get_drvdata(dev);
322 if (hl_device_disabled_or_in_reset(hdev)) {
327 rc = kstrtoul(buf, 0, &value);
334 hdev->max_power = value;
335 hl_set_max_power(hdev);
341 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
342 struct bin_attribute *attr, char *buf, loff_t offset,
345 struct device *dev = container_of(kobj, struct device, kobj);
346 struct hl_device *hdev = dev_get_drvdata(dev);
350 if (hl_device_disabled_or_in_reset(hdev))
356 data = kzalloc(max_size, GFP_KERNEL);
360 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
364 memcpy(buf, data, max_size);
372 static DEVICE_ATTR_RO(armcp_kernel_ver);
373 static DEVICE_ATTR_RO(armcp_ver);
374 static DEVICE_ATTR_RO(cpld_ver);
375 static DEVICE_ATTR_RO(cpucp_kernel_ver);
376 static DEVICE_ATTR_RO(cpucp_ver);
377 static DEVICE_ATTR_RO(device_type);
378 static DEVICE_ATTR_RO(fuse_ver);
379 static DEVICE_ATTR_WO(hard_reset);
380 static DEVICE_ATTR_RO(hard_reset_cnt);
381 static DEVICE_ATTR_RO(infineon_ver);
382 static DEVICE_ATTR_RW(max_power);
383 static DEVICE_ATTR_RO(pci_addr);
384 static DEVICE_ATTR_RO(preboot_btl_ver);
385 static DEVICE_ATTR_WO(soft_reset);
386 static DEVICE_ATTR_RO(soft_reset_cnt);
387 static DEVICE_ATTR_RO(status);
388 static DEVICE_ATTR_RO(thermal_ver);
389 static DEVICE_ATTR_RO(uboot_ver);
391 static struct bin_attribute bin_attr_eeprom = {
392 .attr = {.name = "eeprom", .mode = (0444)},
394 .read = eeprom_read_handler
397 static struct attribute *hl_dev_attrs[] = {
398 &dev_attr_armcp_kernel_ver.attr,
399 &dev_attr_armcp_ver.attr,
400 &dev_attr_cpld_ver.attr,
401 &dev_attr_cpucp_kernel_ver.attr,
402 &dev_attr_cpucp_ver.attr,
403 &dev_attr_device_type.attr,
404 &dev_attr_fuse_ver.attr,
405 &dev_attr_hard_reset.attr,
406 &dev_attr_hard_reset_cnt.attr,
407 &dev_attr_infineon_ver.attr,
408 &dev_attr_max_power.attr,
409 &dev_attr_pci_addr.attr,
410 &dev_attr_preboot_btl_ver.attr,
411 &dev_attr_soft_reset.attr,
412 &dev_attr_soft_reset_cnt.attr,
413 &dev_attr_status.attr,
414 &dev_attr_thermal_ver.attr,
415 &dev_attr_uboot_ver.attr,
419 static struct bin_attribute *hl_dev_bin_attrs[] = {
424 static struct attribute_group hl_dev_attr_group = {
425 .attrs = hl_dev_attrs,
426 .bin_attrs = hl_dev_bin_attrs,
429 static struct attribute_group hl_dev_clks_attr_group;
431 static const struct attribute_group *hl_dev_attr_groups[] = {
433 &hl_dev_clks_attr_group,
437 int hl_sysfs_init(struct hl_device *hdev)
441 if (hdev->asic_type == ASIC_GOYA)
442 hdev->pm_mng_profile = PM_AUTO;
444 hdev->pm_mng_profile = PM_MANUAL;
446 hdev->max_power = hdev->asic_prop.max_power_default;
448 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
450 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
453 "Failed to add groups to device, error %d\n", rc);
460 void hl_sysfs_fini(struct hl_device *hdev)
462 device_remove_groups(hdev->dev, hl_dev_attr_groups);