Message ID | 20230911125936.10648-13-yunfei.dong@mediatek.com (mailing list archive) |
---|---|
State | Changes Requested |
Headers |
Received: from vger.kernel.org ([23.128.96.18]) by www.linuxtv.org with esmtp (Exim 4.92) (envelope-from <linux-media-owner@vger.kernel.org>) id 1qfnqr-002Kua-Uk; Mon, 11 Sep 2023 20:49:46 +0000 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236447AbjIKUtp (ORCPT <rfc822;mkrufky@linuxtv.org> + 1 other); Mon, 11 Sep 2023 16:49:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237588AbjIKNAB (ORCPT <rfc822;linux-media@vger.kernel.org>); Mon, 11 Sep 2023 09:00:01 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4831BE50; Mon, 11 Sep 2023 05:59:56 -0700 (PDT) X-UUID: 1874652a50a311eea33bb35ae8d461a2-20230911 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=tKlHvN5/G1frDG7GgPVmMjQ8CSmun7vd2dASfS/alS8=; b=dk8fw5oO/YP3nRzbXwRHpyZ3/YKIqkP34FMaqSifFZVMoM2UYsEQWijB99WPXFPOxpWSwDmbYXRKjzEiiJcyP+SqatFks4xukSpwCyq1d6Z2sIM3mhcuZfyMo1UAcETMOgYVz8rQ8jqLaKvsyZo5x0AYOo3N4YDP0nyartiyolQ=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31,REQID:da9cb164-9104-4aa8-8c15-a737d2620425,IP:0,U RL:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-5 X-CID-META: VersionHash:0ad78a4,CLOUDID:dc17b4be-14cc-44ca-b657-2d2783296e72,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 1874652a50a311eea33bb35ae8d461a2-20230911 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw01.mediatek.com (envelope-from <yunfei.dong@mediatek.com>) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1580286477; Mon, 11 Sep 2023 20:59:51 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 11 Sep 2023 20:59:50 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 11 Sep 2023 20:59:49 +0800 From: Yunfei Dong <yunfei.dong@mediatek.com> To: =?utf-8?q?N=C3=ADcolas_F_=2E_R_=2E_A_=2E_Prado?= <nfraprado@collabora.com>, Nicolas Dufresne <nicolas.dufresne@collabora.com>, Hans Verkuil <hverkuil-cisco@xs4all.nl>, AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>, Benjamin Gaignard <benjamin.gaignard@collabora.com>, Nathan Hebert <nhebert@chromium.org> CC: Chen-Yu Tsai <wenst@chromium.org>, Hsin-Yi Wang <hsinyi@chromium.org>, Fritz Koenig <frkoenig@chromium.org>, Daniel Vetter <daniel@ffwll.ch>, Steve Cho <stevecho@chromium.org>, Yunfei Dong <yunfei.dong@mediatek.com>, <linux-media@vger.kernel.org>, <devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>, <linux-arm-kernel@lists.infradead.org>, <linux-mediatek@lists.infradead.org>, <Project_Global_Chrome_Upstream_Group@mediatek.com> Subject: [PATCH 12/14] media: medkatek: vcodec: set secure mode to decoder driver Date: Mon, 11 Sep 2023 20:59:34 +0800 Message-ID: <20230911125936.10648-13-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230911125936.10648-1-yunfei.dong@mediatek.com> References: <20230911125936.10648-1-yunfei.dong@mediatek.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-MTK: N X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-media.vger.kernel.org> X-Mailing-List: linux-media@vger.kernel.org X-LSpam-Score: -2.5 (--) X-LSpam-Report: No, score=-2.5 required=5.0 tests=BAYES_00=-1.9,DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,HEADER_FROM_DIFFERENT_DOMAINS=0.5,MAILING_LIST_MULTI=-1,UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no |
Series |
add driver to support secure video decoder
|
|
Commit Message
Yunfei Dong
Sept. 11, 2023, 12:59 p.m. UTC
Setting secure mode flag to kernel when trying to play secure video,
then decoder driver will initialize tee related interface to support
svp.
Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com>
---
.../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 ++++++++++++++-
drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++
include/uapi/linux/v4l2-controls.h | 1 +
3 files changed, 20 insertions(+), 1 deletion(-)
Comments
Hi Nicolas, Thanks for your advice. On Mon, 2023-09-11 at 11:54 -0400, Nicolas Dufresne wrote: > Hi, > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > > Setting secure mode flag to kernel when trying to play secure > > video, > > then decoder driver will initialize tee related interface to > > support > > svp. > > > This is not what the patch is doing, please rework. This patch is an > vendor API > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should not > have to > read your patch to understand this. > I don't know your meaning clearly. Whether I need to add one new patch to add the definition of V4L2_CID_MPEG_MTK_SET_SECURE_MODE? Than the driver calling it to set secure mode? Best Regards, Yunfei Dong > > > > Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > > --- > > .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > > ++++++++++++++- > > drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > > include/uapi/linux/v4l2-controls.h | 1 + > > 3 files changed, 20 insertions(+), 1 deletion(-) > > > > diff --git > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > teless.c > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > teless.c > > index d2b09ce9f1cf..a981178c25d9 100644 > > --- > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > teless.c > > +++ > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > teless.c > > @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > > *ctrl) > > ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl- > > >val); > > mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => > > 0x%x", sec_fd, ctrl->val); > > break; > > + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > Stepping back a little and focusing on the API, what makes your > driver so > special that it should be the only one having a "secure mode" ? We > are touching > in gap in the media pipeline in Linux, and this should come with > consideration > of the global API. > > Why is this API better then let's say Google Android one, were they > expose 2 > device nodes in their fork of the MFC driver (a secure and a non > secure one) ? > > regards, > Nicolas > > p.s. you forgot to document your control in the RST doc, please do in > following > release. > > > + ctx->is_svp_mode = ctrl->val; > > + > > + if (ctx->is_svp_mode) { > > + ret = mtk_vcodec_dec_optee_open(ctx->dev- > > >optee_private); > > + if (ret) > > + mtk_v4l2_vdec_err(ctx, "open secure > > mode failed."); > > + else > > + mtk_v4l2_vdec_dbg(3, ctx, "decoder in > > secure mode: %d", ctrl->val); > > + } > > + break; > > default: > > mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl > > id: 0x%x\n", hdr_ctrl->id); > > return ret; > > @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > mtk_vcodec_dec_ctx *ctx) > > unsigned int i; > > struct v4l2_ctrl *ctrl; > > > > - v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > > + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > > if (ctx->ctrl_hdl.error) { > > mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init > > failed\n"); > > return ctx->ctrl_hdl.error; > > @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > mtk_vcodec_dec_ctx *ctx) > > > > ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > &mtk_vcodec_dec_ctrl_ops, > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, > > 0, 65535, 1, 0); > > + ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > &mtk_vcodec_dec_ctrl_ops, > > + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, > > 65535, 1, 0); > > > > v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > > > > diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > index d8cf01f76aab..a507045a3f30 100644 > > --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > > case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES: return > > "Reference Frames for a P-Frame"; > > case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR: ret > > urn "Prepend SPS and PPS to IDR"; > > case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE: return > > "MediaTek Decoder get secure handle"; > > + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: ret > > urn "MediaTek Decoder set secure mode"; > > > > /* AV1 controls */ > > case V4L2_CID_MPEG_VIDEO_AV1_PROFILE: ret > > urn "AV1 Profile"; > > @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > > **name, enum v4l2_ctrl_type *type, > > *type = V4L2_CTRL_TYPE_INTEGER; > > *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > break; > > + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > + *type = V4L2_CTRL_TYPE_INTEGER; > > + *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > + break; > > case V4L2_CID_USER_CLASS: > > case V4L2_CID_CAMERA_CLASS: > > case V4L2_CID_CODEC_CLASS: > > diff --git a/include/uapi/linux/v4l2-controls.h > > b/include/uapi/linux/v4l2-controls.h > > index 7b3694985366..88e90d943e38 100644 > > --- a/include/uapi/linux/v4l2-controls.h > > +++ b/include/uapi/linux/v4l2-controls.h > > @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > > /* MPEG-class control IDs specific to the MediaTek Decoder driver > > as defined by V4L2 */ > > #define V4L2_CID_MPEG_MTK_BASE (V4L2_CTRL_CLAS > > S_CODEC | 0x2000) > > #define V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE (V4L2_CID_MPEG_ > > MTK_BASE+8) > > +#define V4L2_CID_MPEG_MTK_SET_SECURE_MODE (V4L2_CID_MPEG_MTK_BASE > > +9) > > > > /* Camera class control IDs */ > > > >
Hi, On 9/11/23 17:54, Nicolas Dufresne wrote: > Hi, > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : >> Setting secure mode flag to kernel when trying to play secure video, >> then decoder driver will initialize tee related interface to support >> svp. > > > This is not what the patch is doing, please rework. This patch is an vendor API > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should not have to > read your patch to understand this. > >> >> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> >> --- >> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 ++++++++++++++- >> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ >> include/uapi/linux/v4l2-controls.h | 1 + >> 3 files changed, 20 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c >> index d2b09ce9f1cf..a981178c25d9 100644 >> --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c >> +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c >> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl *ctrl) >> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); >> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", sec_fd, ctrl->val); >> break; >> + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > Stepping back a little and focusing on the API, what makes your driver so > special that it should be the only one having a "secure mode" ? We are touching > in gap in the media pipeline in Linux, and this should come with consideration > of the global API. > > Why is this API better then let's say Google Android one, were they expose 2 > device nodes in their fork of the MFC driver (a secure and a non secure one) ? Perhaps it is a good idea to first post an RFC with an uAPI proposal on how to handle secure video. I suspect this isn't mediatek specific, other SoCs with tee support could use this as well. As Nicolas said, it's long known to be a gap in our media support, so it is really great that you started work on this, but you need to look at this from a more generic point-of-view, and not mediatek-specific. Regards, Hans > > regards, > Nicolas > > p.s. you forgot to document your control in the RST doc, please do in following > release. > >> + ctx->is_svp_mode = ctrl->val; >> + >> + if (ctx->is_svp_mode) { >> + ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); >> + if (ret) >> + mtk_v4l2_vdec_err(ctx, "open secure mode failed."); >> + else >> + mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl->val); >> + } >> + break; >> default: >> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: 0x%x\n", hdr_ctrl->id); >> return ret; >> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_dec_ctx *ctx) >> unsigned int i; >> struct v4l2_ctrl *ctrl; >> >> - v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); >> + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); >> if (ctx->ctrl_hdl.error) { >> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); >> return ctx->ctrl_hdl.error; >> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_dec_ctx *ctx) >> >> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops, >> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); >> + ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops, >> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); >> >> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); >> >> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >> index d8cf01f76aab..a507045a3f30 100644 >> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) >> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES: return "Reference Frames for a P-Frame"; >> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR: return "Prepend SPS and PPS to IDR"; >> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE: return "MediaTek Decoder get secure handle"; >> + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: return "MediaTek Decoder set secure mode"; >> >> /* AV1 controls */ >> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE: return "AV1 Profile"; >> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, >> *type = V4L2_CTRL_TYPE_INTEGER; >> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >> break; >> + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >> + *type = V4L2_CTRL_TYPE_INTEGER; >> + *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >> + break; >> case V4L2_CID_USER_CLASS: >> case V4L2_CID_CAMERA_CLASS: >> case V4L2_CID_CODEC_CLASS: >> diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h >> index 7b3694985366..88e90d943e38 100644 >> --- a/include/uapi/linux/v4l2-controls.h >> +++ b/include/uapi/linux/v4l2-controls.h >> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { >> /* MPEG-class control IDs specific to the MediaTek Decoder driver as defined by V4L2 */ >> #define V4L2_CID_MPEG_MTK_BASE (V4L2_CTRL_CLASS_CODEC | 0x2000) >> #define V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE (V4L2_CID_MPEG_MTK_BASE+8) >> +#define V4L2_CID_MPEG_MTK_SET_SECURE_MODE (V4L2_CID_MPEG_MTK_BASE+9) >> >> /* Camera class control IDs */ >> >
Le mardi 12 septembre 2023 à 01:48 +0000, Yunfei Dong (董云飞) a écrit : > Hi Nicolas, > > Thanks for your advice. > > On Mon, 2023-09-11 at 11:54 -0400, Nicolas Dufresne wrote: > > Hi, > > > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > > > Setting secure mode flag to kernel when trying to play secure > > > video, > > > then decoder driver will initialize tee related interface to > > > support > > > svp. > > > > > > This is not what the patch is doing, please rework. This patch is an > > vendor API > > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should not > > have to > > read your patch to understand this. > > > I don't know your meaning clearly. Whether I need to add one new patch > to add the definition of V4L2_CID_MPEG_MTK_SET_SECURE_MODE? Than the > driver calling it to set secure mode? I'm commenting next to your commit message. The commit message is saying something that does not represent what the patch is actually doing, please rewrite your commit message according to good practices. > > Best Regards, > Yunfei Dong > > > > > > > Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > > > --- > > > .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > > > ++++++++++++++- > > > drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > > > include/uapi/linux/v4l2-controls.h | 1 + > > > 3 files changed, 20 insertions(+), 1 deletion(-) > > > > > > diff --git > > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > > teless.c > > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > > teless.c > > > index d2b09ce9f1cf..a981178c25d9 100644 > > > --- > > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > > teless.c > > > +++ > > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_sta > > > teless.c > > > @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > > > *ctrl) > > > ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl- > > > > val); > > > mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => > > > 0x%x", sec_fd, ctrl->val); > > > break; > > > + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > Stepping back a little and focusing on the API, what makes your > > driver so > > special that it should be the only one having a "secure mode" ? We > > are touching > > in gap in the media pipeline in Linux, and this should come with > > consideration > > of the global API. > > > > Why is this API better then let's say Google Android one, were they > > expose 2 > > device nodes in their fork of the MFC driver (a secure and a non > > secure one) ? > > > > regards, > > Nicolas > > > > p.s. you forgot to document your control in the RST doc, please do in > > following > > release. > > > > > + ctx->is_svp_mode = ctrl->val; > > > + > > > + if (ctx->is_svp_mode) { > > > + ret = mtk_vcodec_dec_optee_open(ctx->dev- > > > > optee_private); > > > + if (ret) > > > + mtk_v4l2_vdec_err(ctx, "open secure > > > mode failed."); > > > + else > > > + mtk_v4l2_vdec_dbg(3, ctx, "decoder in > > > secure mode: %d", ctrl->val); > > > + } > > > + break; > > > default: > > > mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl > > > id: 0x%x\n", hdr_ctrl->id); > > > return ret; > > > @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > > mtk_vcodec_dec_ctx *ctx) > > > unsigned int i; > > > struct v4l2_ctrl *ctrl; > > > > > > - v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > > > + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > > > if (ctx->ctrl_hdl.error) { > > > mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init > > > failed\n"); > > > return ctx->ctrl_hdl.error; > > > @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > > mtk_vcodec_dec_ctx *ctx) > > > > > > ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > > &mtk_vcodec_dec_ctrl_ops, > > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, > > > 0, 65535, 1, 0); > > > + ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > > &mtk_vcodec_dec_ctrl_ops, > > > + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, > > > 65535, 1, 0); > > > > > > v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > > > > > > diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > index d8cf01f76aab..a507045a3f30 100644 > > > --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > > > case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES: return > > > "Reference Frames for a P-Frame"; > > > case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR: ret > > > urn "Prepend SPS and PPS to IDR"; > > > case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE: return > > > "MediaTek Decoder get secure handle"; > > > + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: ret > > > urn "MediaTek Decoder set secure mode"; > > > > > > /* AV1 controls */ > > > case V4L2_CID_MPEG_VIDEO_AV1_PROFILE: ret > > > urn "AV1 Profile"; > > > @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > > > **name, enum v4l2_ctrl_type *type, > > > *type = V4L2_CTRL_TYPE_INTEGER; > > > *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > break; > > > + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > + *type = V4L2_CTRL_TYPE_INTEGER; > > > + *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > + break; > > > case V4L2_CID_USER_CLASS: > > > case V4L2_CID_CAMERA_CLASS: > > > case V4L2_CID_CODEC_CLASS: > > > diff --git a/include/uapi/linux/v4l2-controls.h > > > b/include/uapi/linux/v4l2-controls.h > > > index 7b3694985366..88e90d943e38 100644 > > > --- a/include/uapi/linux/v4l2-controls.h > > > +++ b/include/uapi/linux/v4l2-controls.h > > > @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > > > /* MPEG-class control IDs specific to the MediaTek Decoder driver > > > as defined by V4L2 */ > > > #define V4L2_CID_MPEG_MTK_BASE (V4L2_CTRL_CLAS > > > S_CODEC | 0x2000) > > > #define V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE (V4L2_CID_MPEG_ > > > MTK_BASE+8) > > > +#define V4L2_CID_MPEG_MTK_SET_SECURE_MODE (V4L2_CID_MPEG_MTK_BASE > > > +9) > > > > > > /* Camera class control IDs */ > > > > > > >
Hi Hans & Nicolas, Thanks for your advice. On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > > External email : Please do not click links or open attachments until > you have verified the sender or the content. > Hi, > > On 9/11/23 17:54, Nicolas Dufresne wrote: > > Hi, > > > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > > > Setting secure mode flag to kernel when trying to play secure > > video, > > > then decoder driver will initialize tee related interface to > > support > > > svp. > > > > > > This is not what the patch is doing, please rework. This patch is > > an vendor API > > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > > not have to > > read your patch to understand this. > > > > > > > > Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > > > --- > > > .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > > ++++++++++++++- > > > drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > > > include/uapi/linux/v4l2-controls.h | 1 + > > > 3 files changed, 20 insertions(+), 1 deletion(-) > > > > > > diff --git > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > > > index d2b09ce9f1cf..a981178c25d9 100644 > > > --- > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > > > +++ > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > > > @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > > *ctrl) > > > ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > > > mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > > sec_fd, ctrl->val); > > > break; > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > Stepping back a little and focusing on the API, what makes your > > driver so > > special that it should be the only one having a "secure mode" ? We > > are touching > > in gap in the media pipeline in Linux, and this should come with > > consideration > > of the global API. > > > > Why is this API better then let's say Google Android one, were they > > expose 2 > > device nodes in their fork of the MFC driver (a secure and a non > > secure one) ? > > Perhaps it is a good idea to first post an RFC with an uAPI proposal > on how to > handle secure video. I suspect this isn't mediatek specific, other > SoCs with > tee support could use this as well. > > As Nicolas said, it's long known to be a gap in our media support, so > it is > really great that you started work on this, but you need to look at > this from > a more generic point-of-view, and not mediatek-specific. > Whether your have any advice about how to do a more generic driver to handle secure video playback? There are several kind of buffer: output queue buffer/capture queue buffer/working buffer. output and capture queue buffer: user space will call tee related interface to allocate secure handle. Will convert to secure handle with v4l2 framework, then send secure handle to optee-os. working buffer: calling dma_heap and dma_buf to get secure memory handle, then covert secure iova in optee-os. Using the same kernel driver for svp and non-svp playback, just the buffer type are different. Normal is iova and secure is secure handle. User driver will tell the kernel driver with CID control whether the current playback is svp or non-svp. Best Regards, Yunfei Dong > Regards, > > Hans > > > > > regards, > > Nicolas > > > > p.s. you forgot to document your control in the RST doc, please do > > in following > > release. > > > > > +ctx->is_svp_mode = ctrl->val; > > > + > > > +if (ctx->is_svp_mode) { > > > +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > > > +if (ret) > > > +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > > > +else > > > +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > > > > val); > > > +} > > > +break; > > > default: > > > mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > > > 0x%x\n", > > hdr_ctrl->id); > > > return ret; > > > @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > mtk_vcodec_dec_ctx *ctx) > > > unsigned int i; > > > struct v4l2_ctrl *ctrl; > > > > > > -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > > > +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > > > if (ctx->ctrl_hdl.error) { > > > mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > > > return ctx->ctrl_hdl.error; > > > @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > mtk_vcodec_dec_ctx *ctx) > > > > > > ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > &mtk_vcodec_dec_ctrl_ops, > > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > > > +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > &mtk_vcodec_dec_ctrl_ops, > > > + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > > > > > > v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > > > > > > diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > index d8cf01f76aab..a507045a3f30 100644 > > > --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > > > case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > > > "Reference > > Frames for a P-Frame"; > > > case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > > SPS and PPS to IDR"; > > > case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > > > Decoder > > get secure handle"; > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > > set secure mode"; > > > > > > /* AV1 controls */ > > > case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > > > @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > > **name, enum v4l2_ctrl_type *type, > > > *type = V4L2_CTRL_TYPE_INTEGER; > > > *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > break; > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > +*type = V4L2_CTRL_TYPE_INTEGER; > > > +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > +break; > > > case V4L2_CID_USER_CLASS: > > > case V4L2_CID_CAMERA_CLASS: > > > case V4L2_CID_CODEC_CLASS: > > > diff --git a/include/uapi/linux/v4l2-controls.h > > b/include/uapi/linux/v4l2-controls.h > > > index 7b3694985366..88e90d943e38 100644 > > > --- a/include/uapi/linux/v4l2-controls.h > > > +++ b/include/uapi/linux/v4l2-controls.h > > > @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > > > /* MPEG-class control IDs specific to the MediaTek Decoder > > driver as defined by V4L2 */ > > > #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > > > #define > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > > > +#define > > V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > > > > > > /* Camera class control IDs */ > > >
On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > Hi Hans & Nicolas, > > Thanks for your advice. > > On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: >> >> External email : Please do not click links or open attachments until >> you have verified the sender or the content. >> Hi, >> >> On 9/11/23 17:54, Nicolas Dufresne wrote: >>> Hi, >>> >>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : >>>> Setting secure mode flag to kernel when trying to play secure >> >> video, >>>> then decoder driver will initialize tee related interface to >> >> support >>>> svp. >>> >>> >>> This is not what the patch is doing, please rework. This patch is >> >> an vendor API >>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should >> >> not have to >>> read your patch to understand this. >>> >>>> >>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> >>>> --- >>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 >> >> ++++++++++++++- >>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ >>>> include/uapi/linux/v4l2-controls.h | 1 + >>>> 3 files changed, 20 insertions(+), 1 deletion(-) >>>> >>>> diff --git >> >> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >> less.c >> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >> less.c >>>> index d2b09ce9f1cf..a981178c25d9 100644 >>>> --- >> >> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >> less.c >>>> +++ >> >> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >> less.c >>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl >> >> *ctrl) >>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); >>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", >> >> sec_fd, ctrl->val); >>>> break; >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>> >>> Stepping back a little and focusing on the API, what makes your >> >> driver so >>> special that it should be the only one having a "secure mode" ? We >> >> are touching >>> in gap in the media pipeline in Linux, and this should come with >> >> consideration >>> of the global API. >>> >>> Why is this API better then let's say Google Android one, were they >> >> expose 2 >>> device nodes in their fork of the MFC driver (a secure and a non >> >> secure one) ? >> >> Perhaps it is a good idea to first post an RFC with an uAPI proposal >> on how to >> handle secure video. I suspect this isn't mediatek specific, other >> SoCs with >> tee support could use this as well. >> >> As Nicolas said, it's long known to be a gap in our media support, so >> it is >> really great that you started work on this, but you need to look at >> this from >> a more generic point-of-view, and not mediatek-specific. >> > > Whether your have any advice about how to do a more generic driver to > handle secure video playback? > > There are several kind of buffer: output queue buffer/capture queue > buffer/working buffer. > > output and capture queue buffer: user space will call tee related > interface to allocate secure handle. Will convert to secure handle with > v4l2 framework, then send secure handle to optee-os. > > working buffer: calling dma_heap and dma_buf to get secure memory > handle, then covert secure iova in optee-os. > > Using the same kernel driver for svp and non-svp playback, just the > buffer type are different. Normal is iova and secure is secure handle. > > User driver will tell the kernel driver with CID control whether the > current playback is svp or non-svp. My understanding is that when you switch to secure mode, the driver makes some optee calls to set everything up. And userspace needs a way convert a dmabuf fd to a 'secure handle', which appears to be the DMA address of the buffer. Who uses that handle? In any case, using a control to switch to secure mode and using a control to convert a dmabuf fd to a secure handle seems a poor choice to me. I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that once you create buffers for the first time, the driver can switch into secure mode, and until all buffers are released again you know that the driver will stay in secure mode. For converting the dmabuf fd into a secure handle: a new ioctl similar to VIDIOC_EXPBUF might be more suited for that. Note that I am the first to admit that I have no experience with secure video pipelines or optee-os, so I am looking at this purely from an uAPI perspective. Regards, Hans > > Best Regards, > Yunfei Dong >> Regards, >> >> Hans >> >>> >>> regards, >>> Nicolas >>> >>> p.s. you forgot to document your control in the RST doc, please do >> >> in following >>> release. >>> >>>> +ctx->is_svp_mode = ctrl->val; >>>> + >>>> +if (ctx->is_svp_mode) { >>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); >>>> +if (ret) >>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); >>>> +else >>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- >>> >>> val); >>>> +} >>>> +break; >>>> default: >>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: >>>> 0x%x\n", >> >> hdr_ctrl->id); >>>> return ret; >>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct >> >> mtk_vcodec_dec_ctx *ctx) >>>> unsigned int i; >>>> struct v4l2_ctrl *ctrl; >>>> >>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); >>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); >>>> if (ctx->ctrl_hdl.error) { >>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); >>>> return ctx->ctrl_hdl.error; >>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct >> >> mtk_vcodec_dec_ctx *ctx) >>>> >>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >> >> &mtk_vcodec_dec_ctrl_ops, >>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); >>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >> >> &mtk_vcodec_dec_ctrl_ops, >>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); >>>> >>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); >>>> >>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >> >> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>> index d8cf01f76aab..a507045a3f30 100644 >>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) >>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return >>>> "Reference >> >> Frames for a P-Frame"; >>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend >> >> SPS and PPS to IDR"; >>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek >>>> Decoder >> >> get secure handle"; >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder >> >> set secure mode"; >>>> >>>> /* AV1 controls */ >>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; >>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char >> >> **name, enum v4l2_ctrl_type *type, >>>> *type = V4L2_CTRL_TYPE_INTEGER; >>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>> break; >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>> +*type = V4L2_CTRL_TYPE_INTEGER; >>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>> +break; >>>> case V4L2_CID_USER_CLASS: >>>> case V4L2_CID_CAMERA_CLASS: >>>> case V4L2_CID_CODEC_CLASS: >>>> diff --git a/include/uapi/linux/v4l2-controls.h >> >> b/include/uapi/linux/v4l2-controls.h >>>> index 7b3694985366..88e90d943e38 100644 >>>> --- a/include/uapi/linux/v4l2-controls.h >>>> +++ b/include/uapi/linux/v4l2-controls.h >>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { >>>> /* MPEG-class control IDs specific to the MediaTek Decoder >> >> driver as defined by V4L2 */ >>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) >>>> #define >> >> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) >>>> +#define >> >> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) >>>> >>>> /* Camera class control IDs */ >>>>
Hi Hans & Nicolas, Thanks for your advice. On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > > External email : Please do not click links or open attachments until > you have verified the sender or the content. > Hi, > > On 9/11/23 17:54, Nicolas Dufresne wrote: > > Hi, > > > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > >> Setting secure mode flag to kernel when trying to play secure > video, > >> then decoder driver will initialize tee related interface to > support > >> svp. > > > > > > This is not what the patch is doing, please rework. This patch is > an vendor API > > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > not have to > > read your patch to understand this. > > > >> > >> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > >> --- > >> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > ++++++++++++++- > >> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > >> include/uapi/linux/v4l2-controls.h | 1 + > >> 3 files changed, 20 insertions(+), 1 deletion(-) > >> > >> diff --git > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > >> index d2b09ce9f1cf..a981178c25d9 100644 > >> --- > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > >> +++ > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > less.c > >> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > *ctrl) > >> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > >> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > sec_fd, ctrl->val); > >> break; > >> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > Stepping back a little and focusing on the API, what makes your > driver so > > special that it should be the only one having a "secure mode" ? We > are touching > > in gap in the media pipeline in Linux, and this should come with > consideration > > of the global API. > > > > Why is this API better then let's say Google Android one, were they > expose 2 > > device nodes in their fork of the MFC driver (a secure and a non > secure one) ? > > Perhaps it is a good idea to first post an RFC with an uAPI proposal > on how to > handle secure video. I suspect this isn't mediatek specific, other > SoCs with > tee support could use this as well. > > As Nicolas said, it's long known to be a gap in our media support, so > it is > really great that you started work on this, but you need to look at > this from > a more generic point-of-view, and not mediatek-specific. > Whether your have any advice about how to do a more generic driver to handle secure video playback? There are several kind of buffer: output queue buffer/capture queue buffer/working buffer. output and capture queue buffer: user space will call tee related interface to allocate secure handle. Will convert to secure handle with v4l2 framework, then send secure handle to optee-os. working buffer: calling dma_heap and dma_buf to get secure memory handle, then covert secure iova in optee-os. Using the same kernel driver for svp and non-svp playback, just the buffer type are different. Normal is iova and secure is secure handle. User driver will tell the kernel driver with CID control whether the current playback is svp or non-svp. Best Regards, Yunfei Dong > Regards, > > Hans > > > > > regards, > > Nicolas > > > > p.s. you forgot to document your control in the RST doc, please do > in following > > release. > > > >> +ctx->is_svp_mode = ctrl->val; > >> + > >> +if (ctx->is_svp_mode) { > >> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >> +if (ret) > >> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >> +else > >> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > >val); > >> +} > >> +break; > >> default: > >> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: 0x%x\n", > hdr_ctrl->id); > >> return ret; > >> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > mtk_vcodec_dec_ctx *ctx) > >> unsigned int i; > >> struct v4l2_ctrl *ctrl; > >> > >> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >> if (ctx->ctrl_hdl.error) { > >> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >> return ctx->ctrl_hdl.error; > >> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > mtk_vcodec_dec_ctx *ctx) > >> > >> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > &mtk_vcodec_dec_ctrl_ops, > >> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > &mtk_vcodec_dec_ctrl_ops, > >> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >> > >> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >> > >> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >> index d8cf01f76aab..a507045a3f30 100644 > >> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return "Reference > Frames for a P-Frame"; > >> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > SPS and PPS to IDR"; > >> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek Decoder > get secure handle"; > >> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > set secure mode"; > >> > >> /* AV1 controls */ > >> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > **name, enum v4l2_ctrl_type *type, > >> *type = V4L2_CTRL_TYPE_INTEGER; > >> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >> break; > >> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >> +*type = V4L2_CTRL_TYPE_INTEGER; > >> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >> +break; > >> case V4L2_CID_USER_CLASS: > >> case V4L2_CID_CAMERA_CLASS: > >> case V4L2_CID_CODEC_CLASS: > >> diff --git a/include/uapi/linux/v4l2-controls.h > b/include/uapi/linux/v4l2-controls.h > >> index 7b3694985366..88e90d943e38 100644 > >> --- a/include/uapi/linux/v4l2-controls.h > >> +++ b/include/uapi/linux/v4l2-controls.h > >> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > >> /* MPEG-class control IDs specific to the MediaTek Decoder > driver as defined by V4L2 */ > >> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > >> #define > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >> +#define > V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >> > >> /* Camera class control IDs */ > >> > > >
Hi Hans, Thanks for your advice. On Fri, 2023-09-15 at 10:54 +0200, Hans Verkuil wrote: > > External email : Please do not click links or open attachments until > you have verified the sender or the content. > On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > > Hi Hans & Nicolas, > > > > Thanks for your advice. > > > > On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > >> > >> External email : Please do not click links or open attachments > until > >> you have verified the sender or the content. > >> Hi, > >> > >> On 9/11/23 17:54, Nicolas Dufresne wrote: > >>> Hi, > >>> > >>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > >>>> Setting secure mode flag to kernel when trying to play secure > >> > >> video, > >>>> then decoder driver will initialize tee related interface to > >> > >> support > >>>> svp. > >>> > >>> > >>> This is not what the patch is doing, please rework. This patch is > >> > >> an vendor API > >>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > >> > >> not have to > >>> read your patch to understand this. > >>> > >>>> > >>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > >>>> --- > >>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > >> > >> ++++++++++++++- > >>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > >>>> include/uapi/linux/v4l2-controls.h | 1 + > >>>> 3 files changed, 20 insertions(+), 1 deletion(-) > >>>> > >>>> diff --git > >> > >> > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >> > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >>>> index d2b09ce9f1cf..a981178c25d9 100644 > >>>> --- > >> > >> > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >>>> +++ > >> > >> > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > >> > >> *ctrl) > >>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > >>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > >> > >> sec_fd, ctrl->val); > >>>> break; > >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>> > >>> Stepping back a little and focusing on the API, what makes your > >> > >> driver so > >>> special that it should be the only one having a "secure mode" ? > We > >> > >> are touching > >>> in gap in the media pipeline in Linux, and this should come with > >> > >> consideration > >>> of the global API. > >>> > >>> Why is this API better then let's say Google Android one, were > they > >> > >> expose 2 > >>> device nodes in their fork of the MFC driver (a secure and a non > >> > >> secure one) ? > >> > >> Perhaps it is a good idea to first post an RFC with an uAPI > proposal > >> on how to > >> handle secure video. I suspect this isn't mediatek specific, other > >> SoCs with > >> tee support could use this as well. > >> > >> As Nicolas said, it's long known to be a gap in our media support, > so > >> it is > >> really great that you started work on this, but you need to look > at > >> this from > >> a more generic point-of-view, and not mediatek-specific. > >> > > > > Whether your have any advice about how to do a more generic driver > to > > handle secure video playback? > > > > There are several kind of buffer: output queue buffer/capture queue > > buffer/working buffer. > > > > output and capture queue buffer: user space will call tee related > > interface to allocate secure handle. Will convert to secure handle > with > > v4l2 framework, then send secure handle to optee-os. > > > > working buffer: calling dma_heap and dma_buf to get secure memory > > handle, then covert secure iova in optee-os. > > > > Using the same kernel driver for svp and non-svp playback, just the > > buffer type are different. Normal is iova and secure is secure > handle. > > > > User driver will tell the kernel driver with CID control whether > the > > current playback is svp or non-svp. > > My understanding is that when you switch to secure mode, the driver > makes > some optee calls to set everything up. And userspace needs a way > convert a > dmabuf fd to a 'secure handle', which appears to be the DMA address > of the > buffer. Who uses that handle? > User space need to get/set information for some secure memory with dma secure handle, such as input buffer/frame buffer/tmp buffer, etc. Secure fd will convert to secure handle in kernel space then in optee- os in user space. > In any case, using a control to switch to secure mode and using a > control > to convert a dmabuf fd to a secure handle seems a poor choice to me. > > I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ > type, > e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > ensures that > once you create buffers for the first time, the driver can switch > into secure > mode, and until all buffers are released again you know that the > driver will > stay in secure mode. > It's no need to add V4L2_MEMORY_DMABUF_SECURE, v4l2 framewrok can convert secure fd to secure handle directly for VB2_MEMROY_DMABUF. User space will queue capture and output buffer, then calling vb2_dma_contig_plane_dma_addr to get secure handle for secure mode, getting iova for normal playback. If the driver want to use memory type MMAP, maybe need to add VB2_MEMROY_MMAP_SECURE. > For converting the dmabuf fd into a secure handle: a new ioctl > similar to > VIDIOC_EXPBUF might be more suited for that. > > Note that I am the first to admit that I have no experience with > secure > video pipelines or optee-os, so I am looking at this purely from an > uAPI > perspective. > It's a good choice to convert secure fd to secure handle with a new ioctl callback, will call the ioctl many times for there are many buffers. If the driver in secure fd to secure handler callback, the driver regard the playback is secure mode? Best Regards, Yunfei Dong > Regards, > > Hans > > > > > Best Regards, > > Yunfei Dong > >> Regards, > >> > >> Hans > >> > >>> > >>> regards, > >>> Nicolas > >>> > >>> p.s. you forgot to document your control in the RST doc, please > do > >> > >> in following > >>> release. > >>> > >>>> +ctx->is_svp_mode = ctrl->val; > >>>> + > >>>> +if (ctx->is_svp_mode) { > >>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >>>> +if (ret) > >>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >>>> +else > >>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > >>> > >>> val); > >>>> +} > >>>> +break; > >>>> default: > >>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > >>>> 0x%x\n", > >> > >> hdr_ctrl->id); > >>>> return ret; > >>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >> > >> mtk_vcodec_dec_ctx *ctx) > >>>> unsigned int i; > >>>> struct v4l2_ctrl *ctrl; > >>>> > >>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >>>> if (ctx->ctrl_hdl.error) { > >>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >>>> return ctx->ctrl_hdl.error; > >>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >> > >> mtk_vcodec_dec_ctx *ctx) > >>>> > >>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >> > >> &mtk_vcodec_dec_ctrl_ops, > >>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >> > >> &mtk_vcodec_dec_ctrl_ops, > >>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >>>> > >>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >>>> > >>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >> > >> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> index d8cf01f76aab..a507045a3f30 100644 > >>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > >>>> "Reference > >> > >> Frames for a P-Frame"; > >>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > >> > >> SPS and PPS to IDR"; > >>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > >>>> Decoder > >> > >> get secure handle"; > >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > >> > >> set secure mode"; > >>>> > >>>> /* AV1 controls */ > >>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > >> > >> **name, enum v4l2_ctrl_type *type, > >>>> *type = V4L2_CTRL_TYPE_INTEGER; > >>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>> break; > >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>> +*type = V4L2_CTRL_TYPE_INTEGER; > >>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>> +break; > >>>> case V4L2_CID_USER_CLASS: > >>>> case V4L2_CID_CAMERA_CLASS: > >>>> case V4L2_CID_CODEC_CLASS: > >>>> diff --git a/include/uapi/linux/v4l2-controls.h > >> > >> b/include/uapi/linux/v4l2-controls.h > >>>> index 7b3694985366..88e90d943e38 100644 > >>>> --- a/include/uapi/linux/v4l2-controls.h > >>>> +++ b/include/uapi/linux/v4l2-controls.h > >>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type > { > >>>> /* MPEG-class control IDs specific to the MediaTek Decoder > >> > >> driver as defined by V4L2 */ > >>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > >>>> #define > >> > >> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >>>> +#define > >> > >> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >>>> > >>>> /* Camera class control IDs */ > >>>> >
On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > > Hi Hans & Nicolas, > > > > Thanks for your advice. > > > > On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > >> > >> External email : Please do not click links or open attachments until > >> you have verified the sender or the content. > >> Hi, > >> > >> On 9/11/23 17:54, Nicolas Dufresne wrote: > >>> Hi, > >>> > >>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > >>>> Setting secure mode flag to kernel when trying to play secure > >> > >> video, > >>>> then decoder driver will initialize tee related interface to > >> > >> support > >>>> svp. > >>> > >>> > >>> This is not what the patch is doing, please rework. This patch is > >> > >> an vendor API > >>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > >> > >> not have to > >>> read your patch to understand this. > >>> > >>>> > >>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > >>>> --- > >>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > >> > >> ++++++++++++++- > >>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > >>>> include/uapi/linux/v4l2-controls.h | 1 + > >>>> 3 files changed, 20 insertions(+), 1 deletion(-) > >>>> > >>>> diff --git > >> > >> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >>>> index d2b09ce9f1cf..a981178c25d9 100644 > >>>> --- > >> > >> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >>>> +++ > >> > >> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >> less.c > >>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > >> > >> *ctrl) > >>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > >>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > >> > >> sec_fd, ctrl->val); > >>>> break; > >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>> > >>> Stepping back a little and focusing on the API, what makes your > >> > >> driver so > >>> special that it should be the only one having a "secure mode" ? We > >> > >> are touching > >>> in gap in the media pipeline in Linux, and this should come with > >> > >> consideration > >>> of the global API. > >>> > >>> Why is this API better then let's say Google Android one, were they > >> > >> expose 2 > >>> device nodes in their fork of the MFC driver (a secure and a non > >> > >> secure one) ? > >> > >> Perhaps it is a good idea to first post an RFC with an uAPI proposal > >> on how to > >> handle secure video. I suspect this isn't mediatek specific, other > >> SoCs with > >> tee support could use this as well. > >> > >> As Nicolas said, it's long known to be a gap in our media support, so > >> it is > >> really great that you started work on this, but you need to look at > >> this from > >> a more generic point-of-view, and not mediatek-specific. > >> > > > > Whether your have any advice about how to do a more generic driver to > > handle secure video playback? > > > > There are several kind of buffer: output queue buffer/capture queue > > buffer/working buffer. > > > > output and capture queue buffer: user space will call tee related > > interface to allocate secure handle. Will convert to secure handle with > > v4l2 framework, then send secure handle to optee-os. > > > > working buffer: calling dma_heap and dma_buf to get secure memory > > handle, then covert secure iova in optee-os. > > > > Using the same kernel driver for svp and non-svp playback, just the > > buffer type are different. Normal is iova and secure is secure handle. > > > > User driver will tell the kernel driver with CID control whether the > > current playback is svp or non-svp. > > My understanding is that when you switch to secure mode, the driver makes > some optee calls to set everything up. And userspace needs a way convert a > dmabuf fd to a 'secure handle', which appears to be the DMA address of the > buffer. Who uses that handle? The only user space usage for getting the 'secure handle' from an fd is when that memory is written to. This is done when the TEE decrypts the video contents. User space sends the encrypted video + 'secure handle' to the TEE, and the TEE decrypts the contents to the memory associated with the 'secure handle'. Then the 'secure handle' is passed into the TEE again with the v4l2 driver to use as the source for video decoding (but w/ v4l2, user space is passing in fds). > > In any case, using a control to switch to secure mode and using a control > to convert a dmabuf fd to a secure handle seems a poor choice to me. > > I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, > e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that > once you create buffers for the first time, the driver can switch into secure > mode, and until all buffers are released again you know that the driver will > stay in secure mode. Why do you think the control for setting secure mode is a poor choice? There's various places in the driver code where functionality changes based on being secure/non-secure mode, so this is very much a 'global' setting for the driver. It could be inferred based off a new memory type for the queues...which then sets that flag in the driver; but that seems like it would be more fragile and would require checking for incompatible output/capture memory types. I'm not against another way of doing this; but didn't see why you think the proposed method is a poor choice. > > For converting the dmabuf fd into a secure handle: a new ioctl similar to > VIDIOC_EXPBUF might be more suited for that. I actually think the best way for converting the dmabuf fd into a secure handle would be another ioctl in the dma-heap driver...since that's where the memory is actually allocated from. But this really depends on upstream maintainers and what they are comfortable with. > > Note that I am the first to admit that I have no experience with secure > video pipelines or optee-os, so I am looking at this purely from an uAPI > perspective. > > Regards, > > Hans > > > > > Best Regards, > > Yunfei Dong > >> Regards, > >> > >> Hans > >> > >>> > >>> regards, > >>> Nicolas > >>> > >>> p.s. you forgot to document your control in the RST doc, please do > >> > >> in following > >>> release. > >>> > >>>> +ctx->is_svp_mode = ctrl->val; > >>>> + > >>>> +if (ctx->is_svp_mode) { > >>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >>>> +if (ret) > >>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >>>> +else > >>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > >>> > >>> val); > >>>> +} > >>>> +break; > >>>> default: > >>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > >>>> 0x%x\n", > >> > >> hdr_ctrl->id); > >>>> return ret; > >>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >> > >> mtk_vcodec_dec_ctx *ctx) > >>>> unsigned int i; > >>>> struct v4l2_ctrl *ctrl; > >>>> > >>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >>>> if (ctx->ctrl_hdl.error) { > >>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >>>> return ctx->ctrl_hdl.error; > >>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >> > >> mtk_vcodec_dec_ctx *ctx) > >>>> > >>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >> > >> &mtk_vcodec_dec_ctrl_ops, > >>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >> > >> &mtk_vcodec_dec_ctrl_ops, > >>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >>>> > >>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >>>> > >>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >> > >> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> index d8cf01f76aab..a507045a3f30 100644 > >>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > >>>> "Reference > >> > >> Frames for a P-Frame"; > >>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > >> > >> SPS and PPS to IDR"; > >>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > >>>> Decoder > >> > >> get secure handle"; > >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > >> > >> set secure mode"; > >>>> > >>>> /* AV1 controls */ > >>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > >> > >> **name, enum v4l2_ctrl_type *type, > >>>> *type = V4L2_CTRL_TYPE_INTEGER; > >>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>> break; > >>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>> +*type = V4L2_CTRL_TYPE_INTEGER; > >>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>> +break; > >>>> case V4L2_CID_USER_CLASS: > >>>> case V4L2_CID_CAMERA_CLASS: > >>>> case V4L2_CID_CODEC_CLASS: > >>>> diff --git a/include/uapi/linux/v4l2-controls.h > >> > >> b/include/uapi/linux/v4l2-controls.h > >>>> index 7b3694985366..88e90d943e38 100644 > >>>> --- a/include/uapi/linux/v4l2-controls.h > >>>> +++ b/include/uapi/linux/v4l2-controls.h > >>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > >>>> /* MPEG-class control IDs specific to the MediaTek Decoder > >> > >> driver as defined by V4L2 */ > >>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > >>>> #define > >> > >> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >>>> +#define > >> > >> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >>>> > >>>> /* Camera class control IDs */ > >>>> > > > _______________________________________________ > linux-arm-kernel mailing list > linux-arm-kernel@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
On 18/09/2023 22:57, Jeffrey Kardatzke wrote: > On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >> >> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: >>> Hi Hans & Nicolas, >>> >>> Thanks for your advice. >>> >>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: >>>> >>>> External email : Please do not click links or open attachments until >>>> you have verified the sender or the content. >>>> Hi, >>>> >>>> On 9/11/23 17:54, Nicolas Dufresne wrote: >>>>> Hi, >>>>> >>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : >>>>>> Setting secure mode flag to kernel when trying to play secure >>>> >>>> video, >>>>>> then decoder driver will initialize tee related interface to >>>> >>>> support >>>>>> svp. >>>>> >>>>> >>>>> This is not what the patch is doing, please rework. This patch is >>>> >>>> an vendor API >>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should >>>> >>>> not have to >>>>> read your patch to understand this. >>>>> >>>>>> >>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> >>>>>> --- >>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 >>>> >>>> ++++++++++++++- >>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ >>>>>> include/uapi/linux/v4l2-controls.h | 1 + >>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) >>>>>> >>>>>> diff --git >>>> >>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>> less.c >>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>> less.c >>>>>> index d2b09ce9f1cf..a981178c25d9 100644 >>>>>> --- >>>> >>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>> less.c >>>>>> +++ >>>> >>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>> less.c >>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl >>>> >>>> *ctrl) >>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); >>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", >>>> >>>> sec_fd, ctrl->val); >>>>>> break; >>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>> >>>>> Stepping back a little and focusing on the API, what makes your >>>> >>>> driver so >>>>> special that it should be the only one having a "secure mode" ? We >>>> >>>> are touching >>>>> in gap in the media pipeline in Linux, and this should come with >>>> >>>> consideration >>>>> of the global API. >>>>> >>>>> Why is this API better then let's say Google Android one, were they >>>> >>>> expose 2 >>>>> device nodes in their fork of the MFC driver (a secure and a non >>>> >>>> secure one) ? >>>> >>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal >>>> on how to >>>> handle secure video. I suspect this isn't mediatek specific, other >>>> SoCs with >>>> tee support could use this as well. >>>> >>>> As Nicolas said, it's long known to be a gap in our media support, so >>>> it is >>>> really great that you started work on this, but you need to look at >>>> this from >>>> a more generic point-of-view, and not mediatek-specific. >>>> >>> >>> Whether your have any advice about how to do a more generic driver to >>> handle secure video playback? >>> >>> There are several kind of buffer: output queue buffer/capture queue >>> buffer/working buffer. >>> >>> output and capture queue buffer: user space will call tee related >>> interface to allocate secure handle. Will convert to secure handle with >>> v4l2 framework, then send secure handle to optee-os. >>> >>> working buffer: calling dma_heap and dma_buf to get secure memory >>> handle, then covert secure iova in optee-os. >>> >>> Using the same kernel driver for svp and non-svp playback, just the >>> buffer type are different. Normal is iova and secure is secure handle. >>> >>> User driver will tell the kernel driver with CID control whether the >>> current playback is svp or non-svp. >> >> My understanding is that when you switch to secure mode, the driver makes >> some optee calls to set everything up. And userspace needs a way convert a >> dmabuf fd to a 'secure handle', which appears to be the DMA address of the >> buffer. Who uses that handle? > > The only user space usage for getting the 'secure handle' from an fd > is when that memory is written to. This is done when the TEE decrypts > the video contents. User space sends the encrypted video + 'secure > handle' to the TEE, and the TEE decrypts the contents to the memory > associated with the 'secure handle'. Then the 'secure handle' is > passed into the TEE again with the v4l2 driver to use as the source > for video decoding (but w/ v4l2, user space is passing in fds). I think I need some more background. This series is to support a 'Secure Video Processor' (at least, that's what svp stands for I believe, something that is not mentioned anywhere in this series, BTW) which is used to decode an encrypted h264 stream. First question: how is that stream encrypted? Is that according to some standard? Nothing is mentioned about that. I gather that the encrypted stream is fed to the codec as usual (i.e. just put it in the output buffer and queue it to the codec), nothing special is needed for that. Except, how does the hardware know it is encrypted? I guess that's where the control comes in, you have to turn on SVP mode first. For the capture buffers you need to provide buffers from secure/trusted memory. That's a dmabuf fd, but where does that come from? I saw this message: https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u so I expect that's where it comes from. But I agree that getting this from dma-heaps seems more natural. I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') For actually displaying these secure buffers you would use drm, and I assume that the hardware would mix in the contents of the secure buffer into the video output pipeline? I.e., the actual contents remain inaccessible. And that the video output (HDMI or DisplayPort) is using HDCP? > >> >> In any case, using a control to switch to secure mode and using a control >> to convert a dmabuf fd to a secure handle seems a poor choice to me. >> >> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, >> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that >> once you create buffers for the first time, the driver can switch into secure >> mode, and until all buffers are released again you know that the driver will >> stay in secure mode. > > Why do you think the control for setting secure mode is a poor choice? > There's various places in the driver code where functionality changes > based on being secure/non-secure mode, so this is very much a 'global' > setting for the driver. It could be inferred based off a new memory > type for the queues...which then sets that flag in the driver; but > that seems like it would be more fragile and would require checking > for incompatible output/capture memory types. I'm not against another > way of doing this; but didn't see why you think the proposed method is > a poor choice. I assume you are either decoding to secure memory all the time, or not at all. That's something you would want to select the moment you allocate the first buffer. Using the V4L2_MEMORY_ value would be the natural place for that. A control can typically be toggled at any time, and it makes no sense to do that for secure streaming. Related to that: if you pass a dmabuf fd you will need to check somewhere if the fd points to secure memory or not. You don't want to mix the two but you want to check that at VIDIOC_QBUF time. Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, drivers do not need to do that. > >> >> For converting the dmabuf fd into a secure handle: a new ioctl similar to >> VIDIOC_EXPBUF might be more suited for that. > > I actually think the best way for converting the dmabuf fd into a > secure handle would be another ioctl in the dma-heap driver...since > that's where the memory is actually allocated from. But this really > depends on upstream maintainers and what they are comfortable with. That feels like a more natural place of doing this. Regards, Hans > >> >> Note that I am the first to admit that I have no experience with secure >> video pipelines or optee-os, so I am looking at this purely from an uAPI >> perspective. >> >> Regards, >> >> Hans >> >>> >>> Best Regards, >>> Yunfei Dong >>>> Regards, >>>> >>>> Hans >>>> >>>>> >>>>> regards, >>>>> Nicolas >>>>> >>>>> p.s. you forgot to document your control in the RST doc, please do >>>> >>>> in following >>>>> release. >>>>> >>>>>> +ctx->is_svp_mode = ctrl->val; >>>>>> + >>>>>> +if (ctx->is_svp_mode) { >>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); >>>>>> +if (ret) >>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); >>>>>> +else >>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- >>>>> >>>>> val); >>>>>> +} >>>>>> +break; >>>>>> default: >>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: >>>>>> 0x%x\n", >>>> >>>> hdr_ctrl->id); >>>>>> return ret; >>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>> >>>> mtk_vcodec_dec_ctx *ctx) >>>>>> unsigned int i; >>>>>> struct v4l2_ctrl *ctrl; >>>>>> >>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); >>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); >>>>>> if (ctx->ctrl_hdl.error) { >>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); >>>>>> return ctx->ctrl_hdl.error; >>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>> >>>> mtk_vcodec_dec_ctx *ctx) >>>>>> >>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>> >>>> &mtk_vcodec_dec_ctrl_ops, >>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); >>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>> >>>> &mtk_vcodec_dec_ctrl_ops, >>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); >>>>>> >>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); >>>>>> >>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>> >>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>> index d8cf01f76aab..a507045a3f30 100644 >>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) >>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return >>>>>> "Reference >>>> >>>> Frames for a P-Frame"; >>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend >>>> >>>> SPS and PPS to IDR"; >>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek >>>>>> Decoder >>>> >>>> get secure handle"; >>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder >>>> >>>> set secure mode"; >>>>>> >>>>>> /* AV1 controls */ >>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; >>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char >>>> >>>> **name, enum v4l2_ctrl_type *type, >>>>>> *type = V4L2_CTRL_TYPE_INTEGER; >>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>> break; >>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; >>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>> +break; >>>>>> case V4L2_CID_USER_CLASS: >>>>>> case V4L2_CID_CAMERA_CLASS: >>>>>> case V4L2_CID_CODEC_CLASS: >>>>>> diff --git a/include/uapi/linux/v4l2-controls.h >>>> >>>> b/include/uapi/linux/v4l2-controls.h >>>>>> index 7b3694985366..88e90d943e38 100644 >>>>>> --- a/include/uapi/linux/v4l2-controls.h >>>>>> +++ b/include/uapi/linux/v4l2-controls.h >>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { >>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder >>>> >>>> driver as defined by V4L2 */ >>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) >>>>>> #define >>>> >>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) >>>>>> +#define >>>> >>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) >>>>>> >>>>>> /* Camera class control IDs */ >>>>>> >> >> >> _______________________________________________ >> linux-arm-kernel mailing list >> linux-arm-kernel@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : > On 18/09/2023 22:57, Jeffrey Kardatzke wrote: > > On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > > > > > On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > > > > Hi Hans & Nicolas, > > > > > > > > Thanks for your advice. > > > > > > > > On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > > > > > > > > > > External email : Please do not click links or open attachments until > > > > > you have verified the sender or the content. > > > > > Hi, > > > > > > > > > > On 9/11/23 17:54, Nicolas Dufresne wrote: > > > > > > Hi, > > > > > > > > > > > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > > > > > > > Setting secure mode flag to kernel when trying to play secure > > > > > > > > > > video, > > > > > > > then decoder driver will initialize tee related interface to > > > > > > > > > > support > > > > > > > svp. > > > > > > > > > > > > > > > > > > This is not what the patch is doing, please rework. This patch is > > > > > > > > > > an vendor API > > > > > > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > > > > > > > > > > not have to > > > > > > read your patch to understand this. > > > > > > > > > > > > > > > > > > > > Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > > > > > > > --- > > > > > > > .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > > > > > > > > > > ++++++++++++++- > > > > > > > drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > > > > > > > include/uapi/linux/v4l2-controls.h | 1 + > > > > > > > 3 files changed, 20 insertions(+), 1 deletion(-) > > > > > > > > > > > > > > diff --git > > > > > > > > > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > less.c > > > > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > less.c > > > > > > > index d2b09ce9f1cf..a981178c25d9 100644 > > > > > > > --- > > > > > > > > > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > less.c > > > > > > > +++ > > > > > > > > > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > less.c > > > > > > > @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > > > > > > > > > > *ctrl) > > > > > > > ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > > > > > > > mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > > > > > > > > > > sec_fd, ctrl->val); > > > > > > > break; > > > > > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > > > > > > > > > Stepping back a little and focusing on the API, what makes your > > > > > > > > > > driver so > > > > > > special that it should be the only one having a "secure mode" ? We > > > > > > > > > > are touching > > > > > > in gap in the media pipeline in Linux, and this should come with > > > > > > > > > > consideration > > > > > > of the global API. > > > > > > > > > > > > Why is this API better then let's say Google Android one, were they > > > > > > > > > > expose 2 > > > > > > device nodes in their fork of the MFC driver (a secure and a non > > > > > > > > > > secure one) ? > > > > > > > > > > Perhaps it is a good idea to first post an RFC with an uAPI proposal > > > > > on how to > > > > > handle secure video. I suspect this isn't mediatek specific, other > > > > > SoCs with > > > > > tee support could use this as well. > > > > > > > > > > As Nicolas said, it's long known to be a gap in our media support, so > > > > > it is > > > > > really great that you started work on this, but you need to look at > > > > > this from > > > > > a more generic point-of-view, and not mediatek-specific. > > > > > > > > > > > > > Whether your have any advice about how to do a more generic driver to > > > > handle secure video playback? > > > > > > > > There are several kind of buffer: output queue buffer/capture queue > > > > buffer/working buffer. > > > > > > > > output and capture queue buffer: user space will call tee related > > > > interface to allocate secure handle. Will convert to secure handle with > > > > v4l2 framework, then send secure handle to optee-os. > > > > > > > > working buffer: calling dma_heap and dma_buf to get secure memory > > > > handle, then covert secure iova in optee-os. > > > > > > > > Using the same kernel driver for svp and non-svp playback, just the > > > > buffer type are different. Normal is iova and secure is secure handle. > > > > > > > > User driver will tell the kernel driver with CID control whether the > > > > current playback is svp or non-svp. > > > > > > My understanding is that when you switch to secure mode, the driver makes > > > some optee calls to set everything up. And userspace needs a way convert a > > > dmabuf fd to a 'secure handle', which appears to be the DMA address of the > > > buffer. Who uses that handle? > > > > The only user space usage for getting the 'secure handle' from an fd > > is when that memory is written to. This is done when the TEE decrypts > > the video contents. User space sends the encrypted video + 'secure > > handle' to the TEE, and the TEE decrypts the contents to the memory > > associated with the 'secure handle'. Then the 'secure handle' is > > passed into the TEE again with the v4l2 driver to use as the source > > for video decoding (but w/ v4l2, user space is passing in fds). > > I think I need some more background. This series is to support a 'Secure Video > Processor' (at least, that's what svp stands for I believe, something that > is not mentioned anywhere in this series, BTW) which is used to decode an > encrypted h264 stream. > > First question: how is that stream encrypted? Is that according to some standard? > Nothing is mentioned about that. > > I gather that the encrypted stream is fed to the codec as usual (i.e. just put it > in the output buffer and queue it to the codec), nothing special is needed for that. > Except, how does the hardware know it is encrypted? I guess that's where the > control comes in, you have to turn on SVP mode first. Decryption takes place before the decoder. I suspect there is no dedicated driver for that, the TEE driver API is similar to smart card API and fits well this task. So the decrytor consume normal memory that is encrypted and is only allowed to decrypt into secure memory. All this is happening before the decoder, so is out of scope for this patchset. Just a correction :-D. > > For the capture buffers you need to provide buffers from secure/trusted memory. > That's a dmabuf fd, but where does that come from? > > I saw this message: > > https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u > > so I expect that's where it comes from. But I agree that getting this from dma-heaps > seems more natural. > > I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') > > For actually displaying these secure buffers you would use drm, and I assume that > the hardware would mix in the contents of the secure buffer into the video output > pipeline? I.e., the actual contents remain inaccessible. And that the video output > (HDMI or DisplayPort) is using HDCP? > > > > > > > > > In any case, using a control to switch to secure mode and using a control > > > to convert a dmabuf fd to a secure handle seems a poor choice to me. > > > > > > I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, > > > e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that > > > once you create buffers for the first time, the driver can switch into secure > > > mode, and until all buffers are released again you know that the driver will > > > stay in secure mode. > > > > Why do you think the control for setting secure mode is a poor choice? > > There's various places in the driver code where functionality changes > > based on being secure/non-secure mode, so this is very much a 'global' > > setting for the driver. It could be inferred based off a new memory > > type for the queues...which then sets that flag in the driver; but > > that seems like it would be more fragile and would require checking > > for incompatible output/capture memory types. I'm not against another > > way of doing this; but didn't see why you think the proposed method is > > a poor choice. > > I assume you are either decoding to secure memory all the time, or not > at all. That's something you would want to select the moment you allocate > the first buffer. Using the V4L2_MEMORY_ value would be the natural place > for that. A control can typically be toggled at any time, and it makes > no sense to do that for secure streaming. > > Related to that: if you pass a dmabuf fd you will need to check somewhere > if the fd points to secure memory or not. You don't want to mix the two > but you want to check that at VIDIOC_QBUF time. > > Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, > drivers do not need to do that. Just to clarify a bit, and make sure I understand this too. You are proposing to introduce something like: V4L2_MEMORY_SECURE_DMABUF Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the driver that the memory is secure according to the definition of "secure" for the platform its running on. This drivers also allocate secure SHM (a standard tee concept) and have internal allocation for reconstruction buffer and some hw specific reference metadata. So the idea would be that it would keep allocation using the dmabuf heap internal APIs ? And decide which type of memory based on the memory type found in the queue? Stepping back a little, why can't we have a way for drivers to detect that dmabuf are secure ? I'm wondering if its actually useful to impose to all userspace component to know that a dmabuf is secure ? Also, regarding MTK, these are stateless decoders. I think it would be nice to show use example code that can properly parse the un-encrypted header, pass the data to the decryptor and decode. There is a bit of mechanic in there that lacks clarification, a reference implementation would clearly help. Finally, does this platform offers some clearkey implementation (or other alternative) so we can do validation and regression testing? It would be very unfortunate to add feature upstream that can only be tested by proprietary CDM software. Nicolas > > > > > > > > > For converting the dmabuf fd into a secure handle: a new ioctl similar to > > > VIDIOC_EXPBUF might be more suited for that. > > > > I actually think the best way for converting the dmabuf fd into a > > secure handle would be another ioctl in the dma-heap driver...since > > that's where the memory is actually allocated from. But this really > > depends on upstream maintainers and what they are comfortable with. > > That feels like a more natural place of doing this. > > Regards, > > Hans > > > > > > > > > Note that I am the first to admit that I have no experience with secure > > > video pipelines or optee-os, so I am looking at this purely from an uAPI > > > perspective. > > > > > > Regards, > > > > > > Hans > > > > > > > > > > > Best Regards, > > > > Yunfei Dong > > > > > Regards, > > > > > > > > > > Hans > > > > > > > > > > > > > > > > > regards, > > > > > > Nicolas > > > > > > > > > > > > p.s. you forgot to document your control in the RST doc, please do > > > > > > > > > > in following > > > > > > release. > > > > > > > > > > > > > +ctx->is_svp_mode = ctrl->val; > > > > > > > + > > > > > > > +if (ctx->is_svp_mode) { > > > > > > > +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > > > > > > > +if (ret) > > > > > > > +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > > > > > > > +else > > > > > > > +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > > > > > > > > > > > > val); > > > > > > > +} > > > > > > > +break; > > > > > > > default: > > > > > > > mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > > > > > > > 0x%x\n", > > > > > > > > > > hdr_ctrl->id); > > > > > > > return ret; > > > > > > > @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > > > > > > > > > mtk_vcodec_dec_ctx *ctx) > > > > > > > unsigned int i; > > > > > > > struct v4l2_ctrl *ctrl; > > > > > > > > > > > > > > -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > > > > > > > +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > > > > > > > if (ctx->ctrl_hdl.error) { > > > > > > > mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > > > > > > > return ctx->ctrl_hdl.error; > > > > > > > @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > > > > > > > > > mtk_vcodec_dec_ctx *ctx) > > > > > > > > > > > > > > ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > > > > > > > > > &mtk_vcodec_dec_ctrl_ops, > > > > > > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > > > > > > > +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > > > > > > > > > &mtk_vcodec_dec_ctrl_ops, > > > > > > > + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > > > > > > > > > > > > > > v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > > > > > > > > > > > > > > diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > > > > b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > index d8cf01f76aab..a507045a3f30 100644 > > > > > > > --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > > > > > > > case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > > > > > > > "Reference > > > > > > > > > > Frames for a P-Frame"; > > > > > > > case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > > > > > > > > > > SPS and PPS to IDR"; > > > > > > > case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > > > > > > > Decoder > > > > > > > > > > get secure handle"; > > > > > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > > > > > > > > > > set secure mode"; > > > > > > > > > > > > > > /* AV1 controls */ > > > > > > > case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > > > > > > > @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > > > > > > > > > > **name, enum v4l2_ctrl_type *type, > > > > > > > *type = V4L2_CTRL_TYPE_INTEGER; > > > > > > > *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > > > > > break; > > > > > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > > > > +*type = V4L2_CTRL_TYPE_INTEGER; > > > > > > > +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > > > > > +break; > > > > > > > case V4L2_CID_USER_CLASS: > > > > > > > case V4L2_CID_CAMERA_CLASS: > > > > > > > case V4L2_CID_CODEC_CLASS: > > > > > > > diff --git a/include/uapi/linux/v4l2-controls.h > > > > > > > > > > b/include/uapi/linux/v4l2-controls.h > > > > > > > index 7b3694985366..88e90d943e38 100644 > > > > > > > --- a/include/uapi/linux/v4l2-controls.h > > > > > > > +++ b/include/uapi/linux/v4l2-controls.h > > > > > > > @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > > > > > > > /* MPEG-class control IDs specific to the MediaTek Decoder > > > > > > > > > > driver as defined by V4L2 */ > > > > > > > #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > > > > > > > #define > > > > > > > > > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > > > > > > > +#define > > > > > > > > > > V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > > > > > > > > > > > > > > /* Camera class control IDs */ > > > > > > > > > > > > > > > > _______________________________________________ > > > linux-arm-kernel mailing list > > > linux-arm-kernel@lists.infradead.org > > > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >
On Tue, Sep 19, 2023 at 1:53 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 18/09/2023 22:57, Jeffrey Kardatzke wrote: > > On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > >> > >> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > >>> Hi Hans & Nicolas, > >>> > >>> Thanks for your advice. > >>> > >>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > >>>> > >>>> External email : Please do not click links or open attachments until > >>>> you have verified the sender or the content. > >>>> Hi, > >>>> > >>>> On 9/11/23 17:54, Nicolas Dufresne wrote: > >>>>> Hi, > >>>>> > >>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > >>>>>> Setting secure mode flag to kernel when trying to play secure > >>>> > >>>> video, > >>>>>> then decoder driver will initialize tee related interface to > >>>> > >>>> support > >>>>>> svp. > >>>>> > >>>>> > >>>>> This is not what the patch is doing, please rework. This patch is > >>>> > >>>> an vendor API > >>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > >>>> > >>>> not have to > >>>>> read your patch to understand this. > >>>>> > >>>>>> > >>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > >>>>>> --- > >>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > >>>> > >>>> ++++++++++++++- > >>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > >>>>>> include/uapi/linux/v4l2-controls.h | 1 + > >>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) > >>>>>> > >>>>>> diff --git > >>>> > >>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>> less.c > >>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>> less.c > >>>>>> index d2b09ce9f1cf..a981178c25d9 100644 > >>>>>> --- > >>>> > >>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>> less.c > >>>>>> +++ > >>>> > >>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>> less.c > >>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > >>>> > >>>> *ctrl) > >>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > >>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > >>>> > >>>> sec_fd, ctrl->val); > >>>>>> break; > >>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>> > >>>>> Stepping back a little and focusing on the API, what makes your > >>>> > >>>> driver so > >>>>> special that it should be the only one having a "secure mode" ? We > >>>> > >>>> are touching > >>>>> in gap in the media pipeline in Linux, and this should come with > >>>> > >>>> consideration > >>>>> of the global API. > >>>>> > >>>>> Why is this API better then let's say Google Android one, were they > >>>> > >>>> expose 2 > >>>>> device nodes in their fork of the MFC driver (a secure and a non > >>>> > >>>> secure one) ? > >>>> > >>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal > >>>> on how to > >>>> handle secure video. I suspect this isn't mediatek specific, other > >>>> SoCs with > >>>> tee support could use this as well. > >>>> > >>>> As Nicolas said, it's long known to be a gap in our media support, so > >>>> it is > >>>> really great that you started work on this, but you need to look at > >>>> this from > >>>> a more generic point-of-view, and not mediatek-specific. > >>>> > >>> > >>> Whether your have any advice about how to do a more generic driver to > >>> handle secure video playback? > >>> > >>> There are several kind of buffer: output queue buffer/capture queue > >>> buffer/working buffer. > >>> > >>> output and capture queue buffer: user space will call tee related > >>> interface to allocate secure handle. Will convert to secure handle with > >>> v4l2 framework, then send secure handle to optee-os. > >>> > >>> working buffer: calling dma_heap and dma_buf to get secure memory > >>> handle, then covert secure iova in optee-os. > >>> > >>> Using the same kernel driver for svp and non-svp playback, just the > >>> buffer type are different. Normal is iova and secure is secure handle. > >>> > >>> User driver will tell the kernel driver with CID control whether the > >>> current playback is svp or non-svp. > >> > >> My understanding is that when you switch to secure mode, the driver makes > >> some optee calls to set everything up. And userspace needs a way convert a > >> dmabuf fd to a 'secure handle', which appears to be the DMA address of the > >> buffer. Who uses that handle? > > > > The only user space usage for getting the 'secure handle' from an fd > > is when that memory is written to. This is done when the TEE decrypts > > the video contents. User space sends the encrypted video + 'secure > > handle' to the TEE, and the TEE decrypts the contents to the memory > > associated with the 'secure handle'. Then the 'secure handle' is > > passed into the TEE again with the v4l2 driver to use as the source > > for video decoding (but w/ v4l2, user space is passing in fds). > > I think I need some more background. This series is to support a 'Secure Video > Processor' (at least, that's what svp stands for I believe, something that > is not mentioned anywhere in this series, BTW) which is used to decode an > encrypted h264 stream. SVP = Secure Video Path, which is explained in what you linked to below. > > First question: how is that stream encrypted? Is that according to some standard? > Nothing is mentioned about that. The TEE does the decryption of the incoming stream to the secure buffer...and that is then fed into the output queue of v4l2. So there's no handling of encrypted content in the kernel patches...it's just about handling secure buffers and secure surfaces. > > I gather that the encrypted stream is fed to the codec as usual (i.e. just put it > in the output buffer and queue it to the codec), nothing special is needed for that. > Except, how does the hardware know it is encrypted? I guess that's where the > control comes in, you have to turn on SVP mode first. Yeah, the driver does need to know if the FDs are from secure or non-secure memory...which is what the SVP control was about. > > For the capture buffers you need to provide buffers from secure/trusted memory. > That's a dmabuf fd, but where does that come from? That fd is allocated from the secure dma-buf heap which is being proposed in another patchset. https://lore.kernel.org/linux-mediatek/20230911023038.30649-1-yong.wu@mediatek.com/ > > I saw this message: > > https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u > > so I expect that's where it comes from. But I agree that getting this from dma-heaps > seems more natural. > > I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') Correct > > For actually displaying these secure buffers you would use drm, and I assume that > the hardware would mix in the contents of the secure buffer into the video output > pipeline? I.e., the actual contents remain inaccessible. And that the video output > (HDMI or DisplayPort) is using HDCP? Correct > > > > >> > >> In any case, using a control to switch to secure mode and using a control > >> to convert a dmabuf fd to a secure handle seems a poor choice to me. > >> > >> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, > >> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that > >> once you create buffers for the first time, the driver can switch into secure > >> mode, and until all buffers are released again you know that the driver will > >> stay in secure mode. > > > > Why do you think the control for setting secure mode is a poor choice? > > There's various places in the driver code where functionality changes > > based on being secure/non-secure mode, so this is very much a 'global' > > setting for the driver. It could be inferred based off a new memory > > type for the queues...which then sets that flag in the driver; but > > that seems like it would be more fragile and would require checking > > for incompatible output/capture memory types. I'm not against another > > way of doing this; but didn't see why you think the proposed method is > > a poor choice. > > I assume you are either decoding to secure memory all the time, or not > at all. That's something you would want to select the moment you allocate > the first buffer. Using the V4L2_MEMORY_ value would be the natural place > for that. A control can typically be toggled at any time, and it makes > no sense to do that for secure streaming. Ahh ok, good point about the control being able to be toggled at any time. So adding a new V4L2_MEMORY_ type would make sense relating to that. > > Related to that: if you pass a dmabuf fd you will need to check somewhere > if the fd points to secure memory or not. You don't want to mix the two > but you want to check that at VIDIOC_QBUF time. > > Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, > drivers do not need to do that. > > > > >> > >> For converting the dmabuf fd into a secure handle: a new ioctl similar to > >> VIDIOC_EXPBUF might be more suited for that. > > > > I actually think the best way for converting the dmabuf fd into a > > secure handle would be another ioctl in the dma-heap driver...since > > that's where the memory is actually allocated from. But this really > > depends on upstream maintainers and what they are comfortable with. > > That feels like a more natural place of doing this. > > Regards, > > Hans > > > > >> > >> Note that I am the first to admit that I have no experience with secure > >> video pipelines or optee-os, so I am looking at this purely from an uAPI > >> perspective. > >> > >> Regards, > >> > >> Hans > >> > >>> > >>> Best Regards, > >>> Yunfei Dong > >>>> Regards, > >>>> > >>>> Hans > >>>> > >>>>> > >>>>> regards, > >>>>> Nicolas > >>>>> > >>>>> p.s. you forgot to document your control in the RST doc, please do > >>>> > >>>> in following > >>>>> release. > >>>>> > >>>>>> +ctx->is_svp_mode = ctrl->val; > >>>>>> + > >>>>>> +if (ctx->is_svp_mode) { > >>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >>>>>> +if (ret) > >>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >>>>>> +else > >>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > >>>>> > >>>>> val); > >>>>>> +} > >>>>>> +break; > >>>>>> default: > >>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > >>>>>> 0x%x\n", > >>>> > >>>> hdr_ctrl->id); > >>>>>> return ret; > >>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >>>> > >>>> mtk_vcodec_dec_ctx *ctx) > >>>>>> unsigned int i; > >>>>>> struct v4l2_ctrl *ctrl; > >>>>>> > >>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >>>>>> if (ctx->ctrl_hdl.error) { > >>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >>>>>> return ctx->ctrl_hdl.error; > >>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >>>> > >>>> mtk_vcodec_dec_ctx *ctx) > >>>>>> > >>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>> > >>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>> > >>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >>>>>> > >>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >>>>>> > >>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>> > >>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>> index d8cf01f76aab..a507045a3f30 100644 > >>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > >>>>>> "Reference > >>>> > >>>> Frames for a P-Frame"; > >>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > >>>> > >>>> SPS and PPS to IDR"; > >>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > >>>>>> Decoder > >>>> > >>>> get secure handle"; > >>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > >>>> > >>>> set secure mode"; > >>>>>> > >>>>>> /* AV1 controls */ > >>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > >>>> > >>>> **name, enum v4l2_ctrl_type *type, > >>>>>> *type = V4L2_CTRL_TYPE_INTEGER; > >>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>> break; > >>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; > >>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>> +break; > >>>>>> case V4L2_CID_USER_CLASS: > >>>>>> case V4L2_CID_CAMERA_CLASS: > >>>>>> case V4L2_CID_CODEC_CLASS: > >>>>>> diff --git a/include/uapi/linux/v4l2-controls.h > >>>> > >>>> b/include/uapi/linux/v4l2-controls.h > >>>>>> index 7b3694985366..88e90d943e38 100644 > >>>>>> --- a/include/uapi/linux/v4l2-controls.h > >>>>>> +++ b/include/uapi/linux/v4l2-controls.h > >>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > >>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder > >>>> > >>>> driver as defined by V4L2 */ > >>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > >>>>>> #define > >>>> > >>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >>>>>> +#define > >>>> > >>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >>>>>> > >>>>>> /* Camera class control IDs */ > >>>>>> > >> > >> > >> _______________________________________________ > >> linux-arm-kernel mailing list > >> linux-arm-kernel@lists.infradead.org > >> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >
On Tue, Sep 19, 2023 at 11:51 AM Nicolas Dufresne <nicolas.dufresne@collabora.com> wrote: > > Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : > > On 18/09/2023 22:57, Jeffrey Kardatzke wrote: > > > On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > > > > > > > On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > > > > > Hi Hans & Nicolas, > > > > > > > > > > Thanks for your advice. > > > > > > > > > > On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > > > > > > > > > > > > External email : Please do not click links or open attachments until > > > > > > you have verified the sender or the content. > > > > > > Hi, > > > > > > > > > > > > On 9/11/23 17:54, Nicolas Dufresne wrote: > > > > > > > Hi, > > > > > > > > > > > > > > Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > > > > > > > > Setting secure mode flag to kernel when trying to play secure > > > > > > > > > > > > video, > > > > > > > > then decoder driver will initialize tee related interface to > > > > > > > > > > > > support > > > > > > > > svp. > > > > > > > > > > > > > > > > > > > > > This is not what the patch is doing, please rework. This patch is > > > > > > > > > > > > an vendor API > > > > > > > addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > > > > > > > > > > > > not have to > > > > > > > read your patch to understand this. > > > > > > > > > > > > > > > > > > > > > > > Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > > > > > > > > --- > > > > > > > > .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > > > > > > > > > > > > ++++++++++++++- > > > > > > > > drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > > > > > > > > include/uapi/linux/v4l2-controls.h | 1 + > > > > > > > > 3 files changed, 20 insertions(+), 1 deletion(-) > > > > > > > > > > > > > > > > diff --git > > > > > > > > > > > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > > less.c > > > > > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > > less.c > > > > > > > > index d2b09ce9f1cf..a981178c25d9 100644 > > > > > > > > --- > > > > > > > > > > > > a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > > less.c > > > > > > > > +++ > > > > > > > > > > > > b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > > > > > > less.c > > > > > > > > @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > > > > > > > > > > > > *ctrl) > > > > > > > > ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > > > > > > > > mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > > > > > > > > > > > > sec_fd, ctrl->val); > > > > > > > > break; > > > > > > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > > > > > > > > > > > Stepping back a little and focusing on the API, what makes your > > > > > > > > > > > > driver so > > > > > > > special that it should be the only one having a "secure mode" ? We > > > > > > > > > > > > are touching > > > > > > > in gap in the media pipeline in Linux, and this should come with > > > > > > > > > > > > consideration > > > > > > > of the global API. > > > > > > > > > > > > > > Why is this API better then let's say Google Android one, were they > > > > > > > > > > > > expose 2 > > > > > > > device nodes in their fork of the MFC driver (a secure and a non > > > > > > > > > > > > secure one) ? > > > > > > > > > > > > Perhaps it is a good idea to first post an RFC with an uAPI proposal > > > > > > on how to > > > > > > handle secure video. I suspect this isn't mediatek specific, other > > > > > > SoCs with > > > > > > tee support could use this as well. > > > > > > > > > > > > As Nicolas said, it's long known to be a gap in our media support, so > > > > > > it is > > > > > > really great that you started work on this, but you need to look at > > > > > > this from > > > > > > a more generic point-of-view, and not mediatek-specific. > > > > > > > > > > > > > > > > Whether your have any advice about how to do a more generic driver to > > > > > handle secure video playback? > > > > > > > > > > There are several kind of buffer: output queue buffer/capture queue > > > > > buffer/working buffer. > > > > > > > > > > output and capture queue buffer: user space will call tee related > > > > > interface to allocate secure handle. Will convert to secure handle with > > > > > v4l2 framework, then send secure handle to optee-os. > > > > > > > > > > working buffer: calling dma_heap and dma_buf to get secure memory > > > > > handle, then covert secure iova in optee-os. > > > > > > > > > > Using the same kernel driver for svp and non-svp playback, just the > > > > > buffer type are different. Normal is iova and secure is secure handle. > > > > > > > > > > User driver will tell the kernel driver with CID control whether the > > > > > current playback is svp or non-svp. > > > > > > > > My understanding is that when you switch to secure mode, the driver makes > > > > some optee calls to set everything up. And userspace needs a way convert a > > > > dmabuf fd to a 'secure handle', which appears to be the DMA address of the > > > > buffer. Who uses that handle? > > > > > > The only user space usage for getting the 'secure handle' from an fd > > > is when that memory is written to. This is done when the TEE decrypts > > > the video contents. User space sends the encrypted video + 'secure > > > handle' to the TEE, and the TEE decrypts the contents to the memory > > > associated with the 'secure handle'. Then the 'secure handle' is > > > passed into the TEE again with the v4l2 driver to use as the source > > > for video decoding (but w/ v4l2, user space is passing in fds). > > > > I think I need some more background. This series is to support a 'Secure Video > > Processor' (at least, that's what svp stands for I believe, something that > > is not mentioned anywhere in this series, BTW) which is used to decode an > > encrypted h264 stream. > > > > First question: how is that stream encrypted? Is that according to some standard? > > Nothing is mentioned about that. > > > > I gather that the encrypted stream is fed to the codec as usual (i.e. just put it > > in the output buffer and queue it to the codec), nothing special is needed for that. > > Except, how does the hardware know it is encrypted? I guess that's where the > > control comes in, you have to turn on SVP mode first. > > Decryption takes place before the decoder. I suspect there is no dedicated > driver for that, the TEE driver API is similar to smart card API and fits well > this task. So the decrytor consume normal memory that is encrypted and is only > allowed to decrypt into secure memory. All this is happening before the decoder, > so is out of scope for this patchset. > > Just a correction :-D. > > > > > For the capture buffers you need to provide buffers from secure/trusted memory. > > That's a dmabuf fd, but where does that come from? > > > > I saw this message: > > > > https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u > > > > so I expect that's where it comes from. But I agree that getting this from dma-heaps > > seems more natural. > > > > I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') > > > > For actually displaying these secure buffers you would use drm, and I assume that > > the hardware would mix in the contents of the secure buffer into the video output > > pipeline? I.e., the actual contents remain inaccessible. And that the video output > > (HDMI or DisplayPort) is using HDCP? > > > > > > > > > > > > > In any case, using a control to switch to secure mode and using a control > > > > to convert a dmabuf fd to a secure handle seems a poor choice to me. > > > > > > > > I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, > > > > e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that > > > > once you create buffers for the first time, the driver can switch into secure > > > > mode, and until all buffers are released again you know that the driver will > > > > stay in secure mode. > > > > > > Why do you think the control for setting secure mode is a poor choice? > > > There's various places in the driver code where functionality changes > > > based on being secure/non-secure mode, so this is very much a 'global' > > > setting for the driver. It could be inferred based off a new memory > > > type for the queues...which then sets that flag in the driver; but > > > that seems like it would be more fragile and would require checking > > > for incompatible output/capture memory types. I'm not against another > > > way of doing this; but didn't see why you think the proposed method is > > > a poor choice. > > > > I assume you are either decoding to secure memory all the time, or not > > at all. That's something you would want to select the moment you allocate > > the first buffer. Using the V4L2_MEMORY_ value would be the natural place > > for that. A control can typically be toggled at any time, and it makes > > no sense to do that for secure streaming. > > > > Related to that: if you pass a dmabuf fd you will need to check somewhere > > if the fd points to secure memory or not. You don't want to mix the two > > but you want to check that at VIDIOC_QBUF time. > > > > Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, > > drivers do not need to do that. > > Just to clarify a bit, and make sure I understand this too. You are proposing to > introduce something like: > > V4L2_MEMORY_SECURE_DMABUF > > Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the > driver that the memory is secure according to the definition of "secure" for the > platform its running on. > > This drivers also allocate secure SHM (a standard tee concept) and have internal > allocation for reconstruction buffer and some hw specific reference metadata. So > the idea would be that it would keep allocation using the dmabuf heap internal > APIs ? And decide which type of memory based on the memory type found in the > queue? > > Stepping back a little, why can't we have a way for drivers to detect that > dmabuf are secure ? I'm wondering if its actually useful to impose to all > userspace component to know that a dmabuf is secure ? > > Also, regarding MTK, these are stateless decoders. I think it would be nice to > show use example code that can properly parse the un-encrypted header, pass the > data to the decryptor and decode. There is a bit of mechanic in there that lacks > clarification, a reference implementation would clearly help. Finally, does this > platform offers some clearkey implementation (or other alternative) so we can do > validation and regression testing? It would be very unfortunate to add feature > upstream that can only be tested by proprietary CDM software. > > Nicolas Here's some links to the current userspace implementation built on top of the MTK patches (and yeah, this'll end up changing based on what happens upstream). 1. This is where we are decrypting the video to a secure buffer, it's invoking IPC into a closed source component to do that: https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/decoder_buffer_transcryptor.cc;l=87 2. This is where we aren enabling secure mode: https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=412 3. This is where we are resolving secure buffers to secure handles: https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=535 (the allocation of the secure buffers is done in closed source CDM code, but it's just opening the dma-buf heap and issuing the ioctl to allocate it) 4. This is where we submit the secure buffers to the output queue: https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_queue.cc;l=816 (this is nothing special, since it's just passing in the fd) 5. For the capture queue, there's zero changes in Chrome V4L2 code for that...it's all transparent to user space that it's a secure surface that's being rendered to. We do allocate them w/ different flags via minigbm which happens here: https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/platform_video_frame_pool.cc;l=37 > > > > > > > > > > > > > > For converting the dmabuf fd into a secure handle: a new ioctl similar to > > > > VIDIOC_EXPBUF might be more suited for that. > > > > > > I actually think the best way for converting the dmabuf fd into a > > > secure handle would be another ioctl in the dma-heap driver...since > > > that's where the memory is actually allocated from. But this really > > > depends on upstream maintainers and what they are comfortable with. > > > > That feels like a more natural place of doing this. > > > > Regards, > > > > Hans > > > > > > > > > > > > > Note that I am the first to admit that I have no experience with secure > > > > video pipelines or optee-os, so I am looking at this purely from an uAPI > > > > perspective. > > > > > > > > Regards, > > > > > > > > Hans > > > > > > > > > > > > > > Best Regards, > > > > > Yunfei Dong > > > > > > Regards, > > > > > > > > > > > > Hans > > > > > > > > > > > > > > > > > > > > regards, > > > > > > > Nicolas > > > > > > > > > > > > > > p.s. you forgot to document your control in the RST doc, please do > > > > > > > > > > > > in following > > > > > > > release. > > > > > > > > > > > > > > > +ctx->is_svp_mode = ctrl->val; > > > > > > > > + > > > > > > > > +if (ctx->is_svp_mode) { > > > > > > > > +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > > > > > > > > +if (ret) > > > > > > > > +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > > > > > > > > +else > > > > > > > > +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > > > > > > > > > > > > > > val); > > > > > > > > +} > > > > > > > > +break; > > > > > > > > default: > > > > > > > > mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > > > > > > > > 0x%x\n", > > > > > > > > > > > > hdr_ctrl->id); > > > > > > > > return ret; > > > > > > > > @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > > > > > > > > > > > mtk_vcodec_dec_ctx *ctx) > > > > > > > > unsigned int i; > > > > > > > > struct v4l2_ctrl *ctrl; > > > > > > > > > > > > > > > > -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > > > > > > > > +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > > > > > > > > if (ctx->ctrl_hdl.error) { > > > > > > > > mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > > > > > > > > return ctx->ctrl_hdl.error; > > > > > > > > @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > > > > > > > > > > > > mtk_vcodec_dec_ctx *ctx) > > > > > > > > > > > > > > > > ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > > > > > > > > > > > &mtk_vcodec_dec_ctrl_ops, > > > > > > > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > > > > > > > > +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > > > > > > > > > > > > &mtk_vcodec_dec_ctrl_ops, > > > > > > > > + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > > > > > > > > > > > > > > > > v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > > > > > > > > > > > > > > > > diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > > > > > > b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > > index d8cf01f76aab..a507045a3f30 100644 > > > > > > > > --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > > +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > > > > > > > > @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > > > > > > > > case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > > > > > > > > "Reference > > > > > > > > > > > > Frames for a P-Frame"; > > > > > > > > case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > > > > > > > > > > > > SPS and PPS to IDR"; > > > > > > > > case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > > > > > > > > Decoder > > > > > > > > > > > > get secure handle"; > > > > > > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > > > > > > > > > > > > set secure mode"; > > > > > > > > > > > > > > > > /* AV1 controls */ > > > > > > > > case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > > > > > > > > @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > > > > > > > > > > > > **name, enum v4l2_ctrl_type *type, > > > > > > > > *type = V4L2_CTRL_TYPE_INTEGER; > > > > > > > > *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > > > > > > break; > > > > > > > > +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > > > > > > > > +*type = V4L2_CTRL_TYPE_INTEGER; > > > > > > > > +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > > > > > > > > +break; > > > > > > > > case V4L2_CID_USER_CLASS: > > > > > > > > case V4L2_CID_CAMERA_CLASS: > > > > > > > > case V4L2_CID_CODEC_CLASS: > > > > > > > > diff --git a/include/uapi/linux/v4l2-controls.h > > > > > > > > > > > > b/include/uapi/linux/v4l2-controls.h > > > > > > > > index 7b3694985366..88e90d943e38 100644 > > > > > > > > --- a/include/uapi/linux/v4l2-controls.h > > > > > > > > +++ b/include/uapi/linux/v4l2-controls.h > > > > > > > > @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > > > > > > > > /* MPEG-class control IDs specific to the MediaTek Decoder > > > > > > > > > > > > driver as defined by V4L2 */ > > > > > > > > #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > > > > > > > > #define > > > > > > > > > > > > V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > > > > > > > > +#define > > > > > > > > > > > > V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > > > > > > > > > > > > > > > > /* Camera class control IDs */ > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > linux-arm-kernel mailing list > > > > linux-arm-kernel@lists.infradead.org > > > > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel > > >
On 19/09/2023 21:49, Jeffrey Kardatzke wrote: > On Tue, Sep 19, 2023 at 11:51 AM Nicolas Dufresne > <nicolas.dufresne@collabora.com> wrote: >> >> Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : >>> On 18/09/2023 22:57, Jeffrey Kardatzke wrote: >>>> On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >>>>> >>>>> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: >>>>>> Hi Hans & Nicolas, >>>>>> >>>>>> Thanks for your advice. >>>>>> >>>>>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: >>>>>>> >>>>>>> External email : Please do not click links or open attachments until >>>>>>> you have verified the sender or the content. >>>>>>> Hi, >>>>>>> >>>>>>> On 9/11/23 17:54, Nicolas Dufresne wrote: >>>>>>>> Hi, >>>>>>>> >>>>>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : >>>>>>>>> Setting secure mode flag to kernel when trying to play secure >>>>>>> >>>>>>> video, >>>>>>>>> then decoder driver will initialize tee related interface to >>>>>>> >>>>>>> support >>>>>>>>> svp. >>>>>>>> >>>>>>>> >>>>>>>> This is not what the patch is doing, please rework. This patch is >>>>>>> >>>>>>> an vendor API >>>>>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should >>>>>>> >>>>>>> not have to >>>>>>>> read your patch to understand this. >>>>>>>> >>>>>>>>> >>>>>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> >>>>>>>>> --- >>>>>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 >>>>>>> >>>>>>> ++++++++++++++- >>>>>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ >>>>>>>>> include/uapi/linux/v4l2-controls.h | 1 + >>>>>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) >>>>>>>>> >>>>>>>>> diff --git >>>>>>> >>>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>> less.c >>>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>> less.c >>>>>>>>> index d2b09ce9f1cf..a981178c25d9 100644 >>>>>>>>> --- >>>>>>> >>>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>> less.c >>>>>>>>> +++ >>>>>>> >>>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>> less.c >>>>>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl >>>>>>> >>>>>>> *ctrl) >>>>>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); >>>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", >>>>>>> >>>>>>> sec_fd, ctrl->val); >>>>>>>>> break; >>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>>>> >>>>>>>> Stepping back a little and focusing on the API, what makes your >>>>>>> >>>>>>> driver so >>>>>>>> special that it should be the only one having a "secure mode" ? We >>>>>>> >>>>>>> are touching >>>>>>>> in gap in the media pipeline in Linux, and this should come with >>>>>>> >>>>>>> consideration >>>>>>>> of the global API. >>>>>>>> >>>>>>>> Why is this API better then let's say Google Android one, were they >>>>>>> >>>>>>> expose 2 >>>>>>>> device nodes in their fork of the MFC driver (a secure and a non >>>>>>> >>>>>>> secure one) ? >>>>>>> >>>>>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal >>>>>>> on how to >>>>>>> handle secure video. I suspect this isn't mediatek specific, other >>>>>>> SoCs with >>>>>>> tee support could use this as well. >>>>>>> >>>>>>> As Nicolas said, it's long known to be a gap in our media support, so >>>>>>> it is >>>>>>> really great that you started work on this, but you need to look at >>>>>>> this from >>>>>>> a more generic point-of-view, and not mediatek-specific. >>>>>>> >>>>>> >>>>>> Whether your have any advice about how to do a more generic driver to >>>>>> handle secure video playback? >>>>>> >>>>>> There are several kind of buffer: output queue buffer/capture queue >>>>>> buffer/working buffer. >>>>>> >>>>>> output and capture queue buffer: user space will call tee related >>>>>> interface to allocate secure handle. Will convert to secure handle with >>>>>> v4l2 framework, then send secure handle to optee-os. >>>>>> >>>>>> working buffer: calling dma_heap and dma_buf to get secure memory >>>>>> handle, then covert secure iova in optee-os. >>>>>> >>>>>> Using the same kernel driver for svp and non-svp playback, just the >>>>>> buffer type are different. Normal is iova and secure is secure handle. >>>>>> >>>>>> User driver will tell the kernel driver with CID control whether the >>>>>> current playback is svp or non-svp. >>>>> >>>>> My understanding is that when you switch to secure mode, the driver makes >>>>> some optee calls to set everything up. And userspace needs a way convert a >>>>> dmabuf fd to a 'secure handle', which appears to be the DMA address of the >>>>> buffer. Who uses that handle? >>>> >>>> The only user space usage for getting the 'secure handle' from an fd >>>> is when that memory is written to. This is done when the TEE decrypts >>>> the video contents. User space sends the encrypted video + 'secure >>>> handle' to the TEE, and the TEE decrypts the contents to the memory >>>> associated with the 'secure handle'. Then the 'secure handle' is >>>> passed into the TEE again with the v4l2 driver to use as the source >>>> for video decoding (but w/ v4l2, user space is passing in fds). >>> >>> I think I need some more background. This series is to support a 'Secure Video >>> Processor' (at least, that's what svp stands for I believe, something that >>> is not mentioned anywhere in this series, BTW) which is used to decode an >>> encrypted h264 stream. >>> >>> First question: how is that stream encrypted? Is that according to some standard? >>> Nothing is mentioned about that. >>> >>> I gather that the encrypted stream is fed to the codec as usual (i.e. just put it >>> in the output buffer and queue it to the codec), nothing special is needed for that. >>> Except, how does the hardware know it is encrypted? I guess that's where the >>> control comes in, you have to turn on SVP mode first. >> >> Decryption takes place before the decoder. I suspect there is no dedicated >> driver for that, the TEE driver API is similar to smart card API and fits well >> this task. So the decrytor consume normal memory that is encrypted and is only >> allowed to decrypt into secure memory. All this is happening before the decoder, >> so is out of scope for this patchset. >> >> Just a correction :-D. >> >>> >>> For the capture buffers you need to provide buffers from secure/trusted memory. >>> That's a dmabuf fd, but where does that come from? >>> >>> I saw this message: >>> >>> https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u >>> >>> so I expect that's where it comes from. But I agree that getting this from dma-heaps >>> seems more natural. >>> >>> I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') >>> >>> For actually displaying these secure buffers you would use drm, and I assume that >>> the hardware would mix in the contents of the secure buffer into the video output >>> pipeline? I.e., the actual contents remain inaccessible. And that the video output >>> (HDMI or DisplayPort) is using HDCP? >>> >>>> >>>>> >>>>> In any case, using a control to switch to secure mode and using a control >>>>> to convert a dmabuf fd to a secure handle seems a poor choice to me. >>>>> >>>>> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, >>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that >>>>> once you create buffers for the first time, the driver can switch into secure >>>>> mode, and until all buffers are released again you know that the driver will >>>>> stay in secure mode. >>>> >>>> Why do you think the control for setting secure mode is a poor choice? >>>> There's various places in the driver code where functionality changes >>>> based on being secure/non-secure mode, so this is very much a 'global' >>>> setting for the driver. It could be inferred based off a new memory >>>> type for the queues...which then sets that flag in the driver; but >>>> that seems like it would be more fragile and would require checking >>>> for incompatible output/capture memory types. I'm not against another >>>> way of doing this; but didn't see why you think the proposed method is >>>> a poor choice. >>> >>> I assume you are either decoding to secure memory all the time, or not >>> at all. That's something you would want to select the moment you allocate >>> the first buffer. Using the V4L2_MEMORY_ value would be the natural place >>> for that. A control can typically be toggled at any time, and it makes >>> no sense to do that for secure streaming. >>> >>> Related to that: if you pass a dmabuf fd you will need to check somewhere >>> if the fd points to secure memory or not. You don't want to mix the two >>> but you want to check that at VIDIOC_QBUF time. >>> >>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, >>> drivers do not need to do that. >> >> Just to clarify a bit, and make sure I understand this too. You are proposing to >> introduce something like: >> >> V4L2_MEMORY_SECURE_DMABUF >> >> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the >> driver that the memory is secure according to the definition of "secure" for the >> platform its running on. >> >> This drivers also allocate secure SHM (a standard tee concept) and have internal >> allocation for reconstruction buffer and some hw specific reference metadata. So >> the idea would be that it would keep allocation using the dmabuf heap internal >> APIs ? And decide which type of memory based on the memory type found in the >> queue? >> >> Stepping back a little, why can't we have a way for drivers to detect that >> dmabuf are secure ? I'm wondering if its actually useful to impose to all >> userspace component to know that a dmabuf is secure ? >> >> Also, regarding MTK, these are stateless decoders. I think it would be nice to >> show use example code that can properly parse the un-encrypted header, pass the >> data to the decryptor and decode. There is a bit of mechanic in there that lacks >> clarification, a reference implementation would clearly help. Finally, does this >> platform offers some clearkey implementation (or other alternative) so we can do >> validation and regression testing? It would be very unfortunate to add feature >> upstream that can only be tested by proprietary CDM software. >> >> Nicolas > > > Here's some links to the current userspace implementation built on top > of the MTK patches (and yeah, this'll end up changing based on what > happens upstream). > > 1. This is where we are decrypting the video to a secure buffer, it's > invoking IPC into a closed source component to do that: > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/decoder_buffer_transcryptor.cc;l=87 So the encrypted compressed stream (contained in a regular non-secure buffer) is decrypted here into secure buffers. Correct? The hardware codec will just operate on those secure buffers, both for the output and capture queues, right? And no decryption/encryption takes place, it is all operating on unencrypted secure buffers, right? Or is the plan to include the decryption step in the driver? But who encrypted the compressed stream? Is it encrypted according to some standard? Or it is mediatek specific? Regards, Hans > 2. This is where we aren enabling secure mode: > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=412 > 3. This is where we are resolving secure buffers to secure handles: > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=535 > (the allocation of the secure buffers is done in closed source CDM > code, but it's just opening the dma-buf heap and issuing the ioctl to > allocate it) > 4. This is where we submit the secure buffers to the output queue: > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_queue.cc;l=816 > (this is nothing special, since it's just passing in the fd) > 5. For the capture queue, there's zero changes in Chrome V4L2 code for > that...it's all transparent to user space that it's a secure surface > that's being rendered to. We do allocate them w/ different flags via > minigbm which happens here: > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/platform_video_frame_pool.cc;l=37 > >> >>> >>>> >>>>> >>>>> For converting the dmabuf fd into a secure handle: a new ioctl similar to >>>>> VIDIOC_EXPBUF might be more suited for that. >>>> >>>> I actually think the best way for converting the dmabuf fd into a >>>> secure handle would be another ioctl in the dma-heap driver...since >>>> that's where the memory is actually allocated from. But this really >>>> depends on upstream maintainers and what they are comfortable with. >>> >>> That feels like a more natural place of doing this. >>> >>> Regards, >>> >>> Hans >>> >>>> >>>>> >>>>> Note that I am the first to admit that I have no experience with secure >>>>> video pipelines or optee-os, so I am looking at this purely from an uAPI >>>>> perspective. >>>>> >>>>> Regards, >>>>> >>>>> Hans >>>>> >>>>>> >>>>>> Best Regards, >>>>>> Yunfei Dong >>>>>>> Regards, >>>>>>> >>>>>>> Hans >>>>>>> >>>>>>>> >>>>>>>> regards, >>>>>>>> Nicolas >>>>>>>> >>>>>>>> p.s. you forgot to document your control in the RST doc, please do >>>>>>> >>>>>>> in following >>>>>>>> release. >>>>>>>> >>>>>>>>> +ctx->is_svp_mode = ctrl->val; >>>>>>>>> + >>>>>>>>> +if (ctx->is_svp_mode) { >>>>>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); >>>>>>>>> +if (ret) >>>>>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); >>>>>>>>> +else >>>>>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- >>>>>>>> >>>>>>>> val); >>>>>>>>> +} >>>>>>>>> +break; >>>>>>>>> default: >>>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: >>>>>>>>> 0x%x\n", >>>>>>> >>>>>>> hdr_ctrl->id); >>>>>>>>> return ret; >>>>>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>>>>> >>>>>>> mtk_vcodec_dec_ctx *ctx) >>>>>>>>> unsigned int i; >>>>>>>>> struct v4l2_ctrl *ctrl; >>>>>>>>> >>>>>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); >>>>>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); >>>>>>>>> if (ctx->ctrl_hdl.error) { >>>>>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); >>>>>>>>> return ctx->ctrl_hdl.error; >>>>>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>>>>> >>>>>>> mtk_vcodec_dec_ctx *ctx) >>>>>>>>> >>>>>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>>>>> >>>>>>> &mtk_vcodec_dec_ctrl_ops, >>>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); >>>>>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>>>>> >>>>>>> &mtk_vcodec_dec_ctrl_ops, >>>>>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); >>>>>>>>> >>>>>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); >>>>>>>>> >>>>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>> >>>>>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>> index d8cf01f76aab..a507045a3f30 100644 >>>>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) >>>>>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return >>>>>>>>> "Reference >>>>>>> >>>>>>> Frames for a P-Frame"; >>>>>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend >>>>>>> >>>>>>> SPS and PPS to IDR"; >>>>>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek >>>>>>>>> Decoder >>>>>>> >>>>>>> get secure handle"; >>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder >>>>>>> >>>>>>> set secure mode"; >>>>>>>>> >>>>>>>>> /* AV1 controls */ >>>>>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; >>>>>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char >>>>>>> >>>>>>> **name, enum v4l2_ctrl_type *type, >>>>>>>>> *type = V4L2_CTRL_TYPE_INTEGER; >>>>>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>>>>> break; >>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; >>>>>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>>>>> +break; >>>>>>>>> case V4L2_CID_USER_CLASS: >>>>>>>>> case V4L2_CID_CAMERA_CLASS: >>>>>>>>> case V4L2_CID_CODEC_CLASS: >>>>>>>>> diff --git a/include/uapi/linux/v4l2-controls.h >>>>>>> >>>>>>> b/include/uapi/linux/v4l2-controls.h >>>>>>>>> index 7b3694985366..88e90d943e38 100644 >>>>>>>>> --- a/include/uapi/linux/v4l2-controls.h >>>>>>>>> +++ b/include/uapi/linux/v4l2-controls.h >>>>>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { >>>>>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder >>>>>>> >>>>>>> driver as defined by V4L2 */ >>>>>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) >>>>>>>>> #define >>>>>>> >>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) >>>>>>>>> +#define >>>>>>> >>>>>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) >>>>>>>>> >>>>>>>>> /* Camera class control IDs */ >>>>>>>>> >>>>> >>>>> >>>>> _______________________________________________ >>>>> linux-arm-kernel mailing list >>>>> linux-arm-kernel@lists.infradead.org >>>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >>> >>
On 19/09/2023 20:51, Nicolas Dufresne wrote: > Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : >> On 18/09/2023 22:57, Jeffrey Kardatzke wrote: >>> On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >>>> >>>> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: >>>>> Hi Hans & Nicolas, >>>>> >>>>> Thanks for your advice. >>>>> >>>>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: >>>>>> >>>>>> External email : Please do not click links or open attachments until >>>>>> you have verified the sender or the content. >>>>>> Hi, >>>>>> >>>>>> On 9/11/23 17:54, Nicolas Dufresne wrote: >>>>>>> Hi, >>>>>>> >>>>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : >>>>>>>> Setting secure mode flag to kernel when trying to play secure >>>>>> >>>>>> video, >>>>>>>> then decoder driver will initialize tee related interface to >>>>>> >>>>>> support >>>>>>>> svp. >>>>>>> >>>>>>> >>>>>>> This is not what the patch is doing, please rework. This patch is >>>>>> >>>>>> an vendor API >>>>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should >>>>>> >>>>>> not have to >>>>>>> read your patch to understand this. >>>>>>> >>>>>>>> >>>>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> >>>>>>>> --- >>>>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 >>>>>> >>>>>> ++++++++++++++- >>>>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ >>>>>>>> include/uapi/linux/v4l2-controls.h | 1 + >>>>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) >>>>>>>> >>>>>>>> diff --git >>>>>> >>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>> less.c >>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>> less.c >>>>>>>> index d2b09ce9f1cf..a981178c25d9 100644 >>>>>>>> --- >>>>>> >>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>> less.c >>>>>>>> +++ >>>>>> >>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>> less.c >>>>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl >>>>>> >>>>>> *ctrl) >>>>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); >>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", >>>>>> >>>>>> sec_fd, ctrl->val); >>>>>>>> break; >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>>> >>>>>>> Stepping back a little and focusing on the API, what makes your >>>>>> >>>>>> driver so >>>>>>> special that it should be the only one having a "secure mode" ? We >>>>>> >>>>>> are touching >>>>>>> in gap in the media pipeline in Linux, and this should come with >>>>>> >>>>>> consideration >>>>>>> of the global API. >>>>>>> >>>>>>> Why is this API better then let's say Google Android one, were they >>>>>> >>>>>> expose 2 >>>>>>> device nodes in their fork of the MFC driver (a secure and a non >>>>>> >>>>>> secure one) ? >>>>>> >>>>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal >>>>>> on how to >>>>>> handle secure video. I suspect this isn't mediatek specific, other >>>>>> SoCs with >>>>>> tee support could use this as well. >>>>>> >>>>>> As Nicolas said, it's long known to be a gap in our media support, so >>>>>> it is >>>>>> really great that you started work on this, but you need to look at >>>>>> this from >>>>>> a more generic point-of-view, and not mediatek-specific. >>>>>> >>>>> >>>>> Whether your have any advice about how to do a more generic driver to >>>>> handle secure video playback? >>>>> >>>>> There are several kind of buffer: output queue buffer/capture queue >>>>> buffer/working buffer. >>>>> >>>>> output and capture queue buffer: user space will call tee related >>>>> interface to allocate secure handle. Will convert to secure handle with >>>>> v4l2 framework, then send secure handle to optee-os. >>>>> >>>>> working buffer: calling dma_heap and dma_buf to get secure memory >>>>> handle, then covert secure iova in optee-os. >>>>> >>>>> Using the same kernel driver for svp and non-svp playback, just the >>>>> buffer type are different. Normal is iova and secure is secure handle. >>>>> >>>>> User driver will tell the kernel driver with CID control whether the >>>>> current playback is svp or non-svp. >>>> >>>> My understanding is that when you switch to secure mode, the driver makes >>>> some optee calls to set everything up. And userspace needs a way convert a >>>> dmabuf fd to a 'secure handle', which appears to be the DMA address of the >>>> buffer. Who uses that handle? >>> >>> The only user space usage for getting the 'secure handle' from an fd >>> is when that memory is written to. This is done when the TEE decrypts >>> the video contents. User space sends the encrypted video + 'secure >>> handle' to the TEE, and the TEE decrypts the contents to the memory >>> associated with the 'secure handle'. Then the 'secure handle' is >>> passed into the TEE again with the v4l2 driver to use as the source >>> for video decoding (but w/ v4l2, user space is passing in fds). >> >> I think I need some more background. This series is to support a 'Secure Video >> Processor' (at least, that's what svp stands for I believe, something that >> is not mentioned anywhere in this series, BTW) which is used to decode an >> encrypted h264 stream. >> >> First question: how is that stream encrypted? Is that according to some standard? >> Nothing is mentioned about that. >> >> I gather that the encrypted stream is fed to the codec as usual (i.e. just put it >> in the output buffer and queue it to the codec), nothing special is needed for that. >> Except, how does the hardware know it is encrypted? I guess that's where the >> control comes in, you have to turn on SVP mode first. > > Decryption takes place before the decoder. I suspect there is no dedicated > driver for that, the TEE driver API is similar to smart card API and fits well > this task. So the decrytor consume normal memory that is encrypted and is only > allowed to decrypt into secure memory. All this is happening before the decoder, > so is out of scope for this patchset. > > Just a correction :-D. > >> >> For the capture buffers you need to provide buffers from secure/trusted memory. >> That's a dmabuf fd, but where does that come from? >> >> I saw this message: >> >> https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u >> >> so I expect that's where it comes from. But I agree that getting this from dma-heaps >> seems more natural. >> >> I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') >> >> For actually displaying these secure buffers you would use drm, and I assume that >> the hardware would mix in the contents of the secure buffer into the video output >> pipeline? I.e., the actual contents remain inaccessible. And that the video output >> (HDMI or DisplayPort) is using HDCP? >> >>> >>>> >>>> In any case, using a control to switch to secure mode and using a control >>>> to convert a dmabuf fd to a secure handle seems a poor choice to me. >>>> >>>> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, >>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that >>>> once you create buffers for the first time, the driver can switch into secure >>>> mode, and until all buffers are released again you know that the driver will >>>> stay in secure mode. >>> >>> Why do you think the control for setting secure mode is a poor choice? >>> There's various places in the driver code where functionality changes >>> based on being secure/non-secure mode, so this is very much a 'global' >>> setting for the driver. It could be inferred based off a new memory >>> type for the queues...which then sets that flag in the driver; but >>> that seems like it would be more fragile and would require checking >>> for incompatible output/capture memory types. I'm not against another >>> way of doing this; but didn't see why you think the proposed method is >>> a poor choice. >> >> I assume you are either decoding to secure memory all the time, or not >> at all. That's something you would want to select the moment you allocate >> the first buffer. Using the V4L2_MEMORY_ value would be the natural place >> for that. A control can typically be toggled at any time, and it makes >> no sense to do that for secure streaming. >> >> Related to that: if you pass a dmabuf fd you will need to check somewhere >> if the fd points to secure memory or not. You don't want to mix the two >> but you want to check that at VIDIOC_QBUF time. >> >> Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, >> drivers do not need to do that. > > Just to clarify a bit, and make sure I understand this too. You are proposing to > introduce something like: > > V4L2_MEMORY_SECURE_DMABUF > > Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the > driver that the memory is secure according to the definition of "secure" for the > platform its running on. > > This drivers also allocate secure SHM (a standard tee concept) and have internal > allocation for reconstruction buffer and some hw specific reference metadata. So > the idea would be that it would keep allocation using the dmabuf heap internal > APIs ? And decide which type of memory based on the memory type found in the > queue? Yes. Once you request the first buffer you basically tell the driver whether it will operate in secure or non-secure mode, and that stays that way until all buffers are freed. I think that makes sense. If there is a need in the future to have V4L2 allocate the secure buffers, then a similar V4L2_MEMORY_MMAP_SECURE type can be added. I think using v4l2_memory to select secure or non-secure mode is logical and fits well with the V4L2 API. > Stepping back a little, why can't we have a way for drivers to detect that > dmabuf are secure ? I'm wondering if its actually useful to impose to all > userspace component to know that a dmabuf is secure ? I was wondering the same thing: there should be a simple way for drivers and userspace to check if a dmabuf fd is secure or not. That will certainly help the vb2 framework verify that you don't mix secure and non-secure dmabuf fds. > > Also, regarding MTK, these are stateless decoders. I think it would be nice to > show use example code that can properly parse the un-encrypted header, pass the > data to the decryptor and decode. There is a bit of mechanic in there that lacks > clarification, a reference implementation would clearly help. Finally, does this > platform offers some clearkey implementation (or other alternative) so we can do > validation and regression testing? It would be very unfortunate to add feature > upstream that can only be tested by proprietary CDM software. Good points. Hans > > Nicolas > >> >>> >>>> >>>> For converting the dmabuf fd into a secure handle: a new ioctl similar to >>>> VIDIOC_EXPBUF might be more suited for that. >>> >>> I actually think the best way for converting the dmabuf fd into a >>> secure handle would be another ioctl in the dma-heap driver...since >>> that's where the memory is actually allocated from. But this really >>> depends on upstream maintainers and what they are comfortable with. >> >> That feels like a more natural place of doing this. >> >> Regards, >> >> Hans >> >>> >>>> >>>> Note that I am the first to admit that I have no experience with secure >>>> video pipelines or optee-os, so I am looking at this purely from an uAPI >>>> perspective. >>>> >>>> Regards, >>>> >>>> Hans >>>> >>>>> >>>>> Best Regards, >>>>> Yunfei Dong >>>>>> Regards, >>>>>> >>>>>> Hans >>>>>> >>>>>>> >>>>>>> regards, >>>>>>> Nicolas >>>>>>> >>>>>>> p.s. you forgot to document your control in the RST doc, please do >>>>>> >>>>>> in following >>>>>>> release. >>>>>>> >>>>>>>> +ctx->is_svp_mode = ctrl->val; >>>>>>>> + >>>>>>>> +if (ctx->is_svp_mode) { >>>>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); >>>>>>>> +if (ret) >>>>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); >>>>>>>> +else >>>>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- >>>>>>> >>>>>>> val); >>>>>>>> +} >>>>>>>> +break; >>>>>>>> default: >>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: >>>>>>>> 0x%x\n", >>>>>> >>>>>> hdr_ctrl->id); >>>>>>>> return ret; >>>>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>>>> >>>>>> mtk_vcodec_dec_ctx *ctx) >>>>>>>> unsigned int i; >>>>>>>> struct v4l2_ctrl *ctrl; >>>>>>>> >>>>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); >>>>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); >>>>>>>> if (ctx->ctrl_hdl.error) { >>>>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); >>>>>>>> return ctx->ctrl_hdl.error; >>>>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>>>> >>>>>> mtk_vcodec_dec_ctx *ctx) >>>>>>>> >>>>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>>>> >>>>>> &mtk_vcodec_dec_ctrl_ops, >>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); >>>>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>>>> >>>>>> &mtk_vcodec_dec_ctrl_ops, >>>>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); >>>>>>>> >>>>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); >>>>>>>> >>>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>> >>>>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>> index d8cf01f76aab..a507045a3f30 100644 >>>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) >>>>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return >>>>>>>> "Reference >>>>>> >>>>>> Frames for a P-Frame"; >>>>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend >>>>>> >>>>>> SPS and PPS to IDR"; >>>>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek >>>>>>>> Decoder >>>>>> >>>>>> get secure handle"; >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder >>>>>> >>>>>> set secure mode"; >>>>>>>> >>>>>>>> /* AV1 controls */ >>>>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; >>>>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char >>>>>> >>>>>> **name, enum v4l2_ctrl_type *type, >>>>>>>> *type = V4L2_CTRL_TYPE_INTEGER; >>>>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>>>> break; >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; >>>>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>>>> +break; >>>>>>>> case V4L2_CID_USER_CLASS: >>>>>>>> case V4L2_CID_CAMERA_CLASS: >>>>>>>> case V4L2_CID_CODEC_CLASS: >>>>>>>> diff --git a/include/uapi/linux/v4l2-controls.h >>>>>> >>>>>> b/include/uapi/linux/v4l2-controls.h >>>>>>>> index 7b3694985366..88e90d943e38 100644 >>>>>>>> --- a/include/uapi/linux/v4l2-controls.h >>>>>>>> +++ b/include/uapi/linux/v4l2-controls.h >>>>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { >>>>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder >>>>>> >>>>>> driver as defined by V4L2 */ >>>>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) >>>>>>>> #define >>>>>> >>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) >>>>>>>> +#define >>>>>> >>>>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) >>>>>>>> >>>>>>>> /* Camera class control IDs */ >>>>>>>> >>>> >>>> >>>> _______________________________________________ >>>> linux-arm-kernel mailing list >>>> linux-arm-kernel@lists.infradead.org >>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >> >
On Wed, Sep 20, 2023 at 12:10 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 19/09/2023 21:49, Jeffrey Kardatzke wrote: > > On Tue, Sep 19, 2023 at 11:51 AM Nicolas Dufresne > > <nicolas.dufresne@collabora.com> wrote: > >> > >> Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : > >>> On 18/09/2023 22:57, Jeffrey Kardatzke wrote: > >>>> On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > >>>>> > >>>>> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > >>>>>> Hi Hans & Nicolas, > >>>>>> > >>>>>> Thanks for your advice. > >>>>>> > >>>>>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > >>>>>>> > >>>>>>> External email : Please do not click links or open attachments until > >>>>>>> you have verified the sender or the content. > >>>>>>> Hi, > >>>>>>> > >>>>>>> On 9/11/23 17:54, Nicolas Dufresne wrote: > >>>>>>>> Hi, > >>>>>>>> > >>>>>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > >>>>>>>>> Setting secure mode flag to kernel when trying to play secure > >>>>>>> > >>>>>>> video, > >>>>>>>>> then decoder driver will initialize tee related interface to > >>>>>>> > >>>>>>> support > >>>>>>>>> svp. > >>>>>>>> > >>>>>>>> > >>>>>>>> This is not what the patch is doing, please rework. This patch is > >>>>>>> > >>>>>>> an vendor API > >>>>>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > >>>>>>> > >>>>>>> not have to > >>>>>>>> read your patch to understand this. > >>>>>>>> > >>>>>>>>> > >>>>>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > >>>>>>>>> --- > >>>>>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > >>>>>>> > >>>>>>> ++++++++++++++- > >>>>>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > >>>>>>>>> include/uapi/linux/v4l2-controls.h | 1 + > >>>>>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) > >>>>>>>>> > >>>>>>>>> diff --git > >>>>>>> > >>>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>>> less.c > >>>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>>> less.c > >>>>>>>>> index d2b09ce9f1cf..a981178c25d9 100644 > >>>>>>>>> --- > >>>>>>> > >>>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>>> less.c > >>>>>>>>> +++ > >>>>>>> > >>>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>>> less.c > >>>>>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > >>>>>>> > >>>>>>> *ctrl) > >>>>>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > >>>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > >>>>>>> > >>>>>>> sec_fd, ctrl->val); > >>>>>>>>> break; > >>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>>>>> > >>>>>>>> Stepping back a little and focusing on the API, what makes your > >>>>>>> > >>>>>>> driver so > >>>>>>>> special that it should be the only one having a "secure mode" ? We > >>>>>>> > >>>>>>> are touching > >>>>>>>> in gap in the media pipeline in Linux, and this should come with > >>>>>>> > >>>>>>> consideration > >>>>>>>> of the global API. > >>>>>>>> > >>>>>>>> Why is this API better then let's say Google Android one, were they > >>>>>>> > >>>>>>> expose 2 > >>>>>>>> device nodes in their fork of the MFC driver (a secure and a non > >>>>>>> > >>>>>>> secure one) ? > >>>>>>> > >>>>>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal > >>>>>>> on how to > >>>>>>> handle secure video. I suspect this isn't mediatek specific, other > >>>>>>> SoCs with > >>>>>>> tee support could use this as well. > >>>>>>> > >>>>>>> As Nicolas said, it's long known to be a gap in our media support, so > >>>>>>> it is > >>>>>>> really great that you started work on this, but you need to look at > >>>>>>> this from > >>>>>>> a more generic point-of-view, and not mediatek-specific. > >>>>>>> > >>>>>> > >>>>>> Whether your have any advice about how to do a more generic driver to > >>>>>> handle secure video playback? > >>>>>> > >>>>>> There are several kind of buffer: output queue buffer/capture queue > >>>>>> buffer/working buffer. > >>>>>> > >>>>>> output and capture queue buffer: user space will call tee related > >>>>>> interface to allocate secure handle. Will convert to secure handle with > >>>>>> v4l2 framework, then send secure handle to optee-os. > >>>>>> > >>>>>> working buffer: calling dma_heap and dma_buf to get secure memory > >>>>>> handle, then covert secure iova in optee-os. > >>>>>> > >>>>>> Using the same kernel driver for svp and non-svp playback, just the > >>>>>> buffer type are different. Normal is iova and secure is secure handle. > >>>>>> > >>>>>> User driver will tell the kernel driver with CID control whether the > >>>>>> current playback is svp or non-svp. > >>>>> > >>>>> My understanding is that when you switch to secure mode, the driver makes > >>>>> some optee calls to set everything up. And userspace needs a way convert a > >>>>> dmabuf fd to a 'secure handle', which appears to be the DMA address of the > >>>>> buffer. Who uses that handle? > >>>> > >>>> The only user space usage for getting the 'secure handle' from an fd > >>>> is when that memory is written to. This is done when the TEE decrypts > >>>> the video contents. User space sends the encrypted video + 'secure > >>>> handle' to the TEE, and the TEE decrypts the contents to the memory > >>>> associated with the 'secure handle'. Then the 'secure handle' is > >>>> passed into the TEE again with the v4l2 driver to use as the source > >>>> for video decoding (but w/ v4l2, user space is passing in fds). > >>> > >>> I think I need some more background. This series is to support a 'Secure Video > >>> Processor' (at least, that's what svp stands for I believe, something that > >>> is not mentioned anywhere in this series, BTW) which is used to decode an > >>> encrypted h264 stream. > >>> > >>> First question: how is that stream encrypted? Is that according to some standard? > >>> Nothing is mentioned about that. > >>> > >>> I gather that the encrypted stream is fed to the codec as usual (i.e. just put it > >>> in the output buffer and queue it to the codec), nothing special is needed for that. > >>> Except, how does the hardware know it is encrypted? I guess that's where the > >>> control comes in, you have to turn on SVP mode first. > >> > >> Decryption takes place before the decoder. I suspect there is no dedicated > >> driver for that, the TEE driver API is similar to smart card API and fits well > >> this task. So the decrytor consume normal memory that is encrypted and is only > >> allowed to decrypt into secure memory. All this is happening before the decoder, > >> so is out of scope for this patchset. > >> > >> Just a correction :-D. > >> > >>> > >>> For the capture buffers you need to provide buffers from secure/trusted memory. > >>> That's a dmabuf fd, but where does that come from? > >>> > >>> I saw this message: > >>> > >>> https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u > >>> > >>> so I expect that's where it comes from. But I agree that getting this from dma-heaps > >>> seems more natural. > >>> > >>> I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') > >>> > >>> For actually displaying these secure buffers you would use drm, and I assume that > >>> the hardware would mix in the contents of the secure buffer into the video output > >>> pipeline? I.e., the actual contents remain inaccessible. And that the video output > >>> (HDMI or DisplayPort) is using HDCP? > >>> > >>>> > >>>>> > >>>>> In any case, using a control to switch to secure mode and using a control > >>>>> to convert a dmabuf fd to a secure handle seems a poor choice to me. > >>>>> > >>>>> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, > >>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that > >>>>> once you create buffers for the first time, the driver can switch into secure > >>>>> mode, and until all buffers are released again you know that the driver will > >>>>> stay in secure mode. > >>>> > >>>> Why do you think the control for setting secure mode is a poor choice? > >>>> There's various places in the driver code where functionality changes > >>>> based on being secure/non-secure mode, so this is very much a 'global' > >>>> setting for the driver. It could be inferred based off a new memory > >>>> type for the queues...which then sets that flag in the driver; but > >>>> that seems like it would be more fragile and would require checking > >>>> for incompatible output/capture memory types. I'm not against another > >>>> way of doing this; but didn't see why you think the proposed method is > >>>> a poor choice. > >>> > >>> I assume you are either decoding to secure memory all the time, or not > >>> at all. That's something you would want to select the moment you allocate > >>> the first buffer. Using the V4L2_MEMORY_ value would be the natural place > >>> for that. A control can typically be toggled at any time, and it makes > >>> no sense to do that for secure streaming. > >>> > >>> Related to that: if you pass a dmabuf fd you will need to check somewhere > >>> if the fd points to secure memory or not. You don't want to mix the two > >>> but you want to check that at VIDIOC_QBUF time. > >>> > >>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, > >>> drivers do not need to do that. > >> > >> Just to clarify a bit, and make sure I understand this too. You are proposing to > >> introduce something like: > >> > >> V4L2_MEMORY_SECURE_DMABUF > >> > >> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the > >> driver that the memory is secure according to the definition of "secure" for the > >> platform its running on. > >> > >> This drivers also allocate secure SHM (a standard tee concept) and have internal > >> allocation for reconstruction buffer and some hw specific reference metadata. So > >> the idea would be that it would keep allocation using the dmabuf heap internal > >> APIs ? And decide which type of memory based on the memory type found in the > >> queue? > >> > >> Stepping back a little, why can't we have a way for drivers to detect that > >> dmabuf are secure ? I'm wondering if its actually useful to impose to all > >> userspace component to know that a dmabuf is secure ? > >> > >> Also, regarding MTK, these are stateless decoders. I think it would be nice to > >> show use example code that can properly parse the un-encrypted header, pass the > >> data to the decryptor and decode. There is a bit of mechanic in there that lacks > >> clarification, a reference implementation would clearly help. Finally, does this > >> platform offers some clearkey implementation (or other alternative) so we can do > >> validation and regression testing? It would be very unfortunate to add feature > >> upstream that can only be tested by proprietary CDM software. > >> > >> Nicolas > > > > > > Here's some links to the current userspace implementation built on top > > of the MTK patches (and yeah, this'll end up changing based on what > > happens upstream). > > > > 1. This is where we are decrypting the video to a secure buffer, it's > > invoking IPC into a closed source component to do that: > > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/decoder_buffer_transcryptor.cc;l=87 > > So the encrypted compressed stream (contained in a regular non-secure buffer) > is decrypted here into secure buffers. Correct? Correct > > The hardware codec will just operate on those secure buffers, both for the > output and capture queues, right? And no decryption/encryption takes place, > it is all operating on unencrypted secure buffers, right? Correct > > Or is the plan to include the decryption step in the driver? No, the driver will never be doing the decryption. > > But who encrypted the compressed stream? Is it encrypted according to > some standard? Or it is mediatek specific? It's encrypted using CENC (Common Encryption). The method for acquiring the keys to perform the decryption is Widevine specific (Widevine is the Digital Rights Management system we are using...but nothing in the kernel patches dictates which Digital Rights Management system is used, but the encryption technique is a standard). > > Regards, > > Hans > > > 2. This is where we aren enabling secure mode: > > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=412 > > 3. This is where we are resolving secure buffers to secure handles: > > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=535 > > (the allocation of the secure buffers is done in closed source CDM > > code, but it's just opening the dma-buf heap and issuing the ioctl to > > allocate it) > > 4. This is where we submit the secure buffers to the output queue: > > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_queue.cc;l=816 > > (this is nothing special, since it's just passing in the fd) > > 5. For the capture queue, there's zero changes in Chrome V4L2 code for > > that...it's all transparent to user space that it's a secure surface > > that's being rendered to. We do allocate them w/ different flags via > > minigbm which happens here: > > https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/platform_video_frame_pool.cc;l=37 > > > >> > >>> > >>>> > >>>>> > >>>>> For converting the dmabuf fd into a secure handle: a new ioctl similar to > >>>>> VIDIOC_EXPBUF might be more suited for that. > >>>> > >>>> I actually think the best way for converting the dmabuf fd into a > >>>> secure handle would be another ioctl in the dma-heap driver...since > >>>> that's where the memory is actually allocated from. But this really > >>>> depends on upstream maintainers and what they are comfortable with. > >>> > >>> That feels like a more natural place of doing this. > >>> > >>> Regards, > >>> > >>> Hans > >>> > >>>> > >>>>> > >>>>> Note that I am the first to admit that I have no experience with secure > >>>>> video pipelines or optee-os, so I am looking at this purely from an uAPI > >>>>> perspective. > >>>>> > >>>>> Regards, > >>>>> > >>>>> Hans > >>>>> > >>>>>> > >>>>>> Best Regards, > >>>>>> Yunfei Dong > >>>>>>> Regards, > >>>>>>> > >>>>>>> Hans > >>>>>>> > >>>>>>>> > >>>>>>>> regards, > >>>>>>>> Nicolas > >>>>>>>> > >>>>>>>> p.s. you forgot to document your control in the RST doc, please do > >>>>>>> > >>>>>>> in following > >>>>>>>> release. > >>>>>>>> > >>>>>>>>> +ctx->is_svp_mode = ctrl->val; > >>>>>>>>> + > >>>>>>>>> +if (ctx->is_svp_mode) { > >>>>>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >>>>>>>>> +if (ret) > >>>>>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >>>>>>>>> +else > >>>>>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > >>>>>>>> > >>>>>>>> val); > >>>>>>>>> +} > >>>>>>>>> +break; > >>>>>>>>> default: > >>>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > >>>>>>>>> 0x%x\n", > >>>>>>> > >>>>>>> hdr_ctrl->id); > >>>>>>>>> return ret; > >>>>>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >>>>>>> > >>>>>>> mtk_vcodec_dec_ctx *ctx) > >>>>>>>>> unsigned int i; > >>>>>>>>> struct v4l2_ctrl *ctrl; > >>>>>>>>> > >>>>>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >>>>>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >>>>>>>>> if (ctx->ctrl_hdl.error) { > >>>>>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >>>>>>>>> return ctx->ctrl_hdl.error; > >>>>>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >>>>>>> > >>>>>>> mtk_vcodec_dec_ctx *ctx) > >>>>>>>>> > >>>>>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>>>>> > >>>>>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >>>>>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>>>>> > >>>>>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >>>>>>>>> > >>>>>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >>>>>>>>> > >>>>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>> > >>>>>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>>> index d8cf01f76aab..a507045a3f30 100644 > >>>>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >>>>>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > >>>>>>>>> "Reference > >>>>>>> > >>>>>>> Frames for a P-Frame"; > >>>>>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > >>>>>>> > >>>>>>> SPS and PPS to IDR"; > >>>>>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > >>>>>>>>> Decoder > >>>>>>> > >>>>>>> get secure handle"; > >>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > >>>>>>> > >>>>>>> set secure mode"; > >>>>>>>>> > >>>>>>>>> /* AV1 controls */ > >>>>>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >>>>>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > >>>>>>> > >>>>>>> **name, enum v4l2_ctrl_type *type, > >>>>>>>>> *type = V4L2_CTRL_TYPE_INTEGER; > >>>>>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>>>>> break; > >>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; > >>>>>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>>>>> +break; > >>>>>>>>> case V4L2_CID_USER_CLASS: > >>>>>>>>> case V4L2_CID_CAMERA_CLASS: > >>>>>>>>> case V4L2_CID_CODEC_CLASS: > >>>>>>>>> diff --git a/include/uapi/linux/v4l2-controls.h > >>>>>>> > >>>>>>> b/include/uapi/linux/v4l2-controls.h > >>>>>>>>> index 7b3694985366..88e90d943e38 100644 > >>>>>>>>> --- a/include/uapi/linux/v4l2-controls.h > >>>>>>>>> +++ b/include/uapi/linux/v4l2-controls.h > >>>>>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > >>>>>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder > >>>>>>> > >>>>>>> driver as defined by V4L2 */ > >>>>>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > >>>>>>>>> #define > >>>>>>> > >>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >>>>>>>>> +#define > >>>>>>> > >>>>>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >>>>>>>>> > >>>>>>>>> /* Camera class control IDs */ > >>>>>>>>> > >>>>> > >>>>> > >>>>> _______________________________________________ > >>>>> linux-arm-kernel mailing list > >>>>> linux-arm-kernel@lists.infradead.org > >>>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel > >>> > >> >
On Wed, Sep 20, 2023 at 12:21 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 19/09/2023 20:51, Nicolas Dufresne wrote: > > Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : > >> On 18/09/2023 22:57, Jeffrey Kardatzke wrote: > >>> On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > >>>> > >>>> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: > >>>>> Hi Hans & Nicolas, > >>>>> > >>>>> Thanks for your advice. > >>>>> > >>>>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: > >>>>>> > >>>>>> External email : Please do not click links or open attachments until > >>>>>> you have verified the sender or the content. > >>>>>> Hi, > >>>>>> > >>>>>> On 9/11/23 17:54, Nicolas Dufresne wrote: > >>>>>>> Hi, > >>>>>>> > >>>>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : > >>>>>>>> Setting secure mode flag to kernel when trying to play secure > >>>>>> > >>>>>> video, > >>>>>>>> then decoder driver will initialize tee related interface to > >>>>>> > >>>>>> support > >>>>>>>> svp. > >>>>>>> > >>>>>>> > >>>>>>> This is not what the patch is doing, please rework. This patch is > >>>>>> > >>>>>> an vendor API > >>>>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should > >>>>>> > >>>>>> not have to > >>>>>>> read your patch to understand this. > >>>>>>> > >>>>>>>> > >>>>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> > >>>>>>>> --- > >>>>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 > >>>>>> > >>>>>> ++++++++++++++- > >>>>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ > >>>>>>>> include/uapi/linux/v4l2-controls.h | 1 + > >>>>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) > >>>>>>>> > >>>>>>>> diff --git > >>>>>> > >>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>> less.c > >>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>> less.c > >>>>>>>> index d2b09ce9f1cf..a981178c25d9 100644 > >>>>>>>> --- > >>>>>> > >>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>> less.c > >>>>>>>> +++ > >>>>>> > >>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state > >>>>>> less.c > >>>>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl > >>>>>> > >>>>>> *ctrl) > >>>>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); > >>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", > >>>>>> > >>>>>> sec_fd, ctrl->val); > >>>>>>>> break; > >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>>>> > >>>>>>> Stepping back a little and focusing on the API, what makes your > >>>>>> > >>>>>> driver so > >>>>>>> special that it should be the only one having a "secure mode" ? We > >>>>>> > >>>>>> are touching > >>>>>>> in gap in the media pipeline in Linux, and this should come with > >>>>>> > >>>>>> consideration > >>>>>>> of the global API. > >>>>>>> > >>>>>>> Why is this API better then let's say Google Android one, were they > >>>>>> > >>>>>> expose 2 > >>>>>>> device nodes in their fork of the MFC driver (a secure and a non > >>>>>> > >>>>>> secure one) ? > >>>>>> > >>>>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal > >>>>>> on how to > >>>>>> handle secure video. I suspect this isn't mediatek specific, other > >>>>>> SoCs with > >>>>>> tee support could use this as well. > >>>>>> > >>>>>> As Nicolas said, it's long known to be a gap in our media support, so > >>>>>> it is > >>>>>> really great that you started work on this, but you need to look at > >>>>>> this from > >>>>>> a more generic point-of-view, and not mediatek-specific. > >>>>>> > >>>>> > >>>>> Whether your have any advice about how to do a more generic driver to > >>>>> handle secure video playback? > >>>>> > >>>>> There are several kind of buffer: output queue buffer/capture queue > >>>>> buffer/working buffer. > >>>>> > >>>>> output and capture queue buffer: user space will call tee related > >>>>> interface to allocate secure handle. Will convert to secure handle with > >>>>> v4l2 framework, then send secure handle to optee-os. > >>>>> > >>>>> working buffer: calling dma_heap and dma_buf to get secure memory > >>>>> handle, then covert secure iova in optee-os. > >>>>> > >>>>> Using the same kernel driver for svp and non-svp playback, just the > >>>>> buffer type are different. Normal is iova and secure is secure handle. > >>>>> > >>>>> User driver will tell the kernel driver with CID control whether the > >>>>> current playback is svp or non-svp. > >>>> > >>>> My understanding is that when you switch to secure mode, the driver makes > >>>> some optee calls to set everything up. And userspace needs a way convert a > >>>> dmabuf fd to a 'secure handle', which appears to be the DMA address of the > >>>> buffer. Who uses that handle? > >>> > >>> The only user space usage for getting the 'secure handle' from an fd > >>> is when that memory is written to. This is done when the TEE decrypts > >>> the video contents. User space sends the encrypted video + 'secure > >>> handle' to the TEE, and the TEE decrypts the contents to the memory > >>> associated with the 'secure handle'. Then the 'secure handle' is > >>> passed into the TEE again with the v4l2 driver to use as the source > >>> for video decoding (but w/ v4l2, user space is passing in fds). > >> > >> I think I need some more background. This series is to support a 'Secure Video > >> Processor' (at least, that's what svp stands for I believe, something that > >> is not mentioned anywhere in this series, BTW) which is used to decode an > >> encrypted h264 stream. > >> > >> First question: how is that stream encrypted? Is that according to some standard? > >> Nothing is mentioned about that. > >> > >> I gather that the encrypted stream is fed to the codec as usual (i.e. just put it > >> in the output buffer and queue it to the codec), nothing special is needed for that. > >> Except, how does the hardware know it is encrypted? I guess that's where the > >> control comes in, you have to turn on SVP mode first. > > > > Decryption takes place before the decoder. I suspect there is no dedicated > > driver for that, the TEE driver API is similar to smart card API and fits well > > this task. So the decrytor consume normal memory that is encrypted and is only > > allowed to decrypt into secure memory. All this is happening before the decoder, > > so is out of scope for this patchset. > > > > Just a correction :-D. > > > >> > >> For the capture buffers you need to provide buffers from secure/trusted memory. > >> That's a dmabuf fd, but where does that come from? > >> > >> I saw this message: > >> > >> https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u > >> > >> so I expect that's where it comes from. But I agree that getting this from dma-heaps > >> seems more natural. > >> > >> I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') > >> > >> For actually displaying these secure buffers you would use drm, and I assume that > >> the hardware would mix in the contents of the secure buffer into the video output > >> pipeline? I.e., the actual contents remain inaccessible. And that the video output > >> (HDMI or DisplayPort) is using HDCP? > >> > >>> > >>>> > >>>> In any case, using a control to switch to secure mode and using a control > >>>> to convert a dmabuf fd to a secure handle seems a poor choice to me. > >>>> > >>>> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, > >>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that > >>>> once you create buffers for the first time, the driver can switch into secure > >>>> mode, and until all buffers are released again you know that the driver will > >>>> stay in secure mode. > >>> > >>> Why do you think the control for setting secure mode is a poor choice? > >>> There's various places in the driver code where functionality changes > >>> based on being secure/non-secure mode, so this is very much a 'global' > >>> setting for the driver. It could be inferred based off a new memory > >>> type for the queues...which then sets that flag in the driver; but > >>> that seems like it would be more fragile and would require checking > >>> for incompatible output/capture memory types. I'm not against another > >>> way of doing this; but didn't see why you think the proposed method is > >>> a poor choice. > >> > >> I assume you are either decoding to secure memory all the time, or not > >> at all. That's something you would want to select the moment you allocate > >> the first buffer. Using the V4L2_MEMORY_ value would be the natural place > >> for that. A control can typically be toggled at any time, and it makes > >> no sense to do that for secure streaming. > >> > >> Related to that: if you pass a dmabuf fd you will need to check somewhere > >> if the fd points to secure memory or not. You don't want to mix the two > >> but you want to check that at VIDIOC_QBUF time. > >> > >> Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, > >> drivers do not need to do that. > > > > Just to clarify a bit, and make sure I understand this too. You are proposing to > > introduce something like: > > > > V4L2_MEMORY_SECURE_DMABUF > > > > Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the > > driver that the memory is secure according to the definition of "secure" for the > > platform its running on. > > > > This drivers also allocate secure SHM (a standard tee concept) and have internal > > allocation for reconstruction buffer and some hw specific reference metadata. So > > the idea would be that it would keep allocation using the dmabuf heap internal > > APIs ? And decide which type of memory based on the memory type found in the > > queue? > > Yes. Once you request the first buffer you basically tell the driver whether it > will operate in secure or non-secure mode, and that stays that way until all > buffers are freed. I think that makes sense. > > If there is a need in the future to have V4L2 allocate the secure buffers, then > a similar V4L2_MEMORY_MMAP_SECURE type can be added. I think using v4l2_memory > to select secure or non-secure mode is logical and fits well with the V4L2 API. > OK, sounds good. I'll work with Mediatek to get the patches updated for that. > > Stepping back a little, why can't we have a way for drivers to detect that > > dmabuf are secure ? I'm wondering if its actually useful to impose to all > > userspace component to know that a dmabuf is secure ? > > I was wondering the same thing: there should be a simple way for drivers and > userspace to check if a dmabuf fd is secure or not. That will certainly help > the vb2 framework verify that you don't mix secure and non-secure dmabuf fds. > Already talked to Mediatek about this and they are working on updating the dma-buf patches for this. > > > > Also, regarding MTK, these are stateless decoders. I think it would be nice to > > show use example code that can properly parse the un-encrypted header, pass the > > data to the decryptor and decode. There is a bit of mechanic in there that lacks > > clarification, a reference implementation would clearly help. Finally, does this > > platform offers some clearkey implementation (or other alternative) so we can do > > validation and regression testing? It would be very unfortunate to add feature > > upstream that can only be tested by proprietary CDM software. > It would be possible to use this with clearkey w/ some additional work on our end. If this is then part of the public ChromiumOS build, would that be satisfactory? (the TEE would have some binary blob components like firmware does though) > Good points. > > Hans > > > > > Nicolas > > > >> > >>> > >>>> > >>>> For converting the dmabuf fd into a secure handle: a new ioctl similar to > >>>> VIDIOC_EXPBUF might be more suited for that. > >>> > >>> I actually think the best way for converting the dmabuf fd into a > >>> secure handle would be another ioctl in the dma-heap driver...since > >>> that's where the memory is actually allocated from. But this really > >>> depends on upstream maintainers and what they are comfortable with. > >> > >> That feels like a more natural place of doing this. > >> > >> Regards, > >> > >> Hans > >> > >>> > >>>> > >>>> Note that I am the first to admit that I have no experience with secure > >>>> video pipelines or optee-os, so I am looking at this purely from an uAPI > >>>> perspective. > >>>> > >>>> Regards, > >>>> > >>>> Hans > >>>> > >>>>> > >>>>> Best Regards, > >>>>> Yunfei Dong > >>>>>> Regards, > >>>>>> > >>>>>> Hans > >>>>>> > >>>>>>> > >>>>>>> regards, > >>>>>>> Nicolas > >>>>>>> > >>>>>>> p.s. you forgot to document your control in the RST doc, please do > >>>>>> > >>>>>> in following > >>>>>>> release. > >>>>>>> > >>>>>>>> +ctx->is_svp_mode = ctrl->val; > >>>>>>>> + > >>>>>>>> +if (ctx->is_svp_mode) { > >>>>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >>>>>>>> +if (ret) > >>>>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >>>>>>>> +else > >>>>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- > >>>>>>> > >>>>>>> val); > >>>>>>>> +} > >>>>>>>> +break; > >>>>>>>> default: > >>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > >>>>>>>> 0x%x\n", > >>>>>> > >>>>>> hdr_ctrl->id); > >>>>>>>> return ret; > >>>>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >>>>>> > >>>>>> mtk_vcodec_dec_ctx *ctx) > >>>>>>>> unsigned int i; > >>>>>>>> struct v4l2_ctrl *ctrl; > >>>>>>>> > >>>>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >>>>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >>>>>>>> if (ctx->ctrl_hdl.error) { > >>>>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >>>>>>>> return ctx->ctrl_hdl.error; > >>>>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct > >>>>>> > >>>>>> mtk_vcodec_dec_ctx *ctx) > >>>>>>>> > >>>>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>>>> > >>>>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >>>>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>>>> > >>>>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >>>>>>>> > >>>>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >>>>>>>> > >>>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>> > >>>>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>> index d8cf01f76aab..a507045a3f30 100644 > >>>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >>>>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > >>>>>>>> "Reference > >>>>>> > >>>>>> Frames for a P-Frame"; > >>>>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend > >>>>>> > >>>>>> SPS and PPS to IDR"; > >>>>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > >>>>>>>> Decoder > >>>>>> > >>>>>> get secure handle"; > >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder > >>>>>> > >>>>>> set secure mode"; > >>>>>>>> > >>>>>>>> /* AV1 controls */ > >>>>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >>>>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char > >>>>>> > >>>>>> **name, enum v4l2_ctrl_type *type, > >>>>>>>> *type = V4L2_CTRL_TYPE_INTEGER; > >>>>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>>>> break; > >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; > >>>>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>>>> +break; > >>>>>>>> case V4L2_CID_USER_CLASS: > >>>>>>>> case V4L2_CID_CAMERA_CLASS: > >>>>>>>> case V4L2_CID_CODEC_CLASS: > >>>>>>>> diff --git a/include/uapi/linux/v4l2-controls.h > >>>>>> > >>>>>> b/include/uapi/linux/v4l2-controls.h > >>>>>>>> index 7b3694985366..88e90d943e38 100644 > >>>>>>>> --- a/include/uapi/linux/v4l2-controls.h > >>>>>>>> +++ b/include/uapi/linux/v4l2-controls.h > >>>>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { > >>>>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder > >>>>>> > >>>>>> driver as defined by V4L2 */ > >>>>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) > >>>>>>>> #define > >>>>>> > >>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >>>>>>>> +#define > >>>>>> > >>>>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >>>>>>>> > >>>>>>>> /* Camera class control IDs */ > >>>>>>>> > >>>> > >>>> > >>>> _______________________________________________ > >>>> linux-arm-kernel mailing list > >>>> linux-arm-kernel@lists.infradead.org > >>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel > >> > > >
On 20/09/2023 20:13, Jeffrey Kardatzke wrote: > On Wed, Sep 20, 2023 at 12:10 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >> >> On 19/09/2023 21:49, Jeffrey Kardatzke wrote: >>> On Tue, Sep 19, 2023 at 11:51 AM Nicolas Dufresne >>> <nicolas.dufresne@collabora.com> wrote: >>>> >>>> Le mardi 19 septembre 2023 à 10:53 +0200, Hans Verkuil a écrit : >>>>> On 18/09/2023 22:57, Jeffrey Kardatzke wrote: >>>>>> On Fri, Sep 15, 2023 at 1:56 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >>>>>>> >>>>>>> On 15/09/2023 10:25, Yunfei Dong (董云飞) wrote: >>>>>>>> Hi Hans & Nicolas, >>>>>>>> >>>>>>>> Thanks for your advice. >>>>>>>> >>>>>>>> On Tue, 2023-09-12 at 11:30 +0200, Hans Verkuil wrote: >>>>>>>>> >>>>>>>>> External email : Please do not click links or open attachments until >>>>>>>>> you have verified the sender or the content. >>>>>>>>> Hi, >>>>>>>>> >>>>>>>>> On 9/11/23 17:54, Nicolas Dufresne wrote: >>>>>>>>>> Hi, >>>>>>>>>> >>>>>>>>>> Le lundi 11 septembre 2023 à 20:59 +0800, Yunfei Dong a écrit : >>>>>>>>>>> Setting secure mode flag to kernel when trying to play secure >>>>>>>>> >>>>>>>>> video, >>>>>>>>>>> then decoder driver will initialize tee related interface to >>>>>>>>> >>>>>>>>> support >>>>>>>>>>> svp. >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> This is not what the patch is doing, please rework. This patch is >>>>>>>>> >>>>>>>>> an vendor API >>>>>>>>>> addition introducing V4L2_CID_MPEG_MTK_SET_SECURE_MODE. I should >>>>>>>>> >>>>>>>>> not have to >>>>>>>>>> read your patch to understand this. >>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> >>>>>>>>>>> --- >>>>>>>>>>> .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 15 >>>>>>>>> >>>>>>>>> ++++++++++++++- >>>>>>>>>>> drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 +++++ >>>>>>>>>>> include/uapi/linux/v4l2-controls.h | 1 + >>>>>>>>>>> 3 files changed, 20 insertions(+), 1 deletion(-) >>>>>>>>>>> >>>>>>>>>>> diff --git >>>>>>>>> >>>>>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>>>> less.c >>>>>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>>>> less.c >>>>>>>>>>> index d2b09ce9f1cf..a981178c25d9 100644 >>>>>>>>>>> --- >>>>>>>>> >>>>>>>>> a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>>>> less.c >>>>>>>>>>> +++ >>>>>>>>> >>>>>>>>> b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_state >>>>>>>>> less.c >>>>>>>>>>> @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl >>>>>>>>> >>>>>>>>> *ctrl) >>>>>>>>>>> ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); >>>>>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", >>>>>>>>> >>>>>>>>> sec_fd, ctrl->val); >>>>>>>>>>> break; >>>>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>>>>>> >>>>>>>>>> Stepping back a little and focusing on the API, what makes your >>>>>>>>> >>>>>>>>> driver so >>>>>>>>>> special that it should be the only one having a "secure mode" ? We >>>>>>>>> >>>>>>>>> are touching >>>>>>>>>> in gap in the media pipeline in Linux, and this should come with >>>>>>>>> >>>>>>>>> consideration >>>>>>>>>> of the global API. >>>>>>>>>> >>>>>>>>>> Why is this API better then let's say Google Android one, were they >>>>>>>>> >>>>>>>>> expose 2 >>>>>>>>>> device nodes in their fork of the MFC driver (a secure and a non >>>>>>>>> >>>>>>>>> secure one) ? >>>>>>>>> >>>>>>>>> Perhaps it is a good idea to first post an RFC with an uAPI proposal >>>>>>>>> on how to >>>>>>>>> handle secure video. I suspect this isn't mediatek specific, other >>>>>>>>> SoCs with >>>>>>>>> tee support could use this as well. >>>>>>>>> >>>>>>>>> As Nicolas said, it's long known to be a gap in our media support, so >>>>>>>>> it is >>>>>>>>> really great that you started work on this, but you need to look at >>>>>>>>> this from >>>>>>>>> a more generic point-of-view, and not mediatek-specific. >>>>>>>>> >>>>>>>> >>>>>>>> Whether your have any advice about how to do a more generic driver to >>>>>>>> handle secure video playback? >>>>>>>> >>>>>>>> There are several kind of buffer: output queue buffer/capture queue >>>>>>>> buffer/working buffer. >>>>>>>> >>>>>>>> output and capture queue buffer: user space will call tee related >>>>>>>> interface to allocate secure handle. Will convert to secure handle with >>>>>>>> v4l2 framework, then send secure handle to optee-os. >>>>>>>> >>>>>>>> working buffer: calling dma_heap and dma_buf to get secure memory >>>>>>>> handle, then covert secure iova in optee-os. >>>>>>>> >>>>>>>> Using the same kernel driver for svp and non-svp playback, just the >>>>>>>> buffer type are different. Normal is iova and secure is secure handle. >>>>>>>> >>>>>>>> User driver will tell the kernel driver with CID control whether the >>>>>>>> current playback is svp or non-svp. >>>>>>> >>>>>>> My understanding is that when you switch to secure mode, the driver makes >>>>>>> some optee calls to set everything up. And userspace needs a way convert a >>>>>>> dmabuf fd to a 'secure handle', which appears to be the DMA address of the >>>>>>> buffer. Who uses that handle? >>>>>> >>>>>> The only user space usage for getting the 'secure handle' from an fd >>>>>> is when that memory is written to. This is done when the TEE decrypts >>>>>> the video contents. User space sends the encrypted video + 'secure >>>>>> handle' to the TEE, and the TEE decrypts the contents to the memory >>>>>> associated with the 'secure handle'. Then the 'secure handle' is >>>>>> passed into the TEE again with the v4l2 driver to use as the source >>>>>> for video decoding (but w/ v4l2, user space is passing in fds). >>>>> >>>>> I think I need some more background. This series is to support a 'Secure Video >>>>> Processor' (at least, that's what svp stands for I believe, something that >>>>> is not mentioned anywhere in this series, BTW) which is used to decode an >>>>> encrypted h264 stream. >>>>> >>>>> First question: how is that stream encrypted? Is that according to some standard? >>>>> Nothing is mentioned about that. >>>>> >>>>> I gather that the encrypted stream is fed to the codec as usual (i.e. just put it >>>>> in the output buffer and queue it to the codec), nothing special is needed for that. >>>>> Except, how does the hardware know it is encrypted? I guess that's where the >>>>> control comes in, you have to turn on SVP mode first. >>>> >>>> Decryption takes place before the decoder. I suspect there is no dedicated >>>> driver for that, the TEE driver API is similar to smart card API and fits well >>>> this task. So the decrytor consume normal memory that is encrypted and is only >>>> allowed to decrypt into secure memory. All this is happening before the decoder, >>>> so is out of scope for this patchset. >>>> >>>> Just a correction :-D. >>>> >>>>> >>>>> For the capture buffers you need to provide buffers from secure/trusted memory. >>>>> That's a dmabuf fd, but where does that come from? >>>>> >>>>> I saw this message: >>>>> >>>>> https://lore.kernel.org/linux-media/CAPj87rOHctwHJM-7HiQpt8Q0b09x0WWw_T4XsL0qT=dS+XzyZQ@mail.gmail.com/T/#u >>>>> >>>>> so I expect that's where it comes from. But I agree that getting this from dma-heaps >>>>> seems more natural. >>>>> >>>>> I assume that those capture buffers are inaccessible from the CPU? (Hence 'secure') >>>>> >>>>> For actually displaying these secure buffers you would use drm, and I assume that >>>>> the hardware would mix in the contents of the secure buffer into the video output >>>>> pipeline? I.e., the actual contents remain inaccessible. And that the video output >>>>> (HDMI or DisplayPort) is using HDCP? >>>>> >>>>>> >>>>>>> >>>>>>> In any case, using a control to switch to secure mode and using a control >>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to me. >>>>>>> >>>>>>> I was wondering if it wouldn't be better to create a new V4L2_MEMORY_ type, >>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That ensures that >>>>>>> once you create buffers for the first time, the driver can switch into secure >>>>>>> mode, and until all buffers are released again you know that the driver will >>>>>>> stay in secure mode. >>>>>> >>>>>> Why do you think the control for setting secure mode is a poor choice? >>>>>> There's various places in the driver code where functionality changes >>>>>> based on being secure/non-secure mode, so this is very much a 'global' >>>>>> setting for the driver. It could be inferred based off a new memory >>>>>> type for the queues...which then sets that flag in the driver; but >>>>>> that seems like it would be more fragile and would require checking >>>>>> for incompatible output/capture memory types. I'm not against another >>>>>> way of doing this; but didn't see why you think the proposed method is >>>>>> a poor choice. >>>>> >>>>> I assume you are either decoding to secure memory all the time, or not >>>>> at all. That's something you would want to select the moment you allocate >>>>> the first buffer. Using the V4L2_MEMORY_ value would be the natural place >>>>> for that. A control can typically be toggled at any time, and it makes >>>>> no sense to do that for secure streaming. >>>>> >>>>> Related to that: if you pass a dmabuf fd you will need to check somewhere >>>>> if the fd points to secure memory or not. You don't want to mix the two >>>>> but you want to check that at VIDIOC_QBUF time. >>>>> >>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 core, >>>>> drivers do not need to do that. >>>> >>>> Just to clarify a bit, and make sure I understand this too. You are proposing to >>>> introduce something like: >>>> >>>> V4L2_MEMORY_SECURE_DMABUF >>>> >>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while telling the >>>> driver that the memory is secure according to the definition of "secure" for the >>>> platform its running on. >>>> >>>> This drivers also allocate secure SHM (a standard tee concept) and have internal >>>> allocation for reconstruction buffer and some hw specific reference metadata. So >>>> the idea would be that it would keep allocation using the dmabuf heap internal >>>> APIs ? And decide which type of memory based on the memory type found in the >>>> queue? >>>> >>>> Stepping back a little, why can't we have a way for drivers to detect that >>>> dmabuf are secure ? I'm wondering if its actually useful to impose to all >>>> userspace component to know that a dmabuf is secure ? >>>> >>>> Also, regarding MTK, these are stateless decoders. I think it would be nice to >>>> show use example code that can properly parse the un-encrypted header, pass the >>>> data to the decryptor and decode. There is a bit of mechanic in there that lacks >>>> clarification, a reference implementation would clearly help. Finally, does this >>>> platform offers some clearkey implementation (or other alternative) so we can do >>>> validation and regression testing? It would be very unfortunate to add feature >>>> upstream that can only be tested by proprietary CDM software. >>>> >>>> Nicolas >>> >>> >>> Here's some links to the current userspace implementation built on top >>> of the MTK patches (and yeah, this'll end up changing based on what >>> happens upstream). >>> >>> 1. This is where we are decrypting the video to a secure buffer, it's >>> invoking IPC into a closed source component to do that: >>> https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/decoder_buffer_transcryptor.cc;l=87 >> >> So the encrypted compressed stream (contained in a regular non-secure buffer) >> is decrypted here into secure buffers. Correct? > Correct >> >> The hardware codec will just operate on those secure buffers, both for the >> output and capture queues, right? And no decryption/encryption takes place, >> it is all operating on unencrypted secure buffers, right? > Correct >> >> Or is the plan to include the decryption step in the driver? > No, the driver will never be doing the decryption. >> >> But who encrypted the compressed stream? Is it encrypted according to >> some standard? Or it is mediatek specific? > It's encrypted using CENC (Common Encryption). The method for > acquiring the keys to perform the decryption is Widevine specific > (Widevine is the Digital Rights Management system we are using...but > nothing in the kernel patches dictates which Digital Rights Management > system is used, but the encryption technique is a standard). Ah, that's the missing piece for me. Now I understand the context of these patches. Thank you, this was very helpful. Regards, Hans >> >> Regards, >> >> Hans >> >>> 2. This is where we aren enabling secure mode: >>> https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=412 >>> 3. This is where we are resolving secure buffers to secure handles: >>> https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_video_decoder.cc;l=535 >>> (the allocation of the secure buffers is done in closed source CDM >>> code, but it's just opening the dma-buf heap and issuing the ioctl to >>> allocate it) >>> 4. This is where we submit the secure buffers to the output queue: >>> https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/v4l2/v4l2_queue.cc;l=816 >>> (this is nothing special, since it's just passing in the fd) >>> 5. For the capture queue, there's zero changes in Chrome V4L2 code for >>> that...it's all transparent to user space that it's a secure surface >>> that's being rendered to. We do allocate them w/ different flags via >>> minigbm which happens here: >>> https://source.chromium.org/chromium/chromium/src/+/main:media/gpu/chromeos/platform_video_frame_pool.cc;l=37 >>> >>>> >>>>> >>>>>> >>>>>>> >>>>>>> For converting the dmabuf fd into a secure handle: a new ioctl similar to >>>>>>> VIDIOC_EXPBUF might be more suited for that. >>>>>> >>>>>> I actually think the best way for converting the dmabuf fd into a >>>>>> secure handle would be another ioctl in the dma-heap driver...since >>>>>> that's where the memory is actually allocated from. But this really >>>>>> depends on upstream maintainers and what they are comfortable with. >>>>> >>>>> That feels like a more natural place of doing this. >>>>> >>>>> Regards, >>>>> >>>>> Hans >>>>> >>>>>> >>>>>>> >>>>>>> Note that I am the first to admit that I have no experience with secure >>>>>>> video pipelines or optee-os, so I am looking at this purely from an uAPI >>>>>>> perspective. >>>>>>> >>>>>>> Regards, >>>>>>> >>>>>>> Hans >>>>>>> >>>>>>>> >>>>>>>> Best Regards, >>>>>>>> Yunfei Dong >>>>>>>>> Regards, >>>>>>>>> >>>>>>>>> Hans >>>>>>>>> >>>>>>>>>> >>>>>>>>>> regards, >>>>>>>>>> Nicolas >>>>>>>>>> >>>>>>>>>> p.s. you forgot to document your control in the RST doc, please do >>>>>>>>> >>>>>>>>> in following >>>>>>>>>> release. >>>>>>>>>> >>>>>>>>>>> +ctx->is_svp_mode = ctrl->val; >>>>>>>>>>> + >>>>>>>>>>> +if (ctx->is_svp_mode) { >>>>>>>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); >>>>>>>>>>> +if (ret) >>>>>>>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); >>>>>>>>>>> +else >>>>>>>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl- >>>>>>>>>> >>>>>>>>>> val); >>>>>>>>>>> +} >>>>>>>>>>> +break; >>>>>>>>>>> default: >>>>>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: >>>>>>>>>>> 0x%x\n", >>>>>>>>> >>>>>>>>> hdr_ctrl->id); >>>>>>>>>>> return ret; >>>>>>>>>>> @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>>>>>>> >>>>>>>>> mtk_vcodec_dec_ctx *ctx) >>>>>>>>>>> unsigned int i; >>>>>>>>>>> struct v4l2_ctrl *ctrl; >>>>>>>>>>> >>>>>>>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); >>>>>>>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); >>>>>>>>>>> if (ctx->ctrl_hdl.error) { >>>>>>>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); >>>>>>>>>>> return ctx->ctrl_hdl.error; >>>>>>>>>>> @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct >>>>>>>>> >>>>>>>>> mtk_vcodec_dec_ctx *ctx) >>>>>>>>>>> >>>>>>>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>>>>>>> >>>>>>>>> &mtk_vcodec_dec_ctrl_ops, >>>>>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); >>>>>>>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, >>>>>>>>> >>>>>>>>> &mtk_vcodec_dec_ctrl_ops, >>>>>>>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); >>>>>>>>>>> >>>>>>>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); >>>>>>>>>>> >>>>>>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>> >>>>>>>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>>>> index d8cf01f76aab..a507045a3f30 100644 >>>>>>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c >>>>>>>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) >>>>>>>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return >>>>>>>>>>> "Reference >>>>>>>>> >>>>>>>>> Frames for a P-Frame"; >>>>>>>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return "Prepend >>>>>>>>> >>>>>>>>> SPS and PPS to IDR"; >>>>>>>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek >>>>>>>>>>> Decoder >>>>>>>>> >>>>>>>>> get secure handle"; >>>>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek Decoder >>>>>>>>> >>>>>>>>> set secure mode"; >>>>>>>>>>> >>>>>>>>>>> /* AV1 controls */ >>>>>>>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; >>>>>>>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char >>>>>>>>> >>>>>>>>> **name, enum v4l2_ctrl_type *type, >>>>>>>>>>> *type = V4L2_CTRL_TYPE_INTEGER; >>>>>>>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>>>>>>> break; >>>>>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: >>>>>>>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; >>>>>>>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; >>>>>>>>>>> +break; >>>>>>>>>>> case V4L2_CID_USER_CLASS: >>>>>>>>>>> case V4L2_CID_CAMERA_CLASS: >>>>>>>>>>> case V4L2_CID_CODEC_CLASS: >>>>>>>>>>> diff --git a/include/uapi/linux/v4l2-controls.h >>>>>>>>> >>>>>>>>> b/include/uapi/linux/v4l2-controls.h >>>>>>>>>>> index 7b3694985366..88e90d943e38 100644 >>>>>>>>>>> --- a/include/uapi/linux/v4l2-controls.h >>>>>>>>>>> +++ b/include/uapi/linux/v4l2-controls.h >>>>>>>>>>> @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { >>>>>>>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder >>>>>>>>> >>>>>>>>> driver as defined by V4L2 */ >>>>>>>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | 0x2000) >>>>>>>>>>> #define >>>>>>>>> >>>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) >>>>>>>>>>> +#define >>>>>>>>> >>>>>>>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) >>>>>>>>>>> >>>>>>>>>>> /* Camera class control IDs */ >>>>>>>>>>> >>>>>>> >>>>>>> >>>>>>> _______________________________________________ >>>>>>> linux-arm-kernel mailing list >>>>>>> linux-arm-kernel@lists.infradead.org >>>>>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel >>>>> >>>> >>
Le mercredi 20 septembre 2023 à 11:20 -0700, Jeffrey Kardatzke a écrit : > > > > > > Also, regarding MTK, these are stateless decoders. I think it would be nice to > > > show use example code that can properly parse the un-encrypted header, pass the > > > data to the decryptor and decode. There is a bit of mechanic in there that lacks > > > clarification, a reference implementation would clearly help. Finally, does this > > > platform offers some clearkey implementation (or other alternative) so we can do > > > validation and regression testing? It would be very unfortunate to add feature > > > upstream that can only be tested by proprietary CDM software. > > > > It would be possible to use this with clearkey w/ some additional work > on our end. If this is then part of the public ChromiumOS build, would > that be satisfactory? (the TEE would have some binary blob components > like firmware does though) From my point of view, this would fully cover my concern. To clarify this concern, the decryption into secure memory currently only ever take place in proprietary code that implements the protection (Widewine CDM). With clear key, we can have an open source CDM (made for testing purpose) so that we don't have to have hidden code to test the entire pipeline. So appart from the TEE firmware, which is just a firmware like all the others, we could have open source tests in kernelCI and other CI, and we could extend these test to eventually support other vendors. Note that currently, with other proposal, one could allocate and fill a normal buffer, and "secure" that buffer to test the CODECs and display, but on this specific architecture, with the limitation on the number of secure regions, this feature isn't available. Alternatives to this end-to-end solution, we could consider a TA (Trusted Application) that simply copy data from a untrusted chunk of memory into a trusted chunk of memory. That seems like a cross-platform solution. It would be even better if this get standardized in TEEs for course (or at least required with all secure memory implementation). Then copying from untrusted to trusted could easily become an ioctl generic to all TEE drivers. That to me would be equally acceptable, and perhaps easier to use. Nicolas
On Thu, Sep 21, 2023 at 8:46 AM Nicolas Dufresne <nicolas.dufresne@collabora.com> wrote: > > Le mercredi 20 septembre 2023 à 11:20 -0700, Jeffrey Kardatzke a écrit : > > > > > > > > Also, regarding MTK, these are stateless decoders. I think it would be nice to > > > > show use example code that can properly parse the un-encrypted header, pass the > > > > data to the decryptor and decode. There is a bit of mechanic in there that lacks > > > > clarification, a reference implementation would clearly help. Finally, does this > > > > platform offers some clearkey implementation (or other alternative) so we can do > > > > validation and regression testing? It would be very unfortunate to add feature > > > > upstream that can only be tested by proprietary CDM software. > > > > > > > It would be possible to use this with clearkey w/ some additional work > > on our end. If this is then part of the public ChromiumOS build, would > > that be satisfactory? (the TEE would have some binary blob components > > like firmware does though) > > From my point of view, this would fully cover my concern. To clarify this > concern, the decryption into secure memory currently only ever take place in > proprietary code that implements the protection (Widewine CDM). With clear key, > we can have an open source CDM (made for testing purpose) so that we don't have > to have hidden code to test the entire pipeline. So appart from the TEE > firmware, which is just a firmware like all the others, we could have open > source tests in kernelCI and other CI, and we could extend these test to > eventually support other vendors. > > Note that currently, with other proposal, one could allocate and fill a normal > buffer, and "secure" that buffer to test the CODECs and display, but on this > specific architecture, with the limitation on the number of secure regions, this > feature isn't available. > > Alternatives to this end-to-end solution, we could consider a TA (Trusted > Application) that simply copy data from a untrusted chunk of memory into a > trusted chunk of memory. That seems like a cross-platform solution. It would be > even better if this get standardized in TEEs for course (or at least required > with all secure memory implementation). Then copying from untrusted to trusted > could easily become an ioctl generic to all TEE drivers. That to me would be > equally acceptable, and perhaps easier to use. > > Nicolas It's very likely for the clearkey implementation that I would just have it copying the data from a non-secure to secure buffer in a TA. We would never do that in production of course, but for testing images that would suffice.
Hi Hans, Thanks for your help to give some good advice. On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: > > >>>> In any case, using a control to switch to secure mode and using > a control > >>>> to convert a dmabuf fd to a secure handle seems a poor choice to > me. > >>>> > >>>> I was wondering if it wouldn't be better to create a new > V4L2_MEMORY_ type, > >>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > ensures that > >>>> once you create buffers for the first time, the driver can > switch into secure > >>>> mode, and until all buffers are released again you know that the > driver will > >>>> stay in secure mode. > >>> > >>> Why do you think the control for setting secure mode is a poor > choice? > >>> There's various places in the driver code where functionality > changes > >>> based on being secure/non-secure mode, so this is very much a > 'global' > >>> setting for the driver. It could be inferred based off a new > memory > >>> type for the queues...which then sets that flag in the driver; > but > >>> that seems like it would be more fragile and would require > checking > >>> for incompatible output/capture memory types. I'm not against > another > >>> way of doing this; but didn't see why you think the proposed > method is > >>> a poor choice. > >> > >> I assume you are either decoding to secure memory all the time, or > not > >> at all. That's something you would want to select the moment you > allocate > >> the first buffer. Using the V4L2_MEMORY_ value would be the > natural place > >> for that. A control can typically be toggled at any time, and it > makes > >> no sense to do that for secure streaming. > >> > >> Related to that: if you pass a dmabuf fd you will need to check > somewhere > >> if the fd points to secure memory or not. You don't want to mix > the two > >> but you want to check that at VIDIOC_QBUF time. > >> > >> Note that the V4L2_MEMORY_ value is already checked in the v4l2 > core, > >> drivers do not need to do that. > > > > Just to clarify a bit, and make sure I understand this too. You are > proposing to > > introduce something like: > > > > V4L2_MEMORY_SECURE_DMABUF > > > > Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while > telling the > > driver that the memory is secure according to the definition of > "secure" for the > > platform its running on. > > > > This drivers also allocate secure SHM (a standard tee concept) and > have internal > > allocation for reconstruction buffer and some hw specific reference > metadata. So > > the idea would be that it would keep allocation using the dmabuf > heap internal > > APIs ? And decide which type of memory based on the memory type > found in the > > queue? > > Yes. Once you request the first buffer you basically tell the driver > whether it > will operate in secure or non-secure mode, and that stays that way > until all > buffers are freed. I think that makes sense. > According to iommu's information, the dma operation for secure and non- secure are the same, whether just need to add one memory type in v4l2 framework the same as V4L2_MEMORY_DMABUF? The dma operation in videobuf2-dma-contig.c can use the same functions. Best Regards, Yunfei Dong > If there is a need in the future to have V4L2 allocate the secure > buffers, then > a similar V4L2_MEMORY_MMAP_SECURE type can be added. I think using > v4l2_memory > to select secure or non-secure mode is logical and fits well with the > V4L2 API. > > Stepping back a little, why can't we have a way for drivers to > detect that > > dmabuf are secure ? I'm wondering if its actually useful to impose > to all > > userspace component to know that a dmabuf is secure ? > > I was wondering the same thing: there should be a simple way for > drivers and > userspace to check if a dmabuf fd is secure or not. That will > certainly help > the vb2 framework verify that you don't mix secure and non-secure > dmabuf fds. > > > > > Also, regarding MTK, these are stateless decoders. I think it would > be nice to > > show use example code that can properly parse the un-encrypted > header, pass the > > data to the decryptor and decode. There is a bit of mechanic in > there that lacks > > clarification, a reference implementation would clearly help. > Finally, does this > > platform offers some clearkey implementation (or other alternative) > so we can do > > validation and regression testing? It would be very unfortunate to > add feature > > upstream that can only be tested by proprietary CDM software. > > Good points. > > Hans > > > > > Nicolas > > > >> > >>> > >>>> > >>>> For converting the dmabuf fd into a secure handle: a new ioctl > similar to > >>>> VIDIOC_EXPBUF might be more suited for that. > >>> > >>> I actually think the best way for converting the dmabuf fd into a > >>> secure handle would be another ioctl in the dma-heap > driver...since > >>> that's where the memory is actually allocated from. But this > really > >>> depends on upstream maintainers and what they are comfortable > with. > >> > >> That feels like a more natural place of doing this. > >> > >> Regards, > >> > >> Hans > >> > >>> > >>>> > >>>> Note that I am the first to admit that I have no experience with > secure > >>>> video pipelines or optee-os, so I am looking at this purely from > an uAPI > >>>> perspective. > >>>> > >>>> Regards, > >>>> > >>>> Hans > >>>> > >>>>> > >>>>> Best Regards, > >>>>> Yunfei Dong > >>>>>> Regards, > >>>>>> > >>>>>> Hans > >>>>>> > >>>>>>> > >>>>>>> regards, > >>>>>>> Nicolas > >>>>>>> > >>>>>>> p.s. you forgot to document your control in the RST doc, > please do > >>>>>> > >>>>>> in following > >>>>>>> release. > >>>>>>> > >>>>>>>> +ctx->is_svp_mode = ctrl->val; > >>>>>>>> + > >>>>>>>> +if (ctx->is_svp_mode) { > >>>>>>>> +ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); > >>>>>>>> +if (ret) > >>>>>>>> +mtk_v4l2_vdec_err(ctx, "open secure mode failed."); > >>>>>>>> +else > >>>>>>>> +mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", > ctrl- > >>>>>>> > >>>>>>> val); > >>>>>>>> +} > >>>>>>>> +break; > >>>>>>>> default: > >>>>>>>> mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: > >>>>>>>> 0x%x\n", > >>>>>> > >>>>>> hdr_ctrl->id); > >>>>>>>> return ret; > >>>>>>>> @@ -573,7 +584,7 @@ static int > mtk_vcodec_dec_ctrls_setup(struct > >>>>>> > >>>>>> mtk_vcodec_dec_ctx *ctx) > >>>>>>>> unsigned int i; > >>>>>>>> struct v4l2_ctrl *ctrl; > >>>>>>>> > >>>>>>>> -v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); > >>>>>>>> +v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); > >>>>>>>> if (ctx->ctrl_hdl.error) { > >>>>>>>> mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); > >>>>>>>> return ctx->ctrl_hdl.error; > >>>>>>>> @@ -592,6 +603,8 @@ static int > mtk_vcodec_dec_ctrls_setup(struct > >>>>>> > >>>>>> mtk_vcodec_dec_ctx *ctx) > >>>>>>>> > >>>>>>>> ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>>>> > >>>>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); > >>>>>>>> +ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, > >>>>>> > >>>>>> &mtk_vcodec_dec_ctrl_ops, > >>>>>>>> + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); > >>>>>>>> > >>>>>>>> v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); > >>>>>>>> > >>>>>>>> diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>> > >>>>>> b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>> index d8cf01f76aab..a507045a3f30 100644 > >>>>>>>> --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>> +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c > >>>>>>>> @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) > >>>>>>>> case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES:return > >>>>>>>> "Reference > >>>>>> > >>>>>> Frames for a P-Frame"; > >>>>>>>> case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR:return > "Prepend > >>>>>> > >>>>>> SPS and PPS to IDR"; > >>>>>>>> case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE:return "MediaTek > >>>>>>>> Decoder > >>>>>> > >>>>>> get secure handle"; > >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE:return "MediaTek > Decoder > >>>>>> > >>>>>> set secure mode"; > >>>>>>>> > >>>>>>>> /* AV1 controls */ > >>>>>>>> case V4L2_CID_MPEG_VIDEO_AV1_PROFILE:return "AV1 Profile"; > >>>>>>>> @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const > char > >>>>>> > >>>>>> **name, enum v4l2_ctrl_type *type, > >>>>>>>> *type = V4L2_CTRL_TYPE_INTEGER; > >>>>>>>> *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>>>> break; > >>>>>>>> +case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: > >>>>>>>> +*type = V4L2_CTRL_TYPE_INTEGER; > >>>>>>>> +*flags |= V4L2_CTRL_FLAG_WRITE_ONLY; > >>>>>>>> +break; > >>>>>>>> case V4L2_CID_USER_CLASS: > >>>>>>>> case V4L2_CID_CAMERA_CLASS: > >>>>>>>> case V4L2_CID_CODEC_CLASS: > >>>>>>>> diff --git a/include/uapi/linux/v4l2-controls.h > >>>>>> > >>>>>> b/include/uapi/linux/v4l2-controls.h > >>>>>>>> index 7b3694985366..88e90d943e38 100644 > >>>>>>>> --- a/include/uapi/linux/v4l2-controls.h > >>>>>>>> +++ b/include/uapi/linux/v4l2-controls.h > >>>>>>>> @@ -957,6 +957,7 @@ enum > v4l2_mpeg_mfc51_video_force_frame_type { > >>>>>>>> /* MPEG-class control IDs specific to the MediaTek Decoder > >>>>>> > >>>>>> driver as defined by V4L2 */ > >>>>>>>> #define V4L2_CID_MPEG_MTK_BASE(V4L2_CTRL_CLASS_CODEC | > 0x2000) > >>>>>>>> #define > >>>>>> > >>>>>> V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE(V4L2_CID_MPEG_MTK_BASE+8) > >>>>>>>> +#define > >>>>>> > >>>>>> V4L2_CID_MPEG_MTK_SET_SECURE_MODE(V4L2_CID_MPEG_MTK_BASE+9) > >>>>>>>> > >>>>>>>> /* Camera class control IDs */ > >>>>>>>> > >>>> > >>>> > >>>> _______________________________________________ > >>>> linux-arm-kernel mailing list > >>>> linux-arm-kernel@lists.infradead.org > >>>> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel > >> > > >
On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: > Hi Hans, > > Thanks for your help to give some good advice. > On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: >> >>>>>> In any case, using a control to switch to secure mode and using >> a control >>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to >> me. >>>>>> >>>>>> I was wondering if it wouldn't be better to create a new >> V4L2_MEMORY_ type, >>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That >> ensures that >>>>>> once you create buffers for the first time, the driver can >> switch into secure >>>>>> mode, and until all buffers are released again you know that the >> driver will >>>>>> stay in secure mode. >>>>> >>>>> Why do you think the control for setting secure mode is a poor >> choice? >>>>> There's various places in the driver code where functionality >> changes >>>>> based on being secure/non-secure mode, so this is very much a >> 'global' >>>>> setting for the driver. It could be inferred based off a new >> memory >>>>> type for the queues...which then sets that flag in the driver; >> but >>>>> that seems like it would be more fragile and would require >> checking >>>>> for incompatible output/capture memory types. I'm not against >> another >>>>> way of doing this; but didn't see why you think the proposed >> method is >>>>> a poor choice. >>>> >>>> I assume you are either decoding to secure memory all the time, or >> not >>>> at all. That's something you would want to select the moment you >> allocate >>>> the first buffer. Using the V4L2_MEMORY_ value would be the >> natural place >>>> for that. A control can typically be toggled at any time, and it >> makes >>>> no sense to do that for secure streaming. >>>> >>>> Related to that: if you pass a dmabuf fd you will need to check >> somewhere >>>> if the fd points to secure memory or not. You don't want to mix >> the two >>>> but you want to check that at VIDIOC_QBUF time. >>>> >>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 >> core, >>>> drivers do not need to do that. >>> >>> Just to clarify a bit, and make sure I understand this too. You are >> proposing to >>> introduce something like: >>> >>> V4L2_MEMORY_SECURE_DMABUF >>> >>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while >> telling the >>> driver that the memory is secure according to the definition of >> "secure" for the >>> platform its running on. >>> >>> This drivers also allocate secure SHM (a standard tee concept) and >> have internal >>> allocation for reconstruction buffer and some hw specific reference >> metadata. So >>> the idea would be that it would keep allocation using the dmabuf >> heap internal >>> APIs ? And decide which type of memory based on the memory type >> found in the >>> queue? >> >> Yes. Once you request the first buffer you basically tell the driver >> whether it >> will operate in secure or non-secure mode, and that stays that way >> until all >> buffers are freed. I think that makes sense. >> > > According to iommu's information, the dma operation for secure and non- > secure are the same, whether just need to add one memory type in v4l2 > framework the same as V4L2_MEMORY_DMABUF? The dma operation in > videobuf2-dma-contig.c can use the same functions. So if I pass a non-secure dma fd to the capture queue of the codec, who will check that it can't write the data to that fd? Since doing so would expose the video. Presumably at some point the tee code will prevent that? (I sincerely hope so!) Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the driver that 1) it can expect secure dmabuf fds, 2) it can configure itself for that (that avoids using a control to toggle between normal and secure mode), and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the fd that is passed in is for secure memory. This means that mistakes by userspace are caught at QBUF time. Of course, this will not protect you (people can disable this check by recompiling the kernel), that still has to be done by the firmware, but it catches userspace errors early on. Also, while for this hardware the DMA operation is the same, that might not be the case for other hardware. Regards, Hans > > Best Regards, > Yunfei Dong >
On Fri, Sep 22, 2023 at 1:44 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: > > Hi Hans, > > > > Thanks for your help to give some good advice. > > On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: > >> > >>>>>> In any case, using a control to switch to secure mode and using > >> a control > >>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to > >> me. > >>>>>> > >>>>>> I was wondering if it wouldn't be better to create a new > >> V4L2_MEMORY_ type, > >>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > >> ensures that > >>>>>> once you create buffers for the first time, the driver can > >> switch into secure > >>>>>> mode, and until all buffers are released again you know that the > >> driver will > >>>>>> stay in secure mode. > >>>>> > >>>>> Why do you think the control for setting secure mode is a poor > >> choice? > >>>>> There's various places in the driver code where functionality > >> changes > >>>>> based on being secure/non-secure mode, so this is very much a > >> 'global' > >>>>> setting for the driver. It could be inferred based off a new > >> memory > >>>>> type for the queues...which then sets that flag in the driver; > >> but > >>>>> that seems like it would be more fragile and would require > >> checking > >>>>> for incompatible output/capture memory types. I'm not against > >> another > >>>>> way of doing this; but didn't see why you think the proposed > >> method is > >>>>> a poor choice. > >>>> > >>>> I assume you are either decoding to secure memory all the time, or > >> not > >>>> at all. That's something you would want to select the moment you > >> allocate > >>>> the first buffer. Using the V4L2_MEMORY_ value would be the > >> natural place > >>>> for that. A control can typically be toggled at any time, and it > >> makes > >>>> no sense to do that for secure streaming. > >>>> > >>>> Related to that: if you pass a dmabuf fd you will need to check > >> somewhere > >>>> if the fd points to secure memory or not. You don't want to mix > >> the two > >>>> but you want to check that at VIDIOC_QBUF time. > >>>> > >>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 > >> core, > >>>> drivers do not need to do that. > >>> > >>> Just to clarify a bit, and make sure I understand this too. You are > >> proposing to > >>> introduce something like: > >>> > >>> V4L2_MEMORY_SECURE_DMABUF > >>> > >>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while > >> telling the > >>> driver that the memory is secure according to the definition of > >> "secure" for the > >>> platform its running on. > >>> > >>> This drivers also allocate secure SHM (a standard tee concept) and > >> have internal > >>> allocation for reconstruction buffer and some hw specific reference > >> metadata. So > >>> the idea would be that it would keep allocation using the dmabuf > >> heap internal > >>> APIs ? And decide which type of memory based on the memory type > >> found in the > >>> queue? > >> > >> Yes. Once you request the first buffer you basically tell the driver > >> whether it > >> will operate in secure or non-secure mode, and that stays that way > >> until all > >> buffers are freed. I think that makes sense. > >> > > > > According to iommu's information, the dma operation for secure and non- > > secure are the same, whether just need to add one memory type in v4l2 > > framework the same as V4L2_MEMORY_DMABUF? The dma operation in > > videobuf2-dma-contig.c can use the same functions. > > So if I pass a non-secure dma fd to the capture queue of the codec, who > will check that it can't write the data to that fd? Since doing so would > expose the video. Presumably at some point the tee code will prevent that? > (I sincerely hope so!) It is entirely the job of the TEE to prevent this. Nothing in the kernel should allow exploitation of what happens in the TEE no matter what goes on in the kernel > > Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the > driver that 1) it can expect secure dmabuf fds, 2) it can configure itself > for that (that avoids using a control to toggle between normal and secure mode), > and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the > fd that is passed in is for secure memory. This means that mistakes by > userspace are caught at QBUF time. > > Of course, this will not protect you (people can disable this check by > recompiling the kernel), that still has to be done by the firmware, but > it catches userspace errors early on. > > Also, while for this hardware the DMA operation is the same, that might > not be the case for other hardware. That's a really good point. So one of the other models that is used for secure video decoding is to send the encrypted buffer into the video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in all the corresponding crypto parameters (i.e. algorithm, IV, encryption pattern, etc.). Then the video driver internally does the decryption and decode in one operation. That's not what we want to use here for Mediatek; but I've done other integrations that work that way (that was for VAAPI [1], not V4L2...but there are other ARM implementations that do operate that way). So if we end up requiring V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on output+capture, that'll close off other potential solutions in the future. Expanding on your point about DMA operations being different on various hardware, that also makes me think a general check for this in v4l2 code may also be limiting. There are various ways secure video pipelines are done, so leaving these checks up to the individual drivers that implement secure video decode may be more pragmatic. If there's a generic V4L2 _CID_SECURE_MODE control, that makes it more general for how drivers can handle secure video decode. [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 > > Regards, > > Hans > > > > > Best Regards, > > Yunfei Dong > > >
On 22/09/2023 21:17, Jeffrey Kardatzke wrote: > On Fri, Sep 22, 2023 at 1:44 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >> >> On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: >>> Hi Hans, >>> >>> Thanks for your help to give some good advice. >>> On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: >>>> >>>>>>>> In any case, using a control to switch to secure mode and using >>>> a control >>>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to >>>> me. >>>>>>>> >>>>>>>> I was wondering if it wouldn't be better to create a new >>>> V4L2_MEMORY_ type, >>>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That >>>> ensures that >>>>>>>> once you create buffers for the first time, the driver can >>>> switch into secure >>>>>>>> mode, and until all buffers are released again you know that the >>>> driver will >>>>>>>> stay in secure mode. >>>>>>> >>>>>>> Why do you think the control for setting secure mode is a poor >>>> choice? >>>>>>> There's various places in the driver code where functionality >>>> changes >>>>>>> based on being secure/non-secure mode, so this is very much a >>>> 'global' >>>>>>> setting for the driver. It could be inferred based off a new >>>> memory >>>>>>> type for the queues...which then sets that flag in the driver; >>>> but >>>>>>> that seems like it would be more fragile and would require >>>> checking >>>>>>> for incompatible output/capture memory types. I'm not against >>>> another >>>>>>> way of doing this; but didn't see why you think the proposed >>>> method is >>>>>>> a poor choice. >>>>>> >>>>>> I assume you are either decoding to secure memory all the time, or >>>> not >>>>>> at all. That's something you would want to select the moment you >>>> allocate >>>>>> the first buffer. Using the V4L2_MEMORY_ value would be the >>>> natural place >>>>>> for that. A control can typically be toggled at any time, and it >>>> makes >>>>>> no sense to do that for secure streaming. >>>>>> >>>>>> Related to that: if you pass a dmabuf fd you will need to check >>>> somewhere >>>>>> if the fd points to secure memory or not. You don't want to mix >>>> the two >>>>>> but you want to check that at VIDIOC_QBUF time. >>>>>> >>>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 >>>> core, >>>>>> drivers do not need to do that. >>>>> >>>>> Just to clarify a bit, and make sure I understand this too. You are >>>> proposing to >>>>> introduce something like: >>>>> >>>>> V4L2_MEMORY_SECURE_DMABUF >>>>> >>>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while >>>> telling the >>>>> driver that the memory is secure according to the definition of >>>> "secure" for the >>>>> platform its running on. >>>>> >>>>> This drivers also allocate secure SHM (a standard tee concept) and >>>> have internal >>>>> allocation for reconstruction buffer and some hw specific reference >>>> metadata. So >>>>> the idea would be that it would keep allocation using the dmabuf >>>> heap internal >>>>> APIs ? And decide which type of memory based on the memory type >>>> found in the >>>>> queue? >>>> >>>> Yes. Once you request the first buffer you basically tell the driver >>>> whether it >>>> will operate in secure or non-secure mode, and that stays that way >>>> until all >>>> buffers are freed. I think that makes sense. >>>> >>> >>> According to iommu's information, the dma operation for secure and non- >>> secure are the same, whether just need to add one memory type in v4l2 >>> framework the same as V4L2_MEMORY_DMABUF? The dma operation in >>> videobuf2-dma-contig.c can use the same functions. >> >> So if I pass a non-secure dma fd to the capture queue of the codec, who >> will check that it can't write the data to that fd? Since doing so would >> expose the video. Presumably at some point the tee code will prevent that? >> (I sincerely hope so!) > > It is entirely the job of the TEE to prevent this. Nothing in the > kernel should allow exploitation of what happens in the TEE no matter > what goes on in the kernel > >> >> Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the >> driver that 1) it can expect secure dmabuf fds, 2) it can configure itself >> for that (that avoids using a control to toggle between normal and secure mode), >> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the >> fd that is passed in is for secure memory. This means that mistakes by >> userspace are caught at QBUF time. >> >> Of course, this will not protect you (people can disable this check by >> recompiling the kernel), that still has to be done by the firmware, but >> it catches userspace errors early on. >> >> Also, while for this hardware the DMA operation is the same, that might >> not be the case for other hardware. > > That's a really good point. So one of the other models that is used > for secure video decoding is to send the encrypted buffer into the > video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in > all the corresponding crypto parameters (i.e. algorithm, IV, > encryption pattern, etc.). Then the video driver internally does the > decryption and decode in one operation. That's not what we want to > use here for Mediatek; but I've done other integrations that work that > way (that was for VAAPI [1], not V4L2...but there are other ARM > implementations that do operate that way). So if we end up requiring > V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on > output+capture, that'll close off other potential solutions in the > future. > > Expanding on your point about DMA operations being different on > various hardware, that also makes me think a general check for this in > v4l2 code may also be limiting. There are various ways secure video > pipelines are done, so leaving these checks up to the individual > drivers that implement secure video decode may be more pragmatic. If > there's a generic V4L2 _CID_SECURE_MODE control, that makes it more > general for how drivers can handle secure video decode. No, using a control for this is really wrong. The reason why I want it as a separate memory type is that that is what you use when you call VIDIOC_REQBUFS, and that ioctl is also when things are locked down in a driver. As long as no buffers have been allocated, you can still change formats, parameters, etc. But once buffers are allocated, most of that can't be changed, since changing e.g. the format would also change the buffer sizes. It also locks down who owns the buffers by storing the file descriptor. This prevents other processes from hijacking the I/O streaming, only the owner can stream buffers. So it is a natural point in the sequence for selecting secure buffers. If you request V4L2_MEMORY_DMABUF_SECURE for the output, then the capture side must also use DMABUF_SECURE. Whether or not you can use regular DMABUF for the output side and select DMABUF_SECURE on the capture side is a driver decision. It can be useful to support this for testing the secure capture using regular video streams (something Nicolas discussed as well), but it depends on the hardware whether you can use that technique. Regards, Hans > > [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 > >> >> Regards, >> >> Hans >> >>> >>> Best Regards, >>> Yunfei Dong >>> >>
On Mon, Sep 25, 2023 at 2:00 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 22/09/2023 21:17, Jeffrey Kardatzke wrote: > > On Fri, Sep 22, 2023 at 1:44 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > >> > >> On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: > >>> Hi Hans, > >>> > >>> Thanks for your help to give some good advice. > >>> On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: > >>>> > >>>>>>>> In any case, using a control to switch to secure mode and using > >>>> a control > >>>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to > >>>> me. > >>>>>>>> > >>>>>>>> I was wondering if it wouldn't be better to create a new > >>>> V4L2_MEMORY_ type, > >>>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > >>>> ensures that > >>>>>>>> once you create buffers for the first time, the driver can > >>>> switch into secure > >>>>>>>> mode, and until all buffers are released again you know that the > >>>> driver will > >>>>>>>> stay in secure mode. > >>>>>>> > >>>>>>> Why do you think the control for setting secure mode is a poor > >>>> choice? > >>>>>>> There's various places in the driver code where functionality > >>>> changes > >>>>>>> based on being secure/non-secure mode, so this is very much a > >>>> 'global' > >>>>>>> setting for the driver. It could be inferred based off a new > >>>> memory > >>>>>>> type for the queues...which then sets that flag in the driver; > >>>> but > >>>>>>> that seems like it would be more fragile and would require > >>>> checking > >>>>>>> for incompatible output/capture memory types. I'm not against > >>>> another > >>>>>>> way of doing this; but didn't see why you think the proposed > >>>> method is > >>>>>>> a poor choice. > >>>>>> > >>>>>> I assume you are either decoding to secure memory all the time, or > >>>> not > >>>>>> at all. That's something you would want to select the moment you > >>>> allocate > >>>>>> the first buffer. Using the V4L2_MEMORY_ value would be the > >>>> natural place > >>>>>> for that. A control can typically be toggled at any time, and it > >>>> makes > >>>>>> no sense to do that for secure streaming. > >>>>>> > >>>>>> Related to that: if you pass a dmabuf fd you will need to check > >>>> somewhere > >>>>>> if the fd points to secure memory or not. You don't want to mix > >>>> the two > >>>>>> but you want to check that at VIDIOC_QBUF time. > >>>>>> > >>>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 > >>>> core, > >>>>>> drivers do not need to do that. > >>>>> > >>>>> Just to clarify a bit, and make sure I understand this too. You are > >>>> proposing to > >>>>> introduce something like: > >>>>> > >>>>> V4L2_MEMORY_SECURE_DMABUF > >>>>> > >>>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while > >>>> telling the > >>>>> driver that the memory is secure according to the definition of > >>>> "secure" for the > >>>>> platform its running on. > >>>>> > >>>>> This drivers also allocate secure SHM (a standard tee concept) and > >>>> have internal > >>>>> allocation for reconstruction buffer and some hw specific reference > >>>> metadata. So > >>>>> the idea would be that it would keep allocation using the dmabuf > >>>> heap internal > >>>>> APIs ? And decide which type of memory based on the memory type > >>>> found in the > >>>>> queue? > >>>> > >>>> Yes. Once you request the first buffer you basically tell the driver > >>>> whether it > >>>> will operate in secure or non-secure mode, and that stays that way > >>>> until all > >>>> buffers are freed. I think that makes sense. > >>>> > >>> > >>> According to iommu's information, the dma operation for secure and non- > >>> secure are the same, whether just need to add one memory type in v4l2 > >>> framework the same as V4L2_MEMORY_DMABUF? The dma operation in > >>> videobuf2-dma-contig.c can use the same functions. > >> > >> So if I pass a non-secure dma fd to the capture queue of the codec, who > >> will check that it can't write the data to that fd? Since doing so would > >> expose the video. Presumably at some point the tee code will prevent that? > >> (I sincerely hope so!) > > > > It is entirely the job of the TEE to prevent this. Nothing in the > > kernel should allow exploitation of what happens in the TEE no matter > > what goes on in the kernel > > > >> > >> Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the > >> driver that 1) it can expect secure dmabuf fds, 2) it can configure itself > >> for that (that avoids using a control to toggle between normal and secure mode), > >> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the > >> fd that is passed in is for secure memory. This means that mistakes by > >> userspace are caught at QBUF time. > >> > >> Of course, this will not protect you (people can disable this check by > >> recompiling the kernel), that still has to be done by the firmware, but > >> it catches userspace errors early on. > >> > >> Also, while for this hardware the DMA operation is the same, that might > >> not be the case for other hardware. > > > > That's a really good point. So one of the other models that is used > > for secure video decoding is to send the encrypted buffer into the > > video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in > > all the corresponding crypto parameters (i.e. algorithm, IV, > > encryption pattern, etc.). Then the video driver internally does the > > decryption and decode in one operation. That's not what we want to > > use here for Mediatek; but I've done other integrations that work that > > way (that was for VAAPI [1], not V4L2...but there are other ARM > > implementations that do operate that way). So if we end up requiring > > V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on > > output+capture, that'll close off other potential solutions in the > > future. > > > > Expanding on your point about DMA operations being different on > > various hardware, that also makes me think a general check for this in > > v4l2 code may also be limiting. There are various ways secure video > > pipelines are done, so leaving these checks up to the individual > > drivers that implement secure video decode may be more pragmatic. If > > there's a generic V4L2 _CID_SECURE_MODE control, that makes it more > > general for how drivers can handle secure video decode. > > No, using a control for this is really wrong. > > The reason why I want it as a separate memory type is that that is > what you use when you call VIDIOC_REQBUFS, and that ioctl is also > when things are locked down in a driver. As long as no buffers have > been allocated, you can still change formats, parameters, etc. But > once buffers are allocated, most of that can't be changed, since > changing e.g. the format would also change the buffer sizes. > > It also locks down who owns the buffers by storing the file descriptor. > This prevents other processes from hijacking the I/O streaming, only > the owner can stream buffers. > > So it is a natural point in the sequence for selecting secure > buffers. > > If you request V4L2_MEMORY_DMABUF_SECURE for the output, then the > capture side must also use DMABUF_SECURE. Whether or not you can > use regular DMABUF for the output side and select DMABUF_SECURE > on the capture side is a driver decision. It can be useful to > support this for testing the secure capture using regular video > streams (something Nicolas discussed as well), but it depends on > the hardware whether you can use that technique. OK, that does work for the additional cases I mentioned. And for testing...we would still want to use DMABUF_SECURE on both ends for Mediatek at least (that's the only way they support it). But rather than having to bother with a clearkey implementation...we can just do something that directly copies compressed video into the secure dmabufs and then exercises the whole pipeline from there. This same thing happens with the 'clear lead' that is sometimes there with encrypted video (where the first X seconds are unencrypted and then it switches to encrypted...but you're still using the secure video pipeline on the unencrypted frames in that case). > > Regards, > > Hans > > > > > [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 > > > >> > >> Regards, > >> > >> Hans > >> > >>> > >>> Best Regards, > >>> Yunfei Dong > >>> > >> >
Hans, I've been looking through the v4l2/vbuf2 code to get an idea of the details for implementing a new memory type for secure buffers. What it comes down to essentially is that it would behave just like V4L2_MEMORY_DMABUF, but then there would be an extra check in __prepare_dmabuf (in videobuf2-core.c) when the memory type is SECURE to ensure that it is actually from a secure dma-buf allocation. So I'm thinking an alternate solution might be cleaner so we don't have two memory types that are handled nearly identically in most of the code. What do you think about a new memory flag like V4L2_MEMORY_FLAG_SECURE? This would be set in vb2_queue struct like the other existing memory flag. Then when it gets into __prepare_dmabuf and invokes attach_dmabuf on each buffer...that call could then check for the existence of that flag, and if it's there it could validate it is actually secure memory. Then in various other dmabuf vb2_mem_ops (maybe alloc, get_userptr, vaddr and mmap) those could also check for the secure flag, and if present return an error/null. Then also in the driver specific vb2_ops for queue_setup, the MTK driver could recognize the flag there and then configure itself for secure mode. How does that sound as an overall strategy? Cheers, Jeff On Mon, Sep 25, 2023 at 9:51 AM Jeffrey Kardatzke <jkardatzke@google.com> wrote: > > On Mon, Sep 25, 2023 at 2:00 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > > > On 22/09/2023 21:17, Jeffrey Kardatzke wrote: > > > On Fri, Sep 22, 2023 at 1:44 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > >> > > >> On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: > > >>> Hi Hans, > > >>> > > >>> Thanks for your help to give some good advice. > > >>> On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: > > >>>> > > >>>>>>>> In any case, using a control to switch to secure mode and using > > >>>> a control > > >>>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to > > >>>> me. > > >>>>>>>> > > >>>>>>>> I was wondering if it wouldn't be better to create a new > > >>>> V4L2_MEMORY_ type, > > >>>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > > >>>> ensures that > > >>>>>>>> once you create buffers for the first time, the driver can > > >>>> switch into secure > > >>>>>>>> mode, and until all buffers are released again you know that the > > >>>> driver will > > >>>>>>>> stay in secure mode. > > >>>>>>> > > >>>>>>> Why do you think the control for setting secure mode is a poor > > >>>> choice? > > >>>>>>> There's various places in the driver code where functionality > > >>>> changes > > >>>>>>> based on being secure/non-secure mode, so this is very much a > > >>>> 'global' > > >>>>>>> setting for the driver. It could be inferred based off a new > > >>>> memory > > >>>>>>> type for the queues...which then sets that flag in the driver; > > >>>> but > > >>>>>>> that seems like it would be more fragile and would require > > >>>> checking > > >>>>>>> for incompatible output/capture memory types. I'm not against > > >>>> another > > >>>>>>> way of doing this; but didn't see why you think the proposed > > >>>> method is > > >>>>>>> a poor choice. > > >>>>>> > > >>>>>> I assume you are either decoding to secure memory all the time, or > > >>>> not > > >>>>>> at all. That's something you would want to select the moment you > > >>>> allocate > > >>>>>> the first buffer. Using the V4L2_MEMORY_ value would be the > > >>>> natural place > > >>>>>> for that. A control can typically be toggled at any time, and it > > >>>> makes > > >>>>>> no sense to do that for secure streaming. > > >>>>>> > > >>>>>> Related to that: if you pass a dmabuf fd you will need to check > > >>>> somewhere > > >>>>>> if the fd points to secure memory or not. You don't want to mix > > >>>> the two > > >>>>>> but you want to check that at VIDIOC_QBUF time. > > >>>>>> > > >>>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 > > >>>> core, > > >>>>>> drivers do not need to do that. > > >>>>> > > >>>>> Just to clarify a bit, and make sure I understand this too. You are > > >>>> proposing to > > >>>>> introduce something like: > > >>>>> > > >>>>> V4L2_MEMORY_SECURE_DMABUF > > >>>>> > > >>>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while > > >>>> telling the > > >>>>> driver that the memory is secure according to the definition of > > >>>> "secure" for the > > >>>>> platform its running on. > > >>>>> > > >>>>> This drivers also allocate secure SHM (a standard tee concept) and > > >>>> have internal > > >>>>> allocation for reconstruction buffer and some hw specific reference > > >>>> metadata. So > > >>>>> the idea would be that it would keep allocation using the dmabuf > > >>>> heap internal > > >>>>> APIs ? And decide which type of memory based on the memory type > > >>>> found in the > > >>>>> queue? > > >>>> > > >>>> Yes. Once you request the first buffer you basically tell the driver > > >>>> whether it > > >>>> will operate in secure or non-secure mode, and that stays that way > > >>>> until all > > >>>> buffers are freed. I think that makes sense. > > >>>> > > >>> > > >>> According to iommu's information, the dma operation for secure and non- > > >>> secure are the same, whether just need to add one memory type in v4l2 > > >>> framework the same as V4L2_MEMORY_DMABUF? The dma operation in > > >>> videobuf2-dma-contig.c can use the same functions. > > >> > > >> So if I pass a non-secure dma fd to the capture queue of the codec, who > > >> will check that it can't write the data to that fd? Since doing so would > > >> expose the video. Presumably at some point the tee code will prevent that? > > >> (I sincerely hope so!) > > > > > > It is entirely the job of the TEE to prevent this. Nothing in the > > > kernel should allow exploitation of what happens in the TEE no matter > > > what goes on in the kernel > > > > > >> > > >> Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the > > >> driver that 1) it can expect secure dmabuf fds, 2) it can configure itself > > >> for that (that avoids using a control to toggle between normal and secure mode), > > >> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the > > >> fd that is passed in is for secure memory. This means that mistakes by > > >> userspace are caught at QBUF time. > > >> > > >> Of course, this will not protect you (people can disable this check by > > >> recompiling the kernel), that still has to be done by the firmware, but > > >> it catches userspace errors early on. > > >> > > >> Also, while for this hardware the DMA operation is the same, that might > > >> not be the case for other hardware. > > > > > > That's a really good point. So one of the other models that is used > > > for secure video decoding is to send the encrypted buffer into the > > > video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in > > > all the corresponding crypto parameters (i.e. algorithm, IV, > > > encryption pattern, etc.). Then the video driver internally does the > > > decryption and decode in one operation. That's not what we want to > > > use here for Mediatek; but I've done other integrations that work that > > > way (that was for VAAPI [1], not V4L2...but there are other ARM > > > implementations that do operate that way). So if we end up requiring > > > V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on > > > output+capture, that'll close off other potential solutions in the > > > future. > > > > > > Expanding on your point about DMA operations being different on > > > various hardware, that also makes me think a general check for this in > > > v4l2 code may also be limiting. There are various ways secure video > > > pipelines are done, so leaving these checks up to the individual > > > drivers that implement secure video decode may be more pragmatic. If > > > there's a generic V4L2 _CID_SECURE_MODE control, that makes it more > > > general for how drivers can handle secure video decode. > > > > No, using a control for this is really wrong. > > > > The reason why I want it as a separate memory type is that that is > > what you use when you call VIDIOC_REQBUFS, and that ioctl is also > > when things are locked down in a driver. As long as no buffers have > > been allocated, you can still change formats, parameters, etc. But > > once buffers are allocated, most of that can't be changed, since > > changing e.g. the format would also change the buffer sizes. > > > > It also locks down who owns the buffers by storing the file descriptor. > > This prevents other processes from hijacking the I/O streaming, only > > the owner can stream buffers. > > > > So it is a natural point in the sequence for selecting secure > > buffers. > > > > If you request V4L2_MEMORY_DMABUF_SECURE for the output, then the > > capture side must also use DMABUF_SECURE. Whether or not you can > > use regular DMABUF for the output side and select DMABUF_SECURE > > on the capture side is a driver decision. It can be useful to > > support this for testing the secure capture using regular video > > streams (something Nicolas discussed as well), but it depends on > > the hardware whether you can use that technique. > > OK, that does work for the additional cases I mentioned. And for > testing...we would still want to use DMABUF_SECURE on both ends for > Mediatek at least (that's the only way they support it). But rather > than having to bother with a clearkey implementation...we can just do > something that directly copies compressed video into the secure > dmabufs and then exercises the whole pipeline from there. This same > thing happens with the 'clear lead' that is sometimes there with > encrypted video (where the first X seconds are unencrypted and then it > switches to encrypted...but you're still using the secure video > pipeline on the unencrypted frames in that case). > > > > > > Regards, > > > > Hans > > > > > > > > [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 > > > > > >> > > >> Regards, > > >> > > >> Hans > > >> > > >>> > > >>> Best Regards, > > >>> Yunfei Dong > > >>> > > >> > >
On 26/09/2023 22:59, Jeffrey Kardatzke wrote: > Hans, > > I've been looking through the v4l2/vbuf2 code to get an idea of the > details for implementing a new memory type for secure buffers. What > it comes down to essentially is that it would behave just like > V4L2_MEMORY_DMABUF, but then there would be an extra check in > __prepare_dmabuf (in videobuf2-core.c) when the memory type is SECURE > to ensure that it is actually from a secure dma-buf allocation. So > I'm thinking an alternate solution might be cleaner so we don't have > two memory types that are handled nearly identically in most of the > code. What do you think about a new memory flag like > V4L2_MEMORY_FLAG_SECURE? This would be set in vb2_queue struct like > the other existing memory flag. Then when it gets into > __prepare_dmabuf and invokes attach_dmabuf on each buffer...that call > could then check for the existence of that flag, and if it's there it > could validate it is actually secure memory. Then in various other > dmabuf vb2_mem_ops (maybe alloc, get_userptr, vaddr and mmap) those > could also check for the secure flag, and if present return an > error/null. Then also in the driver specific vb2_ops for queue_setup, > the MTK driver could recognize the flag there and then configure > itself for secure mode. > > How does that sound as an overall strategy? Yes, I actually had the same thought. You would also need a new capability: V4L2_BUF_CAP_SUPPORTS_SECURE_MEMORY It makes more sense than creating a new V4L2_MEMORY_ type, and it still is handled at the right place (creating the buffers). Regards, Hans > > Cheers, > Jeff > > On Mon, Sep 25, 2023 at 9:51 AM Jeffrey Kardatzke <jkardatzke@google.com> wrote: >> >> On Mon, Sep 25, 2023 at 2:00 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >>> >>> On 22/09/2023 21:17, Jeffrey Kardatzke wrote: >>>> On Fri, Sep 22, 2023 at 1:44 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: >>>>> >>>>> On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: >>>>>> Hi Hans, >>>>>> >>>>>> Thanks for your help to give some good advice. >>>>>> On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: >>>>>>> >>>>>>>>>>> In any case, using a control to switch to secure mode and using >>>>>>> a control >>>>>>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to >>>>>>> me. >>>>>>>>>>> >>>>>>>>>>> I was wondering if it wouldn't be better to create a new >>>>>>> V4L2_MEMORY_ type, >>>>>>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That >>>>>>> ensures that >>>>>>>>>>> once you create buffers for the first time, the driver can >>>>>>> switch into secure >>>>>>>>>>> mode, and until all buffers are released again you know that the >>>>>>> driver will >>>>>>>>>>> stay in secure mode. >>>>>>>>>> >>>>>>>>>> Why do you think the control for setting secure mode is a poor >>>>>>> choice? >>>>>>>>>> There's various places in the driver code where functionality >>>>>>> changes >>>>>>>>>> based on being secure/non-secure mode, so this is very much a >>>>>>> 'global' >>>>>>>>>> setting for the driver. It could be inferred based off a new >>>>>>> memory >>>>>>>>>> type for the queues...which then sets that flag in the driver; >>>>>>> but >>>>>>>>>> that seems like it would be more fragile and would require >>>>>>> checking >>>>>>>>>> for incompatible output/capture memory types. I'm not against >>>>>>> another >>>>>>>>>> way of doing this; but didn't see why you think the proposed >>>>>>> method is >>>>>>>>>> a poor choice. >>>>>>>>> >>>>>>>>> I assume you are either decoding to secure memory all the time, or >>>>>>> not >>>>>>>>> at all. That's something you would want to select the moment you >>>>>>> allocate >>>>>>>>> the first buffer. Using the V4L2_MEMORY_ value would be the >>>>>>> natural place >>>>>>>>> for that. A control can typically be toggled at any time, and it >>>>>>> makes >>>>>>>>> no sense to do that for secure streaming. >>>>>>>>> >>>>>>>>> Related to that: if you pass a dmabuf fd you will need to check >>>>>>> somewhere >>>>>>>>> if the fd points to secure memory or not. You don't want to mix >>>>>>> the two >>>>>>>>> but you want to check that at VIDIOC_QBUF time. >>>>>>>>> >>>>>>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 >>>>>>> core, >>>>>>>>> drivers do not need to do that. >>>>>>>> >>>>>>>> Just to clarify a bit, and make sure I understand this too. You are >>>>>>> proposing to >>>>>>>> introduce something like: >>>>>>>> >>>>>>>> V4L2_MEMORY_SECURE_DMABUF >>>>>>>> >>>>>>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while >>>>>>> telling the >>>>>>>> driver that the memory is secure according to the definition of >>>>>>> "secure" for the >>>>>>>> platform its running on. >>>>>>>> >>>>>>>> This drivers also allocate secure SHM (a standard tee concept) and >>>>>>> have internal >>>>>>>> allocation for reconstruction buffer and some hw specific reference >>>>>>> metadata. So >>>>>>>> the idea would be that it would keep allocation using the dmabuf >>>>>>> heap internal >>>>>>>> APIs ? And decide which type of memory based on the memory type >>>>>>> found in the >>>>>>>> queue? >>>>>>> >>>>>>> Yes. Once you request the first buffer you basically tell the driver >>>>>>> whether it >>>>>>> will operate in secure or non-secure mode, and that stays that way >>>>>>> until all >>>>>>> buffers are freed. I think that makes sense. >>>>>>> >>>>>> >>>>>> According to iommu's information, the dma operation for secure and non- >>>>>> secure are the same, whether just need to add one memory type in v4l2 >>>>>> framework the same as V4L2_MEMORY_DMABUF? The dma operation in >>>>>> videobuf2-dma-contig.c can use the same functions. >>>>> >>>>> So if I pass a non-secure dma fd to the capture queue of the codec, who >>>>> will check that it can't write the data to that fd? Since doing so would >>>>> expose the video. Presumably at some point the tee code will prevent that? >>>>> (I sincerely hope so!) >>>> >>>> It is entirely the job of the TEE to prevent this. Nothing in the >>>> kernel should allow exploitation of what happens in the TEE no matter >>>> what goes on in the kernel >>>> >>>>> >>>>> Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the >>>>> driver that 1) it can expect secure dmabuf fds, 2) it can configure itself >>>>> for that (that avoids using a control to toggle between normal and secure mode), >>>>> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the >>>>> fd that is passed in is for secure memory. This means that mistakes by >>>>> userspace are caught at QBUF time. >>>>> >>>>> Of course, this will not protect you (people can disable this check by >>>>> recompiling the kernel), that still has to be done by the firmware, but >>>>> it catches userspace errors early on. >>>>> >>>>> Also, while for this hardware the DMA operation is the same, that might >>>>> not be the case for other hardware. >>>> >>>> That's a really good point. So one of the other models that is used >>>> for secure video decoding is to send the encrypted buffer into the >>>> video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in >>>> all the corresponding crypto parameters (i.e. algorithm, IV, >>>> encryption pattern, etc.). Then the video driver internally does the >>>> decryption and decode in one operation. That's not what we want to >>>> use here for Mediatek; but I've done other integrations that work that >>>> way (that was for VAAPI [1], not V4L2...but there are other ARM >>>> implementations that do operate that way). So if we end up requiring >>>> V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on >>>> output+capture, that'll close off other potential solutions in the >>>> future. >>>> >>>> Expanding on your point about DMA operations being different on >>>> various hardware, that also makes me think a general check for this in >>>> v4l2 code may also be limiting. There are various ways secure video >>>> pipelines are done, so leaving these checks up to the individual >>>> drivers that implement secure video decode may be more pragmatic. If >>>> there's a generic V4L2 _CID_SECURE_MODE control, that makes it more >>>> general for how drivers can handle secure video decode. >>> >>> No, using a control for this is really wrong. >>> >>> The reason why I want it as a separate memory type is that that is >>> what you use when you call VIDIOC_REQBUFS, and that ioctl is also >>> when things are locked down in a driver. As long as no buffers have >>> been allocated, you can still change formats, parameters, etc. But >>> once buffers are allocated, most of that can't be changed, since >>> changing e.g. the format would also change the buffer sizes. >>> >>> It also locks down who owns the buffers by storing the file descriptor. >>> This prevents other processes from hijacking the I/O streaming, only >>> the owner can stream buffers. >>> >>> So it is a natural point in the sequence for selecting secure >>> buffers. >>> >>> If you request V4L2_MEMORY_DMABUF_SECURE for the output, then the >>> capture side must also use DMABUF_SECURE. Whether or not you can >>> use regular DMABUF for the output side and select DMABUF_SECURE >>> on the capture side is a driver decision. It can be useful to >>> support this for testing the secure capture using regular video >>> streams (something Nicolas discussed as well), but it depends on >>> the hardware whether you can use that technique. >> >> OK, that does work for the additional cases I mentioned. And for >> testing...we would still want to use DMABUF_SECURE on both ends for >> Mediatek at least (that's the only way they support it). But rather >> than having to bother with a clearkey implementation...we can just do >> something that directly copies compressed video into the secure >> dmabufs and then exercises the whole pipeline from there. This same >> thing happens with the 'clear lead' that is sometimes there with >> encrypted video (where the first X seconds are unencrypted and then it >> switches to encrypted...but you're still using the secure video >> pipeline on the unencrypted frames in that case). >> >> >>> >>> Regards, >>> >>> Hans >>> >>>> >>>> [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 >>>> >>>>> >>>>> Regards, >>>>> >>>>> Hans >>>>> >>>>>> >>>>>> Best Regards, >>>>>> Yunfei Dong >>>>>> >>>>> >>>
Sounds great Hans! I'll work with Mediatek to update their code for that. On Wed, Sep 27, 2023 at 12:26 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > > On 26/09/2023 22:59, Jeffrey Kardatzke wrote: > > Hans, > > > > I've been looking through the v4l2/vbuf2 code to get an idea of the > > details for implementing a new memory type for secure buffers. What > > it comes down to essentially is that it would behave just like > > V4L2_MEMORY_DMABUF, but then there would be an extra check in > > __prepare_dmabuf (in videobuf2-core.c) when the memory type is SECURE > > to ensure that it is actually from a secure dma-buf allocation. So > > I'm thinking an alternate solution might be cleaner so we don't have > > two memory types that are handled nearly identically in most of the > > code. What do you think about a new memory flag like > > V4L2_MEMORY_FLAG_SECURE? This would be set in vb2_queue struct like > > the other existing memory flag. Then when it gets into > > __prepare_dmabuf and invokes attach_dmabuf on each buffer...that call > > could then check for the existence of that flag, and if it's there it > > could validate it is actually secure memory. Then in various other > > dmabuf vb2_mem_ops (maybe alloc, get_userptr, vaddr and mmap) those > > could also check for the secure flag, and if present return an > > error/null. Then also in the driver specific vb2_ops for queue_setup, > > the MTK driver could recognize the flag there and then configure > > itself for secure mode. > > > > How does that sound as an overall strategy? > > Yes, I actually had the same thought. > > You would also need a new capability: V4L2_BUF_CAP_SUPPORTS_SECURE_MEMORY > > It makes more sense than creating a new V4L2_MEMORY_ type, and it still > is handled at the right place (creating the buffers). > > Regards, > > Hans > > > > > Cheers, > > Jeff > > > > On Mon, Sep 25, 2023 at 9:51 AM Jeffrey Kardatzke <jkardatzke@google.com> wrote: > >> > >> On Mon, Sep 25, 2023 at 2:00 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > >>> > >>> On 22/09/2023 21:17, Jeffrey Kardatzke wrote: > >>>> On Fri, Sep 22, 2023 at 1:44 AM Hans Verkuil <hverkuil-cisco@xs4all.nl> wrote: > >>>>> > >>>>> On 22/09/2023 05:28, Yunfei Dong (董云飞) wrote: > >>>>>> Hi Hans, > >>>>>> > >>>>>> Thanks for your help to give some good advice. > >>>>>> On Wed, 2023-09-20 at 09:20 +0200, Hans Verkuil wrote: > >>>>>>> > >>>>>>>>>>> In any case, using a control to switch to secure mode and using > >>>>>>> a control > >>>>>>>>>>> to convert a dmabuf fd to a secure handle seems a poor choice to > >>>>>>> me. > >>>>>>>>>>> > >>>>>>>>>>> I was wondering if it wouldn't be better to create a new > >>>>>>> V4L2_MEMORY_ type, > >>>>>>>>>>> e.g. V4L2_MEMORY_DMABUF_SECURE (or perhaps _DMABUF_OPTEE). That > >>>>>>> ensures that > >>>>>>>>>>> once you create buffers for the first time, the driver can > >>>>>>> switch into secure > >>>>>>>>>>> mode, and until all buffers are released again you know that the > >>>>>>> driver will > >>>>>>>>>>> stay in secure mode. > >>>>>>>>>> > >>>>>>>>>> Why do you think the control for setting secure mode is a poor > >>>>>>> choice? > >>>>>>>>>> There's various places in the driver code where functionality > >>>>>>> changes > >>>>>>>>>> based on being secure/non-secure mode, so this is very much a > >>>>>>> 'global' > >>>>>>>>>> setting for the driver. It could be inferred based off a new > >>>>>>> memory > >>>>>>>>>> type for the queues...which then sets that flag in the driver; > >>>>>>> but > >>>>>>>>>> that seems like it would be more fragile and would require > >>>>>>> checking > >>>>>>>>>> for incompatible output/capture memory types. I'm not against > >>>>>>> another > >>>>>>>>>> way of doing this; but didn't see why you think the proposed > >>>>>>> method is > >>>>>>>>>> a poor choice. > >>>>>>>>> > >>>>>>>>> I assume you are either decoding to secure memory all the time, or > >>>>>>> not > >>>>>>>>> at all. That's something you would want to select the moment you > >>>>>>> allocate > >>>>>>>>> the first buffer. Using the V4L2_MEMORY_ value would be the > >>>>>>> natural place > >>>>>>>>> for that. A control can typically be toggled at any time, and it > >>>>>>> makes > >>>>>>>>> no sense to do that for secure streaming. > >>>>>>>>> > >>>>>>>>> Related to that: if you pass a dmabuf fd you will need to check > >>>>>>> somewhere > >>>>>>>>> if the fd points to secure memory or not. You don't want to mix > >>>>>>> the two > >>>>>>>>> but you want to check that at VIDIOC_QBUF time. > >>>>>>>>> > >>>>>>>>> Note that the V4L2_MEMORY_ value is already checked in the v4l2 > >>>>>>> core, > >>>>>>>>> drivers do not need to do that. > >>>>>>>> > >>>>>>>> Just to clarify a bit, and make sure I understand this too. You are > >>>>>>> proposing to > >>>>>>>> introduce something like: > >>>>>>>> > >>>>>>>> V4L2_MEMORY_SECURE_DMABUF > >>>>>>>> > >>>>>>>> Which like V4L2_MEMORY_DMABUF is meant to import dmabuf, while > >>>>>>> telling the > >>>>>>>> driver that the memory is secure according to the definition of > >>>>>>> "secure" for the > >>>>>>>> platform its running on. > >>>>>>>> > >>>>>>>> This drivers also allocate secure SHM (a standard tee concept) and > >>>>>>> have internal > >>>>>>>> allocation for reconstruction buffer and some hw specific reference > >>>>>>> metadata. So > >>>>>>>> the idea would be that it would keep allocation using the dmabuf > >>>>>>> heap internal > >>>>>>>> APIs ? And decide which type of memory based on the memory type > >>>>>>> found in the > >>>>>>>> queue? > >>>>>>> > >>>>>>> Yes. Once you request the first buffer you basically tell the driver > >>>>>>> whether it > >>>>>>> will operate in secure or non-secure mode, and that stays that way > >>>>>>> until all > >>>>>>> buffers are freed. I think that makes sense. > >>>>>>> > >>>>>> > >>>>>> According to iommu's information, the dma operation for secure and non- > >>>>>> secure are the same, whether just need to add one memory type in v4l2 > >>>>>> framework the same as V4L2_MEMORY_DMABUF? The dma operation in > >>>>>> videobuf2-dma-contig.c can use the same functions. > >>>>> > >>>>> So if I pass a non-secure dma fd to the capture queue of the codec, who > >>>>> will check that it can't write the data to that fd? Since doing so would > >>>>> expose the video. Presumably at some point the tee code will prevent that? > >>>>> (I sincerely hope so!) > >>>> > >>>> It is entirely the job of the TEE to prevent this. Nothing in the > >>>> kernel should allow exploitation of what happens in the TEE no matter > >>>> what goes on in the kernel > >>>> > >>>>> > >>>>> Having a separate V4L2_MEMORY_DMABUF_SECURE type is to indicate to the > >>>>> driver that 1) it can expect secure dmabuf fds, 2) it can configure itself > >>>>> for that (that avoids using a control to toggle between normal and secure mode), > >>>>> and at VIDIOC_QBUF time it is easy for the V4L2 core to verify that the > >>>>> fd that is passed in is for secure memory. This means that mistakes by > >>>>> userspace are caught at QBUF time. > >>>>> > >>>>> Of course, this will not protect you (people can disable this check by > >>>>> recompiling the kernel), that still has to be done by the firmware, but > >>>>> it catches userspace errors early on. > >>>>> > >>>>> Also, while for this hardware the DMA operation is the same, that might > >>>>> not be the case for other hardware. > >>>> > >>>> That's a really good point. So one of the other models that is used > >>>> for secure video decoding is to send the encrypted buffer into the > >>>> video decoder directly (i.e. V4L2_MEMORY_MMAP) and then also send in > >>>> all the corresponding crypto parameters (i.e. algorithm, IV, > >>>> encryption pattern, etc.). Then the video driver internally does the > >>>> decryption and decode in one operation. That's not what we want to > >>>> use here for Mediatek; but I've done other integrations that work that > >>>> way (that was for VAAPI [1], not V4L2...but there are other ARM > >>>> implementations that do operate that way). So if we end up requiring > >>>> V4L2_MEMORY_DMABUF_SECURE to indicate secure mode and enforce it on > >>>> output+capture, that'll close off other potential solutions in the > >>>> future. > >>>> > >>>> Expanding on your point about DMA operations being different on > >>>> various hardware, that also makes me think a general check for this in > >>>> v4l2 code may also be limiting. There are various ways secure video > >>>> pipelines are done, so leaving these checks up to the individual > >>>> drivers that implement secure video decode may be more pragmatic. If > >>>> there's a generic V4L2 _CID_SECURE_MODE control, that makes it more > >>>> general for how drivers can handle secure video decode. > >>> > >>> No, using a control for this is really wrong. > >>> > >>> The reason why I want it as a separate memory type is that that is > >>> what you use when you call VIDIOC_REQBUFS, and that ioctl is also > >>> when things are locked down in a driver. As long as no buffers have > >>> been allocated, you can still change formats, parameters, etc. But > >>> once buffers are allocated, most of that can't be changed, since > >>> changing e.g. the format would also change the buffer sizes. > >>> > >>> It also locks down who owns the buffers by storing the file descriptor. > >>> This prevents other processes from hijacking the I/O streaming, only > >>> the owner can stream buffers. > >>> > >>> So it is a natural point in the sequence for selecting secure > >>> buffers. > >>> > >>> If you request V4L2_MEMORY_DMABUF_SECURE for the output, then the > >>> capture side must also use DMABUF_SECURE. Whether or not you can > >>> use regular DMABUF for the output side and select DMABUF_SECURE > >>> on the capture side is a driver decision. It can be useful to > >>> support this for testing the secure capture using regular video > >>> streams (something Nicolas discussed as well), but it depends on > >>> the hardware whether you can use that technique. > >> > >> OK, that does work for the additional cases I mentioned. And for > >> testing...we would still want to use DMABUF_SECURE on both ends for > >> Mediatek at least (that's the only way they support it). But rather > >> than having to bother with a clearkey implementation...we can just do > >> something that directly copies compressed video into the secure > >> dmabufs and then exercises the whole pipeline from there. This same > >> thing happens with the 'clear lead' that is sometimes there with > >> encrypted video (where the first X seconds are unencrypted and then it > >> switches to encrypted...but you're still using the secure video > >> pipeline on the unencrypted frames in that case). > >> > >> > >>> > >>> Regards, > >>> > >>> Hans > >>> > >>>> > >>>> [1] - https://github.com/intel/libva/blob/master/va/va.h#L2177 > >>>> > >>>>> > >>>>> Regards, > >>>>> > >>>>> Hans > >>>>> > >>>>>> > >>>>>> Best Regards, > >>>>>> Yunfei Dong > >>>>>> > >>>>> > >>> >
diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c index d2b09ce9f1cf..a981178c25d9 100644 --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c @@ -535,6 +535,17 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl *ctrl) ctrl->val = mtk_dma_contig_get_secure_handle(ctx, ctrl->val); mtk_v4l2_vdec_dbg(3, ctx, "get secure handle: %d => 0x%x", sec_fd, ctrl->val); break; + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: + ctx->is_svp_mode = ctrl->val; + + if (ctx->is_svp_mode) { + ret = mtk_vcodec_dec_optee_open(ctx->dev->optee_private); + if (ret) + mtk_v4l2_vdec_err(ctx, "open secure mode failed."); + else + mtk_v4l2_vdec_dbg(3, ctx, "decoder in secure mode: %d", ctrl->val); + } + break; default: mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: 0x%x\n", hdr_ctrl->id); return ret; @@ -573,7 +584,7 @@ static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_dec_ctx *ctx) unsigned int i; struct v4l2_ctrl *ctrl; - v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 2); if (ctx->ctrl_hdl.error) { mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); return ctx->ctrl_hdl.error; @@ -592,6 +603,8 @@ static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_dec_ctx *ctx) ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops, V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE, 0, 65535, 1, 0); + ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops, + V4L2_CID_MPEG_MTK_SET_SECURE_MODE, 0, 65535, 1, 0); v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c b/drivers/media/v4l2-core/v4l2-ctrls-defs.c index d8cf01f76aab..a507045a3f30 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c @@ -1042,6 +1042,7 @@ const char *v4l2_ctrl_get_name(u32 id) case V4L2_CID_MPEG_VIDEO_REF_NUMBER_FOR_PFRAMES: return "Reference Frames for a P-Frame"; case V4L2_CID_MPEG_VIDEO_PREPEND_SPSPPS_TO_IDR: return "Prepend SPS and PPS to IDR"; case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE: return "MediaTek Decoder get secure handle"; + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: return "MediaTek Decoder set secure mode"; /* AV1 controls */ case V4L2_CID_MPEG_VIDEO_AV1_PROFILE: return "AV1 Profile"; @@ -1442,6 +1443,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, *type = V4L2_CTRL_TYPE_INTEGER; *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; break; + case V4L2_CID_MPEG_MTK_SET_SECURE_MODE: + *type = V4L2_CTRL_TYPE_INTEGER; + *flags |= V4L2_CTRL_FLAG_WRITE_ONLY; + break; case V4L2_CID_USER_CLASS: case V4L2_CID_CAMERA_CLASS: case V4L2_CID_CODEC_CLASS: diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h index 7b3694985366..88e90d943e38 100644 --- a/include/uapi/linux/v4l2-controls.h +++ b/include/uapi/linux/v4l2-controls.h @@ -957,6 +957,7 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { /* MPEG-class control IDs specific to the MediaTek Decoder driver as defined by V4L2 */ #define V4L2_CID_MPEG_MTK_BASE (V4L2_CTRL_CLASS_CODEC | 0x2000) #define V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE (V4L2_CID_MPEG_MTK_BASE+8) +#define V4L2_CID_MPEG_MTK_SET_SECURE_MODE (V4L2_CID_MPEG_MTK_BASE+9) /* Camera class control IDs */