rockchip_drm_fb.c 9.0 KB

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