From patchwork Sun Mar 3 15:01:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13579825 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 6B4D5C48BF6 for ; Sun, 3 Mar 2024 15:01:52 +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=B1oU4c8WBK1kc0g/EW1Z/72DmMFtZpWr7G/8PqMVlfE=; b=Nf2TGVEAGceP4/ TJZrHBQTzNjhxFABqhxydOLOGLfBaeeAOcaCOv8layjqUBmC9yKcKgUCivtQF+D+56I/kX++UOWFB jyin+5bzNSIuSKDQrJOpzAkhhg4+sln811uz09vij3qZdwXHX03wEp3aeaXreHwROzHBdniM1QCVH NgUs9QLuNQN71efq2ZA+fPwscLS5NCWAC43ajYXQdMEDQMtowiB+5ieMta3h39mJXST70w6RnKrmY fxNepG26ekx0q4RfKsIoDjn5QHHjaOlKT2lkjNN0CVLMGbJax7lykg1pvNSRrvF+6IMF+1kLom2tD 6l54jFXj6kKPldaQVbVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rgnLS-000000068Nf-3y2J; Sun, 03 Mar 2024 15:01:42 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rgnLK-000000068HV-3gRh for linux-arm-kernel@lists.infradead.org; Sun, 03 Mar 2024 15:01:36 +0000 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-565c6cf4819so7899646a12.1 for ; Sun, 03 Mar 2024 07:01:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1709478093; x=1710082893; 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=iX5g+V/jIoB/U2fD/Lz2zFWCo9i8AUpfaivI8I9uXqI=; b=GpaA8W0ga1186Zgyqv1NERw9TmkeC3we6lFLS0OBb0weUmKNWbnsXfCbJ9b7XozHiE gJk+HndqYS+Gi7C/Ztitx9gke2KdayCd+yCf1t9FNvcWaUetndsYQu5za1xZIN0948gN pdN1D3lJNrzFYBdtvtRq0Uow7O1jvpWcY16IBVzsblBH6sJsLnpXfN7cPKK4PHq+r879 hDIW53jxNT5RIL/s9xBbprCnzlYA5gbk/5sHoDxmmhh43OELzKhehUaSoy3p5rx36eCH HYK+Nd7qS6tF9uXyLFMz2gT4xdRYpmmiU/DelsZiCwiUpq3tJLzw4pSmyaM+yxQqnlMk 0CTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709478093; x=1710082893; 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=iX5g+V/jIoB/U2fD/Lz2zFWCo9i8AUpfaivI8I9uXqI=; b=Bm9wijLs2rq5Yxl/png54N+QVwoim/APpDJ9zhAT+QEEyR9yt0kI5kXUX2ZI7+rS41 2FaA69otfRFfThC2mLIYkp8oVXaz4/E15yd6LXZkkzVRRHfa3B6TBM8Nf0YEhIzeSq1K 8252msuORWNRafbuawnlqCiMTjFhAo8KxvA4oPkdZbeNiAtIQ+CQ20BMV6EVL1y77PNB N6rx4J+IPSxzpyVFeGcq1uFuqzdJN0xPYTo/0zBMuyuAJJHFqRjCqaxahlfJD/8W9IQb QkB1uBnosM6K2TJTEVblDt3XRZxxcLnhmghpCAx+uQen0/uBad7SpoPKcTZi9VkaM803 Ex6w== X-Forwarded-Encrypted: i=1; AJvYcCXxZmYm5A4zeT+e8P9Qp9hnabvfeMmSyQsnGUUXFthNmTSehipLwGCHfXOkz9uydlck2OIR9pGgzvBbTDXvOJdOgFjBZlt8q6Bn9esY/Nfl5fUGq1M= X-Gm-Message-State: AOJu0YwUfdcfHXTMKS9m585xg2LKfEmZOi7PvqZ62mzP5Q9dIEiqCyaJ TCNMCKBN8h18bWL5i9M2P2KS7o+QidGLwJa7IFDXWz0ST0TULEvqgUu52VHr3OU= X-Google-Smtp-Source: AGHT+IHaFX784uguRkmMeLyP7wBdJksLN2mrMw6APcJ/cXGZXrdnmicuWEsAAPZo80PWjA0bjfx94w== X-Received: by 2002:a17:906:780b:b0:a3f:f8a7:e1f7 with SMTP id u11-20020a170906780b00b00a3ff8a7e1f7mr5317002ejm.5.1709478092975; Sun, 03 Mar 2024 07:01:32 -0800 (PST) Received: from brgl-uxlite.. ([77.241.232.10]) by smtp.gmail.com with ESMTPSA id ch14-20020a170906c2ce00b00a42ea946917sm3740293ejb.130.2024.03.03.07.01.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 07:01:32 -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 , Arnd Bergmann Cc: linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel@quicinc.com, Bartosz Golaszewski , Deepti Jaggi Subject: [PATCH v8 10/12] firmware: qcom: tzmem: enable SHM Bridge support Date: Sun, 3 Mar 2024 16:01:13 +0100 Message-Id: <20240303150115.133633-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240303150115.133633-1-brgl@bgdev.pl> References: <20240303150115.133633-1-brgl@bgdev.pl> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240303_070135_029612_BB014898 X-CRM114-Status: GOOD ( 18.79 ) 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 SHM Bridge is a safety mechanism allowing to limit the amount of memory shared between the kernel and the TrustZone to regions explicitly marked as such. Add a variant of the tzmem allocator that configures the memory pools as SHM bridges. It also enables the SHM bridge globally so non-SHM bridge memory will no longer work with SCM calls. 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 fall back to the generic mode. Signed-off-by: Bartosz Golaszewski Tested-by: Andrew Halaney # sc8280xp-lenovo-thinkpad-x13s Tested-by: Deepti Jaggi #sa8775p-ride Reviewed-by: Elliot Berman --- 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 4634f8cecc7b..7f6eb4174734 100644 --- a/drivers/firmware/qcom/Kconfig +++ b/drivers/firmware/qcom/Kconfig @@ -28,6 +28,16 @@ config QCOM_TZMEM_MODE_GENERIC Use the generic allocator mode. The memory is page-aligned, non-cachable and physically contiguous. +config QCOM_TZMEM_MODE_SHMBRIDGE + bool "SHM Bridge" + help + Use Qualcomm Shared Memory Bridge. The memory has the same alignment as + in the 'Generic' 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 23f364afa6ca..244615e8c505 100644 --- a/drivers/firmware/qcom/qcom_tzmem.c +++ b/drivers/firmware/qcom/qcom_tzmem.c @@ -67,7 +67,70 @@ static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area) } -#endif /* CONFIG_QCOM_TZMEM_MODE_GENERIC */ +#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_area(struct qcom_tzmem_area *area) +{ + 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)area->paddr | ns_perms; + ipfn_and_s_perm = (u64)area->paddr | ns_perms; + size_and_flags = area->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; + + area->priv = no_free_ptr(handle); + + return 0; +} + +static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area) +{ + u64 *handle = area->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 */ static int qcom_tzmem_pool_add_memory(struct qcom_tzmem_pool *pool, size_t size, gfp_t gfp)