exynos_drm_fb.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_gem.h"
  22. #include "exynos_drm_iommu.h"
  23. #include "exynos_drm_encoder.h"
  24. #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
  25. /*
  26. * exynos specific framebuffer structure.
  27. *
  28. * @fb: drm framebuffer obejct.
  29. * @buf_cnt: a buffer count to drm framebuffer.
  30. * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
  31. */
  32. struct exynos_drm_fb {
  33. struct drm_framebuffer fb;
  34. unsigned int buf_cnt;
  35. struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
  36. };
  37. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  38. struct exynos_drm_gem_obj *exynos_gem_obj)
  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_obj->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. DRM_DEBUG_KMS("%s\n", __FILE__);
  63. /* make sure that overlay data are updated before relesing fb. */
  64. exynos_drm_encoder_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. DRM_DEBUG_KMS("%s\n", __FILE__);
  82. return drm_gem_handle_create(file_priv,
  83. &exynos_fb->exynos_gem_obj[0]->base, handle);
  84. }
  85. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  86. struct drm_file *file_priv, unsigned flags,
  87. unsigned color, struct drm_clip_rect *clips,
  88. unsigned num_clips)
  89. {
  90. DRM_DEBUG_KMS("%s\n", __FILE__);
  91. /* TODO */
  92. return 0;
  93. }
  94. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  95. .destroy = exynos_drm_fb_destroy,
  96. .create_handle = exynos_drm_fb_create_handle,
  97. .dirty = exynos_drm_fb_dirty,
  98. };
  99. void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
  100. unsigned int cnt)
  101. {
  102. struct exynos_drm_fb *exynos_fb;
  103. exynos_fb = to_exynos_fb(fb);
  104. exynos_fb->buf_cnt = cnt;
  105. }
  106. unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
  107. {
  108. struct exynos_drm_fb *exynos_fb;
  109. exynos_fb = to_exynos_fb(fb);
  110. return exynos_fb->buf_cnt;
  111. }
  112. struct drm_framebuffer *
  113. exynos_drm_framebuffer_init(struct drm_device *dev,
  114. struct drm_mode_fb_cmd2 *mode_cmd,
  115. struct drm_gem_object *obj)
  116. {
  117. struct exynos_drm_fb *exynos_fb;
  118. struct exynos_drm_gem_obj *exynos_gem_obj;
  119. int ret;
  120. exynos_gem_obj = to_exynos_gem_obj(obj);
  121. ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
  122. if (ret < 0) {
  123. DRM_ERROR("cannot use this gem memory type for fb.\n");
  124. return ERR_PTR(-EINVAL);
  125. }
  126. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  127. if (!exynos_fb) {
  128. DRM_ERROR("failed to allocate exynos drm framebuffer\n");
  129. return ERR_PTR(-ENOMEM);
  130. }
  131. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  132. exynos_fb->exynos_gem_obj[0] = exynos_gem_obj;
  133. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  134. if (ret) {
  135. DRM_ERROR("failed to initialize framebuffer\n");
  136. return ERR_PTR(ret);
  137. }
  138. return &exynos_fb->fb;
  139. }
  140. static u32 exynos_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd)
  141. {
  142. unsigned int cnt = 0;
  143. if (mode_cmd->pixel_format != DRM_FORMAT_NV12)
  144. return drm_format_num_planes(mode_cmd->pixel_format);
  145. while (cnt != MAX_FB_BUFFER) {
  146. if (!mode_cmd->handles[cnt])
  147. break;
  148. cnt++;
  149. }
  150. /*
  151. * check if NV12 or NV12M.
  152. *
  153. * NV12
  154. * handles[0] = base1, offsets[0] = 0
  155. * handles[1] = base1, offsets[1] = Y_size
  156. *
  157. * NV12M
  158. * handles[0] = base1, offsets[0] = 0
  159. * handles[1] = base2, offsets[1] = 0
  160. */
  161. if (cnt == 2) {
  162. /*
  163. * in case of NV12 format, offsets[1] is not 0 and
  164. * handles[0] is same as handles[1].
  165. */
  166. if (mode_cmd->offsets[1] &&
  167. mode_cmd->handles[0] == mode_cmd->handles[1])
  168. cnt = 1;
  169. }
  170. return cnt;
  171. }
  172. static struct drm_framebuffer *
  173. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  174. struct drm_mode_fb_cmd2 *mode_cmd)
  175. {
  176. struct drm_gem_object *obj;
  177. struct exynos_drm_fb *exynos_fb;
  178. int i, ret;
  179. DRM_DEBUG_KMS("%s\n", __FILE__);
  180. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  181. if (!obj) {
  182. DRM_ERROR("failed to lookup gem object\n");
  183. return ERR_PTR(-ENOENT);
  184. }
  185. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  186. if (!exynos_fb) {
  187. DRM_ERROR("failed to allocate exynos drm framebuffer\n");
  188. return ERR_PTR(-ENOMEM);
  189. }
  190. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  191. exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
  192. exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd);
  193. DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
  194. for (i = 1; i < exynos_fb->buf_cnt; i++) {
  195. struct exynos_drm_gem_obj *exynos_gem_obj;
  196. int ret;
  197. obj = drm_gem_object_lookup(dev, file_priv,
  198. mode_cmd->handles[i]);
  199. if (!obj) {
  200. DRM_ERROR("failed to lookup gem object\n");
  201. kfree(exynos_fb);
  202. return ERR_PTR(-ENOENT);
  203. }
  204. exynos_gem_obj = to_exynos_gem_obj(obj);
  205. ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
  206. if (ret < 0) {
  207. DRM_ERROR("cannot use this gem memory type for fb.\n");
  208. kfree(exynos_fb);
  209. return ERR_PTR(ret);
  210. }
  211. exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
  212. }
  213. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  214. if (ret) {
  215. for (i = 0; i < exynos_fb->buf_cnt; i++) {
  216. struct exynos_drm_gem_obj *gem_obj;
  217. gem_obj = exynos_fb->exynos_gem_obj[i];
  218. drm_gem_object_unreference_unlocked(&gem_obj->base);
  219. }
  220. kfree(exynos_fb);
  221. return ERR_PTR(ret);
  222. }
  223. return &exynos_fb->fb;
  224. }
  225. struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
  226. int index)
  227. {
  228. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  229. struct exynos_drm_gem_buf *buffer;
  230. DRM_DEBUG_KMS("%s\n", __FILE__);
  231. if (index >= MAX_FB_BUFFER)
  232. return NULL;
  233. buffer = exynos_fb->exynos_gem_obj[index]->buffer;
  234. if (!buffer)
  235. return NULL;
  236. DRM_DEBUG_KMS("dma_addr = 0x%lx\n", (unsigned long)buffer->dma_addr);
  237. return buffer;
  238. }
  239. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  240. {
  241. struct exynos_drm_private *private = dev->dev_private;
  242. struct drm_fb_helper *fb_helper = private->fb_helper;
  243. if (fb_helper)
  244. drm_fb_helper_hotplug_event(fb_helper);
  245. }
  246. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  247. .fb_create = exynos_user_fb_create,
  248. .output_poll_changed = exynos_drm_output_poll_changed,
  249. };
  250. void exynos_drm_mode_config_init(struct drm_device *dev)
  251. {
  252. dev->mode_config.min_width = 0;
  253. dev->mode_config.min_height = 0;
  254. /*
  255. * set max width and height as default value(4096x4096).
  256. * this value would be used to check framebuffer size limitation
  257. * at drm_mode_addfb().
  258. */
  259. dev->mode_config.max_width = 4096;
  260. dev->mode_config.max_height = 4096;
  261. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  262. }