exynos_drm_buf.c 5.2 KB

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