s5p_mfc.c 42 KB

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