@@ -278,6 +278,9 @@ extern int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m);
extern int
irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
+int irq_create_affinity_mask(struct cpumask **affinity_mask,
+ unsigned int nr_vecs);
+
#else /* CONFIG_SMP */
static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
@@ -308,6 +311,13 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
{
return 0;
}
+
+static inline int irq_create_affinity_mask(struct cpumask **affinity_mask,
+ unsigned int nr_vecs)
+{
+ *affinity_mask = NULL;
+ return 0;
+}
#endif /* CONFIG_SMP */
/*
@@ -9,3 +9,4 @@ obj-$(CONFIG_GENERIC_IRQ_MIGRATION) += cpuhotplug.o
obj-$(CONFIG_PM_SLEEP) += pm.o
obj-$(CONFIG_GENERIC_MSI_IRQ) += msi.o
obj-$(CONFIG_GENERIC_IRQ_IPI) += ipi.o
+obj-$(CONFIG_SMP) += affinity.o
new file mode 100644
@@ -0,0 +1,54 @@
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/cpu.h>
+
+static int get_first_sibling(unsigned int cpu)
+{
+ unsigned int ret;
+
+ ret = cpumask_first(topology_sibling_cpumask(cpu));
+ if (ret < nr_cpu_ids)
+ return ret;
+ return cpu;
+}
+
+/*
+ * Take a map of online CPUs and the number of available interrupt vectors
+ * and generate an output cpumask suitable for spreading MSI/MSI-X vectors
+ * so that they are distributed as good as possible around the CPUs. If
+ * more vectors than CPUs are available we'll map one to each CPU,
+ * otherwise we map one to the first sibling of each socket.
+ *
+ * If there are more vectors than CPUs we will still only have one bit
+ * set per CPU, but interrupt code will keep on assining the vectors from
+ * the start of the bitmap until we run out of vectors.
+ */
+int irq_create_affinity_mask(struct cpumask **affinity_mask,
+ unsigned int nr_vecs)
+{
+ if (nr_vecs == 1) {
+ *affinity_mask = NULL;
+ return 0;
+ }
+
+ *affinity_mask = kzalloc(cpumask_size(), GFP_KERNEL);
+ if (!*affinity_mask)
+ return -ENOMEM;
+
+ if (nr_vecs >= num_online_cpus()) {
+ cpumask_copy(*affinity_mask, cpu_online_mask);
+ } else {
+ unsigned int cpu;
+
+ for_each_online_cpu(cpu) {
+ if (cpu == get_first_sibling(cpu))
+ cpumask_set_cpu(cpu, *affinity_mask);
+
+ if (--nr_vecs == 0)
+ break;
+ }
+ }
+
+ return 0;
+}
Signed-off-by: Christoph Hellwig <hch@lst.de> --- include/linux/interrupt.h | 10 +++++++++ kernel/irq/Makefile | 1 + kernel/irq/affinity.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 kernel/irq/affinity.c