|
8 | 8 | #include <linux/irqdesc.h> |
9 | 9 | #include <linux/kernel_stat.h> |
10 | 10 | #include <linux/pm_runtime.h> |
| 11 | +#include <linux/sched/clock.h> |
11 | 12 |
|
12 | 13 | #ifdef CONFIG_SPARSE_IRQ |
13 | 14 | # define IRQ_BITMAP_BITS (NR_IRQS + 8196) |
|
57 | 58 | IRQS_WAITING = 0x00000080, |
58 | 59 | IRQS_PENDING = 0x00000200, |
59 | 60 | IRQS_SUSPENDED = 0x00000800, |
| 61 | + IRQS_TIMINGS = 0x00001000, |
60 | 62 | }; |
61 | 63 |
|
62 | 64 | #include "debug.h" |
@@ -255,6 +257,94 @@ static inline void |
255 | 257 | irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { } |
256 | 258 | #endif |
257 | 259 |
|
| 260 | +#ifdef CONFIG_IRQ_TIMINGS |
| 261 | + |
| 262 | +#define IRQ_TIMINGS_SHIFT 5 |
| 263 | +#define IRQ_TIMINGS_SIZE (1 << IRQ_TIMINGS_SHIFT) |
| 264 | +#define IRQ_TIMINGS_MASK (IRQ_TIMINGS_SIZE - 1) |
| 265 | + |
| 266 | +/** |
| 267 | + * struct irq_timings - irq timings storing structure |
| 268 | + * @values: a circular buffer of u64 encoded <timestamp,irq> values |
| 269 | + * @count: the number of elements in the array |
| 270 | + */ |
| 271 | +struct irq_timings { |
| 272 | + u64 values[IRQ_TIMINGS_SIZE]; |
| 273 | + int count; |
| 274 | +}; |
| 275 | + |
| 276 | +DECLARE_PER_CPU(struct irq_timings, irq_timings); |
| 277 | + |
| 278 | +static inline void irq_remove_timings(struct irq_desc *desc) |
| 279 | +{ |
| 280 | + desc->istate &= ~IRQS_TIMINGS; |
| 281 | +} |
| 282 | + |
| 283 | +static inline void irq_setup_timings(struct irq_desc *desc, struct irqaction *act) |
| 284 | +{ |
| 285 | + /* |
| 286 | + * We don't need the measurement because the idle code already |
| 287 | + * knows the next expiry event. |
| 288 | + */ |
| 289 | + if (act->flags & __IRQF_TIMER) |
| 290 | + return; |
| 291 | + |
| 292 | + desc->istate |= IRQS_TIMINGS; |
| 293 | +} |
| 294 | + |
| 295 | +extern void irq_timings_enable(void); |
| 296 | +extern void irq_timings_disable(void); |
| 297 | + |
| 298 | +DECLARE_STATIC_KEY_FALSE(irq_timing_enabled); |
| 299 | + |
| 300 | +/* |
| 301 | + * The interrupt number and the timestamp are encoded into a single |
| 302 | + * u64 variable to optimize the size. |
| 303 | + * 48 bit time stamp and 16 bit IRQ number is way sufficient. |
| 304 | + * Who cares an IRQ after 78 hours of idle time? |
| 305 | + */ |
| 306 | +static inline u64 irq_timing_encode(u64 timestamp, int irq) |
| 307 | +{ |
| 308 | + return (timestamp << 16) | irq; |
| 309 | +} |
| 310 | + |
| 311 | +static inline int irq_timing_decode(u64 value, u64 *timestamp) |
| 312 | +{ |
| 313 | + *timestamp = value >> 16; |
| 314 | + return value & U16_MAX; |
| 315 | +} |
| 316 | + |
| 317 | +/* |
| 318 | + * The function record_irq_time is only called in one place in the |
| 319 | + * interrupts handler. We want this function always inline so the code |
| 320 | + * inside is embedded in the function and the static key branching |
| 321 | + * code can act at the higher level. Without the explicit |
| 322 | + * __always_inline we can end up with a function call and a small |
| 323 | + * overhead in the hotpath for nothing. |
| 324 | + */ |
| 325 | +static __always_inline void record_irq_time(struct irq_desc *desc) |
| 326 | +{ |
| 327 | + if (!static_branch_likely(&irq_timing_enabled)) |
| 328 | + return; |
| 329 | + |
| 330 | + if (desc->istate & IRQS_TIMINGS) { |
| 331 | + struct irq_timings *timings = this_cpu_ptr(&irq_timings); |
| 332 | + |
| 333 | + timings->values[timings->count & IRQ_TIMINGS_MASK] = |
| 334 | + irq_timing_encode(local_clock(), |
| 335 | + irq_desc_get_irq(desc)); |
| 336 | + |
| 337 | + timings->count++; |
| 338 | + } |
| 339 | +} |
| 340 | +#else |
| 341 | +static inline void irq_remove_timings(struct irq_desc *desc) {} |
| 342 | +static inline void irq_setup_timings(struct irq_desc *desc, |
| 343 | + struct irqaction *act) {}; |
| 344 | +static inline void record_irq_time(struct irq_desc *desc) {} |
| 345 | +#endif /* CONFIG_IRQ_TIMINGS */ |
| 346 | + |
| 347 | + |
258 | 348 | #ifdef CONFIG_GENERIC_IRQ_CHIP |
259 | 349 | void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name, |
260 | 350 | int num_ct, unsigned int irq_base, |
|
0 commit comments