From: "Gjorgji Rosikopulos (Consultant)" <gjorgji.rosikopulos@oss.qualcomm.com>
To: Loic Poulain <loic.poulain@oss.qualcomm.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
Bryan O'Donoghue <bryan.odonoghue@linaro.org>,
Vladimir Zapolskiy <vladimir.zapolskiy@linaro.org>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arm-msm@vger.kernel.org
Subject: Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
Date: Wed, 25 Nov 2026 19:23:42 +0200 [thread overview]
Message-ID: <d56b1e2b-ff7a-4587-b45e-2607ab85861d@oss.qualcomm.com> (raw)
In-Reply-To: <CAFEp6-1Sk1rjhqsdm+-GHBESdExWr4vVX5wMcFzEpMt8zQJhsA@mail.gmail.com>
On 11/25/2026 6:21 PM, Loic Poulain wrote:
> On Wed, Nov 25, 2026 at 4:01 PM Gjorgji Rosikopulos (Consultant)
> <gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>>
>>
>>
>> On 11/25/2026 3:18 PM, Loic Poulain wrote:
>>> On Wed, Nov 25, 2026 at 1:06 PM Gjorgji Rosikopulos (Consultant)
>>> <gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>>>>
>>>>
>>>>
>>>> On 11/25/2026 2:04 PM, Gjorgji Rosikopulos (Consultant) wrote:
>>>>> Hi Loic,
>>>>>
>>>>> On 11/25/2026 12:19 PM, Loic Poulain wrote:
>>>>>> Modern CSID revisions latch the global timestamp counter at SOF. That
>>>>>> counter is the ARM architected counter, so correlating it with
>>>>>> CLOCK_MONOTONIC through ktime_get_snapshot_id() turns the latched value
>>>>>> into the time the frame actually started, rather than the time the
>>>>>> interrupt handler ran.
>>>>>>
>>>>>> Add a hardware operation returning that counter value and do the
>>>>>> correlation in one place, leaving a CSID revision with nothing to do but
>>>>>> read its own registers. Revisions not implementing it keep being
>>>>>> timestamped at queue time.
>>>>>>
>>>>>> Suggested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
>>>>>> Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
>>>>>> ---
>>>>>> drivers/media/platform/qcom/camss/camss-csid.c | 72 ++++++++++++++++++++++++++
>>>>>> drivers/media/platform/qcom/camss/camss-csid.h | 9 ++++
>>>>>> 2 files changed, 81 insertions(+)
>>>>>>
>>>>>> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
>>>>>> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 100644
>>>>>> --- a/drivers/media/platform/qcom/camss/camss-csid.c
>>>>>> +++ b/drivers/media/platform/qcom/camss/camss-csid.c
>>>>>> @@ -8,14 +8,17 @@
>>>>>> * Copyright (C) 2015-2018 Linaro Ltd.
>>>>>> */
>>>>>> #include <linux/clk.h>
>>>>>> +#include <linux/clocksource_ids.h>
>>>>>> #include <linux/completion.h>
>>>>>> #include <linux/interrupt.h>
>>>>>> #include <linux/io.h>
>>>>>> #include <linux/kernel.h>
>>>>>> +#include <linux/math64.h>
>>>>>> #include <linux/of.h>
>>>>>> #include <linux/platform_device.h>
>>>>>> #include <linux/pm_runtime.h>
>>>>>> #include <linux/regulator/consumer.h>
>>>>>> +#include <linux/timekeeping.h>
>>>>>> #include <media/media-entity.h>
>>>>>> #include <media/mipi-csi2.h>
>>>>>> #include <media/v4l2-device.h>
>>>>>> @@ -37,6 +40,16 @@
>>>>>>
>>>>>> #define LANE_CFG_BITWIDTH 4
>>>>>>
>>>>>> +/*
>>>>>> + * The CSID latches the global timestamp counter on SOF. That counter is
>>>>>> + * the ARM architected counter, 56 bit wide and ticking at 19.2 MHz.
>>>>>> + */
>>>>>> +#define CSID_TIMESTAMP_WIDTH 56
>>>>>> +#define CSID_TIMESTAMP_MASK GENMASK_ULL(CSID_TIMESTAMP_WIDTH - 1, 0)
>>>>>> +#define CSID_TIMESTAMP_NS_MUL 10000 /* NSEC_PER_SEC / 19.2 MHz, reduced */
>>>>>> +#define CSID_TIMESTAMP_NS_DIV 192
>>>>>> +#define CSID_TIMESTAMP_MAX_AGE_NS (2 * NSEC_PER_MSEC)
>>>>>> +
>>>>>> #define MSM_CSID_NAME "msm_csid"
>>>>>>
>>>>>> const char * const csid_testgen_modes[] = {
>>>>>> @@ -1370,12 +1383,71 @@ atomic_t *csid_get_sof_seq(struct media_pad *pad)
>>>>>> return &csid->sof_seq[pad->index - MSM_CSID_PAD_FIRST_SRC];
>>>>>> }
>>>>>>
>>>>>> +static u64 csid_timestamp_to_ns(u64 ticks)
>>>>>> +{
>>>>>> + return mul_u64_u32_div(ticks, CSID_TIMESTAMP_NS_MUL, CSID_TIMESTAMP_NS_DIV);
>>>>>> +}
>>>>>> +
>>>>>> +static u64 csid_timestamp_to_monotonic(struct csid_device *csid, u64 ticks)
>>>>>> +{
>>>>>> + struct system_time_snapshot snap;
>>>>>> + u64 delta, delta_ns, now;
>>>>>> +
>>>>>> + ktime_get_snapshot_id(CLOCK_MONOTONIC, &snap);
>>>>>> +
>>>>>> + /*
>>>>>> + * Translating requires reading the counter the CSID latched from.
>>>>>> + * If timekeeping is driven by something else there is nothing to
>>>>>> + * correlate against, so let the caller fall back to IRQ time.
>>>>>> + */
>>>>>> + if (!snap.valid || snap.cs_id != CSID_ARM_ARCH_COUNTER) {
>>>>>> + dev_warn_once(csid->camss->dev,
>>>>>> + "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
>>>>>> + csid->id, snap.cs_id);
>>>>>> + return 0;
>>>>>> + }
>>>>>> +
>>>>>> + /* How long ago, in counter ticks, the timestamp was latched */
>>>>>> + delta = (snap.cycles - ticks) & CSID_TIMESTAMP_MASK;
>>>>>> + delta_ns = csid_timestamp_to_ns(delta);
>>>>>> + now = ktime_to_ns(snap.systime);
>>>>>> +
>>>>>> + /* Fall back rather than reporting a nonsensical timestamp */
>>>>>> + if (delta_ns > now) {
>>>>>> + dev_warn_once(csid->camss->dev,
>>>>>> + "CSID%u: SOF timestamp predates CLOCK_MONOTONIC, ignoring\n",
>>>>>> + csid->id);
>>>>>> + return 0;
>>>>>> + }
>>>>>> +
>>>>>> + if (delta_ns > CSID_TIMESTAMP_MAX_AGE_NS)
>>>>>> + dev_warn_ratelimited(csid->camss->dev,
>>>>>> + "CSID%u: Too slow! SOF timestamp is %llu us old\n",
>>>>>> + csid->id, delta_ns / NSEC_PER_USEC);
>>>>>> +
>>>>>> + return now - delta_ns;
>>>>>> +}
>>>>>> +
>>>>>> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
>>>>>> +{
>>>>>> + const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
>>>>>> +
>>>>>> + if (!hw_ops->sof_ts)
>>>>>> + return 0;
>>>>>> +
>>>>>> + return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
>>>>>
>>>>> I am not sure if we need to do this every SOF. Most probably the delta will
>>>>> remain the same during the streaming, so I think it's best to use the delta within
>>>>> tolerance and then apply that for the whole streaming session. Also, I think it's better
>>>>> to have a constant delta from the monotonic clock rather than a fluctuating one.
>>>>
>>>> I think it will be the best to send both timestamps to userspace, and give the freedom
>>>> to userspace to decide by him self. But the API is not allowing that as discussed.
>>>
>>> No, the delta is not constant. In fact, that's precisely why we use
>>> it, it captures the variable CPU and software latencies between the
>>> hardware event and the timestamp acquisition. By combining the
>>> captured hardware timestamp with the current system timing
>>> information, we can reconstruct an accurate event timestamp that
>>> remains aligned with the Sensor/CSI timing, rather than being affected
>>> by software scheduling and processing delays. According to my testing,
>>> the resulting timing error is below 1 µs, from which we can derive an
>>> accurate sensor framerate.
>>>
>>> --
>>> watching /dev/v4l-subdev2 (msm_csid0), id 0
>>> id0 478 ev 60.06 fps 16.650 ms
>>> events : 478
>>> timestamp (mono) : 180.160315690 .. 188.102256050
>>> duration : 7.942 s
>>> frame rate : 60.061 fps
>>> frame_sequence : 539 .. 1016 (missing: 0)
>>> interval (ms) : mean 16.650 median 16.650 min 16.650 max 16.650
>>> p95 16.650 p99 16.650
>>> ---
>>
>> Ok i will test the changes on my side. Just for other people we have conflicting names
>> CSID (stands from clock fw perspective to Clock Source ID, and from camss for the
>> csi decoder). If you are sure that the for csi decoder the CSID_ARM_ARCH_COUNTER
>> is always the clock source this will work i agree.
>> But if that is not the case and we end up with big delta what is the idea to do
>> that every frame. It is kind of confusing to me at least. Also i think we need
>> to check for the other platforms if the clock source is always the same.
>
> Yes, the camera hardware always snapshots the architecture counter. If
> we detect that the monotonic clock is not derived from the same timer,
> we simply fall back to the traditional IRQ timestamping path. The same
> fallback is used if the computed delta is found to be inconsistent.
If the both clocks are from same timer there are two checks:
1. Big delta big delta something is completely wrong,
most probably the csid timestamp has some stall data.
2. snap.cs_id != CSID_ARM_ARCH_COUNTER. If that is true do
we need to check on every SOF, most probably every time
you will get the same result.
If the error can be recovered on next SOF maybe make sense
to enter this function. But i think in both cases most probably
you will end up there for every SOF, which is for each pipeline.
If all all streaming 4 times per frame.
>
> When Linux receives the interrupt, we know:
> - The counter value now (C_NOW)
> - The Linux timestamp now (T_NOW)
> - The counter value captured by hardware when the frame started (C_SOF)
>
> Since the Linux monotonic clock and the camera counter both come from
> the same hardware timer, we can convert the difference between C_NOW
> and C_SOF into time (T()) and "rewind" from T_NOW back to the
> **exact** frame start time:
>
> T_SOF = T_NOW - T(C_NOW - C_SOF)
>
> Regards,
> Loic
>
>>
>> ~Gjorgji
>>
>>>
>>> Regards,
>>> Loic
>>>
>>>>
>>>>>
>>>>> ~Gjorgji
>>>>>
>>>>>> +}
>>>>>> +
>>>>>> void csid_sof_event(struct csid_device *csid, unsigned int id)
>>>>>> {
>>>>>> + u64 ts = csid_hw_sof_ts(csid, id);
>>>>>> + struct timespec64 sof = ns_to_timespec64(ts);
>>>>>> struct v4l2_event event = {
>>>>>> .type = V4L2_EVENT_FRAME_SYNC,
>>>>>> .u.frame_sync.frame_sequence =
>>>>>> atomic_inc_return(&csid->sof_seq[id]),
>>>>>> + .timestamp.tv_sec = sof.tv_sec,
>>>>>> + .timestamp.tv_nsec = sof.tv_nsec,
>>>>>> };
>>>>>>
>>>>>> if (atomic_read(&csid->frame_sync_users))
>>>>>> diff --git a/drivers/media/platform/qcom/camss/camss-csid.h b/drivers/media/platform/qcom/camss/camss-csid.h
>>>>>> index ccafdbf3a9fe0c282da68ccdd04661b3fab81ff5..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
>>>>>> --- a/drivers/media/platform/qcom/camss/camss-csid.h
>>>>>> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
>>>>>> @@ -111,6 +111,15 @@ struct csid_hw_ops {
>>>>>> */
>>>>>> int (*reset)(struct csid_device *csid);
>>>>>>
>>>>>> + /*
>>>>>> + * sof_ts - Read the counter value the hardware latched at SOF
>>>>>> + * @csid: CSID device
>>>>>> + * @id: CSID src-stream ID
>>>>>> + *
>>>>>> + * Return the raw counter value of the most recent SOF
>>>>>> + */
>>>>>> + u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
>>>>>> +
>>>>>> /*
>>>>>> * src_pad_code - Pick an output/src format based on the input/sink format
>>>>>> * @csid: CSID device
>>>>>>
>>>>>
>>>>
>>
next prev parent reply other threads:[~2026-11-25 17:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware " Loic Poulain
2026-11-25 10:19 ` [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
2026-11-25 12:29 ` Gjorgji Rosikopulos (Consultant)
2026-11-25 12:58 ` Vladimir Zapolskiy
2026-11-25 13:21 ` Loic Poulain
2026-11-25 10:19 ` [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp Loic Poulain
2026-11-25 12:04 ` Gjorgji Rosikopulos (Consultant)
2026-11-25 12:06 ` Gjorgji Rosikopulos (Consultant)
2026-11-25 13:18 ` Loic Poulain
2026-11-25 15:01 ` Gjorgji Rosikopulos (Consultant)
2026-11-25 16:21 ` Loic Poulain
2026-11-25 17:23 ` Gjorgji Rosikopulos (Consultant) [this message]
2026-11-25 10:19 ` [PATCH v2 3/7] media: qcom: camss: csid-340: Report the hardware " Loic Poulain
2026-11-25 10:19 ` [PATCH v2 4/7] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
2026-11-25 10:19 ` [PATCH v2 5/7] media: camss: csid-gen2: Report the hardware SOF timestamp Loic Poulain
2026-11-25 10:19 ` [PATCH v2 6/7] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
2026-11-25 10:19 ` [PATCH v2 7/7] media: qcom: camss: Tell userspace we're using start-of-frame timestamps Loic Poulain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d56b1e2b-ff7a-4587-b45e-2607ab85861d@oss.qualcomm.com \
--to=gjorgji.rosikopulos@oss.qualcomm.com \
--cc=bryan.odonoghue@linaro.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=loic.poulain@oss.qualcomm.com \
--cc=mchehab@kernel.org \
--cc=vladimir.zapolskiy@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®