xref: /linux/drivers/mailbox/zynqmp-ipi-mailbox.c (revision 62b5bf58b928f0f4fcc8bb633b63795517825d31)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Inter Processor Interrupt(IPI) Mailbox Driver
4  *
5  * Copyright (C) 2018 Xilinx, Inc.
6  */
7 
8 #include <linux/arm-smccc.h>
9 #include <linux/cpuhotplug.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/interrupt.h>
13 #include <linux/irqdomain.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/mailbox_controller.h>
17 #include <linux/mailbox/zynqmp-ipi-message.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 
24 /* IPI agent ID any */
25 #define IPI_ID_ANY 0xFFUL
26 
27 /* indicate if ZynqMP IPI mailbox driver uses SMC calls or HVC calls */
28 #define USE_SMC 0
29 #define USE_HVC 1
30 
31 /* Default IPI SMC function IDs */
32 #define SMC_IPI_MAILBOX_OPEN		0x82001000U
33 #define SMC_IPI_MAILBOX_RELEASE		0x82001001U
34 #define SMC_IPI_MAILBOX_STATUS_ENQUIRY	0x82001002U
35 #define SMC_IPI_MAILBOX_NOTIFY		0x82001003U
36 #define SMC_IPI_MAILBOX_ACK		0x82001004U
37 #define SMC_IPI_MAILBOX_ENABLE_IRQ	0x82001005U
38 #define SMC_IPI_MAILBOX_DISABLE_IRQ	0x82001006U
39 
40 /* IPI SMC Macros */
41 #define IPI_SMC_ENQUIRY_DIRQ_MASK	0x00000001UL /* Flag to indicate if
42 						      * notification interrupt
43 						      * to be disabled.
44 						      */
45 #define IPI_SMC_ACK_EIRQ_MASK		0x00000001UL /* Flag to indicate if
46 						      * notification interrupt
47 						      * to be enabled.
48 						      */
49 
50 /* IPI mailbox status */
51 #define IPI_MB_STATUS_IDLE		0
52 #define IPI_MB_STATUS_SEND_PENDING	1
53 #define IPI_MB_STATUS_RECV_PENDING	2
54 
55 #define IPI_MB_CHNL_TX	0 /* IPI mailbox TX channel */
56 #define IPI_MB_CHNL_RX	1 /* IPI mailbox RX channel */
57 
58 /* IPI Message Buffer Information */
59 #define RESP_OFFSET	0x20U
60 #define DEST_OFFSET	0x40U
61 #define IPI_BUF_SIZE	0x20U
62 #define DST_BIT_POS	9U
63 #define SRC_BITMASK	GENMASK(11, 8)
64 
65 #define MAX_SGI 16
66 
67 /**
68  * struct zynqmp_ipi_mchan - Description of a Xilinx ZynqMP IPI mailbox channel
69  * @is_opened: indicate if the IPI channel is opened
70  * @req_buf: local to remote request buffer start address
71  * @resp_buf: local to remote response buffer start address
72  * @req_buf_size: request buffer size
73  * @resp_buf_size: response buffer size
74  * @rx_buf: receive buffer to pass received message to client
75  * @chan_type: channel type
76  */
77 struct zynqmp_ipi_mchan {
78 	int is_opened;
79 	void __iomem *req_buf;
80 	void __iomem *resp_buf;
81 	void *rx_buf;
82 	size_t req_buf_size;
83 	size_t resp_buf_size;
84 	unsigned int chan_type;
85 };
86 
87 struct zynqmp_ipi_mbox;
88 
89 typedef int (*setup_ipi_fn)(struct zynqmp_ipi_mbox *ipi_mbox, struct device_node *node);
90 
91 /**
92  * struct zynqmp_ipi_mbox - Description of a ZynqMP IPI mailbox
93  *                          platform data.
94  * @pdata:		  pointer to the IPI private data
95  * @dev:                  device pointer corresponding to the Xilinx ZynqMP
96  *                        IPI mailbox
97  * @remote_id:            remote IPI agent ID
98  * @mbox:                 mailbox Controller
99  * @mchans:               array for channels, tx channel and rx channel.
100  * @setup_ipi_fn:         Function Pointer to set up IPI Channels
101  */
102 struct zynqmp_ipi_mbox {
103 	struct zynqmp_ipi_pdata *pdata;
104 	struct device dev;
105 	u32 remote_id;
106 	struct mbox_controller mbox;
107 	struct zynqmp_ipi_mchan mchans[2];
108 	setup_ipi_fn setup_ipi_fn;
109 };
110 
111 /**
112  * struct zynqmp_ipi_pdata - Description of z ZynqMP IPI agent platform data.
113  *
114  * @dev:                  device pointer corresponding to the Xilinx ZynqMP
115  *                        IPI agent
116  * @irq:                  IPI agent interrupt ID
117  * @method:               IPI SMC or HVC is going to be used
118  * @local_id:             local IPI agent ID
119  * @virq_sgi:             IRQ number mapped to SGI
120  * @num_mboxes:           number of mailboxes of this IPI agent
121  * @ipi_mboxes:           IPI mailboxes of this IPI agent
122  */
123 struct zynqmp_ipi_pdata {
124 	struct device *dev;
125 	int irq;
126 	unsigned int method;
127 	u32 local_id;
128 	int virq_sgi;
129 	int num_mboxes;
130 	struct zynqmp_ipi_mbox ipi_mboxes[] __counted_by(num_mboxes);
131 };
132 
133 static DEFINE_PER_CPU(struct zynqmp_ipi_pdata *, per_cpu_pdata);
134 
135 static struct device_driver zynqmp_ipi_mbox_driver = {
136 	.owner = THIS_MODULE,
137 	.name = "zynqmp-ipi-mbox",
138 };
139 
140 static void zynqmp_ipi_fw_call(struct zynqmp_ipi_mbox *ipi_mbox,
141 			       unsigned long a0, unsigned long a3,
142 			       struct arm_smccc_res *res)
143 {
144 	struct zynqmp_ipi_pdata *pdata = ipi_mbox->pdata;
145 	unsigned long a1, a2;
146 
147 	a1 = pdata->local_id;
148 	a2 = ipi_mbox->remote_id;
149 	if (pdata->method == USE_SMC)
150 		arm_smccc_smc(a0, a1, a2, a3, 0, 0, 0, 0, res);
151 	else
152 		arm_smccc_hvc(a0, a1, a2, a3, 0, 0, 0, 0, res);
153 }
154 
155 /**
156  * zynqmp_ipi_interrupt - Interrupt handler for IPI notification
157  *
158  * @irq:  Interrupt number
159  * @data: ZynqMP IPI mailbox platform data.
160  *
161  * Return: -EINVAL if there is no instance
162  * IRQ_NONE if the interrupt is not ours.
163  * IRQ_HANDLED if the rx interrupt was successfully handled.
164  */
165 static irqreturn_t zynqmp_ipi_interrupt(int irq, void *data)
166 {
167 	struct zynqmp_ipi_pdata *pdata = data;
168 	struct mbox_chan *chan;
169 	struct zynqmp_ipi_mbox *ipi_mbox;
170 	struct zynqmp_ipi_mchan *mchan;
171 	struct zynqmp_ipi_message *msg;
172 	u64 arg0, arg3;
173 	struct arm_smccc_res res;
174 	int ret, i, status = IRQ_NONE;
175 
176 	(void)irq;
177 	arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
178 	arg3 = IPI_SMC_ENQUIRY_DIRQ_MASK;
179 	for (i = 0; i < pdata->num_mboxes; i++) {
180 		ipi_mbox = &pdata->ipi_mboxes[i];
181 		mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
182 		chan = &ipi_mbox->mbox.chans[IPI_MB_CHNL_RX];
183 		zynqmp_ipi_fw_call(ipi_mbox, arg0, arg3, &res);
184 		ret = (int)(res.a0 & 0xFFFFFFFF);
185 		if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
186 			if (mchan->is_opened) {
187 				msg = mchan->rx_buf;
188 				if (msg) {
189 					msg->len = mchan->req_buf_size;
190 					memcpy_fromio(msg->data, mchan->req_buf,
191 						      msg->len);
192 				}
193 				mbox_chan_received_data(chan, (void *)msg);
194 				status = IRQ_HANDLED;
195 			}
196 		}
197 	}
198 	return status;
199 }
200 
201 static irqreturn_t zynqmp_sgi_interrupt(int irq, void *data)
202 {
203 	struct zynqmp_ipi_pdata **pdata_ptr = data;
204 	struct zynqmp_ipi_pdata *pdata = *pdata_ptr;
205 
206 	return zynqmp_ipi_interrupt(irq, pdata);
207 }
208 
209 /**
210  * zynqmp_ipi_peek_data - Peek to see if there are any rx messages.
211  *
212  * @chan: Channel Pointer
213  *
214  * Return: 'true' if there is pending rx data, 'false' if there is none.
215  */
216 static bool zynqmp_ipi_peek_data(struct mbox_chan *chan)
217 {
218 	struct device *dev = chan->mbox->dev;
219 	struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
220 	struct zynqmp_ipi_mchan *mchan = chan->con_priv;
221 	int ret;
222 	u64 arg0;
223 	struct arm_smccc_res res;
224 
225 	if (WARN_ON(!ipi_mbox)) {
226 		dev_err(dev, "no platform drv data??\n");
227 		return false;
228 	}
229 
230 	arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
231 	zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
232 	ret = (int)(res.a0 & 0xFFFFFFFF);
233 
234 	if (mchan->chan_type == IPI_MB_CHNL_TX) {
235 		/* TX channel, check if the message has been acked
236 		 * by the remote, if yes, response is available.
237 		 */
238 		if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
239 			return false;
240 		else
241 			return true;
242 	} else if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) {
243 		/* RX channel, check if there is message arrived. */
244 		return true;
245 	}
246 	return false;
247 }
248 
249 /**
250  * zynqmp_ipi_last_tx_done - See if the last tx message is sent
251  *
252  * @chan: Channel pointer
253  *
254  * Return: 'true' is no pending tx data, 'false' if there are any.
255  */
256 static bool zynqmp_ipi_last_tx_done(struct mbox_chan *chan)
257 {
258 	struct device *dev = chan->mbox->dev;
259 	struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
260 	struct zynqmp_ipi_mchan *mchan = chan->con_priv;
261 	int ret;
262 	u64 arg0;
263 	struct arm_smccc_res res;
264 
265 	if (WARN_ON(!ipi_mbox)) {
266 		dev_err(dev, "no platform drv data??\n");
267 		return false;
268 	}
269 
270 	if (mchan->chan_type == IPI_MB_CHNL_TX) {
271 		/* We only need to check if the message been taken
272 		 * by the remote in the TX channel
273 		 */
274 		arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY;
275 		zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
276 		/* Check the SMC call status, a0 of the result */
277 		ret = (int)(res.a0 & 0xFFFFFFFF);
278 		if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING)
279 			return false;
280 		return true;
281 	}
282 	/* Always true for the response message in RX channel */
283 	return true;
284 }
285 
286 /**
287  * zynqmp_ipi_send_data - Send data
288  *
289  * @chan: Channel Pointer
290  * @data: Message Pointer
291  *
292  * Return: 0 if all goes good, else appropriate error messages.
293  */
294 static int zynqmp_ipi_send_data(struct mbox_chan *chan, void *data)
295 {
296 	struct device *dev = chan->mbox->dev;
297 	struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
298 	struct zynqmp_ipi_mchan *mchan = chan->con_priv;
299 	struct zynqmp_ipi_message *msg = data;
300 	u64 arg0;
301 	struct arm_smccc_res res;
302 
303 	if (WARN_ON(!ipi_mbox)) {
304 		dev_err(dev, "no platform drv data??\n");
305 		return -EINVAL;
306 	}
307 
308 	if (mchan->chan_type == IPI_MB_CHNL_TX) {
309 		/* Send request message */
310 		if (msg && msg->len > mchan->req_buf_size && mchan->req_buf) {
311 			dev_err(dev, "channel %d message length %u > max %lu\n",
312 				mchan->chan_type, (unsigned int)msg->len,
313 				mchan->req_buf_size);
314 			return -EINVAL;
315 		}
316 		if (msg && msg->len && mchan->req_buf)
317 			memcpy_toio(mchan->req_buf, msg->data, msg->len);
318 		/* Kick IPI mailbox to send message */
319 		arg0 = SMC_IPI_MAILBOX_NOTIFY;
320 		zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
321 	} else {
322 		/* Send response message */
323 		if (msg && msg->len > mchan->resp_buf_size && mchan->resp_buf) {
324 			dev_err(dev, "channel %d message length %u > max %lu\n",
325 				mchan->chan_type, (unsigned int)msg->len,
326 				mchan->resp_buf_size);
327 			return -EINVAL;
328 		}
329 		if (msg && msg->len && mchan->resp_buf)
330 			memcpy_toio(mchan->resp_buf, msg->data, msg->len);
331 		arg0 = SMC_IPI_MAILBOX_ACK;
332 		zynqmp_ipi_fw_call(ipi_mbox, arg0, IPI_SMC_ACK_EIRQ_MASK,
333 				   &res);
334 	}
335 	return 0;
336 }
337 
338 /**
339  * zynqmp_ipi_startup - Startup the IPI channel
340  *
341  * @chan: Channel pointer
342  *
343  * Return: 0 if all goes good, else return corresponding error message
344  */
345 static int zynqmp_ipi_startup(struct mbox_chan *chan)
346 {
347 	struct device *dev = chan->mbox->dev;
348 	struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
349 	struct zynqmp_ipi_mchan *mchan = chan->con_priv;
350 	u64 arg0;
351 	struct arm_smccc_res res;
352 	int ret = 0;
353 	unsigned int nchan_type;
354 
355 	if (mchan->is_opened)
356 		return 0;
357 
358 	/* If no channel has been opened, open the IPI mailbox */
359 	nchan_type = (mchan->chan_type + 1) % 2;
360 	if (!ipi_mbox->mchans[nchan_type].is_opened) {
361 		arg0 = SMC_IPI_MAILBOX_OPEN;
362 		zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
363 		/* Check the SMC call status, a0 of the result */
364 		ret = (int)(res.a0 & 0xFFFFFFFF);
365 		if (ret < 0) {
366 			dev_err(dev, "SMC to open the IPI channel failed.\n");
367 			return ret;
368 		}
369 		ret = 0;
370 	}
371 
372 	/* If it is RX channel, enable the IPI notification interrupt */
373 	if (mchan->chan_type == IPI_MB_CHNL_RX) {
374 		arg0 = SMC_IPI_MAILBOX_ENABLE_IRQ;
375 		zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
376 	}
377 	mchan->is_opened = 1;
378 
379 	return ret;
380 }
381 
382 /**
383  * zynqmp_ipi_shutdown - Shutdown the IPI channel
384  *
385  * @chan: Channel pointer
386  */
387 static void zynqmp_ipi_shutdown(struct mbox_chan *chan)
388 {
389 	struct device *dev = chan->mbox->dev;
390 	struct zynqmp_ipi_mbox *ipi_mbox = dev_get_drvdata(dev);
391 	struct zynqmp_ipi_mchan *mchan = chan->con_priv;
392 	u64 arg0;
393 	struct arm_smccc_res res;
394 	unsigned int chan_type;
395 
396 	if (!mchan->is_opened)
397 		return;
398 
399 	/* If it is RX channel, disable notification interrupt */
400 	chan_type = mchan->chan_type;
401 	if (chan_type == IPI_MB_CHNL_RX) {
402 		arg0 = SMC_IPI_MAILBOX_DISABLE_IRQ;
403 		zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
404 	}
405 	/* Release IPI mailbox if no other channel is opened */
406 	chan_type = (chan_type + 1) % 2;
407 	if (!ipi_mbox->mchans[chan_type].is_opened) {
408 		arg0 = SMC_IPI_MAILBOX_RELEASE;
409 		zynqmp_ipi_fw_call(ipi_mbox, arg0, 0, &res);
410 	}
411 
412 	mchan->is_opened = 0;
413 }
414 
415 /* ZynqMP IPI mailbox operations */
416 static const struct mbox_chan_ops zynqmp_ipi_chan_ops = {
417 	.startup = zynqmp_ipi_startup,
418 	.shutdown = zynqmp_ipi_shutdown,
419 	.peek_data = zynqmp_ipi_peek_data,
420 	.last_tx_done = zynqmp_ipi_last_tx_done,
421 	.send_data = zynqmp_ipi_send_data,
422 };
423 
424 /**
425  * zynqmp_ipi_of_xlate - Translate of phandle to IPI mailbox channel
426  *
427  * @mbox: mailbox controller pointer
428  * @p:    phandle pointer
429  *
430  * Return: Mailbox channel, else return error pointer.
431  */
432 static struct mbox_chan *zynqmp_ipi_of_xlate(struct mbox_controller *mbox,
433 					     const struct of_phandle_args *p)
434 {
435 	struct mbox_chan *chan;
436 	struct device *dev = mbox->dev;
437 	unsigned int chan_type;
438 
439 	/* Only supports TX and RX channels */
440 	chan_type = p->args[0];
441 	if (chan_type != IPI_MB_CHNL_TX && chan_type != IPI_MB_CHNL_RX) {
442 		dev_err(dev, "req chnl failure: invalid chnl type %u.\n",
443 			chan_type);
444 		return ERR_PTR(-EINVAL);
445 	}
446 	chan = &mbox->chans[chan_type];
447 	return chan;
448 }
449 
450 /**
451  * zynqmp_ipi_mbox_get_buf_res - Get buffer resource from the IPI dev node
452  *
453  * @node: IPI mbox device child node
454  * @name: name of the IPI buffer
455  * @res: pointer to where the resource information will be stored.
456  *
457  * Return: 0 for success, negative value for failure
458  */
459 static int zynqmp_ipi_mbox_get_buf_res(struct device_node *node,
460 				       const char *name,
461 				       struct resource *res)
462 {
463 	int ret, index;
464 
465 	index = of_property_match_string(node, "reg-names", name);
466 	if (index >= 0) {
467 		ret = of_address_to_resource(node, index, res);
468 		if (ret < 0)
469 			return -EINVAL;
470 		return 0;
471 	}
472 	return -ENODEV;
473 }
474 
475 /**
476  * zynqmp_ipi_mbox_dev_release() - release the existence of a ipi mbox dev
477  *
478  * @dev: the ipi mailbox device
479  *
480  * This is to avoid the no device release() function kernel warning.
481  *
482  */
483 static void zynqmp_ipi_mbox_dev_release(struct device *dev)
484 {
485 	(void)dev;
486 }
487 
488 /**
489  * zynqmp_ipi_mbox_probe - probe IPI mailbox resource from device node
490  *
491  * @ipi_mbox: pointer to IPI mailbox private data structure
492  * @node: IPI mailbox device node
493  *
494  * Return: 0 for success, negative value for failure
495  */
496 static int zynqmp_ipi_mbox_probe(struct zynqmp_ipi_mbox *ipi_mbox,
497 				 struct device_node *node)
498 {
499 	struct mbox_chan *chans;
500 	struct mbox_controller *mbox;
501 	struct device *dev, *mdev;
502 	int ret;
503 
504 	dev = ipi_mbox->pdata->dev;
505 	/* Initialize dev for IPI mailbox */
506 	ipi_mbox->dev.parent = dev;
507 	ipi_mbox->dev.release = NULL;
508 	ipi_mbox->dev.of_node = node;
509 	dev_set_name(&ipi_mbox->dev, "%s", of_node_full_name(node));
510 	dev_set_drvdata(&ipi_mbox->dev, ipi_mbox);
511 	ipi_mbox->dev.release = zynqmp_ipi_mbox_dev_release;
512 	ipi_mbox->dev.driver = &zynqmp_ipi_mbox_driver;
513 	ret = device_register(&ipi_mbox->dev);
514 	if (ret) {
515 		dev_err(dev, "Failed to register ipi mbox dev.\n");
516 		put_device(&ipi_mbox->dev);
517 		return ret;
518 	}
519 	mdev = &ipi_mbox->dev;
520 
521 	/* Get the IPI remote agent ID */
522 	ret = of_property_read_u32(node, "xlnx,ipi-id", &ipi_mbox->remote_id);
523 	if (ret < 0) {
524 		dev_err(dev, "No IPI remote ID is specified.\n");
525 		return ret;
526 	}
527 
528 	ret = ipi_mbox->setup_ipi_fn(ipi_mbox, node);
529 	if (ret) {
530 		dev_err(dev, "Failed to set up IPI Buffers.\n");
531 		return ret;
532 	}
533 
534 	mbox = &ipi_mbox->mbox;
535 	mbox->dev = mdev;
536 	mbox->ops = &zynqmp_ipi_chan_ops;
537 	mbox->num_chans = 2;
538 	mbox->txdone_irq = false;
539 	mbox->txdone_poll = true;
540 	mbox->txpoll_period = 5;
541 	mbox->of_xlate = zynqmp_ipi_of_xlate;
542 	chans = devm_kzalloc(mdev, 2 * sizeof(*chans), GFP_KERNEL);
543 	if (!chans)
544 		return -ENOMEM;
545 	mbox->chans = chans;
546 	chans[IPI_MB_CHNL_TX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
547 	chans[IPI_MB_CHNL_RX].con_priv = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
548 	ipi_mbox->mchans[IPI_MB_CHNL_TX].chan_type = IPI_MB_CHNL_TX;
549 	ipi_mbox->mchans[IPI_MB_CHNL_RX].chan_type = IPI_MB_CHNL_RX;
550 	ret = devm_mbox_controller_register(mdev, mbox);
551 	if (ret)
552 		dev_err(mdev,
553 			"Failed to register mbox_controller(%d)\n", ret);
554 	else
555 		dev_info(mdev,
556 			 "Registered ZynqMP IPI mbox with TX/RX channels.\n");
557 	return ret;
558 }
559 
560 /**
561  * zynqmp_ipi_setup - set up IPI Buffers for classic flow
562  *
563  * @ipi_mbox: pointer to IPI mailbox private data structure
564  * @node: IPI mailbox device node
565  *
566  * This will be used to set up IPI Buffers for ZynqMP SOC if user
567  * wishes to use classic driver usage model on new SOC's with only
568  * buffered IPIs.
569  *
570  * Note that bufferless IPIs and mixed usage of buffered and bufferless
571  * IPIs are not supported with this flow.
572  *
573  * This will be invoked with compatible string "xlnx,zynqmp-ipi-mailbox".
574  *
575  * Return: 0 for success, negative value for failure
576  */
577 static int zynqmp_ipi_setup(struct zynqmp_ipi_mbox *ipi_mbox,
578 			    struct device_node *node)
579 {
580 	struct zynqmp_ipi_mchan *mchan;
581 	struct device *mdev;
582 	struct resource res;
583 	const char *name;
584 	int ret;
585 
586 	mdev = &ipi_mbox->dev;
587 
588 	mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
589 	name = "local_request_region";
590 	ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
591 	if (!ret) {
592 		mchan->req_buf_size = resource_size(&res);
593 		mchan->req_buf = devm_ioremap(mdev, res.start,
594 					      mchan->req_buf_size);
595 		if (!mchan->req_buf) {
596 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
597 			return -ENOMEM;
598 		}
599 	} else if (ret != -ENODEV) {
600 		dev_err(mdev, "Unmatched resource %s, %d.\n", name, ret);
601 		return ret;
602 	}
603 
604 	name = "remote_response_region";
605 	ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
606 	if (!ret) {
607 		mchan->resp_buf_size = resource_size(&res);
608 		mchan->resp_buf = devm_ioremap(mdev, res.start,
609 					       mchan->resp_buf_size);
610 		if (!mchan->resp_buf) {
611 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
612 			return -ENOMEM;
613 		}
614 	} else if (ret != -ENODEV) {
615 		dev_err(mdev, "Unmatched resource %s.\n", name);
616 		return ret;
617 	}
618 	mchan->rx_buf = devm_kzalloc(mdev,
619 				     mchan->resp_buf_size +
620 				     sizeof(struct zynqmp_ipi_message),
621 				     GFP_KERNEL);
622 	if (!mchan->rx_buf)
623 		return -ENOMEM;
624 
625 	mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
626 	name = "remote_request_region";
627 	ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
628 	if (!ret) {
629 		mchan->req_buf_size = resource_size(&res);
630 		mchan->req_buf = devm_ioremap(mdev, res.start,
631 					      mchan->req_buf_size);
632 		if (!mchan->req_buf) {
633 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
634 			return -ENOMEM;
635 		}
636 	} else if (ret != -ENODEV) {
637 		dev_err(mdev, "Unmatched resource %s.\n", name);
638 		return ret;
639 	}
640 
641 	name = "local_response_region";
642 	ret = zynqmp_ipi_mbox_get_buf_res(node, name, &res);
643 	if (!ret) {
644 		mchan->resp_buf_size = resource_size(&res);
645 		mchan->resp_buf = devm_ioremap(mdev, res.start,
646 					       mchan->resp_buf_size);
647 		if (!mchan->resp_buf) {
648 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
649 			return -ENOMEM;
650 		}
651 	} else if (ret != -ENODEV) {
652 		dev_err(mdev, "Unmatched resource %s.\n", name);
653 		return ret;
654 	}
655 	mchan->rx_buf = devm_kzalloc(mdev,
656 				     mchan->resp_buf_size +
657 				     sizeof(struct zynqmp_ipi_message),
658 				     GFP_KERNEL);
659 	if (!mchan->rx_buf)
660 		return -ENOMEM;
661 
662 	return 0;
663 }
664 
665 /**
666  * versal_ipi_setup - Set up IPIs to support mixed usage of
667  *				 Buffered and Bufferless IPIs.
668  *
669  * @ipi_mbox: pointer to IPI mailbox private data structure
670  * @node: IPI mailbox device node
671  *
672  * Return: 0 for success, negative value for failure
673  */
674 static int versal_ipi_setup(struct zynqmp_ipi_mbox *ipi_mbox,
675 			    struct device_node *node)
676 {
677 	struct zynqmp_ipi_mchan *tx_mchan, *rx_mchan;
678 	struct resource host_res, remote_res;
679 	struct device_node *parent_node;
680 	int host_idx, remote_idx;
681 	struct device *mdev;
682 
683 	tx_mchan = &ipi_mbox->mchans[IPI_MB_CHNL_TX];
684 	rx_mchan = &ipi_mbox->mchans[IPI_MB_CHNL_RX];
685 	parent_node = of_get_parent(node);
686 	mdev = &ipi_mbox->dev;
687 
688 	host_idx = zynqmp_ipi_mbox_get_buf_res(parent_node, "msg", &host_res);
689 	remote_idx = zynqmp_ipi_mbox_get_buf_res(node, "msg", &remote_res);
690 
691 	/*
692 	 * Only set up buffers if both sides claim to have msg buffers.
693 	 * This is because each buffered IPI's corresponding msg buffers
694 	 * are reserved for use by other buffered IPI's.
695 	 */
696 	if (!host_idx && !remote_idx) {
697 		u32 host_src, host_dst, remote_src, remote_dst;
698 		u32 buff_sz;
699 
700 		buff_sz = resource_size(&host_res);
701 
702 		host_src = host_res.start & SRC_BITMASK;
703 		remote_src = remote_res.start & SRC_BITMASK;
704 
705 		host_dst = (host_src >> DST_BIT_POS) * DEST_OFFSET;
706 		remote_dst = (remote_src >> DST_BIT_POS) * DEST_OFFSET;
707 
708 		/* Validate that IPI IDs is within IPI Message buffer space. */
709 		if (host_dst >= buff_sz || remote_dst >= buff_sz) {
710 			dev_err(mdev,
711 				"Invalid IPI Message buffer values: %x %x\n",
712 				host_dst, remote_dst);
713 			return -EINVAL;
714 		}
715 
716 		tx_mchan->req_buf = devm_ioremap(mdev,
717 						 host_res.start | remote_dst,
718 						 IPI_BUF_SIZE);
719 		if (!tx_mchan->req_buf) {
720 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
721 			return -ENOMEM;
722 		}
723 
724 		tx_mchan->resp_buf = devm_ioremap(mdev,
725 						  (remote_res.start | host_dst) +
726 						  RESP_OFFSET, IPI_BUF_SIZE);
727 		if (!tx_mchan->resp_buf) {
728 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
729 			return -ENOMEM;
730 		}
731 
732 		rx_mchan->req_buf = devm_ioremap(mdev,
733 						 remote_res.start | host_dst,
734 						 IPI_BUF_SIZE);
735 		if (!rx_mchan->req_buf) {
736 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
737 			return -ENOMEM;
738 		}
739 
740 		rx_mchan->resp_buf = devm_ioremap(mdev,
741 						  (host_res.start | remote_dst) +
742 						  RESP_OFFSET, IPI_BUF_SIZE);
743 		if (!rx_mchan->resp_buf) {
744 			dev_err(mdev, "Unable to map IPI buffer I/O memory\n");
745 			return -ENOMEM;
746 		}
747 
748 		tx_mchan->resp_buf_size = IPI_BUF_SIZE;
749 		tx_mchan->req_buf_size = IPI_BUF_SIZE;
750 		tx_mchan->rx_buf = devm_kzalloc(mdev, IPI_BUF_SIZE +
751 						sizeof(struct zynqmp_ipi_message),
752 						GFP_KERNEL);
753 		if (!tx_mchan->rx_buf)
754 			return -ENOMEM;
755 
756 		rx_mchan->resp_buf_size = IPI_BUF_SIZE;
757 		rx_mchan->req_buf_size = IPI_BUF_SIZE;
758 		rx_mchan->rx_buf = devm_kzalloc(mdev, IPI_BUF_SIZE +
759 						sizeof(struct zynqmp_ipi_message),
760 						GFP_KERNEL);
761 		if (!rx_mchan->rx_buf)
762 			return -ENOMEM;
763 	}
764 
765 	return 0;
766 }
767 
768 static int xlnx_mbox_cpuhp_start(unsigned int cpu)
769 {
770 	struct zynqmp_ipi_pdata *pdata;
771 
772 	pdata = get_cpu_var(per_cpu_pdata);
773 	put_cpu_var(per_cpu_pdata);
774 	enable_percpu_irq(pdata->virq_sgi, IRQ_TYPE_NONE);
775 
776 	return 0;
777 }
778 
779 static int xlnx_mbox_cpuhp_down(unsigned int cpu)
780 {
781 	struct zynqmp_ipi_pdata *pdata;
782 
783 	pdata = get_cpu_var(per_cpu_pdata);
784 	put_cpu_var(per_cpu_pdata);
785 	disable_percpu_irq(pdata->virq_sgi);
786 
787 	return 0;
788 }
789 
790 static void xlnx_disable_percpu_irq(void *data)
791 {
792 	struct zynqmp_ipi_pdata *pdata;
793 
794 	pdata = *this_cpu_ptr(&per_cpu_pdata);
795 
796 	disable_percpu_irq(pdata->virq_sgi);
797 }
798 
799 static int xlnx_mbox_init_sgi(struct platform_device *pdev,
800 			      int sgi_num,
801 			      struct zynqmp_ipi_pdata *pdata)
802 {
803 	int ret = 0;
804 	int cpu;
805 
806 	/*
807 	 * IRQ related structures are used for the following:
808 	 * for each SGI interrupt ensure its mapped by GIC IRQ domain
809 	 * and that each corresponding linux IRQ for the HW IRQ has
810 	 * a handler for when receiving an interrupt from the remote
811 	 * processor.
812 	 */
813 	struct irq_domain *domain;
814 	struct irq_fwspec sgi_fwspec;
815 	struct device_node *interrupt_parent = NULL;
816 	struct device *dev = &pdev->dev;
817 
818 	/* Find GIC controller to map SGIs. */
819 	interrupt_parent = of_irq_find_parent(dev->of_node);
820 	if (!interrupt_parent) {
821 		dev_err(&pdev->dev, "Failed to find property for Interrupt parent\n");
822 		return -EINVAL;
823 	}
824 
825 	/* Each SGI needs to be associated with GIC's IRQ domain. */
826 	domain = irq_find_host(interrupt_parent);
827 	of_node_put(interrupt_parent);
828 
829 	/* Each mapping needs GIC domain when finding IRQ mapping. */
830 	sgi_fwspec.fwnode = domain->fwnode;
831 
832 	/*
833 	 * When irq domain looks at mapping each arg is as follows:
834 	 * 3 args for: interrupt type (SGI), interrupt # (set later), type
835 	 */
836 	sgi_fwspec.param_count = 1;
837 
838 	/* Set SGI's hwirq */
839 	sgi_fwspec.param[0] = sgi_num;
840 	pdata->virq_sgi = irq_create_fwspec_mapping(&sgi_fwspec);
841 
842 	for_each_possible_cpu(cpu)
843 		per_cpu(per_cpu_pdata, cpu) = pdata;
844 
845 	ret = request_percpu_irq(pdata->virq_sgi, zynqmp_sgi_interrupt, pdev->name,
846 				 &per_cpu_pdata);
847 	WARN_ON(ret);
848 	if (ret) {
849 		irq_dispose_mapping(pdata->virq_sgi);
850 		return ret;
851 	}
852 
853 	irq_set_status_flags(pdata->virq_sgi, IRQ_PER_CPU);
854 
855 	/* Setup function for the CPU hot-plug cases */
856 	cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mailbox/sgi:starting",
857 			  xlnx_mbox_cpuhp_start, xlnx_mbox_cpuhp_down);
858 
859 	return ret;
860 }
861 
862 static void xlnx_mbox_cleanup_sgi(struct zynqmp_ipi_pdata *pdata)
863 {
864 	cpuhp_remove_state(CPUHP_AP_ONLINE_DYN);
865 
866 	on_each_cpu(xlnx_disable_percpu_irq, NULL, 1);
867 
868 	irq_clear_status_flags(pdata->virq_sgi, IRQ_PER_CPU);
869 	free_percpu_irq(pdata->virq_sgi, &per_cpu_pdata);
870 	irq_dispose_mapping(pdata->virq_sgi);
871 }
872 
873 /**
874  * zynqmp_ipi_free_mboxes - Free IPI mailboxes devices
875  *
876  * @pdata: IPI private data
877  */
878 static void zynqmp_ipi_free_mboxes(struct zynqmp_ipi_pdata *pdata)
879 {
880 	struct zynqmp_ipi_mbox *ipi_mbox;
881 	int i;
882 
883 	if (pdata->irq < MAX_SGI)
884 		xlnx_mbox_cleanup_sgi(pdata);
885 
886 	i = pdata->num_mboxes;
887 	for (; i >= 0; i--) {
888 		ipi_mbox = &pdata->ipi_mboxes[i];
889 		if (ipi_mbox->dev.parent) {
890 			mbox_controller_unregister(&ipi_mbox->mbox);
891 			if (device_is_registered(&ipi_mbox->dev))
892 				device_unregister(&ipi_mbox->dev);
893 		}
894 	}
895 }
896 
897 static int zynqmp_ipi_probe(struct platform_device *pdev)
898 {
899 	struct device *dev = &pdev->dev;
900 	struct device_node *nc, *np = pdev->dev.of_node;
901 	struct zynqmp_ipi_pdata __percpu *pdata;
902 	struct of_phandle_args out_irq;
903 	struct zynqmp_ipi_mbox *mbox;
904 	int num_mboxes, ret = -EINVAL;
905 	setup_ipi_fn ipi_fn;
906 
907 	num_mboxes = of_get_available_child_count(np);
908 	if (num_mboxes == 0) {
909 		dev_err(dev, "mailbox nodes not available\n");
910 		return -EINVAL;
911 	}
912 
913 	pdata = devm_kzalloc(dev, struct_size(pdata, ipi_mboxes, num_mboxes),
914 			     GFP_KERNEL);
915 	if (!pdata)
916 		return -ENOMEM;
917 	pdata->dev = dev;
918 
919 	/* Get the IPI local agents ID */
920 	ret = of_property_read_u32(np, "xlnx,ipi-id", &pdata->local_id);
921 	if (ret < 0) {
922 		dev_err(dev, "No IPI local ID is specified.\n");
923 		return ret;
924 	}
925 
926 	ipi_fn = (setup_ipi_fn)device_get_match_data(&pdev->dev);
927 	if (!ipi_fn) {
928 		dev_err(dev,
929 			"Mbox Compatible String is missing IPI Setup fn.\n");
930 		return -ENODEV;
931 	}
932 
933 	pdata->num_mboxes = num_mboxes;
934 
935 	mbox = pdata->ipi_mboxes;
936 	mbox->setup_ipi_fn = ipi_fn;
937 
938 	for_each_available_child_of_node(np, nc) {
939 		mbox->pdata = pdata;
940 		ret = zynqmp_ipi_mbox_probe(mbox, nc);
941 		if (ret) {
942 			of_node_put(nc);
943 			dev_err(dev, "failed to probe subdev.\n");
944 			ret = -EINVAL;
945 			goto free_mbox_dev;
946 		}
947 		mbox++;
948 	}
949 
950 	ret = of_irq_parse_one(dev_of_node(dev), 0, &out_irq);
951 	if (ret < 0) {
952 		dev_err(dev, "failed to parse interrupts\n");
953 		goto free_mbox_dev;
954 	}
955 	ret = out_irq.args[1];
956 
957 	/*
958 	 * If Interrupt number is in SGI range, then request SGI else request
959 	 * IPI system IRQ.
960 	 */
961 	if (ret < MAX_SGI) {
962 		pdata->irq = ret;
963 		ret = xlnx_mbox_init_sgi(pdev, pdata->irq, pdata);
964 		if (ret)
965 			goto free_mbox_dev;
966 	} else {
967 		ret = platform_get_irq(pdev, 0);
968 		if (ret < 0)
969 			goto free_mbox_dev;
970 
971 		pdata->irq = ret;
972 		ret = devm_request_irq(dev, pdata->irq, zynqmp_ipi_interrupt,
973 				       IRQF_SHARED, dev_name(dev), pdata);
974 	}
975 
976 	if (ret) {
977 		dev_err(dev, "IRQ %d is not requested successfully.\n",
978 			pdata->irq);
979 		goto free_mbox_dev;
980 	}
981 
982 	platform_set_drvdata(pdev, pdata);
983 	return ret;
984 
985 free_mbox_dev:
986 	zynqmp_ipi_free_mboxes(pdata);
987 	return ret;
988 }
989 
990 static void zynqmp_ipi_remove(struct platform_device *pdev)
991 {
992 	struct zynqmp_ipi_pdata *pdata;
993 
994 	pdata = platform_get_drvdata(pdev);
995 	zynqmp_ipi_free_mboxes(pdata);
996 }
997 
998 static const struct of_device_id zynqmp_ipi_of_match[] = {
999 	{ .compatible = "xlnx,zynqmp-ipi-mailbox",
1000 	  .data = &zynqmp_ipi_setup,
1001 	},
1002 	{ .compatible = "xlnx,versal-ipi-mailbox",
1003 	  .data = &versal_ipi_setup,
1004 	},
1005 	{},
1006 };
1007 MODULE_DEVICE_TABLE(of, zynqmp_ipi_of_match);
1008 
1009 static struct platform_driver zynqmp_ipi_driver = {
1010 	.probe = zynqmp_ipi_probe,
1011 	.remove_new = zynqmp_ipi_remove,
1012 	.driver = {
1013 		   .name = "zynqmp-ipi",
1014 		   .of_match_table = of_match_ptr(zynqmp_ipi_of_match),
1015 	},
1016 };
1017 
1018 static int __init zynqmp_ipi_init(void)
1019 {
1020 	return platform_driver_register(&zynqmp_ipi_driver);
1021 }
1022 subsys_initcall(zynqmp_ipi_init);
1023 
1024 static void __exit zynqmp_ipi_exit(void)
1025 {
1026 	platform_driver_unregister(&zynqmp_ipi_driver);
1027 }
1028 module_exit(zynqmp_ipi_exit);
1029 
1030 MODULE_LICENSE("GPL v2");
1031 MODULE_DESCRIPTION("Xilinx ZynqMP IPI Mailbox driver");
1032 MODULE_AUTHOR("Xilinx Inc.");
1033