ptp: Support hardware clocks with additional free running cycle counter
Subsystems (5)
v4 cover ptp: Support hardware clocks with additional free running cycle counter
ptp vclocks require a clock with free running time for the timecounter. Currently only a physical clock forced to free running is supported. If vclocks are used, then the physical clock cannot be synchronized anymore. The synchronized time is not available in hardware in this case. As a result, timed transmission with TAPRIO hardware support is not possible anymore.
If hardware would support a free running time additionally to the physical clock, then the physical clock does not need to be forced to free running. Thus, the physical clocks can still be synchronized while vclocks are in use.
The physical clock could be used to synchronize the time domain of the TSN network and trigger TAPRIO. In parallel vclocks can be used to synchronize other time domains.
One year ago I thought for two time domains within a TSN network also two physical clocks are required. This would lead to new kernel interfaces for asking for the second clock, ... . But actually for a time triggered system like TSN there can be only one time domain that controls the system itself. All other time domains belong to other layers, but not to the time triggered system itself. So other time domains can be based on a free running counter if similar mechanisms like 2 step synchroisation are used.
Synchronisation was tested with two time domains between two directly connected hosts. Each host run two ptp4l instances, the first used the physical clock and the second used the virtual clock. I used my FPGA based network controller as network device. ptp4l was used in combination with the virtual clock support patches from Miroslav Lichvar.
v4:
- if_index of 0 is invalid (Jonathan Lemon)
- set if_index to 0 in the SOF_TIMESTAMPING_RAW_HARDWARE block (Jonathan Lemon)
- add helper function for netdev_get_tstamp() call (Jonathan Lemon)
- update SKBTX_ANY_TSTAMP (Paolo Abeni)
- use separate bits for new tx_flags (Richard Cochran)
v3:
- optimize ptp_convert_timestamp (Richard Cochran)
- call dev_get_by_napi_id() only if needed (Richard Cochran)
- use non-negated logical test (Richard Cochran)
- add comment for skipped output (Richard Cochran)
- add comment for SKBTX_HW_TSTAMP_USE_CYCLES masking (Richard Cochran)
v2:
- rename ptp_clock cycles to has_cycles (Richard Cochran)
- call it free running cycle counter (Richard Cochran)
- update struct skb_shared_hwtstamps kdoc (Richard Cochran)
- optimize timestamp address/cookie processing path (Richard Cochran, Vinicius Costa Gomes)
v1:
- complete rework based on suggestions (Richard Cochran)
Gerhard Engleder (6):
ptp: Add cycles support for virtual clocks
ptp: Request cycles for TX timestamp
ptp: Pass hwtstamp to ptp_convert_timestamp()
ptp: Support late timestamp determination
ptp: Speed up vclock lookup
tsnep: Add free running cycle counter support
drivers/net/ethernet/engleder/tsnep_hw.h | 9 ++- drivers/net/ethernet/engleder/tsnep_main.c | 33 +++++++-- drivers/net/ethernet/engleder/tsnep_ptp.c | 28 ++++++++ drivers/ptp/ptp_clock.c | 31 ++++++-- drivers/ptp/ptp_private.h | 11 +++ drivers/ptp/ptp_sysfs.c | 11 +-- drivers/ptp/ptp_vclock.c | 82 ++++++++++++++-------- include/linux/netdevice.h | 21 ++++++ include/linux/ptp_clock_kernel.h | 38 ++++++++-- include/linux/skbuff.h | 21 ++++-- net/socket.c | 60 +++++++++++++--- 11 files changed, 282 insertions(+), 63 deletions(-)
Signature · 1 line
2.20.1
PATCH v4 1/6 ptp: Add cycles support for virtual clocks
ptp vclocks require a free running time for their timecounter. Currently only a physical clock forced to free running is supported. If vclocks are used, then the physical clock cannot be synchronized anymore. The synchronized time is not available in hardware in this case. As a result, timed transmission with TAPRIO hardware support is not possible anymore.
If hardware would support a free running time additionally to the physical clock, then the physical clock does not need to be forced to free running. Thus, the physical clocks can still be synchronized while vclocks are in use.
The physical clock could be used to synchronize the time domain of the TSN network and trigger TAPRIO. In parallel vclocks can be used to synchronize other time domains.
Introduce support for a free running cycle counter called cycles to physical clocks. Rework ptp vclocks to use this free running cycle counter. Default implementation is based on time of physical clock. Thus, behavior of ptp vclocks based on physical clocks without free running cycle counter is identical to previous behavior.
--- drivers/ptp/ptp_clock.c | 31 +++++++++++++++++++++++++++---- drivers/ptp/ptp_private.h | 10 ++++++++++ drivers/ptp/ptp_sysfs.c | 11 +++++++---- drivers/ptp/ptp_vclock.c | 13 +++++-------- include/linux/ptp_clock_kernel.h | 31 +++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 16 deletions(-)
drivers/ptp/ptp_clock.c +27 -4
@@ -77,8 +77,8 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp { struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); -if (ptp_vclock_in_use (ptp)) { -pr_err ("ptp: virtual clock in use\n" ); +if (ptp_clock_freerun (ptp)) { +pr_err ("ptp: physical clock is free running\n" );return -EBUSY ; }
@@ -103,8 +103,8 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)struct ptp_clock_info *ops;int err = -EOPNOTSUPP ; -if (ptp_vclock_in_use (ptp)) { -pr_err ("ptp: virtual clock in use\n" ); +if (ptp_clock_freerun (ptp)) { +pr_err ("ptp: physical clock is free running\n" );return -EBUSY ; }
@@ -178,6 +178,14 @@ static void ptp_clock_release(struct device *dev)kfree (ptp); } +static int ptp_getcycles64 (struct ptp_clock_info *info,struct timespec64 *ts) +{ +if (info->getcyclesx64) +return info->getcyclesx64 (info, ts,NULL ); +else +return info->gettime64 (info, ts); +} +static void ptp_aux_kworker (struct kthread_work *work) { struct ptp_clock *ptp = container_of(work, struct ptp_clock,
@@ -225,6 +233,21 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,mutex_init (&ptp->n_vclocks_mux);init_waitqueue_head (&ptp->tsev_wq); +if (ptp->info->getcycles64 || ptp->info->getcyclesx64) { + ptp->has_cycles = true; +if (!ptp->info->getcycles64 && ptp->info->getcyclesx64) + ptp->info->getcycles64 = ptp_getcycles64; + }else { +/* Free running cycle counter not supported, use time. */ + ptp->info->getcycles64 = ptp_getcycles64; + +if (ptp->info->gettimex64) + ptp->info->getcyclesx64 = ptp->info->gettimex64; + +if (ptp->info->getcrosststamp) + ptp->info->getcrosscycles = ptp->info->getcrosststamp; + } +if (ptp->info->do_aux_work) {kthread_init_delayed_work (&ptp->aux_work, ptp_aux_kworker); ptp->kworker =kthread_create_worker (0 ,"ptp%d" , ptp->index);
drivers/ptp/ptp_private.h +10 -0
@@ -52,6 +52,7 @@ struct ptp_clock {int *vclock_index;struct mutex n_vclocks_mux;/* protect concurrent n_vclocks access */ bool is_virtual_clock; +bool has_cycles; };#define info_to_vclock (d) container_of((d), struct ptp_vclock, info)
@@ -96,6 +97,15 @@ static inline bool ptp_vclock_in_use(struct ptp_clock *ptp)return in_use; } +/* Check if ptp clock shall be free running */ +static inline bool ptp_clock_freerun (struct ptp_clock *ptp) +{ +if (ptp->has_cycles) +return false; + +return ptp_vclock_in_use (ptp); +} +extern struct class *ptp_class;/*
drivers/ptp/ptp_sysfs.c +7 -4
@@ -231,10 +231,13 @@ static ssize_t n_vclocks_store(struct device *dev, *(ptp->vclock_index + ptp->n_vclocks - i) =-1 ; } -if (num ==0 ) -dev_info (dev,"only physical clock in use now\n" ); -else -dev_info (dev,"guarantee physical clock free running\n" ); +/* Need to inform about changed physical clock behavior */ +if (!ptp->has_cycles) { +if (num ==0 ) +dev_info (dev,"only physical clock in use now\n" ); +else +dev_info (dev,"guarantee physical clock free running\n" ); + } ptp->n_vclocks = num;mutex_unlock (&ptp->n_vclocks_mux);
drivers/ptp/ptp_vclock.c +5 -8
@@ -68,7 +68,7 @@ static int ptp_vclock_gettimex(struct ptp_clock_info *ptp,int err;u64 ns; - err = pptp->info->gettimex64 (pptp->info, &pts, sts); + err = pptp->info->getcyclesx64 (pptp->info, &pts, sts);if (err)return err;
@@ -104,7 +104,7 @@ static int ptp_vclock_getcrosststamp(struct ptp_clock_info *ptp,int err;u64 ns; - err = pptp->info->getcrosststamp (pptp->info, xtstamp); + err = pptp->info->getcrosscycles (pptp->info, xtstamp);if (err)return err;
@@ -143,10 +143,7 @@ static u64 ptp_vclock_read(const struct cyclecounter *cc)struct ptp_clock *ptp = vclock->pclock;struct timespec64 ts = {}; -if (ptp->info->gettimex64) - ptp->info->gettimex64 (ptp->info, &ts,NULL ); -else - ptp->info->gettime64 (ptp->info, &ts); + ptp->info->getcycles64 (ptp->info, &ts);return timespec64_to_ns (&ts); }
@@ -168,11 +165,11 @@ struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock) vclock->pclock = pclock; vclock->info = ptp_vclock_info; -if (pclock->info->gettimex64) +if (pclock->info->getcyclesx64) vclock->info.gettimex64 = ptp_vclock_gettimex;else vclock->info.gettime64 = ptp_vclock_gettime; -if (pclock->info->getcrosststamp) +if (pclock->info->getcrosscycles) vclock->info.getcrosststamp = ptp_vclock_getcrosststamp; vclock->cc = ptp_vclock_cc;
include/linux/ptp_clock_kernel.h +31 -0
@@ -108,6 +108,32 @@ struct ptp_system_timestamp {* @settime64: Set the current time on the hardware clock. * parameter ts: Time value to set. * +* @getcycles64: Reads the current free running cycle counter from the hardware +* clock. +* If @getcycles64 and @getcyclesx64 are not supported, then +* @gettime64 or @gettimex64 will be used as default +* implementation. +* parameter ts: Holds the result. +* +* @getcyclesx64: Reads the current free running cycle counter from the +* hardware clock and optionally also the system clock. +* If @getcycles64 and @getcyclesx64 are not supported, then +* @gettimex64 will be used as default implementation if +* available. +* parameter ts: Holds the PHC timestamp. +* parameter sts: If not NULL, it holds a pair of timestamps +* from the system clock. The first reading is made right before +* reading the lowest bits of the PHC timestamp and the second +* reading immediately follows that. +* +* @getcrosscycles: Reads the current free running cycle counter from the +* hardware clock and system clock simultaneously. +* If @getcycles64 and @getcyclesx64 are not supported, then +* @getcrosststamp will be used as default implementation if +* available. +* parameter cts: Contains timestamp (device,system) pair, +* where system time is realtime and monotonic. +* * @enable: Request driver to enable or disable an ancillary feature. * parameter request: Desired resource to enable or disable. * parameter on: Caller passes one to enable or zero to disable.
@@ -155,6 +181,11 @@ struct ptp_clock_info {int (*getcrosststamp)(struct ptp_clock_info *ptp,struct system_device_crosststamp *cts);int (*settime64)(struct ptp_clock_info *p,const struct timespec64 *ts); +int (*getcycles64)(struct ptp_clock_info *ptp,struct timespec64 *ts); +int (*getcyclesx64)(struct ptp_clock_info *ptp,struct timespec64 *ts, +struct ptp_system_timestamp *sts); +int (*getcrosscycles)(struct ptp_clock_info *ptp, +struct system_device_crosststamp *cts);int (*enable)(struct ptp_clock_info *ptp,struct ptp_clock_request *request,int on); int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
Signature · 1 line
2.20.1
PATCH v4 2/6 ptp: Request cycles for TX timestamp
In reply to Gerhard EnglederThe free running cycle counter of physical clocks called cycles shall be used for hardware timestamps to enable synchronisation.
Introduce new flag SKBTX_HW_TSTAMP_USE_CYCLES, which signals driver to provide a TX timestamp based on cycles if cycles are supported.
--- include/linux/skbuff.h | 7 ++++++- net/socket.c | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-)
include/linux/skbuff.h +6 -1
@@ -615,6 +615,9 @@ enum {/* device driver is going to provide hardware time stamp */ SKBTX_IN_PROGRESS =1 <<2 , +/* generate hardware time stamp based on cycles if supported */ +SKBTX_HW_TSTAMP_USE_CYCLES =1 <<3 , +/* generate wifi status information (where possible) */ SKBTX_WIFI_STATUS = 1 << 4,
@@ -624,7 +627,9 @@ enum {#define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \ SKBTX_SCHED_TSTAMP) -#define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP) +#define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | \ + SKBTX_HW_TSTAMP_USE_CYCLES | \ + SKBTX_ANY_SW_TSTAMP)/* Definitions for flags in struct skb_shared_info */ enum {
net/socket.c +10 -1
@@ -683,9 +683,18 @@ void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags) {u8 flags = *tx_flags; -if (tsflags &SOF_TIMESTAMPING_TX_HARDWARE ) +if (tsflags &SOF_TIMESTAMPING_TX_HARDWARE ) { flags |=SKBTX_HW_TSTAMP ; +/* PTP hardware clocks can provide a free running cycle counter +* as a time base for virtual clocks. Tell driver to use the +* free running cycle counter for timestamp if socket is bound +* to virtual clock. +*/ +if (tsflags &SOF_TIMESTAMPING_BIND_PHC ) + flags |=SKBTX_HW_TSTAMP_USE_CYCLES ; + } +if (tsflags &SOF_TIMESTAMPING_TX_SOFTWARE ) flags |=SKBTX_SW_TSTAMP ;
Signature · 1 line
2.20.1
PATCH v4 3/6 ptp: Pass hwtstamp to ptp_convert_timestamp()
In reply to Gerhard Englederptp_convert_timestamp() converts only the timestamp hwtstamp, which is a field of the argument with the type struct skb_shared_hwtstamps *. So a pointer to the hwtstamp field of this structure is sufficient.
Rework ptp_convert_timestamp() to use an argument of type ktime_t *. This allows to add additional timestamp manipulation stages before the call of ptp_convert_timestamp().
--- drivers/ptp/ptp_vclock.c | 5 ++--- include/linux/ptp_clock_kernel.h | 7 +++---- net/socket.c | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-)
drivers/ptp/ptp_vclock.c +2 -3
@@ -232,8 +232,7 @@ int ptp_get_vclocks_index(int pclock_index, int **vclock_index) }EXPORT_SYMBOL (ptp_get_vclocks_index); -ktime_t ptp_convert_timestamp (const struct skb_shared_hwtstamps *hwtstamps, -int vclock_index) +ktime_t ptp_convert_timestamp (const ktime_t *hwtstamp,int vclock_index) {char name[PTP_CLOCK_NAME_LEN ] ="" ;struct ptp_vclock *vclock;
@@ -255,7 +254,7 @@ ktime_t ptp_convert_timestamp(const struct skb_shared_hwtstamps *hwtstamps, vclock =info_to_vclock (ptp->info); - ns =ktime_to_ns (hwtstamps->hwtstamp); + ns =ktime_to_ns (*hwtstamp);spin_lock_irqsave (&vclock->lock, flags); ns =timecounter_cyc2time (&vclock->tc, ns);
include/linux/ptp_clock_kernel.h +3 -4
@@ -384,17 +384,16 @@ int ptp_get_vclocks_index(int pclock_index, int **vclock_index);/** * ptp_convert_timestamp() - convert timestamp to a ptp vclock time * -* @hwtstamps: skb_shared_hwtstamps structure pointer +* @hwtstamp: timestamp * @vclock_index: phc index of ptp vclock. * * Returns converted timestamp, or 0 on error. */ -ktime_t ptp_convert_timestamp (const struct skb_shared_hwtstamps *hwtstamps, -int vclock_index); +ktime_t ptp_convert_timestamp (const ktime_t *hwtstamp,int vclock_index);#else static inline int ptp_get_vclocks_index (int pclock_index,int **vclock_index) {return 0 ; } -static inline ktime_t ptp_convert_timestamp (const struct skb_shared_hwtstamps *hwtstamps, +static inline ktime_t ptp_convert_timestamp (const ktime_t *hwtstamp,int vclock_index) {return 0 ; }
net/socket.c +1 -1
@@ -888,7 +888,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && !skb_is_swtx_tstamp(skb, false_tstamp)) {if (sk->sk_tsflags &SOF_TIMESTAMPING_BIND_PHC ) - hwtstamp =ptp_convert_timestamp (shhwtstamps, + hwtstamp =ptp_convert_timestamp (&shhwtstamps->hwtstamp, sk->sk_bind_phc);else hwtstamp = shhwtstamps->hwtstamp;
Signature · 1 line
2.20.1
PATCH v4 4/6 ptp: Support late timestamp determination
In reply to Gerhard EnglederIf a physical clock supports a free running cycle counter, then timestamps shall be based on this time too. For TX it is known in advance before the transmission if a timestamp based on the free running cycle counter is needed. For RX it is impossible to know which timestamp is needed before the packet is received and assigned to a socket.
Support late timestamp determination by a network device. Therefore, an address/cookie is stored within the new netdev_data field of struct skb_shared_hwtstamps. This address/cookie is provided to a new network device function called ndo_get_tstamp(), which returns a timestamp based on the normal/adjustable time or based on the free running cycle counter. If function is not supported, then timestamp handling is not changed.
This mechanism is intended for RX, but TX use is also possible.
--- include/linux/netdevice.h | 21 +++++++++++++++++ include/linux/skbuff.h | 14 ++++++++--- net/socket.c | 49 +++++++++++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 13 deletions(-)
include/linux/netdevice.h +21 -0
@@ -1356,6 +1356,12 @@ struct netdev_net_notifier {* The caller must be under RCU read context. * int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx, struct net_device_path *path); * Get the forwarding path to reach the real device from the HW destination address +* ktime_t (*ndo_get_tstamp)(struct net_device *dev, +* const struct skb_shared_hwtstamps *hwtstamps, +* bool cycles); +* Get hardware timestamp based on normal/adjustable time or free running +* cycle counter. This function is required if physical clock supports a +* free running cycle counter. */ struct net_device_ops {int (*ndo_init)(struct net_device *dev);
@@ -1578,6 +1584,9 @@ struct net_device_ops {struct net_device * (*ndo_get_peer_dev)(struct net_device *dev);int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx,struct net_device_path *path); +ktime_t (*ndo_get_tstamp)(struct net_device *dev, +const struct skb_shared_hwtstamps *hwtstamps, +bool cycles); };/**
@@ -4761,6 +4770,18 @@ static inline void netdev_rx_csum_fault(struct net_device *dev,void net_enable_timestamp (void );void net_disable_timestamp (void ); +static inline ktime_t netdev_get_tstamp (struct net_device *dev, +const struct skb_shared_hwtstamps *hwtstamps, +bool cycles) +{ +const struct net_device_ops *ops = dev->netdev_ops; + +if (ops->ndo_get_tstamp) +return ops->ndo_get_tstamp (dev, hwtstamps, cycles); + +return hwtstamps->hwtstamp; +} +static inline netdev_tx_t __netdev_start_xmit (const struct net_device_ops *ops,struct sk_buff *skb,struct net_device *dev,bool more)
include/linux/skbuff.h +11 -3
@@ -588,8 +588,10 @@ static inline bool skb_frag_must_loop(struct page *p)/** * struct skb_shared_hwtstamps - hardware time stamps -* @hwtstamp: hardware time stamp transformed into duration -* since arbitrary point in time +* @hwtstamp: hardware time stamp transformed into duration +* since arbitrary point in time +* @netdev_data: address/cookie of network device driver used as +* reference to actual hardware time stamp * * Software time stamps generated by ktime_get_real() are stored in * skb->tstamp.
@@ -601,7 +603,10 @@ static inline bool skb_frag_must_loop(struct page *p)* &skb_shared_info. Use skb_hwtstamps() to get a pointer. */ struct skb_shared_hwtstamps { -ktime_t hwtstamp; +union { +ktime_t hwtstamp; +void *netdev_data; + }; };/* Definitions for tx_flags in struct skb_shared_info */
@@ -621,6 +626,9 @@ enum {/* generate wifi status information (where possible) */ SKBTX_WIFI_STATUS =1 <<4 , +/* determine hardware time stamp based on time or cycles */ +SKBTX_HW_TSTAMP_NETDEV =1 <<5 , +/* generate software time stamp when entering packet scheduling */ SKBTX_SCHED_TSTAMP = 1 << 6, };
net/socket.c +39 -10
81 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH net-next v4 4/6] ptp: Support late timestamp determination
Quoted 14 lines · Gerhard Engleder
If a physical clock supports a free running cycle counter, then timestamps shall be based on this time too. For TX it is known in advance before the transmission if a timestamp based on the free running cycle counter is needed. For RX it is impossible to know which timestamp is needed before the packet is received and assigned to a socket.
Support late timestamp determination by a network device. Therefore, an address/cookie is stored within the new
netdev_datafield of structskb_shared_hwtstamps. This address/cookie is provided to a new network device function calledndo_get_tstamp(), which returns a timestamp based on the normal/adjustable time or based on the free running cycle counter. If function is not supported, then timestamp handling is not changed.
Quoted 3 lines
This mechanism is intended for RX, but TX use is also possible.
Signed-off-by Gerhard Engleder <[email protected]>
Re: [PATCH net-next v4 4/6] ptp: Support late timestamp determination
In reply to Gerhard EnglederQuoted 14 lines · Gerhard Engleder
If a physical clock supports a free running cycle counter, then timestamps shall be based on this time too. For TX it is known in advance before the transmission if a timestamp based on the free running cycle counter is needed. For RX it is impossible to know which timestamp is needed before the packet is received and assigned to a socket.
Support late timestamp determination by a network device. Therefore, an address/cookie is stored within the new
netdev_datafield of structskb_shared_hwtstamps. This address/cookie is provided to a new network device function calledndo_get_tstamp(), which returns a timestamp based on the normal/adjustable time or based on the free running cycle counter. If function is not supported, then timestamp handling is not changed.
Quoted 3 lines
This mechanism is intended for RX, but TX use is also possible.
Signed-off-by Gerhard Engleder <[email protected]>
PATCH v4 5/6 ptp: Speed up vclock lookup
In reply to Gerhard Englederptp_convert_timestamp() is called in the RX path of network messages. The current implementation takes ~5000ns on 1.2GHz A53. This is too much for the hot path of packet processing.
Introduce hash table for fast vclock lookup in ptp_convert_timestamp(). The execution time of ptp_convert_timestamp() is reduced to ~700ns on 1.2GHz A53.
--- drivers/ptp/ptp_private.h | 1 + drivers/ptp/ptp_vclock.c | 66 ++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 19 deletions(-)
drivers/ptp/ptp_private.h +1 -0
@@ -63,6 +63,7 @@ struct ptp_vclock {struct ptp_clock *pclock;struct ptp_clock_info info;struct ptp_clock *clock; +struct hlist_node vclock_hash_node;struct cyclecounter cc;struct timecounter tc;spinlock_t lock;/* protects tc/cc */
drivers/ptp/ptp_vclock.c +47 -19
110 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v4 6/6 tsnep: Add free running cycle counter support
In reply to Gerhard EnglederThe TSN endpoint Ethernet MAC supports a free running counter additionally to its clock. This free running counter can be read and hardware timestamps are supported. As the name implies, this counter cannot be set and its frequency cannot be adjusted.
Add free running cycle counter support based on this free running counter to physical clock. This also requires hardware time stamps based on that free running counter.
--- drivers/net/ethernet/engleder/tsnep_hw.h | 9 ++++-- drivers/net/ethernet/engleder/tsnep_main.c | 33 ++++++++++++++++++---- drivers/net/ethernet/engleder/tsnep_ptp.c | 28 ++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-)
drivers/net/ethernet/engleder/tsnep_hw.h +7 -2
@@ -43,6 +43,10 @@#define ECM_RESET_CHANNEL 0x00000100#define ECM_RESET_TXRX 0x00010000 +/* counter */ +#define ECM_COUNTER_LOW 0x0028 +#define ECM_COUNTER_HIGH 0x002C +/* control and status */ #define ECM_STATUS 0x0080#define ECM_LINK_MODE_OFF 0x01000000
@@ -190,7 +194,8 @@ struct tsnep_tx_desc {/* tsnep TX descriptor writeback */ struct tsnep_tx_desc_wb {__le32 properties; -__le32 reserved1[3 ]; +__le32 reserved1; +__le64 counter;__le64 timestamp;__le32 dma_delay;__le32 reserved2;
@@ -221,7 +226,7 @@ struct tsnep_rx_desc_wb {/* tsnep RX inline meta */ struct tsnep_rx_inline { -__le64 reserved; +__le64 counter;__le64 timestamp; };
drivers/net/ethernet/engleder/tsnep_main.c +28 -5
@@ -470,8 +470,15 @@ static bool tsnep_tx_poll(struct tsnep_tx *tx, int napi_budget) (__le32_to_cpu(entry->desc_wb->properties) & TSNEP_DESC_EXTENDED_WRITEBACK_FLAG)) {struct skb_shared_hwtstamps hwtstamps; -u64 timestamp = -__le64_to_cpu (entry->desc_wb->timestamp); +u64 timestamp; + +if (skb_shinfo (entry->skb)->tx_flags & +SKBTX_HW_TSTAMP_USE_CYCLES ) + timestamp = +__le64_to_cpu (entry->desc_wb->counter); +else + timestamp = +__le64_to_cpu (entry->desc_wb->timestamp);memset (&hwtstamps,0 ,sizeof (hwtstamps)); hwtstamps.hwtstamp =ns_to_ktime (timestamp);
@@ -704,11 +711,11 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,skb_hwtstamps (skb);struct tsnep_rx_inline *rx_inline = (struct tsnep_rx_inline *)skb->data; -u64 timestamp = -__le64_to_cpu (rx_inline->timestamp); +skb_shinfo (skb)->tx_flags |= +SKBTX_HW_TSTAMP_NETDEV ;memset (hwtstamps,0 ,sizeof (*hwtstamps)); - hwtstamps->hwtstamp =ns_to_ktime (timestamp); + hwtstamps->netdev_data = rx_inline; }skb_pull (skb,TSNEP_RX_INLINE_METADATA_SIZE ); skb->protocol = eth_type_trans(skb,
@@ -1010,6 +1017,21 @@ static int tsnep_netdev_set_mac_address(struct net_device *netdev, void *addr)return 0 ; } +static ktime_t tsnep_netdev_get_tstamp (struct net_device *netdev, +const struct skb_shared_hwtstamps *hwtstamps, +bool cycles) +{ +struct tsnep_rx_inline *rx_inline = hwtstamps->netdev_data; +u64 timestamp; + +if (cycles) + timestamp =__le64_to_cpu (rx_inline->counter); +else + timestamp =__le64_to_cpu (rx_inline->timestamp); + +return ns_to_ktime (timestamp); +} +static const struct net_device_ops tsnep_netdev_ops = { .ndo_open = tsnep_netdev_open, .ndo_stop = tsnep_netdev_close,
@@ -1019,6 +1041,7 @@ static const struct net_device_ops tsnep_netdev_ops = { .ndo_get_stats64 = tsnep_netdev_get_stats64, .ndo_set_mac_address = tsnep_netdev_set_mac_address, + .ndo_get_tstamp = tsnep_netdev_get_tstamp, .ndo_setup_tc = tsnep_tc_setup, };
drivers/net/ethernet/engleder/tsnep_ptp.c +28 -0
@@ -175,6 +175,33 @@ static int tsnep_ptp_settime64(struct ptp_clock_info *ptp,return 0 ; } +static int tsnep_ptp_getcyclesx64 (struct ptp_clock_info *ptp, +struct timespec64 *ts, +struct ptp_system_timestamp *sts) +{ + struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter, + ptp_clock_info); +u32 high_before; +u32 low; +u32 high; +u64 counter; + +/* read high dword twice to detect overrun */ + high =ioread32 (adapter->addr +ECM_COUNTER_HIGH ); +do { +ptp_read_system_prets (sts); + low =ioread32 (adapter->addr +ECM_COUNTER_LOW ); +ptp_read_system_postts (sts); + high_before = high; + high =ioread32 (adapter->addr +ECM_COUNTER_HIGH ); + }while (high != high_before); + counter = (((u64 )high) <<32 ) | ((u64 )low); + + *ts =ns_to_timespec64 (counter); + +return 0 ; +} +int tsnep_ptp_init (struct tsnep_adapter *adapter) {int retval =0 ;
@@ -192,6 +219,7 @@ int tsnep_ptp_init(struct tsnep_adapter *adapter) adapter->ptp_clock_info.adjtime = tsnep_ptp_adjtime; adapter->ptp_clock_info.gettimex64 = tsnep_ptp_gettimex64; adapter->ptp_clock_info.settime64 = tsnep_ptp_settime64; + adapter->ptp_clock_info.getcyclesx64 = tsnep_ptp_getcyclesx64;spin_lock_init (&adapter->ptp_lock);
Signature · 1 line
2.20.1
Re: [PATCH net-next v4 6/6] tsnep: Add free running cycle counter support
Quoted 10 lines · Gerhard Engleder
The TSN endpoint Ethernet MAC supports a free running counter additionally to its clock. This free running counter can be read and hardware timestamps are supported. As the name implies, this counter cannot be set and its frequency cannot be adjusted.
Add free running cycle counter support based on this free running counter to physical clock. This also requires hardware time stamps based on that free running counter.
Signed-off-by Gerhard Engleder <[email protected]>
This series was applied to netdev/net-next.git (master) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (master) by Paolo Abeni <[email protected]>:
Quoted 8 lines · On Fri, 6 May 2022 22:01:36 +0200 you wrote:
ptp vclocks require a clock with free running time for the timecounter. Currently only a physical clock forced to free running is supported. If vclocks are used, then the physical clock cannot be synchronized anymore. The synchronized time is not available in hardware in this case. As a result, timed transmission with TAPRIO hardware support is not possible anymore.
[...]
Here is the summary with links:
- [net-next,v4,1/6] ptp: Add cycles support for virtual clocks
https://git.kernel.org/netdev/net-next/c/42704b26b0f1 - [net-next,v4,2/6] ptp: Request cycles for TX timestamp https://git.kernel.org/netdev/net-next/c/51eb7492af27 - [net-next,v4,3/6] ptp: Pass hwtstamp to ptp_convert_timestamp() https://git.kernel.org/netdev/net-next/c/d58809d854c9 - [net-next,v4,4/6] ptp: Support late timestamp determination https://git.kernel.org/netdev/net-next/c/97dc7cd92ac6 - [net-next,v4,5/6] ptp: Speed up vclock lookup https://git.kernel.org/netdev/net-next/c/fcf308e50928 - [net-next,v4,6/6] tsnep: Add free running cycle counter support https://git.kernel.org/netdev/net-next/c/0abb62b68252
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html