Merge tag 'vfio-v6.3-rc1' of https://github.com/awilliam/linux-vfio
[platform/kernel/linux-rpi.git] / drivers / vfio / vfio_iommu_type1.c
index 29616f2..493c31d 100644 (file)
@@ -37,7 +37,6 @@
 #include <linux/vfio.h>
 #include <linux/workqueue.h>
 #include <linux/notifier.h>
-#include <linux/irqdomain.h>
 #include "vfio.h"
 
 #define DRIVER_VERSION  "0.2"
@@ -1428,7 +1427,8 @@ static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,
 
        list_for_each_entry(d, &iommu->domain_list, next) {
                ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,
-                               npage << PAGE_SHIFT, prot | IOMMU_CACHE);
+                               npage << PAGE_SHIFT, prot | IOMMU_CACHE,
+                               GFP_KERNEL);
                if (ret)
                        goto unwind;
 
@@ -1741,8 +1741,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu,
                                size = npage << PAGE_SHIFT;
                        }
 
-                       ret = iommu_map(domain->domain, iova, phys,
-                                       size, dma->prot | IOMMU_CACHE);
+                       ret = iommu_map(domain->domain, iova, phys, size,
+                                       dma->prot | IOMMU_CACHE, GFP_KERNEL);
                        if (ret) {
                                if (!dma->iommu_mapped) {
                                        vfio_unpin_pages_remote(dma, iova,
@@ -1820,24 +1820,33 @@ unwind:
  * significantly boosts non-hugetlbfs mappings and doesn't seem to hurt when
  * hugetlbfs is in use.
  */
-static void vfio_test_domain_fgsp(struct vfio_domain *domain)
+static void vfio_test_domain_fgsp(struct vfio_domain *domain, struct list_head *regions)
 {
-       struct page *pages;
        int ret, order = get_order(PAGE_SIZE * 2);
+       struct vfio_iova *region;
+       struct page *pages;
+       dma_addr_t start;
 
        pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
        if (!pages)
                return;
 
-       ret = iommu_map(domain->domain, 0, page_to_phys(pages), PAGE_SIZE * 2,
-                       IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE);
-       if (!ret) {
-               size_t unmapped = iommu_unmap(domain->domain, 0, PAGE_SIZE);
+       list_for_each_entry(region, regions, list) {
+               start = ALIGN(region->start, PAGE_SIZE * 2);
+               if (start >= region->end || (region->end - start < PAGE_SIZE * 2))
+                       continue;
 
-               if (unmapped == PAGE_SIZE)
-                       iommu_unmap(domain->domain, PAGE_SIZE, PAGE_SIZE);
-               else
-                       domain->fgsp = true;
+               ret = iommu_map(domain->domain, start, page_to_phys(pages), PAGE_SIZE * 2,
+                               IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE, GFP_KERNEL);
+               if (!ret) {
+                       size_t unmapped = iommu_unmap(domain->domain, start, PAGE_SIZE);
+
+                       if (unmapped == PAGE_SIZE)
+                               iommu_unmap(domain->domain, start + PAGE_SIZE, PAGE_SIZE);
+                       else
+                               domain->fgsp = true;
+               }
+               break;
        }
 
        __free_pages(pages, order);
@@ -2124,12 +2133,6 @@ static void vfio_iommu_iova_insert_copy(struct vfio_iommu *iommu,
        list_splice_tail(iova_copy, iova);
 }
 
-/* Redundantly walks non-present capabilities to simplify caller */
-static int vfio_iommu_device_capable(struct device *dev, void *data)
-{
-       return device_iommu_capable(dev, (enum iommu_cap)data);
-}
-
 static int vfio_iommu_domain_alloc(struct device *dev, void *data)
 {
        struct iommu_domain **domain = data;
@@ -2144,7 +2147,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
        struct vfio_iommu *iommu = iommu_data;
        struct vfio_iommu_group *group;
        struct vfio_domain *domain, *d;
-       bool resv_msi, msi_remap;
+       bool resv_msi;
        phys_addr_t resv_msi_base = 0;
        struct iommu_domain_geometry *geo;
        LIST_HEAD(iova_copy);
@@ -2247,11 +2250,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
        INIT_LIST_HEAD(&domain->group_list);
        list_add(&group->next, &domain->group_list);
 
-       msi_remap = irq_domain_check_msi_remap() ||
-                   iommu_group_for_each_dev(iommu_group, (void *)IOMMU_CAP_INTR_REMAP,
-                                            vfio_iommu_device_capable);
-
-       if (!allow_unsafe_interrupts && !msi_remap) {
+       if (!allow_unsafe_interrupts &&
+           !iommu_group_has_isolated_msi(iommu_group)) {
                pr_warn("%s: No interrupt remapping support.  Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
                       __func__);
                ret = -EPERM;
@@ -2295,7 +2295,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
                }
        }
 
-       vfio_test_domain_fgsp(domain);
+       vfio_test_domain_fgsp(domain, &iova_copy);
 
        /* replay mappings on new domains */
        ret = vfio_iommu_replay(iommu, domain);