x86/PCI: VMD: Synchronize with RCU freeing MSI IRQ descs
[cascardo/linux.git] / arch / x86 / pci / vmd.c
index 613cac7..5705852 100644 (file)
@@ -56,16 +56,11 @@ struct vmd_irq {
 /**
  * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector
  * @irq_list:  the list of irq's the VMD one demuxes to.
- * @vmd_vector:        the h/w IRQ assigned to the VMD.
- * @index:     index into the VMD MSI-X table; used for message routing.
  * @count:     number of child IRQs assigned to this vector; used to track
  *             sharing.
  */
 struct vmd_irq_list {
        struct list_head        irq_list;
-       struct vmd_dev          *vmd;
-       unsigned int            vmd_vector;
-       unsigned int            index;
        unsigned int            count;
 };
 
@@ -76,7 +71,6 @@ struct vmd_dev {
        char __iomem            *cfgbar;
 
        int msix_count;
-       struct msix_entry       *msix_entries;
        struct vmd_irq_list     *irqs;
 
        struct pci_sysdata      sysdata;
@@ -95,6 +89,12 @@ static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
        return container_of(bus->sysdata, struct vmd_dev, sysdata);
 }
 
+static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
+                                          struct vmd_irq_list *irqs)
+{
+       return irqs - vmd->irqs;
+}
+
 /*
  * Drivers managing a device in a VMD domain allocate their own IRQs as before,
  * but the MSI entry for the hardware it's driving will be programmed with a
@@ -107,9 +107,11 @@ static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 {
        struct vmd_irq *vmdirq = data->chip_data;
        struct vmd_irq_list *irq = vmdirq->irq;
+       struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
 
        msg->address_hi = MSI_ADDR_BASE_HI;
-       msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_DEST_ID(irq->index);
+       msg->address_lo = MSI_ADDR_BASE_LO |
+                         MSI_ADDR_DEST_ID(index_from_irqs(vmd, irq));
        msg->data = 0;
 }
 
@@ -119,10 +121,11 @@ static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
 static void vmd_irq_enable(struct irq_data *data)
 {
        struct vmd_irq *vmdirq = data->chip_data;
+       unsigned long flags;
 
-       raw_spin_lock(&list_lock);
+       raw_spin_lock_irqsave(&list_lock, flags);
        list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
-       raw_spin_unlock(&list_lock);
+       raw_spin_unlock_irqrestore(&list_lock, flags);
 
        data->chip->irq_unmask(data);
 }
@@ -130,12 +133,14 @@ static void vmd_irq_enable(struct irq_data *data)
 static void vmd_irq_disable(struct irq_data *data)
 {
        struct vmd_irq *vmdirq = data->chip_data;
+       unsigned long flags;
 
        data->chip->irq_mask(data);
 
-       raw_spin_lock(&list_lock);
+       raw_spin_lock_irqsave(&list_lock, flags);
        list_del_rcu(&vmdirq->node);
-       raw_spin_unlock(&list_lock);
+       INIT_LIST_HEAD_RCU(&vmdirq->node);
+       raw_spin_unlock_irqrestore(&list_lock, flags);
 }
 
 /*
@@ -166,16 +171,20 @@ static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
  * XXX: We can be even smarter selecting the best IRQ once we solve the
  * affinity problem.
  */
-static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd)
+static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
 {
-       int i, best = 0;
+       int i, best = 1;
+       unsigned long flags;
+
+       if (!desc->msi_attrib.is_msix || vmd->msix_count == 1)
+               return &vmd->irqs[0];
 
-       raw_spin_lock(&list_lock);
+       raw_spin_lock_irqsave(&list_lock, flags);
        for (i = 1; i < vmd->msix_count; i++)
                if (vmd->irqs[i].count < vmd->irqs[best].count)
                        best = i;
        vmd->irqs[best].count++;
-       raw_spin_unlock(&list_lock);
+       raw_spin_unlock_irqrestore(&list_lock, flags);
 
        return &vmd->irqs[best];
 }
@@ -184,18 +193,22 @@ static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
                        unsigned int virq, irq_hw_number_t hwirq,
                        msi_alloc_info_t *arg)
 {
-       struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(arg->desc)->bus);
+       struct msi_desc *desc = arg->desc;
+       struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
        struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
+       unsigned int index, vector;
 
        if (!vmdirq)
                return -ENOMEM;
 
        INIT_LIST_HEAD(&vmdirq->node);
-       vmdirq->irq = vmd_next_irq(vmd);
+       vmdirq->irq = vmd_next_irq(vmd, desc);
        vmdirq->virq = virq;
+       index = index_from_irqs(vmd, vmdirq->irq);
+       vector = pci_irq_vector(vmd->dev, index);
 
-       irq_domain_set_info(domain, virq, vmdirq->irq->vmd_vector, info->chip,
-                           vmdirq, handle_untracked_irq, vmd, NULL);
+       irq_domain_set_info(domain, virq, vector, info->chip, vmdirq,
+                           handle_untracked_irq, vmd, NULL);
        return 0;
 }
 
@@ -203,11 +216,14 @@ static void vmd_msi_free(struct irq_domain *domain,
                        struct msi_domain_info *info, unsigned int virq)
 {
        struct vmd_irq *vmdirq = irq_get_chip_data(virq);
+       unsigned long flags;
+
+       synchronize_rcu();
 
        /* XXX: Potential optimization to rebalance */
-       raw_spin_lock(&list_lock);
+       raw_spin_lock_irqsave(&list_lock, flags);
        vmdirq->irq->count--;
-       raw_spin_unlock(&list_lock);
+       raw_spin_unlock_irqrestore(&list_lock, flags);
 
        kfree_rcu(vmdirq, rcu);
 }
@@ -261,18 +277,18 @@ static struct device *to_vmd_dev(struct device *dev)
 
 static struct dma_map_ops *vmd_dma_ops(struct device *dev)
 {
-       return to_vmd_dev(dev)->archdata.dma_ops;
+       return get_dma_ops(to_vmd_dev(dev));
 }
 
 static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
-                      gfp_t flag, struct dma_attrs *attrs)
+                      gfp_t flag, unsigned long attrs)
 {
        return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
                                       attrs);
 }
 
 static void vmd_free(struct device *dev, size_t size, void *vaddr,
-                    dma_addr_t addr, struct dma_attrs *attrs)
+                    dma_addr_t addr, unsigned long attrs)
 {
        return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
                                      attrs);
@@ -280,7 +296,7 @@ static void vmd_free(struct device *dev, size_t size, void *vaddr,
 
 static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
                    void *cpu_addr, dma_addr_t addr, size_t size,
-                   struct dma_attrs *attrs)
+                   unsigned long attrs)
 {
        return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
                                      size, attrs);
@@ -288,7 +304,7 @@ static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
 
 static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
                           void *cpu_addr, dma_addr_t addr, size_t size,
-                          struct dma_attrs *attrs)
+                          unsigned long attrs)
 {
        return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
                                             addr, size, attrs);
@@ -297,26 +313,26 @@ static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
 static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
                               unsigned long offset, size_t size,
                               enum dma_data_direction dir,
-                              struct dma_attrs *attrs)
+                              unsigned long attrs)
 {
        return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
                                          dir, attrs);
 }
 
 static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
-                          enum dma_data_direction dir, struct dma_attrs *attrs)
+                          enum dma_data_direction dir, unsigned long attrs)
 {
        vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
 }
 
 static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
-                     enum dma_data_direction dir, struct dma_attrs *attrs)
+                     enum dma_data_direction dir, unsigned long attrs)
 {
        return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
 }
 
 static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
-                        enum dma_data_direction dir, struct dma_attrs *attrs)
+                        enum dma_data_direction dir, unsigned long attrs)
 {
        vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
 }
@@ -367,7 +383,7 @@ static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
 {
        struct dma_domain *domain = &vmd->dma_domain;
 
-       if (vmd->dev->dev.archdata.dma_ops)
+       if (get_dma_ops(&vmd->dev->dev))
                del_dma_domain(domain);
 }
 
@@ -379,7 +395,7 @@ static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
 
 static void vmd_setup_dma_ops(struct vmd_dev *vmd)
 {
-       const struct dma_map_ops *source = vmd->dev->dev.archdata.dma_ops;
+       const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
        struct dma_map_ops *dest = &vmd->dma_ops;
        struct dma_domain *domain = &vmd->dma_domain;
 
@@ -594,7 +610,7 @@ static int vmd_enable_domain(struct vmd_dev *vmd)
        sd->node = pcibus_to_node(vmd->dev->bus);
 
        vmd->irq_domain = pci_msi_create_irq_domain(NULL, &vmd_msi_domain_info,
-                                                   NULL);
+                                                   x86_vector_domain);
        if (!vmd->irq_domain)
                return -ENODEV;
 
@@ -662,30 +678,19 @@ static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
        if (vmd->msix_count < 0)
                return -ENODEV;
 
+       vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count,
+                                       PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
+       if (vmd->msix_count < 0)
+               return vmd->msix_count;
+
        vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
                                 GFP_KERNEL);
        if (!vmd->irqs)
                return -ENOMEM;
 
-       vmd->msix_entries = devm_kcalloc(&dev->dev, vmd->msix_count,
-                                        sizeof(*vmd->msix_entries),
-                                        GFP_KERNEL);
-       if (!vmd->msix_entries)
-               return -ENOMEM;
-       for (i = 0; i < vmd->msix_count; i++)
-               vmd->msix_entries[i].entry = i;
-
-       vmd->msix_count = pci_enable_msix_range(vmd->dev, vmd->msix_entries, 1,
-                                               vmd->msix_count);
-       if (vmd->msix_count < 0)
-               return vmd->msix_count;
-
        for (i = 0; i < vmd->msix_count; i++) {
                INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
-               vmd->irqs[i].vmd_vector = vmd->msix_entries[i].vector;
-               vmd->irqs[i].index = i;
-
-               err = devm_request_irq(&dev->dev, vmd->irqs[i].vmd_vector,
+               err = devm_request_irq(&dev->dev, pci_irq_vector(dev, i),
                                       vmd_irq, 0, "vmd", &vmd->irqs[i]);
                if (err)
                        return err;