rockchip_drm_fb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <drm/drm.h>
  16. #include <drm/drmP.h>
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include "rockchip_drm_drv.h"
  21. #include "rockchip_drm_fb.h"
  22. #include "rockchip_drm_gem.h"
  23. #include "rockchip_drm_psr.h"
  24. #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
  25. struct rockchip_drm_fb {
  26. struct drm_framebuffer fb;
  27. struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
  28. };
  29. struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb,
  30. unsigned int plane)
  31. {
  32. struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
  33. if (plane >= ROCKCHIP_MAX_FB_BUFFER)
  34. return NULL;
  35. return rk_fb->obj[plane];
  36. }
  37. static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
  38. {
  39. struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
  40. int i;
  41. for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++)
  42. drm_gem_object_unreference_unlocked(rockchip_fb->obj[i]);
  43. drm_framebuffer_cleanup(fb);
  44. kfree(rockchip_fb);
  45. }
  46. static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
  47. struct drm_file *file_priv,
  48. unsigned int *handle)
  49. {
  50. struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
  51. return drm_gem_handle_create(file_priv,
  52. rockchip_fb->obj[0], handle);
  53. }
  54. static int rockchip_drm_fb_dirty(struct drm_framebuffer *fb,
  55. struct drm_file *file,
  56. unsigned int flags, unsigned int color,
  57. struct drm_clip_rect *clips,
  58. unsigned int num_clips)
  59. {
  60. rockchip_drm_psr_flush_all(fb->dev);
  61. return 0;
  62. }
  63. static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
  64. .destroy = rockchip_drm_fb_destroy,
  65. .create_handle = rockchip_drm_fb_create_handle,
  66. .dirty = rockchip_drm_fb_dirty,
  67. };
  68. static struct rockchip_drm_fb *
  69. rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
  70. struct drm_gem_object **obj, unsigned int num_planes)
  71. {
  72. struct rockchip_drm_fb *rockchip_fb;
  73. int ret;
  74. int i;
  75. rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
  76. if (!rockchip_fb)
  77. return ERR_PTR(-ENOMEM);
  78. drm_helper_mode_fill_fb_struct(dev, &rockchip_fb->fb, mode_cmd);
  79. for (i = 0; i < num_planes; i++)
  80. rockchip_fb->obj[i] = obj[i];
  81. ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
  82. &rockchip_drm_fb_funcs);
  83. if (ret) {
  84. dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
  85. ret);
  86. kfree(rockchip_fb);
  87. return ERR_PTR(ret);
  88. }
  89. return rockchip_fb;
  90. }
  91. static struct drm_framebuffer *
  92. rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  93. const struct drm_mode_fb_cmd2 *mode_cmd)
  94. {
  95. struct rockchip_drm_fb *rockchip_fb;
  96. struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
  97. struct drm_gem_object *obj;
  98. unsigned int hsub;
  99. unsigned int vsub;
  100. int num_planes;
  101. int ret;
  102. int i;
  103. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  104. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  105. num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
  106. ROCKCHIP_MAX_FB_BUFFER);
  107. for (i = 0; i < num_planes; i++) {
  108. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  109. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  110. unsigned int min_size;
  111. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
  112. if (!obj) {
  113. dev_err(dev->dev, "Failed to lookup GEM object\n");
  114. ret = -ENXIO;
  115. goto err_gem_object_unreference;
  116. }
  117. min_size = (height - 1) * mode_cmd->pitches[i] +
  118. mode_cmd->offsets[i] +
  119. width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
  120. if (obj->size < min_size) {
  121. drm_gem_object_unreference_unlocked(obj);
  122. ret = -EINVAL;
  123. goto err_gem_object_unreference;
  124. }
  125. objs[i] = obj;
  126. }
  127. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
  128. if (IS_ERR(rockchip_fb)) {
  129. ret = PTR_ERR(rockchip_fb);
  130. goto err_gem_object_unreference;
  131. }
  132. return &rockchip_fb->fb;
  133. err_gem_object_unreference:
  134. for (i--; i >= 0; i--)
  135. drm_gem_object_unreference_unlocked(objs[i]);
  136. return ERR_PTR(ret);
  137. }
  138. static void rockchip_drm_output_poll_changed(struct drm_device *dev)
  139. {
  140. struct rockchip_drm_private *private = dev->dev_private;
  141. struct drm_fb_helper *fb_helper = &private->fbdev_helper;
  142. if (fb_helper)
  143. drm_fb_helper_hotplug_event(fb_helper);
  144. }
  145. static void
  146. rockchip_atomic_commit_tail(struct drm_atomic_state *state)
  147. {
  148. struct drm_device *dev = state->dev;
  149. drm_atomic_helper_commit_modeset_disables(dev, state);
  150. drm_atomic_helper_commit_modeset_enables(dev, state);
  151. drm_atomic_helper_commit_planes(dev, state,
  152. DRM_PLANE_COMMIT_ACTIVE_ONLY);
  153. drm_atomic_helper_commit_hw_done(state);
  154. drm_atomic_helper_wait_for_vblanks(dev, state);
  155. drm_atomic_helper_cleanup_planes(dev, state);
  156. }
  157. static struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
  158. .atomic_commit_tail = rockchip_atomic_commit_tail,
  159. };
  160. static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
  161. .fb_create = rockchip_user_fb_create,
  162. .output_poll_changed = rockchip_drm_output_poll_changed,
  163. .atomic_check = drm_atomic_helper_check,
  164. .atomic_commit = drm_atomic_helper_commit,
  165. };
  166. struct drm_framebuffer *
  167. rockchip_drm_framebuffer_init(struct drm_device *dev,
  168. const struct drm_mode_fb_cmd2 *mode_cmd,
  169. struct drm_gem_object *obj)
  170. {
  171. struct rockchip_drm_fb *rockchip_fb;
  172. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
  173. if (IS_ERR(rockchip_fb))
  174. return NULL;
  175. return &rockchip_fb->fb;
  176. }
  177. void rockchip_drm_mode_config_init(struct drm_device *dev)
  178. {
  179. dev->mode_config.min_width = 0;
  180. dev->mode_config.min_height = 0;
  181. /*
  182. * set max width and height as default value(4096x4096).
  183. * this value would be used to check framebuffer size limitation
  184. * at drm_mode_addfb().
  185. */
  186. dev->mode_config.max_width = 4096;
  187. dev->mode_config.max_height = 4096;
  188. dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
  189. dev->mode_config.helper_private = &rockchip_mode_config_helpers;
  190. }