s5p_mfc.c 42 KB

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