Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753259AbdLDGgR (ORCPT ); Mon, 4 Dec 2017 01:36:17 -0500 Received: from mail-sn1nam02on0060.outbound.protection.outlook.com ([104.47.36.60]:35376 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753148AbdLDGgF (ORCPT ); Mon, 4 Dec 2017 01:36:05 -0500 Authentication-Results: spf=pass (sender IP is 149.199.60.100) smtp.mailfrom=xilinx.com; vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=bestguesspass action=none header.from=xilinx.com; From: Wendy Liang To: , , , CC: , , , Wendy Liang Subject: [RFC v2 1/2] mailbox: ZynqMP IPI mailbox controller Date: Sun, 3 Dec 2017 22:25:34 -0800 Message-ID: <1512368735-27147-2-git-send-email-jliang@xilinx.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1512368735-27147-1-git-send-email-jliang@xilinx.com> References: <1512368735-27147-1-git-send-email-jliang@xilinx.com> X-RCIS-Action: ALLOW X-TM-AS-Product-Ver: IMSS-7.1.0.1224-8.1.0.1062-23506.006 X-TM-AS-User-Approved-Sender: Yes;Yes X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:149.199.60.100;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(376002)(346002)(39860400002)(2980300002)(438002)(199003)(189002)(9786002)(7696005)(33646002)(356003)(50226002)(15650500001)(5660300001)(51416003)(36386004)(478600001)(76176011)(4326008)(107886003)(2950100002)(6666003)(39060400002)(189998001)(47776003)(63266004)(106466001)(36756003)(305945005)(50466002)(48376002)(8936002)(110136005)(8676002)(77096006)(81166006)(81156014)(54906003)(106002)(316002)(16586007)(2906002)(107986001)(5001870100001);DIR:OUT;SFP:1101;SCL:1;SRVR:BN6PR02MB3378;H:xsj-pvapsmtpgw02;FPR:;SPF:Pass;PTR:xapps1.xilinx.com,unknown-60-100.xilinx.com;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;SN1NAM02FT020;1:V7r77IJm7PXvsaOX6Uw6IZQ6tOxSqyUXNUTOQMmrFPm/sWw9T93DR5Nej+g4Rv0GBkZukYdmhlLpuGHBhim3trTCY355SPCvAPxjW61XFOrUG+o0nUFncEcJbliuINxB MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1ceea855-a5bd-4870-fbee-08d53ae14913 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(4608076)(2017052603286);SRVR:BN6PR02MB3378; X-Microsoft-Exchange-Diagnostics: 1;BN6PR02MB3378;3:h/wxB0R+1uIuhX42aFkHCQtet7nU3mtLDxJZO3wV/krTnWCHqWjx/2jOBO+3WJUBl2lVItFy5IjocKvmJeV7cZ1ABAe+MaRsku9oxLZu2PexhvRHvehbjL2E0c/y/7DKkA3H2QffGtwg8GSLJq/AWXU7w2FbCuWVbLC80el1XzNwtUmlaPDnxgWaOZuy2PgRmsf3TnK9DQEiUpfluM2E4pbVyqMd7CDjgpGaHe8xOFy2YlxjW0FUKIdndwjqHmYDZnoiQYoPKz5hgQpRfCr6xyLBxqPcy/1NBY6XvYXQUmKb3RNABSzJPx+4CKFR7wZ9szmR4GXsPF62Vvu4W7tm/CMoVhHXt6cB7kD2D9Wcscw=;25:SuCCNwV+QiuvlbC/IaSxsjIHfw/b4PfHHJJ+/lWOApZ+7VAH4BLRy/mtd45xKZmILzF4pCtafFPdN6aZjlhJ98T6yGh0x66uktMtJNUmSga/b3sLW1jD/dsEu54cbf195QIopcldifBx0IrbJ+RbkJ08LfN8qoZH8ebg0P609tJSj21Q6wEouNX/iI/3wMVDp4VeQ9b7xlK8VN2h6Mc5k5+N9BX1qLoc8kJ6n1aYFmIT6knYRX7vjchsnmJt66lI76BIJ4pbWGkVmxqFQ7odksTRMOovO1SjjceWRIMppqjKso2p20hl7bSn+MR5LdJZTyOmc8W6kARqErQTHq2oRA== X-MS-TrafficTypeDiagnostic: BN6PR02MB3378: X-Microsoft-Exchange-Diagnostics: 1;BN6PR02MB3378;31:teogg+YkE6Zs38c/BEQc8bJpcUNWHDi/C2PStL2NSOZ5JgnkRLxb5YjVExY2YgZsKBJM4ooDbZbUg5RVMXWHGNazd7yq9wZ84T4p1aMm2N0fKZXVxOv0PeNmsrgqYsC6gDbpVxz/AJx0wVl25U2SP7qafx/qwiE2hHBgGmkNdNtKQpknNvR47wRHp/AFba9AXdie77KugjOPQ6UdWLt/92DwovD6uYfi0y+AedC34jA=;20:eIHX1EjpWzPB66cSRzQ7eoMwz0UQXkEUs+NFEjDo3nW5XFvdHM2Xmd/RaJUBagx5cmxHIeO3Dpz/9KLkvRjQlJCTRSbwS6lL35du+EuxlRfpTKKIjZjN5Ysd3M0kVKf2dO/Wfsw2Cfqk8xZV81cfAFwHUVMJjBA34ludE7xSGdS4UVQyR9RR92QkbLbC1q5te0UiAWDp5y1WAbFeBYltiapAWKEaI3MxYq3uYmzeP0OgpRLNcGXLsMVlk+HTEQoOfrM65/Rd8XqkrMomEbjTF6/MGGMuILYdYHIwxpohBB8dYDNv3k8+j8yDo5t8q/gQo5q6lG87KvW7+m+zYyvfdS9E3K45ZlxhnNBUILIrMxbjA480zDkHvthMQyj7P1SdpkMC4jVfpZkMr0oLwm/FgLvwg3347TgbkYmmPrPkovwu4B7K2bzP1NcsyRJBqRHmkbb7Y/5GUolpDJDoL5cZg6HSvn5hUEtWS00/Y/Zlsm48JIim4Ty1u1E3xVWHu2iJ X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192813158149592); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(2401047)(5005006)(8121501046)(10201501046)(3231022)(3002001)(93006095)(93004095)(6055026)(6041248)(20161123555025)(20161123560025)(20161123558100)(20161123564025)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:BN6PR02MB3378;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:BN6PR02MB3378; X-Microsoft-Exchange-Diagnostics: 1;BN6PR02MB3378;4:zm6zQ+DfNsnzuoBGK/RJOPCjzeHbiwwV8bbaGGYvau8o/WV0SPXat4XFq6wa+Djh688s4+0aQL3n/CEYp/BYeX07Bnc1H+TmknzsHenJ0Z4/NahWecNFaPA4xmvgYSNj/4oI2wvEpyVw7+i3xPIojibqZH6lfqb0Me0wIGYWwpUVUQNyO6UCBNYdnx26cq1nYqI6xfLuDQboMp2/K3BnHCtgM2Ez0uOVc/LVDK3+mWGb8yYc8EDBX6BokIfn8GWjzjaWlPsDu7ODur1cBU22qc8WuvB/tp6alvuMgspPnwbX8rkmjGqEy3IgskqFlKro X-Forefront-PRVS: 051158ECBB X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BN6PR02MB3378;23:RngQZvXu3LACd/ziso9u/zhjxbNo529uH3S1BCaNe?= =?us-ascii?Q?wx3pS3FNLXy0MAzdlF5xRNZcQGieovB/XbELHmVD4B4TEomCpjpfMGwx0ay9?= =?us-ascii?Q?Z+woTVK6hyO7NWCSop6sM50RJ5BmNIrWxUBSDfgPl8xP+TPwx1Iamn1Z7TqT?= =?us-ascii?Q?+2pNUnfi0yCO7Xw1Z77qgEmC84USpqFNAc69VDea3450o4VgKzHoVlZSrwr6?= =?us-ascii?Q?3JEIzjv6MAZmr+aj8fIItMZ2EOKWBRZiSxUIUF474RCr43+1CkJwoR0Cmn+v?= =?us-ascii?Q?1VKxqw8sJvLYqHI8/0p4R+1LmR6uggjqoWZHwtUvvqnonJbrtq55JXtDWR8V?= =?us-ascii?Q?z52sTXqT02aCXKgwzrSaHkzJlgJ21Pk4KVgprns0ohhADpRleBQ3g3hplg4u?= =?us-ascii?Q?MCCAd+sCi644Y3d+UXCm5dkKFEer2/VZcpzvSvuyovGIP+csmRqxOqfmENvB?= =?us-ascii?Q?zSKevfC93baTnQSelvzojcJv37o+2Q4Fvn2Nme4Sldfqawc9kzRF0K9aAX60?= =?us-ascii?Q?iY0LOQBd+L42lmH4rTdIdRX6v8ZpRfStK4pbyUSBW220ndW/nKfPHN9eOqpC?= =?us-ascii?Q?l7ot8pmwUUmT8FJamweOou+RwmS5I5c+H06a1qGVP6POGT4L/EQ4bjxirOOV?= =?us-ascii?Q?c+ToNl5ULwfvP96piI6SlNiWZDqdqTVUteuopjNZRiIsQIG9bPoXaNt6S4P0?= =?us-ascii?Q?G6ib4JUYmP0iJ0EZuB/LoBF5KnUnT0V9O3OupnQ3bwdbTGbP6pNiEfB+Kc8I?= =?us-ascii?Q?2hoTAxuAiSFX8OMcowp51Jo/X17pcm5uZeNBa1Y4lTTGQX4kHcafdb1sY+Yn?= =?us-ascii?Q?pNrPkJppVBwqxiOyVVXRvZtOMjhpuyPbLjsV+Cg3PhUOVgiZWsUIlBhGX5Io?= =?us-ascii?Q?uPbbupinBcjgOTaZBYT9Uh0T6HOoe2IQUCpI2q8GD2JjbH/Sy2MIs5xBAPs/?= =?us-ascii?Q?fUkkDtD6db5mXjenmp3yVff/8eyDqwOXjPXLrlcMI3ZkA34AwFqiDgRJkALt?= =?us-ascii?Q?+LRRMLM+dENQqYJESOuTMR0PmByfjLkQ6xIk8ck0nJw4ShXIl0PJy5TNh/95?= =?us-ascii?Q?QXyOlzuS1/l6IKRqsqYMQ53CG8a?= X-Microsoft-Exchange-Diagnostics: 1;BN6PR02MB3378;6:XYi+maGN4STwUzSxG0MPOIqCteWg+ccg2l51xywLrFuCaNxfDrzzeYeP+wG9UYYa0fvdI6aJDARGj2dZEA5l/UzpATgmyN0qnqsut8F2C46tV++row2e2k0EJPfiq+eDGMxD8hf5Y6DcjXZ/szOd9bxcyhdC12qWlbU29A8X5CrxNiewNeo2yRI9GDlENfa2HQ/GWLF01RGRSA6hyh1mjWbcwS9vbiEpU/RlLPtoe8tEdauSY/vnM/UOFDHbKBrR4yMeYpN7300BUHs8U4qWlaq+rxp+Scr0q63ltWl8WhgWRLl7rwI6oG21K/5hVyFAJCkjzgThq0ZIi/MqFcuCeeQtxZ3bcVLZP55gwULO9xU=;5:7ymgrOlBmHWtOgcExzgpx4kyXxOpQGJbH7SysdF4+imfvuqU6/c+ppA0fVTglO67b+Y3LM2ZgRzRxO1vp85Pf0GO7CCF0oNE6g+5YZrZWUsBOE83u9FQqFIlv2SYFE9IUbEwsnumtdlmW3zVzuwNdOuEgseZPDdhcp7UPNCzEdk=;24:60Whm3AgJeoQB3oYEi/J2GeqQvy+PvxoaLHvLnk+CcAMO75oEel/wl6dmetqtNLTLGU7pxuuHQ20SCzgG8dCp8/hb1I+hn9W3fvyXEp/OvQ=;7:50h37Oej/2pQ21GB0U5EqqK6EoDs+tOKXk+g3wz3OoGpzdgdFGCbQjA5HP9iZK8PYwhxFfvw5drwns7EviNSqmZxNRH5sOlonEHBnpwtAf2CPHs4FsQLsXhn8v5W3qkMVMit+5f4ZmvFq2qJdLPC66kbi8A/32SFDy6Gc+wYVnpOWwu0GbmLXx0pe41+gtGWYIMY1UXdc+SGzmK5PRS6TVhaUGWrjbc4dOjxMHG7WTIgPVjpLTT6+hPzILt7DaXz SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2017 06:36:00.6985 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1ceea855-a5bd-4870-fbee-08d53ae14913 X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c;Ip=[149.199.60.100];Helo=[xsj-pvapsmtpgw02] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR02MB3378 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 21128 Lines: 711 This patch is to introduce ZynqMP IPI mailbox controller driver to use the ZynqMP IPI block as mailboxes. Signed-off-by: Wendy Liang --- drivers/mailbox/Kconfig | 8 + drivers/mailbox/Makefile | 2 + drivers/mailbox/zynqmp-ipi-mailbox.c | 633 +++++++++++++++++++++++++++++ include/linux/mailbox/zynqmp-ipi-message.h | 24 ++ 4 files changed, 667 insertions(+) create mode 100644 drivers/mailbox/zynqmp-ipi-mailbox.c create mode 100644 include/linux/mailbox/zynqmp-ipi-message.h diff --git a/drivers/mailbox/Kconfig b/drivers/mailbox/Kconfig index ba2f152..876614a 100644 --- a/drivers/mailbox/Kconfig +++ b/drivers/mailbox/Kconfig @@ -171,4 +171,12 @@ config BCM_FLEXRM_MBOX Mailbox implementation of the Broadcom FlexRM ring manager, which provides access to various offload engines on Broadcom SoCs. Say Y here if you want to use the Broadcom FlexRM. + +config ZYNQMP_IPI_MBOX + tristate "Xilinx ZynqMP IPI Mailbox" + depends on ARCH_ZYNQMP && OF + help + Mailbox implementation for Xilinx ZynqMP IPI. It is used to send + notification or short message between processors with Xilinx + ZynqMP IPI. endif diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile index 4896f8d..155f72f 100644 --- a/drivers/mailbox/Makefile +++ b/drivers/mailbox/Makefile @@ -36,3 +36,5 @@ obj-$(CONFIG_BCM_FLEXRM_MBOX) += bcm-flexrm-mailbox.o obj-$(CONFIG_QCOM_APCS_IPC) += qcom-apcs-ipc-mailbox.o obj-$(CONFIG_TEGRA_HSP_MBOX) += tegra-hsp.o + +obj-$(CONFIG_ZYNQMP_IPI_MBOX) += zynqmp-ipi-mailbox.o diff --git a/drivers/mailbox/zynqmp-ipi-mailbox.c b/drivers/mailbox/zynqmp-ipi-mailbox.c new file mode 100644 index 0000000..334f5e1 --- /dev/null +++ b/drivers/mailbox/zynqmp-ipi-mailbox.c @@ -0,0 +1,633 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Xilinx Inter Processor Interrupt(IPI) Mailbox Driver + * + * Copyright (C) 2017 Xilinx Inc. + * + */ + +#define pr_fmt(fmt) "%s: " fmt, __func__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* IPI agent ID any */ +#define IPI_ID_ANY 0xFFUL + +/* indicate if ZynqMP IPI mailbox driver uses SMC calls or HVC calls */ +#define USE_SMC 0 +#define USE_HVC 1 + +/* Default IPI SMC function IDs */ +#define SMC_IPI_MAILBOX_OPEN 0x82001000U +#define SMC_IPI_MAILBOX_RELEASE 0x82001001U +#define SMC_IPI_MAILBOX_STATUS_ENQUIRY 0x82001002U +#define SMC_IPI_MAILBOX_NOTIFY 0x82001003U +#define SMC_IPI_MAILBOX_ACK 0x82001004U +#define SMC_IPI_MAILBOX_ENABLE_IRQ 0x82001005U +#define SMC_IPI_MAILBOX_DISABLE_IRQ 0x82001006U + +/* IPI SMC Macros */ +#define IPI_SMC_OPEN_IRQ_MASK 0x00000001UL /* IRQ enable bit in IPI + * open SMC call + */ +#define IPI_SMC_NOTIFY_BLOCK_MASK 0x00000001UL /* Flag to indicate if + * IPI notification needs + * to be blocking. + */ +#define IPI_SMC_ENQUIRY_DIRQ_MASK 0x00000001UL /* Flag to indicate if + * notification interrupt + * to be disabled. + */ +#define IPI_SMC_ACK_EIRQ_MASK 0x00000001UL /* Flag to indicate if + * notification interrupt + * to be enabled. + */ + +/* IPI mailbox status */ +#define IPI_MB_STATUS_IDLE 0 +#define IPI_MB_STATUS_SEND_PENDING 1 +#define IPI_MB_STATUS_RECV_PENDING 2 + +#define IPI_MB_CHNL_TX 0 /* IPI mailbox TX channel */ +#define IPI_MB_CHNL_RX 1 /* IPI mailbox RX channel */ + +/** + * struct zynqmp_ipi_mchan - Description of a Xilinx ZynqMP IPI mailbox channel + * @is_opened: indicate if the IPI channel is opened + * @req_buf: local to remote request buffer start address + * @resp_buf: local to remote response buffer start address + * @req_buf_size: request buffer size + * @resp_buf_size: response buffer size + * @chan_type: channel type + */ +struct zynqmp_ipi_mchan { + int is_opened; + void __iomem *req_buf; + void __iomem *resp_buf; + size_t req_buf_size; + size_t resp_buf_size; + unsigned int chan_type; +}; + +/** + * struct zynqmp_ipi_mbox_pdata - Description of a ZynqMP IPI mailbox + * platform data. + * @dev: device pointer corresponding to the Xilinx ZynqMP + * IPI mailbox + * @local_id: local IPI agent ID + * @remote_id: remote IPI agent ID + * @method: IPI SMC or HVC is going to be used + * @mbox: mailbox Controller + * @mchans: array for channels, tx channel and rx channel. + * @irq: IPI agent interrupt ID + * @lock: IPI mailbox platform data lock + */ +struct zynqmp_ipi_mbox_pdata { + struct device *dev; + u32 local_id; + u32 remote_id; + unsigned int method; + struct mbox_controller mbox; + struct zynqmp_ipi_mchan mchans[2]; + int irq; + spinlock_t lock; /* spin lock for local data */ +}; + +static void zynqmp_ipi_fw_call(struct zynqmp_ipi_mbox_pdata *pdata, + unsigned long a0, unsigned long a3, + unsigned long a4, unsigned long a5, + unsigned long a6, unsigned long a7, + struct arm_smccc_res *res) +{ + unsigned long a1, a2; + + a1 = pdata->local_id; + a2 = pdata->remote_id; + if (pdata->method == USE_SMC) + arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res); + else + arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res); +} + +/** + * zynqmp_ipi_interrupt - Interrupt handler for IPI notification + * + * @irq: Interrupt number + * @data: ZynqMP IPI mailbox platform data. + * + * Return: -EINVAL if there is no instance + * IRQ_NONE if the interrupt is not ours. + * IRQ_HANDLED if the rx interrupt was successfully handled. + */ +static irqreturn_t zynqmp_ipi_interrupt(int irq, void *data) +{ + struct zynqmp_ipi_mbox_pdata *pdata = data; + struct mbox_chan *chan; + struct zynqmp_ipi_mchan *mchan; + struct zynqmp_ipi_message msg; + u64 arg0, arg3; + struct arm_smccc_res res; + int ret; + + arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY; + arg3 = IPI_SMC_ENQUIRY_DIRQ_MASK; + zynqmp_ipi_fw_call(pdata, arg0, arg3, 0, 0, 0, 0, &res); + ret = (int)(res.a0 & 0xFFFFFFFF); + if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) { + chan = &pdata->mbox.chans[IPI_MB_CHNL_RX]; + mchan = chan->con_priv; + if (mchan->is_opened) { + msg.len = mchan->req_buf_size; + msg.data = mchan->req_buf; + /* Client will direclty copy data from + * IPI buffer to client data memory + */ + mbox_chan_received_data(chan, (void *)&msg); + return IRQ_HANDLED; + } + } + return IRQ_NONE; +} + +/** + * zynqmp_ipi_peek_data - Peek to see if there are any rx messages. + * + * @chan: Channel Pointer + * + * Return: 'true' if there is pending rx data, 'false' if there is none. + */ +static bool zynqmp_ipi_peek_data(struct mbox_chan *chan) +{ + struct device *dev = chan->mbox->dev; + struct zynqmp_ipi_mbox_pdata *pdata = dev_get_drvdata(dev); + struct zynqmp_ipi_mchan *mchan = chan->con_priv; + int ret; + u64 arg0; + struct arm_smccc_res res; + + if (WARN_ON(!pdata)) { + dev_err(dev, "no platform drv data??\n"); + return false; + } + + arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + ret = (int)(res.a0 & 0xFFFFFFFF); + + if (mchan->chan_type == IPI_MB_CHNL_TX) { + /* TX channel, check if the message has been acked + * by the remote, if yes, response is available. + */ + if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING) + return false; + else + return true; + } else if (ret > 0 && ret & IPI_MB_STATUS_RECV_PENDING) { + /* RX channel, check if there is message arrived. */ + return true; + } + return false; +} + +/** + * zynqmp_ipi_last_tx_done - See if the last tx message is sent + * + * @chan: Channel pointer + * + * Return: 'true' is no pending tx data, 'false' if there are any. + */ +static bool zynqmp_ipi_last_tx_done(struct mbox_chan *chan) +{ + struct device *dev = chan->mbox->dev; + struct zynqmp_ipi_mbox_pdata *pdata = dev_get_drvdata(dev); + struct zynqmp_ipi_mchan *mchan = chan->con_priv; + int ret; + u64 arg0; + struct arm_smccc_res res; + struct zynqmp_ipi_message msg; + + if (WARN_ON(!pdata)) { + dev_err(dev, "no platform drv data??\n"); + return false; + } + + if (mchan->chan_type == IPI_MB_CHNL_TX) { + /* We only need to check if the message been taken + * by the remote in the TX channel + */ + arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + /* Check the SMC call status, a0 of the result */ + ret = (int)(res.a0 & 0xFFFFFFFF); + if (ret < 0 || ret & IPI_MB_STATUS_SEND_PENDING) + return false; + + msg.len = mchan->resp_buf_size; + msg.data = mchan->resp_buf; + /* Client will direclty copy data from + * IPI buffer to client data memory + */ + mbox_chan_received_data(chan, (void *)&msg); + return true; + } + /* Always true for the response message in RX channel */ + return true; +} + +/** + * zynqmp_ipi_send_data - Send data + * + * @chan: Channel Pointer + * @data: Message Pointer + * + * Return: 0 if all goes good, else appropriate error messages. + */ +static int zynqmp_ipi_send_data(struct mbox_chan *chan, void *data) +{ + struct device *dev = chan->mbox->dev; + struct zynqmp_ipi_mbox_pdata *pdata = dev_get_drvdata(dev); + struct zynqmp_ipi_mchan *mchan = chan->con_priv; + struct zynqmp_ipi_message *msg = data; + u64 arg0; + struct arm_smccc_res res; + u32 timeout; + int ret; + + if (WARN_ON(!pdata)) { + dev_err(dev, "no platform drv data??\n"); + return -EINVAL; + } + + if (mchan->chan_type == IPI_MB_CHNL_TX) { + /* Send request message */ + if (msg && msg->len > mchan->resp_buf_size) { + dev_err(dev, "channel %d message length %u > max %lu\n", + mchan->chan_type, (unsigned int)msg->len, + mchan->resp_buf_size); + return -EINVAL; + } + /* Enquire if the mailbox is free to send message */ + arg0 = SMC_IPI_MAILBOX_STATUS_ENQUIRY; + timeout = 10; + do { + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + ret = res.a0 & 0xFFFFFFFF; + if (ret >= 0 && !(ret & IPI_MB_STATUS_SEND_PENDING)) + break; + usleep_range(1, 2); + timeout--; + } while (timeout); + if (!timeout) { + dev_warn(dev, "channel %d sending msg timesout.\n", + pdata->remote_id); + return -ETIME; + } + /* Copy message to the request buffer */ + if (msg && msg->len) + memcpy_toio(mchan->req_buf, msg->data, msg->len); + /* Kick IPI mailbox to send message */ + arg0 = SMC_IPI_MAILBOX_NOTIFY; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + } else { + /* Send response message */ + if (msg && msg->len > mchan->resp_buf_size) { + dev_err(dev, "channel %d message length %u > max %lu\n", + mchan->chan_type, (unsigned int)msg->len, + mchan->resp_buf_size); + return -EINVAL; + } + if (msg && msg->len) + memcpy(mchan->resp_buf, msg->data, msg->len); + arg0 = SMC_IPI_MAILBOX_NOTIFY; + arg0 = SMC_IPI_MAILBOX_ACK; + zynqmp_ipi_fw_call(pdata, arg0, IPI_SMC_ACK_EIRQ_MASK, + 0, 0, 0, 0, &res); + } + return 0; +} + +/** + * zynqmp_ipi_startup - Startup the IPI channel + * + * @chan: Channel pointer + * + * Return: 0 if all goes good, else return corresponding error message + */ +static int zynqmp_ipi_startup(struct mbox_chan *chan) +{ + struct device *dev = chan->mbox->dev; + struct zynqmp_ipi_mbox_pdata *pdata = dev_get_drvdata(dev); + struct zynqmp_ipi_mchan *mchan = chan->con_priv; + u64 arg0; + struct arm_smccc_res res; + int ret = 0; + unsigned long flags; + unsigned int nchan_type; + + spin_lock_irqsave(&pdata->lock, flags); + if (mchan->is_opened) { + spin_unlock_irqrestore(&pdata->lock, flags); + return 0; + } + + /* If no channel has been opened, open the IPI mailbox */ + nchan_type = (mchan->chan_type + 1) % 2; + if (!pdata->mchans[nchan_type].is_opened) { + arg0 = SMC_IPI_MAILBOX_OPEN; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + /* Check the SMC call status, a0 of the result */ + ret = (int)(res.a0 | 0xFFFFFFFF); + if (res.a0 < 0) { + dev_err(dev, "SMC to open the IPI channel failed.\n"); + ret = res.a0; + spin_unlock_irqrestore(&pdata->lock, flags); + return ret; + } + ret = 0; + } + + /* If it is RX channel, enable the IPI notification interrupt */ + if (mchan->chan_type == IPI_MB_CHNL_RX) { + arg0 = SMC_IPI_MAILBOX_ENABLE_IRQ; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + } + mchan->is_opened = 1; + spin_unlock_irqrestore(&pdata->lock, flags); + + return ret; +} + +/** + * zynqmp_ipi_shutdown - Shutdown the IPI channel + * + * @chan: Channel pointer + */ +static void zynqmp_ipi_shutdown(struct mbox_chan *chan) +{ + struct device *dev = chan->mbox->dev; + struct zynqmp_ipi_mbox_pdata *pdata = dev_get_drvdata(dev); + struct zynqmp_ipi_mchan *mchan = chan->con_priv; + u64 arg0; + struct arm_smccc_res res; + unsigned long flags; + unsigned int chan_type; + + spin_lock_irqsave(&pdata->lock, flags); + if (!mchan->is_opened) { + spin_unlock_irqrestore(&pdata->lock, flags); + return; + } + + /* If it is RX channel, disable notification interrupt */ + chan_type = mchan->chan_type; + if (chan_type == IPI_MB_CHNL_RX) { + arg0 = SMC_IPI_MAILBOX_DISABLE_IRQ; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + } + /* Release IPI mailbox if no other channel is opened */ + chan_type = (chan_type + 1) % 2; + if (!pdata->mchans[chan_type].is_opened) { + arg0 = SMC_IPI_MAILBOX_RELEASE; + zynqmp_ipi_fw_call(pdata, arg0, 0, 0, 0, 0, 0, &res); + } + + mchan->is_opened = 0; + spin_unlock_irqrestore(&pdata->lock, flags); +} + +/* ZynqMP IPI mailbox operations */ +static const struct mbox_chan_ops zynqmp_ipi_chan_ops = { + .startup = zynqmp_ipi_startup, + .shutdown = zynqmp_ipi_shutdown, + .peek_data = zynqmp_ipi_peek_data, + .last_tx_done = zynqmp_ipi_last_tx_done, + .send_data = zynqmp_ipi_send_data, +}; + +/** + * zynqmp_ipi_of_xlate - Translate of phandle to IPI mailbox channel + * + * @mbox: mailbox controller pointer + * @p: phandle pointer + * + * Return: Mailbox channel, else return error pointer. + */ +static struct mbox_chan *zynqmp_ipi_of_xlate(struct mbox_controller *mbox, + const struct of_phandle_args *p) +{ + struct zynqmp_ipi_mbox_pdata *pdata; + struct mbox_chan *chan; + struct device *dev = mbox->dev; + unsigned int chan_type; + + pdata = container_of(mbox, struct zynqmp_ipi_mbox_pdata, mbox); + + /* Only supports TX and RX channels */ + chan_type = p->args[0]; + if (chan_type != IPI_MB_CHNL_TX && chan_type != IPI_MB_CHNL_RX) { + dev_err(dev, "req chnl failure: invalid chnl type %u.\n", + chan_type); + return ERR_PTR(-EINVAL); + } + chan = &mbox->chans[chan_type]; + return chan; +} + +static const struct of_device_id zynqmp_ipi_of_match[] = { + {.compatible = "xlnx,zynqmp-ipi-mailbox"}, +}; +MODULE_DEVICE_TABLE(of, zynqmp_ipi_of_match); + +static int zynqmp_ipi_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *np = pdev->dev.of_node; + struct zynqmp_ipi_mbox_pdata *pdata; + struct zynqmp_ipi_mchan *mchan; + struct mbox_chan *chans; + struct mbox_controller *mbox; + const unsigned char *prop; + struct resource *res; + int ret = -EINVAL; + + pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) + return -ENOMEM; + + pdata->dev = dev; + + mchan = &pdata->mchans[IPI_MB_CHNL_TX]; + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + "local_request_region"); + if (res) { + mchan->req_buf_size = resource_size(res); + mchan->req_buf = devm_ioremap(&pdev->dev, res->start, + mchan->req_buf_size); + if (IS_ERR(mchan->req_buf)) { + dev_err(dev, "Unable to map IPI buffer I/O memory\n"); + ret = PTR_ERR(mchan->req_buf); + return ret; + } + } + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + "remote_response_region"); + if (res) { + mchan->resp_buf_size = resource_size(res); + mchan->resp_buf = devm_ioremap(&pdev->dev, res->start, + mchan->resp_buf_size); + if (IS_ERR(mchan->resp_buf)) { + dev_err(dev, "Unable to map IPI buffer I/O memory\n"); + ret = PTR_ERR(mchan->resp_buf); + return ret; + } + } + + mchan = &pdata->mchans[IPI_MB_CHNL_RX]; + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + "remote_request_region"); + if (res) { + mchan->req_buf_size = resource_size(res); + mchan->req_buf = devm_ioremap(&pdev->dev, res->start, + mchan->req_buf_size); + if (IS_ERR(mchan->req_buf)) { + dev_err(dev, "Unable to map IPI buffer I/O memory\n"); + ret = PTR_ERR(mchan->req_buf); + return ret; + } + } + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, + "local_response_region"); + if (res) { + mchan->resp_buf_size = resource_size(res); + mchan->resp_buf = devm_ioremap(&pdev->dev, res->start, + mchan->resp_buf_size); + if (IS_ERR(mchan->resp_buf)) { + dev_err(dev, "Unable to map IPI buffer I/O memory\n"); + ret = PTR_ERR(mchan->resp_buf); + return ret; + } + } + + /* Get the IPI local and remote agents IDs */ + ret = of_property_read_u32_index(np, "xlnx,ipi-ids", 0, + &pdata->local_id); + if (ret < 0) { + dev_err(dev, "No IPI local ID is specified.\n"); + return ret; + } + ret = of_property_read_u32_index(np, "xlnx,ipi-ids", 1, + &pdata->remote_id); + if (ret < 0) { + dev_err(dev, "No IPI remote ID is specified.\n"); + return ret; + } + + /* Get how to access IPI agent method */ + prop = of_get_property(np, "method", NULL); + if (!prop) { + pdata->method = USE_SMC; + } else if (!strcmp(prop, "smc")) { + pdata->method = USE_SMC; + } else if (!strcmp(prop, "hvc")) { + pdata->method = USE_HVC; + } else { + dev_err(dev, "Invalid \"method\" %s.\n", prop); + return ret; + } + + /* IPI IRQ */ + ret = platform_get_irq(pdev, 0); + if (ret < 0) { + dev_err(dev, "unable to find IPI IRQ.\n"); + return ret; + } + pdata->irq = ret; + ret = devm_request_irq(dev, pdata->irq, zynqmp_ipi_interrupt, + IRQF_SHARED, dev_name(dev), pdata); + if (ret) { + dev_err(dev, "IRQ %d is not requested successfully.\n", + pdata->irq); + return ret; + } + + mbox = &pdata->mbox; + mbox->dev = dev; + mbox->ops = &zynqmp_ipi_chan_ops; + mbox->num_chans = 2; + mbox->txdone_irq = false; + mbox->txdone_poll = true; + mbox->txpoll_period = 5; + mbox->of_xlate = zynqmp_ipi_of_xlate; + chans = devm_kzalloc(dev, 2 * sizeof(*chans), GFP_KERNEL); + if (!chans) + return -ENOMEM; + mbox->chans = chans; + mbox->chans[IPI_MB_CHNL_TX].con_priv = &pdata->mchans[IPI_MB_CHNL_TX]; + mbox->chans[IPI_MB_CHNL_RX].con_priv = &pdata->mchans[IPI_MB_CHNL_RX]; + pdata->mchans[IPI_MB_CHNL_TX].chan_type = IPI_MB_CHNL_TX; + pdata->mchans[IPI_MB_CHNL_RX].chan_type = IPI_MB_CHNL_RX; + spin_lock_init(&pdata->lock); + platform_set_drvdata(pdev, pdata); + ret = mbox_controller_register(mbox); + if (ret) + dev_err(dev, "Failed to register mbox_controller(%d)\n", ret); + else + dev_info(dev, "Probed ZynqMP IPI Mailbox driver.\n"); + return ret; +} + +static int zynqmp_ipi_remove(struct platform_device *pdev) +{ + struct zynqmp_ipi_mbox_pdata *pdata; + + pdata = platform_get_drvdata(pdev); + mbox_controller_unregister(&pdata->mbox); + + return 0; +} + +static struct platform_driver zynqmp_ipi_driver = { + .probe = zynqmp_ipi_probe, + .remove = zynqmp_ipi_remove, + .driver = { + .name = "zynqmp-ipi", + .of_match_table = of_match_ptr(zynqmp_ipi_of_match), + }, +}; + +static struct class zynqmp_ipi_class = { .name = "zynqmp_ipi_mbox", }; + +static int __init zynqmp_ipi_init(void) +{ + int err; + + err = class_register(&zynqmp_ipi_class); + if (err) + return err; + + return platform_driver_register(&zynqmp_ipi_driver); +} +subsys_initcall(zynqmp_ipi_init); + +static void __exit zynqmp_ipi_exit(void) +{ + platform_driver_unregister(&zynqmp_ipi_driver); + class_unregister(&zynqmp_ipi_class); +} +module_exit(zynqmp_ipi_exit); + +MODULE_LICENSE("GPL v2"); +MODULE_DESCRIPTION("Xilinx ZynqMP IPI Mailbox driver"); +MODULE_AUTHOR("Xilinx Inc."); diff --git a/include/linux/mailbox/zynqmp-ipi-message.h b/include/linux/mailbox/zynqmp-ipi-message.h new file mode 100644 index 0000000..173c41d --- /dev/null +++ b/include/linux/mailbox/zynqmp-ipi-message.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2017 Xilinx Inc. + * + */ + +#ifndef _LINUX_ZYNQMP_IPI_MESSAGE_H_ +#define _LINUX_ZYNQMP_IPI_MESSAGE_H_ + +/** + * struct zynqmp_ipi_message - ZynqMP IPI message structure + * @len: Length of the request message + * @data: Request message pointer + * + * This is the structure for data used in mbox_send_message + * the maximum length of data buffer is fixed to 12 bytes. + * Client is supposed to be aware of this. + */ +struct zynqmp_ipi_message { + size_t len; + u8 *data; +}; + +#endif /* _LINUX_ZYNQMP_IPI_MESSAGE_H_ */ -- 2.7.4