net: stmmac: dwxgmac2: timestamp interrupt support + Agilex5 fix
Subsystems (6)
v6 cover net: stmmac: dwxgmac2: timestamp interrupt support + Agilex5 fix
This series adds auxiliary snapshot (EXTTS) interrupt support to DWXGMAC2/DWXLGMAC2, fixes a stale TSIS race on the Agilex5 cross-timestamp path that the new handler would otherwise expose, and guards the shared aux snapshot handler against a zero channel mask.
Patch 1 makes smtg_crosststamp() complete on the persistent ATSNS count instead of the transient TSIS bit, waits for the ATSFC FIFO clear to complete, and holds aux_ts_lock across the whole trigger/poll/drain sequence so a concurrent PTP_CLK_REQ_EXTTS request cannot flush the snapshot FIFO mid-flight.
Patch 2 guards the shared aux snapshot handler against a zero PTP_ACR channel mask: ilog2() is applied to the mask without checking for zero, and ilog2(0) yields an out-of-range event index that ptp_clock_event() feeds to test_bit() unchecked from hard IRQ context.
Patch 3 wires up a dedicated DWXGMAC2 timestamp interrupt handler, following the guarded pattern of the shared one. Before this change the XGMAC hwif entries used the generic stmmac_ptp ops, whose timestamp_interrupt callback read the dwmac4 offset GMAC_TIMESTAMP_STATUS (0xb20) instead of the XGMAC register at 0xd20. The PTP clock advertised the aux snapshot channels, so PTP_EXTTS_REQUEST succeeded but no event was ever delivered.
Following 30300d9f9150 ("net: stmmac: xgmac: Disable the Timestamp interrupt by default"), XGMAC_TSIE is not added back to XGMAC_INT_DEFAULT_EN. Instead it is armed on demand from the PTP_CLK_REQ_EXTTS enable/disable path via a new optional timestamp_interrupt_cfg mac callback (mirroring dwmac1000). The interrupt is only touched after the ATSFC FIFO clear has completed, and the handler refuses to drain entries while that clear is still in flight; it also leaves the snapshot FIFO alone while an internal cross-timestamp owns it (STMMAC_FLAG_INT_SNAPSHOT_EN), is disarmed when the PTP clock is unregistered, and is re-armed on resume by stmmac_rearm_timestamp_irq() when a channel was left enabled across suspend.
v1: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v4: https://lore.kernel.org/netdev/[email protected]/
v5: https://lore.kernel.org/netdev/[email protected]/
v6:
- Hold aux_ts_lock across the whole crosststamp sequence instead of
dropping it after arming, so a concurrent EXTTS request cannot flush
the FIFO between the poll and the drain loop.
- Arm XGMAC_TSIE on demand from the PTP_CLK_REQ_EXTTS path instead of
adding it to XGMAC_INT_DEFAULT_EN, addressing the review comments on
reverting 30300d9f9150 and on the missing disable counterpart.
- Reword the ATSNS comments: ATSNS is the aux snapshot FIFO level; it
is not cleared by reading XGMAC_TIMESTAMP_STATUS, which is the
property the poll relies on.
- Add Fixes: to the EXTTS support patch and guard the shared handler
against a zero PTP_ACR channel mask, placed before the new handler
so every intermediate tree leaves the shared path hardened.
Zxyan Zhu (3):
net: stmmac: dwmac-socfpga: complete cross-timestamp on ATSNS
net: stmmac: guard against a zero channel in the aux snapshot handler
net: stmmac: dwxgmac2: add XGMAC timestamp interrupt support
.../ethernet/stmicro/stmmac/dwmac-socfpga.c | 36 ++++++++++--- .../ethernet/stmicro/stmmac/dwxgmac2_core.c | 54 +++++++++++++++++++ drivers/net/ethernet/stmicro/stmmac/hwif.c | 4 +- drivers/net/ethernet/stmicro/stmmac/hwif.h | 5 ++ .../ethernet/stmicro/stmmac/stmmac_hwtstamp.c | 17 +++++- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 14 +++++ .../net/ethernet/stmicro/stmmac/stmmac_ptp.c | 9 ++++ .../net/ethernet/stmicro/stmmac/stmmac_ptp.h | 1 + 8 files changed, 130 insertions(+), 10 deletions(-)
base-commit: d0ec95a8a4e79f2fd6063fc8932415db8c227689
Signature · 1 line
2.34.1
PATCH v6 1/3 net: stmmac: dwmac-socfpga: complete cross-timestamp on ATSNS
The Agilex5 smtg_crosststamp() handler arms an internal auxiliary snapshot, toggles GPO0 and then polls XGMAC_INT_STATUS for TSIS to learn that the snapshot is ready. TSIS is a transient, read-to-clear status bit: it is set by any MAC timestamp event and cleared the moment XGMAC_TIMESTAMP_STATUS is read.
That makes the TSIS poll racy in two ways. A stale TSIS latched by an unrelated event satisfies the poll immediately, before the auxiliary snapshot is latched, so the FIFO comes back empty and *device is never written even though the call returns 0. Conversely a concurrent reader of XGMAC_TIMESTAMP_STATUS, such as the TX timestamp completion path, can clear TSIS while the poll is waiting and make it time out with "Wait for time sync operation timeout".
The auxiliary snapshot FIFO level is also reported by the ATSNS count in XGMAC_TIMESTAMP_STATUS. Reading XGMAC_TIMESTAMP_STATUS does not affect ATSNS, so the destructive reads above cannot disturb it. Poll ATSNS instead of TSIS, and wait for the PTP_ACR_ATSFC FIFO clear to complete first so a stale ATSNS from a previous snapshot cannot satisfy the poll before the new snapshot is latched.
Hold aux_ts_lock across the whole sequence instead of dropping it right after arming, so a concurrent PTP_CLK_REQ_EXTTS request cannot set PTP_ACR_ATSFC and flush the FIFO between the poll and the drain loop, which would leave *device filled from an empty FIFO.
--- .../ethernet/stmicro/stmmac/dwmac-socfpga.c | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-socfpga.c +24 -7
@@ -337,8 +337,18 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system, acr_value =readl (ptpaddr +PTP_ACR ); acr_value |=PTP_ACR_ATSFC ;writel (acr_value, ptpaddr +PTP_ACR ); -/* Release the mutex */ -mutex_unlock (&priv->aux_ts_lock); + +/* Wait for the FIFO clear to complete, so the poll below only +* observes snapshots latched by this trigger. +*/ + ret =readl_poll_timeout (ptpaddr +PTP_ACR , acr_value, + !(acr_value &PTP_ACR_ATSFC ),10 ,10000 ); +if (ret) { +mutex_unlock (&priv->aux_ts_lock); +netdev_err (priv->dev,"%s: Failed to clear snapshot FIFO\n" , + __func__); +return ret; + }/* Trigger Internal snapshot signal. Create a rising edge by just toggle * the GPO0 to low and back to high.
@@ -349,10 +359,16 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system, gpio_value |=XGMAC_GPIO_GPO0 ;writel (gpio_value, ioaddr +XGMAC_GPIO_STATUS ); -/* Poll for time sync operation done */ - ret =readl_poll_timeout (priv->ioaddr +XGMAC_INT_STATUS , v, - (v &XGMAC_INT_TSIS ),100 ,10000 ); +/* Wait for the auxiliary snapshot to be latched: the ATSNS count +* is the FIFO level and is not affected by reading +* XGMAC_TIMESTAMP_STATUS, so concurrent readers cannot disturb +* the poll. +*/ + ret =readl_poll_timeout (ioaddr +XGMAC_TIMESTAMP_STATUS , v, +FIELD_GET (XGMAC_TIMESTAMP_ATSNS_MASK , v), +100 ,10000 );if (ret) { +mutex_unlock (&priv->aux_ts_lock);netdev_err (priv->dev,"%s: Wait for time sync operation timeout\n" , __func__);return ret;
@@ -364,8 +380,7 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system, .use_nsecs = false, }; - num_snapshot =FIELD_GET (XGMAC_TIMESTAMP_ATSNS_MASK , -readl (ioaddr +XGMAC_TIMESTAMP_STATUS )); + num_snapshot =FIELD_GET (XGMAC_TIMESTAMP_ATSNS_MASK , v);/* Repeat until the timestamps are from the FIFO last segment */ for (i =0 ; i < num_snapshot; i++) {
@@ -375,6 +390,8 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system,read_unlock_irqrestore (&priv->ptp_lock, flags); } +mutex_unlock (&priv->aux_ts_lock); +get_smtgtime (priv->mii,SMTG_MDIO_ADDR , &smtg_time); system->cycles = smtg_time;
Signature · 1 line
2.34.1
PATCH v6 2/3 net: stmmac: guard against a zero channel in the aux snapshot handler
In reply to Zxyan ZhuThe generic timestamp_interrupt() handler derives the EXTTS channel index with ilog2() applied directly to the PTP_ACR channel mask, with no check for a zero mask. ilog2(0) yields -1, which ends up in event.index as 0xffffffff, and ptp_clock_event() uses that index in test_bit() against a PTP_MAX_CHANNELS bitmap without range validation, reading far past the allocation from hard IRQ context.
The zero-mask window is reachable: stmmac_enable() sets STMMAC_FLAG_EXT_SNAPSHOT_EN before it programs PTP_ACR, so an EXTTS interrupt arriving in between passes the flag check while the mask is still clear (snapshots can also linger in the FIFO from a previous enable, as only PTP_ACR_ATSFC clears them).
Check the mask before applying the ilog2().
--- drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c +4 -1
@@ -242,7 +242,10 @@ static void timestamp_interrupt(struct stmmac_priv *priv)GMAC_TIMESTAMP_ATSNS_SHIFT ; acr_value =readl (priv->ptpaddr +PTP_ACR ); - channel =ilog2 (FIELD_GET (PTP_ACR_MASK , acr_value)); + channel =FIELD_GET (PTP_ACR_MASK , acr_value); +if (!channel) +return ; + channel =ilog2 (channel);for (i =0 ; i < num_snapshot; i++) {read_lock_irqsave (&priv->ptp_lock, flags);
Signature · 1 line
2.34.1
PATCH v6 3/3 net: stmmac: dwxgmac2: add XGMAC timestamp interrupt support
In reply to Zxyan ZhuThe DWXGMAC2 hwif entries use the generic stmmac_ptp hwtimestamp ops, whose timestamp_interrupt callback reads the dwmac4 offset GMAC_TIMESTAMP_STATUS (0xb20) instead of the XGMAC register at 0xd20, and whose interrupt enable mask XGMAC_INT_DEFAULT_EN omits XGMAC_TSIE. The PTP clock nevertheless advertises the auxiliary snapshot channels on XGMAC, so PTP_EXTTS_REQUEST succeeds but no PTP_CLOCK_EXTTS event is ever delivered.
Fix this with a dedicated DWXGMAC2 timestamp interrupt handler that reads XGMAC_TIMESTAMP_STATUS and reports the pending auxiliary snapshots as PTP_CLOCK_EXTTS events. The handler deliberately does not gate its status read on XGMAC_INT_TSIS: TSIS is an aggregate, read-to-clear bit that the TX timestamp completion path (which polls the same register for TXTSC) clears before the handler can observe it, while ATSNS and the snapshot FIFO still hold the aux events. A dwmac4-style TSIS gate would silently drop them.
XGMAC_TSIE is not added back to XGMAC_INT_DEFAULT_EN, which 30300d9f9150 ("net: stmmac: xgmac: Disable the Timestamp interrupt by default") deliberately keeps clear. Instead it is armed on demand from the PTP_CLK_REQ_EXTTS enable/disable path of stmmac_enable(), through a new optional stmmac_ops->timestamp_interrupt_cfg() callback implemented only for DWXGMAC2/DWXLGMAC2 (on top of dwxgmac2_irq_modify()), like dwmac1000 does with dwmac1000_timestamp_interrupt_cfg(). This keeps platforms that do not use EXTTS at their current interrupt load and leaves the other cores untouched: stmmac_enable() is shared with dwmac4/dwmac5, whose timestamp interrupt stays always-enabled and is relied upon by intel_crosststamp(), so a direct irq_modify() call there is not an option; cores that do not implement the callback keep their current behaviour.
The interrupt is only touched after the PTP_ACR_ATSFC FIFO clear has completed, and the handler refuses to drain entries while that clear is still in flight, so a stale snapshot is never reported as an event; if the clear times out, the error is returned without changing the interrupt state. The handler also leaves the snapshot FIFO alone while an internal cross-timestamp owns it: smtg_crosststamp() sets STMMAC_FLAG_INT_SNAPSHOT_EN for the duration of the cross-timestamp, raised and dropped under aux_ts_lock so concurrent requests cannot lose it, and the handler returns early when that flag is set, mirroring the dwmac4 handler's treatment of intel_crosststamp().
The timestamp interrupt is also disarmed after ptp_clock_unregister(), which drops STMMAC_FLAG_EXT_SNAPSHOT_EN as well. stmmac_hw_setup() reprogramsXGMAC_INT_EN from XGMAC_INT_DEFAULT_EN on resume, which drops XGMAC_TSIE, sostmmac_rearm_timestamp_irq() re-arms it there whenever an auxiliary snapshot channel was left enabled.
--- .../ethernet/stmicro/stmmac/dwmac-socfpga.c | 5 ++ .../ethernet/stmicro/stmmac/dwxgmac2_core.c | 54 +++++++++++++++++++ drivers/net/ethernet/stmicro/stmmac/hwif.c | 4 +- drivers/net/ethernet/stmicro/stmmac/hwif.h | 5 ++ .../ethernet/stmicro/stmmac/stmmac_hwtstamp.c | 12 +++++ .../net/ethernet/stmicro/stmmac/stmmac_main.c | 14 +++++ .../net/ethernet/stmicro/stmmac/stmmac_ptp.c | 9 ++++ .../net/ethernet/stmicro/stmmac/stmmac_ptp.h | 1 + 8 files changed, 102 insertions(+), 2 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-socfpga.c +5 -0
@@ -311,6 +311,7 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system,return -EBUSY ;mutex_lock (&priv->aux_ts_lock); + priv->plat->flags |=STMMAC_FLAG_INT_SNAPSHOT_EN ;/* Enable Internal snapshot trigger */ acr_value =readl (ptpaddr +PTP_ACR ); acr_value &= ~PTP_ACR_MASK ;
@@ -328,6 +329,7 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system, acr_value |=PTP_ACR_ATSEN3 ;break ;default : + priv->plat->flags &= ~STMMAC_FLAG_INT_SNAPSHOT_EN ;mutex_unlock (&priv->aux_ts_lock);return -EINVAL ; }
@@ -344,6 +346,7 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system, ret =readl_poll_timeout (ptpaddr +PTP_ACR , acr_value, !(acr_value &PTP_ACR_ATSFC ),10 ,10000 );if (ret) { + priv->plat->flags &= ~STMMAC_FLAG_INT_SNAPSHOT_EN ;mutex_unlock (&priv->aux_ts_lock);netdev_err (priv->dev,"%s: Failed to clear snapshot FIFO\n" , __func__);
@@ -368,6 +371,7 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system,FIELD_GET (XGMAC_TIMESTAMP_ATSNS_MASK , v), 100, 10000);if (ret) { + priv->plat->flags &= ~STMMAC_FLAG_INT_SNAPSHOT_EN ;mutex_unlock (&priv->aux_ts_lock);netdev_err (priv->dev,"%s: Wait for time sync operation timeout\n" , __func__);
@@ -390,6 +394,7 @@ static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system,read_unlock_irqrestore (&priv->ptp_lock, flags); } + priv->plat->flags &= ~STMMAC_FLAG_INT_SNAPSHOT_EN ;mutex_unlock (&priv->aux_ts_lock);get_smtgtime (priv->mii,SMTG_MDIO_ADDR , &smtg_time);
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c +54 -0
78 lines not on this page · raw message
drivers/net/ethernet/stmicro/stmmac/hwif.c +2 -2
@@ -258,7 +258,7 @@ static const struct stmmac_hwif_entry { .dma = &dwxgmac210_dma_ops, .mac = &dwxgmac210_ops, .vlan = &dwxgmac210_vlan_ops, - .hwtimestamp = &stmmac_ptp, + .hwtimestamp = &dwxgmac2_ptp, .ptp = &stmmac_ptp_clock_ops, .mode = NULL, .tc = &dwmac510_tc_ops,
@@ -280,7 +280,7 @@ static const struct stmmac_hwif_entry { .dma = &dwxgmac210_dma_ops, .mac = &dwxlgmac2_ops, .vlan = &dwxlgmac2_vlan_ops, - .hwtimestamp = &stmmac_ptp, + .hwtimestamp = &dwxgmac2_ptp, .ptp = &stmmac_ptp_clock_ops, .mode = NULL, .tc = &dwmac510_tc_ops,
drivers/net/ethernet/stmicro/stmmac/hwif.h +5 -0
@@ -317,6 +317,8 @@ struct stmmac_ops {void (*update_caps)(struct stmmac_priv *priv);/* Change the interrupt enable setting. Enable takes precedence. */ void (*irq_modify)(struct mac_device_info *hw,u32 disable,u32 enable); +/* Arm or disarm the timestamp interrupt on demand (optional) */ +void (*timestamp_interrupt_cfg)(struct stmmac_priv *priv,bool en);/* Enable the MAC RX/TX */ void (*set_mac)(void __iomem *ioaddr,bool enable);/* Enable and verify that the IPC module is supported */
@@ -420,6 +422,8 @@ struct stmmac_ops { stmmac_do_void_callback(__priv, mac, update_caps, __priv) #define stmmac_mac_irq_modify(__priv, __args...) \ stmmac_do_void_callback(__priv, mac, irq_modify, (__priv)->hw, __args) +#define stmmac_mac_timestamp_interrupt_cfg(__priv, __args...) \ + stmmac_do_void_callback(__priv, mac, timestamp_interrupt_cfg, __priv, __args) #define stmmac_mac_set(__priv, __args...) \ stmmac_do_void_callback(__priv, mac, set_mac, __args) #define stmmac_rx_ipc(__priv, __args...) \
@@ -672,6 +676,7 @@ extern const struct stmmac_desc_ops ndesc_ops;extern const struct stmmac_hwtimestamp stmmac_ptp;extern const struct stmmac_hwtimestamp dwmac1000_ptp; +extern const struct stmmac_hwtimestamp dwxgmac2_ptp;extern const struct stmmac_mode_ops ring_mode_ops;extern const struct stmmac_mode_ops chain_mode_ops;
drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c +12 -0
@@ -280,3 +280,15 @@ const struct stmmac_hwtimestamp dwmac1000_ptp = { .get_ptptime = dwmac1000_get_ptptime, .timestamp_interrupt = dwmac1000_timestamp_interrupt, }; + +const struct stmmac_hwtimestamp dwxgmac2_ptp = { + .config_hw_tstamping = config_hw_tstamping, + .init_systime = init_systime, + .config_sub_second_increment = config_sub_second_increment, + .config_addend = config_addend, + .adjust_systime = adjust_systime, + .get_systime = get_systime, + .get_ptptime = get_ptptime, + .timestamp_interrupt = dwxgmac2_timestamp_interrupt, + .hwtstamp_correct_latency = hwtstamp_correct_latency, +};
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +14 -0
@@ -885,6 +885,19 @@ static int stmmac_init_tstamp_counter(struct stmmac_priv *priv,return 0 ; } +/** +* stmmac_rearm_timestamp_irq - re-arm the timestamp interrupt +* @priv: driver private structure +* Description: this re-arms the on-demand timestamp interrupt if an +* auxiliary snapshot channel was left enabled. +*/ +static void stmmac_rearm_timestamp_irq (struct stmmac_priv *priv) +{ +if (priv->plat->flags &STMMAC_FLAG_EXT_SNAPSHOT_EN && + priv->ptp_clock) +stmmac_mac_timestamp_interrupt_cfg (priv, true); +} +/** * stmmac_init_timestamping - initialise timestamping * @priv: driver private structure
@@ -8314,6 +8327,7 @@ int stmmac_resume(struct device *dev) }stmmac_init_timestamping (priv); +stmmac_rearm_timestamp_irq (priv);stmmac_init_coalesce (priv);phylink_rx_clk_stop_block (priv->phylink);
drivers/net/ethernet/stmicro/stmmac/stmmac_ptp.c +9 -0
@@ -255,6 +255,12 @@ static int stmmac_enable(struct ptp_clock_info *ptp, ret =readl_poll_timeout (ptpaddr +PTP_ACR , acr_value, !(acr_value &PTP_ACR_ATSFC ),10 ,10000 ); +/* Arm or disarm the timestamp interrupt only once the FIFO +* clear has completed, so the handler does not observe a +* snapshot that the clear is about to discard. +*/ +if (!ret) +stmmac_mac_timestamp_interrupt_cfg (priv, on);break ; }
@@ -395,6 +401,9 @@ void stmmac_ptp_unregister(struct stmmac_priv *priv)pr_debug ("Removed PTP HW clock successfully on %s\n" , priv->dev->name); +stmmac_mac_timestamp_interrupt_cfg (priv, false); + priv->plat->flags &= ~STMMAC_FLAG_EXT_SNAPSHOT_EN ; +mutex_destroy (&priv->aux_ts_lock); } }
drivers/net/ethernet/stmicro/stmmac/stmmac_ptp.h +1 -0
@@ -103,6 +103,7 @@ int dwmac1000_ptp_enable(struct ptp_clock_info *ptp,void dwmac1000_get_ptptime (void __iomem *ptpaddr,u64 *ptp_time);void dwmac1000_timestamp_interrupt (struct stmmac_priv *priv); +void dwxgmac2_timestamp_interrupt (struct stmmac_priv *priv);extern const struct ptp_clock_info stmmac_ptp_clock_ops;extern const struct ptp_clock_info dwmac1000_ptp_clock_ops;
Signature · 1 line
2.34.1
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.