Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 1 | /* exynos_drm_iommu.h |
| 2 | * |
| 3 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 4 | * Authoer: Inki Dae <inki.dae@samsung.com> |
| 5 | * |
Inki Dae | d81aecb | 2012-12-18 02:30:17 +0900 | [diff] [blame] | 6 | * 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 Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef _EXYNOS_DRM_IOMMU_H_ |
| 13 | #define _EXYNOS_DRM_IOMMU_H_ |
| 14 | |
| 15 | #define EXYNOS_DEV_ADDR_START 0x20000000 |
| 16 | #define EXYNOS_DEV_ADDR_SIZE 0x40000000 |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 17 | |
| 18 | #ifdef CONFIG_DRM_EXYNOS_IOMMU |
| 19 | |
Marek Szyprowski | 17879a4 | 2016-06-17 09:54:26 +0200 | [diff] [blame] | 20 | #if defined(CONFIG_ARM_DMA_USE_IOMMU) |
| 21 | #include <asm/dma-iommu.h> |
| 22 | |
| 23 | static inline int __exynos_iommu_create_mapping(struct exynos_drm_private *priv, |
| 24 | unsigned long start, unsigned long size) |
| 25 | { |
| 26 | priv->mapping = arm_iommu_create_mapping(&platform_bus_type, start, |
| 27 | size); |
| 28 | return IS_ERR(priv->mapping); |
| 29 | } |
| 30 | |
| 31 | static inline void |
| 32 | __exynos_iommu_release_mapping(struct exynos_drm_private *priv) |
| 33 | { |
| 34 | arm_iommu_release_mapping(priv->mapping); |
| 35 | } |
| 36 | |
| 37 | static inline int __exynos_iommu_attach(struct exynos_drm_private *priv, |
| 38 | struct device *dev) |
| 39 | { |
| 40 | if (dev->archdata.mapping) |
| 41 | arm_iommu_detach_device(dev); |
| 42 | |
| 43 | return arm_iommu_attach_device(dev, priv->mapping); |
| 44 | } |
| 45 | |
| 46 | static inline void __exynos_iommu_detach(struct exynos_drm_private *priv, |
| 47 | struct device *dev) |
| 48 | { |
| 49 | arm_iommu_detach_device(dev); |
| 50 | } |
| 51 | |
Marek Szyprowski | 197adf0 | 2016-06-17 09:54:27 +0200 | [diff] [blame] | 52 | #elif defined(CONFIG_IOMMU_DMA) |
| 53 | #include <linux/dma-iommu.h> |
| 54 | |
| 55 | static inline int __exynos_iommu_create_mapping(struct exynos_drm_private *priv, |
| 56 | unsigned long start, unsigned long size) |
| 57 | { |
| 58 | struct iommu_domain *domain; |
| 59 | int ret; |
| 60 | |
| 61 | domain = iommu_domain_alloc(priv->dma_dev->bus); |
| 62 | if (!domain) |
| 63 | return -ENOMEM; |
| 64 | |
| 65 | ret = iommu_get_dma_cookie(domain); |
| 66 | if (ret) |
| 67 | goto free_domain; |
| 68 | |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 69 | ret = iommu_dma_init_domain(domain, start, size, NULL); |
Marek Szyprowski | 197adf0 | 2016-06-17 09:54:27 +0200 | [diff] [blame] | 70 | if (ret) |
| 71 | goto put_cookie; |
| 72 | |
| 73 | priv->mapping = domain; |
| 74 | return 0; |
| 75 | |
| 76 | put_cookie: |
| 77 | iommu_put_dma_cookie(domain); |
| 78 | free_domain: |
| 79 | iommu_domain_free(domain); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static inline void __exynos_iommu_release_mapping(struct exynos_drm_private *priv) |
| 84 | { |
| 85 | struct iommu_domain *domain = priv->mapping; |
| 86 | |
| 87 | iommu_put_dma_cookie(domain); |
| 88 | iommu_domain_free(domain); |
| 89 | priv->mapping = NULL; |
| 90 | } |
| 91 | |
| 92 | static inline int __exynos_iommu_attach(struct exynos_drm_private *priv, |
| 93 | struct device *dev) |
| 94 | { |
| 95 | struct iommu_domain *domain = priv->mapping; |
| 96 | |
| 97 | return iommu_attach_device(domain, dev); |
| 98 | } |
| 99 | |
| 100 | static inline void __exynos_iommu_detach(struct exynos_drm_private *priv, |
| 101 | struct device *dev) |
| 102 | { |
| 103 | struct iommu_domain *domain = priv->mapping; |
| 104 | |
| 105 | iommu_detach_device(domain, dev); |
| 106 | } |
Marek Szyprowski | 17879a4 | 2016-06-17 09:54:26 +0200 | [diff] [blame] | 107 | #else |
| 108 | #error Unsupported architecture and IOMMU/DMA-mapping glue code |
| 109 | #endif |
| 110 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 111 | int drm_create_iommu_mapping(struct drm_device *drm_dev); |
| 112 | |
| 113 | void drm_release_iommu_mapping(struct drm_device *drm_dev); |
| 114 | |
| 115 | int drm_iommu_attach_device(struct drm_device *drm_dev, |
| 116 | struct device *subdrv_dev); |
| 117 | |
| 118 | void drm_iommu_detach_device(struct drm_device *dev_dev, |
| 119 | struct device *subdrv_dev); |
| 120 | |
| 121 | static inline bool is_drm_iommu_supported(struct drm_device *drm_dev) |
| 122 | { |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 123 | struct exynos_drm_private *priv = drm_dev->dev_private; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 124 | |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 125 | return priv->mapping ? true : false; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | #else |
| 129 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 130 | static inline int drm_create_iommu_mapping(struct drm_device *drm_dev) |
| 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline void drm_release_iommu_mapping(struct drm_device *drm_dev) |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | static inline int drm_iommu_attach_device(struct drm_device *drm_dev, |
| 140 | struct device *subdrv_dev) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static inline void drm_iommu_detach_device(struct drm_device *drm_dev, |
| 146 | struct device *subdrv_dev) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | static inline bool is_drm_iommu_supported(struct drm_device *drm_dev) |
| 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 155 | #endif |
| 156 | #endif |