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 <drm/drmP.h> 867fbf3a3SAndrzej Hajda #include <drm/exynos_drm.h> 967fbf3a3SAndrzej Hajda #include <linux/dma-iommu.h> 1067fbf3a3SAndrzej Hajda #include <linux/dma-mapping.h> 1167fbf3a3SAndrzej Hajda #include <linux/iommu.h> 1267fbf3a3SAndrzej Hajda 1323755696SAndrzej Hajda #include "exynos_drm_drv.h" 1467fbf3a3SAndrzej Hajda 1567fbf3a3SAndrzej Hajda #if defined(CONFIG_ARM_DMA_USE_IOMMU) 1667fbf3a3SAndrzej Hajda #include <asm/dma-iommu.h> 1767fbf3a3SAndrzej Hajda #else 1867fbf3a3SAndrzej Hajda #define arm_iommu_create_mapping(...) ({ NULL; }) 1967fbf3a3SAndrzej Hajda #define arm_iommu_attach_device(...) ({ -ENODEV; }) 2067fbf3a3SAndrzej Hajda #define arm_iommu_release_mapping(...) ({ }) 2167fbf3a3SAndrzej Hajda #define arm_iommu_detach_device(...) ({ }) 2267fbf3a3SAndrzej Hajda #define to_dma_iommu_mapping(dev) NULL 2367fbf3a3SAndrzej Hajda #endif 2467fbf3a3SAndrzej Hajda 2567fbf3a3SAndrzej Hajda #if !defined(CONFIG_IOMMU_DMA) 2667fbf3a3SAndrzej Hajda #define iommu_dma_init_domain(...) ({ -EINVAL; }) 2767fbf3a3SAndrzej Hajda #endif 2867fbf3a3SAndrzej Hajda 2967fbf3a3SAndrzej Hajda #define EXYNOS_DEV_ADDR_START 0x20000000 3067fbf3a3SAndrzej Hajda #define EXYNOS_DEV_ADDR_SIZE 0x40000000 3167fbf3a3SAndrzej Hajda 3267fbf3a3SAndrzej Hajda static inline int configure_dma_max_seg_size(struct device *dev) 3367fbf3a3SAndrzej Hajda { 3467fbf3a3SAndrzej Hajda if (!dev->dma_parms) 3567fbf3a3SAndrzej Hajda dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL); 3667fbf3a3SAndrzej Hajda if (!dev->dma_parms) 3767fbf3a3SAndrzej Hajda return -ENOMEM; 3867fbf3a3SAndrzej Hajda 3967fbf3a3SAndrzej Hajda dma_set_max_seg_size(dev, DMA_BIT_MASK(32)); 4067fbf3a3SAndrzej Hajda return 0; 4167fbf3a3SAndrzej Hajda } 4267fbf3a3SAndrzej Hajda 4367fbf3a3SAndrzej Hajda static inline void clear_dma_max_seg_size(struct device *dev) 4467fbf3a3SAndrzej Hajda { 4567fbf3a3SAndrzej Hajda kfree(dev->dma_parms); 4667fbf3a3SAndrzej Hajda dev->dma_parms = NULL; 4767fbf3a3SAndrzej Hajda } 4867fbf3a3SAndrzej Hajda 4967fbf3a3SAndrzej Hajda /* 5067fbf3a3SAndrzej Hajda * drm_iommu_attach_device- attach device to iommu mapping 5167fbf3a3SAndrzej Hajda * 5267fbf3a3SAndrzej Hajda * @drm_dev: DRM device 5367fbf3a3SAndrzej Hajda * @subdrv_dev: device to be attach 5467fbf3a3SAndrzej Hajda * 5567fbf3a3SAndrzej Hajda * This function should be called by sub drivers to attach it to iommu 5667fbf3a3SAndrzej Hajda * mapping. 5767fbf3a3SAndrzej Hajda */ 5867fbf3a3SAndrzej Hajda static int drm_iommu_attach_device(struct drm_device *drm_dev, 5967fbf3a3SAndrzej Hajda struct device *subdrv_dev) 6067fbf3a3SAndrzej Hajda { 6167fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm_dev->dev_private; 6267fbf3a3SAndrzej Hajda int ret; 6367fbf3a3SAndrzej Hajda 6467fbf3a3SAndrzej Hajda if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) { 65*6f83d208SInki Dae DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n", 6667fbf3a3SAndrzej Hajda dev_name(subdrv_dev)); 6767fbf3a3SAndrzej Hajda return -EINVAL; 6867fbf3a3SAndrzej Hajda } 6967fbf3a3SAndrzej Hajda 7067fbf3a3SAndrzej Hajda ret = configure_dma_max_seg_size(subdrv_dev); 7167fbf3a3SAndrzej Hajda if (ret) 7267fbf3a3SAndrzej Hajda return ret; 7367fbf3a3SAndrzej Hajda 7467fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 7567fbf3a3SAndrzej Hajda if (to_dma_iommu_mapping(subdrv_dev)) 7667fbf3a3SAndrzej Hajda arm_iommu_detach_device(subdrv_dev); 7767fbf3a3SAndrzej Hajda 7867fbf3a3SAndrzej Hajda ret = arm_iommu_attach_device(subdrv_dev, priv->mapping); 7967fbf3a3SAndrzej Hajda } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) { 8067fbf3a3SAndrzej Hajda ret = iommu_attach_device(priv->mapping, subdrv_dev); 8167fbf3a3SAndrzej Hajda } 8267fbf3a3SAndrzej Hajda 8367fbf3a3SAndrzej Hajda if (ret) 8467fbf3a3SAndrzej Hajda clear_dma_max_seg_size(subdrv_dev); 8567fbf3a3SAndrzej Hajda 8667fbf3a3SAndrzej Hajda return 0; 8767fbf3a3SAndrzej Hajda } 8867fbf3a3SAndrzej Hajda 8967fbf3a3SAndrzej Hajda /* 9067fbf3a3SAndrzej Hajda * drm_iommu_detach_device -detach device address space mapping from device 9167fbf3a3SAndrzej Hajda * 9267fbf3a3SAndrzej Hajda * @drm_dev: DRM device 9367fbf3a3SAndrzej Hajda * @subdrv_dev: device to be detached 9467fbf3a3SAndrzej Hajda * 9567fbf3a3SAndrzej Hajda * This function should be called by sub drivers to detach it from iommu 9667fbf3a3SAndrzej Hajda * mapping 9767fbf3a3SAndrzej Hajda */ 9867fbf3a3SAndrzej Hajda static void drm_iommu_detach_device(struct drm_device *drm_dev, 9967fbf3a3SAndrzej Hajda struct device *subdrv_dev) 10067fbf3a3SAndrzej Hajda { 10167fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm_dev->dev_private; 10267fbf3a3SAndrzej Hajda 10367fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) 10467fbf3a3SAndrzej Hajda arm_iommu_detach_device(subdrv_dev); 10567fbf3a3SAndrzej Hajda else if (IS_ENABLED(CONFIG_IOMMU_DMA)) 10667fbf3a3SAndrzej Hajda iommu_detach_device(priv->mapping, subdrv_dev); 10767fbf3a3SAndrzej Hajda 10867fbf3a3SAndrzej Hajda clear_dma_max_seg_size(subdrv_dev); 10967fbf3a3SAndrzej Hajda } 11023755696SAndrzej Hajda 11123755696SAndrzej Hajda int exynos_drm_register_dma(struct drm_device *drm, struct device *dev) 11223755696SAndrzej Hajda { 11323755696SAndrzej Hajda struct exynos_drm_private *priv = drm->dev_private; 11423755696SAndrzej Hajda 11523755696SAndrzej Hajda if (!priv->dma_dev) { 11623755696SAndrzej Hajda priv->dma_dev = dev; 11723755696SAndrzej Hajda DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n", 11823755696SAndrzej Hajda dev_name(dev)); 11923755696SAndrzej Hajda } 12067fbf3a3SAndrzej Hajda 12167fbf3a3SAndrzej Hajda if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 12267fbf3a3SAndrzej Hajda return 0; 12367fbf3a3SAndrzej Hajda 12467fbf3a3SAndrzej Hajda if (!priv->mapping) { 12567fbf3a3SAndrzej Hajda void *mapping; 12667fbf3a3SAndrzej Hajda 12767fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) 12867fbf3a3SAndrzej Hajda mapping = arm_iommu_create_mapping(&platform_bus_type, 12967fbf3a3SAndrzej Hajda EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE); 13067fbf3a3SAndrzej Hajda else if (IS_ENABLED(CONFIG_IOMMU_DMA)) 13167fbf3a3SAndrzej Hajda mapping = iommu_get_domain_for_dev(priv->dma_dev); 13267fbf3a3SAndrzej Hajda 13367fbf3a3SAndrzej Hajda if (IS_ERR(mapping)) 13467fbf3a3SAndrzej Hajda return PTR_ERR(mapping); 13567fbf3a3SAndrzej Hajda priv->mapping = mapping; 13623755696SAndrzej Hajda } 13723755696SAndrzej Hajda 13823755696SAndrzej Hajda return drm_iommu_attach_device(drm, dev); 13923755696SAndrzej Hajda } 14023755696SAndrzej Hajda 14123755696SAndrzej Hajda void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev) 14223755696SAndrzej Hajda { 14323755696SAndrzej Hajda if (IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 14423755696SAndrzej Hajda drm_iommu_detach_device(drm, dev); 14523755696SAndrzej Hajda } 14623755696SAndrzej Hajda 14723755696SAndrzej Hajda void exynos_drm_cleanup_dma(struct drm_device *drm) 14823755696SAndrzej Hajda { 14967fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm->dev_private; 15067fbf3a3SAndrzej Hajda 15167fbf3a3SAndrzej Hajda if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 15267fbf3a3SAndrzej Hajda return; 15367fbf3a3SAndrzej Hajda 15467fbf3a3SAndrzej Hajda arm_iommu_release_mapping(priv->mapping); 15567fbf3a3SAndrzej Hajda priv->mapping = NULL; 15667fbf3a3SAndrzej Hajda priv->dma_dev = NULL; 15723755696SAndrzej Hajda } 158