s5p_mfc.c 38 KB

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