Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'irq-msi-2022-01-13' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull MSI irq updates from Thomas Gleixner:
"Rework of the MSI interrupt infrastructure.

This is a treewide cleanup and consolidation of MSI interrupt handling
in preparation for further changes in this area which are necessary
to:

- address existing shortcomings in the VFIO area

- support the upcoming Interrupt Message Store functionality which
decouples the message store from the PCI config/MMIO space"

* tag 'irq-msi-2022-01-13' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (94 commits)
genirq/msi: Populate sysfs entry only once
PCI/MSI: Unbreak pci_irq_get_affinity()
genirq/msi: Convert storage to xarray
genirq/msi: Simplify sysfs handling
genirq/msi: Add abuse prevention comment to msi header
genirq/msi: Mop up old interfaces
genirq/msi: Convert to new functions
genirq/msi: Make interrupt allocation less convoluted
platform-msi: Simplify platform device MSI code
platform-msi: Let core code handle MSI descriptors
bus: fsl-mc-msi: Simplify MSI descriptor handling
soc: ti: ti_sci_inta_msi: Remove ti_sci_inta_msi_domain_free_irqs()
soc: ti: ti_sci_inta_msi: Rework MSI descriptor allocation
NTB/msi: Convert to msi_on_each_desc()
PCI: hv: Rework MSI handling
powerpc/mpic_u3msi: Use msi_for_each-desc()
powerpc/fsl_msi: Use msi_for_each_desc()
powerpc/pasemi/msi: Convert to msi_on_each_dec()
powerpc/cell/axon_msi: Convert to msi_on_each_desc()
powerpc/4xx/hsta: Rework MSI handling
...

+1783 -2088
-1
.clang-format
··· 216 216 - 'for_each_migratetype_order' 217 217 - 'for_each_msi_entry' 218 218 - 'for_each_msi_entry_safe' 219 - - 'for_each_msi_vector' 220 219 - 'for_each_net' 221 220 - 'for_each_net_continue_reverse' 222 221 - 'for_each_netdev'
+1 -1
Documentation/driver-api/pci/pci.rst
··· 13 13 .. kernel-doc:: drivers/pci/search.c 14 14 :export: 15 15 16 - .. kernel-doc:: drivers/pci/msi.c 16 + .. kernel-doc:: drivers/pci/msi/msi.c 17 17 :export: 18 18 19 19 .. kernel-doc:: drivers/pci/bus.c
+3 -29
arch/mips/pci/msi-octeon.c
··· 68 68 u64 search_mask; 69 69 int index; 70 70 71 + if (desc->pci.msi_attrib.is_msix) 72 + return -EINVAL; 73 + 71 74 /* 72 75 * Read the MSI config to figure out how many IRQs this device 73 76 * wants. Most devices only want 1, which will give ··· 182 179 183 180 irq_set_msi_desc(irq, desc); 184 181 pci_write_msi_msg(irq, &msg); 185 - return 0; 186 - } 187 - 188 - int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 189 - { 190 - struct msi_desc *entry; 191 - int ret; 192 - 193 - /* 194 - * MSI-X is not supported. 195 - */ 196 - if (type == PCI_CAP_ID_MSIX) 197 - return -EINVAL; 198 - 199 - /* 200 - * If an architecture wants to support multiple MSI, it needs to 201 - * override arch_setup_msi_irqs() 202 - */ 203 - if (type == PCI_CAP_ID_MSI && nvec > 1) 204 - return 1; 205 - 206 - for_each_pci_msi_entry(entry, dev) { 207 - ret = arch_setup_msi_irq(dev, entry); 208 - if (ret < 0) 209 - return ret; 210 - if (ret > 0) 211 - return -ENOSPC; 212 - } 213 - 214 182 return 0; 215 183 } 216 184
-25
arch/powerpc/boot/dts/bluestone.dts
··· 366 366 0x0 0x0 0x0 0x3 &UIC3 0xe 0x4 /* swizzled int C */ 367 367 0x0 0x0 0x0 0x4 &UIC3 0xf 0x4 /* swizzled int D */>; 368 368 }; 369 - 370 - MSI: ppc4xx-msi@C10000000 { 371 - compatible = "amcc,ppc4xx-msi", "ppc4xx-msi"; 372 - reg = < 0xC 0x10000000 0x100 373 - 0xC 0x10000000 0x100>; 374 - sdr-base = <0x36C>; 375 - msi-data = <0x00004440>; 376 - msi-mask = <0x0000ffe0>; 377 - interrupts =<0 1 2 3 4 5 6 7>; 378 - interrupt-parent = <&MSI>; 379 - #interrupt-cells = <1>; 380 - #address-cells = <0>; 381 - #size-cells = <0>; 382 - msi-available-ranges = <0x0 0x100>; 383 - interrupt-map = < 384 - 0 &UIC3 0x18 1 385 - 1 &UIC3 0x19 1 386 - 2 &UIC3 0x1A 1 387 - 3 &UIC3 0x1B 1 388 - 4 &UIC3 0x1C 1 389 - 5 &UIC3 0x1D 1 390 - 6 &UIC3 0x1E 1 391 - 7 &UIC3 0x1F 1 392 - >; 393 - }; 394 369 }; 395 370 };
-18
arch/powerpc/boot/dts/canyonlands.dts
··· 544 544 0x0 0x0 0x0 0x3 &UIC3 0x12 0x4 /* swizzled int C */ 545 545 0x0 0x0 0x0 0x4 &UIC3 0x13 0x4 /* swizzled int D */>; 546 546 }; 547 - 548 - MSI: ppc4xx-msi@C10000000 { 549 - compatible = "amcc,ppc4xx-msi", "ppc4xx-msi"; 550 - reg = < 0xC 0x10000000 0x100>; 551 - sdr-base = <0x36C>; 552 - msi-data = <0x00000000>; 553 - msi-mask = <0x44440000>; 554 - interrupt-count = <3>; 555 - interrupts = <0 1 2 3>; 556 - interrupt-parent = <&UIC3>; 557 - #interrupt-cells = <1>; 558 - #address-cells = <0>; 559 - #size-cells = <0>; 560 - interrupt-map = <0 &UIC3 0x18 1 561 - 1 &UIC3 0x19 1 562 - 2 &UIC3 0x1A 1 563 - 3 &UIC3 0x1B 1>; 564 - }; 565 547 }; 566 548 };
-18
arch/powerpc/boot/dts/katmai.dts
··· 442 442 0x0 0x0 0x0 0x4 &UIC3 0xb 0x4 /* swizzled int D */>; 443 443 }; 444 444 445 - MSI: ppc4xx-msi@400300000 { 446 - compatible = "amcc,ppc4xx-msi", "ppc4xx-msi"; 447 - reg = < 0x4 0x00300000 0x100>; 448 - sdr-base = <0x3B0>; 449 - msi-data = <0x00000000>; 450 - msi-mask = <0x44440000>; 451 - interrupt-count = <3>; 452 - interrupts =<0 1 2 3>; 453 - interrupt-parent = <&UIC0>; 454 - #interrupt-cells = <1>; 455 - #address-cells = <0>; 456 - #size-cells = <0>; 457 - interrupt-map = <0 &UIC0 0xC 1 458 - 1 &UIC0 0x0D 1 459 - 2 &UIC0 0x0E 1 460 - 3 &UIC0 0x0F 1>; 461 - }; 462 - 463 445 I2O: i2o@400100000 { 464 446 compatible = "ibm,i2o-440spe"; 465 447 reg = <0x00000004 0x00100000 0x100>;
-28
arch/powerpc/boot/dts/kilauea.dts
··· 403 403 0x0 0x0 0x0 0x3 &UIC2 0xd 0x4 /* swizzled int C */ 404 404 0x0 0x0 0x0 0x4 &UIC2 0xe 0x4 /* swizzled int D */>; 405 405 }; 406 - 407 - MSI: ppc4xx-msi@C10000000 { 408 - compatible = "amcc,ppc4xx-msi", "ppc4xx-msi"; 409 - reg = <0xEF620000 0x100>; 410 - sdr-base = <0x4B0>; 411 - msi-data = <0x00000000>; 412 - msi-mask = <0x44440000>; 413 - interrupt-count = <12>; 414 - interrupts = <0 1 2 3 4 5 6 7 8 9 0xA 0xB 0xC 0xD>; 415 - interrupt-parent = <&UIC2>; 416 - #interrupt-cells = <1>; 417 - #address-cells = <0>; 418 - #size-cells = <0>; 419 - interrupt-map = <0 &UIC2 0x10 1 420 - 1 &UIC2 0x11 1 421 - 2 &UIC2 0x12 1 422 - 2 &UIC2 0x13 1 423 - 2 &UIC2 0x14 1 424 - 2 &UIC2 0x15 1 425 - 2 &UIC2 0x16 1 426 - 2 &UIC2 0x17 1 427 - 2 &UIC2 0x18 1 428 - 2 &UIC2 0x19 1 429 - 2 &UIC2 0x1A 1 430 - 2 &UIC2 0x1B 1 431 - 2 &UIC2 0x1C 1 432 - 3 &UIC2 0x1D 1>; 433 - }; 434 406 }; 435 407 };
-19
arch/powerpc/boot/dts/redwood.dts
··· 358 358 0x0 0x0 0x0 0x4 &UIC3 0xb 0x4 /* swizzled int D */>; 359 359 }; 360 360 361 - MSI: ppc4xx-msi@400300000 { 362 - compatible = "amcc,ppc4xx-msi", "ppc4xx-msi"; 363 - reg = < 0x4 0x00300000 0x100 364 - 0x4 0x00300000 0x100>; 365 - sdr-base = <0x3B0>; 366 - msi-data = <0x00000000>; 367 - msi-mask = <0x44440000>; 368 - interrupt-count = <3>; 369 - interrupts =<0 1 2 3>; 370 - interrupt-parent = <&UIC0>; 371 - #interrupt-cells = <1>; 372 - #address-cells = <0>; 373 - #size-cells = <0>; 374 - interrupt-map = <0 &UIC0 0xC 1 375 - 1 &UIC0 0x0D 1 376 - 2 &UIC0 0x0E 1 377 - 3 &UIC0 0x0F 1>; 378 - }; 379 - 380 361 }; 381 362 382 363
-1
arch/powerpc/platforms/40x/Kconfig
··· 23 23 select PPC4xx_PCI_EXPRESS 24 24 select FORCE_PCI 25 25 select PCI_MSI 26 - select PPC4xx_MSI 27 26 help 28 27 This option enables support for the AMCC PPC405EX evaluation board. 29 28
-4
arch/powerpc/platforms/44x/Kconfig
··· 23 23 select APM821xx 24 24 select FORCE_PCI 25 25 select PCI_MSI 26 - select PPC4xx_MSI 27 26 select PPC4xx_PCI_EXPRESS 28 27 select IBM_EMAC_RGMII if IBM_EMAC 29 28 help ··· 72 73 select FORCE_PCI 73 74 select PPC4xx_PCI_EXPRESS 74 75 select PCI_MSI 75 - select PPC4xx_MSI 76 76 help 77 77 This option enables support for the AMCC PPC440SPe evaluation board. 78 78 ··· 113 115 select FORCE_PCI 114 116 select PPC4xx_PCI_EXPRESS 115 117 select PCI_MSI 116 - select PPC4xx_MSI 117 118 select IBM_EMAC_RGMII if IBM_EMAC 118 119 select IBM_EMAC_ZMII if IBM_EMAC 119 120 help ··· 138 141 select FORCE_PCI 139 142 select PPC4xx_PCI_EXPRESS 140 143 select PCI_MSI 141 - select PPC4xx_MSI 142 144 help 143 145 This option enables support for the AMCC PPC460SX Redwood board. 144 146
-1
arch/powerpc/platforms/4xx/Makefile
··· 3 3 obj-$(CONFIG_4xx_SOC) += soc.o 4 4 obj-$(CONFIG_PCI) += pci.o 5 5 obj-$(CONFIG_PPC4xx_HSTA_MSI) += hsta_msi.o 6 - obj-$(CONFIG_PPC4xx_MSI) += msi.o 7 6 obj-$(CONFIG_PPC4xx_CPM) += cpm.o 8 7 obj-$(CONFIG_PPC4xx_GPIO) += gpio.o
+2 -5
arch/powerpc/platforms/4xx/hsta_msi.c
··· 47 47 return -EINVAL; 48 48 } 49 49 50 - for_each_pci_msi_entry(entry, dev) { 50 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_NOTASSOCIATED) { 51 51 irq = msi_bitmap_alloc_hwirqs(&ppc4xx_hsta_msi.bmp, 1); 52 52 if (irq < 0) { 53 53 pr_debug("%s: Failed to allocate msi interrupt\n", ··· 105 105 struct msi_desc *entry; 106 106 int irq; 107 107 108 - for_each_pci_msi_entry(entry, dev) { 109 - if (!entry->irq) 110 - continue; 111 - 108 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_ASSOCIATED) { 112 109 irq = hsta_find_hwirq_offset(entry->irq); 113 110 114 111 /* entry->irq should always be in irq_map */
-281
arch/powerpc/platforms/4xx/msi.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-or-later 2 - /* 3 - * Adding PCI-E MSI support for PPC4XX SoCs. 4 - * 5 - * Copyright (c) 2010, Applied Micro Circuits Corporation 6 - * Authors: Tirumala R Marri <tmarri@apm.com> 7 - * Feng Kan <fkan@apm.com> 8 - */ 9 - 10 - #include <linux/irq.h> 11 - #include <linux/pci.h> 12 - #include <linux/msi.h> 13 - #include <linux/of_platform.h> 14 - #include <linux/interrupt.h> 15 - #include <linux/export.h> 16 - #include <linux/kernel.h> 17 - #include <asm/prom.h> 18 - #include <asm/hw_irq.h> 19 - #include <asm/ppc-pci.h> 20 - #include <asm/dcr.h> 21 - #include <asm/dcr-regs.h> 22 - #include <asm/msi_bitmap.h> 23 - 24 - #define PEIH_TERMADH 0x00 25 - #define PEIH_TERMADL 0x08 26 - #define PEIH_MSIED 0x10 27 - #define PEIH_MSIMK 0x18 28 - #define PEIH_MSIASS 0x20 29 - #define PEIH_FLUSH0 0x30 30 - #define PEIH_FLUSH1 0x38 31 - #define PEIH_CNTRST 0x48 32 - 33 - static int msi_irqs; 34 - 35 - struct ppc4xx_msi { 36 - u32 msi_addr_lo; 37 - u32 msi_addr_hi; 38 - void __iomem *msi_regs; 39 - int *msi_virqs; 40 - struct msi_bitmap bitmap; 41 - struct device_node *msi_dev; 42 - }; 43 - 44 - static struct ppc4xx_msi ppc4xx_msi; 45 - 46 - static int ppc4xx_msi_init_allocator(struct platform_device *dev, 47 - struct ppc4xx_msi *msi_data) 48 - { 49 - int err; 50 - 51 - err = msi_bitmap_alloc(&msi_data->bitmap, msi_irqs, 52 - dev->dev.of_node); 53 - if (err) 54 - return err; 55 - 56 - err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap); 57 - if (err < 0) { 58 - msi_bitmap_free(&msi_data->bitmap); 59 - return err; 60 - } 61 - 62 - return 0; 63 - } 64 - 65 - static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 66 - { 67 - int int_no = -ENOMEM; 68 - unsigned int virq; 69 - struct msi_msg msg; 70 - struct msi_desc *entry; 71 - struct ppc4xx_msi *msi_data = &ppc4xx_msi; 72 - 73 - dev_dbg(&dev->dev, "PCIE-MSI:%s called. vec %x type %d\n", 74 - __func__, nvec, type); 75 - if (type == PCI_CAP_ID_MSIX) 76 - pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n"); 77 - 78 - msi_data->msi_virqs = kmalloc_array(msi_irqs, sizeof(int), GFP_KERNEL); 79 - if (!msi_data->msi_virqs) 80 - return -ENOMEM; 81 - 82 - for_each_pci_msi_entry(entry, dev) { 83 - int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1); 84 - if (int_no >= 0) 85 - break; 86 - if (int_no < 0) { 87 - pr_debug("%s: fail allocating msi interrupt\n", 88 - __func__); 89 - } 90 - virq = irq_of_parse_and_map(msi_data->msi_dev, int_no); 91 - if (!virq) { 92 - dev_err(&dev->dev, "%s: fail mapping irq\n", __func__); 93 - msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1); 94 - return -ENOSPC; 95 - } 96 - dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq); 97 - 98 - /* Setup msi address space */ 99 - msg.address_hi = msi_data->msi_addr_hi; 100 - msg.address_lo = msi_data->msi_addr_lo; 101 - 102 - irq_set_msi_desc(virq, entry); 103 - msg.data = int_no; 104 - pci_write_msi_msg(virq, &msg); 105 - } 106 - return 0; 107 - } 108 - 109 - void ppc4xx_teardown_msi_irqs(struct pci_dev *dev) 110 - { 111 - struct msi_desc *entry; 112 - struct ppc4xx_msi *msi_data = &ppc4xx_msi; 113 - irq_hw_number_t hwirq; 114 - 115 - dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n"); 116 - 117 - for_each_pci_msi_entry(entry, dev) { 118 - if (!entry->irq) 119 - continue; 120 - hwirq = virq_to_hw(entry->irq); 121 - irq_set_msi_desc(entry->irq, NULL); 122 - irq_dispose_mapping(entry->irq); 123 - msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1); 124 - } 125 - } 126 - 127 - static int ppc4xx_setup_pcieh_hw(struct platform_device *dev, 128 - struct resource res, struct ppc4xx_msi *msi) 129 - { 130 - const u32 *msi_data; 131 - const u32 *msi_mask; 132 - const u32 *sdr_addr; 133 - dma_addr_t msi_phys; 134 - void *msi_virt; 135 - int err; 136 - 137 - sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL); 138 - if (!sdr_addr) 139 - return -EINVAL; 140 - 141 - msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL); 142 - if (!msi_data) 143 - return -EINVAL; 144 - 145 - msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL); 146 - if (!msi_mask) 147 - return -EINVAL; 148 - 149 - msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi"); 150 - if (!msi->msi_dev) 151 - return -ENODEV; 152 - 153 - msi->msi_regs = of_iomap(msi->msi_dev, 0); 154 - if (!msi->msi_regs) { 155 - dev_err(&dev->dev, "of_iomap failed\n"); 156 - err = -ENOMEM; 157 - goto node_put; 158 - } 159 - dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n", 160 - (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs)); 161 - 162 - msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL); 163 - if (!msi_virt) { 164 - err = -ENOMEM; 165 - goto iounmap; 166 - } 167 - msi->msi_addr_hi = upper_32_bits(msi_phys); 168 - msi->msi_addr_lo = lower_32_bits(msi_phys & 0xffffffff); 169 - dev_dbg(&dev->dev, "PCIE-MSI: msi address high 0x%x, low 0x%x\n", 170 - msi->msi_addr_hi, msi->msi_addr_lo); 171 - 172 - mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */ 173 - mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */ 174 - 175 - /* Progam the Interrupt handler Termination addr registers */ 176 - out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi); 177 - out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo); 178 - 179 - /* Program MSI Expected data and Mask bits */ 180 - out_be32(msi->msi_regs + PEIH_MSIED, *msi_data); 181 - out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask); 182 - 183 - dma_free_coherent(&dev->dev, 64, msi_virt, msi_phys); 184 - 185 - return 0; 186 - 187 - iounmap: 188 - iounmap(msi->msi_regs); 189 - node_put: 190 - of_node_put(msi->msi_dev); 191 - return err; 192 - } 193 - 194 - static int ppc4xx_of_msi_remove(struct platform_device *dev) 195 - { 196 - struct ppc4xx_msi *msi = dev->dev.platform_data; 197 - int i; 198 - int virq; 199 - 200 - for (i = 0; i < msi_irqs; i++) { 201 - virq = msi->msi_virqs[i]; 202 - if (virq) 203 - irq_dispose_mapping(virq); 204 - } 205 - 206 - if (msi->bitmap.bitmap) 207 - msi_bitmap_free(&msi->bitmap); 208 - iounmap(msi->msi_regs); 209 - of_node_put(msi->msi_dev); 210 - 211 - return 0; 212 - } 213 - 214 - static int ppc4xx_msi_probe(struct platform_device *dev) 215 - { 216 - struct ppc4xx_msi *msi; 217 - struct resource res; 218 - int err = 0; 219 - struct pci_controller *phb; 220 - 221 - dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n"); 222 - 223 - msi = devm_kzalloc(&dev->dev, sizeof(*msi), GFP_KERNEL); 224 - if (!msi) 225 - return -ENOMEM; 226 - dev->dev.platform_data = msi; 227 - 228 - /* Get MSI ranges */ 229 - err = of_address_to_resource(dev->dev.of_node, 0, &res); 230 - if (err) { 231 - dev_err(&dev->dev, "%pOF resource error!\n", dev->dev.of_node); 232 - return err; 233 - } 234 - 235 - msi_irqs = of_irq_count(dev->dev.of_node); 236 - if (!msi_irqs) 237 - return -ENODEV; 238 - 239 - err = ppc4xx_setup_pcieh_hw(dev, res, msi); 240 - if (err) 241 - return err; 242 - 243 - err = ppc4xx_msi_init_allocator(dev, msi); 244 - if (err) { 245 - dev_err(&dev->dev, "Error allocating MSI bitmap\n"); 246 - goto error_out; 247 - } 248 - ppc4xx_msi = *msi; 249 - 250 - list_for_each_entry(phb, &hose_list, list_node) { 251 - phb->controller_ops.setup_msi_irqs = ppc4xx_setup_msi_irqs; 252 - phb->controller_ops.teardown_msi_irqs = ppc4xx_teardown_msi_irqs; 253 - } 254 - return 0; 255 - 256 - error_out: 257 - ppc4xx_of_msi_remove(dev); 258 - return err; 259 - } 260 - static const struct of_device_id ppc4xx_msi_ids[] = { 261 - { 262 - .compatible = "amcc,ppc4xx-msi", 263 - }, 264 - {} 265 - }; 266 - static struct platform_driver ppc4xx_msi_driver = { 267 - .probe = ppc4xx_msi_probe, 268 - .remove = ppc4xx_of_msi_remove, 269 - .driver = { 270 - .name = "ppc4xx-msi", 271 - .of_match_table = ppc4xx_msi_ids, 272 - }, 273 - 274 - }; 275 - 276 - static __init int ppc4xx_msi_init(void) 277 - { 278 - return platform_driver_register(&ppc4xx_msi_driver); 279 - } 280 - 281 - subsys_initcall(ppc4xx_msi_init);
+3 -9
arch/powerpc/platforms/cell/axon_msi.c
··· 199 199 static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg) 200 200 { 201 201 struct device_node *dn; 202 - struct msi_desc *entry; 203 202 int len; 204 203 const u32 *prop; 205 204 ··· 208 209 return -ENODEV; 209 210 } 210 211 211 - entry = first_pci_msi_entry(dev); 212 - 213 212 for (; dn; dn = of_get_next_parent(dn)) { 214 - if (entry->msi_attrib.is_64) { 213 + if (!dev->no_64bit_msi) { 215 214 prop = of_get_property(dn, "msi-address-64", &len); 216 215 if (prop) 217 216 break; ··· 262 265 if (rc) 263 266 return rc; 264 267 265 - for_each_pci_msi_entry(entry, dev) { 268 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_NOTASSOCIATED) { 266 269 virq = irq_create_direct_mapping(msic->irq_domain); 267 270 if (!virq) { 268 271 dev_warn(&dev->dev, ··· 285 288 286 289 dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n"); 287 290 288 - for_each_pci_msi_entry(entry, dev) { 289 - if (!entry->irq) 290 - continue; 291 - 291 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_ASSOCIATED) { 292 292 irq_set_msi_desc(entry->irq, NULL); 293 293 irq_dispose_mapping(entry->irq); 294 294 }
+2 -7
arch/powerpc/platforms/pasemi/msi.c
··· 62 62 63 63 pr_debug("pasemi_msi_teardown_msi_irqs, pdev %p\n", pdev); 64 64 65 - for_each_pci_msi_entry(entry, pdev) { 66 - if (!entry->irq) 67 - continue; 68 - 65 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { 69 66 hwirq = virq_to_hw(entry->irq); 70 67 irq_set_msi_desc(entry->irq, NULL); 71 68 irq_dispose_mapping(entry->irq); 72 69 msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, ALLOC_CHUNK); 73 70 } 74 - 75 - return; 76 71 } 77 72 78 73 static int pasemi_msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) ··· 85 90 msg.address_hi = 0; 86 91 msg.address_lo = PASEMI_MSI_ADDR; 87 92 88 - for_each_pci_msi_entry(entry, pdev) { 93 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) { 89 94 /* Allocate 16 interrupts for now, since that's the grouping for 90 95 * affinity. This can be changed later if it turns out 32 is too 91 96 * few MSIs for someone, but restrictions will apply to how the
+2 -2
arch/powerpc/platforms/powernv/pci-ioda.c
··· 2154 2154 int rc; 2155 2155 2156 2156 rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq, 2157 - entry->msi_attrib.is_64, msg); 2157 + entry->pci.msi_attrib.is_64, msg); 2158 2158 if (rc) 2159 2159 dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n", 2160 - entry->msi_attrib.is_64 ? "64" : "32", d->hwirq, rc); 2160 + entry->pci.msi_attrib.is_64 ? "64" : "32", d->hwirq, rc); 2161 2161 } 2162 2162 2163 2163 /*
+10 -28
arch/powerpc/platforms/pseries/msi.c
··· 321 321 return request; 322 322 } 323 323 324 - static int check_msix_entries(struct pci_dev *pdev) 325 - { 326 - struct msi_desc *entry; 327 - int expected; 328 - 329 - /* There's no way for us to express to firmware that we want 330 - * a discontiguous, or non-zero based, range of MSI-X entries. 331 - * So we must reject such requests. */ 332 - 333 - expected = 0; 334 - for_each_pci_msi_entry(entry, pdev) { 335 - if (entry->msi_attrib.entry_nr != expected) { 336 - pr_debug("rtas_msi: bad MSI-X entries.\n"); 337 - return -EINVAL; 338 - } 339 - expected++; 340 - } 341 - 342 - return 0; 343 - } 344 - 345 324 static void rtas_hack_32bit_msi_gen2(struct pci_dev *pdev) 346 325 { 347 326 u32 addr_hi, addr_lo; ··· 358 379 359 380 if (quota && quota < nvec) 360 381 return quota; 361 - 362 - if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev)) 363 - return -EINVAL; 364 382 365 383 /* 366 384 * Firmware currently refuse any non power of two allocation ··· 424 448 int nvec, msi_alloc_info_t *arg) 425 449 { 426 450 struct pci_dev *pdev = to_pci_dev(dev); 427 - struct msi_desc *desc = first_pci_msi_entry(pdev); 428 - int type = desc->msi_attrib.is_msix ? PCI_CAP_ID_MSIX : PCI_CAP_ID_MSI; 451 + int type = pdev->msix_enabled ? PCI_CAP_ID_MSIX : PCI_CAP_ID_MSI; 429 452 430 453 return rtas_prepare_msi_irqs(pdev, nvec, type, arg); 431 454 } ··· 505 530 .irq_write_msi_msg = pseries_msi_write_msg, 506 531 }; 507 532 533 + 534 + /* 535 + * Set MSI_FLAG_MSIX_CONTIGUOUS as there is no way to express to 536 + * firmware to request a discontiguous or non-zero based range of 537 + * MSI-X entries. Core code will reject such setup attempts. 538 + */ 508 539 static struct msi_domain_info pseries_msi_domain_info = { 509 540 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 510 - MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX), 541 + MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX | 542 + MSI_FLAG_MSIX_CONTIGUOUS), 511 543 .ops = &pseries_pci_msi_domain_ops, 512 544 .chip = &pseries_pci_msi_irq_chip, 513 545 }; ··· 562 580 int hwirq; 563 581 int i, ret; 564 582 565 - hwirq = rtas_query_irq_number(pci_get_pdn(pdev), desc->msi_attrib.entry_nr); 583 + hwirq = rtas_query_irq_number(pci_get_pdn(pdev), desc->msi_index); 566 584 if (hwirq < 0) { 567 585 dev_err(&pdev->dev, "Failed to query HW IRQ: %d\n", hwirq); 568 586 return hwirq;
-6
arch/powerpc/sysdev/Kconfig
··· 12 12 depends on PCI_MSI 13 13 depends on PCI && 4xx 14 14 15 - config PPC4xx_MSI 16 - bool 17 - depends on PCI_MSI 18 - depends on PCI && 4xx 19 - 20 15 config PPC_MSI_BITMAP 21 16 bool 22 17 depends on PCI_MSI 23 18 default y if MPIC 24 19 default y if FSL_PCI 25 - default y if PPC4xx_MSI 26 20 default y if PPC_POWERNV 27 21 28 22 source "arch/powerpc/sysdev/xics/Kconfig"
+2 -6
arch/powerpc/sysdev/fsl_msi.c
··· 125 125 struct fsl_msi *msi_data; 126 126 irq_hw_number_t hwirq; 127 127 128 - for_each_pci_msi_entry(entry, pdev) { 129 - if (!entry->irq) 130 - continue; 128 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { 131 129 hwirq = virq_to_hw(entry->irq); 132 130 msi_data = irq_get_chip_data(entry->irq); 133 131 irq_set_msi_desc(entry->irq, NULL); 134 132 irq_dispose_mapping(entry->irq); 135 133 msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1); 136 134 } 137 - 138 - return; 139 135 } 140 136 141 137 static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq, ··· 211 215 } 212 216 } 213 217 214 - for_each_pci_msi_entry(entry, pdev) { 218 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) { 215 219 /* 216 220 * Loop over all the MSI devices until we find one that has an 217 221 * available interrupt.
+2 -7
arch/powerpc/sysdev/mpic_u3msi.c
··· 104 104 struct msi_desc *entry; 105 105 irq_hw_number_t hwirq; 106 106 107 - for_each_pci_msi_entry(entry, pdev) { 108 - if (!entry->irq) 109 - continue; 110 - 107 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { 111 108 hwirq = virq_to_hw(entry->irq); 112 109 irq_set_msi_desc(entry->irq, NULL); 113 110 irq_dispose_mapping(entry->irq); 114 111 msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1); 115 112 } 116 - 117 - return; 118 113 } 119 114 120 115 static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) ··· 131 136 return -ENXIO; 132 137 } 133 138 134 - for_each_pci_msi_entry(entry, pdev) { 139 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) { 135 140 hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1); 136 141 if (hwirq < 0) { 137 142 pr_debug("u3msi: failed allocating hwirq\n");
+4 -6
arch/s390/pci/pci_irq.c
··· 303 303 304 304 /* Request MSI interrupts */ 305 305 hwirq = bit; 306 - for_each_pci_msi_entry(msi, pdev) { 306 + msi_for_each_desc(msi, &pdev->dev, MSI_DESC_NOTASSOCIATED) { 307 307 rc = -EIO; 308 308 if (hwirq - bit >= msi_vecs) 309 309 break; ··· 362 362 return; 363 363 364 364 /* Release MSI interrupts */ 365 - for_each_pci_msi_entry(msi, pdev) { 366 - if (!msi->irq) 367 - continue; 365 + msi_for_each_desc(msi, &pdev->dev, MSI_DESC_ASSOCIATED) { 368 366 irq_set_msi_desc(msi->irq, NULL); 369 367 irq_free_desc(msi->irq); 370 368 msi->msg.address_lo = 0; ··· 385 387 airq_iv_free(zpci_ibv[0], zdev->msi_first_bit, zdev->msi_nr_irqs); 386 388 } 387 389 388 - void arch_restore_msi_irqs(struct pci_dev *pdev) 390 + bool arch_restore_msi_irqs(struct pci_dev *pdev) 389 391 { 390 392 struct zpci_dev *zdev = to_zpci(pdev); 391 393 392 394 if (!zdev->irqs_registered) 393 395 zpci_set_irq(zdev); 394 - default_restore_msi_irqs(pdev); 396 + return true; 395 397 } 396 398 397 399 static struct airq_struct zpci_airq = {
+2 -2
arch/sparc/kernel/pci_msi.c
··· 146 146 msiqid = pick_msiq(pbm); 147 147 148 148 err = ops->msi_setup(pbm, msiqid, msi, 149 - (entry->msi_attrib.is_64 ? 1 : 0)); 149 + (entry->pci.msi_attrib.is_64 ? 1 : 0)); 150 150 if (err) 151 151 goto out_msi_free; 152 152 153 153 pbm->msi_irq_table[msi - pbm->msi_first] = *irq_p; 154 154 155 - if (entry->msi_attrib.is_64) { 155 + if (entry->pci.msi_attrib.is_64) { 156 156 msg.address_hi = pbm->msi64_start >> 32; 157 157 msg.address_lo = pbm->msi64_start & 0xffffffff; 158 158 } else {
+17 -38
arch/x86/hyperv/irqdomain.c
··· 253 253 return hv_unmap_interrupt(hv_build_pci_dev_id(dev).as_uint64, old_entry); 254 254 } 255 255 256 - static void hv_teardown_msi_irq_common(struct pci_dev *dev, struct msi_desc *msidesc, int irq) 256 + static void hv_teardown_msi_irq(struct pci_dev *dev, struct irq_data *irqd) 257 257 { 258 - u64 status; 259 258 struct hv_interrupt_entry old_entry; 260 - struct irq_desc *desc; 261 - struct irq_data *data; 262 259 struct msi_msg msg; 260 + u64 status; 263 261 264 - desc = irq_to_desc(irq); 265 - if (!desc) { 266 - pr_debug("%s: no irq desc\n", __func__); 267 - return; 268 - } 269 - 270 - data = &desc->irq_data; 271 - if (!data) { 272 - pr_debug("%s: no irq data\n", __func__); 273 - return; 274 - } 275 - 276 - if (!data->chip_data) { 262 + if (!irqd->chip_data) { 277 263 pr_debug("%s: no chip data\n!", __func__); 278 264 return; 279 265 } 280 266 281 - old_entry = *(struct hv_interrupt_entry *)data->chip_data; 267 + old_entry = *(struct hv_interrupt_entry *)irqd->chip_data; 282 268 entry_to_msi_msg(&old_entry, &msg); 283 269 284 - kfree(data->chip_data); 285 - data->chip_data = NULL; 270 + kfree(irqd->chip_data); 271 + irqd->chip_data = NULL; 286 272 287 273 status = hv_unmap_msi_interrupt(dev, &old_entry); 288 274 289 - if (status != HV_STATUS_SUCCESS) { 275 + if (status != HV_STATUS_SUCCESS) 290 276 pr_err("%s: hypercall failed, status %lld\n", __func__, status); 291 - return; 292 - } 293 277 } 294 278 295 - static void hv_msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) 279 + static void hv_msi_free_irq(struct irq_domain *domain, 280 + struct msi_domain_info *info, unsigned int virq) 296 281 { 297 - int i; 298 - struct msi_desc *entry; 299 - struct pci_dev *pdev; 282 + struct irq_data *irqd = irq_get_irq_data(virq); 283 + struct msi_desc *desc; 300 284 301 - if (WARN_ON_ONCE(!dev_is_pci(dev))) 285 + if (!irqd) 302 286 return; 303 287 304 - pdev = to_pci_dev(dev); 288 + desc = irq_data_get_msi_desc(irqd); 289 + if (!desc || !desc->irq || WARN_ON_ONCE(!dev_is_pci(desc->dev))) 290 + return; 305 291 306 - for_each_pci_msi_entry(entry, pdev) { 307 - if (entry->irq) { 308 - for (i = 0; i < entry->nvec_used; i++) { 309 - hv_teardown_msi_irq_common(pdev, entry, entry->irq + i); 310 - irq_domain_free_irqs(entry->irq + i, 1); 311 - } 312 - } 313 - } 292 + hv_teardown_msi_irq(to_pci_dev(desc->dev), irqd); 314 293 } 315 294 316 295 /* ··· 308 329 }; 309 330 310 331 static struct msi_domain_ops pci_msi_domain_ops = { 311 - .domain_free_irqs = hv_msi_domain_free_irqs, 332 + .msi_free = hv_msi_free_irq, 312 333 .msi_prepare = pci_msi_prepare, 313 334 }; 314 335
-6
arch/x86/include/asm/x86_init.h
··· 289 289 struct x86_hyper_runtime hyper; 290 290 }; 291 291 292 - struct pci_dev; 293 - 294 - struct x86_msi_ops { 295 - void (*restore_msi_irqs)(struct pci_dev *dev); 296 - }; 297 - 298 292 struct x86_apic_ops { 299 293 unsigned int (*io_apic_read) (unsigned int apic, unsigned int reg); 300 294 void (*restore)(void);
+8
arch/x86/include/asm/xen/hypervisor.h
··· 57 57 } 58 58 #endif 59 59 60 + struct pci_dev; 61 + 62 + #ifdef CONFIG_XEN_PV_DOM0 63 + bool xen_initdom_restore_msi(struct pci_dev *dev); 64 + #else 65 + static inline bool xen_initdom_restore_msi(struct pci_dev *dev) { return true; } 66 + #endif 67 + 60 68 #ifdef CONFIG_HOTPLUG_CPU 61 69 void xen_arch_register_cpu(int num); 62 70 void xen_arch_unregister_cpu(int num);
+7 -4
arch/x86/kernel/apic/msi.c
··· 19 19 #include <asm/hw_irq.h> 20 20 #include <asm/apic.h> 21 21 #include <asm/irq_remapping.h> 22 + #include <asm/xen/hypervisor.h> 22 23 23 24 struct irq_domain *x86_pci_msi_default_domain __ro_after_init; 24 25 ··· 160 159 int pci_msi_prepare(struct irq_domain *domain, struct device *dev, int nvec, 161 160 msi_alloc_info_t *arg) 162 161 { 163 - struct pci_dev *pdev = to_pci_dev(dev); 164 - struct msi_desc *desc = first_pci_msi_entry(pdev); 165 - 166 162 init_irq_alloc_info(arg, NULL); 167 - if (desc->msi_attrib.is_msix) { 163 + if (to_pci_dev(dev)->msix_enabled) { 168 164 arg->type = X86_IRQ_ALLOC_TYPE_PCI_MSIX; 169 165 } else { 170 166 arg->type = X86_IRQ_ALLOC_TYPE_PCI_MSI; ··· 343 345 irq_domain_free_irqs(irq, 1); 344 346 } 345 347 #endif 348 + 349 + bool arch_restore_msi_irqs(struct pci_dev *dev) 350 + { 351 + return xen_initdom_restore_msi(dev); 352 + }
-12
arch/x86/kernel/x86_init.c
··· 145 145 146 146 EXPORT_SYMBOL_GPL(x86_platform); 147 147 148 - #if defined(CONFIG_PCI_MSI) 149 - struct x86_msi_ops x86_msi __ro_after_init = { 150 - .restore_msi_irqs = default_restore_msi_irqs, 151 - }; 152 - 153 - /* MSI arch specific hooks */ 154 - void arch_restore_msi_irqs(struct pci_dev *dev) 155 - { 156 - x86_msi.restore_msi_irqs(dev); 157 - } 158 - #endif 159 - 160 148 struct x86_apic_ops x86_apic_ops __ro_after_init = { 161 149 .io_apic_read = native_io_apic_read, 162 150 .restore = native_restore_boot_irq_mode,
+16 -22
arch/x86/pci/xen.c
··· 184 184 if (ret) 185 185 goto error; 186 186 i = 0; 187 - for_each_pci_msi_entry(msidesc, dev) { 187 + msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_NOTASSOCIATED) { 188 188 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 189 189 (type == PCI_CAP_ID_MSI) ? nvec : 1, 190 190 (type == PCI_CAP_ID_MSIX) ? ··· 235 235 if (type == PCI_CAP_ID_MSI && nvec > 1) 236 236 return 1; 237 237 238 - for_each_pci_msi_entry(msidesc, dev) { 238 + msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_NOTASSOCIATED) { 239 239 pirq = xen_allocate_pirq_msi(dev, msidesc); 240 240 if (pirq < 0) { 241 241 irq = -ENODEV; ··· 270 270 int ret = 0; 271 271 struct msi_desc *msidesc; 272 272 273 - for_each_pci_msi_entry(msidesc, dev) { 273 + msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_NOTASSOCIATED) { 274 274 struct physdev_map_pirq map_irq; 275 275 domid_t domid; 276 276 ··· 306 306 return -EINVAL; 307 307 308 308 map_irq.table_base = pci_resource_start(dev, bir); 309 - map_irq.entry_nr = msidesc->msi_attrib.entry_nr; 309 + map_irq.entry_nr = msidesc->msi_index; 310 310 } 311 311 312 312 ret = -EINVAL; ··· 351 351 return ret; 352 352 } 353 353 354 - static void xen_initdom_restore_msi_irqs(struct pci_dev *dev) 354 + bool xen_initdom_restore_msi(struct pci_dev *dev) 355 355 { 356 356 int ret = 0; 357 + 358 + if (!xen_initial_domain()) 359 + return true; 357 360 358 361 if (pci_seg_supported) { 359 362 struct physdev_pci_device restore_ext; ··· 378 375 ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi, &restore); 379 376 WARN(ret && ret != -ENOSYS, "restore_msi -> %d\n", ret); 380 377 } 378 + return false; 381 379 } 382 380 #else /* CONFIG_XEN_PV_DOM0 */ 383 381 #define xen_initdom_setup_msi_irqs NULL 384 - #define xen_initdom_restore_msi_irqs NULL 385 382 #endif /* !CONFIG_XEN_PV_DOM0 */ 386 383 387 384 static void xen_teardown_msi_irqs(struct pci_dev *dev) ··· 389 386 struct msi_desc *msidesc; 390 387 int i; 391 388 392 - for_each_pci_msi_entry(msidesc, dev) { 393 - if (msidesc->irq) { 394 - for (i = 0; i < msidesc->nvec_used; i++) 395 - xen_destroy_irq(msidesc->irq + i); 396 - } 389 + msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_ASSOCIATED) { 390 + for (i = 0; i < msidesc->nvec_used; i++) 391 + xen_destroy_irq(msidesc->irq + i); 397 392 } 398 393 } 399 394 400 395 static void xen_pv_teardown_msi_irqs(struct pci_dev *dev) 401 396 { 402 - struct msi_desc *msidesc = first_pci_msi_entry(dev); 403 - 404 - if (msidesc->msi_attrib.is_msix) 397 + if (dev->msix_enabled) 405 398 xen_pci_frontend_disable_msix(dev); 406 399 else 407 400 xen_pci_frontend_disable_msi(dev); ··· 413 414 if (WARN_ON_ONCE(!dev_is_pci(dev))) 414 415 return -EINVAL; 415 416 416 - if (first_msi_entry(dev)->msi_attrib.is_msix) 417 - type = PCI_CAP_ID_MSIX; 418 - else 419 - type = PCI_CAP_ID_MSI; 417 + type = to_pci_dev(dev)->msix_enabled ? PCI_CAP_ID_MSIX : PCI_CAP_ID_MSI; 420 418 421 419 return xen_msi_ops.setup_msi_irqs(to_pci_dev(dev), nvec, type); 422 420 } ··· 462 466 static __init void xen_setup_pci_msi(void) 463 467 { 464 468 if (xen_pv_domain()) { 465 - if (xen_initial_domain()) { 469 + if (xen_initial_domain()) 466 470 xen_msi_ops.setup_msi_irqs = xen_initdom_setup_msi_irqs; 467 - x86_msi.restore_msi_irqs = xen_initdom_restore_msi_irqs; 468 - } else { 471 + else 469 472 xen_msi_ops.setup_msi_irqs = xen_setup_msi_irqs; 470 - } 471 473 xen_msi_ops.teardown_msi_irqs = xen_pv_teardown_msi_irqs; 472 474 pci_msi_ignore_mask = 1; 473 475 } else if (xen_hvm_domain()) {
-4
drivers/base/core.c
··· 2872 2872 INIT_LIST_HEAD(&dev->devres_head); 2873 2873 device_pm_init(dev); 2874 2874 set_dev_node(dev, NUMA_NO_NODE); 2875 - #ifdef CONFIG_GENERIC_MSI_IRQ 2876 - raw_spin_lock_init(&dev->msi_lock); 2877 - INIT_LIST_HEAD(&dev->msi_list); 2878 - #endif 2879 2875 INIT_LIST_HEAD(&dev->links.consumers); 2880 2876 INIT_LIST_HEAD(&dev->links.suppliers); 2881 2877 INIT_LIST_HEAD(&dev->links.defer_sync);
+76 -156
drivers/base/platform-msi.c
··· 23 23 struct platform_msi_priv_data { 24 24 struct device *dev; 25 25 void *host_data; 26 - const struct attribute_group **msi_irq_groups; 27 26 msi_alloc_info_t arg; 28 27 irq_write_msi_msg_t write_msg; 29 28 int devid; ··· 38 39 */ 39 40 static irq_hw_number_t platform_msi_calc_hwirq(struct msi_desc *desc) 40 41 { 41 - u32 devid; 42 + u32 devid = desc->dev->msi.data->platform_data->devid; 42 43 43 - devid = desc->platform.msi_priv_data->devid; 44 - 45 - return (devid << (32 - DEV_ID_SHIFT)) | desc->platform.msi_index; 44 + return (devid << (32 - DEV_ID_SHIFT)) | desc->msi_index; 46 45 } 47 46 48 47 static void platform_msi_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc) ··· 83 86 static void platform_msi_write_msg(struct irq_data *data, struct msi_msg *msg) 84 87 { 85 88 struct msi_desc *desc = irq_data_get_msi_desc(data); 86 - struct platform_msi_priv_data *priv_data; 87 89 88 - priv_data = desc->platform.msi_priv_data; 89 - 90 - priv_data->write_msg(desc, msg); 90 + desc->dev->msi.data->platform_data->write_msg(desc, msg); 91 91 } 92 92 93 93 static void platform_msi_update_chip_ops(struct msi_domain_info *info) ··· 105 111 if (WARN_ON((info->flags & MSI_FLAG_LEVEL_CAPABLE) && 106 112 !(chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI))) 107 113 info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; 108 - } 109 - 110 - static void platform_msi_free_descs(struct device *dev, int base, int nvec) 111 - { 112 - struct msi_desc *desc, *tmp; 113 - 114 - list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) { 115 - if (desc->platform.msi_index >= base && 116 - desc->platform.msi_index < (base + nvec)) { 117 - list_del(&desc->list); 118 - free_msi_entry(desc); 119 - } 120 - } 121 - } 122 - 123 - static int platform_msi_alloc_descs_with_irq(struct device *dev, int virq, 124 - int nvec, 125 - struct platform_msi_priv_data *data) 126 - 127 - { 128 - struct msi_desc *desc; 129 - int i, base = 0; 130 - 131 - if (!list_empty(dev_to_msi_list(dev))) { 132 - desc = list_last_entry(dev_to_msi_list(dev), 133 - struct msi_desc, list); 134 - base = desc->platform.msi_index + 1; 135 - } 136 - 137 - for (i = 0; i < nvec; i++) { 138 - desc = alloc_msi_entry(dev, 1, NULL); 139 - if (!desc) 140 - break; 141 - 142 - desc->platform.msi_priv_data = data; 143 - desc->platform.msi_index = base + i; 144 - desc->irq = virq ? virq + i : 0; 145 - 146 - list_add_tail(&desc->list, dev_to_msi_list(dev)); 147 - } 148 - 149 - if (i != nvec) { 150 - /* Clean up the mess */ 151 - platform_msi_free_descs(dev, base, nvec); 152 - 153 - return -ENOMEM; 154 - } 155 - 156 - return 0; 157 - } 158 - 159 - static int platform_msi_alloc_descs(struct device *dev, int nvec, 160 - struct platform_msi_priv_data *data) 161 - 162 - { 163 - return platform_msi_alloc_descs_with_irq(dev, 0, nvec, data); 164 114 } 165 115 166 116 /** ··· 129 191 platform_msi_update_dom_ops(info); 130 192 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 131 193 platform_msi_update_chip_ops(info); 194 + info->flags |= MSI_FLAG_DEV_SYSFS | MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS | 195 + MSI_FLAG_FREE_MSI_DESCS; 132 196 133 197 domain = msi_create_irq_domain(fwnode, info, parent); 134 198 if (domain) ··· 139 199 return domain; 140 200 } 141 201 142 - static struct platform_msi_priv_data * 143 - platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec, 144 - irq_write_msi_msg_t write_msi_msg) 202 + static int platform_msi_alloc_priv_data(struct device *dev, unsigned int nvec, 203 + irq_write_msi_msg_t write_msi_msg) 145 204 { 146 205 struct platform_msi_priv_data *datap; 206 + int err; 207 + 147 208 /* 148 209 * Limit the number of interrupts to 2048 per device. Should we 149 210 * need to bump this up, DEV_ID_SHIFT should be adjusted 150 211 * accordingly (which would impact the max number of MSI 151 212 * capable devices). 152 213 */ 153 - if (!dev->msi_domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS) 154 - return ERR_PTR(-EINVAL); 214 + if (!dev->msi.domain || !write_msi_msg || !nvec || nvec > MAX_DEV_MSIS) 215 + return -EINVAL; 155 216 156 - if (dev->msi_domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) { 217 + if (dev->msi.domain->bus_token != DOMAIN_BUS_PLATFORM_MSI) { 157 218 dev_err(dev, "Incompatible msi_domain, giving up\n"); 158 - return ERR_PTR(-EINVAL); 219 + return -EINVAL; 159 220 } 160 221 161 - /* Already had a helping of MSI? Greed... */ 162 - if (!list_empty(dev_to_msi_list(dev))) 163 - return ERR_PTR(-EBUSY); 222 + err = msi_setup_device_data(dev); 223 + if (err) 224 + return err; 225 + 226 + /* Already initialized? */ 227 + if (dev->msi.data->platform_data) 228 + return -EBUSY; 164 229 165 230 datap = kzalloc(sizeof(*datap), GFP_KERNEL); 166 231 if (!datap) 167 - return ERR_PTR(-ENOMEM); 232 + return -ENOMEM; 168 233 169 234 datap->devid = ida_simple_get(&platform_msi_devid_ida, 170 235 0, 1 << DEV_ID_SHIFT, GFP_KERNEL); 171 236 if (datap->devid < 0) { 172 - int err = datap->devid; 237 + err = datap->devid; 173 238 kfree(datap); 174 - return ERR_PTR(err); 239 + return err; 175 240 } 176 241 177 242 datap->write_msg = write_msi_msg; 178 243 datap->dev = dev; 179 - 180 - return datap; 244 + dev->msi.data->platform_data = datap; 245 + return 0; 181 246 } 182 247 183 - static void platform_msi_free_priv_data(struct platform_msi_priv_data *data) 248 + static void platform_msi_free_priv_data(struct device *dev) 184 249 { 250 + struct platform_msi_priv_data *data = dev->msi.data->platform_data; 251 + 252 + dev->msi.data->platform_data = NULL; 185 253 ida_simple_remove(&platform_msi_devid_ida, data->devid); 186 254 kfree(data); 187 255 } ··· 206 258 int platform_msi_domain_alloc_irqs(struct device *dev, unsigned int nvec, 207 259 irq_write_msi_msg_t write_msi_msg) 208 260 { 209 - struct platform_msi_priv_data *priv_data; 210 261 int err; 211 262 212 - priv_data = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg); 213 - if (IS_ERR(priv_data)) 214 - return PTR_ERR(priv_data); 215 - 216 - err = platform_msi_alloc_descs(dev, nvec, priv_data); 263 + err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg); 217 264 if (err) 218 - goto out_free_priv_data; 265 + return err; 219 266 220 - err = msi_domain_alloc_irqs(dev->msi_domain, dev, nvec); 267 + err = msi_domain_alloc_irqs(dev->msi.domain, dev, nvec); 221 268 if (err) 222 - goto out_free_desc; 223 - 224 - priv_data->msi_irq_groups = msi_populate_sysfs(dev); 225 - if (IS_ERR(priv_data->msi_irq_groups)) { 226 - err = PTR_ERR(priv_data->msi_irq_groups); 227 - goto out_free_irqs; 228 - } 229 - 230 - return 0; 231 - 232 - out_free_irqs: 233 - msi_domain_free_irqs(dev->msi_domain, dev); 234 - out_free_desc: 235 - platform_msi_free_descs(dev, 0, nvec); 236 - out_free_priv_data: 237 - platform_msi_free_priv_data(priv_data); 269 + platform_msi_free_priv_data(dev); 238 270 239 271 return err; 240 272 } ··· 226 298 */ 227 299 void platform_msi_domain_free_irqs(struct device *dev) 228 300 { 229 - if (!list_empty(dev_to_msi_list(dev))) { 230 - struct msi_desc *desc; 231 - 232 - desc = first_msi_entry(dev); 233 - msi_destroy_sysfs(dev, desc->platform.msi_priv_data->msi_irq_groups); 234 - platform_msi_free_priv_data(desc->platform.msi_priv_data); 235 - } 236 - 237 - msi_domain_free_irqs(dev->msi_domain, dev); 238 - platform_msi_free_descs(dev, 0, MAX_DEV_MSIS); 301 + msi_domain_free_irqs(dev->msi.domain, dev); 302 + platform_msi_free_priv_data(dev); 239 303 } 240 304 EXPORT_SYMBOL_GPL(platform_msi_domain_free_irqs); 241 305 ··· 236 316 * a platform-msi domain 237 317 * @domain: The platform-msi domain 238 318 * 239 - * Returns the private data provided when calling 240 - * platform_msi_create_device_domain. 319 + * Return: The private data provided when calling 320 + * platform_msi_create_device_domain(). 241 321 */ 242 322 void *platform_msi_get_host_data(struct irq_domain *domain) 243 323 { 244 324 struct platform_msi_priv_data *data = domain->host_data; 325 + 245 326 return data->host_data; 246 327 } 247 328 329 + static struct lock_class_key platform_device_msi_lock_class; 330 + 248 331 /** 249 - * __platform_msi_create_device_domain - Create a platform-msi domain 332 + * __platform_msi_create_device_domain - Create a platform-msi device domain 250 333 * 251 334 * @dev: The device generating the MSIs 252 335 * @nvec: The number of MSIs that need to be allocated ··· 258 335 * @ops: The hierarchy domain operations to use 259 336 * @host_data: Private data associated to this domain 260 337 * 261 - * Returns an irqdomain for @nvec interrupts 338 + * Return: An irqdomain for @nvec interrupts on success, NULL in case of error. 339 + * 340 + * This is for interrupt domains which stack on a platform-msi domain 341 + * created by platform_msi_create_irq_domain(). @dev->msi.domain points to 342 + * that platform-msi domain which is the parent for the new domain. 262 343 */ 263 344 struct irq_domain * 264 345 __platform_msi_create_device_domain(struct device *dev, ··· 276 349 struct irq_domain *domain; 277 350 int err; 278 351 279 - data = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg); 280 - if (IS_ERR(data)) 352 + err = platform_msi_alloc_priv_data(dev, nvec, write_msi_msg); 353 + if (err) 281 354 return NULL; 282 355 356 + /* 357 + * Use a separate lock class for the MSI descriptor mutex on 358 + * platform MSI device domains because the descriptor mutex nests 359 + * into the domain mutex. See alloc/free below. 360 + */ 361 + lockdep_set_class(&dev->msi.data->mutex, &platform_device_msi_lock_class); 362 + 363 + data = dev->msi.data->platform_data; 283 364 data->host_data = host_data; 284 - domain = irq_domain_create_hierarchy(dev->msi_domain, 0, 365 + domain = irq_domain_create_hierarchy(dev->msi.domain, 0, 285 366 is_tree ? 0 : nvec, 286 367 dev->fwnode, ops, data); 287 368 if (!domain) ··· 305 370 free_domain: 306 371 irq_domain_remove(domain); 307 372 free_priv: 308 - platform_msi_free_priv_data(data); 373 + platform_msi_free_priv_data(dev); 309 374 return NULL; 310 375 } 311 376 312 377 /** 313 - * platform_msi_domain_free - Free interrupts associated with a platform-msi 314 - * domain 378 + * platform_msi_device_domain_free - Free interrupts associated with a platform-msi 379 + * device domain 315 380 * 316 - * @domain: The platform-msi domain 381 + * @domain: The platform-msi device domain 317 382 * @virq: The base irq from which to perform the free operation 318 - * @nvec: How many interrupts to free from @virq 383 + * @nr_irqs: How many interrupts to free from @virq 319 384 */ 320 - void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq, 321 - unsigned int nvec) 385 + void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq, 386 + unsigned int nr_irqs) 322 387 { 323 388 struct platform_msi_priv_data *data = domain->host_data; 324 - struct msi_desc *desc, *tmp; 325 - for_each_msi_entry_safe(desc, tmp, data->dev) { 326 - if (WARN_ON(!desc->irq || desc->nvec_used != 1)) 327 - return; 328 - if (!(desc->irq >= virq && desc->irq < (virq + nvec))) 329 - continue; 330 389 331 - irq_domain_free_irqs_common(domain, desc->irq, 1); 332 - list_del(&desc->list); 333 - free_msi_entry(desc); 334 - } 390 + msi_lock_descs(data->dev); 391 + irq_domain_free_irqs_common(domain, virq, nr_irqs); 392 + msi_free_msi_descs_range(data->dev, MSI_DESC_ALL, virq, virq + nr_irqs - 1); 393 + msi_unlock_descs(data->dev); 335 394 } 336 395 337 396 /** 338 - * platform_msi_domain_alloc - Allocate interrupts associated with 339 - * a platform-msi domain 397 + * platform_msi_device_domain_alloc - Allocate interrupts associated with 398 + * a platform-msi device domain 340 399 * 341 - * @domain: The platform-msi domain 400 + * @domain: The platform-msi device domain 342 401 * @virq: The base irq from which to perform the allocate operation 343 - * @nr_irqs: How many interrupts to free from @virq 402 + * @nr_irqs: How many interrupts to allocate from @virq 344 403 * 345 404 * Return 0 on success, or an error code on failure. Must be called 346 405 * with irq_domain_mutex held (which can only be done as part of a 347 406 * top-level interrupt allocation). 348 407 */ 349 - int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq, 350 - unsigned int nr_irqs) 408 + int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq, 409 + unsigned int nr_irqs) 351 410 { 352 411 struct platform_msi_priv_data *data = domain->host_data; 353 - int err; 412 + struct device *dev = data->dev; 354 413 355 - err = platform_msi_alloc_descs_with_irq(data->dev, virq, nr_irqs, data); 356 - if (err) 357 - return err; 358 - 359 - err = msi_domain_populate_irqs(domain->parent, data->dev, 360 - virq, nr_irqs, &data->arg); 361 - if (err) 362 - platform_msi_domain_free(domain, virq, nr_irqs); 363 - 364 - return err; 414 + return msi_domain_populate_irqs(domain->parent, dev, virq, nr_irqs, &data->arg); 365 415 }
+4 -4
drivers/bus/fsl-mc/dprc-driver.c
··· 400 400 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 401 401 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev); 402 402 struct fsl_mc_io *mc_io = mc_dev->mc_io; 403 - struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc; 403 + int irq = mc_dev->irqs[0]->virq; 404 404 405 405 dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n", 406 406 irq_num, smp_processor_id()); ··· 409 409 return IRQ_HANDLED; 410 410 411 411 mutex_lock(&mc_bus->scan_mutex); 412 - if (!msi_desc || msi_desc->irq != (u32)irq_num) 412 + if (irq != (u32)irq_num) 413 413 goto out; 414 414 415 415 status = 0; ··· 521 521 * function that programs the MSI physically in the device 522 522 */ 523 523 error = devm_request_threaded_irq(&mc_dev->dev, 524 - irq->msi_desc->irq, 524 + irq->virq, 525 525 dprc_irq0_handler, 526 526 dprc_irq0_handler_thread, 527 527 IRQF_NO_SUSPEND | IRQF_ONESHOT, ··· 771 771 772 772 (void)disable_dprc_irq(mc_dev); 773 773 774 - devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev); 774 + devm_free_irq(&mc_dev->dev, irq->virq, &mc_dev->dev); 775 775 776 776 fsl_mc_free_irqs(mc_dev); 777 777 }
+2 -7
drivers/bus/fsl-mc/fsl-mc-allocator.c
··· 350 350 unsigned int irq_count) 351 351 { 352 352 unsigned int i; 353 - struct msi_desc *msi_desc; 354 353 struct fsl_mc_device_irq *irq_resources; 355 354 struct fsl_mc_device_irq *mc_dev_irq; 356 355 int error; ··· 387 388 mc_dev_irq->resource.type = res_pool->type; 388 389 mc_dev_irq->resource.data = mc_dev_irq; 389 390 mc_dev_irq->resource.parent_pool = res_pool; 391 + mc_dev_irq->virq = msi_get_virq(&mc_bus_dev->dev, i); 392 + mc_dev_irq->resource.id = mc_dev_irq->virq; 390 393 INIT_LIST_HEAD(&mc_dev_irq->resource.node); 391 394 list_add_tail(&mc_dev_irq->resource.node, &res_pool->free_list); 392 - } 393 - 394 - for_each_msi_entry(msi_desc, &mc_bus_dev->dev) { 395 - mc_dev_irq = &irq_resources[msi_desc->fsl_mc.msi_index]; 396 - mc_dev_irq->msi_desc = msi_desc; 397 - mc_dev_irq->resource.id = msi_desc->irq; 398 395 } 399 396 400 397 res_pool->max_count = irq_count;
+14 -65
drivers/bus/fsl-mc/fsl-mc-msi.c
··· 29 29 * Make the base hwirq value for ICID*10000 so it is readable 30 30 * as a decimal value in /proc/interrupts. 31 31 */ 32 - return (irq_hw_number_t)(desc->fsl_mc.msi_index + (dev->icid * 10000)); 32 + return (irq_hw_number_t)(desc->msi_index + (dev->icid * 10000)); 33 33 } 34 34 35 35 static void fsl_mc_msi_set_desc(msi_alloc_info_t *arg, ··· 58 58 } 59 59 60 60 static void __fsl_mc_msi_write_msg(struct fsl_mc_device *mc_bus_dev, 61 - struct fsl_mc_device_irq *mc_dev_irq) 61 + struct fsl_mc_device_irq *mc_dev_irq, 62 + struct msi_desc *msi_desc) 62 63 { 63 64 int error; 64 65 struct fsl_mc_device *owner_mc_dev = mc_dev_irq->mc_dev; 65 - struct msi_desc *msi_desc = mc_dev_irq->msi_desc; 66 66 struct dprc_irq_cfg irq_cfg; 67 67 68 68 /* ··· 122 122 struct fsl_mc_device *mc_bus_dev = to_fsl_mc_device(msi_desc->dev); 123 123 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev); 124 124 struct fsl_mc_device_irq *mc_dev_irq = 125 - &mc_bus->irq_resources[msi_desc->fsl_mc.msi_index]; 125 + &mc_bus->irq_resources[msi_desc->msi_index]; 126 126 127 127 msi_desc->msg = *msg; 128 128 129 129 /* 130 130 * Program the MSI (paddr, value) pair in the device: 131 131 */ 132 - __fsl_mc_msi_write_msg(mc_bus_dev, mc_dev_irq); 132 + __fsl_mc_msi_write_msg(mc_bus_dev, mc_dev_irq, msi_desc); 133 133 } 134 134 135 135 static void fsl_mc_msi_update_chip_ops(struct msi_domain_info *info) ··· 170 170 fsl_mc_msi_update_dom_ops(info); 171 171 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 172 172 fsl_mc_msi_update_chip_ops(info); 173 + info->flags |= MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS | MSI_FLAG_FREE_MSI_DESCS; 173 174 174 175 domain = msi_create_irq_domain(fwnode, info, parent); 175 176 if (domain) ··· 211 210 return msi_domain; 212 211 } 213 212 214 - static void fsl_mc_msi_free_descs(struct device *dev) 215 - { 216 - struct msi_desc *desc, *tmp; 217 - 218 - list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) { 219 - list_del(&desc->list); 220 - free_msi_entry(desc); 221 - } 222 - } 223 - 224 - static int fsl_mc_msi_alloc_descs(struct device *dev, unsigned int irq_count) 225 - 226 - { 227 - unsigned int i; 228 - int error; 229 - struct msi_desc *msi_desc; 230 - 231 - for (i = 0; i < irq_count; i++) { 232 - msi_desc = alloc_msi_entry(dev, 1, NULL); 233 - if (!msi_desc) { 234 - dev_err(dev, "Failed to allocate msi entry\n"); 235 - error = -ENOMEM; 236 - goto cleanup_msi_descs; 237 - } 238 - 239 - msi_desc->fsl_mc.msi_index = i; 240 - INIT_LIST_HEAD(&msi_desc->list); 241 - list_add_tail(&msi_desc->list, dev_to_msi_list(dev)); 242 - } 243 - 244 - return 0; 245 - 246 - cleanup_msi_descs: 247 - fsl_mc_msi_free_descs(dev); 248 - return error; 249 - } 250 - 251 - int fsl_mc_msi_domain_alloc_irqs(struct device *dev, 252 - unsigned int irq_count) 213 + int fsl_mc_msi_domain_alloc_irqs(struct device *dev, unsigned int irq_count) 253 214 { 254 215 struct irq_domain *msi_domain; 255 216 int error; 256 217 257 - if (!list_empty(dev_to_msi_list(dev))) 218 + msi_domain = dev_get_msi_domain(dev); 219 + if (!msi_domain) 258 220 return -EINVAL; 259 221 260 - error = fsl_mc_msi_alloc_descs(dev, irq_count); 261 - if (error < 0) 222 + error = msi_setup_device_data(dev); 223 + if (error) 262 224 return error; 263 225 264 - msi_domain = dev_get_msi_domain(dev); 265 - if (!msi_domain) { 266 - error = -EINVAL; 267 - goto cleanup_msi_descs; 268 - } 226 + if (msi_first_desc(dev, MSI_DESC_ALL)) 227 + return -EINVAL; 269 228 270 229 /* 271 230 * NOTE: Calling this function will trigger the invocation of the ··· 233 272 */ 234 273 error = msi_domain_alloc_irqs(msi_domain, dev, irq_count); 235 274 236 - if (error) { 275 + if (error) 237 276 dev_err(dev, "Failed to allocate IRQs\n"); 238 - goto cleanup_msi_descs; 239 - } 240 - 241 - return 0; 242 - 243 - cleanup_msi_descs: 244 - fsl_mc_msi_free_descs(dev); 245 277 return error; 246 278 } 247 279 ··· 247 293 return; 248 294 249 295 msi_domain_free_irqs(msi_domain, dev); 250 - 251 - if (list_empty(dev_to_msi_list(dev))) 252 - return; 253 - 254 - fsl_mc_msi_free_descs(dev); 255 296 }
+5 -11
drivers/dma/mv_xor_v2.c
··· 149 149 * @desc_size: HW descriptor size 150 150 * @npendings: number of pending descriptors (for which tx_submit has 151 151 * @hw_queue_idx: HW queue index 152 - * @msi_desc: local interrupt descriptor information 152 + * @irq: The Linux interrupt number 153 153 * been called, but not yet issue_pending) 154 154 */ 155 155 struct mv_xor_v2_device { ··· 168 168 int desc_size; 169 169 unsigned int npendings; 170 170 unsigned int hw_queue_idx; 171 - struct msi_desc *msi_desc; 171 + unsigned int irq; 172 172 }; 173 173 174 174 /** ··· 718 718 int i, ret = 0; 719 719 struct dma_device *dma_dev; 720 720 struct mv_xor_v2_sw_desc *sw_desc; 721 - struct msi_desc *msi_desc; 722 721 723 722 BUILD_BUG_ON(sizeof(struct mv_xor_v2_descriptor) != 724 723 MV_XOR_V2_EXT_DESC_SIZE); ··· 769 770 if (ret) 770 771 goto disable_clk; 771 772 772 - msi_desc = first_msi_entry(&pdev->dev); 773 - if (!msi_desc) { 774 - ret = -ENODEV; 775 - goto free_msi_irqs; 776 - } 777 - xor_dev->msi_desc = msi_desc; 773 + xor_dev->irq = msi_get_virq(&pdev->dev, 0); 778 774 779 - ret = devm_request_irq(&pdev->dev, msi_desc->irq, 775 + ret = devm_request_irq(&pdev->dev, xor_dev->irq, 780 776 mv_xor_v2_interrupt_handler, 0, 781 777 dev_name(&pdev->dev), xor_dev); 782 778 if (ret) ··· 886 892 xor_dev->desc_size * MV_XOR_V2_DESC_NUM, 887 893 xor_dev->hw_desq_virt, xor_dev->hw_desq); 888 894 889 - devm_free_irq(&pdev->dev, xor_dev->msi_desc->irq, xor_dev); 895 + devm_free_irq(&pdev->dev, xor_dev->irq, xor_dev); 890 896 891 897 platform_msi_domain_free_irqs(&pdev->dev); 892 898
+19 -25
drivers/dma/qcom/hidma.c
··· 666 666 struct device *dev = msi_desc_to_dev(desc); 667 667 struct hidma_dev *dmadev = dev_get_drvdata(dev); 668 668 669 - if (!desc->platform.msi_index) { 669 + if (!desc->msi_index) { 670 670 writel(msg->address_lo, dmadev->dev_evca + 0x118); 671 671 writel(msg->address_hi, dmadev->dev_evca + 0x11C); 672 672 writel(msg->data, dmadev->dev_evca + 0x120); ··· 678 678 { 679 679 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 680 680 struct device *dev = dmadev->ddev.dev; 681 - struct msi_desc *desc; 681 + int i, virq; 682 682 683 - /* free allocated MSI interrupts above */ 684 - for_each_msi_entry(desc, dev) 685 - devm_free_irq(dev, desc->irq, &dmadev->lldev); 683 + for (i = 0; i < HIDMA_MSI_INTS; i++) { 684 + virq = msi_get_virq(dev, i); 685 + if (virq) 686 + devm_free_irq(dev, virq, &dmadev->lldev); 687 + } 686 688 687 689 platform_msi_domain_free_irqs(dev); 688 690 #endif ··· 694 692 struct platform_device *pdev) 695 693 { 696 694 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 697 - int rc; 698 - struct msi_desc *desc; 699 - struct msi_desc *failed_desc = NULL; 695 + int rc, i, virq; 700 696 701 697 rc = platform_msi_domain_alloc_irqs(&pdev->dev, HIDMA_MSI_INTS, 702 698 hidma_write_msi_msg); 703 699 if (rc) 704 700 return rc; 705 701 706 - for_each_msi_entry(desc, &pdev->dev) { 707 - if (!desc->platform.msi_index) 708 - dmadev->msi_virqbase = desc->irq; 709 - 710 - rc = devm_request_irq(&pdev->dev, desc->irq, 702 + for (i = 0; i < HIDMA_MSI_INTS; i++) { 703 + virq = msi_get_virq(&pdev->dev, i); 704 + rc = devm_request_irq(&pdev->dev, virq, 711 705 hidma_chirq_handler_msi, 712 706 0, "qcom-hidma-msi", 713 707 &dmadev->lldev); 714 - if (rc) { 715 - failed_desc = desc; 708 + if (rc) 716 709 break; 717 - } 710 + if (!i) 711 + dmadev->msi_virqbase = virq; 718 712 } 719 713 720 714 if (rc) { 721 715 /* free allocated MSI interrupts above */ 722 - for_each_msi_entry(desc, &pdev->dev) { 723 - if (desc == failed_desc) 724 - break; 725 - devm_free_irq(&pdev->dev, desc->irq, 726 - &dmadev->lldev); 716 + for (--i; i >= 0; i--) { 717 + virq = msi_get_virq(&pdev->dev, i); 718 + devm_free_irq(&pdev->dev, virq, &dmadev->lldev); 727 719 } 720 + dev_warn(&pdev->dev, 721 + "failed to request MSI irq, falling back to wired IRQ\n"); 728 722 } else { 729 723 /* Add callback to free MSIs on teardown */ 730 724 hidma_ll_setup_irq(dmadev->lldev, true); 731 - 732 725 } 733 - if (rc) 734 - dev_warn(&pdev->dev, 735 - "failed to request MSI irq, falling back to wired IRQ\n"); 736 726 return rc; 737 727 #else 738 728 return -EINVAL;
+2 -4
drivers/dma/ti/k3-udma-private.c
··· 168 168 { 169 169 const struct udma_oes_offsets *oes = &ud->soc_data->oes; 170 170 171 - return ti_sci_inta_msi_get_virq(ud->dev, udma_tflow_id + 172 - oes->pktdma_tchan_flow); 171 + return msi_get_virq(ud->dev, udma_tflow_id + oes->pktdma_tchan_flow); 173 172 } 174 173 EXPORT_SYMBOL(xudma_pktdma_tflow_get_irq); 175 174 ··· 176 177 { 177 178 const struct udma_oes_offsets *oes = &ud->soc_data->oes; 178 179 179 - return ti_sci_inta_msi_get_virq(ud->dev, udma_rflow_id + 180 - oes->pktdma_rchan_flow); 180 + return msi_get_virq(ud->dev, udma_rflow_id + oes->pktdma_rchan_flow); 181 181 } 182 182 EXPORT_SYMBOL(xudma_pktdma_rflow_get_irq);
+6 -8
drivers/dma/ti/k3-udma.c
··· 2313 2313 2314 2314 /* Event from UDMA (TR events) only needed for slave TR mode channels */ 2315 2315 if (is_slave_direction(uc->config.dir) && !uc->config.pkt_mode) { 2316 - uc->irq_num_udma = ti_sci_inta_msi_get_virq(ud->dev, 2317 - irq_udma_idx); 2316 + uc->irq_num_udma = msi_get_virq(ud->dev, irq_udma_idx); 2318 2317 if (uc->irq_num_udma <= 0) { 2319 2318 dev_err(ud->dev, "Failed to get udma irq (index: %u)\n", 2320 2319 irq_udma_idx); ··· 2485 2486 uc->psil_paired = true; 2486 2487 } 2487 2488 2488 - uc->irq_num_ring = ti_sci_inta_msi_get_virq(ud->dev, irq_ring_idx); 2489 + uc->irq_num_ring = msi_get_virq(ud->dev, irq_ring_idx); 2489 2490 if (uc->irq_num_ring <= 0) { 2490 2491 dev_err(ud->dev, "Failed to get ring irq (index: %u)\n", 2491 2492 irq_ring_idx); ··· 2502 2503 2503 2504 /* Event from BCDMA (TR events) only needed for slave channels */ 2504 2505 if (is_slave_direction(uc->config.dir)) { 2505 - uc->irq_num_udma = ti_sci_inta_msi_get_virq(ud->dev, 2506 - irq_udma_idx); 2506 + uc->irq_num_udma = msi_get_virq(ud->dev, irq_udma_idx); 2507 2507 if (uc->irq_num_udma <= 0) { 2508 2508 dev_err(ud->dev, "Failed to get bcdma irq (index: %u)\n", 2509 2509 irq_udma_idx); ··· 2670 2672 2671 2673 uc->psil_paired = true; 2672 2674 2673 - uc->irq_num_ring = ti_sci_inta_msi_get_virq(ud->dev, irq_ring_idx); 2675 + uc->irq_num_ring = msi_get_virq(ud->dev, irq_ring_idx); 2674 2676 if (uc->irq_num_ring <= 0) { 2675 2677 dev_err(ud->dev, "Failed to get ring irq (index: %u)\n", 2676 2678 irq_ring_idx); ··· 5334 5336 if (IS_ERR(ud->ringacc)) 5335 5337 return PTR_ERR(ud->ringacc); 5336 5338 5337 - dev->msi_domain = of_msi_get_domain(dev, dev->of_node, 5339 + dev->msi.domain = of_msi_get_domain(dev, dev->of_node, 5338 5340 DOMAIN_BUS_TI_SCI_INTA_MSI); 5339 - if (!dev->msi_domain) { 5341 + if (!dev->msi.domain) { 5340 5342 dev_err(dev, "Failed to get MSI domain\n"); 5341 5343 return -EPROBE_DEFER; 5342 5344 }
+5 -18
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
··· 3142 3142 phys_addr_t doorbell; 3143 3143 struct device *dev = msi_desc_to_dev(desc); 3144 3144 struct arm_smmu_device *smmu = dev_get_drvdata(dev); 3145 - phys_addr_t *cfg = arm_smmu_msi_cfg[desc->platform.msi_index]; 3145 + phys_addr_t *cfg = arm_smmu_msi_cfg[desc->msi_index]; 3146 3146 3147 3147 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo; 3148 3148 doorbell &= MSI_CFG0_ADDR_MASK; ··· 3154 3154 3155 3155 static void arm_smmu_setup_msis(struct arm_smmu_device *smmu) 3156 3156 { 3157 - struct msi_desc *desc; 3158 3157 int ret, nvec = ARM_SMMU_MAX_MSIS; 3159 3158 struct device *dev = smmu->dev; 3160 3159 ··· 3169 3170 if (!(smmu->features & ARM_SMMU_FEAT_MSI)) 3170 3171 return; 3171 3172 3172 - if (!dev->msi_domain) { 3173 + if (!dev->msi.domain) { 3173 3174 dev_info(smmu->dev, "msi_domain absent - falling back to wired irqs\n"); 3174 3175 return; 3175 3176 } ··· 3181 3182 return; 3182 3183 } 3183 3184 3184 - for_each_msi_entry(desc, dev) { 3185 - switch (desc->platform.msi_index) { 3186 - case EVTQ_MSI_INDEX: 3187 - smmu->evtq.q.irq = desc->irq; 3188 - break; 3189 - case GERROR_MSI_INDEX: 3190 - smmu->gerr_irq = desc->irq; 3191 - break; 3192 - case PRIQ_MSI_INDEX: 3193 - smmu->priq.q.irq = desc->irq; 3194 - break; 3195 - default: /* Unknown */ 3196 - continue; 3197 - } 3198 - } 3185 + smmu->evtq.q.irq = msi_get_virq(dev, EVTQ_MSI_INDEX); 3186 + smmu->gerr_irq = msi_get_virq(dev, GERROR_MSI_INDEX); 3187 + smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX); 3199 3188 3200 3189 /* Add callback to free MSIs on teardown */ 3201 3190 devm_add_action(dev, arm_smmu_free_msis, dev);
-1
drivers/irqchip/irq-gic-v2m.c
··· 88 88 .irq_mask = gicv2m_mask_msi_irq, 89 89 .irq_unmask = gicv2m_unmask_msi_irq, 90 90 .irq_eoi = irq_chip_eoi_parent, 91 - .irq_write_msi_msg = pci_msi_domain_write_msg, 92 91 }; 93 92 94 93 static struct msi_domain_info gicv2m_msi_domain_info = {
-1
drivers/irqchip/irq-gic-v3-its-pci-msi.c
··· 28 28 .irq_unmask = its_unmask_msi_irq, 29 29 .irq_mask = its_mask_msi_irq, 30 30 .irq_eoi = irq_chip_eoi_parent, 31 - .irq_write_msi_msg = pci_msi_domain_write_msg, 32 31 }; 33 32 34 33 static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
-1
drivers/irqchip/irq-gic-v3-mbi.c
··· 171 171 .irq_unmask = mbi_unmask_msi_irq, 172 172 .irq_eoi = irq_chip_eoi_parent, 173 173 .irq_compose_msi_msg = mbi_compose_msi_msg, 174 - .irq_write_msi_msg = pci_msi_domain_write_msg, 175 174 }; 176 175 177 176 static struct msi_domain_info mbi_msi_domain_info = {
+2 -2
drivers/irqchip/irq-mbigen.c
··· 207 207 if (err) 208 208 return err; 209 209 210 - err = platform_msi_domain_alloc(domain, virq, nr_irqs); 210 + err = platform_msi_device_domain_alloc(domain, virq, nr_irqs); 211 211 if (err) 212 212 return err; 213 213 ··· 223 223 static void mbigen_irq_domain_free(struct irq_domain *domain, unsigned int virq, 224 224 unsigned int nr_irqs) 225 225 { 226 - platform_msi_domain_free(domain, virq, nr_irqs); 226 + platform_msi_device_domain_free(domain, virq, nr_irqs); 227 227 } 228 228 229 229 static const struct irq_domain_ops mbigen_domain_ops = {
+6 -6
drivers/irqchip/irq-mvebu-icu.c
··· 221 221 icu_irqd->icu_group = msi_data->subset_data->icu_group; 222 222 icu_irqd->icu = icu; 223 223 224 - err = platform_msi_domain_alloc(domain, virq, nr_irqs); 224 + err = platform_msi_device_domain_alloc(domain, virq, nr_irqs); 225 225 if (err) { 226 226 dev_err(icu->dev, "failed to allocate ICU interrupt in parent domain\n"); 227 227 goto free_irqd; ··· 245 245 return 0; 246 246 247 247 free_msi: 248 - platform_msi_domain_free(domain, virq, nr_irqs); 248 + platform_msi_device_domain_free(domain, virq, nr_irqs); 249 249 free_irqd: 250 250 kfree(icu_irqd); 251 251 return err; ··· 260 260 261 261 kfree(icu_irqd); 262 262 263 - platform_msi_domain_free(domain, virq, nr_irqs); 263 + platform_msi_device_domain_free(domain, virq, nr_irqs); 264 264 } 265 265 266 266 static const struct irq_domain_ops mvebu_icu_domain_ops = { ··· 314 314 msi_data->subset_data = of_device_get_match_data(dev); 315 315 } 316 316 317 - dev->msi_domain = of_msi_get_domain(dev, dev->of_node, 317 + dev->msi.domain = of_msi_get_domain(dev, dev->of_node, 318 318 DOMAIN_BUS_PLATFORM_MSI); 319 - if (!dev->msi_domain) 319 + if (!dev->msi.domain) 320 320 return -EPROBE_DEFER; 321 321 322 - msi_parent_dn = irq_domain_get_of_node(dev->msi_domain); 322 + msi_parent_dn = irq_domain_get_of_node(dev->msi.domain); 323 323 if (!msi_parent_dn) 324 324 return -ENODEV; 325 325
+1 -1
drivers/irqchip/irq-ti-sci-inta.c
··· 595 595 struct platform_device *pdev = to_platform_device(desc->dev); 596 596 597 597 arg->desc = desc; 598 - arg->hwirq = TO_HWIRQ(pdev->id, desc->inta.dev_index); 598 + arg->hwirq = TO_HWIRQ(pdev->id, desc->msi_index); 599 599 } 600 600 601 601 static struct msi_domain_ops ti_sci_inta_msi_ops = {
+3 -6
drivers/mailbox/bcm-flexrm-mailbox.c
··· 1484 1484 { 1485 1485 struct device *dev = msi_desc_to_dev(desc); 1486 1486 struct flexrm_mbox *mbox = dev_get_drvdata(dev); 1487 - struct flexrm_ring *ring = &mbox->rings[desc->platform.msi_index]; 1487 + struct flexrm_ring *ring = &mbox->rings[desc->msi_index]; 1488 1488 1489 1489 /* Configure per-Ring MSI registers */ 1490 1490 writel_relaxed(msg->address_lo, ring->regs + RING_MSI_ADDR_LS); ··· 1497 1497 int index, ret = 0; 1498 1498 void __iomem *regs; 1499 1499 void __iomem *regs_end; 1500 - struct msi_desc *desc; 1501 1500 struct resource *iomem; 1502 1501 struct flexrm_ring *ring; 1503 1502 struct flexrm_mbox *mbox; ··· 1607 1608 goto fail_destroy_cmpl_pool; 1608 1609 1609 1610 /* Save alloced IRQ numbers for each ring */ 1610 - for_each_msi_entry(desc, dev) { 1611 - ring = &mbox->rings[desc->platform.msi_index]; 1612 - ring->irq = desc->irq; 1613 - } 1611 + for (index = 0; index < mbox->num_rings; index++) 1612 + mbox->rings[index].irq = msi_get_virq(dev, index); 1614 1613 1615 1614 /* Check availability of debugfs */ 1616 1615 if (!debugfs_initialized())
+2 -2
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
··· 4246 4246 } 4247 4247 4248 4248 irq = ls_dev->irqs[0]; 4249 - err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq, 4249 + err = devm_request_threaded_irq(&ls_dev->dev, irq->virq, 4250 4250 NULL, dpni_irq0_handler_thread, 4251 4251 IRQF_NO_SUSPEND | IRQF_ONESHOT, 4252 4252 dev_name(&ls_dev->dev), &ls_dev->dev); ··· 4273 4273 return 0; 4274 4274 4275 4275 free_irq: 4276 - devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev); 4276 + devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev); 4277 4277 free_mc_irq: 4278 4278 fsl_mc_free_irqs(ls_dev); 4279 4279
+1 -3
drivers/net/ethernet/freescale/dpaa2/dpaa2-ptp.c
··· 129 129 static int dpaa2_ptp_probe(struct fsl_mc_device *mc_dev) 130 130 { 131 131 struct device *dev = &mc_dev->dev; 132 - struct fsl_mc_device_irq *irq; 133 132 struct ptp_qoriq *ptp_qoriq; 134 133 struct device_node *node; 135 134 void __iomem *base; ··· 176 177 goto err_unmap; 177 178 } 178 179 179 - irq = mc_dev->irqs[0]; 180 - ptp_qoriq->irq = irq->msi_desc->irq; 180 + ptp_qoriq->irq = mc_dev->irqs[0]->virq; 181 181 182 182 err = request_threaded_irq(ptp_qoriq->irq, NULL, 183 183 dpaa2_ptp_irq_handler_thread,
+2 -3
drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.c
··· 1554 1554 1555 1555 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF]; 1556 1556 1557 - err = devm_request_threaded_irq(dev, irq->msi_desc->irq, 1558 - NULL, 1557 + err = devm_request_threaded_irq(dev, irq->virq, NULL, 1559 1558 dpaa2_switch_irq0_handler_thread, 1560 1559 IRQF_NO_SUSPEND | IRQF_ONESHOT, 1561 1560 dev_name(dev), dev); ··· 1580 1581 return 0; 1581 1582 1582 1583 free_devm_irq: 1583 - devm_free_irq(dev, irq->msi_desc->irq, dev); 1584 + devm_free_irq(dev, irq->virq, dev); 1584 1585 free_irq: 1585 1586 fsl_mc_free_irqs(sw_dev); 1586 1587 return err;
+1 -1
drivers/net/wireless/ath/ath11k/pci.c
··· 1014 1014 } 1015 1015 1016 1016 ab_pci->msi_ep_base_data = msi_desc->msg.data; 1017 - if (msi_desc->msi_attrib.is_64) 1017 + if (msi_desc->pci.msi_attrib.is_64) 1018 1018 set_bit(ATH11K_PCI_FLAG_IS_MSI_64, &ab_pci->flags); 1019 1019 1020 1020 ath11k_dbg(ab, ATH11K_DBG_PCI, "msi base data is %d\n", ab_pci->msi_ep_base_data);
+13 -6
drivers/ntb/msi.c
··· 108 108 if (!ntb->msi) 109 109 return -EINVAL; 110 110 111 - desc = first_msi_entry(&ntb->pdev->dev); 111 + msi_lock_descs(&ntb->pdev->dev); 112 + desc = msi_first_desc(&ntb->pdev->dev, MSI_DESC_ASSOCIATED); 112 113 addr = desc->msg.address_lo + ((uint64_t)desc->msg.address_hi << 32); 114 + msi_unlock_descs(&ntb->pdev->dev); 113 115 114 116 for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) { 115 117 peer_widx = ntb_peer_highest_mw_idx(ntb, peer); ··· 283 281 const char *name, void *dev_id, 284 282 struct ntb_msi_desc *msi_desc) 285 283 { 284 + struct device *dev = &ntb->pdev->dev; 286 285 struct msi_desc *entry; 287 286 int ret; 288 287 289 288 if (!ntb->msi) 290 289 return -EINVAL; 291 290 292 - for_each_pci_msi_entry(entry, ntb->pdev) { 291 + msi_lock_descs(dev); 292 + msi_for_each_desc(entry, dev, MSI_DESC_ASSOCIATED) { 293 293 if (irq_has_action(entry->irq)) 294 294 continue; 295 295 ··· 308 304 ret = ntbm_msi_setup_callback(ntb, entry, msi_desc); 309 305 if (ret) { 310 306 devm_free_irq(&ntb->dev, entry->irq, dev_id); 311 - return ret; 307 + goto unlock; 312 308 } 313 309 314 - 315 - return entry->irq; 310 + ret = entry->irq; 311 + goto unlock; 316 312 } 313 + ret = -ENODEV; 317 314 318 - return -ENODEV; 315 + unlock: 316 + msi_unlock_descs(dev); 317 + return ret; 319 318 } 320 319 EXPORT_SYMBOL(ntbm_msi_request_threaded_irq); 321 320
+2 -1
drivers/pci/Makefile
··· 5 5 obj-$(CONFIG_PCI) += access.o bus.o probe.o host-bridge.o \ 6 6 remove.o pci.o pci-driver.o search.o \ 7 7 pci-sysfs.o rom.o setup-res.o irq.o vpd.o \ 8 - setup-bus.o vc.o mmap.o setup-irq.o msi.o 8 + setup-bus.o vc.o mmap.o setup-irq.o 9 9 10 + obj-$(CONFIG_PCI) += msi/ 10 11 obj-$(CONFIG_PCI) += pcie/ 11 12 12 13 ifdef CONFIG_PCI
+10 -5
drivers/pci/controller/pci-hyperv.c
··· 3445 3445 3446 3446 static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg) 3447 3447 { 3448 - struct msi_desc *entry; 3449 3448 struct irq_data *irq_data; 3449 + struct msi_desc *entry; 3450 + int ret = 0; 3450 3451 3451 - for_each_pci_msi_entry(entry, pdev) { 3452 + msi_lock_descs(&pdev->dev); 3453 + msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { 3452 3454 irq_data = irq_get_irq_data(entry->irq); 3453 - if (WARN_ON_ONCE(!irq_data)) 3454 - return -EINVAL; 3455 + if (WARN_ON_ONCE(!irq_data)) { 3456 + ret = -EINVAL; 3457 + break; 3458 + } 3455 3459 3456 3460 hv_compose_msi_msg(irq_data, &entry->msg); 3457 3461 } 3462 + msi_unlock_descs(&pdev->dev); 3458 3463 3459 - return 0; 3464 + return ret; 3460 3465 } 3461 3466 3462 3467 /*
+271 -685
drivers/pci/msi.c drivers/pci/msi/msi.c
··· 6 6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com) 7 7 * Copyright (C) 2016 Christoph Hellwig. 8 8 */ 9 - 10 9 #include <linux/err.h> 11 - #include <linux/mm.h> 12 - #include <linux/irq.h> 13 - #include <linux/interrupt.h> 14 10 #include <linux/export.h> 15 - #include <linux/ioport.h> 16 - #include <linux/pci.h> 17 - #include <linux/proc_fs.h> 18 - #include <linux/msi.h> 19 - #include <linux/smp.h> 20 - #include <linux/errno.h> 21 - #include <linux/io.h> 22 - #include <linux/acpi_iort.h> 23 - #include <linux/slab.h> 24 - #include <linux/irqdomain.h> 25 - #include <linux/of_irq.h> 11 + #include <linux/irq.h> 26 12 27 - #include "pci.h" 28 - 29 - #ifdef CONFIG_PCI_MSI 13 + #include "../pci.h" 14 + #include "msi.h" 30 15 31 16 static int pci_msi_enable = 1; 32 17 int pci_msi_ignore_mask; 33 18 34 - #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) 35 - 36 - #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN 37 - static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 38 - { 39 - struct irq_domain *domain; 40 - 41 - domain = dev_get_msi_domain(&dev->dev); 42 - if (domain && irq_domain_is_hierarchy(domain)) 43 - return msi_domain_alloc_irqs(domain, &dev->dev, nvec); 44 - 45 - return arch_setup_msi_irqs(dev, nvec, type); 46 - } 47 - 48 - static void pci_msi_teardown_msi_irqs(struct pci_dev *dev) 49 - { 50 - struct irq_domain *domain; 51 - 52 - domain = dev_get_msi_domain(&dev->dev); 53 - if (domain && irq_domain_is_hierarchy(domain)) 54 - msi_domain_free_irqs(domain, &dev->dev); 55 - else 56 - arch_teardown_msi_irqs(dev); 57 - } 58 - #else 59 - #define pci_msi_setup_msi_irqs arch_setup_msi_irqs 60 - #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs 61 - #endif 62 - 63 - #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS 64 - /* Arch hooks */ 65 - int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc) 66 - { 67 - return -EINVAL; 68 - } 69 - 70 - void __weak arch_teardown_msi_irq(unsigned int irq) 71 - { 72 - } 73 - 74 - int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 75 - { 76 - struct msi_desc *entry; 77 - int ret; 78 - 79 - /* 80 - * If an architecture wants to support multiple MSI, it needs to 81 - * override arch_setup_msi_irqs() 82 - */ 83 - if (type == PCI_CAP_ID_MSI && nvec > 1) 84 - return 1; 85 - 86 - for_each_pci_msi_entry(entry, dev) { 87 - ret = arch_setup_msi_irq(dev, entry); 88 - if (ret < 0) 89 - return ret; 90 - if (ret > 0) 91 - return -ENOSPC; 92 - } 93 - 94 - return 0; 95 - } 96 - 97 - void __weak arch_teardown_msi_irqs(struct pci_dev *dev) 98 - { 99 - int i; 100 - struct msi_desc *entry; 101 - 102 - for_each_pci_msi_entry(entry, dev) 103 - if (entry->irq) 104 - for (i = 0; i < entry->nvec_used; i++) 105 - arch_teardown_msi_irq(entry->irq + i); 106 - } 107 - #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */ 108 - 109 - static void default_restore_msi_irq(struct pci_dev *dev, int irq) 110 - { 111 - struct msi_desc *entry; 112 - 113 - entry = NULL; 114 - if (dev->msix_enabled) { 115 - for_each_pci_msi_entry(entry, dev) { 116 - if (irq == entry->irq) 117 - break; 118 - } 119 - } else if (dev->msi_enabled) { 120 - entry = irq_get_msi_desc(irq); 121 - } 122 - 123 - if (entry) 124 - __pci_write_msi_msg(entry, &entry->msg); 125 - } 126 - 127 - void __weak arch_restore_msi_irqs(struct pci_dev *dev) 128 - { 129 - return default_restore_msi_irqs(dev); 130 - } 131 - 132 - /* 133 - * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to 134 - * mask all MSI interrupts by clearing the MSI enable bit does not work 135 - * reliably as devices without an INTx disable bit will then generate a 136 - * level IRQ which will never be cleared. 137 - */ 138 - static inline __attribute_const__ u32 msi_multi_mask(struct msi_desc *desc) 139 - { 140 - /* Don't shift by >= width of type */ 141 - if (desc->msi_attrib.multi_cap >= 5) 142 - return 0xffffffff; 143 - return (1 << (1 << desc->msi_attrib.multi_cap)) - 1; 144 - } 145 - 146 19 static noinline void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set) 147 20 { 148 - raw_spinlock_t *lock = &desc->dev->msi_lock; 21 + raw_spinlock_t *lock = &to_pci_dev(desc->dev)->msi_lock; 149 22 unsigned long flags; 150 23 151 - if (!desc->msi_attrib.can_mask) 24 + if (!desc->pci.msi_attrib.can_mask) 152 25 return; 153 26 154 27 raw_spin_lock_irqsave(lock, flags); 155 - desc->msi_mask &= ~clear; 156 - desc->msi_mask |= set; 157 - pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos, 158 - desc->msi_mask); 28 + desc->pci.msi_mask &= ~clear; 29 + desc->pci.msi_mask |= set; 30 + pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->pci.mask_pos, 31 + desc->pci.msi_mask); 159 32 raw_spin_unlock_irqrestore(lock, flags); 160 33 } 161 34 ··· 44 171 45 172 static inline void __iomem *pci_msix_desc_addr(struct msi_desc *desc) 46 173 { 47 - return desc->mask_base + desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; 174 + return desc->pci.mask_base + desc->msi_index * PCI_MSIX_ENTRY_SIZE; 48 175 } 49 176 50 177 /* ··· 57 184 { 58 185 void __iomem *desc_addr = pci_msix_desc_addr(desc); 59 186 60 - if (desc->msi_attrib.can_mask) 187 + if (desc->pci.msi_attrib.can_mask) 61 188 writel(ctrl, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL); 62 189 } 63 190 64 191 static inline void pci_msix_mask(struct msi_desc *desc) 65 192 { 66 - desc->msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT; 67 - pci_msix_write_vector_ctrl(desc, desc->msix_ctrl); 193 + desc->pci.msix_ctrl |= PCI_MSIX_ENTRY_CTRL_MASKBIT; 194 + pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl); 68 195 /* Flush write to device */ 69 - readl(desc->mask_base); 196 + readl(desc->pci.mask_base); 70 197 } 71 198 72 199 static inline void pci_msix_unmask(struct msi_desc *desc) 73 200 { 74 - desc->msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; 75 - pci_msix_write_vector_ctrl(desc, desc->msix_ctrl); 201 + desc->pci.msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; 202 + pci_msix_write_vector_ctrl(desc, desc->pci.msix_ctrl); 76 203 } 77 204 78 205 static void __pci_msi_mask_desc(struct msi_desc *desc, u32 mask) 79 206 { 80 - if (desc->msi_attrib.is_msix) 207 + if (desc->pci.msi_attrib.is_msix) 81 208 pci_msix_mask(desc); 82 209 else 83 210 pci_msi_mask(desc, mask); ··· 85 212 86 213 static void __pci_msi_unmask_desc(struct msi_desc *desc, u32 mask) 87 214 { 88 - if (desc->msi_attrib.is_msix) 215 + if (desc->pci.msi_attrib.is_msix) 89 216 pci_msix_unmask(desc); 90 217 else 91 218 pci_msi_unmask(desc, mask); ··· 115 242 } 116 243 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq); 117 244 118 - void default_restore_msi_irqs(struct pci_dev *dev) 119 - { 120 - struct msi_desc *entry; 121 - 122 - for_each_pci_msi_entry(entry, dev) 123 - default_restore_msi_irq(dev, entry->irq); 124 - } 125 - 126 245 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg) 127 246 { 128 247 struct pci_dev *dev = msi_desc_to_pci_dev(entry); 129 248 130 249 BUG_ON(dev->current_state != PCI_D0); 131 250 132 - if (entry->msi_attrib.is_msix) { 251 + if (entry->pci.msi_attrib.is_msix) { 133 252 void __iomem *base = pci_msix_desc_addr(entry); 134 253 135 - if (WARN_ON_ONCE(entry->msi_attrib.is_virtual)) 254 + if (WARN_ON_ONCE(entry->pci.msi_attrib.is_virtual)) 136 255 return; 137 256 138 257 msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR); ··· 136 271 137 272 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, 138 273 &msg->address_lo); 139 - if (entry->msi_attrib.is_64) { 274 + if (entry->pci.msi_attrib.is_64) { 140 275 pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, 141 276 &msg->address_hi); 142 277 pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data); ··· 154 289 155 290 if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) { 156 291 /* Don't touch the hardware now */ 157 - } else if (entry->msi_attrib.is_msix) { 292 + } else if (entry->pci.msi_attrib.is_msix) { 158 293 void __iomem *base = pci_msix_desc_addr(entry); 159 - u32 ctrl = entry->msix_ctrl; 294 + u32 ctrl = entry->pci.msix_ctrl; 160 295 bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT); 161 296 162 - if (entry->msi_attrib.is_virtual) 297 + if (entry->pci.msi_attrib.is_virtual) 163 298 goto skip; 164 299 165 300 /* ··· 188 323 189 324 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); 190 325 msgctl &= ~PCI_MSI_FLAGS_QSIZE; 191 - msgctl |= entry->msi_attrib.multiple << 4; 326 + msgctl |= entry->pci.msi_attrib.multiple << 4; 192 327 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl); 193 328 194 329 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, 195 330 msg->address_lo); 196 - if (entry->msi_attrib.is_64) { 331 + if (entry->pci.msi_attrib.is_64) { 197 332 pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, 198 333 msg->address_hi); 199 334 pci_write_config_word(dev, pos + PCI_MSI_DATA_64, ··· 224 359 225 360 static void free_msi_irqs(struct pci_dev *dev) 226 361 { 227 - struct list_head *msi_list = dev_to_msi_list(&dev->dev); 228 - struct msi_desc *entry, *tmp; 229 - int i; 230 - 231 - for_each_pci_msi_entry(entry, dev) 232 - if (entry->irq) 233 - for (i = 0; i < entry->nvec_used; i++) 234 - BUG_ON(irq_has_action(entry->irq + i)); 235 - 236 - if (dev->msi_irq_groups) { 237 - msi_destroy_sysfs(&dev->dev, dev->msi_irq_groups); 238 - dev->msi_irq_groups = NULL; 239 - } 240 - 241 362 pci_msi_teardown_msi_irqs(dev); 242 363 243 - list_for_each_entry_safe(entry, tmp, msi_list, list) { 244 - if (entry->msi_attrib.is_msix) { 245 - if (list_is_last(&entry->list, msi_list)) 246 - iounmap(entry->mask_base); 247 - } 248 - 249 - list_del(&entry->list); 250 - free_msi_entry(entry); 364 + if (dev->msix_base) { 365 + iounmap(dev->msix_base); 366 + dev->msix_base = NULL; 251 367 } 252 368 } 253 369 ··· 249 403 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); 250 404 } 251 405 406 + /* 407 + * Architecture override returns true when the PCI MSI message should be 408 + * written by the generic restore function. 409 + */ 410 + bool __weak arch_restore_msi_irqs(struct pci_dev *dev) 411 + { 412 + return true; 413 + } 414 + 252 415 static void __pci_restore_msi_state(struct pci_dev *dev) 253 416 { 254 - u16 control; 255 417 struct msi_desc *entry; 418 + u16 control; 256 419 257 420 if (!dev->msi_enabled) 258 421 return; ··· 270 415 271 416 pci_intx_for_msi(dev, 0); 272 417 pci_msi_set_enable(dev, 0); 273 - arch_restore_msi_irqs(dev); 418 + if (arch_restore_msi_irqs(dev)) 419 + __pci_write_msi_msg(entry, &entry->msg); 274 420 275 421 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); 276 422 pci_msi_update_mask(entry, 0, 0); 277 423 control &= ~PCI_MSI_FLAGS_QSIZE; 278 - control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE; 424 + control |= (entry->pci.msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE; 279 425 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); 280 426 } 281 427 ··· 293 437 static void __pci_restore_msix_state(struct pci_dev *dev) 294 438 { 295 439 struct msi_desc *entry; 440 + bool write_msg; 296 441 297 442 if (!dev->msix_enabled) 298 443 return; 299 - BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); 300 444 301 445 /* route the table */ 302 446 pci_intx_for_msi(dev, 0); 303 447 pci_msix_clear_and_set_ctrl(dev, 0, 304 448 PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL); 305 449 306 - arch_restore_msi_irqs(dev); 307 - for_each_pci_msi_entry(entry, dev) 308 - pci_msix_write_vector_ctrl(entry, entry->msix_ctrl); 450 + write_msg = arch_restore_msi_irqs(dev); 451 + 452 + msi_lock_descs(&dev->dev); 453 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) { 454 + if (write_msg) 455 + __pci_write_msi_msg(entry, &entry->msg); 456 + pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl); 457 + } 458 + msi_unlock_descs(&dev->dev); 309 459 310 460 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); 311 461 } ··· 323 461 } 324 462 EXPORT_SYMBOL_GPL(pci_restore_msi_state); 325 463 326 - static struct msi_desc * 327 - msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd) 464 + static void pcim_msi_release(void *pcidev) 328 465 { 329 - struct irq_affinity_desc *masks = NULL; 330 - struct msi_desc *entry; 466 + struct pci_dev *dev = pcidev; 467 + 468 + dev->is_msi_managed = false; 469 + pci_free_irq_vectors(dev); 470 + } 471 + 472 + /* 473 + * Needs to be separate from pcim_release to prevent an ordering problem 474 + * vs. msi_device_data_release() in the MSI core code. 475 + */ 476 + static int pcim_setup_msi_release(struct pci_dev *dev) 477 + { 478 + int ret; 479 + 480 + if (!pci_is_managed(dev) || dev->is_msi_managed) 481 + return 0; 482 + 483 + ret = devm_add_action(&dev->dev, pcim_msi_release, dev); 484 + if (!ret) 485 + dev->is_msi_managed = true; 486 + return ret; 487 + } 488 + 489 + /* 490 + * Ordering vs. devres: msi device data has to be installed first so that 491 + * pcim_msi_release() is invoked before it on device release. 492 + */ 493 + static int pci_setup_msi_context(struct pci_dev *dev) 494 + { 495 + int ret = msi_setup_device_data(&dev->dev); 496 + 497 + if (!ret) 498 + ret = pcim_setup_msi_release(dev); 499 + return ret; 500 + } 501 + 502 + static int msi_setup_msi_desc(struct pci_dev *dev, int nvec, 503 + struct irq_affinity_desc *masks) 504 + { 505 + struct msi_desc desc; 331 506 u16 control; 332 507 333 - if (affd) 334 - masks = irq_create_affinity_masks(nvec, affd); 335 - 336 508 /* MSI Entry Initialization */ 337 - entry = alloc_msi_entry(&dev->dev, nvec, masks); 338 - if (!entry) 339 - goto out; 509 + memset(&desc, 0, sizeof(desc)); 340 510 341 511 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); 342 512 /* Lies, damned lies, and MSIs */ 343 513 if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING) 344 514 control |= PCI_MSI_FLAGS_MASKBIT; 515 + /* Respect XEN's mask disabling */ 516 + if (pci_msi_ignore_mask) 517 + control &= ~PCI_MSI_FLAGS_MASKBIT; 345 518 346 - entry->msi_attrib.is_msix = 0; 347 - entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT); 348 - entry->msi_attrib.is_virtual = 0; 349 - entry->msi_attrib.entry_nr = 0; 350 - entry->msi_attrib.can_mask = !pci_msi_ignore_mask && 351 - !!(control & PCI_MSI_FLAGS_MASKBIT); 352 - entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ 353 - entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1; 354 - entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec)); 519 + desc.nvec_used = nvec; 520 + desc.pci.msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT); 521 + desc.pci.msi_attrib.can_mask = !!(control & PCI_MSI_FLAGS_MASKBIT); 522 + desc.pci.msi_attrib.default_irq = dev->irq; 523 + desc.pci.msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1; 524 + desc.pci.msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec)); 525 + desc.affinity = masks; 355 526 356 527 if (control & PCI_MSI_FLAGS_64BIT) 357 - entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64; 528 + desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_64; 358 529 else 359 - entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32; 530 + desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_32; 360 531 361 532 /* Save the initial mask status */ 362 - if (entry->msi_attrib.can_mask) 363 - pci_read_config_dword(dev, entry->mask_pos, &entry->msi_mask); 533 + if (desc.pci.msi_attrib.can_mask) 534 + pci_read_config_dword(dev, desc.pci.mask_pos, &desc.pci.msi_mask); 364 535 365 - out: 366 - kfree(masks); 367 - return entry; 536 + return msi_add_msi_desc(&dev->dev, &desc); 368 537 } 369 538 370 539 static int msi_verify_entries(struct pci_dev *dev) ··· 405 512 if (!dev->no_64bit_msi) 406 513 return 0; 407 514 408 - for_each_pci_msi_entry(entry, dev) { 515 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) { 409 516 if (entry->msg.address_hi) { 410 517 pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n", 411 518 entry->msg.address_hi, entry->msg.address_lo); 412 - return -EIO; 519 + break; 413 520 } 414 521 } 415 - return 0; 522 + return !entry ? 0 : -EIO; 416 523 } 417 524 418 525 /** ··· 430 537 static int msi_capability_init(struct pci_dev *dev, int nvec, 431 538 struct irq_affinity *affd) 432 539 { 433 - const struct attribute_group **groups; 540 + struct irq_affinity_desc *masks = NULL; 434 541 struct msi_desc *entry; 435 542 int ret; 436 543 437 - pci_msi_set_enable(dev, 0); /* Disable MSI during set up */ 544 + /* 545 + * Disable MSI during setup in the hardware, but mark it enabled 546 + * so that setup code can evaluate it. 547 + */ 548 + pci_msi_set_enable(dev, 0); 549 + dev->msi_enabled = 1; 438 550 439 - entry = msi_setup_entry(dev, nvec, affd); 440 - if (!entry) 441 - return -ENOMEM; 551 + if (affd) 552 + masks = irq_create_affinity_masks(nvec, affd); 553 + 554 + msi_lock_descs(&dev->dev); 555 + ret = msi_setup_msi_desc(dev, nvec, masks); 556 + if (ret) 557 + goto fail; 442 558 443 559 /* All MSIs are unmasked by default; mask them all */ 560 + entry = msi_first_desc(&dev->dev, MSI_DESC_ALL); 444 561 pci_msi_mask(entry, msi_multi_mask(entry)); 445 - 446 - list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); 447 562 448 563 /* Configure MSI capability structure */ 449 564 ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI); ··· 462 561 if (ret) 463 562 goto err; 464 563 465 - groups = msi_populate_sysfs(&dev->dev); 466 - if (IS_ERR(groups)) { 467 - ret = PTR_ERR(groups); 468 - goto err; 469 - } 470 - 471 - dev->msi_irq_groups = groups; 472 - 473 564 /* Set MSI enabled bits */ 474 565 pci_intx_for_msi(dev, 0); 475 566 pci_msi_set_enable(dev, 1); 476 - dev->msi_enabled = 1; 477 567 478 568 pcibios_free_irq(dev); 479 569 dev->irq = entry->irq; 480 - return 0; 570 + goto unlock; 481 571 482 572 err: 483 573 pci_msi_unmask(entry, msi_multi_mask(entry)); 484 574 free_msi_irqs(dev); 575 + fail: 576 + dev->msi_enabled = 0; 577 + unlock: 578 + msi_unlock_descs(&dev->dev); 579 + kfree(masks); 485 580 return ret; 486 581 } 487 582 ··· 502 605 return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); 503 606 } 504 607 505 - static int msix_setup_entries(struct pci_dev *dev, void __iomem *base, 506 - struct msix_entry *entries, int nvec, 507 - struct irq_affinity *affd) 608 + static int msix_setup_msi_descs(struct pci_dev *dev, void __iomem *base, 609 + struct msix_entry *entries, int nvec, 610 + struct irq_affinity_desc *masks) 508 611 { 509 - struct irq_affinity_desc *curmsk, *masks = NULL; 510 - struct msi_desc *entry; 612 + int ret = 0, i, vec_count = pci_msix_vec_count(dev); 613 + struct irq_affinity_desc *curmsk; 614 + struct msi_desc desc; 511 615 void __iomem *addr; 512 - int ret, i; 513 - int vec_count = pci_msix_vec_count(dev); 514 616 515 - if (affd) 516 - masks = irq_create_affinity_masks(nvec, affd); 617 + memset(&desc, 0, sizeof(desc)); 517 618 518 - for (i = 0, curmsk = masks; i < nvec; i++) { 519 - entry = alloc_msi_entry(&dev->dev, 1, curmsk); 520 - if (!entry) { 521 - if (!i) 522 - iounmap(base); 523 - else 524 - free_msi_irqs(dev); 525 - /* No enough memory. Don't try again */ 526 - ret = -ENOMEM; 527 - goto out; 619 + desc.nvec_used = 1; 620 + desc.pci.msi_attrib.is_msix = 1; 621 + desc.pci.msi_attrib.is_64 = 1; 622 + desc.pci.msi_attrib.default_irq = dev->irq; 623 + desc.pci.mask_base = base; 624 + 625 + for (i = 0, curmsk = masks; i < nvec; i++, curmsk++) { 626 + desc.msi_index = entries ? entries[i].entry : i; 627 + desc.affinity = masks ? curmsk : NULL; 628 + desc.pci.msi_attrib.is_virtual = desc.msi_index >= vec_count; 629 + desc.pci.msi_attrib.can_mask = !pci_msi_ignore_mask && 630 + !desc.pci.msi_attrib.is_virtual; 631 + 632 + if (!desc.pci.msi_attrib.can_mask) { 633 + addr = pci_msix_desc_addr(&desc); 634 + desc.pci.msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL); 528 635 } 529 636 530 - entry->msi_attrib.is_msix = 1; 531 - entry->msi_attrib.is_64 = 1; 532 - 533 - if (entries) 534 - entry->msi_attrib.entry_nr = entries[i].entry; 535 - else 536 - entry->msi_attrib.entry_nr = i; 537 - 538 - entry->msi_attrib.is_virtual = 539 - entry->msi_attrib.entry_nr >= vec_count; 540 - 541 - entry->msi_attrib.can_mask = !pci_msi_ignore_mask && 542 - !entry->msi_attrib.is_virtual; 543 - 544 - entry->msi_attrib.default_irq = dev->irq; 545 - entry->mask_base = base; 546 - 547 - if (entry->msi_attrib.can_mask) { 548 - addr = pci_msix_desc_addr(entry); 549 - entry->msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL); 550 - } 551 - 552 - list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); 553 - if (masks) 554 - curmsk++; 637 + ret = msi_add_msi_desc(&dev->dev, &desc); 638 + if (ret) 639 + break; 555 640 } 556 - ret = 0; 557 - out: 558 - kfree(masks); 559 641 return ret; 560 642 } 561 643 562 644 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries) 563 645 { 564 - struct msi_desc *entry; 646 + struct msi_desc *desc; 565 647 566 - for_each_pci_msi_entry(entry, dev) { 567 - if (entries) { 568 - entries->vector = entry->irq; 648 + if (entries) { 649 + msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL) { 650 + entries->vector = desc->irq; 569 651 entries++; 570 652 } 571 653 } ··· 562 686 writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL); 563 687 } 564 688 689 + static int msix_setup_interrupts(struct pci_dev *dev, void __iomem *base, 690 + struct msix_entry *entries, int nvec, 691 + struct irq_affinity *affd) 692 + { 693 + struct irq_affinity_desc *masks = NULL; 694 + int ret; 695 + 696 + if (affd) 697 + masks = irq_create_affinity_masks(nvec, affd); 698 + 699 + msi_lock_descs(&dev->dev); 700 + ret = msix_setup_msi_descs(dev, base, entries, nvec, masks); 701 + if (ret) 702 + goto out_free; 703 + 704 + ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); 705 + if (ret) 706 + goto out_free; 707 + 708 + /* Check if all MSI entries honor device restrictions */ 709 + ret = msi_verify_entries(dev); 710 + if (ret) 711 + goto out_free; 712 + 713 + msix_update_entries(dev, entries); 714 + goto out_unlock; 715 + 716 + out_free: 717 + free_msi_irqs(dev); 718 + out_unlock: 719 + msi_unlock_descs(&dev->dev); 720 + kfree(masks); 721 + return ret; 722 + } 723 + 565 724 /** 566 725 * msix_capability_init - configure device's MSI-X capability 567 726 * @dev: pointer to the pci_dev data structure of MSI-X device function ··· 611 700 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, 612 701 int nvec, struct irq_affinity *affd) 613 702 { 614 - const struct attribute_group **groups; 615 703 void __iomem *base; 616 704 int ret, tsize; 617 705 u16 control; ··· 623 713 pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL | 624 714 PCI_MSIX_FLAGS_ENABLE); 625 715 716 + /* Mark it enabled so setup functions can query it */ 717 + dev->msix_enabled = 1; 718 + 626 719 pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); 627 720 /* Request & Map MSI-X table region */ 628 721 tsize = msix_table_size(control); ··· 635 722 goto out_disable; 636 723 } 637 724 638 - ret = msix_setup_entries(dev, base, entries, nvec, affd); 725 + dev->msix_base = base; 726 + 727 + ret = msix_setup_interrupts(dev, base, entries, nvec, affd); 639 728 if (ret) 640 729 goto out_disable; 641 730 642 - ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); 643 - if (ret) 644 - goto out_avail; 645 - 646 - /* Check if all MSI entries honor device restrictions */ 647 - ret = msi_verify_entries(dev); 648 - if (ret) 649 - goto out_free; 650 - 651 - msix_update_entries(dev, entries); 652 - 653 - groups = msi_populate_sysfs(&dev->dev); 654 - if (IS_ERR(groups)) { 655 - ret = PTR_ERR(groups); 656 - goto out_free; 657 - } 658 - 659 - dev->msi_irq_groups = groups; 660 - 661 - /* Set MSI-X enabled bits and unmask the function */ 731 + /* Disable INTX */ 662 732 pci_intx_for_msi(dev, 0); 663 - dev->msix_enabled = 1; 664 733 665 734 /* 666 735 * Ensure that all table entries are masked to prevent ··· 658 763 pcibios_free_irq(dev); 659 764 return 0; 660 765 661 - out_avail: 662 - if (ret < 0) { 663 - /* 664 - * If we had some success, report the number of IRQs 665 - * we succeeded in setting up. 666 - */ 667 - struct msi_desc *entry; 668 - int avail = 0; 669 - 670 - for_each_pci_msi_entry(entry, dev) { 671 - if (entry->irq != 0) 672 - avail++; 673 - } 674 - if (avail != 0) 675 - ret = avail; 676 - } 677 - 678 - out_free: 679 - free_msi_irqs(dev); 680 - 681 766 out_disable: 767 + dev->msix_enabled = 0; 682 768 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0); 683 769 684 770 return ret; ··· 746 870 if (!pci_msi_enable || !dev || !dev->msi_enabled) 747 871 return; 748 872 749 - BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); 750 - desc = first_pci_msi_entry(dev); 751 - 752 873 pci_msi_set_enable(dev, 0); 753 874 pci_intx_for_msi(dev, 1); 754 875 dev->msi_enabled = 0; 755 876 756 877 /* Return the device with MSI unmasked as initial states */ 757 - pci_msi_unmask(desc, msi_multi_mask(desc)); 878 + desc = msi_first_desc(&dev->dev, MSI_DESC_ALL); 879 + if (!WARN_ON_ONCE(!desc)) 880 + pci_msi_unmask(desc, msi_multi_mask(desc)); 758 881 759 882 /* Restore dev->irq to its default pin-assertion IRQ */ 760 - dev->irq = desc->msi_attrib.default_irq; 883 + dev->irq = desc->pci.msi_attrib.default_irq; 761 884 pcibios_alloc_irq(dev); 762 885 } 763 886 ··· 765 890 if (!pci_msi_enable || !dev || !dev->msi_enabled) 766 891 return; 767 892 893 + msi_lock_descs(&dev->dev); 768 894 pci_msi_shutdown(dev); 769 895 free_msi_irqs(dev); 896 + msi_unlock_descs(&dev->dev); 770 897 } 771 898 EXPORT_SYMBOL(pci_disable_msi); 772 899 ··· 829 952 830 953 static void pci_msix_shutdown(struct pci_dev *dev) 831 954 { 832 - struct msi_desc *entry; 955 + struct msi_desc *desc; 833 956 834 957 if (!pci_msi_enable || !dev || !dev->msix_enabled) 835 958 return; ··· 840 963 } 841 964 842 965 /* Return the device with MSI-X masked as initial states */ 843 - for_each_pci_msi_entry(entry, dev) 844 - pci_msix_mask(entry); 966 + msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL) 967 + pci_msix_mask(desc); 845 968 846 969 pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); 847 970 pci_intx_for_msi(dev, 1); ··· 854 977 if (!pci_msi_enable || !dev || !dev->msix_enabled) 855 978 return; 856 979 980 + msi_lock_descs(&dev->dev); 857 981 pci_msix_shutdown(dev); 858 982 free_msi_irqs(dev); 983 + msi_unlock_descs(&dev->dev); 859 984 } 860 985 EXPORT_SYMBOL(pci_disable_msix); 861 - 862 - void pci_no_msi(void) 863 - { 864 - pci_msi_enable = 0; 865 - } 866 - 867 - /** 868 - * pci_msi_enabled - is MSI enabled? 869 - * 870 - * Returns true if MSI has not been disabled by the command-line option 871 - * pci=nomsi. 872 - **/ 873 - int pci_msi_enabled(void) 874 - { 875 - return pci_msi_enable; 876 - } 877 - EXPORT_SYMBOL(pci_msi_enabled); 878 986 879 987 static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec, 880 988 struct irq_affinity *affd) ··· 890 1028 891 1029 if (nvec > maxvec) 892 1030 nvec = maxvec; 1031 + 1032 + rc = pci_setup_msi_context(dev); 1033 + if (rc) 1034 + return rc; 893 1035 894 1036 for (;;) { 895 1037 if (affd) { ··· 937 1071 938 1072 if (WARN_ON_ONCE(dev->msix_enabled)) 939 1073 return -EINVAL; 1074 + 1075 + rc = pci_setup_msi_context(dev); 1076 + if (rc) 1077 + return rc; 940 1078 941 1079 for (;;) { 942 1080 if (affd) { ··· 1064 1194 1065 1195 /** 1066 1196 * pci_irq_vector - return Linux IRQ number of a device vector 1067 - * @dev: PCI device to operate on 1068 - * @nr: device-relative interrupt vector index (0-based). 1197 + * @dev: PCI device to operate on 1198 + * @nr: Interrupt vector index (0-based) 1199 + * 1200 + * @nr has the following meanings depending on the interrupt mode: 1201 + * MSI-X: The index in the MSI-X vector table 1202 + * MSI: The index of the enabled MSI vectors 1203 + * INTx: Must be 0 1204 + * 1205 + * Return: The Linux interrupt number or -EINVAl if @nr is out of range. 1069 1206 */ 1070 1207 int pci_irq_vector(struct pci_dev *dev, unsigned int nr) 1071 1208 { 1072 - if (dev->msix_enabled) { 1073 - struct msi_desc *entry; 1074 - int i = 0; 1209 + unsigned int irq; 1075 1210 1076 - for_each_pci_msi_entry(entry, dev) { 1077 - if (i == nr) 1078 - return entry->irq; 1079 - i++; 1080 - } 1081 - WARN_ON_ONCE(1); 1082 - return -EINVAL; 1083 - } 1211 + if (!dev->msi_enabled && !dev->msix_enabled) 1212 + return !nr ? dev->irq : -EINVAL; 1084 1213 1085 - if (dev->msi_enabled) { 1086 - struct msi_desc *entry = first_pci_msi_entry(dev); 1087 - 1088 - if (WARN_ON_ONCE(nr >= entry->nvec_used)) 1089 - return -EINVAL; 1090 - } else { 1091 - if (WARN_ON_ONCE(nr > 0)) 1092 - return -EINVAL; 1093 - } 1094 - 1095 - return dev->irq + nr; 1214 + irq = msi_get_virq(&dev->dev, nr); 1215 + return irq ? irq : -EINVAL; 1096 1216 } 1097 1217 EXPORT_SYMBOL(pci_irq_vector); 1098 1218 ··· 1090 1230 * pci_irq_get_affinity - return the affinity of a particular MSI vector 1091 1231 * @dev: PCI device to operate on 1092 1232 * @nr: device-relative interrupt vector index (0-based). 1233 + * 1234 + * @nr has the following meanings depending on the interrupt mode: 1235 + * MSI-X: The index in the MSI-X vector table 1236 + * MSI: The index of the enabled MSI vectors 1237 + * INTx: Must be 0 1238 + * 1239 + * Return: A cpumask pointer or NULL if @nr is out of range 1093 1240 */ 1094 1241 const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr) 1095 1242 { 1096 - if (dev->msix_enabled) { 1097 - struct msi_desc *entry; 1098 - int i = 0; 1243 + int idx, irq = pci_irq_vector(dev, nr); 1244 + struct msi_desc *desc; 1099 1245 1100 - for_each_pci_msi_entry(entry, dev) { 1101 - if (i == nr) 1102 - return &entry->affinity->mask; 1103 - i++; 1104 - } 1105 - WARN_ON_ONCE(1); 1246 + if (WARN_ON_ONCE(irq <= 0)) 1106 1247 return NULL; 1107 - } else if (dev->msi_enabled) { 1108 - struct msi_desc *entry = first_pci_msi_entry(dev); 1109 1248 1110 - if (WARN_ON_ONCE(!entry || !entry->affinity || 1111 - nr >= entry->nvec_used)) 1112 - return NULL; 1113 - 1114 - return &entry->affinity[nr].mask; 1115 - } else { 1249 + desc = irq_get_msi_desc(irq); 1250 + /* Non-MSI does not have the information handy */ 1251 + if (!desc) 1116 1252 return cpu_possible_mask; 1117 - } 1253 + 1254 + if (WARN_ON_ONCE(!desc->affinity)) 1255 + return NULL; 1256 + 1257 + /* 1258 + * MSI has a mask array in the descriptor. 1259 + * MSI-X has a single mask. 1260 + */ 1261 + idx = dev->msi_enabled ? nr : 0; 1262 + return &desc->affinity[idx].mask; 1118 1263 } 1119 1264 EXPORT_SYMBOL(pci_irq_get_affinity); 1120 1265 ··· 1129 1264 } 1130 1265 EXPORT_SYMBOL(msi_desc_to_pci_dev); 1131 1266 1132 - void *msi_desc_to_pci_sysdata(struct msi_desc *desc) 1267 + void pci_no_msi(void) 1133 1268 { 1134 - struct pci_dev *dev = msi_desc_to_pci_dev(desc); 1135 - 1136 - return dev->bus->sysdata; 1137 - } 1138 - EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata); 1139 - 1140 - #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN 1141 - /** 1142 - * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space 1143 - * @irq_data: Pointer to interrupt data of the MSI interrupt 1144 - * @msg: Pointer to the message 1145 - */ 1146 - void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg) 1147 - { 1148 - struct msi_desc *desc = irq_data_get_msi_desc(irq_data); 1149 - 1150 - /* 1151 - * For MSI-X desc->irq is always equal to irq_data->irq. For 1152 - * MSI only the first interrupt of MULTI MSI passes the test. 1153 - */ 1154 - if (desc->irq == irq_data->irq) 1155 - __pci_write_msi_msg(desc, msg); 1269 + pci_msi_enable = 0; 1156 1270 } 1157 1271 1158 1272 /** 1159 - * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source 1160 - * @desc: Pointer to the MSI descriptor 1273 + * pci_msi_enabled - is MSI enabled? 1161 1274 * 1162 - * The ID number is only used within the irqdomain. 1163 - */ 1164 - static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc) 1275 + * Returns true if MSI has not been disabled by the command-line option 1276 + * pci=nomsi. 1277 + **/ 1278 + int pci_msi_enabled(void) 1165 1279 { 1166 - struct pci_dev *dev = msi_desc_to_pci_dev(desc); 1167 - 1168 - return (irq_hw_number_t)desc->msi_attrib.entry_nr | 1169 - pci_dev_id(dev) << 11 | 1170 - (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27; 1280 + return pci_msi_enable; 1171 1281 } 1172 - 1173 - static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc) 1174 - { 1175 - return !desc->msi_attrib.is_msix && desc->nvec_used > 1; 1176 - } 1177 - 1178 - /** 1179 - * pci_msi_domain_check_cap - Verify that @domain supports the capabilities 1180 - * for @dev 1181 - * @domain: The interrupt domain to check 1182 - * @info: The domain info for verification 1183 - * @dev: The device to check 1184 - * 1185 - * Returns: 1186 - * 0 if the functionality is supported 1187 - * 1 if Multi MSI is requested, but the domain does not support it 1188 - * -ENOTSUPP otherwise 1189 - */ 1190 - int pci_msi_domain_check_cap(struct irq_domain *domain, 1191 - struct msi_domain_info *info, struct device *dev) 1192 - { 1193 - struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev)); 1194 - 1195 - /* Special handling to support __pci_enable_msi_range() */ 1196 - if (pci_msi_desc_is_multi_msi(desc) && 1197 - !(info->flags & MSI_FLAG_MULTI_PCI_MSI)) 1198 - return 1; 1199 - else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX)) 1200 - return -ENOTSUPP; 1201 - 1202 - return 0; 1203 - } 1204 - 1205 - static int pci_msi_domain_handle_error(struct irq_domain *domain, 1206 - struct msi_desc *desc, int error) 1207 - { 1208 - /* Special handling to support __pci_enable_msi_range() */ 1209 - if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC) 1210 - return 1; 1211 - 1212 - return error; 1213 - } 1214 - 1215 - static void pci_msi_domain_set_desc(msi_alloc_info_t *arg, 1216 - struct msi_desc *desc) 1217 - { 1218 - arg->desc = desc; 1219 - arg->hwirq = pci_msi_domain_calc_hwirq(desc); 1220 - } 1221 - 1222 - static struct msi_domain_ops pci_msi_domain_ops_default = { 1223 - .set_desc = pci_msi_domain_set_desc, 1224 - .msi_check = pci_msi_domain_check_cap, 1225 - .handle_error = pci_msi_domain_handle_error, 1226 - }; 1227 - 1228 - static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info) 1229 - { 1230 - struct msi_domain_ops *ops = info->ops; 1231 - 1232 - if (ops == NULL) { 1233 - info->ops = &pci_msi_domain_ops_default; 1234 - } else { 1235 - if (ops->set_desc == NULL) 1236 - ops->set_desc = pci_msi_domain_set_desc; 1237 - if (ops->msi_check == NULL) 1238 - ops->msi_check = pci_msi_domain_check_cap; 1239 - if (ops->handle_error == NULL) 1240 - ops->handle_error = pci_msi_domain_handle_error; 1241 - } 1242 - } 1243 - 1244 - static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info) 1245 - { 1246 - struct irq_chip *chip = info->chip; 1247 - 1248 - BUG_ON(!chip); 1249 - if (!chip->irq_write_msi_msg) 1250 - chip->irq_write_msi_msg = pci_msi_domain_write_msg; 1251 - if (!chip->irq_mask) 1252 - chip->irq_mask = pci_msi_mask_irq; 1253 - if (!chip->irq_unmask) 1254 - chip->irq_unmask = pci_msi_unmask_irq; 1255 - } 1256 - 1257 - /** 1258 - * pci_msi_create_irq_domain - Create a MSI interrupt domain 1259 - * @fwnode: Optional fwnode of the interrupt controller 1260 - * @info: MSI domain info 1261 - * @parent: Parent irq domain 1262 - * 1263 - * Updates the domain and chip ops and creates a MSI interrupt domain. 1264 - * 1265 - * Returns: 1266 - * A domain pointer or NULL in case of failure. 1267 - */ 1268 - struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, 1269 - struct msi_domain_info *info, 1270 - struct irq_domain *parent) 1271 - { 1272 - struct irq_domain *domain; 1273 - 1274 - if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE)) 1275 - info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; 1276 - 1277 - if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS) 1278 - pci_msi_domain_update_dom_ops(info); 1279 - if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 1280 - pci_msi_domain_update_chip_ops(info); 1281 - 1282 - info->flags |= MSI_FLAG_ACTIVATE_EARLY; 1283 - if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE)) 1284 - info->flags |= MSI_FLAG_MUST_REACTIVATE; 1285 - 1286 - /* PCI-MSI is oneshot-safe */ 1287 - info->chip->flags |= IRQCHIP_ONESHOT_SAFE; 1288 - 1289 - domain = msi_create_irq_domain(fwnode, info, parent); 1290 - if (!domain) 1291 - return NULL; 1292 - 1293 - irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI); 1294 - return domain; 1295 - } 1296 - EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain); 1297 - 1298 - /* 1299 - * Users of the generic MSI infrastructure expect a device to have a single ID, 1300 - * so with DMA aliases we have to pick the least-worst compromise. Devices with 1301 - * DMA phantom functions tend to still emit MSIs from the real function number, 1302 - * so we ignore those and only consider topological aliases where either the 1303 - * alias device or RID appears on a different bus number. We also make the 1304 - * reasonable assumption that bridges are walked in an upstream direction (so 1305 - * the last one seen wins), and the much braver assumption that the most likely 1306 - * case is that of PCI->PCIe so we should always use the alias RID. This echoes 1307 - * the logic from intel_irq_remapping's set_msi_sid(), which presumably works 1308 - * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions 1309 - * for taking ownership all we can really do is close our eyes and hope... 1310 - */ 1311 - static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data) 1312 - { 1313 - u32 *pa = data; 1314 - u8 bus = PCI_BUS_NUM(*pa); 1315 - 1316 - if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus) 1317 - *pa = alias; 1318 - 1319 - return 0; 1320 - } 1321 - 1322 - /** 1323 - * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID) 1324 - * @domain: The interrupt domain 1325 - * @pdev: The PCI device. 1326 - * 1327 - * The RID for a device is formed from the alias, with a firmware 1328 - * supplied mapping applied 1329 - * 1330 - * Returns: The RID. 1331 - */ 1332 - u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev) 1333 - { 1334 - struct device_node *of_node; 1335 - u32 rid = pci_dev_id(pdev); 1336 - 1337 - pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); 1338 - 1339 - of_node = irq_domain_get_of_node(domain); 1340 - rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) : 1341 - iort_msi_map_id(&pdev->dev, rid); 1342 - 1343 - return rid; 1344 - } 1345 - 1346 - /** 1347 - * pci_msi_get_device_domain - Get the MSI domain for a given PCI device 1348 - * @pdev: The PCI device 1349 - * 1350 - * Use the firmware data to find a device-specific MSI domain 1351 - * (i.e. not one that is set as a default). 1352 - * 1353 - * Returns: The corresponding MSI domain or NULL if none has been found. 1354 - */ 1355 - struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev) 1356 - { 1357 - struct irq_domain *dom; 1358 - u32 rid = pci_dev_id(pdev); 1359 - 1360 - pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); 1361 - dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI); 1362 - if (!dom) 1363 - dom = iort_get_device_domain(&pdev->dev, rid, 1364 - DOMAIN_BUS_PCI_MSI); 1365 - return dom; 1366 - } 1367 - 1368 - /** 1369 - * pci_dev_has_special_msi_domain - Check whether the device is handled by 1370 - * a non-standard PCI-MSI domain 1371 - * @pdev: The PCI device to check. 1372 - * 1373 - * Returns: True if the device irqdomain or the bus irqdomain is 1374 - * non-standard PCI/MSI. 1375 - */ 1376 - bool pci_dev_has_special_msi_domain(struct pci_dev *pdev) 1377 - { 1378 - struct irq_domain *dom = dev_get_msi_domain(&pdev->dev); 1379 - 1380 - if (!dom) 1381 - dom = dev_get_msi_domain(&pdev->bus->dev); 1382 - 1383 - if (!dom) 1384 - return true; 1385 - 1386 - return dom->bus_token != DOMAIN_BUS_PCI_MSI; 1387 - } 1388 - 1389 - #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */ 1390 - #endif /* CONFIG_PCI_MSI */ 1391 - 1392 - void pci_msi_init(struct pci_dev *dev) 1393 - { 1394 - u16 ctrl; 1395 - 1396 - /* 1397 - * Disable the MSI hardware to avoid screaming interrupts 1398 - * during boot. This is the power on reset default so 1399 - * usually this should be a noop. 1400 - */ 1401 - dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI); 1402 - if (!dev->msi_cap) 1403 - return; 1404 - 1405 - pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl); 1406 - if (ctrl & PCI_MSI_FLAGS_ENABLE) 1407 - pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, 1408 - ctrl & ~PCI_MSI_FLAGS_ENABLE); 1409 - 1410 - if (!(ctrl & PCI_MSI_FLAGS_64BIT)) 1411 - dev->no_64bit_msi = 1; 1412 - } 1413 - 1414 - void pci_msix_init(struct pci_dev *dev) 1415 - { 1416 - u16 ctrl; 1417 - 1418 - dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX); 1419 - if (!dev->msix_cap) 1420 - return; 1421 - 1422 - pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 1423 - if (ctrl & PCI_MSIX_FLAGS_ENABLE) 1424 - pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, 1425 - ctrl & ~PCI_MSIX_FLAGS_ENABLE); 1426 - } 1282 + EXPORT_SYMBOL(pci_msi_enabled);
+7
drivers/pci/msi/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + # 3 + # Makefile for the PCI/MSI 4 + obj-$(CONFIG_PCI) += pcidev_msi.o 5 + obj-$(CONFIG_PCI_MSI) += msi.o 6 + obj-$(CONFIG_PCI_MSI_IRQ_DOMAIN) += irqdomain.o 7 + obj-$(CONFIG_PCI_MSI_ARCH_FALLBACKS) += legacy.o
+280
drivers/pci/msi/irqdomain.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * PCI Message Signaled Interrupt (MSI) - irqdomain support 4 + */ 5 + #include <linux/acpi_iort.h> 6 + #include <linux/irqdomain.h> 7 + #include <linux/of_irq.h> 8 + 9 + #include "msi.h" 10 + 11 + int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 12 + { 13 + struct irq_domain *domain; 14 + 15 + domain = dev_get_msi_domain(&dev->dev); 16 + if (domain && irq_domain_is_hierarchy(domain)) 17 + return msi_domain_alloc_irqs_descs_locked(domain, &dev->dev, nvec); 18 + 19 + return pci_msi_legacy_setup_msi_irqs(dev, nvec, type); 20 + } 21 + 22 + void pci_msi_teardown_msi_irqs(struct pci_dev *dev) 23 + { 24 + struct irq_domain *domain; 25 + 26 + domain = dev_get_msi_domain(&dev->dev); 27 + if (domain && irq_domain_is_hierarchy(domain)) 28 + msi_domain_free_irqs_descs_locked(domain, &dev->dev); 29 + else 30 + pci_msi_legacy_teardown_msi_irqs(dev); 31 + } 32 + 33 + /** 34 + * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space 35 + * @irq_data: Pointer to interrupt data of the MSI interrupt 36 + * @msg: Pointer to the message 37 + */ 38 + static void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg) 39 + { 40 + struct msi_desc *desc = irq_data_get_msi_desc(irq_data); 41 + 42 + /* 43 + * For MSI-X desc->irq is always equal to irq_data->irq. For 44 + * MSI only the first interrupt of MULTI MSI passes the test. 45 + */ 46 + if (desc->irq == irq_data->irq) 47 + __pci_write_msi_msg(desc, msg); 48 + } 49 + 50 + /** 51 + * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source 52 + * @desc: Pointer to the MSI descriptor 53 + * 54 + * The ID number is only used within the irqdomain. 55 + */ 56 + static irq_hw_number_t pci_msi_domain_calc_hwirq(struct msi_desc *desc) 57 + { 58 + struct pci_dev *dev = msi_desc_to_pci_dev(desc); 59 + 60 + return (irq_hw_number_t)desc->msi_index | 61 + pci_dev_id(dev) << 11 | 62 + (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27; 63 + } 64 + 65 + static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc) 66 + { 67 + return !desc->pci.msi_attrib.is_msix && desc->nvec_used > 1; 68 + } 69 + 70 + /** 71 + * pci_msi_domain_check_cap - Verify that @domain supports the capabilities 72 + * for @dev 73 + * @domain: The interrupt domain to check 74 + * @info: The domain info for verification 75 + * @dev: The device to check 76 + * 77 + * Returns: 78 + * 0 if the functionality is supported 79 + * 1 if Multi MSI is requested, but the domain does not support it 80 + * -ENOTSUPP otherwise 81 + */ 82 + static int pci_msi_domain_check_cap(struct irq_domain *domain, 83 + struct msi_domain_info *info, 84 + struct device *dev) 85 + { 86 + struct msi_desc *desc = msi_first_desc(dev, MSI_DESC_ALL); 87 + 88 + /* Special handling to support __pci_enable_msi_range() */ 89 + if (pci_msi_desc_is_multi_msi(desc) && 90 + !(info->flags & MSI_FLAG_MULTI_PCI_MSI)) 91 + return 1; 92 + 93 + if (desc->pci.msi_attrib.is_msix) { 94 + if (!(info->flags & MSI_FLAG_PCI_MSIX)) 95 + return -ENOTSUPP; 96 + 97 + if (info->flags & MSI_FLAG_MSIX_CONTIGUOUS) { 98 + unsigned int idx = 0; 99 + 100 + /* Check for gaps in the entry indices */ 101 + msi_for_each_desc(desc, dev, MSI_DESC_ALL) { 102 + if (desc->msi_index != idx++) 103 + return -ENOTSUPP; 104 + } 105 + } 106 + } 107 + return 0; 108 + } 109 + 110 + static void pci_msi_domain_set_desc(msi_alloc_info_t *arg, 111 + struct msi_desc *desc) 112 + { 113 + arg->desc = desc; 114 + arg->hwirq = pci_msi_domain_calc_hwirq(desc); 115 + } 116 + 117 + static struct msi_domain_ops pci_msi_domain_ops_default = { 118 + .set_desc = pci_msi_domain_set_desc, 119 + .msi_check = pci_msi_domain_check_cap, 120 + }; 121 + 122 + static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info) 123 + { 124 + struct msi_domain_ops *ops = info->ops; 125 + 126 + if (ops == NULL) { 127 + info->ops = &pci_msi_domain_ops_default; 128 + } else { 129 + if (ops->set_desc == NULL) 130 + ops->set_desc = pci_msi_domain_set_desc; 131 + if (ops->msi_check == NULL) 132 + ops->msi_check = pci_msi_domain_check_cap; 133 + } 134 + } 135 + 136 + static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info) 137 + { 138 + struct irq_chip *chip = info->chip; 139 + 140 + BUG_ON(!chip); 141 + if (!chip->irq_write_msi_msg) 142 + chip->irq_write_msi_msg = pci_msi_domain_write_msg; 143 + if (!chip->irq_mask) 144 + chip->irq_mask = pci_msi_mask_irq; 145 + if (!chip->irq_unmask) 146 + chip->irq_unmask = pci_msi_unmask_irq; 147 + } 148 + 149 + /** 150 + * pci_msi_create_irq_domain - Create a MSI interrupt domain 151 + * @fwnode: Optional fwnode of the interrupt controller 152 + * @info: MSI domain info 153 + * @parent: Parent irq domain 154 + * 155 + * Updates the domain and chip ops and creates a MSI interrupt domain. 156 + * 157 + * Returns: 158 + * A domain pointer or NULL in case of failure. 159 + */ 160 + struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, 161 + struct msi_domain_info *info, 162 + struct irq_domain *parent) 163 + { 164 + struct irq_domain *domain; 165 + 166 + if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE)) 167 + info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; 168 + 169 + if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS) 170 + pci_msi_domain_update_dom_ops(info); 171 + if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 172 + pci_msi_domain_update_chip_ops(info); 173 + 174 + info->flags |= MSI_FLAG_ACTIVATE_EARLY | MSI_FLAG_DEV_SYSFS | 175 + MSI_FLAG_FREE_MSI_DESCS; 176 + if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE)) 177 + info->flags |= MSI_FLAG_MUST_REACTIVATE; 178 + 179 + /* PCI-MSI is oneshot-safe */ 180 + info->chip->flags |= IRQCHIP_ONESHOT_SAFE; 181 + 182 + domain = msi_create_irq_domain(fwnode, info, parent); 183 + if (!domain) 184 + return NULL; 185 + 186 + irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI); 187 + return domain; 188 + } 189 + EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain); 190 + 191 + /* 192 + * Users of the generic MSI infrastructure expect a device to have a single ID, 193 + * so with DMA aliases we have to pick the least-worst compromise. Devices with 194 + * DMA phantom functions tend to still emit MSIs from the real function number, 195 + * so we ignore those and only consider topological aliases where either the 196 + * alias device or RID appears on a different bus number. We also make the 197 + * reasonable assumption that bridges are walked in an upstream direction (so 198 + * the last one seen wins), and the much braver assumption that the most likely 199 + * case is that of PCI->PCIe so we should always use the alias RID. This echoes 200 + * the logic from intel_irq_remapping's set_msi_sid(), which presumably works 201 + * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions 202 + * for taking ownership all we can really do is close our eyes and hope... 203 + */ 204 + static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data) 205 + { 206 + u32 *pa = data; 207 + u8 bus = PCI_BUS_NUM(*pa); 208 + 209 + if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus) 210 + *pa = alias; 211 + 212 + return 0; 213 + } 214 + 215 + /** 216 + * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID) 217 + * @domain: The interrupt domain 218 + * @pdev: The PCI device. 219 + * 220 + * The RID for a device is formed from the alias, with a firmware 221 + * supplied mapping applied 222 + * 223 + * Returns: The RID. 224 + */ 225 + u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev) 226 + { 227 + struct device_node *of_node; 228 + u32 rid = pci_dev_id(pdev); 229 + 230 + pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); 231 + 232 + of_node = irq_domain_get_of_node(domain); 233 + rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) : 234 + iort_msi_map_id(&pdev->dev, rid); 235 + 236 + return rid; 237 + } 238 + 239 + /** 240 + * pci_msi_get_device_domain - Get the MSI domain for a given PCI device 241 + * @pdev: The PCI device 242 + * 243 + * Use the firmware data to find a device-specific MSI domain 244 + * (i.e. not one that is set as a default). 245 + * 246 + * Returns: The corresponding MSI domain or NULL if none has been found. 247 + */ 248 + struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev) 249 + { 250 + struct irq_domain *dom; 251 + u32 rid = pci_dev_id(pdev); 252 + 253 + pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); 254 + dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI); 255 + if (!dom) 256 + dom = iort_get_device_domain(&pdev->dev, rid, 257 + DOMAIN_BUS_PCI_MSI); 258 + return dom; 259 + } 260 + 261 + /** 262 + * pci_dev_has_special_msi_domain - Check whether the device is handled by 263 + * a non-standard PCI-MSI domain 264 + * @pdev: The PCI device to check. 265 + * 266 + * Returns: True if the device irqdomain or the bus irqdomain is 267 + * non-standard PCI/MSI. 268 + */ 269 + bool pci_dev_has_special_msi_domain(struct pci_dev *pdev) 270 + { 271 + struct irq_domain *dom = dev_get_msi_domain(&pdev->dev); 272 + 273 + if (!dom) 274 + dom = dev_get_msi_domain(&pdev->bus->dev); 275 + 276 + if (!dom) 277 + return true; 278 + 279 + return dom->bus_token != DOMAIN_BUS_PCI_MSI; 280 + }
+81
drivers/pci/msi/legacy.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * PCI Message Signaled Interrupt (MSI). 4 + * 5 + * Legacy architecture specific setup and teardown mechanism. 6 + */ 7 + #include "msi.h" 8 + 9 + /* Arch hooks */ 10 + int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc) 11 + { 12 + return -EINVAL; 13 + } 14 + 15 + void __weak arch_teardown_msi_irq(unsigned int irq) 16 + { 17 + } 18 + 19 + int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 20 + { 21 + struct msi_desc *desc; 22 + int ret; 23 + 24 + /* 25 + * If an architecture wants to support multiple MSI, it needs to 26 + * override arch_setup_msi_irqs() 27 + */ 28 + if (type == PCI_CAP_ID_MSI && nvec > 1) 29 + return 1; 30 + 31 + msi_for_each_desc(desc, &dev->dev, MSI_DESC_NOTASSOCIATED) { 32 + ret = arch_setup_msi_irq(dev, desc); 33 + if (ret) 34 + return ret < 0 ? ret : -ENOSPC; 35 + } 36 + 37 + return 0; 38 + } 39 + 40 + void __weak arch_teardown_msi_irqs(struct pci_dev *dev) 41 + { 42 + struct msi_desc *desc; 43 + int i; 44 + 45 + msi_for_each_desc(desc, &dev->dev, MSI_DESC_ASSOCIATED) { 46 + for (i = 0; i < desc->nvec_used; i++) 47 + arch_teardown_msi_irq(desc->irq + i); 48 + } 49 + } 50 + 51 + static int pci_msi_setup_check_result(struct pci_dev *dev, int type, int ret) 52 + { 53 + struct msi_desc *desc; 54 + int avail = 0; 55 + 56 + if (type != PCI_CAP_ID_MSIX || ret >= 0) 57 + return ret; 58 + 59 + /* Scan the MSI descriptors for successfully allocated ones. */ 60 + msi_for_each_desc(desc, &dev->dev, MSI_DESC_ASSOCIATED) 61 + avail++; 62 + 63 + return avail ? avail : ret; 64 + } 65 + 66 + int pci_msi_legacy_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 67 + { 68 + int ret = arch_setup_msi_irqs(dev, nvec, type); 69 + 70 + ret = pci_msi_setup_check_result(dev, type, ret); 71 + if (!ret) 72 + ret = msi_device_populate_sysfs(&dev->dev); 73 + return ret; 74 + } 75 + 76 + void pci_msi_legacy_teardown_msi_irqs(struct pci_dev *dev) 77 + { 78 + msi_device_destroy_sysfs(&dev->dev); 79 + arch_teardown_msi_irqs(dev); 80 + msi_free_msi_descs(&dev->dev); 81 + }
+39
drivers/pci/msi/msi.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + #include <linux/pci.h> 4 + #include <linux/msi.h> 5 + 6 + #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) 7 + 8 + extern int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type); 9 + extern void pci_msi_teardown_msi_irqs(struct pci_dev *dev); 10 + 11 + #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS 12 + extern int pci_msi_legacy_setup_msi_irqs(struct pci_dev *dev, int nvec, int type); 13 + extern void pci_msi_legacy_teardown_msi_irqs(struct pci_dev *dev); 14 + #else 15 + static inline int pci_msi_legacy_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 16 + { 17 + WARN_ON_ONCE(1); 18 + return -ENODEV; 19 + } 20 + 21 + static inline void pci_msi_legacy_teardown_msi_irqs(struct pci_dev *dev) 22 + { 23 + WARN_ON_ONCE(1); 24 + } 25 + #endif 26 + 27 + /* 28 + * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to 29 + * mask all MSI interrupts by clearing the MSI enable bit does not work 30 + * reliably as devices without an INTx disable bit will then generate a 31 + * level IRQ which will never be cleared. 32 + */ 33 + static inline __attribute_const__ u32 msi_multi_mask(struct msi_desc *desc) 34 + { 35 + /* Don't shift by >= width of type */ 36 + if (desc->pci.msi_attrib.multi_cap >= 5) 37 + return 0xffffffff; 38 + return (1 << (1 << desc->pci.msi_attrib.multi_cap)) - 1; 39 + }
+43
drivers/pci/msi/pcidev_msi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * MSI[X} related functions which are available unconditionally. 4 + */ 5 + #include "../pci.h" 6 + 7 + /* 8 + * Disable the MSI[X] hardware to avoid screaming interrupts during boot. 9 + * This is the power on reset default so usually this should be a noop. 10 + */ 11 + 12 + void pci_msi_init(struct pci_dev *dev) 13 + { 14 + u16 ctrl; 15 + 16 + dev->msi_cap = pci_find_capability(dev, PCI_CAP_ID_MSI); 17 + if (!dev->msi_cap) 18 + return; 19 + 20 + pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &ctrl); 21 + if (ctrl & PCI_MSI_FLAGS_ENABLE) { 22 + pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, 23 + ctrl & ~PCI_MSI_FLAGS_ENABLE); 24 + } 25 + 26 + if (!(ctrl & PCI_MSI_FLAGS_64BIT)) 27 + dev->no_64bit_msi = 1; 28 + } 29 + 30 + void pci_msix_init(struct pci_dev *dev) 31 + { 32 + u16 ctrl; 33 + 34 + dev->msix_cap = pci_find_capability(dev, PCI_CAP_ID_MSIX); 35 + if (!dev->msix_cap) 36 + return; 37 + 38 + pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl); 39 + if (ctrl & PCI_MSIX_FLAGS_ENABLE) { 40 + pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, 41 + ctrl & ~PCI_MSIX_FLAGS_ENABLE); 42 + } 43 + }
+2 -5
drivers/pci/pci-sysfs.c
··· 62 62 * For MSI, show the first MSI IRQ; for all other cases including 63 63 * MSI-X, show the legacy INTx IRQ. 64 64 */ 65 - if (pdev->msi_enabled) { 66 - struct msi_desc *desc = first_pci_msi_entry(pdev); 67 - 68 - return sysfs_emit(buf, "%u\n", desc->irq); 69 - } 65 + if (pdev->msi_enabled) 66 + return sysfs_emit(buf, "%u\n", pci_irq_vector(pdev, 0)); 70 67 #endif 71 68 72 69 return sysfs_emit(buf, "%u\n", pdev->irq);
-5
drivers/pci/pci.c
··· 2024 2024 struct pci_devres *this = res; 2025 2025 int i; 2026 2026 2027 - if (dev->msi_enabled) 2028 - pci_disable_msi(dev); 2029 - if (dev->msix_enabled) 2030 - pci_disable_msix(dev); 2031 - 2032 2027 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) 2033 2028 if (this->region_mask & (1 << i)) 2034 2029 pci_release_region(dev, i);
+3 -1
drivers/pci/probe.c
··· 2311 2311 INIT_LIST_HEAD(&dev->bus_list); 2312 2312 dev->dev.type = &pci_dev_type; 2313 2313 dev->bus = pci_bus_get(bus); 2314 - 2314 + #ifdef CONFIG_PCI_MSI 2315 + raw_spin_lock_init(&dev->msi_lock); 2316 + #endif 2315 2317 return dev; 2316 2318 } 2317 2319 EXPORT_SYMBOL(pci_alloc_dev);
+2 -2
drivers/pci/xen-pcifront.c
··· 262 262 } 263 263 264 264 i = 0; 265 - for_each_pci_msi_entry(entry, dev) { 266 - op.msix_entries[i].entry = entry->msi_attrib.entry_nr; 265 + msi_for_each_desc(entry, &dev->dev, MSI_DESC_NOTASSOCIATED) { 266 + op.msix_entries[i].entry = entry->msi_index; 267 267 /* Vector is useless at this point. */ 268 268 op.msix_entries[i].vector = -1; 269 269 i++;
+1 -4
drivers/perf/arm_smmuv3_pmu.c
··· 703 703 704 704 static void smmu_pmu_setup_msi(struct smmu_pmu *pmu) 705 705 { 706 - struct msi_desc *desc; 707 706 struct device *dev = pmu->dev; 708 707 int ret; 709 708 ··· 719 720 return; 720 721 } 721 722 722 - desc = first_msi_entry(dev); 723 - if (desc) 724 - pmu->irq = desc->irq; 723 + pmu->irq = msi_get_virq(dev, 0); 725 724 726 725 /* Add callback to free MSIs on teardown */ 727 726 devm_add_action(dev, smmu_pmu_free_msis, dev);
+4 -4
drivers/soc/fsl/dpio/dpio-driver.c
··· 88 88 irq = dpio_dev->irqs[0]; 89 89 90 90 /* clear the affinity hint */ 91 - irq_set_affinity_hint(irq->msi_desc->irq, NULL); 91 + irq_set_affinity_hint(irq->virq, NULL); 92 92 } 93 93 94 94 static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu) ··· 98 98 99 99 irq = dpio_dev->irqs[0]; 100 100 error = devm_request_irq(&dpio_dev->dev, 101 - irq->msi_desc->irq, 101 + irq->virq, 102 102 dpio_irq_handler, 103 103 0, 104 104 dev_name(&dpio_dev->dev), ··· 111 111 } 112 112 113 113 /* set the affinity hint */ 114 - if (irq_set_affinity_hint(irq->msi_desc->irq, cpumask_of(cpu))) 114 + if (irq_set_affinity_hint(irq->virq, cpumask_of(cpu))) 115 115 dev_err(&dpio_dev->dev, 116 116 "irq_set_affinity failed irq %d cpu %d\n", 117 - irq->msi_desc->irq, cpu); 117 + irq->virq, cpu); 118 118 119 119 return 0; 120 120 }
+3 -3
drivers/soc/ti/k3-ringacc.c
··· 647 647 if (!ring) 648 648 return -EINVAL; 649 649 650 - irq_num = ti_sci_inta_msi_get_virq(ring->parent->dev, ring->ring_id); 650 + irq_num = msi_get_virq(ring->parent->dev, ring->ring_id); 651 651 if (irq_num <= 0) 652 652 irq_num = -EINVAL; 653 653 return irq_num; ··· 1356 1356 struct resource *res; 1357 1357 int ret, i; 1358 1358 1359 - dev->msi_domain = of_msi_get_domain(dev, dev->of_node, 1359 + dev->msi.domain = of_msi_get_domain(dev, dev->of_node, 1360 1360 DOMAIN_BUS_TI_SCI_INTA_MSI); 1361 - if (!dev->msi_domain) { 1361 + if (!dev->msi.domain) { 1362 1362 dev_err(dev, "Failed to get MSI domain\n"); 1363 1363 return -EPROBE_DEFER; 1364 1364 }
+29 -63
drivers/soc/ti/ti_sci_inta_msi.c
··· 51 51 struct irq_domain *domain; 52 52 53 53 ti_sci_inta_msi_update_chip_ops(info); 54 + info->flags |= MSI_FLAG_FREE_MSI_DESCS; 54 55 55 56 domain = msi_create_irq_domain(fwnode, info, parent); 56 57 if (domain) ··· 61 60 } 62 61 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_create_irq_domain); 63 62 64 - static void ti_sci_inta_msi_free_descs(struct device *dev) 65 - { 66 - struct msi_desc *desc, *tmp; 67 - 68 - list_for_each_entry_safe(desc, tmp, dev_to_msi_list(dev), list) { 69 - list_del(&desc->list); 70 - free_msi_entry(desc); 71 - } 72 - } 73 - 74 63 static int ti_sci_inta_msi_alloc_descs(struct device *dev, 75 64 struct ti_sci_resource *res) 76 65 { 77 - struct msi_desc *msi_desc; 66 + struct msi_desc msi_desc; 78 67 int set, i, count = 0; 79 68 69 + memset(&msi_desc, 0, sizeof(msi_desc)); 70 + msi_desc.nvec_used = 1; 71 + 80 72 for (set = 0; set < res->sets; set++) { 81 - for (i = 0; i < res->desc[set].num; i++) { 82 - msi_desc = alloc_msi_entry(dev, 1, NULL); 83 - if (!msi_desc) { 84 - ti_sci_inta_msi_free_descs(dev); 85 - return -ENOMEM; 86 - } 87 - 88 - msi_desc->inta.dev_index = res->desc[set].start + i; 89 - INIT_LIST_HEAD(&msi_desc->list); 90 - list_add_tail(&msi_desc->list, dev_to_msi_list(dev)); 91 - count++; 73 + for (i = 0; i < res->desc[set].num; i++, count++) { 74 + msi_desc.msi_index = res->desc[set].start + i; 75 + if (msi_add_msi_desc(dev, &msi_desc)) 76 + goto fail; 92 77 } 93 - for (i = 0; i < res->desc[set].num_sec; i++) { 94 - msi_desc = alloc_msi_entry(dev, 1, NULL); 95 - if (!msi_desc) { 96 - ti_sci_inta_msi_free_descs(dev); 97 - return -ENOMEM; 98 - } 99 78 100 - msi_desc->inta.dev_index = res->desc[set].start_sec + i; 101 - INIT_LIST_HEAD(&msi_desc->list); 102 - list_add_tail(&msi_desc->list, dev_to_msi_list(dev)); 103 - count++; 79 + for (i = 0; i < res->desc[set].num_sec; i++, count++) { 80 + msi_desc.msi_index = res->desc[set].start_sec + i; 81 + if (msi_add_msi_desc(dev, &msi_desc)) 82 + goto fail; 104 83 } 105 84 } 106 - 107 85 return count; 86 + fail: 87 + msi_free_msi_descs(dev); 88 + return -ENOMEM; 108 89 } 109 90 110 91 int ti_sci_inta_msi_domain_alloc_irqs(struct device *dev, ··· 103 120 if (pdev->id < 0) 104 121 return -ENODEV; 105 122 106 - nvec = ti_sci_inta_msi_alloc_descs(dev, res); 107 - if (nvec <= 0) 108 - return nvec; 123 + ret = msi_setup_device_data(dev); 124 + if (ret) 125 + return ret; 109 126 110 - ret = msi_domain_alloc_irqs(msi_domain, dev, nvec); 111 - if (ret) { 112 - dev_err(dev, "Failed to allocate IRQs %d\n", ret); 113 - goto cleanup; 127 + msi_lock_descs(dev); 128 + nvec = ti_sci_inta_msi_alloc_descs(dev, res); 129 + if (nvec <= 0) { 130 + ret = nvec; 131 + goto unlock; 114 132 } 115 133 116 - return 0; 117 - 118 - cleanup: 119 - ti_sci_inta_msi_free_descs(&pdev->dev); 134 + ret = msi_domain_alloc_irqs_descs_locked(msi_domain, dev, nvec); 135 + if (ret) 136 + dev_err(dev, "Failed to allocate IRQs %d\n", ret); 137 + unlock: 138 + msi_unlock_descs(dev); 120 139 return ret; 121 140 } 122 141 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs); 123 - 124 - void ti_sci_inta_msi_domain_free_irqs(struct device *dev) 125 - { 126 - msi_domain_free_irqs(dev->msi_domain, dev); 127 - ti_sci_inta_msi_free_descs(dev); 128 - } 129 - EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_free_irqs); 130 - 131 - unsigned int ti_sci_inta_msi_get_virq(struct device *dev, u32 dev_index) 132 - { 133 - struct msi_desc *desc; 134 - 135 - for_each_msi_entry(desc, dev) 136 - if (desc->inta.dev_index == dev_index) 137 - return desc->irq; 138 - 139 - return -ENODEV; 140 - } 141 - EXPORT_SYMBOL_GPL(ti_sci_inta_msi_get_virq);
+2 -2
drivers/vfio/fsl-mc/vfio_fsl_mc_intr.c
··· 67 67 int hwirq; 68 68 int ret; 69 69 70 - hwirq = vdev->mc_dev->irqs[index]->msi_desc->irq; 70 + hwirq = vdev->mc_dev->irqs[index]->virq; 71 71 if (irq->trigger) { 72 72 free_irq(hwirq, irq); 73 73 kfree(irq->name); ··· 137 137 return vfio_set_trigger(vdev, index, fd); 138 138 } 139 139 140 - hwirq = vdev->mc_dev->irqs[index]->msi_desc->irq; 140 + hwirq = vdev->mc_dev->irqs[index]->virq; 141 141 142 142 irq = &vdev->mc_irqs[index]; 143 143
+19 -12
include/linux/device.h
··· 45 45 struct iommu_group; 46 46 struct dev_pin_info; 47 47 struct dev_iommu; 48 + struct msi_device_data; 48 49 49 50 /** 50 51 * struct subsys_interface - interfaces to device functions ··· 373 372 }; 374 373 375 374 /** 375 + * struct dev_msi_info - Device data related to MSI 376 + * @domain: The MSI interrupt domain associated to the device 377 + * @data: Pointer to MSI device data 378 + */ 379 + struct dev_msi_info { 380 + #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 381 + struct irq_domain *domain; 382 + #endif 383 + #ifdef CONFIG_GENERIC_MSI_IRQ 384 + struct msi_device_data *data; 385 + #endif 386 + }; 387 + 388 + /** 376 389 * struct device - The basic device structure 377 390 * @parent: The device's "parent" device, the device to which it is attached. 378 391 * In most cases, a parent device is some sort of bus or host ··· 422 407 * @em_pd: device's energy model performance domain 423 408 * @pins: For device pin management. 424 409 * See Documentation/driver-api/pin-control.rst for details. 425 - * @msi_lock: Lock to protect MSI mask cache and mask register 426 - * @msi_list: Hosts MSI descriptors 427 - * @msi_domain: The generic MSI domain this device is using. 410 + * @msi: MSI related data 428 411 * @numa_node: NUMA node this device is close to. 429 412 * @dma_ops: DMA mapping operations for this device. 430 413 * @dma_mask: Dma mask (if dma'ble device). ··· 514 501 struct em_perf_domain *em_pd; 515 502 #endif 516 503 517 - #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 518 - struct irq_domain *msi_domain; 519 - #endif 520 504 #ifdef CONFIG_PINCTRL 521 505 struct dev_pin_info *pins; 522 506 #endif 523 - #ifdef CONFIG_GENERIC_MSI_IRQ 524 - raw_spinlock_t msi_lock; 525 - struct list_head msi_list; 526 - #endif 507 + struct dev_msi_info msi; 527 508 #ifdef CONFIG_DMA_OPS 528 509 const struct dma_map_ops *dma_ops; 529 510 #endif ··· 675 668 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev) 676 669 { 677 670 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 678 - return dev->msi_domain; 671 + return dev->msi.domain; 679 672 #else 680 673 return NULL; 681 674 #endif ··· 684 677 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d) 685 678 { 686 679 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 687 - dev->msi_domain = d; 680 + dev->msi.domain = d; 688 681 #endif 689 682 } 690 683
+2 -2
include/linux/fsl/mc.h
··· 91 91 92 92 /** 93 93 * struct fsl_mc_device_irq - MC object device message-based interrupt 94 - * @msi_desc: pointer to MSI descriptor allocated by fsl_mc_msi_alloc_descs() 94 + * @virq: Linux virtual interrupt number 95 95 * @mc_dev: MC object device that owns this interrupt 96 96 * @dev_irq_index: device-relative IRQ index 97 97 * @resource: MC generic resource associated with the interrupt 98 98 */ 99 99 struct fsl_mc_device_irq { 100 - struct msi_desc *msi_desc; 100 + unsigned int virq; 101 101 struct fsl_mc_device *mc_dev; 102 102 u8 dev_irq_index; 103 103 struct fsl_mc_resource resource;
+160 -141
include/linux/msi.h
··· 2 2 #ifndef LINUX_MSI_H 3 3 #define LINUX_MSI_H 4 4 5 - #include <linux/kobject.h> 5 + /* 6 + * This header file contains MSI data structures and functions which are 7 + * only relevant for: 8 + * - Interrupt core code 9 + * - PCI/MSI core code 10 + * - MSI interrupt domain implementations 11 + * - IOMMU, low level VFIO, NTB and other justified exceptions 12 + * dealing with low level MSI details. 13 + * 14 + * Regular device drivers have no business with any of these functions and 15 + * especially storing MSI descriptor pointers in random code is considered 16 + * abuse. The only function which is relevant for drivers is msi_get_virq(). 17 + */ 18 + 19 + #include <linux/cpumask.h> 20 + #include <linux/xarray.h> 21 + #include <linux/mutex.h> 6 22 #include <linux/list.h> 7 23 #include <asm/msi.h> 8 24 ··· 72 56 struct msi_desc; 73 57 struct pci_dev; 74 58 struct platform_msi_priv_data; 59 + struct device_attribute; 60 + 75 61 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg); 76 62 #ifdef CONFIG_GENERIC_MSI_IRQ 77 63 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg); ··· 87 69 struct msi_msg *msg); 88 70 89 71 /** 90 - * platform_msi_desc - Platform device specific msi descriptor data 91 - * @msi_priv_data: Pointer to platform private data 92 - * @msi_index: The index of the MSI descriptor for multi MSI 93 - */ 94 - struct platform_msi_desc { 95 - struct platform_msi_priv_data *msi_priv_data; 96 - u16 msi_index; 97 - }; 98 - 99 - /** 100 - * fsl_mc_msi_desc - FSL-MC device specific msi descriptor data 101 - * @msi_index: The index of the MSI descriptor 102 - */ 103 - struct fsl_mc_msi_desc { 104 - u16 msi_index; 105 - }; 106 - 107 - /** 108 - * ti_sci_inta_msi_desc - TISCI based INTA specific msi descriptor data 109 - * @dev_index: TISCI device index 110 - */ 111 - struct ti_sci_inta_msi_desc { 112 - u16 dev_index; 113 - }; 114 - 115 - /** 116 - * struct msi_desc - Descriptor structure for MSI based interrupts 117 - * @list: List head for management 118 - * @irq: The base interrupt number 119 - * @nvec_used: The number of vectors used 120 - * @dev: Pointer to the device which uses this descriptor 121 - * @msg: The last set MSI message cached for reuse 122 - * @affinity: Optional pointer to a cpu affinity mask for this descriptor 123 - * 124 - * @write_msi_msg: Callback that may be called when the MSI message 125 - * address or data changes 126 - * @write_msi_msg_data: Data parameter for the callback. 72 + * pci_msi_desc - PCI/MSI specific MSI descriptor data 127 73 * 128 74 * @msi_mask: [PCI MSI] MSI cached mask bits 129 75 * @msix_ctrl: [PCI MSI-X] MSI-X cached per vector control bits 130 76 * @is_msix: [PCI MSI/X] True if MSI-X 131 77 * @multiple: [PCI MSI/X] log2 num of messages allocated 132 78 * @multi_cap: [PCI MSI/X] log2 num of messages supported 133 - * @maskbit: [PCI MSI/X] Mask-Pending bit supported? 79 + * @can_mask: [PCI MSI/X] Masking supported? 134 80 * @is_64: [PCI MSI/X] Address size: 0=32bit 1=64bit 135 - * @entry_nr: [PCI MSI/X] Entry which is described by this descriptor 136 81 * @default_irq:[PCI MSI/X] The default pre-assigned non-MSI irq 137 82 * @mask_pos: [PCI MSI] Mask register position 138 83 * @mask_base: [PCI MSI-X] Mask register base address 139 - * @platform: [platform] Platform device specific msi descriptor data 140 - * @fsl_mc: [fsl-mc] FSL MC device specific msi descriptor data 141 - * @inta: [INTA] TISCI based INTA specific msi descriptor data 84 + */ 85 + struct pci_msi_desc { 86 + union { 87 + u32 msi_mask; 88 + u32 msix_ctrl; 89 + }; 90 + struct { 91 + u8 is_msix : 1; 92 + u8 multiple : 3; 93 + u8 multi_cap : 3; 94 + u8 can_mask : 1; 95 + u8 is_64 : 1; 96 + u8 is_virtual : 1; 97 + unsigned default_irq; 98 + } msi_attrib; 99 + union { 100 + u8 mask_pos; 101 + void __iomem *mask_base; 102 + }; 103 + }; 104 + 105 + #define MSI_MAX_INDEX ((unsigned int)USHRT_MAX) 106 + 107 + /** 108 + * struct msi_desc - Descriptor structure for MSI based interrupts 109 + * @irq: The base interrupt number 110 + * @nvec_used: The number of vectors used 111 + * @dev: Pointer to the device which uses this descriptor 112 + * @msg: The last set MSI message cached for reuse 113 + * @affinity: Optional pointer to a cpu affinity mask for this descriptor 114 + * @sysfs_attr: Pointer to sysfs device attribute 115 + * 116 + * @write_msi_msg: Callback that may be called when the MSI message 117 + * address or data changes 118 + * @write_msi_msg_data: Data parameter for the callback. 119 + * 120 + * @msi_index: Index of the msi descriptor 121 + * @pci: PCI specific msi descriptor data 142 122 */ 143 123 struct msi_desc { 144 124 /* Shared device/bus type independent data */ 145 - struct list_head list; 146 125 unsigned int irq; 147 126 unsigned int nvec_used; 148 127 struct device *dev; ··· 148 133 #ifdef CONFIG_IRQ_MSI_IOMMU 149 134 const void *iommu_cookie; 150 135 #endif 136 + #ifdef CONFIG_SYSFS 137 + struct device_attribute *sysfs_attrs; 138 + #endif 151 139 152 140 void (*write_msi_msg)(struct msi_desc *entry, void *data); 153 141 void *write_msi_msg_data; 154 142 155 - union { 156 - /* PCI MSI/X specific data */ 157 - struct { 158 - union { 159 - u32 msi_mask; 160 - u32 msix_ctrl; 161 - }; 162 - struct { 163 - u8 is_msix : 1; 164 - u8 multiple : 3; 165 - u8 multi_cap : 3; 166 - u8 can_mask : 1; 167 - u8 is_64 : 1; 168 - u8 is_virtual : 1; 169 - u16 entry_nr; 170 - unsigned default_irq; 171 - } msi_attrib; 172 - union { 173 - u8 mask_pos; 174 - void __iomem *mask_base; 175 - }; 176 - }; 177 - 178 - /* 179 - * Non PCI variants add their data structure here. New 180 - * entries need to use a named structure. We want 181 - * proper name spaces for this. The PCI part is 182 - * anonymous for now as it would require an immediate 183 - * tree wide cleanup. 184 - */ 185 - struct platform_msi_desc platform; 186 - struct fsl_mc_msi_desc fsl_mc; 187 - struct ti_sci_inta_msi_desc inta; 188 - }; 143 + u16 msi_index; 144 + struct pci_msi_desc pci; 189 145 }; 190 146 191 - /* Helpers to hide struct msi_desc implementation details */ 147 + /* 148 + * Filter values for the MSI descriptor iterators and accessor functions. 149 + */ 150 + enum msi_desc_filter { 151 + /* All descriptors */ 152 + MSI_DESC_ALL, 153 + /* Descriptors which have no interrupt associated */ 154 + MSI_DESC_NOTASSOCIATED, 155 + /* Descriptors which have an interrupt associated */ 156 + MSI_DESC_ASSOCIATED, 157 + }; 158 + 159 + /** 160 + * msi_device_data - MSI per device data 161 + * @properties: MSI properties which are interesting to drivers 162 + * @platform_data: Platform-MSI specific data 163 + * @mutex: Mutex protecting the MSI descriptor store 164 + * @__store: Xarray for storing MSI descriptor pointers 165 + * @__iter_idx: Index to search the next entry for iterators 166 + */ 167 + struct msi_device_data { 168 + unsigned long properties; 169 + struct platform_msi_priv_data *platform_data; 170 + struct mutex mutex; 171 + struct xarray __store; 172 + unsigned long __iter_idx; 173 + }; 174 + 175 + int msi_setup_device_data(struct device *dev); 176 + 177 + unsigned int msi_get_virq(struct device *dev, unsigned int index); 178 + void msi_lock_descs(struct device *dev); 179 + void msi_unlock_descs(struct device *dev); 180 + 181 + struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter); 182 + struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter); 183 + 184 + /** 185 + * msi_for_each_desc - Iterate the MSI descriptors 186 + * 187 + * @desc: struct msi_desc pointer used as iterator 188 + * @dev: struct device pointer - device to iterate 189 + * @filter: Filter for descriptor selection 190 + * 191 + * Notes: 192 + * - The loop must be protected with a msi_lock_descs()/msi_unlock_descs() 193 + * pair. 194 + * - It is safe to remove a retrieved MSI descriptor in the loop. 195 + */ 196 + #define msi_for_each_desc(desc, dev, filter) \ 197 + for ((desc) = msi_first_desc((dev), (filter)); (desc); \ 198 + (desc) = msi_next_desc((dev), (filter))) 199 + 192 200 #define msi_desc_to_dev(desc) ((desc)->dev) 193 - #define dev_to_msi_list(dev) (&(dev)->msi_list) 194 - #define first_msi_entry(dev) \ 195 - list_first_entry(dev_to_msi_list((dev)), struct msi_desc, list) 196 - #define for_each_msi_entry(desc, dev) \ 197 - list_for_each_entry((desc), dev_to_msi_list((dev)), list) 198 - #define for_each_msi_entry_safe(desc, tmp, dev) \ 199 - list_for_each_entry_safe((desc), (tmp), dev_to_msi_list((dev)), list) 200 - #define for_each_msi_vector(desc, __irq, dev) \ 201 - for_each_msi_entry((desc), (dev)) \ 202 - if ((desc)->irq) \ 203 - for (__irq = (desc)->irq; \ 204 - __irq < ((desc)->irq + (desc)->nvec_used); \ 205 - __irq++) 206 201 207 202 #ifdef CONFIG_IRQ_MSI_IOMMU 208 203 static inline const void *msi_desc_get_iommu_cookie(struct msi_desc *desc) ··· 238 213 #endif 239 214 240 215 #ifdef CONFIG_PCI_MSI 241 - #define first_pci_msi_entry(pdev) first_msi_entry(&(pdev)->dev) 242 - #define for_each_pci_msi_entry(desc, pdev) \ 243 - for_each_msi_entry((desc), &(pdev)->dev) 244 - 245 216 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc); 246 - void *msi_desc_to_pci_sysdata(struct msi_desc *desc); 247 217 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg); 248 218 #else /* CONFIG_PCI_MSI */ 249 - static inline void *msi_desc_to_pci_sysdata(struct msi_desc *desc) 250 - { 251 - return NULL; 252 - } 253 219 static inline void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) 254 220 { 255 221 } 256 222 #endif /* CONFIG_PCI_MSI */ 257 223 258 - struct msi_desc *alloc_msi_entry(struct device *dev, int nvec, 259 - const struct irq_affinity_desc *affinity); 260 - void free_msi_entry(struct msi_desc *entry); 224 + int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc); 225 + void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter, 226 + unsigned int first_index, unsigned int last_index); 227 + 228 + /** 229 + * msi_free_msi_descs - Free MSI descriptors of a device 230 + * @dev: Device to free the descriptors 231 + */ 232 + static inline void msi_free_msi_descs(struct device *dev) 233 + { 234 + msi_free_msi_descs_range(dev, MSI_DESC_ALL, 0, MSI_MAX_INDEX); 235 + } 236 + 261 237 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg); 262 238 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg); 263 239 264 240 void pci_msi_mask_irq(struct irq_data *data); 265 241 void pci_msi_unmask_irq(struct irq_data *data); 266 - 267 - const struct attribute_group **msi_populate_sysfs(struct device *dev); 268 - void msi_destroy_sysfs(struct device *dev, 269 - const struct attribute_group **msi_irq_groups); 270 242 271 243 /* 272 244 * The arch hooks to setup up msi irqs. Default functions are implemented ··· 278 256 void arch_teardown_msi_irq(unsigned int irq); 279 257 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type); 280 258 void arch_teardown_msi_irqs(struct pci_dev *dev); 281 - #else 282 - static inline int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) 283 - { 284 - WARN_ON_ONCE(1); 285 - return -ENODEV; 286 - } 287 - 288 - static inline void arch_teardown_msi_irqs(struct pci_dev *dev) 289 - { 290 - WARN_ON_ONCE(1); 291 - } 292 - #endif 259 + #ifdef CONFIG_SYSFS 260 + int msi_device_populate_sysfs(struct device *dev); 261 + void msi_device_destroy_sysfs(struct device *dev); 262 + #else /* CONFIG_SYSFS */ 263 + static inline int msi_device_populate_sysfs(struct device *dev) { return 0; } 264 + static inline void msi_device_destroy_sysfs(struct device *dev) { } 265 + #endif /* !CONFIG_SYSFS */ 266 + #endif /* CONFIG_PCI_MSI_ARCH_FALLBACKS */ 293 267 294 268 /* 295 - * The restore hooks are still available as they are useful even 296 - * for fully irq domain based setups. Courtesy to XEN/X86. 269 + * The restore hook is still available even for fully irq domain based 270 + * setups. Courtesy to XEN/X86. 297 271 */ 298 - void arch_restore_msi_irqs(struct pci_dev *dev); 299 - void default_restore_msi_irqs(struct pci_dev *dev); 272 + bool arch_restore_msi_irqs(struct pci_dev *dev); 300 273 301 274 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 302 275 ··· 311 294 * @msi_free: Domain specific function to free a MSI interrupts 312 295 * @msi_check: Callback for verification of the domain/info/dev data 313 296 * @msi_prepare: Prepare the allocation of the interrupts in the domain 314 - * @msi_finish: Optional callback to finalize the allocation 315 297 * @set_desc: Set the msi descriptor for an interrupt 316 - * @handle_error: Optional error handler if the allocation fails 317 298 * @domain_alloc_irqs: Optional function to override the default allocation 318 299 * function. 319 300 * @domain_free_irqs: Optional function to override the default free 320 301 * function. 321 302 * 322 - * @get_hwirq, @msi_init and @msi_free are callbacks used by 323 - * msi_create_irq_domain() and related interfaces 303 + * @get_hwirq, @msi_init and @msi_free are callbacks used by the underlying 304 + * irqdomain. 324 305 * 325 - * @msi_check, @msi_prepare, @msi_finish, @set_desc and @handle_error 326 - * are callbacks used by msi_domain_alloc_irqs() and related 327 - * interfaces which are based on msi_desc. 306 + * @msi_check, @msi_prepare and @set_desc are callbacks used by 307 + * msi_domain_alloc/free_irqs(). 328 308 * 329 309 * @domain_alloc_irqs, @domain_free_irqs can be used to override the 330 310 * default allocation/free functions (__msi_domain_alloc/free_irqs). This ··· 355 341 int (*msi_prepare)(struct irq_domain *domain, 356 342 struct device *dev, int nvec, 357 343 msi_alloc_info_t *arg); 358 - void (*msi_finish)(msi_alloc_info_t *arg, int retval); 359 344 void (*set_desc)(msi_alloc_info_t *arg, 360 345 struct msi_desc *desc); 361 - int (*handle_error)(struct irq_domain *domain, 362 - struct msi_desc *desc, int error); 363 346 int (*domain_alloc_irqs)(struct irq_domain *domain, 364 347 struct device *dev, int nvec); 365 348 void (*domain_free_irqs)(struct irq_domain *domain, ··· 410 399 MSI_FLAG_MUST_REACTIVATE = (1 << 5), 411 400 /* Is level-triggered capable, using two messages */ 412 401 MSI_FLAG_LEVEL_CAPABLE = (1 << 6), 402 + /* Populate sysfs on alloc() and destroy it on free() */ 403 + MSI_FLAG_DEV_SYSFS = (1 << 7), 404 + /* MSI-X entries must be contiguous */ 405 + MSI_FLAG_MSIX_CONTIGUOUS = (1 << 8), 406 + /* Allocate simple MSI descriptors */ 407 + MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS = (1 << 9), 408 + /* Free MSI descriptors */ 409 + MSI_FLAG_FREE_MSI_DESCS = (1 << 10), 413 410 }; 414 411 415 412 int msi_domain_set_affinity(struct irq_data *data, const struct cpumask *mask, ··· 428 409 struct irq_domain *parent); 429 410 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, 430 411 int nvec); 412 + int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev, 413 + int nvec); 431 414 int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, 432 415 int nvec); 433 416 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev); 417 + void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev); 434 418 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev); 435 419 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain); 436 420 ··· 462 440 #define platform_msi_create_device_tree_domain(dev, nvec, write, ops, data) \ 463 441 __platform_msi_create_device_domain(dev, nvec, true, write, ops, data) 464 442 465 - int platform_msi_domain_alloc(struct irq_domain *domain, unsigned int virq, 466 - unsigned int nr_irqs); 467 - void platform_msi_domain_free(struct irq_domain *domain, unsigned int virq, 468 - unsigned int nvec); 443 + int platform_msi_device_domain_alloc(struct irq_domain *domain, unsigned int virq, 444 + unsigned int nr_irqs); 445 + void platform_msi_device_domain_free(struct irq_domain *domain, unsigned int virq, 446 + unsigned int nvec); 469 447 void *platform_msi_get_host_data(struct irq_domain *domain); 470 448 #endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */ 471 449 472 450 #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN 473 - void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg); 474 451 struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, 475 452 struct msi_domain_info *info, 476 453 struct irq_domain *parent); 477 - int pci_msi_domain_check_cap(struct irq_domain *domain, 478 - struct msi_domain_info *info, struct device *dev); 479 454 u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev); 480 455 struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev); 481 456 bool pci_dev_has_special_msi_domain(struct pci_dev *pdev);
+4 -2
include/linux/pci.h
··· 425 425 unsigned int ats_enabled:1; /* Address Translation Svc */ 426 426 unsigned int pasid_enabled:1; /* Process Address Space ID */ 427 427 unsigned int pri_enabled:1; /* Page Request Interface */ 428 - unsigned int is_managed:1; 428 + unsigned int is_managed:1; /* Managed via devres */ 429 + unsigned int is_msi_managed:1; /* MSI release via devres installed */ 429 430 unsigned int needs_freset:1; /* Requires fundamental reset */ 430 431 unsigned int state_saved:1; 431 432 unsigned int is_physfn:1; ··· 474 473 u8 ptm_granularity; 475 474 #endif 476 475 #ifdef CONFIG_PCI_MSI 477 - const struct attribute_group **msi_irq_groups; 476 + void __iomem *msix_base; 477 + raw_spinlock_t msi_lock; 478 478 #endif 479 479 struct pci_vpd vpd; 480 480 #ifdef CONFIG_PCIE_DPC
-2
include/linux/soc/ti/ti_sci_inta_msi.h
··· 18 18 struct irq_domain *parent); 19 19 int ti_sci_inta_msi_domain_alloc_irqs(struct device *dev, 20 20 struct ti_sci_resource *res); 21 - unsigned int ti_sci_inta_msi_get_virq(struct device *dev, u32 index); 22 - void ti_sci_inta_msi_domain_free_irqs(struct device *dev); 23 21 #endif /* __INCLUDE_LINUX_IRQCHIP_TI_SCI_INTA_H */
+574 -228
kernel/irq/msi.c
··· 14 14 #include <linux/irqdomain.h> 15 15 #include <linux/msi.h> 16 16 #include <linux/slab.h> 17 + #include <linux/sysfs.h> 17 18 #include <linux/pci.h> 18 19 19 20 #include "internals.h" 20 21 22 + static inline int msi_sysfs_create_group(struct device *dev); 23 + 21 24 /** 22 - * alloc_msi_entry - Allocate an initialized msi_desc 25 + * msi_alloc_desc - Allocate an initialized msi_desc 23 26 * @dev: Pointer to the device for which this is allocated 24 27 * @nvec: The number of vectors used in this entry 25 28 * @affinity: Optional pointer to an affinity mask array size of @nvec ··· 32 29 * 33 30 * Return: pointer to allocated &msi_desc on success or %NULL on failure 34 31 */ 35 - struct msi_desc *alloc_msi_entry(struct device *dev, int nvec, 36 - const struct irq_affinity_desc *affinity) 32 + static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec, 33 + const struct irq_affinity_desc *affinity) 37 34 { 38 - struct msi_desc *desc; 35 + struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL); 39 36 40 - desc = kzalloc(sizeof(*desc), GFP_KERNEL); 41 37 if (!desc) 42 38 return NULL; 43 39 44 - INIT_LIST_HEAD(&desc->list); 45 40 desc->dev = dev; 46 41 desc->nvec_used = nvec; 47 42 if (affinity) { 48 - desc->affinity = kmemdup(affinity, 49 - nvec * sizeof(*desc->affinity), GFP_KERNEL); 43 + desc->affinity = kmemdup(affinity, nvec * sizeof(*desc->affinity), GFP_KERNEL); 50 44 if (!desc->affinity) { 51 45 kfree(desc); 52 46 return NULL; 53 47 } 54 48 } 55 - 56 49 return desc; 57 50 } 58 51 59 - void free_msi_entry(struct msi_desc *entry) 52 + static void msi_free_desc(struct msi_desc *desc) 60 53 { 61 - kfree(entry->affinity); 62 - kfree(entry); 54 + kfree(desc->affinity); 55 + kfree(desc); 56 + } 57 + 58 + static int msi_insert_desc(struct msi_device_data *md, struct msi_desc *desc, unsigned int index) 59 + { 60 + int ret; 61 + 62 + desc->msi_index = index; 63 + ret = xa_insert(&md->__store, index, desc, GFP_KERNEL); 64 + if (ret) 65 + msi_free_desc(desc); 66 + return ret; 67 + } 68 + 69 + /** 70 + * msi_add_msi_desc - Allocate and initialize a MSI descriptor 71 + * @dev: Pointer to the device for which the descriptor is allocated 72 + * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor 73 + * 74 + * Return: 0 on success or an appropriate failure code. 75 + */ 76 + int msi_add_msi_desc(struct device *dev, struct msi_desc *init_desc) 77 + { 78 + struct msi_desc *desc; 79 + 80 + lockdep_assert_held(&dev->msi.data->mutex); 81 + 82 + desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity); 83 + if (!desc) 84 + return -ENOMEM; 85 + 86 + /* Copy type specific data to the new descriptor. */ 87 + desc->pci = init_desc->pci; 88 + return msi_insert_desc(dev->msi.data, desc, init_desc->msi_index); 89 + } 90 + 91 + /** 92 + * msi_add_simple_msi_descs - Allocate and initialize MSI descriptors 93 + * @dev: Pointer to the device for which the descriptors are allocated 94 + * @index: Index for the first MSI descriptor 95 + * @ndesc: Number of descriptors to allocate 96 + * 97 + * Return: 0 on success or an appropriate failure code. 98 + */ 99 + static int msi_add_simple_msi_descs(struct device *dev, unsigned int index, unsigned int ndesc) 100 + { 101 + unsigned int idx, last = index + ndesc - 1; 102 + struct msi_desc *desc; 103 + int ret; 104 + 105 + lockdep_assert_held(&dev->msi.data->mutex); 106 + 107 + for (idx = index; idx <= last; idx++) { 108 + desc = msi_alloc_desc(dev, 1, NULL); 109 + if (!desc) 110 + goto fail_mem; 111 + ret = msi_insert_desc(dev->msi.data, desc, idx); 112 + if (ret) 113 + goto fail; 114 + } 115 + return 0; 116 + 117 + fail_mem: 118 + ret = -ENOMEM; 119 + fail: 120 + msi_free_msi_descs_range(dev, MSI_DESC_NOTASSOCIATED, index, last); 121 + return ret; 122 + } 123 + 124 + static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter) 125 + { 126 + switch (filter) { 127 + case MSI_DESC_ALL: 128 + return true; 129 + case MSI_DESC_NOTASSOCIATED: 130 + return !desc->irq; 131 + case MSI_DESC_ASSOCIATED: 132 + return !!desc->irq; 133 + } 134 + WARN_ON_ONCE(1); 135 + return false; 136 + } 137 + 138 + /** 139 + * msi_free_msi_descs_range - Free MSI descriptors of a device 140 + * @dev: Device to free the descriptors 141 + * @filter: Descriptor state filter 142 + * @first_index: Index to start freeing from 143 + * @last_index: Last index to be freed 144 + */ 145 + void msi_free_msi_descs_range(struct device *dev, enum msi_desc_filter filter, 146 + unsigned int first_index, unsigned int last_index) 147 + { 148 + struct xarray *xa = &dev->msi.data->__store; 149 + struct msi_desc *desc; 150 + unsigned long idx; 151 + 152 + lockdep_assert_held(&dev->msi.data->mutex); 153 + 154 + xa_for_each_range(xa, idx, desc, first_index, last_index) { 155 + if (msi_desc_match(desc, filter)) { 156 + xa_erase(xa, idx); 157 + msi_free_desc(desc); 158 + } 159 + } 63 160 } 64 161 65 162 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg) ··· 175 72 } 176 73 EXPORT_SYMBOL_GPL(get_cached_msi_msg); 177 74 75 + static void msi_device_data_release(struct device *dev, void *res) 76 + { 77 + struct msi_device_data *md = res; 78 + 79 + WARN_ON_ONCE(!xa_empty(&md->__store)); 80 + xa_destroy(&md->__store); 81 + dev->msi.data = NULL; 82 + } 83 + 84 + /** 85 + * msi_setup_device_data - Setup MSI device data 86 + * @dev: Device for which MSI device data should be set up 87 + * 88 + * Return: 0 on success, appropriate error code otherwise 89 + * 90 + * This can be called more than once for @dev. If the MSI device data is 91 + * already allocated the call succeeds. The allocated memory is 92 + * automatically released when the device is destroyed. 93 + */ 94 + int msi_setup_device_data(struct device *dev) 95 + { 96 + struct msi_device_data *md; 97 + int ret; 98 + 99 + if (dev->msi.data) 100 + return 0; 101 + 102 + md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL); 103 + if (!md) 104 + return -ENOMEM; 105 + 106 + ret = msi_sysfs_create_group(dev); 107 + if (ret) { 108 + devres_free(md); 109 + return ret; 110 + } 111 + 112 + xa_init(&md->__store); 113 + mutex_init(&md->mutex); 114 + dev->msi.data = md; 115 + devres_add(dev, md); 116 + return 0; 117 + } 118 + 119 + /** 120 + * msi_lock_descs - Lock the MSI descriptor storage of a device 121 + * @dev: Device to operate on 122 + */ 123 + void msi_lock_descs(struct device *dev) 124 + { 125 + mutex_lock(&dev->msi.data->mutex); 126 + } 127 + EXPORT_SYMBOL_GPL(msi_lock_descs); 128 + 129 + /** 130 + * msi_unlock_descs - Unlock the MSI descriptor storage of a device 131 + * @dev: Device to operate on 132 + */ 133 + void msi_unlock_descs(struct device *dev) 134 + { 135 + /* Invalidate the index wich was cached by the iterator */ 136 + dev->msi.data->__iter_idx = MSI_MAX_INDEX; 137 + mutex_unlock(&dev->msi.data->mutex); 138 + } 139 + EXPORT_SYMBOL_GPL(msi_unlock_descs); 140 + 141 + static struct msi_desc *msi_find_desc(struct msi_device_data *md, enum msi_desc_filter filter) 142 + { 143 + struct msi_desc *desc; 144 + 145 + xa_for_each_start(&md->__store, md->__iter_idx, desc, md->__iter_idx) { 146 + if (msi_desc_match(desc, filter)) 147 + return desc; 148 + } 149 + md->__iter_idx = MSI_MAX_INDEX; 150 + return NULL; 151 + } 152 + 153 + /** 154 + * msi_first_desc - Get the first MSI descriptor of a device 155 + * @dev: Device to operate on 156 + * @filter: Descriptor state filter 157 + * 158 + * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs() 159 + * must be invoked before the call. 160 + * 161 + * Return: Pointer to the first MSI descriptor matching the search 162 + * criteria, NULL if none found. 163 + */ 164 + struct msi_desc *msi_first_desc(struct device *dev, enum msi_desc_filter filter) 165 + { 166 + struct msi_device_data *md = dev->msi.data; 167 + 168 + if (WARN_ON_ONCE(!md)) 169 + return NULL; 170 + 171 + lockdep_assert_held(&md->mutex); 172 + 173 + md->__iter_idx = 0; 174 + return msi_find_desc(md, filter); 175 + } 176 + EXPORT_SYMBOL_GPL(msi_first_desc); 177 + 178 + /** 179 + * msi_next_desc - Get the next MSI descriptor of a device 180 + * @dev: Device to operate on 181 + * 182 + * The first invocation of msi_next_desc() has to be preceeded by a 183 + * successful invocation of __msi_first_desc(). Consecutive invocations are 184 + * only valid if the previous one was successful. All these operations have 185 + * to be done within the same MSI mutex held region. 186 + * 187 + * Return: Pointer to the next MSI descriptor matching the search 188 + * criteria, NULL if none found. 189 + */ 190 + struct msi_desc *msi_next_desc(struct device *dev, enum msi_desc_filter filter) 191 + { 192 + struct msi_device_data *md = dev->msi.data; 193 + 194 + if (WARN_ON_ONCE(!md)) 195 + return NULL; 196 + 197 + lockdep_assert_held(&md->mutex); 198 + 199 + if (md->__iter_idx >= (unsigned long)MSI_MAX_INDEX) 200 + return NULL; 201 + 202 + md->__iter_idx++; 203 + return msi_find_desc(md, filter); 204 + } 205 + EXPORT_SYMBOL_GPL(msi_next_desc); 206 + 207 + /** 208 + * msi_get_virq - Return Linux interrupt number of a MSI interrupt 209 + * @dev: Device to operate on 210 + * @index: MSI interrupt index to look for (0-based) 211 + * 212 + * Return: The Linux interrupt number on success (> 0), 0 if not found 213 + */ 214 + unsigned int msi_get_virq(struct device *dev, unsigned int index) 215 + { 216 + struct msi_desc *desc; 217 + unsigned int ret = 0; 218 + bool pcimsi; 219 + 220 + if (!dev->msi.data) 221 + return 0; 222 + 223 + pcimsi = dev_is_pci(dev) ? to_pci_dev(dev)->msi_enabled : false; 224 + 225 + msi_lock_descs(dev); 226 + desc = xa_load(&dev->msi.data->__store, pcimsi ? 0 : index); 227 + if (desc && desc->irq) { 228 + /* 229 + * PCI-MSI has only one descriptor for multiple interrupts. 230 + * PCI-MSIX and platform MSI use a descriptor per 231 + * interrupt. 232 + */ 233 + if (pcimsi) { 234 + if (index < desc->nvec_used) 235 + ret = desc->irq + index; 236 + } else { 237 + ret = desc->irq; 238 + } 239 + } 240 + msi_unlock_descs(dev); 241 + return ret; 242 + } 243 + EXPORT_SYMBOL_GPL(msi_get_virq); 244 + 245 + #ifdef CONFIG_SYSFS 246 + static struct attribute *msi_dev_attrs[] = { 247 + NULL 248 + }; 249 + 250 + static const struct attribute_group msi_irqs_group = { 251 + .name = "msi_irqs", 252 + .attrs = msi_dev_attrs, 253 + }; 254 + 255 + static inline int msi_sysfs_create_group(struct device *dev) 256 + { 257 + return devm_device_add_group(dev, &msi_irqs_group); 258 + } 259 + 178 260 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr, 179 261 char *buf) 180 262 { 181 - struct msi_desc *entry; 182 - bool is_msix = false; 183 - unsigned long irq; 184 - int retval; 185 - 186 - retval = kstrtoul(attr->attr.name, 10, &irq); 187 - if (retval) 188 - return retval; 189 - 190 - entry = irq_get_msi_desc(irq); 191 - if (!entry) 192 - return -ENODEV; 193 - 194 - if (dev_is_pci(dev)) 195 - is_msix = entry->msi_attrib.is_msix; 263 + /* MSI vs. MSIX is per device not per interrupt */ 264 + bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false; 196 265 197 266 return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi"); 198 267 } 199 268 200 - /** 201 - * msi_populate_sysfs - Populate msi_irqs sysfs entries for devices 202 - * @dev: The device(PCI, platform etc) who will get sysfs entries 203 - * 204 - * Return attribute_group ** so that specific bus MSI can save it to 205 - * somewhere during initilizing msi irqs. If devices has no MSI irq, 206 - * return NULL; if it fails to populate sysfs, return ERR_PTR 207 - */ 208 - const struct attribute_group **msi_populate_sysfs(struct device *dev) 269 + static void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) 209 270 { 210 - const struct attribute_group **msi_irq_groups; 211 - struct attribute **msi_attrs, *msi_attr; 212 - struct device_attribute *msi_dev_attr; 213 - struct attribute_group *msi_irq_group; 214 - struct msi_desc *entry; 215 - int ret = -ENOMEM; 216 - int num_msi = 0; 217 - int count = 0; 271 + struct device_attribute *attrs = desc->sysfs_attrs; 218 272 int i; 219 273 220 - /* Determine how many msi entries we have */ 221 - for_each_msi_entry(entry, dev) 222 - num_msi += entry->nvec_used; 223 - if (!num_msi) 224 - return NULL; 274 + if (!attrs) 275 + return; 225 276 226 - /* Dynamically create the MSI attributes for the device */ 227 - msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL); 228 - if (!msi_attrs) 229 - return ERR_PTR(-ENOMEM); 277 + desc->sysfs_attrs = NULL; 278 + for (i = 0; i < desc->nvec_used; i++) { 279 + if (attrs[i].show) 280 + sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name); 281 + kfree(attrs[i].attr.name); 282 + } 283 + kfree(attrs); 284 + } 230 285 231 - for_each_msi_entry(entry, dev) { 232 - for (i = 0; i < entry->nvec_used; i++) { 233 - msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); 234 - if (!msi_dev_attr) 235 - goto error_attrs; 236 - msi_attrs[count] = &msi_dev_attr->attr; 286 + static int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) 287 + { 288 + struct device_attribute *attrs; 289 + int ret, i; 237 290 238 - sysfs_attr_init(&msi_dev_attr->attr); 239 - msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d", 240 - entry->irq + i); 241 - if (!msi_dev_attr->attr.name) 242 - goto error_attrs; 243 - msi_dev_attr->attr.mode = 0444; 244 - msi_dev_attr->show = msi_mode_show; 245 - ++count; 291 + attrs = kcalloc(desc->nvec_used, sizeof(*attrs), GFP_KERNEL); 292 + if (!attrs) 293 + return -ENOMEM; 294 + 295 + desc->sysfs_attrs = attrs; 296 + for (i = 0; i < desc->nvec_used; i++) { 297 + sysfs_attr_init(&attrs[i].attr); 298 + attrs[i].attr.name = kasprintf(GFP_KERNEL, "%d", desc->irq + i); 299 + if (!attrs[i].attr.name) { 300 + ret = -ENOMEM; 301 + goto fail; 302 + } 303 + 304 + attrs[i].attr.mode = 0444; 305 + attrs[i].show = msi_mode_show; 306 + 307 + ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name); 308 + if (ret) { 309 + attrs[i].show = NULL; 310 + goto fail; 246 311 } 247 312 } 313 + return 0; 248 314 249 - msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL); 250 - if (!msi_irq_group) 251 - goto error_attrs; 252 - msi_irq_group->name = "msi_irqs"; 253 - msi_irq_group->attrs = msi_attrs; 315 + fail: 316 + msi_sysfs_remove_desc(dev, desc); 317 + return ret; 318 + } 254 319 255 - msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL); 256 - if (!msi_irq_groups) 257 - goto error_irq_group; 258 - msi_irq_groups[0] = msi_irq_group; 320 + #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS 321 + /** 322 + * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device 323 + * @dev: The device (PCI, platform etc) which will get sysfs entries 324 + */ 325 + int msi_device_populate_sysfs(struct device *dev) 326 + { 327 + struct msi_desc *desc; 328 + int ret; 259 329 260 - ret = sysfs_create_groups(&dev->kobj, msi_irq_groups); 261 - if (ret) 262 - goto error_irq_groups; 263 - 264 - return msi_irq_groups; 265 - 266 - error_irq_groups: 267 - kfree(msi_irq_groups); 268 - error_irq_group: 269 - kfree(msi_irq_group); 270 - error_attrs: 271 - count = 0; 272 - msi_attr = msi_attrs[count]; 273 - while (msi_attr) { 274 - msi_dev_attr = container_of(msi_attr, struct device_attribute, attr); 275 - kfree(msi_attr->name); 276 - kfree(msi_dev_attr); 277 - ++count; 278 - msi_attr = msi_attrs[count]; 330 + msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) { 331 + if (desc->sysfs_attrs) 332 + continue; 333 + ret = msi_sysfs_populate_desc(dev, desc); 334 + if (ret) 335 + return ret; 279 336 } 280 - kfree(msi_attrs); 281 - return ERR_PTR(ret); 337 + return 0; 282 338 } 283 339 284 340 /** 285 - * msi_destroy_sysfs - Destroy msi_irqs sysfs entries for devices 286 - * @dev: The device(PCI, platform etc) who will remove sysfs entries 287 - * @msi_irq_groups: attribute_group for device msi_irqs entries 341 + * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device 342 + * @dev: The device (PCI, platform etc) for which to remove 343 + * sysfs entries 288 344 */ 289 - void msi_destroy_sysfs(struct device *dev, const struct attribute_group **msi_irq_groups) 345 + void msi_device_destroy_sysfs(struct device *dev) 290 346 { 291 - struct device_attribute *dev_attr; 292 - struct attribute **msi_attrs; 293 - int count = 0; 347 + struct msi_desc *desc; 294 348 295 - if (msi_irq_groups) { 296 - sysfs_remove_groups(&dev->kobj, msi_irq_groups); 297 - msi_attrs = msi_irq_groups[0]->attrs; 298 - while (msi_attrs[count]) { 299 - dev_attr = container_of(msi_attrs[count], 300 - struct device_attribute, attr); 301 - kfree(dev_attr->attr.name); 302 - kfree(dev_attr); 303 - ++count; 304 - } 305 - kfree(msi_attrs); 306 - kfree(msi_irq_groups[0]); 307 - kfree(msi_irq_groups); 308 - } 349 + msi_for_each_desc(desc, dev, MSI_DESC_ALL) 350 + msi_sysfs_remove_desc(dev, desc); 309 351 } 352 + #endif /* CONFIG_PCI_MSI_ARCH_FALLBACK */ 353 + #else /* CONFIG_SYSFS */ 354 + static inline int msi_sysfs_create_group(struct device *dev) { return 0; } 355 + static inline int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) { return 0; } 356 + static inline void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) { } 357 + #endif /* !CONFIG_SYSFS */ 310 358 311 359 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN 312 360 static inline void irq_chip_write_msi_msg(struct irq_data *data, ··· 710 456 } 711 457 712 458 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev, 713 - int virq, int nvec, msi_alloc_info_t *arg) 459 + int virq_base, int nvec, msi_alloc_info_t *arg) 714 460 { 715 461 struct msi_domain_info *info = domain->host_data; 716 462 struct msi_domain_ops *ops = info->ops; 717 463 struct msi_desc *desc; 718 - int ret = 0; 464 + int ret, virq; 719 465 720 - for_each_msi_entry(desc, dev) { 721 - /* Don't even try the multi-MSI brain damage. */ 722 - if (WARN_ON(!desc->irq || desc->nvec_used != 1)) { 723 - ret = -EINVAL; 724 - break; 725 - } 466 + msi_lock_descs(dev); 467 + ret = msi_add_simple_msi_descs(dev, virq_base, nvec); 468 + if (ret) 469 + goto unlock; 726 470 727 - if (!(desc->irq >= virq && desc->irq < (virq + nvec))) 728 - continue; 471 + for (virq = virq_base; virq < virq_base + nvec; virq++) { 472 + desc = xa_load(&dev->msi.data->__store, virq); 473 + desc->irq = virq; 729 474 730 475 ops->set_desc(arg, desc); 731 - /* Assumes the domain mutex is held! */ 732 - ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1, 733 - arg); 476 + ret = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg); 734 477 if (ret) 735 - break; 478 + goto fail; 736 479 737 - irq_set_msi_desc_off(desc->irq, 0, desc); 480 + irq_set_msi_desc(virq, desc); 738 481 } 482 + msi_unlock_descs(dev); 483 + return 0; 739 484 740 - if (ret) { 741 - /* Mop up the damage */ 742 - for_each_msi_entry(desc, dev) { 743 - if (!(desc->irq >= virq && desc->irq < (virq + nvec))) 744 - continue; 745 - 746 - irq_domain_free_irqs_common(domain, desc->irq, 1); 747 - } 748 - } 749 - 485 + fail: 486 + for (--virq; virq >= virq_base; virq--) 487 + irq_domain_free_irqs_common(domain, virq, 1); 488 + msi_free_msi_descs_range(dev, MSI_DESC_ALL, virq_base, virq_base + nvec - 1); 489 + unlock: 490 + msi_unlock_descs(dev); 750 491 return ret; 751 492 } 752 493 ··· 780 531 * Checking the first MSI descriptor is sufficient. MSIX supports 781 532 * masking and MSI does so when the can_mask attribute is set. 782 533 */ 783 - desc = first_msi_entry(dev); 784 - return desc->msi_attrib.is_msix || desc->msi_attrib.can_mask; 534 + desc = msi_first_desc(dev, MSI_DESC_ALL); 535 + return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask; 536 + } 537 + 538 + static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc, 539 + int allocated) 540 + { 541 + switch(domain->bus_token) { 542 + case DOMAIN_BUS_PCI_MSI: 543 + case DOMAIN_BUS_VMD_MSI: 544 + if (IS_ENABLED(CONFIG_PCI_MSI)) 545 + break; 546 + fallthrough; 547 + default: 548 + return -ENOSPC; 549 + } 550 + 551 + /* Let a failed PCI multi MSI allocation retry */ 552 + if (desc->nvec_used > 1) 553 + return 1; 554 + 555 + /* If there was a successful allocation let the caller know */ 556 + return allocated ? allocated : -ENOSPC; 557 + } 558 + 559 + #define VIRQ_CAN_RESERVE 0x01 560 + #define VIRQ_ACTIVATE 0x02 561 + #define VIRQ_NOMASK_QUIRK 0x04 562 + 563 + static int msi_init_virq(struct irq_domain *domain, int virq, unsigned int vflags) 564 + { 565 + struct irq_data *irqd = irq_domain_get_irq_data(domain, virq); 566 + int ret; 567 + 568 + if (!(vflags & VIRQ_CAN_RESERVE)) { 569 + irqd_clr_can_reserve(irqd); 570 + if (vflags & VIRQ_NOMASK_QUIRK) 571 + irqd_set_msi_nomask_quirk(irqd); 572 + } 573 + 574 + if (!(vflags & VIRQ_ACTIVATE)) 575 + return 0; 576 + 577 + ret = irq_domain_activate_irq(irqd, vflags & VIRQ_CAN_RESERVE); 578 + if (ret) 579 + return ret; 580 + /* 581 + * If the interrupt uses reservation mode, clear the activated bit 582 + * so request_irq() will assign the final vector. 583 + */ 584 + if (vflags & VIRQ_CAN_RESERVE) 585 + irqd_clr_activated(irqd); 586 + return 0; 785 587 } 786 588 787 589 int __msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, ··· 840 540 { 841 541 struct msi_domain_info *info = domain->host_data; 842 542 struct msi_domain_ops *ops = info->ops; 843 - struct irq_data *irq_data; 844 - struct msi_desc *desc; 845 543 msi_alloc_info_t arg = { }; 544 + unsigned int vflags = 0; 545 + struct msi_desc *desc; 546 + int allocated = 0; 846 547 int i, ret, virq; 847 - bool can_reserve; 848 548 849 549 ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg); 850 550 if (ret) 851 551 return ret; 852 - 853 - for_each_msi_entry(desc, dev) { 854 - ops->set_desc(&arg, desc); 855 - 856 - virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used, 857 - dev_to_node(dev), &arg, false, 858 - desc->affinity); 859 - if (virq < 0) { 860 - ret = -ENOSPC; 861 - if (ops->handle_error) 862 - ret = ops->handle_error(domain, desc, ret); 863 - if (ops->msi_finish) 864 - ops->msi_finish(&arg, ret); 865 - return ret; 866 - } 867 - 868 - for (i = 0; i < desc->nvec_used; i++) { 869 - irq_set_msi_desc_off(virq, i, desc); 870 - irq_debugfs_copy_devname(virq + i, dev); 871 - } 872 - } 873 - 874 - if (ops->msi_finish) 875 - ops->msi_finish(&arg, 0); 876 - 877 - can_reserve = msi_check_reservation_mode(domain, info, dev); 878 552 879 553 /* 880 554 * This flag is set by the PCI layer as we need to activate 881 555 * the MSI entries before the PCI layer enables MSI in the 882 556 * card. Otherwise the card latches a random msi message. 883 557 */ 884 - if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY)) 885 - goto skip_activate; 558 + if (info->flags & MSI_FLAG_ACTIVATE_EARLY) 559 + vflags |= VIRQ_ACTIVATE; 886 560 887 - for_each_msi_vector(desc, i, dev) { 888 - if (desc->irq == i) { 889 - virq = desc->irq; 890 - dev_dbg(dev, "irq [%d-%d] for MSI\n", 891 - virq, virq + desc->nvec_used - 1); 892 - } 893 - 894 - irq_data = irq_domain_get_irq_data(domain, i); 895 - if (!can_reserve) { 896 - irqd_clr_can_reserve(irq_data); 897 - if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK) 898 - irqd_set_msi_nomask_quirk(irq_data); 899 - } 900 - ret = irq_domain_activate_irq(irq_data, can_reserve); 901 - if (ret) 902 - goto cleanup; 561 + /* 562 + * Interrupt can use a reserved vector and will not occupy 563 + * a real device vector until the interrupt is requested. 564 + */ 565 + if (msi_check_reservation_mode(domain, info, dev)) { 566 + vflags |= VIRQ_CAN_RESERVE; 567 + /* 568 + * MSI affinity setting requires a special quirk (X86) when 569 + * reservation mode is active. 570 + */ 571 + if (domain->flags & IRQ_DOMAIN_MSI_NOMASK_QUIRK) 572 + vflags |= VIRQ_NOMASK_QUIRK; 903 573 } 904 574 905 - skip_activate: 906 - /* 907 - * If these interrupts use reservation mode, clear the activated bit 908 - * so request_irq() will assign the final vector. 909 - */ 910 - if (can_reserve) { 911 - for_each_msi_vector(desc, i, dev) { 912 - irq_data = irq_domain_get_irq_data(domain, i); 913 - irqd_clr_activated(irq_data); 575 + msi_for_each_desc(desc, dev, MSI_DESC_NOTASSOCIATED) { 576 + ops->set_desc(&arg, desc); 577 + 578 + virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used, 579 + dev_to_node(dev), &arg, false, 580 + desc->affinity); 581 + if (virq < 0) 582 + return msi_handle_pci_fail(domain, desc, allocated); 583 + 584 + for (i = 0; i < desc->nvec_used; i++) { 585 + irq_set_msi_desc_off(virq, i, desc); 586 + irq_debugfs_copy_devname(virq + i, dev); 587 + ret = msi_init_virq(domain, virq + i, vflags); 588 + if (ret) 589 + return ret; 914 590 } 591 + if (info->flags & MSI_FLAG_DEV_SYSFS) { 592 + ret = msi_sysfs_populate_desc(dev, desc); 593 + if (ret) 594 + return ret; 595 + } 596 + allocated++; 915 597 } 916 598 return 0; 599 + } 917 600 918 - cleanup: 919 - msi_domain_free_irqs(domain, dev); 601 + static int msi_domain_add_simple_msi_descs(struct msi_domain_info *info, 602 + struct device *dev, 603 + unsigned int num_descs) 604 + { 605 + if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS)) 606 + return 0; 607 + 608 + return msi_add_simple_msi_descs(dev, 0, num_descs); 609 + } 610 + 611 + /** 612 + * msi_domain_alloc_irqs_descs_locked - Allocate interrupts from a MSI interrupt domain 613 + * @domain: The domain to allocate from 614 + * @dev: Pointer to device struct of the device for which the interrupts 615 + * are allocated 616 + * @nvec: The number of interrupts to allocate 617 + * 618 + * Must be invoked from within a msi_lock_descs() / msi_unlock_descs() 619 + * pair. Use this for MSI irqdomains which implement their own vector 620 + * allocation/free. 621 + * 622 + * Return: %0 on success or an error code. 623 + */ 624 + int msi_domain_alloc_irqs_descs_locked(struct irq_domain *domain, struct device *dev, 625 + int nvec) 626 + { 627 + struct msi_domain_info *info = domain->host_data; 628 + struct msi_domain_ops *ops = info->ops; 629 + int ret; 630 + 631 + lockdep_assert_held(&dev->msi.data->mutex); 632 + 633 + ret = msi_domain_add_simple_msi_descs(info, dev, nvec); 634 + if (ret) 635 + return ret; 636 + 637 + ret = ops->domain_alloc_irqs(domain, dev, nvec); 638 + if (ret) 639 + msi_domain_free_irqs_descs_locked(domain, dev); 920 640 return ret; 921 641 } 922 642 ··· 949 629 * 950 630 * Return: %0 on success or an error code. 951 631 */ 952 - int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, 953 - int nvec) 632 + int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev, int nvec) 954 633 { 955 - struct msi_domain_info *info = domain->host_data; 956 - struct msi_domain_ops *ops = info->ops; 634 + int ret; 957 635 958 - return ops->domain_alloc_irqs(domain, dev, nvec); 636 + msi_lock_descs(dev); 637 + ret = msi_domain_alloc_irqs_descs_locked(domain, dev, nvec); 638 + msi_unlock_descs(dev); 639 + return ret; 959 640 } 960 641 961 642 void __msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) 962 643 { 963 - struct irq_data *irq_data; 644 + struct msi_domain_info *info = domain->host_data; 645 + struct irq_data *irqd; 964 646 struct msi_desc *desc; 965 647 int i; 966 648 967 - for_each_msi_vector(desc, i, dev) { 968 - irq_data = irq_domain_get_irq_data(domain, i); 969 - if (irqd_is_activated(irq_data)) 970 - irq_domain_deactivate_irq(irq_data); 971 - } 972 - 973 - for_each_msi_entry(desc, dev) { 974 - /* 975 - * We might have failed to allocate an MSI early 976 - * enough that there is no IRQ associated to this 977 - * entry. If that's the case, don't do anything. 978 - */ 979 - if (desc->irq) { 980 - irq_domain_free_irqs(desc->irq, desc->nvec_used); 981 - desc->irq = 0; 649 + /* Only handle MSI entries which have an interrupt associated */ 650 + msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) { 651 + /* Make sure all interrupts are deactivated */ 652 + for (i = 0; i < desc->nvec_used; i++) { 653 + irqd = irq_domain_get_irq_data(domain, desc->irq + i); 654 + if (irqd && irqd_is_activated(irqd)) 655 + irq_domain_deactivate_irq(irqd); 982 656 } 657 + 658 + irq_domain_free_irqs(desc->irq, desc->nvec_used); 659 + if (info->flags & MSI_FLAG_DEV_SYSFS) 660 + msi_sysfs_remove_desc(dev, desc); 661 + desc->irq = 0; 983 662 } 663 + } 664 + 665 + static void msi_domain_free_msi_descs(struct msi_domain_info *info, 666 + struct device *dev) 667 + { 668 + if (info->flags & MSI_FLAG_FREE_MSI_DESCS) 669 + msi_free_msi_descs(dev); 670 + } 671 + 672 + /** 673 + * msi_domain_free_irqs_descs_locked - Free interrupts from a MSI interrupt @domain associated to @dev 674 + * @domain: The domain to managing the interrupts 675 + * @dev: Pointer to device struct of the device for which the interrupts 676 + * are free 677 + * 678 + * Must be invoked from within a msi_lock_descs() / msi_unlock_descs() 679 + * pair. Use this for MSI irqdomains which implement their own vector 680 + * allocation. 681 + */ 682 + void msi_domain_free_irqs_descs_locked(struct irq_domain *domain, struct device *dev) 683 + { 684 + struct msi_domain_info *info = domain->host_data; 685 + struct msi_domain_ops *ops = info->ops; 686 + 687 + lockdep_assert_held(&dev->msi.data->mutex); 688 + 689 + ops->domain_free_irqs(domain, dev); 690 + msi_domain_free_msi_descs(info, dev); 984 691 } 985 692 986 693 /** ··· 1018 671 */ 1019 672 void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev) 1020 673 { 1021 - struct msi_domain_info *info = domain->host_data; 1022 - struct msi_domain_ops *ops = info->ops; 1023 - 1024 - return ops->domain_free_irqs(domain, dev); 674 + msi_lock_descs(dev); 675 + msi_domain_free_irqs_descs_locked(domain, dev); 676 + msi_unlock_descs(dev); 1025 677 } 1026 678 1027 679 /**