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