xgene-hwmon.c 20 KB

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