v4l2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NVIDIA Tegra Video decoder driver
  4. *
  5. * Copyright (C) 2019-2022 Dmitry Osipenko <digetx@gmail.com>
  6. *
  7. * Based on Cedrus driver by Bootlin.
  8. * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
  9. * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
  10. *
  11. * Based on Rockchip driver by Collabora.
  12. * Copyright (C) 2019 Boris Brezillon <boris.brezillon@collabora.com>
  13. */
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include "vde.h"
  17. static const struct v4l2_ctrl_config ctrl_cfgs[] = {
  18. { .id = V4L2_CID_STATELESS_H264_DECODE_PARAMS, },
  19. { .id = V4L2_CID_STATELESS_H264_SPS, },
  20. { .id = V4L2_CID_STATELESS_H264_PPS, },
  21. {
  22. .id = V4L2_CID_STATELESS_H264_DECODE_MODE,
  23. .min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
  24. .max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
  25. .def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
  26. },
  27. {
  28. .id = V4L2_CID_STATELESS_H264_START_CODE,
  29. .min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
  30. .max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
  31. .def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
  32. },
  33. {
  34. .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  35. .min = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
  36. .max = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
  37. .def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
  38. },
  39. {
  40. .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
  41. .min = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
  42. .max = V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
  43. },
  44. };
  45. static inline struct tegra_ctx *file_to_tegra_ctx(struct file *file)
  46. {
  47. return container_of(file_to_v4l2_fh(file), struct tegra_ctx, fh);
  48. }
  49. static void tegra_set_control_data(struct tegra_ctx *ctx, void *data, u32 id)
  50. {
  51. switch (id) {
  52. case V4L2_CID_STATELESS_H264_DECODE_PARAMS:
  53. ctx->h264.decode_params = data;
  54. break;
  55. case V4L2_CID_STATELESS_H264_SPS:
  56. ctx->h264.sps = data;
  57. break;
  58. case V4L2_CID_STATELESS_H264_PPS:
  59. ctx->h264.pps = data;
  60. break;
  61. }
  62. }
  63. void tegra_vde_prepare_control_data(struct tegra_ctx *ctx, u32 id)
  64. {
  65. unsigned int i;
  66. for (i = 0; i < ARRAY_SIZE(ctrl_cfgs); i++) {
  67. if (ctx->ctrls[i]->id == id) {
  68. tegra_set_control_data(ctx, ctx->ctrls[i]->p_cur.p, id);
  69. return;
  70. }
  71. }
  72. tegra_set_control_data(ctx, NULL, id);
  73. }
  74. static int tegra_queue_setup(struct vb2_queue *vq,
  75. unsigned int *nbufs,
  76. unsigned int *num_planes,
  77. unsigned int sizes[],
  78. struct device *alloc_devs[])
  79. {
  80. struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
  81. struct v4l2_format *f;
  82. unsigned int i;
  83. if (V4L2_TYPE_IS_OUTPUT(vq->type))
  84. f = &ctx->coded_fmt;
  85. else
  86. f = &ctx->decoded_fmt;
  87. if (*num_planes) {
  88. if (*num_planes != f->fmt.pix_mp.num_planes)
  89. return -EINVAL;
  90. for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
  91. if (sizes[i] < f->fmt.pix_mp.plane_fmt[i].sizeimage)
  92. return -EINVAL;
  93. }
  94. } else {
  95. *num_planes = f->fmt.pix_mp.num_planes;
  96. for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
  97. sizes[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
  98. }
  99. return 0;
  100. }
  101. static int tegra_buf_out_validate(struct vb2_buffer *vb)
  102. {
  103. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  104. vbuf->field = V4L2_FIELD_NONE;
  105. return 0;
  106. }
  107. static void __tegra_buf_cleanup(struct vb2_buffer *vb, unsigned int i)
  108. {
  109. struct vb2_queue *vq = vb->vb2_queue;
  110. struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
  111. struct tegra_m2m_buffer *tb = vb_to_tegra_buf(vb);
  112. while (i--) {
  113. if (tb->a[i]) {
  114. tegra_vde_dmabuf_cache_unmap(ctx->vde, tb->a[i], true);
  115. tb->a[i] = NULL;
  116. }
  117. if (tb->iova[i]) {
  118. tegra_vde_iommu_unmap(ctx->vde, tb->iova[i]);
  119. tb->iova[i] = NULL;
  120. }
  121. }
  122. if (tb->aux) {
  123. tegra_vde_free_bo(tb->aux);
  124. tb->aux = NULL;
  125. }
  126. }
  127. static int tegra_buf_init(struct vb2_buffer *vb)
  128. {
  129. struct vb2_queue *vq = vb->vb2_queue;
  130. struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
  131. struct tegra_m2m_buffer *tb = vb_to_tegra_buf(vb);
  132. struct tegra_vde *vde = ctx->vde;
  133. enum dma_data_direction dma_dir;
  134. struct sg_table *sgt;
  135. unsigned int i;
  136. int err;
  137. if (V4L2_TYPE_IS_CAPTURE(vq->type) && vb->num_planes > 1) {
  138. /*
  139. * Tegra decoder writes auxiliary data for I/P frames.
  140. * This data is needed for decoding of B frames.
  141. */
  142. err = tegra_vde_alloc_bo(vde, &tb->aux, DMA_FROM_DEVICE,
  143. vb2_plane_size(vb, 1));
  144. if (err)
  145. return err;
  146. }
  147. if (V4L2_TYPE_IS_OUTPUT(vq->type))
  148. dma_dir = DMA_TO_DEVICE;
  149. else
  150. dma_dir = DMA_FROM_DEVICE;
  151. for (i = 0; i < vb->num_planes; i++) {
  152. if (vq->memory == VB2_MEMORY_DMABUF) {
  153. get_dma_buf(vb->planes[i].dbuf);
  154. err = tegra_vde_dmabuf_cache_map(vde, vb->planes[i].dbuf,
  155. dma_dir, &tb->a[i],
  156. &tb->dma_base[i]);
  157. if (err) {
  158. dma_buf_put(vb->planes[i].dbuf);
  159. goto cleanup;
  160. }
  161. continue;
  162. }
  163. if (vde->domain) {
  164. sgt = vb2_dma_sg_plane_desc(vb, i);
  165. err = tegra_vde_iommu_map(vde, sgt, &tb->iova[i],
  166. vb2_plane_size(vb, i));
  167. if (err)
  168. goto cleanup;
  169. tb->dma_base[i] = iova_dma_addr(&vde->iova, tb->iova[i]);
  170. } else {
  171. tb->dma_base[i] = vb2_dma_contig_plane_dma_addr(vb, i);
  172. }
  173. }
  174. return 0;
  175. cleanup:
  176. __tegra_buf_cleanup(vb, i);
  177. return err;
  178. }
  179. static void tegra_buf_cleanup(struct vb2_buffer *vb)
  180. {
  181. __tegra_buf_cleanup(vb, vb->num_planes);
  182. }
  183. static int tegra_buf_prepare(struct vb2_buffer *vb)
  184. {
  185. struct vb2_queue *vq = vb->vb2_queue;
  186. struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
  187. struct tegra_m2m_buffer *tb = vb_to_tegra_buf(vb);
  188. size_t hw_align, hw_size, hw_payload, size, offset;
  189. struct v4l2_pix_format_mplane *pixfmt;
  190. unsigned int i;
  191. void *vb_data;
  192. if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
  193. hw_align = BSEV_ALIGN;
  194. pixfmt = &ctx->coded_fmt.fmt.pix_mp;
  195. } else {
  196. hw_align = FRAMEID_ALIGN;
  197. pixfmt = &ctx->decoded_fmt.fmt.pix_mp;
  198. }
  199. for (i = 0; i < vb->num_planes; i++) {
  200. offset = vb->planes[i].data_offset;
  201. if (offset & (hw_align - 1))
  202. return -EINVAL;
  203. if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
  204. size = pixfmt->plane_fmt[i].sizeimage;
  205. hw_payload = ALIGN(size, VDE_ATOM);
  206. } else {
  207. size = vb2_get_plane_payload(vb, i) - offset;
  208. hw_payload = ALIGN(size + VDE_ATOM, SXE_BUFFER);
  209. }
  210. hw_size = offset + hw_payload;
  211. if (vb2_plane_size(vb, i) < hw_size)
  212. return -EINVAL;
  213. vb2_set_plane_payload(vb, i, hw_payload);
  214. if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
  215. vb_data = vb2_plane_vaddr(vb, i);
  216. /*
  217. * Hardware requires zero-padding of coded data.
  218. * Otherwise it will fail to parse the trailing
  219. * data and abort the decoding.
  220. */
  221. if (vb_data)
  222. memset(vb_data + offset + size, 0,
  223. hw_size - offset - size);
  224. }
  225. tb->dma_addr[i] = tb->dma_base[i] + offset;
  226. }
  227. switch (pixfmt->pixelformat) {
  228. case V4L2_PIX_FMT_YVU420M:
  229. swap(tb->dma_addr[1], tb->dma_addr[2]);
  230. break;
  231. }
  232. return 0;
  233. }
  234. static void tegra_buf_queue(struct vb2_buffer *vb)
  235. {
  236. struct tegra_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  237. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  238. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  239. }
  240. static void tegra_buf_request_complete(struct vb2_buffer *vb)
  241. {
  242. struct tegra_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  243. v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
  244. }
  245. static int tegra_start_streaming(struct vb2_queue *vq, unsigned int count)
  246. {
  247. return 0;
  248. }
  249. static void tegra_stop_streaming(struct vb2_queue *vq)
  250. {
  251. struct tegra_ctx *ctx = vb2_get_drv_priv(vq);
  252. while (true) {
  253. struct vb2_v4l2_buffer *vbuf;
  254. if (V4L2_TYPE_IS_OUTPUT(vq->type))
  255. vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  256. else
  257. vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  258. if (!vbuf)
  259. break;
  260. v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, &ctx->hdl);
  261. v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
  262. }
  263. }
  264. static const struct vb2_ops tegra_qops = {
  265. .queue_setup = tegra_queue_setup,
  266. .buf_init = tegra_buf_init,
  267. .buf_cleanup = tegra_buf_cleanup,
  268. .buf_prepare = tegra_buf_prepare,
  269. .buf_queue = tegra_buf_queue,
  270. .buf_out_validate = tegra_buf_out_validate,
  271. .buf_request_complete = tegra_buf_request_complete,
  272. .start_streaming = tegra_start_streaming,
  273. .stop_streaming = tegra_stop_streaming,
  274. };
  275. static int tegra_queue_init(void *priv,
  276. struct vb2_queue *src_vq,
  277. struct vb2_queue *dst_vq)
  278. {
  279. struct tegra_ctx *ctx = priv;
  280. struct tegra_vde *vde = ctx->vde;
  281. const struct vb2_mem_ops *mem_ops;
  282. unsigned long dma_attrs;
  283. int err;
  284. /*
  285. * TODO: Switch to use of vb2_dma_contig_memops uniformly once we
  286. * will add IOMMU_DOMAIN support for video decoder to tegra-smmu
  287. * driver. For now we need to stick with SG ops in order to be able
  288. * to get SGT table easily. This is suboptimal since SG mappings are
  289. * wasting CPU cache and we don't need that caching.
  290. */
  291. if (vde->domain)
  292. mem_ops = &vb2_dma_sg_memops;
  293. else
  294. mem_ops = &vb2_dma_contig_memops;
  295. dma_attrs = DMA_ATTR_WRITE_COMBINE;
  296. src_vq->buf_struct_size = sizeof(struct tegra_m2m_buffer);
  297. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  298. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  299. src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  300. src_vq->supports_requests = true;
  301. src_vq->requires_requests = true;
  302. src_vq->lock = &vde->v4l2_lock;
  303. src_vq->dma_attrs = dma_attrs;
  304. src_vq->mem_ops = mem_ops;
  305. src_vq->ops = &tegra_qops;
  306. src_vq->drv_priv = ctx;
  307. src_vq->dev = vde->dev;
  308. err = vb2_queue_init(src_vq);
  309. if (err) {
  310. v4l2_err(&vde->v4l2_dev,
  311. "failed to initialize src queue: %d\n", err);
  312. return err;
  313. }
  314. /*
  315. * We may need to zero the end of bitstream in kernel if userspace
  316. * doesn't do that, hence kmap is needed for the coded data. It's not
  317. * needed for framebuffers.
  318. */
  319. dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
  320. dst_vq->buf_struct_size = sizeof(struct tegra_m2m_buffer);
  321. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  322. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  323. dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
  324. dst_vq->lock = &vde->v4l2_lock;
  325. dst_vq->dma_attrs = dma_attrs;
  326. dst_vq->mem_ops = mem_ops;
  327. dst_vq->ops = &tegra_qops;
  328. dst_vq->drv_priv = ctx;
  329. dst_vq->dev = vde->dev;
  330. err = vb2_queue_init(dst_vq);
  331. if (err) {
  332. v4l2_err(&vde->v4l2_dev,
  333. "failed to initialize dst queue: %d\n", err);
  334. return err;
  335. }
  336. return 0;
  337. }
  338. static void tegra_reset_fmt(struct tegra_ctx *ctx, struct v4l2_format *f,
  339. u32 fourcc)
  340. {
  341. memset(f, 0, sizeof(*f));
  342. f->fmt.pix_mp.pixelformat = fourcc;
  343. f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  344. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  345. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  346. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
  347. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  348. }
  349. static void tegra_reset_coded_fmt(struct tegra_ctx *ctx)
  350. {
  351. const struct tegra_vde_soc *soc = ctx->vde->soc;
  352. struct v4l2_format *f = &ctx->coded_fmt;
  353. ctx->coded_fmt_desc = &soc->coded_fmts[0];
  354. tegra_reset_fmt(ctx, f, ctx->coded_fmt_desc->fourcc);
  355. f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  356. f->fmt.pix_mp.width = ctx->coded_fmt_desc->frmsize.min_width;
  357. f->fmt.pix_mp.height = ctx->coded_fmt_desc->frmsize.min_height;
  358. }
  359. static void tegra_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt,
  360. u32 pixelformat, u32 width, u32 height)
  361. {
  362. const struct v4l2_format_info *info = v4l2_format_info(pixelformat);
  363. struct v4l2_plane_pix_format *plane;
  364. unsigned int i;
  365. switch (pixelformat) {
  366. case V4L2_PIX_FMT_YUV420M:
  367. case V4L2_PIX_FMT_YVU420M:
  368. pixfmt->width = width;
  369. pixfmt->height = height;
  370. pixfmt->pixelformat = pixelformat;
  371. pixfmt->num_planes = info->mem_planes;
  372. for (i = 0; i < pixfmt->num_planes; i++) {
  373. unsigned int hdiv = (i == 0) ? 1 : 2;
  374. unsigned int vdiv = (i == 0) ? 1 : 2;
  375. /*
  376. * VDE is connected to Graphics Memory using 128bit port,
  377. * all memory accesses are made using 16B atoms.
  378. *
  379. * V4L requires Cb/Cr strides to be exactly half of the
  380. * Y stride, hence we're aligning Y to 16B x 2.
  381. */
  382. plane = &pixfmt->plane_fmt[i];
  383. plane->bytesperline = ALIGN(width, VDE_ATOM * 2) / hdiv;
  384. plane->sizeimage = plane->bytesperline * height / vdiv;
  385. }
  386. break;
  387. }
  388. }
  389. static void tegra_reset_decoded_fmt(struct tegra_ctx *ctx)
  390. {
  391. struct v4l2_format *f = &ctx->decoded_fmt;
  392. tegra_reset_fmt(ctx, f, ctx->coded_fmt_desc->decoded_fmts[0]);
  393. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  394. tegra_fill_pixfmt_mp(&f->fmt.pix_mp,
  395. ctx->coded_fmt_desc->decoded_fmts[0],
  396. ctx->coded_fmt.fmt.pix_mp.width,
  397. ctx->coded_fmt.fmt.pix_mp.height);
  398. }
  399. static void tegra_job_finish(struct tegra_ctx *ctx,
  400. enum vb2_buffer_state result)
  401. {
  402. v4l2_m2m_buf_done_and_job_finish(ctx->vde->m2m, ctx->fh.m2m_ctx,
  403. result);
  404. }
  405. static void tegra_decode_complete(struct work_struct *work)
  406. {
  407. struct tegra_ctx *ctx = container_of(work, struct tegra_ctx, work);
  408. int err;
  409. err = ctx->coded_fmt_desc->decode_wait(ctx);
  410. if (err)
  411. tegra_job_finish(ctx, VB2_BUF_STATE_ERROR);
  412. else
  413. tegra_job_finish(ctx, VB2_BUF_STATE_DONE);
  414. }
  415. static int tegra_querycap(struct file *file, void *priv,
  416. struct v4l2_capability *cap)
  417. {
  418. strscpy(cap->bus_info, "platform:tegra-vde", sizeof(cap->bus_info));
  419. strscpy(cap->driver, "tegra-vde", sizeof(cap->driver));
  420. strscpy(cap->card, "tegra-vde", sizeof(cap->card));
  421. return 0;
  422. }
  423. static int tegra_enum_decoded_fmt(struct file *file, void *priv,
  424. struct v4l2_fmtdesc *f)
  425. {
  426. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  427. if (WARN_ON(!ctx->coded_fmt_desc))
  428. return -EINVAL;
  429. if (f->index >= ctx->coded_fmt_desc->num_decoded_fmts)
  430. return -EINVAL;
  431. f->pixelformat = ctx->coded_fmt_desc->decoded_fmts[f->index];
  432. return 0;
  433. }
  434. static int tegra_g_decoded_fmt(struct file *file, void *priv,
  435. struct v4l2_format *f)
  436. {
  437. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  438. *f = ctx->decoded_fmt;
  439. return 0;
  440. }
  441. static int tegra_try_decoded_fmt(struct file *file, void *priv,
  442. struct v4l2_format *f)
  443. {
  444. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  445. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  446. const struct tegra_coded_fmt_desc *coded_desc;
  447. unsigned int i;
  448. /*
  449. * The codec context should point to a coded format desc, if the format
  450. * on the coded end has not been set yet, it should point to the
  451. * default value.
  452. */
  453. coded_desc = ctx->coded_fmt_desc;
  454. if (WARN_ON(!coded_desc))
  455. return -EINVAL;
  456. if (!coded_desc->num_decoded_fmts)
  457. return -EINVAL;
  458. for (i = 0; i < coded_desc->num_decoded_fmts; i++) {
  459. if (coded_desc->decoded_fmts[i] == pix_mp->pixelformat)
  460. break;
  461. }
  462. if (i == coded_desc->num_decoded_fmts)
  463. pix_mp->pixelformat = coded_desc->decoded_fmts[0];
  464. /* always apply the frmsize constraint of the coded end */
  465. v4l2_apply_frmsize_constraints(&pix_mp->width,
  466. &pix_mp->height,
  467. &coded_desc->frmsize);
  468. tegra_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat,
  469. pix_mp->width, pix_mp->height);
  470. pix_mp->field = V4L2_FIELD_NONE;
  471. return 0;
  472. }
  473. static int tegra_s_decoded_fmt(struct file *file, void *priv,
  474. struct v4l2_format *f)
  475. {
  476. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  477. struct vb2_queue *vq;
  478. int err;
  479. /* change not allowed if queue is busy */
  480. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  481. V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  482. if (vb2_is_busy(vq))
  483. return -EBUSY;
  484. err = tegra_try_decoded_fmt(file, priv, f);
  485. if (err)
  486. return err;
  487. ctx->decoded_fmt = *f;
  488. return 0;
  489. }
  490. static int tegra_enum_coded_fmt(struct file *file, void *priv,
  491. struct v4l2_fmtdesc *f)
  492. {
  493. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  494. const struct tegra_vde_soc *soc = ctx->vde->soc;
  495. if (f->index >= soc->num_coded_fmts)
  496. return -EINVAL;
  497. f->pixelformat = soc->coded_fmts[f->index].fourcc;
  498. return 0;
  499. }
  500. static int tegra_g_coded_fmt(struct file *file, void *priv,
  501. struct v4l2_format *f)
  502. {
  503. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  504. *f = ctx->coded_fmt;
  505. return 0;
  506. }
  507. static const struct tegra_coded_fmt_desc *
  508. tegra_find_coded_fmt_desc(struct tegra_ctx *ctx, u32 fourcc)
  509. {
  510. const struct tegra_vde_soc *soc = ctx->vde->soc;
  511. unsigned int i;
  512. for (i = 0; i < soc->num_coded_fmts; i++) {
  513. if (soc->coded_fmts[i].fourcc == fourcc)
  514. return &soc->coded_fmts[i];
  515. }
  516. return NULL;
  517. }
  518. static int tegra_try_coded_fmt(struct file *file, void *priv,
  519. struct v4l2_format *f)
  520. {
  521. struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  522. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  523. const struct tegra_vde_soc *soc = ctx->vde->soc;
  524. int size = pix_mp->plane_fmt[0].sizeimage;
  525. const struct tegra_coded_fmt_desc *desc;
  526. desc = tegra_find_coded_fmt_desc(ctx, pix_mp->pixelformat);
  527. if (!desc) {
  528. pix_mp->pixelformat = soc->coded_fmts[0].fourcc;
  529. desc = &soc->coded_fmts[0];
  530. }
  531. v4l2_apply_frmsize_constraints(&pix_mp->width,
  532. &pix_mp->height,
  533. &desc->frmsize);
  534. pix_mp->plane_fmt[0].sizeimage = max(ALIGN(size, SXE_BUFFER), SZ_2M);
  535. pix_mp->field = V4L2_FIELD_NONE;
  536. pix_mp->num_planes = 1;
  537. return 0;
  538. }
  539. static int tegra_s_coded_fmt(struct file *file, void *priv,
  540. struct v4l2_format *f)
  541. {
  542. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  543. struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
  544. const struct tegra_coded_fmt_desc *desc;
  545. struct vb2_queue *peer_vq, *vq;
  546. struct v4l2_format *cap_fmt;
  547. int err;
  548. /*
  549. * In order to support dynamic resolution change, the decoder admits
  550. * a resolution change, as long as the pixelformat remains. Can't be
  551. * done if streaming.
  552. */
  553. vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  554. if (vb2_is_streaming(vq) ||
  555. (vb2_is_busy(vq) &&
  556. f->fmt.pix_mp.pixelformat != ctx->coded_fmt.fmt.pix_mp.pixelformat))
  557. return -EBUSY;
  558. /*
  559. * Since format change on the OUTPUT queue will reset the CAPTURE
  560. * queue, we can't allow doing so when the CAPTURE queue has buffers
  561. * allocated.
  562. */
  563. peer_vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  564. if (vb2_is_busy(peer_vq))
  565. return -EBUSY;
  566. err = tegra_try_coded_fmt(file, priv, f);
  567. if (err)
  568. return err;
  569. desc = tegra_find_coded_fmt_desc(ctx, f->fmt.pix_mp.pixelformat);
  570. if (!desc)
  571. return -EINVAL;
  572. ctx->coded_fmt_desc = desc;
  573. ctx->coded_fmt = *f;
  574. /*
  575. * Current decoded format might have become invalid with newly
  576. * selected codec, so reset it to default just to be safe and
  577. * keep internal driver state sane. User is mandated to set
  578. * the decoded format again after we return, so we don't need
  579. * anything smarter.
  580. *
  581. * Note that this will propagates any size changes to the decoded format.
  582. */
  583. tegra_reset_decoded_fmt(ctx);
  584. /* propagate colorspace information to capture */
  585. cap_fmt = &ctx->decoded_fmt;
  586. cap_fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
  587. cap_fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  588. cap_fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
  589. cap_fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
  590. return 0;
  591. }
  592. static int tegra_enum_framesizes(struct file *file, void *priv,
  593. struct v4l2_frmsizeenum *fsize)
  594. {
  595. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  596. const struct tegra_coded_fmt_desc *fmt;
  597. if (fsize->index)
  598. return -EINVAL;
  599. fmt = tegra_find_coded_fmt_desc(ctx, fsize->pixel_format);
  600. if (!fmt)
  601. return -EINVAL;
  602. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  603. fsize->stepwise = fmt->frmsize;
  604. return 0;
  605. }
  606. static const struct v4l2_ioctl_ops tegra_v4l2_ioctl_ops = {
  607. .vidioc_querycap = tegra_querycap,
  608. .vidioc_enum_framesizes = tegra_enum_framesizes,
  609. .vidioc_try_fmt_vid_out_mplane = tegra_try_coded_fmt,
  610. .vidioc_g_fmt_vid_out_mplane = tegra_g_coded_fmt,
  611. .vidioc_s_fmt_vid_out_mplane = tegra_s_coded_fmt,
  612. .vidioc_enum_fmt_vid_out = tegra_enum_coded_fmt,
  613. .vidioc_try_fmt_vid_cap_mplane = tegra_try_decoded_fmt,
  614. .vidioc_g_fmt_vid_cap_mplane = tegra_g_decoded_fmt,
  615. .vidioc_s_fmt_vid_cap_mplane = tegra_s_decoded_fmt,
  616. .vidioc_enum_fmt_vid_cap = tegra_enum_decoded_fmt,
  617. .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  618. .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  619. .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  620. .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  621. .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  622. .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  623. .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  624. .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  625. .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  626. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  627. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  628. };
  629. static int tegra_init_ctrls(struct tegra_ctx *ctx)
  630. {
  631. unsigned int i;
  632. int err;
  633. err = v4l2_ctrl_handler_init(&ctx->hdl, ARRAY_SIZE(ctrl_cfgs));
  634. if (err)
  635. return err;
  636. for (i = 0; i < ARRAY_SIZE(ctrl_cfgs); i++) {
  637. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->hdl, &ctrl_cfgs[i],
  638. NULL);
  639. if (ctx->hdl.error) {
  640. err = ctx->hdl.error;
  641. goto free_ctrls;
  642. }
  643. }
  644. err = v4l2_ctrl_handler_setup(&ctx->hdl);
  645. if (err)
  646. goto free_ctrls;
  647. ctx->fh.ctrl_handler = &ctx->hdl;
  648. return 0;
  649. free_ctrls:
  650. v4l2_ctrl_handler_free(&ctx->hdl);
  651. return err;
  652. }
  653. static int tegra_init_m2m(struct tegra_ctx *ctx)
  654. {
  655. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(ctx->vde->m2m,
  656. ctx, tegra_queue_init);
  657. if (IS_ERR(ctx->fh.m2m_ctx))
  658. return PTR_ERR(ctx->fh.m2m_ctx);
  659. return 0;
  660. }
  661. static int tegra_open(struct file *file)
  662. {
  663. struct tegra_vde *vde = video_drvdata(file);
  664. struct tegra_ctx *ctx;
  665. int err;
  666. ctx = kzalloc_flex(*ctx, ctrls, ARRAY_SIZE(ctrl_cfgs));
  667. if (!ctx)
  668. return -ENOMEM;
  669. ctx->vde = vde;
  670. v4l2_fh_init(&ctx->fh, video_devdata(file));
  671. INIT_WORK(&ctx->work, tegra_decode_complete);
  672. err = tegra_init_ctrls(ctx);
  673. if (err) {
  674. v4l2_err(&vde->v4l2_dev, "failed to add controls: %d\n", err);
  675. goto free_ctx;
  676. }
  677. err = tegra_init_m2m(ctx);
  678. if (err) {
  679. v4l2_err(&vde->v4l2_dev, "failed to initialize m2m: %d\n", err);
  680. goto free_ctrls;
  681. }
  682. v4l2_fh_add(&ctx->fh, file);
  683. tegra_reset_coded_fmt(ctx);
  684. tegra_try_coded_fmt(file, &ctx->fh, &ctx->coded_fmt);
  685. tegra_reset_decoded_fmt(ctx);
  686. tegra_try_decoded_fmt(file, &ctx->fh, &ctx->decoded_fmt);
  687. return 0;
  688. free_ctrls:
  689. v4l2_ctrl_handler_free(&ctx->hdl);
  690. free_ctx:
  691. kfree(ctx);
  692. return err;
  693. }
  694. static int tegra_release(struct file *file)
  695. {
  696. struct tegra_ctx *ctx = file_to_tegra_ctx(file);
  697. struct v4l2_fh *fh = file_to_v4l2_fh(file);
  698. struct tegra_vde *vde = ctx->vde;
  699. v4l2_fh_del(fh, file);
  700. v4l2_m2m_ctx_release(fh->m2m_ctx);
  701. v4l2_ctrl_handler_free(&ctx->hdl);
  702. v4l2_fh_exit(fh);
  703. kfree(ctx);
  704. tegra_vde_dmabuf_cache_unmap_sync(vde);
  705. return 0;
  706. }
  707. static const struct v4l2_file_operations tegra_v4l2_fops = {
  708. .owner = THIS_MODULE,
  709. .open = tegra_open,
  710. .poll = v4l2_m2m_fop_poll,
  711. .mmap = v4l2_m2m_fop_mmap,
  712. .release = tegra_release,
  713. .unlocked_ioctl = video_ioctl2,
  714. };
  715. static void tegra_device_run(void *priv)
  716. {
  717. struct tegra_ctx *ctx = priv;
  718. struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  719. struct media_request *src_req = src->vb2_buf.req_obj.req;
  720. int err;
  721. v4l2_ctrl_request_setup(src_req, &ctx->hdl);
  722. err = ctx->coded_fmt_desc->decode_run(ctx);
  723. v4l2_ctrl_request_complete(src_req, &ctx->hdl);
  724. if (err)
  725. tegra_job_finish(ctx, VB2_BUF_STATE_ERROR);
  726. else
  727. queue_work(ctx->vde->wq, &ctx->work);
  728. }
  729. static const struct v4l2_m2m_ops tegra_v4l2_m2m_ops = {
  730. .device_run = tegra_device_run,
  731. };
  732. static int tegra_request_validate(struct media_request *req)
  733. {
  734. unsigned int count;
  735. count = vb2_request_buffer_cnt(req);
  736. if (!count)
  737. return -ENOENT;
  738. else if (count > 1)
  739. return -EINVAL;
  740. return vb2_request_validate(req);
  741. }
  742. static const struct media_device_ops tegra_media_device_ops = {
  743. .req_validate = tegra_request_validate,
  744. .req_queue = v4l2_m2m_request_queue,
  745. };
  746. int tegra_vde_v4l2_init(struct tegra_vde *vde)
  747. {
  748. struct device *dev = vde->dev;
  749. int err;
  750. mutex_init(&vde->v4l2_lock);
  751. media_device_init(&vde->mdev);
  752. video_set_drvdata(&vde->vdev, vde);
  753. vde->vdev.lock = &vde->v4l2_lock;
  754. vde->vdev.fops = &tegra_v4l2_fops;
  755. vde->vdev.vfl_dir = VFL_DIR_M2M;
  756. vde->vdev.release = video_device_release_empty;
  757. vde->vdev.v4l2_dev = &vde->v4l2_dev;
  758. vde->vdev.ioctl_ops = &tegra_v4l2_ioctl_ops;
  759. vde->vdev.device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  760. vde->v4l2_dev.mdev = &vde->mdev;
  761. vde->mdev.ops = &tegra_media_device_ops;
  762. vde->mdev.dev = dev;
  763. strscpy(vde->mdev.model, "tegra-vde", sizeof(vde->mdev.model));
  764. strscpy(vde->vdev.name, "tegra-vde", sizeof(vde->vdev.name));
  765. strscpy(vde->mdev.bus_info, "platform:tegra-vde",
  766. sizeof(vde->mdev.bus_info));
  767. vde->wq = create_workqueue("tegra-vde");
  768. if (!vde->wq)
  769. return -ENOMEM;
  770. err = media_device_register(&vde->mdev);
  771. if (err) {
  772. dev_err(dev, "failed to register media device: %d\n", err);
  773. goto clean_up_media_device;
  774. }
  775. err = v4l2_device_register(dev, &vde->v4l2_dev);
  776. if (err) {
  777. dev_err(dev, "failed to register v4l2 device: %d\n", err);
  778. goto unreg_media_device;
  779. }
  780. err = video_register_device(&vde->vdev, VFL_TYPE_VIDEO, -1);
  781. if (err) {
  782. dev_err(dev, "failed to register video device: %d\n", err);
  783. goto unreg_v4l2;
  784. }
  785. vde->m2m = v4l2_m2m_init(&tegra_v4l2_m2m_ops);
  786. err = PTR_ERR_OR_ZERO(vde->m2m);
  787. if (err) {
  788. dev_err(dev, "failed to initialize m2m device: %d\n", err);
  789. goto unreg_video_device;
  790. }
  791. err = v4l2_m2m_register_media_controller(vde->m2m, &vde->vdev,
  792. MEDIA_ENT_F_PROC_VIDEO_DECODER);
  793. if (err) {
  794. dev_err(dev, "failed to register media controller: %d\n", err);
  795. goto release_m2m;
  796. }
  797. v4l2_info(&vde->v4l2_dev, "v4l2 device registered as /dev/video%d\n",
  798. vde->vdev.num);
  799. return 0;
  800. release_m2m:
  801. v4l2_m2m_release(vde->m2m);
  802. unreg_video_device:
  803. video_unregister_device(&vde->vdev);
  804. unreg_v4l2:
  805. v4l2_device_unregister(&vde->v4l2_dev);
  806. unreg_media_device:
  807. media_device_unregister(&vde->mdev);
  808. clean_up_media_device:
  809. media_device_cleanup(&vde->mdev);
  810. destroy_workqueue(vde->wq);
  811. return err;
  812. }
  813. void tegra_vde_v4l2_deinit(struct tegra_vde *vde)
  814. {
  815. v4l2_m2m_unregister_media_controller(vde->m2m);
  816. v4l2_m2m_release(vde->m2m);
  817. video_unregister_device(&vde->vdev);
  818. v4l2_device_unregister(&vde->v4l2_dev);
  819. media_device_unregister(&vde->mdev);
  820. media_device_cleanup(&vde->mdev);
  821. destroy_workqueue(vde->wq);
  822. }