1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
10 #include <linux/group_cpus.h>
12 static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
15 affd->set_size[0] = affvecs;
19 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
20 * @nvecs: The total number of vectors
21 * @affd: Description of the affinity requirements
23 * Returns the irq_affinity_desc pointer or NULL if allocation failed.
25 struct irq_affinity_desc *
26 irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
28 unsigned int affvecs, curvec, usedvecs, i;
29 struct irq_affinity_desc *masks = NULL;
32 * Determine the number of vectors which need interrupt affinities
33 * assigned. If the pre/post request exhausts the available vectors
34 * then nothing to do here except for invoking the calc_sets()
35 * callback so the device driver can adjust to the situation.
37 if (nvecs > affd->pre_vectors + affd->post_vectors)
38 affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
43 * Simple invocations do not provide a calc_sets() callback. Install
47 affd->calc_sets = default_calc_sets;
49 /* Recalculate the sets */
50 affd->calc_sets(affd, affvecs);
52 if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
55 /* Nothing to assign? */
59 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
63 /* Fill out vectors at the beginning that don't need affinity */
64 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
65 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
68 * Spread on present CPUs starting from affd->pre_vectors. If we
69 * have multiple sets, build each sets affinity mask separately.
71 for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
72 unsigned int this_vecs = affd->set_size[i];
74 struct cpumask *result = group_cpus_evenly(this_vecs);
81 for (j = 0; j < this_vecs; j++)
82 cpumask_copy(&masks[curvec + j].mask, &result[j]);
86 usedvecs += this_vecs;
89 /* Fill out vectors at the end that don't need affinity */
90 if (usedvecs >= affvecs)
91 curvec = affd->pre_vectors + affvecs;
93 curvec = affd->pre_vectors + usedvecs;
94 for (; curvec < nvecs; curvec++)
95 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
97 /* Mark the managed interrupts */
98 for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
99 masks[i].is_managed = 1;
105 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
106 * @minvec: The minimum number of vectors available
107 * @maxvec: The maximum number of vectors available
108 * @affd: Description of the affinity requirements
110 unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
111 const struct irq_affinity *affd)
113 unsigned int resv = affd->pre_vectors + affd->post_vectors;
114 unsigned int set_vecs;
119 if (affd->calc_sets) {
120 set_vecs = maxvec - resv;
123 set_vecs = cpumask_weight(cpu_possible_mask);
127 return resv + min(set_vecs, maxvec - resv);