s5p_mfc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. s5p_mfc_hw_call(dev->mfc_ops,
  343. get_dec_frame_type, dev) ==
  344. S5P_FIMV_DECODE_FRAME_P_FRAME
  345. && ctx->consumed_stream + STUFF_BYTE <
  346. src_buf->b->v4l2_planes[0].bytesused) {
  347. /* Run MFC again on the same buffer */
  348. mfc_debug(2, "Running again the same buffer\n");
  349. ctx->after_packed_pb = 1;
  350. } else {
  351. index = src_buf->b->v4l2_buf.index;
  352. mfc_debug(2, "MFC needs next buffer\n");
  353. ctx->consumed_stream = 0;
  354. list_del(&src_buf->list);
  355. ctx->src_queue_cnt--;
  356. if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
  357. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
  358. else
  359. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
  360. }
  361. }
  362. leave_handle_frame:
  363. spin_unlock_irqrestore(&dev->irqlock, flags);
  364. if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
  365. || ctx->dst_queue_cnt < ctx->dpb_count)
  366. clear_work_bit(ctx);
  367. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  368. wake_up_ctx(ctx, reason, err);
  369. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  370. BUG();
  371. s5p_mfc_clock_off();
  372. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  373. }
  374. /* Error handling for interrupt */
  375. static void s5p_mfc_handle_error(struct s5p_mfc_ctx *ctx,
  376. unsigned int reason, unsigned int err)
  377. {
  378. struct s5p_mfc_dev *dev;
  379. unsigned long flags;
  380. /* If no context is available then all necessary
  381. * processing has been done. */
  382. if (ctx == NULL)
  383. return;
  384. dev = ctx->dev;
  385. mfc_err("Interrupt Error: %08x\n", err);
  386. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  387. wake_up_dev(dev, reason, err);
  388. /* Error recovery is dependent on the state of context */
  389. switch (ctx->state) {
  390. case MFCINST_INIT:
  391. /* This error had to happen while acquireing instance */
  392. case MFCINST_GOT_INST:
  393. /* This error had to happen while parsing the header */
  394. case MFCINST_HEAD_PARSED:
  395. /* This error had to happen while setting dst buffers */
  396. case MFCINST_RETURN_INST:
  397. /* This error had to happen while releasing instance */
  398. clear_work_bit(ctx);
  399. wake_up_ctx(ctx, reason, err);
  400. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  401. BUG();
  402. s5p_mfc_clock_off();
  403. ctx->state = MFCINST_ERROR;
  404. break;
  405. case MFCINST_FINISHING:
  406. case MFCINST_FINISHED:
  407. case MFCINST_RUNNING:
  408. /* It is higly probable that an error occured
  409. * while decoding a frame */
  410. clear_work_bit(ctx);
  411. ctx->state = MFCINST_ERROR;
  412. /* Mark all dst buffers as having an error */
  413. spin_lock_irqsave(&dev->irqlock, flags);
  414. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
  415. &ctx->vq_dst);
  416. /* Mark all src buffers as having an error */
  417. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
  418. &ctx->vq_src);
  419. spin_unlock_irqrestore(&dev->irqlock, flags);
  420. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  421. BUG();
  422. s5p_mfc_clock_off();
  423. break;
  424. default:
  425. mfc_err("Encountered an error interrupt which had not been handled\n");
  426. break;
  427. }
  428. return;
  429. }
  430. /* Header parsing interrupt handling */
  431. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  432. unsigned int reason, unsigned int err)
  433. {
  434. struct s5p_mfc_dev *dev;
  435. if (ctx == NULL)
  436. return;
  437. dev = ctx->dev;
  438. if (ctx->c_ops->post_seq_start) {
  439. if (ctx->c_ops->post_seq_start(ctx))
  440. mfc_err("post_seq_start() failed\n");
  441. } else {
  442. ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
  443. dev);
  444. ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
  445. dev);
  446. s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
  447. ctx->dpb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
  448. dev);
  449. ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
  450. dev);
  451. if (ctx->img_width == 0 || ctx->img_height == 0)
  452. ctx->state = MFCINST_ERROR;
  453. else
  454. ctx->state = MFCINST_HEAD_PARSED;
  455. if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  456. ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
  457. !list_empty(&ctx->src_queue)) {
  458. struct s5p_mfc_buf *src_buf;
  459. src_buf = list_entry(ctx->src_queue.next,
  460. struct s5p_mfc_buf, list);
  461. if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
  462. dev) <
  463. src_buf->b->v4l2_planes[0].bytesused)
  464. ctx->head_processed = 0;
  465. else
  466. ctx->head_processed = 1;
  467. } else {
  468. ctx->head_processed = 1;
  469. }
  470. }
  471. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  472. clear_work_bit(ctx);
  473. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  474. BUG();
  475. s5p_mfc_clock_off();
  476. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  477. wake_up_ctx(ctx, reason, err);
  478. }
  479. /* Header parsing interrupt handling */
  480. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  481. unsigned int reason, unsigned int err)
  482. {
  483. struct s5p_mfc_buf *src_buf;
  484. struct s5p_mfc_dev *dev;
  485. unsigned long flags;
  486. if (ctx == NULL)
  487. return;
  488. dev = ctx->dev;
  489. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  490. ctx->int_type = reason;
  491. ctx->int_err = err;
  492. ctx->int_cond = 1;
  493. clear_work_bit(ctx);
  494. if (err == 0) {
  495. ctx->state = MFCINST_RUNNING;
  496. if (!ctx->dpb_flush_flag && ctx->head_processed) {
  497. spin_lock_irqsave(&dev->irqlock, flags);
  498. if (!list_empty(&ctx->src_queue)) {
  499. src_buf = list_entry(ctx->src_queue.next,
  500. struct s5p_mfc_buf, list);
  501. list_del(&src_buf->list);
  502. ctx->src_queue_cnt--;
  503. vb2_buffer_done(src_buf->b,
  504. VB2_BUF_STATE_DONE);
  505. }
  506. spin_unlock_irqrestore(&dev->irqlock, flags);
  507. } else {
  508. ctx->dpb_flush_flag = 0;
  509. }
  510. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  511. BUG();
  512. s5p_mfc_clock_off();
  513. wake_up(&ctx->queue);
  514. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  515. } else {
  516. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  517. BUG();
  518. s5p_mfc_clock_off();
  519. wake_up(&ctx->queue);
  520. }
  521. }
  522. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
  523. unsigned int reason, unsigned int err)
  524. {
  525. struct s5p_mfc_dev *dev = ctx->dev;
  526. struct s5p_mfc_buf *mb_entry;
  527. mfc_debug(2, "Stream completed");
  528. s5p_mfc_clear_int_flags(dev);
  529. ctx->int_type = reason;
  530. ctx->int_err = err;
  531. ctx->state = MFCINST_FINISHED;
  532. spin_lock(&dev->irqlock);
  533. if (!list_empty(&ctx->dst_queue)) {
  534. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  535. list);
  536. list_del(&mb_entry->list);
  537. ctx->dst_queue_cnt--;
  538. vb2_set_plane_payload(mb_entry->b, 0, 0);
  539. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  540. }
  541. spin_unlock(&dev->irqlock);
  542. clear_work_bit(ctx);
  543. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  544. WARN_ON(1);
  545. s5p_mfc_clock_off();
  546. wake_up(&ctx->queue);
  547. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  548. }
  549. /* Interrupt processing */
  550. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  551. {
  552. struct s5p_mfc_dev *dev = priv;
  553. struct s5p_mfc_ctx *ctx;
  554. unsigned int reason;
  555. unsigned int err;
  556. mfc_debug_enter();
  557. /* Reset the timeout watchdog */
  558. atomic_set(&dev->watchdog_cnt, 0);
  559. ctx = dev->ctx[dev->curr_ctx];
  560. /* Get the reason of interrupt and the error code */
  561. reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
  562. err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
  563. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  564. switch (reason) {
  565. case S5P_MFC_R2H_CMD_ERR_RET:
  566. /* An error has occured */
  567. if (ctx->state == MFCINST_RUNNING &&
  568. s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
  569. dev->warn_start)
  570. s5p_mfc_handle_frame(ctx, reason, err);
  571. else
  572. s5p_mfc_handle_error(ctx, reason, err);
  573. clear_bit(0, &dev->enter_suspend);
  574. break;
  575. case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
  576. case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
  577. case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
  578. if (ctx->c_ops->post_frame_start) {
  579. if (ctx->c_ops->post_frame_start(ctx))
  580. mfc_err("post_frame_start() failed\n");
  581. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  582. wake_up_ctx(ctx, reason, err);
  583. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  584. BUG();
  585. s5p_mfc_clock_off();
  586. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  587. } else {
  588. s5p_mfc_handle_frame(ctx, reason, err);
  589. }
  590. break;
  591. case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
  592. s5p_mfc_handle_seq_done(ctx, reason, err);
  593. break;
  594. case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
  595. ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
  596. ctx->state = MFCINST_GOT_INST;
  597. clear_work_bit(ctx);
  598. wake_up(&ctx->queue);
  599. goto irq_cleanup_hw;
  600. case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
  601. clear_work_bit(ctx);
  602. ctx->state = MFCINST_FREE;
  603. wake_up(&ctx->queue);
  604. goto irq_cleanup_hw;
  605. case S5P_MFC_R2H_CMD_SYS_INIT_RET:
  606. case S5P_MFC_R2H_CMD_FW_STATUS_RET:
  607. case S5P_MFC_R2H_CMD_SLEEP_RET:
  608. case S5P_MFC_R2H_CMD_WAKEUP_RET:
  609. if (ctx)
  610. clear_work_bit(ctx);
  611. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  612. wake_up_dev(dev, reason, err);
  613. clear_bit(0, &dev->hw_lock);
  614. clear_bit(0, &dev->enter_suspend);
  615. break;
  616. case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
  617. s5p_mfc_handle_init_buffers(ctx, reason, err);
  618. break;
  619. case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
  620. s5p_mfc_handle_stream_complete(ctx, reason, err);
  621. break;
  622. default:
  623. mfc_debug(2, "Unknown int reason\n");
  624. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  625. }
  626. mfc_debug_leave();
  627. return IRQ_HANDLED;
  628. irq_cleanup_hw:
  629. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  630. ctx->int_type = reason;
  631. ctx->int_err = err;
  632. ctx->int_cond = 1;
  633. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  634. mfc_err("Failed to unlock hw\n");
  635. s5p_mfc_clock_off();
  636. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  637. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  638. return IRQ_HANDLED;
  639. }
  640. /* Open an MFC node */
  641. static int s5p_mfc_open(struct file *file)
  642. {
  643. struct s5p_mfc_dev *dev = video_drvdata(file);
  644. struct s5p_mfc_ctx *ctx = NULL;
  645. struct vb2_queue *q;
  646. int ret = 0;
  647. mfc_debug_enter();
  648. if (mutex_lock_interruptible(&dev->mfc_mutex))
  649. return -ERESTARTSYS;
  650. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  651. /* Allocate memory for context */
  652. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  653. if (!ctx) {
  654. mfc_err("Not enough memory\n");
  655. ret = -ENOMEM;
  656. goto err_alloc;
  657. }
  658. v4l2_fh_init(&ctx->fh, video_devdata(file));
  659. file->private_data = &ctx->fh;
  660. v4l2_fh_add(&ctx->fh);
  661. ctx->dev = dev;
  662. INIT_LIST_HEAD(&ctx->src_queue);
  663. INIT_LIST_HEAD(&ctx->dst_queue);
  664. ctx->src_queue_cnt = 0;
  665. ctx->dst_queue_cnt = 0;
  666. /* Get context number */
  667. ctx->num = 0;
  668. while (dev->ctx[ctx->num]) {
  669. ctx->num++;
  670. if (ctx->num >= MFC_NUM_CONTEXTS) {
  671. mfc_err("Too many open contexts\n");
  672. ret = -EBUSY;
  673. goto err_no_ctx;
  674. }
  675. }
  676. /* Mark context as idle */
  677. clear_work_bit_irqsave(ctx);
  678. dev->ctx[ctx->num] = ctx;
  679. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  680. ctx->type = MFCINST_DECODER;
  681. ctx->c_ops = get_dec_codec_ops();
  682. s5p_mfc_dec_init(ctx);
  683. /* Setup ctrl handler */
  684. ret = s5p_mfc_dec_ctrls_setup(ctx);
  685. if (ret) {
  686. mfc_err("Failed to setup mfc controls\n");
  687. goto err_ctrls_setup;
  688. }
  689. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  690. ctx->type = MFCINST_ENCODER;
  691. ctx->c_ops = get_enc_codec_ops();
  692. /* only for encoder */
  693. INIT_LIST_HEAD(&ctx->ref_queue);
  694. ctx->ref_queue_cnt = 0;
  695. s5p_mfc_enc_init(ctx);
  696. /* Setup ctrl handler */
  697. ret = s5p_mfc_enc_ctrls_setup(ctx);
  698. if (ret) {
  699. mfc_err("Failed to setup mfc controls\n");
  700. goto err_ctrls_setup;
  701. }
  702. } else {
  703. ret = -ENOENT;
  704. goto err_bad_node;
  705. }
  706. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  707. ctx->inst_no = -1;
  708. /* Load firmware if this is the first instance */
  709. if (dev->num_inst == 1) {
  710. dev->watchdog_timer.expires = jiffies +
  711. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  712. add_timer(&dev->watchdog_timer);
  713. ret = s5p_mfc_power_on();
  714. if (ret < 0) {
  715. mfc_err("power on failed\n");
  716. goto err_pwr_enable;
  717. }
  718. s5p_mfc_clock_on();
  719. ret = s5p_mfc_alloc_and_load_firmware(dev);
  720. if (ret)
  721. goto err_alloc_fw;
  722. /* Init the FW */
  723. ret = s5p_mfc_init_hw(dev);
  724. if (ret)
  725. goto err_init_hw;
  726. s5p_mfc_clock_off();
  727. }
  728. /* Init videobuf2 queue for CAPTURE */
  729. q = &ctx->vq_dst;
  730. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  731. q->drv_priv = &ctx->fh;
  732. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  733. q->io_modes = VB2_MMAP;
  734. q->ops = get_dec_queue_ops();
  735. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  736. q->io_modes = VB2_MMAP | VB2_USERPTR;
  737. q->ops = get_enc_queue_ops();
  738. } else {
  739. ret = -ENOENT;
  740. goto err_queue_init;
  741. }
  742. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  743. ret = vb2_queue_init(q);
  744. if (ret) {
  745. mfc_err("Failed to initialize videobuf2 queue(capture)\n");
  746. goto err_queue_init;
  747. }
  748. /* Init videobuf2 queue for OUTPUT */
  749. q = &ctx->vq_src;
  750. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  751. q->io_modes = VB2_MMAP;
  752. q->drv_priv = &ctx->fh;
  753. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  754. q->io_modes = VB2_MMAP;
  755. q->ops = get_dec_queue_ops();
  756. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  757. q->io_modes = VB2_MMAP | VB2_USERPTR;
  758. q->ops = get_enc_queue_ops();
  759. } else {
  760. ret = -ENOENT;
  761. goto err_queue_init;
  762. }
  763. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  764. ret = vb2_queue_init(q);
  765. if (ret) {
  766. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  767. goto err_queue_init;
  768. }
  769. init_waitqueue_head(&ctx->queue);
  770. mutex_unlock(&dev->mfc_mutex);
  771. mfc_debug_leave();
  772. return ret;
  773. /* Deinit when failure occured */
  774. err_queue_init:
  775. err_init_hw:
  776. s5p_mfc_release_firmware(dev);
  777. err_alloc_fw:
  778. dev->ctx[ctx->num] = NULL;
  779. del_timer_sync(&dev->watchdog_timer);
  780. s5p_mfc_clock_off();
  781. err_pwr_enable:
  782. if (dev->num_inst == 1) {
  783. if (s5p_mfc_power_off() < 0)
  784. mfc_err("power off failed\n");
  785. s5p_mfc_release_firmware(dev);
  786. }
  787. err_ctrls_setup:
  788. s5p_mfc_dec_ctrls_delete(ctx);
  789. err_bad_node:
  790. err_no_ctx:
  791. v4l2_fh_del(&ctx->fh);
  792. v4l2_fh_exit(&ctx->fh);
  793. kfree(ctx);
  794. err_alloc:
  795. dev->num_inst--;
  796. mutex_unlock(&dev->mfc_mutex);
  797. mfc_debug_leave();
  798. return ret;
  799. }
  800. /* Release MFC context */
  801. static int s5p_mfc_release(struct file *file)
  802. {
  803. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  804. struct s5p_mfc_dev *dev = ctx->dev;
  805. mfc_debug_enter();
  806. mutex_lock(&dev->mfc_mutex);
  807. s5p_mfc_clock_on();
  808. vb2_queue_release(&ctx->vq_src);
  809. vb2_queue_release(&ctx->vq_dst);
  810. /* Mark context as idle */
  811. clear_work_bit_irqsave(ctx);
  812. /* If instance was initialised then
  813. * return instance and free reosurces */
  814. if (ctx->inst_no != MFC_NO_INSTANCE_SET) {
  815. mfc_debug(2, "Has to free instance\n");
  816. ctx->state = MFCINST_RETURN_INST;
  817. set_work_bit_irqsave(ctx);
  818. s5p_mfc_clean_ctx_int_flags(ctx);
  819. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  820. /* Wait until instance is returned or timeout occured */
  821. if (s5p_mfc_wait_for_done_ctx
  822. (ctx, S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET, 0)) {
  823. s5p_mfc_clock_off();
  824. mfc_err("Err returning instance\n");
  825. }
  826. mfc_debug(2, "After free instance\n");
  827. /* Free resources */
  828. s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
  829. s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer, ctx);
  830. if (ctx->type == MFCINST_DECODER)
  831. s5p_mfc_hw_call(dev->mfc_ops, release_dec_desc_buffer,
  832. ctx);
  833. ctx->inst_no = MFC_NO_INSTANCE_SET;
  834. }
  835. /* hardware locking scheme */
  836. if (dev->curr_ctx == ctx->num)
  837. clear_bit(0, &dev->hw_lock);
  838. dev->num_inst--;
  839. if (dev->num_inst == 0) {
  840. mfc_debug(2, "Last instance - release firmware\n");
  841. /* reset <-> F/W release */
  842. s5p_mfc_reset(dev);
  843. s5p_mfc_deinit_hw(dev);
  844. s5p_mfc_release_firmware(dev);
  845. del_timer_sync(&dev->watchdog_timer);
  846. if (s5p_mfc_power_off() < 0)
  847. mfc_err("Power off failed\n");
  848. }
  849. mfc_debug(2, "Shutting down clock\n");
  850. s5p_mfc_clock_off();
  851. dev->ctx[ctx->num] = NULL;
  852. s5p_mfc_dec_ctrls_delete(ctx);
  853. v4l2_fh_del(&ctx->fh);
  854. v4l2_fh_exit(&ctx->fh);
  855. kfree(ctx);
  856. mfc_debug_leave();
  857. mutex_unlock(&dev->mfc_mutex);
  858. return 0;
  859. }
  860. /* Poll */
  861. static unsigned int s5p_mfc_poll(struct file *file,
  862. struct poll_table_struct *wait)
  863. {
  864. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  865. struct s5p_mfc_dev *dev = ctx->dev;
  866. struct vb2_queue *src_q, *dst_q;
  867. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  868. unsigned int rc = 0;
  869. unsigned long flags;
  870. mutex_lock(&dev->mfc_mutex);
  871. src_q = &ctx->vq_src;
  872. dst_q = &ctx->vq_dst;
  873. /*
  874. * There has to be at least one buffer queued on each queued_list, which
  875. * means either in driver already or waiting for driver to claim it
  876. * and start processing.
  877. */
  878. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  879. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  880. rc = POLLERR;
  881. goto end;
  882. }
  883. mutex_unlock(&dev->mfc_mutex);
  884. poll_wait(file, &ctx->fh.wait, wait);
  885. poll_wait(file, &src_q->done_wq, wait);
  886. poll_wait(file, &dst_q->done_wq, wait);
  887. mutex_lock(&dev->mfc_mutex);
  888. if (v4l2_event_pending(&ctx->fh))
  889. rc |= POLLPRI;
  890. spin_lock_irqsave(&src_q->done_lock, flags);
  891. if (!list_empty(&src_q->done_list))
  892. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  893. done_entry);
  894. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  895. || src_vb->state == VB2_BUF_STATE_ERROR))
  896. rc |= POLLOUT | POLLWRNORM;
  897. spin_unlock_irqrestore(&src_q->done_lock, flags);
  898. spin_lock_irqsave(&dst_q->done_lock, flags);
  899. if (!list_empty(&dst_q->done_list))
  900. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  901. done_entry);
  902. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  903. || dst_vb->state == VB2_BUF_STATE_ERROR))
  904. rc |= POLLIN | POLLRDNORM;
  905. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  906. end:
  907. mutex_unlock(&dev->mfc_mutex);
  908. return rc;
  909. }
  910. /* Mmap */
  911. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  912. {
  913. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  914. struct s5p_mfc_dev *dev = ctx->dev;
  915. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  916. int ret;
  917. if (mutex_lock_interruptible(&dev->mfc_mutex))
  918. return -ERESTARTSYS;
  919. if (offset < DST_QUEUE_OFF_BASE) {
  920. mfc_debug(2, "mmaping source\n");
  921. ret = vb2_mmap(&ctx->vq_src, vma);
  922. } else { /* capture */
  923. mfc_debug(2, "mmaping destination\n");
  924. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  925. ret = vb2_mmap(&ctx->vq_dst, vma);
  926. }
  927. mutex_unlock(&dev->mfc_mutex);
  928. return ret;
  929. }
  930. /* v4l2 ops */
  931. static const struct v4l2_file_operations s5p_mfc_fops = {
  932. .owner = THIS_MODULE,
  933. .open = s5p_mfc_open,
  934. .release = s5p_mfc_release,
  935. .poll = s5p_mfc_poll,
  936. .unlocked_ioctl = video_ioctl2,
  937. .mmap = s5p_mfc_mmap,
  938. };
  939. static int match_child(struct device *dev, void *data)
  940. {
  941. if (!dev_name(dev))
  942. return 0;
  943. return !strcmp(dev_name(dev), (char *)data);
  944. }
  945. /* MFC probe function */
  946. static int s5p_mfc_probe(struct platform_device *pdev)
  947. {
  948. struct s5p_mfc_dev *dev;
  949. struct video_device *vfd;
  950. struct resource *res;
  951. int ret;
  952. pr_debug("%s++\n", __func__);
  953. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  954. if (!dev) {
  955. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  956. return -ENOMEM;
  957. }
  958. spin_lock_init(&dev->irqlock);
  959. spin_lock_init(&dev->condlock);
  960. dev->plat_dev = pdev;
  961. if (!dev->plat_dev) {
  962. dev_err(&pdev->dev, "No platform data specified\n");
  963. return -ENODEV;
  964. }
  965. dev->variant = (struct s5p_mfc_variant *)
  966. platform_get_device_id(pdev)->driver_data;
  967. ret = s5p_mfc_init_pm(dev);
  968. if (ret < 0) {
  969. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  970. return ret;
  971. }
  972. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  973. dev->regs_base = devm_request_and_ioremap(&pdev->dev, res);
  974. if (dev->regs_base == NULL) {
  975. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  976. return -ENOENT;
  977. }
  978. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  979. if (res == NULL) {
  980. dev_err(&pdev->dev, "failed to get irq resource\n");
  981. ret = -ENOENT;
  982. goto err_res;
  983. }
  984. dev->irq = res->start;
  985. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  986. IRQF_DISABLED, pdev->name, dev);
  987. if (ret) {
  988. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  989. goto err_res;
  990. }
  991. dev->mem_dev_l = device_find_child(&dev->plat_dev->dev, "s5p-mfc-l",
  992. match_child);
  993. if (!dev->mem_dev_l) {
  994. mfc_err("Mem child (L) device get failed\n");
  995. ret = -ENODEV;
  996. goto err_res;
  997. }
  998. dev->mem_dev_r = device_find_child(&dev->plat_dev->dev, "s5p-mfc-r",
  999. match_child);
  1000. if (!dev->mem_dev_r) {
  1001. mfc_err("Mem child (R) device get failed\n");
  1002. ret = -ENODEV;
  1003. goto err_res;
  1004. }
  1005. dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
  1006. if (IS_ERR_OR_NULL(dev->alloc_ctx[0])) {
  1007. ret = PTR_ERR(dev->alloc_ctx[0]);
  1008. goto err_res;
  1009. }
  1010. dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
  1011. if (IS_ERR_OR_NULL(dev->alloc_ctx[1])) {
  1012. ret = PTR_ERR(dev->alloc_ctx[1]);
  1013. goto err_mem_init_ctx_1;
  1014. }
  1015. mutex_init(&dev->mfc_mutex);
  1016. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1017. if (ret)
  1018. goto err_v4l2_dev_reg;
  1019. init_waitqueue_head(&dev->queue);
  1020. /* decoder */
  1021. vfd = video_device_alloc();
  1022. if (!vfd) {
  1023. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1024. ret = -ENOMEM;
  1025. goto err_dec_alloc;
  1026. }
  1027. vfd->fops = &s5p_mfc_fops,
  1028. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1029. vfd->release = video_device_release,
  1030. vfd->lock = &dev->mfc_mutex;
  1031. vfd->v4l2_dev = &dev->v4l2_dev;
  1032. vfd->vfl_dir = VFL_DIR_M2M;
  1033. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1034. dev->vfd_dec = vfd;
  1035. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1036. if (ret) {
  1037. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1038. video_device_release(vfd);
  1039. goto err_dec_reg;
  1040. }
  1041. v4l2_info(&dev->v4l2_dev,
  1042. "decoder registered as /dev/video%d\n", vfd->num);
  1043. video_set_drvdata(vfd, dev);
  1044. /* encoder */
  1045. vfd = video_device_alloc();
  1046. if (!vfd) {
  1047. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1048. ret = -ENOMEM;
  1049. goto err_enc_alloc;
  1050. }
  1051. vfd->fops = &s5p_mfc_fops,
  1052. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1053. vfd->release = video_device_release,
  1054. vfd->lock = &dev->mfc_mutex;
  1055. vfd->v4l2_dev = &dev->v4l2_dev;
  1056. vfd->vfl_dir = VFL_DIR_M2M;
  1057. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1058. dev->vfd_enc = vfd;
  1059. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1060. if (ret) {
  1061. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1062. video_device_release(vfd);
  1063. goto err_enc_reg;
  1064. }
  1065. v4l2_info(&dev->v4l2_dev,
  1066. "encoder registered as /dev/video%d\n", vfd->num);
  1067. video_set_drvdata(vfd, dev);
  1068. platform_set_drvdata(pdev, dev);
  1069. dev->hw_lock = 0;
  1070. dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
  1071. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1072. atomic_set(&dev->watchdog_cnt, 0);
  1073. init_timer(&dev->watchdog_timer);
  1074. dev->watchdog_timer.data = (unsigned long)dev;
  1075. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1076. /* Initialize HW ops and commands based on MFC version */
  1077. s5p_mfc_init_hw_ops(dev);
  1078. s5p_mfc_init_hw_cmds(dev);
  1079. pr_debug("%s--\n", __func__);
  1080. return 0;
  1081. /* Deinit MFC if probe had failed */
  1082. err_enc_reg:
  1083. video_device_release(dev->vfd_enc);
  1084. err_enc_alloc:
  1085. video_unregister_device(dev->vfd_dec);
  1086. err_dec_reg:
  1087. video_device_release(dev->vfd_dec);
  1088. err_dec_alloc:
  1089. v4l2_device_unregister(&dev->v4l2_dev);
  1090. err_v4l2_dev_reg:
  1091. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1092. err_mem_init_ctx_1:
  1093. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1094. err_res:
  1095. s5p_mfc_final_pm(dev);
  1096. pr_debug("%s-- with error\n", __func__);
  1097. return ret;
  1098. }
  1099. /* Remove the driver */
  1100. static int __devexit s5p_mfc_remove(struct platform_device *pdev)
  1101. {
  1102. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1103. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1104. del_timer_sync(&dev->watchdog_timer);
  1105. flush_workqueue(dev->watchdog_workqueue);
  1106. destroy_workqueue(dev->watchdog_workqueue);
  1107. video_unregister_device(dev->vfd_enc);
  1108. video_unregister_device(dev->vfd_dec);
  1109. v4l2_device_unregister(&dev->v4l2_dev);
  1110. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1111. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1112. s5p_mfc_final_pm(dev);
  1113. return 0;
  1114. }
  1115. #ifdef CONFIG_PM_SLEEP
  1116. static int s5p_mfc_suspend(struct device *dev)
  1117. {
  1118. struct platform_device *pdev = to_platform_device(dev);
  1119. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1120. int ret;
  1121. if (m_dev->num_inst == 0)
  1122. return 0;
  1123. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1124. mfc_err("Error: going to suspend for a second time\n");
  1125. return -EIO;
  1126. }
  1127. /* Check if we're processing then wait if it necessary. */
  1128. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1129. /* Try and lock the HW */
  1130. /* Wait on the interrupt waitqueue */
  1131. ret = wait_event_interruptible_timeout(m_dev->queue,
  1132. m_dev->int_cond || m_dev->ctx[m_dev->curr_ctx]->int_cond,
  1133. msecs_to_jiffies(MFC_INT_TIMEOUT));
  1134. if (ret == 0) {
  1135. mfc_err("Waiting for hardware to finish timed out\n");
  1136. return -EIO;
  1137. }
  1138. }
  1139. return s5p_mfc_sleep(m_dev);
  1140. }
  1141. static int s5p_mfc_resume(struct device *dev)
  1142. {
  1143. struct platform_device *pdev = to_platform_device(dev);
  1144. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1145. if (m_dev->num_inst == 0)
  1146. return 0;
  1147. return s5p_mfc_wakeup(m_dev);
  1148. }
  1149. #endif
  1150. #ifdef CONFIG_PM_RUNTIME
  1151. static int s5p_mfc_runtime_suspend(struct device *dev)
  1152. {
  1153. struct platform_device *pdev = to_platform_device(dev);
  1154. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1155. atomic_set(&m_dev->pm.power, 0);
  1156. return 0;
  1157. }
  1158. static int s5p_mfc_runtime_resume(struct device *dev)
  1159. {
  1160. struct platform_device *pdev = to_platform_device(dev);
  1161. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1162. int pre_power;
  1163. if (!m_dev->alloc_ctx)
  1164. return 0;
  1165. pre_power = atomic_read(&m_dev->pm.power);
  1166. atomic_set(&m_dev->pm.power, 1);
  1167. return 0;
  1168. }
  1169. #endif
  1170. /* Power management */
  1171. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1172. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1173. SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
  1174. NULL)
  1175. };
  1176. struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
  1177. .h264_ctx = MFC_H264_CTX_BUF_SIZE,
  1178. .non_h264_ctx = MFC_CTX_BUF_SIZE,
  1179. .dsc = DESC_BUF_SIZE,
  1180. .shm = SHARED_BUF_SIZE,
  1181. };
  1182. struct s5p_mfc_buf_size buf_size_v5 = {
  1183. .fw = MAX_FW_SIZE,
  1184. .cpb = MAX_CPB_SIZE,
  1185. .priv = &mfc_buf_size_v5,
  1186. };
  1187. struct s5p_mfc_buf_align mfc_buf_align_v5 = {
  1188. .base = MFC_BASE_ALIGN_ORDER,
  1189. };
  1190. static struct s5p_mfc_variant mfc_drvdata_v5 = {
  1191. .version = MFC_VERSION,
  1192. .port_num = MFC_NUM_PORTS,
  1193. .buf_size = &buf_size_v5,
  1194. .buf_align = &mfc_buf_align_v5,
  1195. .mclk_name = "sclk_mfc",
  1196. .fw_name = "s5p-mfc.fw",
  1197. };
  1198. struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
  1199. .dev_ctx = MFC_CTX_BUF_SIZE_V6,
  1200. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
  1201. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
  1202. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
  1203. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
  1204. };
  1205. struct s5p_mfc_buf_size buf_size_v6 = {
  1206. .fw = MAX_FW_SIZE_V6,
  1207. .cpb = MAX_CPB_SIZE_V6,
  1208. .priv = &mfc_buf_size_v6,
  1209. };
  1210. struct s5p_mfc_buf_align mfc_buf_align_v6 = {
  1211. .base = 0,
  1212. };
  1213. static struct s5p_mfc_variant mfc_drvdata_v6 = {
  1214. .version = MFC_VERSION_V6,
  1215. .port_num = MFC_NUM_PORTS_V6,
  1216. .buf_size = &buf_size_v6,
  1217. .buf_align = &mfc_buf_align_v6,
  1218. .mclk_name = "aclk_333",
  1219. .fw_name = "s5p-mfc-v6.fw",
  1220. };
  1221. static struct platform_device_id mfc_driver_ids[] = {
  1222. {
  1223. .name = "s5p-mfc",
  1224. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1225. }, {
  1226. .name = "s5p-mfc-v5",
  1227. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1228. }, {
  1229. .name = "s5p-mfc-v6",
  1230. .driver_data = (unsigned long)&mfc_drvdata_v6,
  1231. },
  1232. {},
  1233. };
  1234. MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
  1235. static struct platform_driver s5p_mfc_driver = {
  1236. .probe = s5p_mfc_probe,
  1237. .remove = __devexit_p(s5p_mfc_remove),
  1238. .id_table = mfc_driver_ids,
  1239. .driver = {
  1240. .name = S5P_MFC_NAME,
  1241. .owner = THIS_MODULE,
  1242. .pm = &s5p_mfc_pm_ops
  1243. },
  1244. };
  1245. module_platform_driver(s5p_mfc_driver);
  1246. MODULE_LICENSE("GPL");
  1247. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1248. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");