From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (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 C5D2420E702 for ; Fri, 2 May 2025 10:00:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180058; cv=none; b=Pu58fsg5PI73x3HlM+9E+bDTXvRadxICc0R+GG424M1SDY1PRPmxnPRRzRbbhbahyMcrfKjRhzjSfGARfwvd6KxAUgVqiDgS5VIu05pyAK7ZpyqDWNKERiMLjQIYt1ViYfGWQsjSsv7gTBG7tV5+fu7YrXsB5OqK7uVm3LJNlDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180058; c=relaxed/simple; bh=vCWwdAowEJBs6cvWQ7lIUrdUovkUmMtp1i6POol9XM0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YyAzkntVVrOjm/6++vS1fvPWJ/F5ZRL5msRp07Euy+qewC3cQ0peYS0GgRkwG6J3x7VuZMot5q1T+8iC/GZbhomCzWlSqMc4DZkziiIRvSYYtY54ZGKjup2spo9kdtRVTRNjAli2yQdw2wlp9LUt9IVXPKzcLpEZWfhvJyNteKQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=lILZ+Ba4; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="lILZ+Ba4" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-ac25d2b2354so276800866b.1 for ; Fri, 02 May 2025 03:00:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180055; x=1746784855; darn=vger.kernel.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=5ItVLbwXh6klaFZ4DOTZKuJkyifurR/QGuqAr/eTlGU=; b=lILZ+Ba4INSx0smZ4cNKOx16Jw5Um9PE2xGKNsOtjqSdd/B97FLiQZzJJcGhMKQq2V wrCd6Wfqj0Ekey/4awoq9yqHVi3h4A41BG1C0gchOg5irt3wrz/LoMYGIk07FI8ORbL5 ZP6mmPwJLamfHRQqw9qY646pQkruAF6txP4OzgNEKdakqMpeQkKSGD5/gVtMNPSQ4J+Y 7YN/T5dJj6SpykXZtV6ovY6sTndDDEtxdntHOj1KzLyju9BzNTTe2+czhhkV2RmfWECP k2nNc/MKa0IUIHa2dQf+N4FQ006+Fgz4ociY1kb+ptf2V9uIjNGSWBVP5ekMTW/JUMen Z2kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180055; x=1746784855; 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=5ItVLbwXh6klaFZ4DOTZKuJkyifurR/QGuqAr/eTlGU=; b=ExR8YAsG0b9zb1Q/w1YY5LowpCc3XLlWrmUEPDnEk0vFuJN0i+PnLlrnqZV2i3xSBf xOaITPzCHRXIZqKsj0g+D8FgZlceT0xLJ3/YyFwK1IgEC8VdWtJs5HkKGACJVqwIQQbm 2i6+p/A13L2XVi5okzhDNjWbKgSyQqtsJBPRfiju3Kh9mnjbhS7E45dyRm8lpDTY3F38 hha6x67RQVYp1Anvn0EcYH0VEirQeudr6HbKQXDw8ikFpWLj8KiUYgICfFtqI91grJ3M slFS21fqu5SIKrPVP910n0OJton3PCW3Lh0bn4ybjIuvMedPp7ccNtsDkzSoXQBqkS51 GCeQ== X-Gm-Message-State: AOJu0YxZmgkk8xnF7FSjdMn6l7m9tpaapAFCVzgd1LJFciAQWbSRFYxI omtx4Z6Oq3SBTwMO1qgy+N6o20SqAK8X2Y05BtbOTDNoj702r14HU1S63AgdHDf2+musoBU3aN0 XKGE= X-Gm-Gg: ASbGncuZGvEcpNYR+QuS45fNmuhSFuf8RYQJuOE3ef64psEmqBLOD89Ek1ScrjGFhhP XN0ufURgZsOy5QVImcjgaIi8A5ZhbD14GB6Jab27C5q18WESZge9BZJLMX6SVSdnN6j5amR2z1b zds7nSHDVuqJ4jGGKP8bT6eTA7Jt5ehxZleUN39gisivwmpr3FbFaZahdBUdthFu5UKDqpXJowS tF0tnlUnY/bw8Vu8oKhHan/076Jz3+rrKZGWY7o8mqaeWtl0hdvSaEPzB2kwpJVtOZiW4Ohwlzw 97Am5s8KbHG7QwkbAVV+he60sxvuY7u2pZDt8v3QuKUN1HA8ArmrFzSj32eER7r2lE8oEthpKOY SGnb/M/jnLI2ux/2d9w== X-Google-Smtp-Source: AGHT+IHsIhA3Bp5ORflZ8bo2wewMeJPgYiXyKbBXizljuVxA1br3p39Mij4FHprUjRbWMN7T6ANK8g== X-Received: by 2002:a17:907:3f0f:b0:ace:9d35:6987 with SMTP id a640c23a62f3a-ad17ad24ac3mr199033566b.3.1746180054591; Fri, 02 May 2025 03:00:54 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.00.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:00:53 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 01/14] tee: tee_device_alloc(): copy dma_mask from parent device Date: Fri, 2 May 2025 11:59:15 +0200 Message-ID: <20250502100049.1746335-2-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If a parent device is supplied to tee_device_alloc(), copy the dma_mask field into the new device. This avoids future warnings when mapping a DMA-buf for the device. Signed-off-by: Jens Wiklander Reviewed-by: Sumit Garg --- drivers/tee/tee_core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index d113679b1e2d..685afcaa3ea1 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -922,6 +922,8 @@ struct tee_device *tee_device_alloc(const struct tee_de= sc *teedesc, teedev->dev.class =3D &tee_class; teedev->dev.release =3D tee_release_device; teedev->dev.parent =3D dev; + if (dev) + teedev->dev.dma_mask =3D dev->dma_mask; =20 teedev->dev.devt =3D MKDEV(MAJOR(tee_devt), teedev->id); =20 --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 9225A238C35 for ; Fri, 2 May 2025 10:00:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180060; cv=none; b=kDkBaw72hYpUg7xvrVJ2VjzIeOhJRUfDEJbomh1Yyqx5ayH6hxS9F4JQ5k6XsjRGjlRuIeVcPjlmo/y7SjwHSBFn0L2SuQ3fhgrJl/g5ymdJIZxYUKZcunxJneuO2mjmAxQEBkRQMaXQsZJ0ubx2vL5OdSgh0Y1VnohxlshMDVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180060; c=relaxed/simple; bh=ClljJjvyTa+hIt59esq/vRhRnkjP2r10upW6mdBKOtM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mNfDZi/sAvMJcjlmzsqQBW/Kg+68ngKQytfgegD2c8hxMtPo5APypzy3EBBAYk2WOuq8FLNXActHIiVQrZ00qDQBvVdTMFamCaXhCPAT4bOLz6xptbVl86LS6S4lNOCVppt4N486F1Q1J8Xt0aAWHa4/rg6RyJtaLQVoOjt55Xs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=QwXxWk/C; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="QwXxWk/C" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-acb415dd8faso270547766b.2 for ; Fri, 02 May 2025 03:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180056; x=1746784856; darn=vger.kernel.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=5X2I1MlNrc8OtxTMPdYsTmJ1JjM0sba198ISiH4P8wU=; b=QwXxWk/C7ctR0XXAawQ2/OH4u8VX7Iudnsh62++BVJHzaUxM5UVU0h1oZ1Ubgu+nsx 8aWk3m/mQChfqEq0k52fSDrgy8pViDBbfzi9m11A3FJjM86WSOJSa7CAR7TWQ6vcJ+i/ rgPevFNZK5u9CMcOl6w+1FTNAFJvItCiK5eNPhqo4zvqrbO+2xANSGcaOMEwgFr1VlzQ 4nTK092QWCQJeBUZa3qWSeaiA9dJaWDffcez3M/2rON1bV711+oRP0LOgmW1vU2fXnU3 1pJYvENF0zJZUtD53E1MJnISi2uggjPuhwclNHIsfiYxHUXNfIWgovsKkf2lAB3zTUsG dTAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180056; x=1746784856; 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=5X2I1MlNrc8OtxTMPdYsTmJ1JjM0sba198ISiH4P8wU=; b=OnQ3QhaJTtdDEn/nPg2tuhsUT0ogaeK84U1WXJOJcz3KsGR4flo3luw8mhnMw6lW69 8YWdzRZ+lmt42dREGs22V2Lf2T4DMwAvbG506FDM7gpJANtTiCpBzSNYe5iOIyFkbG2e RWLR529S9M1xBAebpYW5fGYgR03SGP4LPKSLn+NJtgNOcFfSg3GnVx/+IrGeI9MFqR5S xg5tNFthe4nLlqN1O/RXTCb/tf0/hxuxJKi8B9WCqFUVwsnPR3n4sPbZLWadPEV3FUaD fHvM7yPBwxjj31DAji/RglocBLfU/9/O6auGYi4AhwdLPKDrQmkPFK3oPna36Ei7Rlr6 zW/w== X-Gm-Message-State: AOJu0Yxv04/E9seKbBS0Imyni2Vk/6SDps8xcKHQm9uDu2wYn/KoliDL dWo6VtNtFtZgQoLkC/MkH6sYogopEgJHpeA+dyfe9zX4MYbmm20uVWWzPrK3enS7yqh1YrqwDXk tO94= X-Gm-Gg: ASbGnctFxpVWqIXhKS8oLeLGgQAK/jxaSIpIWH0nu+z1NpGTycsqM1zNY2ru45toS4J GUimzpLv4lYK7BZVVorBOdRIXYgL9TCI+TvPDJuxo1VFxaJHJr/ILH6NAW7Q+6tbWC1QsZn9sr7 K40z6VgbgTXJxSiXdyG8Hs8JSxqToXCRKWX/c36Hg0etbgq03RDPajzjvT/ZgO7jxgLCU4qWwP1 rMXt4gpVf8VVEjb+CclVdGCl5F/3+4XGeP0PGQ3Vy4pKXodx0h9YPxGmdHgUJpdjkK/GPQa7B1k FkqZpL9W4Yy8vHt/oypONUHO8T9oaVLsuXKgzUNkkdzbLytnS+R7Zz7s528udoD0PTzb7XNBexv +T6dgy3AnQivBWewu2w== X-Google-Smtp-Source: AGHT+IExOoNljerMnP1yPtzh01rTrQJbnwgTFzxXZMgDFfUCD64SgckGTYoDWopJzNJPBPdV4tUdDA== X-Received: by 2002:a17:906:6a23:b0:ace:ed3b:285e with SMTP id a640c23a62f3a-ad17af80380mr236112866b.56.1746180056392; Fri, 02 May 2025 03:00:56 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.00.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:00:55 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 02/14] optee: pass parent device to tee_device_alloc() Date: Fri, 2 May 2025 11:59:16 +0200 Message-ID: <20250502100049.1746335-3-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During probing of the OP-TEE driver, pass the parent device to tee_device_alloc() so the dma_mask of the new devices can be updated accordingly. Signed-off-by: Jens Wiklander Reviewed-by: Sumit Garg --- drivers/tee/optee/ffa_abi.c | 8 ++++---- drivers/tee/optee/smc_abi.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index f3af5666bb11..4ca1d5161b82 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -914,16 +914,16 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) (sec_caps & OPTEE_FFA_SEC_CAP_RPMB_PROBE)) optee->in_kernel_rpmb_routing =3D true; =20 - teedev =3D tee_device_alloc(&optee_ffa_clnt_desc, NULL, optee->pool, - optee); + teedev =3D tee_device_alloc(&optee_ffa_clnt_desc, &ffa_dev->dev, + optee->pool, optee); if (IS_ERR(teedev)) { rc =3D PTR_ERR(teedev); goto err_free_pool; } optee->teedev =3D teedev; =20 - teedev =3D tee_device_alloc(&optee_ffa_supp_desc, NULL, optee->pool, - optee); + teedev =3D tee_device_alloc(&optee_ffa_supp_desc, &ffa_dev->dev, + optee->pool, optee); if (IS_ERR(teedev)) { rc =3D PTR_ERR(teedev); goto err_unreg_teedev; diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index f0c3ac1103bb..165fadd9abc9 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -1691,14 +1691,14 @@ static int optee_probe(struct platform_device *pdev) (sec_caps & OPTEE_SMC_SEC_CAP_RPMB_PROBE)) optee->in_kernel_rpmb_routing =3D true; =20 - teedev =3D tee_device_alloc(&optee_clnt_desc, NULL, pool, optee); + teedev =3D tee_device_alloc(&optee_clnt_desc, &pdev->dev, pool, optee); if (IS_ERR(teedev)) { rc =3D PTR_ERR(teedev); goto err_free_optee; } optee->teedev =3D teedev; =20 - teedev =3D tee_device_alloc(&optee_supp_desc, NULL, pool, optee); + teedev =3D tee_device_alloc(&optee_supp_desc, &pdev->dev, pool, optee); if (IS_ERR(teedev)) { rc =3D PTR_ERR(teedev); goto err_unreg_teedev; --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 0533323C4F8 for ; Fri, 2 May 2025 10:01:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180063; cv=none; b=qxKpx30ypBHZwVN7ImsdM2q8H+/fqNlt4J2lj2yirYe/+HO7MHao+46jT/tU/LzKhTNvJ4ev+7gqvk8jQpERlDSpNVNmfex/O5UmU9pGDYNcs+r3qhfQZdPQckhWxu7LyyG6fbSs10aY4BFZsgH/DLhclmUx3S/12Lqci1bfvOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180063; c=relaxed/simple; bh=+8DfPloRk/O2wbK0yr6MftUtkn5813LmLNGOT0rjEEQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LdSUmzl/aA6d05QI+VJZG5BIzpCB/VewFgtTBzQLlJOR2NKAtrDzGhA79LhIuH3YtqzUaLI6jcX+gnwDVzIB1hhCgQnzpIrfeoxRKYU8EmORia8GBTHmIOc5eafbN7lY3rGYgsqIbNdB9b0/3lVd2AarlT0mimb9yyauyqhUn+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=DOBA8HIM; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="DOBA8HIM" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-ac289147833so325869766b.2 for ; Fri, 02 May 2025 03:01:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180059; x=1746784859; darn=vger.kernel.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=tnKwjJ1FVLv4umx37hI66Bb7z/WCUlz7TSg/JWGf0qo=; b=DOBA8HIMVy25gMP8AYzwzf8gSmi10v+5wbqcAU8PmBXwEp6pyB3+r0NK9Ajev1Gllk hXV55IggR7GoWUxSA1XH1epqGOMKZfXljLYh1n0UZOXVvxwPfEbvhqqGbRry90myhu4r DhEA8yyvrP7UNq0ZZpZIQwEr5jfAzO8m7SSf4aJXQch/w7psn+r2QXa0RCSNYzBIvxlp aErQXNKMEgr1ksDtB4cWNcPLoKY9IUuOhuoGHkRVZv7ef3M0akr4meNrRKZUbSHJM5Gx d5Ox5xalPNqQTrJ55S5DaHlA2tWFwJyGuk3hP53L7x5xEpFiMMTi0xn6WOexsyF98CxW Pk3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180059; x=1746784859; 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=tnKwjJ1FVLv4umx37hI66Bb7z/WCUlz7TSg/JWGf0qo=; b=ktJBR0nbyil4M1xt3ZNdyetXR5wqx76w8c1swmk/QT+xIpwiHL0hVAOowJe8Am4VPG 5dGSe1Gv844j++V6Hy2u4Yi2093RPQCgiNPQjPhEh4lHIjMiai0fBfofJjPaS7oBqYOD UNbRwAoL3Om+TEIGk7s3WF76VLFJMFfIMVQzoKvF4Hv80sSg3xgsnLKwvpZvrUBA5IOz GlxiVAgpqWCeiWbCJLvbsyzNNJoaV1t2+WojpPs7q889fYc5xE+d4l6tCbRpqpA4Rqfm dpQPko0ZhAfQc627yNbH5QZnFXuj1blLwfv/x9fcXKqVCzfDuzw67RB5+3sLMd9aJSuz /HxQ== X-Gm-Message-State: AOJu0Yxtx35DTLgAVoSFS2UzrlP0K14KL7HOeUX4B1K4x8ZyKtsAOi+8 UbGY7uVqR6YVa06ATaJJAbSBB+f4PhLDvISoBS2VOaEuuAV0nFBBT1c6LiJu2AsXzMxgAbeyyam /ac8= X-Gm-Gg: ASbGncuZFvpg7obpn4+2SZpexOOdo2wzK4usFTt7ARcPuBNCx0TSEmrjwOyplSRiUfn oeDWaTwE8+iD8+9VHjhprYZ9JXZLycqvCV5epuIGhQdPdW4/ylZg2ZyfMb/tCJPgRGl9+KpKaPk ry1hTs+or7JN+bQ3Qy63nsJtVfJYAOo+SkVhH6GFGAIxUeRBoonl+M4fmQ/xFrR6WncsKyWjOm3 wNzowLlCCFSD9IrdJQDcA/HQ7kcg/tGhZ1CPEdTjjTejmcWGc2b5+tb2q+a+ab+0CG2jqQHXkWA qpFZtTDn+KL4MV4S1y8BWBrWT+jXUVWqXOQHSY+h5bPp5yIVfTIXqvzFw+0LYGVslWH81SRPVRs iTf8BOvJgMnRVIDDYZA== X-Google-Smtp-Source: AGHT+IFPNMCqsMDxMLuFroAEqxekeWOXdTQGgYJ7MzFxV924FF8gq0hAgCFpltxFY+0PVqvrTlE4hw== X-Received: by 2002:a17:907:c489:b0:acb:baff:fd5f with SMTP id a640c23a62f3a-ad17adabaf8mr210124766b.35.1746180058667; Fri, 02 May 2025 03:00:58 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:00:57 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 03/14] optee: account for direction while converting parameters Date: Fri, 2 May 2025 11:59:17 +0200 Message-ID: <20250502100049.1746335-4-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The OP-TEE backend driver has two internal function pointers to convert between the subsystem type struct tee_param and the OP-TEE type struct optee_msg_param. The conversion is done from one of the types to the other, which is then involved in some operation and finally converted back to the original type. When converting to prepare the parameters for the operation, all fields must be taken into account, but then converting back, it's enough to update only out-values and out-sizes. So, an update_out parameter is added to the conversion functions to tell if all or only some fields must be copied. This is needed in a later patch where it might get confusing when converting back in from_msg_param() callback since an allocated restricted SHM can be using the sec_world_id of the used restricted memory pool and that doesn't translate back well. Signed-off-by: Jens Wiklander --- drivers/tee/optee/call.c | 10 ++-- drivers/tee/optee/ffa_abi.c | 43 +++++++++++++---- drivers/tee/optee/optee_private.h | 42 +++++++++++------ drivers/tee/optee/rpc.c | 31 +++++++++---- drivers/tee/optee/smc_abi.c | 76 +++++++++++++++++++++++-------- 5 files changed, 144 insertions(+), 58 deletions(-) diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index 16eb953e14bb..f1533b894726 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -400,7 +400,8 @@ int optee_open_session(struct tee_context *ctx, export_uuid(msg_arg->params[1].u.octets, &client_uuid); =20 rc =3D optee->ops->to_msg_param(optee, msg_arg->params + 2, - arg->num_params, param); + arg->num_params, param, + false /*!update_out*/); if (rc) goto out; =20 @@ -427,7 +428,8 @@ int optee_open_session(struct tee_context *ctx, } =20 if (optee->ops->from_msg_param(optee, param, arg->num_params, - msg_arg->params + 2)) { + msg_arg->params + 2, + true /*update_out*/)) { arg->ret =3D TEEC_ERROR_COMMUNICATION; arg->ret_origin =3D TEEC_ORIGIN_COMMS; /* Close session again to avoid leakage */ @@ -541,7 +543,7 @@ int optee_invoke_func(struct tee_context *ctx, struct t= ee_ioctl_invoke_arg *arg, msg_arg->cancel_id =3D arg->cancel_id; =20 rc =3D optee->ops->to_msg_param(optee, msg_arg->params, arg->num_params, - param); + param, false /*!update_out*/); if (rc) goto out; =20 @@ -551,7 +553,7 @@ int optee_invoke_func(struct tee_context *ctx, struct t= ee_ioctl_invoke_arg *arg, } =20 if (optee->ops->from_msg_param(optee, param, arg->num_params, - msg_arg->params)) { + msg_arg->params, true /*update_out*/)) { msg_arg->ret =3D TEEC_ERROR_COMMUNICATION; msg_arg->ret_origin =3D TEEC_ORIGIN_COMMS; } diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index 4ca1d5161b82..e4b08cd195f3 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -122,15 +122,21 @@ static int optee_shm_rem_ffa_handle(struct optee *opt= ee, u64 global_id) */ =20 static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *= p, - u32 attr, const struct optee_msg_param *mp) + u32 attr, const struct optee_msg_param *mp, + bool update_out) { struct tee_shm *shm =3D NULL; u64 offs_high =3D 0; u64 offs_low =3D 0; =20 + if (update_out) { + if (attr =3D=3D OPTEE_MSG_ATTR_TYPE_FMEM_INPUT) + return; + goto out; + } + p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr - OPTEE_MSG_ATTR_TYPE_FMEM_INPUT; - p->u.memref.size =3D mp->u.fmem.size; =20 if (mp->u.fmem.global_id !=3D OPTEE_MSG_FMEM_INVALID_GLOBAL_ID) shm =3D optee_shm_from_ffa_handle(optee, mp->u.fmem.global_id); @@ -141,6 +147,8 @@ static void from_msg_param_ffa_mem(struct optee *optee,= struct tee_param *p, offs_high =3D mp->u.fmem.offs_high; } p->u.memref.shm_offs =3D offs_low | offs_high << 32; +out: + p->u.memref.size =3D mp->u.fmem.size; } =20 /** @@ -150,12 +158,14 @@ static void from_msg_param_ffa_mem(struct optee *opte= e, struct tee_param *p, * @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters + * @update_out: update parameter for output only * * Returns 0 on success or <0 on failure */ static int optee_ffa_from_msg_param(struct optee *optee, struct tee_param *params, size_t num_params, - const struct optee_msg_param *msg_params) + const struct optee_msg_param *msg_params, + bool update_out) { size_t n; =20 @@ -166,18 +176,20 @@ static int optee_ffa_from_msg_param(struct optee *opt= ee, =20 switch (attr) { case OPTEE_MSG_ATTR_TYPE_NONE: + if (update_out) + break; p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_NONE; memset(&p->u, 0, sizeof(p->u)); break; case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT: case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT: case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT: - optee_from_msg_param_value(p, attr, mp); + optee_from_msg_param_value(p, attr, mp, update_out); break; case OPTEE_MSG_ATTR_TYPE_FMEM_INPUT: case OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT: case OPTEE_MSG_ATTR_TYPE_FMEM_INOUT: - from_msg_param_ffa_mem(optee, p, attr, mp); + from_msg_param_ffa_mem(optee, p, attr, mp, update_out); break; default: return -EINVAL; @@ -188,10 +200,16 @@ static int optee_ffa_from_msg_param(struct optee *opt= ee, } =20 static int to_msg_param_ffa_mem(struct optee_msg_param *mp, - const struct tee_param *p) + const struct tee_param *p, bool update_out) { struct tee_shm *shm =3D p->u.memref.shm; =20 + if (update_out) { + if (p->attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT) + return 0; + goto out; + } + mp->attr =3D OPTEE_MSG_ATTR_TYPE_FMEM_INPUT + p->attr - TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT; =20 @@ -211,6 +229,7 @@ static int to_msg_param_ffa_mem(struct optee_msg_param = *mp, memset(&mp->u, 0, sizeof(mp->u)); mp->u.fmem.global_id =3D OPTEE_MSG_FMEM_INVALID_GLOBAL_ID; } +out: mp->u.fmem.size =3D p->u.memref.size; =20 return 0; @@ -222,13 +241,15 @@ static int to_msg_param_ffa_mem(struct optee_msg_para= m *mp, * @optee: main service struct * @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays - * @params: subsystem itnernal parameter representation + * @params: subsystem internal parameter representation + * @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_ffa_to_msg_param(struct optee *optee, struct optee_msg_param *msg_params, size_t num_params, - const struct tee_param *params) + const struct tee_param *params, + bool update_out) { size_t n; =20 @@ -238,18 +259,20 @@ static int optee_ffa_to_msg_param(struct optee *optee, =20 switch (p->attr) { case TEE_IOCTL_PARAM_ATTR_TYPE_NONE: + if (update_out) + break; mp->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_NONE; memset(&mp->u, 0, sizeof(mp->u)); break; case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: - optee_to_msg_param_value(mp, p); + optee_to_msg_param_value(mp, p, update_out); break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: - if (to_msg_param_ffa_mem(mp, p)) + if (to_msg_param_ffa_mem(mp, p, update_out)) return -EINVAL; break; default: diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index dc0f355ef72a..20eda508dbac 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -185,10 +185,12 @@ struct optee_ops { bool system_thread); int (*to_msg_param)(struct optee *optee, struct optee_msg_param *msg_params, - size_t num_params, const struct tee_param *params); + size_t num_params, const struct tee_param *params, + bool update_out); int (*from_msg_param)(struct optee *optee, struct tee_param *params, size_t num_params, - const struct optee_msg_param *msg_params); + const struct optee_msg_param *msg_params, + bool update_out); }; =20 /** @@ -316,23 +318,35 @@ void optee_release(struct tee_context *ctx); void optee_release_supp(struct tee_context *ctx); =20 static inline void optee_from_msg_param_value(struct tee_param *p, u32 att= r, - const struct optee_msg_param *mp) + const struct optee_msg_param *mp, + bool update_out) { - p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + - attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; - p->u.value.a =3D mp->u.value.a; - p->u.value.b =3D mp->u.value.b; - p->u.value.c =3D mp->u.value.c; + if (!update_out) + p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + + attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; + + if (attr =3D=3D OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT || + attr =3D=3D OPTEE_MSG_ATTR_TYPE_VALUE_INOUT || !update_out) { + p->u.value.a =3D mp->u.value.a; + p->u.value.b =3D mp->u.value.b; + p->u.value.c =3D mp->u.value.c; + } } =20 static inline void optee_to_msg_param_value(struct optee_msg_param *mp, - const struct tee_param *p) + const struct tee_param *p, + bool update_out) { - mp->attr =3D OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - - TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; - mp->u.value.a =3D p->u.value.a; - mp->u.value.b =3D p->u.value.b; - mp->u.value.c =3D p->u.value.c; + if (!update_out) + mp->attr =3D OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - + TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; + + if (p->attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || + p->attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT || !update_out) { + mp->u.value.a =3D p->u.value.a; + mp->u.value.b =3D p->u.value.b; + mp->u.value.c =3D p->u.value.c; + } } =20 void optee_cq_init(struct optee_call_queue *cq, int thread_count); diff --git a/drivers/tee/optee/rpc.c b/drivers/tee/optee/rpc.c index ebbbd42b0e3e..580e6b9b0606 100644 --- a/drivers/tee/optee/rpc.c +++ b/drivers/tee/optee/rpc.c @@ -63,7 +63,7 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_c= ontext *ctx, } =20 if (optee->ops->from_msg_param(optee, params, arg->num_params, - arg->params)) + arg->params, false /*!update_out*/)) goto bad; =20 for (i =3D 0; i < arg->num_params; i++) { @@ -107,7 +107,8 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee= _context *ctx, } else { params[3].u.value.a =3D msg.len; if (optee->ops->to_msg_param(optee, arg->params, - arg->num_params, params)) + arg->num_params, params, + true /*update_out*/)) arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; else arg->ret =3D TEEC_SUCCESS; @@ -188,6 +189,7 @@ static void handle_rpc_func_cmd_wait(struct optee_msg_a= rg *arg) static void handle_rpc_supp_cmd(struct tee_context *ctx, struct optee *opt= ee, struct optee_msg_arg *arg) { + bool update_out =3D false; struct tee_param *params; =20 arg->ret_origin =3D TEEC_ORIGIN_COMMS; @@ -200,15 +202,21 @@ static void handle_rpc_supp_cmd(struct tee_context *c= tx, struct optee *optee, } =20 if (optee->ops->from_msg_param(optee, params, arg->num_params, - arg->params)) { + arg->params, update_out)) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; goto out; } =20 arg->ret =3D optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params); =20 + /* + * Special treatment for OPTEE_RPC_CMD_SHM_ALLOC since input is a + * value type, but the output is a memref type. + */ + if (arg->cmd !=3D OPTEE_RPC_CMD_SHM_ALLOC) + update_out =3D true; if (optee->ops->to_msg_param(optee, arg->params, arg->num_params, - params)) + params, update_out)) arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; out: kfree(params); @@ -270,7 +278,7 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee= _context *ctx, =20 if (arg->num_params !=3D ARRAY_SIZE(params) || optee->ops->from_msg_param(optee, params, arg->num_params, - arg->params) || + arg->params, false /*!update_out*/) || params[0].attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; return; @@ -280,7 +288,8 @@ static void handle_rpc_func_rpmb_probe_reset(struct tee= _context *ctx, params[0].u.value.b =3D 0; params[0].u.value.c =3D 0; if (optee->ops->to_msg_param(optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true /*update_out*/)) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; return; } @@ -324,7 +333,7 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_= context *ctx, =20 if (arg->num_params !=3D ARRAY_SIZE(params) || optee->ops->from_msg_param(optee, params, arg->num_params, - arg->params) || + arg->params, false /*!update_out*/) || params[0].attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT || params[1].attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; @@ -358,7 +367,8 @@ static void handle_rpc_func_rpmb_probe_next(struct tee_= context *ctx, params[0].u.value.b =3D rdev->descr.capacity; params[0].u.value.c =3D rdev->descr.reliable_wr_count; if (optee->ops->to_msg_param(optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true /*update_out*/)) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; return; } @@ -384,7 +394,7 @@ static void handle_rpc_func_rpmb_frames(struct tee_cont= ext *ctx, =20 if (arg->num_params !=3D ARRAY_SIZE(params) || optee->ops->from_msg_param(optee, params, arg->num_params, - arg->params) || + arg->params, false /*!update_out*/) || params[0].attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT || params[1].attr !=3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; @@ -401,7 +411,8 @@ static void handle_rpc_func_rpmb_frames(struct tee_cont= ext *ctx, goto out; } if (optee->ops->to_msg_param(optee, arg->params, - arg->num_params, params)) { + arg->num_params, params, + true /*update_out*/)) { arg->ret =3D TEEC_ERROR_BAD_PARAMETERS; goto out; } diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 165fadd9abc9..cfdae266548b 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -81,20 +81,26 @@ static int optee_cpuhp_disable_pcpu_irq(unsigned int cp= u) */ =20 static int from_msg_param_tmp_mem(struct tee_param *p, u32 attr, - const struct optee_msg_param *mp) + const struct optee_msg_param *mp, + bool update_out) { struct tee_shm *shm; phys_addr_t pa; int rc; =20 + if (update_out) { + if (attr =3D=3D OPTEE_MSG_ATTR_TYPE_TMEM_INPUT) + return 0; + goto out; + } + p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT; - p->u.memref.size =3D mp->u.tmem.size; shm =3D (struct tee_shm *)(unsigned long)mp->u.tmem.shm_ref; if (!shm) { p->u.memref.shm_offs =3D 0; p->u.memref.shm =3D NULL; - return 0; + goto out; } =20 rc =3D tee_shm_get_pa(shm, 0, &pa); @@ -103,18 +109,25 @@ static int from_msg_param_tmp_mem(struct tee_param *p= , u32 attr, =20 p->u.memref.shm_offs =3D mp->u.tmem.buf_ptr - pa; p->u.memref.shm =3D shm; - +out: + p->u.memref.size =3D mp->u.tmem.size; return 0; } =20 static void from_msg_param_reg_mem(struct tee_param *p, u32 attr, - const struct optee_msg_param *mp) + const struct optee_msg_param *mp, + bool update_out) { struct tee_shm *shm; =20 + if (update_out) { + if (attr =3D=3D OPTEE_MSG_ATTR_TYPE_RMEM_INPUT) + return; + goto out; + } + p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT; - p->u.memref.size =3D mp->u.rmem.size; shm =3D (struct tee_shm *)(unsigned long)mp->u.rmem.shm_ref; =20 if (shm) { @@ -124,6 +137,8 @@ static void from_msg_param_reg_mem(struct tee_param *p,= u32 attr, p->u.memref.shm_offs =3D 0; p->u.memref.shm =3D NULL; } +out: + p->u.memref.size =3D mp->u.rmem.size; } =20 /** @@ -133,11 +148,13 @@ static void from_msg_param_reg_mem(struct tee_param *= p, u32 attr, * @params: subsystem internal parameter representation * @num_params: number of elements in the parameter arrays * @msg_params: OPTEE_MSG parameters + * @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_from_msg_param(struct optee *optee, struct tee_param *par= ams, size_t num_params, - const struct optee_msg_param *msg_params) + const struct optee_msg_param *msg_params, + bool update_out) { int rc; size_t n; @@ -149,25 +166,27 @@ static int optee_from_msg_param(struct optee *optee, = struct tee_param *params, =20 switch (attr) { case OPTEE_MSG_ATTR_TYPE_NONE: + if (update_out) + break; p->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_NONE; memset(&p->u, 0, sizeof(p->u)); break; case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT: case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT: case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT: - optee_from_msg_param_value(p, attr, mp); + optee_from_msg_param_value(p, attr, mp, update_out); break; case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT: case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT: case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT: - rc =3D from_msg_param_tmp_mem(p, attr, mp); + rc =3D from_msg_param_tmp_mem(p, attr, mp, update_out); if (rc) return rc; break; case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT: case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT: case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT: - from_msg_param_reg_mem(p, attr, mp); + from_msg_param_reg_mem(p, attr, mp, update_out); break; =20 default: @@ -178,20 +197,25 @@ static int optee_from_msg_param(struct optee *optee, = struct tee_param *params, } =20 static int to_msg_param_tmp_mem(struct optee_msg_param *mp, - const struct tee_param *p) + const struct tee_param *p, bool update_out) { int rc; phys_addr_t pa; =20 + if (update_out) { + if (p->attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT) + return 0; + goto out; + } + mp->attr =3D OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr - TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT; =20 mp->u.tmem.shm_ref =3D (unsigned long)p->u.memref.shm; - mp->u.tmem.size =3D p->u.memref.size; =20 if (!p->u.memref.shm) { mp->u.tmem.buf_ptr =3D 0; - return 0; + goto out; } =20 rc =3D tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa); @@ -201,19 +225,27 @@ static int to_msg_param_tmp_mem(struct optee_msg_para= m *mp, mp->u.tmem.buf_ptr =3D pa; mp->attr |=3D OPTEE_MSG_ATTR_CACHE_PREDEFINED << OPTEE_MSG_ATTR_CACHE_SHIFT; - +out: + mp->u.tmem.size =3D p->u.memref.size; return 0; } =20 static int to_msg_param_reg_mem(struct optee_msg_param *mp, - const struct tee_param *p) + const struct tee_param *p, bool update_out) { + if (update_out) { + if (p->attr =3D=3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT) + return 0; + goto out; + } + mp->attr =3D OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr - TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT; =20 mp->u.rmem.shm_ref =3D (unsigned long)p->u.memref.shm; - mp->u.rmem.size =3D p->u.memref.size; mp->u.rmem.offs =3D p->u.memref.shm_offs; +out: + mp->u.rmem.size =3D p->u.memref.size; return 0; } =20 @@ -223,11 +255,13 @@ static int to_msg_param_reg_mem(struct optee_msg_para= m *mp, * @msg_params: OPTEE_MSG parameters * @num_params: number of elements in the parameter arrays * @params: subsystem itnernal parameter representation + * @update_out: update parameter for output only * Returns 0 on success or <0 on failure */ static int optee_to_msg_param(struct optee *optee, struct optee_msg_param *msg_params, - size_t num_params, const struct tee_param *params) + size_t num_params, const struct tee_param *params, + bool update_out) { int rc; size_t n; @@ -238,21 +272,23 @@ static int optee_to_msg_param(struct optee *optee, =20 switch (p->attr) { case TEE_IOCTL_PARAM_ATTR_TYPE_NONE: + if (update_out) + break; mp->attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_NONE; memset(&mp->u, 0, sizeof(mp->u)); break; case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: - optee_to_msg_param_value(mp, p); + optee_to_msg_param_value(mp, p, update_out); break; case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: if (tee_shm_is_dynamic(p->u.memref.shm)) - rc =3D to_msg_param_reg_mem(mp, p); + rc =3D to_msg_param_reg_mem(mp, p, update_out); else - rc =3D to_msg_param_tmp_mem(mp, p); + rc =3D to_msg_param_tmp_mem(mp, p, update_out); if (rc) return rc; break; --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 DD4A8238C26 for ; Fri, 2 May 2025 10:01:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180065; cv=none; b=G77oUo/qV5H2+Ki4FChSGDrXssTEokiVEaP7K6jZvvOEoRKO4S1cBxZjVGspDvHYBf4RRaLdroS4nLJqAO7R1OeaGXOW+a8Me4JLkbR3aGtqWjEXqz9KI4GRvnCeEBG8CeUuDhXL2sUTXkp6gq+MMmfGvIBmAahBccxCNTFrDFU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180065; c=relaxed/simple; bh=MLYdamHH6WHXHtmEhN21msS7dBG/z1oyi6pQZSju0Es=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s9ort7h0zJ9Y4fie8ZPb2/VLO7Iag1FiSTKMlRNGye7N1S68Yv31fJA2DqsYoKest3imqw39TsQ12RRR4ArM1ZsrWHWA4AAJxdn5iFzOc61EjRdcb2VNLdqNO0arz6/xBwyb8l/oGnd6yjRzBQIOyT3ewmZmxokGn2wKo6eMm7g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=Z/B6sU2I; arc=none smtp.client-ip=209.85.208.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Z/B6sU2I" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-5edc07c777eso2663818a12.3 for ; Fri, 02 May 2025 03:01:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180061; x=1746784861; darn=vger.kernel.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=5s86D78yiZx5WFd3E3wPyOpz8asivIW+9gMjXmMDlyU=; b=Z/B6sU2IiEh8eW2OlCIAQAluvF3mPO4A848LmdoU2mj1vtLDv+OMprj3vTIV0iw9e8 Z8+D4Z3rUD5dvxuCxdI8NsnydrK2dRfTHFC+eEFOgQpQDpYoJoCo6U7XEdY0RjBlUUg5 3TR8FW2WXPFu0OUjvwK7lhGKgPs+0oaMjL0siJx/NfyDJJxZRxle8oMg8scO4Cd9ghkW KX0rvPGJ7ExWU61Mwu2QYEly3RqCgu4P815c+niF8t7ULK2opf7bEcY0PEz8YeEkLhor R8TPXlRbuKPM4oxBU6gbf00pagIO9ITlb1K4cjXtLNZxpsZhr82DwRxGhmw34xKd3myB 4HLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180061; x=1746784861; 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=5s86D78yiZx5WFd3E3wPyOpz8asivIW+9gMjXmMDlyU=; b=mc5BT5P9P+jhJb8ruMVtzVZSRiBlDdiCwL0IdsLUAbyy6+Nsft2rJR2mrv5sOM4eVG uEBc7KO7U+xTn6U3XOzHPGqWnnG46mqcMrZpg/7emEfPz3j7fgAhu2vbVn/voJnBiK40 0l8CDlTRp7Gt6FBmt2OAYJJ81iPlLY676qTUBz86hSN0IS3MJ7t5o+TU7yGYwtYwa+9l SGz6elA7pboPlZCNI2Yfybxpn3/prCuja7ATqY2Z62kBXB2oiKhaPssnxVZ42V02FmjD XLKJ1YODt5Brjl/yP8E1z6WkOWVpyRr4mj3TJ1XrcbLRQ7H7BiZwxapuot7Mar1M1UQm F1rg== X-Gm-Message-State: AOJu0YwhKtHPP0ZW9FUN0ZKppiYj3svF2JatvxafczPnUHmUbw39ZDCa 0dTxb27V9wNEt+Hfm47kgWJ+bwvA+acZT+7ZR8zJ7HleDlf8p4C55Y59ld4rfglUIFXrFLuKswH gcwk= X-Gm-Gg: ASbGncvZ1lzDISwEHt7rj5qgpN+pjwJH3u7izAKeywejOtIFYUXP8V9WzzEtAY6EawV mgru5tTMtJxEdvSo90ekBkY9Bvlb4NYOszqJ8dYMyY4WKiE0wFfZQekxMjRegKTGWRbO6e1hX2s h9gQprguHUyNF53Axc94eOXjFWgYZ0ha9gZJAlNW5TUcELjYSzTu9fcmZ14YQ92IRKXs3weKYMN wm5A4Bdes2am6IOvC+EUAteCiUSoKLSh/9QWfjjMFiWEkKehYsQuM5DXdTRRs6tAYH+rw9d5mxq AF/tA+Ca+JkV78rsCPaZHFwojN0WgtVR9+CJPegT5XazzEJt6PWnr9v9gXSbIcn6GqK4sOhP1BM ogAdgn5x/h04nI0NFFA== X-Google-Smtp-Source: AGHT+IEpFqp/1tLyV2ua5DVW2AZ1ie6CKGdDrgM8EWE0Juydt2Oj3UgkNdKAP5Gam4hEcvSWIVKhdA== X-Received: by 2002:a17:907:6d14:b0:ac6:fec7:34dd with SMTP id a640c23a62f3a-ad17afbe974mr195667366b.52.1746180060558; Fri, 02 May 2025 03:01:00 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.00.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:00:59 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 04/14] optee: sync secure world ABI headers Date: Fri, 2 May 2025 11:59:18 +0200 Message-ID: <20250502100049.1746335-5-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update the header files describing the secure world ABI, both with and without FF-A. The ABI is extended to deal with protected memory, but as usual backward compatible. Signed-off-by: Jens Wiklander --- drivers/tee/optee/optee_ffa.h | 27 +++++++++--- drivers/tee/optee/optee_msg.h | 83 ++++++++++++++++++++++++++++++----- drivers/tee/optee/optee_smc.h | 69 ++++++++++++++++++++++++++++- 3 files changed, 161 insertions(+), 18 deletions(-) diff --git a/drivers/tee/optee/optee_ffa.h b/drivers/tee/optee/optee_ffa.h index 257735ae5b56..cc257e7956a3 100644 --- a/drivers/tee/optee/optee_ffa.h +++ b/drivers/tee/optee/optee_ffa.h @@ -81,7 +81,7 @@ * as the second MSG arg struct for * OPTEE_FFA_YIELDING_CALL_WITH_ARG. * Bit[31:8]: Reserved (MBZ) - * w5: Bitfield of secure world capabilities OPTEE_FFA_SEC_CAP_* below, + * w5: Bitfield of OP-TEE capabilities OPTEE_FFA_SEC_CAP_* * w6: The maximum secure world notification number * w7: Not used (MBZ) */ @@ -94,6 +94,8 @@ #define OPTEE_FFA_SEC_CAP_ASYNC_NOTIF BIT(1) /* OP-TEE supports probing for RPMB device if needed */ #define OPTEE_FFA_SEC_CAP_RPMB_PROBE BIT(2) +/* OP-TEE supports Protected Memory for secure data path */ +#define OPTEE_FFA_SEC_CAP_PROTMEM BIT(3) =20 #define OPTEE_FFA_EXCHANGE_CAPABILITIES OPTEE_FFA_BLOCKING_CALL(2) =20 @@ -108,7 +110,7 @@ * * Return register usage: * w3: Error code, 0 on success - * w4-w7: Note used (MBZ) + * w4-w7: Not used (MBZ) */ #define OPTEE_FFA_UNREGISTER_SHM OPTEE_FFA_BLOCKING_CALL(3) =20 @@ -119,16 +121,31 @@ * Call register usage: * w3: Service ID, OPTEE_FFA_ENABLE_ASYNC_NOTIF * w4: Notification value to request bottom half processing, should be - * less than OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE. + * less than OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE * w5-w7: Not used (MBZ) * * Return register usage: * w3: Error code, 0 on success - * w4-w7: Note used (MBZ) + * w4-w7: Not used (MBZ) */ #define OPTEE_FFA_ENABLE_ASYNC_NOTIF OPTEE_FFA_BLOCKING_CALL(5) =20 -#define OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE 64 +#define OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE 64 + +/* + * Release Protected memory + * + * Call register usage: + * w3: Service ID, OPTEE_FFA_RECLAIM_PROTMEM + * w4: Shared memory handle, lower bits + * w5: Shared memory handle, higher bits + * w6-w7: Not used (MBZ) + * + * Return register usage: + * w3: Error code, 0 on success + * w4-w7: Note used (MBZ) + */ +#define OPTEE_FFA_RELEASE_PROTMEM OPTEE_FFA_BLOCKING_CALL(8) =20 /* * Call with struct optee_msg_arg as argument in the supplied shared memory diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h index e8840a82b983..22d71d6f110d 100644 --- a/drivers/tee/optee/optee_msg.h +++ b/drivers/tee/optee/optee_msg.h @@ -133,13 +133,13 @@ struct optee_msg_param_rmem { }; =20 /** - * struct optee_msg_param_fmem - ffa memory reference parameter + * struct optee_msg_param_fmem - FF-A memory reference parameter * @offs_lower: Lower bits of offset into shared memory reference * @offs_upper: Upper bits of offset into shared memory reference * @internal_offs: Internal offset into the first page of shared memory * reference * @size: Size of the buffer - * @global_id: Global identifier of Shared memory + * @global_id: Global identifier of the shared memory */ struct optee_msg_param_fmem { u32 offs_low; @@ -165,7 +165,7 @@ struct optee_msg_param_value { * @attr: attributes * @tmem: parameter by temporary memory reference * @rmem: parameter by registered memory reference - * @fmem: parameter by ffa registered memory reference + * @fmem: parameter by FF-A registered memory reference * @value: parameter by opaque value * @octets: parameter by octet string * @@ -296,6 +296,18 @@ struct optee_msg_arg { */ #define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001 =20 +/* + * Values used in OPTEE_MSG_CMD_LEND_PROTMEM below + * OPTEE_MSG_PROTMEM_RESERVED Reserved + * OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY Secure Video Playback + * OPTEE_MSG_PROTMEM_TRUSTED_UI Trused UI + * OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD Secure Video Recording + */ +#define OPTEE_MSG_PROTMEM_RESERVED 0 +#define OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY 1 +#define OPTEE_MSG_PROTMEM_TRUSTED_UI 2 +#define OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD 3 + /* * Do a secure call with struct optee_msg_arg as argument * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd @@ -337,15 +349,62 @@ struct optee_msg_arg { * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is * normal world unable to process asynchronous notifications. Typically * used when the driver is shut down. + * + * OPTEE_MSG_CMD_LEND_PROTMEM lends protected memory. The passed normal + * physical memory is protected from normal world access. The memory + * should be unmapped prior to this call since it becomes inaccessible + * during the request. + * Parameters are passed as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + * [in] param[0].u.value.a OPTEE_MSG_PROTMEM_* defined above + * [in] param[1].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + * [in] param[1].u.tmem.buf_ptr physical address + * [in] param[1].u.tmem.size size + * [in] param[1].u.tmem.shm_ref holds protected memory reference + * + * OPTEE_MSG_CMD_RECLAIM_PROTMEM reclaims a previously lent protected + * memory reference. The physical memory is accessible by the normal world + * after this function has return and can be mapped again. The information + * is passed as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + * [in] param[0].u.value.a holds protected memory cookie + * + * OPTEE_MSG_CMD_GET_PROTMEM_CONFIG get configuration for a specific + * protected memory use case. Parameters are passed as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INOUT + * [in] param[0].value.a OPTEE_MSG_PROTMEM_* + * [in] param[1].attr OPTEE_MSG_ATTR_TYPE_{R,F}MEM_OUTPUT + * [in] param[1].u.{r,f}mem Buffer or NULL + * [in] param[1].u.{r,f}mem.size Provided size of buffer or 0 for query + * output for the protected use case: + * [out] param[0].value.a Minimal size of protected memory + * [out] param[0].value.b Required alignment of size and start of + * protected memory + * [out] param[1].{r,f}mem.size Size of output data + * [out] param[1].{r,f}mem If non-NULL, contains an array of + * uint16_t holding endpoints that + * must be included when lending + * memory for this use case + * + * OPTEE_MSG_CMD_ASSIGN_PROTMEM assigns use-case to protected memory + * previously lent using the FFA_LEND framework ABI. Parameters are passed + * as: + * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + * [in] param[0].u.value.a holds protected memory cookie + * [in] param[0].u.value.b OPTEE_MSG_PROTMEM_* defined above */ -#define OPTEE_MSG_CMD_OPEN_SESSION 0 -#define OPTEE_MSG_CMD_INVOKE_COMMAND 1 -#define OPTEE_MSG_CMD_CLOSE_SESSION 2 -#define OPTEE_MSG_CMD_CANCEL 3 -#define OPTEE_MSG_CMD_REGISTER_SHM 4 -#define OPTEE_MSG_CMD_UNREGISTER_SHM 5 -#define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6 -#define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7 -#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004 +#define OPTEE_MSG_CMD_OPEN_SESSION 0 +#define OPTEE_MSG_CMD_INVOKE_COMMAND 1 +#define OPTEE_MSG_CMD_CLOSE_SESSION 2 +#define OPTEE_MSG_CMD_CANCEL 3 +#define OPTEE_MSG_CMD_REGISTER_SHM 4 +#define OPTEE_MSG_CMD_UNREGISTER_SHM 5 +#define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6 +#define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7 +#define OPTEE_MSG_CMD_LEND_PROTMEM 8 +#define OPTEE_MSG_CMD_RECLAIM_PROTMEM 9 +#define OPTEE_MSG_CMD_GET_PROTMEM_CONFIG 10 +#define OPTEE_MSG_CMD_ASSIGN_PROTMEM 11 +#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004 =20 #endif /* _OPTEE_MSG_H */ diff --git a/drivers/tee/optee/optee_smc.h b/drivers/tee/optee/optee_smc.h index 879426300821..8d3a22bafbdd 100644 --- a/drivers/tee/optee/optee_smc.h +++ b/drivers/tee/optee/optee_smc.h @@ -264,7 +264,6 @@ struct optee_smc_get_shm_config_result { #define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM BIT(0) /* Secure world can communicate via previously unregistered shared memory = */ #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM BIT(1) - /* * Secure world supports commands "register/unregister shared memory", * secure world accepts command buffers located in any parts of non-secure= RAM @@ -280,6 +279,10 @@ struct optee_smc_get_shm_config_result { #define OPTEE_SMC_SEC_CAP_RPC_ARG BIT(6) /* Secure world supports probing for RPMB device if needed */ #define OPTEE_SMC_SEC_CAP_RPMB_PROBE BIT(7) +/* Secure world supports protected memory */ +#define OPTEE_SMC_SEC_CAP_PROTMEM BIT(8) +/* Secure world supports dynamic protected memory */ +#define OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM BIT(9) =20 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \ @@ -451,6 +454,70 @@ struct optee_smc_disable_shm_cache_result { =20 /* See OPTEE_SMC_CALL_WITH_REGD_ARG above */ #define OPTEE_SMC_FUNCID_CALL_WITH_REGD_ARG 19 +/* + * Get protected memory config + * + * Returns the protected memory config. + * + * Call register usage: + * a0 SMC Function ID, OPTEE_SMC_GET_PROTMEM_CONFIG + * a2-6 Not used, must be zero + * a7 Hypervisor Client ID register + * + * Have config return register usage: + * a0 OPTEE_SMC_RETURN_OK + * a1 Physical address of start of protected memory + * a2 Size of protected memory + * a3 Not used + * a4-7 Preserved + * + * Not available register usage: + * a0 OPTEE_SMC_RETURN_ENOTAVAIL + * a1-3 Not used + * a4-7 Preserved + */ +#define OPTEE_SMC_FUNCID_GET_PROTMEM_CONFIG 20 +#define OPTEE_SMC_GET_PROTMEM_CONFIG \ + OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_PROTMEM_CONFIG) + +struct optee_smc_get_protmem_config_result { + unsigned long status; + unsigned long start; + unsigned long size; +}; + +/* + * Get dynamic protected memory config + * + * Returns the dynamic protected memory config. + * + * Call register usage: + * a0 SMC Function ID, OPTEE_SMC_GET_DYN_PROTMEM_CONFIG + * a2-6 Not used, must be zero + * a7 Hypervisor Client ID register + * + * Have config return register usage: + * a0 OPTEE_SMC_RETURN_OK + * a1 Minimal size of protected memory + * a2 Required alignment of size and start of registered protected memory + * a3 Not used + * a4-7 Preserved + * + * Not available register usage: + * a0 OPTEE_SMC_RETURN_ENOTAVAIL + * a1-3 Not used + * a4-7 Preserved + */ + +#define OPTEE_SMC_FUNCID_GET_DYN_PROTMEM_CONFIG 21 +#define OPTEE_SMC_GET_DYN_PROTMEM_CONFIG \ + OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_DYN_PROTMEM_CONFIG) + +struct optee_smc_get_dyn_protmem_config_result { + unsigned long status; + unsigned long size; + unsigned long align; +}; =20 /* * Resume from RPC (for example after processing a foreign interrupt) --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) (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 99D7723184A for ; Fri, 2 May 2025 10:01:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180066; cv=none; b=fETH1fUq+Q6aFiB62WskT22LIqNvnslZU7J1MOinQO06kYO/shCjrKYYaThX4/wYuugl4a25P1vHnuw65SJ9e9PUyNzB5hVFGQQJ4HACirqc638I7EmtC3Yq7QeLn7jkqpu+vD64obxj0uz87aBuY+q2mDIYt7OjoMxD8IQbDBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180066; c=relaxed/simple; bh=UxN93SvSHVTP+m+eEEKFbkfsohK3RhjYDOa6gbczcdk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QwjrG5Od8iafNFHntbdLrMCS+PugNry5fBlyMwgNsKMRonfy3BnA8LkUV32Gue7s9XBxJqrUyuzqVa2n7/FlXGJosKH0Le3pSFLsf2zmjP7an0PFOwslK05huURLds5XXYxRthp5eDhsnyeLSZUj6PVa8ErHxRfLaKE8J55+FtM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=yjdTQHFm; arc=none smtp.client-ip=209.85.218.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="yjdTQHFm" Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-ac345bd8e13so303799166b.0 for ; Fri, 02 May 2025 03:01:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180062; x=1746784862; darn=vger.kernel.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=t+mAUVqOxIXjZijXnkHFZmAFDQtFJ4Kh5PO6PLUJhLU=; b=yjdTQHFmHGQrvTur2lR8v1utuu7A+8xYJpLWbDApR+nASaJaaM3aaI4NZcHOiN+J93 G6AA2e6gaf+EhTbyChOHTqF9Cc8b0c/AFsUZOMkf5g6pvHUFedkGliwAoUUQ5pKrpJAM XL4vvABecCyiQKcHOdDl4xbaZ90zK66I3U9W3ATsTohvKpyd6MdNm4mqM0LIwS8AvTCH LbEoF4PKuCYy0yU+y5MdDAUv26UxBk63r4EMMFSXngch5WsbUcO6bJvdkVjHFefXbA5L 7fFrG7PnX807WKF+bfAQ0ayZ/ABwexDQUt72n5UYXOXf+ynj3mu1JvYMqnTKQwZCFtvR 9KUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180062; x=1746784862; 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=t+mAUVqOxIXjZijXnkHFZmAFDQtFJ4Kh5PO6PLUJhLU=; b=xVAC1PdEykgWaIkRjXUc8EPrR5/sHUs3+KaYuhHKK6eB/YuarAurBqQ0GjFyx2+pYa L8Em3mOn8YLkTnLgRc5xdiJp1P4U0cVdlYjoehdh/V2kNpw/WnB3kFcwOkDJDzNA5Jad zjIkhjsrJ9SYNurh2oETyZ1xoDwO9V9DU1a/oWBj9Z8lztixTbXoaWvj+lBGHHno7mqF zX5k2ERXzHvnCblnXqRLRV2QDrTHPYgL/13JHIixmYfA7Xo9FX6d/nxx97KnSJAHVNpQ 15rQ1ZJmwfOv0vgt26JbkurslEEY/sCrODhdgs8yOqaY90+AYCVCSjwzwCnc+J3rb1mO k91w== X-Gm-Message-State: AOJu0YwdkLYIPLOhj4KjkAPXzbwl2vba/8QuvsF2PQZcUn4Dh31Rwewx kiEPDC5BYlAmhcE9ZwZ2MLFkyjbzxtpRbasHnXCRtIjsfS7QzNGKAEsfLJA/C9+0TLKp2PlysgV 8an8= X-Gm-Gg: ASbGncuU7ukx3LejOyQN61tqqIdBpvO3+duXEKTLYsLXbtqGm2A3nG6YUokYK8TQctg /NmBoYtqEicPqj7/uq4NQgmBQ9iijCoDpZborlSXadvih/Lxqq0+wF4WQRyGYqByontlFdEchd2 Kc6tUP9T2Cy0MFHRpBg4cJFA9fATjG0ItY2CyW2mBt6xzAu2QYo3i2kGxgDO/ovNop1IFwGZ21r tDDBAKgZj4MhK5cRrIGkQBwaasHdCpYoNeW9ObDxYDYA30exDhR4ExMMAR6hCVI8bXS7Mrpt4n/ gkjK/ERXBE6ueIvEyFqI8ENWs57tF08afLgxPrdx0/qVw9BnjlIinWOBRa4m4BMLstsej6mm47S afIdyQ+F6VJyCehetVg== X-Google-Smtp-Source: AGHT+IF8JUCWliZjcbeUSHN6uSsFMCJc6NTPRM+5qGy9ZTGCC1yXzVdzLyehffDilJirBntp5oHCOw== X-Received: by 2002:a17:907:9411:b0:abf:fb78:673a with SMTP id a640c23a62f3a-ad17adaee50mr226144666b.29.1746180062402; Fri, 02 May 2025 03:01:02 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:01 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 05/14] dma-buf: dma-heap: export declared functions Date: Fri, 2 May 2025 11:59:19 +0200 Message-ID: <20250502100049.1746335-6-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Export the dma-buf heap functions declared in . Signed-off-by: Jens Wiklander --- drivers/dma-buf/dma-heap.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 3cbe87d4a464..cdddf0e24dce 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -202,6 +202,7 @@ void *dma_heap_get_drvdata(struct dma_heap *heap) { return heap->priv; } +EXPORT_SYMBOL(dma_heap_get_drvdata); =20 /** * dma_heap_get_name - get heap name @@ -214,6 +215,7 @@ const char *dma_heap_get_name(struct dma_heap *heap) { return heap->name; } +EXPORT_SYMBOL(dma_heap_get_name); =20 /** * dma_heap_add - adds a heap to dmabuf heaps @@ -303,6 +305,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_exp= ort_info *exp_info) kfree(heap); return err_ret; } +EXPORT_SYMBOL(dma_heap_add); =20 static char *dma_heap_devnode(const struct device *dev, umode_t *mode) { --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 AAD45241684 for ; Fri, 2 May 2025 10:01:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180069; cv=none; b=YvTS/8Q1Int3FcgAg6ZyfQCF95tIS1QmXAKpp9HJxuq7iUnUanQaMTJMzrDNE7N6sjH7iUh2YYUSnHS0MDvDQE+XByJEJMLys9BDnrc78sg2I4EcMRYjdsJAqBwDQ/ppmnNGi20bdbyZLNOcmlCEoJKG/zfToWeHY5wJwi/rYb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180069; c=relaxed/simple; bh=+S0KxwNOG+BydqUmor4U5BrwDGhG6gIgSlENnHbYeSg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZMcX+VrJfFJEYv8lL+2gcaT5cz3/b4+sWvzyJr+yOcaOzbuA1nBu7qcyxwbFT/PrPld6R9FSnuexyFIm9mbaz1kNhqjMvwVf/4c/v/3ESh6+dysdxdOnwAKwdYNiQFt2+QH3D/cNYZ4txHT0PBFFhf23hw7yqcUAwsLbYaJPD4A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=c6ojVmFc; arc=none smtp.client-ip=209.85.208.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="c6ojVmFc" Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-5f4d0da2d2cso3577431a12.3 for ; Fri, 02 May 2025 03:01:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180064; x=1746784864; darn=vger.kernel.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=f5jhiqA1zG29Lgsbk1e3tgzIZj3BB1weGWEg1Dsr52w=; b=c6ojVmFcvTQDZ0iswRu/sDfqTUW+bIZZQVRhnBlrVAe+zZB+L78XvfN+pQce8G0zpE 9kc0MkbVQP5EZBWLQhkNqtsr5DeBItxhE5sGBcHuh1vxJSgoOvetP8GRwTYDyI2XJXlS jxmj9QB76UzKKOhRODV3gFMU00lOQfwe/UjyWfmN30N11Xih/bB69unVGSDeCGfGcTrT cCPdV9M7bddbLfqTspvs6VZJcx0K9IoUrdJ3qKttqSm52RRVgkBQc5sW/ZdzOcQpHE4E iZbup60tQiJz/cyCAoyw1j2LTlE5K3oWPLZskHNMV0XX8DrTONuOYYyoQ6ESl9HgJTol fjLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180064; x=1746784864; 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=f5jhiqA1zG29Lgsbk1e3tgzIZj3BB1weGWEg1Dsr52w=; b=IUn/g+kWK7fQdJJ1ZeE9emZVBey4ADqMGRehTQU5EoqKBVAdUFFtC20rKOmZcY6ck1 e9qc9q/hhwP5wWiyavazNCQhOe+VAMGd+2Xde0x7RhDlqG/BWpQoKepYbdOktb/TieE/ IDD722MCSzJta49LzgcCnKfj0LROg85j0jemiphMnhTwwntrhnk57yGUXo7aVk78A0ew Gz++1EGyh5g+kjiTiA6xkzHuLY9gD7wW8h5CMTsBbYEMcDhJTHw/hBkh+qJF6FefXvnt g4TYPS0JfmGhe3nktAHPF69tXD72ZDWMoBr0KGNQeVb7VLr1MFNJ9zZyS4Xs9B/i6Hmd VrwQ== X-Gm-Message-State: AOJu0Yxi9kYZd1r+3fGRsfQ/7gYK68U4nMfAPg1U37gvFYdXj09ju2sF TBCaab8AZHoH/NIrqFMdAywyZK838M9CqTaj3HoBP9W9iNnrMmb+M8c0EPui0wYKAg2KgO7oFQB Cu2M= X-Gm-Gg: ASbGncvGpSRO2EjDmCBy/GhKkjlTTbSNnR/gWySq7ibfveL76HD3crUKrO8F/MO+R7z DiSbCjYuK9iBA81rPTcb7MKfACAATTBsMii/Qql1btNc7DWe1RQjJ6yhpdKBSBqSVTRVkpYKiF6 L9O760M316UDt+1QduD8DfrEr3ZfqPPeMdzzJLQcTlSp8IWif84SOdy934cBTALuJQBgveP3pPU t2sdCqh4G3hvwzVzWQnHUbl7I1GeJ6b1APYrQwwt4IrW1MSErmK5MIoQAgipzpPB9NIPQtvQQKN +ao3s2KApLKf+8BfV6PPWPATw6yR4nb+lmcfFxhfjuVI6amrG0ELi4uDGa+Wiofiq1+KQUV12Fg DWlRHDrqfTsfDUtBJtQ== X-Google-Smtp-Source: AGHT+IH4tfnvhjJC6foaVXRAQB4ztRnFhg1yI0XSDv0B7be9RXR5Lh1kGm9yySiWtepeWwHfj9zmLA== X-Received: by 2002:a17:907:7da9:b0:ac1:dfab:d38e with SMTP id a640c23a62f3a-ad17ad44563mr219961666b.15.1746180064318; Fri, 02 May 2025 03:01:04 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:03 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 06/14] tee: implement protected DMA-heap Date: Fri, 2 May 2025 11:59:20 +0200 Message-ID: <20250502100049.1746335-7-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement DMA heap for protected DMA-buf allocation in the TEE subsystem. Restricted memory refers to memory buffers behind a hardware enforced firewall. It is not accessible to the kernel during normal circumstances but rather only accessible to certain hardware IPs or CPUs executing in higher or differently privileged mode than the kernel itself. This interface allows to allocate and manage such protected memory buffers via interaction with a TEE implementation. The protected memory is allocated for a specific use-case, like Secure Video Playback, Trusted UI, or Secure Video Recording where certain hardware devices can access the memory. The DMA-heaps are enabled explicitly by the TEE backend driver. The TEE backend drivers needs to implement protected memory pool to manage the protected memory. Signed-off-by: Jens Wiklander --- drivers/tee/Makefile | 1 + drivers/tee/tee_heap.c | 470 ++++++++++++++++++++++++++++++++++++++ drivers/tee/tee_private.h | 6 + include/linux/tee_core.h | 65 ++++++ 4 files changed, 542 insertions(+) create mode 100644 drivers/tee/tee_heap.c diff --git a/drivers/tee/Makefile b/drivers/tee/Makefile index 5488cba30bd2..949a6a79fb06 100644 --- a/drivers/tee/Makefile +++ b/drivers/tee/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_TEE) +=3D tee.o tee-objs +=3D tee_core.o +tee-objs +=3D tee_heap.o tee-objs +=3D tee_shm.o tee-objs +=3D tee_shm_pool.o obj-$(CONFIG_OPTEE) +=3D optee/ diff --git a/drivers/tee/tee_heap.c b/drivers/tee/tee_heap.c new file mode 100644 index 000000000000..d91f0b6be760 --- /dev/null +++ b/drivers/tee/tee_heap.c @@ -0,0 +1,470 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2025, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tee_private.h" + +struct tee_dma_heap { + struct dma_heap *heap; + enum tee_dma_heap_id id; + struct tee_protmem_pool *pool; + struct tee_device *teedev; + /* Protects pool and teedev above */ + struct mutex mu; +}; + +struct tee_heap_buffer { + struct tee_protmem_pool *pool; + struct tee_device *teedev; + size_t size; + size_t offs; + struct sg_table table; +}; + +struct tee_heap_attachment { + struct sg_table table; + struct device *dev; +}; + +struct tee_protmem_static_pool { + struct tee_protmem_pool pool; + struct gen_pool *gen_pool; + phys_addr_t pa_base; +}; + +#if IS_ENABLED(CONFIG_DMABUF_HEAPS) +static DEFINE_XARRAY_ALLOC(tee_dma_heap); + +static int copy_sg_table(struct sg_table *dst, struct sg_table *src) +{ + struct scatterlist *dst_sg; + struct scatterlist *src_sg; + int ret; + int i; + + ret =3D sg_alloc_table(dst, src->orig_nents, GFP_KERNEL); + if (ret) + return ret; + + dst_sg =3D dst->sgl; + for_each_sgtable_sg(src, src_sg, i) { + sg_set_page(dst_sg, sg_page(src_sg), src_sg->length, + src_sg->offset); + dst_sg =3D sg_next(dst_sg); + } + + return 0; +} + +static int tee_heap_attach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct tee_heap_buffer *buf =3D dmabuf->priv; + struct tee_heap_attachment *a; + int ret; + + a =3D kzalloc(sizeof(*a), GFP_KERNEL); + if (!a) + return -ENOMEM; + + ret =3D copy_sg_table(&a->table, &buf->table); + if (ret) { + kfree(a); + return ret; + } + + a->dev =3D attachment->dev; + attachment->priv =3D a; + + return 0; +} + +static void tee_heap_detach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct tee_heap_attachment *a =3D attachment->priv; + + sg_free_table(&a->table); + kfree(a); +} + +static struct sg_table * +tee_heap_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) +{ + struct tee_heap_attachment *a =3D attachment->priv; + int ret; + + ret =3D dma_map_sgtable(attachment->dev, &a->table, direction, + DMA_ATTR_SKIP_CPU_SYNC); + if (ret) + return ERR_PTR(ret); + + return &a->table; +} + +static void tee_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, + struct sg_table *table, + enum dma_data_direction direction) +{ + struct tee_heap_attachment *a =3D attachment->priv; + + WARN_ON(&a->table !=3D table); + + dma_unmap_sgtable(attachment->dev, table, direction, + DMA_ATTR_SKIP_CPU_SYNC); +} + +static void tee_heap_buf_free(struct dma_buf *dmabuf) +{ + struct tee_heap_buffer *buf =3D dmabuf->priv; + struct tee_device *teedev =3D buf->teedev; + + buf->pool->ops->free(buf->pool, &buf->table); + tee_device_put(teedev); +} + +static const struct dma_buf_ops tee_heap_buf_ops =3D { + .attach =3D tee_heap_attach, + .detach =3D tee_heap_detach, + .map_dma_buf =3D tee_heap_map_dma_buf, + .unmap_dma_buf =3D tee_heap_unmap_dma_buf, + .release =3D tee_heap_buf_free, +}; + +static struct dma_buf *tee_dma_heap_alloc(struct dma_heap *heap, + unsigned long len, u32 fd_flags, + u64 heap_flags) +{ + struct tee_dma_heap *h =3D dma_heap_get_drvdata(heap); + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct tee_device *teedev =3D NULL; + struct tee_heap_buffer *buf; + struct tee_protmem_pool *pool; + struct dma_buf *dmabuf; + int rc; + + mutex_lock(&h->mu); + if (tee_device_get(h->teedev)) { + teedev =3D h->teedev; + pool =3D h->pool; + } + mutex_unlock(&h->mu); + + if (!teedev) + return ERR_PTR(-EINVAL); + + buf =3D kzalloc(sizeof(*buf), GFP_KERNEL); + if (!buf) { + dmabuf =3D ERR_PTR(-ENOMEM); + goto err; + } + buf->size =3D len; + buf->pool =3D pool; + buf->teedev =3D teedev; + + rc =3D pool->ops->alloc(pool, &buf->table, len, &buf->offs); + if (rc) { + dmabuf =3D ERR_PTR(rc); + goto err_kfree; + } + + exp_info.ops =3D &tee_heap_buf_ops; + exp_info.size =3D len; + exp_info.priv =3D buf; + exp_info.flags =3D fd_flags; + dmabuf =3D dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) + goto err_protmem_free; + + return dmabuf; + +err_protmem_free: + pool->ops->free(pool, &buf->table); +err_kfree: + kfree(buf); +err: + tee_device_put(h->teedev); + return dmabuf; +} + +static const struct dma_heap_ops tee_dma_heap_ops =3D { + .allocate =3D tee_dma_heap_alloc, +}; + +static const char *heap_id_2_name(enum tee_dma_heap_id id) +{ + switch (id) { + case TEE_DMA_HEAP_SECURE_VIDEO_PLAY: + return "protected,secure-video"; + case TEE_DMA_HEAP_TRUSTED_UI: + return "protected,trusted-ui"; + case TEE_DMA_HEAP_SECURE_VIDEO_RECORD: + return "protected,secure-video-record"; + default: + return NULL; + } +} + +static int alloc_dma_heap(struct tee_device *teedev, enum tee_dma_heap_id = id, + struct tee_protmem_pool *pool) +{ + struct dma_heap_export_info exp_info =3D { + .ops =3D &tee_dma_heap_ops, + .name =3D heap_id_2_name(id), + }; + struct tee_dma_heap *h; + int rc; + + if (!exp_info.name) + return -EINVAL; + + if (xa_reserve(&tee_dma_heap, id, GFP_KERNEL)) { + if (!xa_load(&tee_dma_heap, id)) + return -EEXIST; + return -ENOMEM; + } + + h =3D kzalloc(sizeof(*h), GFP_KERNEL); + if (!h) + return -ENOMEM; + h->id =3D id; + h->teedev =3D teedev; + h->pool =3D pool; + mutex_init(&h->mu); + + exp_info.priv =3D h; + h->heap =3D dma_heap_add(&exp_info); + if (IS_ERR(h->heap)) { + rc =3D PTR_ERR(h->heap); + kfree(h); + + return rc; + } + + /* "can't fail" due to the call to xa_reserve() above */ + return WARN(xa_store(&tee_dma_heap, id, h, GFP_KERNEL), + "xa_store() failed"); +} + +int tee_device_register_dma_heap(struct tee_device *teedev, + enum tee_dma_heap_id id, + struct tee_protmem_pool *pool) +{ + struct tee_dma_heap *h; + int rc; + + h =3D xa_load(&tee_dma_heap, id); + if (h) { + mutex_lock(&h->mu); + if (h->teedev) { + rc =3D -EBUSY; + } else { + h->teedev =3D teedev; + h->pool =3D pool; + rc =3D 0; + } + mutex_unlock(&h->mu); + } else { + rc =3D alloc_dma_heap(teedev, id, pool); + } + + if (rc) + dev_err(&teedev->dev, "can't register DMA heap id %d (%s)\n", + id, heap_id_2_name(id)); + + return rc; +} +EXPORT_SYMBOL_GPL(tee_device_register_dma_heap); + +void tee_device_unregister_all_dma_heaps(struct tee_device *teedev) +{ + struct tee_protmem_pool *pool; + struct tee_dma_heap *h; + u_long i; + + xa_for_each(&tee_dma_heap, i, h) { + if (h) { + pool =3D NULL; + mutex_lock(&h->mu); + if (h->teedev =3D=3D teedev) { + pool =3D h->pool; + h->teedev =3D NULL; + h->pool =3D NULL; + } + mutex_unlock(&h->mu); + if (pool) + pool->ops->destroy_pool(pool); + } + } +} +EXPORT_SYMBOL_GPL(tee_device_unregister_all_dma_heaps); + +int tee_heap_update_from_dma_buf(struct tee_device *teedev, + struct dma_buf *dmabuf, size_t *offset, + struct tee_shm *shm, + struct tee_shm **parent_shm) +{ + struct tee_heap_buffer *buf; + int rc; + + /* The DMA-buf must be from our heap */ + if (dmabuf->ops !=3D &tee_heap_buf_ops) + return -EINVAL; + + buf =3D dmabuf->priv; + /* The buffer must be from the same teedev */ + if (buf->teedev !=3D teedev) + return -EINVAL; + + shm->size =3D buf->size; + + rc =3D buf->pool->ops->update_shm(buf->pool, &buf->table, buf->offs, shm, + parent_shm); + if (!rc && *parent_shm) + *offset =3D buf->offs; + + return rc; +} +#else +int tee_device_register_dma_heap(struct tee_device *teedev __always_unused, + enum tee_dma_heap_id id __always_unused, + struct tee_protmem_pool *pool __always_unused) +{ + return -EINVAL; +} +EXPORT_SYMBOL_GPL(tee_device_register_dma_heap); + +void +tee_device_unregister_all_dma_heaps(struct tee_device *teedev __always_unu= sed) +{ +} +EXPORT_SYMBOL_GPL(tee_device_unregister_all_dma_heaps); + +int tee_heap_update_from_dma_buf(struct tee_device *teedev __always_unused, + struct dma_buf *dmabuf __always_unused, + size_t *offset __always_unused, + struct tee_shm *shm __always_unused, + struct tee_shm **parent_shm __always_unused) +{ + return -EINVAL; +} +#endif + +static struct tee_protmem_static_pool * +to_protmem_static_pool(struct tee_protmem_pool *pool) +{ + return container_of(pool, struct tee_protmem_static_pool, pool); +} + +static int protmem_pool_op_static_alloc(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t size, + size_t *offs) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + phys_addr_t pa; + int ret; + + pa =3D gen_pool_alloc(stp->gen_pool, size); + if (!pa) + return -ENOMEM; + + ret =3D sg_alloc_table(sgt, 1, GFP_KERNEL); + if (ret) { + gen_pool_free(stp->gen_pool, pa, size); + return ret; + } + + sg_set_page(sgt->sgl, phys_to_page(pa), size, 0); + *offs =3D pa - stp->pa_base; + + return 0; +} + +static void protmem_pool_op_static_free(struct tee_protmem_pool *pool, + struct sg_table *sgt) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + struct scatterlist *sg; + int i; + + for_each_sgtable_sg(sgt, sg, i) + gen_pool_free(stp->gen_pool, sg_phys(sg), sg->length); + sg_free_table(sgt); +} + +static int protmem_pool_op_static_update_shm(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t offs, + struct tee_shm *shm, + struct tee_shm **parent_shm) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + + shm->paddr =3D stp->pa_base + offs; + *parent_shm =3D NULL; + + return 0; +} + +static void protmem_pool_op_static_destroy_pool(struct tee_protmem_pool *p= ool) +{ + struct tee_protmem_static_pool *stp =3D to_protmem_static_pool(pool); + + gen_pool_destroy(stp->gen_pool); + kfree(stp); +} + +static struct tee_protmem_pool_ops protmem_pool_ops_static =3D { + .alloc =3D protmem_pool_op_static_alloc, + .free =3D protmem_pool_op_static_free, + .update_shm =3D protmem_pool_op_static_update_shm, + .destroy_pool =3D protmem_pool_op_static_destroy_pool, +}; + +struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr, + size_t size) +{ + const size_t page_mask =3D PAGE_SIZE - 1; + struct tee_protmem_static_pool *stp; + int rc; + + /* Check it's page aligned */ + if ((paddr | size) & page_mask) + return ERR_PTR(-EINVAL); + + stp =3D kzalloc(sizeof(*stp), GFP_KERNEL); + if (!stp) + return ERR_PTR(-ENOMEM); + + stp->gen_pool =3D gen_pool_create(PAGE_SHIFT, -1); + if (!stp->gen_pool) { + rc =3D -ENOMEM; + goto err_free; + } + + rc =3D gen_pool_add(stp->gen_pool, paddr, size, -1); + if (rc) + goto err_free_pool; + + stp->pool.ops =3D &protmem_pool_ops_static; + stp->pa_base =3D paddr; + return &stp->pool; + +err_free_pool: + gen_pool_destroy(stp->gen_pool); +err_free: + kfree(stp); + + return ERR_PTR(rc); +} +EXPORT_SYMBOL_GPL(tee_protmem_static_pool_alloc); diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 9bc50605227c..6c6ff5d5eed2 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -24,4 +25,9 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_context= *ctx, size_t size); struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx, unsigned long addr, size_t length); =20 +int tee_heap_update_from_dma_buf(struct tee_device *teedev, + struct dma_buf *dmabuf, size_t *offset, + struct tee_shm *shm, + struct tee_shm **parent_shm); + #endif /*TEE_PRIVATE_H*/ diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index a38494d6b5f4..b8b99c97e00c 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -8,9 +8,11 @@ =20 #include #include +#include #include #include #include +#include #include #include #include @@ -30,6 +32,12 @@ #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 =20 +enum tee_dma_heap_id { + TEE_DMA_HEAP_SECURE_VIDEO_PLAY =3D 1, + TEE_DMA_HEAP_TRUSTED_UI, + TEE_DMA_HEAP_SECURE_VIDEO_RECORD, +}; + /** * struct tee_device - TEE Device representation * @name: name of device @@ -116,6 +124,36 @@ struct tee_desc { u32 flags; }; =20 +/** + * struct tee_protmem_pool - protected memory pool + * @ops: operations + * + * This is an abstract interface where this struct is expected to be + * embedded in another struct specific to the implementation. + */ +struct tee_protmem_pool { + const struct tee_protmem_pool_ops *ops; +}; + +/** + * struct tee_protmem_pool_ops - protected memory pool operations + * @alloc: called when allocating protected memory + * @free: called when freeing protected memory + * @update_shm: called when registering a dma-buf to update the @shm + * with physical address of the buffer or to return the + * @parent_shm of the memory pool + * @destroy_pool: called when destroying the pool + */ +struct tee_protmem_pool_ops { + int (*alloc)(struct tee_protmem_pool *pool, struct sg_table *sgt, + size_t size, size_t *offs); + void (*free)(struct tee_protmem_pool *pool, struct sg_table *sgt); + int (*update_shm)(struct tee_protmem_pool *pool, struct sg_table *sgt, + size_t offs, struct tee_shm *shm, + struct tee_shm **parent_shm); + void (*destroy_pool)(struct tee_protmem_pool *pool); +}; + /** * tee_device_alloc() - Allocate a new struct tee_device instance * @teedesc: Descriptor for this driver @@ -154,6 +192,11 @@ int tee_device_register(struct tee_device *teedev); */ void tee_device_unregister(struct tee_device *teedev); =20 +int tee_device_register_dma_heap(struct tee_device *teedev, + enum tee_dma_heap_id id, + struct tee_protmem_pool *pool); +void tee_device_unregister_all_dma_heaps(struct tee_device *teedev); + /** * tee_device_set_dev_groups() - Set device attribute groups * @teedev: Device to register @@ -229,6 +272,28 @@ static inline void tee_shm_pool_free(struct tee_shm_po= ol *pool) pool->ops->destroy_pool(pool); } =20 +/** + * tee_protmem_static_pool_alloc() - Create a protected memory manager + * @paddr: Physical address of start of pool + * @size: Size in bytes of the pool + * + * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. + */ +struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr, + size_t size); + +/** + * tee_protmem_pool_free() - Free a protected memory pool + * @pool: The protected memory pool to free + * + * There must be no remaining protected memory allocated from this pool + * when this function is called. + */ +static inline void tee_protmem_pool_free(struct tee_protmem_pool *pool) +{ + pool->ops->destroy_pool(pool); +} + /** * tee_get_drvdata() - Return driver_data pointer * @returns the driver_data pointer supplied to tee_register(). --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (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 E8FD6242D82 for ; Fri, 2 May 2025 10:01:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180070; cv=none; b=TPAfjMKT/0lb7tLz+TA6Pyzzu8sGEJFnH9frWZxhcc5Fl+n9220uCLP1Vynq0LH0Ar//A0phU5OeNbrROK2n2u6uInPrkqgilAOm+WLr4Xkew2nHVVfXhWid9RA6NPzdpI00oEOKJ3IqG42aewMOZROLbQ5K5igVvW6DxBGeemA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180070; c=relaxed/simple; bh=J9LDfo6gNpW71xs4AHsdLctBmBSEcjpZaBz793nPpSU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NnenVMY6/5z2np0zCYvH87jsZ27G8ENN/2kLMTFo8px5zPaZ6lKFSpKcrWoV0mDFKL539L0xJQgjJ1g1DQw5HM3MhE3QYVC6RvDpGCpz3AqJCFpdmyOBozCOFt6pNuvUrpGkNOSS4lGaX26vAEojeb0DQyW4Uxyf4+JvOkaLM14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=kOWOrp86; arc=none smtp.client-ip=209.85.208.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="kOWOrp86" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-5e5e22e6ed2so2773354a12.3 for ; Fri, 02 May 2025 03:01:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180067; x=1746784867; darn=vger.kernel.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=fs7nqD12lkBPC3o5IKJmDFRrEPWNWVuJ0deEV/XnG9g=; b=kOWOrp86fwZIqmrnzs4rxJoXPuzSO6ectUYp7XQd669jv0N5XSDGFLNhbX0uF7H9Hh YfrbIDUvDgPT9Z2xk7/ma0DdMDouiv2arVBC2ZAn5nW25NWlVYtD5MjwmYeA1UmWmxQv /z1INvP2Mmkpag6t36ns+z1w1oY3hRTVFHoP/9XQ0BDDfsHwAiciyF+27mpqzg9ad0Id fR8PG9olGLAcm6NBltxmrS3ZQOUzrtFPKP5zeWMGtaE5iTPIOrNHPBbbpIf0zSb78K0B KSrzyvsuxkhANyB7Lc5RRJAxoQ+FspCBucqcvVBIhC/SHI98rxTQH3zwpuhBbKP/qURt H/Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180067; x=1746784867; 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=fs7nqD12lkBPC3o5IKJmDFRrEPWNWVuJ0deEV/XnG9g=; b=XA2sTyqc6GpxilCw2DXTxpNwZa0UZd0kcDkvsdtmCowYCywX2v9xeCenbZlsSoa0hC VvRtY6w10+K9Ht8fS/WLGF/dKBvgWZQjZbem7JMgIotMtp4RdhMNIOk7SO++FwzUA7CU dgx3Pr//+DfhJ0xu/BsTEVfhBj3f9llD6mmQjkEuWkkZrhdwRHzn9zPub10L98fm+pdc FwEcHU186Jx17iEZE96FfbQnlccjo70Af2Er9xNWekBses1uH2fDqQLF24GopLoeWshs Sda1tW6/GpIZByM6djOHweOQHxFYC33G5H8Z+HjQXWsEQ9MvoEkj6uocRyM/oKFZfyOD XL1w== X-Gm-Message-State: AOJu0YzLvqIxkUscUc/U81EW3K9TS//5PugQJGqNBv7NMvkx3gV8a7qc 02594yXcjttoTDqYP0Bt1rU86OpDsOqx4fWoHUzkbcr30znll+WhrwALrReLTr9tygnqLtGeQrc XQMg= X-Gm-Gg: ASbGncvXZPzsOrZ57PA9gozZyT7oEG7weBLo6T8Byg9185Il5iCxb3PKjno8HHoe6OP zfXiUwW6vdLYwxy+1c3+7Jv6KMjWGu4uhhK8l196uUzl/BTX775DdhPntUTkm4Y84OJEKygSRqa 5qMQZnGupKCokNXlOOvwNNfvkET8RyBM/ENQ/yzv6Jt7fjWoQD/izW4GVcz+vxyxCEEeolzdMaV 9OFP61d0iMtgxevYuXVuhQW7f0mPUMY0KAATSP2LnUIEkld5fWqnUBQcWuxn+O3XhLjyPz8plhO FknTEEEbYlIX18inqx7oBfhJzAtEtI8mi/bkhEDMr+yAUo2JhSqyA6AcQVyNOLybU2YQ0QW+Kl0 CiY7+Zv+8b9bmOV8PWw== X-Google-Smtp-Source: AGHT+IFFcCMXXLABaW9tvRZ1qpMWwe7p/TbtI+cL2YnquR0Nl0siV3BBf3ahUJ21DjrVNKWPwxmwtg== X-Received: by 2002:a17:907:3f0f:b0:ace:9d35:6987 with SMTP id a640c23a62f3a-ad17ad24ac3mr199106166b.3.1746180066113; Fri, 02 May 2025 03:01:06 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:05 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 07/14] tee: refactor params_from_user() Date: Fri, 2 May 2025 11:59:21 +0200 Message-ID: <20250502100049.1746335-8-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Break out the memref handling into a separate helper function. No change in behavior. Signed-off-by: Jens Wiklander Reviewed-by: Sumit Garg --- drivers/tee/tee_core.c | 94 ++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 685afcaa3ea1..820e394b9054 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -353,6 +353,55 @@ tee_ioctl_shm_register(struct tee_context *ctx, return ret; } =20 +static int param_from_user_memref(struct tee_context *ctx, + struct tee_param_memref *memref, + struct tee_ioctl_param *ip) +{ + struct tee_shm *shm; + + /* + * If a NULL pointer is passed to a TA in the TEE, + * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL + * indicating a NULL memory reference. + */ + if (ip->c !=3D TEE_MEMREF_NULL) { + /* + * If we fail to get a pointer to a shared + * memory object (and increase the ref count) + * from an identifier we return an error. All + * pointers that has been added in params have + * an increased ref count. It's the callers + * responibility to do tee_shm_put() on all + * resolved pointers. + */ + shm =3D tee_shm_get_from_id(ctx, ip->c); + if (IS_ERR(shm)) + return PTR_ERR(shm); + + /* + * Ensure offset + size does not overflow + * offset and does not overflow the size of + * the referred shared memory object. + */ + if ((ip->a + ip->b) < ip->a || + (ip->a + ip->b) > shm->size) { + tee_shm_put(shm); + return -EINVAL; + } + } else if (ctx->cap_memref_null) { + /* Pass NULL pointer to OP-TEE */ + shm =3D NULL; + } else { + return -EINVAL; + } + + memref->shm_offs =3D ip->a; + memref->size =3D ip->b; + memref->shm =3D shm; + + return 0; +} + static int params_from_user(struct tee_context *ctx, struct tee_param *par= ams, size_t num_params, struct tee_ioctl_param __user *uparams) @@ -360,8 +409,8 @@ static int params_from_user(struct tee_context *ctx, st= ruct tee_param *params, size_t n; =20 for (n =3D 0; n < num_params; n++) { - struct tee_shm *shm; struct tee_ioctl_param ip; + int rc; =20 if (copy_from_user(&ip, uparams + n, sizeof(ip))) return -EFAULT; @@ -384,45 +433,10 @@ static int params_from_user(struct tee_context *ctx, = struct tee_param *params, case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: - /* - * If a NULL pointer is passed to a TA in the TEE, - * the ip.c IOCTL parameters is set to TEE_MEMREF_NULL - * indicating a NULL memory reference. - */ - if (ip.c !=3D TEE_MEMREF_NULL) { - /* - * If we fail to get a pointer to a shared - * memory object (and increase the ref count) - * from an identifier we return an error. All - * pointers that has been added in params have - * an increased ref count. It's the callers - * responibility to do tee_shm_put() on all - * resolved pointers. - */ - shm =3D tee_shm_get_from_id(ctx, ip.c); - if (IS_ERR(shm)) - return PTR_ERR(shm); - - /* - * Ensure offset + size does not overflow - * offset and does not overflow the size of - * the referred shared memory object. - */ - if ((ip.a + ip.b) < ip.a || - (ip.a + ip.b) > shm->size) { - tee_shm_put(shm); - return -EINVAL; - } - } else if (ctx->cap_memref_null) { - /* Pass NULL pointer to OP-TEE */ - shm =3D NULL; - } else { - return -EINVAL; - } - - params[n].u.memref.shm_offs =3D ip.a; - params[n].u.memref.size =3D ip.b; - params[n].u.memref.shm =3D shm; + rc =3D param_from_user_memref(ctx, ¶ms[n].u.memref, + &ip); + if (rc) + return rc; break; default: /* Unknown attribute */ --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.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 AD18922A4F8 for ; Fri, 2 May 2025 10:01:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180073; cv=none; b=d82fSu31fmdPze7fT8l8j/mBvp8O3uUQxq67uEg7qe+bArF7O/KKHRW2G+L1MIrjjMgpF7pnkvxvMDaJmqn3USszEh/fJ2EyGr4isxOOXeQTTHIaYEgOZeMP2fuw+1+MrszFs30VW1ech85NmoQkd6T2CVfNEioGmA+TGBNC7uw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180073; c=relaxed/simple; bh=eLFeeEbkndtB6WhFYXHsPt6TTGoLVeBbdQEZVfJN3M0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p5a1j8DB35UzmTz4chqRMx6PKkrN9sNxeOvQhdrqqul2J4SaLg0z3uAe0HgG0xXWxYQ7HfJqlQNELHbCssh03n7KcGyPnoZEH1s8e8gl/zkL4hYbFPGu69d1Vo6m9tjzwH0dkK2DHO314Ib1tWBN32pt1MM6ZUa/qbZvfiGWvSU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=c+oMqTUT; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="c+oMqTUT" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-ac29fd22163so312164266b.3 for ; Fri, 02 May 2025 03:01:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180068; x=1746784868; darn=vger.kernel.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=MVW36J04OD10abfKAvr/C0zzpgJG5wW+CSg45CWkszU=; b=c+oMqTUT4Adz0ywp0O7NRpHC4je9/yNDR3jq7KxIcbIv5h37tu76B+lpKJPyl6vuW5 +zvxIo1BbvX+/ZIgVV2yLaq77L56qIRu79id0u8H4ZYgEE4OJ2n4I58ftHHFOBM2S8dC U/s0i2MfBHjKGNvem0VR0JjCxHDd703DFDxhXSvGU6NU6giNrK04ko8A8Vr0T6lsPY0f sFS7qYhNDYoSliWC2VB9gR7lvtPoEDHtzQpES0ER7xHHKbtDRufVvEEsePLLeqa/mxhD XzoklPN4LYvnoRy6wOdGpFC2pkwkAEGMv7HFRe/LIUY7aGltMV8IXRnfbloOWsUdOLcb 4fjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180068; x=1746784868; 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=MVW36J04OD10abfKAvr/C0zzpgJG5wW+CSg45CWkszU=; b=Tad/8cnv+0RmDYwQfoKf1nirYH2obor0RhzHVUto3m0aVYyYI8XwqSTDY7YZANRfvI 9b8c2JZKysWKE7veHySEQknXm+skY9/9HRWEw1XXpVzAlxOMa1GoU4RDDFKfFQdvBdbz 3t/gA6VNEJVrBVa5p7MjHeZOsMIoTRA0WTkC/6pfqKGT+VsA6i63Hb71eQ43p1yh5gWb ru8uSPSx2oWtBCKWwsni55qRVI6lHZl5G5zSLQLvAkDd7JxiuByTv0UgmH6HAmTWUPGy roh9hks6mOsae3Iw6s+yW0Ft7ZCrebGPzn8lOsYoYV2zK7zFN3srFKRzIHmc8ysHOil5 Jk4A== X-Gm-Message-State: AOJu0YyAEMeO5xfgQa1K4zEjft7AScn79TUrXkU9xK/TC1gBloZidnS/ L1nPphoH8+7k4T22dZ5cQrszzp1W8Q1Y7IKf4yreaOvf4bSRwg1sJ6jzUtEwO1yjipf4RaMDqMY UIao= X-Gm-Gg: ASbGncsLCjV22ihDHT1bzFbl9dcJTUJ1moQINpCBsTu94aNjnQ0U/MILdmC3Thf9d4a n/WSz4EsYQDDX8Sk4Tq6ZERZuez3DuUwCPG6yoPJpkrOkpRw6dsEEejrooOEYa/9/eMViZRArvh p0tze3dAZ1WB8A+LtH2FSrxMVwlGtU2gqwYLAej+eQAR/wistbZ8gQBN6BkPWSK3pq9T937l0Ml Y3y1/quOrcQAtysqrK9oBCrUgi0wdyY/asR7s4EYTEn73A+y5uqi08rZhqYa3Px09Xm+1FydtmX Si/DTLNRS5zxhC3oVDmSbL+vU5iOk6aUjHcsftItXWpJPjPDyLu7Bt5JHFuga4RCXTxie3B2Z2m P+3HHGG4uvnu8HSq+GQ== X-Google-Smtp-Source: AGHT+IHLbEpDcxb4+kBeX79PDIalluSRLjbJ0GJNv5cwrmnZsB+Sm1+64y2DhUaX8E2vBpq82q61tw== X-Received: by 2002:a17:907:3e90:b0:ac2:cf0b:b809 with SMTP id a640c23a62f3a-ad17adc1d4amr223483666b.31.1746180068119; Fri, 02 May 2025 03:01:08 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:07 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Etienne Carriere , Jens Wiklander Subject: [PATCH v8 08/14] tee: new ioctl to a register tee_shm from a dmabuf file descriptor Date: Fri, 2 May 2025 11:59:22 +0200 Message-ID: <20250502100049.1746335-9-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Etienne Carriere Add a userspace API to create a tee_shm object that refers to a dmabuf reference. Userspace registers the dmabuf file descriptor as in a tee_shm object. The registration is completed with a tee_shm returned file descriptor. Userspace is free to close the dmabuf file descriptor after it has been registered since all the resources are now held via the new tee_shm object. Closing the tee_shm file descriptor will eventually release all resources used by the tee_shm object when all references are released. The new IOCTL, TEE_IOC_SHM_REGISTER_FD, supports dmabuf references to physically contiguous memory buffers. Dmabuf references acquired from the TEE DMA-heap can be used as protected memory for Secure Video Path and such use cases. It depends on the TEE and the TEE driver if dmabuf references acquired by other means can be used. A new tee_shm flag is added to identify tee_shm objects built from a registered dmabuf, TEE_SHM_DMA_BUF. Signed-off-by: Etienne Carriere Signed-off-by: Olivier Masse Signed-off-by: Jens Wiklander --- drivers/tee/tee_core.c | 63 +++++++++++++++++++++- drivers/tee/tee_private.h | 10 ++++ drivers/tee/tee_shm.c | 111 ++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 1 + include/linux/tee_drv.h | 10 ++++ include/uapi/linux/tee.h | 31 +++++++++++ 6 files changed, 221 insertions(+), 5 deletions(-) diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c index 820e394b9054..d26612ac060b 100644 --- a/drivers/tee/tee_core.c +++ b/drivers/tee/tee_core.c @@ -353,11 +353,49 @@ tee_ioctl_shm_register(struct tee_context *ctx, return ret; } =20 +static int +tee_ioctl_shm_register_fd(struct tee_context *ctx, + struct tee_ioctl_shm_register_fd_data __user *udata) +{ + struct tee_ioctl_shm_register_fd_data data; + struct tee_shm *shm; + long ret; + + if (copy_from_user(&data, udata, sizeof(data))) + return -EFAULT; + + /* Currently no input flags are supported */ + if (data.flags) + return -EINVAL; + + shm =3D tee_shm_register_fd(ctx, data.fd); + if (IS_ERR(shm)) + return -EINVAL; + + data.id =3D shm->id; + data.flags =3D shm->flags; + data.size =3D shm->size; + + if (copy_to_user(udata, &data, sizeof(data))) + ret =3D -EFAULT; + else + ret =3D tee_shm_get_fd(shm); + + /* + * When user space closes the file descriptor the shared memory + * should be freed or if tee_shm_get_fd() failed then it will + * be freed immediately. + */ + tee_shm_put(shm); + return ret; +} + static int param_from_user_memref(struct tee_context *ctx, struct tee_param_memref *memref, struct tee_ioctl_param *ip) { struct tee_shm *shm; + size_t offs =3D 0; =20 /* * If a NULL pointer is passed to a TA in the TEE, @@ -388,6 +426,26 @@ static int param_from_user_memref(struct tee_context *= ctx, tee_shm_put(shm); return -EINVAL; } + + if (shm->flags & TEE_SHM_DMA_BUF) { + struct tee_shm_dmabuf_ref *ref; + + ref =3D container_of(shm, struct tee_shm_dmabuf_ref, shm); + if (ref->parent_shm) { + /* + * The shm already has one reference to + * ref->parent_shm so we are clear of 0. + * We're getting another reference since + * this shm will be used in the parameter + * list instead of the shm we got with + * tee_shm_get_from_id() above. + */ + refcount_inc(&ref->parent_shm->refcount); + tee_shm_put(shm); + shm =3D ref->parent_shm; + offs =3D ref->offset; + } + } } else if (ctx->cap_memref_null) { /* Pass NULL pointer to OP-TEE */ shm =3D NULL; @@ -395,7 +453,7 @@ static int param_from_user_memref(struct tee_context *c= tx, return -EINVAL; } =20 - memref->shm_offs =3D ip->a; + memref->shm_offs =3D ip->a + offs; memref->size =3D ip->b; memref->shm =3D shm; =20 @@ -841,6 +899,8 @@ static long tee_ioctl(struct file *filp, unsigned int c= md, unsigned long arg) return tee_ioctl_shm_alloc(ctx, uarg); case TEE_IOC_SHM_REGISTER: return tee_ioctl_shm_register(ctx, uarg); + case TEE_IOC_SHM_REGISTER_FD: + return tee_ioctl_shm_register_fd(ctx, uarg); case TEE_IOC_OPEN_SESSION: return tee_ioctl_open_session(ctx, uarg); case TEE_IOC_INVOKE: @@ -1302,3 +1362,4 @@ MODULE_AUTHOR("Linaro"); MODULE_DESCRIPTION("TEE Driver"); MODULE_VERSION("1.0"); MODULE_LICENSE("GPL v2"); +MODULE_IMPORT_NS("DMA_BUF"); diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h index 6c6ff5d5eed2..308467705da6 100644 --- a/drivers/tee/tee_private.h +++ b/drivers/tee/tee_private.h @@ -13,6 +13,16 @@ #include #include =20 +/* extra references appended to shm object for registered shared memory */ +struct tee_shm_dmabuf_ref { + struct tee_shm shm; + size_t offset; + struct dma_buf *dmabuf; + struct dma_buf_attachment *attach; + struct sg_table *sgt; + struct tee_shm *parent_shm; +}; + int tee_shm_get_fd(struct tee_shm *shm); =20 bool tee_device_get(struct tee_device *teedev); diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index daf6e5cfd59a..e1ed52ee0a16 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -4,6 +4,7 @@ */ #include #include +#include #include #include #include @@ -45,7 +46,23 @@ static void release_registered_pages(struct tee_shm *shm) =20 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm) { - if (shm->flags & TEE_SHM_POOL) { + struct tee_shm *parent_shm =3D NULL; + void *p =3D shm; + + if (shm->flags & TEE_SHM_DMA_BUF) { + struct tee_shm_dmabuf_ref *ref; + + ref =3D container_of(shm, struct tee_shm_dmabuf_ref, shm); + parent_shm =3D ref->parent_shm; + p =3D ref; + if (ref->attach) { + dma_buf_unmap_attachment(ref->attach, ref->sgt, + DMA_BIDIRECTIONAL); + + dma_buf_detach(ref->dmabuf, ref->attach); + } + dma_buf_put(ref->dmabuf); + } else if (shm->flags & TEE_SHM_POOL) { teedev->pool->ops->free(teedev->pool, shm); } else if (shm->flags & TEE_SHM_DYNAMIC) { int rc =3D teedev->desc->ops->shm_unregister(shm->ctx, shm); @@ -57,9 +74,10 @@ static void tee_shm_release(struct tee_device *teedev, s= truct tee_shm *shm) release_registered_pages(shm); } =20 - teedev_ctx_put(shm->ctx); + if (shm->ctx) + teedev_ctx_put(shm->ctx); =20 - kfree(shm); + kfree(p); =20 tee_device_put(teedev); } @@ -169,7 +187,7 @@ struct tee_shm *tee_shm_alloc_user_buf(struct tee_conte= xt *ctx, size_t size) * tee_client_invoke_func(). The memory allocated is later freed with a * call to tee_shm_free(). * - * @returns a pointer to 'struct tee_shm' + * @returns a pointer to 'struct tee_shm' on success, and ERR_PTR on failu= re */ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t s= ize) { @@ -179,6 +197,91 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_co= ntext *ctx, size_t size) } EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf); =20 +struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd) +{ + struct tee_shm_dmabuf_ref *ref; + int rc; + + if (!tee_device_get(ctx->teedev)) + return ERR_PTR(-EINVAL); + + teedev_ctx_get(ctx); + + ref =3D kzalloc(sizeof(*ref), GFP_KERNEL); + if (!ref) { + rc =3D -ENOMEM; + goto err_put_tee; + } + + refcount_set(&ref->shm.refcount, 1); + ref->shm.ctx =3D ctx; + ref->shm.id =3D -1; + ref->shm.flags =3D TEE_SHM_DMA_BUF; + + ref->dmabuf =3D dma_buf_get(fd); + if (IS_ERR(ref->dmabuf)) { + rc =3D PTR_ERR(ref->dmabuf); + goto err_kfree_ref; + } + + rc =3D tee_heap_update_from_dma_buf(ctx->teedev, ref->dmabuf, + &ref->offset, &ref->shm, + &ref->parent_shm); + if (!rc) + goto out; + if (rc !=3D -EINVAL) + goto err_put_dmabuf; + + ref->attach =3D dma_buf_attach(ref->dmabuf, &ctx->teedev->dev); + if (IS_ERR(ref->attach)) { + rc =3D PTR_ERR(ref->attach); + goto err_put_dmabuf; + } + + ref->sgt =3D dma_buf_map_attachment(ref->attach, DMA_BIDIRECTIONAL); + if (IS_ERR(ref->sgt)) { + rc =3D PTR_ERR(ref->sgt); + goto err_detach; + } + + if (sg_nents(ref->sgt->sgl) !=3D 1) { + rc =3D -EINVAL; + goto err_unmap_attachement; + } + + ref->shm.paddr =3D page_to_phys(sg_page(ref->sgt->sgl)); + ref->shm.size =3D ref->sgt->sgl->length; + +out: + mutex_lock(&ref->shm.ctx->teedev->mutex); + ref->shm.id =3D idr_alloc(&ref->shm.ctx->teedev->idr, &ref->shm, + 1, 0, GFP_KERNEL); + mutex_unlock(&ref->shm.ctx->teedev->mutex); + if (ref->shm.id < 0) { + rc =3D ref->shm.id; + if (ref->attach) + goto err_unmap_attachement; + goto err_put_dmabuf; + } + + return &ref->shm; + +err_unmap_attachement: + dma_buf_unmap_attachment(ref->attach, ref->sgt, DMA_BIDIRECTIONAL); +err_detach: + dma_buf_detach(ref->dmabuf, ref->attach); +err_put_dmabuf: + dma_buf_put(ref->dmabuf); +err_kfree_ref: + kfree(ref); +err_put_tee: + teedev_ctx_put(ctx); + tee_device_put(ctx->teedev); + + return ERR_PTR(rc); +} +EXPORT_SYMBOL_GPL(tee_shm_register_fd); + /** * tee_shm_alloc_priv_buf() - Allocate shared memory for a privately shared * kernel buffer diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index b8b99c97e00c..02c07f661349 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -28,6 +28,7 @@ #define TEE_SHM_USER_MAPPED BIT(1) /* Memory mapped in user space */ #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */ +#define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */ =20 #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 diff --git a/include/linux/tee_drv.h b/include/linux/tee_drv.h index a54c203000ed..824f1251de60 100644 --- a/include/linux/tee_drv.h +++ b/include/linux/tee_drv.h @@ -116,6 +116,16 @@ struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_co= ntext *ctx, size_t size); struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx, void *addr, size_t length); =20 +/** + * tee_shm_register_fd() - Register shared memory from file descriptor + * + * @ctx: Context that allocates the shared memory + * @fd: Shared memory file descriptor reference + * + * @returns a pointer to 'struct tee_shm' on success, and ERR_PTR on failu= re + */ +struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd); + /** * tee_shm_free() - Free shared memory * @shm: Handle to shared memory to free diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h index d0430bee8292..8ec5f46fbfbe 100644 --- a/include/uapi/linux/tee.h +++ b/include/uapi/linux/tee.h @@ -118,6 +118,37 @@ struct tee_ioctl_shm_alloc_data { #define TEE_IOC_SHM_ALLOC _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 1, \ struct tee_ioctl_shm_alloc_data) =20 +/** + * struct tee_ioctl_shm_register_fd_data - Shared memory registering argum= ent + * @fd: [in] File descriptor identifying dmabuf reference + * @size: [out] Size of referenced memory + * @flags: [in] Flags to/from allocation. + * @id: [out] Identifier of the shared memory + * + * The flags field should currently be zero as input. Updated by the call + * with actual flags as defined by TEE_IOCTL_SHM_* above. + * This structure is used as argument for TEE_IOC_SHM_REGISTER_FD below. + */ +struct tee_ioctl_shm_register_fd_data { + __s64 fd; + __u64 size; + __u32 flags; + __s32 id; +}; + +/** + * TEE_IOC_SHM_REGISTER_FD - register a shared memory from a file descript= or + * + * Returns a file descriptor on success or < 0 on failure + * + * The returned file descriptor refers to the shared memory object in the + * kernel. The supplied file deccriptor can be closed if it's not needed + * for other purposes. The shared memory is freed when the descriptor is + * closed. + */ +#define TEE_IOC_SHM_REGISTER_FD _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 8, \ + struct tee_ioctl_shm_register_fd_data) + /** * struct tee_ioctl_buf_data - Variable sized buffer * @buf_ptr: [in] A __user pointer to a buffer --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 5183824501D for ; Fri, 2 May 2025 10:01:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180077; cv=none; b=ur3O7g9sjOHFqpeYO2wpJxnW7nsxU3RG5072F9Eqel7ykuSV93DU+aeFUb/ZJ1CZtPyYEbgyE6WXLjgj+duYHNIxanXEggtBkpm2S6yhfz6/C8LcneSSrRPHgb9yRBf5u9GuifGe0objGEBdbA4CqunVHHFJAtpNis7mxxIG+pM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180077; c=relaxed/simple; bh=K6D/di6lA+uVA6E3AQ7OcMI+aIt5a8gGMoofegAlu74=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mkxrnLQI5RK0Uwswg5eJx/uaCM3aZwOOYA61vsA7xpNuzZyb+fgpWmEnh01/4W+zuAv+09ESau1dr7FHWfrxKwVc3Eym55nqysRFpU3JPpfMcRhrAivCH1yiPZgMYYvQktmps4jXd2hoclQWm8l5WCBwJV/tvmVv5FggTKaqV2c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=udsnngaM; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="udsnngaM" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-acbb85ce788so422101566b.3 for ; Fri, 02 May 2025 03:01:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180070; x=1746784870; darn=vger.kernel.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=1anNv4Z25lfJ6IEDtR0M/iXAZvESKw8M2gLCl/3GqgY=; b=udsnngaMiu4HZwa72tQ13KsWDqtwRtRluVrAszilSe4HH9W1TGkq+VLjlNoWyuRZvO nOEW8xOs5u6zWvCFsslAKAgVImXaDLN9S3nh+lgBS6yi2ajf7ZFZH7qlIfvygqi9l5YS vuIYW6lfewu0h+A50BBwkYfLujgImh7HFWmqFowpZzJYZfLA9YgzWX5vI4jp+7mJc64t ZwsI4t1teZMq+qqSgpsQwxMDlfzP8/+du+jnLjHh8Xt25GWRa50unQ2LLkepm2Au5yEi lrhrPKwWkOpAV6p0MFRDkY3tOUMdP15VkrCr0J7YGwS51d/bgSkZIn1+rBxS0ackmIQ0 fMTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180070; x=1746784870; 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=1anNv4Z25lfJ6IEDtR0M/iXAZvESKw8M2gLCl/3GqgY=; b=GNATGl0HBESFPunnBJEkfClkabRqfSBk2SFmnvy3dMbQRScYLlpoR5f88qdURVMgc8 /zuntSR4SvEvpxcDyfgoLdsL9Erpi7zj3S9Jbix1SLtGy8L+OQ23VbOqycP5eczSRSVc iB0b2Rw1bXBYpHUc3eO6ywOIHjKudOQHl7/SmHuc0QeS/oZRd/EfpNC653yJaJs9k6k6 Pj1MQIhEwcf7AcSjcD9QeMjfkpjageEOo817hvqnYQ+/OIx0j4c8I8UhyFC933aROlVZ 3CjjY570cSNGM5Qio+CZpWfkwgE//yrvWQv8EGIW79JdoOdpux5zca7Aee9/tHgcS2w7 EExA== X-Gm-Message-State: AOJu0YwMtIOXqlmn9UtZm3TSl41PFNxeNU1nR31qx4izHHYT6Qx3AGvO BBjzIU/W9FLQtl6L7KMrmsQgzd+4dUfV33JWgpcahDGoP4tFczu5kt77uNmyvvvl4T17CnUZkQi Z3yw= X-Gm-Gg: ASbGnct9ZtHhiqminpo+jUch/zPK/HXgivFSHlznfj1CJQ2A6q5cK6+kGHHCDH7TLYm 9HusOCntO9o+o1Aaj6nyOqsXYyMLuHabLpn2mEFwgzWuiMGaBFeeKel93NhBxobk+vzB1C3Nof2 uHcbeyTbaIAvRx3M7Skt5bJOHwOb9E9vcGshHrEecHwA8p+saSqboVGWlKnywipFwTIrlec6e6I JMlBnrYBaaxCAzl0bQWLXu+4PyodKgge4e5xuTTNNyrIhHfKH7mJ87dLEYs4//FcvA39gSjzI++ /gUYg3lVL1Qr26MAfOMkC49VxFOXxCqssalAbaKO5TadSpLHdzVsGRCT0chUfmIVOt+ImnIp3KV G079tg7hRQ3qdva1JQw== X-Google-Smtp-Source: AGHT+IGbNYwwYka0rS/o95u834QMshNlm6ZE08gyDe8QEBQfbOvJaJeikFzKXGrLN54PFU3NRdyFmg== X-Received: by 2002:a17:907:7255:b0:ace:f2c2:5a4 with SMTP id a640c23a62f3a-ad17ad5d279mr201865366b.13.1746180070053; Fri, 02 May 2025 03:01:10 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:09 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander , Andrew Morton , linux-mm@kvack.org Subject: [PATCH v8 09/14] cma: export cma_alloc() and cma_release() Date: Fri, 2 May 2025 11:59:23 +0200 Message-ID: <20250502100049.1746335-10-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Export the two functions cma_alloc() and cma_release(). Cc: Andrew Morton Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Jens Wiklander --- mm/cma.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/cma.c b/mm/cma.c index 15632939f20a..c60901e73a26 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -915,6 +915,7 @@ struct page *cma_alloc(struct cma *cma, unsigned long c= ount, { return __cma_alloc(cma, count, align, GFP_KERNEL | (no_warn ? __GFP_NOWAR= N : 0)); } +EXPORT_SYMBOL(cma_alloc); =20 struct folio *cma_alloc_folio(struct cma *cma, int order, gfp_t gfp) { @@ -1002,6 +1003,7 @@ bool cma_release(struct cma *cma, const struct page *= pages, =20 return true; } +EXPORT_SYMBOL(cma_release); =20 bool cma_free_folio(struct cma *cma, const struct folio *folio) { --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.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 23ADE246790 for ; Fri, 2 May 2025 10:01:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180075; cv=none; b=T/Rg/HBpnfsmbENlX1c2x0YcSyjqvuz4aWUv2nrQMICUUxey4zDAK/Df6/YRS4Hjj9tRkYHt6ha7BMrnzfJuh1rsHvZqVVp7Wdwrnet4NL9VvvJ3O/5EnMp5N86iRBxYJs7cJbbS4RkilBf00Kb+GuKSS4pVscmogaxbrerzvvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180075; c=relaxed/simple; bh=JBFJnBv1Vm67I2drd1XhP0qbGY0AFjAC5ZYn7HBNfWg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hMZq3h2oadXQcZ10KXrRum1lOQB/MqXdW2IlP0o9NYxAFZsDIXsPuRqeE3RQLF+vJyAhpqaq2hVQpXnZu1wpi+LhMHKyIyr0VmU2CynVie46fe48DFBGipameNTcMye+nZjQiz0X3hIU//EjS0flGDuo39BjyuK00o+UdB7f4Yw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=VRPd8PK0; arc=none smtp.client-ip=209.85.218.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="VRPd8PK0" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-ac339f53df9so391412966b.1 for ; Fri, 02 May 2025 03:01:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180072; x=1746784872; darn=vger.kernel.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=K96TP7RufWR0A5oYw5jAf7HfaOkuZ/rMipxLAIxdi/A=; b=VRPd8PK0ERWcTjG48qEpGUPUjpAsLO4CrLTlUAfNpoc2RkOC6HYgVxzJ/1jvtEvgkB zaevmFd3QGTx5kWomkX/lIWRxizweqjKxnVx3zB89sU8gRpqLa6OVPRo/TpRMTwxV6OO CKJ5XVhLm6kWvNnUkOfMEfKUlJrFoLmNzpzveRwVJVlvDRFvlmVeCweC6AjTCbfha2B6 GrOS51bMsPpgKBMqBe+eOEFdK4+/x0W30hV+C1CkIsVqOFlPLjgszXVljlvDDbGIvRtV bXKcLFaNMsBzArp6f3Qlt0fdIvpAcNhoj1vTuSOxZz5QnEUmYXs+F3pQMiBhpUHndn8A Kc/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180072; x=1746784872; 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=K96TP7RufWR0A5oYw5jAf7HfaOkuZ/rMipxLAIxdi/A=; b=fFJycTuzqGwe2ycFYD5G/Ma0z4siPH/7ltdN5NyhUon3L63nUm41UmOcPoz1eV0I1W hQGruwLRiUrgryvprMmUakwwNVJWv4l8bC3WMyHPCSwijQxiDRpYHzlQTtGFTIYyv1kl dw1tFv0HpoH5CHwi1mUA/7wnemhgt1qTwhkHjimxiGSJbMIxJz9Vyxiyyr4zczJvmT5u tZT0tYxa9pjjiktch2BxCA8jN/gZ+pXSWSnodkVB4sQsnB8d9cR2Ka3STIp0NmAQ7F/I xI4BrQDrmtXmfC7casf9YPcVQC9ZTBkDatCBQUNa4JcZ7KTbgVm7z7EhQByGwWU0BQUS R8uQ== X-Gm-Message-State: AOJu0YxXGZMvt0s080OSz3SQ26C3151WVEIyManvnKyKAS5sbal3pQTa 0RK82xQ5T8J9tEn3pBMv43p+RKqN3qGrf7OyUhtQH89EV6D5JCc61NeTGM4ystfYN9Gu+kubd/n KBdg= X-Gm-Gg: ASbGncvPPnYQ2K5ikZDM3vE3xJw33NnvnAeKPQIwE1K5L0u6ENy65FmuHYe4vBGVPW+ G6+vxGqLsO5+j3YurbaOZeh5xa+zAOrAs9Gv3eFbk8EVav4hfs41bIUO7dFj/UgOAY0YFkRwdBM dB5CWftonicEx0gQxINGcW3ObXjv6eeWFpm9nMNRA2xZJFt4S6LHPNYgmdau8iKAyT51x77NjRd azFrUiU+x5HxdrChb6Kcxc26+Mapb40YYaOvivZLM2quU/C6Tk1lEKfPWoyK2ew21LTE7z8lN6m rdnPaLlYqsRRHb+8j2JBfxbjsv85LejCGQGH99IaQC1TPJdL6r93ajgiU/msKORZ846Bem+nl0O pdI2xHZxUFIN+cciHs7T9JPyN+Jyn X-Google-Smtp-Source: AGHT+IEw/QFZ4Q15wjtCZMc2Ys2EDGftsTen4aFJcOHmrUkANvg/HjzdHRZE7b55C3GGic4/3YhYzw== X-Received: by 2002:a17:907:74a:b0:acf:15d:2387 with SMTP id a640c23a62f3a-ad17ada7132mr232347066b.19.1746180071962; Fri, 02 May 2025 03:01:11 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:11 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander , Marek Szyprowski , Robin Murphy , iommu@lists.linux.dev Subject: [PATCH v8 10/14] dma-contiguous: export dma_contiguous_default_area Date: Fri, 2 May 2025 11:59:24 +0200 Message-ID: <20250502100049.1746335-11-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Export the global variable dma_contiguous_default_area so dev_get_cma_area() can be called a module. Cc: Marek Szyprowski Cc: Robin Murphy Cc: iommu@lists.linux.dev Signed-off-by: Jens Wiklander --- kernel/dma/contiguous.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index 8df0dfaaca18..eb361794a9c5 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -53,6 +53,7 @@ #endif =20 struct cma *dma_contiguous_default_area; +EXPORT_SYMBOL(dma_contiguous_default_area); =20 /* * Default global CMA area size can be defined in kernel's .config. --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (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 0B8942512ED for ; Fri, 2 May 2025 10:01:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180078; cv=none; b=I46Pp9PwcBCobU7L5KTTHoIYlllwo/QVPaLp22yc6FIGj3XD1XmtaxesuCG+lAejllv31yZhimsSKbr/rDkZ/s78aNa75nbBAnZc0e5eIl5ZkiA8yGJaxqXt8/X5mjFBwc0jCOHMHVPNcPRiLdauLIKeB9T1Z1kencQuP5qoVNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180078; c=relaxed/simple; bh=adYscF/VWtjspVg2TPKRQL4Dl/ZaZON4Rv7qOq3mBNM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ihXcjkogSgIoEbrzry25s2f5GJ2ZaujI+Whhs0xegV8h6Q1dgHkw9PSYVOzPbeDh0AFFAM/Zqnck1O4iZY+dJMBSpy3zaAm750XjNAX05iH3jtMDQE6BbV6/VkEVk5eSRSvwMltgqd46V+ftQkid046eHf7rmpOu5uGcyrGOUpg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=xudrqNaX; arc=none smtp.client-ip=209.85.218.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="xudrqNaX" Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-acbb85ce788so422120566b.3 for ; Fri, 02 May 2025 03:01:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180074; x=1746784874; darn=vger.kernel.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=JHc/i2uVCttA+F6G/pvXVydhhQiLzwlzjcfTEmJaCtI=; b=xudrqNaXtcfxR/lRb3bfNxnwxp4L/LtO+GkhuhWn0t5Lp1Pyq2foM0RTSqf9Cyi5wl 11Gn7wj1fiDqvpgAGvJy51lL4Sg6RtSSGvsPPkFccSQ1pa6KOe3AjlLLCWi397zBGY4Q 9ykOrpwk4d+/naGRBypT1X8FXMNxn4WfsCWxSZANrorxpD+biAK9CjX12AswGW86jPSj U+hUW88QXZSVfb7HbzNANLemuIV79/AS6TLqGaXRfivgO7mXmbCdCnM9r0Hf5A7pTx6i Vo+Mr/LdNzY5JMxOa/9ky1Qy2MR8OyrwOMy87P8NmFb/pTucfgtWKCNFIbfGV/iC+EZU 9zEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180074; x=1746784874; 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=JHc/i2uVCttA+F6G/pvXVydhhQiLzwlzjcfTEmJaCtI=; b=fficMfKiDO/fcr3vq8DmoNCM3EmTRynWEHJZRMoekrQPMetEwMNAeARsGT92CMtIy+ FxcE4vtAp4iMlCu7KU0I0Nvl97tR3Fe8VwkIs8zwUfRRbCRZ+HJRBPCesicL2yGdXRHv 3MgSiM0uKcdbLGCNTNXV9Er9Lj+Ov/9M4lFV9CG+dv4AQv8RGWlIR8OieC7GD6r0a+CO qkPo6x/GEj8A9N8enCUtNdmWgrjhEtejqh8rZ8cVRfs4ivIyS93v+00ZjkkKN9cDWzOk UzRfHD8pID9yxFQjYXTtMmnw/yGQD/mKmTUgowuPTAo37hi2GY7SQKJk1R1/rzUneri0 Fcdg== X-Gm-Message-State: AOJu0YzUo2Bz85+KRWcU6ewpbcSAuo7jdWxwq7vBI8Moiv6N6OheTfOQ FK2DnwdPm8x0N1qe3p+M76DUiLoKiBAf9COCGIQfh40U0mBQzlsnjzhTjKeTToF6SCVJtTEidjz byCg= X-Gm-Gg: ASbGncuC/JwVUkhC/Z1tdg9jfzCJ3Yljued6MM8CpWA8XcGwPsguqtFoNcnX07PGGj6 u0em3qBtyatv6+GlLSICKMcq3IKJ54JPBYqnpJ+OsbKeLvOpCCbKnOs/W7/4wwbsCw1dsOncVpK /WVfg88QzMtwlOpEv1pNMVchNfDQUkewthKADq45Lu0vA4qZjkpm96LkokkQmwrxJ0g64L8yX2X lQ7nyUp5tkJHAzDQ/J2V67enxQS+nEZhah4GMWBQJiimS9u4STbraRhljIZN0fgUu8IzwbiPwNA mIdcNhYwVOG+0AgOudmVepOpFAtxFDdG3Ayy5xnXGFiSHeGAhOMe0JGJkht/nlbiaUSQhfPiuzD psf3KbI6tn4dzfnCaWg== X-Google-Smtp-Source: AGHT+IFOsgLF6Acl3Dnu+kY+8WzAAhOB09Jk1srS30TZnn+BAtVaux5M4/L7RxPQOkhdypP2FHq2bw== X-Received: by 2002:a17:907:da7:b0:abf:749f:f719 with SMTP id a640c23a62f3a-ad17ad17aa7mr211271466b.7.1746180073795; Fri, 02 May 2025 03:01:13 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:13 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 11/14] tee: add tee_shm_alloc_cma_phys_mem() Date: Fri, 2 May 2025 11:59:25 +0200 Message-ID: <20250502100049.1746335-12-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add tee_shm_alloc_cma_phys_mem() to allocate a physical memory using from the default CMA pool. The memory is represented by a tee_shm object using the new flag TEE_SHM_CMA_BUF to identify it as physical memory from CMA. Signed-off-by: Jens Wiklander --- drivers/tee/tee_shm.c | 55 ++++++++++++++++++++++++++++++++++++++-- include/linux/tee_core.h | 4 +++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index e1ed52ee0a16..faaa0a87bb18 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -3,8 +3,11 @@ * Copyright (c) 2015-2017, 2019-2021 Linaro Limited */ #include +#include #include #include +#include +#include #include #include #include @@ -13,7 +16,6 @@ #include #include #include -#include #include "tee_private.h" =20 static void shm_put_kernel_pages(struct page **pages, size_t page_count) @@ -49,7 +51,14 @@ static void tee_shm_release(struct tee_device *teedev, s= truct tee_shm *shm) struct tee_shm *parent_shm =3D NULL; void *p =3D shm; =20 - if (shm->flags & TEE_SHM_DMA_BUF) { + if (shm->flags & TEE_SHM_CMA_BUF) { +#if IS_ENABLED(CONFIG_CMA) + struct page *page =3D phys_to_page(shm->paddr); + struct cma *cma =3D dev_get_cma_area(&shm->ctx->teedev->dev); + + cma_release(cma, page, shm->size / PAGE_SIZE); +#endif + } else if (shm->flags & TEE_SHM_DMA_BUF) { struct tee_shm_dmabuf_ref *ref; =20 ref =3D container_of(shm, struct tee_shm_dmabuf_ref, shm); @@ -306,6 +315,48 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_cont= ext *ctx, size_t size) } EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf); =20 +struct tee_shm *tee_shm_alloc_cma_phys_mem(struct tee_context *ctx, + size_t page_count, size_t align) +{ +#if IS_ENABLED(CONFIG_CMA) + struct tee_device *teedev =3D ctx->teedev; + struct cma *cma =3D dev_get_cma_area(&teedev->dev); + struct tee_shm *shm; + struct page *page; + + if (!tee_device_get(teedev)) + return ERR_PTR(-EINVAL); + + page =3D cma_alloc(cma, page_count, align, true/*no_warn*/); + if (!page) + goto err_put_teedev; + + shm =3D kzalloc(sizeof(*shm), GFP_KERNEL); + if (!shm) + goto err_cma_crelease; + + refcount_set(&shm->refcount, 1); + shm->ctx =3D ctx; + shm->paddr =3D page_to_phys(page); + shm->size =3D page_count * PAGE_SIZE; + shm->flags =3D TEE_SHM_CMA_BUF; + + teedev_ctx_get(ctx); + + return shm; + +err_cma_crelease: + cma_release(cma, page, page_count); +err_put_teedev: + tee_device_put(teedev); + + return ERR_PTR(-ENOMEM); +#else + return ERR_PTR(-EINVAL); +#endif +} +EXPORT_SYMBOL_GPL(tee_shm_alloc_cma_phys_mem); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t alig= n, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, diff --git a/include/linux/tee_core.h b/include/linux/tee_core.h index 02c07f661349..3a4e1b00fcc7 100644 --- a/include/linux/tee_core.h +++ b/include/linux/tee_core.h @@ -29,6 +29,7 @@ #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */ #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */ #define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */ +#define TEE_SHM_CMA_BUF BIT(5) /* CMA allocated memory */ =20 #define TEE_DEVICE_FLAG_REGISTERED 0x1 #define TEE_MAX_DEV_NAME_LEN 32 @@ -310,6 +311,9 @@ void *tee_get_drvdata(struct tee_device *teedev); */ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t siz= e); =20 +struct tee_shm *tee_shm_alloc_cma_phys_mem(struct tee_context *ctx, + size_t page_count, size_t align); + int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t alig= n, int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm, --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 0A7D0253321 for ; Fri, 2 May 2025 10:01:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180080; cv=none; b=Uj1NmD4CR+aU/RtPmsGUM4h6Pf8wLJqDrqWtGtlbHfaYs3gq8iSdNhzMjpojOzT7Gnzz8/fOiju8X26/XlOQg0yK1pdohUdVfeU2D6aOaMLIbIpTvxLUH2oGTcvdGdupYfns1iLf+WQWsM4bOaNHzIF6dWZxkJWWufDjO976+xY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180080; c=relaxed/simple; bh=qWKjVCqV0w+dT8t2+rlJ6OZyMHmR7QzWxO1E0IapEGo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fcuFmMfTZoZ6fUDHguU9qq3MQtBMktIFhGFMRei2uBBMamth2+S1HnMs0wLMTGmN1N5uHqMxGIPxU6jlCGHi8FZhUd2OLWrAkDJ6vhaklW1fprsAgw43qxGLt3W0+5G2T4rPdyAK6qK9HKqdmzy+hcCsPj/4tmGWbXH45VKEYdg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=YrBsu8dj; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="YrBsu8dj" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-aca99fc253bso286721366b.0 for ; Fri, 02 May 2025 03:01:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180076; x=1746784876; darn=vger.kernel.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=aeQOjVkq3EBC9GxZGgQiApE4KnUvSm/cM8uXSgvbEb0=; b=YrBsu8dj60THQ/lLgnbDxxj0ytkW+IB699DwzR4kwPi7K32LRQvG3Mt+UruelwfMDb KQa0boXjpXFUsqFezckawUTcGnrTSOhxWtSaYPvCAlfGKv3LSsvFfNb0ixrmqymAem+P V0CfDS5sg1oE3e0gPtzRR0aBlZD5FWem2yzvcTcwtN/95W8JVe3fvb4B/aPt3lUpUiYQ CkrxI2plcVNMz+5FkcZReP+6FoP5V7j7Pa04mir71WOGOjHr4JwOi6ydPQ9JAnKMdBXz t9A6AroV329bAcSyqF5kQ0a2dntQuAuanwwdSsHt/oX6/XLjGPjIGe5biEstzbDVo1KU CMwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180076; x=1746784876; 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=aeQOjVkq3EBC9GxZGgQiApE4KnUvSm/cM8uXSgvbEb0=; b=wJmyLj/SDBS7B3/8AVzKpuYr4L1PR5GJ9vTef5StsqevAWKQBi1T1kq5G2sQOLtBCA TaIn9n7P92obiZat+RzcHDCcP6ygfSOwUMlsLHhi18mB5hsato6pbxsYwwfXGN27Yd8/ T3dCWeSDz+vfqvf+PXyPdTvtTB4WkjZavEVE0hvkgisismmm1Uwo7zboi1mFQF/TbxI/ h5Syljv5veWbngVV7IdNyrZU+dr+yUz9ctXJdOpLpVCllcCtF8td8ix6j5kWgAAN785K nJ8o0VlBRwuxQUiiLyFTBZ9F/MQsst6q6Ml9utynmizGiyszlXEyUyIFmYZyWA1/75y+ Tf2Q== X-Gm-Message-State: AOJu0YyI7udq5N39CuHq/KRlx+wGZ4+P+HQWD/xbGPUrrJ071yk1Po36 EKTv5JV9SqkwBuPWzV66nTPPdZkGPRTM4fWYf3fRLMihlxuqa5UwCWvlLSq9yozeMkWZeCFRhmD AG2Q= X-Gm-Gg: ASbGncsA4CNIf6Q1Pr8zYw8a54z2c+oqT+DWlRWykfzL0zYJKdO9bzWBWSSMJIF9nv6 5WO/7yNbFtKSiKqHbqscCfQzBcPxFBOTs15Pcd7IIDFO0ZkplRx38E81MN4qf1k6SbBY0ooxX8i gscTwx3yGR3o+8ojsjMGmqtIwZ+y9M+o+ZXIiHfUKVz+vh22rnQXuJfsbW1uV5qNBDjzkTbe6lb pS21ZbljXJamY4GJ87gnk/BIW6M8WDarCkfGOPEt0YP3ru301jEfvf93x/U7xsRa91oE7nNcCZV AFbqzypiPalIeST1yVaJUjRtOHSEpBarGKjyWjdKbJCH+kp5L6OhXbYHg0wxCnxKZMUypdy/nZ1 sEVYtUg9tLeFxjTDVMA== X-Google-Smtp-Source: AGHT+IFF9zknIP+dqaL1V+FMTT0LVPgHDeyr+aipRO30IcpUVrj2BVxgOIBN2MQg2BYB/rbfAelPxg== X-Received: by 2002:a17:907:96a2:b0:ace:3c0b:1947 with SMTP id a640c23a62f3a-ad17ad39db1mr215652166b.4.1746180075654; Fri, 02 May 2025 03:01:15 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:15 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 12/14] optee: support protected memory allocation Date: Fri, 2 May 2025 11:59:26 +0200 Message-ID: <20250502100049.1746335-13-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support in the OP-TEE backend driver for protected memory allocation. The support is limited to only the SMC ABI and for secure video buffers. OP-TEE is probed for the range of protected physical memory and a memory pool allocator is initialized if OP-TEE have support for such memory. Signed-off-by: Jens Wiklander --- drivers/tee/optee/core.c | 1 + drivers/tee/optee/smc_abi.c | 44 +++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index c75fddc83576..c7fd8040480e 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -181,6 +181,7 @@ void optee_remove_common(struct optee *optee) tee_device_unregister(optee->supp_teedev); tee_device_unregister(optee->teedev); =20 + tee_device_unregister_all_dma_heaps(optee->teedev); tee_shm_pool_free(optee->pool); optee_supp_uninit(&optee->supp); mutex_destroy(&optee->call_queue.mutex); diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index cfdae266548b..7acb43852c4b 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -1620,6 +1620,41 @@ static inline int optee_load_fw(struct platform_devi= ce *pdev, } #endif =20 +static int optee_protmem_pool_init(struct optee *optee) +{ + enum tee_dma_heap_id heap_id =3D TEE_DMA_HEAP_SECURE_VIDEO_PLAY; + struct tee_protmem_pool *pool; + int rc; + + if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM) { + union { + struct arm_smccc_res smccc; + struct optee_smc_get_protmem_config_result result; + } res; + + optee->smc.invoke_fn(OPTEE_SMC_GET_PROTMEM_CONFIG, 0, 0, 0, 0, + 0, 0, 0, &res.smccc); + if (res.result.status !=3D OPTEE_SMC_RETURN_OK) { + pr_err("Secure Data Path service not available\n"); + return 0; + } + + pool =3D tee_protmem_static_pool_alloc(res.result.start, + res.result.size); + if (IS_ERR(pool)) + return PTR_ERR(pool); + + rc =3D tee_device_register_dma_heap(optee->teedev, heap_id, pool); + if (rc) + goto err; + } + + return 0; +err: + pool->ops->destroy_pool(pool); + return rc; +} + static int optee_probe(struct platform_device *pdev) { optee_invoke_fn *invoke_fn; @@ -1715,7 +1750,7 @@ static int optee_probe(struct platform_device *pdev) optee =3D kzalloc(sizeof(*optee), GFP_KERNEL); if (!optee) { rc =3D -ENOMEM; - goto err_free_pool; + goto err_free_shm_pool; } =20 optee->ops =3D &optee_ops; @@ -1788,6 +1823,10 @@ static int optee_probe(struct platform_device *pdev) pr_info("Asynchronous notifications enabled\n"); } =20 + rc =3D optee_protmem_pool_init(optee); + if (rc) + goto err_notif_uninit; + /* * Ensure that there are no pre-existing shm objects before enabling * the shm cache so that there's no chance of receiving an invalid @@ -1823,6 +1862,7 @@ static int optee_probe(struct platform_device *pdev) optee_disable_shm_cache(optee); optee_smc_notif_uninit_irq(optee); optee_unregister_devices(); + tee_device_unregister_all_dma_heaps(optee->teedev); err_notif_uninit: optee_notif_uninit(optee); err_close_ctx: @@ -1839,7 +1879,7 @@ static int optee_probe(struct platform_device *pdev) tee_device_unregister(optee->teedev); err_free_optee: kfree(optee); -err_free_pool: +err_free_shm_pool: tee_shm_pool_free(pool); if (memremaped_shm) memunmap(memremaped_shm); --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 DBDA92550AB for ; Fri, 2 May 2025 10:01:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180083; cv=none; b=ZoZHJJAaGTZvqRMkz9L/Q0VwE3PKHrsTDUdLr24jO/OhWJRRWhBebIc8+vfS0yU8Sk0AaJUh5c9ACYT7ps/kzDptGOLCi6G+GV061X97TUbs2ihKHHRVqEMOwk6iSubVjCmrT0/M7f7xIho3UwhIB5maiJ2I7/+bV5LU/eyPHkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180083; c=relaxed/simple; bh=oSZQ3DENCbIwwwzvczDrymUA6Xn2Fa0m63F4FSyye54=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NKBzxym5khcAB/LzjrZmJrUaxy4v4K6TOx3dG4D+BDQJnF+Nc5cs9Xd3psjBj4ffarodQgpKQ3RS2oCXRT+M8IwhqNCOdd6GjoxQIISC45BDfbICJiR+MQFS4d2Goeq+hmnaSB3mLTL7guaYjZ0VxeFLcTXDaOXDlwe1sO+z5bo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=rgw1FqO6; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="rgw1FqO6" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-ac345bd8e13so303852566b.0 for ; Fri, 02 May 2025 03:01:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180078; x=1746784878; darn=vger.kernel.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=CvUXav+GQi4h/Z/+dGorWUhrj+YXnlklNArIvigL5rg=; b=rgw1FqO6uVUgUgs+p2LgdGS/+RGh81MlSynk/djBpqE7wX1mciDIla2c3fhQokHHse TDEewvJx2C+J2dkxkqI59IO+JtWTWG1R+ltLgbuhRfTIMSrCKDc8ApqPIilCWVUBeogk rKCCAjX/zVIDpAKpxI3F9rTLumR9tz32zd+PuEy2om20kK9pLVAE1Oe9GP3Uy0P1mrzU tyObwESuyS4IS/WhfnKQ0ryyhAThuemQJmU3iEPz0lyKJtdN/rGyy6eeatqrRLdMOMXy 8cilj8ybJsnDjwDGuxwqUEnpKn5JOpJSsHDV//1hZsr3l8Emg6qKzVH7BEoX8UdWEr5B Ucow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180078; x=1746784878; 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=CvUXav+GQi4h/Z/+dGorWUhrj+YXnlklNArIvigL5rg=; b=oHydvaN3grqmz9IBmACzChcV69SlVxTBk+3k4pUu9RvLVNvww5gVWluzLIG3OZK/kI zX09n+9r9ZTfXn8H+eJQgFlrCaAPep9vftGciFBCC1iutApwkP0XVD2a25TbhKyAx4RK N/YsiEFh5Z21WZnuJETP2DdxBOKiydlvMy+/kPsXv2jqKZltIo7+rAafyn+71A2R0VMw q3lUOsHfOLxkEfWDbpXS0P+L+wL2YQ/CL+rk3uXDvf+V3b88tfBVfVeG5Ac0zqbLwuUO Fd68TURJyKAKdA5hjq9hIkcePdLEnRp09QVJl/q3w+N1Ue29D1L0XF1GFjP8iEXn9DD8 S2aw== X-Gm-Message-State: AOJu0YwLcXkeW+JYft+Ff0CWRTwlMJ7lBvqMJ7bG19pschXFloG8ME0h p9gmtCOtpdlUNL+p36vXFS8arAJ672Bb6QvbJt9ZyswhjGmtqAroRFDPppYqCeqeLvBufGsg2VM QbPM= X-Gm-Gg: ASbGnct3HQvlr+AgiYDizhRKwMNwaPo0usZnxmKc2Bjqgsi72P3ZaQphQD0rUtDSQXJ Fer3JU4dr/sg98JqturYLF+hDZA/A73ABmYWmFLLX7GdzRB6oThZknOIelQaobPyaoojZoidcT8 hfbvXh15w6uyz2tb9JYIvPWaPjoXsZ7no6mJl9A64wYGoV+UPspraJvjmJ0nYjjbV1tHYO9kYjR Q6hX1UEmAs6pkUTzlbs6IPDR1F898w76prZ2nkf08uEnBdkJG9o+vTeAGB8Vi/ai3uqaN7Vi+kf +JxLk0GRdSZx6PagSe2+y/+3RvmZtyfCJz6l4dnT+WcpnheYn8X1qLaw159Y9qUN8gbigV6/jis ZvvAfLKDn/N38ZpnjNLTZ8BBzjGMk X-Google-Smtp-Source: AGHT+IFCbPXsg9uHWcMB5b0Lwzc++35Wsw+gbQ2upqff1Ugc/ZLKw2zYFdCgKrXu5dXeM3dXWx1XPg== X-Received: by 2002:a17:907:8946:b0:aca:a3da:80b8 with SMTP id a640c23a62f3a-ad17afbe1b6mr200782766b.55.1746180077533; Fri, 02 May 2025 03:01:17 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:16 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 13/14] optee: FF-A: dynamic protected memory allocation Date: Fri, 2 May 2025 11:59:27 +0200 Message-ID: <20250502100049.1746335-14-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support in the OP-TEE backend driver dynamic protected memory allocation with FF-A. The protected memory pools for dynamically allocated protected memory are instantiated when requested by user-space. This instantiation can fail if OP-TEE doesn't support the requested use-case of protected memory. Restricted memory pools based on a static carveout or dynamic allocation can coexist for different use-cases. We use only dynamic allocation with FF-A. Signed-off-by: Jens Wiklander --- drivers/tee/optee/Makefile | 1 + drivers/tee/optee/ffa_abi.c | 147 ++++++++++++- drivers/tee/optee/optee_private.h | 13 +- drivers/tee/optee/protmem.c | 330 ++++++++++++++++++++++++++++++ 4 files changed, 488 insertions(+), 3 deletions(-) create mode 100644 drivers/tee/optee/protmem.c diff --git a/drivers/tee/optee/Makefile b/drivers/tee/optee/Makefile index a6eff388d300..ad7049c1c107 100644 --- a/drivers/tee/optee/Makefile +++ b/drivers/tee/optee/Makefile @@ -4,6 +4,7 @@ optee-objs +=3D core.o optee-objs +=3D call.o optee-objs +=3D notif.o optee-objs +=3D rpc.o +optee-objs +=3D protmem.o optee-objs +=3D supp.o optee-objs +=3D device.o optee-objs +=3D smc_abi.o diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c index e4b08cd195f3..e1f02fc42c2f 100644 --- a/drivers/tee/optee/ffa_abi.c +++ b/drivers/tee/optee/ffa_abi.c @@ -672,6 +672,124 @@ static int optee_ffa_do_call_with_arg(struct tee_cont= ext *ctx, return optee_ffa_yielding_call(ctx, &data, rpc_arg, system_thread); } =20 +static int do_call_lend_protmem(struct optee *optee, u64 cookie, u32 use_c= ase) +{ + struct optee_shm_arg_entry *entry; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + msg_arg =3D optee_get_msg_arg(optee->ctx, 1, &entry, &shm, &offs); + if (IS_ERR(msg_arg)) + return PTR_ERR(msg_arg); + + msg_arg->cmd =3D OPTEE_MSG_CMD_ASSIGN_PROTMEM; + msg_arg->params[0].attr =3D OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; + msg_arg->params[0].u.value.a =3D cookie; + msg_arg->params[0].u.value.b =3D use_case; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out; + if (msg_arg->ret !=3D TEEC_SUCCESS) { + rc =3D -EINVAL; + goto out; + } + +out: + optee_free_msg_arg(optee->ctx, entry, offs); + return rc; +} + +static int optee_ffa_lend_protmem(struct optee *optee, struct tee_shm *pro= tmem, + u16 *end_points, unsigned int ep_count, + u32 use_case) +{ + struct ffa_device *ffa_dev =3D optee->ffa.ffa_dev; + const struct ffa_mem_ops *mem_ops =3D ffa_dev->ops->mem_ops; + const struct ffa_msg_ops *msg_ops =3D ffa_dev->ops->msg_ops; + struct ffa_send_direct_data data; + struct ffa_mem_region_attributes *mem_attr; + struct ffa_mem_ops_args args =3D { + .use_txbuf =3D true, + .tag =3D use_case, + }; + struct page *page; + struct scatterlist sgl; + unsigned int n; + int rc; + + mem_attr =3D kcalloc(ep_count, sizeof(*mem_attr), GFP_KERNEL); + for (n =3D 0; n < ep_count; n++) { + mem_attr[n].receiver =3D end_points[n]; + mem_attr[n].attrs =3D FFA_MEM_RW; + } + args.attrs =3D mem_attr; + args.nattrs =3D ep_count; + + page =3D phys_to_page(protmem->paddr); + sg_init_table(&sgl, 1); + sg_set_page(&sgl, page, protmem->size, 0); + + args.sg =3D &sgl; + rc =3D mem_ops->memory_lend(&args); + kfree(mem_attr); + if (rc) + return rc; + + rc =3D do_call_lend_protmem(optee, args.g_handle, use_case); + if (rc) + goto err_reclaim; + + rc =3D optee_shm_add_ffa_handle(optee, protmem, args.g_handle); + if (rc) + goto err_unreg; + + protmem->sec_world_id =3D args.g_handle; + + return 0; + +err_unreg: + data =3D (struct ffa_send_direct_data){ + .data0 =3D OPTEE_FFA_RELEASE_PROTMEM, + .data1 =3D (u32)args.g_handle, + .data2 =3D (u32)(args.g_handle >> 32), + }; + msg_ops->sync_send_receive(ffa_dev, &data); +err_reclaim: + mem_ops->memory_reclaim(args.g_handle, 0); + return rc; +} + +static int optee_ffa_reclaim_protmem(struct optee *optee, + struct tee_shm *protmem) +{ + struct ffa_device *ffa_dev =3D optee->ffa.ffa_dev; + const struct ffa_msg_ops *msg_ops =3D ffa_dev->ops->msg_ops; + const struct ffa_mem_ops *mem_ops =3D ffa_dev->ops->mem_ops; + u64 global_handle =3D protmem->sec_world_id; + struct ffa_send_direct_data data =3D { + .data0 =3D OPTEE_FFA_RELEASE_PROTMEM, + .data1 =3D (u32)global_handle, + .data2 =3D (u32)(global_handle >> 32) + }; + int rc; + + optee_shm_rem_ffa_handle(optee, global_handle); + protmem->sec_world_id =3D 0; + + rc =3D msg_ops->sync_send_receive(ffa_dev, &data); + if (rc) + pr_err("Release SHM id 0x%llx rc %d\n", global_handle, rc); + + rc =3D mem_ops->memory_reclaim(global_handle, 0); + if (rc) + pr_err("mem_reclaim: 0x%llx %d", global_handle, rc); + + return rc; +} + /* * 6. Driver initialization * @@ -833,6 +951,8 @@ static const struct optee_ops optee_ffa_ops =3D { .do_call_with_arg =3D optee_ffa_do_call_with_arg, .to_msg_param =3D optee_ffa_to_msg_param, .from_msg_param =3D optee_ffa_from_msg_param, + .lend_protmem =3D optee_ffa_lend_protmem, + .reclaim_protmem =3D optee_ffa_reclaim_protmem, }; =20 static void optee_ffa_remove(struct ffa_device *ffa_dev) @@ -891,6 +1011,25 @@ static int optee_ffa_async_notif_init(struct ffa_devi= ce *ffa_dev, return rc; } =20 +static int optee_ffa_protmem_pool_init(struct optee *optee, u32 sec_caps) +{ + enum tee_dma_heap_id id =3D TEE_DMA_HEAP_SECURE_VIDEO_PLAY; + struct tee_protmem_pool *pool; + int rc =3D 0; + + if (sec_caps & OPTEE_FFA_SEC_CAP_PROTMEM) { + pool =3D optee_protmem_alloc_cma_pool(optee, id); + if (IS_ERR(pool)) + return PTR_ERR(pool); + + rc =3D tee_device_register_dma_heap(optee->teedev, id, pool); + if (rc) + pool->ops->destroy_pool(pool); + } + + return rc; +} + static int optee_ffa_probe(struct ffa_device *ffa_dev) { const struct ffa_notifier_ops *notif_ops; @@ -941,7 +1080,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) optee->pool, optee); if (IS_ERR(teedev)) { rc =3D PTR_ERR(teedev); - goto err_free_pool; + goto err_free_shm_pool; } optee->teedev =3D teedev; =20 @@ -988,6 +1127,9 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) rc); } =20 + if (optee_ffa_protmem_pool_init(optee, sec_caps)) + pr_info("Protected memory service not available\n"); + rc =3D optee_enumerate_devices(PTA_CMD_GET_DEVICES); if (rc) goto err_unregister_devices; @@ -1001,6 +1143,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) =20 err_unregister_devices: optee_unregister_devices(); + tee_device_unregister_all_dma_heaps(optee->teedev); if (optee->ffa.bottom_half_value !=3D U32_MAX) notif_ops->notify_relinquish(ffa_dev, optee->ffa.bottom_half_value); @@ -1018,7 +1161,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) tee_device_unregister(optee->supp_teedev); err_unreg_teedev: tee_device_unregister(optee->teedev); -err_free_pool: +err_free_shm_pool: tee_shm_pool_free(pool); err_free_optee: kfree(optee); diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index 20eda508dbac..7c53433f6562 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -174,9 +174,14 @@ struct optee; * @do_call_with_arg: enters OP-TEE in secure world * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param + * @lend_protmem: lends physically contiguous memory as restricted + * memory, inaccessible by the kernel + * @reclaim_protmem: reclaims restricted memory previously lent with + * @lend_protmem() and makes it accessible by the + * kernel again * * These OPs are only supposed to be used internally in the OP-TEE driver - * as a way of abstracting the different methogs of entering OP-TEE in + * as a way of abstracting the different methods of entering OP-TEE in * secure world. */ struct optee_ops { @@ -191,6 +196,10 @@ struct optee_ops { size_t num_params, const struct optee_msg_param *msg_params, bool update_out); + int (*lend_protmem)(struct optee *optee, struct tee_shm *protmem, + u16 *end_points, unsigned int ep_count, + u32 use_case); + int (*reclaim_protmem)(struct optee *optee, struct tee_shm *protmem); }; =20 /** @@ -285,6 +294,8 @@ u32 optee_supp_thrd_req(struct tee_context *ctx, u32 fu= nc, size_t num_params, void optee_supp_init(struct optee_supp *supp); void optee_supp_uninit(struct optee_supp *supp); void optee_supp_release(struct optee_supp *supp); +struct tee_protmem_pool *optee_protmem_alloc_cma_pool(struct optee *optee, + enum tee_dma_heap_id id); =20 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params, struct tee_param *param); diff --git a/drivers/tee/optee/protmem.c b/drivers/tee/optee/protmem.c new file mode 100644 index 000000000000..e8d6b750a881 --- /dev/null +++ b/drivers/tee/optee/protmem.c @@ -0,0 +1,330 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2025, Linaro Limited + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include "optee_private.h" + +struct optee_protmem_cma_pool { + struct tee_protmem_pool pool; + struct gen_pool *gen_pool; + struct optee *optee; + size_t page_count; + u16 *end_points; + u_int end_point_count; + u_int align; + refcount_t refcount; + u32 use_case; + struct tee_shm *protmem; + /* Protects when initializing and tearing down this struct */ + struct mutex mutex; +}; + +static struct optee_protmem_cma_pool * +to_protmem_cma_pool(struct tee_protmem_pool *pool) +{ + return container_of(pool, struct optee_protmem_cma_pool, pool); +} + +static int init_cma_protmem(struct optee_protmem_cma_pool *rp) +{ + int rc; + + rp->protmem =3D tee_shm_alloc_cma_phys_mem(rp->optee->ctx, rp->page_count, + rp->align); + if (IS_ERR(rp->protmem)) { + rc =3D PTR_ERR(rp->protmem); + goto err_null_protmem; + } + + /* + * TODO unmap the memory range since the physical memory will + * become inaccesible after the lend_protmem() call. + */ + rc =3D rp->optee->ops->lend_protmem(rp->optee, rp->protmem, + rp->end_points, + rp->end_point_count, rp->use_case); + if (rc) + goto err_put_shm; + rp->protmem->flags |=3D TEE_SHM_DYNAMIC; + + rp->gen_pool =3D gen_pool_create(PAGE_SHIFT, -1); + if (!rp->gen_pool) { + rc =3D -ENOMEM; + goto err_reclaim; + } + + rc =3D gen_pool_add(rp->gen_pool, rp->protmem->paddr, + rp->protmem->size, -1); + if (rc) + goto err_free_pool; + + refcount_set(&rp->refcount, 1); + return 0; + +err_free_pool: + gen_pool_destroy(rp->gen_pool); + rp->gen_pool =3D NULL; +err_reclaim: + rp->optee->ops->reclaim_protmem(rp->optee, rp->protmem); +err_put_shm: + tee_shm_put(rp->protmem); +err_null_protmem: + rp->protmem =3D NULL; + return rc; +} + +static int get_cma_protmem(struct optee_protmem_cma_pool *rp) +{ + int rc =3D 0; + + if (!refcount_inc_not_zero(&rp->refcount)) { + mutex_lock(&rp->mutex); + if (rp->gen_pool) { + /* + * Another thread has already initialized the pool + * before us, or the pool was just about to be torn + * down. Either way we only need to increase the + * refcount and we're done. + */ + refcount_inc(&rp->refcount); + } else { + rc =3D init_cma_protmem(rp); + } + mutex_unlock(&rp->mutex); + } + + return rc; +} + +static void release_cma_protmem(struct optee_protmem_cma_pool *rp) +{ + gen_pool_destroy(rp->gen_pool); + rp->gen_pool =3D NULL; + + rp->optee->ops->reclaim_protmem(rp->optee, rp->protmem); + rp->protmem->flags &=3D ~TEE_SHM_DYNAMIC; + + WARN(refcount_read(&rp->protmem->refcount) !=3D 1, "Unexpected refcount"); + tee_shm_put(rp->protmem); + rp->protmem =3D NULL; +} + +static void put_cma_protmem(struct optee_protmem_cma_pool *rp) +{ + if (refcount_dec_and_test(&rp->refcount)) { + mutex_lock(&rp->mutex); + if (rp->gen_pool) + release_cma_protmem(rp); + mutex_unlock(&rp->mutex); + } +} + +static int protmem_pool_op_cma_alloc(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t size, + size_t *offs) +{ + struct optee_protmem_cma_pool *rp =3D to_protmem_cma_pool(pool); + size_t sz =3D ALIGN(size, PAGE_SIZE); + phys_addr_t pa; + int rc; + + rc =3D get_cma_protmem(rp); + if (rc) + return rc; + + pa =3D gen_pool_alloc(rp->gen_pool, sz); + if (!pa) { + rc =3D -ENOMEM; + goto err_put; + } + + rc =3D sg_alloc_table(sgt, 1, GFP_KERNEL); + if (rc) + goto err_free; + + sg_set_page(sgt->sgl, phys_to_page(pa), size, 0); + *offs =3D pa - rp->protmem->paddr; + + return 0; +err_free: + gen_pool_free(rp->gen_pool, pa, size); +err_put: + put_cma_protmem(rp); + + return rc; +} + +static void protmem_pool_op_cma_free(struct tee_protmem_pool *pool, + struct sg_table *sgt) +{ + struct optee_protmem_cma_pool *rp =3D to_protmem_cma_pool(pool); + struct scatterlist *sg; + int i; + + for_each_sgtable_sg(sgt, sg, i) + gen_pool_free(rp->gen_pool, sg_phys(sg), sg->length); + sg_free_table(sgt); + put_cma_protmem(rp); +} + +static int protmem_pool_op_cma_update_shm(struct tee_protmem_pool *pool, + struct sg_table *sgt, size_t offs, + struct tee_shm *shm, + struct tee_shm **parent_shm) +{ + struct optee_protmem_cma_pool *rp =3D to_protmem_cma_pool(pool); + + *parent_shm =3D rp->protmem; + + return 0; +} + +static void pool_op_cma_destroy_pool(struct tee_protmem_pool *pool) +{ + struct optee_protmem_cma_pool *rp =3D to_protmem_cma_pool(pool); + + mutex_destroy(&rp->mutex); + kfree(rp); +} + +static struct tee_protmem_pool_ops protmem_pool_ops_cma =3D { + .alloc =3D protmem_pool_op_cma_alloc, + .free =3D protmem_pool_op_cma_free, + .update_shm =3D protmem_pool_op_cma_update_shm, + .destroy_pool =3D pool_op_cma_destroy_pool, +}; + +static int get_protmem_config(struct optee *optee, u32 use_case, + size_t *min_size, u_int *min_align, + u16 *end_points, u_int *ep_count) +{ + struct tee_param params[2] =3D { + [0] =3D { + .attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT, + .u.value.a =3D use_case, + }, + [1] =3D { + .attr =3D TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT, + }, + }; + struct optee_shm_arg_entry *entry; + struct tee_shm *shm_param =3D NULL; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + if (end_points && *ep_count) { + params[1].u.memref.size =3D *ep_count * sizeof(*end_points); + shm_param =3D tee_shm_alloc_priv_buf(optee->ctx, + params[1].u.memref.size); + if (IS_ERR(shm_param)) + return PTR_ERR(shm_param); + params[1].u.memref.shm =3D shm_param; + } + + msg_arg =3D optee_get_msg_arg(optee->ctx, ARRAY_SIZE(params), &entry, + &shm, &offs); + if (IS_ERR(msg_arg)) { + rc =3D PTR_ERR(msg_arg); + goto out_free_shm; + } + msg_arg->cmd =3D OPTEE_MSG_CMD_GET_PROTMEM_CONFIG; + + rc =3D optee->ops->to_msg_param(optee, msg_arg->params, + ARRAY_SIZE(params), params, + false /*!update_out*/); + if (rc) + goto out_free_msg; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out_free_msg; + if (msg_arg->ret && msg_arg->ret !=3D TEEC_ERROR_SHORT_BUFFER) { + rc =3D -EINVAL; + goto out_free_msg; + } + + rc =3D optee->ops->from_msg_param(optee, params, ARRAY_SIZE(params), + msg_arg->params, true /*update_out*/); + if (rc) + goto out_free_msg; + + if (!msg_arg->ret && end_points && + *ep_count < params[1].u.memref.size / sizeof(u16)) { + rc =3D -EINVAL; + goto out_free_msg; + } + + *min_size =3D params[0].u.value.a; + *min_align =3D params[0].u.value.b; + *ep_count =3D params[1].u.memref.size / sizeof(u16); + + if (msg_arg->ret =3D=3D TEEC_ERROR_SHORT_BUFFER) { + rc =3D -ENOSPC; + goto out_free_msg; + } + + if (end_points) + memcpy(end_points, tee_shm_get_va(shm_param, 0), + params[1].u.memref.size); + +out_free_msg: + optee_free_msg_arg(optee->ctx, entry, offs); +out_free_shm: + if (shm_param) + tee_shm_free(shm_param); + return rc; +} + +struct tee_protmem_pool *optee_protmem_alloc_cma_pool(struct optee *optee, + enum tee_dma_heap_id id) +{ + struct optee_protmem_cma_pool *rp; + u32 use_case =3D id; + size_t min_size; + int rc; + + rp =3D kzalloc(sizeof(*rp), GFP_KERNEL); + if (!rp) + return ERR_PTR(-ENOMEM); + rp->use_case =3D use_case; + + rc =3D get_protmem_config(optee, use_case, &min_size, &rp->align, NULL, + &rp->end_point_count); + if (rc) { + if (rc !=3D -ENOSPC) + goto err; + rp->end_points =3D kcalloc(rp->end_point_count, + sizeof(*rp->end_points), GFP_KERNEL); + if (!rp->end_points) { + rc =3D -ENOMEM; + goto err; + } + rc =3D get_protmem_config(optee, use_case, &min_size, &rp->align, + rp->end_points, &rp->end_point_count); + if (rc) + goto err_kfree_eps; + } + + rp->pool.ops =3D &protmem_pool_ops_cma; + rp->optee =3D optee; + rp->page_count =3D min_size / PAGE_SIZE; + mutex_init(&rp->mutex); + + return &rp->pool; + +err_kfree_eps: + kfree(rp->end_points); +err: + kfree(rp); + return ERR_PTR(rc); +} --=20 2.43.0 From nobody Fri Dec 19 04:23:28 2025 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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 E0EE0246790 for ; Fri, 2 May 2025 10:01:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180084; cv=none; b=L38GDCBGI89qJRxc1cJPSomT6ry5YX5bmVn9yTE7tZyK+bxZiIuCJaFUbXEIldAr9T/emKA/5ONcFehZbMzBVt3Bn8JqRVTMW7N1xwJ6ClH3S1cQeEgg9pVeGum13Q+vB1K7MA2tjs7/oT03SQZ1DBUMDyVl5xw/ITxb468sKx8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746180084; c=relaxed/simple; bh=ZUwd027OBe08q9YquZgjzVa7y9MBnIPHNh4bwe/oj4A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YPB3P7aR5BXnI3yQSHPMbrxiXqWP7rm/KUMZUWerV1k+3kBVQM4kE69tp7DnH4I4HnLDboTmxz0tbqx2Bs2Rbz/1OkEFZ4EuYNB8UhSqtvjxcBfFQXH8+VwjotpHACgxIb67nRRHwQt9GfWpbU/+yiU4iGlpUEFZ90dEM0VLkaY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=Sblha9Uu; arc=none smtp.client-ip=209.85.218.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Sblha9Uu" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-acbb48bad09so323450866b.0 for ; Fri, 02 May 2025 03:01:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1746180080; x=1746784880; darn=vger.kernel.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=OlmCXINH+dafzBgYNqgq0wXLz20MQsNU7TGuhsodLww=; b=Sblha9UuBFwGdLzFIysbFAxi7PT4Gcn5iwHs4hWXknwgdomnQh++J+gnfryQU+WWEC OTWXj5RjZNVScIH4MJLgs1FsrgizetRO2RNyUxyrQsngYbGTpHycD6Zj2YlpDLExYhqF Y8fOnm9qfwy21vRD91URaydv+h1AW1g33vxt/gY+DTlM95R3Q7odqJAVjk4V8cl9zG+Z FVQI0IMlDneVsNp6CHT6OAF+6Sndg+BE+63mESheBK53WDrzKcGMO+4w6ZF9t9Nzlwi5 qH8AO9Qzn29RSVsUrRX/FyjbiQMZw2jBLPtFA5hs1/j5naudVWdtiwlxq0FdzjxaZCvl A2YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746180080; x=1746784880; 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=OlmCXINH+dafzBgYNqgq0wXLz20MQsNU7TGuhsodLww=; b=if4jjRLCCtw/ckjmZjvX/bDOWmtmUo9YXW/2GS8/X+XKNtsCdRvsxLZPMs2oeAeaDI x0rfETsvv8O5apTd0OhZDlOEeDKkVvlifi3GdvjO7idC7UZCkIzAQ+j0Rz0ceF6YvOnB jJHnYxrHNzWBJ/sti51aiVbwwPGTjR01/eZBKr11P8mvkeZhxm8w+7xfu/xamgiDHm1B 2zbjVifTc8FAa3cE/quG8gtbds2z9Se5CUfxgMCTooTqZnNR3GyTIvdX+E2P8NOMXkZS dxqezUdXzl3WlPSa9xB6EWH15dPmw6dQB9YNS0Mt7o3hNjx83hA0EBSAKnH1tqd6tEoT iw5A== X-Gm-Message-State: AOJu0YwkC9cIzMS4BbZRCShTJnHB0sq/Tg1BNKspNGihOxKPV1nPNssJ YdTIuZfGrztc6TujUNTxcyRDK8Zn7mZmNnIxdaps18DMexSYYWr92X50J4eIZT8g+/tED/Fy1oW 7ie8= X-Gm-Gg: ASbGnctvsLOpYViu6rcU74PWS3yGUeOpIPSoJSEjeDjyFQHIIRCmQordE4d4WQaz04C TVdOYY0G6/AkR99uE62GK8z5Z7wD7ekD/OJioHGom8eP0A05xBN/0ewn3E2OA6K43go+C+KjOzO y6YBiJOSdqfqqrn3aOEnicLiKRqMGImO9Vp3TgLV+Ano3XVMXDFMwusw6JDUgxfe4Uczp2ssTmk Nm1mi3sop+0jPQ7/VPN8JPDQ1Q+BnOpW1lhO73hy8e5GxwUmtiWPGYM6JYqZohj9whoePHJ9pu1 wDuX+xnpfC47U6Vioay/4RvTsI6jc9LZyqSjfWOKfYi4nluybZfjdihU4qQIXitrFC+YMwXG4bl S1a1psm0GoG+tfoop2A== X-Google-Smtp-Source: AGHT+IEYfv3P+wsN6CjsHEtvD+7RxYmI0A0Zuh0sF27ecW7PLXPxnJ4H/XpyhrWak3QZy6czVPn/hg== X-Received: by 2002:a17:907:728a:b0:ace:5461:81dd with SMTP id a640c23a62f3a-ad17acfda03mr249587766b.3.1746180079442; Fri, 02 May 2025 03:01:19 -0700 (PDT) Received: from rayden.urgonet (h-98-128-140-123.A175.priv.bahnhof.se. [98.128.140.123]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad1891473a1sm26030566b.4.2025.05.02.03.01.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 May 2025 03:01:18 -0700 (PDT) From: Jens Wiklander To: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, op-tee@lists.trustedfirmware.org, linux-arm-kernel@lists.infradead.org Cc: Olivier Masse , Thierry Reding , Yong Wu , Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T . J . Mercier" , =?UTF-8?q?Christian=20K=C3=B6nig?= , Sumit Garg , Matthias Brugger , AngeloGioacchino Del Regno , azarrabi@qti.qualcomm.com, Simona Vetter , Daniel Stone , Rouven Czerwinski , Jens Wiklander Subject: [PATCH v8 14/14] optee: smc abi: dynamic protected memory allocation Date: Fri, 2 May 2025 11:59:28 +0200 Message-ID: <20250502100049.1746335-15-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250502100049.1746335-1-jens.wiklander@linaro.org> References: <20250502100049.1746335-1-jens.wiklander@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support in the OP-TEE backend driver for dynamic protected memory allocation using the SMC ABI. Signed-off-by: Jens Wiklander --- drivers/tee/optee/smc_abi.c | 103 +++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 18 deletions(-) diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 7acb43852c4b..766e7f5a3953 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -1001,6 +1001,70 @@ static int optee_smc_do_call_with_arg(struct tee_con= text *ctx, return rc; } =20 +static int optee_smc_lend_protmem(struct optee *optee, struct tee_shm *pro= tmem, + u16 *end_points, unsigned int ep_count, + u32 use_case) +{ + struct optee_shm_arg_entry *entry; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + msg_arg =3D optee_get_msg_arg(optee->ctx, 2, &entry, &shm, &offs); + if (IS_ERR(msg_arg)) + return PTR_ERR(msg_arg); + + msg_arg->cmd =3D OPTEE_MSG_CMD_LEND_PROTMEM; + msg_arg->params[0].attr =3D OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; + msg_arg->params[0].u.value.a =3D use_case; + msg_arg->params[1].attr =3D OPTEE_MSG_ATTR_TYPE_TMEM_INPUT; + msg_arg->params[1].u.tmem.buf_ptr =3D protmem->paddr; + msg_arg->params[1].u.tmem.size =3D protmem->size; + msg_arg->params[1].u.tmem.shm_ref =3D (u_long)protmem; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out; + if (msg_arg->ret !=3D TEEC_SUCCESS) { + rc =3D -EINVAL; + goto out; + } + protmem->sec_world_id =3D (u_long)protmem; + +out: + optee_free_msg_arg(optee->ctx, entry, offs); + return rc; +} + +static int optee_smc_reclaim_protmem(struct optee *optee, + struct tee_shm *protmem) +{ + struct optee_shm_arg_entry *entry; + struct optee_msg_arg *msg_arg; + struct tee_shm *shm; + u_int offs; + int rc; + + msg_arg =3D optee_get_msg_arg(optee->ctx, 1, &entry, &shm, &offs); + if (IS_ERR(msg_arg)) + return PTR_ERR(msg_arg); + + msg_arg->cmd =3D OPTEE_MSG_CMD_RECLAIM_PROTMEM; + msg_arg->params[0].attr =3D OPTEE_MSG_ATTR_TYPE_RMEM_INPUT; + msg_arg->params[0].u.rmem.shm_ref =3D (u_long)protmem; + + rc =3D optee->ops->do_call_with_arg(optee->ctx, shm, offs, false); + if (rc) + goto out; + if (msg_arg->ret !=3D TEEC_SUCCESS) + rc =3D -EINVAL; + +out: + optee_free_msg_arg(optee->ctx, entry, offs); + return rc; +} + /* * 5. Asynchronous notification */ @@ -1252,6 +1316,8 @@ static const struct optee_ops optee_ops =3D { .do_call_with_arg =3D optee_smc_do_call_with_arg, .to_msg_param =3D optee_to_msg_param, .from_msg_param =3D optee_from_msg_param, + .lend_protmem =3D optee_smc_lend_protmem, + .reclaim_protmem =3D optee_smc_reclaim_protmem, }; =20 static int enable_async_notif(optee_invoke_fn *invoke_fn) @@ -1622,11 +1688,14 @@ static inline int optee_load_fw(struct platform_dev= ice *pdev, =20 static int optee_protmem_pool_init(struct optee *optee) { + bool protm =3D optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM; + bool dyn_protm =3D optee->smc.sec_caps & + OPTEE_SMC_SEC_CAP_DYNAMIC_PROTMEM; enum tee_dma_heap_id heap_id =3D TEE_DMA_HEAP_SECURE_VIDEO_PLAY; - struct tee_protmem_pool *pool; - int rc; + struct tee_protmem_pool *pool =3D ERR_PTR(-EINVAL); + int rc =3D -EINVAL; =20 - if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM) { + if (protm) { union { struct arm_smccc_res smccc; struct optee_smc_get_protmem_config_result result; @@ -1634,25 +1703,24 @@ static int optee_protmem_pool_init(struct optee *op= tee) =20 optee->smc.invoke_fn(OPTEE_SMC_GET_PROTMEM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc); - if (res.result.status !=3D OPTEE_SMC_RETURN_OK) { - pr_err("Secure Data Path service not available\n"); - return 0; - } + if (res.result.status =3D=3D OPTEE_SMC_RETURN_OK) + pool =3D tee_protmem_static_pool_alloc(res.result.start, + res.result.size); + } =20 - pool =3D tee_protmem_static_pool_alloc(res.result.start, - res.result.size); - if (IS_ERR(pool)) - return PTR_ERR(pool); + if (dyn_protm && IS_ERR(pool)) + pool =3D optee_protmem_alloc_cma_pool(optee, heap_id); =20 + if (!IS_ERR(pool)) { rc =3D tee_device_register_dma_heap(optee->teedev, heap_id, pool); if (rc) - goto err; + pool->ops->destroy_pool(pool); } =20 + if (protm || dyn_protm) + return rc; + return 0; -err: - pool->ops->destroy_pool(pool); - return rc; } =20 static int optee_probe(struct platform_device *pdev) @@ -1823,9 +1891,8 @@ static int optee_probe(struct platform_device *pdev) pr_info("Asynchronous notifications enabled\n"); } =20 - rc =3D optee_protmem_pool_init(optee); - if (rc) - goto err_notif_uninit; + if (optee_protmem_pool_init(optee)) + pr_info("Protected memory service not available\n"); =20 /* * Ensure that there are no pre-existing shm objects before enabling --=20 2.43.0