Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This file contains the base functions to manage periodic tick
0004  * related events.
0005  *
0006  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
0007  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
0008  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
0009  */
0010 #include <linux/cpu.h>
0011 #include <linux/err.h>
0012 #include <linux/hrtimer.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/nmi.h>
0015 #include <linux/percpu.h>
0016 #include <linux/profile.h>
0017 #include <linux/sched.h>
0018 #include <linux/module.h>
0019 #include <trace/events/power.h>
0020 
0021 #include <asm/irq_regs.h>
0022 
0023 #include "tick-internal.h"
0024 
0025 /*
0026  * Tick devices
0027  */
0028 DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
0029 /*
0030  * Tick next event: keeps track of the tick time. It's updated by the
0031  * CPU which handles the tick and protected by jiffies_lock. There is
0032  * no requirement to write hold the jiffies seqcount for it.
0033  */
0034 ktime_t tick_next_period;
0035 
0036 /*
0037  * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
0038  * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
0039  * variable has two functions:
0040  *
0041  * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
0042  *    timekeeping lock all at once. Only the CPU which is assigned to do the
0043  *    update is handling it.
0044  *
0045  * 2) Hand off the duty in the NOHZ idle case by setting the value to
0046  *    TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
0047  *    at it will take over and keep the time keeping alive.  The handover
0048  *    procedure also covers cpu hotplug.
0049  */
0050 int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
0051 #ifdef CONFIG_NO_HZ_FULL
0052 /*
0053  * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns
0054  * tick_do_timer_cpu and it should be taken over by an eligible secondary
0055  * when one comes online.
0056  */
0057 static int tick_do_timer_boot_cpu __read_mostly = -1;
0058 #endif
0059 
0060 /*
0061  * Debugging: see timer_list.c
0062  */
0063 struct tick_device *tick_get_device(int cpu)
0064 {
0065     return &per_cpu(tick_cpu_device, cpu);
0066 }
0067 
0068 /**
0069  * tick_is_oneshot_available - check for a oneshot capable event device
0070  */
0071 int tick_is_oneshot_available(void)
0072 {
0073     struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
0074 
0075     if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
0076         return 0;
0077     if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
0078         return 1;
0079     return tick_broadcast_oneshot_available();
0080 }
0081 
0082 /*
0083  * Periodic tick
0084  */
0085 static void tick_periodic(int cpu)
0086 {
0087     if (tick_do_timer_cpu == cpu) {
0088         raw_spin_lock(&jiffies_lock);
0089         write_seqcount_begin(&jiffies_seq);
0090 
0091         /* Keep track of the next tick event */
0092         tick_next_period = ktime_add_ns(tick_next_period, TICK_NSEC);
0093 
0094         do_timer(1);
0095         write_seqcount_end(&jiffies_seq);
0096         raw_spin_unlock(&jiffies_lock);
0097         update_wall_time();
0098     }
0099 
0100     update_process_times(user_mode(get_irq_regs()));
0101     profile_tick(CPU_PROFILING);
0102 }
0103 
0104 /*
0105  * Event handler for periodic ticks
0106  */
0107 void tick_handle_periodic(struct clock_event_device *dev)
0108 {
0109     int cpu = smp_processor_id();
0110     ktime_t next = dev->next_event;
0111 
0112     tick_periodic(cpu);
0113 
0114 #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
0115     /*
0116      * The cpu might have transitioned to HIGHRES or NOHZ mode via
0117      * update_process_times() -> run_local_timers() ->
0118      * hrtimer_run_queues().
0119      */
0120     if (dev->event_handler != tick_handle_periodic)
0121         return;
0122 #endif
0123 
0124     if (!clockevent_state_oneshot(dev))
0125         return;
0126     for (;;) {
0127         /*
0128          * Setup the next period for devices, which do not have
0129          * periodic mode:
0130          */
0131         next = ktime_add_ns(next, TICK_NSEC);
0132 
0133         if (!clockevents_program_event(dev, next, false))
0134             return;
0135         /*
0136          * Have to be careful here. If we're in oneshot mode,
0137          * before we call tick_periodic() in a loop, we need
0138          * to be sure we're using a real hardware clocksource.
0139          * Otherwise we could get trapped in an infinite
0140          * loop, as the tick_periodic() increments jiffies,
0141          * which then will increment time, possibly causing
0142          * the loop to trigger again and again.
0143          */
0144         if (timekeeping_valid_for_hres())
0145             tick_periodic(cpu);
0146     }
0147 }
0148 
0149 /*
0150  * Setup the device for a periodic tick
0151  */
0152 void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
0153 {
0154     tick_set_periodic_handler(dev, broadcast);
0155 
0156     /* Broadcast setup ? */
0157     if (!tick_device_is_functional(dev))
0158         return;
0159 
0160     if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
0161         !tick_broadcast_oneshot_active()) {
0162         clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
0163     } else {
0164         unsigned int seq;
0165         ktime_t next;
0166 
0167         do {
0168             seq = read_seqcount_begin(&jiffies_seq);
0169             next = tick_next_period;
0170         } while (read_seqcount_retry(&jiffies_seq, seq));
0171 
0172         clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
0173 
0174         for (;;) {
0175             if (!clockevents_program_event(dev, next, false))
0176                 return;
0177             next = ktime_add_ns(next, TICK_NSEC);
0178         }
0179     }
0180 }
0181 
0182 #ifdef CONFIG_NO_HZ_FULL
0183 static void giveup_do_timer(void *info)
0184 {
0185     int cpu = *(unsigned int *)info;
0186 
0187     WARN_ON(tick_do_timer_cpu != smp_processor_id());
0188 
0189     tick_do_timer_cpu = cpu;
0190 }
0191 
0192 static void tick_take_do_timer_from_boot(void)
0193 {
0194     int cpu = smp_processor_id();
0195     int from = tick_do_timer_boot_cpu;
0196 
0197     if (from >= 0 && from != cpu)
0198         smp_call_function_single(from, giveup_do_timer, &cpu, 1);
0199 }
0200 #endif
0201 
0202 /*
0203  * Setup the tick device
0204  */
0205 static void tick_setup_device(struct tick_device *td,
0206                   struct clock_event_device *newdev, int cpu,
0207                   const struct cpumask *cpumask)
0208 {
0209     void (*handler)(struct clock_event_device *) = NULL;
0210     ktime_t next_event = 0;
0211 
0212     /*
0213      * First device setup ?
0214      */
0215     if (!td->evtdev) {
0216         /*
0217          * If no cpu took the do_timer update, assign it to
0218          * this cpu:
0219          */
0220         if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
0221             tick_do_timer_cpu = cpu;
0222 
0223             tick_next_period = ktime_get();
0224 #ifdef CONFIG_NO_HZ_FULL
0225             /*
0226              * The boot CPU may be nohz_full, in which case set
0227              * tick_do_timer_boot_cpu so the first housekeeping
0228              * secondary that comes up will take do_timer from
0229              * us.
0230              */
0231             if (tick_nohz_full_cpu(cpu))
0232                 tick_do_timer_boot_cpu = cpu;
0233 
0234         } else if (tick_do_timer_boot_cpu != -1 &&
0235                         !tick_nohz_full_cpu(cpu)) {
0236             tick_take_do_timer_from_boot();
0237             tick_do_timer_boot_cpu = -1;
0238             WARN_ON(tick_do_timer_cpu != cpu);
0239 #endif
0240         }
0241 
0242         /*
0243          * Startup in periodic mode first.
0244          */
0245         td->mode = TICKDEV_MODE_PERIODIC;
0246     } else {
0247         handler = td->evtdev->event_handler;
0248         next_event = td->evtdev->next_event;
0249         td->evtdev->event_handler = clockevents_handle_noop;
0250     }
0251 
0252     td->evtdev = newdev;
0253 
0254     /*
0255      * When the device is not per cpu, pin the interrupt to the
0256      * current cpu:
0257      */
0258     if (!cpumask_equal(newdev->cpumask, cpumask))
0259         irq_set_affinity(newdev->irq, cpumask);
0260 
0261     /*
0262      * When global broadcasting is active, check if the current
0263      * device is registered as a placeholder for broadcast mode.
0264      * This allows us to handle this x86 misfeature in a generic
0265      * way. This function also returns !=0 when we keep the
0266      * current active broadcast state for this CPU.
0267      */
0268     if (tick_device_uses_broadcast(newdev, cpu))
0269         return;
0270 
0271     if (td->mode == TICKDEV_MODE_PERIODIC)
0272         tick_setup_periodic(newdev, 0);
0273     else
0274         tick_setup_oneshot(newdev, handler, next_event);
0275 }
0276 
0277 void tick_install_replacement(struct clock_event_device *newdev)
0278 {
0279     struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
0280     int cpu = smp_processor_id();
0281 
0282     clockevents_exchange_device(td->evtdev, newdev);
0283     tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
0284     if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
0285         tick_oneshot_notify();
0286 }
0287 
0288 static bool tick_check_percpu(struct clock_event_device *curdev,
0289                   struct clock_event_device *newdev, int cpu)
0290 {
0291     if (!cpumask_test_cpu(cpu, newdev->cpumask))
0292         return false;
0293     if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
0294         return true;
0295     /* Check if irq affinity can be set */
0296     if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
0297         return false;
0298     /* Prefer an existing cpu local device */
0299     if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
0300         return false;
0301     return true;
0302 }
0303 
0304 static bool tick_check_preferred(struct clock_event_device *curdev,
0305                  struct clock_event_device *newdev)
0306 {
0307     /* Prefer oneshot capable device */
0308     if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
0309         if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
0310             return false;
0311         if (tick_oneshot_mode_active())
0312             return false;
0313     }
0314 
0315     /*
0316      * Use the higher rated one, but prefer a CPU local device with a lower
0317      * rating than a non-CPU local device
0318      */
0319     return !curdev ||
0320         newdev->rating > curdev->rating ||
0321            !cpumask_equal(curdev->cpumask, newdev->cpumask);
0322 }
0323 
0324 /*
0325  * Check whether the new device is a better fit than curdev. curdev
0326  * can be NULL !
0327  */
0328 bool tick_check_replacement(struct clock_event_device *curdev,
0329                 struct clock_event_device *newdev)
0330 {
0331     if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
0332         return false;
0333 
0334     return tick_check_preferred(curdev, newdev);
0335 }
0336 
0337 /*
0338  * Check, if the new registered device should be used. Called with
0339  * clockevents_lock held and interrupts disabled.
0340  */
0341 void tick_check_new_device(struct clock_event_device *newdev)
0342 {
0343     struct clock_event_device *curdev;
0344     struct tick_device *td;
0345     int cpu;
0346 
0347     cpu = smp_processor_id();
0348     td = &per_cpu(tick_cpu_device, cpu);
0349     curdev = td->evtdev;
0350 
0351     if (!tick_check_replacement(curdev, newdev))
0352         goto out_bc;
0353 
0354     if (!try_module_get(newdev->owner))
0355         return;
0356 
0357     /*
0358      * Replace the eventually existing device by the new
0359      * device. If the current device is the broadcast device, do
0360      * not give it back to the clockevents layer !
0361      */
0362     if (tick_is_broadcast_device(curdev)) {
0363         clockevents_shutdown(curdev);
0364         curdev = NULL;
0365     }
0366     clockevents_exchange_device(curdev, newdev);
0367     tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
0368     if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
0369         tick_oneshot_notify();
0370     return;
0371 
0372 out_bc:
0373     /*
0374      * Can the new device be used as a broadcast device ?
0375      */
0376     tick_install_broadcast_device(newdev, cpu);
0377 }
0378 
0379 /**
0380  * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
0381  * @state:  The target state (enter/exit)
0382  *
0383  * The system enters/leaves a state, where affected devices might stop
0384  * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
0385  *
0386  * Called with interrupts disabled, so clockevents_lock is not
0387  * required here because the local clock event device cannot go away
0388  * under us.
0389  */
0390 int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
0391 {
0392     struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
0393 
0394     if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
0395         return 0;
0396 
0397     return __tick_broadcast_oneshot_control(state);
0398 }
0399 EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
0400 
0401 #ifdef CONFIG_HOTPLUG_CPU
0402 /*
0403  * Transfer the do_timer job away from a dying cpu.
0404  *
0405  * Called with interrupts disabled. No locking required. If
0406  * tick_do_timer_cpu is owned by this cpu, nothing can change it.
0407  */
0408 void tick_handover_do_timer(void)
0409 {
0410     if (tick_do_timer_cpu == smp_processor_id())
0411         tick_do_timer_cpu = cpumask_first(cpu_online_mask);
0412 }
0413 
0414 /*
0415  * Shutdown an event device on a given cpu:
0416  *
0417  * This is called on a life CPU, when a CPU is dead. So we cannot
0418  * access the hardware device itself.
0419  * We just set the mode and remove it from the lists.
0420  */
0421 void tick_shutdown(unsigned int cpu)
0422 {
0423     struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
0424     struct clock_event_device *dev = td->evtdev;
0425 
0426     td->mode = TICKDEV_MODE_PERIODIC;
0427     if (dev) {
0428         /*
0429          * Prevent that the clock events layer tries to call
0430          * the set mode function!
0431          */
0432         clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
0433         clockevents_exchange_device(dev, NULL);
0434         dev->event_handler = clockevents_handle_noop;
0435         td->evtdev = NULL;
0436     }
0437 }
0438 #endif
0439 
0440 /**
0441  * tick_suspend_local - Suspend the local tick device
0442  *
0443  * Called from the local cpu for freeze with interrupts disabled.
0444  *
0445  * No locks required. Nothing can change the per cpu device.
0446  */
0447 void tick_suspend_local(void)
0448 {
0449     struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
0450 
0451     clockevents_shutdown(td->evtdev);
0452 }
0453 
0454 /**
0455  * tick_resume_local - Resume the local tick device
0456  *
0457  * Called from the local CPU for unfreeze or XEN resume magic.
0458  *
0459  * No locks required. Nothing can change the per cpu device.
0460  */
0461 void tick_resume_local(void)
0462 {
0463     struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
0464     bool broadcast = tick_resume_check_broadcast();
0465 
0466     clockevents_tick_resume(td->evtdev);
0467     if (!broadcast) {
0468         if (td->mode == TICKDEV_MODE_PERIODIC)
0469             tick_setup_periodic(td->evtdev, 0);
0470         else
0471             tick_resume_oneshot();
0472     }
0473 
0474     /*
0475      * Ensure that hrtimers are up to date and the clockevents device
0476      * is reprogrammed correctly when high resolution timers are
0477      * enabled.
0478      */
0479     hrtimers_resume_local();
0480 }
0481 
0482 /**
0483  * tick_suspend - Suspend the tick and the broadcast device
0484  *
0485  * Called from syscore_suspend() via timekeeping_suspend with only one
0486  * CPU online and interrupts disabled or from tick_unfreeze() under
0487  * tick_freeze_lock.
0488  *
0489  * No locks required. Nothing can change the per cpu device.
0490  */
0491 void tick_suspend(void)
0492 {
0493     tick_suspend_local();
0494     tick_suspend_broadcast();
0495 }
0496 
0497 /**
0498  * tick_resume - Resume the tick and the broadcast device
0499  *
0500  * Called from syscore_resume() via timekeeping_resume with only one
0501  * CPU online and interrupts disabled.
0502  *
0503  * No locks required. Nothing can change the per cpu device.
0504  */
0505 void tick_resume(void)
0506 {
0507     tick_resume_broadcast();
0508     tick_resume_local();
0509 }
0510 
0511 #ifdef CONFIG_SUSPEND
0512 static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
0513 static unsigned int tick_freeze_depth;
0514 
0515 /**
0516  * tick_freeze - Suspend the local tick and (possibly) timekeeping.
0517  *
0518  * Check if this is the last online CPU executing the function and if so,
0519  * suspend timekeeping.  Otherwise suspend the local tick.
0520  *
0521  * Call with interrupts disabled.  Must be balanced with %tick_unfreeze().
0522  * Interrupts must not be enabled before the subsequent %tick_unfreeze().
0523  */
0524 void tick_freeze(void)
0525 {
0526     raw_spin_lock(&tick_freeze_lock);
0527 
0528     tick_freeze_depth++;
0529     if (tick_freeze_depth == num_online_cpus()) {
0530         trace_suspend_resume(TPS("timekeeping_freeze"),
0531                      smp_processor_id(), true);
0532         system_state = SYSTEM_SUSPEND;
0533         sched_clock_suspend();
0534         timekeeping_suspend();
0535     } else {
0536         tick_suspend_local();
0537     }
0538 
0539     raw_spin_unlock(&tick_freeze_lock);
0540 }
0541 
0542 /**
0543  * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
0544  *
0545  * Check if this is the first CPU executing the function and if so, resume
0546  * timekeeping.  Otherwise resume the local tick.
0547  *
0548  * Call with interrupts disabled.  Must be balanced with %tick_freeze().
0549  * Interrupts must not be enabled after the preceding %tick_freeze().
0550  */
0551 void tick_unfreeze(void)
0552 {
0553     raw_spin_lock(&tick_freeze_lock);
0554 
0555     if (tick_freeze_depth == num_online_cpus()) {
0556         timekeeping_resume();
0557         sched_clock_resume();
0558         system_state = SYSTEM_RUNNING;
0559         trace_suspend_resume(TPS("timekeeping_freeze"),
0560                      smp_processor_id(), false);
0561     } else {
0562         touch_softlockup_watchdog();
0563         tick_resume_local();
0564     }
0565 
0566     tick_freeze_depth--;
0567 
0568     raw_spin_unlock(&tick_freeze_lock);
0569 }
0570 #endif /* CONFIG_SUSPEND */
0571 
0572 /**
0573  * tick_init - initialize the tick control
0574  */
0575 void __init tick_init(void)
0576 {
0577     tick_broadcast_init();
0578     tick_nohz_init();
0579 }