s5p_mfc.c 40 KB

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