exynos_drm_fb.c 6.0 KB

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