Linux Audio

Check our new training course

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