s5p_mfc.c 46 KB

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