/** * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state * @dev: pointer to the pci_dev data structure of MSI(X) device function * * Being called during hotplug remove, from which the device function * is hot-removed. All previous assigned MSI/MSI-X vectors, if * allocated for this device function, are reclaimed to unused state, * which may be used later on. **/ void msi_remove_pci_irq_vectors(struct pci_dev* dev) { int state, pos, temp; unsigned long flags; if (!pci_msi_enable || !dev) return; temp = dev->irq; /* Save IOAPIC IRQ */ pos = pci_find_capability(dev, PCI_CAP_ID_MSI); if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[dev->irq]->msi_attrib.state; spin_unlock_irqrestore(&msi_lock, flags); if (state) { printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() " "called without free_irq() on MSI vector %d\n", pci_name(dev), dev->irq); BUG_ON(state > 0); } else /* Release MSI vector assigned to this device */ msi_free_vector(dev, dev->irq, 0); dev->irq = temp; /* Restore IOAPIC IRQ */ } pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); if (pos > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) { int vector, head, tail = 0, warning = 0; void __iomem *base = NULL; vector = head = dev->irq; while (head != tail) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[vector]->msi_attrib.state; tail = msi_desc[vector]->link.tail; base = msi_desc[vector]->mask_base; spin_unlock_irqrestore(&msi_lock, flags); if (state) warning = 1; else if (vector != head) /* Release MSI-X vector */ msi_free_vector(dev, vector, 0); vector = tail; } msi_free_vector(dev, vector, 0); if (warning) { iounmap(base); printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() " "called without free_irq() on all MSI-X vectors\n", pci_name(dev)); BUG_ON(warning > 0); } dev->irq = temp; /* Restore IOAPIC IRQ */ } }
void pci_disable_msix(struct pci_dev* dev) { int pos, temp; u16 control; if (!pci_msi_enable) return; if (!dev) return; if (!dev->msix_enabled) return; pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); if (pos) { pci_read_config_word(dev, msi_control_reg(pos), &control); if (control & PCI_MSIX_FLAGS_ENABLE) disable_msi_mode(dev, pos, PCI_CAP_ID_MSIX); } temp = dev->irq; if (!msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) { int state, vector, head, tail = 0, warning = 0; unsigned long flags; vector = head = dev->irq; dev->irq = temp; /* Restore pin IRQ */ while (head != tail) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[vector]->msi_attrib.state; tail = msi_desc[vector]->link.tail; spin_unlock_irqrestore(&msi_lock, flags); if (state) warning = 1; else if (vector != head) /* Release MSI-X vector */ msi_free_vector(dev, vector, 0); vector = tail; } msi_free_vector(dev, vector, 0); if (warning) { printk(KERN_WARNING "PCI: %s: pci_disable_msix() called without " "free_irq() on all MSI-X vectors\n", pci_name(dev)); BUG_ON(warning > 0); } } }
void pci_disable_msi(struct pci_dev* dev) { struct msi_desc *entry; int pos, default_vector; u16 control; unsigned long flags; if (!pci_msi_enable) return; if (!dev) return; if (!dev->msi_enabled) return; pos = pci_find_capability(dev, PCI_CAP_ID_MSI); if (pos) { pci_read_config_word(dev, msi_control_reg(pos), &control); if (control & PCI_MSI_FLAGS_ENABLE) disable_msi_mode(dev, pos, PCI_CAP_ID_MSI); } spin_lock_irqsave(&msi_lock, flags); entry = msi_desc[dev->irq]; if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) { spin_unlock_irqrestore(&msi_lock, flags); return; } if (entry->msi_attrib.state) { spin_unlock_irqrestore(&msi_lock, flags); printk(KERN_WARNING "PCI: %s: pci_disable_msi() called without " "free_irq() on MSI vector %d\n", pci_name(dev), dev->irq); BUG_ON(entry->msi_attrib.state > 0); } else { default_vector = entry->msi_attrib.default_vector; spin_unlock_irqrestore(&msi_lock, flags); msi_free_vector(dev, dev->irq, 0); /* Restore dev->irq to its default pin-assertion vector */ dev->irq = default_vector; } }
/** * msix_capability_init - configure device's MSI-X capability * @dev: pointer to the pci_dev data structure of MSI-X device function * @entries: pointer to an array of struct msix_entry entries * @nvec: number of @entries * * Setup the MSI-X capability structure of device function with a * single MSI-X vector. A return of zero indicates the successful setup of * requested MSI-X entries with allocated vectors or non-zero for otherwise. **/ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, int nvec) { struct msi_desc *head = NULL, *tail = NULL, *entry = NULL; u32 address_hi; u32 address_lo; u32 data; int status; int vector, pos, i, j, nr_entries, temp = 0; unsigned long phys_addr; u32 table_offset; u16 control; u8 bir; void __iomem *base; pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); /* Request & Map MSI-X table region */ pci_read_config_word(dev, msi_control_reg(pos), &control); nr_entries = multi_msix_capable(control); pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); table_offset &= ~PCI_MSIX_FLAGS_BIRMASK; phys_addr = pci_resource_start (dev, bir) + table_offset; base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); if (base == NULL) return -ENOMEM; /* MSI-X Table Initialization */ for (i = 0; i < nvec; i++) { entry = alloc_msi_entry(); if (!entry) break; vector = get_msi_vector(dev); if (vector < 0) { kmem_cache_free(msi_cachep, entry); break; } j = entries[i].entry; entries[i].vector = vector; entry->msi_attrib.type = PCI_CAP_ID_MSIX; entry->msi_attrib.state = 0; /* Mark it not active */ entry->msi_attrib.entry_nr = j; entry->msi_attrib.maskbit = 1; entry->msi_attrib.default_vector = dev->irq; entry->dev = dev; entry->mask_base = base; if (!head) { entry->link.head = vector; entry->link.tail = vector; head = entry; } else { entry->link.head = temp; entry->link.tail = tail->link.tail; tail->link.tail = vector; head->link.head = vector; } temp = vector; tail = entry; /* Replace with MSI-X handler */ irq_handler_init(PCI_CAP_ID_MSIX, vector, 1); /* Configure MSI-X capability structure */ status = msi_ops->setup(dev, vector, &address_hi, &address_lo, &data); if (status < 0) break; writel(address_lo, base + j * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); writel(address_hi, base + j * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); writel(data, base + j * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_DATA_OFFSET); attach_msi_entry(entry, vector); } if (i != nvec) { i--; for (; i >= 0; i--) { vector = (entries + i)->vector; msi_free_vector(dev, vector, 0); (entries + i)->vector = 0; } return -EBUSY; } /* Set MSI-X enabled bits */ enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX); return 0; }
static int assign_msi_vector(void) { static int new_vector_avail = 1; int vector; unsigned long flags; /* * msi_lock is provided to ensure that successful allocation of MSI * vector is assigned unique among drivers. */ spin_lock_irqsave(&msi_lock, flags); if (!new_vector_avail) { int free_vector = 0; /* * vector_irq[] = -1 indicates that this specific vector is: * - assigned for MSI (since MSI have no associated IRQ) or * - assigned for legacy if less than 16, or * - having no corresponding 1:1 vector-to-IOxAPIC IRQ mapping * vector_irq[] = 0 indicates that this vector, previously * assigned for MSI, is freed by hotplug removed operations. * This vector will be reused for any subsequent hotplug added * operations. * vector_irq[] > 0 indicates that this vector is assigned for * IOxAPIC IRQs. This vector and its value provides a 1-to-1 * vector-to-IOxAPIC IRQ mapping. */ for (vector = FIRST_DEVICE_VECTOR; vector < NR_IRQS; vector++) { if (vector_irq[vector] != 0) continue; free_vector = vector; if (!msi_desc[vector]) break; else continue; } if (!free_vector) { spin_unlock_irqrestore(&msi_lock, flags); return -EBUSY; } vector_irq[free_vector] = -1; nr_released_vectors--; spin_unlock_irqrestore(&msi_lock, flags); if (msi_desc[free_vector] != NULL) { struct pci_dev *dev; int tail; /* free all linked vectors before re-assign */ do { spin_lock_irqsave(&msi_lock, flags); dev = msi_desc[free_vector]->dev; tail = msi_desc[free_vector]->link.tail; spin_unlock_irqrestore(&msi_lock, flags); msi_free_vector(dev, tail, 1); } while (free_vector != tail); } return free_vector; } vector = assign_irq_vector(AUTO_ASSIGN); last_alloc_vector = vector; if (vector == LAST_DEVICE_VECTOR) new_vector_avail = 0; spin_unlock_irqrestore(&msi_lock, flags); return vector; }
/** * msix_capability_init - configure device's MSI-X capability * @dev: pointer to the pci_dev data structure of MSI-X device function * @entries: pointer to an array of struct msix_entry entries * @nvec: number of @entries * * Setup the MSI-X capability structure of device function with a * single MSI-X vector. A return of zero indicates the successful setup of * requested MSI-X entries with allocated vectors or non-zero for otherwise. **/ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, int nvec) { struct msi_desc *head = NULL, *tail = NULL, *entry = NULL; struct msg_address address; struct msg_data data; int vector, pos, i, j, nr_entries, temp = 0; u32 phys_addr, table_offset; u16 control; u8 bir; void __iomem *base; pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); /* Request & Map MSI-X table region */ pci_read_config_word(dev, msi_control_reg(pos), &control); nr_entries = multi_msix_capable(control); pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); phys_addr = pci_resource_start (dev, bir); phys_addr += (u32)(table_offset & ~PCI_MSIX_FLAGS_BIRMASK); base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); if (base == NULL) return -ENOMEM; /* MSI-X Table Initialization */ for (i = 0; i < nvec; i++) { entry = alloc_msi_entry(); if (!entry) break; if ((vector = get_msi_vector(dev)) < 0) break; j = entries[i].entry; entries[i].vector = vector; entry->msi_attrib.type = PCI_CAP_ID_MSIX; entry->msi_attrib.state = 0; /* Mark it not active */ entry->msi_attrib.entry_nr = j; entry->msi_attrib.maskbit = 1; entry->msi_attrib.default_vector = dev->irq; entry->dev = dev; entry->mask_base = base; if (!head) { entry->link.head = vector; entry->link.tail = vector; head = entry; } else { entry->link.head = temp; entry->link.tail = tail->link.tail; tail->link.tail = vector; head->link.head = vector; } temp = vector; tail = entry; /* Replace with MSI-X handler */ irq_handler_init(PCI_CAP_ID_MSIX, vector, 1); /* Configure MSI-X capability structure */ msi_address_init(&address); msi_data_init(&data, vector); entry->msi_attrib.current_cpu = ((address.lo_address.u.dest_id >> MSI_TARGET_CPU_SHIFT) & MSI_TARGET_CPU_MASK); writel(address.lo_address.value, base + j * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); writel(address.hi_address, base + j * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); writel(*(u32*)&data, base + j * PCI_MSIX_ENTRY_SIZE + PCI_MSIX_ENTRY_DATA_OFFSET); attach_msi_entry(entry, vector); } if (i != nvec) { i--; for (; i >= 0; i--) { vector = (entries + i)->vector; msi_free_vector(dev, vector, 0); (entries + i)->vector = 0; } return -EBUSY; } /* Set MSI-X enabled bits */ enable_msi_mode(dev, pos, PCI_CAP_ID_MSIX); return 0; }
/** * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state * @dev: pointer to the pci_dev data structure of MSI(X) device function * * Being called during hotplug remove, from which the device function * is hot-removed. All previous assigned MSI/MSI-X vectors, if * allocated for this device function, are reclaimed to unused state, * which may be used later on. **/ void msi_remove_pci_irq_vectors(struct pci_dev* dev) { int state, pos, temp; unsigned long flags; if (!pci_msi_enable || !dev) return; temp = dev->irq; /* Save IOAPIC IRQ */ if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[dev->irq]->msi_attrib.state; spin_unlock_irqrestore(&msi_lock, flags); if (state) { printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() " "called without free_irq() on MSI vector %d\n", pci_name(dev), dev->irq); BUG_ON(state > 0); } else /* Release MSI vector assigned to this device */ msi_free_vector(dev, dev->irq, 0); dev->irq = temp; /* Restore IOAPIC IRQ */ } if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)) > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) { int vector, head, tail = 0, warning = 0; void __iomem *base = NULL; vector = head = dev->irq; while (head != tail) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[vector]->msi_attrib.state; tail = msi_desc[vector]->link.tail; base = msi_desc[vector]->mask_base; spin_unlock_irqrestore(&msi_lock, flags); if (state) warning = 1; else if (vector != head) /* Release MSI-X vector */ msi_free_vector(dev, vector, 0); vector = tail; } msi_free_vector(dev, vector, 0); if (warning) { /* Force to release the MSI-X memory-mapped table */ u32 phys_addr, table_offset; u16 control; u8 bir; pci_read_config_word(dev, msi_control_reg(pos), &control); pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); phys_addr = pci_resource_start (dev, bir); phys_addr += (u32)(table_offset & ~PCI_MSIX_FLAGS_BIRMASK); iounmap(base); printk(KERN_WARNING "PCI: %s: msi_remove_pci_irq_vectors() " "called without free_irq() on all MSI-X vectors\n", pci_name(dev)); BUG_ON(warning > 0); } dev->irq = temp; /* Restore IOAPIC IRQ */ } }
/** * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state * @dev: pointer to the pci_dev data structure of MSI(X) device function * * Being called during hotplug remove, from which the device funciton * is hot-removed. All previous assigned MSI/MSI-X vectors, if * allocated for this device function, are reclaimed to unused state, * which may be used later on. **/ void msi_remove_pci_irq_vectors(struct pci_dev* dev) { int state, pos, temp; unsigned long flags; if (!pci_msi_enable || !dev) return; temp = dev->irq; /* Save IOAPIC IRQ */ if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSI)) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[dev->irq]->msi_attrib.state; spin_unlock_irqrestore(&msi_lock, flags); if (state) { printk(KERN_DEBUG "Driver[%d:%d:%d] unloaded wo doing free_irq on vector->%d\n", dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), dev->irq); BUG_ON(state > 0); } else /* Release MSI vector assigned to this device */ msi_free_vector(dev, dev->irq, 0); dev->irq = temp; /* Restore IOAPIC IRQ */ } if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSIX)) > 0 && !msi_lookup_vector(dev, PCI_CAP_ID_MSIX)) { int vector, head, tail = 0, warning = 0; unsigned long base = 0L; vector = head = dev->irq; while (head != tail) { spin_lock_irqsave(&msi_lock, flags); state = msi_desc[vector]->msi_attrib.state; tail = msi_desc[vector]->link.tail; base = msi_desc[vector]->mask_base; spin_unlock_irqrestore(&msi_lock, flags); if (state) warning = 1; else if (vector != head) /* Release MSI-X vector */ msi_free_vector(dev, vector, 0); vector = tail; } msi_free_vector(dev, vector, 0); if (warning) { /* Force to release the MSI-X memory-mapped table */ u32 phys_addr, table_offset; u16 control; u8 bir; pci_read_config_word(dev, msi_control_reg(pos), &control); pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); phys_addr = pci_resource_start (dev, bir); phys_addr += (u32)(table_offset & ~PCI_MSIX_FLAGS_BIRMASK); iounmap((void*)base); release_mem_region(phys_addr, PCI_MSIX_ENTRY_SIZE * multi_msix_capable(control)); printk(KERN_DEBUG "Driver[%d:%d:%d] unloaded wo doing free_irq on all vectors\n", dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn)); BUG_ON(warning > 0); } dev->irq = temp; /* Restore IOAPIC IRQ */ } }