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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 default: 255 dev_err(hdev->dev, "Unrecognized ASIC type %d\n", 256 hdev->asic_type); 257 return -EINVAL; 258 } 259 260 return sprintf(buf, "%s\n", str); 261 } 262 263 static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr, 264 char *buf) 265 { 266 struct hl_device *hdev = dev_get_drvdata(dev); 267 268 return sprintf(buf, "%04x:%02x:%02x.%x\n", 269 pci_domain_nr(hdev->pdev->bus), 270 hdev->pdev->bus->number, 271 PCI_SLOT(hdev->pdev->devfn), 272 PCI_FUNC(hdev->pdev->devfn)); 273 } 274 275 static ssize_t status_show(struct device *dev, struct device_attribute *attr, 276 char *buf) 277 { 278 struct hl_device *hdev = dev_get_drvdata(dev); 279 char str[HL_STR_MAX]; 280 281 strscpy(str, hdev->status[hl_device_status(hdev)], HL_STR_MAX); 282 283 /* use uppercase for backward compatibility */ 284 str[0] = 'A' + (str[0] - 'a'); 285 286 return sprintf(buf, "%s\n", str); 287 } 288 289 static ssize_t soft_reset_cnt_show(struct device *dev, 290 struct device_attribute *attr, char *buf) 291 { 292 struct hl_device *hdev = dev_get_drvdata(dev); 293 294 return sprintf(buf, "%d\n", hdev->reset_info.compute_reset_cnt); 295 } 296 297 static ssize_t hard_reset_cnt_show(struct device *dev, 298 struct device_attribute *attr, char *buf) 299 { 300 struct hl_device *hdev = dev_get_drvdata(dev); 301 302 return sprintf(buf, "%d\n", hdev->reset_info.hard_reset_cnt); 303 } 304 305 static ssize_t max_power_show(struct device *dev, struct device_attribute *attr, 306 char *buf) 307 { 308 struct hl_device *hdev = dev_get_drvdata(dev); 309 long val; 310 311 if (!hl_device_operational(hdev, NULL)) 312 return -ENODEV; 313 314 val = hl_fw_get_max_power(hdev); 315 if (val < 0) 316 return val; 317 318 return sprintf(buf, "%lu\n", val); 319 } 320 321 static ssize_t max_power_store(struct device *dev, 322 struct device_attribute *attr, const char *buf, size_t count) 323 { 324 struct hl_device *hdev = dev_get_drvdata(dev); 325 unsigned long value; 326 int rc; 327 328 if (!hl_device_operational(hdev, NULL)) { 329 count = -ENODEV; 330 goto out; 331 } 332 333 rc = kstrtoul(buf, 0, &value); 334 335 if (rc) { 336 count = -EINVAL; 337 goto out; 338 } 339 340 hdev->max_power = value; 341 hl_fw_set_max_power(hdev); 342 343 out: 344 return count; 345 } 346 347 static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj, 348 struct bin_attribute *attr, char *buf, loff_t offset, 349 size_t max_size) 350 { 351 struct device *dev = kobj_to_dev(kobj); 352 struct hl_device *hdev = dev_get_drvdata(dev); 353 char *data; 354 int rc; 355 356 if (!hl_device_operational(hdev, NULL)) 357 return -ENODEV; 358 359 if (!max_size) 360 return -EINVAL; 361 362 data = kzalloc(max_size, GFP_KERNEL); 363 if (!data) 364 return -ENOMEM; 365 366 rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size); 367 if (rc) 368 goto out; 369 370 memcpy(buf, data, max_size); 371 372 out: 373 kfree(data); 374 375 return max_size; 376 } 377 378 static ssize_t security_enabled_show(struct device *dev, 379 struct device_attribute *attr, char *buf) 380 { 381 struct hl_device *hdev = dev_get_drvdata(dev); 382 383 return sprintf(buf, "%d\n", hdev->asic_prop.fw_security_enabled); 384 } 385 386 static DEVICE_ATTR_RO(armcp_kernel_ver); 387 static DEVICE_ATTR_RO(armcp_ver); 388 static DEVICE_ATTR_RO(cpld_ver); 389 static DEVICE_ATTR_RO(cpucp_kernel_ver); 390 static DEVICE_ATTR_RO(cpucp_ver); 391 static DEVICE_ATTR_RO(device_type); 392 static DEVICE_ATTR_RO(fuse_ver); 393 static DEVICE_ATTR_WO(hard_reset); 394 static DEVICE_ATTR_RO(hard_reset_cnt); 395 static DEVICE_ATTR_RW(max_power); 396 static DEVICE_ATTR_RO(pci_addr); 397 static DEVICE_ATTR_RO(preboot_btl_ver); 398 static DEVICE_ATTR_WO(soft_reset); 399 static DEVICE_ATTR_RO(soft_reset_cnt); 400 static DEVICE_ATTR_RO(status); 401 static DEVICE_ATTR_RO(thermal_ver); 402 static DEVICE_ATTR_RO(uboot_ver); 403 static DEVICE_ATTR_RO(fw_os_ver); 404 static DEVICE_ATTR_RO(security_enabled); 405 406 static struct bin_attribute bin_attr_eeprom = { 407 .attr = {.name = "eeprom", .mode = (0444)}, 408 .size = PAGE_SIZE, 409 .read = eeprom_read_handler 410 }; 411 412 static struct attribute *hl_dev_attrs[] = { 413 &dev_attr_armcp_kernel_ver.attr, 414 &dev_attr_armcp_ver.attr, 415 &dev_attr_cpld_ver.attr, 416 &dev_attr_cpucp_kernel_ver.attr, 417 &dev_attr_cpucp_ver.attr, 418 &dev_attr_device_type.attr, 419 &dev_attr_fuse_ver.attr, 420 &dev_attr_hard_reset.attr, 421 &dev_attr_hard_reset_cnt.attr, 422 &dev_attr_max_power.attr, 423 &dev_attr_pci_addr.attr, 424 &dev_attr_preboot_btl_ver.attr, 425 &dev_attr_status.attr, 426 &dev_attr_thermal_ver.attr, 427 &dev_attr_uboot_ver.attr, 428 &dev_attr_fw_os_ver.attr, 429 &dev_attr_security_enabled.attr, 430 NULL, 431 }; 432 433 static struct bin_attribute *hl_dev_bin_attrs[] = { 434 &bin_attr_eeprom, 435 NULL 436 }; 437 438 static struct attribute_group hl_dev_attr_group = { 439 .attrs = hl_dev_attrs, 440 .bin_attrs = hl_dev_bin_attrs, 441 }; 442 443 static struct attribute_group hl_dev_clks_attr_group; 444 static struct attribute_group hl_dev_vrm_attr_group; 445 446 static const struct attribute_group *hl_dev_attr_groups[] = { 447 &hl_dev_attr_group, 448 &hl_dev_clks_attr_group, 449 &hl_dev_vrm_attr_group, 450 NULL, 451 }; 452 453 static struct attribute *hl_dev_inference_attrs[] = { 454 &dev_attr_soft_reset.attr, 455 &dev_attr_soft_reset_cnt.attr, 456 NULL, 457 }; 458 459 static struct attribute_group hl_dev_inference_attr_group = { 460 .attrs = hl_dev_inference_attrs, 461 }; 462 463 static const struct attribute_group *hl_dev_inference_attr_groups[] = { 464 &hl_dev_inference_attr_group, 465 NULL, 466 }; 467 468 void hl_sysfs_add_dev_clk_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp) 469 { 470 dev_clk_attr_grp->attrs = hl_dev_clk_attrs; 471 } 472 473 void hl_sysfs_add_dev_vrm_attr(struct hl_device *hdev, struct attribute_group *dev_vrm_attr_grp) 474 { 475 dev_vrm_attr_grp->attrs = hl_dev_vrm_attrs; 476 } 477 478 int hl_sysfs_init(struct hl_device *hdev) 479 { 480 int rc; 481 482 hdev->max_power = hdev->asic_prop.max_power_default; 483 484 hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group, &hl_dev_vrm_attr_group); 485 486 rc = device_add_groups(hdev->dev, hl_dev_attr_groups); 487 if (rc) { 488 dev_err(hdev->dev, 489 "Failed to add groups to device, error %d\n", rc); 490 return rc; 491 } 492 493 if (!hdev->asic_prop.allow_inference_soft_reset) 494 return 0; 495 496 rc = device_add_groups(hdev->dev, hl_dev_inference_attr_groups); 497 if (rc) { 498 dev_err(hdev->dev, 499 "Failed to add groups to device, error %d\n", rc); 500 goto remove_groups; 501 } 502 503 return 0; 504 505 remove_groups: 506 device_remove_groups(hdev->dev, hl_dev_attr_groups); 507 return rc; 508 } 509 510 void hl_sysfs_fini(struct hl_device *hdev) 511 { 512 device_remove_groups(hdev->dev, hl_dev_attr_groups); 513 514 if (!hdev->asic_prop.allow_inference_soft_reset) 515 return; 516 517 device_remove_groups(hdev->dev, hl_dev_inference_attr_groups); 518 } 519