1906568c9SThomas Gleixner /* 2906568c9SThomas Gleixner * linux/kernel/time/tick-common.c 3906568c9SThomas Gleixner * 4906568c9SThomas Gleixner * This file contains the base functions to manage periodic tick 5906568c9SThomas Gleixner * related events. 6906568c9SThomas Gleixner * 7906568c9SThomas Gleixner * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> 8906568c9SThomas Gleixner * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar 9906568c9SThomas Gleixner * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner 10906568c9SThomas Gleixner * 11906568c9SThomas Gleixner * This code is licenced under the GPL version 2. For details see 12906568c9SThomas Gleixner * kernel-base/COPYING. 13906568c9SThomas Gleixner */ 14906568c9SThomas Gleixner #include <linux/cpu.h> 15906568c9SThomas Gleixner #include <linux/err.h> 16906568c9SThomas Gleixner #include <linux/hrtimer.h> 17d7b90689SRussell King #include <linux/interrupt.h> 18906568c9SThomas Gleixner #include <linux/percpu.h> 19906568c9SThomas Gleixner #include <linux/profile.h> 20906568c9SThomas Gleixner #include <linux/sched.h> 21906568c9SThomas Gleixner #include <linux/tick.h> 22906568c9SThomas Gleixner 23d7b90689SRussell King #include <asm/irq_regs.h> 24d7b90689SRussell King 25f8381cbaSThomas Gleixner #include "tick-internal.h" 26f8381cbaSThomas Gleixner 27906568c9SThomas Gleixner /* 28906568c9SThomas Gleixner * Tick devices 29906568c9SThomas Gleixner */ 30f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device); 31906568c9SThomas Gleixner /* 32906568c9SThomas Gleixner * Tick next event: keeps track of the tick time 33906568c9SThomas Gleixner */ 34f8381cbaSThomas Gleixner ktime_t tick_next_period; 35f8381cbaSThomas Gleixner ktime_t tick_period; 366441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT; 37f8381cbaSThomas Gleixner DEFINE_SPINLOCK(tick_device_lock); 38906568c9SThomas Gleixner 39289f480aSIngo Molnar /* 40289f480aSIngo Molnar * Debugging: see timer_list.c 41289f480aSIngo Molnar */ 42289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu) 43289f480aSIngo Molnar { 44289f480aSIngo Molnar return &per_cpu(tick_cpu_device, cpu); 45289f480aSIngo Molnar } 46289f480aSIngo Molnar 4779bf2bb3SThomas Gleixner /** 4879bf2bb3SThomas Gleixner * tick_is_oneshot_available - check for a oneshot capable event device 4979bf2bb3SThomas Gleixner */ 5079bf2bb3SThomas Gleixner int tick_is_oneshot_available(void) 5179bf2bb3SThomas Gleixner { 5279bf2bb3SThomas Gleixner struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; 5379bf2bb3SThomas Gleixner 5479bf2bb3SThomas Gleixner return dev && (dev->features & CLOCK_EVT_FEAT_ONESHOT); 5579bf2bb3SThomas Gleixner } 5679bf2bb3SThomas Gleixner 57906568c9SThomas Gleixner /* 58906568c9SThomas Gleixner * Periodic tick 59906568c9SThomas Gleixner */ 60906568c9SThomas Gleixner static void tick_periodic(int cpu) 61906568c9SThomas Gleixner { 62906568c9SThomas Gleixner if (tick_do_timer_cpu == cpu) { 63906568c9SThomas Gleixner write_seqlock(&xtime_lock); 64906568c9SThomas Gleixner 65906568c9SThomas Gleixner /* Keep track of the next tick event */ 66906568c9SThomas Gleixner tick_next_period = ktime_add(tick_next_period, tick_period); 67906568c9SThomas Gleixner 68906568c9SThomas Gleixner do_timer(1); 69906568c9SThomas Gleixner write_sequnlock(&xtime_lock); 70906568c9SThomas Gleixner } 71906568c9SThomas Gleixner 72906568c9SThomas Gleixner update_process_times(user_mode(get_irq_regs())); 73906568c9SThomas Gleixner profile_tick(CPU_PROFILING); 74906568c9SThomas Gleixner } 75906568c9SThomas Gleixner 76906568c9SThomas Gleixner /* 77906568c9SThomas Gleixner * Event handler for periodic ticks 78906568c9SThomas Gleixner */ 79906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev) 80906568c9SThomas Gleixner { 81906568c9SThomas Gleixner int cpu = smp_processor_id(); 823494c166SDavid S. Miller ktime_t next; 83906568c9SThomas Gleixner 84906568c9SThomas Gleixner tick_periodic(cpu); 85906568c9SThomas Gleixner 86906568c9SThomas Gleixner if (dev->mode != CLOCK_EVT_MODE_ONESHOT) 87906568c9SThomas Gleixner return; 88906568c9SThomas Gleixner /* 89906568c9SThomas Gleixner * Setup the next period for devices, which do not have 90906568c9SThomas Gleixner * periodic mode: 91906568c9SThomas Gleixner */ 923494c166SDavid S. Miller next = ktime_add(dev->next_event, tick_period); 93906568c9SThomas Gleixner for (;;) { 94906568c9SThomas Gleixner if (!clockevents_program_event(dev, next, ktime_get())) 95906568c9SThomas Gleixner return; 96906568c9SThomas Gleixner tick_periodic(cpu); 973494c166SDavid S. Miller next = ktime_add(next, tick_period); 98906568c9SThomas Gleixner } 99906568c9SThomas Gleixner } 100906568c9SThomas Gleixner 101906568c9SThomas Gleixner /* 102906568c9SThomas Gleixner * Setup the device for a periodic tick 103906568c9SThomas Gleixner */ 104f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast) 105906568c9SThomas Gleixner { 106f8381cbaSThomas Gleixner tick_set_periodic_handler(dev, broadcast); 107f8381cbaSThomas Gleixner 108f8381cbaSThomas Gleixner /* Broadcast setup ? */ 109f8381cbaSThomas Gleixner if (!tick_device_is_functional(dev)) 110f8381cbaSThomas Gleixner return; 111906568c9SThomas Gleixner 11227ce4cb4SThomas Gleixner if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) && 11327ce4cb4SThomas Gleixner !tick_broadcast_oneshot_active()) { 114906568c9SThomas Gleixner clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC); 115906568c9SThomas Gleixner } else { 116906568c9SThomas Gleixner unsigned long seq; 117906568c9SThomas Gleixner ktime_t next; 118906568c9SThomas Gleixner 119906568c9SThomas Gleixner do { 120906568c9SThomas Gleixner seq = read_seqbegin(&xtime_lock); 121906568c9SThomas Gleixner next = tick_next_period; 122906568c9SThomas Gleixner } while (read_seqretry(&xtime_lock, seq)); 123906568c9SThomas Gleixner 124906568c9SThomas Gleixner clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); 125906568c9SThomas Gleixner 126906568c9SThomas Gleixner for (;;) { 127906568c9SThomas Gleixner if (!clockevents_program_event(dev, next, ktime_get())) 128906568c9SThomas Gleixner return; 129906568c9SThomas Gleixner next = ktime_add(next, tick_period); 130906568c9SThomas Gleixner } 131906568c9SThomas Gleixner } 132906568c9SThomas Gleixner } 133906568c9SThomas Gleixner 134906568c9SThomas Gleixner /* 135906568c9SThomas Gleixner * Setup the tick device 136906568c9SThomas Gleixner */ 137906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td, 138906568c9SThomas Gleixner struct clock_event_device *newdev, int cpu, 1390de26520SRusty Russell const struct cpumask *cpumask) 140906568c9SThomas Gleixner { 141906568c9SThomas Gleixner ktime_t next_event; 142906568c9SThomas Gleixner void (*handler)(struct clock_event_device *) = NULL; 143906568c9SThomas Gleixner 144906568c9SThomas Gleixner /* 145906568c9SThomas Gleixner * First device setup ? 146906568c9SThomas Gleixner */ 147906568c9SThomas Gleixner if (!td->evtdev) { 148906568c9SThomas Gleixner /* 149906568c9SThomas Gleixner * If no cpu took the do_timer update, assign it to 150906568c9SThomas Gleixner * this cpu: 151906568c9SThomas Gleixner */ 1526441402bSThomas Gleixner if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) { 153906568c9SThomas Gleixner tick_do_timer_cpu = cpu; 154906568c9SThomas Gleixner tick_next_period = ktime_get(); 155906568c9SThomas Gleixner tick_period = ktime_set(0, NSEC_PER_SEC / HZ); 156906568c9SThomas Gleixner } 157906568c9SThomas Gleixner 158906568c9SThomas Gleixner /* 159906568c9SThomas Gleixner * Startup in periodic mode first. 160906568c9SThomas Gleixner */ 161906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 162906568c9SThomas Gleixner } else { 163906568c9SThomas Gleixner handler = td->evtdev->event_handler; 164906568c9SThomas Gleixner next_event = td->evtdev->next_event; 1657c1e7689SVenkatesh Pallipadi td->evtdev->event_handler = clockevents_handle_noop; 166906568c9SThomas Gleixner } 167906568c9SThomas Gleixner 168906568c9SThomas Gleixner td->evtdev = newdev; 169906568c9SThomas Gleixner 170906568c9SThomas Gleixner /* 171906568c9SThomas Gleixner * When the device is not per cpu, pin the interrupt to the 172906568c9SThomas Gleixner * current cpu: 173906568c9SThomas Gleixner */ 174320ab2b0SRusty Russell if (!cpumask_equal(newdev->cpumask, cpumask)) 1750de26520SRusty Russell irq_set_affinity(newdev->irq, cpumask); 176906568c9SThomas Gleixner 177f8381cbaSThomas Gleixner /* 178f8381cbaSThomas Gleixner * When global broadcasting is active, check if the current 179f8381cbaSThomas Gleixner * device is registered as a placeholder for broadcast mode. 180f8381cbaSThomas Gleixner * This allows us to handle this x86 misfeature in a generic 181f8381cbaSThomas Gleixner * way. 182f8381cbaSThomas Gleixner */ 183f8381cbaSThomas Gleixner if (tick_device_uses_broadcast(newdev, cpu)) 184f8381cbaSThomas Gleixner return; 185f8381cbaSThomas Gleixner 186906568c9SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 187906568c9SThomas Gleixner tick_setup_periodic(newdev, 0); 18879bf2bb3SThomas Gleixner else 18979bf2bb3SThomas Gleixner tick_setup_oneshot(newdev, handler, next_event); 190906568c9SThomas Gleixner } 191906568c9SThomas Gleixner 192906568c9SThomas Gleixner /* 193906568c9SThomas Gleixner * Check, if the new registered device should be used. 194906568c9SThomas Gleixner */ 195906568c9SThomas Gleixner static int tick_check_new_device(struct clock_event_device *newdev) 196906568c9SThomas Gleixner { 197906568c9SThomas Gleixner struct clock_event_device *curdev; 198906568c9SThomas Gleixner struct tick_device *td; 199906568c9SThomas Gleixner int cpu, ret = NOTIFY_OK; 200906568c9SThomas Gleixner unsigned long flags; 201906568c9SThomas Gleixner 202906568c9SThomas Gleixner spin_lock_irqsave(&tick_device_lock, flags); 203906568c9SThomas Gleixner 204906568c9SThomas Gleixner cpu = smp_processor_id(); 205320ab2b0SRusty Russell if (!cpumask_test_cpu(cpu, newdev->cpumask)) 2064a93232dSVenki Pallipadi goto out_bc; 207906568c9SThomas Gleixner 208906568c9SThomas Gleixner td = &per_cpu(tick_cpu_device, cpu); 209906568c9SThomas Gleixner curdev = td->evtdev; 210906568c9SThomas Gleixner 211906568c9SThomas Gleixner /* cpu local device ? */ 212320ab2b0SRusty Russell if (!cpumask_equal(newdev->cpumask, cpumask_of(cpu))) { 213906568c9SThomas Gleixner 214906568c9SThomas Gleixner /* 215906568c9SThomas Gleixner * If the cpu affinity of the device interrupt can not 216906568c9SThomas Gleixner * be set, ignore it. 217906568c9SThomas Gleixner */ 218906568c9SThomas Gleixner if (!irq_can_set_affinity(newdev->irq)) 219906568c9SThomas Gleixner goto out_bc; 220906568c9SThomas Gleixner 221906568c9SThomas Gleixner /* 222906568c9SThomas Gleixner * If we have a cpu local device already, do not replace it 223906568c9SThomas Gleixner * by a non cpu local device 224906568c9SThomas Gleixner */ 225320ab2b0SRusty Russell if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu))) 226906568c9SThomas Gleixner goto out_bc; 227906568c9SThomas Gleixner } 228906568c9SThomas Gleixner 229906568c9SThomas Gleixner /* 230906568c9SThomas Gleixner * If we have an active device, then check the rating and the oneshot 231906568c9SThomas Gleixner * feature. 232906568c9SThomas Gleixner */ 233906568c9SThomas Gleixner if (curdev) { 234906568c9SThomas Gleixner /* 23579bf2bb3SThomas Gleixner * Prefer one shot capable devices ! 23679bf2bb3SThomas Gleixner */ 23779bf2bb3SThomas Gleixner if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) && 23879bf2bb3SThomas Gleixner !(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) 23979bf2bb3SThomas Gleixner goto out_bc; 24079bf2bb3SThomas Gleixner /* 241906568c9SThomas Gleixner * Check the rating 242906568c9SThomas Gleixner */ 243906568c9SThomas Gleixner if (curdev->rating >= newdev->rating) 244f8381cbaSThomas Gleixner goto out_bc; 245906568c9SThomas Gleixner } 246906568c9SThomas Gleixner 247906568c9SThomas Gleixner /* 248906568c9SThomas Gleixner * Replace the eventually existing device by the new 249f8381cbaSThomas Gleixner * device. If the current device is the broadcast device, do 250f8381cbaSThomas Gleixner * not give it back to the clockevents layer ! 251906568c9SThomas Gleixner */ 252f8381cbaSThomas Gleixner if (tick_is_broadcast_device(curdev)) { 2532344abbcSThomas Gleixner clockevents_shutdown(curdev); 254f8381cbaSThomas Gleixner curdev = NULL; 255f8381cbaSThomas Gleixner } 256906568c9SThomas Gleixner clockevents_exchange_device(curdev, newdev); 257*6b954823SRusty Russell tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 25879bf2bb3SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 25979bf2bb3SThomas Gleixner tick_oneshot_notify(); 260906568c9SThomas Gleixner 261f8381cbaSThomas Gleixner spin_unlock_irqrestore(&tick_device_lock, flags); 262f8381cbaSThomas Gleixner return NOTIFY_STOP; 263f8381cbaSThomas Gleixner 264f8381cbaSThomas Gleixner out_bc: 265f8381cbaSThomas Gleixner /* 266f8381cbaSThomas Gleixner * Can the new device be used as a broadcast device ? 267f8381cbaSThomas Gleixner */ 268f8381cbaSThomas Gleixner if (tick_check_broadcast_device(newdev)) 269f8381cbaSThomas Gleixner ret = NOTIFY_STOP; 2704a93232dSVenki Pallipadi 271906568c9SThomas Gleixner spin_unlock_irqrestore(&tick_device_lock, flags); 272f8381cbaSThomas Gleixner 273906568c9SThomas Gleixner return ret; 274906568c9SThomas Gleixner } 275906568c9SThomas Gleixner 276906568c9SThomas Gleixner /* 277906568c9SThomas Gleixner * Shutdown an event device on a given cpu: 278906568c9SThomas Gleixner * 279906568c9SThomas Gleixner * This is called on a life CPU, when a CPU is dead. So we cannot 280906568c9SThomas Gleixner * access the hardware device itself. 281906568c9SThomas Gleixner * We just set the mode and remove it from the lists. 282906568c9SThomas Gleixner */ 283906568c9SThomas Gleixner static void tick_shutdown(unsigned int *cpup) 284906568c9SThomas Gleixner { 285906568c9SThomas Gleixner struct tick_device *td = &per_cpu(tick_cpu_device, *cpup); 286906568c9SThomas Gleixner struct clock_event_device *dev = td->evtdev; 287906568c9SThomas Gleixner unsigned long flags; 288906568c9SThomas Gleixner 289906568c9SThomas Gleixner spin_lock_irqsave(&tick_device_lock, flags); 290906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 291906568c9SThomas Gleixner if (dev) { 292906568c9SThomas Gleixner /* 293906568c9SThomas Gleixner * Prevent that the clock events layer tries to call 294906568c9SThomas Gleixner * the set mode function! 295906568c9SThomas Gleixner */ 296906568c9SThomas Gleixner dev->mode = CLOCK_EVT_MODE_UNUSED; 297906568c9SThomas Gleixner clockevents_exchange_device(dev, NULL); 298906568c9SThomas Gleixner td->evtdev = NULL; 299906568c9SThomas Gleixner } 300d3ed7824SThomas Gleixner /* Transfer the do_timer job away from this cpu */ 301d3ed7824SThomas Gleixner if (*cpup == tick_do_timer_cpu) { 302*6b954823SRusty Russell int cpu = cpumask_first(cpu_online_mask); 303d3ed7824SThomas Gleixner 304*6b954823SRusty Russell tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu : 3056441402bSThomas Gleixner TICK_DO_TIMER_NONE; 306d3ed7824SThomas Gleixner } 307906568c9SThomas Gleixner spin_unlock_irqrestore(&tick_device_lock, flags); 308906568c9SThomas Gleixner } 309906568c9SThomas Gleixner 310cd05a1f8SThomas Gleixner static void tick_suspend(void) 3116321dd60SThomas Gleixner { 3126321dd60SThomas Gleixner struct tick_device *td = &__get_cpu_var(tick_cpu_device); 3136321dd60SThomas Gleixner unsigned long flags; 3146321dd60SThomas Gleixner 3156321dd60SThomas Gleixner spin_lock_irqsave(&tick_device_lock, flags); 3162344abbcSThomas Gleixner clockevents_shutdown(td->evtdev); 3176321dd60SThomas Gleixner spin_unlock_irqrestore(&tick_device_lock, flags); 3186321dd60SThomas Gleixner } 3196321dd60SThomas Gleixner 320cd05a1f8SThomas Gleixner static void tick_resume(void) 3216321dd60SThomas Gleixner { 3226321dd60SThomas Gleixner struct tick_device *td = &__get_cpu_var(tick_cpu_device); 3236321dd60SThomas Gleixner unsigned long flags; 32418de5bc4SThomas Gleixner int broadcast = tick_resume_broadcast(); 3256321dd60SThomas Gleixner 3266321dd60SThomas Gleixner spin_lock_irqsave(&tick_device_lock, flags); 32718de5bc4SThomas Gleixner clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_RESUME); 32818de5bc4SThomas Gleixner 32918de5bc4SThomas Gleixner if (!broadcast) { 3306321dd60SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 3316321dd60SThomas Gleixner tick_setup_periodic(td->evtdev, 0); 332cd05a1f8SThomas Gleixner else 333cd05a1f8SThomas Gleixner tick_resume_oneshot(); 33418de5bc4SThomas Gleixner } 3356321dd60SThomas Gleixner spin_unlock_irqrestore(&tick_device_lock, flags); 3366321dd60SThomas Gleixner } 3376321dd60SThomas Gleixner 338906568c9SThomas Gleixner /* 339906568c9SThomas Gleixner * Notification about clock event devices 340906568c9SThomas Gleixner */ 341906568c9SThomas Gleixner static int tick_notify(struct notifier_block *nb, unsigned long reason, 342906568c9SThomas Gleixner void *dev) 343906568c9SThomas Gleixner { 344906568c9SThomas Gleixner switch (reason) { 345906568c9SThomas Gleixner 346906568c9SThomas Gleixner case CLOCK_EVT_NOTIFY_ADD: 347906568c9SThomas Gleixner return tick_check_new_device(dev); 348906568c9SThomas Gleixner 349f8381cbaSThomas Gleixner case CLOCK_EVT_NOTIFY_BROADCAST_ON: 350f8381cbaSThomas Gleixner case CLOCK_EVT_NOTIFY_BROADCAST_OFF: 3511595f452SThomas Gleixner case CLOCK_EVT_NOTIFY_BROADCAST_FORCE: 352f8381cbaSThomas Gleixner tick_broadcast_on_off(reason, dev); 353f8381cbaSThomas Gleixner break; 354f8381cbaSThomas Gleixner 35579bf2bb3SThomas Gleixner case CLOCK_EVT_NOTIFY_BROADCAST_ENTER: 35679bf2bb3SThomas Gleixner case CLOCK_EVT_NOTIFY_BROADCAST_EXIT: 35779bf2bb3SThomas Gleixner tick_broadcast_oneshot_control(reason); 35879bf2bb3SThomas Gleixner break; 35979bf2bb3SThomas Gleixner 360906568c9SThomas Gleixner case CLOCK_EVT_NOTIFY_CPU_DEAD: 36179bf2bb3SThomas Gleixner tick_shutdown_broadcast_oneshot(dev); 362f8381cbaSThomas Gleixner tick_shutdown_broadcast(dev); 363906568c9SThomas Gleixner tick_shutdown(dev); 364906568c9SThomas Gleixner break; 365906568c9SThomas Gleixner 3666321dd60SThomas Gleixner case CLOCK_EVT_NOTIFY_SUSPEND: 367cd05a1f8SThomas Gleixner tick_suspend(); 3686321dd60SThomas Gleixner tick_suspend_broadcast(); 3696321dd60SThomas Gleixner break; 3706321dd60SThomas Gleixner 3716321dd60SThomas Gleixner case CLOCK_EVT_NOTIFY_RESUME: 372cd05a1f8SThomas Gleixner tick_resume(); 3736321dd60SThomas Gleixner break; 3746321dd60SThomas Gleixner 375906568c9SThomas Gleixner default: 376906568c9SThomas Gleixner break; 377906568c9SThomas Gleixner } 378906568c9SThomas Gleixner 379906568c9SThomas Gleixner return NOTIFY_OK; 380906568c9SThomas Gleixner } 381906568c9SThomas Gleixner 382906568c9SThomas Gleixner static struct notifier_block tick_notifier = { 383906568c9SThomas Gleixner .notifier_call = tick_notify, 384906568c9SThomas Gleixner }; 385906568c9SThomas Gleixner 386906568c9SThomas Gleixner /** 387906568c9SThomas Gleixner * tick_init - initialize the tick control 388906568c9SThomas Gleixner * 389906568c9SThomas Gleixner * Register the notifier with the clockevents framework 390906568c9SThomas Gleixner */ 391906568c9SThomas Gleixner void __init tick_init(void) 392906568c9SThomas Gleixner { 393906568c9SThomas Gleixner clockevents_register_notifier(&tick_notifier); 394906568c9SThomas Gleixner } 395