hwmon.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2019 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include "habanalabs.h"
  7. #include <linux/pci.h>
  8. #include <linux/hwmon.h>
  9. #define HWMON_NR_SENSOR_TYPES (hwmon_max)
  10. #ifdef _HAS_HWMON_HWMON_T_ENABLE
  11. static u32 fixup_flags_legacy_fw(struct hl_device *hdev, enum hwmon_sensor_types type,
  12. u32 cpucp_flags)
  13. {
  14. u32 flags;
  15. switch (type) {
  16. case hwmon_temp:
  17. flags = (cpucp_flags << 1) | HWMON_T_ENABLE;
  18. break;
  19. case hwmon_in:
  20. flags = (cpucp_flags << 1) | HWMON_I_ENABLE;
  21. break;
  22. case hwmon_curr:
  23. flags = (cpucp_flags << 1) | HWMON_C_ENABLE;
  24. break;
  25. case hwmon_fan:
  26. flags = (cpucp_flags << 1) | HWMON_F_ENABLE;
  27. break;
  28. case hwmon_power:
  29. flags = (cpucp_flags << 1) | HWMON_P_ENABLE;
  30. break;
  31. case hwmon_pwm:
  32. /* enable bit was here from day 1, so no need to adjust */
  33. flags = cpucp_flags;
  34. break;
  35. default:
  36. dev_err(hdev->dev, "unsupported h/w sensor type %d\n", type);
  37. flags = cpucp_flags;
  38. break;
  39. }
  40. return flags;
  41. }
  42. static u32 fixup_attr_legacy_fw(u32 attr)
  43. {
  44. return (attr - 1);
  45. }
  46. #else
  47. static u32 fixup_flags_legacy_fw(struct hl_device *hdev, enum hwmon_sensor_types type,
  48. u32 cpucp_flags)
  49. {
  50. return cpucp_flags;
  51. }
  52. static u32 fixup_attr_legacy_fw(u32 attr)
  53. {
  54. return attr;
  55. }
  56. #endif /* !_HAS_HWMON_HWMON_T_ENABLE */
  57. static u32 adjust_hwmon_flags(struct hl_device *hdev, enum hwmon_sensor_types type, u32 cpucp_flags)
  58. {
  59. u32 flags, cpucp_input_val;
  60. bool use_cpucp_enum;
  61. use_cpucp_enum = (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
  62. CPU_BOOT_DEV_STS0_MAP_HWMON_EN) ? true : false;
  63. /* If f/w is using it's own enum, we need to check if the properties values are aligned.
  64. * If not, it means we need to adjust the values to the new format that is used in the
  65. * kernel since 5.6 (enum values were incremented by 1 by adding a new enable value).
  66. */
  67. if (use_cpucp_enum) {
  68. switch (type) {
  69. case hwmon_temp:
  70. cpucp_input_val = cpucp_temp_input;
  71. if (cpucp_input_val == hwmon_temp_input)
  72. flags = cpucp_flags;
  73. else
  74. flags = (cpucp_flags << 1) | HWMON_T_ENABLE;
  75. break;
  76. case hwmon_in:
  77. cpucp_input_val = cpucp_in_input;
  78. if (cpucp_input_val == hwmon_in_input)
  79. flags = cpucp_flags;
  80. else
  81. flags = (cpucp_flags << 1) | HWMON_I_ENABLE;
  82. break;
  83. case hwmon_curr:
  84. cpucp_input_val = cpucp_curr_input;
  85. if (cpucp_input_val == hwmon_curr_input)
  86. flags = cpucp_flags;
  87. else
  88. flags = (cpucp_flags << 1) | HWMON_C_ENABLE;
  89. break;
  90. case hwmon_fan:
  91. cpucp_input_val = cpucp_fan_input;
  92. if (cpucp_input_val == hwmon_fan_input)
  93. flags = cpucp_flags;
  94. else
  95. flags = (cpucp_flags << 1) | HWMON_F_ENABLE;
  96. break;
  97. case hwmon_pwm:
  98. /* enable bit was here from day 1, so no need to adjust */
  99. flags = cpucp_flags;
  100. break;
  101. case hwmon_power:
  102. cpucp_input_val = CPUCP_POWER_INPUT;
  103. if (cpucp_input_val == hwmon_power_input)
  104. flags = cpucp_flags;
  105. else
  106. flags = (cpucp_flags << 1) | HWMON_P_ENABLE;
  107. break;
  108. default:
  109. dev_err(hdev->dev, "unsupported h/w sensor type %d\n", type);
  110. flags = cpucp_flags;
  111. break;
  112. }
  113. } else {
  114. flags = fixup_flags_legacy_fw(hdev, type, cpucp_flags);
  115. }
  116. return flags;
  117. }
  118. int hl_build_hwmon_channel_info(struct hl_device *hdev, struct cpucp_sensor *sensors_arr)
  119. {
  120. u32 num_sensors_for_type, flags, num_active_sensor_types = 0, arr_size = 0, *curr_arr;
  121. u32 sensors_by_type_next_index[HWMON_NR_SENSOR_TYPES] = {0};
  122. u32 *sensors_by_type[HWMON_NR_SENSOR_TYPES] = {NULL};
  123. struct hwmon_channel_info **channels_info;
  124. u32 counts[HWMON_NR_SENSOR_TYPES] = {0};
  125. enum hwmon_sensor_types type;
  126. int rc, i, j;
  127. for (i = 0 ; i < CPUCP_MAX_SENSORS ; i++) {
  128. type = le32_to_cpu(sensors_arr[i].type);
  129. if ((type == 0) && (sensors_arr[i].flags == 0))
  130. break;
  131. if (type >= HWMON_NR_SENSOR_TYPES) {
  132. dev_err(hdev->dev, "Got wrong sensor type %d from device\n", type);
  133. return -EINVAL;
  134. }
  135. counts[type]++;
  136. arr_size++;
  137. }
  138. for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
  139. if (counts[i] == 0)
  140. continue;
  141. num_sensors_for_type = counts[i] + 1;
  142. dev_dbg(hdev->dev, "num_sensors_for_type %d = %d\n", i, num_sensors_for_type);
  143. curr_arr = kcalloc(num_sensors_for_type, sizeof(*curr_arr), GFP_KERNEL);
  144. if (!curr_arr) {
  145. rc = -ENOMEM;
  146. goto sensors_type_err;
  147. }
  148. num_active_sensor_types++;
  149. sensors_by_type[i] = curr_arr;
  150. }
  151. for (i = 0 ; i < arr_size ; i++) {
  152. type = le32_to_cpu(sensors_arr[i].type);
  153. curr_arr = sensors_by_type[type];
  154. flags = adjust_hwmon_flags(hdev, type, le32_to_cpu(sensors_arr[i].flags));
  155. curr_arr[sensors_by_type_next_index[type]++] = flags;
  156. }
  157. channels_info = kcalloc(num_active_sensor_types + 1, sizeof(struct hwmon_channel_info *),
  158. GFP_KERNEL);
  159. if (!channels_info) {
  160. rc = -ENOMEM;
  161. goto channels_info_array_err;
  162. }
  163. for (i = 0 ; i < num_active_sensor_types ; i++) {
  164. channels_info[i] = kzalloc(sizeof(*channels_info[i]), GFP_KERNEL);
  165. if (!channels_info[i]) {
  166. rc = -ENOMEM;
  167. goto channel_info_err;
  168. }
  169. }
  170. for (i = 0, j = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
  171. if (!sensors_by_type[i])
  172. continue;
  173. channels_info[j]->type = i;
  174. channels_info[j]->config = sensors_by_type[i];
  175. j++;
  176. }
  177. hdev->hl_chip_info->info = (const struct hwmon_channel_info **)channels_info;
  178. return 0;
  179. channel_info_err:
  180. for (i = 0 ; i < num_active_sensor_types ; i++) {
  181. if (channels_info[i]) {
  182. kfree(channels_info[i]->config);
  183. kfree(channels_info[i]);
  184. }
  185. }
  186. kfree(channels_info);
  187. channels_info_array_err:
  188. sensors_type_err:
  189. for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++)
  190. kfree(sensors_by_type[i]);
  191. return rc;
  192. }
  193. static int hl_read(struct device *dev, enum hwmon_sensor_types type,
  194. u32 attr, int channel, long *val)
  195. {
  196. struct hl_device *hdev = dev_get_drvdata(dev);
  197. bool use_cpucp_enum;
  198. u32 cpucp_attr;
  199. int rc;
  200. if (!hl_device_operational(hdev, NULL))
  201. return -ENODEV;
  202. use_cpucp_enum = (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
  203. CPU_BOOT_DEV_STS0_MAP_HWMON_EN) ? true : false;
  204. switch (type) {
  205. case hwmon_temp:
  206. switch (attr) {
  207. case hwmon_temp_input:
  208. cpucp_attr = cpucp_temp_input;
  209. break;
  210. case hwmon_temp_max:
  211. cpucp_attr = cpucp_temp_max;
  212. break;
  213. case hwmon_temp_crit:
  214. cpucp_attr = cpucp_temp_crit;
  215. break;
  216. case hwmon_temp_max_hyst:
  217. cpucp_attr = cpucp_temp_max_hyst;
  218. break;
  219. case hwmon_temp_crit_hyst:
  220. cpucp_attr = cpucp_temp_crit_hyst;
  221. break;
  222. case hwmon_temp_offset:
  223. cpucp_attr = cpucp_temp_offset;
  224. break;
  225. case hwmon_temp_highest:
  226. cpucp_attr = cpucp_temp_highest;
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. if (use_cpucp_enum)
  232. rc = hl_get_temperature(hdev, channel, cpucp_attr, val);
  233. else
  234. rc = hl_get_temperature(hdev, channel, fixup_attr_legacy_fw(attr), val);
  235. break;
  236. case hwmon_in:
  237. switch (attr) {
  238. case hwmon_in_input:
  239. cpucp_attr = cpucp_in_input;
  240. break;
  241. case hwmon_in_min:
  242. cpucp_attr = cpucp_in_min;
  243. break;
  244. case hwmon_in_max:
  245. cpucp_attr = cpucp_in_max;
  246. break;
  247. case hwmon_in_highest:
  248. cpucp_attr = cpucp_in_highest;
  249. break;
  250. default:
  251. return -EINVAL;
  252. }
  253. if (use_cpucp_enum)
  254. rc = hl_get_voltage(hdev, channel, cpucp_attr, val);
  255. else
  256. rc = hl_get_voltage(hdev, channel, fixup_attr_legacy_fw(attr), val);
  257. break;
  258. case hwmon_curr:
  259. switch (attr) {
  260. case hwmon_curr_input:
  261. cpucp_attr = cpucp_curr_input;
  262. break;
  263. case hwmon_curr_min:
  264. cpucp_attr = cpucp_curr_min;
  265. break;
  266. case hwmon_curr_max:
  267. cpucp_attr = cpucp_curr_max;
  268. break;
  269. case hwmon_curr_highest:
  270. cpucp_attr = cpucp_curr_highest;
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. if (use_cpucp_enum)
  276. rc = hl_get_current(hdev, channel, cpucp_attr, val);
  277. else
  278. rc = hl_get_current(hdev, channel, fixup_attr_legacy_fw(attr), val);
  279. break;
  280. case hwmon_fan:
  281. switch (attr) {
  282. case hwmon_fan_input:
  283. cpucp_attr = cpucp_fan_input;
  284. break;
  285. case hwmon_fan_min:
  286. cpucp_attr = cpucp_fan_min;
  287. break;
  288. case hwmon_fan_max:
  289. cpucp_attr = cpucp_fan_max;
  290. break;
  291. default:
  292. return -EINVAL;
  293. }
  294. if (use_cpucp_enum)
  295. rc = hl_get_fan_speed(hdev, channel, cpucp_attr, val);
  296. else
  297. rc = hl_get_fan_speed(hdev, channel, fixup_attr_legacy_fw(attr), val);
  298. break;
  299. case hwmon_pwm:
  300. switch (attr) {
  301. case hwmon_pwm_input:
  302. cpucp_attr = cpucp_pwm_input;
  303. break;
  304. case hwmon_pwm_enable:
  305. cpucp_attr = cpucp_pwm_enable;
  306. break;
  307. default:
  308. return -EINVAL;
  309. }
  310. if (use_cpucp_enum)
  311. rc = hl_get_pwm_info(hdev, channel, cpucp_attr, val);
  312. else
  313. /* no need for fixup as pwm was aligned from day 1 */
  314. rc = hl_get_pwm_info(hdev, channel, attr, val);
  315. break;
  316. case hwmon_power:
  317. switch (attr) {
  318. case hwmon_power_input:
  319. cpucp_attr = CPUCP_POWER_INPUT;
  320. break;
  321. case hwmon_power_input_highest:
  322. cpucp_attr = CPUCP_POWER_INPUT_HIGHEST;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. if (use_cpucp_enum)
  328. rc = hl_get_power(hdev, channel, cpucp_attr, val);
  329. else
  330. rc = hl_get_power(hdev, channel, fixup_attr_legacy_fw(attr), val);
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. return rc;
  336. }
  337. static int hl_write(struct device *dev, enum hwmon_sensor_types type,
  338. u32 attr, int channel, long val)
  339. {
  340. struct hl_device *hdev = dev_get_drvdata(dev);
  341. u32 cpucp_attr;
  342. bool use_cpucp_enum = (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
  343. CPU_BOOT_DEV_STS0_MAP_HWMON_EN) ? true : false;
  344. if (!hl_device_operational(hdev, NULL))
  345. return -ENODEV;
  346. switch (type) {
  347. case hwmon_temp:
  348. switch (attr) {
  349. case hwmon_temp_offset:
  350. cpucp_attr = cpucp_temp_offset;
  351. break;
  352. case hwmon_temp_reset_history:
  353. cpucp_attr = cpucp_temp_reset_history;
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. if (use_cpucp_enum)
  359. hl_set_temperature(hdev, channel, cpucp_attr, val);
  360. else
  361. hl_set_temperature(hdev, channel, fixup_attr_legacy_fw(attr), val);
  362. break;
  363. case hwmon_pwm:
  364. switch (attr) {
  365. case hwmon_pwm_input:
  366. cpucp_attr = cpucp_pwm_input;
  367. break;
  368. case hwmon_pwm_enable:
  369. cpucp_attr = cpucp_pwm_enable;
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. if (use_cpucp_enum)
  375. hl_set_pwm_info(hdev, channel, cpucp_attr, val);
  376. else
  377. /* no need for fixup as pwm was aligned from day 1 */
  378. hl_set_pwm_info(hdev, channel, attr, val);
  379. break;
  380. case hwmon_in:
  381. switch (attr) {
  382. case hwmon_in_reset_history:
  383. cpucp_attr = cpucp_in_reset_history;
  384. break;
  385. default:
  386. return -EINVAL;
  387. }
  388. if (use_cpucp_enum)
  389. hl_set_voltage(hdev, channel, cpucp_attr, val);
  390. else
  391. hl_set_voltage(hdev, channel, fixup_attr_legacy_fw(attr), val);
  392. break;
  393. case hwmon_curr:
  394. switch (attr) {
  395. case hwmon_curr_reset_history:
  396. cpucp_attr = cpucp_curr_reset_history;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. if (use_cpucp_enum)
  402. hl_set_current(hdev, channel, cpucp_attr, val);
  403. else
  404. hl_set_current(hdev, channel, fixup_attr_legacy_fw(attr), val);
  405. break;
  406. case hwmon_power:
  407. switch (attr) {
  408. case hwmon_power_reset_history:
  409. cpucp_attr = CPUCP_POWER_RESET_INPUT_HISTORY;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. if (use_cpucp_enum)
  415. hl_set_power(hdev, channel, cpucp_attr, val);
  416. else
  417. hl_set_power(hdev, channel, fixup_attr_legacy_fw(attr), val);
  418. break;
  419. default:
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. static umode_t hl_is_visible(const void *data, enum hwmon_sensor_types type,
  425. u32 attr, int channel)
  426. {
  427. switch (type) {
  428. case hwmon_temp:
  429. switch (attr) {
  430. case hwmon_temp_input:
  431. case hwmon_temp_max:
  432. case hwmon_temp_max_hyst:
  433. case hwmon_temp_crit:
  434. case hwmon_temp_crit_hyst:
  435. case hwmon_temp_highest:
  436. return 0444;
  437. case hwmon_temp_offset:
  438. return 0644;
  439. case hwmon_temp_reset_history:
  440. return 0200;
  441. }
  442. break;
  443. case hwmon_in:
  444. switch (attr) {
  445. case hwmon_in_input:
  446. case hwmon_in_min:
  447. case hwmon_in_max:
  448. case hwmon_in_highest:
  449. return 0444;
  450. case hwmon_in_reset_history:
  451. return 0200;
  452. }
  453. break;
  454. case hwmon_curr:
  455. switch (attr) {
  456. case hwmon_curr_input:
  457. case hwmon_curr_min:
  458. case hwmon_curr_max:
  459. case hwmon_curr_highest:
  460. return 0444;
  461. case hwmon_curr_reset_history:
  462. return 0200;
  463. }
  464. break;
  465. case hwmon_fan:
  466. switch (attr) {
  467. case hwmon_fan_input:
  468. case hwmon_fan_min:
  469. case hwmon_fan_max:
  470. return 0444;
  471. }
  472. break;
  473. case hwmon_pwm:
  474. switch (attr) {
  475. case hwmon_pwm_input:
  476. case hwmon_pwm_enable:
  477. return 0644;
  478. }
  479. break;
  480. case hwmon_power:
  481. switch (attr) {
  482. case hwmon_power_input:
  483. case hwmon_power_input_highest:
  484. return 0444;
  485. case hwmon_power_reset_history:
  486. return 0200;
  487. }
  488. break;
  489. default:
  490. break;
  491. }
  492. return 0;
  493. }
  494. static const struct hwmon_ops hl_hwmon_ops = {
  495. .is_visible = hl_is_visible,
  496. .read = hl_read,
  497. .write = hl_write
  498. };
  499. int hl_get_temperature(struct hl_device *hdev,
  500. int sensor_index, u32 attr, long *value)
  501. {
  502. struct cpucp_packet pkt;
  503. u64 result;
  504. int rc;
  505. memset(&pkt, 0, sizeof(pkt));
  506. pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_GET <<
  507. CPUCP_PKT_CTL_OPCODE_SHIFT);
  508. pkt.sensor_index = __cpu_to_le16(sensor_index);
  509. pkt.type = __cpu_to_le16(attr);
  510. dev_dbg(hdev->dev, "get temp, ctl 0x%x, sensor %d, type %d\n",
  511. pkt.ctl, pkt.sensor_index, pkt.type);
  512. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  513. 0, &result);
  514. *value = (long) result;
  515. if (rc) {
  516. dev_err(hdev->dev,
  517. "Failed to get temperature from sensor %d, error %d\n",
  518. sensor_index, rc);
  519. *value = 0;
  520. }
  521. return rc;
  522. }
  523. int hl_set_temperature(struct hl_device *hdev,
  524. int sensor_index, u32 attr, long value)
  525. {
  526. struct cpucp_packet pkt;
  527. int rc;
  528. memset(&pkt, 0, sizeof(pkt));
  529. pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_SET <<
  530. CPUCP_PKT_CTL_OPCODE_SHIFT);
  531. pkt.sensor_index = __cpu_to_le16(sensor_index);
  532. pkt.type = __cpu_to_le16(attr);
  533. pkt.value = __cpu_to_le64(value);
  534. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  535. 0, NULL);
  536. if (rc)
  537. dev_err(hdev->dev,
  538. "Failed to set temperature of sensor %d, error %d\n",
  539. sensor_index, rc);
  540. return rc;
  541. }
  542. int hl_get_voltage(struct hl_device *hdev,
  543. int sensor_index, u32 attr, long *value)
  544. {
  545. struct cpucp_packet pkt;
  546. u64 result;
  547. int rc;
  548. memset(&pkt, 0, sizeof(pkt));
  549. pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_GET <<
  550. CPUCP_PKT_CTL_OPCODE_SHIFT);
  551. pkt.sensor_index = __cpu_to_le16(sensor_index);
  552. pkt.type = __cpu_to_le16(attr);
  553. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  554. 0, &result);
  555. *value = (long) result;
  556. if (rc) {
  557. dev_err(hdev->dev,
  558. "Failed to get voltage from sensor %d, error %d\n",
  559. sensor_index, rc);
  560. *value = 0;
  561. }
  562. return rc;
  563. }
  564. int hl_get_current(struct hl_device *hdev,
  565. int sensor_index, u32 attr, long *value)
  566. {
  567. struct cpucp_packet pkt;
  568. u64 result;
  569. int rc;
  570. memset(&pkt, 0, sizeof(pkt));
  571. pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_GET <<
  572. CPUCP_PKT_CTL_OPCODE_SHIFT);
  573. pkt.sensor_index = __cpu_to_le16(sensor_index);
  574. pkt.type = __cpu_to_le16(attr);
  575. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  576. 0, &result);
  577. *value = (long) result;
  578. if (rc) {
  579. dev_err(hdev->dev,
  580. "Failed to get current from sensor %d, error %d\n",
  581. sensor_index, rc);
  582. *value = 0;
  583. }
  584. return rc;
  585. }
  586. int hl_get_fan_speed(struct hl_device *hdev,
  587. int sensor_index, u32 attr, long *value)
  588. {
  589. struct cpucp_packet pkt;
  590. u64 result;
  591. int rc;
  592. memset(&pkt, 0, sizeof(pkt));
  593. pkt.ctl = cpu_to_le32(CPUCP_PACKET_FAN_SPEED_GET <<
  594. CPUCP_PKT_CTL_OPCODE_SHIFT);
  595. pkt.sensor_index = __cpu_to_le16(sensor_index);
  596. pkt.type = __cpu_to_le16(attr);
  597. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  598. 0, &result);
  599. *value = (long) result;
  600. if (rc) {
  601. dev_err(hdev->dev,
  602. "Failed to get fan speed from sensor %d, error %d\n",
  603. sensor_index, rc);
  604. *value = 0;
  605. }
  606. return rc;
  607. }
  608. int hl_get_pwm_info(struct hl_device *hdev,
  609. int sensor_index, u32 attr, long *value)
  610. {
  611. struct cpucp_packet pkt;
  612. u64 result;
  613. int rc;
  614. memset(&pkt, 0, sizeof(pkt));
  615. pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_GET <<
  616. CPUCP_PKT_CTL_OPCODE_SHIFT);
  617. pkt.sensor_index = __cpu_to_le16(sensor_index);
  618. pkt.type = __cpu_to_le16(attr);
  619. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  620. 0, &result);
  621. *value = (long) result;
  622. if (rc) {
  623. dev_err(hdev->dev,
  624. "Failed to get pwm info from sensor %d, error %d\n",
  625. sensor_index, rc);
  626. *value = 0;
  627. }
  628. return rc;
  629. }
  630. void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
  631. long value)
  632. {
  633. struct cpucp_packet pkt;
  634. int rc;
  635. memset(&pkt, 0, sizeof(pkt));
  636. pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_SET <<
  637. CPUCP_PKT_CTL_OPCODE_SHIFT);
  638. pkt.sensor_index = __cpu_to_le16(sensor_index);
  639. pkt.type = __cpu_to_le16(attr);
  640. pkt.value = cpu_to_le64(value);
  641. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  642. 0, NULL);
  643. if (rc)
  644. dev_err(hdev->dev,
  645. "Failed to set pwm info to sensor %d, error %d\n",
  646. sensor_index, rc);
  647. }
  648. int hl_set_voltage(struct hl_device *hdev,
  649. int sensor_index, u32 attr, long value)
  650. {
  651. struct cpucp_packet pkt;
  652. int rc;
  653. memset(&pkt, 0, sizeof(pkt));
  654. pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_SET <<
  655. CPUCP_PKT_CTL_OPCODE_SHIFT);
  656. pkt.sensor_index = __cpu_to_le16(sensor_index);
  657. pkt.type = __cpu_to_le16(attr);
  658. pkt.value = __cpu_to_le64(value);
  659. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  660. 0, NULL);
  661. if (rc)
  662. dev_err(hdev->dev,
  663. "Failed to set voltage of sensor %d, error %d\n",
  664. sensor_index, rc);
  665. return rc;
  666. }
  667. int hl_set_current(struct hl_device *hdev,
  668. int sensor_index, u32 attr, long value)
  669. {
  670. struct cpucp_packet pkt;
  671. int rc;
  672. memset(&pkt, 0, sizeof(pkt));
  673. pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_SET <<
  674. CPUCP_PKT_CTL_OPCODE_SHIFT);
  675. pkt.sensor_index = __cpu_to_le16(sensor_index);
  676. pkt.type = __cpu_to_le16(attr);
  677. pkt.value = __cpu_to_le64(value);
  678. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  679. 0, NULL);
  680. if (rc)
  681. dev_err(hdev->dev,
  682. "Failed to set current of sensor %d, error %d\n",
  683. sensor_index, rc);
  684. return rc;
  685. }
  686. int hl_set_power(struct hl_device *hdev,
  687. int sensor_index, u32 attr, long value)
  688. {
  689. struct cpucp_packet pkt;
  690. struct asic_fixed_properties *prop = &hdev->asic_prop;
  691. int rc;
  692. memset(&pkt, 0, sizeof(pkt));
  693. if (prop->use_get_power_for_reset_history)
  694. pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
  695. CPUCP_PKT_CTL_OPCODE_SHIFT);
  696. else
  697. pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_SET <<
  698. CPUCP_PKT_CTL_OPCODE_SHIFT);
  699. pkt.sensor_index = __cpu_to_le16(sensor_index);
  700. pkt.type = __cpu_to_le16(attr);
  701. pkt.value = __cpu_to_le64(value);
  702. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  703. 0, NULL);
  704. if (rc)
  705. dev_err(hdev->dev,
  706. "Failed to set power of sensor %d, error %d\n",
  707. sensor_index, rc);
  708. return rc;
  709. }
  710. int hl_get_power(struct hl_device *hdev,
  711. int sensor_index, u32 attr, long *value)
  712. {
  713. struct cpucp_packet pkt;
  714. u64 result;
  715. int rc;
  716. memset(&pkt, 0, sizeof(pkt));
  717. pkt.ctl = cpu_to_le32(CPUCP_PACKET_POWER_GET <<
  718. CPUCP_PKT_CTL_OPCODE_SHIFT);
  719. pkt.sensor_index = __cpu_to_le16(sensor_index);
  720. pkt.type = __cpu_to_le16(attr);
  721. rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
  722. 0, &result);
  723. *value = (long) result;
  724. if (rc) {
  725. dev_err(hdev->dev,
  726. "Failed to get power of sensor %d, error %d\n",
  727. sensor_index, rc);
  728. *value = 0;
  729. }
  730. return rc;
  731. }
  732. int hl_hwmon_init(struct hl_device *hdev)
  733. {
  734. struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev;
  735. struct asic_fixed_properties *prop = &hdev->asic_prop;
  736. int rc;
  737. if ((hdev->hwmon_initialized) || !(hdev->cpu_queues_enable))
  738. return 0;
  739. if (hdev->hl_chip_info->info) {
  740. hdev->hl_chip_info->ops = &hl_hwmon_ops;
  741. hdev->hwmon_dev = hwmon_device_register_with_info(dev,
  742. prop->cpucp_info.card_name, hdev,
  743. hdev->hl_chip_info, NULL);
  744. if (IS_ERR(hdev->hwmon_dev)) {
  745. rc = PTR_ERR(hdev->hwmon_dev);
  746. dev_err(hdev->dev,
  747. "Unable to register hwmon device: %d\n", rc);
  748. return rc;
  749. }
  750. dev_info(hdev->dev, "%s: add sensors information\n",
  751. dev_name(hdev->hwmon_dev));
  752. hdev->hwmon_initialized = true;
  753. } else {
  754. dev_info(hdev->dev, "no available sensors\n");
  755. }
  756. return 0;
  757. }
  758. void hl_hwmon_fini(struct hl_device *hdev)
  759. {
  760. if (!hdev->hwmon_initialized)
  761. return;
  762. hwmon_device_unregister(hdev->hwmon_dev);
  763. }
  764. void hl_hwmon_release_resources(struct hl_device *hdev)
  765. {
  766. const struct hwmon_channel_info **channel_info_arr;
  767. int i = 0;
  768. if (!hdev->hl_chip_info->info)
  769. return;
  770. channel_info_arr = hdev->hl_chip_info->info;
  771. while (channel_info_arr[i]) {
  772. kfree(channel_info_arr[i]->config);
  773. kfree(channel_info_arr[i]);
  774. i++;
  775. }
  776. kfree(channel_info_arr);
  777. hdev->hl_chip_info->info = NULL;
  778. }