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> 22*75e0678eSRafael J. Wysocki #include <trace/events/power.h> 23906568c9SThomas Gleixner 24d7b90689SRussell King #include <asm/irq_regs.h> 25d7b90689SRussell King 26f8381cbaSThomas Gleixner #include "tick-internal.h" 27f8381cbaSThomas Gleixner 28906568c9SThomas Gleixner /* 29906568c9SThomas Gleixner * Tick devices 30906568c9SThomas Gleixner */ 31f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device); 32906568c9SThomas Gleixner /* 33906568c9SThomas Gleixner * Tick next event: keeps track of the tick time 34906568c9SThomas Gleixner */ 35f8381cbaSThomas Gleixner ktime_t tick_next_period; 36f8381cbaSThomas Gleixner ktime_t tick_period; 37050ded1bSAndrew Morton 38050ded1bSAndrew Morton /* 39050ded1bSAndrew Morton * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR 40050ded1bSAndrew Morton * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This 41050ded1bSAndrew Morton * variable has two functions: 42050ded1bSAndrew Morton * 43050ded1bSAndrew Morton * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the 44050ded1bSAndrew Morton * timekeeping lock all at once. Only the CPU which is assigned to do the 45050ded1bSAndrew Morton * update is handling it. 46050ded1bSAndrew Morton * 47050ded1bSAndrew Morton * 2) Hand off the duty in the NOHZ idle case by setting the value to 48050ded1bSAndrew Morton * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks 49050ded1bSAndrew Morton * at it will take over and keep the time keeping alive. The handover 50050ded1bSAndrew Morton * procedure also covers cpu hotplug. 51050ded1bSAndrew Morton */ 526441402bSThomas Gleixner int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT; 53906568c9SThomas Gleixner 54289f480aSIngo Molnar /* 55289f480aSIngo Molnar * Debugging: see timer_list.c 56289f480aSIngo Molnar */ 57289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu) 58289f480aSIngo Molnar { 59289f480aSIngo Molnar return &per_cpu(tick_cpu_device, cpu); 60289f480aSIngo Molnar } 61289f480aSIngo Molnar 6279bf2bb3SThomas Gleixner /** 6379bf2bb3SThomas Gleixner * tick_is_oneshot_available - check for a oneshot capable event device 6479bf2bb3SThomas Gleixner */ 6579bf2bb3SThomas Gleixner int tick_is_oneshot_available(void) 6679bf2bb3SThomas Gleixner { 67909ea964SChristoph Lameter struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev); 6879bf2bb3SThomas Gleixner 693a142a06SThomas Gleixner if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT)) 703a142a06SThomas Gleixner return 0; 713a142a06SThomas Gleixner if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) 723a142a06SThomas Gleixner return 1; 733a142a06SThomas Gleixner return tick_broadcast_oneshot_available(); 7479bf2bb3SThomas Gleixner } 7579bf2bb3SThomas Gleixner 76906568c9SThomas Gleixner /* 77906568c9SThomas Gleixner * Periodic tick 78906568c9SThomas Gleixner */ 79906568c9SThomas Gleixner static void tick_periodic(int cpu) 80906568c9SThomas Gleixner { 81906568c9SThomas Gleixner if (tick_do_timer_cpu == cpu) { 82d6ad4187SJohn Stultz write_seqlock(&jiffies_lock); 83906568c9SThomas Gleixner 84906568c9SThomas Gleixner /* Keep track of the next tick event */ 85906568c9SThomas Gleixner tick_next_period = ktime_add(tick_next_period, tick_period); 86906568c9SThomas Gleixner 87906568c9SThomas Gleixner do_timer(1); 88d6ad4187SJohn Stultz write_sequnlock(&jiffies_lock); 8947a1b796SJohn Stultz update_wall_time(); 90906568c9SThomas Gleixner } 91906568c9SThomas Gleixner 92906568c9SThomas Gleixner update_process_times(user_mode(get_irq_regs())); 93906568c9SThomas Gleixner profile_tick(CPU_PROFILING); 94906568c9SThomas Gleixner } 95906568c9SThomas Gleixner 96906568c9SThomas Gleixner /* 97906568c9SThomas Gleixner * Event handler for periodic ticks 98906568c9SThomas Gleixner */ 99906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev) 100906568c9SThomas Gleixner { 101906568c9SThomas Gleixner int cpu = smp_processor_id(); 102b97f0291SViresh Kumar ktime_t next = dev->next_event; 103906568c9SThomas Gleixner 104906568c9SThomas Gleixner tick_periodic(cpu); 105906568c9SThomas Gleixner 10677e32c89SViresh Kumar if (dev->state != CLOCK_EVT_STATE_ONESHOT) 107906568c9SThomas Gleixner return; 108b97f0291SViresh Kumar for (;;) { 109906568c9SThomas Gleixner /* 110906568c9SThomas Gleixner * Setup the next period for devices, which do not have 111906568c9SThomas Gleixner * periodic mode: 112906568c9SThomas Gleixner */ 113b97f0291SViresh Kumar next = ktime_add(next, tick_period); 114b97f0291SViresh Kumar 115d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false)) 116906568c9SThomas Gleixner return; 11774a03b69Sjohn stultz /* 11874a03b69Sjohn stultz * Have to be careful here. If we're in oneshot mode, 11974a03b69Sjohn stultz * before we call tick_periodic() in a loop, we need 12074a03b69Sjohn stultz * to be sure we're using a real hardware clocksource. 12174a03b69Sjohn stultz * Otherwise we could get trapped in an infinite 12274a03b69Sjohn stultz * loop, as the tick_periodic() increments jiffies, 123cacb3c76SViresh Kumar * which then will increment time, possibly causing 12474a03b69Sjohn stultz * the loop to trigger again and again. 12574a03b69Sjohn stultz */ 12674a03b69Sjohn stultz if (timekeeping_valid_for_hres()) 127906568c9SThomas Gleixner tick_periodic(cpu); 128906568c9SThomas Gleixner } 129906568c9SThomas Gleixner } 130906568c9SThomas Gleixner 131906568c9SThomas Gleixner /* 132906568c9SThomas Gleixner * Setup the device for a periodic tick 133906568c9SThomas Gleixner */ 134f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast) 135906568c9SThomas Gleixner { 136f8381cbaSThomas Gleixner tick_set_periodic_handler(dev, broadcast); 137f8381cbaSThomas Gleixner 138f8381cbaSThomas Gleixner /* Broadcast setup ? */ 139f8381cbaSThomas Gleixner if (!tick_device_is_functional(dev)) 140f8381cbaSThomas Gleixner return; 141906568c9SThomas Gleixner 14227ce4cb4SThomas Gleixner if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) && 14327ce4cb4SThomas Gleixner !tick_broadcast_oneshot_active()) { 14477e32c89SViresh Kumar clockevents_set_state(dev, CLOCK_EVT_STATE_PERIODIC); 145906568c9SThomas Gleixner } else { 146906568c9SThomas Gleixner unsigned long seq; 147906568c9SThomas Gleixner ktime_t next; 148906568c9SThomas Gleixner 149906568c9SThomas Gleixner do { 150d6ad4187SJohn Stultz seq = read_seqbegin(&jiffies_lock); 151906568c9SThomas Gleixner next = tick_next_period; 152d6ad4187SJohn Stultz } while (read_seqretry(&jiffies_lock, seq)); 153906568c9SThomas Gleixner 15477e32c89SViresh Kumar clockevents_set_state(dev, CLOCK_EVT_STATE_ONESHOT); 155906568c9SThomas Gleixner 156906568c9SThomas Gleixner for (;;) { 157d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false)) 158906568c9SThomas Gleixner return; 159906568c9SThomas Gleixner next = ktime_add(next, tick_period); 160906568c9SThomas Gleixner } 161906568c9SThomas Gleixner } 162906568c9SThomas Gleixner } 163906568c9SThomas Gleixner 164906568c9SThomas Gleixner /* 165906568c9SThomas Gleixner * Setup the tick device 166906568c9SThomas Gleixner */ 167906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td, 168906568c9SThomas Gleixner struct clock_event_device *newdev, int cpu, 1690de26520SRusty Russell const struct cpumask *cpumask) 170906568c9SThomas Gleixner { 171906568c9SThomas Gleixner ktime_t next_event; 172906568c9SThomas Gleixner void (*handler)(struct clock_event_device *) = NULL; 173906568c9SThomas Gleixner 174906568c9SThomas Gleixner /* 175906568c9SThomas Gleixner * First device setup ? 176906568c9SThomas Gleixner */ 177906568c9SThomas Gleixner if (!td->evtdev) { 178906568c9SThomas Gleixner /* 179906568c9SThomas Gleixner * If no cpu took the do_timer update, assign it to 180906568c9SThomas Gleixner * this cpu: 181906568c9SThomas Gleixner */ 1826441402bSThomas Gleixner if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) { 183c5bfece2SFrederic Weisbecker if (!tick_nohz_full_cpu(cpu)) 184906568c9SThomas Gleixner tick_do_timer_cpu = cpu; 185a382bf93SFrederic Weisbecker else 186a382bf93SFrederic Weisbecker tick_do_timer_cpu = TICK_DO_TIMER_NONE; 187906568c9SThomas Gleixner tick_next_period = ktime_get(); 188906568c9SThomas Gleixner tick_period = ktime_set(0, NSEC_PER_SEC / HZ); 189906568c9SThomas Gleixner } 190906568c9SThomas Gleixner 191906568c9SThomas Gleixner /* 192906568c9SThomas Gleixner * Startup in periodic mode first. 193906568c9SThomas Gleixner */ 194906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 195906568c9SThomas Gleixner } else { 196906568c9SThomas Gleixner handler = td->evtdev->event_handler; 197906568c9SThomas Gleixner next_event = td->evtdev->next_event; 1987c1e7689SVenkatesh Pallipadi td->evtdev->event_handler = clockevents_handle_noop; 199906568c9SThomas Gleixner } 200906568c9SThomas Gleixner 201906568c9SThomas Gleixner td->evtdev = newdev; 202906568c9SThomas Gleixner 203906568c9SThomas Gleixner /* 204906568c9SThomas Gleixner * When the device is not per cpu, pin the interrupt to the 205906568c9SThomas Gleixner * current cpu: 206906568c9SThomas Gleixner */ 207320ab2b0SRusty Russell if (!cpumask_equal(newdev->cpumask, cpumask)) 2080de26520SRusty Russell irq_set_affinity(newdev->irq, cpumask); 209906568c9SThomas Gleixner 210f8381cbaSThomas Gleixner /* 211f8381cbaSThomas Gleixner * When global broadcasting is active, check if the current 212f8381cbaSThomas Gleixner * device is registered as a placeholder for broadcast mode. 213f8381cbaSThomas Gleixner * This allows us to handle this x86 misfeature in a generic 21407bd1172SThomas Gleixner * way. This function also returns !=0 when we keep the 21507bd1172SThomas Gleixner * current active broadcast state for this CPU. 216f8381cbaSThomas Gleixner */ 217f8381cbaSThomas Gleixner if (tick_device_uses_broadcast(newdev, cpu)) 218f8381cbaSThomas Gleixner return; 219f8381cbaSThomas Gleixner 220906568c9SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 221906568c9SThomas Gleixner tick_setup_periodic(newdev, 0); 22279bf2bb3SThomas Gleixner else 22379bf2bb3SThomas Gleixner tick_setup_oneshot(newdev, handler, next_event); 224906568c9SThomas Gleixner } 225906568c9SThomas Gleixner 22603e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev) 22703e13cf5SThomas Gleixner { 22822127e93SChristoph Lameter struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 22903e13cf5SThomas Gleixner int cpu = smp_processor_id(); 23003e13cf5SThomas Gleixner 23103e13cf5SThomas Gleixner clockevents_exchange_device(td->evtdev, newdev); 23203e13cf5SThomas Gleixner tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 23303e13cf5SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 23403e13cf5SThomas Gleixner tick_oneshot_notify(); 23503e13cf5SThomas Gleixner } 23603e13cf5SThomas Gleixner 23745cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev, 23845cb8e01SThomas Gleixner struct clock_event_device *newdev, int cpu) 23945cb8e01SThomas Gleixner { 24045cb8e01SThomas Gleixner if (!cpumask_test_cpu(cpu, newdev->cpumask)) 24145cb8e01SThomas Gleixner return false; 24245cb8e01SThomas Gleixner if (cpumask_equal(newdev->cpumask, cpumask_of(cpu))) 24345cb8e01SThomas Gleixner return true; 24445cb8e01SThomas Gleixner /* Check if irq affinity can be set */ 24545cb8e01SThomas Gleixner if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq)) 24645cb8e01SThomas Gleixner return false; 24745cb8e01SThomas Gleixner /* Prefer an existing cpu local device */ 24845cb8e01SThomas Gleixner if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu))) 24945cb8e01SThomas Gleixner return false; 25045cb8e01SThomas Gleixner return true; 25145cb8e01SThomas Gleixner } 25245cb8e01SThomas Gleixner 25345cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev, 25445cb8e01SThomas Gleixner struct clock_event_device *newdev) 25545cb8e01SThomas Gleixner { 25645cb8e01SThomas Gleixner /* Prefer oneshot capable device */ 25745cb8e01SThomas Gleixner if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) { 25845cb8e01SThomas Gleixner if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT)) 25945cb8e01SThomas Gleixner return false; 26045cb8e01SThomas Gleixner if (tick_oneshot_mode_active()) 26145cb8e01SThomas Gleixner return false; 26245cb8e01SThomas Gleixner } 26345cb8e01SThomas Gleixner 26470e5975dSStephen Boyd /* 26570e5975dSStephen Boyd * Use the higher rated one, but prefer a CPU local device with a lower 26670e5975dSStephen Boyd * rating than a non-CPU local device 26770e5975dSStephen Boyd */ 26870e5975dSStephen Boyd return !curdev || 26970e5975dSStephen Boyd newdev->rating > curdev->rating || 27070e5975dSStephen Boyd !cpumask_equal(curdev->cpumask, newdev->cpumask); 27145cb8e01SThomas Gleixner } 27245cb8e01SThomas Gleixner 273906568c9SThomas Gleixner /* 27403e13cf5SThomas Gleixner * Check whether the new device is a better fit than curdev. curdev 27503e13cf5SThomas Gleixner * can be NULL ! 27603e13cf5SThomas Gleixner */ 27703e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev, 27803e13cf5SThomas Gleixner struct clock_event_device *newdev) 27903e13cf5SThomas Gleixner { 280521c4299SViresh Kumar if (!tick_check_percpu(curdev, newdev, smp_processor_id())) 28103e13cf5SThomas Gleixner return false; 28203e13cf5SThomas Gleixner 28303e13cf5SThomas Gleixner return tick_check_preferred(curdev, newdev); 28403e13cf5SThomas Gleixner } 28503e13cf5SThomas Gleixner 28603e13cf5SThomas Gleixner /* 2877126cac4SThomas Gleixner * Check, if the new registered device should be used. Called with 2887126cac4SThomas Gleixner * clockevents_lock held and interrupts disabled. 289906568c9SThomas Gleixner */ 2907172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev) 291906568c9SThomas Gleixner { 292906568c9SThomas Gleixner struct clock_event_device *curdev; 293906568c9SThomas Gleixner struct tick_device *td; 2947172a286SThomas Gleixner int cpu; 295906568c9SThomas Gleixner 296906568c9SThomas Gleixner cpu = smp_processor_id(); 297320ab2b0SRusty Russell if (!cpumask_test_cpu(cpu, newdev->cpumask)) 2984a93232dSVenki Pallipadi goto out_bc; 299906568c9SThomas Gleixner 300906568c9SThomas Gleixner td = &per_cpu(tick_cpu_device, cpu); 301906568c9SThomas Gleixner curdev = td->evtdev; 302906568c9SThomas Gleixner 303906568c9SThomas Gleixner /* cpu local device ? */ 30445cb8e01SThomas Gleixner if (!tick_check_percpu(curdev, newdev, cpu)) 305906568c9SThomas Gleixner goto out_bc; 306906568c9SThomas Gleixner 30745cb8e01SThomas Gleixner /* Preference decision */ 30845cb8e01SThomas Gleixner if (!tick_check_preferred(curdev, newdev)) 309906568c9SThomas Gleixner goto out_bc; 310906568c9SThomas Gleixner 311ccf33d68SThomas Gleixner if (!try_module_get(newdev->owner)) 312ccf33d68SThomas Gleixner return; 313ccf33d68SThomas Gleixner 314906568c9SThomas Gleixner /* 315906568c9SThomas Gleixner * Replace the eventually existing device by the new 316f8381cbaSThomas Gleixner * device. If the current device is the broadcast device, do 317f8381cbaSThomas Gleixner * not give it back to the clockevents layer ! 318906568c9SThomas Gleixner */ 319f8381cbaSThomas Gleixner if (tick_is_broadcast_device(curdev)) { 3202344abbcSThomas Gleixner clockevents_shutdown(curdev); 321f8381cbaSThomas Gleixner curdev = NULL; 322f8381cbaSThomas Gleixner } 323906568c9SThomas Gleixner clockevents_exchange_device(curdev, newdev); 3246b954823SRusty Russell tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 32579bf2bb3SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 32679bf2bb3SThomas Gleixner tick_oneshot_notify(); 3277172a286SThomas Gleixner return; 328f8381cbaSThomas Gleixner 329f8381cbaSThomas Gleixner out_bc: 330f8381cbaSThomas Gleixner /* 331f8381cbaSThomas Gleixner * Can the new device be used as a broadcast device ? 332f8381cbaSThomas Gleixner */ 3337172a286SThomas Gleixner tick_install_broadcast_device(newdev); 334906568c9SThomas Gleixner } 335906568c9SThomas Gleixner 33652c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 337906568c9SThomas Gleixner /* 33894df7de0SSebastien Dugue * Transfer the do_timer job away from a dying cpu. 33994df7de0SSebastien Dugue * 34052c063d1SThomas Gleixner * Called with interrupts disabled. Not locking required. If 34152c063d1SThomas Gleixner * tick_do_timer_cpu is owned by this cpu, nothing can change it. 34294df7de0SSebastien Dugue */ 34352c063d1SThomas Gleixner void tick_handover_do_timer(void) 34494df7de0SSebastien Dugue { 34552c063d1SThomas Gleixner if (tick_do_timer_cpu == smp_processor_id()) { 34694df7de0SSebastien Dugue int cpu = cpumask_first(cpu_online_mask); 34794df7de0SSebastien Dugue 34894df7de0SSebastien Dugue tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu : 34994df7de0SSebastien Dugue TICK_DO_TIMER_NONE; 35094df7de0SSebastien Dugue } 35194df7de0SSebastien Dugue } 35294df7de0SSebastien Dugue 35394df7de0SSebastien Dugue /* 354906568c9SThomas Gleixner * Shutdown an event device on a given cpu: 355906568c9SThomas Gleixner * 356906568c9SThomas Gleixner * This is called on a life CPU, when a CPU is dead. So we cannot 357906568c9SThomas Gleixner * access the hardware device itself. 358906568c9SThomas Gleixner * We just set the mode and remove it from the lists. 359906568c9SThomas Gleixner */ 360a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu) 361906568c9SThomas Gleixner { 362a49b116dSThomas Gleixner struct tick_device *td = &per_cpu(tick_cpu_device, cpu); 363906568c9SThomas Gleixner struct clock_event_device *dev = td->evtdev; 364906568c9SThomas Gleixner 365906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 366906568c9SThomas Gleixner if (dev) { 367906568c9SThomas Gleixner /* 368906568c9SThomas Gleixner * Prevent that the clock events layer tries to call 369906568c9SThomas Gleixner * the set mode function! 370906568c9SThomas Gleixner */ 37177e32c89SViresh Kumar dev->state = CLOCK_EVT_STATE_DETACHED; 372906568c9SThomas Gleixner dev->mode = CLOCK_EVT_MODE_UNUSED; 373906568c9SThomas Gleixner clockevents_exchange_device(dev, NULL); 3746f7a05d7SThomas Gleixner dev->event_handler = clockevents_handle_noop; 375906568c9SThomas Gleixner td->evtdev = NULL; 376906568c9SThomas Gleixner } 377906568c9SThomas Gleixner } 378a49b116dSThomas Gleixner #endif 379906568c9SThomas Gleixner 3804ffee521SThomas Gleixner /** 381f46481d0SThomas Gleixner * tick_suspend_local - Suspend the local tick device 382f46481d0SThomas Gleixner * 383f46481d0SThomas Gleixner * Called from the local cpu for freeze with interrupts disabled. 384f46481d0SThomas Gleixner * 385f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 386f46481d0SThomas Gleixner */ 3877270d11cSThomas Gleixner void tick_suspend_local(void) 388f46481d0SThomas Gleixner { 389f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 390f46481d0SThomas Gleixner 391f46481d0SThomas Gleixner clockevents_shutdown(td->evtdev); 392f46481d0SThomas Gleixner } 393f46481d0SThomas Gleixner 394f46481d0SThomas Gleixner /** 395f46481d0SThomas Gleixner * tick_resume_local - Resume the local tick device 396f46481d0SThomas Gleixner * 397f46481d0SThomas Gleixner * Called from the local CPU for unfreeze or XEN resume magic. 398f46481d0SThomas Gleixner * 399f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 400f46481d0SThomas Gleixner */ 401f46481d0SThomas Gleixner void tick_resume_local(void) 402f46481d0SThomas Gleixner { 403f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 404f46481d0SThomas Gleixner bool broadcast = tick_resume_check_broadcast(); 405f46481d0SThomas Gleixner 406f46481d0SThomas Gleixner clockevents_tick_resume(td->evtdev); 407f46481d0SThomas Gleixner if (!broadcast) { 408f46481d0SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 409f46481d0SThomas Gleixner tick_setup_periodic(td->evtdev, 0); 410f46481d0SThomas Gleixner else 411f46481d0SThomas Gleixner tick_resume_oneshot(); 412f46481d0SThomas Gleixner } 413f46481d0SThomas Gleixner } 414f46481d0SThomas Gleixner 415f46481d0SThomas Gleixner /** 4164ffee521SThomas Gleixner * tick_suspend - Suspend the tick and the broadcast device 4174ffee521SThomas Gleixner * 4184ffee521SThomas Gleixner * Called from syscore_suspend() via timekeeping_suspend with only one 4194ffee521SThomas Gleixner * CPU online and interrupts disabled or from tick_unfreeze() under 4204ffee521SThomas Gleixner * tick_freeze_lock. 4214ffee521SThomas Gleixner * 4224ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4234ffee521SThomas Gleixner */ 4248c53daf6SThomas Gleixner void tick_suspend(void) 4256321dd60SThomas Gleixner { 426f46481d0SThomas Gleixner tick_suspend_local(); 4274ffee521SThomas Gleixner tick_suspend_broadcast(); 4286321dd60SThomas Gleixner } 4296321dd60SThomas Gleixner 4304ffee521SThomas Gleixner /** 4314ffee521SThomas Gleixner * tick_resume - Resume the tick and the broadcast device 4324ffee521SThomas Gleixner * 4334ffee521SThomas Gleixner * Called from syscore_resume() via timekeeping_resume with only one 434f46481d0SThomas Gleixner * CPU online and interrupts disabled. 4354ffee521SThomas Gleixner * 4364ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4374ffee521SThomas Gleixner */ 4388c53daf6SThomas Gleixner void tick_resume(void) 4396321dd60SThomas Gleixner { 440f46481d0SThomas Gleixner tick_resume_broadcast(); 441f46481d0SThomas Gleixner tick_resume_local(); 4426321dd60SThomas Gleixner } 4436321dd60SThomas Gleixner 444124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock); 445124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth; 446124cf911SRafael J. Wysocki 447124cf911SRafael J. Wysocki /** 448124cf911SRafael J. Wysocki * tick_freeze - Suspend the local tick and (possibly) timekeeping. 449124cf911SRafael J. Wysocki * 450124cf911SRafael J. Wysocki * Check if this is the last online CPU executing the function and if so, 451124cf911SRafael J. Wysocki * suspend timekeeping. Otherwise suspend the local tick. 452124cf911SRafael J. Wysocki * 453124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_unfreeze(). 454124cf911SRafael J. Wysocki * Interrupts must not be enabled before the subsequent %tick_unfreeze(). 455124cf911SRafael J. Wysocki */ 456124cf911SRafael J. Wysocki void tick_freeze(void) 457124cf911SRafael J. Wysocki { 458124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 459124cf911SRafael J. Wysocki 460124cf911SRafael J. Wysocki tick_freeze_depth++; 461*75e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) { 462*75e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"), 463*75e0678eSRafael J. Wysocki smp_processor_id(), true); 464124cf911SRafael J. Wysocki timekeeping_suspend(); 465*75e0678eSRafael J. Wysocki } else { 466f46481d0SThomas Gleixner tick_suspend_local(); 467*75e0678eSRafael J. Wysocki } 468124cf911SRafael J. Wysocki 469124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 470124cf911SRafael J. Wysocki } 471124cf911SRafael J. Wysocki 472124cf911SRafael J. Wysocki /** 473124cf911SRafael J. Wysocki * tick_unfreeze - Resume the local tick and (possibly) timekeeping. 474124cf911SRafael J. Wysocki * 475124cf911SRafael J. Wysocki * Check if this is the first CPU executing the function and if so, resume 476124cf911SRafael J. Wysocki * timekeeping. Otherwise resume the local tick. 477124cf911SRafael J. Wysocki * 478124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_freeze(). 479124cf911SRafael J. Wysocki * Interrupts must not be enabled after the preceding %tick_freeze(). 480124cf911SRafael J. Wysocki */ 481124cf911SRafael J. Wysocki void tick_unfreeze(void) 482124cf911SRafael J. Wysocki { 483124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 484124cf911SRafael J. Wysocki 485*75e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) { 486124cf911SRafael J. Wysocki timekeeping_resume(); 487*75e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"), 488*75e0678eSRafael J. Wysocki smp_processor_id(), false); 489*75e0678eSRafael J. Wysocki } else { 490422fe750SRafael J. Wysocki tick_resume_local(); 491*75e0678eSRafael J. Wysocki } 492124cf911SRafael J. Wysocki 493124cf911SRafael J. Wysocki tick_freeze_depth--; 494124cf911SRafael J. Wysocki 495124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 496124cf911SRafael J. Wysocki } 497124cf911SRafael J. Wysocki 498906568c9SThomas Gleixner /** 499906568c9SThomas Gleixner * tick_init - initialize the tick control 500906568c9SThomas Gleixner */ 501906568c9SThomas Gleixner void __init tick_init(void) 502906568c9SThomas Gleixner { 503b352bc1cSThomas Gleixner tick_broadcast_init(); 504a80e49e2SFrederic Weisbecker tick_nohz_init(); 505906568c9SThomas Gleixner } 506