From patchwork Mon May 27 12:55:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 13675201 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 AA091C25B7D for ; Mon, 27 May 2024 12:58:24 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=UvQ8qbWIUt4vUx0uBDPS11uJ0NlwEYndYLqbUR7R60s=; b=aOscY8Uw5TGMhr t9Kkz9ce4wdDB1FNz6g2kRemgJJYwy67BFx6MquWeviXE+bF6/jUCUNxeZGFxfJKNMesDwmdVTe19 6yPnPHCMo0WIVD0wm41C4o/ODALhBeRKB/NterXeMlqTQRzK/UhoDfp8JcgQGh9M738nB8jR8WdIf HF6ltz8nUV8gbWgA1MHi7urnktkBaGoojEjVpbWVZbv2Yy4odu0BXfx4DrMsPApsLZKmuJH1rFiHX VAsHAcPjo2sv/wVcU/Kc1IO3F1FZyF7MjVn+DPqBH/b9KuB2x+f1MHpaT2EncCBKERb5nj8VAtlyV Z6FSXzLH7aMH0KBIyogQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sBZva-0000000EyOZ-2Oh5; Mon, 27 May 2024 12:58:14 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sBZuK-0000000Exey-3IKo for linux-arm-kernel@lists.infradead.org; Mon, 27 May 2024 12:56:58 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-42011507a54so31338365e9.0 for ; Mon, 27 May 2024 05:56:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20230601.gappssmtp.com; s=20230601; t=1716814615; x=1717419415; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Xb011fqkeryzcbBET/6jqlq0C5NLk6ehH9YotcPeHdY=; b=LlYYWYIBvwjj6XFldcRgA87sMNi8pWqF3Q7SNdbCTZRxz+/GCvCTBqidoBnKD2Co6G 7YQw6HWSco+6tYm+wmUor9AzimO8uiAHXXZGy8xrTeq8y6GUZVFn4s72nZZJFFpLs0Z+ 1WoQEII7Q8/+4ecOTZc9WnjH+hwizAEa+i+5j+eK1hwAxqVFYZgMjq41z82VmBTCZOBO qevbowZG5FFI5CENxpYUZxogWBew8kATSpB8ACOEfvs+MfCvq6BBuOUtD30B754q2384 GoB/cruwXZKBo+a1N+rCIwKncyWmsYIvj84J7klrpogEZkcRbNpl4dSo2/X1WFgB/w6l Ikpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716814615; x=1717419415; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xb011fqkeryzcbBET/6jqlq0C5NLk6ehH9YotcPeHdY=; b=ZU62q67VBwwb+3hYCrlKEteI1nGNYjNZlwvC/cDQZsYCbtgRSDVFOYVUZfaWCWlPDH Tet8r2ojQv8tEJ9XEZuaGxxfobEqPUNlnUEXvJTESS1eHJZu7jipAY0/WjbnxzRxryX2 SZAFY7g/70aXfvODnykNIUZ0lEwK635qM6UuasgMI2Xu6v2dGxpcCAXXFK4hy9+3Aa2Z +V2l+9X4BcOmojxKpSQZ9qj1c9K2Hk0jpnJDSqOw6Snd8EBVIC7p/S6hmie/YBH5kp5X TWbHgXbG7IwHUBNniQwrGzxakHKa1aK4UnDTt6RStmMmqdoljX9ZDTueH6+Ko6r0nTo0 s/ZA== X-Forwarded-Encrypted: i=1; AJvYcCXGYU8YCFOBFvmKhQoNppvrjUCY4Bnh+XghbLlITe7V51d2Txb4A8iJVD3yON8+A9H5o3TbRaGk9CtRGdUY+iQ+Ejwojd/JhuwIyAPB3lx+NcgZTiU= X-Gm-Message-State: AOJu0Yz3/WDrBch5WA7z/35gZc/51XaPhif87c5Il+P6YMhl5EcgK1cJ su+EfuJ+qGrljBZ8LuYxINKsYm3MUJQfPU5OwN/h1m4vsvVrAZ31hVL3hQgEq+9lH0Og/NsHrEB u X-Google-Smtp-Source: AGHT+IFMm7MJVK46CgCVCnpTDtuqMAFg/I+zhUZQjMTG8tD0AIdgSKeVpj4upyM8cXEOycp43d68vQ== X-Received: by 2002:a05:600c:1913:b0:419:e3b9:b384 with SMTP id 5b1f17b1804b1-421015b4734mr101893285e9.11.1716814615508; Mon, 27 May 2024 05:56:55 -0700 (PDT) Received: from [127.0.1.1] ([2a01:cb1d:75a:e000:c322:131e:ff9d:ef41]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42100f64f98sm138753635e9.24.2024.05.27.05.56.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 05:56:55 -0700 (PDT) From: Bartosz Golaszewski Date: Mon, 27 May 2024 14:55:01 +0200 Subject: [PATCH v10 11/15] firmware: qcom: tzmem: enable SHM Bridge support MIME-Version: 1.0 Message-Id: <20240527-shm-bridge-v10-11-ce7afaa58d3a@linaro.org> References: <20240527-shm-bridge-v10-0-ce7afaa58d3a@linaro.org> In-Reply-To: <20240527-shm-bridge-v10-0-ce7afaa58d3a@linaro.org> To: Bjorn Andersson , Konrad Dybcio , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Robert Marko , Das Srinagesh , Bartosz Golaszewski , Maximilian Luz , Catalin Marinas , Will Deacon , Srini Kandagatla , Arnd Bergmann , Elliot Berman , Alex Elder Cc: linux-arm-msm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kernel@quicinc.com, Andrew Halaney , Deepti Jaggi X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4147; i=bartosz.golaszewski@linaro.org; h=from:subject:message-id; bh=uu55syoIGqSAHT5CWYTbsAIG3EX3ZWdIO4aqT+vkzWw=; b=owEBbQKS/ZANAwAKARGnLqAUcddyAcsmYgBmVIMKq01dcR08z169Y8aOU+wTgVn8nGVClVW3X UsyKRkrcoGJAjMEAAEKAB0WIQQWnetsC8PEYBPSx58Rpy6gFHHXcgUCZlSDCgAKCRARpy6gFHHX cquLEADCF0YY/DACosvnIHqr0PeJpvVfdMjtv0D9JWbTZ3J8PhKl2I7qK16Ghk6K99IPsBx68mb iC7BL2JCLV/pDgMNITVRsMf1UzkOpNtcc/5OtTCO62ViA0KZrws3WSdS/0dtRRnIPQxSZXanY0V tSzQ0+4rVqLz907BSOWPSOahub9zhsya5Jf6azljX1pSe6mZlgpQNmweQqI2VMHm3QXg6992OSO wqafLWC3+oMe+AvD2VjlUAvMA2FlBxkjom1IOA03zhlL5Van9Erw1p6NvpqQ0fWrT7RqrRzE9gP iYfhfz1u8nfzbYMZT0iP9ZxrhcvsqIKjcSOqwvMilpT10mC3kY/hQhKqGtNJcTceDbIApIudKjS vQlW6kgQG8ewP0tWKNy7g3LBQL5xeCZbLqNT7z7GRtvrPx+JWLADwWKyMBcZGoPZhV8lx6+aziz V9Yq1W9K45PgfEW3p7jUOMX0dPFDQu6hup/M55QFbim0H8KGoBYarziICmqUPmetksDlJVJQQi0 8cwEZvchXDmTl5OJMpOJ9HEkSCXFps0nOpFLF8XYS6Dn2qrH1CYXsRexFxRgDToIKmfH3+I0D9y +Q97F3tnSncqZHxunogUBWLwNfMGgiUQ+YSdKnWvIQ4Zv2BizW/969mHottbTutYPTDo/tWe80e WFaQb9xPgWZlIPw== X-Developer-Key: i=bartosz.golaszewski@linaro.org; a=openpgp; fpr=169DEB6C0BC3C46013D2C79F11A72EA01471D772 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240527_055656_959205_0385A91D X-CRM114-Status: GOOD ( 20.24 ) 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 | 79 +++++++++++++++++++++++++++++++++++++- 2 files changed, 88 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 3853385bf215..aab6376cade5 100644 --- a/drivers/firmware/qcom/qcom_tzmem.c +++ b/drivers/firmware/qcom/qcom_tzmem.c @@ -66,7 +66,84 @@ 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 +#include + +#define QCOM_SHM_BRIDGE_NUM_VM_SHIFT 9 + +static bool qcom_tzmem_using_shm_bridge; + +/* List of machines that are known to not support SHM bridge correctly. */ +static const char *const qcom_tzmem_blacklist[] = { + "qcom,sc8180x", + NULL +}; + +static int qcom_tzmem_init(void) +{ + const char *const *platform; + int ret; + + for (platform = qcom_tzmem_blacklist; *platform; platform++) { + if (of_machine_is_compatible(*platform)) + goto notsupp; + } + + ret = qcom_scm_shm_bridge_enable(); + if (ret == -EOPNOTSUPP) + goto notsupp; + + if (!ret) + qcom_tzmem_using_shm_bridge = true; + + return ret; + +notsupp: + dev_info(qcom_tzmem_dev, "SHM Bridge not supported\n"); + return 0; +} + +static int qcom_tzmem_init_area(struct qcom_tzmem_area *area) +{ + u64 pfn_and_ns_perm, ipfn_and_s_perm, size_and_flags; + int ret; + + if (!qcom_tzmem_using_shm_bridge) + return 0; + + pfn_and_ns_perm = (u64)area->paddr | QCOM_SCM_PERM_RW; + ipfn_and_s_perm = (u64)area->paddr | QCOM_SCM_PERM_RW; + 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)