qcom-ipcc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/mailbox_controller.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <dt-bindings/mailbox/qcom-ipcc.h>
  13. /* IPCC Register offsets */
  14. #define IPCC_REG_CONFIG 0x08
  15. #define IPCC_REG_SEND_ID 0x0c
  16. #define IPCC_REG_RECV_ID 0x10
  17. #define IPCC_REG_RECV_SIGNAL_ENABLE 0x14
  18. #define IPCC_REG_RECV_SIGNAL_DISABLE 0x18
  19. #define IPCC_REG_RECV_SIGNAL_CLEAR 0x1c
  20. #define IPCC_REG_CLIENT_CLEAR 0x38
  21. #define IPCC_CLEAR_ON_RECV_RD BIT(0)
  22. #define IPCC_SIGNAL_ID_MASK GENMASK(15, 0)
  23. #define IPCC_CLIENT_ID_MASK GENMASK(31, 16)
  24. #define IPCC_NO_PENDING_IRQ GENMASK(31, 0)
  25. /**
  26. * struct qcom_ipcc_chan_info - Per-mailbox-channel info
  27. * @client_id: The client-id to which the interrupt has to be triggered
  28. * @signal_id: The signal-id to which the interrupt has to be triggered
  29. */
  30. struct qcom_ipcc_chan_info {
  31. u16 client_id;
  32. u16 signal_id;
  33. };
  34. /**
  35. * struct qcom_ipcc - Holder for the mailbox driver
  36. * @dev: Device associated with this instance
  37. * @base: Base address of the IPCC frame associated to APSS
  38. * @irq_domain: The irq_domain associated with this instance
  39. * @chans: The mailbox channels array
  40. * @mchan: The per-mailbox channel info array
  41. * @mbox: The mailbox controller
  42. * @num_chans: Number of @chans elements
  43. * @irq: Summary irq
  44. */
  45. struct qcom_ipcc {
  46. struct device *dev;
  47. void __iomem *base;
  48. struct irq_domain *irq_domain;
  49. struct mbox_chan *chans;
  50. struct qcom_ipcc_chan_info *mchan;
  51. struct mbox_controller mbox;
  52. int num_chans;
  53. int irq;
  54. };
  55. static inline struct qcom_ipcc *to_qcom_ipcc(struct mbox_controller *mbox)
  56. {
  57. return container_of(mbox, struct qcom_ipcc, mbox);
  58. }
  59. static inline u32 qcom_ipcc_get_hwirq(u16 client_id, u16 signal_id)
  60. {
  61. return FIELD_PREP(IPCC_CLIENT_ID_MASK, client_id) |
  62. FIELD_PREP(IPCC_SIGNAL_ID_MASK, signal_id);
  63. }
  64. static irqreturn_t qcom_ipcc_irq_fn(int irq, void *data)
  65. {
  66. struct qcom_ipcc *ipcc = data;
  67. u32 hwirq;
  68. int virq;
  69. for (;;) {
  70. hwirq = readl(ipcc->base + IPCC_REG_RECV_ID);
  71. if (hwirq == IPCC_NO_PENDING_IRQ)
  72. break;
  73. virq = irq_find_mapping(ipcc->irq_domain, hwirq);
  74. writel(hwirq, ipcc->base + IPCC_REG_RECV_SIGNAL_CLEAR);
  75. generic_handle_irq(virq);
  76. }
  77. return IRQ_HANDLED;
  78. }
  79. static void qcom_ipcc_mask_irq(struct irq_data *irqd)
  80. {
  81. struct qcom_ipcc *ipcc = irq_data_get_irq_chip_data(irqd);
  82. irq_hw_number_t hwirq = irqd_to_hwirq(irqd);
  83. writel(hwirq, ipcc->base + IPCC_REG_RECV_SIGNAL_DISABLE);
  84. }
  85. static void qcom_ipcc_unmask_irq(struct irq_data *irqd)
  86. {
  87. struct qcom_ipcc *ipcc = irq_data_get_irq_chip_data(irqd);
  88. irq_hw_number_t hwirq = irqd_to_hwirq(irqd);
  89. writel(hwirq, ipcc->base + IPCC_REG_RECV_SIGNAL_ENABLE);
  90. }
  91. static struct irq_chip qcom_ipcc_irq_chip = {
  92. .name = "ipcc",
  93. .irq_mask = qcom_ipcc_mask_irq,
  94. .irq_unmask = qcom_ipcc_unmask_irq,
  95. .flags = IRQCHIP_SKIP_SET_WAKE,
  96. };
  97. static int qcom_ipcc_domain_map(struct irq_domain *d, unsigned int irq,
  98. irq_hw_number_t hw)
  99. {
  100. struct qcom_ipcc *ipcc = d->host_data;
  101. irq_set_chip_and_handler(irq, &qcom_ipcc_irq_chip, handle_level_irq);
  102. irq_set_chip_data(irq, ipcc);
  103. irq_set_noprobe(irq);
  104. return 0;
  105. }
  106. static int qcom_ipcc_domain_xlate(struct irq_domain *d,
  107. struct device_node *node, const u32 *intspec,
  108. unsigned int intsize,
  109. unsigned long *out_hwirq,
  110. unsigned int *out_type)
  111. {
  112. if (intsize != 3)
  113. return -EINVAL;
  114. *out_hwirq = qcom_ipcc_get_hwirq(intspec[0], intspec[1]);
  115. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  116. return 0;
  117. }
  118. static const struct irq_domain_ops qcom_ipcc_irq_ops = {
  119. .map = qcom_ipcc_domain_map,
  120. .xlate = qcom_ipcc_domain_xlate,
  121. };
  122. static int qcom_ipcc_mbox_send_data(struct mbox_chan *chan, void *data)
  123. {
  124. struct qcom_ipcc *ipcc = to_qcom_ipcc(chan->mbox);
  125. struct qcom_ipcc_chan_info *mchan = chan->con_priv;
  126. u32 hwirq;
  127. hwirq = qcom_ipcc_get_hwirq(mchan->client_id, mchan->signal_id);
  128. writel(hwirq, ipcc->base + IPCC_REG_SEND_ID);
  129. return 0;
  130. }
  131. static void qcom_ipcc_mbox_shutdown(struct mbox_chan *chan)
  132. {
  133. chan->con_priv = NULL;
  134. }
  135. static struct mbox_chan *qcom_ipcc_mbox_xlate(struct mbox_controller *mbox,
  136. const struct of_phandle_args *ph)
  137. {
  138. struct qcom_ipcc *ipcc = to_qcom_ipcc(mbox);
  139. struct qcom_ipcc_chan_info *mchan;
  140. struct mbox_chan *chan;
  141. struct device *dev;
  142. int chan_id;
  143. dev = ipcc->dev;
  144. if (ph->args_count != 2)
  145. return ERR_PTR(-EINVAL);
  146. for (chan_id = 0; chan_id < mbox->num_chans; chan_id++) {
  147. chan = &ipcc->chans[chan_id];
  148. mchan = chan->con_priv;
  149. if (!mchan)
  150. break;
  151. else if (mchan->client_id == ph->args[0] &&
  152. mchan->signal_id == ph->args[1])
  153. return ERR_PTR(-EBUSY);
  154. }
  155. if (chan_id >= mbox->num_chans)
  156. return ERR_PTR(-EBUSY);
  157. mchan = devm_kzalloc(dev, sizeof(*mchan), GFP_KERNEL);
  158. if (!mchan)
  159. return ERR_PTR(-ENOMEM);
  160. mchan->client_id = ph->args[0];
  161. mchan->signal_id = ph->args[1];
  162. chan->con_priv = mchan;
  163. return chan;
  164. }
  165. static const struct mbox_chan_ops ipcc_mbox_chan_ops = {
  166. .send_data = qcom_ipcc_mbox_send_data,
  167. .shutdown = qcom_ipcc_mbox_shutdown,
  168. };
  169. static int qcom_ipcc_setup_mbox(struct qcom_ipcc *ipcc,
  170. struct device_node *controller_dn)
  171. {
  172. struct of_phandle_args curr_ph;
  173. struct device_node *client_dn;
  174. struct mbox_controller *mbox;
  175. struct device *dev = ipcc->dev;
  176. int i, j, ret;
  177. /*
  178. * Find out the number of clients interested in this mailbox
  179. * and create channels accordingly.
  180. */
  181. ipcc->num_chans = 0;
  182. for_each_node_with_property(client_dn, "mboxes") {
  183. if (!of_device_is_available(client_dn))
  184. continue;
  185. i = of_count_phandle_with_args(client_dn,
  186. "mboxes", "#mbox-cells");
  187. for (j = 0; j < i; j++) {
  188. ret = of_parse_phandle_with_args(client_dn, "mboxes",
  189. "#mbox-cells", j, &curr_ph);
  190. of_node_put(curr_ph.np);
  191. if (!ret && curr_ph.np == controller_dn)
  192. ipcc->num_chans++;
  193. }
  194. }
  195. /* If no clients are found, skip registering as a mbox controller */
  196. if (!ipcc->num_chans)
  197. return 0;
  198. ipcc->chans = devm_kcalloc(dev, ipcc->num_chans,
  199. sizeof(struct mbox_chan), GFP_KERNEL);
  200. if (!ipcc->chans)
  201. return -ENOMEM;
  202. mbox = &ipcc->mbox;
  203. mbox->dev = dev;
  204. mbox->num_chans = ipcc->num_chans;
  205. mbox->chans = ipcc->chans;
  206. mbox->ops = &ipcc_mbox_chan_ops;
  207. mbox->of_xlate = qcom_ipcc_mbox_xlate;
  208. mbox->txdone_irq = false;
  209. mbox->txdone_poll = false;
  210. return devm_mbox_controller_register(dev, mbox);
  211. }
  212. static int qcom_ipcc_pm_resume(struct device *dev)
  213. {
  214. struct qcom_ipcc *ipcc = dev_get_drvdata(dev);
  215. u32 hwirq;
  216. int virq;
  217. hwirq = readl(ipcc->base + IPCC_REG_RECV_ID);
  218. if (hwirq == IPCC_NO_PENDING_IRQ)
  219. return 0;
  220. virq = irq_find_mapping(ipcc->irq_domain, hwirq);
  221. dev_dbg(dev, "virq: %d triggered client-id: %ld; signal-id: %ld\n", virq,
  222. FIELD_GET(IPCC_CLIENT_ID_MASK, hwirq), FIELD_GET(IPCC_SIGNAL_ID_MASK, hwirq));
  223. return 0;
  224. }
  225. static int qcom_ipcc_probe(struct platform_device *pdev)
  226. {
  227. struct qcom_ipcc *ipcc;
  228. u32 config_value;
  229. static int id;
  230. char *name;
  231. int ret;
  232. ipcc = devm_kzalloc(&pdev->dev, sizeof(*ipcc), GFP_KERNEL);
  233. if (!ipcc)
  234. return -ENOMEM;
  235. ipcc->dev = &pdev->dev;
  236. ipcc->base = devm_platform_ioremap_resource(pdev, 0);
  237. if (IS_ERR(ipcc->base))
  238. return PTR_ERR(ipcc->base);
  239. /*
  240. * It is possible that boot firmware is using the same IPCC instance
  241. * as of the HLOS and it has kept CLEAR_ON_RECV_RD set which basically
  242. * means Interrupt pending registers are cleared when RECV_ID is read.
  243. * The register automatically updates to the next pending interrupt/client
  244. * status based on priority.
  245. */
  246. config_value = readl(ipcc->base + IPCC_REG_CONFIG);
  247. if (config_value & IPCC_CLEAR_ON_RECV_RD) {
  248. config_value &= ~(IPCC_CLEAR_ON_RECV_RD);
  249. writel(config_value, ipcc->base + IPCC_REG_CONFIG);
  250. }
  251. ipcc->irq = platform_get_irq(pdev, 0);
  252. if (ipcc->irq < 0)
  253. return ipcc->irq;
  254. name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "ipcc_%d", id++);
  255. if (!name)
  256. return -ENOMEM;
  257. ipcc->irq_domain = irq_domain_create_tree(dev_fwnode(&pdev->dev), &qcom_ipcc_irq_ops, ipcc);
  258. if (!ipcc->irq_domain)
  259. return -ENOMEM;
  260. ret = qcom_ipcc_setup_mbox(ipcc, pdev->dev.of_node);
  261. if (ret)
  262. goto err_mbox;
  263. ret = devm_request_irq(&pdev->dev, ipcc->irq, qcom_ipcc_irq_fn,
  264. IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND |
  265. IRQF_NO_THREAD, name, ipcc);
  266. if (ret < 0) {
  267. dev_err(&pdev->dev, "Failed to register the irq: %d\n", ret);
  268. goto err_req_irq;
  269. }
  270. platform_set_drvdata(pdev, ipcc);
  271. return 0;
  272. err_req_irq:
  273. if (ipcc->num_chans)
  274. mbox_controller_unregister(&ipcc->mbox);
  275. err_mbox:
  276. irq_domain_remove(ipcc->irq_domain);
  277. return ret;
  278. }
  279. static void qcom_ipcc_remove(struct platform_device *pdev)
  280. {
  281. struct qcom_ipcc *ipcc = platform_get_drvdata(pdev);
  282. disable_irq_wake(ipcc->irq);
  283. irq_domain_remove(ipcc->irq_domain);
  284. }
  285. static const struct of_device_id qcom_ipcc_of_match[] = {
  286. { .compatible = "qcom,ipcc"},
  287. {}
  288. };
  289. MODULE_DEVICE_TABLE(of, qcom_ipcc_of_match);
  290. static const struct dev_pm_ops qcom_ipcc_dev_pm_ops = {
  291. NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, qcom_ipcc_pm_resume)
  292. };
  293. static struct platform_driver qcom_ipcc_driver = {
  294. .probe = qcom_ipcc_probe,
  295. .remove = qcom_ipcc_remove,
  296. .driver = {
  297. .name = "qcom-ipcc",
  298. .of_match_table = qcom_ipcc_of_match,
  299. .suppress_bind_attrs = true,
  300. .pm = pm_sleep_ptr(&qcom_ipcc_dev_pm_ops),
  301. },
  302. };
  303. static int __init qcom_ipcc_init(void)
  304. {
  305. return platform_driver_register(&qcom_ipcc_driver);
  306. }
  307. arch_initcall(qcom_ipcc_init);
  308. MODULE_AUTHOR("Venkata Narendra Kumar Gutta <vnkgutta@codeaurora.org>");
  309. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  310. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. IPCC driver");
  311. MODULE_LICENSE("GPL v2");