]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/irq/matrix.c
Revert "uio: use request_threaded_irq instead"
[mirror_ubuntu-bionic-kernel.git] / kernel / irq / matrix.c
1 /*
2 * Copyright (C) 2017 Thomas Gleixner <tglx@linutronix.de>
3 *
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>
12
13 #define IRQ_MATRIX_SIZE (BITS_TO_LONGS(IRQ_MATRIX_BITS) * sizeof(unsigned long))
14
15 struct cpumap {
16 unsigned int available;
17 unsigned int allocated;
18 unsigned int managed;
19 unsigned int managed_allocated;
20 bool initialized;
21 bool online;
22 unsigned long alloc_map[IRQ_MATRIX_SIZE];
23 unsigned long managed_map[IRQ_MATRIX_SIZE];
24 };
25
26 struct irq_matrix {
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];
39 };
40
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/irq_matrix.h>
43
44 /**
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
49 * invalid bit
50 */
51 __init struct irq_matrix *irq_alloc_matrix(unsigned int matrix_bits,
52 unsigned int alloc_start,
53 unsigned int alloc_end)
54 {
55 struct irq_matrix *m;
56
57 if (matrix_bits > IRQ_MATRIX_BITS)
58 return NULL;
59
60 m = kzalloc(sizeof(*m), GFP_KERNEL);
61 if (!m)
62 return NULL;
63
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);
69 if (!m->maps) {
70 kfree(m);
71 return NULL;
72 }
73 return m;
74 }
75
76 /**
77 * irq_matrix_online - Bring the local CPU matrix online
78 * @m: Matrix pointer
79 */
80 void irq_matrix_online(struct irq_matrix *m)
81 {
82 struct cpumap *cm = this_cpu_ptr(m->maps);
83
84 BUG_ON(cm->online);
85
86 if (!cm->initialized) {
87 cm->available = m->alloc_size;
88 cm->available -= cm->managed + m->systembits_inalloc;
89 cm->initialized = true;
90 }
91 m->global_available += cm->available;
92 cm->online = true;
93 m->online_maps++;
94 trace_irq_matrix_online(m);
95 }
96
97 /**
98 * irq_matrix_offline - Bring the local CPU matrix offline
99 * @m: Matrix pointer
100 */
101 void irq_matrix_offline(struct irq_matrix *m)
102 {
103 struct cpumap *cm = this_cpu_ptr(m->maps);
104
105 /* Update the global available size */
106 m->global_available -= cm->available;
107 cm->online = false;
108 m->online_maps--;
109 trace_irq_matrix_offline(m);
110 }
111
112 static unsigned int matrix_alloc_area(struct irq_matrix *m, struct cpumap *cm,
113 unsigned int num, bool managed)
114 {
115 unsigned int area, start = m->alloc_start;
116 unsigned int end = m->alloc_end;
117
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);
121 if (area >= end)
122 return area;
123 if (managed)
124 bitmap_set(cm->managed_map, area, num);
125 else
126 bitmap_set(cm->alloc_map, area, num);
127 return area;
128 }
129
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)
133 {
134 unsigned int cpu, best_cpu, maxavl = 0;
135 struct cpumap *cm;
136
137 best_cpu = UINT_MAX;
138
139 for_each_cpu(cpu, msk) {
140 cm = per_cpu_ptr(m->maps, cpu);
141
142 if (!cm->online || cm->available <= maxavl)
143 continue;
144
145 best_cpu = cpu;
146 maxavl = cm->available;
147 }
148 return best_cpu;
149 }
150
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)
154 {
155 unsigned int cpu, best_cpu, allocated = UINT_MAX;
156 struct cpumap *cm;
157
158 best_cpu = UINT_MAX;
159
160 for_each_cpu(cpu, msk) {
161 cm = per_cpu_ptr(m->maps, cpu);
162
163 if (!cm->online || cm->managed_allocated > allocated)
164 continue;
165
166 best_cpu = cpu;
167 allocated = cm->managed_allocated;
168 }
169 return best_cpu;
170 }
171
172 /**
173 * irq_matrix_assign_system - Assign system wide entry in the matrix
174 * @m: Matrix pointer
175 * @bit: Which bit to reserve
176 * @replace: Replace an already allocated vector with a system
177 * vector at the same bit position.
178 *
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.
182 */
183 void irq_matrix_assign_system(struct irq_matrix *m, unsigned int bit,
184 bool replace)
185 {
186 struct cpumap *cm = this_cpu_ptr(m->maps);
187
188 BUG_ON(bit > m->matrix_bits);
189 BUG_ON(m->online_maps > 1 || (m->online_maps && !replace));
190
191 set_bit(bit, m->system_map);
192 if (replace) {
193 BUG_ON(!test_and_clear_bit(bit, cm->alloc_map));
194 cm->allocated--;
195 m->total_allocated--;
196 }
197 if (bit >= m->alloc_start && bit < m->alloc_end)
198 m->systembits_inalloc++;
199
200 trace_irq_matrix_assign_system(bit, m);
201 }
202
203 /**
204 * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
205 * @m: Matrix pointer
206 * @msk: On which CPUs the bits should be reserved.
207 *
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
211 */
212 int irq_matrix_reserve_managed(struct irq_matrix *m, const struct cpumask *msk)
213 {
214 unsigned int cpu, failed_cpu;
215
216 for_each_cpu(cpu, msk) {
217 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
218 unsigned int bit;
219
220 bit = matrix_alloc_area(m, cm, 1, true);
221 if (bit >= m->alloc_end)
222 goto cleanup;
223 cm->managed++;
224 if (cm->online) {
225 cm->available--;
226 m->global_available--;
227 }
228 trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
229 }
230 return 0;
231 cleanup:
232 failed_cpu = cpu;
233 for_each_cpu(cpu, msk) {
234 if (cpu == failed_cpu)
235 break;
236 irq_matrix_remove_managed(m, cpumask_of(cpu));
237 }
238 return -ENOSPC;
239 }
240
241 /**
242 * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
243 * @m: Matrix pointer
244 * @msk: On which CPUs the bits should be removed
245 *
246 * Can be called for offline CPUs
247 *
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.
252 */
253 void irq_matrix_remove_managed(struct irq_matrix *m, const struct cpumask *msk)
254 {
255 unsigned int cpu;
256
257 for_each_cpu(cpu, msk) {
258 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
259 unsigned int bit, end = m->alloc_end;
260
261 if (WARN_ON_ONCE(!cm->managed))
262 continue;
263
264 /* Get managed bit which are not allocated */
265 bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
266
267 bit = find_first_bit(m->scratch_map, end);
268 if (WARN_ON_ONCE(bit >= end))
269 continue;
270
271 clear_bit(bit, cm->managed_map);
272
273 cm->managed--;
274 if (cm->online) {
275 cm->available++;
276 m->global_available++;
277 }
278 trace_irq_matrix_remove_managed(bit, cpu, m, cm);
279 }
280 }
281
282 /**
283 * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
284 * @m: Matrix pointer
285 * @cpu: On which CPU the interrupt should be allocated
286 */
287 int irq_matrix_alloc_managed(struct irq_matrix *m, const struct cpumask *msk,
288 unsigned int *mapped_cpu)
289 {
290 unsigned int bit, cpu, end = m->alloc_end;
291 struct cpumap *cm;
292
293 if (cpumask_empty(msk))
294 return -EINVAL;
295
296 cpu = matrix_find_best_cpu_managed(m, msk);
297 if (cpu == UINT_MAX)
298 return -ENOSPC;
299
300 cm = per_cpu_ptr(m->maps, cpu);
301 end = m->alloc_end;
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);
305 if (bit >= end)
306 return -ENOSPC;
307 set_bit(bit, cm->alloc_map);
308 cm->allocated++;
309 cm->managed_allocated++;
310 m->total_allocated++;
311 *mapped_cpu = cpu;
312 trace_irq_matrix_alloc_managed(bit, cpu, m, cm);
313 return bit;
314 }
315
316 /**
317 * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
318 * @m: Matrix pointer
319 * @bit: Which bit to mark
320 *
321 * This should only be used to mark preallocated vectors
322 */
323 void irq_matrix_assign(struct irq_matrix *m, unsigned int bit)
324 {
325 struct cpumap *cm = this_cpu_ptr(m->maps);
326
327 if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
328 return;
329 if (WARN_ON_ONCE(test_and_set_bit(bit, cm->alloc_map)))
330 return;
331 cm->allocated++;
332 m->total_allocated++;
333 cm->available--;
334 m->global_available--;
335 trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
336 }
337
338 /**
339 * irq_matrix_reserve - Reserve interrupts
340 * @m: Matrix pointer
341 *
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.
346 */
347 void irq_matrix_reserve(struct irq_matrix *m)
348 {
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");
352
353 m->global_reserved++;
354 trace_irq_matrix_reserve(m);
355 }
356
357 /**
358 * irq_matrix_remove_reserved - Remove interrupt reservation
359 * @m: Matrix pointer
360 *
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
364 * reservation.
365 */
366 void irq_matrix_remove_reserved(struct irq_matrix *m)
367 {
368 m->global_reserved--;
369 trace_irq_matrix_remove_reserved(m);
370 }
371
372 /**
373 * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
374 * @m: Matrix pointer
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
378 */
379 int irq_matrix_alloc(struct irq_matrix *m, const struct cpumask *msk,
380 bool reserved, unsigned int *mapped_cpu)
381 {
382 unsigned int cpu, bit;
383 struct cpumap *cm;
384
385 cpu = matrix_find_best_cpu(m, msk);
386 if (cpu == UINT_MAX)
387 return -ENOSPC;
388
389 cm = per_cpu_ptr(m->maps, cpu);
390 bit = matrix_alloc_area(m, cm, 1, false);
391 if (bit >= m->alloc_end)
392 return -ENOSPC;
393 cm->allocated++;
394 cm->available--;
395 m->total_allocated++;
396 m->global_available--;
397 if (reserved)
398 m->global_reserved--;
399 *mapped_cpu = cpu;
400 trace_irq_matrix_alloc(bit, cpu, m, cm);
401 return bit;
402
403 }
404
405 /**
406 * irq_matrix_free - Free allocated interrupt in the matrix
407 * @m: Matrix pointer
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
411 * as available.
412 */
413 void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
414 unsigned int bit, bool managed)
415 {
416 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
417
418 if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
419 return;
420
421 clear_bit(bit, cm->alloc_map);
422 cm->allocated--;
423 if(managed)
424 cm->managed_allocated--;
425
426 if (cm->online)
427 m->total_allocated--;
428
429 if (!managed) {
430 cm->available++;
431 if (cm->online)
432 m->global_available++;
433 }
434 trace_irq_matrix_free(bit, cpu, m, cm);
435 }
436
437 /**
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
442 */
443 unsigned int irq_matrix_available(struct irq_matrix *m, bool cpudown)
444 {
445 struct cpumap *cm = this_cpu_ptr(m->maps);
446
447 if (!cpudown)
448 return m->global_available;
449 return m->global_available - cm->available;
450 }
451
452 /**
453 * irq_matrix_reserved - Get the number of globally reserved irqs
454 * @m: Pointer to the matrix to query
455 */
456 unsigned int irq_matrix_reserved(struct irq_matrix *m)
457 {
458 return m->global_reserved;
459 }
460
461 /**
462 * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
463 * @m: Pointer to the matrix to search
464 *
465 * This returns number of allocated irqs
466 */
467 unsigned int irq_matrix_allocated(struct irq_matrix *m)
468 {
469 struct cpumap *cm = this_cpu_ptr(m->maps);
470
471 return cm->allocated;
472 }
473
474 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
475 /**
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
480 *
481 * Note, this is a lockless snapshot.
482 */
483 void irq_matrix_debug_show(struct seq_file *sf, struct irq_matrix *m, int ind)
484 {
485 unsigned int nsys = bitmap_weight(m->system_map, m->matrix_bits);
486 int cpu;
487
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,
493 m->system_map);
494 seq_printf(sf, "%*s| CPU | avl | man | mac | act | vectors\n", ind, " ");
495 cpus_read_lock();
496 for_each_online_cpu(cpu) {
497 struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
498
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);
503 }
504 cpus_read_unlock();
505 }
506 #endif