From patchwork Tue Oct 17 09:27:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13424903 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 9DCDACDB483 for ; Tue, 17 Oct 2023 09:28:47 +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=EDZIHslVmgp4E1eog2X+eDoeIZbFJAzrRE9R6Mrj78M=; b=UYeu1tp31lnIi8 iuhNqFGqB4US1qLhbmVko0jlqSl9sh7QTmxS6JWhTaY0K9YhxFNHXEZLF5DyL2QR8g3INmQAnrzlu Jj5oC1sIK0w2lG9L2wCDSvzwpUubRYQrNZBips7CiaU2B4G8GFgf8ie4MgsLZLLUyvreqlVQbrEql 7xwWNIhbuBbXwilceX/1xmZQKxz5u40PAeGvVwC9ftrIuK6zgqgsE5mYJomumk45dY/7gbZpbdCWV BBEnAHZ+sDW8YONbiK5HKp5a4x/FVywnqzkwGmYQqP69mcKnLytXxUEaX6+Emmhnd3FhqTxr6BWGC YWwX6Ncx/YCTsk//TL8Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qsgN4-00BoXG-32; Tue, 17 Oct 2023 09:28:14 +0000 Received: from mail-lj1-x22a.google.com ([2a00:1450:4864:20::22a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qsgMl-00BoLD-2y for linux-arm-kernel@lists.infradead.org; Tue, 17 Oct 2023 09:27:57 +0000 Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2c50cd16f3bso48262601fa.2 for ; Tue, 17 Oct 2023 02:27:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1697534874; x=1698139674; 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=lH1sZwRo7QEIPRBoUHgIgC70+qzWsBOkYQusaG/ALss=; b=u0791t1ac+AY4l+f4fYy3p3h5pViV65Sv2/MyQ9/VB0njxRtvO85IU33t64Ma+tY6f fOdvA/pUNswg6fdlgnpnIpxH46GVRjqfPf1MIAv3JKZyEyn2jPLr+rn6BPuaRLS/ZLpj sUiuhuY2jdev8DZNB3YkBqAP0WxN6q4Vrv3nn32WVNBvUYUqhxlakR0xtLPzYtCpQAbh KD8l/XvtYwHT3laggPinUWBY4ZgiS1S0ge56wYwJFBgMG2/z07lRfAmkhWKZ5Nqqivrg LqAUaQEeDLqHjNWN843QQjcQPJWw5NW4X9b9VOHR0rwTy5pWEo0WJ2yz/b/PeSawKB7m T87w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697534874; x=1698139674; 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=lH1sZwRo7QEIPRBoUHgIgC70+qzWsBOkYQusaG/ALss=; b=imE2/eFN+ce6K6fXoJOCReGCa+tphIN1s8vHNC+KjayfsSo3HkUrq6I7WjaeDTqQoY bt+l51vepC7SUlRLdt1r6JcuVKR9ZoofQEJ+0T6p7lr/koUQEb8PtBEeQ53ETEjy+iWt 6bi6KDJ+U/BSLsv39fTjPRLMszomeWx608O8/PbSsH+D/eecsauUv3Mnk4P//hf74dKT Pnrm4znPnHgOhKW/9wiPQmt/xlRLC8IzvnoWZDmujvaJGAWoVlXeuGnAtjJcL6BB+ir3 iOwE5+JrKGnq/+4WeTCfPz5khQInsmKuU0Rhoj9fyCgSB+uDEqwoPToy1/d2yupMcuF8 58+A== X-Gm-Message-State: AOJu0YxDmwpth5CbipkXSoEl4ETeITunTqXVMLqxysMneXEjNzW4nQma UCLveryPgm8xV+AkKbewnfnHDg== X-Google-Smtp-Source: AGHT+IFMnsoZTr0gCda3WphosvnSaVWKUn1wmH8Se8PR8EHHE7NaF6qkxMUjNOU5KvY9xhu2LYXVwA== X-Received: by 2002:a2e:6a12:0:b0:2c5:6c7:9e73 with SMTP id f18-20020a2e6a12000000b002c506c79e73mr1332354ljc.48.1697534874495; Tue, 17 Oct 2023 02:27:54 -0700 (PDT) Received: from brgl-uxlite.home ([2a01:cb1d:334:ac00:f1ee:b000:ce90:ed14]) by smtp.gmail.com with ESMTPSA id j5-20020a05600c1c0500b003fe1c332810sm9460787wms.33.2023.10.17.02.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 02:27:53 -0700 (PDT) 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 v5 13/15] firmware: qcom: tzmem: enable SHM Bridge support Date: Tue, 17 Oct 2023 11:27:30 +0200 Message-Id: <20231017092732.19983-14-brgl@bgdev.pl> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231017092732.19983-1-brgl@bgdev.pl> References: <20231017092732.19983-1-brgl@bgdev.pl> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231017_022755_995908_CC18D4B2 X-CRM114-Status: GOOD ( 17.30 ) 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 68ca59c5598e..8010af80fd59 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.