exynos_drm_fb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. };
  37. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  38. struct exynos_drm_gem *exynos_gem)
  39. {
  40. unsigned int flags;
  41. /*
  42. * if exynos drm driver supports iommu then framebuffer can use
  43. * all the buffer types.
  44. */
  45. if (is_drm_iommu_supported(drm_dev))
  46. return 0;
  47. flags = exynos_gem->flags;
  48. /*
  49. * without iommu support, not support physically non-continuous memory
  50. * for framebuffer.
  51. */
  52. if (IS_NONCONTIG_BUFFER(flags)) {
  53. DRM_ERROR("cannot use this gem memory type for fb.\n");
  54. return -EINVAL;
  55. }
  56. return 0;
  57. }
  58. static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
  59. {
  60. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  61. unsigned int i;
  62. /* make sure that overlay data are updated before relesing fb. */
  63. exynos_drm_crtc_complete_scanout(fb);
  64. drm_framebuffer_cleanup(fb);
  65. for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem); i++) {
  66. struct drm_gem_object *obj;
  67. if (exynos_fb->exynos_gem[i] == NULL)
  68. continue;
  69. obj = &exynos_fb->exynos_gem[i]->base;
  70. drm_gem_object_unreference_unlocked(obj);
  71. }
  72. kfree(exynos_fb);
  73. exynos_fb = NULL;
  74. }
  75. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  76. struct drm_file *file_priv,
  77. unsigned int *handle)
  78. {
  79. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  80. return drm_gem_handle_create(file_priv,
  81. &exynos_fb->exynos_gem[0]->base, handle);
  82. }
  83. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  84. struct drm_file *file_priv, unsigned flags,
  85. unsigned color, struct drm_clip_rect *clips,
  86. unsigned num_clips)
  87. {
  88. /* TODO */
  89. return 0;
  90. }
  91. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  92. .destroy = exynos_drm_fb_destroy,
  93. .create_handle = exynos_drm_fb_create_handle,
  94. .dirty = exynos_drm_fb_dirty,
  95. };
  96. struct drm_framebuffer *
  97. exynos_drm_framebuffer_init(struct drm_device *dev,
  98. struct drm_mode_fb_cmd2 *mode_cmd,
  99. struct exynos_drm_gem **exynos_gem,
  100. int count)
  101. {
  102. struct exynos_drm_fb *exynos_fb;
  103. int i;
  104. int ret;
  105. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  106. if (!exynos_fb)
  107. return ERR_PTR(-ENOMEM);
  108. for (i = 0; i < count; i++) {
  109. ret = check_fb_gem_memory_type(dev, exynos_gem[i]);
  110. if (ret < 0)
  111. goto err;
  112. exynos_fb->exynos_gem[i] = exynos_gem[i];
  113. }
  114. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  115. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  116. if (ret < 0) {
  117. DRM_ERROR("failed to initialize framebuffer\n");
  118. goto err;
  119. }
  120. return &exynos_fb->fb;
  121. err:
  122. kfree(exynos_fb);
  123. return ERR_PTR(ret);
  124. }
  125. static struct drm_framebuffer *
  126. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  127. struct drm_mode_fb_cmd2 *mode_cmd)
  128. {
  129. struct exynos_drm_gem *exynos_gem[MAX_FB_BUFFER];
  130. struct drm_gem_object *obj;
  131. struct drm_framebuffer *fb;
  132. int i;
  133. int ret;
  134. for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) {
  135. obj = drm_gem_object_lookup(dev, file_priv,
  136. mode_cmd->handles[i]);
  137. if (!obj) {
  138. DRM_ERROR("failed to lookup gem object\n");
  139. ret = -ENOENT;
  140. goto err;
  141. }
  142. exynos_gem[i] = to_exynos_gem(obj);
  143. }
  144. fb = exynos_drm_framebuffer_init(dev, mode_cmd, exynos_gem, i);
  145. if (IS_ERR(fb)) {
  146. ret = PTR_ERR(fb);
  147. goto err;
  148. }
  149. return fb;
  150. err:
  151. while (i--)
  152. drm_gem_object_unreference_unlocked(&exynos_gem[i]->base);
  153. return ERR_PTR(ret);
  154. }
  155. struct exynos_drm_gem *exynos_drm_fb_gem(struct drm_framebuffer *fb, int index)
  156. {
  157. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  158. struct exynos_drm_gem *exynos_gem;
  159. if (index >= MAX_FB_BUFFER)
  160. return NULL;
  161. exynos_gem = exynos_fb->exynos_gem[index];
  162. if (!exynos_gem)
  163. return NULL;
  164. DRM_DEBUG_KMS("dma_addr: 0x%lx\n", (unsigned long)exynos_gem->dma_addr);
  165. return exynos_gem;
  166. }
  167. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  168. {
  169. struct exynos_drm_private *private = dev->dev_private;
  170. struct drm_fb_helper *fb_helper = private->fb_helper;
  171. if (fb_helper)
  172. drm_fb_helper_hotplug_event(fb_helper);
  173. else
  174. exynos_drm_fbdev_init(dev);
  175. }
  176. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  177. .fb_create = exynos_user_fb_create,
  178. .output_poll_changed = exynos_drm_output_poll_changed,
  179. .atomic_check = drm_atomic_helper_check,
  180. .atomic_commit = exynos_atomic_commit,
  181. };
  182. void exynos_drm_mode_config_init(struct drm_device *dev)
  183. {
  184. dev->mode_config.min_width = 0;
  185. dev->mode_config.min_height = 0;
  186. /*
  187. * set max width and height as default value(4096x4096).
  188. * this value would be used to check framebuffer size limitation
  189. * at drm_mode_addfb().
  190. */
  191. dev->mode_config.max_width = 4096;
  192. dev->mode_config.max_height = 4096;
  193. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  194. }