From patchwork Mon Jan 20 16:52:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pankaj Gupta X-Patchwork-Id: 13945029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BE317C02181 for ; Mon, 20 Jan 2025 11:36:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Cc:To: In-Reply-To:References:Message-Id:Content-Transfer-Encoding:Content-Type: Subject:Date:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=UhFXF0ovvZLiB8xgzT78CIaCZf9iQwK2qrOsanZrvOE=; b=yjJM4CufCN+JoUm8XFwXTvqogz k8X81yciCDO+QNIMcPdKfGA4ztg0NuC0P5qNu24UWMjB51bx3ECtSGIe70hCiVIturTszvG9WU1O4 +OXyA2bCixI3sOF9xR4ruBnicoxGO2/XasZmskqic+D3um6Rwf127mnB9phpNufOZxernOSnwMvDu LGn+rtI5IUtY+yUiPEp4LxviX32b3j6CEnvcDdiWOUTdsK7yNSDD5VsII38/LSK7fSaCgBkW2FOT+ +z76G0MNWIfF+GnZCe3YI+uhNESeA2TPMUDLSGhJnLO+exWQkTMFmm3T70FtJl8tYL/VPyLBHZvT9 5817rpPg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tZq4h-00000005SEW-22gQ; Mon, 20 Jan 2025 11:36:11 +0000 Received: from mail-northeuropeazlp170130004.outbound.protection.outlook.com ([2a01:111:f403:c200::4] helo=DUZPR83CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tZpxT-00000005QwS-1ofz for linux-arm-kernel@lists.infradead.org; Mon, 20 Jan 2025 11:28:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=S1XMt1YWdEZ+e268Idyzqfv0FDndXddrtJFdImDUgPZxli2zmMxZizBIIRSWf7B9i/ILdnO3eFqFBHzJgLywCqiPebRmUvSv9JYqnx+g3XCZAwd+Y4phZXnTq9zUjx5raSEJ9wCsGGHYfv8JvaARn/9cBQEqARaAznv2SzePv6O5H/uRIJ4KAp2YZehkDTbg8Gr/q9QcZeUiNVPjYrFom8rFc3T+Lef+6UKBq4PYsu9xDkYtCLyTG6Oe4HpgIcd9zO6GcTO4K9NN4jtVv5XWdkiRlrxLBpQEzL/6yqV1JzY4SV8Y9vRderTFZkKLE+XuNrSyqoC5ycL/1eVPrVUZXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UhFXF0ovvZLiB8xgzT78CIaCZf9iQwK2qrOsanZrvOE=; b=Akky0MY48ifeWHuRb5a83eYuJ0l4yAxvdOZv4SNMoaBB6NKqlwvv5fR2dGyihP96Depave45vVxPmcEXCf2TTknDKRdqUIlDJPi+wk2geZpSmIEn4z6BEcGqshfeeG3GzLhmwx10J0/lIppTWvHQHLpxIGYz/7nKQvQKNCA8t/KQv3/30sGcjzQU/1wVmquYor7rLsNfHtQpYpv58x5BMTSgXGWYKgtgluIp7MZckroLe0NfUIoOdUhDE62jghgCibck0ooM4rjuVxsDJY7f/3kuDXXq3AcmCqbvce8s6UPyXJF443XRO/9qex8NCfbv3Y0z/9xZPldVv1wMWs3ZmA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UhFXF0ovvZLiB8xgzT78CIaCZf9iQwK2qrOsanZrvOE=; b=NVwXfSEEO5+BQmTWXUc6CEEP67EsxofMGAThAncvTll16+LQMlWLj6NshHAOtS/wTzeYnYpzG82nNGW1saklmMQFC22+lSs75L4cuApgyC7V0fsu+rfuMhB5fftCmBtBjdxf3AFskSuwfNnNBWTxsEtEa1JI0rHRSKWuJAaDYXlMMJQya9UpgwL+IaXGKMC6DnrJhCQSEWx5ZnGFKwHjls4rVd9qshLXpewoW6lNFhgx0qrU/PNmAXjefYLfyT7akoKC8gkG/hg6NCj0Cmxd2YNeXrdeReQnJYL/tm95HAUgj2hse3wqeO3LSEBgUWmiBzpEXDalyvPQnZkauY/4HQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from AM9PR04MB8604.eurprd04.prod.outlook.com (2603:10a6:20b:43b::21) by DBAPR04MB7302.eurprd04.prod.outlook.com (2603:10a6:10:1a5::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8356.20; Mon, 20 Jan 2025 11:28:39 +0000 Received: from AM9PR04MB8604.eurprd04.prod.outlook.com ([fe80::e751:223e:aa3d:5827]) by AM9PR04MB8604.eurprd04.prod.outlook.com ([fe80::e751:223e:aa3d:5827%5]) with mapi id 15.20.8356.020; Mon, 20 Jan 2025 11:28:39 +0000 From: Pankaj Gupta Date: Mon, 20 Jan 2025 22:22:23 +0530 Subject: [PATCH v12 5/5] firmware: imx: adds miscdev Message-Id: <20250120-imx-se-if-v12-5-c5ec9754570c@nxp.com> References: <20250120-imx-se-if-v12-0-c5ec9754570c@nxp.com> In-Reply-To: <20250120-imx-se-if-v12-0-c5ec9754570c@nxp.com> To: Jonathan Corbet , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Pankaj Gupta Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737391948; l=39438; i=pankaj.gupta@nxp.com; s=20240523; h=from:subject:message-id; bh=MQVmDOIxOfwa/7y0YUsUX7VmxtUntkwSz1875bnIo0I=; b=30rOkBcI87oSbZGIUgW9t6U67K/7rlL4oKsRi3R29yj6rS0o7kj+4rQ354I5esYS2s/8mvs5h YEn5/LQ07oyAg4PqVZamdEH0DS/Xd/ee38R5HG7l14EWaNxDI+Er/Lj X-Developer-Key: i=pankaj.gupta@nxp.com; a=ed25519; pk=OA0pBQoupy5lV0XfKzD8B0OOBVB6tpAoIf+0x1bYGRg= X-ClientProxiedBy: SI2PR01CA0051.apcprd01.prod.exchangelabs.com (2603:1096:4:193::6) To AM9PR04MB8604.eurprd04.prod.outlook.com (2603:10a6:20b:43b::21) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM9PR04MB8604:EE_|DBAPR04MB7302:EE_ X-MS-Office365-Filtering-Correlation-Id: 4e5b1ef4-c22f-4432-8d67-08dd39459668 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|52116014|366016|1800799024|13003099007|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?f6Ype41j2FVT1+HNZfnTGOeiD4mo4F5?= =?utf-8?q?WZjaaeGERSsZid7+YRH1PTnapcbFWhol6tHZELqD9QzKWR3cqDnRDnG2BLJzVgKTo?= =?utf-8?q?/w1HYVx8f+p9zFMNgNB9jmg0nlHkcXhrGsbC82A47jxXSxSt90I8apH7qoomXc76m?= =?utf-8?q?dp+t8z8oM1Z/FiTtfN2Yg4ya4KC9bH53TZkCk4c3Tb+XdflLDQnLgEnz5Ah5DGbp+?= =?utf-8?q?DGnlEDnaz7ICHsWvFAv9MhflZb8uEf/YeG1CTIykW8wPR7nJMP1Yws4lciV2Z3v+s?= =?utf-8?q?gbNeBWpoyJFxNsc1Dn4qJ8MejMUxfNnk9C9JuV3J4IJaC1y0XPCjjW0i9p6zu8zVi?= =?utf-8?q?MjWOwIrpaDSm11zdskkXJJZHB+Z74n0aN+LTAUsYB9KJHH+6DohNDFJKwcUWqh3nB?= =?utf-8?q?FmlekMzDg0sr65vYvPRf5ZJoNqboWppi+rXG2mKlytvJ6jVmgAxPz/WAyUajUvOyt?= =?utf-8?q?oLNBDKmCXrMulTHJzfWNeG9NggWohncr0kUPQFAP0gQXgyVGtJFaDtV+aafcK0Ozv?= =?utf-8?q?mdg6Y+F7eKD8ol8w67blr/ZcF3V04POF3P3ld/Pzf4eg6Oefak6edjDLyX7a9USkE?= =?utf-8?q?+q0+AT500FPyvyJsVZSywvMuX61DVKac5o+HV1V4G/ZskRVLKMC8MiheoE9zqeUUP?= =?utf-8?q?0ZNR56TPUbZHsWggdpy5yK7qBFwLTyfr/Earh7RTGGI/9z5DQe1Q9KwkjYmv2eqOe?= =?utf-8?q?F52rSIvLADnuufssmAXxc32LR24+v9Y+buKSrlWiPgYeE+99rW1/XT1qaQazV8lHy?= =?utf-8?q?5l/2zuF30dVVaEbiI4vh6R0dKjo3Re+08IXPWbQIfVpeQmPFDstHm03GAICQmlcQd?= =?utf-8?q?DlccZGmiT6WHxasekZX0R64/lmrN1FQuU2hwtOIwnx4NEUEVTPqpnT3TM1tOneHeN?= =?utf-8?q?pIMhIWfBlC27u7KZs9yyRscQ5n2Q5yzqchwnFquLYKiIsFp/drNm6BDNPWv2qkXjk?= =?utf-8?q?ydmtvl3Kr9zdCJsDArKGxdmIIB12MT2B0VmpynphsVkVVLpLP8lvLXpKI+eCyTxrx?= =?utf-8?q?Fe4ziZmZ8a56O6P5qeFz1dOhJmVm7O7NJ5vzHr5AVudPjjE0SpVRUZ0H5BxaSw7S/?= =?utf-8?q?w3D0uuzoU/TmfvTESt2+78xk8Hb3arsodcXW43QtmYy/aPhR47SI2t12FhAWnAuSa?= =?utf-8?q?JER7V18nithSCQvrvOO/IWvv7psd6OC+P07Yfw+pFSWoV6NjD2IhnMh+HrtQM/kEK?= =?utf-8?q?CZjjUuY4zTN4RSDUS/kI4EkoUBV00aMQac1dI6DZwpCvStUWJQWypnQNldfRF1Nxz?= =?utf-8?q?CtiGP7pBM99UeHLFmhK4r3kmfT/V/7A6nMb35gBgCB0/zwNDF+OxVmR2mB9weuGnp?= =?utf-8?q?90sWW5XhiBUPGc8lt8KNJOsAeRAPjq4SVGxyPbve8SAdwo+yycTzDylvH4mdpBuCY?= =?utf-8?q?00PNXWz1n0h0fr9/Jjawrm17R0aLBXQaw=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM9PR04MB8604.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(52116014)(366016)(1800799024)(13003099007)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?gyw/90HdDSIHMwDVvVs8aY3QndG3?= =?utf-8?q?3+aeIfCuRTKYbZ67gwe1QdUkeYxzrIOHUFjkSb2XZ1qzUF8LbWFoRTrYjd77RSaz4?= =?utf-8?q?7LCY/hhOAbnwt4iUtX2iGIcph2Ipz8LUCMfM7XLUQNnQmGQ9h2cjzeUo56N/jdCKq?= =?utf-8?q?7d2RhXhGECDWs7sYEuCz8brSue7hw5YLoeCN/2J8ypkJ3+Dh/xSv/Lj9rLza4GRN+?= =?utf-8?q?WC4cyqpKOshSCEDc+eMOGLycrAY2fkfYYgWsiXjHrUZQwLvQtkoaFEEeyEt8MD0Bk?= =?utf-8?q?IIk229ye/9i3Ul1rJkFWbfr1+8cB59DcRDucrXbyGw057yPFftaQIgLY5oc6nKk9G?= =?utf-8?q?iQsTzwp7wDrzjL4PjmJFT1YADSvV9w9lu3Q7FDPmFgIsdpioVDyoMnWPaL324D1zh?= =?utf-8?q?NMs+uOMZNnUXmVflu/wA3+diON8et04ish6Cu/XOljvqaKrPZLmELSAc2T9uKsCWf?= =?utf-8?q?IeUAdtAlRINVG9Usr9OdsYOsNApKzfGRYqcH+Yg6KobAOP41wi0F23Tj0dwIDgdPE?= =?utf-8?q?Acl8BzTYwdUQuELXyLqKpU+UFb4gCx6v5UmB05rfXktGiY8iJplw2rH4RAHqMUBj6?= =?utf-8?q?ky/XTuDZt23LDrklzvcZgalGmLTavGhZnNl9jPC2sCko+ynYB1yJKQBDja18xH+wr?= =?utf-8?q?vK/y8x8iY/BTRfghAsHjG5vqZhCmmOC/XSdj3X6sgzifnGqyiD33KeWiOrgZfmpAb?= =?utf-8?q?C7zpv8szq5eKhUaAtY6nu6VHDpWWqK2GiotIVnKzWgkznAiO2WALcSbsS2n0aaoWJ?= =?utf-8?q?d0bHGMX3Yn492Q/HbC7cB9vX2jz2fdckBx+bsDZJo2Be98n3VH0CXUXxfFOdW9Mnh?= =?utf-8?q?f2Z+Eh8mAtD0VF3ULSdJaxewtDHBnM1fYazMwOSasBQ0fJDgMxcTMoXxwH4YlKCcD?= =?utf-8?q?KL7ujOIMPRd4Xe4trgy7iWSNOFxlvN64wD5E+sqCtXK7F/OTybenVKQfashUafpja?= =?utf-8?q?AAYuWMl2a+PZe1WOJX0NP8gkFVo6hJUJvqzTV2DMi+S7QdjEREFzsAKPlNKZgSrXj?= =?utf-8?q?j3uFQaPWObr2o1e1MV/+HKcaljlr3wG6IB/NrvGAqS6HQxQfIqLzaA6XEM8hOIm1q?= =?utf-8?q?FjfBacGek6C+e4a2JGGnQgmGXkZJTvBEFsX2Vxe7dzIAH2IjD68Ixz1WzSzOwDfs/?= =?utf-8?q?B+eJ+wsJLveuExO8CpuNzpIAa+z02y3eZ4JTl8ho71ouGaTFeJxAW9S1t3kXCh1Zf?= =?utf-8?q?+T83SjefwURuGK64Lm4H3ewDhL+yXf3mLAklEPsyCSk6A6gWahEEpQrAOHBgE2gdu?= =?utf-8?q?mJlj+j7T+BOl578dPMVu36rzCIfwlBSwWr0t3+QAnnKQbCOoHTCshtv5i3asuworL?= =?utf-8?q?9ukM+iXZ9p4KF3c5vLUp4ClQo+ShNI3se2U3M03KDC5t6rBRAbKDD0j4Bi8r08ZVF?= =?utf-8?q?960ZeDdidxkoVblZUtFWyqT9oVehjObI0/zg27qWvZistDirgo3K8Ildyvxf9uaEJ?= =?utf-8?q?l8dFa5XZkWQyxq0HwMxidZ215CDK0fantLWJqJMmkTKpW59XAK1hdmJB/nx5NtONv?= =?utf-8?q?raEc5oLMdxwa?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4e5b1ef4-c22f-4432-8d67-08dd39459668 X-MS-Exchange-CrossTenant-AuthSource: AM9PR04MB8604.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2025 11:28:39.8477 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: I/gCtq8BHeaZZayVSvI6eP1Ewr0iIgUFKgYITBII+JhP4b9Co+38KuoBxhfNlseTQPCkmi5SPgI6J5eBzXldNA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR04MB7302 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250120_032843_779644_BB3ED416 X-CRM114-Status: GOOD ( 21.00 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Adds the driver for communication interface to secure-enclave, for exchanging messages with NXP secure enclave HW IP(s) like EdgeLock Enclave from: - User-Space Applications via character driver. ABI documentation for the NXP secure-enclave driver. User-space library using this driver: - i.MX Secure Enclave library: -- URL: https://github.com/nxp-imx/imx-secure-enclave.git, - i.MX Secure Middle-Ware: -- URL: https://github.com/nxp-imx/imx-smw.git Signed-off-by: Pankaj Gupta --- Documentation/ABI/testing/se-cdev | 43 ++ drivers/firmware/imx/ele_base_msg.c | 8 +- drivers/firmware/imx/ele_common.c | 87 ++-- drivers/firmware/imx/ele_common.h | 6 +- drivers/firmware/imx/se_ctrl.c | 790 +++++++++++++++++++++++++++++++++++- drivers/firmware/imx/se_ctrl.h | 42 ++ include/uapi/linux/se_ioctl.h | 101 +++++ 7 files changed, 1042 insertions(+), 35 deletions(-) diff --git a/Documentation/ABI/testing/se-cdev b/Documentation/ABI/testing/se-cdev new file mode 100644 index 000000000000..3451c909ccc4 --- /dev/null +++ b/Documentation/ABI/testing/se-cdev @@ -0,0 +1,43 @@ +What: /dev/_mu[0-9]+_ch[0-9]+ +Date: May 2024 +KernelVersion: 6.8 +Contact: linux-imx@nxp.com, pankaj.gupta@nxp.com +Description: + NXP offers multiple hardware IP(s) for secure enclaves like EdgeLock- + Enclave(ELE), SECO. The character device file descriptors + /dev/_mu*_ch* are the interface between userspace NXP's secure- + enclave shared library and the kernel driver. + + The ioctl(2)-based ABI is defined and documented in + [include]. + ioctl(s) are used primarily for: + - shared memory management + - allocation of I/O buffers + - getting mu info + - setting a dev-ctx as receiver to receive all the commands from FW + - getting SoC info + - send command and receive command response + + The following file operations are supported: + + open(2) + Currently the only useful flags are O_RDWR. + + read(2) + Every read() from the opened character device context is waiting on + wait_event_interruptible, that gets set by the registered mailbox callback + function, indicating a message received from the firmware on message- + unit. + + write(2) + Every write() to the opened character device context needs to acquire + mailbox_lock before sending message on to the message unit. + + close(2) + Stops and frees up the I/O contexts that were associated + with the file descriptor. + +Users: https://github.com/nxp-imx/imx-secure-enclave.git, + https://github.com/nxp-imx/imx-smw.git + crypto/skcipher, + drivers/nvmem/imx-ocotp-ele.c diff --git a/drivers/firmware/imx/ele_base_msg.c b/drivers/firmware/imx/ele_base_msg.c index aa00e95d6aee..ff9a39530fa9 100644 --- a/drivers/firmware/imx/ele_base_msg.c +++ b/drivers/firmware/imx/ele_base_msg.c @@ -67,7 +67,7 @@ int ele_get_info(struct se_if_priv *priv, struct ele_dev_info *s_info) tx_msg->data[0] = upper_32_bits(get_info_addr); tx_msg->data[1] = lower_32_bits(get_info_addr); tx_msg->data[2] = sizeof(*s_info); - ret = ele_msg_send_rcv(priv, + ret = ele_msg_send_rcv(priv->priv_dev_ctx, tx_msg, ELE_GET_INFO_REQ_MSG_SZ, rx_msg, @@ -139,7 +139,7 @@ int ele_ping(struct se_if_priv *priv) goto exit; } - ret = ele_msg_send_rcv(priv, + ret = ele_msg_send_rcv(priv->priv_dev_ctx, tx_msg, ELE_PING_REQ_SZ, rx_msg, @@ -194,7 +194,7 @@ int ele_service_swap(struct se_if_priv *priv, tx_msg->data[3] = lower_32_bits(addr); tx_msg->data[4] = se_add_msg_crc((uint32_t *)&tx_msg[0], ELE_SERVICE_SWAP_REQ_MSG_SZ); - ret = ele_msg_send_rcv(priv, + ret = ele_msg_send_rcv(priv->priv_dev_ctx, tx_msg, ELE_SERVICE_SWAP_REQ_MSG_SZ, rx_msg, @@ -255,7 +255,7 @@ int ele_fw_authenticate(struct se_if_priv *priv, phys_addr_t addr) tx_msg->data[0] = lower_32_bits(addr); tx_msg->data[2] = addr; - ret = ele_msg_send_rcv(priv, + ret = ele_msg_send_rcv(priv->priv_dev_ctx, tx_msg, ELE_FW_AUTH_REQ_SZ, rx_msg, diff --git a/drivers/firmware/imx/ele_common.c b/drivers/firmware/imx/ele_common.c index 67d1fa761172..2e14aec3f2e7 100644 --- a/drivers/firmware/imx/ele_common.c +++ b/drivers/firmware/imx/ele_common.c @@ -6,6 +6,8 @@ #include "ele_base_msg.h" #include "ele_common.h" +#define SE_RCV_MSG_TIMEOUT 120000 + u32 se_add_msg_crc(u32 *msg, u32 msg_len) { u32 nb_words = msg_len / (u32)sizeof(u32); @@ -18,38 +20,62 @@ u32 se_add_msg_crc(u32 *msg, u32 msg_len) return crc; } -int ele_msg_rcv(struct se_if_priv *priv, +int ele_msg_rcv(struct se_if_device_ctx *dev_ctx, struct se_clbk_handle *se_clbk_hdl) { - int err = 0; + struct se_if_priv *priv = dev_ctx->priv; + bool wait_timeout_enabled = true; + unsigned int wait; + int err; do { - /* If callback is executed before entrying to wait state, - * it will immediately come out after entering the wait state, - * but completion_done(&se_clbk_hdl->done), will return false - * after exiting the wait state, with err = 0. - */ - err = wait_for_completion_interruptible(&se_clbk_hdl->done); + if (priv->cmd_receiver_clbk_hdl.dev_ctx == dev_ctx) { + /* For NVM-D that are slaves of SE-FW, are waiting indefinitly + * to receive the command from SE-FW. + */ + wait_timeout_enabled = false; + + /* If callback is executed before entrying to wait state, + * it will immediately come out after entering the wait state, + * but completion_done(&se_clbk_hdl->done), will return false + * after exiting the wait state, with err = 0. + */ + err = wait_for_completion_interruptible(&se_clbk_hdl->done); + } else { + /* FW must send the message response to application in a finite + * time. + */ + wait = msecs_to_jiffies(SE_RCV_MSG_TIMEOUT); + err = wait_for_completion_interruptible_timeout(&se_clbk_hdl->done, wait); + } if (err == -ERESTARTSYS) { - if (priv->waiting_rsp_clbk_hdl.rx_msg) { + if (priv->waiting_rsp_clbk_hdl.dev_ctx) { priv->waiting_rsp_clbk_hdl.signal_rcvd = true; continue; } - dev_err(priv->dev, - "Err[0x%x]:Interrupted by signal.\n", - err); err = -EINTR; break; } - } while (err != 0); + if (err == 0) { + if (wait_timeout_enabled) { + err = -ETIMEDOUT; + dev_err(priv->dev, + "Fatal Error: SE interface: %s%d, hangs indefinitely.\n", + get_se_if_name(priv->if_defs->se_if_type), + priv->if_defs->se_instance_id); + } + break; + } + } while (err < 0); - return err ? err : se_clbk_hdl->rx_msg_sz; + return (err >= 0) ? se_clbk_hdl->rx_msg_sz : err; } -int ele_msg_send(struct se_if_priv *priv, +int ele_msg_send(struct se_if_device_ctx *dev_ctx, void *tx_msg, int tx_msg_sz) { + struct se_if_priv *priv = dev_ctx->priv; struct se_msg_hdr *header; int err; @@ -62,7 +88,8 @@ int ele_msg_send(struct se_if_priv *priv, if (header->size << 2 != tx_msg_sz) { err = -EINVAL; dev_err(priv->dev, - "User buf hdr: 0x%x, sz mismatced with input-sz (%d != %d).", + "%s: User buf hdr: 0x%x, sz mismatced with input-sz (%d != %d).", + dev_ctx->devname, *(u32 *)header, header->size << 2, tx_msg_sz); goto exit; @@ -70,7 +97,9 @@ int ele_msg_send(struct se_if_priv *priv, err = mbox_send_message(priv->tx_chan, tx_msg); if (err < 0) { - dev_err(priv->dev, "Error: mbox_send_message failure.\n"); + dev_err(priv->dev, + "%s: Error: mbox_send_message failure.", + dev_ctx->devname); return err; } err = tx_msg_sz; @@ -80,32 +109,36 @@ int ele_msg_send(struct se_if_priv *priv, } /* API used for send/receive blocking call. */ -int ele_msg_send_rcv(struct se_if_priv *priv, +int ele_msg_send_rcv(struct se_if_device_ctx *dev_ctx, void *tx_msg, int tx_msg_sz, void *rx_msg, int exp_rx_msg_sz) { int err; + struct se_if_priv *priv = dev_ctx->priv; guard(mutex)(&priv->se_if_cmd_lock); + priv->waiting_rsp_clbk_hdl.dev_ctx = dev_ctx; priv->waiting_rsp_clbk_hdl.rx_msg_sz = exp_rx_msg_sz; priv->waiting_rsp_clbk_hdl.rx_msg = rx_msg; - err = ele_msg_send(priv, tx_msg, tx_msg_sz); + err = ele_msg_send(dev_ctx, tx_msg, tx_msg_sz); if (err < 0) goto exit; - err = ele_msg_rcv(priv, &priv->waiting_rsp_clbk_hdl); + err = ele_msg_rcv(dev_ctx, &priv->waiting_rsp_clbk_hdl); if (priv->waiting_rsp_clbk_hdl.signal_rcvd) { err = -EINTR; priv->waiting_rsp_clbk_hdl.signal_rcvd = false; dev_err(priv->dev, - "Err[0x%x]:Interrupted by signal.\n", + "%s: Err[0x%x]:Interrupted by signal.\n", + dev_ctx->devname, err); } + priv->waiting_rsp_clbk_hdl.dev_ctx = NULL; exit: return err; @@ -152,7 +185,8 @@ void se_if_rx_callback(struct mbox_client *mbox_cl, void *msg) if (header->tag == priv->if_defs->cmd_tag) { se_clbk_hdl = &priv->cmd_receiver_clbk_hdl; dev_dbg(dev, - "Selecting cmd receiver for mesg header:0x%x.", + "Selecting cmd receiver:%s for mesg header:0x%x.", + se_clbk_hdl->dev_ctx->devname, *(u32 *) header); /* Pre-allocated buffer of MAX_NVM_MSG_LEN @@ -161,7 +195,8 @@ void se_if_rx_callback(struct mbox_client *mbox_cl, void *msg) */ if (rx_msg_sz > MAX_NVM_MSG_LEN) { dev_err(dev, - "CMD-RCVER NVM: hdr(0x%x) with different sz(%d != %d).\n", + "%s: CMD-RCVER NVM: hdr(0x%x) with different sz(%d != %d).\n", + se_clbk_hdl->dev_ctx->devname, *(u32 *) header, rx_msg_sz, se_clbk_hdl->rx_msg_sz); @@ -172,13 +207,15 @@ void se_if_rx_callback(struct mbox_client *mbox_cl, void *msg) } else if (header->tag == priv->if_defs->rsp_tag) { se_clbk_hdl = &priv->waiting_rsp_clbk_hdl; dev_dbg(dev, - "Selecting resp waiter for mesg header:0x%x.", + "Selecting resp waiter:%s for mesg header:0x%x.", + se_clbk_hdl->dev_ctx->devname, *(u32 *) header); if (rx_msg_sz != se_clbk_hdl->rx_msg_sz && !exception_for_size(priv, header)) { dev_err(dev, - "Rsp to CMD: hdr(0x%x) with different sz(%d != %d).\n", + "%s: Rsp to CMD: hdr(0x%x) with different sz(%d != %d).\n", + se_clbk_hdl->dev_ctx->devname, *(u32 *) header, rx_msg_sz, se_clbk_hdl->rx_msg_sz); diff --git a/drivers/firmware/imx/ele_common.h b/drivers/firmware/imx/ele_common.h index 3d8b6f83fb9d..9bded800c103 100644 --- a/drivers/firmware/imx/ele_common.h +++ b/drivers/firmware/imx/ele_common.h @@ -14,12 +14,12 @@ #define IMX_ELE_FW_DIR "imx/ele/" uint32_t se_add_msg_crc(uint32_t *msg, uint32_t msg_len); -int ele_msg_rcv(struct se_if_priv *priv, +int ele_msg_rcv(struct se_if_device_ctx *dev_ctx, struct se_clbk_handle *se_clbk_hdl); -int ele_msg_send(struct se_if_priv *priv, +int ele_msg_send(struct se_if_device_ctx *dev_ctx, void *tx_msg, int tx_msg_sz); -int ele_msg_send_rcv(struct se_if_priv *priv, +int ele_msg_send_rcv(struct se_if_device_ctx *dev_ctx, void *tx_msg, int tx_msg_sz, void *rx_msg, diff --git a/drivers/firmware/imx/se_ctrl.c b/drivers/firmware/imx/se_ctrl.c index 7f6b9199e6ac..b597ad0398f6 100644 --- a/drivers/firmware/imx/se_ctrl.c +++ b/drivers/firmware/imx/se_ctrl.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "ele_base_msg.h" #include "ele_common.h" @@ -31,8 +32,6 @@ #define MAX_SOC_INFO_DATA_SZ 256 #define MBOX_TX_NAME "tx" #define MBOX_RX_NAME "rx" -#define SE_TYPE_STR_HSM "hsm" -#define SE_TYPE_ID_HSM 0x2 struct se_fw_img_name { const u8 *prim_fw_nm_in_rfs; @@ -140,6 +139,24 @@ static const struct of_device_id se_match[] = { {}, }; +char *get_se_if_name(u8 se_if_id) +{ + switch (se_if_id) { + case SE_TYPE_ID_DBG: return SE_TYPE_STR_DBG; + case SE_TYPE_ID_HSM: return SE_TYPE_STR_HSM; + } + + return NULL; +} + +static uint32_t get_se_soc_id(struct se_if_priv *priv) +{ + const struct se_if_node_info_list *info_list + = device_get_match_data(priv->dev); + + return info_list->soc_id; +} + static struct se_fw_load_info *get_load_fw_instance(struct se_if_priv *priv) { return &var_se_info.load_fw; @@ -289,6 +306,756 @@ static int se_load_firmware(struct se_if_priv *priv) return ret; } +static int init_se_shared_mem(struct se_if_device_ctx *dev_ctx) +{ + struct se_shared_mem_mgmt_info *se_shared_mem_mgmt = &dev_ctx->se_shared_mem_mgmt; + struct se_if_priv *priv = dev_ctx->priv; + + INIT_LIST_HEAD(&se_shared_mem_mgmt->pending_out); + INIT_LIST_HEAD(&se_shared_mem_mgmt->pending_in); + + /* + * Allocate some memory for data exchanges with S40x. + * This will be used for data not requiring secure memory. + */ + se_shared_mem_mgmt->non_secure_mem.ptr + = dma_alloc_coherent(priv->dev, + MAX_DATA_SIZE_PER_USER, + &se_shared_mem_mgmt->non_secure_mem.dma_addr, + GFP_KERNEL); + if (!se_shared_mem_mgmt->non_secure_mem.ptr) + return -ENOMEM; + + se_shared_mem_mgmt->non_secure_mem.size = MAX_DATA_SIZE_PER_USER; + se_shared_mem_mgmt->non_secure_mem.pos = 0; + + return 0; +} + +static void cleanup_se_shared_mem(struct se_if_device_ctx *dev_ctx) +{ + struct se_shared_mem_mgmt_info *se_shared_mem_mgmt = &dev_ctx->se_shared_mem_mgmt; + struct se_if_priv *priv = dev_ctx->priv; + + /* Unmap secure memory shared buffer. */ + if (se_shared_mem_mgmt->secure_mem.ptr) + devm_iounmap(priv->dev, + (void __iomem *)se_shared_mem_mgmt->secure_mem.ptr); + + se_shared_mem_mgmt->secure_mem.ptr = NULL; + se_shared_mem_mgmt->secure_mem.dma_addr = 0; + se_shared_mem_mgmt->secure_mem.size = 0; + se_shared_mem_mgmt->secure_mem.pos = 0; + + /* Free non-secure shared buffer. */ + dma_free_coherent(priv->dev, MAX_DATA_SIZE_PER_USER, + se_shared_mem_mgmt->non_secure_mem.ptr, + se_shared_mem_mgmt->non_secure_mem.dma_addr); + + se_shared_mem_mgmt->non_secure_mem.ptr = NULL; + se_shared_mem_mgmt->non_secure_mem.dma_addr = 0; + se_shared_mem_mgmt->non_secure_mem.size = 0; + se_shared_mem_mgmt->non_secure_mem.pos = 0; +} + +/* Need to copy the output data to user-device context. + */ +static int se_dev_ctx_cpy_out_data(struct se_if_device_ctx *dev_ctx) +{ + struct se_shared_mem_mgmt_info *se_shared_mem_mgmt = &dev_ctx->se_shared_mem_mgmt; + struct se_if_priv *priv = dev_ctx->priv; + struct se_buf_desc *b_desc, *temp; + bool do_cpy = true; + + list_for_each_entry_safe(b_desc, temp, &se_shared_mem_mgmt->pending_out, link) { + if (b_desc->usr_buf_ptr && b_desc->shared_buf_ptr && do_cpy) { + + dev_dbg(priv->dev, + "Copying output data to user."); + if (do_cpy && copy_to_user(b_desc->usr_buf_ptr, + b_desc->shared_buf_ptr, + b_desc->size)) { + dev_err(priv->dev, + "Failure copying output data to user."); + do_cpy = false; + } + } + + if (b_desc->shared_buf_ptr) + memset(b_desc->shared_buf_ptr, 0, b_desc->size); + + list_del(&b_desc->link); + kfree(b_desc); + } + + return do_cpy ? 0 : -EFAULT; +} + +/* + * Clean the used Shared Memory space, + * whether its Input Data copied from user buffers, or + * Data received from FW. + */ +static void se_dev_ctx_shared_mem_cleanup(struct se_if_device_ctx *dev_ctx) +{ + struct se_shared_mem_mgmt_info *se_shared_mem_mgmt = &dev_ctx->se_shared_mem_mgmt; + struct list_head *pending_lists[] = {&se_shared_mem_mgmt->pending_in, + &se_shared_mem_mgmt->pending_out}; + struct se_buf_desc *b_desc, *temp; + int i; + + for (i = 0; i < 2; i++) { + list_for_each_entry_safe(b_desc, temp, + pending_lists[i], link) { + + if (b_desc->shared_buf_ptr) + memset(b_desc->shared_buf_ptr, 0, b_desc->size); + + list_del(&b_desc->link); + kfree(b_desc); + } + } + se_shared_mem_mgmt->secure_mem.pos = 0; + se_shared_mem_mgmt->non_secure_mem.pos = 0; +} + +static int add_b_desc_to_pending_list(void *shared_ptr_with_pos, + struct se_ioctl_setup_iobuf *io, + struct se_if_device_ctx *dev_ctx) +{ + struct se_shared_mem_mgmt_info *se_shared_mem_mgmt = &dev_ctx->se_shared_mem_mgmt; + struct se_buf_desc *b_desc = NULL; + + b_desc = kzalloc(sizeof(*b_desc), GFP_KERNEL); + if (!b_desc) + return -ENOMEM; + + b_desc->shared_buf_ptr = shared_ptr_with_pos; + b_desc->usr_buf_ptr = io->user_buf; + b_desc->size = io->length; + + if (io->flags & SE_IO_BUF_FLAGS_IS_INPUT) { + /* + * buffer is input: + * add an entry in the "pending input buffers" list so + * that copied data can be cleaned from shared memory + * later. + */ + list_add_tail(&b_desc->link, &se_shared_mem_mgmt->pending_in); + } else { + /* + * buffer is output: + * add an entry in the "pending out buffers" list so data + * can be copied to user space when receiving Secure-Enclave + * response. + */ + list_add_tail(&b_desc->link, &se_shared_mem_mgmt->pending_out); + } + + return 0; +} + +/* interface for managed res to unregister a character device */ +static void if_misc_deregister(void *miscdevice) +{ + misc_deregister(miscdevice); +} + +static int init_device_context(struct se_if_priv *priv, int ch_id, + struct se_if_device_ctx **new_dev_ctx, + const struct file_operations *se_if_fops) +{ + struct se_if_device_ctx *dev_ctx; + int ret = 0; + + if (ch_id) + dev_ctx = kzalloc(sizeof(*dev_ctx), GFP_KERNEL); + else + dev_ctx = devm_kzalloc(priv->dev, sizeof(*dev_ctx), GFP_KERNEL); + + if (!dev_ctx) { + ret = -ENOMEM; + return ret; + } + + dev_ctx->priv = priv; + + if (ch_id) + dev_ctx->devname = kasprintf(GFP_KERNEL, "%s%d_ch%d", + get_se_if_name(priv->if_defs->se_if_type), + priv->if_defs->se_instance_id, + ch_id); + else + dev_ctx->devname = devm_kasprintf(priv->dev, GFP_KERNEL, "%s%d_ch%d", + get_se_if_name(priv->if_defs->se_if_type), + priv->if_defs->se_instance_id, + ch_id); + if (!dev_ctx->devname) { + ret = -ENOMEM; + if (ch_id) + kfree(dev_ctx); + + return ret; + } + + mutex_init(&dev_ctx->fops_lock); + + *new_dev_ctx = dev_ctx; + + if (ch_id) { + list_add_tail(&dev_ctx->link, &priv->dev_ctx_list); + priv->active_devctx_count++; + + ret = init_se_shared_mem(dev_ctx); + if (ret < 0) { + kfree(dev_ctx->devname); + kfree(dev_ctx); + *new_dev_ctx = NULL; + return ret; + } + + return ret; + } + + /* Only for ch_id = 0: + * - register the misc device. + * - add action + */ + dev_ctx->miscdev = devm_kzalloc(priv->dev, sizeof(*dev_ctx->miscdev), GFP_KERNEL); + if (!dev_ctx->miscdev) { + ret = -ENOMEM; + *new_dev_ctx = NULL; + return ret; + } + + dev_ctx->miscdev->name = dev_ctx->devname; + dev_ctx->miscdev->minor = MISC_DYNAMIC_MINOR; + dev_ctx->miscdev->fops = se_if_fops; + dev_ctx->miscdev->parent = priv->dev; + ret = misc_register(dev_ctx->miscdev); + if (ret) { + dev_err(priv->dev, "failed to register misc device %d\n", + ret); + return ret; + } + + ret = devm_add_action(priv->dev, if_misc_deregister, + dev_ctx->miscdev); + if (ret) { + dev_err(priv->dev, + "failed[%d] to add action to the misc-dev\n", + ret); + misc_deregister(dev_ctx->miscdev); + } + + return ret; +} + +static int se_ioctl_cmd_snd_rcv_rsp_handler(struct se_if_device_ctx *dev_ctx, + u64 arg) +{ + struct se_ioctl_cmd_snd_rcv_rsp_info cmd_snd_rcv_rsp_info; + struct se_if_priv *priv = dev_ctx->priv; + struct se_api_msg *tx_msg __free(kfree) = NULL; + struct se_api_msg *rx_msg __free(kfree) = NULL; + int err = 0; + + if (copy_from_user(&cmd_snd_rcv_rsp_info, (u8 __user *)arg, + sizeof(cmd_snd_rcv_rsp_info))) { + dev_err(priv->dev, + "%s: Failed to copy cmd_snd_rcv_rsp_info from user\n", + dev_ctx->devname); + err = -EFAULT; + goto exit; + } + + if (cmd_snd_rcv_rsp_info.tx_buf_sz < SE_MU_HDR_SZ) { + dev_err(priv->dev, + "%s: User buffer too small(%d < %d)\n", + dev_ctx->devname, + cmd_snd_rcv_rsp_info.tx_buf_sz, + SE_MU_HDR_SZ); + err = -ENOSPC; + goto exit; + } + + rx_msg = kzalloc(cmd_snd_rcv_rsp_info.rx_buf_sz, GFP_KERNEL); + if (!rx_msg) { + err = -ENOMEM; + goto exit; + } + + tx_msg = memdup_user(cmd_snd_rcv_rsp_info.tx_buf, + cmd_snd_rcv_rsp_info.tx_buf_sz); + if (IS_ERR(tx_msg)) { + err = PTR_ERR(tx_msg); + goto exit; + } + + if (tx_msg->header.tag != priv->if_defs->cmd_tag) { + err = -EINVAL; + goto exit; + } + + if (tx_msg->header.ver == priv->if_defs->fw_api_ver && + !get_load_fw_instance(priv)->is_fw_loaded) { + err = se_load_firmware(priv); + if (err) { + dev_err(priv->dev, "Could not send the message as FW is not loaded."); + err = -EPERM; + goto exit; + } + } + err = ele_msg_send_rcv(dev_ctx, + tx_msg, + cmd_snd_rcv_rsp_info.tx_buf_sz, + rx_msg, + cmd_snd_rcv_rsp_info.rx_buf_sz); + if (err < 0) + goto exit; + + dev_dbg(priv->dev, + "%s: %s %s\n", + dev_ctx->devname, + __func__, + "message received, start transmit to user"); + + /* We may need to copy the output data to user before + * delivering the completion message. + */ + err = se_dev_ctx_cpy_out_data(dev_ctx); + if (err < 0) + goto exit; + + /* Copy data from the buffer */ + print_hex_dump_debug("to user ", DUMP_PREFIX_OFFSET, 4, 4, + rx_msg, + cmd_snd_rcv_rsp_info.rx_buf_sz, false); + + if (copy_to_user(cmd_snd_rcv_rsp_info.rx_buf, rx_msg, + cmd_snd_rcv_rsp_info.rx_buf_sz)) { + dev_err(priv->dev, + "%s: Failed to copy to user\n", + dev_ctx->devname); + err = -EFAULT; + } + +exit: + se_dev_ctx_shared_mem_cleanup(dev_ctx); + + if (copy_to_user((void __user *)arg, &cmd_snd_rcv_rsp_info, + sizeof(cmd_snd_rcv_rsp_info))) { + dev_err(priv->dev, + "%s: Failed to copy cmd_snd_rcv_rsp_info from user\n", + dev_ctx->devname); + err = -EFAULT; + } + + return err; +} + +static int se_ioctl_get_mu_info(struct se_if_device_ctx *dev_ctx, + u64 arg) +{ + struct se_if_priv *priv = dev_ctx->priv; + struct se_if_node_info *info; + struct se_ioctl_get_if_info if_info; + int err = 0; + + info = container_of(priv->if_defs, typeof(*info), if_defs); + + if_info.se_if_id = 0; + if_info.interrupt_idx = 0; + if_info.tz = 0; + if_info.did = info->se_if_did; + if_info.cmd_tag = priv->if_defs->cmd_tag; + if_info.rsp_tag = priv->if_defs->rsp_tag; + if_info.success_tag = priv->if_defs->success_tag; + if_info.base_api_ver = priv->if_defs->base_api_ver; + if_info.fw_api_ver = priv->if_defs->fw_api_ver; + + dev_dbg(priv->dev, + "%s: info [se_if_id: %d, irq_idx: %d, tz: 0x%x, did: 0x%x]\n", + dev_ctx->devname, + if_info.se_if_id, if_info.interrupt_idx, + if_info.tz, if_info.did); + + if (copy_to_user((u8 __user *)arg, &if_info, sizeof(if_info))) { + dev_err(priv->dev, + "%s: Failed to copy mu info to user\n", + dev_ctx->devname); + err = -EFAULT; + goto exit; + } + +exit: + return err; +} + +/* + * Copy a buffer of data to/from the user and return the address to use in + * messages + */ +static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx, + u64 arg) +{ + struct se_shared_mem *shared_mem = NULL; + struct se_ioctl_setup_iobuf io = {0}; + int err = 0; + u32 pos; + + if (copy_from_user(&io, (u8 __user *)arg, sizeof(io))) { + dev_err(dev_ctx->priv->dev, + "%s: Failed copy iobuf config from user\n", + dev_ctx->devname); + err = -EFAULT; + goto exit; + } + + dev_dbg(dev_ctx->priv->dev, + "%s: io [buf: %p(%d) flag: %x]\n", + dev_ctx->devname, + io.user_buf, io.length, io.flags); + + if (io.length == 0 || !io.user_buf) { + /* + * Accept NULL pointers since some buffers are optional + * in FW commands. In this case we should return 0 as + * pointer to be embedded into the message. + * Skip all data copy part of code below. + */ + io.ele_addr = 0; + goto copy; + } + + /* No specific requirement for this buffer. */ + shared_mem = &dev_ctx->se_shared_mem_mgmt.non_secure_mem; + + /* Check there is enough space in the shared memory. */ + dev_dbg(dev_ctx->priv->dev, + "%s: req_size = %d, max_size= %d, curr_pos = %d", + dev_ctx->devname, + round_up(io.length, 8u), + shared_mem->size, shared_mem->pos); + + if (shared_mem->size < shared_mem->pos || + round_up(io.length, 8u) > (shared_mem->size - shared_mem->pos)) { + dev_err(dev_ctx->priv->dev, + "%s: Not enough space in shared memory\n", + dev_ctx->devname); + err = -ENOMEM; + goto exit; + } + + /* Allocate space in shared memory. 8 bytes aligned. */ + pos = shared_mem->pos; + shared_mem->pos += round_up(io.length, 8u); + io.ele_addr = (u64)shared_mem->dma_addr + pos; + + memset(shared_mem->ptr + pos, 0, io.length); + if ((io.flags & SE_IO_BUF_FLAGS_IS_INPUT) || + (io.flags & SE_IO_BUF_FLAGS_IS_IN_OUT)) { + /* + * buffer is input: + * copy data from user space to this allocated buffer. + */ + if (copy_from_user(shared_mem->ptr + pos, io.user_buf, + io.length)) { + dev_err(dev_ctx->priv->dev, + "%s: Failed copy data to shared memory\n", + dev_ctx->devname); + err = -EFAULT; + goto exit; + } + } + + err = add_b_desc_to_pending_list(shared_mem->ptr + pos, + &io, + dev_ctx); + if (err < 0) + dev_err(dev_ctx->priv->dev, + "%s: Failed to allocate/link b_desc.", + dev_ctx->devname); + +copy: + /* Provide the EdgeLock Enclave address to user space only if success.*/ + if (copy_to_user((u8 __user *)arg, &io, sizeof(io))) { + dev_err(dev_ctx->priv->dev, + "%s: Failed to copy iobuff setup to user.", + dev_ctx->devname); + err = -EFAULT; + goto exit; + } +exit: + return err; +} + +/* IOCTL to provide SoC information */ +static int se_ioctl_get_se_soc_info_handler(struct se_if_device_ctx *dev_ctx, + u64 arg) +{ + struct se_ioctl_get_soc_info soc_info; + int err = -EINVAL; + + soc_info.soc_id = get_se_soc_id(dev_ctx->priv); + soc_info.soc_rev = var_se_info.soc_rev; + + err = (int)copy_to_user((u8 __user *)arg, (u8 *)(&soc_info), sizeof(soc_info)); + if (err) { + dev_err(dev_ctx->priv->dev, + "%s: Failed to copy soc info to user\n", + dev_ctx->devname); + err = -EFAULT; + goto exit; + } + +exit: + return err; +} + +/* + * File operations for user-space + */ + +/* Write a message to the MU. */ +static ssize_t se_if_fops_write(struct file *fp, const char __user *buf, + size_t size, loff_t *ppos) +{ + struct se_if_device_ctx *dev_ctx = fp->private_data; + struct se_api_msg *tx_msg __free(kfree) = NULL; + struct se_if_priv *priv = dev_ctx->priv; + int err; + + dev_dbg(priv->dev, + "%s: write from buf (%p)%zu, ppos=%lld\n", + dev_ctx->devname, + buf, size, ((ppos) ? *ppos : 0)); + + if (mutex_lock_interruptible(&dev_ctx->fops_lock)) + return -EBUSY; + + if (dev_ctx != priv->cmd_receiver_clbk_hdl.dev_ctx) { + err = -EINVAL; + goto exit; + } + + if (size < SE_MU_HDR_SZ) { + dev_err(priv->dev, + "%s: User buffer too small(%zu < %d)\n", + dev_ctx->devname, + size, SE_MU_HDR_SZ); + err = -ENOSPC; + goto exit; + } + + tx_msg = memdup_user(buf, size); + if (IS_ERR(tx_msg)) { + err = PTR_ERR(tx_msg); + goto exit; + } + + print_hex_dump_debug("from user ", DUMP_PREFIX_OFFSET, 4, 4, + tx_msg, size, false); + + err = ele_msg_send(dev_ctx, tx_msg, size); + if (err < 0) + goto exit; +exit: + mutex_unlock(&dev_ctx->fops_lock); + return err; +} + +/* + * Read a message from the MU. + * Blocking until a message is available. + */ +static ssize_t se_if_fops_read(struct file *fp, char __user *buf, + size_t size, loff_t *ppos) +{ + struct se_if_device_ctx *dev_ctx = fp->private_data; + struct se_if_priv *priv = dev_ctx->priv; + int err; + + dev_dbg(priv->dev, + "%s: read to buf %p(%zu), ppos=%lld\n", + dev_ctx->devname, + buf, size, ((ppos) ? *ppos : 0)); + + if (mutex_lock_interruptible(&dev_ctx->fops_lock)) + return -EBUSY; + + if (dev_ctx != priv->cmd_receiver_clbk_hdl.dev_ctx) { + err = -EINVAL; + goto exit; + } + + err = ele_msg_rcv(dev_ctx, &priv->cmd_receiver_clbk_hdl); + if (err < 0) { + dev_err(priv->dev, + "%s: Err[0x%x]:Interrupted by signal.\n", + dev_ctx->devname, err); + dev_dbg(priv->dev, + "Current active dev-ctx count = %d.\n", + dev_ctx->priv->active_devctx_count); + goto exit; + } + + /* We may need to copy the output data to user before + * delivering the completion message. + */ + err = se_dev_ctx_cpy_out_data(dev_ctx); + if (err < 0) + goto exit; + + /* Copy data from the buffer */ + print_hex_dump_debug("to user ", DUMP_PREFIX_OFFSET, 4, 4, + priv->cmd_receiver_clbk_hdl.rx_msg, + priv->cmd_receiver_clbk_hdl.rx_msg_sz, + false); + + if (copy_to_user(buf, priv->cmd_receiver_clbk_hdl.rx_msg, + priv->cmd_receiver_clbk_hdl.rx_msg_sz)) { + dev_err(priv->dev, + "%s: Failed to copy to user\n", + dev_ctx->devname); + err = -EFAULT; + } + err = priv->cmd_receiver_clbk_hdl.rx_msg_sz; +exit: + priv->cmd_receiver_clbk_hdl.rx_msg_sz = 0; + + se_dev_ctx_shared_mem_cleanup(dev_ctx); + + mutex_unlock(&dev_ctx->fops_lock); + return err; +} + +/* Open a character device. */ +static int se_if_fops_open(struct inode *nd, struct file *fp) +{ + struct miscdevice *miscdev = fp->private_data; + struct se_if_priv *priv = dev_get_drvdata(miscdev->parent); + struct se_if_device_ctx *misc_dev_ctx = priv->priv_dev_ctx; + struct se_if_device_ctx *dev_ctx; + int err = 0; + + if (mutex_lock_interruptible(&misc_dev_ctx->fops_lock)) + return -EBUSY; + + priv->dev_ctx_mono_count++; + err = init_device_context(priv, + priv->dev_ctx_mono_count ? + priv->dev_ctx_mono_count + : priv->dev_ctx_mono_count++, + &dev_ctx, NULL); + if (err) { + dev_err(priv->dev, + "Failed[0x%x] to create device contexts.\n", + err); + goto exit; + } + + fp->private_data = dev_ctx; + +exit: + mutex_unlock(&misc_dev_ctx->fops_lock); + return err; +} + +/* Close a character device. */ +static int se_if_fops_close(struct inode *nd, struct file *fp) +{ + struct se_if_device_ctx *dev_ctx = fp->private_data; + struct se_if_priv *priv = dev_ctx->priv; + + if (mutex_lock_interruptible(&dev_ctx->fops_lock)) + return -EBUSY; + + /* check if this device was registered as command receiver. */ + if (priv->cmd_receiver_clbk_hdl.dev_ctx == dev_ctx) { + priv->cmd_receiver_clbk_hdl.dev_ctx = NULL; + kfree(priv->cmd_receiver_clbk_hdl.rx_msg); + priv->cmd_receiver_clbk_hdl.rx_msg = NULL; + } + + se_dev_ctx_shared_mem_cleanup(dev_ctx); + cleanup_se_shared_mem(dev_ctx); + + priv->active_devctx_count--; + list_del(&dev_ctx->link); + + mutex_unlock(&dev_ctx->fops_lock); + kfree(dev_ctx->devname); + kfree(dev_ctx); + + return 0; +} + +/* IOCTL entry point of a character device */ +static long se_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) +{ + struct se_if_device_ctx *dev_ctx = fp->private_data; + struct se_if_priv *priv = dev_ctx->priv; + int err; + + /* Prevent race during change of device context */ + if (mutex_lock_interruptible(&dev_ctx->fops_lock)) + return -EBUSY; + + switch (cmd) { + case SE_IOCTL_ENABLE_CMD_RCV: + if (!priv->cmd_receiver_clbk_hdl.dev_ctx) { + if (!priv->cmd_receiver_clbk_hdl.rx_msg) { + priv->cmd_receiver_clbk_hdl.rx_msg + = kzalloc(MAX_NVM_MSG_LEN, + GFP_KERNEL); + if (!priv->cmd_receiver_clbk_hdl.rx_msg) { + err = -ENOMEM; + break; + } + } + priv->cmd_receiver_clbk_hdl.rx_msg_sz = MAX_NVM_MSG_LEN; + priv->cmd_receiver_clbk_hdl.dev_ctx = dev_ctx; + err = 0; + } else { + err = -EBUSY; + } + break; + case SE_IOCTL_GET_MU_INFO: + err = se_ioctl_get_mu_info(dev_ctx, arg); + break; + case SE_IOCTL_SETUP_IOBUF: + err = se_ioctl_setup_iobuf_handler(dev_ctx, arg); + break; + case SE_IOCTL_GET_SOC_INFO: + err = se_ioctl_get_se_soc_info_handler(dev_ctx, arg); + break; + case SE_IOCTL_CMD_SEND_RCV_RSP: + err = se_ioctl_cmd_snd_rcv_rsp_handler(dev_ctx, arg); + break; + default: + err = -EINVAL; + dev_dbg(priv->dev, + "%s: IOCTL %.8x not supported\n", + dev_ctx->devname, + cmd); + } + + mutex_unlock(&dev_ctx->fops_lock); + + return (long)err; +} + +/* Char driver setup */ +static const struct file_operations se_if_fops = { + .open = se_if_fops_open, + .owner = THIS_MODULE, + .release = se_if_fops_close, + .unlocked_ioctl = se_ioctl, + .read = se_if_fops_read, + .write = se_if_fops_write, +}; + /* interface for managed res to free a mailbox channel */ static void if_mbox_free_channel(void *mbox_chan) { @@ -324,6 +1091,7 @@ static int se_if_request_channel(struct device *dev, static void se_if_probe_cleanup(void *plat_dev) { + struct se_if_device_ctx *dev_ctx, *t_dev_ctx; struct platform_device *pdev = plat_dev; struct device *dev = &pdev->dev; struct se_fw_load_info *load_fw; @@ -348,6 +1116,13 @@ static void se_if_probe_cleanup(void *plat_dev) load_fw->imem.buf = NULL; } + if (priv->dev_ctx_mono_count) { + list_for_each_entry_safe(dev_ctx, t_dev_ctx, &priv->dev_ctx_list, link) { + list_del(&dev_ctx->link); + priv->active_devctx_count--; + } + } + /* No need to check, if reserved memory is allocated * before calling for its release. Or clearing the * un-set bit. @@ -431,6 +1206,7 @@ static int se_if_probe(struct platform_device *pdev) goto exit; } } + INIT_LIST_HEAD(&priv->dev_ctx_list); if (info->reserved_dma_ranges) { ret = of_reserved_mem_device_init(dev); @@ -442,6 +1218,14 @@ static int se_if_probe(struct platform_device *pdev) } } + ret = init_device_context(priv, 0, &priv->priv_dev_ctx, &se_if_fops); + if (ret) { + dev_err(dev, + "Failed[0x%x] to create device contexts.\n", + ret); + goto exit; + } + if (info->if_defs.se_if_type == SE_TYPE_ID_HSM) { ret = se_soc_info(priv); if (ret) { @@ -473,7 +1257,7 @@ static int se_if_probe(struct platform_device *pdev) } } dev_info(dev, "i.MX secure-enclave: %s%d interface to firmware, configured.\n", - SE_TYPE_STR_HSM, + get_se_if_name(priv->if_defs->se_if_type), priv->if_defs->se_instance_id); return ret; diff --git a/drivers/firmware/imx/se_ctrl.h b/drivers/firmware/imx/se_ctrl.h index 12c201b503b1..91a2f084b1f4 100644 --- a/drivers/firmware/imx/se_ctrl.h +++ b/drivers/firmware/imx/se_ctrl.h @@ -13,6 +13,7 @@ #define MAX_FW_LOAD_RETRIES 50 #define RES_STATUS(x) FIELD_GET(0x000000ff, x) +#define MAX_DATA_SIZE_PER_USER (65 * 1024) #define MAX_NVM_MSG_LEN (256) #define MESSAGING_VERSION_6 0x6 #define MESSAGING_VERSION_7 0x7 @@ -31,6 +32,7 @@ struct se_clbk_handle { struct completion done; bool signal_rcvd; + struct se_if_device_ctx *dev_ctx; u32 rx_msg_sz; /* Assignment of the rx_msg buffer to held till the * received content as part callback function, is copied. @@ -45,6 +47,40 @@ struct se_imem_buf { u32 state; }; +struct se_buf_desc { + u8 *shared_buf_ptr; + void __user *usr_buf_ptr; + u32 size; + struct list_head link; +}; + +struct se_shared_mem { + dma_addr_t dma_addr; + u32 size; + u32 pos; + u8 *ptr; +}; + +struct se_shared_mem_mgmt_info { + struct list_head pending_in; + struct list_head pending_out; + + struct se_shared_mem secure_mem; + struct se_shared_mem non_secure_mem; +}; + +/* Private struct for each char device instance. */ +struct se_if_device_ctx { + struct se_if_priv *priv; + struct miscdevice *miscdev; + const char *devname; + + struct mutex fops_lock; + + struct se_shared_mem_mgmt_info se_shared_mem_mgmt; + struct list_head link; +}; + /* Header of the messages exchange with the EdgeLock Enclave */ struct se_msg_hdr { u8 ver; @@ -89,6 +125,12 @@ struct se_if_priv { struct gen_pool *mem_pool; const struct se_if_defines *if_defs; + + struct se_if_device_ctx *priv_dev_ctx; + struct list_head dev_ctx_list; + u32 active_devctx_count; + u32 dev_ctx_mono_count; }; +char *get_se_if_name(u8 se_if_id); #endif diff --git a/include/uapi/linux/se_ioctl.h b/include/uapi/linux/se_ioctl.h new file mode 100644 index 000000000000..5cf3d56734f4 --- /dev/null +++ b/include/uapi/linux/se_ioctl.h @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: (GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause*/ +/* + * Copyright 2024 NXP + */ + +#ifndef SE_IOCTL_H +#define SE_IOCTL_H + +#include + +#define SE_TYPE_STR_DBG "dbg" +#define SE_TYPE_STR_HSM "hsm" +#define SE_TYPE_ID_UNKWN 0x0 +#define SE_TYPE_ID_DBG 0x1 +#define SE_TYPE_ID_HSM 0x2 +/* IOCTL definitions. */ + +struct se_ioctl_setup_iobuf { + void __user *user_buf; + __u32 length; + __u32 flags; + __u64 ele_addr; +}; + +struct se_ioctl_shared_mem_cfg { + __u32 base_offset; + __u32 size; +}; + +struct se_ioctl_get_if_info { + __u8 se_if_id; + __u8 interrupt_idx; + __u8 tz; + __u8 did; + __u8 cmd_tag; + __u8 rsp_tag; + __u8 success_tag; + __u8 base_api_ver; + __u8 fw_api_ver; +}; + +struct se_ioctl_cmd_snd_rcv_rsp_info { + __u32 __user *tx_buf; + int tx_buf_sz; + __u32 __user *rx_buf; + int rx_buf_sz; +}; + +struct se_ioctl_get_soc_info { + __u16 soc_id; + __u16 soc_rev; +}; + +/* IO Buffer Flags */ +#define SE_IO_BUF_FLAGS_IS_OUTPUT (0x00u) +#define SE_IO_BUF_FLAGS_IS_INPUT (0x01u) +#define SE_IO_BUF_FLAGS_USE_SEC_MEM (0x02u) +#define SE_IO_BUF_FLAGS_USE_SHORT_ADDR (0x04u) +#define SE_IO_BUF_FLAGS_IS_IN_OUT (0x10u) + +/* IOCTLS */ +#define SE_IOCTL 0x0A /* like MISC_MAJOR. */ + +/* + * ioctl to designated the current fd as logical-reciever. + * This is ioctl is send when the nvm-daemon, a slave to the + * firmware is started by the user. + */ +#define SE_IOCTL_ENABLE_CMD_RCV _IO(SE_IOCTL, 0x01) + +/* + * ioctl to get the buffer allocated from the memory, which is shared + * between kernel and FW. + * Post allocation, the kernel tagged the allocated memory with: + * Output + * Input + * Input-Output + * Short address + * Secure-memory + */ +#define SE_IOCTL_SETUP_IOBUF _IOWR(SE_IOCTL, 0x03, \ + struct se_ioctl_setup_iobuf) + +/* + * ioctl to get the mu information, that is used to exchange message + * with FW, from user-spaced. + */ +#define SE_IOCTL_GET_MU_INFO _IOR(SE_IOCTL, 0x04, \ + struct se_ioctl_get_if_info) +/* + * ioctl to get SoC Info from user-space. + */ +#define SE_IOCTL_GET_SOC_INFO _IOR(SE_IOCTL, 0x06, \ + struct se_ioctl_get_soc_info) + +/* + * ioctl to send command and receive response from user-space. + */ +#define SE_IOCTL_CMD_SEND_RCV_RSP _IOWR(SE_IOCTL, 0x07, \ + struct se_ioctl_cmd_snd_rcv_rsp_info) +#endif