1*35728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0 2906568c9SThomas Gleixner /* 3906568c9SThomas Gleixner * This file contains the base functions to manage periodic tick 4906568c9SThomas Gleixner * related events. 5906568c9SThomas Gleixner * 6906568c9SThomas Gleixner * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> 7906568c9SThomas Gleixner * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar 8906568c9SThomas Gleixner * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner 9906568c9SThomas Gleixner * 10906568c9SThomas Gleixner * This code is licenced under the GPL version 2. For details see 11906568c9SThomas Gleixner * kernel-base/COPYING. 12906568c9SThomas Gleixner */ 13906568c9SThomas Gleixner #include <linux/cpu.h> 14906568c9SThomas Gleixner #include <linux/err.h> 15906568c9SThomas Gleixner #include <linux/hrtimer.h> 16d7b90689SRussell King #include <linux/interrupt.h> 17906568c9SThomas Gleixner #include <linux/percpu.h> 18906568c9SThomas Gleixner #include <linux/profile.h> 19906568c9SThomas Gleixner #include <linux/sched.h> 20ccf33d68SThomas Gleixner #include <linux/module.h> 2175e0678eSRafael J. Wysocki #include <trace/events/power.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 115472c4a94SViresh 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()) { 153d7eb231cSThomas Gleixner clockevents_switch_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 163d7eb231cSThomas Gleixner clockevents_switch_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 void (*handler)(struct clock_event_device *) = NULL; 1818b0e1953SThomas Gleixner ktime_t next_event = 0; 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(); 1978b0e1953SThomas Gleixner tick_period = 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 || 2795b5ccbc2SSudeep Holla !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(); 306906568c9SThomas Gleixner td = &per_cpu(tick_cpu_device, cpu); 307906568c9SThomas Gleixner curdev = td->evtdev; 308906568c9SThomas Gleixner 309906568c9SThomas Gleixner /* cpu local device ? */ 31045cb8e01SThomas Gleixner if (!tick_check_percpu(curdev, newdev, cpu)) 311906568c9SThomas Gleixner goto out_bc; 312906568c9SThomas Gleixner 31345cb8e01SThomas Gleixner /* Preference decision */ 31445cb8e01SThomas Gleixner if (!tick_check_preferred(curdev, newdev)) 315906568c9SThomas Gleixner goto out_bc; 316906568c9SThomas Gleixner 317ccf33d68SThomas Gleixner if (!try_module_get(newdev->owner)) 318ccf33d68SThomas Gleixner return; 319ccf33d68SThomas Gleixner 320906568c9SThomas Gleixner /* 321906568c9SThomas Gleixner * Replace the eventually existing device by the new 322f8381cbaSThomas Gleixner * device. If the current device is the broadcast device, do 323f8381cbaSThomas Gleixner * not give it back to the clockevents layer ! 324906568c9SThomas Gleixner */ 325f8381cbaSThomas Gleixner if (tick_is_broadcast_device(curdev)) { 3262344abbcSThomas Gleixner clockevents_shutdown(curdev); 327f8381cbaSThomas Gleixner curdev = NULL; 328f8381cbaSThomas Gleixner } 329906568c9SThomas Gleixner clockevents_exchange_device(curdev, newdev); 3306b954823SRusty Russell tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 33179bf2bb3SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 33279bf2bb3SThomas Gleixner tick_oneshot_notify(); 3337172a286SThomas Gleixner return; 334f8381cbaSThomas Gleixner 335f8381cbaSThomas Gleixner out_bc: 336f8381cbaSThomas Gleixner /* 337f8381cbaSThomas Gleixner * Can the new device be used as a broadcast device ? 338f8381cbaSThomas Gleixner */ 3397172a286SThomas Gleixner tick_install_broadcast_device(newdev); 340906568c9SThomas Gleixner } 341906568c9SThomas Gleixner 342f32dd117SThomas Gleixner /** 343f32dd117SThomas Gleixner * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode 344f32dd117SThomas Gleixner * @state: The target state (enter/exit) 345f32dd117SThomas Gleixner * 346f32dd117SThomas Gleixner * The system enters/leaves a state, where affected devices might stop 347f32dd117SThomas Gleixner * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups. 348f32dd117SThomas Gleixner * 349f32dd117SThomas Gleixner * Called with interrupts disabled, so clockevents_lock is not 350f32dd117SThomas Gleixner * required here because the local clock event device cannot go away 351f32dd117SThomas Gleixner * under us. 352f32dd117SThomas Gleixner */ 353f32dd117SThomas Gleixner int tick_broadcast_oneshot_control(enum tick_broadcast_state state) 354f32dd117SThomas Gleixner { 355f32dd117SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 356f32dd117SThomas Gleixner 357f32dd117SThomas Gleixner if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP)) 358f32dd117SThomas Gleixner return 0; 359f32dd117SThomas Gleixner 360f32dd117SThomas Gleixner return __tick_broadcast_oneshot_control(state); 361f32dd117SThomas Gleixner } 3620f447051SThomas Gleixner EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control); 363f32dd117SThomas Gleixner 36452c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 365906568c9SThomas Gleixner /* 36694df7de0SSebastien Dugue * Transfer the do_timer job away from a dying cpu. 36794df7de0SSebastien Dugue * 36852c063d1SThomas Gleixner * Called with interrupts disabled. Not locking required. If 36952c063d1SThomas Gleixner * tick_do_timer_cpu is owned by this cpu, nothing can change it. 37094df7de0SSebastien Dugue */ 37152c063d1SThomas Gleixner void tick_handover_do_timer(void) 37294df7de0SSebastien Dugue { 37352c063d1SThomas Gleixner if (tick_do_timer_cpu == smp_processor_id()) { 37494df7de0SSebastien Dugue int cpu = cpumask_first(cpu_online_mask); 37594df7de0SSebastien Dugue 37694df7de0SSebastien Dugue tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu : 37794df7de0SSebastien Dugue TICK_DO_TIMER_NONE; 37894df7de0SSebastien Dugue } 37994df7de0SSebastien Dugue } 38094df7de0SSebastien Dugue 38194df7de0SSebastien Dugue /* 382906568c9SThomas Gleixner * Shutdown an event device on a given cpu: 383906568c9SThomas Gleixner * 384906568c9SThomas Gleixner * This is called on a life CPU, when a CPU is dead. So we cannot 385906568c9SThomas Gleixner * access the hardware device itself. 386906568c9SThomas Gleixner * We just set the mode and remove it from the lists. 387906568c9SThomas Gleixner */ 388a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu) 389906568c9SThomas Gleixner { 390a49b116dSThomas Gleixner struct tick_device *td = &per_cpu(tick_cpu_device, cpu); 391906568c9SThomas Gleixner struct clock_event_device *dev = td->evtdev; 392906568c9SThomas Gleixner 393906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 394906568c9SThomas Gleixner if (dev) { 395906568c9SThomas Gleixner /* 396906568c9SThomas Gleixner * Prevent that the clock events layer tries to call 397906568c9SThomas Gleixner * the set mode function! 398906568c9SThomas Gleixner */ 399051ebd10SThomas Gleixner clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED); 400906568c9SThomas Gleixner clockevents_exchange_device(dev, NULL); 4016f7a05d7SThomas Gleixner dev->event_handler = clockevents_handle_noop; 402906568c9SThomas Gleixner td->evtdev = NULL; 403906568c9SThomas Gleixner } 404906568c9SThomas Gleixner } 405a49b116dSThomas Gleixner #endif 406906568c9SThomas Gleixner 4074ffee521SThomas Gleixner /** 408f46481d0SThomas Gleixner * tick_suspend_local - Suspend the local tick device 409f46481d0SThomas Gleixner * 410f46481d0SThomas Gleixner * Called from the local cpu for freeze with interrupts disabled. 411f46481d0SThomas Gleixner * 412f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 413f46481d0SThomas Gleixner */ 4147270d11cSThomas Gleixner void tick_suspend_local(void) 415f46481d0SThomas Gleixner { 416f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 417f46481d0SThomas Gleixner 418f46481d0SThomas Gleixner clockevents_shutdown(td->evtdev); 419f46481d0SThomas Gleixner } 420f46481d0SThomas Gleixner 421f46481d0SThomas Gleixner /** 422f46481d0SThomas Gleixner * tick_resume_local - Resume the local tick device 423f46481d0SThomas Gleixner * 424f46481d0SThomas Gleixner * Called from the local CPU for unfreeze or XEN resume magic. 425f46481d0SThomas Gleixner * 426f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 427f46481d0SThomas Gleixner */ 428f46481d0SThomas Gleixner void tick_resume_local(void) 429f46481d0SThomas Gleixner { 430f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 431f46481d0SThomas Gleixner bool broadcast = tick_resume_check_broadcast(); 432f46481d0SThomas Gleixner 433f46481d0SThomas Gleixner clockevents_tick_resume(td->evtdev); 434f46481d0SThomas Gleixner if (!broadcast) { 435f46481d0SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 436f46481d0SThomas Gleixner tick_setup_periodic(td->evtdev, 0); 437f46481d0SThomas Gleixner else 438f46481d0SThomas Gleixner tick_resume_oneshot(); 439f46481d0SThomas Gleixner } 440f46481d0SThomas Gleixner } 441f46481d0SThomas Gleixner 442f46481d0SThomas Gleixner /** 4434ffee521SThomas Gleixner * tick_suspend - Suspend the tick and the broadcast device 4444ffee521SThomas Gleixner * 4454ffee521SThomas Gleixner * Called from syscore_suspend() via timekeeping_suspend with only one 4464ffee521SThomas Gleixner * CPU online and interrupts disabled or from tick_unfreeze() under 4474ffee521SThomas Gleixner * tick_freeze_lock. 4484ffee521SThomas Gleixner * 4494ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4504ffee521SThomas Gleixner */ 4518c53daf6SThomas Gleixner void tick_suspend(void) 4526321dd60SThomas Gleixner { 453f46481d0SThomas Gleixner tick_suspend_local(); 4544ffee521SThomas Gleixner tick_suspend_broadcast(); 4556321dd60SThomas Gleixner } 4566321dd60SThomas Gleixner 4574ffee521SThomas Gleixner /** 4584ffee521SThomas Gleixner * tick_resume - Resume the tick and the broadcast device 4594ffee521SThomas Gleixner * 4604ffee521SThomas Gleixner * Called from syscore_resume() via timekeeping_resume with only one 461f46481d0SThomas Gleixner * CPU online and interrupts disabled. 4624ffee521SThomas Gleixner * 4634ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4644ffee521SThomas Gleixner */ 4658c53daf6SThomas Gleixner void tick_resume(void) 4666321dd60SThomas Gleixner { 467f46481d0SThomas Gleixner tick_resume_broadcast(); 468f46481d0SThomas Gleixner tick_resume_local(); 4696321dd60SThomas Gleixner } 4706321dd60SThomas Gleixner 47187e9b9f1SRafael J. Wysocki #ifdef CONFIG_SUSPEND 472124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock); 473124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth; 474124cf911SRafael J. Wysocki 475124cf911SRafael J. Wysocki /** 476124cf911SRafael J. Wysocki * tick_freeze - Suspend the local tick and (possibly) timekeeping. 477124cf911SRafael J. Wysocki * 478124cf911SRafael J. Wysocki * Check if this is the last online CPU executing the function and if so, 479124cf911SRafael J. Wysocki * suspend timekeeping. Otherwise suspend the local tick. 480124cf911SRafael J. Wysocki * 481124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_unfreeze(). 482124cf911SRafael J. Wysocki * Interrupts must not be enabled before the subsequent %tick_unfreeze(). 483124cf911SRafael J. Wysocki */ 484124cf911SRafael J. Wysocki void tick_freeze(void) 485124cf911SRafael J. Wysocki { 486124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 487124cf911SRafael J. Wysocki 488124cf911SRafael J. Wysocki tick_freeze_depth++; 48975e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) { 49075e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"), 49175e0678eSRafael J. Wysocki smp_processor_id(), true); 492c1a957d1SThomas Gleixner system_state = SYSTEM_SUSPEND; 493124cf911SRafael J. Wysocki timekeeping_suspend(); 49475e0678eSRafael J. Wysocki } else { 495f46481d0SThomas Gleixner tick_suspend_local(); 49675e0678eSRafael J. Wysocki } 497124cf911SRafael J. Wysocki 498124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 499124cf911SRafael J. Wysocki } 500124cf911SRafael J. Wysocki 501124cf911SRafael J. Wysocki /** 502124cf911SRafael J. Wysocki * tick_unfreeze - Resume the local tick and (possibly) timekeeping. 503124cf911SRafael J. Wysocki * 504124cf911SRafael J. Wysocki * Check if this is the first CPU executing the function and if so, resume 505124cf911SRafael J. Wysocki * timekeeping. Otherwise resume the local tick. 506124cf911SRafael J. Wysocki * 507124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_freeze(). 508124cf911SRafael J. Wysocki * Interrupts must not be enabled after the preceding %tick_freeze(). 509124cf911SRafael J. Wysocki */ 510124cf911SRafael J. Wysocki void tick_unfreeze(void) 511124cf911SRafael J. Wysocki { 512124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 513124cf911SRafael J. Wysocki 51475e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) { 515124cf911SRafael J. Wysocki timekeeping_resume(); 516c1a957d1SThomas Gleixner system_state = SYSTEM_RUNNING; 51775e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"), 51875e0678eSRafael J. Wysocki smp_processor_id(), false); 51975e0678eSRafael J. Wysocki } else { 520422fe750SRafael J. Wysocki tick_resume_local(); 52175e0678eSRafael J. Wysocki } 522124cf911SRafael J. Wysocki 523124cf911SRafael J. Wysocki tick_freeze_depth--; 524124cf911SRafael J. Wysocki 525124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 526124cf911SRafael J. Wysocki } 52787e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */ 528124cf911SRafael J. Wysocki 529906568c9SThomas Gleixner /** 530906568c9SThomas Gleixner * tick_init - initialize the tick control 531906568c9SThomas Gleixner */ 532906568c9SThomas Gleixner void __init tick_init(void) 533906568c9SThomas Gleixner { 534b352bc1cSThomas Gleixner tick_broadcast_init(); 535a80e49e2SFrederic Weisbecker tick_nohz_init(); 536906568c9SThomas Gleixner } 537