exynos_drm_iommu.c 3.4 KB

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