s5p_mfc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*
  2. * Samsung S5P Multi Format Codec v 5.1
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * Kamil Debski, <k.debski@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.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/videodev2.h>
  21. #include <media/v4l2-event.h>
  22. #include <linux/workqueue.h>
  23. #include <media/videobuf2-core.h>
  24. #include "s5p_mfc_common.h"
  25. #include "s5p_mfc_ctrl.h"
  26. #include "s5p_mfc_debug.h"
  27. #include "s5p_mfc_dec.h"
  28. #include "s5p_mfc_enc.h"
  29. #include "s5p_mfc_intr.h"
  30. #include "s5p_mfc_opr.h"
  31. #include "s5p_mfc_cmd.h"
  32. #include "s5p_mfc_pm.h"
  33. #define S5P_MFC_NAME "s5p-mfc"
  34. #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
  35. #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
  36. int debug;
  37. module_param(debug, int, S_IRUGO | S_IWUSR);
  38. MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
  39. /* Helper functions for interrupt processing */
  40. /* Remove from hw execution round robin */
  41. void clear_work_bit(struct s5p_mfc_ctx *ctx)
  42. {
  43. struct s5p_mfc_dev *dev = ctx->dev;
  44. spin_lock(&dev->condlock);
  45. __clear_bit(ctx->num, &dev->ctx_work_bits);
  46. spin_unlock(&dev->condlock);
  47. }
  48. /* Add to hw execution round robin */
  49. void set_work_bit(struct s5p_mfc_ctx *ctx)
  50. {
  51. struct s5p_mfc_dev *dev = ctx->dev;
  52. spin_lock(&dev->condlock);
  53. __set_bit(ctx->num, &dev->ctx_work_bits);
  54. spin_unlock(&dev->condlock);
  55. }
  56. /* Remove from hw execution round robin */
  57. void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
  58. {
  59. struct s5p_mfc_dev *dev = ctx->dev;
  60. unsigned long flags;
  61. spin_lock_irqsave(&dev->condlock, flags);
  62. __clear_bit(ctx->num, &dev->ctx_work_bits);
  63. spin_unlock_irqrestore(&dev->condlock, flags);
  64. }
  65. /* Add to hw execution round robin */
  66. void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
  67. {
  68. struct s5p_mfc_dev *dev = ctx->dev;
  69. unsigned long flags;
  70. spin_lock_irqsave(&dev->condlock, flags);
  71. __set_bit(ctx->num, &dev->ctx_work_bits);
  72. spin_unlock_irqrestore(&dev->condlock, flags);
  73. }
  74. /* Wake up context wait_queue */
  75. static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
  76. unsigned int err)
  77. {
  78. ctx->int_cond = 1;
  79. ctx->int_type = reason;
  80. ctx->int_err = err;
  81. wake_up(&ctx->queue);
  82. }
  83. /* Wake up device wait_queue */
  84. static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
  85. unsigned int err)
  86. {
  87. dev->int_cond = 1;
  88. dev->int_type = reason;
  89. dev->int_err = err;
  90. wake_up(&dev->queue);
  91. }
  92. static void s5p_mfc_watchdog(unsigned long arg)
  93. {
  94. struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;
  95. if (test_bit(0, &dev->hw_lock))
  96. atomic_inc(&dev->watchdog_cnt);
  97. if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
  98. /* This means that hw is busy and no interrupts were
  99. * generated by hw for the Nth time of running this
  100. * watchdog timer. This usually means a serious hw
  101. * error. Now it is time to kill all instances and
  102. * reset the MFC. */
  103. mfc_err("Time out during waiting for HW\n");
  104. queue_work(dev->watchdog_workqueue, &dev->watchdog_work);
  105. }
  106. dev->watchdog_timer.expires = jiffies +
  107. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  108. add_timer(&dev->watchdog_timer);
  109. }
  110. static void s5p_mfc_watchdog_worker(struct work_struct *work)
  111. {
  112. struct s5p_mfc_dev *dev;
  113. struct s5p_mfc_ctx *ctx;
  114. unsigned long flags;
  115. int mutex_locked;
  116. int i, ret;
  117. dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
  118. mfc_err("Driver timeout error handling\n");
  119. /* Lock the mutex that protects open and release.
  120. * This is necessary as they may load and unload firmware. */
  121. mutex_locked = mutex_trylock(&dev->mfc_mutex);
  122. if (!mutex_locked)
  123. mfc_err("Error: some instance may be closing/opening\n");
  124. spin_lock_irqsave(&dev->irqlock, flags);
  125. s5p_mfc_clock_off();
  126. for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
  127. ctx = dev->ctx[i];
  128. if (!ctx)
  129. continue;
  130. ctx->state = MFCINST_ERROR;
  131. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
  132. &ctx->vq_dst);
  133. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
  134. &ctx->vq_src);
  135. clear_work_bit(ctx);
  136. wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
  137. }
  138. clear_bit(0, &dev->hw_lock);
  139. spin_unlock_irqrestore(&dev->irqlock, flags);
  140. /* Double check if there is at least one instance running.
  141. * If no instance is in memory than no firmware should be present */
  142. if (dev->num_inst > 0) {
  143. ret = s5p_mfc_reload_firmware(dev);
  144. if (ret) {
  145. mfc_err("Failed to reload FW\n");
  146. goto unlock;
  147. }
  148. s5p_mfc_clock_on();
  149. ret = s5p_mfc_init_hw(dev);
  150. if (ret)
  151. mfc_err("Failed to reinit FW\n");
  152. }
  153. unlock:
  154. if (mutex_locked)
  155. mutex_unlock(&dev->mfc_mutex);
  156. }
  157. static enum s5p_mfc_node_type s5p_mfc_get_node_type(struct file *file)
  158. {
  159. struct video_device *vdev = video_devdata(file);
  160. if (!vdev) {
  161. mfc_err("failed to get video_device");
  162. return MFCNODE_INVALID;
  163. }
  164. if (vdev->index == 0)
  165. return MFCNODE_DECODER;
  166. else if (vdev->index == 1)
  167. return MFCNODE_ENCODER;
  168. return MFCNODE_INVALID;
  169. }
  170. static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
  171. {
  172. mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
  173. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
  174. mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
  175. }
  176. static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
  177. {
  178. struct s5p_mfc_buf *dst_buf;
  179. struct s5p_mfc_dev *dev = ctx->dev;
  180. ctx->state = MFCINST_FINISHED;
  181. ctx->sequence++;
  182. while (!list_empty(&ctx->dst_queue)) {
  183. dst_buf = list_entry(ctx->dst_queue.next,
  184. struct s5p_mfc_buf, list);
  185. mfc_debug(2, "Cleaning up buffer: %d\n",
  186. dst_buf->b->v4l2_buf.index);
  187. vb2_set_plane_payload(dst_buf->b, 0, 0);
  188. vb2_set_plane_payload(dst_buf->b, 1, 0);
  189. list_del(&dst_buf->list);
  190. ctx->dst_queue_cnt--;
  191. dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
  192. if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
  193. s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
  194. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  195. else
  196. dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
  197. ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
  198. vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
  199. }
  200. }
  201. static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
  202. {
  203. struct s5p_mfc_dev *dev = ctx->dev;
  204. struct s5p_mfc_buf *dst_buf, *src_buf;
  205. size_t dec_y_addr;
  206. unsigned int frame_type;
  207. dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
  208. frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
  209. /* Copy timestamp / timecode from decoded src to dst and set
  210. appropraite flags */
  211. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  212. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  213. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
  214. memcpy(&dst_buf->b->v4l2_buf.timecode,
  215. &src_buf->b->v4l2_buf.timecode,
  216. sizeof(struct v4l2_timecode));
  217. memcpy(&dst_buf->b->v4l2_buf.timestamp,
  218. &src_buf->b->v4l2_buf.timestamp,
  219. sizeof(struct timeval));
  220. switch (frame_type) {
  221. case S5P_FIMV_DECODE_FRAME_I_FRAME:
  222. dst_buf->b->v4l2_buf.flags |=
  223. V4L2_BUF_FLAG_KEYFRAME;
  224. break;
  225. case S5P_FIMV_DECODE_FRAME_P_FRAME:
  226. dst_buf->b->v4l2_buf.flags |=
  227. V4L2_BUF_FLAG_PFRAME;
  228. break;
  229. case S5P_FIMV_DECODE_FRAME_B_FRAME:
  230. dst_buf->b->v4l2_buf.flags |=
  231. V4L2_BUF_FLAG_BFRAME;
  232. break;
  233. }
  234. break;
  235. }
  236. }
  237. }
  238. static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
  239. {
  240. struct s5p_mfc_dev *dev = ctx->dev;
  241. struct s5p_mfc_buf *dst_buf;
  242. size_t dspl_y_addr;
  243. unsigned int frame_type;
  244. unsigned int index;
  245. dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
  246. frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
  247. /* If frame is same as previous then skip and do not dequeue */
  248. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
  249. if (!ctx->after_packed_pb)
  250. ctx->sequence++;
  251. ctx->after_packed_pb = 0;
  252. return;
  253. }
  254. ctx->sequence++;
  255. /* The MFC returns address of the buffer, now we have to
  256. * check which videobuf does it correspond to */
  257. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  258. /* Check if this is the buffer we're looking for */
  259. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
  260. list_del(&dst_buf->list);
  261. ctx->dst_queue_cnt--;
  262. dst_buf->b->v4l2_buf.sequence = ctx->sequence;
  263. if (s5p_mfc_hw_call(dev->mfc_ops,
  264. get_pic_type_top, ctx) ==
  265. s5p_mfc_hw_call(dev->mfc_ops,
  266. get_pic_type_bot, ctx))
  267. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  268. else
  269. dst_buf->b->v4l2_buf.field =
  270. V4L2_FIELD_INTERLACED;
  271. vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
  272. vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
  273. clear_bit(dst_buf->b->v4l2_buf.index,
  274. &ctx->dec_dst_flag);
  275. vb2_buffer_done(dst_buf->b,
  276. err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  277. index = dst_buf->b->v4l2_buf.index;
  278. break;
  279. }
  280. }
  281. }
  282. /* Handle frame decoding interrupt */
  283. static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
  284. unsigned int reason, unsigned int err)
  285. {
  286. struct s5p_mfc_dev *dev = ctx->dev;
  287. unsigned int dst_frame_status;
  288. struct s5p_mfc_buf *src_buf;
  289. unsigned long flags;
  290. unsigned int res_change;
  291. unsigned int index;
  292. dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  293. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  294. res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  295. & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
  296. >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
  297. mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
  298. if (ctx->state == MFCINST_RES_CHANGE_INIT)
  299. ctx->state = MFCINST_RES_CHANGE_FLUSH;
  300. if (res_change == S5P_FIMV_RES_INCREASE ||
  301. res_change == S5P_FIMV_RES_DECREASE) {
  302. ctx->state = MFCINST_RES_CHANGE_INIT;
  303. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  304. wake_up_ctx(ctx, reason, err);
  305. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  306. BUG();
  307. s5p_mfc_clock_off();
  308. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  309. return;
  310. }
  311. if (ctx->dpb_flush_flag)
  312. ctx->dpb_flush_flag = 0;
  313. spin_lock_irqsave(&dev->irqlock, flags);
  314. /* All frames remaining in the buffer have been extracted */
  315. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
  316. if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
  317. s5p_mfc_handle_frame_all_extracted(ctx);
  318. ctx->state = MFCINST_RES_CHANGE_END;
  319. goto leave_handle_frame;
  320. } else {
  321. s5p_mfc_handle_frame_all_extracted(ctx);
  322. }
  323. }
  324. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY ||
  325. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_ONLY)
  326. s5p_mfc_handle_frame_copy_time(ctx);
  327. /* A frame has been decoded and is in the buffer */
  328. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
  329. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
  330. s5p_mfc_handle_frame_new(ctx, err);
  331. } else {
  332. mfc_debug(2, "No frame decode\n");
  333. }
  334. /* Mark source buffer as complete */
  335. if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
  336. && !list_empty(&ctx->src_queue)) {
  337. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  338. list);
  339. ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
  340. get_consumed_stream, dev);
  341. if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
  342. s5p_mfc_hw_call(dev->mfc_ops,
  343. get_dec_frame_type, dev) ==
  344. S5P_FIMV_DECODE_FRAME_P_FRAME
  345. && ctx->consumed_stream + STUFF_BYTE <
  346. src_buf->b->v4l2_planes[0].bytesused) {
  347. /* Run MFC again on the same buffer */
  348. mfc_debug(2, "Running again the same buffer\n");
  349. ctx->after_packed_pb = 1;
  350. } else {
  351. index = src_buf->b->v4l2_buf.index;
  352. mfc_debug(2, "MFC needs next buffer\n");
  353. ctx->consumed_stream = 0;
  354. list_del(&src_buf->list);
  355. ctx->src_queue_cnt--;
  356. if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
  357. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
  358. else
  359. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
  360. }
  361. }
  362. leave_handle_frame:
  363. spin_unlock_irqrestore(&dev->irqlock, flags);
  364. if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
  365. || ctx->dst_queue_cnt < ctx->dpb_count)
  366. clear_work_bit(ctx);
  367. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  368. wake_up_ctx(ctx, reason, err);
  369. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  370. BUG();
  371. s5p_mfc_clock_off();
  372. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  373. }
  374. /* Error handling for interrupt */
  375. static void s5p_mfc_handle_error(struct s5p_mfc_ctx *ctx,
  376. unsigned int reason, unsigned int err)
  377. {
  378. struct s5p_mfc_dev *dev;
  379. unsigned long flags;
  380. /* If no context is available then all necessary
  381. * processing has been done. */
  382. if (ctx == NULL)
  383. return;
  384. dev = ctx->dev;
  385. mfc_err("Interrupt Error: %08x\n", err);
  386. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  387. wake_up_dev(dev, reason, err);
  388. /* Error recovery is dependent on the state of context */
  389. switch (ctx->state) {
  390. case MFCINST_INIT:
  391. /* This error had to happen while acquireing instance */
  392. case MFCINST_GOT_INST:
  393. /* This error had to happen while parsing the header */
  394. case MFCINST_HEAD_PARSED:
  395. /* This error had to happen while setting dst buffers */
  396. case MFCINST_RETURN_INST:
  397. /* This error had to happen while releasing instance */
  398. clear_work_bit(ctx);
  399. wake_up_ctx(ctx, reason, err);
  400. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  401. BUG();
  402. s5p_mfc_clock_off();
  403. ctx->state = MFCINST_ERROR;
  404. break;
  405. case MFCINST_FINISHING:
  406. case MFCINST_FINISHED:
  407. case MFCINST_RUNNING:
  408. /* It is higly probable that an error occured
  409. * while decoding a frame */
  410. clear_work_bit(ctx);
  411. ctx->state = MFCINST_ERROR;
  412. /* Mark all dst buffers as having an error */
  413. spin_lock_irqsave(&dev->irqlock, flags);
  414. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
  415. &ctx->vq_dst);
  416. /* Mark all src buffers as having an error */
  417. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
  418. &ctx->vq_src);
  419. spin_unlock_irqrestore(&dev->irqlock, flags);
  420. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  421. BUG();
  422. s5p_mfc_clock_off();
  423. break;
  424. default:
  425. mfc_err("Encountered an error interrupt which had not been handled\n");
  426. break;
  427. }
  428. return;
  429. }
  430. /* Header parsing interrupt handling */
  431. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  432. unsigned int reason, unsigned int err)
  433. {
  434. struct s5p_mfc_dev *dev;
  435. if (ctx == NULL)
  436. return;
  437. dev = ctx->dev;
  438. if (ctx->c_ops->post_seq_start) {
  439. if (ctx->c_ops->post_seq_start(ctx))
  440. mfc_err("post_seq_start() failed\n");
  441. } else {
  442. ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
  443. dev);
  444. ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
  445. dev);
  446. s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
  447. ctx->dpb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
  448. dev);
  449. ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
  450. dev);
  451. if (ctx->img_width == 0 || ctx->img_height == 0)
  452. ctx->state = MFCINST_ERROR;
  453. else
  454. ctx->state = MFCINST_HEAD_PARSED;
  455. if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  456. ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
  457. !list_empty(&ctx->src_queue)) {
  458. struct s5p_mfc_buf *src_buf;
  459. src_buf = list_entry(ctx->src_queue.next,
  460. struct s5p_mfc_buf, list);
  461. if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
  462. dev) <
  463. src_buf->b->v4l2_planes[0].bytesused)
  464. ctx->head_processed = 0;
  465. else
  466. ctx->head_processed = 1;
  467. } else {
  468. ctx->head_processed = 1;
  469. }
  470. }
  471. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  472. clear_work_bit(ctx);
  473. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  474. BUG();
  475. s5p_mfc_clock_off();
  476. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  477. wake_up_ctx(ctx, reason, err);
  478. }
  479. /* Header parsing interrupt handling */
  480. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  481. unsigned int reason, unsigned int err)
  482. {
  483. struct s5p_mfc_buf *src_buf;
  484. struct s5p_mfc_dev *dev;
  485. unsigned long flags;
  486. if (ctx == NULL)
  487. return;
  488. dev = ctx->dev;
  489. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  490. ctx->int_type = reason;
  491. ctx->int_err = err;
  492. ctx->int_cond = 1;
  493. clear_work_bit(ctx);
  494. if (err == 0) {
  495. ctx->state = MFCINST_RUNNING;
  496. if (!ctx->dpb_flush_flag && ctx->head_processed) {
  497. spin_lock_irqsave(&dev->irqlock, flags);
  498. if (!list_empty(&ctx->src_queue)) {
  499. src_buf = list_entry(ctx->src_queue.next,
  500. struct s5p_mfc_buf, list);
  501. list_del(&src_buf->list);
  502. ctx->src_queue_cnt--;
  503. vb2_buffer_done(src_buf->b,
  504. VB2_BUF_STATE_DONE);
  505. }
  506. spin_unlock_irqrestore(&dev->irqlock, flags);
  507. } else {
  508. ctx->dpb_flush_flag = 0;
  509. }
  510. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  511. BUG();
  512. s5p_mfc_clock_off();
  513. wake_up(&ctx->queue);
  514. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  515. } else {
  516. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  517. BUG();
  518. s5p_mfc_clock_off();
  519. wake_up(&ctx->queue);
  520. }
  521. }
  522. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
  523. unsigned int reason, unsigned int err)
  524. {
  525. struct s5p_mfc_dev *dev = ctx->dev;
  526. struct s5p_mfc_buf *mb_entry;
  527. mfc_debug(2, "Stream completed");
  528. s5p_mfc_clear_int_flags(dev);
  529. ctx->int_type = reason;
  530. ctx->int_err = err;
  531. ctx->state = MFCINST_FINISHED;
  532. spin_lock(&dev->irqlock);
  533. if (!list_empty(&ctx->dst_queue)) {
  534. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  535. list);
  536. list_del(&mb_entry->list);
  537. ctx->dst_queue_cnt--;
  538. vb2_set_plane_payload(mb_entry->b, 0, 0);
  539. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  540. }
  541. spin_unlock(&dev->irqlock);
  542. clear_work_bit(ctx);
  543. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  544. WARN_ON(1);
  545. s5p_mfc_clock_off();
  546. wake_up(&ctx->queue);
  547. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  548. }
  549. /* Interrupt processing */
  550. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  551. {
  552. struct s5p_mfc_dev *dev = priv;
  553. struct s5p_mfc_ctx *ctx;
  554. unsigned int reason;
  555. unsigned int err;
  556. mfc_debug_enter();
  557. /* Reset the timeout watchdog */
  558. atomic_set(&dev->watchdog_cnt, 0);
  559. ctx = dev->ctx[dev->curr_ctx];
  560. /* Get the reason of interrupt and the error code */
  561. reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
  562. err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
  563. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  564. switch (reason) {
  565. case S5P_MFC_R2H_CMD_ERR_RET:
  566. /* An error has occured */
  567. if (ctx->state == MFCINST_RUNNING &&
  568. s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
  569. dev->warn_start)
  570. s5p_mfc_handle_frame(ctx, reason, err);
  571. else
  572. s5p_mfc_handle_error(ctx, reason, err);
  573. clear_bit(0, &dev->enter_suspend);
  574. break;
  575. case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
  576. case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
  577. case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
  578. if (ctx->c_ops->post_frame_start) {
  579. if (ctx->c_ops->post_frame_start(ctx))
  580. mfc_err("post_frame_start() failed\n");
  581. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  582. wake_up_ctx(ctx, reason, err);
  583. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  584. BUG();
  585. s5p_mfc_clock_off();
  586. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  587. } else {
  588. s5p_mfc_handle_frame(ctx, reason, err);
  589. }
  590. break;
  591. case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
  592. s5p_mfc_handle_seq_done(ctx, reason, err);
  593. break;
  594. case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
  595. ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
  596. ctx->state = MFCINST_GOT_INST;
  597. clear_work_bit(ctx);
  598. wake_up(&ctx->queue);
  599. goto irq_cleanup_hw;
  600. case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
  601. clear_work_bit(ctx);
  602. ctx->state = MFCINST_FREE;
  603. wake_up(&ctx->queue);
  604. goto irq_cleanup_hw;
  605. case S5P_MFC_R2H_CMD_SYS_INIT_RET:
  606. case S5P_MFC_R2H_CMD_FW_STATUS_RET:
  607. case S5P_MFC_R2H_CMD_SLEEP_RET:
  608. case S5P_MFC_R2H_CMD_WAKEUP_RET:
  609. if (ctx)
  610. clear_work_bit(ctx);
  611. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  612. wake_up_dev(dev, reason, err);
  613. clear_bit(0, &dev->hw_lock);
  614. clear_bit(0, &dev->enter_suspend);
  615. break;
  616. case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
  617. s5p_mfc_handle_init_buffers(ctx, reason, err);
  618. break;
  619. case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
  620. s5p_mfc_handle_stream_complete(ctx, reason, err);
  621. break;
  622. default:
  623. mfc_debug(2, "Unknown int reason\n");
  624. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  625. }
  626. mfc_debug_leave();
  627. return IRQ_HANDLED;
  628. irq_cleanup_hw:
  629. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  630. ctx->int_type = reason;
  631. ctx->int_err = err;
  632. ctx->int_cond = 1;
  633. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  634. mfc_err("Failed to unlock hw\n");
  635. s5p_mfc_clock_off();
  636. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  637. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  638. return IRQ_HANDLED;
  639. }
  640. /* Open an MFC node */
  641. static int s5p_mfc_open(struct file *file)
  642. {
  643. struct s5p_mfc_dev *dev = video_drvdata(file);
  644. struct s5p_mfc_ctx *ctx = NULL;
  645. struct vb2_queue *q;
  646. int ret = 0;
  647. mfc_debug_enter();
  648. if (mutex_lock_interruptible(&dev->mfc_mutex))
  649. return -ERESTARTSYS;
  650. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  651. /* Allocate memory for context */
  652. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  653. if (!ctx) {
  654. mfc_err("Not enough memory\n");
  655. ret = -ENOMEM;
  656. goto err_alloc;
  657. }
  658. v4l2_fh_init(&ctx->fh, video_devdata(file));
  659. file->private_data = &ctx->fh;
  660. v4l2_fh_add(&ctx->fh);
  661. ctx->dev = dev;
  662. INIT_LIST_HEAD(&ctx->src_queue);
  663. INIT_LIST_HEAD(&ctx->dst_queue);
  664. ctx->src_queue_cnt = 0;
  665. ctx->dst_queue_cnt = 0;
  666. /* Get context number */
  667. ctx->num = 0;
  668. while (dev->ctx[ctx->num]) {
  669. ctx->num++;
  670. if (ctx->num >= MFC_NUM_CONTEXTS) {
  671. mfc_err("Too many open contexts\n");
  672. ret = -EBUSY;
  673. goto err_no_ctx;
  674. }
  675. }
  676. /* Mark context as idle */
  677. clear_work_bit_irqsave(ctx);
  678. dev->ctx[ctx->num] = ctx;
  679. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  680. ctx->type = MFCINST_DECODER;
  681. ctx->c_ops = get_dec_codec_ops();
  682. s5p_mfc_dec_init(ctx);
  683. /* Setup ctrl handler */
  684. ret = s5p_mfc_dec_ctrls_setup(ctx);
  685. if (ret) {
  686. mfc_err("Failed to setup mfc controls\n");
  687. goto err_ctrls_setup;
  688. }
  689. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  690. ctx->type = MFCINST_ENCODER;
  691. ctx->c_ops = get_enc_codec_ops();
  692. /* only for encoder */
  693. INIT_LIST_HEAD(&ctx->ref_queue);
  694. ctx->ref_queue_cnt = 0;
  695. s5p_mfc_enc_init(ctx);
  696. /* Setup ctrl handler */
  697. ret = s5p_mfc_enc_ctrls_setup(ctx);
  698. if (ret) {
  699. mfc_err("Failed to setup mfc controls\n");
  700. goto err_ctrls_setup;
  701. }
  702. } else {
  703. ret = -ENOENT;
  704. goto err_bad_node;
  705. }
  706. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  707. ctx->inst_no = -1;
  708. /* Load firmware if this is the first instance */
  709. if (dev->num_inst == 1) {
  710. dev->watchdog_timer.expires = jiffies +
  711. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  712. add_timer(&dev->watchdog_timer);
  713. ret = s5p_mfc_power_on();
  714. if (ret < 0) {
  715. mfc_err("power on failed\n");
  716. goto err_pwr_enable;
  717. }
  718. s5p_mfc_clock_on();
  719. ret = s5p_mfc_alloc_and_load_firmware(dev);
  720. if (ret)
  721. goto err_alloc_fw;
  722. /* Init the FW */
  723. ret = s5p_mfc_init_hw(dev);
  724. if (ret)
  725. goto err_init_hw;
  726. s5p_mfc_clock_off();
  727. }
  728. /* Init videobuf2 queue for CAPTURE */
  729. q = &ctx->vq_dst;
  730. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  731. q->drv_priv = &ctx->fh;
  732. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  733. q->io_modes = VB2_MMAP;
  734. q->ops = get_dec_queue_ops();
  735. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  736. q->io_modes = VB2_MMAP | VB2_USERPTR;
  737. q->ops = get_enc_queue_ops();
  738. } else {
  739. ret = -ENOENT;
  740. goto err_queue_init;
  741. }
  742. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  743. ret = vb2_queue_init(q);
  744. if (ret) {
  745. mfc_err("Failed to initialize videobuf2 queue(capture)\n");
  746. goto err_queue_init;
  747. }
  748. /* Init videobuf2 queue for OUTPUT */
  749. q = &ctx->vq_src;
  750. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  751. q->io_modes = VB2_MMAP;
  752. q->drv_priv = &ctx->fh;
  753. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  754. q->io_modes = VB2_MMAP;
  755. q->ops = get_dec_queue_ops();
  756. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  757. q->io_modes = VB2_MMAP | VB2_USERPTR;
  758. q->ops = get_enc_queue_ops();
  759. } else {
  760. ret = -ENOENT;
  761. goto err_queue_init;
  762. }
  763. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  764. ret = vb2_queue_init(q);
  765. if (ret) {
  766. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  767. goto err_queue_init;
  768. }
  769. init_waitqueue_head(&ctx->queue);
  770. mutex_unlock(&dev->mfc_mutex);
  771. mfc_debug_leave();
  772. return ret;
  773. /* Deinit when failure occured */
  774. err_queue_init:
  775. err_init_hw:
  776. s5p_mfc_release_firmware(dev);
  777. err_alloc_fw:
  778. dev->ctx[ctx->num] = NULL;
  779. del_timer_sync(&dev->watchdog_timer);
  780. s5p_mfc_clock_off();
  781. err_pwr_enable:
  782. if (dev->num_inst == 1) {
  783. if (s5p_mfc_power_off() < 0)
  784. mfc_err("power off failed\n");
  785. s5p_mfc_release_firmware(dev);
  786. }
  787. err_ctrls_setup:
  788. s5p_mfc_dec_ctrls_delete(ctx);
  789. err_bad_node:
  790. err_no_ctx:
  791. v4l2_fh_del(&ctx->fh);
  792. v4l2_fh_exit(&ctx->fh);
  793. kfree(ctx);
  794. err_alloc:
  795. dev->num_inst--;
  796. mutex_unlock(&dev->mfc_mutex);
  797. mfc_debug_leave();
  798. return ret;
  799. }
  800. /* Release MFC context */
  801. static int s5p_mfc_release(struct file *file)
  802. {
  803. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  804. struct s5p_mfc_dev *dev = ctx->dev;
  805. mfc_debug_enter();
  806. mutex_lock(&dev->mfc_mutex);
  807. s5p_mfc_clock_on();
  808. vb2_queue_release(&ctx->vq_src);
  809. vb2_queue_release(&ctx->vq_dst);
  810. /* Mark context as idle */
  811. clear_work_bit_irqsave(ctx);
  812. /* If instance was initialised then
  813. * return instance and free reosurces */
  814. if (ctx->inst_no != MFC_NO_INSTANCE_SET) {
  815. mfc_debug(2, "Has to free instance\n");
  816. ctx->state = MFCINST_RETURN_INST;
  817. set_work_bit_irqsave(ctx);
  818. s5p_mfc_clean_ctx_int_flags(ctx);
  819. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  820. /* Wait until instance is returned or timeout occured */
  821. if (s5p_mfc_wait_for_done_ctx
  822. (ctx, S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET, 0)) {
  823. s5p_mfc_clock_off();
  824. mfc_err("Err returning instance\n");
  825. }
  826. mfc_debug(2, "After free instance\n");
  827. /* Free resources */
  828. s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
  829. s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer, ctx);
  830. if (ctx->type == MFCINST_DECODER)
  831. s5p_mfc_hw_call(dev->mfc_ops, release_dec_desc_buffer,
  832. ctx);
  833. ctx->inst_no = MFC_NO_INSTANCE_SET;
  834. }
  835. /* hardware locking scheme */
  836. if (dev->curr_ctx == ctx->num)
  837. clear_bit(0, &dev->hw_lock);
  838. dev->num_inst--;
  839. if (dev->num_inst == 0) {
  840. mfc_debug(2, "Last instance - release firmware\n");
  841. /* reset <-> F/W release */
  842. s5p_mfc_reset(dev);
  843. s5p_mfc_deinit_hw(dev);
  844. s5p_mfc_release_firmware(dev);
  845. del_timer_sync(&dev->watchdog_timer);
  846. if (s5p_mfc_power_off() < 0)
  847. mfc_err("Power off failed\n");
  848. }
  849. mfc_debug(2, "Shutting down clock\n");
  850. s5p_mfc_clock_off();
  851. dev->ctx[ctx->num] = NULL;
  852. s5p_mfc_dec_ctrls_delete(ctx);
  853. v4l2_fh_del(&ctx->fh);
  854. v4l2_fh_exit(&ctx->fh);
  855. kfree(ctx);
  856. mfc_debug_leave();
  857. mutex_unlock(&dev->mfc_mutex);
  858. return 0;
  859. }
  860. /* Poll */
  861. static unsigned int s5p_mfc_poll(struct file *file,
  862. struct poll_table_struct *wait)
  863. {
  864. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  865. struct s5p_mfc_dev *dev = ctx->dev;
  866. struct vb2_queue *src_q, *dst_q;
  867. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  868. unsigned int rc = 0;
  869. unsigned long flags;
  870. mutex_lock(&dev->mfc_mutex);
  871. src_q = &ctx->vq_src;
  872. dst_q = &ctx->vq_dst;
  873. /*
  874. * There has to be at least one buffer queued on each queued_list, which
  875. * means either in driver already or waiting for driver to claim it
  876. * and start processing.
  877. */
  878. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  879. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  880. rc = POLLERR;
  881. goto end;
  882. }
  883. mutex_unlock(&dev->mfc_mutex);
  884. poll_wait(file, &ctx->fh.wait, wait);
  885. poll_wait(file, &src_q->done_wq, wait);
  886. poll_wait(file, &dst_q->done_wq, wait);
  887. mutex_lock(&dev->mfc_mutex);
  888. if (v4l2_event_pending(&ctx->fh))
  889. rc |= POLLPRI;
  890. spin_lock_irqsave(&src_q->done_lock, flags);
  891. if (!list_empty(&src_q->done_list))
  892. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  893. done_entry);
  894. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  895. || src_vb->state == VB2_BUF_STATE_ERROR))
  896. rc |= POLLOUT | POLLWRNORM;
  897. spin_unlock_irqrestore(&src_q->done_lock, flags);
  898. spin_lock_irqsave(&dst_q->done_lock, flags);
  899. if (!list_empty(&dst_q->done_list))
  900. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  901. done_entry);
  902. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  903. || dst_vb->state == VB2_BUF_STATE_ERROR))
  904. rc |= POLLIN | POLLRDNORM;
  905. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  906. end:
  907. mutex_unlock(&dev->mfc_mutex);
  908. return rc;
  909. }
  910. /* Mmap */
  911. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  912. {
  913. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  914. struct s5p_mfc_dev *dev = ctx->dev;
  915. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  916. int ret;
  917. if (mutex_lock_interruptible(&dev->mfc_mutex))
  918. return -ERESTARTSYS;
  919. if (offset < DST_QUEUE_OFF_BASE) {
  920. mfc_debug(2, "mmaping source\n");
  921. ret = vb2_mmap(&ctx->vq_src, vma);
  922. } else { /* capture */
  923. mfc_debug(2, "mmaping destination\n");
  924. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  925. ret = vb2_mmap(&ctx->vq_dst, vma);
  926. }
  927. mutex_unlock(&dev->mfc_mutex);
  928. return ret;
  929. }
  930. /* v4l2 ops */
  931. static const struct v4l2_file_operations s5p_mfc_fops = {
  932. .owner = THIS_MODULE,
  933. .open = s5p_mfc_open,
  934. .release = s5p_mfc_release,
  935. .poll = s5p_mfc_poll,
  936. .unlocked_ioctl = video_ioctl2,
  937. .mmap = s5p_mfc_mmap,
  938. };
  939. static int match_child(struct device *dev, void *data)
  940. {
  941. if (!dev_name(dev))
  942. return 0;
  943. return !strcmp(dev_name(dev), (char *)data);
  944. }
  945. /* MFC probe function */
  946. static int s5p_mfc_probe(struct platform_device *pdev)
  947. {
  948. struct s5p_mfc_dev *dev;
  949. struct video_device *vfd;
  950. struct resource *res;
  951. int ret;
  952. pr_debug("%s++\n", __func__);
  953. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  954. if (!dev) {
  955. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  956. return -ENOMEM;
  957. }
  958. spin_lock_init(&dev->irqlock);
  959. spin_lock_init(&dev->condlock);
  960. dev->plat_dev = pdev;
  961. if (!dev->plat_dev) {
  962. dev_err(&pdev->dev, "No platform data specified\n");
  963. return -ENODEV;
  964. }
  965. dev->variant = (struct s5p_mfc_variant *)
  966. platform_get_device_id(pdev)->driver_data;
  967. ret = s5p_mfc_init_pm(dev);
  968. if (ret < 0) {
  969. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  970. return ret;
  971. }
  972. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  973. dev->regs_base = devm_request_and_ioremap(&pdev->dev, res);
  974. if (dev->regs_base == NULL) {
  975. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  976. return -ENOENT;
  977. }
  978. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  979. if (res == NULL) {
  980. dev_err(&pdev->dev, "failed to get irq resource\n");
  981. ret = -ENOENT;
  982. goto err_res;
  983. }
  984. dev->irq = res->start;
  985. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  986. IRQF_DISABLED, pdev->name, dev);
  987. if (ret) {
  988. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  989. goto err_res;
  990. }
  991. dev->mem_dev_l = device_find_child(&dev->plat_dev->dev, "s5p-mfc-l",
  992. match_child);
  993. if (!dev->mem_dev_l) {
  994. mfc_err("Mem child (L) device get failed\n");
  995. ret = -ENODEV;
  996. goto err_res;
  997. }
  998. dev->mem_dev_r = device_find_child(&dev->plat_dev->dev, "s5p-mfc-r",
  999. match_child);
  1000. if (!dev->mem_dev_r) {
  1001. mfc_err("Mem child (R) device get failed\n");
  1002. ret = -ENODEV;
  1003. goto err_res;
  1004. }
  1005. dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
  1006. if (IS_ERR_OR_NULL(dev->alloc_ctx[0])) {
  1007. ret = PTR_ERR(dev->alloc_ctx[0]);
  1008. goto err_res;
  1009. }
  1010. dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
  1011. if (IS_ERR_OR_NULL(dev->alloc_ctx[1])) {
  1012. ret = PTR_ERR(dev->alloc_ctx[1]);
  1013. goto err_mem_init_ctx_1;
  1014. }
  1015. mutex_init(&dev->mfc_mutex);
  1016. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1017. if (ret)
  1018. goto err_v4l2_dev_reg;
  1019. init_waitqueue_head(&dev->queue);
  1020. /* decoder */
  1021. vfd = video_device_alloc();
  1022. if (!vfd) {
  1023. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1024. ret = -ENOMEM;
  1025. goto err_dec_alloc;
  1026. }
  1027. vfd->fops = &s5p_mfc_fops,
  1028. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1029. vfd->release = video_device_release,
  1030. vfd->lock = &dev->mfc_mutex;
  1031. vfd->v4l2_dev = &dev->v4l2_dev;
  1032. vfd->vfl_dir = VFL_DIR_M2M;
  1033. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1034. dev->vfd_dec = vfd;
  1035. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1036. if (ret) {
  1037. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1038. video_device_release(vfd);
  1039. goto err_dec_reg;
  1040. }
  1041. v4l2_info(&dev->v4l2_dev,
  1042. "decoder registered as /dev/video%d\n", vfd->num);
  1043. video_set_drvdata(vfd, dev);
  1044. /* encoder */
  1045. vfd = video_device_alloc();
  1046. if (!vfd) {
  1047. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1048. ret = -ENOMEM;
  1049. goto err_enc_alloc;
  1050. }
  1051. vfd->fops = &s5p_mfc_fops,
  1052. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1053. vfd->release = video_device_release,
  1054. vfd->lock = &dev->mfc_mutex;
  1055. vfd->v4l2_dev = &dev->v4l2_dev;
  1056. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1057. dev->vfd_enc = vfd;
  1058. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1059. if (ret) {
  1060. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1061. video_device_release(vfd);
  1062. goto err_enc_reg;
  1063. }
  1064. v4l2_info(&dev->v4l2_dev,
  1065. "encoder registered as /dev/video%d\n", vfd->num);
  1066. video_set_drvdata(vfd, dev);
  1067. platform_set_drvdata(pdev, dev);
  1068. dev->hw_lock = 0;
  1069. dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
  1070. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1071. atomic_set(&dev->watchdog_cnt, 0);
  1072. init_timer(&dev->watchdog_timer);
  1073. dev->watchdog_timer.data = (unsigned long)dev;
  1074. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1075. /* Initialize HW ops and commands based on MFC version */
  1076. s5p_mfc_init_hw_ops(dev);
  1077. s5p_mfc_init_hw_cmds(dev);
  1078. pr_debug("%s--\n", __func__);
  1079. return 0;
  1080. /* Deinit MFC if probe had failed */
  1081. err_enc_reg:
  1082. video_device_release(dev->vfd_enc);
  1083. err_enc_alloc:
  1084. video_unregister_device(dev->vfd_dec);
  1085. err_dec_reg:
  1086. video_device_release(dev->vfd_dec);
  1087. err_dec_alloc:
  1088. v4l2_device_unregister(&dev->v4l2_dev);
  1089. err_v4l2_dev_reg:
  1090. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1091. err_mem_init_ctx_1:
  1092. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1093. err_res:
  1094. s5p_mfc_final_pm(dev);
  1095. pr_debug("%s-- with error\n", __func__);
  1096. return ret;
  1097. }
  1098. /* Remove the driver */
  1099. static int __devexit s5p_mfc_remove(struct platform_device *pdev)
  1100. {
  1101. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1102. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1103. del_timer_sync(&dev->watchdog_timer);
  1104. flush_workqueue(dev->watchdog_workqueue);
  1105. destroy_workqueue(dev->watchdog_workqueue);
  1106. video_unregister_device(dev->vfd_enc);
  1107. video_unregister_device(dev->vfd_dec);
  1108. v4l2_device_unregister(&dev->v4l2_dev);
  1109. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1110. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1111. s5p_mfc_final_pm(dev);
  1112. return 0;
  1113. }
  1114. #ifdef CONFIG_PM_SLEEP
  1115. static int s5p_mfc_suspend(struct device *dev)
  1116. {
  1117. struct platform_device *pdev = to_platform_device(dev);
  1118. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1119. int ret;
  1120. if (m_dev->num_inst == 0)
  1121. return 0;
  1122. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1123. mfc_err("Error: going to suspend for a second time\n");
  1124. return -EIO;
  1125. }
  1126. /* Check if we're processing then wait if it necessary. */
  1127. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1128. /* Try and lock the HW */
  1129. /* Wait on the interrupt waitqueue */
  1130. ret = wait_event_interruptible_timeout(m_dev->queue,
  1131. m_dev->int_cond || m_dev->ctx[m_dev->curr_ctx]->int_cond,
  1132. msecs_to_jiffies(MFC_INT_TIMEOUT));
  1133. if (ret == 0) {
  1134. mfc_err("Waiting for hardware to finish timed out\n");
  1135. return -EIO;
  1136. }
  1137. }
  1138. return s5p_mfc_sleep(m_dev);
  1139. }
  1140. static int s5p_mfc_resume(struct device *dev)
  1141. {
  1142. struct platform_device *pdev = to_platform_device(dev);
  1143. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1144. if (m_dev->num_inst == 0)
  1145. return 0;
  1146. return s5p_mfc_wakeup(m_dev);
  1147. }
  1148. #endif
  1149. #ifdef CONFIG_PM_RUNTIME
  1150. static int s5p_mfc_runtime_suspend(struct device *dev)
  1151. {
  1152. struct platform_device *pdev = to_platform_device(dev);
  1153. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1154. atomic_set(&m_dev->pm.power, 0);
  1155. return 0;
  1156. }
  1157. static int s5p_mfc_runtime_resume(struct device *dev)
  1158. {
  1159. struct platform_device *pdev = to_platform_device(dev);
  1160. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1161. int pre_power;
  1162. if (!m_dev->alloc_ctx)
  1163. return 0;
  1164. pre_power = atomic_read(&m_dev->pm.power);
  1165. atomic_set(&m_dev->pm.power, 1);
  1166. return 0;
  1167. }
  1168. #endif
  1169. /* Power management */
  1170. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1171. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1172. SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
  1173. NULL)
  1174. };
  1175. struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
  1176. .h264_ctx = MFC_H264_CTX_BUF_SIZE,
  1177. .non_h264_ctx = MFC_CTX_BUF_SIZE,
  1178. .dsc = DESC_BUF_SIZE,
  1179. .shm = SHARED_BUF_SIZE,
  1180. };
  1181. struct s5p_mfc_buf_size buf_size_v5 = {
  1182. .fw = MAX_FW_SIZE,
  1183. .cpb = MAX_CPB_SIZE,
  1184. .priv = &mfc_buf_size_v5,
  1185. };
  1186. struct s5p_mfc_buf_align mfc_buf_align_v5 = {
  1187. .base = MFC_BASE_ALIGN_ORDER,
  1188. };
  1189. static struct s5p_mfc_variant mfc_drvdata_v5 = {
  1190. .version = MFC_VERSION,
  1191. .port_num = MFC_NUM_PORTS,
  1192. .buf_size = &buf_size_v5,
  1193. .buf_align = &mfc_buf_align_v5,
  1194. .mclk_name = "sclk_mfc",
  1195. .fw_name = "s5p-mfc.fw",
  1196. };
  1197. struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
  1198. .dev_ctx = MFC_CTX_BUF_SIZE_V6,
  1199. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
  1200. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
  1201. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
  1202. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
  1203. };
  1204. struct s5p_mfc_buf_size buf_size_v6 = {
  1205. .fw = MAX_FW_SIZE_V6,
  1206. .cpb = MAX_CPB_SIZE_V6,
  1207. .priv = &mfc_buf_size_v6,
  1208. };
  1209. struct s5p_mfc_buf_align mfc_buf_align_v6 = {
  1210. .base = 0,
  1211. };
  1212. static struct s5p_mfc_variant mfc_drvdata_v6 = {
  1213. .version = MFC_VERSION_V6,
  1214. .port_num = MFC_NUM_PORTS_V6,
  1215. .buf_size = &buf_size_v6,
  1216. .buf_align = &mfc_buf_align_v6,
  1217. .mclk_name = "aclk_333",
  1218. .fw_name = "s5p-mfc-v6.fw",
  1219. };
  1220. static struct platform_device_id mfc_driver_ids[] = {
  1221. {
  1222. .name = "s5p-mfc",
  1223. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1224. }, {
  1225. .name = "s5p-mfc-v5",
  1226. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1227. }, {
  1228. .name = "s5p-mfc-v6",
  1229. .driver_data = (unsigned long)&mfc_drvdata_v6,
  1230. },
  1231. {},
  1232. };
  1233. MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
  1234. static struct platform_driver s5p_mfc_driver = {
  1235. .probe = s5p_mfc_probe,
  1236. .remove = __devexit_p(s5p_mfc_remove),
  1237. .id_table = mfc_driver_ids,
  1238. .driver = {
  1239. .name = S5P_MFC_NAME,
  1240. .owner = THIS_MODULE,
  1241. .pm = &s5p_mfc_pm_ops
  1242. },
  1243. };
  1244. module_platform_driver(s5p_mfc_driver);
  1245. MODULE_LICENSE("GPL");
  1246. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1247. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");