s5p_mfc.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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 mfc_debug_level;
  38. module_param_named(debug, mfc_debug_level, 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_void(dev->mfc_ops, cleanup_queue,
  133. &ctx->dst_queue, &ctx->vq_dst);
  134. s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
  135. &ctx->src_queue, &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_load_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 void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
  159. {
  160. mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
  161. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
  162. mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
  163. }
  164. static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
  165. {
  166. struct s5p_mfc_buf *dst_buf;
  167. struct s5p_mfc_dev *dev = ctx->dev;
  168. ctx->state = MFCINST_FINISHED;
  169. ctx->sequence++;
  170. while (!list_empty(&ctx->dst_queue)) {
  171. dst_buf = list_entry(ctx->dst_queue.next,
  172. struct s5p_mfc_buf, list);
  173. mfc_debug(2, "Cleaning up buffer: %d\n",
  174. dst_buf->b->v4l2_buf.index);
  175. vb2_set_plane_payload(dst_buf->b, 0, 0);
  176. vb2_set_plane_payload(dst_buf->b, 1, 0);
  177. list_del(&dst_buf->list);
  178. ctx->dst_queue_cnt--;
  179. dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
  180. if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
  181. s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
  182. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  183. else
  184. dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
  185. ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
  186. vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
  187. }
  188. }
  189. static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
  190. {
  191. struct s5p_mfc_dev *dev = ctx->dev;
  192. struct s5p_mfc_buf *dst_buf, *src_buf;
  193. size_t dec_y_addr;
  194. unsigned int frame_type;
  195. dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
  196. frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
  197. /* Copy timestamp / timecode from decoded src to dst and set
  198. appropriate flags */
  199. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  200. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  201. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
  202. dst_buf->b->v4l2_buf.timecode =
  203. src_buf->b->v4l2_buf.timecode;
  204. dst_buf->b->v4l2_buf.timestamp =
  205. src_buf->b->v4l2_buf.timestamp;
  206. dst_buf->b->v4l2_buf.flags &=
  207. ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  208. dst_buf->b->v4l2_buf.flags |=
  209. src_buf->b->v4l2_buf.flags
  210. & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  211. switch (frame_type) {
  212. case S5P_FIMV_DECODE_FRAME_I_FRAME:
  213. dst_buf->b->v4l2_buf.flags |=
  214. V4L2_BUF_FLAG_KEYFRAME;
  215. break;
  216. case S5P_FIMV_DECODE_FRAME_P_FRAME:
  217. dst_buf->b->v4l2_buf.flags |=
  218. V4L2_BUF_FLAG_PFRAME;
  219. break;
  220. case S5P_FIMV_DECODE_FRAME_B_FRAME:
  221. dst_buf->b->v4l2_buf.flags |=
  222. V4L2_BUF_FLAG_BFRAME;
  223. break;
  224. }
  225. break;
  226. }
  227. }
  228. }
  229. static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
  230. {
  231. struct s5p_mfc_dev *dev = ctx->dev;
  232. struct s5p_mfc_buf *dst_buf;
  233. size_t dspl_y_addr;
  234. unsigned int frame_type;
  235. dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
  236. if (IS_MFCV6_PLUS(dev))
  237. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  238. get_disp_frame_type, ctx);
  239. else
  240. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  241. get_dec_frame_type, dev);
  242. /* If frame is same as previous then skip and do not dequeue */
  243. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
  244. if (!ctx->after_packed_pb)
  245. ctx->sequence++;
  246. ctx->after_packed_pb = 0;
  247. return;
  248. }
  249. ctx->sequence++;
  250. /* The MFC returns address of the buffer, now we have to
  251. * check which videobuf does it correspond to */
  252. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  253. /* Check if this is the buffer we're looking for */
  254. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
  255. list_del(&dst_buf->list);
  256. ctx->dst_queue_cnt--;
  257. dst_buf->b->v4l2_buf.sequence = ctx->sequence;
  258. if (s5p_mfc_hw_call(dev->mfc_ops,
  259. get_pic_type_top, ctx) ==
  260. s5p_mfc_hw_call(dev->mfc_ops,
  261. get_pic_type_bot, ctx))
  262. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  263. else
  264. dst_buf->b->v4l2_buf.field =
  265. V4L2_FIELD_INTERLACED;
  266. vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
  267. vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
  268. clear_bit(dst_buf->b->v4l2_buf.index,
  269. &ctx->dec_dst_flag);
  270. vb2_buffer_done(dst_buf->b,
  271. err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  272. break;
  273. }
  274. }
  275. }
  276. /* Handle frame decoding interrupt */
  277. static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
  278. unsigned int reason, unsigned int err)
  279. {
  280. struct s5p_mfc_dev *dev = ctx->dev;
  281. unsigned int dst_frame_status;
  282. unsigned int dec_frame_status;
  283. struct s5p_mfc_buf *src_buf;
  284. unsigned long flags;
  285. unsigned int res_change;
  286. dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  287. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  288. dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
  289. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  290. res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  291. & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
  292. >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
  293. mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
  294. if (ctx->state == MFCINST_RES_CHANGE_INIT)
  295. ctx->state = MFCINST_RES_CHANGE_FLUSH;
  296. if (res_change == S5P_FIMV_RES_INCREASE ||
  297. res_change == S5P_FIMV_RES_DECREASE) {
  298. ctx->state = MFCINST_RES_CHANGE_INIT;
  299. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  300. wake_up_ctx(ctx, reason, err);
  301. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  302. BUG();
  303. s5p_mfc_clock_off();
  304. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  305. return;
  306. }
  307. if (ctx->dpb_flush_flag)
  308. ctx->dpb_flush_flag = 0;
  309. spin_lock_irqsave(&dev->irqlock, flags);
  310. /* All frames remaining in the buffer have been extracted */
  311. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
  312. if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
  313. static const struct v4l2_event ev_src_ch = {
  314. .type = V4L2_EVENT_SOURCE_CHANGE,
  315. .u.src_change.changes =
  316. V4L2_EVENT_SRC_CH_RESOLUTION,
  317. };
  318. s5p_mfc_handle_frame_all_extracted(ctx);
  319. ctx->state = MFCINST_RES_CHANGE_END;
  320. v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
  321. goto leave_handle_frame;
  322. } else {
  323. s5p_mfc_handle_frame_all_extracted(ctx);
  324. }
  325. }
  326. if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
  327. s5p_mfc_handle_frame_copy_time(ctx);
  328. /* A frame has been decoded and is in the buffer */
  329. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
  330. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
  331. s5p_mfc_handle_frame_new(ctx, err);
  332. } else {
  333. mfc_debug(2, "No frame decode\n");
  334. }
  335. /* Mark source buffer as complete */
  336. if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
  337. && !list_empty(&ctx->src_queue)) {
  338. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  339. list);
  340. ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
  341. get_consumed_stream, dev);
  342. if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
  343. ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
  344. ctx->consumed_stream + STUFF_BYTE <
  345. src_buf->b->v4l2_planes[0].bytesused) {
  346. /* Run MFC again on the same buffer */
  347. mfc_debug(2, "Running again the same buffer\n");
  348. ctx->after_packed_pb = 1;
  349. } else {
  350. mfc_debug(2, "MFC needs next buffer\n");
  351. ctx->consumed_stream = 0;
  352. if (src_buf->flags & MFC_BUF_FLAG_EOS)
  353. ctx->state = MFCINST_FINISHING;
  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->pb_count)
  366. clear_work_bit(ctx);
  367. s5p_mfc_hw_call_void(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. /* if suspending, wake up device and do not try_run again*/
  373. if (test_bit(0, &dev->enter_suspend))
  374. wake_up_dev(dev, reason, err);
  375. else
  376. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  377. }
  378. /* Error handling for interrupt */
  379. static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
  380. struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
  381. {
  382. unsigned long flags;
  383. mfc_err("Interrupt Error: %08x\n", err);
  384. if (ctx != NULL) {
  385. /* Error recovery is dependent on the state of context */
  386. switch (ctx->state) {
  387. case MFCINST_RES_CHANGE_INIT:
  388. case MFCINST_RES_CHANGE_FLUSH:
  389. case MFCINST_RES_CHANGE_END:
  390. case MFCINST_FINISHING:
  391. case MFCINST_FINISHED:
  392. case MFCINST_RUNNING:
  393. /* It is highly probable that an error occurred
  394. * while decoding a frame */
  395. clear_work_bit(ctx);
  396. ctx->state = MFCINST_ERROR;
  397. /* Mark all dst buffers as having an error */
  398. spin_lock_irqsave(&dev->irqlock, flags);
  399. s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
  400. &ctx->dst_queue, &ctx->vq_dst);
  401. /* Mark all src buffers as having an error */
  402. s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
  403. &ctx->src_queue, &ctx->vq_src);
  404. spin_unlock_irqrestore(&dev->irqlock, flags);
  405. wake_up_ctx(ctx, reason, err);
  406. break;
  407. default:
  408. clear_work_bit(ctx);
  409. ctx->state = MFCINST_ERROR;
  410. wake_up_ctx(ctx, reason, err);
  411. break;
  412. }
  413. }
  414. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  415. BUG();
  416. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  417. s5p_mfc_clock_off();
  418. wake_up_dev(dev, reason, err);
  419. return;
  420. }
  421. /* Header parsing interrupt handling */
  422. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  423. unsigned int reason, unsigned int err)
  424. {
  425. struct s5p_mfc_dev *dev;
  426. if (ctx == NULL)
  427. return;
  428. dev = ctx->dev;
  429. if (ctx->c_ops->post_seq_start) {
  430. if (ctx->c_ops->post_seq_start(ctx))
  431. mfc_err("post_seq_start() failed\n");
  432. } else {
  433. ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
  434. dev);
  435. ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
  436. dev);
  437. s5p_mfc_hw_call_void(dev->mfc_ops, dec_calc_dpb_size, ctx);
  438. ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
  439. dev);
  440. ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
  441. dev);
  442. if (ctx->img_width == 0 || ctx->img_height == 0)
  443. ctx->state = MFCINST_ERROR;
  444. else
  445. ctx->state = MFCINST_HEAD_PARSED;
  446. if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  447. ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
  448. !list_empty(&ctx->src_queue)) {
  449. struct s5p_mfc_buf *src_buf;
  450. src_buf = list_entry(ctx->src_queue.next,
  451. struct s5p_mfc_buf, list);
  452. if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
  453. dev) <
  454. src_buf->b->v4l2_planes[0].bytesused)
  455. ctx->head_processed = 0;
  456. else
  457. ctx->head_processed = 1;
  458. } else {
  459. ctx->head_processed = 1;
  460. }
  461. }
  462. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  463. clear_work_bit(ctx);
  464. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  465. BUG();
  466. s5p_mfc_clock_off();
  467. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  468. wake_up_ctx(ctx, reason, err);
  469. }
  470. /* Header parsing interrupt handling */
  471. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  472. unsigned int reason, unsigned int err)
  473. {
  474. struct s5p_mfc_buf *src_buf;
  475. struct s5p_mfc_dev *dev;
  476. unsigned long flags;
  477. if (ctx == NULL)
  478. return;
  479. dev = ctx->dev;
  480. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  481. ctx->int_type = reason;
  482. ctx->int_err = err;
  483. ctx->int_cond = 1;
  484. clear_work_bit(ctx);
  485. if (err == 0) {
  486. ctx->state = MFCINST_RUNNING;
  487. if (!ctx->dpb_flush_flag && ctx->head_processed) {
  488. spin_lock_irqsave(&dev->irqlock, flags);
  489. if (!list_empty(&ctx->src_queue)) {
  490. src_buf = list_entry(ctx->src_queue.next,
  491. struct s5p_mfc_buf, list);
  492. list_del(&src_buf->list);
  493. ctx->src_queue_cnt--;
  494. vb2_buffer_done(src_buf->b,
  495. VB2_BUF_STATE_DONE);
  496. }
  497. spin_unlock_irqrestore(&dev->irqlock, flags);
  498. } else {
  499. ctx->dpb_flush_flag = 0;
  500. }
  501. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  502. BUG();
  503. s5p_mfc_clock_off();
  504. wake_up(&ctx->queue);
  505. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  506. } else {
  507. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  508. BUG();
  509. s5p_mfc_clock_off();
  510. wake_up(&ctx->queue);
  511. }
  512. }
  513. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
  514. unsigned int reason, unsigned int err)
  515. {
  516. struct s5p_mfc_dev *dev = ctx->dev;
  517. struct s5p_mfc_buf *mb_entry;
  518. mfc_debug(2, "Stream completed\n");
  519. s5p_mfc_clear_int_flags(dev);
  520. ctx->int_type = reason;
  521. ctx->int_err = err;
  522. ctx->state = MFCINST_FINISHED;
  523. spin_lock(&dev->irqlock);
  524. if (!list_empty(&ctx->dst_queue)) {
  525. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  526. list);
  527. list_del(&mb_entry->list);
  528. ctx->dst_queue_cnt--;
  529. vb2_set_plane_payload(mb_entry->b, 0, 0);
  530. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  531. }
  532. spin_unlock(&dev->irqlock);
  533. clear_work_bit(ctx);
  534. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  535. s5p_mfc_clock_off();
  536. wake_up(&ctx->queue);
  537. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  538. }
  539. /* Interrupt processing */
  540. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  541. {
  542. struct s5p_mfc_dev *dev = priv;
  543. struct s5p_mfc_ctx *ctx;
  544. unsigned int reason;
  545. unsigned int err;
  546. mfc_debug_enter();
  547. /* Reset the timeout watchdog */
  548. atomic_set(&dev->watchdog_cnt, 0);
  549. ctx = dev->ctx[dev->curr_ctx];
  550. /* Get the reason of interrupt and the error code */
  551. reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
  552. err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
  553. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  554. switch (reason) {
  555. case S5P_MFC_R2H_CMD_ERR_RET:
  556. /* An error has occurred */
  557. if (ctx->state == MFCINST_RUNNING &&
  558. s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
  559. dev->warn_start)
  560. s5p_mfc_handle_frame(ctx, reason, err);
  561. else
  562. s5p_mfc_handle_error(dev, ctx, reason, err);
  563. clear_bit(0, &dev->enter_suspend);
  564. break;
  565. case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
  566. case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
  567. case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
  568. if (ctx->c_ops->post_frame_start) {
  569. if (ctx->c_ops->post_frame_start(ctx))
  570. mfc_err("post_frame_start() failed\n");
  571. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  572. wake_up_ctx(ctx, reason, err);
  573. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  574. BUG();
  575. s5p_mfc_clock_off();
  576. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  577. } else {
  578. s5p_mfc_handle_frame(ctx, reason, err);
  579. }
  580. break;
  581. case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
  582. s5p_mfc_handle_seq_done(ctx, reason, err);
  583. break;
  584. case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
  585. ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
  586. ctx->state = MFCINST_GOT_INST;
  587. clear_work_bit(ctx);
  588. wake_up(&ctx->queue);
  589. goto irq_cleanup_hw;
  590. case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
  591. clear_work_bit(ctx);
  592. ctx->inst_no = MFC_NO_INSTANCE_SET;
  593. ctx->state = MFCINST_FREE;
  594. wake_up(&ctx->queue);
  595. goto irq_cleanup_hw;
  596. case S5P_MFC_R2H_CMD_SYS_INIT_RET:
  597. case S5P_MFC_R2H_CMD_FW_STATUS_RET:
  598. case S5P_MFC_R2H_CMD_SLEEP_RET:
  599. case S5P_MFC_R2H_CMD_WAKEUP_RET:
  600. if (ctx)
  601. clear_work_bit(ctx);
  602. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  603. wake_up_dev(dev, reason, err);
  604. clear_bit(0, &dev->hw_lock);
  605. clear_bit(0, &dev->enter_suspend);
  606. break;
  607. case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
  608. s5p_mfc_handle_init_buffers(ctx, reason, err);
  609. break;
  610. case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
  611. s5p_mfc_handle_stream_complete(ctx, reason, err);
  612. break;
  613. case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
  614. clear_work_bit(ctx);
  615. ctx->state = MFCINST_RUNNING;
  616. wake_up(&ctx->queue);
  617. goto irq_cleanup_hw;
  618. default:
  619. mfc_debug(2, "Unknown int reason\n");
  620. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  621. }
  622. mfc_debug_leave();
  623. return IRQ_HANDLED;
  624. irq_cleanup_hw:
  625. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  626. ctx->int_type = reason;
  627. ctx->int_err = err;
  628. ctx->int_cond = 1;
  629. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  630. mfc_err("Failed to unlock hw\n");
  631. s5p_mfc_clock_off();
  632. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  633. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  634. return IRQ_HANDLED;
  635. }
  636. /* Open an MFC node */
  637. static int s5p_mfc_open(struct file *file)
  638. {
  639. struct video_device *vdev = video_devdata(file);
  640. struct s5p_mfc_dev *dev = video_drvdata(file);
  641. struct s5p_mfc_ctx *ctx = NULL;
  642. struct vb2_queue *q;
  643. int ret = 0;
  644. mfc_debug_enter();
  645. if (mutex_lock_interruptible(&dev->mfc_mutex))
  646. return -ERESTARTSYS;
  647. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  648. /* Allocate memory for context */
  649. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  650. if (!ctx) {
  651. mfc_err("Not enough memory\n");
  652. ret = -ENOMEM;
  653. goto err_alloc;
  654. }
  655. v4l2_fh_init(&ctx->fh, vdev);
  656. file->private_data = &ctx->fh;
  657. v4l2_fh_add(&ctx->fh);
  658. ctx->dev = dev;
  659. INIT_LIST_HEAD(&ctx->src_queue);
  660. INIT_LIST_HEAD(&ctx->dst_queue);
  661. ctx->src_queue_cnt = 0;
  662. ctx->dst_queue_cnt = 0;
  663. /* Get context number */
  664. ctx->num = 0;
  665. while (dev->ctx[ctx->num]) {
  666. ctx->num++;
  667. if (ctx->num >= MFC_NUM_CONTEXTS) {
  668. mfc_err("Too many open contexts\n");
  669. ret = -EBUSY;
  670. goto err_no_ctx;
  671. }
  672. }
  673. /* Mark context as idle */
  674. clear_work_bit_irqsave(ctx);
  675. dev->ctx[ctx->num] = ctx;
  676. if (vdev == dev->vfd_dec) {
  677. ctx->type = MFCINST_DECODER;
  678. ctx->c_ops = get_dec_codec_ops();
  679. s5p_mfc_dec_init(ctx);
  680. /* Setup ctrl handler */
  681. ret = s5p_mfc_dec_ctrls_setup(ctx);
  682. if (ret) {
  683. mfc_err("Failed to setup mfc controls\n");
  684. goto err_ctrls_setup;
  685. }
  686. } else if (vdev == dev->vfd_enc) {
  687. ctx->type = MFCINST_ENCODER;
  688. ctx->c_ops = get_enc_codec_ops();
  689. /* only for encoder */
  690. INIT_LIST_HEAD(&ctx->ref_queue);
  691. ctx->ref_queue_cnt = 0;
  692. s5p_mfc_enc_init(ctx);
  693. /* Setup ctrl handler */
  694. ret = s5p_mfc_enc_ctrls_setup(ctx);
  695. if (ret) {
  696. mfc_err("Failed to setup mfc controls\n");
  697. goto err_ctrls_setup;
  698. }
  699. } else {
  700. ret = -ENOENT;
  701. goto err_bad_node;
  702. }
  703. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  704. ctx->inst_no = MFC_NO_INSTANCE_SET;
  705. /* Load firmware if this is the first instance */
  706. if (dev->num_inst == 1) {
  707. dev->watchdog_timer.expires = jiffies +
  708. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  709. add_timer(&dev->watchdog_timer);
  710. ret = s5p_mfc_power_on();
  711. if (ret < 0) {
  712. mfc_err("power on failed\n");
  713. goto err_pwr_enable;
  714. }
  715. s5p_mfc_clock_on();
  716. ret = s5p_mfc_load_firmware(dev);
  717. if (ret) {
  718. s5p_mfc_clock_off();
  719. goto err_load_fw;
  720. }
  721. /* Init the FW */
  722. ret = s5p_mfc_init_hw(dev);
  723. s5p_mfc_clock_off();
  724. if (ret)
  725. goto err_init_hw;
  726. }
  727. /* Init videobuf2 queue for CAPTURE */
  728. q = &ctx->vq_dst;
  729. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  730. q->drv_priv = &ctx->fh;
  731. if (vdev == dev->vfd_dec) {
  732. q->io_modes = VB2_MMAP;
  733. q->ops = get_dec_queue_ops();
  734. } else if (vdev == dev->vfd_enc) {
  735. q->io_modes = VB2_MMAP | VB2_USERPTR;
  736. q->ops = get_enc_queue_ops();
  737. } else {
  738. ret = -ENOENT;
  739. goto err_queue_init;
  740. }
  741. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  742. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  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 (vdev == dev->vfd_dec) {
  754. q->io_modes = VB2_MMAP;
  755. q->ops = get_dec_queue_ops();
  756. } else if (vdev == dev->vfd_enc) {
  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. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  765. ret = vb2_queue_init(q);
  766. if (ret) {
  767. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  768. goto err_queue_init;
  769. }
  770. init_waitqueue_head(&ctx->queue);
  771. mutex_unlock(&dev->mfc_mutex);
  772. mfc_debug_leave();
  773. return ret;
  774. /* Deinit when failure occurred */
  775. err_queue_init:
  776. if (dev->num_inst == 1)
  777. s5p_mfc_deinit_hw(dev);
  778. err_init_hw:
  779. err_load_fw:
  780. err_pwr_enable:
  781. if (dev->num_inst == 1) {
  782. if (s5p_mfc_power_off() < 0)
  783. mfc_err("power off failed\n");
  784. del_timer_sync(&dev->watchdog_timer);
  785. }
  786. err_ctrls_setup:
  787. s5p_mfc_dec_ctrls_delete(ctx);
  788. err_bad_node:
  789. dev->ctx[ctx->num] = NULL;
  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 and not yet freed,
  813. * return instance and free resources */
  814. if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
  815. mfc_debug(2, "Has to free instance\n");
  816. s5p_mfc_close_mfc_inst(dev, ctx);
  817. }
  818. /* hardware locking scheme */
  819. if (dev->curr_ctx == ctx->num)
  820. clear_bit(0, &dev->hw_lock);
  821. dev->num_inst--;
  822. if (dev->num_inst == 0) {
  823. mfc_debug(2, "Last instance\n");
  824. s5p_mfc_deinit_hw(dev);
  825. del_timer_sync(&dev->watchdog_timer);
  826. if (s5p_mfc_power_off() < 0)
  827. mfc_err("Power off failed\n");
  828. }
  829. mfc_debug(2, "Shutting down clock\n");
  830. s5p_mfc_clock_off();
  831. dev->ctx[ctx->num] = NULL;
  832. s5p_mfc_dec_ctrls_delete(ctx);
  833. v4l2_fh_del(&ctx->fh);
  834. v4l2_fh_exit(&ctx->fh);
  835. kfree(ctx);
  836. mfc_debug_leave();
  837. mutex_unlock(&dev->mfc_mutex);
  838. return 0;
  839. }
  840. /* Poll */
  841. static unsigned int s5p_mfc_poll(struct file *file,
  842. struct poll_table_struct *wait)
  843. {
  844. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  845. struct s5p_mfc_dev *dev = ctx->dev;
  846. struct vb2_queue *src_q, *dst_q;
  847. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  848. unsigned int rc = 0;
  849. unsigned long flags;
  850. mutex_lock(&dev->mfc_mutex);
  851. src_q = &ctx->vq_src;
  852. dst_q = &ctx->vq_dst;
  853. /*
  854. * There has to be at least one buffer queued on each queued_list, which
  855. * means either in driver already or waiting for driver to claim it
  856. * and start processing.
  857. */
  858. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  859. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  860. rc = POLLERR;
  861. goto end;
  862. }
  863. mutex_unlock(&dev->mfc_mutex);
  864. poll_wait(file, &ctx->fh.wait, wait);
  865. poll_wait(file, &src_q->done_wq, wait);
  866. poll_wait(file, &dst_q->done_wq, wait);
  867. mutex_lock(&dev->mfc_mutex);
  868. if (v4l2_event_pending(&ctx->fh))
  869. rc |= POLLPRI;
  870. spin_lock_irqsave(&src_q->done_lock, flags);
  871. if (!list_empty(&src_q->done_list))
  872. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  873. done_entry);
  874. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  875. || src_vb->state == VB2_BUF_STATE_ERROR))
  876. rc |= POLLOUT | POLLWRNORM;
  877. spin_unlock_irqrestore(&src_q->done_lock, flags);
  878. spin_lock_irqsave(&dst_q->done_lock, flags);
  879. if (!list_empty(&dst_q->done_list))
  880. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  881. done_entry);
  882. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  883. || dst_vb->state == VB2_BUF_STATE_ERROR))
  884. rc |= POLLIN | POLLRDNORM;
  885. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  886. end:
  887. mutex_unlock(&dev->mfc_mutex);
  888. return rc;
  889. }
  890. /* Mmap */
  891. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  892. {
  893. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  894. struct s5p_mfc_dev *dev = ctx->dev;
  895. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  896. int ret;
  897. if (mutex_lock_interruptible(&dev->mfc_mutex))
  898. return -ERESTARTSYS;
  899. if (offset < DST_QUEUE_OFF_BASE) {
  900. mfc_debug(2, "mmaping source\n");
  901. ret = vb2_mmap(&ctx->vq_src, vma);
  902. } else { /* capture */
  903. mfc_debug(2, "mmaping destination\n");
  904. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  905. ret = vb2_mmap(&ctx->vq_dst, vma);
  906. }
  907. mutex_unlock(&dev->mfc_mutex);
  908. return ret;
  909. }
  910. /* v4l2 ops */
  911. static const struct v4l2_file_operations s5p_mfc_fops = {
  912. .owner = THIS_MODULE,
  913. .open = s5p_mfc_open,
  914. .release = s5p_mfc_release,
  915. .poll = s5p_mfc_poll,
  916. .unlocked_ioctl = video_ioctl2,
  917. .mmap = s5p_mfc_mmap,
  918. };
  919. static int match_child(struct device *dev, void *data)
  920. {
  921. if (!dev_name(dev))
  922. return 0;
  923. return !strcmp(dev_name(dev), (char *)data);
  924. }
  925. static void *mfc_get_drv_data(struct platform_device *pdev);
  926. static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev *dev)
  927. {
  928. unsigned int mem_info[2] = { };
  929. dev->mem_dev_l = devm_kzalloc(&dev->plat_dev->dev,
  930. sizeof(struct device), GFP_KERNEL);
  931. if (!dev->mem_dev_l) {
  932. mfc_err("Not enough memory\n");
  933. return -ENOMEM;
  934. }
  935. device_initialize(dev->mem_dev_l);
  936. of_property_read_u32_array(dev->plat_dev->dev.of_node,
  937. "samsung,mfc-l", mem_info, 2);
  938. if (dma_declare_coherent_memory(dev->mem_dev_l, mem_info[0],
  939. mem_info[0], mem_info[1],
  940. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
  941. mfc_err("Failed to declare coherent memory for\n"
  942. "MFC device\n");
  943. return -ENOMEM;
  944. }
  945. dev->mem_dev_r = devm_kzalloc(&dev->plat_dev->dev,
  946. sizeof(struct device), GFP_KERNEL);
  947. if (!dev->mem_dev_r) {
  948. mfc_err("Not enough memory\n");
  949. return -ENOMEM;
  950. }
  951. device_initialize(dev->mem_dev_r);
  952. of_property_read_u32_array(dev->plat_dev->dev.of_node,
  953. "samsung,mfc-r", mem_info, 2);
  954. if (dma_declare_coherent_memory(dev->mem_dev_r, mem_info[0],
  955. mem_info[0], mem_info[1],
  956. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
  957. pr_err("Failed to declare coherent memory for\n"
  958. "MFC device\n");
  959. return -ENOMEM;
  960. }
  961. return 0;
  962. }
  963. /* MFC probe function */
  964. static int s5p_mfc_probe(struct platform_device *pdev)
  965. {
  966. struct s5p_mfc_dev *dev;
  967. struct video_device *vfd;
  968. struct resource *res;
  969. int ret;
  970. pr_debug("%s++\n", __func__);
  971. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  972. if (!dev) {
  973. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  974. return -ENOMEM;
  975. }
  976. spin_lock_init(&dev->irqlock);
  977. spin_lock_init(&dev->condlock);
  978. dev->plat_dev = pdev;
  979. if (!dev->plat_dev) {
  980. dev_err(&pdev->dev, "No platform data specified\n");
  981. return -ENODEV;
  982. }
  983. dev->variant = mfc_get_drv_data(pdev);
  984. ret = s5p_mfc_init_pm(dev);
  985. if (ret < 0) {
  986. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  987. return ret;
  988. }
  989. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  990. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  991. if (IS_ERR(dev->regs_base))
  992. return PTR_ERR(dev->regs_base);
  993. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  994. if (res == NULL) {
  995. dev_err(&pdev->dev, "failed to get irq resource\n");
  996. ret = -ENOENT;
  997. goto err_res;
  998. }
  999. dev->irq = res->start;
  1000. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  1001. 0, pdev->name, dev);
  1002. if (ret) {
  1003. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  1004. goto err_res;
  1005. }
  1006. if (pdev->dev.of_node) {
  1007. ret = s5p_mfc_alloc_memdevs(dev);
  1008. if (ret < 0)
  1009. goto err_res;
  1010. } else {
  1011. dev->mem_dev_l = device_find_child(&dev->plat_dev->dev,
  1012. "s5p-mfc-l", match_child);
  1013. if (!dev->mem_dev_l) {
  1014. mfc_err("Mem child (L) device get failed\n");
  1015. ret = -ENODEV;
  1016. goto err_res;
  1017. }
  1018. dev->mem_dev_r = device_find_child(&dev->plat_dev->dev,
  1019. "s5p-mfc-r", match_child);
  1020. if (!dev->mem_dev_r) {
  1021. mfc_err("Mem child (R) device get failed\n");
  1022. ret = -ENODEV;
  1023. goto err_res;
  1024. }
  1025. }
  1026. dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
  1027. if (IS_ERR(dev->alloc_ctx[0])) {
  1028. ret = PTR_ERR(dev->alloc_ctx[0]);
  1029. goto err_res;
  1030. }
  1031. dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
  1032. if (IS_ERR(dev->alloc_ctx[1])) {
  1033. ret = PTR_ERR(dev->alloc_ctx[1]);
  1034. goto err_mem_init_ctx_1;
  1035. }
  1036. mutex_init(&dev->mfc_mutex);
  1037. ret = s5p_mfc_alloc_firmware(dev);
  1038. if (ret)
  1039. goto err_alloc_fw;
  1040. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1041. if (ret)
  1042. goto err_v4l2_dev_reg;
  1043. init_waitqueue_head(&dev->queue);
  1044. /* decoder */
  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_dec_alloc;
  1050. }
  1051. vfd->fops = &s5p_mfc_fops;
  1052. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1053. vfd->release = video_device_release;
  1054. vfd->lock = &dev->mfc_mutex;
  1055. vfd->v4l2_dev = &dev->v4l2_dev;
  1056. vfd->vfl_dir = VFL_DIR_M2M;
  1057. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1058. dev->vfd_dec = vfd;
  1059. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1060. if (ret) {
  1061. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1062. video_device_release(vfd);
  1063. goto err_dec_reg;
  1064. }
  1065. v4l2_info(&dev->v4l2_dev,
  1066. "decoder registered as /dev/video%d\n", vfd->num);
  1067. video_set_drvdata(vfd, dev);
  1068. /* encoder */
  1069. vfd = video_device_alloc();
  1070. if (!vfd) {
  1071. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1072. ret = -ENOMEM;
  1073. goto err_enc_alloc;
  1074. }
  1075. vfd->fops = &s5p_mfc_fops;
  1076. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1077. vfd->release = video_device_release;
  1078. vfd->lock = &dev->mfc_mutex;
  1079. vfd->v4l2_dev = &dev->v4l2_dev;
  1080. vfd->vfl_dir = VFL_DIR_M2M;
  1081. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1082. dev->vfd_enc = vfd;
  1083. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1084. if (ret) {
  1085. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1086. video_device_release(vfd);
  1087. goto err_enc_reg;
  1088. }
  1089. v4l2_info(&dev->v4l2_dev,
  1090. "encoder registered as /dev/video%d\n", vfd->num);
  1091. video_set_drvdata(vfd, dev);
  1092. platform_set_drvdata(pdev, dev);
  1093. dev->hw_lock = 0;
  1094. dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
  1095. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1096. atomic_set(&dev->watchdog_cnt, 0);
  1097. init_timer(&dev->watchdog_timer);
  1098. dev->watchdog_timer.data = (unsigned long)dev;
  1099. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1100. /* Initialize HW ops and commands based on MFC version */
  1101. s5p_mfc_init_hw_ops(dev);
  1102. s5p_mfc_init_hw_cmds(dev);
  1103. s5p_mfc_init_regs(dev);
  1104. pr_debug("%s--\n", __func__);
  1105. return 0;
  1106. /* Deinit MFC if probe had failed */
  1107. err_enc_reg:
  1108. video_device_release(dev->vfd_enc);
  1109. err_enc_alloc:
  1110. video_unregister_device(dev->vfd_dec);
  1111. err_dec_reg:
  1112. video_device_release(dev->vfd_dec);
  1113. err_dec_alloc:
  1114. v4l2_device_unregister(&dev->v4l2_dev);
  1115. err_v4l2_dev_reg:
  1116. s5p_mfc_release_firmware(dev);
  1117. err_alloc_fw:
  1118. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1119. err_mem_init_ctx_1:
  1120. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1121. err_res:
  1122. s5p_mfc_final_pm(dev);
  1123. pr_debug("%s-- with error\n", __func__);
  1124. return ret;
  1125. }
  1126. /* Remove the driver */
  1127. static int s5p_mfc_remove(struct platform_device *pdev)
  1128. {
  1129. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1130. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1131. del_timer_sync(&dev->watchdog_timer);
  1132. flush_workqueue(dev->watchdog_workqueue);
  1133. destroy_workqueue(dev->watchdog_workqueue);
  1134. video_unregister_device(dev->vfd_enc);
  1135. video_unregister_device(dev->vfd_dec);
  1136. v4l2_device_unregister(&dev->v4l2_dev);
  1137. s5p_mfc_release_firmware(dev);
  1138. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1139. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1140. if (pdev->dev.of_node) {
  1141. put_device(dev->mem_dev_l);
  1142. put_device(dev->mem_dev_r);
  1143. }
  1144. s5p_mfc_final_pm(dev);
  1145. return 0;
  1146. }
  1147. #ifdef CONFIG_PM_SLEEP
  1148. static int s5p_mfc_suspend(struct device *dev)
  1149. {
  1150. struct platform_device *pdev = to_platform_device(dev);
  1151. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1152. int ret;
  1153. if (m_dev->num_inst == 0)
  1154. return 0;
  1155. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1156. mfc_err("Error: going to suspend for a second time\n");
  1157. return -EIO;
  1158. }
  1159. /* Check if we're processing then wait if it necessary. */
  1160. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1161. /* Try and lock the HW */
  1162. /* Wait on the interrupt waitqueue */
  1163. ret = wait_event_interruptible_timeout(m_dev->queue,
  1164. m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
  1165. if (ret == 0) {
  1166. mfc_err("Waiting for hardware to finish timed out\n");
  1167. return -EIO;
  1168. }
  1169. }
  1170. return s5p_mfc_sleep(m_dev);
  1171. }
  1172. static int s5p_mfc_resume(struct device *dev)
  1173. {
  1174. struct platform_device *pdev = to_platform_device(dev);
  1175. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1176. if (m_dev->num_inst == 0)
  1177. return 0;
  1178. return s5p_mfc_wakeup(m_dev);
  1179. }
  1180. #endif
  1181. #ifdef CONFIG_PM_RUNTIME
  1182. static int s5p_mfc_runtime_suspend(struct device *dev)
  1183. {
  1184. struct platform_device *pdev = to_platform_device(dev);
  1185. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1186. atomic_set(&m_dev->pm.power, 0);
  1187. return 0;
  1188. }
  1189. static int s5p_mfc_runtime_resume(struct device *dev)
  1190. {
  1191. struct platform_device *pdev = to_platform_device(dev);
  1192. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1193. if (!m_dev->alloc_ctx)
  1194. return 0;
  1195. atomic_set(&m_dev->pm.power, 1);
  1196. return 0;
  1197. }
  1198. #endif
  1199. /* Power management */
  1200. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1201. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1202. SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
  1203. NULL)
  1204. };
  1205. static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
  1206. .h264_ctx = MFC_H264_CTX_BUF_SIZE,
  1207. .non_h264_ctx = MFC_CTX_BUF_SIZE,
  1208. .dsc = DESC_BUF_SIZE,
  1209. .shm = SHARED_BUF_SIZE,
  1210. };
  1211. static struct s5p_mfc_buf_size buf_size_v5 = {
  1212. .fw = MAX_FW_SIZE,
  1213. .cpb = MAX_CPB_SIZE,
  1214. .priv = &mfc_buf_size_v5,
  1215. };
  1216. static struct s5p_mfc_buf_align mfc_buf_align_v5 = {
  1217. .base = MFC_BASE_ALIGN_ORDER,
  1218. };
  1219. static struct s5p_mfc_variant mfc_drvdata_v5 = {
  1220. .version = MFC_VERSION,
  1221. .version_bit = MFC_V5_BIT,
  1222. .port_num = MFC_NUM_PORTS,
  1223. .buf_size = &buf_size_v5,
  1224. .buf_align = &mfc_buf_align_v5,
  1225. .fw_name[0] = "s5p-mfc.fw",
  1226. };
  1227. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
  1228. .dev_ctx = MFC_CTX_BUF_SIZE_V6,
  1229. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
  1230. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
  1231. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
  1232. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
  1233. };
  1234. static struct s5p_mfc_buf_size buf_size_v6 = {
  1235. .fw = MAX_FW_SIZE_V6,
  1236. .cpb = MAX_CPB_SIZE_V6,
  1237. .priv = &mfc_buf_size_v6,
  1238. };
  1239. static struct s5p_mfc_buf_align mfc_buf_align_v6 = {
  1240. .base = 0,
  1241. };
  1242. static struct s5p_mfc_variant mfc_drvdata_v6 = {
  1243. .version = MFC_VERSION_V6,
  1244. .version_bit = MFC_V6_BIT,
  1245. .port_num = MFC_NUM_PORTS_V6,
  1246. .buf_size = &buf_size_v6,
  1247. .buf_align = &mfc_buf_align_v6,
  1248. .fw_name[0] = "s5p-mfc-v6.fw",
  1249. /*
  1250. * v6-v2 firmware contains bug fixes and interface change
  1251. * for init buffer command
  1252. */
  1253. .fw_name[1] = "s5p-mfc-v6-v2.fw",
  1254. };
  1255. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
  1256. .dev_ctx = MFC_CTX_BUF_SIZE_V7,
  1257. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
  1258. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
  1259. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
  1260. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
  1261. };
  1262. static struct s5p_mfc_buf_size buf_size_v7 = {
  1263. .fw = MAX_FW_SIZE_V7,
  1264. .cpb = MAX_CPB_SIZE_V7,
  1265. .priv = &mfc_buf_size_v7,
  1266. };
  1267. static struct s5p_mfc_buf_align mfc_buf_align_v7 = {
  1268. .base = 0,
  1269. };
  1270. static struct s5p_mfc_variant mfc_drvdata_v7 = {
  1271. .version = MFC_VERSION_V7,
  1272. .version_bit = MFC_V7_BIT,
  1273. .port_num = MFC_NUM_PORTS_V7,
  1274. .buf_size = &buf_size_v7,
  1275. .buf_align = &mfc_buf_align_v7,
  1276. .fw_name[0] = "s5p-mfc-v7.fw",
  1277. };
  1278. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
  1279. .dev_ctx = MFC_CTX_BUF_SIZE_V8,
  1280. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
  1281. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
  1282. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
  1283. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
  1284. };
  1285. static struct s5p_mfc_buf_size buf_size_v8 = {
  1286. .fw = MAX_FW_SIZE_V8,
  1287. .cpb = MAX_CPB_SIZE_V8,
  1288. .priv = &mfc_buf_size_v8,
  1289. };
  1290. static struct s5p_mfc_buf_align mfc_buf_align_v8 = {
  1291. .base = 0,
  1292. };
  1293. static struct s5p_mfc_variant mfc_drvdata_v8 = {
  1294. .version = MFC_VERSION_V8,
  1295. .version_bit = MFC_V8_BIT,
  1296. .port_num = MFC_NUM_PORTS_V8,
  1297. .buf_size = &buf_size_v8,
  1298. .buf_align = &mfc_buf_align_v8,
  1299. .fw_name[0] = "s5p-mfc-v8.fw",
  1300. };
  1301. static struct platform_device_id mfc_driver_ids[] = {
  1302. {
  1303. .name = "s5p-mfc",
  1304. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1305. }, {
  1306. .name = "s5p-mfc-v5",
  1307. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1308. }, {
  1309. .name = "s5p-mfc-v6",
  1310. .driver_data = (unsigned long)&mfc_drvdata_v6,
  1311. }, {
  1312. .name = "s5p-mfc-v7",
  1313. .driver_data = (unsigned long)&mfc_drvdata_v7,
  1314. }, {
  1315. .name = "s5p-mfc-v8",
  1316. .driver_data = (unsigned long)&mfc_drvdata_v8,
  1317. },
  1318. {},
  1319. };
  1320. MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
  1321. static const struct of_device_id exynos_mfc_match[] = {
  1322. {
  1323. .compatible = "samsung,mfc-v5",
  1324. .data = &mfc_drvdata_v5,
  1325. }, {
  1326. .compatible = "samsung,mfc-v6",
  1327. .data = &mfc_drvdata_v6,
  1328. }, {
  1329. .compatible = "samsung,mfc-v7",
  1330. .data = &mfc_drvdata_v7,
  1331. }, {
  1332. .compatible = "samsung,mfc-v8",
  1333. .data = &mfc_drvdata_v8,
  1334. },
  1335. {},
  1336. };
  1337. MODULE_DEVICE_TABLE(of, exynos_mfc_match);
  1338. static void *mfc_get_drv_data(struct platform_device *pdev)
  1339. {
  1340. struct s5p_mfc_variant *driver_data = NULL;
  1341. if (pdev->dev.of_node) {
  1342. const struct of_device_id *match;
  1343. match = of_match_node(exynos_mfc_match,
  1344. pdev->dev.of_node);
  1345. if (match)
  1346. driver_data = (struct s5p_mfc_variant *)match->data;
  1347. } else {
  1348. driver_data = (struct s5p_mfc_variant *)
  1349. platform_get_device_id(pdev)->driver_data;
  1350. }
  1351. return driver_data;
  1352. }
  1353. static struct platform_driver s5p_mfc_driver = {
  1354. .probe = s5p_mfc_probe,
  1355. .remove = s5p_mfc_remove,
  1356. .id_table = mfc_driver_ids,
  1357. .driver = {
  1358. .name = S5P_MFC_NAME,
  1359. .owner = THIS_MODULE,
  1360. .pm = &s5p_mfc_pm_ops,
  1361. .of_match_table = exynos_mfc_match,
  1362. },
  1363. };
  1364. module_platform_driver(s5p_mfc_driver);
  1365. MODULE_LICENSE("GPL");
  1366. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1367. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");