s5p_mfc_dec.c 34 KB

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