···142142 }143143144144 for (i = 0; i < nvec; i++) {145145- desc = alloc_msi_entry(dev);145145+ desc = alloc_msi_entry(dev, 1, NULL);146146 if (!desc)147147 break;148148149149 desc->platform.msi_priv_data = data;150150 desc->platform.msi_index = base + i;151151- desc->nvec_used = 1;152151 desc->irq = virq ? virq + i : 0;153152154153 list_add_tail(&desc->list, dev_to_msi_list(dev));
+102-57
drivers/pci/msi.c
···550550 return ret;551551}552552553553-static struct msi_desc *msi_setup_entry(struct pci_dev *dev, int nvec)553553+static struct msi_desc *554554+msi_setup_entry(struct pci_dev *dev, int nvec, bool affinity)554555{555555- u16 control;556556+ struct cpumask *masks = NULL;556557 struct msi_desc *entry;558558+ u16 control;559559+560560+ if (affinity) {561561+ masks = irq_create_affinity_masks(dev->irq_affinity, nvec);562562+ if (!masks)563563+ pr_err("Unable to allocate affinity masks, ignoring\n");564564+ }557565558566 /* MSI Entry Initialization */559559- entry = alloc_msi_entry(&dev->dev);567567+ entry = alloc_msi_entry(&dev->dev, nvec, masks);560568 if (!entry)561561- return NULL;569569+ goto out;562570563571 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);564572···577569 entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */578570 entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1;579571 entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec));580580- entry->nvec_used = nvec;581581- entry->affinity = dev->irq_affinity;582572583573 if (control & PCI_MSI_FLAGS_64BIT)584574 entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64;···587581 if (entry->msi_attrib.maskbit)588582 pci_read_config_dword(dev, entry->mask_pos, &entry->masked);589583584584+out:585585+ kfree(masks);590586 return entry;591587}592588···617609 * an error, and a positive return value indicates the number of interrupts618610 * which could have been allocated.619611 */620620-static int msi_capability_init(struct pci_dev *dev, int nvec)612612+static int msi_capability_init(struct pci_dev *dev, int nvec, bool affinity)621613{622614 struct msi_desc *entry;623615 int ret;···625617626618 pci_msi_set_enable(dev, 0); /* Disable MSI during set up */627619628628- entry = msi_setup_entry(dev, nvec);620620+ entry = msi_setup_entry(dev, nvec, affinity);629621 if (!entry)630622 return -ENOMEM;631623···688680}689681690682static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,691691- struct msix_entry *entries, int nvec)683683+ struct msix_entry *entries, int nvec,684684+ bool affinity)692685{693693- const struct cpumask *mask = NULL;686686+ struct cpumask *curmsk, *masks = NULL;694687 struct msi_desc *entry;695695- int cpu = -1, i;688688+ int ret, i;696689697697- for (i = 0; i < nvec; i++) {698698- if (dev->irq_affinity) {699699- cpu = cpumask_next(cpu, dev->irq_affinity);700700- if (cpu >= nr_cpu_ids)701701- cpu = cpumask_first(dev->irq_affinity);702702- mask = cpumask_of(cpu);703703- }690690+ if (affinity) {691691+ masks = irq_create_affinity_masks(dev->irq_affinity, nvec);692692+ if (!masks)693693+ pr_err("Unable to allocate affinity masks, ignoring\n");694694+ }704695705705- entry = alloc_msi_entry(&dev->dev);696696+ for (i = 0, curmsk = masks; i < nvec; i++) {697697+ entry = alloc_msi_entry(&dev->dev, 1, curmsk);706698 if (!entry) {707699 if (!i)708700 iounmap(base);709701 else710702 free_msi_irqs(dev);711703 /* No enough memory. Don't try again */712712- return -ENOMEM;704704+ ret = -ENOMEM;705705+ goto out;713706 }714707715708 entry->msi_attrib.is_msix = 1;···721712 entry->msi_attrib.entry_nr = i;722713 entry->msi_attrib.default_irq = dev->irq;723714 entry->mask_base = base;724724- entry->nvec_used = 1;725725- entry->affinity = mask;726715727716 list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));717717+ if (masks)718718+ curmsk++;728719 }729729-720720+ ret = 0;721721+out:722722+ kfree(masks);730723 return 0;731724}732725···757746 * single MSI-X irq. A return of zero indicates the successful setup of758747 * requested MSI-X entries with allocated irqs or non-zero for otherwise.759748 **/760760-static int msix_capability_init(struct pci_dev *dev,761761- struct msix_entry *entries, int nvec)749749+static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,750750+ int nvec, bool affinity)762751{763752 int ret;764753 u16 control;···773762 if (!base)774763 return -ENOMEM;775764776776- ret = msix_setup_entries(dev, base, entries, nvec);765765+ ret = msix_setup_entries(dev, base, entries, nvec, affinity);777766 if (ret)778767 return ret;779768···953942}954943EXPORT_SYMBOL(pci_msix_vec_count);955944956956-/**957957- * pci_enable_msix - configure device's MSI-X capability structure958958- * @dev: pointer to the pci_dev data structure of MSI-X device function959959- * @entries: pointer to an array of MSI-X entries (optional)960960- * @nvec: number of MSI-X irqs requested for allocation by device driver961961- *962962- * Setup the MSI-X capability structure of device function with the number963963- * of requested irqs upon its software driver call to request for964964- * MSI-X mode enabled on its hardware device function. A return of zero965965- * indicates the successful configuration of MSI-X capability structure966966- * with new allocated MSI-X irqs. A return of < 0 indicates a failure.967967- * Or a return of > 0 indicates that driver request is exceeding the number968968- * of irqs or MSI-X vectors available. Driver should use the returned value to969969- * re-send its request.970970- **/971971-int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)945945+static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries,946946+ int nvec, bool affinity)972947{973948 int nr_entries;974949 int i, j;···986989 dev_info(&dev->dev, "can't enable MSI-X (MSI IRQ already assigned)\n");987990 return -EINVAL;988991 }989989- return msix_capability_init(dev, entries, nvec);992992+ return msix_capability_init(dev, entries, nvec, affinity);993993+}994994+995995+/**996996+ * pci_enable_msix - configure device's MSI-X capability structure997997+ * @dev: pointer to the pci_dev data structure of MSI-X device function998998+ * @entries: pointer to an array of MSI-X entries (optional)999999+ * @nvec: number of MSI-X irqs requested for allocation by device driver10001000+ *10011001+ * Setup the MSI-X capability structure of device function with the number10021002+ * of requested irqs upon its software driver call to request for10031003+ * MSI-X mode enabled on its hardware device function. A return of zero10041004+ * indicates the successful configuration of MSI-X capability structure10051005+ * with new allocated MSI-X irqs. A return of < 0 indicates a failure.10061006+ * Or a return of > 0 indicates that driver request is exceeding the number10071007+ * of irqs or MSI-X vectors available. Driver should use the returned value to10081008+ * re-send its request.10091009+ **/10101010+int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)10111011+{10121012+ return __pci_enable_msix(dev, entries, nvec, false);9901013}9911014EXPORT_SYMBOL(pci_enable_msix);9921015···10591042static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,10601043 unsigned int flags)10611044{10451045+ bool affinity = flags & PCI_IRQ_AFFINITY;10621046 int nvec;10631047 int rc;10641048···10881070 nvec = maxvec;1089107110901072 for (;;) {10911091- if (flags & PCI_IRQ_AFFINITY) {10921092- dev->irq_affinity = irq_create_affinity_mask(&nvec);10731073+ if (affinity) {10741074+ nvec = irq_calc_affinity_vectors(dev->irq_affinity,10751075+ nvec);10931076 if (nvec < minvec)10941077 return -ENOSPC;10951078 }1096107910971097- rc = msi_capability_init(dev, nvec);10801080+ rc = msi_capability_init(dev, nvec, affinity);10981081 if (rc == 0)10991082 return nvec;11001100-11011101- kfree(dev->irq_affinity);11021102- dev->irq_affinity = NULL;1103108311041084 if (rc < 0)11051085 return rc;···11301114 struct msix_entry *entries, int minvec, int maxvec,11311115 unsigned int flags)11321116{11331133- int nvec = maxvec;11341134- int rc;11171117+ bool affinity = flags & PCI_IRQ_AFFINITY;11181118+ int rc, nvec = maxvec;1135111911361120 if (maxvec < minvec)11371121 return -ERANGE;1138112211391123 for (;;) {11401140- if (flags & PCI_IRQ_AFFINITY) {11411141- dev->irq_affinity = irq_create_affinity_mask(&nvec);11241124+ if (affinity) {11251125+ nvec = irq_calc_affinity_vectors(dev->irq_affinity,11261126+ nvec);11421127 if (nvec < minvec)11431128 return -ENOSPC;11441129 }1145113011461146- rc = pci_enable_msix(dev, entries, nvec);11311131+ rc = __pci_enable_msix(dev, entries, nvec, affinity);11471132 if (rc == 0)11481133 return nvec;11491149-11501150- kfree(dev->irq_affinity);11511151- dev->irq_affinity = NULL;1152113411531135 if (rc < 0)11541136 return rc;···12701256 return dev->irq + nr;12711257}12721258EXPORT_SYMBOL(pci_irq_vector);12591259+12601260+/**12611261+ * pci_irq_get_affinity - return the affinity of a particular msi vector12621262+ * @dev: PCI device to operate on12631263+ * @nr: device-relative interrupt vector index (0-based).12641264+ */12651265+const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr)12661266+{12671267+ if (dev->msix_enabled) {12681268+ struct msi_desc *entry;12691269+ int i = 0;12701270+12711271+ for_each_pci_msi_entry(entry, dev) {12721272+ if (i == nr)12731273+ return entry->affinity;12741274+ i++;12751275+ }12761276+ WARN_ON_ONCE(1);12771277+ return NULL;12781278+ } else if (dev->msi_enabled) {12791279+ struct msi_desc *entry = first_pci_msi_entry(dev);12801280+12811281+ if (WARN_ON_ONCE(!entry || nr >= entry->nvec_used))12821282+ return NULL;12831283+12841284+ return &entry->affinity[nr];12851285+ } else {12861286+ return cpu_possible_mask;12871287+ }12881288+}12891289+EXPORT_SYMBOL(pci_irq_get_affinity);1273129012741291struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)12751292{
+1-2
drivers/staging/fsl-mc/bus/mc-msi.c
···213213 struct msi_desc *msi_desc;214214215215 for (i = 0; i < irq_count; i++) {216216- msi_desc = alloc_msi_entry(dev);216216+ msi_desc = alloc_msi_entry(dev, 1, NULL);217217 if (!msi_desc) {218218 dev_err(dev, "Failed to allocate msi entry\n");219219 error = -ENOMEM;···221221 }222222223223 msi_desc->fsl_mc.msi_index = i;224224- msi_desc->nvec_used = 1;225224 INIT_LIST_HEAD(&msi_desc->list);226225 list_add_tail(&msi_desc->list, dev_to_msi_list(dev));227226 }
+11-3
include/linux/interrupt.h
···278278extern int279279irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);280280281281-struct cpumask *irq_create_affinity_mask(unsigned int *nr_vecs);281281+struct cpumask *irq_create_affinity_masks(const struct cpumask *affinity, int nvec);282282+int irq_calc_affinity_vectors(const struct cpumask *affinity, int maxvec);282283283284#else /* CONFIG_SMP */284285···312311 return 0;313312}314313315315-static inline struct cpumask *irq_create_affinity_mask(unsigned int *nr_vecs)314314+static inline struct cpumask *315315+irq_create_affinity_masks(const struct cpumask *affinity, int nvec)316316{317317- *nr_vecs = 1;318317 return NULL;319318}319319+320320+static inline int321321+irq_calc_affinity_vectors(const struct cpumask *affinity, int maxvec)322322+{323323+ return maxvec;324324+}325325+320326#endif /* CONFIG_SMP */321327322328/*
···13001300 unsigned int max_vecs, unsigned int flags);13011301void pci_free_irq_vectors(struct pci_dev *dev);13021302int pci_irq_vector(struct pci_dev *dev, unsigned int nr);13031303+const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev, int vec);1303130413041305#else13051306static inline int pci_msi_vec_count(struct pci_dev *dev) { return -ENOSYS; }···13421341 if (WARN_ON_ONCE(nr > 0))13431342 return -EINVAL;13441343 return dev->irq;13441344+}13451345+static inline const struct cpumask *pci_irq_get_affinity(struct pci_dev *pdev,13461346+ int vec)13471347+{13481348+ return cpu_possible_mask;13451349}13461350#endif13471351
+132-41
kernel/irq/affinity.c
···44#include <linux/slab.h>55#include <linux/cpu.h>6677-static int get_first_sibling(unsigned int cpu)77+static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,88+ int cpus_per_vec)89{99- unsigned int ret;1010+ const struct cpumask *siblmsk;1111+ int cpu, sibl;10121111- ret = cpumask_first(topology_sibling_cpumask(cpu));1212- if (ret < nr_cpu_ids)1313- return ret;1414- return cpu;1313+ for ( ; cpus_per_vec > 0; ) {1414+ cpu = cpumask_first(nmsk);1515+1616+ /* Should not happen, but I'm too lazy to think about it */1717+ if (cpu >= nr_cpu_ids)1818+ return;1919+2020+ cpumask_clear_cpu(cpu, nmsk);2121+ cpumask_set_cpu(cpu, irqmsk);2222+ cpus_per_vec--;2323+2424+ /* If the cpu has siblings, use them first */2525+ siblmsk = topology_sibling_cpumask(cpu);2626+ for (sibl = -1; cpus_per_vec > 0; ) {2727+ sibl = cpumask_next(sibl, siblmsk);2828+ if (sibl >= nr_cpu_ids)2929+ break;3030+ if (!cpumask_test_and_clear_cpu(sibl, nmsk))3131+ continue;3232+ cpumask_set_cpu(sibl, irqmsk);3333+ cpus_per_vec--;3434+ }3535+ }1536}16371717-/*1818- * Take a map of online CPUs and the number of available interrupt vectors1919- * and generate an output cpumask suitable for spreading MSI/MSI-X vectors2020- * so that they are distributed as good as possible around the CPUs. If2121- * more vectors than CPUs are available we'll map one to each CPU,2222- * otherwise we map one to the first sibling of each socket.2323- *2424- * If there are more vectors than CPUs we will still only have one bit2525- * set per CPU, but interrupt code will keep on assigning the vectors from2626- * the start of the bitmap until we run out of vectors.2727- */2828-struct cpumask *irq_create_affinity_mask(unsigned int *nr_vecs)3838+static int get_nodes_in_cpumask(const struct cpumask *mask, nodemask_t *nodemsk)2939{3030- struct cpumask *affinity_mask;3131- unsigned int max_vecs = *nr_vecs;4040+ int n, nodes;32413333- if (max_vecs == 1)3434- return NULL;3535-3636- affinity_mask = kzalloc(cpumask_size(), GFP_KERNEL);3737- if (!affinity_mask) {3838- *nr_vecs = 1;3939- return NULL;4242+ /* Calculate the number of nodes in the supplied affinity mask */4343+ for (n = 0, nodes = 0; n < num_online_nodes(); n++) {4444+ if (cpumask_intersects(mask, cpumask_of_node(n))) {4545+ node_set(n, *nodemsk);4646+ nodes++;4747+ }4048 }4949+ return nodes;5050+}41515252+/**5353+ * irq_create_affinity_masks - Create affinity masks for multiqueue spreading5454+ * @affinity: The affinity mask to spread. If NULL cpu_online_mask5555+ * is used5656+ * @nvecs: The number of vectors5757+ *5858+ * Returns the masks pointer or NULL if allocation failed.5959+ */6060+struct cpumask *irq_create_affinity_masks(const struct cpumask *affinity,6161+ int nvec)6262+{6363+ int n, nodes, vecs_per_node, cpus_per_vec, extra_vecs, curvec = 0;6464+ nodemask_t nodemsk = NODE_MASK_NONE;6565+ struct cpumask *masks;6666+ cpumask_var_t nmsk;6767+6868+ if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))6969+ return NULL;7070+7171+ masks = kzalloc(nvec * sizeof(*masks), GFP_KERNEL);7272+ if (!masks)7373+ goto out;7474+7575+ /* Stabilize the cpumasks */4276 get_online_cpus();4343- if (max_vecs >= num_online_cpus()) {4444- cpumask_copy(affinity_mask, cpu_online_mask);4545- *nr_vecs = num_online_cpus();4646- } else {4747- unsigned int vecs = 0, cpu;7777+ /* If the supplied affinity mask is NULL, use cpu online mask */7878+ if (!affinity)7979+ affinity = cpu_online_mask;48804949- for_each_online_cpu(cpu) {5050- if (cpu == get_first_sibling(cpu)) {5151- cpumask_set_cpu(cpu, affinity_mask);5252- vecs++;5353- }8181+ nodes = get_nodes_in_cpumask(affinity, &nodemsk);54825555- if (--max_vecs == 0)8383+ /*8484+ * If the number of nodes in the mask is less than or equal the8585+ * number of vectors we just spread the vectors across the nodes.8686+ */8787+ if (nvec <= nodes) {8888+ for_each_node_mask(n, nodemsk) {8989+ cpumask_copy(masks + curvec, cpumask_of_node(n));9090+ if (++curvec == nvec)5691 break;5792 }5858- *nr_vecs = vecs;9393+ goto outonl;5994 }6060- put_online_cpus();61956262- return affinity_mask;9696+ /* Spread the vectors per node */9797+ vecs_per_node = nvec / nodes;9898+ /* Account for rounding errors */9999+ extra_vecs = nvec - (nodes * vecs_per_node);100100+101101+ for_each_node_mask(n, nodemsk) {102102+ int ncpus, v, vecs_to_assign = vecs_per_node;103103+104104+ /* Get the cpus on this node which are in the mask */105105+ cpumask_and(nmsk, affinity, cpumask_of_node(n));106106+107107+ /* Calculate the number of cpus per vector */108108+ ncpus = cpumask_weight(nmsk);109109+110110+ for (v = 0; curvec < nvec && v < vecs_to_assign; curvec++, v++) {111111+ cpus_per_vec = ncpus / vecs_to_assign;112112+113113+ /* Account for extra vectors to compensate rounding errors */114114+ if (extra_vecs) {115115+ cpus_per_vec++;116116+ if (!--extra_vecs)117117+ vecs_per_node++;118118+ }119119+ irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);120120+ }121121+122122+ if (curvec >= nvec)123123+ break;124124+ }125125+126126+outonl:127127+ put_online_cpus();128128+out:129129+ free_cpumask_var(nmsk);130130+ return masks;131131+}132132+133133+/**134134+ * irq_calc_affinity_vectors - Calculate to optimal number of vectors for a given affinity mask135135+ * @affinity: The affinity mask to spread. If NULL cpu_online_mask136136+ * is used137137+ * @maxvec: The maximum number of vectors available138138+ */139139+int irq_calc_affinity_vectors(const struct cpumask *affinity, int maxvec)140140+{141141+ int cpus, ret;142142+143143+ /* Stabilize the cpumasks */144144+ get_online_cpus();145145+ /* If the supplied affinity mask is NULL, use cpu online mask */146146+ if (!affinity)147147+ affinity = cpu_online_mask;148148+149149+ cpus = cpumask_weight(affinity);150150+ ret = (cpus < maxvec) ? cpus : maxvec;151151+152152+ put_online_cpus();153153+ return ret;63154}
+15-16
kernel/irq/irqdesc.c
···424424 const struct cpumask *mask = NULL;425425 struct irq_desc *desc;426426 unsigned int flags;427427- int i, cpu = -1;427427+ int i;428428429429- if (affinity && cpumask_empty(affinity))430430- return -EINVAL;429429+ /* Validate affinity mask(s) */430430+ if (affinity) {431431+ for (i = 0, mask = affinity; i < cnt; i++, mask++) {432432+ if (cpumask_empty(mask))433433+ return -EINVAL;434434+ }435435+ }431436432437 flags = affinity ? IRQD_AFFINITY_MANAGED : 0;438438+ mask = NULL;433439434440 for (i = 0; i < cnt; i++) {435441 if (affinity) {436436- cpu = cpumask_next(cpu, affinity);437437- if (cpu >= nr_cpu_ids)438438- cpu = cpumask_first(affinity);439439- node = cpu_to_node(cpu);440440-441441- /*442442- * For single allocations we use the caller provided443443- * mask otherwise we use the mask of the target cpu444444- */445445- mask = cnt == 1 ? affinity : cpumask_of(cpu);442442+ node = cpu_to_node(cpumask_first(affinity));443443+ mask = affinity;444444+ affinity++;446445 }447446 desc = alloc_desc(start + i, node, flags, mask, owner);448447 if (!desc)···669670 * @cnt: Number of consecutive irqs to allocate.670671 * @node: Preferred node on which the irq descriptor should be allocated671672 * @owner: Owning module (can be NULL)672672- * @affinity: Optional pointer to an affinity mask which hints where the673673- * irq descriptors should be allocated and which default674674- * affinities to use673673+ * @affinity: Optional pointer to an affinity mask array of size @cnt which674674+ * hints where the irq descriptors should be allocated and which675675+ * default affinities to use675676 *676677 * Returns the first irq number or error code677678 */
+24-2
kernel/irq/msi.c
···1818/* Temparory solution for building, will be removed later */1919#include <linux/pci.h>20202121-struct msi_desc *alloc_msi_entry(struct device *dev)2121+/**2222+ * alloc_msi_entry - Allocate an initialize msi_entry2323+ * @dev: Pointer to the device for which this is allocated2424+ * @nvec: The number of vectors used in this entry2525+ * @affinity: Optional pointer to an affinity mask array size of @nvec2626+ *2727+ * If @affinity is not NULL then a an affinity array[@nvec] is allocated2828+ * and the affinity masks from @affinity are copied.2929+ */3030+struct msi_desc *3131+alloc_msi_entry(struct device *dev, int nvec, const struct cpumask *affinity)2232{2323- struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);3333+ struct msi_desc *desc;3434+3535+ desc = kzalloc(sizeof(*desc), GFP_KERNEL);2436 if (!desc)2537 return NULL;26382739 INIT_LIST_HEAD(&desc->list);2840 desc->dev = dev;4141+ desc->nvec_used = nvec;4242+ if (affinity) {4343+ desc->affinity = kmemdup(affinity,4444+ nvec * sizeof(*desc->affinity), GFP_KERNEL);4545+ if (!desc->affinity) {4646+ kfree(desc);4747+ return NULL;4848+ }4949+ }29503051 return desc;3152}32533354void free_msi_entry(struct msi_desc *entry)3455{5656+ kfree(entry->affinity);3557 kfree(entry);3658}3759