1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
12 #define SET_CLK_PKT_TIMEOUT 1000000 /* 1s */
13 #define SET_PWR_PKT_TIMEOUT 1000000 /* 1s */
15 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr)
17 struct armcp_packet pkt;
21 memset(&pkt, 0, sizeof(pkt));
24 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_FREQUENCY_CURR_GET <<
25 ARMCP_PKT_CTL_OPCODE_SHIFT);
27 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_FREQUENCY_GET <<
28 ARMCP_PKT_CTL_OPCODE_SHIFT);
29 pkt.pll_index = __cpu_to_le32(pll_index);
31 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
32 SET_CLK_PKT_TIMEOUT, &result);
36 "Failed to get frequency of PLL %d, error %d\n",
44 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
46 struct armcp_packet pkt;
49 memset(&pkt, 0, sizeof(pkt));
51 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_FREQUENCY_SET <<
52 ARMCP_PKT_CTL_OPCODE_SHIFT);
53 pkt.pll_index = __cpu_to_le32(pll_index);
54 pkt.value = __cpu_to_le64(freq);
56 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
57 SET_CLK_PKT_TIMEOUT, NULL);
61 "Failed to set frequency to PLL %d, error %d\n",
65 u64 hl_get_max_power(struct hl_device *hdev)
67 struct armcp_packet pkt;
71 memset(&pkt, 0, sizeof(pkt));
73 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_MAX_POWER_GET <<
74 ARMCP_PKT_CTL_OPCODE_SHIFT);
76 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
77 SET_PWR_PKT_TIMEOUT, &result);
80 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
87 void hl_set_max_power(struct hl_device *hdev, u64 value)
89 struct armcp_packet pkt;
92 memset(&pkt, 0, sizeof(pkt));
94 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_MAX_POWER_SET <<
95 ARMCP_PKT_CTL_OPCODE_SHIFT);
96 pkt.value = __cpu_to_le64(value);
98 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
99 SET_PWR_PKT_TIMEOUT, NULL);
102 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
105 static ssize_t pm_mng_profile_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 struct hl_device *hdev = dev_get_drvdata(dev);
110 if (hl_device_disabled_or_in_reset(hdev))
113 return sprintf(buf, "%s\n",
114 (hdev->pm_mng_profile == PM_AUTO) ? "auto" :
115 (hdev->pm_mng_profile == PM_MANUAL) ? "manual" :
119 static ssize_t pm_mng_profile_store(struct device *dev,
120 struct device_attribute *attr, const char *buf, size_t count)
122 struct hl_device *hdev = dev_get_drvdata(dev);
124 if (hl_device_disabled_or_in_reset(hdev)) {
129 mutex_lock(&hdev->fd_open_cnt_lock);
131 if (atomic_read(&hdev->fd_open_cnt) > 0) {
133 "Can't change PM profile while user process is opened on the device\n");
138 if (strncmp("auto", buf, strlen("auto")) == 0) {
139 /* Make sure we are in LOW PLL when changing modes */
140 if (hdev->pm_mng_profile == PM_MANUAL) {
141 atomic_set(&hdev->curr_pll_profile, PLL_HIGH);
142 hl_device_set_frequency(hdev, PLL_LOW);
143 hdev->pm_mng_profile = PM_AUTO;
145 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
146 /* Make sure we are in LOW PLL when changing modes */
147 if (hdev->pm_mng_profile == PM_AUTO) {
148 flush_delayed_work(&hdev->work_freq);
149 hdev->pm_mng_profile = PM_MANUAL;
152 dev_err(hdev->dev, "value should be auto or manual\n");
158 mutex_unlock(&hdev->fd_open_cnt_lock);
163 static ssize_t high_pll_show(struct device *dev, struct device_attribute *attr,
166 struct hl_device *hdev = dev_get_drvdata(dev);
168 if (hl_device_disabled_or_in_reset(hdev))
171 return sprintf(buf, "%u\n", hdev->high_pll);
174 static ssize_t high_pll_store(struct device *dev, struct device_attribute *attr,
175 const char *buf, size_t count)
177 struct hl_device *hdev = dev_get_drvdata(dev);
181 if (hl_device_disabled_or_in_reset(hdev)) {
186 rc = kstrtoul(buf, 0, &value);
193 hdev->high_pll = value;
199 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
202 struct hl_device *hdev = dev_get_drvdata(dev);
204 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
207 static ssize_t armcp_kernel_ver_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
210 struct hl_device *hdev = dev_get_drvdata(dev);
212 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.kernel_version);
215 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
218 struct hl_device *hdev = dev_get_drvdata(dev);
220 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.armcp_version);
223 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
226 struct hl_device *hdev = dev_get_drvdata(dev);
228 return sprintf(buf, "0x%08x\n",
229 hdev->asic_prop.armcp_info.cpld_version);
232 static ssize_t infineon_ver_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct hl_device *hdev = dev_get_drvdata(dev);
237 return sprintf(buf, "0x%04x\n",
238 hdev->asic_prop.armcp_info.infineon_version);
241 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
244 struct hl_device *hdev = dev_get_drvdata(dev);
246 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.fuse_version);
249 static ssize_t thermal_ver_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
252 struct hl_device *hdev = dev_get_drvdata(dev);
254 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.thermal_version);
257 static ssize_t preboot_btl_ver_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
260 struct hl_device *hdev = dev_get_drvdata(dev);
262 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
265 static ssize_t soft_reset_store(struct device *dev,
266 struct device_attribute *attr, const char *buf,
269 struct hl_device *hdev = dev_get_drvdata(dev);
273 rc = kstrtoul(buf, 0, &value);
280 hl_device_reset(hdev, false, false);
286 static ssize_t hard_reset_store(struct device *dev,
287 struct device_attribute *attr,
288 const char *buf, size_t count)
290 struct hl_device *hdev = dev_get_drvdata(dev);
294 rc = kstrtoul(buf, 0, &value);
301 hl_device_reset(hdev, true, false);
307 static ssize_t device_type_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct hl_device *hdev = dev_get_drvdata(dev);
313 switch (hdev->asic_type) {
318 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
323 return sprintf(buf, "%s\n", str);
326 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
329 struct hl_device *hdev = dev_get_drvdata(dev);
331 /* Use dummy, fixed address for simulator */
333 return sprintf(buf, "0000:%02d:00.0\n", hdev->id);
335 return sprintf(buf, "%04x:%02x:%02x.%x\n",
336 pci_domain_nr(hdev->pdev->bus),
337 hdev->pdev->bus->number,
338 PCI_SLOT(hdev->pdev->devfn),
339 PCI_FUNC(hdev->pdev->devfn));
342 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
345 struct hl_device *hdev = dev_get_drvdata(dev);
348 if (atomic_read(&hdev->in_reset))
350 else if (hdev->disabled)
355 return sprintf(buf, "%s\n", str);
358 static ssize_t write_open_cnt_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 struct hl_device *hdev = dev_get_drvdata(dev);
363 return sprintf(buf, "%d\n", hdev->user_ctx ? 1 : 0);
366 static ssize_t soft_reset_cnt_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 struct hl_device *hdev = dev_get_drvdata(dev);
371 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
374 static ssize_t hard_reset_cnt_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 struct hl_device *hdev = dev_get_drvdata(dev);
379 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
382 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
385 struct hl_device *hdev = dev_get_drvdata(dev);
388 if (hl_device_disabled_or_in_reset(hdev))
391 val = hl_get_max_power(hdev);
393 return sprintf(buf, "%lu\n", val);
396 static ssize_t max_power_store(struct device *dev,
397 struct device_attribute *attr, const char *buf, size_t count)
399 struct hl_device *hdev = dev_get_drvdata(dev);
403 if (hl_device_disabled_or_in_reset(hdev)) {
408 rc = kstrtoul(buf, 0, &value);
415 hdev->max_power = value;
416 hl_set_max_power(hdev, value);
422 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
423 struct bin_attribute *attr, char *buf, loff_t offset,
426 struct device *dev = container_of(kobj, struct device, kobj);
427 struct hl_device *hdev = dev_get_drvdata(dev);
434 data = kzalloc(max_size, GFP_KERNEL);
438 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
442 memcpy(buf, data, max_size);
450 static DEVICE_ATTR_RO(armcp_kernel_ver);
451 static DEVICE_ATTR_RO(armcp_ver);
452 static DEVICE_ATTR_RO(cpld_ver);
453 static DEVICE_ATTR_RO(device_type);
454 static DEVICE_ATTR_RO(fuse_ver);
455 static DEVICE_ATTR_WO(hard_reset);
456 static DEVICE_ATTR_RO(hard_reset_cnt);
457 static DEVICE_ATTR_RW(high_pll);
458 static DEVICE_ATTR_RO(infineon_ver);
459 static DEVICE_ATTR_RW(max_power);
460 static DEVICE_ATTR_RO(pci_addr);
461 static DEVICE_ATTR_RW(pm_mng_profile);
462 static DEVICE_ATTR_RO(preboot_btl_ver);
463 static DEVICE_ATTR_WO(soft_reset);
464 static DEVICE_ATTR_RO(soft_reset_cnt);
465 static DEVICE_ATTR_RO(status);
466 static DEVICE_ATTR_RO(thermal_ver);
467 static DEVICE_ATTR_RO(uboot_ver);
468 static DEVICE_ATTR_RO(write_open_cnt);
470 static struct bin_attribute bin_attr_eeprom = {
471 .attr = {.name = "eeprom", .mode = (0444)},
473 .read = eeprom_read_handler
476 static struct attribute *hl_dev_attrs[] = {
477 &dev_attr_armcp_kernel_ver.attr,
478 &dev_attr_armcp_ver.attr,
479 &dev_attr_cpld_ver.attr,
480 &dev_attr_device_type.attr,
481 &dev_attr_fuse_ver.attr,
482 &dev_attr_hard_reset.attr,
483 &dev_attr_hard_reset_cnt.attr,
484 &dev_attr_high_pll.attr,
485 &dev_attr_infineon_ver.attr,
486 &dev_attr_max_power.attr,
487 &dev_attr_pci_addr.attr,
488 &dev_attr_pm_mng_profile.attr,
489 &dev_attr_preboot_btl_ver.attr,
490 &dev_attr_soft_reset.attr,
491 &dev_attr_soft_reset_cnt.attr,
492 &dev_attr_status.attr,
493 &dev_attr_thermal_ver.attr,
494 &dev_attr_uboot_ver.attr,
495 &dev_attr_write_open_cnt.attr,
499 static struct bin_attribute *hl_dev_bin_attrs[] = {
504 static struct attribute_group hl_dev_attr_group = {
505 .attrs = hl_dev_attrs,
506 .bin_attrs = hl_dev_bin_attrs,
509 static struct attribute_group hl_dev_clks_attr_group;
511 static const struct attribute_group *hl_dev_attr_groups[] = {
513 &hl_dev_clks_attr_group,
517 int hl_sysfs_init(struct hl_device *hdev)
521 hdev->pm_mng_profile = PM_AUTO;
522 hdev->max_power = hdev->asic_prop.max_power_default;
524 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
526 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
529 "Failed to add groups to device, error %d\n", rc);
536 void hl_sysfs_fini(struct hl_device *hdev)
538 device_remove_groups(hdev->dev, hl_dev_attr_groups);