exynos_drm_fb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/drm_crtc.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_fb_helper.h>
  32. #include "exynos_drm_drv.h"
  33. #include "exynos_drm_fb.h"
  34. #include "exynos_drm_gem.h"
  35. #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
  36. /*
  37. * exynos specific framebuffer structure.
  38. *
  39. * @fb: drm framebuffer obejct.
  40. * @buf_cnt: a buffer count to drm framebuffer.
  41. * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
  42. */
  43. struct exynos_drm_fb {
  44. struct drm_framebuffer fb;
  45. unsigned int buf_cnt;
  46. struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
  47. };
  48. static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
  49. {
  50. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  51. unsigned int i;
  52. DRM_DEBUG_KMS("%s\n", __FILE__);
  53. drm_framebuffer_cleanup(fb);
  54. for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
  55. struct drm_gem_object *obj;
  56. if (exynos_fb->exynos_gem_obj[i] == NULL)
  57. continue;
  58. obj = &exynos_fb->exynos_gem_obj[i]->base;
  59. drm_gem_object_unreference_unlocked(obj);
  60. }
  61. kfree(exynos_fb);
  62. exynos_fb = NULL;
  63. }
  64. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  65. struct drm_file *file_priv,
  66. unsigned int *handle)
  67. {
  68. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  69. DRM_DEBUG_KMS("%s\n", __FILE__);
  70. return drm_gem_handle_create(file_priv,
  71. &exynos_fb->exynos_gem_obj[0]->base, handle);
  72. }
  73. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  74. struct drm_file *file_priv, unsigned flags,
  75. unsigned color, struct drm_clip_rect *clips,
  76. unsigned num_clips)
  77. {
  78. DRM_DEBUG_KMS("%s\n", __FILE__);
  79. /* TODO */
  80. return 0;
  81. }
  82. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  83. .destroy = exynos_drm_fb_destroy,
  84. .create_handle = exynos_drm_fb_create_handle,
  85. .dirty = exynos_drm_fb_dirty,
  86. };
  87. void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
  88. unsigned int cnt)
  89. {
  90. struct exynos_drm_fb *exynos_fb;
  91. exynos_fb = to_exynos_fb(fb);
  92. exynos_fb->buf_cnt = cnt;
  93. }
  94. unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
  95. {
  96. struct exynos_drm_fb *exynos_fb;
  97. exynos_fb = to_exynos_fb(fb);
  98. return exynos_fb->buf_cnt;
  99. }
  100. struct drm_framebuffer *
  101. exynos_drm_framebuffer_init(struct drm_device *dev,
  102. struct drm_mode_fb_cmd2 *mode_cmd,
  103. struct drm_gem_object *obj)
  104. {
  105. struct exynos_drm_fb *exynos_fb;
  106. int ret;
  107. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  108. if (!exynos_fb) {
  109. DRM_ERROR("failed to allocate exynos drm framebuffer\n");
  110. return ERR_PTR(-ENOMEM);
  111. }
  112. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  113. if (ret) {
  114. DRM_ERROR("failed to initialize framebuffer\n");
  115. return ERR_PTR(ret);
  116. }
  117. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  118. exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
  119. return &exynos_fb->fb;
  120. }
  121. static u32 exynos_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd)
  122. {
  123. unsigned int cnt = 0;
  124. if (mode_cmd->pixel_format != DRM_FORMAT_NV12)
  125. return drm_format_num_planes(mode_cmd->pixel_format);
  126. while (cnt != MAX_FB_BUFFER) {
  127. if (!mode_cmd->handles[cnt])
  128. break;
  129. cnt++;
  130. }
  131. /*
  132. * check if NV12 or NV12M.
  133. *
  134. * NV12
  135. * handles[0] = base1, offsets[0] = 0
  136. * handles[1] = base1, offsets[1] = Y_size
  137. *
  138. * NV12M
  139. * handles[0] = base1, offsets[0] = 0
  140. * handles[1] = base2, offsets[1] = 0
  141. */
  142. if (cnt == 2) {
  143. /*
  144. * in case of NV12 format, offsets[1] is not 0 and
  145. * handles[0] is same as handles[1].
  146. */
  147. if (mode_cmd->offsets[1] &&
  148. mode_cmd->handles[0] == mode_cmd->handles[1])
  149. cnt = 1;
  150. }
  151. return cnt;
  152. }
  153. static struct drm_framebuffer *
  154. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  155. struct drm_mode_fb_cmd2 *mode_cmd)
  156. {
  157. struct drm_gem_object *obj;
  158. struct drm_framebuffer *fb;
  159. struct exynos_drm_fb *exynos_fb;
  160. int i;
  161. DRM_DEBUG_KMS("%s\n", __FILE__);
  162. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  163. if (!obj) {
  164. DRM_ERROR("failed to lookup gem object\n");
  165. return ERR_PTR(-ENOENT);
  166. }
  167. fb = exynos_drm_framebuffer_init(dev, mode_cmd, obj);
  168. if (IS_ERR(fb)) {
  169. drm_gem_object_unreference_unlocked(obj);
  170. return fb;
  171. }
  172. exynos_fb = to_exynos_fb(fb);
  173. exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd);
  174. DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
  175. for (i = 1; i < exynos_fb->buf_cnt; i++) {
  176. obj = drm_gem_object_lookup(dev, file_priv,
  177. mode_cmd->handles[i]);
  178. if (!obj) {
  179. DRM_ERROR("failed to lookup gem object\n");
  180. exynos_drm_fb_destroy(fb);
  181. return ERR_PTR(-ENOENT);
  182. }
  183. exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
  184. }
  185. return fb;
  186. }
  187. struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
  188. int index)
  189. {
  190. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  191. struct exynos_drm_gem_buf *buffer;
  192. DRM_DEBUG_KMS("%s\n", __FILE__);
  193. if (index >= MAX_FB_BUFFER)
  194. return NULL;
  195. buffer = exynos_fb->exynos_gem_obj[index]->buffer;
  196. if (!buffer)
  197. return NULL;
  198. DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
  199. (unsigned long)buffer->kvaddr,
  200. (unsigned long)buffer->dma_addr);
  201. return buffer;
  202. }
  203. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  204. {
  205. struct exynos_drm_private *private = dev->dev_private;
  206. struct drm_fb_helper *fb_helper = private->fb_helper;
  207. if (fb_helper)
  208. drm_fb_helper_hotplug_event(fb_helper);
  209. }
  210. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  211. .fb_create = exynos_user_fb_create,
  212. .output_poll_changed = exynos_drm_output_poll_changed,
  213. };
  214. void exynos_drm_mode_config_init(struct drm_device *dev)
  215. {
  216. dev->mode_config.min_width = 0;
  217. dev->mode_config.min_height = 0;
  218. /*
  219. * set max width and height as default value(4096x4096).
  220. * this value would be used to check framebuffer size limitation
  221. * at drm_mode_addfb().
  222. */
  223. dev->mode_config.max_width = 4096;
  224. dev->mode_config.max_height = 4096;
  225. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  226. }