2018-10-12 18:53:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
2018-10-12 18:53:46 +08:00
|
|
|
// Author: Inki Dae <inki.dae@samsung.com>
|
2018-10-12 18:53:43 +08:00
|
|
|
// Author: Andrzej Hajda <a.hajda@samsung.com>
|
|
|
|
|
2018-10-12 18:53:46 +08:00
|
|
|
#include <linux/dma-iommu.h>
|
2020-09-22 21:31:03 +08:00
|
|
|
#include <linux/dma-map-ops.h>
|
2018-10-12 18:53:46 +08:00
|
|
|
#include <linux/iommu.h>
|
2019-06-24 21:06:28 +08:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
|
|
|
#include <drm/drm_print.h>
|
|
|
|
#include <drm/exynos_drm.h>
|
2018-10-12 18:53:46 +08:00
|
|
|
|
2018-10-12 18:53:43 +08:00
|
|
|
#include "exynos_drm_drv.h"
|
2018-10-12 18:53:46 +08:00
|
|
|
|
|
|
|
#if defined(CONFIG_ARM_DMA_USE_IOMMU)
|
|
|
|
#include <asm/dma-iommu.h>
|
|
|
|
#else
|
|
|
|
#define arm_iommu_create_mapping(...) ({ NULL; })
|
|
|
|
#define arm_iommu_attach_device(...) ({ -ENODEV; })
|
|
|
|
#define arm_iommu_release_mapping(...) ({ })
|
|
|
|
#define arm_iommu_detach_device(...) ({ })
|
|
|
|
#define to_dma_iommu_mapping(dev) NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CONFIG_IOMMU_DMA)
|
|
|
|
#define iommu_dma_init_domain(...) ({ -EINVAL; })
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define EXYNOS_DEV_ADDR_START 0x20000000
|
|
|
|
#define EXYNOS_DEV_ADDR_SIZE 0x40000000
|
|
|
|
|
|
|
|
/*
|
|
|
|
* drm_iommu_attach_device- attach device to iommu mapping
|
|
|
|
*
|
|
|
|
* @drm_dev: DRM device
|
|
|
|
* @subdrv_dev: device to be attach
|
|
|
|
*
|
|
|
|
* This function should be called by sub drivers to attach it to iommu
|
|
|
|
* mapping.
|
|
|
|
*/
|
|
|
|
static int drm_iommu_attach_device(struct drm_device *drm_dev,
|
2020-03-09 19:02:14 +08:00
|
|
|
struct device *subdrv_dev, void **dma_priv)
|
2018-10-12 18:53:46 +08:00
|
|
|
{
|
|
|
|
struct exynos_drm_private *priv = drm_dev->dev_private;
|
2020-06-01 16:06:30 +08:00
|
|
|
int ret = 0;
|
2018-10-12 18:53:46 +08:00
|
|
|
|
|
|
|
if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) {
|
2019-04-15 13:24:36 +08:00
|
|
|
DRM_DEV_ERROR(subdrv_dev, "Device %s lacks support for IOMMU\n",
|
2018-10-12 18:53:46 +08:00
|
|
|
dev_name(subdrv_dev));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:08:27 +08:00
|
|
|
dma_set_max_seg_size(subdrv_dev, DMA_BIT_MASK(32));
|
2018-10-12 18:53:46 +08:00
|
|
|
if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
|
2020-03-09 19:02:14 +08:00
|
|
|
/*
|
|
|
|
* Keep the original DMA mapping of the sub-device and
|
|
|
|
* restore it on Exynos DRM detach, otherwise the DMA
|
|
|
|
* framework considers it as IOMMU-less during the next
|
|
|
|
* probe (in case of deferred probe or modular build)
|
|
|
|
*/
|
|
|
|
*dma_priv = to_dma_iommu_mapping(subdrv_dev);
|
|
|
|
if (*dma_priv)
|
2018-10-12 18:53:46 +08:00
|
|
|
arm_iommu_detach_device(subdrv_dev);
|
|
|
|
|
|
|
|
ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
|
|
|
|
} else if (IS_ENABLED(CONFIG_IOMMU_DMA)) {
|
|
|
|
ret = iommu_attach_device(priv->mapping, subdrv_dev);
|
|
|
|
}
|
|
|
|
|
2020-06-01 16:06:30 +08:00
|
|
|
return ret;
|
2018-10-12 18:53:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* drm_iommu_detach_device -detach device address space mapping from device
|
|
|
|
*
|
|
|
|
* @drm_dev: DRM device
|
|
|
|
* @subdrv_dev: device to be detached
|
|
|
|
*
|
|
|
|
* This function should be called by sub drivers to detach it from iommu
|
|
|
|
* mapping
|
|
|
|
*/
|
|
|
|
static void drm_iommu_detach_device(struct drm_device *drm_dev,
|
2020-03-09 19:02:14 +08:00
|
|
|
struct device *subdrv_dev, void **dma_priv)
|
2018-10-12 18:53:46 +08:00
|
|
|
{
|
|
|
|
struct exynos_drm_private *priv = drm_dev->dev_private;
|
|
|
|
|
2020-03-09 19:02:14 +08:00
|
|
|
if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
|
2018-10-12 18:53:46 +08:00
|
|
|
arm_iommu_detach_device(subdrv_dev);
|
2020-03-09 19:02:14 +08:00
|
|
|
arm_iommu_attach_device(subdrv_dev, *dma_priv);
|
|
|
|
} else if (IS_ENABLED(CONFIG_IOMMU_DMA))
|
2018-10-12 18:53:46 +08:00
|
|
|
iommu_detach_device(priv->mapping, subdrv_dev);
|
|
|
|
}
|
2018-10-12 18:53:43 +08:00
|
|
|
|
2020-03-09 19:02:14 +08:00
|
|
|
int exynos_drm_register_dma(struct drm_device *drm, struct device *dev,
|
|
|
|
void **dma_priv)
|
2018-10-12 18:53:43 +08:00
|
|
|
{
|
|
|
|
struct exynos_drm_private *priv = drm->dev_private;
|
|
|
|
|
|
|
|
if (!priv->dma_dev) {
|
|
|
|
priv->dma_dev = dev;
|
|
|
|
DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
|
|
|
|
dev_name(dev));
|
2018-10-12 18:53:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!priv->mapping) {
|
|
|
|
void *mapping;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))
|
|
|
|
mapping = arm_iommu_create_mapping(&platform_bus_type,
|
|
|
|
EXYNOS_DEV_ADDR_START, EXYNOS_DEV_ADDR_SIZE);
|
|
|
|
else if (IS_ENABLED(CONFIG_IOMMU_DMA))
|
|
|
|
mapping = iommu_get_domain_for_dev(priv->dma_dev);
|
2021-07-28 07:36:56 +08:00
|
|
|
else
|
|
|
|
mapping = ERR_PTR(-ENODEV);
|
2018-10-12 18:53:46 +08:00
|
|
|
|
|
|
|
if (IS_ERR(mapping))
|
|
|
|
return PTR_ERR(mapping);
|
|
|
|
priv->mapping = mapping;
|
2018-10-12 18:53:43 +08:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:02:14 +08:00
|
|
|
return drm_iommu_attach_device(drm, dev, dma_priv);
|
2018-10-12 18:53:43 +08:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:02:14 +08:00
|
|
|
void exynos_drm_unregister_dma(struct drm_device *drm, struct device *dev,
|
|
|
|
void **dma_priv)
|
2018-10-12 18:53:43 +08:00
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_EXYNOS_IOMMU))
|
2020-03-09 19:02:14 +08:00
|
|
|
drm_iommu_detach_device(drm, dev, dma_priv);
|
2018-10-12 18:53:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_drm_cleanup_dma(struct drm_device *drm)
|
|
|
|
{
|
2018-10-12 18:53:46 +08:00
|
|
|
struct exynos_drm_private *priv = drm->dev_private;
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_EXYNOS_IOMMU))
|
|
|
|
return;
|
|
|
|
|
|
|
|
arm_iommu_release_mapping(priv->mapping);
|
|
|
|
priv->mapping = NULL;
|
|
|
|
priv->dma_dev = NULL;
|
2018-10-12 18:53:43 +08:00
|
|
|
}
|