x86/amd-iommu: Use check_device for amd_iommu_dma_supported
[pandora-kernel.git] / arch / x86 / kernel / amd_iommu.c
index 0285521..ac27b1d 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);
-}
+       spin_lock_irqsave(&amd_iommu_pd_lock, flags);
 
-static void flush_all_domains_on_iommu(struct amd_iommu *iommu)
-{
-       int i;
-
-       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;
+       struct protection_domain *domain;
+       unsigned long flags;
 
-       INC_STATS_COUNTER(domain_flush_all);
+       spin_lock_irqsave(&amd_iommu_pd_lock, flags);
 
-       for_each_iommu(iommu)
-               flush_domain_on_iommu(iommu, domid);
-}
-
-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)
@@ -928,7 +941,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 +986,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 +1099,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,6 +1141,8 @@ static struct dma_ops_domain *dma_ops_domain_alloc(struct amd_iommu *iommu)
        dma_dom->need_flush = false;
        dma_dom->target_dev = 0xffff;
 
+       add_domain_to_list(&dma_dom->domain);
+
        if (alloc_new_range(iommu, dma_dom, true, GFP_KERNEL))
                goto free_dma_dom;
 
@@ -1174,7 +1216,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 +1244,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 +1252,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 +1269,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 +1375,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;
 }
 
@@ -1432,7 +1498,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;
 }
@@ -1544,7 +1610,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;
 
@@ -1625,7 +1691,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
@@ -1646,7 +1712,7 @@ 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)
+               if (ret == DMA_ERROR_CODE)
                        goto out_unmap;
 
                paddr += PAGE_SIZE;
@@ -1657,10 +1723,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;
@@ -1674,7 +1740,7 @@ out_unmap:
 
        dma_ops_free_addresses(dma_dom, address, pages);
 
-       return bad_dma_address;
+       return DMA_ERROR_CODE;
 }
 
 /*
@@ -1690,7 +1756,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;
 
@@ -1708,7 +1774,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;
        }
 }
@@ -1732,7 +1798,7 @@ 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;
+               return DMA_ERROR_CODE;
 
        dma_mask = *dev->dma_mask;
 
@@ -1743,15 +1809,15 @@ static dma_addr_t map_page(struct device *dev, struct page *page,
                return (dma_addr_t)paddr;
 
        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);
@@ -1784,7 +1850,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);
 }
@@ -1856,7 +1922,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);
@@ -1907,7 +1973,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);
 }
@@ -1957,12 +2023,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);
 
@@ -2003,7 +2069,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 +2083,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 +2098,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 +2116,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 +2165,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 +2221,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 +2242,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 +2400,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,