rcar_du_vsp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * rcar_du_vsp.h -- R-Car Display Unit VSP-Based Compositor
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <linux/bitops.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/videodev2.h>
  25. #include <media/vsp1.h>
  26. #include "rcar_du_drv.h"
  27. #include "rcar_du_kms.h"
  28. #include "rcar_du_vsp.h"
  29. static void rcar_du_vsp_complete(void *private, bool completed)
  30. {
  31. struct rcar_du_crtc *crtc = private;
  32. if (crtc->vblank_enable)
  33. drm_crtc_handle_vblank(&crtc->crtc);
  34. if (completed)
  35. rcar_du_crtc_finish_page_flip(crtc);
  36. }
  37. void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
  38. {
  39. const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
  40. struct rcar_du_device *rcdu = crtc->group->dev;
  41. struct vsp1_du_lif_config cfg = {
  42. .width = mode->hdisplay,
  43. .height = mode->vdisplay,
  44. .callback = rcar_du_vsp_complete,
  45. .callback_data = crtc,
  46. };
  47. struct rcar_du_plane_state state = {
  48. .state = {
  49. .crtc = &crtc->crtc,
  50. .crtc_x = 0,
  51. .crtc_y = 0,
  52. .crtc_w = mode->hdisplay,
  53. .crtc_h = mode->vdisplay,
  54. .src_x = 0,
  55. .src_y = 0,
  56. .src_w = mode->hdisplay << 16,
  57. .src_h = mode->vdisplay << 16,
  58. .zpos = 0,
  59. },
  60. .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
  61. .source = RCAR_DU_PLANE_VSPD1,
  62. .alpha = 255,
  63. .colorkey = 0,
  64. };
  65. if (rcdu->info->gen >= 3)
  66. state.hwindex = (crtc->index % 2) ? 2 : 0;
  67. else
  68. state.hwindex = crtc->index % 2;
  69. __rcar_du_plane_setup(crtc->group, &state);
  70. /*
  71. * Ensure that the plane source configuration takes effect by requesting
  72. * a restart of the group. See rcar_du_plane_atomic_update() for a more
  73. * detailed explanation.
  74. *
  75. * TODO: Check whether this is still needed on Gen3.
  76. */
  77. crtc->group->need_restart = true;
  78. vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
  79. }
  80. void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
  81. {
  82. vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
  83. }
  84. void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
  85. {
  86. vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
  87. }
  88. void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
  89. {
  90. vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe);
  91. }
  92. /* Keep the two tables in sync. */
  93. static const u32 formats_kms[] = {
  94. DRM_FORMAT_RGB332,
  95. DRM_FORMAT_ARGB4444,
  96. DRM_FORMAT_XRGB4444,
  97. DRM_FORMAT_ARGB1555,
  98. DRM_FORMAT_XRGB1555,
  99. DRM_FORMAT_RGB565,
  100. DRM_FORMAT_BGR888,
  101. DRM_FORMAT_RGB888,
  102. DRM_FORMAT_BGRA8888,
  103. DRM_FORMAT_BGRX8888,
  104. DRM_FORMAT_ARGB8888,
  105. DRM_FORMAT_XRGB8888,
  106. DRM_FORMAT_UYVY,
  107. DRM_FORMAT_VYUY,
  108. DRM_FORMAT_YUYV,
  109. DRM_FORMAT_YVYU,
  110. DRM_FORMAT_NV12,
  111. DRM_FORMAT_NV21,
  112. DRM_FORMAT_NV16,
  113. DRM_FORMAT_NV61,
  114. DRM_FORMAT_YUV420,
  115. DRM_FORMAT_YVU420,
  116. DRM_FORMAT_YUV422,
  117. DRM_FORMAT_YVU422,
  118. DRM_FORMAT_YUV444,
  119. DRM_FORMAT_YVU444,
  120. };
  121. static const u32 formats_v4l2[] = {
  122. V4L2_PIX_FMT_RGB332,
  123. V4L2_PIX_FMT_ARGB444,
  124. V4L2_PIX_FMT_XRGB444,
  125. V4L2_PIX_FMT_ARGB555,
  126. V4L2_PIX_FMT_XRGB555,
  127. V4L2_PIX_FMT_RGB565,
  128. V4L2_PIX_FMT_RGB24,
  129. V4L2_PIX_FMT_BGR24,
  130. V4L2_PIX_FMT_ARGB32,
  131. V4L2_PIX_FMT_XRGB32,
  132. V4L2_PIX_FMT_ABGR32,
  133. V4L2_PIX_FMT_XBGR32,
  134. V4L2_PIX_FMT_UYVY,
  135. V4L2_PIX_FMT_VYUY,
  136. V4L2_PIX_FMT_YUYV,
  137. V4L2_PIX_FMT_YVYU,
  138. V4L2_PIX_FMT_NV12M,
  139. V4L2_PIX_FMT_NV21M,
  140. V4L2_PIX_FMT_NV16M,
  141. V4L2_PIX_FMT_NV61M,
  142. V4L2_PIX_FMT_YUV420M,
  143. V4L2_PIX_FMT_YVU420M,
  144. V4L2_PIX_FMT_YUV422M,
  145. V4L2_PIX_FMT_YVU422M,
  146. V4L2_PIX_FMT_YUV444M,
  147. V4L2_PIX_FMT_YVU444M,
  148. };
  149. static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
  150. {
  151. struct rcar_du_vsp_plane_state *state =
  152. to_rcar_vsp_plane_state(plane->plane.state);
  153. struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
  154. struct drm_framebuffer *fb = plane->plane.state->fb;
  155. struct vsp1_du_atomic_config cfg = {
  156. .pixelformat = 0,
  157. .pitch = fb->pitches[0],
  158. .alpha = state->alpha,
  159. .zpos = state->state.zpos,
  160. };
  161. unsigned int i;
  162. cfg.src.left = state->state.src_x >> 16;
  163. cfg.src.top = state->state.src_y >> 16;
  164. cfg.src.width = state->state.src_w >> 16;
  165. cfg.src.height = state->state.src_h >> 16;
  166. cfg.dst.left = state->state.crtc_x;
  167. cfg.dst.top = state->state.crtc_y;
  168. cfg.dst.width = state->state.crtc_w;
  169. cfg.dst.height = state->state.crtc_h;
  170. for (i = 0; i < state->format->planes; ++i)
  171. cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
  172. + fb->offsets[i];
  173. for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) {
  174. if (formats_kms[i] == state->format->fourcc) {
  175. cfg.pixelformat = formats_v4l2[i];
  176. break;
  177. }
  178. }
  179. vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
  180. plane->index, &cfg);
  181. }
  182. static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
  183. struct drm_plane_state *state)
  184. {
  185. struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
  186. struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
  187. struct rcar_du_device *rcdu = vsp->dev;
  188. unsigned int i;
  189. int ret;
  190. if (!state->fb)
  191. return 0;
  192. for (i = 0; i < rstate->format->planes; ++i) {
  193. struct drm_gem_cma_object *gem =
  194. drm_fb_cma_get_gem_obj(state->fb, i);
  195. struct sg_table *sgt = &rstate->sg_tables[i];
  196. ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr, gem->paddr,
  197. gem->base.size);
  198. if (ret)
  199. goto fail;
  200. ret = vsp1_du_map_sg(vsp->vsp, sgt);
  201. if (!ret) {
  202. sg_free_table(sgt);
  203. ret = -ENOMEM;
  204. goto fail;
  205. }
  206. }
  207. return 0;
  208. fail:
  209. while (i--) {
  210. struct sg_table *sgt = &rstate->sg_tables[i];
  211. vsp1_du_unmap_sg(vsp->vsp, sgt);
  212. sg_free_table(sgt);
  213. }
  214. return ret;
  215. }
  216. static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
  217. struct drm_plane_state *state)
  218. {
  219. struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
  220. struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
  221. unsigned int i;
  222. if (!state->fb)
  223. return;
  224. for (i = 0; i < rstate->format->planes; ++i) {
  225. struct sg_table *sgt = &rstate->sg_tables[i];
  226. vsp1_du_unmap_sg(vsp->vsp, sgt);
  227. sg_free_table(sgt);
  228. }
  229. }
  230. static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
  231. struct drm_plane_state *state)
  232. {
  233. struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
  234. struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
  235. struct rcar_du_device *rcdu = rplane->vsp->dev;
  236. if (!state->fb || !state->crtc) {
  237. rstate->format = NULL;
  238. return 0;
  239. }
  240. if (state->src_w >> 16 != state->crtc_w ||
  241. state->src_h >> 16 != state->crtc_h) {
  242. dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
  243. return -EINVAL;
  244. }
  245. rstate->format = rcar_du_format_info(state->fb->format->format);
  246. if (rstate->format == NULL) {
  247. dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
  248. state->fb->format->format);
  249. return -EINVAL;
  250. }
  251. return 0;
  252. }
  253. static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
  254. struct drm_plane_state *old_state)
  255. {
  256. struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
  257. struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
  258. if (plane->state->crtc)
  259. rcar_du_vsp_plane_setup(rplane);
  260. else
  261. vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
  262. rplane->index, NULL);
  263. }
  264. static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
  265. .prepare_fb = rcar_du_vsp_plane_prepare_fb,
  266. .cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
  267. .atomic_check = rcar_du_vsp_plane_atomic_check,
  268. .atomic_update = rcar_du_vsp_plane_atomic_update,
  269. };
  270. static struct drm_plane_state *
  271. rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
  272. {
  273. struct rcar_du_vsp_plane_state *state;
  274. struct rcar_du_vsp_plane_state *copy;
  275. if (WARN_ON(!plane->state))
  276. return NULL;
  277. state = to_rcar_vsp_plane_state(plane->state);
  278. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  279. if (copy == NULL)
  280. return NULL;
  281. __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
  282. return &copy->state;
  283. }
  284. static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
  285. struct drm_plane_state *state)
  286. {
  287. __drm_atomic_helper_plane_destroy_state(state);
  288. kfree(to_rcar_vsp_plane_state(state));
  289. }
  290. static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
  291. {
  292. struct rcar_du_vsp_plane_state *state;
  293. if (plane->state) {
  294. rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
  295. plane->state = NULL;
  296. }
  297. state = kzalloc(sizeof(*state), GFP_KERNEL);
  298. if (state == NULL)
  299. return;
  300. state->alpha = 255;
  301. state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
  302. plane->state = &state->state;
  303. plane->state->plane = plane;
  304. }
  305. static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane *plane,
  306. struct drm_plane_state *state, struct drm_property *property,
  307. uint64_t val)
  308. {
  309. struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
  310. struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
  311. if (property == rcdu->props.alpha)
  312. rstate->alpha = val;
  313. else
  314. return -EINVAL;
  315. return 0;
  316. }
  317. static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane *plane,
  318. const struct drm_plane_state *state, struct drm_property *property,
  319. uint64_t *val)
  320. {
  321. const struct rcar_du_vsp_plane_state *rstate =
  322. container_of(state, const struct rcar_du_vsp_plane_state, state);
  323. struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
  324. if (property == rcdu->props.alpha)
  325. *val = rstate->alpha;
  326. else
  327. return -EINVAL;
  328. return 0;
  329. }
  330. static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
  331. .update_plane = drm_atomic_helper_update_plane,
  332. .disable_plane = drm_atomic_helper_disable_plane,
  333. .reset = rcar_du_vsp_plane_reset,
  334. .destroy = drm_plane_cleanup,
  335. .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
  336. .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
  337. .atomic_set_property = rcar_du_vsp_plane_atomic_set_property,
  338. .atomic_get_property = rcar_du_vsp_plane_atomic_get_property,
  339. };
  340. int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
  341. unsigned int crtcs)
  342. {
  343. struct rcar_du_device *rcdu = vsp->dev;
  344. struct platform_device *pdev;
  345. unsigned int num_crtcs = hweight32(crtcs);
  346. unsigned int i;
  347. int ret;
  348. /* Find the VSP device and initialize it. */
  349. pdev = of_find_device_by_node(np);
  350. if (!pdev)
  351. return -ENXIO;
  352. vsp->vsp = &pdev->dev;
  353. ret = vsp1_du_init(vsp->vsp);
  354. if (ret < 0)
  355. return ret;
  356. /*
  357. * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
  358. * 4 RPFs.
  359. */
  360. vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4;
  361. vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes,
  362. sizeof(*vsp->planes), GFP_KERNEL);
  363. if (!vsp->planes)
  364. return -ENOMEM;
  365. for (i = 0; i < vsp->num_planes; ++i) {
  366. enum drm_plane_type type = i < num_crtcs
  367. ? DRM_PLANE_TYPE_PRIMARY
  368. : DRM_PLANE_TYPE_OVERLAY;
  369. struct rcar_du_vsp_plane *plane = &vsp->planes[i];
  370. plane->vsp = vsp;
  371. plane->index = i;
  372. ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
  373. &rcar_du_vsp_plane_funcs,
  374. formats_kms,
  375. ARRAY_SIZE(formats_kms),
  376. NULL, type, NULL);
  377. if (ret < 0)
  378. return ret;
  379. drm_plane_helper_add(&plane->plane,
  380. &rcar_du_vsp_plane_helper_funcs);
  381. if (type == DRM_PLANE_TYPE_PRIMARY)
  382. continue;
  383. drm_object_attach_property(&plane->plane.base,
  384. rcdu->props.alpha, 255);
  385. drm_plane_create_zpos_property(&plane->plane, 1, 1,
  386. vsp->num_planes - 1);
  387. }
  388. return 0;
  389. }