[PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values

Tariq Toukan posted 3 patches 1 month, 3 weeks ago
[PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Tariq Toukan 1 month, 3 weeks ago
From: Carolina Jubran <cjubran@nvidia.com>

Introduce two new ioctl commands, PTP_SYS_OFFSET_PRECISE_CYCLES and
PTP_SYS_OFFSET_EXTENDED_CYCLES, to allow user space to access the
raw free-running cycle counter from PTP devices.

These ioctls are variants of the existing PRECISE and EXTENDED
offset queries, but instead of returning device time in realtime,
they return the raw cycle counter value.

Signed-off-by: Carolina Jubran <cjubran@nvidia.com>
Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 drivers/ptp/ptp_chardev.c      | 34 ++++++++++++++++++++++++++--------
 include/uapi/linux/ptp_clock.h |  4 ++++
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/drivers/ptp/ptp_chardev.c b/drivers/ptp/ptp_chardev.c
index 4ca5a464a46a..e9719f365aab 100644
--- a/drivers/ptp/ptp_chardev.c
+++ b/drivers/ptp/ptp_chardev.c
@@ -285,17 +285,21 @@ static long ptp_enable_pps(struct ptp_clock *ptp, bool enable)
 		return ops->enable(ops, &req, enable);
 }
 
-static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg)
+typedef int (*ptp_crosststamp_fn)(struct ptp_clock_info *,
+				  struct system_device_crosststamp *);
+
+static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg,
+				   ptp_crosststamp_fn crosststamp_fn)
 {
 	struct ptp_sys_offset_precise precise_offset;
 	struct system_device_crosststamp xtstamp;
 	struct timespec64 ts;
 	int err;
 
-	if (!ptp->info->getcrosststamp)
+	if (!crosststamp_fn)
 		return -EOPNOTSUPP;
 
-	err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
+	err = crosststamp_fn(ptp->info, &xtstamp);
 	if (err)
 		return err;
 
@@ -313,12 +317,17 @@ static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg)
 	return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAULT : 0;
 }
 
-static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg)
+typedef int (*ptp_gettimex_fn)(struct ptp_clock_info *,
+			       struct timespec64 *,
+			       struct ptp_system_timestamp *);
+
+static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg,
+				    ptp_gettimex_fn gettimex_fn)
 {
 	struct ptp_sys_offset_extended *extoff __free(kfree) = NULL;
 	struct ptp_system_timestamp sts;
 
-	if (!ptp->info->gettimex64)
+	if (!gettimex_fn)
 		return -EOPNOTSUPP;
 
 	extoff = memdup_user(arg, sizeof(*extoff));
@@ -346,7 +355,7 @@ static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg)
 		struct timespec64 ts;
 		int err;
 
-		err = ptp->info->gettimex64(ptp->info, &ts, &sts);
+		err = gettimex_fn(ptp->info, &ts, &sts);
 		if (err)
 			return err;
 
@@ -497,11 +506,13 @@ long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
 
 	case PTP_SYS_OFFSET_PRECISE:
 	case PTP_SYS_OFFSET_PRECISE2:
-		return ptp_sys_offset_precise(ptp, argptr);
+		return ptp_sys_offset_precise(ptp, argptr,
+					      ptp->info->getcrosststamp);
 
 	case PTP_SYS_OFFSET_EXTENDED:
 	case PTP_SYS_OFFSET_EXTENDED2:
-		return ptp_sys_offset_extended(ptp, argptr);
+		return ptp_sys_offset_extended(ptp, argptr,
+					       ptp->info->gettimex64);
 
 	case PTP_SYS_OFFSET:
 	case PTP_SYS_OFFSET2:
@@ -523,6 +534,13 @@ long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
 	case PTP_MASK_EN_SINGLE:
 		return ptp_mask_en_single(pccontext->private_clkdata, argptr);
 
+	case PTP_SYS_OFFSET_PRECISE_CYCLES:
+		return ptp_sys_offset_precise(ptp, argptr,
+					      ptp->info->getcrosscycles);
+
+	case PTP_SYS_OFFSET_EXTENDED_CYCLES:
+		return ptp_sys_offset_extended(ptp, argptr,
+					       ptp->info->getcyclesx64);
 	default:
 		return -ENOTTY;
 	}
diff --git a/include/uapi/linux/ptp_clock.h b/include/uapi/linux/ptp_clock.h
index 18eefa6d93d6..65f187b5f0d0 100644
--- a/include/uapi/linux/ptp_clock.h
+++ b/include/uapi/linux/ptp_clock.h
@@ -245,6 +245,10 @@ struct ptp_pin_desc {
 	_IOWR(PTP_CLK_MAGIC, 18, struct ptp_sys_offset_extended)
 #define PTP_MASK_CLEAR_ALL  _IO(PTP_CLK_MAGIC, 19)
 #define PTP_MASK_EN_SINGLE  _IOW(PTP_CLK_MAGIC, 20, unsigned int)
+#define PTP_SYS_OFFSET_PRECISE_CYCLES \
+	_IOWR(PTP_CLK_MAGIC, 21, struct ptp_sys_offset_precise)
+#define PTP_SYS_OFFSET_EXTENDED_CYCLES \
+	_IOWR(PTP_CLK_MAGIC, 22, struct ptp_sys_offset_extended)
 
 struct ptp_extts_event {
 	struct ptp_clock_time t; /* Time event occurred. */
-- 
2.40.1
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Richard Cochran 1 month ago
On Tue, Aug 12, 2025 at 05:17:06PM +0300, Tariq Toukan wrote:
> From: Carolina Jubran <cjubran@nvidia.com>
> 
> Introduce two new ioctl commands, PTP_SYS_OFFSET_PRECISE_CYCLES and
> PTP_SYS_OFFSET_EXTENDED_CYCLES, to allow user space to access the
> raw free-running cycle counter from PTP devices.
> 
> These ioctls are variants of the existing PRECISE and EXTENDED
> offset queries, but instead of returning device time in realtime,
> they return the raw cycle counter value.
> 
> Signed-off-by: Carolina Jubran <cjubran@nvidia.com>
> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
> Signed-off-by: Tariq Toukan <tariqt@nvidia.com>

Acked-by: Richard Cochran <richardcochran@gmail.com>
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Paolo Abeni 1 month, 2 weeks ago
On 8/12/25 4:17 PM, Tariq Toukan wrote:
> From: Carolina Jubran <cjubran@nvidia.com>
> 
> Introduce two new ioctl commands, PTP_SYS_OFFSET_PRECISE_CYCLES and
> PTP_SYS_OFFSET_EXTENDED_CYCLES, to allow user space to access the
> raw free-running cycle counter from PTP devices.
> 
> These ioctls are variants of the existing PRECISE and EXTENDED
> offset queries, but instead of returning device time in realtime,
> they return the raw cycle counter value.
> 
> Signed-off-by: Carolina Jubran <cjubran@nvidia.com>
> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
> Signed-off-by: Tariq Toukan <tariqt@nvidia.com>

Hi Richard,

can we have a formal ack here?

Thanks,

Paolo
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Mark Bloch 1 month, 1 week ago

On 19/08/2025 11:43, Paolo Abeni wrote:
> On 8/12/25 4:17 PM, Tariq Toukan wrote:
>> From: Carolina Jubran <cjubran@nvidia.com>
>>
>> Introduce two new ioctl commands, PTP_SYS_OFFSET_PRECISE_CYCLES and
>> PTP_SYS_OFFSET_EXTENDED_CYCLES, to allow user space to access the
>> raw free-running cycle counter from PTP devices.
>>
>> These ioctls are variants of the existing PRECISE and EXTENDED
>> offset queries, but instead of returning device time in realtime,
>> they return the raw cycle counter value.
>>
>> Signed-off-by: Carolina Jubran <cjubran@nvidia.com>
>> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
>> Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
> 
> Hi Richard,
> 
> can we have a formal ack here?

Gentle reminder.

Mark
> 
> Thanks,
> 
> Paolo
>
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Richard Cochran 1 month, 1 week ago
On Mon, Aug 25, 2025 at 08:52:52PM +0300, Mark Bloch wrote:
> 
> 
> On 19/08/2025 11:43, Paolo Abeni wrote:
> > can we have a formal ack here?
> 
> Gentle reminder.

I'm travelling ATM.  Please ping again next week.

Thanks,
Richard
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Carolina Jubran 1 month ago
On 28/08/2025 13:00, Richard Cochran wrote:
> On Mon, Aug 25, 2025 at 08:52:52PM +0300, Mark Bloch wrote:
>>
>> On 19/08/2025 11:43, Paolo Abeni wrote:
>>> can we have a formal ack here?
>> Gentle reminder.
> I'm travelling ATM.  Please ping again next week.
>
> Thanks,
> Richard

Hi Richard,

Just a kind reminder.

Thanks!

Carolina
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Richard Cochran 1 month ago
On Thu, Sep 04, 2025 at 03:09:23PM +0300, Carolina Jubran wrote:
> 
> On 28/08/2025 13:00, Richard Cochran wrote:
> > On Mon, Aug 25, 2025 at 08:52:52PM +0300, Mark Bloch wrote:
> > > 
> > > On 19/08/2025 11:43, Paolo Abeni wrote:
> > > > can we have a formal ack here?

Looks good to me.

Thanks,
Richard
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Tariq Toukan 3 weeks, 4 days ago

On 04/09/2025 16:14, Richard Cochran wrote:
> On Thu, Sep 04, 2025 at 03:09:23PM +0300, Carolina Jubran wrote:
>>
>> On 28/08/2025 13:00, Richard Cochran wrote:
>>> On Mon, Aug 25, 2025 at 08:52:52PM +0300, Mark Bloch wrote:
>>>>
>>>> On 19/08/2025 11:43, Paolo Abeni wrote:
>>>>> can we have a formal ack here?
> 
> Looks good to me.
> 
> Thanks,
> Richard
> 

Hi Paolo,
A kind reminder, we got the needed ack here.
Yet patchwork state is still 'Needs ACK'.

Regards,
Tariq
Re: [PATCH net-next V2 1/3] ptp: Add ioctl commands to expose raw cycle counter values
Posted by Paolo Abeni 3 weeks, 4 days ago
On 9/9/25 7:52 AM, Tariq Toukan wrote:
> On 04/09/2025 16:14, Richard Cochran wrote:
>> On Thu, Sep 04, 2025 at 03:09:23PM +0300, Carolina Jubran wrote:
>>>
>>> On 28/08/2025 13:00, Richard Cochran wrote:
>>>> On Mon, Aug 25, 2025 at 08:52:52PM +0300, Mark Bloch wrote:
>>>>>
>>>>> On 19/08/2025 11:43, Paolo Abeni wrote:
>>>>>> can we have a formal ack here?
>>
>> Looks good to me.
>>
>> Thanks,
>> Richard
>>
> 
> Hi Paolo,
> A kind reminder, we got the needed ack here.
> Yet patchwork state is still 'Needs ACK'.

I was traveling in the past days.

I see Jakub has some perplexity WRT this series. I read that as "I
prefer don't touch it" (as opposed to a "no-go") and I think that the
feature makes sense.

I'm applying this now.

/P