exynos_drm_iommu.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* exynos_drm_iommu.c
  2. *
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * Author: Inki Dae <inki.dae@samsung.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drmP.h>
  26. #include <drm/exynos_drm.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/iommu.h>
  29. #include <linux/kref.h>
  30. #include <asm/dma-iommu.h>
  31. #include "exynos_drm_drv.h"
  32. #include "exynos_drm_iommu.h"
  33. /*
  34. * drm_create_iommu_mapping - create a mapping structure
  35. *
  36. * @drm_dev: DRM device
  37. */
  38. int drm_create_iommu_mapping(struct drm_device *drm_dev)
  39. {
  40. struct dma_iommu_mapping *mapping = NULL;
  41. struct exynos_drm_private *priv = drm_dev->dev_private;
  42. struct device *dev = drm_dev->dev;
  43. if (!priv->da_start)
  44. priv->da_start = EXYNOS_DEV_ADDR_START;
  45. if (!priv->da_space_size)
  46. priv->da_space_size = EXYNOS_DEV_ADDR_SIZE;
  47. if (!priv->da_space_order)
  48. priv->da_space_order = EXYNOS_DEV_ADDR_ORDER;
  49. mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
  50. priv->da_space_size,
  51. priv->da_space_order);
  52. if (!mapping)
  53. return -ENOMEM;
  54. dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
  55. GFP_KERNEL);
  56. dma_set_max_seg_size(dev, 0xffffffffu);
  57. dev->archdata.mapping = mapping;
  58. return 0;
  59. }
  60. /*
  61. * drm_release_iommu_mapping - release iommu mapping structure
  62. *
  63. * @drm_dev: DRM device
  64. *
  65. * if mapping->kref becomes 0 then all things related to iommu mapping
  66. * will be released
  67. */
  68. void drm_release_iommu_mapping(struct drm_device *drm_dev)
  69. {
  70. struct device *dev = drm_dev->dev;
  71. arm_iommu_release_mapping(dev->archdata.mapping);
  72. }
  73. /*
  74. * drm_iommu_attach_device- attach device to iommu mapping
  75. *
  76. * @drm_dev: DRM device
  77. * @subdrv_dev: device to be attach
  78. *
  79. * This function should be called by sub drivers to attach it to iommu
  80. * mapping.
  81. */
  82. int drm_iommu_attach_device(struct drm_device *drm_dev,
  83. struct device *subdrv_dev)
  84. {
  85. struct device *dev = drm_dev->dev;
  86. int ret;
  87. if (!dev->archdata.mapping) {
  88. DRM_ERROR("iommu_mapping is null.\n");
  89. return -EFAULT;
  90. }
  91. subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev,
  92. sizeof(*subdrv_dev->dma_parms),
  93. GFP_KERNEL);
  94. dma_set_max_seg_size(subdrv_dev, 0xffffffffu);
  95. ret = arm_iommu_attach_device(subdrv_dev, dev->archdata.mapping);
  96. if (ret < 0) {
  97. DRM_DEBUG_KMS("failed iommu attach.\n");
  98. return ret;
  99. }
  100. /*
  101. * Set dma_ops to drm_device just one time.
  102. *
  103. * The dma mapping api needs device object and the api is used
  104. * to allocate physial memory and map it with iommu table.
  105. * If iommu attach succeeded, the sub driver would have dma_ops
  106. * for iommu and also all sub drivers have same dma_ops.
  107. */
  108. if (!dev->archdata.dma_ops)
  109. dev->archdata.dma_ops = subdrv_dev->archdata.dma_ops;
  110. return 0;
  111. }
  112. /*
  113. * drm_iommu_detach_device -detach device address space mapping from device
  114. *
  115. * @drm_dev: DRM device
  116. * @subdrv_dev: device to be detached
  117. *
  118. * This function should be called by sub drivers to detach it from iommu
  119. * mapping
  120. */
  121. void drm_iommu_detach_device(struct drm_device *drm_dev,
  122. struct device *subdrv_dev)
  123. {
  124. struct device *dev = drm_dev->dev;
  125. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  126. if (!mapping || !mapping->domain)
  127. return;
  128. iommu_detach_device(mapping->domain, subdrv_dev);
  129. drm_release_iommu_mapping(drm_dev);
  130. }