futex: futex_wake_op, fix sign_extend32 sign bits
[linux/fpc-iii.git] / kernel / irq / matrix.c
blob0ba0dd8863a779f5b318ef07f03c2915ea57cd57
1 /*
2 * Copyright (C) 2017 Thomas Gleixner <tglx@linutronix.de>
4 * SPDX-License-Identifier: GPL-2.0
5 */
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))
15 struct cpumap {
16 unsigned int available;
17 unsigned int allocated;
18 unsigned int managed;
19 bool online;
20 unsigned long alloc_map[IRQ_MATRIX_SIZE];
21 unsigned long managed_map[IRQ_MATRIX_SIZE];
24 struct irq_matrix {
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>
42 /**
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
47 * invalid bit
49 __init struct irq_matrix *irq_alloc_matrix(unsigned int matrix_bits,
50 unsigned int alloc_start,
51 unsigned int alloc_end)
53 struct irq_matrix *m;
55 if (matrix_bits > IRQ_MATRIX_BITS)
56 return NULL;
58 m = kzalloc(sizeof(*m), GFP_KERNEL);
59 if (!m)
60 return NULL;
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);
67 if (!m->maps) {
68 kfree(m);
69 return NULL;
71 return m;
74 /**
75 * irq_matrix_online - Bring the local CPU matrix online
76 * @m: Matrix pointer
78 void irq_matrix_online(struct irq_matrix *m)
80 struct cpumap *cm = this_cpu_ptr(m->maps);
82 BUG_ON(cm->online);
84 bitmap_zero(cm->alloc_map, m->matrix_bits);
85 cm->available = m->alloc_size - (cm->managed + m->systembits_inalloc);
86 cm->allocated = 0;
87 m->global_available += cm->available;
88 cm->online = true;
89 m->online_maps++;
90 trace_irq_matrix_online(m);
93 /**
94 * irq_matrix_offline - Bring the local CPU matrix offline
95 * @m: Matrix pointer
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;
103 cm->online = false;
104 m->online_maps--;
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);
117 if (area >= end)
118 return area;
119 if (managed)
120 bitmap_set(cm->managed_map, area, num);
121 else
122 bitmap_set(cm->alloc_map, area, num);
123 return area;
127 * irq_matrix_assign_system - Assign system wide entry in the matrix
128 * @m: Matrix pointer
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,
138 bool replace)
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);
146 if (replace) {
147 BUG_ON(!test_and_clear_bit(bit, cm->alloc_map));
148 cm->allocated--;
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
159 * @m: Matrix pointer
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);
172 unsigned int bit;
174 bit = matrix_alloc_area(m, cm, 1, true);
175 if (bit >= m->alloc_end)
176 goto cleanup;
177 cm->managed++;
178 if (cm->online) {
179 cm->available--;
180 m->global_available--;
182 trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
184 return 0;
185 cleanup:
186 failed_cpu = cpu;
187 for_each_cpu(cpu, msk) {
188 if (cpu == failed_cpu)
189 break;
190 irq_matrix_remove_managed(m, cpumask_of(cpu));
192 return -ENOSPC;
196 * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
197 * @m: Matrix pointer
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)
209 unsigned int cpu;
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))
216 continue;
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))
223 continue;
225 clear_bit(bit, cm->managed_map);
227 cm->managed--;
228 if (cm->online) {
229 cm->available++;
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
238 * @m: Matrix pointer
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);
249 if (bit >= end)
250 return -ENOSPC;
251 set_bit(bit, cm->alloc_map);
252 cm->allocated++;
253 m->total_allocated++;
254 trace_irq_matrix_alloc_managed(bit, cpu, m, cm);
255 return bit;
259 * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
260 * @m: Matrix pointer
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))
270 return;
271 if (WARN_ON_ONCE(test_and_set_bit(bit, cm->alloc_map)))
272 return;
273 cm->allocated++;
274 m->total_allocated++;
275 cm->available--;
276 m->global_available--;
277 trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
281 * irq_matrix_reserve - Reserve interrupts
282 * @m: Matrix pointer
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
301 * @m: Matrix pointer
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
306 * reservation.
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
316 * @m: Matrix pointer
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)
324 unsigned int cpu;
326 for_each_cpu(cpu, msk) {
327 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
328 unsigned int bit;
330 if (!cm->online)
331 continue;
333 bit = matrix_alloc_area(m, cm, 1, false);
334 if (bit < m->alloc_end) {
335 cm->allocated++;
336 cm->available--;
337 m->total_allocated++;
338 m->global_available--;
339 if (reserved)
340 m->global_reserved--;
341 *mapped_cpu = cpu;
342 trace_irq_matrix_alloc(bit, cpu, m, cm);
343 return bit;
346 return -ENOSPC;
350 * irq_matrix_free - Free allocated interrupt in the matrix
351 * @m: Matrix pointer
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
355 * as available.
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))
363 return;
365 if (cm->online) {
366 clear_bit(bit, cm->alloc_map);
367 cm->allocated--;
368 m->total_allocated--;
369 if (!managed) {
370 cm->available++;
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 if (!cpudown)
388 return m->global_available;
389 return m->global_available - cm->available;
393 * irq_matrix_reserved - Get the number of globally reserved irqs
394 * @m: Pointer to the matrix to query
396 unsigned int irq_matrix_reserved(struct irq_matrix *m)
398 return m->global_reserved;
402 * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
403 * @m: Pointer to the matrix to search
405 * This returns number of allocated irqs
407 unsigned int irq_matrix_allocated(struct irq_matrix *m)
409 struct cpumap *cm = this_cpu_ptr(m->maps);
411 return cm->allocated;
414 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
416 * irq_matrix_debug_show - Show detailed allocation information
417 * @sf: Pointer to the seq_file to print to
418 * @m: Pointer to the matrix allocator
419 * @ind: Indentation for the print format
421 * Note, this is a lockless snapshot.
423 void irq_matrix_debug_show(struct seq_file *sf, struct irq_matrix *m, int ind)
425 unsigned int nsys = bitmap_weight(m->system_map, m->matrix_bits);
426 int cpu;
428 seq_printf(sf, "Online bitmaps: %6u\n", m->online_maps);
429 seq_printf(sf, "Global available: %6u\n", m->global_available);
430 seq_printf(sf, "Global reserved: %6u\n", m->global_reserved);
431 seq_printf(sf, "Total allocated: %6u\n", m->total_allocated);
432 seq_printf(sf, "System: %u: %*pbl\n", nsys, m->matrix_bits,
433 m->system_map);
434 seq_printf(sf, "%*s| CPU | avl | man | act | vectors\n", ind, " ");
435 cpus_read_lock();
436 for_each_online_cpu(cpu) {
437 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
439 seq_printf(sf, "%*s %4d %4u %4u %4u %*pbl\n", ind, " ",
440 cpu, cm->available, cm->managed, cm->allocated,
441 m->matrix_bits, cm->alloc_map);
443 cpus_read_unlock();
445 #endif