Blame view

kernel/irq/matrix.c 13.2 KB
90cafdd52   Thomas Gleixner   genirq/matrix: Cl...
1
2
  // SPDX-License-Identifier: GPL-2.0
  // Copyright (C) 2017 Thomas Gleixner <tglx@linutronix.de>
2f75d9e1c   Thomas Gleixner   genirq: Implement...
3
4
5
6
7
8
  #include <linux/spinlock.h>
  #include <linux/seq_file.h>
  #include <linux/bitmap.h>
  #include <linux/percpu.h>
  #include <linux/cpu.h>
  #include <linux/irq.h>
57f01796f   Michael Kelley   irq/matrix: Fix m...
9
  #define IRQ_MATRIX_SIZE	(BITS_TO_LONGS(IRQ_MATRIX_BITS))
2f75d9e1c   Thomas Gleixner   genirq: Implement...
10
11
12
13
14
  
  struct cpumap {
  	unsigned int		available;
  	unsigned int		allocated;
  	unsigned int		managed;
e8da8794a   Long Li   genirq/matrix: Im...
15
  	unsigned int		managed_allocated;
651ca2c00   Thomas Gleixner   genirq/matrix: Ha...
16
  	bool			initialized;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  	bool			online;
  	unsigned long		alloc_map[IRQ_MATRIX_SIZE];
  	unsigned long		managed_map[IRQ_MATRIX_SIZE];
  };
  
  struct irq_matrix {
  	unsigned int		matrix_bits;
  	unsigned int		alloc_start;
  	unsigned int		alloc_end;
  	unsigned int		alloc_size;
  	unsigned int		global_available;
  	unsigned int		global_reserved;
  	unsigned int		systembits_inalloc;
  	unsigned int		total_allocated;
  	unsigned int		online_maps;
  	struct cpumap __percpu	*maps;
  	unsigned long		scratch_map[IRQ_MATRIX_SIZE];
  	unsigned long		system_map[IRQ_MATRIX_SIZE];
  };
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
36
37
  #define CREATE_TRACE_POINTS
  #include <trace/events/irq_matrix.h>
2f75d9e1c   Thomas Gleixner   genirq: Implement...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /**
   * irq_alloc_matrix - Allocate a irq_matrix structure and initialize it
   * @matrix_bits:	Number of matrix bits must be <= IRQ_MATRIX_BITS
   * @alloc_start:	From which bit the allocation search starts
   * @alloc_end:		At which bit the allocation search ends, i.e first
   *			invalid bit
   */
  __init struct irq_matrix *irq_alloc_matrix(unsigned int matrix_bits,
  					   unsigned int alloc_start,
  					   unsigned int alloc_end)
  {
  	struct irq_matrix *m;
  
  	if (matrix_bits > IRQ_MATRIX_BITS)
  		return NULL;
  
  	m = kzalloc(sizeof(*m), GFP_KERNEL);
  	if (!m)
  		return NULL;
  
  	m->matrix_bits = matrix_bits;
  	m->alloc_start = alloc_start;
  	m->alloc_end = alloc_end;
  	m->alloc_size = alloc_end - alloc_start;
  	m->maps = alloc_percpu(*m->maps);
  	if (!m->maps) {
  		kfree(m);
  		return NULL;
  	}
  	return m;
  }
  
  /**
   * irq_matrix_online - Bring the local CPU matrix online
   * @m:		Matrix pointer
   */
  void irq_matrix_online(struct irq_matrix *m)
  {
  	struct cpumap *cm = this_cpu_ptr(m->maps);
  
  	BUG_ON(cm->online);
651ca2c00   Thomas Gleixner   genirq/matrix: Ha...
79
80
81
82
83
  	if (!cm->initialized) {
  		cm->available = m->alloc_size;
  		cm->available -= cm->managed + m->systembits_inalloc;
  		cm->initialized = true;
  	}
2f75d9e1c   Thomas Gleixner   genirq: Implement...
84
85
86
  	m->global_available += cm->available;
  	cm->online = true;
  	m->online_maps++;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
87
  	trace_irq_matrix_online(m);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  }
  
  /**
   * irq_matrix_offline - Bring the local CPU matrix offline
   * @m:		Matrix pointer
   */
  void irq_matrix_offline(struct irq_matrix *m)
  {
  	struct cpumap *cm = this_cpu_ptr(m->maps);
  
  	/* Update the global available size */
  	m->global_available -= cm->available;
  	cm->online = false;
  	m->online_maps--;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
102
  	trace_irq_matrix_offline(m);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  }
  
  static unsigned int matrix_alloc_area(struct irq_matrix *m, struct cpumap *cm,
  				      unsigned int num, bool managed)
  {
  	unsigned int area, start = m->alloc_start;
  	unsigned int end = m->alloc_end;
  
  	bitmap_or(m->scratch_map, cm->managed_map, m->system_map, end);
  	bitmap_or(m->scratch_map, m->scratch_map, cm->alloc_map, end);
  	area = bitmap_find_next_zero_area(m->scratch_map, end, start, num, 0);
  	if (area >= end)
  		return area;
  	if (managed)
  		bitmap_set(cm->managed_map, area, num);
  	else
  		bitmap_set(cm->alloc_map, area, num);
  	return area;
  }
8ffe4e61c   Dou Liyang   irq/matrix: Split...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  /* Find the best CPU which has the lowest vector allocation count */
  static unsigned int matrix_find_best_cpu(struct irq_matrix *m,
  					const struct cpumask *msk)
  {
  	unsigned int cpu, best_cpu, maxavl = 0;
  	struct cpumap *cm;
  
  	best_cpu = UINT_MAX;
  
  	for_each_cpu(cpu, msk) {
  		cm = per_cpu_ptr(m->maps, cpu);
  
  		if (!cm->online || cm->available <= maxavl)
  			continue;
  
  		best_cpu = cpu;
  		maxavl = cm->available;
  	}
  	return best_cpu;
  }
e8da8794a   Long Li   genirq/matrix: Im...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  /* Find the best CPU which has the lowest number of managed IRQs allocated */
  static unsigned int matrix_find_best_cpu_managed(struct irq_matrix *m,
  						const struct cpumask *msk)
  {
  	unsigned int cpu, best_cpu, allocated = UINT_MAX;
  	struct cpumap *cm;
  
  	best_cpu = UINT_MAX;
  
  	for_each_cpu(cpu, msk) {
  		cm = per_cpu_ptr(m->maps, cpu);
  
  		if (!cm->online || cm->managed_allocated > allocated)
  			continue;
  
  		best_cpu = cpu;
  		allocated = cm->managed_allocated;
  	}
  	return best_cpu;
  }
2f75d9e1c   Thomas Gleixner   genirq: Implement...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  /**
   * irq_matrix_assign_system - Assign system wide entry in the matrix
   * @m:		Matrix pointer
   * @bit:	Which bit to reserve
   * @replace:	Replace an already allocated vector with a system
   *		vector at the same bit position.
   *
   * The BUG_ON()s below are on purpose. If this goes wrong in the
   * early boot process, then the chance to survive is about zero.
   * If this happens when the system is life, it's not much better.
   */
  void irq_matrix_assign_system(struct irq_matrix *m, unsigned int bit,
  			      bool replace)
  {
  	struct cpumap *cm = this_cpu_ptr(m->maps);
  
  	BUG_ON(bit > m->matrix_bits);
  	BUG_ON(m->online_maps > 1 || (m->online_maps && !replace));
  
  	set_bit(bit, m->system_map);
  	if (replace) {
  		BUG_ON(!test_and_clear_bit(bit, cm->alloc_map));
  		cm->allocated--;
  		m->total_allocated--;
  	}
  	if (bit >= m->alloc_start && bit < m->alloc_end)
  		m->systembits_inalloc++;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
189
190
  
  	trace_irq_matrix_assign_system(bit, m);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  }
  
  /**
   * irq_matrix_reserve_managed - Reserve a managed interrupt in a CPU map
   * @m:		Matrix pointer
   * @msk:	On which CPUs the bits should be reserved.
   *
   * Can be called for offline CPUs. Note, this will only reserve one bit
   * on all CPUs in @msk, but it's not guaranteed that the bits are at the
   * same offset on all CPUs
   */
  int irq_matrix_reserve_managed(struct irq_matrix *m, const struct cpumask *msk)
  {
  	unsigned int cpu, failed_cpu;
  
  	for_each_cpu(cpu, msk) {
  		struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
  		unsigned int bit;
  
  		bit = matrix_alloc_area(m, cm, 1, true);
  		if (bit >= m->alloc_end)
  			goto cleanup;
  		cm->managed++;
  		if (cm->online) {
  			cm->available--;
  			m->global_available--;
  		}
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
218
  		trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  	}
  	return 0;
  cleanup:
  	failed_cpu = cpu;
  	for_each_cpu(cpu, msk) {
  		if (cpu == failed_cpu)
  			break;
  		irq_matrix_remove_managed(m, cpumask_of(cpu));
  	}
  	return -ENOSPC;
  }
  
  /**
   * irq_matrix_remove_managed - Remove managed interrupts in a CPU map
   * @m:		Matrix pointer
   * @msk:	On which CPUs the bits should be removed
   *
   * Can be called for offline CPUs
   *
   * This removes not allocated managed interrupts from the map. It does
   * not matter which one because the managed interrupts free their
   * allocation when they shut down. If not, the accounting is screwed,
   * but all what can be done at this point is warn about it.
   */
  void irq_matrix_remove_managed(struct irq_matrix *m, const struct cpumask *msk)
  {
  	unsigned int cpu;
  
  	for_each_cpu(cpu, msk) {
  		struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
  		unsigned int bit, end = m->alloc_end;
  
  		if (WARN_ON_ONCE(!cm->managed))
  			continue;
  
  		/* Get managed bit which are not allocated */
  		bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
  
  		bit = find_first_bit(m->scratch_map, end);
  		if (WARN_ON_ONCE(bit >= end))
  			continue;
  
  		clear_bit(bit, cm->managed_map);
  
  		cm->managed--;
  		if (cm->online) {
  			cm->available++;
  			m->global_available++;
  		}
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
268
  		trace_irq_matrix_remove_managed(bit, cpu, m, cm);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
269
270
271
272
273
274
275
276
  	}
  }
  
  /**
   * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map
   * @m:		Matrix pointer
   * @cpu:	On which CPU the interrupt should be allocated
   */
76f99ae5b   Dou Liyang   irq/matrix: Sprea...
277
278
  int irq_matrix_alloc_managed(struct irq_matrix *m, const struct cpumask *msk,
  			     unsigned int *mapped_cpu)
2f75d9e1c   Thomas Gleixner   genirq: Implement...
279
  {
76f99ae5b   Dou Liyang   irq/matrix: Sprea...
280
281
282
283
284
  	unsigned int bit, cpu, end = m->alloc_end;
  	struct cpumap *cm;
  
  	if (cpumask_empty(msk))
  		return -EINVAL;
e8da8794a   Long Li   genirq/matrix: Im...
285
  	cpu = matrix_find_best_cpu_managed(m, msk);
76f99ae5b   Dou Liyang   irq/matrix: Sprea...
286
287
  	if (cpu == UINT_MAX)
  		return -ENOSPC;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
288

76f99ae5b   Dou Liyang   irq/matrix: Sprea...
289
290
  	cm = per_cpu_ptr(m->maps, cpu);
  	end = m->alloc_end;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
291
292
293
294
295
296
297
  	/* Get managed bit which are not allocated */
  	bitmap_andnot(m->scratch_map, cm->managed_map, cm->alloc_map, end);
  	bit = find_first_bit(m->scratch_map, end);
  	if (bit >= end)
  		return -ENOSPC;
  	set_bit(bit, cm->alloc_map);
  	cm->allocated++;
e8da8794a   Long Li   genirq/matrix: Im...
298
  	cm->managed_allocated++;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
299
  	m->total_allocated++;
76f99ae5b   Dou Liyang   irq/matrix: Sprea...
300
  	*mapped_cpu = cpu;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
301
  	trace_irq_matrix_alloc_managed(bit, cpu, m, cm);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  	return bit;
  }
  
  /**
   * irq_matrix_assign - Assign a preallocated interrupt in the local CPU map
   * @m:		Matrix pointer
   * @bit:	Which bit to mark
   *
   * This should only be used to mark preallocated vectors
   */
  void irq_matrix_assign(struct irq_matrix *m, unsigned int bit)
  {
  	struct cpumap *cm = this_cpu_ptr(m->maps);
  
  	if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
  		return;
  	if (WARN_ON_ONCE(test_and_set_bit(bit, cm->alloc_map)))
  		return;
  	cm->allocated++;
  	m->total_allocated++;
  	cm->available--;
  	m->global_available--;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
324
  	trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  }
  
  /**
   * irq_matrix_reserve - Reserve interrupts
   * @m:		Matrix pointer
   *
   * This is merily a book keeping call. It increments the number of globally
   * reserved interrupt bits w/o actually allocating them. This allows to
   * setup interrupt descriptors w/o assigning low level resources to it.
   * The actual allocation happens when the interrupt gets activated.
   */
  void irq_matrix_reserve(struct irq_matrix *m)
  {
  	if (m->global_reserved <= m->global_available &&
  	    m->global_reserved + 1 > m->global_available)
  		pr_warn("Interrupt reservation exceeds available resources
  ");
  
  	m->global_reserved++;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
344
  	trace_irq_matrix_reserve(m);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  }
  
  /**
   * irq_matrix_remove_reserved - Remove interrupt reservation
   * @m:		Matrix pointer
   *
   * This is merily a book keeping call. It decrements the number of globally
   * reserved interrupt bits. This is used to undo irq_matrix_reserve() when the
   * interrupt was never in use and a real vector allocated, which undid the
   * reservation.
   */
  void irq_matrix_remove_reserved(struct irq_matrix *m)
  {
  	m->global_reserved--;
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
359
  	trace_irq_matrix_remove_reserved(m);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
360
361
362
363
364
365
366
367
368
369
370
371
  }
  
  /**
   * irq_matrix_alloc - Allocate a regular interrupt in a CPU map
   * @m:		Matrix pointer
   * @msk:	Which CPUs to search in
   * @reserved:	Allocate previously reserved interrupts
   * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
   */
  int irq_matrix_alloc(struct irq_matrix *m, const struct cpumask *msk,
  		     bool reserved, unsigned int *mapped_cpu)
  {
8ffe4e61c   Dou Liyang   irq/matrix: Split...
372
  	unsigned int cpu, bit;
a0c9259dc   Thomas Gleixner   irq/matrix: Sprea...
373
  	struct cpumap *cm;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
374

784a08303   Thomas Gleixner   genirq/matrix: De...
375
376
377
378
379
380
  	/*
  	 * Not required in theory, but matrix_find_best_cpu() uses
  	 * for_each_cpu() which ignores the cpumask on UP .
  	 */
  	if (cpumask_empty(msk))
  		return -EINVAL;
8ffe4e61c   Dou Liyang   irq/matrix: Split...
381
382
383
  	cpu = matrix_find_best_cpu(m, msk);
  	if (cpu == UINT_MAX)
  		return -ENOSPC;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
384

8ffe4e61c   Dou Liyang   irq/matrix: Split...
385
386
387
388
389
390
391
392
393
394
395
396
397
  	cm = per_cpu_ptr(m->maps, cpu);
  	bit = matrix_alloc_area(m, cm, 1, false);
  	if (bit >= m->alloc_end)
  		return -ENOSPC;
  	cm->allocated++;
  	cm->available--;
  	m->total_allocated++;
  	m->global_available--;
  	if (reserved)
  		m->global_reserved--;
  	*mapped_cpu = cpu;
  	trace_irq_matrix_alloc(bit, cpu, m, cm);
  	return bit;
a0c9259dc   Thomas Gleixner   irq/matrix: Sprea...
398

2f75d9e1c   Thomas Gleixner   genirq: Implement...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  }
  
  /**
   * irq_matrix_free - Free allocated interrupt in the matrix
   * @m:		Matrix pointer
   * @cpu:	Which CPU map needs be updated
   * @bit:	The bit to remove
   * @managed:	If true, the interrupt is managed and not accounted
   *		as available.
   */
  void irq_matrix_free(struct irq_matrix *m, unsigned int cpu,
  		     unsigned int bit, bool managed)
  {
  	struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
  
  	if (WARN_ON_ONCE(bit < m->alloc_start || bit >= m->alloc_end))
  		return;
651ca2c00   Thomas Gleixner   genirq/matrix: Ha...
416
417
  	clear_bit(bit, cm->alloc_map);
  	cm->allocated--;
e8da8794a   Long Li   genirq/matrix: Im...
418
419
  	if(managed)
  		cm->managed_allocated--;
651ca2c00   Thomas Gleixner   genirq/matrix: Ha...
420
421
  
  	if (cm->online)
2f75d9e1c   Thomas Gleixner   genirq: Implement...
422
  		m->total_allocated--;
651ca2c00   Thomas Gleixner   genirq/matrix: Ha...
423
424
425
426
  
  	if (!managed) {
  		cm->available++;
  		if (cm->online)
2f75d9e1c   Thomas Gleixner   genirq: Implement...
427
  			m->global_available++;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
428
  	}
ec0f7cd27   Thomas Gleixner   genirq/matrix: Ad...
429
  	trace_irq_matrix_free(bit, cpu, m, cm);
2f75d9e1c   Thomas Gleixner   genirq: Implement...
430
431
432
433
434
435
436
437
438
439
440
  }
  
  /**
   * irq_matrix_available - Get the number of globally available irqs
   * @m:		Pointer to the matrix to query
   * @cpudown:	If true, the local CPU is about to go down, adjust
   *		the number of available irqs accordingly
   */
  unsigned int irq_matrix_available(struct irq_matrix *m, bool cpudown)
  {
  	struct cpumap *cm = this_cpu_ptr(m->maps);
bb5c43428   Thomas Gleixner   genirq/matrix: Fi...
441
442
443
  	if (!cpudown)
  		return m->global_available;
  	return m->global_available - cm->available;
2f75d9e1c   Thomas Gleixner   genirq: Implement...
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  }
  
  /**
   * irq_matrix_reserved - Get the number of globally reserved irqs
   * @m:		Pointer to the matrix to query
   */
  unsigned int irq_matrix_reserved(struct irq_matrix *m)
  {
  	return m->global_reserved;
  }
  
  /**
   * irq_matrix_allocated - Get the number of allocated irqs on the local cpu
   * @m:		Pointer to the matrix to search
   *
   * This returns number of allocated irqs
   */
  unsigned int irq_matrix_allocated(struct irq_matrix *m)
  {
  	struct cpumap *cm = this_cpu_ptr(m->maps);
  
  	return cm->allocated;
  }
  
  #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  /**
   * irq_matrix_debug_show - Show detailed allocation information
   * @sf:		Pointer to the seq_file to print to
   * @m:		Pointer to the matrix allocator
   * @ind:	Indentation for the print format
   *
   * Note, this is a lockless snapshot.
   */
  void irq_matrix_debug_show(struct seq_file *sf, struct irq_matrix *m, int ind)
  {
  	unsigned int nsys = bitmap_weight(m->system_map, m->matrix_bits);
  	int cpu;
  
  	seq_printf(sf, "Online bitmaps:   %6u
  ", m->online_maps);
  	seq_printf(sf, "Global available: %6u
  ", m->global_available);
  	seq_printf(sf, "Global reserved:  %6u
  ", m->global_reserved);
  	seq_printf(sf, "Total allocated:  %6u
  ", m->total_allocated);
  	seq_printf(sf, "System: %u: %*pbl
  ", nsys, m->matrix_bits,
  		   m->system_map);
e8da8794a   Long Li   genirq/matrix: Im...
493
494
  	seq_printf(sf, "%*s| CPU | avl | man | mac | act | vectors
  ", ind, " ");
2f75d9e1c   Thomas Gleixner   genirq: Implement...
495
496
497
  	cpus_read_lock();
  	for_each_online_cpu(cpu) {
  		struct cpumap *cm = per_cpu_ptr(m->maps, cpu);
e8da8794a   Long Li   genirq/matrix: Im...
498
499
500
501
  		seq_printf(sf, "%*s %4d  %4u  %4u  %4u %4u  %*pbl
  ", ind, " ",
  			   cpu, cm->available, cm->managed,
  			   cm->managed_allocated, cm->allocated,
2f75d9e1c   Thomas Gleixner   genirq: Implement...
502
503
504
505
506
  			   m->matrix_bits, cm->alloc_map);
  	}
  	cpus_read_unlock();
  }
  #endif