/** * 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(unsignedint matrix_bits, unsignedint alloc_start, unsignedint alloc_end)
{ unsignedint cpu, matrix_size = BITS_TO_LONGS(matrix_bits); struct irq_matrix *m;
m = kzalloc(struct_size(m, scratch_map, matrix_size * 2), GFP_KERNEL); if (!m) return NULL;
/* Find the best CPU which has the lowest vector allocation count */ staticunsignedint matrix_find_best_cpu(struct irq_matrix *m, conststruct cpumask *msk)
{ unsignedint 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;
/* Find the best CPU which has the lowest number of managed IRQs allocated */ staticunsignedint matrix_find_best_cpu_managed(struct irq_matrix *m, conststruct cpumask *msk)
{ unsignedint 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;
/** * 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, unsignedint bit, bool replace)
{ struct cpumap *cm = this_cpu_ptr(m->maps);
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++;
trace_irq_matrix_assign_system(bit, m);
}
/** * 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, conststruct cpumask *msk)
{ unsignedint cpu, failed_cpu;
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--;
}
trace_irq_matrix_reserve_managed(bit, cpu, m, cm);
} 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, conststruct cpumask *msk)
{ unsignedint cpu;
/* 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++;
}
trace_irq_matrix_remove_managed(bit, cpu, m, cm);
}
}
/** * irq_matrix_alloc_managed - Allocate a managed interrupt in a CPU map * @m: Matrix pointer * @msk: Which CPUs to search in * @mapped_cpu: Pointer to store the CPU for which the irq was allocated
*/ int irq_matrix_alloc_managed(struct irq_matrix *m, conststruct cpumask *msk, unsignedint *mapped_cpu)
{ unsignedint bit, cpu, end; struct cpumap *cm;
if (cpumask_empty(msk)) return -EINVAL;
cpu = matrix_find_best_cpu_managed(m, msk); if (cpu == UINT_MAX) return -ENOSPC;
cm = per_cpu_ptr(m->maps, cpu);
end = m->alloc_end; /* 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++;
cm->managed_allocated++;
m->total_allocated++;
*mapped_cpu = cpu;
trace_irq_matrix_alloc_managed(bit, cpu, m, cm); 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, unsignedint 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--;
trace_irq_matrix_assign(bit, smp_processor_id(), m, cm);
}
/** * irq_matrix_reserve - Reserve interrupts * @m: Matrix pointer * * This is merely 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)
pr_warn("Interrupt reservation exceeds available resources\n");
/** * irq_matrix_remove_reserved - Remove interrupt reservation * @m: Matrix pointer * * This is merely 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--;
trace_irq_matrix_remove_reserved(m);
}
/** * 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, conststruct cpumask *msk, bool reserved, unsignedint *mapped_cpu)
{ unsignedint cpu, bit; struct cpumap *cm;
/* * 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;
cpu = matrix_find_best_cpu(m, msk); if (cpu == UINT_MAX) return -ENOSPC;
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;
}
/** * 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, unsignedint cpu, unsignedint 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;
if (WARN_ON_ONCE(!test_and_clear_bit(bit, cm->alloc_map))) return;
if (!managed) {
cm->available++; if (cm->online)
m->global_available++;
}
trace_irq_matrix_free(bit, cpu, m, cm);
}
/** * 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
*/ unsignedint irq_matrix_available(struct irq_matrix *m, bool cpudown)
{ struct cpumap *cm = this_cpu_ptr(m->maps);
if (!cpudown) return m->global_available; return m->global_available - cm->available;
}
/** * irq_matrix_reserved - Get the number of globally reserved irqs * @m: Pointer to the matrix to query
*/ unsignedint irq_matrix_reserved(struct irq_matrix *m)
{ return m->global_reserved;
}
/** * irq_matrix_allocated - Get the number of allocated non-managed irqs on the local CPU * @m: Pointer to the matrix to search * * This returns number of allocated non-managed interrupts.
*/ unsignedint irq_matrix_allocated(struct irq_matrix *m)
{ struct cpumap *cm = this_cpu_ptr(m->maps);
return cm->allocated - cm->managed_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)
{ unsignedint nsys = bitmap_weight(m->system_map, m->matrix_bits); int cpu;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.