exynos_drm_iommu.c 3.4 KB

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