vsp1_pipe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_pipe.c -- R-Car VSP1 Pipeline
  4. *
  5. * Copyright (C) 2013-2015 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/list.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <media/media-entity.h>
  14. #include <media/v4l2-subdev.h>
  15. #include "vsp1.h"
  16. #include "vsp1_brx.h"
  17. #include "vsp1_dl.h"
  18. #include "vsp1_entity.h"
  19. #include "vsp1_hgo.h"
  20. #include "vsp1_hgt.h"
  21. #include "vsp1_pipe.h"
  22. #include "vsp1_rwpf.h"
  23. #include "vsp1_uds.h"
  24. /* -----------------------------------------------------------------------------
  25. * Helper Functions
  26. */
  27. static const struct vsp1_format_info vsp1_video_formats[] = {
  28. { V4L2_PIX_FMT_RGB332, MEDIA_BUS_FMT_ARGB8888_1X32,
  29. VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  30. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  31. 1, { 8, 0, 0 }, false, false, 1, 1, false },
  32. { V4L2_PIX_FMT_ARGB444, MEDIA_BUS_FMT_ARGB8888_1X32,
  33. VI6_FMT_ARGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  34. VI6_RPF_DSWAP_P_WDS,
  35. 1, { 16, 0, 0 }, false, false, 1, 1, true },
  36. { V4L2_PIX_FMT_XRGB444, MEDIA_BUS_FMT_ARGB8888_1X32,
  37. VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  38. VI6_RPF_DSWAP_P_WDS,
  39. 1, { 16, 0, 0 }, false, false, 1, 1, false },
  40. { V4L2_PIX_FMT_ARGB555, MEDIA_BUS_FMT_ARGB8888_1X32,
  41. VI6_FMT_ARGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  42. VI6_RPF_DSWAP_P_WDS,
  43. 1, { 16, 0, 0 }, false, false, 1, 1, true },
  44. { V4L2_PIX_FMT_XRGB555, MEDIA_BUS_FMT_ARGB8888_1X32,
  45. VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  46. VI6_RPF_DSWAP_P_WDS,
  47. 1, { 16, 0, 0 }, false, false, 1, 1, false },
  48. { V4L2_PIX_FMT_RGB565, MEDIA_BUS_FMT_ARGB8888_1X32,
  49. VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  50. VI6_RPF_DSWAP_P_WDS,
  51. 1, { 16, 0, 0 }, false, false, 1, 1, false },
  52. { V4L2_PIX_FMT_BGR24, MEDIA_BUS_FMT_ARGB8888_1X32,
  53. VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  54. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  55. 1, { 24, 0, 0 }, false, false, 1, 1, false },
  56. { V4L2_PIX_FMT_RGB24, MEDIA_BUS_FMT_ARGB8888_1X32,
  57. VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  58. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  59. 1, { 24, 0, 0 }, false, false, 1, 1, false },
  60. { V4L2_PIX_FMT_ABGR32, MEDIA_BUS_FMT_ARGB8888_1X32,
  61. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
  62. 1, { 32, 0, 0 }, false, false, 1, 1, true },
  63. { V4L2_PIX_FMT_XBGR32, MEDIA_BUS_FMT_ARGB8888_1X32,
  64. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
  65. 1, { 32, 0, 0 }, false, false, 1, 1, false },
  66. { V4L2_PIX_FMT_ARGB32, MEDIA_BUS_FMT_ARGB8888_1X32,
  67. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  68. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  69. 1, { 32, 0, 0 }, false, false, 1, 1, true },
  70. { V4L2_PIX_FMT_XRGB32, MEDIA_BUS_FMT_ARGB8888_1X32,
  71. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  72. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  73. 1, { 32, 0, 0 }, false, false, 1, 1, false },
  74. { V4L2_PIX_FMT_HSV24, MEDIA_BUS_FMT_AHSV8888_1X32,
  75. VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  76. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  77. 1, { 24, 0, 0 }, false, false, 1, 1, false },
  78. { V4L2_PIX_FMT_HSV32, MEDIA_BUS_FMT_AHSV8888_1X32,
  79. VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  80. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  81. 1, { 32, 0, 0 }, false, false, 1, 1, false },
  82. { V4L2_PIX_FMT_UYVY, MEDIA_BUS_FMT_AYUV8_1X32,
  83. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  84. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  85. 1, { 16, 0, 0 }, false, false, 2, 1, false },
  86. { V4L2_PIX_FMT_VYUY, MEDIA_BUS_FMT_AYUV8_1X32,
  87. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  88. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  89. 1, { 16, 0, 0 }, false, true, 2, 1, false },
  90. { V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_AYUV8_1X32,
  91. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  92. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  93. 1, { 16, 0, 0 }, true, false, 2, 1, false },
  94. { V4L2_PIX_FMT_YVYU, MEDIA_BUS_FMT_AYUV8_1X32,
  95. VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  96. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  97. 1, { 16, 0, 0 }, true, true, 2, 1, false },
  98. { V4L2_PIX_FMT_NV12M, MEDIA_BUS_FMT_AYUV8_1X32,
  99. VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  100. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  101. 2, { 8, 16, 0 }, false, false, 2, 2, false },
  102. { V4L2_PIX_FMT_NV21M, MEDIA_BUS_FMT_AYUV8_1X32,
  103. VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  104. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  105. 2, { 8, 16, 0 }, false, true, 2, 2, false },
  106. { V4L2_PIX_FMT_NV16M, MEDIA_BUS_FMT_AYUV8_1X32,
  107. VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  108. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  109. 2, { 8, 16, 0 }, false, false, 2, 1, false },
  110. { V4L2_PIX_FMT_NV61M, MEDIA_BUS_FMT_AYUV8_1X32,
  111. VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  112. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  113. 2, { 8, 16, 0 }, false, true, 2, 1, false },
  114. { V4L2_PIX_FMT_YUV420M, MEDIA_BUS_FMT_AYUV8_1X32,
  115. VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  116. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  117. 3, { 8, 8, 8 }, false, false, 2, 2, false },
  118. { V4L2_PIX_FMT_YVU420M, MEDIA_BUS_FMT_AYUV8_1X32,
  119. VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  120. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  121. 3, { 8, 8, 8 }, false, true, 2, 2, false },
  122. { V4L2_PIX_FMT_YUV422M, MEDIA_BUS_FMT_AYUV8_1X32,
  123. VI6_FMT_Y_U_V_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  124. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  125. 3, { 8, 8, 8 }, false, false, 2, 1, false },
  126. { V4L2_PIX_FMT_YVU422M, MEDIA_BUS_FMT_AYUV8_1X32,
  127. VI6_FMT_Y_U_V_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  128. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  129. 3, { 8, 8, 8 }, false, true, 2, 1, false },
  130. { V4L2_PIX_FMT_YUV444M, MEDIA_BUS_FMT_AYUV8_1X32,
  131. VI6_FMT_Y_U_V_444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  132. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  133. 3, { 8, 8, 8 }, false, false, 1, 1, false },
  134. { V4L2_PIX_FMT_YVU444M, MEDIA_BUS_FMT_AYUV8_1X32,
  135. VI6_FMT_Y_U_V_444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
  136. VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
  137. 3, { 8, 8, 8 }, false, true, 1, 1, false },
  138. };
  139. /**
  140. * vsp1_get_format_info - Retrieve format information for a 4CC
  141. * @vsp1: the VSP1 device
  142. * @fourcc: the format 4CC
  143. *
  144. * Return a pointer to the format information structure corresponding to the
  145. * given V4L2 format 4CC, or NULL if no corresponding format can be found.
  146. */
  147. const struct vsp1_format_info *vsp1_get_format_info(struct vsp1_device *vsp1,
  148. u32 fourcc)
  149. {
  150. unsigned int i;
  151. /* Special case, the VYUY and HSV formats are supported on Gen2 only. */
  152. if (vsp1->info->gen != 2) {
  153. switch (fourcc) {
  154. case V4L2_PIX_FMT_VYUY:
  155. case V4L2_PIX_FMT_HSV24:
  156. case V4L2_PIX_FMT_HSV32:
  157. return NULL;
  158. }
  159. }
  160. for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
  161. const struct vsp1_format_info *info = &vsp1_video_formats[i];
  162. if (info->fourcc == fourcc)
  163. return info;
  164. }
  165. return NULL;
  166. }
  167. /* -----------------------------------------------------------------------------
  168. * Pipeline Management
  169. */
  170. void vsp1_pipeline_reset(struct vsp1_pipeline *pipe)
  171. {
  172. struct vsp1_entity *entity;
  173. unsigned int i;
  174. if (pipe->brx) {
  175. struct vsp1_brx *brx = to_brx(&pipe->brx->subdev);
  176. for (i = 0; i < ARRAY_SIZE(brx->inputs); ++i)
  177. brx->inputs[i].rpf = NULL;
  178. }
  179. for (i = 0; i < ARRAY_SIZE(pipe->inputs); ++i)
  180. pipe->inputs[i] = NULL;
  181. pipe->output = NULL;
  182. list_for_each_entry(entity, &pipe->entities, list_pipe)
  183. entity->pipe = NULL;
  184. INIT_LIST_HEAD(&pipe->entities);
  185. pipe->state = VSP1_PIPELINE_STOPPED;
  186. pipe->buffers_ready = 0;
  187. pipe->num_inputs = 0;
  188. pipe->brx = NULL;
  189. pipe->hgo = NULL;
  190. pipe->hgt = NULL;
  191. pipe->lif = NULL;
  192. pipe->uds = NULL;
  193. }
  194. void vsp1_pipeline_init(struct vsp1_pipeline *pipe)
  195. {
  196. mutex_init(&pipe->lock);
  197. spin_lock_init(&pipe->irqlock);
  198. init_waitqueue_head(&pipe->wq);
  199. kref_init(&pipe->kref);
  200. INIT_LIST_HEAD(&pipe->entities);
  201. pipe->state = VSP1_PIPELINE_STOPPED;
  202. }
  203. /* Must be called with the pipe irqlock held. */
  204. void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
  205. {
  206. struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
  207. if (pipe->state == VSP1_PIPELINE_STOPPED) {
  208. vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index),
  209. VI6_CMD_STRCMD);
  210. pipe->state = VSP1_PIPELINE_RUNNING;
  211. }
  212. pipe->buffers_ready = 0;
  213. }
  214. bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe)
  215. {
  216. unsigned long flags;
  217. bool stopped;
  218. spin_lock_irqsave(&pipe->irqlock, flags);
  219. stopped = pipe->state == VSP1_PIPELINE_STOPPED;
  220. spin_unlock_irqrestore(&pipe->irqlock, flags);
  221. return stopped;
  222. }
  223. int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
  224. {
  225. struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
  226. struct vsp1_entity *entity;
  227. unsigned long flags;
  228. int ret;
  229. if (pipe->lif) {
  230. /*
  231. * When using display lists in continuous frame mode the only
  232. * way to stop the pipeline is to reset the hardware.
  233. */
  234. ret = vsp1_reset_wpf(vsp1, pipe->output->entity.index);
  235. if (ret == 0) {
  236. spin_lock_irqsave(&pipe->irqlock, flags);
  237. pipe->state = VSP1_PIPELINE_STOPPED;
  238. spin_unlock_irqrestore(&pipe->irqlock, flags);
  239. }
  240. } else {
  241. /* Otherwise just request a stop and wait. */
  242. spin_lock_irqsave(&pipe->irqlock, flags);
  243. if (pipe->state == VSP1_PIPELINE_RUNNING)
  244. pipe->state = VSP1_PIPELINE_STOPPING;
  245. spin_unlock_irqrestore(&pipe->irqlock, flags);
  246. ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
  247. msecs_to_jiffies(500));
  248. ret = ret == 0 ? -ETIMEDOUT : 0;
  249. }
  250. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  251. if (entity->route && entity->route->reg)
  252. vsp1_write(vsp1, entity->route->reg,
  253. VI6_DPR_NODE_UNUSED);
  254. }
  255. if (pipe->hgo)
  256. vsp1_write(vsp1, VI6_DPR_HGO_SMPPT,
  257. (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
  258. (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
  259. if (pipe->hgt)
  260. vsp1_write(vsp1, VI6_DPR_HGT_SMPPT,
  261. (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
  262. (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
  263. v4l2_subdev_call(&pipe->output->entity.subdev, video, s_stream, 0);
  264. return ret;
  265. }
  266. bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
  267. {
  268. unsigned int mask;
  269. mask = ((1 << pipe->num_inputs) - 1) << 1;
  270. if (!pipe->lif)
  271. mask |= 1 << 0;
  272. return pipe->buffers_ready == mask;
  273. }
  274. void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
  275. {
  276. unsigned int flags;
  277. if (pipe == NULL)
  278. return;
  279. /*
  280. * If the DL commit raced with the frame end interrupt, the commit ends
  281. * up being postponed by one frame. The returned flags tell whether the
  282. * active frame was finished or postponed.
  283. */
  284. flags = vsp1_dlm_irq_frame_end(pipe->output->dlm);
  285. if (pipe->hgo)
  286. vsp1_hgo_frame_end(pipe->hgo);
  287. if (pipe->hgt)
  288. vsp1_hgt_frame_end(pipe->hgt);
  289. /*
  290. * Regardless of frame completion we still need to notify the pipe
  291. * frame_end to account for vblank events.
  292. */
  293. if (pipe->frame_end)
  294. pipe->frame_end(pipe, flags);
  295. pipe->sequence++;
  296. }
  297. /*
  298. * Propagate the alpha value through the pipeline.
  299. *
  300. * As the UDS has restricted scaling capabilities when the alpha component needs
  301. * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha
  302. * value. The UDS then outputs a fixed alpha value which needs to be programmed
  303. * from the input RPF alpha.
  304. */
  305. void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe,
  306. struct vsp1_dl_body *dlb, unsigned int alpha)
  307. {
  308. if (!pipe->uds)
  309. return;
  310. /*
  311. * The BRU and BRS background color has a fixed alpha value set to 255,
  312. * the output alpha value is thus always equal to 255.
  313. */
  314. if (pipe->uds_input->type == VSP1_ENTITY_BRU ||
  315. pipe->uds_input->type == VSP1_ENTITY_BRS)
  316. alpha = 255;
  317. vsp1_uds_set_alpha(pipe->uds, dlb, alpha);
  318. }
  319. /*
  320. * Propagate the partition calculations through the pipeline
  321. *
  322. * Work backwards through the pipe, allowing each entity to update the partition
  323. * parameters based on its configuration, and the entity connected to its
  324. * source. Each entity must produce the partition required for the previous
  325. * entity in the pipeline.
  326. */
  327. void vsp1_pipeline_propagate_partition(struct vsp1_pipeline *pipe,
  328. struct vsp1_partition *partition,
  329. unsigned int index,
  330. struct vsp1_partition_window *window)
  331. {
  332. struct vsp1_entity *entity;
  333. list_for_each_entry_reverse(entity, &pipe->entities, list_pipe) {
  334. if (entity->ops->partition)
  335. entity->ops->partition(entity, pipe, partition, index,
  336. window);
  337. }
  338. }