2121 *
2222 * TODO WishList:
2323 * o Allow clocksource drivers to be unregistered
24- * o get rid of clocksource_jiffies extern
2524 */
2625
2726#include <linux/clocksource.h>
@@ -107,12 +106,9 @@ u64 timecounter_cyc2time(struct timecounter *tc,
107106}
108107EXPORT_SYMBOL (timecounter_cyc2time );
109108
110- /* XXX - Would like a better way for initializing curr_clocksource */
111- extern struct clocksource clocksource_jiffies ;
112-
113109/*[Clocksource internal variables]---------
114110 * curr_clocksource:
115- * currently selected clocksource. Initialized to clocksource_jiffies.
111+ * currently selected clocksource.
116112 * next_clocksource:
117113 * pending next selected clocksource.
118114 * clocksource_list:
@@ -123,9 +119,8 @@ extern struct clocksource clocksource_jiffies;
123119 * override_name:
124120 * Name of the user-specified clocksource.
125121 */
126- static struct clocksource * curr_clocksource = & clocksource_jiffies ;
122+ static struct clocksource * curr_clocksource ;
127123static struct clocksource * next_clocksource ;
128- static struct clocksource * clocksource_override ;
129124static LIST_HEAD (clocksource_list );
130125static DEFINE_SPINLOCK (clocksource_lock );
131126static char override_name [32 ];
@@ -320,6 +315,7 @@ void clocksource_touch_watchdog(void)
320315 clocksource_resume_watchdog ();
321316}
322317
318+ #ifdef CONFIG_GENERIC_TIME
323319/**
324320 * clocksource_get_next - Returns the selected clocksource
325321 *
@@ -339,56 +335,65 @@ struct clocksource *clocksource_get_next(void)
339335}
340336
341337/**
342- * select_clocksource - Selects the best registered clocksource.
338+ * clocksource_select - Select the best clocksource available
343339 *
344340 * Private function. Must hold clocksource_lock when called.
345341 *
346342 * Select the clocksource with the best rating, or the clocksource,
347343 * which is selected by userspace override.
348344 */
349- static struct clocksource * select_clocksource (void )
345+ static void clocksource_select (void )
350346{
351- struct clocksource * next ;
347+ struct clocksource * best , * cs ;
352348
353349 if (list_empty (& clocksource_list ))
354- return NULL ;
350+ return ;
351+ /* First clocksource on the list has the best rating. */
352+ best = list_first_entry (& clocksource_list , struct clocksource , list );
353+ /* Check for the override clocksource. */
354+ list_for_each_entry (cs , & clocksource_list , list ) {
355+ if (strcmp (cs -> name , override_name ) != 0 )
356+ continue ;
357+ /*
358+ * Check to make sure we don't switch to a non-highres
359+ * capable clocksource if the tick code is in oneshot
360+ * mode (highres or nohz)
361+ */
362+ if (!(cs -> flags & CLOCK_SOURCE_VALID_FOR_HRES ) &&
363+ tick_oneshot_mode_active ()) {
364+ /* Override clocksource cannot be used. */
365+ printk (KERN_WARNING "Override clocksource %s is not "
366+ "HRT compatible. Cannot switch while in "
367+ "HRT/NOHZ mode\n" , cs -> name );
368+ override_name [0 ] = 0 ;
369+ } else
370+ /* Override clocksource can be used. */
371+ best = cs ;
372+ break ;
373+ }
374+ if (curr_clocksource != best )
375+ next_clocksource = best ;
376+ }
355377
356- if (clocksource_override )
357- next = clocksource_override ;
358- else
359- next = list_entry (clocksource_list .next , struct clocksource ,
360- list );
378+ #else /* CONFIG_GENERIC_TIME */
361379
362- if (next == curr_clocksource )
363- return NULL ;
380+ static void clocksource_select (void ) { }
364381
365- return next ;
366- }
382+ #endif
367383
368384/*
369385 * Enqueue the clocksource sorted by rating
370386 */
371- static int clocksource_enqueue (struct clocksource * c )
387+ static void clocksource_enqueue (struct clocksource * cs )
372388{
373- struct list_head * tmp , * entry = & clocksource_list ;
374-
375- list_for_each (tmp , & clocksource_list ) {
376- struct clocksource * cs ;
389+ struct list_head * entry = & clocksource_list ;
390+ struct clocksource * tmp ;
377391
378- cs = list_entry (tmp , struct clocksource , list );
379- if (cs == c )
380- return - EBUSY ;
392+ list_for_each_entry (tmp , & clocksource_list , list )
381393 /* Keep track of the place, where to insert */
382- if (cs -> rating >= c -> rating )
383- entry = tmp ;
384- }
385- list_add (& c -> list , entry );
386-
387- if (strlen (c -> name ) == strlen (override_name ) &&
388- !strcmp (c -> name , override_name ))
389- clocksource_override = c ;
390-
391- return 0 ;
394+ if (tmp -> rating >= cs -> rating )
395+ entry = & tmp -> list ;
396+ list_add (& cs -> list , entry );
392397}
393398
394399/**
@@ -397,19 +402,16 @@ static int clocksource_enqueue(struct clocksource *c)
397402 *
398403 * Returns -EBUSY if registration fails, zero otherwise.
399404 */
400- int clocksource_register (struct clocksource * c )
405+ int clocksource_register (struct clocksource * cs )
401406{
402407 unsigned long flags ;
403- int ret ;
404408
405409 spin_lock_irqsave (& clocksource_lock , flags );
406- ret = clocksource_enqueue (c );
407- if (!ret )
408- next_clocksource = select_clocksource ();
410+ clocksource_enqueue (cs );
411+ clocksource_select ();
409412 spin_unlock_irqrestore (& clocksource_lock , flags );
410- if (!ret )
411- clocksource_check_watchdog (c );
412- return ret ;
413+ clocksource_check_watchdog (cs );
414+ return 0 ;
413415}
414416EXPORT_SYMBOL (clocksource_register );
415417
@@ -425,7 +427,7 @@ void clocksource_change_rating(struct clocksource *cs, int rating)
425427 list_del (& cs -> list );
426428 cs -> rating = rating ;
427429 clocksource_enqueue (cs );
428- next_clocksource = select_clocksource ();
430+ clocksource_select ();
429431 spin_unlock_irqrestore (& clocksource_lock , flags );
430432}
431433
@@ -438,9 +440,7 @@ void clocksource_unregister(struct clocksource *cs)
438440
439441 spin_lock_irqsave (& clocksource_lock , flags );
440442 list_del (& cs -> list );
441- if (clocksource_override == cs )
442- clocksource_override = NULL ;
443- next_clocksource = select_clocksource ();
443+ clocksource_select ();
444444 spin_unlock_irqrestore (& clocksource_lock , flags );
445445}
446446
@@ -478,9 +478,7 @@ static ssize_t sysfs_override_clocksource(struct sys_device *dev,
478478 struct sysdev_attribute * attr ,
479479 const char * buf , size_t count )
480480{
481- struct clocksource * ovr = NULL ;
482481 size_t ret = count ;
483- int len ;
484482
485483 /* strings from sysfs write are not 0 terminated! */
486484 if (count >= sizeof (override_name ))
@@ -495,37 +493,7 @@ static ssize_t sysfs_override_clocksource(struct sys_device *dev,
495493 if (count > 0 )
496494 memcpy (override_name , buf , count );
497495 override_name [count ] = 0 ;
498-
499- len = strlen (override_name );
500- if (len ) {
501- struct clocksource * cs ;
502-
503- ovr = clocksource_override ;
504- /* try to select it: */
505- list_for_each_entry (cs , & clocksource_list , list ) {
506- if (strlen (cs -> name ) == len &&
507- !strcmp (cs -> name , override_name ))
508- ovr = cs ;
509- }
510- }
511-
512- /*
513- * Check to make sure we don't switch to a non-highres capable
514- * clocksource if the tick code is in oneshot mode (highres or nohz)
515- */
516- if (tick_oneshot_mode_active () && ovr &&
517- !(ovr -> flags & CLOCK_SOURCE_VALID_FOR_HRES )) {
518- printk (KERN_WARNING "%s clocksource is not HRT compatible. "
519- "Cannot switch while in HRT/NOHZ mode\n" , ovr -> name );
520- ovr = NULL ;
521- override_name [0 ] = 0 ;
522- }
523-
524- /* Reselect, when the override name has changed */
525- if (ovr != clocksource_override ) {
526- clocksource_override = ovr ;
527- next_clocksource = select_clocksource ();
528- }
496+ clocksource_select ();
529497
530498 spin_unlock_irq (& clocksource_lock );
531499
0 commit comments