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 70a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h> 867fbf3a3SAndrzej Hajda #include <linux/iommu.h> 92bda34d7SSam Ravnborg #include <linux/platform_device.h> 102bda34d7SSam Ravnborg 112bda34d7SSam Ravnborg #include <drm/drm_print.h> 122bda34d7SSam Ravnborg #include <drm/exynos_drm.h> 1367fbf3a3SAndrzej Hajda 1423755696SAndrzej Hajda #include "exynos_drm_drv.h" 1567fbf3a3SAndrzej Hajda 1667fbf3a3SAndrzej Hajda #if defined(CONFIG_ARM_DMA_USE_IOMMU) 1767fbf3a3SAndrzej Hajda #include <asm/dma-iommu.h> 1867fbf3a3SAndrzej Hajda #else 1967fbf3a3SAndrzej Hajda #define arm_iommu_create_mapping(...) ({ NULL; }) 2067fbf3a3SAndrzej Hajda #define arm_iommu_attach_device(...) ({ -ENODEV; }) 2167fbf3a3SAndrzej Hajda #define arm_iommu_release_mapping(...) ({ }) 2267fbf3a3SAndrzej Hajda #define arm_iommu_detach_device(...) ({ }) 2367fbf3a3SAndrzej Hajda #define to_dma_iommu_mapping(dev) NULL 2467fbf3a3SAndrzej Hajda #endif 2567fbf3a3SAndrzej Hajda 2667fbf3a3SAndrzej Hajda #if !defined(CONFIG_IOMMU_DMA) 2767fbf3a3SAndrzej Hajda #define iommu_dma_init_domain(...) ({ -EINVAL; }) 2867fbf3a3SAndrzej Hajda #endif 2967fbf3a3SAndrzej Hajda 3067fbf3a3SAndrzej Hajda #define EXYNOS_DEV_ADDR_START 0x20000000 3167fbf3a3SAndrzej Hajda #define EXYNOS_DEV_ADDR_SIZE 0x40000000 3267fbf3a3SAndrzej Hajda 3367fbf3a3SAndrzej Hajda /* 3467fbf3a3SAndrzej Hajda * drm_iommu_attach_device- attach device to iommu mapping 3567fbf3a3SAndrzej Hajda * 3667fbf3a3SAndrzej Hajda * @drm_dev: DRM device 3767fbf3a3SAndrzej Hajda * @subdrv_dev: device to be attach 3867fbf3a3SAndrzej Hajda * 3967fbf3a3SAndrzej Hajda * This function should be called by sub drivers to attach it to iommu 4067fbf3a3SAndrzej Hajda * mapping. 4167fbf3a3SAndrzej Hajda */ 4267fbf3a3SAndrzej Hajda static int drm_iommu_attach_device(struct drm_device *drm_dev, 4307dc3678SMarek Szyprowski struct device *subdrv_dev, void **dma_priv) 4467fbf3a3SAndrzej Hajda { 4567fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm_dev->dev_private; 46b9c63388SMarek Szyprowski int ret = 0; 4767fbf3a3SAndrzej Hajda 4867fbf3a3SAndrzej Hajda if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) { 496f83d208SInki Dae DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n", 5067fbf3a3SAndrzej Hajda dev_name(subdrv_dev)); 5167fbf3a3SAndrzej Hajda return -EINVAL; 5267fbf3a3SAndrzej Hajda } 5367fbf3a3SAndrzej Hajda 54ddfd4ab6SMarek Szyprowski dma_set_max_seg_size(subdrv_dev, DMA_BIT_MASK(32)); 5567fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 5607dc3678SMarek Szyprowski /* 5707dc3678SMarek Szyprowski * Keep the original DMA mapping of the sub-device and 5807dc3678SMarek Szyprowski * restore it on Exynos DRM detach, otherwise the DMA 5907dc3678SMarek Szyprowski * framework considers it as IOMMU-less during the next 6007dc3678SMarek Szyprowski * probe (in case of deferred probe or modular build) 6107dc3678SMarek Szyprowski */ 6207dc3678SMarek Szyprowski *dma_priv = to_dma_iommu_mapping(subdrv_dev); 6307dc3678SMarek Szyprowski if (*dma_priv) 6467fbf3a3SAndrzej Hajda arm_iommu_detach_device(subdrv_dev); 6567fbf3a3SAndrzej Hajda 6667fbf3a3SAndrzej Hajda ret = arm_iommu_attach_device(subdrv_dev, priv->mapping); 6767fbf3a3SAndrzej Hajda } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) { 6867fbf3a3SAndrzej Hajda ret = iommu_attach_device(priv->mapping, subdrv_dev); 6967fbf3a3SAndrzej Hajda } 7067fbf3a3SAndrzej Hajda 71b9c63388SMarek Szyprowski return ret; 7267fbf3a3SAndrzej Hajda } 7367fbf3a3SAndrzej Hajda 7467fbf3a3SAndrzej Hajda /* 7567fbf3a3SAndrzej Hajda * drm_iommu_detach_device -detach device address space mapping from device 7667fbf3a3SAndrzej Hajda * 7767fbf3a3SAndrzej Hajda * @drm_dev: DRM device 7867fbf3a3SAndrzej Hajda * @subdrv_dev: device to be detached 7967fbf3a3SAndrzej Hajda * 8067fbf3a3SAndrzej Hajda * This function should be called by sub drivers to detach it from iommu 8167fbf3a3SAndrzej Hajda * mapping 8267fbf3a3SAndrzej Hajda */ 8367fbf3a3SAndrzej Hajda static void drm_iommu_detach_device(struct drm_device *drm_dev, 8407dc3678SMarek Szyprowski struct device *subdrv_dev, void **dma_priv) 8567fbf3a3SAndrzej Hajda { 8667fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm_dev->dev_private; 8767fbf3a3SAndrzej Hajda 8807dc3678SMarek Szyprowski if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 8967fbf3a3SAndrzej Hajda arm_iommu_detach_device(subdrv_dev); 9007dc3678SMarek Szyprowski arm_iommu_attach_device(subdrv_dev, *dma_priv); 9107dc3678SMarek Szyprowski } else if (IS_ENABLED(CONFIG_IOMMU_DMA)) 9267fbf3a3SAndrzej Hajda iommu_detach_device(priv->mapping, subdrv_dev); 9367fbf3a3SAndrzej Hajda } 9423755696SAndrzej Hajda 9507dc3678SMarek Szyprowski int exynos_drm_register_dma(struct drm_device *drm, struct device *dev, 9607dc3678SMarek Szyprowski void **dma_priv) 9723755696SAndrzej Hajda { 9823755696SAndrzej Hajda struct exynos_drm_private *priv = drm->dev_private; 9923755696SAndrzej Hajda 10023755696SAndrzej Hajda if (!priv->dma_dev) { 10123755696SAndrzej Hajda priv->dma_dev = dev; 10223755696SAndrzej Hajda DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n", 10323755696SAndrzej Hajda dev_name(dev)); 10423755696SAndrzej Hajda } 10567fbf3a3SAndrzej Hajda 10667fbf3a3SAndrzej Hajda if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 10767fbf3a3SAndrzej Hajda return 0; 10867fbf3a3SAndrzej Hajda 10967fbf3a3SAndrzej Hajda if (!priv->mapping) { 1108d1b7809SInki Dae void *mapping = NULL; 11167fbf3a3SAndrzej Hajda 11267fbf3a3SAndrzej Hajda if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) 113*9e8354b3SJason Gunthorpe mapping = arm_iommu_create_mapping(dev, 11467fbf3a3SAndrzej Hajda EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE); 11567fbf3a3SAndrzej Hajda else if (IS_ENABLED(CONFIG_IOMMU_DMA)) 11667fbf3a3SAndrzej Hajda mapping = iommu_get_domain_for_dev(priv->dma_dev); 11767fbf3a3SAndrzej Hajda 1188d1b7809SInki Dae if (!mapping) 1198d1b7809SInki Dae return -ENODEV; 12067fbf3a3SAndrzej Hajda priv->mapping = mapping; 12123755696SAndrzej Hajda } 12223755696SAndrzej Hajda 12307dc3678SMarek Szyprowski return drm_iommu_attach_device(drm, dev, dma_priv); 12423755696SAndrzej Hajda } 12523755696SAndrzej Hajda 12607dc3678SMarek Szyprowski void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev, 12707dc3678SMarek Szyprowski void **dma_priv) 12823755696SAndrzej Hajda { 12923755696SAndrzej Hajda if (IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 13007dc3678SMarek Szyprowski drm_iommu_detach_device(drm, dev, dma_priv); 13123755696SAndrzej Hajda } 13223755696SAndrzej Hajda 13323755696SAndrzej Hajda void exynos_drm_cleanup_dma(struct drm_device *drm) 13423755696SAndrzej Hajda { 13567fbf3a3SAndrzej Hajda struct exynos_drm_private *priv = drm->dev_private; 13667fbf3a3SAndrzej Hajda 13767fbf3a3SAndrzej Hajda if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU)) 13867fbf3a3SAndrzej Hajda return; 13967fbf3a3SAndrzej Hajda 14067fbf3a3SAndrzej Hajda arm_iommu_release_mapping(priv->mapping); 14167fbf3a3SAndrzej Hajda priv->mapping = NULL; 14267fbf3a3SAndrzej Hajda priv->dma_dev = NULL; 14323755696SAndrzej Hajda } 144