Skip to content

Commit d7e81c2

Browse files
John StultzKAGA-KOKO
authored andcommitted
clocksource: Add clocksource_register_hz/khz interface
How to pick good mult/shift pairs has always been difficult to describe to folks writing clocksource drivers, since it requires careful tradeoffs in adjustment accuracy vs overflow limits. Now, with the clocks_calc_mult_shift function, its much easier. However, not many clocksources have converted to using that function, and there is still the issue of the max interval length assumption being made by each clocksource driver independently. So this patch simplifies the registration process by having clocksources be registered with a hz/khz value and the registration function taking care of setting mult/shift. This should take most of the confusion out of writing a clocksource driver. Additionally it also keeps the shift size tradeoff (more accuracy vs longer possible nohz times) centralized so the timekeeping core can keep track of the assumptions being made. [ tglx: Coding style and comments fixed ] Signed-off-by: John Stultz <johnstul@us.ibm.com> LKML-Reference: <1273280858-30143-1-git-send-email-johnstul@us.ibm.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 29f87b7 commit d7e81c2

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎include/linux/clocksource.h‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ static inline s64 clocksource_cyc2ns(cycle_t cycles, u32 mult, u32 shift)
273273
}
274274

275275

276-
/* used to install a new clocksource */
277276
extern int clocksource_register(struct clocksource*);
278277
extern void clocksource_unregister(struct clocksource*);
279278
extern void clocksource_touch_watchdog(void);
@@ -287,6 +286,24 @@ extern void clocksource_mark_unstable(struct clocksource *cs);
287286
extern void
288287
clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
289288

289+
/*
290+
* Don't call __clocksource_register_scale directly, use
291+
* clocksource_register_hz/khz
292+
*/
293+
extern int
294+
__clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
295+
296+
static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
297+
{
298+
return __clocksource_register_scale(cs, 1, hz);
299+
}
300+
301+
static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
302+
{
303+
return __clocksource_register_scale(cs, 1000, khz);
304+
}
305+
306+
290307
static inline void
291308
clocksource_calc_mult_shift(struct clocksource *cs, u32 freq, u32 minsec)
292309
{

‎kernel/time/clocksource.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,54 @@ static void clocksource_enqueue(struct clocksource *cs)
625625
list_add(&cs->list, entry);
626626
}
627627

628+
629+
/*
630+
* Maximum time we expect to go between ticks. This includes idle
631+
* tickless time. It provides the trade off between selecting a
632+
* mult/shift pair that is very precise but can only handle a short
633+
* period of time, vs. a mult/shift pair that can handle long periods
634+
* of time but isn't as precise.
635+
*
636+
* This is a subsystem constant, and actual hardware limitations
637+
* may override it (ie: clocksources that wrap every 3 seconds).
638+
*/
639+
#define MAX_UPDATE_LENGTH 5 /* Seconds */
640+
641+
/**
642+
* __clocksource_register_scale - Used to install new clocksources
643+
* @t: clocksource to be registered
644+
* @scale: Scale factor multiplied against freq to get clocksource hz
645+
* @freq: clocksource frequency (cycles per second) divided by scale
646+
*
647+
* Returns -EBUSY if registration fails, zero otherwise.
648+
*
649+
* This *SHOULD NOT* be called directly! Please use the
650+
* clocksource_register_hz() or clocksource_register_khz helper functions.
651+
*/
652+
int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
653+
{
654+
655+
/*
656+
* Ideally we want to use some of the limits used in
657+
* clocksource_max_deferment, to provide a more informed
658+
* MAX_UPDATE_LENGTH. But for now this just gets the
659+
* register interface working properly.
660+
*/
661+
clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
662+
NSEC_PER_SEC/scale,
663+
MAX_UPDATE_LENGTH*scale);
664+
cs->max_idle_ns = clocksource_max_deferment(cs);
665+
666+
mutex_lock(&clocksource_mutex);
667+
clocksource_enqueue(cs);
668+
clocksource_select();
669+
clocksource_enqueue_watchdog(cs);
670+
mutex_unlock(&clocksource_mutex);
671+
return 0;
672+
}
673+
EXPORT_SYMBOL_GPL(__clocksource_register_scale);
674+
675+
628676
/**
629677
* clocksource_register - Used to install new clocksources
630678
* @t: clocksource to be registered

0 commit comments

Comments
 (0)