gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / misc / habanalabs / sysfs.c
blob4cd622b017b9a9daa28dccf5b3263a0231d09876
1 // SPDX-License-Identifier: GPL-2.0
3 /*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
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;
18 long result;
19 int rc;
21 memset(&pkt, 0, sizeof(pkt));
23 if (curr)
24 pkt.ctl = cpu_to_le32(ARMCP_PACKET_FREQUENCY_CURR_GET <<
25 ARMCP_PKT_CTL_OPCODE_SHIFT);
26 else
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);
34 if (rc) {
35 dev_err(hdev->dev,
36 "Failed to get frequency of PLL %d, error %d\n",
37 pll_index, rc);
38 result = rc;
41 return result;
44 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
46 struct armcp_packet pkt;
47 int rc;
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);
59 if (rc)
60 dev_err(hdev->dev,
61 "Failed to set frequency to PLL %d, error %d\n",
62 pll_index, rc);
65 u64 hl_get_max_power(struct hl_device *hdev)
67 struct armcp_packet pkt;
68 long result;
69 int rc;
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);
79 if (rc) {
80 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
81 result = rc;
84 return result;
87 void hl_set_max_power(struct hl_device *hdev, u64 value)
89 struct armcp_packet pkt;
90 int rc;
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);
101 if (rc)
102 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
105 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
106 char *buf)
108 struct hl_device *hdev = dev_get_drvdata(dev);
110 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
113 static ssize_t armcp_kernel_ver_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
116 struct hl_device *hdev = dev_get_drvdata(dev);
118 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.kernel_version);
121 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
122 char *buf)
124 struct hl_device *hdev = dev_get_drvdata(dev);
126 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.armcp_version);
129 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
130 char *buf)
132 struct hl_device *hdev = dev_get_drvdata(dev);
134 return sprintf(buf, "0x%08x\n",
135 hdev->asic_prop.armcp_info.cpld_version);
138 static ssize_t infineon_ver_show(struct device *dev,
139 struct device_attribute *attr, char *buf)
141 struct hl_device *hdev = dev_get_drvdata(dev);
143 return sprintf(buf, "0x%04x\n",
144 hdev->asic_prop.armcp_info.infineon_version);
147 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
148 char *buf)
150 struct hl_device *hdev = dev_get_drvdata(dev);
152 return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.fuse_version);
155 static ssize_t thermal_ver_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
158 struct hl_device *hdev = dev_get_drvdata(dev);
160 return sprintf(buf, "%s", hdev->asic_prop.armcp_info.thermal_version);
163 static ssize_t preboot_btl_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, "%s\n", hdev->asic_prop.preboot_ver);
171 static ssize_t soft_reset_store(struct device *dev,
172 struct device_attribute *attr, const char *buf,
173 size_t count)
175 struct hl_device *hdev = dev_get_drvdata(dev);
176 long value;
177 int rc;
179 rc = kstrtoul(buf, 0, &value);
181 if (rc) {
182 count = -EINVAL;
183 goto out;
186 hl_device_reset(hdev, false, false);
188 out:
189 return count;
192 static ssize_t hard_reset_store(struct device *dev,
193 struct device_attribute *attr,
194 const char *buf, size_t count)
196 struct hl_device *hdev = dev_get_drvdata(dev);
197 long value;
198 int rc;
200 rc = kstrtoul(buf, 0, &value);
202 if (rc) {
203 count = -EINVAL;
204 goto out;
207 hl_device_reset(hdev, true, false);
209 out:
210 return count;
213 static ssize_t device_type_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct hl_device *hdev = dev_get_drvdata(dev);
217 char *str;
219 switch (hdev->asic_type) {
220 case ASIC_GOYA:
221 str = "GOYA";
222 break;
223 default:
224 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
225 hdev->asic_type);
226 return -EINVAL;
229 return sprintf(buf, "%s\n", str);
232 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
233 char *buf)
235 struct hl_device *hdev = dev_get_drvdata(dev);
237 return sprintf(buf, "%04x:%02x:%02x.%x\n",
238 pci_domain_nr(hdev->pdev->bus),
239 hdev->pdev->bus->number,
240 PCI_SLOT(hdev->pdev->devfn),
241 PCI_FUNC(hdev->pdev->devfn));
244 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
245 char *buf)
247 struct hl_device *hdev = dev_get_drvdata(dev);
248 char *str;
250 if (atomic_read(&hdev->in_reset))
251 str = "In reset";
252 else if (hdev->disabled)
253 str = "Malfunction";
254 else
255 str = "Operational";
257 return sprintf(buf, "%s\n", str);
260 static ssize_t soft_reset_cnt_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct hl_device *hdev = dev_get_drvdata(dev);
265 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
268 static ssize_t hard_reset_cnt_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 struct hl_device *hdev = dev_get_drvdata(dev);
273 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
276 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
277 char *buf)
279 struct hl_device *hdev = dev_get_drvdata(dev);
280 long val;
282 if (hl_device_disabled_or_in_reset(hdev))
283 return -ENODEV;
285 val = hl_get_max_power(hdev);
287 return sprintf(buf, "%lu\n", val);
290 static ssize_t max_power_store(struct device *dev,
291 struct device_attribute *attr, const char *buf, size_t count)
293 struct hl_device *hdev = dev_get_drvdata(dev);
294 unsigned long value;
295 int rc;
297 if (hl_device_disabled_or_in_reset(hdev)) {
298 count = -ENODEV;
299 goto out;
302 rc = kstrtoul(buf, 0, &value);
304 if (rc) {
305 count = -EINVAL;
306 goto out;
309 hdev->max_power = value;
310 hl_set_max_power(hdev, value);
312 out:
313 return count;
316 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
317 struct bin_attribute *attr, char *buf, loff_t offset,
318 size_t max_size)
320 struct device *dev = container_of(kobj, struct device, kobj);
321 struct hl_device *hdev = dev_get_drvdata(dev);
322 char *data;
323 int rc;
325 if (!max_size)
326 return -EINVAL;
328 data = kzalloc(max_size, GFP_KERNEL);
329 if (!data)
330 return -ENOMEM;
332 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
333 if (rc)
334 goto out;
336 memcpy(buf, data, max_size);
338 out:
339 kfree(data);
341 return max_size;
344 static DEVICE_ATTR_RO(armcp_kernel_ver);
345 static DEVICE_ATTR_RO(armcp_ver);
346 static DEVICE_ATTR_RO(cpld_ver);
347 static DEVICE_ATTR_RO(device_type);
348 static DEVICE_ATTR_RO(fuse_ver);
349 static DEVICE_ATTR_WO(hard_reset);
350 static DEVICE_ATTR_RO(hard_reset_cnt);
351 static DEVICE_ATTR_RO(infineon_ver);
352 static DEVICE_ATTR_RW(max_power);
353 static DEVICE_ATTR_RO(pci_addr);
354 static DEVICE_ATTR_RO(preboot_btl_ver);
355 static DEVICE_ATTR_WO(soft_reset);
356 static DEVICE_ATTR_RO(soft_reset_cnt);
357 static DEVICE_ATTR_RO(status);
358 static DEVICE_ATTR_RO(thermal_ver);
359 static DEVICE_ATTR_RO(uboot_ver);
361 static struct bin_attribute bin_attr_eeprom = {
362 .attr = {.name = "eeprom", .mode = (0444)},
363 .size = PAGE_SIZE,
364 .read = eeprom_read_handler
367 static struct attribute *hl_dev_attrs[] = {
368 &dev_attr_armcp_kernel_ver.attr,
369 &dev_attr_armcp_ver.attr,
370 &dev_attr_cpld_ver.attr,
371 &dev_attr_device_type.attr,
372 &dev_attr_fuse_ver.attr,
373 &dev_attr_hard_reset.attr,
374 &dev_attr_hard_reset_cnt.attr,
375 &dev_attr_infineon_ver.attr,
376 &dev_attr_max_power.attr,
377 &dev_attr_pci_addr.attr,
378 &dev_attr_preboot_btl_ver.attr,
379 &dev_attr_soft_reset.attr,
380 &dev_attr_soft_reset_cnt.attr,
381 &dev_attr_status.attr,
382 &dev_attr_thermal_ver.attr,
383 &dev_attr_uboot_ver.attr,
384 NULL,
387 static struct bin_attribute *hl_dev_bin_attrs[] = {
388 &bin_attr_eeprom,
389 NULL
392 static struct attribute_group hl_dev_attr_group = {
393 .attrs = hl_dev_attrs,
394 .bin_attrs = hl_dev_bin_attrs,
397 static struct attribute_group hl_dev_clks_attr_group;
399 static const struct attribute_group *hl_dev_attr_groups[] = {
400 &hl_dev_attr_group,
401 &hl_dev_clks_attr_group,
402 NULL,
405 int hl_sysfs_init(struct hl_device *hdev)
407 int rc;
409 hdev->pm_mng_profile = PM_AUTO;
410 hdev->max_power = hdev->asic_prop.max_power_default;
412 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
414 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
415 if (rc) {
416 dev_err(hdev->dev,
417 "Failed to add groups to device, error %d\n", rc);
418 return rc;
421 return 0;
424 void hl_sysfs_fini(struct hl_device *hdev)
426 device_remove_groups(hdev->dev, hl_dev_attr_groups);