|
| 1 | +/* Copyright (c) 2026 The F9 Microkernel Project. All rights reserved. |
| 2 | + * Use of this source code is governed by a BSD-style license that can be |
| 3 | + * found in the LICENSE file. |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef PLATFORM_IRQ_LATENCY_H_ |
| 7 | +#define PLATFORM_IRQ_LATENCY_H_ |
| 8 | + |
| 9 | +#include <stdint.h> |
| 10 | + |
| 11 | +/* DWT (Data Watchpoint and Trace) registers for cycle counting */ |
| 12 | +#define DWT_CTRL ((volatile uint32_t *) 0xE0001000) |
| 13 | +#define DWT_CYCCNT ((volatile uint32_t *) 0xE0001004) |
| 14 | +#define DWT_CTRL_CYCCNTENA (1 << 0) |
| 15 | + |
| 16 | +#define DEMCR ((volatile uint32_t *) 0xE000EDFC) |
| 17 | +#define DEMCR_TRCENA (1 << 24) |
| 18 | + |
| 19 | +/** |
| 20 | + * @file irq_latency.h |
| 21 | + * @brief Interrupt latency measurement and profiling infrastructure |
| 22 | + * |
| 23 | + * Provides cycle-accurate latency tracking for zero-latency ISRs and |
| 24 | + * standard IRQs. Enables validation of BASEPRI-based zero-latency |
| 25 | + * interrupt performance (<10 cycle target). |
| 26 | + * |
| 27 | + * Usage: |
| 28 | + * 1. Call latency_sample_start() at ISR entry |
| 29 | + * 2. Call latency_sample_end(priority, irq_num) at ISR exit |
| 30 | + * 3. View statistics via KDB 'L' command |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * Latency statistics per interrupt priority level. |
| 35 | + */ |
| 36 | +typedef struct { |
| 37 | + uint32_t count; /* Number of samples */ |
| 38 | + uint32_t min; /* Minimum latency (cycles) */ |
| 39 | + uint32_t max; /* Maximum latency (cycles) */ |
| 40 | + uint32_t sum; /* Sum for average calculation */ |
| 41 | + uint32_t avg; /* Average latency (cycles) */ |
| 42 | +} latency_stats_t; |
| 43 | + |
| 44 | +/** |
| 45 | + * Get current cycle count from DWT_CYCCNT. |
| 46 | + * Returns 0 if DWT is not enabled. |
| 47 | + */ |
| 48 | +static inline uint32_t get_cycle_count(void) |
| 49 | +{ |
| 50 | + return *DWT_CYCCNT; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Enable DWT cycle counter for latency measurements. |
| 55 | + * Called during system initialization. |
| 56 | + */ |
| 57 | +void latency_init(void); |
| 58 | + |
| 59 | +/** |
| 60 | + * Record latency sample for an interrupt. |
| 61 | + * |
| 62 | + * @param priority Interrupt priority (0x0-0xF) |
| 63 | + * @param irq_num IRQ number (-15 to 239) |
| 64 | + * @param cycles Measured latency in cycles |
| 65 | + */ |
| 66 | +void latency_record(uint8_t priority, int16_t irq_num, uint32_t cycles); |
| 67 | + |
| 68 | +/** |
| 69 | + * Get latency statistics for a priority level. |
| 70 | + * |
| 71 | + * @param priority Interrupt priority (0x0-0xF) |
| 72 | + * @return Pointer to statistics structure |
| 73 | + */ |
| 74 | +const latency_stats_t *latency_get_stats(uint8_t priority); |
| 75 | + |
| 76 | +/** |
| 77 | + * Get a best-effort atomic snapshot of latency statistics. |
| 78 | + * |
| 79 | + * Uses relaxed atomics only; intended for diagnostic reads outside ISR |
| 80 | + * context. Returns 1 on success, 0 on invalid input. |
| 81 | + */ |
| 82 | +int latency_get_stats_snapshot(uint8_t priority, latency_stats_t *out); |
| 83 | + |
| 84 | +/** |
| 85 | + * Reset all latency statistics. |
| 86 | + */ |
| 87 | +void latency_reset(void); |
| 88 | + |
| 89 | +/** |
| 90 | + * Get interrupt number from IPSR. |
| 91 | + * Returns 0 for thread mode, 1-15 for exceptions, 16+ for IRQs. |
| 92 | + */ |
| 93 | +static inline uint32_t get_irq_number(void) |
| 94 | +{ |
| 95 | + uint32_t ipsr; |
| 96 | + __asm__ __volatile__("mrs %0, ipsr" : "=r"(ipsr)); |
| 97 | + return ipsr & 0x1FF; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Latency measurement helper - call at ISR entry. |
| 102 | + * Returns timestamp for latency_sample_end(). |
| 103 | + */ |
| 104 | +static inline uint32_t latency_sample_start(void) |
| 105 | +{ |
| 106 | + return get_cycle_count(); |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Latency measurement helper - call at ISR exit. |
| 111 | + * |
| 112 | + * @param start_cycles Timestamp from latency_sample_start() |
| 113 | + * @param priority Interrupt priority level |
| 114 | + * @param irq_num IRQ number from IPSR |
| 115 | + */ |
| 116 | +static inline void latency_sample_end(uint32_t start_cycles, |
| 117 | + uint8_t priority, |
| 118 | + int16_t irq_num) |
| 119 | +{ |
| 120 | + uint32_t end_cycles = get_cycle_count(); |
| 121 | + uint32_t elapsed = end_cycles - start_cycles; |
| 122 | + latency_record(priority, irq_num, elapsed); |
| 123 | +} |
| 124 | + |
| 125 | +#endif /* PLATFORM_IRQ_LATENCY_H_ */ |
0 commit comments