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