exynos_drm_iommu.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* exynos_drm_iommu.c
  2. *
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * Author: Inki Dae <inki.dae@samsung.com>
  5. *
  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.
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/exynos_drm.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/iommu.h>
  15. #include <linux/kref.h>
  16. #include <asm/dma-iommu.h>
  17. #include "exynos_drm_drv.h"
  18. #include "exynos_drm_iommu.h"
  19. /*
  20. * drm_create_iommu_mapping - create a mapping structure
  21. *
  22. * @drm_dev: DRM device
  23. */
  24. int drm_create_iommu_mapping(struct drm_device *drm_dev)
  25. {
  26. struct dma_iommu_mapping *mapping = NULL;
  27. struct exynos_drm_private *priv = drm_dev->dev_private;
  28. if (!priv->da_start)
  29. priv->da_start = EXYNOS_DEV_ADDR_START;
  30. if (!priv->da_space_size)
  31. priv->da_space_size = EXYNOS_DEV_ADDR_SIZE;
  32. mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
  33. priv->da_space_size);
  34. if (IS_ERR(mapping))
  35. return PTR_ERR(mapping);
  36. priv->mapping = mapping;
  37. return 0;
  38. }
  39. /*
  40. * drm_release_iommu_mapping - release iommu mapping structure
  41. *
  42. * @drm_dev: DRM device
  43. *
  44. * if mapping->kref becomes 0 then all things related to iommu mapping
  45. * will be released
  46. */
  47. void drm_release_iommu_mapping(struct drm_device *drm_dev)
  48. {
  49. struct exynos_drm_private *priv = drm_dev->dev_private;
  50. arm_iommu_release_mapping(priv->mapping);
  51. }
  52. /*
  53. * drm_iommu_attach_device- attach device to iommu mapping
  54. *
  55. * @drm_dev: DRM device
  56. * @subdrv_dev: device to be attach
  57. *
  58. * This function should be called by sub drivers to attach it to iommu
  59. * mapping.
  60. */
  61. int drm_iommu_attach_device(struct drm_device *drm_dev,
  62. struct device *subdrv_dev)
  63. {
  64. struct exynos_drm_private *priv = drm_dev->dev_private;
  65. int ret;
  66. if (!priv->mapping)
  67. return 0;
  68. subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev,
  69. sizeof(*subdrv_dev->dma_parms),
  70. GFP_KERNEL);
  71. if (!subdrv_dev->dma_parms)
  72. return -ENOMEM;
  73. dma_set_max_seg_size(subdrv_dev, 0xffffffffu);
  74. if (subdrv_dev->archdata.mapping)
  75. arm_iommu_detach_device(subdrv_dev);
  76. ret = arm_iommu_attach_device(subdrv_dev, priv->mapping);
  77. if (ret < 0) {
  78. DRM_DEBUG_KMS("failed iommu attach.\n");
  79. return ret;
  80. }
  81. return 0;
  82. }
  83. /*
  84. * drm_iommu_detach_device -detach device address space mapping from device
  85. *
  86. * @drm_dev: DRM device
  87. * @subdrv_dev: device to be detached
  88. *
  89. * This function should be called by sub drivers to detach it from iommu
  90. * mapping
  91. */
  92. void drm_iommu_detach_device(struct drm_device *drm_dev,
  93. struct device *subdrv_dev)
  94. {
  95. struct exynos_drm_private *priv = drm_dev->dev_private;
  96. struct dma_iommu_mapping *mapping = priv->mapping;
  97. if (!mapping || !mapping->domain)
  98. return;
  99. arm_iommu_detach_device(subdrv_dev);
  100. }