Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : // Copyright (C) 2017 Thomas Gleixner <tglx@linutronix.de>
3 :
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>
10 :
11 : #define IRQ_MATRIX_SIZE (BITS_TO_LONGS(IRQ_MATRIX_BITS))
12 :
13 : struct cpumap {
14 : unsigned int available;
15 : unsigned int allocated;
16 : unsigned int managed;
17 : unsigned int managed_allocated;
18 : bool initialized;
19 : bool online;
20 : unsigned long alloc_map[IRQ_MATRIX_SIZE];
21 : unsigned long managed_map[IRQ_MATRIX_SIZE];
22 : };
23 :
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];
37 : };
38 :
39 : #define CREATE_TRACE_POINTS
40 : #include <trace/events/irq_matrix.h>
41 :
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
48 : */
49 1 : __init struct irq_matrix *irq_alloc_matrix(unsigned int matrix_bits,
50 : unsigned int alloc_start,
51 : unsigned int alloc_end)
52 : {
53 1 : struct irq_matrix *m;
54 :
55 1 : if (matrix_bits > IRQ_MATRIX_BITS)
56 : return NULL;
57 :
58 1 : m = kzalloc(sizeof(*m), GFP_KERNEL);
59 1 : if (!m)
60 : return NULL;
61 :
62 1 : m->matrix_bits = matrix_bits;
63 1 : m->alloc_start = alloc_start;
64 1 : m->alloc_end = alloc_end;
65 1 : m->alloc_size = alloc_end - alloc_start;
66 1 : m->maps = alloc_percpu(*m->maps);
67 1 : if (!m->maps) {
68 0 : kfree(m);
69 0 : return NULL;
70 : }
71 : return m;
72 : }
73 :
74 : /**
75 : * irq_matrix_online - Bring the local CPU matrix online
76 : * @m: Matrix pointer
77 : */
78 4 : void irq_matrix_online(struct irq_matrix *m)
79 : {
80 4 : struct cpumap *cm = this_cpu_ptr(m->maps);
81 :
82 4 : BUG_ON(cm->online);
83 :
84 4 : if (!cm->initialized) {
85 4 : cm->available = m->alloc_size;
86 4 : cm->available -= cm->managed + m->systembits_inalloc;
87 4 : cm->initialized = true;
88 : }
89 4 : m->global_available += cm->available;
90 4 : cm->online = true;
91 4 : m->online_maps++;
92 4 : trace_irq_matrix_online(m);
93 4 : }
94 :
95 : /**
96 : * irq_matrix_offline - Bring the local CPU matrix offline
97 : * @m: Matrix pointer
98 : */
99 0 : void irq_matrix_offline(struct irq_matrix *m)
100 : {
101 0 : struct cpumap *cm = this_cpu_ptr(m->maps);
102 :
103 : /* Update the global available size */
104 0 : m->global_available -= cm->available;
105 0 : cm->online = false;
106 0 : m->online_maps--;
107 0 : trace_irq_matrix_offline(m);
108 0 : }
109 :
110 3 : static unsigned int matrix_alloc_area(struct irq_matrix *m, struct cpumap *cm,
111 : unsigned int num, bool managed)
112 : {
113 3 : unsigned int area, start = m->alloc_start;
114 3 : unsigned int end = m->alloc_end;
115 :
116 3 : bitmap_or(m->scratch_map, cm->managed_map, m->system_map, end);
117 3 : bitmap_or(m->scratch_map, m->scratch_map, cm->alloc_map, end);
118 3 : area = bitmap_find_next_zero_area(m->scratch_map, end, start, num, 0);
119 3 : if (area >= end)
120 : return area;
121 3 : if (managed)
122 0 : bitmap_set(cm->managed_map, area, num);
123 : else
124 3 : bitmap_set(cm->alloc_map, area, num);
125 : return area;
126 : }
127 :
128 : /* Find the best CPU which has the lowest vector allocation count */
129 3 : static unsigned int matrix_find_best_cpu(struct irq_matrix *m,
130 : const struct cpumask *msk)
131 : {
132 3 : unsigned int cpu, best_cpu, maxavl = 0;
133 3 : struct cpumap *cm;
134 :
135 3 : best_cpu = UINT_MAX;
136 :
137 15 : for_each_cpu(cpu, msk) {
138 12 : cm = per_cpu_ptr(m->maps, cpu);
139 :
140 12 : if (!cm->online || cm->available <= maxavl)
141 7 : continue;
142 :
143 : best_cpu = cpu;
144 : maxavl = cm->available;
145 : }
146 3 : return best_cpu;
147 : }
148 :
149 : /* Find the best CPU which has the lowest number of managed IRQs allocated */
150 0 : static unsigned int matrix_find_best_cpu_managed(struct irq_matrix *m,
151 : const struct cpumask *msk)
152 : {
153 0 : unsigned int cpu, best_cpu, allocated = UINT_MAX;
154 0 : struct cpumap *cm;
155 :
156 0 : best_cpu = UINT_MAX;
157 :
158 0 : for_each_cpu(cpu, msk) {
159 0 : cm = per_cpu_ptr(m->maps, cpu);
160 :
161 0 : if (!cm->online || cm->managed_allocated > allocated)
162 0 : continue;
163 :
164 : best_cpu = cpu;
165 : allocated = cm->managed_allocated;
166 : }
167 0 : return best_cpu;
168 : }
169 :
170 : /**
171 : * irq_matrix_assign_system - Assign system wide entry in the matrix
172 : * @m: Matrix pointer
173 : * @bit: Which bit to reserve
174 : * @replace: Replace an already allocated vector with a system
175 : * vector at the same bit position.
176 : *
177 : * The BUG_ON()s below are on purpose. If this goes wrong in the
178 : * early boot process, then the chance to survive is about zero.
179 : * If this happens when the system is life, it's not much better.
180 : */
181 34 : void irq_matrix_assign_system(struct irq_matrix *m, unsigned int bit,
182 : bool replace)
183 : {
184 34 : struct cpumap *cm = this_cpu_ptr(m->maps);
185 :
186 34 : BUG_ON(bit > m->matrix_bits);
187 34 : BUG_ON(m->online_maps > 1 || (m->online_maps && !replace));
188 :
189 34 : set_bit(bit, m->system_map);
190 34 : if (replace) {
191 0 : BUG_ON(!test_and_clear_bit(bit, cm->alloc_map));
192 0 : cm->allocated--;
193 0 : m->total_allocated--;
194 : }
195 34 : if (bit >= m->alloc_start && bit < m->alloc_end)
196 3 : m->systembits_inalloc++;
197 :
198 34 : trace_irq_matrix_assign_system(bit, m);
199 34 : }
200 :
201 : /**
202 : * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
203 : * @m: Matrix pointer
204 : * @msk: On which CPUs the bits should be reserved.
205 : *
206 : * Can be called for offline CPUs. Note, this will only reserve one bit
207 : * on all CPUs in @msk, but it's not guaranteed that the bits are at the
208 : * same offset on all CPUs
209 : */
210 0 : int irq_matrix_reserve_managed(struct irq_matrix *m, const struct cpumask *msk)
211 : {
212 0 : unsigned int cpu, failed_cpu;
213 :
214 0 : for_each_cpu(cpu, msk) {
215 0 : struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
216 0 : unsigned int bit;
217 :
218 0 : bit = matrix_alloc_area(m, cm, 1, true);
219 0 : if (bit >= m->alloc_end)
220 0 : goto cleanup;
221 0 : cm->managed++;
222 0 : if (cm->online) {
223 0 : cm->available--;
224 0 : m->global_available--;
225 : }
226 0 : trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
227 : }
228 : return 0;
229 0 : cleanup:
230 0 : failed_cpu = cpu;
231 0 : for_each_cpu(cpu, msk) {
232 0 : if (cpu == failed_cpu)
233 : break;
234 0 : irq_matrix_remove_managed(m, cpumask_of(cpu));
235 : }
236 : return -ENOSPC;
237 : }
238 :
239 : /**
240 : * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
241 : * @m: Matrix pointer
242 : * @msk: On which CPUs the bits should be removed
243 : *
244 : * Can be called for offline CPUs
245 : *
246 : * This removes not allocated managed interrupts from the map. It does
247 : * not matter which one because the managed interrupts free their
248 : * allocation when they shut down. If not, the accounting is screwed,
249 : * but all what can be done at this point is warn about it.
250 : */
251 0 : void irq_matrix_remove_managed(struct irq_matrix *m, const struct cpumask *msk)
252 : {
253 0 : unsigned int cpu;
254 :
255 0 : for_each_cpu(cpu, msk) {
256 0 : struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
257 0 : unsigned int bit, end = m->alloc_end;
258 :
259 0 : if (WARN_ON_ONCE(!cm->managed))
260 0 : continue;
261 :
262 : /* Get managed bit which are not allocated */
263 0 : bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
264 :
265 0 : bit = find_first_bit(m->scratch_map, end);
266 0 : if (WARN_ON_ONCE(bit >= end))
267 0 : continue;
268 :
269 0 : clear_bit(bit, cm->managed_map);
270 :
271 0 : cm->managed--;
272 0 : if (cm->online) {
273 0 : cm->available++;
274 0 : m->global_available++;
275 : }
276 0 : trace_irq_matrix_remove_managed(bit, cpu, m, cm);
277 : }
278 0 : }
279 :
280 : /**
281 : * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
282 : * @m: Matrix pointer
283 : * @cpu: On which CPU the interrupt should be allocated
284 : */
285 0 : int irq_matrix_alloc_managed(struct irq_matrix *m, const struct cpumask *msk,
286 : unsigned int *mapped_cpu)
287 : {
288 0 : unsigned int bit, cpu, end = m->alloc_end;
289 0 : struct cpumap *cm;
290 :
291 0 : if (cpumask_empty(msk))
292 : return -EINVAL;
293 :
294 0 : cpu = matrix_find_best_cpu_managed(m, msk);
295 0 : if (cpu == UINT_MAX)
296 : return -ENOSPC;
297 :
298 0 : cm = per_cpu_ptr(m->maps, cpu);
299 0 : end = m->alloc_end;
300 : /* Get managed bit which are not allocated */
301 0 : bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
302 0 : bit = find_first_bit(m->scratch_map, end);
303 0 : if (bit >= end)
304 : return -ENOSPC;
305 0 : set_bit(bit, cm->alloc_map);
306 0 : cm->allocated++;
307 0 : cm->managed_allocated++;
308 0 : m->total_allocated++;
309 0 : *mapped_cpu = cpu;
310 0 : trace_irq_matrix_alloc_managed(bit, cpu, m, cm);
311 0 : return bit;
312 : }
313 :
314 : /**
315 : * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
316 : * @m: Matrix pointer
317 : * @bit: Which bit to mark
318 : *
319 : * This should only be used to mark preallocated vectors
320 : */
321 15 : void irq_matrix_assign(struct irq_matrix *m, unsigned int bit)
322 : {
323 15 : struct cpumap *cm = this_cpu_ptr(m->maps);
324 :
325 30 : if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
326 : return;
327 15 : if (WARN_ON_ONCE(test_and_set_bit(bit, cm->alloc_map)))
328 : return;
329 15 : cm->allocated++;
330 15 : m->total_allocated++;
331 15 : cm->available--;
332 15 : m->global_available--;
333 15 : trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
334 : }
335 :
336 : /**
337 : * irq_matrix_reserve - Reserve interrupts
338 : * @m: Matrix pointer
339 : *
340 : * This is merily a book keeping call. It increments the number of globally
341 : * reserved interrupt bits w/o actually allocating them. This allows to
342 : * setup interrupt descriptors w/o assigning low level resources to it.
343 : * The actual allocation happens when the interrupt gets activated.
344 : */
345 15 : void irq_matrix_reserve(struct irq_matrix *m)
346 : {
347 15 : if (m->global_reserved <= m->global_available &&
348 15 : m->global_reserved + 1 > m->global_available)
349 0 : pr_warn("Interrupt reservation exceeds available resources\n");
350 :
351 15 : m->global_reserved++;
352 15 : trace_irq_matrix_reserve(m);
353 15 : }
354 :
355 : /**
356 : * irq_matrix_remove_reserved - Remove interrupt reservation
357 : * @m: Matrix pointer
358 : *
359 : * This is merily a book keeping call. It decrements the number of globally
360 : * reserved interrupt bits. This is used to undo irq_matrix_reserve() when the
361 : * interrupt was never in use and a real vector allocated, which undid the
362 : * reservation.
363 : */
364 0 : void irq_matrix_remove_reserved(struct irq_matrix *m)
365 : {
366 0 : m->global_reserved--;
367 0 : trace_irq_matrix_remove_reserved(m);
368 0 : }
369 :
370 : /**
371 : * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
372 : * @m: Matrix pointer
373 : * @msk: Which CPUs to search in
374 : * @reserved: Allocate previously reserved interrupts
375 : * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
376 : */
377 3 : int irq_matrix_alloc(struct irq_matrix *m, const struct cpumask *msk,
378 : bool reserved, unsigned int *mapped_cpu)
379 : {
380 3 : unsigned int cpu, bit;
381 3 : struct cpumap *cm;
382 :
383 : /*
384 : * Not required in theory, but matrix_find_best_cpu() uses
385 : * for_each_cpu() which ignores the cpumask on UP .
386 : */
387 3 : if (cpumask_empty(msk))
388 : return -EINVAL;
389 :
390 3 : cpu = matrix_find_best_cpu(m, msk);
391 3 : if (cpu == UINT_MAX)
392 : return -ENOSPC;
393 :
394 3 : cm = per_cpu_ptr(m->maps, cpu);
395 3 : bit = matrix_alloc_area(m, cm, 1, false);
396 3 : if (bit >= m->alloc_end)
397 : return -ENOSPC;
398 3 : cm->allocated++;
399 3 : cm->available--;
400 3 : m->total_allocated++;
401 3 : m->global_available--;
402 3 : if (reserved)
403 3 : m->global_reserved--;
404 3 : *mapped_cpu = cpu;
405 3 : trace_irq_matrix_alloc(bit, cpu, m, cm);
406 3 : return bit;
407 :
408 : }
409 :
410 : /**
411 : * irq_matrix_free - Free allocated interrupt in the matrix
412 : * @m: Matrix pointer
413 : * @cpu: Which CPU map needs be updated
414 : * @bit: The bit to remove
415 : * @managed: If true, the interrupt is managed and not accounted
416 : * as available.
417 : */
418 15 : void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
419 : unsigned int bit, bool managed)
420 : {
421 15 : struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
422 :
423 30 : if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
424 : return;
425 :
426 15 : clear_bit(bit, cm->alloc_map);
427 15 : cm->allocated--;
428 15 : if(managed)
429 0 : cm->managed_allocated--;
430 :
431 15 : if (cm->online)
432 15 : m->total_allocated--;
433 :
434 15 : if (!managed) {
435 15 : cm->available++;
436 15 : if (cm->online)
437 15 : m->global_available++;
438 : }
439 15 : trace_irq_matrix_free(bit, cpu, m, cm);
440 : }
441 :
442 : /**
443 : * irq_matrix_available - Get the number of globally available irqs
444 : * @m: Pointer to the matrix to query
445 : * @cpudown: If true, the local CPU is about to go down, adjust
446 : * the number of available irqs accordingly
447 : */
448 0 : unsigned int irq_matrix_available(struct irq_matrix *m, bool cpudown)
449 : {
450 0 : struct cpumap *cm = this_cpu_ptr(m->maps);
451 :
452 0 : if (!cpudown)
453 0 : return m->global_available;
454 0 : return m->global_available - cm->available;
455 : }
456 :
457 : /**
458 : * irq_matrix_reserved - Get the number of globally reserved irqs
459 : * @m: Pointer to the matrix to query
460 : */
461 0 : unsigned int irq_matrix_reserved(struct irq_matrix *m)
462 : {
463 0 : return m->global_reserved;
464 : }
465 :
466 : /**
467 : * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
468 : * @m: Pointer to the matrix to search
469 : *
470 : * This returns number of allocated irqs
471 : */
472 0 : unsigned int irq_matrix_allocated(struct irq_matrix *m)
473 : {
474 0 : struct cpumap *cm = this_cpu_ptr(m->maps);
475 :
476 0 : return cm->allocated;
477 : }
478 :
479 : #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
480 : /**
481 : * irq_matrix_debug_show - Show detailed allocation information
482 : * @sf: Pointer to the seq_file to print to
483 : * @m: Pointer to the matrix allocator
484 : * @ind: Indentation for the print format
485 : *
486 : * Note, this is a lockless snapshot.
487 : */
488 : void irq_matrix_debug_show(struct seq_file *sf, struct irq_matrix *m, int ind)
489 : {
490 : unsigned int nsys = bitmap_weight(m->system_map, m->matrix_bits);
491 : int cpu;
492 :
493 : seq_printf(sf, "Online bitmaps: %6u\n", m->online_maps);
494 : seq_printf(sf, "Global available: %6u\n", m->global_available);
495 : seq_printf(sf, "Global reserved: %6u\n", m->global_reserved);
496 : seq_printf(sf, "Total allocated: %6u\n", m->total_allocated);
497 : seq_printf(sf, "System: %u: %*pbl\n", nsys, m->matrix_bits,
498 : m->system_map);
499 : seq_printf(sf, "%*s| CPU | avl | man | mac | act | vectors\n", ind, " ");
500 : cpus_read_lock();
501 : for_each_online_cpu(cpu) {
502 : struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
503 :
504 : seq_printf(sf, "%*s %4d %4u %4u %4u %4u %*pbl\n", ind, " ",
505 : cpu, cm->available, cm->managed,
506 : cm->managed_allocated, cm->allocated,
507 : m->matrix_bits, cm->alloc_map);
508 : }
509 : cpus_read_unlock();
510 : }
511 : #endif
|