From patchwork Sat Jul 20 07:15:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WXVuZmVpIERvbmcgKOiRo+S6kemjnik=?= X-Patchwork-Id: 813575 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 426E11C27; Sat, 20 Jul 2024 08:03:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.61.82.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721462601; cv=none; b=JCN05JOWrPKq03muxIpK32LCOVVEi3bEl2wWIDFND3nHC+8czEdLHHFwQbWdZxP+pcuE5gj84lEyEZzMNayvXOWZ1aiMGf6T8ppabIqKFk5d2oOektdZDPQN9ZwVTE2STip0oRnV4R0Jqd1fMiJF7pFXJPGPB0FP5R+X9Tzgl2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721462601; c=relaxed/simple; bh=ToqaC2fQpqA71Jvnw8czm5+q9TOF3TTZS8re9l0gl3A=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YQYxU0oBEOvDK/lc9r7g05I9ysfCInFdiYd7pbN+2a9VG/aTL1t6jI70PD+NeCbroqGTq0KZeI4yQV4kFWlW1OW7QxgLYRhW6cPzyZ1FgNSj7a7dK2z9LANydUnKHOuqL+znNlWQ2ssnx9McsAOl0DLjPJtjCtIzQzs2EEYcqjQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b=p+jW2YlC; arc=none smtp.client-ip=210.61.82.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="p+jW2YlC" X-UUID: f1b8048a466711ef87684b57767b52b1-20240720 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=xQJAVcEsNPwhyH05n9NzE0IPLrlyrT5EWY2i00FYOis=; b=p+jW2YlCkuwX1ZTVIq1TsDOTdvn/chhQ574+hj+qN54rfTGS6CtZKonh2T9uW5GjUSdZ9S8t/0yEbbIGU7+tZMvCzvTMGG3J2bW1Zcs23VgjqlgX4mF5H7PJc9Afuh6wpLlUrfenKjvt+MU+6ExvTcGlj8g1nAsENxzPj070p8w=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.40, REQID:15bc6639-9406-406d-932b-3fa1a22e8354, IP:0, U RL:0,TC:0,Content:0,EDM:-25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:ba885a6, CLOUDID:c210ae0d-46b0-425a-97d3-4623fe284021, B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:1,IP:nil,UR L:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,S PR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: f1b8048a466711ef87684b57767b52b1-20240720 Received: from mtkmbs14n2.mediatek.inc [(172.21.101.76)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1237293394; Sat, 20 Jul 2024 15:16:12 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Sat, 20 Jul 2024 15:16:11 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Sat, 20 Jul 2024 15:16:10 +0800 From: Yunfei Dong To: Jeffrey Kardatzke , =?utf-8?q?N=C3=ADcolas_F_=2E_?= =?utf-8?q?R_=2E_A_=2E_Prado?= , Nathan Hebert , Nicolas Dufresne , Hans Verkuil , AngeloGioacchino Del Regno , Benjamin Gaignard , Sebastian Fricke , Tomasz Figa , Mauro Carvalho Chehab , Marek Szyprowski CC: Chen-Yu Tsai , Yong Wu , Hsin-Yi Wang , Fritz Koenig , Daniel Vetter , Steve Cho , Yunfei Dong , Sumit Semwal , Brian Starkey , John Stultz , "T . J . Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Matthias Brugger , , , , , , Subject: [PATCH v7 02/28] v4l2: handle restricted memory flags in queue setup Date: Sat, 20 Jul 2024 15:15:40 +0800 Message-ID: <20240720071606.27930-3-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240720071606.27930-1-yunfei.dong@mediatek.com> References: <20240720071606.27930-1-yunfei.dong@mediatek.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MTK: N From: Jeffrey Kardatzke Validates the restricted memory flags when setting up a queue and ensures the queue has the proper capability. Signed-off-by: Jeffrey Kardatzke Signed-off-by: Yunfei Dong [Yunfei: Change reviewer's comments] --- .../media/common/videobuf2/videobuf2-core.c | 29 +++++++++++++++++++ .../media/common/videobuf2/videobuf2-v4l2.c | 4 ++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 0217392fcc0d..44080121f37e 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -830,6 +830,23 @@ static bool verify_coherency_flags(struct vb2_queue *q, bool non_coherent_mem) return true; } +static bool verify_restricted_mem_flags(struct vb2_queue *q, bool restricted_mem) +{ + if (restricted_mem != q->restricted_mem) { + dprintk(q, 1, "restricted memory model mismatch\n"); + return false; + } + + return true; +} + +static inline int restricted_mem_mismatch(bool restricted_mem, struct vb2_queue *q, + enum vb2_memory memory) +{ + return restricted_mem && (!q->allow_restricted_mem || memory != VB2_MEMORY_DMABUF) ? + -1 : 0; +} + static int vb2_core_allocated_buffers_storage(struct vb2_queue *q) { if (!q->bufs) @@ -863,6 +880,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, unsigned int q_num_bufs = vb2_get_num_buffers(q); unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; + bool restricted_mem = flags & V4L2_MEMORY_FLAG_RESTRICTED; unsigned int i, first_index; int ret = 0; @@ -906,6 +924,9 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, return 0; } + if (restricted_mem_mismatch(restricted_mem, q, memory)) + return -EINVAL; + /* * Make sure the requested values and current defaults are sane. */ @@ -923,6 +944,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, if (ret) return ret; set_queue_coherency(q, non_coherent_mem); + q->restricted_mem = restricted_mem; /* * Ask the driver how many buffers and planes per buffer it requires. @@ -1031,6 +1053,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, unsigned plane_sizes[VB2_MAX_PLANES] = { }; bool non_coherent_mem = flags & V4L2_MEMORY_FLAG_NON_COHERENT; unsigned int q_num_bufs = vb2_get_num_buffers(q); + bool restricted_mem = flags & V4L2_MEMORY_FLAG_RESTRICTED; bool no_previous_buffers = !q_num_bufs; int ret = 0; @@ -1039,6 +1062,9 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, return -ENOBUFS; } + if (restricted_mem_mismatch(restricted_mem, q, memory)) + return -EINVAL; + if (no_previous_buffers) { if (q->waiting_in_dqbuf && *count) { dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); @@ -1057,6 +1083,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, return ret; q->waiting_for_buffers = !q->is_output; set_queue_coherency(q, non_coherent_mem); + q->restricted_mem = restricted_mem; } else { if (q->memory != memory) { dprintk(q, 1, "memory model mismatch\n"); @@ -1064,6 +1091,8 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, } if (!verify_coherency_flags(q, non_coherent_mem)) return -EINVAL; + if (!verify_restricted_mem_flags(q, restricted_mem)) + return -EINVAL; } num_buffers = min(*count, q->max_num_buffers - q_num_bufs); diff --git a/drivers/media/common/videobuf2/videobuf2-v4l2.c b/drivers/media/common/videobuf2/videobuf2-v4l2.c index 293f3d5f1c4e..9ee24e537e0c 100644 --- a/drivers/media/common/videobuf2/videobuf2-v4l2.c +++ b/drivers/media/common/videobuf2/videobuf2-v4l2.c @@ -682,7 +682,7 @@ static void vb2_set_flags_and_caps(struct vb2_queue *q, u32 memory, *flags = 0; } else { /* Clear all unknown flags. */ - *flags &= V4L2_MEMORY_FLAG_NON_COHERENT; + *flags &= V4L2_MEMORY_FLAG_NON_COHERENT | V4L2_MEMORY_FLAG_RESTRICTED; } *caps |= V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS; @@ -698,6 +698,8 @@ static void vb2_set_flags_and_caps(struct vb2_queue *q, u32 memory, *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS; if (q->supports_requests) *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS; + if (q->allow_restricted_mem && q->io_modes & VB2_DMABUF) + *caps |= V4L2_BUF_CAP_SUPPORTS_RESTRICTED_MEM; if (max_num_bufs) { *max_num_bufs = q->max_num_buffers; *caps |= V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS;