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