exynos_drm_buf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* exynos_drm_buf.c
  2. *
  3. * Copyright (c) 2011 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 <drm/drmP.h>
  26. #include <drm/exynos_drm.h>
  27. #include "exynos_drm_drv.h"
  28. #include "exynos_drm_gem.h"
  29. #include "exynos_drm_buf.h"
  30. static int lowlevel_buffer_allocate(struct drm_device *dev,
  31. unsigned int flags, struct exynos_drm_gem_buf *buf)
  32. {
  33. int ret = 0;
  34. enum dma_attr attr;
  35. unsigned int nr_pages;
  36. DRM_DEBUG_KMS("%s\n", __FILE__);
  37. if (buf->dma_addr) {
  38. DRM_DEBUG_KMS("already allocated.\n");
  39. return 0;
  40. }
  41. init_dma_attrs(&buf->dma_attrs);
  42. /*
  43. * if EXYNOS_BO_CONTIG, fully physically contiguous memory
  44. * region will be allocated else physically contiguous
  45. * as possible.
  46. */
  47. if (flags & EXYNOS_BO_CONTIG)
  48. dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &buf->dma_attrs);
  49. /*
  50. * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
  51. * else cachable mapping.
  52. */
  53. if (flags & EXYNOS_BO_WC || !(flags & EXYNOS_BO_CACHABLE))
  54. attr = DMA_ATTR_WRITE_COMBINE;
  55. else
  56. attr = DMA_ATTR_NON_CONSISTENT;
  57. dma_set_attr(attr, &buf->dma_attrs);
  58. dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &buf->dma_attrs);
  59. buf->pages = dma_alloc_attrs(dev->dev, buf->size,
  60. &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs);
  61. if (!buf->pages) {
  62. DRM_ERROR("failed to allocate buffer.\n");
  63. return -ENOMEM;
  64. }
  65. nr_pages = buf->size >> PAGE_SHIFT;
  66. buf->sgt = drm_prime_pages_to_sg(buf->pages, nr_pages);
  67. if (!buf->sgt) {
  68. DRM_ERROR("failed to get sg table.\n");
  69. ret = -ENOMEM;
  70. goto err_free_attrs;
  71. }
  72. DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
  73. (unsigned long)buf->dma_addr,
  74. buf->size);
  75. return ret;
  76. err_free_attrs:
  77. dma_free_attrs(dev->dev, buf->size, buf->pages,
  78. (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
  79. buf->dma_addr = (dma_addr_t)NULL;
  80. return ret;
  81. }
  82. static void lowlevel_buffer_deallocate(struct drm_device *dev,
  83. unsigned int flags, struct exynos_drm_gem_buf *buf)
  84. {
  85. DRM_DEBUG_KMS("%s.\n", __FILE__);
  86. if (!buf->dma_addr) {
  87. DRM_DEBUG_KMS("dma_addr is invalid.\n");
  88. return;
  89. }
  90. DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
  91. (unsigned long)buf->dma_addr,
  92. buf->size);
  93. sg_free_table(buf->sgt);
  94. kfree(buf->sgt);
  95. buf->sgt = NULL;
  96. dma_free_attrs(dev->dev, buf->size, buf->pages,
  97. (dma_addr_t)buf->dma_addr, &buf->dma_attrs);
  98. buf->dma_addr = (dma_addr_t)NULL;
  99. }
  100. struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
  101. unsigned int size)
  102. {
  103. struct exynos_drm_gem_buf *buffer;
  104. DRM_DEBUG_KMS("%s.\n", __FILE__);
  105. DRM_DEBUG_KMS("desired size = 0x%x\n", size);
  106. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  107. if (!buffer) {
  108. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  109. return NULL;
  110. }
  111. buffer->size = size;
  112. return buffer;
  113. }
  114. void exynos_drm_fini_buf(struct drm_device *dev,
  115. struct exynos_drm_gem_buf *buffer)
  116. {
  117. DRM_DEBUG_KMS("%s.\n", __FILE__);
  118. if (!buffer) {
  119. DRM_DEBUG_KMS("buffer is null.\n");
  120. return;
  121. }
  122. kfree(buffer);
  123. buffer = NULL;
  124. }
  125. int exynos_drm_alloc_buf(struct drm_device *dev,
  126. struct exynos_drm_gem_buf *buf, unsigned int flags)
  127. {
  128. /*
  129. * allocate memory region and set the memory information
  130. * to vaddr and dma_addr of a buffer object.
  131. */
  132. if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
  133. return -ENOMEM;
  134. return 0;
  135. }
  136. void exynos_drm_free_buf(struct drm_device *dev,
  137. unsigned int flags, struct exynos_drm_gem_buf *buffer)
  138. {
  139. lowlevel_buffer_deallocate(dev, flags, buffer);
  140. }