1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2017 Thomas Gleixner <tglx@linutronix.de>
4 #include <linux/spinlock.h>
5 #include <linux/seq_file.h>
6 #include <linux/bitmap.h>
7 #include <linux/percpu.h>
12 unsigned int available
;
13 unsigned int allocated
;
15 unsigned int managed_allocated
;
18 unsigned long *managed_map
;
19 unsigned long alloc_map
[];
23 unsigned int matrix_bits
;
24 unsigned int alloc_start
;
25 unsigned int alloc_end
;
26 unsigned int alloc_size
;
27 unsigned int global_available
;
28 unsigned int global_reserved
;
29 unsigned int systembits_inalloc
;
30 unsigned int total_allocated
;
31 unsigned int online_maps
;
32 struct cpumap __percpu
*maps
;
33 unsigned long *system_map
;
34 unsigned long scratch_map
[];
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/irq_matrix.h>
41 * irq_alloc_matrix - Allocate a irq_matrix structure and initialize it
42 * @matrix_bits: Number of matrix bits must be <= IRQ_MATRIX_BITS
43 * @alloc_start: From which bit the allocation search starts
44 * @alloc_end: At which bit the allocation search ends, i.e first
47 __init
struct irq_matrix
*irq_alloc_matrix(unsigned int matrix_bits
,
48 unsigned int alloc_start
,
49 unsigned int alloc_end
)
51 unsigned int cpu
, matrix_size
= BITS_TO_LONGS(matrix_bits
);
54 m
= kzalloc(struct_size(m
, scratch_map
, matrix_size
* 2), GFP_KERNEL
);
58 m
->system_map
= &m
->scratch_map
[matrix_size
];
60 m
->matrix_bits
= matrix_bits
;
61 m
->alloc_start
= alloc_start
;
62 m
->alloc_end
= alloc_end
;
63 m
->alloc_size
= alloc_end
- alloc_start
;
64 m
->maps
= __alloc_percpu(struct_size(m
->maps
, alloc_map
, matrix_size
* 2),
65 __alignof__(*m
->maps
));
71 for_each_possible_cpu(cpu
) {
72 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
74 cm
->managed_map
= &cm
->alloc_map
[matrix_size
];
81 * irq_matrix_online - Bring the local CPU matrix online
84 void irq_matrix_online(struct irq_matrix
*m
)
86 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
90 if (!cm
->initialized
) {
91 cm
->available
= m
->alloc_size
;
92 cm
->available
-= cm
->managed
+ m
->systembits_inalloc
;
93 cm
->initialized
= true;
95 m
->global_available
+= cm
->available
;
98 trace_irq_matrix_online(m
);
102 * irq_matrix_offline - Bring the local CPU matrix offline
105 void irq_matrix_offline(struct irq_matrix
*m
)
107 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
109 /* Update the global available size */
110 m
->global_available
-= cm
->available
;
113 trace_irq_matrix_offline(m
);
116 static unsigned int matrix_alloc_area(struct irq_matrix
*m
, struct cpumap
*cm
,
117 unsigned int num
, bool managed
)
119 unsigned int area
, start
= m
->alloc_start
;
120 unsigned int end
= m
->alloc_end
;
122 bitmap_or(m
->scratch_map
, cm
->managed_map
, m
->system_map
, end
);
123 bitmap_or(m
->scratch_map
, m
->scratch_map
, cm
->alloc_map
, end
);
124 area
= bitmap_find_next_zero_area(m
->scratch_map
, end
, start
, num
, 0);
128 bitmap_set(cm
->managed_map
, area
, num
);
130 bitmap_set(cm
->alloc_map
, area
, num
);
134 /* Find the best CPU which has the lowest vector allocation count */
135 static unsigned int matrix_find_best_cpu(struct irq_matrix
*m
,
136 const struct cpumask
*msk
)
138 unsigned int cpu
, best_cpu
, maxavl
= 0;
143 for_each_cpu(cpu
, msk
) {
144 cm
= per_cpu_ptr(m
->maps
, cpu
);
146 if (!cm
->online
|| cm
->available
<= maxavl
)
150 maxavl
= cm
->available
;
155 /* Find the best CPU which has the lowest number of managed IRQs allocated */
156 static unsigned int matrix_find_best_cpu_managed(struct irq_matrix
*m
,
157 const struct cpumask
*msk
)
159 unsigned int cpu
, best_cpu
, allocated
= UINT_MAX
;
164 for_each_cpu(cpu
, msk
) {
165 cm
= per_cpu_ptr(m
->maps
, cpu
);
167 if (!cm
->online
|| cm
->managed_allocated
> allocated
)
171 allocated
= cm
->managed_allocated
;
177 * irq_matrix_assign_system - Assign system wide entry in the matrix
179 * @bit: Which bit to reserve
180 * @replace: Replace an already allocated vector with a system
181 * vector at the same bit position.
183 * The BUG_ON()s below are on purpose. If this goes wrong in the
184 * early boot process, then the chance to survive is about zero.
185 * If this happens when the system is life, it's not much better.
187 void irq_matrix_assign_system(struct irq_matrix
*m
, unsigned int bit
,
190 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
192 BUG_ON(bit
> m
->matrix_bits
);
193 BUG_ON(m
->online_maps
> 1 || (m
->online_maps
&& !replace
));
195 set_bit(bit
, m
->system_map
);
197 BUG_ON(!test_and_clear_bit(bit
, cm
->alloc_map
));
199 m
->total_allocated
--;
201 if (bit
>= m
->alloc_start
&& bit
< m
->alloc_end
)
202 m
->systembits_inalloc
++;
204 trace_irq_matrix_assign_system(bit
, m
);
208 * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
210 * @msk: On which CPUs the bits should be reserved.
212 * Can be called for offline CPUs. Note, this will only reserve one bit
213 * on all CPUs in @msk, but it's not guaranteed that the bits are at the
214 * same offset on all CPUs
216 int irq_matrix_reserve_managed(struct irq_matrix
*m
, const struct cpumask
*msk
)
218 unsigned int cpu
, failed_cpu
;
220 for_each_cpu(cpu
, msk
) {
221 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
224 bit
= matrix_alloc_area(m
, cm
, 1, true);
225 if (bit
>= m
->alloc_end
)
230 m
->global_available
--;
232 trace_irq_matrix_reserve_managed(bit
, cpu
, m
, cm
);
237 for_each_cpu(cpu
, msk
) {
238 if (cpu
== failed_cpu
)
240 irq_matrix_remove_managed(m
, cpumask_of(cpu
));
246 * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
248 * @msk: On which CPUs the bits should be removed
250 * Can be called for offline CPUs
252 * This removes not allocated managed interrupts from the map. It does
253 * not matter which one because the managed interrupts free their
254 * allocation when they shut down. If not, the accounting is screwed,
255 * but all what can be done at this point is warn about it.
257 void irq_matrix_remove_managed(struct irq_matrix
*m
, const struct cpumask
*msk
)
261 for_each_cpu(cpu
, msk
) {
262 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
263 unsigned int bit
, end
= m
->alloc_end
;
265 if (WARN_ON_ONCE(!cm
->managed
))
268 /* Get managed bit which are not allocated */
269 bitmap_andnot(m
->scratch_map
, cm
->managed_map
, cm
->alloc_map
, end
);
271 bit
= find_first_bit(m
->scratch_map
, end
);
272 if (WARN_ON_ONCE(bit
>= end
))
275 clear_bit(bit
, cm
->managed_map
);
280 m
->global_available
++;
282 trace_irq_matrix_remove_managed(bit
, cpu
, m
, cm
);
287 * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
289 * @msk: Which CPUs to search in
290 * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
292 int irq_matrix_alloc_managed(struct irq_matrix
*m
, const struct cpumask
*msk
,
293 unsigned int *mapped_cpu
)
295 unsigned int bit
, cpu
, end
;
298 if (cpumask_empty(msk
))
301 cpu
= matrix_find_best_cpu_managed(m
, msk
);
305 cm
= per_cpu_ptr(m
->maps
, cpu
);
307 /* Get managed bit which are not allocated */
308 bitmap_andnot(m
->scratch_map
, cm
->managed_map
, cm
->alloc_map
, end
);
309 bit
= find_first_bit(m
->scratch_map
, end
);
312 set_bit(bit
, cm
->alloc_map
);
314 cm
->managed_allocated
++;
315 m
->total_allocated
++;
317 trace_irq_matrix_alloc_managed(bit
, cpu
, m
, cm
);
322 * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
324 * @bit: Which bit to mark
326 * This should only be used to mark preallocated vectors
328 void irq_matrix_assign(struct irq_matrix
*m
, unsigned int bit
)
330 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
332 if (WARN_ON_ONCE(bit
< m
->alloc_start
|| bit
>= m
->alloc_end
))
334 if (WARN_ON_ONCE(test_and_set_bit(bit
, cm
->alloc_map
)))
337 m
->total_allocated
++;
339 m
->global_available
--;
340 trace_irq_matrix_assign(bit
, smp_processor_id(), m
, cm
);
344 * irq_matrix_reserve - Reserve interrupts
347 * This is merely a book keeping call. It increments the number of globally
348 * reserved interrupt bits w/o actually allocating them. This allows to
349 * setup interrupt descriptors w/o assigning low level resources to it.
350 * The actual allocation happens when the interrupt gets activated.
352 void irq_matrix_reserve(struct irq_matrix
*m
)
354 if (m
->global_reserved
== m
->global_available
)
355 pr_warn("Interrupt reservation exceeds available resources\n");
357 m
->global_reserved
++;
358 trace_irq_matrix_reserve(m
);
362 * irq_matrix_remove_reserved - Remove interrupt reservation
365 * This is merely a book keeping call. It decrements the number of globally
366 * reserved interrupt bits. This is used to undo irq_matrix_reserve() when the
367 * interrupt was never in use and a real vector allocated, which undid the
370 void irq_matrix_remove_reserved(struct irq_matrix
*m
)
372 m
->global_reserved
--;
373 trace_irq_matrix_remove_reserved(m
);
377 * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
379 * @msk: Which CPUs to search in
380 * @reserved: Allocate previously reserved interrupts
381 * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
383 int irq_matrix_alloc(struct irq_matrix
*m
, const struct cpumask
*msk
,
384 bool reserved
, unsigned int *mapped_cpu
)
386 unsigned int cpu
, bit
;
390 * Not required in theory, but matrix_find_best_cpu() uses
391 * for_each_cpu() which ignores the cpumask on UP .
393 if (cpumask_empty(msk
))
396 cpu
= matrix_find_best_cpu(m
, msk
);
400 cm
= per_cpu_ptr(m
->maps
, cpu
);
401 bit
= matrix_alloc_area(m
, cm
, 1, false);
402 if (bit
>= m
->alloc_end
)
406 m
->total_allocated
++;
407 m
->global_available
--;
409 m
->global_reserved
--;
411 trace_irq_matrix_alloc(bit
, cpu
, m
, cm
);
417 * irq_matrix_free - Free allocated interrupt in the matrix
419 * @cpu: Which CPU map needs be updated
420 * @bit: The bit to remove
421 * @managed: If true, the interrupt is managed and not accounted
424 void irq_matrix_free(struct irq_matrix
*m
, unsigned int cpu
,
425 unsigned int bit
, bool managed
)
427 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
429 if (WARN_ON_ONCE(bit
< m
->alloc_start
|| bit
>= m
->alloc_end
))
432 if (WARN_ON_ONCE(!test_and_clear_bit(bit
, cm
->alloc_map
)))
437 cm
->managed_allocated
--;
440 m
->total_allocated
--;
445 m
->global_available
++;
447 trace_irq_matrix_free(bit
, cpu
, m
, cm
);
451 * irq_matrix_available - Get the number of globally available irqs
452 * @m: Pointer to the matrix to query
453 * @cpudown: If true, the local CPU is about to go down, adjust
454 * the number of available irqs accordingly
456 unsigned int irq_matrix_available(struct irq_matrix
*m
, bool cpudown
)
458 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
461 return m
->global_available
;
462 return m
->global_available
- cm
->available
;
466 * irq_matrix_reserved - Get the number of globally reserved irqs
467 * @m: Pointer to the matrix to query
469 unsigned int irq_matrix_reserved(struct irq_matrix
*m
)
471 return m
->global_reserved
;
475 * irq_matrix_allocated - Get the number of allocated non-managed irqs on the local CPU
476 * @m: Pointer to the matrix to search
478 * This returns number of allocated non-managed interrupts.
480 unsigned int irq_matrix_allocated(struct irq_matrix
*m
)
482 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
484 return cm
->allocated
- cm
->managed_allocated
;
487 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
489 * irq_matrix_debug_show - Show detailed allocation information
490 * @sf: Pointer to the seq_file to print to
491 * @m: Pointer to the matrix allocator
492 * @ind: Indentation for the print format
494 * Note, this is a lockless snapshot.
496 void irq_matrix_debug_show(struct seq_file
*sf
, struct irq_matrix
*m
, int ind
)
498 unsigned int nsys
= bitmap_weight(m
->system_map
, m
->matrix_bits
);
501 seq_printf(sf
, "Online bitmaps: %6u\n", m
->online_maps
);
502 seq_printf(sf
, "Global available: %6u\n", m
->global_available
);
503 seq_printf(sf
, "Global reserved: %6u\n", m
->global_reserved
);
504 seq_printf(sf
, "Total allocated: %6u\n", m
->total_allocated
);
505 seq_printf(sf
, "System: %u: %*pbl\n", nsys
, m
->matrix_bits
,
507 seq_printf(sf
, "%*s| CPU | avl | man | mac | act | vectors\n", ind
, " ");
509 for_each_online_cpu(cpu
) {
510 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
512 seq_printf(sf
, "%*s %4d %4u %4u %4u %4u %*pbl\n", ind
, " ",
513 cpu
, cm
->available
, cm
->managed
,
514 cm
->managed_allocated
, cm
->allocated
,
515 m
->matrix_bits
, cm
->alloc_map
);