exynos_drm_fb.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <uapi/drm/exynos_drm.h>
  19. #include "exynos_drm_drv.h"
  20. #include "exynos_drm_fb.h"
  21. #include "exynos_drm_fbdev.h"
  22. #include "exynos_drm_gem.h"
  23. #include "exynos_drm_iommu.h"
  24. #include "exynos_drm_crtc.h"
  25. #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
  26. /*
  27. * exynos specific framebuffer structure.
  28. *
  29. * @fb: drm framebuffer obejct.
  30. * @buf_cnt: a buffer count to drm framebuffer.
  31. * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
  32. */
  33. struct exynos_drm_fb {
  34. struct drm_framebuffer fb;
  35. unsigned int buf_cnt;
  36. struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
  37. };
  38. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  39. struct exynos_drm_gem_obj *exynos_gem_obj)
  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_obj->flags;
  49. /*
  50. * without iommu support, not support physically non-continuous memory
  51. * for framebuffer.
  52. */
  53. if (IS_NONCONTIG_BUFFER(flags)) {
  54. DRM_ERROR("cannot use this gem memory type for fb.\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. /* make sure that overlay data are updated before relesing fb. */
  64. exynos_drm_crtc_complete_scanout(fb);
  65. drm_framebuffer_cleanup(fb);
  66. for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
  67. struct drm_gem_object *obj;
  68. if (exynos_fb->exynos_gem_obj[i] == NULL)
  69. continue;
  70. obj = &exynos_fb->exynos_gem_obj[i]->base;
  71. drm_gem_object_unreference_unlocked(obj);
  72. }
  73. kfree(exynos_fb);
  74. exynos_fb = NULL;
  75. }
  76. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  77. struct drm_file *file_priv,
  78. unsigned int *handle)
  79. {
  80. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  81. /* This fb should have only one gem object. */
  82. if (WARN_ON(exynos_fb->buf_cnt != 1))
  83. return -EINVAL;
  84. return drm_gem_handle_create(file_priv,
  85. &exynos_fb->exynos_gem_obj[0]->base, handle);
  86. }
  87. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  88. struct drm_file *file_priv, unsigned flags,
  89. unsigned color, struct drm_clip_rect *clips,
  90. unsigned num_clips)
  91. {
  92. /* TODO */
  93. return 0;
  94. }
  95. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  96. .destroy = exynos_drm_fb_destroy,
  97. .create_handle = exynos_drm_fb_create_handle,
  98. .dirty = exynos_drm_fb_dirty,
  99. };
  100. void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
  101. unsigned int cnt)
  102. {
  103. struct exynos_drm_fb *exynos_fb;
  104. exynos_fb = to_exynos_fb(fb);
  105. exynos_fb->buf_cnt = cnt;
  106. }
  107. unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
  108. {
  109. struct exynos_drm_fb *exynos_fb;
  110. exynos_fb = to_exynos_fb(fb);
  111. return exynos_fb->buf_cnt;
  112. }
  113. struct drm_framebuffer *
  114. exynos_drm_framebuffer_init(struct drm_device *dev,
  115. struct drm_mode_fb_cmd2 *mode_cmd,
  116. struct drm_gem_object *obj)
  117. {
  118. struct exynos_drm_fb *exynos_fb;
  119. struct exynos_drm_gem_obj *exynos_gem_obj;
  120. int ret;
  121. exynos_gem_obj = to_exynos_gem_obj(obj);
  122. ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
  123. if (ret < 0)
  124. return ERR_PTR(ret);
  125. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  126. if (!exynos_fb)
  127. return ERR_PTR(-ENOMEM);
  128. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  129. exynos_fb->exynos_gem_obj[0] = exynos_gem_obj;
  130. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  131. if (ret) {
  132. kfree(exynos_fb);
  133. DRM_ERROR("failed to initialize framebuffer\n");
  134. return ERR_PTR(ret);
  135. }
  136. return &exynos_fb->fb;
  137. }
  138. static struct drm_framebuffer *
  139. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  140. struct drm_mode_fb_cmd2 *mode_cmd)
  141. {
  142. struct drm_gem_object *obj;
  143. struct exynos_drm_gem_obj *exynos_gem_obj;
  144. struct exynos_drm_fb *exynos_fb;
  145. int i, ret;
  146. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  147. if (!exynos_fb)
  148. return ERR_PTR(-ENOMEM);
  149. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  150. if (!obj) {
  151. DRM_ERROR("failed to lookup gem object\n");
  152. ret = -ENOENT;
  153. goto err_free;
  154. }
  155. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  156. exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
  157. exynos_fb->buf_cnt = drm_format_num_planes(mode_cmd->pixel_format);
  158. DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
  159. for (i = 1; i < exynos_fb->buf_cnt; i++) {
  160. obj = drm_gem_object_lookup(dev, file_priv,
  161. mode_cmd->handles[i]);
  162. if (!obj) {
  163. DRM_ERROR("failed to lookup gem object\n");
  164. ret = -ENOENT;
  165. exynos_fb->buf_cnt = i;
  166. goto err_unreference;
  167. }
  168. exynos_gem_obj = to_exynos_gem_obj(obj);
  169. exynos_fb->exynos_gem_obj[i] = exynos_gem_obj;
  170. ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
  171. if (ret < 0)
  172. goto err_unreference;
  173. }
  174. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  175. if (ret) {
  176. DRM_ERROR("failed to init framebuffer.\n");
  177. goto err_unreference;
  178. }
  179. return &exynos_fb->fb;
  180. err_unreference:
  181. for (i = 0; i < exynos_fb->buf_cnt; i++) {
  182. struct drm_gem_object *obj;
  183. obj = &exynos_fb->exynos_gem_obj[i]->base;
  184. if (obj)
  185. drm_gem_object_unreference_unlocked(obj);
  186. }
  187. err_free:
  188. kfree(exynos_fb);
  189. return ERR_PTR(ret);
  190. }
  191. struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
  192. int index)
  193. {
  194. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  195. struct exynos_drm_gem_buf *buffer;
  196. if (index >= MAX_FB_BUFFER)
  197. return NULL;
  198. buffer = exynos_fb->exynos_gem_obj[index]->buffer;
  199. if (!buffer)
  200. return NULL;
  201. DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr);
  202. return buffer;
  203. }
  204. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  205. {
  206. struct exynos_drm_private *private = dev->dev_private;
  207. struct drm_fb_helper *fb_helper = private->fb_helper;
  208. if (fb_helper)
  209. drm_fb_helper_hotplug_event(fb_helper);
  210. else
  211. exynos_drm_fbdev_init(dev);
  212. }
  213. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  214. .fb_create = exynos_user_fb_create,
  215. .output_poll_changed = exynos_drm_output_poll_changed,
  216. };
  217. void exynos_drm_mode_config_init(struct drm_device *dev)
  218. {
  219. dev->mode_config.min_width = 0;
  220. dev->mode_config.min_height = 0;
  221. /*
  222. * set max width and height as default value(4096x4096).
  223. * this value would be used to check framebuffer size limitation
  224. * at drm_mode_addfb().
  225. */
  226. dev->mode_config.max_width = 4096;
  227. dev->mode_config.max_height = 4096;
  228. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  229. }