exynos_drm_fb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* exynos_drm_fb.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_helper.h>
  18. #include <drm/drm_atomic.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <uapi/drm/exynos_drm.h>
  21. #include "exynos_drm_drv.h"
  22. #include "exynos_drm_fb.h"
  23. #include "exynos_drm_fbdev.h"
  24. #include "exynos_drm_iommu.h"
  25. #include "exynos_drm_crtc.h"
  26. #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
  27. /*
  28. * exynos specific framebuffer structure.
  29. *
  30. * @fb: drm framebuffer obejct.
  31. * @exynos_gem: array of exynos specific gem object containing a gem object.
  32. */
  33. struct exynos_drm_fb {
  34. struct drm_framebuffer fb;
  35. struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
  36. dma_addr_t dma_addr[MAX_FB_BUFFER];
  37. };
  38. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  39. struct exynos_drm_gem *exynos_gem)
  40. {
  41. unsigned int flags;
  42. /*
  43. * if exynos drm driver supports iommu then framebuffer can use
  44. * all the buffer types.
  45. */
  46. if (is_drm_iommu_supported(drm_dev))
  47. return 0;
  48. flags = exynos_gem->flags;
  49. /*
  50. * Physically non-contiguous memory type for framebuffer is not
  51. * supported without IOMMU.
  52. */
  53. if (IS_NONCONTIG_BUFFER(flags)) {
  54. DRM_ERROR("Non-contiguous GEM memory is not supported.\n");
  55. return -EINVAL;
  56. }
  57. return 0;
  58. }
  59. static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
  60. {
  61. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  62. unsigned int i;
  63. drm_framebuffer_cleanup(fb);
  64. for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) {
  65. struct drm_gem_object *obj;
  66. if (exynos_fb->exynos_gem[i] == NULL)
  67. continue;
  68. obj = &exynos_fb->exynos_gem[i]->base;
  69. drm_gem_object_unreference_unlocked(obj);
  70. }
  71. kfree(exynos_fb);
  72. exynos_fb = NULL;
  73. }
  74. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  75. struct drm_file *file_priv,
  76. unsigned int *handle)
  77. {
  78. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  79. return drm_gem_handle_create(file_priv,
  80. &exynos_fb->exynos_gem[0]->base, handle);
  81. }
  82. static const struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  83. .destroy = exynos_drm_fb_destroy,
  84. .create_handle = exynos_drm_fb_create_handle,
  85. };
  86. struct drm_framebuffer *
  87. exynos_drm_framebuffer_init(struct drm_device *dev,
  88. const struct drm_mode_fb_cmd2 *mode_cmd,
  89. struct exynos_drm_gem **exynos_gem,
  90. int count)
  91. {
  92. struct exynos_drm_fb *exynos_fb;
  93. int i;
  94. int ret;
  95. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  96. if (!exynos_fb)
  97. return ERR_PTR(-ENOMEM);
  98. for (i = 0; i < count; i++) {
  99. ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
  100. if (ret < 0)
  101. goto err;
  102. exynos_fb->exynos_gem[i] = exynos_gem[i];
  103. exynos_fb->dma_addr[i] = exynos_gem[i]->dma_addr
  104. + mode_cmd->offsets[i];
  105. }
  106. drm_helper_mode_fill_fb_struct(dev, &exynos_fb->fb, mode_cmd);
  107. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  108. if (ret < 0) {
  109. DRM_ERROR("failed to initialize framebuffer\n");
  110. goto err;
  111. }
  112. return &exynos_fb->fb;
  113. err:
  114. kfree(exynos_fb);
  115. return ERR_PTR(ret);
  116. }
  117. static struct drm_framebuffer *
  118. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  119. const struct drm_mode_fb_cmd2 *mode_cmd)
  120. {
  121. const struct drm_format_info *info = drm_get_format_info(dev, mode_cmd);
  122. struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
  123. struct drm_gem_object *obj;
  124. struct drm_framebuffer *fb;
  125. int i;
  126. int ret;
  127. for (i = 0; i < info->num_planes; i++) {
  128. unsigned int height = (i == 0) ? mode_cmd->height :
  129. DIV_ROUND_UP(mode_cmd->height, info->vsub);
  130. unsigned long size = height * mode_cmd->pitches[i] +
  131. mode_cmd->offsets[i];
  132. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
  133. if (!obj) {
  134. DRM_ERROR("failed to lookup gem object\n");
  135. ret = -ENOENT;
  136. goto err;
  137. }
  138. exynos_gem[i] = to_exynos_gem(obj);
  139. if (size > exynos_gem[i]->size) {
  140. i++;
  141. ret = -EINVAL;
  142. goto err;
  143. }
  144. }
  145. fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
  146. if (IS_ERR(fb)) {
  147. ret = PTR_ERR(fb);
  148. goto err;
  149. }
  150. return fb;
  151. err:
  152. while (i--)
  153. drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
  154. return ERR_PTR(ret);
  155. }
  156. dma_addr_t exynos_drm_fb_dma_addr(struct drm_framebuffer *fb, int index)
  157. {
  158. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  159. if (WARN_ON_ONCE(index >= MAX_FB_BUFFER))
  160. return 0;
  161. return exynos_fb->dma_addr[index];
  162. }
  163. static struct drm_mode_config_helper_funcs exynos_drm_mode_config_helpers = {
  164. .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
  165. };
  166. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  167. .fb_create = exynos_user_fb_create,
  168. .output_poll_changed = exynos_drm_output_poll_changed,
  169. .atomic_check = exynos_atomic_check,
  170. .atomic_commit = drm_atomic_helper_commit,
  171. };
  172. void exynos_drm_mode_config_init(struct drm_device *dev)
  173. {
  174. dev->mode_config.min_width = 0;
  175. dev->mode_config.min_height = 0;
  176. /*
  177. * set max width and height as default value(4096x4096).
  178. * this value would be used to check framebuffer size limitation
  179. * at drm_mode_addfb().
  180. */
  181. dev->mode_config.max_width = 4096;
  182. dev->mode_config.max_height = 4096;
  183. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  184. dev->mode_config.helper_private = &exynos_drm_mode_config_helpers;
  185. dev->mode_config.allow_fb_modifiers = true;
  186. }