From: Wei Fang <[email protected]> The NETC Switch does not have its own time registers and must obtain the current PTP time from the NETC Timer bound to it. Since the two are separate PCIe functions with independent drivers, add netc_timer_get_current_time() to the Timer driver and export it via EXPORT_SYMBOL_GPL().
The function takes the Timer's pci_dev pointer, acquires the per-device spinlock to protect against concurrent register access, and reads TMR_CUR_TIME via netc_timer_cur_time_read(). It returns an error if the Timer driver has not yet probed or has already been removed, allowing the caller to handle the unavailable case gracefully. Hold the device lock around pci_get_drvdata() and the register read to serialize against concurrent driver unbind. The remove() callback runs under the same device lock, so this guarantees that priv and the MMIO mapping remain valid for the entire duration of the read. Move spin_lock_init() from netc_timer_probe() to netc_timer_pci_probe(), before pci_set_drvdata(), so that the spinlock is fully initialized before the driver data becomes visible to other drivers. Signed-off-by: Wei Fang <[email protected]> --- drivers/ptp/ptp_netc.c | 52 ++++++++++++++++++++++++++++++++- include/linux/fsl/netc_global.h | 10 +++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/drivers/ptp/ptp_netc.c b/drivers/ptp/ptp_netc.c index aa9be8e2a630..854a206b6c5b 100644 --- a/drivers/ptp/ptp_netc.c +++ b/drivers/ptp/ptp_netc.c @@ -798,6 +798,7 @@ static int netc_timer_pci_probe(struct pci_dev *pdev) goto release_mem_regions; } + spin_lock_init(&priv->lock); pci_set_drvdata(pdev, priv); return 0; @@ -968,7 +969,6 @@ static int netc_timer_probe(struct pci_dev *pdev, priv->caps = netc_timer_ptp_caps; priv->oclk_prsc = NETC_TMR_DEFAULT_PRSC; priv->pps_channel = NETC_TMR_INVALID_CHANNEL; - spin_lock_init(&priv->lock); snprintf(priv->irq_name, sizeof(priv->irq_name), "ptp-netc %s", pci_name(pdev)); @@ -1021,5 +1021,55 @@ 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: The current PTP clock time in nanoseconds, returned to the caller + * + * Reads the 64-bit current time register (TMR_CUR_TIME) from the NETC Timer + * device associated with @pdev. Returns an error if the Timer driver has not + * yet probed or has already been removed. + * + * Context: Process context only. Acquires the device mutex via device_lock(), + * which may sleep. Must not be called from atomic context, softirq, + * BH, or while holding a spinlock. + * + * Return: 0 on success, otherwise a negative error code. + */ +int netc_timer_get_current_time(struct pci_dev *pdev, u64 *ns) +{ + struct device *dev = &pdev->dev; + struct netc_timer *priv; + unsigned long flags; + int err = 0; + + /* Serialize against driver unbind: the remove() callback runs under + * the device lock, so holding it here ensures that priv remains valid + * for the entire duration of the register read. + */ + device_lock(dev); + + if (pci_dev_driver(pdev) != &netc_timer_driver) { + err = -EINVAL; + goto unlock_device; + } + + priv = pci_get_drvdata(pdev); + if (!priv) { + err = -ENOMEM; + goto unlock_device; + } + + spin_lock_irqsave(&priv->lock, flags); + *ns = netc_timer_cur_time_read(priv); + spin_unlock_irqrestore(&priv->lock, flags); + +unlock_device: + device_unlock(dev); + + 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
