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> 21ccf33d68SThomas Gleixner #include <linux/module.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; 36050ded1bSAndrew Morton 37050ded1bSAndrew Morton /* 38050ded1bSAndrew Morton * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR 39050ded1bSAndrew Morton * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This 40050ded1bSAndrew Morton * variable has two functions: 41050ded1bSAndrew Morton * 42050ded1bSAndrew Morton * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the 43050ded1bSAndrew Morton * timekeeping lock all at once. Only the CPU which is assigned to do the 44050ded1bSAndrew Morton * update is handling it. 45050ded1bSAndrew Morton * 46050ded1bSAndrew Morton * 2) Hand off the duty in the NOHZ idle case by setting the value to 47050ded1bSAndrew Morton * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks 48050ded1bSAndrew Morton * at it will take over and keep the time keeping alive. The handover 49050ded1bSAndrew Morton * procedure also covers cpu hotplug. 50050ded1bSAndrew Morton */ 516441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT; 52906568c9SThomas Gleixner 53289f480aSIngo Molnar /* 54289f480aSIngo Molnar * Debugging: see timer_list.c 55289f480aSIngo Molnar */ 56289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu) 57289f480aSIngo Molnar { 58289f480aSIngo Molnar return &per_cpu(tick_cpu_device, cpu); 59289f480aSIngo Molnar } 60289f480aSIngo Molnar 6179bf2bb3SThomas Gleixner /** 6279bf2bb3SThomas Gleixner * tick_is_oneshot_available - check for a oneshot capable event device 6379bf2bb3SThomas Gleixner */ 6479bf2bb3SThomas Gleixner int tick_is_oneshot_available(void) 6579bf2bb3SThomas Gleixner { 66909ea964SChristoph Lameter struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev); 6779bf2bb3SThomas Gleixner 683a142a06SThomas Gleixner if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT)) 693a142a06SThomas Gleixner return 0; 703a142a06SThomas Gleixner if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) 713a142a06SThomas Gleixner return 1; 723a142a06SThomas Gleixner return tick_broadcast_oneshot_available(); 7379bf2bb3SThomas Gleixner } 7479bf2bb3SThomas Gleixner 75906568c9SThomas Gleixner /* 76906568c9SThomas Gleixner * Periodic tick 77906568c9SThomas Gleixner */ 78906568c9SThomas Gleixner static void tick_periodic(int cpu) 79906568c9SThomas Gleixner { 80906568c9SThomas Gleixner if (tick_do_timer_cpu == cpu) { 81d6ad4187SJohn Stultz write_seqlock(&jiffies_lock); 82906568c9SThomas Gleixner 83906568c9SThomas Gleixner /* Keep track of the next tick event */ 84906568c9SThomas Gleixner tick_next_period = ktime_add(tick_next_period, tick_period); 85906568c9SThomas Gleixner 86906568c9SThomas Gleixner do_timer(1); 87d6ad4187SJohn Stultz write_sequnlock(&jiffies_lock); 8847a1b796SJohn Stultz update_wall_time(); 89906568c9SThomas Gleixner } 90906568c9SThomas Gleixner 91906568c9SThomas Gleixner update_process_times(user_mode(get_irq_regs())); 92906568c9SThomas Gleixner profile_tick(CPU_PROFILING); 93906568c9SThomas Gleixner } 94906568c9SThomas Gleixner 95906568c9SThomas Gleixner /* 96906568c9SThomas Gleixner * Event handler for periodic ticks 97906568c9SThomas Gleixner */ 98906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev) 99906568c9SThomas Gleixner { 100906568c9SThomas Gleixner int cpu = smp_processor_id(); 101b97f0291SViresh Kumar ktime_t next = dev->next_event; 102906568c9SThomas Gleixner 103906568c9SThomas Gleixner tick_periodic(cpu); 104906568c9SThomas Gleixner 105c6eb3f70SThomas Gleixner #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON) 106c6eb3f70SThomas Gleixner /* 107c6eb3f70SThomas Gleixner * The cpu might have transitioned to HIGHRES or NOHZ mode via 108c6eb3f70SThomas Gleixner * update_process_times() -> run_local_timers() -> 109c6eb3f70SThomas Gleixner * hrtimer_run_queues(). 110c6eb3f70SThomas Gleixner */ 111c6eb3f70SThomas Gleixner if (dev->event_handler != tick_handle_periodic) 112c6eb3f70SThomas Gleixner return; 113c6eb3f70SThomas Gleixner #endif 114c6eb3f70SThomas Gleixner 115*472c4a94SViresh Kumar if (!clockevent_state_oneshot(dev)) 116906568c9SThomas Gleixner return; 117b97f0291SViresh Kumar for (;;) { 118906568c9SThomas Gleixner /* 119906568c9SThomas Gleixner * Setup the next period for devices, which do not have 120906568c9SThomas Gleixner * periodic mode: 121906568c9SThomas Gleixner */ 122b97f0291SViresh Kumar next = ktime_add(next, tick_period); 123b97f0291SViresh Kumar 124d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false)) 125906568c9SThomas Gleixner return; 12674a03b69Sjohn stultz /* 12774a03b69Sjohn stultz * Have to be careful here. If we're in oneshot mode, 12874a03b69Sjohn stultz * before we call tick_periodic() in a loop, we need 12974a03b69Sjohn stultz * to be sure we're using a real hardware clocksource. 13074a03b69Sjohn stultz * Otherwise we could get trapped in an infinite 13174a03b69Sjohn stultz * loop, as the tick_periodic() increments jiffies, 132cacb3c76SViresh Kumar * which then will increment time, possibly causing 13374a03b69Sjohn stultz * the loop to trigger again and again. 13474a03b69Sjohn stultz */ 13574a03b69Sjohn stultz if (timekeeping_valid_for_hres()) 136906568c9SThomas Gleixner tick_periodic(cpu); 137906568c9SThomas Gleixner } 138906568c9SThomas Gleixner } 139906568c9SThomas Gleixner 140906568c9SThomas Gleixner /* 141906568c9SThomas Gleixner * Setup the device for a periodic tick 142906568c9SThomas Gleixner */ 143f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast) 144906568c9SThomas Gleixner { 145f8381cbaSThomas Gleixner tick_set_periodic_handler(dev, broadcast); 146f8381cbaSThomas Gleixner 147f8381cbaSThomas Gleixner /* Broadcast setup ? */ 148f8381cbaSThomas Gleixner if (!tick_device_is_functional(dev)) 149f8381cbaSThomas Gleixner return; 150906568c9SThomas Gleixner 15127ce4cb4SThomas Gleixner if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) && 15227ce4cb4SThomas Gleixner !tick_broadcast_oneshot_active()) { 15377e32c89SViresh Kumar clockevents_set_state(dev, CLOCK_EVT_STATE_PERIODIC); 154906568c9SThomas Gleixner } else { 155906568c9SThomas Gleixner unsigned long seq; 156906568c9SThomas Gleixner ktime_t next; 157906568c9SThomas Gleixner 158906568c9SThomas Gleixner do { 159d6ad4187SJohn Stultz seq = read_seqbegin(&jiffies_lock); 160906568c9SThomas Gleixner next = tick_next_period; 161d6ad4187SJohn Stultz } while (read_seqretry(&jiffies_lock, seq)); 162906568c9SThomas Gleixner 16377e32c89SViresh Kumar clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT); 164906568c9SThomas Gleixner 165906568c9SThomas Gleixner for (;;) { 166d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false)) 167906568c9SThomas Gleixner return; 168906568c9SThomas Gleixner next = ktime_add(next, tick_period); 169906568c9SThomas Gleixner } 170906568c9SThomas Gleixner } 171906568c9SThomas Gleixner } 172906568c9SThomas Gleixner 173906568c9SThomas Gleixner /* 174906568c9SThomas Gleixner * Setup the tick device 175906568c9SThomas Gleixner */ 176906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td, 177906568c9SThomas Gleixner struct clock_event_device *newdev, int cpu, 1780de26520SRusty Russell const struct cpumask *cpumask) 179906568c9SThomas Gleixner { 180906568c9SThomas Gleixner ktime_t next_event; 181906568c9SThomas Gleixner void (*handler)(struct clock_event_device *) = NULL; 182906568c9SThomas Gleixner 183906568c9SThomas Gleixner /* 184906568c9SThomas Gleixner * First device setup ? 185906568c9SThomas Gleixner */ 186906568c9SThomas Gleixner if (!td->evtdev) { 187906568c9SThomas Gleixner /* 188906568c9SThomas Gleixner * If no cpu took the do_timer update, assign it to 189906568c9SThomas Gleixner * this cpu: 190906568c9SThomas Gleixner */ 1916441402bSThomas Gleixner if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) { 192c5bfece2SFrederic Weisbecker if (!tick_nohz_full_cpu(cpu)) 193906568c9SThomas Gleixner tick_do_timer_cpu = cpu; 194a382bf93SFrederic Weisbecker else 195a382bf93SFrederic Weisbecker tick_do_timer_cpu = TICK_DO_TIMER_NONE; 196906568c9SThomas Gleixner tick_next_period = ktime_get(); 197906568c9SThomas Gleixner tick_period = ktime_set(0, NSEC_PER_SEC / HZ); 198906568c9SThomas Gleixner } 199906568c9SThomas Gleixner 200906568c9SThomas Gleixner /* 201906568c9SThomas Gleixner * Startup in periodic mode first. 202906568c9SThomas Gleixner */ 203906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 204906568c9SThomas Gleixner } else { 205906568c9SThomas Gleixner handler = td->evtdev->event_handler; 206906568c9SThomas Gleixner next_event = td->evtdev->next_event; 2077c1e7689SVenkatesh Pallipadi td->evtdev->event_handler = clockevents_handle_noop; 208906568c9SThomas Gleixner } 209906568c9SThomas Gleixner 210906568c9SThomas Gleixner td->evtdev = newdev; 211906568c9SThomas Gleixner 212906568c9SThomas Gleixner /* 213906568c9SThomas Gleixner * When the device is not per cpu, pin the interrupt to the 214906568c9SThomas Gleixner * current cpu: 215906568c9SThomas Gleixner */ 216320ab2b0SRusty Russell if (!cpumask_equal(newdev->cpumask, cpumask)) 2170de26520SRusty Russell irq_set_affinity(newdev->irq, cpumask); 218906568c9SThomas Gleixner 219f8381cbaSThomas Gleixner /* 220f8381cbaSThomas Gleixner * When global broadcasting is active, check if the current 221f8381cbaSThomas Gleixner * device is registered as a placeholder for broadcast mode. 222f8381cbaSThomas Gleixner * This allows us to handle this x86 misfeature in a generic 22307bd1172SThomas Gleixner * way. This function also returns !=0 when we keep the 22407bd1172SThomas Gleixner * current active broadcast state for this CPU. 225f8381cbaSThomas Gleixner */ 226f8381cbaSThomas Gleixner if (tick_device_uses_broadcast(newdev, cpu)) 227f8381cbaSThomas Gleixner return; 228f8381cbaSThomas Gleixner 229906568c9SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 230906568c9SThomas Gleixner tick_setup_periodic(newdev, 0); 23179bf2bb3SThomas Gleixner else 23279bf2bb3SThomas Gleixner tick_setup_oneshot(newdev, handler, next_event); 233906568c9SThomas Gleixner } 234906568c9SThomas Gleixner 23503e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev) 23603e13cf5SThomas Gleixner { 23722127e93SChristoph Lameter struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 23803e13cf5SThomas Gleixner int cpu = smp_processor_id(); 23903e13cf5SThomas Gleixner 24003e13cf5SThomas Gleixner clockevents_exchange_device(td->evtdev, newdev); 24103e13cf5SThomas Gleixner tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 24203e13cf5SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 24303e13cf5SThomas Gleixner tick_oneshot_notify(); 24403e13cf5SThomas Gleixner } 24503e13cf5SThomas Gleixner 24645cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev, 24745cb8e01SThomas Gleixner struct clock_event_device *newdev, int cpu) 24845cb8e01SThomas Gleixner { 24945cb8e01SThomas Gleixner if (!cpumask_test_cpu(cpu, newdev->cpumask)) 25045cb8e01SThomas Gleixner return false; 25145cb8e01SThomas Gleixner if (cpumask_equal(newdev->cpumask, cpumask_of(cpu))) 25245cb8e01SThomas Gleixner return true; 25345cb8e01SThomas Gleixner /* Check if irq affinity can be set */ 25445cb8e01SThomas Gleixner if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq)) 25545cb8e01SThomas Gleixner return false; 25645cb8e01SThomas Gleixner /* Prefer an existing cpu local device */ 25745cb8e01SThomas Gleixner if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu))) 25845cb8e01SThomas Gleixner return false; 25945cb8e01SThomas Gleixner return true; 26045cb8e01SThomas Gleixner } 26145cb8e01SThomas Gleixner 26245cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev, 26345cb8e01SThomas Gleixner struct clock_event_device *newdev) 26445cb8e01SThomas Gleixner { 26545cb8e01SThomas Gleixner /* Prefer oneshot capable device */ 26645cb8e01SThomas Gleixner if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) { 26745cb8e01SThomas Gleixner if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT)) 26845cb8e01SThomas Gleixner return false; 26945cb8e01SThomas Gleixner if (tick_oneshot_mode_active()) 27045cb8e01SThomas Gleixner return false; 27145cb8e01SThomas Gleixner } 27245cb8e01SThomas Gleixner 27370e5975dSStephen Boyd /* 27470e5975dSStephen Boyd * Use the higher rated one, but prefer a CPU local device with a lower 27570e5975dSStephen Boyd * rating than a non-CPU local device 27670e5975dSStephen Boyd */ 27770e5975dSStephen Boyd return !curdev || 27870e5975dSStephen Boyd newdev->rating > curdev->rating || 27970e5975dSStephen Boyd !cpumask_equal(curdev->cpumask, newdev->cpumask); 28045cb8e01SThomas Gleixner } 28145cb8e01SThomas Gleixner 282906568c9SThomas Gleixner /* 28303e13cf5SThomas Gleixner * Check whether the new device is a better fit than curdev. curdev 28403e13cf5SThomas Gleixner * can be NULL ! 28503e13cf5SThomas Gleixner */ 28603e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev, 28703e13cf5SThomas Gleixner struct clock_event_device *newdev) 28803e13cf5SThomas Gleixner { 289521c4299SViresh Kumar if (!tick_check_percpu(curdev, newdev, smp_processor_id())) 29003e13cf5SThomas Gleixner return false; 29103e13cf5SThomas Gleixner 29203e13cf5SThomas Gleixner return tick_check_preferred(curdev, newdev); 29303e13cf5SThomas Gleixner } 29403e13cf5SThomas Gleixner 29503e13cf5SThomas Gleixner /* 2967126cac4SThomas Gleixner * Check, if the new registered device should be used. Called with 2977126cac4SThomas Gleixner * clockevents_lock held and interrupts disabled. 298906568c9SThomas Gleixner */ 2997172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev) 300906568c9SThomas Gleixner { 301906568c9SThomas Gleixner struct clock_event_device *curdev; 302906568c9SThomas Gleixner struct tick_device *td; 3037172a286SThomas Gleixner int cpu; 304906568c9SThomas Gleixner 305906568c9SThomas Gleixner cpu = smp_processor_id(); 306320ab2b0SRusty Russell if (!cpumask_test_cpu(cpu, newdev->cpumask)) 3074a93232dSVenki Pallipadi goto out_bc; 308906568c9SThomas Gleixner 309906568c9SThomas Gleixner td = &per_cpu(tick_cpu_device, cpu); 310906568c9SThomas Gleixner curdev = td->evtdev; 311906568c9SThomas Gleixner 312906568c9SThomas Gleixner /* cpu local device ? */ 31345cb8e01SThomas Gleixner if (!tick_check_percpu(curdev, newdev, cpu)) 314906568c9SThomas Gleixner goto out_bc; 315906568c9SThomas Gleixner 31645cb8e01SThomas Gleixner /* Preference decision */ 31745cb8e01SThomas Gleixner if (!tick_check_preferred(curdev, newdev)) 318906568c9SThomas Gleixner goto out_bc; 319906568c9SThomas Gleixner 320ccf33d68SThomas Gleixner if (!try_module_get(newdev->owner)) 321ccf33d68SThomas Gleixner return; 322ccf33d68SThomas Gleixner 323906568c9SThomas Gleixner /* 324906568c9SThomas Gleixner * Replace the eventually existing device by the new 325f8381cbaSThomas Gleixner * device. If the current device is the broadcast device, do 326f8381cbaSThomas Gleixner * not give it back to the clockevents layer ! 327906568c9SThomas Gleixner */ 328f8381cbaSThomas Gleixner if (tick_is_broadcast_device(curdev)) { 3292344abbcSThomas Gleixner clockevents_shutdown(curdev); 330f8381cbaSThomas Gleixner curdev = NULL; 331f8381cbaSThomas Gleixner } 332906568c9SThomas Gleixner clockevents_exchange_device(curdev, newdev); 3336b954823SRusty Russell tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 33479bf2bb3SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 33579bf2bb3SThomas Gleixner tick_oneshot_notify(); 3367172a286SThomas Gleixner return; 337f8381cbaSThomas Gleixner 338f8381cbaSThomas Gleixner out_bc: 339f8381cbaSThomas Gleixner /* 340f8381cbaSThomas Gleixner * Can the new device be used as a broadcast device ? 341f8381cbaSThomas Gleixner */ 3427172a286SThomas Gleixner tick_install_broadcast_device(newdev); 343906568c9SThomas Gleixner } 344906568c9SThomas Gleixner 34552c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 346906568c9SThomas Gleixner /* 34794df7de0SSebastien Dugue * Transfer the do_timer job away from a dying cpu. 34894df7de0SSebastien Dugue * 34952c063d1SThomas Gleixner * Called with interrupts disabled. Not locking required. If 35052c063d1SThomas Gleixner * tick_do_timer_cpu is owned by this cpu, nothing can change it. 35194df7de0SSebastien Dugue */ 35252c063d1SThomas Gleixner void tick_handover_do_timer(void) 35394df7de0SSebastien Dugue { 35452c063d1SThomas Gleixner if (tick_do_timer_cpu == smp_processor_id()) { 35594df7de0SSebastien Dugue int cpu = cpumask_first(cpu_online_mask); 35694df7de0SSebastien Dugue 35794df7de0SSebastien Dugue tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu : 35894df7de0SSebastien Dugue TICK_DO_TIMER_NONE; 35994df7de0SSebastien Dugue } 36094df7de0SSebastien Dugue } 36194df7de0SSebastien Dugue 36294df7de0SSebastien Dugue /* 363906568c9SThomas Gleixner * Shutdown an event device on a given cpu: 364906568c9SThomas Gleixner * 365906568c9SThomas Gleixner * This is called on a life CPU, when a CPU is dead. So we cannot 366906568c9SThomas Gleixner * access the hardware device itself. 367906568c9SThomas Gleixner * We just set the mode and remove it from the lists. 368906568c9SThomas Gleixner */ 369a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu) 370906568c9SThomas Gleixner { 371a49b116dSThomas Gleixner struct tick_device *td = &per_cpu(tick_cpu_device, cpu); 372906568c9SThomas Gleixner struct clock_event_device *dev = td->evtdev; 373906568c9SThomas Gleixner 374906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 375906568c9SThomas Gleixner if (dev) { 376906568c9SThomas Gleixner /* 377906568c9SThomas Gleixner * Prevent that the clock events layer tries to call 378906568c9SThomas Gleixner * the set mode function! 379906568c9SThomas Gleixner */ 38077e32c89SViresh Kumar dev->state = CLOCK_EVT_STATE_DETACHED; 381906568c9SThomas Gleixner dev->mode = CLOCK_EVT_MODE_UNUSED; 382906568c9SThomas Gleixner clockevents_exchange_device(dev, NULL); 3836f7a05d7SThomas Gleixner dev->event_handler = clockevents_handle_noop; 384906568c9SThomas Gleixner td->evtdev = NULL; 385906568c9SThomas Gleixner } 386906568c9SThomas Gleixner } 387a49b116dSThomas Gleixner #endif 388906568c9SThomas Gleixner 3894ffee521SThomas Gleixner /** 390f46481d0SThomas Gleixner * tick_suspend_local - Suspend the local tick device 391f46481d0SThomas Gleixner * 392f46481d0SThomas Gleixner * Called from the local cpu for freeze with interrupts disabled. 393f46481d0SThomas Gleixner * 394f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 395f46481d0SThomas Gleixner */ 3967270d11cSThomas Gleixner void tick_suspend_local(void) 397f46481d0SThomas Gleixner { 398f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 399f46481d0SThomas Gleixner 400f46481d0SThomas Gleixner clockevents_shutdown(td->evtdev); 401f46481d0SThomas Gleixner } 402f46481d0SThomas Gleixner 403f46481d0SThomas Gleixner /** 404f46481d0SThomas Gleixner * tick_resume_local - Resume the local tick device 405f46481d0SThomas Gleixner * 406f46481d0SThomas Gleixner * Called from the local CPU for unfreeze or XEN resume magic. 407f46481d0SThomas Gleixner * 408f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 409f46481d0SThomas Gleixner */ 410f46481d0SThomas Gleixner void tick_resume_local(void) 411f46481d0SThomas Gleixner { 412f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 413f46481d0SThomas Gleixner bool broadcast = tick_resume_check_broadcast(); 414f46481d0SThomas Gleixner 415f46481d0SThomas Gleixner clockevents_tick_resume(td->evtdev); 416f46481d0SThomas Gleixner if (!broadcast) { 417f46481d0SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 418f46481d0SThomas Gleixner tick_setup_periodic(td->evtdev, 0); 419f46481d0SThomas Gleixner else 420f46481d0SThomas Gleixner tick_resume_oneshot(); 421f46481d0SThomas Gleixner } 422f46481d0SThomas Gleixner } 423f46481d0SThomas Gleixner 424f46481d0SThomas Gleixner /** 4254ffee521SThomas Gleixner * tick_suspend - Suspend the tick and the broadcast device 4264ffee521SThomas Gleixner * 4274ffee521SThomas Gleixner * Called from syscore_suspend() via timekeeping_suspend with only one 4284ffee521SThomas Gleixner * CPU online and interrupts disabled or from tick_unfreeze() under 4294ffee521SThomas Gleixner * tick_freeze_lock. 4304ffee521SThomas Gleixner * 4314ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4324ffee521SThomas Gleixner */ 4338c53daf6SThomas Gleixner void tick_suspend(void) 4346321dd60SThomas Gleixner { 435f46481d0SThomas Gleixner tick_suspend_local(); 4364ffee521SThomas Gleixner tick_suspend_broadcast(); 4376321dd60SThomas Gleixner } 4386321dd60SThomas Gleixner 4394ffee521SThomas Gleixner /** 4404ffee521SThomas Gleixner * tick_resume - Resume the tick and the broadcast device 4414ffee521SThomas Gleixner * 4424ffee521SThomas Gleixner * Called from syscore_resume() via timekeeping_resume with only one 443f46481d0SThomas Gleixner * CPU online and interrupts disabled. 4444ffee521SThomas Gleixner * 4454ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4464ffee521SThomas Gleixner */ 4478c53daf6SThomas Gleixner void tick_resume(void) 4486321dd60SThomas Gleixner { 449f46481d0SThomas Gleixner tick_resume_broadcast(); 450f46481d0SThomas Gleixner tick_resume_local(); 4516321dd60SThomas Gleixner } 4526321dd60SThomas Gleixner 453124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock); 454124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth; 455124cf911SRafael J. Wysocki 456124cf911SRafael J. Wysocki /** 457124cf911SRafael J. Wysocki * tick_freeze - Suspend the local tick and (possibly) timekeeping. 458124cf911SRafael J. Wysocki * 459124cf911SRafael J. Wysocki * Check if this is the last online CPU executing the function and if so, 460124cf911SRafael J. Wysocki * suspend timekeeping. Otherwise suspend the local tick. 461124cf911SRafael J. Wysocki * 462124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_unfreeze(). 463124cf911SRafael J. Wysocki * Interrupts must not be enabled before the subsequent %tick_unfreeze(). 464124cf911SRafael J. Wysocki */ 465124cf911SRafael J. Wysocki void tick_freeze(void) 466124cf911SRafael J. Wysocki { 467124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 468124cf911SRafael J. Wysocki 469124cf911SRafael J. Wysocki tick_freeze_depth++; 470def74708SRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) 471124cf911SRafael J. Wysocki timekeeping_suspend(); 472def74708SRafael J. Wysocki else 473f46481d0SThomas Gleixner tick_suspend_local(); 474124cf911SRafael J. Wysocki 475124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 476124cf911SRafael J. Wysocki } 477124cf911SRafael J. Wysocki 478124cf911SRafael J. Wysocki /** 479124cf911SRafael J. Wysocki * tick_unfreeze - Resume the local tick and (possibly) timekeeping. 480124cf911SRafael J. Wysocki * 481124cf911SRafael J. Wysocki * Check if this is the first CPU executing the function and if so, resume 482124cf911SRafael J. Wysocki * timekeeping. Otherwise resume the local tick. 483124cf911SRafael J. Wysocki * 484124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_freeze(). 485124cf911SRafael J. Wysocki * Interrupts must not be enabled after the preceding %tick_freeze(). 486124cf911SRafael J. Wysocki */ 487124cf911SRafael J. Wysocki void tick_unfreeze(void) 488124cf911SRafael J. Wysocki { 489124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 490124cf911SRafael J. Wysocki 491124cf911SRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) 492124cf911SRafael J. Wysocki timekeeping_resume(); 493124cf911SRafael J. Wysocki else 494422fe750SRafael J. Wysocki tick_resume_local(); 495124cf911SRafael J. Wysocki 496124cf911SRafael J. Wysocki tick_freeze_depth--; 497124cf911SRafael J. Wysocki 498124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 499124cf911SRafael J. Wysocki } 500124cf911SRafael J. Wysocki 501906568c9SThomas Gleixner /** 502906568c9SThomas Gleixner * tick_init - initialize the tick control 503906568c9SThomas Gleixner */ 504906568c9SThomas Gleixner void __init tick_init(void) 505906568c9SThomas Gleixner { 506b352bc1cSThomas Gleixner tick_broadcast_init(); 507a80e49e2SFrederic Weisbecker tick_nohz_init(); 508906568c9SThomas Gleixner } 509