Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / misc / habanalabs / common / sysfs.c
blob4366d8f9384290437e05fe23d06044f0bda9e50c
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 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr)
14 struct cpucp_packet pkt;
15 u64 result;
16 int rc;
18 memset(&pkt, 0, sizeof(pkt));
20 if (curr)
21 pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET <<
22 CPUCP_PKT_CTL_OPCODE_SHIFT);
23 else
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),
29 0, &result);
31 if (rc) {
32 dev_err(hdev->dev,
33 "Failed to get frequency of PLL %d, error %d\n",
34 pll_index, rc);
35 return rc;
38 return (long) result;
41 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
43 struct cpucp_packet pkt;
44 int rc;
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),
54 0, NULL);
56 if (rc)
57 dev_err(hdev->dev,
58 "Failed to set frequency to PLL %d, error %d\n",
59 pll_index, rc);
62 u64 hl_get_max_power(struct hl_device *hdev)
64 struct cpucp_packet pkt;
65 u64 result;
66 int rc;
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),
74 0, &result);
76 if (rc) {
77 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
78 return (u64) rc;
81 return result;
84 void hl_set_max_power(struct hl_device *hdev)
86 struct cpucp_packet pkt;
87 int rc;
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),
96 0, NULL);
98 if (rc)
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,
103 char *buf)
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,
119 char *buf)
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,
127 char *buf)
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,
144 char *buf)
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,
161 char *buf)
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,
186 size_t count)
188 struct hl_device *hdev = dev_get_drvdata(dev);
189 long value;
190 int rc;
192 rc = kstrtoul(buf, 0, &value);
194 if (rc) {
195 count = -EINVAL;
196 goto out;
199 if (!hdev->supports_soft_reset) {
200 dev_err(hdev->dev, "Device does not support soft-reset\n");
201 goto out;
204 dev_warn(hdev->dev, "Soft-Reset requested through sysfs\n");
206 hl_device_reset(hdev, false, false);
208 out:
209 return count;
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);
217 long value;
218 int rc;
220 rc = kstrtoul(buf, 0, &value);
222 if (rc) {
223 count = -EINVAL;
224 goto out;
227 dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
229 hl_device_reset(hdev, true, false);
231 out:
232 return count;
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);
239 char *str;
241 switch (hdev->asic_type) {
242 case ASIC_GOYA:
243 str = "GOYA";
244 break;
245 case ASIC_GAUDI:
246 str = "GAUDI";
247 break;
248 default:
249 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
250 hdev->asic_type);
251 return -EINVAL;
254 return sprintf(buf, "%s\n", str);
257 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
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,
270 char *buf)
272 struct hl_device *hdev = dev_get_drvdata(dev);
273 char *str;
275 if (atomic_read(&hdev->in_reset))
276 str = "In reset";
277 else if (hdev->disabled)
278 str = "Malfunction";
279 else if (hdev->needs_reset)
280 str = "Needs Reset";
281 else
282 str = "Operational";
284 return sprintf(buf, "%s\n", str);
287 static ssize_t soft_reset_cnt_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
290 struct hl_device *hdev = dev_get_drvdata(dev);
292 return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
295 static ssize_t hard_reset_cnt_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
298 struct hl_device *hdev = dev_get_drvdata(dev);
300 return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
303 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
304 char *buf)
306 struct hl_device *hdev = dev_get_drvdata(dev);
307 long val;
309 if (!hl_device_operational(hdev, NULL))
310 return -ENODEV;
312 val = hl_get_max_power(hdev);
314 return sprintf(buf, "%lu\n", val);
317 static ssize_t max_power_store(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
320 struct hl_device *hdev = dev_get_drvdata(dev);
321 unsigned long value;
322 int rc;
324 if (!hl_device_operational(hdev, NULL)) {
325 count = -ENODEV;
326 goto out;
329 rc = kstrtoul(buf, 0, &value);
331 if (rc) {
332 count = -EINVAL;
333 goto out;
336 hdev->max_power = value;
337 hl_set_max_power(hdev);
339 out:
340 return count;
343 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
344 struct bin_attribute *attr, char *buf, loff_t offset,
345 size_t max_size)
347 struct device *dev = container_of(kobj, struct device, kobj);
348 struct hl_device *hdev = dev_get_drvdata(dev);
349 char *data;
350 int rc;
352 if (!hl_device_operational(hdev, NULL))
353 return -ENODEV;
355 if (!max_size)
356 return -EINVAL;
358 data = kzalloc(max_size, GFP_KERNEL);
359 if (!data)
360 return -ENOMEM;
362 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
363 if (rc)
364 goto out;
366 memcpy(buf, data, max_size);
368 out:
369 kfree(data);
371 return max_size;
374 static DEVICE_ATTR_RO(armcp_kernel_ver);
375 static DEVICE_ATTR_RO(armcp_ver);
376 static DEVICE_ATTR_RO(cpld_ver);
377 static DEVICE_ATTR_RO(cpucp_kernel_ver);
378 static DEVICE_ATTR_RO(cpucp_ver);
379 static DEVICE_ATTR_RO(device_type);
380 static DEVICE_ATTR_RO(fuse_ver);
381 static DEVICE_ATTR_WO(hard_reset);
382 static DEVICE_ATTR_RO(hard_reset_cnt);
383 static DEVICE_ATTR_RO(infineon_ver);
384 static DEVICE_ATTR_RW(max_power);
385 static DEVICE_ATTR_RO(pci_addr);
386 static DEVICE_ATTR_RO(preboot_btl_ver);
387 static DEVICE_ATTR_WO(soft_reset);
388 static DEVICE_ATTR_RO(soft_reset_cnt);
389 static DEVICE_ATTR_RO(status);
390 static DEVICE_ATTR_RO(thermal_ver);
391 static DEVICE_ATTR_RO(uboot_ver);
393 static struct bin_attribute bin_attr_eeprom = {
394 .attr = {.name = "eeprom", .mode = (0444)},
395 .size = PAGE_SIZE,
396 .read = eeprom_read_handler
399 static struct attribute *hl_dev_attrs[] = {
400 &dev_attr_armcp_kernel_ver.attr,
401 &dev_attr_armcp_ver.attr,
402 &dev_attr_cpld_ver.attr,
403 &dev_attr_cpucp_kernel_ver.attr,
404 &dev_attr_cpucp_ver.attr,
405 &dev_attr_device_type.attr,
406 &dev_attr_fuse_ver.attr,
407 &dev_attr_hard_reset.attr,
408 &dev_attr_hard_reset_cnt.attr,
409 &dev_attr_infineon_ver.attr,
410 &dev_attr_max_power.attr,
411 &dev_attr_pci_addr.attr,
412 &dev_attr_preboot_btl_ver.attr,
413 &dev_attr_soft_reset.attr,
414 &dev_attr_soft_reset_cnt.attr,
415 &dev_attr_status.attr,
416 &dev_attr_thermal_ver.attr,
417 &dev_attr_uboot_ver.attr,
418 NULL,
421 static struct bin_attribute *hl_dev_bin_attrs[] = {
422 &bin_attr_eeprom,
423 NULL
426 static struct attribute_group hl_dev_attr_group = {
427 .attrs = hl_dev_attrs,
428 .bin_attrs = hl_dev_bin_attrs,
431 static struct attribute_group hl_dev_clks_attr_group;
433 static const struct attribute_group *hl_dev_attr_groups[] = {
434 &hl_dev_attr_group,
435 &hl_dev_clks_attr_group,
436 NULL,
439 int hl_sysfs_init(struct hl_device *hdev)
441 int rc;
443 if (hdev->asic_type == ASIC_GOYA)
444 hdev->pm_mng_profile = PM_AUTO;
445 else
446 hdev->pm_mng_profile = PM_MANUAL;
448 hdev->max_power = hdev->asic_prop.max_power_default;
450 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
452 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
453 if (rc) {
454 dev_err(hdev->dev,
455 "Failed to add groups to device, error %d\n", rc);
456 return rc;
459 return 0;
462 void hl_sysfs_fini(struct hl_device *hdev)
464 device_remove_groups(hdev->dev, hl_dev_attr_groups);