2 #include <linux/interrupt.h>
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
7 static void irq_spread_init_one(struct cpumask
*irqmsk
, struct cpumask
*nmsk
,
10 const struct cpumask
*siblmsk
;
13 for ( ; cpus_per_vec
> 0; ) {
14 cpu
= cpumask_first(nmsk
);
16 /* Should not happen, but I'm too lazy to think about it */
17 if (cpu
>= nr_cpu_ids
)
20 cpumask_clear_cpu(cpu
, nmsk
);
21 cpumask_set_cpu(cpu
, irqmsk
);
24 /* If the cpu has siblings, use them first */
25 siblmsk
= topology_sibling_cpumask(cpu
);
26 for (sibl
= -1; cpus_per_vec
> 0; ) {
27 sibl
= cpumask_next(sibl
, siblmsk
);
28 if (sibl
>= nr_cpu_ids
)
30 if (!cpumask_test_and_clear_cpu(sibl
, nmsk
))
32 cpumask_set_cpu(sibl
, irqmsk
);
38 static int get_nodes_in_cpumask(const struct cpumask
*mask
, nodemask_t
*nodemsk
)
42 /* Calculate the number of nodes in the supplied affinity mask */
43 for_each_online_node(n
) {
44 if (cpumask_intersects(mask
, cpumask_of_node(n
))) {
45 node_set(n
, *nodemsk
);
53 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
54 * @nvecs: The total number of vectors
55 * @affd: Description of the affinity requirements
57 * Returns the masks pointer or NULL if allocation failed.
60 irq_create_affinity_masks(int nvecs
, const struct irq_affinity
*affd
)
62 int n
, nodes
, vecs_per_node
, cpus_per_vec
, extra_vecs
, curvec
;
63 int affv
= nvecs
- affd
->pre_vectors
- affd
->post_vectors
;
64 int last_affv
= affv
+ affd
->pre_vectors
;
65 nodemask_t nodemsk
= NODE_MASK_NONE
;
66 struct cpumask
*masks
;
69 if (!zalloc_cpumask_var(&nmsk
, GFP_KERNEL
))
72 masks
= kcalloc(nvecs
, sizeof(*masks
), GFP_KERNEL
);
76 /* Fill out vectors at the beginning that don't need affinity */
77 for (curvec
= 0; curvec
< affd
->pre_vectors
; curvec
++)
78 cpumask_copy(masks
+ curvec
, irq_default_affinity
);
80 /* Stabilize the cpumasks */
82 nodes
= get_nodes_in_cpumask(cpu_online_mask
, &nodemsk
);
85 * If the number of nodes in the mask is greater than or equal the
86 * number of vectors we just spread the vectors across the nodes.
89 for_each_node_mask(n
, nodemsk
) {
90 cpumask_copy(masks
+ curvec
, cpumask_of_node(n
));
91 if (++curvec
== last_affv
)
97 /* Spread the vectors per node */
98 vecs_per_node
= affv
/ nodes
;
99 /* Account for rounding errors */
100 extra_vecs
= affv
- (nodes
* vecs_per_node
);
102 for_each_node_mask(n
, nodemsk
) {
103 int ncpus
, v
, vecs_to_assign
= vecs_per_node
;
105 /* Get the cpus on this node which are in the mask */
106 cpumask_and(nmsk
, cpu_online_mask
, cpumask_of_node(n
));
108 /* Calculate the number of cpus per vector */
109 ncpus
= cpumask_weight(nmsk
);
111 for (v
= 0; curvec
< last_affv
&& v
< vecs_to_assign
;
113 cpus_per_vec
= ncpus
/ vecs_to_assign
;
115 /* Account for extra vectors to compensate rounding errors */
121 irq_spread_init_one(masks
+ curvec
, nmsk
, cpus_per_vec
);
124 if (curvec
>= last_affv
)
131 /* Fill out vectors at the end that don't need affinity */
132 for (; curvec
< nvecs
; curvec
++)
133 cpumask_copy(masks
+ curvec
, irq_default_affinity
);
135 free_cpumask_var(nmsk
);
140 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
141 * @maxvec: The maximum number of vectors available
142 * @affd: Description of the affinity requirements
144 int irq_calc_affinity_vectors(int maxvec
, const struct irq_affinity
*affd
)
146 int resv
= affd
->pre_vectors
+ affd
->post_vectors
;
147 int vecs
= maxvec
- resv
;
150 /* Stabilize the cpumasks */
152 cpus
= cpumask_weight(cpu_online_mask
);
155 return min(cpus
, vecs
) + resv
;