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