[PATCH v4 11/14] media: qcom: camss: Stamp the buffer the write master is filling
From: Loic Poulain
Date: Thu Dec 03 2026 - 11:55:24 EST
The SOF timestamp is read at buf done and applied to the buffer that
just completed, which assumes the frame that last started is the frame
being handed back. With two buffers in flight that is one frame out: the
buffer completing was started by the previous SOF, not the one whose
timestamp is still on record.
Describe the buffer from the frame start instead. Ask the write master
which address it is filling, match it against the buffers the output
holds and stamp that one, leaving vfe_buf_done() to describe only
buffers nobody got to, which keeps hardware without an address report
working as before. A zero timestamp is the sentinel for that, which vb2
guarantees by clearing it on every queue.
Buffers are filled in order, so a frame starting into the second slot
while the first has never been described means that first frame start
never reached us. Say so rather than describing the wrong buffer.
Suggested-by: Gjorgji Rosikopulos <gjorgji.rosikopulos@xxxxxxxxxxxxxxxx>
Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
---
drivers/media/platform/qcom/camss/camss-vfe.c | 70 ++++++++++++++++++++++++---
drivers/media/platform/qcom/camss/camss-vfe.h | 2 +
2 files changed, 66 insertions(+), 6 deletions(-)
diff --git a/drivers/media/platform/qcom/camss/camss-vfe.c b/drivers/media/platform/qcom/camss/camss-vfe.c
index 87fc09f7849c6e147d2a3a8fc85233c2e1f52ebb..0d06728e54456ea56923611cf9c19f3a0cacec98 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe.c
+++ b/drivers/media/platform/qcom/camss/camss-vfe.c
@@ -473,12 +473,15 @@ void vfe_buf_done(struct vfe_device *vfe, int wm)
goto out_unlock;
}
- if (line->sof) {
- ready_buf->vb.vb2_buf.timestamp = atomic64_read(&line->sof->ts);
- ready_buf->vb.sequence = atomic_read(&line->sof->seq);
- } else {
- ready_buf->vb.vb2_buf.timestamp = ktime_get_ns();
- ready_buf->vb.sequence = output->sequence++;
+ /* Tag TS/seq info now if not already done */
+ if (!ready_buf->vb.vb2_buf.timestamp) {
+ if (line->sof) {
+ ready_buf->vb.vb2_buf.timestamp = atomic64_read(&line->sof->ts);
+ ready_buf->vb.sequence = atomic_read(&line->sof->seq);
+ } else {
+ ready_buf->vb.vb2_buf.timestamp = ktime_get_ns();
+ ready_buf->vb.sequence = output->sequence++;
+ }
}
index = 0;
@@ -509,6 +512,55 @@ void vfe_buf_done(struct vfe_device *vfe, int wm)
spin_unlock_irqrestore(&vfe->output_lock, flags);
}
+/* Retrieve the buffer starting at a given address. */
+static struct camss_buffer *vfe_wm_curr_buf(struct vfe_device *vfe,
+ struct vfe_output *output, u32 addr)
+{
+ lockdep_assert_held(&vfe->output_lock);
+
+ for (unsigned int i = 0; i < ARRAY_SIZE(output->buf); i++)
+ if (output->buf[i] && output->buf[i]->addr[0] == addr)
+ return output->buf[i];
+
+ return NULL;
+}
+
+/* On SOF, associate the current buffer with the seq number and timestamp. */
+static void vfe_sof_notify(void *priv)
+{
+ struct vfe_line *line = priv;
+ struct vfe_output *output = &line->output;
+ struct vfe_device *vfe = to_vfe(line);
+ struct camss_buffer *buf;
+ u32 addr;
+
+ guard(spinlock_irqsave)(&vfe->output_lock);
+
+ if (output->state != VFE_OUTPUT_ON)
+ return;
+
+ addr = vfe->res->hw_ops->vfe_wm_curr_addr(vfe, output->wm_idx[0]);
+ if (!addr)
+ return;
+
+ buf = vfe_wm_curr_buf(vfe, output, addr);
+ if (!buf) {
+ dev_warn_ratelimited(vfe->camss->dev,
+ "VFE:%d line:%d SOF while no buffer ready at 0x%x\n",
+ vfe->id, line->id, addr);
+ return;
+ }
+
+ /* Buffers are filled in order */
+ if (buf == output->buf[1] && output->buf[0] &&
+ !output->buf[0]->vb.vb2_buf.timestamp)
+ dev_warn_ratelimited(vfe->camss->dev, "VFE:%d line:%d missed SOF\n",
+ vfe->id, line->id);
+
+ buf->vb.vb2_buf.timestamp = atomic64_read(&line->sof->ts);
+ buf->vb.sequence = atomic_read(&line->sof->seq);
+}
+
int vfe_enable_output_v2(struct vfe_line *line)
{
struct vfe_device *vfe = to_vfe(line);
@@ -569,6 +621,9 @@ int vfe_enable_output_v2(struct vfe_line *line)
ops->reg_update(vfe, line->id);
}
+ if (line->sof && ops->vfe_wm_curr_addr)
+ csid_set_sof_notify(line->sof, vfe_sof_notify, line);
+
spin_unlock_irqrestore(&vfe->output_lock, flags);
return 0;
@@ -849,6 +904,9 @@ int vfe_disable(struct vfe_line *line)
struct vfe_device *vfe = to_vfe(line);
int ret = 0;
+ if (line->sof)
+ csid_set_sof_notify(line->sof, NULL, NULL);
+
vfe_disable_output(line);
vfe_put_output(line);
diff --git a/drivers/media/platform/qcom/camss/camss-vfe.h b/drivers/media/platform/qcom/camss/camss-vfe.h
index b0921e348f2c73a5533c196400c70da187be2cee..ca2d47219545ea91b1bbf1a616bb0b4b36b507d6 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe.h
+++ b/drivers/media/platform/qcom/camss/camss-vfe.h
@@ -122,6 +122,8 @@ struct vfe_hw_ops {
struct vfe_line *line);
void (*vfe_wm_stop)(struct vfe_device *vfe, u8 wm);
void (*vfe_buf_done)(struct vfe_device *vfe, int port_id);
+ /* Address of the frame the write master is filling, 0 if unsupported */
+ u32 (*vfe_wm_curr_addr)(struct vfe_device *vfe, u8 wm);
void (*vfe_wm_update)(struct vfe_device *vfe, u8 wm, u32 addr,
struct vfe_line *line);
};
--
2.34.1