s5p_mfc.c 40 KB

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