lkml.org 
[lkml]   [2009]   [Mar]   [19]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
SubjectRe: [PATCH 08/57] microblaze_v7: Interrupt handling, timer support, selfmod code
From
Date
On Thu, 2009-03-19 at 22:47 +0100, Thomas Gleixner wrote:
> On Thu, 19 Mar 2009, Michal Simek wrote:
> > And the second question is about shift and rating values.
> > I wrote one message in past http://lkml.org/lkml/2009/1/11/291
> > Here is the important of part of that message.
> >
> > ...
> >
> > And the second part is about shift and rating values. Rating is
> > describe(linux/clocksource.h) and seems to me that should be
> > corresponded with CONFIG_HZ value,right?

Not sure where the idea of correspondence w/ CONFIG_HZ came from. The
rating value just provides a relative ordering of preferences between
possible clocksources. Since different hardware may have a number of
different clocksources available, we just need to have a method of
selecting a preferred clocksource, and the rating value is used for
that.
The guide in linux/clocksource.h is just a guide. Most arches, which
only have one or two clocksource options probably won't need much care,
and a rating of 200 or 300 will probably suffice. Or if there really
isn't any option about it and there is only one which is a must-use
clocksource, 400.


> > And I found any explanation of shift value -> max value for equation
> > (2-5) * freq << shift / NSEC_PER_SEC should be for my case still 32bit
> > number, where (2-5s) are because of NTP
>
> @John, can you explain the shift vlaue please ?

The shift value is a bit more difficult to explain. The algorithm you
describe above is used by sparc to generate shift, and I think it will
work, but may not be optimal.

This question comes up over and over, so I figured I should sit down and
really solve it.

Basically the constraint is you want to calculate a mult value using the
highest shift possible. However we have to be careful not to overflow
64bits when we multiply ~5second worth of cycles times the mult value.

So I finally put this down into code and here it is. No promises that it
is 100% right, but from my simple test examples it worked ok.

Let me know if it helps.

thanks
-john


Add helper functions to calculate ideal clocksource shift values given
its frequency (either hz or khz). Lightly tested, use with care.
Also fixes a few old-old-old timesource (well, timsource) references my
find and replace attempts failed on.

Signed-off-by: John Stultz <johnstul@us.ibm.com>

diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index f88d32f..9ef062d 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -116,7 +116,7 @@ extern struct clocksource *clock; /* current clocksource */
* @khz: Clocksource frequency in KHz
* @shift_constant: Clocksource shift factor
*
- * Helper functions that converts a khz counter frequency to a timsource
+ * Helper functions that converts a khz counter frequency to a clocksource
* multiplier, given the clocksource shift value
*/
static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
@@ -142,7 +142,7 @@ static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
* @shift_constant: Clocksource shift factor
*
* Helper functions that converts a hz counter
- * frequency to a timsource multiplier, given the
+ * frequency to a clocksource multiplier, given the
* clocksource shift value
*/
static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
@@ -162,6 +162,10 @@ static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
return (u32)tmp;
}

+extern u32 clocksource_khz2shift(u32 freq_khz);
+extern u32 clocksource_hz2shift(u32 freq_hz);
+
+
/**
* clocksource_read: - Access the clocksource's current cycle value
* @cs: pointer to clocksource being read
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index ca89e15..0a2538d 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -212,6 +212,83 @@ static void clocksource_check_watchdog(struct clocksource *cs)
static inline void clocksource_resume_watchdog(void) { }
#endif

+
+/* Maximum seconds worth of cycles we consider valid */
+#define MAX_SEC_OF_CYCLES 5ULL
+
+/**
+ * clocksource_khz2shift - calculates shift from khz
+ * @freq_khz: Clocksource frequency in KHz
+ *
+ * Helper functions that converts a khz counter
+ * frequency to a clocksource shift.
+ */
+u32 clocksource_khz2shift(u32 freq_khz)
+{
+ s32 shift = 32;
+ u32 mult;
+
+ while (1) {
+ s64 tmp;
+ u64 cycles;
+ mult = clocksource_khz2mult(freq_khz, shift);
+
+ cycles = (u64)freq_khz*1000*MAX_SEC_OF_CYCLES;
+ tmp = (cycles * mult) >> shift;
+
+ tmp = (MAX_SEC_OF_CYCLES*NSEC_PER_SEC) - tmp;
+ if (tmp < 0)
+ tmp = -tmp;
+
+ /* if we are witin one mult unit, we're good */
+ if (tmp < cycles>>shift)
+ break;
+
+ shift--;
+ if (shift < 1)
+ BUG();
+ }
+ return shift;
+}
+
+/**
+ * clocksource_hz2shift - calculates shift from hz
+ * @freq_hz: Clocksource frequency in Hz
+ *
+ * Helper functions that converts a hz counter
+ * frequency to a clocksource shift.
+ */
+
+u32 clocksource_hz2shift(u32 freq_hz)
+{
+ s32 shift = 32;
+ u32 mult;
+
+ while (1) {
+ s64 tmp;
+ u64 cycles;
+ mult = clocksource_hz2mult(freq_hz, shift);
+
+ cycles = (u64)freq_hz*MAX_SEC_OF_CYCLES;
+ tmp = (cycles * mult) >> shift;
+
+ tmp = MAX_SEC_OF_CYCLES*NSEC_PER_SEC - tmp;
+ if (tmp < 0)
+ tmp = -tmp;
+
+ /* if we are witin one mult unit, we're good */
+ if (tmp < cycles>>shift)
+ break;
+
+ shift--;
+ if (shift < 1)
+ BUG();
+ }
+ return shift;
+}
+
+
+
/**
* clocksource_resume - resume the clocksource(s)
*/



\
 
 \ /
  Last update: 2009-03-20 03:35    [from the cache]
©2003-2011 Jasper Spaans