From patchwork Mon Nov 27 14:15:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13469725 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 4ABE2C07CB1 for ; Mon, 27 Nov 2023 14:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=K0aWlJ2GlsdzDyq9OeMhtl4wQu0PwJdxzPkqoNCLlTQ=; b=JPUAGa9UO9zk0s KRaC0B6fHZsWuxlIQj6OrLTeEY7LQcg67/2rOnqnG+hAX6X+g20RuqNRGizACtJsLiJfVgtSmtqry 37yIe3YDr7BHZUR+H6IKpslICxhof8qJLiM58+Q8pnYqCreRxorLwMkrCZURUocovuwTqr8U67rtH aTj6XFn+O/AjH/IkfJngrj4ajpqQQmfz4hxrGywqPHzna82zQyBpc7SqOTv9H++4axoJakZjLAghQ RRv1xAYTHU73KOaz8bnlclcJTO/8gozsDmf2KFym8fRqsG0l1gjfCdm9Du0KoELs8H00bYplyfhJm y9+rU9t5etma0s9eUsLw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r7cQ0-002fXH-2c; Mon, 27 Nov 2023 14:17:01 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r7cPR-002fM7-2e for linux-arm-kernel@lists.infradead.org; Mon, 27 Nov 2023 14:16:27 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2c873cf9f74so54664921fa.0 for ; Mon, 27 Nov 2023 06:16:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1701094583; x=1701699383; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Mi8eKf9Bk+MHBCMvhATqbV/lq6r88mkgwcH/G87faK0=; b=QNu7YhNmok9PJsonsOgsq8noNwmDDrJhX1GeZsifXrwuy9253EU07xOgRYErpJF57p GoMUyx/V7ZzndCnDU967bDQ2JewD/l5MvyOKGoB7IBGFKXGdAgtJNyJNQTHjU9B0w2Lj LPwjI9BeL7YSvnPj+6TaD51z0vMjvG8eDltXYCew85fBdNGhJe/Hf0A5+2UJEPYoViU3 eb6VXTcBEgbkrVcreigshmuLPtYRY6Dks+SLIHTBBDa23CoOa20T9EtRYzkvShGm+rrd kYs5DXF6OEGuV1/MEOdGUFE3AOqoXYVOXez56pna6t0cx/KIprCx/ggZUTu9Gf3KoSsy PgZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701094583; x=1701699383; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Mi8eKf9Bk+MHBCMvhATqbV/lq6r88mkgwcH/G87faK0=; b=sKLy6gHwjubnwISHAdShQol4LkK/Nt+JWHn9CJ8e+2YVELiPgZ8yRkfQC1Mdz4aGun oyv7SqCK/Uood9GWLXt1lDsU8JhOvjCJelVCGKY7BPsc54sKkdGpQktom7uYvG9sgs2f vGdSp0xoyeo1WrCQjmDn5oQt8i+IgVoVi5fRnlvPe9Cta5EGt6cUpPNorFTTPtoExOXm rA2vcb/gw3UtVKde6Pwvy5btuXdMdQ85fGkkWGU/ICQKknE5lvuzgFha7Ydv6yYmEA2l u4U/HKhxcHTIxJsjkF5lPh+eTvqafiJ7lOXkuODkLjktlr+ldG4/Vqmg0gWZrlfSUQlL xXyA== X-Gm-Message-State: AOJu0Yxr4ZB19GBpe9xuXUGfWvdhu7KeR4FVkowuG30kmlyC9Fzys7iG I4IRCR33M1sNYrvshC8IF2M4Ew== X-Google-Smtp-Source: AGHT+IFNL1Em68j1sMBW/4w1EkRoANc+xc8NwuXyMpzGr/usWcq8SWNsyNFhkYwxwMDQJxFCSq7AIg== X-Received: by 2002:a2e:88c2:0:b0:2bc:c4af:36b9 with SMTP id a2-20020a2e88c2000000b002bcc4af36b9mr7465362ljk.52.1701094583228; Mon, 27 Nov 2023 06:16:23 -0800 (PST) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:bf33:77c7:8131:5e64]) by smtp.gmail.com with ESMTPSA id be7-20020a05600c1e8700b00405442edc69sm14658830wmb.14.2023.11.27.06.16.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 06:16:22 -0800 (PST) From: Bartosz Golaszewski To: Andy Gross , Bjorn Andersson , Konrad Dybcio , Elliot Berman , Krzysztof Kozlowski , Guru Das Srinagesh , Andrew Halaney , Maximilian Luz , Alex Elder , Srini Kandagatla Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel@quicinc.com, Bartosz Golaszewski Subject: [PATCH v6 11/13] firmware: qcom: tzmem: enable SHM Bridge support Date: Mon, 27 Nov 2023 15:15:58 +0100 Message-Id: <20231127141600.20929-12-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231127141600.20929-1-brgl@bgdev.pl> References: <20231127141600.20929-1-brgl@bgdev.pl> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231127_061625_872972_6731D435 X-CRM114-Status: GOOD ( 17.48 ) 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 From: Bartosz Golaszewski Add a new Kconfig option for selecting the SHM Bridge mode of operation for the TrustZone memory allocator. If enabled at build-time, it will still be checked for availability at run-time. If the architecture doesn't support SHM Bridge, the allocator will work just like in the default mode. Signed-off-by: Bartosz Golaszewski Tested-by: Andrew Halaney # sc8280xp-lenovo-thinkpad-x13s --- drivers/firmware/qcom/Kconfig | 10 +++++ drivers/firmware/qcom/qcom_tzmem.c | 65 +++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/qcom/Kconfig b/drivers/firmware/qcom/Kconfig index 237da40de832..e01407e31ae4 100644 --- a/drivers/firmware/qcom/Kconfig +++ b/drivers/firmware/qcom/Kconfig @@ -27,6 +27,16 @@ config QCOM_TZMEM_MODE_DEFAULT Use the default allocator mode. The memory is page-aligned, non-cachable and contiguous. +config QCOM_TZMEM_MODE_SHMBRIDGE + bool "SHM Bridge" + help + Use Qualcomm Shared Memory Bridge. The memory has the same alignment as + in the 'Default' allocator but is also explicitly marked as an SHM Bridge + buffer. + + With this selected, all buffers passed to the TrustZone must be allocated + using the TZMem allocator or else the TrustZone will refuse to use them. + endchoice config QCOM_SCM_DOWNLOAD_MODE_DEFAULT diff --git a/drivers/firmware/qcom/qcom_tzmem.c b/drivers/firmware/qcom/qcom_tzmem.c index 44a062f2abd4..1ca3773263e5 100644 --- a/drivers/firmware/qcom/qcom_tzmem.c +++ b/drivers/firmware/qcom/qcom_tzmem.c @@ -55,7 +55,70 @@ static void qcom_tzmem_cleanup_pool(struct qcom_tzmem_pool *pool) } -#endif /* CONFIG_QCOM_TZMEM_MODE_DEFAULT */ +#elif IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE) + +#include + +#define QCOM_SHM_BRIDGE_NUM_VM_SHIFT 9 + +static bool qcom_tzmem_using_shm_bridge; + +static int qcom_tzmem_init(void) +{ + int ret; + + ret = qcom_scm_shm_bridge_enable(); + if (ret == -EOPNOTSUPP) { + dev_info(qcom_tzmem_dev, "SHM Bridge not supported\n"); + return 0; + } + + if (!ret) + qcom_tzmem_using_shm_bridge = true; + + return ret; +} + +static int qcom_tzmem_init_pool(struct qcom_tzmem_pool *pool) +{ + u64 pfn_and_ns_perm, ipfn_and_s_perm, size_and_flags, ns_perms; + int ret; + + if (!qcom_tzmem_using_shm_bridge) + return 0; + + ns_perms = (QCOM_SCM_PERM_WRITE | QCOM_SCM_PERM_READ); + pfn_and_ns_perm = (u64)pool->pbase | ns_perms; + ipfn_and_s_perm = (u64)pool->pbase | ns_perms; + size_and_flags = pool->size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT); + + u64 *handle __free(kfree) = kzalloc(sizeof(*handle), GFP_KERNEL); + if (!handle) + return -ENOMEM; + + ret = qcom_scm_shm_bridge_create(qcom_tzmem_dev, pfn_and_ns_perm, + ipfn_and_s_perm, size_and_flags, + QCOM_SCM_VMID_HLOS, handle); + if (ret) + return ret; + + pool->priv = no_free_ptr(handle); + + return 0; +} + +static void qcom_tzmem_cleanup_pool(struct qcom_tzmem_pool *pool) +{ + u64 *handle = pool->priv; + + if (!qcom_tzmem_using_shm_bridge) + return; + + qcom_scm_shm_bridge_delete(qcom_tzmem_dev, *handle); + kfree(handle); +} + +#endif /* CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE */ /** * qcom_tzmem_pool_new() - Create a new TZ memory pool.