From nobody Mon Feb 9 17:05:27 2026 Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.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 DE19A1F8ACA for ; Fri, 4 Apr 2025 14:32:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743777169; cv=none; b=hdZ0aYVrYtX/nkbweDuEUnwXmnMy7rCk6k/9YzVEk9vsu6bUGp7ZRRzwGJfbSuIDoofpi9rjMrTapq5XIsUVZZ061ztfYGH2j33BUBFsYMtHmjA1oOVIDNk/rYmJ9Vyxu6BVY1JSfImy7Xow5wFqE+woy/xA9Y5MXRy5QCD7JWc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743777169; c=relaxed/simple; bh=xHB8JHjf/tOVUigrC/8AjwRmS/VCkvn1T1vcXrDlRsY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V98XQ3KHPizzmW5BU2bIO5cZISXPgVjXW79x53BohAEBuuMMreQoZ2gc7yV3YqUlIXQUF96++mPHD9H/FVxZQh40s9JVlGDqdigqCIJ3VoMuVtjikEeO8xuScuF6TwAa3MPQSFW/KwfXuccwGrTlH8t9eG5MUsCFRnz29B6s6YE= 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=uriEuaJZ; arc=none smtp.client-ip=209.85.208.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="uriEuaJZ" Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-5e5e34f4e89so4138248a12.1 for ; Fri, 04 Apr 2025 07:32:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743777166; x=1744381966; 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=vPfYhkbEaDQyvItBPqD6f0Sef9tTAM/sN3gZBFbfI4Q=; b=uriEuaJZdUPLjwZtsi8S67HcDJT5KNCorcdh+Irh2AqfN3nQCxuvcwpQ4vv3XLWQwr olQu31GvYq2O2wOYfQuFfZcmcf88iGKcQAhnb2oq9hjOHHWgahWHnNDhLWvGgO0MeJ2r xQI7jwM6UPAoNXSNuus2OLHdx6kb5rb4exyjwySGEVfvm7F4mganKYgB5/xTLRJmJ3hP une0Rr3/6qbt6ad2FhVTv6M3HVhdZjIE7etVXJEyuuaO1GJ1XS+rKdb/E4zJpdsJT7OS a94nWa0o5vn7QsTeAuo6ao9uxIwVUa/qLuT+nbOZrdFhAlBRln+97JXAWoKh/Kj/orzz /rLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743777166; x=1744381966; 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=vPfYhkbEaDQyvItBPqD6f0Sef9tTAM/sN3gZBFbfI4Q=; b=tDQ6kXhFJwciz+MG8XgGV8nzhIKXb3woZUwiGfegH34bDjYQL9mMSqPND6JWUJIQpG p/0kSFALBqUt+CubUn7mXf70HgB4d8gkSVbhlLl3ons1qvdBH2JNIP/HURa5s8+XfMLi /2odHES5hOuxjumroWXNggk1c73CeKZoudvDVSpq8gMKLCIw+dssXd15H1SmJE18zi3+ T23PIjtGEO3/kE29FOlfHCIK7kbUDjbdQYeunQUJzScv91qjEx0j3Vf0Q4/M0p07v52g 8OfXAd114Z6htfiNE7jaUAPmC3SPugJy7UR3A8ia3vpnfKD5VSI4sDF0ZolY5FRk+O7C kw8A== X-Gm-Message-State: AOJu0Yz6rvqgMq5VVdsJFj7hu0ZkZLDowBTxMdfzkf0lKVDFINV21vlc uhyHDvZZfqf6azvjjeM7705iIVh4Xo/z3C4eWYtYotk2+YBy3gm/+FHZpAwYizQ4u/Ksh+9UTyr 6mVE= X-Gm-Gg: ASbGncuw8eIi/sho13esNqlV0mpKJcLBApg35tHcCRhgiel5IttxOu1mWU89roQ93xh A510+0eTScjzEH1E9IKBdmmFHrrLeprOXQSgOYvSOgPKyQU5ru0Hfu9FH20LIOsHLdWUZuJuEoA Cfuf5xPkw7Hv/16LLmGjqR3lQ8pERSLPSt4WzjMmhLNqEPa6zuvM7n5JYeUrv1x3GcWHEaPV33m qZESYuQXduzZCBqdPx2JHZNv/NG/BNLTSu7fLodvPEJOXj2FQK/7Wv94HDyimxl/5PdePzlrilg aVUgubhqFn0yQn/Q4t2FFigoKGz+H0djb0EQvPnkvUfTuPMT+wzzJVPo4sLtov9SvdUMCL2I6FU 6NAol6w4Fegt9a2phKFA8sg== X-Google-Smtp-Source: AGHT+IExyXHbOf2taNxw2yz+3CcX0NYoBCdJtwB6sr0Vv5nuFXnktNWKFMtHaLzI32Tb9SdvBjUavw== X-Received: by 2002:a05:6402:380c:b0:5f0:8551:9790 with SMTP id 4fb4d7f45d1cf-5f0b3bcd2c0mr2481680a12.16.1743777165747; Fri, 04 Apr 2025 07:32:45 -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 4fb4d7f45d1cf-5f087f0a0f3sm2567450a12.43.2025.04.04.07.32.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Apr 2025 07:32:45 -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 , Jens Wiklander Subject: [PATCH v7 11/11] optee: smc abi: dynamic protected memory allocation Date: Fri, 4 Apr 2025 16:31:34 +0200 Message-ID: <20250404143215.2281034-12-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250404143215.2281034-1-jens.wiklander@linaro.org> References: <20250404143215.2281034-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 | 102 ++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 17 deletions(-) diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index c10b38b23586..dd9887826590 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) @@ -1620,13 +1686,16 @@ static inline int optee_load_fw(struct platform_dev= ice *pdev, } #endif =20 -static int optee_rstmem_pool_init(struct optee *optee) +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_rstmem_pool_init(struct optee *opt= ee) =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 (rc && (protm || dyn_protm)) + pr_info("Protected memory service not available\n"); + return 0; -err: - pool->ops->destroy_pool(pool); - return rc; } =20 static int optee_probe(struct platform_device *pdev) @@ -1823,7 +1891,7 @@ static int optee_probe(struct platform_device *pdev) pr_info("Asynchronous notifications enabled\n"); } =20 - rc =3D optee_rstmem_pool_init(optee); + rc =3D optee_protmem_pool_init(optee); if (rc) goto err_notif_uninit; =20 --=20 2.43.0