From: wei.fang@oss.nxp.com
To: xiaoning.wang@nxp.com, andrew@lunn.ch, olteanv@gmail.com,
andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
richardcochran@gmail.com, linusw@kernel.org,
linux@armlinux.org.uk
Cc: wei.fang@nxp.com, imx@lists.linux.dev, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
linux-arm-kernel@lists.infradead.org
Subject: [PATCH v4 net-next 3/8] ptp: netc: export netc_timer_get_current_time() for cross-driver use
Date: Fri, 18 Sep 2026 15:28:47 +0800 [thread overview]
Message-ID: <20260918072852.501420-4-wei.fang@oss.nxp.com> (raw)
In-Reply-To: <20260918072852.501420-1-wei.fang@oss.nxp.com>
From: Wei Fang <wei.fang@nxp.com>
The NETC Switch does not have its own time registers and must obtain
the current PTP time from the NETC Timer bound to it. As they are
separate PCIe functions with independent drivers, add
netc_timer_get_current_time() to the Timer driver and export it so the
Switch driver can call it.
The Timer may be unbound while the Switch is calling this function,
which would free priv and unmap the MMIO region under an in-flight
read. To keep priv valid for the whole read, track each probed Timer
in a module-private list keyed by its pci_dev, and protect the lookup
and the register read with a global spinlock. A list hit proves the
pci_dev is bound to this driver, so priv is valid. remove() deletes
the entry under the same lock before tearing the device down, so no
read can observe a stale priv.
Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
drivers/ptp/ptp_netc.c | 63 ++++++++++++++++++++++++++++++++-
include/linux/fsl/netc_global.h | 10 ++++++
2 files changed, 72 insertions(+), 1 deletion(-)
diff --git a/drivers/ptp/ptp_netc.c b/drivers/ptp/ptp_netc.c
index aa9be8e2a630..1211aa9a37d3 100644
--- a/drivers/ptp/ptp_netc.c
+++ b/drivers/ptp/ptp_netc.c
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
/*
* NXP NETC V4 Timer driver
- * Copyright 2025 NXP
+ * Copyright 2025-2026 NXP
*/
#include <linux/bitfield.h>
@@ -123,8 +123,12 @@ struct netc_timer {
u8 fs_alarm_num;
u8 fs_alarm_bitmap;
struct netc_pp pp[NETC_TMR_FIPER_NUM]; /* periodic pulse */
+ struct list_head node;
};
+static LIST_HEAD(netc_timer_list);
+static DEFINE_SPINLOCK(netc_timer_list_lock);
+
#define netc_timer_rd(p, o) netc_read((p)->base + (o))
#define netc_timer_wr(p, o, v) netc_write((p)->base + (o), v)
@@ -985,6 +989,10 @@ static int netc_timer_probe(struct pci_dev *pdev,
enable_irq(priv->irq);
+ spin_lock_bh(&netc_timer_list_lock);
+ list_add(&priv->node, &netc_timer_list);
+ spin_unlock_bh(&netc_timer_list_lock);
+
return 0;
free_msix_irq:
@@ -999,6 +1007,10 @@ static void netc_timer_remove(struct pci_dev *pdev)
{
struct netc_timer *priv = pci_get_drvdata(pdev);
+ spin_lock_bh(&netc_timer_list_lock);
+ list_del(&priv->node);
+ spin_unlock_bh(&netc_timer_list_lock);
+
disable_irq(priv->irq);
ptp_clock_unregister(priv->clock);
netc_timer_wr(priv, NETC_TMR_TEMASK, 0);
@@ -1021,5 +1033,54 @@ static struct pci_driver netc_timer_driver = {
};
module_pci_driver(netc_timer_driver);
+/**
+ * netc_timer_get_current_time - read the current PTP time from the NETC Timer
+ * @pdev: PCI device of the NETC Timer
+ * @ns: Output, the current PTP clock time in nanoseconds
+ *
+ * Read TMR_CUR_TIME from the NETC Timer bound to @pdev. The lookup and read
+ * run under netc_timer_list_lock, so the Timer cannot be unbound and its priv
+ * freed during the read.
+ *
+ * Context: Process or softirq context. Must not be called from hardirq.
+ *
+ * Return: 0 on success, -ENODEV if the Timer is not present (not yet probed
+ * or already removed).
+ */
+int netc_timer_get_current_time(struct pci_dev *pdev, u64 *ns)
+{
+ struct netc_timer *priv = NULL;
+ struct netc_timer *tmp;
+ unsigned long flags;
+ int err = 0;
+
+ /* Serialize against driver unbind, so holding it here ensures that
+ * priv remains valid for the entire duration of the register read.
+ */
+ spin_lock_bh(&netc_timer_list_lock);
+
+ list_for_each_entry(tmp, &netc_timer_list, node) {
+ if (tmp->pdev == pdev) {
+ priv = tmp;
+ break;
+ }
+ }
+
+ if (!priv) {
+ err = -ENODEV;
+ goto netc_timer_list_unlock;
+ }
+
+ spin_lock_irqsave(&priv->lock, flags);
+ *ns = netc_timer_cur_time_read(priv);
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+netc_timer_list_unlock:
+ spin_unlock_bh(&netc_timer_list_lock);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(netc_timer_get_current_time);
+
MODULE_DESCRIPTION("NXP NETC Timer PTP Driver");
MODULE_LICENSE("Dual BSD/GPL");
diff --git a/include/linux/fsl/netc_global.h b/include/linux/fsl/netc_global.h
index 5b8ff528d369..d4a26c17f99a 100644
--- a/include/linux/fsl/netc_global.h
+++ b/include/linux/fsl/netc_global.h
@@ -6,6 +6,7 @@
#include <linux/io.h>
#include <linux/io-64-nonatomic-lo-hi.h>
+#include <linux/pci.h>
static inline u32 netc_read(void __iomem *reg)
{
@@ -22,4 +23,13 @@ static inline u64 netc_read64(void __iomem *reg)
return ioread64(reg);
}
+#if IS_REACHABLE(CONFIG_PTP_NETC_V4_TIMER)
+int netc_timer_get_current_time(struct pci_dev *pdev, u64 *ns);
+#else
+static inline int netc_timer_get_current_time(struct pci_dev *pdev, u64 *ns)
+{
+ return -ENODEV;
+}
+#endif
+
#endif
--
2.34.1
next prev parent reply other threads:[~2026-09-18 7:25 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 7:28 [PATCH v4 net-next 0/8] net: dsa: netc: add PTP support for NETC switch wei.fang
2026-09-18 7:28 ` [PATCH v4 net-next 1/8] ptp: netc: use ioread64_lo_hi/iowrite64_lo_hi for 64-bit register access wei.fang
2026-09-18 7:28 ` [PATCH v4 net-next 2/8] ptp: netc: remove unnecessary pcie_flr() call in probe wei.fang
2026-09-18 7:28 ` wei.fang [this message]
2026-09-18 7:28 ` [PATCH v4 net-next 4/8] net: dsa: netc: use entry ID instead of pointer to track host flood rule wei.fang
2026-09-18 7:28 ` [PATCH v4 net-next 5/8] net: dsa: netc: check return value of ntmp_ipft_delete_entry() wei.fang
2026-09-18 7:28 ` [PATCH v4 net-next 6/8] net: dsa: netc: enable ingress port filtering lookup by default wei.fang
2026-09-18 7:28 ` [PATCH v4 net-next 7/8] net: dsa: netc: add PTP two-step timestamping support wei.fang
2026-09-18 7:28 ` [PATCH v4 net-next 8/8] net: dsa: netc: add PTP one-step " wei.fang
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=20260918072852.501420-4-wei.fang@oss.nxp.com \
--to=wei.fang@oss.nxp.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=imx@lists.linux.dev \
--cc=kuba@kernel.org \
--cc=linusw@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=netdev@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
--cc=richardcochran@gmail.com \
--cc=wei.fang@nxp.com \
--cc=xiaoning.wang@nxp.com \
/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®