blob: 0f373702414e32342fa98d09ac597d3996f24c58 [file] [log] [blame]
Inki Dae0519f9a2012-10-20 07:53:42 -07001/* exynos_drm_iommu.c
2 *
3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
Inki Daed81aecb2012-12-18 02:30:17 +09006 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
Inki Dae0519f9a2012-10-20 07:53:42 -070010 */
11
Andrzej Hajda4e6319c2016-02-24 15:15:22 +010012#include <drm/drmP.h>
Inki Dae0519f9a2012-10-20 07:53:42 -070013#include <drm/exynos_drm.h>
14
15#include <linux/dma-mapping.h>
16#include <linux/iommu.h>
Inki Dae0519f9a2012-10-20 07:53:42 -070017
18#include "exynos_drm_drv.h"
19#include "exynos_drm_iommu.h"
20
Marek Szyprowskif7c72772016-06-17 09:54:23 +020021static inline int configure_dma_max_seg_size(struct device *dev)
22{
23 if (!dev->dma_parms)
24 dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
25 if (!dev->dma_parms)
26 return -ENOMEM;
27
28 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
29 return 0;
30}
31
32static inline void clear_dma_max_seg_size(struct device *dev)
33{
34 kfree(dev->dma_parms);
35 dev->dma_parms = NULL;
36}
37
Inki Dae0519f9a2012-10-20 07:53:42 -070038/*
39 * drm_create_iommu_mapping - create a mapping structure
40 *
41 * @drm_dev: DRM device
42 */
43int drm_create_iommu_mapping(struct drm_device *drm_dev)
44{
Inki Dae0519f9a2012-10-20 07:53:42 -070045 struct exynos_drm_private *priv = drm_dev->dev_private;
Inki Dae0519f9a2012-10-20 07:53:42 -070046
Marek Szyprowski17879a42016-06-17 09:54:26 +020047 return __exynos_iommu_create_mapping(priv, EXYNOS_DEV_ADDR_START,
48 EXYNOS_DEV_ADDR_SIZE);
Inki Dae0519f9a2012-10-20 07:53:42 -070049}
50
51/*
52 * drm_release_iommu_mapping - release iommu mapping structure
53 *
54 * @drm_dev: DRM device
Inki Dae0519f9a2012-10-20 07:53:42 -070055 */
56void drm_release_iommu_mapping(struct drm_device *drm_dev)
57{
Marek Szyprowskif43c3592016-02-29 17:50:53 +090058 struct exynos_drm_private *priv = drm_dev->dev_private;
Inki Dae0519f9a2012-10-20 07:53:42 -070059
Marek Szyprowski17879a42016-06-17 09:54:26 +020060 __exynos_iommu_release_mapping(priv);
Inki Dae0519f9a2012-10-20 07:53:42 -070061}
62
63/*
64 * drm_iommu_attach_device- attach device to iommu mapping
65 *
66 * @drm_dev: DRM device
67 * @subdrv_dev: device to be attach
68 *
69 * This function should be called by sub drivers to attach it to iommu
70 * mapping.
71 */
72int drm_iommu_attach_device(struct drm_device *drm_dev,
73 struct device *subdrv_dev)
74{
Marek Szyprowskif43c3592016-02-29 17:50:53 +090075 struct exynos_drm_private *priv = drm_dev->dev_private;
Inki Dae0519f9a2012-10-20 07:53:42 -070076 int ret;
77
Marek Szyprowskic04d9eb2016-06-17 09:54:24 +020078 if (get_dma_ops(priv->dma_dev) != get_dma_ops(subdrv_dev)) {
79 DRM_ERROR("Device %s lacks support for IOMMU\n",
80 dev_name(subdrv_dev));
81 return -EINVAL;
82 }
Sachin Kamat4db7fcd2013-08-14 16:38:03 +053083
Marek Szyprowskif7c72772016-06-17 09:54:23 +020084 ret = configure_dma_max_seg_size(subdrv_dev);
85 if (ret)
86 return ret;
Inki Dae0519f9a2012-10-20 07:53:42 -070087
Marek Szyprowski17879a42016-06-17 09:54:26 +020088 ret = __exynos_iommu_attach(priv, subdrv_dev);
89 if (ret)
Marek Szyprowskif7c72772016-06-17 09:54:23 +020090 clear_dma_max_seg_size(subdrv_dev);
Inki Dae0519f9a2012-10-20 07:53:42 -070091
Inki Dae0519f9a2012-10-20 07:53:42 -070092 return 0;
93}
94
95/*
96 * drm_iommu_detach_device -detach device address space mapping from device
97 *
98 * @drm_dev: DRM device
99 * @subdrv_dev: device to be detached
100 *
101 * This function should be called by sub drivers to detach it from iommu
102 * mapping
103 */
104void drm_iommu_detach_device(struct drm_device *drm_dev,
105 struct device *subdrv_dev)
106{
Marek Szyprowskif43c3592016-02-29 17:50:53 +0900107 struct exynos_drm_private *priv = drm_dev->dev_private;
Inki Dae0519f9a2012-10-20 07:53:42 -0700108
Marek Szyprowski17879a42016-06-17 09:54:26 +0200109 __exynos_iommu_detach(priv, subdrv_dev);
Marek Szyprowskif7c72772016-06-17 09:54:23 +0200110 clear_dma_max_seg_size(subdrv_dev);
Inki Dae0519f9a2012-10-20 07:53:42 -0700111}