s5p_mfc_dec.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
  4. *
  5. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. * Kamil Debski, <k.debski@samsung.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/workqueue.h>
  18. #include <media/v4l2-ctrls.h>
  19. #include <media/v4l2-event.h>
  20. #include <media/videobuf2-v4l2.h>
  21. #include "s5p_mfc_common.h"
  22. #include "s5p_mfc_ctrl.h"
  23. #include "s5p_mfc_debug.h"
  24. #include "s5p_mfc_dec.h"
  25. #include "s5p_mfc_intr.h"
  26. #include "s5p_mfc_opr.h"
  27. #include "s5p_mfc_pm.h"
  28. static struct s5p_mfc_fmt formats[] = {
  29. {
  30. .fourcc = V4L2_PIX_FMT_NV12MT_16X16,
  31. .codec_mode = S5P_MFC_CODEC_NONE,
  32. .type = MFC_FMT_RAW,
  33. .num_planes = 2,
  34. .versions = MFC_V6_BIT | MFC_V7_BIT,
  35. },
  36. {
  37. .fourcc = V4L2_PIX_FMT_NV12MT,
  38. .codec_mode = S5P_MFC_CODEC_NONE,
  39. .type = MFC_FMT_RAW,
  40. .num_planes = 2,
  41. .versions = MFC_V5_BIT,
  42. },
  43. {
  44. .fourcc = V4L2_PIX_FMT_NV12M,
  45. .codec_mode = S5P_MFC_CODEC_NONE,
  46. .type = MFC_FMT_RAW,
  47. .num_planes = 2,
  48. .versions = MFC_V6PLUS_BITS,
  49. },
  50. {
  51. .fourcc = V4L2_PIX_FMT_NV21M,
  52. .codec_mode = S5P_MFC_CODEC_NONE,
  53. .type = MFC_FMT_RAW,
  54. .num_planes = 2,
  55. .versions = MFC_V6PLUS_BITS,
  56. },
  57. {
  58. .fourcc = V4L2_PIX_FMT_H264,
  59. .codec_mode = S5P_MFC_CODEC_H264_DEC,
  60. .type = MFC_FMT_DEC,
  61. .num_planes = 1,
  62. .versions = MFC_V5PLUS_BITS,
  63. },
  64. {
  65. .fourcc = V4L2_PIX_FMT_H264_MVC,
  66. .codec_mode = S5P_MFC_CODEC_H264_MVC_DEC,
  67. .type = MFC_FMT_DEC,
  68. .num_planes = 1,
  69. .versions = MFC_V6PLUS_BITS,
  70. },
  71. {
  72. .fourcc = V4L2_PIX_FMT_H263,
  73. .codec_mode = S5P_MFC_CODEC_H263_DEC,
  74. .type = MFC_FMT_DEC,
  75. .num_planes = 1,
  76. .versions = MFC_V5PLUS_BITS,
  77. },
  78. {
  79. .fourcc = V4L2_PIX_FMT_MPEG1,
  80. .codec_mode = S5P_MFC_CODEC_MPEG2_DEC,
  81. .type = MFC_FMT_DEC,
  82. .num_planes = 1,
  83. .versions = MFC_V5PLUS_BITS,
  84. },
  85. {
  86. .fourcc = V4L2_PIX_FMT_MPEG2,
  87. .codec_mode = S5P_MFC_CODEC_MPEG2_DEC,
  88. .type = MFC_FMT_DEC,
  89. .num_planes = 1,
  90. .versions = MFC_V5PLUS_BITS,
  91. },
  92. {
  93. .fourcc = V4L2_PIX_FMT_MPEG4,
  94. .codec_mode = S5P_MFC_CODEC_MPEG4_DEC,
  95. .type = MFC_FMT_DEC,
  96. .num_planes = 1,
  97. .versions = MFC_V5PLUS_BITS,
  98. },
  99. {
  100. .fourcc = V4L2_PIX_FMT_XVID,
  101. .codec_mode = S5P_MFC_CODEC_MPEG4_DEC,
  102. .type = MFC_FMT_DEC,
  103. .num_planes = 1,
  104. .versions = MFC_V5PLUS_BITS,
  105. },
  106. {
  107. .fourcc = V4L2_PIX_FMT_VC1_ANNEX_G,
  108. .codec_mode = S5P_MFC_CODEC_VC1_DEC,
  109. .type = MFC_FMT_DEC,
  110. .num_planes = 1,
  111. .versions = MFC_V5PLUS_BITS,
  112. },
  113. {
  114. .fourcc = V4L2_PIX_FMT_VC1_ANNEX_L,
  115. .codec_mode = S5P_MFC_CODEC_VC1RCV_DEC,
  116. .type = MFC_FMT_DEC,
  117. .num_planes = 1,
  118. .versions = MFC_V5PLUS_BITS,
  119. },
  120. {
  121. .fourcc = V4L2_PIX_FMT_VP8,
  122. .codec_mode = S5P_MFC_CODEC_VP8_DEC,
  123. .type = MFC_FMT_DEC,
  124. .num_planes = 1,
  125. .versions = MFC_V6PLUS_BITS,
  126. },
  127. {
  128. .fourcc = V4L2_PIX_FMT_HEVC,
  129. .codec_mode = S5P_FIMV_CODEC_HEVC_DEC,
  130. .type = MFC_FMT_DEC,
  131. .num_planes = 1,
  132. .versions = MFC_V10_BIT,
  133. },
  134. {
  135. .fourcc = V4L2_PIX_FMT_VP9,
  136. .codec_mode = S5P_FIMV_CODEC_VP9_DEC,
  137. .type = MFC_FMT_DEC,
  138. .num_planes = 1,
  139. .versions = MFC_V10_BIT,
  140. },
  141. };
  142. #define NUM_FORMATS ARRAY_SIZE(formats)
  143. /* Find selected format description */
  144. static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
  145. {
  146. unsigned int i;
  147. for (i = 0; i < NUM_FORMATS; i++) {
  148. if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
  149. formats[i].type == t)
  150. return &formats[i];
  151. }
  152. return NULL;
  153. }
  154. static struct mfc_control controls[] = {
  155. {
  156. .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
  157. .type = V4L2_CTRL_TYPE_INTEGER,
  158. .name = "H264 Display Delay",
  159. .minimum = 0,
  160. .maximum = 16383,
  161. .step = 1,
  162. .default_value = 0,
  163. },
  164. {
  165. .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
  166. .type = V4L2_CTRL_TYPE_BOOLEAN,
  167. .name = "H264 Display Delay Enable",
  168. .minimum = 0,
  169. .maximum = 1,
  170. .step = 1,
  171. .default_value = 0,
  172. },
  173. {
  174. .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
  175. .type = V4L2_CTRL_TYPE_BOOLEAN,
  176. .name = "Mpeg4 Loop Filter Enable",
  177. .minimum = 0,
  178. .maximum = 1,
  179. .step = 1,
  180. .default_value = 0,
  181. },
  182. {
  183. .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
  184. .type = V4L2_CTRL_TYPE_BOOLEAN,
  185. .name = "Slice Interface Enable",
  186. .minimum = 0,
  187. .maximum = 1,
  188. .step = 1,
  189. .default_value = 0,
  190. },
  191. {
  192. .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
  193. .type = V4L2_CTRL_TYPE_INTEGER,
  194. .name = "Minimum number of cap bufs",
  195. .minimum = 1,
  196. .maximum = 32,
  197. .step = 1,
  198. .default_value = 1,
  199. .is_volatile = 1,
  200. },
  201. };
  202. #define NUM_CTRLS ARRAY_SIZE(controls)
  203. /* Check whether a context should be run on hardware */
  204. static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
  205. {
  206. /* Context is to parse header */
  207. if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
  208. return 1;
  209. /* Context is to decode a frame */
  210. if (ctx->src_queue_cnt >= 1 &&
  211. ctx->state == MFCINST_RUNNING &&
  212. ctx->dst_queue_cnt >= ctx->pb_count)
  213. return 1;
  214. /* Context is to return last frame */
  215. if (ctx->state == MFCINST_FINISHING &&
  216. ctx->dst_queue_cnt >= ctx->pb_count)
  217. return 1;
  218. /* Context is to set buffers */
  219. if (ctx->src_queue_cnt >= 1 &&
  220. ctx->state == MFCINST_HEAD_PARSED &&
  221. ctx->capture_state == QUEUE_BUFS_MMAPED)
  222. return 1;
  223. /* Resolution change */
  224. if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
  225. ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
  226. ctx->dst_queue_cnt >= ctx->pb_count)
  227. return 1;
  228. if (ctx->state == MFCINST_RES_CHANGE_END &&
  229. ctx->src_queue_cnt >= 1)
  230. return 1;
  231. mfc_debug(2, "ctx is not ready\n");
  232. return 0;
  233. }
  234. static const struct s5p_mfc_codec_ops decoder_codec_ops = {
  235. .pre_seq_start = NULL,
  236. .post_seq_start = NULL,
  237. .pre_frame_start = NULL,
  238. .post_frame_start = NULL,
  239. };
  240. /* Query capabilities of the device */
  241. static int vidioc_querycap(struct file *file, void *priv,
  242. struct v4l2_capability *cap)
  243. {
  244. struct s5p_mfc_dev *dev = video_drvdata(file);
  245. strscpy(cap->driver, S5P_MFC_NAME, sizeof(cap->driver));
  246. strscpy(cap->card, dev->vfd_dec->name, sizeof(cap->card));
  247. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  248. dev_name(&dev->plat_dev->dev));
  249. return 0;
  250. }
  251. /* Enumerate format */
  252. static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f,
  253. bool out)
  254. {
  255. struct s5p_mfc_dev *dev = video_drvdata(file);
  256. int i, j = 0;
  257. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  258. if (out && formats[i].type != MFC_FMT_DEC)
  259. continue;
  260. else if (!out && formats[i].type != MFC_FMT_RAW)
  261. continue;
  262. else if ((dev->variant->version_bit & formats[i].versions) == 0)
  263. continue;
  264. if (j == f->index)
  265. break;
  266. ++j;
  267. }
  268. if (i == ARRAY_SIZE(formats))
  269. return -EINVAL;
  270. f->pixelformat = formats[i].fourcc;
  271. return 0;
  272. }
  273. static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
  274. struct v4l2_fmtdesc *f)
  275. {
  276. return vidioc_enum_fmt(file, f, false);
  277. }
  278. static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
  279. struct v4l2_fmtdesc *f)
  280. {
  281. return vidioc_enum_fmt(file, f, true);
  282. }
  283. /* Get format */
  284. static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  285. {
  286. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  287. struct v4l2_pix_format_mplane *pix_mp;
  288. mfc_debug_enter();
  289. pix_mp = &f->fmt.pix_mp;
  290. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  291. (ctx->state == MFCINST_GOT_INST || ctx->state ==
  292. MFCINST_RES_CHANGE_END)) {
  293. /* If the MFC is parsing the header,
  294. * so wait until it is finished */
  295. s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_SEQ_DONE_RET,
  296. 0);
  297. }
  298. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  299. ctx->state >= MFCINST_HEAD_PARSED &&
  300. ctx->state < MFCINST_ABORT) {
  301. /* This is run on CAPTURE (decode output) */
  302. /* Width and height are set to the dimensions
  303. of the movie, the buffer is bigger and
  304. further processing stages should crop to this
  305. rectangle. */
  306. pix_mp->width = ctx->buf_width;
  307. pix_mp->height = ctx->buf_height;
  308. pix_mp->field = V4L2_FIELD_NONE;
  309. pix_mp->num_planes = 2;
  310. /* Set pixelformat to the format in which MFC
  311. outputs the decoded frame */
  312. pix_mp->pixelformat = ctx->dst_fmt->fourcc;
  313. pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  314. pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  315. pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  316. pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  317. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  318. /* This is run on OUTPUT
  319. The buffer contains compressed image
  320. so width and height have no meaning */
  321. pix_mp->width = 0;
  322. pix_mp->height = 0;
  323. pix_mp->field = V4L2_FIELD_NONE;
  324. pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
  325. pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
  326. pix_mp->pixelformat = ctx->src_fmt->fourcc;
  327. pix_mp->num_planes = ctx->src_fmt->num_planes;
  328. } else {
  329. mfc_err("Format could not be read\n");
  330. mfc_debug(2, "%s-- with error\n", __func__);
  331. return -EINVAL;
  332. }
  333. mfc_debug_leave();
  334. return 0;
  335. }
  336. /* Try format */
  337. static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  338. {
  339. struct s5p_mfc_dev *dev = video_drvdata(file);
  340. struct s5p_mfc_fmt *fmt;
  341. mfc_debug(2, "Type is %d\n", f->type);
  342. if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  343. fmt = find_format(f, MFC_FMT_DEC);
  344. if (!fmt) {
  345. mfc_err("Unsupported format for source.\n");
  346. return -EINVAL;
  347. }
  348. if (fmt->codec_mode == S5P_FIMV_CODEC_NONE) {
  349. mfc_err("Unknown codec\n");
  350. return -EINVAL;
  351. }
  352. if ((dev->variant->version_bit & fmt->versions) == 0) {
  353. mfc_err("Unsupported format by this MFC version.\n");
  354. return -EINVAL;
  355. }
  356. } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  357. fmt = find_format(f, MFC_FMT_RAW);
  358. if (!fmt) {
  359. mfc_err("Unsupported format for destination.\n");
  360. return -EINVAL;
  361. }
  362. if ((dev->variant->version_bit & fmt->versions) == 0) {
  363. mfc_err("Unsupported format by this MFC version.\n");
  364. return -EINVAL;
  365. }
  366. }
  367. return 0;
  368. }
  369. /* Set format */
  370. static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  371. {
  372. struct s5p_mfc_dev *dev = video_drvdata(file);
  373. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  374. int ret = 0;
  375. struct v4l2_pix_format_mplane *pix_mp;
  376. struct s5p_mfc_buf_size *buf_size = dev->variant->buf_size;
  377. mfc_debug_enter();
  378. ret = vidioc_try_fmt(file, priv, f);
  379. pix_mp = &f->fmt.pix_mp;
  380. if (ret)
  381. return ret;
  382. if (vb2_is_streaming(&ctx->vq_src) || vb2_is_streaming(&ctx->vq_dst)) {
  383. v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
  384. ret = -EBUSY;
  385. goto out;
  386. }
  387. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  388. /* dst_fmt is validated by call to vidioc_try_fmt */
  389. ctx->dst_fmt = find_format(f, MFC_FMT_RAW);
  390. ret = 0;
  391. goto out;
  392. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  393. /* src_fmt is validated by call to vidioc_try_fmt */
  394. ctx->src_fmt = find_format(f, MFC_FMT_DEC);
  395. ctx->codec_mode = ctx->src_fmt->codec_mode;
  396. mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
  397. pix_mp->height = 0;
  398. pix_mp->width = 0;
  399. if (pix_mp->plane_fmt[0].sizeimage == 0)
  400. pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
  401. DEF_CPB_SIZE;
  402. else if (pix_mp->plane_fmt[0].sizeimage > buf_size->cpb)
  403. ctx->dec_src_buf_size = buf_size->cpb;
  404. else
  405. ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
  406. pix_mp->plane_fmt[0].bytesperline = 0;
  407. ctx->state = MFCINST_INIT;
  408. ret = 0;
  409. goto out;
  410. } else {
  411. mfc_err("Wrong type error for S_FMT : %d", f->type);
  412. ret = -EINVAL;
  413. goto out;
  414. }
  415. out:
  416. mfc_debug_leave();
  417. return ret;
  418. }
  419. static int reqbufs_output(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
  420. struct v4l2_requestbuffers *reqbufs)
  421. {
  422. int ret = 0;
  423. s5p_mfc_clock_on();
  424. if (reqbufs->count == 0) {
  425. mfc_debug(2, "Freeing buffers\n");
  426. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  427. if (ret)
  428. goto out;
  429. ctx->src_bufs_cnt = 0;
  430. ctx->output_state = QUEUE_FREE;
  431. } else if (ctx->output_state == QUEUE_FREE) {
  432. /* Can only request buffers when we have a valid format set. */
  433. WARN_ON(ctx->src_bufs_cnt != 0);
  434. if (ctx->state != MFCINST_INIT) {
  435. mfc_err("Reqbufs called in an invalid state\n");
  436. ret = -EINVAL;
  437. goto out;
  438. }
  439. mfc_debug(2, "Allocating %d buffers for OUTPUT queue\n",
  440. reqbufs->count);
  441. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  442. if (ret)
  443. goto out;
  444. ret = s5p_mfc_open_mfc_inst(dev, ctx);
  445. if (ret) {
  446. reqbufs->count = 0;
  447. vb2_reqbufs(&ctx->vq_src, reqbufs);
  448. goto out;
  449. }
  450. ctx->output_state = QUEUE_BUFS_REQUESTED;
  451. } else {
  452. mfc_err("Buffers have already been requested\n");
  453. ret = -EINVAL;
  454. }
  455. out:
  456. s5p_mfc_clock_off();
  457. if (ret)
  458. mfc_err("Failed allocating buffers for OUTPUT queue\n");
  459. return ret;
  460. }
  461. static int reqbufs_capture(struct s5p_mfc_dev *dev, struct s5p_mfc_ctx *ctx,
  462. struct v4l2_requestbuffers *reqbufs)
  463. {
  464. int ret = 0;
  465. s5p_mfc_clock_on();
  466. if (reqbufs->count == 0) {
  467. mfc_debug(2, "Freeing buffers\n");
  468. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  469. if (ret)
  470. goto out;
  471. s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
  472. ctx->dst_bufs_cnt = 0;
  473. } else if (ctx->capture_state == QUEUE_FREE) {
  474. WARN_ON(ctx->dst_bufs_cnt != 0);
  475. mfc_debug(2, "Allocating %d buffers for CAPTURE queue\n",
  476. reqbufs->count);
  477. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  478. if (ret)
  479. goto out;
  480. ctx->capture_state = QUEUE_BUFS_REQUESTED;
  481. ctx->total_dpb_count = reqbufs->count;
  482. ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_codec_buffers, ctx);
  483. if (ret) {
  484. mfc_err("Failed to allocate decoding buffers\n");
  485. reqbufs->count = 0;
  486. vb2_reqbufs(&ctx->vq_dst, reqbufs);
  487. ret = -ENOMEM;
  488. ctx->capture_state = QUEUE_FREE;
  489. goto out;
  490. }
  491. WARN_ON(ctx->dst_bufs_cnt != ctx->total_dpb_count);
  492. ctx->capture_state = QUEUE_BUFS_MMAPED;
  493. if (s5p_mfc_ctx_ready(ctx))
  494. set_work_bit_irqsave(ctx);
  495. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  496. s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_INIT_BUFFERS_RET,
  497. 0);
  498. } else {
  499. mfc_err("Buffers have already been requested\n");
  500. ret = -EINVAL;
  501. }
  502. out:
  503. s5p_mfc_clock_off();
  504. if (ret)
  505. mfc_err("Failed allocating buffers for CAPTURE queue\n");
  506. return ret;
  507. }
  508. /* Request buffers */
  509. static int vidioc_reqbufs(struct file *file, void *priv,
  510. struct v4l2_requestbuffers *reqbufs)
  511. {
  512. struct s5p_mfc_dev *dev = video_drvdata(file);
  513. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  514. if (reqbufs->memory != V4L2_MEMORY_MMAP) {
  515. mfc_debug(2, "Only V4L2_MEMORY_MMAP is supported\n");
  516. return -EINVAL;
  517. }
  518. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  519. return reqbufs_output(dev, ctx, reqbufs);
  520. } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  521. return reqbufs_capture(dev, ctx, reqbufs);
  522. } else {
  523. mfc_err("Invalid type requested\n");
  524. return -EINVAL;
  525. }
  526. }
  527. /* Query buffer */
  528. static int vidioc_querybuf(struct file *file, void *priv,
  529. struct v4l2_buffer *buf)
  530. {
  531. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  532. int ret;
  533. int i;
  534. if (buf->memory != V4L2_MEMORY_MMAP) {
  535. mfc_err("Only mmapped buffers can be used\n");
  536. return -EINVAL;
  537. }
  538. mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
  539. if (ctx->state == MFCINST_GOT_INST &&
  540. buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  541. ret = vb2_querybuf(&ctx->vq_src, buf);
  542. } else if (ctx->state == MFCINST_RUNNING &&
  543. buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  544. ret = vb2_querybuf(&ctx->vq_dst, buf);
  545. for (i = 0; i < buf->length; i++)
  546. buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
  547. } else {
  548. mfc_err("vidioc_querybuf called in an inappropriate state\n");
  549. ret = -EINVAL;
  550. }
  551. mfc_debug_leave();
  552. return ret;
  553. }
  554. /* Queue a buffer */
  555. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  556. {
  557. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  558. if (ctx->state == MFCINST_ERROR) {
  559. mfc_err("Call on QBUF after unrecoverable error\n");
  560. return -EIO;
  561. }
  562. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  563. return vb2_qbuf(&ctx->vq_src, NULL, buf);
  564. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  565. return vb2_qbuf(&ctx->vq_dst, NULL, buf);
  566. return -EINVAL;
  567. }
  568. /* Dequeue a buffer */
  569. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  570. {
  571. const struct v4l2_event ev = {
  572. .type = V4L2_EVENT_EOS
  573. };
  574. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  575. int ret;
  576. if (ctx->state == MFCINST_ERROR) {
  577. mfc_err_limited("Call on DQBUF after unrecoverable error\n");
  578. return -EIO;
  579. }
  580. switch (buf->type) {
  581. case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  582. return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
  583. case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  584. ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
  585. if (ret)
  586. return ret;
  587. if (ctx->state == MFCINST_FINISHED &&
  588. (ctx->dst_bufs[buf->index].flags & MFC_BUF_FLAG_EOS))
  589. v4l2_event_queue_fh(&ctx->fh, &ev);
  590. return 0;
  591. default:
  592. return -EINVAL;
  593. }
  594. }
  595. /* Export DMA buffer */
  596. static int vidioc_expbuf(struct file *file, void *priv,
  597. struct v4l2_exportbuffer *eb)
  598. {
  599. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  600. if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  601. return vb2_expbuf(&ctx->vq_src, eb);
  602. if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  603. return vb2_expbuf(&ctx->vq_dst, eb);
  604. return -EINVAL;
  605. }
  606. /* Stream on */
  607. static int vidioc_streamon(struct file *file, void *priv,
  608. enum v4l2_buf_type type)
  609. {
  610. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  611. int ret = -EINVAL;
  612. mfc_debug_enter();
  613. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  614. ret = vb2_streamon(&ctx->vq_src, type);
  615. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  616. ret = vb2_streamon(&ctx->vq_dst, type);
  617. mfc_debug_leave();
  618. return ret;
  619. }
  620. /* Stream off, which equals to a pause */
  621. static int vidioc_streamoff(struct file *file, void *priv,
  622. enum v4l2_buf_type type)
  623. {
  624. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  625. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  626. return vb2_streamoff(&ctx->vq_src, type);
  627. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  628. return vb2_streamoff(&ctx->vq_dst, type);
  629. return -EINVAL;
  630. }
  631. /* Set controls - v4l2 control framework */
  632. static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
  633. {
  634. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  635. switch (ctrl->id) {
  636. case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
  637. ctx->display_delay = ctrl->val;
  638. break;
  639. case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
  640. ctx->display_delay_enable = ctrl->val;
  641. break;
  642. case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
  643. ctx->loop_filter_mpeg4 = ctrl->val;
  644. break;
  645. case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
  646. ctx->slice_interface = ctrl->val;
  647. break;
  648. default:
  649. mfc_err("Invalid control 0x%08x\n", ctrl->id);
  650. return -EINVAL;
  651. }
  652. return 0;
  653. }
  654. static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
  655. {
  656. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  657. struct s5p_mfc_dev *dev = ctx->dev;
  658. switch (ctrl->id) {
  659. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  660. if (ctx->state >= MFCINST_HEAD_PARSED &&
  661. ctx->state < MFCINST_ABORT) {
  662. ctrl->val = ctx->pb_count;
  663. break;
  664. } else if (ctx->state != MFCINST_INIT &&
  665. ctx->state != MFCINST_RES_CHANGE_END) {
  666. v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
  667. return -EINVAL;
  668. }
  669. /* Should wait for the header to be parsed */
  670. s5p_mfc_wait_for_done_ctx(ctx,
  671. S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
  672. if (ctx->state >= MFCINST_HEAD_PARSED &&
  673. ctx->state < MFCINST_ABORT) {
  674. ctrl->val = ctx->pb_count;
  675. } else {
  676. v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
  677. return -EINVAL;
  678. }
  679. break;
  680. }
  681. return 0;
  682. }
  683. static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
  684. .s_ctrl = s5p_mfc_dec_s_ctrl,
  685. .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
  686. };
  687. /* Get compose information */
  688. static int vidioc_g_selection(struct file *file, void *priv,
  689. struct v4l2_selection *s)
  690. {
  691. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  692. struct s5p_mfc_dev *dev = ctx->dev;
  693. u32 left, right, top, bottom;
  694. u32 width, height;
  695. if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  696. return -EINVAL;
  697. if (ctx->state != MFCINST_HEAD_PARSED &&
  698. ctx->state != MFCINST_RUNNING &&
  699. ctx->state != MFCINST_FINISHING &&
  700. ctx->state != MFCINST_FINISHED) {
  701. mfc_err("Can not get compose information\n");
  702. return -EINVAL;
  703. }
  704. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
  705. left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
  706. right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
  707. left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
  708. top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
  709. bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
  710. top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
  711. width = ctx->img_width - left - right;
  712. height = ctx->img_height - top - bottom;
  713. mfc_debug(2, "Composing info [h264]: l=%d t=%d w=%d h=%d (r=%d b=%d fw=%d fh=%d\n",
  714. left, top, s->r.width, s->r.height, right, bottom,
  715. ctx->buf_width, ctx->buf_height);
  716. } else {
  717. left = 0;
  718. top = 0;
  719. width = ctx->img_width;
  720. height = ctx->img_height;
  721. mfc_debug(2, "Composing info: w=%d h=%d fw=%d fh=%d\n",
  722. s->r.width, s->r.height, ctx->buf_width,
  723. ctx->buf_height);
  724. }
  725. switch (s->target) {
  726. case V4L2_SEL_TGT_COMPOSE:
  727. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  728. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  729. s->r.left = left;
  730. s->r.top = top;
  731. s->r.width = width;
  732. s->r.height = height;
  733. break;
  734. default:
  735. return -EINVAL;
  736. }
  737. return 0;
  738. }
  739. static int vidioc_decoder_cmd(struct file *file, void *priv,
  740. struct v4l2_decoder_cmd *cmd)
  741. {
  742. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  743. struct s5p_mfc_dev *dev = ctx->dev;
  744. struct s5p_mfc_buf *buf;
  745. unsigned long flags;
  746. switch (cmd->cmd) {
  747. case V4L2_DEC_CMD_STOP:
  748. if (cmd->flags != 0)
  749. return -EINVAL;
  750. if (!vb2_is_streaming(&ctx->vq_src))
  751. return -EINVAL;
  752. spin_lock_irqsave(&dev->irqlock, flags);
  753. if (list_empty(&ctx->src_queue)) {
  754. mfc_err("EOS: empty src queue, entering finishing state");
  755. ctx->state = MFCINST_FINISHING;
  756. if (s5p_mfc_ctx_ready(ctx))
  757. set_work_bit_irqsave(ctx);
  758. spin_unlock_irqrestore(&dev->irqlock, flags);
  759. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  760. } else {
  761. mfc_err("EOS: marking last buffer of stream");
  762. buf = list_entry(ctx->src_queue.prev,
  763. struct s5p_mfc_buf, list);
  764. if (buf->flags & MFC_BUF_FLAG_USED)
  765. ctx->state = MFCINST_FINISHING;
  766. else
  767. buf->flags |= MFC_BUF_FLAG_EOS;
  768. spin_unlock_irqrestore(&dev->irqlock, flags);
  769. }
  770. break;
  771. default:
  772. return -EINVAL;
  773. }
  774. return 0;
  775. }
  776. static int vidioc_subscribe_event(struct v4l2_fh *fh,
  777. const struct v4l2_event_subscription *sub)
  778. {
  779. switch (sub->type) {
  780. case V4L2_EVENT_EOS:
  781. return v4l2_event_subscribe(fh, sub, 2, NULL);
  782. case V4L2_EVENT_SOURCE_CHANGE:
  783. return v4l2_src_change_event_subscribe(fh, sub);
  784. default:
  785. return -EINVAL;
  786. }
  787. }
  788. /* v4l2_ioctl_ops */
  789. static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
  790. .vidioc_querycap = vidioc_querycap,
  791. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  792. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  793. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
  794. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
  795. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
  796. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
  797. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
  798. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
  799. .vidioc_reqbufs = vidioc_reqbufs,
  800. .vidioc_querybuf = vidioc_querybuf,
  801. .vidioc_qbuf = vidioc_qbuf,
  802. .vidioc_dqbuf = vidioc_dqbuf,
  803. .vidioc_expbuf = vidioc_expbuf,
  804. .vidioc_streamon = vidioc_streamon,
  805. .vidioc_streamoff = vidioc_streamoff,
  806. .vidioc_g_selection = vidioc_g_selection,
  807. .vidioc_decoder_cmd = vidioc_decoder_cmd,
  808. .vidioc_subscribe_event = vidioc_subscribe_event,
  809. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  810. };
  811. static int s5p_mfc_queue_setup(struct vb2_queue *vq,
  812. unsigned int *buf_count,
  813. unsigned int *plane_count, unsigned int psize[],
  814. struct device *alloc_devs[])
  815. {
  816. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  817. struct s5p_mfc_dev *dev = ctx->dev;
  818. /* Video output for decoding (source)
  819. * this can be set after getting an instance */
  820. if (ctx->state == MFCINST_INIT &&
  821. vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  822. /* A single plane is required for input */
  823. *plane_count = 1;
  824. if (*buf_count < 1)
  825. *buf_count = 1;
  826. if (*buf_count > MFC_MAX_BUFFERS)
  827. *buf_count = MFC_MAX_BUFFERS;
  828. /* Video capture for decoding (destination)
  829. * this can be set after the header was parsed */
  830. } else if (ctx->state == MFCINST_HEAD_PARSED &&
  831. vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  832. /* Output plane count is 2 - one for Y and one for CbCr */
  833. *plane_count = 2;
  834. /* Setup buffer count */
  835. if (*buf_count < ctx->pb_count)
  836. *buf_count = ctx->pb_count;
  837. if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB)
  838. *buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB;
  839. if (*buf_count > MFC_MAX_BUFFERS)
  840. *buf_count = MFC_MAX_BUFFERS;
  841. } else {
  842. mfc_err("State seems invalid. State = %d, vq->type = %d\n",
  843. ctx->state, vq->type);
  844. return -EINVAL;
  845. }
  846. mfc_debug(2, "Buffer count=%d, plane count=%d\n",
  847. *buf_count, *plane_count);
  848. if (ctx->state == MFCINST_HEAD_PARSED &&
  849. vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  850. psize[0] = ctx->luma_size;
  851. psize[1] = ctx->chroma_size;
  852. if (IS_MFCV6_PLUS(dev))
  853. alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
  854. else
  855. alloc_devs[0] = ctx->dev->mem_dev[BANK_R_CTX];
  856. alloc_devs[1] = ctx->dev->mem_dev[BANK_L_CTX];
  857. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  858. ctx->state == MFCINST_INIT) {
  859. psize[0] = ctx->dec_src_buf_size;
  860. alloc_devs[0] = ctx->dev->mem_dev[BANK_L_CTX];
  861. } else {
  862. mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
  863. return -EINVAL;
  864. }
  865. return 0;
  866. }
  867. static int s5p_mfc_buf_init(struct vb2_buffer *vb)
  868. {
  869. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  870. struct vb2_queue *vq = vb->vb2_queue;
  871. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  872. unsigned int i;
  873. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  874. if (ctx->capture_state == QUEUE_BUFS_MMAPED)
  875. return 0;
  876. for (i = 0; i < ctx->dst_fmt->num_planes; i++) {
  877. if (IS_ERR_OR_NULL(ERR_PTR(
  878. vb2_dma_contig_plane_dma_addr(vb, i)))) {
  879. mfc_err("Plane mem not allocated\n");
  880. return -EINVAL;
  881. }
  882. }
  883. if (vb2_plane_size(vb, 0) < ctx->luma_size ||
  884. vb2_plane_size(vb, 1) < ctx->chroma_size) {
  885. mfc_err("Plane buffer (CAPTURE) is too small\n");
  886. return -EINVAL;
  887. }
  888. i = vb->index;
  889. ctx->dst_bufs[i].b = vbuf;
  890. ctx->dst_bufs[i].cookie.raw.luma =
  891. vb2_dma_contig_plane_dma_addr(vb, 0);
  892. ctx->dst_bufs[i].cookie.raw.chroma =
  893. vb2_dma_contig_plane_dma_addr(vb, 1);
  894. ctx->dst_bufs_cnt++;
  895. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  896. if (IS_ERR_OR_NULL(ERR_PTR(
  897. vb2_dma_contig_plane_dma_addr(vb, 0)))) {
  898. mfc_err("Plane memory not allocated\n");
  899. return -EINVAL;
  900. }
  901. if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
  902. mfc_err("Plane buffer (OUTPUT) is too small\n");
  903. return -EINVAL;
  904. }
  905. i = vb->index;
  906. ctx->src_bufs[i].b = vbuf;
  907. ctx->src_bufs[i].cookie.stream =
  908. vb2_dma_contig_plane_dma_addr(vb, 0);
  909. ctx->src_bufs_cnt++;
  910. } else {
  911. mfc_err("s5p_mfc_buf_init: unknown queue type\n");
  912. return -EINVAL;
  913. }
  914. return 0;
  915. }
  916. static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
  917. {
  918. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  919. struct s5p_mfc_dev *dev = ctx->dev;
  920. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  921. if (ctx->state == MFCINST_FINISHING ||
  922. ctx->state == MFCINST_FINISHED)
  923. ctx->state = MFCINST_RUNNING;
  924. /* If context is ready then dev = work->data;schedule it to run */
  925. if (s5p_mfc_ctx_ready(ctx))
  926. set_work_bit_irqsave(ctx);
  927. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  928. return 0;
  929. }
  930. static void s5p_mfc_stop_streaming(struct vb2_queue *q)
  931. {
  932. unsigned long flags;
  933. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  934. struct s5p_mfc_dev *dev = ctx->dev;
  935. int aborted = 0;
  936. spin_lock_irqsave(&dev->irqlock, flags);
  937. if ((ctx->state == MFCINST_FINISHING ||
  938. ctx->state == MFCINST_RUNNING) &&
  939. dev->curr_ctx == ctx->num && dev->hw_lock) {
  940. ctx->state = MFCINST_ABORT;
  941. spin_unlock_irqrestore(&dev->irqlock, flags);
  942. s5p_mfc_wait_for_done_ctx(ctx,
  943. S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
  944. aborted = 1;
  945. spin_lock_irqsave(&dev->irqlock, flags);
  946. }
  947. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  948. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  949. INIT_LIST_HEAD(&ctx->dst_queue);
  950. ctx->dst_queue_cnt = 0;
  951. ctx->dpb_flush_flag = 1;
  952. ctx->dec_dst_flag = 0;
  953. if (IS_MFCV6_PLUS(dev) && (ctx->state == MFCINST_RUNNING)) {
  954. ctx->state = MFCINST_FLUSH;
  955. set_work_bit_irqsave(ctx);
  956. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  957. spin_unlock_irqrestore(&dev->irqlock, flags);
  958. if (s5p_mfc_wait_for_done_ctx(ctx,
  959. S5P_MFC_R2H_CMD_DPB_FLUSH_RET, 0))
  960. mfc_err("Err flushing buffers\n");
  961. spin_lock_irqsave(&dev->irqlock, flags);
  962. }
  963. } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  964. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  965. INIT_LIST_HEAD(&ctx->src_queue);
  966. ctx->src_queue_cnt = 0;
  967. }
  968. if (aborted)
  969. ctx->state = MFCINST_RUNNING;
  970. spin_unlock_irqrestore(&dev->irqlock, flags);
  971. }
  972. static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
  973. {
  974. struct vb2_queue *vq = vb->vb2_queue;
  975. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  976. struct s5p_mfc_dev *dev = ctx->dev;
  977. unsigned long flags;
  978. struct s5p_mfc_buf *mfc_buf;
  979. if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  980. mfc_buf = &ctx->src_bufs[vb->index];
  981. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  982. spin_lock_irqsave(&dev->irqlock, flags);
  983. list_add_tail(&mfc_buf->list, &ctx->src_queue);
  984. ctx->src_queue_cnt++;
  985. spin_unlock_irqrestore(&dev->irqlock, flags);
  986. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  987. mfc_buf = &ctx->dst_bufs[vb->index];
  988. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  989. /* Mark destination as available for use by MFC */
  990. spin_lock_irqsave(&dev->irqlock, flags);
  991. set_bit(vb->index, &ctx->dec_dst_flag);
  992. list_add_tail(&mfc_buf->list, &ctx->dst_queue);
  993. ctx->dst_queue_cnt++;
  994. spin_unlock_irqrestore(&dev->irqlock, flags);
  995. } else {
  996. mfc_err("Unsupported buffer type (%d)\n", vq->type);
  997. }
  998. if (s5p_mfc_ctx_ready(ctx))
  999. set_work_bit_irqsave(ctx);
  1000. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1001. }
  1002. static struct vb2_ops s5p_mfc_dec_qops = {
  1003. .queue_setup = s5p_mfc_queue_setup,
  1004. .wait_prepare = vb2_ops_wait_prepare,
  1005. .wait_finish = vb2_ops_wait_finish,
  1006. .buf_init = s5p_mfc_buf_init,
  1007. .start_streaming = s5p_mfc_start_streaming,
  1008. .stop_streaming = s5p_mfc_stop_streaming,
  1009. .buf_queue = s5p_mfc_buf_queue,
  1010. };
  1011. const struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
  1012. {
  1013. return &decoder_codec_ops;
  1014. }
  1015. struct vb2_ops *get_dec_queue_ops(void)
  1016. {
  1017. return &s5p_mfc_dec_qops;
  1018. }
  1019. const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
  1020. {
  1021. return &s5p_mfc_dec_ioctl_ops;
  1022. }
  1023. #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_MPEG) \
  1024. && V4L2_CTRL_DRIVER_PRIV(x))
  1025. int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
  1026. {
  1027. struct v4l2_ctrl_config cfg;
  1028. int i;
  1029. v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
  1030. if (ctx->ctrl_handler.error) {
  1031. mfc_err("v4l2_ctrl_handler_init failed\n");
  1032. return ctx->ctrl_handler.error;
  1033. }
  1034. for (i = 0; i < NUM_CTRLS; i++) {
  1035. if (IS_MFC51_PRIV(controls[i].id)) {
  1036. memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
  1037. cfg.ops = &s5p_mfc_dec_ctrl_ops;
  1038. cfg.id = controls[i].id;
  1039. cfg.min = controls[i].minimum;
  1040. cfg.max = controls[i].maximum;
  1041. cfg.def = controls[i].default_value;
  1042. cfg.name = controls[i].name;
  1043. cfg.type = controls[i].type;
  1044. cfg.step = controls[i].step;
  1045. cfg.menu_skip_mask = 0;
  1046. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
  1047. &cfg, NULL);
  1048. } else {
  1049. ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  1050. &s5p_mfc_dec_ctrl_ops,
  1051. controls[i].id, controls[i].minimum,
  1052. controls[i].maximum, controls[i].step,
  1053. controls[i].default_value);
  1054. }
  1055. if (ctx->ctrl_handler.error) {
  1056. mfc_err("Adding control (%d) failed\n", i);
  1057. return ctx->ctrl_handler.error;
  1058. }
  1059. if (controls[i].is_volatile && ctx->ctrls[i])
  1060. ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1061. }
  1062. return 0;
  1063. }
  1064. void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
  1065. {
  1066. int i;
  1067. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1068. for (i = 0; i < NUM_CTRLS; i++)
  1069. ctx->ctrls[i] = NULL;
  1070. }
  1071. void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
  1072. {
  1073. struct v4l2_format f;
  1074. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  1075. ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
  1076. if (IS_MFCV8_PLUS(ctx->dev))
  1077. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
  1078. else if (IS_MFCV6_PLUS(ctx->dev))
  1079. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12MT_16X16;
  1080. else
  1081. f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12MT;
  1082. ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
  1083. mfc_debug(2, "Default src_fmt is %p, dest_fmt is %p\n",
  1084. ctx->src_fmt, ctx->dst_fmt);
  1085. }