sysfs.c 12 KB

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