Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2033185lqb; Mon, 27 May 2024 06:00:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWkFATeWBGAuDC4nlIWApkIfmFmp2nstSYNiYP3f7P7hTmSPy4w8EjoXaLfdAn0vKIN81Mhem0BS0+3bgAD7wLpX0KZVoQIl6GZINIu0A== X-Google-Smtp-Source: AGHT+IGrAerHSQOb+1meKFc8hzcKw4uO51n87FNSNGK6y8GJP6ef0OYjbByU/1sIwHXriVh03vl8 X-Received: by 2002:a05:6102:50a8:b0:47c:549c:d6a3 with SMTP id ada2fe7eead31-48a38516f78mr10516324137.2.1716814831031; Mon, 27 May 2024 06:00:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716814831; cv=pass; d=google.com; s=arc-20160816; b=k1r3dP8qLH/Hl/0JS7DL1/BFcNRqncvnr3px7mTDhLWDQbffVKnjnBYUFmCoeXKvjs CzLhp0iOdAhzb8Kcz/uMryGuITCuxYDVSKCQipMmNvWVSWcE1WP7pQFQ24z97k03cPxe KCYoT2PjmUffIu7FaRHGFNeA7IQBrb+7OUI6giDRt9E7y+dnbRDo/D0U1a2IADdXdFBH fgjMQGniIQTcxVc2v+mZp8MndA6SZl1Px41qCLvMk0FNjukPGmX7HAHtKC1Lodi1nLrK v9u22uZ6NRAdocYYddBew5Y21kfCBD7cFuQetrjATnsBnvyLSAOAA+wc9p1ra+YuLaGL Qzdg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=Xb011fqkeryzcbBET/6jqlq0C5NLk6ehH9YotcPeHdY=; fh=bLajTxU/Uw/KAaXruaPuTpHHO0IcupbMXJV8j67EbZs=; b=ETXl9mxVSdUcUpVlZNAwsR8SvmhbNunSUWRGVkK1K2BDtwUAAQVARlQBK6kU81vttS el9k7yu8M+tuoRAYYXpLIL7nftcoKdu/CresW7uFPReIqpV2Tj24JDt4VcVMiTU7f/83 njmt/HEBoQZyBSADHnC8y5oQ4WTJCXZ/vMVR3yG+FOhnu3+oku5EGEsq+g0MfP/QOIXr df1D35cHkA5sBpKE6Uq7Gi3lx3PZD8FHTaM9AMHknC/Tfrq6s3Z4Xw8xs3+i0LHfohoc 5N2lXHYNF3/zzJb6rVGk/oFS4C9pMGe53q/i3ujY8lnHhRlabycpqVYG5lYYwC7JnByB GCvw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=HlFd9aRp; arc=pass (i=1 dkim=pass dkdomain=bgdev-pl.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-190635-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190635-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ada2fe7eead31-48a3a39a4e1si1346585137.579.2024.05.27.06.00.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 06:00:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190635-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20230601.gappssmtp.com header.s=20230601 header.b=HlFd9aRp; arc=pass (i=1 dkim=pass dkdomain=bgdev-pl.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-190635-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190635-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DE6601C22E3E for ; Mon, 27 May 2024 13:00:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF98316130A; Mon, 27 May 2024 12:57:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b="HlFd9aRp" Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A0F515F404 for ; Mon, 27 May 2024 12:56:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716814619; cv=none; b=eTgIr6W/FCcHx+ZlWh3/celu4rEIiEZkvwKq3iXOPVmtQhWMTgxOBgryFpvi3w+xgvvAUccNkRKv+3w453lyLvOxtwvoR2urGuq9Vw9dt0FugNnB6Trs/lHFO0OtD57oFPcIViaXR3bWWW2HBFCkMFqNEze+GfdiQeeii6rf60w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716814619; c=relaxed/simple; bh=xVAP2yUo860qGVVdjxAiMXPUbzaHrXsGq8uq0Y6cr70=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YIMBD4tKBQyn1O+6hLo9NzaN4xDYBmDv4g67MZAFIFa+Tdak+s2axxsn7VzmDaMKUyQbBCtfU0uc07qItksrgvzOSnrBesgW1+nQTAaqzQ6SQkWfTsWZFp7e58/bZCY4EEKZ33DNSPZVmBHWAFstO2QLzaovtMB932KRXu0gQSI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl; spf=none smtp.mailfrom=bgdev.pl; dkim=pass (2048-bit key) header.d=bgdev-pl.20230601.gappssmtp.com header.i=@bgdev-pl.20230601.gappssmtp.com header.b=HlFd9aRp; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=bgdev.pl Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bgdev.pl Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-42108856c33so16448505e9.1 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=vger.kernel.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=HlFd9aRpl8yOYfNwn5CyunJc8UFuK79dHIyKVIpPZBYuK29nDUhwA3Odshwt2tGAbQ 2lJi+dxv6/5KZcgpH2Y7qXXCemqMNwN9Az2feFj1KBHjrTelayhoCoQZTPs4+4di1XIX 5oEqOltxw+3YAyvcEf31b6K9gNAG1/gPmLtT+jSAoB07eUPjHaRmyMLWSfHs7mV/Adox XIgjeE89fLU1H00mqVpCOZ/wGFujKD/VXQQgV0Wx/xb0HLi5vgVZ8/EJNv19F/dQiiLd 3BPniWAMi8axCt4N7+O2fYxqWP9EDXJlH7e8cL6Q2u8fmn/hOyH/pZl85Gw3AaCS8chN RkCQ== 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=l0HKuwAVpysL48XjlecorheunGjF6q0zpUASfziaXo8oK77f4Va5faw6JJqgxtLWGQ FBMp0MGNjZ7M9S/78oOQBPXb3chfTI3Px9msWPCLVXA4UgXByd9IrpYzB9xTOkffXK0w g19HvpkE/z1Cik5h00T//5WXsryR66w8Am9v+PUrh0DIpqhOyt6jrkRDsURfIzeslaGV dYW6DCl4hTqaCIqp2AQQikMail6Z0Gl7wKLS7dbcYsQo/QGIVqW84BLcJRMYt/wXstiq ZDQFdLqiMmPjNDNpiLhME2KLRqTyvcfqz7R+g1HoqW2aG8jzD4Bq3irbQgDk34T+ZgV7 pJ/g== X-Forwarded-Encrypted: i=1; AJvYcCX6xjSuIPEPpGqRx27KE5pNr7fZj3bjeiLFliMr7LCkb4MYKgihF1Ug7r4a/87p9nQVJMpKXaCxgg4waV65hmSkd68DgiEgOOhklYnm X-Gm-Message-State: AOJu0YxDOWL6Gh880Q0Dkg+qW1P+v7qWj/eVG/qEQ+dhXabh4v7UdChC id/VohYJJc/Yx85DWHho22Y3omBMUIFyAM760KkyD9mpdJgQH26Pvgi83VoXbuw= 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 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) -- 2.43.0