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 = ARMCP_PACKET_FREQUENCY_CURR_GET <<
25 ARMCP_PKT_CTL_OPCODE_SHIFT;
27 pkt.ctl = ARMCP_PACKET_FREQUENCY_GET <<
28 ARMCP_PKT_CTL_OPCODE_SHIFT;
29 pkt.pll_index = 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 = ARMCP_PACKET_FREQUENCY_SET << ARMCP_PKT_CTL_OPCODE_SHIFT;
52 pkt.pll_index = pll_index;
55 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
56 SET_CLK_PKT_TIMEOUT, NULL);
60 "Failed to set frequency to PLL %d, error %d\n",
64 u64 hl_get_max_power(struct hl_device *hdev)
66 struct armcp_packet pkt;
70 memset(&pkt, 0, sizeof(pkt));
72 pkt.ctl = ARMCP_PACKET_MAX_POWER_GET << ARMCP_PKT_CTL_OPCODE_SHIFT;
74 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
75 SET_PWR_PKT_TIMEOUT, &result);
78 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
85 void hl_set_max_power(struct hl_device *hdev, u64 value)
87 struct armcp_packet pkt;
90 memset(&pkt, 0, sizeof(pkt));
92 pkt.ctl = ARMCP_PACKET_MAX_POWER_SET << ARMCP_PKT_CTL_OPCODE_SHIFT;
95 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
96 SET_PWR_PKT_TIMEOUT, NULL);
99 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
102 static ssize_t pm_mng_profile_show(struct device *dev,
103 struct device_attribute *attr, char *buf)
105 struct hl_device *hdev = dev_get_drvdata(dev);
107 if (hl_device_disabled_or_in_reset(hdev))
110 return sprintf(buf, "%s\n",
111 (hdev->pm_mng_profile == PM_AUTO) ? "auto" :
112 (hdev->pm_mng_profile == PM_MANUAL) ? "manual" :
116 static ssize_t pm_mng_profile_store(struct device *dev,
117 struct device_attribute *attr, const char *buf, size_t count)
119 struct hl_device *hdev = dev_get_drvdata(dev);
121 if (hl_device_disabled_or_in_reset(hdev)) {
126 mutex_lock(&hdev->fd_open_cnt_lock);
128 if (atomic_read(&hdev->fd_open_cnt) > 0) {
130 "Can't change PM profile while user process is opened on the device\n");
135 if (strncmp("auto", buf, strlen("auto")) == 0) {
136 /* Make sure we are in LOW PLL when changing modes */
137 if (hdev->pm_mng_profile == PM_MANUAL) {
138 atomic_set(&hdev->curr_pll_profile, PLL_HIGH);
139 hl_device_set_frequency(hdev, PLL_LOW);
140 hdev->pm_mng_profile = PM_AUTO;
142 } else if (strncmp("manual", buf, strlen("manual")) == 0) {
143 /* Make sure we are in LOW PLL when changing modes */
144 if (hdev->pm_mng_profile == PM_AUTO) {
145 flush_delayed_work(&hdev->work_freq);
146 hdev->pm_mng_profile = PM_MANUAL;
149 dev_err(hdev->dev, "value should be auto or manual\n");
155 mutex_unlock(&hdev->fd_open_cnt_lock);
160 static ssize_t high_pll_show(struct device *dev, struct device_attribute *attr,
163 struct hl_device *hdev = dev_get_drvdata(dev);
165 if (hl_device_disabled_or_in_reset(hdev))
168 return sprintf(buf, "%u\n", hdev->high_pll);
171 static ssize_t high_pll_store(struct device *dev, struct device_attribute *attr,
172 const char *buf, size_t count)
174 struct hl_device *hdev = dev_get_drvdata(dev);
178 if (hl_device_disabled_or_in_reset(hdev)) {
183 rc = kstrtoul(buf, 0, &value);
190 hdev->high_pll = value;
196 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
199 struct hl_device *hdev = dev_get_drvdata(dev);
201 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
204 static ssize_t armcp_kernel_ver_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
207 struct hl_device *hdev = dev_get_drvdata(dev);
209 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.kernel_version);
212 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
215 struct hl_device *hdev = dev_get_drvdata(dev);
217 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.armcp_version);
220 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
223 struct hl_device *hdev = dev_get_drvdata(dev);
225 return sprintf(buf, "0x%08x\n",
226 hdev->asic_prop.armcp_info.cpld_version);
229 static ssize_t infineon_ver_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct hl_device *hdev = dev_get_drvdata(dev);
234 return sprintf(buf, "0x%04x\n",
235 hdev->asic_prop.armcp_info.infineon_version);
238 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
241 struct hl_device *hdev = dev_get_drvdata(dev);
243 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.fuse_version);
246 static ssize_t thermal_ver_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
249 struct hl_device *hdev = dev_get_drvdata(dev);
251 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.thermal_version);
254 static ssize_t preboot_btl_ver_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct hl_device *hdev = dev_get_drvdata(dev);
259 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
262 static ssize_t soft_reset_store(struct device *dev,
263 struct device_attribute *attr, const char *buf,
266 struct hl_device *hdev = dev_get_drvdata(dev);
270 rc = kstrtoul(buf, 0, &value);
277 hl_device_reset(hdev, false, false);
283 static ssize_t hard_reset_store(struct device *dev,
284 struct device_attribute *attr,
285 const char *buf, size_t count)
287 struct hl_device *hdev = dev_get_drvdata(dev);
291 rc = kstrtoul(buf, 0, &value);
298 hl_device_reset(hdev, true, false);
304 static ssize_t device_type_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct hl_device *hdev = dev_get_drvdata(dev);
310 switch (hdev->asic_type) {
315 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
320 return sprintf(buf, "%s\n", str);
323 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
326 struct hl_device *hdev = dev_get_drvdata(dev);
328 /* Use dummy, fixed address for simulator */
330 return sprintf(buf, "0000:%02d:00.0\n", hdev->id);
332 return sprintf(buf, "%04x:%02x:%02x.%x\n",
333 pci_domain_nr(hdev->pdev->bus),
334 hdev->pdev->bus->number,
335 PCI_SLOT(hdev->pdev->devfn),
336 PCI_FUNC(hdev->pdev->devfn));
339 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
342 struct hl_device *hdev = dev_get_drvdata(dev);
345 if (atomic_read(&hdev->in_reset))
347 else if (hdev->disabled)
352 return sprintf(buf, "%s\n", str);
355 static ssize_t write_open_cnt_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
358 struct hl_device *hdev = dev_get_drvdata(dev);
360 return sprintf(buf, "%d\n", hdev->user_ctx ? 1 : 0);
363 static ssize_t soft_reset_cnt_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct hl_device *hdev = dev_get_drvdata(dev);
368 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
371 static ssize_t hard_reset_cnt_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
374 struct hl_device *hdev = dev_get_drvdata(dev);
376 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
379 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
382 struct hl_device *hdev = dev_get_drvdata(dev);
385 if (hl_device_disabled_or_in_reset(hdev))
388 val = hl_get_max_power(hdev);
390 return sprintf(buf, "%lu\n", val);
393 static ssize_t max_power_store(struct device *dev,
394 struct device_attribute *attr, const char *buf, size_t count)
396 struct hl_device *hdev = dev_get_drvdata(dev);
400 if (hl_device_disabled_or_in_reset(hdev)) {
405 rc = kstrtoul(buf, 0, &value);
412 hdev->max_power = value;
413 hl_set_max_power(hdev, value);
419 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
420 struct bin_attribute *attr, char *buf, loff_t offset,
423 struct device *dev = container_of(kobj, struct device, kobj);
424 struct hl_device *hdev = dev_get_drvdata(dev);
431 data = kzalloc(max_size, GFP_KERNEL);
435 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
439 memcpy(buf, data, max_size);
447 static DEVICE_ATTR_RO(armcp_kernel_ver);
448 static DEVICE_ATTR_RO(armcp_ver);
449 static DEVICE_ATTR_RO(cpld_ver);
450 static DEVICE_ATTR_RO(device_type);
451 static DEVICE_ATTR_RO(fuse_ver);
452 static DEVICE_ATTR_WO(hard_reset);
453 static DEVICE_ATTR_RO(hard_reset_cnt);
454 static DEVICE_ATTR_RW(high_pll);
455 static DEVICE_ATTR_RO(infineon_ver);
456 static DEVICE_ATTR_RW(max_power);
457 static DEVICE_ATTR_RO(pci_addr);
458 static DEVICE_ATTR_RW(pm_mng_profile);
459 static DEVICE_ATTR_RO(preboot_btl_ver);
460 static DEVICE_ATTR_WO(soft_reset);
461 static DEVICE_ATTR_RO(soft_reset_cnt);
462 static DEVICE_ATTR_RO(status);
463 static DEVICE_ATTR_RO(thermal_ver);
464 static DEVICE_ATTR_RO(uboot_ver);
465 static DEVICE_ATTR_RO(write_open_cnt);
467 static struct bin_attribute bin_attr_eeprom = {
468 .attr = {.name = "eeprom", .mode = (0444)},
470 .read = eeprom_read_handler
473 static struct attribute *hl_dev_attrs[] = {
474 &dev_attr_armcp_kernel_ver.attr,
475 &dev_attr_armcp_ver.attr,
476 &dev_attr_cpld_ver.attr,
477 &dev_attr_device_type.attr,
478 &dev_attr_fuse_ver.attr,
479 &dev_attr_hard_reset.attr,
480 &dev_attr_hard_reset_cnt.attr,
481 &dev_attr_high_pll.attr,
482 &dev_attr_infineon_ver.attr,
483 &dev_attr_max_power.attr,
484 &dev_attr_pci_addr.attr,
485 &dev_attr_pm_mng_profile.attr,
486 &dev_attr_preboot_btl_ver.attr,
487 &dev_attr_soft_reset.attr,
488 &dev_attr_soft_reset_cnt.attr,
489 &dev_attr_status.attr,
490 &dev_attr_thermal_ver.attr,
491 &dev_attr_uboot_ver.attr,
492 &dev_attr_write_open_cnt.attr,
496 static struct bin_attribute *hl_dev_bin_attrs[] = {
501 static struct attribute_group hl_dev_attr_group = {
502 .attrs = hl_dev_attrs,
503 .bin_attrs = hl_dev_bin_attrs,
506 static struct attribute_group hl_dev_clks_attr_group;
508 static const struct attribute_group *hl_dev_attr_groups[] = {
510 &hl_dev_clks_attr_group,
514 int hl_sysfs_init(struct hl_device *hdev)
518 hdev->pm_mng_profile = PM_AUTO;
519 hdev->max_power = hdev->asic_prop.max_power_default;
521 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
523 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
526 "Failed to add groups to device, error %d\n", rc);
533 void hl_sysfs_fini(struct hl_device *hdev)
535 device_remove_groups(hdev->dev, hl_dev_attr_groups);