exynos_drm_buf.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 (flags & EXYNOS_BO_NONCONTIG) {
  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) + 1;
  49. page_size = SECTION_SIZE;
  50. } else if (buf->size >= SZ_64K) {
  51. npages = (buf->size >> 16) + 1;
  52. page_size = SZ_64K;
  53. } else {
  54. npages = (buf->size >> PAGE_SHIFT) + 1;
  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. sgl = sg_next(sgl);
  105. sg_set_page(sgl, buf->pages[i+1], end_addr - start_addr, 0);
  106. DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
  107. (unsigned long)buf->kvaddr,
  108. (unsigned long)buf->dma_addr,
  109. buf->size);
  110. return ret;
  111. err2:
  112. dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
  113. (dma_addr_t)buf->dma_addr);
  114. buf->dma_addr = (dma_addr_t)NULL;
  115. err1:
  116. sg_free_table(buf->sgt);
  117. kfree(buf->sgt);
  118. buf->sgt = NULL;
  119. return ret;
  120. }
  121. static void lowlevel_buffer_deallocate(struct drm_device *dev,
  122. unsigned int flags, struct exynos_drm_gem_buf *buf)
  123. {
  124. DRM_DEBUG_KMS("%s.\n", __FILE__);
  125. /*
  126. * release only physically continuous memory and
  127. * non-continuous memory would be released by exynos
  128. * gem framework.
  129. */
  130. if (flags & EXYNOS_BO_NONCONTIG) {
  131. DRM_DEBUG_KMS("not support allocation type.\n");
  132. return;
  133. }
  134. if (!buf->dma_addr) {
  135. DRM_DEBUG_KMS("dma_addr is invalid.\n");
  136. return;
  137. }
  138. DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n",
  139. (unsigned long)buf->kvaddr,
  140. (unsigned long)buf->dma_addr,
  141. buf->size);
  142. sg_free_table(buf->sgt);
  143. kfree(buf->sgt);
  144. buf->sgt = NULL;
  145. kfree(buf->pages);
  146. buf->pages = NULL;
  147. dma_free_writecombine(dev->dev, buf->size, buf->kvaddr,
  148. (dma_addr_t)buf->dma_addr);
  149. buf->dma_addr = (dma_addr_t)NULL;
  150. }
  151. struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev,
  152. unsigned int size)
  153. {
  154. struct exynos_drm_gem_buf *buffer;
  155. DRM_DEBUG_KMS("%s.\n", __FILE__);
  156. DRM_DEBUG_KMS("desired size = 0x%x\n", size);
  157. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  158. if (!buffer) {
  159. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  160. return NULL;
  161. }
  162. buffer->size = size;
  163. return buffer;
  164. }
  165. void exynos_drm_fini_buf(struct drm_device *dev,
  166. struct exynos_drm_gem_buf *buffer)
  167. {
  168. DRM_DEBUG_KMS("%s.\n", __FILE__);
  169. if (!buffer) {
  170. DRM_DEBUG_KMS("buffer is null.\n");
  171. return;
  172. }
  173. kfree(buffer);
  174. buffer = NULL;
  175. }
  176. int exynos_drm_alloc_buf(struct drm_device *dev,
  177. struct exynos_drm_gem_buf *buf, unsigned int flags)
  178. {
  179. /*
  180. * allocate memory region and set the memory information
  181. * to vaddr and dma_addr of a buffer object.
  182. */
  183. if (lowlevel_buffer_allocate(dev, flags, buf) < 0)
  184. return -ENOMEM;
  185. return 0;
  186. }
  187. void exynos_drm_free_buf(struct drm_device *dev,
  188. unsigned int flags, struct exynos_drm_gem_buf *buffer)
  189. {
  190. lowlevel_buffer_deallocate(dev, flags, buffer);
  191. }