exynos_drm_iommu.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <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. struct device *dev = drm_dev->dev;
  29. if (!priv->da_start)
  30. priv->da_start = EXYNOS_DEV_ADDR_START;
  31. if (!priv->da_space_size)
  32. priv->da_space_size = EXYNOS_DEV_ADDR_SIZE;
  33. if (!priv->da_space_order)
  34. priv->da_space_order = EXYNOS_DEV_ADDR_ORDER;
  35. mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
  36. priv->da_space_size,
  37. priv->da_space_order);
  38. if (IS_ERR(mapping))
  39. return PTR_ERR(mapping);
  40. dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
  41. GFP_KERNEL);
  42. if (!dev->dma_parms)
  43. goto error;
  44. dma_set_max_seg_size(dev, 0xffffffffu);
  45. dev->archdata.mapping = mapping;
  46. return 0;
  47. error:
  48. arm_iommu_release_mapping(mapping);
  49. return -ENOMEM;
  50. }
  51. /*
  52. * drm_release_iommu_mapping - release iommu mapping structure
  53. *
  54. * @drm_dev: DRM device
  55. *
  56. * if mapping->kref becomes 0 then all things related to iommu mapping
  57. * will be released
  58. */
  59. void drm_release_iommu_mapping(struct drm_device *drm_dev)
  60. {
  61. struct device *dev = drm_dev->dev;
  62. arm_iommu_release_mapping(dev->archdata.mapping);
  63. }
  64. /*
  65. * drm_iommu_attach_device- attach device to iommu mapping
  66. *
  67. * @drm_dev: DRM device
  68. * @subdrv_dev: device to be attach
  69. *
  70. * This function should be called by sub drivers to attach it to iommu
  71. * mapping.
  72. */
  73. int drm_iommu_attach_device(struct drm_device *drm_dev,
  74. struct device *subdrv_dev)
  75. {
  76. struct device *dev = drm_dev->dev;
  77. int ret;
  78. if (!dev->archdata.mapping) {
  79. DRM_ERROR("iommu_mapping is null.\n");
  80. return -EFAULT;
  81. }
  82. subdrv_dev->dma_parms = devm_kzalloc(subdrv_dev,
  83. sizeof(*subdrv_dev->dma_parms),
  84. GFP_KERNEL);
  85. if (!subdrv_dev->dma_parms)
  86. return -ENOMEM;
  87. dma_set_max_seg_size(subdrv_dev, 0xffffffffu);
  88. ret = arm_iommu_attach_device(subdrv_dev, dev->archdata.mapping);
  89. if (ret < 0) {
  90. DRM_DEBUG_KMS("failed iommu attach.\n");
  91. return ret;
  92. }
  93. /*
  94. * Set dma_ops to drm_device just one time.
  95. *
  96. * The dma mapping api needs device object and the api is used
  97. * to allocate physial memory and map it with iommu table.
  98. * If iommu attach succeeded, the sub driver would have dma_ops
  99. * for iommu and also all sub drivers have same dma_ops.
  100. */
  101. if (!dev->archdata.dma_ops)
  102. dev->archdata.dma_ops = subdrv_dev->archdata.dma_ops;
  103. return 0;
  104. }
  105. /*
  106. * drm_iommu_detach_device -detach device address space mapping from device
  107. *
  108. * @drm_dev: DRM device
  109. * @subdrv_dev: device to be detached
  110. *
  111. * This function should be called by sub drivers to detach it from iommu
  112. * mapping
  113. */
  114. void drm_iommu_detach_device(struct drm_device *drm_dev,
  115. struct device *subdrv_dev)
  116. {
  117. struct device *dev = drm_dev->dev;
  118. struct dma_iommu_mapping *mapping = dev->archdata.mapping;
  119. if (!mapping || !mapping->domain)
  120. return;
  121. iommu_detach_device(mapping->domain, subdrv_dev);
  122. drm_release_iommu_mapping(drm_dev);
  123. }