1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Inter Processor Interrupt(IPI) Mailbox Driver
5 * Copyright (C) 2018 Xilinx, Inc.
8 #include <linux/arm-smccc.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/mailbox_controller.h>
15 #include <linux/mailbox/zynqmp-ipi-message.h>
16 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
21 /* IPI agent ID any */
22 #define IPI_ID_ANY 0xFFUL
24 /* indicate if ZynqMP IPI mailbox driver uses SMC calls or HVC calls */
28 /* Default IPI SMC function IDs */
29 #define SMC_IPI_MAILBOX_OPEN 0x82001000U
30 #define SMC_IPI_MAILBOX_RELEASE 0x82001001U
31 #define SMC_IPI_MAILBOX_STATUS_ENQUIRY 0x82001002U
32 #define SMC_IPI_MAILBOX_NOTIFY 0x82001003U
33 #define SMC_IPI_MAILBOX_ACK 0x82001004U
34 #define SMC_IPI_MAILBOX_ENABLE_IRQ 0x82001005U
35 #define SMC_IPI_MAILBOX_DISABLE_IRQ 0x82001006U
38 #define IPI_SMC_ENQUIRY_DIRQ_MASK 0x00000001UL /* Flag to indicate if
39 * notification interrupt
42 #define IPI_SMC_ACK_EIRQ_MASK 0x00000001UL /* Flag to indicate if
43 * notification interrupt
47 /* IPI mailbox status */
48 #define IPI_MB_STATUS_IDLE 0
49 #define IPI_MB_STATUS_SEND_PENDING 1
50 #define IPI_MB_STATUS_RECV_PENDING 2
52 #define IPI_MB_CHNL_TX 0 /* IPI mailbox TX channel */
53 #define IPI_MB_CHNL_RX 1 /* IPI mailbox RX channel */
56 * struct zynqmp_ipi_mchan - Description of a Xilinx ZynqMP IPI mailbox channel
57 * @is_opened: indicate if the IPI channel is opened
58 * @req_buf: local to remote request buffer start address
59 * @resp_buf: local to remote response buffer start address
60 * @req_buf_size: request buffer size
61 * @resp_buf_size: response buffer size
62 * @rx_buf: receive buffer to pass received message to client
63 * @chan_type: channel type
65 struct zynqmp_ipi_mchan {
67 void __iomem *req_buf;
68 void __iomem *resp_buf;
72 unsigned int chan_type;
76 * struct zynqmp_ipi_mbox - Description of a ZynqMP IPI mailbox
78 * @pdata: pointer to the IPI private data
79 * @dev: device pointer corresponding to the Xilinx ZynqMP
81 * @remote_id: remote IPI agent ID
82 * @mbox: mailbox Controller
83 * @mchans: array for channels, tx channel and rx channel.
85 struct zynqmp_ipi_mbox {
86 struct zynqmp_ipi_pdata *pdata;
89 struct mbox_controller mbox;
90 struct zynqmp_ipi_mchan mchans[2];
94 * struct zynqmp_ipi_pdata - Description of z ZynqMP IPI agent platform data.
96 * @dev: device pointer corresponding to the Xilinx ZynqMP
98 * @irq: IPI agent interrupt ID
99 * @method: IPI SMC or HVC is going to be used
100 * @local_id: local IPI agent ID
101 * @num_mboxes: number of mailboxes of this IPI agent
102 * @ipi_mboxes: IPI mailboxes of this IPI agent
104 struct zynqmp_ipi_pdata {
110 struct zynqmp_ipi_mbox ipi_mboxes[] __counted_by(num_mboxes);
113 static struct device_driver zynqmp_ipi_mbox_driver = {
114 .owner = THIS_MODULE,
115 .name = "zynqmp-ipi-mbox",
118 static void zynqmp_ipi_fw_call(struct zynqmp_ipi_mbox *ipi_mbox,
119 unsigned long a0, unsigned long a3,
120 struct arm_smccc_res *res)
122 struct zynqmp_ipi_pdata *pdata = ipi_mbox->pdata;
123 unsigned long a1, a2;
125 a1 = pdata->local_id;
126 a2 = ipi_mbox->remote_id;
127 if (pdata->method == USE_SMC)
128 arm_smccc_smc(a0, a1, a2, a3, 0, 0, 0, 0, res);
130 arm_smccc_hvc(a0, a1, a2, a3, 0, 0, 0, 0, res);
134 * zynqmp_ipi_interrupt - Interrupt handler for IPI notification
136 * @irq: Interrupt number
137 * @data: ZynqMP IPI mailbox platform data.
139 * Return: -EINVAL if there is no instance
140 * IRQ_NONE if the interrupt is not ours.
141 * IRQ_HANDLED if the rx interrupt was successfully handled.
143 static irqreturn_t zynqmp_ipi_interrupt(int irq, void *data)
145 struct zynqmp_ipi_pdata *pdata = data;
146 struct mbox_chan *chan;
147 struct zynqmp_ipi_mbox *ipi_mbox;
148 struct zynqmp_ipi_mchan *mchan;
149 struct zynqmp_ipi_message *msg;
151 struct arm_smccc_res res;
152 int ret, i, status = IRQ_NONE;
155 arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
156 arg3 = IPI_SMC_ENQUIRY_DIRQ_MASK;
157 for (i = 0; i < pdata->num_mboxes; i++) {
158 ipi_mbox = &pdata->ipi_mboxes[i];
159 mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
160 chan = &ipi_mbox->mbox.chans[IPI_MB_CHNL_RX];
161 zynqmp_ipi_fw_call(ipi_mbox, arg0, arg3, &res);
162 ret = (int)(res.a0 & 0xFFFFFFFF);
163 if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
164 if (mchan->is_opened) {
166 msg->len = mchan->req_buf_size;
167 memcpy_fromio(msg->data, mchan->req_buf,
169 mbox_chan_received_data(chan, (void *)msg);
170 status = IRQ_HANDLED;
178 * zynqmp_ipi_peek_data - Peek to see if there are any rx messages.
180 * @chan: Channel Pointer
182 * Return: 'true' if there is pending rx data, 'false' if there is none.
184 static bool zynqmp_ipi_peek_data(struct mbox_chan *chan)
186 struct device *dev = chan->mbox->dev;
187 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
188 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
191 struct arm_smccc_res res;
193 if (WARN_ON(!ipi_mbox)) {
194 dev_err(dev, "no platform drv data??\n");
198 arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
199 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
200 ret = (int)(res.a0 & 0xFFFFFFFF);
202 if (mchan->chan_type == IPI_MB_CHNL_TX) {
203 /* TX channel, check if the message has been acked
204 * by the remote, if yes, response is available.
206 if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
210 } else if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
211 /* RX channel, check if there is message arrived. */
218 * zynqmp_ipi_last_tx_done - See if the last tx message is sent
220 * @chan: Channel pointer
222 * Return: 'true' is no pending tx data, 'false' if there are any.
224 static bool zynqmp_ipi_last_tx_done(struct mbox_chan *chan)
226 struct device *dev = chan->mbox->dev;
227 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
228 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
231 struct arm_smccc_res res;
233 if (WARN_ON(!ipi_mbox)) {
234 dev_err(dev, "no platform drv data??\n");
238 if (mchan->chan_type == IPI_MB_CHNL_TX) {
239 /* We only need to check if the message been taken
240 * by the remote in the TX channel
242 arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
243 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
244 /* Check the SMC call status, a0 of the result */
245 ret = (int)(res.a0 & 0xFFFFFFFF);
246 if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
250 /* Always true for the response message in RX channel */
255 * zynqmp_ipi_send_data - Send data
257 * @chan: Channel Pointer
258 * @data: Message Pointer
260 * Return: 0 if all goes good, else appropriate error messages.
262 static int zynqmp_ipi_send_data(struct mbox_chan *chan, void *data)
264 struct device *dev = chan->mbox->dev;
265 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
266 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
267 struct zynqmp_ipi_message *msg = data;
269 struct arm_smccc_res res;
271 if (WARN_ON(!ipi_mbox)) {
272 dev_err(dev, "no platform drv data??\n");
276 if (mchan->chan_type == IPI_MB_CHNL_TX) {
277 /* Send request message */
278 if (msg && msg->len > mchan->req_buf_size) {
279 dev_err(dev, "channel %d message length %u > max %lu\n",
280 mchan->chan_type, (unsigned int)msg->len,
281 mchan->req_buf_size);
285 memcpy_toio(mchan->req_buf, msg->data, msg->len);
286 /* Kick IPI mailbox to send message */
287 arg0 = SMC_IPI_MAILBOX_NOTIFY;
288 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
290 /* Send response message */
291 if (msg && msg->len > mchan->resp_buf_size) {
292 dev_err(dev, "channel %d message length %u > max %lu\n",
293 mchan->chan_type, (unsigned int)msg->len,
294 mchan->resp_buf_size);
298 memcpy_toio(mchan->resp_buf, msg->data, msg->len);
299 arg0 = SMC_IPI_MAILBOX_ACK;
300 zynqmp_ipi_fw_call(ipi_mbox, arg0, IPI_SMC_ACK_EIRQ_MASK,
307 * zynqmp_ipi_startup - Startup the IPI channel
309 * @chan: Channel pointer
311 * Return: 0 if all goes good, else return corresponding error message
313 static int zynqmp_ipi_startup(struct mbox_chan *chan)
315 struct device *dev = chan->mbox->dev;
316 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
317 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
319 struct arm_smccc_res res;
321 unsigned int nchan_type;
323 if (mchan->is_opened)
326 /* If no channel has been opened, open the IPI mailbox */
327 nchan_type = (mchan->chan_type + 1) % 2;
328 if (!ipi_mbox->mchans[nchan_type].is_opened) {
329 arg0 = SMC_IPI_MAILBOX_OPEN;
330 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
331 /* Check the SMC call status, a0 of the result */
332 ret = (int)(res.a0 & 0xFFFFFFFF);
334 dev_err(dev, "SMC to open the IPI channel failed.\n");
340 /* If it is RX channel, enable the IPI notification interrupt */
341 if (mchan->chan_type == IPI_MB_CHNL_RX) {
342 arg0 = SMC_IPI_MAILBOX_ENABLE_IRQ;
343 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
345 mchan->is_opened = 1;
351 * zynqmp_ipi_shutdown - Shutdown the IPI channel
353 * @chan: Channel pointer
355 static void zynqmp_ipi_shutdown(struct mbox_chan *chan)
357 struct device *dev = chan->mbox->dev;
358 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
359 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
361 struct arm_smccc_res res;
362 unsigned int chan_type;
364 if (!mchan->is_opened)
367 /* If it is RX channel, disable notification interrupt */
368 chan_type = mchan->chan_type;
369 if (chan_type == IPI_MB_CHNL_RX) {
370 arg0 = SMC_IPI_MAILBOX_DISABLE_IRQ;
371 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
373 /* Release IPI mailbox if no other channel is opened */
374 chan_type = (chan_type + 1) % 2;
375 if (!ipi_mbox->mchans[chan_type].is_opened) {
376 arg0 = SMC_IPI_MAILBOX_RELEASE;
377 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
380 mchan->is_opened = 0;
383 /* ZynqMP IPI mailbox operations */
384 static const struct mbox_chan_ops zynqmp_ipi_chan_ops = {
385 .startup = zynqmp_ipi_startup,
386 .shutdown = zynqmp_ipi_shutdown,
387 .peek_data = zynqmp_ipi_peek_data,
388 .last_tx_done = zynqmp_ipi_last_tx_done,
389 .send_data = zynqmp_ipi_send_data,
393 * zynqmp_ipi_of_xlate - Translate of phandle to IPI mailbox channel
395 * @mbox: mailbox controller pointer
396 * @p: phandle pointer
398 * Return: Mailbox channel, else return error pointer.
400 static struct mbox_chan *zynqmp_ipi_of_xlate(struct mbox_controller *mbox,
401 const struct of_phandle_args *p)
403 struct mbox_chan *chan;
404 struct device *dev = mbox->dev;
405 unsigned int chan_type;
407 /* Only supports TX and RX channels */
408 chan_type = p->args[0];
409 if (chan_type != IPI_MB_CHNL_TX && chan_type != IPI_MB_CHNL_RX) {
410 dev_err(dev, "req chnl failure: invalid chnl type %u.\n",
412 return ERR_PTR(-EINVAL);
414 chan = &mbox->chans[chan_type];
418 static const struct of_device_id zynqmp_ipi_of_match[] = {
419 { .compatible = "xlnx,zynqmp-ipi-mailbox" },
422 MODULE_DEVICE_TABLE(of, zynqmp_ipi_of_match);
425 * zynqmp_ipi_mbox_get_buf_res - Get buffer resource from the IPI dev node
427 * @node: IPI mbox device child node
428 * @name: name of the IPI buffer
429 * @res: pointer to where the resource information will be stored.
431 * Return: 0 for success, negative value for failure
433 static int zynqmp_ipi_mbox_get_buf_res(struct device_node *node,
435 struct resource *res)
439 index = of_property_match_string(node, "reg-names", name);
441 ret = of_address_to_resource(node, index, res);
450 * zynqmp_ipi_mbox_dev_release() - release the existence of a ipi mbox dev
452 * @dev: the ipi mailbox device
454 * This is to avoid the no device release() function kernel warning.
457 static void zynqmp_ipi_mbox_dev_release(struct device *dev)
463 * zynqmp_ipi_mbox_probe - probe IPI mailbox resource from device node
465 * @ipi_mbox: pointer to IPI mailbox private data structure
466 * @node: IPI mailbox device node
468 * Return: 0 for success, negative value for failure
470 static int zynqmp_ipi_mbox_probe(struct zynqmp_ipi_mbox *ipi_mbox,
471 struct device_node *node)
473 struct zynqmp_ipi_mchan *mchan;
474 struct mbox_chan *chans;
475 struct mbox_controller *mbox;
477 struct device *dev, *mdev;
481 dev = ipi_mbox->pdata->dev;
482 /* Initialize dev for IPI mailbox */
483 ipi_mbox->dev.parent = dev;
484 ipi_mbox->dev.release = NULL;
485 ipi_mbox->dev.of_node = node;
486 dev_set_name(&ipi_mbox->dev, "%s", of_node_full_name(node));
487 dev_set_drvdata(&ipi_mbox->dev, ipi_mbox);
488 ipi_mbox->dev.release = zynqmp_ipi_mbox_dev_release;
489 ipi_mbox->dev.driver = &zynqmp_ipi_mbox_driver;
490 ret = device_register(&ipi_mbox->dev);
492 dev_err(dev, "Failed to register ipi mbox dev.\n");
493 put_device(&ipi_mbox->dev);
496 mdev = &ipi_mbox->dev;
498 mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
499 name = "local_request_region";
500 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
502 mchan->req_buf_size = resource_size(&res);
503 mchan->req_buf = devm_ioremap(mdev, res.start,
504 mchan->req_buf_size);
505 if (!mchan->req_buf) {
506 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
509 } else if (ret != -ENODEV) {
510 dev_err(mdev, "Unmatched resource %s, %d.\n", name, ret);
514 name = "remote_response_region";
515 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
517 mchan->resp_buf_size = resource_size(&res);
518 mchan->resp_buf = devm_ioremap(mdev, res.start,
519 mchan->resp_buf_size);
520 if (!mchan->resp_buf) {
521 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
524 } else if (ret != -ENODEV) {
525 dev_err(mdev, "Unmatched resource %s.\n", name);
528 mchan->rx_buf = devm_kzalloc(mdev,
529 mchan->resp_buf_size +
530 sizeof(struct zynqmp_ipi_message),
535 mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
536 name = "remote_request_region";
537 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
539 mchan->req_buf_size = resource_size(&res);
540 mchan->req_buf = devm_ioremap(mdev, res.start,
541 mchan->req_buf_size);
542 if (!mchan->req_buf) {
543 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
546 } else if (ret != -ENODEV) {
547 dev_err(mdev, "Unmatched resource %s.\n", name);
551 name = "local_response_region";
552 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
554 mchan->resp_buf_size = resource_size(&res);
555 mchan->resp_buf = devm_ioremap(mdev, res.start,
556 mchan->resp_buf_size);
557 if (!mchan->resp_buf) {
558 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
561 } else if (ret != -ENODEV) {
562 dev_err(mdev, "Unmatched resource %s.\n", name);
565 mchan->rx_buf = devm_kzalloc(mdev,
566 mchan->resp_buf_size +
567 sizeof(struct zynqmp_ipi_message),
572 /* Get the IPI remote agent ID */
573 ret = of_property_read_u32(node, "xlnx,ipi-id", &ipi_mbox->remote_id);
575 dev_err(dev, "No IPI remote ID is specified.\n");
579 mbox = &ipi_mbox->mbox;
581 mbox->ops = &zynqmp_ipi_chan_ops;
583 mbox->txdone_irq = false;
584 mbox->txdone_poll = true;
585 mbox->txpoll_period = 5;
586 mbox->of_xlate = zynqmp_ipi_of_xlate;
587 chans = devm_kzalloc(mdev, 2 * sizeof(*chans), GFP_KERNEL);
591 chans[IPI_MB_CHNL_TX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
592 chans[IPI_MB_CHNL_RX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
593 ipi_mbox->mchans[IPI_MB_CHNL_TX].chan_type = IPI_MB_CHNL_TX;
594 ipi_mbox->mchans[IPI_MB_CHNL_RX].chan_type = IPI_MB_CHNL_RX;
595 ret = devm_mbox_controller_register(mdev, mbox);
598 "Failed to register mbox_controller(%d)\n", ret);
601 "Registered ZynqMP IPI mbox with TX/RX channels.\n");
606 * zynqmp_ipi_free_mboxes - Free IPI mailboxes devices
608 * @pdata: IPI private data
610 static void zynqmp_ipi_free_mboxes(struct zynqmp_ipi_pdata *pdata)
612 struct zynqmp_ipi_mbox *ipi_mbox;
615 i = pdata->num_mboxes;
616 for (; i >= 0; i--) {
617 ipi_mbox = &pdata->ipi_mboxes[i];
618 if (ipi_mbox->dev.parent) {
619 mbox_controller_unregister(&ipi_mbox->mbox);
620 if (device_is_registered(&ipi_mbox->dev))
621 device_unregister(&ipi_mbox->dev);
626 static int zynqmp_ipi_probe(struct platform_device *pdev)
628 struct device *dev = &pdev->dev;
629 struct device_node *nc, *np = pdev->dev.of_node;
630 struct zynqmp_ipi_pdata *pdata;
631 struct zynqmp_ipi_mbox *mbox;
632 int num_mboxes, ret = -EINVAL;
634 num_mboxes = of_get_available_child_count(np);
635 if (num_mboxes == 0) {
636 dev_err(dev, "mailbox nodes not available\n");
640 pdata = devm_kzalloc(dev, struct_size(pdata, ipi_mboxes, num_mboxes),
646 /* Get the IPI local agents ID */
647 ret = of_property_read_u32(np, "xlnx,ipi-id", &pdata->local_id);
649 dev_err(dev, "No IPI local ID is specified.\n");
653 pdata->num_mboxes = num_mboxes;
655 mbox = pdata->ipi_mboxes;
656 for_each_available_child_of_node(np, nc) {
658 ret = zynqmp_ipi_mbox_probe(mbox, nc);
661 dev_err(dev, "failed to probe subdev.\n");
669 ret = platform_get_irq(pdev, 0);
674 ret = devm_request_irq(dev, pdata->irq, zynqmp_ipi_interrupt,
675 IRQF_SHARED, dev_name(dev), pdata);
677 dev_err(dev, "IRQ %d is not requested successfully.\n",
682 platform_set_drvdata(pdev, pdata);
686 zynqmp_ipi_free_mboxes(pdata);
690 static void zynqmp_ipi_remove(struct platform_device *pdev)
692 struct zynqmp_ipi_pdata *pdata;
694 pdata = platform_get_drvdata(pdev);
695 zynqmp_ipi_free_mboxes(pdata);
698 static struct platform_driver zynqmp_ipi_driver = {
699 .probe = zynqmp_ipi_probe,
700 .remove_new = zynqmp_ipi_remove,
702 .name = "zynqmp-ipi",
703 .of_match_table = of_match_ptr(zynqmp_ipi_of_match),
707 static int __init zynqmp_ipi_init(void)
709 return platform_driver_register(&zynqmp_ipi_driver);
711 subsys_initcall(zynqmp_ipi_init);
713 static void __exit zynqmp_ipi_exit(void)
715 platform_driver_unregister(&zynqmp_ipi_driver);
717 module_exit(zynqmp_ipi_exit);
719 MODULE_LICENSE("GPL v2");
720 MODULE_DESCRIPTION("Xilinx ZynqMP IPI Mailbox driver");
721 MODULE_AUTHOR("Xilinx Inc.");