]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/irq/matrix.c
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
;
19 unsigned int managed_allocated
;
22 unsigned long alloc_map
[IRQ_MATRIX_SIZE
];
23 unsigned long managed_map
[IRQ_MATRIX_SIZE
];
27 unsigned int matrix_bits
;
28 unsigned int alloc_start
;
29 unsigned int alloc_end
;
30 unsigned int alloc_size
;
31 unsigned int global_available
;
32 unsigned int global_reserved
;
33 unsigned int systembits_inalloc
;
34 unsigned int total_allocated
;
35 unsigned int online_maps
;
36 struct cpumap __percpu
*maps
;
37 unsigned long scratch_map
[IRQ_MATRIX_SIZE
];
38 unsigned long system_map
[IRQ_MATRIX_SIZE
];
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/irq_matrix.h>
45 * irq_alloc_matrix - Allocate a irq_matrix structure and initialize it
46 * @matrix_bits: Number of matrix bits must be <= IRQ_MATRIX_BITS
47 * @alloc_start: From which bit the allocation search starts
48 * @alloc_end: At which bit the allocation search ends, i.e first
51 __init
struct irq_matrix
*irq_alloc_matrix(unsigned int matrix_bits
,
52 unsigned int alloc_start
,
53 unsigned int alloc_end
)
57 if (matrix_bits
> IRQ_MATRIX_BITS
)
60 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
64 m
->matrix_bits
= matrix_bits
;
65 m
->alloc_start
= alloc_start
;
66 m
->alloc_end
= alloc_end
;
67 m
->alloc_size
= alloc_end
- alloc_start
;
68 m
->maps
= alloc_percpu(*m
->maps
);
77 * irq_matrix_online - Bring the local CPU matrix online
80 void irq_matrix_online(struct irq_matrix
*m
)
82 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
86 if (!cm
->initialized
) {
87 cm
->available
= m
->alloc_size
;
88 cm
->available
-= cm
->managed
+ m
->systembits_inalloc
;
89 cm
->initialized
= true;
91 m
->global_available
+= cm
->available
;
94 trace_irq_matrix_online(m
);
98 * irq_matrix_offline - Bring the local CPU matrix offline
101 void irq_matrix_offline(struct irq_matrix
*m
)
103 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
105 /* Update the global available size */
106 m
->global_available
-= cm
->available
;
109 trace_irq_matrix_offline(m
);
112 static unsigned int matrix_alloc_area(struct irq_matrix
*m
, struct cpumap
*cm
,
113 unsigned int num
, bool managed
)
115 unsigned int area
, start
= m
->alloc_start
;
116 unsigned int end
= m
->alloc_end
;
118 bitmap_or(m
->scratch_map
, cm
->managed_map
, m
->system_map
, end
);
119 bitmap_or(m
->scratch_map
, m
->scratch_map
, cm
->alloc_map
, end
);
120 area
= bitmap_find_next_zero_area(m
->scratch_map
, end
, start
, num
, 0);
124 bitmap_set(cm
->managed_map
, area
, num
);
126 bitmap_set(cm
->alloc_map
, area
, num
);
130 /* Find the best CPU which has the lowest vector allocation count */
131 static unsigned int matrix_find_best_cpu(struct irq_matrix
*m
,
132 const struct cpumask
*msk
)
134 unsigned int cpu
, best_cpu
, maxavl
= 0;
139 for_each_cpu(cpu
, msk
) {
140 cm
= per_cpu_ptr(m
->maps
, cpu
);
142 if (!cm
->online
|| cm
->available
<= maxavl
)
146 maxavl
= cm
->available
;
151 /* Find the best CPU which has the lowest number of managed IRQs allocated */
152 static unsigned int matrix_find_best_cpu_managed(struct irq_matrix
*m
,
153 const struct cpumask
*msk
)
155 unsigned int cpu
, best_cpu
, allocated
= UINT_MAX
;
160 for_each_cpu(cpu
, msk
) {
161 cm
= per_cpu_ptr(m
->maps
, cpu
);
163 if (!cm
->online
|| cm
->managed_allocated
> allocated
)
167 allocated
= cm
->managed_allocated
;
173 * irq_matrix_assign_system - Assign system wide entry in the matrix
175 * @bit: Which bit to reserve
176 * @replace: Replace an already allocated vector with a system
177 * vector at the same bit position.
179 * The BUG_ON()s below are on purpose. If this goes wrong in the
180 * early boot process, then the chance to survive is about zero.
181 * If this happens when the system is life, it's not much better.
183 void irq_matrix_assign_system(struct irq_matrix
*m
, unsigned int bit
,
186 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
188 BUG_ON(bit
> m
->matrix_bits
);
189 BUG_ON(m
->online_maps
> 1 || (m
->online_maps
&& !replace
));
191 set_bit(bit
, m
->system_map
);
193 BUG_ON(!test_and_clear_bit(bit
, cm
->alloc_map
));
195 m
->total_allocated
--;
197 if (bit
>= m
->alloc_start
&& bit
< m
->alloc_end
)
198 m
->systembits_inalloc
++;
200 trace_irq_matrix_assign_system(bit
, m
);
204 * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
206 * @msk: On which CPUs the bits should be reserved.
208 * Can be called for offline CPUs. Note, this will only reserve one bit
209 * on all CPUs in @msk, but it's not guaranteed that the bits are at the
210 * same offset on all CPUs
212 int irq_matrix_reserve_managed(struct irq_matrix
*m
, const struct cpumask
*msk
)
214 unsigned int cpu
, failed_cpu
;
216 for_each_cpu(cpu
, msk
) {
217 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
220 bit
= matrix_alloc_area(m
, cm
, 1, true);
221 if (bit
>= m
->alloc_end
)
226 m
->global_available
--;
228 trace_irq_matrix_reserve_managed(bit
, cpu
, m
, cm
);
233 for_each_cpu(cpu
, msk
) {
234 if (cpu
== failed_cpu
)
236 irq_matrix_remove_managed(m
, cpumask_of(cpu
));
242 * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
244 * @msk: On which CPUs the bits should be removed
246 * Can be called for offline CPUs
248 * This removes not allocated managed interrupts from the map. It does
249 * not matter which one because the managed interrupts free their
250 * allocation when they shut down. If not, the accounting is screwed,
251 * but all what can be done at this point is warn about it.
253 void irq_matrix_remove_managed(struct irq_matrix
*m
, const struct cpumask
*msk
)
257 for_each_cpu(cpu
, msk
) {
258 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
259 unsigned int bit
, end
= m
->alloc_end
;
261 if (WARN_ON_ONCE(!cm
->managed
))
264 /* Get managed bit which are not allocated */
265 bitmap_andnot(m
->scratch_map
, cm
->managed_map
, cm
->alloc_map
, end
);
267 bit
= find_first_bit(m
->scratch_map
, end
);
268 if (WARN_ON_ONCE(bit
>= end
))
271 clear_bit(bit
, cm
->managed_map
);
276 m
->global_available
++;
278 trace_irq_matrix_remove_managed(bit
, cpu
, m
, cm
);
283 * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
285 * @cpu: On which CPU the interrupt should be allocated
287 int irq_matrix_alloc_managed(struct irq_matrix
*m
, const struct cpumask
*msk
,
288 unsigned int *mapped_cpu
)
290 unsigned int bit
, cpu
, end
= m
->alloc_end
;
293 if (cpumask_empty(msk
))
296 cpu
= matrix_find_best_cpu_managed(m
, msk
);
300 cm
= per_cpu_ptr(m
->maps
, cpu
);
302 /* Get managed bit which are not allocated */
303 bitmap_andnot(m
->scratch_map
, cm
->managed_map
, cm
->alloc_map
, end
);
304 bit
= find_first_bit(m
->scratch_map
, end
);
307 set_bit(bit
, cm
->alloc_map
);
309 cm
->managed_allocated
++;
310 m
->total_allocated
++;
312 trace_irq_matrix_alloc_managed(bit
, cpu
, m
, cm
);
317 * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
319 * @bit: Which bit to mark
321 * This should only be used to mark preallocated vectors
323 void irq_matrix_assign(struct irq_matrix
*m
, unsigned int bit
)
325 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
327 if (WARN_ON_ONCE(bit
< m
->alloc_start
|| bit
>= m
->alloc_end
))
329 if (WARN_ON_ONCE(test_and_set_bit(bit
, cm
->alloc_map
)))
332 m
->total_allocated
++;
334 m
->global_available
--;
335 trace_irq_matrix_assign(bit
, smp_processor_id(), m
, cm
);
339 * irq_matrix_reserve - Reserve interrupts
342 * This is merily a book keeping call. It increments the number of globally
343 * reserved interrupt bits w/o actually allocating them. This allows to
344 * setup interrupt descriptors w/o assigning low level resources to it.
345 * The actual allocation happens when the interrupt gets activated.
347 void irq_matrix_reserve(struct irq_matrix
*m
)
349 if (m
->global_reserved
<= m
->global_available
&&
350 m
->global_reserved
+ 1 > m
->global_available
)
351 pr_warn("Interrupt reservation exceeds available resources\n");
353 m
->global_reserved
++;
354 trace_irq_matrix_reserve(m
);
358 * irq_matrix_remove_reserved - Remove interrupt reservation
361 * This is merily a book keeping call. It decrements the number of globally
362 * reserved interrupt bits. This is used to undo irq_matrix_reserve() when the
363 * interrupt was never in use and a real vector allocated, which undid the
366 void irq_matrix_remove_reserved(struct irq_matrix
*m
)
368 m
->global_reserved
--;
369 trace_irq_matrix_remove_reserved(m
);
373 * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
375 * @msk: Which CPUs to search in
376 * @reserved: Allocate previously reserved interrupts
377 * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
379 int irq_matrix_alloc(struct irq_matrix
*m
, const struct cpumask
*msk
,
380 bool reserved
, unsigned int *mapped_cpu
)
382 unsigned int cpu
, bit
;
385 cpu
= matrix_find_best_cpu(m
, msk
);
389 cm
= per_cpu_ptr(m
->maps
, cpu
);
390 bit
= matrix_alloc_area(m
, cm
, 1, false);
391 if (bit
>= m
->alloc_end
)
395 m
->total_allocated
++;
396 m
->global_available
--;
398 m
->global_reserved
--;
400 trace_irq_matrix_alloc(bit
, cpu
, m
, cm
);
406 * irq_matrix_free - Free allocated interrupt in the matrix
408 * @cpu: Which CPU map needs be updated
409 * @bit: The bit to remove
410 * @managed: If true, the interrupt is managed and not accounted
413 void irq_matrix_free(struct irq_matrix
*m
, unsigned int cpu
,
414 unsigned int bit
, bool managed
)
416 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
418 if (WARN_ON_ONCE(bit
< m
->alloc_start
|| bit
>= m
->alloc_end
))
421 clear_bit(bit
, cm
->alloc_map
);
424 cm
->managed_allocated
--;
427 m
->total_allocated
--;
432 m
->global_available
++;
434 trace_irq_matrix_free(bit
, cpu
, m
, cm
);
438 * irq_matrix_available - Get the number of globally available irqs
439 * @m: Pointer to the matrix to query
440 * @cpudown: If true, the local CPU is about to go down, adjust
441 * the number of available irqs accordingly
443 unsigned int irq_matrix_available(struct irq_matrix
*m
, bool cpudown
)
445 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
448 return m
->global_available
;
449 return m
->global_available
- cm
->available
;
453 * irq_matrix_reserved - Get the number of globally reserved irqs
454 * @m: Pointer to the matrix to query
456 unsigned int irq_matrix_reserved(struct irq_matrix
*m
)
458 return m
->global_reserved
;
462 * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
463 * @m: Pointer to the matrix to search
465 * This returns number of allocated irqs
467 unsigned int irq_matrix_allocated(struct irq_matrix
*m
)
469 struct cpumap
*cm
= this_cpu_ptr(m
->maps
);
471 return cm
->allocated
;
474 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
476 * irq_matrix_debug_show - Show detailed allocation information
477 * @sf: Pointer to the seq_file to print to
478 * @m: Pointer to the matrix allocator
479 * @ind: Indentation for the print format
481 * Note, this is a lockless snapshot.
483 void irq_matrix_debug_show(struct seq_file
*sf
, struct irq_matrix
*m
, int ind
)
485 unsigned int nsys
= bitmap_weight(m
->system_map
, m
->matrix_bits
);
488 seq_printf(sf
, "Online bitmaps: %6u\n", m
->online_maps
);
489 seq_printf(sf
, "Global available: %6u\n", m
->global_available
);
490 seq_printf(sf
, "Global reserved: %6u\n", m
->global_reserved
);
491 seq_printf(sf
, "Total allocated: %6u\n", m
->total_allocated
);
492 seq_printf(sf
, "System: %u: %*pbl\n", nsys
, m
->matrix_bits
,
494 seq_printf(sf
, "%*s| CPU | avl | man | mac | act | vectors\n", ind
, " ");
496 for_each_online_cpu(cpu
) {
497 struct cpumap
*cm
= per_cpu_ptr(m
->maps
, cpu
);
499 seq_printf(sf
, "%*s %4d %4u %4u %4u %4u %*pbl\n", ind
, " ",
500 cpu
, cm
->available
, cm
->managed
,
501 cm
->managed_allocated
, cm
->allocated
,
502 m
->matrix_bits
, cm
->alloc_map
);