Merge remote-tracking branches 'asoc/topic/adsp', 'asoc/topic/ak4613', 'asoc/topic...
[sfrench/cifs-2.6.git] / kernel / irq / affinity.c
1
2 #include <linux/interrupt.h>
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
5 #include <linux/cpu.h>
6
7 static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
8                                 int cpus_per_vec)
9 {
10         const struct cpumask *siblmsk;
11         int cpu, sibl;
12
13         for ( ; cpus_per_vec > 0; ) {
14                 cpu = cpumask_first(nmsk);
15
16                 /* Should not happen, but I'm too lazy to think about it */
17                 if (cpu >= nr_cpu_ids)
18                         return;
19
20                 cpumask_clear_cpu(cpu, nmsk);
21                 cpumask_set_cpu(cpu, irqmsk);
22                 cpus_per_vec--;
23
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)
29                                 break;
30                         if (!cpumask_test_and_clear_cpu(sibl, nmsk))
31                                 continue;
32                         cpumask_set_cpu(sibl, irqmsk);
33                         cpus_per_vec--;
34                 }
35         }
36 }
37
38 static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)
39 {
40         int n, nodes = 0;
41
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);
46                         nodes++;
47                 }
48         }
49         return nodes;
50 }
51
52 /**
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
56  *
57  * Returns the masks pointer or NULL if allocation failed.
58  */
59 struct cpumask *
60 irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
61 {
62         int n, nodes, 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;
67         cpumask_var_t nmsk;
68
69         if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
70                 return NULL;
71
72         masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
73         if (!masks)
74                 goto out;
75
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);
79
80         /* Stabilize the cpumasks */
81         get_online_cpus();
82         nodes = get_nodes_in_cpumask(cpu_online_mask, &nodemsk);
83
84         /*
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.
87          */
88         if (affv <= nodes) {
89                 for_each_node_mask(n, nodemsk) {
90                         cpumask_copy(masks + curvec, cpumask_of_node(n));
91                         if (++curvec == last_affv)
92                                 break;
93                 }
94                 goto done;
95         }
96
97         for_each_node_mask(n, nodemsk) {
98                 int ncpus, v, vecs_to_assign, vecs_per_node;
99
100                 /* Spread the vectors per node */
101                 vecs_per_node = (affv - (curvec - affd->pre_vectors)) / nodes;
102
103                 /* Get the cpus on this node which are in the mask */
104                 cpumask_and(nmsk, cpu_online_mask, cpumask_of_node(n));
105
106                 /* Calculate the number of cpus per vector */
107                 ncpus = cpumask_weight(nmsk);
108                 vecs_to_assign = min(vecs_per_node, ncpus);
109
110                 /* Account for rounding errors */
111                 extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
112
113                 for (v = 0; curvec < last_affv && v < vecs_to_assign;
114                      curvec++, v++) {
115                         cpus_per_vec = ncpus / vecs_to_assign;
116
117                         /* Account for extra vectors to compensate rounding errors */
118                         if (extra_vecs) {
119                                 cpus_per_vec++;
120                                 --extra_vecs;
121                         }
122                         irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
123                 }
124
125                 if (curvec >= last_affv)
126                         break;
127                 --nodes;
128         }
129
130 done:
131         put_online_cpus();
132
133         /* Fill out vectors at the end that don't need affinity */
134         for (; curvec < nvecs; curvec++)
135                 cpumask_copy(masks + curvec, irq_default_affinity);
136 out:
137         free_cpumask_var(nmsk);
138         return masks;
139 }
140
141 /**
142  * irq_calc_affinity_vectors - Calculate the optimal number of vectors
143  * @maxvec:     The maximum number of vectors available
144  * @affd:       Description of the affinity requirements
145  */
146 int irq_calc_affinity_vectors(int maxvec, const struct irq_affinity *affd)
147 {
148         int resv = affd->pre_vectors + affd->post_vectors;
149         int vecs = maxvec - resv;
150         int cpus;
151
152         /* Stabilize the cpumasks */
153         get_online_cpus();
154         cpus = cpumask_weight(cpu_online_mask);
155         put_online_cpus();
156
157         return min(cpus, vecs) + resv;
158 }