]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
ptp: introduce helpers to adjust by scaled parts per million
authorJacob Keller <jacob.e.keller@intel.com>
Fri, 28 Oct 2022 11:04:13 +0000 (04:04 -0700)
committerDavid S. Miller <davem@davemloft.net>
Mon, 31 Oct 2022 11:14:16 +0000 (11:14 +0000)
Many drivers implement the .adjfreq or .adjfine PTP op function with the
same basic logic:

  1. Determine a base frequency value
  2. Multiply this by the abs() of the requested adjustment, then divide by
     the appropriate divisor (1 billion, or 65,536 billion).
  3. Add or subtract this difference from the base frequency to calculate a
     new adjustment.

A few drivers need the difference and direction rather than the combined
new increment value.

I recently converted the Intel drivers to .adjfine and the scaled parts per
million (65.536 parts per billion) logic. To avoid overflow with minimal
loss of precision, mul_u64_u64_div_u64 was used.

The basic logic used by all of these drivers is very similar, and leads to
a lot of duplicate code to perform the same task.

Rather than keep this duplicate code, introduce diff_by_scaled_ppm and
adjust_by_scaled_ppm. These helper functions calculate the difference or
adjustment necessary based on the scaled parts per million input.

The diff_by_scaled_ppm function returns true if the difference should be
subtracted, and false otherwise.

Update the Intel drivers to use the new helper functions. Other vendor
drivers will be converted to .adjfine and this helper function in the
following changes.

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/intel/e1000e/ptp.c
drivers/net/ethernet/intel/i40e/i40e_ptp.c
drivers/net/ethernet/intel/ice/ice_ptp.c
drivers/net/ethernet/intel/igb/igb_ptp.c
drivers/net/ethernet/intel/ixgbe/ixgbe_ptp.c
include/linux/ptp_clock_kernel.h

index 0e488e4fa5c167ffbd31c3ccc67799916a984652..6e5a1720e6cd663a75b1e57d57886c1805c77312 100644 (file)
@@ -29,17 +29,11 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
        struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
                                                     ptp_clock_info);
        struct e1000_hw *hw = &adapter->hw;
-       bool neg_adj = false;
        unsigned long flags;
-       u64 adjustment;
-       u32 timinca, incvalue;
+       u64 incvalue;
+       u32 timinca;
        s32 ret_val;
 
-       if (delta < 0) {
-               neg_adj = true;
-               delta = -delta;
-       }
-
        /* Get the System Time Register SYSTIM base frequency */
        ret_val = e1000e_get_base_timinca(adapter, &timinca);
        if (ret_val)
@@ -48,11 +42,7 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
        spin_lock_irqsave(&adapter->systim_lock, flags);
 
        incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
-
-       adjustment = mul_u64_u64_div_u64(incvalue, (u64)delta,
-                                        1000000ULL << 16);
-
-       incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
+       incvalue = adjust_by_scaled_ppm(incvalue, delta);
 
        timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
        timinca |= incvalue;
index ffea0c9c82f1e755c0a0f17bc0b08f40fefb39c2..c37abbb3cd068b1901e8e9a2cb75925d4df222e6 100644 (file)
@@ -347,23 +347,12 @@ static int i40e_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
 {
        struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
        struct i40e_hw *hw = &pf->hw;
-       u64 adj, freq, diff;
-       int neg_adj = 0;
-
-       if (scaled_ppm < 0) {
-               neg_adj = 1;
-               scaled_ppm = -scaled_ppm;
-       }
+       u64 adj, base_adj;
 
        smp_mb(); /* Force any pending update before accessing. */
-       freq = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult);
-       diff = mul_u64_u64_div_u64(freq, (u64)scaled_ppm,
-                                  1000000ULL << 16);
+       base_adj = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult);
 
-       if (neg_adj)
-               adj = I40E_PTP_40GB_INCVAL - diff;
-       else
-               adj = I40E_PTP_40GB_INCVAL + diff;
+       adj = adjust_by_scaled_ppm(base_adj, scaled_ppm);
 
        wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
        wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
index 011b727ab1903f5b183aaa9fd0daeb5eaa86a4f1..5cf198a33e26bad8a923ee7f3ba2806e9984e9e1 100644 (file)
@@ -1444,24 +1444,10 @@ static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
 {
        struct ice_pf *pf = ptp_info_to_pf(info);
        struct ice_hw *hw = &pf->hw;
-       u64 incval, diff;
-       int neg_adj = 0;
+       u64 incval;
        int err;
 
-       incval = ice_base_incval(pf);
-
-       if (scaled_ppm < 0) {
-               neg_adj = 1;
-               scaled_ppm = -scaled_ppm;
-       }
-
-       diff = mul_u64_u64_div_u64(incval, (u64)scaled_ppm,
-                                  1000000ULL << 16);
-       if (neg_adj)
-               incval -= diff;
-       else
-               incval += diff;
-
+       incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
        err = ice_ptp_write_incval_locked(hw, incval);
        if (err) {
                dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
index 15e57460e19ea0823d60b275d68180271c177084..6f471b91f562016914dcf6f230f04e31d06b8809 100644 (file)
@@ -195,23 +195,9 @@ static int igb_ptp_adjfine_82576(struct ptp_clock_info *ptp, long scaled_ppm)
        struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
                                               ptp_caps);
        struct e1000_hw *hw = &igb->hw;
-       int neg_adj = 0;
-       u64 rate;
-       u32 incvalue;
-
-       if (scaled_ppm < 0) {
-               neg_adj = 1;
-               scaled_ppm = -scaled_ppm;
-       }
-
-       incvalue = INCVALUE_82576;
-       rate = mul_u64_u64_div_u64(incvalue, (u64)scaled_ppm,
-                                  1000000ULL << 16);
+       u64 incvalue;
 
-       if (neg_adj)
-               incvalue -= rate;
-       else
-               incvalue += rate;
+       incvalue = adjust_by_scaled_ppm(INCVALUE_82576, scaled_ppm);
 
        wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
 
index f8605f57bd067d64f44ee028c80f1606107f019f..fd3f77a9e28db68b61646b20de380311ce33fd12 100644 (file)
@@ -451,21 +451,11 @@ static int ixgbe_ptp_adjfine_82599(struct ptp_clock_info *ptp, long scaled_ppm)
        struct ixgbe_adapter *adapter =
                container_of(ptp, struct ixgbe_adapter, ptp_caps);
        struct ixgbe_hw *hw = &adapter->hw;
-       u64 incval, diff;
-       int neg_adj = 0;
-
-       if (scaled_ppm < 0) {
-               neg_adj = 1;
-               scaled_ppm = -scaled_ppm;
-       }
+       u64 incval;
 
        smp_mb();
        incval = READ_ONCE(adapter->base_incval);
-
-       diff = mul_u64_u64_div_u64(incval, scaled_ppm,
-                                  1000000ULL << 16);
-
-       incval = neg_adj ? (incval - diff) : (incval + diff);
+       incval = adjust_by_scaled_ppm(incval, scaled_ppm);
 
        switch (hw->mac.type) {
        case ixgbe_mac_X540:
@@ -502,17 +492,11 @@ static int ixgbe_ptp_adjfine_X550(struct ptp_clock_info *ptp, long scaled_ppm)
        struct ixgbe_adapter *adapter =
                        container_of(ptp, struct ixgbe_adapter, ptp_caps);
        struct ixgbe_hw *hw = &adapter->hw;
-       int neg_adj = 0;
+       bool neg_adj;
        u64 rate;
        u32 inca;
 
-       if (scaled_ppm < 0) {
-               neg_adj = 1;
-               scaled_ppm = -scaled_ppm;
-       }
-
-       rate = mul_u64_u64_div_u64(IXGBE_X550_BASE_PERIOD, scaled_ppm,
-                                  1000000ULL << 16);
+       neg_adj = diff_by_scaled_ppm(IXGBE_X550_BASE_PERIOD, scaled_ppm, &rate);
 
        /* warn if rate is too large */
        if (rate >= INCVALUE_MASK)
index ad4aaadc2f7aaaddd947cbcd48e00c4333416115..f4781c5766d6870951ec443b3411967740091d7e 100644 (file)
@@ -248,6 +248,52 @@ static inline long scaled_ppm_to_ppb(long ppm)
        return (long)ppb;
 }
 
+/**
+ * diff_by_scaled_ppm - Calculate difference using scaled ppm
+ * @base: the base increment value to adjust
+ * @scaled_ppm: scaled parts per million to adjust by
+ * @diff: on return, the absolute value of calculated diff
+ *
+ * Calculate the difference to adjust the base increment using scaled parts
+ * per million.
+ *
+ * Use mul_u64_u64_div_u64 to perform the difference calculation in avoid
+ * possible overflow.
+ *
+ * Returns: true if scaled_ppm is negative, false otherwise
+ */
+static inline bool diff_by_scaled_ppm(u64 base, long scaled_ppm, u64 *diff)
+{
+       bool negative = false;
+
+       if (scaled_ppm < 0) {
+               negative = true;
+               scaled_ppm = -scaled_ppm;
+       }
+
+       *diff = mul_u64_u64_div_u64(base, (u64)scaled_ppm, 1000000ULL << 16);
+
+       return negative;
+}
+
+/**
+ * adjust_by_scaled_ppm - Adjust a base increment by scaled parts per million
+ * @base: the base increment value to adjust
+ * @scaled_ppm: scaled parts per million frequency adjustment
+ *
+ * Helper function which calculates a new increment value based on the
+ * requested scaled parts per million adjustment.
+ */
+static inline u64 adjust_by_scaled_ppm(u64 base, long scaled_ppm)
+{
+       u64 diff;
+
+       if (diff_by_scaled_ppm(base, scaled_ppm, &diff))
+               return base - diff;
+
+       return base + diff;
+}
+
 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
 
 /**