xgene-hwmon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * APM X-Gene SoC Hardware Monitoring Driver
  4. *
  5. * Copyright (c) 2016, Applied Micro Circuits Corporation
  6. * Author: Loc Ho <[email protected]>
  7. * Hoan Tran <[email protected]>
  8. *
  9. * This driver provides the following features:
  10. * - Retrieve CPU total power (uW)
  11. * - Retrieve IO total power (uW)
  12. * - Retrieve SoC temperature (milli-degree C) and alarm
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/io.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kfifo.h>
  21. #include <linux/mailbox_controller.h>
  22. #include <linux/mailbox_client.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <acpi/pcc.h>
  27. /* SLIMpro message defines */
  28. #define MSG_TYPE_DBG 0
  29. #define MSG_TYPE_ERR 7
  30. #define MSG_TYPE_PWRMGMT 9
  31. #define MSG_TYPE(v) (((v) & 0xF0000000) >> 28)
  32. #define MSG_TYPE_SET(v) (((v) << 28) & 0xF0000000)
  33. #define MSG_SUBTYPE(v) (((v) & 0x0F000000) >> 24)
  34. #define MSG_SUBTYPE_SET(v) (((v) << 24) & 0x0F000000)
  35. #define DBG_SUBTYPE_SENSOR_READ 4
  36. #define SENSOR_RD_MSG 0x04FFE902
  37. #define SENSOR_RD_EN_ADDR(a) ((a) & 0x000FFFFF)
  38. #define PMD_PWR_REG 0x20
  39. #define PMD_PWR_MW_REG 0x26
  40. #define SOC_PWR_REG 0x21
  41. #define SOC_PWR_MW_REG 0x27
  42. #define SOC_TEMP_REG 0x10
  43. #define TEMP_NEGATIVE_BIT 8
  44. #define SENSOR_INVALID_DATA BIT(15)
  45. #define PWRMGMT_SUBTYPE_TPC 1
  46. #define TPC_ALARM 2
  47. #define TPC_GET_ALARM 3
  48. #define TPC_CMD(v) (((v) & 0x00FF0000) >> 16)
  49. #define TPC_CMD_SET(v) (((v) << 16) & 0x00FF0000)
  50. #define TPC_EN_MSG(hndl, cmd, type) \
  51. (MSG_TYPE_SET(MSG_TYPE_PWRMGMT) | \
  52. MSG_SUBTYPE_SET(hndl) | TPC_CMD_SET(cmd) | type)
  53. /* PCC defines */
  54. #define PCC_SIGNATURE_MASK 0x50424300
  55. #define PCCC_GENERATE_DB_INT BIT(15)
  56. #define PCCS_CMD_COMPLETE BIT(0)
  57. #define PCCS_SCI_DOORBEL BIT(1)
  58. #define PCCS_PLATFORM_NOTIFICATION BIT(3)
  59. /*
  60. * Arbitrary retries in case the remote processor is slow to respond
  61. * to PCC commands
  62. */
  63. #define PCC_NUM_RETRIES 500
  64. #define ASYNC_MSG_FIFO_SIZE 16
  65. #define MBOX_OP_TIMEOUTMS 1000
  66. #define WATT_TO_mWATT(x) ((x) * 1000)
  67. #define mWATT_TO_uWATT(x) ((x) * 1000)
  68. #define CELSIUS_TO_mCELSIUS(x) ((x) * 1000)
  69. #define to_xgene_hwmon_dev(cl) \
  70. container_of(cl, struct xgene_hwmon_dev, mbox_client)
  71. enum xgene_hwmon_version {
  72. XGENE_HWMON_V1 = 0,
  73. XGENE_HWMON_V2 = 1,
  74. };
  75. struct slimpro_resp_msg {
  76. u32 msg;
  77. u32 param1;
  78. u32 param2;
  79. } __packed;
  80. struct xgene_hwmon_dev {
  81. struct device *dev;
  82. struct mbox_chan *mbox_chan;
  83. struct pcc_mbox_chan *pcc_chan;
  84. struct mbox_client mbox_client;
  85. int mbox_idx;
  86. spinlock_t kfifo_lock;
  87. struct mutex rd_mutex;
  88. struct completion rd_complete;
  89. int resp_pending;
  90. struct slimpro_resp_msg sync_msg;
  91. struct work_struct workq;
  92. struct kfifo_rec_ptr_1 async_msg_fifo;
  93. struct device *hwmon_dev;
  94. bool temp_critical_alarm;
  95. phys_addr_t comm_base_addr;
  96. void *pcc_comm_addr;
  97. u64 usecs_lat;
  98. };
  99. /*
  100. * This function tests and clears a bitmask then returns its old value
  101. */
  102. static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask)
  103. {
  104. u16 ret, val;
  105. val = le16_to_cpu(READ_ONCE(*addr));
  106. ret = val & mask;
  107. val &= ~mask;
  108. WRITE_ONCE(*addr, cpu_to_le16(val));
  109. return ret;
  110. }
  111. static int xgene_hwmon_pcc_rd(struct xgene_hwmon_dev *ctx, u32 *msg)
  112. {
  113. struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
  114. u32 *ptr = (void *)(generic_comm_base + 1);
  115. int rc, i;
  116. u16 val;
  117. mutex_lock(&ctx->rd_mutex);
  118. init_completion(&ctx->rd_complete);
  119. ctx->resp_pending = true;
  120. /* Write signature for subspace */
  121. WRITE_ONCE(generic_comm_base->signature,
  122. cpu_to_le32(PCC_SIGNATURE_MASK | ctx->mbox_idx));
  123. /* Write to the shared command region */
  124. WRITE_ONCE(generic_comm_base->command,
  125. cpu_to_le16(MSG_TYPE(msg[0]) | PCCC_GENERATE_DB_INT));
  126. /* Flip CMD COMPLETE bit */
  127. val = le16_to_cpu(READ_ONCE(generic_comm_base->status));
  128. val &= ~PCCS_CMD_COMPLETE;
  129. WRITE_ONCE(generic_comm_base->status, cpu_to_le16(val));
  130. /* Copy the message to the PCC comm space */
  131. for (i = 0; i < sizeof(struct slimpro_resp_msg) / 4; i++)
  132. WRITE_ONCE(ptr[i], cpu_to_le32(msg[i]));
  133. /* Ring the doorbell */
  134. rc = mbox_send_message(ctx->mbox_chan, msg);
  135. if (rc < 0) {
  136. dev_err(ctx->dev, "Mailbox send error %d\n", rc);
  137. goto err;
  138. }
  139. if (!wait_for_completion_timeout(&ctx->rd_complete,
  140. usecs_to_jiffies(ctx->usecs_lat))) {
  141. dev_err(ctx->dev, "Mailbox operation timed out\n");
  142. rc = -ETIMEDOUT;
  143. goto err;
  144. }
  145. /* Check for error message */
  146. if (MSG_TYPE(ctx->sync_msg.msg) == MSG_TYPE_ERR) {
  147. rc = -EINVAL;
  148. goto err;
  149. }
  150. msg[0] = ctx->sync_msg.msg;
  151. msg[1] = ctx->sync_msg.param1;
  152. msg[2] = ctx->sync_msg.param2;
  153. err:
  154. mbox_chan_txdone(ctx->mbox_chan, 0);
  155. ctx->resp_pending = false;
  156. mutex_unlock(&ctx->rd_mutex);
  157. return rc;
  158. }
  159. static int xgene_hwmon_rd(struct xgene_hwmon_dev *ctx, u32 *msg)
  160. {
  161. int rc;
  162. mutex_lock(&ctx->rd_mutex);
  163. init_completion(&ctx->rd_complete);
  164. ctx->resp_pending = true;
  165. rc = mbox_send_message(ctx->mbox_chan, msg);
  166. if (rc < 0) {
  167. dev_err(ctx->dev, "Mailbox send error %d\n", rc);
  168. goto err;
  169. }
  170. if (!wait_for_completion_timeout(&ctx->rd_complete,
  171. msecs_to_jiffies(MBOX_OP_TIMEOUTMS))) {
  172. dev_err(ctx->dev, "Mailbox operation timed out\n");
  173. rc = -ETIMEDOUT;
  174. goto err;
  175. }
  176. /* Check for error message */
  177. if (MSG_TYPE(ctx->sync_msg.msg) == MSG_TYPE_ERR) {
  178. rc = -EINVAL;
  179. goto err;
  180. }
  181. msg[0] = ctx->sync_msg.msg;
  182. msg[1] = ctx->sync_msg.param1;
  183. msg[2] = ctx->sync_msg.param2;
  184. err:
  185. ctx->resp_pending = false;
  186. mutex_unlock(&ctx->rd_mutex);
  187. return rc;
  188. }
  189. static int xgene_hwmon_reg_map_rd(struct xgene_hwmon_dev *ctx, u32 addr,
  190. u32 *data)
  191. {
  192. u32 msg[3];
  193. int rc;
  194. msg[0] = SENSOR_RD_MSG;
  195. msg[1] = SENSOR_RD_EN_ADDR(addr);
  196. msg[2] = 0;
  197. if (acpi_disabled)
  198. rc = xgene_hwmon_rd(ctx, msg);
  199. else
  200. rc = xgene_hwmon_pcc_rd(ctx, msg);
  201. if (rc < 0)
  202. return rc;
  203. /*
  204. * Check if sensor data is valid.
  205. */
  206. if (msg[1] & SENSOR_INVALID_DATA)
  207. return -ENODATA;
  208. *data = msg[1];
  209. return rc;
  210. }
  211. static int xgene_hwmon_get_notification_msg(struct xgene_hwmon_dev *ctx,
  212. u32 *amsg)
  213. {
  214. u32 msg[3];
  215. int rc;
  216. msg[0] = TPC_EN_MSG(PWRMGMT_SUBTYPE_TPC, TPC_GET_ALARM, 0);
  217. msg[1] = 0;
  218. msg[2] = 0;
  219. rc = xgene_hwmon_pcc_rd(ctx, msg);
  220. if (rc < 0)
  221. return rc;
  222. amsg[0] = msg[0];
  223. amsg[1] = msg[1];
  224. amsg[2] = msg[2];
  225. return rc;
  226. }
  227. static int xgene_hwmon_get_cpu_pwr(struct xgene_hwmon_dev *ctx, u32 *val)
  228. {
  229. u32 watt, mwatt;
  230. int rc;
  231. rc = xgene_hwmon_reg_map_rd(ctx, PMD_PWR_REG, &watt);
  232. if (rc < 0)
  233. return rc;
  234. rc = xgene_hwmon_reg_map_rd(ctx, PMD_PWR_MW_REG, &mwatt);
  235. if (rc < 0)
  236. return rc;
  237. *val = WATT_TO_mWATT(watt) + mwatt;
  238. return 0;
  239. }
  240. static int xgene_hwmon_get_io_pwr(struct xgene_hwmon_dev *ctx, u32 *val)
  241. {
  242. u32 watt, mwatt;
  243. int rc;
  244. rc = xgene_hwmon_reg_map_rd(ctx, SOC_PWR_REG, &watt);
  245. if (rc < 0)
  246. return rc;
  247. rc = xgene_hwmon_reg_map_rd(ctx, SOC_PWR_MW_REG, &mwatt);
  248. if (rc < 0)
  249. return rc;
  250. *val = WATT_TO_mWATT(watt) + mwatt;
  251. return 0;
  252. }
  253. static int xgene_hwmon_get_temp(struct xgene_hwmon_dev *ctx, u32 *val)
  254. {
  255. return xgene_hwmon_reg_map_rd(ctx, SOC_TEMP_REG, val);
  256. }
  257. /*
  258. * Sensor temperature/power functions
  259. */
  260. static ssize_t temp1_input_show(struct device *dev,
  261. struct device_attribute *attr,
  262. char *buf)
  263. {
  264. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  265. int rc, temp;
  266. u32 val;
  267. rc = xgene_hwmon_get_temp(ctx, &val);
  268. if (rc < 0)
  269. return rc;
  270. temp = sign_extend32(val, TEMP_NEGATIVE_BIT);
  271. return sysfs_emit(buf, "%d\n", CELSIUS_TO_mCELSIUS(temp));
  272. }
  273. static ssize_t temp1_label_show(struct device *dev,
  274. struct device_attribute *attr,
  275. char *buf)
  276. {
  277. return sysfs_emit(buf, "SoC Temperature\n");
  278. }
  279. static ssize_t temp1_critical_alarm_show(struct device *dev,
  280. struct device_attribute *devattr,
  281. char *buf)
  282. {
  283. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  284. return sysfs_emit(buf, "%d\n", ctx->temp_critical_alarm);
  285. }
  286. static ssize_t power1_label_show(struct device *dev,
  287. struct device_attribute *attr,
  288. char *buf)
  289. {
  290. return sysfs_emit(buf, "CPU power\n");
  291. }
  292. static ssize_t power2_label_show(struct device *dev,
  293. struct device_attribute *attr,
  294. char *buf)
  295. {
  296. return sysfs_emit(buf, "IO power\n");
  297. }
  298. static ssize_t power1_input_show(struct device *dev,
  299. struct device_attribute *attr,
  300. char *buf)
  301. {
  302. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  303. u32 val;
  304. int rc;
  305. rc = xgene_hwmon_get_cpu_pwr(ctx, &val);
  306. if (rc < 0)
  307. return rc;
  308. return sysfs_emit(buf, "%u\n", mWATT_TO_uWATT(val));
  309. }
  310. static ssize_t power2_input_show(struct device *dev,
  311. struct device_attribute *attr,
  312. char *buf)
  313. {
  314. struct xgene_hwmon_dev *ctx = dev_get_drvdata(dev);
  315. u32 val;
  316. int rc;
  317. rc = xgene_hwmon_get_io_pwr(ctx, &val);
  318. if (rc < 0)
  319. return rc;
  320. return sysfs_emit(buf, "%u\n", mWATT_TO_uWATT(val));
  321. }
  322. static DEVICE_ATTR_RO(temp1_label);
  323. static DEVICE_ATTR_RO(temp1_input);
  324. static DEVICE_ATTR_RO(temp1_critical_alarm);
  325. static DEVICE_ATTR_RO(power1_label);
  326. static DEVICE_ATTR_RO(power1_input);
  327. static DEVICE_ATTR_RO(power2_label);
  328. static DEVICE_ATTR_RO(power2_input);
  329. static struct attribute *xgene_hwmon_attrs[] = {
  330. &dev_attr_temp1_label.attr,
  331. &dev_attr_temp1_input.attr,
  332. &dev_attr_temp1_critical_alarm.attr,
  333. &dev_attr_power1_label.attr,
  334. &dev_attr_power1_input.attr,
  335. &dev_attr_power2_label.attr,
  336. &dev_attr_power2_input.attr,
  337. NULL,
  338. };
  339. ATTRIBUTE_GROUPS(xgene_hwmon);
  340. static int xgene_hwmon_tpc_alarm(struct xgene_hwmon_dev *ctx,
  341. struct slimpro_resp_msg *amsg)
  342. {
  343. ctx->temp_critical_alarm = !!amsg->param2;
  344. sysfs_notify(&ctx->dev->kobj, NULL, "temp1_critical_alarm");
  345. return 0;
  346. }
  347. static void xgene_hwmon_process_pwrmsg(struct xgene_hwmon_dev *ctx,
  348. struct slimpro_resp_msg *amsg)
  349. {
  350. if ((MSG_SUBTYPE(amsg->msg) == PWRMGMT_SUBTYPE_TPC) &&
  351. (TPC_CMD(amsg->msg) == TPC_ALARM))
  352. xgene_hwmon_tpc_alarm(ctx, amsg);
  353. }
  354. /*
  355. * This function is called to process async work queue
  356. */
  357. static void xgene_hwmon_evt_work(struct work_struct *work)
  358. {
  359. struct slimpro_resp_msg amsg;
  360. struct xgene_hwmon_dev *ctx;
  361. int ret;
  362. ctx = container_of(work, struct xgene_hwmon_dev, workq);
  363. while (kfifo_out_spinlocked(&ctx->async_msg_fifo, &amsg,
  364. sizeof(struct slimpro_resp_msg),
  365. &ctx->kfifo_lock)) {
  366. /*
  367. * If PCC, send a consumer command to Platform to get info
  368. * If Slimpro Mailbox, get message from specific FIFO
  369. */
  370. if (!acpi_disabled) {
  371. ret = xgene_hwmon_get_notification_msg(ctx,
  372. (u32 *)&amsg);
  373. if (ret < 0)
  374. continue;
  375. }
  376. if (MSG_TYPE(amsg.msg) == MSG_TYPE_PWRMGMT)
  377. xgene_hwmon_process_pwrmsg(ctx, &amsg);
  378. }
  379. }
  380. static int xgene_hwmon_rx_ready(struct xgene_hwmon_dev *ctx, void *msg)
  381. {
  382. if (IS_ERR_OR_NULL(ctx->hwmon_dev) && !ctx->resp_pending) {
  383. /* Enqueue to the FIFO */
  384. kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
  385. sizeof(struct slimpro_resp_msg),
  386. &ctx->kfifo_lock);
  387. return -ENODEV;
  388. }
  389. return 0;
  390. }
  391. /*
  392. * This function is called when the SLIMpro Mailbox received a message
  393. */
  394. static void xgene_hwmon_rx_cb(struct mbox_client *cl, void *msg)
  395. {
  396. struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
  397. /*
  398. * While the driver registers with the mailbox framework, an interrupt
  399. * can be pending before the probe function completes its
  400. * initialization. If such condition occurs, just queue up the message
  401. * as the driver is not ready for servicing the callback.
  402. */
  403. if (xgene_hwmon_rx_ready(ctx, msg) < 0)
  404. return;
  405. /*
  406. * Response message format:
  407. * msg[0] is the return code of the operation
  408. * msg[1] is the first parameter word
  409. * msg[2] is the second parameter word
  410. *
  411. * As message only supports dword size, just assign it.
  412. */
  413. /* Check for sync query */
  414. if (ctx->resp_pending &&
  415. ((MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_ERR) ||
  416. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_DBG &&
  417. MSG_SUBTYPE(((u32 *)msg)[0]) == DBG_SUBTYPE_SENSOR_READ) ||
  418. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_PWRMGMT &&
  419. MSG_SUBTYPE(((u32 *)msg)[0]) == PWRMGMT_SUBTYPE_TPC &&
  420. TPC_CMD(((u32 *)msg)[0]) == TPC_ALARM))) {
  421. ctx->sync_msg.msg = ((u32 *)msg)[0];
  422. ctx->sync_msg.param1 = ((u32 *)msg)[1];
  423. ctx->sync_msg.param2 = ((u32 *)msg)[2];
  424. /* Operation waiting for response */
  425. complete(&ctx->rd_complete);
  426. return;
  427. }
  428. /* Enqueue to the FIFO */
  429. kfifo_in_spinlocked(&ctx->async_msg_fifo, msg,
  430. sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
  431. /* Schedule the bottom handler */
  432. schedule_work(&ctx->workq);
  433. }
  434. /*
  435. * This function is called when the PCC Mailbox received a message
  436. */
  437. static void xgene_hwmon_pcc_rx_cb(struct mbox_client *cl, void *msg)
  438. {
  439. struct xgene_hwmon_dev *ctx = to_xgene_hwmon_dev(cl);
  440. struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
  441. struct slimpro_resp_msg amsg;
  442. /*
  443. * While the driver registers with the mailbox framework, an interrupt
  444. * can be pending before the probe function completes its
  445. * initialization. If such condition occurs, just queue up the message
  446. * as the driver is not ready for servicing the callback.
  447. */
  448. if (xgene_hwmon_rx_ready(ctx, &amsg) < 0)
  449. return;
  450. msg = generic_comm_base + 1;
  451. /* Check if platform sends interrupt */
  452. if (!xgene_word_tst_and_clr(&generic_comm_base->status,
  453. PCCS_SCI_DOORBEL))
  454. return;
  455. /*
  456. * Response message format:
  457. * msg[0] is the return code of the operation
  458. * msg[1] is the first parameter word
  459. * msg[2] is the second parameter word
  460. *
  461. * As message only supports dword size, just assign it.
  462. */
  463. /* Check for sync query */
  464. if (ctx->resp_pending &&
  465. ((MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_ERR) ||
  466. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_DBG &&
  467. MSG_SUBTYPE(((u32 *)msg)[0]) == DBG_SUBTYPE_SENSOR_READ) ||
  468. (MSG_TYPE(((u32 *)msg)[0]) == MSG_TYPE_PWRMGMT &&
  469. MSG_SUBTYPE(((u32 *)msg)[0]) == PWRMGMT_SUBTYPE_TPC &&
  470. TPC_CMD(((u32 *)msg)[0]) == TPC_ALARM))) {
  471. /* Check if platform completes command */
  472. if (xgene_word_tst_and_clr(&generic_comm_base->status,
  473. PCCS_CMD_COMPLETE)) {
  474. ctx->sync_msg.msg = ((u32 *)msg)[0];
  475. ctx->sync_msg.param1 = ((u32 *)msg)[1];
  476. ctx->sync_msg.param2 = ((u32 *)msg)[2];
  477. /* Operation waiting for response */
  478. complete(&ctx->rd_complete);
  479. return;
  480. }
  481. }
  482. /*
  483. * Platform notifies interrupt to OSPM.
  484. * OPSM schedules a consumer command to get this information
  485. * in a workqueue. Platform must wait until OSPM has issued
  486. * a consumer command that serves this notification.
  487. */
  488. /* Enqueue to the FIFO */
  489. kfifo_in_spinlocked(&ctx->async_msg_fifo, &amsg,
  490. sizeof(struct slimpro_resp_msg), &ctx->kfifo_lock);
  491. /* Schedule the bottom handler */
  492. schedule_work(&ctx->workq);
  493. }
  494. static void xgene_hwmon_tx_done(struct mbox_client *cl, void *msg, int ret)
  495. {
  496. if (ret) {
  497. dev_dbg(cl->dev, "TX did not complete: CMD sent:%x, ret:%d\n",
  498. *(u16 *)msg, ret);
  499. } else {
  500. dev_dbg(cl->dev, "TX completed. CMD sent:%x, ret:%d\n",
  501. *(u16 *)msg, ret);
  502. }
  503. }
  504. #ifdef CONFIG_ACPI
  505. static const struct acpi_device_id xgene_hwmon_acpi_match[] = {
  506. {"APMC0D29", XGENE_HWMON_V1},
  507. {"APMC0D8A", XGENE_HWMON_V2},
  508. {},
  509. };
  510. MODULE_DEVICE_TABLE(acpi, xgene_hwmon_acpi_match);
  511. #endif
  512. static int xgene_hwmon_probe(struct platform_device *pdev)
  513. {
  514. struct xgene_hwmon_dev *ctx;
  515. struct mbox_client *cl;
  516. int rc;
  517. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  518. if (!ctx)
  519. return -ENOMEM;
  520. ctx->dev = &pdev->dev;
  521. platform_set_drvdata(pdev, ctx);
  522. cl = &ctx->mbox_client;
  523. spin_lock_init(&ctx->kfifo_lock);
  524. mutex_init(&ctx->rd_mutex);
  525. rc = kfifo_alloc(&ctx->async_msg_fifo,
  526. sizeof(struct slimpro_resp_msg) * ASYNC_MSG_FIFO_SIZE,
  527. GFP_KERNEL);
  528. if (rc)
  529. return -ENOMEM;
  530. INIT_WORK(&ctx->workq, xgene_hwmon_evt_work);
  531. /* Request mailbox channel */
  532. cl->dev = &pdev->dev;
  533. cl->tx_done = xgene_hwmon_tx_done;
  534. cl->tx_block = false;
  535. cl->tx_tout = MBOX_OP_TIMEOUTMS;
  536. cl->knows_txdone = false;
  537. if (acpi_disabled) {
  538. cl->rx_callback = xgene_hwmon_rx_cb;
  539. ctx->mbox_chan = mbox_request_channel(cl, 0);
  540. if (IS_ERR(ctx->mbox_chan)) {
  541. dev_err(&pdev->dev,
  542. "SLIMpro mailbox channel request failed\n");
  543. rc = -ENODEV;
  544. goto out_mbox_free;
  545. }
  546. } else {
  547. struct pcc_mbox_chan *pcc_chan;
  548. const struct acpi_device_id *acpi_id;
  549. int version;
  550. acpi_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  551. &pdev->dev);
  552. if (!acpi_id) {
  553. rc = -EINVAL;
  554. goto out_mbox_free;
  555. }
  556. version = (int)acpi_id->driver_data;
  557. if (device_property_read_u32(&pdev->dev, "pcc-channel",
  558. &ctx->mbox_idx)) {
  559. dev_err(&pdev->dev, "no pcc-channel property\n");
  560. rc = -ENODEV;
  561. goto out_mbox_free;
  562. }
  563. cl->rx_callback = xgene_hwmon_pcc_rx_cb;
  564. pcc_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx);
  565. if (IS_ERR(pcc_chan)) {
  566. dev_err(&pdev->dev,
  567. "PPC channel request failed\n");
  568. rc = -ENODEV;
  569. goto out_mbox_free;
  570. }
  571. ctx->pcc_chan = pcc_chan;
  572. ctx->mbox_chan = pcc_chan->mchan;
  573. if (!ctx->mbox_chan->mbox->txdone_irq) {
  574. dev_err(&pdev->dev, "PCC IRQ not supported\n");
  575. rc = -ENODEV;
  576. goto out;
  577. }
  578. /*
  579. * This is the shared communication region
  580. * for the OS and Platform to communicate over.
  581. */
  582. ctx->comm_base_addr = pcc_chan->shmem_base_addr;
  583. if (ctx->comm_base_addr) {
  584. if (version == XGENE_HWMON_V2)
  585. ctx->pcc_comm_addr = (void __force *)devm_ioremap(&pdev->dev,
  586. ctx->comm_base_addr,
  587. pcc_chan->shmem_size);
  588. else
  589. ctx->pcc_comm_addr = devm_memremap(&pdev->dev,
  590. ctx->comm_base_addr,
  591. pcc_chan->shmem_size,
  592. MEMREMAP_WB);
  593. } else {
  594. dev_err(&pdev->dev, "Failed to get PCC comm region\n");
  595. rc = -ENODEV;
  596. goto out;
  597. }
  598. if (!ctx->pcc_comm_addr) {
  599. dev_err(&pdev->dev,
  600. "Failed to ioremap PCC comm region\n");
  601. rc = -ENOMEM;
  602. goto out;
  603. }
  604. /*
  605. * pcc_chan->latency is just a Nominal value. In reality
  606. * the remote processor could be much slower to reply.
  607. * So add an arbitrary amount of wait on top of Nominal.
  608. */
  609. ctx->usecs_lat = PCC_NUM_RETRIES * pcc_chan->latency;
  610. }
  611. ctx->hwmon_dev = hwmon_device_register_with_groups(ctx->dev,
  612. "apm_xgene",
  613. ctx,
  614. xgene_hwmon_groups);
  615. if (IS_ERR(ctx->hwmon_dev)) {
  616. dev_err(&pdev->dev, "Failed to register HW monitor device\n");
  617. rc = PTR_ERR(ctx->hwmon_dev);
  618. goto out;
  619. }
  620. /*
  621. * Schedule the bottom handler if there is a pending message.
  622. */
  623. schedule_work(&ctx->workq);
  624. dev_info(&pdev->dev, "APM X-Gene SoC HW monitor driver registered\n");
  625. return 0;
  626. out:
  627. if (acpi_disabled)
  628. mbox_free_channel(ctx->mbox_chan);
  629. else
  630. pcc_mbox_free_channel(ctx->pcc_chan);
  631. out_mbox_free:
  632. kfifo_free(&ctx->async_msg_fifo);
  633. return rc;
  634. }
  635. static int xgene_hwmon_remove(struct platform_device *pdev)
  636. {
  637. struct xgene_hwmon_dev *ctx = platform_get_drvdata(pdev);
  638. cancel_work_sync(&ctx->workq);
  639. hwmon_device_unregister(ctx->hwmon_dev);
  640. kfifo_free(&ctx->async_msg_fifo);
  641. if (acpi_disabled)
  642. mbox_free_channel(ctx->mbox_chan);
  643. else
  644. pcc_mbox_free_channel(ctx->pcc_chan);
  645. return 0;
  646. }
  647. static const struct of_device_id xgene_hwmon_of_match[] = {
  648. {.compatible = "apm,xgene-slimpro-hwmon"},
  649. {}
  650. };
  651. MODULE_DEVICE_TABLE(of, xgene_hwmon_of_match);
  652. static struct platform_driver xgene_hwmon_driver = {
  653. .probe = xgene_hwmon_probe,
  654. .remove = xgene_hwmon_remove,
  655. .driver = {
  656. .name = "xgene-slimpro-hwmon",
  657. .of_match_table = xgene_hwmon_of_match,
  658. .acpi_match_table = ACPI_PTR(xgene_hwmon_acpi_match),
  659. },
  660. };
  661. module_platform_driver(xgene_hwmon_driver);
  662. MODULE_DESCRIPTION("APM X-Gene SoC hardware monitor");
  663. MODULE_LICENSE("GPL");