vsp1_video.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. * vsp1_video.c -- R-Car VSP1 Video Node
  3. *
  4. * Copyright (C) 2013-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 <linux/list.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/v4l2-mediabus.h>
  18. #include <linux/videodev2.h>
  19. #include <linux/wait.h>
  20. #include <media/media-entity.h>
  21. #include <media/v4l2-dev.h>
  22. #include <media/v4l2-fh.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-subdev.h>
  25. #include <media/videobuf2-v4l2.h>
  26. #include <media/videobuf2-dma-contig.h>
  27. #include "vsp1.h"
  28. #include "vsp1_bru.h"
  29. #include "vsp1_dl.h"
  30. #include "vsp1_entity.h"
  31. #include "vsp1_pipe.h"
  32. #include "vsp1_rwpf.h"
  33. #include "vsp1_uds.h"
  34. #include "vsp1_video.h"
  35. #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
  36. #define VSP1_VIDEO_DEF_WIDTH 1024
  37. #define VSP1_VIDEO_DEF_HEIGHT 768
  38. #define VSP1_VIDEO_MIN_WIDTH 2U
  39. #define VSP1_VIDEO_MAX_WIDTH 8190U
  40. #define VSP1_VIDEO_MIN_HEIGHT 2U
  41. #define VSP1_VIDEO_MAX_HEIGHT 8190U
  42. /* -----------------------------------------------------------------------------
  43. * Helper functions
  44. */
  45. static struct v4l2_subdev *
  46. vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
  47. {
  48. struct media_pad *remote;
  49. remote = media_entity_remote_pad(local);
  50. if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
  51. return NULL;
  52. if (pad)
  53. *pad = remote->index;
  54. return media_entity_to_v4l2_subdev(remote->entity);
  55. }
  56. static int vsp1_video_verify_format(struct vsp1_video *video)
  57. {
  58. struct v4l2_subdev_format fmt;
  59. struct v4l2_subdev *subdev;
  60. int ret;
  61. subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
  62. if (subdev == NULL)
  63. return -EINVAL;
  64. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  65. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  66. if (ret < 0)
  67. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  68. if (video->rwpf->fmtinfo->mbus != fmt.format.code ||
  69. video->rwpf->format.height != fmt.format.height ||
  70. video->rwpf->format.width != fmt.format.width)
  71. return -EINVAL;
  72. return 0;
  73. }
  74. static int __vsp1_video_try_format(struct vsp1_video *video,
  75. struct v4l2_pix_format_mplane *pix,
  76. const struct vsp1_format_info **fmtinfo)
  77. {
  78. static const u32 xrgb_formats[][2] = {
  79. { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
  80. { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
  81. { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
  82. { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
  83. };
  84. const struct vsp1_format_info *info;
  85. unsigned int width = pix->width;
  86. unsigned int height = pix->height;
  87. unsigned int i;
  88. /* Backward compatibility: replace deprecated RGB formats by their XRGB
  89. * equivalent. This selects the format older userspace applications want
  90. * while still exposing the new format.
  91. */
  92. for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
  93. if (xrgb_formats[i][0] == pix->pixelformat) {
  94. pix->pixelformat = xrgb_formats[i][1];
  95. break;
  96. }
  97. }
  98. /* Retrieve format information and select the default format if the
  99. * requested format isn't supported.
  100. */
  101. info = vsp1_get_format_info(video->vsp1, pix->pixelformat);
  102. if (info == NULL)
  103. info = vsp1_get_format_info(video->vsp1, VSP1_VIDEO_DEF_FORMAT);
  104. pix->pixelformat = info->fourcc;
  105. pix->colorspace = V4L2_COLORSPACE_SRGB;
  106. pix->field = V4L2_FIELD_NONE;
  107. if (info->fourcc == V4L2_PIX_FMT_HSV24 ||
  108. info->fourcc == V4L2_PIX_FMT_HSV32)
  109. pix->hsv_enc = V4L2_HSV_ENC_256;
  110. memset(pix->reserved, 0, sizeof(pix->reserved));
  111. /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
  112. width = round_down(width, info->hsub);
  113. height = round_down(height, info->vsub);
  114. /* Clamp the width and height. */
  115. pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
  116. pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
  117. VSP1_VIDEO_MAX_HEIGHT);
  118. /* Compute and clamp the stride and image size. While not documented in
  119. * the datasheet, strides not aligned to a multiple of 128 bytes result
  120. * in image corruption.
  121. */
  122. for (i = 0; i < min(info->planes, 2U); ++i) {
  123. unsigned int hsub = i > 0 ? info->hsub : 1;
  124. unsigned int vsub = i > 0 ? info->vsub : 1;
  125. unsigned int align = 128;
  126. unsigned int bpl;
  127. bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
  128. pix->width / hsub * info->bpp[i] / 8,
  129. round_down(65535U, align));
  130. pix->plane_fmt[i].bytesperline = round_up(bpl, align);
  131. pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
  132. * pix->height / vsub;
  133. }
  134. if (info->planes == 3) {
  135. /* The second and third planes must have the same stride. */
  136. pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
  137. pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
  138. }
  139. pix->num_planes = info->planes;
  140. if (fmtinfo)
  141. *fmtinfo = info;
  142. return 0;
  143. }
  144. /* -----------------------------------------------------------------------------
  145. * VSP1 Partition Algorithm support
  146. */
  147. static void vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline *pipe)
  148. {
  149. struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
  150. const struct v4l2_mbus_framefmt *format;
  151. struct vsp1_entity *entity;
  152. unsigned int div_size;
  153. format = vsp1_entity_get_pad_format(&pipe->output->entity,
  154. pipe->output->entity.config,
  155. RWPF_PAD_SOURCE);
  156. div_size = format->width;
  157. /* Gen2 hardware doesn't require image partitioning. */
  158. if (vsp1->info->gen == 2) {
  159. pipe->div_size = div_size;
  160. pipe->partitions = 1;
  161. return;
  162. }
  163. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  164. unsigned int entity_max = VSP1_VIDEO_MAX_WIDTH;
  165. if (entity->ops->max_width) {
  166. entity_max = entity->ops->max_width(entity, pipe);
  167. if (entity_max)
  168. div_size = min(div_size, entity_max);
  169. }
  170. }
  171. pipe->div_size = div_size;
  172. pipe->partitions = DIV_ROUND_UP(format->width, div_size);
  173. }
  174. /**
  175. * vsp1_video_partition - Calculate the active partition output window
  176. *
  177. * @div_size: pre-determined maximum partition division size
  178. * @index: partition index
  179. *
  180. * Returns a v4l2_rect describing the partition window.
  181. */
  182. static struct v4l2_rect vsp1_video_partition(struct vsp1_pipeline *pipe,
  183. unsigned int div_size,
  184. unsigned int index)
  185. {
  186. const struct v4l2_mbus_framefmt *format;
  187. struct v4l2_rect partition;
  188. unsigned int modulus;
  189. format = vsp1_entity_get_pad_format(&pipe->output->entity,
  190. pipe->output->entity.config,
  191. RWPF_PAD_SOURCE);
  192. /* A single partition simply processes the output size in full. */
  193. if (pipe->partitions <= 1) {
  194. partition.left = 0;
  195. partition.top = 0;
  196. partition.width = format->width;
  197. partition.height = format->height;
  198. return partition;
  199. }
  200. /* Initialise the partition with sane starting conditions. */
  201. partition.left = index * div_size;
  202. partition.top = 0;
  203. partition.width = div_size;
  204. partition.height = format->height;
  205. modulus = format->width % div_size;
  206. /*
  207. * We need to prevent the last partition from being smaller than the
  208. * *minimum* width of the hardware capabilities.
  209. *
  210. * If the modulus is less than half of the partition size,
  211. * the penultimate partition is reduced to half, which is added
  212. * to the final partition: |1234|1234|1234|12|341|
  213. * to prevents this: |1234|1234|1234|1234|1|.
  214. */
  215. if (modulus) {
  216. /*
  217. * pipe->partitions is 1 based, whilst index is a 0 based index.
  218. * Normalise this locally.
  219. */
  220. unsigned int partitions = pipe->partitions - 1;
  221. if (modulus < div_size / 2) {
  222. if (index == partitions - 1) {
  223. /* Halve the penultimate partition. */
  224. partition.width = div_size / 2;
  225. } else if (index == partitions) {
  226. /* Increase the final partition. */
  227. partition.width = (div_size / 2) + modulus;
  228. partition.left -= div_size / 2;
  229. }
  230. } else if (index == partitions) {
  231. partition.width = modulus;
  232. }
  233. }
  234. return partition;
  235. }
  236. /* -----------------------------------------------------------------------------
  237. * Pipeline Management
  238. */
  239. /*
  240. * vsp1_video_complete_buffer - Complete the current buffer
  241. * @video: the video node
  242. *
  243. * This function completes the current buffer by filling its sequence number,
  244. * time stamp and payload size, and hands it back to the videobuf core.
  245. *
  246. * When operating in DU output mode (deep pipeline to the DU through the LIF),
  247. * the VSP1 needs to constantly supply frames to the display. In that case, if
  248. * no other buffer is queued, reuse the one that has just been processed instead
  249. * of handing it back to the videobuf core.
  250. *
  251. * Return the next queued buffer or NULL if the queue is empty.
  252. */
  253. static struct vsp1_vb2_buffer *
  254. vsp1_video_complete_buffer(struct vsp1_video *video)
  255. {
  256. struct vsp1_pipeline *pipe = video->rwpf->pipe;
  257. struct vsp1_vb2_buffer *next = NULL;
  258. struct vsp1_vb2_buffer *done;
  259. unsigned long flags;
  260. unsigned int i;
  261. spin_lock_irqsave(&video->irqlock, flags);
  262. if (list_empty(&video->irqqueue)) {
  263. spin_unlock_irqrestore(&video->irqlock, flags);
  264. return NULL;
  265. }
  266. done = list_first_entry(&video->irqqueue,
  267. struct vsp1_vb2_buffer, queue);
  268. /* In DU output mode reuse the buffer if the list is singular. */
  269. if (pipe->lif && list_is_singular(&video->irqqueue)) {
  270. spin_unlock_irqrestore(&video->irqlock, flags);
  271. return done;
  272. }
  273. list_del(&done->queue);
  274. if (!list_empty(&video->irqqueue))
  275. next = list_first_entry(&video->irqqueue,
  276. struct vsp1_vb2_buffer, queue);
  277. spin_unlock_irqrestore(&video->irqlock, flags);
  278. done->buf.sequence = pipe->sequence;
  279. done->buf.vb2_buf.timestamp = ktime_get_ns();
  280. for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
  281. vb2_set_plane_payload(&done->buf.vb2_buf, i,
  282. vb2_plane_size(&done->buf.vb2_buf, i));
  283. vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
  284. return next;
  285. }
  286. static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
  287. struct vsp1_rwpf *rwpf)
  288. {
  289. struct vsp1_video *video = rwpf->video;
  290. struct vsp1_vb2_buffer *buf;
  291. buf = vsp1_video_complete_buffer(video);
  292. if (buf == NULL)
  293. return;
  294. video->rwpf->mem = buf->mem;
  295. pipe->buffers_ready |= 1 << video->pipe_index;
  296. }
  297. static void vsp1_video_pipeline_run_partition(struct vsp1_pipeline *pipe,
  298. struct vsp1_dl_list *dl)
  299. {
  300. struct vsp1_entity *entity;
  301. pipe->partition = vsp1_video_partition(pipe, pipe->div_size,
  302. pipe->current_partition);
  303. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  304. if (entity->ops->configure)
  305. entity->ops->configure(entity, pipe, dl,
  306. VSP1_ENTITY_PARAMS_PARTITION);
  307. }
  308. }
  309. static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
  310. {
  311. struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
  312. struct vsp1_entity *entity;
  313. if (!pipe->dl)
  314. pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
  315. /*
  316. * Start with the runtime parameters as the configure operation can
  317. * compute/cache information needed when configuring partitions. This
  318. * is the case with flipping in the WPF.
  319. */
  320. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  321. if (entity->ops->configure)
  322. entity->ops->configure(entity, pipe, pipe->dl,
  323. VSP1_ENTITY_PARAMS_RUNTIME);
  324. }
  325. /* Run the first partition */
  326. pipe->current_partition = 0;
  327. vsp1_video_pipeline_run_partition(pipe, pipe->dl);
  328. /* Process consecutive partitions as necessary */
  329. for (pipe->current_partition = 1;
  330. pipe->current_partition < pipe->partitions;
  331. pipe->current_partition++) {
  332. struct vsp1_dl_list *dl;
  333. /*
  334. * Partition configuration operations will utilise
  335. * the pipe->current_partition variable to determine
  336. * the work they should complete.
  337. */
  338. dl = vsp1_dl_list_get(pipe->output->dlm);
  339. /*
  340. * An incomplete chain will still function, but output only
  341. * the partitions that had a dl available. The frame end
  342. * interrupt will be marked on the last dl in the chain.
  343. */
  344. if (!dl) {
  345. dev_err(vsp1->dev, "Failed to obtain a dl list. Frame will be incomplete\n");
  346. break;
  347. }
  348. vsp1_video_pipeline_run_partition(pipe, dl);
  349. vsp1_dl_list_add_chain(pipe->dl, dl);
  350. }
  351. /* Complete, and commit the head display list. */
  352. vsp1_dl_list_commit(pipe->dl);
  353. pipe->dl = NULL;
  354. vsp1_pipeline_run(pipe);
  355. }
  356. static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe)
  357. {
  358. struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
  359. enum vsp1_pipeline_state state;
  360. unsigned long flags;
  361. unsigned int i;
  362. spin_lock_irqsave(&pipe->irqlock, flags);
  363. /* Complete buffers on all video nodes. */
  364. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  365. if (!pipe->inputs[i])
  366. continue;
  367. vsp1_video_frame_end(pipe, pipe->inputs[i]);
  368. }
  369. vsp1_video_frame_end(pipe, pipe->output);
  370. state = pipe->state;
  371. pipe->state = VSP1_PIPELINE_STOPPED;
  372. /* If a stop has been requested, mark the pipeline as stopped and
  373. * return. Otherwise restart the pipeline if ready.
  374. */
  375. if (state == VSP1_PIPELINE_STOPPING)
  376. wake_up(&pipe->wq);
  377. else if (vsp1_pipeline_ready(pipe))
  378. vsp1_video_pipeline_run(pipe);
  379. spin_unlock_irqrestore(&pipe->irqlock, flags);
  380. }
  381. static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe,
  382. struct vsp1_rwpf *input,
  383. struct vsp1_rwpf *output)
  384. {
  385. struct media_entity_enum ent_enum;
  386. struct vsp1_entity *entity;
  387. struct media_pad *pad;
  388. bool bru_found = false;
  389. int ret;
  390. ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
  391. if (ret < 0)
  392. return ret;
  393. pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
  394. while (1) {
  395. if (pad == NULL) {
  396. ret = -EPIPE;
  397. goto out;
  398. }
  399. /* We've reached a video node, that shouldn't have happened. */
  400. if (!is_media_entity_v4l2_subdev(pad->entity)) {
  401. ret = -EPIPE;
  402. goto out;
  403. }
  404. entity = to_vsp1_entity(
  405. media_entity_to_v4l2_subdev(pad->entity));
  406. /* A BRU is present in the pipeline, store the BRU input pad
  407. * number in the input RPF for use when configuring the RPF.
  408. */
  409. if (entity->type == VSP1_ENTITY_BRU) {
  410. struct vsp1_bru *bru = to_bru(&entity->subdev);
  411. bru->inputs[pad->index].rpf = input;
  412. input->bru_input = pad->index;
  413. bru_found = true;
  414. }
  415. /* We've reached the WPF, we're done. */
  416. if (entity->type == VSP1_ENTITY_WPF)
  417. break;
  418. /* Ensure the branch has no loop. */
  419. if (media_entity_enum_test_and_set(&ent_enum,
  420. &entity->subdev.entity)) {
  421. ret = -EPIPE;
  422. goto out;
  423. }
  424. /* UDS can't be chained. */
  425. if (entity->type == VSP1_ENTITY_UDS) {
  426. if (pipe->uds) {
  427. ret = -EPIPE;
  428. goto out;
  429. }
  430. pipe->uds = entity;
  431. pipe->uds_input = bru_found ? pipe->bru
  432. : &input->entity;
  433. }
  434. /* Follow the source link. The link setup operations ensure
  435. * that the output fan-out can't be more than one, there is thus
  436. * no need to verify here that only a single source link is
  437. * activated.
  438. */
  439. pad = &entity->pads[entity->source_pad];
  440. pad = media_entity_remote_pad(pad);
  441. }
  442. /* The last entity must be the output WPF. */
  443. if (entity != &output->entity)
  444. ret = -EPIPE;
  445. out:
  446. media_entity_enum_cleanup(&ent_enum);
  447. return ret;
  448. }
  449. static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe,
  450. struct vsp1_video *video)
  451. {
  452. struct media_entity_graph graph;
  453. struct media_entity *entity = &video->video.entity;
  454. struct media_device *mdev = entity->graph_obj.mdev;
  455. unsigned int i;
  456. int ret;
  457. /* Walk the graph to locate the entities and video nodes. */
  458. ret = media_entity_graph_walk_init(&graph, mdev);
  459. if (ret)
  460. return ret;
  461. media_entity_graph_walk_start(&graph, entity);
  462. while ((entity = media_entity_graph_walk_next(&graph))) {
  463. struct v4l2_subdev *subdev;
  464. struct vsp1_rwpf *rwpf;
  465. struct vsp1_entity *e;
  466. if (!is_media_entity_v4l2_subdev(entity))
  467. continue;
  468. subdev = media_entity_to_v4l2_subdev(entity);
  469. e = to_vsp1_entity(subdev);
  470. list_add_tail(&e->list_pipe, &pipe->entities);
  471. if (e->type == VSP1_ENTITY_RPF) {
  472. rwpf = to_rwpf(subdev);
  473. pipe->inputs[rwpf->entity.index] = rwpf;
  474. rwpf->video->pipe_index = ++pipe->num_inputs;
  475. rwpf->pipe = pipe;
  476. } else if (e->type == VSP1_ENTITY_WPF) {
  477. rwpf = to_rwpf(subdev);
  478. pipe->output = rwpf;
  479. rwpf->video->pipe_index = 0;
  480. rwpf->pipe = pipe;
  481. } else if (e->type == VSP1_ENTITY_LIF) {
  482. pipe->lif = e;
  483. } else if (e->type == VSP1_ENTITY_BRU) {
  484. pipe->bru = e;
  485. }
  486. }
  487. media_entity_graph_walk_cleanup(&graph);
  488. /* We need one output and at least one input. */
  489. if (pipe->num_inputs == 0 || !pipe->output)
  490. return -EPIPE;
  491. /* Follow links downstream for each input and make sure the graph
  492. * contains no loop and that all branches end at the output WPF.
  493. */
  494. for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
  495. if (!pipe->inputs[i])
  496. continue;
  497. ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i],
  498. pipe->output);
  499. if (ret < 0)
  500. return ret;
  501. }
  502. return 0;
  503. }
  504. static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
  505. struct vsp1_video *video)
  506. {
  507. vsp1_pipeline_init(pipe);
  508. pipe->frame_end = vsp1_video_pipeline_frame_end;
  509. return vsp1_video_pipeline_build(pipe, video);
  510. }
  511. static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video)
  512. {
  513. struct vsp1_pipeline *pipe;
  514. int ret;
  515. /* Get a pipeline object for the video node. If a pipeline has already
  516. * been allocated just increment its reference count and return it.
  517. * Otherwise allocate a new pipeline and initialize it, it will be freed
  518. * when the last reference is released.
  519. */
  520. if (!video->rwpf->pipe) {
  521. pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
  522. if (!pipe)
  523. return ERR_PTR(-ENOMEM);
  524. ret = vsp1_video_pipeline_init(pipe, video);
  525. if (ret < 0) {
  526. vsp1_pipeline_reset(pipe);
  527. kfree(pipe);
  528. return ERR_PTR(ret);
  529. }
  530. } else {
  531. pipe = video->rwpf->pipe;
  532. kref_get(&pipe->kref);
  533. }
  534. return pipe;
  535. }
  536. static void vsp1_video_pipeline_release(struct kref *kref)
  537. {
  538. struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref);
  539. vsp1_pipeline_reset(pipe);
  540. kfree(pipe);
  541. }
  542. static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
  543. {
  544. struct media_device *mdev = &pipe->output->entity.vsp1->media_dev;
  545. mutex_lock(&mdev->graph_mutex);
  546. kref_put(&pipe->kref, vsp1_video_pipeline_release);
  547. mutex_unlock(&mdev->graph_mutex);
  548. }
  549. /* -----------------------------------------------------------------------------
  550. * videobuf2 Queue Operations
  551. */
  552. static int
  553. vsp1_video_queue_setup(struct vb2_queue *vq,
  554. unsigned int *nbuffers, unsigned int *nplanes,
  555. unsigned int sizes[], struct device *alloc_devs[])
  556. {
  557. struct vsp1_video *video = vb2_get_drv_priv(vq);
  558. const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
  559. unsigned int i;
  560. if (*nplanes) {
  561. if (*nplanes != format->num_planes)
  562. return -EINVAL;
  563. for (i = 0; i < *nplanes; i++)
  564. if (sizes[i] < format->plane_fmt[i].sizeimage)
  565. return -EINVAL;
  566. return 0;
  567. }
  568. *nplanes = format->num_planes;
  569. for (i = 0; i < format->num_planes; ++i)
  570. sizes[i] = format->plane_fmt[i].sizeimage;
  571. return 0;
  572. }
  573. static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
  574. {
  575. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  576. struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
  577. struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
  578. const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
  579. unsigned int i;
  580. if (vb->num_planes < format->num_planes)
  581. return -EINVAL;
  582. for (i = 0; i < vb->num_planes; ++i) {
  583. buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
  584. if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
  585. return -EINVAL;
  586. }
  587. for ( ; i < 3; ++i)
  588. buf->mem.addr[i] = 0;
  589. return 0;
  590. }
  591. static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
  592. {
  593. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  594. struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
  595. struct vsp1_pipeline *pipe = video->rwpf->pipe;
  596. struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
  597. unsigned long flags;
  598. bool empty;
  599. spin_lock_irqsave(&video->irqlock, flags);
  600. empty = list_empty(&video->irqqueue);
  601. list_add_tail(&buf->queue, &video->irqqueue);
  602. spin_unlock_irqrestore(&video->irqlock, flags);
  603. if (!empty)
  604. return;
  605. spin_lock_irqsave(&pipe->irqlock, flags);
  606. video->rwpf->mem = buf->mem;
  607. pipe->buffers_ready |= 1 << video->pipe_index;
  608. if (vb2_is_streaming(&video->queue) &&
  609. vsp1_pipeline_ready(pipe))
  610. vsp1_video_pipeline_run(pipe);
  611. spin_unlock_irqrestore(&pipe->irqlock, flags);
  612. }
  613. static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
  614. {
  615. struct vsp1_entity *entity;
  616. /* Determine this pipelines sizes for image partitioning support. */
  617. vsp1_video_pipeline_setup_partitions(pipe);
  618. /* Prepare the display list. */
  619. pipe->dl = vsp1_dl_list_get(pipe->output->dlm);
  620. if (!pipe->dl)
  621. return -ENOMEM;
  622. if (pipe->uds) {
  623. struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
  624. /* If a BRU is present in the pipeline before the UDS, the alpha
  625. * component doesn't need to be scaled as the BRU output alpha
  626. * value is fixed to 255. Otherwise we need to scale the alpha
  627. * component only when available at the input RPF.
  628. */
  629. if (pipe->uds_input->type == VSP1_ENTITY_BRU) {
  630. uds->scale_alpha = false;
  631. } else {
  632. struct vsp1_rwpf *rpf =
  633. to_rwpf(&pipe->uds_input->subdev);
  634. uds->scale_alpha = rpf->fmtinfo->alpha;
  635. }
  636. }
  637. list_for_each_entry(entity, &pipe->entities, list_pipe) {
  638. vsp1_entity_route_setup(entity, pipe->dl);
  639. if (entity->ops->configure)
  640. entity->ops->configure(entity, pipe, pipe->dl,
  641. VSP1_ENTITY_PARAMS_INIT);
  642. }
  643. return 0;
  644. }
  645. static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
  646. {
  647. struct vsp1_video *video = vb2_get_drv_priv(vq);
  648. struct vsp1_pipeline *pipe = video->rwpf->pipe;
  649. unsigned long flags;
  650. int ret;
  651. mutex_lock(&pipe->lock);
  652. if (pipe->stream_count == pipe->num_inputs) {
  653. ret = vsp1_video_setup_pipeline(pipe);
  654. if (ret < 0) {
  655. mutex_unlock(&pipe->lock);
  656. return ret;
  657. }
  658. }
  659. pipe->stream_count++;
  660. mutex_unlock(&pipe->lock);
  661. spin_lock_irqsave(&pipe->irqlock, flags);
  662. if (vsp1_pipeline_ready(pipe))
  663. vsp1_video_pipeline_run(pipe);
  664. spin_unlock_irqrestore(&pipe->irqlock, flags);
  665. return 0;
  666. }
  667. static void vsp1_video_stop_streaming(struct vb2_queue *vq)
  668. {
  669. struct vsp1_video *video = vb2_get_drv_priv(vq);
  670. struct vsp1_pipeline *pipe = video->rwpf->pipe;
  671. struct vsp1_vb2_buffer *buffer;
  672. unsigned long flags;
  673. int ret;
  674. /*
  675. * Clear the buffers ready flag to make sure the device won't be started
  676. * by a QBUF on the video node on the other side of the pipeline.
  677. */
  678. spin_lock_irqsave(&video->irqlock, flags);
  679. pipe->buffers_ready &= ~(1 << video->pipe_index);
  680. spin_unlock_irqrestore(&video->irqlock, flags);
  681. mutex_lock(&pipe->lock);
  682. if (--pipe->stream_count == pipe->num_inputs) {
  683. /* Stop the pipeline. */
  684. ret = vsp1_pipeline_stop(pipe);
  685. if (ret == -ETIMEDOUT)
  686. dev_err(video->vsp1->dev, "pipeline stop timeout\n");
  687. vsp1_dl_list_put(pipe->dl);
  688. pipe->dl = NULL;
  689. }
  690. mutex_unlock(&pipe->lock);
  691. media_entity_pipeline_stop(&video->video.entity);
  692. vsp1_video_pipeline_put(pipe);
  693. /* Remove all buffers from the IRQ queue. */
  694. spin_lock_irqsave(&video->irqlock, flags);
  695. list_for_each_entry(buffer, &video->irqqueue, queue)
  696. vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
  697. INIT_LIST_HEAD(&video->irqqueue);
  698. spin_unlock_irqrestore(&video->irqlock, flags);
  699. }
  700. static const struct vb2_ops vsp1_video_queue_qops = {
  701. .queue_setup = vsp1_video_queue_setup,
  702. .buf_prepare = vsp1_video_buffer_prepare,
  703. .buf_queue = vsp1_video_buffer_queue,
  704. .wait_prepare = vb2_ops_wait_prepare,
  705. .wait_finish = vb2_ops_wait_finish,
  706. .start_streaming = vsp1_video_start_streaming,
  707. .stop_streaming = vsp1_video_stop_streaming,
  708. };
  709. /* -----------------------------------------------------------------------------
  710. * V4L2 ioctls
  711. */
  712. static int
  713. vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  714. {
  715. struct v4l2_fh *vfh = file->private_data;
  716. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  717. cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
  718. | V4L2_CAP_VIDEO_CAPTURE_MPLANE
  719. | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  720. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  721. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
  722. | V4L2_CAP_STREAMING;
  723. else
  724. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
  725. | V4L2_CAP_STREAMING;
  726. strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
  727. strlcpy(cap->card, video->video.name, sizeof(cap->card));
  728. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  729. dev_name(video->vsp1->dev));
  730. return 0;
  731. }
  732. static int
  733. vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
  734. {
  735. struct v4l2_fh *vfh = file->private_data;
  736. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  737. if (format->type != video->queue.type)
  738. return -EINVAL;
  739. mutex_lock(&video->lock);
  740. format->fmt.pix_mp = video->rwpf->format;
  741. mutex_unlock(&video->lock);
  742. return 0;
  743. }
  744. static int
  745. vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
  746. {
  747. struct v4l2_fh *vfh = file->private_data;
  748. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  749. if (format->type != video->queue.type)
  750. return -EINVAL;
  751. return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
  752. }
  753. static int
  754. vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
  755. {
  756. struct v4l2_fh *vfh = file->private_data;
  757. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  758. const struct vsp1_format_info *info;
  759. int ret;
  760. if (format->type != video->queue.type)
  761. return -EINVAL;
  762. ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
  763. if (ret < 0)
  764. return ret;
  765. mutex_lock(&video->lock);
  766. if (vb2_is_busy(&video->queue)) {
  767. ret = -EBUSY;
  768. goto done;
  769. }
  770. video->rwpf->format = format->fmt.pix_mp;
  771. video->rwpf->fmtinfo = info;
  772. done:
  773. mutex_unlock(&video->lock);
  774. return ret;
  775. }
  776. static int
  777. vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  778. {
  779. struct v4l2_fh *vfh = file->private_data;
  780. struct vsp1_video *video = to_vsp1_video(vfh->vdev);
  781. struct media_device *mdev = &video->vsp1->media_dev;
  782. struct vsp1_pipeline *pipe;
  783. int ret;
  784. if (video->queue.owner && video->queue.owner != file->private_data)
  785. return -EBUSY;
  786. /* Get a pipeline for the video node and start streaming on it. No link
  787. * touching an entity in the pipeline can be activated or deactivated
  788. * once streaming is started.
  789. */
  790. mutex_lock(&mdev->graph_mutex);
  791. pipe = vsp1_video_pipeline_get(video);
  792. if (IS_ERR(pipe)) {
  793. mutex_unlock(&mdev->graph_mutex);
  794. return PTR_ERR(pipe);
  795. }
  796. ret = __media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
  797. if (ret < 0) {
  798. mutex_unlock(&mdev->graph_mutex);
  799. goto err_pipe;
  800. }
  801. mutex_unlock(&mdev->graph_mutex);
  802. /* Verify that the configured format matches the output of the connected
  803. * subdev.
  804. */
  805. ret = vsp1_video_verify_format(video);
  806. if (ret < 0)
  807. goto err_stop;
  808. /* Start the queue. */
  809. ret = vb2_streamon(&video->queue, type);
  810. if (ret < 0)
  811. goto err_stop;
  812. return 0;
  813. err_stop:
  814. media_entity_pipeline_stop(&video->video.entity);
  815. err_pipe:
  816. vsp1_video_pipeline_put(pipe);
  817. return ret;
  818. }
  819. static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
  820. .vidioc_querycap = vsp1_video_querycap,
  821. .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
  822. .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
  823. .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
  824. .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
  825. .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
  826. .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
  827. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  828. .vidioc_querybuf = vb2_ioctl_querybuf,
  829. .vidioc_qbuf = vb2_ioctl_qbuf,
  830. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  831. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  832. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  833. .vidioc_streamon = vsp1_video_streamon,
  834. .vidioc_streamoff = vb2_ioctl_streamoff,
  835. };
  836. /* -----------------------------------------------------------------------------
  837. * V4L2 File Operations
  838. */
  839. static int vsp1_video_open(struct file *file)
  840. {
  841. struct vsp1_video *video = video_drvdata(file);
  842. struct v4l2_fh *vfh;
  843. int ret = 0;
  844. vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
  845. if (vfh == NULL)
  846. return -ENOMEM;
  847. v4l2_fh_init(vfh, &video->video);
  848. v4l2_fh_add(vfh);
  849. file->private_data = vfh;
  850. ret = vsp1_device_get(video->vsp1);
  851. if (ret < 0) {
  852. v4l2_fh_del(vfh);
  853. kfree(vfh);
  854. }
  855. return ret;
  856. }
  857. static int vsp1_video_release(struct file *file)
  858. {
  859. struct vsp1_video *video = video_drvdata(file);
  860. struct v4l2_fh *vfh = file->private_data;
  861. mutex_lock(&video->lock);
  862. if (video->queue.owner == vfh) {
  863. vb2_queue_release(&video->queue);
  864. video->queue.owner = NULL;
  865. }
  866. mutex_unlock(&video->lock);
  867. vsp1_device_put(video->vsp1);
  868. v4l2_fh_release(file);
  869. file->private_data = NULL;
  870. return 0;
  871. }
  872. static const struct v4l2_file_operations vsp1_video_fops = {
  873. .owner = THIS_MODULE,
  874. .unlocked_ioctl = video_ioctl2,
  875. .open = vsp1_video_open,
  876. .release = vsp1_video_release,
  877. .poll = vb2_fop_poll,
  878. .mmap = vb2_fop_mmap,
  879. };
  880. /* -----------------------------------------------------------------------------
  881. * Initialization and Cleanup
  882. */
  883. struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
  884. struct vsp1_rwpf *rwpf)
  885. {
  886. struct vsp1_video *video;
  887. const char *direction;
  888. int ret;
  889. video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
  890. if (!video)
  891. return ERR_PTR(-ENOMEM);
  892. rwpf->video = video;
  893. video->vsp1 = vsp1;
  894. video->rwpf = rwpf;
  895. if (rwpf->entity.type == VSP1_ENTITY_RPF) {
  896. direction = "input";
  897. video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  898. video->pad.flags = MEDIA_PAD_FL_SOURCE;
  899. video->video.vfl_dir = VFL_DIR_TX;
  900. } else {
  901. direction = "output";
  902. video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  903. video->pad.flags = MEDIA_PAD_FL_SINK;
  904. video->video.vfl_dir = VFL_DIR_RX;
  905. }
  906. mutex_init(&video->lock);
  907. spin_lock_init(&video->irqlock);
  908. INIT_LIST_HEAD(&video->irqqueue);
  909. /* Initialize the media entity... */
  910. ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
  911. if (ret < 0)
  912. return ERR_PTR(ret);
  913. /* ... and the format ... */
  914. rwpf->format.pixelformat = VSP1_VIDEO_DEF_FORMAT;
  915. rwpf->format.width = VSP1_VIDEO_DEF_WIDTH;
  916. rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT;
  917. __vsp1_video_try_format(video, &rwpf->format, &rwpf->fmtinfo);
  918. /* ... and the video node... */
  919. video->video.v4l2_dev = &video->vsp1->v4l2_dev;
  920. video->video.fops = &vsp1_video_fops;
  921. snprintf(video->video.name, sizeof(video->video.name), "%s %s",
  922. rwpf->entity.subdev.name, direction);
  923. video->video.vfl_type = VFL_TYPE_GRABBER;
  924. video->video.release = video_device_release_empty;
  925. video->video.ioctl_ops = &vsp1_video_ioctl_ops;
  926. video_set_drvdata(&video->video, video);
  927. video->queue.type = video->type;
  928. video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  929. video->queue.lock = &video->lock;
  930. video->queue.drv_priv = video;
  931. video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer);
  932. video->queue.ops = &vsp1_video_queue_qops;
  933. video->queue.mem_ops = &vb2_dma_contig_memops;
  934. video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  935. video->queue.dev = video->vsp1->dev;
  936. ret = vb2_queue_init(&video->queue);
  937. if (ret < 0) {
  938. dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
  939. goto error;
  940. }
  941. /* ... and register the video device. */
  942. video->video.queue = &video->queue;
  943. ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
  944. if (ret < 0) {
  945. dev_err(video->vsp1->dev, "failed to register video device\n");
  946. goto error;
  947. }
  948. return video;
  949. error:
  950. vsp1_video_cleanup(video);
  951. return ERR_PTR(ret);
  952. }
  953. void vsp1_video_cleanup(struct vsp1_video *video)
  954. {
  955. if (video_is_registered(&video->video))
  956. video_unregister_device(&video->video);
  957. media_entity_cleanup(&video->video.entity);
  958. }