rockchip_drm_fb.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #define to_rockchip_fb(x) container_of(x, struct rockchip_drm_fb, fb)
  24. struct rockchip_drm_fb {
  25. struct drm_framebuffer fb;
  26. struct drm_gem_object *obj[ROCKCHIP_MAX_FB_BUFFER];
  27. };
  28. struct drm_gem_object *rockchip_fb_get_gem_obj(struct drm_framebuffer *fb,
  29. unsigned int plane)
  30. {
  31. struct rockchip_drm_fb *rk_fb = to_rockchip_fb(fb);
  32. if (plane >= ROCKCHIP_MAX_FB_BUFFER)
  33. return NULL;
  34. return rk_fb->obj[plane];
  35. }
  36. static void rockchip_drm_fb_destroy(struct drm_framebuffer *fb)
  37. {
  38. struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
  39. int i;
  40. for (i = 0; i < ROCKCHIP_MAX_FB_BUFFER; i++)
  41. drm_gem_object_unreference_unlocked(rockchip_fb->obj[i]);
  42. drm_framebuffer_cleanup(fb);
  43. kfree(rockchip_fb);
  44. }
  45. static int rockchip_drm_fb_create_handle(struct drm_framebuffer *fb,
  46. struct drm_file *file_priv,
  47. unsigned int *handle)
  48. {
  49. struct rockchip_drm_fb *rockchip_fb = to_rockchip_fb(fb);
  50. return drm_gem_handle_create(file_priv,
  51. rockchip_fb->obj[0], handle);
  52. }
  53. static const struct drm_framebuffer_funcs rockchip_drm_fb_funcs = {
  54. .destroy = rockchip_drm_fb_destroy,
  55. .create_handle = rockchip_drm_fb_create_handle,
  56. };
  57. static struct rockchip_drm_fb *
  58. rockchip_fb_alloc(struct drm_device *dev, const struct drm_mode_fb_cmd2 *mode_cmd,
  59. struct drm_gem_object **obj, unsigned int num_planes)
  60. {
  61. struct rockchip_drm_fb *rockchip_fb;
  62. int ret;
  63. int i;
  64. rockchip_fb = kzalloc(sizeof(*rockchip_fb), GFP_KERNEL);
  65. if (!rockchip_fb)
  66. return ERR_PTR(-ENOMEM);
  67. drm_helper_mode_fill_fb_struct(&rockchip_fb->fb, mode_cmd);
  68. for (i = 0; i < num_planes; i++)
  69. rockchip_fb->obj[i] = obj[i];
  70. ret = drm_framebuffer_init(dev, &rockchip_fb->fb,
  71. &rockchip_drm_fb_funcs);
  72. if (ret) {
  73. dev_err(dev->dev, "Failed to initialize framebuffer: %d\n",
  74. ret);
  75. kfree(rockchip_fb);
  76. return ERR_PTR(ret);
  77. }
  78. return rockchip_fb;
  79. }
  80. static struct drm_framebuffer *
  81. rockchip_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  82. const struct drm_mode_fb_cmd2 *mode_cmd)
  83. {
  84. struct rockchip_drm_fb *rockchip_fb;
  85. struct drm_gem_object *objs[ROCKCHIP_MAX_FB_BUFFER];
  86. struct drm_gem_object *obj;
  87. unsigned int hsub;
  88. unsigned int vsub;
  89. int num_planes;
  90. int ret;
  91. int i;
  92. hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format);
  93. vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format);
  94. num_planes = min(drm_format_num_planes(mode_cmd->pixel_format),
  95. ROCKCHIP_MAX_FB_BUFFER);
  96. for (i = 0; i < num_planes; i++) {
  97. unsigned int width = mode_cmd->width / (i ? hsub : 1);
  98. unsigned int height = mode_cmd->height / (i ? vsub : 1);
  99. unsigned int min_size;
  100. obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]);
  101. if (!obj) {
  102. dev_err(dev->dev, "Failed to lookup GEM object\n");
  103. ret = -ENXIO;
  104. goto err_gem_object_unreference;
  105. }
  106. min_size = (height - 1) * mode_cmd->pitches[i] +
  107. mode_cmd->offsets[i] +
  108. width * drm_format_plane_cpp(mode_cmd->pixel_format, i);
  109. if (obj->size < min_size) {
  110. drm_gem_object_unreference_unlocked(obj);
  111. ret = -EINVAL;
  112. goto err_gem_object_unreference;
  113. }
  114. objs[i] = obj;
  115. }
  116. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, objs, i);
  117. if (IS_ERR(rockchip_fb)) {
  118. ret = PTR_ERR(rockchip_fb);
  119. goto err_gem_object_unreference;
  120. }
  121. return &rockchip_fb->fb;
  122. err_gem_object_unreference:
  123. for (i--; i >= 0; i--)
  124. drm_gem_object_unreference_unlocked(objs[i]);
  125. return ERR_PTR(ret);
  126. }
  127. static void rockchip_drm_output_poll_changed(struct drm_device *dev)
  128. {
  129. struct rockchip_drm_private *private = dev->dev_private;
  130. struct drm_fb_helper *fb_helper = &private->fbdev_helper;
  131. if (fb_helper)
  132. drm_fb_helper_hotplug_event(fb_helper);
  133. }
  134. static void rockchip_crtc_wait_for_update(struct drm_crtc *crtc)
  135. {
  136. struct rockchip_drm_private *priv = crtc->dev->dev_private;
  137. int pipe = drm_crtc_index(crtc);
  138. const struct rockchip_crtc_funcs *crtc_funcs = priv->crtc_funcs[pipe];
  139. if (crtc_funcs && crtc_funcs->wait_for_update)
  140. crtc_funcs->wait_for_update(crtc);
  141. }
  142. /*
  143. * We can't use drm_atomic_helper_wait_for_vblanks() because rk3288 and rk3066
  144. * have hardware counters for neither vblanks nor scanlines, which results in
  145. * a race where:
  146. * | <-- HW vsync irq and reg take effect
  147. * plane_commit --> |
  148. * get_vblank and wait --> |
  149. * | <-- handle_vblank, vblank->count + 1
  150. * cleanup_fb --> |
  151. * iommu crash --> |
  152. * | <-- HW vsync irq and reg take effect
  153. *
  154. * This function is equivalent but uses rockchip_crtc_wait_for_update() instead
  155. * of waiting for vblank_count to change.
  156. */
  157. static void
  158. rockchip_atomic_wait_for_complete(struct drm_device *dev, struct drm_atomic_state *old_state)
  159. {
  160. struct drm_crtc_state *old_crtc_state;
  161. struct drm_crtc *crtc;
  162. int i, ret;
  163. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  164. /* No one cares about the old state, so abuse it for tracking
  165. * and store whether we hold a vblank reference (and should do a
  166. * vblank wait) in the ->enable boolean.
  167. */
  168. old_crtc_state->enable = false;
  169. if (!crtc->state->active)
  170. continue;
  171. if (!drm_atomic_helper_framebuffer_changed(dev,
  172. old_state, crtc))
  173. continue;
  174. ret = drm_crtc_vblank_get(crtc);
  175. if (ret != 0)
  176. continue;
  177. old_crtc_state->enable = true;
  178. }
  179. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  180. if (!old_crtc_state->enable)
  181. continue;
  182. rockchip_crtc_wait_for_update(crtc);
  183. drm_crtc_vblank_put(crtc);
  184. }
  185. }
  186. static void
  187. rockchip_atomic_commit_tail(struct drm_atomic_state *state)
  188. {
  189. struct drm_device *dev = state->dev;
  190. drm_atomic_helper_commit_modeset_disables(dev, state);
  191. drm_atomic_helper_commit_modeset_enables(dev, state);
  192. drm_atomic_helper_commit_planes(dev, state, true);
  193. drm_atomic_helper_commit_hw_done(state);
  194. rockchip_atomic_wait_for_complete(dev, state);
  195. drm_atomic_helper_cleanup_planes(dev, state);
  196. }
  197. static struct drm_mode_config_helper_funcs rockchip_mode_config_helpers = {
  198. .atomic_commit_tail = rockchip_atomic_commit_tail,
  199. };
  200. static const struct drm_mode_config_funcs rockchip_drm_mode_config_funcs = {
  201. .fb_create = rockchip_user_fb_create,
  202. .output_poll_changed = rockchip_drm_output_poll_changed,
  203. .atomic_check = drm_atomic_helper_check,
  204. .atomic_commit = drm_atomic_helper_commit,
  205. };
  206. struct drm_framebuffer *
  207. rockchip_drm_framebuffer_init(struct drm_device *dev,
  208. const struct drm_mode_fb_cmd2 *mode_cmd,
  209. struct drm_gem_object *obj)
  210. {
  211. struct rockchip_drm_fb *rockchip_fb;
  212. rockchip_fb = rockchip_fb_alloc(dev, mode_cmd, &obj, 1);
  213. if (IS_ERR(rockchip_fb))
  214. return NULL;
  215. return &rockchip_fb->fb;
  216. }
  217. void rockchip_drm_mode_config_init(struct drm_device *dev)
  218. {
  219. dev->mode_config.min_width = 0;
  220. dev->mode_config.min_height = 0;
  221. /*
  222. * set max width and height as default value(4096x4096).
  223. * this value would be used to check framebuffer size limitation
  224. * at drm_mode_addfb().
  225. */
  226. dev->mode_config.max_width = 4096;
  227. dev->mode_config.max_height = 4096;
  228. dev->mode_config.funcs = &rockchip_drm_mode_config_funcs;
  229. dev->mode_config.helper_private = &rockchip_mode_config_helpers;
  230. }