2 * Copyright (C) 2017 Thomas Gleixner <tglx@linutronix.de>
4 * SPDX-License-Identifier: GPL-2.0
6 #include <linux/spinlock.h>
7 #include <linux/seq_file.h>
8 #include <linux/bitmap.h>
9 #include <linux/percpu.h>
10 #include <linux/cpu.h>
11 #include <linux/irq.h>
13 #define IRQ_MATRIX_SIZE (BITS_TO_LONGS(IRQ_MATRIX_BITS) * sizeof(unsigned long))
16 unsigned int available;
17 unsigned int allocated;
20 unsigned long alloc_map[IRQ_MATRIX_SIZE];
21 unsigned long managed_map[IRQ_MATRIX_SIZE];
25 unsigned int matrix_bits;
26 unsigned int alloc_start;
27 unsigned int alloc_end;
28 unsigned int alloc_size;
29 unsigned int global_available;
30 unsigned int global_reserved;
31 unsigned int systembits_inalloc;
32 unsigned int total_allocated;
33 unsigned int online_maps;
34 struct cpumap __percpu *maps;
35 unsigned long scratch_map[IRQ_MATRIX_SIZE];
36 unsigned long system_map[IRQ_MATRIX_SIZE];
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/irq_matrix.h>
43 * irq_alloc_matrix - Allocate a irq_matrix structure and initialize it
44 * @matrix_bits: Number of matrix bits must be <= IRQ_MATRIX_BITS
45 * @alloc_start: From which bit the allocation search starts
46 * @alloc_end: At which bit the allocation search ends, i.e first
49 __init struct irq_matrix *irq_alloc_matrix(unsigned int matrix_bits,
50 unsigned int alloc_start,
51 unsigned int alloc_end)
55 if (matrix_bits > IRQ_MATRIX_BITS)
58 m = kzalloc(sizeof(*m), GFP_KERNEL);
62 m->matrix_bits = matrix_bits;
63 m->alloc_start = alloc_start;
64 m->alloc_end = alloc_end;
65 m->alloc_size = alloc_end - alloc_start;
66 m->maps = alloc_percpu(*m->maps);
75 * irq_matrix_online - Bring the local CPU matrix online
78 void irq_matrix_online(struct irq_matrix *m)
80 struct cpumap *cm = this_cpu_ptr(m->maps);
84 bitmap_zero(cm->alloc_map, m->matrix_bits);
85 cm->available = m->alloc_size - (cm->managed + m->systembits_inalloc);
87 m->global_available += cm->available;
90 trace_irq_matrix_online(m);
94 * irq_matrix_offline - Bring the local CPU matrix offline
97 void irq_matrix_offline(struct irq_matrix *m)
99 struct cpumap *cm = this_cpu_ptr(m->maps);
101 /* Update the global available size */
102 m->global_available -= cm->available;
105 trace_irq_matrix_offline(m);
108 static unsigned int matrix_alloc_area(struct irq_matrix *m, struct cpumap *cm,
109 unsigned int num, bool managed)
111 unsigned int area, start = m->alloc_start;
112 unsigned int end = m->alloc_end;
114 bitmap_or(m->scratch_map, cm->managed_map, m->system_map, end);
115 bitmap_or(m->scratch_map, m->scratch_map, cm->alloc_map, end);
116 area = bitmap_find_next_zero_area(m->scratch_map, end, start, num, 0);
120 bitmap_set(cm->managed_map, area, num);
122 bitmap_set(cm->alloc_map, area, num);
127 * irq_matrix_assign_system - Assign system wide entry in the matrix
129 * @bit: Which bit to reserve
130 * @replace: Replace an already allocated vector with a system
131 * vector at the same bit position.
133 * The BUG_ON()s below are on purpose. If this goes wrong in the
134 * early boot process, then the chance to survive is about zero.
135 * If this happens when the system is life, it's not much better.
137 void irq_matrix_assign_system(struct irq_matrix *m, unsigned int bit,
140 struct cpumap *cm = this_cpu_ptr(m->maps);
142 BUG_ON(bit > m->matrix_bits);
143 BUG_ON(m->online_maps > 1 || (m->online_maps && !replace));
145 set_bit(bit, m->system_map);
147 BUG_ON(!test_and_clear_bit(bit, cm->alloc_map));
149 m->total_allocated--;
151 if (bit >= m->alloc_start && bit < m->alloc_end)
152 m->systembits_inalloc++;
154 trace_irq_matrix_assign_system(bit, m);
158 * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
160 * @msk: On which CPUs the bits should be reserved.
162 * Can be called for offline CPUs. Note, this will only reserve one bit
163 * on all CPUs in @msk, but it's not guaranteed that the bits are at the
164 * same offset on all CPUs
166 int irq_matrix_reserve_managed(struct irq_matrix *m, const struct cpumask *msk)
168 unsigned int cpu, failed_cpu;
170 for_each_cpu(cpu, msk) {
171 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
174 bit = matrix_alloc_area(m, cm, 1, true);
175 if (bit >= m->alloc_end)
180 m->global_available--;
182 trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
187 for_each_cpu(cpu, msk) {
188 if (cpu == failed_cpu)
190 irq_matrix_remove_managed(m, cpumask_of(cpu));
196 * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
198 * @msk: On which CPUs the bits should be removed
200 * Can be called for offline CPUs
202 * This removes not allocated managed interrupts from the map. It does
203 * not matter which one because the managed interrupts free their
204 * allocation when they shut down. If not, the accounting is screwed,
205 * but all what can be done at this point is warn about it.
207 void irq_matrix_remove_managed(struct irq_matrix *m, const struct cpumask *msk)
211 for_each_cpu(cpu, msk) {
212 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
213 unsigned int bit, end = m->alloc_end;
215 if (WARN_ON_ONCE(!cm->managed))
218 /* Get managed bit which are not allocated */
219 bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
221 bit = find_first_bit(m->scratch_map, end);
222 if (WARN_ON_ONCE(bit >= end))
225 clear_bit(bit, cm->managed_map);
230 m->global_available++;
232 trace_irq_matrix_remove_managed(bit, cpu, m, cm);
237 * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
239 * @cpu: On which CPU the interrupt should be allocated
241 int irq_matrix_alloc_managed(struct irq_matrix *m, unsigned int cpu)
243 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
244 unsigned int bit, end = m->alloc_end;
246 /* Get managed bit which are not allocated */
247 bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
248 bit = find_first_bit(m->scratch_map, end);
251 set_bit(bit, cm->alloc_map);
253 m->total_allocated++;
254 trace_irq_matrix_alloc_managed(bit, cpu, m, cm);
259 * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
261 * @bit: Which bit to mark
263 * This should only be used to mark preallocated vectors
265 void irq_matrix_assign(struct irq_matrix *m, unsigned int bit)
267 struct cpumap *cm = this_cpu_ptr(m->maps);
269 if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
271 if (WARN_ON_ONCE(test_and_set_bit(bit, cm->alloc_map)))
274 m->total_allocated++;
276 m->global_available--;
277 trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
281 * irq_matrix_reserve - Reserve interrupts
284 * This is merily a book keeping call. It increments the number of globally
285 * reserved interrupt bits w/o actually allocating them. This allows to
286 * setup interrupt descriptors w/o assigning low level resources to it.
287 * The actual allocation happens when the interrupt gets activated.
289 void irq_matrix_reserve(struct irq_matrix *m)
291 if (m->global_reserved <= m->global_available &&
292 m->global_reserved + 1 > m->global_available)
293 pr_warn("Interrupt reservation exceeds available resources\n");
295 m->global_reserved++;
296 trace_irq_matrix_reserve(m);
300 * irq_matrix_remove_reserved - Remove interrupt reservation
303 * This is merily a book keeping call. It decrements the number of globally
304 * reserved interrupt bits. This is used to undo irq_matrix_reserve() when the
305 * interrupt was never in use and a real vector allocated, which undid the
308 void irq_matrix_remove_reserved(struct irq_matrix *m)
310 m->global_reserved--;
311 trace_irq_matrix_remove_reserved(m);
315 * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
317 * @msk: Which CPUs to search in
318 * @reserved: Allocate previously reserved interrupts
319 * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
321 int irq_matrix_alloc(struct irq_matrix *m, const struct cpumask *msk,
322 bool reserved, unsigned int *mapped_cpu)
326 for_each_cpu(cpu, msk) {
327 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
333 bit = matrix_alloc_area(m, cm, 1, false);
334 if (bit < m->alloc_end) {
337 m->total_allocated++;
338 m->global_available--;
340 m->global_reserved--;
342 trace_irq_matrix_alloc(bit, cpu, m, cm);
350 * irq_matrix_free - Free allocated interrupt in the matrix
352 * @cpu: Which CPU map needs be updated
353 * @bit: The bit to remove
354 * @managed: If true, the interrupt is managed and not accounted
357 void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
358 unsigned int bit, bool managed)
360 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
362 if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
366 clear_bit(bit, cm->alloc_map);
368 m->total_allocated--;
371 m->global_available++;
374 trace_irq_matrix_free(bit, cpu, m, cm);
378 * irq_matrix_available - Get the number of globally available irqs
379 * @m: Pointer to the matrix to query
380 * @cpudown: If true, the local CPU is about to go down, adjust
381 * the number of available irqs accordingly
383 unsigned int irq_matrix_available(struct irq_matrix *m, bool cpudown)
385 struct cpumap *cm = this_cpu_ptr(m->maps);
387 return (m->global_available - cpudown) ? cm->available : 0;
391 * irq_matrix_reserved - Get the number of globally reserved irqs
392 * @m: Pointer to the matrix to query
394 unsigned int irq_matrix_reserved(struct irq_matrix *m)
396 return m->global_reserved;
400 * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
401 * @m: Pointer to the matrix to search
403 * This returns number of allocated irqs
405 unsigned int irq_matrix_allocated(struct irq_matrix *m)
407 struct cpumap *cm = this_cpu_ptr(m->maps);
409 return cm->allocated;
412 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
414 * irq_matrix_debug_show - Show detailed allocation information
415 * @sf: Pointer to the seq_file to print to
416 * @m: Pointer to the matrix allocator
417 * @ind: Indentation for the print format
419 * Note, this is a lockless snapshot.
421 void irq_matrix_debug_show(struct seq_file *sf, struct irq_matrix *m, int ind)
423 unsigned int nsys = bitmap_weight(m->system_map, m->matrix_bits);
426 seq_printf(sf, "Online bitmaps: %6u\n", m->online_maps);
427 seq_printf(sf, "Global available: %6u\n", m->global_available);
428 seq_printf(sf, "Global reserved: %6u\n", m->global_reserved);
429 seq_printf(sf, "Total allocated: %6u\n", m->total_allocated);
430 seq_printf(sf, "System: %u: %*pbl\n", nsys, m->matrix_bits,
432 seq_printf(sf, "%*s| CPU | avl | man | act | vectors\n", ind, " ");
434 for_each_online_cpu(cpu) {
435 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
437 seq_printf(sf, "%*s %4d %4u %4u %4u %*pbl\n", ind, " ",
438 cpu, cm->available, cm->managed, cm->allocated,
439 m->matrix_bits, cm->alloc_map);