135728b82SThomas 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 #include <linux/cpu.h> 11906568c9SThomas Gleixner #include <linux/err.h> 12906568c9SThomas Gleixner #include <linux/hrtimer.h> 13d7b90689SRussell King #include <linux/interrupt.h> 145167c506SChunyan Zhang #include <linux/nmi.h> 15906568c9SThomas Gleixner #include <linux/percpu.h> 16906568c9SThomas Gleixner #include <linux/profile.h> 17906568c9SThomas Gleixner #include <linux/sched.h> 18ccf33d68SThomas Gleixner #include <linux/module.h> 1975e0678eSRafael J. Wysocki #include <trace/events/power.h> 20906568c9SThomas Gleixner 21d7b90689SRussell King #include <asm/irq_regs.h> 22d7b90689SRussell King 23f8381cbaSThomas Gleixner #include "tick-internal.h" 24f8381cbaSThomas Gleixner 25906568c9SThomas Gleixner /* 26906568c9SThomas Gleixner * Tick devices 27906568c9SThomas Gleixner */ 28f8381cbaSThomas Gleixner DEFINE_PER_CPU(struct tick_device, tick_cpu_device); 29906568c9SThomas Gleixner /* 30*c398960cSThomas Gleixner * Tick next event: keeps track of the tick time. It's updated by the 31*c398960cSThomas Gleixner * CPU which handles the tick and protected by jiffies_lock. There is 32*c398960cSThomas Gleixner * no requirement to write hold the jiffies seqcount for it. 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; 5208ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL 5308ae95f4SNicholas Piggin /* 5408ae95f4SNicholas Piggin * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns 5508ae95f4SNicholas Piggin * tick_do_timer_cpu and it should be taken over by an eligible secondary 5608ae95f4SNicholas Piggin * when one comes online. 5708ae95f4SNicholas Piggin */ 5808ae95f4SNicholas Piggin static int tick_do_timer_boot_cpu __read_mostly = -1; 5908ae95f4SNicholas Piggin #endif 60906568c9SThomas Gleixner 61289f480aSIngo Molnar /* 62289f480aSIngo Molnar * Debugging: see timer_list.c 63289f480aSIngo Molnar */ 64289f480aSIngo Molnar struct tick_device *tick_get_device(int cpu) 65289f480aSIngo Molnar { 66289f480aSIngo Molnar return &per_cpu(tick_cpu_device, cpu); 67289f480aSIngo Molnar } 68289f480aSIngo Molnar 6979bf2bb3SThomas Gleixner /** 7079bf2bb3SThomas Gleixner * tick_is_oneshot_available - check for a oneshot capable event device 7179bf2bb3SThomas Gleixner */ 7279bf2bb3SThomas Gleixner int tick_is_oneshot_available(void) 7379bf2bb3SThomas Gleixner { 74909ea964SChristoph Lameter struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev); 7579bf2bb3SThomas Gleixner 763a142a06SThomas Gleixner if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT)) 773a142a06SThomas Gleixner return 0; 783a142a06SThomas Gleixner if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) 793a142a06SThomas Gleixner return 1; 803a142a06SThomas Gleixner return tick_broadcast_oneshot_available(); 8179bf2bb3SThomas Gleixner } 8279bf2bb3SThomas Gleixner 83906568c9SThomas Gleixner /* 84906568c9SThomas Gleixner * Periodic tick 85906568c9SThomas Gleixner */ 86906568c9SThomas Gleixner static void tick_periodic(int cpu) 87906568c9SThomas Gleixner { 88906568c9SThomas Gleixner if (tick_do_timer_cpu == cpu) { 89e5d4d175SThomas Gleixner raw_spin_lock(&jiffies_lock); 90e5d4d175SThomas Gleixner write_seqcount_begin(&jiffies_seq); 91906568c9SThomas Gleixner 92906568c9SThomas Gleixner /* Keep track of the next tick event */ 93906568c9SThomas Gleixner tick_next_period = ktime_add(tick_next_period, tick_period); 94906568c9SThomas Gleixner 95906568c9SThomas Gleixner do_timer(1); 96e5d4d175SThomas Gleixner write_seqcount_end(&jiffies_seq); 97e5d4d175SThomas Gleixner raw_spin_unlock(&jiffies_lock); 9847a1b796SJohn Stultz update_wall_time(); 99906568c9SThomas Gleixner } 100906568c9SThomas Gleixner 101906568c9SThomas Gleixner update_process_times(user_mode(get_irq_regs())); 102906568c9SThomas Gleixner profile_tick(CPU_PROFILING); 103906568c9SThomas Gleixner } 104906568c9SThomas Gleixner 105906568c9SThomas Gleixner /* 106906568c9SThomas Gleixner * Event handler for periodic ticks 107906568c9SThomas Gleixner */ 108906568c9SThomas Gleixner void tick_handle_periodic(struct clock_event_device *dev) 109906568c9SThomas Gleixner { 110906568c9SThomas Gleixner int cpu = smp_processor_id(); 111b97f0291SViresh Kumar ktime_t next = dev->next_event; 112906568c9SThomas Gleixner 113906568c9SThomas Gleixner tick_periodic(cpu); 114906568c9SThomas Gleixner 115c6eb3f70SThomas Gleixner #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON) 116c6eb3f70SThomas Gleixner /* 117c6eb3f70SThomas Gleixner * The cpu might have transitioned to HIGHRES or NOHZ mode via 118c6eb3f70SThomas Gleixner * update_process_times() -> run_local_timers() -> 119c6eb3f70SThomas Gleixner * hrtimer_run_queues(). 120c6eb3f70SThomas Gleixner */ 121c6eb3f70SThomas Gleixner if (dev->event_handler != tick_handle_periodic) 122c6eb3f70SThomas Gleixner return; 123c6eb3f70SThomas Gleixner #endif 124c6eb3f70SThomas Gleixner 125472c4a94SViresh Kumar if (!clockevent_state_oneshot(dev)) 126906568c9SThomas Gleixner return; 127b97f0291SViresh Kumar for (;;) { 128906568c9SThomas Gleixner /* 129906568c9SThomas Gleixner * Setup the next period for devices, which do not have 130906568c9SThomas Gleixner * periodic mode: 131906568c9SThomas Gleixner */ 132b97f0291SViresh Kumar next = ktime_add(next, tick_period); 133b97f0291SViresh Kumar 134d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false)) 135906568c9SThomas Gleixner return; 13674a03b69Sjohn stultz /* 13774a03b69Sjohn stultz * Have to be careful here. If we're in oneshot mode, 13874a03b69Sjohn stultz * before we call tick_periodic() in a loop, we need 13974a03b69Sjohn stultz * to be sure we're using a real hardware clocksource. 14074a03b69Sjohn stultz * Otherwise we could get trapped in an infinite 14174a03b69Sjohn stultz * loop, as the tick_periodic() increments jiffies, 142cacb3c76SViresh Kumar * which then will increment time, possibly causing 14374a03b69Sjohn stultz * the loop to trigger again and again. 14474a03b69Sjohn stultz */ 14574a03b69Sjohn stultz if (timekeeping_valid_for_hres()) 146906568c9SThomas Gleixner tick_periodic(cpu); 147906568c9SThomas Gleixner } 148906568c9SThomas Gleixner } 149906568c9SThomas Gleixner 150906568c9SThomas Gleixner /* 151906568c9SThomas Gleixner * Setup the device for a periodic tick 152906568c9SThomas Gleixner */ 153f8381cbaSThomas Gleixner void tick_setup_periodic(struct clock_event_device *dev, int broadcast) 154906568c9SThomas Gleixner { 155f8381cbaSThomas Gleixner tick_set_periodic_handler(dev, broadcast); 156f8381cbaSThomas Gleixner 157f8381cbaSThomas Gleixner /* Broadcast setup ? */ 158f8381cbaSThomas Gleixner if (!tick_device_is_functional(dev)) 159f8381cbaSThomas Gleixner return; 160906568c9SThomas Gleixner 16127ce4cb4SThomas Gleixner if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) && 16227ce4cb4SThomas Gleixner !tick_broadcast_oneshot_active()) { 163d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC); 164906568c9SThomas Gleixner } else { 165e1e41b6cSRasmus Villemoes unsigned int seq; 166906568c9SThomas Gleixner ktime_t next; 167906568c9SThomas Gleixner 168906568c9SThomas Gleixner do { 169e5d4d175SThomas Gleixner seq = read_seqcount_begin(&jiffies_seq); 170906568c9SThomas Gleixner next = tick_next_period; 171e5d4d175SThomas Gleixner } while (read_seqcount_retry(&jiffies_seq, seq)); 172906568c9SThomas Gleixner 173d7eb231cSThomas Gleixner clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT); 174906568c9SThomas Gleixner 175906568c9SThomas Gleixner for (;;) { 176d1748302SMartin Schwidefsky if (!clockevents_program_event(dev, next, false)) 177906568c9SThomas Gleixner return; 178906568c9SThomas Gleixner next = ktime_add(next, tick_period); 179906568c9SThomas Gleixner } 180906568c9SThomas Gleixner } 181906568c9SThomas Gleixner } 182906568c9SThomas Gleixner 18308ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL 18408ae95f4SNicholas Piggin static void giveup_do_timer(void *info) 18508ae95f4SNicholas Piggin { 18608ae95f4SNicholas Piggin int cpu = *(unsigned int *)info; 18708ae95f4SNicholas Piggin 18808ae95f4SNicholas Piggin WARN_ON(tick_do_timer_cpu != smp_processor_id()); 18908ae95f4SNicholas Piggin 19008ae95f4SNicholas Piggin tick_do_timer_cpu = cpu; 19108ae95f4SNicholas Piggin } 19208ae95f4SNicholas Piggin 19308ae95f4SNicholas Piggin static void tick_take_do_timer_from_boot(void) 19408ae95f4SNicholas Piggin { 19508ae95f4SNicholas Piggin int cpu = smp_processor_id(); 19608ae95f4SNicholas Piggin int from = tick_do_timer_boot_cpu; 19708ae95f4SNicholas Piggin 19808ae95f4SNicholas Piggin if (from >= 0 && from != cpu) 19908ae95f4SNicholas Piggin smp_call_function_single(from, giveup_do_timer, &cpu, 1); 20008ae95f4SNicholas Piggin } 20108ae95f4SNicholas Piggin #endif 20208ae95f4SNicholas Piggin 203906568c9SThomas Gleixner /* 204906568c9SThomas Gleixner * Setup the tick device 205906568c9SThomas Gleixner */ 206906568c9SThomas Gleixner static void tick_setup_device(struct tick_device *td, 207906568c9SThomas Gleixner struct clock_event_device *newdev, int cpu, 2080de26520SRusty Russell const struct cpumask *cpumask) 209906568c9SThomas Gleixner { 210906568c9SThomas Gleixner void (*handler)(struct clock_event_device *) = NULL; 2118b0e1953SThomas Gleixner ktime_t next_event = 0; 212906568c9SThomas Gleixner 213906568c9SThomas Gleixner /* 214906568c9SThomas Gleixner * First device setup ? 215906568c9SThomas Gleixner */ 216906568c9SThomas Gleixner if (!td->evtdev) { 217906568c9SThomas Gleixner /* 218906568c9SThomas Gleixner * If no cpu took the do_timer update, assign it to 219906568c9SThomas Gleixner * this cpu: 220906568c9SThomas Gleixner */ 2216441402bSThomas Gleixner if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) { 222906568c9SThomas Gleixner tick_do_timer_cpu = cpu; 22308ae95f4SNicholas Piggin 224906568c9SThomas Gleixner tick_next_period = ktime_get(); 2258b0e1953SThomas Gleixner tick_period = NSEC_PER_SEC / HZ; 22608ae95f4SNicholas Piggin #ifdef CONFIG_NO_HZ_FULL 22708ae95f4SNicholas Piggin /* 22808ae95f4SNicholas Piggin * The boot CPU may be nohz_full, in which case set 22908ae95f4SNicholas Piggin * tick_do_timer_boot_cpu so the first housekeeping 23008ae95f4SNicholas Piggin * secondary that comes up will take do_timer from 23108ae95f4SNicholas Piggin * us. 23208ae95f4SNicholas Piggin */ 23308ae95f4SNicholas Piggin if (tick_nohz_full_cpu(cpu)) 23408ae95f4SNicholas Piggin tick_do_timer_boot_cpu = cpu; 23508ae95f4SNicholas Piggin 23608ae95f4SNicholas Piggin } else if (tick_do_timer_boot_cpu != -1 && 23708ae95f4SNicholas Piggin !tick_nohz_full_cpu(cpu)) { 23808ae95f4SNicholas Piggin tick_take_do_timer_from_boot(); 23908ae95f4SNicholas Piggin tick_do_timer_boot_cpu = -1; 24008ae95f4SNicholas Piggin WARN_ON(tick_do_timer_cpu != cpu); 24108ae95f4SNicholas Piggin #endif 242906568c9SThomas Gleixner } 243906568c9SThomas Gleixner 244906568c9SThomas Gleixner /* 245906568c9SThomas Gleixner * Startup in periodic mode first. 246906568c9SThomas Gleixner */ 247906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 248906568c9SThomas Gleixner } else { 249906568c9SThomas Gleixner handler = td->evtdev->event_handler; 250906568c9SThomas Gleixner next_event = td->evtdev->next_event; 2517c1e7689SVenkatesh Pallipadi td->evtdev->event_handler = clockevents_handle_noop; 252906568c9SThomas Gleixner } 253906568c9SThomas Gleixner 254906568c9SThomas Gleixner td->evtdev = newdev; 255906568c9SThomas Gleixner 256906568c9SThomas Gleixner /* 257906568c9SThomas Gleixner * When the device is not per cpu, pin the interrupt to the 258906568c9SThomas Gleixner * current cpu: 259906568c9SThomas Gleixner */ 260320ab2b0SRusty Russell if (!cpumask_equal(newdev->cpumask, cpumask)) 2610de26520SRusty Russell irq_set_affinity(newdev->irq, cpumask); 262906568c9SThomas Gleixner 263f8381cbaSThomas Gleixner /* 264f8381cbaSThomas Gleixner * When global broadcasting is active, check if the current 265f8381cbaSThomas Gleixner * device is registered as a placeholder for broadcast mode. 266f8381cbaSThomas Gleixner * This allows us to handle this x86 misfeature in a generic 26707bd1172SThomas Gleixner * way. This function also returns !=0 when we keep the 26807bd1172SThomas Gleixner * current active broadcast state for this CPU. 269f8381cbaSThomas Gleixner */ 270f8381cbaSThomas Gleixner if (tick_device_uses_broadcast(newdev, cpu)) 271f8381cbaSThomas Gleixner return; 272f8381cbaSThomas Gleixner 273906568c9SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 274906568c9SThomas Gleixner tick_setup_periodic(newdev, 0); 27579bf2bb3SThomas Gleixner else 27679bf2bb3SThomas Gleixner tick_setup_oneshot(newdev, handler, next_event); 277906568c9SThomas Gleixner } 278906568c9SThomas Gleixner 27903e13cf5SThomas Gleixner void tick_install_replacement(struct clock_event_device *newdev) 28003e13cf5SThomas Gleixner { 28122127e93SChristoph Lameter struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 28203e13cf5SThomas Gleixner int cpu = smp_processor_id(); 28303e13cf5SThomas Gleixner 28403e13cf5SThomas Gleixner clockevents_exchange_device(td->evtdev, newdev); 28503e13cf5SThomas Gleixner tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 28603e13cf5SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 28703e13cf5SThomas Gleixner tick_oneshot_notify(); 28803e13cf5SThomas Gleixner } 28903e13cf5SThomas Gleixner 29045cb8e01SThomas Gleixner static bool tick_check_percpu(struct clock_event_device *curdev, 29145cb8e01SThomas Gleixner struct clock_event_device *newdev, int cpu) 29245cb8e01SThomas Gleixner { 29345cb8e01SThomas Gleixner if (!cpumask_test_cpu(cpu, newdev->cpumask)) 29445cb8e01SThomas Gleixner return false; 29545cb8e01SThomas Gleixner if (cpumask_equal(newdev->cpumask, cpumask_of(cpu))) 29645cb8e01SThomas Gleixner return true; 29745cb8e01SThomas Gleixner /* Check if irq affinity can be set */ 29845cb8e01SThomas Gleixner if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq)) 29945cb8e01SThomas Gleixner return false; 30045cb8e01SThomas Gleixner /* Prefer an existing cpu local device */ 30145cb8e01SThomas Gleixner if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu))) 30245cb8e01SThomas Gleixner return false; 30345cb8e01SThomas Gleixner return true; 30445cb8e01SThomas Gleixner } 30545cb8e01SThomas Gleixner 30645cb8e01SThomas Gleixner static bool tick_check_preferred(struct clock_event_device *curdev, 30745cb8e01SThomas Gleixner struct clock_event_device *newdev) 30845cb8e01SThomas Gleixner { 30945cb8e01SThomas Gleixner /* Prefer oneshot capable device */ 31045cb8e01SThomas Gleixner if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) { 31145cb8e01SThomas Gleixner if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT)) 31245cb8e01SThomas Gleixner return false; 31345cb8e01SThomas Gleixner if (tick_oneshot_mode_active()) 31445cb8e01SThomas Gleixner return false; 31545cb8e01SThomas Gleixner } 31645cb8e01SThomas Gleixner 31770e5975dSStephen Boyd /* 31870e5975dSStephen Boyd * Use the higher rated one, but prefer a CPU local device with a lower 31970e5975dSStephen Boyd * rating than a non-CPU local device 32070e5975dSStephen Boyd */ 32170e5975dSStephen Boyd return !curdev || 32270e5975dSStephen Boyd newdev->rating > curdev->rating || 3235b5ccbc2SSudeep Holla !cpumask_equal(curdev->cpumask, newdev->cpumask); 32445cb8e01SThomas Gleixner } 32545cb8e01SThomas Gleixner 326906568c9SThomas Gleixner /* 32703e13cf5SThomas Gleixner * Check whether the new device is a better fit than curdev. curdev 32803e13cf5SThomas Gleixner * can be NULL ! 32903e13cf5SThomas Gleixner */ 33003e13cf5SThomas Gleixner bool tick_check_replacement(struct clock_event_device *curdev, 33103e13cf5SThomas Gleixner struct clock_event_device *newdev) 33203e13cf5SThomas Gleixner { 333521c4299SViresh Kumar if (!tick_check_percpu(curdev, newdev, smp_processor_id())) 33403e13cf5SThomas Gleixner return false; 33503e13cf5SThomas Gleixner 33603e13cf5SThomas Gleixner return tick_check_preferred(curdev, newdev); 33703e13cf5SThomas Gleixner } 33803e13cf5SThomas Gleixner 33903e13cf5SThomas Gleixner /* 3407126cac4SThomas Gleixner * Check, if the new registered device should be used. Called with 3417126cac4SThomas Gleixner * clockevents_lock held and interrupts disabled. 342906568c9SThomas Gleixner */ 3437172a286SThomas Gleixner void tick_check_new_device(struct clock_event_device *newdev) 344906568c9SThomas Gleixner { 345906568c9SThomas Gleixner struct clock_event_device *curdev; 346906568c9SThomas Gleixner struct tick_device *td; 3477172a286SThomas Gleixner int cpu; 348906568c9SThomas Gleixner 349906568c9SThomas Gleixner cpu = smp_processor_id(); 350906568c9SThomas Gleixner td = &per_cpu(tick_cpu_device, cpu); 351906568c9SThomas Gleixner curdev = td->evtdev; 352906568c9SThomas Gleixner 353906568c9SThomas Gleixner /* cpu local device ? */ 35445cb8e01SThomas Gleixner if (!tick_check_percpu(curdev, newdev, cpu)) 355906568c9SThomas Gleixner goto out_bc; 356906568c9SThomas Gleixner 35745cb8e01SThomas Gleixner /* Preference decision */ 35845cb8e01SThomas Gleixner if (!tick_check_preferred(curdev, newdev)) 359906568c9SThomas Gleixner goto out_bc; 360906568c9SThomas Gleixner 361ccf33d68SThomas Gleixner if (!try_module_get(newdev->owner)) 362ccf33d68SThomas Gleixner return; 363ccf33d68SThomas Gleixner 364906568c9SThomas Gleixner /* 365906568c9SThomas Gleixner * Replace the eventually existing device by the new 366f8381cbaSThomas Gleixner * device. If the current device is the broadcast device, do 367f8381cbaSThomas Gleixner * not give it back to the clockevents layer ! 368906568c9SThomas Gleixner */ 369f8381cbaSThomas Gleixner if (tick_is_broadcast_device(curdev)) { 3702344abbcSThomas Gleixner clockevents_shutdown(curdev); 371f8381cbaSThomas Gleixner curdev = NULL; 372f8381cbaSThomas Gleixner } 373906568c9SThomas Gleixner clockevents_exchange_device(curdev, newdev); 3746b954823SRusty Russell tick_setup_device(td, newdev, cpu, cpumask_of(cpu)); 37579bf2bb3SThomas Gleixner if (newdev->features & CLOCK_EVT_FEAT_ONESHOT) 37679bf2bb3SThomas Gleixner tick_oneshot_notify(); 3777172a286SThomas Gleixner return; 378f8381cbaSThomas Gleixner 379f8381cbaSThomas Gleixner out_bc: 380f8381cbaSThomas Gleixner /* 381f8381cbaSThomas Gleixner * Can the new device be used as a broadcast device ? 382f8381cbaSThomas Gleixner */ 3837172a286SThomas Gleixner tick_install_broadcast_device(newdev); 384906568c9SThomas Gleixner } 385906568c9SThomas Gleixner 386f32dd117SThomas Gleixner /** 387f32dd117SThomas Gleixner * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode 388f32dd117SThomas Gleixner * @state: The target state (enter/exit) 389f32dd117SThomas Gleixner * 390f32dd117SThomas Gleixner * The system enters/leaves a state, where affected devices might stop 391f32dd117SThomas Gleixner * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups. 392f32dd117SThomas Gleixner * 393f32dd117SThomas Gleixner * Called with interrupts disabled, so clockevents_lock is not 394f32dd117SThomas Gleixner * required here because the local clock event device cannot go away 395f32dd117SThomas Gleixner * under us. 396f32dd117SThomas Gleixner */ 397f32dd117SThomas Gleixner int tick_broadcast_oneshot_control(enum tick_broadcast_state state) 398f32dd117SThomas Gleixner { 399f32dd117SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 400f32dd117SThomas Gleixner 401f32dd117SThomas Gleixner if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP)) 402f32dd117SThomas Gleixner return 0; 403f32dd117SThomas Gleixner 404f32dd117SThomas Gleixner return __tick_broadcast_oneshot_control(state); 405f32dd117SThomas Gleixner } 4060f447051SThomas Gleixner EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control); 407f32dd117SThomas Gleixner 40852c063d1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 409906568c9SThomas Gleixner /* 41094df7de0SSebastien Dugue * Transfer the do_timer job away from a dying cpu. 41194df7de0SSebastien Dugue * 41252c063d1SThomas Gleixner * Called with interrupts disabled. Not locking required. If 41352c063d1SThomas Gleixner * tick_do_timer_cpu is owned by this cpu, nothing can change it. 41494df7de0SSebastien Dugue */ 41552c063d1SThomas Gleixner void tick_handover_do_timer(void) 41694df7de0SSebastien Dugue { 41752c063d1SThomas Gleixner if (tick_do_timer_cpu == smp_processor_id()) { 41894df7de0SSebastien Dugue int cpu = cpumask_first(cpu_online_mask); 41994df7de0SSebastien Dugue 42094df7de0SSebastien Dugue tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu : 42194df7de0SSebastien Dugue TICK_DO_TIMER_NONE; 42294df7de0SSebastien Dugue } 42394df7de0SSebastien Dugue } 42494df7de0SSebastien Dugue 42594df7de0SSebastien Dugue /* 426906568c9SThomas Gleixner * Shutdown an event device on a given cpu: 427906568c9SThomas Gleixner * 428906568c9SThomas Gleixner * This is called on a life CPU, when a CPU is dead. So we cannot 429906568c9SThomas Gleixner * access the hardware device itself. 430906568c9SThomas Gleixner * We just set the mode and remove it from the lists. 431906568c9SThomas Gleixner */ 432a49b116dSThomas Gleixner void tick_shutdown(unsigned int cpu) 433906568c9SThomas Gleixner { 434a49b116dSThomas Gleixner struct tick_device *td = &per_cpu(tick_cpu_device, cpu); 435906568c9SThomas Gleixner struct clock_event_device *dev = td->evtdev; 436906568c9SThomas Gleixner 437906568c9SThomas Gleixner td->mode = TICKDEV_MODE_PERIODIC; 438906568c9SThomas Gleixner if (dev) { 439906568c9SThomas Gleixner /* 440906568c9SThomas Gleixner * Prevent that the clock events layer tries to call 441906568c9SThomas Gleixner * the set mode function! 442906568c9SThomas Gleixner */ 443051ebd10SThomas Gleixner clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED); 444906568c9SThomas Gleixner clockevents_exchange_device(dev, NULL); 4456f7a05d7SThomas Gleixner dev->event_handler = clockevents_handle_noop; 446906568c9SThomas Gleixner td->evtdev = NULL; 447906568c9SThomas Gleixner } 448906568c9SThomas Gleixner } 449a49b116dSThomas Gleixner #endif 450906568c9SThomas Gleixner 4514ffee521SThomas Gleixner /** 452f46481d0SThomas Gleixner * tick_suspend_local - Suspend the local tick device 453f46481d0SThomas Gleixner * 454f46481d0SThomas Gleixner * Called from the local cpu for freeze with interrupts disabled. 455f46481d0SThomas Gleixner * 456f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 457f46481d0SThomas Gleixner */ 4587270d11cSThomas Gleixner void tick_suspend_local(void) 459f46481d0SThomas Gleixner { 460f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 461f46481d0SThomas Gleixner 462f46481d0SThomas Gleixner clockevents_shutdown(td->evtdev); 463f46481d0SThomas Gleixner } 464f46481d0SThomas Gleixner 465f46481d0SThomas Gleixner /** 466f46481d0SThomas Gleixner * tick_resume_local - Resume the local tick device 467f46481d0SThomas Gleixner * 468f46481d0SThomas Gleixner * Called from the local CPU for unfreeze or XEN resume magic. 469f46481d0SThomas Gleixner * 470f46481d0SThomas Gleixner * No locks required. Nothing can change the per cpu device. 471f46481d0SThomas Gleixner */ 472f46481d0SThomas Gleixner void tick_resume_local(void) 473f46481d0SThomas Gleixner { 474f46481d0SThomas Gleixner struct tick_device *td = this_cpu_ptr(&tick_cpu_device); 475f46481d0SThomas Gleixner bool broadcast = tick_resume_check_broadcast(); 476f46481d0SThomas Gleixner 477f46481d0SThomas Gleixner clockevents_tick_resume(td->evtdev); 478f46481d0SThomas Gleixner if (!broadcast) { 479f46481d0SThomas Gleixner if (td->mode == TICKDEV_MODE_PERIODIC) 480f46481d0SThomas Gleixner tick_setup_periodic(td->evtdev, 0); 481f46481d0SThomas Gleixner else 482f46481d0SThomas Gleixner tick_resume_oneshot(); 483f46481d0SThomas Gleixner } 484f46481d0SThomas Gleixner } 485f46481d0SThomas Gleixner 486f46481d0SThomas Gleixner /** 4874ffee521SThomas Gleixner * tick_suspend - Suspend the tick and the broadcast device 4884ffee521SThomas Gleixner * 4894ffee521SThomas Gleixner * Called from syscore_suspend() via timekeeping_suspend with only one 4904ffee521SThomas Gleixner * CPU online and interrupts disabled or from tick_unfreeze() under 4914ffee521SThomas Gleixner * tick_freeze_lock. 4924ffee521SThomas Gleixner * 4934ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 4944ffee521SThomas Gleixner */ 4958c53daf6SThomas Gleixner void tick_suspend(void) 4966321dd60SThomas Gleixner { 497f46481d0SThomas Gleixner tick_suspend_local(); 4984ffee521SThomas Gleixner tick_suspend_broadcast(); 4996321dd60SThomas Gleixner } 5006321dd60SThomas Gleixner 5014ffee521SThomas Gleixner /** 5024ffee521SThomas Gleixner * tick_resume - Resume the tick and the broadcast device 5034ffee521SThomas Gleixner * 5044ffee521SThomas Gleixner * Called from syscore_resume() via timekeeping_resume with only one 505f46481d0SThomas Gleixner * CPU online and interrupts disabled. 5064ffee521SThomas Gleixner * 5074ffee521SThomas Gleixner * No locks required. Nothing can change the per cpu device. 5084ffee521SThomas Gleixner */ 5098c53daf6SThomas Gleixner void tick_resume(void) 5106321dd60SThomas Gleixner { 511f46481d0SThomas Gleixner tick_resume_broadcast(); 512f46481d0SThomas Gleixner tick_resume_local(); 5136321dd60SThomas Gleixner } 5146321dd60SThomas Gleixner 51587e9b9f1SRafael J. Wysocki #ifdef CONFIG_SUSPEND 516124cf911SRafael J. Wysocki static DEFINE_RAW_SPINLOCK(tick_freeze_lock); 517124cf911SRafael J. Wysocki static unsigned int tick_freeze_depth; 518124cf911SRafael J. Wysocki 519124cf911SRafael J. Wysocki /** 520124cf911SRafael J. Wysocki * tick_freeze - Suspend the local tick and (possibly) timekeeping. 521124cf911SRafael J. Wysocki * 522124cf911SRafael J. Wysocki * Check if this is the last online CPU executing the function and if so, 523124cf911SRafael J. Wysocki * suspend timekeeping. Otherwise suspend the local tick. 524124cf911SRafael J. Wysocki * 525124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_unfreeze(). 526124cf911SRafael J. Wysocki * Interrupts must not be enabled before the subsequent %tick_unfreeze(). 527124cf911SRafael J. Wysocki */ 528124cf911SRafael J. Wysocki void tick_freeze(void) 529124cf911SRafael J. Wysocki { 530124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 531124cf911SRafael J. Wysocki 532124cf911SRafael J. Wysocki tick_freeze_depth++; 53375e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) { 53475e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"), 53575e0678eSRafael J. Wysocki smp_processor_id(), true); 536c1a957d1SThomas Gleixner system_state = SYSTEM_SUSPEND; 5373f2552f7SChang-An Chen sched_clock_suspend(); 538124cf911SRafael J. Wysocki timekeeping_suspend(); 53975e0678eSRafael J. Wysocki } else { 540f46481d0SThomas Gleixner tick_suspend_local(); 54175e0678eSRafael J. Wysocki } 542124cf911SRafael J. Wysocki 543124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 544124cf911SRafael J. Wysocki } 545124cf911SRafael J. Wysocki 546124cf911SRafael J. Wysocki /** 547124cf911SRafael J. Wysocki * tick_unfreeze - Resume the local tick and (possibly) timekeeping. 548124cf911SRafael J. Wysocki * 549124cf911SRafael J. Wysocki * Check if this is the first CPU executing the function and if so, resume 550124cf911SRafael J. Wysocki * timekeeping. Otherwise resume the local tick. 551124cf911SRafael J. Wysocki * 552124cf911SRafael J. Wysocki * Call with interrupts disabled. Must be balanced with %tick_freeze(). 553124cf911SRafael J. Wysocki * Interrupts must not be enabled after the preceding %tick_freeze(). 554124cf911SRafael J. Wysocki */ 555124cf911SRafael J. Wysocki void tick_unfreeze(void) 556124cf911SRafael J. Wysocki { 557124cf911SRafael J. Wysocki raw_spin_lock(&tick_freeze_lock); 558124cf911SRafael J. Wysocki 55975e0678eSRafael J. Wysocki if (tick_freeze_depth == num_online_cpus()) { 560124cf911SRafael J. Wysocki timekeeping_resume(); 5613f2552f7SChang-An Chen sched_clock_resume(); 562c1a957d1SThomas Gleixner system_state = SYSTEM_RUNNING; 56375e0678eSRafael J. Wysocki trace_suspend_resume(TPS("timekeeping_freeze"), 56475e0678eSRafael J. Wysocki smp_processor_id(), false); 56575e0678eSRafael J. Wysocki } else { 5665167c506SChunyan Zhang touch_softlockup_watchdog(); 567422fe750SRafael J. Wysocki tick_resume_local(); 56875e0678eSRafael J. Wysocki } 569124cf911SRafael J. Wysocki 570124cf911SRafael J. Wysocki tick_freeze_depth--; 571124cf911SRafael J. Wysocki 572124cf911SRafael J. Wysocki raw_spin_unlock(&tick_freeze_lock); 573124cf911SRafael J. Wysocki } 57487e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */ 575124cf911SRafael J. Wysocki 576906568c9SThomas Gleixner /** 577906568c9SThomas Gleixner * tick_init - initialize the tick control 578906568c9SThomas Gleixner */ 579906568c9SThomas Gleixner void __init tick_init(void) 580906568c9SThomas Gleixner { 581b352bc1cSThomas Gleixner tick_broadcast_init(); 582a80e49e2SFrederic Weisbecker tick_nohz_init(); 583906568c9SThomas Gleixner } 584