perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / kernel / irq / matrix.c
blob6e6d467f3dec57717ffb7cfae9098dedafb02b22
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>
8 #include <linux/cpu.h>
9 #include <linux/irq.h>
11 #define IRQ_MATRIX_SIZE (BITS_TO_LONGS(IRQ_MATRIX_BITS) * sizeof(unsigned long))
13 struct cpumap {
14 unsigned int available;
15 unsigned int allocated;
16 unsigned int managed;
17 bool initialized;
18 bool online;
19 unsigned long alloc_map[IRQ_MATRIX_SIZE];
20 unsigned long managed_map[IRQ_MATRIX_SIZE];
23 struct irq_matrix {
24 unsigned int matrix_bits;
25 unsigned int alloc_start;
26 unsigned int alloc_end;
27 unsigned int alloc_size;
28 unsigned int global_available;
29 unsigned int global_reserved;
30 unsigned int systembits_inalloc;
31 unsigned int total_allocated;
32 unsigned int online_maps;
33 struct cpumap __percpu *maps;
34 unsigned long scratch_map[IRQ_MATRIX_SIZE];
35 unsigned long system_map[IRQ_MATRIX_SIZE];
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/irq_matrix.h>
41 /**
42 * irq_alloc_matrix - Allocate a irq_matrix structure and initialize it
43 * @matrix_bits: Number of matrix bits must be <= IRQ_MATRIX_BITS
44 * @alloc_start: From which bit the allocation search starts
45 * @alloc_end: At which bit the allocation search ends, i.e first
46 * invalid bit
48 __init struct irq_matrix *irq_alloc_matrix(unsigned int matrix_bits,
49 unsigned int alloc_start,
50 unsigned int alloc_end)
52 struct irq_matrix *m;
54 if (matrix_bits > IRQ_MATRIX_BITS)
55 return NULL;
57 m = kzalloc(sizeof(*m), GFP_KERNEL);
58 if (!m)
59 return NULL;
61 m->matrix_bits = matrix_bits;
62 m->alloc_start = alloc_start;
63 m->alloc_end = alloc_end;
64 m->alloc_size = alloc_end - alloc_start;
65 m->maps = alloc_percpu(*m->maps);
66 if (!m->maps) {
67 kfree(m);
68 return NULL;
70 return m;
73 /**
74 * irq_matrix_online - Bring the local CPU matrix online
75 * @m: Matrix pointer
77 void irq_matrix_online(struct irq_matrix *m)
79 struct cpumap *cm = this_cpu_ptr(m->maps);
81 BUG_ON(cm->online);
83 if (!cm->initialized) {
84 cm->available = m->alloc_size;
85 cm->available -= cm->managed + m->systembits_inalloc;
86 cm->initialized = true;
88 m->global_available += cm->available;
89 cm->online = true;
90 m->online_maps++;
91 trace_irq_matrix_online(m);
94 /**
95 * irq_matrix_offline - Bring the local CPU matrix offline
96 * @m: Matrix pointer
98 void irq_matrix_offline(struct irq_matrix *m)
100 struct cpumap *cm = this_cpu_ptr(m->maps);
102 /* Update the global available size */
103 m->global_available -= cm->available;
104 cm->online = false;
105 m->online_maps--;
106 trace_irq_matrix_offline(m);
109 static unsigned int matrix_alloc_area(struct irq_matrix *m, struct cpumap *cm,
110 unsigned int num, bool managed)
112 unsigned int area, start = m->alloc_start;
113 unsigned int end = m->alloc_end;
115 bitmap_or(m->scratch_map, cm->managed_map, m->system_map, end);
116 bitmap_or(m->scratch_map, m->scratch_map, cm->alloc_map, end);
117 area = bitmap_find_next_zero_area(m->scratch_map, end, start, num, 0);
118 if (area >= end)
119 return area;
120 if (managed)
121 bitmap_set(cm->managed_map, area, num);
122 else
123 bitmap_set(cm->alloc_map, area, num);
124 return area;
127 /* Find the best CPU which has the lowest vector allocation count */
128 static unsigned int matrix_find_best_cpu(struct irq_matrix *m,
129 const struct cpumask *msk)
131 unsigned int cpu, best_cpu, maxavl = 0;
132 struct cpumap *cm;
134 best_cpu = UINT_MAX;
136 for_each_cpu(cpu, msk) {
137 cm = per_cpu_ptr(m->maps, cpu);
139 if (!cm->online || cm->available <= maxavl)
140 continue;
142 best_cpu = cpu;
143 maxavl = cm->available;
145 return best_cpu;
149 * irq_matrix_assign_system - Assign system wide entry in the matrix
150 * @m: Matrix pointer
151 * @bit: Which bit to reserve
152 * @replace: Replace an already allocated vector with a system
153 * vector at the same bit position.
155 * The BUG_ON()s below are on purpose. If this goes wrong in the
156 * early boot process, then the chance to survive is about zero.
157 * If this happens when the system is life, it's not much better.
159 void irq_matrix_assign_system(struct irq_matrix *m, unsigned int bit,
160 bool replace)
162 struct cpumap *cm = this_cpu_ptr(m->maps);
164 BUG_ON(bit > m->matrix_bits);
165 BUG_ON(m->online_maps > 1 || (m->online_maps && !replace));
167 set_bit(bit, m->system_map);
168 if (replace) {
169 BUG_ON(!test_and_clear_bit(bit, cm->alloc_map));
170 cm->allocated--;
171 m->total_allocated--;
173 if (bit >= m->alloc_start && bit < m->alloc_end)
174 m->systembits_inalloc++;
176 trace_irq_matrix_assign_system(bit, m);
180 * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
181 * @m: Matrix pointer
182 * @msk: On which CPUs the bits should be reserved.
184 * Can be called for offline CPUs. Note, this will only reserve one bit
185 * on all CPUs in @msk, but it's not guaranteed that the bits are at the
186 * same offset on all CPUs
188 int irq_matrix_reserve_managed(struct irq_matrix *m, const struct cpumask *msk)
190 unsigned int cpu, failed_cpu;
192 for_each_cpu(cpu, msk) {
193 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
194 unsigned int bit;
196 bit = matrix_alloc_area(m, cm, 1, true);
197 if (bit >= m->alloc_end)
198 goto cleanup;
199 cm->managed++;
200 if (cm->online) {
201 cm->available--;
202 m->global_available--;
204 trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
206 return 0;
207 cleanup:
208 failed_cpu = cpu;
209 for_each_cpu(cpu, msk) {
210 if (cpu == failed_cpu)
211 break;
212 irq_matrix_remove_managed(m, cpumask_of(cpu));
214 return -ENOSPC;
218 * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
219 * @m: Matrix pointer
220 * @msk: On which CPUs the bits should be removed
222 * Can be called for offline CPUs
224 * This removes not allocated managed interrupts from the map. It does
225 * not matter which one because the managed interrupts free their
226 * allocation when they shut down. If not, the accounting is screwed,
227 * but all what can be done at this point is warn about it.
229 void irq_matrix_remove_managed(struct irq_matrix *m, const struct cpumask *msk)
231 unsigned int cpu;
233 for_each_cpu(cpu, msk) {
234 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
235 unsigned int bit, end = m->alloc_end;
237 if (WARN_ON_ONCE(!cm->managed))
238 continue;
240 /* Get managed bit which are not allocated */
241 bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
243 bit = find_first_bit(m->scratch_map, end);
244 if (WARN_ON_ONCE(bit >= end))
245 continue;
247 clear_bit(bit, cm->managed_map);
249 cm->managed--;
250 if (cm->online) {
251 cm->available++;
252 m->global_available++;
254 trace_irq_matrix_remove_managed(bit, cpu, m, cm);
259 * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
260 * @m: Matrix pointer
261 * @cpu: On which CPU the interrupt should be allocated
263 int irq_matrix_alloc_managed(struct irq_matrix *m, const struct cpumask *msk,
264 unsigned int *mapped_cpu)
266 unsigned int bit, cpu, end = m->alloc_end;
267 struct cpumap *cm;
269 if (cpumask_empty(msk))
270 return -EINVAL;
272 cpu = matrix_find_best_cpu(m, msk);
273 if (cpu == UINT_MAX)
274 return -ENOSPC;
276 cm = per_cpu_ptr(m->maps, cpu);
277 end = m->alloc_end;
278 /* Get managed bit which are not allocated */
279 bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
280 bit = find_first_bit(m->scratch_map, end);
281 if (bit >= end)
282 return -ENOSPC;
283 set_bit(bit, cm->alloc_map);
284 cm->allocated++;
285 m->total_allocated++;
286 *mapped_cpu = cpu;
287 trace_irq_matrix_alloc_managed(bit, cpu, m, cm);
288 return bit;
292 * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
293 * @m: Matrix pointer
294 * @bit: Which bit to mark
296 * This should only be used to mark preallocated vectors
298 void irq_matrix_assign(struct irq_matrix *m, unsigned int bit)
300 struct cpumap *cm = this_cpu_ptr(m->maps);
302 if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
303 return;
304 if (WARN_ON_ONCE(test_and_set_bit(bit, cm->alloc_map)))
305 return;
306 cm->allocated++;
307 m->total_allocated++;
308 cm->available--;
309 m->global_available--;
310 trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
314 * irq_matrix_reserve - Reserve interrupts
315 * @m: Matrix pointer
317 * This is merily a book keeping call. It increments the number of globally
318 * reserved interrupt bits w/o actually allocating them. This allows to
319 * setup interrupt descriptors w/o assigning low level resources to it.
320 * The actual allocation happens when the interrupt gets activated.
322 void irq_matrix_reserve(struct irq_matrix *m)
324 if (m->global_reserved <= m->global_available &&
325 m->global_reserved + 1 > m->global_available)
326 pr_warn("Interrupt reservation exceeds available resources\n");
328 m->global_reserved++;
329 trace_irq_matrix_reserve(m);
333 * irq_matrix_remove_reserved - Remove interrupt reservation
334 * @m: Matrix pointer
336 * This is merily a book keeping call. It decrements the number of globally
337 * reserved interrupt bits. This is used to undo irq_matrix_reserve() when the
338 * interrupt was never in use and a real vector allocated, which undid the
339 * reservation.
341 void irq_matrix_remove_reserved(struct irq_matrix *m)
343 m->global_reserved--;
344 trace_irq_matrix_remove_reserved(m);
348 * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
349 * @m: Matrix pointer
350 * @msk: Which CPUs to search in
351 * @reserved: Allocate previously reserved interrupts
352 * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
354 int irq_matrix_alloc(struct irq_matrix *m, const struct cpumask *msk,
355 bool reserved, unsigned int *mapped_cpu)
357 unsigned int cpu, bit;
358 struct cpumap *cm;
360 cpu = matrix_find_best_cpu(m, msk);
361 if (cpu == UINT_MAX)
362 return -ENOSPC;
364 cm = per_cpu_ptr(m->maps, cpu);
365 bit = matrix_alloc_area(m, cm, 1, false);
366 if (bit >= m->alloc_end)
367 return -ENOSPC;
368 cm->allocated++;
369 cm->available--;
370 m->total_allocated++;
371 m->global_available--;
372 if (reserved)
373 m->global_reserved--;
374 *mapped_cpu = cpu;
375 trace_irq_matrix_alloc(bit, cpu, m, cm);
376 return bit;
381 * irq_matrix_free - Free allocated interrupt in the matrix
382 * @m: Matrix pointer
383 * @cpu: Which CPU map needs be updated
384 * @bit: The bit to remove
385 * @managed: If true, the interrupt is managed and not accounted
386 * as available.
388 void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
389 unsigned int bit, bool managed)
391 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
393 if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
394 return;
396 clear_bit(bit, cm->alloc_map);
397 cm->allocated--;
399 if (cm->online)
400 m->total_allocated--;
402 if (!managed) {
403 cm->available++;
404 if (cm->online)
405 m->global_available++;
407 trace_irq_matrix_free(bit, cpu, m, cm);
411 * irq_matrix_available - Get the number of globally available irqs
412 * @m: Pointer to the matrix to query
413 * @cpudown: If true, the local CPU is about to go down, adjust
414 * the number of available irqs accordingly
416 unsigned int irq_matrix_available(struct irq_matrix *m, bool cpudown)
418 struct cpumap *cm = this_cpu_ptr(m->maps);
420 if (!cpudown)
421 return m->global_available;
422 return m->global_available - cm->available;
426 * irq_matrix_reserved - Get the number of globally reserved irqs
427 * @m: Pointer to the matrix to query
429 unsigned int irq_matrix_reserved(struct irq_matrix *m)
431 return m->global_reserved;
435 * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
436 * @m: Pointer to the matrix to search
438 * This returns number of allocated irqs
440 unsigned int irq_matrix_allocated(struct irq_matrix *m)
442 struct cpumap *cm = this_cpu_ptr(m->maps);
444 return cm->allocated;
447 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
449 * irq_matrix_debug_show - Show detailed allocation information
450 * @sf: Pointer to the seq_file to print to
451 * @m: Pointer to the matrix allocator
452 * @ind: Indentation for the print format
454 * Note, this is a lockless snapshot.
456 void irq_matrix_debug_show(struct seq_file *sf, struct irq_matrix *m, int ind)
458 unsigned int nsys = bitmap_weight(m->system_map, m->matrix_bits);
459 int cpu;
461 seq_printf(sf, "Online bitmaps: %6u\n", m->online_maps);
462 seq_printf(sf, "Global available: %6u\n", m->global_available);
463 seq_printf(sf, "Global reserved: %6u\n", m->global_reserved);
464 seq_printf(sf, "Total allocated: %6u\n", m->total_allocated);
465 seq_printf(sf, "System: %u: %*pbl\n", nsys, m->matrix_bits,
466 m->system_map);
467 seq_printf(sf, "%*s| CPU | avl | man | act | vectors\n", ind, " ");
468 cpus_read_lock();
469 for_each_online_cpu(cpu) {
470 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
472 seq_printf(sf, "%*s %4d %4u %4u %4u %*pbl\n", ind, " ",
473 cpu, cm->available, cm->managed, cm->allocated,
474 m->matrix_bits, cm->alloc_map);
476 cpus_read_unlock();
478 #endif