media: nxp: imx8-isi: Fix single frame capture and optimize buffer usage
v3 cover media: nxp: imx8-isi: Fix single frame capture and optimize buffer usage
This series fixes a hang issue when capturing single frames and improves buffer utilization by prioritizing user buffers over discard buffers.
Patch 1 reduces min_queued_buffers to allow streaming to start without any queued buffers.
Patch 2 changes the buffer selection logic to use pending user buffers first, minimizing unnecessary frame drops at stream start.
---
Changes in v3:
- Expanded commit message to explain the problem in current driver and the benefits gained from this change
- No code changes
- Add Rb tag from Laurent Pinchart
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Add fix tag
- Replace "This ensures" with "ensure"
- Reduce min_queued_buffers from 2 to 0 suggested by Jacopo Mondi
- Put example from commit message to comment in driver suggested by Frank
- Detailed changes can be found in each patch's changelog
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Guoniu Zhou (2):
media: nxp: imx8-isi: Reduce minimum queued buffers from 2 to 0
media: nxp: imx8-isi: Prioritize pending buffers over discard buffers
drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260310-isi_min_buffers-4b490a124223
Best regards,
Signature · 1 line
Guoniu Zhou <[email protected]>
PATCH v3 1/2 media: nxp: imx8-isi: Reduce minimum queued buffers from 2 to 0
From: Guoniu Zhou <[email protected]>
Fix a hang issue when capturing a single frame with applications like cam in libcamera. It would hang waiting for the driver to complete the buffer, but streaming never starts because min_queued_buffers was set to 2.
The ISI module uses a ping-pong buffer mechanism that requires two buffers to be programmed at all times. However, when fewer than 2 user buffers are available, the driver use internal discard buffers to fill the remaining slot(s). Reduce minimum queued buffers from 2 to 0 allows streaming to start without any queued buffers.
---
Changes in v3:
- Add Rb tag from Laurent Pinchart
Changes in v2:
- Reduce min_queued_buffers from 2 to 0 suggested by Jacopo Mondi
https://lore.kernel.org/linux-media/[email protected]/T/#mcd4b7dcc218a02e2f218ba2c83b947ccefd9308b
- Add fix tag
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c +1 -1
@@ -1410,7 +1410,7 @@ int mxc_isi_video_register(struct mxc_isi_pipe *pipe, q->mem_ops = &vb2_dma_contig_memops; q->buf_struct_size =sizeof (struct mxc_isi_buffer ); q->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC ; - q->min_queued_buffers =2 ; + q->min_queued_buffers =0 ; q->lock = &video->lock; q->dev = pipe->isi->dev;
Signature · 1 line
2.34.1
PATCH v3 2/2 media: nxp: imx8-isi: Prioritize pending buffers over discard buffers
In reply to Guoniu ZhouFrom: Guoniu Zhou <[email protected]>
The number of times to use the discard buffer is determined by the out_pending list size:
discard = list_empty(&video->out_pending) ? 2
: list_is_singular(&video->out_pending) ? 1 : 0;
In the current buffer selection logic, when both discard and pending buffers are available, the driver fills hardware slots with discard buffers first which results in an unnecessary frame drop even though a user buffer was queued and ready.
Change the buffer selection logic to use pending buffers first (up to the number available), and only use discard buffers to fill remaining slots when insufficient pending buffers are queued.
This improves behavior by:
- Reducing discarded frames at stream start when user buffers are ready
- Decreasing latency in delivering captured frames to user-space
- Ensuring user buffers are utilized as soon as they are queued
- Improving overall buffer utilization efficiency
---
Changes in v3:
- Expanded commit message to explain the problem in current driver and the benefits gained from this change
- No code changes
Changes in v2:
- Replace "This ensures" with "ensure"
- Put example from commit message to comment in driver suggested by Frank
https://lore.kernel.org/linux-media/[email protected]/T/#m2774912ed31553ef1fdcc840bd6eae53a03ecccd
--- drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c +8 -1
@@ -792,7 +792,14 @@ static void mxc_isi_video_queue_first_buffers(struct mxc_isi_video *video)struct mxc_isi_buffer *buf;struct list_head *list; - list = i < discard ? &video->out_discard : &video->out_pending; +/* +* Queue buffers: prioritize pending buffers, then discard buffers +* For example: +* - 2 pending buffers: both slots use pending buffers +* - 1 pending buffer: first slot uses pending, second uses discard +* - 0 pending buffers: both slots use discard buffers +*/ + list = (i <2 - discard) ? &video->out_pending : &video->out_discard; buf = list_first_entry(list, struct mxc_isi_buffer, list);mxc_isi_channel_set_outbuf (video->pipe, buf->dma_addrs, buf_id);
Signature · 1 line
2.34.1
Re: [PATCH v3 2/2] media: nxp: imx8-isi: Prioritize pending buffers over discard buffers
Hello Guoniu,
Thank you for the patch.
Quoted 18 lines · Guoniu Zhou
From: Guoniu Zhou <[email protected]>
The number of times to use the discard buffer is determined by the
out_pendinglist size:discard =
list_empty(&video->out_pending)? 2: list_is_singular(&video->out_pending) ? 1 : 0;In the current buffer selection logic, when both discard and pending buffers are available, the driver fills hardware slots with discard buffers first which results in an unnecessary frame drop even though a user buffer was queued and ready.
Change the buffer selection logic to use pending buffers first (up to the number available), and only use discard buffers to fill remaining slots when insufficient pending buffers are queued.
Quoted 5 lines
This improves behavior by:
- Reducing discarded frames at stream start when user buffers are ready
- Decreasing latency in delivering captured frames to user-space
- Ensuring user buffers are utilized as soon as they are queued
- Improving overall buffer utilization efficiency
There's a bit of repeat here, but that's OK.
Quoted 35 lines
Signed-off-by Guoniu Zhou <[email protected]>---
Changes in v3:
- Expanded commit message to explain the problem in current driver and the benefits gained from this change
- No code changesChanges in v2:
- Replace "This ensures" with "ensure"
- Put example from commit message to comment in driver suggested by Frank
https://lore.kernel.org/linux-media/[email protected]/T/#m2774912ed31553ef1fdcc840bd6eae53a03ecccd--- drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c +8
-1@@ -792,7 +792,14 @@ static void mxc_isi_video_queue_first_buffers(struct mxc_isi_video *video)struct mxc_isi_buffer *buf;struct list_head *list; - list = i < discard ? &video->out_discard : &video->out_pending; +/* +* Queue buffers: prioritize pending buffers, then discard buffers +* For example: +* - 2 pending buffers: both slots use pending buffers +* - 1 pending buffer: first slot uses pending, second uses discard +* - 0 pending buffers: both slots use discard buffers +*/ + list = (i <2 - discard) ? &video->out_pending : &video->out_discard; buf = list_first_entry(list, struct mxc_isi_buffer, list);mxc_isi_channel_set_outbuf (video->pipe, buf->dma_addrs, buf_id);
Signature · 3 lines
Regards, Laurent Pinchart
Laurent Pinchart
maintainerRe: [PATCH v3 2/2] media: nxp: imx8-isi: Prioritize pending buffers over discard buffers
Quoted 56 lines · Laurent Pinchart
Hello Guoniu,
Thank you for the patch.
Quoted 18 lines · Guoniu Zhou
From: Guoniu Zhou <[email protected]>
The number of times to use the discard buffer is determined by the
out_pendinglist size:discard =
list_empty(&video->out_pending)? 2: list_is_singular(&video->out_pending) ? 1 : 0;In the current buffer selection logic, when both discard and pending buffers are available, the driver fills hardware slots with discard buffers first which results in an unnecessary frame drop even though a user buffer was queued and ready.
Change the buffer selection logic to use pending buffers first (up to the number available), and only use discard buffers to fill remaining slots when insufficient pending buffers are queued.
Quoted 5 lines
This improves behavior by:
- Reducing discarded frames at stream start when user buffers are ready
- Decreasing latency in delivering captured frames to user-space
- Ensuring user buffers are utilized as soon as they are queued
- Improving overall buffer utilization efficiencyThere's a bit of repeat here, but that's OK.
Reviewed-by Laurent Pinchart <[email protected]>Quoted 23 lines
Signed-off-by Guoniu Zhou <[email protected]>---
Changes in v3:
- Expanded commit message to explain the problem in current driver and the benefits gained from this change
- No code changesChanges in v2:
- Replace "This ensures" with "ensure"
- Put example from commit message to comment in driver suggested by Frank
https://lore.kernel.org/linux-media/[email protected]/T/#m2774912ed31553ef1fdcc840bd6eae53a03ecccd--- drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c +0
-0@@ -792,7 +792,14 @@ static void mxc_isi_video_queue_first_buffers(struct mxc_isi_video *video)struct mxc_isi_buffer *buf;struct list_head *list;
Quoted 8 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c static void mxc_isi_video_queue_first_buffers(struct mxc_isi_video *video) · line 801
Quoted 8 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c +7
-1- list = i < discard ? &video->out_discard : &video->out_pending; +/* +* Queue buffers: prioritize pending buffers, then discard buffers +* For example: +* - 2 pending buffers: both slots use pending buffers +* - 1 pending buffer: first slot uses pending, second uses discard +* - 0 pending buffers: both slots use discard buffers +*/
Having a second look, I think this is a bit too verbose. Unless you object, I'll write
* Queue buffers: prioritize pending buffers, then discard
* buffers.
No need to submit a new version.
Quoted 4 lines
Quoted 4 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-video.c +1
-0+ list = (i <2 - discard) ? &video->out_pending : &video->out_discard; buf = list_first_entry(list, struct mxc_isi_buffer, list);mxc_isi_channel_set_outbuf (video->pipe, buf->dma_addrs, buf_id);
Signature · 3 lines
Regards, Laurent Pinchart