]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/iommu/amd_iommu.c
Merge branch 'akpm-current/current'
[karo-tx-linux.git] / drivers / iommu / amd_iommu.c
index 3aa0116c843c050490b675498ac09e825ba78807..8b2be1e7714f8bb7aa1d62193d3b3320fab64414 100644 (file)
@@ -89,8 +89,6 @@ static struct dma_map_ops amd_iommu_dma_ops;
 struct iommu_dev_data {
        struct list_head list;            /* For domain->dev_list */
        struct list_head dev_data_list;   /* For global dev_data_list */
-       struct list_head alias_list;      /* Link alias-groups together */
-       struct iommu_dev_data *alias_data;/* The alias dev_data */
        struct protection_domain *domain; /* Domain the device is bound to */
        u16 devid;                        /* PCI Device ID */
        bool iommu_v2;                    /* Device can make use of IOMMUv2 */
@@ -136,8 +134,6 @@ static struct iommu_dev_data *alloc_dev_data(u16 devid)
        if (!dev_data)
                return NULL;
 
-       INIT_LIST_HEAD(&dev_data->alias_list);
-
        dev_data->devid = devid;
 
        spin_lock_irqsave(&dev_data_list_lock, flags);
@@ -147,17 +143,6 @@ static struct iommu_dev_data *alloc_dev_data(u16 devid)
        return dev_data;
 }
 
-static void free_dev_data(struct iommu_dev_data *dev_data)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&dev_data_list_lock, flags);
-       list_del(&dev_data->dev_data_list);
-       spin_unlock_irqrestore(&dev_data_list_lock, flags);
-
-       kfree(dev_data);
-}
-
 static struct iommu_dev_data *search_dev_data(u16 devid)
 {
        struct iommu_dev_data *dev_data;
@@ -311,73 +296,10 @@ out:
        iommu_group_put(group);
 }
 
-static int __last_alias(struct pci_dev *pdev, u16 alias, void *data)
-{
-       *(u16 *)data = alias;
-       return 0;
-}
-
-static u16 get_alias(struct device *dev)
-{
-       struct pci_dev *pdev = to_pci_dev(dev);
-       u16 devid, ivrs_alias, pci_alias;
-
-       devid = get_device_id(dev);
-       ivrs_alias = amd_iommu_alias_table[devid];
-       pci_for_each_dma_alias(pdev, __last_alias, &pci_alias);
-
-       if (ivrs_alias == pci_alias)
-               return ivrs_alias;
-
-       /*
-        * DMA alias showdown
-        *
-        * The IVRS is fairly reliable in telling us about aliases, but it
-        * can't know about every screwy device.  If we don't have an IVRS
-        * reported alias, use the PCI reported alias.  In that case we may
-        * still need to initialize the rlookup and dev_table entries if the
-        * alias is to a non-existent device.
-        */
-       if (ivrs_alias == devid) {
-               if (!amd_iommu_rlookup_table[pci_alias]) {
-                       amd_iommu_rlookup_table[pci_alias] =
-                               amd_iommu_rlookup_table[devid];
-                       memcpy(amd_iommu_dev_table[pci_alias].data,
-                              amd_iommu_dev_table[devid].data,
-                              sizeof(amd_iommu_dev_table[pci_alias].data));
-               }
-
-               return pci_alias;
-       }
-
-       pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d "
-               "for device %s[%04x:%04x], kernel reported alias "
-               "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias),
-               PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device,
-               PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias),
-               PCI_FUNC(pci_alias));
-
-       /*
-        * If we don't have a PCI DMA alias and the IVRS alias is on the same
-        * bus, then the IVRS table may know about a quirk that we don't.
-        */
-       if (pci_alias == devid &&
-           PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) {
-               pdev->dev_flags |= PCI_DEV_FLAGS_DMA_ALIAS_DEVFN;
-               pdev->dma_alias_devfn = ivrs_alias & 0xff;
-               pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n",
-                       PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias),
-                       dev_name(dev));
-       }
-
-       return ivrs_alias;
-}
-
 static int iommu_init_device(struct device *dev)
 {
        struct pci_dev *pdev = to_pci_dev(dev);
        struct iommu_dev_data *dev_data;
-       u16 alias;
 
        if (dev->archdata.iommu)
                return 0;
@@ -386,24 +308,6 @@ static int iommu_init_device(struct device *dev)
        if (!dev_data)
                return -ENOMEM;
 
-       alias = get_alias(dev);
-
-       if (alias != dev_data->devid) {
-               struct iommu_dev_data *alias_data;
-
-               alias_data = find_dev_data(alias);
-               if (alias_data == NULL) {
-                       pr_err("AMD-Vi: Warning: Unhandled device %s\n",
-                                       dev_name(dev));
-                       free_dev_data(dev_data);
-                       return -ENOTSUPP;
-               }
-               dev_data->alias_data = alias_data;
-
-               /* Add device to the alias_list */
-               list_add(&dev_data->alias_list, &alias_data->alias_list);
-       }
-
        if (pci_iommuv2_capable(pdev)) {
                struct amd_iommu *iommu;
 
@@ -445,9 +349,6 @@ static void iommu_uninit_device(struct device *dev)
 
        iommu_group_remove_device(dev);
 
-       /* Unlink from alias, it may change if another device is re-plugged */
-       dev_data->alias_data = NULL;
-
        /* Remove dma-ops */
        dev->archdata.dma_ops = NULL;
 
@@ -633,7 +534,7 @@ static void iommu_poll_events(struct amd_iommu *iommu)
 
        while (head != tail) {
                iommu_print_event(iommu, iommu->evt_buf + head);
-               head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size;
+               head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE;
        }
 
        writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
@@ -783,7 +684,7 @@ static void copy_cmd_to_buffer(struct amd_iommu *iommu,
        u8 *target;
 
        target = iommu->cmd_buf + tail;
-       tail   = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
+       tail   = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
 
        /* Copy command to buffer */
        memcpy(target, cmd, sizeof(*cmd));
@@ -950,15 +851,13 @@ static int iommu_queue_command_sync(struct amd_iommu *iommu,
        u32 left, tail, head, next_tail;
        unsigned long flags;
 
-       WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED);
-
 again:
        spin_lock_irqsave(&iommu->lock, flags);
 
        head      = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
        tail      = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
-       next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size;
-       left      = (head - next_tail) % iommu->cmd_buf_size;
+       next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE;
+       left      = (head - next_tail) % CMD_BUFFER_SIZE;
 
        if (left <= 2) {
                struct iommu_cmd sync_cmd;
@@ -1114,11 +1013,15 @@ static int device_flush_iotlb(struct iommu_dev_data *dev_data,
 static int device_flush_dte(struct iommu_dev_data *dev_data)
 {
        struct amd_iommu *iommu;
+       u16 alias;
        int ret;
 
        iommu = amd_iommu_rlookup_table[dev_data->devid];
+       alias = amd_iommu_alias_table[dev_data->devid];
 
        ret = iommu_flush_dte(iommu, dev_data->devid);
+       if (!ret && alias != dev_data->devid)
+               ret = iommu_flush_dte(iommu, alias);
        if (ret)
                return ret;
 
@@ -1974,8 +1877,8 @@ static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
 static void clear_dte_entry(u16 devid)
 {
        /* remove entry from the device table seen by the hardware */
-       amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV;
-       amd_iommu_dev_table[devid].data[1] = 0;
+       amd_iommu_dev_table[devid].data[0]  = IOMMU_PTE_P | IOMMU_PTE_TV;
+       amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
 
        amd_iommu_apply_erratum_63(devid);
 }
@@ -1984,27 +1887,33 @@ static void do_attach(struct iommu_dev_data *dev_data,
                      struct protection_domain *domain)
 {
        struct amd_iommu *iommu;
+       u16 alias;
        bool ats;
 
        iommu = amd_iommu_rlookup_table[dev_data->devid];
+       alias = amd_iommu_alias_table[dev_data->devid];
        ats   = dev_data->ats.enabled;
 
        /* Update data structures */
        dev_data->domain = domain;
        list_add(&dev_data->list, &domain->dev_list);
-       set_dte_entry(dev_data->devid, domain, ats);
 
        /* Do reference counting */
        domain->dev_iommu[iommu->index] += 1;
        domain->dev_cnt                 += 1;
 
-       /* Flush the DTE entry */
+       /* Update device table */
+       set_dte_entry(dev_data->devid, domain, ats);
+       if (alias != dev_data->devid)
+               set_dte_entry(dev_data->devid, domain, ats);
+
        device_flush_dte(dev_data);
 }
 
 static void do_detach(struct iommu_dev_data *dev_data)
 {
        struct amd_iommu *iommu;
+       u16 alias;
 
        /*
         * First check if the device is still attached. It might already
@@ -2016,6 +1925,7 @@ static void do_detach(struct iommu_dev_data *dev_data)
                return;
 
        iommu = amd_iommu_rlookup_table[dev_data->devid];
+       alias = amd_iommu_alias_table[dev_data->devid];
 
        /* decrease reference counters */
        dev_data->domain->dev_iommu[iommu->index] -= 1;
@@ -2025,6 +1935,8 @@ static void do_detach(struct iommu_dev_data *dev_data)
        dev_data->domain = NULL;
        list_del(&dev_data->list);
        clear_dte_entry(dev_data->devid);
+       if (alias != dev_data->devid)
+               clear_dte_entry(alias);
 
        /* Flush the DTE entry */
        device_flush_dte(dev_data);
@@ -2037,29 +1949,23 @@ static void do_detach(struct iommu_dev_data *dev_data)
 static int __attach_device(struct iommu_dev_data *dev_data,
                           struct protection_domain *domain)
 {
-       struct iommu_dev_data *head, *entry;
        int ret;
 
+       /*
+        * Must be called with IRQs disabled. Warn here to detect early
+        * when its not.
+        */
+       WARN_ON(!irqs_disabled());
+
        /* lock domain */
        spin_lock(&domain->lock);
 
-       head = dev_data;
-
-       if (head->alias_data != NULL)
-               head = head->alias_data;
-
-       /* Now we have the root of the alias group, if any */
-
        ret = -EBUSY;
-       if (head->domain != NULL)
+       if (dev_data->domain != NULL)
                goto out_unlock;
 
        /* Attach alias group root */
-       do_attach(head, domain);
-
-       /* Attach other devices in the alias group */
-       list_for_each_entry(entry, &head->alias_list, alias_list)
-               do_attach(entry, domain);
+       do_attach(dev_data, domain);
 
        ret = 0;
 
@@ -2209,26 +2115,24 @@ static int attach_device(struct device *dev,
  */
 static void __detach_device(struct iommu_dev_data *dev_data)
 {
-       struct iommu_dev_data *head, *entry;
        struct protection_domain *domain;
-       unsigned long flags;
 
-       BUG_ON(!dev_data->domain);
-
-       domain = dev_data->domain;
+       /*
+        * Must be called with IRQs disabled. Warn here to detect early
+        * when its not.
+        */
+       WARN_ON(!irqs_disabled());
 
-       spin_lock_irqsave(&domain->lock, flags);
+       if (WARN_ON(!dev_data->domain))
+               return;
 
-       head = dev_data;
-       if (head->alias_data != NULL)
-               head = head->alias_data;
+       domain = dev_data->domain;
 
-       list_for_each_entry(entry, &head->alias_list, alias_list)
-               do_detach(entry);
+       spin_lock(&domain->lock);
 
-       do_detach(head);
+       do_detach(dev_data);
 
-       spin_unlock_irqrestore(&domain->lock, flags);
+       spin_unlock(&domain->lock);
 }
 
 /*
@@ -3198,6 +3102,7 @@ static const struct iommu_ops amd_iommu_ops = {
        .iova_to_phys = amd_iommu_iova_to_phys,
        .add_device = amd_iommu_add_device,
        .remove_device = amd_iommu_remove_device,
+       .device_group = pci_device_group,
        .get_dm_regions = amd_iommu_get_dm_regions,
        .put_dm_regions = amd_iommu_put_dm_regions,
        .pgsize_bitmap  = AMD_IOMMU_PGSIZES,