blob: df401463a19131dc71b1e395577907c80f7f233c [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Gleixner906568c2007-02-16 01:28:01 -08002/*
Thomas Gleixner906568c2007-02-16 01:28:01 -08003 * This file contains the base functions to manage periodic tick
4 * related events.
5 *
6 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
Thomas Gleixner906568c2007-02-16 01:28:01 -08009 */
10#include <linux/cpu.h>
11#include <linux/err.h>
12#include <linux/hrtimer.h>
Russell Kingd7b90682008-04-17 07:46:24 +020013#include <linux/interrupt.h>
Thomas Gleixner906568c2007-02-16 01:28:01 -080014#include <linux/percpu.h>
15#include <linux/profile.h>
16#include <linux/sched.h>
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000017#include <linux/module.h>
Rafael J. Wysocki75e06782015-05-10 01:23:35 +020018#include <trace/events/power.h>
Thomas Gleixner906568c2007-02-16 01:28:01 -080019
Russell Kingd7b90682008-04-17 07:46:24 +020020#include <asm/irq_regs.h>
21
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080022#include "tick-internal.h"
23
Thomas Gleixner906568c2007-02-16 01:28:01 -080024/*
25 * Tick devices
26 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080027DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
Thomas Gleixner906568c2007-02-16 01:28:01 -080028/*
29 * Tick next event: keeps track of the tick time
30 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -080031ktime_t tick_next_period;
32ktime_t tick_period;
Andrew Morton050ded12013-11-15 14:15:33 -080033
34/*
35 * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
36 * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
37 * variable has two functions:
38 *
39 * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
40 * timekeeping lock all at once. Only the CPU which is assigned to do the
41 * update is handling it.
42 *
43 * 2) Hand off the duty in the NOHZ idle case by setting the value to
44 * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
45 * at it will take over and keep the time keeping alive. The handover
46 * procedure also covers cpu hotplug.
47 */
Thomas Gleixner64414022008-09-22 18:46:37 +020048int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
Thomas Gleixner906568c2007-02-16 01:28:01 -080049
Ingo Molnar289f4802007-02-16 01:28:15 -080050/*
51 * Debugging: see timer_list.c
52 */
53struct tick_device *tick_get_device(int cpu)
54{
55 return &per_cpu(tick_cpu_device, cpu);
56}
57
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080058/**
59 * tick_is_oneshot_available - check for a oneshot capable event device
60 */
61int tick_is_oneshot_available(void)
62{
Christoph Lameter909ea962010-12-08 16:22:55 +010063 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080064
Thomas Gleixner3a142a02011-02-25 22:34:23 +010065 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
66 return 0;
67 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
68 return 1;
69 return tick_broadcast_oneshot_available();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080070}
71
Thomas Gleixner906568c2007-02-16 01:28:01 -080072/*
73 * Periodic tick
74 */
75static void tick_periodic(int cpu)
76{
77 if (tick_do_timer_cpu == cpu) {
John Stultzd6ad4182012-02-28 16:50:11 -080078 write_seqlock(&jiffies_lock);
Thomas Gleixner906568c2007-02-16 01:28:01 -080079
80 /* Keep track of the next tick event */
81 tick_next_period = ktime_add(tick_next_period, tick_period);
82
83 do_timer(1);
John Stultzd6ad4182012-02-28 16:50:11 -080084 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -080085 update_wall_time();
Thomas Gleixner906568c2007-02-16 01:28:01 -080086 }
87
88 update_process_times(user_mode(get_irq_regs()));
89 profile_tick(CPU_PROFILING);
90}
91
92/*
93 * Event handler for periodic ticks
94 */
95void tick_handle_periodic(struct clock_event_device *dev)
96{
97 int cpu = smp_processor_id();
Viresh Kumarb97f0292014-03-25 13:56:23 +053098 ktime_t next = dev->next_event;
Thomas Gleixner906568c2007-02-16 01:28:01 -080099
100 tick_periodic(cpu);
101
Thomas Gleixnerc6eb3f72015-04-14 21:08:51 +0000102#if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
103 /*
104 * The cpu might have transitioned to HIGHRES or NOHZ mode via
105 * update_process_times() -> run_local_timers() ->
106 * hrtimer_run_queues().
107 */
108 if (dev->event_handler != tick_handle_periodic)
109 return;
110#endif
111
Viresh Kumar472c4a92015-05-21 13:33:46 +0530112 if (!clockevent_state_oneshot(dev))
Thomas Gleixner906568c2007-02-16 01:28:01 -0800113 return;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800114 for (;;) {
Viresh Kumarb97f0292014-03-25 13:56:23 +0530115 /*
116 * Setup the next period for devices, which do not have
117 * periodic mode:
118 */
119 next = ktime_add(next, tick_period);
120
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200121 if (!clockevents_program_event(dev, next, false))
Thomas Gleixner906568c2007-02-16 01:28:01 -0800122 return;
john stultz74a03b62009-05-01 13:10:25 -0700123 /*
124 * Have to be careful here. If we're in oneshot mode,
125 * before we call tick_periodic() in a loop, we need
126 * to be sure we're using a real hardware clocksource.
127 * Otherwise we could get trapped in an infinite
128 * loop, as the tick_periodic() increments jiffies,
Viresh Kumarcacb3c72014-03-25 16:09:18 +0530129 * which then will increment time, possibly causing
john stultz74a03b62009-05-01 13:10:25 -0700130 * the loop to trigger again and again.
131 */
132 if (timekeeping_valid_for_hres())
133 tick_periodic(cpu);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800134 }
135}
136
137/*
138 * Setup the device for a periodic tick
139 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800140void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800141{
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800142 tick_set_periodic_handler(dev, broadcast);
143
144 /* Broadcast setup ? */
145 if (!tick_device_is_functional(dev))
146 return;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800147
Thomas Gleixner27ce4cb2008-09-22 19:04:02 +0200148 if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
149 !tick_broadcast_oneshot_active()) {
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200150 clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800151 } else {
152 unsigned long seq;
153 ktime_t next;
154
155 do {
John Stultzd6ad4182012-02-28 16:50:11 -0800156 seq = read_seqbegin(&jiffies_lock);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800157 next = tick_next_period;
John Stultzd6ad4182012-02-28 16:50:11 -0800158 } while (read_seqretry(&jiffies_lock, seq));
Thomas Gleixner906568c2007-02-16 01:28:01 -0800159
Thomas Gleixnerd7eb2312015-06-02 14:08:46 +0200160 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800161
162 for (;;) {
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200163 if (!clockevents_program_event(dev, next, false))
Thomas Gleixner906568c2007-02-16 01:28:01 -0800164 return;
165 next = ktime_add(next, tick_period);
166 }
167 }
168}
169
170/*
171 * Setup the tick device
172 */
173static void tick_setup_device(struct tick_device *td,
174 struct clock_event_device *newdev, int cpu,
Rusty Russell0de26522008-12-13 21:20:26 +1030175 const struct cpumask *cpumask)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800176{
Thomas Gleixner906568c2007-02-16 01:28:01 -0800177 void (*handler)(struct clock_event_device *) = NULL;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100178 ktime_t next_event = 0;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800179
180 /*
181 * First device setup ?
182 */
183 if (!td->evtdev) {
184 /*
185 * If no cpu took the do_timer update, assign it to
186 * this cpu:
187 */
Thomas Gleixner64414022008-09-22 18:46:37 +0200188 if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200189 if (!tick_nohz_full_cpu(cpu))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100190 tick_do_timer_cpu = cpu;
191 else
192 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800193 tick_next_period = ktime_get();
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100194 tick_period = NSEC_PER_SEC / HZ;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800195 }
196
197 /*
198 * Startup in periodic mode first.
199 */
200 td->mode = TICKDEV_MODE_PERIODIC;
201 } else {
202 handler = td->evtdev->event_handler;
203 next_event = td->evtdev->next_event;
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000204 td->evtdev->event_handler = clockevents_handle_noop;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800205 }
206
207 td->evtdev = newdev;
208
209 /*
210 * When the device is not per cpu, pin the interrupt to the
211 * current cpu:
212 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030213 if (!cpumask_equal(newdev->cpumask, cpumask))
Rusty Russell0de26522008-12-13 21:20:26 +1030214 irq_set_affinity(newdev->irq, cpumask);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800215
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800216 /*
217 * When global broadcasting is active, check if the current
218 * device is registered as a placeholder for broadcast mode.
219 * This allows us to handle this x86 misfeature in a generic
Thomas Gleixner07bd1172013-07-01 22:14:10 +0200220 * way. This function also returns !=0 when we keep the
221 * current active broadcast state for this CPU.
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800222 */
223 if (tick_device_uses_broadcast(newdev, cpu))
224 return;
225
Thomas Gleixner906568c2007-02-16 01:28:01 -0800226 if (td->mode == TICKDEV_MODE_PERIODIC)
227 tick_setup_periodic(newdev, 0);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800228 else
229 tick_setup_oneshot(newdev, handler, next_event);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800230}
231
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000232void tick_install_replacement(struct clock_event_device *newdev)
233{
Christoph Lameter22127e92014-08-17 12:30:25 -0500234 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000235 int cpu = smp_processor_id();
236
237 clockevents_exchange_device(td->evtdev, newdev);
238 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
239 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
240 tick_oneshot_notify();
241}
242
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000243static bool tick_check_percpu(struct clock_event_device *curdev,
244 struct clock_event_device *newdev, int cpu)
245{
246 if (!cpumask_test_cpu(cpu, newdev->cpumask))
247 return false;
248 if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
249 return true;
250 /* Check if irq affinity can be set */
251 if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
252 return false;
253 /* Prefer an existing cpu local device */
254 if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
255 return false;
256 return true;
257}
258
259static bool tick_check_preferred(struct clock_event_device *curdev,
260 struct clock_event_device *newdev)
261{
262 /* Prefer oneshot capable device */
263 if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
264 if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
265 return false;
266 if (tick_oneshot_mode_active())
267 return false;
268 }
269
Stephen Boyd70e59752013-06-13 11:39:50 -0700270 /*
271 * Use the higher rated one, but prefer a CPU local device with a lower
272 * rating than a non-CPU local device
273 */
274 return !curdev ||
275 newdev->rating > curdev->rating ||
Sudeep Holla5b5ccbc2018-07-09 16:45:35 +0100276 !cpumask_equal(curdev->cpumask, newdev->cpumask);
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000277}
278
Thomas Gleixner906568c2007-02-16 01:28:01 -0800279/*
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000280 * Check whether the new device is a better fit than curdev. curdev
281 * can be NULL !
282 */
283bool tick_check_replacement(struct clock_event_device *curdev,
284 struct clock_event_device *newdev)
285{
Viresh Kumar521c4292014-04-15 10:54:37 +0530286 if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000287 return false;
288
289 return tick_check_preferred(curdev, newdev);
290}
291
292/*
Thomas Gleixner7126cac2013-04-25 20:31:48 +0000293 * Check, if the new registered device should be used. Called with
294 * clockevents_lock held and interrupts disabled.
Thomas Gleixner906568c2007-02-16 01:28:01 -0800295 */
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000296void tick_check_new_device(struct clock_event_device *newdev)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800297{
298 struct clock_event_device *curdev;
299 struct tick_device *td;
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000300 int cpu;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800301
302 cpu = smp_processor_id();
Thomas Gleixner906568c2007-02-16 01:28:01 -0800303 td = &per_cpu(tick_cpu_device, cpu);
304 curdev = td->evtdev;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800305
306 /* cpu local device ? */
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000307 if (!tick_check_percpu(curdev, newdev, cpu))
308 goto out_bc;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800309
Thomas Gleixner45cb8e02013-04-25 20:31:50 +0000310 /* Preference decision */
311 if (!tick_check_preferred(curdev, newdev))
312 goto out_bc;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800313
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000314 if (!try_module_get(newdev->owner))
315 return;
316
Thomas Gleixner906568c2007-02-16 01:28:01 -0800317 /*
318 * Replace the eventually existing device by the new
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800319 * device. If the current device is the broadcast device, do
320 * not give it back to the clockevents layer !
Thomas Gleixner906568c2007-02-16 01:28:01 -0800321 */
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800322 if (tick_is_broadcast_device(curdev)) {
Thomas Gleixner2344abb2008-09-16 11:32:50 -0700323 clockevents_shutdown(curdev);
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800324 curdev = NULL;
325 }
Thomas Gleixner906568c2007-02-16 01:28:01 -0800326 clockevents_exchange_device(curdev, newdev);
Rusty Russell6b954822009-01-01 10:12:25 +1030327 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800328 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
329 tick_oneshot_notify();
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000330 return;
Thomas Gleixnerf8381cb2007-02-16 01:28:02 -0800331
332out_bc:
333 /*
334 * Can the new device be used as a broadcast device ?
335 */
Thomas Gleixner7172a2862013-04-25 20:31:47 +0000336 tick_install_broadcast_device(newdev);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800337}
338
Thomas Gleixnerf32dd112015-07-07 16:29:38 +0200339/**
340 * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
341 * @state: The target state (enter/exit)
342 *
343 * The system enters/leaves a state, where affected devices might stop
344 * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
345 *
346 * Called with interrupts disabled, so clockevents_lock is not
347 * required here because the local clock event device cannot go away
348 * under us.
349 */
350int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
351{
352 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
353
354 if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
355 return 0;
356
357 return __tick_broadcast_oneshot_control(state);
358}
Thomas Gleixner0f447052015-07-14 12:01:04 +0200359EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
Thomas Gleixnerf32dd112015-07-07 16:29:38 +0200360
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200361#ifdef CONFIG_HOTPLUG_CPU
Thomas Gleixner906568c2007-02-16 01:28:01 -0800362/*
Sebastien Dugue94df7de2008-12-01 14:09:07 +0100363 * Transfer the do_timer job away from a dying cpu.
364 *
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200365 * Called with interrupts disabled. Not locking required. If
366 * tick_do_timer_cpu is owned by this cpu, nothing can change it.
Sebastien Dugue94df7de2008-12-01 14:09:07 +0100367 */
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200368void tick_handover_do_timer(void)
Sebastien Dugue94df7de2008-12-01 14:09:07 +0100369{
Thomas Gleixner52c063d2015-04-03 02:37:24 +0200370 if (tick_do_timer_cpu == smp_processor_id()) {
Sebastien Dugue94df7de2008-12-01 14:09:07 +0100371 int cpu = cpumask_first(cpu_online_mask);
372
373 tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
374 TICK_DO_TIMER_NONE;
375 }
376}
377
378/*
Thomas Gleixner906568c2007-02-16 01:28:01 -0800379 * Shutdown an event device on a given cpu:
380 *
381 * This is called on a life CPU, when a CPU is dead. So we cannot
382 * access the hardware device itself.
383 * We just set the mode and remove it from the lists.
384 */
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200385void tick_shutdown(unsigned int cpu)
Thomas Gleixner906568c2007-02-16 01:28:01 -0800386{
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200387 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800388 struct clock_event_device *dev = td->evtdev;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800389
Thomas Gleixner906568c2007-02-16 01:28:01 -0800390 td->mode = TICKDEV_MODE_PERIODIC;
391 if (dev) {
392 /*
393 * Prevent that the clock events layer tries to call
394 * the set mode function!
395 */
Thomas Gleixner051ebd12015-06-02 14:13:46 +0200396 clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
Thomas Gleixner906568c2007-02-16 01:28:01 -0800397 clockevents_exchange_device(dev, NULL);
Thomas Gleixner6f7a05d2013-04-25 11:45:53 +0200398 dev->event_handler = clockevents_handle_noop;
Thomas Gleixner906568c2007-02-16 01:28:01 -0800399 td->evtdev = NULL;
400 }
Thomas Gleixner906568c2007-02-16 01:28:01 -0800401}
Thomas Gleixnera49b1162015-04-03 02:38:05 +0200402#endif
Thomas Gleixner906568c2007-02-16 01:28:01 -0800403
Thomas Gleixner4ffee522015-03-25 13:09:16 +0100404/**
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100405 * tick_suspend_local - Suspend the local tick device
406 *
407 * Called from the local cpu for freeze with interrupts disabled.
408 *
409 * No locks required. Nothing can change the per cpu device.
410 */
Thomas Gleixner7270d112015-03-25 13:11:52 +0100411void tick_suspend_local(void)
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100412{
413 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
414
415 clockevents_shutdown(td->evtdev);
416}
417
418/**
419 * tick_resume_local - Resume the local tick device
420 *
421 * Called from the local CPU for unfreeze or XEN resume magic.
422 *
423 * No locks required. Nothing can change the per cpu device.
424 */
425void tick_resume_local(void)
426{
427 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
428 bool broadcast = tick_resume_check_broadcast();
429
430 clockevents_tick_resume(td->evtdev);
431 if (!broadcast) {
432 if (td->mode == TICKDEV_MODE_PERIODIC)
433 tick_setup_periodic(td->evtdev, 0);
434 else
435 tick_resume_oneshot();
436 }
437}
438
439/**
Thomas Gleixner4ffee522015-03-25 13:09:16 +0100440 * tick_suspend - Suspend the tick and the broadcast device
441 *
442 * Called from syscore_suspend() via timekeeping_suspend with only one
443 * CPU online and interrupts disabled or from tick_unfreeze() under
444 * tick_freeze_lock.
445 *
446 * No locks required. Nothing can change the per cpu device.
447 */
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000448void tick_suspend(void)
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100449{
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100450 tick_suspend_local();
Thomas Gleixner4ffee522015-03-25 13:09:16 +0100451 tick_suspend_broadcast();
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100452}
453
Thomas Gleixner4ffee522015-03-25 13:09:16 +0100454/**
455 * tick_resume - Resume the tick and the broadcast device
456 *
457 * Called from syscore_resume() via timekeeping_resume with only one
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100458 * CPU online and interrupts disabled.
Thomas Gleixner4ffee522015-03-25 13:09:16 +0100459 *
460 * No locks required. Nothing can change the per cpu device.
461 */
Thomas Gleixner8c53daf2013-04-25 20:31:48 +0000462void tick_resume(void)
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100463{
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100464 tick_resume_broadcast();
465 tick_resume_local();
Thomas Gleixner6321dd62007-03-06 08:25:42 +0100466}
467
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200468#ifdef CONFIG_SUSPEND
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100469static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
470static unsigned int tick_freeze_depth;
471
472/**
473 * tick_freeze - Suspend the local tick and (possibly) timekeeping.
474 *
475 * Check if this is the last online CPU executing the function and if so,
476 * suspend timekeeping. Otherwise suspend the local tick.
477 *
478 * Call with interrupts disabled. Must be balanced with %tick_unfreeze().
479 * Interrupts must not be enabled before the subsequent %tick_unfreeze().
480 */
481void tick_freeze(void)
482{
483 raw_spin_lock(&tick_freeze_lock);
484
485 tick_freeze_depth++;
Rafael J. Wysocki75e06782015-05-10 01:23:35 +0200486 if (tick_freeze_depth == num_online_cpus()) {
487 trace_suspend_resume(TPS("timekeeping_freeze"),
488 smp_processor_id(), true);
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200489 system_state = SYSTEM_SUSPEND;
Chang-An Chen3f2552f2019-03-29 10:59:09 +0800490 sched_clock_suspend();
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100491 timekeeping_suspend();
Rafael J. Wysocki75e06782015-05-10 01:23:35 +0200492 } else {
Thomas Gleixnerf46481d2015-03-25 13:11:04 +0100493 tick_suspend_local();
Rafael J. Wysocki75e06782015-05-10 01:23:35 +0200494 }
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100495
496 raw_spin_unlock(&tick_freeze_lock);
497}
498
499/**
500 * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
501 *
502 * Check if this is the first CPU executing the function and if so, resume
503 * timekeeping. Otherwise resume the local tick.
504 *
505 * Call with interrupts disabled. Must be balanced with %tick_freeze().
506 * Interrupts must not be enabled after the preceding %tick_freeze().
507 */
508void tick_unfreeze(void)
509{
510 raw_spin_lock(&tick_freeze_lock);
511
Rafael J. Wysocki75e06782015-05-10 01:23:35 +0200512 if (tick_freeze_depth == num_online_cpus()) {
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100513 timekeeping_resume();
Chang-An Chen3f2552f2019-03-29 10:59:09 +0800514 sched_clock_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200515 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki75e06782015-05-10 01:23:35 +0200516 trace_suspend_resume(TPS("timekeeping_freeze"),
517 smp_processor_id(), false);
518 } else {
Rafael J. Wysocki422fe752015-04-03 15:21:51 +0200519 tick_resume_local();
Rafael J. Wysocki75e06782015-05-10 01:23:35 +0200520 }
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100521
522 tick_freeze_depth--;
523
524 raw_spin_unlock(&tick_freeze_lock);
525}
Rafael J. Wysocki87e9b9f2015-05-16 01:38:15 +0200526#endif /* CONFIG_SUSPEND */
Rafael J. Wysocki124cf9112015-02-13 23:50:43 +0100527
Thomas Gleixner906568c2007-02-16 01:28:01 -0800528/**
529 * tick_init - initialize the tick control
Thomas Gleixner906568c2007-02-16 01:28:01 -0800530 */
531void __init tick_init(void)
532{
Thomas Gleixnerb352bc12013-03-05 14:25:32 +0100533 tick_broadcast_init();
Frederic Weisbeckera80e49e2014-08-16 17:47:18 +0200534 tick_nohz_init();
Thomas Gleixner906568c2007-02-16 01:28:01 -0800535}