From patchwork Mon Sep 11 12:59:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yunfei Dong X-Patchwork-Id: 13379293 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0AF55EE57DF for ; Mon, 11 Sep 2023 13:00:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date :Subject:CC:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=i8lf/NQkzqcIVQSywOYprcMBzCK/pk6C1DTn4KOOSno=; b=PVaZj65MSXHJ8Tr0BP/pZmwvd9 2Pn2yYexvlvOeC69DFPvFwF7X3VwgqG8aNvGWJw89DpImiiOcp8JzZXbFDVMaMdRNzHbsPQOVEy64 sMmidHlD0yax8Z97gZN8pkyzTjHR4pI05EF03Vg7cI9OQZ2FN+ddaTV8EYTUBx+N/ZnlOguorYCfb 7+fPCqK0bLvTPCWYtE4Jcmjyz9dprdSRY1no7+kkgWOBpuPT+qOL8x8r+lxX1azuXHIGCwHdqnxpH MQdw2WWzwKrBKRlrbwdSmcuoNtbCg7GHsnrCdeFLH65Zb2cjzvdxCMxqBYCG7oIBTe6OTONkpu7ps UzcphPpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qfgWd-000XtS-2s; Mon, 11 Sep 2023 13:00:23 +0000 Received: from mailgw02.mediatek.com ([216.200.240.185]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qfgWZ-000Xr0-0p; Mon, 11 Sep 2023 13:00:21 +0000 X-UUID: 267129e250a311ee86758d4a7c00f3a0-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=i8lf/NQkzqcIVQSywOYprcMBzCK/pk6C1DTn4KOOSno=; b=X0gx9aQ/p5/JiTK8FqIJduCAeVnzm8jpeTN1GdySvXrubHshipc4F6ZbsqJxE8/OJlXCCNM4mLn/goXTd3OS6qtRpfNhBTBj5MpzVgDbGlYDsyQ5gvEB9SCXba9coQJoyXOHnaBwZ8BJ9BbmJZHRULYbO5X116AQNV4aQggkVOI=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.31,REQID:95c298ac-8bcf-4c49-8ac7-f9c4ef343b62,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:0ad78a4,CLOUDID:d1c4e4c2-1e57-4345-9d31-31ad9818b39f,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: 267129e250a311ee86758d4a7c00f3a0-20230911 Received: from mtkmbs11n1.mediatek.inc [(172.21.101.185)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 985499648; Mon, 11 Sep 2023 06:00:14 -0700 Received: from mtkmbs13n2.mediatek.inc (172.21.101.194) by mtkmbs10n2.mediatek.inc (172.21.101.183) 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:49 +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:48 +0800 From: Yunfei Dong To: =?utf-8?q?N=C3=ADcolas_F_=2E_R_=2E_A_=2E_Prado?= , Nicolas Dufresne , Hans Verkuil , AngeloGioacchino Del Regno , Benjamin Gaignard , Nathan Hebert CC: Chen-Yu Tsai , Hsin-Yi Wang , Fritz Koenig , Daniel Vetter , "Steve Cho" , Yunfei Dong , , , , , , Subject: [PATCH 11/14] media: medkatek: vcodec: covert secure fd to secure handle Date: Mon, 11 Sep 2023 20:59:33 +0800 Message-ID: <20230911125936.10648-12-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 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--12.534500-8.000000 X-TMASE-MatchedRID: P/hT4jlJcUSvVT6bfkFW/hafLXbshfog0uu9IYFQ6w7wTO+AdP0/mjcG i0cOVVasTiQecD9aKoRYo3G+rvxrNVc6r+JSY5jbtw+xHnsmQjNbAoaK+wS4jV6Ic79Y87/7tqn 6zdLiUfc1C2f8ND2KYOY2eV25VpuBroQgRZf2+t7nx2TmxvCbKCi16p3/NdojZ5yuplze9pt6Ra SpHJq/AIiyX6xrn0xcn5u7YzlPkLcm4SY1UdFN+ZA6S0SjvcYU1M2UyPXKJXkUtdRZTmEaIQOku VkkJKW7Ntt/7UYCnkS1L0WZKCITP3uLfgSBmtT0dXu122+iJtqFKXsoBdprwpsoi2XrUn/Jn6Kd MrRsL14qtq5d3cxkNZd/mwLf2BVUgrupyzvJJhmo24LNhuZ3v3/2nGZwJmNDFPsCg1345/BgO21 BQaodlQ== X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--12.534500-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: AA197EF0F13D4AED8583911B1312E64C584D5BAA8073AC167BEE49505D876A7A2000:8 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230911_060019_339611_E7E98265 X-CRM114-Status: GOOD ( 17.38 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org User driver will fill or parse data in optee-os with secure handle, need to covert secure fd to secure handle in kernel. Signed-off-by: Yunfei Dong --- .../vcodec/decoder/mtk_vcodec_dec_drv.c | 1 + .../vcodec/decoder/mtk_vcodec_dec_stateless.c | 54 ++++++++++++++++++- drivers/media/v4l2-core/v4l2-ctrls-defs.c | 5 ++ include/uapi/linux/v4l2-controls.h | 4 ++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c index 0a89ce452ac3..64e006820f43 100644 --- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c @@ -571,3 +571,4 @@ module_platform_driver(mtk_vcodec_dec_driver); MODULE_LICENSE("GPL v2"); MODULE_DESCRIPTION("Mediatek video codec V4L2 decoder driver"); +MODULE_IMPORT_NS(DMA_BUF); 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 2ea517883a86..d2b09ce9f1cf 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 @@ -426,6 +426,46 @@ static int mtk_vcodec_get_pic_info(struct mtk_vcodec_dec_ctx *ctx) return ret; } +static int mtk_dma_contig_get_secure_handle(struct mtk_vcodec_dec_ctx *ctx, int fd) +{ + int secure_handle = 0; + struct dma_buf *buf; + struct dma_buf_attachment *dba; + struct sg_table *sgt; + struct device *dev = &ctx->dev->plat_dev->dev; + + buf = dma_buf_get(fd); + if (IS_ERR(buf)) { + mtk_v4l2_vdec_err(ctx, "dma_buf_get fail fd:%d", fd); + return 0; + } + + dba = dma_buf_attach(buf, dev); + if (IS_ERR(dba)) { + mtk_v4l2_vdec_err(ctx, "dma_buf_attach fail fd:%d", fd); + goto err_attach; + } + + sgt = dma_buf_map_attachment(dba, DMA_BIDIRECTIONAL); + if (IS_ERR(sgt)) { + mtk_v4l2_vdec_err(ctx, "dma_buf_map_attachment fail fd:%d", fd); + goto err_map; + } + secure_handle = sg_dma_address(sgt->sgl); + + dma_buf_unmap_attachment(dba, sgt, DMA_BIDIRECTIONAL); + dma_buf_detach(buf, dba); + dma_buf_put(buf); + + return secure_handle; +err_map: + dma_buf_detach(buf, dba); +err_attach: + dma_buf_put(buf); + + return 0; +} + static int mtk_vdec_s_ctrl(struct v4l2_ctrl *ctrl) { struct mtk_vcodec_dec_ctx *ctx = ctrl_to_dec_ctx(ctrl); @@ -436,7 +476,7 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl *ctrl) struct v4l2_ctrl *hdr_ctrl; const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; const struct mtk_video_fmt *fmt; - int i = 0, ret = 0; + int i = 0, ret = 0, sec_fd; hdr_ctrl = ctrl; if (!hdr_ctrl || !hdr_ctrl->p_new.p) @@ -489,6 +529,12 @@ static int mtk_vdec_s_ctrl(struct v4l2_ctrl *ctrl) return -EINVAL; } break; + case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE: + sec_fd = ctrl->val; + + 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; default: mtk_v4l2_vdec_dbg(3, ctx, "Not supported to set ctrl id: 0x%x\n", hdr_ctrl->id); return ret; @@ -525,8 +571,9 @@ static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = { 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); + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, NUM_CTRLS + 1); if (ctx->ctrl_hdl.error) { mtk_v4l2_vdec_err(ctx, "v4l2_ctrl_handler_init failed\n"); return ctx->ctrl_hdl.error; @@ -543,6 +590,9 @@ 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); + v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); return 0; diff --git a/drivers/media/v4l2-core/v4l2-ctrls-defs.c b/drivers/media/v4l2-core/v4l2-ctrls-defs.c index 8696eb1cdd61..d8cf01f76aab 100644 --- a/drivers/media/v4l2-core/v4l2-ctrls-defs.c +++ b/drivers/media/v4l2-core/v4l2-ctrls-defs.c @@ -1041,6 +1041,7 @@ const char *v4l2_ctrl_get_name(u32 id) case V4L2_CID_MPEG_VIDEO_HEVC_SIZE_OF_LENGTH_FIELD: return "HEVC Size of Length Field"; 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"; /* AV1 controls */ case V4L2_CID_MPEG_VIDEO_AV1_PROFILE: return "AV1 Profile"; @@ -1437,6 +1438,10 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type, case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES: *type = V4L2_CTRL_TYPE_INTEGER_MENU; break; + case V4L2_CID_MPEG_MTK_GET_SECURE_HANDLE: + *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 c3604a0a3e30..7b3694985366 100644 --- a/include/uapi/linux/v4l2-controls.h +++ b/include/uapi/linux/v4l2-controls.h @@ -954,6 +954,10 @@ enum v4l2_mpeg_mfc51_video_force_frame_type { #define V4L2_CID_MPEG_MFC51_VIDEO_H264_ADAPTIVE_RC_STATIC (V4L2_CID_CODEC_MFC51_BASE+53) #define V4L2_CID_MPEG_MFC51_VIDEO_H264_NUM_REF_PIC_FOR_P (V4L2_CID_CODEC_MFC51_BASE+54) +/* 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) + /* Camera class control IDs */ #define V4L2_CID_CAMERA_CLASS_BASE (V4L2_CTRL_CLASS_CAMERA | 0x900)