s5p_mfc.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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 mfc_debug_level;
  38. module_param_named(debug, mfc_debug_level, 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_void(dev->mfc_ops, cleanup_queue,
  133. &ctx->dst_queue, &ctx->vq_dst);
  134. s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
  135. &ctx->src_queue, &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. /* De-init MFC */
  142. s5p_mfc_deinit_hw(dev);
  143. /* Double check if there is at least one instance running.
  144. * If no instance is in memory than no firmware should be present */
  145. if (dev->num_inst > 0) {
  146. ret = s5p_mfc_load_firmware(dev);
  147. if (ret) {
  148. mfc_err("Failed to reload FW\n");
  149. goto unlock;
  150. }
  151. s5p_mfc_clock_on();
  152. ret = s5p_mfc_init_hw(dev);
  153. if (ret)
  154. mfc_err("Failed to reinit FW\n");
  155. }
  156. unlock:
  157. if (mutex_locked)
  158. mutex_unlock(&dev->mfc_mutex);
  159. }
  160. static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
  161. {
  162. mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
  163. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
  164. mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
  165. }
  166. static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
  167. {
  168. struct s5p_mfc_buf *dst_buf;
  169. struct s5p_mfc_dev *dev = ctx->dev;
  170. ctx->state = MFCINST_FINISHED;
  171. ctx->sequence++;
  172. while (!list_empty(&ctx->dst_queue)) {
  173. dst_buf = list_entry(ctx->dst_queue.next,
  174. struct s5p_mfc_buf, list);
  175. mfc_debug(2, "Cleaning up buffer: %d\n",
  176. dst_buf->b->v4l2_buf.index);
  177. vb2_set_plane_payload(dst_buf->b, 0, 0);
  178. vb2_set_plane_payload(dst_buf->b, 1, 0);
  179. list_del(&dst_buf->list);
  180. ctx->dst_queue_cnt--;
  181. dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
  182. if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
  183. s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
  184. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  185. else
  186. dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
  187. ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
  188. vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
  189. }
  190. }
  191. static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
  192. {
  193. struct s5p_mfc_dev *dev = ctx->dev;
  194. struct s5p_mfc_buf *dst_buf, *src_buf;
  195. size_t dec_y_addr;
  196. unsigned int frame_type;
  197. /* Make sure we actually have a new frame before continuing. */
  198. frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
  199. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
  200. return;
  201. dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
  202. /* Copy timestamp / timecode from decoded src to dst and set
  203. appropriate flags. */
  204. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  205. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  206. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
  207. dst_buf->b->v4l2_buf.timecode =
  208. src_buf->b->v4l2_buf.timecode;
  209. dst_buf->b->v4l2_buf.timestamp =
  210. src_buf->b->v4l2_buf.timestamp;
  211. dst_buf->b->v4l2_buf.flags &=
  212. ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  213. dst_buf->b->v4l2_buf.flags |=
  214. src_buf->b->v4l2_buf.flags
  215. & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  216. switch (frame_type) {
  217. case S5P_FIMV_DECODE_FRAME_I_FRAME:
  218. dst_buf->b->v4l2_buf.flags |=
  219. V4L2_BUF_FLAG_KEYFRAME;
  220. break;
  221. case S5P_FIMV_DECODE_FRAME_P_FRAME:
  222. dst_buf->b->v4l2_buf.flags |=
  223. V4L2_BUF_FLAG_PFRAME;
  224. break;
  225. case S5P_FIMV_DECODE_FRAME_B_FRAME:
  226. dst_buf->b->v4l2_buf.flags |=
  227. V4L2_BUF_FLAG_BFRAME;
  228. break;
  229. default:
  230. /* Don't know how to handle
  231. S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
  232. mfc_debug(2, "Unexpected frame type: %d\n",
  233. frame_type);
  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. if (IS_MFCV6_PLUS(dev))
  247. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  248. get_disp_frame_type, ctx);
  249. else
  250. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  251. get_dec_frame_type, dev);
  252. /* If frame is same as previous then skip and do not dequeue */
  253. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
  254. if (!ctx->after_packed_pb)
  255. ctx->sequence++;
  256. ctx->after_packed_pb = 0;
  257. return;
  258. }
  259. ctx->sequence++;
  260. /* The MFC returns address of the buffer, now we have to
  261. * check which videobuf does it correspond to */
  262. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  263. /* Check if this is the buffer we're looking for */
  264. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
  265. list_del(&dst_buf->list);
  266. ctx->dst_queue_cnt--;
  267. dst_buf->b->v4l2_buf.sequence = ctx->sequence;
  268. if (s5p_mfc_hw_call(dev->mfc_ops,
  269. get_pic_type_top, ctx) ==
  270. s5p_mfc_hw_call(dev->mfc_ops,
  271. get_pic_type_bot, ctx))
  272. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  273. else
  274. dst_buf->b->v4l2_buf.field =
  275. V4L2_FIELD_INTERLACED;
  276. vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
  277. vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
  278. clear_bit(dst_buf->b->v4l2_buf.index,
  279. &ctx->dec_dst_flag);
  280. vb2_buffer_done(dst_buf->b,
  281. err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  282. break;
  283. }
  284. }
  285. }
  286. /* Handle frame decoding interrupt */
  287. static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
  288. unsigned int reason, unsigned int err)
  289. {
  290. struct s5p_mfc_dev *dev = ctx->dev;
  291. unsigned int dst_frame_status;
  292. unsigned int dec_frame_status;
  293. struct s5p_mfc_buf *src_buf;
  294. unsigned long flags;
  295. unsigned int res_change;
  296. dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  297. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  298. dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
  299. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  300. res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  301. & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
  302. >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
  303. mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
  304. if (ctx->state == MFCINST_RES_CHANGE_INIT)
  305. ctx->state = MFCINST_RES_CHANGE_FLUSH;
  306. if (res_change == S5P_FIMV_RES_INCREASE ||
  307. res_change == S5P_FIMV_RES_DECREASE) {
  308. ctx->state = MFCINST_RES_CHANGE_INIT;
  309. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  310. wake_up_ctx(ctx, reason, err);
  311. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  312. s5p_mfc_clock_off();
  313. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  314. return;
  315. }
  316. if (ctx->dpb_flush_flag)
  317. ctx->dpb_flush_flag = 0;
  318. spin_lock_irqsave(&dev->irqlock, flags);
  319. /* All frames remaining in the buffer have been extracted */
  320. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
  321. if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
  322. static const struct v4l2_event ev_src_ch = {
  323. .type = V4L2_EVENT_SOURCE_CHANGE,
  324. .u.src_change.changes =
  325. V4L2_EVENT_SRC_CH_RESOLUTION,
  326. };
  327. s5p_mfc_handle_frame_all_extracted(ctx);
  328. ctx->state = MFCINST_RES_CHANGE_END;
  329. v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
  330. goto leave_handle_frame;
  331. } else {
  332. s5p_mfc_handle_frame_all_extracted(ctx);
  333. }
  334. }
  335. if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
  336. s5p_mfc_handle_frame_copy_time(ctx);
  337. /* A frame has been decoded and is in the buffer */
  338. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
  339. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
  340. s5p_mfc_handle_frame_new(ctx, err);
  341. } else {
  342. mfc_debug(2, "No frame decode\n");
  343. }
  344. /* Mark source buffer as complete */
  345. if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
  346. && !list_empty(&ctx->src_queue)) {
  347. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  348. list);
  349. ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
  350. get_consumed_stream, dev);
  351. if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
  352. ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
  353. ctx->consumed_stream + STUFF_BYTE <
  354. src_buf->b->v4l2_planes[0].bytesused) {
  355. /* Run MFC again on the same buffer */
  356. mfc_debug(2, "Running again the same buffer\n");
  357. ctx->after_packed_pb = 1;
  358. } else {
  359. mfc_debug(2, "MFC needs next buffer\n");
  360. ctx->consumed_stream = 0;
  361. if (src_buf->flags & MFC_BUF_FLAG_EOS)
  362. ctx->state = MFCINST_FINISHING;
  363. list_del(&src_buf->list);
  364. ctx->src_queue_cnt--;
  365. if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
  366. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
  367. else
  368. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
  369. }
  370. }
  371. leave_handle_frame:
  372. spin_unlock_irqrestore(&dev->irqlock, flags);
  373. if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
  374. || ctx->dst_queue_cnt < ctx->pb_count)
  375. clear_work_bit(ctx);
  376. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  377. wake_up_ctx(ctx, reason, err);
  378. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  379. s5p_mfc_clock_off();
  380. /* if suspending, wake up device and do not try_run again*/
  381. if (test_bit(0, &dev->enter_suspend))
  382. wake_up_dev(dev, reason, err);
  383. else
  384. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  385. }
  386. /* Error handling for interrupt */
  387. static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
  388. struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
  389. {
  390. unsigned long flags;
  391. mfc_err("Interrupt Error: %08x\n", err);
  392. if (ctx != NULL) {
  393. /* Error recovery is dependent on the state of context */
  394. switch (ctx->state) {
  395. case MFCINST_RES_CHANGE_INIT:
  396. case MFCINST_RES_CHANGE_FLUSH:
  397. case MFCINST_RES_CHANGE_END:
  398. case MFCINST_FINISHING:
  399. case MFCINST_FINISHED:
  400. case MFCINST_RUNNING:
  401. /* It is highly probable that an error occurred
  402. * while decoding a frame */
  403. clear_work_bit(ctx);
  404. ctx->state = MFCINST_ERROR;
  405. /* Mark all dst buffers as having an error */
  406. spin_lock_irqsave(&dev->irqlock, flags);
  407. s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
  408. &ctx->dst_queue, &ctx->vq_dst);
  409. /* Mark all src buffers as having an error */
  410. s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
  411. &ctx->src_queue, &ctx->vq_src);
  412. spin_unlock_irqrestore(&dev->irqlock, flags);
  413. wake_up_ctx(ctx, reason, err);
  414. break;
  415. default:
  416. clear_work_bit(ctx);
  417. ctx->state = MFCINST_ERROR;
  418. wake_up_ctx(ctx, reason, err);
  419. break;
  420. }
  421. }
  422. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  423. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  424. s5p_mfc_clock_off();
  425. wake_up_dev(dev, reason, err);
  426. return;
  427. }
  428. /* Header parsing interrupt handling */
  429. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  430. unsigned int reason, unsigned int err)
  431. {
  432. struct s5p_mfc_dev *dev;
  433. if (ctx == NULL)
  434. return;
  435. dev = ctx->dev;
  436. if (ctx->c_ops->post_seq_start) {
  437. if (ctx->c_ops->post_seq_start(ctx))
  438. mfc_err("post_seq_start() failed\n");
  439. } else {
  440. ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
  441. dev);
  442. ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
  443. dev);
  444. s5p_mfc_hw_call_void(dev->mfc_ops, dec_calc_dpb_size, ctx);
  445. ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
  446. dev);
  447. ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
  448. dev);
  449. if (ctx->img_width == 0 || ctx->img_height == 0)
  450. ctx->state = MFCINST_ERROR;
  451. else
  452. ctx->state = MFCINST_HEAD_PARSED;
  453. if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  454. ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
  455. !list_empty(&ctx->src_queue)) {
  456. struct s5p_mfc_buf *src_buf;
  457. src_buf = list_entry(ctx->src_queue.next,
  458. struct s5p_mfc_buf, list);
  459. if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
  460. dev) <
  461. src_buf->b->v4l2_planes[0].bytesused)
  462. ctx->head_processed = 0;
  463. else
  464. ctx->head_processed = 1;
  465. } else {
  466. ctx->head_processed = 1;
  467. }
  468. }
  469. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  470. clear_work_bit(ctx);
  471. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  472. s5p_mfc_clock_off();
  473. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  474. wake_up_ctx(ctx, reason, err);
  475. }
  476. /* Header parsing interrupt handling */
  477. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  478. unsigned int reason, unsigned int err)
  479. {
  480. struct s5p_mfc_buf *src_buf;
  481. struct s5p_mfc_dev *dev;
  482. unsigned long flags;
  483. if (ctx == NULL)
  484. return;
  485. dev = ctx->dev;
  486. s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
  487. ctx->int_type = reason;
  488. ctx->int_err = err;
  489. ctx->int_cond = 1;
  490. clear_work_bit(ctx);
  491. if (err == 0) {
  492. ctx->state = MFCINST_RUNNING;
  493. if (!ctx->dpb_flush_flag && ctx->head_processed) {
  494. spin_lock_irqsave(&dev->irqlock, flags);
  495. if (!list_empty(&ctx->src_queue)) {
  496. src_buf = list_entry(ctx->src_queue.next,
  497. struct s5p_mfc_buf, list);
  498. list_del(&src_buf->list);
  499. ctx->src_queue_cnt--;
  500. vb2_buffer_done(src_buf->b,
  501. VB2_BUF_STATE_DONE);
  502. }
  503. spin_unlock_irqrestore(&dev->irqlock, flags);
  504. } else {
  505. ctx->dpb_flush_flag = 0;
  506. }
  507. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  508. s5p_mfc_clock_off();
  509. wake_up(&ctx->queue);
  510. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  511. } else {
  512. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  513. s5p_mfc_clock_off();
  514. wake_up(&ctx->queue);
  515. }
  516. }
  517. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
  518. unsigned int reason, unsigned int err)
  519. {
  520. struct s5p_mfc_dev *dev = ctx->dev;
  521. struct s5p_mfc_buf *mb_entry;
  522. mfc_debug(2, "Stream completed\n");
  523. s5p_mfc_clear_int_flags(dev);
  524. ctx->int_type = reason;
  525. ctx->int_err = err;
  526. ctx->state = MFCINST_FINISHED;
  527. spin_lock(&dev->irqlock);
  528. if (!list_empty(&ctx->dst_queue)) {
  529. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  530. list);
  531. list_del(&mb_entry->list);
  532. ctx->dst_queue_cnt--;
  533. vb2_set_plane_payload(mb_entry->b, 0, 0);
  534. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  535. }
  536. spin_unlock(&dev->irqlock);
  537. clear_work_bit(ctx);
  538. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  539. s5p_mfc_clock_off();
  540. wake_up(&ctx->queue);
  541. s5p_mfc_hw_call_void(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 occurred */
  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(dev, 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_void(dev->mfc_ops, clear_int_flags, dev);
  576. wake_up_ctx(ctx, reason, err);
  577. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  578. s5p_mfc_clock_off();
  579. s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
  580. } else {
  581. s5p_mfc_handle_frame(ctx, reason, err);
  582. }
  583. break;
  584. case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
  585. s5p_mfc_handle_seq_done(ctx, reason, err);
  586. break;
  587. case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
  588. ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
  589. ctx->state = MFCINST_GOT_INST;
  590. clear_work_bit(ctx);
  591. wake_up(&ctx->queue);
  592. goto irq_cleanup_hw;
  593. case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
  594. clear_work_bit(ctx);
  595. ctx->inst_no = MFC_NO_INSTANCE_SET;
  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_void(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_void(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_void(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_void(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 video_device *vdev = video_devdata(file);
  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, vdev);
  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 (vdev == dev->vfd_dec) {
  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 (vdev == dev->vfd_enc) {
  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 = MFC_NO_INSTANCE_SET;
  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_load_firmware(dev);
  720. if (ret) {
  721. s5p_mfc_clock_off();
  722. goto err_load_fw;
  723. }
  724. /* Init the FW */
  725. ret = s5p_mfc_init_hw(dev);
  726. s5p_mfc_clock_off();
  727. if (ret)
  728. goto err_init_hw;
  729. }
  730. /* Init videobuf2 queue for CAPTURE */
  731. q = &ctx->vq_dst;
  732. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  733. q->drv_priv = &ctx->fh;
  734. q->lock = &dev->mfc_mutex;
  735. if (vdev == dev->vfd_dec) {
  736. q->io_modes = VB2_MMAP;
  737. q->ops = get_dec_queue_ops();
  738. } else if (vdev == dev->vfd_enc) {
  739. q->io_modes = VB2_MMAP | VB2_USERPTR;
  740. q->ops = get_enc_queue_ops();
  741. } else {
  742. ret = -ENOENT;
  743. goto err_queue_init;
  744. }
  745. q->mem_ops = &vb2_dma_contig_memops;
  746. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  747. ret = vb2_queue_init(q);
  748. if (ret) {
  749. mfc_err("Failed to initialize videobuf2 queue(capture)\n");
  750. goto err_queue_init;
  751. }
  752. /* Init videobuf2 queue for OUTPUT */
  753. q = &ctx->vq_src;
  754. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  755. q->io_modes = VB2_MMAP;
  756. q->drv_priv = &ctx->fh;
  757. q->lock = &dev->mfc_mutex;
  758. if (vdev == dev->vfd_dec) {
  759. q->io_modes = VB2_MMAP;
  760. q->ops = get_dec_queue_ops();
  761. } else if (vdev == dev->vfd_enc) {
  762. q->io_modes = VB2_MMAP | VB2_USERPTR;
  763. q->ops = get_enc_queue_ops();
  764. } else {
  765. ret = -ENOENT;
  766. goto err_queue_init;
  767. }
  768. /* One way to indicate end-of-stream for MFC is to set the
  769. * bytesused == 0. However by default videobuf2 handles bytesused
  770. * equal to 0 as a special case and changes its value to the size
  771. * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
  772. * will keep the value of bytesused intact.
  773. */
  774. q->allow_zero_bytesused = 1;
  775. q->mem_ops = &vb2_dma_contig_memops;
  776. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  777. ret = vb2_queue_init(q);
  778. if (ret) {
  779. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  780. goto err_queue_init;
  781. }
  782. init_waitqueue_head(&ctx->queue);
  783. mutex_unlock(&dev->mfc_mutex);
  784. mfc_debug_leave();
  785. return ret;
  786. /* Deinit when failure occurred */
  787. err_queue_init:
  788. if (dev->num_inst == 1)
  789. s5p_mfc_deinit_hw(dev);
  790. err_init_hw:
  791. err_load_fw:
  792. err_pwr_enable:
  793. if (dev->num_inst == 1) {
  794. if (s5p_mfc_power_off() < 0)
  795. mfc_err("power off failed\n");
  796. del_timer_sync(&dev->watchdog_timer);
  797. }
  798. err_ctrls_setup:
  799. s5p_mfc_dec_ctrls_delete(ctx);
  800. err_bad_node:
  801. dev->ctx[ctx->num] = NULL;
  802. err_no_ctx:
  803. v4l2_fh_del(&ctx->fh);
  804. v4l2_fh_exit(&ctx->fh);
  805. kfree(ctx);
  806. err_alloc:
  807. dev->num_inst--;
  808. mutex_unlock(&dev->mfc_mutex);
  809. mfc_debug_leave();
  810. return ret;
  811. }
  812. /* Release MFC context */
  813. static int s5p_mfc_release(struct file *file)
  814. {
  815. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  816. struct s5p_mfc_dev *dev = ctx->dev;
  817. mfc_debug_enter();
  818. mutex_lock(&dev->mfc_mutex);
  819. s5p_mfc_clock_on();
  820. vb2_queue_release(&ctx->vq_src);
  821. vb2_queue_release(&ctx->vq_dst);
  822. /* Mark context as idle */
  823. clear_work_bit_irqsave(ctx);
  824. /* If instance was initialised and not yet freed,
  825. * return instance and free resources */
  826. if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
  827. mfc_debug(2, "Has to free instance\n");
  828. s5p_mfc_close_mfc_inst(dev, ctx);
  829. }
  830. /* hardware locking scheme */
  831. if (dev->curr_ctx == ctx->num)
  832. clear_bit(0, &dev->hw_lock);
  833. dev->num_inst--;
  834. if (dev->num_inst == 0) {
  835. mfc_debug(2, "Last instance\n");
  836. s5p_mfc_deinit_hw(dev);
  837. del_timer_sync(&dev->watchdog_timer);
  838. if (s5p_mfc_power_off() < 0)
  839. mfc_err("Power off failed\n");
  840. }
  841. mfc_debug(2, "Shutting down clock\n");
  842. s5p_mfc_clock_off();
  843. dev->ctx[ctx->num] = NULL;
  844. s5p_mfc_dec_ctrls_delete(ctx);
  845. v4l2_fh_del(&ctx->fh);
  846. v4l2_fh_exit(&ctx->fh);
  847. kfree(ctx);
  848. mfc_debug_leave();
  849. mutex_unlock(&dev->mfc_mutex);
  850. return 0;
  851. }
  852. /* Poll */
  853. static unsigned int s5p_mfc_poll(struct file *file,
  854. struct poll_table_struct *wait)
  855. {
  856. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  857. struct s5p_mfc_dev *dev = ctx->dev;
  858. struct vb2_queue *src_q, *dst_q;
  859. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  860. unsigned int rc = 0;
  861. unsigned long flags;
  862. mutex_lock(&dev->mfc_mutex);
  863. src_q = &ctx->vq_src;
  864. dst_q = &ctx->vq_dst;
  865. /*
  866. * There has to be at least one buffer queued on each queued_list, which
  867. * means either in driver already or waiting for driver to claim it
  868. * and start processing.
  869. */
  870. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  871. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  872. rc = POLLERR;
  873. goto end;
  874. }
  875. mutex_unlock(&dev->mfc_mutex);
  876. poll_wait(file, &ctx->fh.wait, wait);
  877. poll_wait(file, &src_q->done_wq, wait);
  878. poll_wait(file, &dst_q->done_wq, wait);
  879. mutex_lock(&dev->mfc_mutex);
  880. if (v4l2_event_pending(&ctx->fh))
  881. rc |= POLLPRI;
  882. spin_lock_irqsave(&src_q->done_lock, flags);
  883. if (!list_empty(&src_q->done_list))
  884. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  885. done_entry);
  886. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  887. || src_vb->state == VB2_BUF_STATE_ERROR))
  888. rc |= POLLOUT | POLLWRNORM;
  889. spin_unlock_irqrestore(&src_q->done_lock, flags);
  890. spin_lock_irqsave(&dst_q->done_lock, flags);
  891. if (!list_empty(&dst_q->done_list))
  892. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  893. done_entry);
  894. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  895. || dst_vb->state == VB2_BUF_STATE_ERROR))
  896. rc |= POLLIN | POLLRDNORM;
  897. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  898. end:
  899. mutex_unlock(&dev->mfc_mutex);
  900. return rc;
  901. }
  902. /* Mmap */
  903. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  904. {
  905. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  906. struct s5p_mfc_dev *dev = ctx->dev;
  907. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  908. int ret;
  909. if (mutex_lock_interruptible(&dev->mfc_mutex))
  910. return -ERESTARTSYS;
  911. if (offset < DST_QUEUE_OFF_BASE) {
  912. mfc_debug(2, "mmaping source\n");
  913. ret = vb2_mmap(&ctx->vq_src, vma);
  914. } else { /* capture */
  915. mfc_debug(2, "mmaping destination\n");
  916. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  917. ret = vb2_mmap(&ctx->vq_dst, vma);
  918. }
  919. mutex_unlock(&dev->mfc_mutex);
  920. return ret;
  921. }
  922. /* v4l2 ops */
  923. static const struct v4l2_file_operations s5p_mfc_fops = {
  924. .owner = THIS_MODULE,
  925. .open = s5p_mfc_open,
  926. .release = s5p_mfc_release,
  927. .poll = s5p_mfc_poll,
  928. .unlocked_ioctl = video_ioctl2,
  929. .mmap = s5p_mfc_mmap,
  930. };
  931. static int match_child(struct device *dev, void *data)
  932. {
  933. if (!dev_name(dev))
  934. return 0;
  935. return !strcmp(dev_name(dev), (char *)data);
  936. }
  937. static void *mfc_get_drv_data(struct platform_device *pdev);
  938. static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev *dev)
  939. {
  940. unsigned int mem_info[2] = { };
  941. dev->mem_dev_l = devm_kzalloc(&dev->plat_dev->dev,
  942. sizeof(struct device), GFP_KERNEL);
  943. if (!dev->mem_dev_l) {
  944. mfc_err("Not enough memory\n");
  945. return -ENOMEM;
  946. }
  947. device_initialize(dev->mem_dev_l);
  948. of_property_read_u32_array(dev->plat_dev->dev.of_node,
  949. "samsung,mfc-l", mem_info, 2);
  950. if (dma_declare_coherent_memory(dev->mem_dev_l, mem_info[0],
  951. mem_info[0], mem_info[1],
  952. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
  953. mfc_err("Failed to declare coherent memory for\n"
  954. "MFC device\n");
  955. return -ENOMEM;
  956. }
  957. dev->mem_dev_r = devm_kzalloc(&dev->plat_dev->dev,
  958. sizeof(struct device), GFP_KERNEL);
  959. if (!dev->mem_dev_r) {
  960. mfc_err("Not enough memory\n");
  961. return -ENOMEM;
  962. }
  963. device_initialize(dev->mem_dev_r);
  964. of_property_read_u32_array(dev->plat_dev->dev.of_node,
  965. "samsung,mfc-r", mem_info, 2);
  966. if (dma_declare_coherent_memory(dev->mem_dev_r, mem_info[0],
  967. mem_info[0], mem_info[1],
  968. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
  969. pr_err("Failed to declare coherent memory for\n"
  970. "MFC device\n");
  971. return -ENOMEM;
  972. }
  973. return 0;
  974. }
  975. /* MFC probe function */
  976. static int s5p_mfc_probe(struct platform_device *pdev)
  977. {
  978. struct s5p_mfc_dev *dev;
  979. struct video_device *vfd;
  980. struct resource *res;
  981. int ret;
  982. pr_debug("%s++\n", __func__);
  983. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  984. if (!dev) {
  985. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  986. return -ENOMEM;
  987. }
  988. spin_lock_init(&dev->irqlock);
  989. spin_lock_init(&dev->condlock);
  990. dev->plat_dev = pdev;
  991. if (!dev->plat_dev) {
  992. dev_err(&pdev->dev, "No platform data specified\n");
  993. return -ENODEV;
  994. }
  995. dev->variant = mfc_get_drv_data(pdev);
  996. ret = s5p_mfc_init_pm(dev);
  997. if (ret < 0) {
  998. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  999. return ret;
  1000. }
  1001. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1002. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  1003. if (IS_ERR(dev->regs_base))
  1004. return PTR_ERR(dev->regs_base);
  1005. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1006. if (res == NULL) {
  1007. dev_err(&pdev->dev, "failed to get irq resource\n");
  1008. ret = -ENOENT;
  1009. goto err_res;
  1010. }
  1011. dev->irq = res->start;
  1012. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  1013. 0, pdev->name, dev);
  1014. if (ret) {
  1015. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  1016. goto err_res;
  1017. }
  1018. if (pdev->dev.of_node) {
  1019. ret = s5p_mfc_alloc_memdevs(dev);
  1020. if (ret < 0)
  1021. goto err_res;
  1022. } else {
  1023. dev->mem_dev_l = device_find_child(&dev->plat_dev->dev,
  1024. "s5p-mfc-l", match_child);
  1025. if (!dev->mem_dev_l) {
  1026. mfc_err("Mem child (L) device get failed\n");
  1027. ret = -ENODEV;
  1028. goto err_res;
  1029. }
  1030. dev->mem_dev_r = device_find_child(&dev->plat_dev->dev,
  1031. "s5p-mfc-r", match_child);
  1032. if (!dev->mem_dev_r) {
  1033. mfc_err("Mem child (R) device get failed\n");
  1034. ret = -ENODEV;
  1035. goto err_res;
  1036. }
  1037. }
  1038. dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
  1039. if (IS_ERR(dev->alloc_ctx[0])) {
  1040. ret = PTR_ERR(dev->alloc_ctx[0]);
  1041. goto err_res;
  1042. }
  1043. dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
  1044. if (IS_ERR(dev->alloc_ctx[1])) {
  1045. ret = PTR_ERR(dev->alloc_ctx[1]);
  1046. goto err_mem_init_ctx_1;
  1047. }
  1048. mutex_init(&dev->mfc_mutex);
  1049. ret = s5p_mfc_alloc_firmware(dev);
  1050. if (ret)
  1051. goto err_alloc_fw;
  1052. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1053. if (ret)
  1054. goto err_v4l2_dev_reg;
  1055. init_waitqueue_head(&dev->queue);
  1056. /* decoder */
  1057. vfd = video_device_alloc();
  1058. if (!vfd) {
  1059. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1060. ret = -ENOMEM;
  1061. goto err_dec_alloc;
  1062. }
  1063. vfd->fops = &s5p_mfc_fops;
  1064. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1065. vfd->release = video_device_release;
  1066. vfd->lock = &dev->mfc_mutex;
  1067. vfd->v4l2_dev = &dev->v4l2_dev;
  1068. vfd->vfl_dir = VFL_DIR_M2M;
  1069. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1070. dev->vfd_dec = vfd;
  1071. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1072. if (ret) {
  1073. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1074. video_device_release(vfd);
  1075. goto err_dec_reg;
  1076. }
  1077. v4l2_info(&dev->v4l2_dev,
  1078. "decoder registered as /dev/video%d\n", vfd->num);
  1079. video_set_drvdata(vfd, dev);
  1080. /* encoder */
  1081. vfd = video_device_alloc();
  1082. if (!vfd) {
  1083. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1084. ret = -ENOMEM;
  1085. goto err_enc_alloc;
  1086. }
  1087. vfd->fops = &s5p_mfc_fops;
  1088. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1089. vfd->release = video_device_release;
  1090. vfd->lock = &dev->mfc_mutex;
  1091. vfd->v4l2_dev = &dev->v4l2_dev;
  1092. vfd->vfl_dir = VFL_DIR_M2M;
  1093. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1094. dev->vfd_enc = vfd;
  1095. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1096. if (ret) {
  1097. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1098. video_device_release(vfd);
  1099. goto err_enc_reg;
  1100. }
  1101. v4l2_info(&dev->v4l2_dev,
  1102. "encoder registered as /dev/video%d\n", vfd->num);
  1103. video_set_drvdata(vfd, dev);
  1104. platform_set_drvdata(pdev, dev);
  1105. dev->hw_lock = 0;
  1106. dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
  1107. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1108. atomic_set(&dev->watchdog_cnt, 0);
  1109. init_timer(&dev->watchdog_timer);
  1110. dev->watchdog_timer.data = (unsigned long)dev;
  1111. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1112. /* Initialize HW ops and commands based on MFC version */
  1113. s5p_mfc_init_hw_ops(dev);
  1114. s5p_mfc_init_hw_cmds(dev);
  1115. s5p_mfc_init_regs(dev);
  1116. pr_debug("%s--\n", __func__);
  1117. return 0;
  1118. /* Deinit MFC if probe had failed */
  1119. err_enc_reg:
  1120. video_device_release(dev->vfd_enc);
  1121. err_enc_alloc:
  1122. video_unregister_device(dev->vfd_dec);
  1123. err_dec_reg:
  1124. video_device_release(dev->vfd_dec);
  1125. err_dec_alloc:
  1126. v4l2_device_unregister(&dev->v4l2_dev);
  1127. err_v4l2_dev_reg:
  1128. s5p_mfc_release_firmware(dev);
  1129. err_alloc_fw:
  1130. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1131. err_mem_init_ctx_1:
  1132. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1133. err_res:
  1134. s5p_mfc_final_pm(dev);
  1135. pr_debug("%s-- with error\n", __func__);
  1136. return ret;
  1137. }
  1138. /* Remove the driver */
  1139. static int s5p_mfc_remove(struct platform_device *pdev)
  1140. {
  1141. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1142. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1143. del_timer_sync(&dev->watchdog_timer);
  1144. flush_workqueue(dev->watchdog_workqueue);
  1145. destroy_workqueue(dev->watchdog_workqueue);
  1146. video_unregister_device(dev->vfd_enc);
  1147. video_unregister_device(dev->vfd_dec);
  1148. v4l2_device_unregister(&dev->v4l2_dev);
  1149. s5p_mfc_release_firmware(dev);
  1150. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1151. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1152. if (pdev->dev.of_node) {
  1153. put_device(dev->mem_dev_l);
  1154. put_device(dev->mem_dev_r);
  1155. }
  1156. s5p_mfc_final_pm(dev);
  1157. return 0;
  1158. }
  1159. #ifdef CONFIG_PM_SLEEP
  1160. static int s5p_mfc_suspend(struct device *dev)
  1161. {
  1162. struct platform_device *pdev = to_platform_device(dev);
  1163. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1164. int ret;
  1165. if (m_dev->num_inst == 0)
  1166. return 0;
  1167. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1168. mfc_err("Error: going to suspend for a second time\n");
  1169. return -EIO;
  1170. }
  1171. /* Check if we're processing then wait if it necessary. */
  1172. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1173. /* Try and lock the HW */
  1174. /* Wait on the interrupt waitqueue */
  1175. ret = wait_event_interruptible_timeout(m_dev->queue,
  1176. m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
  1177. if (ret == 0) {
  1178. mfc_err("Waiting for hardware to finish timed out\n");
  1179. clear_bit(0, &m_dev->enter_suspend);
  1180. return -EIO;
  1181. }
  1182. }
  1183. ret = s5p_mfc_sleep(m_dev);
  1184. if (ret) {
  1185. clear_bit(0, &m_dev->enter_suspend);
  1186. clear_bit(0, &m_dev->hw_lock);
  1187. }
  1188. return ret;
  1189. }
  1190. static int s5p_mfc_resume(struct device *dev)
  1191. {
  1192. struct platform_device *pdev = to_platform_device(dev);
  1193. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1194. if (m_dev->num_inst == 0)
  1195. return 0;
  1196. return s5p_mfc_wakeup(m_dev);
  1197. }
  1198. #endif
  1199. #ifdef CONFIG_PM
  1200. static int s5p_mfc_runtime_suspend(struct device *dev)
  1201. {
  1202. struct platform_device *pdev = to_platform_device(dev);
  1203. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1204. atomic_set(&m_dev->pm.power, 0);
  1205. return 0;
  1206. }
  1207. static int s5p_mfc_runtime_resume(struct device *dev)
  1208. {
  1209. struct platform_device *pdev = to_platform_device(dev);
  1210. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1211. if (!m_dev->alloc_ctx)
  1212. return 0;
  1213. atomic_set(&m_dev->pm.power, 1);
  1214. return 0;
  1215. }
  1216. #endif
  1217. /* Power management */
  1218. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1219. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1220. SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
  1221. NULL)
  1222. };
  1223. static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
  1224. .h264_ctx = MFC_H264_CTX_BUF_SIZE,
  1225. .non_h264_ctx = MFC_CTX_BUF_SIZE,
  1226. .dsc = DESC_BUF_SIZE,
  1227. .shm = SHARED_BUF_SIZE,
  1228. };
  1229. static struct s5p_mfc_buf_size buf_size_v5 = {
  1230. .fw = MAX_FW_SIZE,
  1231. .cpb = MAX_CPB_SIZE,
  1232. .priv = &mfc_buf_size_v5,
  1233. };
  1234. static struct s5p_mfc_buf_align mfc_buf_align_v5 = {
  1235. .base = MFC_BASE_ALIGN_ORDER,
  1236. };
  1237. static struct s5p_mfc_variant mfc_drvdata_v5 = {
  1238. .version = MFC_VERSION,
  1239. .version_bit = MFC_V5_BIT,
  1240. .port_num = MFC_NUM_PORTS,
  1241. .buf_size = &buf_size_v5,
  1242. .buf_align = &mfc_buf_align_v5,
  1243. .fw_name[0] = "s5p-mfc.fw",
  1244. };
  1245. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
  1246. .dev_ctx = MFC_CTX_BUF_SIZE_V6,
  1247. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
  1248. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
  1249. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
  1250. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
  1251. };
  1252. static struct s5p_mfc_buf_size buf_size_v6 = {
  1253. .fw = MAX_FW_SIZE_V6,
  1254. .cpb = MAX_CPB_SIZE_V6,
  1255. .priv = &mfc_buf_size_v6,
  1256. };
  1257. static struct s5p_mfc_buf_align mfc_buf_align_v6 = {
  1258. .base = 0,
  1259. };
  1260. static struct s5p_mfc_variant mfc_drvdata_v6 = {
  1261. .version = MFC_VERSION_V6,
  1262. .version_bit = MFC_V6_BIT,
  1263. .port_num = MFC_NUM_PORTS_V6,
  1264. .buf_size = &buf_size_v6,
  1265. .buf_align = &mfc_buf_align_v6,
  1266. .fw_name[0] = "s5p-mfc-v6.fw",
  1267. /*
  1268. * v6-v2 firmware contains bug fixes and interface change
  1269. * for init buffer command
  1270. */
  1271. .fw_name[1] = "s5p-mfc-v6-v2.fw",
  1272. };
  1273. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
  1274. .dev_ctx = MFC_CTX_BUF_SIZE_V7,
  1275. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
  1276. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
  1277. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
  1278. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
  1279. };
  1280. static struct s5p_mfc_buf_size buf_size_v7 = {
  1281. .fw = MAX_FW_SIZE_V7,
  1282. .cpb = MAX_CPB_SIZE_V7,
  1283. .priv = &mfc_buf_size_v7,
  1284. };
  1285. static struct s5p_mfc_buf_align mfc_buf_align_v7 = {
  1286. .base = 0,
  1287. };
  1288. static struct s5p_mfc_variant mfc_drvdata_v7 = {
  1289. .version = MFC_VERSION_V7,
  1290. .version_bit = MFC_V7_BIT,
  1291. .port_num = MFC_NUM_PORTS_V7,
  1292. .buf_size = &buf_size_v7,
  1293. .buf_align = &mfc_buf_align_v7,
  1294. .fw_name[0] = "s5p-mfc-v7.fw",
  1295. };
  1296. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
  1297. .dev_ctx = MFC_CTX_BUF_SIZE_V8,
  1298. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
  1299. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
  1300. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
  1301. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
  1302. };
  1303. static struct s5p_mfc_buf_size buf_size_v8 = {
  1304. .fw = MAX_FW_SIZE_V8,
  1305. .cpb = MAX_CPB_SIZE_V8,
  1306. .priv = &mfc_buf_size_v8,
  1307. };
  1308. static struct s5p_mfc_buf_align mfc_buf_align_v8 = {
  1309. .base = 0,
  1310. };
  1311. static struct s5p_mfc_variant mfc_drvdata_v8 = {
  1312. .version = MFC_VERSION_V8,
  1313. .version_bit = MFC_V8_BIT,
  1314. .port_num = MFC_NUM_PORTS_V8,
  1315. .buf_size = &buf_size_v8,
  1316. .buf_align = &mfc_buf_align_v8,
  1317. .fw_name[0] = "s5p-mfc-v8.fw",
  1318. };
  1319. static struct platform_device_id mfc_driver_ids[] = {
  1320. {
  1321. .name = "s5p-mfc",
  1322. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1323. }, {
  1324. .name = "s5p-mfc-v5",
  1325. .driver_data = (unsigned long)&mfc_drvdata_v5,
  1326. }, {
  1327. .name = "s5p-mfc-v6",
  1328. .driver_data = (unsigned long)&mfc_drvdata_v6,
  1329. }, {
  1330. .name = "s5p-mfc-v7",
  1331. .driver_data = (unsigned long)&mfc_drvdata_v7,
  1332. }, {
  1333. .name = "s5p-mfc-v8",
  1334. .driver_data = (unsigned long)&mfc_drvdata_v8,
  1335. },
  1336. {},
  1337. };
  1338. MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
  1339. static const struct of_device_id exynos_mfc_match[] = {
  1340. {
  1341. .compatible = "samsung,mfc-v5",
  1342. .data = &mfc_drvdata_v5,
  1343. }, {
  1344. .compatible = "samsung,mfc-v6",
  1345. .data = &mfc_drvdata_v6,
  1346. }, {
  1347. .compatible = "samsung,mfc-v7",
  1348. .data = &mfc_drvdata_v7,
  1349. }, {
  1350. .compatible = "samsung,mfc-v8",
  1351. .data = &mfc_drvdata_v8,
  1352. },
  1353. {},
  1354. };
  1355. MODULE_DEVICE_TABLE(of, exynos_mfc_match);
  1356. static void *mfc_get_drv_data(struct platform_device *pdev)
  1357. {
  1358. struct s5p_mfc_variant *driver_data = NULL;
  1359. if (pdev->dev.of_node) {
  1360. const struct of_device_id *match;
  1361. match = of_match_node(exynos_mfc_match,
  1362. pdev->dev.of_node);
  1363. if (match)
  1364. driver_data = (struct s5p_mfc_variant *)match->data;
  1365. } else {
  1366. driver_data = (struct s5p_mfc_variant *)
  1367. platform_get_device_id(pdev)->driver_data;
  1368. }
  1369. return driver_data;
  1370. }
  1371. static struct platform_driver s5p_mfc_driver = {
  1372. .probe = s5p_mfc_probe,
  1373. .remove = s5p_mfc_remove,
  1374. .id_table = mfc_driver_ids,
  1375. .driver = {
  1376. .name = S5P_MFC_NAME,
  1377. .pm = &s5p_mfc_pm_ops,
  1378. .of_match_table = exynos_mfc_match,
  1379. },
  1380. };
  1381. module_platform_driver(s5p_mfc_driver);
  1382. MODULE_LICENSE("GPL");
  1383. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1384. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");