Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp2524777rdb; Tue, 12 Sep 2023 04:56:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGG3EGmpvtjrg4FdwY9WGlS3vd+SnjMJLNQtPJ9fOItY/yA2xOWWQmSF5qkEYmR+w1eWj2Z X-Received: by 2002:a05:6870:4511:b0:1d5:aa83:c225 with SMTP id e17-20020a056870451100b001d5aa83c225mr7271919oao.21.1694519802322; Tue, 12 Sep 2023 04:56:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694519802; cv=none; d=google.com; s=arc-20160816; b=D4wKX5DPwgaOfEJVj2HotwjTS56gY6MJOZHBZNOxsaPZvVjNmW9e1srzCLt51+3h3x gfxTFUnMyH2R84/BHOqZre5X5wWnMawsU3udXaqRSW2HXZJNLyEbnMTmWdJVDmXDGDQU C7D+x8C2IvL4HXhbvSFrEEGkYhAnbhFeC8XSH4anoxguINV71rIOKZmPG1kTRlOF86Mn AL8kgh6fHu3m94qSoTQUuEM4mT/MOaHQ1D+RRpxjetxDVIo441s0RTD0SRdPSRko5N1O 0LrDxZxzL1KCLuiL1zK46kqTo6xsKAcAMwt+A1sfu8sxvDaj3Allfhp275zN0cXPTTm1 7pxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=WrTiKWSurW3c0S9a2fG3MnSs7O2949Ur1yIn1VGqCkw=; fh=CED6S+BFgm58E7124njkm5tesMW4ptnuEqifv15p4Uk=; b=Yf6tsbviV5c4ytoepjw2rg7aQSxP/ZOV6UaD3dUCBOCKU1q1rbnAXm60fxk7CUc7+i Bqo0UPmHiDNIQesfl+ERjjOTtqQPE5su0uB6vPkCGpLVwHTysgHX4cgVnUa/sN767Cuv jUShWq1qmcioe+UqA8UpxvMQfHDk9Fe9K1k3JOMWtfERK88PyzpaPvR8ijfjPFE9ibAw LO3Hgj2NYPHMJBvbYu6G7kgBAXZkuwOT0ihwqTbMBp133L2UYxHfiuVU56CemS+r67ZL 87xApfNo76ajgaHpRZ+zVMihKj4y6ZUpo/75hryDt6Im5Xf4PzdrZw0wcbJ+4eu8235K +T8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=leOPTMb+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id z16-20020a656650000000b00565e45d8a20si7604796pgv.708.2023.09.12.04.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 04:56:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=leOPTMb+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 1AA998116B16; Tue, 12 Sep 2023 02:32:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232360AbjILJcd (ORCPT + 99 others); Tue, 12 Sep 2023 05:32:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231574AbjILJcb (ORCPT ); Tue, 12 Sep 2023 05:32:31 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBE6CCD; Tue, 12 Sep 2023 02:32:27 -0700 (PDT) Received: from [192.168.1.100] (2-237-20-237.ip236.fastwebnet.it [2.237.20.237]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: kholk11) by madras.collabora.co.uk (Postfix) with ESMTPSA id B76C566072F2; Tue, 12 Sep 2023 10:32:25 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1694511146; bh=nboqvOkj/fOk7wG5yLQU5h6H3bj8HNMHs3m8cZuPp8g=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=leOPTMb+UgyVQ7uNRBIbc4bummxwqu0liVI46DPr3hA0ZMaSkS1CjLtz/mqVft49Z xFjyoG767NLmgk7EBBdlhCvsO3Te6WxU7aeBJuYEPJfvmXBPzhVmf0BzY9sGPYGJxL 5RJTGwtBTYBNd98Ovs9eDiZRp5+fdCSF3shUiUH3eRJC+DAQDfbV0qnzMeWvlAlD54 1M/ycX4PoO5ZP+QtowvQgTv5u2NktAquPZCBeJDafxev3n8eNkbQuSci0wkphL+en4 lo9PKMFdN0ZAuIQs+tFZnn3yahQ/wGOOoIblTuDuIQTkM4pWBpJBtG8Bi4FwWsRYRB NnAZEo8JCQtcQ== Message-ID: Date: Tue, 12 Sep 2023 11:32:23 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH 5/9] dma-buf: heaps: mtk_sec_heap: Initialise tee session Content-Language: en-US To: =?UTF-8?B?WW9uZyBXdSAo5ZC05YuHKQ==?= , "matthias.bgg@gmail.com" , "christian.koenig@amd.com" , "robh+dt@kernel.org" , "sumit.semwal@linaro.org" Cc: "linux-kernel@vger.kernel.org" , "linux-mediatek@lists.infradead.org" , "jstultz@google.com" , "linaro-mm-sig@lists.linaro.org" , "linux-media@vger.kernel.org" , "devicetree@vger.kernel.org" , =?UTF-8?B?SmlhbmppYW8gWmVuZyAo5pu+5YGl5aejKQ==?= , =?UTF-8?B?S3VvaG9uZyBXYW5nICjnjovlnIvptLsp?= , "conor+dt@kernel.org" , "Brian.Starkey@arm.com" , "benjamin.gaignard@collabora.com" , "tjmercier@google.com" , "krzysztof.kozlowski+dt@linaro.org" , "dri-devel@lists.freedesktop.org" , "linux-arm-kernel@lists.infradead.org" References: <20230911023038.30649-1-yong.wu@mediatek.com> <20230911023038.30649-6-yong.wu@mediatek.com> From: AngeloGioacchino Del Regno In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 12 Sep 2023 02:32:43 -0700 (PDT) X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Il 12/09/23 08:17, Yong Wu (吴勇) ha scritto: > On Mon, 2023-09-11 at 11:29 +0200, AngeloGioacchino Del Regno wrote: >> Il 11/09/23 04:30, Yong Wu ha scritto: >>> The TEE probe later than dma-buf heap, and PROBE_DEDER doesn't work >>> here since this is not a platform driver, therefore initialise the >>> TEE >>> context/session while we allocate the first secure buffer. >>> >>> Signed-off-by: Yong Wu >>> --- >>> drivers/dma-buf/heaps/mtk_secure_heap.c | 61 >>> +++++++++++++++++++++++++ >>> 1 file changed, 61 insertions(+) >>> >>> diff --git a/drivers/dma-buf/heaps/mtk_secure_heap.c b/drivers/dma- >>> buf/heaps/mtk_secure_heap.c >>> index bbf1c8dce23e..e3da33a3d083 100644 >>> --- a/drivers/dma-buf/heaps/mtk_secure_heap.c >>> +++ b/drivers/dma-buf/heaps/mtk_secure_heap.c >>> @@ -10,6 +10,12 @@ >>> #include >>> #include >>> #include >>> +#include >>> +#include >>> + >>> +#define TZ_TA_MEM_UUID "4477588a-8476-11e2-ad15- >>> e41f1390d676" >>> + >> >> Is this UUID the same for all SoCs and all TZ versions? > > Yes. It is the same for all SoCs and all TZ versions currently. > That's good news! Is this UUID used in any userspace component? (example: Android HALs?) If it is (and I somehow expect that it is), then this definition should go to a UAPI header, as suggested by Christian. Cheers! >> >> Thanks, >> Angelo >> >> >>> +#define MTK_TEE_PARAM_NUM 4 >>> >>> /* >>> * MediaTek secure (chunk) memory type >>> @@ -28,17 +34,72 @@ struct mtk_secure_heap_buffer { >>> struct mtk_secure_heap { >>> const char *name; >>> const enum kree_mem_type mem_type; >>> + u32 mem_session; >>> + struct tee_context *tee_ctx; >>> }; >>> >>> +static int mtk_optee_ctx_match(struct tee_ioctl_version_data *ver, >>> const void *data) >>> +{ >>> + return ver->impl_id == TEE_IMPL_ID_OPTEE; >>> +} >>> + >>> +static int mtk_kree_secure_session_init(struct mtk_secure_heap >>> *sec_heap) >>> +{ >>> + struct tee_param t_param[MTK_TEE_PARAM_NUM] = {0}; >>> + struct tee_ioctl_open_session_arg arg = {0}; >>> + uuid_t ta_mem_uuid; >>> + int ret; >>> + >>> + sec_heap->tee_ctx = tee_client_open_context(NULL, >>> mtk_optee_ctx_match, >>> + NULL, NULL); >>> + if (IS_ERR(sec_heap->tee_ctx)) { >>> + pr_err("%s: open context failed, ret=%ld\n", sec_heap- >>>> name, >>> + PTR_ERR(sec_heap->tee_ctx)); >>> + return -ENODEV; >>> + } >>> + >>> + arg.num_params = MTK_TEE_PARAM_NUM; >>> + arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC; >>> + ret = uuid_parse(TZ_TA_MEM_UUID, &ta_mem_uuid); >>> + if (ret) >>> + goto close_context; >>> + memcpy(&arg.uuid, &ta_mem_uuid.b, sizeof(ta_mem_uuid)); >>> + >>> + ret = tee_client_open_session(sec_heap->tee_ctx, &arg, >>> t_param); >>> + if (ret < 0 || arg.ret) { >>> + pr_err("%s: open session failed, ret=%d:%d\n", >>> + sec_heap->name, ret, arg.ret); >>> + ret = -EINVAL; >>> + goto close_context; >>> + } >>> + sec_heap->mem_session = arg.session; >>> + return 0; >>> + >>> +close_context: >>> + tee_client_close_context(sec_heap->tee_ctx); >>> + return ret; >>> +} >>> + >>> static struct dma_buf * >>> mtk_sec_heap_allocate(struct dma_heap *heap, size_t size, >>> unsigned long fd_flags, unsigned long heap_flags) >>> { >>> + struct mtk_secure_heap *sec_heap = dma_heap_get_drvdata(heap); >>> struct mtk_secure_heap_buffer *sec_buf; >>> DEFINE_DMA_BUF_EXPORT_INFO(exp_info); >>> struct dma_buf *dmabuf; >>> int ret; >>> >>> + /* >>> + * TEE probe may be late. Initialise the secure session in the >>> first >>> + * allocating secure buffer. >>> + */ >>> + if (!sec_heap->mem_session) { >>> + ret = mtk_kree_secure_session_init(sec_heap); >>> + if (ret) >>> + return ERR_PTR(ret); >>> + } >>> + >>> sec_buf = kzalloc(sizeof(*sec_buf), GFP_KERNEL); >>> if (!sec_buf) >>> return ERR_PTR(-ENOMEM); >> >>