Skip to contentSkip to conversation

Provide generic function to calc mult/shift factors for clocks

PATCH In review Thomas Gleixner 4 participants 10 messages Nov 11, 2009 – Nov 13, 2009

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/3 clockevents: Use u32 for mult and shift factors

In reply to Thomas Gleixner

The mult and shift factors of clock events differ in their data type from those of clock sources for no reason. u32 is sufficient for both. shift is always <= 32 and mult is limited to 2^32-1 to avoid 64bit multiplication overflows in the conversion.

Preparatory patch for a generic mult/shift factor calculation function.

Cc Mikael Pettersson <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Linus Walleij <[email protected]>
Cc John Stultz <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/clockchips.h |    4 ++--
 kernel/time/timer_list.c   |    4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/clockchips.h +2 -2
Index: linux-2.6-tip/include/linux/clockchips.h
===================================================================
@@ -79,8 +79,8 @@ struct clock_event_device {
 	unsigned int		features;
 	unsigned long		max_delta_ns;
 	unsigned long		min_delta_ns;
-	unsigned long		mult;
-	int			shift;
+	u32			mult;
+	u32			shift;
 	int			rating;
 	int			irq;
 	const struct cpumask	*cpumask;
kernel/time/timer_list.c +2 -2
Index: linux-2.6-tip/kernel/time/timer_list.c
===================================================================
@@ -206,8 +206,8 @@ print_tickdevice(struct seq_file *m, str
 	SEQ_printf(m, "%s\n", dev->name);
 	SEQ_printf(m, " max_delta_ns:   %lu\n", dev->max_delta_ns);
 	SEQ_printf(m, " min_delta_ns:   %lu\n", dev->min_delta_ns);
-	SEQ_printf(m, " mult:           %lu\n", dev->mult);
-	SEQ_printf(m, " shift:          %d\n", dev->shift);
+	SEQ_printf(m, " mult:           %u\n", dev->mult);
+	SEQ_printf(m, " shift:          %u\n", dev->shift);
 	SEQ_printf(m, " mode:           %d\n", dev->mode);
 	SEQ_printf(m, " next_event:     %Ld nsecs\n",
 		   (unsigned long long) ktime_to_ns(dev->next_event));
#5 lore raw

PATCH 3/3 mips: Use generic mult/shift factor calculation for clocks

In reply to Thomas Gleixner

Replace the MIPS functions of mult/shift factor calculation for clock events and clock sources with inline functions which call the generic functions. The minimum guaranteed conversion range is set to 4 seconds which corresponds to the current MIPS implementation.

Cc Mikael Pettersson <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Linus Walleij <[email protected]>
Cc John Stultz <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 arch/mips/include/asm/time.h |   14 +++++++++++---
 arch/mips/kernel/time.c      |   33 ---------------------------------
 2 files changed, 11 insertions(+), 36 deletions(-)
arch/mips/include/asm/time.h +11 -3
Index: linux-2.6-tip/arch/mips/include/asm/time.h
===================================================================
@@ -84,8 +84,16 @@ static inline int init_mips_clocksource(
 #endif
 }
 
-extern void clocksource_set_clock(struct clocksource *cs, unsigned int clock);
-extern void clockevent_set_clock(struct clock_event_device *cd,
-		unsigned int clock);
+static inline void clocksource_set_clock(struct clocksource *cs,
+					 unsigned int clock)
+{
+	clocksource_calc_mult_shift(cs, clock, 4);
+}
+
+static inline void clockevent_set_clock(struct clock_event_device *cd,
+					unsigned int clock)
+{
+	clockevents_calc_mult_shift(cd, clock, 4);
+}
 
 #endif /* _ASM_TIME_H */
arch/mips/kernel/time.c +0 -33
Index: linux-2.6-tip/arch/mips/kernel/time.c
===================================================================
@@ -71,39 +71,6 @@ EXPORT_SYMBOL(perf_irq);
 
 unsigned int mips_hpt_frequency;
 
-void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
-{
-	u64 temp;
-	u32 shift;
-
-	/* Find a shift value */
-	for (shift = 32; shift > 0; shift--) {
-		temp = (u64) NSEC_PER_SEC << shift;
-		do_div(temp, clock);
-		if ((temp >> 32) == 0)
-			break;
-	}
-	cs->shift = shift;
-	cs->mult = (u32) temp;
-}
-
-void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
-	unsigned int clock)
-{
-	u64 temp;
-	u32 shift;
-
-	/* Find a shift value */
-	for (shift = 32; shift > 0; shift--) {
-		temp = (u64) clock << shift;
-		do_div(temp, NSEC_PER_SEC);
-		if ((temp >> 32) == 0)
-			break;
-	}
-	cd->shift = shift;
-	cd->mult = (u32) temp;
-}
-
 /*
  * This function exists in order to cause an error due to a duplicate
  * definition if platform code should have its own implementation.  The hook
#3 lore raw

PATCH 2/3 clocksource: Provide a generic mult/shift factor calculation

In reply to Thomas Gleixner

MIPS has two functions to calculcate the mult/shift factors for clock sources and clock events at run time. ARM needs such functions as well.

Implement a function which calculates the mult/shift factors based on the frequencies to which and from which is converted. The function also has a parameter to specify the minimum conversion range in seconds. This range is guaranteed not to produce a 64bit overflow when a value is multiplied with the calculated mult factor. The larger the conversion range the less becomes the conversion accuracy.

Provide two inline wrappers which handle clock events and clock sources. For clock events the "from" frequency is nano seconds per second which corresponds to 1GHz and "to" is the device frequency. For clock sources "from" is the device frequency and "to" is nano seconds per second.

Cc Mikael Pettersson <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Linus Walleij <[email protected]>
Cc John Stultz <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 include/linux/clockchips.h  |    7 +++++
 include/linux/clocksource.h |   10 ++++++++
 kernel/time/clocksource.c   |   53 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+)
include/linux/clockchips.h +7 -0
Index: linux-2.6-tip/include/linux/clockchips.h
===================================================================
@@ -130,6 +130,13 @@ extern int clockevents_program_event(str
 
 extern void clockevents_handle_noop(struct clock_event_device *dev);
 
+static inline void
+clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
+{
+	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
+				      freq, minsec);
+}
+
 #ifdef CONFIG_GENERIC_CLOCKEVENTS
 extern void clockevents_notify(unsigned long reason, void *arg);
 #else
include/linux/clocksource.h +10 -0
Index: linux-2.6-tip/include/linux/clocksource.h
===================================================================
@@ -279,6 +279,16 @@ extern void clocksource_resume(void);
 extern struct clocksource * __init __weak clocksource_default_clock(void);
 extern void clocksource_mark_unstable(struct clocksource *cs);
 
+extern void
+clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
+
+static inline void
+clocksource_calc_mult_shift(struct clocksource *cs, u32 freq, u32 minsec)
+{
+	return clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
+				      NSEC_PER_SEC, minsec);
+}
+
 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
 extern void update_vsyscall_tz(void);
kernel/time/clocksource.c +53 -0

59 lines not on this page · raw message

#0 lore raw

cover Provide generic function to calc mult/shift factors for clocks

The MIPS tree has functions to calculate the mult/shift factors for clock events and clock sources. ARM needs such functions as well.

This patch set provides a generic function which replaces the MIPS implementation.

Thanks,

tglx

Acked-by on the v1 series
#7 lore raw

RE: [patch 0/3] Provide generic function to calc mult/shift factors forclocks

[tglx]

Quoted 2 lines

This patch set provides a generic function which replaces the MIPS implementation.

Sweet as pie!

Linus Walleij

#8 lore raw
In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner

The MIPS tree has functions to calculate the mult/shift factors for clock events and clock sources. ARM needs such functions as well.

This patch set provides a generic function which replaces the MIPS implementation.

Looks good to me, feel free to throw in my Ack-by. I suggest you merge the MIPS part of the series through your tree.

Ralf

Tested-by on the v1 series
#9 lore raw
In reply to Thomas Gleixner
Quoted 5 lines · Thomas Gleixner

The MIPS tree has functions to calculate the mult/shift factors for clock events and clock sources. ARM needs such functions as well.

This patch set provides a generic function which replaces the MIPS implementation.

I rebased my ARM IOP clocksource/clockevent patch kit on these generic functions, and it works fine and results in the exact same shift/mult values as with my clone of the MIPS code.

#2 lore raw

[tip:timers/core] clockevents: Use u32 for mult and shift factors

In reply to Thomas Gleixner
Commit-ID:  23af368e9a904f59256c27d371ce223d6cee0430
Gitweb:     http://git.kernel.org/tip/23af368e9a904f59256c27d371ce223d6cee0430
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Wed, 11 Nov 2009 14:05:25 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 13 Nov 2009 20:46:23 +0100

clockevents: Use u32 for mult and shift factors

The mult and shift factors of clock events differ in their data type from those of clock sources for no reason. u32 is sufficient for both. shift is always <= 32 and mult is limited to 2^32-1 to avoid 64bit multiplication overflows in the conversion.

Preparatory patch for a generic mult/shift factor calculation function.

LKML-Reference: <[email protected]>

---
 include/linux/clockchips.h |    4 ++--
 kernel/time/timer_list.c   |    4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/clockchips.h +2 -2
index 3a1dbba..3b58410 100644
@@ -79,8 +79,8 @@ struct clock_event_device {
 	unsigned int		features;
 	unsigned long		max_delta_ns;
 	unsigned long		min_delta_ns;
-	unsigned long		mult;
-	int			shift;
+	u32			mult;
+	u32			shift;
 	int			rating;
 	int			irq;
 	const struct cpumask	*cpumask;
kernel/time/timer_list.c +2 -2
index 1b5b7aa..fa00da1 100644
@@ -206,8 +206,8 @@ print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
 	SEQ_printf(m, "%s\n", dev->name);
 	SEQ_printf(m, " max_delta_ns:   %lu\n", dev->max_delta_ns);
 	SEQ_printf(m, " min_delta_ns:   %lu\n", dev->min_delta_ns);
-	SEQ_printf(m, " mult:           %lu\n", dev->mult);
-	SEQ_printf(m, " shift:          %d\n", dev->shift);
+	SEQ_printf(m, " mult:           %u\n", dev->mult);
+	SEQ_printf(m, " shift:          %u\n", dev->shift);
 	SEQ_printf(m, " mode:           %d\n", dev->mode);
 	SEQ_printf(m, " next_event:     %Ld nsecs\n",
 		   (unsigned long long) ktime_to_ns(dev->next_event));
#4 lore raw

[tip:timers/core] clocksource: Provide a generic mult/shift factor calculation

In reply to Thomas Gleixner
Commit-ID:  7d2f944a2b836c69a9d260a0a5f0d1720d57fdff
Gitweb:     http://git.kernel.org/tip/7d2f944a2b836c69a9d260a0a5f0d1720d57fdff
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Wed, 11 Nov 2009 14:05:29 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 13 Nov 2009 20:46:23 +0100

clocksource: Provide a generic mult/shift factor calculation

MIPS has two functions to calculcate the mult/shift factors for clock sources and clock events at run time. ARM needs such functions as well.

Implement a function which calculates the mult/shift factors based on the frequencies to which and from which is converted. The function also has a parameter to specify the minimum conversion range in seconds. This range is guaranteed not to produce a 64bit overflow when a value is multiplied with the calculated mult factor. The larger the conversion range the less becomes the conversion accuracy.

Provide two inline wrappers which handle clock events and clock sources. For clock events the "from" frequency is nano seconds per second which corresponds to 1GHz and "to" is the device frequency. For clock sources "from" is the device frequency and "to" is nano seconds per second.

LKML-Reference: <[email protected]>

---
 include/linux/clockchips.h  |    7 +++++
 include/linux/clocksource.h |   10 ++++++++
 kernel/time/clocksource.c   |   53 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+), 0 deletions(-)
include/linux/clockchips.h +7 -0
index 3b58410..4d438b0 100644
@@ -130,6 +130,13 @@ extern int clockevents_program_event(struct clock_event_device *dev,
 
 extern void clockevents_handle_noop(struct clock_event_device *dev);
 
+static inline void
+clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
+{
+	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
+				      freq, minsec);
+}
+
 #ifdef CONFIG_GENERIC_CLOCKEVENTS
 extern void clockevents_notify(unsigned long reason, void *arg);
 #else
include/linux/clocksource.h +10 -0
index 83d2fbd..f57f882 100644
@@ -279,6 +279,16 @@ extern void clocksource_resume(void);
 extern struct clocksource * __init __weak clocksource_default_clock(void);
 extern void clocksource_mark_unstable(struct clocksource *cs);
 
+extern void
+clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
+
+static inline void
+clocksource_calc_mult_shift(struct clocksource *cs, u32 freq, u32 minsec)
+{
+	return clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
+				      NSEC_PER_SEC, minsec);
+}
+
 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
 extern void update_vsyscall_tz(void);
kernel/time/clocksource.c +53 -0

59 lines not on this page · raw message

#6 lore raw

[tip:timers/core] mips: Use generic mult/shift factor calculation for clocks

In reply to Thomas Gleixner
Commit-ID:  e3a4fab0c0c30e21e104712f4e9cb39f175d0f21
Gitweb:     http://git.kernel.org/tip/e3a4fab0c0c30e21e104712f4e9cb39f175d0f21
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Wed, 11 Nov 2009 14:05:34 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 13 Nov 2009 20:46:24 +0100

mips: Use generic mult/shift factor calculation for clocks

Replace the MIPS functions of mult/shift factor calculation for clock events and clock sources with inline functions which call the generic functions. The minimum guaranteed conversion range is set to 4 seconds which corresponds to the current MIPS implementation.

LKML-Reference: <[email protected]>

---
 arch/mips/include/asm/time.h |   14 +++++++++++---
 arch/mips/kernel/time.c      |   33 ---------------------------------
 2 files changed, 11 insertions(+), 36 deletions(-)
arch/mips/include/asm/time.h +11 -3
index df6a430..c7f1bfe 100644
@@ -84,8 +84,16 @@ static inline int init_mips_clocksource(void)
 #endif
 }
 
-extern void clocksource_set_clock(struct clocksource *cs, unsigned int clock);
-extern void clockevent_set_clock(struct clock_event_device *cd,
-		unsigned int clock);
+static inline void clocksource_set_clock(struct clocksource *cs,
+					 unsigned int clock)
+{
+	clocksource_calc_mult_shift(cs, clock, 4);
+}
+
+static inline void clockevent_set_clock(struct clock_event_device *cd,
+					unsigned int clock)
+{
+	clockevents_calc_mult_shift(cd, clock, 4);
+}
 
 #endif /* _ASM_TIME_H */
arch/mips/kernel/time.c +0 -33
index 1f467d5..fb74974 100644
@@ -71,39 +71,6 @@ EXPORT_SYMBOL(perf_irq);
 
 unsigned int mips_hpt_frequency;
 
-void __init clocksource_set_clock(struct clocksource *cs, unsigned int clock)
-{
-	u64 temp;
-	u32 shift;
-
-	/* Find a shift value */
-	for (shift = 32; shift > 0; shift--) {
-		temp = (u64) NSEC_PER_SEC << shift;
-		do_div(temp, clock);
-		if ((temp >> 32) == 0)
-			break;
-	}
-	cs->shift = shift;
-	cs->mult = (u32) temp;
-}
-
-void __cpuinit clockevent_set_clock(struct clock_event_device *cd,
-	unsigned int clock)
-{
-	u64 temp;
-	u32 shift;
-
-	/* Find a shift value */
-	for (shift = 32; shift > 0; shift--) {
-		temp = (u64) clock << shift;
-		do_div(temp, NSEC_PER_SEC);
-		if ((temp >> 32) == 0)
-			break;
-	}
-	cd->shift = shift;
-	cd->mult = (u32) temp;
-}
-
 /*
  * This function exists in order to cause an error due to a duplicate
  * definition if platform code should have its own implementation.  The hook