Commit 1060707e authored by Jacob Keller's avatar Jacob Keller Committed by David S. Miller

ptp: introduce helpers to adjust by scaled parts per million

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: default avatarJacob Keller <jacob.e.keller@intel.com>
Acked-by: default avatarRichard Cochran <richardcochran@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent b9a61b97
...@@ -29,17 +29,11 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta) ...@@ -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, struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
ptp_clock_info); ptp_clock_info);
struct e1000_hw *hw = &adapter->hw; struct e1000_hw *hw = &adapter->hw;
bool neg_adj = false;
unsigned long flags; unsigned long flags;
u64 adjustment; u64 incvalue;
u32 timinca, incvalue; u32 timinca;
s32 ret_val; s32 ret_val;
if (delta < 0) {
neg_adj = true;
delta = -delta;
}
/* Get the System Time Register SYSTIM base frequency */ /* Get the System Time Register SYSTIM base frequency */
ret_val = e1000e_get_base_timinca(adapter, &timinca); ret_val = e1000e_get_base_timinca(adapter, &timinca);
if (ret_val) if (ret_val)
...@@ -48,11 +42,7 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta) ...@@ -48,11 +42,7 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
spin_lock_irqsave(&adapter->systim_lock, flags); spin_lock_irqsave(&adapter->systim_lock, flags);
incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK; incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
incvalue = adjust_by_scaled_ppm(incvalue, delta);
adjustment = mul_u64_u64_div_u64(incvalue, (u64)delta,
1000000ULL << 16);
incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
timinca &= ~E1000_TIMINCA_INCVALUE_MASK; timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
timinca |= incvalue; timinca |= incvalue;
......
...@@ -347,23 +347,12 @@ static int i40e_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) ...@@ -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_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
struct i40e_hw *hw = &pf->hw; struct i40e_hw *hw = &pf->hw;
u64 adj, freq, diff; u64 adj, base_adj;
int neg_adj = 0;
if (scaled_ppm < 0) {
neg_adj = 1;
scaled_ppm = -scaled_ppm;
}
smp_mb(); /* Force any pending update before accessing. */ smp_mb(); /* Force any pending update before accessing. */
freq = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult); base_adj = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult);
diff = mul_u64_u64_div_u64(freq, (u64)scaled_ppm,
1000000ULL << 16);
if (neg_adj) adj = adjust_by_scaled_ppm(base_adj, scaled_ppm);
adj = I40E_PTP_40GB_INCVAL - diff;
else
adj = I40E_PTP_40GB_INCVAL + diff;
wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF); wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32); wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
......
...@@ -1444,24 +1444,10 @@ static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) ...@@ -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_pf *pf = ptp_info_to_pf(info);
struct ice_hw *hw = &pf->hw; struct ice_hw *hw = &pf->hw;
u64 incval, diff; u64 incval;
int neg_adj = 0;
int err; int err;
incval = ice_base_incval(pf); incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
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;
err = ice_ptp_write_incval_locked(hw, incval); err = ice_ptp_write_incval_locked(hw, incval);
if (err) { if (err) {
dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
......
...@@ -195,23 +195,9 @@ static int igb_ptp_adjfine_82576(struct ptp_clock_info *ptp, long scaled_ppm) ...@@ -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, struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
ptp_caps); ptp_caps);
struct e1000_hw *hw = &igb->hw; struct e1000_hw *hw = &igb->hw;
int neg_adj = 0; u64 incvalue;
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);
if (neg_adj) incvalue = adjust_by_scaled_ppm(INCVALUE_82576, scaled_ppm);
incvalue -= rate;
else
incvalue += rate;
wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK)); wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
......
...@@ -451,21 +451,11 @@ static int ixgbe_ptp_adjfine_82599(struct ptp_clock_info *ptp, long scaled_ppm) ...@@ -451,21 +451,11 @@ static int ixgbe_ptp_adjfine_82599(struct ptp_clock_info *ptp, long scaled_ppm)
struct ixgbe_adapter *adapter = struct ixgbe_adapter *adapter =
container_of(ptp, struct ixgbe_adapter, ptp_caps); container_of(ptp, struct ixgbe_adapter, ptp_caps);
struct ixgbe_hw *hw = &adapter->hw; struct ixgbe_hw *hw = &adapter->hw;
u64 incval, diff; u64 incval;
int neg_adj = 0;
if (scaled_ppm < 0) {
neg_adj = 1;
scaled_ppm = -scaled_ppm;
}
smp_mb(); smp_mb();
incval = READ_ONCE(adapter->base_incval); incval = READ_ONCE(adapter->base_incval);
incval = adjust_by_scaled_ppm(incval, scaled_ppm);
diff = mul_u64_u64_div_u64(incval, scaled_ppm,
1000000ULL << 16);
incval = neg_adj ? (incval - diff) : (incval + diff);
switch (hw->mac.type) { switch (hw->mac.type) {
case ixgbe_mac_X540: case ixgbe_mac_X540:
...@@ -502,17 +492,11 @@ static int ixgbe_ptp_adjfine_X550(struct ptp_clock_info *ptp, long scaled_ppm) ...@@ -502,17 +492,11 @@ static int ixgbe_ptp_adjfine_X550(struct ptp_clock_info *ptp, long scaled_ppm)
struct ixgbe_adapter *adapter = struct ixgbe_adapter *adapter =
container_of(ptp, struct ixgbe_adapter, ptp_caps); container_of(ptp, struct ixgbe_adapter, ptp_caps);
struct ixgbe_hw *hw = &adapter->hw; struct ixgbe_hw *hw = &adapter->hw;
int neg_adj = 0; bool neg_adj;
u64 rate; u64 rate;
u32 inca; u32 inca;
if (scaled_ppm < 0) { neg_adj = diff_by_scaled_ppm(IXGBE_X550_BASE_PERIOD, scaled_ppm, &rate);
neg_adj = 1;
scaled_ppm = -scaled_ppm;
}
rate = mul_u64_u64_div_u64(IXGBE_X550_BASE_PERIOD, scaled_ppm,
1000000ULL << 16);
/* warn if rate is too large */ /* warn if rate is too large */
if (rate >= INCVALUE_MASK) if (rate >= INCVALUE_MASK)
......
...@@ -248,6 +248,52 @@ static inline long scaled_ppm_to_ppb(long ppm) ...@@ -248,6 +248,52 @@ static inline long scaled_ppm_to_ppb(long ppm)
return (long)ppb; 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) #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment