Skip to content

Commit 58a1d14

Browse files
jacob-kellerNipaLocal
authored andcommitted
ice: add and use roundup_u64 instead of open coding equivalent
In ice_ptp_cfg_clkout(), the ice driver needs to calculate the nearest next second of a current time value specified in nanoseconds. It implements this using div64_u64, because the time value is a u64. It could use div_u64 since NSEC_PER_SEC is smaller than 32-bits. Ideally this would be implemented directly with roundup(), but that can't work on all platforms due to a division which requires using the specific macros and functions due to platform restrictions, and to ensure that the most appropriate and fast instructions are used. The kernel doesn't currently provide any 64-bit equivalents for doing roundup. Attempting to use roundup() on a 32-bit platform will result in a link failure due to not having a direct 64-bit division. The closest equivalent for this is DIV64_U64_ROUND_UP, which does a division always rounding up. However, this only computes the division, and forces use of the div64_u64 in cases where the divisor is a 32bit value and could make use of div_u64. Introduce DIV_U64_ROUND_UP based on div_u64, and then use it to implement roundup_u64 which takes a u64 input value and a u32 rounding value. The name roundup_u64 matches the naming scheme of div_u64, and future patches could implement roundup64_u64 if they need to round by a multiple that is greater than 32-bits. Replace the logic in ice_ptp.c which does this equivalent with the newly added roundup_u64. Tested-by: Pucha Himasekhar Reddy <[email protected]> Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 43b59e0 commit 58a1d14

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

drivers/net/ethernet/intel/ice/ice_ptp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,8 +1773,7 @@ static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan,
17731773
* maintaining phase
17741774
*/
17751775
if (start_time < current_time)
1776-
start_time = div64_u64(current_time + NSEC_PER_SEC - 1,
1777-
NSEC_PER_SEC) * NSEC_PER_SEC + phase;
1776+
start_time = roundup_u64(current_time, NSEC_PER_SEC) + phase;
17781777

17791778
if (ice_is_e810(hw))
17801779
start_time -= E810_OUT_PROP_DELAY_NS;

include/linux/math64.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,19 @@ u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div);
297297
#define DIV64_U64_ROUND_UP(ll, d) \
298298
({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); })
299299

300+
/**
301+
* DIV_U64_ROUND_UP - unsigned 64bit divide with 32bit divisor rounded up
302+
* @ll: unsigned 64bit dividend
303+
* @d: unsigned 32bit divisor
304+
*
305+
* Divide unsigned 64bit dividend by unsigned 32bit divisor
306+
* and round up.
307+
*
308+
* Return: dividend / divisor rounded up
309+
*/
310+
#define DIV_U64_ROUND_UP(ll, d) \
311+
({ u32 _tmp = (d); div_u64((ll) + _tmp - 1, _tmp); })
312+
300313
/**
301314
* DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer
302315
* @dividend: unsigned 64bit dividend
@@ -342,4 +355,19 @@ u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div);
342355
div_s64((__x - (__d / 2)), __d); \
343356
} \
344357
)
358+
359+
/**
360+
* roundup_u64 - Round up a 64bit value to the next specified 32bit multiple
361+
* @x: the value to up
362+
* @y: 32bit multiple to round up to
363+
*
364+
* Rounds @x to the next multiple of @y. For 32bit @x values, see roundup and
365+
* the faster round_up() for powers of 2.
366+
*
367+
* Return: rounded up value.
368+
*/
369+
static inline u64 roundup_u64(u64 x, u32 y)
370+
{
371+
return DIV_U64_ROUND_UP(x, y) * y;
372+
}
345373
#endif /* _LINUX_MATH64_H */

0 commit comments

Comments
 (0)