Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 1 | /* exynos_drm_iommu.c |
| 2 | * |
| 3 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 4 | * Author: 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 | |
Andrzej Hajda | 4e6319c | 2016-02-24 15:15:22 +0100 | [diff] [blame] | 12 | #include <drm/drmP.h> |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 13 | #include <drm/exynos_drm.h> |
| 14 | |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <linux/iommu.h> |
| 17 | #include <linux/kref.h> |
| 18 | |
| 19 | #include <asm/dma-iommu.h> |
| 20 | |
| 21 | #include "exynos_drm_drv.h" |
| 22 | #include "exynos_drm_iommu.h" |
| 23 | |
| 24 | /* |
| 25 | * drm_create_iommu_mapping - create a mapping structure |
| 26 | * |
| 27 | * @drm_dev: DRM device |
| 28 | */ |
| 29 | int drm_create_iommu_mapping(struct drm_device *drm_dev) |
| 30 | { |
| 31 | struct dma_iommu_mapping *mapping = NULL; |
| 32 | struct exynos_drm_private *priv = drm_dev->dev_private; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 33 | |
| 34 | if (!priv->da_start) |
| 35 | priv->da_start = EXYNOS_DEV_ADDR_START; |
| 36 | if (!priv->da_space_size) |
| 37 | priv->da_space_size = EXYNOS_DEV_ADDR_SIZE; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 38 | |
| 39 | mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start, |
Marek Szyprowski | 68efd7d | 2014-02-25 13:01:09 +0100 | [diff] [blame] | 40 | priv->da_space_size); |
| 41 | |
Wei Yongjun | a0e41b5 | 2012-12-10 02:11:13 -0500 | [diff] [blame] | 42 | if (IS_ERR(mapping)) |
| 43 | return PTR_ERR(mapping); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 44 | |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 45 | priv->mapping = mapping; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * drm_release_iommu_mapping - release iommu mapping structure |
| 52 | * |
| 53 | * @drm_dev: DRM device |
| 54 | * |
| 55 | * if mapping->kref becomes 0 then all things related to iommu mapping |
| 56 | * will be released |
| 57 | */ |
| 58 | void drm_release_iommu_mapping(struct drm_device *drm_dev) |
| 59 | { |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 60 | struct exynos_drm_private *priv = drm_dev->dev_private; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 61 | |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 62 | arm_iommu_release_mapping(priv->mapping); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* |
| 66 | * drm_iommu_attach_device- attach device to iommu mapping |
| 67 | * |
| 68 | * @drm_dev: DRM device |
| 69 | * @subdrv_dev: device to be attach |
| 70 | * |
| 71 | * This function should be called by sub drivers to attach it to iommu |
| 72 | * mapping. |
| 73 | */ |
| 74 | int drm_iommu_attach_device(struct drm_device *drm_dev, |
| 75 | struct device *subdrv_dev) |
| 76 | { |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 77 | struct exynos_drm_private *priv = drm_dev->dev_private; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 78 | int ret; |
| 79 | |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 80 | if (!priv->mapping) |
Joonyoung Shim | bf56608 | 2015-07-02 21:49:38 +0900 | [diff] [blame] | 81 | return 0; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 82 | |
| 83 | subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev, |
| 84 | sizeof(*subdrv_dev->dma_parms), |
| 85 | GFP_KERNEL); |
Sachin Kamat | 4db7fcd | 2013-08-14 16:38:03 +0530 | [diff] [blame] | 86 | if (!subdrv_dev->dma_parms) |
| 87 | return -ENOMEM; |
| 88 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 89 | dma_set_max_seg_size(subdrv_dev, 0xffffffffu); |
| 90 | |
Marek Szyprowski | 4528685 | 2015-06-03 10:26:23 +0200 | [diff] [blame] | 91 | if (subdrv_dev->archdata.mapping) |
| 92 | arm_iommu_detach_device(subdrv_dev); |
| 93 | |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 94 | ret = arm_iommu_attach_device(subdrv_dev, priv->mapping); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 95 | if (ret < 0) { |
| 96 | DRM_DEBUG_KMS("failed iommu attach.\n"); |
| 97 | return ret; |
| 98 | } |
| 99 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * drm_iommu_detach_device -detach device address space mapping from device |
| 105 | * |
| 106 | * @drm_dev: DRM device |
| 107 | * @subdrv_dev: device to be detached |
| 108 | * |
| 109 | * This function should be called by sub drivers to detach it from iommu |
| 110 | * mapping |
| 111 | */ |
| 112 | void drm_iommu_detach_device(struct drm_device *drm_dev, |
| 113 | struct device *subdrv_dev) |
| 114 | { |
Marek Szyprowski | f43c359 | 2016-02-29 17:50:53 +0900 | [diff] [blame] | 115 | struct exynos_drm_private *priv = drm_dev->dev_private; |
| 116 | struct dma_iommu_mapping *mapping = priv->mapping; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 117 | |
| 118 | if (!mapping || !mapping->domain) |
| 119 | return; |
| 120 | |
Joonyoung Shim | 4d91a85 | 2015-10-02 09:30:38 +0900 | [diff] [blame] | 121 | arm_iommu_detach_device(subdrv_dev); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 122 | } |