Skip to contentSkip to conversation

ptp_qoriq: fix latency in ptp_qoriq_adjtime() operation.

PATCH In review [email protected] 2 participants 4 messages Jan 10, 2023 – Jan 16, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ptp_qoriq: fix latency in ptp_qoriq_adjtime() operation.

From: Nikhil Gupta <[email protected]>

1588 driver loses about 1us in adjtime operation at PTP slave. This is because adjtime operation uses a slow non-atomic tmr_cnt_read() followed by tmr_cnt_write() operation.
In the above sequence, since the timer counter operation loses about 1us. Instead, tmr_offset register should be programmed with the delta nanoseconds This won't lead to timer counter stopping and losing time while tmr_cnt_write() is being done.
This Patch adds api for tmr_offset_read/write to program the delta nanoseconds in the Timer offset Register.

---
 drivers/ptp/ptp_qoriq.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/ptp/ptp_qoriq.c +30 -6
index 08f4cf0ad9e3..5b6ea6d590be 100644
@@ -48,6 +48,29 @@ static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
 	ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);
 }
 
+static void tmr_offset_write(struct ptp_qoriq *ptp_qoriq, u64 delta_ns)
+{
+	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+	u32 hi = delta_ns >> 32;
+	u32 lo = delta_ns & 0xffffffff;
+
+	ptp_qoriq->write(&regs->ctrl_regs->tmroff_l, lo);
+	ptp_qoriq->write(&regs->ctrl_regs->tmroff_h, hi);
+}
+
+static u64 tmr_offset_read(struct ptp_qoriq *ptp_qoriq)
+{
+	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+	u64 ns;
+	u32 lo, hi;
+
+	lo = ptp_qoriq->read(&regs->ctrl_regs->tmroff_l);
+	hi = ptp_qoriq->read(&regs->ctrl_regs->tmroff_h);
+	ns = ((u64) hi) << 32;
+	ns |= lo;
+	return ns;
+}
+
 /* Caller must hold ptp_qoriq->lock. */
 static void set_alarm(struct ptp_qoriq *ptp_qoriq)
 {
@@ -55,7 +78,9 @@ static void set_alarm(struct ptp_qoriq *ptp_qoriq)
 	u64 ns;
 	u32 lo, hi;
 
-	ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
+	ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq)
+				     + 1500000000ULL;
+
 	ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
 	ns -= ptp_qoriq->tclk_period;
 	hi = ns >> 32;
@@ -207,15 +232,12 @@ EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
 
 int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
 {
-	s64 now;
 	unsigned long flags;
 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
 
 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
-	now = tmr_cnt_read(ptp_qoriq);
-	now += delta;
-	tmr_cnt_write(ptp_qoriq, now);
+	tmr_offset_write(ptp_qoriq, delta);
 	set_fipers(ptp_qoriq);
 
 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
@@ -232,7 +254,7 @@ int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
 
 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
-	ns = tmr_cnt_read(ptp_qoriq);
+	ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq);
 
 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
 
@@ -251,6 +273,8 @@ int ptp_qoriq_settime(struct ptp_clock_info *ptp,
 
 	ns = timespec64_to_ns(ts);
 
+	tmr_offset_write(ptp_qoriq, 0);
+
 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
 	tmr_cnt_write(ptp_qoriq, ns);
Signature · 1 line
2.17.1
author Reviewed-by Yangbo Lu on v1 1/1
#1 lore raw

++ Richard Cochran

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Tuesday, January 10, 2023 5:00 PM
To: [email protected]; Y.B. Lu <[email protected]>; [email protected]; [email protected]
Cc: Vakul Garg <[email protected]>; Rajan Gupta <[email protected]>; Nikhil Gupta <[email protected]>
Subject: [PATCH] ptp_qoriq: fix latency in ptp_qoriq_adjtime() operation.

From: Nikhil Gupta <[email protected]>

1588 driver loses about 1us in adjtime operation at PTP slave.
This is because adjtime operation uses a slow non-atomic tmr_cnt_read() followed by tmr_cnt_write() operation.
In the above sequence, since the timer counter operation loses about 1us.
Instead, tmr_offset register should be programmed with the delta nanoseconds This won't lead to timer counter stopping and losing time while tmr_cnt_write() is being done. This Patch adds api for tmr_offset_read/write to program the delta nanoseconds in the Timer offset Register.

---
 drivers/ptp/ptp_qoriq.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/ptp/ptp_qoriq.c +21 -0
@@ -48,6 +48,29 @@ static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
 	ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);  }
 
+static void tmr_offset_write(struct ptp_qoriq *ptp_qoriq, u64 delta_ns) 
+{
+	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+	u32 hi = delta_ns >> 32;
+	u32 lo = delta_ns & 0xffffffff;
+
+	ptp_qoriq->write(&regs->ctrl_regs->tmroff_l, lo);
+	ptp_qoriq->write(&regs->ctrl_regs->tmroff_h, hi); }
+
+static u64 tmr_offset_read(struct ptp_qoriq *ptp_qoriq) {
+	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+	u64 ns;
+	u32 lo, hi;
+
+	lo = ptp_qoriq->read(&regs->ctrl_regs->tmroff_l);
+	hi = ptp_qoriq->read(&regs->ctrl_regs->tmroff_h);
+	ns = ((u64) hi) << 32;
+	ns |= lo;
+	return ns;
+}
+
 /* Caller must hold ptp_qoriq->lock. */  static void set_alarm(struct ptp_qoriq *ptp_qoriq)  { @@ -55,7 +78,9 @@ static void set_alarm(struct ptp_qoriq *ptp_qoriq)
 	u64 ns;
 	u32 lo, hi;
 

- ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
+ ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq)
+ + 1500000000ULL;
+
ns = div_u64(ns, 1000000000UL) * 1000000000ULL; ns -= ptp_qoriq->tclk_period;
hi = ns >> 32;
@@ -207,15 +232,12 @@ EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);

int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta) {
- s64 now;
unsigned long flags;
struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);

spin_lock_irqsave(&ptp_qoriq->lock, flags);

- now = tmr_cnt_read(ptp_qoriq);
- now += delta;
- tmr_cnt_write(ptp_qoriq, now);
+ tmr_offset_write(ptp_qoriq, delta);
set_fipers(ptp_qoriq);

spin_unlock_irqrestore(&ptp_qoriq->lock, flags); @@ -232,7 +254,7 @@ int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)

spin_lock_irqsave(&ptp_qoriq->lock, flags);

- ns = tmr_cnt_read(ptp_qoriq);
+ ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq);

spin_unlock_irqrestore(&ptp_qoriq->lock, flags);

@@ -251,6 +273,8 @@ int ptp_qoriq_settime(struct ptp_clock_info *ptp,

ns = timespec64_to_ns(ts);

+ tmr_offset_write(ptp_qoriq, 0);
+
spin_lock_irqsave(&ptp_qoriq->lock, flags);

tmr_cnt_write(ptp_qoriq, ns);
--
2.17.1

maintainer
#2 lore raw
In reply to [email protected]

please put [PATCH net-next] in the subject.

Quoted 5 lines · On Tue, 10 Jan 2023 17:00:24 +0530 [email protected] wrote:

From: Nikhil Gupta <[email protected]>

1588 driver loses about 1us in adjtime operation at PTP slave. This is because adjtime operation uses a slow non-atomic tmr_cnt_read() followed by tmr_cnt_write() operation.

So far so good..

Quoted 1 line

In the above sequence, since the timer counter operation loses about 1us.

s/operation/keeps incrementing after the read/ ?

but frankly I don't think this sentence adds much

Quoted 2 lines

Instead, tmr_offset register should be programmed with the delta nanoseconds

missing full stop at the end.
You should describe what the tmr_offset register does.

Quoted 2 lines

This won't lead to timer counter stopping and losing time while tmr_cnt_write() is being done.

Stopping? The timer was actually stopping?

Quoted 1 line

This Patch adds api for tmr_offset_read/write to program the

Use imperative mood.

Quoted 21 lines

delta nanoseconds in the Timer offset Register.

---
 drivers/ptp/ptp_qoriq.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/ptp/ptp_qoriq.c +5 -0
index 08f4cf0ad9e3..5b6ea6d590be 100644
@@ -48,6 +48,29 @@ static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
 	ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);
 }
 
+static void tmr_offset_write(struct ptp_qoriq *ptp_qoriq, u64 delta_ns)
+{
+	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+	u32 hi = delta_ns >> 32;
+	u32 lo = delta_ns & 0xffffffff;

drivers/ptp/ptp_qoriq.c static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns) · line 65

Quoted 10 lines
drivers/ptp/ptp_qoriq.c +10 -0
+
+	ptp_qoriq->write(&regs->ctrl_regs->tmroff_l, lo);
+	ptp_qoriq->write(&regs->ctrl_regs->tmroff_h, hi);
+}
+
+static u64 tmr_offset_read(struct ptp_qoriq *ptp_qoriq)
+{
+	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+	u64 ns;
+	u32 lo, hi;

Order variable lines longest to shortest

Quoted 25 lines
drivers/ptp/ptp_qoriq.c +10 -1
+	lo = ptp_qoriq->read(&regs->ctrl_regs->tmroff_l);
+	hi = ptp_qoriq->read(&regs->ctrl_regs->tmroff_h);
+	ns = ((u64) hi) << 32;
+	ns |= lo;
+	return ns;
+}
+
 /* Caller must hold ptp_qoriq->lock. */
 static void set_alarm(struct ptp_qoriq *ptp_qoriq)
 {
@@ -55,7 +78,9 @@ static void set_alarm(struct ptp_qoriq *ptp_qoriq)
 	u64 ns;
 	u32 lo, hi;
 
-	ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
+	ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq)
+				     + 1500000000ULL;
+
 	ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
 	ns -= ptp_qoriq->tclk_period;
 	hi = ns >> 32;
@@ -207,15 +232,12 @@ EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
 
 int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
 {

drivers/ptp/ptp_qoriq.c EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine); · line 240

Quoted 10 lines
drivers/ptp/ptp_qoriq.c +1 -4
-	s64 now;
 	unsigned long flags;
 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
 
 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
-	now = tmr_cnt_read(ptp_qoriq);
-	now += delta;
-	tmr_cnt_write(ptp_qoriq, now);
+	tmr_offset_write(ptp_qoriq, delta);

Writes to the offset register result in an add operation? Or it's a pure write? What will the offset be after a sequence of following adjtime() calls:
adjtime(+100);
adjtime(+100);
adjtime(+100);
?

Quoted 13 lines
drivers/ptp/ptp_qoriq.c +1 -1
 	set_fipers(ptp_qoriq);
 
 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
@@ -232,7 +254,7 @@ int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
 
 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
-	ns = tmr_cnt_read(ptp_qoriq);
+	ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq);
 
 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
 

drivers/ptp/ptp_qoriq.c int ptp_qoriq_settime(struct ptp_clock_info *ptp, · line 276

Quoted 4 lines
drivers/ptp/ptp_qoriq.c +1 -0
@@ -251,6 +273,8 @@ int ptp_qoriq_settime(struct ptp_clock_info *ptp,
 
 	ns = timespec64_to_ns(ts);
 
+	tmr_offset_write(ptp_qoriq, 0);

Shouldn't this be under the lock?

Quoted 3 lines
drivers/ptp/ptp_qoriq.c +0 -0
 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
 	tmr_cnt_write(ptp_qoriq, ns);
#3 lore raw

PATCH 1/1 ptp_qoriq: fix latency in ptp_qoriq_adjtime() operation.

-----Original Message-----
From: Jakub Kicinski <[email protected]>
Sent: Thursday, January 12, 2023 10:39 AM
To: Nikhil Gupta <[email protected]>
Cc: [email protected]; Y.B. Lu <[email protected]>; [email protected]; [email protected]; Vakul Garg <[email protected]>; Rajan Gupta <[email protected]>
Subject: [EXT] Re: [PATCH] ptp_qoriq: fix latency in ptp_qoriq_adjtime() operation.

Caution: EXT Email

please put [PATCH net-next] in the subject.

Quoted 5 lines · On Tue, 10 Jan 2023 17:00:24 +0530 [email protected] wrote:

From: Nikhil Gupta <[email protected]>

1588 driver loses about 1us in adjtime operation at PTP slave. This is because adjtime operation uses a slow non-atomic tmr_cnt_read() followed by tmr_cnt_write() operation.

So far so good..

Quoted 1 line

In the above sequence, since the timer counter operation loses about 1us.

s/operation/keeps incrementing after the read/ ?

but frankly I don't think this sentence adds much

Quoted 2 lines

Instead, tmr_offset register should be programmed with the delta nanoseconds

missing full stop at the end.
You should describe what the tmr_offset register does.
[Nikhil] : current time is calculated by adding TMROFF_H/L with the timer's counter TMR_CNT_H/L register.

Quoted 2 lines

This won't lead to timer counter stopping and losing time while tmr_cnt_write() is being done.

Stopping? The timer was actually stopping?

Quoted 1 line

This Patch adds api for tmr_offset_read/write to program the

Use imperative mood.

Quoted 18 lines

delta nanoseconds in the Timer offset Register.

---
 drivers/ptp/ptp_qoriq.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/ptp/ptp_qoriq.c index renamed from drivers/ptp/ptp_qoriq.c +0 -0

08f4cf0ad9e3..5b6ea6d590be 100644

drivers/ptp/ptp_qoriq.c +3 -0
@@ -48,6 +48,29 @@ static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
      ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);  }
 
+static void tmr_offset_write(struct ptp_qoriq *ptp_qoriq, u64 
+delta_ns) {
+     struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;

drivers/ptp/ptp_qoriq.c static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns) · line 62

Quoted 10 lines
drivers/ptp/ptp_qoriq.c +10 -0
+     u32 hi = delta_ns >> 32;
+     u32 lo = delta_ns & 0xffffffff;
+
+     ptp_qoriq->write(&regs->ctrl_regs->tmroff_l, lo);
+     ptp_qoriq->write(&regs->ctrl_regs->tmroff_h, hi); }
+
+static u64 tmr_offset_read(struct ptp_qoriq *ptp_qoriq) {
+     struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
+     u64 ns;
+     u32 lo, hi;

Order variable lines longest to shortest

Quoted 24 lines
drivers/ptp/ptp_qoriq.c +7 -0
+     lo = ptp_qoriq->read(&regs->ctrl_regs->tmroff_l);
+     hi = ptp_qoriq->read(&regs->ctrl_regs->tmroff_h);
+     ns = ((u64) hi) << 32;
+     ns |= lo;
+     return ns;
+}
+
 /* Caller must hold ptp_qoriq->lock. */  static void set_alarm(struct 

ptp_qoriq *ptp_qoriq) { @@ -55,7 +78,9 @@ static void set_alarm(struct ptp_qoriq *ptp_qoriq)

      u64 ns;
      u32 lo, hi;
drivers/ptp/ptp_qoriq.c +3 -2
-     ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
+     ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq)
+                                  + 1500000000ULL;
+
      ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
      ns -= ptp_qoriq->tclk_period;
      hi = ns >> 32;
@@ -207,15 +232,12 @@ EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
 
 int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)  {
-     s64 now;

drivers/ptp/ptp_qoriq.c EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine); · line 240

Quoted 10 lines
drivers/ptp/ptp_qoriq.c +1 -3
      unsigned long flags;
      struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct 
 ptp_qoriq, caps);
 
      spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
-     now = tmr_cnt_read(ptp_qoriq);
-     now += delta;
-     tmr_cnt_write(ptp_qoriq, now);
+     tmr_offset_write(ptp_qoriq, delta);

Writes to the offset register result in an add operation?
Or it's a pure write? What will the offset be after a sequence of following adjtime() calls:
adjtime(+100);
adjtime(+100);
adjtime(+100);
?
[Nikhil] : It's a pure write operation, I will be sending the updated version of the patch.
Wherein retaining the earlier offset value and adding to the new.

Quoted 14 lines
drivers/ptp/ptp_qoriq.c +1 -0
      set_fipers(ptp_qoriq);
 
      spin_unlock_irqrestore(&ptp_qoriq->lock, flags); @@ -232,7 
+254,7 @@ int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct 

timespec64 *ts)

      spin_lock_irqsave(&ptp_qoriq->lock, flags);
drivers/ptp/ptp_qoriq.c +1 -1
-     ns = tmr_cnt_read(ptp_qoriq);
+     ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq);
 
      spin_unlock_irqrestore(&ptp_qoriq->lock, flags);

drivers/ptp/ptp_qoriq.c int ptp_qoriq_settime(struct ptp_clock_info *ptp, · line 276

Quoted 4 lines
drivers/ptp/ptp_qoriq.c +1 -0
@@ -251,6 +273,8 @@ int ptp_qoriq_settime(struct ptp_clock_info *ptp,
 
      ns = timespec64_to_ns(ts);
 
+     tmr_offset_write(ptp_qoriq, 0);

Shouldn't this be under the lock?
[Nikhil] : will update this.

Quoted 3 lines
drivers/ptp/ptp_qoriq.c +0 -0
      spin_lock_irqsave(&ptp_qoriq->lock, flags);
 
      tmr_cnt_write(ptp_qoriq, ns);