]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/iommu/amd_iommu.c
Merge branches 'arm/exynos', 'arm/renesas', 'arm/rockchip', 'arm/omap', 'arm/mediatek...
[mirror_ubuntu-bionic-kernel.git] / drivers / iommu / amd_iommu.c
index 5a61cf5a4443b61426d5d33b2e8344a1b51cfe51..538c16f669f9bf7cb8fda0491c9c02b63f483a2b 100644 (file)
@@ -102,29 +102,6 @@ int amd_iommu_max_glx_val = -1;
 
 static const struct dma_map_ops amd_iommu_dma_ops;
 
-/*
- * This struct contains device specific data for the IOMMU
- */
-struct iommu_dev_data {
-       struct list_head list;            /* For domain->dev_list */
-       struct list_head dev_data_list;   /* For global dev_data_list */
-       struct protection_domain *domain; /* Domain the device is bound to */
-       u16 devid;                        /* PCI Device ID */
-       u16 alias;                        /* Alias Device ID */
-       bool iommu_v2;                    /* Device can make use of IOMMUv2 */
-       bool passthrough;                 /* Device is identity mapped */
-       struct {
-               bool enabled;
-               int qdep;
-       } ats;                            /* ATS state */
-       bool pri_tlp;                     /* PASID TLB required for
-                                            PPR completions */
-       u32 errata;                       /* Bitmap for errata to apply */
-       bool use_vapic;                   /* Enable device to use vapic mode */
-
-       struct ratelimit_state rs;        /* Ratelimit IOPF messages */
-};
-
 /*
  * general struct to manage commands send to an IOMMU
  */
@@ -328,19 +305,25 @@ static u16 get_alias(struct device *dev)
 static struct iommu_dev_data *find_dev_data(u16 devid)
 {
        struct iommu_dev_data *dev_data;
+       struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
 
        dev_data = search_dev_data(devid);
 
-       if (dev_data == NULL)
+       if (dev_data == NULL) {
                dev_data = alloc_dev_data(devid);
 
+               if (translation_pre_enabled(iommu))
+                       dev_data->defer_attach = true;
+       }
+
        return dev_data;
 }
 
-static struct iommu_dev_data *get_dev_data(struct device *dev)
+struct iommu_dev_data *get_dev_data(struct device *dev)
 {
        return dev->archdata.iommu;
 }
+EXPORT_SYMBOL(get_dev_data);
 
 /*
 * Find or create an IOMMU group for a acpihid device.
@@ -1122,7 +1105,7 @@ static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid)
        return iommu_queue_command(iommu, &cmd);
 }
 
-static void iommu_flush_dte_all(struct amd_iommu *iommu)
+static void amd_iommu_flush_dte_all(struct amd_iommu *iommu)
 {
        u32 devid;
 
@@ -1136,7 +1119,7 @@ static void iommu_flush_dte_all(struct amd_iommu *iommu)
  * 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_tlb_all(struct amd_iommu *iommu)
+static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu)
 {
        u32 dom_id;
 
@@ -1150,7 +1133,7 @@ static void iommu_flush_tlb_all(struct amd_iommu *iommu)
        iommu_completion_wait(iommu);
 }
 
-static void iommu_flush_all(struct amd_iommu *iommu)
+static void amd_iommu_flush_all(struct amd_iommu *iommu)
 {
        struct iommu_cmd cmd;
 
@@ -1169,7 +1152,7 @@ static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid)
        iommu_queue_command(iommu, &cmd);
 }
 
-static void iommu_flush_irt_all(struct amd_iommu *iommu)
+static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
 {
        u32 devid;
 
@@ -1182,11 +1165,11 @@ static void iommu_flush_irt_all(struct amd_iommu *iommu)
 void iommu_flush_all_caches(struct amd_iommu *iommu)
 {
        if (iommu_feature(iommu, FEATURE_IA)) {
-               iommu_flush_all(iommu);
+               amd_iommu_flush_all(iommu);
        } else {
-               iommu_flush_dte_all(iommu);
-               iommu_flush_irt_all(iommu);
-               iommu_flush_tlb_all(iommu);
+               amd_iommu_flush_dte_all(iommu);
+               amd_iommu_flush_irt_all(iommu);
+               amd_iommu_flush_tlb_all(iommu);
        }
 }
 
@@ -1494,9 +1477,9 @@ static int iommu_map_page(struct protection_domain *dom,
 
        if (count > 1) {
                __pte = PAGE_SIZE_PTE(phys_addr, page_size);
-               __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC;
+               __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC;
        } else
-               __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC;
+               __pte = phys_addr | IOMMU_PTE_PR | IOMMU_PTE_FC;
 
        if (prot & IOMMU_PROT_IR)
                __pte |= IOMMU_PTE_IR;
@@ -1841,7 +1824,7 @@ static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
 
        pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK)
                    << DEV_ENTRY_MODE_SHIFT;
-       pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV;
+       pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV;
 
        flags = amd_iommu_dev_table[devid].data[1];
 
@@ -1874,8 +1857,7 @@ static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats)
                flags    |= tmp;
        }
 
-
-       flags &= ~(DTE_FLAG_SA | 0xffffULL);
+       flags &= ~DEV_DOMID_MASK;
        flags |= domain->id;
 
        amd_iommu_dev_table[devid].data[1]  = flags;
@@ -1885,7 +1867,7 @@ 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[0]  = DTE_FLAG_V | DTE_FLAG_TV;
        amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK;
 
        amd_iommu_apply_erratum_63(devid);
@@ -2266,11 +2248,21 @@ static struct iommu_group *amd_iommu_device_group(struct device *dev)
 static struct protection_domain *get_domain(struct device *dev)
 {
        struct protection_domain *domain;
+       struct iommu_domain *io_domain;
 
        if (!check_device(dev))
                return ERR_PTR(-EINVAL);
 
        domain = get_dev_data(dev)->domain;
+       if (domain == NULL && get_dev_data(dev)->defer_attach) {
+               get_dev_data(dev)->defer_attach = false;
+               io_domain = iommu_get_domain_for_dev(dev);
+               domain = to_pdomain(io_domain);
+               attach_device(dev, domain);
+       }
+       if (domain == NULL)
+               return ERR_PTR(-EBUSY);
+
        if (!dma_ops_domain(domain))
                return ERR_PTR(-EBUSY);
 
@@ -2316,6 +2308,7 @@ static int dir2prot(enum dma_data_direction direction)
        else
                return 0;
 }
+
 /*
  * This function contains common code for mapping of a physically
  * contiguous memory region into DMA address space. It is used by all
@@ -3162,6 +3155,13 @@ static void amd_iommu_apply_resv_region(struct device *dev,
        WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL);
 }
 
+static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain,
+                                        struct device *dev)
+{
+       struct iommu_dev_data *dev_data = dev->archdata.iommu;
+       return dev_data->defer_attach;
+}
+
 const struct iommu_ops amd_iommu_ops = {
        .capable = amd_iommu_capable,
        .domain_alloc = amd_iommu_domain_alloc,
@@ -3178,6 +3178,7 @@ const struct iommu_ops amd_iommu_ops = {
        .get_resv_regions = amd_iommu_get_resv_regions,
        .put_resv_regions = amd_iommu_put_resv_regions,
        .apply_resv_region = amd_iommu_apply_resv_region,
+       .is_attach_deferred = amd_iommu_is_attach_deferred,
        .pgsize_bitmap  = AMD_IOMMU_PGSIZES,
 };
 
@@ -3566,11 +3567,6 @@ EXPORT_SYMBOL(amd_iommu_device_info);
 
 static struct irq_chip amd_ir_chip;
 
-#define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
-#define DTE_IRQ_REMAP_INTCTL    (2ULL << 60)
-#define DTE_IRQ_TABLE_LEN       (8ULL << 1)
-#define DTE_IRQ_REMAP_ENABLE    1ULL
-
 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table)
 {
        u64 dte;