123755696SAndrzej Hajda // SPDX-License-Identifier: GPL-2.0 223755696SAndrzej Hajda // 323755696SAndrzej Hajda // Copyright (c) 2012 Samsung Electronics Co., Ltd. 467fbf3a3SAndrzej Hajda // Author: Inki Dae <inki.dae@samsung.com> 523755696SAndrzej Hajda // Author: Andrzej Hajda <a.hajda@samsung.com> 623755696SAndrzej Hajda 767fbf3a3SAndrzej Hajda #include <linux/dma-iommu.h> 867fbf3a3SAndrzej Hajda #include <linux/dma-mapping.h> 967fbf3a3SAndrzej Hajda #include <linux/iommu.h> 102bda34d7SSam Ravnborg #include <linux/platform_device.h> 112bda34d7SSam Ravnborg 122bda34d7SSam Ravnborg #include <drm/drm_print.h> 132bda34d7SSam Ravnborg #include <drm/exynos_drm.h> 1467fbf3a3SAndrzej Hajda 1523755696SAndrzej Hajda #include "exynos_drm_drv.h" 1667fbf3a3SAndrzej Hajda 1767fbf3a3SAndrzej Hajda #if defined(CONFIG_ARM_DMA_USE_IOMMU) 1867fbf3a3SAndrzej Hajda #include <asm/dma-iommu.h> 1967fbf3a3SAndrzej Hajda #else 2067fbf3a3SAndrzej Hajda #define arm_iommu_create_mapping(...) ({ NULL; }) 2167fbf3a3SAndrzej Hajda #define arm_iommu_attach_device(...) ({ -ENODEV; }) 2267fbf3a3SAndrzej Hajda #define arm_iommu_release_mapping(...) ({ }) 2367fbf3a3SAndrzej Hajda #define arm_iommu_detach_device(...) ({ }) 2467fbf3a3SAndrzej Hajda #define to_dma_iommu_mapping(dev) NULL 2567fbf3a3SAndrzej Hajda #endif 2667fbf3a3SAndrzej Hajda 2767fbf3a3SAndrzej Hajda #if !defined(CONFIG_IOMMU_DMA) 2867fbf3a3SAndrzej Hajda #define iommu_dma_init_domain(...) ({ -EINVAL; }) 2967fbf3a3SAndrzej Hajda #endif 3067fbf3a3SAndrzej Hajda 3167fbf3a3SAndrzej Hajda #define EXYNOS_DEV_ADDR_START 0x20000000 3267fbf3a3SAndrzej Hajda #define EXYNOS_DEV_ADDR_SIZE 0x40000000 3367fbf3a3SAndrzej Hajda 3467fbf3a3SAndrzej Hajda static inline int configure_dma_max_seg_size(struct device *dev) 3567fbf3a3SAndrzej Hajda { 3667fbf3a3SAndrzej Hajda if (!dev->dma_parms) 3767fbf3a3SAndrzej Hajda dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL); 3867fbf3a3SAndrzej Hajda if (!dev->dma_parms) 3967fbf3a3SAndrzej Hajda return -ENOMEM; 4067fbf3a3SAndrzej Hajda 4167fbf3a3SAndrzej Hajda dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); 4267fbf3a3SAndrzej Hajda return 0; 4367fbf3a3SAndrzej Hajda } 4467fbf3a3SAndrzej Hajda 4567fbf3a3SAndrzej Hajda static inline void clear_dma_max_seg_size(struct device *dev) 4667fbf3a3SAndrzej Hajda { 4767fbf3a3SAndrzej Hajda kfree(dev->dma_parms); 4867fbf3a3SAndrzej Hajda dev->dma_parms = NULL; 4967fbf3a3SAndrzej Hajda } 5067fbf3a3SAndrzej Hajda 5167fbf3a3SAndrzej Hajda /* 5267fbf3a3SAndrzej Hajda * drm_iommu_attach_device- attach device to iommu mapping 5367fbf3a3SAndrzej Hajda * 5467fbf3a3SAndrzej Hajda * @drm_dev: DRM device 5567fbf3a3SAndrzej Hajda * @subdrv_dev: device to be attach 5667fbf3a3SAndrzej Hajda * 5767fbf3a3SAndrzej Hajda * This function should be called by sub drivers to attach it to iommu 5867fbf3a3SAndrzej Hajda * mapping. 5967fbf3a3SAndrzej Hajda */ 6067fbf3a3SAndrzej Hajda static int drm_iommu_attach_device(struct drm_device *drm_dev, 61*07dc3678SMarek Szyprowski struct device *subdrv_dev, void **dma_priv) 6267fbf3a3SAndrzej Hajda { 6367fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm_dev->dev_private; 6467fbf3a3SAndrzej Hajda int ret; 6567fbf3a3SAndrzej Hajda 6667fbf3a3SAndrzej Hajda if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) { 676f83d208SInki Dae DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n", 6867fbf3a3SAndrzej Hajda dev_name(subdrv_dev)); 6967fbf3a3SAndrzej Hajda return -EINVAL; 7067fbf3a3SAndrzej Hajda } 7167fbf3a3SAndrzej Hajda 7267fbf3a3SAndrzej Hajda ret = configure_dma_max_seg_size(subdrv_dev); 7367fbf3a3SAndrzej Hajda if (ret) 7467fbf3a3SAndrzej Hajda return ret; 7567fbf3a3SAndrzej Hajda 7667fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 77*07dc3678SMarek Szyprowski /* 78*07dc3678SMarek Szyprowski * Keep the original DMA mapping of the sub-device and 79*07dc3678SMarek Szyprowski * restore it on Exynos DRM detach, otherwise the DMA 80*07dc3678SMarek Szyprowski * framework considers it as IOMMU-less during the next 81*07dc3678SMarek Szyprowski * probe (in case of deferred probe or modular build) 82*07dc3678SMarek Szyprowski */ 83*07dc3678SMarek Szyprowski *dma_priv = to_dma_iommu_mapping(subdrv_dev); 84*07dc3678SMarek Szyprowski if (*dma_priv) 8567fbf3a3SAndrzej Hajda arm_iommu_detach_device(subdrv_dev); 8667fbf3a3SAndrzej Hajda 8767fbf3a3SAndrzej Hajda ret = arm_iommu_attach_device(subdrv_dev, priv->mapping); 8867fbf3a3SAndrzej Hajda } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) { 8967fbf3a3SAndrzej Hajda ret = iommu_attach_device(priv->mapping, subdrv_dev); 9067fbf3a3SAndrzej Hajda } 9167fbf3a3SAndrzej Hajda 9267fbf3a3SAndrzej Hajda if (ret) 9367fbf3a3SAndrzej Hajda clear_dma_max_seg_size(subdrv_dev); 9467fbf3a3SAndrzej Hajda 9567fbf3a3SAndrzej Hajda return 0; 9667fbf3a3SAndrzej Hajda } 9767fbf3a3SAndrzej Hajda 9867fbf3a3SAndrzej Hajda /* 9967fbf3a3SAndrzej Hajda * drm_iommu_detach_device -detach device address space mapping from device 10067fbf3a3SAndrzej Hajda * 10167fbf3a3SAndrzej Hajda * @drm_dev: DRM device 10267fbf3a3SAndrzej Hajda * @subdrv_dev: device to be detached 10367fbf3a3SAndrzej Hajda * 10467fbf3a3SAndrzej Hajda * This function should be called by sub drivers to detach it from iommu 10567fbf3a3SAndrzej Hajda * mapping 10667fbf3a3SAndrzej Hajda */ 10767fbf3a3SAndrzej Hajda static void drm_iommu_detach_device(struct drm_device *drm_dev, 108*07dc3678SMarek Szyprowski struct device *subdrv_dev, void **dma_priv) 10967fbf3a3SAndrzej Hajda { 11067fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm_dev->dev_private; 11167fbf3a3SAndrzej Hajda 112*07dc3678SMarek Szyprowski if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 11367fbf3a3SAndrzej Hajda arm_iommu_detach_device(subdrv_dev); 114*07dc3678SMarek Szyprowski arm_iommu_attach_device(subdrv_dev, *dma_priv); 115*07dc3678SMarek Szyprowski } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) 11667fbf3a3SAndrzej Hajda iommu_detach_device(priv->mapping, subdrv_dev); 11767fbf3a3SAndrzej Hajda 11867fbf3a3SAndrzej Hajda clear_dma_max_seg_size(subdrv_dev); 11967fbf3a3SAndrzej Hajda } 12023755696SAndrzej Hajda 121*07dc3678SMarek Szyprowski int exynos_drm_register_dma(struct drm_device *drm, struct device *dev, 122*07dc3678SMarek Szyprowski void **dma_priv) 12323755696SAndrzej Hajda { 12423755696SAndrzej Hajda struct exynos_drm_private *priv = drm->dev_private; 12523755696SAndrzej Hajda 12623755696SAndrzej Hajda if (!priv->dma_dev) { 12723755696SAndrzej Hajda priv->dma_dev = dev; 12823755696SAndrzej Hajda DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n", 12923755696SAndrzej Hajda dev_name(dev)); 13023755696SAndrzej Hajda } 13167fbf3a3SAndrzej Hajda 13267fbf3a3SAndrzej Hajda if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 13367fbf3a3SAndrzej Hajda return 0; 13467fbf3a3SAndrzej Hajda 13567fbf3a3SAndrzej Hajda if (!priv->mapping) { 13667fbf3a3SAndrzej Hajda void *mapping; 13767fbf3a3SAndrzej Hajda 13867fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) 13967fbf3a3SAndrzej Hajda mapping = arm_iommu_create_mapping(&platform_bus_type, 14067fbf3a3SAndrzej Hajda EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE); 14167fbf3a3SAndrzej Hajda else if (IS_ENABLED(CONFIG_IOMMU_DMA)) 14267fbf3a3SAndrzej Hajda mapping = iommu_get_domain_for_dev(priv->dma_dev); 14367fbf3a3SAndrzej Hajda 14467fbf3a3SAndrzej Hajda if (IS_ERR(mapping)) 14567fbf3a3SAndrzej Hajda return PTR_ERR(mapping); 14667fbf3a3SAndrzej Hajda priv->mapping = mapping; 14723755696SAndrzej Hajda } 14823755696SAndrzej Hajda 149*07dc3678SMarek Szyprowski return drm_iommu_attach_device(drm, dev, dma_priv); 15023755696SAndrzej Hajda } 15123755696SAndrzej Hajda 152*07dc3678SMarek Szyprowski void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev, 153*07dc3678SMarek Szyprowski void **dma_priv) 15423755696SAndrzej Hajda { 15523755696SAndrzej Hajda if (IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 156*07dc3678SMarek Szyprowski drm_iommu_detach_device(drm, dev, dma_priv); 15723755696SAndrzej Hajda } 15823755696SAndrzej Hajda 15923755696SAndrzej Hajda void exynos_drm_cleanup_dma(struct drm_device *drm) 16023755696SAndrzej Hajda { 16167fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm->dev_private; 16267fbf3a3SAndrzej Hajda 16367fbf3a3SAndrzej Hajda if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 16467fbf3a3SAndrzej Hajda return; 16567fbf3a3SAndrzej Hajda 16667fbf3a3SAndrzej Hajda arm_iommu_release_mapping(priv->mapping); 16767fbf3a3SAndrzej Hajda priv->mapping = NULL; 16867fbf3a3SAndrzej Hajda priv->dma_dev = NULL; 16923755696SAndrzej Hajda } 170