exynos_drm_fb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "drmP.h"
  29. #include "drm_crtc.h"
  30. #include "drm_crtc_helper.h"
  31. #include "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. * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
  41. */
  42. struct exynos_drm_fb {
  43. struct drm_framebuffer fb;
  44. struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
  45. };
  46. static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
  47. {
  48. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  49. unsigned int i;
  50. DRM_DEBUG_KMS("%s\n", __FILE__);
  51. drm_framebuffer_cleanup(fb);
  52. for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
  53. struct drm_gem_object *obj;
  54. if (exynos_fb->exynos_gem_obj[i] == NULL)
  55. continue;
  56. obj = &exynos_fb->exynos_gem_obj[i]->base;
  57. drm_gem_object_unreference_unlocked(obj);
  58. }
  59. kfree(exynos_fb);
  60. exynos_fb = NULL;
  61. }
  62. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  63. struct drm_file *file_priv,
  64. unsigned int *handle)
  65. {
  66. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  67. DRM_DEBUG_KMS("%s\n", __FILE__);
  68. return drm_gem_handle_create(file_priv,
  69. &exynos_fb->exynos_gem_obj[0]->base, handle);
  70. }
  71. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  72. struct drm_file *file_priv, unsigned flags,
  73. unsigned color, struct drm_clip_rect *clips,
  74. unsigned num_clips)
  75. {
  76. DRM_DEBUG_KMS("%s\n", __FILE__);
  77. /* TODO */
  78. return 0;
  79. }
  80. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  81. .destroy = exynos_drm_fb_destroy,
  82. .create_handle = exynos_drm_fb_create_handle,
  83. .dirty = exynos_drm_fb_dirty,
  84. };
  85. struct drm_framebuffer *
  86. exynos_drm_framebuffer_init(struct drm_device *dev,
  87. struct drm_mode_fb_cmd2 *mode_cmd,
  88. struct drm_gem_object *obj)
  89. {
  90. struct exynos_drm_fb *exynos_fb;
  91. int ret;
  92. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  93. if (!exynos_fb) {
  94. DRM_ERROR("failed to allocate exynos drm framebuffer\n");
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  98. if (ret) {
  99. DRM_ERROR("failed to initialize framebuffer\n");
  100. return ERR_PTR(ret);
  101. }
  102. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  103. exynos_fb->exynos_gem_obj[0] = to_exynos_gem_obj(obj);
  104. return &exynos_fb->fb;
  105. }
  106. static struct drm_framebuffer *
  107. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  108. struct drm_mode_fb_cmd2 *mode_cmd)
  109. {
  110. struct drm_gem_object *obj;
  111. struct drm_framebuffer *fb;
  112. struct exynos_drm_fb *exynos_fb;
  113. int nr;
  114. int i;
  115. DRM_DEBUG_KMS("%s\n", __FILE__);
  116. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  117. if (!obj) {
  118. DRM_ERROR("failed to lookup gem object\n");
  119. return ERR_PTR(-ENOENT);
  120. }
  121. fb = exynos_drm_framebuffer_init(dev, mode_cmd, obj);
  122. if (IS_ERR(fb)) {
  123. drm_gem_object_unreference_unlocked(obj);
  124. return fb;
  125. }
  126. exynos_fb = to_exynos_fb(fb);
  127. nr = exynos_drm_format_num_buffers(fb->pixel_format);
  128. for (i = 1; i < nr; i++) {
  129. obj = drm_gem_object_lookup(dev, file_priv,
  130. mode_cmd->handles[i]);
  131. if (!obj) {
  132. DRM_ERROR("failed to lookup gem object\n");
  133. exynos_drm_fb_destroy(fb);
  134. return ERR_PTR(-ENOENT);
  135. }
  136. exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
  137. }
  138. return fb;
  139. }
  140. struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
  141. int index)
  142. {
  143. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  144. struct exynos_drm_gem_buf *buffer;
  145. DRM_DEBUG_KMS("%s\n", __FILE__);
  146. if (index >= MAX_FB_BUFFER)
  147. return NULL;
  148. buffer = exynos_fb->exynos_gem_obj[index]->buffer;
  149. if (!buffer)
  150. return NULL;
  151. DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
  152. (unsigned long)buffer->kvaddr,
  153. (unsigned long)buffer->dma_addr);
  154. return buffer;
  155. }
  156. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  157. {
  158. struct exynos_drm_private *private = dev->dev_private;
  159. struct drm_fb_helper *fb_helper = private->fb_helper;
  160. if (fb_helper)
  161. drm_fb_helper_hotplug_event(fb_helper);
  162. }
  163. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  164. .fb_create = exynos_user_fb_create,
  165. .output_poll_changed = exynos_drm_output_poll_changed,
  166. };
  167. void exynos_drm_mode_config_init(struct drm_device *dev)
  168. {
  169. dev->mode_config.min_width = 0;
  170. dev->mode_config.min_height = 0;
  171. /*
  172. * set max width and height as default value(4096x4096).
  173. * this value would be used to check framebuffer size limitation
  174. * at drm_mode_addfb().
  175. */
  176. dev->mode_config.max_width = 4096;
  177. dev->mode_config.max_height = 4096;
  178. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  179. }