From patchwork Wed May 19 14:27:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Etienne Carriere X-Patchwork-Id: 1480960 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=QAAHNm5r; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FlZx72SW4z9sVt for ; Thu, 20 May 2021 00:28:43 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BD4A381895; Wed, 19 May 2021 16:28:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="QAAHNm5r"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5D49E82A8B; Wed, 19 May 2021 16:28:37 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3DB96816D2 for ; Wed, 19 May 2021 16:28:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=etienne.carriere@linaro.org Received: by mail-wm1-x32d.google.com with SMTP id z137-20020a1c7e8f0000b02901774f2a7dc4so3437299wmc.0 for ; Wed, 19 May 2021 07:28:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=Sle1dw+pH3URHScGWs02Zo3vMqp8avhLOvlOwDe0Fj4=; b=QAAHNm5r7eo7Z4+kXcmXIJ3PMHmXZlNVeQZzVGSbkl5b5mZYV8X1sqpIEwHESEnDAy zsxE3bEzvfKhJz9H7XBfA42hMvYjAPR7b0MT4UPQXZpWBYtYXr4GaD/ValvuvB3yneZD RWKcK/uxh5MZhlOgv6fVSgl8ahs9s/87692HDp1hGdYT715YmEPk8iDTEq24TTMP+gJc 5ihS9kCmIa51kdMYlmi1Tsu3G2EmdcGbgLFlqxzP34ROnxm7qEdaMX4hHsSrC0KGVD4J xMIWYcwnz3/5v1P26PgvU2Uq/Yr1nV8daD/LO9qEjocZ0ASx5hD8579uZ69eQ+iV7KKM HyCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Sle1dw+pH3URHScGWs02Zo3vMqp8avhLOvlOwDe0Fj4=; b=FmAJwe8iEETzIGNiWVEi/ll517P14TpR8wlhGhpdVEvkCwbi2iYcSs7TfnkJkfTLj4 vOScrNPocqgDbscYnEX17L60WmNZ+q/taJOHV/zWL5hxMf4iByTf2CEXzbOXQjgDMCxK bB8O2WrZnamTJrKELM5WMWvPx5A/gjW8MhahySmgSW4/ve+xUdjVNaj0XTsccfHCvj/n tAFYmgeSyFq/YS7737XR24zIpOSKxMZYDDWlPLci4+DA3yb5MjRhmJzesgDhCGKms1Rw fUJqygq6NRANTzHR6xI/mONe80JI40hB2W48OrVOamUD+dN2Ir90COih8/CoLnT9o56h uzjA== X-Gm-Message-State: AOAM530P0caCVI5H8DMcoKlmrNua+lbWJf+h35QdDZ3F5U/Hakgsjxy3 vMNNbpNf57o4HOnKI14x1//4vCMWge0qADEu X-Google-Smtp-Source: ABdhPJwvyF1VVkz+4iCWRo/FTIvNS93xUcb8zOAB+Mo9R4YT9H2nyMz0wwtMRBFtpDn7Ew5ICjAWCg== X-Received: by 2002:a1c:1d8e:: with SMTP id d136mr11701385wmd.80.1621434513616; Wed, 19 May 2021 07:28:33 -0700 (PDT) Received: from lmecxl0524.lme.st.com (2a01cb058b850800452ff0effb46d5f4.ipv6.abo.wanadoo.fr. [2a01:cb05:8b85:800:452f:f0ef:fb46:d5f4]) by smtp.gmail.com with ESMTPSA id b8sm26606375wrx.15.2021.05.19.07.28.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 May 2021 07:28:33 -0700 (PDT) From: Etienne Carriere To: u-boot@lists.denx.de Cc: Jens Wiklander , Simon Glass , Patrick Delaunay , Ilias Apalodimas , Etienne Carriere Subject: [PATCH v2] tee: optee: sync cache on pre-reloc OP-TEE invocation Date: Wed, 19 May 2021 16:27:41 +0200 Message-Id: <20210519142741.7782-1-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean This change ensures both U-Boot and OP-TEE see the same content from shared memory when OP-TEE is invoked prior U-Boot relocation. This change is required since U-Boot may execute with data cahce off while OP-TEE always enables cache on memory shared with U-Boot. Signed-off-by: Etienne Carriere Reviewed-by: Jens Wiklander Acked-by: Ilias Apalodimas --- Changes since v1: - Fix invocation argument buffer size in optee/core.c, using OPTEE_MSG_GET_ARG_SIZE(). - add missing #include in tee-uclass.c --- drivers/tee/optee/core.c | 21 ++++++++++++++++++++- drivers/tee/tee-uclass.c | 19 ++++++++++++++++++- include/tee.h | 6 ++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index 73dbb22ba0..dad46aa388 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -1,9 +1,10 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Copyright (c) 2018 Linaro Limited + * Copyright (c) 2018-2020 Linaro Limited */ #include +#include #include #include #include @@ -295,6 +296,16 @@ static u32 call_err_to_res(u32 call_err) } } +static void flush_shm_dcache(struct udevice *dev, struct optee_msg_arg *arg) +{ + size_t sz = OPTEE_MSG_GET_ARG_SIZE(arg->num_params); + + flush_dcache_range(rounddown((ulong)arg, CONFIG_SYS_CACHELINE_SIZE), + roundup((ulong)arg + sz, CONFIG_SYS_CACHELINE_SIZE)); + + tee_flush_all_shm_dcache(dev); +} + static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg) { struct optee_pdata *pdata = dev_get_plat(dev); @@ -305,9 +316,17 @@ static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg) while (true) { struct arm_smccc_res res; + /* If cache are off from U-Boot, sync the cache shared with OP-TEE */ + if (!dcache_status()) + flush_shm_dcache(dev, arg); + pdata->invoke_fn(param.a0, param.a1, param.a2, param.a3, param.a4, param.a5, param.a6, param.a7, &res); + /* If cache are off from U-Boot, sync the cache shared with OP-TEE */ + if (!dcache_status()) + flush_shm_dcache(dev, arg); + free(page_list); page_list = NULL; diff --git a/drivers/tee/tee-uclass.c b/drivers/tee/tee-uclass.c index 2cc6b6c407..24321ba1d2 100644 --- a/drivers/tee/tee-uclass.c +++ b/drivers/tee/tee-uclass.c @@ -1,13 +1,15 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * Copyright (c) 2018 Linaro Limited + * Copyright (c) 2018-2020 Linaro Limited */ #include +#include #include #include #include #include +#include #include #include @@ -233,3 +235,18 @@ void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN], d[7] = s->time_hi_and_version; memcpy(d + 8, s->clock_seq_and_node, sizeof(s->clock_seq_and_node)); } + +void tee_flush_all_shm_dcache(struct udevice *dev) +{ + struct tee_uclass_priv *priv = dev_get_uclass_priv(dev); + struct tee_shm *s; + + list_for_each_entry(s, &priv->list_shm, link) { + ulong start = rounddown((ulong)s->addr, + CONFIG_SYS_CACHELINE_SIZE); + ulong end = roundup((ulong)s->addr + s->size, + CONFIG_SYS_CACHELINE_SIZE); + + flush_dcache_range(start, end); + } +} diff --git a/include/tee.h b/include/tee.h index 99367b258e..2ef29bfc8f 100644 --- a/include/tee.h +++ b/include/tee.h @@ -377,4 +377,10 @@ void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d, void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN], const struct tee_optee_ta_uuid *s); +/** + * tee_flush_all_shm_dcache() - Flush data cache for all shared memories + * @dev: The TEE device + */ +void tee_flush_all_shm_dcache(struct udevice *dev); + #endif /* __TEE_H */