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.
84 * @irq: IPI agent interrupt ID
86 struct zynqmp_ipi_mbox {
87 struct zynqmp_ipi_pdata *pdata;
90 struct mbox_controller mbox;
91 struct zynqmp_ipi_mchan mchans[2];
95 * struct zynqmp_ipi_pdata - Description of z ZynqMP IPI agent platform data.
97 * @dev: device pointer corresponding to the Xilinx ZynqMP
99 * @irq: IPI agent interrupt ID
100 * @method: IPI SMC or HVC is going to be used
101 * @local_id: local IPI agent ID
102 * @num_mboxes: number of mailboxes of this IPI agent
103 * @ipi_mboxes: IPI mailboxes of this IPI agent
105 struct zynqmp_ipi_pdata {
111 struct zynqmp_ipi_mbox ipi_mboxes[];
114 static struct device_driver zynqmp_ipi_mbox_driver = {
115 .owner = THIS_MODULE,
116 .name = "zynqmp-ipi-mbox",
119 static void zynqmp_ipi_fw_call(struct zynqmp_ipi_mbox *ipi_mbox,
120 unsigned long a0, unsigned long a3,
121 struct arm_smccc_res *res)
123 struct zynqmp_ipi_pdata *pdata = ipi_mbox->pdata;
124 unsigned long a1, a2;
126 a1 = pdata->local_id;
127 a2 = ipi_mbox->remote_id;
128 if (pdata->method == USE_SMC)
129 arm_smccc_smc(a0, a1, a2, a3, 0, 0, 0, 0, res);
131 arm_smccc_hvc(a0, a1, a2, a3, 0, 0, 0, 0, res);
135 * zynqmp_ipi_interrupt - Interrupt handler for IPI notification
137 * @irq: Interrupt number
138 * @data: ZynqMP IPI mailbox platform data.
140 * Return: -EINVAL if there is no instance
141 * IRQ_NONE if the interrupt is not ours.
142 * IRQ_HANDLED if the rx interrupt was successfully handled.
144 static irqreturn_t zynqmp_ipi_interrupt(int irq, void *data)
146 struct zynqmp_ipi_pdata *pdata = data;
147 struct mbox_chan *chan;
148 struct zynqmp_ipi_mbox *ipi_mbox;
149 struct zynqmp_ipi_mchan *mchan;
150 struct zynqmp_ipi_message *msg;
152 struct arm_smccc_res res;
153 int ret, i, status = IRQ_NONE;
156 arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
157 arg3 = IPI_SMC_ENQUIRY_DIRQ_MASK;
158 for (i = 0; i < pdata->num_mboxes; i++) {
159 ipi_mbox = &pdata->ipi_mboxes[i];
160 mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
161 chan = &ipi_mbox->mbox.chans[IPI_MB_CHNL_RX];
162 zynqmp_ipi_fw_call(ipi_mbox, arg0, arg3, &res);
163 ret = (int)(res.a0 & 0xFFFFFFFF);
164 if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
165 if (mchan->is_opened) {
167 msg->len = mchan->req_buf_size;
168 memcpy_fromio(msg->data, mchan->req_buf,
170 mbox_chan_received_data(chan, (void *)msg);
171 status = IRQ_HANDLED;
179 * zynqmp_ipi_peek_data - Peek to see if there are any rx messages.
181 * @chan: Channel Pointer
183 * Return: 'true' if there is pending rx data, 'false' if there is none.
185 static bool zynqmp_ipi_peek_data(struct mbox_chan *chan)
187 struct device *dev = chan->mbox->dev;
188 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
189 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
192 struct arm_smccc_res res;
194 if (WARN_ON(!ipi_mbox)) {
195 dev_err(dev, "no platform drv data??\n");
199 arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
200 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
201 ret = (int)(res.a0 & 0xFFFFFFFF);
203 if (mchan->chan_type == IPI_MB_CHNL_TX) {
204 /* TX channel, check if the message has been acked
205 * by the remote, if yes, response is available.
207 if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
211 } else if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
212 /* RX channel, check if there is message arrived. */
219 * zynqmp_ipi_last_tx_done - See if the last tx message is sent
221 * @chan: Channel pointer
223 * Return: 'true' is no pending tx data, 'false' if there are any.
225 static bool zynqmp_ipi_last_tx_done(struct mbox_chan *chan)
227 struct device *dev = chan->mbox->dev;
228 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
229 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
232 struct arm_smccc_res res;
234 if (WARN_ON(!ipi_mbox)) {
235 dev_err(dev, "no platform drv data??\n");
239 if (mchan->chan_type == IPI_MB_CHNL_TX) {
240 /* We only need to check if the message been taken
241 * by the remote in the TX channel
243 arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
244 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
245 /* Check the SMC call status, a0 of the result */
246 ret = (int)(res.a0 & 0xFFFFFFFF);
247 if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
251 /* Always true for the response message in RX channel */
256 * zynqmp_ipi_send_data - Send data
258 * @chan: Channel Pointer
259 * @data: Message Pointer
261 * Return: 0 if all goes good, else appropriate error messages.
263 static int zynqmp_ipi_send_data(struct mbox_chan *chan, void *data)
265 struct device *dev = chan->mbox->dev;
266 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
267 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
268 struct zynqmp_ipi_message *msg = data;
270 struct arm_smccc_res res;
272 if (WARN_ON(!ipi_mbox)) {
273 dev_err(dev, "no platform drv data??\n");
277 if (mchan->chan_type == IPI_MB_CHNL_TX) {
278 /* Send request message */
279 if (msg && msg->len > mchan->req_buf_size) {
280 dev_err(dev, "channel %d message length %u > max %lu\n",
281 mchan->chan_type, (unsigned int)msg->len,
282 mchan->req_buf_size);
286 memcpy_toio(mchan->req_buf, msg->data, msg->len);
287 /* Kick IPI mailbox to send message */
288 arg0 = SMC_IPI_MAILBOX_NOTIFY;
289 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
291 /* Send response message */
292 if (msg && msg->len > mchan->resp_buf_size) {
293 dev_err(dev, "channel %d message length %u > max %lu\n",
294 mchan->chan_type, (unsigned int)msg->len,
295 mchan->resp_buf_size);
299 memcpy_toio(mchan->resp_buf, msg->data, msg->len);
300 arg0 = SMC_IPI_MAILBOX_ACK;
301 zynqmp_ipi_fw_call(ipi_mbox, arg0, IPI_SMC_ACK_EIRQ_MASK,
308 * zynqmp_ipi_startup - Startup the IPI channel
310 * @chan: Channel pointer
312 * Return: 0 if all goes good, else return corresponding error message
314 static int zynqmp_ipi_startup(struct mbox_chan *chan)
316 struct device *dev = chan->mbox->dev;
317 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
318 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
320 struct arm_smccc_res res;
322 unsigned int nchan_type;
324 if (mchan->is_opened)
327 /* If no channel has been opened, open the IPI mailbox */
328 nchan_type = (mchan->chan_type + 1) % 2;
329 if (!ipi_mbox->mchans[nchan_type].is_opened) {
330 arg0 = SMC_IPI_MAILBOX_OPEN;
331 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
332 /* Check the SMC call status, a0 of the result */
333 ret = (int)(res.a0 & 0xFFFFFFFF);
335 dev_err(dev, "SMC to open the IPI channel failed.\n");
341 /* If it is RX channel, enable the IPI notification interrupt */
342 if (mchan->chan_type == IPI_MB_CHNL_RX) {
343 arg0 = SMC_IPI_MAILBOX_ENABLE_IRQ;
344 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
346 mchan->is_opened = 1;
352 * zynqmp_ipi_shutdown - Shutdown the IPI channel
354 * @chan: Channel pointer
356 static void zynqmp_ipi_shutdown(struct mbox_chan *chan)
358 struct device *dev = chan->mbox->dev;
359 struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
360 struct zynqmp_ipi_mchan *mchan = chan->con_priv;
362 struct arm_smccc_res res;
363 unsigned int chan_type;
365 if (!mchan->is_opened)
368 /* If it is RX channel, disable notification interrupt */
369 chan_type = mchan->chan_type;
370 if (chan_type == IPI_MB_CHNL_RX) {
371 arg0 = SMC_IPI_MAILBOX_DISABLE_IRQ;
372 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
374 /* Release IPI mailbox if no other channel is opened */
375 chan_type = (chan_type + 1) % 2;
376 if (!ipi_mbox->mchans[chan_type].is_opened) {
377 arg0 = SMC_IPI_MAILBOX_RELEASE;
378 zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
381 mchan->is_opened = 0;
384 /* ZynqMP IPI mailbox operations */
385 static const struct mbox_chan_ops zynqmp_ipi_chan_ops = {
386 .startup = zynqmp_ipi_startup,
387 .shutdown = zynqmp_ipi_shutdown,
388 .peek_data = zynqmp_ipi_peek_data,
389 .last_tx_done = zynqmp_ipi_last_tx_done,
390 .send_data = zynqmp_ipi_send_data,
394 * zynqmp_ipi_of_xlate - Translate of phandle to IPI mailbox channel
396 * @mbox: mailbox controller pointer
397 * @p: phandle pointer
399 * Return: Mailbox channel, else return error pointer.
401 static struct mbox_chan *zynqmp_ipi_of_xlate(struct mbox_controller *mbox,
402 const struct of_phandle_args *p)
404 struct mbox_chan *chan;
405 struct device *dev = mbox->dev;
406 unsigned int chan_type;
408 /* Only supports TX and RX channels */
409 chan_type = p->args[0];
410 if (chan_type != IPI_MB_CHNL_TX && chan_type != IPI_MB_CHNL_RX) {
411 dev_err(dev, "req chnl failure: invalid chnl type %u.\n",
413 return ERR_PTR(-EINVAL);
415 chan = &mbox->chans[chan_type];
419 static const struct of_device_id zynqmp_ipi_of_match[] = {
420 { .compatible = "xlnx,zynqmp-ipi-mailbox" },
423 MODULE_DEVICE_TABLE(of, zynqmp_ipi_of_match);
426 * zynqmp_ipi_mbox_get_buf_res - Get buffer resource from the IPI dev node
428 * @node: IPI mbox device child node
429 * @name: name of the IPI buffer
430 * @res: pointer to where the resource information will be stored.
432 * Return: 0 for success, negative value for failure
434 static int zynqmp_ipi_mbox_get_buf_res(struct device_node *node,
436 struct resource *res)
440 index = of_property_match_string(node, "reg-names", name);
442 ret = of_address_to_resource(node, index, res);
451 * zynqmp_ipi_mbox_dev_release() - release the existence of a ipi mbox dev
453 * @dev: the ipi mailbox device
455 * This is to avoid the no device release() function kernel warning.
458 static void zynqmp_ipi_mbox_dev_release(struct device *dev)
464 * zynqmp_ipi_mbox_probe - probe IPI mailbox resource from device node
466 * @ipi_mbox: pointer to IPI mailbox private data structure
467 * @node: IPI mailbox device node
469 * Return: 0 for success, negative value for failure
471 static int zynqmp_ipi_mbox_probe(struct zynqmp_ipi_mbox *ipi_mbox,
472 struct device_node *node)
474 struct zynqmp_ipi_mchan *mchan;
475 struct mbox_chan *chans;
476 struct mbox_controller *mbox;
478 struct device *dev, *mdev;
482 dev = ipi_mbox->pdata->dev;
483 /* Initialize dev for IPI mailbox */
484 ipi_mbox->dev.parent = dev;
485 ipi_mbox->dev.release = NULL;
486 ipi_mbox->dev.of_node = node;
487 dev_set_name(&ipi_mbox->dev, "%s", of_node_full_name(node));
488 dev_set_drvdata(&ipi_mbox->dev, ipi_mbox);
489 ipi_mbox->dev.release = zynqmp_ipi_mbox_dev_release;
490 ipi_mbox->dev.driver = &zynqmp_ipi_mbox_driver;
491 ret = device_register(&ipi_mbox->dev);
493 dev_err(dev, "Failed to register ipi mbox dev.\n");
494 put_device(&ipi_mbox->dev);
497 mdev = &ipi_mbox->dev;
499 mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
500 name = "local_request_region";
501 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
503 mchan->req_buf_size = resource_size(&res);
504 mchan->req_buf = devm_ioremap(mdev, res.start,
505 mchan->req_buf_size);
506 if (!mchan->req_buf) {
507 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
510 } else if (ret != -ENODEV) {
511 dev_err(mdev, "Unmatched resource %s, %d.\n", name, ret);
515 name = "remote_response_region";
516 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
518 mchan->resp_buf_size = resource_size(&res);
519 mchan->resp_buf = devm_ioremap(mdev, res.start,
520 mchan->resp_buf_size);
521 if (!mchan->resp_buf) {
522 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
525 } else if (ret != -ENODEV) {
526 dev_err(mdev, "Unmatched resource %s.\n", name);
529 mchan->rx_buf = devm_kzalloc(mdev,
530 mchan->resp_buf_size +
531 sizeof(struct zynqmp_ipi_message),
536 mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
537 name = "remote_request_region";
538 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
540 mchan->req_buf_size = resource_size(&res);
541 mchan->req_buf = devm_ioremap(mdev, res.start,
542 mchan->req_buf_size);
543 if (!mchan->req_buf) {
544 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
547 } else if (ret != -ENODEV) {
548 dev_err(mdev, "Unmatched resource %s.\n", name);
552 name = "local_response_region";
553 ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
555 mchan->resp_buf_size = resource_size(&res);
556 mchan->resp_buf = devm_ioremap(mdev, res.start,
557 mchan->resp_buf_size);
558 if (!mchan->resp_buf) {
559 dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
562 } else if (ret != -ENODEV) {
563 dev_err(mdev, "Unmatched resource %s.\n", name);
566 mchan->rx_buf = devm_kzalloc(mdev,
567 mchan->resp_buf_size +
568 sizeof(struct zynqmp_ipi_message),
573 /* Get the IPI remote agent ID */
574 ret = of_property_read_u32(node, "xlnx,ipi-id", &ipi_mbox->remote_id);
576 dev_err(dev, "No IPI remote ID is specified.\n");
580 mbox = &ipi_mbox->mbox;
582 mbox->ops = &zynqmp_ipi_chan_ops;
584 mbox->txdone_irq = false;
585 mbox->txdone_poll = true;
586 mbox->txpoll_period = 5;
587 mbox->of_xlate = zynqmp_ipi_of_xlate;
588 chans = devm_kzalloc(mdev, 2 * sizeof(*chans), GFP_KERNEL);
592 chans[IPI_MB_CHNL_TX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
593 chans[IPI_MB_CHNL_RX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
594 ipi_mbox->mchans[IPI_MB_CHNL_TX].chan_type = IPI_MB_CHNL_TX;
595 ipi_mbox->mchans[IPI_MB_CHNL_RX].chan_type = IPI_MB_CHNL_RX;
596 ret = devm_mbox_controller_register(mdev, mbox);
599 "Failed to register mbox_controller(%d)\n", ret);
602 "Registered ZynqMP IPI mbox with TX/RX channels.\n");
607 * zynqmp_ipi_free_mboxes - Free IPI mailboxes devices
609 * @pdata: IPI private data
611 static void zynqmp_ipi_free_mboxes(struct zynqmp_ipi_pdata *pdata)
613 struct zynqmp_ipi_mbox *ipi_mbox;
616 i = pdata->num_mboxes;
617 for (; i >= 0; i--) {
618 ipi_mbox = &pdata->ipi_mboxes[i];
619 if (ipi_mbox->dev.parent) {
620 mbox_controller_unregister(&ipi_mbox->mbox);
621 if (device_is_registered(&ipi_mbox->dev))
622 device_unregister(&ipi_mbox->dev);
627 static int zynqmp_ipi_probe(struct platform_device *pdev)
629 struct device *dev = &pdev->dev;
630 struct device_node *nc, *np = pdev->dev.of_node;
631 struct zynqmp_ipi_pdata *pdata;
632 struct zynqmp_ipi_mbox *mbox;
633 int num_mboxes, ret = -EINVAL;
635 num_mboxes = of_get_available_child_count(np);
636 if (num_mboxes == 0) {
637 dev_err(dev, "mailbox nodes not available\n");
641 pdata = devm_kzalloc(dev, struct_size(pdata, ipi_mboxes, num_mboxes),
647 /* Get the IPI local agents ID */
648 ret = of_property_read_u32(np, "xlnx,ipi-id", &pdata->local_id);
650 dev_err(dev, "No IPI local ID is specified.\n");
654 pdata->num_mboxes = num_mboxes;
656 mbox = pdata->ipi_mboxes;
657 for_each_available_child_of_node(np, nc) {
659 ret = zynqmp_ipi_mbox_probe(mbox, nc);
662 dev_err(dev, "failed to probe subdev.\n");
670 ret = platform_get_irq(pdev, 0);
675 ret = devm_request_irq(dev, pdata->irq, zynqmp_ipi_interrupt,
676 IRQF_SHARED, dev_name(dev), pdata);
678 dev_err(dev, "IRQ %d is not requested successfully.\n",
683 platform_set_drvdata(pdev, pdata);
687 zynqmp_ipi_free_mboxes(pdata);
691 static int zynqmp_ipi_remove(struct platform_device *pdev)
693 struct zynqmp_ipi_pdata *pdata;
695 pdata = platform_get_drvdata(pdev);
696 zynqmp_ipi_free_mboxes(pdata);
701 static struct platform_driver zynqmp_ipi_driver = {
702 .probe = zynqmp_ipi_probe,
703 .remove = zynqmp_ipi_remove,
705 .name = "zynqmp-ipi",
706 .of_match_table = of_match_ptr(zynqmp_ipi_of_match),
710 static int __init zynqmp_ipi_init(void)
712 return platform_driver_register(&zynqmp_ipi_driver);
714 subsys_initcall(zynqmp_ipi_init);
716 static void __exit zynqmp_ipi_exit(void)
718 platform_driver_unregister(&zynqmp_ipi_driver);
720 module_exit(zynqmp_ipi_exit);
722 MODULE_LICENSE("GPL v2");
723 MODULE_DESCRIPTION("Xilinx ZynqMP IPI Mailbox driver");
724 MODULE_AUTHOR("Xilinx Inc.");