habanalabs: check for DMA errors when clearing memory
[linux-2.6-microblaze.git] / drivers / misc / habanalabs / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright 2016-2019 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7
8 #include "habanalabs.h"
9
10 #include <linux/pci.h>
11
12 #define SET_CLK_PKT_TIMEOUT     1000000 /* 1s */
13 #define SET_PWR_PKT_TIMEOUT     1000000 /* 1s */
14
15 long hl_get_frequency(struct hl_device *hdev, u32 pll_index, bool curr)
16 {
17         struct armcp_packet pkt;
18         long result;
19         int rc;
20
21         memset(&pkt, 0, sizeof(pkt));
22
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);
30
31         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
32                                                 SET_CLK_PKT_TIMEOUT, &result);
33
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;
39         }
40
41         return result;
42 }
43
44 void hl_set_frequency(struct hl_device *hdev, u32 pll_index, u64 freq)
45 {
46         struct armcp_packet pkt;
47         int rc;
48
49         memset(&pkt, 0, sizeof(pkt));
50
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);
55
56         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
57                                         SET_CLK_PKT_TIMEOUT, NULL);
58
59         if (rc)
60                 dev_err(hdev->dev,
61                         "Failed to set frequency to PLL %d, error %d\n",
62                         pll_index, rc);
63 }
64
65 u64 hl_get_max_power(struct hl_device *hdev)
66 {
67         struct armcp_packet pkt;
68         long result;
69         int rc;
70
71         memset(&pkt, 0, sizeof(pkt));
72
73         pkt.ctl = cpu_to_le32(ARMCP_PACKET_MAX_POWER_GET <<
74                                 ARMCP_PKT_CTL_OPCODE_SHIFT);
75
76         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
77                                                 SET_PWR_PKT_TIMEOUT, &result);
78
79         if (rc) {
80                 dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
81                 result = rc;
82         }
83
84         return result;
85 }
86
87 void hl_set_max_power(struct hl_device *hdev, u64 value)
88 {
89         struct armcp_packet pkt;
90         int rc;
91
92         memset(&pkt, 0, sizeof(pkt));
93
94         pkt.ctl = cpu_to_le32(ARMCP_PACKET_MAX_POWER_SET <<
95                                 ARMCP_PKT_CTL_OPCODE_SHIFT);
96         pkt.value = cpu_to_le64(value);
97
98         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
99                                         SET_PWR_PKT_TIMEOUT, NULL);
100
101         if (rc)
102                 dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
103 }
104
105 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
106                                 char *buf)
107 {
108         struct hl_device *hdev = dev_get_drvdata(dev);
109
110         return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
111 }
112
113 static ssize_t armcp_kernel_ver_show(struct device *dev,
114                                 struct device_attribute *attr, char *buf)
115 {
116         struct hl_device *hdev = dev_get_drvdata(dev);
117
118         return sprintf(buf, "%s", hdev->asic_prop.armcp_info.kernel_version);
119 }
120
121 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
122                                 char *buf)
123 {
124         struct hl_device *hdev = dev_get_drvdata(dev);
125
126         return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.armcp_version);
127 }
128
129 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
130                                 char *buf)
131 {
132         struct hl_device *hdev = dev_get_drvdata(dev);
133
134         return sprintf(buf, "0x%08x\n",
135                         hdev->asic_prop.armcp_info.cpld_version);
136 }
137
138 static ssize_t infineon_ver_show(struct device *dev,
139                                 struct device_attribute *attr, char *buf)
140 {
141         struct hl_device *hdev = dev_get_drvdata(dev);
142
143         return sprintf(buf, "0x%04x\n",
144                         hdev->asic_prop.armcp_info.infineon_version);
145 }
146
147 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
148                                 char *buf)
149 {
150         struct hl_device *hdev = dev_get_drvdata(dev);
151
152         return sprintf(buf, "%s\n", hdev->asic_prop.armcp_info.fuse_version);
153 }
154
155 static ssize_t thermal_ver_show(struct device *dev,
156                                 struct device_attribute *attr, char *buf)
157 {
158         struct hl_device *hdev = dev_get_drvdata(dev);
159
160         return sprintf(buf, "%s", hdev->asic_prop.armcp_info.thermal_version);
161 }
162
163 static ssize_t preboot_btl_ver_show(struct device *dev,
164                                 struct device_attribute *attr, char *buf)
165 {
166         struct hl_device *hdev = dev_get_drvdata(dev);
167
168         return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
169 }
170
171 static ssize_t soft_reset_store(struct device *dev,
172                                 struct device_attribute *attr, const char *buf,
173                                 size_t count)
174 {
175         struct hl_device *hdev = dev_get_drvdata(dev);
176         long value;
177         int rc;
178
179         rc = kstrtoul(buf, 0, &value);
180
181         if (rc) {
182                 count = -EINVAL;
183                 goto out;
184         }
185
186         if (!hdev->supports_soft_reset) {
187                 dev_err(hdev->dev, "Device does not support soft-reset\n");
188                 goto out;
189         }
190
191         dev_warn(hdev->dev, "Soft-Reset requested through sysfs\n");
192
193         hl_device_reset(hdev, false, false);
194
195 out:
196         return count;
197 }
198
199 static ssize_t hard_reset_store(struct device *dev,
200                                 struct device_attribute *attr,
201                                 const char *buf, size_t count)
202 {
203         struct hl_device *hdev = dev_get_drvdata(dev);
204         long value;
205         int rc;
206
207         rc = kstrtoul(buf, 0, &value);
208
209         if (rc) {
210                 count = -EINVAL;
211                 goto out;
212         }
213
214         dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
215
216         hl_device_reset(hdev, true, false);
217
218 out:
219         return count;
220 }
221
222 static ssize_t device_type_show(struct device *dev,
223                 struct device_attribute *attr, char *buf)
224 {
225         struct hl_device *hdev = dev_get_drvdata(dev);
226         char *str;
227
228         switch (hdev->asic_type) {
229         case ASIC_GOYA:
230                 str = "GOYA";
231                 break;
232         case ASIC_GAUDI:
233                 str = "GAUDI";
234                 break;
235         default:
236                 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
237                                 hdev->asic_type);
238                 return -EINVAL;
239         }
240
241         return sprintf(buf, "%s\n", str);
242 }
243
244 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
245                                 char *buf)
246 {
247         struct hl_device *hdev = dev_get_drvdata(dev);
248
249         return sprintf(buf, "%04x:%02x:%02x.%x\n",
250                         pci_domain_nr(hdev->pdev->bus),
251                         hdev->pdev->bus->number,
252                         PCI_SLOT(hdev->pdev->devfn),
253                         PCI_FUNC(hdev->pdev->devfn));
254 }
255
256 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
257                                 char *buf)
258 {
259         struct hl_device *hdev = dev_get_drvdata(dev);
260         char *str;
261
262         if (atomic_read(&hdev->in_reset))
263                 str = "In reset";
264         else if (hdev->disabled)
265                 str = "Malfunction";
266         else
267                 str = "Operational";
268
269         return sprintf(buf, "%s\n", str);
270 }
271
272 static ssize_t soft_reset_cnt_show(struct device *dev,
273                 struct device_attribute *attr, char *buf)
274 {
275         struct hl_device *hdev = dev_get_drvdata(dev);
276
277         return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
278 }
279
280 static ssize_t hard_reset_cnt_show(struct device *dev,
281                 struct device_attribute *attr, char *buf)
282 {
283         struct hl_device *hdev = dev_get_drvdata(dev);
284
285         return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
286 }
287
288 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
289                                 char *buf)
290 {
291         struct hl_device *hdev = dev_get_drvdata(dev);
292         long val;
293
294         if (hl_device_disabled_or_in_reset(hdev))
295                 return -ENODEV;
296
297         val = hl_get_max_power(hdev);
298
299         return sprintf(buf, "%lu\n", val);
300 }
301
302 static ssize_t max_power_store(struct device *dev,
303                 struct device_attribute *attr, const char *buf, size_t count)
304 {
305         struct hl_device *hdev = dev_get_drvdata(dev);
306         unsigned long value;
307         int rc;
308
309         if (hl_device_disabled_or_in_reset(hdev)) {
310                 count = -ENODEV;
311                 goto out;
312         }
313
314         rc = kstrtoul(buf, 0, &value);
315
316         if (rc) {
317                 count = -EINVAL;
318                 goto out;
319         }
320
321         hdev->max_power = value;
322         hl_set_max_power(hdev, value);
323
324 out:
325         return count;
326 }
327
328 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
329                         struct bin_attribute *attr, char *buf, loff_t offset,
330                         size_t max_size)
331 {
332         struct device *dev = container_of(kobj, struct device, kobj);
333         struct hl_device *hdev = dev_get_drvdata(dev);
334         char *data;
335         int rc;
336
337         if (!max_size)
338                 return -EINVAL;
339
340         data = kzalloc(max_size, GFP_KERNEL);
341         if (!data)
342                 return -ENOMEM;
343
344         rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
345         if (rc)
346                 goto out;
347
348         memcpy(buf, data, max_size);
349
350 out:
351         kfree(data);
352
353         return max_size;
354 }
355
356 static DEVICE_ATTR_RO(armcp_kernel_ver);
357 static DEVICE_ATTR_RO(armcp_ver);
358 static DEVICE_ATTR_RO(cpld_ver);
359 static DEVICE_ATTR_RO(device_type);
360 static DEVICE_ATTR_RO(fuse_ver);
361 static DEVICE_ATTR_WO(hard_reset);
362 static DEVICE_ATTR_RO(hard_reset_cnt);
363 static DEVICE_ATTR_RO(infineon_ver);
364 static DEVICE_ATTR_RW(max_power);
365 static DEVICE_ATTR_RO(pci_addr);
366 static DEVICE_ATTR_RO(preboot_btl_ver);
367 static DEVICE_ATTR_WO(soft_reset);
368 static DEVICE_ATTR_RO(soft_reset_cnt);
369 static DEVICE_ATTR_RO(status);
370 static DEVICE_ATTR_RO(thermal_ver);
371 static DEVICE_ATTR_RO(uboot_ver);
372
373 static struct bin_attribute bin_attr_eeprom = {
374         .attr = {.name = "eeprom", .mode = (0444)},
375         .size = PAGE_SIZE,
376         .read = eeprom_read_handler
377 };
378
379 static struct attribute *hl_dev_attrs[] = {
380         &dev_attr_armcp_kernel_ver.attr,
381         &dev_attr_armcp_ver.attr,
382         &dev_attr_cpld_ver.attr,
383         &dev_attr_device_type.attr,
384         &dev_attr_fuse_ver.attr,
385         &dev_attr_hard_reset.attr,
386         &dev_attr_hard_reset_cnt.attr,
387         &dev_attr_infineon_ver.attr,
388         &dev_attr_max_power.attr,
389         &dev_attr_pci_addr.attr,
390         &dev_attr_preboot_btl_ver.attr,
391         &dev_attr_soft_reset.attr,
392         &dev_attr_soft_reset_cnt.attr,
393         &dev_attr_status.attr,
394         &dev_attr_thermal_ver.attr,
395         &dev_attr_uboot_ver.attr,
396         NULL,
397 };
398
399 static struct bin_attribute *hl_dev_bin_attrs[] = {
400         &bin_attr_eeprom,
401         NULL
402 };
403
404 static struct attribute_group hl_dev_attr_group = {
405         .attrs = hl_dev_attrs,
406         .bin_attrs = hl_dev_bin_attrs,
407 };
408
409 static struct attribute_group hl_dev_clks_attr_group;
410
411 static const struct attribute_group *hl_dev_attr_groups[] = {
412         &hl_dev_attr_group,
413         &hl_dev_clks_attr_group,
414         NULL,
415 };
416
417 int hl_sysfs_init(struct hl_device *hdev)
418 {
419         int rc;
420
421         if (hdev->asic_type == ASIC_GOYA)
422                 hdev->pm_mng_profile = PM_AUTO;
423         else
424                 hdev->pm_mng_profile = PM_MANUAL;
425         hdev->max_power = hdev->asic_prop.max_power_default;
426
427         hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);
428
429         rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
430         if (rc) {
431                 dev_err(hdev->dev,
432                         "Failed to add groups to device, error %d\n", rc);
433                 return rc;
434         }
435
436         return 0;
437 }
438
439 void hl_sysfs_fini(struct hl_device *hdev)
440 {
441         device_remove_groups(hdev->dev, hl_dev_attr_groups);
442 }