x86/amd-iommu: Make alloc_new_range aware of multiple IOMMUs
[deliverable/linux.git] / arch / x86 / kernel / amd_iommu.c
index 0285521e0a991d09dbf3fd01e85489f068083d3f..687f617b95d7684b04abfca545a63a05cea877c4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
+ * Copyright (C) 2007-2009 Advanced Micro Devices, Inc.
  * Author: Joerg Roedel <joerg.roedel@amd.com>
  *         Leo Duran <leo.duran@amd.com>
  *
@@ -28,6 +28,7 @@
 #include <asm/proto.h>
 #include <asm/iommu.h>
 #include <asm/gart.h>
+#include <asm/amd_iommu_proto.h>
 #include <asm/amd_iommu_types.h>
 #include <asm/amd_iommu.h>
 
@@ -130,12 +131,6 @@ static void amd_iommu_stats_init(void)
 
 #endif
 
-/* returns !0 if the IOMMU is caching non-present entries in its TLB */
-static int iommu_has_npcache(struct amd_iommu *iommu)
-{
-       return iommu->cap & (1UL << IOMMU_CAP_NPCACHE);
-}
-
 /****************************************************************************
  *
  * Interrupt handling functions
@@ -375,6 +370,22 @@ out:
        return 0;
 }
 
+static void iommu_flush_complete(struct protection_domain *domain)
+{
+       int i;
+
+       for (i = 0; i < amd_iommus_present; ++i) {
+               if (!domain->dev_iommu[i])
+                       continue;
+
+               /*
+                * Devices of this domain are behind this IOMMU
+                * We need to wait for completion of all commands.
+                */
+               iommu_completion_wait(amd_iommus[i]);
+       }
+}
+
 /*
  * Command send function for invalidating a device table entry
  */
@@ -430,11 +441,11 @@ static int iommu_queue_inv_iommu_pages(struct amd_iommu *iommu,
  * It invalidates a single PTE if the range to flush is within a single
  * page. Otherwise it flushes the whole TLB of the IOMMU.
  */
-static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
-               u64 address, size_t size)
+static void __iommu_flush_pages(struct protection_domain *domain,
+                               u64 address, size_t size, int pde)
 {
-       int s = 0;
-       unsigned pages = iommu_num_pages(address, size, PAGE_SIZE);
+       int s = 0, i;
+       unsigned long pages = iommu_num_pages(address, size, PAGE_SIZE);
 
        address &= PAGE_MASK;
 
@@ -447,81 +458,83 @@ static int iommu_flush_pages(struct amd_iommu *iommu, u16 domid,
                s = 1;
        }
 
-       iommu_queue_inv_iommu_pages(iommu, address, domid, 0, s);
 
-       return 0;
+       for (i = 0; i < amd_iommus_present; ++i) {
+               if (!domain->dev_iommu[i])
+                       continue;
+
+               /*
+                * Devices of this domain are behind this IOMMU
+                * We need a TLB flush
+                */
+               iommu_queue_inv_iommu_pages(amd_iommus[i], address,
+                                           domain->id, pde, s);
+       }
+
+       return;
 }
 
-/* Flush the whole IO/TLB for a given protection domain */
-static void iommu_flush_tlb(struct amd_iommu *iommu, u16 domid)
+static void iommu_flush_pages(struct protection_domain *domain,
+                            u64 address, size_t size)
 {
-       u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
-
-       INC_STATS_COUNTER(domain_flush_single);
+       __iommu_flush_pages(domain, address, size, 0);
+}
 
-       iommu_queue_inv_iommu_pages(iommu, address, domid, 0, 1);
+/* Flush the whole IO/TLB for a given protection domain */
+static void iommu_flush_tlb(struct protection_domain *domain)
+{
+       __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0);
 }
 
 /* Flush the whole IO/TLB for a given protection domain - including PDE */
-static void iommu_flush_tlb_pde(struct amd_iommu *iommu, u16 domid)
+static void iommu_flush_tlb_pde(struct protection_domain *domain)
 {
-       u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
-
-       INC_STATS_COUNTER(domain_flush_single);
-
-       iommu_queue_inv_iommu_pages(iommu, address, domid, 1, 1);
+       __iommu_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1);
 }
 
 /*
- * This function flushes one domain on one IOMMU
+ * This function flushes all domains that have devices on the given IOMMU
  */
-static void flush_domain_on_iommu(struct amd_iommu *iommu, u16 domid)
+static void flush_all_domains_on_iommu(struct amd_iommu *iommu)
 {
-       struct iommu_cmd cmd;
+       u64 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS;
+       struct protection_domain *domain;
        unsigned long flags;
 
-       __iommu_build_inv_iommu_pages(&cmd, CMD_INV_IOMMU_ALL_PAGES_ADDRESS,
-                                     domid, 1, 1);
-
-       spin_lock_irqsave(&iommu->lock, flags);
-       __iommu_queue_command(iommu, &cmd);
-       __iommu_completion_wait(iommu);
-       __iommu_wait_for_completion(iommu);
-       spin_unlock_irqrestore(&iommu->lock, flags);
-}
-
-static void flush_all_domains_on_iommu(struct amd_iommu *iommu)
-{
-       int i;
+       spin_lock_irqsave(&amd_iommu_pd_lock, flags);
 
-       for (i = 1; i < MAX_DOMAIN_ID; ++i) {
-               if (!test_bit(i, amd_iommu_pd_alloc_bitmap))
+       list_for_each_entry(domain, &amd_iommu_pd_list, list) {
+               if (domain->dev_iommu[iommu->index] == 0)
                        continue;
-               flush_domain_on_iommu(iommu, i);
+
+               spin_lock(&domain->lock);
+               iommu_queue_inv_iommu_pages(iommu, address, domain->id, 1, 1);
+               iommu_flush_complete(domain);
+               spin_unlock(&domain->lock);
        }
 
+       spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
 }
 
 /*
- * This function is used to flush the IO/TLB for a given protection domain
- * on every IOMMU in the system
+ * This function uses heavy locking and may disable irqs for some time. But
+ * this is no issue because it is only called during resume.
  */
-static void iommu_flush_domain(u16 domid)
+void amd_iommu_flush_all_domains(void)
 {
-       struct amd_iommu *iommu;
-
-       INC_STATS_COUNTER(domain_flush_all);
+       struct protection_domain *domain;
+       unsigned long flags;
 
-       for_each_iommu(iommu)
-               flush_domain_on_iommu(iommu, domid);
-}
+       spin_lock_irqsave(&amd_iommu_pd_lock, flags);
 
-void amd_iommu_flush_all_domains(void)
-{
-       struct amd_iommu *iommu;
+       list_for_each_entry(domain, &amd_iommu_pd_list, list) {
+               spin_lock(&domain->lock);
+               iommu_flush_tlb_pde(domain);
+               iommu_flush_complete(domain);
+               spin_unlock(&domain->lock);
+       }
 
-       for_each_iommu(iommu)
-               flush_all_domains_on_iommu(iommu);
+       spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
 }
 
 static void flush_all_devices_for_iommu(struct amd_iommu *iommu)
@@ -775,11 +788,11 @@ static u64 *fetch_pte(struct protection_domain *domain,
  * aperture in case of dma_ops domain allocation or address allocation
  * failure.
  */
-static int alloc_new_range(struct amd_iommu *iommu,
-                          struct dma_ops_domain *dma_dom,
+static int alloc_new_range(struct dma_ops_domain *dma_dom,
                           bool populate, gfp_t gfp)
 {
        int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT;
+       struct amd_iommu *iommu;
        int i;
 
 #ifdef CONFIG_IOMMU_STRESS
@@ -819,14 +832,17 @@ static int alloc_new_range(struct amd_iommu *iommu,
        dma_dom->aperture_size += APERTURE_RANGE_SIZE;
 
        /* Intialize the exclusion range if necessary */
-       if (iommu->exclusion_start &&
-           iommu->exclusion_start >= dma_dom->aperture[index]->offset &&
-           iommu->exclusion_start < dma_dom->aperture_size) {
-               unsigned long startpage = iommu->exclusion_start >> PAGE_SHIFT;
-               int pages = iommu_num_pages(iommu->exclusion_start,
-                                           iommu->exclusion_length,
-                                           PAGE_SIZE);
-               dma_ops_reserve_addresses(dma_dom, startpage, pages);
+       for_each_iommu(iommu) {
+               if (iommu->exclusion_start &&
+                   iommu->exclusion_start >= dma_dom->aperture[index]->offset
+                   && iommu->exclusion_start < dma_dom->aperture_size) {
+                       unsigned long startpage;
+                       int pages = iommu_num_pages(iommu->exclusion_start,
+                                                   iommu->exclusion_length,
+                                                   PAGE_SIZE);
+                       startpage = iommu->exclusion_start >> PAGE_SHIFT;
+                       dma_ops_reserve_addresses(dma_dom, startpage, pages);
+               }
        }
 
        /*
@@ -928,7 +944,7 @@ static unsigned long dma_ops_alloc_addresses(struct device *dev,
        }
 
        if (unlikely(address == -1))
-               address = bad_dma_address;
+               address = DMA_ERROR_CODE;
 
        WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size);
 
@@ -973,6 +989,31 @@ static void dma_ops_free_addresses(struct dma_ops_domain *dom,
  *
  ****************************************************************************/
 
+/*
+ * This function adds a protection domain to the global protection domain list
+ */
+static void add_domain_to_list(struct protection_domain *domain)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&amd_iommu_pd_lock, flags);
+       list_add(&domain->list, &amd_iommu_pd_list);
+       spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
+}
+
+/*
+ * This function removes a protection domain to the global
+ * protection domain list
+ */
+static void del_domain_from_list(struct protection_domain *domain)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&amd_iommu_pd_lock, flags);
+       list_del(&domain->list);
+       spin_unlock_irqrestore(&amd_iommu_pd_lock, flags);
+}
+
 static u16 domain_id_alloc(void)
 {
        unsigned long flags;
@@ -1061,6 +1102,8 @@ static void dma_ops_domain_free(struct dma_ops_domain *dom)
        if (!dom)
                return;
 
+       del_domain_from_list(&dom->domain);
+
        free_pagetable(&dom->domain);
 
        for (i = 0; i < APERTURE_MAX_RANGES; ++i) {
@@ -1101,7 +1144,9 @@ static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu)
        dma_dom->need_flush = false;
        dma_dom->target_dev = 0xffff;
 
-       if (alloc_new_range(iommu, dma_dom, true, GFP_KERNEL))
+       add_domain_to_list(&dma_dom->domain);
+
+       if (alloc_new_range(dma_dom, true, GFP_KERNEL))
                goto free_dma_dom;
 
        /*
@@ -1174,7 +1219,9 @@ static void __attach_device(struct amd_iommu *iommu,
        /* update DTE entry */
        set_dte_entry(devid, domain);
 
-       domain->dev_cnt += 1;
+       /* Do reference counting */
+       domain->dev_iommu[iommu->index] += 1;
+       domain->dev_cnt                 += 1;
 
        /* ready */
        spin_unlock(&domain->lock);
@@ -1200,7 +1247,7 @@ static void attach_device(struct amd_iommu *iommu,
         * here to evict all dirty stuff.
         */
        iommu_queue_inv_dev_entry(iommu, devid);
-       iommu_flush_tlb_pde(iommu, domain->id);
+       iommu_flush_tlb_pde(domain);
 }
 
 /*
@@ -1208,6 +1255,9 @@ static void attach_device(struct amd_iommu *iommu,
  */
 static void __detach_device(struct protection_domain *domain, u16 devid)
 {
+       struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
+
+       BUG_ON(!iommu);
 
        /* lock domain */
        spin_lock(&domain->lock);
@@ -1222,8 +1272,9 @@ static void __detach_device(struct protection_domain *domain, u16 devid)
 
        amd_iommu_apply_erratum_63(devid);
 
-       /* decrease reference counter */
-       domain->dev_cnt -= 1;
+       /* decrease reference counters */
+       domain->dev_iommu[iommu->index] -= 1;
+       domain->dev_cnt                 -= 1;
 
        /* ready */
        spin_unlock(&domain->lock);
@@ -1327,9 +1378,27 @@ static struct notifier_block device_nb = {
  */
 static bool check_device(struct device *dev)
 {
+       u16 bdf;
+       struct pci_dev *pcidev;
+
        if (!dev || !dev->dma_mask)
                return false;
 
+       /* No device or no PCI device */
+       if (!dev || dev->bus != &pci_bus_type)
+               return false;
+
+       pcidev = to_pci_dev(dev);
+
+       bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
+
+       /* Out of our scope? */
+       if (bdf > amd_iommu_last_bdf)
+               return false;
+
+       if (amd_iommu_rlookup_table[bdf] == NULL)
+               return false;
+
        return true;
 }
 
@@ -1366,35 +1435,24 @@ static struct dma_ops_domain *find_protection_domain(u16 devid)
  * If the device is not yet associated with a domain this is also done
  * in this function.
  */
-static int get_device_resources(struct device *dev,
-                               struct amd_iommu **iommu,
-                               struct protection_domain **domain,
-                               u16 *bdf)
+static bool get_device_resources(struct device *dev,
+                                struct amd_iommu **iommu,
+                                struct protection_domain **domain,
+                                u16 *bdf)
 {
        struct dma_ops_domain *dma_dom;
        struct pci_dev *pcidev;
        u16 _bdf;
 
-       *iommu = NULL;
-       *domain = NULL;
-       *bdf = 0xffff;
-
-       if (dev->bus != &pci_bus_type)
-               return 0;
-
-       pcidev = to_pci_dev(dev);
-       _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
-
-       /* device not translated by any IOMMU in the system? */
-       if (_bdf > amd_iommu_last_bdf)
-               return 0;
-
-       *bdf = amd_iommu_alias_table[_bdf];
+       if (!check_device(dev))
+               return false;
 
-       *iommu = amd_iommu_rlookup_table[*bdf];
-       if (*iommu == NULL)
-               return 0;
+       pcidev  = to_pci_dev(dev);
+       _bdf    = calc_devid(pcidev->bus->number, pcidev->devfn);
+       *bdf    = amd_iommu_alias_table[_bdf];
+       *iommu  = amd_iommu_rlookup_table[*bdf];
        *domain = domain_for_device(*bdf);
+
        if (*domain == NULL) {
                dma_dom = find_protection_domain(*bdf);
                if (!dma_dom)
@@ -1408,7 +1466,7 @@ static int get_device_resources(struct device *dev,
        if (domain_for_device(_bdf) == NULL)
                attach_device(*iommu, *domain, _bdf);
 
-       return 1;
+       return true;
 }
 
 static void update_device_table(struct protection_domain *domain)
@@ -1432,7 +1490,7 @@ static void update_domain(struct protection_domain *domain)
 
        update_device_table(domain);
        flush_devices_by_domain(domain);
-       iommu_flush_domain(domain->id);
+       iommu_flush_tlb_pde(domain);
 
        domain->updated = false;
 }
@@ -1530,8 +1588,7 @@ static u64* dma_ops_get_pte(struct dma_ops_domain *dom,
  * This is the generic map function. It maps one 4kb page at paddr to
  * the given address in the DMA address space for the domain.
  */
-static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
-                                    struct dma_ops_domain *dom,
+static dma_addr_t dma_ops_domain_map(struct dma_ops_domain *dom,
                                     unsigned long address,
                                     phys_addr_t paddr,
                                     int direction)
@@ -1544,7 +1601,7 @@ static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
 
        pte  = dma_ops_get_pte(dom, address);
        if (!pte)
-               return bad_dma_address;
+               return DMA_ERROR_CODE;
 
        __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC;
 
@@ -1565,8 +1622,7 @@ static dma_addr_t dma_ops_domain_map(struct amd_iommu *iommu,
 /*
  * The generic unmapping function for on page in the DMA address space.
  */
-static void dma_ops_domain_unmap(struct amd_iommu *iommu,
-                                struct dma_ops_domain *dom,
+static void dma_ops_domain_unmap(struct dma_ops_domain *dom,
                                 unsigned long address)
 {
        struct aperture_range *aperture;
@@ -1625,7 +1681,7 @@ static dma_addr_t __map_single(struct device *dev,
 retry:
        address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask,
                                          dma_mask);
-       if (unlikely(address == bad_dma_address)) {
+       if (unlikely(address == DMA_ERROR_CODE)) {
                /*
                 * setting next_address here will let the address
                 * allocator only scan the new allocated range in the
@@ -1633,7 +1689,7 @@ retry:
                 */
                dma_dom->next_address = dma_dom->aperture_size;
 
-               if (alloc_new_range(iommu, dma_dom, false, GFP_ATOMIC))
+               if (alloc_new_range(dma_dom, false, GFP_ATOMIC))
                        goto out;
 
                /*
@@ -1645,8 +1701,8 @@ retry:
 
        start = address;
        for (i = 0; i < pages; ++i) {
-               ret = dma_ops_domain_map(iommu, dma_dom, start, paddr, dir);
-               if (ret == bad_dma_address)
+               ret = dma_ops_domain_map(dma_dom, start, paddr, dir);
+               if (ret == DMA_ERROR_CODE)
                        goto out_unmap;
 
                paddr += PAGE_SIZE;
@@ -1657,10 +1713,10 @@ retry:
        ADD_STATS_COUNTER(alloced_io_mem, size);
 
        if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) {
-               iommu_flush_tlb(iommu, dma_dom->domain.id);
+               iommu_flush_tlb(&dma_dom->domain);
                dma_dom->need_flush = false;
-       } else if (unlikely(iommu_has_npcache(iommu)))
-               iommu_flush_pages(iommu, dma_dom->domain.id, address, size);
+       } else if (unlikely(amd_iommu_np_cache))
+               iommu_flush_pages(&dma_dom->domain, address, size);
 
 out:
        return address;
@@ -1669,12 +1725,12 @@ out_unmap:
 
        for (--i; i >= 0; --i) {
                start -= PAGE_SIZE;
-               dma_ops_domain_unmap(iommu, dma_dom, start);
+               dma_ops_domain_unmap(dma_dom, start);
        }
 
        dma_ops_free_addresses(dma_dom, address, pages);
 
-       return bad_dma_address;
+       return DMA_ERROR_CODE;
 }
 
 /*
@@ -1690,7 +1746,7 @@ static void __unmap_single(struct amd_iommu *iommu,
        dma_addr_t i, start;
        unsigned int pages;
 
-       if ((dma_addr == bad_dma_address) ||
+       if ((dma_addr == DMA_ERROR_CODE) ||
            (dma_addr + size > dma_dom->aperture_size))
                return;
 
@@ -1699,7 +1755,7 @@ static void __unmap_single(struct amd_iommu *iommu,
        start = dma_addr;
 
        for (i = 0; i < pages; ++i) {
-               dma_ops_domain_unmap(iommu, dma_dom, start);
+               dma_ops_domain_unmap(dma_dom, start);
                start += PAGE_SIZE;
        }
 
@@ -1708,7 +1764,7 @@ static void __unmap_single(struct amd_iommu *iommu,
        dma_ops_free_addresses(dma_dom, dma_addr, pages);
 
        if (amd_iommu_unmap_flush || dma_dom->need_flush) {
-               iommu_flush_pages(iommu, dma_dom->domain.id, dma_addr, size);
+               iommu_flush_pages(&dma_dom->domain, dma_addr, size);
                dma_dom->need_flush = false;
        }
 }
@@ -1731,27 +1787,22 @@ static dma_addr_t map_page(struct device *dev, struct page *page,
 
        INC_STATS_COUNTER(cnt_map_single);
 
-       if (!check_device(dev))
-               return bad_dma_address;
-
-       dma_mask = *dev->dma_mask;
-
-       get_device_resources(dev, &iommu, &domain, &devid);
-
-       if (iommu == NULL || domain == NULL)
+       if (!get_device_resources(dev, &iommu, &domain, &devid))
                /* device not handled by any AMD IOMMU */
                return (dma_addr_t)paddr;
 
+       dma_mask = *dev->dma_mask;
+
        if (!dma_ops_domain(domain))
-               return bad_dma_address;
+               return DMA_ERROR_CODE;
 
        spin_lock_irqsave(&domain->lock, flags);
        addr = __map_single(dev, iommu, domain->priv, paddr, size, dir, false,
                            dma_mask);
-       if (addr == bad_dma_address)
+       if (addr == DMA_ERROR_CODE)
                goto out;
 
-       iommu_completion_wait(iommu);
+       iommu_flush_complete(domain);
 
 out:
        spin_unlock_irqrestore(&domain->lock, flags);
@@ -1772,8 +1823,7 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
 
        INC_STATS_COUNTER(cnt_unmap_single);
 
-       if (!check_device(dev) ||
-           !get_device_resources(dev, &iommu, &domain, &devid))
+       if (!get_device_resources(dev, &iommu, &domain, &devid))
                /* device not handled by any AMD IOMMU */
                return;
 
@@ -1784,7 +1834,7 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
 
        __unmap_single(iommu, domain->priv, dma_addr, size, dir);
 
-       iommu_completion_wait(iommu);
+       iommu_flush_complete(domain);
 
        spin_unlock_irqrestore(&domain->lock, flags);
 }
@@ -1827,16 +1877,11 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
 
        INC_STATS_COUNTER(cnt_map_sg);
 
-       if (!check_device(dev))
-               return 0;
+       if (!get_device_resources(dev, &iommu, &domain, &devid))
+               return map_sg_no_iommu(dev, sglist, nelems, dir);
 
        dma_mask = *dev->dma_mask;
 
-       get_device_resources(dev, &iommu, &domain, &devid);
-
-       if (!iommu || !domain)
-               return map_sg_no_iommu(dev, sglist, nelems, dir);
-
        if (!dma_ops_domain(domain))
                return 0;
 
@@ -1856,7 +1901,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
                        goto unmap;
        }
 
-       iommu_completion_wait(iommu);
+       iommu_flush_complete(domain);
 
 out:
        spin_unlock_irqrestore(&domain->lock, flags);
@@ -1892,8 +1937,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist,
 
        INC_STATS_COUNTER(cnt_unmap_sg);
 
-       if (!check_device(dev) ||
-           !get_device_resources(dev, &iommu, &domain, &devid))
+       if (!get_device_resources(dev, &iommu, &domain, &devid))
                return;
 
        if (!dma_ops_domain(domain))
@@ -1907,7 +1951,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist,
                s->dma_address = s->dma_length = 0;
        }
 
-       iommu_completion_wait(iommu);
+       iommu_flush_complete(domain);
 
        spin_unlock_irqrestore(&domain->lock, flags);
 }
@@ -1928,24 +1972,22 @@ static void *alloc_coherent(struct device *dev, size_t size,
 
        INC_STATS_COUNTER(cnt_alloc_coherent);
 
-       if (!check_device(dev))
-               return NULL;
+       if (!get_device_resources(dev, &iommu, &domain, &devid)) {
+               virt_addr = (void *)__get_free_pages(flag, get_order(size));
+               *dma_addr = __pa(virt_addr);
+               return virt_addr;
+       }
 
-       if (!get_device_resources(dev, &iommu, &domain, &devid))
-               flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
+       dma_mask  = dev->coherent_dma_mask;
+       flag     &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
+       flag     |= __GFP_ZERO;
 
-       flag |= __GFP_ZERO;
        virt_addr = (void *)__get_free_pages(flag, get_order(size));
        if (!virt_addr)
                return NULL;
 
        paddr = virt_to_phys(virt_addr);
 
-       if (!iommu || !domain) {
-               *dma_addr = (dma_addr_t)paddr;
-               return virt_addr;
-       }
-
        if (!dma_ops_domain(domain))
                goto out_free;
 
@@ -1957,12 +1999,12 @@ static void *alloc_coherent(struct device *dev, size_t size,
        *dma_addr = __map_single(dev, iommu, domain->priv, paddr,
                                 size, DMA_BIDIRECTIONAL, true, dma_mask);
 
-       if (*dma_addr == bad_dma_address) {
+       if (*dma_addr == DMA_ERROR_CODE) {
                spin_unlock_irqrestore(&domain->lock, flags);
                goto out_free;
        }
 
-       iommu_completion_wait(iommu);
+       iommu_flush_complete(domain);
 
        spin_unlock_irqrestore(&domain->lock, flags);
 
@@ -1988,12 +2030,7 @@ static void free_coherent(struct device *dev, size_t size,
 
        INC_STATS_COUNTER(cnt_free_coherent);
 
-       if (!check_device(dev))
-               return;
-
-       get_device_resources(dev, &iommu, &domain, &devid);
-
-       if (!iommu || !domain)
+       if (!get_device_resources(dev, &iommu, &domain, &devid))
                goto free_mem;
 
        if (!dma_ops_domain(domain))
@@ -2003,7 +2040,7 @@ static void free_coherent(struct device *dev, size_t size,
 
        __unmap_single(iommu, domain->priv, dma_addr, size, DMA_BIDIRECTIONAL);
 
-       iommu_completion_wait(iommu);
+       iommu_flush_complete(domain);
 
        spin_unlock_irqrestore(&domain->lock, flags);
 
@@ -2017,22 +2054,7 @@ free_mem:
  */
 static int amd_iommu_dma_supported(struct device *dev, u64 mask)
 {
-       u16 bdf;
-       struct pci_dev *pcidev;
-
-       /* No device or no PCI device */
-       if (!dev || dev->bus != &pci_bus_type)
-               return 0;
-
-       pcidev = to_pci_dev(dev);
-
-       bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
-
-       /* Out of our scope? */
-       if (bdf > amd_iommu_last_bdf)
-               return 0;
-
-       return 1;
+       return check_device(dev);
 }
 
 /*
@@ -2047,10 +2069,10 @@ static void prealloc_protection_domains(void)
        struct pci_dev *dev = NULL;
        struct dma_ops_domain *dma_dom;
        struct amd_iommu *iommu;
-       u16 devid;
+       u16 devid, __devid;
 
        while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
-               devid = calc_devid(dev->bus->number, dev->devfn);
+               __devid = devid = calc_devid(dev->bus->number, dev->devfn);
                if (devid > amd_iommu_last_bdf)
                        continue;
                devid = amd_iommu_alias_table[devid];
@@ -2065,6 +2087,10 @@ static void prealloc_protection_domains(void)
                init_unity_mappings_for_device(dma_dom, devid);
                dma_dom->target_dev = devid;
 
+               attach_device(iommu, &dma_dom->domain, devid);
+               if (__devid != devid)
+                       attach_device(iommu, &dma_dom->domain, __devid);
+
                list_add_tail(&dma_dom->list, &iommu_pd_list);
        }
 }
@@ -2110,8 +2136,7 @@ int __init amd_iommu_init_dma_ops(void)
                prealloc_protection_domains();
 
        iommu_detected = 1;
-       force_iommu = 1;
-       bad_dma_address = 0;
+       swiotlb = 0;
 #ifdef CONFIG_GART_IOMMU
        gart_iommu_aperture_disabled = 1;
        gart_iommu_aperture = 0;
@@ -2167,6 +2192,8 @@ static void protection_domain_free(struct protection_domain *domain)
        if (!domain)
                return;
 
+       del_domain_from_list(domain);
+
        if (domain->id)
                domain_id_free(domain->id);
 
@@ -2186,6 +2213,8 @@ static struct protection_domain *protection_domain_alloc(void)
        if (!domain->id)
                goto out_err;
 
+       add_domain_to_list(domain);
+
        return domain;
 
 out_err:
@@ -2342,7 +2371,7 @@ static void amd_iommu_unmap_range(struct iommu_domain *dom,
                iova  += PAGE_SIZE;
        }
 
-       iommu_flush_domain(domain->id);
+       iommu_flush_tlb_pde(domain);
 }
 
 static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
This page took 0.035466 seconds and 5 git commands to generate.