1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Copyright 2016-2022 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8 #include "habanalabs.h"
9
10 #include <linux/pci.h>
11
clk_max_freq_mhz_show(struct device * dev,struct device_attribute * attr,char * buf)12 static ssize_t clk_max_freq_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
13 {
14 struct hl_device *hdev = dev_get_drvdata(dev);
15 long value;
16
17 if (!hl_device_operational(hdev, NULL))
18 return -ENODEV;
19
20 value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, false);
21 if (value < 0)
22 return value;
23
24 hdev->asic_prop.max_freq_value = value;
25
26 return sprintf(buf, "%lu\n", (value / 1000 / 1000));
27 }
28
clk_max_freq_mhz_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)29 static ssize_t clk_max_freq_mhz_store(struct device *dev, struct device_attribute *attr,
30 const char *buf, size_t count)
31 {
32 struct hl_device *hdev = dev_get_drvdata(dev);
33 int rc;
34 u64 value;
35
36 if (!hl_device_operational(hdev, NULL)) {
37 count = -ENODEV;
38 goto fail;
39 }
40
41 rc = kstrtoull(buf, 0, &value);
42 if (rc) {
43 count = -EINVAL;
44 goto fail;
45 }
46
47 hdev->asic_prop.max_freq_value = value * 1000 * 1000;
48
49 hl_fw_set_frequency(hdev, hdev->asic_prop.clk_pll_index, hdev->asic_prop.max_freq_value);
50
51 fail:
52 return count;
53 }
54
clk_cur_freq_mhz_show(struct device * dev,struct device_attribute * attr,char * buf)55 static ssize_t clk_cur_freq_mhz_show(struct device *dev, struct device_attribute *attr, char *buf)
56 {
57 struct hl_device *hdev = dev_get_drvdata(dev);
58 long value;
59
60 if (!hl_device_operational(hdev, NULL))
61 return -ENODEV;
62
63 value = hl_fw_get_frequency(hdev, hdev->asic_prop.clk_pll_index, true);
64 if (value < 0)
65 return value;
66
67 return sprintf(buf, "%lu\n", (value / 1000 / 1000));
68 }
69
70 static DEVICE_ATTR_RW(clk_max_freq_mhz);
71 static DEVICE_ATTR_RO(clk_cur_freq_mhz);
72
73 static struct attribute *hl_dev_clk_attrs[] = {
74 &dev_attr_clk_max_freq_mhz.attr,
75 &dev_attr_clk_cur_freq_mhz.attr,
76 NULL,
77 };
78
vrm_ver_show(struct device * dev,struct device_attribute * attr,char * buf)79 static ssize_t vrm_ver_show(struct device *dev, struct device_attribute *attr, char *buf)
80 {
81 struct hl_device *hdev = dev_get_drvdata(dev);
82 struct cpucp_info *cpucp_info;
83
84 cpucp_info = &hdev->asic_prop.cpucp_info;
85
86 if (cpucp_info->infineon_second_stage_version)
87 return sprintf(buf, "%#04x %#04x\n", le32_to_cpu(cpucp_info->infineon_version),
88 le32_to_cpu(cpucp_info->infineon_second_stage_version));
89 else
90 return sprintf(buf, "%#04x\n", le32_to_cpu(cpucp_info->infineon_version));
91 }
92
93 static DEVICE_ATTR_RO(vrm_ver);
94
95 static struct attribute *hl_dev_vrm_attrs[] = {
96 &dev_attr_vrm_ver.attr,
97 NULL,
98 };
99
uboot_ver_show(struct device * dev,struct device_attribute * attr,char * buf)100 static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
101 char *buf)
102 {
103 struct hl_device *hdev = dev_get_drvdata(dev);
104
105 return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
106 }
107
armcp_kernel_ver_show(struct device * dev,struct device_attribute * attr,char * buf)108 static ssize_t armcp_kernel_ver_show(struct device *dev,
109 struct device_attribute *attr, char *buf)
110 {
111 struct hl_device *hdev = dev_get_drvdata(dev);
112
113 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
114 }
115
armcp_ver_show(struct device * dev,struct device_attribute * attr,char * buf)116 static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
117 char *buf)
118 {
119 struct hl_device *hdev = dev_get_drvdata(dev);
120
121 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
122 }
123
cpld_ver_show(struct device * dev,struct device_attribute * attr,char * buf)124 static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
125 char *buf)
126 {
127 struct hl_device *hdev = dev_get_drvdata(dev);
128
129 return sprintf(buf, "0x%08x\n",
130 le32_to_cpu(hdev->asic_prop.cpucp_info.cpld_version));
131 }
132
cpucp_kernel_ver_show(struct device * dev,struct device_attribute * attr,char * buf)133 static ssize_t cpucp_kernel_ver_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135 {
136 struct hl_device *hdev = dev_get_drvdata(dev);
137
138 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
139 }
140
cpucp_ver_show(struct device * dev,struct device_attribute * attr,char * buf)141 static ssize_t cpucp_ver_show(struct device *dev, struct device_attribute *attr,
142 char *buf)
143 {
144 struct hl_device *hdev = dev_get_drvdata(dev);
145
146 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
147 }
148
fuse_ver_show(struct device * dev,struct device_attribute * attr,char * buf)149 static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
150 char *buf)
151 {
152 struct hl_device *hdev = dev_get_drvdata(dev);
153
154 return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.fuse_version);
155 }
156
thermal_ver_show(struct device * dev,struct device_attribute * attr,char * buf)157 static ssize_t thermal_ver_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159 {
160 struct hl_device *hdev = dev_get_drvdata(dev);
161
162 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.thermal_version);
163 }
164
fw_os_ver_show(struct device * dev,struct device_attribute * attr,char * buf)165 static ssize_t fw_os_ver_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
167 {
168 struct hl_device *hdev = dev_get_drvdata(dev);
169
170 return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.fw_os_version);
171 }
172
preboot_btl_ver_show(struct device * dev,struct device_attribute * attr,char * buf)173 static ssize_t preboot_btl_ver_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
175 {
176 struct hl_device *hdev = dev_get_drvdata(dev);
177
178 return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
179 }
180
soft_reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)181 static ssize_t soft_reset_store(struct device *dev,
182 struct device_attribute *attr, const char *buf,
183 size_t count)
184 {
185 struct hl_device *hdev = dev_get_drvdata(dev);
186 long value;
187 int rc;
188
189 rc = kstrtoul(buf, 0, &value);
190
191 if (rc) {
192 count = -EINVAL;
193 goto out;
194 }
195
196 if (!hdev->asic_prop.allow_inference_soft_reset) {
197 dev_err(hdev->dev, "Device does not support inference soft-reset\n");
198 goto out;
199 }
200
201 dev_warn(hdev->dev, "Inference Soft-Reset requested through sysfs\n");
202
203 hl_device_reset(hdev, 0);
204
205 out:
206 return count;
207 }
208
hard_reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)209 static ssize_t hard_reset_store(struct device *dev,
210 struct device_attribute *attr,
211 const char *buf, size_t count)
212 {
213 struct hl_device *hdev = dev_get_drvdata(dev);
214 long value;
215 int rc;
216
217 rc = kstrtoul(buf, 0, &value);
218
219 if (rc) {
220 count = -EINVAL;
221 goto out;
222 }
223
224 dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");
225
226 hl_device_reset(hdev, HL_DRV_RESET_HARD);
227
228 out:
229 return count;
230 }
231
device_type_show(struct device * dev,struct device_attribute * attr,char * buf)232 static ssize_t device_type_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234 {
235 struct hl_device *hdev = dev_get_drvdata(dev);
236 char *str;
237
238 switch (hdev->asic_type) {
239 case ASIC_GOYA:
240 str = "GOYA";
241 break;
242 case ASIC_GAUDI:
243 str = "GAUDI";
244 break;
245 case ASIC_GAUDI_SEC:
246 str = "GAUDI SEC";
247 break;
248 case ASIC_GAUDI2:
249 str = "GAUDI2";
250 break;
251 case ASIC_GAUDI2B:
252 str = "GAUDI2B";
253 break;
254 case ASIC_GAUDI2C:
255 str = "GAUDI2C";
256 break;
257 default:
258 dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
259 hdev->asic_type);
260 return -EINVAL;
261 }
262
263 return sprintf(buf, "%s\n", str);
264 }
265
pci_addr_show(struct device * dev,struct device_attribute * attr,char * buf)266 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
267 char *buf)
268 {
269 struct hl_device *hdev = dev_get_drvdata(dev);
270
271 return sprintf(buf, "%04x:%02x:%02x.%x\n",
272 pci_domain_nr(hdev->pdev->bus),
273 hdev->pdev->bus->number,
274 PCI_SLOT(hdev->pdev->devfn),
275 PCI_FUNC(hdev->pdev->devfn));
276 }
277
status_show(struct device * dev,struct device_attribute * attr,char * buf)278 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
279 char *buf)
280 {
281 struct hl_device *hdev = dev_get_drvdata(dev);
282 char str[HL_STR_MAX];
283
284 strscpy(str, hdev->status[hl_device_status(hdev)], HL_STR_MAX);
285
286 /* use uppercase for backward compatibility */
287 str[0] = 'A' + (str[0] - 'a');
288
289 return sprintf(buf, "%s\n", str);
290 }
291
soft_reset_cnt_show(struct device * dev,struct device_attribute * attr,char * buf)292 static ssize_t soft_reset_cnt_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294 {
295 struct hl_device *hdev = dev_get_drvdata(dev);
296
297 return sprintf(buf, "%d\n", hdev->reset_info.compute_reset_cnt);
298 }
299
hard_reset_cnt_show(struct device * dev,struct device_attribute * attr,char * buf)300 static ssize_t hard_reset_cnt_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
302 {
303 struct hl_device *hdev = dev_get_drvdata(dev);
304
305 return sprintf(buf, "%d\n", hdev->reset_info.hard_reset_cnt);
306 }
307
max_power_show(struct device * dev,struct device_attribute * attr,char * buf)308 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
309 char *buf)
310 {
311 struct hl_device *hdev = dev_get_drvdata(dev);
312 long val;
313
314 if (!hl_device_operational(hdev, NULL))
315 return -ENODEV;
316
317 val = hl_fw_get_max_power(hdev);
318 if (val < 0)
319 return val;
320
321 return sprintf(buf, "%lu\n", val);
322 }
323
max_power_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)324 static ssize_t max_power_store(struct device *dev,
325 struct device_attribute *attr, const char *buf, size_t count)
326 {
327 struct hl_device *hdev = dev_get_drvdata(dev);
328 unsigned long value;
329 int rc;
330
331 if (!hl_device_operational(hdev, NULL)) {
332 count = -ENODEV;
333 goto out;
334 }
335
336 rc = kstrtoul(buf, 0, &value);
337
338 if (rc) {
339 count = -EINVAL;
340 goto out;
341 }
342
343 hdev->max_power = value;
344 hl_fw_set_max_power(hdev);
345
346 out:
347 return count;
348 }
349
eeprom_read_handler(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t offset,size_t max_size)350 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
351 struct bin_attribute *attr, char *buf, loff_t offset,
352 size_t max_size)
353 {
354 struct device *dev = kobj_to_dev(kobj);
355 struct hl_device *hdev = dev_get_drvdata(dev);
356 char *data;
357 int rc;
358
359 if (!hl_device_operational(hdev, NULL))
360 return -ENODEV;
361
362 if (!max_size)
363 return -EINVAL;
364
365 data = kzalloc(max_size, GFP_KERNEL);
366 if (!data)
367 return -ENOMEM;
368
369 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
370 if (rc)
371 goto out;
372
373 memcpy(buf, data, max_size);
374
375 out:
376 kfree(data);
377
378 return max_size;
379 }
380
security_enabled_show(struct device * dev,struct device_attribute * attr,char * buf)381 static ssize_t security_enabled_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
383 {
384 struct hl_device *hdev = dev_get_drvdata(dev);
385
386 return sprintf(buf, "%d\n", hdev->asic_prop.fw_security_enabled);
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_RW(max_power);
399 static DEVICE_ATTR_RO(pci_addr);
400 static DEVICE_ATTR_RO(preboot_btl_ver);
401 static DEVICE_ATTR_WO(soft_reset);
402 static DEVICE_ATTR_RO(soft_reset_cnt);
403 static DEVICE_ATTR_RO(status);
404 static DEVICE_ATTR_RO(thermal_ver);
405 static DEVICE_ATTR_RO(uboot_ver);
406 static DEVICE_ATTR_RO(fw_os_ver);
407 static DEVICE_ATTR_RO(security_enabled);
408
409 static struct bin_attribute bin_attr_eeprom = {
410 .attr = {.name = "eeprom", .mode = (0444)},
411 .size = PAGE_SIZE,
412 .read = eeprom_read_handler
413 };
414
415 static struct attribute *hl_dev_attrs[] = {
416 &dev_attr_armcp_kernel_ver.attr,
417 &dev_attr_armcp_ver.attr,
418 &dev_attr_cpld_ver.attr,
419 &dev_attr_cpucp_kernel_ver.attr,
420 &dev_attr_cpucp_ver.attr,
421 &dev_attr_device_type.attr,
422 &dev_attr_fuse_ver.attr,
423 &dev_attr_hard_reset.attr,
424 &dev_attr_hard_reset_cnt.attr,
425 &dev_attr_max_power.attr,
426 &dev_attr_pci_addr.attr,
427 &dev_attr_preboot_btl_ver.attr,
428 &dev_attr_status.attr,
429 &dev_attr_thermal_ver.attr,
430 &dev_attr_uboot_ver.attr,
431 &dev_attr_fw_os_ver.attr,
432 &dev_attr_security_enabled.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 static struct attribute_group hl_dev_vrm_attr_group;
448
449 static const struct attribute_group *hl_dev_attr_groups[] = {
450 &hl_dev_attr_group,
451 &hl_dev_clks_attr_group,
452 &hl_dev_vrm_attr_group,
453 NULL,
454 };
455
456 static struct attribute *hl_dev_inference_attrs[] = {
457 &dev_attr_soft_reset.attr,
458 &dev_attr_soft_reset_cnt.attr,
459 NULL,
460 };
461
462 static struct attribute_group hl_dev_inference_attr_group = {
463 .attrs = hl_dev_inference_attrs,
464 };
465
466 static const struct attribute_group *hl_dev_inference_attr_groups[] = {
467 &hl_dev_inference_attr_group,
468 NULL,
469 };
470
hl_sysfs_add_dev_clk_attr(struct hl_device * hdev,struct attribute_group * dev_clk_attr_grp)471 void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp)
472 {
473 dev_clk_attr_grp->attrs = hl_dev_clk_attrs;
474 }
475
hl_sysfs_add_dev_vrm_attr(struct hl_device * hdev,struct attribute_group * dev_vrm_attr_grp)476 void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp)
477 {
478 dev_vrm_attr_grp->attrs = hl_dev_vrm_attrs;
479 }
480
hl_sysfs_init(struct hl_device * hdev)481 int hl_sysfs_init(struct hl_device *hdev)
482 {
483 int rc;
484
485 hdev->max_power = hdev->asic_prop.max_power_default;
486
487 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group, &hl_dev_vrm_attr_group);
488
489 rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
490 if (rc) {
491 dev_err(hdev->dev,
492 "Failed to add groups to device, error %d\n", rc);
493 return rc;
494 }
495
496 if (!hdev->asic_prop.allow_inference_soft_reset)
497 return 0;
498
499 rc = device_add_groups(hdev->dev, hl_dev_inference_attr_groups);
500 if (rc) {
501 dev_err(hdev->dev,
502 "Failed to add groups to device, error %d\n", rc);
503 goto remove_groups;
504 }
505
506 return 0;
507
508 remove_groups:
509 device_remove_groups(hdev->dev, hl_dev_attr_groups);
510 return rc;
511 }
512
hl_sysfs_fini(struct hl_device * hdev)513 void hl_sysfs_fini(struct hl_device *hdev)
514 {
515 device_remove_groups(hdev->dev, hl_dev_attr_groups);
516
517 if (!hdev->asic_prop.allow_inference_soft_reset)
518 return;
519
520 device_remove_groups(hdev->dev, hl_dev_inference_attr_groups);
521 }
522