Skip to contentSkip to conversation

bnxt_en: cache only 24 bits of hw counter

PATCH Superseded Vadim Fedorenko 3 participants 4 messages Oct 28, 2024 – Oct 29, 2024

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 bnxt_en: cache only 24 bits of hw counter

This hardware can provide only 48 bits of cycle counter. We can leave only 24 bits in the cache to extend RX timestamps from 32 bits to 48 bits. Lower 8 bits of the cached value will be used to check for roll-over while extending to full 48 bits.
This change makes cache writes atomic even on 32 bit platforms and we can simply use READ_ONCE()/WRITE_ONCE() pair and remove spinlock. The configuration structure will be also reduced by 4 bytes.

---
v2 -> v3:
* define BNXT_HI_TIMER_SHIFT for shift
* add comment explaining that 8 bits of cahced value will be used to check for roll-over

---
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c |  8 ++++----
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h | 18 +++---------------
 2 files changed, 7 insertions(+), 19 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +4 -4
index fa514be87650..820c7e83e586 100644
@@ -106,7 +106,7 @@ static void bnxt_ptp_get_current_time(struct bnxt *bp)
 	if (!ptp)
 		return;
 	spin_lock_irqsave(&ptp->ptp_lock, flags);
-	WRITE_ONCE(ptp->old_time, ptp->current_time);
+	WRITE_ONCE(ptp->old_time, (u32)(ptp->current_time >> BNXT_HI_TIMER_SHIFT));
 	bnxt_refclk_read(bp, NULL, &ptp->current_time);
 	spin_unlock_irqrestore(&ptp->ptp_lock, flags);
 }
@@ -174,7 +174,7 @@ void bnxt_ptp_update_current_time(struct bnxt *bp)
 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 
 	bnxt_refclk_read(ptp->bp, NULL, &ptp->current_time);
-	WRITE_ONCE(ptp->old_time, ptp->current_time);
+	WRITE_ONCE(ptp->old_time, (u32)(ptp->current_time >> BNXT_HI_TIMER_SHIFT));
 }
 
 static int bnxt_ptp_adjphc(struct bnxt_ptp_cfg *ptp, s64 delta)
@@ -813,7 +813,7 @@ int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts)
 	if (!ptp)
 		return -ENODEV;
 
-	BNXT_READ_TIME64(ptp, time, ptp->old_time);
+	time = (u64)(READ_ONCE(ptp->old_time) << BNXT_HI_TIMER_SHIFT);
 	*ts = (time & BNXT_HI_TIMER_MASK) | pkt_ts;
 	if (pkt_ts < (time & BNXT_LO_TIMER_MASK))
 		*ts += BNXT_LO_TIMER_MASK + 1;
@@ -1079,7 +1079,7 @@ int bnxt_ptp_init(struct bnxt *bp, bool phc_cfg)
 
 		spin_lock_irqsave(&ptp->ptp_lock, flags);
 		bnxt_refclk_read(bp, NULL, &ptp->current_time);
-		WRITE_ONCE(ptp->old_time, ptp->current_time);
+		WRITE_ONCE(ptp->old_time, (u32)(ptp->current_time >> BNXT_HI_TIMER_SHIFT));
 		spin_unlock_irqrestore(&ptp->ptp_lock, flags);
 		ptp_schedule_worker(ptp->ptp_clock, 0);
 	}
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h +3 -15
index f322466ecad3..3ac5cbc1c5c4 100644
@@ -21,6 +21,7 @@
 #define BNXT_DEVCLK_FREQ	1000000
 #define BNXT_LO_TIMER_MASK	0x0000ffffffffUL
 #define BNXT_HI_TIMER_MASK	0xffff00000000UL
+#define BNXT_HI_TIMER_SHIFT	24
 
 #define BNXT_PTP_DFLT_TX_TMO	1000 /* ms */
 #define BNXT_PTP_QTS_TIMEOUT	1000
@@ -106,10 +107,11 @@ struct bnxt_ptp_cfg {
 	/* serialize ts tx request queuing */
 	spinlock_t		ptp_tx_lock;
 	u64			current_time;
-	u64			old_time;
 	unsigned long		next_period;
 	unsigned long		next_overflow_check;
 	u32			cmult;
+	/* cache of upper 24 bits of cyclecoutner. 8 bits are used to check for roll-over */
+	u32			old_time;
 	/* a 23b shift cyclecounter will overflow in ~36 mins.  Check overflow every 18 mins. */
 	#define BNXT_PHC_OVERFLOW_PERIOD	(18 * 60 * HZ)
 
@@ -145,20 +147,6 @@ struct bnxt_ptp_cfg {
 	struct bnxt_ptp_stats	stats;
 };
 
-#if BITS_PER_LONG == 32
-#define BNXT_READ_TIME64(ptp, dst, src)				\
-do {								\
-	unsigned long flags;					\
-								\
-	spin_lock_irqsave(&(ptp)->ptp_lock, flags);		\
-	(dst) = (src);						\
-	spin_unlock_irqrestore(&(ptp)->ptp_lock, flags);	\
-} while (0)
-#else
-#define BNXT_READ_TIME64(ptp, dst, src)		\
-	((dst) = READ_ONCE(src))
-#endif
-
 #define BNXT_PTP_INC_TX_AVAIL(ptp)		\
 do {						\
 	spin_lock_bh(&(ptp)->ptp_tx_lock);	\
Signature · 1 line
2.43.5
#1 lore raw

PATCH v3 2/2 bnxt_en: replace PTP spinlock with seqlock

We can see high contention on ptp_lock while doing RX timestamping on high packet rates over several queues. Spinlock is not effecient to protect timecounter for RX timestamps when reads are the most usual operations and writes are only occasional. It's better to use seqlock in such cases.

---
v3:
- remove unused variable
v2:
- use read_excl lock to serialize reg access with FW reset

---
 drivers/net/ethernet/broadcom/bnxt/bnxt.c     | 19 +++--
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c | 73 ++++++-------------
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h | 14 +++-
 3 files changed, 46 insertions(+), 60 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt.c +9 -10
index 6dd6541d8619..a7eabef6e7ea 100644
@@ -2254,11 +2254,8 @@ static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
 
 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
-				unsigned long flags;
 
-				spin_lock_irqsave(&ptp->ptp_lock, flags);
-				ns = timecounter_cyc2time(&ptp->tc, ts);
-				spin_unlock_irqrestore(&ptp->ptp_lock, flags);
+				ns = bnxt_timecounter_cyc2time(ptp, ts);
 				memset(skb_hwtstamps(skb), 0,
 				       sizeof(*skb_hwtstamps(skb)));
 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
@@ -2764,12 +2761,12 @@ static int bnxt_async_event_process(struct bnxt *bp,
 				if (!ptp)
 					goto async_event_process_exit;
 
-				spin_lock_irqsave(&ptp->ptp_lock, flags);
 				bnxt_ptp_update_current_time(bp);
 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
 				       BNXT_PHC_BITS) | ptp->current_time);
+				write_seqlock_irqsave(&ptp->ptp_lock, flags);
 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
-				spin_unlock_irqrestore(&ptp->ptp_lock, flags);
+				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
 			}
 			break;
 		}
@@ -13496,12 +13493,13 @@ static void bnxt_force_fw_reset(struct bnxt *bp)
 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
 		return;
 
+	/* we have to serialize with bnxt_refclk_read()*/
 	if (ptp) {
 		unsigned long flags;
 
-		spin_lock_irqsave(&ptp->ptp_lock, flags);
+		write_seqlock_irqsave(&ptp->ptp_lock, flags);
 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
-		spin_unlock_irqrestore(&ptp->ptp_lock, flags);
+		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
 	} else {
 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
 	}
@@ -13565,12 +13563,13 @@ void bnxt_fw_reset(struct bnxt *bp)
 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 		int n = 0, tmo;
 
+		/* we have to serialize with bnxt_refclk_read()*/
 		if (ptp) {
 			unsigned long flags;
 
-			spin_lock_irqsave(&ptp->ptp_lock, flags);
+			write_seqlock_irqsave(&ptp->ptp_lock, flags);
 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
-			spin_unlock_irqrestore(&ptp->ptp_lock, flags);
+			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
 		} else {
 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
 		}
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +24 -49

221 lines not on this page · raw message

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h +13 -1
index 3ac5cbc1c5c4..4df4c2f373e0 100644
@@ -103,7 +103,7 @@ struct bnxt_ptp_cfg {
 	struct timecounter	tc;
 	struct bnxt_pps		pps_info;
 	/* serialize timecounter access */
-	spinlock_t		ptp_lock;
+	seqlock_t		ptp_lock;
 	/* serialize ts tx request queuing */
 	spinlock_t		ptp_tx_lock;
 	u64			current_time;
@@ -170,4 +170,16 @@ void bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg *ptp, u64 ns);
 int bnxt_ptp_init_rtc(struct bnxt *bp, bool phc_cfg);
 int bnxt_ptp_init(struct bnxt *bp, bool phc_cfg);
 void bnxt_ptp_clear(struct bnxt *bp);
+static inline u64 bnxt_timecounter_cyc2time(struct bnxt_ptp_cfg *ptp, u64 ts)
+{
+	unsigned int seq;
+	u64 ns;
+
+	do {
+		seq = read_seqbegin(&ptp->ptp_lock);
+		ns = timecounter_cyc2time(&ptp->tc, ts);
+	} while (read_seqretry(&ptp->ptp_lock, seq));
+
+	return ns;
+}
 #endif
Signature · 1 line
2.43.5
maintainer
#2 lore raw

Re: [PATCH net-next v3 2/2] bnxt_en: replace PTP spinlock with seqlock

Quoted 37 lines · Vadim Fedorenko

We can see high contention on ptp_lock while doing RX timestamping on high packet rates over several queues. Spinlock is not effecient to protect timecounter for RX timestamps when reads are the most usual operations and writes are only occasional. It's better to use seqlock in such cases.

---
v3:
- remove unused variable
v2:
- use read_excl lock to serialize reg access with FW reset

---
 drivers/net/ethernet/broadcom/bnxt/bnxt.c     | 19 +++--
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c | 73 ++++++-------------
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h | 14 +++-
 3 files changed, 46 insertions(+), 60 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +2 -3
index 820c7e83e586..5ab52f7a282d 100644
@@ -67,19 +67,21 @@ static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info,
        if (BNXT_PTP_USE_RTC(ptp->bp))
                return bnxt_ptp_cfg_settime(ptp->bp, ns);
 
-       spin_lock_irqsave(&ptp->ptp_lock, flags);
+       write_seqlock_irqsave(&ptp->ptp_lock, flags);
        timecounter_init(&ptp->tc, &ptp->cc, ns);
-       spin_unlock_irqrestore(&ptp->ptp_lock, flags);
+       write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
        return 0;
 }
 
-/* Caller holds ptp_lock */

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info, · line 85

Quoted 10 lines
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +3 -0
 static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
                            u64 *ns)
 {
        struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
        u32 high_before, high_now, low;
+       unsigned long flags;
 
+       /* We have to serialize reg access and FW reset */
+       read_seqlock_excl_irqsave(&ptp->ptp_lock, flags);
        if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))

I think we need read_sequnlock_excl_irqrestore() here before returning.

Quoted 10 lines
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +1 -0
                return -EIO;
 
@@ -93,6 +95,7 @@ static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
                low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
                ptp_read_system_postts(sts);
        }
+       read_sequnlock_excl_irqrestore(&ptp->ptp_lock, flags);
        *ns = ((u64)high_now << 32) | low;
 
        return 0;
#3 lore raw

Re: [PATCH net-next v3 2/2] bnxt_en: replace PTP spinlock with seqlock

Quoted 45 lines · Michael Chan
Quoted 44 lines · Vadim Fedorenko

We can see high contention on ptp_lock while doing RX timestamping on high packet rates over several queues. Spinlock is not effecient to protect timecounter for RX timestamps when reads are the most usual operations and writes are only occasional. It's better to use seqlock in such cases.

---
v3:
- remove unused variable
v2:
- use read_excl lock to serialize reg access with FW reset

---
  drivers/net/ethernet/broadcom/bnxt/bnxt.c     | 19 +++--
  drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c | 73 ++++++-------------
  drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h | 14 +++-
  3 files changed, 46 insertions(+), 60 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +3 -3
index 820c7e83e586..5ab52f7a282d 100644
@@ -67,19 +67,21 @@ static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info,
         if (BNXT_PTP_USE_RTC(ptp->bp))
                 return bnxt_ptp_cfg_settime(ptp->bp, ns);
 
-       spin_lock_irqsave(&ptp->ptp_lock, flags);
+       write_seqlock_irqsave(&ptp->ptp_lock, flags);
         timecounter_init(&ptp->tc, &ptp->cc, ns);
-       spin_unlock_irqrestore(&ptp->ptp_lock, flags);
+       write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
         return 0;
  }
 
-/* Caller holds ptp_lock */
  static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
                             u64 *ns)
  {
         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
         u32 high_before, high_now, low;
+       unsigned long flags;
 
Quoted 5 lines

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info, · line 85

Quoted 3 lines
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +2 -0
+       /* We have to serialize reg access and FW reset */
+       read_seqlock_excl_irqsave(&ptp->ptp_lock, flags);
         if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))

I think we need read_sequnlock_excl_irqrestore() here before returning.

Yes, you are right, we need to unlock seqlock in case of error. I'll post v4 soon, thanks!

Quoted 11 lines
Quoted 11 lines
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +1 -0
                 return -EIO;
 
@@ -93,6 +95,7 @@ static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
                 low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
                 ptp_read_system_postts(sts);
         }
+       read_sequnlock_excl_irqrestore(&ptp->ptp_lock, flags);
         *ns = ((u64)high_now << 32) | low;
 
         return 0;