s5p_mfc.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Samsung S5P Multi Format Codec v 5.1
  4. *
  5. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  6. * Kamil Debski, <k.debski@samsung.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/videodev2.h>
  17. #include <media/v4l2-event.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_reserved_mem.h>
  22. #include <media/videobuf2-v4l2.h>
  23. #include "s5p_mfc_common.h"
  24. #include "s5p_mfc_ctrl.h"
  25. #include "s5p_mfc_debug.h"
  26. #include "s5p_mfc_dec.h"
  27. #include "s5p_mfc_enc.h"
  28. #include "s5p_mfc_intr.h"
  29. #include "s5p_mfc_iommu.h"
  30. #include "s5p_mfc_opr.h"
  31. #include "s5p_mfc_cmd.h"
  32. #include "s5p_mfc_pm.h"
  33. #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
  34. #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
  35. int mfc_debug_level;
  36. module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
  37. MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
  38. static char *mfc_mem_size;
  39. module_param_named(mem, mfc_mem_size, charp, 0644);
  40. MODULE_PARM_DESC(mem, "Preallocated memory size for the firmware and context buffers");
  41. /* Helper functions for interrupt processing */
  42. /* Remove from hw execution round robin */
  43. void clear_work_bit(struct s5p_mfc_ctx *ctx)
  44. {
  45. struct s5p_mfc_dev *dev = ctx->dev;
  46. spin_lock(&dev->condlock);
  47. __clear_bit(ctx->num, &dev->ctx_work_bits);
  48. spin_unlock(&dev->condlock);
  49. }
  50. /* Add to hw execution round robin */
  51. void set_work_bit(struct s5p_mfc_ctx *ctx)
  52. {
  53. struct s5p_mfc_dev *dev = ctx->dev;
  54. spin_lock(&dev->condlock);
  55. __set_bit(ctx->num, &dev->ctx_work_bits);
  56. spin_unlock(&dev->condlock);
  57. }
  58. /* Remove from hw execution round robin */
  59. void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
  60. {
  61. struct s5p_mfc_dev *dev = ctx->dev;
  62. unsigned long flags;
  63. spin_lock_irqsave(&dev->condlock, flags);
  64. __clear_bit(ctx->num, &dev->ctx_work_bits);
  65. spin_unlock_irqrestore(&dev->condlock, flags);
  66. }
  67. /* Add to hw execution round robin */
  68. void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
  69. {
  70. struct s5p_mfc_dev *dev = ctx->dev;
  71. unsigned long flags;
  72. spin_lock_irqsave(&dev->condlock, flags);
  73. __set_bit(ctx->num, &dev->ctx_work_bits);
  74. spin_unlock_irqrestore(&dev->condlock, flags);
  75. }
  76. int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
  77. {
  78. unsigned long flags;
  79. int ctx;
  80. spin_lock_irqsave(&dev->condlock, flags);
  81. ctx = dev->curr_ctx;
  82. do {
  83. ctx = (ctx + 1) % MFC_NUM_CONTEXTS;
  84. if (ctx == dev->curr_ctx) {
  85. if (!test_bit(ctx, &dev->ctx_work_bits))
  86. ctx = -EAGAIN;
  87. break;
  88. }
  89. } while (!test_bit(ctx, &dev->ctx_work_bits));
  90. spin_unlock_irqrestore(&dev->condlock, flags);
  91. return ctx;
  92. }
  93. /* Wake up context wait_queue */
  94. static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
  95. unsigned int err)
  96. {
  97. ctx->int_cond = 1;
  98. ctx->int_type = reason;
  99. ctx->int_err = err;
  100. wake_up(&ctx->queue);
  101. }
  102. /* Wake up device wait_queue */
  103. static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
  104. unsigned int err)
  105. {
  106. dev->int_cond = 1;
  107. dev->int_type = reason;
  108. dev->int_err = err;
  109. wake_up(&dev->queue);
  110. }
  111. void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
  112. {
  113. struct s5p_mfc_buf *b;
  114. int i;
  115. while (!list_empty(lh)) {
  116. b = list_entry(lh->next, struct s5p_mfc_buf, list);
  117. for (i = 0; i < b->b->vb2_buf.num_planes; i++)
  118. vb2_set_plane_payload(&b->b->vb2_buf, i, 0);
  119. vb2_buffer_done(&b->b->vb2_buf, VB2_BUF_STATE_ERROR);
  120. list_del(&b->list);
  121. }
  122. }
  123. static void s5p_mfc_watchdog(struct timer_list *t)
  124. {
  125. struct s5p_mfc_dev *dev = from_timer(dev, t, watchdog_timer);
  126. if (test_bit(0, &dev->hw_lock))
  127. atomic_inc(&dev->watchdog_cnt);
  128. if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
  129. /* This means that hw is busy and no interrupts were
  130. * generated by hw for the Nth time of running this
  131. * watchdog timer. This usually means a serious hw
  132. * error. Now it is time to kill all instances and
  133. * reset the MFC. */
  134. mfc_err("Time out during waiting for HW\n");
  135. schedule_work(&dev->watchdog_work);
  136. }
  137. dev->watchdog_timer.expires = jiffies +
  138. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  139. add_timer(&dev->watchdog_timer);
  140. }
  141. static void s5p_mfc_watchdog_worker(struct work_struct *work)
  142. {
  143. struct s5p_mfc_dev *dev;
  144. struct s5p_mfc_ctx *ctx;
  145. unsigned long flags;
  146. int mutex_locked;
  147. int i, ret;
  148. dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
  149. mfc_err("Driver timeout error handling\n");
  150. /* Lock the mutex that protects open and release.
  151. * This is necessary as they may load and unload firmware. */
  152. mutex_locked = mutex_trylock(&dev->mfc_mutex);
  153. if (!mutex_locked)
  154. mfc_err("Error: some instance may be closing/opening\n");
  155. spin_lock_irqsave(&dev->irqlock, flags);
  156. s5p_mfc_clock_off();
  157. for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
  158. ctx = dev->ctx[i];
  159. if (!ctx)
  160. continue;
  161. ctx->state = MFCINST_ERROR;
  162. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  163. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  164. clear_work_bit(ctx);
  165. wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
  166. }
  167. clear_bit(0, &dev->hw_lock);
  168. spin_unlock_irqrestore(&dev->irqlock, flags);
  169. /* De-init MFC */
  170. s5p_mfc_deinit_hw(dev);
  171. /* Double check if there is at least one instance running.
  172. * If no instance is in memory than no firmware should be present */
  173. if (dev->num_inst > 0) {
  174. ret = s5p_mfc_load_firmware(dev);
  175. if (ret) {
  176. mfc_err("Failed to reload FW\n");
  177. goto unlock;
  178. }
  179. s5p_mfc_clock_on();
  180. ret = s5p_mfc_init_hw(dev);
  181. s5p_mfc_clock_off();
  182. if (ret)
  183. mfc_err("Failed to reinit FW\n");
  184. }
  185. unlock:
  186. if (mutex_locked)
  187. mutex_unlock(&dev->mfc_mutex);
  188. }
  189. static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
  190. {
  191. struct s5p_mfc_buf *dst_buf;
  192. struct s5p_mfc_dev *dev = ctx->dev;
  193. ctx->state = MFCINST_FINISHED;
  194. ctx->sequence++;
  195. while (!list_empty(&ctx->dst_queue)) {
  196. dst_buf = list_entry(ctx->dst_queue.next,
  197. struct s5p_mfc_buf, list);
  198. mfc_debug(2, "Cleaning up buffer: %d\n",
  199. dst_buf->b->vb2_buf.index);
  200. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
  201. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
  202. list_del(&dst_buf->list);
  203. dst_buf->flags |= MFC_BUF_FLAG_EOS;
  204. ctx->dst_queue_cnt--;
  205. dst_buf->b->sequence = (ctx->sequence++);
  206. if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
  207. s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
  208. dst_buf->b->field = V4L2_FIELD_NONE;
  209. else
  210. dst_buf->b->field = V4L2_FIELD_INTERLACED;
  211. dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
  212. ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
  213. vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
  214. }
  215. }
  216. static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
  217. {
  218. struct s5p_mfc_dev *dev = ctx->dev;
  219. struct s5p_mfc_buf *dst_buf, *src_buf;
  220. u32 dec_y_addr;
  221. unsigned int frame_type;
  222. /* Make sure we actually have a new frame before continuing. */
  223. frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
  224. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
  225. return;
  226. dec_y_addr = (u32)s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
  227. /* Copy timestamp / timecode from decoded src to dst and set
  228. appropriate flags. */
  229. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  230. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  231. u32 addr = (u32)vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0);
  232. if (addr == dec_y_addr) {
  233. dst_buf->b->timecode = src_buf->b->timecode;
  234. dst_buf->b->vb2_buf.timestamp =
  235. src_buf->b->vb2_buf.timestamp;
  236. dst_buf->b->flags &=
  237. ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  238. dst_buf->b->flags |=
  239. src_buf->b->flags
  240. & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  241. switch (frame_type) {
  242. case S5P_FIMV_DECODE_FRAME_I_FRAME:
  243. dst_buf->b->flags |=
  244. V4L2_BUF_FLAG_KEYFRAME;
  245. break;
  246. case S5P_FIMV_DECODE_FRAME_P_FRAME:
  247. dst_buf->b->flags |=
  248. V4L2_BUF_FLAG_PFRAME;
  249. break;
  250. case S5P_FIMV_DECODE_FRAME_B_FRAME:
  251. dst_buf->b->flags |=
  252. V4L2_BUF_FLAG_BFRAME;
  253. break;
  254. default:
  255. /* Don't know how to handle
  256. S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
  257. mfc_debug(2, "Unexpected frame type: %d\n",
  258. frame_type);
  259. }
  260. break;
  261. }
  262. }
  263. }
  264. static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
  265. {
  266. struct s5p_mfc_dev *dev = ctx->dev;
  267. struct s5p_mfc_buf *dst_buf;
  268. u32 dspl_y_addr;
  269. unsigned int frame_type;
  270. dspl_y_addr = (u32)s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
  271. if (IS_MFCV6_PLUS(dev))
  272. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  273. get_disp_frame_type, ctx);
  274. else
  275. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  276. get_dec_frame_type, dev);
  277. /* If frame is same as previous then skip and do not dequeue */
  278. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
  279. if (!ctx->after_packed_pb)
  280. ctx->sequence++;
  281. ctx->after_packed_pb = 0;
  282. return;
  283. }
  284. ctx->sequence++;
  285. /* The MFC returns address of the buffer, now we have to
  286. * check which videobuf does it correspond to */
  287. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  288. u32 addr = (u32)vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0);
  289. /* Check if this is the buffer we're looking for */
  290. if (addr == dspl_y_addr) {
  291. list_del(&dst_buf->list);
  292. ctx->dst_queue_cnt--;
  293. dst_buf->b->sequence = ctx->sequence;
  294. if (s5p_mfc_hw_call(dev->mfc_ops,
  295. get_pic_type_top, ctx) ==
  296. s5p_mfc_hw_call(dev->mfc_ops,
  297. get_pic_type_bot, ctx))
  298. dst_buf->b->field = V4L2_FIELD_NONE;
  299. else
  300. dst_buf->b->field =
  301. V4L2_FIELD_INTERLACED;
  302. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
  303. ctx->luma_size);
  304. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
  305. ctx->chroma_size);
  306. clear_bit(dst_buf->b->vb2_buf.index,
  307. &ctx->dec_dst_flag);
  308. vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
  309. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  310. break;
  311. }
  312. }
  313. }
  314. /* Handle frame decoding interrupt */
  315. static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
  316. unsigned int reason, unsigned int err)
  317. {
  318. struct s5p_mfc_dev *dev = ctx->dev;
  319. unsigned int dst_frame_status;
  320. unsigned int dec_frame_status;
  321. struct s5p_mfc_buf *src_buf;
  322. unsigned int res_change;
  323. dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  324. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  325. dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
  326. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  327. res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  328. & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
  329. >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
  330. mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
  331. if (ctx->state == MFCINST_RES_CHANGE_INIT)
  332. ctx->state = MFCINST_RES_CHANGE_FLUSH;
  333. if (res_change == S5P_FIMV_RES_INCREASE ||
  334. res_change == S5P_FIMV_RES_DECREASE) {
  335. ctx->state = MFCINST_RES_CHANGE_INIT;
  336. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  337. wake_up_ctx(ctx, reason, err);
  338. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  339. s5p_mfc_clock_off();
  340. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  341. return;
  342. }
  343. if (ctx->dpb_flush_flag)
  344. ctx->dpb_flush_flag = 0;
  345. /* All frames remaining in the buffer have been extracted */
  346. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
  347. if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
  348. static const struct v4l2_event ev_src_ch = {
  349. .type = V4L2_EVENT_SOURCE_CHANGE,
  350. .u.src_change.changes =
  351. V4L2_EVENT_SRC_CH_RESOLUTION,
  352. };
  353. s5p_mfc_handle_frame_all_extracted(ctx);
  354. ctx->state = MFCINST_RES_CHANGE_END;
  355. v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
  356. goto leave_handle_frame;
  357. } else {
  358. s5p_mfc_handle_frame_all_extracted(ctx);
  359. }
  360. }
  361. if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
  362. s5p_mfc_handle_frame_copy_time(ctx);
  363. /* A frame has been decoded and is in the buffer */
  364. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
  365. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
  366. s5p_mfc_handle_frame_new(ctx, err);
  367. } else {
  368. mfc_debug(2, "No frame decode\n");
  369. }
  370. /* Mark source buffer as complete */
  371. if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
  372. && !list_empty(&ctx->src_queue)) {
  373. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  374. list);
  375. ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
  376. get_consumed_stream, dev);
  377. if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
  378. ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
  379. ctx->consumed_stream + STUFF_BYTE <
  380. src_buf->b->vb2_buf.planes[0].bytesused) {
  381. /* Run MFC again on the same buffer */
  382. mfc_debug(2, "Running again the same buffer\n");
  383. ctx->after_packed_pb = 1;
  384. } else {
  385. mfc_debug(2, "MFC needs next buffer\n");
  386. ctx->consumed_stream = 0;
  387. if (src_buf->flags & MFC_BUF_FLAG_EOS)
  388. ctx->state = MFCINST_FINISHING;
  389. list_del(&src_buf->list);
  390. ctx->src_queue_cnt--;
  391. if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
  392. vb2_buffer_done(&src_buf->b->vb2_buf,
  393. VB2_BUF_STATE_ERROR);
  394. else
  395. vb2_buffer_done(&src_buf->b->vb2_buf,
  396. VB2_BUF_STATE_DONE);
  397. }
  398. }
  399. leave_handle_frame:
  400. if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
  401. || ctx->dst_queue_cnt < ctx->pb_count)
  402. clear_work_bit(ctx);
  403. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  404. wake_up_ctx(ctx, reason, err);
  405. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  406. s5p_mfc_clock_off();
  407. /* if suspending, wake up device and do not try_run again*/
  408. if (test_bit(0, &dev->enter_suspend))
  409. wake_up_dev(dev, reason, err);
  410. else
  411. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  412. }
  413. /* Error handling for interrupt */
  414. static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
  415. struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
  416. {
  417. mfc_err("Interrupt Error: %08x\n", err);
  418. if (ctx) {
  419. /* Error recovery is dependent on the state of context */
  420. switch (ctx->state) {
  421. case MFCINST_RES_CHANGE_INIT:
  422. case MFCINST_RES_CHANGE_FLUSH:
  423. case MFCINST_RES_CHANGE_END:
  424. case MFCINST_FINISHING:
  425. case MFCINST_FINISHED:
  426. case MFCINST_RUNNING:
  427. /* It is highly probable that an error occurred
  428. * while decoding a frame */
  429. clear_work_bit(ctx);
  430. ctx->state = MFCINST_ERROR;
  431. /* Mark all dst buffers as having an error */
  432. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  433. /* Mark all src buffers as having an error */
  434. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  435. wake_up_ctx(ctx, reason, err);
  436. break;
  437. default:
  438. clear_work_bit(ctx);
  439. ctx->state = MFCINST_ERROR;
  440. wake_up_ctx(ctx, reason, err);
  441. break;
  442. }
  443. }
  444. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  445. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  446. s5p_mfc_clock_off();
  447. wake_up_dev(dev, reason, err);
  448. }
  449. /* Header parsing interrupt handling */
  450. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  451. unsigned int reason, unsigned int err)
  452. {
  453. struct s5p_mfc_dev *dev;
  454. if (!ctx)
  455. return;
  456. dev = ctx->dev;
  457. if (ctx->c_ops->post_seq_start) {
  458. if (ctx->c_ops->post_seq_start(ctx))
  459. mfc_err("post_seq_start() failed\n");
  460. } else {
  461. ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
  462. dev);
  463. ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
  464. dev);
  465. s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
  466. ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
  467. dev);
  468. ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
  469. dev);
  470. if (FW_HAS_E_MIN_SCRATCH_BUF(dev))
  471. ctx->scratch_buf_size = s5p_mfc_hw_call(dev->mfc_ops,
  472. get_min_scratch_buf_size, dev);
  473. if (ctx->img_width == 0 || ctx->img_height == 0)
  474. ctx->state = MFCINST_ERROR;
  475. else
  476. ctx->state = MFCINST_HEAD_PARSED;
  477. if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  478. ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
  479. !list_empty(&ctx->src_queue)) {
  480. struct s5p_mfc_buf *src_buf;
  481. src_buf = list_entry(ctx->src_queue.next,
  482. struct s5p_mfc_buf, list);
  483. if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
  484. dev) <
  485. src_buf->b->vb2_buf.planes[0].bytesused)
  486. ctx->head_processed = 0;
  487. else
  488. ctx->head_processed = 1;
  489. } else {
  490. ctx->head_processed = 1;
  491. }
  492. }
  493. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  494. clear_work_bit(ctx);
  495. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  496. s5p_mfc_clock_off();
  497. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  498. wake_up_ctx(ctx, reason, err);
  499. }
  500. /* Header parsing interrupt handling */
  501. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  502. unsigned int reason, unsigned int err)
  503. {
  504. struct s5p_mfc_buf *src_buf;
  505. struct s5p_mfc_dev *dev;
  506. if (!ctx)
  507. return;
  508. dev = ctx->dev;
  509. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  510. ctx->int_type = reason;
  511. ctx->int_err = err;
  512. ctx->int_cond = 1;
  513. clear_work_bit(ctx);
  514. if (err == 0) {
  515. ctx->state = MFCINST_RUNNING;
  516. if (!ctx->dpb_flush_flag && ctx->head_processed) {
  517. if (!list_empty(&ctx->src_queue)) {
  518. src_buf = list_entry(ctx->src_queue.next,
  519. struct s5p_mfc_buf, list);
  520. list_del(&src_buf->list);
  521. ctx->src_queue_cnt--;
  522. vb2_buffer_done(&src_buf->b->vb2_buf,
  523. VB2_BUF_STATE_DONE);
  524. }
  525. } else {
  526. ctx->dpb_flush_flag = 0;
  527. }
  528. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  529. s5p_mfc_clock_off();
  530. wake_up(&ctx->queue);
  531. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  532. } else {
  533. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  534. s5p_mfc_clock_off();
  535. wake_up(&ctx->queue);
  536. }
  537. }
  538. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
  539. {
  540. struct s5p_mfc_dev *dev = ctx->dev;
  541. struct s5p_mfc_buf *mb_entry;
  542. mfc_debug(2, "Stream completed\n");
  543. ctx->state = MFCINST_FINISHED;
  544. if (!list_empty(&ctx->dst_queue)) {
  545. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  546. list);
  547. list_del(&mb_entry->list);
  548. ctx->dst_queue_cnt--;
  549. vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
  550. vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
  551. }
  552. clear_work_bit(ctx);
  553. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  554. s5p_mfc_clock_off();
  555. wake_up(&ctx->queue);
  556. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  557. }
  558. /* Interrupt processing */
  559. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  560. {
  561. struct s5p_mfc_dev *dev = priv;
  562. struct s5p_mfc_ctx *ctx;
  563. unsigned int reason;
  564. unsigned int err;
  565. mfc_debug_enter();
  566. /* Reset the timeout watchdog */
  567. atomic_set(&dev->watchdog_cnt, 0);
  568. spin_lock(&dev->irqlock);
  569. ctx = dev->ctx[dev->curr_ctx];
  570. /* Get the reason of interrupt and the error code */
  571. reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
  572. err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
  573. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  574. switch (reason) {
  575. case S5P_MFC_R2H_CMD_ERR_RET:
  576. /* An error has occurred */
  577. if (ctx->state == MFCINST_RUNNING &&
  578. (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
  579. dev->warn_start ||
  580. err == S5P_FIMV_ERR_NO_VALID_SEQ_HDR ||
  581. err == S5P_FIMV_ERR_INCOMPLETE_FRAME ||
  582. err == S5P_FIMV_ERR_TIMEOUT))
  583. s5p_mfc_handle_frame(ctx, reason, err);
  584. else
  585. s5p_mfc_handle_error(dev, ctx, reason, err);
  586. clear_bit(0, &dev->enter_suspend);
  587. break;
  588. case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
  589. case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
  590. case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
  591. if (ctx->c_ops->post_frame_start) {
  592. if (ctx->c_ops->post_frame_start(ctx))
  593. mfc_err("post_frame_start() failed\n");
  594. if (ctx->state == MFCINST_FINISHING &&
  595. list_empty(&ctx->ref_queue)) {
  596. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  597. s5p_mfc_handle_stream_complete(ctx);
  598. break;
  599. }
  600. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  601. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  602. s5p_mfc_clock_off();
  603. wake_up_ctx(ctx, reason, err);
  604. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  605. } else {
  606. s5p_mfc_handle_frame(ctx, reason, err);
  607. }
  608. break;
  609. case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
  610. s5p_mfc_handle_seq_done(ctx, reason, err);
  611. break;
  612. case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
  613. ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
  614. ctx->state = MFCINST_GOT_INST;
  615. goto irq_cleanup_hw;
  616. case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
  617. ctx->inst_no = MFC_NO_INSTANCE_SET;
  618. ctx->state = MFCINST_FREE;
  619. goto irq_cleanup_hw;
  620. case S5P_MFC_R2H_CMD_SYS_INIT_RET:
  621. case S5P_MFC_R2H_CMD_FW_STATUS_RET:
  622. case S5P_MFC_R2H_CMD_SLEEP_RET:
  623. case S5P_MFC_R2H_CMD_WAKEUP_RET:
  624. if (ctx)
  625. clear_work_bit(ctx);
  626. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  627. clear_bit(0, &dev->hw_lock);
  628. clear_bit(0, &dev->enter_suspend);
  629. wake_up_dev(dev, reason, err);
  630. break;
  631. case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
  632. s5p_mfc_handle_init_buffers(ctx, reason, err);
  633. break;
  634. case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
  635. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  636. ctx->int_type = reason;
  637. ctx->int_err = err;
  638. s5p_mfc_handle_stream_complete(ctx);
  639. break;
  640. case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
  641. ctx->state = MFCINST_RUNNING;
  642. goto irq_cleanup_hw;
  643. default:
  644. mfc_debug(2, "Unknown int reason\n");
  645. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  646. }
  647. spin_unlock(&dev->irqlock);
  648. mfc_debug_leave();
  649. return IRQ_HANDLED;
  650. irq_cleanup_hw:
  651. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  652. ctx->int_type = reason;
  653. ctx->int_err = err;
  654. ctx->int_cond = 1;
  655. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  656. mfc_err("Failed to unlock hw\n");
  657. s5p_mfc_clock_off();
  658. clear_work_bit(ctx);
  659. wake_up(&ctx->queue);
  660. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  661. spin_unlock(&dev->irqlock);
  662. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  663. return IRQ_HANDLED;
  664. }
  665. /* Open an MFC node */
  666. static int s5p_mfc_open(struct file *file)
  667. {
  668. struct video_device *vdev = video_devdata(file);
  669. struct s5p_mfc_dev *dev = video_drvdata(file);
  670. struct s5p_mfc_ctx *ctx = NULL;
  671. struct vb2_queue *q;
  672. int ret = 0;
  673. mfc_debug_enter();
  674. if (mutex_lock_interruptible(&dev->mfc_mutex))
  675. return -ERESTARTSYS;
  676. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  677. /* Allocate memory for context */
  678. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  679. if (!ctx) {
  680. ret = -ENOMEM;
  681. goto err_alloc;
  682. }
  683. init_waitqueue_head(&ctx->queue);
  684. v4l2_fh_init(&ctx->fh, vdev);
  685. file->private_data = &ctx->fh;
  686. v4l2_fh_add(&ctx->fh);
  687. ctx->dev = dev;
  688. INIT_LIST_HEAD(&ctx->src_queue);
  689. INIT_LIST_HEAD(&ctx->dst_queue);
  690. ctx->src_queue_cnt = 0;
  691. ctx->dst_queue_cnt = 0;
  692. /* Get context number */
  693. ctx->num = 0;
  694. while (dev->ctx[ctx->num]) {
  695. ctx->num++;
  696. if (ctx->num >= MFC_NUM_CONTEXTS) {
  697. mfc_debug(2, "Too many open contexts\n");
  698. ret = -EBUSY;
  699. goto err_no_ctx;
  700. }
  701. }
  702. /* Mark context as idle */
  703. clear_work_bit_irqsave(ctx);
  704. dev->ctx[ctx->num] = ctx;
  705. if (vdev == dev->vfd_dec) {
  706. ctx->type = MFCINST_DECODER;
  707. ctx->c_ops = get_dec_codec_ops();
  708. s5p_mfc_dec_init(ctx);
  709. /* Setup ctrl handler */
  710. ret = s5p_mfc_dec_ctrls_setup(ctx);
  711. if (ret) {
  712. mfc_err("Failed to setup mfc controls\n");
  713. goto err_ctrls_setup;
  714. }
  715. } else if (vdev == dev->vfd_enc) {
  716. ctx->type = MFCINST_ENCODER;
  717. ctx->c_ops = get_enc_codec_ops();
  718. /* only for encoder */
  719. INIT_LIST_HEAD(&ctx->ref_queue);
  720. ctx->ref_queue_cnt = 0;
  721. s5p_mfc_enc_init(ctx);
  722. /* Setup ctrl handler */
  723. ret = s5p_mfc_enc_ctrls_setup(ctx);
  724. if (ret) {
  725. mfc_err("Failed to setup mfc controls\n");
  726. goto err_ctrls_setup;
  727. }
  728. } else {
  729. ret = -ENOENT;
  730. goto err_bad_node;
  731. }
  732. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  733. ctx->inst_no = MFC_NO_INSTANCE_SET;
  734. /* Load firmware if this is the first instance */
  735. if (dev->num_inst == 1) {
  736. dev->watchdog_timer.expires = jiffies +
  737. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  738. add_timer(&dev->watchdog_timer);
  739. ret = s5p_mfc_power_on();
  740. if (ret < 0) {
  741. mfc_err("power on failed\n");
  742. goto err_pwr_enable;
  743. }
  744. s5p_mfc_clock_on();
  745. ret = s5p_mfc_load_firmware(dev);
  746. if (ret) {
  747. s5p_mfc_clock_off();
  748. goto err_load_fw;
  749. }
  750. /* Init the FW */
  751. ret = s5p_mfc_init_hw(dev);
  752. s5p_mfc_clock_off();
  753. if (ret)
  754. goto err_init_hw;
  755. }
  756. /* Init videobuf2 queue for CAPTURE */
  757. q = &ctx->vq_dst;
  758. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  759. q->drv_priv = &ctx->fh;
  760. q->lock = &dev->mfc_mutex;
  761. if (vdev == dev->vfd_dec) {
  762. q->io_modes = VB2_MMAP;
  763. q->ops = get_dec_queue_ops();
  764. } else if (vdev == dev->vfd_enc) {
  765. q->io_modes = VB2_MMAP | VB2_USERPTR;
  766. q->ops = get_enc_queue_ops();
  767. } else {
  768. ret = -ENOENT;
  769. goto err_queue_init;
  770. }
  771. /*
  772. * We'll do mostly sequential access, so sacrifice TLB efficiency for
  773. * faster allocation.
  774. */
  775. q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
  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(capture)\n");
  781. goto err_queue_init;
  782. }
  783. /* Init videobuf2 queue for OUTPUT */
  784. q = &ctx->vq_src;
  785. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  786. q->drv_priv = &ctx->fh;
  787. q->lock = &dev->mfc_mutex;
  788. if (vdev == dev->vfd_dec) {
  789. q->io_modes = VB2_MMAP;
  790. q->ops = get_dec_queue_ops();
  791. } else if (vdev == dev->vfd_enc) {
  792. q->io_modes = VB2_MMAP | VB2_USERPTR;
  793. q->ops = get_enc_queue_ops();
  794. } else {
  795. ret = -ENOENT;
  796. goto err_queue_init;
  797. }
  798. /* One way to indicate end-of-stream for MFC is to set the
  799. * bytesused == 0. However by default videobuf2 handles bytesused
  800. * equal to 0 as a special case and changes its value to the size
  801. * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
  802. * will keep the value of bytesused intact.
  803. */
  804. q->allow_zero_bytesused = 1;
  805. /*
  806. * We'll do mostly sequential access, so sacrifice TLB efficiency for
  807. * faster allocation.
  808. */
  809. q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
  810. q->mem_ops = &vb2_dma_contig_memops;
  811. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  812. ret = vb2_queue_init(q);
  813. if (ret) {
  814. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  815. goto err_queue_init;
  816. }
  817. mutex_unlock(&dev->mfc_mutex);
  818. mfc_debug_leave();
  819. return ret;
  820. /* Deinit when failure occurred */
  821. err_queue_init:
  822. if (dev->num_inst == 1)
  823. s5p_mfc_deinit_hw(dev);
  824. err_init_hw:
  825. err_load_fw:
  826. err_pwr_enable:
  827. if (dev->num_inst == 1) {
  828. if (s5p_mfc_power_off() < 0)
  829. mfc_err("power off failed\n");
  830. del_timer_sync(&dev->watchdog_timer);
  831. }
  832. err_ctrls_setup:
  833. s5p_mfc_dec_ctrls_delete(ctx);
  834. err_bad_node:
  835. dev->ctx[ctx->num] = NULL;
  836. err_no_ctx:
  837. v4l2_fh_del(&ctx->fh);
  838. v4l2_fh_exit(&ctx->fh);
  839. kfree(ctx);
  840. err_alloc:
  841. dev->num_inst--;
  842. mutex_unlock(&dev->mfc_mutex);
  843. mfc_debug_leave();
  844. return ret;
  845. }
  846. /* Release MFC context */
  847. static int s5p_mfc_release(struct file *file)
  848. {
  849. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  850. struct s5p_mfc_dev *dev = ctx->dev;
  851. /* if dev is null, do cleanup that doesn't need dev */
  852. mfc_debug_enter();
  853. if (dev)
  854. mutex_lock(&dev->mfc_mutex);
  855. vb2_queue_release(&ctx->vq_src);
  856. vb2_queue_release(&ctx->vq_dst);
  857. if (dev) {
  858. s5p_mfc_clock_on();
  859. /* Mark context as idle */
  860. clear_work_bit_irqsave(ctx);
  861. /*
  862. * If instance was initialised and not yet freed,
  863. * return instance and free resources
  864. */
  865. if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
  866. mfc_debug(2, "Has to free instance\n");
  867. s5p_mfc_close_mfc_inst(dev, ctx);
  868. }
  869. /* hardware locking scheme */
  870. if (dev->curr_ctx == ctx->num)
  871. clear_bit(0, &dev->hw_lock);
  872. dev->num_inst--;
  873. if (dev->num_inst == 0) {
  874. mfc_debug(2, "Last instance\n");
  875. s5p_mfc_deinit_hw(dev);
  876. del_timer_sync(&dev->watchdog_timer);
  877. s5p_mfc_clock_off();
  878. if (s5p_mfc_power_off() < 0)
  879. mfc_err("Power off failed\n");
  880. } else {
  881. mfc_debug(2, "Shutting down clock\n");
  882. s5p_mfc_clock_off();
  883. }
  884. }
  885. if (dev)
  886. dev->ctx[ctx->num] = NULL;
  887. s5p_mfc_dec_ctrls_delete(ctx);
  888. v4l2_fh_del(&ctx->fh);
  889. /* vdev is gone if dev is null */
  890. if (dev)
  891. v4l2_fh_exit(&ctx->fh);
  892. kfree(ctx);
  893. mfc_debug_leave();
  894. if (dev)
  895. mutex_unlock(&dev->mfc_mutex);
  896. return 0;
  897. }
  898. /* Poll */
  899. static __poll_t s5p_mfc_poll(struct file *file,
  900. struct poll_table_struct *wait)
  901. {
  902. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  903. struct s5p_mfc_dev *dev = ctx->dev;
  904. struct vb2_queue *src_q, *dst_q;
  905. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  906. __poll_t rc = 0;
  907. unsigned long flags;
  908. mutex_lock(&dev->mfc_mutex);
  909. src_q = &ctx->vq_src;
  910. dst_q = &ctx->vq_dst;
  911. /*
  912. * There has to be at least one buffer queued on each queued_list, which
  913. * means either in driver already or waiting for driver to claim it
  914. * and start processing.
  915. */
  916. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  917. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  918. rc = EPOLLERR;
  919. goto end;
  920. }
  921. mutex_unlock(&dev->mfc_mutex);
  922. poll_wait(file, &ctx->fh.wait, wait);
  923. poll_wait(file, &src_q->done_wq, wait);
  924. poll_wait(file, &dst_q->done_wq, wait);
  925. mutex_lock(&dev->mfc_mutex);
  926. if (v4l2_event_pending(&ctx->fh))
  927. rc |= EPOLLPRI;
  928. spin_lock_irqsave(&src_q->done_lock, flags);
  929. if (!list_empty(&src_q->done_list))
  930. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  931. done_entry);
  932. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  933. || src_vb->state == VB2_BUF_STATE_ERROR))
  934. rc |= EPOLLOUT | EPOLLWRNORM;
  935. spin_unlock_irqrestore(&src_q->done_lock, flags);
  936. spin_lock_irqsave(&dst_q->done_lock, flags);
  937. if (!list_empty(&dst_q->done_list))
  938. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  939. done_entry);
  940. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  941. || dst_vb->state == VB2_BUF_STATE_ERROR))
  942. rc |= EPOLLIN | EPOLLRDNORM;
  943. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  944. end:
  945. mutex_unlock(&dev->mfc_mutex);
  946. return rc;
  947. }
  948. /* Mmap */
  949. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  950. {
  951. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  952. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  953. int ret;
  954. if (offset < DST_QUEUE_OFF_BASE) {
  955. mfc_debug(2, "mmaping source\n");
  956. ret = vb2_mmap(&ctx->vq_src, vma);
  957. } else { /* capture */
  958. mfc_debug(2, "mmaping destination\n");
  959. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  960. ret = vb2_mmap(&ctx->vq_dst, vma);
  961. }
  962. return ret;
  963. }
  964. /* v4l2 ops */
  965. static const struct v4l2_file_operations s5p_mfc_fops = {
  966. .owner = THIS_MODULE,
  967. .open = s5p_mfc_open,
  968. .release = s5p_mfc_release,
  969. .poll = s5p_mfc_poll,
  970. .unlocked_ioctl = video_ioctl2,
  971. .mmap = s5p_mfc_mmap,
  972. };
  973. /* DMA memory related helper functions */
  974. static void s5p_mfc_memdev_release(struct device *dev)
  975. {
  976. of_reserved_mem_device_release(dev);
  977. }
  978. static struct device *s5p_mfc_alloc_memdev(struct device *dev,
  979. const char *name, unsigned int idx)
  980. {
  981. struct device *child;
  982. int ret;
  983. child = devm_kzalloc(dev, sizeof(*child), GFP_KERNEL);
  984. if (!child)
  985. return NULL;
  986. device_initialize(child);
  987. dev_set_name(child, "%s:%s", dev_name(dev), name);
  988. child->parent = dev;
  989. child->coherent_dma_mask = dev->coherent_dma_mask;
  990. child->dma_mask = dev->dma_mask;
  991. child->release = s5p_mfc_memdev_release;
  992. /*
  993. * The memdevs are not proper OF platform devices, so in order for them
  994. * to be treated as valid DMA masters we need a bit of a hack to force
  995. * them to inherit the MFC node's DMA configuration.
  996. */
  997. of_dma_configure(child, dev->of_node, true);
  998. if (device_add(child) == 0) {
  999. ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
  1000. idx);
  1001. if (ret == 0)
  1002. return child;
  1003. device_del(child);
  1004. }
  1005. put_device(child);
  1006. return NULL;
  1007. }
  1008. static int s5p_mfc_configure_2port_memory(struct s5p_mfc_dev *mfc_dev)
  1009. {
  1010. struct device *dev = &mfc_dev->plat_dev->dev;
  1011. void *bank2_virt;
  1012. dma_addr_t bank2_dma_addr;
  1013. unsigned long align_size = 1 << MFC_BASE_ALIGN_ORDER;
  1014. int ret;
  1015. /*
  1016. * Create and initialize virtual devices for accessing
  1017. * reserved memory regions.
  1018. */
  1019. mfc_dev->mem_dev[BANK_L_CTX] = s5p_mfc_alloc_memdev(dev, "left",
  1020. BANK_L_CTX);
  1021. if (!mfc_dev->mem_dev[BANK_L_CTX])
  1022. return -ENODEV;
  1023. mfc_dev->mem_dev[BANK_R_CTX] = s5p_mfc_alloc_memdev(dev, "right",
  1024. BANK_R_CTX);
  1025. if (!mfc_dev->mem_dev[BANK_R_CTX]) {
  1026. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1027. return -ENODEV;
  1028. }
  1029. /* Allocate memory for firmware and initialize both banks addresses */
  1030. ret = s5p_mfc_alloc_firmware(mfc_dev);
  1031. if (ret) {
  1032. device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
  1033. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1034. return ret;
  1035. }
  1036. mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->fw_buf.dma;
  1037. bank2_virt = dma_alloc_coherent(mfc_dev->mem_dev[BANK_R_CTX],
  1038. align_size, &bank2_dma_addr, GFP_KERNEL);
  1039. if (!bank2_virt) {
  1040. mfc_err("Allocating bank2 base failed\n");
  1041. s5p_mfc_release_firmware(mfc_dev);
  1042. device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
  1043. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1044. return -ENOMEM;
  1045. }
  1046. /* Valid buffers passed to MFC encoder with LAST_FRAME command
  1047. * should not have address of bank2 - MFC will treat it as a null frame.
  1048. * To avoid such situation we set bank2 address below the pool address.
  1049. */
  1050. mfc_dev->dma_base[BANK_R_CTX] = bank2_dma_addr - align_size;
  1051. dma_free_coherent(mfc_dev->mem_dev[BANK_R_CTX], align_size, bank2_virt,
  1052. bank2_dma_addr);
  1053. vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX],
  1054. DMA_BIT_MASK(32));
  1055. vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX],
  1056. DMA_BIT_MASK(32));
  1057. return 0;
  1058. }
  1059. static void s5p_mfc_unconfigure_2port_memory(struct s5p_mfc_dev *mfc_dev)
  1060. {
  1061. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1062. device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
  1063. vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX]);
  1064. vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX]);
  1065. }
  1066. static int s5p_mfc_configure_common_memory(struct s5p_mfc_dev *mfc_dev)
  1067. {
  1068. struct device *dev = &mfc_dev->plat_dev->dev;
  1069. unsigned long mem_size = SZ_4M;
  1070. unsigned int bitmap_size;
  1071. if (IS_ENABLED(CONFIG_DMA_CMA) || exynos_is_iommu_available(dev))
  1072. mem_size = SZ_8M;
  1073. if (mfc_mem_size)
  1074. mem_size = memparse(mfc_mem_size, NULL);
  1075. bitmap_size = BITS_TO_LONGS(mem_size >> PAGE_SHIFT) * sizeof(long);
  1076. mfc_dev->mem_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1077. if (!mfc_dev->mem_bitmap)
  1078. return -ENOMEM;
  1079. mfc_dev->mem_virt = dma_alloc_coherent(dev, mem_size,
  1080. &mfc_dev->mem_base, GFP_KERNEL);
  1081. if (!mfc_dev->mem_virt) {
  1082. kfree(mfc_dev->mem_bitmap);
  1083. dev_err(dev, "failed to preallocate %ld MiB for the firmware and context buffers\n",
  1084. (mem_size / SZ_1M));
  1085. return -ENOMEM;
  1086. }
  1087. mfc_dev->mem_size = mem_size;
  1088. mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->mem_base;
  1089. mfc_dev->dma_base[BANK_R_CTX] = mfc_dev->mem_base;
  1090. /*
  1091. * MFC hardware cannot handle 0 as a base address, so mark first 128K
  1092. * as used (to keep required base alignment) and adjust base address
  1093. */
  1094. if (mfc_dev->mem_base == (dma_addr_t)0) {
  1095. unsigned int offset = 1 << MFC_BASE_ALIGN_ORDER;
  1096. bitmap_set(mfc_dev->mem_bitmap, 0, offset >> PAGE_SHIFT);
  1097. mfc_dev->dma_base[BANK_L_CTX] += offset;
  1098. mfc_dev->dma_base[BANK_R_CTX] += offset;
  1099. }
  1100. /* Firmware allocation cannot fail in this case */
  1101. s5p_mfc_alloc_firmware(mfc_dev);
  1102. mfc_dev->mem_dev[BANK_L_CTX] = mfc_dev->mem_dev[BANK_R_CTX] = dev;
  1103. vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
  1104. dev_info(dev, "preallocated %ld MiB buffer for the firmware and context buffers\n",
  1105. (mem_size / SZ_1M));
  1106. return 0;
  1107. }
  1108. static void s5p_mfc_unconfigure_common_memory(struct s5p_mfc_dev *mfc_dev)
  1109. {
  1110. struct device *dev = &mfc_dev->plat_dev->dev;
  1111. dma_free_coherent(dev, mfc_dev->mem_size, mfc_dev->mem_virt,
  1112. mfc_dev->mem_base);
  1113. kfree(mfc_dev->mem_bitmap);
  1114. vb2_dma_contig_clear_max_seg_size(dev);
  1115. }
  1116. static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
  1117. {
  1118. struct device *dev = &mfc_dev->plat_dev->dev;
  1119. if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
  1120. return s5p_mfc_configure_common_memory(mfc_dev);
  1121. else
  1122. return s5p_mfc_configure_2port_memory(mfc_dev);
  1123. }
  1124. static void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
  1125. {
  1126. struct device *dev = &mfc_dev->plat_dev->dev;
  1127. s5p_mfc_release_firmware(mfc_dev);
  1128. if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
  1129. s5p_mfc_unconfigure_common_memory(mfc_dev);
  1130. else
  1131. s5p_mfc_unconfigure_2port_memory(mfc_dev);
  1132. }
  1133. /* MFC probe function */
  1134. static int s5p_mfc_probe(struct platform_device *pdev)
  1135. {
  1136. struct s5p_mfc_dev *dev;
  1137. struct video_device *vfd;
  1138. struct resource *res;
  1139. int ret;
  1140. pr_debug("%s++\n", __func__);
  1141. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1142. if (!dev)
  1143. return -ENOMEM;
  1144. spin_lock_init(&dev->irqlock);
  1145. spin_lock_init(&dev->condlock);
  1146. dev->plat_dev = pdev;
  1147. if (!dev->plat_dev) {
  1148. dev_err(&pdev->dev, "No platform data specified\n");
  1149. return -ENODEV;
  1150. }
  1151. dev->variant = of_device_get_match_data(&pdev->dev);
  1152. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1153. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  1154. if (IS_ERR(dev->regs_base))
  1155. return PTR_ERR(dev->regs_base);
  1156. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1157. if (!res) {
  1158. dev_err(&pdev->dev, "failed to get irq resource\n");
  1159. return -ENOENT;
  1160. }
  1161. dev->irq = res->start;
  1162. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  1163. 0, pdev->name, dev);
  1164. if (ret) {
  1165. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  1166. return ret;
  1167. }
  1168. ret = s5p_mfc_configure_dma_memory(dev);
  1169. if (ret < 0) {
  1170. dev_err(&pdev->dev, "failed to configure DMA memory\n");
  1171. return ret;
  1172. }
  1173. ret = s5p_mfc_init_pm(dev);
  1174. if (ret < 0) {
  1175. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  1176. goto err_dma;
  1177. }
  1178. /*
  1179. * Load fails if fs isn't mounted. Try loading anyway.
  1180. * _open() will load it, it it fails now. Ignore failure.
  1181. */
  1182. s5p_mfc_load_firmware(dev);
  1183. mutex_init(&dev->mfc_mutex);
  1184. init_waitqueue_head(&dev->queue);
  1185. dev->hw_lock = 0;
  1186. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1187. atomic_set(&dev->watchdog_cnt, 0);
  1188. timer_setup(&dev->watchdog_timer, s5p_mfc_watchdog, 0);
  1189. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1190. if (ret)
  1191. goto err_v4l2_dev_reg;
  1192. /* decoder */
  1193. vfd = video_device_alloc();
  1194. if (!vfd) {
  1195. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1196. ret = -ENOMEM;
  1197. goto err_dec_alloc;
  1198. }
  1199. vfd->fops = &s5p_mfc_fops;
  1200. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1201. vfd->release = video_device_release;
  1202. vfd->lock = &dev->mfc_mutex;
  1203. vfd->v4l2_dev = &dev->v4l2_dev;
  1204. vfd->vfl_dir = VFL_DIR_M2M;
  1205. vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1206. set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
  1207. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1208. dev->vfd_dec = vfd;
  1209. video_set_drvdata(vfd, dev);
  1210. /* encoder */
  1211. vfd = video_device_alloc();
  1212. if (!vfd) {
  1213. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1214. ret = -ENOMEM;
  1215. goto err_enc_alloc;
  1216. }
  1217. vfd->fops = &s5p_mfc_fops;
  1218. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1219. vfd->release = video_device_release;
  1220. vfd->lock = &dev->mfc_mutex;
  1221. vfd->v4l2_dev = &dev->v4l2_dev;
  1222. vfd->vfl_dir = VFL_DIR_M2M;
  1223. vfd->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
  1224. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1225. dev->vfd_enc = vfd;
  1226. video_set_drvdata(vfd, dev);
  1227. platform_set_drvdata(pdev, dev);
  1228. /* Initialize HW ops and commands based on MFC version */
  1229. s5p_mfc_init_hw_ops(dev);
  1230. s5p_mfc_init_hw_cmds(dev);
  1231. s5p_mfc_init_regs(dev);
  1232. /* Register decoder and encoder */
  1233. ret = video_register_device(dev->vfd_dec, VFL_TYPE_GRABBER, 0);
  1234. if (ret) {
  1235. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1236. goto err_dec_reg;
  1237. }
  1238. v4l2_info(&dev->v4l2_dev,
  1239. "decoder registered as /dev/video%d\n", dev->vfd_dec->num);
  1240. ret = video_register_device(dev->vfd_enc, VFL_TYPE_GRABBER, 0);
  1241. if (ret) {
  1242. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1243. goto err_enc_reg;
  1244. }
  1245. v4l2_info(&dev->v4l2_dev,
  1246. "encoder registered as /dev/video%d\n", dev->vfd_enc->num);
  1247. pr_debug("%s--\n", __func__);
  1248. return 0;
  1249. /* Deinit MFC if probe had failed */
  1250. err_enc_reg:
  1251. video_unregister_device(dev->vfd_dec);
  1252. err_dec_reg:
  1253. video_device_release(dev->vfd_enc);
  1254. err_enc_alloc:
  1255. video_device_release(dev->vfd_dec);
  1256. err_dec_alloc:
  1257. v4l2_device_unregister(&dev->v4l2_dev);
  1258. err_v4l2_dev_reg:
  1259. s5p_mfc_final_pm(dev);
  1260. err_dma:
  1261. s5p_mfc_unconfigure_dma_memory(dev);
  1262. pr_debug("%s-- with error\n", __func__);
  1263. return ret;
  1264. }
  1265. /* Remove the driver */
  1266. static int s5p_mfc_remove(struct platform_device *pdev)
  1267. {
  1268. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1269. struct s5p_mfc_ctx *ctx;
  1270. int i;
  1271. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1272. /*
  1273. * Clear ctx dev pointer to avoid races between s5p_mfc_remove()
  1274. * and s5p_mfc_release() and s5p_mfc_release() accessing ctx->dev
  1275. * after s5p_mfc_remove() is run during unbind.
  1276. */
  1277. mutex_lock(&dev->mfc_mutex);
  1278. for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
  1279. ctx = dev->ctx[i];
  1280. if (!ctx)
  1281. continue;
  1282. /* clear ctx->dev */
  1283. ctx->dev = NULL;
  1284. }
  1285. mutex_unlock(&dev->mfc_mutex);
  1286. del_timer_sync(&dev->watchdog_timer);
  1287. flush_work(&dev->watchdog_work);
  1288. video_unregister_device(dev->vfd_enc);
  1289. video_unregister_device(dev->vfd_dec);
  1290. video_device_release(dev->vfd_enc);
  1291. video_device_release(dev->vfd_dec);
  1292. v4l2_device_unregister(&dev->v4l2_dev);
  1293. s5p_mfc_unconfigure_dma_memory(dev);
  1294. s5p_mfc_final_pm(dev);
  1295. return 0;
  1296. }
  1297. #ifdef CONFIG_PM_SLEEP
  1298. static int s5p_mfc_suspend(struct device *dev)
  1299. {
  1300. struct s5p_mfc_dev *m_dev = dev_get_drvdata(dev);
  1301. int ret;
  1302. if (m_dev->num_inst == 0)
  1303. return 0;
  1304. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1305. mfc_err("Error: going to suspend for a second time\n");
  1306. return -EIO;
  1307. }
  1308. /* Check if we're processing then wait if it necessary. */
  1309. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1310. /* Try and lock the HW */
  1311. /* Wait on the interrupt waitqueue */
  1312. ret = wait_event_interruptible_timeout(m_dev->queue,
  1313. m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
  1314. if (ret == 0) {
  1315. mfc_err("Waiting for hardware to finish timed out\n");
  1316. clear_bit(0, &m_dev->enter_suspend);
  1317. return -EIO;
  1318. }
  1319. }
  1320. ret = s5p_mfc_sleep(m_dev);
  1321. if (ret) {
  1322. clear_bit(0, &m_dev->enter_suspend);
  1323. clear_bit(0, &m_dev->hw_lock);
  1324. }
  1325. return ret;
  1326. }
  1327. static int s5p_mfc_resume(struct device *dev)
  1328. {
  1329. struct s5p_mfc_dev *m_dev = dev_get_drvdata(dev);
  1330. if (m_dev->num_inst == 0)
  1331. return 0;
  1332. return s5p_mfc_wakeup(m_dev);
  1333. }
  1334. #endif
  1335. /* Power management */
  1336. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1337. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1338. };
  1339. static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
  1340. .h264_ctx = MFC_H264_CTX_BUF_SIZE,
  1341. .non_h264_ctx = MFC_CTX_BUF_SIZE,
  1342. .dsc = DESC_BUF_SIZE,
  1343. .shm = SHARED_BUF_SIZE,
  1344. };
  1345. static struct s5p_mfc_buf_size buf_size_v5 = {
  1346. .fw = MAX_FW_SIZE,
  1347. .cpb = MAX_CPB_SIZE,
  1348. .priv = &mfc_buf_size_v5,
  1349. };
  1350. static struct s5p_mfc_variant mfc_drvdata_v5 = {
  1351. .version = MFC_VERSION,
  1352. .version_bit = MFC_V5_BIT,
  1353. .port_num = MFC_NUM_PORTS,
  1354. .buf_size = &buf_size_v5,
  1355. .fw_name[0] = "s5p-mfc.fw",
  1356. .clk_names = {"mfc", "sclk_mfc"},
  1357. .num_clocks = 2,
  1358. .use_clock_gating = true,
  1359. };
  1360. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
  1361. .dev_ctx = MFC_CTX_BUF_SIZE_V6,
  1362. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
  1363. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
  1364. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
  1365. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
  1366. };
  1367. static struct s5p_mfc_buf_size buf_size_v6 = {
  1368. .fw = MAX_FW_SIZE_V6,
  1369. .cpb = MAX_CPB_SIZE_V6,
  1370. .priv = &mfc_buf_size_v6,
  1371. };
  1372. static struct s5p_mfc_variant mfc_drvdata_v6 = {
  1373. .version = MFC_VERSION_V6,
  1374. .version_bit = MFC_V6_BIT,
  1375. .port_num = MFC_NUM_PORTS_V6,
  1376. .buf_size = &buf_size_v6,
  1377. .fw_name[0] = "s5p-mfc-v6.fw",
  1378. /*
  1379. * v6-v2 firmware contains bug fixes and interface change
  1380. * for init buffer command
  1381. */
  1382. .fw_name[1] = "s5p-mfc-v6-v2.fw",
  1383. .clk_names = {"mfc"},
  1384. .num_clocks = 1,
  1385. };
  1386. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
  1387. .dev_ctx = MFC_CTX_BUF_SIZE_V7,
  1388. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
  1389. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
  1390. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
  1391. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
  1392. };
  1393. static struct s5p_mfc_buf_size buf_size_v7 = {
  1394. .fw = MAX_FW_SIZE_V7,
  1395. .cpb = MAX_CPB_SIZE_V7,
  1396. .priv = &mfc_buf_size_v7,
  1397. };
  1398. static struct s5p_mfc_variant mfc_drvdata_v7 = {
  1399. .version = MFC_VERSION_V7,
  1400. .version_bit = MFC_V7_BIT,
  1401. .port_num = MFC_NUM_PORTS_V7,
  1402. .buf_size = &buf_size_v7,
  1403. .fw_name[0] = "s5p-mfc-v7.fw",
  1404. .clk_names = {"mfc", "sclk_mfc"},
  1405. .num_clocks = 2,
  1406. };
  1407. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
  1408. .dev_ctx = MFC_CTX_BUF_SIZE_V8,
  1409. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
  1410. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
  1411. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
  1412. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
  1413. };
  1414. static struct s5p_mfc_buf_size buf_size_v8 = {
  1415. .fw = MAX_FW_SIZE_V8,
  1416. .cpb = MAX_CPB_SIZE_V8,
  1417. .priv = &mfc_buf_size_v8,
  1418. };
  1419. static struct s5p_mfc_variant mfc_drvdata_v8 = {
  1420. .version = MFC_VERSION_V8,
  1421. .version_bit = MFC_V8_BIT,
  1422. .port_num = MFC_NUM_PORTS_V8,
  1423. .buf_size = &buf_size_v8,
  1424. .fw_name[0] = "s5p-mfc-v8.fw",
  1425. .clk_names = {"mfc"},
  1426. .num_clocks = 1,
  1427. };
  1428. static struct s5p_mfc_variant mfc_drvdata_v8_5433 = {
  1429. .version = MFC_VERSION_V8,
  1430. .version_bit = MFC_V8_BIT,
  1431. .port_num = MFC_NUM_PORTS_V8,
  1432. .buf_size = &buf_size_v8,
  1433. .fw_name[0] = "s5p-mfc-v8.fw",
  1434. .clk_names = {"pclk", "aclk", "aclk_xiu"},
  1435. .num_clocks = 3,
  1436. };
  1437. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v10 = {
  1438. .dev_ctx = MFC_CTX_BUF_SIZE_V10,
  1439. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V10,
  1440. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V10,
  1441. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V10,
  1442. .hevc_enc_ctx = MFC_HEVC_ENC_CTX_BUF_SIZE_V10,
  1443. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V10,
  1444. };
  1445. static struct s5p_mfc_buf_size buf_size_v10 = {
  1446. .fw = MAX_FW_SIZE_V10,
  1447. .cpb = MAX_CPB_SIZE_V10,
  1448. .priv = &mfc_buf_size_v10,
  1449. };
  1450. static struct s5p_mfc_variant mfc_drvdata_v10 = {
  1451. .version = MFC_VERSION_V10,
  1452. .version_bit = MFC_V10_BIT,
  1453. .port_num = MFC_NUM_PORTS_V10,
  1454. .buf_size = &buf_size_v10,
  1455. .fw_name[0] = "s5p-mfc-v10.fw",
  1456. };
  1457. static const struct of_device_id exynos_mfc_match[] = {
  1458. {
  1459. .compatible = "samsung,mfc-v5",
  1460. .data = &mfc_drvdata_v5,
  1461. }, {
  1462. .compatible = "samsung,mfc-v6",
  1463. .data = &mfc_drvdata_v6,
  1464. }, {
  1465. .compatible = "samsung,mfc-v7",
  1466. .data = &mfc_drvdata_v7,
  1467. }, {
  1468. .compatible = "samsung,mfc-v8",
  1469. .data = &mfc_drvdata_v8,
  1470. }, {
  1471. .compatible = "samsung,exynos5433-mfc",
  1472. .data = &mfc_drvdata_v8_5433,
  1473. }, {
  1474. .compatible = "samsung,mfc-v10",
  1475. .data = &mfc_drvdata_v10,
  1476. },
  1477. {},
  1478. };
  1479. MODULE_DEVICE_TABLE(of, exynos_mfc_match);
  1480. static struct platform_driver s5p_mfc_driver = {
  1481. .probe = s5p_mfc_probe,
  1482. .remove = s5p_mfc_remove,
  1483. .driver = {
  1484. .name = S5P_MFC_NAME,
  1485. .pm = &s5p_mfc_pm_ops,
  1486. .of_match_table = exynos_mfc_match,
  1487. },
  1488. };
  1489. module_platform_driver(s5p_mfc_driver);
  1490. MODULE_LICENSE("GPL");
  1491. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1492. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");