exynos_drm_fb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_cmd *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. mode_cmd->pitch = max(mode_cmd->pitch,
  100. mode_cmd->width * (mode_cmd->bpp >> 3));
  101. DRM_LOG_KMS("drm fb create(%dx%d)\n",
  102. mode_cmd->width, mode_cmd->height);
  103. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  104. if (!exynos_fb) {
  105. DRM_ERROR("failed to allocate exynos drm framebuffer.\n");
  106. return ERR_PTR(-ENOMEM);
  107. }
  108. fb = &exynos_fb->fb;
  109. ret = drm_framebuffer_init(dev, fb, &exynos_drm_fb_funcs);
  110. if (ret) {
  111. DRM_ERROR("failed to initialize framebuffer.\n");
  112. goto err_init;
  113. }
  114. DRM_LOG_KMS("create: fb id: %d\n", fb->base.id);
  115. size = mode_cmd->pitch * mode_cmd->height;
  116. /*
  117. * mode_cmd->handle could be NULL at booting time or
  118. * with user request. if NULL, a new buffer or a gem object
  119. * would be allocated.
  120. */
  121. if (!mode_cmd->handle) {
  122. if (!file_priv) {
  123. struct exynos_drm_gem_buf *buffer;
  124. /*
  125. * in case that file_priv is NULL, it allocates
  126. * only buffer and this buffer would be used
  127. * for default framebuffer.
  128. */
  129. buffer = exynos_drm_buf_create(dev, size);
  130. if (IS_ERR(buffer)) {
  131. ret = PTR_ERR(buffer);
  132. goto err_buffer;
  133. }
  134. exynos_fb->buffer = buffer;
  135. DRM_LOG_KMS("default: dma_addr = 0x%lx, size = 0x%x\n",
  136. (unsigned long)buffer->dma_addr, size);
  137. goto out;
  138. } else {
  139. exynos_gem_obj = exynos_drm_gem_create(dev, file_priv,
  140. &mode_cmd->handle,
  141. size);
  142. if (IS_ERR(exynos_gem_obj)) {
  143. ret = PTR_ERR(exynos_gem_obj);
  144. goto err_buffer;
  145. }
  146. }
  147. } else {
  148. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  149. if (!obj) {
  150. DRM_ERROR("failed to lookup gem object.\n");
  151. goto err_buffer;
  152. }
  153. exynos_gem_obj = to_exynos_gem_obj(obj);
  154. drm_gem_object_unreference_unlocked(obj);
  155. }
  156. /*
  157. * if got a exynos_gem_obj from either a handle or
  158. * a new creation then exynos_fb->exynos_gem_obj is NULL
  159. * so that default framebuffer has no its own gem object,
  160. * only its own buffer object.
  161. */
  162. exynos_fb->buffer = exynos_gem_obj->buffer;
  163. DRM_LOG_KMS("dma_addr = 0x%lx, size = 0x%x, gem object = 0x%x\n",
  164. (unsigned long)exynos_fb->buffer->dma_addr, size,
  165. (unsigned int)&exynos_gem_obj->base);
  166. out:
  167. exynos_fb->exynos_gem_obj = exynos_gem_obj;
  168. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  169. return fb;
  170. err_buffer:
  171. drm_framebuffer_cleanup(fb);
  172. err_init:
  173. kfree(exynos_fb);
  174. return ERR_PTR(ret);
  175. }
  176. struct drm_framebuffer *exynos_drm_fb_create(struct drm_device *dev,
  177. struct drm_file *file_priv,
  178. struct drm_mode_fb_cmd *mode_cmd)
  179. {
  180. DRM_DEBUG_KMS("%s\n", __FILE__);
  181. return exynos_drm_fb_init(file_priv, dev, mode_cmd);
  182. }
  183. struct exynos_drm_gem_buf *exynos_drm_fb_get_buf(struct drm_framebuffer *fb)
  184. {
  185. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  186. struct exynos_drm_gem_buf *buffer;
  187. DRM_DEBUG_KMS("%s\n", __FILE__);
  188. buffer = exynos_fb->buffer;
  189. if (!buffer)
  190. return NULL;
  191. DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
  192. (unsigned long)buffer->kvaddr,
  193. (unsigned long)buffer->dma_addr);
  194. return buffer;
  195. }
  196. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  197. {
  198. struct exynos_drm_private *private = dev->dev_private;
  199. struct drm_fb_helper *fb_helper = private->fb_helper;
  200. if (fb_helper)
  201. drm_fb_helper_hotplug_event(fb_helper);
  202. }
  203. static struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  204. .fb_create = exynos_drm_fb_create,
  205. .output_poll_changed = exynos_drm_output_poll_changed,
  206. };
  207. void exynos_drm_mode_config_init(struct drm_device *dev)
  208. {
  209. dev->mode_config.min_width = 0;
  210. dev->mode_config.min_height = 0;
  211. /*
  212. * set max width and height as default value(4096x4096).
  213. * this value would be used to check framebuffer size limitation
  214. * at drm_mode_addfb().
  215. */
  216. dev->mode_config.max_width = 4096;
  217. dev->mode_config.max_height = 4096;
  218. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  219. }
  220. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  221. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  222. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  223. MODULE_DESCRIPTION("Samsung SoC DRM FB Driver");
  224. MODULE_LICENSE("GPL");