exynos_drm_buf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. dma_addr_t start_addr;
  34. unsigned int npages, i = 0;
  35. struct scatterlist *sgl;
  36. int ret = 0;
  37. DRM_DEBUG_KMS("%s\n", __FILE__);
  38. if (IS_NONCONTIG_BUFFER(flags)) {
  39. DRM_DEBUG_KMS("not support allocation type.\n");
  40. return -EINVAL;
  41. }
  42. if (buf->dma_addr) {
  43. DRM_DEBUG_KMS("already allocated.\n");
  44. return 0;
  45. }
  46. if (buf->size >= SZ_1M) {
  47. npages = buf->size >> SECTION_SHIFT;
  48. buf->page_size = SECTION_SIZE;
  49. } else if (buf->size >= SZ_64K) {
  50. npages = buf->size >> 16;
  51. buf->page_size = SZ_64K;
  52. } else {
  53. npages = buf->size >> PAGE_SHIFT;
  54. buf->page_size = PAGE_SIZE;
  55. }
  56. buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
  57. if (!buf->sgt) {
  58. DRM_ERROR("failed to allocate sg table.\n");
  59. return -ENOMEM;
  60. }
  61. ret = sg_alloc_table(buf->sgt, npages, GFP_KERNEL);
  62. if (ret < 0) {
  63. DRM_ERROR("failed to initialize sg table.\n");
  64. kfree(buf->sgt);
  65. buf->sgt = NULL;
  66. return -ENOMEM;
  67. }
  68. buf->kvaddr = dma_alloc_writecombine(dev->dev, buf->size,
  69. &buf->dma_addr, GFP_KERNEL);
  70. if (!buf->kvaddr) {
  71. DRM_ERROR("failed to allocate buffer.\n");
  72. ret = -ENOMEM;
  73. goto err1;
  74. }
  75. buf->pages = kzalloc(sizeof(struct page) * npages, GFP_KERNEL);
  76. if (!buf->pages) {
  77. DRM_ERROR("failed to allocate pages.\n");
  78. ret = -ENOMEM;
  79. goto err2;
  80. }
  81. sgl = buf->sgt->sgl;
  82. start_addr = buf->dma_addr;
  83. while (i < npages) {
  84. buf->pages[i] = phys_to_page(start_addr);
  85. sg_set_page(sgl, buf->pages[i], buf->page_size, 0);
  86. sg_dma_address(sgl) = start_addr;
  87. start_addr += buf->page_size;
  88. sgl = sg_next(sgl);
  89. i++;
  90. }
  91. DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
  92. (unsigned long)buf->kvaddr,
  93. (unsigned long)buf->dma_addr,
  94. buf->size);
  95. return ret;
  96. err2:
  97. dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
  98. (dma_addr_t)buf->dma_addr);
  99. buf->dma_addr = (dma_addr_t)NULL;
  100. err1:
  101. sg_free_table(buf->sgt);
  102. kfree(buf->sgt);
  103. buf->sgt = NULL;
  104. return ret;
  105. }
  106. static void lowlevel_buffer_deallocate(struct drm_device *dev,
  107. unsigned int flags, struct exynos_drm_gem_buf *buf)
  108. {
  109. DRM_DEBUG_KMS("%s.\n", __FILE__);
  110. /*
  111. * release only physically continuous memory and
  112. * non-continuous memory would be released by exynos
  113. * gem framework.
  114. */
  115. if (IS_NONCONTIG_BUFFER(flags)) {
  116. DRM_DEBUG_KMS("not support allocation type.\n");
  117. return;
  118. }
  119. if (!buf->dma_addr) {
  120. DRM_DEBUG_KMS("dma_addr is invalid.\n");
  121. return;
  122. }
  123. DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
  124. (unsigned long)buf->kvaddr,
  125. (unsigned long)buf->dma_addr,
  126. buf->size);
  127. sg_free_table(buf->sgt);
  128. kfree(buf->sgt);
  129. buf->sgt = NULL;
  130. kfree(buf->pages);
  131. buf->pages = NULL;
  132. dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
  133. (dma_addr_t)buf->dma_addr);
  134. buf->dma_addr = (dma_addr_t)NULL;
  135. }
  136. struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
  137. unsigned int size)
  138. {
  139. struct exynos_drm_gem_buf *buffer;
  140. DRM_DEBUG_KMS("%s.\n", __FILE__);
  141. DRM_DEBUG_KMS("desired size = 0x%x\n", size);
  142. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  143. if (!buffer) {
  144. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  145. return NULL;
  146. }
  147. buffer->size = size;
  148. return buffer;
  149. }
  150. void exynos_drm_fini_buf(struct drm_device *dev,
  151. struct exynos_drm_gem_buf *buffer)
  152. {
  153. DRM_DEBUG_KMS("%s.\n", __FILE__);
  154. if (!buffer) {
  155. DRM_DEBUG_KMS("buffer is null.\n");
  156. return;
  157. }
  158. kfree(buffer);
  159. buffer = NULL;
  160. }
  161. int exynos_drm_alloc_buf(struct drm_device *dev,
  162. struct exynos_drm_gem_buf *buf, unsigned int flags)
  163. {
  164. /*
  165. * allocate memory region and set the memory information
  166. * to vaddr and dma_addr of a buffer object.
  167. */
  168. if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
  169. return -ENOMEM;
  170. return 0;
  171. }
  172. void exynos_drm_free_buf(struct drm_device *dev,
  173. unsigned int flags, struct exynos_drm_gem_buf *buffer)
  174. {
  175. lowlevel_buffer_deallocate(dev, flags, buffer);
  176. }