drm/rockchip: fix iommu page fault when use boot logo
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
index e2a935de39bf2a63950a7074b3740ec69cf1858d..5b4f1daa45e9704697fe0e99f8c30fcfd5ce4a53 100644 (file)
@@ -14,8 +14,6 @@
  * GNU General Public License for more details.
  */
 
-#include <asm/dma-iommu.h>
-
 #include <drm/drmP.h>
 #include <drm/drm_atomic.h>
 #include <drm/drm_crtc_helper.h>
@@ -23,6 +21,7 @@
 #include <drm/drm_sync_helper.h>
 #include <drm/rockchip_drm.h>
 #include <linux/dma-mapping.h>
+#include <linux/dma-iommu.h>
 #include <linux/pm_runtime.h>
 #include <linux/memblock.h>
 #include <linux/module.h>
@@ -31,6 +30,9 @@
 #include <linux/component.h>
 #include <linux/fence.h>
 #include <linux/console.h>
+#include <linux/iommu.h>
+
+#include <drm/rockchip_drm.h>
 
 #include "rockchip_drm_drv.h"
 #include "rockchip_drm_fb.h"
@@ -44,6 +46,8 @@
 #define DRIVER_MAJOR   1
 #define DRIVER_MINOR   0
 
+static bool is_support_iommu = true;
+
 static LIST_HEAD(rockchip_drm_subdrv_list);
 static DEFINE_MUTEX(subdrv_list_mutex);
 
@@ -107,7 +111,6 @@ static int init_loader_memory(struct drm_device *drm_dev)
        unsigned long nr_pages;
        struct page **pages;
        struct sg_table *sgt;
-       DEFINE_DMA_ATTRS(attrs);
        phys_addr_t start, size;
        struct resource res;
        int i, ret;
@@ -140,15 +143,35 @@ static int init_loader_memory(struct drm_device *drm_dev)
        }
        sgt = drm_prime_pages_to_sg(pages, nr_pages);
        if (IS_ERR(sgt)) {
-               kfree(pages);
-               return PTR_ERR(sgt);
+               ret = PTR_ERR(sgt);
+               goto err_free_pages;
+       }
+
+       if (private->domain) {
+               int prot = IOMMU_READ | IOMMU_WRITE;
+
+               memset(&logo->mm, 0, sizeof(logo->mm));
+               ret = drm_mm_insert_node_generic(&private->mm, &logo->mm,
+                                                size, PAGE_SIZE,
+                                                0, 0, 0);
+               if (ret < 0) {
+                       DRM_ERROR("out of I/O virtual memory: %d\n", ret);
+                       goto err_free_pages;
+               }
+
+               logo->dma_addr = logo->mm.start;
+
+               if (iommu_map_sg(private->domain, logo->dma_addr, sgt->sgl,
+                                sgt->nents, prot) < size) {
+                       DRM_ERROR("failed to map buffer");
+                       ret = -ENOMEM;
+                       goto err_remove_node;
+               }
+       } else {
+               dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE);
+               logo->dma_addr = sg_dma_address(sgt->sgl);
        }
 
-       dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
-       dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
-       dma_map_sg_attrs(drm_dev->dev, sgt->sgl, sgt->nents,
-                        DMA_TO_DEVICE, &attrs);
-       logo->dma_addr = sg_dma_address(sgt->sgl);
        logo->sgt = sgt;
        logo->start = res.start;
        logo->size = size;
@@ -156,6 +179,13 @@ static int init_loader_memory(struct drm_device *drm_dev)
        private->logo = logo;
 
        return 0;
+
+err_remove_node:
+       drm_mm_remove_node(&logo->mm);
+err_free_pages:
+       kfree(pages);
+
+       return ret;
 }
 
 static struct drm_framebuffer *
@@ -614,22 +644,31 @@ err_unlock:
 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
                                   struct device *dev)
 {
-       struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
+       struct rockchip_drm_private *private = drm_dev->dev_private;
        int ret;
 
-       ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
-       if (ret)
-               return ret;
+       if (!is_support_iommu)
+               return 0;
 
-       dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
+       ret = iommu_attach_device(private->domain, dev);
+       if (ret) {
+               dev_err(dev, "Failed to attach iommu device\n");
+               return ret;
+       }
 
-       return arm_iommu_attach_device(dev, mapping);
+       return 0;
 }
 
 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
                                    struct device *dev)
 {
-       arm_iommu_detach_device(dev);
+       struct rockchip_drm_private *private = drm_dev->dev_private;
+       struct iommu_domain *domain = private->domain;
+
+       if (!is_support_iommu)
+               return;
+
+       iommu_detach_device(domain, dev);
 }
 
 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
@@ -694,10 +733,44 @@ static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
                priv->crtc_funcs[pipe]->disable_vblank(crtc);
 }
 
+static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
+{
+       struct rockchip_drm_private *private = drm_dev->dev_private;
+       struct iommu_domain_geometry *geometry;
+       u64 start, end;
+
+       if (!is_support_iommu)
+               return 0;
+
+       private->domain = iommu_domain_alloc(&platform_bus_type);
+       if (!private->domain)
+               return -ENOMEM;
+
+       geometry = &private->domain->geometry;
+       start = geometry->aperture_start;
+       end = geometry->aperture_end;
+
+       DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
+                 start, end);
+       drm_mm_init(&private->mm, start, end - start + 1);
+
+       return 0;
+}
+
+static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
+{
+       struct rockchip_drm_private *private = drm_dev->dev_private;
+
+       if (!is_support_iommu)
+               return;
+
+       drm_mm_takedown(&private->mm);
+       iommu_domain_free(private->domain);
+}
+
 static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
 {
        struct rockchip_drm_private *private;
-       struct dma_iommu_mapping *mapping;
        struct device *dev = drm_dev->dev;
        struct drm_connector *connector;
        int ret;
@@ -720,35 +793,14 @@ static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
 
        rockchip_drm_mode_config_init(drm_dev);
 
-       dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
-                                     GFP_KERNEL);
-       if (!dev->dma_parms) {
-               ret = -ENOMEM;
-               goto err_config_cleanup;
-       }
-
-       /* TODO(djkurtz): fetch the mapping start/size from somewhere */
-       mapping = arm_iommu_create_mapping(&platform_bus_type, 0x00000000,
-                                          SZ_2G);
-       if (IS_ERR(mapping)) {
-               ret = PTR_ERR(mapping);
-               goto err_config_cleanup;
-       }
-
-       ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+       ret = rockchip_drm_init_iommu(drm_dev);
        if (ret)
-               goto err_release_mapping;
-
-       dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
-
-       ret = arm_iommu_attach_device(dev, mapping);
-       if (ret)
-               goto err_release_mapping;
+               goto err_config_cleanup;
 
        /* Try to bind all sub drivers. */
        ret = component_bind_all(dev, drm_dev);
        if (ret)
-               goto err_detach_device;
+               goto err_iommu_cleanup;
 
        /*
         * All components are now added, we can publish the connector sysfs
@@ -804,10 +856,8 @@ err_kms_helper_poll_fini:
        drm_kms_helper_poll_fini(drm_dev);
 err_unbind:
        component_unbind_all(dev, drm_dev);
-err_detach_device:
-       arm_iommu_detach_device(dev);
-err_release_mapping:
-       arm_iommu_release_mapping(dev->archdata.mapping);
+err_iommu_cleanup:
+       rockchip_iommu_cleanup(drm_dev);
 err_config_cleanup:
        drm_mode_config_cleanup(drm_dev);
        drm_dev->dev_private = NULL;
@@ -822,8 +872,7 @@ static int rockchip_drm_unload(struct drm_device *drm_dev)
        drm_vblank_cleanup(drm_dev);
        drm_kms_helper_poll_fini(drm_dev);
        component_unbind_all(dev, drm_dev);
-       arm_iommu_detach_device(dev);
-       arm_iommu_release_mapping(dev->archdata.mapping);
+       rockchip_iommu_cleanup(drm_dev);
        drm_mode_config_cleanup(drm_dev);
        drm_dev->dev_private = NULL;
 
@@ -1163,6 +1212,8 @@ static int rockchip_drm_platform_probe(struct platform_device *pdev)
         * works as expected.
         */
        for (i = 0;; i++) {
+               struct device_node *iommu;
+
                port = of_parse_phandle(np, "ports", i);
                if (!port)
                        break;
@@ -1172,6 +1223,17 @@ static int rockchip_drm_platform_probe(struct platform_device *pdev)
                        continue;
                }
 
+               iommu = of_parse_phandle(port->parent, "iommus", 0);
+               if (!iommu || !of_device_is_available(iommu->parent)) {
+                       dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
+                               port->parent->full_name);
+                       /*
+                        * if there is a crtc not support iommu, force set all
+                        * crtc use non-iommu buffer.
+                        */
+                       is_support_iommu = false;
+               }
+
                component_match_add(dev, &match, compare_of, port->parent);
                of_node_put(port);
        }