From patchwork Mon Aug 1 17:20:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abdellatif El Khlifi X-Patchwork-Id: 1662682 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LxPzz03Ypz9sGP for ; Tue, 2 Aug 2022 03:21:34 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BA0D7844CF; Mon, 1 Aug 2022 19:21:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 1F5D38449C; Mon, 1 Aug 2022 19:21:26 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 689488449C for ; Mon, 1 Aug 2022 19:21:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=abdellatif.elkhlifi@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 17486139F; Mon, 1 Aug 2022 10:21:23 -0700 (PDT) Received: from e121910.arm.com (unknown [10.57.9.251]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 682273F67D; Mon, 1 Aug 2022 10:21:13 -0700 (PDT) From: Abdellatif El Khlifi To: ilias.apalodimas@linaro.org Cc: jens.wiklander@linaro.org, abdellatif.elkhlifi@arm.com, achin.gupta@arm.com, trini@konsulko.com, u-boot@lists.denx.de, vishnu.banavath@arm.com, xueliang.zhong@arm.com, nd@arm.com, Sudeep Holla Subject: [PATCH v3 1/4] arm64: smccc: add Xn registers support used by SMC calls Date: Mon, 1 Aug 2022 18:20:50 +0100 Message-Id: <20220801172053.20163-2-abdellatif.elkhlifi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> References: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean use x0-x17 registers in the SMC32/SMC64 calls according to SMCCCv1.2 Signed-off-by: Sudeep Holla Signed-off-by: Abdellatif El Khlifi Cc: Tom Rini Cc: Ilias Apalodimas Cc: Jens Wiklander --- arch/arm/cpu/armv8/smccc-call.S | 53 +++++++++++++++++++++++++++++++++ arch/arm/lib/asm-offsets.c | 13 ++++++++ include/linux/arm-smccc.h | 43 ++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) diff --git a/arch/arm/cpu/armv8/smccc-call.S b/arch/arm/cpu/armv8/smccc-call.S index dc92b28777..ec6f299bc9 100644 --- a/arch/arm/cpu/armv8/smccc-call.S +++ b/arch/arm/cpu/armv8/smccc-call.S @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* * Copyright (c) 2015, Linaro Limited + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi */ #include #include @@ -45,3 +47,54 @@ ENDPROC(__arm_smccc_smc) ENTRY(__arm_smccc_hvc) SMCCC hvc ENDPROC(__arm_smccc_hvc) + +#ifdef CONFIG_ARM64 + + .macro SMCCC_1_2 instr + /* Save `res` and free a GPR that won't be clobbered */ + stp x1, x19, [sp, #-16]! + + /* Ensure `args` won't be clobbered while loading regs in next step */ + mov x19, x0 + + /* Load the registers x0 - x17 from the struct arm_smccc_1_2_regs */ + ldp x0, x1, [x19, #ARM_SMCCC_1_2_REGS_X0_OFFS] + ldp x2, x3, [x19, #ARM_SMCCC_1_2_REGS_X2_OFFS] + ldp x4, x5, [x19, #ARM_SMCCC_1_2_REGS_X4_OFFS] + ldp x6, x7, [x19, #ARM_SMCCC_1_2_REGS_X6_OFFS] + ldp x8, x9, [x19, #ARM_SMCCC_1_2_REGS_X8_OFFS] + ldp x10, x11, [x19, #ARM_SMCCC_1_2_REGS_X10_OFFS] + ldp x12, x13, [x19, #ARM_SMCCC_1_2_REGS_X12_OFFS] + ldp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS] + ldp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS] + + \instr #0 + + /* Load the `res` from the stack */ + ldr x19, [sp] + + /* Store the registers x0 - x17 into the result structure */ + stp x0, x1, [x19, #ARM_SMCCC_1_2_REGS_X0_OFFS] + stp x2, x3, [x19, #ARM_SMCCC_1_2_REGS_X2_OFFS] + stp x4, x5, [x19, #ARM_SMCCC_1_2_REGS_X4_OFFS] + stp x6, x7, [x19, #ARM_SMCCC_1_2_REGS_X6_OFFS] + stp x8, x9, [x19, #ARM_SMCCC_1_2_REGS_X8_OFFS] + stp x10, x11, [x19, #ARM_SMCCC_1_2_REGS_X10_OFFS] + stp x12, x13, [x19, #ARM_SMCCC_1_2_REGS_X12_OFFS] + stp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS] + stp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS] + + /* Restore original x19 */ + ldp xzr, x19, [sp], #16 + ret + .endm + +/* + * void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args, + * struct arm_smccc_1_2_regs *res); + */ +ENTRY(arm_smccc_1_2_smc) + SMCCC_1_2 smc +ENDPROC(arm_smccc_1_2_smc) + +#endif diff --git a/arch/arm/lib/asm-offsets.c b/arch/arm/lib/asm-offsets.c index 22fd541f9a..b6bd1b32b0 100644 --- a/arch/arm/lib/asm-offsets.c +++ b/arch/arm/lib/asm-offsets.c @@ -9,6 +9,8 @@ * generate asm statements containing #defines, * compile this file to assembler, and then extract the * #defines from the assembly-language output. + * + * (C) Copyright 2022 ARM Limited */ #include @@ -117,6 +119,17 @@ int main(void) DEFINE(ARM_SMCCC_RES_X2_OFFS, offsetof(struct arm_smccc_res, a2)); DEFINE(ARM_SMCCC_QUIRK_ID_OFFS, offsetof(struct arm_smccc_quirk, id)); DEFINE(ARM_SMCCC_QUIRK_STATE_OFFS, offsetof(struct arm_smccc_quirk, state)); + #ifdef CONFIG_ARM64 + DEFINE(ARM_SMCCC_1_2_REGS_X0_OFFS, offsetof(struct arm_smccc_1_2_regs, a0)); + DEFINE(ARM_SMCCC_1_2_REGS_X2_OFFS, offsetof(struct arm_smccc_1_2_regs, a2)); + DEFINE(ARM_SMCCC_1_2_REGS_X4_OFFS, offsetof(struct arm_smccc_1_2_regs, a4)); + DEFINE(ARM_SMCCC_1_2_REGS_X6_OFFS, offsetof(struct arm_smccc_1_2_regs, a6)); + DEFINE(ARM_SMCCC_1_2_REGS_X8_OFFS, offsetof(struct arm_smccc_1_2_regs, a8)); + DEFINE(ARM_SMCCC_1_2_REGS_X10_OFFS, offsetof(struct arm_smccc_1_2_regs, a10)); + DEFINE(ARM_SMCCC_1_2_REGS_X12_OFFS, offsetof(struct arm_smccc_1_2_regs, a12)); + DEFINE(ARM_SMCCC_1_2_REGS_X14_OFFS, offsetof(struct arm_smccc_1_2_regs, a14)); + DEFINE(ARM_SMCCC_1_2_REGS_X16_OFFS, offsetof(struct arm_smccc_1_2_regs, a16)); + #endif #endif return 0; diff --git a/include/linux/arm-smccc.h b/include/linux/arm-smccc.h index e1d09884a1..9105031d55 100644 --- a/include/linux/arm-smccc.h +++ b/include/linux/arm-smccc.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* * Copyright (c) 2015, Linaro Limited + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi */ #ifndef __LINUX_ARM_SMCCC_H #define __LINUX_ARM_SMCCC_H @@ -70,6 +72,47 @@ struct arm_smccc_res { unsigned long a3; }; +#ifdef CONFIG_ARM64 +/** + * struct arm_smccc_1_2_regs - Arguments for or Results from SMC call + * @a0-a17 argument values from registers 0 to 17 + */ +struct arm_smccc_1_2_regs { + unsigned long a0; + unsigned long a1; + unsigned long a2; + unsigned long a3; + unsigned long a4; + unsigned long a5; + unsigned long a6; + unsigned long a7; + unsigned long a8; + unsigned long a9; + unsigned long a10; + unsigned long a11; + unsigned long a12; + unsigned long a13; + unsigned long a14; + unsigned long a15; + unsigned long a16; + unsigned long a17; +}; + +/** + * arm_smccc_1_2_smc() - make SMC calls + * @args: arguments passed via struct arm_smccc_1_2_regs + * @res: result values via struct arm_smccc_1_2_regs + * + * This function is used to make SMC calls following SMC Calling Convention + * v1.2 or above. The content of the supplied param are copied from the + * structure to registers prior to the SMC instruction. The return values + * are updated with the content from registers on return from the SMC + * instruction. + */ +asmlinkage void arm_smccc_1_2_smc(const struct arm_smccc_1_2_regs *args, + struct arm_smccc_1_2_regs *res); +#endif + /** * struct arm_smccc_quirk - Contains quirk information * @id: quirk identification From patchwork Mon Aug 1 17:20:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abdellatif El Khlifi X-Patchwork-Id: 1662683 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LxQ081Xbdz9sGP for ; Tue, 2 Aug 2022 03:21:44 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2E492844B5; Mon, 1 Aug 2022 19:21:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id E8BFB844B4; Mon, 1 Aug 2022 19:21:30 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 3CD7C844DE for ; Mon, 1 Aug 2022 19:21:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=abdellatif.elkhlifi@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 124D6139F; Mon, 1 Aug 2022 10:21:28 -0700 (PDT) Received: from e121910.arm.com (unknown [10.57.9.251]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0EE373F67D; Mon, 1 Aug 2022 10:21:22 -0700 (PDT) From: Abdellatif El Khlifi To: ilias.apalodimas@linaro.org Cc: jens.wiklander@linaro.org, abdellatif.elkhlifi@arm.com, achin.gupta@arm.com, trini@konsulko.com, u-boot@lists.denx.de, vishnu.banavath@arm.com, xueliang.zhong@arm.com, nd@arm.com Subject: [PATCH v3 2/4] arm64: smccc: clear the Xn registers after SMC calls Date: Mon, 1 Aug 2022 18:20:51 +0100 Message-Id: <20220801172053.20163-3-abdellatif.elkhlifi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> References: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean set to zero the x0-x17 registers As per the SMCCC v1.2 spec, unused result and scratch registers can leak information after an SMC call. We can mitigate against this risk by returning zero in each register. Signed-off-by: Abdellatif El Khlifi Cc: Tom Rini Cc: Ilias Apalodimas Cc: Jens Wiklander --- arch/arm/cpu/armv8/smccc-call.S | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/arm/cpu/armv8/smccc-call.S b/arch/arm/cpu/armv8/smccc-call.S index ec6f299bc9..8ac3e461e4 100644 --- a/arch/arm/cpu/armv8/smccc-call.S +++ b/arch/arm/cpu/armv8/smccc-call.S @@ -84,6 +84,26 @@ ENDPROC(__arm_smccc_hvc) stp x14, x15, [x19, #ARM_SMCCC_1_2_REGS_X14_OFFS] stp x16, x17, [x19, #ARM_SMCCC_1_2_REGS_X16_OFFS] + /* x0-x17 registers can leak information after an SMC or HVC call. Let's clear them */ + mov x0, xzr + mov x1, xzr + mov x2, xzr + mov x3, xzr + mov x4, xzr + mov x5, xzr + mov x6, xzr + mov x7, xzr + mov x8, xzr + mov x9, xzr + mov x10, xzr + mov x11, xzr + mov x12, xzr + mov x13, xzr + mov x14, xzr + mov x15, xzr + mov x16, xzr + mov x17, xzr + /* Restore original x19 */ ldp xzr, x19, [sp], #16 ret From patchwork Mon Aug 1 17:20:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abdellatif El Khlifi X-Patchwork-Id: 1662684 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LxQ0N3jKpz9sGP for ; Tue, 2 Aug 2022 03:21:56 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9B4E8844E4; Mon, 1 Aug 2022 19:21:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 20BF3844FD; Mon, 1 Aug 2022 19:21:41 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 411C1844DE for ; Mon, 1 Aug 2022 19:21:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=abdellatif.elkhlifi@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3663D139F; Mon, 1 Aug 2022 10:21:34 -0700 (PDT) Received: from e121910.arm.com (unknown [10.57.9.251]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A896A3F67D; Mon, 1 Aug 2022 10:21:27 -0700 (PDT) From: Abdellatif El Khlifi To: ilias.apalodimas@linaro.org Cc: jens.wiklander@linaro.org, abdellatif.elkhlifi@arm.com, achin.gupta@arm.com, trini@konsulko.com, u-boot@lists.denx.de, vishnu.banavath@arm.com, xueliang.zhong@arm.com, nd@arm.com Subject: [PATCH v3 3/4] arm_ffa: introduce Arm FF-A low-level driver Date: Mon, 1 Aug 2022 18:20:52 +0100 Message-Id: <20220801172053.20163-4-abdellatif.elkhlifi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> References: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Add the driver implementing Arm Firmware Framework for Armv8-A v1.0 The Firmware Framework for Arm A-profile processors (FF-A) describes interfaces (ABIs) that standardize communication between the Secure World and Normal World leveraging TrustZone technology. This driver uses 64-bit registers as per SMCCCv1.2 spec and comes on top of the SMCCC layer. The driver provides the FF-A ABIs needed for querying the FF-A framework from the secure world. 32-bit version of the ABIs is supported and 64-bit version of FFA_RXTX_MAP and FFA_MSG_SEND_DIRECT_{REQ, RESP}. In u-boot FF-A design, FF-A is considered as a discoverable bus. The Secure World is considered as one entity to communicate with using the FF-A bus. FF-A communication is handled by one device and one instance (the bus). This FF-A driver takes care of all the interactions between Normal world and Secure World. The driver exports its operations to be used by upper layers. Exported operations: - partition_info_get - sync_send_receive - rxtx_unmap This implementation provides an optional feature to copy the driver data to EFI runtime area. Signed-off-by: Abdellatif El Khlifi Cc: Tom Rini Cc: Ilias Apalodimas Cc: Jens Wiklander --- MAINTAINERS | 6 + common/board_r.c | 7 + drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/arm-ffa/Kconfig | 33 + drivers/arm-ffa/Makefile | 7 + drivers/arm-ffa/arm-ffa-uclass.c | 16 + drivers/arm-ffa/arm_ffa_prv.h | 219 ++++ drivers/arm-ffa/core.c | 1338 ++++++++++++++++++++ drivers/arm-ffa/efi_ffa_runtime_data_mgr.c | 94 ++ include/arm_ffa.h | 132 ++ include/dm/uclass-id.h | 1 + include/uuid.h | 8 + lib/efi_loader/efi_boottime.c | 17 + lib/uuid.c | 65 + 15 files changed, 1946 insertions(+) create mode 100644 drivers/arm-ffa/Kconfig create mode 100644 drivers/arm-ffa/Makefile create mode 100644 drivers/arm-ffa/arm-ffa-uclass.c create mode 100644 drivers/arm-ffa/arm_ffa_prv.h create mode 100644 drivers/arm-ffa/core.c create mode 100644 drivers/arm-ffa/efi_ffa_runtime_data_mgr.c create mode 100644 include/arm_ffa.h diff --git a/MAINTAINERS b/MAINTAINERS index f371d864f2..93a57f4df2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -248,6 +248,12 @@ F: drivers/net/cortina_ni.h F: drivers/net/phy/ca_phy.c F: configs/cortina_presidio-asic-pnand_defconfig +ARM FF-A +M: Abdellatif El Khlifi +S: Maintained +F: drivers/arm-ffa/ +F: include/arm_ffa.h + ARM FREESCALE IMX M: Stefano Babic M: Fabio Estevam diff --git a/common/board_r.c b/common/board_r.c index ed29069d2d..2d3f359b96 100644 --- a/common/board_r.c +++ b/common/board_r.c @@ -63,6 +63,10 @@ #include #include +#ifdef CONFIG_ARM_FFA_TRANSPORT +#include +#endif + DECLARE_GLOBAL_DATA_PTR; ulong monitor_flash_len; @@ -767,6 +771,9 @@ static init_fnc_t init_sequence_r[] = { INIT_FUNC_WATCHDOG_RESET initr_net, #endif +#ifdef CONFIG_ARM_FFA_TRANSPORT + ffa_bus_discover, +#endif #ifdef CONFIG_POST initr_post, #endif diff --git a/drivers/Kconfig b/drivers/Kconfig index 8b6fead351..60b5fd4d9d 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -6,6 +6,8 @@ source "drivers/core/Kconfig" source "drivers/adc/Kconfig" +source "drivers/arm-ffa/Kconfig" + source "drivers/ata/Kconfig" source "drivers/axi/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index eba9940231..dfde371381 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -110,6 +110,7 @@ obj-y += iommu/ obj-y += smem/ obj-y += thermal/ obj-$(CONFIG_TEE) += tee/ +obj-$(CONFIG_ARM_FFA_TRANSPORT) += arm-ffa/ obj-y += axi/ obj-y += ufs/ obj-$(CONFIG_W1) += w1/ diff --git a/drivers/arm-ffa/Kconfig b/drivers/arm-ffa/Kconfig new file mode 100644 index 0000000000..882ffde90f --- /dev/null +++ b/drivers/arm-ffa/Kconfig @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: GPL-2.0 + +config ARM_FFA_TRANSPORT + bool "Enable Arm Firmware Framework for Armv8-A driver" + depends on DM && ARM64 + select ARM_SMCCC + select LIB_UUID + select DEVRES + help + The Firmware Framework for Arm A-profile processors (FF-A) + describes interfaces (ABIs) that standardize communication + between the Secure World and Normal World leveraging TrustZone + technology. + + This driver is based on FF-A specification v1.0 and uses SMC32 + calling convention. + + FF-A specification: + + https://developer.arm.com/documentation/den0077/a/?lang=en + + In u-boot FF-A design, FF-A is considered as a discoverable bus. + The Secure World is considered as one entity to communicate with + using the FF-A bus. + FF-A communication is handled by one device and one instance (the bus). + This FF-A driver takes care of all the interactions between Normal world + and Secure World. + +config ARM_FFA_EFI_RUNTIME_MODE + bool "Enable EFI runtime support for FF-A data and code " + depends on ARM_FFA_TRANSPORT && EFI_LOADER + help + Allows FF-A driver data structures and code to be accessible at EFI runtime diff --git a/drivers/arm-ffa/Makefile b/drivers/arm-ffa/Makefile new file mode 100644 index 0000000000..0b9b0a61b4 --- /dev/null +++ b/drivers/arm-ffa/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# (C) Copyright 2022 Abdellatif El Khlifi +# + +obj-y += arm-ffa-uclass.o core.o +obj-$(CONFIG_ARM_FFA_EFI_RUNTIME_MODE) += efi_ffa_runtime_data_mgr.o diff --git a/drivers/arm-ffa/arm-ffa-uclass.c b/drivers/arm-ffa/arm-ffa-uclass.c new file mode 100644 index 0000000000..7d9695d289 --- /dev/null +++ b/drivers/arm-ffa/arm-ffa-uclass.c @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi + */ + +#include +#include +#include + +DECLARE_GLOBAL_DATA_PTR; + +UCLASS_DRIVER(ffa) = { + .name = "ffa", + .id = UCLASS_FFA, +}; diff --git a/drivers/arm-ffa/arm_ffa_prv.h b/drivers/arm-ffa/arm_ffa_prv.h new file mode 100644 index 0000000000..1133c02400 --- /dev/null +++ b/drivers/arm-ffa/arm_ffa_prv.h @@ -0,0 +1,219 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi + */ + +#ifndef __ARM_FFA_PRV_H +#define __ARM_FFA_PRV_H + +#include +#include +#include +#include + +/* + * This header is private. It is exclusively used by the FF-A driver + */ + +/* FF-A core driver name */ +#define FFA_DRV_NAME "arm_ffa" + +/* FF-A driver version definitions */ + +#define MAJOR_VERSION_MASK GENMASK(30, 16) +#define MINOR_VERSION_MASK GENMASK(15, 0) +#define GET_FFA_MAJOR_VERSION(x) \ + ((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x)))) +#define GET_FFA_MINOR_VERSION(x) \ + ((u16)(FIELD_GET(MINOR_VERSION_MASK, (x)))) +#define PACK_VERSION_INFO(major, minor) \ + (FIELD_PREP(MAJOR_VERSION_MASK, (major)) | \ + FIELD_PREP(MINOR_VERSION_MASK, (minor))) + +#define FFA_MAJOR_VERSION (1) +#define FFA_MINOR_VERSION (0) +#define FFA_VERSION_1_0 \ + PACK_VERSION_INFO(FFA_MAJOR_VERSION, FFA_MINOR_VERSION) + +/* Endpoint ID mask (u-boot endpoint ID) */ + +#define GET_SELF_ENDPOINT_ID_MASK GENMASK(15, 0) +#define GET_SELF_ENDPOINT_ID(x) \ + ((u16)(FIELD_GET(GET_SELF_ENDPOINT_ID_MASK, (x)))) + +#define PREP_SELF_ENDPOINT_ID_MASK GENMASK(31, 16) +#define PREP_SELF_ENDPOINT_ID(x) \ + (FIELD_PREP(PREP_SELF_ENDPOINT_ID_MASK, (x))) + +/* Partition endpoint ID mask (partition with which u-boot communicates with) */ + +#define PREP_PART_ENDPOINT_ID_MASK GENMASK(15, 0) +#define PREP_PART_ENDPOINT_ID(x) \ + (FIELD_PREP(PREP_PART_ENDPOINT_ID_MASK, (x))) + +/* + * Definitions of the Arm FF-A interfaces supported by the Arm FF-A driver + */ + +#define FFA_SMC(calling_convention, func_num) \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention), \ + ARM_SMCCC_OWNER_STANDARD, (func_num)) + +#define FFA_SMC_32(func_num) FFA_SMC(ARM_SMCCC_SMC_32, (func_num)) +#define FFA_SMC_64(func_num) FFA_SMC(ARM_SMCCC_SMC_64, (func_num)) + +#define FFA_VERSION FFA_SMC_32(0x63) +#define FFA_ID_GET FFA_SMC_32(0x69) +#define FFA_FEATURES FFA_SMC_32(0x64) +#define FFA_PARTITION_INFO_GET FFA_SMC_32(0x68) +#define FFA_RXTX_UNMAP FFA_SMC_32(0x67) +#define FFA_RX_RELEASE FFA_SMC_32(0x65) +#define FFA_RUN FFA_SMC_32(0x6D) +#define FFA_ERROR FFA_SMC_32(0x60) +#define FFA_SUCCESS FFA_SMC_32(0x61) +#define FFA_INTERRUPT FFA_SMC_32(0x62) +#define FFA_RXTX_MAP FFA_SMC_64(0x66) +#define FFA_MSG_SEND_DIRECT_REQ FFA_SMC_64(0x6F) +#define FFA_MSG_SEND_DIRECT_RESP FFA_SMC_64(0x70) + +/* The FF-A SMC function definitions */ + +typedef struct arm_smccc_1_2_regs ffa_value_t; +typedef void (*invoke_ffa_fn_t)(ffa_value_t args, ffa_value_t *res); + +/* FF-A error codes */ +#define FFA_ERR_STAT_NOT_SUPPORTED (-1) +#define FFA_ERR_STAT_INVALID_PARAMETERS (-2) +#define FFA_ERR_STAT_NO_MEMORY (-3) +#define FFA_ERR_STAT_BUSY (-4) +#define FFA_ERR_STAT_INTERRUPTED (-5) +#define FFA_ERR_STAT_DENIED (-6) +#define FFA_ERR_STAT_RETRY (-7) +#define FFA_ERR_STAT_ABORTED (-8) + +/* UUID data size */ +#define UUID_SIZE (16) + +/* + * union ffa_partition_uuid - Data union hosting the UUID + * transmitted by FFA_PARTITION_INFO_GET + * @words: data structure giving 32-bit words access to the UUID data + * @bytes: data structure giving byte access to the UUID data + * + * The structure holds little-endian UUID data. + */ +union ffa_partition_uuid { + struct __packed words { + u32 a1; /* w1 */ + u32 a2; /* w2 */ + u32 a3; /* w3 */ + u32 a4; /* w4 */ + } words; + u8 bytes[UUID_SIZE]; +}; + +/** + * struct ffa_features_desc - FF-A functions features + * @func_id: FF-A function + * @field1: features read from register w2 + * @field2: features read from register w3 + * + * Data structure describing the features of the FF-A functions queried by + * FFA_FEATURES + */ +struct ffa_features_desc { + u32 func_id; + u32 field1; + u64 field2; +}; + +/** + * enum ffa_rxtx_buf_sizes - minimum sizes supported + * for the RX/TX buffers + */ +enum ffa_rxtx_buf_sizes { + RXTX_4K, + RXTX_64K, + RXTX_16K +}; + +/* + * Number of the FF-A interfaces features descriptors + * currently only FFA_RXTX_MAP descriptor is supported + */ +#define FFA_FEATURE_DESC_CNT (1) + +/** + * struct ffa_rxtxpair - structure hosting the RX/TX buffers virtual addresses + * @rxbuf: virtual address of the RX buffer + * @txbuf: virtual address of the TX buffer + * + * Data structure hosting the virtual addresses of the mapped RX/TX buffers + * These addresses are used by the FF-A functions that use the RX/TX buffers + */ +struct ffa_rxtxpair { + u64 rxbuf; /* virtual address */ + u64 txbuf; /* virtual address */ +}; + +/** + * struct ffa_partition_desc - the secure partition descriptor + * @info: partition information + * @UUID: UUID + * + * Each partition has its descriptor containing the partitions information and the UUID + */ +struct ffa_partition_desc { + struct ffa_partition_info info; + union ffa_partition_uuid UUID; +}; + +/** + * struct ffa_partitions - descriptors for all secure partitions + * @count: The number of partitions descriptors + * @descs The partitions descriptors table + * + * This data structure contains the partitions descriptors table + */ +struct ffa_partitions { + u32 count; + struct ffa_partition_desc *descs; /* virtual address */ +}; + +/** + * struct ffa_prvdata - the driver private data structure + * + * @dev: The arm_ffa device under u-boot driver model + * @ffa_ops: The driver operations structure + * @fwk_version: FF-A framework version + * @id: u-boot endpoint ID + * @partitions: The partitions descriptors structure + * @pair: The RX/TX buffers pair + * @invoke_ffa_fn: The function executing the FF-A function + * @features: Table of the FF-A functions having features + * + * The driver data structure hosting all resident data. + */ +struct ffa_prvdata { + struct udevice *dev; + struct ffa_bus_ops ffa_ops; + u32 fwk_version; + u16 id; + struct ffa_partitions partitions; + struct ffa_rxtxpair pair; + invoke_ffa_fn_t invoke_ffa_fn; + struct ffa_features_desc features[FFA_FEATURE_DESC_CNT]; +}; + +/** + * ffa_device_get - create, bind and probe the arm_ffa device + */ +int ffa_device_get(void); + +/** + * ffa_bus_prvdata_get - bus driver private data getter + */ +struct ffa_prvdata **ffa_bus_prvdata_get(void); + +#endif diff --git a/drivers/arm-ffa/core.c b/drivers/arm-ffa/core.c new file mode 100644 index 0000000000..40d140fc3e --- /dev/null +++ b/drivers/arm-ffa/core.c @@ -0,0 +1,1338 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi + */ + +#include "arm_ffa_prv.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +DECLARE_GLOBAL_DATA_PTR; + +/** + * The device private data structure containing all the resident + * data read from secure world + */ +__ffa_runtime_data struct ffa_prvdata *ffa_priv_data; + +/* + * Driver functions + */ + +/** + * ffa_remove_device - removes the arm_ffa device + * @dev: the device to be removed + * + * This boot time function makes sure the arm_ffa device is removed + * No need to free the kmalloced data when the device is destroyed. + * It's automatically done by devm management. + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +int ffa_remove_device(struct udevice *dev) +{ + int ret; + + if (!dev) + return -ENODEV; + + ret = device_remove(dev, DM_REMOVE_NORMAL); + if (ret) { + ffa_err("Unable to remove. err:%d\n", ret); + return ret; + } + + ret = device_unbind(dev); + if (ret) { + ffa_err("Unable to unbind. err:%d\n", ret); + return ret; + } + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_device_get - create, bind and probe the arm_ffa device + * + * This boot time function makes sure the arm_ffa device is + * created, bound to this driver, probed and ready to use. + * Arm FF-A transport is implemented through a single u-boot + * device managing the FF-A bus (arm_ffa). + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +int ffa_device_get(void) +{ + int ret; + struct udevice *dev = NULL; + + if (ffa_priv_data && ffa_priv_data->dev) + return FFA_ERR_STAT_SUCCESS; + + ret = device_bind(dm_root(), + DM_DRIVER_GET(arm_ffa), + FFA_DRV_NAME, + NULL, + ofnode_null(), + &dev); + if (ret) + return ret; + + /* The FF-A bus discovery succeeds when probing is successful */ + ret = device_probe(dev); + if (ret) { + ffa_err("arm_ffa device probing failed"); + ffa_remove_device(dev); + return ret; + } + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_get_version - FFA_VERSION handler function + * + * This is the boot time function that implements FFA_VERSION FF-A function + * to get from the secure world the FF-A framework version + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_get_version(void) +{ + u16 major, minor; + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_VERSION, + .a1 = FFA_VERSION_1_0, .a2 = 0, .a3 = 0, .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + if (((int)res.a0) == FFA_ERR_STAT_NOT_SUPPORTED) { + ffa_err("A Firmware Framework implementation does not exist"); + return -EOPNOTSUPP; + } + + major = GET_FFA_MAJOR_VERSION((u32)res.a0); + minor = GET_FFA_MINOR_VERSION((u32)res.a0); + + ffa_info("FF-A driver %d.%d\nFF-A framework %d.%d", + FFA_MAJOR_VERSION, FFA_MINOR_VERSION, major, minor); + + if ((major == FFA_MAJOR_VERSION && minor >= FFA_MINOR_VERSION)) { + ffa_info("Versions are compatible "); + + ffa_priv_data->fwk_version = (u32)res.a0; + + return FFA_ERR_STAT_SUCCESS; + } + + ffa_err("Versions are incompatible\nExpected: %d.%d , Found: %d.%d\n", + FFA_MAJOR_VERSION, FFA_MINOR_VERSION, major, minor); + + return -EPROTONOSUPPORT; +} + +/** + * ffa_get_endpoint_id - FFA_ID_GET handler function + * + * This is the boot time function that implements FFA_ID_GET FF-A function + * to get from the secure world u-boot endpoint ID + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_get_endpoint_id(void) +{ + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_ID_GET, + .a1 = 0, .a2 = 0, .a3 = 0, .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + + if (((int)res.a2) == FFA_ERR_STAT_NOT_SUPPORTED) { + ffa_err("This function is not implemented at this FF-A instance"); + return -EOPNOTSUPP; + } + + ffa_err("Undefined error code (%d)", ((int)res.a2)); + return -EINVAL; + + case FFA_SUCCESS: + + ffa_priv_data->id = GET_SELF_ENDPOINT_ID((u32)res.a2); + ffa_info("endpoint ID is %u", ffa_priv_data->id); + + return FFA_ERR_STAT_SUCCESS; + + default: + + ffa_err("Undefined response function (0x%lx)", res.a0); + return -EINVAL; + } +} + +/** + * ffa_get_features_desc - returns the features descriptor of the specified + * FF-A function + * @func_id: the FF-A function which the features are to be retrieved + * + * This is a boot time function that searches the features descriptor of the + * specified FF-A function + * + * Return: + * + * When found, the address of the features descriptor is returned. Otherwise, NULL. + */ +static struct ffa_features_desc *ffa_get_features_desc(u32 func_id) +{ + u32 desc_idx; + + /* + * search for the descriptor of the selected FF-A interface + */ + for (desc_idx = 0; desc_idx < FFA_FEATURE_DESC_CNT ; desc_idx++) + if (ffa_priv_data->features[desc_idx].func_id == func_id) + return &ffa_priv_data->features[desc_idx]; + + return NULL; +} + +/** + * ffa_get_rxtx_map_features - FFA_FEATURES handler function with FFA_RXTX_MAP + * argument + * + * This is the boot time function that implements FFA_FEATURES FF-A function + * to retrieve the FFA_RXTX_MAP features + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_get_rxtx_map_features(void) +{ + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_FEATURES, + .a1 = FFA_RXTX_MAP, + .a2 = 0, .a3 = 0, .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + { + if (((int)res.a2) == FFA_ERR_STAT_NOT_SUPPORTED) { + ffa_err("FFA_RXTX_MAP is not implemented at this FF-A instance"); + return -EOPNOTSUPP; + } + + ffa_err("Undefined error code (%d)", ((int)res.a2)); + return -EINVAL; + } + case FFA_SUCCESS: + { + u32 desc_idx; + + /* + * search for an empty descriptor + */ + for (desc_idx = 0; desc_idx < FFA_FEATURE_DESC_CNT ; desc_idx++) + if (!ffa_priv_data->features[desc_idx].func_id) { + /* + * populate the descriptor with + * the interface features data + */ + ffa_priv_data->features[desc_idx].func_id = + FFA_RXTX_MAP; + ffa_priv_data->features[desc_idx].field1 = + (u32)res.a2; + + ffa_info("FFA_RXTX_MAP features data 0x%x", + ffa_priv_data->features[desc_idx].field1); + + return FFA_ERR_STAT_SUCCESS; + } + + ffa_err("Cannot save FFA_RXTX_MAP features data. Descriptors table full"); + return -ENOBUFS; + } + default: + { + ffa_err("Undefined response function (0x%lx)", + res.a0); + return -EINVAL; + } + } +} + +/** + * ffa_get_rxtx_buffers_pages_cnt - reads from the features data descriptors + * the minimum number of pages in each of the RX/TX + * buffers + * @buf_4k_pages: Pointer to the minimum number of pages + * + * This is the boot time function that returns the minimum number of pages + * in each of the RX/TX buffers + * + * Return: + * + * buf_4k_pages points to the returned number of pages + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_get_rxtx_buffers_pages_cnt(size_t *buf_4k_pages) +{ + struct ffa_features_desc *desc = NULL; + + if (!buf_4k_pages) + return -EINVAL; + + desc = ffa_get_features_desc(FFA_RXTX_MAP); + if (!desc) + return -EINVAL; + + switch (desc->field1) { + case RXTX_4K: + *buf_4k_pages = 1; + break; + case RXTX_16K: + *buf_4k_pages = 4; + break; + case RXTX_64K: + *buf_4k_pages = 16; + break; + default: + ffa_err("RX/TX buffer size not supported"); + return -EINVAL; + } + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_free_rxtx_buffers - frees the RX/TX buffers + * @buf_4k_pages: the minimum number of pages in each of the RX/TX + * buffers + * + * This is the boot time function used to free the RX/TX buffers + * + */ +static void ffa_free_rxtx_buffers(void) +{ + ffa_info("Freeing RX/TX buffers"); + + if (ffa_priv_data->pair.rxbuf) { + free((void *)ffa_priv_data->pair.rxbuf); + ffa_priv_data->pair.rxbuf = 0; + } + + if (ffa_priv_data->pair.txbuf) { + free((void *)ffa_priv_data->pair.txbuf); + ffa_priv_data->pair.txbuf = 0; + } +} + +/** + * ffa_alloc_rxtx_buffers - allocates the RX/TX buffers + * @buf_4k_pages: the minimum number of pages in each of the RX/TX + * buffers + * + * This is the boot time function used by ffa_map_rxtx_buffers to allocate + * the RX/TX buffers before mapping them. The allocated memory is physically + * contiguous since memalign ends up calling malloc which allocates + * contiguous memory in u-boot. + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_alloc_rxtx_buffers(size_t buf_4k_pages) +{ + u64 bytes; + + ffa_info("Using %lu 4KB page(s) for RX/TX buffers size", buf_4k_pages); + + bytes = buf_4k_pages * SZ_4K; + + /* RX/TX buffers addresses should be PAGE_SIZE aligned */ + + ffa_priv_data->pair.rxbuf = (u64)memalign(PAGE_SIZE, bytes); + if (!ffa_priv_data->pair.rxbuf) { + ffa_err("Failure to allocate RX buffer"); + return -ENOBUFS; + } + + ffa_info("RX buffer at virtual address 0x%llx", ffa_priv_data->pair.rxbuf); + + ffa_priv_data->pair.txbuf = (u64)memalign(PAGE_SIZE, bytes); + if (!ffa_priv_data->pair.txbuf) { + free((void *)ffa_priv_data->pair.rxbuf); + ffa_priv_data->pair.rxbuf = 0; + ffa_err("Failure to allocate the TX buffer"); + return -ENOBUFS; + } + + ffa_info("TX buffer at virtual address 0x%llx", ffa_priv_data->pair.txbuf); + + /* + * make sure the buffers are cleared before use + */ + memset((void *)ffa_priv_data->pair.rxbuf, 0, bytes); + memset((void *)ffa_priv_data->pair.txbuf, 0, bytes); + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_map_rxtx_buffers - FFA_RXTX_MAP handler function + * @buf_4k_pages: the minimum number of pages in each of the RX/TX + * buffers + * + * This is the boot time function that implements FFA_RXTX_MAP FF-A function + * to map the RX/TX buffers + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_map_rxtx_buffers(size_t buf_4k_pages) +{ + int ret; + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + ret = ffa_alloc_rxtx_buffers(buf_4k_pages); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + /* + * we need to pass the physical addresses of the RX/TX buffers + * in u-boot physical/virtual mapping is 1:1 + *no need to convert from virtual to physical + */ + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_RXTX_MAP, + .a1 = ffa_priv_data->pair.txbuf, + .a2 = ffa_priv_data->pair.rxbuf, + .a3 = buf_4k_pages, + .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + { + switch (((int)res.a2)) { + case FFA_ERR_STAT_INVALID_PARAMETERS: + ffa_err("One or more fields in input parameters is incorrectly encoded"); + ret = -EPERM; + break; + case FFA_ERR_STAT_NO_MEMORY: + ffa_err("Not enough memory"); + ret = -ENOMEM; + break; + case FFA_ERR_STAT_DENIED: + ffa_err("Buffer pair already registered"); + ret = -EACCES; + break; + case FFA_ERR_STAT_NOT_SUPPORTED: + ffa_err("This function is not implemented at this FF-A instance"); + ret = -EOPNOTSUPP; + break; + default: + ffa_err("Undefined error (%d)", + ((int)res.a2)); + ret = -EINVAL; + } + break; + } + case FFA_SUCCESS: + ffa_info("RX/TX buffers mapped"); + return FFA_ERR_STAT_SUCCESS; + default: + ffa_err("Undefined response function (0x%lx)", + res.a0); + ret = -EINVAL; + } + + ffa_free_rxtx_buffers(); + + return ret; +} + +/** + * ffa_unmap_rxtx_buffers - FFA_RXTX_UNMAP handler function + * + * This is the boot time function that implements FFA_RXTX_UNMAP FF-A function + * to unmap the RX/TX buffers + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_unmap_rxtx_buffers(void) +{ + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_RXTX_UNMAP, + .a1 = PREP_SELF_ENDPOINT_ID(ffa_priv_data->id), + .a2 = 0, .a3 = 0, .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + { + if (((int)res.a2) == FFA_ERR_STAT_NOT_SUPPORTED) + panic("[FFA] FFA_RXTX_UNMAP is not implemented at this FF-A instance\n"); + else if (((int)res.a2) == FFA_ERR_STAT_INVALID_PARAMETERS) + panic("[FFA] There is no buffer pair registered on behalf of the caller\n"); + else + panic("[FFA] Undefined error (%d)\n", ((int)res.a2)); + } + case FFA_SUCCESS: + { + ffa_free_rxtx_buffers(); + + return FFA_ERR_STAT_SUCCESS; + } + default: + panic("[FFA] Undefined response function (0x%lx)", res.a0); + return -ENODEV; + } +} + +/** + * ffa_release_rx_buffer - FFA_RX_RELEASE handler function + * + * This is the boot time function that invokes FFA_RX_RELEASE FF-A function + * to release the ownership of the RX buffer + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_release_rx_buffer(void) +{ + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_RX_RELEASE, + .a1 = 0, .a2 = 0, .a3 = 0, .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + { + if (((int)res.a2) == FFA_ERR_STAT_NOT_SUPPORTED) + panic("[FFA] FFA_RX_RELEASE is not implemented at this FF-A instance\n"); + else if (((int)res.a2) == FFA_ERR_STAT_DENIED) + panic("[FFA] Caller did not have ownership of the RX buffer\n"); + else + panic("[FFA] Undefined error (%d)\n", ((int)res.a2)); + } + case FFA_SUCCESS: + return FFA_ERR_STAT_SUCCESS; + + default: + panic("[FFA] Undefined response function (0x%lx)\n", res.a0); + return -ENODEV; + } +} + +/** + * ffa_uuid_are_identical - checks whether two given UUIDs are identical + * @uuid1: first UUID + * @uuid2: second UUID + * + * This is a boot time function used by ffa_read_partitions_info to search + * for a UUID in the partitions descriptors table + * + * Return: + * + * 1 when UUIDs match. Otherwise, 0 + */ +int ffa_uuid_are_identical(const union ffa_partition_uuid *uuid1, + const union ffa_partition_uuid *uuid2) +{ + if (!uuid1 || !uuid2) + return 0; + + return (!memcmp(uuid1, uuid2, sizeof(union ffa_partition_uuid))); +} + +/** + * ffa_read_partitions_info - reads the data queried by FFA_PARTITION_INFO_GET + * and saves it in the private structure + * @count: The number of partitions queried + * @part_uuid: Pointer to the partition(s) UUID + * + * This is the boot time function that reads the partitions information + * returned by the FFA_PARTITION_INFO_GET and saves it in the private + * data structure. + * + * Return: + * + * The private data structure is updated with the partition(s) information + * FFA_ERR_STAT_SUCCESS is returned on success. Otherwise, failure + */ +static int ffa_read_partitions_info(u32 count, union ffa_partition_uuid *part_uuid) +{ + if (!count) { + ffa_err("No partition detected"); + return -ENODATA; + } + + ffa_info("Reading partitions data from the RX buffer"); + + if (!part_uuid) { + /* + * querying information of all partitions + */ + u64 buf_bytes; + u64 data_bytes; + size_t buf_4k_pages = 0; + u32 desc_idx; + struct ffa_partition_info *parts_info; + int ret; + + data_bytes = count * sizeof(struct ffa_partition_desc); + + /* + * get the RX buffer size in pages + */ + ret = ffa_get_rxtx_buffers_pages_cnt(&buf_4k_pages); + if (ret != FFA_ERR_STAT_SUCCESS) { + ffa_err("Can not get the RX buffer size (error %d)", ret); + return ret; + } + + buf_bytes = buf_4k_pages * SZ_4K; + + if (data_bytes > buf_bytes) { + ffa_err("Partitions data size exceeds the RX buffer size:"); + ffa_err(" Sizes in bytes: data %llu , RX buffer %llu ", + data_bytes, + buf_bytes); + + return -ENOMEM; + } + + ffa_priv_data->partitions.descs = (struct ffa_partition_desc *) + devm_kmalloc(ffa_priv_data->dev, data_bytes, __GFP_ZERO); + if (!ffa_priv_data->partitions.descs) { + ffa_err("Cannot allocate partitions data buffer"); + return -ENOMEM; + } + + parts_info = (struct ffa_partition_info *)ffa_priv_data->pair.rxbuf; + + for (desc_idx = 0 ; desc_idx < count ; desc_idx++) { + ffa_priv_data->partitions.descs[desc_idx].info = + parts_info[desc_idx]; + + ffa_info("Partition ID %x : info cached", + ffa_priv_data->partitions.descs[desc_idx].info.id); + } + + ffa_priv_data->partitions.count = count; + + ffa_info("%d partition(s) found and cached", count); + + } else { + u32 rx_desc_idx, cached_desc_idx; + struct ffa_partition_info *parts_info; + u8 desc_found; + + parts_info = (struct ffa_partition_info *)ffa_priv_data->pair.rxbuf; + + /* + * search for the SP IDs read from the RX buffer + * in the already cached SPs. + * Update the UUID when ID found. + */ + for (rx_desc_idx = 0; rx_desc_idx < count ; rx_desc_idx++) { + desc_found = 0; + + /* + * search the current ID in the cached partitions + */ + for (cached_desc_idx = 0; + cached_desc_idx < ffa_priv_data->partitions.count; + cached_desc_idx++) { + /* + * save the UUID + */ + if (ffa_priv_data->partitions.descs[cached_desc_idx].info.id == + parts_info[rx_desc_idx].id) { + ffa_priv_data->partitions.descs[cached_desc_idx].UUID = + *part_uuid; + + desc_found = 1; + break; + } + } + + if (!desc_found) + return -ENODATA; + } + } + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_query_partitions_info - invokes FFA_PARTITION_INFO_GET and saves partitions data + * + * @part_uuid: Pointer to the partition(s) UUID + * @pcount: Pointer to the number of partitions variable filled when querying + * + * This is the boot time function that executes the FFA_PARTITION_INFO_GET + * to query the partitions data. Then, it calls ffa_read_partitions_info + * to save the data in the private data structure. + * + * After reading the data the RX buffer is released using ffa_release_rx_buffer + * + * Return: + * + * When part_uuid is NULL, all partitions data are retrieved from secure world + * When part_uuid is non NULL, data for partitions matching the given UUID are + * retrieved and the number of partitions is returned + * FFA_ERR_STAT_SUCCESS is returned on success. Otherwise, failure + */ +static int ffa_query_partitions_info(union ffa_partition_uuid *part_uuid, + u32 *pcount) +{ + union ffa_partition_uuid query_uuid = {0}; + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + panic("[FFA] no private data found\n"); + + /* + * If a UUID is specified. Information for one or more + * partitions in the system is queried. Otherwise, information + * for all installed partitions is queried + */ + + if (part_uuid) { + if (!pcount) + return -EINVAL; + + query_uuid = *part_uuid; + } + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_PARTITION_INFO_GET, + .a1 = query_uuid.words.a1, + .a2 = query_uuid.words.a2, + .a3 = query_uuid.words.a3, + .a4 = query_uuid.words.a4, + .a5 = 0, + .a6 = 0, + .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + { + switch (((int)res.a2)) { + case FFA_ERR_STAT_INVALID_PARAMETERS: + ffa_err("Unrecognized UUID"); + return -EPERM; + case FFA_ERR_STAT_NO_MEMORY: + ffa_err("Results cannot fit in RX buffer of the caller"); + return -ENOMEM; + case FFA_ERR_STAT_DENIED: + ffa_err("Callee is not in a state to handle this request"); + return -EACCES; + case FFA_ERR_STAT_NOT_SUPPORTED: + ffa_err("This function is not implemented at this FF-A instance"); + return -EOPNOTSUPP; + case FFA_ERR_STAT_BUSY: + ffa_err("RX buffer of the caller is not free"); + return -EBUSY; + default: + ffa_err("Undefined error (%d)", ((int)res.a2)); + return -EINVAL; + } + } + case FFA_SUCCESS: + { + int ret; + + /* + * res.a2 contains the count of partition information descriptors + * populated in the RX buffer + */ + if (res.a2) { + ret = ffa_read_partitions_info((u32)res.a2, part_uuid); + if (ret) + ffa_err("Failed to read partition(s) data , error (%d)", ret); + } + + /* + * return the SP count + */ + if (part_uuid) { + if (!ret) + *pcount = (u32)res.a2; + else + *pcount = 0; + } + /* + * After calling FFA_PARTITION_INFO_GET the buffer ownership + * is assigned to the consumer (u-boot). So, we need to give + * the ownership back to the SPM or hypervisor + */ + ret = ffa_release_rx_buffer(); + + if (!part_uuid && !res.a2) { + ffa_err("[FFA] no partition installed in the system"); + return -ENODEV; + } + + return ret; + } + default: + ffa_err("Undefined response function (0x%lx)", res.a0); + return -EINVAL; + } +} + +/** + * ffa_get_partitions_info - FFA_PARTITION_INFO_GET handler function + * + * The passed arguments: + * Mode 1: When getting from the driver the number of + * secure partitions: + * @uuid_str: pointer to the UUID string + * @parts_size: pointer to the variable that contains the number of partitions + * The variable will be set by the driver + * @buffer: NULL + * + * Mode 2: When requesting the driver to return the + * partitions information: + * @uuid_str: pointer to the UUID string + * @parts_size: pointer to the size of the SPs information buffer in bytes + * @buffer: pointer to SPs information buffer + * (allocated by the client). + * The buffer will be filled by the driver + * + * This is the boot time function that queries the secure partition data from + * the private data structure. If not found, it invokes FFA_PARTITION_INFO_GET + * FF-A function to query the partition information from secure world. + * + * A client of the FF-A driver should know the UUID of the service it wants to + * access. It should use the UUID to request the FF-A driver to provide the + * partition(s) information of the service. The FF-A driver uses + * PARTITION_INFO_GET to obtain this information. This is implemented through + * ffa_get_partitions_info function. + * A new FFA_PARTITION_INFO_GET call is issued (first one performed through + * ffa_cache_partitions_info) allowing to retrieve the partition(s) information. + * They are not saved (already done). We only update the UUID in the cached area. + * This assumes that partitions data does not change in the secure world. + * Otherwise u-boot will have an outdated partition data. The benefit of caching + * the information in the FF-A driver is to accommodate discovery after + * ExitBootServices(). + * + * When invoked through a client request, ffa_get_partitions_info should be + * called twice. First call is to get from the driver the number of secure + * partitions (SPs) associated to a particular UUID. + * Then, the caller (client) allocates the buffer to host the SPs data and + * issues a 2nd call. Then, the driver fills the SPs data in the pre-allocated + * buffer. + * + * To achieve the mechanism described above, ffa_get_partitions_info uses the + * following functions: + * ffa_read_partitions_info + * ffa_query_partitions_info + * + * Return: + * + * @parts_size: When pointing to the number of partitions variable, the number is + * set by the driver. + * When pointing to the partitions information buffer size, the buffer will be + * filled by the driver. + * + * On success FFA_ERR_STAT_SUCCESS is returned. Otherwise, failure + */ +static int ffa_get_partitions_info(const char *uuid_str, + u32 *parts_size, struct ffa_partition_info *buffer) +{ + /* + * fill_data: + * 0: return the SP count + * 1: fill SP data and return it to the caller + * -1: undefined mode + */ + int fill_data = -1; + u32 desc_idx, client_desc_idx; + union ffa_partition_uuid part_uuid = {0}; + u32 client_desc_max_cnt; + u32 parts_found = 0; + + if (!ffa_priv_data->partitions.count || !ffa_priv_data->partitions.descs) + panic("[FFA] No partition installed\n"); + + if (!uuid_str) { + ffa_err("No UUID provided"); + return -EINVAL; + } + + if (!parts_size) { + ffa_err("No size/count provided"); + return -EINVAL; + } + + if (ffa_uuid_str_to_bin(uuid_str, (unsigned char *)&part_uuid)) { + ffa_err("Invalid UUID"); + return -EINVAL; + } + + if (!buffer) { + /* Mode 1: getting the number of secure partitions */ + + fill_data = 0; + + ffa_info("Preparing for checking partitions count"); + + } else if ((*parts_size >= sizeof(struct ffa_partition_info)) && + !(*parts_size % sizeof(struct ffa_partition_info))) { + /* Mode 2: retrieving the partitions information */ + + fill_data = 1; + + client_desc_idx = 0; + + /* + * number of empty descriptors preallocated by the caller + */ + client_desc_max_cnt = *parts_size / sizeof(struct ffa_partition_info); + + ffa_info("Preparing for filling partitions info"); + + } else { + ffa_err("Invalid function arguments provided"); + return -EINVAL; + } + + ffa_info("Searching partitions using the provided UUID"); + + /* + * search in the cached partitions + */ + for (desc_idx = 0; + desc_idx < ffa_priv_data->partitions.count; + desc_idx++) { + if (ffa_uuid_are_identical(&ffa_priv_data->partitions.descs[desc_idx].UUID, + &part_uuid)) { + ffa_info("Partition ID %x matches the provided UUID", + ffa_priv_data->partitions.descs[desc_idx].info.id); + + parts_found++; + + if (fill_data) { + /* + * trying to fill the partition info in the input buffer + */ + + if (client_desc_idx < client_desc_max_cnt) { + buffer[client_desc_idx++] = + ffa_priv_data->partitions.descs[desc_idx].info; + continue; + } + + ffa_err("Failed to fill the current descriptor client buffer full"); + return -ENOBUFS; + } + } + } + + if (!parts_found) { + int ret; + + ffa_info("No partition found. Querying framework ..."); + + ret = ffa_query_partitions_info(&part_uuid, &parts_found); + + if (ret == FFA_ERR_STAT_SUCCESS) { + if (!fill_data) { + *parts_size = parts_found; + + ffa_info("Number of partition(s) found matching the UUID: %d", + parts_found); + } else { + /* + * If SPs data detected, they are already in the private data + * structure, retry searching SP data again to return them + * to the caller + */ + if (parts_found) + ret = ffa_get_partitions_info(uuid_str, parts_size, buffer); + else + ret = -ENODATA; + } + } + + return ret; + } + + /* partition(s) found */ + if (!fill_data) + *parts_size = parts_found; + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_cache_partitions_info - Queries and saves all secure partitions data + * + * This is a boot time function that invokes FFA_PARTITION_INFO_GET FF-A + * function to query from secure world all partitions information. + * + * The FFA_PARTITION_INFO_GET call is issued with nil UUID as an argument. + * All installed partitions information are returned. We cache them in the + * resident private data structure and we keep the UUID field empty + * (in FF-A 1.0 UUID is not provided by the partition descriptor) + * + * This function is called at the device probing level. + * ffa_cache_partitions_info uses ffa_query_partitions_info to get the data + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_cache_partitions_info(void) +{ + return ffa_query_partitions_info(NULL, NULL); +} + +/** + * ffa_msg_send_direct_req - FFA_MSG_SEND_DIRECT_{REQ,RESP} handler function + * @dst_part_id: destination partition ID + * @msg: pointer to the message data preallocated by the client (in/out) + * + * This is the runtime function that implements FFA_MSG_SEND_DIRECT_{REQ,RESP} + * FF-A functions. + * + * FFA_MSG_SEND_DIRECT_REQ is used to send the data to the secure partition. + * The response from the secure partition is handled by reading the + * FFA_MSG_SEND_DIRECT_RESP arguments. + * + * The maximum size of the data that can be exchanged is 40 bytes which is + * sizeof(struct ffa_send_direct_data) as defined by the FF-A specification 1.0 + * in the section relevant to FFA_MSG_SEND_DIRECT_{REQ,RESP} + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int __ffa_runtime ffa_msg_send_direct_req(u16 dst_part_id, struct ffa_send_direct_data *msg) +{ + ffa_value_t res = {0}; + + if (!ffa_priv_data->invoke_ffa_fn) + return -ENODEV; + + /* No partition installed */ + if (!ffa_priv_data->partitions.count || !ffa_priv_data->partitions.descs) + return -ENODEV; + + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_MSG_SEND_DIRECT_REQ, + .a1 = PREP_SELF_ENDPOINT_ID(ffa_priv_data->id) | + PREP_PART_ENDPOINT_ID(dst_part_id), + .a2 = 0, + .a3 = msg->data0, + .a4 = msg->data1, + .a5 = msg->data2, + .a6 = msg->data3, + .a7 = msg->data4, + }, &res); + + while (res.a0 == FFA_INTERRUPT) + ffa_priv_data->invoke_ffa_fn((ffa_value_t){ + .a0 = FFA_RUN, + .a1 = res.a1, .a2 = 0, .a3 = 0, .a4 = 0, .a5 = 0, .a6 = 0, .a7 = 0, + }, &res); + + switch (res.a0) { + case FFA_ERROR: + { + switch (((int)res.a2)) { + case FFA_ERR_STAT_INVALID_PARAMETERS: + /* Invalid endpoint ID or non-zero reserved register */ + return -EPERM; + case FFA_ERR_STAT_ABORTED: + /* Message target ran into unexpected error and has aborted */ + return -ECONNABORTED; + case FFA_ERR_STAT_DENIED: + /* Callee is not in a state to handle this request */ + return -EACCES; + case FFA_ERR_STAT_NOT_SUPPORTED: + /* This function is not implemented at this FF-A instance */ + return -EOPNOTSUPP; + case FFA_ERR_STAT_BUSY: + /* Message target is busy */ + return -EBUSY; + default: + /* Undefined error */ + return -ENXIO; + } + } + case FFA_SUCCESS: + + /* Message sent with no response */ + return FFA_ERR_STAT_SUCCESS; + + case FFA_MSG_SEND_DIRECT_RESP: + /* + * Message sent with response + * extract the return data + */ + msg->data0 = res.a3; + msg->data1 = res.a4; + msg->data2 = res.a5; + msg->data3 = res.a6; + msg->data4 = res.a7; + + return FFA_ERR_STAT_SUCCESS; + + default: + /* Undefined response function */ + return -ENOENT; + } +} + +/** + * __arm_ffa_fn_smc - SMC wrapper + * + * Calls low level SMC function + * + * Return: void + */ +void __ffa_runtime __arm_ffa_fn_smc(ffa_value_t args, ffa_value_t *res) +{ + arm_smccc_1_2_smc(&args, res); +} + +/** + * ffa_set_conduit - Set the conduit + * + * This boot time function clears the private data structure and sets the conduit + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_set_conduit(void) +{ + ffa_priv_data->invoke_ffa_fn = __arm_ffa_fn_smc; + + ffa_info("Conduit is SMC"); + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_set_bus_ops - Set the bus driver operations + * + * Setting the driver callbacks. + * + */ +static void ffa_set_bus_ops(void) +{ + ffa_priv_data->ffa_ops.partition_info_get = ffa_get_partitions_info; + ffa_priv_data->ffa_ops.sync_send_receive = ffa_msg_send_direct_req; + ffa_priv_data->ffa_ops.rxtx_unmap = ffa_unmap_rxtx_buffers; +} + +/** + * ffa_alloc_prvdata - allocate the driver main data structure + * @dev: the arm_ffa device + * + * This boot time function creates the main data structure embedding all the driver data. + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_alloc_prvdata(struct udevice *dev) +{ + if (!dev) + return -ENODEV; + + /* The device is registered with the DM. Let's create the driver main data structure*/ + + ffa_priv_data = devm_kmalloc(dev, sizeof(struct ffa_prvdata), __GFP_ZERO); + if (!ffa_priv_data) { + ffa_err("can not allocate the driver main data structure"); + return -ENOMEM; + } + + ffa_priv_data->dev = dev; + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_probe - The driver probe function + * @dev: the arm_ffa device + * + * Probing is done at boot time and triggered by the uclass device discovery. + * At probe level the following actions are done: + * - setting the conduit + * - querying the FF-A framework version + * - querying from secure world the u-boot endpoint ID + * - querying from secure world the supported features of the specified FF-A calls + * - mapping the RX/TX buffers + * - querying from secure world all the partitions information + * + * All data queried from secure world is saved in the resident private data structure. + * + * The probe will fail if either FF-A framework is not detected or the + * FF-A requests are not behaving correctly. This ensures that the + * driver is not installed and its operations are not exported to the clients. + * However, once the driver is successfully probed and an FF-A anomaly is + * detected when clients invoke the driver operations, the driver cause + * u-boot to panic because the client would not know what to do in such conditions. + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +static int ffa_probe(struct udevice *dev) +{ + int ret; + size_t buf_4k_pages = 0; + + ret = ffa_alloc_prvdata(dev); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ffa_set_bus_ops(); + + ret = ffa_set_conduit(); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ret = ffa_get_version(); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ret = ffa_get_endpoint_id(); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ret = ffa_get_rxtx_map_features(); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ret = ffa_get_rxtx_buffers_pages_cnt(&buf_4k_pages); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ret = ffa_map_rxtx_buffers(buf_4k_pages); + if (ret != FFA_ERR_STAT_SUCCESS) + return ret; + + ret = ffa_cache_partitions_info(); + if (ret != FFA_ERR_STAT_SUCCESS) { + ffa_free_rxtx_buffers(); + return ret; + } + + return FFA_ERR_STAT_SUCCESS; +} + +/** + * ffa_bus_ops_get - bus driver operations getter + * + * Return: + * This runtime function returns a pointer to the driver operations structure + */ +const struct ffa_bus_ops * __ffa_runtime ffa_bus_ops_get(void) +{ + return &ffa_priv_data->ffa_ops; +} + +/** + * ffa_bus_prvdata_get - bus driver private data getter + * + * Return: + * This boot time function returns a pointer to the main private data structure + */ +struct ffa_prvdata **ffa_bus_prvdata_get(void) +{ + return &ffa_priv_data; +} + +/** + * ffa_bus_discover - discover FF-A bus and probe the arm_ffa device + * + * This boot time function makes sure the FF-A bus is discoverable. + * Then, the arm_ffa device is probed and ready to use. + * This function is called automatically at initcalls + * level (after u-boot relocation). + * + * Arm FF-A transport is implemented through arm_ffa u-boot device managing the FF-A + * communication. + * All FF-A clients should use the arm_ffa device to use the FF-A transport. + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +int ffa_bus_discover(void) +{ + return ffa_device_get(); +} + +/** + * Declaring the arm_ffa driver under UCLASS_FFA + */ + +U_BOOT_DRIVER(arm_ffa) = { + .name = FFA_DRV_NAME, + .id = UCLASS_FFA, + .probe = ffa_probe, +}; diff --git a/drivers/arm-ffa/efi_ffa_runtime_data_mgr.c b/drivers/arm-ffa/efi_ffa_runtime_data_mgr.c new file mode 100644 index 0000000000..942601a7ba --- /dev/null +++ b/drivers/arm-ffa/efi_ffa_runtime_data_mgr.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi + */ + +#include "arm_ffa_prv.h" + +/** + * ffa_copy_runtime_data - copy the private data structure to the runtime area + * + * This boot time function copies the arm_ffa driver data structures including + * partitions data to the EFI runtime data section. + * + * Return: + * + * FFA_ERR_STAT_SUCCESS on success. Otherwise, failure + */ +efi_status_t ffa_copy_runtime_data(void) +{ + efi_status_t efi_ret; + efi_uintn_t prvdata_pages; + efi_uintn_t descs_pages; + struct ffa_prvdata **prvdata = NULL; /* Pointer to the current structure */ + struct ffa_prvdata *runtime_prvdata = NULL; /* Pointer to the structure runtime copy */ + u64 runtime_descs = 0; + + prvdata = ffa_bus_prvdata_get(); + + printf("INFO: EFI: FFA: prv data area at 0x%llx\n", (u64)prvdata); + + /* allocate private data runtime area */ + + prvdata_pages = efi_size_in_pages(sizeof(struct ffa_prvdata)); + efi_ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, + EFI_RUNTIME_SERVICES_DATA, + prvdata_pages, + (u64 *)&runtime_prvdata); + + if (efi_ret != EFI_SUCCESS) { + printf("ERROR: EFI: FFA: allocating runtime data (err: 0x%lx, addr 0x%llx)\n", + efi_ret, (u64)runtime_prvdata); + + return efi_ret; + } + + printf("INFO: EFI: FFA: runtime data area at 0x%llx\n", (u64)runtime_prvdata); + + if (!runtime_prvdata) + return EFI_INVALID_PARAMETER; + + /* allocate the partition data runtime area */ + + descs_pages = efi_size_in_pages((*prvdata)->partitions.count * + sizeof(struct ffa_partition_desc)); + efi_ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, + EFI_RUNTIME_SERVICES_DATA, + descs_pages, + &runtime_descs); + + if (efi_ret != EFI_SUCCESS) { + printf("ERROR: EFI: FFA: allocating runtime SPs data (err: 0x%lx, addr 0x%llx)\n", + efi_ret, runtime_descs); + + efi_free_pages((u64)runtime_prvdata, prvdata_pages); + + return efi_ret; + } + + printf("INFO: EFI: FFA: SPs runtime area at 0x%llx\n", (u64)runtime_descs); + + if (!runtime_descs) + return EFI_INVALID_PARAMETER; + + *runtime_prvdata = **prvdata; + + runtime_prvdata->dev = NULL; + runtime_prvdata->ffa_ops.partition_info_get = NULL; + runtime_prvdata->ffa_ops.rxtx_unmap = NULL; + runtime_prvdata->partitions.descs = (struct ffa_partition_desc *)runtime_descs; + runtime_prvdata->pair.rxbuf = 0; + runtime_prvdata->pair.txbuf = 0; + + /* + * Update the private data structure pointer in the driver + * no need to free the old structure. devm takes care of that + */ + *prvdata = runtime_prvdata; + + printf("INFO: EFI: FFA: runtime prv data now at 0x%llx , SPs count %d\n", + (u64)*prvdata, (*prvdata)->partitions.count); + + return FFA_ERR_STAT_SUCCESS; +} diff --git a/include/arm_ffa.h b/include/arm_ffa.h new file mode 100644 index 0000000000..ee9ce2d99d --- /dev/null +++ b/include/arm_ffa.h @@ -0,0 +1,132 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi + */ + +#ifndef __ARM_FFA_H +#define __ARM_FFA_H + +#include + +/* + * This header is public. It can be used by clients to access + * data structures and definitions they need + */ + +/* + * Macros for displaying logs + */ + +#define ffa_info(fmt, ...) pr_info("[FFA] " fmt "\n", ##__VA_ARGS__) +#define ffa_err(fmt, ...) pr_err("[FFA] " fmt "\n", ##__VA_ARGS__) + +/* + * The driver operations success error code + */ +#define FFA_ERR_STAT_SUCCESS (0) + +/* + * struct ffa_partition_info - Partition information descriptor + * @id: Partition ID + * @exec_ctxt: Execution context count + * @properties: Partition properties + * + * Data structure containing information about partitions instantiated in the system + * This structure is filled with the data queried by FFA_PARTITION_INFO_GET + */ +struct __packed ffa_partition_info { + u16 id; + u16 exec_ctxt; +/* partition supports receipt of direct requests */ +#define FFA_PARTITION_DIRECT_RECV BIT(0) +/* partition can send direct requests. */ +#define FFA_PARTITION_DIRECT_SEND BIT(1) +/* partition can send and receive indirect messages. */ +#define FFA_PARTITION_INDIRECT_MSG BIT(2) + u32 properties; +}; + +/* + * struct ffa_send_direct_data - Data structure hosting the data + * used by FFA_MSG_SEND_DIRECT_{REQ,RESP} + * @data0-4: Data read/written from/to x3-x7 registers + * + * Data structure containing the data to be sent by FFA_MSG_SEND_DIRECT_REQ + * or read from FFA_MSG_SEND_DIRECT_RESP + */ + +/* For use with FFA_MSG_SEND_DIRECT_{REQ,RESP} which pass data via registers */ +struct __packed ffa_send_direct_data { + unsigned long data0; /* w3/x3 */ + unsigned long data1; /* w4/x4 */ + unsigned long data2; /* w5/x5 */ + unsigned long data3; /* w6/x6 */ + unsigned long data4; /* w7/x7 */ +}; + +#if CONFIG_IS_ENABLED(ARM_FFA_EFI_RUNTIME_MODE) + +#include + +/* + * __ffa_runtime - controls whether functions are + * available after calling the EFI ExitBootServices service. + * Functions tagged with these keywords are resident (available at boot time and + * at runtime) + */ + +#define __ffa_runtime_data __efi_runtime_data +#define __ffa_runtime __efi_runtime + +#else + +/* + * The FF-A driver is independent from EFI + */ + +#define __ffa_runtime_data +#define __ffa_runtime + +#endif + +/** + * struct ffa_bus_ops - The driver operations structure + * @partition_info_get: callback for the FFA_PARTITION_INFO_GET + * @sync_send_receive: callback for the FFA_MSG_SEND_DIRECT_REQ + * @rxtx_unmap: callback for the FFA_RXTX_UNMAP + * + * The data structure providing all the operations supported by the driver. + * This structure is EFI runtime resident. + */ +struct ffa_bus_ops { + int (*partition_info_get)(const char *uuid_str, + u32 *parts_size, struct ffa_partition_info *buffer); + int (*sync_send_receive)(u16 dst_part_id, struct ffa_send_direct_data *msg); + int (*rxtx_unmap)(void); +}; + +/** + * The device driver and the Uclass driver public functions + */ + +/** + * ffa_bus_ops_get - driver operations getter + */ +const struct ffa_bus_ops * __ffa_runtime ffa_bus_ops_get(void); + +/** + * ffa_bus_discover - discover FF-A bus and probes the arm_ffa device + */ +int ffa_bus_discover(void); + +#if CONFIG_IS_ENABLED(ARM_FFA_EFI_RUNTIME_MODE) + +/** + * ffa_copy_runtime_data - copy the private data structure and the SPs data to the runtime area + */ +efi_status_t ffa_copy_runtime_data(void); + +#endif + +#endif diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h index a432e43871..6eebbe9c7d 100644 --- a/include/dm/uclass-id.h +++ b/include/dm/uclass-id.h @@ -55,6 +55,7 @@ enum uclass_id { UCLASS_EFI_MEDIA, /* Devices provided by UEFI firmware */ UCLASS_ETH, /* Ethernet device */ UCLASS_ETH_PHY, /* Ethernet PHY device */ + UCLASS_FFA, /* Arm Firmware Framework for Armv8-A */ UCLASS_FIRMWARE, /* Firmware */ UCLASS_FUZZING_ENGINE, /* Fuzzing engine */ UCLASS_FS_FIRMWARE_LOADER, /* Generic loader */ diff --git a/include/uuid.h b/include/uuid.h index 4a4883d3b5..789f8e0f15 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -44,4 +44,12 @@ int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin); const char *uuid_guid_get_str(const unsigned char *guid_bin); void gen_rand_uuid(unsigned char *uuid_bin); void gen_rand_uuid_str(char *uuid_str, int str_format); + +#ifdef CONFIG_ARM_FFA_TRANSPORT +/** + * ffa_uuid_str_to_bin - Converts a big endian UUID string to a little endian buffer + */ +int ffa_uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin); +#endif + #endif diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index 4da64b5d29..e02bb445f5 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -23,6 +23,10 @@ #include #include +#if defined(CONFIG_ARM_FFA_TRANSPORT) +#include +#endif + DECLARE_GLOBAL_DATA_PTR; /* Task priority level */ @@ -2113,6 +2117,10 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, struct efi_event *evt, *next_event; efi_status_t ret = EFI_SUCCESS; +#if defined(CONFIG_ARM_FFA_TRANSPORT) + int ffa_ret; +#endif + EFI_ENTRY("%p, %zx", image_handle, map_key); /* Check that the caller has read the current memory map */ @@ -2173,6 +2181,15 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); } +#if defined(CONFIG_ARM_FFA_TRANSPORT) + /* unmap FF-A RX/TX buffers */ + ffa_ret = ffa_bus_ops_get()->rxtx_unmap(); + if (ffa_ret) + debug("[efi_boottime][ERROR]: can not unmap FF-A RX/TX buffers\n"); + else + debug("[efi_boottime][INFO]: FF-A RX/TX buffers unmapped\n"); +#endif + /* Patch out unsupported runtime function */ efi_runtime_detach(); diff --git a/lib/uuid.c b/lib/uuid.c index 284f8113ff..50b3e61d59 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0+ /* * Copyright 2011 Calxeda, Inc. + * Copyright 2022 ARM Limited */ #include @@ -342,6 +343,70 @@ int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, return 0; } +#ifdef CONFIG_ARM_FFA_TRANSPORT +/** + * ffa_uuid_str_to_bin - Converts a big endian UUID string to a little endian buffer + * @uuid_str: UUID string in big endian format (36 bytes wide + '/0') + * @uuid_bin: preallocated 16 bytes UUID buffer in little endian format + * + * UUID string is 36 characters (36 bytes): + * + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + * be be be be be + * + * where x is a hexadecimal character. Fields are separated by '-'s. + * When converting to a binary UUID, these endianness rules apply: + * be: means the field in the string is considered a big endian hex number + * and should be converted to little endian binary format + * + * Return: + * + * uuid_bin filled with little endian UUID data + * On success 0 is returned. Otherwise, failure code. + */ +int ffa_uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin) +{ + u16 tmp16 = 0; + u32 tmp32 = 0; + u64 tmp64 = 0; + + if (!uuid_str_valid(uuid_str) || !uuid_bin) + return -EINVAL; + + /* + * reverse bytes from big to little endian + */ + tmp32 = simple_strtoul(uuid_str, NULL, 16); + memcpy(uuid_bin, &tmp32, 4); + + /* + * reverse bytes from big to little endian + */ + tmp16 = simple_strtoul(uuid_str + 9, NULL, 16); + memcpy(uuid_bin + 4, &tmp16, 2); + + /* + * reverse bytes from big to little endian + */ + tmp16 = simple_strtoul(uuid_str + 14, NULL, 16); + memcpy(uuid_bin + 6, &tmp16, 2); + + /* + * reverse bytes from big to little endian + */ + tmp16 = simple_strtoul(uuid_str + 19, NULL, 16); + memcpy(uuid_bin + 8, &tmp16, 2); + + /* + * reverse bytes from big to little endian + */ + tmp64 = simple_strtoull(uuid_str + 24, NULL, 16); + memcpy(uuid_bin + 10, (char *)&tmp64, 6); + + return 0; +} +#endif + /* * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. * From patchwork Mon Aug 1 17:20:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abdellatif El Khlifi X-Patchwork-Id: 1662685 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LxQ0d65YQz9sGP for ; Tue, 2 Aug 2022 03:22:09 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 154CD844EB; Mon, 1 Aug 2022 19:21:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 563ED844EB; Mon, 1 Aug 2022 19:21:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 53DCF844E6 for ; Mon, 1 Aug 2022 19:21:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=abdellatif.elkhlifi@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 61BD0139F; Mon, 1 Aug 2022 10:21:40 -0700 (PDT) Received: from e121910.arm.com (unknown [10.57.9.251]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CA9CE3F67D; Mon, 1 Aug 2022 10:21:33 -0700 (PDT) From: Abdellatif El Khlifi To: ilias.apalodimas@linaro.org Cc: jens.wiklander@linaro.org, abdellatif.elkhlifi@arm.com, achin.gupta@arm.com, trini@konsulko.com, u-boot@lists.denx.de, vishnu.banavath@arm.com, xueliang.zhong@arm.com, nd@arm.com Subject: [PATCH v3 4/4] arm_ffa: introduce armffa command Date: Mon, 1 Aug 2022 18:20:53 +0100 Message-Id: <20220801172053.20163-5-abdellatif.elkhlifi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> References: <20220801172053.20163-1-abdellatif.elkhlifi@arm.com> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Provide armffa command showcasing the use of the FF-A driver The armffa command allows to query secure partitions data from the secure world and exchanging messages with the partitions. Signed-off-by: Abdellatif El Khlifi Cc: Tom Rini Cc: Ilias Apalodimas Cc: Jens Wiklander --- MAINTAINERS | 1 + cmd/Kconfig | 10 ++ cmd/Makefile | 2 + cmd/armffa.c | 246 ++++++++++++++++++++++++++++++++++++++++ drivers/arm-ffa/Kconfig | 1 + 5 files changed, 260 insertions(+) create mode 100644 cmd/armffa.c diff --git a/MAINTAINERS b/MAINTAINERS index 93a57f4df2..b0fbc027c2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -251,6 +251,7 @@ F: configs/cortina_presidio-asic-pnand_defconfig ARM FF-A M: Abdellatif El Khlifi S: Maintained +F: cmd/armffa.c F: drivers/arm-ffa/ F: include/arm_ffa.h diff --git a/cmd/Kconfig b/cmd/Kconfig index a8260aa170..22f85188ff 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -902,6 +902,16 @@ endmenu menu "Device access commands" +config CMD_ARMFFA + bool "Arm FF-A test command" + depends on ARM_FFA_TRANSPORT + help + Provides a test command for the Arm FF-A driver + supported options: + - Listing the partition(s) info + - Sending a data pattern to the specified partition + - Displaying the arm_ffa device info + config CMD_ARMFLASH #depends on FLASH_CFI_DRIVER bool "armflash" diff --git a/cmd/Makefile b/cmd/Makefile index 5e43a1e022..e40f52f1e4 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -12,6 +12,8 @@ obj-y += panic.o obj-y += version.o # command + +obj-$(CONFIG_CMD_ARMFFA) += armffa.o obj-$(CONFIG_CMD_ACPI) += acpi.o obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o obj-$(CONFIG_CMD_AES) += aes.o diff --git a/cmd/armffa.c b/cmd/armffa.c new file mode 100644 index 0000000000..cb8a3d40aa --- /dev/null +++ b/cmd/armffa.c @@ -0,0 +1,246 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2022 ARM Limited + * Abdellatif El Khlifi + */ + +#include +#include +#include +#include +#include +#include +#include + +/** + * do_ffa_get_singular_partition_info - implementation of the getpart subcommand + * @cmdtp: Command Table + * @flag: flags + * @argc: number of arguments + * @argv: arguments + * + * This function queries the secure partition information which the UUID is provided + * as an argument. The function uses the arm_ffa driver partition_info_get operation + * to retrieve the data. + * The input UUID string is expected to be in big endian format. + * + * Return: + * + * CMD_RET_SUCCESS: on success, otherwise failure + */ +static int do_ffa_get_singular_partition_info(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + u32 count = 0, size = 0; + int ret; + struct ffa_partition_info *parts_info; + u32 info_idx; + + if (argc != 1) + return -EINVAL; + + /* Mode 1: getting the number of secure partitions */ + ret = ffa_bus_ops_get()->partition_info_get(argv[0], &count, NULL); + if (ret != FFA_ERR_STAT_SUCCESS) { + ffa_err("Failure in querying partitions count (error code: %d)", ret); + return ret; + } + + if (!count) { + ffa_info("No secure partition found"); + return ret; + } + + /* + * pre-allocate a buffer to be filled by the driver + * with ffa_partition_info structs + */ + + ffa_info("Pre-allocating %d partition(s) info structures", count); + + parts_info = calloc(count, sizeof(struct ffa_partition_info)); + if (!parts_info) + return -EINVAL; + + size = count * sizeof(struct ffa_partition_info); + + /* + * ask the driver to fill the buffer with the SPs info + */ + + ret = ffa_bus_ops_get()->partition_info_get(argv[0], &size, parts_info); + if (ret != FFA_ERR_STAT_SUCCESS) { + ffa_err("Failure in querying partition(s) info (error code: %d)", ret); + free(parts_info); + return ret; + } + + /* + * SPs found , show the partition information + */ + for (info_idx = 0; info_idx < count ; info_idx++) { + ffa_info("Partition: id = 0x%x , exec_ctxt 0x%x , properties 0x%x", + parts_info[info_idx].id, + parts_info[info_idx].exec_ctxt, + parts_info[info_idx].properties); + } + + free(parts_info); + + return 0; +} + +/** + * do_ffa_msg_send_direct_req - implementation of the ping subcommand + * @cmdtp: Command Table + * @flag: flags + * @argc: number of arguments + * @argv: arguments + * + * This function sends data to the secure partition which the ID is provided + * as an argument. The function uses the arm_ffa driver sync_send_receive operation + * to send data. + * + * Return: + * + * CMD_RET_SUCCESS: on success, otherwise failure + */ +int do_ffa_msg_send_direct_req(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct ffa_send_direct_data msg = {0}; + u64 pattern = 0xaabbccddaabbccd0; + u16 part_id; + int ret; + + if (argc != 1) + return -EINVAL; + + errno = 0; + part_id = strtoul(argv[0], NULL, 16); + + if (errno) { + ffa_err("Invalid partition ID"); + return -EINVAL; + } + + /* + * filling the message data + */ + msg.data0 = ++pattern; + msg.data1 = ++pattern; + msg.data2 = ++pattern; + msg.data3 = ++pattern; + msg.data4 = ++pattern; + + ret = ffa_bus_ops_get()->sync_send_receive(part_id, &msg); + if (ret == FFA_ERR_STAT_SUCCESS) { + u8 cnt; + + ffa_info("SP response:\n[LSB]"); + for (cnt = 0; + cnt < sizeof(struct ffa_send_direct_data) / sizeof(u64); + cnt++) + ffa_info("0x%llx", ((u64 *)&msg)[cnt]); + } else { + ffa_err("Sending direct request error (%d)", ret); + } + + return ret; +} + +/** + *do_ffa_dev_list - implementation of the devlist subcommand + * @cmdtp: [in] Command Table + * @flag: flags + * @argc: number of arguments + * @argv: arguments + * + * This function queries the devices belonging to the UCLASS_FFA + * class. Currently, one device is expected to show up: the arm_ffa device + * + * Return: + * + * CMD_RET_SUCCESS: on success, otherwise failure + */ +int do_ffa_dev_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + struct udevice *dev = NULL; + int i, ret; + + ffa_info("arm_ffa uclass entries:"); + + for (i = 0, ret = uclass_first_device(UCLASS_FFA, &dev); + dev; + ret = uclass_next_device(&dev), i++) { + if (ret) + break; + + ffa_info("entry %d - instance %08x, ops %08x, plat %08x", + i, + (u32)map_to_sysmem(dev), + (u32)map_to_sysmem(dev->driver->ops), + (u32)map_to_sysmem(dev_get_plat(dev))); + } + + return cmd_process_error(cmdtp, ret); +} + +static struct cmd_tbl armffa_commands[] = { + U_BOOT_CMD_MKENT(getpart, 1, 1, do_ffa_get_singular_partition_info, "", ""), + U_BOOT_CMD_MKENT(ping, 1, 1, do_ffa_msg_send_direct_req, "", ""), + U_BOOT_CMD_MKENT(devlist, 0, 1, do_ffa_dev_list, "", ""), +}; + +/** + * do_armffa - the armffa command main function + * @cmdtp: Command Table + * @flag: flags + * @argc: number of arguments + * @argv: arguments + * + * This function identifies which armffa subcommand to run. + * Then, it makes sure the arm_ffa device is probed and + * ready for use. + * Then, it runs the subcommand. + * + * Return: + * + * CMD_RET_SUCCESS: on success, otherwise failure + */ +static int do_armffa(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + struct cmd_tbl *armffa_cmd; + int ret; + + if (argc < 2) + return CMD_RET_USAGE; + + armffa_cmd = find_cmd_tbl(argv[1], armffa_commands, ARRAY_SIZE(armffa_commands)); + + argc -= 2; + argv += 2; + + if (!armffa_cmd || argc > armffa_cmd->maxargs) + return CMD_RET_USAGE; + + ret = ffa_bus_discover(); + if (ret != FFA_ERR_STAT_SUCCESS) + return cmd_process_error(cmdtp, ret); + + if (!ffa_bus_ops_get()) + return -EINVAL; + + ret = armffa_cmd->cmd(armffa_cmd, flag, argc, argv); + + return cmd_process_error(armffa_cmd, ret); +} + +U_BOOT_CMD(armffa, 4, 1, do_armffa, + "Arm FF-A operations test command", + "getpart \n" + " - lists the partition(s) info\n" + "ping \n" + " - sends a data pattern to the specified partition\n" + "devlist\n" + " - displays the arm_ffa device info\n"); diff --git a/drivers/arm-ffa/Kconfig b/drivers/arm-ffa/Kconfig index 882ffde90f..25816b479b 100644 --- a/drivers/arm-ffa/Kconfig +++ b/drivers/arm-ffa/Kconfig @@ -4,6 +4,7 @@ config ARM_FFA_TRANSPORT bool "Enable Arm Firmware Framework for Armv8-A driver" depends on DM && ARM64 select ARM_SMCCC + select CMD_ARMFFA select LIB_UUID select DEVRES help