exynos_drm_fb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_buf.h"
  35. #include "exynos_drm_gem.h"
  36. #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
  37. /*
  38. * exynos specific framebuffer structure.
  39. *
  40. * @fb: drm framebuffer obejct.
  41. * @exynos_gem_obj: exynos specific gem object containing a gem object.
  42. * @buffer: pointer to exynos_drm_gem_buffer object.
  43. * - contain the memory information to memory region allocated
  44. * at default framebuffer creation.
  45. */
  46. struct exynos_drm_fb {
  47. struct drm_framebuffer fb;
  48. struct exynos_drm_gem_obj *exynos_gem_obj;
  49. struct exynos_drm_gem_buf *buffer;
  50. };
  51. static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
  52. {
  53. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  54. DRM_DEBUG_KMS("%s\n", __FILE__);
  55. drm_framebuffer_cleanup(fb);
  56. /*
  57. * default framebuffer has no gem object so
  58. * a buffer of the default framebuffer should be released at here.
  59. */
  60. if (!exynos_fb->exynos_gem_obj && exynos_fb->buffer)
  61. exynos_drm_buf_destroy(fb->dev, exynos_fb->buffer);
  62. kfree(exynos_fb);
  63. exynos_fb = NULL;
  64. }
  65. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  66. struct drm_file *file_priv,
  67. unsigned int *handle)
  68. {
  69. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  70. DRM_DEBUG_KMS("%s\n", __FILE__);
  71. return drm_gem_handle_create(file_priv,
  72. &exynos_fb->exynos_gem_obj->base, handle);
  73. }
  74. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  75. struct drm_file *file_priv, unsigned flags,
  76. unsigned color, struct drm_clip_rect *clips,
  77. unsigned num_clips)
  78. {
  79. DRM_DEBUG_KMS("%s\n", __FILE__);
  80. /* TODO */
  81. return 0;
  82. }
  83. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  84. .destroy = exynos_drm_fb_destroy,
  85. .create_handle = exynos_drm_fb_create_handle,
  86. .dirty = exynos_drm_fb_dirty,
  87. };
  88. static struct drm_framebuffer *
  89. exynos_drm_fb_init(struct drm_file *file_priv, struct drm_device *dev,
  90. struct drm_mode_fb_cmd2 *mode_cmd)
  91. {
  92. struct exynos_drm_fb *exynos_fb;
  93. struct drm_framebuffer *fb;
  94. struct exynos_drm_gem_obj *exynos_gem_obj = NULL;
  95. struct drm_gem_object *obj;
  96. unsigned int size;
  97. int ret;
  98. DRM_DEBUG_KMS("%s\n", __FILE__);
  99. DRM_LOG_KMS("drm fb create(%dx%d)\n",
  100. mode_cmd->width, mode_cmd->height);
  101. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  102. if (!exynos_fb) {
  103. DRM_ERROR("failed to allocate exynos drm framebuffer.\n");
  104. return ERR_PTR(-ENOMEM);
  105. }
  106. fb = &exynos_fb->fb;
  107. ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
  108. if (ret) {
  109. DRM_ERROR("failed to initialize framebuffer.\n");
  110. goto err_init;
  111. }
  112. DRM_LOG_KMS("create: fb id: %d\n", fb->base.id);
  113. size = mode_cmd->pitches[0] * mode_cmd->height;
  114. /*
  115. * mode_cmd->handles[0] could be NULL at booting time or
  116. * with user request. if NULL, a new buffer or a gem object
  117. * would be allocated.
  118. */
  119. if (!mode_cmd->handles[0]) {
  120. if (!file_priv) {
  121. struct exynos_drm_gem_buf *buffer;
  122. /*
  123. * in case that file_priv is NULL, it allocates
  124. * only buffer and this buffer would be used
  125. * for default framebuffer.
  126. */
  127. buffer = exynos_drm_buf_create(dev, size);
  128. if (IS_ERR(buffer)) {
  129. ret = PTR_ERR(buffer);
  130. goto err_buffer;
  131. }
  132. exynos_fb->buffer = buffer;
  133. DRM_LOG_KMS("default: dma_addr = 0x%lx, size = 0x%x\n",
  134. (unsigned long)buffer->dma_addr, size);
  135. goto out;
  136. } else {
  137. exynos_gem_obj = exynos_drm_gem_create(dev, file_priv,
  138. &mode_cmd->handles[0],
  139. size);
  140. if (IS_ERR(exynos_gem_obj)) {
  141. ret = PTR_ERR(exynos_gem_obj);
  142. goto err_buffer;
  143. }
  144. }
  145. } else {
  146. obj = drm_gem_object_lookup(dev, file_priv,
  147. mode_cmd->handles[0]);
  148. if (!obj) {
  149. DRM_ERROR("failed to lookup gem object.\n");
  150. goto err_buffer;
  151. }
  152. exynos_gem_obj = to_exynos_gem_obj(obj);
  153. drm_gem_object_unreference_unlocked(obj);
  154. }
  155. /*
  156. * if got a exynos_gem_obj from either a handle or
  157. * a new creation then exynos_fb->exynos_gem_obj is NULL
  158. * so that default framebuffer has no its own gem object,
  159. * only its own buffer object.
  160. */
  161. exynos_fb->buffer = exynos_gem_obj->buffer;
  162. DRM_LOG_KMS("dma_addr = 0x%lx, size = 0x%x, gem object = 0x%x\n",
  163. (unsigned long)exynos_fb->buffer->dma_addr, size,
  164. (unsigned int)&exynos_gem_obj->base);
  165. out:
  166. exynos_fb->exynos_gem_obj = exynos_gem_obj;
  167. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  168. return fb;
  169. err_buffer:
  170. drm_framebuffer_cleanup(fb);
  171. err_init:
  172. kfree(exynos_fb);
  173. return ERR_PTR(ret);
  174. }
  175. struct drm_framebuffer *exynos_drm_fb_create(struct drm_device *dev,
  176. struct drm_file *file_priv,
  177. struct drm_mode_fb_cmd2 *mode_cmd)
  178. {
  179. DRM_DEBUG_KMS("%s\n", __FILE__);
  180. return exynos_drm_fb_init(file_priv, dev, mode_cmd);
  181. }
  182. struct exynos_drm_gem_buf *exynos_drm_fb_get_buf(struct drm_framebuffer *fb)
  183. {
  184. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  185. struct exynos_drm_gem_buf *buffer;
  186. DRM_DEBUG_KMS("%s\n", __FILE__);
  187. buffer = exynos_fb->buffer;
  188. if (!buffer)
  189. return NULL;
  190. DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
  191. (unsigned long)buffer->kvaddr,
  192. (unsigned long)buffer->dma_addr);
  193. return buffer;
  194. }
  195. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  196. {
  197. struct exynos_drm_private *private = dev->dev_private;
  198. struct drm_fb_helper *fb_helper = private->fb_helper;
  199. if (fb_helper)
  200. drm_fb_helper_hotplug_event(fb_helper);
  201. }
  202. static struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  203. .fb_create = exynos_drm_fb_create,
  204. .output_poll_changed = exynos_drm_output_poll_changed,
  205. };
  206. void exynos_drm_mode_config_init(struct drm_device *dev)
  207. {
  208. dev->mode_config.min_width = 0;
  209. dev->mode_config.min_height = 0;
  210. /*
  211. * set max width and height as default value(4096x4096).
  212. * this value would be used to check framebuffer size limitation
  213. * at drm_mode_addfb().
  214. */
  215. dev->mode_config.max_width = 4096;
  216. dev->mode_config.max_height = 4096;
  217. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  218. }
  219. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  220. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  221. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  222. MODULE_DESCRIPTION("Samsung SoC DRM FB Driver");
  223. MODULE_LICENSE("GPL");