From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56147C07E95 for ; Tue, 20 Jul 2021 01:49:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3398D61164 for ; Tue, 20 Jul 2021 01:49:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243590AbhGTBIL (ORCPT ); Mon, 19 Jul 2021 21:08:11 -0400 Received: from mail-am6eur05on2071.outbound.protection.outlook.com ([40.107.22.71]:27941 "EHLO EUR05-AM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S243588AbhGTBFD (ORCPT ); Mon, 19 Jul 2021 21:05:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eELvfgDkz0GR/w0i9bcnAiHbXCxHBf+p2yJxpR49YH37qDDLLhlyIRIDXwW8Dl2LEYpyP6R0Ct1OkmUe3XvbBwmauJp2AIBshDInPS5KdpqHrqWj+0UamtnMt02uQOrjSXFmStXDDmC9R1SU59owN+N/ONlA4A+5AaebmYLlKOGUOkhMTtspivL1y/WZxv5ld9MhAeJ4i1hq9LoozCmuUUuEhBlTucvRz0ySYFMxTlMVFI04T1DOuws5QnXgoOEaBZdw7d7IFvxL3ifLv+PFX74PfhK32TPmvYd+LFCPgHYLbfkJq1rrHGpnA824UG4Biey50k5pxIhfb6Tg748Nfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1y4rkSJANxliO/w9ndAhU88nN+dRyI1hT+ebtKJDLT8=; b=OjCYqmsgVF/qVPBe0eJZrKmId/Tv79o/NHhSAHQRe8AjVNihuYpw3zK5VRvB7jc3wBe54urrdcw1ySwUgumv7bo5NiBIQWf9eunAwrGYMdSHWneJENbhBUCgsMHMMPa8/R/Ji8J1lnEM+qBRzE1THsd4ZzxlSCCbQ82lrm6cQm0lbRIXuYIwr0DLxBO7Si8k6fnYT4X7/OM4sOrQ82xCyrt2mM0Qq4SRnRu06GSu9Fq23xvZwrzfaa7lOX5KoWvcuQjhv+gxpM+vSYJ8r9zCySK6RJ8spPDe6xzom/75H2pBWT89/1AvucBhJTYqg5oETvUyKJ3y2QQn1H+ZsvRzfw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1y4rkSJANxliO/w9ndAhU88nN+dRyI1hT+ebtKJDLT8=; b=ILlzDhv0YRORvP9x6wmoNdD2WiV2JJsPKppEAAPv7CMSYbMHbyPOpnE9kfxW2Yu241Z24KNejxqV5OC00rsKa1lHCfq5DtnQf7p1HqvKBTk/L6pILAERo/qRbKqc7ZXvbWHAFZQsoozuEObsPA/AUpqOJbuvIfKrdB2j94/NKsw= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from AM6PR04MB6341.eurprd04.prod.outlook.com (2603:10a6:20b:d8::14) by AM6PR0402MB3813.eurprd04.prod.outlook.com (2603:10a6:209:21::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.21; Tue, 20 Jul 2021 01:44:22 +0000 Received: from AM6PR04MB6341.eurprd04.prod.outlook.com ([fe80::81e3:1e06:83ee:5db8]) by AM6PR04MB6341.eurprd04.prod.outlook.com ([fe80::81e3:1e06:83ee:5db8%3]) with mapi id 15.20.4331.034; Tue, 20 Jul 2021 01:44:22 +0000 From: Ming Qian To: mchehab@kernel.org, shawnguo@kernel.org, robh+dt@kernel.org, s.hauer@pengutronix.de Cc: hverkuil-cisco@xs4all.nl, kernel@pengutronix.de, festevam@gmail.com, linux-imx@nxp.com, aisheng.dong@nxp.com, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 06/13] media: imx: imx8q: add vpu v4l2 m2m support Date: Tue, 20 Jul 2021 09:43:25 +0800 Message-Id: <63d131af38c90bdf2797ff9967ef0271491bc10a.1626743758.git.ming.qian@nxp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SG2PR06CA0232.apcprd06.prod.outlook.com (2603:1096:4:ac::16) To AM6PR04MB6341.eurprd04.prod.outlook.com (2603:10a6:20b:d8::14) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from lsv11149.swis.cn-sha01.nxp.com (119.31.174.70) by SG2PR06CA0232.apcprd06.prod.outlook.com (2603:1096:4:ac::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.21 via Frontend Transport; Tue, 20 Jul 2021 01:44:18 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 81fdaa54-dd69-4913-bab8-08d94b1fe5a2 X-MS-TrafficTypeDiagnostic: AM6PR0402MB3813: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:350; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z/cPbqY2gMf6EooFgaHbaYKjZnz9N+jG8nYwIZSi/HzEV8hvzLiooraG75MAJfxK2f+oUe91N0zcbS2IOXU6UEgKN75CPJu0MLUcQKCF5rMiu180fDOKH7HFTBITvXNEA5Sbe7cTllsy1/UTEaf9/hM+SQaxJ8n2TxQ6vkkY0UmWRjjrOrmD0wEnK8TayWpBIdqt74EgpohIc20YAysYr9u8rEPuM1ktj5J9wTSeZiFGeOhHRrH7BUdnDJVh89f9C0DsROFNFNhue7Ba8M24NuvR1j303zy3HKQOjuVg01RA+jFJz62d7eoyKloO171q2VZjEHRyxhf2Q5HKVneXXwdv6ARnP/x31lgHYOhVap4vGMshsTv84FE9yhZZEXPfvAyFXSpJ0TxIDZ7hdkaPzA+C/uurj07NVP51U6mh2AI8loucZtRv2dR59XQZnFQuuos2p2cb2pxFldfdaS0mwbG3UXGBNuEHP1blKG+UDmWIu8AZNT/FnCT0leaywk/JkSmqJm76Cq6yP9afjabBUgaqyyoRbcT8AqdhaD+fQdPT1uYWNga7c4epCaj2BlOwSKr3nuNZCKEqmpKxk3F0DO1Dj3Bk2g+2suhUuh3CbqkQzYzXoamLG88r9IZeKQFBhd5rP31pq66gPr3P/sCc8HJSdIx/JF+sBy7Yo3/V+fIjmw+h8hQOUDiF+WCVgm898/SgPIrJpHVB0v4g3khoE16bs07rkK+MyIBiXymAAN0nXVQduHpEu5gP5r3EOjOtvOlhOn/PRTImlcoa9IP4ve98WW2f9d0Xw3/5fU/2H3nctdmwHM19r133oN0ErdRX3sHVf02JWQXFS64ucoJQ5g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM6PR04MB6341.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(36756003)(30864003)(316002)(8676002)(7416002)(4326008)(86362001)(6666004)(186003)(66476007)(66946007)(66556008)(38100700002)(966005)(26005)(38350700002)(6486002)(83380400001)(508600001)(44832011)(5660300002)(956004)(2906002)(2616005)(8936002)(7696005)(52116002)(32563001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?hk1DNFNJ7x4Y5RfnnsSAiEV35Eu/5XgAm45GR5yS4pzd6pFu4+/DXF4rmJ84?= =?us-ascii?Q?pGOmHD5vGzeBfWOTLnj28Dae6JuSYmC9r+l43kf8tKU2Lxehr9D7JIc9l3pd?= =?us-ascii?Q?009sxWrRq6XH6OBKCnTL0BOuJpyF5szgTfADdbKPGJgbq3+pixZ3UTkXBM+d?= =?us-ascii?Q?6iYHZVMnzgvdipiT0yYHSeiYovWMCnJhVrz2EYBQZ497rFjWsByqLI3WKamp?= =?us-ascii?Q?b7G7Qf85NSKIrBa8uwbNEmwFKQBRaYXlMoTNUpjGwvEoWGXF+ZaGrMHz2RWU?= =?us-ascii?Q?QkwqN3X8IswIC7JhVibqMF1eDmj+WEmjHot4wsuEH/xdACjVnNwQiRr45gkD?= =?us-ascii?Q?3PVQtgzN1VozKIM5Die7xNhECC8ZmM0ihfLzoUFuDzIjBTP5YgeUuy0BbdRV?= =?us-ascii?Q?HlfqIKOJFU0PsGaFGeztS+yg6ylM38KIzGhwjSXxaLGzut785xXozdUjvubK?= =?us-ascii?Q?0y4vAidP5ZrGru3TJP8E5Zb6TuCX2JLQNHtHEcNHkQWo4ggagyJAB8/oSu00?= =?us-ascii?Q?fWXnU8bqNUf/yw8VMTgeoChl5g1fTNbAr4CXgoISeKzHGyB5hY7JCzMlzfmD?= =?us-ascii?Q?EkEfehYNhPej3BnYpy5F8poQFFGUVl+jT26HQ3d/FQn7mIRIK1nUivvmdmqb?= =?us-ascii?Q?0RZHap9UlGnkPwQVqGAD3G59W7lJbxl3i2PcYhVR1rk45kjnUgwWzIjRytlO?= =?us-ascii?Q?pZuhL02LfCKH5mTK50g6UW0WPDkZaq5sIgMf9APA02ZiR3/TUrKHcXCW5t6H?= =?us-ascii?Q?mg6QH+p37PCYfOGOzLbdsbbgrdT4gSmuUYH1C3bl8u2wZ7uW2uHOKIfBJScP?= =?us-ascii?Q?sFV9jXmjYCVNcIhsSbvuCw/ln/3wLQCye/nBg7i0q/0K5scPor7cGvR5frLf?= =?us-ascii?Q?BIesobhy9SKj/ayHZ0Vh9jnfiYBi8zUqBovZmz9aIFbslLSZCfrRT9U56KgC?= =?us-ascii?Q?sF98jxmZ0stOO2AD5v8lq3Ml/lI3QAyYMJx/UnuU78MzjQn4Uyr0pMHlWQxG?= =?us-ascii?Q?zXe6gOJGv0Eq8ZN5mVxY6F7gJN+/SmDbbChVsUDSBZq7VnLDLJ/CiOlUdRcu?= =?us-ascii?Q?mPHwjn+9MHQqd+A0oYCkDNyG8YSZW4LDfqCHZsM78ZOLPCnzy3CA7S/n+jBV?= =?us-ascii?Q?KGE6nUBPqP9K6iDsYkM2i+kQ9G4Atvml5FcnpCMQivi53VxIEq8CE8XZBYfg?= =?us-ascii?Q?MoYSMaX9XXJl2IumCpQ3QEYZ1Fpy2HtGNkdaxAegb0snH/Zo8gsFWRAC5x9w?= =?us-ascii?Q?wJ0Y4YDqx9/NM8xCV6k09Y0YymWN+P9JI3SSAZzgHeWTcM5LXUxgX7uE5gZu?= =?us-ascii?Q?w/CPEx+tu8kVckuo7oQAlamH?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 81fdaa54-dd69-4913-bab8-08d94b1fe5a2 X-MS-Exchange-CrossTenant-AuthSource: AM6PR04MB6341.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jul 2021 01:44:22.0501 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: fXquCTS9mMED0LjI4zWsiC7Pds0jY8uezCzxXL28NADB4T0zNloNIqgeUMFPeYTeDb1lreMV2/ZDJPW2pVHSVA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR0402MB3813 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org vpu_v4l2.c implements the v4l2 m2m driver methods. vpu_helpers.c implements the common helper functions vpu_color.c converts the v4l2 colorspace with iso Signed-off-by: Ming Qian Signed-off-by: Shijie Qin Signed-off-by: Zhou Peng --- drivers/media/platform/imx/vpu-8q/vpu_color.c | 201 ++++++ .../media/platform/imx/vpu-8q/vpu_helpers.c | 405 +++++++++++ .../media/platform/imx/vpu-8q/vpu_helpers.h | 80 +++ drivers/media/platform/imx/vpu-8q/vpu_v4l2.c | 662 ++++++++++++++++++ drivers/media/platform/imx/vpu-8q/vpu_v4l2.h | 53 ++ 5 files changed, 1401 insertions(+) create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_color.c create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_helpers.c create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_helpers.h create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_v4l2.c create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_v4l2.h diff --git a/drivers/media/platform/imx/vpu-8q/vpu_color.c b/drivers/media/platform/imx/vpu-8q/vpu_color.c new file mode 100644 index 000000000000..62030a3db0ee --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_color.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#define TAG "COLOR" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "vpu.h" +#include "vpu_helpers.h" + +static const u8 colorprimaries[] = { + 0, + V4L2_COLORSPACE_REC709, /*Rec. ITU-R BT.709-6*/ + 0, + 0, + V4L2_COLORSPACE_470_SYSTEM_M, /*Rec. ITU-R BT.470-6 System M*/ + V4L2_COLORSPACE_470_SYSTEM_BG,/*Rec. ITU-R BT.470-6 System B, G*/ + V4L2_COLORSPACE_SMPTE170M, /*SMPTE170M*/ + V4L2_COLORSPACE_SMPTE240M, /*SMPTE240M*/ + V4L2_COLORSPACE_GENERIC_FILM, /*Generic film*/ + V4L2_COLORSPACE_BT2020, /*Rec. ITU-R BT.2020-2*/ + V4L2_COLORSPACE_ST428 /*SMPTE ST 428-1*/ +}; + +static const u8 colortransfers[] = { + 0, + V4L2_XFER_FUNC_709, /*Rec. ITU-R BT.709-6*/ + 0, + 0, + V4L2_XFER_FUNC_GAMMA22, /*Rec. ITU-R BT.470-6 System M*/ + V4L2_XFER_FUNC_GAMMA28, /*Rec. ITU-R BT.470-6 System B, G*/ + V4L2_XFER_FUNC_709, /*SMPTE170M*/ + V4L2_XFER_FUNC_SMPTE240M,/*SMPTE240M*/ + V4L2_XFER_FUNC_LINEAR, /*Linear transfer characteristics*/ + 0, + 0, + V4L2_XFER_FUNC_XVYCC, /*IEC 61966-2-4*/ + V4L2_XFER_FUNC_BT1361, /*Rec. ITU-R BT.1361-0 extended colour gamut*/ + V4L2_XFER_FUNC_SRGB, /*IEC 61966-2-1 sRGB or sYCC*/ + V4L2_XFER_FUNC_709, /*Rec. ITU-R BT.2020-2 (10 bit system)*/ + V4L2_XFER_FUNC_709, /*Rec. ITU-R BT.2020-2 (12 bit system)*/ + V4L2_XFER_FUNC_SMPTE2084,/*SMPTE ST 2084*/ + V4L2_XFER_FUNC_ST428, /*SMPTE ST 428-1*/ + V4L2_XFER_FUNC_HLG /*Rec. ITU-R BT.2100-0 hybrid log-gamma (HLG)*/ +}; + +static const u8 colormatrixcoefs[] = { + 0, + V4L2_YCBCR_ENC_709, /*Rec. ITU-R BT.709-6*/ + 0, + 0, + V4L2_YCBCR_ENC_BT470_6M, /*Title 47 Code of Federal Regulations*/ + V4L2_YCBCR_ENC_601, /*Rec. ITU-R BT.601-7 625*/ + V4L2_YCBCR_ENC_601, /*Rec. ITU-R BT.601-7 525*/ + V4L2_YCBCR_ENC_SMPTE240M, /*SMPTE240M*/ + 0, + V4L2_YCBCR_ENC_BT2020, /*Rec. ITU-R BT.2020-2*/ + V4L2_YCBCR_ENC_BT2020_CONST_LUM /*Rec. ITU-R BT.2020-2 constant*/ +}; + +u32 vpu_color_cvrt_primaries_v2i(u32 primaries) +{ + return VPU_ARRAY_FIND(colorprimaries, primaries); +} + +u32 vpu_color_cvrt_primaries_i2v(u32 primaries) +{ + return VPU_ARRAY_AT(colorprimaries, primaries); +} + +u32 vpu_color_cvrt_transfers_v2i(u32 transfers) +{ + return VPU_ARRAY_FIND(colortransfers, transfers); +} + +u32 vpu_color_cvrt_transfers_i2v(u32 transfers) +{ + return VPU_ARRAY_AT(colortransfers, transfers); +} + +u32 vpu_color_cvrt_matrix_v2i(u32 matrix) +{ + return VPU_ARRAY_FIND(colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_matrix_i2v(u32 matrix) +{ + return VPU_ARRAY_AT(colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_full_range_v2i(u32 full_range) +{ + return (full_range == V4L2_QUANTIZATION_FULL_RANGE); +} + +u32 vpu_color_cvrt_full_range_i2v(u32 full_range) +{ + if (full_range) + return V4L2_QUANTIZATION_FULL_RANGE; + + return V4L2_QUANTIZATION_LIM_RANGE; +} + +int vpu_color_check_primaries(u32 primaries) +{ + return vpu_color_cvrt_primaries_v2i(primaries) ? 0 : -EINVAL; +} + +int vpu_color_check_transfers(u32 transfers) +{ + return vpu_color_cvrt_transfers_v2i(transfers) ? 0 : -EINVAL; +} + +int vpu_color_check_matrix(u32 matrix) +{ + return vpu_color_cvrt_matrix_v2i(matrix) ? 0 : -EINVAL; +} + +int vpu_color_check_full_range(u32 full_range) +{ + int ret = -EINVAL; + + switch (full_range) { + case V4L2_QUANTIZATION_FULL_RANGE: + case V4L2_QUANTIZATION_LIM_RANGE: + ret = 0; + break; + default: + break; + + } + + return ret; +} + +int vpu_color_get_default(u32 primaries, + u32 *ptransfers, u32 *pmatrix, u32 *pfull_range) +{ + u32 transfers; + u32 matrix; + u32 full_range; + + switch (primaries) { + case V4L2_COLORSPACE_REC709: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_709; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_COLORSPACE_470_SYSTEM_M: + case V4L2_COLORSPACE_470_SYSTEM_BG: + case V4L2_COLORSPACE_SMPTE170M: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_601; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_COLORSPACE_SMPTE240M: + transfers = V4L2_XFER_FUNC_SMPTE240M; + matrix = V4L2_YCBCR_ENC_SMPTE240M; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_COLORSPACE_BT2020: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_BT2020; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + default: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_709; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + } + + if (ptransfers) + *ptransfers = transfers; + if (pmatrix) + *pmatrix = matrix; + if (pfull_range) + *pfull_range = full_range; + + + return 0; +} diff --git a/drivers/media/platform/imx/vpu-8q/vpu_helpers.c b/drivers/media/platform/imx/vpu-8q/vpu_helpers.c new file mode 100644 index 000000000000..c21da2f26163 --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_helpers.c @@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#define TAG "HELPER" +#include +#include +#include +#include +#include +#include +#include +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_rpc.h" +#include "vpu_helpers.h" +#include "vpu_log.h" + +int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x) +{ + int i; + + for (i = 0; i < size; i++) { + if (array[i] == x) + return i; + } + + return 0; +} + +bool vpu_helper_check_type(struct vpu_inst *inst, u32 type) +{ + const struct vpu_format *pfmt; + + for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { + if (vpu_core_check_fmt(inst->core, pfmt->pixfmt)) + continue; + if (pfmt->type == type) + return true; + } + + return false; +} + +const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt) +{ + const struct vpu_format *pfmt; + + if (!inst || !inst->formats) + return NULL; + + if (vpu_core_check_fmt(inst->core, pixelfmt)) + return NULL; + + for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { + if (vpu_core_check_fmt(inst->core, pfmt->pixfmt)) + continue; + + if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type)) + return pfmt; + } + + return NULL; +} + +const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index) +{ + const struct vpu_format *pfmt; + int i = 0; + + if (!inst || !inst->formats) + return NULL; + + for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { + if (vpu_core_check_fmt(inst->core, pfmt->pixfmt)) + continue; + + if (pfmt->type == type) { + if (index == i) + return pfmt; + i++; + } + } + + return NULL; +} + +u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width) +{ + const struct vpu_core_resources *res; + + if (!inst || !inst->core || !inst->core->res) + return width; + + res = inst->core->res; + if (res->max_width) + width = clamp(width, res->min_width, res->max_width); + if (res->step_width) + width = ALIGN(width, res->step_width); + + return width; +} + +u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height) +{ + const struct vpu_core_resources *res; + + if (!inst || !inst->core || !inst->core->res) + return height; + + res = inst->core->res; + if (res->max_height) + height = clamp(height, res->min_height, res->max_height); + if (res->step_height) + height = ALIGN(height, res->step_height); + + return height; +} + +u32 vpu_helper_get_frame_size(u32 fmt, u32 width, u32 height) +{ + switch (fmt) { + case V4L2_PIX_FMT_NV12: + return ((width * 3) >> 1) * height; + default: + return width * height; + } +} + +u32 vpu_helper_get_plane_size(u32 fmt, u32 width, u32 height, int plane_no, u32 stride) +{ + width = ALIGN(width, stride); + + switch (fmt) { + case V4L2_PIX_FMT_NV12: + if (plane_no == 0) + return width * height; + else if (plane_no == 1) + return (width >> 1) * height; + else + return 0; + default: + if (plane_no == 0) + return width * height; + else + return 0; + } +} + +u32 vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *rptr, u32 size, void *dst) +{ + u32 offset; + u32 start; + u32 end; + void *virt; + + if (!stream_buffer || !rptr || !dst) + return -EINVAL; + + if (!size) + return 0; + + offset = *rptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + + if (offset < start || offset > end) { + vpu_err("rptr 0x%x is out of range [0x%x, 0x%x]\n", + offset, start, end); + return -EINVAL; + } + + if (offset + size <= end) { + memcpy(dst, virt + (offset - start), size); + } else { + memcpy(dst, virt + (offset - start), end - offset); + memcpy(dst + end - offset, virt, size + offset - end); + } + + *rptr = vpu_helper_step_walk(stream_buffer, offset, size); + return size; +} + +u32 vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u32 size, void *src) +{ + u32 offset; + u32 start; + u32 end; + void *virt; + + if (!stream_buffer || !wptr || !src) + return -EINVAL; + + if (!size) + return 0; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + if (offset < start || offset > end) { + vpu_err("wptr 0x%x is out of range [0x%x, 0x%x]\n", + offset, start, end); + return -EINVAL; + } + + if (offset + size <= end) { + memcpy(virt + (offset - start), src, size); + } else { + memcpy(virt + (offset - start), src, end - offset); + memcpy(virt, src + end - offset, size + offset - end); + } + + *wptr = vpu_helper_step_walk(stream_buffer, offset, size); + + return size; +} + +u32 vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u8 val, u32 size) +{ + u32 offset; + u32 start; + u32 end; + void *virt; + + if (!stream_buffer || !wptr) + return -EINVAL; + + if (!size) + return 0; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + if (offset < start || offset > end) { + vpu_err("wptr 0x%x is out of range [0x%x, 0x%x]\n", + offset, start, end); + return -EINVAL; + } + + if (offset + size <= end) { + memset(virt + (offset - start), val, size); + } else { + memset(virt + (offset - start), val, end - offset); + memset(virt, val, size + offset - end); + } + + offset += size; + if (offset >= end) + offset -= stream_buffer->length; + + *wptr = offset; + + return size; +} + +u32 vpu_helper_get_free_space(struct vpu_inst *inst) +{ + struct vpu_rpc_buffer_desc desc; + + if (vpu_iface_get_stream_buffer_desc(inst, &desc)) + return 0; + + if (desc.rptr > desc.wptr) + return desc.rptr - desc.wptr; + else if (desc.rptr < desc.wptr) + return (desc.end - desc.start + desc.rptr - desc.wptr); + else + return desc.end - desc.start; +} + +u32 vpu_helper_get_used_space(struct vpu_inst *inst) +{ + struct vpu_rpc_buffer_desc desc; + + if (vpu_iface_get_stream_buffer_desc(inst, &desc)) + return 0; + + if (desc.wptr > desc.rptr) + return desc.wptr - desc.rptr; + else if (desc.wptr < desc.rptr) + return (desc.end - desc.start + desc.wptr - desc.rptr); + else + return 0; +} + +int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl) +{ + struct vpu_inst *inst = ctrl_to_inst(ctrl); + + switch (ctrl->id) { + case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: + ctrl->val = inst->min_buffer_cap; + break; + case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: + ctrl->val = inst->min_buffer_out; + break; + default: + return -EINVAL; + } + + return 0; +} + +u32 vpu_helper_calc_coprime(u32 *a, u32 *b) +{ + int m = *a; + int n = *b; + + if (m == 0) + return n; + if (n == 0) + return m; + + while (n != 0) { + int tmp = m % n; + + m = n; + n = tmp; + } + *a = (*a) / m; + *b = (*b) / m; + + return m; +} + +#define READ_BYTE(buffer, pos) (*(u8 *)((buffer)->virt + ((pos) % buffer->length))) +int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer, + u32 pixelformat, u32 offset, u32 bytesused) +{ + u32 start_code; + int start_code_size; + u32 val = 0; + int i; + int ret = -EINVAL; + + if (!stream_buffer || !stream_buffer->virt) + return -EINVAL; + + switch (pixelformat) { + case V4L2_PIX_FMT_H264: + start_code_size = 4; + start_code = 0x00000001; + break; + default: + return 0; + } + + for (i = 0; i < bytesused; i++) { + val = (val << 8) | READ_BYTE(stream_buffer, offset + i); + if (i < start_code_size - 1) + continue; + if (val == start_code) { + ret = i + 1 - start_code_size; + break; + } + } + + return ret; +} + +int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src) +{ + u32 i; + + if (!pairs || !cnt) + return -EINVAL; + + for (i = 0; i < cnt; i++) { + if (pairs[i].src == src) + return pairs[i].dst; + } + + return -EINVAL; +} + +int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst) +{ + u32 i; + + if (!pairs || !cnt) + return -EINVAL; + + for (i = 0; i < cnt; i++) { + if (pairs[i].dst == dst) + return pairs[i].src; + } + + return -EINVAL; +} diff --git a/drivers/media/platform/imx/vpu-8q/vpu_helpers.h b/drivers/media/platform/imx/vpu-8q/vpu_helpers.h new file mode 100644 index 000000000000..3f0acda8522c --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_helpers.h @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#ifndef _IMX_VPU_HELPERS_H +#define _IMX_VPU_HELPERS_H + +struct vpu_pair { + u32 src; + u32 dst; +}; + +#define MAKE_TIMESTAMP(s, ns) (((s32)(s) * NSEC_PER_SEC) + (ns)) +#define VPU_INVALID_TIMESTAMP MAKE_TIMESTAMP(-1, 0) +#define VPU_ARRAY_AT(array, i) (((i) < ARRAY_SIZE(array)) ? array[i] : 0) +#define VPU_ARRAY_FIND(array, x) vpu_helper_find_in_array_u8(array, ARRAY_SIZE(array), x) + +int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x); +bool vpu_helper_check_type(struct vpu_inst *inst, u32 type); +const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt); +const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index); +u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width); +u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height); +u32 vpu_helper_get_frame_size(u32 fmt, u32 width, u32 height); +u32 vpu_helper_get_plane_size(u32 fmt, u32 width, u32 height, int plane_no, u32 stride); +u32 vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *rptr, u32 size, void *dst); +u32 vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u32 size, void *src); +u32 vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u8 val, u32 size); +u32 vpu_helper_get_free_space(struct vpu_inst *inst); +u32 vpu_helper_get_used_space(struct vpu_inst *inst); +int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl); +u32 vpu_helper_calc_coprime(u32 *a, u32 *b); +void vpu_helper_get_kmp_next(const u8 *pattern, int *next, int size); +int vpu_helper_kmp_search(u8 *s, int s_len, const u8 *p, int p_len, int *next); +int vpu_helper_kmp_search_in_stream_buffer(struct vpu_buffer *stream_buffer, + u32 offset, int bytesused, + const u8 *p, int p_len, int *next); +int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer, + u32 pixelformat, u32 offset, u32 bytesused); + +static inline u32 vpu_helper_step_walk(struct vpu_buffer *stream_buffer, u32 pos, u32 step) +{ + pos += step; + if (pos > stream_buffer->phys + stream_buffer->length) + pos -= stream_buffer->length; + + return pos; +} + +int vpu_color_check_primaries(u32 primaries); +int vpu_color_check_transfers(u32 transfers); +int vpu_color_check_matrix(u32 matrix); +int vpu_color_check_full_range(u32 full_range); +u32 vpu_color_cvrt_primaries_v2i(u32 primaries); +u32 vpu_color_cvrt_primaries_i2v(u32 primaries); +u32 vpu_color_cvrt_transfers_v2i(u32 transfers); +u32 vpu_color_cvrt_transfers_i2v(u32 transfers); +u32 vpu_color_cvrt_matrix_v2i(u32 matrix); +u32 vpu_color_cvrt_matrix_i2v(u32 matrix); +u32 vpu_color_cvrt_full_range_v2i(u32 full_range); +u32 vpu_color_cvrt_full_range_i2v(u32 full_range); +int vpu_color_get_default(u32 primaries, + u32 *ptransfers, u32 *pmatrix, u32 *pfull_range); + +int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src); +int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst); +#endif diff --git a/drivers/media/platform/imx/vpu-8q/vpu_v4l2.c b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.c new file mode 100644 index 000000000000..c9dd976ea16f --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.c @@ -0,0 +1,662 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#define TAG "V4L2" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_v4l2.h" +#include "vpu_msgs.h" +#include "vpu_helpers.h" +#include "vpu_log.h" + +void vpu_inst_lock(struct vpu_inst *inst) +{ + mutex_lock(&inst->lock); +} + +void vpu_inst_unlock(struct vpu_inst *inst) +{ + mutex_unlock(&inst->lock); +} + +dma_addr_t vpu_get_vb_phy_addr(struct vb2_buffer *vb, u32 plane_no) +{ + return vb2_dma_contig_plane_dma_addr(vb, plane_no) + + vb->planes[plane_no].data_offset; +} + +unsigned int vpu_get_vb_length(struct vb2_buffer *vb, u32 plane_no) +{ + if (plane_no >= vb->num_planes) + return 0; + return vb2_plane_size(vb, plane_no) - vb->planes[plane_no].data_offset; +} + +void vpu_v4l2_set_error(struct vpu_inst *inst) +{ + struct vb2_queue *src_q; + struct vb2_queue *dst_q; + + src_q = v4l2_m2m_get_src_vq(inst->m2m_ctx); + dst_q = v4l2_m2m_get_dst_vq(inst->m2m_ctx); + if (src_q) + src_q->error = 1; + if (dst_q) + dst_q->error = 1; +} + +int vpu_notify_eos(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_EOS + }; + + inst_dbg(inst, LVL_FLOW, "notify eos event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + + return 0; +} + +int vpu_notify_source_change(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_SOURCE_CHANGE, + .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION + }; + + inst_dbg(inst, LVL_FLOW, "notify source change event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + return 0; +} + +int vpu_notify_skip(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_SKIP, + .u.data[0] = 0xff, + }; + + inst_dbg(inst, LVL_FLOW, "notify skip event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + + return 0; +} + +int vpu_notify_codec_error(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_CODEC_ERROR, + }; + + inst_dbg(inst, LVL_FLOW, "notify error event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + vpu_v4l2_set_error(inst); + + return 0; +} + +const struct vpu_format *vpu_try_fmt_common(struct vpu_inst *inst, + struct v4l2_format *f) +{ + struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; + u32 type = f->type; + u32 stride; + u32 bytesperline; + u32 sizeimage; + const struct vpu_format *fmt; + int i; + + fmt = vpu_helper_find_format(inst, type, pixmp->pixelformat); + if (!fmt) { + fmt = vpu_helper_enum_format(inst, type, 0); + if (!fmt) + return NULL; + pixmp->pixelformat = fmt->pixfmt; + } + + stride = inst->core->res->stride; + pixmp->width = vpu_helper_valid_frame_width(inst, pixmp->width); + pixmp->height = vpu_helper_valid_frame_height(inst, pixmp->height); + pixmp->flags = fmt->flags; + pixmp->num_planes = fmt->num_planes; + if (pixmp->field == V4L2_FIELD_ANY) + pixmp->field = V4L2_FIELD_NONE; + for (i = 0; i < pixmp->num_planes; i++) { + bytesperline = ALIGN(pixmp->width, stride); + sizeimage = vpu_helper_get_plane_size(pixmp->pixelformat, + pixmp->width, pixmp->height, i, stride); + if ((s32)(pixmp->plane_fmt[i].bytesperline) <= 0) + pixmp->plane_fmt[i].bytesperline = bytesperline; + if ((s32)(pixmp->plane_fmt[i].sizeimage) <= 0) + pixmp->plane_fmt[i].sizeimage = sizeimage; + if (pixmp->plane_fmt[i].bytesperline < bytesperline) + pixmp->plane_fmt[i].bytesperline = bytesperline; + if (pixmp->plane_fmt[i].sizeimage <= sizeimage) + pixmp->plane_fmt[i].sizeimage = sizeimage; + } + + return fmt; +} + +static bool vpu_check_ready(struct vpu_inst *inst, u32 type) +{ + if (!inst) + return false; + if (inst->state == VPU_CODEC_STATE_DEINIT || inst->id < 0) + return false; + if (!inst->ops->check_ready) + return true; + return call_vop(inst, check_ready, type); +} + +int vpu_process_output_buffer(struct vpu_inst *inst) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vpu_vb2_buffer *vpu_buf = NULL; + + if (!inst) + return -EINVAL; + + if (!vpu_check_ready(inst, inst->out_format.type)) + return -EINVAL; + + v4l2_m2m_for_each_src_buf(inst->m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); + if (vpu_buf->state == VPU_BUF_STATE_IDLE) + break; + vpu_buf = NULL; + } + + if (!vpu_buf) + return -EINVAL; + + inst_dbg(inst, LVL_DEBUG, "frame id = %d / %d\n", + vpu_buf->m2m_buf.vb.sequence, inst->sequence); + return call_vop(inst, process_output, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +int vpu_process_capture_buffer(struct vpu_inst *inst) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vpu_vb2_buffer *vpu_buf = NULL; + + if (!inst) + return -EINVAL; + + if (!vpu_check_ready(inst, inst->cap_format.type)) + return -EINVAL; + + v4l2_m2m_for_each_dst_buf(inst->m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); + if (vpu_buf->state == VPU_BUF_STATE_IDLE) + break; + vpu_buf = NULL; + } + if (!vpu_buf) + return -EINVAL; + + return call_vop(inst, process_capture, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence(struct vpu_inst *inst, + u32 type, u32 sequence) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vb2_v4l2_buffer *vbuf = NULL; + + if (V4L2_TYPE_IS_OUTPUT(type)) { + v4l2_m2m_for_each_src_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->sequence == sequence) + break; + vbuf = NULL; + } + } else { + v4l2_m2m_for_each_dst_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->sequence == sequence) + break; + vbuf = NULL; + } + } + + return vbuf; +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_idx(struct vpu_inst *inst, + u32 type, u32 idx) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vb2_v4l2_buffer *vbuf = NULL; + + if (V4L2_TYPE_IS_OUTPUT(type)) { + v4l2_m2m_for_each_src_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->vb2_buf.index == idx) + break; + vbuf = NULL; + } + } else { + v4l2_m2m_for_each_dst_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->vb2_buf.index == idx) + break; + vbuf = NULL; + } + } + + return vbuf; +} + +int vpu_get_num_buffers(struct vpu_inst *inst, u32 type) +{ + struct vb2_queue *q; + + if (!inst || !inst->m2m_ctx) + return -EINVAL; + if (V4L2_TYPE_IS_OUTPUT(type)) + q = v4l2_m2m_get_src_vq(inst->m2m_ctx); + else + q = v4l2_m2m_get_dst_vq(inst->m2m_ctx); + + return q->num_buffers; +} + +static void vpu_m2m_device_run(void *priv) +{ +} + +static void vpu_m2m_job_abort(void *priv) +{ + struct vpu_inst *inst = priv; + + v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx); +} + +static const struct v4l2_m2m_ops vpu_m2m_ops = { + .device_run = vpu_m2m_device_run, + .job_abort = vpu_m2m_job_abort +}; + +static int vpu_vb2_queue_setup(struct vb2_queue *vq, + unsigned int *buf_count, + unsigned int *plane_count, + unsigned int psize[], + struct device *allocators[]) +{ + struct vpu_inst *inst = vb2_get_drv_priv(vq); + struct vpu_format *cur_fmt; + int i; + + cur_fmt = vpu_get_format(inst, vq->type); + + if (*plane_count) { + if (*plane_count != cur_fmt->num_planes) + return -EINVAL; + for (i = 0; i < cur_fmt->num_planes; i++) { + if (psize[i] < cur_fmt->sizeimage[i]) + return -EINVAL; + } + } + + *plane_count = cur_fmt->num_planes; + for (i = 0; i < cur_fmt->num_planes; i++) + psize[i] = cur_fmt->sizeimage[i]; + + inst_dbg(inst, LVL_FLOW, "%s queue setup : %u; %u, %u\n", + vpu_type_name(vq->type), + *buf_count, + psize[0], psize[1]); + + return 0; +} + +static int vpu_vb2_buf_init(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf); + + vpu_buf->state = VPU_BUF_STATE_IDLE; + + return 0; +} + +static void vpu_vb2_buf_cleanup(struct vb2_buffer *vb) +{ +} + +static int vpu_vb2_buf_prepare(struct vb2_buffer *vb) +{ + struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue); + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf); + struct vpu_format *cur_fmt; + u32 i; + + cur_fmt = vpu_get_format(inst, vb->type); + if (vb->num_planes != cur_fmt->num_planes) + return -EINVAL; + for (i = 0; i < cur_fmt->num_planes; i++) { + if (vpu_get_vb_length(vb, i) < cur_fmt->sizeimage[i]) { + inst_err(inst, "%s buf[%d] is invalid\n", + vpu_type_name(vb->type), + vb->index); + vpu_buf->state = VPU_BUF_STATE_ERROR; + } + } + + return 0; +} + +static void vpu_vb2_buf_finish(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue); + struct vb2_queue *q = vb->vb2_queue; + + if (vbuf->flags & V4L2_BUF_FLAG_LAST) + vpu_notify_eos(inst); + + if (list_empty(&q->done_list)) + call_vop(inst, on_queue_empty, q->type); +} + +void vpu_vb2_buffers_return(struct vpu_inst *inst, + unsigned int type, enum vb2_buffer_state state) +{ + struct vb2_v4l2_buffer *buf; + + if (!inst || !inst->m2m_ctx) + return; + + if (V4L2_TYPE_IS_OUTPUT(type)) { + while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx))) + v4l2_m2m_buf_done(buf, state); + } else { + while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx))) + v4l2_m2m_buf_done(buf, state); + } +} + +static int vpu_vb2_start_streaming(struct vb2_queue *q, unsigned int count) +{ + struct vpu_inst *inst = vb2_get_drv_priv(q); + int ret; + + vpu_inst_unlock(inst); + ret = vpu_inst_register(inst); + vpu_inst_lock(inst); + if (ret) + return ret; + + vpu_inst_get(inst); + inst_dbg(inst, LVL_FLOW, "%s start streaming : %d\n", + vpu_type_name(q->type), q->num_buffers); + call_vop(inst, start, q->type); + vb2_clear_last_buffer_dequeued(q); + + return 0; +} + +static void vpu_vb2_stop_streaming(struct vb2_queue *q) +{ + struct vpu_inst *inst = vb2_get_drv_priv(q); + + inst_dbg(inst, LVL_FLOW, "%s stop streaming\n", vpu_type_name(q->type)); + + call_vop(inst, stop, q->type); + vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_ERROR); + if (V4L2_TYPE_IS_OUTPUT(q->type)) + inst->sequence = 0; + + vpu_inst_put(inst); +} + +static void vpu_vb2_buf_queue(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue); + + inst_dbg(inst, LVL_DEBUG, "%s buf queue\n", vpu_type_name(vb->type)); + + if (V4L2_TYPE_IS_OUTPUT(vb->type)) { + vbuf->sequence = inst->sequence++; + if ((s64)vb->timestamp < 0) + vb->timestamp = VPU_INVALID_TIMESTAMP; + } + + v4l2_m2m_buf_queue(inst->m2m_ctx, vbuf); + vpu_process_output_buffer(inst); + vpu_process_capture_buffer(inst); +} + +static struct vb2_ops vpu_vb2_ops = { + .queue_setup = vpu_vb2_queue_setup, + .buf_init = vpu_vb2_buf_init, + .buf_cleanup = vpu_vb2_buf_cleanup, + .buf_prepare = vpu_vb2_buf_prepare, + .buf_finish = vpu_vb2_buf_finish, + .start_streaming = vpu_vb2_start_streaming, + .stop_streaming = vpu_vb2_stop_streaming, + .buf_queue = vpu_vb2_buf_queue, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int vpu_m2m_queue_init(void *priv, struct vb2_queue *src_vq, + struct vb2_queue *dst_vq) +{ + struct vpu_inst *inst = priv; + int ret; + + src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; + src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + src_vq->ops = &vpu_vb2_ops; + src_vq->mem_ops = &vb2_dma_contig_memops; + if (inst->type == VPU_CORE_TYPE_DEC && inst->use_stream_buffer) + src_vq->mem_ops = &vb2_vmalloc_memops; + src_vq->drv_priv = inst; + src_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer); + src_vq->allow_zero_bytesused = 1; + src_vq->min_buffers_needed = 1; + src_vq->dev = inst->core->dev; + src_vq->lock = &inst->lock; + ret = vb2_queue_init(src_vq); + if (ret) + return ret; + + dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; + dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + dst_vq->ops = &vpu_vb2_ops; + dst_vq->mem_ops = &vb2_dma_contig_memops; + if (inst->type == VPU_CORE_TYPE_ENC && inst->use_stream_buffer) + dst_vq->mem_ops = &vb2_vmalloc_memops; + dst_vq->drv_priv = inst; + dst_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer); + dst_vq->allow_zero_bytesused = 1; + dst_vq->min_buffers_needed = 1; + dst_vq->dev = inst->core->dev; + dst_vq->lock = &inst->lock; + ret = vb2_queue_init(dst_vq); + if (ret) { + vb2_queue_release(src_vq); + return ret; + } + + return 0; +} + +static int vpu_v4l2_release(struct vpu_inst *inst) +{ + struct vpu_core *core = inst->core; + + inst_dbg(inst, LVL_FLOW, "%s\n", __func__); + + vpu_release_core(core); + + if (inst->workqueue) { + cancel_work_sync(&inst->msg_work); + destroy_workqueue(inst->workqueue); + inst->workqueue = NULL; + } + if (inst->m2m_ctx) { + v4l2_m2m_ctx_release(inst->m2m_ctx); + inst->m2m_ctx = NULL; + } + if (inst->m2m_dev) { + v4l2_m2m_release(inst->m2m_dev); + inst->m2m_dev = NULL; + } + + v4l2_ctrl_handler_free(&inst->ctrl_handler); + mutex_destroy(&inst->lock); + v4l2_fh_del(&inst->fh); + v4l2_fh_exit(&inst->fh); + + call_vop(inst, cleanup); + + return 0; +} + +int vpu_v4l2_open(struct file *file, struct vpu_inst *inst) +{ + struct vpu_dev *vpu = video_drvdata(file); + struct video_device *vdev; + struct vpu_core *core = NULL; + int ret = 0; + + WARN_ON(!file || !inst || !inst->ops); + + if (inst->type == VPU_CORE_TYPE_ENC) + vdev = vpu->vdev_enc; + else + vdev = vpu->vdev_dec; + + mutex_init(&inst->lock); + INIT_LIST_HEAD(&inst->cmd_q); + + inst->id = VPU_INST_NULL_ID; + inst->release = vpu_v4l2_release; + inst->core = vpu_request_core(vpu, inst->type); + + core = inst->core; + if (!core) { + vpu_err("there is no core for %s\n", + vpu_core_type_desc(inst->type)); + return -EINVAL; + } + + inst->min_buffer_cap = 2; + inst->min_buffer_out = 2; + + ret = call_vop(inst, ctrl_init); + if (ret) + goto error; + + inst->m2m_dev = v4l2_m2m_init(&vpu_m2m_ops); + if (IS_ERR(inst->m2m_dev)) { + vpu_err("v4l2_m2m_init fail\n"); + ret = PTR_ERR(inst->m2m_dev); + goto error; + } + + inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, + inst, vpu_m2m_queue_init); + if (IS_ERR(inst->m2m_ctx)) { + vpu_err("v4l2_m2m_ctx_init fail\n"); + ret = PTR_ERR(inst->m2m_dev); + goto error; + } + + v4l2_fh_init(&inst->fh, vdev); + v4l2_fh_add(&inst->fh); + inst->fh.ctrl_handler = &inst->ctrl_handler; + inst->fh.m2m_ctx = inst->m2m_ctx; + file->private_data = &inst->fh; + inst->state = VPU_CODEC_STATE_DEINIT; + inst->workqueue = alloc_workqueue("vpu_inst", WQ_UNBOUND | WQ_MEM_RECLAIM, 1); + if (inst->workqueue) { + INIT_WORK(&inst->msg_work, vpu_inst_run_work); + ret = kfifo_init(&inst->msg_fifo, + inst->msg_buffer, + roundup_pow_of_two(sizeof(inst->msg_buffer))); + if (ret) { + destroy_workqueue(inst->workqueue); + inst->workqueue = NULL; + } + } + atomic_set(&inst->ref_count, 0); + vpu_inst_get(inst); + vpu_dbg(LVL_FLOW, "open, tgid = %d, pid = %d\n", inst->tgid, inst->pid); + + return 0; +error: + if (inst->m2m_ctx) { + v4l2_m2m_ctx_release(inst->m2m_ctx); + inst->m2m_ctx = NULL; + } + if (inst->m2m_dev) { + v4l2_m2m_release(inst->m2m_dev); + inst->m2m_dev = NULL; + } + v4l2_ctrl_handler_free(&inst->ctrl_handler); + vpu_release_core(inst->core); + + return ret; +} + +int vpu_v4l2_close(struct file *file) +{ + struct vpu_inst *inst = to_inst(file); + struct vb2_queue *src_q; + struct vb2_queue *dst_q; + + inst_dbg(inst, LVL_FLOW, "close\n"); + src_q = v4l2_m2m_get_src_vq(inst->m2m_ctx); + dst_q = v4l2_m2m_get_dst_vq(inst->m2m_ctx); + vpu_inst_lock(inst); + if (vb2_is_streaming(src_q)) + v4l2_m2m_streamoff(file, inst->m2m_ctx, src_q->type); + if (vb2_is_streaming(dst_q)) + v4l2_m2m_streamoff(file, inst->m2m_ctx, dst_q->type); + vpu_inst_unlock(inst); + + call_vop(inst, release); + vpu_inst_unregister(inst); + vpu_inst_put(inst); + + return 0; +} diff --git a/drivers/media/platform/imx/vpu-8q/vpu_v4l2.h b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.h new file mode 100644 index 000000000000..87288f0f7625 --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#ifndef _IMX_VPU_V4L2_H +#define _IMX_VPU_V4L2_H + +void vpu_inst_lock(struct vpu_inst *inst); +void vpu_inst_unlock(struct vpu_inst *inst); + +int vpu_v4l2_open(struct file *file, struct vpu_inst *inst); +int vpu_v4l2_close(struct file *file); + +const struct vpu_format *vpu_try_fmt_common(struct vpu_inst *inst, struct v4l2_format *f); +int vpu_process_output_buffer(struct vpu_inst *inst); +int vpu_process_capture_buffer(struct vpu_inst *inst); +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence(struct vpu_inst *inst, u32 type, u32 sequence); +struct vb2_v4l2_buffer *vpu_find_buf_by_idx(struct vpu_inst *inst, u32 type, u32 idx); +void vpu_v4l2_set_error(struct vpu_inst *inst); +int vpu_notify_eos(struct vpu_inst *inst); +int vpu_notify_source_change(struct vpu_inst *inst); +int vpu_notify_skip(struct vpu_inst *inst); +int vpu_notify_codec_error(struct vpu_inst *inst); +void vpu_vb2_buffers_return(struct vpu_inst *inst, + unsigned int type, enum vb2_buffer_state state); +int vpu_get_num_buffers(struct vpu_inst *inst, u32 type); + +dma_addr_t vpu_get_vb_phy_addr(struct vb2_buffer *vb, u32 plane_no); +unsigned int vpu_get_vb_length(struct vb2_buffer *vb, u32 plane_no); +static inline struct vpu_format *vpu_get_format(struct vpu_inst *inst, u32 type) +{ + if (V4L2_TYPE_IS_OUTPUT(type)) + return &inst->out_format; + else + return &inst->cap_format; +} + +static inline char *vpu_type_name(u32 type) +{ + return V4L2_TYPE_IS_OUTPUT(type) ? "output" : "capture"; +} + +#endif -- 2.32.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01577C07E95 for ; Tue, 20 Jul 2021 01:50:30 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id A7F666113B for ; Tue, 20 Jul 2021 01:50:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A7F666113B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nxp.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id: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=Y7yjMxoD9DfPqoTYLaSKKfmdxIAn3zScHzqUEI+DLc0=; b=iTQWuubSr6PAye KNd/LbZzAyHz9JLmrn4Kv96gAspKS+2Nae6/1gnyDd2ep8/QUtWOp1+FxkEKlobmNX24XtF5OYrkz 41AnOyyuPTpO16S3bKLA1Ogijy/wGF6rDPNEWTRGmGV1utioDtvqIBEass/gYf/Bqfl/a/Ds94DEX yrwVB6Pxf50UgxIle0+oHmPl3uESv5REcqVbsDHFu7WbSHr9+jMo8gNiKdz99sWaIm08cLE7f9GN4 s6jfiPWFqo53kddzIQ77vUn09hBpWBrR1Hh/CJHicWsd6J6KG5rsvQ9QsCgWvnj6fObXRbWQX1O4j 9jBrPPXdMuI11H1tXovQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5er8-00BbfA-So; Tue, 20 Jul 2021 01:47:37 +0000 Received: from mail-am6eur05on2075.outbound.protection.outlook.com ([40.107.22.75] helo=EUR05-AM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5eo7-00BaWg-GD for linux-arm-kernel@lists.infradead.org; Tue, 20 Jul 2021 01:44:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eELvfgDkz0GR/w0i9bcnAiHbXCxHBf+p2yJxpR49YH37qDDLLhlyIRIDXwW8Dl2LEYpyP6R0Ct1OkmUe3XvbBwmauJp2AIBshDInPS5KdpqHrqWj+0UamtnMt02uQOrjSXFmStXDDmC9R1SU59owN+N/ONlA4A+5AaebmYLlKOGUOkhMTtspivL1y/WZxv5ld9MhAeJ4i1hq9LoozCmuUUuEhBlTucvRz0ySYFMxTlMVFI04T1DOuws5QnXgoOEaBZdw7d7IFvxL3ifLv+PFX74PfhK32TPmvYd+LFCPgHYLbfkJq1rrHGpnA824UG4Biey50k5pxIhfb6Tg748Nfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1y4rkSJANxliO/w9ndAhU88nN+dRyI1hT+ebtKJDLT8=; b=OjCYqmsgVF/qVPBe0eJZrKmId/Tv79o/NHhSAHQRe8AjVNihuYpw3zK5VRvB7jc3wBe54urrdcw1ySwUgumv7bo5NiBIQWf9eunAwrGYMdSHWneJENbhBUCgsMHMMPa8/R/Ji8J1lnEM+qBRzE1THsd4ZzxlSCCbQ82lrm6cQm0lbRIXuYIwr0DLxBO7Si8k6fnYT4X7/OM4sOrQ82xCyrt2mM0Qq4SRnRu06GSu9Fq23xvZwrzfaa7lOX5KoWvcuQjhv+gxpM+vSYJ8r9zCySK6RJ8spPDe6xzom/75H2pBWT89/1AvucBhJTYqg5oETvUyKJ3y2QQn1H+ZsvRzfw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1y4rkSJANxliO/w9ndAhU88nN+dRyI1hT+ebtKJDLT8=; b=ILlzDhv0YRORvP9x6wmoNdD2WiV2JJsPKppEAAPv7CMSYbMHbyPOpnE9kfxW2Yu241Z24KNejxqV5OC00rsKa1lHCfq5DtnQf7p1HqvKBTk/L6pILAERo/qRbKqc7ZXvbWHAFZQsoozuEObsPA/AUpqOJbuvIfKrdB2j94/NKsw= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=nxp.com; Received: from AM6PR04MB6341.eurprd04.prod.outlook.com (2603:10a6:20b:d8::14) by AM6PR0402MB3813.eurprd04.prod.outlook.com (2603:10a6:209:21::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.21; Tue, 20 Jul 2021 01:44:22 +0000 Received: from AM6PR04MB6341.eurprd04.prod.outlook.com ([fe80::81e3:1e06:83ee:5db8]) by AM6PR04MB6341.eurprd04.prod.outlook.com ([fe80::81e3:1e06:83ee:5db8%3]) with mapi id 15.20.4331.034; Tue, 20 Jul 2021 01:44:22 +0000 From: Ming Qian To: mchehab@kernel.org, shawnguo@kernel.org, robh+dt@kernel.org, s.hauer@pengutronix.de Cc: hverkuil-cisco@xs4all.nl, kernel@pengutronix.de, festevam@gmail.com, linux-imx@nxp.com, aisheng.dong@nxp.com, linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 06/13] media: imx: imx8q: add vpu v4l2 m2m support Date: Tue, 20 Jul 2021 09:43:25 +0800 Message-Id: <63d131af38c90bdf2797ff9967ef0271491bc10a.1626743758.git.ming.qian@nxp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: X-ClientProxiedBy: SG2PR06CA0232.apcprd06.prod.outlook.com (2603:1096:4:ac::16) To AM6PR04MB6341.eurprd04.prod.outlook.com (2603:10a6:20b:d8::14) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from lsv11149.swis.cn-sha01.nxp.com (119.31.174.70) by SG2PR06CA0232.apcprd06.prod.outlook.com (2603:1096:4:ac::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.21 via Frontend Transport; Tue, 20 Jul 2021 01:44:18 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 81fdaa54-dd69-4913-bab8-08d94b1fe5a2 X-MS-TrafficTypeDiagnostic: AM6PR0402MB3813: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:350; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z/cPbqY2gMf6EooFgaHbaYKjZnz9N+jG8nYwIZSi/HzEV8hvzLiooraG75MAJfxK2f+oUe91N0zcbS2IOXU6UEgKN75CPJu0MLUcQKCF5rMiu180fDOKH7HFTBITvXNEA5Sbe7cTllsy1/UTEaf9/hM+SQaxJ8n2TxQ6vkkY0UmWRjjrOrmD0wEnK8TayWpBIdqt74EgpohIc20YAysYr9u8rEPuM1ktj5J9wTSeZiFGeOhHRrH7BUdnDJVh89f9C0DsROFNFNhue7Ba8M24NuvR1j303zy3HKQOjuVg01RA+jFJz62d7eoyKloO171q2VZjEHRyxhf2Q5HKVneXXwdv6ARnP/x31lgHYOhVap4vGMshsTv84FE9yhZZEXPfvAyFXSpJ0TxIDZ7hdkaPzA+C/uurj07NVP51U6mh2AI8loucZtRv2dR59XQZnFQuuos2p2cb2pxFldfdaS0mwbG3UXGBNuEHP1blKG+UDmWIu8AZNT/FnCT0leaywk/JkSmqJm76Cq6yP9afjabBUgaqyyoRbcT8AqdhaD+fQdPT1uYWNga7c4epCaj2BlOwSKr3nuNZCKEqmpKxk3F0DO1Dj3Bk2g+2suhUuh3CbqkQzYzXoamLG88r9IZeKQFBhd5rP31pq66gPr3P/sCc8HJSdIx/JF+sBy7Yo3/V+fIjmw+h8hQOUDiF+WCVgm898/SgPIrJpHVB0v4g3khoE16bs07rkK+MyIBiXymAAN0nXVQduHpEu5gP5r3EOjOtvOlhOn/PRTImlcoa9IP4ve98WW2f9d0Xw3/5fU/2H3nctdmwHM19r133oN0ErdRX3sHVf02JWQXFS64ucoJQ5g== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM6PR04MB6341.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(36756003)(30864003)(316002)(8676002)(7416002)(4326008)(86362001)(6666004)(186003)(66476007)(66946007)(66556008)(38100700002)(966005)(26005)(38350700002)(6486002)(83380400001)(508600001)(44832011)(5660300002)(956004)(2906002)(2616005)(8936002)(7696005)(52116002)(32563001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?hk1DNFNJ7x4Y5RfnnsSAiEV35Eu/5XgAm45GR5yS4pzd6pFu4+/DXF4rmJ84?= =?us-ascii?Q?pGOmHD5vGzeBfWOTLnj28Dae6JuSYmC9r+l43kf8tKU2Lxehr9D7JIc9l3pd?= =?us-ascii?Q?009sxWrRq6XH6OBKCnTL0BOuJpyF5szgTfADdbKPGJgbq3+pixZ3UTkXBM+d?= =?us-ascii?Q?6iYHZVMnzgvdipiT0yYHSeiYovWMCnJhVrz2EYBQZ497rFjWsByqLI3WKamp?= =?us-ascii?Q?b7G7Qf85NSKIrBa8uwbNEmwFKQBRaYXlMoTNUpjGwvEoWGXF+ZaGrMHz2RWU?= =?us-ascii?Q?QkwqN3X8IswIC7JhVibqMF1eDmj+WEmjHot4wsuEH/xdACjVnNwQiRr45gkD?= =?us-ascii?Q?3PVQtgzN1VozKIM5Die7xNhECC8ZmM0ihfLzoUFuDzIjBTP5YgeUuy0BbdRV?= =?us-ascii?Q?HlfqIKOJFU0PsGaFGeztS+yg6ylM38KIzGhwjSXxaLGzut785xXozdUjvubK?= =?us-ascii?Q?0y4vAidP5ZrGru3TJP8E5Zb6TuCX2JLQNHtHEcNHkQWo4ggagyJAB8/oSu00?= =?us-ascii?Q?fWXnU8bqNUf/yw8VMTgeoChl5g1fTNbAr4CXgoISeKzHGyB5hY7JCzMlzfmD?= =?us-ascii?Q?EkEfehYNhPej3BnYpy5F8poQFFGUVl+jT26HQ3d/FQn7mIRIK1nUivvmdmqb?= =?us-ascii?Q?0RZHap9UlGnkPwQVqGAD3G59W7lJbxl3i2PcYhVR1rk45kjnUgwWzIjRytlO?= =?us-ascii?Q?pZuhL02LfCKH5mTK50g6UW0WPDkZaq5sIgMf9APA02ZiR3/TUrKHcXCW5t6H?= =?us-ascii?Q?mg6QH+p37PCYfOGOzLbdsbbgrdT4gSmuUYH1C3bl8u2wZ7uW2uHOKIfBJScP?= =?us-ascii?Q?sFV9jXmjYCVNcIhsSbvuCw/ln/3wLQCye/nBg7i0q/0K5scPor7cGvR5frLf?= =?us-ascii?Q?BIesobhy9SKj/ayHZ0Vh9jnfiYBi8zUqBovZmz9aIFbslLSZCfrRT9U56KgC?= =?us-ascii?Q?sF98jxmZ0stOO2AD5v8lq3Ml/lI3QAyYMJx/UnuU78MzjQn4Uyr0pMHlWQxG?= =?us-ascii?Q?zXe6gOJGv0Eq8ZN5mVxY6F7gJN+/SmDbbChVsUDSBZq7VnLDLJ/CiOlUdRcu?= =?us-ascii?Q?mPHwjn+9MHQqd+A0oYCkDNyG8YSZW4LDfqCHZsM78ZOLPCnzy3CA7S/n+jBV?= =?us-ascii?Q?KGE6nUBPqP9K6iDsYkM2i+kQ9G4Atvml5FcnpCMQivi53VxIEq8CE8XZBYfg?= =?us-ascii?Q?MoYSMaX9XXJl2IumCpQ3QEYZ1Fpy2HtGNkdaxAegb0snH/Zo8gsFWRAC5x9w?= =?us-ascii?Q?wJ0Y4YDqx9/NM8xCV6k09Y0YymWN+P9JI3SSAZzgHeWTcM5LXUxgX7uE5gZu?= =?us-ascii?Q?w/CPEx+tu8kVckuo7oQAlamH?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 81fdaa54-dd69-4913-bab8-08d94b1fe5a2 X-MS-Exchange-CrossTenant-AuthSource: AM6PR04MB6341.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jul 2021 01:44:22.0501 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: fXquCTS9mMED0LjI4zWsiC7Pds0jY8uezCzxXL28NADB4T0zNloNIqgeUMFPeYTeDb1lreMV2/ZDJPW2pVHSVA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR0402MB3813 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210719_184427_635241_1E7CBFBC X-CRM114-Status: GOOD ( 17.22 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org vpu_v4l2.c implements the v4l2 m2m driver methods. vpu_helpers.c implements the common helper functions vpu_color.c converts the v4l2 colorspace with iso Signed-off-by: Ming Qian Signed-off-by: Shijie Qin Signed-off-by: Zhou Peng --- drivers/media/platform/imx/vpu-8q/vpu_color.c | 201 ++++++ .../media/platform/imx/vpu-8q/vpu_helpers.c | 405 +++++++++++ .../media/platform/imx/vpu-8q/vpu_helpers.h | 80 +++ drivers/media/platform/imx/vpu-8q/vpu_v4l2.c | 662 ++++++++++++++++++ drivers/media/platform/imx/vpu-8q/vpu_v4l2.h | 53 ++ 5 files changed, 1401 insertions(+) create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_color.c create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_helpers.c create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_helpers.h create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_v4l2.c create mode 100644 drivers/media/platform/imx/vpu-8q/vpu_v4l2.h diff --git a/drivers/media/platform/imx/vpu-8q/vpu_color.c b/drivers/media/platform/imx/vpu-8q/vpu_color.c new file mode 100644 index 000000000000..62030a3db0ee --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_color.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#define TAG "COLOR" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "vpu.h" +#include "vpu_helpers.h" + +static const u8 colorprimaries[] = { + 0, + V4L2_COLORSPACE_REC709, /*Rec. ITU-R BT.709-6*/ + 0, + 0, + V4L2_COLORSPACE_470_SYSTEM_M, /*Rec. ITU-R BT.470-6 System M*/ + V4L2_COLORSPACE_470_SYSTEM_BG,/*Rec. ITU-R BT.470-6 System B, G*/ + V4L2_COLORSPACE_SMPTE170M, /*SMPTE170M*/ + V4L2_COLORSPACE_SMPTE240M, /*SMPTE240M*/ + V4L2_COLORSPACE_GENERIC_FILM, /*Generic film*/ + V4L2_COLORSPACE_BT2020, /*Rec. ITU-R BT.2020-2*/ + V4L2_COLORSPACE_ST428 /*SMPTE ST 428-1*/ +}; + +static const u8 colortransfers[] = { + 0, + V4L2_XFER_FUNC_709, /*Rec. ITU-R BT.709-6*/ + 0, + 0, + V4L2_XFER_FUNC_GAMMA22, /*Rec. ITU-R BT.470-6 System M*/ + V4L2_XFER_FUNC_GAMMA28, /*Rec. ITU-R BT.470-6 System B, G*/ + V4L2_XFER_FUNC_709, /*SMPTE170M*/ + V4L2_XFER_FUNC_SMPTE240M,/*SMPTE240M*/ + V4L2_XFER_FUNC_LINEAR, /*Linear transfer characteristics*/ + 0, + 0, + V4L2_XFER_FUNC_XVYCC, /*IEC 61966-2-4*/ + V4L2_XFER_FUNC_BT1361, /*Rec. ITU-R BT.1361-0 extended colour gamut*/ + V4L2_XFER_FUNC_SRGB, /*IEC 61966-2-1 sRGB or sYCC*/ + V4L2_XFER_FUNC_709, /*Rec. ITU-R BT.2020-2 (10 bit system)*/ + V4L2_XFER_FUNC_709, /*Rec. ITU-R BT.2020-2 (12 bit system)*/ + V4L2_XFER_FUNC_SMPTE2084,/*SMPTE ST 2084*/ + V4L2_XFER_FUNC_ST428, /*SMPTE ST 428-1*/ + V4L2_XFER_FUNC_HLG /*Rec. ITU-R BT.2100-0 hybrid log-gamma (HLG)*/ +}; + +static const u8 colormatrixcoefs[] = { + 0, + V4L2_YCBCR_ENC_709, /*Rec. ITU-R BT.709-6*/ + 0, + 0, + V4L2_YCBCR_ENC_BT470_6M, /*Title 47 Code of Federal Regulations*/ + V4L2_YCBCR_ENC_601, /*Rec. ITU-R BT.601-7 625*/ + V4L2_YCBCR_ENC_601, /*Rec. ITU-R BT.601-7 525*/ + V4L2_YCBCR_ENC_SMPTE240M, /*SMPTE240M*/ + 0, + V4L2_YCBCR_ENC_BT2020, /*Rec. ITU-R BT.2020-2*/ + V4L2_YCBCR_ENC_BT2020_CONST_LUM /*Rec. ITU-R BT.2020-2 constant*/ +}; + +u32 vpu_color_cvrt_primaries_v2i(u32 primaries) +{ + return VPU_ARRAY_FIND(colorprimaries, primaries); +} + +u32 vpu_color_cvrt_primaries_i2v(u32 primaries) +{ + return VPU_ARRAY_AT(colorprimaries, primaries); +} + +u32 vpu_color_cvrt_transfers_v2i(u32 transfers) +{ + return VPU_ARRAY_FIND(colortransfers, transfers); +} + +u32 vpu_color_cvrt_transfers_i2v(u32 transfers) +{ + return VPU_ARRAY_AT(colortransfers, transfers); +} + +u32 vpu_color_cvrt_matrix_v2i(u32 matrix) +{ + return VPU_ARRAY_FIND(colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_matrix_i2v(u32 matrix) +{ + return VPU_ARRAY_AT(colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_full_range_v2i(u32 full_range) +{ + return (full_range == V4L2_QUANTIZATION_FULL_RANGE); +} + +u32 vpu_color_cvrt_full_range_i2v(u32 full_range) +{ + if (full_range) + return V4L2_QUANTIZATION_FULL_RANGE; + + return V4L2_QUANTIZATION_LIM_RANGE; +} + +int vpu_color_check_primaries(u32 primaries) +{ + return vpu_color_cvrt_primaries_v2i(primaries) ? 0 : -EINVAL; +} + +int vpu_color_check_transfers(u32 transfers) +{ + return vpu_color_cvrt_transfers_v2i(transfers) ? 0 : -EINVAL; +} + +int vpu_color_check_matrix(u32 matrix) +{ + return vpu_color_cvrt_matrix_v2i(matrix) ? 0 : -EINVAL; +} + +int vpu_color_check_full_range(u32 full_range) +{ + int ret = -EINVAL; + + switch (full_range) { + case V4L2_QUANTIZATION_FULL_RANGE: + case V4L2_QUANTIZATION_LIM_RANGE: + ret = 0; + break; + default: + break; + + } + + return ret; +} + +int vpu_color_get_default(u32 primaries, + u32 *ptransfers, u32 *pmatrix, u32 *pfull_range) +{ + u32 transfers; + u32 matrix; + u32 full_range; + + switch (primaries) { + case V4L2_COLORSPACE_REC709: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_709; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_COLORSPACE_470_SYSTEM_M: + case V4L2_COLORSPACE_470_SYSTEM_BG: + case V4L2_COLORSPACE_SMPTE170M: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_601; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_COLORSPACE_SMPTE240M: + transfers = V4L2_XFER_FUNC_SMPTE240M; + matrix = V4L2_YCBCR_ENC_SMPTE240M; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + case V4L2_COLORSPACE_BT2020: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_BT2020; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + default: + transfers = V4L2_XFER_FUNC_709; + matrix = V4L2_YCBCR_ENC_709; + full_range = V4L2_QUANTIZATION_LIM_RANGE; + break; + } + + if (ptransfers) + *ptransfers = transfers; + if (pmatrix) + *pmatrix = matrix; + if (pfull_range) + *pfull_range = full_range; + + + return 0; +} diff --git a/drivers/media/platform/imx/vpu-8q/vpu_helpers.c b/drivers/media/platform/imx/vpu-8q/vpu_helpers.c new file mode 100644 index 000000000000..c21da2f26163 --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_helpers.c @@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#define TAG "HELPER" +#include +#include +#include +#include +#include +#include +#include +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_rpc.h" +#include "vpu_helpers.h" +#include "vpu_log.h" + +int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x) +{ + int i; + + for (i = 0; i < size; i++) { + if (array[i] == x) + return i; + } + + return 0; +} + +bool vpu_helper_check_type(struct vpu_inst *inst, u32 type) +{ + const struct vpu_format *pfmt; + + for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { + if (vpu_core_check_fmt(inst->core, pfmt->pixfmt)) + continue; + if (pfmt->type == type) + return true; + } + + return false; +} + +const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt) +{ + const struct vpu_format *pfmt; + + if (!inst || !inst->formats) + return NULL; + + if (vpu_core_check_fmt(inst->core, pixelfmt)) + return NULL; + + for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { + if (vpu_core_check_fmt(inst->core, pfmt->pixfmt)) + continue; + + if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type)) + return pfmt; + } + + return NULL; +} + +const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index) +{ + const struct vpu_format *pfmt; + int i = 0; + + if (!inst || !inst->formats) + return NULL; + + for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { + if (vpu_core_check_fmt(inst->core, pfmt->pixfmt)) + continue; + + if (pfmt->type == type) { + if (index == i) + return pfmt; + i++; + } + } + + return NULL; +} + +u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width) +{ + const struct vpu_core_resources *res; + + if (!inst || !inst->core || !inst->core->res) + return width; + + res = inst->core->res; + if (res->max_width) + width = clamp(width, res->min_width, res->max_width); + if (res->step_width) + width = ALIGN(width, res->step_width); + + return width; +} + +u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height) +{ + const struct vpu_core_resources *res; + + if (!inst || !inst->core || !inst->core->res) + return height; + + res = inst->core->res; + if (res->max_height) + height = clamp(height, res->min_height, res->max_height); + if (res->step_height) + height = ALIGN(height, res->step_height); + + return height; +} + +u32 vpu_helper_get_frame_size(u32 fmt, u32 width, u32 height) +{ + switch (fmt) { + case V4L2_PIX_FMT_NV12: + return ((width * 3) >> 1) * height; + default: + return width * height; + } +} + +u32 vpu_helper_get_plane_size(u32 fmt, u32 width, u32 height, int plane_no, u32 stride) +{ + width = ALIGN(width, stride); + + switch (fmt) { + case V4L2_PIX_FMT_NV12: + if (plane_no == 0) + return width * height; + else if (plane_no == 1) + return (width >> 1) * height; + else + return 0; + default: + if (plane_no == 0) + return width * height; + else + return 0; + } +} + +u32 vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *rptr, u32 size, void *dst) +{ + u32 offset; + u32 start; + u32 end; + void *virt; + + if (!stream_buffer || !rptr || !dst) + return -EINVAL; + + if (!size) + return 0; + + offset = *rptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + + if (offset < start || offset > end) { + vpu_err("rptr 0x%x is out of range [0x%x, 0x%x]\n", + offset, start, end); + return -EINVAL; + } + + if (offset + size <= end) { + memcpy(dst, virt + (offset - start), size); + } else { + memcpy(dst, virt + (offset - start), end - offset); + memcpy(dst + end - offset, virt, size + offset - end); + } + + *rptr = vpu_helper_step_walk(stream_buffer, offset, size); + return size; +} + +u32 vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u32 size, void *src) +{ + u32 offset; + u32 start; + u32 end; + void *virt; + + if (!stream_buffer || !wptr || !src) + return -EINVAL; + + if (!size) + return 0; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + if (offset < start || offset > end) { + vpu_err("wptr 0x%x is out of range [0x%x, 0x%x]\n", + offset, start, end); + return -EINVAL; + } + + if (offset + size <= end) { + memcpy(virt + (offset - start), src, size); + } else { + memcpy(virt + (offset - start), src, end - offset); + memcpy(virt, src + end - offset, size + offset - end); + } + + *wptr = vpu_helper_step_walk(stream_buffer, offset, size); + + return size; +} + +u32 vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u8 val, u32 size) +{ + u32 offset; + u32 start; + u32 end; + void *virt; + + if (!stream_buffer || !wptr) + return -EINVAL; + + if (!size) + return 0; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + if (offset < start || offset > end) { + vpu_err("wptr 0x%x is out of range [0x%x, 0x%x]\n", + offset, start, end); + return -EINVAL; + } + + if (offset + size <= end) { + memset(virt + (offset - start), val, size); + } else { + memset(virt + (offset - start), val, end - offset); + memset(virt, val, size + offset - end); + } + + offset += size; + if (offset >= end) + offset -= stream_buffer->length; + + *wptr = offset; + + return size; +} + +u32 vpu_helper_get_free_space(struct vpu_inst *inst) +{ + struct vpu_rpc_buffer_desc desc; + + if (vpu_iface_get_stream_buffer_desc(inst, &desc)) + return 0; + + if (desc.rptr > desc.wptr) + return desc.rptr - desc.wptr; + else if (desc.rptr < desc.wptr) + return (desc.end - desc.start + desc.rptr - desc.wptr); + else + return desc.end - desc.start; +} + +u32 vpu_helper_get_used_space(struct vpu_inst *inst) +{ + struct vpu_rpc_buffer_desc desc; + + if (vpu_iface_get_stream_buffer_desc(inst, &desc)) + return 0; + + if (desc.wptr > desc.rptr) + return desc.wptr - desc.rptr; + else if (desc.wptr < desc.rptr) + return (desc.end - desc.start + desc.wptr - desc.rptr); + else + return 0; +} + +int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl) +{ + struct vpu_inst *inst = ctrl_to_inst(ctrl); + + switch (ctrl->id) { + case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: + ctrl->val = inst->min_buffer_cap; + break; + case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: + ctrl->val = inst->min_buffer_out; + break; + default: + return -EINVAL; + } + + return 0; +} + +u32 vpu_helper_calc_coprime(u32 *a, u32 *b) +{ + int m = *a; + int n = *b; + + if (m == 0) + return n; + if (n == 0) + return m; + + while (n != 0) { + int tmp = m % n; + + m = n; + n = tmp; + } + *a = (*a) / m; + *b = (*b) / m; + + return m; +} + +#define READ_BYTE(buffer, pos) (*(u8 *)((buffer)->virt + ((pos) % buffer->length))) +int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer, + u32 pixelformat, u32 offset, u32 bytesused) +{ + u32 start_code; + int start_code_size; + u32 val = 0; + int i; + int ret = -EINVAL; + + if (!stream_buffer || !stream_buffer->virt) + return -EINVAL; + + switch (pixelformat) { + case V4L2_PIX_FMT_H264: + start_code_size = 4; + start_code = 0x00000001; + break; + default: + return 0; + } + + for (i = 0; i < bytesused; i++) { + val = (val << 8) | READ_BYTE(stream_buffer, offset + i); + if (i < start_code_size - 1) + continue; + if (val == start_code) { + ret = i + 1 - start_code_size; + break; + } + } + + return ret; +} + +int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src) +{ + u32 i; + + if (!pairs || !cnt) + return -EINVAL; + + for (i = 0; i < cnt; i++) { + if (pairs[i].src == src) + return pairs[i].dst; + } + + return -EINVAL; +} + +int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst) +{ + u32 i; + + if (!pairs || !cnt) + return -EINVAL; + + for (i = 0; i < cnt; i++) { + if (pairs[i].dst == dst) + return pairs[i].src; + } + + return -EINVAL; +} diff --git a/drivers/media/platform/imx/vpu-8q/vpu_helpers.h b/drivers/media/platform/imx/vpu-8q/vpu_helpers.h new file mode 100644 index 000000000000..3f0acda8522c --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_helpers.h @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#ifndef _IMX_VPU_HELPERS_H +#define _IMX_VPU_HELPERS_H + +struct vpu_pair { + u32 src; + u32 dst; +}; + +#define MAKE_TIMESTAMP(s, ns) (((s32)(s) * NSEC_PER_SEC) + (ns)) +#define VPU_INVALID_TIMESTAMP MAKE_TIMESTAMP(-1, 0) +#define VPU_ARRAY_AT(array, i) (((i) < ARRAY_SIZE(array)) ? array[i] : 0) +#define VPU_ARRAY_FIND(array, x) vpu_helper_find_in_array_u8(array, ARRAY_SIZE(array), x) + +int vpu_helper_find_in_array_u8(const u8 *array, u32 size, u32 x); +bool vpu_helper_check_type(struct vpu_inst *inst, u32 type); +const struct vpu_format *vpu_helper_find_format(struct vpu_inst *inst, u32 type, u32 pixelfmt); +const struct vpu_format *vpu_helper_enum_format(struct vpu_inst *inst, u32 type, int index); +u32 vpu_helper_valid_frame_width(struct vpu_inst *inst, u32 width); +u32 vpu_helper_valid_frame_height(struct vpu_inst *inst, u32 height); +u32 vpu_helper_get_frame_size(u32 fmt, u32 width, u32 height); +u32 vpu_helper_get_plane_size(u32 fmt, u32 width, u32 height, int plane_no, u32 stride); +u32 vpu_helper_copy_from_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *rptr, u32 size, void *dst); +u32 vpu_helper_copy_to_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u32 size, void *src); +u32 vpu_helper_memset_stream_buffer(struct vpu_buffer *stream_buffer, + u32 *wptr, u8 val, u32 size); +u32 vpu_helper_get_free_space(struct vpu_inst *inst); +u32 vpu_helper_get_used_space(struct vpu_inst *inst); +int vpu_helper_g_volatile_ctrl(struct v4l2_ctrl *ctrl); +u32 vpu_helper_calc_coprime(u32 *a, u32 *b); +void vpu_helper_get_kmp_next(const u8 *pattern, int *next, int size); +int vpu_helper_kmp_search(u8 *s, int s_len, const u8 *p, int p_len, int *next); +int vpu_helper_kmp_search_in_stream_buffer(struct vpu_buffer *stream_buffer, + u32 offset, int bytesused, + const u8 *p, int p_len, int *next); +int vpu_helper_find_startcode(struct vpu_buffer *stream_buffer, + u32 pixelformat, u32 offset, u32 bytesused); + +static inline u32 vpu_helper_step_walk(struct vpu_buffer *stream_buffer, u32 pos, u32 step) +{ + pos += step; + if (pos > stream_buffer->phys + stream_buffer->length) + pos -= stream_buffer->length; + + return pos; +} + +int vpu_color_check_primaries(u32 primaries); +int vpu_color_check_transfers(u32 transfers); +int vpu_color_check_matrix(u32 matrix); +int vpu_color_check_full_range(u32 full_range); +u32 vpu_color_cvrt_primaries_v2i(u32 primaries); +u32 vpu_color_cvrt_primaries_i2v(u32 primaries); +u32 vpu_color_cvrt_transfers_v2i(u32 transfers); +u32 vpu_color_cvrt_transfers_i2v(u32 transfers); +u32 vpu_color_cvrt_matrix_v2i(u32 matrix); +u32 vpu_color_cvrt_matrix_i2v(u32 matrix); +u32 vpu_color_cvrt_full_range_v2i(u32 full_range); +u32 vpu_color_cvrt_full_range_i2v(u32 full_range); +int vpu_color_get_default(u32 primaries, + u32 *ptransfers, u32 *pmatrix, u32 *pfull_range); + +int vpu_find_dst_by_src(struct vpu_pair *pairs, u32 cnt, u32 src); +int vpu_find_src_by_dst(struct vpu_pair *pairs, u32 cnt, u32 dst); +#endif diff --git a/drivers/media/platform/imx/vpu-8q/vpu_v4l2.c b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.c new file mode 100644 index 000000000000..c9dd976ea16f --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.c @@ -0,0 +1,662 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#define TAG "V4L2" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_v4l2.h" +#include "vpu_msgs.h" +#include "vpu_helpers.h" +#include "vpu_log.h" + +void vpu_inst_lock(struct vpu_inst *inst) +{ + mutex_lock(&inst->lock); +} + +void vpu_inst_unlock(struct vpu_inst *inst) +{ + mutex_unlock(&inst->lock); +} + +dma_addr_t vpu_get_vb_phy_addr(struct vb2_buffer *vb, u32 plane_no) +{ + return vb2_dma_contig_plane_dma_addr(vb, plane_no) + + vb->planes[plane_no].data_offset; +} + +unsigned int vpu_get_vb_length(struct vb2_buffer *vb, u32 plane_no) +{ + if (plane_no >= vb->num_planes) + return 0; + return vb2_plane_size(vb, plane_no) - vb->planes[plane_no].data_offset; +} + +void vpu_v4l2_set_error(struct vpu_inst *inst) +{ + struct vb2_queue *src_q; + struct vb2_queue *dst_q; + + src_q = v4l2_m2m_get_src_vq(inst->m2m_ctx); + dst_q = v4l2_m2m_get_dst_vq(inst->m2m_ctx); + if (src_q) + src_q->error = 1; + if (dst_q) + dst_q->error = 1; +} + +int vpu_notify_eos(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_EOS + }; + + inst_dbg(inst, LVL_FLOW, "notify eos event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + + return 0; +} + +int vpu_notify_source_change(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_SOURCE_CHANGE, + .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION + }; + + inst_dbg(inst, LVL_FLOW, "notify source change event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + return 0; +} + +int vpu_notify_skip(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_SKIP, + .u.data[0] = 0xff, + }; + + inst_dbg(inst, LVL_FLOW, "notify skip event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + + return 0; +} + +int vpu_notify_codec_error(struct vpu_inst *inst) +{ + const struct v4l2_event ev = { + .id = 0, + .type = V4L2_EVENT_CODEC_ERROR, + }; + + inst_dbg(inst, LVL_FLOW, "notify error event\n"); + v4l2_event_queue_fh(&inst->fh, &ev); + vpu_v4l2_set_error(inst); + + return 0; +} + +const struct vpu_format *vpu_try_fmt_common(struct vpu_inst *inst, + struct v4l2_format *f) +{ + struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; + u32 type = f->type; + u32 stride; + u32 bytesperline; + u32 sizeimage; + const struct vpu_format *fmt; + int i; + + fmt = vpu_helper_find_format(inst, type, pixmp->pixelformat); + if (!fmt) { + fmt = vpu_helper_enum_format(inst, type, 0); + if (!fmt) + return NULL; + pixmp->pixelformat = fmt->pixfmt; + } + + stride = inst->core->res->stride; + pixmp->width = vpu_helper_valid_frame_width(inst, pixmp->width); + pixmp->height = vpu_helper_valid_frame_height(inst, pixmp->height); + pixmp->flags = fmt->flags; + pixmp->num_planes = fmt->num_planes; + if (pixmp->field == V4L2_FIELD_ANY) + pixmp->field = V4L2_FIELD_NONE; + for (i = 0; i < pixmp->num_planes; i++) { + bytesperline = ALIGN(pixmp->width, stride); + sizeimage = vpu_helper_get_plane_size(pixmp->pixelformat, + pixmp->width, pixmp->height, i, stride); + if ((s32)(pixmp->plane_fmt[i].bytesperline) <= 0) + pixmp->plane_fmt[i].bytesperline = bytesperline; + if ((s32)(pixmp->plane_fmt[i].sizeimage) <= 0) + pixmp->plane_fmt[i].sizeimage = sizeimage; + if (pixmp->plane_fmt[i].bytesperline < bytesperline) + pixmp->plane_fmt[i].bytesperline = bytesperline; + if (pixmp->plane_fmt[i].sizeimage <= sizeimage) + pixmp->plane_fmt[i].sizeimage = sizeimage; + } + + return fmt; +} + +static bool vpu_check_ready(struct vpu_inst *inst, u32 type) +{ + if (!inst) + return false; + if (inst->state == VPU_CODEC_STATE_DEINIT || inst->id < 0) + return false; + if (!inst->ops->check_ready) + return true; + return call_vop(inst, check_ready, type); +} + +int vpu_process_output_buffer(struct vpu_inst *inst) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vpu_vb2_buffer *vpu_buf = NULL; + + if (!inst) + return -EINVAL; + + if (!vpu_check_ready(inst, inst->out_format.type)) + return -EINVAL; + + v4l2_m2m_for_each_src_buf(inst->m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); + if (vpu_buf->state == VPU_BUF_STATE_IDLE) + break; + vpu_buf = NULL; + } + + if (!vpu_buf) + return -EINVAL; + + inst_dbg(inst, LVL_DEBUG, "frame id = %d / %d\n", + vpu_buf->m2m_buf.vb.sequence, inst->sequence); + return call_vop(inst, process_output, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +int vpu_process_capture_buffer(struct vpu_inst *inst) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vpu_vb2_buffer *vpu_buf = NULL; + + if (!inst) + return -EINVAL; + + if (!vpu_check_ready(inst, inst->cap_format.type)) + return -EINVAL; + + v4l2_m2m_for_each_dst_buf(inst->m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); + if (vpu_buf->state == VPU_BUF_STATE_IDLE) + break; + vpu_buf = NULL; + } + if (!vpu_buf) + return -EINVAL; + + return call_vop(inst, process_capture, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence(struct vpu_inst *inst, + u32 type, u32 sequence) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vb2_v4l2_buffer *vbuf = NULL; + + if (V4L2_TYPE_IS_OUTPUT(type)) { + v4l2_m2m_for_each_src_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->sequence == sequence) + break; + vbuf = NULL; + } + } else { + v4l2_m2m_for_each_dst_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->sequence == sequence) + break; + vbuf = NULL; + } + } + + return vbuf; +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_idx(struct vpu_inst *inst, + u32 type, u32 idx) +{ + struct v4l2_m2m_buffer *buf = NULL; + struct vb2_v4l2_buffer *vbuf = NULL; + + if (V4L2_TYPE_IS_OUTPUT(type)) { + v4l2_m2m_for_each_src_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->vb2_buf.index == idx) + break; + vbuf = NULL; + } + } else { + v4l2_m2m_for_each_dst_buf(inst->m2m_ctx, buf) { + vbuf = &buf->vb; + if (vbuf->vb2_buf.index == idx) + break; + vbuf = NULL; + } + } + + return vbuf; +} + +int vpu_get_num_buffers(struct vpu_inst *inst, u32 type) +{ + struct vb2_queue *q; + + if (!inst || !inst->m2m_ctx) + return -EINVAL; + if (V4L2_TYPE_IS_OUTPUT(type)) + q = v4l2_m2m_get_src_vq(inst->m2m_ctx); + else + q = v4l2_m2m_get_dst_vq(inst->m2m_ctx); + + return q->num_buffers; +} + +static void vpu_m2m_device_run(void *priv) +{ +} + +static void vpu_m2m_job_abort(void *priv) +{ + struct vpu_inst *inst = priv; + + v4l2_m2m_job_finish(inst->m2m_dev, inst->m2m_ctx); +} + +static const struct v4l2_m2m_ops vpu_m2m_ops = { + .device_run = vpu_m2m_device_run, + .job_abort = vpu_m2m_job_abort +}; + +static int vpu_vb2_queue_setup(struct vb2_queue *vq, + unsigned int *buf_count, + unsigned int *plane_count, + unsigned int psize[], + struct device *allocators[]) +{ + struct vpu_inst *inst = vb2_get_drv_priv(vq); + struct vpu_format *cur_fmt; + int i; + + cur_fmt = vpu_get_format(inst, vq->type); + + if (*plane_count) { + if (*plane_count != cur_fmt->num_planes) + return -EINVAL; + for (i = 0; i < cur_fmt->num_planes; i++) { + if (psize[i] < cur_fmt->sizeimage[i]) + return -EINVAL; + } + } + + *plane_count = cur_fmt->num_planes; + for (i = 0; i < cur_fmt->num_planes; i++) + psize[i] = cur_fmt->sizeimage[i]; + + inst_dbg(inst, LVL_FLOW, "%s queue setup : %u; %u, %u\n", + vpu_type_name(vq->type), + *buf_count, + psize[0], psize[1]); + + return 0; +} + +static int vpu_vb2_buf_init(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf); + + vpu_buf->state = VPU_BUF_STATE_IDLE; + + return 0; +} + +static void vpu_vb2_buf_cleanup(struct vb2_buffer *vb) +{ +} + +static int vpu_vb2_buf_prepare(struct vb2_buffer *vb) +{ + struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue); + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_vb2_buffer *vpu_buf = to_vpu_vb2_buffer(vbuf); + struct vpu_format *cur_fmt; + u32 i; + + cur_fmt = vpu_get_format(inst, vb->type); + if (vb->num_planes != cur_fmt->num_planes) + return -EINVAL; + for (i = 0; i < cur_fmt->num_planes; i++) { + if (vpu_get_vb_length(vb, i) < cur_fmt->sizeimage[i]) { + inst_err(inst, "%s buf[%d] is invalid\n", + vpu_type_name(vb->type), + vb->index); + vpu_buf->state = VPU_BUF_STATE_ERROR; + } + } + + return 0; +} + +static void vpu_vb2_buf_finish(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue); + struct vb2_queue *q = vb->vb2_queue; + + if (vbuf->flags & V4L2_BUF_FLAG_LAST) + vpu_notify_eos(inst); + + if (list_empty(&q->done_list)) + call_vop(inst, on_queue_empty, q->type); +} + +void vpu_vb2_buffers_return(struct vpu_inst *inst, + unsigned int type, enum vb2_buffer_state state) +{ + struct vb2_v4l2_buffer *buf; + + if (!inst || !inst->m2m_ctx) + return; + + if (V4L2_TYPE_IS_OUTPUT(type)) { + while ((buf = v4l2_m2m_src_buf_remove(inst->m2m_ctx))) + v4l2_m2m_buf_done(buf, state); + } else { + while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx))) + v4l2_m2m_buf_done(buf, state); + } +} + +static int vpu_vb2_start_streaming(struct vb2_queue *q, unsigned int count) +{ + struct vpu_inst *inst = vb2_get_drv_priv(q); + int ret; + + vpu_inst_unlock(inst); + ret = vpu_inst_register(inst); + vpu_inst_lock(inst); + if (ret) + return ret; + + vpu_inst_get(inst); + inst_dbg(inst, LVL_FLOW, "%s start streaming : %d\n", + vpu_type_name(q->type), q->num_buffers); + call_vop(inst, start, q->type); + vb2_clear_last_buffer_dequeued(q); + + return 0; +} + +static void vpu_vb2_stop_streaming(struct vb2_queue *q) +{ + struct vpu_inst *inst = vb2_get_drv_priv(q); + + inst_dbg(inst, LVL_FLOW, "%s stop streaming\n", vpu_type_name(q->type)); + + call_vop(inst, stop, q->type); + vpu_vb2_buffers_return(inst, q->type, VB2_BUF_STATE_ERROR); + if (V4L2_TYPE_IS_OUTPUT(q->type)) + inst->sequence = 0; + + vpu_inst_put(inst); +} + +static void vpu_vb2_buf_queue(struct vb2_buffer *vb) +{ + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct vpu_inst *inst = vb2_get_drv_priv(vb->vb2_queue); + + inst_dbg(inst, LVL_DEBUG, "%s buf queue\n", vpu_type_name(vb->type)); + + if (V4L2_TYPE_IS_OUTPUT(vb->type)) { + vbuf->sequence = inst->sequence++; + if ((s64)vb->timestamp < 0) + vb->timestamp = VPU_INVALID_TIMESTAMP; + } + + v4l2_m2m_buf_queue(inst->m2m_ctx, vbuf); + vpu_process_output_buffer(inst); + vpu_process_capture_buffer(inst); +} + +static struct vb2_ops vpu_vb2_ops = { + .queue_setup = vpu_vb2_queue_setup, + .buf_init = vpu_vb2_buf_init, + .buf_cleanup = vpu_vb2_buf_cleanup, + .buf_prepare = vpu_vb2_buf_prepare, + .buf_finish = vpu_vb2_buf_finish, + .start_streaming = vpu_vb2_start_streaming, + .stop_streaming = vpu_vb2_stop_streaming, + .buf_queue = vpu_vb2_buf_queue, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int vpu_m2m_queue_init(void *priv, struct vb2_queue *src_vq, + struct vb2_queue *dst_vq) +{ + struct vpu_inst *inst = priv; + int ret; + + src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; + src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; + src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + src_vq->ops = &vpu_vb2_ops; + src_vq->mem_ops = &vb2_dma_contig_memops; + if (inst->type == VPU_CORE_TYPE_DEC && inst->use_stream_buffer) + src_vq->mem_ops = &vb2_vmalloc_memops; + src_vq->drv_priv = inst; + src_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer); + src_vq->allow_zero_bytesused = 1; + src_vq->min_buffers_needed = 1; + src_vq->dev = inst->core->dev; + src_vq->lock = &inst->lock; + ret = vb2_queue_init(src_vq); + if (ret) + return ret; + + dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; + dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; + dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; + dst_vq->ops = &vpu_vb2_ops; + dst_vq->mem_ops = &vb2_dma_contig_memops; + if (inst->type == VPU_CORE_TYPE_ENC && inst->use_stream_buffer) + dst_vq->mem_ops = &vb2_vmalloc_memops; + dst_vq->drv_priv = inst; + dst_vq->buf_struct_size = sizeof(struct vpu_vb2_buffer); + dst_vq->allow_zero_bytesused = 1; + dst_vq->min_buffers_needed = 1; + dst_vq->dev = inst->core->dev; + dst_vq->lock = &inst->lock; + ret = vb2_queue_init(dst_vq); + if (ret) { + vb2_queue_release(src_vq); + return ret; + } + + return 0; +} + +static int vpu_v4l2_release(struct vpu_inst *inst) +{ + struct vpu_core *core = inst->core; + + inst_dbg(inst, LVL_FLOW, "%s\n", __func__); + + vpu_release_core(core); + + if (inst->workqueue) { + cancel_work_sync(&inst->msg_work); + destroy_workqueue(inst->workqueue); + inst->workqueue = NULL; + } + if (inst->m2m_ctx) { + v4l2_m2m_ctx_release(inst->m2m_ctx); + inst->m2m_ctx = NULL; + } + if (inst->m2m_dev) { + v4l2_m2m_release(inst->m2m_dev); + inst->m2m_dev = NULL; + } + + v4l2_ctrl_handler_free(&inst->ctrl_handler); + mutex_destroy(&inst->lock); + v4l2_fh_del(&inst->fh); + v4l2_fh_exit(&inst->fh); + + call_vop(inst, cleanup); + + return 0; +} + +int vpu_v4l2_open(struct file *file, struct vpu_inst *inst) +{ + struct vpu_dev *vpu = video_drvdata(file); + struct video_device *vdev; + struct vpu_core *core = NULL; + int ret = 0; + + WARN_ON(!file || !inst || !inst->ops); + + if (inst->type == VPU_CORE_TYPE_ENC) + vdev = vpu->vdev_enc; + else + vdev = vpu->vdev_dec; + + mutex_init(&inst->lock); + INIT_LIST_HEAD(&inst->cmd_q); + + inst->id = VPU_INST_NULL_ID; + inst->release = vpu_v4l2_release; + inst->core = vpu_request_core(vpu, inst->type); + + core = inst->core; + if (!core) { + vpu_err("there is no core for %s\n", + vpu_core_type_desc(inst->type)); + return -EINVAL; + } + + inst->min_buffer_cap = 2; + inst->min_buffer_out = 2; + + ret = call_vop(inst, ctrl_init); + if (ret) + goto error; + + inst->m2m_dev = v4l2_m2m_init(&vpu_m2m_ops); + if (IS_ERR(inst->m2m_dev)) { + vpu_err("v4l2_m2m_init fail\n"); + ret = PTR_ERR(inst->m2m_dev); + goto error; + } + + inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, + inst, vpu_m2m_queue_init); + if (IS_ERR(inst->m2m_ctx)) { + vpu_err("v4l2_m2m_ctx_init fail\n"); + ret = PTR_ERR(inst->m2m_dev); + goto error; + } + + v4l2_fh_init(&inst->fh, vdev); + v4l2_fh_add(&inst->fh); + inst->fh.ctrl_handler = &inst->ctrl_handler; + inst->fh.m2m_ctx = inst->m2m_ctx; + file->private_data = &inst->fh; + inst->state = VPU_CODEC_STATE_DEINIT; + inst->workqueue = alloc_workqueue("vpu_inst", WQ_UNBOUND | WQ_MEM_RECLAIM, 1); + if (inst->workqueue) { + INIT_WORK(&inst->msg_work, vpu_inst_run_work); + ret = kfifo_init(&inst->msg_fifo, + inst->msg_buffer, + roundup_pow_of_two(sizeof(inst->msg_buffer))); + if (ret) { + destroy_workqueue(inst->workqueue); + inst->workqueue = NULL; + } + } + atomic_set(&inst->ref_count, 0); + vpu_inst_get(inst); + vpu_dbg(LVL_FLOW, "open, tgid = %d, pid = %d\n", inst->tgid, inst->pid); + + return 0; +error: + if (inst->m2m_ctx) { + v4l2_m2m_ctx_release(inst->m2m_ctx); + inst->m2m_ctx = NULL; + } + if (inst->m2m_dev) { + v4l2_m2m_release(inst->m2m_dev); + inst->m2m_dev = NULL; + } + v4l2_ctrl_handler_free(&inst->ctrl_handler); + vpu_release_core(inst->core); + + return ret; +} + +int vpu_v4l2_close(struct file *file) +{ + struct vpu_inst *inst = to_inst(file); + struct vb2_queue *src_q; + struct vb2_queue *dst_q; + + inst_dbg(inst, LVL_FLOW, "close\n"); + src_q = v4l2_m2m_get_src_vq(inst->m2m_ctx); + dst_q = v4l2_m2m_get_dst_vq(inst->m2m_ctx); + vpu_inst_lock(inst); + if (vb2_is_streaming(src_q)) + v4l2_m2m_streamoff(file, inst->m2m_ctx, src_q->type); + if (vb2_is_streaming(dst_q)) + v4l2_m2m_streamoff(file, inst->m2m_ctx, dst_q->type); + vpu_inst_unlock(inst); + + call_vop(inst, release); + vpu_inst_unregister(inst); + vpu_inst_put(inst); + + return 0; +} diff --git a/drivers/media/platform/imx/vpu-8q/vpu_v4l2.h b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.h new file mode 100644 index 000000000000..87288f0f7625 --- /dev/null +++ b/drivers/media/platform/imx/vpu-8q/vpu_v4l2.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright 2020-2021 NXP + */ + +/* + * The code contained herein is licensed under the GNU General Public + * License. You may obtain a copy of the GNU General Public License + * Version 2 or later at the following locations: + * + * http://www.opensource.org/licenses/gpl-license.html + * http://www.gnu.org/copyleft/gpl.html + */ + +#ifndef _IMX_VPU_V4L2_H +#define _IMX_VPU_V4L2_H + +void vpu_inst_lock(struct vpu_inst *inst); +void vpu_inst_unlock(struct vpu_inst *inst); + +int vpu_v4l2_open(struct file *file, struct vpu_inst *inst); +int vpu_v4l2_close(struct file *file); + +const struct vpu_format *vpu_try_fmt_common(struct vpu_inst *inst, struct v4l2_format *f); +int vpu_process_output_buffer(struct vpu_inst *inst); +int vpu_process_capture_buffer(struct vpu_inst *inst); +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence(struct vpu_inst *inst, u32 type, u32 sequence); +struct vb2_v4l2_buffer *vpu_find_buf_by_idx(struct vpu_inst *inst, u32 type, u32 idx); +void vpu_v4l2_set_error(struct vpu_inst *inst); +int vpu_notify_eos(struct vpu_inst *inst); +int vpu_notify_source_change(struct vpu_inst *inst); +int vpu_notify_skip(struct vpu_inst *inst); +int vpu_notify_codec_error(struct vpu_inst *inst); +void vpu_vb2_buffers_return(struct vpu_inst *inst, + unsigned int type, enum vb2_buffer_state state); +int vpu_get_num_buffers(struct vpu_inst *inst, u32 type); + +dma_addr_t vpu_get_vb_phy_addr(struct vb2_buffer *vb, u32 plane_no); +unsigned int vpu_get_vb_length(struct vb2_buffer *vb, u32 plane_no); +static inline struct vpu_format *vpu_get_format(struct vpu_inst *inst, u32 type) +{ + if (V4L2_TYPE_IS_OUTPUT(type)) + return &inst->out_format; + else + return &inst->cap_format; +} + +static inline char *vpu_type_name(u32 type) +{ + return V4L2_TYPE_IS_OUTPUT(type) ? "output" : "capture"; +} + +#endif -- 2.32.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel