s5p_mfc.c 40 KB

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