exynos_drm_buf.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, end_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. start_addr = buf->dma_addr;
  77. end_addr = buf->dma_addr + buf->size;
  78. buf->pages = kzalloc(sizeof(struct page) * npages, GFP_KERNEL);
  79. if (!buf->pages) {
  80. DRM_ERROR("failed to allocate pages.\n");
  81. ret = -ENOMEM;
  82. goto err2;
  83. }
  84. start_addr = buf->dma_addr;
  85. end_addr = buf->dma_addr + buf->size;
  86. buf->pages = kzalloc(sizeof(struct page) * npages, GFP_KERNEL);
  87. if (!buf->pages) {
  88. DRM_ERROR("failed to allocate pages.\n");
  89. ret = -ENOMEM;
  90. goto err2;
  91. }
  92. sgl = buf->sgt->sgl;
  93. while (i < npages) {
  94. buf->pages[i] = phys_to_page(start_addr);
  95. sg_set_page(sgl, buf->pages[i], page_size, 0);
  96. sg_dma_address(sgl) = start_addr;
  97. start_addr += page_size;
  98. if (end_addr - start_addr < page_size)
  99. break;
  100. sgl = sg_next(sgl);
  101. i++;
  102. }
  103. buf->pages[i] = phys_to_page(start_addr);
  104. DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
  105. (unsigned long)buf->kvaddr,
  106. (unsigned long)buf->dma_addr,
  107. buf->size);
  108. return ret;
  109. err2:
  110. dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
  111. (dma_addr_t)buf->dma_addr);
  112. buf->dma_addr = (dma_addr_t)NULL;
  113. err1:
  114. sg_free_table(buf->sgt);
  115. kfree(buf->sgt);
  116. buf->sgt = NULL;
  117. return ret;
  118. }
  119. static void lowlevel_buffer_deallocate(struct drm_device *dev,
  120. unsigned int flags, struct exynos_drm_gem_buf *buf)
  121. {
  122. DRM_DEBUG_KMS("%s.\n", __FILE__);
  123. /*
  124. * release only physically continuous memory and
  125. * non-continuous memory would be released by exynos
  126. * gem framework.
  127. */
  128. if (IS_NONCONTIG_BUFFER(flags)) {
  129. DRM_DEBUG_KMS("not support allocation type.\n");
  130. return;
  131. }
  132. if (!buf->dma_addr) {
  133. DRM_DEBUG_KMS("dma_addr is invalid.\n");
  134. return;
  135. }
  136. DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
  137. (unsigned long)buf->kvaddr,
  138. (unsigned long)buf->dma_addr,
  139. buf->size);
  140. sg_free_table(buf->sgt);
  141. kfree(buf->sgt);
  142. buf->sgt = NULL;
  143. kfree(buf->pages);
  144. buf->pages = NULL;
  145. dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
  146. (dma_addr_t)buf->dma_addr);
  147. buf->dma_addr = (dma_addr_t)NULL;
  148. }
  149. struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
  150. unsigned int size)
  151. {
  152. struct exynos_drm_gem_buf *buffer;
  153. DRM_DEBUG_KMS("%s.\n", __FILE__);
  154. DRM_DEBUG_KMS("desired size = 0x%x\n", size);
  155. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  156. if (!buffer) {
  157. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  158. return NULL;
  159. }
  160. buffer->size = size;
  161. return buffer;
  162. }
  163. void exynos_drm_fini_buf(struct drm_device *dev,
  164. struct exynos_drm_gem_buf *buffer)
  165. {
  166. DRM_DEBUG_KMS("%s.\n", __FILE__);
  167. if (!buffer) {
  168. DRM_DEBUG_KMS("buffer is null.\n");
  169. return;
  170. }
  171. kfree(buffer);
  172. buffer = NULL;
  173. }
  174. int exynos_drm_alloc_buf(struct drm_device *dev,
  175. struct exynos_drm_gem_buf *buf, unsigned int flags)
  176. {
  177. /*
  178. * allocate memory region and set the memory information
  179. * to vaddr and dma_addr of a buffer object.
  180. */
  181. if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
  182. return -ENOMEM;
  183. return 0;
  184. }
  185. void exynos_drm_free_buf(struct drm_device *dev,
  186. unsigned int flags, struct exynos_drm_gem_buf *buffer)
  187. {
  188. lowlevel_buffer_deallocate(dev, flags, buffer);
  189. }