Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / kernel / irq / affinity.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
9a0ef98e
CH
2/*
3 * Copyright (C) 2016 Thomas Gleixner.
4 * Copyright (C) 2016-2017 Christoph Hellwig.
5 */
5e385a6e
CH
6#include <linux/interrupt.h>
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/cpu.h>
f7b3ea8c 10#include <linux/group_cpus.h>
5c903e10 11
c66d4bd1
ML
12static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
13{
14 affd->nr_sets = 1;
15 affd->set_size[0] = affvecs;
16}
17
b3e6aaa8
ML
18/**
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
22 *
bec04037 23 * Returns the irq_affinity_desc pointer or NULL if allocation failed.
b3e6aaa8 24 */
bec04037 25struct irq_affinity_desc *
9cfef55b 26irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
b3e6aaa8 27{
c66d4bd1 28 unsigned int affvecs, curvec, usedvecs, i;
bec04037 29 struct irq_affinity_desc *masks = NULL;
b3e6aaa8
ML
30
31 /*
c66d4bd1
ML
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()
491beed3 35 * callback so the device driver can adjust to the situation.
b3e6aaa8 36 */
491beed3 37 if (nvecs > affd->pre_vectors + affd->post_vectors)
c66d4bd1
ML
38 affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
39 else
40 affvecs = 0;
41
42 /*
43 * Simple invocations do not provide a calc_sets() callback. Install
a6a309ed 44 * the generic one.
c66d4bd1 45 */
a6a309ed 46 if (!affd->calc_sets)
c66d4bd1
ML
47 affd->calc_sets = default_calc_sets;
48
a6a309ed
TG
49 /* Recalculate the sets */
50 affd->calc_sets(affd, affvecs);
b3e6aaa8 51
9cfef55b
ML
52 if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
53 return NULL;
54
c66d4bd1
ML
55 /* Nothing to assign? */
56 if (!affvecs)
57 return NULL;
58
b3e6aaa8
ML
59 masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
60 if (!masks)
347253c4 61 return NULL;
b3e6aaa8
ML
62
63 /* Fill out vectors at the beginning that don't need affinity */
64 for (curvec = 0; curvec < affd->pre_vectors; curvec++)
bec04037 65 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
c66d4bd1 66
6da4b3ab
JA
67 /*
68 * Spread on present CPUs starting from affd->pre_vectors. If we
69 * have multiple sets, build each sets affinity mask separately.
70 */
c66d4bd1
ML
71 for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
72 unsigned int this_vecs = affd->set_size[i];
e7bdd7f0 73 int j;
523f1ea7 74 struct cpumask *result = group_cpus_evenly(this_vecs);
6da4b3ab 75
e7bdd7f0 76 if (!result) {
c2899c34 77 kfree(masks);
347253c4 78 return NULL;
6da4b3ab 79 }
e7bdd7f0
ML
80
81 for (j = 0; j < this_vecs; j++)
82 cpumask_copy(&masks[curvec + j].mask, &result[j]);
83 kfree(result);
84
6da4b3ab
JA
85 curvec += this_vecs;
86 usedvecs += this_vecs;
87 }
67c93c21
CH
88
89 /* Fill out vectors at the end that don't need affinity */
d3056812
ML
90 if (usedvecs >= affvecs)
91 curvec = affd->pre_vectors + affvecs;
92 else
93 curvec = affd->pre_vectors + usedvecs;
67c93c21 94 for (; curvec < nvecs; curvec++)
bec04037 95 cpumask_copy(&masks[curvec].mask, irq_default_affinity);
d3056812 96
c410abbb
DL
97 /* Mark the managed interrupts */
98 for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
99 masks[i].is_managed = 1;
100
34c3d981
TG
101 return masks;
102}
103
104/**
212bd846 105 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
6f9a22bc 106 * @minvec: The minimum number of vectors available
212bd846
CH
107 * @maxvec: The maximum number of vectors available
108 * @affd: Description of the affinity requirements
34c3d981 109 */
0145c30e
TG
110unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
111 const struct irq_affinity *affd)
34c3d981 112{
0145c30e
TG
113 unsigned int resv = affd->pre_vectors + affd->post_vectors;
114 unsigned int set_vecs;
34c3d981 115
6f9a22bc
MH
116 if (resv > minvec)
117 return 0;
118
c66d4bd1
ML
119 if (affd->calc_sets) {
120 set_vecs = maxvec - resv;
6da4b3ab 121 } else {
428e2116 122 cpus_read_lock();
6da4b3ab 123 set_vecs = cpumask_weight(cpu_possible_mask);
428e2116 124 cpus_read_unlock();
6da4b3ab
JA
125 }
126
0145c30e 127 return resv + min(set_vecs, maxvec - resv);
34c3d981 128}