]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/time/tick-common.c
[PATCH] Add debugging feature /proc/timer_stat
[mirror_ubuntu-jammy-kernel.git] / kernel / time / tick-common.c
CommitLineData
906568c9
TG
1/*
2 * linux/kernel/time/tick-common.c
3 *
4 * This file contains the base functions to manage periodic tick
5 * related events.
6 *
7 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
8 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
9 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
10 *
11 * This code is licenced under the GPL version 2. For details see
12 * kernel-base/COPYING.
13 */
14#include <linux/cpu.h>
15#include <linux/err.h>
16#include <linux/hrtimer.h>
17#include <linux/irq.h>
18#include <linux/percpu.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
21#include <linux/tick.h>
22
f8381cba
TG
23#include "tick-internal.h"
24
906568c9
TG
25/*
26 * Tick devices
27 */
f8381cba 28DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
906568c9
TG
29/*
30 * Tick next event: keeps track of the tick time
31 */
f8381cba
TG
32ktime_t tick_next_period;
33ktime_t tick_period;
906568c9 34static int tick_do_timer_cpu = -1;
f8381cba 35DEFINE_SPINLOCK(tick_device_lock);
906568c9 36
79bf2bb3
TG
37/**
38 * tick_is_oneshot_available - check for a oneshot capable event device
39 */
40int tick_is_oneshot_available(void)
41{
42 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
43
44 return dev && (dev->features & CLOCK_EVT_FEAT_ONESHOT);
45}
46
906568c9
TG
47/*
48 * Periodic tick
49 */
50static void tick_periodic(int cpu)
51{
52 if (tick_do_timer_cpu == cpu) {
53 write_seqlock(&xtime_lock);
54
55 /* Keep track of the next tick event */
56 tick_next_period = ktime_add(tick_next_period, tick_period);
57
58 do_timer(1);
59 write_sequnlock(&xtime_lock);
60 }
61
62 update_process_times(user_mode(get_irq_regs()));
63 profile_tick(CPU_PROFILING);
64}
65
66/*
67 * Event handler for periodic ticks
68 */
69void tick_handle_periodic(struct clock_event_device *dev)
70{
71 int cpu = smp_processor_id();
72
73 tick_periodic(cpu);
74
75 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
76 return;
77 /*
78 * Setup the next period for devices, which do not have
79 * periodic mode:
80 */
81 for (;;) {
82 ktime_t next = ktime_add(dev->next_event, tick_period);
83
84 if (!clockevents_program_event(dev, next, ktime_get()))
85 return;
86 tick_periodic(cpu);
87 }
88}
89
90/*
91 * Setup the device for a periodic tick
92 */
f8381cba 93void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
906568c9 94{
f8381cba
TG
95 tick_set_periodic_handler(dev, broadcast);
96
97 /* Broadcast setup ? */
98 if (!tick_device_is_functional(dev))
99 return;
906568c9
TG
100
101 if (dev->features & CLOCK_EVT_FEAT_PERIODIC) {
102 clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
103 } else {
104 unsigned long seq;
105 ktime_t next;
106
107 do {
108 seq = read_seqbegin(&xtime_lock);
109 next = tick_next_period;
110 } while (read_seqretry(&xtime_lock, seq));
111
112 clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
113
114 for (;;) {
115 if (!clockevents_program_event(dev, next, ktime_get()))
116 return;
117 next = ktime_add(next, tick_period);
118 }
119 }
120}
121
122/*
123 * Setup the tick device
124 */
125static void tick_setup_device(struct tick_device *td,
126 struct clock_event_device *newdev, int cpu,
127 cpumask_t cpumask)
128{
129 ktime_t next_event;
130 void (*handler)(struct clock_event_device *) = NULL;
131
132 /*
133 * First device setup ?
134 */
135 if (!td->evtdev) {
136 /*
137 * If no cpu took the do_timer update, assign it to
138 * this cpu:
139 */
140 if (tick_do_timer_cpu == -1) {
141 tick_do_timer_cpu = cpu;
142 tick_next_period = ktime_get();
143 tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
144 }
145
146 /*
147 * Startup in periodic mode first.
148 */
149 td->mode = TICKDEV_MODE_PERIODIC;
150 } else {
151 handler = td->evtdev->event_handler;
152 next_event = td->evtdev->next_event;
153 }
154
155 td->evtdev = newdev;
156
157 /*
158 * When the device is not per cpu, pin the interrupt to the
159 * current cpu:
160 */
161 if (!cpus_equal(newdev->cpumask, cpumask))
162 irq_set_affinity(newdev->irq, cpumask);
163
f8381cba
TG
164 /*
165 * When global broadcasting is active, check if the current
166 * device is registered as a placeholder for broadcast mode.
167 * This allows us to handle this x86 misfeature in a generic
168 * way.
169 */
170 if (tick_device_uses_broadcast(newdev, cpu))
171 return;
172
906568c9
TG
173 if (td->mode == TICKDEV_MODE_PERIODIC)
174 tick_setup_periodic(newdev, 0);
79bf2bb3
TG
175 else
176 tick_setup_oneshot(newdev, handler, next_event);
906568c9
TG
177}
178
179/*
180 * Check, if the new registered device should be used.
181 */
182static int tick_check_new_device(struct clock_event_device *newdev)
183{
184 struct clock_event_device *curdev;
185 struct tick_device *td;
186 int cpu, ret = NOTIFY_OK;
187 unsigned long flags;
188 cpumask_t cpumask;
189
190 spin_lock_irqsave(&tick_device_lock, flags);
191
192 cpu = smp_processor_id();
193 if (!cpu_isset(cpu, newdev->cpumask))
194 goto out;
195
196 td = &per_cpu(tick_cpu_device, cpu);
197 curdev = td->evtdev;
198 cpumask = cpumask_of_cpu(cpu);
199
200 /* cpu local device ? */
201 if (!cpus_equal(newdev->cpumask, cpumask)) {
202
203 /*
204 * If the cpu affinity of the device interrupt can not
205 * be set, ignore it.
206 */
207 if (!irq_can_set_affinity(newdev->irq))
208 goto out_bc;
209
210 /*
211 * If we have a cpu local device already, do not replace it
212 * by a non cpu local device
213 */
214 if (curdev && cpus_equal(curdev->cpumask, cpumask))
215 goto out_bc;
216 }
217
218 /*
219 * If we have an active device, then check the rating and the oneshot
220 * feature.
221 */
222 if (curdev) {
79bf2bb3
TG
223 /*
224 * Prefer one shot capable devices !
225 */
226 if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) &&
227 !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
228 goto out_bc;
906568c9
TG
229 /*
230 * Check the rating
231 */
232 if (curdev->rating >= newdev->rating)
f8381cba 233 goto out_bc;
906568c9
TG
234 }
235
236 /*
237 * Replace the eventually existing device by the new
f8381cba
TG
238 * device. If the current device is the broadcast device, do
239 * not give it back to the clockevents layer !
906568c9 240 */
f8381cba
TG
241 if (tick_is_broadcast_device(curdev)) {
242 clockevents_set_mode(curdev, CLOCK_EVT_MODE_SHUTDOWN);
243 curdev = NULL;
244 }
906568c9
TG
245 clockevents_exchange_device(curdev, newdev);
246 tick_setup_device(td, newdev, cpu, cpumask);
79bf2bb3
TG
247 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
248 tick_oneshot_notify();
906568c9 249
f8381cba
TG
250 spin_unlock_irqrestore(&tick_device_lock, flags);
251 return NOTIFY_STOP;
252
253out_bc:
254 /*
255 * Can the new device be used as a broadcast device ?
256 */
257 if (tick_check_broadcast_device(newdev))
258 ret = NOTIFY_STOP;
906568c9
TG
259out:
260 spin_unlock_irqrestore(&tick_device_lock, flags);
f8381cba 261
906568c9
TG
262 return ret;
263}
264
265/*
266 * Shutdown an event device on a given cpu:
267 *
268 * This is called on a life CPU, when a CPU is dead. So we cannot
269 * access the hardware device itself.
270 * We just set the mode and remove it from the lists.
271 */
272static void tick_shutdown(unsigned int *cpup)
273{
274 struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
275 struct clock_event_device *dev = td->evtdev;
276 unsigned long flags;
277
278 spin_lock_irqsave(&tick_device_lock, flags);
279 td->mode = TICKDEV_MODE_PERIODIC;
280 if (dev) {
281 /*
282 * Prevent that the clock events layer tries to call
283 * the set mode function!
284 */
285 dev->mode = CLOCK_EVT_MODE_UNUSED;
286 clockevents_exchange_device(dev, NULL);
287 td->evtdev = NULL;
288 }
289 spin_unlock_irqrestore(&tick_device_lock, flags);
290}
291
292/*
293 * Notification about clock event devices
294 */
295static int tick_notify(struct notifier_block *nb, unsigned long reason,
296 void *dev)
297{
298 switch (reason) {
299
300 case CLOCK_EVT_NOTIFY_ADD:
301 return tick_check_new_device(dev);
302
f8381cba
TG
303 case CLOCK_EVT_NOTIFY_BROADCAST_ON:
304 case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
305 tick_broadcast_on_off(reason, dev);
306 break;
307
79bf2bb3
TG
308 case CLOCK_EVT_NOTIFY_BROADCAST_ENTER:
309 case CLOCK_EVT_NOTIFY_BROADCAST_EXIT:
310 tick_broadcast_oneshot_control(reason);
311 break;
312
906568c9 313 case CLOCK_EVT_NOTIFY_CPU_DEAD:
79bf2bb3 314 tick_shutdown_broadcast_oneshot(dev);
f8381cba 315 tick_shutdown_broadcast(dev);
906568c9
TG
316 tick_shutdown(dev);
317 break;
318
319 default:
320 break;
321 }
322
323 return NOTIFY_OK;
324}
325
326static struct notifier_block tick_notifier = {
327 .notifier_call = tick_notify,
328};
329
330/**
331 * tick_init - initialize the tick control
332 *
333 * Register the notifier with the clockevents framework
334 */
335void __init tick_init(void)
336{
337 clockevents_register_notifier(&tick_notifier);
338}