File Index
Files and directories
Sign in
FILE

zynqmp-ipi-mailbox.c

File preview with syntax highlighting.

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