Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751390AbeADXwX (ORCPT + 1 other); Thu, 4 Jan 2018 18:52:23 -0500 Received: from mail-by2nam03on0072.outbound.protection.outlook.com ([104.47.42.72]:39067 "EHLO NAM03-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751298AbeADXv7 (ORCPT ); Thu, 4 Jan 2018 18:51:59 -0500 From: Wendy Liang To: , , , CC: , , , Wendy Liang Subject: [PATCH v3 1/2] mailbox: ZynqMP IPI mailbox controller Date: Thu, 4 Jan 2018 15:51:30 -0800 Message-ID: <1515109891-17133-2-git-send-email-jliang@xilinx.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515109891-17133-1-git-send-email-jliang@xilinx.com> References: <1515109891-17133-1-git-send-email-jliang@xilinx.com> X-RCIS-Action: ALLOW X-TM-AS-Product-Ver: IMSS-7.1.0.1224-8.2.0.1013-23572.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)(39860400002)(346002)(39380400002)(376002)(396003)(2980300002)(438002)(189003)(199004)(305945005)(316002)(48376002)(9786002)(16586007)(106002)(81156014)(110136005)(356003)(8676002)(50226002)(81166006)(107886003)(4326008)(77096006)(36386004)(15650500001)(50466002)(54906003)(478600001)(106466001)(6666003)(2906002)(8936002)(51416003)(5660300001)(7696005)(47776003)(76176011)(59450400001)(36756003)(63266004)(2950100002)(107986001)(5001870100001);DIR:OUT;SFP:1101;SCL:1;SRVR:CY1PR02MB1333;H:xsj-pvapsmtpgw02;FPR:;SPF:Pass;PTR:xapps1.xilinx.com,unknown-60-100.xilinx.com;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;SN1NAM02FT003;1:8QFWB5LYBTy1QPRSPLVNIbiJ1vuX81hwA+SAqCoMBmA6Nf8nNoPvlqBKQhZj9ljUH32AWLuAKpZEgfLkRgaibe5WZqJkgj0PXQKpMJxISBABQSfkuPQyRvGuBwET2hta MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a36f229a-0af0-409f-eba3-08d553ce21c8 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(4608076)(2017052603307)(7153060);SRVR:CY1PR02MB1333; X-Microsoft-Exchange-Diagnostics: 1;CY1PR02MB1333;3:DgzBh5bcFdtb3DgEdrnQHZFU8E2Mc+yR0hn9FibEqdvI0uDkE7dd3a2YW66nz7W87riYRbPI19a4265xgzic4YUQFjO9YIcusLZMmMjTIBYcxUxRz8d5m1iJDcnu4o+lp6wTQgA8J2SH5smNTVq/r2ahxmsARMPRoWV/qSvO9Qzoz+syuHa41K3uZH/Q8dzGlpC74uhknq2YLtJGG4h6lyoI35NX7H/Vt1QMw6nk8bGe3MzSG0xGszYZ38QWEeATX3ODu4xFcf4DpQNkHY3b+697KOAHPMYHgfzfOxy6wNdGvBlp/8yPpGqddiTZ+kY++2w7hqO/x+gpZgh62ksg1yxULjteLWjUROvdVlzT+8M=;25:kOOb3uZAcL3xYSPO0Zc01Xug8hyBfTP3dDJyKt11fYswYOJPQ1cc6wGCh4y3E+T5hbWNiWwwdYZOJPmy8mcoinNJdQw02lG7Dv05GQpVGqN2kUw6lI5rSxgpIGq+RJB6lggIzPkEyba6E/0M7cP2sNhPg6Xau6airVHiiSz/ULN0SPsxsBhYIOYO7EhgwGAvYD0ySPMpJ+coT9eaVTOnGrly5Ltl7C1VysQhBsNtZ7Ijma6k8Ph89w526yy+E+nWh7CwftrSj1/0qotlF96zI8XG8BRt2Xy/m3zTKaRa7zVkyD0Q53hW93QeIKaKmbwr4vkdeTYRFYZQzePoHQfaaA== X-MS-TrafficTypeDiagnostic: CY1PR02MB1333: X-Microsoft-Exchange-Diagnostics: 1;CY1PR02MB1333;31:tAA7uNnj4nDsw+lO59Se2/Q5EgxobH4lG76l/05+Xi4E/9d8IR6rt8jhMgJY2DCfrSoG7jkqriowSN/WdW7lLQhdW7fikQZUwDeIHXhABzG/JRjzPmvaEpaPWntfvBsCgXDD7SRbcNLO3DPnV/AhERi6Jdo31qrfueWzZ/hGScyi0NB8zV2QXfVCYnR/0qn2KfCtue22DSRfbnS5C8jtCVwrO5Gpcdf4WPysUg4OUmI=;20:ckeeYegZZPzbixqKedX7Q5D902oNIB9ZYi3TEnHvt861JBDzrovhY+igNedm9AhHyP8kxR+jcuhLTyPjYPR/WMd+JRY14ocky9VDa767enjuWGO2o0JQONlvupzTrKGuL5sD331go4RUbsmfdNsoDB4pRDhk3E2KVaSC4rt0gfj6gpbigstQFblArFeJQV0kkImp4wmvW3nlBmhB74+tlbqlx8kmJ88zu2NlXqoTFjVyBaBGwPjMJ9OQiRZauLyq5pwWSIRxUk6cbVb5fmx/7Vb7Lt8c3idcE2byGiXumieyIwRLFmCp1FQit0bzalXYl5CwjIUHCZ/KakSpFRL7ZXgl7dZEuEWXwq/HJXlg+avY9lUdSrvbOI0oSO24A9JoPqY0Apo64uLa/oT8/4KIi86ivrwArscXO+/LEdeEOSp/uilcMlR5CNaWtqhD4i5jJNbUuttzHK+t+biDBl0tSz2LG8Si6KAHJ2zfo5A4dfrEkTtQUCvJJGc8X34XIX0m X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192813158149592); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(5005006)(8121501046)(3231023)(944501075)(3002001)(10201501046)(93006095)(93004095)(6055026)(6041268)(20161123558120)(20161123562045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(6072148)(201708071742011);SRVR:CY1PR02MB1333;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:CY1PR02MB1333; X-Microsoft-Exchange-Diagnostics: 1;CY1PR02MB1333;4:QDo6FVJ4GdKZ0IQ0aBab7vNe2XqAZls7Ru7qwMCMtNXXQ0KZy4+22P1xULDMNWu53Q+HlRPGpobTpkRZ5ys+IXU7crRPt0VX92psxufPhWss3SBGNCDHEZpL5DYC32KINgZfGE++VP956qMUp6DasKEvTx6m0cAT3C7zrD7n6oBh9HS3XTHG2uOE3icUtqFrbAUAw6MjaPrQjQuh86D1K70MGH6mvU4nuL478+EJfErSSulhLb5OKLQ/+RQsd+Us4znB5tZlACGu4itfe13fCXzyNoqA+/pHyDX3/uIrdDHiCmZ6eSMMycDl262yBbsb X-Forefront-PRVS: 054231DC40 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY1PR02MB1333;23:V+4bSsbK4Gb6wpK0y2r03m42Sy3siaO1SmpdyZyH4?= =?us-ascii?Q?QqdsY5/GQHcXCtGz2lzIftFSqN/vmUJfSa1Ew4UcNqW6lpZ/Pd5s7MI6LsxF?= =?us-ascii?Q?sM+yp50lYVdesgMWfubAiNardXvVFU2UBkunBJM7/rWdViyKJdw/jtOBeRUF?= =?us-ascii?Q?YT/aM2nZcQdFJgYNxzvJbqbPw5cqAV2pVBjhX4VsfdNY60rzrBtNHAmzsWxA?= =?us-ascii?Q?cuzIXFYqRKnE+fKYaA48mj1wObwnRoNu0qqjnYpWCOqMgviCjZ7/DzR//ndu?= =?us-ascii?Q?0uA/QdT5nFu78cVkqQ5ykh8FadcXbq46+UPC2fPrturz9T8e2YzM64Dpbti+?= =?us-ascii?Q?o0DqxyMZdoFGcxMaKUZEkOFSrzosgRmSyUaZo4DbKtLEVpAYWV7fu2bGVChN?= =?us-ascii?Q?N/ETuIN7SruNHJ83miQ3IRipO5GlTm1W+jdSiHap1Z3uitgXqUvJxMaxe3mF?= =?us-ascii?Q?o7evsHvTC90QEgI+jBe19kduHWHnzToUJgrFbfQjKrQY9mWqqCEC7BY8ClwX?= =?us-ascii?Q?3aFvNXPLvEIHKg5bheKgkwyimDy9Cr88fsdeRw+Ez7shQLfGuUIC6aPJ7/cp?= =?us-ascii?Q?5GSLMIU0xJMq6oz37i+pfM4uQ6/ilMJAlg553EPGxQF9s7SAe3+qtPdtXX2j?= =?us-ascii?Q?jtlviB24DxPbNRVQ+nvuh5KHQr64TTVB3VDG7g/hKYlpvkRDUjZU8OF6/G31?= =?us-ascii?Q?BXS28sJfWz6yMRaC5cVz4lPoTfBFuG9n5t+uv/nGoPNRJjG6xzyw4pnTvVMS?= =?us-ascii?Q?di1huG+iuZ8Bv9MveDz6Tf7HjQLprmFttynlcOcyLZ57XSj4MuIqmnLLE8Q+?= =?us-ascii?Q?hbLt0osDMKhFJAA/CVGO/QpqxsJJoeel47TdJWIHXwo3iuRpAoDZePE0pUfz?= =?us-ascii?Q?RCpr2QmG3VlDB7+jXF/OY/MCsQ3yPNrY5f/tW/qHGw/B8yJbubcYGOkbMYRh?= =?us-ascii?Q?IYer9ZGae7eizjwn0fntzVVE/xvkKfyjFUd/20qCaDNV7PTOHn6N0fnY7+WO?= =?us-ascii?Q?B5JFHdqW55a+934Z+y8wSB3dPLCwnw1YxCC3a9VaQrZqmcDs8TRIzTNKmi+m?= =?us-ascii?Q?J0Jx10=3D?= X-Microsoft-Exchange-Diagnostics: 1;CY1PR02MB1333;6:Bz7kcDUmHBMBq4ujZtQ3w2h/UjRKXO7ZNcs2Z1/tcgesZQ0cJs6LDPpikA6+rARF+DB5S5Hp9L5FuUZ1smgUvIxq5ExQZJV0xk1Lsw39QYzVnobvIqQqgWnnsPcODRFvrvN4j1MXJRypCNJU56m4Z/XVXoo2LzpQ1s6Hawr1u9eVTATW/10lH5kXvc+Rab8Xi1GzIoSIGCbCHOXKzS3GPkVXtbiykDlP5CGHJlLiykO4u72w9KD2xlGff69d3tQtd9LfWKebNouYtT0USgCzPdIhstvB9UT825y62sYhM2sX1ygdMxvrC3u3QQRATILwl6RT+LhR3OaoB/8CIWfauu/5StmM+oTecQumXKeI9oY=;5:S96h6r9Fm9Wvb+22xl0PqFTpp+TdEqy2jHoEJX6B1LzxKs8L0rl1He/RxxUM/HIqS840JMtQeeFkdRf5fqDb94YDK0MMcoW8bbHl3BUpybOZoAY1qMO9mTU7Nqrb2CH7FedtqqAj718NmLHfUg/g9/+fh6M5p2sfW8TN8x7/Ysk=;24:Cj4Qj8j1np0pYHmew6MzGkvkde0smAaq7V2HjS2/wHVWUg52mXWW9o2C9h/lRegWXGh+PasLjH1CbBkfxnXhIaTzBULZK5uXf0bc8rwXv5I=;7:WLQB4wjNToXhtJUwUQXE7ssIXdU+aFqKUn34sPgcndLY+K1WeSa5iCHB/ymZKdHDl3FWE57NoVPTe1DmlErovQx+jC6hxY/m0z8i5gejX0CwkUsU9MKYX3WuMpXAp/7RYmrXZA+gQk8/oOteKCDD4/8OMQ03HZ3OfBw6GAH7PkLx/p9e8C1zR27Dq+3AwTHuDgUQNwMmbLlw9tFqvv97Aszyqv3ksP8Z43EaCT81hw8z4TwAn8/dJymflJUiP7D9 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Jan 2018 23:51:53.5164 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a36f229a-0af0-409f-eba3-08d553ce21c8 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: CY1PR02MB1333 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: 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 | 635 +++++++++++++++++++++++++++++ include/linux/mailbox/zynqmp-ipi-message.h | 24 ++ 4 files changed, 669 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..4258360d --- /dev/null +++ b/drivers/mailbox/zynqmp-ipi-mailbox.c @@ -0,0 +1,635 @@ +// 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) { + /* IPI mailbox has been opened */ + spin_unlock_irqrestore(&pdata->lock, flags); + return -EBUSY; + } + + /* 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