diff options
author | Thomas Gleixner <tglx@linutronix.de> | 2009-11-11 09:05:29 -0500 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2009-11-13 14:46:23 -0500 |
commit | 7d2f944a2b836c69a9d260a0a5f0d1720d57fdff (patch) | |
tree | 67766d878c650ab7ac13d155081b9c745447e24e /kernel | |
parent | 23af368e9a904f59256c27d371ce223d6cee0430 (diff) |
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.
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Mikael Pettersson <mikpe@it.uu.se>
Acked-by: Ralf Baechle <ralf@linux-mips.org>
Acked-by: Linus Walleij <linus.walleij@stericsson.com>
Cc: John Stultz <johnstul@us.ibm.com>
LKML-Reference: <20091111134229.766673305@linutronix.de>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/time/clocksource.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c index 5e18c6ab2c6a..407c0894ef37 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c | |||
@@ -107,6 +107,59 @@ u64 timecounter_cyc2time(struct timecounter *tc, | |||
107 | } | 107 | } |
108 | EXPORT_SYMBOL(timecounter_cyc2time); | 108 | EXPORT_SYMBOL(timecounter_cyc2time); |
109 | 109 | ||
110 | /** | ||
111 | * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks | ||
112 | * @mult: pointer to mult variable | ||
113 | * @shift: pointer to shift variable | ||
114 | * @from: frequency to convert from | ||
115 | * @to: frequency to convert to | ||
116 | * @minsec: guaranteed runtime conversion range in seconds | ||
117 | * | ||
118 | * The function evaluates the shift/mult pair for the scaled math | ||
119 | * operations of clocksources and clockevents. | ||
120 | * | ||
121 | * @to and @from are frequency values in HZ. For clock sources @to is | ||
122 | * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock | ||
123 | * event @to is the counter frequency and @from is NSEC_PER_SEC. | ||
124 | * | ||
125 | * The @minsec conversion range argument controls the time frame in | ||
126 | * seconds which must be covered by the runtime conversion with the | ||
127 | * calculated mult and shift factors. This guarantees that no 64bit | ||
128 | * overflow happens when the input value of the conversion is | ||
129 | * multiplied with the calculated mult factor. Larger ranges may | ||
130 | * reduce the conversion accuracy by chosing smaller mult and shift | ||
131 | * factors. | ||
132 | */ | ||
133 | void | ||
134 | clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec) | ||
135 | { | ||
136 | u64 tmp; | ||
137 | u32 sft, sftacc= 32; | ||
138 | |||
139 | /* | ||
140 | * Calculate the shift factor which is limiting the conversion | ||
141 | * range: | ||
142 | */ | ||
143 | tmp = ((u64)minsec * from) >> 32; | ||
144 | while (tmp) { | ||
145 | tmp >>=1; | ||
146 | sftacc--; | ||
147 | } | ||
148 | |||
149 | /* | ||
150 | * Find the conversion shift/mult pair which has the best | ||
151 | * accuracy and fits the maxsec conversion range: | ||
152 | */ | ||
153 | for (sft = 32; sft > 0; sft--) { | ||
154 | tmp = (u64) to << sft; | ||
155 | do_div(tmp, from); | ||
156 | if ((tmp >> sftacc) == 0) | ||
157 | break; | ||
158 | } | ||
159 | *mult = tmp; | ||
160 | *shift = sft; | ||
161 | } | ||
162 | |||
110 | /*[Clocksource internal variables]--------- | 163 | /*[Clocksource internal variables]--------- |
111 | * curr_clocksource: | 164 | * curr_clocksource: |
112 | * currently selected clocksource. | 165 | * currently selected clocksource. |