xgene-hwmon.c 19 KB

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